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  * Fix up / report on disc quotas & usage
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <ctype.h>
467c478bd9Sstevel@tonic-gate #include <signal.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <fcntl.h>
497c478bd9Sstevel@tonic-gate #include <sys/filio.h>
507c478bd9Sstevel@tonic-gate #include <limits.h>
517c478bd9Sstevel@tonic-gate #include <sys/param.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
567c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
577c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
587c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
597c478bd9Sstevel@tonic-gate #include <sys/stat.h>
607c478bd9Sstevel@tonic-gate #include <sys/wait.h>
617c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
627c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
637c478bd9Sstevel@tonic-gate #include <pwd.h>
647c478bd9Sstevel@tonic-gate #include <iso/limits_iso.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate union {
677c478bd9Sstevel@tonic-gate 	struct	fs	sblk;
687c478bd9Sstevel@tonic-gate 	char	dummy[MAXBSIZE];
697c478bd9Sstevel@tonic-gate } un;
707c478bd9Sstevel@tonic-gate #define	sblock	un.sblk
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	ITABSZ	256
737c478bd9Sstevel@tonic-gate struct	dinode	itab[ITABSZ];
747c478bd9Sstevel@tonic-gate struct	dinode	*dp;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate struct fileusage {
777c478bd9Sstevel@tonic-gate 	struct fileusage *fu_next;
787c478bd9Sstevel@tonic-gate 	ulong_t fu_curfiles;
797c478bd9Sstevel@tonic-gate 	uint64_t fu_curblocks;
807c478bd9Sstevel@tonic-gate 	uid_t	fu_uid;
817c478bd9Sstevel@tonic-gate };
827c478bd9Sstevel@tonic-gate #define	FUHASH 997
837c478bd9Sstevel@tonic-gate struct fileusage *fuhead[FUHASH];
847c478bd9Sstevel@tonic-gate struct fileusage *lookup(uid_t);
857c478bd9Sstevel@tonic-gate struct fileusage *adduid(uid_t);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate int fi;
887c478bd9Sstevel@tonic-gate ino_t ino;
897c478bd9Sstevel@tonic-gate struct	dinode	*ginode();
907c478bd9Sstevel@tonic-gate char *mntopt(), *hasvfsopt(), *hasmntopt();
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate extern int	optind;
937c478bd9Sstevel@tonic-gate extern char	*optarg;
947c478bd9Sstevel@tonic-gate extern int	fsync(int);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static void acct();
977c478bd9Sstevel@tonic-gate static void bread();
987c478bd9Sstevel@tonic-gate static void usage();
997c478bd9Sstevel@tonic-gate static int chkquota();
1007c478bd9Sstevel@tonic-gate static int quotactl();
1017c478bd9Sstevel@tonic-gate static int preen();
1027c478bd9Sstevel@tonic-gate static int waiter();
1037c478bd9Sstevel@tonic-gate static int oneof();
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate int	vflag;		/* verbose */
1067c478bd9Sstevel@tonic-gate int	aflag;		/* all file systems */
1077c478bd9Sstevel@tonic-gate int	pflag;		/* fsck like parallel check */
1087c478bd9Sstevel@tonic-gate int	fflag;		/* force flag */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	QFNAME "quotas"
1117c478bd9Sstevel@tonic-gate #define	CHUNK	50
1127c478bd9Sstevel@tonic-gate char **listbuf;
1137c478bd9Sstevel@tonic-gate struct dqblk zerodqbuf;
1147c478bd9Sstevel@tonic-gate struct fileusage zerofileusage;
1157c478bd9Sstevel@tonic-gate 
116d1a180b0Smaheshvs int
main(int argc,char ** argv)1177c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	struct mnttab mntp;
1207c478bd9Sstevel@tonic-gate 	struct vfstab vfsbuf;
1217c478bd9Sstevel@tonic-gate 	char **listp;
1227c478bd9Sstevel@tonic-gate 	int listcnt;
1237c478bd9Sstevel@tonic-gate 	int listmax = 0;
1247c478bd9Sstevel@tonic-gate 	char quotafile[MAXPATHLEN];
1257c478bd9Sstevel@tonic-gate 	FILE *mtab, *vfstab;
1267c478bd9Sstevel@tonic-gate 	int errs = 0;
1277c478bd9Sstevel@tonic-gate 	int	opt;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if ((listbuf = (char **)malloc(sizeof (char *) * CHUNK)) == NULL) {
1307c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Can't alloc lisbuf array.");
1317c478bd9Sstevel@tonic-gate 		exit(31+1);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	listmax = CHUNK;
1347c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "vapVf")) != EOF) {
1357c478bd9Sstevel@tonic-gate 		switch (opt) {
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		case 'v':
1387c478bd9Sstevel@tonic-gate 			vflag++;
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		case 'a':
1427c478bd9Sstevel@tonic-gate 			aflag++;
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		case 'p':
1467c478bd9Sstevel@tonic-gate 			pflag++;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		case 'V':		/* Print command line */
1507c478bd9Sstevel@tonic-gate 			{
1517c478bd9Sstevel@tonic-gate 				char		*opt_text;
1527c478bd9Sstevel@tonic-gate 				int		opt_count;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "quotacheck -F UFS ");
1557c478bd9Sstevel@tonic-gate 				for (opt_count = 1; opt_count < argc;
1567c478bd9Sstevel@tonic-gate 				    opt_count++) {
1577c478bd9Sstevel@tonic-gate 					opt_text = argv[opt_count];
1587c478bd9Sstevel@tonic-gate 					if (opt_text)
1597c478bd9Sstevel@tonic-gate 						(void) fprintf(stdout, " %s ",
1607c478bd9Sstevel@tonic-gate 						    opt_text);
1617c478bd9Sstevel@tonic-gate 				}
1627c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "\n");
1637c478bd9Sstevel@tonic-gate 			}
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		case 'f':
1677c478bd9Sstevel@tonic-gate 			fflag++;
1687c478bd9Sstevel@tonic-gate 			break;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		case '?':
1717c478bd9Sstevel@tonic-gate 			usage();
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	if (argc <= optind && !aflag) {
1757c478bd9Sstevel@tonic-gate 		usage();
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (quotactl(Q_ALLSYNC, NULL, (uid_t)0, NULL) < 0 &&
1797c478bd9Sstevel@tonic-gate 	    errno == EINVAL && vflag)
1807c478bd9Sstevel@tonic-gate 		printf("Warning: Quotas are not compiled into this kernel\n");
1817c478bd9Sstevel@tonic-gate 	sync();
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (aflag) {
1847c478bd9Sstevel@tonic-gate 		/*
1857c478bd9Sstevel@tonic-gate 		 * Go through vfstab and make a list of appropriate
1867c478bd9Sstevel@tonic-gate 		 * filesystems.
1877c478bd9Sstevel@tonic-gate 		 */
1887c478bd9Sstevel@tonic-gate 		listp = listbuf;
1897c478bd9Sstevel@tonic-gate 		listcnt = 0;
1907c478bd9Sstevel@tonic-gate 		if ((vfstab = fopen(VFSTAB, "r")) == NULL) {
1917c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Can't open ");
1927c478bd9Sstevel@tonic-gate 			perror(VFSTAB);
1937c478bd9Sstevel@tonic-gate 			exit(31+8);
1947c478bd9Sstevel@tonic-gate 		}
195*8509e9caSToomas Soome 		while (getvfsent(vfstab, &vfsbuf) == 0) {
1967c478bd9Sstevel@tonic-gate 			if (strcmp(vfsbuf.vfs_fstype, MNTTYPE_UFS) != 0 ||
1977c478bd9Sstevel@tonic-gate 			    (vfsbuf.vfs_mntopts == 0) ||
1987c478bd9Sstevel@tonic-gate 			    hasvfsopt(&vfsbuf, MNTOPT_RO) ||
1997c478bd9Sstevel@tonic-gate 			    (!hasvfsopt(&vfsbuf, MNTOPT_RQ) &&
2007c478bd9Sstevel@tonic-gate 			    !hasvfsopt(&vfsbuf, MNTOPT_QUOTA)))
2017c478bd9Sstevel@tonic-gate 				continue;
2027c478bd9Sstevel@tonic-gate 			*listp = malloc(strlen(vfsbuf.vfs_special) + 1);
2037c478bd9Sstevel@tonic-gate 			strcpy(*listp, vfsbuf.vfs_special);
2047c478bd9Sstevel@tonic-gate 			listp++;
2057c478bd9Sstevel@tonic-gate 			listcnt++;
2067c478bd9Sstevel@tonic-gate 			/* grow listbuf if needed */
2077c478bd9Sstevel@tonic-gate 			if (listcnt >= listmax) {
2087c478bd9Sstevel@tonic-gate 				listmax += CHUNK;
2097c478bd9Sstevel@tonic-gate 				listbuf = (char **)realloc(listbuf,
2107c478bd9Sstevel@tonic-gate 					sizeof (char *) * listmax);
2117c478bd9Sstevel@tonic-gate 				if (listbuf == NULL) {
2127c478bd9Sstevel@tonic-gate 					fprintf(stderr,
2137c478bd9Sstevel@tonic-gate 						"Can't grow listbuf.\n");
2147c478bd9Sstevel@tonic-gate 					exit(31+1);
2157c478bd9Sstevel@tonic-gate 				}
2167c478bd9Sstevel@tonic-gate 				listp = &listbuf[listcnt];
2177c478bd9Sstevel@tonic-gate 			}
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 		fclose(vfstab);
2207c478bd9Sstevel@tonic-gate 		*listp = (char *)0;
2217c478bd9Sstevel@tonic-gate 		listp = listbuf;
2227c478bd9Sstevel@tonic-gate 	} else {
2237c478bd9Sstevel@tonic-gate 		listp = &argv[optind];
2247c478bd9Sstevel@tonic-gate 		listcnt = argc - optind;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	if (pflag) {
2277c478bd9Sstevel@tonic-gate 		errs = preen(listcnt, listp);
2287c478bd9Sstevel@tonic-gate 	} else {
2297c478bd9Sstevel@tonic-gate 		if ((mtab = fopen(MNTTAB, "r")) == NULL) {
2307c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Can't open ");
2317c478bd9Sstevel@tonic-gate 			perror(MNTTAB);
2327c478bd9Sstevel@tonic-gate 			exit(31+8);
2337c478bd9Sstevel@tonic-gate 		}
234*8509e9caSToomas Soome 		while (getmntent(mtab, &mntp) == 0) {
2357c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) == 0 &&
2367c478bd9Sstevel@tonic-gate 			    !hasmntopt(&mntp, MNTOPT_RO) &&
2377c478bd9Sstevel@tonic-gate 			    (oneof(mntp.mnt_special, listp, listcnt) ||
2387c478bd9Sstevel@tonic-gate 			    oneof(mntp.mnt_mountp, listp, listcnt))) {
2397c478bd9Sstevel@tonic-gate 				(void) snprintf(quotafile, sizeof (quotafile),
2407c478bd9Sstevel@tonic-gate 					"%s/%s", mntp.mnt_mountp, QFNAME);
2417c478bd9Sstevel@tonic-gate 				errs +=
2427c478bd9Sstevel@tonic-gate 				    chkquota(mntp.mnt_special,
2437c478bd9Sstevel@tonic-gate 					mntp.mnt_mountp, quotafile);
2447c478bd9Sstevel@tonic-gate 			}
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 		fclose(mtab);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	while (listcnt--) {
2497c478bd9Sstevel@tonic-gate 		if (*listp) {
2507c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Cannot check %s\n", *listp);
2517c478bd9Sstevel@tonic-gate 			errs++;
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		listp++;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	if (errs > 0)
2567c478bd9Sstevel@tonic-gate 		errs += 31;
257d1a180b0Smaheshvs 	return (errs);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
260d1a180b0Smaheshvs struct active {
2617c478bd9Sstevel@tonic-gate 	char *rdev;
2627c478bd9Sstevel@tonic-gate 	pid_t pid;
2637c478bd9Sstevel@tonic-gate 	struct active *nxt;
2647c478bd9Sstevel@tonic-gate };
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate int
preen(int listcnt,char ** listp)267d1a180b0Smaheshvs preen(int listcnt, char **listp)
2687c478bd9Sstevel@tonic-gate {
269d1a180b0Smaheshvs 	int i, rc, errs;
270d1a180b0Smaheshvs 	char **lp, *rdev, *bdev;
2717c478bd9Sstevel@tonic-gate 	extern char *getfullrawname(), *getfullblkname();
2727c478bd9Sstevel@tonic-gate 	struct mnttab mntp, mpref;
2737c478bd9Sstevel@tonic-gate 	struct active *alist, *ap;
2747c478bd9Sstevel@tonic-gate 	FILE *mtab;
2757c478bd9Sstevel@tonic-gate 	char quotafile[MAXPATHLEN];
2767c478bd9Sstevel@tonic-gate 	char name[MAXPATHLEN];
2777c478bd9Sstevel@tonic-gate 	int nactive, serially;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if ((mtab = fopen(MNTTAB, "r")) == NULL) {
2807c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Can't open ");
2817c478bd9Sstevel@tonic-gate 		perror(MNTTAB);
2827c478bd9Sstevel@tonic-gate 		exit(31+8);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	memset(&mpref, 0, sizeof (struct mnttab));
2857c478bd9Sstevel@tonic-gate 	errs = 0;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	for (lp = listp, i = 0; i < listcnt; lp++, i++) {
2887c478bd9Sstevel@tonic-gate 		serially = 0;
2897c478bd9Sstevel@tonic-gate 		rdev = getfullrawname(*lp);
2907c478bd9Sstevel@tonic-gate 		if (rdev == NULL || *rdev == '\0') {
2917c478bd9Sstevel@tonic-gate 			fprintf(stderr, "can't get rawname for `%s'\n", *lp);
2927c478bd9Sstevel@tonic-gate 			serially = 1;
2937c478bd9Sstevel@tonic-gate 		} else if (preen_addev(rdev) != 0) {
2947c478bd9Sstevel@tonic-gate 			fprintf(stderr, "preen_addev error\n");
2957c478bd9Sstevel@tonic-gate 			serially = 1;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		if (rdev != NULL)
2997c478bd9Sstevel@tonic-gate 			free(rdev);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		if (serially) {
3027c478bd9Sstevel@tonic-gate 			rewind(mtab);
3037c478bd9Sstevel@tonic-gate 			mpref.mnt_special = *lp;
3047c478bd9Sstevel@tonic-gate 			if (getmntany(mtab, &mntp, &mpref) == 0 &&
3057c478bd9Sstevel@tonic-gate 			    strcmp(mntp.mnt_fstype, MNTTYPE_UFS) == 0 &&
3067c478bd9Sstevel@tonic-gate 			    !hasmntopt(&mntp, MNTOPT_RO)) {
3077c478bd9Sstevel@tonic-gate 				errs += (31+chkquota(mntp.mnt_special,
3087c478bd9Sstevel@tonic-gate 				    mntp.mnt_mountp, quotafile));
3097c478bd9Sstevel@tonic-gate 				*lp = (char *)0;
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	nactive = 0;
3157c478bd9Sstevel@tonic-gate 	alist = NULL;
3167c478bd9Sstevel@tonic-gate 	while ((rc = preen_getdev(name)) > 0) {
3177c478bd9Sstevel@tonic-gate 		switch (rc) {
3187c478bd9Sstevel@tonic-gate 		case 1:
3197c478bd9Sstevel@tonic-gate 			bdev = getfullblkname(name);
3207c478bd9Sstevel@tonic-gate 			if (bdev == NULL || *bdev == '\0') {
3217c478bd9Sstevel@tonic-gate 				fprintf(stderr, "can't get blkname for `%s'\n",
3227c478bd9Sstevel@tonic-gate 				    name);
3237c478bd9Sstevel@tonic-gate 				if (bdev)
3247c478bd9Sstevel@tonic-gate 					free(bdev);
3257c478bd9Sstevel@tonic-gate 				continue;
3267c478bd9Sstevel@tonic-gate 			}
3277c478bd9Sstevel@tonic-gate 			rewind(mtab);
3287c478bd9Sstevel@tonic-gate 			mpref.mnt_special = bdev;
3297c478bd9Sstevel@tonic-gate 			if (getmntany(mtab, &mntp, &mpref) != 0) {
3307c478bd9Sstevel@tonic-gate 				fprintf(stderr, "`%s' not mounted?\n", name);
3317c478bd9Sstevel@tonic-gate 				preen_releasedev(name);
3327c478bd9Sstevel@tonic-gate 				free(bdev);
3337c478bd9Sstevel@tonic-gate 				continue;
3347c478bd9Sstevel@tonic-gate 			} else if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0 ||
3357c478bd9Sstevel@tonic-gate 			    hasmntopt(&mntp, MNTOPT_RO) ||
3367c478bd9Sstevel@tonic-gate 			    (!oneof(mntp.mnt_special, listp, listcnt) &&
3377c478bd9Sstevel@tonic-gate 			    !oneof(mntp.mnt_mountp, listp, listcnt))) {
3387c478bd9Sstevel@tonic-gate 				preen_releasedev(name);
3397c478bd9Sstevel@tonic-gate 				free(bdev);
3407c478bd9Sstevel@tonic-gate 				continue;
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 			free(bdev);
3437c478bd9Sstevel@tonic-gate 			ap = (struct active *)malloc(sizeof (struct active));
3447c478bd9Sstevel@tonic-gate 			if (ap == NULL) {
3457c478bd9Sstevel@tonic-gate 				fprintf(stderr, "out of memory\n");
3467c478bd9Sstevel@tonic-gate 				exit(31+8);
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 			ap->rdev = (char *)strdup(name);
3497c478bd9Sstevel@tonic-gate 			if (ap->rdev == NULL) {
3507c478bd9Sstevel@tonic-gate 				fprintf(stderr, "out of memory\n");
3517c478bd9Sstevel@tonic-gate 				exit(31+8);
3527c478bd9Sstevel@tonic-gate 			}
3537c478bd9Sstevel@tonic-gate 			ap->nxt = alist;
3547c478bd9Sstevel@tonic-gate 			alist = ap;
3557c478bd9Sstevel@tonic-gate 			switch (ap->pid = fork()) {
3567c478bd9Sstevel@tonic-gate 			case -1:
3577c478bd9Sstevel@tonic-gate 				perror("fork");
3587c478bd9Sstevel@tonic-gate 				exit(31+8);
3597c478bd9Sstevel@tonic-gate 				break;
3607c478bd9Sstevel@tonic-gate 			case 0:
3617c478bd9Sstevel@tonic-gate 				(void) snprintf(quotafile, sizeof (quotafile),
3627c478bd9Sstevel@tonic-gate 					"%s/%s", mntp.mnt_mountp, QFNAME);
3637c478bd9Sstevel@tonic-gate 				exit(31+chkquota(mntp.mnt_special,
3647c478bd9Sstevel@tonic-gate 				    mntp.mnt_mountp, quotafile));
3657c478bd9Sstevel@tonic-gate 				break;
3667c478bd9Sstevel@tonic-gate 			default:
3677c478bd9Sstevel@tonic-gate 				nactive++;
3687c478bd9Sstevel@tonic-gate 				break;
3697c478bd9Sstevel@tonic-gate 			}
3707c478bd9Sstevel@tonic-gate 			break;
3717c478bd9Sstevel@tonic-gate 		case 2:
3727c478bd9Sstevel@tonic-gate 			errs += waiter(&alist);
3737c478bd9Sstevel@tonic-gate 			nactive--;
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	fclose(mtab);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	while (nactive > 0) {
3807c478bd9Sstevel@tonic-gate 		errs += waiter(&alist);
3817c478bd9Sstevel@tonic-gate 		nactive--;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	return (errs);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate int
waiter(struct active ** alp)387d1a180b0Smaheshvs waiter(struct active **alp)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	pid_t curpid;
3907c478bd9Sstevel@tonic-gate 	int status;
391d1a180b0Smaheshvs 	struct active *ap, *lap;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	curpid = wait(&status);
3947c478bd9Sstevel@tonic-gate 	if (curpid == -1) {
3957c478bd9Sstevel@tonic-gate 		if (errno == ECHILD)
3967c478bd9Sstevel@tonic-gate 			return (0);
3977c478bd9Sstevel@tonic-gate 		perror("wait");
3987c478bd9Sstevel@tonic-gate 		exit(31+8);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	for (lap = NULL, ap = *alp; ap != NULL; lap = ap, ap = ap->nxt) {
4027c478bd9Sstevel@tonic-gate 		if (ap->pid == curpid)
4037c478bd9Sstevel@tonic-gate 			break;
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (ap == NULL) {
4077c478bd9Sstevel@tonic-gate 		fprintf(stderr, "wait returns unknown pid\n");
4087c478bd9Sstevel@tonic-gate 		exit(31+8);
4097c478bd9Sstevel@tonic-gate 	} else if (lap) {
4107c478bd9Sstevel@tonic-gate 		lap->nxt = ap->nxt;
4117c478bd9Sstevel@tonic-gate 	} else {
4127c478bd9Sstevel@tonic-gate 		*alp = ap->nxt;
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 	preen_releasedev(ap->rdev);
4157c478bd9Sstevel@tonic-gate 	free(ap->rdev);
4167c478bd9Sstevel@tonic-gate 	free(ap);
4177c478bd9Sstevel@tonic-gate 	return (WHIBYTE(status));
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate int
chkquota(char * fsdev,char * fsfile,char * qffile)421d1a180b0Smaheshvs chkquota(char *fsdev, char *fsfile, char *qffile)
4227c478bd9Sstevel@tonic-gate {
423d1a180b0Smaheshvs 	struct fileusage *fup;
4247c478bd9Sstevel@tonic-gate 	dev_t quotadev;
4257c478bd9Sstevel@tonic-gate 	FILE *qf;
4267c478bd9Sstevel@tonic-gate 	uid_t uid;
4277c478bd9Sstevel@tonic-gate 	struct passwd *pw;
4287c478bd9Sstevel@tonic-gate 	int cg, i;
4297c478bd9Sstevel@tonic-gate 	char *rawdisk;
4307c478bd9Sstevel@tonic-gate 	struct stat64 statb;
4317c478bd9Sstevel@tonic-gate 	struct dqblk dqbuf;
4327c478bd9Sstevel@tonic-gate 	extern char *getfullrawname();
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if ((rawdisk = getfullrawname(fsdev)) == NULL) {
4357c478bd9Sstevel@tonic-gate 		fprintf(stderr, "malloc failed\n");
4367c478bd9Sstevel@tonic-gate 		return (1);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (*rawdisk == '\0') {
4407c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Could not find character device for %s\n",
4417c478bd9Sstevel@tonic-gate 		    fsdev);
4427c478bd9Sstevel@tonic-gate 		return (1);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (vflag)
4467c478bd9Sstevel@tonic-gate 		printf("*** Checking quotas for %s (%s)\n", rawdisk, fsfile);
4477c478bd9Sstevel@tonic-gate 	fi = open64(rawdisk, 0);
4487c478bd9Sstevel@tonic-gate 	if (fi < 0) {
4497c478bd9Sstevel@tonic-gate 		perror(rawdisk);
4507c478bd9Sstevel@tonic-gate 		return (1);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	qf = fopen64(qffile, "r+");
4537c478bd9Sstevel@tonic-gate 	if (qf == NULL) {
4547c478bd9Sstevel@tonic-gate 		perror(qffile);
4557c478bd9Sstevel@tonic-gate 		close(fi);
4567c478bd9Sstevel@tonic-gate 		return (1);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 	if (fstat64(fileno(qf), &statb) < 0) {
4597c478bd9Sstevel@tonic-gate 		perror(qffile);
4607c478bd9Sstevel@tonic-gate 		fclose(qf);
4617c478bd9Sstevel@tonic-gate 		close(fi);
4627c478bd9Sstevel@tonic-gate 		return (1);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 	quotadev = statb.st_dev;
4657c478bd9Sstevel@tonic-gate 	if (stat64(fsdev, &statb) < 0) {
4667c478bd9Sstevel@tonic-gate 		perror(fsdev);
4677c478bd9Sstevel@tonic-gate 		fclose(qf);
4687c478bd9Sstevel@tonic-gate 		close(fi);
4697c478bd9Sstevel@tonic-gate 		return (1);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	if (quotadev != statb.st_rdev) {
4727c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s dev (0x%x) mismatch %s dev (0x%x)\n",
4737c478bd9Sstevel@tonic-gate 			qffile, quotadev, fsdev, statb.st_rdev);
4747c478bd9Sstevel@tonic-gate 		fclose(qf);
4757c478bd9Sstevel@tonic-gate 		close(fi);
4767c478bd9Sstevel@tonic-gate 		return (1);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (char *)&sblock, SBSIZE);
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Flush filesystem since we are going to read
4827c478bd9Sstevel@tonic-gate 	 * disk raw and we want to make sure everything is
4837c478bd9Sstevel@tonic-gate 	 * synced to disk before we read it.
4847c478bd9Sstevel@tonic-gate 	 */
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(qf), _FIOFFS, NULL) == -1) {
4877c478bd9Sstevel@tonic-gate 		perror(qffile);
4887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot flush file system.\n",
4897c478bd9Sstevel@tonic-gate 				qffile);
4907c478bd9Sstevel@tonic-gate 		(void) fclose(qf);
4917c478bd9Sstevel@tonic-gate 		return (1);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/*
4957c478bd9Sstevel@tonic-gate 	 * no need to quotacheck a rw, mounted, and logging file system
4967c478bd9Sstevel@tonic-gate 	 */
4977c478bd9Sstevel@tonic-gate 	if ((fflag == 0) && pflag &&
4987c478bd9Sstevel@tonic-gate 	    (FSOKAY == (sblock.fs_state + sblock.fs_time)) &&
4997c478bd9Sstevel@tonic-gate 	    (sblock.fs_clean == FSLOG)) {
5007c478bd9Sstevel@tonic-gate 		fclose(qf);
5017c478bd9Sstevel@tonic-gate 		close(fi);
5027c478bd9Sstevel@tonic-gate 		return (0);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	ino = 0;
5057c478bd9Sstevel@tonic-gate 	for (cg = 0; cg < sblock.fs_ncg; cg++) {
5067c478bd9Sstevel@tonic-gate 		dp = NULL;
5077c478bd9Sstevel@tonic-gate 		for (i = 0; i < sblock.fs_ipg; i++)
5087c478bd9Sstevel@tonic-gate 			acct(ginode());
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 	for (uid = 0; uid <= MAXUID && uid >= 0; uid++) {
5117c478bd9Sstevel@tonic-gate 		(void) fread(&dqbuf, sizeof (struct dqblk), 1, qf);
5127c478bd9Sstevel@tonic-gate 		if (feof(qf))
5137c478bd9Sstevel@tonic-gate 			break;
5147c478bd9Sstevel@tonic-gate 		fup = lookup(uid);
5157c478bd9Sstevel@tonic-gate 		if (fup == 0)
5167c478bd9Sstevel@tonic-gate 			fup = &zerofileusage;
5177c478bd9Sstevel@tonic-gate 		if (dqbuf.dqb_bhardlimit == 0 && dqbuf.dqb_bsoftlimit == 0 &&
5187c478bd9Sstevel@tonic-gate 		    dqbuf.dqb_fhardlimit == 0 && dqbuf.dqb_fsoftlimit == 0) {
5197c478bd9Sstevel@tonic-gate 			fup->fu_curfiles = 0;
5207c478bd9Sstevel@tonic-gate 			fup->fu_curblocks = 0;
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 		if (dqbuf.dqb_curfiles == fup->fu_curfiles &&
5237c478bd9Sstevel@tonic-gate 		    dqbuf.dqb_curblocks == fup->fu_curblocks) {
5247c478bd9Sstevel@tonic-gate 			fup->fu_curfiles = 0;
5257c478bd9Sstevel@tonic-gate 			fup->fu_curblocks = 0;
5267c478bd9Sstevel@tonic-gate 			continue;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		/*
5297c478bd9Sstevel@tonic-gate 		 * The maximum number of blocks that can be stored in the
5307c478bd9Sstevel@tonic-gate 		 * dqb_curblocks field in the quota record is 2^32 - 1,
5317c478bd9Sstevel@tonic-gate 		 * since it must fit into an unsigned 32-bit quantity.
5327c478bd9Sstevel@tonic-gate 		 * If this user has more blocks than that, print a message
5337c478bd9Sstevel@tonic-gate 		 * to that effect and reduce the count of allocated blocks
5347c478bd9Sstevel@tonic-gate 		 * to the maximum value, which is UINT_MAX.
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 		if (fup->fu_curblocks > UINT_MAX) {
5377c478bd9Sstevel@tonic-gate 			if (pflag || aflag)
5387c478bd9Sstevel@tonic-gate 				printf("%s: ", rawdisk);
5397c478bd9Sstevel@tonic-gate 			printf("512-byte blocks allocated to user ");
5407c478bd9Sstevel@tonic-gate 			if ((pw = getpwuid(uid)) && pw->pw_name[0])
5417c478bd9Sstevel@tonic-gate 				printf("%-10s ", pw->pw_name);
5427c478bd9Sstevel@tonic-gate 			else
5437c478bd9Sstevel@tonic-gate 				printf("#%-9d ", uid);
5447c478bd9Sstevel@tonic-gate 			printf(" = %lld\n", fup->fu_curblocks);
5457c478bd9Sstevel@tonic-gate 			printf(
5467c478bd9Sstevel@tonic-gate "This exceeds the maximum number of blocks recordable in a quota record.\n");
5477c478bd9Sstevel@tonic-gate 			printf(
5487c478bd9Sstevel@tonic-gate "The value will be set to the maximum, which is %lu.\n", UINT_MAX);
5497c478bd9Sstevel@tonic-gate 			fup->fu_curblocks = UINT_MAX;
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		if (vflag) {
5537c478bd9Sstevel@tonic-gate 			if (pflag || aflag)
5547c478bd9Sstevel@tonic-gate 				printf("%s: ", rawdisk);
5557c478bd9Sstevel@tonic-gate 			if ((pw = getpwuid(uid)) && pw->pw_name[0])
5567c478bd9Sstevel@tonic-gate 				printf("%-10s fixed:", pw->pw_name);
5577c478bd9Sstevel@tonic-gate 			else
5587c478bd9Sstevel@tonic-gate 				printf("#%-9d fixed:", uid);
5597c478bd9Sstevel@tonic-gate 			if (dqbuf.dqb_curfiles != fup->fu_curfiles)
5607c478bd9Sstevel@tonic-gate 				printf("  files %lu -> %lu",
5617c478bd9Sstevel@tonic-gate 				    dqbuf.dqb_curfiles, fup->fu_curfiles);
5627c478bd9Sstevel@tonic-gate 			if (dqbuf.dqb_curblocks != fup->fu_curblocks)
5637c478bd9Sstevel@tonic-gate 				printf("  blocks %lu -> %llu",
5647c478bd9Sstevel@tonic-gate 				    dqbuf.dqb_curblocks, fup->fu_curblocks);
5657c478bd9Sstevel@tonic-gate 			printf("\n");
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 		dqbuf.dqb_curfiles = fup->fu_curfiles;
5687c478bd9Sstevel@tonic-gate 		dqbuf.dqb_curblocks = fup->fu_curblocks;
5697c478bd9Sstevel@tonic-gate 		/*
5707c478bd9Sstevel@tonic-gate 		 * If quotas are not enabled for the current filesystem
5717c478bd9Sstevel@tonic-gate 		 * then just update the quotas file directly.
5727c478bd9Sstevel@tonic-gate 		 */
5737c478bd9Sstevel@tonic-gate 		if ((quotactl(Q_SETQUOTA, fsfile, uid, &dqbuf) < 0) &&
5747c478bd9Sstevel@tonic-gate 		    (errno == ESRCH)) {
5757c478bd9Sstevel@tonic-gate 			/* back up, overwrite the entry we just read */
5767c478bd9Sstevel@tonic-gate 			(void) fseeko64(qf, (offset_t)dqoff(uid), 0);
5777c478bd9Sstevel@tonic-gate 			(void) fwrite(&dqbuf, sizeof (struct dqblk), 1, qf);
5787c478bd9Sstevel@tonic-gate 			(void) fflush(qf);
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 		fup->fu_curfiles = 0;
5817c478bd9Sstevel@tonic-gate 		fup->fu_curblocks = 0;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	(void) fflush(qf);
5847c478bd9Sstevel@tonic-gate 	(void) fsync(fileno(qf));
5857c478bd9Sstevel@tonic-gate 	fclose(qf);
5867c478bd9Sstevel@tonic-gate 	close(fi);
5877c478bd9Sstevel@tonic-gate 	return (0);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate void
acct(struct dinode * ip)591d1a180b0Smaheshvs acct(struct dinode *ip)
5927c478bd9Sstevel@tonic-gate {
593d1a180b0Smaheshvs 	struct fileusage *fup;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (ip == NULL)
5967c478bd9Sstevel@tonic-gate 		return;
5977c478bd9Sstevel@tonic-gate 	ip->di_mode = ip->di_smode;
5987c478bd9Sstevel@tonic-gate 	if (ip->di_suid != UID_LONG) {
5997c478bd9Sstevel@tonic-gate 		ip->di_uid = ip->di_suid;
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	if (ip->di_mode == 0)
6027c478bd9Sstevel@tonic-gate 		return;
6037c478bd9Sstevel@tonic-gate 	fup = adduid(ip->di_uid);
6047c478bd9Sstevel@tonic-gate 	fup->fu_curfiles++;
6057c478bd9Sstevel@tonic-gate 	if ((ip->di_mode & IFMT) == IFCHR || (ip->di_mode & IFMT) == IFBLK)
6067c478bd9Sstevel@tonic-gate 		return;
6077c478bd9Sstevel@tonic-gate 	fup->fu_curblocks += ip->di_blocks;
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate int
oneof(char * target,char ** olistp,int on)611d1a180b0Smaheshvs oneof(char *target, char **olistp, int on)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	char **listp = olistp;
6147c478bd9Sstevel@tonic-gate 	int n = on;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	while (n--) {
6177c478bd9Sstevel@tonic-gate 		if (*listp && strcmp(target, *listp) == 0) {
6187c478bd9Sstevel@tonic-gate 			*listp = (char *)0;
6197c478bd9Sstevel@tonic-gate 			return (1);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		listp++;
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 	return (0);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate struct dinode *
ginode()6277c478bd9Sstevel@tonic-gate ginode()
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	ulong_t iblk;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (dp == NULL || ++dp >= &itab[ITABSZ]) {
6327c478bd9Sstevel@tonic-gate 		iblk = itod(&sblock, ino);
6337c478bd9Sstevel@tonic-gate 		bread(fsbtodb(&sblock, iblk),
6347c478bd9Sstevel@tonic-gate 		    (char *)itab, sizeof (itab));
6357c478bd9Sstevel@tonic-gate 		dp = &itab[(int)ino % (int)INOPB(&sblock)];
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 	if (ino++ < UFSROOTINO)
6387c478bd9Sstevel@tonic-gate 		return (NULL);
6397c478bd9Sstevel@tonic-gate 	return (dp);
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate void
bread(diskaddr_t bno,char * buf,int cnt)6437c478bd9Sstevel@tonic-gate bread(diskaddr_t bno, char *buf, int cnt)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	extern offset_t llseek();
6467c478bd9Sstevel@tonic-gate 	offset_t pos;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	pos = (offset_t)bno * DEV_BSIZE;
6497c478bd9Sstevel@tonic-gate 	if (llseek(fi, pos, 0) != pos) {
6507c478bd9Sstevel@tonic-gate 		perror("lseek");
6517c478bd9Sstevel@tonic-gate 		exit(31+1);
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 	if (read(fi, buf, cnt) != cnt) {
6547c478bd9Sstevel@tonic-gate 		perror("read");
6557c478bd9Sstevel@tonic-gate 		exit(31+1);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate struct fileusage *
lookup(uid_t uid)6607c478bd9Sstevel@tonic-gate lookup(uid_t uid)
6617c478bd9Sstevel@tonic-gate {
662d1a180b0Smaheshvs 	struct fileusage *fup;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	for (fup = fuhead[uid % FUHASH]; fup != 0; fup = fup->fu_next)
6657c478bd9Sstevel@tonic-gate 		if (fup->fu_uid == uid)
6667c478bd9Sstevel@tonic-gate 			return (fup);
6677c478bd9Sstevel@tonic-gate 	return ((struct fileusage *)0);
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate struct fileusage *
adduid(uid_t uid)6717c478bd9Sstevel@tonic-gate adduid(uid_t uid)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	struct fileusage *fup, **fhp;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	fup = lookup(uid);
6767c478bd9Sstevel@tonic-gate 	if (fup != 0)
6777c478bd9Sstevel@tonic-gate 		return (fup);
6787c478bd9Sstevel@tonic-gate 	fup = (struct fileusage *)calloc(1, sizeof (struct fileusage));
6797c478bd9Sstevel@tonic-gate 	if (fup == 0) {
6807c478bd9Sstevel@tonic-gate 		fprintf(stderr, "out of memory for fileusage structures\n");
6817c478bd9Sstevel@tonic-gate 		exit(31+1);
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 	fhp = &fuhead[uid % FUHASH];
6847c478bd9Sstevel@tonic-gate 	fup->fu_next = *fhp;
6857c478bd9Sstevel@tonic-gate 	*fhp = fup;
6867c478bd9Sstevel@tonic-gate 	fup->fu_uid = uid;
6877c478bd9Sstevel@tonic-gate 	return (fup);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate void
usage()6917c478bd9Sstevel@tonic-gate usage()
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	fprintf(stderr, "ufs usage:\n");
6947c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\tquotacheck [-v] [-f] [-p] -a\n");
6957c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\tquotacheck [-v] [-f] [-p] filesys ...\n");
6967c478bd9Sstevel@tonic-gate 	exit(31+1);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate int
quotactl(int cmd,char * mountp,uid_t uid,caddr_t addr)700d1a180b0Smaheshvs quotactl(int cmd, char *mountp, uid_t uid, caddr_t addr)
7017c478bd9Sstevel@tonic-gate {
702*8509e9caSToomas Soome 	int		fd;
703*8509e9caSToomas Soome 	int		status;
7047c478bd9Sstevel@tonic-gate 	struct quotctl	quota;
7057c478bd9Sstevel@tonic-gate 	char		qfile[MAXPATHLEN];
7067c478bd9Sstevel@tonic-gate 	FILE		*fstab;
7077c478bd9Sstevel@tonic-gate 	struct mnttab	mntp;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if ((mountp == NULL) && (cmd == Q_ALLSYNC)) {
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * Find the mount point of any ufs file system.   This is
7137c478bd9Sstevel@tonic-gate 		 * because the ioctl that implements the quotactl call has
7147c478bd9Sstevel@tonic-gate 		 * to go to a real file, and not to the block device.
7157c478bd9Sstevel@tonic-gate 		 */
7167c478bd9Sstevel@tonic-gate 		if ((fstab = fopen(MNTTAB, "r")) == NULL) {
7177c478bd9Sstevel@tonic-gate 			fprintf(stderr, "%s: ", MNTTAB);
7187c478bd9Sstevel@tonic-gate 			perror("open");
7197c478bd9Sstevel@tonic-gate 			exit(31+1);
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 		fd = -1;
722*8509e9caSToomas Soome 		while ((status = getmntent(fstab, &mntp)) == 0) {
7237c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0 ||
7247c478bd9Sstevel@tonic-gate 				hasmntopt(&mntp, MNTOPT_RO))
7257c478bd9Sstevel@tonic-gate 				continue;
7267c478bd9Sstevel@tonic-gate 			if ((strlcpy(qfile, mntp.mnt_mountp,
7277c478bd9Sstevel@tonic-gate 				sizeof (qfile)) >= sizeof (qfile)) ||
7287c478bd9Sstevel@tonic-gate 			    (strlcat(qfile, "/" QFNAME, sizeof (qfile)) >=
7297c478bd9Sstevel@tonic-gate 				sizeof (qfile))) {
7307c478bd9Sstevel@tonic-gate 				continue;
7317c478bd9Sstevel@tonic-gate 			}
7327c478bd9Sstevel@tonic-gate 			if ((fd = open64(qfile, O_RDWR)) == -1)
7337c478bd9Sstevel@tonic-gate 				break;
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 		fclose(fstab);
7367c478bd9Sstevel@tonic-gate 		if (fd == -1) {
7377c478bd9Sstevel@tonic-gate 			errno = ENOENT;
7387c478bd9Sstevel@tonic-gate 			return (-1);
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 	} else {
7417c478bd9Sstevel@tonic-gate 		if (mountp == NULL || mountp[0] == '\0') {
7427c478bd9Sstevel@tonic-gate 			errno =  ENOENT;
7437c478bd9Sstevel@tonic-gate 			return (-1);
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 		if ((strlcpy(qfile, mountp, sizeof (qfile)) >=
7467c478bd9Sstevel@tonic-gate 			sizeof (qfile)) ||
7477c478bd9Sstevel@tonic-gate 		    (strlcat(qfile, "/" QFNAME, sizeof (qfile)) >=
7487c478bd9Sstevel@tonic-gate 			sizeof (qfile))) {
7497c478bd9Sstevel@tonic-gate 			errno = ENOENT;
7507c478bd9Sstevel@tonic-gate 			return (-1);
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 		if ((fd = open64(qfile, O_RDWR)) < 0) {
7537c478bd9Sstevel@tonic-gate 			fprintf(stderr, "quotactl: ");
7547c478bd9Sstevel@tonic-gate 			perror("open");
7557c478bd9Sstevel@tonic-gate 			exit(31+1);
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 	}	/* else */
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	quota.op = cmd;
7607c478bd9Sstevel@tonic-gate 	quota.uid = uid;
7617c478bd9Sstevel@tonic-gate 	quota.addr = addr;
7627c478bd9Sstevel@tonic-gate 	status = ioctl(fd, Q_QUOTACTL, &quota);
7637c478bd9Sstevel@tonic-gate 	if (fd != 0)
7647c478bd9Sstevel@tonic-gate 		close(fd);
7657c478bd9Sstevel@tonic-gate 	return (status);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate char *
hasvfsopt(struct vfstab * vfs,char * opt)769d1a180b0Smaheshvs hasvfsopt(struct vfstab *vfs, char *opt)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	char *f, *opts;
7727c478bd9Sstevel@tonic-gate 	static char *tmpopts;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	if (tmpopts == 0) {
7757c478bd9Sstevel@tonic-gate 		tmpopts = (char *)calloc(256, sizeof (char));
7767c478bd9Sstevel@tonic-gate 		if (tmpopts == 0)
7777c478bd9Sstevel@tonic-gate 			return (0);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	strcpy(tmpopts, vfs->vfs_mntopts);
7807c478bd9Sstevel@tonic-gate 	opts = tmpopts;
7817c478bd9Sstevel@tonic-gate 	f = mntopt(&opts);
7827c478bd9Sstevel@tonic-gate 	for (; *f; f = mntopt(&opts)) {
7837c478bd9Sstevel@tonic-gate 		if (strncmp(opt, f, strlen(opt)) == 0)
7847c478bd9Sstevel@tonic-gate 			return (f - tmpopts + vfs->vfs_mntopts);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	return (NULL);
7877c478bd9Sstevel@tonic-gate }
788