errcode_t ocfs2_read_whole_file(ocfs2_filesys *fs, uint64_t blkno, char **buf, int *len) { struct read_whole_context ctx; errcode_t retval; char *inode_buf; struct ocfs2_dinode *di; /* So the caller can see nothing was read */ *len = 0; *buf = NULL; retval = ocfs2_malloc_block(fs->fs_io, &inode_buf); if (retval) return retval; retval = ocfs2_read_inode(fs, blkno, inode_buf); if (retval) goto out_free; di = (struct ocfs2_dinode *)inode_buf; /* Arbitrary limit for our malloc */ retval = OCFS2_ET_INVALID_ARGUMENT; if (di->i_size > INT_MAX) goto out_free; retval = ocfs2_malloc_blocks(fs->fs_io, ocfs2_blocks_in_bytes(fs, di->i_size), buf); if (retval) goto out_free; if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) return ocfs2_inline_data_read(di, *buf, di->i_size, 0, (uint32_t *)len); ctx.buf = *buf; ctx.ptr = *buf; ctx.size = di->i_size; ctx.offset = 0; ctx.errcode = 0; retval = ocfs2_block_iterate(fs, blkno, 0, read_whole_func, &ctx); *len = ctx.size; if (ctx.offset < ctx.size) *len = ctx.offset; out_free: ocfs2_free(&inode_buf); if (!(*len)) { ocfs2_free(buf); *buf = NULL; } if (retval) return retval; return ctx.errcode; }
int main(int argc, char *argv[]) { errcode_t ret; uint64_t blkno, sys_blkno; int c; char *filename, *buf; const char *bitmap_name = ocfs2_system_inodes[GLOBAL_BITMAP_SYSTEM_INODE].si_name; ocfs2_filesys *fs; struct ocfs2_dinode *di; struct walk_block wb; blkno = OCFS2_SUPER_BLOCK_BLKNO; initialize_ocfs_error_table(); while ((c = getopt(argc, argv, "i:")) != EOF) { switch (c) { case 'i': blkno = read_number(optarg); if (blkno <= OCFS2_SUPER_BLOCK_BLKNO) { fprintf(stderr, "Invalid inode block: %s\n", optarg); print_usage(); return 1; } break; default: print_usage(); return 1; break; } } if (optind >= argc) { fprintf(stderr, "Missing filename\n"); print_usage(); return 1; } filename = argv[optind]; ret = ocfs2_open(filename, OCFS2_FLAG_RW, 0, 0, &fs); if (ret) { com_err(argv[0], ret, "while opening file \"%s\"", filename); goto out; } if (blkno == OCFS2_SUPER_BLOCK_BLKNO) { sys_blkno = OCFS2_RAW_SB(fs->fs_super)->s_system_dir_blkno; ret = ocfs2_lookup(fs, sys_blkno, bitmap_name, strlen(bitmap_name), NULL, &blkno); if (ret) { com_err(argv[0], ret, "while looking up \"%s\"", bitmap_name); goto out; } } ret = ocfs2_malloc_block(fs->fs_io, &buf); if (ret) { com_err(argv[0], ret, "while allocating inode buffer"); goto out_close; } memset(&wb, 0, sizeof(wb)); ret = ocfs2_read_inode(fs, blkno, buf); if (ret) { com_err(argv[0], ret, "while reading inode %"PRIu64, blkno); goto out_free; } di = (struct ocfs2_dinode *)buf; wb.di = di; ret = ocfs2_malloc_block(fs->fs_io, &wb.buf); if (ret) { com_err(argv[0], ret, "while allocating block buffer"); goto out_free; } ret = ocfs2_block_iterate(fs, blkno, 0, walk_blocks_func, &wb); if (ret) { com_err(argv[0], ret, "while walking blocks"); goto out_free; } di->id1.bitmap1.i_used = wb.used; ret = ocfs2_write_inode(fs, blkno, buf); if (ret) { com_err(argv[0], ret, "while reading inode %"PRIu64, blkno); goto out_free; } out_free: if (wb.buf) ocfs2_free(&wb.buf); ocfs2_free(&buf); out_close: ret = ocfs2_close(fs); if (ret) { com_err(argv[0], ret, "while closing file \"%s\"", filename); } out: return 0; }
int main(int argc, char *argv[]) { errcode_t ret; uint64_t blkno; int c; int walk_blocks = 0, walk_extents = 0; char *filename, *buf, *eb_buf = NULL; ocfs2_filesys *fs; struct ocfs2_dinode *di; struct walk_it wi; struct walk_block wb; blkno = OCFS2_SUPER_BLOCK_BLKNO; initialize_ocfs_error_table(); while ((c = getopt(argc, argv, "bei:")) != EOF) { switch (c) { case 'b': walk_blocks = 1; break; case 'e': walk_extents = 1; break; case 'i': blkno = read_number(optarg); if (blkno <= OCFS2_SUPER_BLOCK_BLKNO) { fprintf(stderr, "Invalid inode block: %s\n", optarg); print_usage(); return 1; } break; default: print_usage(); return 1; break; } } if (optind >= argc) { fprintf(stderr, "Missing filename\n"); print_usage(); return 1; } filename = argv[optind]; if (!(walk_blocks + walk_extents)) { fprintf(stderr, "No operation specified\n"); print_usage(); return 1; } ret = ocfs2_open(filename, OCFS2_FLAG_RO, 0, 0, &fs); if (ret) { com_err(argv[0], ret, "while opening file \"%s\"", filename); goto out; } ret = ocfs2_malloc_block(fs->fs_io, &buf); if (ret) { com_err(argv[0], ret, "while allocating inode buffer"); goto out_close; } ret = ocfs2_read_inode(fs, blkno, buf); if (ret) { com_err(argv[0], ret, "while reading inode %"PRIu64, blkno); goto out_free; } di = (struct ocfs2_dinode *)buf; fprintf(stdout, "OCFS2 inode %"PRIu64" on \"%s\" has depth %"PRId16"\n", blkno, filename, di->id2.i_list.l_tree_depth); if (walk_extents) { if (di->id2.i_list.l_tree_depth) { ret = ocfs2_malloc_blocks(fs->fs_io, di->id2.i_list.l_tree_depth, &eb_buf); if (ret) { com_err(argv[0], ret, "while allocating eb buffer"); goto out_free; } } wi.di = di; ret = ocfs2_extent_iterate(fs, blkno, 0, eb_buf, walk_extents_func, &wi); if (ret) { com_err(argv[0], ret, "while walking extents"); goto out_free; } } if (walk_blocks) { wb.di = di; wb.run_first_blkno = wb.run_first_bcount = wb.run_prev_blkno = 0; wb.last_block = (wb.di->i_size + (fs->fs_blocksize - 1)) / fs->fs_blocksize; ret = ocfs2_block_iterate(fs, blkno, 0, walk_blocks_func, &wb); if (ret) { com_err(argv[0], ret, "while walking blocks"); goto out_free; } } out_free: if (eb_buf) ocfs2_free(&eb_buf); ocfs2_free(&buf); out_close: ret = ocfs2_close(fs); if (ret) { com_err(argv[0], ret, "while closing file \"%s\"", filename); } out: return 0; }