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