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 $
42 * Fix up / report on disk quotas & usage
44 #include <sys/param.h>
47 #include <vfs/ufs/dinode.h>
48 #include <vfs/ufs/quota.h>
49 #include <vfs/ufs/fs.h>
62 const char *qfname = QUOTAFILENAME;
63 const char *qfextension[] = INITQFNAMES;
64 const char *quotagroup = QUOTAGROUP;
70 #define sblock un.sblk
76 char grpqfname[MAXPATHLEN + 1];
77 char usrqfname[MAXPATHLEN + 1];
83 struct fileusage *fu_next;
90 #define FUHASH 1024 /* must be power of two */
91 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
93 int aflag; /* all file systems */
94 int gflag; /* check group quotas */
95 int uflag; /* check user quotas */
96 int vflag; /* verbose */
97 int fi; /* open disk file descriptor */
98 u_long highid[MAXQUOTAS]; /* highest addid()'ed identifier per type */
101 addid(u_long, int, char *);
102 char *blockcheck(char *);
103 void bread(daddr_t, char *, long);
104 extern int checkfstab(int, int, void * (*)(struct fstab *),
105 int (*)(char *, char *, struct quotaname *));
106 int chkquota(char *, char *, struct quotaname *);
107 void freeinodebuf(void);
110 int getquotagid(void);
111 int hasquota(struct fstab *, int, char **);
114 void *needchk(struct fstab *);
115 int oneof(char *, char*[], int);
116 void resetinodebuf(void);
117 int update(char *, char *, int);
121 main(int argc, char **argv)
126 struct quotaname *auxdata;
127 int i, argnum, maxrun, errs;
132 while ((ch = getopt(argc, argv, "aguvl:")) != -1) {
147 maxrun = atoi(optarg);
155 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
157 if (!gflag && !uflag) {
163 while ((gr = getgrent()) != NULL)
164 addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
169 while ((pw = getpwent()) != NULL)
170 addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
174 exit(checkfstab(1, maxrun, needchk, chkquota));
176 errx(1, "%s: can't open", FSTAB);
177 while ((fs = getfsent()) != NULL) {
178 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
179 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
180 (auxdata = needchk(fs)) &&
181 (name = blockcheck(fs->fs_spec))) {
183 errs += chkquota(name, fs->fs_file, auxdata);
187 for (i = 0; i < argc; i++)
188 if ((done & (1 << i)) == 0)
189 fprintf(stderr, "%s not found in %s\n",
197 fprintf(stderr, "%s\n%s\n",
198 "usage: quotacheck -a [-guv]",
199 " quotacheck [-guv] filesys ...");
204 needchk(struct fstab *fs)
206 struct quotaname *qnp;
209 if (strcmp(fs->fs_vfstype, "ufs") ||
210 strcmp(fs->fs_type, FSTAB_RW))
212 if ((qnp = malloc(sizeof(*qnp))) == NULL)
213 errx(1, "malloc failed");
215 if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
216 strcpy(qnp->grpqfname, qfnp);
217 qnp->flags |= HASGRP;
219 if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
220 strcpy(qnp->usrqfname, qfnp);
221 qnp->flags |= HASUSR;
230 * Scan the specified filesystem to check quota(s) present on it.
233 chkquota(char *fsname, char *mntpt, struct quotaname *qnp)
235 struct fileusage *fup;
236 struct ufs1_dinode *dp;
237 int cg, i, mode, errs = 0;
240 if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
245 printf("*** Checking ");
246 if (qnp->flags & HASUSR)
247 printf("%s%s", qfextension[USRQUOTA],
248 (qnp->flags & HASGRP) ? " and " : "");
249 if (qnp->flags & HASGRP)
250 printf("%s", qfextension[GRPQUOTA]);
251 printf(" quotas for %s (%s)\n", fsname, mntpt);
255 bread(SBOFF, (char *)&sblock, (long)SBSIZE);
256 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
257 maxino = sblock.fs_ncg * sblock.fs_ipg;
259 for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
260 for (i = 0; i < sblock.fs_ipg; i++, ino++) {
263 if ((dp = getnextinode(ino)) == NULL)
265 if ((mode = dp->di_mode & IFMT) == 0)
267 if (qnp->flags & HASGRP) {
268 fup = addid((u_long)dp->di_gid, GRPQUOTA,
271 if (mode == IFREG || mode == IFDIR ||
273 fup->fu_curblocks += dp->di_blocks;
275 if (qnp->flags & HASUSR) {
276 fup = addid((u_long)dp->di_uid, USRQUOTA,
279 if (mode == IFREG || mode == IFDIR ||
281 fup->fu_curblocks += dp->di_blocks;
286 if (qnp->flags & HASUSR)
287 errs += update(mntpt, qnp->usrqfname, USRQUOTA);
288 if (qnp->flags & HASGRP)
289 errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
295 * Update a specified quota file.
298 update(char *fsname, char *quotafile, int type)
300 struct fileusage *fup;
304 struct ufs_dqblk dqbuf;
305 static int warned = 0;
306 static struct ufs_dqblk zerodqbuf;
307 static struct fileusage zerofileusage;
309 if ((qfo = fopen(quotafile, "r+")) == NULL) {
311 qfo = fopen(quotafile, "w+");
313 warnx("creating quota file %s", quotafile);
314 #define MODE (S_IRUSR|S_IWUSR|S_IRGRP)
315 fchown(fileno(qfo), getuid(), getquotagid());
316 fchmod(fileno(qfo), MODE);
318 warn("%s", quotafile);
322 if ((qfi = fopen(quotafile, "r")) == NULL) {
323 warn("%s", quotafile);
327 if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
328 errno == EOPNOTSUPP && !warned && vflag) {
330 printf("*** Warning: %s\n",
331 "Quotas are not compiled into this kernel");
333 for (lastid = highid[type], id = 0, offset = 0; id <= lastid;
334 id++, offset += sizeof(struct ufs_dqblk)) {
335 if (fread((char *)&dqbuf, sizeof(struct ufs_dqblk), 1, qfi) == 0)
337 if ((fup = lookup(id, type)) == NULL)
338 fup = &zerofileusage;
339 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
340 dqbuf.dqb_curblocks == fup->fu_curblocks) {
341 fup->fu_curinodes = 0;
342 fup->fu_curblocks = 0;
347 printf("%s: ", fsname);
348 printf("%-8s fixed:", fup->fu_name);
349 if (dqbuf.dqb_curinodes != fup->fu_curinodes)
350 printf("\tinodes %lu -> %lu",
351 (u_long)dqbuf.dqb_curinodes,
352 (u_long)fup->fu_curinodes);
353 if (dqbuf.dqb_curblocks != fup->fu_curblocks)
354 printf("\tblocks %lu -> %lu",
355 (u_long)dqbuf.dqb_curblocks,
356 (u_long)fup->fu_curblocks);
360 * Reset time limit if have a soft limit and were
361 * previously under it, but are now over it.
363 if (dqbuf.dqb_bsoftlimit &&
364 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
365 fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
367 if (dqbuf.dqb_isoftlimit &&
368 dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
369 fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
371 dqbuf.dqb_curinodes = fup->fu_curinodes;
372 dqbuf.dqb_curblocks = fup->fu_curblocks;
373 if (fseek(qfo, (long)offset, SEEK_SET) < 0) {
374 warn("%s: seek failed", quotafile);
377 fwrite((char *)&dqbuf, sizeof(struct ufs_dqblk), 1, qfo);
378 quotactl(fsname, QCMD(Q_SETUSE, type), id,
380 fup->fu_curinodes = 0;
381 fup->fu_curblocks = 0;
385 ftruncate(fileno(qfo),
386 (off_t)((highid[type] + 1) * sizeof(struct ufs_dqblk)));
392 * Check to see if target appears in list of size cnt.
395 oneof(char *target, char **list, int cnt)
399 for (i = 0; i < cnt; i++)
400 if (strcmp(target, list[i]) == 0)
406 * Determine the group identifier for quota files.
413 if ((gr = getgrnam(quotagroup)) != NULL)
419 * Check to see if a particular quota is to be enabled.
422 hasquota(struct fstab *fs, int type, char **qfnamep)
426 static char initname, usrname[100], grpname[100];
427 static char buf[BUFSIZ];
430 snprintf(usrname, sizeof(usrname),
431 "%s%s", qfextension[USRQUOTA], qfname);
432 snprintf(grpname, sizeof(grpname),
433 "%s%s", qfextension[GRPQUOTA], qfname);
436 strcpy(buf, fs->fs_mntops);
437 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
438 if ((cp = strchr(opt, '=')) != NULL)
440 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
442 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
450 snprintf(buf, sizeof(buf),
451 "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
458 * Routines to manage the file usage table.
460 * Lookup an id of a specific type.
463 lookup(u_long id, int type)
465 struct fileusage *fup;
467 for (fup = fuhead[type][id & (FUHASH-1)]; fup != NULL; fup = fup->fu_next)
468 if (fup->fu_id == id)
474 * Add a new file usage id if it does not already exist.
477 addid(u_long id, int type, char *name)
479 struct fileusage *fup, **fhp;
482 if ((fup = lookup(id, type)) != NULL)
488 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
489 errx(1, "calloc failed");
490 fhp = &fuhead[type][id & (FUHASH - 1)];
494 if (id > highid[type])
497 bcopy(name, fup->fu_name, len + 1);
499 sprintf(fup->fu_name, "%lu", id);
501 printf("unknown %cid: %lu\n",
502 type == USRQUOTA ? 'u' : 'g', id);
508 * Special purpose version of ginode used to optimize pass
509 * over all the inodes in numerical order.
511 ino_t nextino, lastinum;
512 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
513 struct ufs1_dinode *inodebuf;
514 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
517 getnextinode(ino_t inumber)
521 static struct ufs1_dinode *dp;
523 if (inumber != nextino++ || inumber > maxino) {
524 errx(1, "bad inode number %ju to nextinode",
527 if (inumber >= lastinum) {
529 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
530 if (readcnt % readpercg == 0) {
532 lastinum += partialcnt;
537 bread(dblk, (char *)inodebuf, size);
544 * Prepare to scan a set of inodes.
553 inobufsize = blkroundup(&sblock, INOBUFSIZE);
554 fullcnt = inobufsize / sizeof(struct ufs1_dinode);
555 readpercg = sblock.fs_ipg / fullcnt;
556 partialcnt = sblock.fs_ipg % fullcnt;
557 partialsize = partialcnt * sizeof(struct ufs1_dinode);
558 if (partialcnt != 0) {
561 partialcnt = fullcnt;
562 partialsize = inobufsize;
564 if (inodebuf == NULL &&
565 (inodebuf = malloc((u_int)inobufsize)) == NULL)
566 errx(1, "malloc failed");
567 while (nextino < ROOTINO)
568 getnextinode(nextino);
572 * Free up data structures used to scan inodes.
578 if (inodebuf != NULL)
584 * Read specified disk blocks.
587 bread(daddr_t bno, char *buf, long cnt)
590 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
591 read(fi, buf, cnt) != cnt)
592 errx(1, "block %ld", (long)bno);