1 /* $OpenBSD: quotacheck.c,v 1.43 2024/09/15 07:14:58 jsg Exp $ */
2 /* $NetBSD: quotacheck.c,v 1.12 1996/03/30 22:34:25 mark Exp $ */
3
4 /*
5 * Copyright (c) 1980, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Robert Elz at The University of Melbourne.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 /*
37 * Fix up / report on disk quotas & usage
38 */
39 #include <sys/param.h> /* DEV_BSIZE MAXBSIZE */
40 #include <sys/stat.h>
41 #include <sys/wait.h>
42
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/quota.h>
45 #include <ufs/ffs/fs.h>
46
47 #include <fcntl.h>
48 #include <fstab.h>
49 #include <pwd.h>
50 #include <grp.h>
51 #include <errno.h>
52 #include <unistd.h>
53 #include <limits.h>
54 #include <util.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <err.h>
59 #include "fsutil.h"
60
61 char *qfname = QUOTAFILENAME;
62 char *qfextension[] = INITQFNAMES;
63 char *quotagroup = QUOTAGROUP;
64
65 union {
66 struct fs sblk;
67 char dummy[MAXBSIZE];
68 } sb_un;
69 #define sblock sb_un.sblk
70 union {
71 struct cg cgblk;
72 char dummy[MAXBSIZE];
73 } cg_un;
74 #define cgblk cg_un.cgblk
75
76 long maxino;
77
78 union dinode {
79 struct ufs1_dinode dp1;
80 struct ufs2_dinode dp2;
81 };
82 #define DIP(dp, field) \
83 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
84 (dp)->dp1.field : (dp)->dp2.field)
85
86 struct quotaname {
87 long flags;
88 char grpqfname[PATH_MAX + 1];
89 char usrqfname[PATH_MAX + 1];
90 };
91 #define HASUSR 1
92 #define HASGRP 2
93
94 struct fileusage {
95 struct fileusage *fu_next;
96 u_int32_t fu_curinodes;
97 u_int32_t fu_curblocks;
98 u_int32_t fu_id; /* uid_t or gid_t */
99 char fu_name[1];
100 /* actually bigger */
101 };
102 #define FUHASH 1024 /* must be power of two */
103 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
104
105 int gflag; /* check group quotas */
106 int uflag; /* check user quotas */
107 int flags; /* check flags (avd) */
108 int fi; /* open disk file descriptor */
109 u_int32_t highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
110
111 struct fileusage *
112 addid(u_int32_t, int, char *);
113 char *blockcheck(char *);
114 void bread(daddr_t, char *, long);
115 int chkquota(const char *, const char *, const char *, void *, pid_t *);
116 void freeinodebuf(void);
117 union dinode *
118 getnextinode(ino_t);
119 int getquotagid(void);
120 int hasquota(struct fstab *, int, char **);
121 struct fileusage *
122 lookup(u_int32_t, int);
123 void *needchk(struct fstab *);
124 int oneof_realpath(char *, char*[], int);
125 int oneof_specname(char *, char*[], int);
126 void setinodebuf(ino_t);
127 int update(const char *, const char *, int);
128 void usage(void);
129
130 int
main(int argc,char * argv[])131 main(int argc, char *argv[])
132 {
133 struct fstab *fs;
134 struct passwd *pw;
135 struct group *gr;
136 struct quotaname *auxdata;
137 int i, argnum, maxrun, errs, ch;
138 u_int64_t done = 0; /* XXX supports maximum 64 filesystems */
139 const char *errstr;
140 char *name;
141
142 checkroot();
143
144 errs = maxrun = 0;
145 while ((ch = getopt(argc, argv, "adguvl:")) != -1) {
146 switch(ch) {
147 case 'a':
148 flags |= CHECK_PREEN;
149 break;
150 case 'd':
151 flags |= CHECK_DEBUG;
152 break;
153 case 'g':
154 gflag = 1;
155 break;
156 case 'l':
157 maxrun = strtonum(optarg, 0, INT_MAX, &errstr);
158 if (errstr)
159 errx(1, "-l %s: %s", optarg, errstr);
160 break;
161 case 'u':
162 uflag = 1;
163 break;
164 case 'v':
165 flags |= CHECK_VERBOSE;
166 break;
167 default:
168 usage();
169 }
170 }
171 argc -= optind;
172 argv += optind;
173 if ((argc == 0 && !(flags&CHECK_PREEN)) ||
174 (argc > 0 && (flags&CHECK_PREEN)))
175 usage();
176 if (!gflag && !uflag) {
177 gflag++;
178 uflag++;
179 }
180 if (gflag) {
181 setgrent();
182 while ((gr = getgrent()) != 0)
183 (void) addid(gr->gr_gid, GRPQUOTA, gr->gr_name);
184 endgrent();
185 }
186 if (uflag) {
187 setpwent();
188 while ((pw = getpwent()) != 0)
189 (void) addid(pw->pw_uid, USRQUOTA, pw->pw_name);
190 endpwent();
191 }
192 if (flags&CHECK_PREEN)
193 exit(checkfstab(flags, maxrun, needchk, chkquota));
194 if (setfsent() == 0)
195 err(1, "%s: can't open", _PATH_FSTAB);
196 while ((fs = getfsent()) != NULL) {
197 if (((argnum = oneof_realpath(fs->fs_file, argv, argc)) >= 0 ||
198 (argnum = oneof_specname(fs->fs_spec, argv, argc)) >= 0) &&
199 (auxdata = needchk(fs)) &&
200 (name = blockcheck(fs->fs_spec))) {
201 done |= 1 << argnum;
202 errs += chkquota(fs->fs_vfstype, name,
203 fs->fs_file, auxdata, NULL);
204 }
205 }
206 endfsent();
207 for (i = 0; i < argc; i++)
208 if ((done & (1 << i)) == 0)
209 fprintf(stderr, "%s not found in %s\n",
210 argv[i], _PATH_FSTAB);
211 exit(errs);
212 }
213
214 void
usage(void)215 usage(void)
216 {
217 extern char *__progname;
218 (void)fprintf(stderr, "usage: %s [-adguv] [-l maxparallel] "
219 "filesystem ...\n", __progname);
220 exit(1);
221 }
222
223 void *
needchk(struct fstab * fs)224 needchk(struct fstab *fs)
225 {
226 struct quotaname *qnp;
227 char *qfnp;
228
229 if (fs->fs_passno == 0)
230 return NULL;
231 if (strcmp(fs->fs_type, FSTAB_RW))
232 return (NULL);
233 if (strcmp(fs->fs_vfstype, "ffs") &&
234 strcmp(fs->fs_vfstype, "ufs") &&
235 strcmp(fs->fs_vfstype, "mfs"))
236 return (NULL);
237 if ((qnp = malloc(sizeof(*qnp))) == NULL)
238 err(1, "%s", strerror(errno));
239 qnp->flags = 0;
240 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
241 strlcpy(qnp->grpqfname, qfnp, sizeof qnp->grpqfname);
242 qnp->flags |= HASGRP;
243 }
244 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
245 strlcpy(qnp->usrqfname, qfnp, sizeof qnp->usrqfname);
246 qnp->flags |= HASUSR;
247 }
248 if (qnp->flags)
249 return (qnp);
250 free(qnp);
251 return (NULL);
252 }
253
254 /*
255 * Possible superblock locations ordered from most to least likely.
256 */
257 static int sblock_try[] = SBLOCKSEARCH;
258
259 /*
260 * Scan the specified file system to check quota(s) present on it.
261 */
262 int
chkquota(const char * vfstype,const char * fsname,const char * mntpt,void * auxarg,pid_t * pidp)263 chkquota(const char *vfstype, const char *fsname, const char *mntpt,
264 void *auxarg, pid_t *pidp)
265 {
266 struct quotaname *qnp = auxarg;
267 struct fileusage *fup;
268 union dinode *dp;
269 int cg, i, mode, errs = 0, status;
270 ino_t ino, inosused;
271 pid_t pid;
272
273 switch (pid = fork()) {
274 case -1: /* error */
275 warn("fork");
276 return 1;
277 case 0: /* child */
278 if ((fi = opendev(fsname, O_RDONLY, 0, NULL)) == -1)
279 err(1, "%s", fsname);
280 sync();
281 for (i = 0; sblock_try[i] != -1; i++) {
282 bread(sblock_try[i] / DEV_BSIZE, (char *)&sblock,
283 (long)SBLOCKSIZE);
284 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
285 (sblock.fs_magic == FS_UFS2_MAGIC &&
286 sblock.fs_sblockloc == sblock_try[i])) &&
287 sblock.fs_bsize <= MAXBSIZE &&
288 sblock.fs_bsize >= sizeof(struct fs))
289 break;
290 }
291 if (sblock_try[i] == -1) {
292 warn("Cannot find file system superblock");
293 return (1);
294 }
295 maxino = sblock.fs_ncg * sblock.fs_ipg;
296 for (cg = 0; cg < sblock.fs_ncg; cg++) {
297 ino = cg * sblock.fs_ipg;
298 setinodebuf(ino);
299 bread(fsbtodb(&sblock, cgtod(&sblock, cg)),
300 (char *)(&cgblk), sblock.fs_cgsize);
301 if (sblock.fs_magic == FS_UFS2_MAGIC)
302 inosused = cgblk.cg_initediblk;
303 else
304 inosused = sblock.fs_ipg;
305 for (i = 0; i < inosused; i++, ino++) {
306 if ((dp = getnextinode(ino)) == NULL ||
307 ino < ROOTINO ||
308 (mode = DIP(dp, di_mode) & IFMT) == 0)
309 continue;
310 if (qnp->flags & HASGRP) {
311 fup = addid(DIP(dp, di_gid),
312 GRPQUOTA, NULL);
313 fup->fu_curinodes++;
314 if (mode == IFREG || mode == IFDIR ||
315 mode == IFLNK)
316 fup->fu_curblocks +=
317 DIP(dp, di_blocks);
318 }
319 if (qnp->flags & HASUSR) {
320 fup = addid(DIP(dp, di_uid),
321 USRQUOTA, NULL);
322 fup->fu_curinodes++;
323 if (mode == IFREG || mode == IFDIR ||
324 mode == IFLNK)
325 fup->fu_curblocks +=
326 DIP(dp, di_blocks);
327 }
328 }
329 }
330 freeinodebuf();
331 if (flags&(CHECK_DEBUG|CHECK_VERBOSE)) {
332 (void)printf("*** Checking ");
333 if (qnp->flags & HASUSR) {
334 (void)printf("%s", qfextension[USRQUOTA]);
335 if (qnp->flags & HASGRP)
336 (void)printf(" and ");
337 }
338 if (qnp->flags & HASGRP)
339 (void)printf("%s", qfextension[GRPQUOTA]);
340 (void)printf(" quotas for %s (%s), %swait\n",
341 fsname, mntpt, pidp? "no" : "");
342 }
343 if (qnp->flags & HASUSR)
344 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
345 if (qnp->flags & HASGRP)
346 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
347 close(fi);
348 exit (errs);
349 break;
350 default: /* parent */
351 if (pidp != NULL) {
352 *pidp = pid;
353 return 0;
354 }
355 if (waitpid(pid, &status, 0) == -1) {
356 warn("waitpid");
357 return 1;
358 }
359 if (WIFEXITED(status)) {
360 if (WEXITSTATUS(status) != 0)
361 return WEXITSTATUS(status);
362 } else if (WIFSIGNALED(status)) {
363 warnx("%s: %s", fsname, strsignal(WTERMSIG(status)));
364 return 1;
365 }
366 break;
367 }
368 return (0);
369 }
370
371 /*
372 * Update a specified quota file.
373 */
374 int
update(const char * fsname,const char * quotafile,int type)375 update(const char *fsname, const char *quotafile, int type)
376 {
377 struct fileusage *fup;
378 FILE *qfi, *qfo;
379 u_int32_t id, lastid;
380 struct dqblk dqbuf;
381 static int warned = 0;
382 static struct dqblk zerodqbuf;
383 static struct fileusage zerofileusage;
384
385 if (flags&CHECK_DEBUG)
386 printf("updating: %s\n", quotafile);
387
388 if ((qfo = fopen(quotafile, (flags&CHECK_DEBUG)? "r" : "r+")) == NULL) {
389 if (errno == ENOENT)
390 qfo = fopen(quotafile, "w+");
391 if (qfo) {
392 warnx("creating quota file: %s", quotafile);
393 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
394 (void) fchown(fileno(qfo), getuid(), getquotagid());
395 (void) fchmod(fileno(qfo), MODE);
396 } else {
397 warn("%s", quotafile);
398 return (1);
399 }
400 }
401 if ((qfi = fopen(quotafile, "r")) == NULL) {
402 warn("%s", quotafile);
403 (void) fclose(qfo);
404 return (1);
405 }
406 if (quotactl(fsname, QCMD(Q_SYNC, type), 0, (caddr_t)0) == -1 &&
407 errno == EOPNOTSUPP && !warned &&
408 (flags&(CHECK_DEBUG|CHECK_VERBOSE))) {
409 warned++;
410 (void)printf("*** Warning: %s\n",
411 "Quotas are not compiled into this kernel");
412 }
413 for (lastid = highid[type], id = 0; id <= lastid; id++) {
414 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
415 dqbuf = zerodqbuf;
416 if ((fup = lookup(id, type)) == 0)
417 fup = &zerofileusage;
418 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
419 dqbuf.dqb_curblocks == fup->fu_curblocks) {
420 fup->fu_curinodes = 0;
421 fup->fu_curblocks = 0;
422 fseek(qfo, (long)sizeof(struct dqblk), SEEK_CUR);
423 continue;
424 }
425 if (flags&(CHECK_DEBUG|CHECK_VERBOSE)) {
426 if (flags&CHECK_PREEN)
427 printf("%s: ", fsname);
428 printf("%-8s fixed:", fup->fu_name);
429 if (dqbuf.dqb_curinodes != fup->fu_curinodes)
430 (void)printf("\tinodes %d -> %u",
431 dqbuf.dqb_curinodes, fup->fu_curinodes);
432 if (dqbuf.dqb_curblocks != fup->fu_curblocks)
433 (void)printf("\tblocks %u -> %u",
434 dqbuf.dqb_curblocks, fup->fu_curblocks);
435 (void)printf("\n");
436 }
437 /*
438 * Reset time limit if have a soft limit and were
439 * previously under it, but are now over it.
440 */
441 if (dqbuf.dqb_bsoftlimit &&
442 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
443 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
444 dqbuf.dqb_btime = 0;
445 if (dqbuf.dqb_isoftlimit &&
446 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
447 fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
448 dqbuf.dqb_itime = 0;
449 dqbuf.dqb_curinodes = fup->fu_curinodes;
450 dqbuf.dqb_curblocks = fup->fu_curblocks;
451 if (!(flags & CHECK_DEBUG)) {
452 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
453 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
454 (caddr_t)&dqbuf);
455 }
456 fup->fu_curinodes = 0;
457 fup->fu_curblocks = 0;
458 }
459 fclose(qfi);
460 fflush(qfo);
461 if (!(flags & CHECK_DEBUG))
462 ftruncate(fileno(qfo),
463 (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
464 fclose(qfo);
465 return (0);
466 }
467
468 /*
469 * Check to see if realpath(target) matches a realpath() in list of size cnt.
470 */
471 int
oneof_realpath(char * target,char * list[],int cnt)472 oneof_realpath(char *target, char *list[], int cnt)
473 {
474 int i;
475 char realtarget[PATH_MAX], realargv[PATH_MAX];
476 char *rv;
477
478 rv = realpath(target, realtarget);
479 if (rv == NULL)
480 return (-1);
481
482 for (i = 0; i < cnt; i++) {
483 rv = realpath(list[i], realargv);
484 if (rv && strcmp(realtarget, realargv) == 0)
485 break;
486 }
487
488 if (i < cnt)
489 return (i);
490 else
491 return (-1);
492 }
493
494 /*
495 * Check to see if opendev(target) matches a opendev() in list of size cnt.
496 */
497 int
oneof_specname(char * target,char * list[],int cnt)498 oneof_specname(char *target, char *list[], int cnt)
499 {
500 int i, fd;
501 char *tmp, *targetdev, *argvdev;
502
503 fd = opendev(target, O_RDONLY, 0, &tmp);
504 if (fd == -1)
505 return (-1);
506 close(fd);
507 targetdev = strdup(tmp);
508
509 for (i = 0; i < cnt; i++) {
510 fd = opendev(list[i], O_RDONLY, 0, &argvdev);
511 if (fd == -1)
512 continue;
513 close(fd);
514 if (strcmp(targetdev, argvdev) == 0)
515 break;
516 }
517
518 free(targetdev);
519
520 if (i < cnt)
521 return (i);
522 else
523 return (-1);
524 }
525
526 /*
527 * Determine the group identifier for quota files.
528 */
529 int
getquotagid(void)530 getquotagid(void)
531 {
532 struct group *gr;
533
534 if ((gr = getgrnam(quotagroup)) != NULL)
535 return (gr->gr_gid);
536 return (-1);
537 }
538
539 /*
540 * Check to see if a particular quota is to be enabled.
541 */
542 int
hasquota(struct fstab * fs,int type,char ** qfnamep)543 hasquota(struct fstab *fs, int type, char **qfnamep)
544 {
545 char *opt, *cp;
546 static char initname, usrname[100], grpname[100];
547 static char buf[BUFSIZ];
548
549 if (!initname) {
550 (void)snprintf(usrname, sizeof(usrname),
551 "%s%s", qfextension[USRQUOTA], qfname);
552 (void)snprintf(grpname, sizeof(grpname),
553 "%s%s", qfextension[GRPQUOTA], qfname);
554 initname = 1;
555 }
556 (void)strlcpy(buf, fs->fs_mntops, sizeof(buf));
557 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
558 if ((cp = strchr(opt, '=')) != NULL)
559 *cp++ = '\0';
560 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
561 break;
562 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
563 break;
564 }
565 if (!opt)
566 return (0);
567 if (cp)
568 *qfnamep = cp;
569 else {
570 (void)snprintf(buf, sizeof(buf),
571 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
572 *qfnamep = buf;
573 }
574 return (1);
575 }
576
577 /*
578 * Routines to manage the file usage table.
579 *
580 * Lookup an id of a specific type.
581 */
582 struct fileusage *
lookup(u_int32_t id,int type)583 lookup(u_int32_t id, int type)
584 {
585 struct fileusage *fup;
586
587 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
588 if (fup->fu_id == id)
589 return (fup);
590 return (NULL);
591 }
592
593 /*
594 * Add a new file usage id if it does not already exist.
595 */
596 struct fileusage *
addid(u_int32_t id,int type,char * name)597 addid(u_int32_t id, int type, char *name)
598 {
599 struct fileusage *fup, **fhp;
600 int len;
601
602 if ((fup = lookup(id, type)) != NULL)
603 return (fup);
604 if (name)
605 len = strlen(name);
606 else
607 len = 10;
608 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
609 err(1, "%s", strerror(errno));
610 fhp = &fuhead[type][id & (FUHASH - 1)];
611 fup->fu_next = *fhp;
612 *fhp = fup;
613 fup->fu_id = id;
614 if (id > highid[type])
615 highid[type] = id;
616 if (name)
617 memcpy(fup->fu_name, name, len + 1);
618 else
619 (void)snprintf(fup->fu_name, len, "%u", id);
620 return (fup);
621 }
622
623 /*
624 * Special purpose version of ginode used to optimize pass
625 * over all the inodes in numerical order.
626 */
627 static ino_t nextino, lastinum, lastvalidinum;
628 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
629 static caddr_t inodebuf;
630 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
631
632 union dinode *
getnextinode(ino_t inumber)633 getnextinode(ino_t inumber)
634 {
635 long size;
636 daddr_t dblk;
637 union dinode *dp;
638 static caddr_t nextinop;
639
640 if (inumber != nextino++ || inumber > lastvalidinum)
641 err(1, "bad inode number %llu to nextinode",
642 (unsigned long long)inumber);
643 if (inumber >= lastinum) {
644 readcnt++;
645 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
646 if (readcnt % readpercg == 0) {
647 size = partialsize;
648 lastinum += partialcnt;
649 } else {
650 size = inobufsize;
651 lastinum += fullcnt;
652 }
653 /*
654 * If bread returns an error, it will already have zeroed
655 * out the buffer, so we do not need to do so here.
656 */
657 bread(dblk, inodebuf, size);
658 nextinop = inodebuf;
659 }
660 dp = (union dinode *)nextinop;
661 if (sblock.fs_magic == FS_UFS1_MAGIC)
662 nextinop += sizeof(struct ufs1_dinode);
663 else
664 nextinop += sizeof(struct ufs2_dinode);
665 return (dp);
666 }
667
668 /*
669 * Prepare to scan a set of inodes.
670 */
671 void
setinodebuf(ino_t inum)672 setinodebuf(ino_t inum)
673 {
674
675 if (inum % sblock.fs_ipg != 0)
676 errx(1, "bad inode number %llu to setinodebuf",
677 (unsigned long long)inum);
678 lastvalidinum = inum + sblock.fs_ipg - 1;
679 nextino = inum;
680 lastinum = inum;
681 readcnt = 0;
682 if (inodebuf != NULL)
683 return;
684 inobufsize = blkroundup(&sblock, INOBUFSIZE);
685 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
686 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
687 readpercg = sblock.fs_ipg / fullcnt;
688 partialcnt = sblock.fs_ipg % fullcnt;
689 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
690 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
691 if (partialcnt != 0) {
692 readpercg++;
693 } else {
694 partialcnt = fullcnt;
695 partialsize = inobufsize;
696 }
697 if ((inodebuf = malloc((size_t)inobufsize)) == NULL)
698 errx(1, "cannot allocate space for inode buffer");
699 }
700
701 /*
702 * Free up data structures used to scan inodes.
703 */
704 void
freeinodebuf(void)705 freeinodebuf(void)
706 {
707
708 free(inodebuf);
709 inodebuf = NULL;
710 }
711
712 /*
713 * Read specified disk blocks.
714 */
715 void
bread(daddr_t bno,char * buf,long cnt)716 bread(daddr_t bno, char *buf, long cnt)
717 {
718 if (pread(fi, buf, cnt, bno * DEV_BSIZE) != cnt)
719 err(1, "read failed on block %lld", (long long)bno);
720 }
721