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