debugfs: add allocate and set functionality to the bmap command

This also makes it easier test the ext2fs_bmap2() function.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
crypto
Theodore Ts'o 2014-10-13 11:59:30 -04:00
parent 08c8e319e3
commit 9ed2c124f3
2 changed files with 49 additions and 10 deletions

View File

@ -167,11 +167,14 @@ Print the blocks used by the inode
.I filespec
to stdout.
.TP
.BI bmap " filespec logical_block"
Print the physical block number corresponding to the logical block number
.BI bmap " [ -a ] filespec logical_block [physical_block]"
Print or set the physical block number corresponding to the logical block number
.I logical_block
in the inode
.IR filespec .
If the
.I -a
flag is specified, try to allocate a block if necessary.
.TP
.BI block_dump " [-f filespec] block_num"
Dump the filesystem block given by

View File

@ -1863,28 +1863,64 @@ void do_features(int argc, char *argv[])
void do_bmap(int argc, char *argv[])
{
ext2_ino_t ino;
blk64_t blk, pblk;
int err;
blk64_t blk, pblk = 0;
int c, err, flags = 0, ret_flags = 0;
errcode_t errcode;
if (common_args_process(argc, argv, 3, 3, argv[0],
"<file> logical_blk", 0))
if (check_fs_open(argv[0]))
return;
ino = string_to_inode(argv[1]);
reset_getopt();
while ((c = getopt (argc, argv, "a")) != EOF) {
switch (c) {
case 'a':
flags |= BMAP_ALLOC;
break;
default:
goto print_usage;
}
}
if (argc <= optind+1) {
print_usage:
com_err(0, 0,
"Usage: bmap [-a] <file> logical_blk [physical_blk]");
return;
}
ino = string_to_inode(argv[optind++]);
if (!ino)
return;
err = strtoblk(argv[0], argv[2], "logical block", &blk);
err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
if (err)
return;
errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
if (argc > optind+1)
goto print_usage;
if (argc == optind+1) {
err = strtoblk(argv[0], argv[optind++],
"physical block", &pblk);
if (err)
return;
if (flags & BMAP_ALLOC) {
com_err(0, 0, "Can't set and allocate a block");
return;
}
flags |= BMAP_SET;
}
errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
&ret_flags, &pblk);
if (errcode) {
com_err(argv[0], errcode,
"while mapping logical block %llu\n", blk);
return;
}
printf("%llu\n", pblk);
printf("%llu", pblk);
if (ret_flags & BMAP_RET_UNINIT)
fputs(" (uninit)", stdout);
fputc('\n', stdout);
}
void do_imap(int argc, char *argv[])