xref: /illumos-gate/usr/src/cmd/fs.d/ufs/quot/quot.c (revision 8509e9ca)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d1a180b0Smaheshvs  * Common Development and Distribution License (the "License").
6d1a180b0Smaheshvs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22d1a180b0Smaheshvs  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*8509e9caSToomas Soome /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * quot
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <stdlib.h>
457c478bd9Sstevel@tonic-gate #include <ctype.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <limits.h>
487c478bd9Sstevel@tonic-gate #include <pwd.h>
497c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
507c478bd9Sstevel@tonic-gate #include <sys/param.h>
517c478bd9Sstevel@tonic-gate #include <sys/types.h>
527c478bd9Sstevel@tonic-gate #include <unistd.h>
537c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
547c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
557c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
577c478bd9Sstevel@tonic-gate #include <sys/file.h>
587c478bd9Sstevel@tonic-gate #include <sys/stat.h>
597c478bd9Sstevel@tonic-gate #include <fcntl.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	ISIZ	(MAXBSIZE/sizeof (struct dinode))
627c478bd9Sstevel@tonic-gate static union {
637c478bd9Sstevel@tonic-gate 	struct fs u_sblock;
647c478bd9Sstevel@tonic-gate 	char dummy[SBSIZE];
657c478bd9Sstevel@tonic-gate } sb_un;
667c478bd9Sstevel@tonic-gate #define	sblock sb_un.u_sblock
677c478bd9Sstevel@tonic-gate static struct dinode *itab;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate struct du {
707c478bd9Sstevel@tonic-gate 	struct	du *next;
717c478bd9Sstevel@tonic-gate 	long	blocks;
727c478bd9Sstevel@tonic-gate 	long	blocks30;
737c478bd9Sstevel@tonic-gate 	long	blocks60;
747c478bd9Sstevel@tonic-gate 	long	blocks90;
757c478bd9Sstevel@tonic-gate 	long	nfiles;
767c478bd9Sstevel@tonic-gate 	uid_t	uid;
777c478bd9Sstevel@tonic-gate 	char	*u_name;
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate static struct du **du;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	UHASH 8209
827c478bd9Sstevel@tonic-gate static int	ndu;
837c478bd9Sstevel@tonic-gate #define	HASH(u) ((uint_t)(u) % UHASH)
847c478bd9Sstevel@tonic-gate static struct	du *duhashtbl[UHASH];
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	TSIZE	2048
877c478bd9Sstevel@tonic-gate static int	sizes[TSIZE];
887c478bd9Sstevel@tonic-gate static offset_t overflow;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int	nflg;
917c478bd9Sstevel@tonic-gate static int	fflg;
927c478bd9Sstevel@tonic-gate static int	cflg;
937c478bd9Sstevel@tonic-gate static int	vflg;
947c478bd9Sstevel@tonic-gate static int	hflg;
957c478bd9Sstevel@tonic-gate static int	aflg;
967c478bd9Sstevel@tonic-gate static long	now;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static unsigned	ino;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static void usage(void);
1017c478bd9Sstevel@tonic-gate static void quotall(void);
1027c478bd9Sstevel@tonic-gate static void qacct(struct dinode *);
1037c478bd9Sstevel@tonic-gate static void bread(int, diskaddr_t, char *, int);
1047c478bd9Sstevel@tonic-gate static void report(void);
1057c478bd9Sstevel@tonic-gate static int getdev(char **);
1067c478bd9Sstevel@tonic-gate static int check(char *, char *);
1077c478bd9Sstevel@tonic-gate static struct du *adduid(uid_t);
1087c478bd9Sstevel@tonic-gate static struct du *lookup(uid_t);
1097c478bd9Sstevel@tonic-gate static void sortprep(void);
1107c478bd9Sstevel@tonic-gate static void cleanup(void);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static void
usage()1137c478bd9Sstevel@tonic-gate usage()
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ufs usage: quot [-nfcvha] [filesystem ...]\n");
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])1197c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	int	opt;
1227c478bd9Sstevel@tonic-gate 	int	i;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (argc == 1) {
1257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1267c478bd9Sstevel@tonic-gate 		    "ufs Usage: quot [-nfcvha] [filesystem ...]\n");
1277c478bd9Sstevel@tonic-gate 		return (32);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	now = time(0);
1317c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "nfcvhaV")) != EOF) {
1327c478bd9Sstevel@tonic-gate 		switch (opt) {
1337c478bd9Sstevel@tonic-gate 		case 'n':
1347c478bd9Sstevel@tonic-gate 			nflg++;
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		case 'f':
1377c478bd9Sstevel@tonic-gate 			fflg++;
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		case 'c':
1407c478bd9Sstevel@tonic-gate 			cflg++;
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		case 'v':
1437c478bd9Sstevel@tonic-gate 			vflg++;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case 'h':
1467c478bd9Sstevel@tonic-gate 			hflg++;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case 'a':
1497c478bd9Sstevel@tonic-gate 			aflg++;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		case 'V':		/* Print command line */
1527c478bd9Sstevel@tonic-gate 			{
1537c478bd9Sstevel@tonic-gate 				char		*opt_text;
1547c478bd9Sstevel@tonic-gate 				int		opt_count;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "quot -F UFS ");
1577c478bd9Sstevel@tonic-gate 				for (opt_count = 1; opt_count < argc;
1587c478bd9Sstevel@tonic-gate 				    opt_count++) {
1597c478bd9Sstevel@tonic-gate 					opt_text = argv[opt_count];
1607c478bd9Sstevel@tonic-gate 					if (opt_text)
1617c478bd9Sstevel@tonic-gate 						(void) fprintf(stdout, " %s ",
1627c478bd9Sstevel@tonic-gate 						    opt_text);
1637c478bd9Sstevel@tonic-gate 				}
1647c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "\n");
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		case '?':
1687c478bd9Sstevel@tonic-gate 			usage();
1697c478bd9Sstevel@tonic-gate 			return (32);
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (aflg) {
1747c478bd9Sstevel@tonic-gate 		quotall();
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	for (i = optind; i < argc; i++) {
1787c478bd9Sstevel@tonic-gate 		if ((getdev(&argv[i]) == 0) &&
1797c478bd9Sstevel@tonic-gate 			(check(argv[i], (char *)NULL) == 0)) {
1807c478bd9Sstevel@tonic-gate 				report();
1817c478bd9Sstevel@tonic-gate 				cleanup();
1827c478bd9Sstevel@tonic-gate 		}
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	return (0);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static void
quotall()1887c478bd9Sstevel@tonic-gate quotall()
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	FILE *fstab;
1917c478bd9Sstevel@tonic-gate 	struct mnttab mntp;
1927c478bd9Sstevel@tonic-gate 	char *cp;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	extern char *getfullrawname();
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	fstab = fopen(MNTTAB, "r");
1977c478bd9Sstevel@tonic-gate 	if (fstab == NULL) {
1987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: no %s file\n", MNTTAB);
1997c478bd9Sstevel@tonic-gate 		exit(32);
2007c478bd9Sstevel@tonic-gate 	}
201*8509e9caSToomas Soome 	while (getmntent(fstab, &mntp) == 0) {
2027c478bd9Sstevel@tonic-gate 		if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0)
2037c478bd9Sstevel@tonic-gate 			continue;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		if ((cp = getfullrawname(mntp.mnt_special)) == NULL)
2067c478bd9Sstevel@tonic-gate 			continue;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		if (*cp == '\0')
2097c478bd9Sstevel@tonic-gate 			continue;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		if (check(cp, mntp.mnt_mountp) == 0) {
2127c478bd9Sstevel@tonic-gate 			report();
2137c478bd9Sstevel@tonic-gate 			cleanup();
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		free(cp);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 	(void) fclose(fstab);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static int
check(char * file,char * fsdir)2227c478bd9Sstevel@tonic-gate check(char *file, char *fsdir)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	FILE *fstab;
2257c478bd9Sstevel@tonic-gate 	int i, j;
2267c478bd9Sstevel@tonic-gate 	int c, fd;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Initialize tables between checks;
2317c478bd9Sstevel@tonic-gate 	 * because of the qsort done in report()
2327c478bd9Sstevel@tonic-gate 	 * the hash tables must be rebuilt each time.
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	for (i = 0; i < TSIZE; i++)
2357c478bd9Sstevel@tonic-gate 		sizes[i] = 0;
2367c478bd9Sstevel@tonic-gate 	overflow = 0LL;
2377c478bd9Sstevel@tonic-gate 	ndu = 0;
2387c478bd9Sstevel@tonic-gate 	fd = open64(file, O_RDONLY);
2397c478bd9Sstevel@tonic-gate 	if (fd < 0) {
2407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: ");
2417c478bd9Sstevel@tonic-gate 		perror(file);
2427c478bd9Sstevel@tonic-gate 		exit(32);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	(void) printf("%s", file);
2457c478bd9Sstevel@tonic-gate 	if (fsdir == NULL) {
2467c478bd9Sstevel@tonic-gate 		struct mnttab mntp;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		fstab = fopen(MNTTAB, "r");
2497c478bd9Sstevel@tonic-gate 		if (fstab == NULL) {
2507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "quot: no %s file\n", MNTTAB);
2517c478bd9Sstevel@tonic-gate 			exit(32);
2527c478bd9Sstevel@tonic-gate 		}
253*8509e9caSToomas Soome 		while (getmntent(fstab, &mntp) == 0) {
2547c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0)
2557c478bd9Sstevel@tonic-gate 				continue;
2567c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_special, file) == 0) {
2577c478bd9Sstevel@tonic-gate 				fsdir = mntp.mnt_mountp;
2587c478bd9Sstevel@tonic-gate 				break;
2597c478bd9Sstevel@tonic-gate 			}
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	if (fsdir != NULL && *fsdir != '\0')
2637c478bd9Sstevel@tonic-gate 		(void) printf(" (%s)", fsdir);
2647c478bd9Sstevel@tonic-gate 	(void) printf(":\n");
2657c478bd9Sstevel@tonic-gate 	sync();
2667c478bd9Sstevel@tonic-gate 	bread(fd, (diskaddr_t)SBLOCK, (char *)&sblock, SBSIZE);
2677c478bd9Sstevel@tonic-gate 	if (nflg) {
2687c478bd9Sstevel@tonic-gate 		if (isdigit(c = getchar()))
2697c478bd9Sstevel@tonic-gate 			(void) ungetc(c, stdin);
2707c478bd9Sstevel@tonic-gate 		else while (c != '\n' && c != EOF)
2717c478bd9Sstevel@tonic-gate 			c = getchar();
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	itab = (struct dinode *)calloc(sblock.fs_ipg, sizeof (struct dinode));
2757c478bd9Sstevel@tonic-gate 	if (itab == NULL) {
2767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2777c478bd9Sstevel@tonic-gate 				"not enough memory to allocate tables\n");
2787c478bd9Sstevel@tonic-gate 		return (1);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	ino = 0;
2827c478bd9Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
2837c478bd9Sstevel@tonic-gate 		bread(fd, (diskaddr_t)fsbtodb(&sblock, cgimin(&sblock, c)),
2847c478bd9Sstevel@tonic-gate 				(char *)itab,
2857c478bd9Sstevel@tonic-gate 				(int)(sblock.fs_ipg * sizeof (struct dinode)));
2867c478bd9Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++, ino++) {
2877c478bd9Sstevel@tonic-gate 			if (ino < UFSROOTINO)
2887c478bd9Sstevel@tonic-gate 				continue;
2897c478bd9Sstevel@tonic-gate 			qacct(&itab[j]);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	(void) close(fd);
2937c478bd9Sstevel@tonic-gate 	return (0);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static void
qacct(struct dinode * ip)2977c478bd9Sstevel@tonic-gate qacct(struct dinode *ip)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	struct du *dp;
3007c478bd9Sstevel@tonic-gate 	long blks, frags, size;
3017c478bd9Sstevel@tonic-gate 	int n;
302d1a180b0Smaheshvs 	static int fino;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	ip->di_mode = ip->di_smode;
3057c478bd9Sstevel@tonic-gate 	if (ip->di_suid != UID_LONG) {
3067c478bd9Sstevel@tonic-gate 		ip->di_uid = ip->di_suid;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	if ((ip->di_mode & IFMT) == 0)
3097c478bd9Sstevel@tonic-gate 		return;
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * By default, take block count in inode.  Otherwise (-h),
3127c478bd9Sstevel@tonic-gate 	 * take the size field and estimate the blocks allocated.
3137c478bd9Sstevel@tonic-gate 	 * The latter does not account for holes in files.
3147c478bd9Sstevel@tonic-gate 	 */
3157c478bd9Sstevel@tonic-gate 	if (!hflg)
3167c478bd9Sstevel@tonic-gate 		size = ip->di_blocks / 2;
3177c478bd9Sstevel@tonic-gate 	else {
3187c478bd9Sstevel@tonic-gate 		blks = lblkno(&sblock, ip->di_size);
3197c478bd9Sstevel@tonic-gate 		frags = blks * sblock.fs_frag +
3207c478bd9Sstevel@tonic-gate 			numfrags(&sblock, dblksize(&sblock, ip, blks));
3217c478bd9Sstevel@tonic-gate 		/*
3227c478bd9Sstevel@tonic-gate 		 * Must cast to offset_t because for a large file,
3237c478bd9Sstevel@tonic-gate 		 * frags multiplied by sblock.fs_fsize will not fit in a long.
3247c478bd9Sstevel@tonic-gate 		 * However, when divided by 1024, the end result will fit in
3257c478bd9Sstevel@tonic-gate 		 * the 32 bit size variable (40 bit UFS).
3267c478bd9Sstevel@tonic-gate 		 */
3277c478bd9Sstevel@tonic-gate 	    size = (long)((offset_t)frags * (offset_t)sblock.fs_fsize / 1024);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 	if (cflg) {
3307c478bd9Sstevel@tonic-gate 		if ((ip->di_mode&IFMT) != IFDIR && (ip->di_mode&IFMT) != IFREG)
3317c478bd9Sstevel@tonic-gate 			return;
3327c478bd9Sstevel@tonic-gate 		if (size >= TSIZE) {
3337c478bd9Sstevel@tonic-gate 			overflow += (offset_t)size;
3347c478bd9Sstevel@tonic-gate 			size = TSIZE-1;
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		sizes[size]++;
3377c478bd9Sstevel@tonic-gate 		return;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	dp = lookup(ip->di_uid);
3407c478bd9Sstevel@tonic-gate 	if (dp == NULL)
3417c478bd9Sstevel@tonic-gate 		return;
3427c478bd9Sstevel@tonic-gate 	dp->blocks += size;
3437c478bd9Sstevel@tonic-gate #define	DAY (60 * 60 * 24)	/* seconds per day */
3447c478bd9Sstevel@tonic-gate 	if (now - ip->di_atime > 30 * DAY)
3457c478bd9Sstevel@tonic-gate 		dp->blocks30 += size;
3467c478bd9Sstevel@tonic-gate 	if (now - ip->di_atime > 60 * DAY)
3477c478bd9Sstevel@tonic-gate 		dp->blocks60 += size;
3487c478bd9Sstevel@tonic-gate 	if (now - ip->di_atime > 90 * DAY)
3497c478bd9Sstevel@tonic-gate 		dp->blocks90 += size;
3507c478bd9Sstevel@tonic-gate 	dp->nfiles++;
3517c478bd9Sstevel@tonic-gate 	while (nflg) {
3527c478bd9Sstevel@tonic-gate 		if (fino == 0)
3537c478bd9Sstevel@tonic-gate 			if (scanf("%d", &fino) <= 0)
3547c478bd9Sstevel@tonic-gate 				return;
3557c478bd9Sstevel@tonic-gate 		if (fino > ino)
3567c478bd9Sstevel@tonic-gate 			return;
3577c478bd9Sstevel@tonic-gate 		if (fino < ino) {
3587c478bd9Sstevel@tonic-gate 			while ((n = getchar()) != '\n' && n != EOF)
3597c478bd9Sstevel@tonic-gate 				;
3607c478bd9Sstevel@tonic-gate 			fino = 0;
3617c478bd9Sstevel@tonic-gate 			continue;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 		if (dp->u_name)
3647c478bd9Sstevel@tonic-gate 			(void) printf("%.7s	", dp->u_name);
3657c478bd9Sstevel@tonic-gate 		else
3667c478bd9Sstevel@tonic-gate 			(void) printf("%ld	", (long)ip->di_uid);
3677c478bd9Sstevel@tonic-gate 		while ((n = getchar()) == ' ' || n == '\t')
3687c478bd9Sstevel@tonic-gate 			;
3697c478bd9Sstevel@tonic-gate 		(void) putchar(n);
3707c478bd9Sstevel@tonic-gate 		while (n != EOF && n != '\n') {
3717c478bd9Sstevel@tonic-gate 			n = getchar();
3727c478bd9Sstevel@tonic-gate 			(void) putchar(n);
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		fino = 0;
3757c478bd9Sstevel@tonic-gate 		break;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static void
bread(int fd,diskaddr_t bno,char * buf,int cnt)3807c478bd9Sstevel@tonic-gate bread(int fd, diskaddr_t bno, char *buf, int cnt)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	int	ret;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (llseek(fd, (offset_t)(bno * DEV_BSIZE), SEEK_SET) < 0) {
3857c478bd9Sstevel@tonic-gate 		perror("llseek");
3867c478bd9Sstevel@tonic-gate 		exit(32);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if ((ret = read(fd, buf, cnt)) != cnt) {
3907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: read returns %d (cnt = %d)\n",
3917c478bd9Sstevel@tonic-gate 						ret, cnt);
3927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: read error at block %lld\n", bno);
3937c478bd9Sstevel@tonic-gate 		perror("read");
3947c478bd9Sstevel@tonic-gate 		exit(32);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate static int
qcmp(const void * arg1,const void * arg2)3997c478bd9Sstevel@tonic-gate qcmp(const void *arg1, const void *arg2)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	struct du **p1 = (struct du **)arg1;
4027c478bd9Sstevel@tonic-gate 	struct du **p2 = (struct du **)arg2;
4037c478bd9Sstevel@tonic-gate 	char *s1, *s2;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if ((*p1)->blocks > (*p2)->blocks)
4067c478bd9Sstevel@tonic-gate 		return (-1);
4077c478bd9Sstevel@tonic-gate 	if ((*p1)->blocks < (*p2)->blocks)
4087c478bd9Sstevel@tonic-gate 		return (1);
4097c478bd9Sstevel@tonic-gate 	s1 = (*p1)->u_name;
4107c478bd9Sstevel@tonic-gate 	if (s1 == NULL)
4117c478bd9Sstevel@tonic-gate 		return (0);
4127c478bd9Sstevel@tonic-gate 	s2 = (*p2)->u_name;
4137c478bd9Sstevel@tonic-gate 	if (s2 == NULL)
4147c478bd9Sstevel@tonic-gate 		return (0);
4157c478bd9Sstevel@tonic-gate 	return (strcmp(s1, s2));
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate static void
report()4197c478bd9Sstevel@tonic-gate report()
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	int i;
4227c478bd9Sstevel@tonic-gate 	struct du **dp;
4237c478bd9Sstevel@tonic-gate 	int cnt;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (nflg)
4267c478bd9Sstevel@tonic-gate 		return;
4277c478bd9Sstevel@tonic-gate 	if (cflg) {
4287c478bd9Sstevel@tonic-gate 		long t = 0;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		for (i = 0; i < TSIZE - 1; i++)
4317c478bd9Sstevel@tonic-gate 			if (sizes[i]) {
4327c478bd9Sstevel@tonic-gate 				t += i*sizes[i];
4337c478bd9Sstevel@tonic-gate 				(void) printf("%d	%d	%ld\n",
4347c478bd9Sstevel@tonic-gate 								i, sizes[i], t);
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 		if (sizes[TSIZE -1 ])
4377c478bd9Sstevel@tonic-gate 			(void) printf("%d	%d	%lld\n", TSIZE - 1,
4387c478bd9Sstevel@tonic-gate 			    sizes[TSIZE - 1], overflow + (offset_t)t);
4397c478bd9Sstevel@tonic-gate 		return;
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 	sortprep();
4427c478bd9Sstevel@tonic-gate 	qsort(du, ndu, sizeof (du[0]), qcmp);
4437c478bd9Sstevel@tonic-gate 	for (cnt = 0, dp = &du[0]; dp && cnt != ndu; dp++, cnt++) {
4447c478bd9Sstevel@tonic-gate 		if ((*dp)->blocks == 0)
4457c478bd9Sstevel@tonic-gate 			return;
4467c478bd9Sstevel@tonic-gate 		(void) printf("%5ld\t", (*dp)->blocks);
4477c478bd9Sstevel@tonic-gate 		if (fflg)
4487c478bd9Sstevel@tonic-gate 			(void) printf("%5ld\t", (*dp)->nfiles);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		if ((*dp)->u_name)
4517c478bd9Sstevel@tonic-gate 			(void) printf("%-8s", (*dp)->u_name);
4527c478bd9Sstevel@tonic-gate 		else
4537c478bd9Sstevel@tonic-gate 			(void) printf("#%-8ld", (long)(*dp)->uid);
4547c478bd9Sstevel@tonic-gate 		if (vflg)
4557c478bd9Sstevel@tonic-gate 			(void) printf("\t%5ld\t%5ld\t%5ld",
4567c478bd9Sstevel@tonic-gate 			    (*dp)->blocks30, (*dp)->blocks60, (*dp)->blocks90);
4577c478bd9Sstevel@tonic-gate 		(void) printf("\n");
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate static int
getdev(char ** devpp)4647c478bd9Sstevel@tonic-gate getdev(char **devpp)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	struct stat64 statb;
4677c478bd9Sstevel@tonic-gate 	FILE *fstab;
4687c478bd9Sstevel@tonic-gate 	struct mnttab mntp;
4697c478bd9Sstevel@tonic-gate 	char *cp;	/* Pointer to raw device name */
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	extern char *getfullrawname();
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if (stat64(*devpp, &statb) < 0) {
4747c478bd9Sstevel@tonic-gate 		perror(*devpp);
4757c478bd9Sstevel@tonic-gate 		exit(32);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) == S_IFCHR)
4787c478bd9Sstevel@tonic-gate 		return (0);
4797c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) == S_IFBLK) {
4807c478bd9Sstevel@tonic-gate 		/* If we can't get the raw name, keep the block name */
4817c478bd9Sstevel@tonic-gate 		if ((cp = getfullrawname(*devpp)) != NULL)
4827c478bd9Sstevel@tonic-gate 			*devpp = strdup(cp);
4837c478bd9Sstevel@tonic-gate 		return (0);
4847c478bd9Sstevel@tonic-gate 	}
4857c478bd9Sstevel@tonic-gate 	fstab = fopen(MNTTAB, "r");
4867c478bd9Sstevel@tonic-gate 	if (fstab == NULL) {
4877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "quot: no %s file\n", MNTTAB);
4887c478bd9Sstevel@tonic-gate 		exit(32);
4897c478bd9Sstevel@tonic-gate 	}
490*8509e9caSToomas Soome 	while (getmntent(fstab, &mntp) == 0) {
4917c478bd9Sstevel@tonic-gate 		if (strcmp(mntp.mnt_mountp, *devpp) == 0) {
4927c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0) {
4937c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4947c478bd9Sstevel@tonic-gate 				    "quot: %s not ufs filesystem\n",
4957c478bd9Sstevel@tonic-gate 				    *devpp);
4967c478bd9Sstevel@tonic-gate 				exit(32);
4977c478bd9Sstevel@tonic-gate 			}
4987c478bd9Sstevel@tonic-gate 			/* If we can't get the raw name, use the block name */
4997c478bd9Sstevel@tonic-gate 			if ((cp = getfullrawname(mntp.mnt_special)) == NULL)
5007c478bd9Sstevel@tonic-gate 				cp = mntp.mnt_special;
5017c478bd9Sstevel@tonic-gate 			*devpp = strdup(cp);
5027c478bd9Sstevel@tonic-gate 			(void) fclose(fstab);
5037c478bd9Sstevel@tonic-gate 			return (0);
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	(void) fclose(fstab);
5077c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "quot: %s doesn't appear to be a filesystem.\n",
5087c478bd9Sstevel@tonic-gate 	    *devpp);
5097c478bd9Sstevel@tonic-gate 	usage();
5107c478bd9Sstevel@tonic-gate 	exit(32);
5117c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate static struct du *
lookup(uid_t uid)5157c478bd9Sstevel@tonic-gate lookup(uid_t uid)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	struct	passwd *pwp;
5187c478bd9Sstevel@tonic-gate 	struct	du *up;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	for (up = duhashtbl[HASH(uid)]; up != NULL; up = up->next) {
5217c478bd9Sstevel@tonic-gate 		if (up->uid == uid)
5227c478bd9Sstevel@tonic-gate 			return (up);
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	pwp = getpwuid(uid);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	up = adduid(uid);
5287c478bd9Sstevel@tonic-gate 	if (up && pwp) {
5297c478bd9Sstevel@tonic-gate 		up->u_name = strdup(pwp->pw_name);
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 	return (up);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate static struct du *
adduid(uid_t uid)5357c478bd9Sstevel@tonic-gate adduid(uid_t uid)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	struct du *up, **uhp;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	up = (struct du *)calloc(1, sizeof (struct du));
5407c478bd9Sstevel@tonic-gate 	if (up == NULL) {
5417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5427c478bd9Sstevel@tonic-gate 			"out of memory for du structures\n");
5437c478bd9Sstevel@tonic-gate 			exit(32);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	uhp = &duhashtbl[HASH(uid)];
5477c478bd9Sstevel@tonic-gate 	up->next = *uhp;
5487c478bd9Sstevel@tonic-gate 	*uhp = up;
5497c478bd9Sstevel@tonic-gate 	up->uid = uid;
5507c478bd9Sstevel@tonic-gate 	up->u_name = NULL;
5517c478bd9Sstevel@tonic-gate 	ndu++;
5527c478bd9Sstevel@tonic-gate 	return (up);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate static void
sortprep()5567c478bd9Sstevel@tonic-gate sortprep()
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	struct du **dp, *ep;
5597c478bd9Sstevel@tonic-gate 	struct du **hp;
5607c478bd9Sstevel@tonic-gate 	int i, cnt = 0;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	dp = NULL;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	dp = (struct du **)calloc(ndu, sizeof (struct du **));
5657c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
5667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5677c478bd9Sstevel@tonic-gate 			"out of memory for du structures\n");
5687c478bd9Sstevel@tonic-gate 			exit(32);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	for (hp = duhashtbl, i = 0; i != UHASH; i++) {
5727c478bd9Sstevel@tonic-gate 		if (hp[i] == NULL)
5737c478bd9Sstevel@tonic-gate 			continue;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 		for (ep = hp[i]; ep; ep = ep->next) {
5767c478bd9Sstevel@tonic-gate 			dp[cnt++] = ep;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	du = dp;
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate static void
cleanup()5837c478bd9Sstevel@tonic-gate cleanup()
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	int		i;
586*8509e9caSToomas Soome 	struct du	*ep, *next;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/*
5897c478bd9Sstevel@tonic-gate 	 * Release memory from hash table and du
5907c478bd9Sstevel@tonic-gate 	 */
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (du) {
5937c478bd9Sstevel@tonic-gate 		free(du);
5947c478bd9Sstevel@tonic-gate 		du = NULL;
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	for (i = 0; i != UHASH; i++) {
5997c478bd9Sstevel@tonic-gate 		if (duhashtbl[i] == NULL)
6007c478bd9Sstevel@tonic-gate 			continue;
6017c478bd9Sstevel@tonic-gate 		ep = duhashtbl[i];
6027c478bd9Sstevel@tonic-gate 		while (ep) {
6037c478bd9Sstevel@tonic-gate 			next = ep->next;
6047c478bd9Sstevel@tonic-gate 			if (ep->u_name) {
6057c478bd9Sstevel@tonic-gate 				free(ep->u_name);
6067c478bd9Sstevel@tonic-gate 			}
6077c478bd9Sstevel@tonic-gate 			free(ep);
6087c478bd9Sstevel@tonic-gate 			ep = next;
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 		duhashtbl[i] = NULL;
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate }
613