2 * Copyright (c) 1998, 2003, 2013, 2018 Marshall Kirk McKusick.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <ufs/ffs/fs.h>
39 struct ufs1_dinode dp1;
40 struct ufs2_dinode dp2;
42 extern struct uufsd disk;
43 #else /* used by fsdb */
45 static struct bufarea *bp;
48 void prtblknos(struct fs *fs, union dinode *dp);
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,
61 ufs_lbn_t lbn, lastlbn, len, blksperindir;
65 if (fs->fs_magic == FS_UFS1_MAGIC) {
66 size = dp->dp1.di_size;
67 mode = dp->dp1.di_mode;
69 size = dp->dp2.di_size;
70 mode = dp->dp2.di_mode;
72 switch (mode & IFMT) {
74 printf("unknown inode type 0%d\n", (mode & IFMT));
77 printf("unallocated inode\n");
83 printf("character device\n");
86 printf("block device\n");
96 printf("empty symbolic link\n");
99 if (size < fs->fs_maxsymlinklen) {
100 printf("symbolic link referencing %s\n",
101 (fs->fs_magic == FS_UFS1_MAGIC) ?
102 (char *)dp->dp1.di_db :
103 (char *)dp->dp2.di_db);
106 printf("symbolic link\n");
110 printf("empty file\n");
113 printf("regular file, size %jd\n", (intmax_t)size);
117 printf("empty directory\n");
120 printf("directory, size %jd\n", (intmax_t)size);
123 lastlbn = howmany(size, fs->fs_bsize);
124 len = lastlbn < UFS_NDADDR ? lastlbn : UFS_NDADDR;
125 for (i = 0; i < len; i++) {
129 frags = howmany(size - (lastlbn - 1) * fs->fs_bsize,
131 if (fs->fs_magic == FS_UFS1_MAGIC)
132 blkno = dp->dp1.di_db[i];
134 blkno = dp->dp2.di_db[i];
135 printblk(fs, i, blkno, frags, lastlbn);
139 len = lastlbn - UFS_NDADDR;
141 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
142 if (fs->fs_magic == FS_UFS1_MAGIC)
143 blkno = dp->dp1.di_ib[i];
145 blkno = dp->dp2.di_ib[i];
146 indirprt(fs, i, blksperindir, lbn, blkno, lastlbn);
147 blksperindir *= NINDIR(fs);
152 /* dummy print to flush out last extent */
153 printblk(fs, lastlbn, 0, frags, 0);
157 indirprt(fs, level, blksperindir, lbn, blkno, lastlbn)
160 ufs_lbn_t blksperindir;
165 char indir[MAXBSIZE];
169 printblk(fs, lbn, blkno,
170 blksperindir * NINDIR(fs) * fs->fs_frag, lastlbn);
173 printblk(fs, lbn, blkno, fs->fs_frag, -level);
174 /* read in the indirect block. */
176 if (bread(&disk, fsbtodb(fs, blkno), indir, fs->fs_bsize) == -1) {
177 #else /* used by fsdb */
178 bp = getdatablk(blkno, fs->fs_bsize, BT_LEVEL1 + level);
179 if (bp->b_errs == 0) {
180 memcpy(indir, bp->b_un.b_buf, fs->fs_bsize);
183 warn("Read of indirect block %jd failed", (intmax_t)blkno);
184 /* List the unreadable part as a hole */
186 blksperindir * NINDIR(fs) * fs->fs_frag, lastlbn);
189 last = howmany(lastlbn - lbn, blksperindir) < NINDIR(fs) ?
190 howmany(lastlbn - lbn, blksperindir) : NINDIR(fs);
191 if (blksperindir == 1) {
192 for (i = 0; i < last; i++) {
193 if (fs->fs_magic == FS_UFS1_MAGIC)
194 blkno = ((ufs1_daddr_t *)indir)[i];
196 blkno = ((ufs2_daddr_t *)indir)[i];
197 printblk(fs, lbn + i, blkno, fs->fs_frag, lastlbn);
201 for (i = 0; i < last; i++) {
202 if (fs->fs_magic == FS_UFS1_MAGIC)
203 blkno = ((ufs1_daddr_t *)indir)[i];
205 blkno = ((ufs2_daddr_t *)indir)[i];
206 indirprt(fs, level - 1, blksperindir / NINDIR(fs),
207 lbn + blksperindir * i, blkno, lastlbn);
212 distance(fs, lastblk, firstblk)
214 ufs2_daddr_t lastblk;
215 ufs2_daddr_t firstblk;
219 static char buf[100];
223 delta = firstblk - lastblk - 1;
224 firstcg = dtog(fs, firstblk);
225 lastcg = dtog(fs, lastblk);
226 if (firstcg == lastcg) {
227 snprintf(buf, 100, " distance %jd", (intmax_t)delta);
230 snprintf(buf, 100, " cg %d blk %jd to cg %d blk %jd",
231 lastcg, (intmax_t)dtogd(fs, lastblk), firstcg,
232 (intmax_t)dtogd(fs, firstblk));
237 static const char *indirname[UFS_NIADDR] = { "First", "Second", "Third" };
240 printblk(fs, lbn, blkno, numfrags, lastlbn)
248 static ufs2_daddr_t totfrags, lastindirblk, lastblk, firstblk;
253 seq = howmany(numfrags, fs->fs_frag);
259 seq = howmany(numfrags, fs->fs_frag);
266 if (lbn < lastlbn && ((firstblk == 0 && blkno == 0) ||
267 (firstblk == BLK_NOCOPY && blkno == BLK_NOCOPY) ||
268 (firstblk == BLK_SNAP && blkno == BLK_SNAP) ||
269 blkno == firstblk + seq * fs->fs_frag)) {
270 seq += howmany(numfrags, fs->fs_frag);
271 totfrags += numfrags;
277 if (firstblk <= BLK_SNAP) {
279 printf("\tlbn %jd %s\n", (intmax_t)(lbn - seq),
280 firstblk == 0 ? "hole" :
281 firstblk == BLK_NOCOPY ? "nocopy" :
284 printf("\tlbn %jd-%jd %s\n",
285 (intmax_t)lbn - seq, (intmax_t)lbn - 1,
286 firstblk == 0 ? "hole" :
287 firstblk == BLK_NOCOPY ? "nocopy" :
289 } else if (seq == 1) {
291 printf("\tlbn %jd blkno %jd%s\n", (intmax_t)(lbn - seq),
292 (intmax_t)firstblk, distance(fs, lastblk, firstblk));
294 printf("\tlbn %jd blkno %jd-%jd%s\n",
295 (intmax_t)(lbn - seq), (intmax_t)firstblk,
296 (intmax_t)(firstblk + totfrags - 1),
297 distance(fs, lastblk, firstblk));
298 lastblk = firstblk + totfrags - 1;
300 printf("\tlbn %jd-%jd blkno %jd-%jd%s\n", (intmax_t)(lbn - seq),
301 (intmax_t)(lbn - 1), (intmax_t)firstblk,
302 (intmax_t)(firstblk + totfrags - 1),
303 distance(fs, lastblk, firstblk));
304 lastblk = firstblk + totfrags - 1;
306 if (lastlbn > 0 || blkno == 0) {
313 if (seq != 0 && (fs->fs_metaspace == 0 || lastindirblk == 0))
314 lastindirblk = lastblk;
315 printf("%s-level indirect, blkno %jd-%jd%s\n", indirname[-lastlbn],
316 (intmax_t)blkno, (intmax_t)(blkno + numfrags - 1),
317 distance(fs, lastindirblk, blkno));
318 lastindirblk = blkno + numfrags - 1;
319 if (fs->fs_metaspace == 0)
320 lastblk = lastindirblk;