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/stat.h> 53 54 #include <ufs/ufs/dinode.h> 55 #include <ufs/ufs/quota.h> 56 #include <ufs/ffs/fs.h> 57 58 #include <err.h> 59 #include <errno.h> 60 #include <fcntl.h> 61 #include <fstab.h> 62 #include <grp.h> 63 #include <pwd.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 69 char *qfname = QUOTAFILENAME; 70 char *qfextension[] = INITQFNAMES; 71 char *quotagroup = QUOTAGROUP; 72 73 union { 74 struct fs sblk; 75 char dummy[MAXBSIZE]; 76 } sb_un; 77 #define sblock sb_un.sblk 78 union { 79 struct cg cgblk; 80 char dummy[MAXBSIZE]; 81 } cg_un; 82 #define cgblk cg_un.cgblk 83 long dev_bsize = 1; 84 ino_t maxino; 85 86 union dinode { 87 struct ufs1_dinode dp1; 88 struct ufs2_dinode dp2; 89 }; 90 #define DIP(dp, field) \ 91 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \ 92 (dp)->dp1.field : (dp)->dp2.field) 93 94 struct quotaname { 95 long flags; 96 char grpqfname[PATH_MAX]; 97 char usrqfname[PATH_MAX]; 98 }; 99 #define HASUSR 1 100 #define HASGRP 2 101 102 struct fileusage { 103 struct fileusage *fu_next; 104 u_long fu_curinodes; 105 u_long fu_curblocks; 106 u_long fu_id; 107 char fu_name[1]; 108 /* actually bigger */ 109 }; 110 #define FUHASH 1024 /* must be power of two */ 111 struct fileusage *fuhead[MAXQUOTAS][FUHASH]; 112 113 int aflag; /* all file systems */ 114 int gflag; /* check group quotas */ 115 int uflag; /* check user quotas */ 116 int vflag; /* verbose */ 117 int fi; /* open disk file descriptor */ 118 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */ 119 120 struct fileusage * 121 addid(u_long, int, 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 setinodebuf(ino_t); 137 int update(char *, char *, int); 138 void usage(void); 139 140 int 141 main(argc, argv) 142 int argc; 143 char *argv[]; 144 { 145 struct fstab *fs; 146 struct passwd *pw; 147 struct group *gr; 148 struct quotaname *auxdata; 149 int i, argnum, maxrun, errs, ch; 150 long done = 0; 151 char *name; 152 153 errs = maxrun = 0; 154 while ((ch = getopt(argc, argv, "aguvl:")) != -1) { 155 switch(ch) { 156 case 'a': 157 aflag++; 158 break; 159 case 'g': 160 gflag++; 161 break; 162 case 'u': 163 uflag++; 164 break; 165 case 'v': 166 vflag++; 167 break; 168 case 'l': 169 maxrun = atoi(optarg); 170 break; 171 default: 172 usage(); 173 } 174 } 175 argc -= optind; 176 argv += optind; 177 if ((argc == 0 && !aflag) || (argc > 0 && aflag)) 178 usage(); 179 if (!gflag && !uflag) { 180 gflag++; 181 uflag++; 182 } 183 if (gflag) { 184 setgrent(); 185 while ((gr = getgrent()) != NULL) 186 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name); 187 endgrent(); 188 } 189 if (uflag) { 190 setpwent(); 191 while ((pw = getpwent()) != NULL) 192 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name); 193 endpwent(); 194 } 195 if (aflag) 196 exit(checkfstab(1, maxrun, needchk, chkquota)); 197 if (setfsent() == 0) 198 errx(1, "%s: can't open", FSTAB); 199 while ((fs = getfsent()) != NULL) { 200 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 || 201 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) && 202 (auxdata = needchk(fs)) && 203 (name = blockcheck(fs->fs_spec))) { 204 done |= 1 << argnum; 205 errs += chkquota(name, fs->fs_file, auxdata); 206 } 207 } 208 endfsent(); 209 for (i = 0; i < argc; i++) 210 if ((done & (1 << i)) == 0) 211 fprintf(stderr, "%s not found in %s\n", 212 argv[i], FSTAB); 213 exit(errs); 214 } 215 216 void 217 usage() 218 { 219 (void)fprintf(stderr, "%s\n%s\n", 220 "usage: quotacheck [-guv] -a", 221 " quotacheck [-guv] filesystem ..."); 222 exit(1); 223 } 224 225 void * 226 needchk(fs) 227 struct fstab *fs; 228 { 229 struct quotaname *qnp; 230 char *qfnp; 231 232 if (strcmp(fs->fs_vfstype, "ufs") || 233 strcmp(fs->fs_type, FSTAB_RW)) 234 return (NULL); 235 if ((qnp = malloc(sizeof(*qnp))) == NULL) 236 errx(1, "malloc failed"); 237 qnp->flags = 0; 238 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) { 239 strcpy(qnp->grpqfname, qfnp); 240 qnp->flags |= HASGRP; 241 } 242 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) { 243 strcpy(qnp->usrqfname, qfnp); 244 qnp->flags |= HASUSR; 245 } 246 if (qnp->flags) 247 return (qnp); 248 free(qnp); 249 return (NULL); 250 } 251 252 /* 253 * Possible superblock locations ordered from most to least likely. 254 */ 255 static int sblock_try[] = SBLOCKSEARCH; 256 257 /* 258 * Scan the specified file system to check quota(s) present on it. 259 */ 260 int 261 chkquota(fsname, mntpt, qnp) 262 char *fsname, *mntpt; 263 struct quotaname *qnp; 264 { 265 struct fileusage *fup; 266 union dinode *dp; 267 int cg, i, mode, errs = 0; 268 ino_t ino, inosused; 269 char *cp; 270 271 if ((fi = open(fsname, O_RDONLY, 0)) < 0) { 272 warn("%s", fsname); 273 return (1); 274 } 275 if (vflag) { 276 (void)printf("*** Checking "); 277 if (qnp->flags & HASUSR) 278 (void)printf("%s%s", qfextension[USRQUOTA], 279 (qnp->flags & HASGRP) ? " and " : ""); 280 if (qnp->flags & HASGRP) 281 (void)printf("%s", qfextension[GRPQUOTA]); 282 (void)printf(" quotas for %s (%s)\n", fsname, mntpt); 283 } 284 sync(); 285 dev_bsize = 1; 286 for (i = 0; sblock_try[i] != -1; i++) { 287 bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE); 288 if ((sblock.fs_magic == FS_UFS1_MAGIC || 289 (sblock.fs_magic == FS_UFS2_MAGIC && 290 sblock.fs_sblockloc == sblock_try[i])) && 291 sblock.fs_bsize <= MAXBSIZE && 292 sblock.fs_bsize >= sizeof(struct fs)) 293 break; 294 } 295 if (sblock_try[i] == -1) { 296 warn("Cannot find file system superblock"); 297 return (1); 298 } 299 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1); 300 maxino = sblock.fs_ncg * sblock.fs_ipg; 301 for (cg = 0; cg < sblock.fs_ncg; cg++) { 302 ino = cg * sblock.fs_ipg; 303 setinodebuf(ino); 304 bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk), 305 sblock.fs_cgsize); 306 if (sblock.fs_magic == FS_UFS2_MAGIC) 307 inosused = cgblk.cg_initediblk; 308 else 309 inosused = sblock.fs_ipg; 310 /* 311 * If we are using soft updates, then we can trust the 312 * cylinder group inode allocation maps to tell us which 313 * inodes are allocated. We will scan the used inode map 314 * to find the inodes that are really in use, and then 315 * read only those inodes in from disk. 316 */ 317 if (sblock.fs_flags & FS_DOSOFTDEP) { 318 if (!cg_chkmagic(&cgblk)) 319 errx(1, "CG %d: BAD MAGIC NUMBER\n", cg); 320 cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT]; 321 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) { 322 if (*cp == 0) 323 continue; 324 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) { 325 if (*cp & i) 326 break; 327 inosused--; 328 } 329 break; 330 } 331 if (inosused <= 0) 332 continue; 333 } 334 for (i = 0; i < inosused; i++, ino++) { 335 if ((dp = getnextinode(ino)) == NULL || ino < ROOTINO || 336 (mode = DIP(dp, di_mode) & IFMT) == 0) 337 continue; 338 if (qnp->flags & HASGRP) { 339 fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA, 340 (char *)0); 341 fup->fu_curinodes++; 342 if (mode == IFREG || mode == IFDIR || 343 mode == IFLNK) 344 fup->fu_curblocks += DIP(dp, di_blocks); 345 } 346 if (qnp->flags & HASUSR) { 347 fup = addid((u_long)DIP(dp, di_uid), USRQUOTA, 348 (char *)0); 349 fup->fu_curinodes++; 350 if (mode == IFREG || mode == IFDIR || 351 mode == IFLNK) 352 fup->fu_curblocks += DIP(dp, di_blocks); 353 } 354 } 355 } 356 freeinodebuf(); 357 if (qnp->flags & HASUSR) 358 errs += update(mntpt, qnp->usrqfname, USRQUOTA); 359 if (qnp->flags & HASGRP) 360 errs += update(mntpt, qnp->grpqfname, GRPQUOTA); 361 close(fi); 362 return (errs); 363 } 364 365 /* 366 * Update a specified quota file. 367 */ 368 int 369 update(fsname, quotafile, type) 370 char *fsname, *quotafile; 371 int type; 372 { 373 struct fileusage *fup; 374 FILE *qfi, *qfo; 375 u_long id, lastid; 376 off_t offset; 377 struct dqblk dqbuf; 378 static int warned = 0; 379 static struct dqblk zerodqbuf; 380 static struct fileusage zerofileusage; 381 382 if ((qfo = fopen(quotafile, "r+")) == NULL) { 383 if (errno == ENOENT) 384 qfo = fopen(quotafile, "w+"); 385 if (qfo) { 386 warnx("creating quota file %s", quotafile); 387 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP) 388 (void) fchown(fileno(qfo), getuid(), getquotagid()); 389 (void) fchmod(fileno(qfo), MODE); 390 } else { 391 warn("%s", quotafile); 392 return (1); 393 } 394 } 395 if ((qfi = fopen(quotafile, "r")) == NULL) { 396 warn("%s", quotafile); 397 (void) fclose(qfo); 398 return (1); 399 } 400 if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 && 401 errno == EOPNOTSUPP && !warned && vflag) { 402 warned++; 403 (void)printf("*** Warning: %s\n", 404 "Quotas are not compiled into this kernel"); 405 } 406 for (lastid = highid[type], id = 0, offset = 0; id <= lastid; 407 id++, offset += sizeof(struct dqblk)) { 408 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0) 409 dqbuf = zerodqbuf; 410 if ((fup = lookup(id, type)) == 0) 411 fup = &zerofileusage; 412 if (dqbuf.dqb_curinodes == fup->fu_curinodes && 413 dqbuf.dqb_curblocks == fup->fu_curblocks) { 414 fup->fu_curinodes = 0; 415 fup->fu_curblocks = 0; 416 continue; 417 } 418 if (vflag) { 419 if (aflag) 420 printf("%s: ", fsname); 421 printf("%-8s fixed:", fup->fu_name); 422 if (dqbuf.dqb_curinodes != fup->fu_curinodes) 423 (void)printf("\tinodes %lu -> %lu", 424 (u_long)dqbuf.dqb_curinodes, 425 (u_long)fup->fu_curinodes); 426 if (dqbuf.dqb_curblocks != fup->fu_curblocks) 427 (void)printf("\tblocks %lu -> %lu", 428 (u_long)dqbuf.dqb_curblocks, 429 (u_long)fup->fu_curblocks); 430 (void)printf("\n"); 431 } 432 /* 433 * Reset time limit if have a soft limit and were 434 * previously under it, but are now over it. 435 */ 436 if (dqbuf.dqb_bsoftlimit && 437 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && 438 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit) 439 dqbuf.dqb_btime = 0; 440 if (dqbuf.dqb_isoftlimit && 441 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit && 442 fup->fu_curblocks >= dqbuf.dqb_isoftlimit) 443 dqbuf.dqb_itime = 0; 444 dqbuf.dqb_curinodes = fup->fu_curinodes; 445 dqbuf.dqb_curblocks = fup->fu_curblocks; 446 if (fseek(qfo, offset, SEEK_SET) < 0) { 447 warn("%s: seek failed", quotafile); 448 return(1); 449 } 450 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo); 451 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id, 452 (caddr_t)&dqbuf); 453 fup->fu_curinodes = 0; 454 fup->fu_curblocks = 0; 455 } 456 fclose(qfi); 457 fflush(qfo); 458 ftruncate(fileno(qfo), 459 (((off_t)highid[type] + 1) * sizeof(struct dqblk))); 460 fclose(qfo); 461 return (0); 462 } 463 464 /* 465 * Check to see if target appears in list of size cnt. 466 */ 467 int 468 oneof(target, list, cnt) 469 char *target, *list[]; 470 int cnt; 471 { 472 int i; 473 474 for (i = 0; i < cnt; i++) 475 if (strcmp(target, list[i]) == 0) 476 return (i); 477 return (-1); 478 } 479 480 /* 481 * Determine the group identifier for quota files. 482 */ 483 int 484 getquotagid() 485 { 486 struct group *gr; 487 488 if ((gr = getgrnam(quotagroup)) != NULL) 489 return (gr->gr_gid); 490 return (-1); 491 } 492 493 /* 494 * Check to see if a particular quota is to be enabled. 495 */ 496 int 497 hasquota(fs, type, qfnamep) 498 struct fstab *fs; 499 int type; 500 char **qfnamep; 501 { 502 char *opt; 503 char *cp; 504 static char initname, usrname[100], grpname[100]; 505 static char buf[BUFSIZ]; 506 507 if (!initname) { 508 (void)snprintf(usrname, sizeof(usrname), 509 "%s%s", qfextension[USRQUOTA], qfname); 510 (void)snprintf(grpname, sizeof(grpname), 511 "%s%s", qfextension[GRPQUOTA], qfname); 512 initname = 1; 513 } 514 strcpy(buf, fs->fs_mntops); 515 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 516 if ((cp = index(opt, '=')) != NULL) 517 *cp++ = '\0'; 518 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 519 break; 520 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 521 break; 522 } 523 if (!opt) 524 return (0); 525 if (cp) 526 *qfnamep = cp; 527 else { 528 (void)snprintf(buf, sizeof(buf), 529 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]); 530 *qfnamep = buf; 531 } 532 return (1); 533 } 534 535 /* 536 * Routines to manage the file usage table. 537 * 538 * Lookup an id of a specific type. 539 */ 540 struct fileusage * 541 lookup(id, type) 542 u_long id; 543 int type; 544 { 545 struct fileusage *fup; 546 547 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next) 548 if (fup->fu_id == id) 549 return (fup); 550 return (NULL); 551 } 552 553 /* 554 * Add a new file usage id if it does not already exist. 555 */ 556 struct fileusage * 557 addid(id, type, name) 558 u_long id; 559 int type; 560 char *name; 561 { 562 struct fileusage *fup, **fhp; 563 int len; 564 565 if ((fup = lookup(id, type)) != NULL) 566 return (fup); 567 if (name) 568 len = strlen(name); 569 else 570 len = 10; 571 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL) 572 errx(1, "calloc failed"); 573 fhp = &fuhead[type][id & (FUHASH - 1)]; 574 fup->fu_next = *fhp; 575 *fhp = fup; 576 fup->fu_id = id; 577 if (id > highid[type]) 578 highid[type] = id; 579 if (name) 580 bcopy(name, fup->fu_name, len + 1); 581 else { 582 (void)sprintf(fup->fu_name, "%lu", id); 583 if (vflag) 584 printf("unknown %cid: %lu\n", 585 type == USRQUOTA ? 'u' : 'g', id); 586 } 587 return (fup); 588 } 589 590 /* 591 * Special purpose version of ginode used to optimize pass 592 * over all the inodes in numerical order. 593 */ 594 static ino_t nextino, lastinum, lastvalidinum; 595 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize; 596 static caddr_t inodebuf; 597 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */ 598 599 union dinode * 600 getnextinode(ino_t inumber) 601 { 602 long size; 603 ufs2_daddr_t dblk; 604 union dinode *dp; 605 static caddr_t nextinop; 606 607 if (inumber != nextino++ || inumber > lastvalidinum) 608 errx(1, "bad inode number %d to nextinode", inumber); 609 if (inumber >= lastinum) { 610 readcnt++; 611 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum)); 612 if (readcnt % readpercg == 0) { 613 size = partialsize; 614 lastinum += partialcnt; 615 } else { 616 size = inobufsize; 617 lastinum += fullcnt; 618 } 619 /* 620 * If bread returns an error, it will already have zeroed 621 * out the buffer, so we do not need to do so here. 622 */ 623 bread(dblk, inodebuf, size); 624 nextinop = inodebuf; 625 } 626 dp = (union dinode *)nextinop; 627 if (sblock.fs_magic == FS_UFS1_MAGIC) 628 nextinop += sizeof(struct ufs1_dinode); 629 else 630 nextinop += sizeof(struct ufs2_dinode); 631 return (dp); 632 } 633 634 /* 635 * Prepare to scan a set of inodes. 636 */ 637 void 638 setinodebuf(ino_t inum) 639 { 640 641 if (inum % sblock.fs_ipg != 0) 642 errx(1, "bad inode number %d to setinodebuf", inum); 643 lastvalidinum = inum + sblock.fs_ipg - 1; 644 nextino = inum; 645 lastinum = inum; 646 readcnt = 0; 647 if (inodebuf != NULL) 648 return; 649 inobufsize = blkroundup(&sblock, INOBUFSIZE); 650 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ? 651 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 652 readpercg = sblock.fs_ipg / fullcnt; 653 partialcnt = sblock.fs_ipg % fullcnt; 654 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ? 655 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)); 656 if (partialcnt != 0) { 657 readpercg++; 658 } else { 659 partialcnt = fullcnt; 660 partialsize = inobufsize; 661 } 662 if ((inodebuf = malloc((unsigned)inobufsize)) == NULL) 663 errx(1, "cannot allocate space for inode buffer"); 664 } 665 666 /* 667 * Free up data structures used to scan inodes. 668 */ 669 void 670 freeinodebuf() 671 { 672 673 if (inodebuf != NULL) 674 free(inodebuf); 675 inodebuf = NULL; 676 } 677 678 /* 679 * Read specified disk blocks. 680 */ 681 void 682 bread(bno, buf, cnt) 683 ufs2_daddr_t bno; 684 char *buf; 685 long cnt; 686 { 687 688 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 || 689 read(fi, buf, cnt) != cnt) 690 errx(1, "bread failed on block %ld", (long)bno); 691 } 692