📄 debugreiserfs.c
字号:
case 'l': /* --logfile */ data->log_file_name = optarg; data->log = fopen (optarg, "w"); if (!data->log) { fprintf (stderr, "debugreiserfs: Cannot not open " "\'%s\': %s", optarg, strerror(errno)); } break; case 'L' : /* random fs corruption */ data->mode = DO_RANDOM_CORRUPTION; break; case 'V': data->mode = DO_NOTHING; break; case 'v': data->options |= BE_VERBOSE; break; } } if (data->mode == DO_NOTHING) { print_banner(program_name); exit(0); } if (optind != argc - 1) /* only one non-option argument is permitted */ print_usage_and_exit(); print_banner(program_name); data->device_name = argv[optind]; return argv[optind];}void pack_partition (reiserfs_filsys_t * fs);static void do_pack (reiserfs_filsys_t * fs){ if (certain_block (fs)) pack_one_block (fs, certain_block (fs)); else pack_partition (fs);}/*static int comp (const void * vp1, const void * vp2){ const int * p1, * p2; p1 = vp1; p2 = vp2; if (*p1 < *p2) return -1; if (*p1 > *p2) return 1; return 0;}*/static void init_bitmap (reiserfs_filsys_t * fs){ FILE * fp; unsigned long block_count; if (reiserfs_open_ondisk_bitmap (fs) < 0) reiserfs_exit (1, "Could not open ondisk bitmap"); block_count = get_sb_block_count (fs->fs_ondisk_sb); switch (scan_area (fs)) { case ALL_BLOCKS: input_bitmap (fs) = reiserfs_create_bitmap (block_count); reiserfs_bitmap_fill (input_bitmap (fs)); reiserfs_warning (stderr, "Whole device (%d blocks) is to be scanned\n", reiserfs_bitmap_ones (input_bitmap (fs))); break; case USED_BLOCKS: reiserfs_warning (stderr, "Loading on-disk bitmap .. "); input_bitmap (fs) = reiserfs_create_bitmap (block_count); reiserfs_bitmap_copy (input_bitmap (fs), fs->fs_bitmap2); reiserfs_warning (stderr, "%d bits set - done\n", reiserfs_bitmap_ones (input_bitmap (fs))); break; case UNUSED_BLOCKS: reiserfs_warning (stderr, "Loading on-disk bitmap .. "); input_bitmap (fs) = reiserfs_create_bitmap (block_count); reiserfs_bitmap_copy (input_bitmap (fs), fs->fs_bitmap2); reiserfs_bitmap_invert (input_bitmap (fs)); reiserfs_warning (stderr, "%d bits set - done\n", reiserfs_bitmap_ones (input_bitmap (fs))); break; case EXTERN_BITMAP: fp = fopen (input_bitmap_file_name(fs), "r"); if (!fp) { reiserfs_exit (1, "init_bitmap: could not load bitmap: %m\n"); } input_bitmap (fs) = reiserfs_bitmap_load (fp); if (!input_bitmap (fs)) { reiserfs_exit (1, "could not load fitmap from \"%s\"", input_bitmap_file_name(fs)); } reiserfs_warning (stderr, "%d blocks marked in the given bitmap\n", reiserfs_bitmap_ones (input_bitmap (fs))); fclose (fp); break; default: reiserfs_panic ("No area to scan specified"); }}/* FIXME: statistics does not work */static void do_dump_tree (reiserfs_filsys_t * fs){ if (certain_block (fs)) { print_one_block (fs, certain_block (fs)); return; } if (((data (fs)->options & PRINT_JOURNAL) || (data (fs)->options & PRINT_JOURNAL_HEADER)) && !reiserfs_journal_opened (fs)) { if (reiserfs_open_journal (fs, data(fs)->journal_device_name, O_RDONLY)) { printf ("Could not open journal\n"); return; } } print_filesystem_state (stdout, fs); print_block (stdout, fs, fs->fs_super_bh); if (data (fs)->options & PRINT_JOURNAL) print_journal (fs); if (data (fs)->options & PRINT_JOURNAL_HEADER) print_journal_header (fs); if (data (fs)->options & PRINT_OBJECTID_MAP) print_objectid_map (stdout, fs); if (data (fs)->options & PRINT_BITMAP) print_bmap (stdout, fs, 0/*opt_print_block_map == 1 ? 1 : 0*/); if (data (fs)->options & PRINT_DETAILS) init_bitmap (fs); if (data (fs)->options & PRINT_DETAILS || data (fs)->options & PRINT_TREE_DETAILS) { if (data (fs)->options & PRINT_DETAILS) { print_disk_blocks (fs); printf("The '%s' device with reiserfs has:\n", fs->fs_file_name); } else { print_disk_tree (fs, get_sb_root_block(fs->fs_ondisk_sb)); printf("The internal reiserfs tree has:\n"); } /* print the statistic */ printf ("\t%d internal + %d leaves + %d " "unformatted nodes = %d blocks\n", g_stat_info.nr_internals, g_stat_info.nr_leaves, g_stat_info.nr_unformatted, g_stat_info.nr_internals + g_stat_info.nr_leaves + g_stat_info.nr_unformatted); }}static void callback_badblock_print(reiserfs_filsys_t *fs, struct path *badblock_path, void *data) { struct item_head *tmp_ih; FILE *fd = (FILE *)data; __u32 *ind_item; __u32 i; tmp_ih = get_ih(badblock_path); ind_item = (__u32 *)get_item(badblock_path); for (i = 0; i < I_UNFM_NUM(tmp_ih); i++) fprintf (fd, "%u\n", d32_get (ind_item, i)); pathrelse (badblock_path);}void extract_badblock_list () { FILE *fd; if (!(fd = fopen (badblocks_file, "w"))) { reiserfs_exit(1, "debugreiserfs: could not open badblock file %s\n", badblocks_file); } badblock_list(fs, callback_badblock_print, fd); fclose (fd);}static int str2int (char * str, int * res){ int val; char * tmp; val = (int) strtol (str, &tmp, 0); if (tmp == str) /* could not convert string into a number */ return 0; *res = val; return 1;}void do_corrupt_blocks (reiserfs_filsys_t * fs){ char * line; FILE * fd; size_t n = 0; int numblock; fd = fopen (corruption_list_file, "r"); if (fd == NULL) { reiserfs_exit(1, "debugreiserfs: could not open corruption " "list file %s\n", corruption_list_file); } while (1) { line = NULL; n = 0; if (getline (&line, &n, fd) == -1) { break; } /* remove '\n' */ line [strlen (line) - 1] = '\0'; if (str2int(line, &numblock)) { data (fs)->block = (unsigned long) numblock; } else { do_corrupt_one_block(fs, line); } printf ("before free line : %s\n", line); free (line); printf ("after free\n"); reiserfs_reopen (fs, O_RDWR); } fclose (fd); return;}void debugreiserfs_zero_reiserfs(reiserfs_filsys_t * fs) { unsigned long done, total, i; struct buffer_head * bh; reiserfs_reopen (fs, O_RDWR); total = reiserfs_bitmap_ones (input_bitmap (fs)); done = 0; for (i = 0; i < input_bitmap(fs)->bm_bit_size; i ++) { if (!reiserfs_bitmap_test_bit (input_bitmap(fs), i)) continue; bh = getblk(fs->fs_dev, i, fs->fs_blocksize); if (!bh) die("Could not get block %lu\n", i); memset(bh->b_data, 0, fs->fs_blocksize); mark_buffer_dirty(bh); mark_buffer_uptodate(bh, 0); bwrite(bh); brelse (bh); print_how_far (stderr, &done, total, 1, be_quiet (fs)); } fprintf(stderr, "\n"); fflush(stderr);}/* FIXME: need to open reiserfs filesystem first */int main (int argc, char * argv[]){ char * file_name; int error; struct debugreiserfs_data * data; data = getmem (sizeof (struct debugreiserfs_data)); file_name = parse_options (data, argc, argv); if (data->mode == DO_UNPACK) { do_unpack(file_name, data->journal_device_name, data->input_bitmap, data->options & BE_VERBOSE); return 0; } fs = reiserfs_open (file_name, O_RDONLY, &error, data, 0); if (no_reiserfs_found (fs)) { reiserfs_exit (1, "\n\ndebugreiserfs: can not open reiserfs on " "\"%s\": %s\n\n", file_name, error ? strerror(error) : "no filesystem found"); exit(1) ; } if (reiserfs_open_journal (fs, data (fs)->journal_device_name, O_RDONLY)) { fprintf(stderr,"\ndebugreiserfs: Failed to open the fs journal.\n"); } switch (debug_mode (fs)) { case DO_STAT: init_bitmap (fs); do_stat (fs); break; case DO_PACK: init_bitmap (fs); do_pack (fs); break; case DO_CORRUPT_ONE: reiserfs_reopen (fs, O_RDWR); do_corrupt_one_block (fs, (char *)NULL); break; case DO_CORRUPT_FILE: reiserfs_reopen (fs, O_RDWR); do_corrupt_blocks (fs); break; case DO_RANDOM_CORRUPTION: reiserfs_reopen (fs, O_RDWR); /* do_leaves_corruption (fs); do_bitmap_corruption (fs); */ do_fs_random_corrupt (fs); break; case DO_DUMP: do_dump_tree (fs); break; case DO_SCAN: case DO_SCAN_FOR_NAME: case DO_LOOK_FOR_NAME: case DO_SCAN_JOURNAL: init_bitmap (fs); do_scan (fs); break; case DO_FILE_MAP: print_map(fs); break; case DO_RECOVER: do_recover (fs); break; case DO_TEST: /*do_test (fs);*/ break; case DO_EXTRACT_BADBLOCKS: reiserfs_warning (stderr, "Will try to extract list of bad blocks " "and save it to '%s' file\n", badblocks_file); extract_badblock_list (); reiserfs_warning (stderr, "Done\n\n"); break; case DO_ZERO: init_bitmap (fs); debugreiserfs_zero_reiserfs(fs); break; } reiserfs_close (fs); if (data(fs)->log) { fclose (data(fs)->log); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -