2018-02-09 19:10:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1998, 2003, 2013, 2018 Marshall Kirk McKusick.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
2018-04-08 06:52:58 +00:00
|
|
|
#include <libufs.h>
|
2018-02-09 19:10:46 +00:00
|
|
|
|
|
|
|
union dinode {
|
2018-04-08 06:52:58 +00:00
|
|
|
struct ufs1_dinode dp1;
|
|
|
|
struct ufs2_dinode dp2;
|
2018-02-09 19:10:46 +00:00
|
|
|
};
|
|
|
|
|
2018-04-08 06:52:58 +00:00
|
|
|
void prtblknos(struct uufsd *disk, union dinode *dp);
|
2018-02-09 19:10:46 +00:00
|
|
|
|
2018-04-19 22:43:55 +00:00
|
|
|
static const char *distance(struct fs *, ufs2_daddr_t, ufs2_daddr_t);
|
|
|
|
static void printblk(struct fs *, ufs_lbn_t, ufs2_daddr_t, int, ufs_lbn_t);
|
|
|
|
static void indirprt(struct uufsd *, int, ufs_lbn_t, ufs_lbn_t, ufs2_daddr_t,
|
|
|
|
ufs_lbn_t);
|
2018-02-09 19:10:46 +00:00
|
|
|
|
2018-04-08 06:52:58 +00:00
|
|
|
void
|
|
|
|
prtblknos(disk, dp)
|
|
|
|
struct uufsd *disk;
|
|
|
|
union dinode *dp;
|
2018-02-09 19:10:46 +00:00
|
|
|
{
|
2018-04-19 22:43:55 +00:00
|
|
|
int i, mode, frags;
|
|
|
|
ufs_lbn_t lbn, lastlbn, len, blksperindir;
|
2018-02-09 19:10:46 +00:00
|
|
|
ufs2_daddr_t blkno;
|
2018-04-08 06:52:58 +00:00
|
|
|
struct fs *fs;
|
|
|
|
off_t size;
|
|
|
|
|
|
|
|
fs = (struct fs *)&disk->d_sb;
|
2018-04-18 22:24:44 +00:00
|
|
|
if (fs->fs_magic == FS_UFS1_MAGIC) {
|
2018-04-08 06:52:58 +00:00
|
|
|
size = dp->dp1.di_size;
|
2018-04-18 22:24:44 +00:00
|
|
|
mode = dp->dp1.di_mode;
|
|
|
|
} else {
|
2018-04-08 06:52:58 +00:00
|
|
|
size = dp->dp2.di_size;
|
2018-04-18 22:24:44 +00:00
|
|
|
mode = dp->dp2.di_mode;
|
|
|
|
}
|
|
|
|
switch (mode & IFMT) {
|
2018-04-19 22:43:55 +00:00
|
|
|
default:
|
|
|
|
printf("unknown inode type 0%d\n", (mode & IFMT));
|
|
|
|
return;
|
|
|
|
case 0:
|
|
|
|
printf("unallocated inode\n");
|
|
|
|
return;
|
2018-04-18 22:24:44 +00:00
|
|
|
case IFIFO:
|
|
|
|
printf("fifo\n");
|
|
|
|
return;
|
|
|
|
case IFCHR:
|
|
|
|
printf("character device\n");
|
|
|
|
return;
|
|
|
|
case IFBLK:
|
|
|
|
printf("block device\n");
|
2018-04-08 06:52:58 +00:00
|
|
|
return;
|
2018-04-18 22:24:44 +00:00
|
|
|
case IFSOCK:
|
|
|
|
printf("socket\n");
|
|
|
|
return;
|
|
|
|
case IFWHT:
|
|
|
|
printf("whiteout\n");
|
|
|
|
return;
|
|
|
|
case IFLNK:
|
|
|
|
if (size == 0) {
|
|
|
|
printf("empty symbolic link\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (size < fs->fs_maxsymlinklen) {
|
|
|
|
printf("symbolic link referencing %s\n",
|
|
|
|
(fs->fs_magic == FS_UFS1_MAGIC) ?
|
|
|
|
(char *)dp->dp1.di_db :
|
|
|
|
(char *)dp->dp2.di_db);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("symbolic link\n");
|
|
|
|
break;
|
|
|
|
case IFREG:
|
|
|
|
if (size == 0) {
|
|
|
|
printf("empty file\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-19 22:43:55 +00:00
|
|
|
printf("regular file, size %jd\n", (intmax_t)size);
|
2018-04-18 22:24:44 +00:00
|
|
|
break;
|
|
|
|
case IFDIR:
|
|
|
|
if (size == 0) {
|
|
|
|
printf("empty directory\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-04-19 22:43:55 +00:00
|
|
|
printf("directory, size %jd\n", (intmax_t)size);
|
2018-04-18 22:24:44 +00:00
|
|
|
break;
|
2018-02-09 19:10:46 +00:00
|
|
|
}
|
2018-04-19 22:43:55 +00:00
|
|
|
lastlbn = howmany(size, fs->fs_bsize);
|
|
|
|
len = lastlbn < UFS_NDADDR ? lastlbn : UFS_NDADDR;
|
2018-04-08 06:52:58 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2018-04-19 22:43:55 +00:00
|
|
|
if (i < lastlbn - 1)
|
2018-04-08 06:52:58 +00:00
|
|
|
frags = fs->fs_frag;
|
|
|
|
else
|
|
|
|
frags = howmany(size % fs->fs_bsize,
|
|
|
|
fs->fs_fsize);
|
|
|
|
if (fs->fs_magic == FS_UFS1_MAGIC)
|
|
|
|
blkno = dp->dp1.di_db[i];
|
|
|
|
else
|
|
|
|
blkno = dp->dp2.di_db[i];
|
2018-04-19 22:43:55 +00:00
|
|
|
printblk(fs, i, blkno, frags, lastlbn);
|
2018-02-09 19:10:46 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 06:52:58 +00:00
|
|
|
blksperindir = 1;
|
2018-04-19 22:43:55 +00:00
|
|
|
len = lastlbn - UFS_NDADDR;
|
2018-04-08 06:52:58 +00:00
|
|
|
lbn = UFS_NDADDR;
|
|
|
|
for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
|
|
|
|
if (fs->fs_magic == FS_UFS1_MAGIC)
|
|
|
|
blkno = dp->dp1.di_ib[i];
|
|
|
|
else
|
|
|
|
blkno = dp->dp2.di_ib[i];
|
2018-04-19 22:43:55 +00:00
|
|
|
indirprt(disk, i, blksperindir, lbn, blkno, lastlbn);
|
2018-04-08 06:52:58 +00:00
|
|
|
blksperindir *= NINDIR(fs);
|
|
|
|
lbn += blksperindir;
|
|
|
|
len -= blksperindir;
|
2018-02-09 19:10:46 +00:00
|
|
|
}
|
2018-04-08 06:52:58 +00:00
|
|
|
|
|
|
|
/* dummy print to flush out last extent */
|
2018-04-19 22:43:55 +00:00
|
|
|
printblk(fs, lastlbn, 0, frags, 0);
|
2018-02-09 19:10:46 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 06:52:58 +00:00
|
|
|
static void
|
|
|
|
indirprt(disk, level, blksperindir, lbn, blkno, lastlbn)
|
|
|
|
struct uufsd *disk;
|
2018-02-09 19:10:46 +00:00
|
|
|
int level;
|
2018-04-19 22:43:55 +00:00
|
|
|
ufs_lbn_t blksperindir;
|
|
|
|
ufs_lbn_t lbn;
|
2018-02-09 19:10:46 +00:00
|
|
|
ufs2_daddr_t blkno;
|
2018-04-19 22:43:55 +00:00
|
|
|
ufs_lbn_t lastlbn;
|
2018-02-09 19:10:46 +00:00
|
|
|
{
|
|
|
|
char indir[MAXBSIZE];
|
2018-04-08 06:52:58 +00:00
|
|
|
struct fs *fs;
|
2018-04-19 22:43:55 +00:00
|
|
|
ufs_lbn_t i, last;
|
2018-02-09 19:10:46 +00:00
|
|
|
|
2018-04-08 06:52:58 +00:00
|
|
|
fs = (struct fs *)&disk->d_sb;
|
2018-04-18 22:24:44 +00:00
|
|
|
if (blkno == 0) {
|
|
|
|
printblk(fs, lbn, blkno,
|
|
|
|
blksperindir * NINDIR(fs) * fs->fs_frag, lastlbn);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-08 06:52:58 +00:00
|
|
|
printblk(fs, lbn, blkno, fs->fs_frag, -level);
|
2018-02-09 19:10:46 +00:00
|
|
|
/* read in the indirect block. */
|
2018-04-18 23:08:10 +00:00
|
|
|
if (bread(disk, fsbtodb(fs, blkno), indir, fs->fs_bsize) == -1) {
|
|
|
|
warn("Read of indirect block %jd failed", (intmax_t)blkno);
|
|
|
|
/* List the unreadable part as a hole */
|
|
|
|
printblk(fs, lbn, 0,
|
|
|
|
blksperindir * NINDIR(fs) * fs->fs_frag, lastlbn);
|
|
|
|
return;
|
|
|
|
}
|
2018-04-08 06:52:58 +00:00
|
|
|
last = howmany(lastlbn - lbn, blksperindir) < NINDIR(fs) ?
|
|
|
|
howmany(lastlbn - lbn, blksperindir) : NINDIR(fs);
|
2018-02-09 19:10:46 +00:00
|
|
|
if (blksperindir == 1) {
|
|
|
|
for (i = 0; i < last; i++) {
|
2018-04-08 06:52:58 +00:00
|
|
|
if (fs->fs_magic == FS_UFS1_MAGIC)
|
2018-02-09 19:10:46 +00:00
|
|
|
blkno = ((ufs1_daddr_t *)indir)[i];
|
|
|
|
else
|
|
|
|
blkno = ((ufs2_daddr_t *)indir)[i];
|
2018-04-08 06:52:58 +00:00
|
|
|
printblk(fs, lbn + i, blkno, fs->fs_frag, lastlbn);
|
2018-02-09 19:10:46 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i = 0; i < last; i++) {
|
2018-04-08 06:52:58 +00:00
|
|
|
if (fs->fs_magic == FS_UFS1_MAGIC)
|
2018-02-09 19:10:46 +00:00
|
|
|
blkno = ((ufs1_daddr_t *)indir)[i];
|
|
|
|
else
|
|
|
|
blkno = ((ufs2_daddr_t *)indir)[i];
|
2018-04-08 06:52:58 +00:00
|
|
|
indirprt(disk, level - 1, blksperindir / NINDIR(fs),
|
2018-02-09 19:10:46 +00:00
|
|
|
lbn + blksperindir * i, blkno, lastlbn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 22:43:55 +00:00
|
|
|
static const char *
|
2018-04-08 06:52:58 +00:00
|
|
|
distance(fs, lastblk, firstblk)
|
|
|
|
struct fs *fs;
|
2018-04-19 22:43:55 +00:00
|
|
|
ufs2_daddr_t lastblk;
|
|
|
|
ufs2_daddr_t firstblk;
|
2018-02-09 19:10:46 +00:00
|
|
|
{
|
2018-04-19 22:43:55 +00:00
|
|
|
ufs2_daddr_t delta;
|
2018-02-09 19:10:46 +00:00
|
|
|
int firstcg, lastcg;
|
|
|
|
static char buf[100];
|
|
|
|
|
|
|
|
if (lastblk == 0)
|
|
|
|
return ("");
|
|
|
|
delta = firstblk - lastblk - 1;
|
2018-04-08 06:52:58 +00:00
|
|
|
firstcg = dtog(fs, firstblk);
|
|
|
|
lastcg = dtog(fs, lastblk);
|
2018-02-09 19:10:46 +00:00
|
|
|
if (firstcg == lastcg) {
|
|
|
|
snprintf(buf, 100, " distance %jd", (intmax_t)delta);
|
|
|
|
return (&buf[0]);
|
|
|
|
}
|
|
|
|
snprintf(buf, 100, " cg %d blk %jd to cg %d blk %jd",
|
2018-04-08 07:18:29 +00:00
|
|
|
lastcg, (intmax_t)dtogd(fs, lastblk), firstcg,
|
|
|
|
(intmax_t)dtogd(fs, firstblk));
|
2018-02-09 19:10:46 +00:00
|
|
|
return (&buf[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-19 22:43:55 +00:00
|
|
|
static const char *indirname[UFS_NIADDR] = { "First", "Second", "Third" };
|
2018-02-09 19:10:46 +00:00
|
|
|
|
2018-04-08 06:52:58 +00:00
|
|
|
static void
|
2018-04-19 22:43:55 +00:00
|
|
|
printblk(fs, lbn, blkno, numfrags, lastlbn)
|
2018-04-08 06:52:58 +00:00
|
|
|
struct fs *fs;
|
2018-04-19 22:43:55 +00:00
|
|
|
ufs_lbn_t lbn;
|
2018-02-09 19:10:46 +00:00
|
|
|
ufs2_daddr_t blkno;
|
2018-04-19 22:43:55 +00:00
|
|
|
int numfrags;
|
|
|
|
ufs_lbn_t lastlbn;
|
2018-02-09 19:10:46 +00:00
|
|
|
{
|
|
|
|
static int seq;
|
2018-04-19 22:43:55 +00:00
|
|
|
static ufs2_daddr_t totfrags, lastindirblk, lastblk, firstblk;
|
2018-02-09 19:10:46 +00:00
|
|
|
|
|
|
|
if (lastlbn <= 0)
|
|
|
|
goto flush;
|
|
|
|
if (seq == 0) {
|
2018-04-19 22:43:55 +00:00
|
|
|
seq = howmany(numfrags, fs->fs_frag);
|
|
|
|
totfrags = numfrags;
|
2018-02-09 19:10:46 +00:00
|
|
|
firstblk = blkno;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lbn == 0) {
|
2018-04-19 22:43:55 +00:00
|
|
|
seq = howmany(numfrags, fs->fs_frag);
|
|
|
|
totfrags = numfrags;
|
2018-02-09 19:10:46 +00:00
|
|
|
lastblk = 0;
|
|
|
|
firstblk = blkno;
|
|
|
|
lastindirblk = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lbn < lastlbn && ((firstblk == 0 && blkno == 0) ||
|
|
|
|
(firstblk == BLK_NOCOPY && blkno == BLK_NOCOPY) ||
|
|
|
|
(firstblk == BLK_SNAP && blkno == BLK_SNAP) ||
|
2018-04-18 22:24:44 +00:00
|
|
|
blkno == firstblk + seq * fs->fs_frag)) {
|
2018-04-19 22:43:55 +00:00
|
|
|
seq += howmany(numfrags, fs->fs_frag);
|
|
|
|
totfrags += numfrags;
|
2018-02-09 19:10:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
flush:
|
|
|
|
if (seq == 0)
|
|
|
|
goto prtindir;
|
|
|
|
if (firstblk <= BLK_SNAP) {
|
|
|
|
if (seq == 1)
|
2018-04-19 22:43:55 +00:00
|
|
|
printf("\tlbn %jd %s\n", (intmax_t)(lbn - seq),
|
2018-02-09 19:10:46 +00:00
|
|
|
firstblk == 0 ? "hole" :
|
|
|
|
firstblk == BLK_NOCOPY ? "nocopy" :
|
|
|
|
"snapblk");
|
|
|
|
else
|
2018-04-19 22:43:55 +00:00
|
|
|
printf("\tlbn %jd-%jd %s\n",
|
|
|
|
(intmax_t)lbn - seq, (intmax_t)lbn - 1,
|
2018-02-09 19:10:46 +00:00
|
|
|
firstblk == 0 ? "hole" :
|
|
|
|
firstblk == BLK_NOCOPY ? "nocopy" :
|
|
|
|
"snapblk");
|
|
|
|
} else if (seq == 1) {
|
2018-04-19 22:43:55 +00:00
|
|
|
if (totfrags == 1)
|
|
|
|
printf("\tlbn %jd blkno %jd%s\n", (intmax_t)(lbn - seq),
|
2018-04-08 06:52:58 +00:00
|
|
|
(intmax_t)firstblk, distance(fs, lastblk, firstblk));
|
2018-02-09 19:10:46 +00:00
|
|
|
else
|
2018-04-19 22:43:55 +00:00
|
|
|
printf("\tlbn %jd blkno %jd-%jd%s\n",
|
|
|
|
(intmax_t)(lbn - seq), (intmax_t)firstblk,
|
|
|
|
(intmax_t)(firstblk + totfrags - 1),
|
2018-04-08 06:52:58 +00:00
|
|
|
distance(fs, lastblk, firstblk));
|
2018-04-19 22:43:55 +00:00
|
|
|
lastblk = firstblk + totfrags - 1;
|
2018-02-09 19:10:46 +00:00
|
|
|
} else {
|
2018-04-19 22:43:55 +00:00
|
|
|
printf("\tlbn %jd-%jd blkno %jd-%jd%s\n", (intmax_t)(lbn - seq),
|
|
|
|
(intmax_t)(lbn - 1), (intmax_t)firstblk,
|
|
|
|
(intmax_t)(firstblk + totfrags - 1),
|
2018-04-08 06:52:58 +00:00
|
|
|
distance(fs, lastblk, firstblk));
|
2018-04-19 22:43:55 +00:00
|
|
|
lastblk = firstblk + totfrags - 1;
|
2018-02-09 19:10:46 +00:00
|
|
|
}
|
|
|
|
if (lastlbn > 0 || blkno == 0) {
|
|
|
|
seq = 1;
|
2018-04-19 22:43:55 +00:00
|
|
|
totfrags = numfrags;
|
2018-02-09 19:10:46 +00:00
|
|
|
firstblk = blkno;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prtindir:
|
2018-04-08 06:52:58 +00:00
|
|
|
if (seq != 0 && (fs->fs_metaspace == 0 || lastindirblk == 0))
|
2018-02-09 19:10:46 +00:00
|
|
|
lastindirblk = lastblk;
|
|
|
|
printf("%s-level indirect, blkno %jd-%jd%s\n", indirname[-lastlbn],
|
2018-04-19 22:43:55 +00:00
|
|
|
(intmax_t)blkno, (intmax_t)(blkno + numfrags - 1),
|
2018-04-08 06:52:58 +00:00
|
|
|
distance(fs, lastindirblk, blkno));
|
2018-04-19 22:43:55 +00:00
|
|
|
lastindirblk = blkno + numfrags - 1;
|
2018-04-08 06:52:58 +00:00
|
|
|
if (fs->fs_metaspace == 0)
|
2018-02-09 19:10:46 +00:00
|
|
|
lastblk = lastindirblk;
|
|
|
|
seq = 0;
|
|
|
|
}
|