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