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