2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup lwext4
37 #include "ext4_config.h"
38 #include "ext4_super.h"
39 #include "ext4_debug.h"
40 #include "ext4_mkfs.h"
46 #define DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y))
47 #define EXT4_ALIGN(x, y) ((y) * DIV_ROUND_UP((x), (y)))
50 struct ext4_sblock *sb;
51 struct ext4_bgroup *bg_desc;
52 struct xattr_list_element *xattrs;
53 uint32_t first_data_block;
55 uint32_t inode_table_blocks;
57 uint32_t bg_desc_blocks;
58 uint32_t default_i_flags;
59 uint32_t blocks_per_ind;
60 uint32_t blocks_per_dind;
61 uint32_t blocks_per_tind;
64 static inline int log_2(int j)
68 for (i = 0; j > 0; i++)
74 static int sb2info(struct ext4_sblock *sb, struct ext4_mkfs_info *info)
76 if (to_le16(sb->magic) != EXT4_SUPERBLOCK_MAGIC)
79 info->block_size = 1024 << to_le32(sb->log_block_size);
80 info->blocks_per_group = to_le32(sb->blocks_per_group);
81 info->inodes_per_group = to_le32(sb->inodes_per_group);
82 info->inode_size = to_le16(sb->inode_size);
83 info->inodes = to_le32(sb->inodes_count);
84 info->feat_ro_compat = to_le32(sb->features_read_only);
85 info->feat_compat = to_le32(sb->features_compatible);
86 info->feat_incompat = to_le32(sb->features_incompatible);
87 info->bg_desc_reserve_blocks = to_le16(sb->s_reserved_gdt_blocks);
88 info->label = sb->volume_name;
89 info->len = (uint64_t)info->block_size * ext4_sb_get_blocks_cnt(sb);
94 static uint32_t compute_blocks_per_group(struct ext4_mkfs_info *info)
96 return info->block_size * 8;
99 static uint32_t compute_inodes(struct ext4_mkfs_info *info)
101 return DIV_ROUND_UP(info->len, info->block_size) / 4;
104 static uint32_t compute_inodes_per_group(struct ext4_mkfs_info *info)
106 uint32_t blocks = DIV_ROUND_UP(info->len, info->block_size);
107 uint32_t block_groups = DIV_ROUND_UP(blocks, info->blocks_per_group);
108 uint32_t inodes = DIV_ROUND_UP(info->inodes, block_groups);
109 inodes = EXT4_ALIGN(inodes, (info->block_size / info->inode_size));
111 /* After properly rounding up the number of inodes/group,
112 * make sure to update the total inodes field in the info struct.
114 info->inodes = inodes * block_groups;
119 static uint32_t compute_bg_desc_reserve_blocks(struct ext4_mkfs_info *info)
121 uint32_t blocks = DIV_ROUND_UP(info->len, info->block_size);
122 uint32_t block_groups = DIV_ROUND_UP(blocks, info->blocks_per_group);
123 uint32_t bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext4_bgroup),
126 uint32_t bg_desc_reserve_blocks =
127 DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext4_bgroup),
128 info->block_size) - bg_desc_blocks;
130 if (bg_desc_reserve_blocks > info->block_size / sizeof(uint32_t))
131 bg_desc_reserve_blocks = info->block_size / sizeof(uint32_t);
133 return bg_desc_reserve_blocks;
136 static uint32_t compute_journal_blocks(struct ext4_mkfs_info *info)
138 uint32_t journal_blocks = DIV_ROUND_UP(info->len, info->block_size) / 64;
139 if (journal_blocks < 1024)
140 journal_blocks = 1024;
141 if (journal_blocks > 32768)
142 journal_blocks = 32768;
143 return journal_blocks;
146 static bool has_superblock(struct ext4_mkfs_info *info, uint32_t bgid)
148 if (!(info->feat_ro_compat & EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER))
151 return ext4_sb_sparse(bgid);
154 static int create_fs_aux_info(struct fs_aux_info *aux_info, struct ext4_mkfs_info *info)
156 aux_info->first_data_block = (info->block_size > 1024) ? 0 : 1;
157 aux_info->len_blocks = info->len / info->block_size;
158 aux_info->inode_table_blocks = DIV_ROUND_UP(info->inodes_per_group * info->inode_size,
160 aux_info->groups = DIV_ROUND_UP(aux_info->len_blocks - aux_info->first_data_block,
161 info->blocks_per_group);
162 aux_info->blocks_per_ind = info->block_size / sizeof(uint32_t);
163 aux_info->blocks_per_dind = aux_info->blocks_per_ind * aux_info->blocks_per_ind;
164 aux_info->blocks_per_tind = aux_info->blocks_per_dind * aux_info->blocks_per_dind;
166 aux_info->bg_desc_blocks =
167 DIV_ROUND_UP(aux_info->groups * sizeof(struct ext4_bgroup),
170 aux_info->default_i_flags = EXT4_INODE_FLAG_NOATIME;
172 uint32_t last_group_size = aux_info->len_blocks % info->blocks_per_group;
173 uint32_t last_header_size = 2 + aux_info->inode_table_blocks;
174 if (has_superblock(info, aux_info->groups - 1))
175 last_header_size += 1 + aux_info->bg_desc_blocks +
176 info->bg_desc_reserve_blocks;
178 if (last_group_size > 0 && last_group_size < last_header_size) {
180 aux_info->len_blocks -= last_group_size;
183 aux_info->sb = calloc(1, sizeof(struct ext4_sblock));
187 aux_info->bg_desc = calloc(sizeof(struct ext4_bgroup),
188 aux_info->bg_desc_blocks);
189 if (!aux_info->bg_desc)
192 aux_info->xattrs = NULL;
196 static void release_fs_aux_info(struct fs_aux_info *aux_info)
201 free(aux_info->bg_desc);
205 /* Fill in the superblock memory buffer based on the filesystem parameters */
206 static void fill_in_sb(struct fs_aux_info *aux_info, struct ext4_mkfs_info *info)
209 struct ext4_sblock *sb = aux_info->sb;
211 sb->inodes_count = info->inodes_per_group * aux_info->groups;
212 sb->blocks_count_lo = aux_info->len_blocks;
213 sb->reserved_blocks_count_lo = 0;
214 sb->free_blocks_count_lo = 0;
215 sb->free_inodes_count = 0;
216 sb->first_data_block = aux_info->first_data_block;
217 sb->log_block_size = log_2(info->block_size / 1024);
218 sb->log_cluster_size = log_2(info->block_size / 1024);
219 sb->blocks_per_group = info->blocks_per_group;
220 sb->frags_per_group = info->blocks_per_group;
221 sb->inodes_per_group = info->inodes_per_group;
225 sb->max_mount_count = 0xFFFF;
226 sb->magic = EXT4_SUPERBLOCK_MAGIC;
227 sb->state = EXT4_SUPERBLOCK_STATE_VALID_FS;
228 sb->errors = EXT4_SUPERBLOCK_ERRORS_RO;
229 sb->minor_rev_level = 0;
230 sb->last_check_time = 0;
231 sb->check_interval = 0;
232 sb->creator_os = EXT4_SUPERBLOCK_OS_LINUX;
237 sb->first_inode = EXT4_GOOD_OLD_FIRST_INO;
238 sb->inode_size = info->inode_size;
239 sb->block_group_index = 0;
240 sb->features_compatible = info->feat_compat;
241 sb->features_incompatible = info->feat_incompat;
242 sb->features_read_only = info->feat_ro_compat;
244 memset(sb->uuid, 0, sizeof(sb->uuid));
246 memset(sb->volume_name, 0, sizeof(sb->volume_name));
247 strncpy(sb->volume_name, info->label, sizeof(sb->volume_name));
248 memset(sb->last_mounted, 0, sizeof(sb->last_mounted));
249 sb->algorithm_usage_bitmap = 0;
251 sb->s_reserved_gdt_blocks = info->bg_desc_reserve_blocks;
252 sb->s_prealloc_blocks = 0;
253 sb->s_prealloc_dir_blocks = 0;
255 //memcpy(sb->journal_uuid, sb->uuid, sizeof(sb->journal_uuid));
256 if (info->feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
257 sb->journal_inode_number = EXT4_JOURNAL_INO;
260 sb->hash_seed[0] = 0; /* FIXME */
261 sb->default_hash_version = EXT2_HTREE_HALF_MD4;
262 sb->reserved_char_pad = 1;
263 sb->desc_size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE;
264 sb->default_mount_opts = 0; /* FIXME */
265 sb->first_meta_bg = 0;
267 //sb->jnl_blocks[17]; /* FIXME */
269 sb->blocks_count_hi = aux_info->len_blocks >> 32;
270 sb->reserved_blocks_count_hi = 0;
271 sb->free_blocks_count_hi = 0;
272 sb->min_extra_isize = sizeof(struct ext4_inode) -
273 EXT4_GOOD_OLD_INODE_SIZE;
274 sb->want_extra_isize = sizeof(struct ext4_inode) -
275 EXT4_GOOD_OLD_INODE_SIZE;
278 sb->mmp_interval = 0;
280 sb->raid_stripe_width = 0;
281 sb->log_groups_per_flex = 0;
282 sb->kbytes_written = 0;
284 sb->block_group_index = 0;
286 for (i = 0; i < aux_info->groups; i++) {
288 uint64_t group_start_block = aux_info->first_data_block + i *
289 info->blocks_per_group;
290 uint32_t header_size = 0;
292 if (has_superblock(info, i))
293 header_size = 1 + aux_info->bg_desc_blocks + info->bg_desc_reserve_blocks;
296 aux_info->bg_desc[i].block_bitmap_lo = group_start_block + header_size;
297 aux_info->bg_desc[i].inode_bitmap_lo = group_start_block + header_size + 1;
298 aux_info->bg_desc[i].inode_table_first_block_lo = group_start_block + header_size + 2;
300 aux_info->bg_desc[i].free_blocks_count_lo = sb->blocks_per_group;
301 aux_info->bg_desc[i].free_inodes_count_lo = sb->inodes_per_group;
302 aux_info->bg_desc[i].used_dirs_count_lo = 0;
306 static int bdev_write_sb(struct ext4_blockdev *bd, struct fs_aux_info *aux_info,
307 struct ext4_mkfs_info *info)
313 /* write out the backup superblocks */
314 for (i = 1; i < aux_info->groups; i++) {
315 if (has_superblock(info, i)) {
316 offset = info->block_size * (aux_info->first_data_block
317 + i * info->blocks_per_group);
319 aux_info->sb->block_group_index = i;
320 r = ext4_block_writebytes(bd, offset, aux_info->sb, 1024);
326 /* write out the primary superblock */
327 aux_info->sb->block_group_index = 0;
328 return ext4_block_writebytes(bd, 1024, aux_info->sb, 1024);
332 int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
335 struct ext4_sblock *sb = NULL;
336 r = ext4_block_init(bd);
340 sb = malloc(sizeof(struct ext4_sblock));
345 r = ext4_sb_read(bd, sb);
349 r = sb2info(sb, info);
358 int ext4_mkfs(struct ext4_blockdev *bd, struct ext4_mkfs_info *info)
361 r = ext4_block_init(bd);
366 info->len = bd->ph_bcnt * bd->ph_bsize;
368 if (info->block_size == 0)
369 info->block_size = 4096; /*Set block size to default value*/
371 /* Round down the filesystem length to be a multiple of the block size */
372 info->len &= ~((uint64_t)info->block_size - 1);
374 if (info->journal_blocks == 0)
375 info->journal_blocks = compute_journal_blocks(info);
377 if (info->blocks_per_group == 0)
378 info->blocks_per_group = compute_blocks_per_group(info);
380 if (info->inodes == 0)
381 info->inodes = compute_inodes(info);
383 if (info->inode_size == 0)
384 info->inode_size = 256;
386 if (info->label == NULL)
389 info->inodes_per_group = compute_inodes_per_group(info);
391 info->feat_compat = CONFIG_FEATURE_COMPAT_SUPP;
392 info->feat_ro_compat = CONFIG_FEATURE_RO_COMPAT_SUPP;
393 info->feat_incompat = CONFIG_FEATURE_INCOMPAT_SUPP;
395 if (info->no_journal == 0)
396 info->feat_compat |= EXT4_FEATURE_COMPAT_HAS_JOURNAL;
398 info->bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks(info);
400 ext4_dbg(DEBUG_MKFS, DBG_INFO "Creating filesystem with parameters:\n");
401 ext4_dbg(DEBUG_MKFS, DBG_NONE "Size: %llu\n",
402 (long long unsigned int)info->len);
403 ext4_dbg(DEBUG_MKFS, DBG_NONE "Block size: %"PRIu32"\n", info->block_size);
404 ext4_dbg(DEBUG_MKFS, DBG_NONE "Blocks per group: %"PRIu32"\n",
405 info->blocks_per_group);
406 ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes per group: %"PRIu32"\n",
407 info->inodes_per_group);
408 ext4_dbg(DEBUG_MKFS, DBG_NONE "Inode size: %"PRIu32"\n", info->inode_size);
409 ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes: %"PRIu32"\n", info->inodes);
410 ext4_dbg(DEBUG_MKFS, DBG_NONE "Journal blocks: %"PRIu32"\n",
411 info->journal_blocks);
412 ext4_dbg(DEBUG_MKFS, DBG_NONE "Features ro_compat: 0x%x\n",
413 info->feat_ro_compat);
414 ext4_dbg(DEBUG_MKFS, DBG_NONE "Features compat: 0x%x\n",
416 ext4_dbg(DEBUG_MKFS, DBG_NONE "Features incompat: 0x%x\n",
417 info->feat_incompat);
418 ext4_dbg(DEBUG_MKFS, DBG_NONE "BG desc reserve: %"PRIu32"\n",
419 info->bg_desc_reserve_blocks);
420 ext4_dbg(DEBUG_MKFS, DBG_NONE "journal: %s\n",
421 !info->no_journal ? "yes" : "no");
422 ext4_dbg(DEBUG_MKFS, DBG_NONE "Label: %s\n", info->label);
424 struct fs_aux_info aux_info;
425 memset(&aux_info, 0, sizeof(struct fs_aux_info));
427 r = create_fs_aux_info(&aux_info, info);
431 fill_in_sb(&aux_info, info);
434 r = bdev_write_sb(bd, &aux_info, info);
439 release_fs_aux_info(&aux_info);