Home | History | Annotate | Line # | Download | only in quotacheck
quotacheck.c revision 1.11
      1 /*	$NetBSD: quotacheck.c,v 1.11 1995/11/28 05:25:56 jtc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Robert Elz at The University of Melbourne.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 static char copyright[] =
     41 "@(#) Copyright (c) 1980, 1990, 1993\n\
     42 	The Regents of the University of California.  All rights reserved.\n";
     43 #endif /* not lint */
     44 
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)quotacheck.c	8.3 (Berkeley) 1/29/94";
     48 #else
     49 static char rcsid[] = "$NetBSD: quotacheck.c,v 1.11 1995/11/28 05:25:56 jtc Exp $";
     50 #endif
     51 #endif /* not lint */
     52 
     53 /*
     54  * Fix up / report on disk quotas & usage
     55  */
     56 #include <sys/param.h>
     57 #include <sys/stat.h>
     58 
     59 #include <ufs/ufs/dinode.h>
     60 #include <ufs/ufs/quota.h>
     61 #include <ufs/ffs/fs.h>
     62 
     63 #include <fcntl.h>
     64 #include <fstab.h>
     65 #include <pwd.h>
     66 #include <grp.h>
     67 #include <errno.h>
     68 #include <unistd.h>
     69 #include <stdio.h>
     70 #include <stdlib.h>
     71 #include <string.h>
     72 #include <err.h>
     73 
     74 char *qfname = QUOTAFILENAME;
     75 char *qfextension[] = INITQFNAMES;
     76 char *quotagroup = QUOTAGROUP;
     77 
     78 union {
     79 	struct	fs	sblk;
     80 	char	dummy[MAXBSIZE];
     81 } un;
     82 #define	sblock	un.sblk
     83 long dev_bsize;
     84 long maxino;
     85 
     86 struct quotaname {
     87 	long	flags;
     88 	char	grpqfname[MAXPATHLEN + 1];
     89 	char	usrqfname[MAXPATHLEN + 1];
     90 };
     91 #define	HASUSR	1
     92 #define	HASGRP	2
     93 
     94 struct fileusage {
     95 	struct	fileusage *fu_next;
     96 	u_long	fu_curinodes;
     97 	u_long	fu_curblocks;
     98 	u_long	fu_id;
     99 	char	fu_name[1];
    100 	/* actually bigger */
    101 };
    102 #define FUHASH 1024	/* must be power of two */
    103 struct fileusage *fuhead[MAXQUOTAS][FUHASH];
    104 
    105 int	aflag;			/* all file systems */
    106 int	gflag;			/* check group quotas */
    107 int	uflag;			/* check user quotas */
    108 int	vflag;			/* verbose */
    109 int	fi;			/* open disk file descriptor */
    110 u_long	highid[MAXQUOTAS];	/* highest addid()'ed identifier per type */
    111 
    112 struct fileusage *
    113 	 addid __P((u_long, int, char *));
    114 char	*blockcheck __P((char *));
    115 void	 bread __P((daddr_t, char *, long));
    116 int	 chkquota __P((char *, char *, struct quotaname *));
    117 void	 freeinodebuf __P((void));
    118 struct dinode *
    119 	 getnextinode __P((ino_t));
    120 int	 getquotagid __P((void));
    121 int	 hasquota __P((struct fstab *, int, char **));
    122 struct fileusage *
    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));
    129 
    130 int
    131 main(argc, argv)
    132 	int argc;
    133 	char *argv[];
    134 {
    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;
    140 	long done = 0;
    141 	char ch, *name;
    142 
    143 	errs = maxrun = 0;
    144 	while ((ch = getopt(argc, argv, "aguvl:")) != EOF) {
    145 		switch(ch) {
    146 		case 'a':
    147 			aflag++;
    148 			break;
    149 		case 'g':
    150 			gflag++;
    151 			break;
    152 		case 'u':
    153 			uflag++;
    154 			break;
    155 		case 'v':
    156 			vflag++;
    157 			break;
    158 		case 'l':
    159 			maxrun = atoi(optarg);
    160 			break;
    161 		default:
    162 			usage();
    163 		}
    164 	}
    165 	argc -= optind;
    166 	argv += optind;
    167 	if ((argc == 0 && !aflag) || (argc > 0 && aflag))
    168 		usage();
    169 	if (!gflag && !uflag) {
    170 		gflag++;
    171 		uflag++;
    172 	}
    173 	if (gflag) {
    174 		setgrent();
    175 		while ((gr = getgrent()) != 0)
    176 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
    177 		endgrent();
    178 	}
    179 	if (uflag) {
    180 		setpwent();
    181 		while ((pw = getpwent()) != 0)
    182 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
    183 		endpwent();
    184 	}
    185 	if (aflag)
    186 		exit(checkfstab(1, maxrun, needchk, chkquota));
    187 	if (setfsent() == 0)
    188 		err(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))) {
    194 			done |= 1 << argnum;
    195 			errs += chkquota(name, fs->fs_file, auxdata);
    196 		}
    197 	}
    198 	endfsent();
    199 	for (i = 0; i < argc; i++)
    200 		if ((done & (1 << i)) == 0)
    201 			fprintf(stderr, "%s not found in %s\n",
    202 				argv[i], FSTAB);
    203 	exit(errs);
    204 }
    205 
    206 void
    207 usage()
    208 {
    209 	(void)fprintf(stderr, "usage:\t%s\n\t%s\n",
    210 		"quotacheck -a [-guv]",
    211 		"quotacheck [-guv] filesys ...");
    212 	exit(1);
    213 }
    214 
    215 void *
    216 needchk(fs)
    217 	register struct fstab *fs;
    218 {
    219 	register struct quotaname *qnp;
    220 	char *qfnp;
    221 
    222 	if (strcmp(fs->fs_vfstype, "ffs") ||
    223 	    strcmp(fs->fs_type, FSTAB_RW))
    224 		return (NULL);
    225 	if ((qnp = malloc(sizeof(*qnp))) == NULL)
    226 		err(1, "%s", strerror(errno));
    227 	qnp->flags = 0;
    228 	if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
    229 		strcpy(qnp->grpqfname, qfnp);
    230 		qnp->flags |= HASGRP;
    231 	}
    232 	if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
    233 		strcpy(qnp->usrqfname, qfnp);
    234 		qnp->flags |= HASUSR;
    235 	}
    236 	if (qnp->flags)
    237 		return (qnp);
    238 	free(qnp);
    239 	return (NULL);
    240 }
    241 
    242 /*
    243  * Scan the specified filesystem to check quota(s) present on it.
    244  */
    245 int
    246 chkquota(fsname, mntpt, qnp)
    247 	char *fsname, *mntpt;
    248 	register struct quotaname *qnp;
    249 {
    250 	register struct fileusage *fup;
    251 	register struct dinode *dp;
    252 	int cg, i, mode, errs = 0;
    253 	ino_t ino;
    254 
    255 	if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
    256 		perror(fsname);
    257 		return (1);
    258 	}
    259 	if (vflag) {
    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);
    267 	}
    268 	sync();
    269 	dev_bsize = 1;
    270 	bread(SBOFF, (char *)&sblock, (long)SBSIZE);
    271 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
    272 	maxino = sblock.fs_ncg * sblock.fs_ipg;
    273 	resetinodebuf();
    274 	for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
    275 		for (i = 0; i < sblock.fs_ipg; i++, ino++) {
    276 			if (ino < ROOTINO)
    277 				continue;
    278 			if ((dp = getnextinode(ino)) == NULL)
    279 				continue;
    280 			if ((mode = dp->di_mode & IFMT) == 0)
    281 				continue;
    282 			if (qnp->flags & HASGRP) {
    283 				fup = addid((u_long)dp->di_gid, GRPQUOTA,
    284 				    (char *)0);
    285 				fup->fu_curinodes++;
    286 				if (mode == IFREG || mode == IFDIR ||
    287 				    mode == IFLNK)
    288 					fup->fu_curblocks += dp->di_blocks;
    289 			}
    290 			if (qnp->flags & HASUSR) {
    291 				fup = addid((u_long)dp->di_uid, USRQUOTA,
    292 				    (char *)0);
    293 				fup->fu_curinodes++;
    294 				if (mode == IFREG || mode == IFDIR ||
    295 				    mode == IFLNK)
    296 					fup->fu_curblocks += dp->di_blocks;
    297 			}
    298 		}
    299 	}
    300 	freeinodebuf();
    301 	if (qnp->flags & HASUSR)
    302 		errs += update(mntpt, qnp->usrqfname, USRQUOTA);
    303 	if (qnp->flags & HASGRP)
    304 		errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
    305 	close(fi);
    306 	return (errs);
    307 }
    308 
    309 /*
    310  * Update a specified quota file.
    311  */
    312 int
    313 update(fsname, quotafile, type)
    314 	char *fsname, *quotafile;
    315 	register int type;
    316 {
    317 	register struct fileusage *fup;
    318 	register FILE *qfi, *qfo;
    319 	register u_long id, lastid;
    320 	struct dqblk dqbuf;
    321 	static int warned = 0;
    322 	static struct dqblk zerodqbuf;
    323 	static struct fileusage zerofileusage;
    324 
    325 	if ((qfo = fopen(quotafile, "r+")) == NULL) {
    326 		if (errno == ENOENT)
    327 			qfo = fopen(quotafile, "w+");
    328 		if (qfo) {
    329 			(void) fprintf(stderr,
    330 			    "quotacheck: creating quota file %s\n", quotafile);
    331 #define	MODE	(S_IRUSR|S_IWUSR|S_IRGRP)
    332 			(void) fchown(fileno(qfo), getuid(), getquotagid());
    333 			(void) fchmod(fileno(qfo), MODE);
    334 		} else {
    335 			(void) fprintf(stderr,
    336 			    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    337 			return (1);
    338 		}
    339 	}
    340 	if ((qfi = fopen(quotafile, "r")) == NULL) {
    341 		(void) fprintf(stderr,
    342 		    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    343 		(void) fclose(qfo);
    344 		return (1);
    345 	}
    346 	if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
    347 	    errno == EOPNOTSUPP && !warned && vflag) {
    348 		warned++;
    349 		(void)printf("*** Warning: %s\n",
    350 		    "Quotas are not compiled into this kernel");
    351 	}
    352 	for (lastid = highid[type], id = 0; id <= lastid; id++) {
    353 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
    354 			dqbuf = zerodqbuf;
    355 		if ((fup = lookup(id, type)) == 0)
    356 			fup = &zerofileusage;
    357 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
    358 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
    359 			fup->fu_curinodes = 0;
    360 			fup->fu_curblocks = 0;
    361 			fseek(qfo, (long)sizeof(struct dqblk), 1);
    362 			continue;
    363 		}
    364 		if (vflag) {
    365 			if (aflag)
    366 				printf("%s: ", fsname);
    367 			printf("%-8s fixed:", fup->fu_name);
    368 			if (dqbuf.dqb_curinodes != fup->fu_curinodes)
    369 				(void)printf("\tinodes %d -> %d",
    370 					dqbuf.dqb_curinodes, fup->fu_curinodes);
    371 			if (dqbuf.dqb_curblocks != fup->fu_curblocks)
    372 				(void)printf("\tblocks %d -> %d",
    373 					dqbuf.dqb_curblocks, fup->fu_curblocks);
    374 			(void)printf("\n");
    375 		}
    376 		/*
    377 		 * Reset time limit if have a soft limit and were
    378 		 * previously under it, but are now over it.
    379 		 */
    380 		if (dqbuf.dqb_bsoftlimit &&
    381 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
    382 		    fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
    383 			dqbuf.dqb_btime = 0;
    384 		if (dqbuf.dqb_isoftlimit &&
    385 		    dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
    386 		    fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
    387 			dqbuf.dqb_itime = 0;
    388 		dqbuf.dqb_curinodes = fup->fu_curinodes;
    389 		dqbuf.dqb_curblocks = fup->fu_curblocks;
    390 		fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
    391 		(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
    392 		    (caddr_t)&dqbuf);
    393 		fup->fu_curinodes = 0;
    394 		fup->fu_curblocks = 0;
    395 	}
    396 	fclose(qfi);
    397 	fflush(qfo);
    398 	ftruncate(fileno(qfo),
    399 	    (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
    400 	fclose(qfo);
    401 	return (0);
    402 }
    403 
    404 /*
    405  * Check to see if target appears in list of size cnt.
    406  */
    407 int
    408 oneof(target, list, cnt)
    409 	register char *target, *list[];
    410 	int cnt;
    411 {
    412 	register int i;
    413 
    414 	for (i = 0; i < cnt; i++)
    415 		if (strcmp(target, list[i]) == 0)
    416 			return (i);
    417 	return (-1);
    418 }
    419 
    420 /*
    421  * Determine the group identifier for quota files.
    422  */
    423 int
    424 getquotagid()
    425 {
    426 	struct group *gr;
    427 
    428 	if (gr = getgrnam(quotagroup))
    429 		return (gr->gr_gid);
    430 	return (-1);
    431 }
    432 
    433 /*
    434  * Check to see if a particular quota is to be enabled.
    435  */
    436 int
    437 hasquota(fs, type, qfnamep)
    438 	register struct fstab *fs;
    439 	int type;
    440 	char **qfnamep;
    441 {
    442 	register char *opt;
    443 	char *cp;
    444 	static char initname, usrname[100], grpname[100];
    445 	static char buf[BUFSIZ];
    446 
    447 	if (!initname) {
    448 		(void)snprintf(usrname, sizeof(usrname),
    449 		    "%s%s", qfextension[USRQUOTA], qfname);
    450 		(void)snprintf(grpname, sizeof(grpname),
    451 		    "%s%s", qfextension[GRPQUOTA], qfname);
    452 		initname = 1;
    453 	}
    454 	strcpy(buf, fs->fs_mntops);
    455 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    456 		if (cp = strchr(opt, '='))
    457 			*cp++ = '\0';
    458 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    459 			break;
    460 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    461 			break;
    462 	}
    463 	if (!opt)
    464 		return (0);
    465 	if (cp)
    466 		*qfnamep = cp;
    467 	else {
    468 		(void)snprintf(buf, sizeof(buf),
    469 		    "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    470 		*qfnamep = buf;
    471 	}
    472 	return (1);
    473 }
    474 
    475 /*
    476  * Routines to manage the file usage table.
    477  *
    478  * Lookup an id of a specific type.
    479  */
    480 struct fileusage *
    481 lookup(id, type)
    482 	u_long id;
    483 	int type;
    484 {
    485 	register struct fileusage *fup;
    486 
    487 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
    488 		if (fup->fu_id == id)
    489 			return (fup);
    490 	return (NULL);
    491 }
    492 
    493 /*
    494  * Add a new file usage id if it does not already exist.
    495  */
    496 struct fileusage *
    497 addid(id, type, name)
    498 	u_long id;
    499 	int type;
    500 	char *name;
    501 {
    502 	struct fileusage *fup, **fhp;
    503 	int len;
    504 
    505 	if (fup = lookup(id, type))
    506 		return (fup);
    507 	if (name)
    508 		len = strlen(name);
    509 	else
    510 		len = 10;
    511 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
    512 		err(1, "%s", strerror(errno));
    513 	fhp = &fuhead[type][id & (FUHASH - 1)];
    514 	fup->fu_next = *fhp;
    515 	*fhp = fup;
    516 	fup->fu_id = id;
    517 	if (id > highid[type])
    518 		highid[type] = id;
    519 	if (name)
    520 		memcpy(fup->fu_name, name, len + 1);
    521 	else
    522 		(void)sprintf(fup->fu_name, "%u", id);
    523 	return (fup);
    524 }
    525 
    526 /*
    527  * Special purpose version of ginode used to optimize pass
    528  * over all the inodes in numerical order.
    529  */
    530 ino_t nextino, lastinum;
    531 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    532 struct dinode *inodebuf;
    533 #define	INOBUFSIZE	56*1024	/* size of buffer to read inodes */
    534 
    535 struct dinode *
    536 getnextinode(inumber)
    537 	ino_t inumber;
    538 {
    539 	long size;
    540 	daddr_t dblk;
    541 	static struct dinode *dp;
    542 
    543 	if (inumber != nextino++ || inumber > maxino)
    544 		err(1, "bad inode number %d to nextinode", inumber);
    545 	if (inumber >= lastinum) {
    546 		readcnt++;
    547 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    548 		if (readcnt % readpercg == 0) {
    549 			size = partialsize;
    550 			lastinum += partialcnt;
    551 		} else {
    552 			size = inobufsize;
    553 			lastinum += fullcnt;
    554 		}
    555 		bread(dblk, (char *)inodebuf, size);
    556 		dp = inodebuf;
    557 	}
    558 	return (dp++);
    559 }
    560 
    561 /*
    562  * Prepare to scan a set of inodes.
    563  */
    564 void
    565 resetinodebuf()
    566 {
    567 
    568 	nextino = 0;
    569 	lastinum = 0;
    570 	readcnt = 0;
    571 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    572 	fullcnt = inobufsize / sizeof(struct dinode);
    573 	readpercg = sblock.fs_ipg / fullcnt;
    574 	partialcnt = sblock.fs_ipg % fullcnt;
    575 	partialsize = partialcnt * sizeof(struct dinode);
    576 	if (partialcnt != 0) {
    577 		readpercg++;
    578 	} else {
    579 		partialcnt = fullcnt;
    580 		partialsize = inobufsize;
    581 	}
    582 	if (inodebuf == NULL &&
    583 	   (inodebuf = malloc((u_int)inobufsize)) == NULL)
    584 		err(1, "%s", strerror(errno));
    585 	while (nextino < ROOTINO)
    586 		getnextinode(nextino);
    587 }
    588 
    589 /*
    590  * Free up data structures used to scan inodes.
    591  */
    592 void
    593 freeinodebuf()
    594 {
    595 
    596 	if (inodebuf != NULL)
    597 		free(inodebuf);
    598 	inodebuf = NULL;
    599 }
    600 
    601 /*
    602  * Read specified disk blocks.
    603  */
    604 void
    605 bread(bno, buf, cnt)
    606 	daddr_t bno;
    607 	char *buf;
    608 	long cnt;
    609 {
    610 
    611 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
    612 	    read(fi, buf, cnt) != cnt)
    613 		err(1, "block %ld", bno);
    614 }
    615