Home | History | Annotate | Line # | Download | only in quotacheck
quotacheck.c revision 1.26
      1  1.26      fvdl /*	$NetBSD: quotacheck.c,v 1.26 2003/04/17 09:21:01 fvdl 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.26      fvdl __RCSID("$NetBSD: quotacheck.c,v 1.26 2003/04/17 09:21:01 fvdl 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.25      fvdl 
    111  1.25      fvdl union dinode {
    112  1.25      fvdl 	struct ufs1_dinode dp1;
    113  1.25      fvdl 	struct ufs2_dinode dp2;
    114  1.25      fvdl };
    115  1.25      fvdl #define DIP(dp, field) \
    116  1.25      fvdl 	(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
    117  1.25      fvdl 
    118  1.25      fvdl 
    119  1.13  christos static int	aflag;		/* all file systems */
    120  1.13  christos static int	gflag;		/* check group quotas */
    121  1.13  christos static int	uflag;		/* check user quotas */
    122  1.13  christos static int	vflag;		/* verbose */
    123  1.13  christos static int	fi;		/* open disk file descriptor */
    124  1.13  christos static u_long	highid[MAXQUOTAS];/* highest addid()'ed identifier per type */
    125  1.17    bouyer static int needswap;	/* FS is in swapped order */
    126  1.23    bouyer static int got_siginfo = 0; /* got a siginfo signal */
    127  1.25      fvdl static int is_ufs2;
    128  1.13  christos 
    129  1.13  christos 
    130  1.13  christos int main __P((int, char *[]));
    131  1.13  christos static void usage __P((void));
    132  1.13  christos static void *needchk __P((struct fstab *));
    133  1.14  christos static int chkquota __P((const char *, const char *, const char *, void *,
    134  1.14  christos     pid_t *));
    135  1.13  christos static int update __P((const char *, const char *, int));
    136  1.18   mycroft static int oneof __P((const char *, char *[], int));
    137  1.13  christos static int getquotagid __P((void));
    138  1.13  christos static int hasquota __P((struct fstab *, int, char **));
    139  1.13  christos static struct fileusage *lookup __P((u_long, int));
    140  1.18   mycroft static struct fileusage *addid __P((u_long, int, const char *));
    141  1.25      fvdl static union dinode *getnextinode __P((ino_t));
    142  1.25      fvdl static void setinodebuf __P((ino_t));
    143  1.13  christos static void freeinodebuf __P((void));
    144  1.13  christos static void bread __P((daddr_t, char *, long));
    145  1.23    bouyer static void infohandler __P((int sig));
    146  1.25      fvdl static void swap_dinode1(union dinode *, int);
    147  1.25      fvdl static void swap_dinode2(union dinode *, int);
    148   1.5   mycroft 
    149   1.5   mycroft int
    150   1.1       cgd main(argc, argv)
    151   1.1       cgd 	int argc;
    152   1.5   mycroft 	char *argv[];
    153   1.1       cgd {
    154  1.13  christos 	struct fstab *fs;
    155  1.13  christos 	struct passwd *pw;
    156  1.13  christos 	struct group *gr;
    157   1.5   mycroft 	struct quotaname *auxdata;
    158   1.5   mycroft 	int i, argnum, maxrun, errs;
    159   1.5   mycroft 	long done = 0;
    160  1.14  christos 	int flags = CHECK_PREEN;
    161  1.18   mycroft 	const char *name;
    162  1.12      mark 	int ch;
    163   1.1       cgd 
    164   1.5   mycroft 	errs = maxrun = 0;
    165  1.14  christos 	while ((ch = getopt(argc, argv, "aguvdl:")) != -1) {
    166   1.1       cgd 		switch(ch) {
    167   1.1       cgd 		case 'a':
    168   1.1       cgd 			aflag++;
    169   1.1       cgd 			break;
    170  1.14  christos 		case 'd':
    171  1.14  christos 			flags |= CHECK_DEBUG;
    172  1.14  christos 			break;
    173   1.1       cgd 		case 'g':
    174   1.1       cgd 			gflag++;
    175   1.1       cgd 			break;
    176   1.1       cgd 		case 'u':
    177   1.1       cgd 			uflag++;
    178   1.1       cgd 			break;
    179   1.1       cgd 		case 'v':
    180   1.1       cgd 			vflag++;
    181   1.1       cgd 			break;
    182   1.1       cgd 		case 'l':
    183   1.1       cgd 			maxrun = atoi(optarg);
    184   1.1       cgd 			break;
    185   1.1       cgd 		default:
    186   1.1       cgd 			usage();
    187   1.1       cgd 		}
    188   1.1       cgd 	}
    189   1.1       cgd 	argc -= optind;
    190   1.1       cgd 	argv += optind;
    191   1.1       cgd 	if ((argc == 0 && !aflag) || (argc > 0 && aflag))
    192   1.1       cgd 		usage();
    193   1.1       cgd 	if (!gflag && !uflag) {
    194   1.1       cgd 		gflag++;
    195   1.1       cgd 		uflag++;
    196   1.1       cgd 	}
    197  1.20       abs 
    198  1.20       abs 	/* If -a, we do not want to pay the cost of processing every
    199  1.20       abs 	 * group and password entry if there are no filesystems with quotas
    200  1.20       abs 	 */
    201  1.20       abs 	if (aflag) {
    202  1.20       abs 		i = 0;
    203  1.20       abs 		while ((fs = getfsent()) != NULL) {
    204  1.20       abs 			if (needchk(fs))
    205  1.20       abs 				i=1;
    206  1.20       abs 		}
    207  1.20       abs 		endfsent();
    208  1.20       abs 		if (!i)	/* No filesystems with quotas */
    209  1.20       abs 			exit(0);
    210  1.20       abs 	}
    211  1.20       abs 
    212   1.1       cgd 	if (gflag) {
    213   1.1       cgd 		setgrent();
    214   1.1       cgd 		while ((gr = getgrent()) != 0)
    215   1.1       cgd 			(void) addid((u_long)gr->gr_gid, GRPQUOTA, gr->gr_name);
    216   1.1       cgd 		endgrent();
    217   1.1       cgd 	}
    218   1.1       cgd 	if (uflag) {
    219   1.1       cgd 		setpwent();
    220   1.1       cgd 		while ((pw = getpwent()) != 0)
    221   1.1       cgd 			(void) addid((u_long)pw->pw_uid, USRQUOTA, pw->pw_name);
    222   1.1       cgd 		endpwent();
    223   1.1       cgd 	}
    224   1.1       cgd 	if (aflag)
    225  1.14  christos 		exit(checkfstab(flags, maxrun, needchk, chkquota));
    226   1.5   mycroft 	if (setfsent() == 0)
    227   1.6   mycroft 		err(1, "%s: can't open", FSTAB);
    228   1.1       cgd 	while ((fs = getfsent()) != NULL) {
    229   1.1       cgd 		if (((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
    230   1.1       cgd 		    (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) &&
    231   1.1       cgd 		    (auxdata = needchk(fs)) &&
    232   1.1       cgd 		    (name = blockcheck(fs->fs_spec))) {
    233   1.1       cgd 			done |= 1 << argnum;
    234  1.13  christos 			errs += chkquota(fs->fs_type, name, fs->fs_file,
    235  1.14  christos 			    auxdata, NULL);
    236   1.1       cgd 		}
    237   1.1       cgd 	}
    238   1.1       cgd 	endfsent();
    239   1.1       cgd 	for (i = 0; i < argc; i++)
    240   1.1       cgd 		if ((done & (1 << i)) == 0)
    241   1.1       cgd 			fprintf(stderr, "%s not found in %s\n",
    242   1.1       cgd 				argv[i], FSTAB);
    243   1.1       cgd 	exit(errs);
    244   1.1       cgd }
    245   1.1       cgd 
    246  1.13  christos static void
    247   1.1       cgd usage()
    248   1.1       cgd {
    249  1.21       cgd 
    250  1.13  christos 	(void)fprintf(stderr,
    251  1.21       cgd 	    "Usage:\t%s -a [-guv]\n\t%s [-guv] filesys ...\n", getprogname(),
    252  1.21       cgd 	    getprogname());
    253   1.1       cgd 	exit(1);
    254   1.1       cgd }
    255   1.1       cgd 
    256  1.13  christos static void *
    257   1.1       cgd needchk(fs)
    258  1.13  christos 	struct fstab *fs;
    259   1.1       cgd {
    260  1.13  christos 	struct quotaname *qnp;
    261   1.1       cgd 	char *qfnp;
    262   1.1       cgd 
    263  1.11       jtc 	if (strcmp(fs->fs_vfstype, "ffs") ||
    264   1.1       cgd 	    strcmp(fs->fs_type, FSTAB_RW))
    265   1.5   mycroft 		return (NULL);
    266   1.5   mycroft 	if ((qnp = malloc(sizeof(*qnp))) == NULL)
    267   1.6   mycroft 		err(1, "%s", strerror(errno));
    268   1.1       cgd 	qnp->flags = 0;
    269   1.1       cgd 	if (gflag && hasquota(fs, GRPQUOTA, &qfnp)) {
    270   1.1       cgd 		strcpy(qnp->grpqfname, qfnp);
    271   1.1       cgd 		qnp->flags |= HASGRP;
    272   1.1       cgd 	}
    273   1.1       cgd 	if (uflag && hasquota(fs, USRQUOTA, &qfnp)) {
    274   1.1       cgd 		strcpy(qnp->usrqfname, qfnp);
    275   1.1       cgd 		qnp->flags |= HASUSR;
    276   1.1       cgd 	}
    277   1.1       cgd 	if (qnp->flags)
    278   1.5   mycroft 		return (qnp);
    279   1.5   mycroft 	free(qnp);
    280   1.5   mycroft 	return (NULL);
    281   1.1       cgd }
    282   1.1       cgd 
    283  1.25      fvdl off_t sblock_try[] = SBLOCKSEARCH;
    284  1.25      fvdl 
    285   1.1       cgd /*
    286   1.1       cgd  * Scan the specified filesystem to check quota(s) present on it.
    287   1.1       cgd  */
    288  1.13  christos static int
    289  1.14  christos chkquota(type, fsname, mntpt, v, pid)
    290  1.13  christos 	const char *type, *fsname, *mntpt;
    291  1.13  christos 	void *v;
    292  1.14  christos 	pid_t *pid;
    293  1.13  christos {
    294  1.13  christos 	struct quotaname *qnp = v;
    295  1.13  christos 	struct fileusage *fup;
    296  1.25      fvdl 	union dinode *dp;
    297  1.25      fvdl 	int cg, i, mode, errs = 0, inosused;
    298   1.1       cgd 	ino_t ino;
    299  1.25      fvdl 	struct cg *cgp;
    300   1.1       cgd 
    301  1.14  christos 	if (pid != NULL) {
    302  1.14  christos 		switch ((*pid = fork())) {
    303  1.14  christos 		default:
    304  1.14  christos 			break;
    305  1.14  christos 		case 0:
    306  1.14  christos 			return 0;
    307  1.14  christos 		case -1:
    308  1.14  christos 			err(1, "Cannot fork");
    309  1.14  christos 		}
    310  1.14  christos 	}
    311  1.14  christos 
    312   1.5   mycroft 	if ((fi = open(fsname, O_RDONLY, 0)) < 0) {
    313  1.14  christos 		warn("Cannot open %s", fsname);
    314  1.25      fvdl 		return 1;
    315   1.1       cgd 	}
    316   1.1       cgd 	if (vflag) {
    317   1.5   mycroft 		(void)printf("*** Checking ");
    318   1.1       cgd 		if (qnp->flags & HASUSR)
    319   1.5   mycroft 			(void)printf("%s%s", qfextension[USRQUOTA],
    320   1.1       cgd 			    (qnp->flags & HASGRP) ? " and " : "");
    321   1.1       cgd 		if (qnp->flags & HASGRP)
    322   1.5   mycroft 			(void)printf("%s", qfextension[GRPQUOTA]);
    323   1.5   mycroft 		(void)printf(" quotas for %s (%s)\n", fsname, mntpt);
    324   1.1       cgd 	}
    325  1.23    bouyer 	signal(SIGINFO, infohandler);
    326   1.1       cgd 	sync();
    327   1.6   mycroft 	dev_bsize = 1;
    328  1.25      fvdl 
    329  1.25      fvdl 	cgp = malloc(sblock.fs_cgsize);
    330  1.25      fvdl 	if (cgp == NULL) {
    331  1.25      fvdl 		warn("%s: can't allocate %d bytes of cg space", fsname,
    332  1.25      fvdl 		    sblock.fs_cgsize);
    333  1.25      fvdl 		return 1;
    334  1.25      fvdl 	}
    335  1.25      fvdl 
    336  1.25      fvdl 	for (i = 0; sblock_try[i] != -1; i++) {
    337  1.25      fvdl 		bread(sblock_try[i], (char *)&sblock, SBLOCKSIZE);
    338  1.25      fvdl 		switch (sblock.fs_magic) {
    339  1.25      fvdl 		case FS_UFS2_MAGIC:
    340  1.25      fvdl 			is_ufs2 = 1;
    341  1.25      fvdl 			/*FALLTHROUGH*/
    342  1.25      fvdl 		case FS_UFS1_MAGIC:
    343  1.25      fvdl 			goto found;
    344  1.25      fvdl 		case FS_UFS2_MAGIC_SWAPPED:
    345  1.25      fvdl 			is_ufs2 = 1;
    346  1.25      fvdl 			/*FALLTHROUGH*/
    347  1.25      fvdl 		case FS_UFS1_MAGIC_SWAPPED:
    348  1.17    bouyer 			needswap = 1;
    349  1.25      fvdl 			goto found;
    350  1.25      fvdl 		default:
    351  1.25      fvdl 			continue;
    352  1.25      fvdl 		}
    353  1.19      ross 	}
    354  1.25      fvdl 	warnx("%s: superblock not found", fsname);
    355  1.25      fvdl 	free(cgp);
    356  1.25      fvdl 	return 1;
    357  1.25      fvdl found:
    358  1.25      fvdl 	if (needswap)
    359  1.25      fvdl 		ffs_sb_swap(&sblock, &sblock);
    360   1.1       cgd 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
    361   1.1       cgd 	maxino = sblock.fs_ncg * sblock.fs_ipg;
    362   1.1       cgd 	for (ino = 0, cg = 0; cg < sblock.fs_ncg; cg++) {
    363  1.25      fvdl 		setinodebuf(cg * sblock.fs_ipg);
    364  1.25      fvdl 		if (sblock.fs_magic == FS_UFS2_MAGIC) {
    365  1.25      fvdl 			bread(fsbtodb(&sblock, cgtod(&sblock, cg)), (char *)cgp,
    366  1.25      fvdl 			    sblock.fs_cgsize);
    367  1.25      fvdl 			if (needswap)
    368  1.25      fvdl 				ffs_cg_swap(cgp, cgp, &sblock);
    369  1.25      fvdl 			inosused = cgp->cg_initediblk;
    370  1.25      fvdl 		} else
    371  1.25      fvdl 			inosused = sblock.fs_ipg;
    372  1.25      fvdl 		for (i = 0; i < inosused; i++, ino++) {
    373  1.23    bouyer 			if (got_siginfo) {
    374  1.23    bouyer 				fprintf(stderr,
    375  1.23    bouyer 				    "%s: cyl group %d of %d (%d%%)\n",
    376  1.23    bouyer 				    fsname, cg, sblock.fs_ncg,
    377  1.23    bouyer 				    cg * 100 / sblock.fs_ncg);
    378  1.23    bouyer 				got_siginfo = 0;
    379  1.23    bouyer 			}
    380   1.1       cgd 			if (ino < ROOTINO)
    381   1.1       cgd 				continue;
    382   1.1       cgd 			if ((dp = getnextinode(ino)) == NULL)
    383   1.1       cgd 				continue;
    384  1.25      fvdl 			if ((mode = DIP(dp, mode) & IFMT) == 0)
    385   1.1       cgd 				continue;
    386   1.1       cgd 			if (qnp->flags & HASGRP) {
    387  1.25      fvdl 				fup = addid((u_long)DIP(dp, gid), GRPQUOTA,
    388   1.1       cgd 				    (char *)0);
    389   1.1       cgd 				fup->fu_curinodes++;
    390   1.1       cgd 				if (mode == IFREG || mode == IFDIR ||
    391   1.1       cgd 				    mode == IFLNK)
    392  1.25      fvdl 					fup->fu_curblocks += DIP(dp, blocks);
    393   1.1       cgd 			}
    394   1.1       cgd 			if (qnp->flags & HASUSR) {
    395  1.25      fvdl 				fup = addid((u_long)DIP(dp, uid), USRQUOTA,
    396   1.1       cgd 				    (char *)0);
    397   1.1       cgd 				fup->fu_curinodes++;
    398   1.1       cgd 				if (mode == IFREG || mode == IFDIR ||
    399   1.1       cgd 				    mode == IFLNK)
    400  1.25      fvdl 					fup->fu_curblocks += DIP(dp, blocks);
    401   1.1       cgd 			}
    402   1.1       cgd 		}
    403   1.1       cgd 	}
    404   1.1       cgd 	freeinodebuf();
    405  1.25      fvdl 	free(cgp);
    406   1.1       cgd 	if (qnp->flags & HASUSR)
    407   1.1       cgd 		errs += update(mntpt, qnp->usrqfname, USRQUOTA);
    408   1.1       cgd 	if (qnp->flags & HASGRP)
    409   1.1       cgd 		errs += update(mntpt, qnp->grpqfname, GRPQUOTA);
    410   1.1       cgd 	close(fi);
    411  1.25      fvdl 	return errs;
    412   1.1       cgd }
    413   1.1       cgd 
    414   1.1       cgd /*
    415   1.1       cgd  * Update a specified quota file.
    416   1.1       cgd  */
    417  1.13  christos static int
    418   1.1       cgd update(fsname, quotafile, type)
    419  1.13  christos 	const char *fsname, *quotafile;
    420  1.13  christos 	int type;
    421   1.1       cgd {
    422  1.13  christos 	struct fileusage *fup;
    423  1.13  christos 	FILE *qfi, *qfo;
    424  1.13  christos 	u_long id, lastid;
    425   1.1       cgd 	struct dqblk dqbuf;
    426   1.1       cgd 	static int warned = 0;
    427   1.1       cgd 	static struct dqblk zerodqbuf;
    428   1.1       cgd 	static struct fileusage zerofileusage;
    429   1.1       cgd 
    430   1.1       cgd 	if ((qfo = fopen(quotafile, "r+")) == NULL) {
    431   1.1       cgd 		if (errno == ENOENT)
    432   1.1       cgd 			qfo = fopen(quotafile, "w+");
    433   1.1       cgd 		if (qfo) {
    434   1.1       cgd 			(void) fprintf(stderr,
    435   1.1       cgd 			    "quotacheck: creating quota file %s\n", quotafile);
    436   1.1       cgd #define	MODE	(S_IRUSR|S_IWUSR|S_IRGRP)
    437   1.1       cgd 			(void) fchown(fileno(qfo), getuid(), getquotagid());
    438   1.1       cgd 			(void) fchmod(fileno(qfo), MODE);
    439   1.1       cgd 		} else {
    440   1.1       cgd 			(void) fprintf(stderr,
    441   1.1       cgd 			    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    442   1.1       cgd 			return (1);
    443   1.1       cgd 		}
    444   1.1       cgd 	}
    445   1.1       cgd 	if ((qfi = fopen(quotafile, "r")) == NULL) {
    446   1.1       cgd 		(void) fprintf(stderr,
    447   1.1       cgd 		    "quotacheck: %s: %s\n", quotafile, strerror(errno));
    448   1.1       cgd 		(void) fclose(qfo);
    449   1.1       cgd 		return (1);
    450   1.1       cgd 	}
    451   1.1       cgd 	if (quotactl(fsname, QCMD(Q_SYNC, type), (u_long)0, (caddr_t)0) < 0 &&
    452   1.1       cgd 	    errno == EOPNOTSUPP && !warned && vflag) {
    453   1.1       cgd 		warned++;
    454   1.5   mycroft 		(void)printf("*** Warning: %s\n",
    455   1.1       cgd 		    "Quotas are not compiled into this kernel");
    456   1.1       cgd 	}
    457   1.1       cgd 	for (lastid = highid[type], id = 0; id <= lastid; id++) {
    458   1.1       cgd 		if (fread((char *)&dqbuf, sizeof(struct dqblk), 1, qfi) == 0)
    459   1.1       cgd 			dqbuf = zerodqbuf;
    460   1.1       cgd 		if ((fup = lookup(id, type)) == 0)
    461   1.1       cgd 			fup = &zerofileusage;
    462   1.1       cgd 		if (dqbuf.dqb_curinodes == fup->fu_curinodes &&
    463   1.1       cgd 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
    464   1.1       cgd 			fup->fu_curinodes = 0;
    465   1.1       cgd 			fup->fu_curblocks = 0;
    466  1.13  christos 			(void) fseek(qfo, (long)sizeof(struct dqblk), 1);
    467   1.1       cgd 			continue;
    468   1.1       cgd 		}
    469   1.1       cgd 		if (vflag) {
    470   1.1       cgd 			if (aflag)
    471   1.1       cgd 				printf("%s: ", fsname);
    472   1.1       cgd 			printf("%-8s fixed:", fup->fu_name);
    473   1.1       cgd 			if (dqbuf.dqb_curinodes != fup->fu_curinodes)
    474  1.13  christos 				(void)printf("\tinodes %d -> %ld",
    475   1.1       cgd 					dqbuf.dqb_curinodes, fup->fu_curinodes);
    476   1.1       cgd 			if (dqbuf.dqb_curblocks != fup->fu_curblocks)
    477  1.13  christos 				(void)printf("\tblocks %d -> %ld",
    478   1.1       cgd 					dqbuf.dqb_curblocks, fup->fu_curblocks);
    479   1.5   mycroft 			(void)printf("\n");
    480   1.1       cgd 		}
    481   1.1       cgd 		/*
    482   1.1       cgd 		 * Reset time limit if have a soft limit and were
    483   1.1       cgd 		 * previously under it, but are now over it.
    484   1.1       cgd 		 */
    485   1.1       cgd 		if (dqbuf.dqb_bsoftlimit &&
    486   1.1       cgd 		    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
    487   1.1       cgd 		    fup->fu_curblocks >= dqbuf.dqb_bsoftlimit)
    488   1.1       cgd 			dqbuf.dqb_btime = 0;
    489   1.1       cgd 		if (dqbuf.dqb_isoftlimit &&
    490   1.1       cgd 		    dqbuf.dqb_curblocks < dqbuf.dqb_isoftlimit &&
    491   1.1       cgd 		    fup->fu_curblocks >= dqbuf.dqb_isoftlimit)
    492   1.1       cgd 			dqbuf.dqb_itime = 0;
    493   1.1       cgd 		dqbuf.dqb_curinodes = fup->fu_curinodes;
    494   1.1       cgd 		dqbuf.dqb_curblocks = fup->fu_curblocks;
    495  1.13  christos 		(void) fwrite((char *)&dqbuf, sizeof(struct dqblk), 1, qfo);
    496   1.1       cgd 		(void) quotactl(fsname, QCMD(Q_SETUSE, type), id,
    497   1.1       cgd 		    (caddr_t)&dqbuf);
    498   1.1       cgd 		fup->fu_curinodes = 0;
    499   1.1       cgd 		fup->fu_curblocks = 0;
    500   1.1       cgd 	}
    501  1.13  christos 	(void) fclose(qfi);
    502  1.13  christos 	(void) fflush(qfo);
    503  1.13  christos 	(void) ftruncate(fileno(qfo),
    504   1.1       cgd 	    (off_t)((highid[type] + 1) * sizeof(struct dqblk)));
    505  1.13  christos 	(void) fclose(qfo);
    506   1.1       cgd 	return (0);
    507   1.1       cgd }
    508   1.1       cgd 
    509   1.1       cgd /*
    510   1.1       cgd  * Check to see if target appears in list of size cnt.
    511   1.1       cgd  */
    512  1.13  christos static int
    513   1.1       cgd oneof(target, list, cnt)
    514  1.18   mycroft 	const char *target;
    515  1.18   mycroft 	char *list[];
    516   1.1       cgd 	int cnt;
    517   1.1       cgd {
    518  1.13  christos 	int i;
    519   1.1       cgd 
    520   1.1       cgd 	for (i = 0; i < cnt; i++)
    521   1.1       cgd 		if (strcmp(target, list[i]) == 0)
    522   1.1       cgd 			return (i);
    523   1.1       cgd 	return (-1);
    524   1.1       cgd }
    525   1.1       cgd 
    526   1.1       cgd /*
    527   1.1       cgd  * Determine the group identifier for quota files.
    528   1.1       cgd  */
    529  1.13  christos static int
    530   1.1       cgd getquotagid()
    531   1.1       cgd {
    532   1.1       cgd 	struct group *gr;
    533   1.1       cgd 
    534  1.13  christos 	if ((gr = getgrnam(quotagroup)) != NULL)
    535   1.1       cgd 		return (gr->gr_gid);
    536   1.1       cgd 	return (-1);
    537   1.1       cgd }
    538   1.1       cgd 
    539   1.1       cgd /*
    540   1.1       cgd  * Check to see if a particular quota is to be enabled.
    541   1.1       cgd  */
    542  1.13  christos static int
    543   1.1       cgd hasquota(fs, type, qfnamep)
    544  1.13  christos 	struct fstab *fs;
    545   1.1       cgd 	int type;
    546   1.1       cgd 	char **qfnamep;
    547   1.1       cgd {
    548  1.13  christos 	char *opt;
    549  1.16  christos 	char *cp = NULL;
    550   1.1       cgd 	static char initname, usrname[100], grpname[100];
    551   1.1       cgd 	static char buf[BUFSIZ];
    552   1.1       cgd 
    553   1.1       cgd 	if (!initname) {
    554   1.5   mycroft 		(void)snprintf(usrname, sizeof(usrname),
    555   1.5   mycroft 		    "%s%s", qfextension[USRQUOTA], qfname);
    556   1.5   mycroft 		(void)snprintf(grpname, sizeof(grpname),
    557   1.5   mycroft 		    "%s%s", qfextension[GRPQUOTA], qfname);
    558   1.1       cgd 		initname = 1;
    559   1.1       cgd 	}
    560  1.13  christos 	(void) strcpy(buf, fs->fs_mntops);
    561   1.1       cgd 	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
    562  1.13  christos 		if ((cp = strchr(opt, '=')) != NULL)
    563   1.1       cgd 			*cp++ = '\0';
    564   1.1       cgd 		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
    565   1.1       cgd 			break;
    566   1.1       cgd 		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
    567   1.1       cgd 			break;
    568   1.1       cgd 	}
    569   1.1       cgd 	if (!opt)
    570   1.1       cgd 		return (0);
    571   1.5   mycroft 	if (cp)
    572   1.1       cgd 		*qfnamep = cp;
    573   1.5   mycroft 	else {
    574   1.5   mycroft 		(void)snprintf(buf, sizeof(buf),
    575   1.5   mycroft 		    "%s/%s.%s", fs->fs_file, qfname, qfextension[type]);
    576   1.5   mycroft 		*qfnamep = buf;
    577   1.1       cgd 	}
    578   1.1       cgd 	return (1);
    579   1.1       cgd }
    580   1.1       cgd 
    581   1.1       cgd /*
    582   1.1       cgd  * Routines to manage the file usage table.
    583   1.1       cgd  *
    584   1.1       cgd  * Lookup an id of a specific type.
    585   1.1       cgd  */
    586  1.13  christos static struct fileusage *
    587   1.1       cgd lookup(id, type)
    588   1.1       cgd 	u_long id;
    589   1.1       cgd 	int type;
    590   1.1       cgd {
    591  1.13  christos 	struct fileusage *fup;
    592   1.1       cgd 
    593   1.1       cgd 	for (fup = fuhead[type][id & (FUHASH-1)]; fup != 0; fup = fup->fu_next)
    594   1.1       cgd 		if (fup->fu_id == id)
    595   1.1       cgd 			return (fup);
    596   1.5   mycroft 	return (NULL);
    597   1.1       cgd }
    598   1.1       cgd 
    599   1.1       cgd /*
    600   1.1       cgd  * Add a new file usage id if it does not already exist.
    601   1.1       cgd  */
    602  1.13  christos static struct fileusage *
    603   1.1       cgd addid(id, type, name)
    604   1.1       cgd 	u_long id;
    605   1.1       cgd 	int type;
    606  1.18   mycroft 	const char *name;
    607   1.1       cgd {
    608   1.1       cgd 	struct fileusage *fup, **fhp;
    609   1.1       cgd 	int len;
    610   1.1       cgd 
    611  1.13  christos 	if ((fup = lookup(id, type)) != NULL)
    612   1.1       cgd 		return (fup);
    613   1.1       cgd 	if (name)
    614   1.1       cgd 		len = strlen(name);
    615   1.1       cgd 	else
    616   1.1       cgd 		len = 10;
    617   1.5   mycroft 	if ((fup = calloc(1, sizeof(*fup) + len)) == NULL)
    618   1.6   mycroft 		err(1, "%s", strerror(errno));
    619   1.1       cgd 	fhp = &fuhead[type][id & (FUHASH - 1)];
    620   1.1       cgd 	fup->fu_next = *fhp;
    621   1.1       cgd 	*fhp = fup;
    622   1.1       cgd 	fup->fu_id = id;
    623   1.1       cgd 	if (id > highid[type])
    624   1.1       cgd 		highid[type] = id;
    625   1.5   mycroft 	if (name)
    626  1.15     lukem 		memmove(fup->fu_name, name, len + 1);
    627   1.5   mycroft 	else
    628  1.13  christos 		(void)sprintf(fup->fu_name, "%lu", id);
    629   1.1       cgd 	return (fup);
    630   1.1       cgd }
    631   1.1       cgd 
    632   1.1       cgd /*
    633  1.25      fvdl  * Special purpose version of ginode used to optimize first pass
    634   1.1       cgd  * over all the inodes in numerical order.
    635   1.1       cgd  */
    636  1.25      fvdl static ino_t nextino, lastinum, lastvalidinum;
    637  1.13  christos static long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    638  1.25      fvdl static union dinode *inodebuf;
    639  1.25      fvdl #define INOBUFSIZE	56*1024	/* size of buffer to read inodes */
    640   1.1       cgd 
    641  1.25      fvdl union dinode *
    642   1.1       cgd getnextinode(inumber)
    643   1.1       cgd 	ino_t inumber;
    644   1.1       cgd {
    645   1.1       cgd 	long size;
    646   1.1       cgd 	daddr_t dblk;
    647  1.25      fvdl 	static union dinode *dp;
    648  1.25      fvdl 	union dinode *ret;
    649  1.25      fvdl 
    650  1.25      fvdl 	if (inumber != nextino++ || inumber > lastvalidinum) {
    651  1.25      fvdl 		errx(1, "bad inode number %d to nextinode", inumber);
    652  1.25      fvdl 	}
    653   1.1       cgd 
    654   1.1       cgd 	if (inumber >= lastinum) {
    655   1.1       cgd 		readcnt++;
    656   1.5   mycroft 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    657   1.1       cgd 		if (readcnt % readpercg == 0) {
    658   1.1       cgd 			size = partialsize;
    659   1.1       cgd 			lastinum += partialcnt;
    660   1.1       cgd 		} else {
    661   1.1       cgd 			size = inobufsize;
    662   1.1       cgd 			lastinum += fullcnt;
    663   1.1       cgd 		}
    664  1.25      fvdl 		(void)bread(dblk, (caddr_t)inodebuf, size);
    665  1.25      fvdl 		if (needswap) {
    666  1.25      fvdl 			if (is_ufs2)
    667  1.25      fvdl 				swap_dinode2(inodebuf, lastinum - inumber);
    668  1.25      fvdl 			else
    669  1.25      fvdl 				swap_dinode1(inodebuf, lastinum - inumber);
    670  1.25      fvdl 		}
    671  1.25      fvdl 		dp = (union dinode *)inodebuf;
    672   1.1       cgd 	}
    673  1.25      fvdl 	ret = dp;
    674  1.25      fvdl 	dp = (union dinode *)
    675  1.25      fvdl 	    ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
    676  1.25      fvdl 	return ret;
    677   1.1       cgd }
    678   1.1       cgd 
    679  1.25      fvdl void
    680  1.25      fvdl setinodebuf(inum)
    681  1.25      fvdl 	ino_t inum;
    682   1.1       cgd {
    683   1.1       cgd 
    684  1.25      fvdl 	if (inum % sblock.fs_ipg != 0)
    685  1.25      fvdl 		errx(1, "bad inode number %d to setinodebuf", inum);
    686  1.25      fvdl 
    687  1.25      fvdl 	lastvalidinum = inum + sblock.fs_ipg - 1;
    688  1.25      fvdl 	nextino = inum;
    689  1.25      fvdl 	lastinum = inum;
    690   1.1       cgd 	readcnt = 0;
    691  1.25      fvdl 	if (inodebuf != NULL)
    692  1.25      fvdl 		return;
    693   1.1       cgd 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    694  1.25      fvdl 	fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    695   1.1       cgd 	readpercg = sblock.fs_ipg / fullcnt;
    696   1.1       cgd 	partialcnt = sblock.fs_ipg % fullcnt;
    697  1.25      fvdl 	partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    698   1.1       cgd 	if (partialcnt != 0) {
    699   1.1       cgd 		readpercg++;
    700   1.1       cgd 	} else {
    701   1.1       cgd 		partialcnt = fullcnt;
    702   1.1       cgd 		partialsize = inobufsize;
    703   1.1       cgd 	}
    704   1.1       cgd 	if (inodebuf == NULL &&
    705  1.25      fvdl 	    (inodebuf = malloc((unsigned)inobufsize)) == NULL)
    706  1.25      fvdl 		errx(1, "Cannot allocate space for inode buffer");
    707  1.26      fvdl 	while (nextino < ROOTINO)
    708  1.26      fvdl 		getnextinode(nextino);
    709   1.1       cgd }
    710   1.1       cgd 
    711  1.25      fvdl void
    712   1.1       cgd freeinodebuf()
    713   1.1       cgd {
    714   1.1       cgd 
    715   1.1       cgd 	if (inodebuf != NULL)
    716  1.25      fvdl 		free((char *)inodebuf);
    717   1.1       cgd 	inodebuf = NULL;
    718  1.25      fvdl }
    719  1.25      fvdl 
    720  1.25      fvdl 
    721  1.25      fvdl static void
    722  1.25      fvdl swap_dinode1(union dinode *dp, int n)
    723  1.25      fvdl {
    724  1.25      fvdl 	int i;
    725  1.25      fvdl 	struct ufs1_dinode *dp1;
    726  1.25      fvdl 
    727  1.25      fvdl 	dp1 = (struct ufs1_dinode *)&dp->dp1;
    728  1.25      fvdl 	for (i = 0; i < n; i++, dp1++)
    729  1.25      fvdl 		ffs_dinode1_swap(dp1, dp1);
    730  1.25      fvdl }
    731  1.25      fvdl 
    732  1.25      fvdl static void
    733  1.25      fvdl swap_dinode2(union dinode *dp, int n)
    734  1.25      fvdl {
    735  1.25      fvdl 	int i;
    736  1.25      fvdl 	struct ufs2_dinode *dp2;
    737  1.25      fvdl 
    738  1.25      fvdl 	dp2 = (struct ufs2_dinode *)&dp->dp2;
    739  1.25      fvdl 	for (i = 0; i < n; i++, dp2++)
    740  1.25      fvdl 		ffs_dinode2_swap(dp2, dp2);
    741   1.1       cgd }
    742   1.1       cgd 
    743   1.1       cgd /*
    744   1.1       cgd  * Read specified disk blocks.
    745   1.1       cgd  */
    746  1.13  christos static void
    747   1.1       cgd bread(bno, buf, cnt)
    748   1.1       cgd 	daddr_t bno;
    749   1.1       cgd 	char *buf;
    750   1.1       cgd 	long cnt;
    751   1.1       cgd {
    752   1.1       cgd 
    753   1.5   mycroft 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0 ||
    754   1.5   mycroft 	    read(fi, buf, cnt) != cnt)
    755  1.24      fvdl 		err(1, "block %lld", (long long)bno);
    756   1.1       cgd }
    757  1.23    bouyer 
    758  1.23    bouyer void
    759  1.23    bouyer infohandler(int sig)
    760  1.23    bouyer {
    761  1.23    bouyer 	got_siginfo = 1;
    762  1.23    bouyer }
    763  1.23    bouyer 
    764