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