2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
24 extern int optreset; /* defined by BSD, but not others */
30 #include <sys/types.h>
33 #include "et/com_err.h"
36 #include "uuid/uuid.h"
39 #include "../version.h"
41 extern ss_request_table debug_cmds;
43 ext2_filsys current_fs = NULL;
46 static void open_filesystem(char *device, int open_flags)
50 retval = ext2fs_open(device, open_flags, 0, 0,
51 unix_io_manager, ¤t_fs);
53 com_err(device, retval, "while opening filesystem");
57 retval = ext2fs_read_inode_bitmap(current_fs);
59 com_err(device, retval, "while reading inode bitmap");
62 retval = ext2fs_read_block_bitmap(current_fs);
64 com_err(device, retval, "while reading block bitmap");
67 root = cwd = EXT2_ROOT_INO;
71 retval = ext2fs_close(current_fs);
73 com_err(device, retval, "while trying to close filesystem");
77 void do_open_filesys(int argc, char **argv)
79 const char *usage = "Usage: open [-w] <device>";
85 optreset = 1; /* Makes BSD getopt happy */
87 while ((c = getopt (argc, argv, "wf")) != EOF) {
90 open_flags |= EXT2_FLAG_RW;
93 open_flags |= EXT2_FLAG_FORCE;
96 com_err(argv[0], 0, usage);
100 if (optind != argc-1) {
101 com_err(argv[0], 0, usage);
104 if (check_fs_not_open(argv[0]))
106 open_filesystem(argv[optind], open_flags);
109 static void close_filesystem(NOARGS)
113 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
114 retval = ext2fs_write_inode_bitmap(current_fs);
116 com_err("ext2fs_write_inode_bitmap", retval, "");
118 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
119 retval = ext2fs_write_block_bitmap(current_fs);
121 com_err("ext2fs_write_block_bitmap", retval, "");
123 retval = ext2fs_close(current_fs);
125 com_err("ext2fs_close", retval, "");
130 void do_close_filesys(int argc, char **argv)
133 com_err(argv[0], 0, "Usage: close_filesys");
136 if (check_fs_open(argv[0]))
141 void do_init_filesys(int argc, char **argv)
143 const char *usage = "Usage: initialize <device> <blocksize>";
144 struct ext2_super_block param;
149 com_err(argv[0], 0, usage);
152 if (check_fs_not_open(argv[0]))
155 memset(¶m, 0, sizeof(struct ext2_super_block));
156 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
158 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
161 retval = ext2fs_initialize(argv[1], 0, ¶m,
162 unix_io_manager, ¤t_fs);
164 com_err(argv[1], retval, "while initializing filesystem");
168 root = cwd = EXT2_ROOT_INO;
172 static void print_features(struct ext2fs_sb * s, FILE *f)
174 #ifdef EXT2_DYNAMIC_REV
176 __u32 *mask = &s->s_feature_compat, m;
178 printf ("Filesystem features:");
179 for (i=0; i <3; i++,mask++) {
180 for (j=0,m=1; j < 32; j++, m<<=1) {
182 fprintf(f, " %s", e2p_feature2string(i, m));
193 void do_show_super_stats(int argc, char *argv[])
197 struct ext2fs_sb *sb;
198 struct ext2_group_desc *gdp;
199 int c, header_only = 0;
201 const char *none = "(none)";
202 const char *usage = "Usage: show_super [-h]";
206 optreset = 1; /* Makes BSD getopt happy */
208 while ((c = getopt (argc, argv, "h")) != EOF) {
214 com_err(argv[0], 0, usage);
218 if (optind != argc) {
219 com_err(argv[0], 0, usage);
222 if (check_fs_open(argv[0]))
225 sb = (struct ext2fs_sb *) current_fs->super;
226 fprintf(out, "Filesystem is read-%s\n",
227 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
228 if (sb->s_volume_name[0]) {
229 memset(buf, 0, sizeof(buf));
230 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
233 fprintf(out, "Volume name = %s\n", buf);
234 if (sb->s_last_mounted[0]) {
235 memset(buf, 0, sizeof(buf));
236 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
239 fprintf(out, "Last mounted directory = %s\n", buf);
240 if (!uuid_is_null(sb->s_uuid))
241 uuid_unparse(sb->s_uuid, buf);
244 fprintf(out, "Filesystem UUID = %s\n", buf);
245 print_features(sb, out);
246 fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
247 fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
248 fprintf(out, "Mount counts = %d (maximal = %d)\n",
249 sb->s_mnt_count, sb->s_max_mnt_count);
250 fputs ("Filesystem OS type = ", out);
251 switch (sb->s_creator_os) {
252 case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
253 case EXT2_OS_HURD: fputs ("GNU\n", out); break;
254 case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
255 default: fputs ("unknown\n", out);
257 fprintf(out, "Superblock size = %d\n",
258 sizeof(struct ext2_super_block));
259 fprintf(out, "Block size = %d, fragment size = %d\n",
260 EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
261 fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
262 fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
263 sb->s_free_inodes_count);
264 fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
265 sb->s_blocks_count, sb->s_free_blocks_count,
266 sb->s_r_blocks_count, sb->s_first_data_block);
267 fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
268 fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
269 fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
270 fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
271 current_fs->group_desc_count,
272 (current_fs->group_desc_count != 1) ? "s" : "",
273 current_fs->desc_blocks,
274 (current_fs->desc_blocks != 1) ? "s" : "");
281 gdp = ¤t_fs->group_desc[0];
282 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
283 fprintf(out, " Group %2d: block bitmap at %d, "
284 "inode bitmap at %d, "
285 "inode table at %d\n"
288 "%d used director%s\n",
289 i, gdp->bg_block_bitmap,
290 gdp->bg_inode_bitmap, gdp->bg_inode_table,
291 gdp->bg_free_blocks_count,
292 gdp->bg_free_blocks_count != 1 ? "s" : "",
293 gdp->bg_free_inodes_count,
294 gdp->bg_free_inodes_count != 1 ? "s" : "",
295 gdp->bg_used_dirs_count,
296 gdp->bg_used_dirs_count != 1 ? "ies" : "y");
300 void do_dirty_filesys(int argc, char **argv)
302 if (check_fs_open(argv[0]))
304 if (check_fs_read_write(argv[0]))
307 if (argv[1] && !strcmp(argv[1], "-clean"))
308 current_fs->super->s_state |= EXT2_VALID_FS;
310 current_fs->super->s_state &= ~EXT2_VALID_FS;
311 ext2fs_mark_super_dirty(current_fs);
314 struct list_blocks_struct {
319 static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
322 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
324 fprintf(lb->f, "%d ", *blocknr);
330 static void dump_blocks(FILE *f, ino_t inode)
332 struct list_blocks_struct lb;
334 fprintf(f, "BLOCKS:\n");
337 ext2fs_block_iterate(current_fs, inode, 0, NULL,
338 list_blocks_proc, (void *)&lb);
340 fprintf(f, "\nTOTAL: %d\n", lb.total);
345 static void dump_inode(ino_t inode_num, struct ext2_inode inode)
350 int os = current_fs->super->s_creator_os;
353 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
354 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
355 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
356 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
357 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
358 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
359 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
360 else i_type = "bad type";
361 fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type);
362 #if defined(HAVE_EXT2_INODE_VERSION)
363 fprintf(out, "Mode: %04o Flags: 0x%x Version/Generation: %d\n",
364 inode.i_mode & 0777, inode.i_flags, inode.i_version);
366 fprintf(out, "Mode: %04o Flags: 0x%x Version/Generation: %d\n",
367 inode.i_mode & 0777, inode.i_flags, inode.i_generation);
369 fprintf(out, "User: %5d Group: %5d Size: ",
370 inode.i_uid, inode.i_gid);
371 if (LINUX_S_ISDIR(inode.i_mode))
372 fprintf(out, "%d\n", inode.i_size);
374 __u64 i_size = (inode.i_size |
375 ((unsigned long long)inode.i_size_high << 32));
377 fprintf(out, "%lld\n", i_size);
379 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
381 "File ACL: %d Directory ACL: %d Translator: %d\n",
382 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
383 inode.osd1.hurd1.h_i_translator);
385 fprintf(out, "File ACL: %d Directory ACL: %d\n",
386 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
387 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
391 frag = inode.osd2.linux2.l_i_frag;
392 fsize = inode.osd2.linux2.l_i_fsize;
395 frag = inode.osd2.hurd2.h_i_frag;
396 fsize = inode.osd2.hurd2.h_i_fsize;
399 frag = inode.osd2.masix2.m_i_frag;
400 fsize = inode.osd2.masix2.m_i_fsize;
405 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
406 inode.i_faddr, frag, fsize);
407 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
408 time_to_string(inode.i_ctime));
409 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
410 time_to_string(inode.i_atime));
411 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
412 time_to_string(inode.i_mtime));
414 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
415 time_to_string(inode.i_dtime));
416 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
417 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
419 dump_blocks(out, inode_num);
424 void do_stat(int argc, char *argv[])
427 struct ext2_inode inode_buf;
431 com_err(argv[0], 0, "Usage: stat <file>");
434 if (check_fs_open(argv[0]))
436 inode = string_to_inode(argv[1]);
440 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
443 com_err(argv[0], retval, "while trying to read inode %d", inode);
447 dump_inode(inode,inode_buf);
451 void do_chroot(int argc, char *argv[])
457 com_err(argv[0], 0, "Usage: chroot <file>");
460 if (check_fs_open(argv[0]))
462 inode = string_to_inode(argv[1]);
466 retval = ext2fs_check_directory(current_fs, inode);
468 com_err(argv[1], retval, "");
474 void do_clri(int argc, char *argv[])
478 struct ext2_inode inode_buf;
481 com_err(argv[0], 0, "Usage: clri <file>");
484 if (check_fs_open(argv[0]))
486 if (check_fs_read_write(argv[0]))
488 inode = string_to_inode(argv[1]);
492 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
494 com_err(argv[0], retval, "while trying to read inode %d",
498 memset(&inode_buf, 0, sizeof(inode_buf));
499 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
501 com_err(argv[0], retval, "while trying to write inode %d",
507 void do_freei(int argc, char *argv[])
512 com_err(argv[0], 0, "Usage: freei <file>");
515 if (check_fs_open(argv[0]))
517 if (check_fs_read_write(argv[0]))
519 inode = string_to_inode(argv[1]);
523 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
524 com_err(argv[0], 0, "Warning: inode already clear");
525 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
526 ext2fs_mark_ib_dirty(current_fs);
529 void do_seti(int argc, char *argv[])
534 com_err(argv[0], 0, "Usage: seti <file>");
537 if (check_fs_open(argv[0]))
539 if (check_fs_read_write(argv[0]))
541 inode = string_to_inode(argv[1]);
545 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
546 com_err(argv[0], 0, "Warning: inode already set");
547 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
548 ext2fs_mark_ib_dirty(current_fs);
551 void do_testi(int argc, char *argv[])
556 com_err(argv[0], 0, "Usage: testi <file>");
559 if (check_fs_open(argv[0]))
561 inode = string_to_inode(argv[1]);
565 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
566 printf("Inode %ld is marked in use\n", inode);
568 printf("Inode %ld is not in use\n", inode);
572 void do_freeb(int argc, char *argv[])
578 com_err(argv[0], 0, "Usage: freeb <block>");
581 if (check_fs_open(argv[0]))
583 if (check_fs_read_write(argv[0]))
585 block = strtoul(argv[1], &tmp, 0);
586 if (!block || *tmp) {
587 com_err(argv[0], 0, "No block 0");
590 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
591 com_err(argv[0], 0, "Warning: block already clear");
592 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
593 ext2fs_mark_bb_dirty(current_fs);
596 void do_setb(int argc, char *argv[])
602 com_err(argv[0], 0, "Usage: setb <block>");
605 if (check_fs_open(argv[0]))
607 if (check_fs_read_write(argv[0]))
609 block = strtoul(argv[1], &tmp, 0);
610 if (!block || *tmp) {
611 com_err(argv[0], 0, "No block 0");
614 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
615 com_err(argv[0], 0, "Warning: block already set");
616 ext2fs_mark_block_bitmap(current_fs->block_map,block);
617 ext2fs_mark_bb_dirty(current_fs);
620 void do_testb(int argc, char *argv[])
626 com_err(argv[0], 0, "Usage: testb <block>");
629 if (check_fs_open(argv[0]))
631 block = strtoul(argv[1], &tmp, 0);
632 if (!block || *tmp) {
633 com_err(argv[0], 0, "No block 0");
636 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
637 printf("Block %d marked in use\n", block);
638 else printf("Block %d not in use\n", block);
641 static void modify_u8(char *com, const char *prompt,
642 const char *format, __u8 *val)
648 sprintf(buf, format, *val);
649 printf("%30s [%s] ", prompt, buf);
650 fgets(buf, sizeof(buf), stdin);
651 if (buf[strlen (buf) - 1] == '\n')
652 buf[strlen (buf) - 1] = '\0';
655 v = strtoul(buf, &tmp, 0);
657 com_err(com, 0, "Bad value - %s", buf);
662 static void modify_u16(char *com, const char *prompt,
663 const char *format, __u16 *val)
669 sprintf(buf, format, *val);
670 printf("%30s [%s] ", prompt, buf);
671 fgets(buf, sizeof(buf), stdin);
672 if (buf[strlen (buf) - 1] == '\n')
673 buf[strlen (buf) - 1] = '\0';
676 v = strtoul(buf, &tmp, 0);
678 com_err(com, 0, "Bad value - %s", buf);
683 static void modify_u32(char *com, const char *prompt,
684 const char *format, __u32 *val)
690 sprintf(buf, format, *val);
691 printf("%30s [%s] ", prompt, buf);
692 fgets(buf, sizeof(buf), stdin);
693 if (buf[strlen (buf) - 1] == '\n')
694 buf[strlen (buf) - 1] = '\0';
697 v = strtoul(buf, &tmp, 0);
699 com_err(com, 0, "Bad value - %s", buf);
705 void do_modify_inode(int argc, char *argv[])
707 struct ext2_inode inode;
711 unsigned char *frag, *fsize;
713 int os = current_fs->super->s_creator_os;
714 const char *hex_format = "0x%x";
715 const char *octal_format = "0%o";
716 const char *decimal_format = "%d";
719 com_err(argv[0], 0, "Usage: modify_inode <file>");
722 if (check_fs_open(argv[0]))
724 if (check_fs_read_write(argv[0]))
727 inode_num = string_to_inode(argv[1]);
731 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
733 com_err(argv[1], retval, "while trying to read inode %d",
738 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
739 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
740 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
741 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
742 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
743 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
744 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
745 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
746 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
747 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
748 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
750 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
752 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
753 if (LINUX_S_ISDIR(inode.i_mode))
754 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
756 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
758 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
759 modify_u32(argv[0], "Translator Block",
760 decimal_format, &inode.osd1.hurd1.h_i_translator);
762 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
765 frag = &inode.osd2.linux2.l_i_frag;
766 fsize = &inode.osd2.linux2.l_i_fsize;
769 frag = &inode.osd2.hurd2.h_i_frag;
770 fsize = &inode.osd2.hurd2.h_i_fsize;
773 frag = &inode.osd2.masix2.m_i_frag;
774 fsize = &inode.osd2.masix2.m_i_fsize;
780 modify_u8(argv[0], "Fragment number", decimal_format, frag);
782 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
784 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
785 sprintf(buf, "Direct Block #%d", i);
786 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
788 modify_u32(argv[0], "Indirect Block", decimal_format,
789 &inode.i_block[EXT2_IND_BLOCK]);
790 modify_u32(argv[0], "Double Indirect Block", decimal_format,
791 &inode.i_block[EXT2_DIND_BLOCK]);
792 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
793 &inode.i_block[EXT2_TIND_BLOCK]);
794 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
796 com_err(argv[1], retval, "while trying to write inode %d",
802 void do_change_working_dir(int argc, char *argv[])
808 com_err(argv[0], 0, "Usage: cd <file>");
811 if (check_fs_open(argv[0]))
814 inode = string_to_inode(argv[1]);
818 retval = ext2fs_check_directory(current_fs, inode);
820 com_err(argv[1], retval, "");
827 void do_print_working_directory(int argc, char *argv[])
830 char *pathname = NULL;
833 com_err(argv[0], 0, "Usage: print_working_directory");
836 if (check_fs_open(argv[0]))
839 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
841 com_err(argv[0], retval,
842 "while trying to get pathname of cwd");
844 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
846 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
848 com_err(argv[0], retval,
849 "while trying to get pathname of root");
851 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
856 static void make_link(char *sourcename, char *destname)
861 char *dest, *cp, *basename;
864 * Get the source inode
866 inode = string_to_inode(sourcename);
869 basename = strrchr(sourcename, '/');
873 basename = sourcename;
875 * Figure out the destination. First see if it exists and is
878 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
882 * OK, it doesn't exist. See if it is
883 * '<dir>/basename' or 'basename'
885 cp = strrchr(destname, '/');
888 dir = string_to_inode(destname);
898 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
900 com_err("make_link", retval, "");
905 void do_link(int argc, char *argv[])
908 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
911 if (check_fs_open(argv[0]))
914 make_link(argv[1], argv[2]);
917 static void unlink_file_by_name(char *filename)
923 basename = strrchr(filename, '/');
926 dir = string_to_inode(filename);
933 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
935 com_err("unlink_file_by_name", retval, "");
939 void do_unlink(int argc, char *argv[])
942 com_err(argv[0], 0, "Usage: unlink <pathname>");
945 if (check_fs_open(argv[0]))
948 unlink_file_by_name(argv[1]);
951 void do_find_free_block(int argc, char *argv[])
953 blk_t free_blk, goal;
957 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
958 com_err(argv[0], 0, "Usage: find_free_block [goal]");
961 if (check_fs_open(argv[0]))
965 goal = strtol(argv[1], &tmp, 0);
967 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
972 goal = current_fs->super->s_first_data_block;
974 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
976 com_err("ext2fs_new_block", retval, "");
978 printf("Free block found: %d\n", free_blk);
982 void do_find_free_inode(int argc, char *argv[])
984 ino_t free_inode, dir;
989 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
990 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
993 if (check_fs_open(argv[0]))
997 dir = strtol(argv[1], &tmp, 0);
999 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1006 mode = strtol(argv[2], &tmp, 0);
1008 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1014 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1016 com_err("ext2fs_new_inode", retval, "");
1018 printf("Free inode found: %ld\n", free_inode);
1021 static errcode_t copy_file(int fd, ino_t newfile)
1023 ext2_file_t e2_file;
1025 unsigned int got, written;
1029 retval = ext2fs_file_open(current_fs, newfile,
1030 EXT2_FILE_WRITE, &e2_file);
1035 got = read(fd, buf, sizeof(buf));
1044 retval = ext2fs_file_write(e2_file, ptr,
1053 retval = ext2fs_file_close(e2_file);
1058 (void) ext2fs_file_close(e2_file);
1063 void do_write(int argc, char *argv[])
1066 struct stat statbuf;
1069 struct ext2_inode inode;
1072 if (check_fs_open(argv[0]))
1075 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1078 if (check_fs_read_write(argv[0]))
1080 fd = open(argv[1], O_RDONLY);
1082 com_err(argv[1], fd, "");
1085 if (fstat(fd, &statbuf) < 0) {
1086 com_err(argv[1], errno, "");
1091 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1093 com_err(argv[0], retval, "");
1097 group = ext2fs_group_of_ino(current_fs, newfile);
1098 current_fs->group_desc[group].bg_free_inodes_count--;
1099 current_fs->super->s_free_inodes_count--;
1100 ext2fs_mark_super_dirty(current_fs);
1101 printf("Allocated inode: %ld\n", newfile);
1102 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
1104 com_err(argv[2], retval, "");
1108 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1109 com_err(argv[0], 0, "Warning: inode already set");
1110 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1111 ext2fs_mark_ib_dirty(current_fs);
1112 memset(&inode, 0, sizeof(inode));
1113 inode.i_mode = statbuf.st_mode;
1114 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1115 inode.i_links_count = 1;
1116 inode.i_size = statbuf.st_size;
1117 ext2fs_write_inode(current_fs, newfile, &inode);
1118 retval = ext2fs_write_inode(current_fs, newfile, &inode);
1120 com_err(argv[0], retval, "while trying to write inode %d",
1125 if (LINUX_S_ISREG(inode.i_mode)) {
1126 retval = copy_file(fd, newfile);
1128 com_err("copy_file", retval, "");
1133 void do_mknod(int argc, char *argv[])
1135 unsigned long mode, major, minor, nr;
1138 struct ext2_inode inode;
1140 if (check_fs_open(argv[0]))
1142 if (argc < 3 || argv[2][1]) {
1143 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1146 mode = minor = major = 0;
1147 switch (argv[2][0]) {
1149 mode = LINUX_S_IFIFO;
1153 mode = LINUX_S_IFCHR;
1157 mode = LINUX_S_IFBLK;
1164 major = strtoul(argv[3], argv+3, 0);
1165 minor = strtoul(argv[4], argv+4, 0);
1166 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1170 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1173 if (check_fs_read_write(argv[0]))
1175 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1177 com_err(argv[0], retval, "");
1180 printf("Allocated inode: %ld\n", newfile);
1181 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
1183 if (retval == EXT2_ET_DIR_NO_SPACE) {
1184 retval = ext2fs_expand_dir(current_fs, cwd);
1186 retval = ext2fs_link(current_fs, cwd,
1187 argv[1], newfile, 0);
1190 com_err(argv[1], retval, "");
1194 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1195 com_err(argv[0], 0, "Warning: inode already set");
1196 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1197 ext2fs_mark_ib_dirty(current_fs);
1198 memset(&inode, 0, sizeof(inode));
1199 inode.i_mode = mode;
1200 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1201 inode.i_block[0] = major*256+minor;
1202 inode.i_links_count = 1;
1203 ext2fs_write_inode(current_fs, newfile, &inode);
1204 retval = ext2fs_write_inode(current_fs, newfile, &inode);
1206 com_err(argv[0], retval, "while trying to write inode %d", inode);
1211 void do_mkdir(int argc, char *argv[])
1218 if (check_fs_open(argv[0]))
1222 com_err(argv[0], 0, "Usage: mkdir <file>");
1226 if (check_fs_read_write(argv[0]))
1229 cp = strrchr(argv[1], '/');
1232 parent = string_to_inode(argv[1]);
1234 com_err(argv[1], ENOENT, "");
1244 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1246 com_err("ext2fs_mkdir", retval, "");
1252 void do_rmdir(int argc, char *argv[])
1254 printf("Unimplemented\n");
1258 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1259 int blockcnt, void *private)
1261 printf("%d ", *blocknr);
1262 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
1266 static void kill_file_by_inode(ino_t inode)
1268 struct ext2_inode inode_buf;
1270 ext2fs_read_inode(current_fs, inode, &inode_buf);
1271 inode_buf.i_dtime = time(NULL);
1272 ext2fs_write_inode(current_fs, inode, &inode_buf);
1274 printf("Kill file by inode %ld\n", inode);
1275 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1276 release_blocks_proc, NULL);
1278 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
1280 ext2fs_mark_bb_dirty(current_fs);
1281 ext2fs_mark_ib_dirty(current_fs);
1285 void do_kill_file(int argc, char *argv[])
1290 com_err(argv[0], 0, "Usage: kill_file <file>");
1293 if (check_fs_open(argv[0]))
1296 if (check_fs_read_write(argv[0]))
1299 inode_num = string_to_inode(argv[1]);
1301 com_err(argv[0], 0, "Cannot find file");
1304 kill_file_by_inode(inode_num);
1307 void do_rm(int argc, char *argv[])
1311 struct ext2_inode inode;
1314 com_err(argv[0], 0, "Usage: rm <filename>");
1317 if (check_fs_open(argv[0]))
1320 if (check_fs_read_write(argv[0]))
1323 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1325 com_err(argv[0], retval, "while trying to resolve filename");
1329 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
1331 com_err(argv[0], retval, "while reading file's inode");
1335 if (LINUX_S_ISDIR(inode.i_mode)) {
1336 com_err(argv[0], 0, "file is a directory");
1340 --inode.i_links_count;
1341 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
1343 com_err(argv[0], retval, "while writing inode");
1347 unlink_file_by_name(argv[1]);
1348 if (inode.i_links_count == 0)
1349 kill_file_by_inode(inode_num);
1352 void do_show_debugfs_params(int argc, char *argv[])
1357 fprintf(out, "Open mode: read-%s\n",
1358 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1359 fprintf(out, "Filesystem in use: %s\n",
1360 current_fs ? current_fs->device_name : "--none--");
1363 void do_expand_dir(int argc, char *argv[])
1369 com_err(argv[0], 0, "Usage: expand_dir <file>");
1372 if (check_fs_open(argv[0]))
1374 inode = string_to_inode(argv[1]);
1378 retval = ext2fs_expand_dir(current_fs, inode);
1380 com_err("ext2fs_expand_dir", retval, "");
1384 void do_features(int argc, char *argv[])
1388 if (check_fs_open(argv[0]))
1391 if ((argc != 1) && check_fs_read_write(argv[0]))
1393 for (i=1; i < argc; i++) {
1394 if (e2p_edit_feature(argv[i],
1395 ¤t_fs->super->s_feature_compat, 0))
1396 com_err(argv[0], 0, "Unknown feature: %s\n",
1399 ext2fs_mark_super_dirty(current_fs);
1401 print_features((struct ext2fs_sb *) current_fs->super, stdout);
1404 static int source_file(const char *cmd_file, int sci_idx)
1409 int exit_status = 0;
1412 if (strcmp(cmd_file, "-") == 0)
1415 f = fopen(cmd_file, "r");
1421 setbuf(stdout, NULL);
1422 setbuf(stderr, NULL);
1424 if (fgets(buf, sizeof(buf), f) == NULL)
1426 cp = strchr(buf, '\n');
1429 cp = strchr(buf, '\r');
1432 printf("debugfs: %s\n", buf);
1433 retval = ss_execute_line(sci_idx, buf);
1435 ss_perror(sci_idx, retval, buf);
1442 int main(int argc, char **argv)
1446 const char *usage = "Usage: debugfs [-f cmd_file] [-R request] [-V] [[-w] device]";
1450 int exit_status = 0;
1453 initialize_ext2_error_table();
1454 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1455 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1456 EXT2FS_VERSION, EXT2FS_DATE);
1458 while ((c = getopt (argc, argv, "wR:f:V")) != EOF) {
1467 open_flags = EXT2_FLAG_RW;
1470 /* Print version number and exit */
1471 fprintf(stderr, "\tUsing %s\n",
1472 error_message(EXT2_ET_BASE));
1475 com_err(argv[0], 0, usage);
1480 open_filesystem(argv[optind], open_flags);
1482 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1483 &debug_cmds, &retval);
1485 ss_perror(sci_idx, retval, "creating invocation");
1489 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1491 ss_perror(sci_idx, retval, "adding standard requests");
1496 retval = ss_execute_line(sci_idx, request);
1498 ss_perror(sci_idx, retval, request);
1501 } else if (cmd_file) {
1502 exit_status = source_file(cmd_file, sci_idx);