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 extern ss_request_table debug_cmds
;
40 ext2_filsys current_fs
= NULL
;
43 static void open_filesystem(char *device
, int open_flags
)
47 retval
= ext2fs_open(device
, open_flags
, 0, 0,
48 unix_io_manager
, ¤t_fs
);
50 com_err(device
, retval
, "while opening filesystem");
54 retval
= ext2fs_read_inode_bitmap(current_fs
);
56 com_err(device
, retval
, "while reading inode bitmap");
59 retval
= ext2fs_read_block_bitmap(current_fs
);
61 com_err(device
, retval
, "while reading block bitmap");
64 root
= cwd
= EXT2_ROOT_INO
;
68 retval
= ext2fs_close(current_fs
);
70 com_err(device
, retval
, "while trying to close filesystem");
74 void do_open_filesys(int argc
, char **argv
)
76 const char *usage
= "Usage: open [-w] <device>";
82 optreset
= 1; /* Makes BSD getopt happy */
84 while ((c
= getopt (argc
, argv
, "w")) != EOF
) {
87 open_flags
= EXT2_FLAG_RW
;
90 com_err(argv
[0], 0, usage
);
94 if (optind
!= argc
-1) {
95 com_err(argv
[0], 0, usage
);
98 if (check_fs_not_open(argv
[0]))
100 open_filesystem(argv
[optind
], open_flags
);
103 static void close_filesystem(NOARGS
)
107 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
108 retval
= ext2fs_write_inode_bitmap(current_fs
);
110 com_err("ext2fs_write_inode_bitmap", retval
, "");
112 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
113 retval
= ext2fs_write_block_bitmap(current_fs
);
115 com_err("ext2fs_write_block_bitmap", retval
, "");
117 retval
= ext2fs_close(current_fs
);
119 com_err("ext2fs_close", retval
, "");
124 void do_close_filesys(int argc
, char **argv
)
127 com_err(argv
[0], 0, "Usage: close_filesys");
130 if (check_fs_open(argv
[0]))
135 void do_init_filesys(int argc
, char **argv
)
137 const char *usage
= "Usage: initialize <device> <blocksize>";
138 struct ext2_super_block param
;
143 com_err(argv
[0], 0, usage
);
146 if (check_fs_not_open(argv
[0]))
149 memset(¶m
, 0, sizeof(struct ext2_super_block
));
150 param
.s_blocks_count
= strtoul(argv
[2], &tmp
, 0);
152 com_err(argv
[0], 0, "Bad blocks count - %s", argv
[2]);
155 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
156 unix_io_manager
, ¤t_fs
);
158 com_err(argv
[1], retval
, "while initializing filesystem");
162 root
= cwd
= EXT2_ROOT_INO
;
166 void do_show_super_stats(int argc
, char *argv
[])
170 struct ext2fs_sb
*sb
;
171 struct ext2_group_desc
*gdp
;
173 const char *none
= "(none)";
176 com_err(argv
[0], 0, "Usage: show_super");
179 if (check_fs_open(argv
[0]))
182 sb
= (struct ext2fs_sb
*) current_fs
->super
;
183 fprintf(out
, "Filesystem is read-%s\n",
184 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
185 if (sb
->s_volume_name
[0]) {
186 memset(buf
, 0, sizeof(buf
));
187 strncpy(buf
, sb
->s_volume_name
, sizeof(sb
->s_volume_name
));
190 fprintf(out
, "Volume name = %s\n", buf
);
191 if (sb
->s_last_mounted
[0]) {
192 memset(buf
, 0, sizeof(buf
));
193 strncpy(buf
, sb
->s_last_mounted
, sizeof(sb
->s_last_mounted
));
196 fprintf(out
, "Last mounted directory = %s\n", buf
);
197 if (!uuid_is_null(sb
->s_uuid
))
198 uuid_unparse(sb
->s_uuid
, buf
);
201 fprintf(out
, "Filesystem UUID = %s\n", buf
);
202 fprintf(out
, "Last mount time = %s", time_to_string(sb
->s_mtime
));
203 fprintf(out
, "Last write time = %s", time_to_string(sb
->s_wtime
));
204 fprintf(out
, "Mount counts = %d (maximal = %d)\n",
205 sb
->s_mnt_count
, sb
->s_max_mnt_count
);
206 fputs ("Filesystem OS type = ", out
);
207 switch (sb
->s_creator_os
) {
208 case EXT2_OS_LINUX
: fputs ("Linux\n", out
); break;
209 case EXT2_OS_HURD
: fputs ("GNU\n", out
); break;
210 case EXT2_OS_MASIX
: fputs ("Masix\n", out
); break;
211 default: fputs ("unknown\n", out
);
213 fprintf(out
, "Superblock size = %d\n",
214 sizeof(struct ext2_super_block
));
215 fprintf(out
, "Block size = %d, fragment size = %d\n",
216 EXT2_BLOCK_SIZE(sb
), EXT2_FRAG_SIZE(sb
));
217 fprintf(out
, "Inode size = %d\n", EXT2_INODE_SIZE(sb
));
218 fprintf(out
, "%d inodes, %d free\n", sb
->s_inodes_count
,
219 sb
->s_free_inodes_count
);
220 fprintf(out
, "%d blocks, %d free, %d reserved, first block = %d\n",
221 sb
->s_blocks_count
, sb
->s_free_blocks_count
,
222 sb
->s_r_blocks_count
, sb
->s_first_data_block
);
223 fprintf(out
, "%d blocks per group\n", sb
->s_blocks_per_group
);
224 fprintf(out
, "%d fragments per group\n", sb
->s_frags_per_group
);
225 fprintf(out
, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb
));
226 fprintf(out
, "%ld group%s (%ld descriptors block%s)\n",
227 current_fs
->group_desc_count
,
228 (current_fs
->group_desc_count
!= 1) ? "s" : "",
229 current_fs
->desc_blocks
,
230 (current_fs
->desc_blocks
!= 1) ? "s" : "");
232 gdp
= ¤t_fs
->group_desc
[0];
233 for (i
= 0; i
< current_fs
->group_desc_count
; i
++, gdp
++)
234 fprintf(out
, " Group %2d: block bitmap at %d, "
235 "inode bitmap at %d, "
236 "inode table at %d\n"
239 "%d used director%s\n",
240 i
, gdp
->bg_block_bitmap
,
241 gdp
->bg_inode_bitmap
, gdp
->bg_inode_table
,
242 gdp
->bg_free_blocks_count
,
243 gdp
->bg_free_blocks_count
!= 1 ? "s" : "",
244 gdp
->bg_free_inodes_count
,
245 gdp
->bg_free_inodes_count
!= 1 ? "s" : "",
246 gdp
->bg_used_dirs_count
,
247 gdp
->bg_used_dirs_count
!= 1 ? "ies" : "y");
251 void do_dirty_filesys(int argc
, char *argv
[])
253 if (check_fs_open(argv
[0]))
256 ext2fs_mark_super_dirty(current_fs
);
259 struct list_blocks_struct
{
264 static int list_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
, int blockcnt
,
267 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
269 fprintf(lb
->f
, "%d ", *blocknr
);
275 static void dump_blocks(FILE *f
, ino_t inode
)
277 struct list_blocks_struct lb
;
279 fprintf(f
, "BLOCKS:\n");
282 ext2fs_block_iterate(current_fs
, inode
, 0, NULL
,
283 list_blocks_proc
, (void *)&lb
);
285 fprintf(f
, "\nTOTAL: %d\n", lb
.total
);
290 static void dump_inode(ino_t inode_num
, struct ext2_inode inode
)
295 int os
= current_fs
->super
->s_creator_os
;
298 if (LINUX_S_ISDIR(inode
.i_mode
)) i_type
= "directory";
299 else if (LINUX_S_ISREG(inode
.i_mode
)) i_type
= "regular";
300 else if (LINUX_S_ISLNK(inode
.i_mode
)) i_type
= "symlink";
301 else if (LINUX_S_ISBLK(inode
.i_mode
)) i_type
= "block special";
302 else if (LINUX_S_ISCHR(inode
.i_mode
)) i_type
= "character special";
303 else if (LINUX_S_ISFIFO(inode
.i_mode
)) i_type
= "FIFO";
304 else if (LINUX_S_ISSOCK(inode
.i_mode
)) i_type
= "socket";
305 else i_type
= "bad type";
306 fprintf(out
, "Inode: %ld Type: %s ", inode_num
, i_type
);
307 fprintf(out
, "Mode: %04o Flags: 0x%x Version: %d\n",
308 inode
.i_mode
& 0777, inode
.i_flags
, inode
.i_version
);
309 fprintf(out
, "User: %5d Group: %5d Size: %d\n",
310 inode
.i_uid
, inode
.i_gid
, inode
.i_size
);
311 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
313 "File ACL: %d Directory ACL: %d Translator: %d\n",
314 inode
.i_file_acl
, inode
.i_dir_acl
,
315 inode
.osd1
.hurd1
.h_i_translator
);
317 fprintf(out
, "File ACL: %d Directory ACL: %d\n",
318 inode
.i_file_acl
, inode
.i_dir_acl
);
319 fprintf(out
, "Links: %d Blockcount: %d\n", inode
.i_links_count
,
323 frag
= inode
.osd2
.linux2
.l_i_frag
;
324 fsize
= inode
.osd2
.linux2
.l_i_fsize
;
327 frag
= inode
.osd2
.hurd2
.h_i_frag
;
328 fsize
= inode
.osd2
.hurd2
.h_i_fsize
;
331 frag
= inode
.osd2
.masix2
.m_i_frag
;
332 fsize
= inode
.osd2
.masix2
.m_i_fsize
;
337 fprintf(out
, "Fragment: Address: %d Number: %d Size: %d\n",
338 inode
.i_faddr
, frag
, fsize
);
339 fprintf(out
, "ctime: 0x%08x -- %s", inode
.i_ctime
,
340 time_to_string(inode
.i_ctime
));
341 fprintf(out
, "atime: 0x%08x -- %s", inode
.i_atime
,
342 time_to_string(inode
.i_atime
));
343 fprintf(out
, "mtime: 0x%08x -- %s", inode
.i_mtime
,
344 time_to_string(inode
.i_mtime
));
346 fprintf(out
, "dtime: 0x%08x -- %s", inode
.i_dtime
,
347 time_to_string(inode
.i_dtime
));
348 if (LINUX_S_ISLNK(inode
.i_mode
) && inode
.i_blocks
== 0)
349 fprintf(out
, "Fast_link_dest: %s\n", (char *)inode
.i_block
);
351 dump_blocks(out
, inode_num
);
356 void do_stat(int argc
, char *argv
[])
359 struct ext2_inode inode_buf
;
363 com_err(argv
[0], 0, "Usage: stat <file>");
366 if (check_fs_open(argv
[0]))
368 inode
= string_to_inode(argv
[1]);
372 retval
= ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
375 com_err(argv
[0], 0, "Reading inode");
379 dump_inode(inode
,inode_buf
);
383 void do_chroot(int argc
, char *argv
[])
389 com_err(argv
[0], 0, "Usage: chroot <file>");
392 if (check_fs_open(argv
[0]))
394 inode
= string_to_inode(argv
[1]);
398 retval
= ext2fs_check_directory(current_fs
, inode
);
400 com_err(argv
[1], retval
, "");
406 void do_clri(int argc
, char *argv
[])
410 struct ext2_inode inode_buf
;
413 com_err(argv
[0], 0, "Usage: clri <file>");
416 if (check_fs_open(argv
[0]))
418 if (check_fs_read_write(argv
[0]))
420 inode
= string_to_inode(argv
[1]);
424 retval
= ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
426 com_err(argv
[0], 0, "while trying to read inode %d", inode
);
429 memset(&inode_buf
, 0, sizeof(inode_buf
));
430 retval
= ext2fs_write_inode(current_fs
, inode
, &inode_buf
);
432 com_err(argv
[0], retval
, "while trying to write inode %d",
438 void do_freei(int argc
, char *argv
[])
443 com_err(argv
[0], 0, "Usage: freei <file>");
446 if (check_fs_open(argv
[0]))
448 if (check_fs_read_write(argv
[0]))
450 inode
= string_to_inode(argv
[1]);
454 if (!ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
455 com_err(argv
[0], 0, "Warning: inode already clear");
456 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
,inode
);
457 ext2fs_mark_ib_dirty(current_fs
);
460 void do_seti(int argc
, char *argv
[])
465 com_err(argv
[0], 0, "Usage: seti <file>");
468 if (check_fs_open(argv
[0]))
470 if (check_fs_read_write(argv
[0]))
472 inode
= string_to_inode(argv
[1]);
476 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
477 com_err(argv
[0], 0, "Warning: inode already set");
478 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,inode
);
479 ext2fs_mark_ib_dirty(current_fs
);
482 void do_testi(int argc
, char *argv
[])
487 com_err(argv
[0], 0, "Usage: testi <file>");
490 if (check_fs_open(argv
[0]))
492 inode
= string_to_inode(argv
[1]);
496 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
497 printf("Inode %ld is marked in use\n", inode
);
499 printf("Inode %ld is not in use\n", inode
);
503 void do_freeb(int argc
, char *argv
[])
509 com_err(argv
[0], 0, "Usage: freeb <block>");
512 if (check_fs_open(argv
[0]))
514 if (check_fs_read_write(argv
[0]))
516 block
= strtoul(argv
[1], &tmp
, 0);
517 if (!block
|| *tmp
) {
518 com_err(argv
[0], 0, "No block 0");
521 if (!ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
522 com_err(argv
[0], 0, "Warning: block already clear");
523 ext2fs_unmark_block_bitmap(current_fs
->block_map
,block
);
524 ext2fs_mark_bb_dirty(current_fs
);
527 void do_setb(int argc
, char *argv
[])
533 com_err(argv
[0], 0, "Usage: setb <block>");
536 if (check_fs_open(argv
[0]))
538 if (check_fs_read_write(argv
[0]))
540 block
= strtoul(argv
[1], &tmp
, 0);
541 if (!block
|| *tmp
) {
542 com_err(argv
[0], 0, "No block 0");
545 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
546 com_err(argv
[0], 0, "Warning: block already set");
547 ext2fs_mark_block_bitmap(current_fs
->block_map
,block
);
548 ext2fs_mark_bb_dirty(current_fs
);
551 void do_testb(int argc
, char *argv
[])
557 com_err(argv
[0], 0, "Usage: testb <block>");
560 if (check_fs_open(argv
[0]))
562 block
= strtoul(argv
[1], &tmp
, 0);
563 if (!block
|| *tmp
) {
564 com_err(argv
[0], 0, "No block 0");
567 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
568 printf("Block %d marked in use\n", block
);
569 else printf("Block %d not in use\n", block
);
572 static void modify_u8(char *com
, const char *prompt
,
573 const char *format
, __u8
*val
)
579 sprintf(buf
, format
, *val
);
580 printf("%30s [%s] ", prompt
, buf
);
581 fgets(buf
, sizeof(buf
), stdin
);
582 if (buf
[strlen (buf
) - 1] == '\n')
583 buf
[strlen (buf
) - 1] = '\0';
586 v
= strtoul(buf
, &tmp
, 0);
588 com_err(com
, 0, "Bad value - %s", buf
);
593 static void modify_u16(char *com
, const char *prompt
,
594 const char *format
, __u16
*val
)
600 sprintf(buf
, format
, *val
);
601 printf("%30s [%s] ", prompt
, buf
);
602 fgets(buf
, sizeof(buf
), stdin
);
603 if (buf
[strlen (buf
) - 1] == '\n')
604 buf
[strlen (buf
) - 1] = '\0';
607 v
= strtoul(buf
, &tmp
, 0);
609 com_err(com
, 0, "Bad value - %s", buf
);
614 static void modify_u32(char *com
, const char *prompt
,
615 const char *format
, __u32
*val
)
621 sprintf(buf
, format
, *val
);
622 printf("%30s [%s] ", prompt
, buf
);
623 fgets(buf
, sizeof(buf
), stdin
);
624 if (buf
[strlen (buf
) - 1] == '\n')
625 buf
[strlen (buf
) - 1] = '\0';
628 v
= strtoul(buf
, &tmp
, 0);
630 com_err(com
, 0, "Bad value - %s", buf
);
636 void do_modify_inode(int argc
, char *argv
[])
638 struct ext2_inode inode
;
642 unsigned char *frag
, *fsize
;
644 int os
= current_fs
->super
->s_creator_os
;
645 const char *hex_format
= "0x%x";
646 const char *octal_format
= "0%o";
647 const char *decimal_format
= "%d";
650 com_err(argv
[0], 0, "Usage: modify_inode <file>");
653 if (check_fs_open(argv
[0]))
655 if (check_fs_read_write(argv
[0]))
658 inode_num
= string_to_inode(argv
[1]);
662 retval
= ext2fs_read_inode(current_fs
, inode_num
, &inode
);
664 com_err(argv
[1], retval
, "while trying to read inode %d",
669 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
670 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
671 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
672 modify_u32(argv
[0], "Size", decimal_format
, &inode
.i_size
);
673 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
674 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
675 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
676 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
677 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
678 modify_u32(argv
[0], "Block count", decimal_format
, &inode
.i_blocks
);
679 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
681 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
683 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
684 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
686 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
687 modify_u32(argv
[0], "Translator Block",
688 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
690 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
693 frag
= &inode
.osd2
.linux2
.l_i_frag
;
694 fsize
= &inode
.osd2
.linux2
.l_i_fsize
;
697 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
698 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
701 frag
= &inode
.osd2
.masix2
.m_i_frag
;
702 fsize
= &inode
.osd2
.masix2
.m_i_fsize
;
708 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
710 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
712 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
713 sprintf(buf
, "Direct Block #%d", i
);
714 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
716 modify_u32(argv
[0], "Indirect Block", decimal_format
,
717 &inode
.i_block
[EXT2_IND_BLOCK
]);
718 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
719 &inode
.i_block
[EXT2_DIND_BLOCK
]);
720 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
721 &inode
.i_block
[EXT2_TIND_BLOCK
]);
722 retval
= ext2fs_write_inode(current_fs
, inode_num
, &inode
);
724 com_err(argv
[1], retval
, "while trying to write inode %d",
730 void do_change_working_dir(int argc
, char *argv
[])
736 com_err(argv
[0], 0, "Usage: cd <file>");
739 if (check_fs_open(argv
[0]))
742 inode
= string_to_inode(argv
[1]);
746 retval
= ext2fs_check_directory(current_fs
, inode
);
748 com_err(argv
[1], retval
, "");
755 void do_print_working_directory(int argc
, char *argv
[])
758 char *pathname
= NULL
;
761 com_err(argv
[0], 0, "Usage: print_working_directory");
764 if (check_fs_open(argv
[0]))
767 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
769 com_err(argv
[0], retval
,
770 "while trying to get pathname of cwd");
772 printf("[pwd] INODE: %6ld PATH: %s\n", cwd
, pathname
);
774 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
776 com_err(argv
[0], retval
,
777 "while trying to get pathname of root");
779 printf("[root] INODE: %6ld PATH: %s\n", root
, pathname
);
784 static void make_link(char *sourcename
, char *destname
)
789 char *dest
, *cp
, *basename
;
792 * Get the source inode
794 inode
= string_to_inode(sourcename
);
797 basename
= strrchr(sourcename
, '/');
801 basename
= sourcename
;
803 * Figure out the destination. First see if it exists and is
806 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
810 * OK, it doesn't exist. See if it is
811 * '<dir>/basename' or 'basename'
813 cp
= strrchr(destname
, '/');
816 dir
= string_to_inode(destname
);
826 retval
= ext2fs_link(current_fs
, dir
, dest
, inode
, 0);
828 com_err("make_link", retval
, "");
833 void do_link(int argc
, char *argv
[])
836 com_err(argv
[0], 0, "Usage: link <source_file> <dest_name>");
839 if (check_fs_open(argv
[0]))
842 make_link(argv
[1], argv
[2]);
845 static void unlink_file_by_name(char *filename
)
851 basename
= strrchr(filename
, '/');
854 dir
= string_to_inode(filename
);
861 retval
= ext2fs_unlink(current_fs
, dir
, basename
, 0, 0);
863 com_err("unlink_file_by_name", retval
, "");
867 void do_unlink(int argc
, char *argv
[])
870 com_err(argv
[0], 0, "Usage: unlink <pathname>");
873 if (check_fs_open(argv
[0]))
876 unlink_file_by_name(argv
[1]);
879 void do_find_free_block(int argc
, char *argv
[])
881 blk_t free_blk
, goal
;
885 if ((argc
> 2) || (argc
==2 && *argv
[1] == '?')) {
886 com_err(argv
[0], 0, "Usage: find_free_block [goal]");
889 if (check_fs_open(argv
[0]))
893 goal
= strtol(argv
[1], &tmp
, 0);
895 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
900 goal
= current_fs
->super
->s_first_data_block
;
902 retval
= ext2fs_new_block(current_fs
, goal
, 0, &free_blk
);
904 com_err("ext2fs_new_block", retval
, "");
906 printf("Free block found: %d\n", free_blk
);
910 void do_find_free_inode(int argc
, char *argv
[])
912 ino_t free_inode
, dir
;
917 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
918 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
921 if (check_fs_open(argv
[0]))
925 dir
= strtol(argv
[1], &tmp
, 0);
927 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
934 mode
= strtol(argv
[2], &tmp
, 0);
936 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
942 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
944 com_err("ext2fs_new_inode", retval
, "");
946 printf("Free inode found: %ld\n", free_inode
);
949 struct copy_file_struct
{
951 int done
, fd
, blocks
;
955 static int copy_file_proc(ext2_filsys to_fs
,
960 struct copy_file_struct
*cs
= (struct copy_file_struct
*) private;
962 static blk_t last_blk
= 0;
971 retval
= ext2fs_new_block(to_fs
, last_blk
, 0, &new_blk
);
978 block
= malloc(to_fs
->blocksize
);
984 nr
= read(cs
->fd
, block
, to_fs
->blocksize
);
986 nr
= to_fs
->blocksize
;
987 memset(block
, 0, nr
);
997 retval
= io_channel_write_blk(to_fs
->io
, new_blk
, 1, block
);
1005 cs
->blocks
+= to_fs
->blocksize
/ 512;
1006 printf("%ld(%d) ", cs
->size
, blockcnt
);
1008 if (nr
< to_fs
->blocksize
) {
1013 ext2fs_mark_block_bitmap(to_fs
->block_map
, new_blk
);
1014 ext2fs_mark_bb_dirty(to_fs
);
1015 group
= ext2fs_group_of_blk(to_fs
, new_blk
);
1016 to_fs
->group_desc
[group
].bg_free_blocks_count
--;
1017 to_fs
->super
->s_free_blocks_count
--;
1018 ext2fs_mark_super_dirty(to_fs
);
1020 return (BLOCK_CHANGED
| BLOCK_ABORT
);
1022 return BLOCK_CHANGED
;
1025 static errcode_t
copy_file(int fd
, ino_t newfile
)
1028 struct copy_file_struct cs
;
1029 struct ext2_inode inode
;
1037 retval
= ext2fs_block_iterate(current_fs
, newfile
,
1039 0, copy_file_proc
, &cs
);
1044 return EXT2_ET_EXPAND_DIR_ERR
;
1047 * Update the size and block count fields in the inode.
1049 retval
= ext2fs_read_inode(current_fs
, newfile
, &inode
);
1053 inode
.i_blocks
+= cs
.blocks
;
1055 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1062 void do_write(int argc
, char *argv
[])
1065 struct stat statbuf
;
1068 struct ext2_inode inode
;
1070 if (check_fs_open(argv
[0]))
1073 com_err(argv
[0], 0, "Usage: write <nativefile> <newfile>");
1076 if (check_fs_read_write(argv
[0]))
1078 fd
= open(argv
[1], O_RDONLY
);
1080 com_err(argv
[1], fd
, "");
1083 if (fstat(fd
, &statbuf
) < 0) {
1084 com_err(argv
[1], errno
, "");
1089 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1091 com_err(argv
[0], retval
, "");
1095 printf("Allocated inode: %ld\n", newfile
);
1096 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
, 0);
1098 com_err(argv
[2], retval
, "");
1102 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1103 com_err(argv
[0], 0, "Warning: inode already set");
1104 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,newfile
);
1105 ext2fs_mark_ib_dirty(current_fs
);
1106 memset(&inode
, 0, sizeof(inode
));
1107 inode
.i_mode
= statbuf
.st_mode
;
1108 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1109 inode
.i_links_count
= 1;
1110 inode
.i_size
= statbuf
.st_size
;
1111 ext2fs_write_inode(current_fs
, newfile
, &inode
);
1112 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1114 com_err(argv
[0], retval
, "while trying to write inode %d",
1119 if (LINUX_S_ISREG(inode
.i_mode
)) {
1120 retval
= copy_file(fd
, newfile
);
1122 com_err("copy_file", retval
, "");
1127 void do_mknod(int argc
, char *argv
[])
1129 unsigned long mode
, major
, minor
, nr
;
1132 struct ext2_inode inode
;
1134 if (check_fs_open(argv
[0]))
1136 if (argc
< 3 || argv
[2][1]) {
1137 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1140 mode
= minor
= major
= 0;
1141 switch (argv
[2][0]) {
1143 mode
= LINUX_S_IFIFO
;
1147 mode
= LINUX_S_IFCHR
;
1151 mode
= LINUX_S_IFBLK
;
1158 major
= strtoul(argv
[3], argv
+3, 0);
1159 minor
= strtoul(argv
[4], argv
+4, 0);
1160 if (major
> 255 || minor
> 255 || argv
[3][0] || argv
[4][0])
1164 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1167 if (check_fs_read_write(argv
[0]))
1169 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1171 com_err(argv
[0], retval
, "");
1174 printf("Allocated inode: %ld\n", newfile
);
1175 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, 0);
1177 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1178 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1180 retval
= ext2fs_link(current_fs
, cwd
,
1181 argv
[1], newfile
, 0);
1184 com_err(argv
[1], retval
, "");
1188 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1189 com_err(argv
[0], 0, "Warning: inode already set");
1190 ext2fs_mark_inode_bitmap(current_fs
->inode_map
, newfile
);
1191 ext2fs_mark_ib_dirty(current_fs
);
1192 memset(&inode
, 0, sizeof(inode
));
1193 inode
.i_mode
= mode
;
1194 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1195 inode
.i_block
[0] = major
*256+minor
;
1196 inode
.i_links_count
= 1;
1197 ext2fs_write_inode(current_fs
, newfile
, &inode
);
1198 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1200 com_err(argv
[0], retval
, "while trying to write inode %d", inode
);
1205 void do_mkdir(int argc
, char *argv
[])
1212 if (check_fs_open(argv
[0]))
1216 com_err(argv
[0], 0, "Usage: mkdir <file>");
1220 cp
= strrchr(argv
[1], '/');
1223 parent
= string_to_inode(argv
[1]);
1225 com_err(argv
[1], ENOENT
, "");
1235 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1237 com_err("ext2fs_mkdir", retval
, "");
1243 void do_rmdir(int argc
, char *argv
[])
1245 printf("Unimplemented\n");
1249 static int release_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
1250 int blockcnt
, void *private)
1252 printf("%d ", *blocknr
);
1253 ext2fs_unmark_block_bitmap(fs
->block_map
,*blocknr
);
1257 static void kill_file_by_inode(ino_t inode
)
1259 struct ext2_inode inode_buf
;
1261 ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
1262 inode_buf
.i_dtime
= time(NULL
);
1263 ext2fs_write_inode(current_fs
, inode
, &inode_buf
);
1265 printf("Kill file by inode %ld\n", inode
);
1266 ext2fs_block_iterate(current_fs
, inode
, 0, NULL
,
1267 release_blocks_proc
, NULL
);
1269 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
, inode
);
1271 ext2fs_mark_bb_dirty(current_fs
);
1272 ext2fs_mark_ib_dirty(current_fs
);
1276 void do_kill_file(int argc
, char *argv
[])
1281 com_err(argv
[0], 0, "Usage: kill_file <file>");
1284 if (check_fs_open(argv
[0]))
1287 inode_num
= string_to_inode(argv
[1]);
1289 com_err(argv
[0], 0, "Cannot find file");
1292 kill_file_by_inode(inode_num
);
1295 void do_rm(int argc
, char *argv
[])
1299 struct ext2_inode inode
;
1302 com_err(argv
[0], 0, "Usage: rm <filename>");
1305 if (check_fs_open(argv
[0]))
1308 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1310 com_err(argv
[0], 0, "Cannot find file");
1314 retval
= ext2fs_read_inode(current_fs
,inode_num
,&inode
);
1316 com_err(argv
[0], retval
, "while reading file's inode");
1320 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1321 com_err(argv
[0], 0, "file is a directory");
1325 --inode
.i_links_count
;
1326 retval
= ext2fs_write_inode(current_fs
,inode_num
,&inode
);
1328 com_err(argv
[0], retval
, "while writing inode");
1332 unlink_file_by_name(argv
[1]);
1333 if (inode
.i_links_count
== 0)
1334 kill_file_by_inode(inode_num
);
1337 void do_show_debugfs_params(int argc
, char *argv
[])
1342 fprintf(out
, "Open mode: read-%s\n",
1343 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1344 fprintf(out
, "Filesystem in use: %s\n",
1345 current_fs
? current_fs
->device_name
: "--none--");
1348 void do_expand_dir(int argc
, char *argv
[])
1354 com_err(argv
[0], 0, "Usage: expand_dir <file>");
1357 if (check_fs_open(argv
[0]))
1359 inode
= string_to_inode(argv
[1]);
1363 retval
= ext2fs_expand_dir(current_fs
, inode
);
1365 com_err("ext2fs_expand_dir", retval
, "");
1369 static int source_file(const char *cmd_file
, int sci_idx
)
1374 int exit_status
= 0;
1377 if (strcmp(cmd_file
, "-") == 0)
1380 f
= fopen(cmd_file
, "r");
1386 setbuf(stdout
, NULL
);
1387 setbuf(stderr
, NULL
);
1389 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
1391 cp
= strchr(buf
, '\n');
1394 cp
= strchr(buf
, '\r');
1397 printf("debugfs: %s\n", buf
);
1398 retval
= ss_execute_line(sci_idx
, buf
);
1400 ss_perror(sci_idx
, retval
, buf
);
1407 void main(int argc
, char **argv
)
1411 const char *usage
= "Usage: debugfs [[-w] device]";
1415 int exit_status
= 0;
1418 initialize_ext2_error_table();
1420 while ((c
= getopt (argc
, argv
, "wR:f:")) != EOF
) {
1429 open_flags
= EXT2_FLAG_RW
;
1432 com_err(argv
[0], 0, usage
);
1437 open_filesystem(argv
[optind
], open_flags
);
1439 sci_idx
= ss_create_invocation("debugfs", "0.0", (char *) NULL
,
1440 &debug_cmds
, &retval
);
1442 ss_perror(sci_idx
, retval
, "creating invocation");
1446 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
1448 ss_perror(sci_idx
, retval
, "adding standard requests");
1453 retval
= ss_execute_line(sci_idx
, request
);
1455 ss_perror(sci_idx
, retval
, request
);
1458 } else if (cmd_file
) {
1459 exit_status
= source_file(cmd_file
, sci_idx
);