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