1 /* 2 * Copyright (c) 1980, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Robert Elz at The University of Melbourne. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #if 0 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1990, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)quotacheck.c 8.3 (Berkeley) 1/29/94"; 42 #endif /* not lint */ 43 #endif 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 /* 48 * Fix up / report on disk quotas & usage 49 */ 50 #include <sys/param.h> 51 #include <sys/disklabel.h> 52 #include <sys/mount.h> 53 #include <sys/stat.h> 54 55 #include <ufs/ufs/dinode.h> 56 #include <ufs/ufs/quota.h> 57 #include <ufs/ffs/fs.h> 58 59 #include <err.h> 60 #include <errno.h> 61 #include <fcntl.h> 62 #include <fstab.h> 63 #include <grp.h> 64 #include <pwd.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <unistd.h> 69 70 char *qfname = QUOTAFILENAME; 71 char *qfextension[] = INITQFNAMES; 72 char *quotagroup = QUOTAGROUP; 73 74 union { 75 struct fs sblk; 76 char dummy[MAXBSIZE]; 77 } sb_un; 78 #define sblock sb_un.sblk 79 union { 80 struct cg cgblk; 81 char dummy[MAXBSIZE]; 82 } cg_un; 83 #define cgblk cg_un.cgblk 84 long dev_bsize = 1; 85 ino_t maxino; 86 87 union dinode { 88 struct ufs1_dinode dp1; 89 struct ufs2_dinode dp2; 90 }; 91 #define DIP(dp, field) \ 92 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 93 (dp)->dp1.field : (dp)->dp2.field) 94 95 struct quotaname { 96 long flags; 97 char grpqfname[PATH_MAX]; 98 char usrqfname[PATH_MAX]; 99 }; 100 #define HASUSR 1 101 #define HASGRP 2 102 103 struct fileusage { 104 struct fileusage *fu_next; 105 u_long fu_curinodes; 106 u_long fu_curblocks; 107 u_long fu_id; 108 char fu_name[1]; 109 /* actually bigger */ 110 }; 111 #define FUHASH 1024 /* must be power of two */ 112 struct fileusage *fuhead[MAXQUOTAS][FUHASH]; 113 114 int aflag; /* all file systems */ 115 int gflag; /* check group quotas */ 116 int uflag; /* check user quotas */ 117 int vflag; /* verbose */ 118 int fi; /* open disk file descriptor */ 119 120 struct fileusage * 121 addid(u_long, int, char *, char *); 122 char *blockcheck(char *); 123 void bread(ufs2_daddr_t, char *, long); 124 extern int checkfstab(int, int, void * (*)(struct fstab *), 125 int (*)(char *, char *, struct quotaname *)); 126 int chkquota(char *, char *, struct quotaname *); 127 void freeinodebuf(void); 128 union dinode * 129 getnextinode(ino_t); 130 int getquotagid(void); 131 int hasquota(struct fstab *, int, char **); 132 struct fileusage * 133 lookup(u_long, int); 134 void *needchk(struct fstab *); 135 int oneof(char *, char*[], int); 136 void printchanges(char *, int, struct dqblk *, struct fileusage *, u_long); 137 void setinodebuf(ino_t); 138 int update(char *, char *, int); 139 void usage(void); 140 141 int 142 main(argc, argv) 143 int argc; 144 char *argv[]; 145 { 146 struct fstab *fs; 147 struct passwd *pw; 148 struct group *gr; 149 struct quotaname *auxdata; 150 int i, argnum, maxrun, errs, ch; 151 long done = 0; 152 char *name; 153 154 errs = maxrun = 0; 155 while ((ch = getopt(argc, argv, "aguvl:")) != -1) { 156 switch(ch) { 157 case 'a': 158 aflag++; 159 break; 160 case 'g': 161 gflag++; 162 break; 163 case 'u': 164 uflag++; 165 break; 166 case 'v': 167 vflag++; 168 break; 169 case 'l': 170 maxrun = atoi(optarg); 171 break; 172 default: 173 usage(); 174 } 175 } 176 argc -= optind; 177 argv += optind; 178 if ((argc == 0 && !aflag) || (argc > 0 && aflag)) 179 usage(); 180 if (!gflag && !uflag) { 181 gflag++; 182 uflag++; 183 } 184 if (gflag) { 185 setgrent(); 186 while ((gr = getgrent()) != NULL) 187 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name, 188 NULL); 189 endgrent(); 190 } 191 if (uflag) { 192 setpwent(); 193 while ((pw = getpwent()) != NULL) 194 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name, 195 NULL); 196 endpwent(); 197 } 198 /* 199 * Setting maxrun (-l) makes no sense without the -a flag. 200 * Historically this was never an error, so we just warn. 201 */ 202 if (maxrun > 0 && !aflag) 203 warnx("ignoring -l without -a"); 204 if (aflag) 205 exit(checkfstab(1, maxrun, needchk, chkquota)); 206 if (setfsent() == 0) 207 errx(1, "%s: can't open", FSTAB); 208 while ((fs = getfsent()) != NULL) { 209 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 210 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) && 211 (auxdata = needchk(fs)) && 212 (name = blockcheck(fs->fs_spec))) { 213 done |= 1 << argnum; 214 errs += chkquota(name, fs->fs_file, auxdata); 215 } 216 } 217 endfsent(); 218 for (i = 0; i < argc; i++) 219 if ((done & (1 << i)) == 0) 220 fprintf(stderr, "%s not found in %s\n", 221 argv[i], FSTAB); 222 exit(errs); 223 } 224 225 void 226 usage() 227 { 228 (void)fprintf(stderr, "%s\n%s\n", 229 "usage: quotacheck [-guv] [-l maxrun] -a", 230 " quotacheck [-guv] filesystem ..."); 231 exit(1); 232 } 233 234 void * 235 needchk(fs) 236 struct fstab *fs; 237 { 238 struct quotaname *qnp; 239 char *qfnp; 240 241 if (strcmp(fs->fs_vfstype, "ufs") || 242 strcmp(fs->fs_type, FSTAB_RW)) 243 return (NULL); 244 if ((qnp = malloc(sizeof(*qnp))) == NULL) 245 errx(1, "malloc failed"); 246 qnp->flags = 0; 247 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) { 248 strcpy(qnp->grpqfname, qfnp); 249 qnp->flags |= HASGRP; 250 } 251 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) { 252 strcpy(qnp->usrqfname, qfnp); 253 qnp->flags |= HASUSR; 254 } 255 if (qnp->flags) 256 return (qnp); 257 free(qnp); 258 return (NULL); 259 } 260 261 /* 262 * Possible superblock locations ordered from most to least likely. 263 */ 264 static int sblock_try[] = SBLOCKSEARCH; 265 266 /* 267 * Scan the specified file system to check quota(s) present on it. 268 */ 269 int 270 chkquota(fsname, mntpt, qnp) 271 char *fsname, *mntpt; 272 struct quotaname *qnp; 273 { 274 struct fileusage *fup; 275 union dinode *dp; 276 int cg, i, mode, errs = 0; 277 ino_t ino, inosused, userino = 0, groupino = 0; 278 dev_t dev, userdev = 0, groupdev = 0; 279 char *cp; 280 struct stat sb; 281 282 if ((fi = open(fsname, O_RDONLY, 0)) < 0) { 283 warn("%s", fsname); 284 return (1); 285 } 286 if ((stat(mntpt, &sb)) < 0) { 287 warn("%s", mntpt); 288 return (1); 289 } 290 dev = sb.st_dev; 291 if (vflag) { 292 (void)printf("*** Checking "); 293 if (qnp->flags & HASUSR) 294 (void)printf("%s%s", qfextension[USRQUOTA], 295 (qnp->flags & HASGRP) ? " and " : ""); 296 if (qnp->flags & HASGRP) 297 (void)printf("%s", qfextension[GRPQUOTA]); 298 (void)printf(" quotas for %s (%s)\n", fsname, mntpt); 299 } 300 if (qnp->flags & HASUSR) { 301 if (stat(qnp->usrqfname, &sb) == 0) { 302 userino = sb.st_ino; 303 userdev = sb.st_dev; 304 } 305 } 306 if (qnp->flags & HASGRP) { 307 if (stat(qnp->grpqfname, &sb) == 0) { 308 groupino = sb.st_ino; 309 groupdev = sb.st_dev; 310 } 311 } 312 sync(); 313 dev_bsize = 1; 314 for (i = 0; sblock_try[i] != -1; i++) { 315 bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE); 316 if ((sblock.fs_magic == FS_UFS1_MAGIC || 317 (sblock.fs_magic == FS_UFS2_MAGIC && 318 sblock.fs_sblockloc == sblock_try[i])) && 319 sblock.fs_bsize <= MAXBSIZE && 320 sblock.fs_bsize >= sizeof(struct fs)) 321 break; 322 } 323 if (sblock_try[i] == -1) { 324 warn("Cannot find file system superblock"); 325 return (1); 326 } 327 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 328 maxino = sblock.fs_ncg * sblock.fs_ipg; 329 for (cg = 0; cg < sblock.fs_ncg; cg++) { 330 ino = cg * sblock.fs_ipg; 331 setinodebuf(ino); 332 bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk), 333 sblock.fs_cgsize); 334 if (sblock.fs_magic == FS_UFS2_MAGIC) 335 inosused = cgblk.cg_initediblk; 336 else 337 inosused = sblock.fs_ipg; 338 /* 339 * If we are using soft updates, then we can trust the 340 * cylinder group inode allocation maps to tell us which 341 * inodes are allocated. We will scan the used inode map 342 * to find the inodes that are really in use, and then 343 * read only those inodes in from disk. 344 */ 345 if (sblock.fs_flags & FS_DOSOFTDEP) { 346 if (!cg_chkmagic(&cgblk)) 347 errx(1, "CG %d: BAD MAGIC NUMBER\n", cg); 348 cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT]; 349 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) { 350 if (*cp == 0) 351 continue; 352 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) { 353 if (*cp & i) 354 break; 355 inosused--; 356 } 357 break; 358 } 359 if (inosused <= 0) 360 continue; 361 } 362 for (i = 0; i < inosused; i++, ino++) { 363 if ((dp = getnextinode(ino)) == NULL || ino < ROOTINO || 364 (mode = DIP(dp, di_mode) & IFMT) == 0) 365 continue; 366 /* 367 * XXX: Do not account for UIDs or GIDs that appear 368 * to be negative to prevent generating 100GB+ 369 * quota files. 370 */ 371 if ((int)DIP(dp, di_uid) < 0 || 372 (int)DIP(dp, di_gid) < 0) { 373 if (vflag) { 374 if (aflag) 375 (void)printf("%s: ", mntpt); 376 (void)printf("out of range UID/GID (%u/%u) ino=%u\n", 377 DIP(dp, di_uid), DIP(dp,di_gid), 378 ino); 379 } 380 continue; 381 } 382 383 /* 384 * Do not account for file system snapshot files 385 * or the actual quota data files to be consistent 386 * with how they are handled inside the kernel. 387 */ 388 #ifdef SF_SNAPSHOT 389 if (DIP(dp, di_flags) & SF_SNAPSHOT) 390 continue; 391 #endif 392 if ((ino == userino && dev == userdev) || 393 (ino == groupino && dev == groupdev)) 394 continue; 395 if (qnp->flags & HASGRP) { 396 fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA, 397 (char *)0, mntpt); 398 fup->fu_curinodes++; 399 if (mode == IFREG || mode == IFDIR || 400 mode == IFLNK) 401 fup->fu_curblocks += DIP(dp, di_blocks); 402 } 403 if (qnp->flags & HASUSR) { 404 fup = addid((u_long)DIP(dp, di_uid), USRQUOTA, 405 (char *)0, mntpt); 406 fup->fu_curinodes++; 407 if (mode == IFREG || mode == IFDIR || 408 mode == IFLNK) 409 fup->fu_curblocks += DIP(dp, di_blocks); 410 } 411 } 412 } 413 freeinodebuf(); 414 if (qnp->flags & HASUSR) 415 errs += update(mntpt, qnp->usrqfname, USRQUOTA); 416 if (qnp->flags & HASGRP) 417 errs += update(mntpt, qnp->grpqfname, GRPQUOTA); 418 close(fi); 419 (void)fflush(stdout); 420 return (errs); 421 } 422 423 /* 424 * Update a specified quota file. 425 */ 426 int 427 update(fsname, quotafile, type) 428 char *fsname, *quotafile; 429 int type; 430 { 431 struct fileusage *fup; 432 FILE *qfi, *qfo; 433 u_long id, lastid, highid = 0; 434 off_t offset; 435 int i; 436 struct dqblk dqbuf; 437 struct stat sb; 438 static int warned = 0; 439 static struct dqblk zerodqbuf; 440 static struct fileusage zerofileusage; 441 442 if ((qfo = fopen(quotafile, "r+")) == NULL) { 443 if (errno == ENOENT) 444 qfo = fopen(quotafile, "w+"); 445 if (qfo) { 446 warnx("creating quota file %s", quotafile); 447 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP) 448 (void) fchown(fileno(qfo), getuid(), getquotagid()); 449 (void) fchmod(fileno(qfo), MODE); 450 } else { 451 warn("%s", quotafile); 452 return (1); 453 } 454 } 455 if ((qfi = fopen(quotafile, "r")) == NULL) { 456 warn("%s", quotafile); 457 (void) fclose(qfo); 458 return (1); 459 } 460 if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 && 461 errno == EOPNOTSUPP && !warned && vflag) { 462 warned++; 463 (void)printf("*** Warning: %s\n", 464 "Quotas are not compiled into this kernel"); 465 } 466 if (fstat(fileno(qfi), &sb) < 0) { 467 warn("Cannot fstat quota file %s\n", quotafile); 468 (void) fclose(qfo); 469 (void) fclose(qfi); 470 return (1); 471 } 472 if ((sb.st_size % sizeof(struct dqblk)) != 0) 473 warn("%s size is not a multiple of dqblk\n", quotafile); 474 475 /* 476 * Scan the on-disk quota file and record any usage changes. 477 */ 478 479 if (sb.st_size != 0) 480 lastid = (sb.st_size / sizeof(struct dqblk)) - 1; 481 else 482 lastid = 0; 483 for (id = 0, offset = 0; id <= lastid; 484 id++, offset += sizeof(struct dqblk)) { 485 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0) 486 dqbuf = zerodqbuf; 487 if ((fup = lookup(id, type)) == NULL) 488 fup = &zerofileusage; 489 if (fup->fu_curinodes || fup->fu_curblocks || 490 dqbuf.dqb_bsoftlimit || dqbuf.dqb_bhardlimit || 491 dqbuf.dqb_isoftlimit || dqbuf.dqb_ihardlimit) 492 highid = id; 493 if (dqbuf.dqb_curinodes == fup->fu_curinodes && 494 dqbuf.dqb_curblocks == fup->fu_curblocks) { 495 fup->fu_curinodes = 0; 496 fup->fu_curblocks = 0; 497 continue; 498 } 499 printchanges(fsname, type, &dqbuf, fup, id); 500 /* 501 * Reset time limit if have a soft limit and were 502 * previously under it, but are now over it. 503 */ 504 if (dqbuf.dqb_bsoftlimit && id != 0 && 505 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && 506 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit) 507 dqbuf.dqb_btime = 0; 508 if (dqbuf.dqb_isoftlimit && id != 0 && 509 dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit && 510 fup->fu_curinodes >= dqbuf.dqb_isoftlimit) 511 dqbuf.dqb_itime = 0; 512 dqbuf.dqb_curinodes = fup->fu_curinodes; 513 dqbuf.dqb_curblocks = fup->fu_curblocks; 514 if (fseeko(qfo, offset, SEEK_SET) < 0) { 515 warn("%s: seek failed", quotafile); 516 return(1); 517 } 518 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo); 519 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id, 520 (caddr_t)&dqbuf); 521 fup->fu_curinodes = 0; 522 fup->fu_curblocks = 0; 523 } 524 525 /* 526 * Walk the hash table looking for ids with non-zero usage 527 * that are not currently recorded in the quota file. E.g. 528 * ids that are past the end of the current file. 529 */ 530 531 for (i = 0; i < FUHASH; i++) { 532 for (fup = fuhead[type][i]; fup != NULL; fup = fup->fu_next) { 533 if (fup->fu_id <= lastid) 534 continue; 535 if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0) 536 continue; 537 bzero(&dqbuf, sizeof(struct dqblk)); 538 if (fup->fu_id > highid) 539 highid = fup->fu_id; 540 printchanges(fsname, type, &dqbuf, fup, id); 541 dqbuf.dqb_curinodes = fup->fu_curinodes; 542 dqbuf.dqb_curblocks = fup->fu_curblocks; 543 offset = (off_t)fup->fu_id * sizeof(struct dqblk); 544 if (fseeko(qfo, offset, SEEK_SET) < 0) { 545 warn("%s: seek failed", quotafile); 546 return(1); 547 } 548 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo); 549 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id, 550 (caddr_t)&dqbuf); 551 fup->fu_curinodes = 0; 552 fup->fu_curblocks = 0; 553 } 554 } 555 fclose(qfi); 556 fflush(qfo); 557 ftruncate(fileno(qfo), 558 (((off_t)highid + 1) * sizeof(struct dqblk))); 559 fclose(qfo); 560 return (0); 561 } 562 563 /* 564 * Check to see if target appears in list of size cnt. 565 */ 566 int 567 oneof(target, list, cnt) 568 char *target, *list[]; 569 int cnt; 570 { 571 int i; 572 573 for (i = 0; i < cnt; i++) 574 if (strcmp(target, list[i]) == 0) 575 return (i); 576 return (-1); 577 } 578 579 /* 580 * Determine the group identifier for quota files. 581 */ 582 int 583 getquotagid() 584 { 585 struct group *gr; 586 587 if ((gr = getgrnam(quotagroup)) != NULL) 588 return (gr->gr_gid); 589 return (-1); 590 } 591 592 /* 593 * Check to see if a particular quota is to be enabled. 594 */ 595 int 596 hasquota(fs, type, qfnamep) 597 struct fstab *fs; 598 int type; 599 char **qfnamep; 600 { 601 char *opt; 602 char *cp; 603 struct statfs sfb; 604 static char initname, usrname[100], grpname[100]; 605 static char buf[BUFSIZ]; 606 607 if (!initname) { 608 (void)snprintf(usrname, sizeof(usrname), "%s%s", 609 qfextension[USRQUOTA], qfname); 610 (void)snprintf(grpname, sizeof(grpname), "%s%s", 611 qfextension[GRPQUOTA], qfname); 612 initname = 1; 613 } 614 strcpy(buf, fs->fs_mntops); 615 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 616 if ((cp = index(opt, '=')) != NULL) 617 *cp++ = '\0'; 618 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 619 break; 620 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 621 break; 622 } 623 if (!opt) 624 return (0); 625 if (cp) 626 *qfnamep = cp; 627 else { 628 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file, 629 qfname, qfextension[type]); 630 *qfnamep = buf; 631 } 632 if (statfs(fs->fs_file, &sfb) != 0) { 633 warn("cannot statfs mount point %s", fs->fs_file); 634 return (0); 635 } 636 if (strcmp(fs->fs_file, sfb.f_mntonname)) { 637 warnx("%s not mounted for %s quotas", fs->fs_file, 638 type == USRQUOTA ? "user" : "group"); 639 return (0); 640 } 641 return (1); 642 } 643 644 /* 645 * Routines to manage the file usage table. 646 * 647 * Lookup an id of a specific type. 648 */ 649 struct fileusage * 650 lookup(id, type) 651 u_long id; 652 int type; 653 { 654 struct fileusage *fup; 655 656 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next) 657 if (fup->fu_id == id) 658 return (fup); 659 return (NULL); 660 } 661 662 /* 663 * Add a new file usage id if it does not already exist. 664 */ 665 struct fileusage * 666 addid(id, type, name, fsname) 667 u_long id; 668 int type; 669 char *name; 670 char *fsname; 671 { 672 struct fileusage *fup, **fhp; 673 int len; 674 675 if ((fup = lookup(id, type)) != NULL) 676 return (fup); 677 if (name) 678 len = strlen(name); 679 else 680 len = 0; 681 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL) 682 errx(1, "calloc failed"); 683 fhp = &fuhead[type][id & (FUHASH - 1)]; 684 fup->fu_next = *fhp; 685 *fhp = fup; 686 fup->fu_id = id; 687 if (name) 688 bcopy(name, fup->fu_name, len + 1); 689 else { 690 (void)sprintf(fup->fu_name, "%lu", id); 691 if (vflag) { 692 if (aflag && fsname != NULL) 693 (void)printf("%s: ", fsname); 694 printf("unknown %cid: %lu\n", 695 type == USRQUOTA ? 'u' : 'g', id); 696 } 697 } 698 return (fup); 699 } 700 701 /* 702 * Special purpose version of ginode used to optimize pass 703 * over all the inodes in numerical order. 704 */ 705 static ino_t nextino, lastinum, lastvalidinum; 706 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 707 static caddr_t inodebuf; 708 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */ 709 710 union dinode * 711 getnextinode(ino_t inumber) 712 { 713 long size; 714 ufs2_daddr_t dblk; 715 union dinode *dp; 716 static caddr_t nextinop; 717 718 if (inumber != nextino++ || inumber > lastvalidinum) 719 errx(1, "bad inode number %d to nextinode", inumber); 720 if (inumber >= lastinum) { 721 readcnt++; 722 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum)); 723 if (readcnt % readpercg == 0) { 724 size = partialsize; 725 lastinum += partialcnt; 726 } else { 727 size = inobufsize; 728 lastinum += fullcnt; 729 } 730 /* 731 * If bread returns an error, it will already have zeroed 732 * out the buffer, so we do not need to do so here. 733 */ 734 bread(dblk, inodebuf, size); 735 nextinop = inodebuf; 736 } 737 dp = (union dinode *)nextinop; 738 if (sblock.fs_magic == FS_UFS1_MAGIC) 739 nextinop += sizeof(struct ufs1_dinode); 740 else 741 nextinop += sizeof(struct ufs2_dinode); 742 return (dp); 743 } 744 745 /* 746 * Prepare to scan a set of inodes. 747 */ 748 void 749 setinodebuf(ino_t inum) 750 { 751 752 if (inum % sblock.fs_ipg != 0) 753 errx(1, "bad inode number %d to setinodebuf", inum); 754 lastvalidinum = inum + sblock.fs_ipg - 1; 755 nextino = inum; 756 lastinum = inum; 757 readcnt = 0; 758 if (inodebuf != NULL) 759 return; 760 inobufsize = blkroundup(&sblock, INOBUFSIZE); 761 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ? 762 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 763 readpercg = sblock.fs_ipg / fullcnt; 764 partialcnt = sblock.fs_ipg % fullcnt; 765 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ? 766 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 767 if (partialcnt != 0) { 768 readpercg++; 769 } else { 770 partialcnt = fullcnt; 771 partialsize = inobufsize; 772 } 773 if ((inodebuf = malloc((unsigned)inobufsize)) == NULL) 774 errx(1, "cannot allocate space for inode buffer"); 775 } 776 777 /* 778 * Free up data structures used to scan inodes. 779 */ 780 void 781 freeinodebuf() 782 { 783 784 if (inodebuf != NULL) 785 free(inodebuf); 786 inodebuf = NULL; 787 } 788 789 /* 790 * Read specified disk blocks. 791 */ 792 void 793 bread(bno, buf, cnt) 794 ufs2_daddr_t bno; 795 char *buf; 796 long cnt; 797 { 798 799 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 || 800 read(fi, buf, cnt) != cnt) 801 errx(1, "bread failed on block %ld", (long)bno); 802 } 803 804 /* 805 * Display updated block and i-node counts. 806 */ 807 void 808 printchanges(fsname, type, dp, fup, id) 809 char *fsname; 810 int type; 811 struct dqblk *dp; 812 struct fileusage *fup; 813 u_long id; 814 { 815 if (!vflag) 816 return; 817 if (aflag) 818 (void)printf("%s: ", fsname); 819 if (fup->fu_name[0] == '\0') 820 (void)printf("%-8lu fixed ", id); 821 else 822 (void)printf("%-8s fixed ", fup->fu_name); 823 switch (type) { 824 825 case GRPQUOTA: 826 (void)printf("(group):"); 827 break; 828 829 case USRQUOTA: 830 (void)printf("(user): "); 831 break; 832 833 default: 834 (void)printf("(unknown quota type %d)", type); 835 break; 836 } 837 if (dp->dqb_curinodes != fup->fu_curinodes) 838 (void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes, 839 (u_long)fup->fu_curinodes); 840 if (dp->dqb_curblocks != fup->fu_curblocks) 841 (void)printf("\tblocks %lu -> %lu", 842 (u_long)dp->dqb_curblocks, 843 (u_long)fup->fu_curblocks); 844 (void)printf("\n"); 845 } 846