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