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