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