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