xref: /freebsd/tools/diag/prtblknos/prtblknos.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*
2  * Copyright (c) 1998, 2003, 2013, 2018 Marshall Kirk McKusick.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <ufs/ffs/fs.h>
31 
32 #include <err.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <libufs.h>
36 
37 #ifdef PRTBLKNOS
38 union dinode {
39 	struct ufs1_dinode dp1;
40 	struct ufs2_dinode dp2;
41 };
42 extern struct uufsd disk;
43 #else /* used by fsdb */
44 #include <fsck.h>
45 static struct bufarea *bp;
46 #endif
47 
48 void prtblknos(struct fs *fs, union dinode *dp);
49 
50 static const char *distance(struct fs *, ufs2_daddr_t, ufs2_daddr_t);
51 static void  printblk(struct fs *, ufs_lbn_t, ufs2_daddr_t, int, ufs_lbn_t);
52 static void  indirprt(struct fs *, int, ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t,
53 		ufs_lbn_t);
54 
55 void
56 prtblknos(struct fs *fs, union dinode *dp)
57 {
58 	int i, mode, frags;
59 	ufs_lbn_t lbn, lastlbn, len, blksperindir;
60 	ufs2_daddr_t blkno;
61 	off_t size;
62 
63 	if (fs->fs_magic == FS_UFS1_MAGIC) {
64 		size = dp->dp1.di_size;
65 		mode = dp->dp1.di_mode;
66 	} else {
67 		size = dp->dp2.di_size;
68 		mode = dp->dp2.di_mode;
69 	}
70 	switch (mode & IFMT) {
71 	default:
72 		printf("unknown inode type 0%d\n", (mode & IFMT));
73 		return;
74 	case 0:
75 		printf("unallocated inode\n");
76 		return;
77 	case IFIFO:
78 		printf("fifo\n");
79 		return;
80 	case IFCHR:
81 		printf("character device\n");
82 		return;
83 	case IFBLK:
84 		printf("block device\n");
85 		return;
86 	case IFSOCK:
87 		printf("socket\n");
88 		return;
89 	case IFWHT:
90 		printf("whiteout\n");
91 		return;
92 	case IFLNK:
93 		if (size == 0) {
94 			printf("empty symbolic link\n");
95 			return;
96 		}
97 		if (size < fs->fs_maxsymlinklen) {
98 			printf("symbolic link referencing %s\n",
99 			    (fs->fs_magic == FS_UFS1_MAGIC) ?
100 			    dp->dp1.di_shortlink :
101 			    dp->dp2.di_shortlink);
102 			return;
103 		}
104 		printf("symbolic link\n");
105 		break;
106 	case IFREG:
107 		if (size == 0) {
108 			printf("empty file\n");
109 			return;
110 		}
111 		printf("regular file, size %jd\n", (intmax_t)size);
112 		break;
113 	case IFDIR:
114 		if (size == 0) {
115 			printf("empty directory\n");
116 			return;
117 		}
118 		printf("directory, size %jd\n", (intmax_t)size);
119 		break;
120 	}
121 	lastlbn = howmany(size, fs->fs_bsize);
122 	len = lastlbn < UFS_NDADDR ? lastlbn : UFS_NDADDR;
123 	for (i = 0; i < len; i++) {
124 		if (i < lastlbn - 1)
125 			frags = fs->fs_frag;
126 		else
127 			frags = howmany(size - (lastlbn - 1) * fs->fs_bsize,
128 					  fs->fs_fsize);
129 		if (fs->fs_magic == FS_UFS1_MAGIC)
130 			blkno = dp->dp1.di_db[i];
131 		else
132 			blkno = dp->dp2.di_db[i];
133 		printblk(fs, i, blkno, frags, lastlbn);
134 	}
135 
136 	blksperindir = 1;
137 	len = lastlbn - UFS_NDADDR;
138 	lbn = UFS_NDADDR;
139 	for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
140 		if (fs->fs_magic == FS_UFS1_MAGIC)
141 			blkno = dp->dp1.di_ib[i];
142 		else
143 			blkno = dp->dp2.di_ib[i];
144 		indirprt(fs, i, blksperindir, lbn, blkno, lastlbn);
145 		blksperindir *= NINDIR(fs);
146 		lbn += blksperindir;
147 		len -= blksperindir;
148 	}
149 
150 	/* dummy print to flush out last extent */
151 	printblk(fs, lastlbn, 0, frags, 0);
152 }
153 
154 static void
155 indirprt(struct fs *fs, int level, ufs_lbn_t blksperindir, ufs_lbn_t lbn,
156 	ufs2_daddr_t blkno, ufs_lbn_t lastlbn)
157 {
158 	char indir[MAXBSIZE];
159 	ufs_lbn_t i, last;
160 
161 	if (blkno == 0) {
162 		printblk(fs, lbn, blkno,
163 		    blksperindir * NINDIR(fs) * fs->fs_frag, lastlbn);
164 		return;
165 	}
166 	printblk(fs, lbn, blkno, fs->fs_frag, -level);
167 	/* read in the indirect block. */
168 #ifdef PRTBLKNOS
169 	if (bread(&disk, fsbtodb(fs, blkno), indir, fs->fs_bsize) == -1) {
170 #else /* used by fsdb */
171 	bp = getdatablk(blkno, fs->fs_bsize, BT_LEVEL1 + level);
172 	if (bp->b_errs == 0) {
173 		memcpy(indir, bp->b_un.b_buf, fs->fs_bsize);
174 	} else {
175 #endif
176 		warn("Read of indirect block %jd failed", (intmax_t)blkno);
177 		/* List the unreadable part as a hole */
178 		printblk(fs, lbn, 0,
179 		    blksperindir * NINDIR(fs) * fs->fs_frag, lastlbn);
180 		return;
181 	}
182 	last = howmany(lastlbn - lbn, blksperindir) < NINDIR(fs) ?
183 	    howmany(lastlbn - lbn, blksperindir) : NINDIR(fs);
184 	if (blksperindir == 1) {
185 		for (i = 0; i < last; i++) {
186 			if (fs->fs_magic == FS_UFS1_MAGIC)
187 				blkno = ((ufs1_daddr_t *)indir)[i];
188 			else
189 				blkno = ((ufs2_daddr_t *)indir)[i];
190 			printblk(fs, lbn + i, blkno, fs->fs_frag, lastlbn);
191 		}
192 		return;
193 	}
194 	for (i = 0; i < last; i++) {
195 		if (fs->fs_magic == FS_UFS1_MAGIC)
196 			blkno = ((ufs1_daddr_t *)indir)[i];
197 		else
198 			blkno = ((ufs2_daddr_t *)indir)[i];
199 		indirprt(fs, level - 1, blksperindir / NINDIR(fs),
200 		    lbn + blksperindir * i, blkno, lastlbn);
201 	}
202 }
203 
204 static const char *
205 distance(struct fs *fs, ufs2_daddr_t lastblk, ufs2_daddr_t firstblk)
206 {
207 	ufs2_daddr_t delta;
208 	int firstcg, lastcg;
209 	static char buf[100];
210 
211 	if (lastblk == 0)
212 		return ("");
213 	delta = firstblk - lastblk - 1;
214 	firstcg = dtog(fs, firstblk);
215 	lastcg = dtog(fs, lastblk);
216 	if (firstcg == lastcg) {
217 		snprintf(buf, 100, " distance %jd", (intmax_t)delta);
218 		return (&buf[0]);
219 	}
220 	snprintf(buf, 100, " cg %d blk %jd to cg %d blk %jd",
221 	    lastcg, (intmax_t)dtogd(fs, lastblk), firstcg,
222 	    (intmax_t)dtogd(fs, firstblk));
223 	return (&buf[0]);
224 }
225 
226 
227 static const char *indirname[UFS_NIADDR] = { "First", "Second", "Third" };
228 
229 static void
230 printblk(struct fs *fs, ufs_lbn_t lbn, ufs2_daddr_t blkno, int numfrags,
231 	ufs_lbn_t lastlbn)
232 {
233 	static int seq;
234 	static ufs2_daddr_t totfrags, lastindirblk, lastblk, firstblk;
235 
236 	if (lastlbn <= 0)
237 		goto flush;
238 	if (seq == 0) {
239 		seq = howmany(numfrags, fs->fs_frag);
240 		totfrags = numfrags;
241 		firstblk = blkno;
242 		return;
243 	}
244 	if (lbn == 0) {
245 		seq = howmany(numfrags, fs->fs_frag);
246 		totfrags = numfrags;
247 		lastblk = 0;
248 		firstblk = blkno;
249 		lastindirblk = 0;
250 		return;
251 	}
252 	if (lbn < lastlbn && ((firstblk == 0 && blkno == 0) ||
253 	    (firstblk == BLK_NOCOPY && blkno == BLK_NOCOPY) ||
254 	    (firstblk == BLK_SNAP && blkno == BLK_SNAP) ||
255 	    blkno == firstblk + seq * fs->fs_frag)) {
256 		seq += howmany(numfrags, fs->fs_frag);
257 		totfrags += numfrags;
258 		return;
259 	}
260 flush:
261 	if (seq == 0)
262 		goto prtindir;
263 	if (firstblk <= BLK_SNAP) {
264 		if (seq == 1)
265 			printf("\tlbn %jd %s\n", (intmax_t)(lbn - seq),
266 			    firstblk == 0 ? "hole" :
267 			    firstblk == BLK_NOCOPY ? "nocopy" :
268 			    "snapblk");
269 		else
270 			printf("\tlbn %jd-%jd %s\n",
271 			    (intmax_t)lbn - seq, (intmax_t)lbn - 1,
272 			    firstblk == 0 ? "hole" :
273 			    firstblk == BLK_NOCOPY ? "nocopy" :
274 			    "snapblk");
275 	} else if (seq == 1) {
276 		if (totfrags == 1)
277 			printf("\tlbn %jd blkno %jd%s\n", (intmax_t)(lbn - seq),
278 			   (intmax_t)firstblk, distance(fs, lastblk, firstblk));
279 		else
280 			printf("\tlbn %jd blkno %jd-%jd%s\n",
281 			    (intmax_t)(lbn - seq), (intmax_t)firstblk,
282 			    (intmax_t)(firstblk + totfrags - 1),
283 			    distance(fs, lastblk, firstblk));
284 		lastblk = firstblk + totfrags - 1;
285 	} else {
286 		printf("\tlbn %jd-%jd blkno %jd-%jd%s\n", (intmax_t)(lbn - seq),
287 		    (intmax_t)(lbn - 1), (intmax_t)firstblk,
288 		    (intmax_t)(firstblk + totfrags - 1),
289 		    distance(fs, lastblk, firstblk));
290 		lastblk = firstblk + totfrags - 1;
291 	}
292 	if (lastlbn > 0 || blkno == 0) {
293 		seq = 1;
294 		totfrags = numfrags;
295 		firstblk = blkno;
296 		return;
297 	}
298 prtindir:
299 	if (seq != 0 && (fs->fs_metaspace == 0 || lastindirblk == 0))
300 		lastindirblk = lastblk;
301 	printf("%s-level indirect, blkno %jd-%jd%s\n", indirname[-lastlbn],
302 	    (intmax_t)blkno, (intmax_t)(blkno + numfrags - 1),
303 	    distance(fs, lastindirblk, blkno));
304 	lastindirblk = blkno + numfrags - 1;
305 	if (fs->fs_metaspace == 0)
306 		lastblk = lastindirblk;
307 	seq = 0;
308 }
309