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"
38 #include "../version.h"
40 extern ss_request_table debug_cmds;
42 ext2_filsys current_fs = NULL;
45 static void open_filesystem(char *device, int open_flags)
49 retval = ext2fs_open(device, open_flags, 0, 0,
50 unix_io_manager, ¤t_fs);
52 com_err(device, retval, "while opening filesystem");
56 retval = ext2fs_read_inode_bitmap(current_fs);
58 com_err(device, retval, "while reading inode bitmap");
61 retval = ext2fs_read_block_bitmap(current_fs);
63 com_err(device, retval, "while reading block bitmap");
66 root = cwd = EXT2_ROOT_INO;
70 retval = ext2fs_close(current_fs);
72 com_err(device, retval, "while trying to close filesystem");
76 void do_open_filesys(int argc, char **argv)
78 const char *usage = "Usage: open [-w] <device>";
84 optreset = 1; /* Makes BSD getopt happy */
86 while ((c = getopt (argc, argv, "w")) != EOF) {
89 open_flags = EXT2_FLAG_RW;
92 com_err(argv[0], 0, usage);
96 if (optind != argc-1) {
97 com_err(argv[0], 0, usage);
100 if (check_fs_not_open(argv[0]))
102 open_filesystem(argv[optind], open_flags);
105 static void close_filesystem(NOARGS)
109 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
110 retval = ext2fs_write_inode_bitmap(current_fs);
112 com_err("ext2fs_write_inode_bitmap", retval, "");
114 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
115 retval = ext2fs_write_block_bitmap(current_fs);
117 com_err("ext2fs_write_block_bitmap", retval, "");
119 retval = ext2fs_close(current_fs);
121 com_err("ext2fs_close", retval, "");
126 void do_close_filesys(int argc, char **argv)
129 com_err(argv[0], 0, "Usage: close_filesys");
132 if (check_fs_open(argv[0]))
137 void do_init_filesys(int argc, char **argv)
139 const char *usage = "Usage: initialize <device> <blocksize>";
140 struct ext2_super_block param;
145 com_err(argv[0], 0, usage);
148 if (check_fs_not_open(argv[0]))
151 memset(¶m, 0, sizeof(struct ext2_super_block));
152 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
154 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
157 retval = ext2fs_initialize(argv[1], 0, ¶m,
158 unix_io_manager, ¤t_fs);
160 com_err(argv[1], retval, "while initializing filesystem");
164 root = cwd = EXT2_ROOT_INO;
168 void do_show_super_stats(int argc, char *argv[])
172 struct ext2fs_sb *sb;
173 struct ext2_group_desc *gdp;
175 const char *none = "(none)";
178 com_err(argv[0], 0, "Usage: show_super");
181 if (check_fs_open(argv[0]))
184 sb = (struct ext2fs_sb *) current_fs->super;
185 fprintf(out, "Filesystem is read-%s\n",
186 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
187 if (sb->s_volume_name[0]) {
188 memset(buf, 0, sizeof(buf));
189 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
192 fprintf(out, "Volume name = %s\n", buf);
193 if (sb->s_last_mounted[0]) {
194 memset(buf, 0, sizeof(buf));
195 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
198 fprintf(out, "Last mounted directory = %s\n", buf);
199 if (!uuid_is_null(sb->s_uuid))
200 uuid_unparse(sb->s_uuid, buf);
203 fprintf(out, "Filesystem UUID = %s\n", buf);
204 fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
205 fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
206 fprintf(out, "Mount counts = %d (maximal = %d)\n",
207 sb->s_mnt_count, sb->s_max_mnt_count);
208 fputs ("Filesystem OS type = ", out);
209 switch (sb->s_creator_os) {
210 case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
211 case EXT2_OS_HURD: fputs ("GNU\n", out); break;
212 case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
213 default: fputs ("unknown\n", out);
215 fprintf(out, "Superblock size = %d\n",
216 sizeof(struct ext2_super_block));
217 fprintf(out, "Block size = %d, fragment size = %d\n",
218 EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
219 fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
220 fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
221 sb->s_free_inodes_count);
222 fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
223 sb->s_blocks_count, sb->s_free_blocks_count,
224 sb->s_r_blocks_count, sb->s_first_data_block);
225 fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
226 fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
227 fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
228 fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
229 current_fs->group_desc_count,
230 (current_fs->group_desc_count != 1) ? "s" : "",
231 current_fs->desc_blocks,
232 (current_fs->desc_blocks != 1) ? "s" : "");
234 gdp = ¤t_fs->group_desc[0];
235 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
236 fprintf(out, " Group %2d: block bitmap at %d, "
237 "inode bitmap at %d, "
238 "inode table at %d\n"
241 "%d used director%s\n",
242 i, gdp->bg_block_bitmap,
243 gdp->bg_inode_bitmap, gdp->bg_inode_table,
244 gdp->bg_free_blocks_count,
245 gdp->bg_free_blocks_count != 1 ? "s" : "",
246 gdp->bg_free_inodes_count,
247 gdp->bg_free_inodes_count != 1 ? "s" : "",
248 gdp->bg_used_dirs_count,
249 gdp->bg_used_dirs_count != 1 ? "ies" : "y");
253 void do_dirty_filesys(int argc, char **argv)
255 if (check_fs_open(argv[0]))
258 ext2fs_mark_super_dirty(current_fs);
261 struct list_blocks_struct {
266 static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
269 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
271 fprintf(lb->f, "%d ", *blocknr);
277 static void dump_blocks(FILE *f, ino_t inode)
279 struct list_blocks_struct lb;
281 fprintf(f, "BLOCKS:\n");
284 ext2fs_block_iterate(current_fs, inode, 0, NULL,
285 list_blocks_proc, (void *)&lb);
287 fprintf(f, "\nTOTAL: %d\n", lb.total);
292 static void dump_inode(ino_t inode_num, struct ext2_inode inode)
297 int os = current_fs->super->s_creator_os;
300 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
301 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
302 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
303 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
304 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
305 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
306 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
307 else i_type = "bad type";
308 fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type);
309 fprintf(out, "Mode: %04o Flags: 0x%x Version: %d\n",
310 inode.i_mode & 0777, inode.i_flags, inode.i_version);
311 fprintf(out, "User: %5d Group: %5d Size: ",
312 inode.i_uid, inode.i_gid);
313 if (LINUX_S_ISDIR(inode.i_mode))
314 fprintf(out, "%d\n", inode.i_size);
316 __u64 i_size = (inode.i_size |
317 ((unsigned long long)inode.i_size_high << 32));
319 fprintf(out, "%lld\n", i_size);
321 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
323 "File ACL: %d Directory ACL: %d Translator: %d\n",
324 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
325 inode.osd1.hurd1.h_i_translator);
327 fprintf(out, "File ACL: %d Directory ACL: %d\n",
328 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
329 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
333 frag = inode.osd2.linux2.l_i_frag;
334 fsize = inode.osd2.linux2.l_i_fsize;
337 frag = inode.osd2.hurd2.h_i_frag;
338 fsize = inode.osd2.hurd2.h_i_fsize;
341 frag = inode.osd2.masix2.m_i_frag;
342 fsize = inode.osd2.masix2.m_i_fsize;
347 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
348 inode.i_faddr, frag, fsize);
349 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
350 time_to_string(inode.i_ctime));
351 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
352 time_to_string(inode.i_atime));
353 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
354 time_to_string(inode.i_mtime));
356 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
357 time_to_string(inode.i_dtime));
358 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
359 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
361 dump_blocks(out, inode_num);
366 void do_stat(int argc, char *argv[])
369 struct ext2_inode inode_buf;
373 com_err(argv[0], 0, "Usage: stat <file>");
376 if (check_fs_open(argv[0]))
378 inode = string_to_inode(argv[1]);
382 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
385 com_err(argv[0], 0, "Reading inode");
389 dump_inode(inode,inode_buf);
393 void do_chroot(int argc, char *argv[])
399 com_err(argv[0], 0, "Usage: chroot <file>");
402 if (check_fs_open(argv[0]))
404 inode = string_to_inode(argv[1]);
408 retval = ext2fs_check_directory(current_fs, inode);
410 com_err(argv[1], retval, "");
416 void do_clri(int argc, char *argv[])
420 struct ext2_inode inode_buf;
423 com_err(argv[0], 0, "Usage: clri <file>");
426 if (check_fs_open(argv[0]))
428 if (check_fs_read_write(argv[0]))
430 inode = string_to_inode(argv[1]);
434 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
436 com_err(argv[0], 0, "while trying to read inode %d", inode);
439 memset(&inode_buf, 0, sizeof(inode_buf));
440 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
442 com_err(argv[0], retval, "while trying to write inode %d",
448 void do_freei(int argc, char *argv[])
453 com_err(argv[0], 0, "Usage: freei <file>");
456 if (check_fs_open(argv[0]))
458 if (check_fs_read_write(argv[0]))
460 inode = string_to_inode(argv[1]);
464 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
465 com_err(argv[0], 0, "Warning: inode already clear");
466 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
467 ext2fs_mark_ib_dirty(current_fs);
470 void do_seti(int argc, char *argv[])
475 com_err(argv[0], 0, "Usage: seti <file>");
478 if (check_fs_open(argv[0]))
480 if (check_fs_read_write(argv[0]))
482 inode = string_to_inode(argv[1]);
486 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
487 com_err(argv[0], 0, "Warning: inode already set");
488 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
489 ext2fs_mark_ib_dirty(current_fs);
492 void do_testi(int argc, char *argv[])
497 com_err(argv[0], 0, "Usage: testi <file>");
500 if (check_fs_open(argv[0]))
502 inode = string_to_inode(argv[1]);
506 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
507 printf("Inode %ld is marked in use\n", inode);
509 printf("Inode %ld is not in use\n", inode);
513 void do_freeb(int argc, char *argv[])
519 com_err(argv[0], 0, "Usage: freeb <block>");
522 if (check_fs_open(argv[0]))
524 if (check_fs_read_write(argv[0]))
526 block = strtoul(argv[1], &tmp, 0);
527 if (!block || *tmp) {
528 com_err(argv[0], 0, "No block 0");
531 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
532 com_err(argv[0], 0, "Warning: block already clear");
533 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
534 ext2fs_mark_bb_dirty(current_fs);
537 void do_setb(int argc, char *argv[])
543 com_err(argv[0], 0, "Usage: setb <block>");
546 if (check_fs_open(argv[0]))
548 if (check_fs_read_write(argv[0]))
550 block = strtoul(argv[1], &tmp, 0);
551 if (!block || *tmp) {
552 com_err(argv[0], 0, "No block 0");
555 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
556 com_err(argv[0], 0, "Warning: block already set");
557 ext2fs_mark_block_bitmap(current_fs->block_map,block);
558 ext2fs_mark_bb_dirty(current_fs);
561 void do_testb(int argc, char *argv[])
567 com_err(argv[0], 0, "Usage: testb <block>");
570 if (check_fs_open(argv[0]))
572 block = strtoul(argv[1], &tmp, 0);
573 if (!block || *tmp) {
574 com_err(argv[0], 0, "No block 0");
577 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
578 printf("Block %d marked in use\n", block);
579 else printf("Block %d not in use\n", block);
582 static void modify_u8(char *com, const char *prompt,
583 const char *format, __u8 *val)
589 sprintf(buf, format, *val);
590 printf("%30s [%s] ", prompt, buf);
591 fgets(buf, sizeof(buf), stdin);
592 if (buf[strlen (buf) - 1] == '\n')
593 buf[strlen (buf) - 1] = '\0';
596 v = strtoul(buf, &tmp, 0);
598 com_err(com, 0, "Bad value - %s", buf);
603 static void modify_u16(char *com, const char *prompt,
604 const char *format, __u16 *val)
610 sprintf(buf, format, *val);
611 printf("%30s [%s] ", prompt, buf);
612 fgets(buf, sizeof(buf), stdin);
613 if (buf[strlen (buf) - 1] == '\n')
614 buf[strlen (buf) - 1] = '\0';
617 v = strtoul(buf, &tmp, 0);
619 com_err(com, 0, "Bad value - %s", buf);
624 static void modify_u32(char *com, const char *prompt,
625 const char *format, __u32 *val)
631 sprintf(buf, format, *val);
632 printf("%30s [%s] ", prompt, buf);
633 fgets(buf, sizeof(buf), stdin);
634 if (buf[strlen (buf) - 1] == '\n')
635 buf[strlen (buf) - 1] = '\0';
638 v = strtoul(buf, &tmp, 0);
640 com_err(com, 0, "Bad value - %s", buf);
646 void do_modify_inode(int argc, char *argv[])
648 struct ext2_inode inode;
652 unsigned char *frag, *fsize;
654 int os = current_fs->super->s_creator_os;
655 const char *hex_format = "0x%x";
656 const char *octal_format = "0%o";
657 const char *decimal_format = "%d";
660 com_err(argv[0], 0, "Usage: modify_inode <file>");
663 if (check_fs_open(argv[0]))
665 if (check_fs_read_write(argv[0]))
668 inode_num = string_to_inode(argv[1]);
672 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
674 com_err(argv[1], retval, "while trying to read inode %d",
679 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
680 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
681 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
682 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
683 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
684 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
685 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
686 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
687 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
688 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
689 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
691 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
693 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
694 if (LINUX_S_ISDIR(inode.i_mode))
695 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
697 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
699 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
700 modify_u32(argv[0], "Translator Block",
701 decimal_format, &inode.osd1.hurd1.h_i_translator);
703 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
706 frag = &inode.osd2.linux2.l_i_frag;
707 fsize = &inode.osd2.linux2.l_i_fsize;
710 frag = &inode.osd2.hurd2.h_i_frag;
711 fsize = &inode.osd2.hurd2.h_i_fsize;
714 frag = &inode.osd2.masix2.m_i_frag;
715 fsize = &inode.osd2.masix2.m_i_fsize;
721 modify_u8(argv[0], "Fragment number", decimal_format, frag);
723 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
725 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
726 sprintf(buf, "Direct Block #%d", i);
727 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
729 modify_u32(argv[0], "Indirect Block", decimal_format,
730 &inode.i_block[EXT2_IND_BLOCK]);
731 modify_u32(argv[0], "Double Indirect Block", decimal_format,
732 &inode.i_block[EXT2_DIND_BLOCK]);
733 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
734 &inode.i_block[EXT2_TIND_BLOCK]);
735 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
737 com_err(argv[1], retval, "while trying to write inode %d",
743 void do_change_working_dir(int argc, char *argv[])
749 com_err(argv[0], 0, "Usage: cd <file>");
752 if (check_fs_open(argv[0]))
755 inode = string_to_inode(argv[1]);
759 retval = ext2fs_check_directory(current_fs, inode);
761 com_err(argv[1], retval, "");
768 void do_print_working_directory(int argc, char *argv[])
771 char *pathname = NULL;
774 com_err(argv[0], 0, "Usage: print_working_directory");
777 if (check_fs_open(argv[0]))
780 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
782 com_err(argv[0], retval,
783 "while trying to get pathname of cwd");
785 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
787 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
789 com_err(argv[0], retval,
790 "while trying to get pathname of root");
792 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
797 static void make_link(char *sourcename, char *destname)
802 char *dest, *cp, *basename;
805 * Get the source inode
807 inode = string_to_inode(sourcename);
810 basename = strrchr(sourcename, '/');
814 basename = sourcename;
816 * Figure out the destination. First see if it exists and is
819 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
823 * OK, it doesn't exist. See if it is
824 * '<dir>/basename' or 'basename'
826 cp = strrchr(destname, '/');
829 dir = string_to_inode(destname);
839 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
841 com_err("make_link", retval, "");
846 void do_link(int argc, char *argv[])
849 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
852 if (check_fs_open(argv[0]))
855 make_link(argv[1], argv[2]);
858 static void unlink_file_by_name(char *filename)
864 basename = strrchr(filename, '/');
867 dir = string_to_inode(filename);
874 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
876 com_err("unlink_file_by_name", retval, "");
880 void do_unlink(int argc, char *argv[])
883 com_err(argv[0], 0, "Usage: unlink <pathname>");
886 if (check_fs_open(argv[0]))
889 unlink_file_by_name(argv[1]);
892 void do_find_free_block(int argc, char *argv[])
894 blk_t free_blk, goal;
898 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
899 com_err(argv[0], 0, "Usage: find_free_block [goal]");
902 if (check_fs_open(argv[0]))
906 goal = strtol(argv[1], &tmp, 0);
908 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
913 goal = current_fs->super->s_first_data_block;
915 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
917 com_err("ext2fs_new_block", retval, "");
919 printf("Free block found: %d\n", free_blk);
923 void do_find_free_inode(int argc, char *argv[])
925 ino_t free_inode, dir;
930 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
931 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
934 if (check_fs_open(argv[0]))
938 dir = strtol(argv[1], &tmp, 0);
940 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
947 mode = strtol(argv[2], &tmp, 0);
949 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
955 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
957 com_err("ext2fs_new_inode", retval, "");
959 printf("Free inode found: %ld\n", free_inode);
962 static errcode_t copy_file(int fd, ino_t newfile)
966 unsigned int got, written;
970 retval = ext2fs_file_open(current_fs, newfile,
971 EXT2_FILE_WRITE, &e2_file);
976 got = read(fd, buf, sizeof(buf));
985 retval = ext2fs_file_write(e2_file, ptr,
994 retval = ext2fs_file_close(e2_file);
999 (void) ext2fs_file_close(e2_file);
1004 void do_write(int argc, char *argv[])
1007 struct stat statbuf;
1010 struct ext2_inode inode;
1013 if (check_fs_open(argv[0]))
1016 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1019 if (check_fs_read_write(argv[0]))
1021 fd = open(argv[1], O_RDONLY);
1023 com_err(argv[1], fd, "");
1026 if (fstat(fd, &statbuf) < 0) {
1027 com_err(argv[1], errno, "");
1032 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1034 com_err(argv[0], retval, "");
1038 group = ext2fs_group_of_ino(current_fs, newfile);
1039 current_fs->group_desc[group].bg_free_inodes_count--;
1040 current_fs->super->s_free_inodes_count--;
1041 ext2fs_mark_super_dirty(current_fs);
1042 printf("Allocated inode: %ld\n", newfile);
1043 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
1045 com_err(argv[2], retval, "");
1049 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1050 com_err(argv[0], 0, "Warning: inode already set");
1051 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1052 ext2fs_mark_ib_dirty(current_fs);
1053 memset(&inode, 0, sizeof(inode));
1054 inode.i_mode = statbuf.st_mode;
1055 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1056 inode.i_links_count = 1;
1057 inode.i_size = statbuf.st_size;
1058 ext2fs_write_inode(current_fs, newfile, &inode);
1059 retval = ext2fs_write_inode(current_fs, newfile, &inode);
1061 com_err(argv[0], retval, "while trying to write inode %d",
1066 if (LINUX_S_ISREG(inode.i_mode)) {
1067 retval = copy_file(fd, newfile);
1069 com_err("copy_file", retval, "");
1074 void do_mknod(int argc, char *argv[])
1076 unsigned long mode, major, minor, nr;
1079 struct ext2_inode inode;
1081 if (check_fs_open(argv[0]))
1083 if (argc < 3 || argv[2][1]) {
1084 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1087 mode = minor = major = 0;
1088 switch (argv[2][0]) {
1090 mode = LINUX_S_IFIFO;
1094 mode = LINUX_S_IFCHR;
1098 mode = LINUX_S_IFBLK;
1105 major = strtoul(argv[3], argv+3, 0);
1106 minor = strtoul(argv[4], argv+4, 0);
1107 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1111 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1114 if (check_fs_read_write(argv[0]))
1116 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1118 com_err(argv[0], retval, "");
1121 printf("Allocated inode: %ld\n", newfile);
1122 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
1124 if (retval == EXT2_ET_DIR_NO_SPACE) {
1125 retval = ext2fs_expand_dir(current_fs, cwd);
1127 retval = ext2fs_link(current_fs, cwd,
1128 argv[1], newfile, 0);
1131 com_err(argv[1], retval, "");
1135 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1136 com_err(argv[0], 0, "Warning: inode already set");
1137 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1138 ext2fs_mark_ib_dirty(current_fs);
1139 memset(&inode, 0, sizeof(inode));
1140 inode.i_mode = mode;
1141 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1142 inode.i_block[0] = major*256+minor;
1143 inode.i_links_count = 1;
1144 ext2fs_write_inode(current_fs, newfile, &inode);
1145 retval = ext2fs_write_inode(current_fs, newfile, &inode);
1147 com_err(argv[0], retval, "while trying to write inode %d", inode);
1152 void do_mkdir(int argc, char *argv[])
1159 if (check_fs_open(argv[0]))
1163 com_err(argv[0], 0, "Usage: mkdir <file>");
1167 cp = strrchr(argv[1], '/');
1170 parent = string_to_inode(argv[1]);
1172 com_err(argv[1], ENOENT, "");
1182 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1184 com_err("ext2fs_mkdir", retval, "");
1190 void do_rmdir(int argc, char *argv[])
1192 printf("Unimplemented\n");
1196 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1197 int blockcnt, void *private)
1199 printf("%d ", *blocknr);
1200 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
1204 static void kill_file_by_inode(ino_t inode)
1206 struct ext2_inode inode_buf;
1208 ext2fs_read_inode(current_fs, inode, &inode_buf);
1209 inode_buf.i_dtime = time(NULL);
1210 ext2fs_write_inode(current_fs, inode, &inode_buf);
1212 printf("Kill file by inode %ld\n", inode);
1213 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1214 release_blocks_proc, NULL);
1216 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
1218 ext2fs_mark_bb_dirty(current_fs);
1219 ext2fs_mark_ib_dirty(current_fs);
1223 void do_kill_file(int argc, char *argv[])
1228 com_err(argv[0], 0, "Usage: kill_file <file>");
1231 if (check_fs_open(argv[0]))
1234 inode_num = string_to_inode(argv[1]);
1236 com_err(argv[0], 0, "Cannot find file");
1239 kill_file_by_inode(inode_num);
1242 void do_rm(int argc, char *argv[])
1246 struct ext2_inode inode;
1249 com_err(argv[0], 0, "Usage: rm <filename>");
1252 if (check_fs_open(argv[0]))
1255 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1257 com_err(argv[0], 0, "Cannot find file");
1261 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
1263 com_err(argv[0], retval, "while reading file's inode");
1267 if (LINUX_S_ISDIR(inode.i_mode)) {
1268 com_err(argv[0], 0, "file is a directory");
1272 --inode.i_links_count;
1273 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
1275 com_err(argv[0], retval, "while writing inode");
1279 unlink_file_by_name(argv[1]);
1280 if (inode.i_links_count == 0)
1281 kill_file_by_inode(inode_num);
1284 void do_show_debugfs_params(int argc, char *argv[])
1289 fprintf(out, "Open mode: read-%s\n",
1290 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1291 fprintf(out, "Filesystem in use: %s\n",
1292 current_fs ? current_fs->device_name : "--none--");
1295 void do_expand_dir(int argc, char *argv[])
1301 com_err(argv[0], 0, "Usage: expand_dir <file>");
1304 if (check_fs_open(argv[0]))
1306 inode = string_to_inode(argv[1]);
1310 retval = ext2fs_expand_dir(current_fs, inode);
1312 com_err("ext2fs_expand_dir", retval, "");
1316 static int source_file(const char *cmd_file, int sci_idx)
1321 int exit_status = 0;
1324 if (strcmp(cmd_file, "-") == 0)
1327 f = fopen(cmd_file, "r");
1333 setbuf(stdout, NULL);
1334 setbuf(stderr, NULL);
1336 if (fgets(buf, sizeof(buf), f) == NULL)
1338 cp = strchr(buf, '\n');
1341 cp = strchr(buf, '\r');
1344 printf("debugfs: %s\n", buf);
1345 retval = ss_execute_line(sci_idx, buf);
1347 ss_perror(sci_idx, retval, buf);
1354 int main(int argc, char **argv)
1358 const char *usage = "Usage: debugfs [-f cmd_file] [-R request] [-V] [[-w] device]";
1362 int exit_status = 0;
1365 initialize_ext2_error_table();
1366 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1367 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1368 EXT2FS_VERSION, EXT2FS_DATE);
1370 while ((c = getopt (argc, argv, "wR:f:V")) != EOF) {
1379 open_flags = EXT2_FLAG_RW;
1382 /* Print version number and exit */
1383 fprintf(stderr, "\tUsing %s\n",
1384 error_message(EXT2_ET_BASE));
1387 com_err(argv[0], 0, usage);
1392 open_filesystem(argv[optind], open_flags);
1394 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1395 &debug_cmds, &retval);
1397 ss_perror(sci_idx, retval, "creating invocation");
1401 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1403 ss_perror(sci_idx, retval, "adding standard requests");
1408 retval = ss_execute_line(sci_idx, request);
1410 ss_perror(sci_idx, retval, request);
1413 } else if (cmd_file) {
1414 exit_status = source_file(cmd_file, sci_idx);