void ext4_create_resize_inode() { struct block_allocation *reserve_inode_alloc = create_allocation(); u32 reserve_inode_len = 0; unsigned int i; struct ext4_inode *inode = get_inode(EXT4_RESIZE_INO); if (inode == NULL) { error("failed to get resize inode"); return; } for (i = 0; i < aux_info.groups; i++) { if (ext4_bg_has_super_block(i)) { u64 group_start_block = aux_info.first_data_block + i * info.blocks_per_group; u32 reserved_block_start = group_start_block + 1 + aux_info.bg_desc_blocks; u32 reserved_block_len = info.bg_desc_reserve_blocks; append_region(reserve_inode_alloc, reserved_block_start, reserved_block_len, i); reserve_inode_len += reserved_block_len; } } inode_attach_resize(inode, reserve_inode_alloc); inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR; inode->i_links_count = 1; free_alloc(reserve_inode_alloc); }
/* Allocates enough blocks to hold len bytes, with backing_len bytes in a data buffer, and connects them to an inode. Returns a pointer to the data buffer. */ u8 *inode_allocate_data_indirect(struct ext4_inode *inode, unsigned long len, unsigned long backing_len) { struct block_allocation *alloc; u32 block_len = DIV_ROUND_UP(len, info.block_size); u8 *data = NULL; alloc = do_inode_allocate_indirect(inode, block_len); if (alloc == NULL) { error("failed to allocate extents for %lu bytes", len); return NULL; } if (backing_len) { data = create_backing(alloc, backing_len); if (!data) error("failed to create backing for %lu bytes", backing_len); } rewind_alloc(alloc); if (do_inode_attach_indirect(inode, alloc, block_len)) error("failed to attach blocks to indirect inode"); free_alloc(alloc); return data; }
alloc_t *read_list(FILE *f) { alloc_t *x, *r_old, *r = nil; char c; for (c = next_token_char(f); c != ')'; c = next_token_char(f)) { c = ungetc(c, f); if (c == EOF) { longjmp(fatal, ERROR_PUSHBACK); /* Occurs if we failed to push back a character for re-reading; * but also if we reached end-of-file without closing all parentheses */ } x = read(f); r = cons(x, r); } x = nil; while (r != nil) { x = cons(car(r), x); r_old = r; r = cdr(r); free_alloc(r_old); } return x; }
/* Allocates enough blocks to hold len bytes and connects them to an inode */ void inode_allocate_extents(struct ext4_inode *inode, u64 len) { struct block_allocation *alloc; alloc = do_inode_allocate_extents(inode, len); if (alloc == NULL) { error("failed to allocate extents for %llu bytes", len); return; } free_alloc(alloc); }
/* Allocates enough blocks to hold len bytes, queues them to be written from a file, and connects them to an inode. */ void inode_allocate_file_extents(struct ext4_inode *inode, u64 len, const char *filename) { struct block_allocation *alloc; alloc = do_inode_allocate_extents(inode, len); if (alloc == NULL) { error("failed to allocate extents for %llu bytes", len); return; } extent_create_backing_file(alloc, len, filename); free_alloc(alloc); }
/* x, y, and a are lists; * y is at least as long as x */ alloc_t *pairlis(alloc_t *x, alloc_t *y, alloc_t *a) { /* Tail call modulo cons */ alloc_t *r = nil; alloc_t *r_old; while (x != nil && y != nil) { r = cons(cons(car(x), car(y)), r); x = cdr(x); y = cdr(y); } while (r != nil) { a = cons(car(r), a); r_old = r; r = cdr(r); free_alloc(r_old); } return a; }
alloc_t *evlis(alloc_t *m, alloc_t *a) { alloc_t *e; /* Tail call modulo cons */ alloc_t *r = nil; alloc_t *r_old; while (m != nil) { e = eval(car(m), a); r = cons(e, r); m = cdr(m); } while (r != nil) { m = cons(car(r), m); r_old = r; r = cdr(r); free_alloc(r_old); } return m; }
/* Allocates enough blocks to hold len bytes, with backing_len bytes in a data buffer, and connects them to an inode. Returns a pointer to the data buffer. */ u8 *inode_allocate_data_extents(struct ext4_inode *inode, u64 len, u64 backing_len) { struct block_allocation *alloc; u8 *data = NULL; alloc = do_inode_allocate_extents(inode, len); if (alloc == NULL) { error("failed to allocate extents for %llu bytes", len); return NULL; } if (backing_len) { data = extent_create_backing(alloc, backing_len); if (!data) error("failed to create backing for %llu bytes", backing_len); } free_alloc(alloc); return data; }
/* Allocates enough blocks to hold len bytes and connects them to an inode */ void inode_allocate_indirect(struct ext4_inode *inode, unsigned long len) { struct block_allocation *alloc; u32 block_len = DIV_ROUND_UP(len, info.block_size); u32 indirect_len = indirect_blocks_needed(block_len); alloc = do_inode_allocate_indirect(inode, block_len); if (alloc == NULL) { error("failed to allocate extents for %lu bytes", len); return; } reserve_all_indirect_blocks(alloc, block_len); rewind_alloc(alloc); if (do_inode_attach_indirect(inode, alloc, block_len)) error("failed to attach blocks to indirect inode"); inode->i_flags = 0; inode->i_blocks_lo = (block_len + indirect_len) * info.block_size / 512; inode->i_size_lo = len; free_alloc(alloc); }
int make_ext4fs_internal(int fd, const char *_directory, fs_config_func_t fs_config_func, int gzip, int sparse, int crc, int wipe, int verbose, time_t fixed_time, FILE* block_list_file) { u32 root_inode_num; u16 root_mode; char *directory = NULL; if (setjmp(setjmp_env)) return EXIT_FAILURE; /* Handle a call to longjmp() */ if (_directory == NULL) { fprintf(stderr, "Need a source directory\n"); return EXIT_FAILURE; } directory = canonicalize_rel_slashes(_directory); if (info.len <= 0) info.len = get_file_size(fd); if (info.len <= 0) { fprintf(stderr, "Need size of filesystem\n"); return EXIT_FAILURE; } if (info.block_size <= 0) info.block_size = compute_block_size(); /* Round down the filesystem length to be a multiple of the block size */ info.len &= ~((u64)info.block_size - 1); if (info.journal_blocks == 0) info.journal_blocks = compute_journal_blocks(); if (info.no_journal == 0) info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL; else info.journal_blocks = 0; if (info.blocks_per_group <= 0) info.blocks_per_group = compute_blocks_per_group(); if (info.inodes <= 0) info.inodes = compute_inodes(); if (info.inode_size <= 0) info.inode_size = 256; if (info.label == NULL) info.label = ""; info.inodes_per_group = compute_inodes_per_group(); info.feat_compat |= EXT4_FEATURE_COMPAT_RESIZE_INODE | EXT4_FEATURE_COMPAT_EXT_ATTR; info.feat_ro_compat |= EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | EXT4_FEATURE_RO_COMPAT_LARGE_FILE | EXT4_FEATURE_RO_COMPAT_GDT_CSUM; info.feat_incompat |= EXT4_FEATURE_INCOMPAT_EXTENTS | EXT4_FEATURE_INCOMPAT_FILETYPE; info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks(); printf("Creating filesystem with parameters:\n"); printf(" Size: %"PRIu64"\n", info.len); printf(" Block size: %d\n", info.block_size); printf(" Blocks per group: %d\n", info.blocks_per_group); printf(" Inodes per group: %d\n", info.inodes_per_group); printf(" Inode size: %d\n", info.inode_size); printf(" Journal blocks: %d\n", info.journal_blocks); printf(" Label: %s\n", info.label); ext4_create_fs_aux_info(); printf(" Blocks: %"PRIu64"\n", aux_info.len_blocks); printf(" Block groups: %d\n", aux_info.groups); printf(" Reserved blocks: %"PRIu64"\n", (aux_info.len_blocks / 100) * info.reserve_pcnt); printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks); ext4_sparse_file = sparse_file_new(info.block_size, info.len); block_allocator_init(); ext4_fill_in_sb(); if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED) error("failed to reserve first 10 inodes"); if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL) ext4_create_journal_inode(); if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE) ext4_create_resize_inode(); root_inode_num = build_directory_structure(directory, "", 0, fs_config_func, verbose, fixed_time); root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; inode_set_permissions(root_inode_num, root_mode, 0, 0, 0); ext4_update_free(); ext4_queue_sb(); if (block_list_file) { size_t dirlen = strlen(directory); struct block_allocation* p = get_saved_allocation_chain(); while (p) { if (strncmp(p->filename, directory, dirlen) == 0) { fprintf(block_list_file, "%s", p->filename + dirlen); } else { fprintf(block_list_file, "%s", p->filename); } print_blocks(block_list_file, p); struct block_allocation* pn = p->next; free_alloc(p); p = pn; } } printf("Created filesystem with %d/%d inodes and %d/%d blocks\n", aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, aux_info.sb->s_inodes_count, aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, aux_info.sb->s_blocks_count_lo); if (wipe && WIPE_IS_SUPPORTED) { wipe_block_device(fd, info.len); } write_ext4_image(fd, gzip, sparse, crc); sparse_file_destroy(ext4_sparse_file); ext4_sparse_file = NULL; free(directory); return 0; }
int make_ext4fs_internal(int fd, const char *_directory, const char *_target_out_directory, const char *_mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse, int crc, int wipe, int real_uuid, struct selabel_handle *sehnd, int verbose, time_t fixed_time, FILE* block_list_file) { u32 root_inode_num; u16 root_mode; char *mountpoint; char *directory = NULL; char *target_out_directory = NULL; if (setjmp(setjmp_env)) return EXIT_FAILURE; /* Handle a call to longjmp() */ info.block_device = is_block_device_fd(fd); if (info.block_device && (sparse || gzip || crc)) { fprintf(stderr, "No sparse/gzip/crc allowed for block device\n"); return EXIT_FAILURE; } if (_mountpoint == NULL) { mountpoint = strdup(""); } else { mountpoint = canonicalize_abs_slashes(_mountpoint); } if (_directory) { directory = canonicalize_rel_slashes(_directory); } if (_target_out_directory) { target_out_directory = canonicalize_rel_slashes(_target_out_directory); } if (info.len <= 0) info.len = get_file_size(fd); if (info.len <= 0) { fprintf(stderr, "Need size of filesystem\n"); return EXIT_FAILURE; } if (info.block_size <= 0) info.block_size = compute_block_size(); /* Round down the filesystem length to be a multiple of the block size */ info.len &= ~((u64)info.block_size - 1); if (info.journal_blocks == 0) info.journal_blocks = compute_journal_blocks(); if (info.no_journal == 0) info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL; else info.journal_blocks = 0; if (info.blocks_per_group <= 0) info.blocks_per_group = compute_blocks_per_group(); if (info.inodes <= 0) info.inodes = compute_inodes(); if (info.inode_size <= 0) info.inode_size = 256; if (info.label == NULL) info.label = ""; info.inodes_per_group = compute_inodes_per_group(); info.feat_compat |= EXT4_FEATURE_COMPAT_RESIZE_INODE | EXT4_FEATURE_COMPAT_EXT_ATTR; info.feat_ro_compat |= EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | EXT4_FEATURE_RO_COMPAT_LARGE_FILE | EXT4_FEATURE_RO_COMPAT_GDT_CSUM; info.feat_incompat |= EXT4_FEATURE_INCOMPAT_EXTENTS | EXT4_FEATURE_INCOMPAT_FILETYPE; info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks(); printf("Creating filesystem with parameters:\n"); printf(" Size: %"PRIu64"\n", info.len); printf(" Block size: %d\n", info.block_size); printf(" Blocks per group: %d\n", info.blocks_per_group); printf(" Inodes per group: %d\n", info.inodes_per_group); printf(" Inode size: %d\n", info.inode_size); printf(" Journal blocks: %d\n", info.journal_blocks); printf(" Label: %s\n", info.label); ext4_create_fs_aux_info(); printf(" Blocks: %"PRIu64"\n", aux_info.len_blocks); printf(" Block groups: %d\n", aux_info.groups); printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks); ext4_sparse_file = sparse_file_new(info.block_size, info.len); block_allocator_init(); ext4_fill_in_sb(real_uuid); if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED) error("failed to reserve first 10 inodes"); if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL) ext4_create_journal_inode(); if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE) ext4_create_resize_inode(); #ifdef USE_MINGW // Windows needs only 'create an empty fs image' functionality assert(!directory); root_inode_num = build_default_directory_structure(mountpoint, sehnd); #else if (directory) root_inode_num = build_directory_structure(directory, mountpoint, target_out_directory, 0, fs_config_func, sehnd, verbose, fixed_time); else root_inode_num = build_default_directory_structure(mountpoint, sehnd); #endif root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; inode_set_permissions(root_inode_num, root_mode, 0, 0, 0); #ifndef USE_MINGW if (sehnd) { char *secontext = NULL; if (selabel_lookup(sehnd, &secontext, mountpoint, S_IFDIR) < 0) { error("cannot lookup security context for %s", mountpoint); } if (secontext) { if (verbose) { printf("Labeling %s as %s\n", mountpoint, secontext); } inode_set_selinux(root_inode_num, secontext); } freecon(secontext); } #endif ext4_update_free(); if (block_list_file) { size_t dirlen = directory ? strlen(directory) : 0; struct block_allocation* p = get_saved_allocation_chain(); while (p) { if (directory && strncmp(p->filename, directory, dirlen) == 0) { // substitute mountpoint for the leading directory in the filename, in the output file fprintf(block_list_file, "%s%s", mountpoint, p->filename + dirlen); } else { fprintf(block_list_file, "%s", p->filename); } print_blocks(block_list_file, p); struct block_allocation* pn = p->next; free_alloc(p); p = pn; } } printf("Created filesystem with %d/%d inodes and %d/%d blocks\n", aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, aux_info.sb->s_inodes_count, aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, aux_info.sb->s_blocks_count_lo); if (wipe && WIPE_IS_SUPPORTED) { wipe_block_device(fd, info.len); } write_ext4_image(fd, gzip, sparse, crc); sparse_file_destroy(ext4_sparse_file); ext4_sparse_file = NULL; free(mountpoint); free(directory); return 0; }