1 /* $OpenBSD: quotacheck.c,v 1.41 2019/06/28 13:32:45 deraadt 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 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 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 * 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 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 char *cp; 273 274 switch (pid = fork()) { 275 case -1: /* error */ 276 warn("fork"); 277 return 1; 278 case 0: /* child */ 279 if ((fi = opendev(fsname, O_RDONLY, 0, NULL)) == -1) 280 err(1, "%s", fsname); 281 sync(); 282 for (i = 0; sblock_try[i] != -1; i++) { 283 bread(sblock_try[i] / DEV_BSIZE, (char *)&sblock, 284 (long)SBLOCKSIZE); 285 if ((sblock.fs_magic == FS_UFS1_MAGIC || 286 (sblock.fs_magic == FS_UFS2_MAGIC && 287 sblock.fs_sblockloc == sblock_try[i])) && 288 sblock.fs_bsize <= MAXBSIZE && 289 sblock.fs_bsize >= sizeof(struct fs)) 290 break; 291 } 292 if (sblock_try[i] == -1) { 293 warn("Cannot find file system superblock"); 294 return (1); 295 } 296 maxino = sblock.fs_ncg * sblock.fs_ipg; 297 for (cg = 0; cg < sblock.fs_ncg; cg++) { 298 ino = cg * sblock.fs_ipg; 299 setinodebuf(ino); 300 bread(fsbtodb(&sblock, cgtod(&sblock, cg)), 301 (char *)(&cgblk), sblock.fs_cgsize); 302 if (sblock.fs_magic == FS_UFS2_MAGIC) 303 inosused = cgblk.cg_initediblk; 304 else 305 inosused = sblock.fs_ipg; 306 /* 307 * If we are using soft updates, then we can trust the 308 * cylinder group inode allocation maps to tell us which 309 * inodes are allocated. We will scan the used inode map 310 * to find the inodes that are really in use, and then 311 * read only those inodes in from disk. 312 */ 313 if (sblock.fs_flags & FS_DOSOFTDEP) { 314 if (!cg_chkmagic(&cgblk)) 315 errx(1, "CG %d: BAD MAGIC NUMBER\n", cg); 316 cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT]; 317 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) { 318 if (*cp == 0) 319 continue; 320 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) { 321 if (*cp & i) 322 break; 323 inosused--; 324 } 325 break; 326 } 327 if (inosused <= 0) 328 continue; 329 } 330 for (i = 0; i < inosused; i++, ino++) { 331 if ((dp = getnextinode(ino)) == NULL || 332 ino < ROOTINO || 333 (mode = DIP(dp, di_mode) & IFMT) == 0) 334 continue; 335 if (qnp->flags & HASGRP) { 336 fup = addid(DIP(dp, di_gid), 337 GRPQUOTA, NULL); 338 fup->fu_curinodes++; 339 if (mode == IFREG || mode == IFDIR || 340 mode == IFLNK) 341 fup->fu_curblocks += 342 DIP(dp, di_blocks); 343 } 344 if (qnp->flags & HASUSR) { 345 fup = addid(DIP(dp, di_uid), 346 USRQUOTA, NULL); 347 fup->fu_curinodes++; 348 if (mode == IFREG || mode == IFDIR || 349 mode == IFLNK) 350 fup->fu_curblocks += 351 DIP(dp, di_blocks); 352 } 353 } 354 } 355 freeinodebuf(); 356 if (flags&(CHECK_DEBUG|CHECK_VERBOSE)) { 357 (void)printf("*** Checking "); 358 if (qnp->flags & HASUSR) { 359 (void)printf("%s", qfextension[USRQUOTA]); 360 if (qnp->flags & HASGRP) 361 (void)printf(" and "); 362 } 363 if (qnp->flags & HASGRP) 364 (void)printf("%s", qfextension[GRPQUOTA]); 365 (void)printf(" quotas for %s (%s), %swait\n", 366 fsname, mntpt, pidp? "no" : ""); 367 } 368 if (qnp->flags & HASUSR) 369 errs += update(mntpt, qnp->usrqfname, USRQUOTA); 370 if (qnp->flags & HASGRP) 371 errs += update(mntpt, qnp->grpqfname, GRPQUOTA); 372 close(fi); 373 exit (errs); 374 break; 375 default: /* parent */ 376 if (pidp != NULL) { 377 *pidp = pid; 378 return 0; 379 } 380 if (waitpid(pid, &status, 0) == -1) { 381 warn("waitpid"); 382 return 1; 383 } 384 if (WIFEXITED(status)) { 385 if (WEXITSTATUS(status) != 0) 386 return WEXITSTATUS(status); 387 } else if (WIFSIGNALED(status)) { 388 warnx("%s: %s", fsname, strsignal(WTERMSIG(status))); 389 return 1; 390 } 391 break; 392 } 393 return (0); 394 } 395 396 /* 397 * Update a specified quota file. 398 */ 399 int 400 update(const char *fsname, const char *quotafile, int type) 401 { 402 struct fileusage *fup; 403 FILE *qfi, *qfo; 404 u_int32_t id, lastid; 405 struct dqblk dqbuf; 406 static int warned = 0; 407 static struct dqblk zerodqbuf; 408 static struct fileusage zerofileusage; 409 410 if (flags&CHECK_DEBUG) 411 printf("updating: %s\n", quotafile); 412 413 if ((qfo = fopen(quotafile, (flags&CHECK_DEBUG)? "r" : "r+")) == NULL) { 414 if (errno == ENOENT) 415 qfo = fopen(quotafile, "w+"); 416 if (qfo) { 417 warnx("creating quota file: %s", quotafile); 418 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP) 419 (void) fchown(fileno(qfo), getuid(), getquotagid()); 420 (void) fchmod(fileno(qfo), MODE); 421 } else { 422 warn("%s", quotafile); 423 return (1); 424 } 425 } 426 if ((qfi = fopen(quotafile, "r")) == NULL) { 427 warn("%s", quotafile); 428 (void) fclose(qfo); 429 return (1); 430 } 431 if (quotactl(fsname, QCMD(Q_SYNC, type), 0, (caddr_t)0) == -1 && 432 errno == EOPNOTSUPP && !warned && 433 (flags&(CHECK_DEBUG|CHECK_VERBOSE))) { 434 warned++; 435 (void)printf("*** Warning: %s\n", 436 "Quotas are not compiled into this kernel"); 437 } 438 for (lastid = highid[type], id = 0; id <= lastid; id++) { 439 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0) 440 dqbuf = zerodqbuf; 441 if ((fup = lookup(id, type)) == 0) 442 fup = &zerofileusage; 443 if (dqbuf.dqb_curinodes == fup->fu_curinodes && 444 dqbuf.dqb_curblocks == fup->fu_curblocks) { 445 fup->fu_curinodes = 0; 446 fup->fu_curblocks = 0; 447 fseek(qfo, (long)sizeof(struct dqblk), SEEK_CUR); 448 continue; 449 } 450 if (flags&(CHECK_DEBUG|CHECK_VERBOSE)) { 451 if (flags&CHECK_PREEN) 452 printf("%s: ", fsname); 453 printf("%-8s fixed:", fup->fu_name); 454 if (dqbuf.dqb_curinodes != fup->fu_curinodes) 455 (void)printf("\tinodes %d -> %u", 456 dqbuf.dqb_curinodes, fup->fu_curinodes); 457 if (dqbuf.dqb_curblocks != fup->fu_curblocks) 458 (void)printf("\tblocks %u -> %u", 459 dqbuf.dqb_curblocks, fup->fu_curblocks); 460 (void)printf("\n"); 461 } 462 /* 463 * Reset time limit if have a soft limit and were 464 * previously under it, but are now over it. 465 */ 466 if (dqbuf.dqb_bsoftlimit && 467 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && 468 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit) 469 dqbuf.dqb_btime = 0; 470 if (dqbuf.dqb_isoftlimit && 471 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit && 472 fup->fu_curblocks >= dqbuf.dqb_isoftlimit) 473 dqbuf.dqb_itime = 0; 474 dqbuf.dqb_curinodes = fup->fu_curinodes; 475 dqbuf.dqb_curblocks = fup->fu_curblocks; 476 if (!(flags & CHECK_DEBUG)) { 477 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo); 478 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id, 479 (caddr_t)&dqbuf); 480 } 481 fup->fu_curinodes = 0; 482 fup->fu_curblocks = 0; 483 } 484 fclose(qfi); 485 fflush(qfo); 486 if (!(flags & CHECK_DEBUG)) 487 ftruncate(fileno(qfo), 488 (off_t)((highid[type] + 1) * sizeof(struct dqblk))); 489 fclose(qfo); 490 return (0); 491 } 492 493 /* 494 * Check to see if realpath(target) matches a realpath() in list of size cnt. 495 */ 496 int 497 oneof_realpath(char *target, char *list[], int cnt) 498 { 499 int i; 500 char realtarget[PATH_MAX], realargv[PATH_MAX]; 501 char *rv; 502 503 rv = realpath(target, realtarget); 504 if (rv == NULL) 505 return (-1); 506 507 for (i = 0; i < cnt; i++) { 508 rv = realpath(list[i], realargv); 509 if (rv && strcmp(realtarget, realargv) == 0) 510 break; 511 } 512 513 if (i < cnt) 514 return (i); 515 else 516 return (-1); 517 } 518 519 /* 520 * Check to see if opendev(target) matches a opendev() in list of size cnt. 521 */ 522 int 523 oneof_specname(char *target, char *list[], int cnt) 524 { 525 int i, fd; 526 char *tmp, *targetdev, *argvdev; 527 528 fd = opendev(target, O_RDONLY, 0, &tmp); 529 if (fd == -1) 530 return (-1); 531 close(fd); 532 targetdev = strdup(tmp); 533 534 for (i = 0; i < cnt; i++) { 535 fd = opendev(list[i], O_RDONLY, 0, &argvdev); 536 if (fd == -1) 537 continue; 538 close(fd); 539 if (strcmp(targetdev, argvdev) == 0) 540 break; 541 } 542 543 free(targetdev); 544 545 if (i < cnt) 546 return (i); 547 else 548 return (-1); 549 } 550 551 /* 552 * Determine the group identifier for quota files. 553 */ 554 int 555 getquotagid(void) 556 { 557 struct group *gr; 558 559 if ((gr = getgrnam(quotagroup)) != NULL) 560 return (gr->gr_gid); 561 return (-1); 562 } 563 564 /* 565 * Check to see if a particular quota is to be enabled. 566 */ 567 int 568 hasquota(struct fstab *fs, int type, char **qfnamep) 569 { 570 char *opt, *cp; 571 static char initname, usrname[100], grpname[100]; 572 static char buf[BUFSIZ]; 573 574 if (!initname) { 575 (void)snprintf(usrname, sizeof(usrname), 576 "%s%s", qfextension[USRQUOTA], qfname); 577 (void)snprintf(grpname, sizeof(grpname), 578 "%s%s", qfextension[GRPQUOTA], qfname); 579 initname = 1; 580 } 581 (void)strlcpy(buf, fs->fs_mntops, sizeof(buf)); 582 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 583 if ((cp = strchr(opt, '=')) != NULL) 584 *cp++ = '\0'; 585 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 586 break; 587 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 588 break; 589 } 590 if (!opt) 591 return (0); 592 if (cp) 593 *qfnamep = cp; 594 else { 595 (void)snprintf(buf, sizeof(buf), 596 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 597 *qfnamep = buf; 598 } 599 return (1); 600 } 601 602 /* 603 * Routines to manage the file usage table. 604 * 605 * Lookup an id of a specific type. 606 */ 607 struct fileusage * 608 lookup(u_int32_t id, int type) 609 { 610 struct fileusage *fup; 611 612 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next) 613 if (fup->fu_id == id) 614 return (fup); 615 return (NULL); 616 } 617 618 /* 619 * Add a new file usage id if it does not already exist. 620 */ 621 struct fileusage * 622 addid(u_int32_t id, int type, char *name) 623 { 624 struct fileusage *fup, **fhp; 625 int len; 626 627 if ((fup = lookup(id, type)) != NULL) 628 return (fup); 629 if (name) 630 len = strlen(name); 631 else 632 len = 10; 633 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL) 634 err(1, "%s", strerror(errno)); 635 fhp = &fuhead[type][id & (FUHASH - 1)]; 636 fup->fu_next = *fhp; 637 *fhp = fup; 638 fup->fu_id = id; 639 if (id > highid[type]) 640 highid[type] = id; 641 if (name) 642 memcpy(fup->fu_name, name, len + 1); 643 else 644 (void)snprintf(fup->fu_name, len, "%u", id); 645 return (fup); 646 } 647 648 /* 649 * Special purpose version of ginode used to optimize pass 650 * over all the inodes in numerical order. 651 */ 652 static ino_t nextino, lastinum, lastvalidinum; 653 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 654 static caddr_t inodebuf; 655 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */ 656 657 union dinode * 658 getnextinode(ino_t inumber) 659 { 660 long size; 661 daddr_t dblk; 662 union dinode *dp; 663 static caddr_t nextinop; 664 665 if (inumber != nextino++ || inumber > lastvalidinum) 666 err(1, "bad inode number %llu to nextinode", 667 (unsigned long long)inumber); 668 if (inumber >= lastinum) { 669 readcnt++; 670 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum)); 671 if (readcnt % readpercg == 0) { 672 size = partialsize; 673 lastinum += partialcnt; 674 } else { 675 size = inobufsize; 676 lastinum += fullcnt; 677 } 678 /* 679 * If bread returns an error, it will already have zeroed 680 * out the buffer, so we do not need to do so here. 681 */ 682 bread(dblk, inodebuf, size); 683 nextinop = inodebuf; 684 } 685 dp = (union dinode *)nextinop; 686 if (sblock.fs_magic == FS_UFS1_MAGIC) 687 nextinop += sizeof(struct ufs1_dinode); 688 else 689 nextinop += sizeof(struct ufs2_dinode); 690 return (dp); 691 } 692 693 /* 694 * Prepare to scan a set of inodes. 695 */ 696 void 697 setinodebuf(ino_t inum) 698 { 699 700 if (inum % sblock.fs_ipg != 0) 701 errx(1, "bad inode number %llu to setinodebuf", 702 (unsigned long long)inum); 703 lastvalidinum = inum + sblock.fs_ipg - 1; 704 nextino = inum; 705 lastinum = inum; 706 readcnt = 0; 707 if (inodebuf != NULL) 708 return; 709 inobufsize = blkroundup(&sblock, INOBUFSIZE); 710 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ? 711 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 712 readpercg = sblock.fs_ipg / fullcnt; 713 partialcnt = sblock.fs_ipg % fullcnt; 714 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ? 715 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 716 if (partialcnt != 0) { 717 readpercg++; 718 } else { 719 partialcnt = fullcnt; 720 partialsize = inobufsize; 721 } 722 if ((inodebuf = malloc((size_t)inobufsize)) == NULL) 723 errx(1, "cannot allocate space for inode buffer"); 724 } 725 726 /* 727 * Free up data structures used to scan inodes. 728 */ 729 void 730 freeinodebuf(void) 731 { 732 733 free(inodebuf); 734 inodebuf = NULL; 735 } 736 737 /* 738 * Read specified disk blocks. 739 */ 740 void 741 bread(daddr_t bno, char *buf, long cnt) 742 { 743 if (pread(fi, buf, cnt, bno * DEV_BSIZE) != cnt) 744 err(1, "read failed on block %lld", (long long)bno); 745 } 746