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