xref: /onnv-gate/usr/src/cmd/fs.d/ufs/ncheck/ncheck.c (revision 1051:ce6da8f4b892)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1051Smaheshvs  * Common Development and Distribution License (the "License").
6*1051Smaheshvs  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22757Svsakar  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * ncheck -- obtain file names from reading filesystem
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	MAXNINDIR	(MAXBSIZE / sizeof (daddr_t))
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <sys/types.h>
490Sstevel@tonic-gate #include <sys/vnode.h>
500Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
510Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
520Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
530Sstevel@tonic-gate #include <stdio.h>
540Sstevel@tonic-gate #include <stdlib.h>
550Sstevel@tonic-gate #include <string.h>
560Sstevel@tonic-gate #include <errno.h>
570Sstevel@tonic-gate #include <fcntl.h>
580Sstevel@tonic-gate #include <unistd.h>
590Sstevel@tonic-gate #include "roll_log.h"
600Sstevel@tonic-gate 
610Sstevel@tonic-gate union {
620Sstevel@tonic-gate 	struct	fs	sblk;
630Sstevel@tonic-gate 	char xxx[SBSIZE];	/* because fs is variable length */
640Sstevel@tonic-gate } real_fs;
650Sstevel@tonic-gate #define	sblock real_fs.sblk
660Sstevel@tonic-gate 
670Sstevel@tonic-gate struct	dinode	*itab;
680Sstevel@tonic-gate unsigned itab_size;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 
710Sstevel@tonic-gate struct 	dinode	*gip;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* inode list */
740Sstevel@tonic-gate struct ilist {
750Sstevel@tonic-gate 	ino_t	ino;
760Sstevel@tonic-gate 	ushort_t	mode;
770Sstevel@tonic-gate 	uid_t	uid;
780Sstevel@tonic-gate 	gid_t	gid;
790Sstevel@tonic-gate } *ilist;
800Sstevel@tonic-gate int ilist_size = 0;	/* size of ilist[] */
810Sstevel@tonic-gate int ilist_index = 0;	/* current index for storing into ilist; */
820Sstevel@tonic-gate #define	ILIST_SZ_INCR	1000	/* initial size, amount to incr sz of ilist */
830Sstevel@tonic-gate #define	MAX_ILIST_INDEX()	(ilist_size - 1)
840Sstevel@tonic-gate 
850Sstevel@tonic-gate struct	htab
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	ino_t	h_ino;
880Sstevel@tonic-gate 	ino_t	h_pino;
890Sstevel@tonic-gate 	int	h_name_index;		/* index into string table */
900Sstevel@tonic-gate } *htab;
910Sstevel@tonic-gate unsigned htab_size;		/* how much malloc'd for htab */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * string table: used to hold filenames.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate char *strngtab;
970Sstevel@tonic-gate int strngloc;
980Sstevel@tonic-gate int strngtab_size;
990Sstevel@tonic-gate #define	STRNGTAB_INCR	(1024*16)	/* amount to grow strngtab */
1000Sstevel@tonic-gate #define	MAX_STRNGTAB_INDEX()	(strngtab_size - 1)
1010Sstevel@tonic-gate #define	AVG_PATH_LEN	30		/* average (?) length of name */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate long hsize;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate struct dirstuff {
1060Sstevel@tonic-gate 	int loc;
1070Sstevel@tonic-gate 	struct dinode *ip;
1080Sstevel@tonic-gate 	char dbuf[MAXBSIZE];
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate int	aflg = 0;
1130Sstevel@tonic-gate int	sflg = 0;
1140Sstevel@tonic-gate int	iflg = 0; /* number of inodes being searched for */
1150Sstevel@tonic-gate int	mflg = 0;
1160Sstevel@tonic-gate int	fi;
1170Sstevel@tonic-gate ino_t	ino;
1180Sstevel@tonic-gate int	nhent;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate int	nerror;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate long	atol();
123*1051Smaheshvs daddr_t	bmap(daddr_t);
1240Sstevel@tonic-gate void	bread(diskaddr_t bno, char *buf, int cnt);
1250Sstevel@tonic-gate void	check(char *file);
1260Sstevel@tonic-gate int	dotname(struct direct *dp);
1270Sstevel@tonic-gate offset_t llseek();
1280Sstevel@tonic-gate struct htab *lookup(ino_t i, int ef);
1290Sstevel@tonic-gate void	pass1(struct dinode *ip);
1300Sstevel@tonic-gate void	pass2(struct dinode *ip);
1310Sstevel@tonic-gate void	pass3(struct dinode *ip);
1320Sstevel@tonic-gate void	pname(ino_t i, int lev);
1330Sstevel@tonic-gate char 	*strcpy();
1340Sstevel@tonic-gate void	usage();
1350Sstevel@tonic-gate struct direct *dreaddir();
1360Sstevel@tonic-gate void extend_ilist();
1370Sstevel@tonic-gate int extend_strngtab(unsigned int size);
1380Sstevel@tonic-gate uchar_t *extend_tbl(uchar_t *tbl, unsigned int *current_size,
1390Sstevel@tonic-gate 	unsigned int new_size);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate extern int	optind;
1420Sstevel@tonic-gate extern char	*optarg;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate char *subopts [] = {
1450Sstevel@tonic-gate #define	M_FLAG		0
1460Sstevel@tonic-gate 	"m",
1470Sstevel@tonic-gate 	NULL
1480Sstevel@tonic-gate 	};
1490Sstevel@tonic-gate 
150*1051Smaheshvs int
main(int argc,char * argv[])151*1051Smaheshvs main(int argc, char *argv[])
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	long n;
1540Sstevel@tonic-gate 	int	opt;
1550Sstevel@tonic-gate 	char	*suboptions,	*value;
1560Sstevel@tonic-gate 	int	suboption;
1570Sstevel@tonic-gate 	char	*p;
1580Sstevel@tonic-gate 	int	first = 0;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	extend_ilist();
1610Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "ao:i:s")) != EOF) {
1620Sstevel@tonic-gate 		switch (opt) {
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		case 'a':
1650Sstevel@tonic-gate 			aflg++;
1660Sstevel@tonic-gate 			break;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 		case 'o':
1690Sstevel@tonic-gate 			/*
1700Sstevel@tonic-gate 			 * ufs specific options.
1710Sstevel@tonic-gate 			 */
1720Sstevel@tonic-gate 			suboptions = optarg;
1730Sstevel@tonic-gate 			while (*suboptions != '\0') {
1740Sstevel@tonic-gate 				suboption = getsubopt(&suboptions,
1750Sstevel@tonic-gate 					subopts, &value);
1760Sstevel@tonic-gate 				switch (suboption) {
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 				case M_FLAG:
1790Sstevel@tonic-gate 					mflg++;
1800Sstevel@tonic-gate 					break;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 				default:
1830Sstevel@tonic-gate 					usage();
1840Sstevel@tonic-gate 				}
1850Sstevel@tonic-gate 			}
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		case 'i':
1890Sstevel@tonic-gate 			while ((p = (char *)strtok((first++ == 0 ? optarg : 0),
1900Sstevel@tonic-gate 						    ", ")) != NULL) {
1910Sstevel@tonic-gate 				if ((n = atoi(p)) == 0)
1920Sstevel@tonic-gate 					break;
1930Sstevel@tonic-gate 				ilist[iflg].ino = n;
1940Sstevel@tonic-gate 				iflg++;
1950Sstevel@tonic-gate 				ilist_index = iflg;
1960Sstevel@tonic-gate 				if (iflg > MAX_ILIST_INDEX())
1970Sstevel@tonic-gate 					extend_ilist();
1980Sstevel@tonic-gate 			}
1990Sstevel@tonic-gate 			break;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		case 's':
2020Sstevel@tonic-gate 			sflg++;
2030Sstevel@tonic-gate 			break;
2040Sstevel@tonic-gate #if 0
2050Sstevel@tonic-gate 		case 'V':
2060Sstevel@tonic-gate 			{
2070Sstevel@tonic-gate 				int	opt_count;
2080Sstevel@tonic-gate 				char	*opt_text;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 				(void) fprintf(stdout, "ncheck -F ufs ");
2110Sstevel@tonic-gate 				for (opt_count = 1; opt_count < argc;
2120Sstevel@tonic-gate 						opt_count++) {
2130Sstevel@tonic-gate 					opt_text = argv[opt_count];
2140Sstevel@tonic-gate 					if (opt_text)
2150Sstevel@tonic-gate 						(void) fprintf(stdout, " %s ",
2160Sstevel@tonic-gate 							opt_text);
2170Sstevel@tonic-gate 				}
2180Sstevel@tonic-gate 				(void) fprintf(stdout, "\n");
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 			break;
2210Sstevel@tonic-gate #endif
2220Sstevel@tonic-gate 		case '?':
2230Sstevel@tonic-gate 			usage();
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	argc -= optind;
2270Sstevel@tonic-gate 	argv = &argv[optind];
2280Sstevel@tonic-gate 	while (argc--) {
2290Sstevel@tonic-gate 		check(*argv);
2300Sstevel@tonic-gate 		argv++;
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 	return (nerror);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate void
check(char * file)236*1051Smaheshvs check(char *file)
2370Sstevel@tonic-gate {
238*1051Smaheshvs 	int i, j, c;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	fi = open64(file, 0);
2410Sstevel@tonic-gate 	if (fi < 0) {
2420Sstevel@tonic-gate 		(void) fprintf(stderr, "ncheck: cannot open %s\n", file);
2430Sstevel@tonic-gate 		nerror++;
2440Sstevel@tonic-gate 		return;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 	nhent = 0;
2470Sstevel@tonic-gate 	(void) printf("%s:\n", file);
2480Sstevel@tonic-gate 	sync();
2490Sstevel@tonic-gate 	bread((diskaddr_t)SBLOCK, (char *)&sblock, SBSIZE);
2500Sstevel@tonic-gate 	if ((sblock.fs_magic != FS_MAGIC) &&
2510Sstevel@tonic-gate 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
2520Sstevel@tonic-gate 		(void) printf("%s: not a ufs file system\n", file);
2530Sstevel@tonic-gate 		nerror++;
2540Sstevel@tonic-gate 		return;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
257757Svsakar 	if ((sblock.fs_magic == FS_MAGIC) &&
258757Svsakar 	    ((sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2) &&
259757Svsakar 	    (sblock.fs_version != UFS_VERSION_MIN))) {
260757Svsakar 		(void) printf("%s: unrecognized ufs version number %d\n",
261757Svsakar 		    file, sblock.fs_version);
262757Svsakar 		nerror++;
263757Svsakar 		return;
264757Svsakar 	}
265757Svsakar 
2660Sstevel@tonic-gate 	if ((sblock.fs_magic == MTB_UFS_MAGIC) &&
2670Sstevel@tonic-gate 	    ((sblock.fs_version > MTB_UFS_VERSION_1) ||
2680Sstevel@tonic-gate 	    (sblock.fs_version < MTB_UFS_VERSION_MIN))) {
2690Sstevel@tonic-gate 		(void) printf("%s: unrecognized ufs version number %d\n",
2700Sstevel@tonic-gate 		    file, sblock.fs_version);
2710Sstevel@tonic-gate 		nerror++;
2720Sstevel@tonic-gate 		return;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	/* If fs is logged, roll the log. */
2760Sstevel@tonic-gate 	if (sblock.fs_logbno) {
2770Sstevel@tonic-gate 		switch (rl_roll_log(file)) {
2780Sstevel@tonic-gate 		case RL_SUCCESS:
2790Sstevel@tonic-gate 			/*
2800Sstevel@tonic-gate 			 * Reread the superblock.  Rolling the log may have
2810Sstevel@tonic-gate 			 * changed it.
2820Sstevel@tonic-gate 			 */
2830Sstevel@tonic-gate 			bread((diskaddr_t)SBLOCK, (char *)&sblock, SBSIZE);
2840Sstevel@tonic-gate 			break;
2850Sstevel@tonic-gate 		case RL_SYSERR:
2860Sstevel@tonic-gate 			(void) printf("Warning: cannot roll log for %s.  %s\n",
2870Sstevel@tonic-gate 				file, strerror(errno));
2880Sstevel@tonic-gate 			break;
2890Sstevel@tonic-gate 		default:
2900Sstevel@tonic-gate 			(void) printf("Warning: cannot roll log for %s.\n",
2910Sstevel@tonic-gate 				file);
2920Sstevel@tonic-gate 			break;
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	itab = (struct dinode *)extend_tbl((uchar_t *)itab, &itab_size,
2970Sstevel@tonic-gate 		(unsigned)(sblock.fs_ipg * sizeof (struct dinode)));
2980Sstevel@tonic-gate 	if (itab == 0) {
2990Sstevel@tonic-gate 		(void) fprintf(stderr,
3000Sstevel@tonic-gate 			"ncheck: not enough memory for itab table\n");
3010Sstevel@tonic-gate 		nerror++;
3020Sstevel@tonic-gate 		return;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	hsize = sblock.fs_ipg * sblock.fs_ncg - sblock.fs_cstotal.cs_nifree + 1;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	htab = (struct htab *)extend_tbl((uchar_t *)htab, &htab_size,
3080Sstevel@tonic-gate 		(unsigned)(hsize * sizeof (struct htab)));
3090Sstevel@tonic-gate 	if (htab == 0) {
3100Sstevel@tonic-gate 		(void) fprintf(stderr,
3110Sstevel@tonic-gate 			"ncheck: not enough memory for htab table\n");
3120Sstevel@tonic-gate 		nerror++;
3130Sstevel@tonic-gate 		return;
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (!extend_strngtab(AVG_PATH_LEN * hsize)) {
3170Sstevel@tonic-gate 		(void) printf("not enough memory to allocate tables\n");
3180Sstevel@tonic-gate 		nerror++;
3190Sstevel@tonic-gate 		return;
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 	strngloc = 0;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	ino = 0;
3240Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
3250Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
3260Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
3270Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
3280Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
3290Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
3300Sstevel@tonic-gate 				if (itab[j].di_suid != UID_LONG)
3310Sstevel@tonic-gate 					itab[j].di_uid = itab[j].di_suid;
3320Sstevel@tonic-gate 				if (itab[j].di_sgid != GID_LONG)
3330Sstevel@tonic-gate 					itab[j].di_gid = itab[j].di_sgid;
3340Sstevel@tonic-gate 				pass1(&itab[j]);
3350Sstevel@tonic-gate 			}
3360Sstevel@tonic-gate 			ino++;
3370Sstevel@tonic-gate 		}
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	ilist[ilist_index++].ino = 0;
3400Sstevel@tonic-gate 	if (ilist_index > MAX_ILIST_INDEX())
3410Sstevel@tonic-gate 		extend_ilist();
3420Sstevel@tonic-gate 	ino = 0;
3430Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
3440Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
3450Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
3460Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
3490Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
3500Sstevel@tonic-gate 				pass2(&itab[j]);
3510Sstevel@tonic-gate 			}
3520Sstevel@tonic-gate 			ino++;
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	ino = 0;
3560Sstevel@tonic-gate 	for (c = 0; c < sblock.fs_ncg; c++) {
3570Sstevel@tonic-gate 		bread(fsbtodb(&sblock, cgimin(&sblock, c)), (char *)itab,
3580Sstevel@tonic-gate 		    (int)(sblock.fs_ipg * sizeof (struct dinode)));
3590Sstevel@tonic-gate 		for (j = 0; j < sblock.fs_ipg; j++) {
3600Sstevel@tonic-gate 			if (itab[j].di_smode != 0) {
3610Sstevel@tonic-gate 				itab[j].di_mode = itab[j].di_smode;
3620Sstevel@tonic-gate 				pass3(&itab[j]);
3630Sstevel@tonic-gate 			}
3640Sstevel@tonic-gate 			ino++;
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 	(void) close(fi);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	/*
3700Sstevel@tonic-gate 	 * Clear those elements after inodes specified by "-i" out of
3710Sstevel@tonic-gate 	 * ilist.
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	for (i = iflg; i < ilist_index; i++) {
3740Sstevel@tonic-gate 		ilist[i].ino = 0;
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 	ilist_index = iflg;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate void
pass1(struct dinode * ip)380*1051Smaheshvs pass1(struct dinode *ip)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	int i;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (mflg) {
3850Sstevel@tonic-gate 		for (i = 0; i < iflg; i++)
3860Sstevel@tonic-gate 			if (ino == ilist[i].ino) {
3870Sstevel@tonic-gate 				ilist[i].mode = ip->di_mode;
3880Sstevel@tonic-gate 				ilist[i].uid = ip->di_uid;
3890Sstevel@tonic-gate 				ilist[i].gid = ip->di_gid;
3900Sstevel@tonic-gate 			}
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 	if ((ip->di_mode & IFMT) != IFDIR) {
3930Sstevel@tonic-gate 		if (sflg == 0)
3940Sstevel@tonic-gate 			return;
3950Sstevel@tonic-gate 		if ((ip->di_mode & IFMT) == IFBLK ||
3960Sstevel@tonic-gate 				(ip->di_mode & IFMT) == IFCHR ||
3970Sstevel@tonic-gate 				ip->di_mode&(ISUID|ISGID)) {
3980Sstevel@tonic-gate 			ilist[ilist_index].ino = ino;
3990Sstevel@tonic-gate 			ilist[ilist_index].mode = ip->di_mode;
4000Sstevel@tonic-gate 			ilist[ilist_index].uid = ip->di_uid;
4010Sstevel@tonic-gate 			ilist[ilist_index].gid = ip->di_gid;
4020Sstevel@tonic-gate 			if (++ilist_index > MAX_ILIST_INDEX())
4030Sstevel@tonic-gate 				extend_ilist();
4040Sstevel@tonic-gate 			return;
4050Sstevel@tonic-gate 		}
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 	(void) lookup(ino, 1);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate void
pass2(struct dinode * ip)411*1051Smaheshvs pass2(struct dinode *ip)
4120Sstevel@tonic-gate {
413*1051Smaheshvs 	struct direct *dp;
4140Sstevel@tonic-gate 	struct dirstuff dirp;
4150Sstevel@tonic-gate 	struct htab *hp;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if ((ip->di_mode&IFMT) != IFDIR)
4190Sstevel@tonic-gate 		return;
4200Sstevel@tonic-gate 	dirp.loc = 0;
4210Sstevel@tonic-gate 	dirp.ip = ip;
4220Sstevel@tonic-gate 	gip = ip;
4230Sstevel@tonic-gate 	for (dp = dreaddir(&dirp); dp != NULL; dp = dreaddir(&dirp)) {
4240Sstevel@tonic-gate 		int nmlen;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 		if (dp->d_ino == 0)
4270Sstevel@tonic-gate 			continue;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 		hp = lookup(dp->d_ino, 0);
4300Sstevel@tonic-gate 		if (hp == 0)
4310Sstevel@tonic-gate 			continue;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		if (dotname(dp))
4340Sstevel@tonic-gate 			continue;
4350Sstevel@tonic-gate 		hp->h_pino = ino;
4360Sstevel@tonic-gate 		nmlen = strlen(dp->d_name);
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 		if (strngloc + nmlen + 1 > MAX_STRNGTAB_INDEX()) {
4390Sstevel@tonic-gate 			if (!extend_strngtab(STRNGTAB_INCR)) {
4400Sstevel@tonic-gate 				perror("ncheck: can't grow string table\n");
4410Sstevel@tonic-gate 				exit(32);
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		hp->h_name_index = strngloc;
4460Sstevel@tonic-gate 		(void) strcpy(&strngtab[strngloc], dp->d_name);
4470Sstevel@tonic-gate 		strngloc += nmlen + 1;
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate void
pass3(struct dinode * ip)452*1051Smaheshvs pass3(struct dinode *ip)
4530Sstevel@tonic-gate {
454*1051Smaheshvs 	struct direct *dp;
4550Sstevel@tonic-gate 	struct dirstuff dirp;
4560Sstevel@tonic-gate 	int k;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if ((ip->di_mode&IFMT) != IFDIR)
4590Sstevel@tonic-gate 		return;
4600Sstevel@tonic-gate 	dirp.loc = 0;
4610Sstevel@tonic-gate 	dirp.ip = ip;
4620Sstevel@tonic-gate 	gip = ip;
4630Sstevel@tonic-gate 	for (dp = dreaddir(&dirp); dp != NULL; dp = dreaddir(&dirp)) {
4640Sstevel@tonic-gate 		if (aflg == 0 && dotname(dp))
4650Sstevel@tonic-gate 			continue;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		if (sflg == 0 && iflg == 0)
4680Sstevel@tonic-gate 			goto pr;
4690Sstevel@tonic-gate 		for (k = 0; k < ilist_index && ilist[k].ino != 0; k++) {
4700Sstevel@tonic-gate 			if (ilist[k].ino == dp->d_ino) {
4710Sstevel@tonic-gate 				break;
4720Sstevel@tonic-gate 			}
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 		if (ilist[k].ino == 0)
4750Sstevel@tonic-gate 			continue;
4760Sstevel@tonic-gate 		if (mflg)
4770Sstevel@tonic-gate 			(void) printf("mode %-6o uid %-5ld gid %-5ld ino ",
4780Sstevel@tonic-gate 			    ilist[k].mode, ilist[k].uid, ilist[k].gid);
4790Sstevel@tonic-gate 	pr:
4800Sstevel@tonic-gate 		(void) printf("%-5u\t", dp->d_ino);
4810Sstevel@tonic-gate 		pname(ino, 0);
4820Sstevel@tonic-gate 		(void) printf("/%s", dp->d_name);
4830Sstevel@tonic-gate 		if (lookup(dp->d_ino, 0))
4840Sstevel@tonic-gate 			(void) printf("/.");
4850Sstevel@tonic-gate 		(void) printf("\n");
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate  * get next entry in a directory.
4910Sstevel@tonic-gate  */
4920Sstevel@tonic-gate struct direct *
dreaddir(struct dirstuff * dirp)493*1051Smaheshvs dreaddir(struct dirstuff *dirp)
4940Sstevel@tonic-gate {
495*1051Smaheshvs 	struct direct *dp;
4960Sstevel@tonic-gate 	daddr_t lbn, d;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	for (;;) {
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		if (dirp->loc >= (int)dirp->ip->di_size)
5010Sstevel@tonic-gate 			return (NULL);
5020Sstevel@tonic-gate 		if (blkoff(&sblock, dirp->loc) == 0) {
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 			lbn = lblkno(&sblock, dirp->loc);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 			d = bmap(lbn);
5070Sstevel@tonic-gate 			if (d == 0)
5080Sstevel@tonic-gate 				return (NULL);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 			bread(fsbtodb(&sblock, d), dirp->dbuf,
5110Sstevel@tonic-gate 			    (int)dblksize(&sblock, dirp->ip, (int)lbn));
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 		dp = (struct direct *)
5150Sstevel@tonic-gate 		    (dirp->dbuf + blkoff(&sblock, dirp->loc));
5160Sstevel@tonic-gate 		dirp->loc += dp->d_reclen;
5170Sstevel@tonic-gate 		if (dp->d_ino == 0) {
5180Sstevel@tonic-gate 			continue;
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 		return (dp);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate 
524*1051Smaheshvs int
dotname(struct direct * dp)525*1051Smaheshvs dotname(struct direct *dp)
5260Sstevel@tonic-gate {
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if (dp->d_name[0] == '.') {
5290Sstevel@tonic-gate 		if (dp->d_name[1] == 0 ||
5300Sstevel@tonic-gate 		    (dp->d_name[1] == '.' && dp->d_name[2] == 0))
5310Sstevel@tonic-gate 			return (1);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 	return (0);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate void
pname(ino_t i,int lev)537*1051Smaheshvs pname(ino_t i, int lev)
5380Sstevel@tonic-gate {
539*1051Smaheshvs 	struct htab *hp;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	if (i == UFSROOTINO)
5420Sstevel@tonic-gate 		return;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	if ((hp = lookup(i, 0)) == 0) {
5450Sstevel@tonic-gate 		(void) printf("???");
5460Sstevel@tonic-gate 		return;
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	if (lev > 10) {
5490Sstevel@tonic-gate 		(void) printf("...");
5500Sstevel@tonic-gate 		return;
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	pname(hp->h_pino, ++lev);
5530Sstevel@tonic-gate 	(void) printf("/%s", &(strngtab[hp->h_name_index]));
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate struct htab *
lookup(ino_t i,int ef)558*1051Smaheshvs lookup(ino_t i, int ef)
5590Sstevel@tonic-gate {
560*1051Smaheshvs 	struct htab *hp;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	for (hp = &htab[(int)i%hsize]; hp->h_ino; ) {
5630Sstevel@tonic-gate 		if (hp->h_ino == i)
5640Sstevel@tonic-gate 			return (hp);
5650Sstevel@tonic-gate 		if (++hp >= &htab[hsize])
5660Sstevel@tonic-gate 			hp = htab;
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	if (ef == 0)
5700Sstevel@tonic-gate 		return (0);
5710Sstevel@tonic-gate 	if (++nhent >= hsize) {
5720Sstevel@tonic-gate 		(void) fprintf(stderr, "ncheck: hsize of %ld is too small\n",
5730Sstevel@tonic-gate 									hsize);
5740Sstevel@tonic-gate 		exit(32);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 	hp->h_ino = i;
5770Sstevel@tonic-gate 	return (hp);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate void
bread(diskaddr_t bno,char * buf,int cnt)581*1051Smaheshvs bread(diskaddr_t bno, char *buf, int cnt)
5820Sstevel@tonic-gate {
583*1051Smaheshvs 	int i;
5840Sstevel@tonic-gate 	int got;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (llseek(fi, (offset_t)bno * DEV_BSIZE, 0) == -1) {
5870Sstevel@tonic-gate 		(void) fprintf(stderr, "ncheck: lseek error %lld\n",
5880Sstevel@tonic-gate 		    (offset_t)bno * DEV_BSIZE);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		for (i = 0; i < cnt; i++) {
5910Sstevel@tonic-gate 			buf[i] = 0;
5920Sstevel@tonic-gate 		}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 		return;
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	got = read((int)fi, buf, cnt);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	if (got != cnt) {
6000Sstevel@tonic-gate 		(void) fprintf(stderr,
6010Sstevel@tonic-gate 		    "ncheck: read error at block %lld (wanted %d got %d)\n",
6020Sstevel@tonic-gate 		    bno, cnt, got);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		for (i = 0; i < cnt; i++)
6050Sstevel@tonic-gate 			buf[i] = 0;
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate daddr_t
bmap(daddr_t i)610*1051Smaheshvs bmap(daddr_t i)
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate 	daddr_t ibuf[MAXNINDIR];
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if (i < NDADDR)
6150Sstevel@tonic-gate 		return (gip->di_db[i]);
6160Sstevel@tonic-gate 	i -= NDADDR;
6170Sstevel@tonic-gate 	if (i > NINDIR(&sblock)) {
6180Sstevel@tonic-gate 		(void) fprintf(stderr, "ncheck: %lu - huge directory\n", ino);
6190Sstevel@tonic-gate 		return ((daddr_t)0);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	bread(fsbtodb(&sblock, gip->di_ib[0]), (char *)ibuf, sizeof (ibuf));
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	return (ibuf[i]);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate void
usage()6280Sstevel@tonic-gate usage()
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	(void) fprintf(stderr,
6310Sstevel@tonic-gate 		/*CSTYLED*/
6320Sstevel@tonic-gate 		"ufs usage: ncheck [-F ufs] [generic options] [-a -i #list -s] [-o m] special\n");
6330Sstevel@tonic-gate 	exit(32);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate  * Extend or create the inode list;
6390Sstevel@tonic-gate  * this is used to contains the list of inodes we've been
6400Sstevel@tonic-gate  * asked to check using the "-i" flag and to hold the
6410Sstevel@tonic-gate  * inode numbers of files which we detect as being
6420Sstevel@tonic-gate  * blk|char|setuid|setgid ("-s" flag support).
6430Sstevel@tonic-gate  * Preserves contents.
6440Sstevel@tonic-gate  */
6450Sstevel@tonic-gate void
extend_ilist()6460Sstevel@tonic-gate extend_ilist()
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate 	ilist_size += ILIST_SZ_INCR;
6490Sstevel@tonic-gate 	ilist = (struct ilist *)realloc(ilist,
6500Sstevel@tonic-gate 		(ilist_size * sizeof (struct ilist)));
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	if (ilist == NULL) {
6530Sstevel@tonic-gate 		perror("ncheck: not enough memory to grow ilist\n");
6540Sstevel@tonic-gate 		exit(32);
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate  * Extend or create the string table.
6600Sstevel@tonic-gate  * Preserves contents.
6610Sstevel@tonic-gate  * Return non-zero for success.
6620Sstevel@tonic-gate  */
6630Sstevel@tonic-gate int
extend_strngtab(unsigned int size)664*1051Smaheshvs extend_strngtab(unsigned int size)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate 	strngtab_size += size;
6670Sstevel@tonic-gate 	strngtab = (char *)realloc(strngtab, strngtab_size);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	return ((int)strngtab);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate  * Extend or create a table, throwing away previous
6740Sstevel@tonic-gate  * contents.
6750Sstevel@tonic-gate  * Return null on failure.
6760Sstevel@tonic-gate  */
6770Sstevel@tonic-gate uchar_t *
extend_tbl(uchar_t * tbl,unsigned int * current_size,unsigned int new_size)678*1051Smaheshvs extend_tbl(uchar_t *tbl, unsigned int *current_size, unsigned int new_size)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * if we've already allocated tbl,
6820Sstevel@tonic-gate 	 * but it is too small, free it.
6830Sstevel@tonic-gate 	 * we don't realloc because we are throwing
6840Sstevel@tonic-gate 	 * away its contents.
6850Sstevel@tonic-gate 	 */
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	if (tbl && (*current_size < new_size)) {
6880Sstevel@tonic-gate 		free(tbl);
6890Sstevel@tonic-gate 		tbl = NULL;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if (tbl == NULL) {
6930Sstevel@tonic-gate 		tbl = (uchar_t *)malloc(new_size);
6940Sstevel@tonic-gate 		if (tbl == 0)
6950Sstevel@tonic-gate 			return ((uchar_t *)0);
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		*current_size = new_size;
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	(void) memset(tbl, 0, new_size);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	return (tbl);
7020Sstevel@tonic-gate }
703