From 464df1d5e5ab1322e2dd0a7796939fff1aeefa9a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:49:25 +0200 Subject: Adding upstream version 1.47.0. Signed-off-by: Daniel Baumann --- debugfs/Android.bp | 80 ++ debugfs/Makefile.in | 426 ++++++++ debugfs/debug_cmds.ct | 233 ++++ debugfs/debugfs.8.in | 880 +++++++++++++++ debugfs/debugfs.c | 2682 ++++++++++++++++++++++++++++++++++++++++++++++ debugfs/debugfs.h | 210 ++++ debugfs/do_journal.c | 972 +++++++++++++++++ debugfs/dump.c | 385 +++++++ debugfs/extent_cmds.ct | 77 ++ debugfs/extent_inode.c | 568 ++++++++++ debugfs/filefrag.c | 330 ++++++ debugfs/htree.c | 488 +++++++++ debugfs/icheck.c | 176 +++ debugfs/journal.c | 947 ++++++++++++++++ debugfs/journal.h | 25 + debugfs/logdump.c | 933 ++++++++++++++++ debugfs/ls.c | 252 +++++ debugfs/lsdel.c | 219 ++++ debugfs/ncheck.c | 223 ++++ debugfs/quota.c | 172 +++ debugfs/ro_debug_cmds.ct | 99 ++ debugfs/set_fields.c | 1024 ++++++++++++++++++ debugfs/unused.c | 60 ++ debugfs/util.c | 600 +++++++++++ debugfs/xattrs.c | 503 +++++++++ debugfs/zap.c | 246 +++++ 26 files changed, 12810 insertions(+) create mode 100644 debugfs/Android.bp create mode 100644 debugfs/Makefile.in create mode 100644 debugfs/debug_cmds.ct create mode 100644 debugfs/debugfs.8.in create mode 100644 debugfs/debugfs.c create mode 100644 debugfs/debugfs.h create mode 100644 debugfs/do_journal.c create mode 100644 debugfs/dump.c create mode 100644 debugfs/extent_cmds.ct create mode 100644 debugfs/extent_inode.c create mode 100644 debugfs/filefrag.c create mode 100644 debugfs/htree.c create mode 100644 debugfs/icheck.c create mode 100644 debugfs/journal.c create mode 100644 debugfs/journal.h create mode 100644 debugfs/logdump.c create mode 100644 debugfs/ls.c create mode 100644 debugfs/lsdel.c create mode 100644 debugfs/ncheck.c create mode 100644 debugfs/quota.c create mode 100644 debugfs/ro_debug_cmds.ct create mode 100644 debugfs/set_fields.c create mode 100644 debugfs/unused.c create mode 100644 debugfs/util.c create mode 100644 debugfs/xattrs.c create mode 100644 debugfs/zap.c (limited to 'debugfs') diff --git a/debugfs/Android.bp b/debugfs/Android.bp new file mode 100644 index 0000000..4d087b3 --- /dev/null +++ b/debugfs/Android.bp @@ -0,0 +1,80 @@ +// Copyright 2017 The Android Open Source Project + +//######################## +// Build the debugfs binary + +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "external_e2fsprogs_license" + // to get the below license kinds: + // SPDX-license-identifier-GPL + // SPDX-license-identifier-GPL-2.0 + default_applicable_licenses: ["external_e2fsprogs_license"], +} + +cc_defaults { + name: "debugfs-defaults", + defaults: ["e2fsprogs-defaults"], + srcs: [ + "debug_cmds.c", + "debugfs.c", + "util.c", + "ncheck.c", + "icheck.c", + "ls.c", + "lsdel.c", + "dump.c", + "set_fields.c", + "logdump.c", + "htree.c", + "unused.c", + "e2freefrag.c", + "filefrag.c", + "extent_cmds.c", + "extent_inode.c", + "zap.c", + "quota.c", + "xattrs.c", + "journal.c", + "revoke.c", + "recovery.c", + "do_journal.c", + ], + cflags: [ + "-DDEBUGFS", + ], + include_dirs: [ + "external/e2fsprogs/misc", + "external/e2fsprogs/e2fsck", + ], +} + +debugfs_libs = [ + "libext2_misc", + "libext2fs", + "libext2_blkid", + "libext2_uuid", + "libext2_ss", + "libext2_quota", + "libext2_com_err", + "libext2_e2p", + "libext2_support", +] + +cc_binary { + name: "debugfs", + host_supported: true, + defaults: ["debugfs-defaults"], + + shared_libs: debugfs_libs, +} + +cc_binary { + name: "debugfs_static", + static_executable: true, + host_supported: true, + defaults: ["debugfs-defaults"], + + static_libs: debugfs_libs, +} diff --git a/debugfs/Makefile.in b/debugfs/Makefile.in new file mode 100644 index 0000000..67f8d0b --- /dev/null +++ b/debugfs/Makefile.in @@ -0,0 +1,426 @@ +# +# Standard e2fsprogs prologue.... +# + +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ +VPATH = @srcdir@ +top_builddir = .. +my_dir = debugfs +INSTALL = @INSTALL@ +MKDIR_P = @MKDIR_P@ + +@MCONFIG@ + +PROGS= debugfs +MANPAGES= debugfs.8 + +MK_CMDS= _SS_DIR_OVERRIDE=$(srcdir)/../lib/ss ../lib/ss/mk_cmds + +DEBUG_OBJS= debug_cmds.o debugfs.o util.o ncheck.o icheck.o ls.o \ + lsdel.o dump.o set_fields.o logdump.o htree.o unused.o e2freefrag.o \ + filefrag.o extent_cmds.o extent_inode.o zap.o create_inode.o \ + quota.o xattrs.o journal.o revoke.o recovery.o do_journal.o + +RO_DEBUG_OBJS= ro_debug_cmds.o ro_debugfs.o util.o ncheck.o icheck.o ls.o \ + lsdel.o logdump.o htree.o e2freefrag.o filefrag.o extent_cmds.o \ + extent_inode.o quota.o xattrs.o + +SRCS= debug_cmds.c $(srcdir)/debugfs.c $(srcdir)/util.c $(srcdir)/ls.c \ + $(srcdir)/ncheck.c $(srcdir)/icheck.c $(srcdir)/lsdel.c \ + $(srcdir)/dump.c $(srcdir)/set_fields.c ${srcdir}/logdump.c \ + $(srcdir)/htree.c $(srcdir)/unused.c ${srcdir}/../misc/e2freefrag.c \ + $(srcdir)/filefrag.c $(srcdir)/extent_inode.c $(srcdir)/zap.c \ + $(srcdir)/../misc/create_inode.c $(srcdir)/xattrs.c $(srcdir)/quota.c \ + $(srcdir)/journal.c $(srcdir)/../e2fsck/revoke.c \ + $(srcdir)/../e2fsck/recovery.c $(srcdir)/do_journal.c + +LIBS= $(LIBSUPPORT) $(LIBEXT2FS) $(LIBE2P) $(LIBSS) $(LIBCOM_ERR) $(LIBBLKID) \ + $(LIBUUID) $(LIBMAGIC) $(SYSLIBS) +DEPLIBS= $(DEPLIBSUPPORT) $(LIBEXT2FS) $(LIBE2P) $(DEPLIBSS) $(DEPLIBCOM_ERR) \ + $(DEPLIBBLKID) $(DEPLIBUUID) + +STATIC_LIBS= $(STATIC_LIBSUPPORT) $(STATIC_LIBEXT2FS) $(STATIC_LIBSS) \ + $(STATIC_LIBCOM_ERR) $(STATIC_LIBBLKID) $(STATIC_LIBUUID) \ + $(STATIC_LIBE2P) $(LIBMAGIC) $(SYSLIBS) +STATIC_DEPLIBS= $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBSS) \ + $(DEPSTATIC_LIBCOM_ERR) $(DEPSTATIC_LIBUUID) \ + $(DEPSTATIC_LIBE2P) + +# This nastiness is needed because of jfs_user.h hackery; when we finally +# clean up this mess, we should be able to drop it +LOCAL_CFLAGS = -I$(srcdir)/../e2fsck -DDEBUGFS +DEPEND_CFLAGS = -I$(srcdir) + +.c.o: + $(E) " CC $<" + $(Q) $(CC) -c $(ALL_CFLAGS) $< -o $@ + $(Q) $(CHECK_CMD) $(ALL_CFLAGS) $< + $(Q) $(CPPCHECK_CMD) $(CPPFLAGS) $< + +all:: $(PROGS) $(MANPAGES) + +debugfs: $(DEBUG_OBJS) $(DEPLIBS) + $(E) " LD $@" + $(Q) $(CC) $(ALL_LDFLAGS) -o debugfs $(DEBUG_OBJS) $(LIBS) + +debugfs.static: $(DEBUG_OBJS) $(STATIC_DEPLIBS) + $(E) " LD $@" + $(Q) $(CC) $(LDFLAGS_STATIC) -o debugfs.static $(DEBUG_OBJS) \ + $(STATIC_LIBS) $(READLINE_LIB) + +debugfs.static-libs: $(DEBUG_OBJS) $(STATIC_DEPLIBS) + $(E) " LD $@" + $(Q) $(CC) -o debugfs.static-libs $(DEBUG_OBJS) \ + $(STATIC_LIBS) $(READLINE_LIB) + +rdebugfs: $(RO_DEBUG_OBJS) $(DEPLIBS) + $(E) " LD $@" + $(Q) $(CC) $(ALL_LDFLAGS) -o rdebugfs $(RO_DEBUG_OBJS) $(LIBS) + +debug_cmds.c debug_cmds.h: debug_cmds.ct + $(E) " MK_CMDS $@" + $(Q) $(MK_CMDS) $(srcdir)/debug_cmds.ct + +extent_cmds.c extent_cmds.h: extent_cmds.ct + $(E) " MK_CMDS $@" + $(Q) $(MK_CMDS) $(srcdir)/extent_cmds.ct + +ro_debug_cmds.c ro_debug_cmds.h: ro_debug_cmds.ct + $(E) " MK_CMDS $@" + $(Q) $(MK_CMDS) $(srcdir)/ro_debug_cmds.ct + +ro_debugfs.o: debugfs.c + $(E) " CC $@" + $(Q) $(CC) -c $(ALL_CFLAGS) $< -DREAD_ONLY -o $@ + +e2freefrag.o: $(srcdir)/../misc/e2freefrag.c + $(E) " CC $@" + $(Q) $(CC) -c $(ALL_CFLAGS) -I$(srcdir) $< -o $@ + +recovery.o: $(srcdir)/../e2fsck/recovery.c + $(E) " CC $@" + $(Q) $(CC) -c $(ALL_CFLAGS) -I$(srcdir) \ + $(srcdir)/../e2fsck/recovery.c -o $@ + +revoke.o: $(srcdir)/../e2fsck/revoke.c + $(E) " CC $@" + $(Q) $(CC) -c $(ALL_CFLAGS) -I$(srcdir) \ + $(srcdir)/../e2fsck/revoke.c -o $@ + +create_inode.o: $(srcdir)/../misc/create_inode.c + $(E) " CC $@" + $(Q) $(CC) -c $(ALL_CFLAGS) -I$(srcdir) \ + $(srcdir)/../misc/create_inode.c -o $@ + +debugfs.8: $(DEP_SUBSTITUTE) $(srcdir)/debugfs.8.in + $(E) " SUBST $@" + $(Q) $(SUBSTITUTE_UPTIME) $(srcdir)/debugfs.8.in debugfs.8 + +installdirs: + $(E) " MKDIR_P $(root_sbindir) $(man8dir)" + $(Q) $(MKDIR_P) $(DESTDIR)$(root_sbindir) \ + $(DESTDIR)$(man8dir) + +install: $(PROGS) $(MANPAGES) installdirs + $(Q) for i in $(PROGS); do \ + echo " INSTALL $(root_sbindir)/$$i"; \ + $(INSTALL_PROGRAM) $$i $(DESTDIR)$(root_sbindir)/$$i; \ + done + $(Q) for i in $(MANPAGES); do \ + for j in $(COMPRESS_EXT); do \ + $(RM) -f $(DESTDIR)$(man8dir)/$$i.$$j; \ + done; \ + echo " INSTALL_DATA $(man8dir)/$$i"; \ + $(INSTALL_DATA) $$i $(DESTDIR)$(man8dir)/$$i; \ + done + +install-strip: install + $(Q) for i in $(PROGS); do \ + echo " STRIP $(root_sbindir)/$$i"; \ + $(STRIP) $(DESTDIR)$(root_sbindir)/$$i; \ + done + +uninstall: + for i in $(PROGS); do \ + $(RM) -f $(DESTDIR)$(root_sbindir)/$$i; \ + done + for i in $(MANPAGES); do \ + $(RM) -f $(DESTDIR)$(man8dir)/$$i; \ + done + +clean:: + $(RM) -f $(PROGS) debugfs.8 \#* *.s *.o *.a *~ debug_cmds.c \ + extent_cmds.c ro_debug_cmds.c core rdebugfs debugfs.static \ + debugfs.static-libs tst_set_fields + +mostlyclean: clean +distclean: clean + $(RM) -f debug_cmds.c .depend Makefile $(srcdir)/TAGS \ + $(srcdir)/Makefile.in.old $(srcdir)/recovery.c \ + $(srcdir)/revoke.c + +tst_set_fields: set_fields.c util.c + $(E) " LD $@" + $(Q) $(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) $(SYSLIBS) -DUNITTEST \ + -o tst_set_fields $(srcdir)/set_fields.c $(srcdir)/util.c $(LIBS) + +fullcheck check:: tst_set_fields + $(TESTENV) ./tst_set_fields + +# +++ Dependency line eater +++ +# +# Makefile dependencies follow. This must be the last section in +# the Makefile.in file +# +debug_cmds.o: debug_cmds.c $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h +debugfs.o: $(srcdir)/debugfs.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h $(top_srcdir)/version.h \ + $(srcdir)/../e2fsck/jfs_user.h $(top_srcdir)/lib/ext2fs/kernel-jbd.h \ + $(top_srcdir)/lib/ext2fs/jfs_compat.h $(top_srcdir)/lib/ext2fs/kernel-list.h \ + $(top_srcdir)/lib/ext2fs/compiler.h $(top_srcdir)/lib/support/plausible.h +util.o: $(srcdir)/util.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(srcdir)/debugfs.h $(top_srcdir)/lib/ext2fs/ext2_fs.h \ + $(top_builddir)/lib/ext2fs/ext2_types.h $(top_srcdir)/lib/ext2fs/ext2fs.h \ + $(top_srcdir)/lib/ext2fs/ext3_extents.h $(top_srcdir)/lib/ext2fs/ext2_io.h \ + $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +ls.o: $(srcdir)/ls.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +ncheck.o: $(srcdir)/ncheck.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +icheck.o: $(srcdir)/icheck.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +lsdel.o: $(srcdir)/lsdel.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +dump.o: $(srcdir)/dump.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +set_fields.o: $(srcdir)/set_fields.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +logdump.o: $(srcdir)/logdump.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h $(srcdir)/../e2fsck/jfs_user.h \ + $(top_srcdir)/lib/ext2fs/kernel-jbd.h $(top_srcdir)/lib/ext2fs/jfs_compat.h \ + $(top_srcdir)/lib/ext2fs/kernel-list.h $(top_srcdir)/lib/ext2fs/compiler.h \ + $(top_srcdir)/lib/ext2fs/fast_commit.h +htree.o: $(srcdir)/htree.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +unused.o: $(srcdir)/unused.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +e2freefrag.o: $(srcdir)/../misc/e2freefrag.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(top_srcdir)/lib/ext2fs/ext2_fs.h \ + $(top_builddir)/lib/ext2fs/ext2_types.h $(top_srcdir)/lib/ext2fs/ext2fs.h \ + $(top_srcdir)/lib/ext2fs/ext3_extents.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/e2freefrag.h \ + $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +filefrag.o: $(srcdir)/filefrag.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +extent_inode.o: $(srcdir)/extent_inode.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +zap.o: $(srcdir)/zap.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +create_inode.o: $(srcdir)/../misc/create_inode.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(top_srcdir)/lib/ext2fs/ext2fs.h \ + $(top_builddir)/lib/ext2fs/ext2_types.h $(top_srcdir)/lib/ext2fs/ext2_fs.h \ + $(top_srcdir)/lib/ext2fs/ext3_extents.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(top_srcdir)/lib/ext2fs/fiemap.h \ + $(srcdir)/../misc/create_inode.h $(top_srcdir)/lib/e2p/e2p.h \ + $(top_srcdir)/lib/support/nls-enable.h +xattrs.o: $(srcdir)/xattrs.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(top_srcdir)/lib/support/cstring.h \ + $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +quota.o: $(srcdir)/quota.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h +journal.o: $(srcdir)/journal.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/journal.h \ + $(srcdir)/../e2fsck/jfs_user.h $(top_srcdir)/lib/ext2fs/ext2_fs.h \ + $(top_builddir)/lib/ext2fs/ext2_types.h $(top_srcdir)/lib/ext2fs/ext2fs.h \ + $(top_srcdir)/lib/ext2fs/ext3_extents.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(top_srcdir)/lib/ext2fs/kernel-jbd.h \ + $(top_srcdir)/lib/ext2fs/jfs_compat.h $(top_srcdir)/lib/ext2fs/kernel-list.h \ + $(top_srcdir)/lib/ext2fs/compiler.h +revoke.o: $(srcdir)/../e2fsck/revoke.c $(srcdir)/../e2fsck/jfs_user.h \ + $(top_builddir)/lib/config.h $(top_builddir)/lib/dirpaths.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/et/com_err.h $(top_srcdir)/lib/ext2fs/ext2_io.h \ + $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(top_srcdir)/lib/ext2fs/kernel-jbd.h \ + $(top_srcdir)/lib/ext2fs/jfs_compat.h $(top_srcdir)/lib/ext2fs/kernel-list.h \ + $(top_srcdir)/lib/ext2fs/compiler.h +recovery.o: $(srcdir)/../e2fsck/recovery.c $(srcdir)/../e2fsck/jfs_user.h \ + $(top_builddir)/lib/config.h $(top_builddir)/lib/dirpaths.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/et/com_err.h $(top_srcdir)/lib/ext2fs/ext2_io.h \ + $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(top_srcdir)/lib/ext2fs/kernel-jbd.h \ + $(top_srcdir)/lib/ext2fs/jfs_compat.h $(top_srcdir)/lib/ext2fs/kernel-list.h \ + $(top_srcdir)/lib/ext2fs/compiler.h +do_journal.o: $(srcdir)/do_journal.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/debugfs.h $(top_srcdir)/lib/ss/ss.h \ + $(top_builddir)/lib/ss/ss_err.h $(top_srcdir)/lib/et/com_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/ext2fs/ext2_io.h $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(srcdir)/../misc/create_inode.h \ + $(top_srcdir)/lib/e2p/e2p.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h \ + $(top_srcdir)/lib/ext2fs/kernel-jbd.h $(top_srcdir)/lib/ext2fs/jfs_compat.h \ + $(top_srcdir)/lib/ext2fs/kernel-list.h $(top_srcdir)/lib/ext2fs/compiler.h \ + $(srcdir)/journal.h $(srcdir)/../e2fsck/jfs_user.h diff --git a/debugfs/debug_cmds.ct b/debugfs/debug_cmds.ct new file mode 100644 index 0000000..1ff6c9d --- /dev/null +++ b/debugfs/debug_cmds.ct @@ -0,0 +1,233 @@ +# +# Copyright (C) 1993 Theodore Ts'o. This file may be redistributed +# under the terms of the GNU Public License. +# +command_table debug_cmds; + +request do_show_debugfs_params, "Show debugfs parameters", + show_debugfs_params, params; + +request do_open_filesys, "Open a filesystem", + open_filesys, open; + +request do_close_filesys, "Close the filesystem", + close_filesys, close; + +request do_freefrag, "Report free space fragmentation", + freefrag, e2freefrag; + +request do_features, "Set/print superblock features", + feature, features; + +request do_dirty_filesys, "Mark the filesystem as dirty", + dirty_filesys, dirty; + +request do_init_filesys, "Initialize a filesystem (DESTROYS DATA)", + init_filesys; + +request do_show_super_stats, "Show superblock statistics", + show_super_stats, stats; + +request do_ncheck, "Do inode->name translation", + ncheck; + +request do_icheck, "Do block->inode translation", + icheck; + +request do_chroot, "Change root directory", + change_root_directory, chroot; + +request do_change_working_dir, "Change working directory", + change_working_directory, cd; + +request do_list_dir, "List directory", + list_directory, ls; + +request do_stat, "Show inode information ", + show_inode_info, stat; + +request do_dump_extents, "Dump extents information ", + dump_extents, extents, ex; + +request do_blocks, "Dump blocks used by an inode ", + blocks; + +request do_filefrag, "Report fragmentation information for an inode", + filefrag; + +request do_link, "Create directory link", + link, ln; + +request do_unlink, "Delete a directory link", + unlink; + +request do_mkdir, "Create a directory", + mkdir; + +request do_rmdir, "Remove a directory", + rmdir; + +request do_rm, "Remove a file (unlink and kill_file, if appropriate)", + rm; + +request do_kill_file, "Deallocate an inode and its blocks", + kill_file; + +request do_copy_inode, "Copy the inode structure", + copy_inode; + +request do_clri, "Clear an inode's contents", + clri; + +request do_freei, "Clear an inode's in-use flag", + freei; + +request do_seti, "Set an inode's in-use flag", + seti; + +request do_testi, "Test an inode's in-use flag", + testi; + +request do_freeb, "Clear a block's in-use flag", + freeb; + +request do_setb, "Set a block's in-use flag", + setb; + +request do_testb, "Test a block's in-use flag", + testb; + +request do_modify_inode, "Modify an inode by structure", + modify_inode, mi; + +request do_find_free_block, "Find free block(s)", + find_free_block, ffb; + +request do_find_free_inode, "Find free inode(s)", + find_free_inode, ffi; + +request do_print_working_directory, "Print current working directory", + print_working_directory, pwd; + +request do_expand_dir, "Expand directory", + expand_dir, expand; + +request do_mknod, "Create a special file", + mknod; + +request do_lsdel, "List deleted inodes", + list_deleted_inodes, lsdel; + +request do_undel, "Undelete file", + undelete, undel; + +request do_write, "Copy a file from your native filesystem", + write; + +request do_dump, "Dump an inode out to a file", + dump_inode, dump; + +request do_cat, "Dump an inode out to stdout", + cat; + +request do_lcd, "Change the current directory on your native filesystem", + lcd; + +request do_rdump, "Recursively dump a directory to the native filesystem", + rdump; + +request do_set_super, "Set superblock value", + set_super_value, ssv; + +request do_set_inode, "Set inode field", + set_inode_field, sif; + +request do_set_block_group_descriptor, "Set block group descriptor field", + set_block_group, set_bg; + +request do_logdump, "Dump the contents of the journal", + logdump; + +request do_htree_dump, "Dump a hash-indexed directory", + htree_dump, htree; + +request do_dx_hash, "Calculate the directory hash of a filename", + dx_hash, hash; + +request do_dirsearch, "Search a directory for a particular filename", + dirsearch; + +request do_bmap, "Calculate the logical->physical block mapping for an inode", + bmap; + +request do_fallocate, "Allocate uninitialized blocks to an inode", + fallocate; + +request do_punch, "Punch (or truncate) blocks from an inode by deallocating them", + punch, truncate; + +request do_symlink, "Create a symbolic link", + symlink; + +request do_imap, "Calculate the location of an inode", + imap; + +request do_dump_unused, "Dump unused blocks", + dump_unused; + +request do_set_current_time, "Set current time to use when setting filesystem fields", + set_current_time; + +request do_supported_features, "Print features supported by this version of e2fsprogs", + supported_features; + +request do_dump_mmp, "Dump MMP information", + dump_mmp; + +request do_set_mmp_value, "Set MMP value", + set_mmp_value, smmp; + +request do_extent_open, "Open inode for extent manipulation", + extent_open, eo; + +request do_zap_block, "Zap block: fill with 0, pattern, flip bits etc.", + zap_block, zap; + +request do_block_dump, "Dump contents of a block", + block_dump, bdump, bd; + +request do_list_xattr, "List extended attributes of an inode", + ea_list; + +request do_get_xattr, "Get an extended attribute of an inode", + ea_get; + +request do_set_xattr, "Set an extended attribute of an inode", + ea_set; + +request do_rm_xattr, "Remove an extended attribute of an inode", + ea_rm; + +request do_list_quota, "List quota", + list_quota, lq; + +request do_get_quota, "Get quota", + get_quota, gq; + +request do_idump, "Dump the inode structure in hex", + inode_dump, idump, id; + +request do_journal_open, "Open the journal", + journal_open, jo; + +request do_journal_close, "Close the journal", + journal_close, jc; + +request do_journal_write, "Write a transaction to the journal", + journal_write, jw; + +request do_journal_run, "Recover the journal", + journal_run, jr; + +end; + diff --git a/debugfs/debugfs.8.in b/debugfs/debugfs.8.in new file mode 100644 index 0000000..5b5329c --- /dev/null +++ b/debugfs/debugfs.8.in @@ -0,0 +1,880 @@ +.\" -*- nroff -*- +.\" Copyright 1993, 1994, 1995 by Theodore Ts'o. All Rights Reserved. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH DEBUGFS 8 "@E2FSPROGS_MONTH@ @E2FSPROGS_YEAR@" "E2fsprogs version @E2FSPROGS_VERSION@" +.SH NAME +debugfs \- ext2/ext3/ext4 file system debugger +.SH SYNOPSIS +.B debugfs +[ +.B \-DVwcin +] +[ +.B \-b +blocksize +] +[ +.B \-s +superblock +] +[ +.B \-f +cmd_file +] +[ +.B \-R +request +] +[ +.B \-d +data_source_device +] +[ +.B \-z +.I undo_file +] +[ +device +] +.SH DESCRIPTION +The +.B debugfs +program is an interactive file system debugger. It can be used to +examine and change the state of an ext2, ext3, or ext4 file system. +.PP +.I device +is a block device (e.g., /dev/sdXX) or a file containing the file system. +.SH OPTIONS +.TP +.I \-w +Specifies that the file system should be opened in read-write mode. +Without this option, the file system is opened in read-only mode. +.TP +.I \-n +Disables metadata checksum verification. This should only be used if +you believe the metadata to be correct despite the complaints of +e2fsprogs. +.TP +.I \-c +Specifies that the file system should be opened in catastrophic mode, in +which the inode and group bitmaps are not read initially. This can be +useful for file systems with significant corruption, but because of this, +catastrophic mode forces the file system to be opened read-only. +.TP +.I \-i +Specifies that +.I device +represents an ext2 image file created by the +.B e2image +program. Since the ext2 image file only contains the superblock, block +group descriptor, block and inode allocation bitmaps, and +the inode table, many +.B debugfs +commands will not function properly. +.B Warning: +no safety checks are in place, and +.B debugfs +may fail in interesting ways if commands such as +.IR ls ", " dump ", " +etc. are tried without specifying the +.I data_source_device +using the +.I \-d +option. +.B debugfs +is a debugging tool. It has rough edges! +.TP +.I -d data_source_device +Used with the +.I \-i +option, specifies that +.I data_source_device +should be used when reading blocks not found in the ext2 image file. +This includes data, directory, and indirect blocks. +.TP +.I -b blocksize +Forces the use of the given block size (in bytes) for the file system, +rather than detecting the correct block size automatically. (This +option is rarely needed; it is used primarily when the file system is +extremely badly damaged/corrupted.) +.TP +.I -s superblock +Causes the file system superblock to be read from the given block +number, instead of using the primary superblock (located at an offset of +1024 bytes from the beginning of the file system). If you specify the +.I -s +option, you must also provide the blocksize of the file system via the +.I -b +option. (This +option is rarely needed; it is used primarily when the file system is +extremely badly damaged/corrupted.) +.TP +.I -f cmd_file +Causes +.B debugfs +to read in commands from +.IR cmd_file , +and execute them. When +.B debugfs +is finished executing those commands, it will exit. +.TP +.I -D +Causes +.B debugfs +to open the device using Direct I/O, bypassing the buffer cache. Note +that some Linux devices, notably device mapper as of this writing, do +not support Direct I/O. +.TP +.I -R request +Causes +.B debugfs +to execute the single command +.IR request , +and then exit. +.TP +.I -V +print the version number of +.B debugfs +and exit. +.TP +.BI \-z " undo_file" +Before overwriting a file system block, write the old contents of the block to +an undo file. This undo file can be used with e2undo(8) to restore the old +contents of the file system should something go wrong. If the empty string is +passed as the undo_file argument, the undo file will be written to a file named +debugfs-\fIdevice\fR.e2undo in the directory specified via the +\fIE2FSPROGS_UNDO_DIR\fR environment variable. + +WARNING: The undo file cannot be used to recover from a power or system crash. +.SH SPECIFYING FILES +Many +.B debugfs +commands take a +.I filespec +as an argument to specify an inode (as opposed to a pathname) +in the file system which is currently opened by +.BR debugfs . +The +.I filespec +argument may be specified in two forms. The first form is an inode +number surrounded by angle brackets, e.g., +.IR <2> . +The second form is a pathname; if the pathname is prefixed by a forward slash +('/'), then it is interpreted relative to the root of the file system +which is currently opened by +.BR debugfs . +If not, the pathname is +interpreted relative to the current working directory as maintained by +.BR debugfs . +This may be modified by using the +.B debugfs +command +.IR cd . +.\" +.\" +.\" +.SH COMMANDS +This is a list of the commands which +.B debugfs +supports. +.TP +.BI blocks " filespec" +Print the blocks used by the inode +.I filespec +to stdout. +.TP +.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 " '[ -x ] [-f filespec] block_num" +Dump the file system block given by +.I block_num +in hex and ASCII format to the console. If the +.I \-f +option is specified, the block number is relative to the start of the given +.BR filespec . +If the +.I \-x +option is specified, the block is interpreted as an extended attribute +block and printed to show the structure of extended attribute data +structures. +.TP +.BI cat " filespec" +Dump the contents of the inode +.I filespec +to stdout. +.TP +.BI cd " filespec" +Change the current working directory to +.IR filespec . +.TP +.BI chroot " filespec" +Change the root directory to be the directory +.IR filespec . +.TP +.BI close " [-a]" +Close the currently open file system. If the +.I -a +option is specified, write out any changes to the superblock and block +group descriptors to all of the backup superblocks, not just to the +master superblock. +.TP +.BI clri " filespec" +Clear the contents of the inode +.IR filespec . +.TP +.BI copy_inode " source_inode destination_inode" +Copy the contents of the inode structure in +.I source_inode +and use it to overwrite the inode structure at +.IR destination_inode . +.TP +.BI dirsearch " filespec filename" +Search the directory +.I filespec +for +.IR filename . +.TP +.BI dirty " [-clean]" +Mark the file system as dirty, so that the superblocks will be written on exit. +Additionally, clear the superblock's valid flag, or set it if +.I -clean +is specified. +.TP +.BI dump " [-p] filespec out_file" +Dump the contents of the inode +.I filespec +to the output file +.IR out_file . +If the +.I -p +option is given set the owner, group and permissions information on +.I out_file +to match +.IR filespec . +.TP +.BI dump_mmp " [mmp_block]" +Display the multiple-mount protection (mmp) field values. If +.I mmp_block +is specified then verify and dump the MMP values from the given block +number, otherwise use the +.B s_mmp_block +field in the superblock to locate and use the existing MMP block. +.TP +.BI dx_hash " [-h hash_alg] [-s hash_seed] filename" +Calculate the directory hash of +.IR filename . +The hash algorithm specified with +.I -h +may be +.BR legacy , " half_md4" ", or " tea . +The hash seed specified with +.I -s +must be in UUID format. +.TP +.BI dump_extents " [-n] [-l] filespec" +Dump the extent tree of the inode +.IR filespec . +The +.I -n +flag will cause +.B dump_extents +to only display the interior nodes in the extent tree. The +.I -l +flag will cause +.B dump_extents +to only display the leaf nodes in the extent tree. +.IP +(Please note that the length and range of blocks for the last extent in +an interior node is an estimate by the extents library functions, and is +not stored in file system data structures. Hence, the values displayed +may not necessarily by accurate and does not indicate a problem or +corruption in the file system.) +.TP +.B dump_unused +Dump unused blocks which contain non-null bytes. +.TP +.BI ea_get " [-f outfile]|[-xVC] [-r] filespec attr_name" +Retrieve the value of the extended attribute +.I attr_name +in the file +.I filespec +and write it either to stdout or to \fIoutfile\fR. +.TP +.BI ea_list " filespec +List the extended attributes associated with the file +.I filespec +to standard output. +.TP +.BI ea_set " [-f infile] [-r] filespec attr_name attr_value +Set the value of the extended attribute +.I attr_name +in the file +.I filespec +to the string value +.I attr_value +or read it from \fIinfile\fR. +.TP +.BI ea_rm " filespec attr_names... +Remove the extended attribute +.I attr_name +from the file \fIfilespec\fR. +.TP +.BI expand_dir " filespec" +Expand the directory +.IR filespec . +.TP +.BI fallocate " filespec start_block [end_block] +Allocate and map uninitialized blocks into \fIfilespec\fR between +logical block \fIstart_block\fR and \fIend_block\fR, inclusive. If +\fIend_block\fR is not supplied, this function maps until it runs out +of free disk blocks or the maximum file size is reached. Existing +mappings are left alone. +.TP +.BI feature " [fs_feature] [-fs_feature] ..." +Set or clear various file system features in the superblock. After setting +or clearing any file system features that were requested, print the current +state of the file system feature set. +.TP +.BI filefrag " [-dvr] filespec" +Print the number of contiguous extents in +.IR filespec . +If +.I filespec +is a directory and the +.I -d +option is not specified, +.I filefrag +will print the number of contiguous extents for each file in +the directory. The +.I -v +option will cause +.I filefrag +print a tabular listing of the contiguous extents in the +file. The +.I -r +option will cause +.I filefrag +to do a recursive listing of the directory. +.TP +.BI find_free_block " [count [goal]]" +Find the first +.I count +free blocks, starting from +.I goal +and allocate it. Also available as +.BR ffb . +.TP +.BI find_free_inode " [dir [mode]]" +Find a free inode and allocate it. If present, +.I dir +specifies the inode number of the directory +which the inode is to be located. The second +optional argument +.I mode +specifies the permissions of the new inode. (If the directory bit is set +on the mode, the allocation routine will function differently.) Also +available as +.BR ffi . +.TP +.BI freeb " block [count]" +Mark the block number +.I block +as not allocated. +If the optional argument +.I count +is present, then +.I count +blocks starting at block number +.I block +will be marked as not allocated. +.TP +.BI freefrag " [-c chunk_kb]" +Report free space fragmentation on the currently open file system. +If the +.I \-c +option is specified then the filefrag command will print how many free +chunks of size +.I chunk_kb +can be found in the file system. The chunk size must be a power of two +and be larger than the file system block size. +.TP +.BI freei " filespec [num]" +Free the inode specified by +.IR filespec . +If +.I num +is specified, also clear num-1 inodes after the specified inode. +.TP +.BI get_quota " quota_type id" +Display quota information for given quota type (user, group, or project) and ID. +.TP +.B help +Print a list of commands understood by +.BR debugfs . +.TP +.BI htree_dump " filespec" +Dump the hash-indexed directory +.IR filespec , +showing its tree structure. +.TP +.BI icheck " block ..." +Print a listing of the inodes which use the one or more blocks specified +on the command line. +.TP +.BI inode_dump " [-b]|[-e]|[-x] filespec" +Print the contents of the inode data structure in hex and ASCII format. +The +.I \-b +option causes the command to only dump the contents of the +.B i_blocks +array. The +.I \-e +option causes the command to only dump the contents of the extra inode +space, which is used to store in-line extended attributes. The +.I \-x +option causes the command to dump the extra inode space interpreted and +extended attributes. This is useful to debug corrupted inodes +containing extended attributes. +.TP +.BI imap " filespec" +Print the location of the inode data structure (in the inode table) +of the inode +.IR filespec . +.TP +.BI init_filesys " device blocksize" +Create an ext2 file system on +.I device +with device size +.IR blocksize . +Note that this does not fully initialize all of the data structures; +to do this, use the +.BR mke2fs (8) +program. This is just a call to the low-level library, which sets up +the superblock and block descriptors. +.TP +.BI journal_close +Close the open journal. +.TP +.BI journal_open " [-c] [-v ver] [-f ext_jnl] +Opens the journal for reading and writing. Journal checksumming can +be enabled by supplying \fI-c\fR; checksum formats 2 and 3 can be +selected with the \fI-v\fR option. An external journal can be loaded +from \fIext_jnl\fR. +.TP +.BI journal_run +Replay all transactions in the open journal. +.TP +.BI journal_write " [-b blocks] [-r revoke] [-c] file +Write a transaction to the open journal. The list of blocks to write +should be supplied as a comma-separated list in \fIblocks\fR; the +blocks themselves should be readable from \fIfile\fR. A list of +blocks to revoke can be supplied as a comma-separated list in +\fIrevoke\fR. By default, a commit record is written at the end; the +\fI-c\fR switch writes an uncommitted transaction. +.TP +.BI kill_file " filespec" +Deallocate the inode +.I filespec +and its blocks. Note that this does not remove any directory +entries (if any) to this inode. See the +.BR rm (1) +command if you wish to unlink a file. +.TP +.BI lcd " directory" +Change the current working directory of the +.B debugfs +process to +.I directory +on the native file system. +.TP +.BI list_quota " quota_type" +Display quota information for given quota type (user, group, or project). +.TP +.BI ln " filespec dest_file" +Create a link named +.I dest_file +which is a hard link to +.IR filespec . +Note this does not adjust the inode reference counts. +.TP +.BI logdump " [-acsOS] [-b block] [-n num_trans ] [-i filespec] [-f journal_file] [output_file]" +Dump the contents of the ext3 journal. By default, dump the journal inode as +specified in the superblock. However, this can be overridden with the +.I \-i +option, which dumps the journal from the internal inode given by +.IR filespec . +A regular file containing journal data can be specified using the +.I \-f +option. Finally, the +.I \-s +option utilizes the backup information in the superblock to locate the +journal. +.IP +The +.I \-S +option causes +.B logdump +to print the contents of the journal superblock. +.IP +The +.I \-a +option causes the +.B logdump +to print the contents of all of the descriptor blocks. +The +.I \-b +option causes +.B logdump +to print all journal records that refer to the specified block. +The +.I \-c +option will print out the contents of all of the data blocks selected by +the +.I \-a +and +.I \-b +options. +.IP +The +.I \-O +option causes logdump to display old (checkpointed) journal entries. +This can be used to try to track down journal problems even after the +journal has been replayed. +.IP +The +.I \-n +option causes +.B logdump +to continue past a journal block which is missing a magic number. +Instead, it will stop only when the entire log is printed or after +.I num_trans +transactions. +.TP +.BI ls " [-l] [-c] [-d] [-p] [-r] filespec" +Print a listing of the files in the directory +.IR filespec . +The +.I \-c +flag causes directory block checksums (if present) to be displayed. +The +.I \-d +flag will list deleted entries in the directory. +The +.I \-l +flag will list files using a more verbose format. +The +.I \-p +flag will list the files in a format which is more easily parsable by +scripts, as well as making it more clear when there are spaces or other +non-printing characters at the end of filenames. +The +.I \-r +flag will force the printing of the filename, even if it is encrypted. +.TP +.BI list_deleted_inodes " [limit]" +List deleted inodes, optionally limited to those deleted within +.I limit +seconds ago. Also available as +.BR lsdel . +.IP +This command was useful for recovering from accidental file deletions +for ext2 file systems. Unfortunately, it is not useful for this purpose +if the files were deleted using ext3 or ext4, since the inode's +data blocks are no longer available after the inode is released. +.TP +.BI modify_inode " filespec" +Modify the contents of the inode structure in the inode +.IR filespec . +Also available as +.BR mi . +.TP +.BI mkdir " filespec" +Make a directory. +.TP +.BI mknod " filespec [p|[[c|b] major minor]]" +Create a special device file (a named pipe, character or block device). +If a character or block device is to be made, the +.I major +and +.I minor +device numbers must be specified. +.TP +.BI ncheck " [-c] inode_num ..." +Take the requested list of inode numbers, and print a listing of pathnames +to those inodes. The +.I -c +flag will enable checking the file type information in the directory +entry to make sure it matches the inode's type. +.TP +.BI open " [-weficD] [-b blocksize] [-d image_filename] [-s superblock] [-z undo_file] device" +Open a file system for editing. The +.I -f +flag forces the file system to be opened even if there are some unknown +or incompatible file system features which would normally +prevent the file system from being opened. The +.I -e +flag causes the file system to be opened in exclusive mode. The +.IR -b ", " -c ", " -d ", " -i ", " -s ", " -w ", and " -D +options behave the same as the command-line options to +.BR debugfs . +.TP +.BI punch " filespec start_blk [end_blk]" +Delete the blocks in the inode ranging from +.I start_blk +to +.IR end_blk . +If +.I end_blk +is omitted then this command will function as a truncate command; that +is, all of the blocks starting at +.I start_blk +through to the end of the file will be deallocated. +.TP +.BI symlink " filespec target" +Make a symbolic link. +.TP +.B pwd +Print the current working directory. +.TP +.B quit +Quit +.B debugfs +.TP +.BI rdump " directory[...] destination" +Recursively dump +.IR directory , +or multiple +.IR directories , +and all its contents (including regular files, symbolic links, and other +directories) into the named +.IR destination , +which should be an existing directory on the native file system. +.TP +.BI rm " pathname" +Unlink +.IR pathname . +If this causes the inode pointed to by +.I pathname +to have no other references, deallocate the file. This command functions +as the unlink() system call. +.I +.TP +.BI rmdir " filespec" +Remove the directory +.IR filespec . +.TP +.BI setb " block [count]" +Mark the block number +.I block +as allocated. +If the optional argument +.I count +is present, then +.I count +blocks starting at block number +.I block +will be marked as allocated. +.TP +.BI set_block_group " bgnum field value" +Modify the block group descriptor specified by +.I bgnum +so that the block group descriptor field +.I field +has value +.IR value . +Also available as +.BR set_bg . +.TP +.BI set_current_time " time" +Set current time in seconds since Unix epoch to use when setting file system +fields. +.TP +.BI seti " filespec [num]" +Mark inode +.I filespec +as in use in the inode bitmap. If +.I num +is specified, also set num-1 inodes after the specified inode. +.TP +.BI set_inode_field " filespec field value" +Modify the inode specified by +.I filespec +so that the inode field +.I field +has value +.I value. +The list of valid inode fields which can be set via this command +can be displayed by using the command: +.B set_inode_field -l +Also available as +.BR sif . +.TP +.BI set_mmp_value " field value" +Modify the multiple-mount protection (MMP) data so that the MMP field +.I field +has value +.I value. +The list of valid MMP fields which can be set via this command +can be displayed by using the command: +.B set_mmp_value -l +Also available as +.BR smmp . +.TP +.BI set_super_value " field value" +Set the superblock field +.I field +to +.I value. +The list of valid superblock fields which can be set via this command +can be displayed by using the command: +.B set_super_value -l +Also available as +.BR ssv . +.TP +.B show_debugfs_params +Display +.B debugfs +parameters such as information about currently opened file system. +.TP +.BI show_super_stats " [-h]" +List the contents of the super block and the block group descriptors. If the +.I -h +flag is given, only print out the superblock contents. Also available as +.BR stats . +.TP +.BI stat " filespec" +Display the contents of the inode structure of the inode +.IR filespec . +.TP +.B supported_features +Display file system features supported by this version of +.BR debugfs . +.TP +.BI testb " block [count]" +Test if the block number +.I block +is marked as allocated in the block bitmap. +If the optional argument +.I count +is present, then +.I count +blocks starting at block number +.I block +will be tested. +.TP +.BI testi " filespec" +Test if the inode +.I filespec +is marked as allocated in the inode bitmap. +.TP +.BI undel " [pathname]" +Undelete the specified inode number (which must be surrounded by angle +brackets) so that it and its blocks are marked in use, and optionally +link the recovered inode to the specified pathname. The +.B e2fsck +command should always be run after using the +.B undel +command to recover deleted files. +.IP +Note that if you are recovering a large number of deleted files, linking +the inode to a directory may require the directory to be expanded, which +could allocate a block that had been used by one of the +yet-to-be-undeleted files. So it is safer to undelete all of the +inodes without specifying a destination pathname, and then in a separate +pass, use the debugfs +.B link +command to link the inode to the destination pathname, or use +.B e2fsck +to check the file system and link all of the recovered inodes to the +lost+found directory. +.TP +.BI unlink " pathname" +Remove the link specified by +.I pathname +to an inode. Note this does not adjust the inode reference counts. +.TP +.BI write " source_file out_file" +Copy the contents of +.I source_file +into a newly-created file in the file system named +.IR out_file . +.TP +.BI zap_block " [-f filespec] [-o offset] [-l length] [-p pattern] block_num" +Overwrite the block specified by +.I block_num +with zero (NUL) bytes, or if +.I -p +is given use the byte specified by +.IR pattern . +If +.I -f +is given then +.I block_num +is relative to the start of the file given by +.IR filespec . +The +.I -o +and +.I -l +options limit the range of bytes to zap to the specified +.I offset +and +.I length +relative to the start of the block. +.TP +.BI zap_block " [-f filespec] [-b bit] block_num" +Bit-flip portions of the physical +.IR block_num . +If +.I -f +is given, then +.I block_num +is a logical block relative to the start of +.IR filespec . +.SH ENVIRONMENT VARIABLES +.TP +.B DEBUGFS_PAGER, PAGER +The +.B debugfs +program always pipes the output of the some commands through a +pager program. These commands include: +.IR show_super_stats " (" stats ), +.IR list_directory " (" ls ), +.IR show_inode_info " (" stat ), +.IR list_deleted_inodes " (" lsdel ), +and +.IR htree_dump . +The specific pager can explicitly specified by the +.B DEBUGFS_PAGER +environment variable, and if it is not set, by the +.B PAGER +environment variable. +.IP +Note that since a pager is always used, the +.BR less (1) +pager is not particularly appropriate, since it clears the screen before +displaying the output of the command and clears the output the screen +when the pager is exited. Many users prefer to use the +.BR less (1) +pager for most purposes, which is why the +.B DEBUGFS_PAGER +environment variable is available to override the more general +.B PAGER +environment variable. +.SH AUTHOR +.B debugfs +was written by Theodore Ts'o . +.SH SEE ALSO +.BR dumpe2fs (8), +.BR tune2fs (8), +.BR e2fsck (8), +.BR mke2fs (8), +.BR ext4 (5) diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c new file mode 100644 index 0000000..9b6321d --- /dev/null +++ b/debugfs/debugfs.c @@ -0,0 +1,2682 @@ +/* + * debugfs.c --- a program which allows you to attach an ext2fs + * filesystem and play with it. + * + * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed + * under the terms of the GNU Public License. + * + * Modifications by Robert Sanders + */ + +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_GETOPT_H +#include +#else +extern int optind; +extern char *optarg; +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#include +#ifdef HAVE_SYS_SYSMACROS_H +#include +#endif + +#include "debugfs.h" +#include "uuid/uuid.h" +#include "e2p/e2p.h" + +#include + +#include "../version.h" +#include "jfs_user.h" +#include "support/plausible.h" + +#ifndef BUFSIZ +#define BUFSIZ 8192 +#endif + +#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */ +int journal_enable_debug = -1; +#endif + +/* + * There must be only one definition if we're hooking in extra commands or + * changing default prompt. Use -DSKIP_GLOBDEF for that. + */ +#ifndef SKIP_GLOBDEFS +ss_request_table *extra_cmds; +const char *debug_prog_name; +#endif +int ss_sci_idx; + +ext2_filsys current_fs; +quota_ctx_t current_qctx; +ext2_ino_t root, cwd; +int no_copy_xattrs; + +static int debugfs_setup_tdb(const char *device_name, char *undo_file, + io_manager *io_ptr) +{ + errcode_t retval = ENOMEM; + const char *tdb_dir = NULL; + char *tdb_file = NULL; + char *dev_name, *tmp_name; + + /* (re)open a specific undo file */ + if (undo_file && undo_file[0] != 0) { + retval = set_undo_io_backing_manager(*io_ptr); + if (retval) + goto err; + *io_ptr = undo_io_manager; + retval = set_undo_io_backup_file(undo_file); + if (retval) + goto err; + printf("Overwriting existing filesystem; this can be undone " + "using the command:\n" + " e2undo %s %s\n\n", + undo_file, device_name); + return retval; + } + + /* + * Configuration via a conf file would be + * nice + */ + tdb_dir = ss_safe_getenv("E2FSPROGS_UNDO_DIR"); + if (!tdb_dir) + tdb_dir = "/var/lib/e2fsprogs"; + + if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) || + access(tdb_dir, W_OK)) + return 0; + + tmp_name = strdup(device_name); + if (!tmp_name) + goto errout; + dev_name = basename(tmp_name); + tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1); + if (!tdb_file) { + free(tmp_name); + goto errout; + } + sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name); + free(tmp_name); + + if ((unlink(tdb_file) < 0) && (errno != ENOENT)) { + retval = errno; + com_err("debugfs", retval, + "while trying to delete %s", tdb_file); + goto errout; + } + + retval = set_undo_io_backing_manager(*io_ptr); + if (retval) + goto errout; + *io_ptr = undo_io_manager; + retval = set_undo_io_backup_file(tdb_file); + if (retval) + goto errout; + printf("Overwriting existing filesystem; this can be undone " + "using the command:\n" + " e2undo %s %s\n\n", tdb_file, device_name); + + free(tdb_file); + return 0; +errout: + free(tdb_file); +err: + com_err("debugfs", retval, "while trying to setup undo file\n"); + return retval; +} + +static void open_filesystem(char *device, int open_flags, blk64_t superblock, + blk64_t blocksize, int catastrophic, + char *data_filename, char *undo_file) +{ + int retval; + io_channel data_io = 0; + io_manager io_ptr = unix_io_manager; + + if (superblock != 0 && blocksize == 0) { + com_err(device, 0, "if you specify the superblock, you must also specify the block size"); + current_fs = NULL; + return; + } + + if (data_filename) { + if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) { + com_err(device, 0, + "The -d option is only valid when reading an e2image file"); + current_fs = NULL; + return; + } + retval = unix_io_manager->open(data_filename, 0, &data_io); + if (retval) { + com_err(data_filename, 0, "while opening data source"); + current_fs = NULL; + return; + } + } + + if (catastrophic) + open_flags |= EXT2_FLAG_SKIP_MMP | EXT2_FLAG_IGNORE_SB_ERRORS; + + if (undo_file) { + retval = debugfs_setup_tdb(device, undo_file, &io_ptr); + if (retval) + exit(1); + } + +try_open_again: + retval = ext2fs_open(device, open_flags, superblock, blocksize, + io_ptr, ¤t_fs); + if (retval && (retval == EXT2_ET_SB_CSUM_INVALID) && + !(open_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) { + open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; + printf("Checksum errors in superblock! Retrying...\n"); + goto try_open_again; + } + if (retval) { + com_err(debug_prog_name, retval, + "while trying to open %s", device); + if (retval == EXT2_ET_BAD_MAGIC) + check_plausibility(device, CHECK_FS_EXIST, NULL); + current_fs = NULL; + return; + } + current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; + + if (!catastrophic) { + retval = ext2fs_read_bitmaps(current_fs); + if (retval) { + com_err(device, retval, + "while reading allocation bitmaps"); + goto errout; + } + } + + if (data_io) { + retval = ext2fs_set_data_io(current_fs, data_io); + if (retval) { + com_err(device, retval, + "while setting data source"); + goto errout; + } + } + + root = cwd = EXT2_ROOT_INO; + return; + +errout: + retval = ext2fs_close_free(¤t_fs); + if (retval) + com_err(device, retval, "while trying to close filesystem"); +} + +void do_open_filesys(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + int c, err; + int catastrophic = 0; + blk64_t superblock = 0; + blk64_t blocksize = 0; + int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS | + EXT2_FLAG_THREADS; + char *data_filename = 0; + char *undo_file = NULL; + + reset_getopt(); + while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) { + switch (c) { + case 'i': + 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; + break; + case 'e': + open_flags |= EXT2_FLAG_EXCLUSIVE; + break; + case 'c': + catastrophic = 1; + break; + case 'd': + data_filename = optarg; + break; + case 'D': + open_flags |= EXT2_FLAG_DIRECT_IO; + break; + case 'b': + blocksize = parse_ulong(optarg, argv[0], + "block size", &err); + if (err) + return; + break; + case 's': + err = strtoblk(argv[0], optarg, + "superblock block number", &superblock); + if (err) + return; + break; + case 'z': +#ifdef READ_ONLY + goto print_usage; +#else + undo_file = optarg; +#endif + break; + default: + goto print_usage; + } + } + if (optind != argc-1) { + goto print_usage; + } + if (check_fs_not_open(argv[0])) + return; + open_filesystem(argv[optind], open_flags, + superblock, blocksize, catastrophic, + data_filename, undo_file); + return; + +print_usage: + fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] " +#ifdef READ_ONLY + "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] " +#else + "[-d image_filename] [-c] [-i] [-f] [-e] [-D] [-w] " +#endif + "\n", argv[0]); +} + +void do_lcd(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + if (argc != 2) { + com_err(argv[0], 0, "Usage: %s %s", argv[0], ""); + return; + } + + if (chdir(argv[1]) == -1) { + com_err(argv[0], errno, + "while trying to change native directory to %s", + argv[1]); + return; + } +} + +static void close_filesystem(NOARGS) +{ + int retval; + + if (current_fs->flags & EXT2_FLAG_IB_DIRTY) { + retval = ext2fs_write_inode_bitmap(current_fs); + if (retval) + com_err("ext2fs_write_inode_bitmap", retval, 0); + } + if (current_fs->flags & EXT2_FLAG_BB_DIRTY) { + retval = ext2fs_write_block_bitmap(current_fs); + if (retval) + com_err("ext2fs_write_block_bitmap", retval, 0); + } + if (current_qctx) + quota_release_context(¤t_qctx); + retval = ext2fs_close_free(¤t_fs); + if (retval) + com_err("ext2fs_close", retval, 0); + return; +} + +void do_close_filesys(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + int c; + + if (check_fs_open(argv[0])) + return; + + reset_getopt(); + while ((c = getopt (argc, argv, "a")) != EOF) { + switch (c) { + case 'a': + current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; + break; + default: + goto print_usage; + } + } + + if (argc > optind) { + print_usage: + com_err(0, 0, "Usage: close_filesys [-a]"); + return; + } + + close_filesystem(); +} + +#ifndef READ_ONLY +void do_init_filesys(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + struct ext2_super_block param; + errcode_t retval; + int err; + blk64_t blocks; + + if (common_args_process(argc, argv, 3, 3, "initialize", + " ", CHECK_FS_NOTOPEN)) + return; + + memset(¶m, 0, sizeof(struct ext2_super_block)); + 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) { + com_err(argv[1], retval, "while initializing filesystem"); + current_fs = NULL; + return; + } + root = cwd = EXT2_ROOT_INO; + return; +} + +static void print_features(struct ext2_super_block * s, FILE *f) +{ + int i, j, printed=0; + __u32 *mask = &s->s_feature_compat, m; + + fputs("Filesystem features:", f); + for (i=0; i <3; i++,mask++) { + for (j=0,m=1; j < 32; j++, m<<=1) { + if (*mask & m) { + fprintf(f, " %s", e2p_feature2string(i, m)); + printed++; + } + } + } + if (printed == 0) + 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) +{ + if (ext2fs_bg_flags_test(fs, group, mask)) { + if (*first) { + fputs(" [", f); + *first = 0; + } else + fputs(", ", f); + fputs(str, f); + } +} + +void do_show_super_stats(int argc, char *argv[], + int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + const char *units ="block"; + dgrp_t i; + FILE *out; + int c, header_only = 0; + int numdirs = 0, first, gdt_csum; + + reset_getopt(); + while ((c = getopt (argc, argv, "h")) != EOF) { + switch (c) { + case 'h': + header_only++; + break; + default: + goto print_usage; + } + } + if (optind != argc) { + goto print_usage; + } + if (check_fs_open(argv[0])) + return; + out = open_pager(); + + if (ext2fs_has_feature_bigalloc(current_fs->super)) + units = "cluster"; + + list_super2(current_fs->super, out); + if (ext2fs_has_feature_metadata_csum(current_fs->super) && + !ext2fs_superblock_csum_verify(current_fs, + current_fs->super)) { + __u32 orig_csum = current_fs->super->s_checksum; + + ext2fs_superblock_csum_set(current_fs, + current_fs->super); + fprintf(out, "Expected Checksum: 0x%08x\n", + current_fs->super->s_checksum); + current_fs->super->s_checksum = orig_csum; + } + for (i=0; i < current_fs->group_desc_count; i++) + numdirs += ext2fs_bg_used_dirs_count(current_fs, i); + fprintf(out, "Directories: %u\n", numdirs); + + if (header_only) { + close_pager(out); + return; + } + + gdt_csum = ext2fs_has_group_desc_csum(current_fs); + for (i = 0; i < current_fs->group_desc_count; i++) { + fprintf(out, " Group %2d: block bitmap at %llu, " + "inode bitmap at %llu, " + "inode table at %llu\n" + " %u free %s%s, " + "%u free %s, " + "%u used %s%s", i, + (unsigned long long) ext2fs_block_bitmap_loc(current_fs, i), + (unsigned long long) ext2fs_inode_bitmap_loc(current_fs, i), + (unsigned long long) ext2fs_inode_table_loc(current_fs, i), + ext2fs_bg_free_blocks_count(current_fs, i), + units, + ext2fs_bg_free_blocks_count(current_fs, i) != 1 ? + "s" : "", + ext2fs_bg_free_inodes_count(current_fs, i), + ext2fs_bg_free_inodes_count(current_fs, i) != 1 ? + "inodes" : "inode", + ext2fs_bg_used_dirs_count(current_fs, i), + ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories" + : "directory", gdt_csum ? ", " : "\n"); + if (gdt_csum) + fprintf(out, "%u unused %s\n", + ext2fs_bg_itable_unused(current_fs, i), + ext2fs_bg_itable_unused(current_fs, i) != 1 ? + "inodes" : "inode"); + first = 1; + print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init", + &first, out); + print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init", + &first, out); + if (gdt_csum) { + fprintf(out, "%sChecksum 0x%04x", + first ? " [":", ", ext2fs_bg_checksum(current_fs, i)); + first = 0; + } + if (!first) + fputs("]\n", out); + } + close_pager(out); + return; +print_usage: + fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]); +} + +#ifndef READ_ONLY +void do_dirty_filesys(int argc EXT2FS_ATTR((unused)), + char **argv EXT2FS_ATTR((unused)), + int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + if (check_fs_open(argv[0])) + return; + if (check_fs_read_write(argv[0])) + return; + + if (argv[1] && !strcmp(argv[1], "-clean")) + current_fs->super->s_state |= EXT2_VALID_FS; + else + current_fs->super->s_state &= ~EXT2_VALID_FS; + ext2fs_mark_super_dirty(current_fs); +} +#endif /* READ_ONLY */ + +struct list_blocks_struct { + FILE *f; + e2_blkcnt_t total; + blk64_t first_block, last_block; + e2_blkcnt_t first_bcnt, last_bcnt; + e2_blkcnt_t first; +}; + +static void finish_range(struct list_blocks_struct *lb) +{ + if (lb->first_block == 0) + return; + if (lb->first) + lb->first = 0; + else + fprintf(lb->f, ", "); + if (lb->first_block == lb->last_block) + fprintf(lb->f, "(%lld):%llu", + (long long)lb->first_bcnt, + (unsigned long long) lb->first_block); + else + fprintf(lb->f, "(%lld-%lld):%llu-%llu", + (long long)lb->first_bcnt, (long long)lb->last_bcnt, + (unsigned long long) lb->first_block, + (unsigned long long) lb->last_block); + lb->first_block = 0; +} + +static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), + blk64_t *blocknr, e2_blkcnt_t blockcnt, + blk64_t ref_block EXT2FS_ATTR((unused)), + int ref_offset EXT2FS_ATTR((unused)), + void *private) +{ + struct list_blocks_struct *lb = (struct list_blocks_struct *) private; + + lb->total++; + if (blockcnt >= 0) { + /* + * See if we can add on to the existing range (if it exists) + */ + if (lb->first_block && + (lb->last_block+1 == *blocknr) && + (lb->last_bcnt+1 == blockcnt)) { + lb->last_block = *blocknr; + lb->last_bcnt = blockcnt; + return 0; + } + /* + * Start a new range. + */ + finish_range(lb); + lb->first_block = lb->last_block = *blocknr; + lb->first_bcnt = lb->last_bcnt = blockcnt; + return 0; + } + /* + * Not a normal block. Always force a new range. + */ + finish_range(lb); + if (lb->first) + lb->first = 0; + else + fprintf(lb->f, ", "); + if (blockcnt == -1) + fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr); + else if (blockcnt == -2) + fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr); + else if (blockcnt == -3) + fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr); + return 0; +} + +static void internal_dump_inode_extra(FILE *out, + const char *prefix EXT2FS_ATTR((unused)), + ext2_ino_t inode_num EXT2FS_ATTR((unused)), + struct ext2_inode_large *inode) +{ + fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize); + if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) - + EXT2_GOOD_OLD_INODE_SIZE) { + fprintf(stderr, "invalid inode->i_extra_isize (%u)\n", + inode->i_extra_isize); + return; + } +} + +static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode) +{ + struct list_blocks_struct lb; + + fprintf(f, "%sBLOCKS:\n%s", prefix, prefix); + lb.total = 0; + lb.first_block = 0; + lb.f = f; + lb.first = 1; + ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, + list_blocks_proc, (void *)&lb); + finish_range(&lb); + if (lb.total) + fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total); + fprintf(f,"\n"); +} + +static int int_log10(unsigned long long arg) +{ + int l = 0; + + arg = arg / 10; + while (arg) { + l++; + arg = arg / 10; + } + return l; +} + +#define DUMP_LEAF_EXTENTS 0x01 +#define DUMP_NODE_EXTENTS 0x02 +#define DUMP_EXTENT_TABLE 0x04 + +static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino, + int flags, int logical_width, int physical_width) +{ + ext2_extent_handle_t handle; + struct ext2fs_extent extent; + struct ext2_extent_info info; + int op = EXT2_EXTENT_ROOT; + unsigned int printed = 0; + errcode_t errcode; + + errcode = ext2fs_extent_open(current_fs, ino, &handle); + if (errcode) + return; + + if (flags & DUMP_EXTENT_TABLE) + fprintf(f, "Level Entries %*s %*s Length Flags\n", + (logical_width*2)+3, "Logical", + (physical_width*2)+3, "Physical"); + else + fprintf(f, "%sEXTENTS:\n%s", prefix, prefix); + + while (1) { + errcode = ext2fs_extent_get(handle, op, &extent); + + if (errcode) + break; + + op = EXT2_EXTENT_NEXT; + + if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) + continue; + + if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) { + if ((flags & DUMP_LEAF_EXTENTS) == 0) + continue; + } else { + if ((flags & DUMP_NODE_EXTENTS) == 0) + continue; + } + + errcode = ext2fs_extent_get_info(handle, &info); + if (errcode) + continue; + + if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) { + if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) + continue; + + if (flags & DUMP_EXTENT_TABLE) { + fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu " + "%*llu%*s %6u\n", + info.curr_level, info.max_depth, + info.curr_entry, info.num_entries, + logical_width, + (unsigned long long) extent.e_lblk, + logical_width, + (unsigned long long) extent.e_lblk + (extent.e_len - 1), + physical_width, + (unsigned long long) extent.e_pblk, + physical_width+3, "", extent.e_len); + continue; + } + + fprintf(f, "%s(ETB%d):%llu", + printed ? ", " : "", info.curr_level, + (unsigned long long) extent.e_pblk); + printed = 1; + continue; + } + + if (flags & DUMP_EXTENT_TABLE) { + fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu " + "%*llu - %*llu %6u %s\n", + info.curr_level, info.max_depth, + info.curr_entry, info.num_entries, + logical_width, + (unsigned long long) extent.e_lblk, + logical_width, + (unsigned long long) extent.e_lblk + (extent.e_len - 1), + physical_width, + (unsigned long long) extent.e_pblk, + physical_width, + (unsigned long long) extent.e_pblk + (extent.e_len - 1), + extent.e_len, + extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? + "Uninit" : ""); + continue; + } + + if (extent.e_len == 0) + continue; + else if (extent.e_len == 1) + fprintf(f, + "%s(%lld%s):%lld", + printed ? ", " : "", + (unsigned long long) extent.e_lblk, + extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? + "[u]" : "", + (unsigned long long) extent.e_pblk); + else + fprintf(f, + "%s(%lld-%lld%s):%lld-%lld", + printed ? ", " : "", + (unsigned long long) extent.e_lblk, + (unsigned long long) extent.e_lblk + (extent.e_len - 1), + extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? + "[u]" : "", + (unsigned long long) extent.e_pblk, + (unsigned long long) extent.e_pblk + (extent.e_len - 1)); + printed = 1; + } + if (printed) + fprintf(f, "\n"); + ext2fs_extent_free(handle); +} + +static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num) +{ + errcode_t retval; + size_t size; + + retval = ext2fs_inline_data_size(current_fs, inode_num, &size); + if (!retval) + fprintf(out, "%sSize of inline data: %zu\n", prefix, size); +} + +static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num, + struct ext2_inode *inode, const char *prefix) +{ + errcode_t retval; + char *buf = NULL; + size_t size; + + retval = ext2fs_inline_data_size(current_fs, inode_num, &size); + if (retval) + goto out; + + retval = ext2fs_get_memzero(size + 1, &buf); + if (retval) + goto out; + + retval = ext2fs_inline_data_get(current_fs, inode_num, + inode, buf, &size); + if (retval) + goto out; + + fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, + (int)size, buf); +out: + if (buf) + ext2fs_free_mem(&buf); + if (retval) + com_err(__func__, retval, "while dumping link destination"); +} + +void internal_dump_inode(FILE *out, const char *prefix, + ext2_ino_t inode_num, struct ext2_inode *inode, + int do_dump_blocks) +{ + const char *i_type; + char frag, fsize; + int os = current_fs->super->s_creator_os; + struct ext2_inode_large *large_inode; + int is_large_inode = 0; + + if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE) + is_large_inode = 1; + large_inode = (struct ext2_inode_large *) inode; + + if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory"; + else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular"; + else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink"; + else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special"; + else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special"; + else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO"; + else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket"; + else i_type = "bad type"; + fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type); + fprintf(out, "%sMode: 0%03o Flags: 0x%x\n", + prefix, inode->i_mode & 07777, inode->i_flags); + if (is_large_inode && large_inode->i_extra_isize >= 24) { + fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n", + prefix, inode->i_generation, large_inode->i_version_hi, + inode->osd1.linux1.l_i_version); + } else { + fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix, + inode->i_generation, inode->osd1.linux1.l_i_version); + } + fprintf(out, "%sUser: %5d Group: %5d", + prefix, inode_uid(*inode), inode_gid(*inode)); + if (is_large_inode && large_inode->i_extra_isize >= 32) + fprintf(out, " Project: %5d", large_inode->i_projid); + fputs(" Size: ", out); + if (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) + fprintf(out, "%llu\n", (unsigned long long) EXT2_I_SIZE(inode)); + else + fprintf(out, "%u\n", inode->i_size); + if (os == EXT2_OS_HURD) + fprintf(out, + "%sFile ACL: %u Translator: %u\n", + prefix, + inode->i_file_acl, + inode->osd1.hurd1.h_i_translator); + else + fprintf(out, "%sFile ACL: %llu\n", + prefix, + inode->i_file_acl | ((long long) + (inode->osd2.linux2.l_i_file_acl_high) << 32)); + if (os != EXT2_OS_HURD) + fprintf(out, "%sLinks: %u Blockcount: %llu\n", + prefix, inode->i_links_count, + (((unsigned long long) + inode->osd2.linux2.l_i_blocks_hi << 32)) + + inode->i_blocks); + else + fprintf(out, "%sLinks: %u Blockcount: %u\n", + prefix, inode->i_links_count, inode->i_blocks); + switch (os) { + case EXT2_OS_HURD: + frag = inode->osd2.hurd2.h_i_frag; + fsize = inode->osd2.hurd2.h_i_fsize; + break; + default: + frag = fsize = 0; + } + fprintf(out, "%sFragment: Address: %u Number: %u Size: %u\n", + prefix, inode->i_faddr, frag, fsize); + if (is_large_inode && large_inode->i_extra_isize >= 24) { + fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix, + inode->i_ctime, large_inode->i_ctime_extra, + inode_time_to_string(inode->i_ctime, + large_inode->i_ctime_extra)); + fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix, + inode->i_atime, large_inode->i_atime_extra, + inode_time_to_string(inode->i_atime, + large_inode->i_atime_extra)); + fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix, + inode->i_mtime, large_inode->i_mtime_extra, + inode_time_to_string(inode->i_mtime, + large_inode->i_mtime_extra)); + fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix, + large_inode->i_crtime, large_inode->i_crtime_extra, + inode_time_to_string(large_inode->i_crtime, + large_inode->i_crtime_extra)); + if (inode->i_dtime) + fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix, + large_inode->i_dtime, large_inode->i_ctime_extra, + inode_time_to_string(inode->i_dtime, + large_inode->i_ctime_extra)); + } else { + fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime, + time_to_string((__s32) inode->i_ctime)); + fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime, + time_to_string((__s32) inode->i_atime)); + fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime, + time_to_string((__s32) inode->i_mtime)); + if (inode->i_dtime) + fprintf(out, "%sdtime: 0x%08x -- %s", prefix, + inode->i_dtime, + time_to_string((__s32) inode->i_dtime)); + } + if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE) + internal_dump_inode_extra(out, prefix, inode_num, + (struct ext2_inode_large *) inode); + dump_inode_attributes(out, inode_num); + if (ext2fs_has_feature_metadata_csum(current_fs->super)) { + __u32 crc = inode->i_checksum_lo; + if (is_large_inode && + large_inode->i_extra_isize >= + (offsetof(struct ext2_inode_large, + i_checksum_hi) - + EXT2_GOOD_OLD_INODE_SIZE)) + crc |= ((__u32)large_inode->i_checksum_hi) << 16; + fprintf(out, "Inode checksum: 0x%08x\n", crc); + } + + if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode)) + fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, + (int)EXT2_I_SIZE(inode), (char *)inode->i_block); + else if (LINUX_S_ISLNK(inode->i_mode) && + (inode->i_flags & EXT4_INLINE_DATA_FL)) + dump_inline_symlink(out, inode_num, inode, prefix); + else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) { + int major, minor; + const char *devnote; + + if (inode->i_block[0]) { + major = (inode->i_block[0] >> 8) & 255; + minor = inode->i_block[0] & 255; + devnote = ""; + } else { + major = (inode->i_block[1] & 0xfff00) >> 8; + minor = ((inode->i_block[1] & 0xff) | + ((inode->i_block[1] >> 12) & 0xfff00)); + devnote = "(New-style) "; + } + fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n", + devnote, major, minor, major, minor); + } else if (do_dump_blocks) { + if (inode->i_flags & EXT4_EXTENTS_FL) + dump_extents(out, prefix, inode_num, + DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0); + else if (inode->i_flags & EXT4_INLINE_DATA_FL) + dump_inline_data(out, prefix, inode_num); + else + dump_blocks(out, prefix, inode_num); + } +} + +static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode) +{ + FILE *out; + + out = open_pager(); + internal_dump_inode(out, "", inode_num, inode, 1); + close_pager(out); +} + +void do_stat(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t inode; + struct ext2_inode * inode_buf; + + if (check_fs_open(argv[0])) + return; + + inode_buf = (struct ext2_inode *) + malloc(EXT2_INODE_SIZE(current_fs->super)); + if (!inode_buf) { + fprintf(stderr, "do_stat: can't allocate buffer\n"); + return; + } + + if (common_inode_args_process(argc, argv, &inode, 0)) { + free(inode_buf); + return; + } + + if (debugfs_read_inode2(inode, inode_buf, argv[0], + EXT2_INODE_SIZE(current_fs->super), 0)) { + free(inode_buf); + return; + } + + dump_inode(inode, inode_buf); + free(inode_buf); + return; +} + +void do_dump_extents(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + struct ext2_inode inode; + ext2_ino_t ino; + FILE *out; + int c, flags = 0; + int logical_width; + int physical_width; + + reset_getopt(); + while ((c = getopt(argc, argv, "nl")) != EOF) { + switch (c) { + case 'n': + flags |= DUMP_NODE_EXTENTS; + break; + case 'l': + flags |= DUMP_LEAF_EXTENTS; + break; + } + } + + if (argc != optind + 1) { + com_err(0, 0, "Usage: dump_extents [-n] [-l] file"); + return; + } + + if (flags == 0) + flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS; + flags |= DUMP_EXTENT_TABLE; + + if (check_fs_open(argv[0])) + return; + + ino = string_to_inode(argv[optind]); + if (ino == 0) + return; + + if (debugfs_read_inode(ino, &inode, argv[0])) + return; + + if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) { + fprintf(stderr, "%s: does not uses extent block maps\n", + argv[optind]); + return; + } + + logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/ + current_fs->blocksize) + 1; + if (logical_width < 5) + logical_width = 5; + physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1; + if (physical_width < 5) + physical_width = 5; + + out = open_pager(); + dump_extents(out, "", ino, flags, logical_width, physical_width); + close_pager(out); + return; +} + +static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), + blk64_t *blocknr, + 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))) +{ + printf("%llu ", (unsigned long long) *blocknr); + return 0; +} + +void do_blocks(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t inode; + + if (check_fs_open(argv[0])) + return; + + if (common_inode_args_process(argc, argv, &inode, 0)) { + return; + } + + ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, + print_blocks_proc, NULL); + fputc('\n', stdout); + return; +} + +void do_chroot(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t inode; + int retval; + + if (common_inode_args_process(argc, argv, &inode, 0)) + return; + + retval = ext2fs_check_directory(current_fs, inode); + if (retval) { + com_err(argv[1], retval, 0); + return; + } + root = inode; +} + +#ifndef READ_ONLY +void do_clri(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t inode; + struct ext2_inode inode_buf; + + if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) + return; + + if (debugfs_read_inode(inode, &inode_buf, argv[0])) + return; + memset(&inode_buf, 0, sizeof(inode_buf)); + if (debugfs_write_inode(inode, &inode_buf, argv[0])) + return; +} + +void do_freei(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + unsigned int len = 1; + int err = 0; + ext2_ino_t inode; + + if (common_args_process(argc, argv, 2, 3, argv[0], " [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 (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"); + 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[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + unsigned int len = 1; + int err = 0; + ext2_ino_t inode; + + if (common_args_process(argc, argv, 2, 3, argv[0], " [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 (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"); + 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[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t inode; + + if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS)) + return; + + if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) + printf("Inode %u is marked in use\n", inode); + else + printf("Inode %u is not in use\n", inode); +} + +#ifndef READ_ONLY +void do_freeb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + blk64_t block; + blk64_t count = 1; + + if (common_block_args_process(argc, argv, &block, &count)) + return; + if (check_fs_read_write(argv[0])) + return; + while (count-- > 0) { + if (!ext2fs_test_block_bitmap2(current_fs->block_map,block)) + com_err(argv[0], 0, "Warning: block %llu already clear", + (unsigned long long) block); + ext2fs_unmark_block_bitmap2(current_fs->block_map,block); + block++; + } + ext2fs_mark_bb_dirty(current_fs); +} + +void do_setb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + blk64_t block; + blk64_t count = 1; + + if (common_block_args_process(argc, argv, &block, &count)) + return; + if (check_fs_read_write(argv[0])) + return; + while (count-- > 0) { + if (ext2fs_test_block_bitmap2(current_fs->block_map,block)) + com_err(argv[0], 0, "Warning: block %llu already set", + (unsigned long long) block); + ext2fs_mark_block_bitmap2(current_fs->block_map,block); + block++; + } + ext2fs_mark_bb_dirty(current_fs); +} +#endif /* READ_ONLY */ + +void do_testb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + blk64_t block; + blk64_t count = 1; + + if (common_block_args_process(argc, argv, &block, &count)) + return; + while (count-- > 0) { + if (ext2fs_test_block_bitmap2(current_fs->block_map,block)) + printf("Block %llu marked in use\n", + (unsigned long long) block); + else + printf("Block %llu not in use\n", + (unsigned long long) block); + block++; + } +} + +#ifndef READ_ONLY +static void modify_u8(char *com, const char *prompt, + const char *format, __u8 *val) +{ + char buf[200]; + unsigned long v; + char *tmp; + + sprintf(buf, format, *val); + printf("%30s [%s] ", prompt, buf); + if (!fgets(buf, sizeof(buf), stdin)) + return; + if (buf[strlen (buf) - 1] == '\n') + buf[strlen (buf) - 1] = '\0'; + if (!buf[0]) + return; + v = strtoul(buf, &tmp, 0); + if (*tmp) + com_err(com, 0, "Bad value - %s", buf); + else + *val = v; +} + +static void modify_u16(char *com, const char *prompt, + const char *format, __u16 *val) +{ + char buf[200]; + unsigned long v; + char *tmp; + + sprintf(buf, format, *val); + printf("%30s [%s] ", prompt, buf); + if (!fgets(buf, sizeof(buf), stdin)) + return; + if (buf[strlen (buf) - 1] == '\n') + buf[strlen (buf) - 1] = '\0'; + if (!buf[0]) + return; + v = strtoul(buf, &tmp, 0); + if (*tmp) + com_err(com, 0, "Bad value - %s", buf); + else + *val = v; +} + +static void modify_u32(char *com, const char *prompt, + const char *format, __u32 *val) +{ + char buf[200]; + unsigned long v; + char *tmp; + + sprintf(buf, format, *val); + printf("%30s [%s] ", prompt, buf); + if (!fgets(buf, sizeof(buf), stdin)) + return; + if (buf[strlen (buf) - 1] == '\n') + buf[strlen (buf) - 1] = '\0'; + if (!buf[0]) + return; + v = strtoul(buf, &tmp, 0); + if (*tmp) + com_err(com, 0, "Bad value - %s", buf); + else + *val = v; +} + + +void do_modify_inode(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + struct ext2_inode inode; + ext2_ino_t inode_num; + int i; + unsigned char *frag, *fsize; + char buf[80]; + int os; + const char *hex_format = "0x%x"; + const char *octal_format = "0%o"; + const char *decimal_format = "%d"; + const char *unsignedlong_format = "%lu"; + + if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) + return; + + os = current_fs->super->s_creator_os; + + if (debugfs_read_inode(inode_num, &inode, argv[1])) + return; + + modify_u16(argv[0], "Mode", octal_format, &inode.i_mode); + modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid); + modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid); + modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size); + modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime); + modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime); + modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime); + modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime); + modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count); + if (os == EXT2_OS_LINUX) + modify_u16(argv[0], "Block count high", unsignedlong_format, + &inode.osd2.linux2.l_i_blocks_hi); + modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks); + modify_u32(argv[0], "File flags", hex_format, &inode.i_flags); + modify_u32(argv[0], "Generation", hex_format, &inode.i_generation); +#if 0 + modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1); +#endif + modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl); + + modify_u32(argv[0], "High 32bits of size", decimal_format, + &inode.i_size_high); + + if (os == EXT2_OS_HURD) + modify_u32(argv[0], "Translator Block", + decimal_format, &inode.osd1.hurd1.h_i_translator); + + modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr); + switch (os) { + case EXT2_OS_HURD: + frag = &inode.osd2.hurd2.h_i_frag; + fsize = &inode.osd2.hurd2.h_i_fsize; + break; + default: + frag = fsize = 0; + } + if (frag) + modify_u8(argv[0], "Fragment number", decimal_format, frag); + if (fsize) + modify_u8(argv[0], "Fragment size", decimal_format, fsize); + + for (i=0; i < EXT2_NDIR_BLOCKS; i++) { + sprintf(buf, "Direct Block #%u", i); + modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]); + } + modify_u32(argv[0], "Indirect Block", decimal_format, + &inode.i_block[EXT2_IND_BLOCK]); + modify_u32(argv[0], "Double Indirect Block", decimal_format, + &inode.i_block[EXT2_DIND_BLOCK]); + modify_u32(argv[0], "Triple Indirect Block", decimal_format, + &inode.i_block[EXT2_TIND_BLOCK]); + if (debugfs_write_inode(inode_num, &inode, argv[1])) + return; +} +#endif /* READ_ONLY */ + +void do_change_working_dir(int argc, char *argv[], + int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t inode; + int retval; + + if (common_inode_args_process(argc, argv, &inode, 0)) + return; + + retval = ext2fs_check_directory(current_fs, inode); + if (retval) { + com_err(argv[1], retval, 0); + return; + } + cwd = inode; + return; +} + +void do_print_working_directory(int argc, char *argv[], + int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + int retval; + char *pathname = NULL; + + if (common_args_process(argc, argv, 1, 1, + "print_working_directory", "", 0)) + return; + + retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname); + if (retval) { + com_err(argv[0], retval, + "while trying to get pathname of cwd"); + } + printf("[pwd] INODE: %6u PATH: %s\n", + cwd, pathname ? pathname : "NULL"); + if (pathname) { + free(pathname); + pathname = NULL; + } + retval = ext2fs_get_pathname(current_fs, root, 0, &pathname); + if (retval) { + com_err(argv[0], retval, + "while trying to get pathname of root"); + } + printf("[root] INODE: %6u PATH: %s\n", + root, pathname ? pathname : "NULL"); + if (pathname) { + free(pathname); + pathname = NULL; + } + return; +} + +#ifndef READ_ONLY +static void make_link(char *sourcename, char *destname) +{ + ext2_ino_t ino; + struct ext2_inode inode; + int retval; + ext2_ino_t dir; + char *dest, *cp, *base_name; + + /* + * Get the source inode + */ + ino = string_to_inode(sourcename); + if (!ino) + return; + base_name = strrchr(sourcename, '/'); + if (base_name) + base_name++; + else + base_name = sourcename; + /* + * Figure out the destination. First see if it exists and is + * a directory. + */ + if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir))) + dest = base_name; + else { + /* + * OK, it doesn't exist. See if it is + * '/basename' or 'basename' + */ + cp = strrchr(destname, '/'); + if (cp) { + *cp = 0; + dir = string_to_inode(destname); + if (!dir) + return; + dest = cp+1; + } else { + dir = cwd; + dest = destname; + } + } + + if (debugfs_read_inode(ino, &inode, sourcename)) + return; + + retval = ext2fs_link(current_fs, dir, dest, ino, + ext2_file_type(inode.i_mode)); + if (retval) + com_err("make_link", retval, 0); + return; +} + + +void do_link(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + if (common_args_process(argc, argv, 3, 3, "link", + " ", CHECK_FS_RW)) + return; + + make_link(argv[1], argv[2]); +} + +static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr, + 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))) +{ + blk64_t block; + + block = *blocknr; + ext2fs_block_alloc_stats2(fs, block, +1); + return 0; +} + +void do_undel(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t ino; + struct ext2_inode inode; + + if (common_args_process(argc, argv, 2, 3, "undelete", + " [dest_name]", + CHECK_FS_RW | CHECK_FS_BITMAPS)) + return; + + ino = string_to_inode(argv[1]); + if (!ino) + return; + + if (debugfs_read_inode(ino, &inode, argv[1])) + return; + + if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) { + com_err(argv[1], 0, "Inode is not marked as deleted"); + return; + } + + /* + * XXX this function doesn't handle changing the links count on the + * parent directory when undeleting a directory. + */ + inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1; + inode.i_dtime = 0; + + if (debugfs_write_inode(ino, &inode, argv[0])) + return; + + ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL, + mark_blocks_proc, NULL); + + ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0); + + if (argc > 2) + make_link(argv[1], argv[2]); +} + +static void unlink_file_by_name(char *filename) +{ + int retval; + ext2_ino_t dir; + char *base_name; + + base_name = strrchr(filename, '/'); + if (base_name) { + *base_name++ = '\0'; + dir = string_to_inode(filename); + if (!dir) + return; + } else { + dir = cwd; + base_name = filename; + } + retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0); + if (retval) + com_err("unlink_file_by_name", retval, 0); + return; +} + +void do_unlink(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + if (common_args_process(argc, argv, 2, 2, "link", + "", CHECK_FS_RW)) + return; + + unlink_file_by_name(argv[1]); +} + +void do_copy_inode(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t src_ino, dest_ino; + unsigned char buf[4096]; + + if (common_args_process(argc, argv, 3, 3, "copy_inode", + " ", CHECK_FS_RW)) + return; + + src_ino = string_to_inode(argv[1]); + if (!src_ino) + return; + + dest_ino = string_to_inode(argv[2]); + if (!dest_ino) + return; + + if (debugfs_read_inode2(src_ino, (struct ext2_inode *) buf, + argv[0], sizeof(buf), 0)) + return; + + if (debugfs_write_inode2(dest_ino, (struct ext2_inode *) buf, + argv[0], sizeof(buf), 0)) + return; +} + +#endif /* READ_ONLY */ + +void do_find_free_block(int argc, char *argv[], + int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + blk64_t free_blk, goal, first_free = 0; + int count; + errcode_t retval; + char *tmp; + + if ((argc > 3) || (argc==2 && *argv[1] == '?')) { + com_err(argv[0], 0, "Usage: find_free_block [count [goal]]"); + return; + } + if (check_fs_open(argv[0])) + return; + + if (argc > 1) { + count = strtol(argv[1],&tmp,0); + if (*tmp) { + com_err(argv[0], 0, "Bad count - %s", argv[1]); + return; + } + } else + count = 1; + + if (argc > 2) { + goal = strtol(argv[2], &tmp, 0); + if (*tmp) { + com_err(argv[0], 0, "Bad goal - %s", argv[1]); + return; + } + } + else + goal = current_fs->super->s_first_data_block; + + printf("Free blocks found: "); + free_blk = goal - 1; + while (count-- > 0) { + retval = ext2fs_new_block2(current_fs, free_blk + 1, 0, + &free_blk); + if (first_free) { + if (first_free == free_blk) + break; + } else + first_free = free_blk; + if (retval) { + com_err("ext2fs_new_block", retval, 0); + return; + } else + printf("%llu ", (unsigned long long) free_blk); + } + printf("\n"); +} + +void do_find_free_inode(int argc, char *argv[], + int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t free_inode, dir; + int mode; + int retval; + char *tmp; + + if (argc > 3 || (argc>1 && *argv[1] == '?')) { + com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]"); + return; + } + if (check_fs_open(argv[0])) + return; + + if (argc > 1) { + dir = strtol(argv[1], &tmp, 0); + if (*tmp) { + com_err(argv[0], 0, "Bad dir - %s", argv[1]); + return; + } + } + else + dir = root; + if (argc > 2) { + mode = strtol(argv[2], &tmp, 0); + if (*tmp) { + com_err(argv[0], 0, "Bad mode - %s", argv[2]); + return; + } + } else + mode = 010755; + + retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode); + if (retval) + com_err("ext2fs_new_inode", retval, 0); + else + printf("Free inode found: %u\n", free_inode); +} + +#ifndef READ_ONLY +void do_write(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + errcode_t retval; + + if (common_args_process(argc, argv, 3, 3, "write", + " ", CHECK_FS_RW)) + return; + + retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root); + if (retval) + com_err(argv[0], retval, 0); +} + +void do_mknod(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + unsigned long major, minor; + errcode_t retval; + int nr; + struct stat st; + + if (check_fs_open(argv[0])) + return; + if (argc < 3 || argv[2][1]) { + usage: + com_err(argv[0], 0, "Usage: mknod [p| [c|b] ]"); + return; + } + + minor = major = 0; + switch (argv[2][0]) { + case 'p': + st.st_mode = S_IFIFO; + nr = 3; + break; + case 'c': + st.st_mode = S_IFCHR; + nr = 5; + break; + case 'b': + st.st_mode = S_IFBLK; + nr = 5; + break; + default: + nr = 0; + } + + if (nr == 5) { + major = strtoul(argv[3], argv+3, 0); + minor = strtoul(argv[4], argv+4, 0); + if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0]) + nr = 0; + } + + if (argc != nr) + goto usage; + + st.st_rdev = makedev(major, minor); + retval = do_mknod_internal(current_fs, cwd, argv[1], + st.st_mode, st.st_rdev); + if (retval) + com_err(argv[0], retval, 0); +} + +void do_mkdir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + errcode_t retval; + + if (common_args_process(argc, argv, 2, 2, "mkdir", + "", CHECK_FS_RW)) + return; + + retval = do_mkdir_internal(current_fs, cwd, argv[1], root); + if (retval) + com_err(argv[0], retval, 0); + +} + +static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr, + e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), + blk64_t ref_block EXT2FS_ATTR((unused)), + int ref_offset EXT2FS_ATTR((unused)), + void *private) +{ + blk64_t block = *blocknr; + blk64_t *last_cluster = (blk64_t *)private; + blk64_t cluster = EXT2FS_B2C(fs, block); + + if (cluster == *last_cluster) + return 0; + + *last_cluster = cluster; + + ext2fs_block_alloc_stats2(fs, block, -1); + return 0; +} + +static void kill_file_by_inode(ext2_ino_t inode) +{ + struct ext2_inode inode_buf; + + if (debugfs_read_inode(inode, &inode_buf, 0)) + return; + inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0); + if (debugfs_write_inode(inode, &inode_buf, 0)) + return; + if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) { + blk64_t last_cluster = 0; + ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, + NULL, release_blocks_proc, &last_cluster); + } + printf("\n"); + ext2fs_inode_alloc_stats2(current_fs, inode, -1, + LINUX_S_ISDIR(inode_buf.i_mode)); +} + + +void do_kill_file(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t inode_num; + + if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) + return; + + kill_file_by_inode(inode_num); +} + +void do_rm(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + int retval; + ext2_ino_t inode_num; + struct ext2_inode inode; + + if (common_args_process(argc, argv, 2, 2, "rm", + "", CHECK_FS_RW)) + return; + + retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); + if (retval) { + com_err(argv[0], retval, "while trying to resolve filename"); + return; + } + + if (debugfs_read_inode(inode_num, &inode, argv[0])) + return; + + if (LINUX_S_ISDIR(inode.i_mode)) { + com_err(argv[0], 0, "file is a directory"); + return; + } + + --inode.i_links_count; + if (debugfs_write_inode(inode_num, &inode, argv[0])) + return; + + unlink_file_by_name(argv[1]); + if (inode.i_links_count == 0) + kill_file_by_inode(inode_num); +} + +struct rd_struct { + ext2_ino_t parent; + int empty; +}; + +static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), + int entry EXT2FS_ATTR((unused)), + struct ext2_dir_entry *dirent, + int offset EXT2FS_ATTR((unused)), + int blocksize EXT2FS_ATTR((unused)), + char *buf EXT2FS_ATTR((unused)), + void *private) +{ + struct rd_struct *rds = (struct rd_struct *) private; + + if (dirent->inode == 0) + return 0; + if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.')) + return 0; + if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') && + (dirent->name[1] == '.')) { + rds->parent = dirent->inode; + return 0; + } + rds->empty = 0; + return 0; +} + +void do_rmdir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + int retval; + ext2_ino_t inode_num; + struct ext2_inode inode; + struct rd_struct rds; + + if (common_args_process(argc, argv, 2, 2, "rmdir", + "", CHECK_FS_RW)) + return; + + retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); + if (retval) { + com_err(argv[0], retval, "while trying to resolve filename"); + return; + } + + if (debugfs_read_inode(inode_num, &inode, argv[0])) + return; + + if (!LINUX_S_ISDIR(inode.i_mode)) { + com_err(argv[0], 0, "file is not a directory"); + return; + } + + rds.parent = 0; + rds.empty = 1; + + retval = ext2fs_dir_iterate2(current_fs, inode_num, 0, + 0, rmdir_proc, &rds); + if (retval) { + com_err(argv[0], retval, "while iterating over directory"); + return; + } + if (rds.empty == 0) { + com_err(argv[0], 0, "directory not empty"); + return; + } + + inode.i_links_count = 0; + if (debugfs_write_inode(inode_num, &inode, argv[0])) + return; + + unlink_file_by_name(argv[1]); + kill_file_by_inode(inode_num); + + if (rds.parent) { + if (debugfs_read_inode(rds.parent, &inode, argv[0])) + return; + if (inode.i_links_count > 1) + inode.i_links_count--; + if (debugfs_write_inode(rds.parent, &inode, argv[0])) + return; + } +} +#endif /* READ_ONLY */ + +void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)), + char *argv[] EXT2FS_ATTR((unused)), + int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + if (current_fs) + 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[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t inode; + int retval; + + if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) + return; + + retval = ext2fs_expand_dir(current_fs, inode); + if (retval) + com_err("ext2fs_expand_dir", retval, 0); + return; +} + +void do_features(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + int i; + + if (check_fs_open(argv[0])) + return; + + if ((argc != 1) && check_fs_read_write(argv[0])) + return; + for (i=1; i < argc; i++) { + if (e2p_edit_feature(argv[i], + ¤t_fs->super->s_feature_compat, 0)) + com_err(argv[0], 0, "Unknown feature: %s\n", + argv[i]); + else + ext2fs_mark_super_dirty(current_fs); + } + print_features(current_fs->super, stdout); +} +#endif /* READ_ONLY */ + +void do_bmap(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t ino; + blk64_t blk, pblk = 0; + int c, err, flags = 0, ret_flags = 0; + errcode_t errcode; + + if (check_fs_open(argv[0])) + return; + + 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] logical_blk [physical_blk]"); + return; + } + + ino = string_to_inode(argv[optind++]); + if (!ino) + return; + err = strtoblk(argv[0], argv[optind++], "logical block", &blk); + if (err) + return; + + 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", + (unsigned long long) blk); + return; + } + printf("%llu", (unsigned long long) pblk); + if (ret_flags & BMAP_RET_UNINIT) + fputs(" (uninit)", stdout); + fputc('\n', stdout); +} + +void do_imap(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + ext2_ino_t ino; + unsigned long group, block, block_nr, offset; + + if (common_args_process(argc, argv, 2, 2, argv[0], + "", 0)) + return; + ino = string_to_inode(argv[1]); + if (!ino) + return; + + group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super); + offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) * + EXT2_INODE_SIZE(current_fs->super); + block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super); + if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) { + com_err(argv[0], 0, "Inode table for group %lu is missing\n", + group); + return; + } + block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) + + block; + offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1); + + printf("Inode %u is part of block group %lu\n" + "\tlocated at block %lu, offset 0x%04lx\n", ino, group, + block_nr, offset); + +} + +void do_idump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + struct ext2_inode_large *inode; + ext2_ino_t ino; + unsigned char *buf; + errcode_t err; + unsigned int isize, size, offset = 0; + int c, mode = 0; + + reset_getopt(); + while ((c = getopt (argc, argv, "bex")) != EOF) { + if (mode || c == '?') { + com_err(argv[0], 0, + "Usage: inode_dump [-b]|[-e] "); + return; + } + mode = c; + } + if (optind != argc-1) + return; + + if (check_fs_open(argv[0])) + return; + + ino = string_to_inode(argv[optind]); + 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 %u", ino); + goto err; + } + + inode = (struct ext2_inode_large *) buf; + size = isize; + switch (mode) { + case 'b': + offset = ((char *) (&inode->i_block)) - ((char *) buf); + size = sizeof(inode->i_block); + break; + case 'x': + case 'e': + if (size <= EXT2_GOOD_OLD_INODE_SIZE) { + com_err(argv[0], 0, "No extra space in inode"); + goto err; + } + offset = EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize; + if (offset > size) + goto err; + size -= offset; + break; + } + if (mode == 'x') + raw_inode_xattr_dump(stdout, buf + offset, size); + else + do_byte_hexdump(stdout, buf + offset, size); +err: + ext2fs_free_mem(&buf); +} + +#ifndef READ_ONLY +void do_set_current_time(int argc, char *argv[], + int sci_idx EXT2FS_ATTR((unused)), + void *infop EXT2FS_ATTR((unused))) +{ + __s64 now; + + if (common_args_process(argc, argv, 2, 2, argv[0], + "