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