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