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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1980, 1990, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)quotacheck.c 8.3 (Berkeley) 1/29/94";
47 static const char rcsid[] =
48 "$FreeBSD: src/sbin/quotacheck/quotacheck.c,v 1.11 1999/08/28 00:14:01 peter Exp $";
52 * Fix up / report on disk quotas & usage
54 #include <sys/param.h>
57 #include <ufs/ufs/dinode.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ffs/fs.h>
72 char *qfname = QUOTAFILENAME;
73 char *qfextension[] = INITQFNAMES;
74 char *quotagroup = QUOTAGROUP;
80 #define sblock un.sblk
86 char grpqfname[MAXPATHLEN + 1];
87 char usrqfname[MAXPATHLEN + 1];
93 struct fileusage *fu_next;
100 #define FUHASH 1024 /* must be power of two */
101 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
103 int aflag; /* all file systems */
104 int gflag; /* check group quotas */
105 int uflag; /* check user quotas */
106 int vflag; /* verbose */
107 int fi; /* open disk file descriptor */
108 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
111 addid __P((u_long, int, char *));
112 char *blockcheck __P((char *));
113 void bread __P((daddr_t, char *, long));
114 extern int checkfstab __P((int, int, int (*)(struct fstab *),
115 int (*)(char *, char *, long, int)));
116 int chkquota __P((char *, char *, struct quotaname *));
117 void freeinodebuf __P((void));
119 getnextinode __P((ino_t));
120 int getquotagid __P((void));
121 int hasquota __P((struct fstab *, int, char **));
123 lookup __P((u_long, int));
124 void *needchk __P((struct fstab *));
125 int oneof __P((char *, char*[], int));
126 void resetinodebuf __P((void));
127 int update __P((char *, char *, int));
128 void usage __P((void));
135 register struct fstab *fs;
136 register struct passwd *pw;
137 register struct group *gr;
138 struct quotaname *auxdata;
139 int i, argnum, maxrun, errs;
144 while ((ch = getopt(argc, argv, "aguvl:")) != -1) {
159 maxrun = atoi(optarg);
167 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
169 if (!gflag && !uflag) {
175 while ((gr = getgrent()) != NULL)
176 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
181 while ((pw = getpwent()) != NULL)
182 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
186 exit(checkfstab(1, maxrun, needchk, chkquota));
188 errx(1, "%s: can't open", FSTAB);
189 while ((fs = getfsent()) != NULL) {
190 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
191 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
192 (auxdata = needchk(fs)) &&
193 (name = blockcheck(fs->fs_spec))) {
195 errs += chkquota(name, fs->fs_file, auxdata);
199 for (i = 0; i < argc; i++)
200 if ((done & (1 << i)) == 0)
201 fprintf(stderr, "%s not found in %s\n",
209 (void)fprintf(stderr, "%s\n%s\n",
210 "usage: quotacheck -a [-guv]",
211 " quotacheck [-guv] filesys ...");
217 register struct fstab *fs;
219 register struct quotaname *qnp;
222 if (strcmp(fs->fs_vfstype, "ufs") ||
223 strcmp(fs->fs_type, FSTAB_RW))
225 if ((qnp = malloc(sizeof(*qnp))) == NULL)
226 errx(1, "malloc failed");
228 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
229 strcpy(qnp->grpqfname, qfnp);
230 qnp->flags |= HASGRP;
232 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
233 strcpy(qnp->usrqfname, qfnp);
234 qnp->flags |= HASUSR;
243 * Scan the specified filesystem to check quota(s) present on it.
246 chkquota(fsname, mntpt, qnp)
247 char *fsname, *mntpt;
248 register struct quotaname *qnp;
250 register struct fileusage *fup;
251 register struct dinode *dp;
252 int cg, i, mode, errs = 0;
255 if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
260 (void)printf("*** Checking ");
261 if (qnp->flags & HASUSR)
262 (void)printf("%s%s", qfextension[USRQUOTA],
263 (qnp->flags & HASGRP) ? " and " : "");
264 if (qnp->flags & HASGRP)
265 (void)printf("%s", qfextension[GRPQUOTA]);
266 (void)printf(" quotas for %s (%s)\n", fsname, mntpt);
270 bread(SBOFF, (char *)&sblock, (long)SBSIZE);
271 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
272 maxino = sblock.fs_ncg * sblock.fs_ipg;
274 for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
275 for (i = 0; i < sblock.fs_ipg; i++, ino++) {
278 if ((dp = getnextinode(ino)) == NULL)
280 if ((mode = dp->di_mode & IFMT) == 0)
282 if (qnp->flags & HASGRP) {
283 fup = addid((u_long)dp->di_gid, GRPQUOTA,
286 if (mode == IFREG || mode == IFDIR ||
288 fup->fu_curblocks += dp->di_blocks;
290 if (qnp->flags & HASUSR) {
291 fup = addid((u_long)dp->di_uid, USRQUOTA,
294 if (mode == IFREG || mode == IFDIR ||
296 fup->fu_curblocks += dp->di_blocks;
301 if (qnp->flags & HASUSR)
302 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
303 if (qnp->flags & HASGRP)
304 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
310 * Update a specified quota file.
313 update(fsname, quotafile, type)
314 char *fsname, *quotafile;
317 register struct fileusage *fup;
318 register FILE *qfi, *qfo;
319 register u_long id, lastid;
320 register off_t offset;
322 static int warned = 0;
323 static struct dqblk zerodqbuf;
324 static struct fileusage zerofileusage;
326 if ((qfo = fopen(quotafile, "r+")) == NULL) {
328 qfo = fopen(quotafile, "w+");
330 warnx("creating quota file %s", quotafile);
331 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
332 (void) fchown(fileno(qfo), getuid(), getquotagid());
333 (void) fchmod(fileno(qfo), MODE);
335 warn("%s", quotafile);
339 if ((qfi = fopen(quotafile, "r")) == NULL) {
340 warn("%s", quotafile);
344 if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
345 errno == EOPNOTSUPP && !warned && vflag) {
347 (void)printf("*** Warning: %s\n",
348 "Quotas are not compiled into this kernel");
350 for (lastid = highid[type], id = 0, offset = 0; id <= lastid;
351 id++, offset += sizeof(struct dqblk)) {
352 if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
354 if ((fup = lookup(id, type)) == 0)
355 fup = &zerofileusage;
356 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
357 dqbuf.dqb_curblocks == fup->fu_curblocks) {
358 fup->fu_curinodes = 0;
359 fup->fu_curblocks = 0;
364 printf("%s: ", fsname);
365 printf("%-8s fixed:", fup->fu_name);
366 if (dqbuf.dqb_curinodes != fup->fu_curinodes)
367 (void)printf("\tinodes %lu -> %lu",
368 (u_long)dqbuf.dqb_curinodes,
369 (u_long)fup->fu_curinodes);
370 if (dqbuf.dqb_curblocks != fup->fu_curblocks)
371 (void)printf("\tblocks %lu -> %lu",
372 (u_long)dqbuf.dqb_curblocks,
373 (u_long)fup->fu_curblocks);
377 * Reset time limit if have a soft limit and were
378 * previously under it, but are now over it.
380 if (dqbuf.dqb_bsoftlimit &&
381 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
382 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
384 if (dqbuf.dqb_isoftlimit &&
385 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
386 fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
388 dqbuf.dqb_curinodes = fup->fu_curinodes;
389 dqbuf.dqb_curblocks = fup->fu_curblocks;
390 if (fseek(qfo, (long)offset, SEEK_SET) < 0) {
391 warn("%s: seek failed", quotafile);
394 fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
395 (void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
397 fup->fu_curinodes = 0;
398 fup->fu_curblocks = 0;
402 ftruncate(fileno(qfo),
403 (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
409 * Check to see if target appears in list of size cnt.
412 oneof(target, list, cnt)
413 register char *target, *list[];
418 for (i = 0; i < cnt; i++)
419 if (strcmp(target, list[i]) == 0)
425 * Determine the group identifier for quota files.
432 if ((gr = getgrnam(quotagroup)) != NULL)
438 * Check to see if a particular quota is to be enabled.
441 hasquota(fs, type, qfnamep)
442 register struct fstab *fs;
448 static char initname, usrname[100], grpname[100];
449 static char buf[BUFSIZ];
452 (void)snprintf(usrname, sizeof(usrname),
453 "%s%s", qfextension[USRQUOTA], qfname);
454 (void)snprintf(grpname, sizeof(grpname),
455 "%s%s", qfextension[GRPQUOTA], qfname);
458 strcpy(buf, fs->fs_mntops);
459 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
460 if ((cp = index(opt, '=')) != NULL)
462 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
464 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
472 (void)snprintf(buf, sizeof(buf),
473 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
480 * Routines to manage the file usage table.
482 * Lookup an id of a specific type.
489 register struct fileusage *fup;
491 for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
492 if (fup->fu_id == id)
498 * Add a new file usage id if it does not already exist.
501 addid(id, type, name)
506 struct fileusage *fup, **fhp;
509 if ((fup = lookup(id, type)) != NULL)
515 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
516 errx(1, "calloc failed");
517 fhp = &fuhead[type][id & (FUHASH - 1)];
521 if (id > highid[type])
524 bcopy(name, fup->fu_name, len + 1);
526 (void)sprintf(fup->fu_name, "%lu", id);
528 printf("unknown %cid: %lu\n",
529 type == USRQUOTA ? 'u' : 'g', id);
535 * Special purpose version of ginode used to optimize pass
536 * over all the inodes in numerical order.
538 ino_t nextino, lastinum;
539 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
540 struct dinode *inodebuf;
541 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
544 getnextinode(inumber)
549 static struct dinode *dp;
551 if (inumber != nextino++ || inumber > maxino)
552 errx(1, "bad inode number %d to nextinode", inumber);
553 if (inumber >= lastinum) {
555 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
556 if (readcnt % readpercg == 0) {
558 lastinum += partialcnt;
563 bread(dblk, (char *)inodebuf, size);
570 * Prepare to scan a set of inodes.
579 inobufsize = blkroundup(&sblock, INOBUFSIZE);
580 fullcnt = inobufsize / sizeof(struct dinode);
581 readpercg = sblock.fs_ipg / fullcnt;
582 partialcnt = sblock.fs_ipg % fullcnt;
583 partialsize = partialcnt * sizeof(struct dinode);
584 if (partialcnt != 0) {
587 partialcnt = fullcnt;
588 partialsize = inobufsize;
590 if (inodebuf == NULL &&
591 (inodebuf = malloc((u_int)inobufsize)) == NULL)
592 errx(1, "malloc failed");
593 while (nextino < ROOTINO)
594 getnextinode(nextino);
598 * Free up data structures used to scan inodes.
604 if (inodebuf != NULL)
610 * Read specified disk blocks.
619 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
620 read(fi, buf, cnt) != cnt)
621 errx(1, "block %ld", (long)bno);