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