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>
52 #include <sys/mount.h>
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ffs/fs.h>
71 #include "quotacheck.h"
73 char *qfname = QUOTAFILENAME;
74 char *qfextension[] = INITQFNAMES;
75 char *quotagroup = QUOTAGROUP;
81 #define sblock sb_un.sblk
86 #define cgblk cg_un.cgblk
91 struct ufs1_dinode dp1;
92 struct ufs2_dinode dp2;
94 #define DIP(dp, field) \
95 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
96 (dp)->dp1.field : (dp)->dp2.field)
102 struct fileusage *fu_next;
107 /* actually bigger */
109 #define FUHASH 1024 /* must be power of two */
110 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
112 int aflag; /* all file systems */
113 int cflag; /* convert format to 32 or 64 bit size */
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 *, const char *);
121 void bread(ufs2_daddr_t, char *, long);
122 void freeinodebuf(void);
125 int getquotagid(void);
128 int oneof(char *, char*[], int);
129 void printchanges(const char *, int, struct dqblk *, struct fileusage *,
131 void setinodebuf(ino_t);
132 int update(const char *, struct quotafile *, int);
136 main(int argc, char *argv[])
141 struct quotafile *qfu, *qfg;
142 int i, argnum, maxrun, errs, ch;
147 while ((ch = getopt(argc, argv, "ac:guvl:")) != -1) {
155 cflag = atoi(optarg);
167 maxrun = atoi(optarg);
175 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
177 if (cflag && cflag != 32 && cflag != 64)
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 * The maxrun (-l) option is now deprecated.
201 warnx("the -l option is now deprecated");
203 exit(checkfstab(uflag, gflag));
205 errx(1, "%s: can't open", FSTAB);
206 while ((fs = getfsent()) != NULL) {
207 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
208 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
209 (name = blockcheck(fs->fs_spec))) {
213 qfu = quota_open(fs, USRQUOTA, O_CREAT|O_RDWR);
216 qfg = quota_open(fs, GRPQUOTA, O_CREAT|O_RDWR);
217 if (qfu == NULL && qfg == NULL)
219 errs += chkquota(name, qfu, qfg);
227 for (i = 0; i < argc; i++)
228 if ((done & (1 << i)) == 0)
229 fprintf(stderr, "%s not found in %s\n",
237 (void)fprintf(stderr, "%s\n%s\n",
238 "usage: quotacheck [-guv] [-c 32 | 64] [-l maxrun] -a",
239 " quotacheck [-guv] [-c 32 | 64] filesystem ...");
244 * Possible superblock locations ordered from most to least likely.
246 static int sblock_try[] = SBLOCKSEARCH;
249 * Scan the specified file system to check quota(s) present on it.
252 chkquota(char *specname, struct quotafile *qfu, struct quotafile *qfg)
254 struct fileusage *fup;
256 int cg, i, mode, errs = 0;
257 ino_t ino, inosused, userino = 0, groupino = 0;
258 dev_t dev, userdev = 0, groupdev = 0;
264 mntpt = quota_fsname(qfu);
265 else if (qfg != NULL)
266 mntpt = quota_fsname(qfg);
268 errx(1, "null quotafile information passed to chkquota()\n");
270 if (vflag && qfu != NULL)
271 printf("%s: convert user quota to %d bits\n",
273 if (qfu != NULL && quota_convert(qfu, cflag) < 0) {
276 "%s: cannot convert an active quota file",
278 err(1, "user quota conversion to size %d failed",
281 if (vflag && qfg != NULL)
282 printf("%s: convert group quota to %d bits\n",
284 if (qfg != NULL && quota_convert(qfg, cflag) < 0) {
287 "%s: cannot convert an active quota file",
289 err(1, "group quota conversion to size %d failed",
293 if ((fi = open(specname, O_RDONLY, 0)) < 0) {
294 warn("%s", specname);
297 if ((stat(mntpt, &sb)) < 0) {
303 (void)printf("*** Checking ");
305 (void)printf("user%s", qfg ? " and " : "");
307 (void)printf("group");
308 (void)printf(" quotas for %s (%s)\n", specname, mntpt);
311 if (stat(quota_qfname(qfu), &sb) == 0) {
317 if (stat(quota_qfname(qfg), &sb) == 0) {
318 groupino = sb.st_ino;
319 groupdev = sb.st_dev;
324 for (i = 0; sblock_try[i] != -1; i++) {
325 bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE);
326 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
327 (sblock.fs_magic == FS_UFS2_MAGIC &&
328 sblock.fs_sblockloc == sblock_try[i])) &&
329 sblock.fs_bsize <= MAXBSIZE &&
330 sblock.fs_bsize >= sizeof(struct fs))
333 if (sblock_try[i] == -1) {
334 warn("Cannot find file system superblock");
337 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
338 maxino = sblock.fs_ncg * sblock.fs_ipg;
339 for (cg = 0; cg < sblock.fs_ncg; cg++) {
340 ino = cg * sblock.fs_ipg;
342 bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk),
344 if (sblock.fs_magic == FS_UFS2_MAGIC)
345 inosused = cgblk.cg_initediblk;
347 inosused = sblock.fs_ipg;
349 * If we are using soft updates, then we can trust the
350 * cylinder group inode allocation maps to tell us which
351 * inodes are allocated. We will scan the used inode map
352 * to find the inodes that are really in use, and then
353 * read only those inodes in from disk.
355 if (sblock.fs_flags & FS_DOSOFTDEP) {
356 if (!cg_chkmagic(&cgblk))
357 errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
358 cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
359 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
362 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
372 for (i = 0; i < inosused; i++, ino++) {
373 if ((dp = getnextinode(ino)) == NULL || ino < ROOTINO ||
374 (mode = DIP(dp, di_mode) & IFMT) == 0)
377 * XXX: Do not account for UIDs or GIDs that appear
378 * to be negative to prevent generating 100GB+
381 if ((int)DIP(dp, di_uid) < 0 ||
382 (int)DIP(dp, di_gid) < 0) {
385 (void)printf("%s: ", mntpt);
386 (void)printf("out of range UID/GID (%u/%u) ino=%u\n",
387 DIP(dp, di_uid), DIP(dp,di_gid),
394 * Do not account for file system snapshot files
395 * or the actual quota data files to be consistent
396 * with how they are handled inside the kernel.
399 if (DIP(dp, di_flags) & SF_SNAPSHOT)
402 if ((ino == userino && dev == userdev) ||
403 (ino == groupino && dev == groupdev))
406 fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA,
409 if (mode == IFREG || mode == IFDIR ||
411 fup->fu_curblocks += DIP(dp, di_blocks);
414 fup = addid((u_long)DIP(dp, di_uid), USRQUOTA,
417 if (mode == IFREG || mode == IFDIR ||
419 fup->fu_curblocks += DIP(dp, di_blocks);
425 errs += update(mntpt, qfu, USRQUOTA);
427 errs += update(mntpt, qfg, GRPQUOTA);
429 (void)fflush(stdout);
434 * Update a specified quota file.
437 update(const char *fsname, struct quotafile *qf, int type)
439 struct fileusage *fup;
440 u_long id, lastid, highid = 0;
443 static struct dqblk zerodqbuf;
444 static struct fileusage zerofileusage;
447 * Scan the on-disk quota file and record any usage changes.
449 lastid = quota_maxid(qf);
450 for (id = 0; id <= lastid; id++) {
451 if (quota_read(qf, &dqbuf, id) < 0)
453 if ((fup = lookup(id, type)) == NULL)
454 fup = &zerofileusage;
455 if (fup->fu_curinodes || fup->fu_curblocks ||
456 dqbuf.dqb_bsoftlimit || dqbuf.dqb_bhardlimit ||
457 dqbuf.dqb_isoftlimit || dqbuf.dqb_ihardlimit)
459 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
460 dqbuf.dqb_curblocks == fup->fu_curblocks) {
461 fup->fu_curinodes = 0;
462 fup->fu_curblocks = 0;
465 printchanges(fsname, type, &dqbuf, fup, id);
466 dqbuf.dqb_curinodes = fup->fu_curinodes;
467 dqbuf.dqb_curblocks = fup->fu_curblocks;
468 (void) quota_write_usage(qf, &dqbuf, id);
469 fup->fu_curinodes = 0;
470 fup->fu_curblocks = 0;
474 * Walk the hash table looking for ids with non-zero usage
475 * that are not currently recorded in the quota file. E.g.
476 * ids that are past the end of the current file.
478 for (id = 0; id < FUHASH; id++) {
479 for (fup = fuhead[type][id]; fup != NULL; fup = fup->fu_next) {
480 if (fup->fu_id <= lastid)
482 if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0)
484 bzero(&dqbuf, sizeof(struct dqblk));
485 if (fup->fu_id > highid)
487 printchanges(fsname, type, &dqbuf, fup, fup->fu_id);
488 dqbuf.dqb_curinodes = fup->fu_curinodes;
489 dqbuf.dqb_curblocks = fup->fu_curblocks;
490 (void) quota_write_usage(qf, &dqbuf, fup->fu_id);
491 fup->fu_curinodes = 0;
492 fup->fu_curblocks = 0;
496 * If this is old format file, then size may be smaller,
497 * so ensure that we only truncate when it will make things
498 * smaller, and not if it will grow an old format file.
500 if (highid < lastid &&
501 stat(quota_qfname(qf), &sb) == 0 &&
502 sb.st_size > (((off_t)highid + 2) * sizeof(struct dqblk)))
503 truncate(quota_qfname(qf),
504 (((off_t)highid + 2) * sizeof(struct dqblk)));
509 * Check to see if target appears in list of size cnt.
512 oneof(char *target, char *list[], int cnt)
516 for (i = 0; i < cnt; i++)
517 if (strcmp(target, list[i]) == 0)
523 * Determine the group identifier for quota files.
530 if ((gr = getgrnam(quotagroup)) != NULL)
536 * Routines to manage the file usage table.
538 * Lookup an id of a specific type.
541 lookup(u_long id, int type)
543 struct fileusage *fup;
545 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
546 if (fup->fu_id == id)
552 * Add a new file usage id if it does not already exist.
555 addid(u_long id, int type, char *name, const char *fsname)
557 struct fileusage *fup, **fhp;
560 if ((fup = lookup(id, type)) != NULL)
566 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
567 errx(1, "calloc failed");
568 fhp = &fuhead[type][id & (FUHASH - 1)];
573 bcopy(name, fup->fu_name, len + 1);
575 (void)sprintf(fup->fu_name, "%lu", id);
577 if (aflag && fsname != NULL)
578 (void)printf("%s: ", fsname);
579 printf("unknown %cid: %lu\n",
580 type == USRQUOTA ? 'u' : 'g', id);
587 * Special purpose version of ginode used to optimize pass
588 * over all the inodes in numerical order.
590 static ino_t nextino, lastinum, lastvalidinum;
591 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
592 static caddr_t inodebuf;
593 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
596 getnextinode(ino_t inumber)
601 static caddr_t nextinop;
603 if (inumber != nextino++ || inumber > lastvalidinum)
604 errx(1, "bad inode number %d to nextinode", inumber);
605 if (inumber >= lastinum) {
607 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
608 if (readcnt % readpercg == 0) {
610 lastinum += partialcnt;
616 * If bread returns an error, it will already have zeroed
617 * out the buffer, so we do not need to do so here.
619 bread(dblk, inodebuf, size);
622 dp = (union dinode *)nextinop;
623 if (sblock.fs_magic == FS_UFS1_MAGIC)
624 nextinop += sizeof(struct ufs1_dinode);
626 nextinop += sizeof(struct ufs2_dinode);
631 * Prepare to scan a set of inodes.
634 setinodebuf(ino_t inum)
637 if (inum % sblock.fs_ipg != 0)
638 errx(1, "bad inode number %d to setinodebuf", inum);
639 lastvalidinum = inum + sblock.fs_ipg - 1;
643 if (inodebuf != NULL)
645 inobufsize = blkroundup(&sblock, INOBUFSIZE);
646 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
647 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
648 readpercg = sblock.fs_ipg / fullcnt;
649 partialcnt = sblock.fs_ipg % fullcnt;
650 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
651 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
652 if (partialcnt != 0) {
655 partialcnt = fullcnt;
656 partialsize = inobufsize;
658 if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
659 errx(1, "cannot allocate space for inode buffer");
663 * Free up data structures used to scan inodes.
669 if (inodebuf != NULL)
675 * Read specified disk blocks.
678 bread(ufs2_daddr_t bno, char *buf, long cnt)
681 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
682 read(fi, buf, cnt) != cnt)
683 errx(1, "bread failed on block %ld", (long)bno);
687 * Display updated block and i-node counts.
690 printchanges(const char *fsname, int type, struct dqblk *dp,
691 struct fileusage *fup, u_long id)
696 (void)printf("%s: ", fsname);
697 if (fup->fu_name[0] == '\0')
698 (void)printf("%-8lu fixed ", id);
700 (void)printf("%-8s fixed ", fup->fu_name);
704 (void)printf("(group):");
708 (void)printf("(user): ");
712 (void)printf("(unknown quota type %d)", type);
715 if (dp->dqb_curinodes != fup->fu_curinodes)
716 (void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes,
717 (u_long)fup->fu_curinodes);
718 if (dp->dqb_curblocks != fup->fu_curblocks)
719 (void)printf("\tblocks %lu -> %lu",
720 (u_long)dp->dqb_curblocks,
721 (u_long)fup->fu_curblocks);