Lines Matching +full:include +full:- +full:dirs
4 * SPDX-License-Identifier: Apache-2.0
7 #include <zephyr/logging/log.h>
8 #include <zephyr/random/random.h>
9 #include <zephyr/fs/ext2.h>
10 #include <zephyr/sys/byteorder.h>
12 #include "ext2.h"
13 #include "ext2_impl.h"
14 #include "ext2_struct.h"
15 #include "ext2_diskops.h"
23 if (cfg->block_size == 0) { in validate_config()
24 cfg->block_size = ext2_default.block_size; in validate_config()
27 if (cfg->bytes_per_inode == 0) { in validate_config()
28 cfg->bytes_per_inode = ext2_default.bytes_per_inode; in validate_config()
31 if (cfg->volume_name[0] == '\0') { in validate_config()
32 strcpy(cfg->volume_name, "ext2"); in validate_config()
35 if (!cfg->set_uuid) { in validate_config()
37 sys_rand_get(cfg->uuid, 16); in validate_config()
40 cfg->uuid[6] = (cfg->uuid[6] & 0x0f) | 0x40; in validate_config()
41 cfg->uuid[8] = (cfg->uuid[8] & 0x3f) | 0x80; in validate_config()
52 /* Set padding in block-bitmap block */ in set_bitmap_padding()
54 bitmap[used_bytes - 1] = (0xff << (nelems % 8)) & 0xff; in set_bitmap_padding()
57 memset(bitmap + used_bytes, 0xff, cfg->block_size - used_bytes); in set_bitmap_padding()
67 set_value = (1 << bits) - 1; in set_bitmap_bits()
69 to_set -= bits; in set_bitmap_bits()
78 in->i_mode = sys_cpu_to_le16(EXT2_DEF_DIR_MODE); in default_directory_inode()
79 in->i_uid = 0; in default_directory_inode()
80 in->i_size = sys_cpu_to_le32(nblocks * cfg->block_size); in default_directory_inode()
81 in->i_atime = 0; in default_directory_inode()
82 in->i_ctime = 0; in default_directory_inode()
83 in->i_mtime = 0; in default_directory_inode()
84 in->i_dtime = 0; in default_directory_inode()
85 in->i_gid = 0; in default_directory_inode()
86 in->i_blocks = sys_cpu_to_le32(nblocks * cfg->block_size / 512); in default_directory_inode()
87 in->i_flags = 0; in default_directory_inode()
88 in->i_osd1 = 0; in default_directory_inode()
89 in->i_generation = 0; in default_directory_inode()
90 in->i_file_acl = 0; in default_directory_inode()
91 in->i_dir_acl = 0; in default_directory_inode()
92 in->i_faddr = 0; in default_directory_inode()
93 memset(in->i_block, 0, EXT2_INODE_BLOCKS * sizeof(uint32_t)); in default_directory_inode()
102 cfg->block_size, cfg->fs_size, cfg->bytes_per_inode, cfg->uuid, in ext2_format()
103 cfg->volume_name); in ext2_format()
105 uint32_t fs_memory = cfg->fs_size ? MIN(cfg->fs_size, fs->device_size) : fs->device_size; in ext2_format()
109 * To obtain it we calculate: log(block_size) - 11 in ext2_format()
111 uint8_t block_log_size = find_msb_set(cfg->block_size) - 11; in ext2_format()
113 LOG_INF("[Memory] available:%lld requested:%d", fs->device_size, fs_memory); in ext2_format()
115 if (fs_memory > fs->device_size) { in ext2_format()
117 return -ENOSPC; in ext2_format()
121 fs_memory -= CONFIG_EXT2_DISK_STARTING_SECTOR * fs->write_size; in ext2_format()
123 uint32_t blocks_count = fs_memory / cfg->block_size; in ext2_format()
124 uint32_t blocks_per_group = cfg->block_size * 8; in ext2_format()
125 uint32_t inodes_per_block = cfg->block_size / sizeof(struct ext2_disk_inode); in ext2_format()
126 uint32_t mem_per_inode = cfg->bytes_per_inode + sizeof(struct ext2_disk_inode); in ext2_format()
131 return -ENOSPC; in ext2_format()
138 if (cfg->block_size == 1024) { in ext2_format()
158 return -ENOTSUP; in ext2_format()
161 uint32_t mem_for_inodes = fs_memory - occupied_blocks * cfg->block_size; in ext2_format()
166 inodes_count += inodes_per_block - (inodes_count % inodes_per_block); in ext2_format()
178 /* Two next block after inode table will be the blocks for '/' and 'lost+found' dirs */ in ext2_format()
185 uint32_t free_blocks = blocks_count - occupied_blocks; in ext2_format()
188 uint32_t used_blocks = occupied_blocks - first_data_block; in ext2_format()
205 ret = -ENOMEM; in ext2_format()
210 (struct ext2_disk_superblock *)((uint8_t *)sb_block->data + sb_offset); in ext2_format()
213 sb->s_inodes_count = sys_cpu_to_le32(inodes_count); in ext2_format()
214 sb->s_blocks_count = sys_cpu_to_le32(blocks_count); in ext2_format()
215 sb->s_r_blocks_count = sys_cpu_to_le32(0); in ext2_format()
216 sb->s_free_blocks_count = sys_cpu_to_le32(free_blocks); in ext2_format()
217 sb->s_free_inodes_count = sys_cpu_to_le32(inodes_count - used_inodes); in ext2_format()
218 sb->s_first_data_block = sys_cpu_to_le32(first_data_block); in ext2_format()
219 sb->s_log_block_size = sys_cpu_to_le32(block_log_size); in ext2_format()
220 sb->s_log_frag_size = sys_cpu_to_le32(block_log_size); in ext2_format()
221 sb->s_blocks_per_group = sys_cpu_to_le32(cfg->block_size * 8); in ext2_format()
222 sb->s_frags_per_group = sys_cpu_to_le32(cfg->block_size * 8); in ext2_format()
223 sb->s_inodes_per_group = sys_cpu_to_le32(inodes_count); in ext2_format()
224 sb->s_mtime = sys_cpu_to_le32(0); in ext2_format()
225 sb->s_wtime = sys_cpu_to_le32(0); in ext2_format()
226 sb->s_mnt_count = sys_cpu_to_le32(0); in ext2_format()
227 sb->s_max_mnt_count = sys_cpu_to_le32(-1); in ext2_format()
228 sb->s_magic = sys_cpu_to_le32(0xEF53); in ext2_format()
229 sb->s_state = sys_cpu_to_le32(EXT2_VALID_FS); in ext2_format()
230 sb->s_errors = sys_cpu_to_le32(EXT2_ERRORS_RO); in ext2_format()
231 sb->s_minor_rev_level = sys_cpu_to_le32(0); in ext2_format()
232 sb->s_lastcheck = sys_cpu_to_le32(0); in ext2_format()
233 sb->s_checkinterval = sys_cpu_to_le32(0); in ext2_format()
234 sb->s_creator_os = sys_cpu_to_le32(5); /* Unknown OS */ in ext2_format()
235 sb->s_rev_level = sys_cpu_to_le32(EXT2_DYNAMIC_REV); in ext2_format()
236 sb->s_def_resuid = sys_cpu_to_le32(0); in ext2_format()
237 sb->s_def_resgid = sys_cpu_to_le32(0); in ext2_format()
238 sb->s_first_ino = sys_cpu_to_le32(11); in ext2_format()
239 sb->s_inode_size = sys_cpu_to_le32(sizeof(struct ext2_disk_inode)); in ext2_format()
240 sb->s_block_group_nr = sys_cpu_to_le32(0); in ext2_format()
241 sb->s_feature_compat = sys_cpu_to_le32(0); in ext2_format()
242 sb->s_feature_incompat = sys_cpu_to_le32(EXT2_FEATURE_INCOMPAT_FILETYPE); in ext2_format()
243 sb->s_feature_ro_compat = sys_cpu_to_le32(0); in ext2_format()
244 sb->s_algo_bitmap = sys_cpu_to_le32(0); in ext2_format()
245 sb->s_prealloc_blocks = sys_cpu_to_le32(0); in ext2_format()
246 sb->s_prealloc_dir_blocks = sys_cpu_to_le32(0); in ext2_format()
247 sb->s_journal_inum = sys_cpu_to_le32(0); in ext2_format()
248 sb->s_journal_dev = sys_cpu_to_le32(0); in ext2_format()
249 sb->s_last_orphan = sys_cpu_to_le32(0); in ext2_format()
251 memcpy(sb->s_uuid, cfg->uuid, 16); in ext2_format()
252 strcpy(sb->s_volume_name, cfg->volume_name); in ext2_format()
255 ret = -EIO; in ext2_format()
261 struct ext2_disk_bgroup *bg = (struct ext2_disk_bgroup *)bg_block->data; in ext2_format()
263 memset(bg, 0, cfg->block_size); in ext2_format()
264 bg->bg_block_bitmap = sys_cpu_to_le32(bbitmap_block_num); in ext2_format()
265 bg->bg_inode_bitmap = sys_cpu_to_le32(ibitmap_block_num); in ext2_format()
266 bg->bg_inode_table = sys_cpu_to_le32(itable_block_num); in ext2_format()
267 bg->bg_free_blocks_count = sys_cpu_to_le16(free_blocks); in ext2_format()
268 bg->bg_free_inodes_count = sys_cpu_to_le16(inodes_count - used_inodes); in ext2_format()
269 bg->bg_used_dirs_count = sys_cpu_to_le16(2); /* '/' and 'lost+found' */ in ext2_format()
272 ret = -EIO; in ext2_format()
277 uint8_t *bbitmap = bbitmap_block->data; in ext2_format()
280 set_bitmap_padding(bbitmap, blocks_count - sb->s_first_data_block, cfg); in ext2_format()
283 ret = -EIO; in ext2_format()
288 uint8_t *ibitmap = ibitmap_block->data; in ext2_format()
293 ret = -EIO; in ext2_format()
302 memset(blk->data, 0, cfg->block_size); in ext2_format()
306 ret = -EIO; in ext2_format()
316 in = (struct ext2_disk_inode *)itable_block1->data; in ext2_format()
317 inode_offset = EXT2_ROOT_INODE - 1; in ext2_format()
323 ret = -EIO; in ext2_format()
328 inode_offset = (lost_found_inode - 1) % inodes_per_block; /* We count inodes from 1 */ in ext2_format()
337 in = (struct ext2_disk_inode *)itable_block2->data; in ext2_format()
345 ret = -EIO; in ext2_format()
361 memset(root_dir_blk->data, 0, cfg->block_size); in ext2_format()
365 disk_de = EXT2_DISK_DIRENTRY_BY_OFFSET(root_dir_blk->data, de_offset); in ext2_format()
369 de_offset += de->de_rec_len; in ext2_format()
372 disk_de = EXT2_DISK_DIRENTRY_BY_OFFSET(root_dir_blk->data, de_offset); in ext2_format()
376 de_offset += de->de_rec_len; in ext2_format()
381 disk_de = EXT2_DISK_DIRENTRY_BY_OFFSET(root_dir_blk->data, de_offset); in ext2_format()
383 de_offset += de->de_rec_len; in ext2_format()
386 de->de_rec_len += cfg->block_size - de_offset; in ext2_format()
392 ret = -EIO; in ext2_format()
403 memset(lost_found_dir_blk->data, 0, cfg->block_size); in ext2_format()
407 disk_de = EXT2_DISK_DIRENTRY_BY_OFFSET(lost_found_dir_blk->data, de_offset); in ext2_format()
411 de_offset += de->de_rec_len; in ext2_format()
414 disk_de = EXT2_DISK_DIRENTRY_BY_OFFSET(lost_found_dir_blk->data, de_offset); in ext2_format()
416 de_offset += de->de_rec_len; in ext2_format()
419 de->de_rec_len += cfg->block_size - de_offset; in ext2_format()
425 ret = -EIO; in ext2_format()
437 if ((ret >= 0) && (fs->backend_ops->sync(fs)) < 0) { in ext2_format()
438 ret = -EIO; in ext2_format()