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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1980, 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)quotacheck.c 8.3 (Berkeley) 1/29/94
38 * $FreeBSD: src/sbin/quotacheck/quotacheck.c,v 1.11 1999/08/28 00:14:01 peter Exp $
39 * $DragonFly: src/sbin/quotacheck/quotacheck.c,v 1.10 2006/10/19 21:22:13 pavalos Exp $
43 * Fix up / report on disk quotas & usage
45 #include <sys/param.h>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/quota.h>
50 #include <vfs/ufs/fs.h>
63 const char *qfname = QUOTAFILENAME;
64 const char *qfextension[] = INITQFNAMES;
65 const char *quotagroup = QUOTAGROUP;
71 #define sblock un.sblk
77 char grpqfname[MAXPATHLEN + 1];
78 char usrqfname[MAXPATHLEN + 1];
84 struct fileusage *fu_next;
91 #define FUHASH 1024 /* must be power of two */
92 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
94 int aflag; /* all file systems */
95 int gflag; /* check group quotas */
96 int uflag; /* check user quotas */
97 int vflag; /* verbose */
98 int fi; /* open disk file descriptor */
99 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
102 addid(u_long, int, char *);
103 char *blockcheck(char *);
104 void bread(daddr_t, char *, long);
105 extern int checkfstab(int, int, void * (*)(struct fstab *),
106 int (*)(char *, char *, struct quotaname *));
107 int chkquota(char *, char *, struct quotaname *);
108 void freeinodebuf(void);
111 int getquotagid(void);
112 int hasquota(struct fstab *, int, char **);
115 void *needchk(struct fstab *);
116 int oneof(char *, char*[], int);
117 void resetinodebuf(void);
118 int update(char *, char *, int);
122 main(int argc, char **argv)
127 struct quotaname *auxdata;
128 int i, argnum, maxrun, errs;
133 while ((ch = getopt(argc, argv, "aguvl:")) != -1) {
148 maxrun = atoi(optarg);
156 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
158 if (!gflag && !uflag) {
164 while ((gr = getgrent()) != NULL)
165 addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
170 while ((pw = getpwent()) != NULL)
171 addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
175 exit(checkfstab(1, maxrun, needchk, chkquota));
177 errx(1, "%s: can't open", FSTAB);
178 while ((fs = getfsent()) != NULL) {
179 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
180 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
181 (auxdata = needchk(fs)) &&
182 (name = blockcheck(fs->fs_spec))) {
184 errs += chkquota(name, fs->fs_file, auxdata);
188 for (i = 0; i < argc; i++)
189 if ((done & (1 << i)) == 0)
190 fprintf(stderr, "%s not found in %s\n",
198 fprintf(stderr, "%s\n%s\n",
199 "usage: quotacheck -a [-guv]",
200 " quotacheck [-guv] filesys ...");
205 needchk(struct fstab *fs)
207 struct quotaname *qnp;
210 if (strcmp(fs->fs_vfstype, "ufs") ||
211 strcmp(fs->fs_type, FSTAB_RW))
213 if ((qnp = malloc(sizeof(*qnp))) == NULL)
214 errx(1, "malloc failed");
216 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
217 strcpy(qnp->grpqfname, qfnp);
218 qnp->flags |= HASGRP;
220 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
221 strcpy(qnp->usrqfname, qfnp);
222 qnp->flags |= HASUSR;
231 * Scan the specified filesystem to check quota(s) present on it.
234 chkquota(char *fsname, char *mntpt, struct quotaname *qnp)
236 struct fileusage *fup;
237 struct ufs1_dinode *dp;
238 int cg, i, mode, errs = 0;
241 if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
246 printf("*** Checking ");
247 if (qnp->flags & HASUSR)
248 printf("%s%s", qfextension[USRQUOTA],
249 (qnp->flags & HASGRP) ? " and " : "");
250 if (qnp->flags & HASGRP)
251 printf("%s", qfextension[GRPQUOTA]);
252 printf(" quotas for %s (%s)\n", fsname, mntpt);
256 bread(SBOFF, (char *)&sblock, (long)SBSIZE);
257 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
258 maxino = sblock.fs_ncg * sblock.fs_ipg;
260 for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
261 for (i = 0; i < sblock.fs_ipg; i++, ino++) {
264 if ((dp = getnextinode(ino)) == NULL)
266 if ((mode = dp->di_mode & IFMT) == 0)
268 if (qnp->flags & HASGRP) {
269 fup = addid((u_long)dp->di_gid, GRPQUOTA,
272 if (mode == IFREG || mode == IFDIR ||
274 fup->fu_curblocks += dp->di_blocks;
276 if (qnp->flags & HASUSR) {
277 fup = addid((u_long)dp->di_uid, USRQUOTA,
280 if (mode == IFREG || mode == IFDIR ||
282 fup->fu_curblocks += dp->di_blocks;
287 if (qnp->flags & HASUSR)
288 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
289 if (qnp->flags & HASGRP)
290 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
296 * Update a specified quota file.
299 update(char *fsname, char *quotafile, int type)
301 struct fileusage *fup;
305 struct ufs_dqblk dqbuf;
306 static int warned = 0;
307 static struct ufs_dqblk zerodqbuf;
308 static struct fileusage zerofileusage;
310 if ((qfo = fopen(quotafile, "r+")) == NULL) {
312 qfo = fopen(quotafile, "w+");
314 warnx("creating quota file %s", quotafile);
315 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
316 fchown(fileno(qfo), getuid(), getquotagid());
317 fchmod(fileno(qfo), MODE);
319 warn("%s", quotafile);
323 if ((qfi = fopen(quotafile, "r")) == NULL) {
324 warn("%s", quotafile);
328 if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
329 errno == EOPNOTSUPP && !warned && vflag) {
331 printf("*** Warning: %s\n",
332 "Quotas are not compiled into this kernel");
334 for (lastid = highid[type], id = 0, offset = 0; id <= lastid;
335 id++, offset += sizeof(struct ufs_dqblk)) {
336 if (fread((char *)&dqbuf, sizeof(struct ufs_dqblk), 1, qfi) == 0)
338 if ((fup = lookup(id, type)) == 0)
339 fup = &zerofileusage;
340 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
341 dqbuf.dqb_curblocks == fup->fu_curblocks) {
342 fup->fu_curinodes = 0;
343 fup->fu_curblocks = 0;
348 printf("%s: ", fsname);
349 printf("%-8s fixed:", fup->fu_name);
350 if (dqbuf.dqb_curinodes != fup->fu_curinodes)
351 printf("\tinodes %lu -> %lu",
352 (u_long)dqbuf.dqb_curinodes,
353 (u_long)fup->fu_curinodes);
354 if (dqbuf.dqb_curblocks != fup->fu_curblocks)
355 printf("\tblocks %lu -> %lu",
356 (u_long)dqbuf.dqb_curblocks,
357 (u_long)fup->fu_curblocks);
361 * Reset time limit if have a soft limit and were
362 * previously under it, but are now over it.
364 if (dqbuf.dqb_bsoftlimit &&
365 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
366 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
368 if (dqbuf.dqb_isoftlimit &&
369 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
370 fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
372 dqbuf.dqb_curinodes = fup->fu_curinodes;
373 dqbuf.dqb_curblocks = fup->fu_curblocks;
374 if (fseek(qfo, (long)offset, SEEK_SET) < 0) {
375 warn("%s: seek failed", quotafile);
378 fwrite((char *)&dqbuf, sizeof(struct ufs_dqblk), 1, qfo);
379 quotactl(fsname, QCMD(Q_SETUSE, type), id,
381 fup->fu_curinodes = 0;
382 fup->fu_curblocks = 0;
386 ftruncate(fileno(qfo),
387 (off_t)((highid[type] + 1) * sizeof(struct ufs_dqblk)));
393 * Check to see if target appears in list of size cnt.
396 oneof(char *target, char **list, int cnt)
400 for (i = 0; i < cnt; i++)
401 if (strcmp(target, list[i]) == 0)
407 * Determine the group identifier for quota files.
414 if ((gr = getgrnam(quotagroup)) != NULL)
420 * Check to see if a particular quota is to be enabled.
423 hasquota(struct fstab *fs, int type, char **qfnamep)
427 static char initname, usrname[100], grpname[100];
428 static char buf[BUFSIZ];
431 snprintf(usrname, sizeof(usrname),
432 "%s%s", qfextension[USRQUOTA], qfname);
433 snprintf(grpname, sizeof(grpname),
434 "%s%s", qfextension[GRPQUOTA], qfname);
437 strcpy(buf, fs->fs_mntops);
438 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
439 if ((cp = strchr(opt, '=')) != NULL)
441 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
443 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
451 snprintf(buf, sizeof(buf),
452 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
459 * Routines to manage the file usage table.
461 * Lookup an id of a specific type.
464 lookup(u_long id, int type)
466 struct fileusage *fup;
468 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
469 if (fup->fu_id == id)
475 * Add a new file usage id if it does not already exist.
478 addid(u_long id, int type, char *name)
480 struct fileusage *fup, **fhp;
483 if ((fup = lookup(id, type)) != NULL)
489 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
490 errx(1, "calloc failed");
491 fhp = &fuhead[type][id & (FUHASH - 1)];
495 if (id > highid[type])
498 bcopy(name, fup->fu_name, len + 1);
500 sprintf(fup->fu_name, "%lu", id);
502 printf("unknown %cid: %lu\n",
503 type == USRQUOTA ? 'u' : 'g', id);
509 * Special purpose version of ginode used to optimize pass
510 * over all the inodes in numerical order.
512 ino_t nextino, lastinum;
513 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
514 struct ufs1_dinode *inodebuf;
515 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
518 getnextinode(ino_t inumber)
522 static struct ufs1_dinode *dp;
524 if (inumber != nextino++ || inumber > maxino)
525 errx(1, "bad inode number %llu to nextinode", inumber);
526 if (inumber >= lastinum) {
528 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
529 if (readcnt % readpercg == 0) {
531 lastinum += partialcnt;
536 bread(dblk, (char *)inodebuf, size);
543 * Prepare to scan a set of inodes.
552 inobufsize = blkroundup(&sblock, INOBUFSIZE);
553 fullcnt = inobufsize / sizeof(struct ufs1_dinode);
554 readpercg = sblock.fs_ipg / fullcnt;
555 partialcnt = sblock.fs_ipg % fullcnt;
556 partialsize = partialcnt * sizeof(struct ufs1_dinode);
557 if (partialcnt != 0) {
560 partialcnt = fullcnt;
561 partialsize = inobufsize;
563 if (inodebuf == NULL &&
564 (inodebuf = malloc((u_int)inobufsize)) == NULL)
565 errx(1, "malloc failed");
566 while (nextino < ROOTINO)
567 getnextinode(nextino);
571 * Free up data structures used to scan inodes.
577 if (inodebuf != NULL)
583 * Read specified disk blocks.
586 bread(daddr_t bno, char *buf, long cnt)
589 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
590 read(fi, buf, cnt) != cnt)
591 errx(1, "block %ld", (long)bno);