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