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 #if defined(HAVE_EXT2_INODE_VERSION)
310 fprintf(out
, "Mode: %04o Flags: 0x%x Version/Generation: %d\n",
311 inode
.i_mode
& 0777, inode
.i_flags
, inode
.i_version
);
313 fprintf(out
, "Mode: %04o Flags: 0x%x Version/Generation: %d\n",
314 inode
.i_mode
& 0777, inode
.i_flags
, inode
.i_generation
);
316 fprintf(out
, "User: %5d Group: %5d Size: ",
317 inode
.i_uid
, inode
.i_gid
);
318 if (LINUX_S_ISDIR(inode
.i_mode
))
319 fprintf(out
, "%d\n", inode
.i_size
);
321 __u64 i_size
= (inode
.i_size
|
322 ((unsigned long long)inode
.i_size_high
<< 32));
324 fprintf(out
, "%lld\n", i_size
);
326 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
328 "File ACL: %d Directory ACL: %d Translator: %d\n",
329 inode
.i_file_acl
, LINUX_S_ISDIR(inode
.i_mode
) ? inode
.i_dir_acl
: 0,
330 inode
.osd1
.hurd1
.h_i_translator
);
332 fprintf(out
, "File ACL: %d Directory ACL: %d\n",
333 inode
.i_file_acl
, LINUX_S_ISDIR(inode
.i_mode
) ? inode
.i_dir_acl
: 0);
334 fprintf(out
, "Links: %d Blockcount: %d\n", inode
.i_links_count
,
338 frag
= inode
.osd2
.linux2
.l_i_frag
;
339 fsize
= inode
.osd2
.linux2
.l_i_fsize
;
342 frag
= inode
.osd2
.hurd2
.h_i_frag
;
343 fsize
= inode
.osd2
.hurd2
.h_i_fsize
;
346 frag
= inode
.osd2
.masix2
.m_i_frag
;
347 fsize
= inode
.osd2
.masix2
.m_i_fsize
;
352 fprintf(out
, "Fragment: Address: %d Number: %d Size: %d\n",
353 inode
.i_faddr
, frag
, fsize
);
354 fprintf(out
, "ctime: 0x%08x -- %s", inode
.i_ctime
,
355 time_to_string(inode
.i_ctime
));
356 fprintf(out
, "atime: 0x%08x -- %s", inode
.i_atime
,
357 time_to_string(inode
.i_atime
));
358 fprintf(out
, "mtime: 0x%08x -- %s", inode
.i_mtime
,
359 time_to_string(inode
.i_mtime
));
361 fprintf(out
, "dtime: 0x%08x -- %s", inode
.i_dtime
,
362 time_to_string(inode
.i_dtime
));
363 if (LINUX_S_ISLNK(inode
.i_mode
) && inode
.i_blocks
== 0)
364 fprintf(out
, "Fast_link_dest: %s\n", (char *)inode
.i_block
);
366 dump_blocks(out
, inode_num
);
371 void do_stat(int argc
, char *argv
[])
374 struct ext2_inode inode_buf
;
378 com_err(argv
[0], 0, "Usage: stat <file>");
381 if (check_fs_open(argv
[0]))
383 inode
= string_to_inode(argv
[1]);
387 retval
= ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
390 com_err(argv
[0], retval
, "while trying to read inode %d", inode
);
394 dump_inode(inode
,inode_buf
);
398 void do_chroot(int argc
, char *argv
[])
404 com_err(argv
[0], 0, "Usage: chroot <file>");
407 if (check_fs_open(argv
[0]))
409 inode
= string_to_inode(argv
[1]);
413 retval
= ext2fs_check_directory(current_fs
, inode
);
415 com_err(argv
[1], retval
, "");
421 void do_clri(int argc
, char *argv
[])
425 struct ext2_inode inode_buf
;
428 com_err(argv
[0], 0, "Usage: clri <file>");
431 if (check_fs_open(argv
[0]))
433 if (check_fs_read_write(argv
[0]))
435 inode
= string_to_inode(argv
[1]);
439 retval
= ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
441 com_err(argv
[0], retval
, "while trying to read inode %d",
445 memset(&inode_buf
, 0, sizeof(inode_buf
));
446 retval
= ext2fs_write_inode(current_fs
, inode
, &inode_buf
);
448 com_err(argv
[0], retval
, "while trying to write inode %d",
454 void do_freei(int argc
, char *argv
[])
459 com_err(argv
[0], 0, "Usage: freei <file>");
462 if (check_fs_open(argv
[0]))
464 if (check_fs_read_write(argv
[0]))
466 inode
= string_to_inode(argv
[1]);
470 if (!ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
471 com_err(argv
[0], 0, "Warning: inode already clear");
472 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
,inode
);
473 ext2fs_mark_ib_dirty(current_fs
);
476 void do_seti(int argc
, char *argv
[])
481 com_err(argv
[0], 0, "Usage: seti <file>");
484 if (check_fs_open(argv
[0]))
486 if (check_fs_read_write(argv
[0]))
488 inode
= string_to_inode(argv
[1]);
492 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
493 com_err(argv
[0], 0, "Warning: inode already set");
494 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,inode
);
495 ext2fs_mark_ib_dirty(current_fs
);
498 void do_testi(int argc
, char *argv
[])
503 com_err(argv
[0], 0, "Usage: testi <file>");
506 if (check_fs_open(argv
[0]))
508 inode
= string_to_inode(argv
[1]);
512 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
513 printf("Inode %ld is marked in use\n", inode
);
515 printf("Inode %ld is not in use\n", inode
);
519 void do_freeb(int argc
, char *argv
[])
525 com_err(argv
[0], 0, "Usage: freeb <block>");
528 if (check_fs_open(argv
[0]))
530 if (check_fs_read_write(argv
[0]))
532 block
= strtoul(argv
[1], &tmp
, 0);
533 if (!block
|| *tmp
) {
534 com_err(argv
[0], 0, "No block 0");
537 if (!ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
538 com_err(argv
[0], 0, "Warning: block already clear");
539 ext2fs_unmark_block_bitmap(current_fs
->block_map
,block
);
540 ext2fs_mark_bb_dirty(current_fs
);
543 void do_setb(int argc
, char *argv
[])
549 com_err(argv
[0], 0, "Usage: setb <block>");
552 if (check_fs_open(argv
[0]))
554 if (check_fs_read_write(argv
[0]))
556 block
= strtoul(argv
[1], &tmp
, 0);
557 if (!block
|| *tmp
) {
558 com_err(argv
[0], 0, "No block 0");
561 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
562 com_err(argv
[0], 0, "Warning: block already set");
563 ext2fs_mark_block_bitmap(current_fs
->block_map
,block
);
564 ext2fs_mark_bb_dirty(current_fs
);
567 void do_testb(int argc
, char *argv
[])
573 com_err(argv
[0], 0, "Usage: testb <block>");
576 if (check_fs_open(argv
[0]))
578 block
= strtoul(argv
[1], &tmp
, 0);
579 if (!block
|| *tmp
) {
580 com_err(argv
[0], 0, "No block 0");
583 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
584 printf("Block %d marked in use\n", block
);
585 else printf("Block %d not in use\n", block
);
588 static void modify_u8(char *com
, const char *prompt
,
589 const char *format
, __u8
*val
)
595 sprintf(buf
, format
, *val
);
596 printf("%30s [%s] ", prompt
, buf
);
597 fgets(buf
, sizeof(buf
), stdin
);
598 if (buf
[strlen (buf
) - 1] == '\n')
599 buf
[strlen (buf
) - 1] = '\0';
602 v
= strtoul(buf
, &tmp
, 0);
604 com_err(com
, 0, "Bad value - %s", buf
);
609 static void modify_u16(char *com
, const char *prompt
,
610 const char *format
, __u16
*val
)
616 sprintf(buf
, format
, *val
);
617 printf("%30s [%s] ", prompt
, buf
);
618 fgets(buf
, sizeof(buf
), stdin
);
619 if (buf
[strlen (buf
) - 1] == '\n')
620 buf
[strlen (buf
) - 1] = '\0';
623 v
= strtoul(buf
, &tmp
, 0);
625 com_err(com
, 0, "Bad value - %s", buf
);
630 static void modify_u32(char *com
, const char *prompt
,
631 const char *format
, __u32
*val
)
637 sprintf(buf
, format
, *val
);
638 printf("%30s [%s] ", prompt
, buf
);
639 fgets(buf
, sizeof(buf
), stdin
);
640 if (buf
[strlen (buf
) - 1] == '\n')
641 buf
[strlen (buf
) - 1] = '\0';
644 v
= strtoul(buf
, &tmp
, 0);
646 com_err(com
, 0, "Bad value - %s", buf
);
652 void do_modify_inode(int argc
, char *argv
[])
654 struct ext2_inode inode
;
658 unsigned char *frag
, *fsize
;
660 int os
= current_fs
->super
->s_creator_os
;
661 const char *hex_format
= "0x%x";
662 const char *octal_format
= "0%o";
663 const char *decimal_format
= "%d";
666 com_err(argv
[0], 0, "Usage: modify_inode <file>");
669 if (check_fs_open(argv
[0]))
671 if (check_fs_read_write(argv
[0]))
674 inode_num
= string_to_inode(argv
[1]);
678 retval
= ext2fs_read_inode(current_fs
, inode_num
, &inode
);
680 com_err(argv
[1], retval
, "while trying to read inode %d",
685 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
686 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
687 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
688 modify_u32(argv
[0], "Size", decimal_format
, &inode
.i_size
);
689 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
690 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
691 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
692 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
693 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
694 modify_u32(argv
[0], "Block count", decimal_format
, &inode
.i_blocks
);
695 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
697 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
699 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
700 if (LINUX_S_ISDIR(inode
.i_mode
))
701 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
703 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
705 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
706 modify_u32(argv
[0], "Translator Block",
707 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
709 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
712 frag
= &inode
.osd2
.linux2
.l_i_frag
;
713 fsize
= &inode
.osd2
.linux2
.l_i_fsize
;
716 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
717 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
720 frag
= &inode
.osd2
.masix2
.m_i_frag
;
721 fsize
= &inode
.osd2
.masix2
.m_i_fsize
;
727 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
729 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
731 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
732 sprintf(buf
, "Direct Block #%d", i
);
733 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
735 modify_u32(argv
[0], "Indirect Block", decimal_format
,
736 &inode
.i_block
[EXT2_IND_BLOCK
]);
737 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
738 &inode
.i_block
[EXT2_DIND_BLOCK
]);
739 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
740 &inode
.i_block
[EXT2_TIND_BLOCK
]);
741 retval
= ext2fs_write_inode(current_fs
, inode_num
, &inode
);
743 com_err(argv
[1], retval
, "while trying to write inode %d",
749 void do_change_working_dir(int argc
, char *argv
[])
755 com_err(argv
[0], 0, "Usage: cd <file>");
758 if (check_fs_open(argv
[0]))
761 inode
= string_to_inode(argv
[1]);
765 retval
= ext2fs_check_directory(current_fs
, inode
);
767 com_err(argv
[1], retval
, "");
774 void do_print_working_directory(int argc
, char *argv
[])
777 char *pathname
= NULL
;
780 com_err(argv
[0], 0, "Usage: print_working_directory");
783 if (check_fs_open(argv
[0]))
786 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
788 com_err(argv
[0], retval
,
789 "while trying to get pathname of cwd");
791 printf("[pwd] INODE: %6ld PATH: %s\n", cwd
, pathname
);
793 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
795 com_err(argv
[0], retval
,
796 "while trying to get pathname of root");
798 printf("[root] INODE: %6ld PATH: %s\n", root
, pathname
);
803 static void make_link(char *sourcename
, char *destname
)
808 char *dest
, *cp
, *basename
;
811 * Get the source inode
813 inode
= string_to_inode(sourcename
);
816 basename
= strrchr(sourcename
, '/');
820 basename
= sourcename
;
822 * Figure out the destination. First see if it exists and is
825 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
829 * OK, it doesn't exist. See if it is
830 * '<dir>/basename' or 'basename'
832 cp
= strrchr(destname
, '/');
835 dir
= string_to_inode(destname
);
845 retval
= ext2fs_link(current_fs
, dir
, dest
, inode
, 0);
847 com_err("make_link", retval
, "");
852 void do_link(int argc
, char *argv
[])
855 com_err(argv
[0], 0, "Usage: link <source_file> <dest_name>");
858 if (check_fs_open(argv
[0]))
861 make_link(argv
[1], argv
[2]);
864 static void unlink_file_by_name(char *filename
)
870 basename
= strrchr(filename
, '/');
873 dir
= string_to_inode(filename
);
880 retval
= ext2fs_unlink(current_fs
, dir
, basename
, 0, 0);
882 com_err("unlink_file_by_name", retval
, "");
886 void do_unlink(int argc
, char *argv
[])
889 com_err(argv
[0], 0, "Usage: unlink <pathname>");
892 if (check_fs_open(argv
[0]))
895 unlink_file_by_name(argv
[1]);
898 void do_find_free_block(int argc
, char *argv
[])
900 blk_t free_blk
, goal
;
904 if ((argc
> 2) || (argc
==2 && *argv
[1] == '?')) {
905 com_err(argv
[0], 0, "Usage: find_free_block [goal]");
908 if (check_fs_open(argv
[0]))
912 goal
= strtol(argv
[1], &tmp
, 0);
914 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
919 goal
= current_fs
->super
->s_first_data_block
;
921 retval
= ext2fs_new_block(current_fs
, goal
, 0, &free_blk
);
923 com_err("ext2fs_new_block", retval
, "");
925 printf("Free block found: %d\n", free_blk
);
929 void do_find_free_inode(int argc
, char *argv
[])
931 ino_t free_inode
, dir
;
936 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
937 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
940 if (check_fs_open(argv
[0]))
944 dir
= strtol(argv
[1], &tmp
, 0);
946 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
953 mode
= strtol(argv
[2], &tmp
, 0);
955 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
961 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
963 com_err("ext2fs_new_inode", retval
, "");
965 printf("Free inode found: %ld\n", free_inode
);
968 static errcode_t
copy_file(int fd
, ino_t newfile
)
972 unsigned int got
, written
;
976 retval
= ext2fs_file_open(current_fs
, newfile
,
977 EXT2_FILE_WRITE
, &e2_file
);
982 got
= read(fd
, buf
, sizeof(buf
));
991 retval
= ext2fs_file_write(e2_file
, ptr
,
1000 retval
= ext2fs_file_close(e2_file
);
1005 (void) ext2fs_file_close(e2_file
);
1010 void do_write(int argc
, char *argv
[])
1013 struct stat statbuf
;
1016 struct ext2_inode inode
;
1019 if (check_fs_open(argv
[0]))
1022 com_err(argv
[0], 0, "Usage: write <nativefile> <newfile>");
1025 if (check_fs_read_write(argv
[0]))
1027 fd
= open(argv
[1], O_RDONLY
);
1029 com_err(argv
[1], fd
, "");
1032 if (fstat(fd
, &statbuf
) < 0) {
1033 com_err(argv
[1], errno
, "");
1038 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1040 com_err(argv
[0], retval
, "");
1044 group
= ext2fs_group_of_ino(current_fs
, newfile
);
1045 current_fs
->group_desc
[group
].bg_free_inodes_count
--;
1046 current_fs
->super
->s_free_inodes_count
--;
1047 ext2fs_mark_super_dirty(current_fs
);
1048 printf("Allocated inode: %ld\n", newfile
);
1049 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
, 0);
1051 com_err(argv
[2], retval
, "");
1055 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1056 com_err(argv
[0], 0, "Warning: inode already set");
1057 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,newfile
);
1058 ext2fs_mark_ib_dirty(current_fs
);
1059 memset(&inode
, 0, sizeof(inode
));
1060 inode
.i_mode
= statbuf
.st_mode
;
1061 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1062 inode
.i_links_count
= 1;
1063 inode
.i_size
= statbuf
.st_size
;
1064 ext2fs_write_inode(current_fs
, newfile
, &inode
);
1065 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1067 com_err(argv
[0], retval
, "while trying to write inode %d",
1072 if (LINUX_S_ISREG(inode
.i_mode
)) {
1073 retval
= copy_file(fd
, newfile
);
1075 com_err("copy_file", retval
, "");
1080 void do_mknod(int argc
, char *argv
[])
1082 unsigned long mode
, major
, minor
, nr
;
1085 struct ext2_inode inode
;
1087 if (check_fs_open(argv
[0]))
1089 if (argc
< 3 || argv
[2][1]) {
1090 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1093 mode
= minor
= major
= 0;
1094 switch (argv
[2][0]) {
1096 mode
= LINUX_S_IFIFO
;
1100 mode
= LINUX_S_IFCHR
;
1104 mode
= LINUX_S_IFBLK
;
1111 major
= strtoul(argv
[3], argv
+3, 0);
1112 minor
= strtoul(argv
[4], argv
+4, 0);
1113 if (major
> 255 || minor
> 255 || argv
[3][0] || argv
[4][0])
1117 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1120 if (check_fs_read_write(argv
[0]))
1122 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1124 com_err(argv
[0], retval
, "");
1127 printf("Allocated inode: %ld\n", newfile
);
1128 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, 0);
1130 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1131 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1133 retval
= ext2fs_link(current_fs
, cwd
,
1134 argv
[1], newfile
, 0);
1137 com_err(argv
[1], retval
, "");
1141 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1142 com_err(argv
[0], 0, "Warning: inode already set");
1143 ext2fs_mark_inode_bitmap(current_fs
->inode_map
, newfile
);
1144 ext2fs_mark_ib_dirty(current_fs
);
1145 memset(&inode
, 0, sizeof(inode
));
1146 inode
.i_mode
= mode
;
1147 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1148 inode
.i_block
[0] = major
*256+minor
;
1149 inode
.i_links_count
= 1;
1150 ext2fs_write_inode(current_fs
, newfile
, &inode
);
1151 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1153 com_err(argv
[0], retval
, "while trying to write inode %d", inode
);
1158 void do_mkdir(int argc
, char *argv
[])
1165 if (check_fs_open(argv
[0]))
1169 com_err(argv
[0], 0, "Usage: mkdir <file>");
1173 cp
= strrchr(argv
[1], '/');
1176 parent
= string_to_inode(argv
[1]);
1178 com_err(argv
[1], ENOENT
, "");
1188 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1190 com_err("ext2fs_mkdir", retval
, "");
1196 void do_rmdir(int argc
, char *argv
[])
1198 printf("Unimplemented\n");
1202 static int release_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
1203 int blockcnt
, void *private)
1205 printf("%d ", *blocknr
);
1206 ext2fs_unmark_block_bitmap(fs
->block_map
,*blocknr
);
1210 static void kill_file_by_inode(ino_t inode
)
1212 struct ext2_inode inode_buf
;
1214 ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
1215 inode_buf
.i_dtime
= time(NULL
);
1216 ext2fs_write_inode(current_fs
, inode
, &inode_buf
);
1218 printf("Kill file by inode %ld\n", inode
);
1219 ext2fs_block_iterate(current_fs
, inode
, 0, NULL
,
1220 release_blocks_proc
, NULL
);
1222 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
, inode
);
1224 ext2fs_mark_bb_dirty(current_fs
);
1225 ext2fs_mark_ib_dirty(current_fs
);
1229 void do_kill_file(int argc
, char *argv
[])
1234 com_err(argv
[0], 0, "Usage: kill_file <file>");
1237 if (check_fs_open(argv
[0]))
1240 inode_num
= string_to_inode(argv
[1]);
1242 com_err(argv
[0], 0, "Cannot find file");
1245 kill_file_by_inode(inode_num
);
1248 void do_rm(int argc
, char *argv
[])
1252 struct ext2_inode inode
;
1255 com_err(argv
[0], 0, "Usage: rm <filename>");
1258 if (check_fs_open(argv
[0]))
1261 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1263 com_err(argv
[0], retval
, "while trying to resolve filename");
1267 retval
= ext2fs_read_inode(current_fs
,inode_num
,&inode
);
1269 com_err(argv
[0], retval
, "while reading file's inode");
1273 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1274 com_err(argv
[0], 0, "file is a directory");
1278 --inode
.i_links_count
;
1279 retval
= ext2fs_write_inode(current_fs
,inode_num
,&inode
);
1281 com_err(argv
[0], retval
, "while writing inode");
1285 unlink_file_by_name(argv
[1]);
1286 if (inode
.i_links_count
== 0)
1287 kill_file_by_inode(inode_num
);
1290 void do_show_debugfs_params(int argc
, char *argv
[])
1295 fprintf(out
, "Open mode: read-%s\n",
1296 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1297 fprintf(out
, "Filesystem in use: %s\n",
1298 current_fs
? current_fs
->device_name
: "--none--");
1301 void do_expand_dir(int argc
, char *argv
[])
1307 com_err(argv
[0], 0, "Usage: expand_dir <file>");
1310 if (check_fs_open(argv
[0]))
1312 inode
= string_to_inode(argv
[1]);
1316 retval
= ext2fs_expand_dir(current_fs
, inode
);
1318 com_err("ext2fs_expand_dir", retval
, "");
1322 static int source_file(const char *cmd_file
, int sci_idx
)
1327 int exit_status
= 0;
1330 if (strcmp(cmd_file
, "-") == 0)
1333 f
= fopen(cmd_file
, "r");
1339 setbuf(stdout
, NULL
);
1340 setbuf(stderr
, NULL
);
1342 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
1344 cp
= strchr(buf
, '\n');
1347 cp
= strchr(buf
, '\r');
1350 printf("debugfs: %s\n", buf
);
1351 retval
= ss_execute_line(sci_idx
, buf
);
1353 ss_perror(sci_idx
, retval
, buf
);
1360 int main(int argc
, char **argv
)
1364 const char *usage
= "Usage: debugfs [-f cmd_file] [-R request] [-V] [[-w] device]";
1368 int exit_status
= 0;
1371 initialize_ext2_error_table();
1372 fprintf (stderr
, "debugfs %s, %s for EXT2 FS %s, %s\n",
1373 E2FSPROGS_VERSION
, E2FSPROGS_DATE
,
1374 EXT2FS_VERSION
, EXT2FS_DATE
);
1376 while ((c
= getopt (argc
, argv
, "wR:f:V")) != EOF
) {
1385 open_flags
= EXT2_FLAG_RW
;
1388 /* Print version number and exit */
1389 fprintf(stderr
, "\tUsing %s\n",
1390 error_message(EXT2_ET_BASE
));
1393 com_err(argv
[0], 0, usage
);
1398 open_filesystem(argv
[optind
], open_flags
);
1400 sci_idx
= ss_create_invocation("debugfs", "0.0", (char *) NULL
,
1401 &debug_cmds
, &retval
);
1403 ss_perror(sci_idx
, retval
, "creating invocation");
1407 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
1409 ss_perror(sci_idx
, retval
, "adding standard requests");
1414 retval
= ss_execute_line(sci_idx
, request
);
1416 ss_perror(sci_idx
, retval
, request
);
1419 } else if (cmd_file
) {
1420 exit_status
= source_file(cmd_file
, sci_idx
);