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