]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/debugfs.c
ChangeLog, debugfs.c:
[thirdparty/e2fsprogs.git] / debugfs / debugfs.c
1 /*
2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
4 *
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
7 *
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
9 */
10
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <time.h>
17 #ifdef HAVE_GETOPT_H
18 #include <getopt.h>
19 #else
20 extern int optind;
21 extern char *optarg;
22 #endif
23 #ifdef HAVE_OPTRESET
24 extern int optreset; /* defined by BSD, but not others */
25 #endif
26 #ifdef HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29 #include <fcntl.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #include "et/com_err.h"
34 #include "ss/ss.h"
35 #include "debugfs.h"
36 #include "uuid/uuid.h"
37
38 #include "../version.h"
39
40 extern ss_request_table debug_cmds;
41
42 ext2_filsys current_fs = NULL;
43 ino_t root, cwd;
44
45 static void open_filesystem(char *device, int open_flags)
46 {
47 int retval;
48
49 retval = ext2fs_open(device, open_flags, 0, 0,
50 unix_io_manager, &current_fs);
51 if (retval) {
52 com_err(device, retval, "while opening filesystem");
53 current_fs = NULL;
54 return;
55 }
56 retval = ext2fs_read_inode_bitmap(current_fs);
57 if (retval) {
58 com_err(device, retval, "while reading inode bitmap");
59 goto errout;
60 }
61 retval = ext2fs_read_block_bitmap(current_fs);
62 if (retval) {
63 com_err(device, retval, "while reading block bitmap");
64 goto errout;
65 }
66 root = cwd = EXT2_ROOT_INO;
67 return;
68
69 errout:
70 retval = ext2fs_close(current_fs);
71 if (retval)
72 com_err(device, retval, "while trying to close filesystem");
73 current_fs = NULL;
74 }
75
76 void do_open_filesys(int argc, char **argv)
77 {
78 const char *usage = "Usage: open [-w] <device>";
79 int c;
80 int open_flags = 0;
81
82 optind = 0;
83 #ifdef HAVE_OPTRESET
84 optreset = 1; /* Makes BSD getopt happy */
85 #endif
86 while ((c = getopt (argc, argv, "w")) != EOF) {
87 switch (c) {
88 case 'w':
89 open_flags = EXT2_FLAG_RW;
90 break;
91 default:
92 com_err(argv[0], 0, usage);
93 return;
94 }
95 }
96 if (optind != argc-1) {
97 com_err(argv[0], 0, usage);
98 return;
99 }
100 if (check_fs_not_open(argv[0]))
101 return;
102 open_filesystem(argv[optind], open_flags);
103 }
104
105 static void close_filesystem(NOARGS)
106 {
107 int retval;
108
109 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
110 retval = ext2fs_write_inode_bitmap(current_fs);
111 if (retval)
112 com_err("ext2fs_write_inode_bitmap", retval, "");
113 }
114 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
115 retval = ext2fs_write_block_bitmap(current_fs);
116 if (retval)
117 com_err("ext2fs_write_block_bitmap", retval, "");
118 }
119 retval = ext2fs_close(current_fs);
120 if (retval)
121 com_err("ext2fs_close", retval, "");
122 current_fs = NULL;
123 return;
124 }
125
126 void do_close_filesys(int argc, char **argv)
127 {
128 if (argc > 1) {
129 com_err(argv[0], 0, "Usage: close_filesys");
130 return;
131 }
132 if (check_fs_open(argv[0]))
133 return;
134 close_filesystem();
135 }
136
137 void do_init_filesys(int argc, char **argv)
138 {
139 const char *usage = "Usage: initialize <device> <blocksize>";
140 struct ext2_super_block param;
141 errcode_t retval;
142 char *tmp;
143
144 if (argc != 3) {
145 com_err(argv[0], 0, usage);
146 return;
147 }
148 if (check_fs_not_open(argv[0]))
149 return;
150
151 memset(&param, 0, sizeof(struct ext2_super_block));
152 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
153 if (*tmp) {
154 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
155 return;
156 }
157 retval = ext2fs_initialize(argv[1], 0, &param,
158 unix_io_manager, &current_fs);
159 if (retval) {
160 com_err(argv[1], retval, "while initializing filesystem");
161 current_fs = NULL;
162 return;
163 }
164 root = cwd = EXT2_ROOT_INO;
165 return;
166 }
167
168 void do_show_super_stats(int argc, char *argv[])
169 {
170 int i;
171 FILE *out;
172 struct ext2fs_sb *sb;
173 struct ext2_group_desc *gdp;
174 char buf[80];
175 const char *none = "(none)";
176
177 if (argc > 1) {
178 com_err(argv[0], 0, "Usage: show_super");
179 return;
180 }
181 if (check_fs_open(argv[0]))
182 return;
183 out = open_pager();
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));
190 } else
191 strcpy(buf, none);
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));
196 } else
197 strcpy(buf, none);
198 fprintf(out, "Last mounted directory = %s\n", buf);
199 if (!uuid_is_null(sb->s_uuid))
200 uuid_unparse(sb->s_uuid, buf);
201 else
202 strcpy(buf, none);
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);
214 }
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" : "");
233
234 gdp = &current_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"
239 " %d free block%s, "
240 "%d free inode%s, "
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");
250 close_pager(out);
251 }
252
253 void do_dirty_filesys(int argc, char **argv)
254 {
255 if (check_fs_open(argv[0]))
256 return;
257
258 ext2fs_mark_super_dirty(current_fs);
259 }
260
261 struct list_blocks_struct {
262 FILE *f;
263 int total;
264 };
265
266 static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
267 void *private)
268 {
269 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
270
271 fprintf(lb->f, "%d ", *blocknr);
272 lb->total++;
273 return 0;
274 }
275
276
277 static void dump_blocks(FILE *f, ino_t inode)
278 {
279 struct list_blocks_struct lb;
280
281 fprintf(f, "BLOCKS:\n");
282 lb.total = 0;
283 lb.f = f;
284 ext2fs_block_iterate(current_fs, inode, 0, NULL,
285 list_blocks_proc, (void *)&lb);
286 if (lb.total)
287 fprintf(f, "\nTOTAL: %d\n", lb.total);
288 fprintf(f,"\n");
289 }
290
291
292 static void dump_inode(ino_t inode_num, struct ext2_inode inode)
293 {
294 const char *i_type;
295 FILE *out;
296 char frag, fsize;
297 int os = current_fs->super->s_creator_os;
298
299 out = open_pager();
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);
312 #else
313 fprintf(out, "Mode: %04o Flags: 0x%x Version/Generation: %d\n",
314 inode.i_mode & 0777, inode.i_flags, inode.i_generation);
315 #endif
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);
320 else {
321 __u64 i_size = (inode.i_size |
322 ((unsigned long long)inode.i_size_high << 32));
323
324 fprintf(out, "%lld\n", i_size);
325 }
326 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
327 fprintf(out,
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);
331 else
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,
335 inode.i_blocks);
336 switch (os) {
337 case EXT2_OS_LINUX:
338 frag = inode.osd2.linux2.l_i_frag;
339 fsize = inode.osd2.linux2.l_i_fsize;
340 break;
341 case EXT2_OS_HURD:
342 frag = inode.osd2.hurd2.h_i_frag;
343 fsize = inode.osd2.hurd2.h_i_fsize;
344 break;
345 case EXT2_OS_MASIX:
346 frag = inode.osd2.masix2.m_i_frag;
347 fsize = inode.osd2.masix2.m_i_fsize;
348 break;
349 default:
350 frag = fsize = 0;
351 }
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));
360 if (inode.i_dtime)
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);
365 else
366 dump_blocks(out, inode_num);
367 close_pager(out);
368 }
369
370
371 void do_stat(int argc, char *argv[])
372 {
373 ino_t inode;
374 struct ext2_inode inode_buf;
375 int retval;
376
377 if (argc != 2) {
378 com_err(argv[0], 0, "Usage: stat <file>");
379 return;
380 }
381 if (check_fs_open(argv[0]))
382 return;
383 inode = string_to_inode(argv[1]);
384 if (!inode)
385 return;
386
387 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
388 if (retval)
389 {
390 com_err(argv[0], retval, "while trying to read inode %d", inode);
391 return;
392 }
393
394 dump_inode(inode,inode_buf);
395 return;
396 }
397
398 void do_chroot(int argc, char *argv[])
399 {
400 ino_t inode;
401 int retval;
402
403 if (argc != 2) {
404 com_err(argv[0], 0, "Usage: chroot <file>");
405 return;
406 }
407 if (check_fs_open(argv[0]))
408 return;
409 inode = string_to_inode(argv[1]);
410 if (!inode)
411 return;
412
413 retval = ext2fs_check_directory(current_fs, inode);
414 if (retval) {
415 com_err(argv[1], retval, "");
416 return;
417 }
418 root = inode;
419 }
420
421 void do_clri(int argc, char *argv[])
422 {
423 ino_t inode;
424 int retval;
425 struct ext2_inode inode_buf;
426
427 if (argc != 2) {
428 com_err(argv[0], 0, "Usage: clri <file>");
429 return;
430 }
431 if (check_fs_open(argv[0]))
432 return;
433 if (check_fs_read_write(argv[0]))
434 return;
435 inode = string_to_inode(argv[1]);
436 if (!inode)
437 return;
438
439 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
440 if (retval) {
441 com_err(argv[0], retval, "while trying to read inode %d",
442 inode);
443 return;
444 }
445 memset(&inode_buf, 0, sizeof(inode_buf));
446 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
447 if (retval) {
448 com_err(argv[0], retval, "while trying to write inode %d",
449 inode);
450 return;
451 }
452 }
453
454 void do_freei(int argc, char *argv[])
455 {
456 ino_t inode;
457
458 if (argc != 2) {
459 com_err(argv[0], 0, "Usage: freei <file>");
460 return;
461 }
462 if (check_fs_open(argv[0]))
463 return;
464 if (check_fs_read_write(argv[0]))
465 return;
466 inode = string_to_inode(argv[1]);
467 if (!inode)
468 return;
469
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);
474 }
475
476 void do_seti(int argc, char *argv[])
477 {
478 ino_t inode;
479
480 if (argc != 2) {
481 com_err(argv[0], 0, "Usage: seti <file>");
482 return;
483 }
484 if (check_fs_open(argv[0]))
485 return;
486 if (check_fs_read_write(argv[0]))
487 return;
488 inode = string_to_inode(argv[1]);
489 if (!inode)
490 return;
491
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);
496 }
497
498 void do_testi(int argc, char *argv[])
499 {
500 ino_t inode;
501
502 if (argc != 2) {
503 com_err(argv[0], 0, "Usage: testi <file>");
504 return;
505 }
506 if (check_fs_open(argv[0]))
507 return;
508 inode = string_to_inode(argv[1]);
509 if (!inode)
510 return;
511
512 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
513 printf("Inode %ld is marked in use\n", inode);
514 else
515 printf("Inode %ld is not in use\n", inode);
516 }
517
518
519 void do_freeb(int argc, char *argv[])
520 {
521 blk_t block;
522 char *tmp;
523
524 if (argc != 2) {
525 com_err(argv[0], 0, "Usage: freeb <block>");
526 return;
527 }
528 if (check_fs_open(argv[0]))
529 return;
530 if (check_fs_read_write(argv[0]))
531 return;
532 block = strtoul(argv[1], &tmp, 0);
533 if (!block || *tmp) {
534 com_err(argv[0], 0, "No block 0");
535 return;
536 }
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);
541 }
542
543 void do_setb(int argc, char *argv[])
544 {
545 blk_t block;
546 char *tmp;
547
548 if (argc != 2) {
549 com_err(argv[0], 0, "Usage: setb <block>");
550 return;
551 }
552 if (check_fs_open(argv[0]))
553 return;
554 if (check_fs_read_write(argv[0]))
555 return;
556 block = strtoul(argv[1], &tmp, 0);
557 if (!block || *tmp) {
558 com_err(argv[0], 0, "No block 0");
559 return;
560 }
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);
565 }
566
567 void do_testb(int argc, char *argv[])
568 {
569 blk_t block;
570 char *tmp;
571
572 if (argc != 2) {
573 com_err(argv[0], 0, "Usage: testb <block>");
574 return;
575 }
576 if (check_fs_open(argv[0]))
577 return;
578 block = strtoul(argv[1], &tmp, 0);
579 if (!block || *tmp) {
580 com_err(argv[0], 0, "No block 0");
581 return;
582 }
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);
586 }
587
588 static void modify_u8(char *com, const char *prompt,
589 const char *format, __u8 *val)
590 {
591 char buf[200];
592 unsigned long v;
593 char *tmp;
594
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';
600 if (!buf[0])
601 return;
602 v = strtoul(buf, &tmp, 0);
603 if (*tmp)
604 com_err(com, 0, "Bad value - %s", buf);
605 else
606 *val = v;
607 }
608
609 static void modify_u16(char *com, const char *prompt,
610 const char *format, __u16 *val)
611 {
612 char buf[200];
613 unsigned long v;
614 char *tmp;
615
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';
621 if (!buf[0])
622 return;
623 v = strtoul(buf, &tmp, 0);
624 if (*tmp)
625 com_err(com, 0, "Bad value - %s", buf);
626 else
627 *val = v;
628 }
629
630 static void modify_u32(char *com, const char *prompt,
631 const char *format, __u32 *val)
632 {
633 char buf[200];
634 unsigned long v;
635 char *tmp;
636
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';
642 if (!buf[0])
643 return;
644 v = strtoul(buf, &tmp, 0);
645 if (*tmp)
646 com_err(com, 0, "Bad value - %s", buf);
647 else
648 *val = v;
649 }
650
651
652 void do_modify_inode(int argc, char *argv[])
653 {
654 struct ext2_inode inode;
655 ino_t inode_num;
656 int i;
657 errcode_t retval;
658 unsigned char *frag, *fsize;
659 char buf[80];
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";
664
665 if (argc != 2) {
666 com_err(argv[0], 0, "Usage: modify_inode <file>");
667 return;
668 }
669 if (check_fs_open(argv[0]))
670 return;
671 if (check_fs_read_write(argv[0]))
672 return;
673
674 inode_num = string_to_inode(argv[1]);
675 if (!inode_num)
676 return;
677
678 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
679 if (retval) {
680 com_err(argv[1], retval, "while trying to read inode %d",
681 inode_num);
682 return;
683 }
684
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);
696 #if 0
697 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
698 #endif
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);
702 else
703 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
704
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);
708
709 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
710 switch (os) {
711 case EXT2_OS_LINUX:
712 frag = &inode.osd2.linux2.l_i_frag;
713 fsize = &inode.osd2.linux2.l_i_fsize;
714 break;
715 case EXT2_OS_HURD:
716 frag = &inode.osd2.hurd2.h_i_frag;
717 fsize = &inode.osd2.hurd2.h_i_fsize;
718 break;
719 case EXT2_OS_MASIX:
720 frag = &inode.osd2.masix2.m_i_frag;
721 fsize = &inode.osd2.masix2.m_i_fsize;
722 break;
723 default:
724 frag = fsize = 0;
725 }
726 if (frag)
727 modify_u8(argv[0], "Fragment number", decimal_format, frag);
728 if (fsize)
729 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
730
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]);
734 }
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);
742 if (retval) {
743 com_err(argv[1], retval, "while trying to write inode %d",
744 inode_num);
745 return;
746 }
747 }
748
749 void do_change_working_dir(int argc, char *argv[])
750 {
751 ino_t inode;
752 int retval;
753
754 if (argc != 2) {
755 com_err(argv[0], 0, "Usage: cd <file>");
756 return;
757 }
758 if (check_fs_open(argv[0]))
759 return;
760
761 inode = string_to_inode(argv[1]);
762 if (!inode)
763 return;
764
765 retval = ext2fs_check_directory(current_fs, inode);
766 if (retval) {
767 com_err(argv[1], retval, "");
768 return;
769 }
770 cwd = inode;
771 return;
772 }
773
774 void do_print_working_directory(int argc, char *argv[])
775 {
776 int retval;
777 char *pathname = NULL;
778
779 if (argc > 1) {
780 com_err(argv[0], 0, "Usage: print_working_directory");
781 return;
782 }
783 if (check_fs_open(argv[0]))
784 return;
785
786 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
787 if (retval) {
788 com_err(argv[0], retval,
789 "while trying to get pathname of cwd");
790 }
791 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
792 free(pathname);
793 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
794 if (retval) {
795 com_err(argv[0], retval,
796 "while trying to get pathname of root");
797 }
798 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
799 free(pathname);
800 return;
801 }
802
803 static void make_link(char *sourcename, char *destname)
804 {
805 ino_t inode;
806 int retval;
807 ino_t dir;
808 char *dest, *cp, *basename;
809
810 /*
811 * Get the source inode
812 */
813 inode = string_to_inode(sourcename);
814 if (!inode)
815 return;
816 basename = strrchr(sourcename, '/');
817 if (basename)
818 basename++;
819 else
820 basename = sourcename;
821 /*
822 * Figure out the destination. First see if it exists and is
823 * a directory.
824 */
825 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
826 dest = basename;
827 else {
828 /*
829 * OK, it doesn't exist. See if it is
830 * '<dir>/basename' or 'basename'
831 */
832 cp = strrchr(destname, '/');
833 if (cp) {
834 *cp = 0;
835 dir = string_to_inode(destname);
836 if (!dir)
837 return;
838 dest = cp+1;
839 } else {
840 dir = cwd;
841 dest = destname;
842 }
843 }
844
845 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
846 if (retval)
847 com_err("make_link", retval, "");
848 return;
849 }
850
851
852 void do_link(int argc, char *argv[])
853 {
854 if (argc != 3) {
855 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
856 return;
857 }
858 if (check_fs_open(argv[0]))
859 return;
860
861 make_link(argv[1], argv[2]);
862 }
863
864 static void unlink_file_by_name(char *filename)
865 {
866 int retval;
867 ino_t dir;
868 char *basename;
869
870 basename = strrchr(filename, '/');
871 if (basename) {
872 *basename++ = '\0';
873 dir = string_to_inode(filename);
874 if (!dir)
875 return;
876 } else {
877 dir = cwd;
878 basename = filename;
879 }
880 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
881 if (retval)
882 com_err("unlink_file_by_name", retval, "");
883 return;
884 }
885
886 void do_unlink(int argc, char *argv[])
887 {
888 if (argc != 2) {
889 com_err(argv[0], 0, "Usage: unlink <pathname>");
890 return;
891 }
892 if (check_fs_open(argv[0]))
893 return;
894
895 unlink_file_by_name(argv[1]);
896 }
897
898 void do_find_free_block(int argc, char *argv[])
899 {
900 blk_t free_blk, goal;
901 errcode_t retval;
902 char *tmp;
903
904 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
905 com_err(argv[0], 0, "Usage: find_free_block [goal]");
906 return;
907 }
908 if (check_fs_open(argv[0]))
909 return;
910
911 if (argc > 1) {
912 goal = strtol(argv[1], &tmp, 0);
913 if (*tmp) {
914 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
915 return;
916 }
917 }
918 else
919 goal = current_fs->super->s_first_data_block;
920
921 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
922 if (retval)
923 com_err("ext2fs_new_block", retval, "");
924 else
925 printf("Free block found: %d\n", free_blk);
926
927 }
928
929 void do_find_free_inode(int argc, char *argv[])
930 {
931 ino_t free_inode, dir;
932 int mode;
933 int retval;
934 char *tmp;
935
936 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
937 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
938 return;
939 }
940 if (check_fs_open(argv[0]))
941 return;
942
943 if (argc > 1) {
944 dir = strtol(argv[1], &tmp, 0);
945 if (*tmp) {
946 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
947 return;
948 }
949 }
950 else
951 dir = root;
952 if (argc > 2) {
953 mode = strtol(argv[2], &tmp, 0);
954 if (*tmp) {
955 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
956 return;
957 }
958 } else
959 mode = 010755;
960
961 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
962 if (retval)
963 com_err("ext2fs_new_inode", retval, "");
964 else
965 printf("Free inode found: %ld\n", free_inode);
966 }
967
968 static errcode_t copy_file(int fd, ino_t newfile)
969 {
970 ext2_file_t e2_file;
971 errcode_t retval;
972 unsigned int got, written;
973 char buf[8192];
974 char *ptr;
975
976 retval = ext2fs_file_open(current_fs, newfile,
977 EXT2_FILE_WRITE, &e2_file);
978 if (retval)
979 return retval;
980
981 while (1) {
982 got = read(fd, buf, sizeof(buf));
983 if (got == 0)
984 break;
985 if (got < 0) {
986 retval = errno;
987 goto fail;
988 }
989 ptr = buf;
990 while (got > 0) {
991 retval = ext2fs_file_write(e2_file, ptr,
992 got, &written);
993 if (retval)
994 goto fail;
995
996 got -= written;
997 ptr += written;
998 }
999 }
1000 retval = ext2fs_file_close(e2_file);
1001
1002 return retval;
1003
1004 fail:
1005 (void) ext2fs_file_close(e2_file);
1006 return retval;
1007 }
1008
1009
1010 void do_write(int argc, char *argv[])
1011 {
1012 int fd;
1013 struct stat statbuf;
1014 ino_t newfile;
1015 errcode_t retval;
1016 struct ext2_inode inode;
1017 dgrp_t group;
1018
1019 if (check_fs_open(argv[0]))
1020 return;
1021 if (argc != 3) {
1022 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1023 return;
1024 }
1025 if (check_fs_read_write(argv[0]))
1026 return;
1027 fd = open(argv[1], O_RDONLY);
1028 if (fd < 0) {
1029 com_err(argv[1], fd, "");
1030 return;
1031 }
1032 if (fstat(fd, &statbuf) < 0) {
1033 com_err(argv[1], errno, "");
1034 close(fd);
1035 return;
1036 }
1037
1038 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1039 if (retval) {
1040 com_err(argv[0], retval, "");
1041 close(fd);
1042 return;
1043 }
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);
1050 if (retval) {
1051 com_err(argv[2], retval, "");
1052 close(fd);
1053 return;
1054 }
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);
1066 if (retval) {
1067 com_err(argv[0], retval, "while trying to write inode %d",
1068 inode);
1069 close(fd);
1070 return;
1071 }
1072 if (LINUX_S_ISREG(inode.i_mode)) {
1073 retval = copy_file(fd, newfile);
1074 if (retval)
1075 com_err("copy_file", retval, "");
1076 }
1077 close(fd);
1078 }
1079
1080 void do_mknod(int argc, char *argv[])
1081 {
1082 unsigned long mode, major, minor, nr;
1083 ino_t newfile;
1084 errcode_t retval;
1085 struct ext2_inode inode;
1086
1087 if (check_fs_open(argv[0]))
1088 return;
1089 if (argc < 3 || argv[2][1]) {
1090 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1091 return;
1092 }
1093 mode = minor = major = 0;
1094 switch (argv[2][0]) {
1095 case 'p':
1096 mode = LINUX_S_IFIFO;
1097 nr = 3;
1098 break;
1099 case 'c':
1100 mode = LINUX_S_IFCHR;
1101 nr = 5;
1102 break;
1103 case 'b':
1104 mode = LINUX_S_IFBLK;
1105 nr = 5;
1106 break;
1107 default:
1108 nr = 0;
1109 }
1110 if (nr == 5) {
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])
1114 nr = 0;
1115 }
1116 if (argc != nr) {
1117 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1118 return;
1119 }
1120 if (check_fs_read_write(argv[0]))
1121 return;
1122 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1123 if (retval) {
1124 com_err(argv[0], retval, "");
1125 return;
1126 }
1127 printf("Allocated inode: %ld\n", newfile);
1128 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
1129 if (retval) {
1130 if (retval == EXT2_ET_DIR_NO_SPACE) {
1131 retval = ext2fs_expand_dir(current_fs, cwd);
1132 if (!retval)
1133 retval = ext2fs_link(current_fs, cwd,
1134 argv[1], newfile, 0);
1135 }
1136 if (retval) {
1137 com_err(argv[1], retval, "");
1138 return;
1139 }
1140 }
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);
1152 if (retval) {
1153 com_err(argv[0], retval, "while trying to write inode %d", inode);
1154 return;
1155 }
1156 }
1157
1158 void do_mkdir(int argc, char *argv[])
1159 {
1160 char *cp;
1161 ino_t parent;
1162 char *name;
1163 errcode_t retval;
1164
1165 if (check_fs_open(argv[0]))
1166 return;
1167
1168 if (argc != 2) {
1169 com_err(argv[0], 0, "Usage: mkdir <file>");
1170 return;
1171 }
1172
1173 cp = strrchr(argv[1], '/');
1174 if (cp) {
1175 *cp = 0;
1176 parent = string_to_inode(argv[1]);
1177 if (!parent) {
1178 com_err(argv[1], ENOENT, "");
1179 return;
1180 }
1181 name = cp+1;
1182 } else {
1183 parent = cwd;
1184 name = argv[1];
1185 }
1186
1187
1188 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1189 if (retval) {
1190 com_err("ext2fs_mkdir", retval, "");
1191 return;
1192 }
1193
1194 }
1195
1196 void do_rmdir(int argc, char *argv[])
1197 {
1198 printf("Unimplemented\n");
1199 }
1200
1201
1202 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1203 int blockcnt, void *private)
1204 {
1205 printf("%d ", *blocknr);
1206 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
1207 return 0;
1208 }
1209
1210 static void kill_file_by_inode(ino_t inode)
1211 {
1212 struct ext2_inode inode_buf;
1213
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);
1217
1218 printf("Kill file by inode %ld\n", inode);
1219 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1220 release_blocks_proc, NULL);
1221 printf("\n");
1222 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
1223
1224 ext2fs_mark_bb_dirty(current_fs);
1225 ext2fs_mark_ib_dirty(current_fs);
1226 }
1227
1228
1229 void do_kill_file(int argc, char *argv[])
1230 {
1231 ino_t inode_num;
1232
1233 if (argc != 2) {
1234 com_err(argv[0], 0, "Usage: kill_file <file>");
1235 return;
1236 }
1237 if (check_fs_open(argv[0]))
1238 return;
1239
1240 inode_num = string_to_inode(argv[1]);
1241 if (!inode_num) {
1242 com_err(argv[0], 0, "Cannot find file");
1243 return;
1244 }
1245 kill_file_by_inode(inode_num);
1246 }
1247
1248 void do_rm(int argc, char *argv[])
1249 {
1250 int retval;
1251 ino_t inode_num;
1252 struct ext2_inode inode;
1253
1254 if (argc != 2) {
1255 com_err(argv[0], 0, "Usage: rm <filename>");
1256 return;
1257 }
1258 if (check_fs_open(argv[0]))
1259 return;
1260
1261 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1262 if (retval) {
1263 com_err(argv[0], retval, "while trying to resolve filename");
1264 return;
1265 }
1266
1267 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
1268 if (retval) {
1269 com_err(argv[0], retval, "while reading file's inode");
1270 return;
1271 }
1272
1273 if (LINUX_S_ISDIR(inode.i_mode)) {
1274 com_err(argv[0], 0, "file is a directory");
1275 return;
1276 }
1277
1278 --inode.i_links_count;
1279 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
1280 if (retval) {
1281 com_err(argv[0], retval, "while writing inode");
1282 return;
1283 }
1284
1285 unlink_file_by_name(argv[1]);
1286 if (inode.i_links_count == 0)
1287 kill_file_by_inode(inode_num);
1288 }
1289
1290 void do_show_debugfs_params(int argc, char *argv[])
1291 {
1292 FILE *out = stdout;
1293
1294 if (current_fs)
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--");
1299 }
1300
1301 void do_expand_dir(int argc, char *argv[])
1302 {
1303 ino_t inode;
1304 int retval;
1305
1306 if (argc != 2) {
1307 com_err(argv[0], 0, "Usage: expand_dir <file>");
1308 return;
1309 }
1310 if (check_fs_open(argv[0]))
1311 return;
1312 inode = string_to_inode(argv[1]);
1313 if (!inode)
1314 return;
1315
1316 retval = ext2fs_expand_dir(current_fs, inode);
1317 if (retval)
1318 com_err("ext2fs_expand_dir", retval, "");
1319 return;
1320 }
1321
1322 static int source_file(const char *cmd_file, int sci_idx)
1323 {
1324 FILE *f;
1325 char buf[256];
1326 char *cp;
1327 int exit_status = 0;
1328 int retval;
1329
1330 if (strcmp(cmd_file, "-") == 0)
1331 f = stdin;
1332 else {
1333 f = fopen(cmd_file, "r");
1334 if (!f) {
1335 perror(cmd_file);
1336 exit(1);
1337 }
1338 }
1339 setbuf(stdout, NULL);
1340 setbuf(stderr, NULL);
1341 while (!feof(f)) {
1342 if (fgets(buf, sizeof(buf), f) == NULL)
1343 break;
1344 cp = strchr(buf, '\n');
1345 if (cp)
1346 *cp = 0;
1347 cp = strchr(buf, '\r');
1348 if (cp)
1349 *cp = 0;
1350 printf("debugfs: %s\n", buf);
1351 retval = ss_execute_line(sci_idx, buf);
1352 if (retval) {
1353 ss_perror(sci_idx, retval, buf);
1354 exit_status++;
1355 }
1356 }
1357 return exit_status;
1358 }
1359
1360 int main(int argc, char **argv)
1361 {
1362 int retval;
1363 int sci_idx;
1364 const char *usage = "Usage: debugfs [-f cmd_file] [-R request] [-V] [[-w] device]";
1365 int c;
1366 int open_flags = 0;
1367 char *request = 0;
1368 int exit_status = 0;
1369 char *cmd_file = 0;
1370
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);
1375
1376 while ((c = getopt (argc, argv, "wR:f:V")) != EOF) {
1377 switch (c) {
1378 case 'R':
1379 request = optarg;
1380 break;
1381 case 'f':
1382 cmd_file = optarg;
1383 break;
1384 case 'w':
1385 open_flags = EXT2_FLAG_RW;
1386 break;
1387 case 'V':
1388 /* Print version number and exit */
1389 fprintf(stderr, "\tUsing %s\n",
1390 error_message(EXT2_ET_BASE));
1391 exit(0);
1392 default:
1393 com_err(argv[0], 0, usage);
1394 return 1;
1395 }
1396 }
1397 if (optind < argc)
1398 open_filesystem(argv[optind], open_flags);
1399
1400 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1401 &debug_cmds, &retval);
1402 if (retval) {
1403 ss_perror(sci_idx, retval, "creating invocation");
1404 exit(1);
1405 }
1406
1407 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1408 if (retval) {
1409 ss_perror(sci_idx, retval, "adding standard requests");
1410 exit (1);
1411 }
1412 if (request) {
1413 retval = 0;
1414 retval = ss_execute_line(sci_idx, request);
1415 if (retval) {
1416 ss_perror(sci_idx, retval, request);
1417 exit_status++;
1418 }
1419 } else if (cmd_file) {
1420 exit_status = source_file(cmd_file, sci_idx);
1421 } else {
1422 ss_listen(sci_idx);
1423 }
1424
1425 if (current_fs)
1426 close_filesystem();
1427
1428 exit(exit_status);
1429 }
1430