2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Robert Elz at The University of Melbourne.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1990, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)quotacheck.c 8.3 (Berkeley) 1/29/94";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
50 * Fix up / report on disk quotas & usage
52 #include <sys/param.h>
53 #include <sys/disklabel.h>
54 #include <sys/mount.h>
57 #include <ufs/ufs/dinode.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ffs/fs.h>
75 #include "quotacheck.h"
77 const char *qfname = QUOTAFILENAME;
78 const char *qfextension[] = INITQFNAMES;
79 const char *quotagroup = QUOTAGROUP;
85 #define sblock sb_un.sblk
90 #define cgblk cg_un.cgblk
95 struct ufs1_dinode dp1;
96 struct ufs2_dinode dp2;
98 #define DIP(dp, field) \
99 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
100 (dp)->dp1.field : (dp)->dp2.field)
106 struct fileusage *fu_next;
111 /* actually bigger */
113 #define FUHASH 1024 /* must be power of two */
114 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
116 int aflag; /* all file systems */
117 int cflag; /* convert format to 32 or 64 bit size */
118 int gflag; /* check group quotas */
119 int uflag; /* check user quotas */
120 int vflag; /* verbose */
121 int fi; /* open disk file descriptor */
124 addid(u_long, int, char *, const char *);
125 void blkread(ufs2_daddr_t, char *, long);
126 void freeinodebuf(void);
129 int getquotagid(void);
132 int oneof(char *, char*[], int);
133 void printchanges(const char *, int, struct dqblk *, struct fileusage *,
135 void setinodebuf(ino_t);
136 int update(const char *, struct quotafile *, int);
140 main(int argc, char *argv[])
145 struct quotafile *qfu, *qfg;
146 int i, argnum, maxrun, errs, ch;
151 while ((ch = getopt(argc, argv, "ac:guvl:")) != -1) {
159 cflag = atoi(optarg);
171 maxrun = atoi(optarg);
179 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
181 if (cflag && cflag != 32 && cflag != 64)
183 if (!gflag && !uflag) {
189 while ((gr = getgrent()) != NULL)
190 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name,
196 while ((pw = getpwent()) != NULL)
197 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name,
202 * The maxrun (-l) option is now deprecated.
205 warnx("the -l option is now deprecated");
207 exit(checkfstab(uflag, gflag));
209 errx(1, "%s: can't open", FSTAB);
210 while ((fs = getfsent()) != NULL) {
211 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
212 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
213 (name = blockcheck(fs->fs_spec))) {
217 qfu = quota_open(fs, USRQUOTA, O_CREAT|O_RDWR);
220 qfg = quota_open(fs, GRPQUOTA, O_CREAT|O_RDWR);
221 if (qfu == NULL && qfg == NULL)
223 errs += chkquota(name, qfu, qfg);
231 for (i = 0; i < argc; i++)
232 if ((done & (1 << i)) == 0)
233 fprintf(stderr, "%s not found in %s\n",
241 (void)fprintf(stderr, "%s\n%s\n",
242 "usage: quotacheck [-guv] [-c 32 | 64] [-l maxrun] -a",
243 " quotacheck [-guv] [-c 32 | 64] filesystem ...");
248 * Scan the specified file system to check quota(s) present on it.
251 chkquota(char *specname, struct quotafile *qfu, struct quotafile *qfg)
253 struct fileusage *fup;
256 int i, ret, mode, errs = 0;
258 ino_t curino, ino, inosused, userino = 0, groupino = 0;
259 dev_t dev, userdev = 0, groupdev = 0;
265 mntpt = quota_fsname(qfu);
266 else if (qfg != NULL)
267 mntpt = quota_fsname(qfg);
269 errx(1, "null quotafile information passed to chkquota()\n");
271 if (vflag && qfu != NULL)
272 printf("%s: convert user quota to %d bits\n",
274 if (qfu != NULL && quota_convert(qfu, cflag) < 0) {
277 "%s: cannot convert an active quota file",
279 err(1, "user quota conversion to size %d failed",
282 if (vflag && qfg != NULL)
283 printf("%s: convert group quota to %d bits\n",
285 if (qfg != NULL && quota_convert(qfg, cflag) < 0) {
288 "%s: cannot convert an active quota file",
290 err(1, "group quota conversion to size %d failed",
294 if ((fi = open(specname, O_RDONLY, 0)) < 0) {
295 warn("%s", specname);
298 if ((stat(mntpt, &sb)) < 0) {
304 (void)printf("*** Checking ");
306 (void)printf("user%s", qfg ? " and " : "");
308 (void)printf("group");
309 (void)printf(" quotas for %s (%s)\n", specname, mntpt);
312 if (stat(quota_qfname(qfu), &sb) == 0) {
318 if (stat(quota_qfname(qfg), &sb) == 0) {
319 groupino = sb.st_ino;
320 groupdev = sb.st_dev;
324 if ((ret = sbget(fi, &fs, UFS_STDSB, UFS_NOCSUM)) != 0) {
327 warn("Cannot find file system superblock");
330 warn("Unable to read file system superblock");
334 bcopy(fs, &sblock, fs->fs_sbsize);
336 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
337 maxino = sblock.fs_ncg * sblock.fs_ipg;
338 for (cg = 0; cg < sblock.fs_ncg; cg++) {
339 ino = cg * sblock.fs_ipg;
341 blkread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk),
343 if (sblock.fs_magic == FS_UFS2_MAGIC)
344 inosused = cgblk.cg_initediblk;
346 inosused = sblock.fs_ipg;
348 * If we are using soft updates, then we can trust the
349 * cylinder group inode allocation maps to tell us which
350 * inodes are allocated. We will scan the used inode map
351 * to find the inodes that are really in use, and then
352 * read only those inodes in from disk.
354 if (sblock.fs_flags & FS_DOSOFTDEP) {
355 if (!cg_chkmagic(&cgblk))
356 errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
357 cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
358 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
361 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
371 for (curino = 0; curino < inosused; curino++, ino++) {
372 if ((dp = getnextinode(ino)) == NULL ||
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=%ju\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 != NULL; 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 %ju to nextinode",
606 if (inumber >= lastinum) {
608 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
609 if (readcnt % readpercg == 0) {
611 lastinum += partialcnt;
617 * If blkread returns an error, it will already have zeroed
618 * out the buffer, so we do not need to do so here.
620 blkread(dblk, inodebuf, size);
623 dp = (union dinode *)nextinop;
624 if (sblock.fs_magic == FS_UFS1_MAGIC)
625 nextinop += sizeof(struct ufs1_dinode);
627 nextinop += sizeof(struct ufs2_dinode);
632 * Prepare to scan a set of inodes.
635 setinodebuf(ino_t inum)
638 if (inum % sblock.fs_ipg != 0)
639 errx(1, "bad inode number %ju to setinodebuf", (uintmax_t)inum);
640 lastvalidinum = inum + sblock.fs_ipg - 1;
644 if (inodebuf != NULL)
646 inobufsize = blkroundup(&sblock, INOBUFSIZE);
647 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
648 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
649 readpercg = sblock.fs_ipg / fullcnt;
650 partialcnt = sblock.fs_ipg % fullcnt;
651 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
652 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
653 if (partialcnt != 0) {
656 partialcnt = fullcnt;
657 partialsize = inobufsize;
659 if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
660 errx(1, "cannot allocate space for inode buffer");
664 * Free up data structures used to scan inodes.
670 if (inodebuf != NULL)
676 * Read specified disk blocks.
679 blkread(ufs2_daddr_t bno, char *buf, long cnt)
682 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
683 read(fi, buf, cnt) != cnt)
684 errx(1, "blkread failed on block %ld", (long)bno);
688 * Display updated block and i-node counts.
691 printchanges(const char *fsname, int type, struct dqblk *dp,
692 struct fileusage *fup, u_long id)
697 (void)printf("%s: ", fsname);
698 if (fup->fu_name[0] == '\0')
699 (void)printf("%-8lu fixed ", id);
701 (void)printf("%-8s fixed ", fup->fu_name);
705 (void)printf("(group):");
709 (void)printf("(user): ");
713 (void)printf("(unknown quota type %d)", type);
716 if (dp->dqb_curinodes != fup->fu_curinodes)
717 (void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes,
718 (u_long)fup->fu_curinodes);
719 if (dp->dqb_curblocks != fup->fu_curblocks)
720 (void)printf("\tblocks %lu -> %lu",
721 (u_long)dp->dqb_curblocks,
722 (u_long)fup->fu_curblocks);