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