indent(1): Fix breakage caused by single comment following "else".
[freebsd-src.git] / sbin / quotacheck / quotacheck.c
blob0ee5b092887e13590878b489d30d664067dc1388
1 /*
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
10 * are met:
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #if 0
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
40 #ifndef lint
41 static char sccsid[] = "@(#)quotacheck.c 8.3 (Berkeley) 1/29/94";
42 #endif /* not lint */
43 #endif
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
48 * Fix up / report on disk quotas & usage
50 #include <sys/param.h>
51 #include <sys/disklabel.h>
52 #include <sys/mount.h>
53 #include <sys/stat.h>
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ffs/fs.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <fstab.h>
63 #include <grp.h>
64 #include <libutil.h>
65 #include <pwd.h>
66 #include <stdint.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
72 #include "quotacheck.h"
74 char *qfname = QUOTAFILENAME;
75 char *qfextension[] = INITQFNAMES;
76 char *quotagroup = QUOTAGROUP;
78 union {
79 struct fs sblk;
80 char dummy[MAXBSIZE];
81 } sb_un;
82 #define sblock sb_un.sblk
83 union {
84 struct cg cgblk;
85 char dummy[MAXBSIZE];
86 } cg_un;
87 #define cgblk cg_un.cgblk
88 long dev_bsize = 1;
89 ino_t maxino;
91 union dinode {
92 struct ufs1_dinode dp1;
93 struct ufs2_dinode dp2;
95 #define DIP(dp, field) \
96 ((sblock.fs_magic == FS_UFS1_MAGIC) ? \
97 (dp)->dp1.field : (dp)->dp2.field)
99 #define HASUSR 1
100 #define HASGRP 2
102 struct fileusage {
103 struct fileusage *fu_next;
104 u_long fu_curinodes;
105 u_long fu_curblocks;
106 u_long fu_id;
107 char fu_name[1];
108 /* actually bigger */
110 #define FUHASH 1024 /* must be power of two */
111 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
113 int aflag; /* all file systems */
114 int cflag; /* convert format to 32 or 64 bit size */
115 int gflag; /* check group quotas */
116 int uflag; /* check user quotas */
117 int vflag; /* verbose */
118 int fi; /* open disk file descriptor */
120 struct fileusage *
121 addid(u_long, int, char *, const char *);
122 void bread(ufs2_daddr_t, char *, long);
123 void freeinodebuf(void);
124 union dinode *
125 getnextinode(ino_t);
126 int getquotagid(void);
127 struct fileusage *
128 lookup(u_long, int);
129 int oneof(char *, char*[], int);
130 void printchanges(const char *, int, struct dqblk *, struct fileusage *,
131 u_long);
132 void setinodebuf(ino_t);
133 int update(const char *, struct quotafile *, int);
134 void usage(void);
137 main(int argc, char *argv[])
139 struct fstab *fs;
140 struct passwd *pw;
141 struct group *gr;
142 struct quotafile *qfu, *qfg;
143 int i, argnum, maxrun, errs, ch;
144 long done = 0;
145 char *name;
147 errs = maxrun = 0;
148 while ((ch = getopt(argc, argv, "ac:guvl:")) != -1) {
149 switch(ch) {
150 case 'a':
151 aflag++;
152 break;
153 case 'c':
154 if (cflag)
155 usage();
156 cflag = atoi(optarg);
157 break;
158 case 'g':
159 gflag++;
160 break;
161 case 'u':
162 uflag++;
163 break;
164 case 'v':
165 vflag++;
166 break;
167 case 'l':
168 maxrun = atoi(optarg);
169 break;
170 default:
171 usage();
174 argc -= optind;
175 argv += optind;
176 if ((argc == 0 && !aflag) || (argc > 0 && aflag))
177 usage();
178 if (cflag && cflag != 32 && cflag != 64)
179 usage();
180 if (!gflag && !uflag) {
181 gflag++;
182 uflag++;
184 if (gflag) {
185 setgrent();
186 while ((gr = getgrent()) != NULL)
187 (void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name,
188 NULL);
189 endgrent();
191 if (uflag) {
192 setpwent();
193 while ((pw = getpwent()) != NULL)
194 (void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name,
195 NULL);
196 endpwent();
199 * The maxrun (-l) option is now deprecated.
201 if (maxrun > 0)
202 warnx("the -l option is now deprecated");
203 if (aflag)
204 exit(checkfstab(uflag, gflag));
205 if (setfsent() == 0)
206 errx(1, "%s: can't open", FSTAB);
207 while ((fs = getfsent()) != NULL) {
208 if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
209 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
210 (name = blockcheck(fs->fs_spec))) {
211 done |= 1 << argnum;
212 qfu = NULL;
213 if (uflag)
214 qfu = quota_open(fs, USRQUOTA, O_CREAT|O_RDWR);
215 qfg = NULL;
216 if (gflag)
217 qfg = quota_open(fs, GRPQUOTA, O_CREAT|O_RDWR);
218 if (qfu == NULL && qfg == NULL)
219 continue;
220 errs += chkquota(name, qfu, qfg);
221 if (qfu)
222 quota_close(qfu);
223 if (qfg)
224 quota_close(qfg);
227 endfsent();
228 for (i = 0; i < argc; i++)
229 if ((done & (1 << i)) == 0)
230 fprintf(stderr, "%s not found in %s\n",
231 argv[i], FSTAB);
232 exit(errs);
235 void
236 usage(void)
238 (void)fprintf(stderr, "%s\n%s\n",
239 "usage: quotacheck [-guv] [-c 32 | 64] [-l maxrun] -a",
240 " quotacheck [-guv] [-c 32 | 64] filesystem ...");
241 exit(1);
245 * Possible superblock locations ordered from most to least likely.
247 static int sblock_try[] = SBLOCKSEARCH;
250 * Scan the specified file system to check quota(s) present on it.
253 chkquota(char *specname, struct quotafile *qfu, struct quotafile *qfg)
255 struct fileusage *fup;
256 union dinode *dp;
257 int cg, i, mode, errs = 0;
258 ino_t ino, inosused, userino = 0, groupino = 0;
259 dev_t dev, userdev = 0, groupdev = 0;
260 struct stat sb;
261 const char *mntpt;
262 char *cp;
264 if (qfu != NULL)
265 mntpt = quota_fsname(qfu);
266 else if (qfg != NULL)
267 mntpt = quota_fsname(qfg);
268 else
269 errx(1, "null quotafile information passed to chkquota()\n");
270 if (cflag) {
271 if (vflag && qfu != NULL)
272 printf("%s: convert user quota to %d bits\n",
273 mntpt, cflag);
274 if (qfu != NULL && quota_convert(qfu, cflag) < 0) {
275 if (errno == EBADF)
276 errx(1,
277 "%s: cannot convert an active quota file",
278 mntpt);
279 err(1, "user quota conversion to size %d failed",
280 cflag);
282 if (vflag && qfg != NULL)
283 printf("%s: convert group quota to %d bits\n",
284 mntpt, cflag);
285 if (qfg != NULL && quota_convert(qfg, cflag) < 0) {
286 if (errno == EBADF)
287 errx(1,
288 "%s: cannot convert an active quota file",
289 mntpt);
290 err(1, "group quota conversion to size %d failed",
291 cflag);
294 if ((fi = open(specname, O_RDONLY, 0)) < 0) {
295 warn("%s", specname);
296 return (1);
298 if ((stat(mntpt, &sb)) < 0) {
299 warn("%s", mntpt);
300 return (1);
302 dev = sb.st_dev;
303 if (vflag) {
304 (void)printf("*** Checking ");
305 if (qfu)
306 (void)printf("user%s", qfg ? " and " : "");
307 if (qfg)
308 (void)printf("group");
309 (void)printf(" quotas for %s (%s)\n", specname, mntpt);
311 if (qfu) {
312 if (stat(quota_qfname(qfu), &sb) == 0) {
313 userino = sb.st_ino;
314 userdev = sb.st_dev;
317 if (qfg) {
318 if (stat(quota_qfname(qfg), &sb) == 0) {
319 groupino = sb.st_ino;
320 groupdev = sb.st_dev;
323 sync();
324 dev_bsize = 1;
325 for (i = 0; sblock_try[i] != -1; i++) {
326 bread(sblock_try[i], (char *)&sblock, (long)SBLOCKSIZE);
327 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
328 (sblock.fs_magic == FS_UFS2_MAGIC &&
329 sblock.fs_sblockloc == sblock_try[i])) &&
330 sblock.fs_bsize <= MAXBSIZE &&
331 sblock.fs_bsize >= sizeof(struct fs))
332 break;
334 if (sblock_try[i] == -1) {
335 warn("Cannot find file system superblock");
336 return (1);
338 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
339 maxino = sblock.fs_ncg * sblock.fs_ipg;
340 for (cg = 0; cg < sblock.fs_ncg; cg++) {
341 ino = cg * sblock.fs_ipg;
342 setinodebuf(ino);
343 bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)(&cgblk),
344 sblock.fs_cgsize);
345 if (sblock.fs_magic == FS_UFS2_MAGIC)
346 inosused = cgblk.cg_initediblk;
347 else
348 inosused = sblock.fs_ipg;
350 * If we are using soft updates, then we can trust the
351 * cylinder group inode allocation maps to tell us which
352 * inodes are allocated. We will scan the used inode map
353 * to find the inodes that are really in use, and then
354 * read only those inodes in from disk.
356 if (sblock.fs_flags & FS_DOSOFTDEP) {
357 if (!cg_chkmagic(&cgblk))
358 errx(1, "CG %d: BAD MAGIC NUMBER\n", cg);
359 cp = &cg_inosused(&cgblk)[(inosused - 1) / CHAR_BIT];
360 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
361 if (*cp == 0)
362 continue;
363 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
364 if (*cp & i)
365 break;
366 inosused--;
368 break;
370 if (inosused <= 0)
371 continue;
373 for (i = 0; i < inosused; i++, ino++) {
374 if ((dp = getnextinode(ino)) == NULL || ino < ROOTINO ||
375 (mode = DIP(dp, di_mode) & IFMT) == 0)
376 continue;
378 * XXX: Do not account for UIDs or GIDs that appear
379 * to be negative to prevent generating 100GB+
380 * quota files.
382 if ((int)DIP(dp, di_uid) < 0 ||
383 (int)DIP(dp, di_gid) < 0) {
384 if (vflag) {
385 if (aflag)
386 (void)printf("%s: ", mntpt);
387 (void)printf("out of range UID/GID (%u/%u) ino=%ju\n",
388 DIP(dp, di_uid), DIP(dp,di_gid),
389 (uintmax_t)ino);
391 continue;
395 * Do not account for file system snapshot files
396 * or the actual quota data files to be consistent
397 * with how they are handled inside the kernel.
399 #ifdef SF_SNAPSHOT
400 if (DIP(dp, di_flags) & SF_SNAPSHOT)
401 continue;
402 #endif
403 if ((ino == userino && dev == userdev) ||
404 (ino == groupino && dev == groupdev))
405 continue;
406 if (qfg) {
407 fup = addid((u_long)DIP(dp, di_gid), GRPQUOTA,
408 (char *)0, mntpt);
409 fup->fu_curinodes++;
410 if (mode == IFREG || mode == IFDIR ||
411 mode == IFLNK)
412 fup->fu_curblocks += DIP(dp, di_blocks);
414 if (qfu) {
415 fup = addid((u_long)DIP(dp, di_uid), USRQUOTA,
416 (char *)0, mntpt);
417 fup->fu_curinodes++;
418 if (mode == IFREG || mode == IFDIR ||
419 mode == IFLNK)
420 fup->fu_curblocks += DIP(dp, di_blocks);
424 freeinodebuf();
425 if (qfu)
426 errs += update(mntpt, qfu, USRQUOTA);
427 if (qfg)
428 errs += update(mntpt, qfg, GRPQUOTA);
429 close(fi);
430 (void)fflush(stdout);
431 return (errs);
435 * Update a specified quota file.
438 update(const char *fsname, struct quotafile *qf, int type)
440 struct fileusage *fup;
441 u_long id, lastid, highid = 0;
442 struct dqblk dqbuf;
443 struct stat sb;
444 static struct dqblk zerodqbuf;
445 static struct fileusage zerofileusage;
448 * Scan the on-disk quota file and record any usage changes.
450 lastid = quota_maxid(qf);
451 for (id = 0; id <= lastid; id++) {
452 if (quota_read(qf, &dqbuf, id) < 0)
453 dqbuf = zerodqbuf;
454 if ((fup = lookup(id, type)) == NULL)
455 fup = &zerofileusage;
456 if (fup->fu_curinodes || fup->fu_curblocks ||
457 dqbuf.dqb_bsoftlimit || dqbuf.dqb_bhardlimit ||
458 dqbuf.dqb_isoftlimit || dqbuf.dqb_ihardlimit)
459 highid = id;
460 if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
461 dqbuf.dqb_curblocks == fup->fu_curblocks) {
462 fup->fu_curinodes = 0;
463 fup->fu_curblocks = 0;
464 continue;
466 printchanges(fsname, type, &dqbuf, fup, id);
467 dqbuf.dqb_curinodes = fup->fu_curinodes;
468 dqbuf.dqb_curblocks = fup->fu_curblocks;
469 (void) quota_write_usage(qf, &dqbuf, id);
470 fup->fu_curinodes = 0;
471 fup->fu_curblocks = 0;
475 * Walk the hash table looking for ids with non-zero usage
476 * that are not currently recorded in the quota file. E.g.
477 * ids that are past the end of the current file.
479 for (id = 0; id < FUHASH; id++) {
480 for (fup = fuhead[type][id]; fup != NULL; fup = fup->fu_next) {
481 if (fup->fu_id <= lastid)
482 continue;
483 if (fup->fu_curinodes == 0 && fup->fu_curblocks == 0)
484 continue;
485 bzero(&dqbuf, sizeof(struct dqblk));
486 if (fup->fu_id > highid)
487 highid = fup->fu_id;
488 printchanges(fsname, type, &dqbuf, fup, fup->fu_id);
489 dqbuf.dqb_curinodes = fup->fu_curinodes;
490 dqbuf.dqb_curblocks = fup->fu_curblocks;
491 (void) quota_write_usage(qf, &dqbuf, fup->fu_id);
492 fup->fu_curinodes = 0;
493 fup->fu_curblocks = 0;
497 * If this is old format file, then size may be smaller,
498 * so ensure that we only truncate when it will make things
499 * smaller, and not if it will grow an old format file.
501 if (highid < lastid &&
502 stat(quota_qfname(qf), &sb) == 0 &&
503 sb.st_size > (((off_t)highid + 2) * sizeof(struct dqblk)))
504 truncate(quota_qfname(qf),
505 (((off_t)highid + 2) * sizeof(struct dqblk)));
506 return (0);
510 * Check to see if target appears in list of size cnt.
513 oneof(char *target, char *list[], int cnt)
515 int i;
517 for (i = 0; i < cnt; i++)
518 if (strcmp(target, list[i]) == 0)
519 return (i);
520 return (-1);
524 * Determine the group identifier for quota files.
527 getquotagid(void)
529 struct group *gr;
531 if ((gr = getgrnam(quotagroup)) != NULL)
532 return (gr->gr_gid);
533 return (-1);
537 * Routines to manage the file usage table.
539 * Lookup an id of a specific type.
541 struct fileusage *
542 lookup(u_long id, int type)
544 struct fileusage *fup;
546 for (fup = fuhead[type][id & (FUHASH-1)]; fup != NULL; fup = fup->fu_next)
547 if (fup->fu_id == id)
548 return (fup);
549 return (NULL);
553 * Add a new file usage id if it does not already exist.
555 struct fileusage *
556 addid(u_long id, int type, char *name, const char *fsname)
558 struct fileusage *fup, **fhp;
559 int len;
561 if ((fup = lookup(id, type)) != NULL)
562 return (fup);
563 if (name)
564 len = strlen(name);
565 else
566 len = 0;
567 if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
568 errx(1, "calloc failed");
569 fhp = &fuhead[type][id & (FUHASH - 1)];
570 fup->fu_next = *fhp;
571 *fhp = fup;
572 fup->fu_id = id;
573 if (name)
574 bcopy(name, fup->fu_name, len + 1);
575 else {
576 (void)sprintf(fup->fu_name, "%lu", id);
577 if (vflag) {
578 if (aflag && fsname != NULL)
579 (void)printf("%s: ", fsname);
580 printf("unknown %cid: %lu\n",
581 type == USRQUOTA ? 'u' : 'g', id);
584 return (fup);
588 * Special purpose version of ginode used to optimize pass
589 * over all the inodes in numerical order.
591 static ino_t nextino, lastinum, lastvalidinum;
592 static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
593 static caddr_t inodebuf;
594 #define INOBUFSIZE 56*1024 /* size of buffer to read inodes */
596 union dinode *
597 getnextinode(ino_t inumber)
599 long size;
600 ufs2_daddr_t dblk;
601 union dinode *dp;
602 static caddr_t nextinop;
604 if (inumber != nextino++ || inumber > lastvalidinum)
605 errx(1, "bad inode number %ju to nextinode",
606 (uintmax_t)inumber);
607 if (inumber >= lastinum) {
608 readcnt++;
609 dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
610 if (readcnt % readpercg == 0) {
611 size = partialsize;
612 lastinum += partialcnt;
613 } else {
614 size = inobufsize;
615 lastinum += fullcnt;
618 * If bread returns an error, it will already have zeroed
619 * out the buffer, so we do not need to do so here.
621 bread(dblk, inodebuf, size);
622 nextinop = inodebuf;
624 dp = (union dinode *)nextinop;
625 if (sblock.fs_magic == FS_UFS1_MAGIC)
626 nextinop += sizeof(struct ufs1_dinode);
627 else
628 nextinop += sizeof(struct ufs2_dinode);
629 return (dp);
633 * Prepare to scan a set of inodes.
635 void
636 setinodebuf(ino_t inum)
639 if (inum % sblock.fs_ipg != 0)
640 errx(1, "bad inode number %ju to setinodebuf", (uintmax_t)inum);
641 lastvalidinum = inum + sblock.fs_ipg - 1;
642 nextino = inum;
643 lastinum = inum;
644 readcnt = 0;
645 if (inodebuf != NULL)
646 return;
647 inobufsize = blkroundup(&sblock, INOBUFSIZE);
648 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
649 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
650 readpercg = sblock.fs_ipg / fullcnt;
651 partialcnt = sblock.fs_ipg % fullcnt;
652 partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
653 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
654 if (partialcnt != 0) {
655 readpercg++;
656 } else {
657 partialcnt = fullcnt;
658 partialsize = inobufsize;
660 if ((inodebuf = malloc((unsigned)inobufsize)) == NULL)
661 errx(1, "cannot allocate space for inode buffer");
665 * Free up data structures used to scan inodes.
667 void
668 freeinodebuf(void)
671 if (inodebuf != NULL)
672 free(inodebuf);
673 inodebuf = NULL;
677 * Read specified disk blocks.
679 void
680 bread(ufs2_daddr_t bno, char *buf, long cnt)
683 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
684 read(fi, buf, cnt) != cnt)
685 errx(1, "bread failed on block %ld", (long)bno);
689 * Display updated block and i-node counts.
691 void
692 printchanges(const char *fsname, int type, struct dqblk *dp,
693 struct fileusage *fup, u_long id)
695 if (!vflag)
696 return;
697 if (aflag)
698 (void)printf("%s: ", fsname);
699 if (fup->fu_name[0] == '\0')
700 (void)printf("%-8lu fixed ", id);
701 else
702 (void)printf("%-8s fixed ", fup->fu_name);
703 switch (type) {
705 case GRPQUOTA:
706 (void)printf("(group):");
707 break;
709 case USRQUOTA:
710 (void)printf("(user): ");
711 break;
713 default:
714 (void)printf("(unknown quota type %d)", type);
715 break;
717 if (dp->dqb_curinodes != fup->fu_curinodes)
718 (void)printf("\tinodes %lu -> %lu", (u_long)dp->dqb_curinodes,
719 (u_long)fup->fu_curinodes);
720 if (dp->dqb_curblocks != fup->fu_curblocks)
721 (void)printf("\tblocks %lu -> %lu",
722 (u_long)dp->dqb_curblocks,
723 (u_long)fup->fu_curblocks);
724 (void)printf("\n");