#include <sys/types.h>
#include <sys/stat.h>
-#include "et/com_err.h"
-#include "ss/ss.h"
#include "debugfs.h"
#include "uuid/uuid.h"
#include "e2p/e2p.h"
#include "../version.h"
#include "jfs_user.h"
-extern ss_request_table debug_cmds;
+#ifndef BUFSIZ
+#define BUFSIZ 8192
+#endif
+
+/* 64KiB is the minimium blksize to best minimize system call overhead. */
+#ifndef IO_BUFSIZE
+#define IO_BUFSIZE 64*1024
+#endif
+
+/* Block size for `st_blocks' */
+#ifndef S_BLKSIZE
+#define S_BLKSIZE 512
+#endif
+
ss_request_table *extra_cmds;
const char *debug_prog_name;
+int sci_idx;
ext2_filsys current_fs = NULL;
+quota_ctx_t current_qctx;
ext2_ino_t root, cwd;
static void open_filesystem(char *device, int open_flags, blk64_t superblock,
current_fs = NULL;
return;
}
+ current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
if (catastrophic)
com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
return;
errout:
- retval = ext2fs_close(current_fs);
+ retval = ext2fs_close_free(¤t_fs);
if (retval)
com_err(device, retval, "while trying to close filesystem");
- current_fs = NULL;
}
void do_open_filesys(int argc, char **argv)
open_flags |= EXT2_FLAG_IMAGE_FILE;
break;
case 'w':
+#ifdef READ_ONLY
+ goto print_usage;
+#else
open_flags |= EXT2_FLAG_RW;
+#endif /* READ_ONLY */
break;
case 'f':
open_flags |= EXT2_FLAG_FORCE;
return;
break;
case 's':
- superblock = parse_ulong(optarg, argv[0],
- "superblock number", &err);
+ err = strtoblk(argv[0], optarg,
+ "superblock block number", &superblock);
if (err)
return;
break;
print_usage:
fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
- "[-c] [-w] <device>\n", argv[0]);
+ "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
+#ifndef READ_ONLY
+ "[-w] "
+#endif
+ "<device>\n", argv[0]);
}
void do_lcd(int argc, char **argv)
if (retval)
com_err("ext2fs_write_block_bitmap", retval, 0);
}
- retval = ext2fs_close(current_fs);
+ if (current_qctx)
+ quota_release_context(¤t_qctx);
+ retval = ext2fs_close_free(¤t_fs);
if (retval)
com_err("ext2fs_close", retval, 0);
- current_fs = NULL;
return;
}
close_filesystem();
}
+#ifndef READ_ONLY
void do_init_filesys(int argc, char **argv)
{
struct ext2_super_block param;
errcode_t retval;
int err;
+ blk64_t blocks;
if (common_args_process(argc, argv, 3, 3, "initialize",
- "<device> <blocksize>", CHECK_FS_NOTOPEN))
+ "<device> <blocks>", CHECK_FS_NOTOPEN))
return;
memset(¶m, 0, sizeof(struct ext2_super_block));
- ext2fs_blocks_count_set(¶m, parse_ulong(argv[2], argv[0],
- "blocks count", &err));
+ err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
if (err)
return;
+ ext2fs_blocks_count_set(¶m, blocks);
retval = ext2fs_initialize(argv[1], 0, ¶m,
unix_io_manager, ¤t_fs);
if (retval) {
fputs("(none)", f);
fputs("\n", f);
}
+#endif /* READ_ONLY */
static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
const char *str, int *first, FILE *f)
fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
}
+#ifndef READ_ONLY
void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
char **argv EXT2FS_ATTR((unused)))
{
current_fs->super->s_state &= ~EXT2_VALID_FS;
ext2fs_mark_super_dirty(current_fs);
}
+#endif /* READ_ONLY */
struct list_blocks_struct {
FILE *f;
struct ext2_ext_attr_entry *entry;
__u32 *magic;
char *start, *end;
- unsigned int storage_size;
fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
inode->i_extra_isize);
return;
}
- storage_size = EXT2_INODE_SIZE(current_fs->super) -
- EXT2_GOOD_OLD_INODE_SIZE -
- inode->i_extra_isize;
magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
inode->i_extra_isize);
if (*magic == EXT2_EXT_ATTR_MAGIC) {
while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
struct ext2_ext_attr_entry *next =
EXT2_EXT_ATTR_NEXT(entry);
- if (entry->e_value_size > storage_size ||
- (char *) next >= end) {
+ char *name = EXT2_EXT_ATTR_NAME(entry);
+ char *value = start + entry->e_value_offs;
+
+ if (name + entry->e_name_len >= end ||
+ value + entry->e_value_size >= end ||
+ (char *) next >= end) {
fprintf(out, "invalid EA entry in inode\n");
return;
}
fprintf(out, " ");
- dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
- entry->e_name_len);
+ dump_xattr_string(out, name, entry->e_name_len);
fprintf(out, " = \"");
- dump_xattr_string(out, start + entry->e_value_offs,
- entry->e_value_size);
+ dump_xattr_string(out, value, entry->e_value_size);
fprintf(out, "\" (%u)\n", entry->e_value_size);
entry = next;
}
static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
blk64_t *blocknr,
- e2_blkcnt_t blockcnt,
+ e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
blk64_t ref_block EXT2FS_ATTR((unused)),
int ref_offset EXT2FS_ATTR((unused)),
void *private EXT2FS_ATTR((unused)))
root = inode;
}
+#ifndef READ_ONLY
void do_clri(int argc, char *argv[])
{
ext2_ino_t inode;
void do_freei(int argc, char *argv[])
{
- ext2_ino_t inode;
+ unsigned int len = 1;
+ int err = 0;
+ ext2_ino_t inode;
- if (common_inode_args_process(argc, argv, &inode,
- CHECK_FS_RW | CHECK_FS_BITMAPS))
+ if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
+ CHECK_FS_RW | CHECK_FS_BITMAPS))
+ return;
+ if (check_fs_read_write(argv[0]))
+ return;
+
+ inode = string_to_inode(argv[1]);
+ if (!inode)
return;
- if (!ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
+ if (argc == 3) {
+ len = parse_ulong(argv[2], argv[0], "length", &err);
+ if (err)
+ return;
+ }
+
+ if (len == 1 &&
+ !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
com_err(argv[0], 0, "Warning: inode already clear");
- ext2fs_unmark_inode_bitmap2(current_fs->inode_map,inode);
+ while (len-- > 0)
+ ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
ext2fs_mark_ib_dirty(current_fs);
}
void do_seti(int argc, char *argv[])
{
- ext2_ino_t inode;
+ unsigned int len = 1;
+ int err = 0;
+ ext2_ino_t inode;
- if (common_inode_args_process(argc, argv, &inode,
- CHECK_FS_RW | CHECK_FS_BITMAPS))
+ if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
+ CHECK_FS_RW | CHECK_FS_BITMAPS))
+ return;
+ if (check_fs_read_write(argv[0]))
return;
- if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
+ inode = string_to_inode(argv[1]);
+ if (!inode)
+ return;
+
+ if (argc == 3) {
+ len = parse_ulong(argv[2], argv[0], "length", &err);
+ if (err)
+ return;
+ }
+
+ if ((len == 1) &&
+ ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
com_err(argv[0], 0, "Warning: inode already set");
- ext2fs_mark_inode_bitmap2(current_fs->inode_map,inode);
+ while (len-- > 0)
+ ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
ext2fs_mark_ib_dirty(current_fs);
}
+#endif /* READ_ONLY */
void do_testi(int argc, char *argv[])
{
printf("Inode %u is not in use\n", inode);
}
+#ifndef READ_ONLY
void do_freeb(int argc, char *argv[])
{
blk64_t block;
}
ext2fs_mark_bb_dirty(current_fs);
}
+#endif /* READ_ONLY */
void do_testb(int argc, char *argv[])
{
}
}
+#ifndef READ_ONLY
static void modify_u8(char *com, const char *prompt,
const char *format, __u8 *val)
{
if (debugfs_write_inode(inode_num, &inode, argv[1]))
return;
}
+#endif /* READ_ONLY */
void do_change_working_dir(int argc, char *argv[])
{
return;
}
-/*
- * Given a mode, return the ext2 file type
- */
-static int ext2_file_type(unsigned int mode)
-{
- if (LINUX_S_ISREG(mode))
- return EXT2_FT_REG_FILE;
-
- if (LINUX_S_ISDIR(mode))
- return EXT2_FT_DIR;
-
- if (LINUX_S_ISCHR(mode))
- return EXT2_FT_CHRDEV;
-
- if (LINUX_S_ISBLK(mode))
- return EXT2_FT_BLKDEV;
-
- if (LINUX_S_ISLNK(mode))
- return EXT2_FT_SYMLINK;
-
- if (LINUX_S_ISFIFO(mode))
- return EXT2_FT_FIFO;
-
- if (LINUX_S_ISSOCK(mode))
- return EXT2_FT_SOCK;
-
- return 0;
-}
-
+#ifndef READ_ONLY
static void make_link(char *sourcename, char *destname)
{
ext2_ino_t ino;
unlink_file_by_name(argv[1]);
}
+#endif /* READ_ONLY */
void do_find_free_block(int argc, char *argv[])
{
printf("Free inode found: %u\n", free_inode);
}
-static errcode_t copy_file(int fd, ext2_ino_t newfile)
+#ifndef READ_ONLY
+static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize,
+ int make_holes)
{
ext2_file_t e2_file;
- errcode_t retval;
+ errcode_t retval, close_ret;
int got;
unsigned int written;
- char buf[8192];
+ char *buf;
char *ptr;
+ char *zero_buf;
+ int cmp;
retval = ext2fs_file_open(current_fs, newfile,
EXT2_FILE_WRITE, &e2_file);
if (retval)
return retval;
+ retval = ext2fs_get_mem(bufsize, &buf);
+ if (retval) {
+ com_err("copy_file", retval, "can't allocate buffer\n");
+ goto out_close;
+ }
+
+ /* This is used for checking whether the whole block is zero */
+ retval = ext2fs_get_memzero(bufsize, &zero_buf);
+ if (retval) {
+ com_err("copy_file", retval, "can't allocate zero buffer\n");
+ goto out_free_buf;
+ }
+
while (1) {
- got = read(fd, buf, sizeof(buf));
+ got = read(fd, buf, bufsize);
if (got == 0)
break;
if (got < 0) {
goto fail;
}
ptr = buf;
+
+ /* Sparse copy */
+ if (make_holes) {
+ /* Check whether all is zero */
+ cmp = memcmp(ptr, zero_buf, got);
+ if (cmp == 0) {
+ /* The whole block is zero, make a hole */
+ retval = ext2fs_file_lseek(e2_file, got,
+ EXT2_SEEK_CUR, NULL);
+ if (retval)
+ goto fail;
+ got = 0;
+ }
+ }
+
+ /* Normal copy */
while (got > 0) {
retval = ext2fs_file_write(e2_file, ptr,
got, &written);
ptr += written;
}
}
- retval = ext2fs_file_close(e2_file);
- return retval;
fail:
- (void) ext2fs_file_close(e2_file);
+ ext2fs_free_mem(&zero_buf);
+out_free_buf:
+ ext2fs_free_mem(&buf);
+out_close:
+ close_ret = ext2fs_file_close(e2_file);
+ if (retval == 0)
+ retval = close_ret;
return retval;
}
ext2_ino_t newfile;
errcode_t retval;
struct ext2_inode inode;
+ int bufsize = IO_BUFSIZE;
+ int make_holes = 0;
if (common_args_process(argc, argv, 3, 3, "write",
"<native file> <new file>", CHECK_FS_RW))
inode.i_atime = inode.i_ctime = inode.i_mtime =
current_fs->now ? current_fs->now : time(0);
inode.i_links_count = 1;
- inode.i_size = statbuf.st_size;
+ retval = ext2fs_inode_size_set(current_fs, &inode, statbuf.st_size);
+ if (retval) {
+ com_err(argv[2], retval, 0);
+ close(fd);
+ return;
+ }
if (current_fs->super->s_feature_incompat &
- EXT3_FEATURE_INCOMPAT_EXTENTS)
+ EXT3_FEATURE_INCOMPAT_EXTENTS) {
+ int i;
+ struct ext3_extent_header *eh;
+
+ eh = (struct ext3_extent_header *) &inode.i_block[0];
+ eh->eh_depth = 0;
+ eh->eh_entries = 0;
+ eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
+ i = (sizeof(inode.i_block) - sizeof(*eh)) /
+ sizeof(struct ext3_extent);
+ eh->eh_max = ext2fs_cpu_to_le16(i);
inode.i_flags |= EXT4_EXTENTS_FL;
+ }
if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
close(fd);
return;
}
if (LINUX_S_ISREG(inode.i_mode)) {
- retval = copy_file(fd, newfile);
+ if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
+ make_holes = 1;
+ /*
+ * Use I/O blocksize as buffer size when
+ * copying sparse files.
+ */
+ bufsize = statbuf.st_blksize;
+ }
+ retval = copy_file(fd, newfile, bufsize, make_holes);
if (retval)
com_err("copy_file", retval, 0);
}
}
if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
com_err(argv[0], 0, "Warning: inode already set");
- ext2fs_mark_inode_bitmap2(current_fs->inode_map, newfile);
- ext2fs_mark_ib_dirty(current_fs);
+ ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
memset(&inode, 0, sizeof(inode));
inode.i_mode = mode;
inode.i_atime = inode.i_ctime = inode.i_mtime =
return;
}
}
+#endif /* READ_ONLY */
void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
char *argv[] EXT2FS_ATTR((unused)))
{
- FILE *out = stdout;
-
if (current_fs)
- fprintf(out, "Open mode: read-%s\n",
- current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
- fprintf(out, "Filesystem in use: %s\n",
- current_fs ? current_fs->device_name : "--none--");
+ printf("Open mode: read-%s\n",
+ current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
+ printf("Filesystem in use: %s\n",
+ current_fs ? current_fs->device_name : "--none--");
}
+#ifndef READ_ONLY
void do_expand_dir(int argc, char *argv[])
{
ext2_ino_t inode;
}
print_features(current_fs->super, stdout);
}
+#endif /* READ_ONLY */
void do_bmap(int argc, char *argv[])
{
ino = string_to_inode(argv[1]);
if (!ino)
return;
- blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
+ err = strtoblk(argv[0], argv[2], "logical block", &blk);
+ if (err)
+ return;
errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
if (errcode) {
- com_err("argv[0]", errcode,
+ com_err(argv[0], errcode,
"while mapping logical block %llu\n", blk);
return;
}
}
+void do_idump(int argc, char *argv[])
+{
+ ext2_ino_t ino;
+ unsigned char *buf;
+ errcode_t err;
+ int isize;
+
+ if (common_args_process(argc, argv, 2, 2, argv[0],
+ "<file>", 0))
+ return;
+ ino = string_to_inode(argv[1]);
+ if (!ino)
+ return;
+
+ isize = EXT2_INODE_SIZE(current_fs->super);
+ err = ext2fs_get_mem(isize, &buf);
+ if (err) {
+ com_err(argv[0], err, "while allocating memory");
+ return;
+ }
+
+ err = ext2fs_read_inode_full(current_fs, ino,
+ (struct ext2_inode *)buf, isize);
+ if (err) {
+ com_err(argv[0], err, "while reading inode %d", ino);
+ goto err;
+ }
+
+ do_byte_hexdump(stdout, buf, isize);
+err:
+ ext2fs_free_mem(&buf);
+}
+
+#ifndef READ_ONLY
void do_set_current_time(int argc, char *argv[])
{
time_t now;
current_fs->now = now;
}
}
+#endif /* READ_ONLY */
static int find_supp_feature(__u32 *supp, int feature_type, char *name)
{
}
}
+#ifndef READ_ONLY
void do_punch(int argc, char *argv[])
{
ext2_ino_t ino;
ino = string_to_inode(argv[1]);
if (!ino)
return;
- start = parse_ulong(argv[2], argv[0], "logical_block", &err);
- if (argc == 4)
- end = parse_ulong(argv[3], argv[0], "logical_block", &err);
- else
+ err = strtoblk(argv[0], argv[2], "logical block", &start);
+ if (err)
+ return;
+ if (argc == 4) {
+ err = strtoblk(argv[0], argv[3], "logical block", &end);
+ if (err)
+ return;
+ } else
end = ~0;
errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
return;
}
}
+#endif /* READ_ONLY */
+
+void do_symlink(int argc, char *argv[])
+{
+ char *cp;
+ ext2_ino_t parent;
+ char *name, *target;
+ errcode_t retval;
+
+ if (common_args_process(argc, argv, 3, 3, "symlink",
+ "<filename> <target>", CHECK_FS_RW))
+ return;
+
+ cp = strrchr(argv[1], '/');
+ if (cp) {
+ *cp = 0;
+ parent = string_to_inode(argv[1]);
+ if (!parent) {
+ com_err(argv[1], ENOENT, 0);
+ return;
+ }
+ name = cp+1;
+ } else {
+ parent = cwd;
+ name = argv[1];
+ }
+ target = argv[2];
+
+try_again:
+ retval = ext2fs_symlink(current_fs, parent, 0, name, target);
+ if (retval == EXT2_ET_DIR_NO_SPACE) {
+ retval = ext2fs_expand_dir(current_fs, parent);
+ if (retval) {
+ com_err(argv[0], retval, "while expanding directory");
+ return;
+ }
+ goto try_again;
+ }
+ if (retval) {
+ com_err("ext2fs_symlink", retval, 0);
+ return;
+ }
+
+}
-void do_dump_mmp(int argc, char *argv[])
+void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
{
- struct ext2_super_block *sb = current_fs->super;
struct mmp_struct *mmp_s;
time_t t;
errcode_t retval = 0;
- if (sb->s_mmp_block <= sb->s_first_data_block ||
- sb->s_mmp_block >= ext2fs_blocks_count(sb)) {
- com_err(argv[0], EXT2_ET_MMP_BAD_BLOCK, "while dumping it.\n");
+ if (check_fs_open(argv[0]))
return;
- }
if (current_fs->mmp_buf == NULL) {
retval = ext2fs_get_mem(current_fs->blocksize,
fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
+ fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
}
-static int source_file(const char *cmd_file, int sci_idx)
+static int source_file(const char *cmd_file, int ss_idx)
{
FILE *f;
- char buf[256];
+ char buf[BUFSIZ];
char *cp;
int exit_status = 0;
int retval;
if (cp)
*cp = 0;
printf("debugfs: %s\n", buf);
- retval = ss_execute_line(sci_idx, buf);
+ retval = ss_execute_line(ss_idx, buf);
if (retval) {
- ss_perror(sci_idx, retval, buf);
+ ss_perror(ss_idx, retval, buf);
exit_status++;
}
}
int main(int argc, char **argv)
{
int retval;
- int sci_idx;
- const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
+ const char *usage =
+ "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
+ "[-R request] [-V] ["
+#ifndef READ_ONLY
+ "[-w] "
+#endif
+ "[-c] device]";
int c;
int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
char *request = 0;
blk64_t blocksize = 0;
int catastrophic = 0;
char *data_filename = 0;
+#ifdef READ_ONLY
+ const char *opt_string = "icR:f:b:s:Vd:D";
+#else
+ const char *opt_string = "iwcR:f:b:s:Vd:D";
+#endif
if (debug_prog_name == 0)
+#ifdef READ_ONLY
+ debug_prog_name = "rdebugfs";
+#else
debug_prog_name = "debugfs";
-
+#endif
add_error_table(&et_ext2_error_table);
fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
E2FSPROGS_VERSION, E2FSPROGS_DATE);
- while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:D")) != EOF) {
+ while ((c = getopt (argc, argv, opt_string)) != EOF) {
switch (c) {
case 'R':
request = optarg;
case 'i':
open_flags |= EXT2_FLAG_IMAGE_FILE;
break;
+#ifndef READ_ONLY
case 'w':
open_flags |= EXT2_FLAG_RW;
break;
+#endif
case 'D':
open_flags |= EXT2_FLAG_DIRECT_IO;
break;
"block size", 0);
break;
case 's':
- superblock = parse_ulong(optarg, argv[0],
- "superblock number", 0);
+ retval = strtoblk(argv[0], optarg,
+ "superblock block number",
+ &superblock);
+ if (retval)
+ return 1;
break;
case 'c':
catastrophic = 1;