xref: /openbsd-src/sbin/ncheck_ffs/ncheck_ffs.c (revision e6c7c102cf5d9891f32552a42895134a59937045)
1*e6c7c102Sjsg /*	$OpenBSD: ncheck_ffs.c,v 1.56 2024/04/23 13:34:50 jsg Exp $	*/
2754e29e6Stholo 
3754e29e6Stholo /*-
4754e29e6Stholo  * Copyright (c) 1995, 1996 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
5754e29e6Stholo  * All rights reserved.
6754e29e6Stholo  *
7754e29e6Stholo  * Redistribution and use in source and binary forms, with or without
8754e29e6Stholo  * modification, are permitted provided that the following conditions
9754e29e6Stholo  * are met:
10754e29e6Stholo  * 1. Redistributions of source code must retain the above copyright
11754e29e6Stholo  *    notice, this list of conditions and the following disclaimer.
12754e29e6Stholo  * 2. Redistributions in binary form must reproduce the above copyright
13754e29e6Stholo  *    notice, this list of conditions and the following disclaimer in the
14754e29e6Stholo  *    documentation and/or other materials provided with the distribution.
15754e29e6Stholo  *
16754e29e6Stholo  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17754e29e6Stholo  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18754e29e6Stholo  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
19754e29e6Stholo  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20754e29e6Stholo  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21754e29e6Stholo  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22754e29e6Stholo  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23754e29e6Stholo  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24754e29e6Stholo  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25754e29e6Stholo  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26754e29e6Stholo  */
27754e29e6Stholo 
28b3042f2aSmillert /*-
29b3042f2aSmillert  * Copyright (c) 1980, 1988, 1991, 1993
30b3042f2aSmillert  *	The Regents of the University of California.  All rights reserved.
31b3042f2aSmillert  *
32b3042f2aSmillert  * Redistribution and use in source and binary forms, with or without
33b3042f2aSmillert  * modification, are permitted provided that the following conditions
34b3042f2aSmillert  * are met:
35b3042f2aSmillert  * 1. Redistributions of source code must retain the above copyright
36b3042f2aSmillert  *    notice, this list of conditions and the following disclaimer.
37b3042f2aSmillert  * 2. Redistributions in binary form must reproduce the above copyright
38b3042f2aSmillert  *    notice, this list of conditions and the following disclaimer in the
39b3042f2aSmillert  *    documentation and/or other materials provided with the distribution.
40b3042f2aSmillert  * 3. Neither the name of the University nor the names of its contributors
41b3042f2aSmillert  *    may be used to endorse or promote products derived from this software
42b3042f2aSmillert  *    without specific prior written permission.
43b3042f2aSmillert  *
44b3042f2aSmillert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45b3042f2aSmillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46b3042f2aSmillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47b3042f2aSmillert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48b3042f2aSmillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49b3042f2aSmillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50b3042f2aSmillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51b3042f2aSmillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52b3042f2aSmillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53b3042f2aSmillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54b3042f2aSmillert  * SUCH DAMAGE.
55b3042f2aSmillert  */
56b3042f2aSmillert 
5778eb0b7eSderaadt #include <sys/param.h>	/* MAXBSIZE DEV_BSIZE */
58754e29e6Stholo #include <sys/time.h>
59754e29e6Stholo #include <sys/stat.h>
60b39ffb6cSkrw #include <sys/ioctl.h>
61b39ffb6cSkrw #include <sys/disklabel.h>
62b39ffb6cSkrw #include <sys/dkio.h>
63754e29e6Stholo #include <ufs/ffs/fs.h>
64754e29e6Stholo #include <ufs/ufs/dir.h>
65754e29e6Stholo #include <ufs/ufs/dinode.h>
66754e29e6Stholo 
67754e29e6Stholo #include <stdio.h>
68754e29e6Stholo #include <stdlib.h>
69754e29e6Stholo #include <fcntl.h>
70754e29e6Stholo #include <string.h>
71754e29e6Stholo #include <ctype.h>
72754e29e6Stholo #include <unistd.h>
73b9fc9a72Sderaadt #include <limits.h>
74661fe1dbStholo #include <fstab.h>
75754e29e6Stholo #include <errno.h>
76754e29e6Stholo #include <err.h>
77aae2bfffSkrw #include <util.h>
78754e29e6Stholo 
79b3042f2aSmillert #define DIP(dp, field) \
80b3042f2aSmillert     ((sblock->fs_magic == FS_UFS1_MAGIC) ? \
81b3042f2aSmillert     ((struct ufs1_dinode *)(dp))->field : \
82b3042f2aSmillert     ((struct ufs2_dinode *)(dp))->field)
83754e29e6Stholo 
84754e29e6Stholo char	*disk;		/* name of the disk file */
85a72e5d38Sthib char	rdisk[PATH_MAX];/* resolved name of the disk file */
86754e29e6Stholo int	diskfd;		/* disk file descriptor */
87754e29e6Stholo struct	fs *sblock;	/* the file system super block */
88754e29e6Stholo char	sblock_buf[MAXBSIZE];
89b3042f2aSmillert int	sblock_try[] = SBLOCKSEARCH; /* possible superblock locations */
90fb9a00c4Sguenther ufsino_t *ilist;	/* list of inodes to check */
91754e29e6Stholo int	ninodes;	/* number of inodes in list */
92754e29e6Stholo int	sflag;		/* only suid and special files */
93754e29e6Stholo int	aflag;		/* print the . and .. entries too */
94754e29e6Stholo int	mflag;		/* verbose output */
95744303e5Stholo int	iflag;		/* specific inode */
969c615a2eSmillert char	*format;	/* output format */
97754e29e6Stholo 
98b39ffb6cSkrw struct disklabel lab;
99b39ffb6cSkrw 
100754e29e6Stholo struct icache_s {
101fb9a00c4Sguenther 	ufsino_t	ino;
102b3042f2aSmillert 	union {
103b3042f2aSmillert 		struct ufs1_dinode dp1;
104b3042f2aSmillert 		struct ufs2_dinode dp2;
105b3042f2aSmillert 	} di;
106754e29e6Stholo } *icache;
107754e29e6Stholo int	nicache;
108fb9a00c4Sguenther int	maxicache;
109754e29e6Stholo 
110fb9a00c4Sguenther void addinode(ufsino_t inum);
111fb9a00c4Sguenther void *getino(ufsino_t inum);
112fb9a00c4Sguenther void findinodes(ufsino_t);
1131abdbfdeSderaadt void bread(daddr_t, char *, int);
1149c615a2eSmillert __dead void usage(void);
115fb9a00c4Sguenther void scanonedir(ufsino_t, const char *);
116fb9a00c4Sguenther void dirindir(ufsino_t, daddr_t, int, off_t *, const char *);
117fb9a00c4Sguenther void searchdir(ufsino_t, daddr_t, long, off_t, const char *);
118c72b5b24Smillert int matchino(const void *, const void *);
119c72b5b24Smillert int matchcache(const void *, const void *);
120fb9a00c4Sguenther void cacheino(ufsino_t, void *);
121fb9a00c4Sguenther void *cached(ufsino_t);
122c72b5b24Smillert int main(int, char *[]);
123c72b5b24Smillert char *rawname(char *);
1249c615a2eSmillert void format_entry(const char *, struct direct *);
125754e29e6Stholo 
126754e29e6Stholo /*
127754e29e6Stholo  * Check to see if the indicated inodes are the same
128754e29e6Stholo  */
129754e29e6Stholo int
matchino(const void * key,const void * val)130bc52e260Sderaadt matchino(const void *key, const void *val)
131754e29e6Stholo {
132fb9a00c4Sguenther 	ufsino_t k = *(ufsino_t *)key;
133fb9a00c4Sguenther 	ufsino_t v = *(ufsino_t *)val;
134754e29e6Stholo 
135754e29e6Stholo 	if (k < v)
136754e29e6Stholo 		return -1;
137754e29e6Stholo 	else if (k > v)
138661fe1dbStholo 		return 1;
139754e29e6Stholo 	return 0;
140754e29e6Stholo }
141754e29e6Stholo 
142754e29e6Stholo /*
143754e29e6Stholo  * Check if the indicated inode match the entry in the cache
144754e29e6Stholo  */
1458809fabbSderaadt int
matchcache(const void * key,const void * val)1468809fabbSderaadt matchcache(const void *key, const void *val)
147754e29e6Stholo {
148fb9a00c4Sguenther 	ufsino_t	ino = *(ufsino_t *)key;
149754e29e6Stholo 	struct icache_s	*ic = (struct icache_s *)val;
150754e29e6Stholo 
151754e29e6Stholo 	if (ino < ic->ino)
152754e29e6Stholo 		return -1;
153754e29e6Stholo 	else if (ino > ic->ino)
154754e29e6Stholo 		return 1;
155754e29e6Stholo 	return 0;
156754e29e6Stholo }
157754e29e6Stholo 
158754e29e6Stholo /*
159754e29e6Stholo  * Add an inode to the cached entries
160754e29e6Stholo  */
161754e29e6Stholo void
cacheino(ufsino_t ino,void * dp)162fb9a00c4Sguenther cacheino(ufsino_t ino, void *dp)
163754e29e6Stholo {
164fb9a00c4Sguenther 	if (nicache == maxicache) {
16593a69aacSderaadt 		struct icache_s *newicache;
16693a69aacSderaadt 
167fb9a00c4Sguenther 		/* grow exponentially */
168fb9a00c4Sguenther 		maxicache += 10 + maxicache/2;
169fb9a00c4Sguenther 		newicache = reallocarray(icache, maxicache, sizeof(*icache));
170d80cc147Shalex 		if (newicache == NULL)
17118052b20Sderaadt 			errx(1, "malloc");
17293a69aacSderaadt 		icache = newicache;
173fb9a00c4Sguenther 
174fb9a00c4Sguenther 	}
175754e29e6Stholo 	icache[nicache].ino = ino;
176b3042f2aSmillert 	if (sblock->fs_magic == FS_UFS1_MAGIC)
177b3042f2aSmillert 		icache[nicache++].di.dp1 = *(struct ufs1_dinode *)dp;
178b3042f2aSmillert 	else
179b3042f2aSmillert 		icache[nicache++].di.dp2 = *(struct ufs2_dinode *)dp;
180754e29e6Stholo }
181754e29e6Stholo 
182754e29e6Stholo /*
183754e29e6Stholo  * Get a cached inode
184754e29e6Stholo  */
185b3042f2aSmillert void *
cached(ufsino_t ino)186fb9a00c4Sguenther cached(ufsino_t ino)
187754e29e6Stholo {
188754e29e6Stholo 	struct icache_s *ic;
189b3042f2aSmillert 	void *dp = NULL;
190754e29e6Stholo 
191fb9a00c4Sguenther 	ic = bsearch(&ino, icache, nicache, sizeof(*icache), matchcache);
192b3042f2aSmillert 	if (ic != NULL) {
193b3042f2aSmillert 		if (sblock->fs_magic == FS_UFS1_MAGIC)
194b3042f2aSmillert 			dp = &ic->di.dp1;
195b3042f2aSmillert 		else
196b3042f2aSmillert 			dp = &ic->di.dp2;
197b3042f2aSmillert 	}
198b3042f2aSmillert 	return (dp);
199754e29e6Stholo }
200754e29e6Stholo 
201754e29e6Stholo /*
202754e29e6Stholo  * Walk the inode list for a filesystem to find all allocated inodes
203754e29e6Stholo  * Remember inodes we want to give information about and cache all
204754e29e6Stholo  * inodes pointing to directories
205754e29e6Stholo  */
206754e29e6Stholo void
findinodes(ufsino_t maxino)207fb9a00c4Sguenther findinodes(ufsino_t maxino)
208754e29e6Stholo {
209fb9a00c4Sguenther 	ufsino_t ino;
210b3042f2aSmillert 	void *dp;
211754e29e6Stholo 	mode_t mode;
212754e29e6Stholo 
213754e29e6Stholo 	for (ino = ROOTINO; ino < maxino; ino++) {
214754e29e6Stholo 		dp = getino(ino);
215b3042f2aSmillert 		mode = DIP(dp, di_mode) & IFMT;
216754e29e6Stholo 		if (!mode)
217754e29e6Stholo 			continue;
218754e29e6Stholo 		if (mode == IFDIR)
219754e29e6Stholo 			cacheino(ino, dp);
220744303e5Stholo 		if (iflag ||
221da10a279Smillert 		    (sflag && (mode == IFDIR ||
222b3042f2aSmillert 		     ((DIP(dp, di_mode) & (ISGID | ISUID)) == 0 &&
223da10a279Smillert 		      (mode == IFREG || mode == IFLNK)))))
224754e29e6Stholo 			continue;
225754e29e6Stholo 		addinode(ino);
226754e29e6Stholo 	}
227754e29e6Stholo }
228754e29e6Stholo 
229754e29e6Stholo /*
230754e29e6Stholo  * Get a specified inode from disk.  Attempt to minimize reads to once
231754e29e6Stholo  * per cylinder group
232754e29e6Stholo  */
233b3042f2aSmillert void *
getino(ufsino_t inum)234fb9a00c4Sguenther getino(ufsino_t inum)
235754e29e6Stholo {
236b3042f2aSmillert 	static char *itab = NULL;
2371abdbfdeSderaadt 	static daddr_t iblk = -1;
238b3042f2aSmillert 	void *dp;
239b3042f2aSmillert 	size_t dsize;
240754e29e6Stholo 
241754e29e6Stholo 	if (inum < ROOTINO || inum >= sblock->fs_ncg * sblock->fs_ipg)
242754e29e6Stholo 		return NULL;
243b3042f2aSmillert 	if ((dp = cached(inum)) != NULL)
244b3042f2aSmillert 		return dp;
245b3042f2aSmillert 	if (sblock->fs_magic == FS_UFS1_MAGIC)
246b3042f2aSmillert 		dsize = sizeof(struct ufs1_dinode);
247b3042f2aSmillert 	else
248b3042f2aSmillert 		dsize = sizeof(struct ufs2_dinode);
249754e29e6Stholo 	if ((inum / sblock->fs_ipg) != iblk || itab == NULL) {
250754e29e6Stholo 		iblk = inum / sblock->fs_ipg;
251754e29e6Stholo 		if (itab == NULL &&
252fb9a00c4Sguenther 		    (itab = reallocarray(NULL, sblock->fs_ipg, dsize)) == NULL)
253754e29e6Stholo 			errx(1, "no memory for inodes");
254b3042f2aSmillert 		bread(fsbtodb(sblock, cgimin(sblock, iblk)), itab,
255b3042f2aSmillert 		      sblock->fs_ipg * dsize);
256754e29e6Stholo 	}
257b3042f2aSmillert 	return itab + (inum % sblock->fs_ipg) * dsize;
258754e29e6Stholo }
259754e29e6Stholo 
260754e29e6Stholo /*
261b3042f2aSmillert  * Read a chunk of data from the disk.
262b3042f2aSmillert  * Try to recover from hard errors by reading in sector sized pieces.
263b3042f2aSmillert  * Error recovery is attempted at most BREADEMAX times before seeking
264b3042f2aSmillert  * consent from the operator to continue.
265754e29e6Stholo  */
266754e29e6Stholo int	breaderrors = 0;
267754e29e6Stholo #define	BREADEMAX 32
268754e29e6Stholo 
269754e29e6Stholo void
bread(daddr_t blkno,char * buf,int size)2701abdbfdeSderaadt bread(daddr_t blkno, char *buf, int size)
271754e29e6Stholo {
272171a3685Skrw 	off_t offset;
273754e29e6Stholo  	int cnt, i;
274b39ffb6cSkrw 	u_int32_t secsize = lab.d_secsize;
275754e29e6Stholo 
276171a3685Skrw 	offset = blkno * DEV_BSIZE;
277171a3685Skrw 
278754e29e6Stholo loop:
279171a3685Skrw 	if ((cnt = pread(diskfd, buf, size, offset)) == size)
280754e29e6Stholo 		return;
281171a3685Skrw 	if (blkno + (size / DEV_BSIZE) >
282171a3685Skrw 	    fsbtodb(sblock, sblock->fs_ffs1_size)) {
283754e29e6Stholo 		/*
284754e29e6Stholo 		 * Trying to read the final fragment.
285754e29e6Stholo 		 *
286754e29e6Stholo 		 * NB - dump only works in TP_BSIZE blocks, hence
287171a3685Skrw 		 * rounds `DEV_BSIZE' fragments up to TP_BSIZE pieces.
288754e29e6Stholo 		 * It should be smarter about not actually trying to
289754e29e6Stholo 		 * read more than it can get, but for the time being
290754e29e6Stholo 		 * we punt and scale back the read only when it gets
291754e29e6Stholo 		 * us into trouble. (mkm 9/25/83)
292754e29e6Stholo 		 */
293b39ffb6cSkrw 		size -= secsize;
294754e29e6Stholo 		goto loop;
295754e29e6Stholo 	}
296754e29e6Stholo 	if (cnt == -1)
297b3042f2aSmillert 		warnx("read error from %s: %s: [block %lld]: count=%d",
298b3042f2aSmillert 		    disk, strerror(errno), (long long)blkno, size);
299754e29e6Stholo 	else
300bb4f4faeSkrw 		warnx("short read error from %s: [block %lld]: count=%d, "
301bb4f4faeSkrw 		    "got=%d", disk, (long long)blkno, size, cnt);
302754e29e6Stholo 	if (++breaderrors > BREADEMAX)
303bb4f4faeSkrw 		errx(1, "More than %d block read errors from %s", BREADEMAX,
304bb4f4faeSkrw 		    disk);
305754e29e6Stholo 	/*
306754e29e6Stholo 	 * Zero buffer, then try to read each sector of buffer separately.
307754e29e6Stholo 	 */
308754e29e6Stholo 	memset(buf, 0, size);
309b39ffb6cSkrw 	for (i = 0; i < size; i += secsize, buf += secsize) {
310b39ffb6cSkrw 		if ((cnt = pread(diskfd, buf, secsize, offset + i)) ==
311b39ffb6cSkrw 		    secsize)
312754e29e6Stholo 			continue;
313754e29e6Stholo 		if (cnt == -1) {
314bb4f4faeSkrw 			warnx("read error from %s: %s: [sector %lld]: "
315b39ffb6cSkrw 			    "count=%u", disk, strerror(errno),
316b39ffb6cSkrw 			    (long long)(offset + i) / DEV_BSIZE, secsize);
317754e29e6Stholo 			continue;
318754e29e6Stholo 		}
319b39ffb6cSkrw 		warnx("short read error from %s: [sector %lld]: count=%u, "
320171a3685Skrw 		    "got=%d", disk, (long long)(offset + i) / DEV_BSIZE,
321b39ffb6cSkrw 		    secsize, cnt);
322754e29e6Stholo 	}
323754e29e6Stholo }
324754e29e6Stholo 
325754e29e6Stholo /*
326754e29e6Stholo  * Add an inode to the in-memory list of inodes to dump
327754e29e6Stholo  */
328754e29e6Stholo void
addinode(ufsino_t ino)329fb9a00c4Sguenther addinode(ufsino_t ino)
330754e29e6Stholo {
331fb9a00c4Sguenther 	ufsino_t *newilist;
33293a69aacSderaadt 
333fb9a00c4Sguenther 	newilist = reallocarray(ilist, ninodes + 1, sizeof(*ilist));
334d80cc147Shalex 	if (newilist == NULL)
335754e29e6Stholo 		errx(4, "not enough memory to allocate tables");
33693a69aacSderaadt 	ilist = newilist;
337754e29e6Stholo 	ilist[ninodes] = ino;
338754e29e6Stholo 	ninodes++;
339754e29e6Stholo }
340754e29e6Stholo 
341754e29e6Stholo /*
342754e29e6Stholo  * Scan the directory pointer at by ino
343754e29e6Stholo  */
344754e29e6Stholo void
scanonedir(ufsino_t ino,const char * path)345fb9a00c4Sguenther scanonedir(ufsino_t ino, const char *path)
346754e29e6Stholo {
347b3042f2aSmillert 	void *dp;
348b3042f2aSmillert 	off_t filesize;
349754e29e6Stholo 	int i;
350754e29e6Stholo 
351754e29e6Stholo 	if ((dp = cached(ino)) == NULL)
352754e29e6Stholo 		return;
353b3042f2aSmillert 	filesize = (off_t)DIP(dp, di_size);
354754e29e6Stholo 	for (i = 0; filesize > 0 && i < NDADDR; i++) {
355b3042f2aSmillert 		if (DIP(dp, di_db[i]) != 0) {
356b3042f2aSmillert 			searchdir(ino, DIP(dp, di_db[i]),
357b3042f2aSmillert 			    sblksize(sblock, DIP(dp, di_size), i),
3586dde1419Sderaadt 			    filesize, path);
359b3042f2aSmillert 		}
360754e29e6Stholo 		filesize -= sblock->fs_bsize;
361754e29e6Stholo 	}
362754e29e6Stholo 	for (i = 0; filesize > 0 && i < NIADDR; i++) {
363b3042f2aSmillert 		if (DIP(dp, di_ib[i]))
364fb9a00c4Sguenther 			dirindir(ino, DIP(dp, di_ib[i]), i, &filesize, path);
365754e29e6Stholo 	}
366754e29e6Stholo }
367754e29e6Stholo 
368754e29e6Stholo /*
369754e29e6Stholo  * Read indirect blocks, and pass the data blocks to be searched
370fb9a00c4Sguenther  * as directories.
371754e29e6Stholo  */
372754e29e6Stholo void
dirindir(ufsino_t ino,daddr_t blkno,int ind_level,off_t * filesizep,const char * path)373fb9a00c4Sguenther dirindir(ufsino_t ino, daddr_t blkno, int ind_level, off_t *filesizep,
374bc52e260Sderaadt     const char *path)
375754e29e6Stholo {
376754e29e6Stholo 	int i;
377fb9a00c4Sguenther 	void *idblk;
378754e29e6Stholo 
379fb9a00c4Sguenther 	if ((idblk = malloc(sblock->fs_bsize)) == NULL)
380b3042f2aSmillert 		errx(1, "dirindir: cannot allocate indirect memory.\n");
381b3042f2aSmillert 	bread(fsbtodb(sblock, blkno), idblk, (int)sblock->fs_bsize);
382754e29e6Stholo 	if (ind_level <= 0) {
383fb9a00c4Sguenther 		for (i = 0; *filesizep > 0 && i < NINDIR(sblock); i++) {
384b3042f2aSmillert 			if (sblock->fs_magic == FS_UFS1_MAGIC)
385c29211cfSpedro 				blkno = ((int32_t *)idblk)[i];
386b3042f2aSmillert 			else
387c29211cfSpedro 				blkno = ((int64_t *)idblk)[i];
388b3042f2aSmillert 			if (blkno != 0)
3896dde1419Sderaadt 				searchdir(ino, blkno, sblock->fs_bsize,
390fb9a00c4Sguenther 				    *filesizep, path);
391fb9a00c4Sguenther 			*filesizep -= sblock->fs_bsize;
392754e29e6Stholo 		}
393fb9a00c4Sguenther 	} else {
394754e29e6Stholo 		ind_level--;
395fb9a00c4Sguenther 		for (i = 0; *filesizep > 0 && i < NINDIR(sblock); i++) {
396b3042f2aSmillert 			if (sblock->fs_magic == FS_UFS1_MAGIC)
397c29211cfSpedro 				blkno = ((int32_t *)idblk)[i];
398b3042f2aSmillert 			else
399c29211cfSpedro 				blkno = ((int64_t *)idblk)[i];
400b3042f2aSmillert 			if (blkno != 0)
401fb9a00c4Sguenther 				dirindir(ino, blkno, ind_level, filesizep,
402fb9a00c4Sguenther 				    path);
403754e29e6Stholo 		}
404754e29e6Stholo 	}
405fb9a00c4Sguenther 	free(idblk);
406fb9a00c4Sguenther }
407754e29e6Stholo 
408754e29e6Stholo /*
409754e29e6Stholo  * Scan a disk block containing directory information looking to see if
410b3042f2aSmillert  * any of the entries are on the dump list and to see if the directory
411b3042f2aSmillert  * contains any subdirectories.
412754e29e6Stholo  */
413754e29e6Stholo void
searchdir(ufsino_t ino,daddr_t blkno,long size,off_t filesize,const char * path)414fb9a00c4Sguenther searchdir(ufsino_t ino, daddr_t blkno, long size, off_t filesize,
415bc52e260Sderaadt     const char *path)
416754e29e6Stholo {
417b3042f2aSmillert 	char *dblk;
418754e29e6Stholo 	struct direct *dp;
419b3042f2aSmillert 	void *di;
420754e29e6Stholo 	mode_t mode;
421754e29e6Stholo 	char *npath;
422fb9a00c4Sguenther 	ufsino_t subino;
423754e29e6Stholo 	long loc;
424754e29e6Stholo 
425b3042f2aSmillert 	if ((dblk = malloc(sblock->fs_bsize)) == NULL)
426fb9a00c4Sguenther 		errx(1, "searchdir: cannot allocate directory memory.");
427754e29e6Stholo 	bread(fsbtodb(sblock, blkno), dblk, (int)size);
428754e29e6Stholo 	if (filesize < size)
429754e29e6Stholo 		size = filesize;
430754e29e6Stholo 	for (loc = 0; loc < size; ) {
431754e29e6Stholo 		dp = (struct direct *)(dblk + loc);
432754e29e6Stholo 		if (dp->d_reclen == 0) {
4333b92bd08Sderaadt 			warnx("corrupted directory, inode %llu",
4343b92bd08Sderaadt 			    (unsigned long long)ino);
435754e29e6Stholo 			break;
436754e29e6Stholo 		}
437754e29e6Stholo 		loc += dp->d_reclen;
438b3042f2aSmillert 		if (dp->d_ino == 0)
439754e29e6Stholo 			continue;
440754e29e6Stholo 		if (dp->d_name[0] == '.') {
441754e29e6Stholo 			if (!aflag && (dp->d_name[1] == '\0' ||
442754e29e6Stholo 			    (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
443754e29e6Stholo 				continue;
444754e29e6Stholo 		}
445754e29e6Stholo 		di = getino(dp->d_ino);
446b3042f2aSmillert 		mode = DIP(di, di_mode) & IFMT;
447f199b8ccSguenther 		subino = dp->d_ino;
448f199b8ccSguenther 		if (bsearch(&subino, ilist, ninodes, sizeof(*ilist), matchino)) {
4499c615a2eSmillert 			if (format) {
4509c615a2eSmillert 				format_entry(path, dp);
4519c615a2eSmillert 			} else {
452754e29e6Stholo 				if (mflag)
4539c615a2eSmillert 					printf("mode %-6o uid %-5u gid %-5u ino ",
454b3042f2aSmillert 					    DIP(di, di_mode), DIP(di, di_uid),
455b3042f2aSmillert 					    DIP(di, di_gid));
4563b92bd08Sderaadt 				printf("%-7llu %s/%s%s\n",
4573b92bd08Sderaadt 				    (unsigned long long)dp->d_ino, path,
4589c615a2eSmillert 				    dp->d_name, mode == IFDIR ? "/." : "");
4599c615a2eSmillert 			}
460744303e5Stholo 		}
461744303e5Stholo 		if (mode == IFDIR) {
462754e29e6Stholo 			if (dp->d_name[0] == '.') {
463754e29e6Stholo 				if (dp->d_name[1] == '\0' ||
464754e29e6Stholo 				    (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
465754e29e6Stholo 					continue;
466754e29e6Stholo 			}
467f760b59dSderaadt 			if (asprintf(&npath, "%s/%s", path, dp->d_name) == -1)
46818052b20Sderaadt 				errx(1, "malloc");
469754e29e6Stholo 			scanonedir(dp->d_ino, npath);
470754e29e6Stholo 			free(npath);
471754e29e6Stholo 		}
472754e29e6Stholo 	}
473fb9a00c4Sguenther 	free(dblk);
474754e29e6Stholo }
475754e29e6Stholo 
476661fe1dbStholo char *
rawname(char * name)477bc52e260Sderaadt rawname(char *name)
478661fe1dbStholo {
479b9fc9a72Sderaadt 	static char newname[PATH_MAX];
480661fe1dbStholo 	char *p;
481661fe1dbStholo 
482661fe1dbStholo 	if ((p = strrchr(name, '/')) == NULL)
483661fe1dbStholo 		return name;
484661fe1dbStholo 	*p = '\0';
4856dde1419Sderaadt 	strlcpy(newname, name, sizeof newname - 2);
486661fe1dbStholo 	*p++ = '/';
4876dde1419Sderaadt 	strlcat(newname, "/r", sizeof newname);
4886dde1419Sderaadt 	strlcat(newname, p, sizeof newname);
489661fe1dbStholo 	return(newname);
490661fe1dbStholo }
491661fe1dbStholo 
4929c615a2eSmillert __dead void
usage(void)493bc52e260Sderaadt usage(void)
494754e29e6Stholo {
4959c615a2eSmillert 	extern char *__progname;
4969c615a2eSmillert 
49703cb09c0Ssobrado 	fprintf(stderr,
49803cb09c0Ssobrado 	    "usage: %s [-ams] [-f format] [-i number ...] filesystem\n",
4999c615a2eSmillert 	    __progname);
500754e29e6Stholo 	exit(3);
501754e29e6Stholo }
502754e29e6Stholo 
503754e29e6Stholo int
main(int argc,char * argv[])504bc52e260Sderaadt main(int argc, char *argv[])
505754e29e6Stholo {
506661fe1dbStholo 	struct stat stblock;
507661fe1dbStholo 	struct fstab *fsp;
5083b92bd08Sderaadt 	unsigned long long ullval;
509b3042f2aSmillert 	ssize_t n;
510aae2bfffSkrw 	char *ep;
511b3042f2aSmillert 	int c, i;
512754e29e6Stholo 
5139c615a2eSmillert 	while ((c = getopt(argc, argv, "af:i:ms")) != -1)
514754e29e6Stholo 		switch (c) {
515754e29e6Stholo 		case 'a':
5163579491dSderaadt 			aflag = 1;
517754e29e6Stholo 			break;
518754e29e6Stholo 		case 'i':
5193579491dSderaadt 			iflag = 1;
5206dde1419Sderaadt 
5216dde1419Sderaadt 			errno = 0;
5223b92bd08Sderaadt 			ullval = strtoull(optarg, &ep, 10);
5236dde1419Sderaadt 			if (optarg[0] == '\0' || *ep != '\0')
5246dde1419Sderaadt 				errx(1, "%s is not a number",
5256dde1419Sderaadt 				    optarg);
526fb9a00c4Sguenther 			if ((errno == ERANGE && ullval == ULLONG_MAX) ||
527fb9a00c4Sguenther 			    (ufsino_t)ullval != ullval)
528fb9a00c4Sguenther 				errx(1, "%s is out of range",
5296dde1419Sderaadt 				    optarg);
530fb9a00c4Sguenther 			addinode((ufsino_t)ullval);
5316dde1419Sderaadt 
5326dde1419Sderaadt 			while (optind < argc) {
5336dde1419Sderaadt 				errno = 0;
5343b92bd08Sderaadt 				ullval = strtoull(argv[optind], &ep, 10);
5356dde1419Sderaadt 				if (argv[optind][0] == '\0' || *ep != '\0')
5366dde1419Sderaadt 					break;
537fb9a00c4Sguenther 				if ((errno == ERANGE && ullval == ULLONG_MAX)
538fb9a00c4Sguenther 				    || (ufsino_t)ullval != ullval)
539fb9a00c4Sguenther 					errx(1, "%s is out of range",
5406dde1419Sderaadt 					    argv[optind]);
541fb9a00c4Sguenther 				addinode((ufsino_t)ullval);
542754e29e6Stholo 				optind++;
543754e29e6Stholo 			}
544754e29e6Stholo 			break;
5459c615a2eSmillert 		case 'f':
5469c615a2eSmillert 			format = optarg;
5479c615a2eSmillert 			break;
548754e29e6Stholo 		case 'm':
5493579491dSderaadt 			mflag = 1;
550754e29e6Stholo 			break;
551754e29e6Stholo 		case 's':
5523579491dSderaadt 			sflag = 1;
553754e29e6Stholo 			break;
5546dde1419Sderaadt 		default:
5556dde1419Sderaadt 			usage();
556754e29e6Stholo 			exit(2);
557754e29e6Stholo 		}
5589c615a2eSmillert 	if (optind != argc - 1 || (mflag && format))
559754e29e6Stholo 		usage();
560661fe1dbStholo 
561aae2bfffSkrw 	disk = argv[optind];
562aae2bfffSkrw 	if ((diskfd = opendev(disk, O_RDONLY, 0, NULL)) >= 0) {
563aae2bfffSkrw 		if (fstat(diskfd, &stblock))
564aae2bfffSkrw 			err(1, "cannot stat %s", disk);
565aae2bfffSkrw 		if (S_ISCHR(stblock.st_mode))
566aae2bfffSkrw 			goto gotdev;
567aae2bfffSkrw 		close(diskfd);
568aae2bfffSkrw 	}
569aae2bfffSkrw 
570aae2bfffSkrw 	if (realpath(disk, rdisk) == NULL)
571aae2bfffSkrw 		err(1, "cannot find real path for %s", disk);
572a72e5d38Sthib 	disk = rdisk;
573754e29e6Stholo 
574df69c215Sderaadt 	if (stat(disk, &stblock) == -1)
575661fe1dbStholo 		err(1, "cannot stat %s", disk);
576661fe1dbStholo 
577661fe1dbStholo         if (S_ISBLK(stblock.st_mode)) {
578661fe1dbStholo 		disk = rawname(disk);
5799c615a2eSmillert 	} else if (!S_ISCHR(stblock.st_mode)) {
580661fe1dbStholo 		if ((fsp = getfsfile(disk)) == NULL)
581a72e5d38Sthib 			err(1, "could not find file system %s", disk);
582661fe1dbStholo                 disk = rawname(fsp->fs_spec);
583661fe1dbStholo         }
584661fe1dbStholo 
585df69c215Sderaadt 	if ((diskfd = opendev(disk, O_RDONLY, 0, NULL)) == -1)
586754e29e6Stholo 		err(1, "cannot open %s", disk);
587aae2bfffSkrw 
588aae2bfffSkrw gotdev:
589df69c215Sderaadt 	if (ioctl(diskfd, DIOCGDINFO, (char *)&lab) == -1)
590aae2bfffSkrw 		err(1, "ioctl (DIOCGDINFO)");
591df69c215Sderaadt 	if (ioctl(diskfd, DIOCGPDINFO, (char *)&lab) == -1)
592b39ffb6cSkrw 		err(1, "ioctl (DIOCGPDINFO)");
593fdcd7891Sdoug 
594fdcd7891Sdoug 	if (pledge("stdio", NULL) == -1)
595fdcd7891Sdoug 		err(1, "pledge");
596fdcd7891Sdoug 
597754e29e6Stholo 	sblock = (struct fs *)sblock_buf;
598b3042f2aSmillert 	for (i = 0; sblock_try[i] != -1; i++) {
599b3042f2aSmillert 		n = pread(diskfd, sblock, SBLOCKSIZE, (off_t)sblock_try[i]);
600b3042f2aSmillert 		if (n == SBLOCKSIZE && (sblock->fs_magic == FS_UFS1_MAGIC ||
601b3042f2aSmillert 		     (sblock->fs_magic == FS_UFS2_MAGIC &&
602b3042f2aSmillert 		      sblock->fs_sblockloc == sblock_try[i])) &&
603b3042f2aSmillert 		    sblock->fs_bsize <= MAXBSIZE &&
604b3042f2aSmillert 		    sblock->fs_bsize >= sizeof(struct fs))
605b3042f2aSmillert 			break;
606b3042f2aSmillert 	}
607b3042f2aSmillert 	if (sblock_try[i] == -1)
608b3042f2aSmillert 		errx(1, "cannot find filesystem superblock");
609b3042f2aSmillert 
610754e29e6Stholo 	findinodes(sblock->fs_ipg * sblock->fs_ncg);
6119c615a2eSmillert 	if (!format)
612754e29e6Stholo 		printf("%s:\n", disk);
613754e29e6Stholo 	scanonedir(ROOTINO, "");
614754e29e6Stholo 	close(diskfd);
6159c615a2eSmillert 	exit (0);
6169c615a2eSmillert }
6179c615a2eSmillert 
6189c615a2eSmillert void
format_entry(const char * path,struct direct * dp)6199c615a2eSmillert format_entry(const char *path, struct direct *dp)
6209c615a2eSmillert {
6219c615a2eSmillert 	static size_t size;
6229c615a2eSmillert 	static char *buf;
62393a69aacSderaadt 	char *src, *dst, *newbuf;
6240d4d1fd5Sray 	int len;
6259c615a2eSmillert 
6269c615a2eSmillert 	if (buf == NULL) {
6279c615a2eSmillert 		if ((buf = malloc(LINE_MAX)) == NULL)
6289c615a2eSmillert 			err(1, "malloc");
6299c615a2eSmillert 		size = LINE_MAX;
6309c615a2eSmillert 	}
6319c615a2eSmillert 
6329c615a2eSmillert 	for (src = format, dst = buf; *src; src++) {
6339c615a2eSmillert 		/* Need room for at least one character in buf. */
6349c615a2eSmillert 		if (size <= dst - buf) {
6359c615a2eSmillert 		    expand_buf:
636785ea9ddSderaadt 			if ((newbuf = reallocarray(buf, size, 2)) == NULL)
6379c615a2eSmillert 				err(1, "realloc");
63893a69aacSderaadt 			buf = newbuf;
639785ea9ddSderaadt 			size = size * 2;
6409c615a2eSmillert 		}
6419c615a2eSmillert 		if (src[0] =='\\') {
6429c615a2eSmillert 			switch (src[1]) {
6439c615a2eSmillert 			case 'I':
6443b92bd08Sderaadt 				len = snprintf(dst, size - (dst - buf), "%llu",
6453b92bd08Sderaadt 				    (unsigned long long)dp->d_ino);
646515e489cSderaadt 				if (len < 0 || len >= size - (dst - buf))
6479c615a2eSmillert 					goto expand_buf;
6489c615a2eSmillert 				dst += len;
6499c615a2eSmillert 				break;
6509c615a2eSmillert 			case 'P':
6519c615a2eSmillert 				len = snprintf(dst, size - (dst - buf), "%s/%s",
6529c615a2eSmillert 				    path, dp->d_name);
653515e489cSderaadt 				if (len < 0 || len >= size - (dst - buf))
6549c615a2eSmillert 					goto expand_buf;
6559c615a2eSmillert 				dst += len;
6569c615a2eSmillert 				break;
6579c615a2eSmillert 			case '\\':
6589c615a2eSmillert 				*dst++ = '\\';
6599c615a2eSmillert 				break;
6609c615a2eSmillert 			case '0':
6619c615a2eSmillert 				/* XXX - support other octal numbers? */
6629c615a2eSmillert 				*dst++ = '\0';
6639c615a2eSmillert 				break;
6649c615a2eSmillert 			case 'a':
6659c615a2eSmillert 				*dst++ = '\a';
6669c615a2eSmillert 				break;
6679c615a2eSmillert 			case 'b':
6689c615a2eSmillert 				*dst++ = '\b';
6699c615a2eSmillert 				break;
6709c615a2eSmillert 			case 'e':
6719c615a2eSmillert 				*dst++ = '\e';
6729c615a2eSmillert 				break;
6739c615a2eSmillert 			case 'f':
6749c615a2eSmillert 				*dst++ = '\f';
6759c615a2eSmillert 				break;
6769c615a2eSmillert 			case 'n':
6779c615a2eSmillert 				*dst++ = '\n';
6789c615a2eSmillert 				break;
6799c615a2eSmillert 			case 'r':
6809c615a2eSmillert 				*dst++ = '\r';
6819c615a2eSmillert 				break;
6829c615a2eSmillert 			case 't':
6839c615a2eSmillert 				*dst++ = '\t';
6849c615a2eSmillert 				break;
6859c615a2eSmillert 			case 'v':
6869c615a2eSmillert 				*dst++ = '\v';
6879c615a2eSmillert 				break;
6889c615a2eSmillert 			default:
6899c615a2eSmillert 				*dst++ = src[1];
6909c615a2eSmillert 				break;
6919c615a2eSmillert 			}
6929c615a2eSmillert 			src++;
6939c615a2eSmillert 		} else
6949c615a2eSmillert 			*dst++ = *src;
6959c615a2eSmillert 	}
6969c615a2eSmillert 	fwrite(buf, dst - buf, 1, stdout);
697754e29e6Stholo }
698