2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)quotacheck.c 8.3 (Berkeley) 1/29/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * Fix up / report on disk quotas & usage
50 #include <sys/param.h>
51 #include <sys/disklabel.h>
54 #include <ufs/ufs/dinode.h>
55 #include <ufs/ufs/quota.h>
56 #include <ufs/ffs/fs.h>
69 char *qfname = QUOTAFILENAME;
70 char *qfextension[] = INITQFNAMES;
71 char *quotagroup = QUOTAGROUP;
77 #define sblock sb_un.sblk
82 #define cgblk cg_un.cgblk
87 struct ufs1_dinode dp1;
88 struct ufs2_dinode dp2;
90 #define DIP(dp, field) \
91 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
92 (dp)->dp1.field : (dp)->dp2.field)
96 char grpqfname[PATH_MAX];
97 char usrqfname[PATH_MAX];
103 struct fileusage *fu_next;
108 /* actually bigger */
110 #define FUHASH 1024 /* must be power of two */
111 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
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 */
120 addid(u_long, int, char *, char *);
121 char *blockcheck(char *);
122 void bread(ufs2_daddr_t, char *, long);
123 extern int checkfstab(int, int, void * (*)(struct fstab *),
124 int (*)(char *, char *, struct quotaname *));
125 int chkquota(char *, char *, struct quotaname *);
126 void freeinodebuf(void);
129 int getquotagid(void);
130 int hasquota(struct fstab *, int, char **);
133 void *needchk(struct fstab *);
134 int oneof(char *, char*[], int);
135 void printchanges(char *, int, struct dqblk *, struct fileusage *, u_long);
136 void setinodebuf(ino_t);
137 int update(char *, char *, int);
148 struct quotaname *auxdata;
149 int i, argnum, maxrun, errs, ch;
154 while ((ch = getopt(argc, argv, "aguvl:")) != -1) {
169 maxrun = atoi(optarg);
177 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
179 if (!gflag && !uflag) {
185 while ((gr = getgrent()) != NULL)
186 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name,
192 while ((pw = getpwent()) != NULL)
193 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name,
198 * Setting maxrun (-l) makes no sense without the -a flag.
199 * Historically this was never an error, so we just warn.
201 if (maxrun > 0 && !aflag)
202 warnx("ignoring -l without -a");
204 exit(checkfstab(1, maxrun, needchk, chkquota));
206 errx(1, "%s: can't open", FSTAB);
207 while ((fs = getfsent()) != NULL) {
208 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
209 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
210 (auxdata = needchk(fs)) &&
211 (name = blockcheck(fs->fs_spec))) {
213 errs += chkquota(name, fs->fs_file, auxdata);
217 for (i = 0; i < argc; i++)
218 if ((done & (1 << i)) == 0)
219 fprintf(stderr, "%s not found in %s\n",
227 (void)fprintf(stderr, "%s\n%s\n",
228 "usage: quotacheck [-guv] -a",
229 " quotacheck [-guv] filesystem ...");
237 struct quotaname *qnp;
240 if (strcmp(fs->fs_vfstype, "ufs") ||
241 strcmp(fs->fs_type, FSTAB_RW))
243 if ((qnp = malloc(sizeof(*qnp))) == NULL)
244 errx(1, "malloc failed");
246 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
247 strcpy(qnp->grpqfname, qfnp);
248 qnp->flags |= HASGRP;
250 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
251 strcpy(qnp->usrqfname, qfnp);
252 qnp->flags |= HASUSR;
261 * Possible superblock locations ordered from most to least likely.
263 static int sblock_try[] = SBLOCKSEARCH;
266 * Scan the specified file system to check quota(s) present on it.
269 chkquota(fsname, mntpt, qnp)
270 char *fsname, *mntpt;
271 struct quotaname *qnp;
273 struct fileusage *fup;
275 int cg, i, mode, errs = 0;
276 ino_t ino, inosused, userino = 0, groupino = 0;
280 if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
285 (void)printf("*** Checking ");
286 if (qnp->flags & HASUSR)
287 (void)printf("%s%s", qfextension[USRQUOTA],
288 (qnp->flags & HASGRP) ? " and " : "");
289 if (qnp->flags & HASGRP)
290 (void)printf("%s", qfextension[GRPQUOTA]);
291 (void)printf(" quotas for %s (%s)\n", fsname, mntpt);
293 if (qnp->flags & HASUSR) {
294 if (stat(qnp->usrqfname, &sb) == 0)
297 if (qnp->flags & HASGRP) {
298 if (stat(qnp->grpqfname, &sb) == 0)
299 groupino = sb.st_ino;
303 for (i = 0; sblock_try[i] != -1; i++) {
304 bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE);
305 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
306 (sblock.fs_magic == FS_UFS2_MAGIC &&
307 sblock.fs_sblockloc == sblock_try[i])) &&
308 sblock.fs_bsize <= MAXBSIZE &&
309 sblock.fs_bsize >= sizeof(struct fs))
312 if (sblock_try[i] == -1) {
313 warn("Cannot find file system superblock");
316 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
317 maxino = sblock.fs_ncg * sblock.fs_ipg;
318 for (cg = 0; cg < sblock.fs_ncg; cg++) {
319 ino = cg * sblock.fs_ipg;
321 bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk),
323 if (sblock.fs_magic == FS_UFS2_MAGIC)
324 inosused = cgblk.cg_initediblk;
326 inosused = sblock.fs_ipg;
328 * If we are using soft updates, then we can trust the
329 * cylinder group inode allocation maps to tell us which
330 * inodes are allocated. We will scan the used inode map
331 * to find the inodes that are really in use, and then
332 * read only those inodes in from disk.
334 if (sblock.fs_flags & FS_DOSOFTDEP) {
335 if (!cg_chkmagic(&cgblk))
336 errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
337 cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
338 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
341 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
351 for (i = 0; i < inosused; i++, ino++) {
352 if ((dp = getnextinode(ino)) == NULL || ino < ROOTINO ||
353 (mode = DIP(dp, di_mode) & IFMT) == 0)
356 * XXX: Do not account for UIDs or GIDs that appear
357 * to be negative to prevent generating 100GB+
360 if ((int)DIP(dp, di_uid) < 0 ||
361 (int)DIP(dp, di_gid) < 0) {
364 (void)printf("%s: ", mntpt);
365 (void)printf("out of range UID/GID (%u/%u) ino=%u\n",
366 DIP(dp, di_uid), DIP(dp,di_gid),
373 * Do not account for file system snapshot files
374 * or the actual quota data files to be consistent
375 * with how they are handled inside the kernel.
378 if (DIP(dp, di_flags) & SF_SNAPSHOT)
381 if (ino == userino || ino == groupino)
383 if (qnp->flags & HASGRP) {
384 fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA,
387 if (mode == IFREG || mode == IFDIR ||
389 fup->fu_curblocks += DIP(dp, di_blocks);
391 if (qnp->flags & HASUSR) {
392 fup = addid((u_long)DIP(dp, di_uid), USRQUOTA,
395 if (mode == IFREG || mode == IFDIR ||
397 fup->fu_curblocks += DIP(dp, di_blocks);
402 if (qnp->flags & HASUSR)
403 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
404 if (qnp->flags & HASGRP)
405 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
411 * Update a specified quota file.
414 update(fsname, quotafile, type)
415 char *fsname, *quotafile;
418 struct fileusage *fup;
420 u_long id, lastid, highid = 0;
425 static int warned = 0;
426 static struct dqblk zerodqbuf;
427 static struct fileusage zerofileusage;
429 if ((qfo = fopen(quotafile, "r+")) == NULL) {
431 qfo = fopen(quotafile, "w+");
433 warnx("creating quota file %s", quotafile);
434 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
435 (void) fchown(fileno(qfo), getuid(), getquotagid());
436 (void) fchmod(fileno(qfo), MODE);
438 warn("%s", quotafile);
442 if ((qfi = fopen(quotafile, "r")) == NULL) {
443 warn("%s", quotafile);
447 if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
448 errno == EOPNOTSUPP && !warned && vflag) {
450 (void)printf("*** Warning: %s\n",
451 "Quotas are not compiled into this kernel");
453 if (fstat(fileno(qfi), &sb) < 0) {
454 warn("Cannot fstat quota file %s\n", quotafile);
459 if ((sb.st_size % sizeof(struct dqblk)) != 0)
460 warn("%s size is not a multiple of dqblk\n", quotafile);
463 * Scan the on-disk quota file and record any usage changes.
467 lastid = (sb.st_size / sizeof(struct dqblk)) - 1;
470 for (id = 0, offset = 0; id <= lastid;
471 id++, offset += sizeof(struct dqblk)) {
472 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
474 if ((fup = lookup(id, type)) == NULL)
475 fup = &zerofileusage;
476 if (fup->fu_curinodes || fup->fu_curblocks ||
477 dqbuf.dqb_bsoftlimit || dqbuf.dqb_bhardlimit ||
478 dqbuf.dqb_isoftlimit || dqbuf.dqb_ihardlimit)
480 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
481 dqbuf.dqb_curblocks == fup->fu_curblocks) {
482 fup->fu_curinodes = 0;
483 fup->fu_curblocks = 0;
486 printchanges(fsname, type, &dqbuf, fup, id);
488 * Reset time limit if have a soft limit and were
489 * previously under it, but are now over it.
491 if (dqbuf.dqb_bsoftlimit &&
492 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
493 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
495 if (dqbuf.dqb_isoftlimit &&
496 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
497 fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
499 dqbuf.dqb_curinodes = fup->fu_curinodes;
500 dqbuf.dqb_curblocks = fup->fu_curblocks;
501 if (fseeko(qfo, offset, SEEK_SET) < 0) {
502 warn("%s: seek failed", quotafile);
505 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
506 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
508 fup->fu_curinodes = 0;
509 fup->fu_curblocks = 0;
513 * Walk the hash table looking for ids with non-zero usage
514 * that are not currently recorded in the quota file. E.g.
515 * ids that are past the end of the current file.
518 for (i = 0; i < FUHASH; i++) {
519 for (fup = fuhead[type][i]; fup != NULL; fup = fup->fu_next) {
520 if (fup->fu_id <= lastid)
522 if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0)
524 bzero(&dqbuf, sizeof(struct dqblk));
525 if (fup->fu_id > highid)
527 printchanges(fsname, type, &dqbuf, fup, id);
528 dqbuf.dqb_curinodes = fup->fu_curinodes;
529 dqbuf.dqb_curblocks = fup->fu_curblocks;
530 offset = (off_t)fup->fu_id * sizeof(struct dqblk);
531 if (fseeko(qfo, offset, SEEK_SET) < 0) {
532 warn("%s: seek failed", quotafile);
535 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
536 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
538 fup->fu_curinodes = 0;
539 fup->fu_curblocks = 0;
544 ftruncate(fileno(qfo),
545 (((off_t)highid + 1) * sizeof(struct dqblk)));
551 * Check to see if target appears in list of size cnt.
554 oneof(target, list, cnt)
555 char *target, *list[];
560 for (i = 0; i < cnt; i++)
561 if (strcmp(target, list[i]) == 0)
567 * Determine the group identifier for quota files.
574 if ((gr = getgrnam(quotagroup)) != NULL)
580 * Check to see if a particular quota is to be enabled.
583 hasquota(fs, type, qfnamep)
590 static char initname, usrname[100], grpname[100];
591 static char buf[BUFSIZ];
594 (void)snprintf(usrname, sizeof(usrname),
595 "%s%s", qfextension[USRQUOTA], qfname);
596 (void)snprintf(grpname, sizeof(grpname),
597 "%s%s", qfextension[GRPQUOTA], qfname);
600 strcpy(buf, fs->fs_mntops);
601 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
602 if ((cp = index(opt, '=')) != NULL)
604 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
606 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
614 (void)snprintf(buf, sizeof(buf),
615 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
622 * Routines to manage the file usage table.
624 * Lookup an id of a specific type.
631 struct fileusage *fup;
633 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
634 if (fup->fu_id == id)
640 * Add a new file usage id if it does not already exist.
643 addid(id, type, name, fsname)
649 struct fileusage *fup, **fhp;
652 if ((fup = lookup(id, type)) != NULL)
658 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
659 errx(1, "calloc failed");
660 fhp = &fuhead[type][id & (FUHASH - 1)];
665 bcopy(name, fup->fu_name, len + 1);
667 (void)sprintf(fup->fu_name, "%lu", id);
669 if (aflag && fsname != NULL)
670 (void)printf("%s: ", fsname);
671 printf("unknown %cid: %lu\n",
672 type == USRQUOTA ? 'u' : 'g', id);
679 * Special purpose version of ginode used to optimize pass
680 * over all the inodes in numerical order.
682 static ino_t nextino, lastinum, lastvalidinum;
683 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
684 static caddr_t inodebuf;
685 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
688 getnextinode(ino_t inumber)
693 static caddr_t nextinop;
695 if (inumber != nextino++ || inumber > lastvalidinum)
696 errx(1, "bad inode number %d to nextinode", inumber);
697 if (inumber >= lastinum) {
699 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
700 if (readcnt % readpercg == 0) {
702 lastinum += partialcnt;
708 * If bread returns an error, it will already have zeroed
709 * out the buffer, so we do not need to do so here.
711 bread(dblk, inodebuf, size);
714 dp = (union dinode *)nextinop;
715 if (sblock.fs_magic == FS_UFS1_MAGIC)
716 nextinop += sizeof(struct ufs1_dinode);
718 nextinop += sizeof(struct ufs2_dinode);
723 * Prepare to scan a set of inodes.
726 setinodebuf(ino_t inum)
729 if (inum % sblock.fs_ipg != 0)
730 errx(1, "bad inode number %d to setinodebuf", inum);
731 lastvalidinum = inum + sblock.fs_ipg - 1;
735 if (inodebuf != NULL)
737 inobufsize = blkroundup(&sblock, INOBUFSIZE);
738 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
739 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
740 readpercg = sblock.fs_ipg / fullcnt;
741 partialcnt = sblock.fs_ipg % fullcnt;
742 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
743 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
744 if (partialcnt != 0) {
747 partialcnt = fullcnt;
748 partialsize = inobufsize;
750 if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
751 errx(1, "cannot allocate space for inode buffer");
755 * Free up data structures used to scan inodes.
761 if (inodebuf != NULL)
767 * Read specified disk blocks.
776 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
777 read(fi, buf, cnt) != cnt)
778 errx(1, "bread failed on block %ld", (long)bno);
782 * Display updated block and i-node counts.
785 printchanges(fsname, type, dp, fup, id)
789 struct fileusage *fup;
795 (void)printf("%s: ", fsname);
796 if (fup->fu_name[0] == '\0')
797 (void)printf("%-8lu fixed ", id);
799 (void)printf("%-8s fixed ", fup->fu_name);
803 (void)printf("(group):");
807 (void)printf("(user): ");
811 (void)printf("(unknown quota type %d)", type);
814 if (dp->dqb_curinodes != fup->fu_curinodes)
815 (void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes,
816 (u_long)fup->fu_curinodes);
817 if (dp->dqb_curblocks != fup->fu_curblocks)
818 (void)printf("\tblocks %lu -> %lu",
819 (u_long)dp->dqb_curblocks,
820 (u_long)fup->fu_curblocks);