]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/debugfs.c
ChangeLog, debugfs.8.in, 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 fprintf(out, "Mode: %04o Flags: 0x%x Version: %d\n",
310 inode.i_mode & 0777, inode.i_flags, inode.i_version);
311 fprintf(out, "User: %5d Group: %5d Size: ",
312 inode.i_uid, inode.i_gid);
313 if (LINUX_S_ISDIR(inode.i_mode))
314 fprintf(out, "%d\n", inode.i_size);
315 else {
316 __u64 i_size = (inode.i_size |
317 ((unsigned long long)inode.i_size_high << 32));
318
319 fprintf(out, "%lld\n", i_size);
320 }
321 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
322 fprintf(out,
323 "File ACL: %d Directory ACL: %d Translator: %d\n",
324 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
325 inode.osd1.hurd1.h_i_translator);
326 else
327 fprintf(out, "File ACL: %d Directory ACL: %d\n",
328 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
329 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
330 inode.i_blocks);
331 switch (os) {
332 case EXT2_OS_LINUX:
333 frag = inode.osd2.linux2.l_i_frag;
334 fsize = inode.osd2.linux2.l_i_fsize;
335 break;
336 case EXT2_OS_HURD:
337 frag = inode.osd2.hurd2.h_i_frag;
338 fsize = inode.osd2.hurd2.h_i_fsize;
339 break;
340 case EXT2_OS_MASIX:
341 frag = inode.osd2.masix2.m_i_frag;
342 fsize = inode.osd2.masix2.m_i_fsize;
343 break;
344 default:
345 frag = fsize = 0;
346 }
347 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
348 inode.i_faddr, frag, fsize);
349 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
350 time_to_string(inode.i_ctime));
351 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
352 time_to_string(inode.i_atime));
353 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
354 time_to_string(inode.i_mtime));
355 if (inode.i_dtime)
356 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
357 time_to_string(inode.i_dtime));
358 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
359 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
360 else
361 dump_blocks(out, inode_num);
362 close_pager(out);
363 }
364
365
366 void do_stat(int argc, char *argv[])
367 {
368 ino_t inode;
369 struct ext2_inode inode_buf;
370 int retval;
371
372 if (argc != 2) {
373 com_err(argv[0], 0, "Usage: stat <file>");
374 return;
375 }
376 if (check_fs_open(argv[0]))
377 return;
378 inode = string_to_inode(argv[1]);
379 if (!inode)
380 return;
381
382 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
383 if (retval)
384 {
385 com_err(argv[0], 0, "Reading inode");
386 return;
387 }
388
389 dump_inode(inode,inode_buf);
390 return;
391 }
392
393 void do_chroot(int argc, char *argv[])
394 {
395 ino_t inode;
396 int retval;
397
398 if (argc != 2) {
399 com_err(argv[0], 0, "Usage: chroot <file>");
400 return;
401 }
402 if (check_fs_open(argv[0]))
403 return;
404 inode = string_to_inode(argv[1]);
405 if (!inode)
406 return;
407
408 retval = ext2fs_check_directory(current_fs, inode);
409 if (retval) {
410 com_err(argv[1], retval, "");
411 return;
412 }
413 root = inode;
414 }
415
416 void do_clri(int argc, char *argv[])
417 {
418 ino_t inode;
419 int retval;
420 struct ext2_inode inode_buf;
421
422 if (argc != 2) {
423 com_err(argv[0], 0, "Usage: clri <file>");
424 return;
425 }
426 if (check_fs_open(argv[0]))
427 return;
428 if (check_fs_read_write(argv[0]))
429 return;
430 inode = string_to_inode(argv[1]);
431 if (!inode)
432 return;
433
434 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
435 if (retval) {
436 com_err(argv[0], 0, "while trying to read inode %d", inode);
437 return;
438 }
439 memset(&inode_buf, 0, sizeof(inode_buf));
440 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
441 if (retval) {
442 com_err(argv[0], retval, "while trying to write inode %d",
443 inode);
444 return;
445 }
446 }
447
448 void do_freei(int argc, char *argv[])
449 {
450 ino_t inode;
451
452 if (argc != 2) {
453 com_err(argv[0], 0, "Usage: freei <file>");
454 return;
455 }
456 if (check_fs_open(argv[0]))
457 return;
458 if (check_fs_read_write(argv[0]))
459 return;
460 inode = string_to_inode(argv[1]);
461 if (!inode)
462 return;
463
464 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
465 com_err(argv[0], 0, "Warning: inode already clear");
466 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
467 ext2fs_mark_ib_dirty(current_fs);
468 }
469
470 void do_seti(int argc, char *argv[])
471 {
472 ino_t inode;
473
474 if (argc != 2) {
475 com_err(argv[0], 0, "Usage: seti <file>");
476 return;
477 }
478 if (check_fs_open(argv[0]))
479 return;
480 if (check_fs_read_write(argv[0]))
481 return;
482 inode = string_to_inode(argv[1]);
483 if (!inode)
484 return;
485
486 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
487 com_err(argv[0], 0, "Warning: inode already set");
488 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
489 ext2fs_mark_ib_dirty(current_fs);
490 }
491
492 void do_testi(int argc, char *argv[])
493 {
494 ino_t inode;
495
496 if (argc != 2) {
497 com_err(argv[0], 0, "Usage: testi <file>");
498 return;
499 }
500 if (check_fs_open(argv[0]))
501 return;
502 inode = string_to_inode(argv[1]);
503 if (!inode)
504 return;
505
506 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
507 printf("Inode %ld is marked in use\n", inode);
508 else
509 printf("Inode %ld is not in use\n", inode);
510 }
511
512
513 void do_freeb(int argc, char *argv[])
514 {
515 blk_t block;
516 char *tmp;
517
518 if (argc != 2) {
519 com_err(argv[0], 0, "Usage: freeb <block>");
520 return;
521 }
522 if (check_fs_open(argv[0]))
523 return;
524 if (check_fs_read_write(argv[0]))
525 return;
526 block = strtoul(argv[1], &tmp, 0);
527 if (!block || *tmp) {
528 com_err(argv[0], 0, "No block 0");
529 return;
530 }
531 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
532 com_err(argv[0], 0, "Warning: block already clear");
533 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
534 ext2fs_mark_bb_dirty(current_fs);
535 }
536
537 void do_setb(int argc, char *argv[])
538 {
539 blk_t block;
540 char *tmp;
541
542 if (argc != 2) {
543 com_err(argv[0], 0, "Usage: setb <block>");
544 return;
545 }
546 if (check_fs_open(argv[0]))
547 return;
548 if (check_fs_read_write(argv[0]))
549 return;
550 block = strtoul(argv[1], &tmp, 0);
551 if (!block || *tmp) {
552 com_err(argv[0], 0, "No block 0");
553 return;
554 }
555 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
556 com_err(argv[0], 0, "Warning: block already set");
557 ext2fs_mark_block_bitmap(current_fs->block_map,block);
558 ext2fs_mark_bb_dirty(current_fs);
559 }
560
561 void do_testb(int argc, char *argv[])
562 {
563 blk_t block;
564 char *tmp;
565
566 if (argc != 2) {
567 com_err(argv[0], 0, "Usage: testb <block>");
568 return;
569 }
570 if (check_fs_open(argv[0]))
571 return;
572 block = strtoul(argv[1], &tmp, 0);
573 if (!block || *tmp) {
574 com_err(argv[0], 0, "No block 0");
575 return;
576 }
577 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
578 printf("Block %d marked in use\n", block);
579 else printf("Block %d not in use\n", block);
580 }
581
582 static void modify_u8(char *com, const char *prompt,
583 const char *format, __u8 *val)
584 {
585 char buf[200];
586 unsigned long v;
587 char *tmp;
588
589 sprintf(buf, format, *val);
590 printf("%30s [%s] ", prompt, buf);
591 fgets(buf, sizeof(buf), stdin);
592 if (buf[strlen (buf) - 1] == '\n')
593 buf[strlen (buf) - 1] = '\0';
594 if (!buf[0])
595 return;
596 v = strtoul(buf, &tmp, 0);
597 if (*tmp)
598 com_err(com, 0, "Bad value - %s", buf);
599 else
600 *val = v;
601 }
602
603 static void modify_u16(char *com, const char *prompt,
604 const char *format, __u16 *val)
605 {
606 char buf[200];
607 unsigned long v;
608 char *tmp;
609
610 sprintf(buf, format, *val);
611 printf("%30s [%s] ", prompt, buf);
612 fgets(buf, sizeof(buf), stdin);
613 if (buf[strlen (buf) - 1] == '\n')
614 buf[strlen (buf) - 1] = '\0';
615 if (!buf[0])
616 return;
617 v = strtoul(buf, &tmp, 0);
618 if (*tmp)
619 com_err(com, 0, "Bad value - %s", buf);
620 else
621 *val = v;
622 }
623
624 static void modify_u32(char *com, const char *prompt,
625 const char *format, __u32 *val)
626 {
627 char buf[200];
628 unsigned long v;
629 char *tmp;
630
631 sprintf(buf, format, *val);
632 printf("%30s [%s] ", prompt, buf);
633 fgets(buf, sizeof(buf), stdin);
634 if (buf[strlen (buf) - 1] == '\n')
635 buf[strlen (buf) - 1] = '\0';
636 if (!buf[0])
637 return;
638 v = strtoul(buf, &tmp, 0);
639 if (*tmp)
640 com_err(com, 0, "Bad value - %s", buf);
641 else
642 *val = v;
643 }
644
645
646 void do_modify_inode(int argc, char *argv[])
647 {
648 struct ext2_inode inode;
649 ino_t inode_num;
650 int i;
651 errcode_t retval;
652 unsigned char *frag, *fsize;
653 char buf[80];
654 int os = current_fs->super->s_creator_os;
655 const char *hex_format = "0x%x";
656 const char *octal_format = "0%o";
657 const char *decimal_format = "%d";
658
659 if (argc != 2) {
660 com_err(argv[0], 0, "Usage: modify_inode <file>");
661 return;
662 }
663 if (check_fs_open(argv[0]))
664 return;
665 if (check_fs_read_write(argv[0]))
666 return;
667
668 inode_num = string_to_inode(argv[1]);
669 if (!inode_num)
670 return;
671
672 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
673 if (retval) {
674 com_err(argv[1], retval, "while trying to read inode %d",
675 inode_num);
676 return;
677 }
678
679 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
680 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
681 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
682 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
683 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
684 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
685 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
686 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
687 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
688 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
689 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
690 #if 0
691 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
692 #endif
693 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
694 if (LINUX_S_ISDIR(inode.i_mode))
695 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
696 else
697 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
698
699 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
700 modify_u32(argv[0], "Translator Block",
701 decimal_format, &inode.osd1.hurd1.h_i_translator);
702
703 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
704 switch (os) {
705 case EXT2_OS_LINUX:
706 frag = &inode.osd2.linux2.l_i_frag;
707 fsize = &inode.osd2.linux2.l_i_fsize;
708 break;
709 case EXT2_OS_HURD:
710 frag = &inode.osd2.hurd2.h_i_frag;
711 fsize = &inode.osd2.hurd2.h_i_fsize;
712 break;
713 case EXT2_OS_MASIX:
714 frag = &inode.osd2.masix2.m_i_frag;
715 fsize = &inode.osd2.masix2.m_i_fsize;
716 break;
717 default:
718 frag = fsize = 0;
719 }
720 if (frag)
721 modify_u8(argv[0], "Fragment number", decimal_format, frag);
722 if (fsize)
723 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
724
725 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
726 sprintf(buf, "Direct Block #%d", i);
727 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
728 }
729 modify_u32(argv[0], "Indirect Block", decimal_format,
730 &inode.i_block[EXT2_IND_BLOCK]);
731 modify_u32(argv[0], "Double Indirect Block", decimal_format,
732 &inode.i_block[EXT2_DIND_BLOCK]);
733 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
734 &inode.i_block[EXT2_TIND_BLOCK]);
735 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
736 if (retval) {
737 com_err(argv[1], retval, "while trying to write inode %d",
738 inode_num);
739 return;
740 }
741 }
742
743 void do_change_working_dir(int argc, char *argv[])
744 {
745 ino_t inode;
746 int retval;
747
748 if (argc != 2) {
749 com_err(argv[0], 0, "Usage: cd <file>");
750 return;
751 }
752 if (check_fs_open(argv[0]))
753 return;
754
755 inode = string_to_inode(argv[1]);
756 if (!inode)
757 return;
758
759 retval = ext2fs_check_directory(current_fs, inode);
760 if (retval) {
761 com_err(argv[1], retval, "");
762 return;
763 }
764 cwd = inode;
765 return;
766 }
767
768 void do_print_working_directory(int argc, char *argv[])
769 {
770 int retval;
771 char *pathname = NULL;
772
773 if (argc > 1) {
774 com_err(argv[0], 0, "Usage: print_working_directory");
775 return;
776 }
777 if (check_fs_open(argv[0]))
778 return;
779
780 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
781 if (retval) {
782 com_err(argv[0], retval,
783 "while trying to get pathname of cwd");
784 }
785 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
786 free(pathname);
787 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
788 if (retval) {
789 com_err(argv[0], retval,
790 "while trying to get pathname of root");
791 }
792 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
793 free(pathname);
794 return;
795 }
796
797 static void make_link(char *sourcename, char *destname)
798 {
799 ino_t inode;
800 int retval;
801 ino_t dir;
802 char *dest, *cp, *basename;
803
804 /*
805 * Get the source inode
806 */
807 inode = string_to_inode(sourcename);
808 if (!inode)
809 return;
810 basename = strrchr(sourcename, '/');
811 if (basename)
812 basename++;
813 else
814 basename = sourcename;
815 /*
816 * Figure out the destination. First see if it exists and is
817 * a directory.
818 */
819 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
820 dest = basename;
821 else {
822 /*
823 * OK, it doesn't exist. See if it is
824 * '<dir>/basename' or 'basename'
825 */
826 cp = strrchr(destname, '/');
827 if (cp) {
828 *cp = 0;
829 dir = string_to_inode(destname);
830 if (!dir)
831 return;
832 dest = cp+1;
833 } else {
834 dir = cwd;
835 dest = destname;
836 }
837 }
838
839 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
840 if (retval)
841 com_err("make_link", retval, "");
842 return;
843 }
844
845
846 void do_link(int argc, char *argv[])
847 {
848 if (argc != 3) {
849 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
850 return;
851 }
852 if (check_fs_open(argv[0]))
853 return;
854
855 make_link(argv[1], argv[2]);
856 }
857
858 static void unlink_file_by_name(char *filename)
859 {
860 int retval;
861 ino_t dir;
862 char *basename;
863
864 basename = strrchr(filename, '/');
865 if (basename) {
866 *basename++ = '\0';
867 dir = string_to_inode(filename);
868 if (!dir)
869 return;
870 } else {
871 dir = cwd;
872 basename = filename;
873 }
874 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
875 if (retval)
876 com_err("unlink_file_by_name", retval, "");
877 return;
878 }
879
880 void do_unlink(int argc, char *argv[])
881 {
882 if (argc != 2) {
883 com_err(argv[0], 0, "Usage: unlink <pathname>");
884 return;
885 }
886 if (check_fs_open(argv[0]))
887 return;
888
889 unlink_file_by_name(argv[1]);
890 }
891
892 void do_find_free_block(int argc, char *argv[])
893 {
894 blk_t free_blk, goal;
895 errcode_t retval;
896 char *tmp;
897
898 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
899 com_err(argv[0], 0, "Usage: find_free_block [goal]");
900 return;
901 }
902 if (check_fs_open(argv[0]))
903 return;
904
905 if (argc > 1) {
906 goal = strtol(argv[1], &tmp, 0);
907 if (*tmp) {
908 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
909 return;
910 }
911 }
912 else
913 goal = current_fs->super->s_first_data_block;
914
915 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
916 if (retval)
917 com_err("ext2fs_new_block", retval, "");
918 else
919 printf("Free block found: %d\n", free_blk);
920
921 }
922
923 void do_find_free_inode(int argc, char *argv[])
924 {
925 ino_t free_inode, dir;
926 int mode;
927 int retval;
928 char *tmp;
929
930 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
931 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
932 return;
933 }
934 if (check_fs_open(argv[0]))
935 return;
936
937 if (argc > 1) {
938 dir = strtol(argv[1], &tmp, 0);
939 if (*tmp) {
940 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
941 return;
942 }
943 }
944 else
945 dir = root;
946 if (argc > 2) {
947 mode = strtol(argv[2], &tmp, 0);
948 if (*tmp) {
949 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
950 return;
951 }
952 } else
953 mode = 010755;
954
955 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
956 if (retval)
957 com_err("ext2fs_new_inode", retval, "");
958 else
959 printf("Free inode found: %ld\n", free_inode);
960 }
961
962 static errcode_t copy_file(int fd, ino_t newfile)
963 {
964 ext2_file_t e2_file;
965 errcode_t retval;
966 unsigned int got, written;
967 char buf[8192];
968 char *ptr;
969
970 retval = ext2fs_file_open(current_fs, newfile,
971 EXT2_FILE_WRITE, &e2_file);
972 if (retval)
973 return retval;
974
975 while (1) {
976 got = read(fd, buf, sizeof(buf));
977 if (got == 0)
978 break;
979 if (got < 0) {
980 retval = errno;
981 goto fail;
982 }
983 ptr = buf;
984 while (got > 0) {
985 retval = ext2fs_file_write(e2_file, ptr,
986 got, &written);
987 if (retval)
988 goto fail;
989
990 got -= written;
991 ptr += written;
992 }
993 }
994 retval = ext2fs_file_close(e2_file);
995
996 return retval;
997
998 fail:
999 (void) ext2fs_file_close(e2_file);
1000 return retval;
1001 }
1002
1003
1004 void do_write(int argc, char *argv[])
1005 {
1006 int fd;
1007 struct stat statbuf;
1008 ino_t newfile;
1009 errcode_t retval;
1010 struct ext2_inode inode;
1011 dgrp_t group;
1012
1013 if (check_fs_open(argv[0]))
1014 return;
1015 if (argc != 3) {
1016 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1017 return;
1018 }
1019 if (check_fs_read_write(argv[0]))
1020 return;
1021 fd = open(argv[1], O_RDONLY);
1022 if (fd < 0) {
1023 com_err(argv[1], fd, "");
1024 return;
1025 }
1026 if (fstat(fd, &statbuf) < 0) {
1027 com_err(argv[1], errno, "");
1028 close(fd);
1029 return;
1030 }
1031
1032 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1033 if (retval) {
1034 com_err(argv[0], retval, "");
1035 close(fd);
1036 return;
1037 }
1038 group = ext2fs_group_of_ino(current_fs, newfile);
1039 current_fs->group_desc[group].bg_free_inodes_count--;
1040 current_fs->super->s_free_inodes_count--;
1041 ext2fs_mark_super_dirty(current_fs);
1042 printf("Allocated inode: %ld\n", newfile);
1043 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
1044 if (retval) {
1045 com_err(argv[2], retval, "");
1046 close(fd);
1047 return;
1048 }
1049 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1050 com_err(argv[0], 0, "Warning: inode already set");
1051 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1052 ext2fs_mark_ib_dirty(current_fs);
1053 memset(&inode, 0, sizeof(inode));
1054 inode.i_mode = statbuf.st_mode;
1055 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1056 inode.i_links_count = 1;
1057 inode.i_size = statbuf.st_size;
1058 ext2fs_write_inode(current_fs, newfile, &inode);
1059 retval = ext2fs_write_inode(current_fs, newfile, &inode);
1060 if (retval) {
1061 com_err(argv[0], retval, "while trying to write inode %d",
1062 inode);
1063 close(fd);
1064 return;
1065 }
1066 if (LINUX_S_ISREG(inode.i_mode)) {
1067 retval = copy_file(fd, newfile);
1068 if (retval)
1069 com_err("copy_file", retval, "");
1070 }
1071 close(fd);
1072 }
1073
1074 void do_mknod(int argc, char *argv[])
1075 {
1076 unsigned long mode, major, minor, nr;
1077 ino_t newfile;
1078 errcode_t retval;
1079 struct ext2_inode inode;
1080
1081 if (check_fs_open(argv[0]))
1082 return;
1083 if (argc < 3 || argv[2][1]) {
1084 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1085 return;
1086 }
1087 mode = minor = major = 0;
1088 switch (argv[2][0]) {
1089 case 'p':
1090 mode = LINUX_S_IFIFO;
1091 nr = 3;
1092 break;
1093 case 'c':
1094 mode = LINUX_S_IFCHR;
1095 nr = 5;
1096 break;
1097 case 'b':
1098 mode = LINUX_S_IFBLK;
1099 nr = 5;
1100 break;
1101 default:
1102 nr = 0;
1103 }
1104 if (nr == 5) {
1105 major = strtoul(argv[3], argv+3, 0);
1106 minor = strtoul(argv[4], argv+4, 0);
1107 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1108 nr = 0;
1109 }
1110 if (argc != nr) {
1111 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1112 return;
1113 }
1114 if (check_fs_read_write(argv[0]))
1115 return;
1116 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1117 if (retval) {
1118 com_err(argv[0], retval, "");
1119 return;
1120 }
1121 printf("Allocated inode: %ld\n", newfile);
1122 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
1123 if (retval) {
1124 if (retval == EXT2_ET_DIR_NO_SPACE) {
1125 retval = ext2fs_expand_dir(current_fs, cwd);
1126 if (!retval)
1127 retval = ext2fs_link(current_fs, cwd,
1128 argv[1], newfile, 0);
1129 }
1130 if (retval) {
1131 com_err(argv[1], retval, "");
1132 return;
1133 }
1134 }
1135 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1136 com_err(argv[0], 0, "Warning: inode already set");
1137 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1138 ext2fs_mark_ib_dirty(current_fs);
1139 memset(&inode, 0, sizeof(inode));
1140 inode.i_mode = mode;
1141 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1142 inode.i_block[0] = major*256+minor;
1143 inode.i_links_count = 1;
1144 ext2fs_write_inode(current_fs, newfile, &inode);
1145 retval = ext2fs_write_inode(current_fs, newfile, &inode);
1146 if (retval) {
1147 com_err(argv[0], retval, "while trying to write inode %d", inode);
1148 return;
1149 }
1150 }
1151
1152 void do_mkdir(int argc, char *argv[])
1153 {
1154 char *cp;
1155 ino_t parent;
1156 char *name;
1157 errcode_t retval;
1158
1159 if (check_fs_open(argv[0]))
1160 return;
1161
1162 if (argc != 2) {
1163 com_err(argv[0], 0, "Usage: mkdir <file>");
1164 return;
1165 }
1166
1167 cp = strrchr(argv[1], '/');
1168 if (cp) {
1169 *cp = 0;
1170 parent = string_to_inode(argv[1]);
1171 if (!parent) {
1172 com_err(argv[1], ENOENT, "");
1173 return;
1174 }
1175 name = cp+1;
1176 } else {
1177 parent = cwd;
1178 name = argv[1];
1179 }
1180
1181
1182 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1183 if (retval) {
1184 com_err("ext2fs_mkdir", retval, "");
1185 return;
1186 }
1187
1188 }
1189
1190 void do_rmdir(int argc, char *argv[])
1191 {
1192 printf("Unimplemented\n");
1193 }
1194
1195
1196 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1197 int blockcnt, void *private)
1198 {
1199 printf("%d ", *blocknr);
1200 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
1201 return 0;
1202 }
1203
1204 static void kill_file_by_inode(ino_t inode)
1205 {
1206 struct ext2_inode inode_buf;
1207
1208 ext2fs_read_inode(current_fs, inode, &inode_buf);
1209 inode_buf.i_dtime = time(NULL);
1210 ext2fs_write_inode(current_fs, inode, &inode_buf);
1211
1212 printf("Kill file by inode %ld\n", inode);
1213 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1214 release_blocks_proc, NULL);
1215 printf("\n");
1216 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
1217
1218 ext2fs_mark_bb_dirty(current_fs);
1219 ext2fs_mark_ib_dirty(current_fs);
1220 }
1221
1222
1223 void do_kill_file(int argc, char *argv[])
1224 {
1225 ino_t inode_num;
1226
1227 if (argc != 2) {
1228 com_err(argv[0], 0, "Usage: kill_file <file>");
1229 return;
1230 }
1231 if (check_fs_open(argv[0]))
1232 return;
1233
1234 inode_num = string_to_inode(argv[1]);
1235 if (!inode_num) {
1236 com_err(argv[0], 0, "Cannot find file");
1237 return;
1238 }
1239 kill_file_by_inode(inode_num);
1240 }
1241
1242 void do_rm(int argc, char *argv[])
1243 {
1244 int retval;
1245 ino_t inode_num;
1246 struct ext2_inode inode;
1247
1248 if (argc != 2) {
1249 com_err(argv[0], 0, "Usage: rm <filename>");
1250 return;
1251 }
1252 if (check_fs_open(argv[0]))
1253 return;
1254
1255 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1256 if (retval) {
1257 com_err(argv[0], 0, "Cannot find file");
1258 return;
1259 }
1260
1261 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
1262 if (retval) {
1263 com_err(argv[0], retval, "while reading file's inode");
1264 return;
1265 }
1266
1267 if (LINUX_S_ISDIR(inode.i_mode)) {
1268 com_err(argv[0], 0, "file is a directory");
1269 return;
1270 }
1271
1272 --inode.i_links_count;
1273 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
1274 if (retval) {
1275 com_err(argv[0], retval, "while writing inode");
1276 return;
1277 }
1278
1279 unlink_file_by_name(argv[1]);
1280 if (inode.i_links_count == 0)
1281 kill_file_by_inode(inode_num);
1282 }
1283
1284 void do_show_debugfs_params(int argc, char *argv[])
1285 {
1286 FILE *out = stdout;
1287
1288 if (current_fs)
1289 fprintf(out, "Open mode: read-%s\n",
1290 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1291 fprintf(out, "Filesystem in use: %s\n",
1292 current_fs ? current_fs->device_name : "--none--");
1293 }
1294
1295 void do_expand_dir(int argc, char *argv[])
1296 {
1297 ino_t inode;
1298 int retval;
1299
1300 if (argc != 2) {
1301 com_err(argv[0], 0, "Usage: expand_dir <file>");
1302 return;
1303 }
1304 if (check_fs_open(argv[0]))
1305 return;
1306 inode = string_to_inode(argv[1]);
1307 if (!inode)
1308 return;
1309
1310 retval = ext2fs_expand_dir(current_fs, inode);
1311 if (retval)
1312 com_err("ext2fs_expand_dir", retval, "");
1313 return;
1314 }
1315
1316 static int source_file(const char *cmd_file, int sci_idx)
1317 {
1318 FILE *f;
1319 char buf[256];
1320 char *cp;
1321 int exit_status = 0;
1322 int retval;
1323
1324 if (strcmp(cmd_file, "-") == 0)
1325 f = stdin;
1326 else {
1327 f = fopen(cmd_file, "r");
1328 if (!f) {
1329 perror(cmd_file);
1330 exit(1);
1331 }
1332 }
1333 setbuf(stdout, NULL);
1334 setbuf(stderr, NULL);
1335 while (!feof(f)) {
1336 if (fgets(buf, sizeof(buf), f) == NULL)
1337 break;
1338 cp = strchr(buf, '\n');
1339 if (cp)
1340 *cp = 0;
1341 cp = strchr(buf, '\r');
1342 if (cp)
1343 *cp = 0;
1344 printf("debugfs: %s\n", buf);
1345 retval = ss_execute_line(sci_idx, buf);
1346 if (retval) {
1347 ss_perror(sci_idx, retval, buf);
1348 exit_status++;
1349 }
1350 }
1351 return exit_status;
1352 }
1353
1354 int main(int argc, char **argv)
1355 {
1356 int retval;
1357 int sci_idx;
1358 const char *usage = "Usage: debugfs [-f cmd_file] [-R request] [-V] [[-w] device]";
1359 int c;
1360 int open_flags = 0;
1361 char *request = 0;
1362 int exit_status = 0;
1363 char *cmd_file = 0;
1364
1365 initialize_ext2_error_table();
1366 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1367 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1368 EXT2FS_VERSION, EXT2FS_DATE);
1369
1370 while ((c = getopt (argc, argv, "wR:f:V")) != EOF) {
1371 switch (c) {
1372 case 'R':
1373 request = optarg;
1374 break;
1375 case 'f':
1376 cmd_file = optarg;
1377 break;
1378 case 'w':
1379 open_flags = EXT2_FLAG_RW;
1380 break;
1381 case 'V':
1382 /* Print version number and exit */
1383 fprintf(stderr, "\tUsing %s\n",
1384 error_message(EXT2_ET_BASE));
1385 exit(0);
1386 default:
1387 com_err(argv[0], 0, usage);
1388 return 1;
1389 }
1390 }
1391 if (optind < argc)
1392 open_filesystem(argv[optind], open_flags);
1393
1394 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1395 &debug_cmds, &retval);
1396 if (retval) {
1397 ss_perror(sci_idx, retval, "creating invocation");
1398 exit(1);
1399 }
1400
1401 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1402 if (retval) {
1403 ss_perror(sci_idx, retval, "adding standard requests");
1404 exit (1);
1405 }
1406 if (request) {
1407 retval = 0;
1408 retval = ss_execute_line(sci_idx, request);
1409 if (retval) {
1410 ss_perror(sci_idx, retval, request);
1411 exit_status++;
1412 }
1413 } else if (cmd_file) {
1414 exit_status = source_file(cmd_file, sci_idx);
1415 } else {
1416 ss_listen(sci_idx);
1417 }
1418
1419 if (current_fs)
1420 close_filesystem();
1421
1422 exit(exit_status);
1423 }
1424