Home | History | Annotate | Line # | Download | only in quot
quot.c revision 1.11
      1 /*	$NetBSD: quot.c,v 1.11 1997/10/17 12:36:36 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1991, 1994 Wolfgang Solfrank.
      5  * Copyright (C) 1991, 1994 TooLs GmbH.
      6  * All rights reserved.
      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 TooLs GmbH.
     19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: quot.c,v 1.11 1997/10/17 12:36:36 lukem Exp $");
     37 #endif /* not lint */
     38 
     39 #include <sys/param.h>
     40 #include <sys/mount.h>
     41 #include <sys/time.h>
     42 #include <ufs/ffs/fs.h>
     43 #include <ufs/ufs/quota.h>
     44 #include <ufs/ufs/inode.h>
     45 
     46 #include <errno.h>
     47 #include <fcntl.h>
     48 #include <pwd.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <unistd.h>
     53 
     54 /* some flags of what to do: */
     55 static char estimate;
     56 static char count;
     57 static char unused;
     58 static void (*func) __P((int, struct fs *, char *));
     59 static long blocksize;
     60 static char *header;
     61 static int headerlen;
     62 
     63 /*
     64  * Original BSD quot doesn't round to number of frags/blocks,
     65  * doesn't account for indirection blocks and gets it totally
     66  * wrong if the	size is a multiple of the blocksize.
     67  * The new code always counts the number of DEV_BSIZE byte blocks
     68  * instead of the number of kilobytes and converts them	to
     69  * kByte when done (on request).
     70  */
     71 #ifdef	COMPAT
     72 #define	SIZE(n)	(n)
     73 #else
     74 #define	SIZE(n)	howmany((n) * DEV_BSIZE, blocksize)
     75 #endif
     76 
     77 #define	INOCNT(fs)	((fs)->fs_ipg)
     78 #define	INOSZ(fs)	(sizeof(struct dinode) * INOCNT(fs))
     79 
     80 static	int		cmpusers __P((const void *, const void *));
     81 static	void		dofsizes __P((int, struct fs *, char *));
     82 static	void		donames __P((int, struct fs *, char *));
     83 static	void		douser __P((int, struct fs *, char *));
     84 static	struct dinode  *get_inode __P((int, struct fs*, ino_t));
     85 static	void		ffs_oldfscompat __P((struct fs *));
     86 static	void		initfsizes __P((void));
     87 static	void		inituser __P((void));
     88 static	int		isfree __P((struct dinode *));
     89 	int		main __P((int, char **));
     90 	void		quot __P((char *, char *));
     91 static	void		usage __P((void));
     92 static	struct user    *user __P((uid_t));
     93 static	void		uses __P((uid_t, daddr_t, time_t));
     94 static	void		usrrehash __P((void));
     95 static	int		virtualblocks __P((struct fs *, struct dinode *));
     96 
     97 
     98 static struct dinode *
     99 get_inode(fd, super, ino)
    100 	int fd;
    101 	struct fs *super;
    102 	ino_t ino;
    103 {
    104 	static struct dinode *ip;
    105 	static ino_t last;
    106 
    107 	if (fd < 0) {		/* flush cache */
    108 		if (ip) {
    109 			free(ip);
    110 			ip = 0;
    111 		}
    112 		return 0;
    113 	}
    114 
    115 	if (!ip || ino < last || ino >= last + INOCNT(super)) {
    116 		if (!ip
    117 		    && !(ip = (struct dinode *)malloc(INOSZ(super)))) {
    118 			perror("allocate inodes");
    119 			exit(1);
    120 		}
    121 		last = (ino / INOCNT(super)) * INOCNT(super);
    122 		if (lseek(fd,
    123 		    (off_t)ino_to_fsba(super, last) << super->fs_fshift,
    124 		    0) < 0 ||
    125 		    read(fd, ip, INOSZ(super)) != INOSZ(super)) {
    126 			perror("read inodes");
    127 			exit(1);
    128 		}
    129 	}
    130 
    131 	return ip + ino % INOCNT(super);
    132 }
    133 
    134 #ifdef	COMPAT
    135 #define	actualblocks(super, ip)	((ip)->di_blocks / 2)
    136 #else
    137 #define	actualblocks(super, ip)	((ip)->di_blocks)
    138 #endif
    139 
    140 static int
    141 virtualblocks(super, ip)
    142 	struct fs *super;
    143 	struct dinode *ip;
    144 {
    145 	register off_t nblk, sz;
    146 
    147 	sz = ip->di_size;
    148 #ifdef	COMPAT
    149 	if (lblkno(super, sz) >= NDADDR) {
    150 		nblk = blkroundup(super, sz);
    151 		if (sz == nblk)
    152 			nblk += super->fs_bsize;
    153 	}
    154 
    155 	return sz / 1024;
    156 #else	/* COMPAT */
    157 
    158 	if (lblkno(super, sz) >= NDADDR) {
    159 		nblk = blkroundup(super, sz);
    160 		sz = lblkno(super, nblk);
    161 		sz = howmany(sz - NDADDR, NINDIR(super));
    162 		while (sz > 0) {
    163 			nblk += sz * super->fs_bsize;
    164 			/* One block on this level is in the inode itself */
    165 			sz = howmany(sz - 1, NINDIR(super));
    166 		}
    167 	} else
    168 		nblk = fragroundup(super, sz);
    169 
    170 	return nblk / DEV_BSIZE;
    171 #endif	/* COMPAT */
    172 }
    173 
    174 static int
    175 isfree(ip)
    176 	struct dinode *ip;
    177 {
    178 #ifdef	COMPAT
    179 	return (ip->di_mode&IFMT) == 0;
    180 #else	/* COMPAT */
    181 	switch (ip->di_mode&IFMT) {
    182 	case IFIFO:
    183 	case IFLNK:		/* should check FASTSYMLINK? */
    184 	case IFDIR:
    185 	case IFREG:
    186 		return 0;
    187 	default:
    188 		return 1;
    189 	}
    190 #endif
    191 }
    192 
    193 static struct user {
    194 	uid_t uid;
    195 	char *name;
    196 	daddr_t space;
    197 	long count;
    198 	daddr_t spc30;
    199 	daddr_t spc60;
    200 	daddr_t spc90;
    201 } *users;
    202 static int nusers;
    203 
    204 static void
    205 inituser()
    206 {
    207 	register i;
    208 	register struct user *usr;
    209 
    210 	if (!nusers) {
    211 		nusers = 8;
    212 		if (!(users =
    213 		    (struct user *)calloc(nusers, sizeof(struct user)))) {
    214 			perror("allocate users");
    215 			exit(1);
    216 		}
    217 	} else {
    218 		for (usr = users, i = nusers; --i >= 0; usr++) {
    219 			usr->space = usr->spc30 = usr->spc60 = usr->spc90 = 0;
    220 			usr->count = 0;
    221 		}
    222 	}
    223 }
    224 
    225 static void
    226 usrrehash()
    227 {
    228 	register i;
    229 	register struct user *usr, *usrn;
    230 	struct user *svusr;
    231 
    232 	svusr = users;
    233 	nusers <<= 1;
    234 	if (!(users = (struct user *)calloc(nusers, sizeof(struct user)))) {
    235 		perror("allocate users");
    236 		exit(1);
    237 	}
    238 	for (usr = svusr, i = nusers >> 1; --i >= 0; usr++) {
    239 		for (usrn = users + (usr->uid&(nusers - 1));
    240 		     usrn->name;
    241 		     usrn--) {
    242 			if (usrn <= users)
    243 				usrn = users + nusers;
    244 		}
    245 		*usrn = *usr;
    246 	}
    247 }
    248 
    249 static struct user *
    250 user(uid)
    251 	uid_t uid;
    252 {
    253 	register struct user *usr;
    254 	register i;
    255 	struct passwd *pwd;
    256 
    257 	while (1) {
    258 		for (usr = users + (uid&(nusers - 1)), i = nusers;
    259 		     --i >= 0;
    260 		     usr--) {
    261 			if (!usr->name) {
    262 				usr->uid = uid;
    263 
    264 				if (!(pwd = getpwuid(uid))) {
    265 					if ((usr->name =
    266 					    (char *)malloc(7)) != NULL)
    267 						sprintf(usr->name, "#%d", uid);
    268 				} else {
    269 					if ((usr->name =
    270 					    (char *)malloc(
    271 						strlen(pwd->pw_name) + 1))
    272 					    != NULL)
    273 						strcpy(usr->name, pwd->pw_name);
    274 				}
    275 				if (!usr->name) {
    276 					perror("allocate users");
    277 					exit(1);
    278 				}
    279 				return usr;
    280 			} else if (usr->uid == uid)
    281 				return usr;
    282 
    283 			if (usr <= users)
    284 				usr = users + nusers;
    285 		}
    286 		usrrehash();
    287 	}
    288 }
    289 
    290 static int
    291 cmpusers(u1, u2)
    292 	const void *u1, *u2;
    293 {
    294 	return ((struct user *)u2)->space - ((struct user *)u1)->space;
    295 }
    296 
    297 #define	sortusers(users)	(qsort((users), nusers, sizeof(struct user), \
    298 				       cmpusers))
    299 
    300 static void
    301 uses(uid, blks, act)
    302 	uid_t uid;
    303 	daddr_t blks;
    304 	time_t act;
    305 {
    306 	static time_t today;
    307 	register struct user *usr;
    308 
    309 	if (!today)
    310 		time(&today);
    311 
    312 	usr = user(uid);
    313 	usr->count++;
    314 	usr->space += blks;
    315 
    316 	if (today - act > 90L * 24L * 60L * 60L)
    317 		usr->spc90 += blks;
    318 	if (today - act > 60L * 24L * 60L * 60L)
    319 		usr->spc60 += blks;
    320 	if (today - act > 30L * 24L * 60L * 60L)
    321 		usr->spc30 += blks;
    322 }
    323 
    324 #ifdef	COMPAT
    325 #define	FSZCNT	500
    326 #else
    327 #define	FSZCNT	512
    328 #endif
    329 struct fsizes {
    330 	struct fsizes *fsz_next;
    331 	daddr_t fsz_first, fsz_last;
    332 	ino_t fsz_count[FSZCNT];
    333 	daddr_t fsz_sz[FSZCNT];
    334 } *fsizes;
    335 
    336 static void
    337 initfsizes()
    338 {
    339 	register struct fsizes *fp;
    340 	register i;
    341 
    342 	for (fp = fsizes; fp; fp = fp->fsz_next) {
    343 		for (i = FSZCNT; --i >= 0;) {
    344 			fp->fsz_count[i] = 0;
    345 			fp->fsz_sz[i] = 0;
    346 		}
    347 	}
    348 }
    349 
    350 static void
    351 dofsizes(fd, super, name)
    352 	int fd;
    353 	struct fs *super;
    354 	char *name;
    355 {
    356 	ino_t inode, maxino;
    357 	struct dinode *ip;
    358 	daddr_t sz, ksz;
    359 	struct fsizes *fp, **fsp;
    360 	register i;
    361 
    362 	maxino = super->fs_ncg * super->fs_ipg - 1;
    363 #ifdef	COMPAT
    364 	if (!(fsizes = (struct fsizes *)malloc(sizeof(struct fsizes)))) {
    365 		perror("alloc fsize structure");
    366 		exit(1);
    367 	}
    368 #endif	/* COMPAT */
    369 	for (inode = 0; inode < maxino; inode++) {
    370 		errno = 0;
    371 		if ((ip = get_inode(fd, super, inode))
    372 #ifdef	COMPAT
    373 		    && ((ip->di_mode&IFMT) == IFREG
    374 			|| (ip->di_mode&IFMT) == IFDIR)
    375 #else	/* COMPAT */
    376 		    && !isfree(ip)
    377 #endif	/* COMPAT */
    378 		    ) {
    379 			sz = estimate ? virtualblocks(super, ip) :
    380 			    actualblocks(super, ip);
    381 #ifdef	COMPAT
    382 			if (sz >= FSZCNT) {
    383 				fsizes->fsz_count[FSZCNT-1]++;
    384 				fsizes->fsz_sz[FSZCNT-1] += sz;
    385 			} else {
    386 				fsizes->fsz_count[sz]++;
    387 				fsizes->fsz_sz[sz] += sz;
    388 			}
    389 #else	/* COMPAT */
    390 			ksz = SIZE(sz);
    391 			for (fsp = &fsizes; (fp = *fsp) != NULL;
    392 			    fsp = &fp->fsz_next) {
    393 				if (ksz < fp->fsz_last)
    394 					break;
    395 			}
    396 			if (!fp || ksz < fp->fsz_first) {
    397 				if (!(fp = (struct fsizes *)
    398 				      malloc(sizeof(struct fsizes)))) {
    399 					perror("alloc fsize structure");
    400 					exit(1);
    401 				}
    402 				fp->fsz_next = *fsp;
    403 				*fsp = fp;
    404 				fp->fsz_first = (ksz / FSZCNT) * FSZCNT;
    405 				fp->fsz_last = fp->fsz_first + FSZCNT;
    406 				for (i = FSZCNT; --i >= 0;) {
    407 					fp->fsz_count[i] = 0;
    408 					fp->fsz_sz[i] = 0;
    409 				}
    410 			}
    411 			fp->fsz_count[ksz % FSZCNT]++;
    412 			fp->fsz_sz[ksz % FSZCNT] += sz;
    413 #endif	/* COMPAT */
    414 		} else if (errno) {
    415 			perror(name);
    416 			exit(1);
    417 		}
    418 	}
    419 	sz = 0;
    420 	for (fp = fsizes; fp; fp = fp->fsz_next) {
    421 		for (i = 0; i < FSZCNT; i++) {
    422 			if (fp->fsz_count[i])
    423 				printf("%ld\t%ld\t%ld\n",
    424 				    (long)(fp->fsz_first + i),
    425 				    (long)fp->fsz_count[i],
    426 				    (long)SIZE(sz += fp->fsz_sz[i]));
    427 		}
    428 	}
    429 }
    430 
    431 static void
    432 douser(fd, super, name)
    433 	int fd;
    434 	struct fs *super;
    435 	char *name;
    436 {
    437 	ino_t inode, maxino;
    438 	struct user *usr, *usrs;
    439 	struct dinode *ip;
    440 	register n;
    441 
    442 	maxino = super->fs_ncg * super->fs_ipg - 1;
    443 	for (inode = 0; inode < maxino; inode++) {
    444 		errno = 0;
    445 		if ((ip = get_inode(fd, super, inode))
    446 		    && !isfree(ip))
    447 			uses(ip->di_uid, estimate ? virtualblocks(super, ip) :
    448 			    actualblocks(super, ip), ip->di_atime);
    449 		else if (errno) {
    450 			perror(name);
    451 			exit(1);
    452 		}
    453 	}
    454 	if (!(usrs = (struct user *)malloc(nusers * sizeof(struct user)))) {
    455 		perror("allocate users");
    456 		exit(1);
    457 	}
    458 	bcopy(users, usrs, nusers * sizeof(struct user));
    459 	sortusers(usrs);
    460 	for (usr = usrs, n = nusers; --n >= 0 && usr->count; usr++) {
    461 		printf("%5ld", (long)SIZE(usr->space));
    462 		if (count)
    463 			printf("\t%5ld", (long)usr->count);
    464 		printf("\t%-8s", usr->name);
    465 		if (unused)
    466 			printf("\t%5ld\t%5ld\t%5ld",
    467 			    SIZE(usr->spc30), SIZE(usr->spc60),
    468 			    SIZE(usr->spc90));
    469 		printf("\n");
    470 	}
    471 	free(usrs);
    472 }
    473 
    474 static void
    475 donames(fd, super, name)
    476 	int fd;
    477 	struct fs *super;
    478 	char *name;
    479 {
    480 	int c;
    481 	ino_t inode, inode1;
    482 	ino_t maxino;
    483 	struct dinode *ip;
    484 
    485 	maxino = super->fs_ncg * super->fs_ipg - 1;
    486 	/* first skip the name of the filesystem */
    487 	while ((c = getchar()) != EOF && (c < '0' || c > '9'))
    488 		while ((c = getchar()) != EOF && c != '\n');
    489 	ungetc(c, stdin);
    490 	inode1 = -1;
    491 	while (scanf("%d", &inode) == 1) {
    492 		if (inode < 0 || inode > maxino) {
    493 #ifndef	COMPAT
    494 			fprintf(stderr, "invalid inode %d\n", inode);
    495 #endif
    496 			return;
    497 		}
    498 #ifdef	COMPAT
    499 		if (inode < inode1)
    500 			continue;
    501 #endif
    502 		errno = 0;
    503 		if ((ip = get_inode(fd, super, inode))
    504 		    && !isfree(ip)) {
    505 			printf("%s\t", user(ip->di_uid)->name);
    506 			/* now skip whitespace */
    507 			while ((c = getchar()) == ' ' || c == '\t');
    508 			/* and print out the remainder of the input line */
    509 			while (c != EOF && c != '\n') {
    510 				putchar(c);
    511 				c = getchar();
    512 			}
    513 			putchar('\n');
    514 			inode1 = inode;
    515 		} else {
    516 			if (errno) {
    517 				perror(name);
    518 				exit(1);
    519 			}
    520 			/* skip this line */
    521 			while ((c = getchar()) != EOF && c != '\n');
    522 		}
    523 		if (c == EOF)
    524 			break;
    525 	}
    526 }
    527 
    528 static void
    529 usage()
    530 {
    531 #ifdef	COMPAT
    532 	fprintf(stderr, "Usage: quot [-nfcvha] [filesystem ...]\n");
    533 #else	/* COMPAT */
    534 	fprintf(stderr, "Usage: quot [ -acfhknv ] [ filesystem ... ]\n");
    535 #endif	/* COMPAT */
    536 	exit(1);
    537 }
    538 
    539 static char superblock[SBSIZE];
    540 
    541 #define	max(a,b)	MAX((a),(b))
    542 /*
    543  * Sanity checks for old file systems.
    544  * Stolen from <sys/lib/libsa/ufs.c>
    545  */
    546 static void
    547 ffs_oldfscompat(fs)
    548 	struct fs *fs;
    549 {
    550 	int i;
    551 
    552 	fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect);	/* XXX */
    553 	fs->fs_interleave = max(fs->fs_interleave, 1);		/* XXX */
    554 	if (fs->fs_postblformat == FS_42POSTBLFMT)		/* XXX */
    555 		fs->fs_nrpos = 8;				/* XXX */
    556 	if (fs->fs_inodefmt < FS_44INODEFMT) {			/* XXX */
    557 		quad_t sizepb = fs->fs_bsize;			/* XXX */
    558 								/* XXX */
    559 		fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1;	/* XXX */
    560 		for (i = 0; i < NIADDR; i++) {			/* XXX */
    561 			sizepb *= NINDIR(fs);			/* XXX */
    562 			fs->fs_maxfilesize += sizepb;		/* XXX */
    563 		}						/* XXX */
    564 		fs->fs_qbmask = ~fs->fs_bmask;			/* XXX */
    565 		fs->fs_qfmask = ~fs->fs_fmask;			/* XXX */
    566 	}							/* XXX */
    567 }
    568 
    569 void
    570 quot(name, mp)
    571 	char *name, *mp;
    572 {
    573 	int fd;
    574 
    575 	get_inode(-1, 0, 0);		/* flush cache */
    576 	inituser();
    577 	initfsizes();
    578 	if ((fd = open(name, 0)) < 0
    579 	    || lseek(fd, SBOFF, 0) != SBOFF
    580 	    || read(fd, superblock, SBSIZE) != SBSIZE) {
    581 		perror(name);
    582 		close(fd);
    583 		return;
    584 	}
    585 	if (((struct fs *)superblock)->fs_magic != FS_MAGIC
    586 	    || ((struct fs *)superblock)->fs_bsize > MAXBSIZE
    587 	    || ((struct fs *)superblock)->fs_bsize < sizeof(struct fs)) {
    588 		fprintf(stderr, "%s: not a BSD filesystem\n", name);
    589 		close(fd);
    590 		return;
    591 	}
    592 	ffs_oldfscompat((struct fs *)superblock);
    593 	printf("%s:", name);
    594 	if (mp)
    595 		printf(" (%s)", mp);
    596 	putchar('\n');
    597 	(*func)(fd, (struct fs *)superblock, name);
    598 	close(fd);
    599 }
    600 
    601 int
    602 main(argc, argv)
    603 	int argc;
    604 	char **argv;
    605 {
    606 	char all = 0;
    607 	struct statfs *mp;
    608 	char dev[MNAMELEN + 1];
    609 	char *nm;
    610 	int cnt;
    611 
    612 	func = douser;
    613 #ifndef	COMPAT
    614 	header = getbsize(&headerlen, &blocksize);
    615 #endif
    616 	while (--argc > 0 && **++argv == '-') {
    617 		while (*++*argv) {
    618 			switch (**argv) {
    619 			case 'n':
    620 				func = donames;
    621 				break;
    622 			case 'c':
    623 				func = dofsizes;
    624 				break;
    625 			case 'a':
    626 				all = 1;
    627 				break;
    628 			case 'f':
    629 				count = 1;
    630 				break;
    631 			case 'h':
    632 				estimate = 1;
    633 				break;
    634 #ifndef	COMPAT
    635 			case 'k':
    636 				blocksize = 1024;
    637 				break;
    638 #endif	/* COMPAT */
    639 			case 'v':
    640 				unused = 1;
    641 				break;
    642 			default:
    643 				usage();
    644 			}
    645 		}
    646 	}
    647 	if (all) {
    648 		cnt = getmntinfo(&mp, MNT_NOWAIT);
    649 		for (; --cnt >= 0; mp++) {
    650 			if (!strncmp(mp->f_fstypename, MOUNT_FFS, MFSNAMELEN)) {
    651 				if ((nm =
    652 				    strrchr(mp->f_mntfromname, '/')) != NULL) {
    653 					sprintf(dev, "/dev/r%s", nm + 1);
    654 					nm = dev;
    655 				} else
    656 					nm = mp->f_mntfromname;
    657 				quot(nm, mp->f_mntonname);
    658 			}
    659 		}
    660 	}
    661 	while (--argc >= 0)
    662 		quot(*argv++, 0);
    663 	return 0;
    664 }
    665