]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/debugfs.c
e2fsck: use problem_t to suppress some -Wconversion warnings
[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 "config.h"
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <time.h>
18 #ifdef HAVE_GETOPT_H
19 #include <getopt.h>
20 #else
21 extern int optind;
22 extern char *optarg;
23 #endif
24 #ifdef HAVE_ERRNO_H
25 #include <errno.h>
26 #endif
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 #include "debugfs.h"
32 #include "uuid/uuid.h"
33 #include "e2p/e2p.h"
34
35 #include <ext2fs/ext2_ext_attr.h>
36
37 #include "../version.h"
38 #include "jfs_user.h"
39
40 #ifndef BUFSIZ
41 #define BUFSIZ 8192
42 #endif
43
44 /* 64KiB is the minimium blksize to best minimize system call overhead. */
45 #ifndef IO_BUFSIZE
46 #define IO_BUFSIZE 64*1024
47 #endif
48
49 /* Block size for `st_blocks' */
50 #ifndef S_BLKSIZE
51 #define S_BLKSIZE 512
52 #endif
53
54 ss_request_table *extra_cmds;
55 const char *debug_prog_name;
56 int sci_idx;
57
58 ext2_filsys current_fs = NULL;
59 ext2_ino_t root, cwd;
60
61 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
62 blk64_t blocksize, int catastrophic,
63 char *data_filename)
64 {
65 int retval;
66 io_channel data_io = 0;
67
68 if (superblock != 0 && blocksize == 0) {
69 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
70 current_fs = NULL;
71 return;
72 }
73
74 if (data_filename) {
75 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
76 com_err(device, 0,
77 "The -d option is only valid when reading an e2image file");
78 current_fs = NULL;
79 return;
80 }
81 retval = unix_io_manager->open(data_filename, 0, &data_io);
82 if (retval) {
83 com_err(data_filename, 0, "while opening data source");
84 current_fs = NULL;
85 return;
86 }
87 }
88
89 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
90 com_err(device, 0,
91 "opening read-only because of catastrophic mode");
92 open_flags &= ~EXT2_FLAG_RW;
93 }
94 if (catastrophic)
95 open_flags |= EXT2_FLAG_SKIP_MMP;
96
97 retval = ext2fs_open(device, open_flags, superblock, blocksize,
98 unix_io_manager, &current_fs);
99 if (retval) {
100 com_err(device, retval, "while opening filesystem");
101 current_fs = NULL;
102 return;
103 }
104 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
105
106 if (catastrophic)
107 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
108 else {
109 retval = ext2fs_read_inode_bitmap(current_fs);
110 if (retval) {
111 com_err(device, retval, "while reading inode bitmap");
112 goto errout;
113 }
114 retval = ext2fs_read_block_bitmap(current_fs);
115 if (retval) {
116 com_err(device, retval, "while reading block bitmap");
117 goto errout;
118 }
119 }
120
121 if (data_io) {
122 retval = ext2fs_set_data_io(current_fs, data_io);
123 if (retval) {
124 com_err(device, retval,
125 "while setting data source");
126 goto errout;
127 }
128 }
129
130 root = cwd = EXT2_ROOT_INO;
131 return;
132
133 errout:
134 retval = ext2fs_close(current_fs);
135 if (retval)
136 com_err(device, retval, "while trying to close filesystem");
137 current_fs = NULL;
138 }
139
140 void do_open_filesys(int argc, char **argv)
141 {
142 int c, err;
143 int catastrophic = 0;
144 blk64_t superblock = 0;
145 blk64_t blocksize = 0;
146 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
147 char *data_filename = 0;
148
149 reset_getopt();
150 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
151 switch (c) {
152 case 'i':
153 open_flags |= EXT2_FLAG_IMAGE_FILE;
154 break;
155 case 'w':
156 #ifdef READ_ONLY
157 goto print_usage;
158 #else
159 open_flags |= EXT2_FLAG_RW;
160 #endif /* READ_ONLY */
161 break;
162 case 'f':
163 open_flags |= EXT2_FLAG_FORCE;
164 break;
165 case 'e':
166 open_flags |= EXT2_FLAG_EXCLUSIVE;
167 break;
168 case 'c':
169 catastrophic = 1;
170 break;
171 case 'd':
172 data_filename = optarg;
173 break;
174 case 'D':
175 open_flags |= EXT2_FLAG_DIRECT_IO;
176 break;
177 case 'b':
178 blocksize = parse_ulong(optarg, argv[0],
179 "block size", &err);
180 if (err)
181 return;
182 break;
183 case 's':
184 superblock = parse_ulong(optarg, argv[0],
185 "superblock number", &err);
186 if (err)
187 return;
188 break;
189 default:
190 goto print_usage;
191 }
192 }
193 if (optind != argc-1) {
194 goto print_usage;
195 }
196 if (check_fs_not_open(argv[0]))
197 return;
198 open_filesystem(argv[optind], open_flags,
199 superblock, blocksize, catastrophic,
200 data_filename);
201 return;
202
203 print_usage:
204 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] [-c] "
205 #ifndef READ_ONLY
206 "[-w] "
207 #endif
208 "<device>\n", argv[0]);
209 }
210
211 void do_lcd(int argc, char **argv)
212 {
213 if (argc != 2) {
214 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
215 return;
216 }
217
218 if (chdir(argv[1]) == -1) {
219 com_err(argv[0], errno,
220 "while trying to change native directory to %s",
221 argv[1]);
222 return;
223 }
224 }
225
226 static void close_filesystem(NOARGS)
227 {
228 int retval;
229
230 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
231 retval = ext2fs_write_inode_bitmap(current_fs);
232 if (retval)
233 com_err("ext2fs_write_inode_bitmap", retval, 0);
234 }
235 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
236 retval = ext2fs_write_block_bitmap(current_fs);
237 if (retval)
238 com_err("ext2fs_write_block_bitmap", retval, 0);
239 }
240 retval = ext2fs_close(current_fs);
241 if (retval)
242 com_err("ext2fs_close", retval, 0);
243 current_fs = NULL;
244 return;
245 }
246
247 void do_close_filesys(int argc, char **argv)
248 {
249 int c;
250
251 if (check_fs_open(argv[0]))
252 return;
253
254 reset_getopt();
255 while ((c = getopt (argc, argv, "a")) != EOF) {
256 switch (c) {
257 case 'a':
258 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
259 break;
260 default:
261 goto print_usage;
262 }
263 }
264
265 if (argc > optind) {
266 print_usage:
267 com_err(0, 0, "Usage: close_filesys [-a]");
268 return;
269 }
270
271 close_filesystem();
272 }
273
274 #ifndef READ_ONLY
275 void do_init_filesys(int argc, char **argv)
276 {
277 struct ext2_super_block param;
278 errcode_t retval;
279 int err;
280
281 if (common_args_process(argc, argv, 3, 3, "initialize",
282 "<device> <blocksize>", CHECK_FS_NOTOPEN))
283 return;
284
285 memset(&param, 0, sizeof(struct ext2_super_block));
286 ext2fs_blocks_count_set(&param, parse_ulong(argv[2], argv[0],
287 "blocks count", &err));
288 if (err)
289 return;
290 retval = ext2fs_initialize(argv[1], 0, &param,
291 unix_io_manager, &current_fs);
292 if (retval) {
293 com_err(argv[1], retval, "while initializing filesystem");
294 current_fs = NULL;
295 return;
296 }
297 root = cwd = EXT2_ROOT_INO;
298 return;
299 }
300
301 static void print_features(struct ext2_super_block * s, FILE *f)
302 {
303 int i, j, printed=0;
304 __u32 *mask = &s->s_feature_compat, m;
305
306 fputs("Filesystem features:", f);
307 for (i=0; i <3; i++,mask++) {
308 for (j=0,m=1; j < 32; j++, m<<=1) {
309 if (*mask & m) {
310 fprintf(f, " %s", e2p_feature2string(i, m));
311 printed++;
312 }
313 }
314 }
315 if (printed == 0)
316 fputs("(none)", f);
317 fputs("\n", f);
318 }
319 #endif /* READ_ONLY */
320
321 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
322 const char *str, int *first, FILE *f)
323 {
324 if (ext2fs_bg_flags_test(fs, group, mask)) {
325 if (*first) {
326 fputs(" [", f);
327 *first = 0;
328 } else
329 fputs(", ", f);
330 fputs(str, f);
331 }
332 }
333
334 void do_show_super_stats(int argc, char *argv[])
335 {
336 const char *units ="block";
337 dgrp_t i;
338 FILE *out;
339 int c, header_only = 0;
340 int numdirs = 0, first, gdt_csum;
341
342 reset_getopt();
343 while ((c = getopt (argc, argv, "h")) != EOF) {
344 switch (c) {
345 case 'h':
346 header_only++;
347 break;
348 default:
349 goto print_usage;
350 }
351 }
352 if (optind != argc) {
353 goto print_usage;
354 }
355 if (check_fs_open(argv[0]))
356 return;
357 out = open_pager();
358
359 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
360 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
361 units = "cluster";
362
363 list_super2(current_fs->super, out);
364 for (i=0; i < current_fs->group_desc_count; i++)
365 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
366 fprintf(out, "Directories: %d\n", numdirs);
367
368 if (header_only) {
369 close_pager(out);
370 return;
371 }
372
373 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
374 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
375 for (i = 0; i < current_fs->group_desc_count; i++) {
376 fprintf(out, " Group %2d: block bitmap at %llu, "
377 "inode bitmap at %llu, "
378 "inode table at %llu\n"
379 " %u free %s%s, "
380 "%u free %s, "
381 "%u used %s%s",
382 i, ext2fs_block_bitmap_loc(current_fs, i),
383 ext2fs_inode_bitmap_loc(current_fs, i),
384 ext2fs_inode_table_loc(current_fs, i),
385 ext2fs_bg_free_blocks_count(current_fs, i), units,
386 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
387 "s" : "",
388 ext2fs_bg_free_inodes_count(current_fs, i),
389 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
390 "inodes" : "inode",
391 ext2fs_bg_used_dirs_count(current_fs, i),
392 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
393 : "directory", gdt_csum ? ", " : "\n");
394 if (gdt_csum)
395 fprintf(out, "%u unused %s\n",
396 ext2fs_bg_itable_unused(current_fs, i),
397 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
398 "inodes" : "inode");
399 first = 1;
400 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
401 &first, out);
402 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
403 &first, out);
404 if (gdt_csum) {
405 fprintf(out, "%sChecksum 0x%04x",
406 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
407 first = 0;
408 }
409 if (!first)
410 fputs("]\n", out);
411 }
412 close_pager(out);
413 return;
414 print_usage:
415 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
416 }
417
418 #ifndef READ_ONLY
419 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
420 char **argv EXT2FS_ATTR((unused)))
421 {
422 if (check_fs_open(argv[0]))
423 return;
424 if (check_fs_read_write(argv[0]))
425 return;
426
427 if (argv[1] && !strcmp(argv[1], "-clean"))
428 current_fs->super->s_state |= EXT2_VALID_FS;
429 else
430 current_fs->super->s_state &= ~EXT2_VALID_FS;
431 ext2fs_mark_super_dirty(current_fs);
432 }
433 #endif /* READ_ONLY */
434
435 struct list_blocks_struct {
436 FILE *f;
437 e2_blkcnt_t total;
438 blk64_t first_block, last_block;
439 e2_blkcnt_t first_bcnt, last_bcnt;
440 e2_blkcnt_t first;
441 };
442
443 static void finish_range(struct list_blocks_struct *lb)
444 {
445 if (lb->first_block == 0)
446 return;
447 if (lb->first)
448 lb->first = 0;
449 else
450 fprintf(lb->f, ", ");
451 if (lb->first_block == lb->last_block)
452 fprintf(lb->f, "(%lld):%llu",
453 (long long)lb->first_bcnt, lb->first_block);
454 else
455 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
456 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
457 lb->first_block, lb->last_block);
458 lb->first_block = 0;
459 }
460
461 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
462 blk64_t *blocknr, e2_blkcnt_t blockcnt,
463 blk64_t ref_block EXT2FS_ATTR((unused)),
464 int ref_offset EXT2FS_ATTR((unused)),
465 void *private)
466 {
467 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
468
469 lb->total++;
470 if (blockcnt >= 0) {
471 /*
472 * See if we can add on to the existing range (if it exists)
473 */
474 if (lb->first_block &&
475 (lb->last_block+1 == *blocknr) &&
476 (lb->last_bcnt+1 == blockcnt)) {
477 lb->last_block = *blocknr;
478 lb->last_bcnt = blockcnt;
479 return 0;
480 }
481 /*
482 * Start a new range.
483 */
484 finish_range(lb);
485 lb->first_block = lb->last_block = *blocknr;
486 lb->first_bcnt = lb->last_bcnt = blockcnt;
487 return 0;
488 }
489 /*
490 * Not a normal block. Always force a new range.
491 */
492 finish_range(lb);
493 if (lb->first)
494 lb->first = 0;
495 else
496 fprintf(lb->f, ", ");
497 if (blockcnt == -1)
498 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
499 else if (blockcnt == -2)
500 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
501 else if (blockcnt == -3)
502 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
503 return 0;
504 }
505
506 static void dump_xattr_string(FILE *out, const char *str, int len)
507 {
508 int printable = 0;
509 int i;
510
511 /* check: is string "printable enough?" */
512 for (i = 0; i < len; i++)
513 if (isprint(str[i]))
514 printable++;
515
516 if (printable <= len*7/8)
517 printable = 0;
518
519 for (i = 0; i < len; i++)
520 if (printable)
521 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
522 (unsigned char)str[i]);
523 else
524 fprintf(out, "%02x ", (unsigned char)str[i]);
525 }
526
527 static void internal_dump_inode_extra(FILE *out,
528 const char *prefix EXT2FS_ATTR((unused)),
529 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
530 struct ext2_inode_large *inode)
531 {
532 struct ext2_ext_attr_entry *entry;
533 __u32 *magic;
534 char *start, *end;
535 unsigned int storage_size;
536
537 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
538 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
539 EXT2_GOOD_OLD_INODE_SIZE) {
540 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
541 inode->i_extra_isize);
542 return;
543 }
544 storage_size = EXT2_INODE_SIZE(current_fs->super) -
545 EXT2_GOOD_OLD_INODE_SIZE -
546 inode->i_extra_isize;
547 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
548 inode->i_extra_isize);
549 if (*magic == EXT2_EXT_ATTR_MAGIC) {
550 fprintf(out, "Extended attributes stored in inode body: \n");
551 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
552 start = (char *) magic + sizeof(__u32);
553 entry = (struct ext2_ext_attr_entry *) start;
554 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
555 struct ext2_ext_attr_entry *next =
556 EXT2_EXT_ATTR_NEXT(entry);
557 if (entry->e_value_size > storage_size ||
558 (char *) next >= end) {
559 fprintf(out, "invalid EA entry in inode\n");
560 return;
561 }
562 fprintf(out, " ");
563 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
564 entry->e_name_len);
565 fprintf(out, " = \"");
566 dump_xattr_string(out, start + entry->e_value_offs,
567 entry->e_value_size);
568 fprintf(out, "\" (%u)\n", entry->e_value_size);
569 entry = next;
570 }
571 }
572 }
573
574 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
575 {
576 struct list_blocks_struct lb;
577
578 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
579 lb.total = 0;
580 lb.first_block = 0;
581 lb.f = f;
582 lb.first = 1;
583 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
584 list_blocks_proc, (void *)&lb);
585 finish_range(&lb);
586 if (lb.total)
587 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
588 fprintf(f,"\n");
589 }
590
591 static int int_log10(unsigned long long arg)
592 {
593 int l = 0;
594
595 arg = arg / 10;
596 while (arg) {
597 l++;
598 arg = arg / 10;
599 }
600 return l;
601 }
602
603 #define DUMP_LEAF_EXTENTS 0x01
604 #define DUMP_NODE_EXTENTS 0x02
605 #define DUMP_EXTENT_TABLE 0x04
606
607 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
608 int flags, int logical_width, int physical_width)
609 {
610 ext2_extent_handle_t handle;
611 struct ext2fs_extent extent;
612 struct ext2_extent_info info;
613 int op = EXT2_EXTENT_ROOT;
614 unsigned int printed = 0;
615 errcode_t errcode;
616
617 errcode = ext2fs_extent_open(current_fs, ino, &handle);
618 if (errcode)
619 return;
620
621 if (flags & DUMP_EXTENT_TABLE)
622 fprintf(f, "Level Entries %*s %*s Length Flags\n",
623 (logical_width*2)+3, "Logical",
624 (physical_width*2)+3, "Physical");
625 else
626 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
627
628 while (1) {
629 errcode = ext2fs_extent_get(handle, op, &extent);
630
631 if (errcode)
632 break;
633
634 op = EXT2_EXTENT_NEXT;
635
636 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
637 continue;
638
639 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
640 if ((flags & DUMP_LEAF_EXTENTS) == 0)
641 continue;
642 } else {
643 if ((flags & DUMP_NODE_EXTENTS) == 0)
644 continue;
645 }
646
647 errcode = ext2fs_extent_get_info(handle, &info);
648 if (errcode)
649 continue;
650
651 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
652 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
653 continue;
654
655 if (flags & DUMP_EXTENT_TABLE) {
656 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
657 "%*llu%*s %6u\n",
658 info.curr_level, info.max_depth,
659 info.curr_entry, info.num_entries,
660 logical_width,
661 extent.e_lblk,
662 logical_width,
663 extent.e_lblk + (extent.e_len - 1),
664 physical_width,
665 extent.e_pblk,
666 physical_width+3, "", extent.e_len);
667 continue;
668 }
669
670 fprintf(f, "%s(ETB%d):%lld",
671 printed ? ", " : "", info.curr_level,
672 extent.e_pblk);
673 printed = 1;
674 continue;
675 }
676
677 if (flags & DUMP_EXTENT_TABLE) {
678 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
679 "%*llu - %*llu %6u %s\n",
680 info.curr_level, info.max_depth,
681 info.curr_entry, info.num_entries,
682 logical_width,
683 extent.e_lblk,
684 logical_width,
685 extent.e_lblk + (extent.e_len - 1),
686 physical_width,
687 extent.e_pblk,
688 physical_width,
689 extent.e_pblk + (extent.e_len - 1),
690 extent.e_len,
691 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
692 "Uninit" : "");
693 continue;
694 }
695
696 if (extent.e_len == 0)
697 continue;
698 else if (extent.e_len == 1)
699 fprintf(f,
700 "%s(%lld%s):%lld",
701 printed ? ", " : "",
702 extent.e_lblk,
703 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
704 "[u]" : "",
705 extent.e_pblk);
706 else
707 fprintf(f,
708 "%s(%lld-%lld%s):%lld-%lld",
709 printed ? ", " : "",
710 extent.e_lblk,
711 extent.e_lblk + (extent.e_len - 1),
712 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
713 "[u]" : "",
714 extent.e_pblk,
715 extent.e_pblk + (extent.e_len - 1));
716 printed = 1;
717 }
718 if (printed)
719 fprintf(f, "\n");
720 }
721
722 void internal_dump_inode(FILE *out, const char *prefix,
723 ext2_ino_t inode_num, struct ext2_inode *inode,
724 int do_dump_blocks)
725 {
726 const char *i_type;
727 char frag, fsize;
728 int os = current_fs->super->s_creator_os;
729 struct ext2_inode_large *large_inode;
730 int is_large_inode = 0;
731
732 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
733 is_large_inode = 1;
734 large_inode = (struct ext2_inode_large *) inode;
735
736 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
737 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
738 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
739 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
740 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
741 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
742 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
743 else i_type = "bad type";
744 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
745 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
746 prefix, inode->i_mode & 0777, inode->i_flags);
747 if (is_large_inode && large_inode->i_extra_isize >= 24) {
748 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
749 prefix, inode->i_generation, large_inode->i_version_hi,
750 inode->osd1.linux1.l_i_version);
751 } else {
752 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
753 inode->i_generation, inode->osd1.linux1.l_i_version);
754 }
755 fprintf(out, "%sUser: %5d Group: %5d Size: ",
756 prefix, inode_uid(*inode), inode_gid(*inode));
757 if (LINUX_S_ISREG(inode->i_mode))
758 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
759 else
760 fprintf(out, "%d\n", inode->i_size);
761 if (os == EXT2_OS_HURD)
762 fprintf(out,
763 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
764 prefix,
765 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
766 inode->osd1.hurd1.h_i_translator);
767 else
768 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
769 prefix,
770 inode->i_file_acl | ((long long)
771 (inode->osd2.linux2.l_i_file_acl_high) << 32),
772 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
773 if (os == EXT2_OS_LINUX)
774 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
775 prefix, inode->i_links_count,
776 (((unsigned long long)
777 inode->osd2.linux2.l_i_blocks_hi << 32)) +
778 inode->i_blocks);
779 else
780 fprintf(out, "%sLinks: %d Blockcount: %u\n",
781 prefix, inode->i_links_count, inode->i_blocks);
782 switch (os) {
783 case EXT2_OS_HURD:
784 frag = inode->osd2.hurd2.h_i_frag;
785 fsize = inode->osd2.hurd2.h_i_fsize;
786 break;
787 default:
788 frag = fsize = 0;
789 }
790 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
791 prefix, inode->i_faddr, frag, fsize);
792 if (is_large_inode && large_inode->i_extra_isize >= 24) {
793 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
794 inode->i_ctime, large_inode->i_ctime_extra,
795 time_to_string(inode->i_ctime));
796 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
797 inode->i_atime, large_inode->i_atime_extra,
798 time_to_string(inode->i_atime));
799 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
800 inode->i_mtime, large_inode->i_mtime_extra,
801 time_to_string(inode->i_mtime));
802 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
803 large_inode->i_crtime, large_inode->i_crtime_extra,
804 time_to_string(large_inode->i_crtime));
805 } else {
806 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
807 time_to_string(inode->i_ctime));
808 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
809 time_to_string(inode->i_atime));
810 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
811 time_to_string(inode->i_mtime));
812 }
813 if (inode->i_dtime)
814 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
815 time_to_string(inode->i_dtime));
816 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
817 internal_dump_inode_extra(out, prefix, inode_num,
818 (struct ext2_inode_large *) inode);
819 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
820 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
821 (int) inode->i_size, (char *)inode->i_block);
822 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
823 int major, minor;
824 const char *devnote;
825
826 if (inode->i_block[0]) {
827 major = (inode->i_block[0] >> 8) & 255;
828 minor = inode->i_block[0] & 255;
829 devnote = "";
830 } else {
831 major = (inode->i_block[1] & 0xfff00) >> 8;
832 minor = ((inode->i_block[1] & 0xff) |
833 ((inode->i_block[1] >> 12) & 0xfff00));
834 devnote = "(New-style) ";
835 }
836 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
837 devnote, major, minor, major, minor);
838 } else if (do_dump_blocks) {
839 if (inode->i_flags & EXT4_EXTENTS_FL)
840 dump_extents(out, prefix, inode_num,
841 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
842 else
843 dump_blocks(out, prefix, inode_num);
844 }
845 }
846
847 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
848 {
849 FILE *out;
850
851 out = open_pager();
852 internal_dump_inode(out, "", inode_num, inode, 1);
853 close_pager(out);
854 }
855
856 void do_stat(int argc, char *argv[])
857 {
858 ext2_ino_t inode;
859 struct ext2_inode * inode_buf;
860
861 if (check_fs_open(argv[0]))
862 return;
863
864 inode_buf = (struct ext2_inode *)
865 malloc(EXT2_INODE_SIZE(current_fs->super));
866 if (!inode_buf) {
867 fprintf(stderr, "do_stat: can't allocate buffer\n");
868 return;
869 }
870
871 if (common_inode_args_process(argc, argv, &inode, 0)) {
872 free(inode_buf);
873 return;
874 }
875
876 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
877 EXT2_INODE_SIZE(current_fs->super))) {
878 free(inode_buf);
879 return;
880 }
881
882 dump_inode(inode, inode_buf);
883 free(inode_buf);
884 return;
885 }
886
887 void do_dump_extents(int argc, char **argv)
888 {
889 struct ext2_inode inode;
890 ext2_ino_t ino;
891 FILE *out;
892 int c, flags = 0;
893 int logical_width;
894 int physical_width;
895
896 reset_getopt();
897 while ((c = getopt(argc, argv, "nl")) != EOF) {
898 switch (c) {
899 case 'n':
900 flags |= DUMP_NODE_EXTENTS;
901 break;
902 case 'l':
903 flags |= DUMP_LEAF_EXTENTS;
904 break;
905 }
906 }
907
908 if (argc != optind + 1) {
909 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
910 return;
911 }
912
913 if (flags == 0)
914 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
915 flags |= DUMP_EXTENT_TABLE;
916
917 if (check_fs_open(argv[0]))
918 return;
919
920 ino = string_to_inode(argv[optind]);
921 if (ino == 0)
922 return;
923
924 if (debugfs_read_inode(ino, &inode, argv[0]))
925 return;
926
927 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
928 fprintf(stderr, "%s: does not uses extent block maps\n",
929 argv[optind]);
930 return;
931 }
932
933 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
934 current_fs->blocksize) + 1;
935 if (logical_width < 5)
936 logical_width = 5;
937 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
938 if (physical_width < 5)
939 physical_width = 5;
940
941 out = open_pager();
942 dump_extents(out, "", ino, flags, logical_width, physical_width);
943 close_pager(out);
944 return;
945 }
946
947 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
948 blk64_t *blocknr,
949 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
950 blk64_t ref_block EXT2FS_ATTR((unused)),
951 int ref_offset EXT2FS_ATTR((unused)),
952 void *private EXT2FS_ATTR((unused)))
953 {
954 printf("%llu ", *blocknr);
955 return 0;
956 }
957
958 void do_blocks(int argc, char *argv[])
959 {
960 ext2_ino_t inode;
961
962 if (check_fs_open(argv[0]))
963 return;
964
965 if (common_inode_args_process(argc, argv, &inode, 0)) {
966 return;
967 }
968
969 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
970 print_blocks_proc, NULL);
971 fputc('\n', stdout);
972 return;
973 }
974
975 void do_chroot(int argc, char *argv[])
976 {
977 ext2_ino_t inode;
978 int retval;
979
980 if (common_inode_args_process(argc, argv, &inode, 0))
981 return;
982
983 retval = ext2fs_check_directory(current_fs, inode);
984 if (retval) {
985 com_err(argv[1], retval, 0);
986 return;
987 }
988 root = inode;
989 }
990
991 #ifndef READ_ONLY
992 void do_clri(int argc, char *argv[])
993 {
994 ext2_ino_t inode;
995 struct ext2_inode inode_buf;
996
997 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
998 return;
999
1000 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1001 return;
1002 memset(&inode_buf, 0, sizeof(inode_buf));
1003 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1004 return;
1005 }
1006
1007 void do_freei(int argc, char *argv[])
1008 {
1009 unsigned int len = 1;
1010 int err = 0;
1011 ext2_ino_t inode;
1012
1013 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1014 CHECK_FS_RW | CHECK_FS_BITMAPS))
1015 return;
1016 if (check_fs_read_write(argv[0]))
1017 return;
1018
1019 inode = string_to_inode(argv[1]);
1020 if (!inode)
1021 return;
1022
1023 if (argc == 3) {
1024 len = parse_ulong(argv[2], argv[0], "length", &err);
1025 if (err)
1026 return;
1027 }
1028
1029 if (len == 1 &&
1030 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1031 com_err(argv[0], 0, "Warning: inode already clear");
1032 while (len-- > 0)
1033 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode);
1034 ext2fs_mark_ib_dirty(current_fs);
1035 }
1036
1037 void do_seti(int argc, char *argv[])
1038 {
1039 unsigned int len = 1;
1040 int err = 0;
1041 ext2_ino_t inode;
1042
1043 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1044 CHECK_FS_RW | CHECK_FS_BITMAPS))
1045 return;
1046 if (check_fs_read_write(argv[0]))
1047 return;
1048
1049 inode = string_to_inode(argv[1]);
1050 if (!inode)
1051 return;
1052
1053 if (argc == 3) {
1054 len = parse_ulong(argv[2], argv[0], "length", &err);
1055 if (err)
1056 return;
1057 }
1058
1059 if ((len == 1) &&
1060 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1061 com_err(argv[0], 0, "Warning: inode already set");
1062 while (len-- > 0)
1063 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1064 ext2fs_mark_ib_dirty(current_fs);
1065 }
1066 #endif /* READ_ONLY */
1067
1068 void do_testi(int argc, char *argv[])
1069 {
1070 ext2_ino_t inode;
1071
1072 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1073 return;
1074
1075 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1076 printf("Inode %u is marked in use\n", inode);
1077 else
1078 printf("Inode %u is not in use\n", inode);
1079 }
1080
1081 #ifndef READ_ONLY
1082 void do_freeb(int argc, char *argv[])
1083 {
1084 blk64_t block;
1085 blk64_t count = 1;
1086
1087 if (common_block_args_process(argc, argv, &block, &count))
1088 return;
1089 if (check_fs_read_write(argv[0]))
1090 return;
1091 while (count-- > 0) {
1092 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1093 com_err(argv[0], 0, "Warning: block %llu already clear",
1094 block);
1095 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1096 block++;
1097 }
1098 ext2fs_mark_bb_dirty(current_fs);
1099 }
1100
1101 void do_setb(int argc, char *argv[])
1102 {
1103 blk64_t block;
1104 blk64_t count = 1;
1105
1106 if (common_block_args_process(argc, argv, &block, &count))
1107 return;
1108 if (check_fs_read_write(argv[0]))
1109 return;
1110 while (count-- > 0) {
1111 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1112 com_err(argv[0], 0, "Warning: block %llu already set",
1113 block);
1114 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1115 block++;
1116 }
1117 ext2fs_mark_bb_dirty(current_fs);
1118 }
1119 #endif /* READ_ONLY */
1120
1121 void do_testb(int argc, char *argv[])
1122 {
1123 blk64_t block;
1124 blk64_t count = 1;
1125
1126 if (common_block_args_process(argc, argv, &block, &count))
1127 return;
1128 while (count-- > 0) {
1129 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1130 printf("Block %llu marked in use\n", block);
1131 else
1132 printf("Block %llu not in use\n", block);
1133 block++;
1134 }
1135 }
1136
1137 #ifndef READ_ONLY
1138 static void modify_u8(char *com, const char *prompt,
1139 const char *format, __u8 *val)
1140 {
1141 char buf[200];
1142 unsigned long v;
1143 char *tmp;
1144
1145 sprintf(buf, format, *val);
1146 printf("%30s [%s] ", prompt, buf);
1147 if (!fgets(buf, sizeof(buf), stdin))
1148 return;
1149 if (buf[strlen (buf) - 1] == '\n')
1150 buf[strlen (buf) - 1] = '\0';
1151 if (!buf[0])
1152 return;
1153 v = strtoul(buf, &tmp, 0);
1154 if (*tmp)
1155 com_err(com, 0, "Bad value - %s", buf);
1156 else
1157 *val = v;
1158 }
1159
1160 static void modify_u16(char *com, const char *prompt,
1161 const char *format, __u16 *val)
1162 {
1163 char buf[200];
1164 unsigned long v;
1165 char *tmp;
1166
1167 sprintf(buf, format, *val);
1168 printf("%30s [%s] ", prompt, buf);
1169 if (!fgets(buf, sizeof(buf), stdin))
1170 return;
1171 if (buf[strlen (buf) - 1] == '\n')
1172 buf[strlen (buf) - 1] = '\0';
1173 if (!buf[0])
1174 return;
1175 v = strtoul(buf, &tmp, 0);
1176 if (*tmp)
1177 com_err(com, 0, "Bad value - %s", buf);
1178 else
1179 *val = v;
1180 }
1181
1182 static void modify_u32(char *com, const char *prompt,
1183 const char *format, __u32 *val)
1184 {
1185 char buf[200];
1186 unsigned long v;
1187 char *tmp;
1188
1189 sprintf(buf, format, *val);
1190 printf("%30s [%s] ", prompt, buf);
1191 if (!fgets(buf, sizeof(buf), stdin))
1192 return;
1193 if (buf[strlen (buf) - 1] == '\n')
1194 buf[strlen (buf) - 1] = '\0';
1195 if (!buf[0])
1196 return;
1197 v = strtoul(buf, &tmp, 0);
1198 if (*tmp)
1199 com_err(com, 0, "Bad value - %s", buf);
1200 else
1201 *val = v;
1202 }
1203
1204
1205 void do_modify_inode(int argc, char *argv[])
1206 {
1207 struct ext2_inode inode;
1208 ext2_ino_t inode_num;
1209 int i;
1210 unsigned char *frag, *fsize;
1211 char buf[80];
1212 int os;
1213 const char *hex_format = "0x%x";
1214 const char *octal_format = "0%o";
1215 const char *decimal_format = "%d";
1216 const char *unsignedlong_format = "%lu";
1217
1218 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1219 return;
1220
1221 os = current_fs->super->s_creator_os;
1222
1223 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1224 return;
1225
1226 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1227 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1228 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1229 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1230 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1231 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1232 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1233 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1234 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1235 if (os == EXT2_OS_LINUX)
1236 modify_u16(argv[0], "Block count high", unsignedlong_format,
1237 &inode.osd2.linux2.l_i_blocks_hi);
1238 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1239 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1240 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1241 #if 0
1242 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1243 #endif
1244 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1245 if (LINUX_S_ISDIR(inode.i_mode))
1246 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1247 else
1248 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1249
1250 if (os == EXT2_OS_HURD)
1251 modify_u32(argv[0], "Translator Block",
1252 decimal_format, &inode.osd1.hurd1.h_i_translator);
1253
1254 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1255 switch (os) {
1256 case EXT2_OS_HURD:
1257 frag = &inode.osd2.hurd2.h_i_frag;
1258 fsize = &inode.osd2.hurd2.h_i_fsize;
1259 break;
1260 default:
1261 frag = fsize = 0;
1262 }
1263 if (frag)
1264 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1265 if (fsize)
1266 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1267
1268 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1269 sprintf(buf, "Direct Block #%d", i);
1270 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1271 }
1272 modify_u32(argv[0], "Indirect Block", decimal_format,
1273 &inode.i_block[EXT2_IND_BLOCK]);
1274 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1275 &inode.i_block[EXT2_DIND_BLOCK]);
1276 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1277 &inode.i_block[EXT2_TIND_BLOCK]);
1278 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1279 return;
1280 }
1281 #endif /* READ_ONLY */
1282
1283 void do_change_working_dir(int argc, char *argv[])
1284 {
1285 ext2_ino_t inode;
1286 int retval;
1287
1288 if (common_inode_args_process(argc, argv, &inode, 0))
1289 return;
1290
1291 retval = ext2fs_check_directory(current_fs, inode);
1292 if (retval) {
1293 com_err(argv[1], retval, 0);
1294 return;
1295 }
1296 cwd = inode;
1297 return;
1298 }
1299
1300 void do_print_working_directory(int argc, char *argv[])
1301 {
1302 int retval;
1303 char *pathname = NULL;
1304
1305 if (common_args_process(argc, argv, 1, 1,
1306 "print_working_directory", "", 0))
1307 return;
1308
1309 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1310 if (retval) {
1311 com_err(argv[0], retval,
1312 "while trying to get pathname of cwd");
1313 }
1314 printf("[pwd] INODE: %6u PATH: %s\n",
1315 cwd, pathname ? pathname : "NULL");
1316 if (pathname) {
1317 free(pathname);
1318 pathname = NULL;
1319 }
1320 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1321 if (retval) {
1322 com_err(argv[0], retval,
1323 "while trying to get pathname of root");
1324 }
1325 printf("[root] INODE: %6u PATH: %s\n",
1326 root, pathname ? pathname : "NULL");
1327 if (pathname) {
1328 free(pathname);
1329 pathname = NULL;
1330 }
1331 return;
1332 }
1333
1334 #ifndef READ_ONLY
1335 static void make_link(char *sourcename, char *destname)
1336 {
1337 ext2_ino_t ino;
1338 struct ext2_inode inode;
1339 int retval;
1340 ext2_ino_t dir;
1341 char *dest, *cp, *base_name;
1342
1343 /*
1344 * Get the source inode
1345 */
1346 ino = string_to_inode(sourcename);
1347 if (!ino)
1348 return;
1349 base_name = strrchr(sourcename, '/');
1350 if (base_name)
1351 base_name++;
1352 else
1353 base_name = sourcename;
1354 /*
1355 * Figure out the destination. First see if it exists and is
1356 * a directory.
1357 */
1358 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1359 dest = base_name;
1360 else {
1361 /*
1362 * OK, it doesn't exist. See if it is
1363 * '<dir>/basename' or 'basename'
1364 */
1365 cp = strrchr(destname, '/');
1366 if (cp) {
1367 *cp = 0;
1368 dir = string_to_inode(destname);
1369 if (!dir)
1370 return;
1371 dest = cp+1;
1372 } else {
1373 dir = cwd;
1374 dest = destname;
1375 }
1376 }
1377
1378 if (debugfs_read_inode(ino, &inode, sourcename))
1379 return;
1380
1381 retval = ext2fs_link(current_fs, dir, dest, ino,
1382 ext2_file_type(inode.i_mode));
1383 if (retval)
1384 com_err("make_link", retval, 0);
1385 return;
1386 }
1387
1388
1389 void do_link(int argc, char *argv[])
1390 {
1391 if (common_args_process(argc, argv, 3, 3, "link",
1392 "<source file> <dest_name>", CHECK_FS_RW))
1393 return;
1394
1395 make_link(argv[1], argv[2]);
1396 }
1397
1398 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1399 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1400 blk64_t ref_block EXT2FS_ATTR((unused)),
1401 int ref_offset EXT2FS_ATTR((unused)),
1402 void *private EXT2FS_ATTR((unused)))
1403 {
1404 blk64_t block;
1405
1406 block = *blocknr;
1407 ext2fs_block_alloc_stats2(fs, block, +1);
1408 return 0;
1409 }
1410
1411 void do_undel(int argc, char *argv[])
1412 {
1413 ext2_ino_t ino;
1414 struct ext2_inode inode;
1415
1416 if (common_args_process(argc, argv, 2, 3, "undelete",
1417 "<inode_num> [dest_name]",
1418 CHECK_FS_RW | CHECK_FS_BITMAPS))
1419 return;
1420
1421 ino = string_to_inode(argv[1]);
1422 if (!ino)
1423 return;
1424
1425 if (debugfs_read_inode(ino, &inode, argv[1]))
1426 return;
1427
1428 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1429 com_err(argv[1], 0, "Inode is not marked as deleted");
1430 return;
1431 }
1432
1433 /*
1434 * XXX this function doesn't handle changing the links count on the
1435 * parent directory when undeleting a directory.
1436 */
1437 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1438 inode.i_dtime = 0;
1439
1440 if (debugfs_write_inode(ino, &inode, argv[0]))
1441 return;
1442
1443 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1444 mark_blocks_proc, NULL);
1445
1446 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1447
1448 if (argc > 2)
1449 make_link(argv[1], argv[2]);
1450 }
1451
1452 static void unlink_file_by_name(char *filename)
1453 {
1454 int retval;
1455 ext2_ino_t dir;
1456 char *base_name;
1457
1458 base_name = strrchr(filename, '/');
1459 if (base_name) {
1460 *base_name++ = '\0';
1461 dir = string_to_inode(filename);
1462 if (!dir)
1463 return;
1464 } else {
1465 dir = cwd;
1466 base_name = filename;
1467 }
1468 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1469 if (retval)
1470 com_err("unlink_file_by_name", retval, 0);
1471 return;
1472 }
1473
1474 void do_unlink(int argc, char *argv[])
1475 {
1476 if (common_args_process(argc, argv, 2, 2, "link",
1477 "<pathname>", CHECK_FS_RW))
1478 return;
1479
1480 unlink_file_by_name(argv[1]);
1481 }
1482 #endif /* READ_ONLY */
1483
1484 void do_find_free_block(int argc, char *argv[])
1485 {
1486 blk64_t free_blk, goal, first_free = 0;
1487 int count;
1488 errcode_t retval;
1489 char *tmp;
1490
1491 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1492 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1493 return;
1494 }
1495 if (check_fs_open(argv[0]))
1496 return;
1497
1498 if (argc > 1) {
1499 count = strtol(argv[1],&tmp,0);
1500 if (*tmp) {
1501 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1502 return;
1503 }
1504 } else
1505 count = 1;
1506
1507 if (argc > 2) {
1508 goal = strtol(argv[2], &tmp, 0);
1509 if (*tmp) {
1510 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1511 return;
1512 }
1513 }
1514 else
1515 goal = current_fs->super->s_first_data_block;
1516
1517 printf("Free blocks found: ");
1518 free_blk = goal - 1;
1519 while (count-- > 0) {
1520 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1521 &free_blk);
1522 if (first_free) {
1523 if (first_free == free_blk)
1524 break;
1525 } else
1526 first_free = free_blk;
1527 if (retval) {
1528 com_err("ext2fs_new_block", retval, 0);
1529 return;
1530 } else
1531 printf("%llu ", free_blk);
1532 }
1533 printf("\n");
1534 }
1535
1536 void do_find_free_inode(int argc, char *argv[])
1537 {
1538 ext2_ino_t free_inode, dir;
1539 int mode;
1540 int retval;
1541 char *tmp;
1542
1543 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1544 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1545 return;
1546 }
1547 if (check_fs_open(argv[0]))
1548 return;
1549
1550 if (argc > 1) {
1551 dir = strtol(argv[1], &tmp, 0);
1552 if (*tmp) {
1553 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1554 return;
1555 }
1556 }
1557 else
1558 dir = root;
1559 if (argc > 2) {
1560 mode = strtol(argv[2], &tmp, 0);
1561 if (*tmp) {
1562 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1563 return;
1564 }
1565 } else
1566 mode = 010755;
1567
1568 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1569 if (retval)
1570 com_err("ext2fs_new_inode", retval, 0);
1571 else
1572 printf("Free inode found: %u\n", free_inode);
1573 }
1574
1575 #ifndef READ_ONLY
1576 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
1577 {
1578 ext2_file_t e2_file;
1579 errcode_t retval;
1580 int got;
1581 unsigned int written;
1582 char *buf;
1583 char *ptr;
1584 char *zero_buf;
1585 int cmp;
1586
1587 retval = ext2fs_file_open(current_fs, newfile,
1588 EXT2_FILE_WRITE, &e2_file);
1589 if (retval)
1590 return retval;
1591
1592 if (!(buf = (char *) malloc(bufsize))){
1593 com_err("copy_file", errno, "can't allocate buffer\n");
1594 return;
1595 }
1596
1597 /* This is used for checking whether the whole block is zero */
1598 retval = ext2fs_get_memzero(bufsize, &zero_buf);
1599 if (retval) {
1600 com_err("copy_file", retval, "can't allocate buffer\n");
1601 free(buf);
1602 return retval;
1603 }
1604
1605 while (1) {
1606 got = read(fd, buf, bufsize);
1607 if (got == 0)
1608 break;
1609 if (got < 0) {
1610 retval = errno;
1611 goto fail;
1612 }
1613 ptr = buf;
1614
1615 /* Sparse copy */
1616 if (make_holes) {
1617 /* Check whether all is zero */
1618 cmp = memcmp(ptr, zero_buf, got);
1619 if (cmp == 0) {
1620 /* The whole block is zero, make a hole */
1621 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
1622 if (retval)
1623 goto fail;
1624 got = 0;
1625 }
1626 }
1627
1628 /* Normal copy */
1629 while (got > 0) {
1630 retval = ext2fs_file_write(e2_file, ptr,
1631 got, &written);
1632 if (retval)
1633 goto fail;
1634
1635 got -= written;
1636 ptr += written;
1637 }
1638 }
1639 free(buf);
1640 ext2fs_free_mem(&zero_buf);
1641 retval = ext2fs_file_close(e2_file);
1642 return retval;
1643
1644 fail:
1645 free(buf);
1646 ext2fs_free_mem(&zero_buf);
1647 (void) ext2fs_file_close(e2_file);
1648 return retval;
1649 }
1650
1651
1652 void do_write(int argc, char *argv[])
1653 {
1654 int fd;
1655 struct stat statbuf;
1656 ext2_ino_t newfile;
1657 errcode_t retval;
1658 struct ext2_inode inode;
1659 int bufsize = IO_BUFSIZE;
1660 int make_holes = 0;
1661
1662 if (common_args_process(argc, argv, 3, 3, "write",
1663 "<native file> <new file>", CHECK_FS_RW))
1664 return;
1665
1666 fd = open(argv[1], O_RDONLY);
1667 if (fd < 0) {
1668 com_err(argv[1], errno, 0);
1669 return;
1670 }
1671 if (fstat(fd, &statbuf) < 0) {
1672 com_err(argv[1], errno, 0);
1673 close(fd);
1674 return;
1675 }
1676
1677 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1678 if (retval == 0) {
1679 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1680 close(fd);
1681 return;
1682 }
1683
1684 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1685 if (retval) {
1686 com_err(argv[0], retval, 0);
1687 close(fd);
1688 return;
1689 }
1690 printf("Allocated inode: %u\n", newfile);
1691 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1692 EXT2_FT_REG_FILE);
1693 if (retval == EXT2_ET_DIR_NO_SPACE) {
1694 retval = ext2fs_expand_dir(current_fs, cwd);
1695 if (retval) {
1696 com_err(argv[0], retval, "while expanding directory");
1697 close(fd);
1698 return;
1699 }
1700 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1701 EXT2_FT_REG_FILE);
1702 }
1703 if (retval) {
1704 com_err(argv[2], retval, 0);
1705 close(fd);
1706 return;
1707 }
1708 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1709 com_err(argv[0], 0, "Warning: inode already set");
1710 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1711 memset(&inode, 0, sizeof(inode));
1712 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1713 inode.i_atime = inode.i_ctime = inode.i_mtime =
1714 current_fs->now ? current_fs->now : time(0);
1715 inode.i_links_count = 1;
1716 inode.i_size = statbuf.st_size;
1717 if (current_fs->super->s_feature_incompat &
1718 EXT3_FEATURE_INCOMPAT_EXTENTS) {
1719 int i;
1720 struct ext3_extent_header *eh;
1721
1722 eh = (struct ext3_extent_header *) &inode.i_block[0];
1723 eh->eh_depth = 0;
1724 eh->eh_entries = 0;
1725 eh->eh_magic = EXT3_EXT_MAGIC;
1726 i = (sizeof(inode.i_block) - sizeof(*eh)) /
1727 sizeof(struct ext3_extent);
1728 eh->eh_max = ext2fs_cpu_to_le16(i);
1729 inode.i_flags |= EXT4_EXTENTS_FL;
1730 }
1731 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1732 close(fd);
1733 return;
1734 }
1735 if (LINUX_S_ISREG(inode.i_mode)) {
1736 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
1737 make_holes = 1;
1738 /*
1739 * Use I/O blocksize as buffer size when
1740 * copying sparse files.
1741 */
1742 bufsize = statbuf.st_blksize;
1743 }
1744 retval = copy_file(fd, newfile, bufsize, make_holes);
1745 if (retval)
1746 com_err("copy_file", retval, 0);
1747 }
1748 close(fd);
1749 }
1750
1751 void do_mknod(int argc, char *argv[])
1752 {
1753 unsigned long mode, major, minor;
1754 ext2_ino_t newfile;
1755 errcode_t retval;
1756 struct ext2_inode inode;
1757 int filetype, nr;
1758
1759 if (check_fs_open(argv[0]))
1760 return;
1761 if (argc < 3 || argv[2][1]) {
1762 usage:
1763 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1764 return;
1765 }
1766 mode = minor = major = 0;
1767 switch (argv[2][0]) {
1768 case 'p':
1769 mode = LINUX_S_IFIFO;
1770 filetype = EXT2_FT_FIFO;
1771 nr = 3;
1772 break;
1773 case 'c':
1774 mode = LINUX_S_IFCHR;
1775 filetype = EXT2_FT_CHRDEV;
1776 nr = 5;
1777 break;
1778 case 'b':
1779 mode = LINUX_S_IFBLK;
1780 filetype = EXT2_FT_BLKDEV;
1781 nr = 5;
1782 break;
1783 default:
1784 filetype = 0;
1785 nr = 0;
1786 }
1787 if (nr == 5) {
1788 major = strtoul(argv[3], argv+3, 0);
1789 minor = strtoul(argv[4], argv+4, 0);
1790 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1791 nr = 0;
1792 }
1793 if (argc != nr)
1794 goto usage;
1795 if (check_fs_read_write(argv[0]))
1796 return;
1797 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1798 if (retval) {
1799 com_err(argv[0], retval, 0);
1800 return;
1801 }
1802 printf("Allocated inode: %u\n", newfile);
1803 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1804 if (retval == EXT2_ET_DIR_NO_SPACE) {
1805 retval = ext2fs_expand_dir(current_fs, cwd);
1806 if (retval) {
1807 com_err(argv[0], retval, "while expanding directory");
1808 return;
1809 }
1810 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1811 filetype);
1812 }
1813 if (retval) {
1814 com_err(argv[1], retval, 0);
1815 return;
1816 }
1817 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1818 com_err(argv[0], 0, "Warning: inode already set");
1819 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1820 memset(&inode, 0, sizeof(inode));
1821 inode.i_mode = mode;
1822 inode.i_atime = inode.i_ctime = inode.i_mtime =
1823 current_fs->now ? current_fs->now : time(0);
1824 if ((major < 256) && (minor < 256)) {
1825 inode.i_block[0] = major*256+minor;
1826 inode.i_block[1] = 0;
1827 } else {
1828 inode.i_block[0] = 0;
1829 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1830 }
1831 inode.i_links_count = 1;
1832 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1833 return;
1834 }
1835
1836 void do_mkdir(int argc, char *argv[])
1837 {
1838 char *cp;
1839 ext2_ino_t parent;
1840 char *name;
1841 errcode_t retval;
1842
1843 if (common_args_process(argc, argv, 2, 2, "mkdir",
1844 "<filename>", CHECK_FS_RW))
1845 return;
1846
1847 cp = strrchr(argv[1], '/');
1848 if (cp) {
1849 *cp = 0;
1850 parent = string_to_inode(argv[1]);
1851 if (!parent) {
1852 com_err(argv[1], ENOENT, 0);
1853 return;
1854 }
1855 name = cp+1;
1856 } else {
1857 parent = cwd;
1858 name = argv[1];
1859 }
1860
1861 try_again:
1862 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1863 if (retval == EXT2_ET_DIR_NO_SPACE) {
1864 retval = ext2fs_expand_dir(current_fs, parent);
1865 if (retval) {
1866 com_err(argv[0], retval, "while expanding directory");
1867 return;
1868 }
1869 goto try_again;
1870 }
1871 if (retval) {
1872 com_err("ext2fs_mkdir", retval, 0);
1873 return;
1874 }
1875
1876 }
1877
1878 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1879 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1880 blk64_t ref_block EXT2FS_ATTR((unused)),
1881 int ref_offset EXT2FS_ATTR((unused)),
1882 void *private EXT2FS_ATTR((unused)))
1883 {
1884 blk64_t block;
1885
1886 block = *blocknr;
1887 ext2fs_block_alloc_stats2(fs, block, -1);
1888 return 0;
1889 }
1890
1891 static void kill_file_by_inode(ext2_ino_t inode)
1892 {
1893 struct ext2_inode inode_buf;
1894
1895 if (debugfs_read_inode(inode, &inode_buf, 0))
1896 return;
1897 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1898 if (debugfs_write_inode(inode, &inode_buf, 0))
1899 return;
1900 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
1901 return;
1902
1903 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1904 release_blocks_proc, NULL);
1905 printf("\n");
1906 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1907 LINUX_S_ISDIR(inode_buf.i_mode));
1908 }
1909
1910
1911 void do_kill_file(int argc, char *argv[])
1912 {
1913 ext2_ino_t inode_num;
1914
1915 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1916 return;
1917
1918 kill_file_by_inode(inode_num);
1919 }
1920
1921 void do_rm(int argc, char *argv[])
1922 {
1923 int retval;
1924 ext2_ino_t inode_num;
1925 struct ext2_inode inode;
1926
1927 if (common_args_process(argc, argv, 2, 2, "rm",
1928 "<filename>", CHECK_FS_RW))
1929 return;
1930
1931 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1932 if (retval) {
1933 com_err(argv[0], retval, "while trying to resolve filename");
1934 return;
1935 }
1936
1937 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1938 return;
1939
1940 if (LINUX_S_ISDIR(inode.i_mode)) {
1941 com_err(argv[0], 0, "file is a directory");
1942 return;
1943 }
1944
1945 --inode.i_links_count;
1946 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1947 return;
1948
1949 unlink_file_by_name(argv[1]);
1950 if (inode.i_links_count == 0)
1951 kill_file_by_inode(inode_num);
1952 }
1953
1954 struct rd_struct {
1955 ext2_ino_t parent;
1956 int empty;
1957 };
1958
1959 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1960 int entry EXT2FS_ATTR((unused)),
1961 struct ext2_dir_entry *dirent,
1962 int offset EXT2FS_ATTR((unused)),
1963 int blocksize EXT2FS_ATTR((unused)),
1964 char *buf EXT2FS_ATTR((unused)),
1965 void *private)
1966 {
1967 struct rd_struct *rds = (struct rd_struct *) private;
1968
1969 if (dirent->inode == 0)
1970 return 0;
1971 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1972 return 0;
1973 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1974 (dirent->name[1] == '.')) {
1975 rds->parent = dirent->inode;
1976 return 0;
1977 }
1978 rds->empty = 0;
1979 return 0;
1980 }
1981
1982 void do_rmdir(int argc, char *argv[])
1983 {
1984 int retval;
1985 ext2_ino_t inode_num;
1986 struct ext2_inode inode;
1987 struct rd_struct rds;
1988
1989 if (common_args_process(argc, argv, 2, 2, "rmdir",
1990 "<filename>", CHECK_FS_RW))
1991 return;
1992
1993 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1994 if (retval) {
1995 com_err(argv[0], retval, "while trying to resolve filename");
1996 return;
1997 }
1998
1999 if (debugfs_read_inode(inode_num, &inode, argv[0]))
2000 return;
2001
2002 if (!LINUX_S_ISDIR(inode.i_mode)) {
2003 com_err(argv[0], 0, "file is not a directory");
2004 return;
2005 }
2006
2007 rds.parent = 0;
2008 rds.empty = 1;
2009
2010 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
2011 0, rmdir_proc, &rds);
2012 if (retval) {
2013 com_err(argv[0], retval, "while iterating over directory");
2014 return;
2015 }
2016 if (rds.empty == 0) {
2017 com_err(argv[0], 0, "directory not empty");
2018 return;
2019 }
2020
2021 inode.i_links_count = 0;
2022 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2023 return;
2024
2025 unlink_file_by_name(argv[1]);
2026 kill_file_by_inode(inode_num);
2027
2028 if (rds.parent) {
2029 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2030 return;
2031 if (inode.i_links_count > 1)
2032 inode.i_links_count--;
2033 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2034 return;
2035 }
2036 }
2037 #endif /* READ_ONLY */
2038
2039 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
2040 char *argv[] EXT2FS_ATTR((unused)))
2041 {
2042 if (current_fs)
2043 printf("Open mode: read-%s\n",
2044 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2045 printf("Filesystem in use: %s\n",
2046 current_fs ? current_fs->device_name : "--none--");
2047 }
2048
2049 #ifndef READ_ONLY
2050 void do_expand_dir(int argc, char *argv[])
2051 {
2052 ext2_ino_t inode;
2053 int retval;
2054
2055 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2056 return;
2057
2058 retval = ext2fs_expand_dir(current_fs, inode);
2059 if (retval)
2060 com_err("ext2fs_expand_dir", retval, 0);
2061 return;
2062 }
2063
2064 void do_features(int argc, char *argv[])
2065 {
2066 int i;
2067
2068 if (check_fs_open(argv[0]))
2069 return;
2070
2071 if ((argc != 1) && check_fs_read_write(argv[0]))
2072 return;
2073 for (i=1; i < argc; i++) {
2074 if (e2p_edit_feature(argv[i],
2075 &current_fs->super->s_feature_compat, 0))
2076 com_err(argv[0], 0, "Unknown feature: %s\n",
2077 argv[i]);
2078 else
2079 ext2fs_mark_super_dirty(current_fs);
2080 }
2081 print_features(current_fs->super, stdout);
2082 }
2083 #endif /* READ_ONLY */
2084
2085 void do_bmap(int argc, char *argv[])
2086 {
2087 ext2_ino_t ino;
2088 blk64_t blk, pblk;
2089 int err;
2090 errcode_t errcode;
2091
2092 if (common_args_process(argc, argv, 3, 3, argv[0],
2093 "<file> logical_blk", 0))
2094 return;
2095
2096 ino = string_to_inode(argv[1]);
2097 if (!ino)
2098 return;
2099 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
2100
2101 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2102 if (errcode) {
2103 com_err("argv[0]", errcode,
2104 "while mapping logical block %llu\n", blk);
2105 return;
2106 }
2107 printf("%llu\n", pblk);
2108 }
2109
2110 void do_imap(int argc, char *argv[])
2111 {
2112 ext2_ino_t ino;
2113 unsigned long group, block, block_nr, offset;
2114
2115 if (common_args_process(argc, argv, 2, 2, argv[0],
2116 "<file>", 0))
2117 return;
2118 ino = string_to_inode(argv[1]);
2119 if (!ino)
2120 return;
2121
2122 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2123 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2124 EXT2_INODE_SIZE(current_fs->super);
2125 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2126 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2127 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2128 group);
2129 return;
2130 }
2131 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2132 block;
2133 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2134
2135 printf("Inode %d is part of block group %lu\n"
2136 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2137 block_nr, offset);
2138
2139 }
2140
2141 #ifndef READ_ONLY
2142 void do_set_current_time(int argc, char *argv[])
2143 {
2144 time_t now;
2145
2146 if (common_args_process(argc, argv, 2, 2, argv[0],
2147 "<time>", 0))
2148 return;
2149
2150 now = string_to_time(argv[1]);
2151 if (now == ((time_t) -1)) {
2152 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2153 argv[1]);
2154 return;
2155
2156 } else {
2157 printf("Setting current time to %s\n", time_to_string(now));
2158 current_fs->now = now;
2159 }
2160 }
2161 #endif /* READ_ONLY */
2162
2163 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2164 {
2165 int compat, bit, ret;
2166 unsigned int feature_mask;
2167
2168 if (name) {
2169 if (feature_type == E2P_FS_FEATURE)
2170 ret = e2p_string2feature(name, &compat, &feature_mask);
2171 else
2172 ret = e2p_jrnl_string2feature(name, &compat,
2173 &feature_mask);
2174 if (ret)
2175 return ret;
2176
2177 if (!(supp[compat] & feature_mask))
2178 return 1;
2179 } else {
2180 for (compat = 0; compat < 3; compat++) {
2181 for (bit = 0, feature_mask = 1; bit < 32;
2182 bit++, feature_mask <<= 1) {
2183 if (supp[compat] & feature_mask) {
2184 if (feature_type == E2P_FS_FEATURE)
2185 fprintf(stdout, " %s",
2186 e2p_feature2string(compat,
2187 feature_mask));
2188 else
2189 fprintf(stdout, " %s",
2190 e2p_jrnl_feature2string(compat,
2191 feature_mask));
2192 }
2193 }
2194 }
2195 fprintf(stdout, "\n");
2196 }
2197
2198 return 0;
2199 }
2200
2201 void do_supported_features(int argc, char *argv[])
2202 {
2203 int ret;
2204 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2205 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2206 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2207 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2208 JFS_KNOWN_INCOMPAT_FEATURES,
2209 JFS_KNOWN_ROCOMPAT_FEATURES };
2210
2211 if (argc > 1) {
2212 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2213 if (ret) {
2214 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2215 argv[1]);
2216 }
2217 if (ret)
2218 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2219 else
2220 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2221 } else {
2222 fprintf(stdout, "Supported features:");
2223 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2224 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2225 }
2226 }
2227
2228 #ifndef READ_ONLY
2229 void do_punch(int argc, char *argv[])
2230 {
2231 ext2_ino_t ino;
2232 blk64_t start, end;
2233 int err;
2234 errcode_t errcode;
2235
2236 if (common_args_process(argc, argv, 3, 4, argv[0],
2237 "<file> start_blk [end_blk]",
2238 CHECK_FS_RW | CHECK_FS_BITMAPS))
2239 return;
2240
2241 ino = string_to_inode(argv[1]);
2242 if (!ino)
2243 return;
2244 start = parse_ulong(argv[2], argv[0], "logical_block", &err);
2245 if (argc == 4)
2246 end = parse_ulong(argv[3], argv[0], "logical_block", &err);
2247 else
2248 end = ~0;
2249
2250 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2251
2252 if (errcode) {
2253 com_err(argv[0], errcode,
2254 "while truncating inode %u from %llu to %llu\n", ino,
2255 (unsigned long long) start, (unsigned long long) end);
2256 return;
2257 }
2258 }
2259 #endif /* READ_ONLY */
2260
2261 void do_symlink(int argc, char *argv[])
2262 {
2263 char *cp;
2264 ext2_ino_t parent;
2265 char *name, *target;
2266 errcode_t retval;
2267
2268 if (common_args_process(argc, argv, 3, 3, "symlink",
2269 "<filename> <target>", CHECK_FS_RW))
2270 return;
2271
2272 cp = strrchr(argv[1], '/');
2273 if (cp) {
2274 *cp = 0;
2275 parent = string_to_inode(argv[1]);
2276 if (!parent) {
2277 com_err(argv[1], ENOENT, 0);
2278 return;
2279 }
2280 name = cp+1;
2281 } else {
2282 parent = cwd;
2283 name = argv[1];
2284 }
2285 target = argv[2];
2286
2287 try_again:
2288 retval = ext2fs_symlink(current_fs, parent, 0, name, target);
2289 if (retval == EXT2_ET_DIR_NO_SPACE) {
2290 retval = ext2fs_expand_dir(current_fs, parent);
2291 if (retval) {
2292 com_err(argv[0], retval, "while expanding directory");
2293 return;
2294 }
2295 goto try_again;
2296 }
2297 if (retval) {
2298 com_err("ext2fs_symlink", retval, 0);
2299 return;
2300 }
2301
2302 }
2303
2304 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2305 {
2306 struct ext2_super_block *sb;
2307 struct mmp_struct *mmp_s;
2308 time_t t;
2309 errcode_t retval = 0;
2310
2311 if (check_fs_open(argv[0]))
2312 return;
2313
2314 sb = current_fs->super;
2315 if (sb->s_mmp_block <= sb->s_first_data_block ||
2316 sb->s_mmp_block >= ext2fs_blocks_count(sb)) {
2317 com_err(argv[0], EXT2_ET_MMP_BAD_BLOCK, "while dumping it.\n");
2318 return;
2319 }
2320
2321 if (current_fs->mmp_buf == NULL) {
2322 retval = ext2fs_get_mem(current_fs->blocksize,
2323 &current_fs->mmp_buf);
2324 if (retval) {
2325 com_err(argv[0], retval, "allocating MMP buffer.\n");
2326 return;
2327 }
2328 }
2329
2330 mmp_s = current_fs->mmp_buf;
2331
2332 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2333 current_fs->mmp_buf);
2334 if (retval) {
2335 com_err(argv[0], retval, "reading MMP block.\n");
2336 return;
2337 }
2338
2339 t = mmp_s->mmp_time;
2340 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2341 fprintf(stdout, "update_interval: %d\n",
2342 current_fs->super->s_mmp_update_interval);
2343 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2344 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2345 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2346 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2347 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2348 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2349 }
2350
2351 static int source_file(const char *cmd_file, int ss_idx)
2352 {
2353 FILE *f;
2354 char buf[BUFSIZ];
2355 char *cp;
2356 int exit_status = 0;
2357 int retval;
2358
2359 if (strcmp(cmd_file, "-") == 0)
2360 f = stdin;
2361 else {
2362 f = fopen(cmd_file, "r");
2363 if (!f) {
2364 perror(cmd_file);
2365 exit(1);
2366 }
2367 }
2368 fflush(stdout);
2369 fflush(stderr);
2370 setbuf(stdout, NULL);
2371 setbuf(stderr, NULL);
2372 while (!feof(f)) {
2373 if (fgets(buf, sizeof(buf), f) == NULL)
2374 break;
2375 cp = strchr(buf, '\n');
2376 if (cp)
2377 *cp = 0;
2378 cp = strchr(buf, '\r');
2379 if (cp)
2380 *cp = 0;
2381 printf("debugfs: %s\n", buf);
2382 retval = ss_execute_line(ss_idx, buf);
2383 if (retval) {
2384 ss_perror(ss_idx, retval, buf);
2385 exit_status++;
2386 }
2387 }
2388 if (f != stdin)
2389 fclose(f);
2390 return exit_status;
2391 }
2392
2393 int main(int argc, char **argv)
2394 {
2395 int retval;
2396 const char *usage =
2397 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2398 "[-R request] [-V] ["
2399 #ifndef READ_ONLY
2400 "[-w] "
2401 #endif
2402 "[-c] device]";
2403 int c;
2404 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2405 char *request = 0;
2406 int exit_status = 0;
2407 char *cmd_file = 0;
2408 blk64_t superblock = 0;
2409 blk64_t blocksize = 0;
2410 int catastrophic = 0;
2411 char *data_filename = 0;
2412 #ifdef READ_ONLY
2413 const char *opt_string = "icR:f:b:s:Vd:D";
2414 #else
2415 const char *opt_string = "iwcR:f:b:s:Vd:D";
2416 #endif
2417
2418 if (debug_prog_name == 0)
2419 #ifdef READ_ONLY
2420 debug_prog_name = "rdebugfs";
2421 #else
2422 debug_prog_name = "debugfs";
2423 #endif
2424 add_error_table(&et_ext2_error_table);
2425 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2426 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2427
2428 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2429 switch (c) {
2430 case 'R':
2431 request = optarg;
2432 break;
2433 case 'f':
2434 cmd_file = optarg;
2435 break;
2436 case 'd':
2437 data_filename = optarg;
2438 break;
2439 case 'i':
2440 open_flags |= EXT2_FLAG_IMAGE_FILE;
2441 break;
2442 #ifndef READ_ONLY
2443 case 'w':
2444 open_flags |= EXT2_FLAG_RW;
2445 break;
2446 #endif
2447 case 'D':
2448 open_flags |= EXT2_FLAG_DIRECT_IO;
2449 break;
2450 case 'b':
2451 blocksize = parse_ulong(optarg, argv[0],
2452 "block size", 0);
2453 break;
2454 case 's':
2455 superblock = parse_ulong(optarg, argv[0],
2456 "superblock number", 0);
2457 break;
2458 case 'c':
2459 catastrophic = 1;
2460 break;
2461 case 'V':
2462 /* Print version number and exit */
2463 fprintf(stderr, "\tUsing %s\n",
2464 error_message(EXT2_ET_BASE));
2465 exit(0);
2466 default:
2467 com_err(argv[0], 0, usage, debug_prog_name);
2468 return 1;
2469 }
2470 }
2471 if (optind < argc)
2472 open_filesystem(argv[optind], open_flags,
2473 superblock, blocksize, catastrophic,
2474 data_filename);
2475
2476 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2477 &debug_cmds, &retval);
2478 if (retval) {
2479 ss_perror(sci_idx, retval, "creating invocation");
2480 exit(1);
2481 }
2482 ss_get_readline(sci_idx);
2483
2484 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2485 if (retval) {
2486 ss_perror(sci_idx, retval, "adding standard requests");
2487 exit (1);
2488 }
2489 if (extra_cmds)
2490 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2491 if (retval) {
2492 ss_perror(sci_idx, retval, "adding extra requests");
2493 exit (1);
2494 }
2495 if (request) {
2496 retval = 0;
2497 retval = ss_execute_line(sci_idx, request);
2498 if (retval) {
2499 ss_perror(sci_idx, retval, request);
2500 exit_status++;
2501 }
2502 } else if (cmd_file) {
2503 exit_status = source_file(cmd_file, sci_idx);
2504 } else {
2505 ss_listen(sci_idx);
2506 }
2507
2508 ss_delete_invocation(sci_idx);
2509
2510 if (current_fs)
2511 close_filesystem();
2512
2513 remove_error_table(&et_ext2_error_table);
2514 return exit_status;
2515 }