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.2 2003/06/17 04:27:34 dillon Exp $
43 * Fix up / report on disk quotas & usage
45 #include <sys/param.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/quota.h>
50 #include <ufs/ffs/fs.h>
63 char *qfname = QUOTAFILENAME;
64 char *qfextension[] = INITQFNAMES;
65 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 __P((u_long, int, char *));
103 char *blockcheck __P((char *));
104 void bread __P((daddr_t, char *, long));
105 extern int checkfstab __P((int, int, int (*)(struct fstab *),
106 int (*)(char *, char *, long, int)));
107 int chkquota __P((char *, char *, struct quotaname *));
108 void freeinodebuf __P((void));
110 getnextinode __P((ino_t));
111 int getquotagid __P((void));
112 int hasquota __P((struct fstab *, int, char **));
114 lookup __P((u_long, int));
115 void *needchk __P((struct fstab *));
116 int oneof __P((char *, char*[], int));
117 void resetinodebuf __P((void));
118 int update __P((char *, char *, int));
119 void usage __P((void));
126 register struct fstab *fs;
127 register struct passwd *pw;
128 register struct group *gr;
129 struct quotaname *auxdata;
130 int i, argnum, maxrun, errs;
135 while ((ch = getopt(argc, argv, "aguvl:")) != -1) {
150 maxrun = atoi(optarg);
158 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
160 if (!gflag && !uflag) {
166 while ((gr = getgrent()) != NULL)
167 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
172 while ((pw = getpwent()) != NULL)
173 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
177 exit(checkfstab(1, maxrun, needchk, chkquota));
179 errx(1, "%s: can't open", FSTAB);
180 while ((fs = getfsent()) != NULL) {
181 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
182 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
183 (auxdata = needchk(fs)) &&
184 (name = blockcheck(fs->fs_spec))) {
186 errs += chkquota(name, fs->fs_file, auxdata);
190 for (i = 0; i < argc; i++)
191 if ((done & (1 << i)) == 0)
192 fprintf(stderr, "%s not found in %s\n",
200 (void)fprintf(stderr, "%s\n%s\n",
201 "usage: quotacheck -a [-guv]",
202 " quotacheck [-guv] filesys ...");
208 register struct fstab *fs;
210 register struct quotaname *qnp;
213 if (strcmp(fs->fs_vfstype, "ufs") ||
214 strcmp(fs->fs_type, FSTAB_RW))
216 if ((qnp = malloc(sizeof(*qnp))) == NULL)
217 errx(1, "malloc failed");
219 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
220 strcpy(qnp->grpqfname, qfnp);
221 qnp->flags |= HASGRP;
223 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
224 strcpy(qnp->usrqfname, qfnp);
225 qnp->flags |= HASUSR;
234 * Scan the specified filesystem to check quota(s) present on it.
237 chkquota(fsname, mntpt, qnp)
238 char *fsname, *mntpt;
239 register struct quotaname *qnp;
241 register struct fileusage *fup;
242 register struct dinode *dp;
243 int cg, i, mode, errs = 0;
246 if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
251 (void)printf("*** Checking ");
252 if (qnp->flags & HASUSR)
253 (void)printf("%s%s", qfextension[USRQUOTA],
254 (qnp->flags & HASGRP) ? " and " : "");
255 if (qnp->flags & HASGRP)
256 (void)printf("%s", qfextension[GRPQUOTA]);
257 (void)printf(" quotas for %s (%s)\n", fsname, mntpt);
261 bread(SBOFF, (char *)&sblock, (long)SBSIZE);
262 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
263 maxino = sblock.fs_ncg * sblock.fs_ipg;
265 for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
266 for (i = 0; i < sblock.fs_ipg; i++, ino++) {
269 if ((dp = getnextinode(ino)) == NULL)
271 if ((mode = dp->di_mode & IFMT) == 0)
273 if (qnp->flags & HASGRP) {
274 fup = addid((u_long)dp->di_gid, GRPQUOTA,
277 if (mode == IFREG || mode == IFDIR ||
279 fup->fu_curblocks += dp->di_blocks;
281 if (qnp->flags & HASUSR) {
282 fup = addid((u_long)dp->di_uid, USRQUOTA,
285 if (mode == IFREG || mode == IFDIR ||
287 fup->fu_curblocks += dp->di_blocks;
292 if (qnp->flags & HASUSR)
293 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
294 if (qnp->flags & HASGRP)
295 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
301 * Update a specified quota file.
304 update(fsname, quotafile, type)
305 char *fsname, *quotafile;
308 register struct fileusage *fup;
309 register FILE *qfi, *qfo;
310 register u_long id, lastid;
311 register off_t offset;
313 static int warned = 0;
314 static struct dqblk zerodqbuf;
315 static struct fileusage zerofileusage;
317 if ((qfo = fopen(quotafile, "r+")) == NULL) {
319 qfo = fopen(quotafile, "w+");
321 warnx("creating quota file %s", quotafile);
322 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
323 (void) fchown(fileno(qfo), getuid(), getquotagid());
324 (void) fchmod(fileno(qfo), MODE);
326 warn("%s", quotafile);
330 if ((qfi = fopen(quotafile, "r")) == NULL) {
331 warn("%s", quotafile);
335 if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
336 errno == EOPNOTSUPP && !warned && vflag) {
338 (void)printf("*** Warning: %s\n",
339 "Quotas are not compiled into this kernel");
341 for (lastid = highid[type], id = 0, offset = 0; id <= lastid;
342 id++, offset += sizeof(struct dqblk)) {
343 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
345 if ((fup = lookup(id, type)) == 0)
346 fup = &zerofileusage;
347 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
348 dqbuf.dqb_curblocks == fup->fu_curblocks) {
349 fup->fu_curinodes = 0;
350 fup->fu_curblocks = 0;
355 printf("%s: ", fsname);
356 printf("%-8s fixed:", fup->fu_name);
357 if (dqbuf.dqb_curinodes != fup->fu_curinodes)
358 (void)printf("\tinodes %lu -> %lu",
359 (u_long)dqbuf.dqb_curinodes,
360 (u_long)fup->fu_curinodes);
361 if (dqbuf.dqb_curblocks != fup->fu_curblocks)
362 (void)printf("\tblocks %lu -> %lu",
363 (u_long)dqbuf.dqb_curblocks,
364 (u_long)fup->fu_curblocks);
368 * Reset time limit if have a soft limit and were
369 * previously under it, but are now over it.
371 if (dqbuf.dqb_bsoftlimit &&
372 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
373 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
375 if (dqbuf.dqb_isoftlimit &&
376 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
377 fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
379 dqbuf.dqb_curinodes = fup->fu_curinodes;
380 dqbuf.dqb_curblocks = fup->fu_curblocks;
381 if (fseek(qfo, (long)offset, SEEK_SET) < 0) {
382 warn("%s: seek failed", quotafile);
385 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
386 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
388 fup->fu_curinodes = 0;
389 fup->fu_curblocks = 0;
393 ftruncate(fileno(qfo),
394 (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
400 * Check to see if target appears in list of size cnt.
403 oneof(target, list, cnt)
404 register char *target, *list[];
409 for (i = 0; i < cnt; i++)
410 if (strcmp(target, list[i]) == 0)
416 * Determine the group identifier for quota files.
423 if ((gr = getgrnam(quotagroup)) != NULL)
429 * Check to see if a particular quota is to be enabled.
432 hasquota(fs, type, qfnamep)
433 register struct fstab *fs;
439 static char initname, usrname[100], grpname[100];
440 static char buf[BUFSIZ];
443 (void)snprintf(usrname, sizeof(usrname),
444 "%s%s", qfextension[USRQUOTA], qfname);
445 (void)snprintf(grpname, sizeof(grpname),
446 "%s%s", qfextension[GRPQUOTA], qfname);
449 strcpy(buf, fs->fs_mntops);
450 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
451 if ((cp = index(opt, '=')) != NULL)
453 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
455 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
463 (void)snprintf(buf, sizeof(buf),
464 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
471 * Routines to manage the file usage table.
473 * Lookup an id of a specific type.
480 register struct fileusage *fup;
482 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
483 if (fup->fu_id == id)
489 * Add a new file usage id if it does not already exist.
492 addid(id, type, name)
497 struct fileusage *fup, **fhp;
500 if ((fup = lookup(id, type)) != NULL)
506 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
507 errx(1, "calloc failed");
508 fhp = &fuhead[type][id & (FUHASH - 1)];
512 if (id > highid[type])
515 bcopy(name, fup->fu_name, len + 1);
517 (void)sprintf(fup->fu_name, "%lu", id);
519 printf("unknown %cid: %lu\n",
520 type == USRQUOTA ? 'u' : 'g', id);
526 * Special purpose version of ginode used to optimize pass
527 * over all the inodes in numerical order.
529 ino_t nextino, lastinum;
530 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
531 struct dinode *inodebuf;
532 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
535 getnextinode(inumber)
540 static struct dinode *dp;
542 if (inumber != nextino++ || inumber > maxino)
543 errx(1, "bad inode number %d to nextinode", inumber);
544 if (inumber >= lastinum) {
546 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
547 if (readcnt % readpercg == 0) {
549 lastinum += partialcnt;
554 bread(dblk, (char *)inodebuf, size);
561 * Prepare to scan a set of inodes.
570 inobufsize = blkroundup(&sblock, INOBUFSIZE);
571 fullcnt = inobufsize / sizeof(struct dinode);
572 readpercg = sblock.fs_ipg / fullcnt;
573 partialcnt = sblock.fs_ipg % fullcnt;
574 partialsize = partialcnt * sizeof(struct dinode);
575 if (partialcnt != 0) {
578 partialcnt = fullcnt;
579 partialsize = inobufsize;
581 if (inodebuf == NULL &&
582 (inodebuf = malloc((u_int)inobufsize)) == NULL)
583 errx(1, "malloc failed");
584 while (nextino < ROOTINO)
585 getnextinode(nextino);
589 * Free up data structures used to scan inodes.
595 if (inodebuf != NULL)
601 * Read specified disk blocks.
610 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
611 read(fi, buf, cnt) != cnt)
612 errx(1, "block %ld", (long)bno);