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