2 * Copyright (c) 2013 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
34 * @brief Ext4 high level operations (file, directory, mountpoints...)
37 #include "ext4_config.h"
38 #include "ext4_blockdev.h"
39 #include "ext4_types.h"
40 #include "ext4_debug.h"
41 #include "ext4_errno.h"
44 #include "ext4_inode.h"
45 #include "ext4_super.h"
46 #include "ext4_dir_idx.h"
52 /**@brief Mount point OS dependent lock*/
53 #define EXT4_MP_LOCK(_m) \
56 (_m)->os_locks->lock(); \
59 /**@brief Mount point OS dependent unlock*/
60 #define EXT4_MP_UNLOCK(_m) \
63 (_m)->os_locks->unlock(); \
66 /**@brief Mount point descriptor.*/
67 struct ext4_mountpoint {
69 /**@brief Mount done flag.*/
72 /**@brief Mount point name (@ref ext4_mount)*/
75 /**@brief OS dependent lock/unlock functions.*/
76 const struct ext4_lock *os_locks;
78 /**@brief Ext4 filesystem internals.*/
81 /**@brief Dynamic allocation cache flag.*/
85 /**@brief Block devices descriptor.*/
86 struct _ext4_devices {
88 /**@brief Block device name (@ref ext4_device_register)*/
91 /**@brief Block device handle.*/
92 struct ext4_blockdev *bd;
94 /**@brief Block cache handle.*/
95 struct ext4_bcache *bc;
98 /**@brief Block devices.*/
99 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
101 /**@brief Mountpoints.*/
102 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
104 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
105 const char *dev_name)
108 ext4_assert(bd && dev_name);
110 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
111 if (!_bdevices[i].bd) {
112 strcpy(_bdevices[i].name, dev_name);
113 _bdevices[i].bd = bd;
114 _bdevices[i].bc = bc;
118 if (!strcmp(_bdevices[i].name, dev_name))
124 /****************************************************************************/
126 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
128 if ((name_size == 1) && (name[0] == '.'))
131 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
137 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
139 struct ext4_fs *fs = enode->fs;
141 /* Check if node is directory */
142 if (!ext4_inode_is_type(&fs->sb, enode->inode,
143 EXT4_INODE_MODE_DIRECTORY)) {
144 *has_children = false;
148 struct ext4_directory_iterator it;
149 int rc = ext4_dir_iterator_init(&it, enode, 0);
153 /* Find a non-empty directory entry */
155 while (it.current != NULL) {
156 if (ext4_dir_entry_ll_get_inode(it.current) != 0) {
157 uint16_t name_size = ext4_dir_entry_ll_get_name_length(
158 &fs->sb, it.current);
159 if (!ext4_is_dots(it.current->name, name_size)) {
165 rc = ext4_dir_iterator_next(&it);
167 ext4_dir_iterator_fini(&it);
172 rc = ext4_dir_iterator_fini(&it);
176 *has_children = found;
181 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
182 struct ext4_inode_ref *child, const char *name,
185 /* Check maximum name length */
186 if (name_len > EXT4_DIRECTORY_FILENAME_LEN)
189 /* Add entry to parent directory */
190 int rc = ext4_dir_add_entry(parent, name, name_len, child);
194 /* Fill new dir -> add '.' and '..' entries.
195 * Also newly allocated inode should have 0 link count.
197 if (ext4_inode_is_type(&mp->fs.sb, child->inode,
198 EXT4_INODE_MODE_DIRECTORY) &&
199 ext4_inode_get_links_count(child->inode) == 0) {
200 rc = ext4_dir_add_entry(child, ".", strlen("."), child);
202 ext4_dir_remove_entry(parent, name, strlen(name));
206 rc = ext4_dir_add_entry(child, "..", strlen(".."), parent);
208 ext4_dir_remove_entry(parent, name, strlen(name));
209 ext4_dir_remove_entry(child, ".", strlen("."));
213 /*New empty directory. Two links (. and ..) */
214 ext4_inode_set_links_count(child->inode, 2);
216 #if CONFIG_DIR_INDEX_ENABLE
217 /* Initialize directory index if supported */
218 if (ext4_sb_has_feature_compatible(
219 &mp->fs.sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
220 rc = ext4_dir_dx_init(child);
224 ext4_inode_set_flag(child->inode,
225 EXT4_INODE_FLAG_INDEX);
230 ext4_fs_inode_links_count_inc(parent);
232 parent->dirty = true;
234 if (ext4_inode_is_type(&mp->fs.sb, child->inode,
235 EXT4_INODE_MODE_DIRECTORY)) {
237 ext4_inode_has_flag(child->inode,
238 EXT4_INODE_FLAG_INDEX);
239 struct ext4_directory_search_result result;
240 if (!has_flag_index) {
241 rc = ext4_dir_find_entry(&result,
247 ext4_dir_entry_ll_set_inode(result.dentry,
249 result.block.dirty = true;
250 rc = ext4_dir_destroy_result(child, &result);
255 #if CONFIG_DIR_INDEX_ENABLE
256 rc = ext4_dir_dx_reset_parent_inode(child,
264 ext4_fs_inode_links_count_inc(parent);
265 parent->dirty = true;
267 ext4_fs_inode_links_count_inc(child);
275 static int ext4_unlink(struct ext4_mountpoint *mp,
276 struct ext4_inode_ref *parent,
277 struct ext4_inode_ref *child_inode_ref, const char *name,
281 int rc = ext4_has_children(&has_children, child_inode_ref);
285 /* Cannot unlink non-empty node */
289 /* Remove entry from parent directory */
290 rc = ext4_dir_remove_entry(parent, name, name_len);
294 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
295 EXT4_INODE_MODE_DIRECTORY);
297 /* If directory - handle links from parent */
299 // ext4_assert(ext4_inode_get_links_count(child_inode_ref->inode)
301 ext4_fs_inode_links_count_dec(parent);
302 parent->dirty = true;
306 * TODO: Update timestamps of the parent
307 * (when we have wall-clock time).
309 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
310 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
311 * parent->dirty = true;
315 * TODO: Update timestamp for inode.
317 * ext4_inode_set_change_inode_time(child_inode_ref->inode,
320 if (ext4_inode_get_links_count(child_inode_ref->inode)) {
321 ext4_fs_inode_links_count_dec(child_inode_ref);
322 child_inode_ref->dirty = true;
328 /****************************************************************************/
330 int ext4_mount(const char *dev_name, const char *mount_point)
332 ext4_assert(mount_point && dev_name);
337 struct ext4_blockdev *bd = 0;
338 struct ext4_bcache *bc = 0;
339 struct ext4_mountpoint *mp = 0;
341 if (mount_point[strlen(mount_point) - 1] != '/')
344 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
345 if (_bdevices[i].name) {
346 if (!strcmp(dev_name, _bdevices[i].name)) {
347 bd = _bdevices[i].bd;
348 bc = _bdevices[i].bc;
357 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
358 if (!_mp[i].mounted) {
359 strcpy(_mp[i].name, mount_point);
365 if (!strcmp(_mp[i].name, mount_point))
372 r = ext4_block_init(bd);
376 r = ext4_fs_init(&mp->fs, bd);
382 bsize = ext4_sb_get_block_size(&mp->fs.sb);
383 ext4_block_set_lb_size(bd, bsize);
385 mp->cache_dynamic = 0;
388 /*Automatic block cache alloc.*/
389 mp->cache_dynamic = 1;
390 bc = malloc(sizeof(struct ext4_bcache));
392 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
401 if (bsize != bc->itemsize)
404 /*Bind block cache to block device*/
405 r = ext4_block_bind_bcache(bd, bc);
408 if (mp->cache_dynamic) {
409 ext4_bcache_fini_dynamic(bc);
418 int ext4_umount(const char *mount_point)
422 struct ext4_mountpoint *mp = 0;
424 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
425 if (!strcmp(_mp[i].name, mount_point)) {
434 r = ext4_fs_fini(&mp->fs);
440 if (mp->cache_dynamic) {
441 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
442 free(mp->fs.bdev->bc);
445 return ext4_block_fini(mp->fs.bdev);
448 int ext4_mount_point_stats(const char *mount_point,
449 struct ext4_mount_stats *stats)
452 struct ext4_mountpoint *mp = 0;
454 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
455 if (!strcmp(_mp[i].name, mount_point)) {
464 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
465 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
466 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
467 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
468 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
470 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
471 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
472 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
474 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
480 int ext4_mount_setup_locks(const char *mount_point,
481 const struct ext4_lock *locks)
484 struct ext4_mountpoint *mp = 0;
486 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
487 if (!strcmp(_mp[i].name, mount_point)) {
495 mp->os_locks = locks;
499 /********************************FILE OPERATIONS*****************************/
501 static struct ext4_mountpoint *ext4_get_mount(const char *path)
504 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
509 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
515 static int ext4_path_check(const char *path, bool *is_goal)
519 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
521 if (path[i] == '/') {
535 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
540 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
541 *file_flags = O_RDONLY;
545 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
546 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
550 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
551 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
555 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
556 !strcmp(flags, "r+b")) {
557 *file_flags = O_RDWR;
561 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
562 !strcmp(flags, "w+b")) {
563 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
567 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
568 !strcmp(flags, "a+b")) {
569 *file_flags = O_RDWR | O_CREAT | O_APPEND;
577 * NOTICE: if filetype is equal to EXT4_DIRECTORY_FILETYPE_UNKNOWN,
578 * any filetype of the target dir entry will be accepted.
580 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
581 int filetype, uint32_t *parent_inode,
584 bool is_goal = false;
589 struct ext4_mountpoint *mp = ext4_get_mount(path);
590 struct ext4_directory_search_result result;
591 struct ext4_inode_ref ref;
601 path += strlen(mp->name);
604 *name_off = strlen(mp->name);
607 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
613 *parent_inode = ref.index;
615 int len = ext4_path_check(path, &is_goal);
619 len = ext4_path_check(path, &is_goal);
622 /*If root open was request.*/
624 ((filetype == EXT4_DIRECTORY_FILETYPE_DIR) ||
625 (filetype == EXT4_DIRECTORY_FILETYPE_UNKNOWN)))
632 r = ext4_dir_find_entry(&result, &ref, path, len);
638 if (!(f->flags & O_CREAT))
641 /*O_CREAT allows create new entry*/
642 struct ext4_inode_ref child_ref;
643 r = ext4_fs_alloc_inode(
646 : EXT4_DIRECTORY_FILETYPE_DIR);
650 /*Destroy last result*/
651 ext4_dir_destroy_result(&ref, &result);
653 /*Link with root dir.*/
654 r = ext4_link(mp, &ref, &child_ref, path, len);
656 /*Fail. Free new inode.*/
657 ext4_fs_free_inode(&child_ref);
658 /*We do not want to write new inode.
659 But block has to be released.*/
660 child_ref.dirty = false;
661 ext4_fs_put_inode_ref(&child_ref);
665 ext4_fs_put_inode_ref(&child_ref);
671 *parent_inode = ref.index;
673 next_inode = ext4_dir_entry_ll_get_inode(result.dentry);
675 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
677 r = ext4_dir_destroy_result(&ref, &result);
681 /*If expected file error*/
682 if (inode_type != EXT4_DIRECTORY_FILETYPE_DIR && !is_goal) {
686 if (filetype != EXT4_DIRECTORY_FILETYPE_UNKNOWN) {
687 if ((inode_type != filetype) && is_goal) {
693 r = ext4_fs_put_inode_ref(&ref);
697 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
707 *name_off += len + 1;
711 ext4_fs_put_inode_ref(&ref);
717 if ((f->flags & O_TRUNC) &&
718 (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE)) {
720 r = ext4_fs_truncate_inode(&ref, 0);
722 ext4_fs_put_inode_ref(&ref);
728 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
729 f->inode = ref.index;
732 if (f->flags & O_APPEND)
736 r = ext4_fs_put_inode_ref(&ref);
740 /****************************************************************************/
742 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
743 bool file_expect, uint32_t *parent_inode,
748 if (ext4_parse_flags(flags, &iflags) == false)
751 if (file_expect == true)
752 filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
754 filetype = EXT4_DIRECTORY_FILETYPE_DIR;
756 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
760 static int __ext4_create_hardlink(const char *path,
761 struct ext4_inode_ref *child_ref)
763 bool is_goal = false;
764 uint8_t inode_type = EXT4_DIRECTORY_FILETYPE_DIR;
768 struct ext4_mountpoint *mp = ext4_get_mount(path);
769 struct ext4_directory_search_result result;
770 struct ext4_inode_ref ref;
776 path += strlen(mp->name);
779 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
784 int len = ext4_path_check(path, &is_goal);
788 len = ext4_path_check(path, &is_goal);
791 /*If root open was request.*/
799 r = ext4_dir_find_entry(&result, &ref, path, len);
802 if (r != ENOENT || !is_goal)
805 /*Destroy last result*/
806 ext4_dir_destroy_result(&ref, &result);
808 /*Link with root dir.*/
809 r = ext4_link(mp, &ref, child_ref, path, len);
813 next_inode = result.dentry->inode;
815 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
817 r = ext4_dir_destroy_result(&ref, &result);
821 if (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE) {
830 r = ext4_fs_put_inode_ref(&ref);
834 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
845 ext4_fs_put_inode_ref(&ref);
849 r = ext4_fs_put_inode_ref(&ref);
853 static int __ext4_remove_hardlink(const char *path,
855 struct ext4_inode_ref *parent_ref,
856 struct ext4_inode_ref *child_ref)
861 struct ext4_mountpoint *mp = ext4_get_mount(path);
869 len = ext4_path_check(path, &is_goal);
871 /*Unlink from parent*/
872 r = ext4_unlink(mp, parent_ref, child_ref, path, len);
880 int ext4_flink(const char *path, const char *hardlink_path)
885 bool child_loaded = false;
886 uint32_t parent_inode, child_inode;
887 struct ext4_mountpoint *mp = ext4_get_mount(path);
888 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
889 struct ext4_inode_ref child_ref;
894 /* Will that happen? Anyway return EINVAL for such case. */
900 r = ext4_generic_open2(&f, path, O_RDONLY,
901 EXT4_DIRECTORY_FILETYPE_UNKNOWN,
902 &parent_inode, &name_off);
906 child_inode = f.inode;
909 /*We have file to unlink. Load it.*/
910 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
916 r = __ext4_create_hardlink(hardlink_path, &child_ref);
920 ext4_fs_put_inode_ref(&child_ref);
927 int ext4_frename(const char *path, const char *new_path)
932 bool parent_loaded = false, child_loaded = false;
933 uint32_t parent_inode, child_inode;
934 struct ext4_mountpoint *mp = ext4_get_mount(path);
935 struct ext4_inode_ref child_ref, parent_ref;
942 r = ext4_generic_open2(&f, path, O_RDONLY,
943 EXT4_DIRECTORY_FILETYPE_UNKNOWN,
944 &parent_inode, &name_off);
948 child_inode = f.inode;
952 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
956 parent_loaded = true;
958 /*We have file to unlink. Load it.*/
959 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
965 r = __ext4_create_hardlink(new_path, &child_ref);
969 r = __ext4_remove_hardlink(path, name_off, &parent_ref, &child_ref);
975 ext4_fs_put_inode_ref(&parent_ref);
978 ext4_fs_put_inode_ref(&child_ref);
985 /****************************************************************************/
987 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
989 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
998 int ext4_cache_write_back(const char *path, bool on)
1000 struct ext4_mountpoint *mp = ext4_get_mount(path);
1006 ext4_block_cache_write_back(mp->fs.bdev, on);
1011 int ext4_fremove(const char *path)
1014 uint32_t parent_inode;
1019 struct ext4_inode_ref child;
1020 struct ext4_inode_ref parent;
1021 struct ext4_mountpoint *mp = ext4_get_mount(path);
1027 r = ext4_generic_open2(&f, path, O_RDWR,
1028 EXT4_DIRECTORY_FILETYPE_UNKNOWN,
1029 &parent_inode, &name_off);
1036 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1042 /*We have file to delete. Load it.*/
1043 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1045 ext4_fs_put_inode_ref(&parent);
1053 len = ext4_path_check(path, &is_goal);
1055 /*Unlink from parent*/
1056 r = ext4_unlink(mp, &parent, &child, path, len);
1060 /*Link count is zero, the inode should be freed. */
1061 if (!ext4_inode_get_links_count(child.inode)) {
1062 ext4_inode_set_deletion_time(child.inode, 0xFFFFFFFF);
1064 ext4_block_cache_write_back(mp->fs.bdev, 1);
1065 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1066 r = ext4_fs_truncate_inode(&child, 0);
1067 ext4_block_cache_write_back(mp->fs.bdev, 0);
1072 r = ext4_fs_free_inode(&child);
1078 ext4_fs_put_inode_ref(&child);
1079 ext4_fs_put_inode_ref(&parent);
1084 int ext4_fill_raw_inode(const char *path,
1086 struct ext4_inode *inode)
1090 struct ext4_inode_ref inode_ref;
1091 struct ext4_mountpoint *mp = ext4_get_mount(path);
1099 r = ext4_generic_open2(&f, path, O_RDONLY,
1100 EXT4_DIRECTORY_FILETYPE_UNKNOWN,
1111 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1117 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1119 ext4_fs_put_inode_ref(&inode_ref);
1128 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1130 struct ext4_mountpoint *mp = ext4_get_mount(path);
1137 ext4_block_cache_write_back(mp->fs.bdev, 1);
1138 r = ext4_generic_open(f, path, flags, true, 0, 0);
1139 ext4_block_cache_write_back(mp->fs.bdev, 0);
1144 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1146 struct ext4_mountpoint *mp = ext4_get_mount(path);
1153 filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
1156 ext4_block_cache_write_back(mp->fs.bdev, 1);
1157 r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
1158 ext4_block_cache_write_back(mp->fs.bdev, 0);
1163 int ext4_fclose(ext4_file *f)
1165 ext4_assert(f && f->mp);
1170 f->fpos = f->fsize = 0;
1175 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1177 struct ext4_inode_ref ref;
1181 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1183 EXT4_MP_UNLOCK(f->mp);
1188 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1189 if (f->fsize <= size) {
1194 /*Start write back cache mode.*/
1195 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1199 r = ext4_fs_truncate_inode(&ref, size);
1207 /*Stop write back cache mode*/
1208 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1214 ext4_fs_put_inode_ref(&ref);
1219 int ext4_ftruncate(ext4_file *f, uint64_t size)
1222 ext4_assert(f && f->mp);
1224 if (f->flags & O_RDONLY)
1227 EXT4_MP_LOCK(f->mp);
1229 r = ext4_ftruncate_no_lock(f, size);
1231 EXT4_MP_UNLOCK(f->mp);
1235 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1239 uint32_t fblock_start;
1240 uint32_t fblock_cnt;
1242 uint32_t sblock_end;
1243 uint32_t block_size;
1244 uint8_t *u8_buf = buf;
1246 struct ext4_block b;
1247 struct ext4_inode_ref ref;
1249 ext4_assert(f && f->mp);
1251 if (f->flags & O_WRONLY)
1257 EXT4_MP_LOCK(f->mp);
1262 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1264 EXT4_MP_UNLOCK(f->mp);
1269 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1271 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1272 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1273 sblock = (f->fpos) / block_size;
1274 sblock_end = (f->fpos + size) / block_size;
1275 u = (f->fpos) % block_size;
1277 /*If the size of symlink is smaller than 60 bytes*/
1278 if (ext4_inode_is_type(&f->mp->fs.sb, ref.inode, EXT4_INODE_MODE_SOFTLINK)
1279 && f->fsize < sizeof(ref.inode->blocks)
1280 && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
1281 char *content = (char *)ref.inode->blocks;
1282 if (f->fpos < f->fsize) {
1283 r = (u + size > f->fsize)
1286 memcpy(buf, content + u, r);
1303 uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
1305 r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
1309 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1313 memcpy(u8_buf, b.data + u, ll);
1315 r = ext4_block_set(f->mp->fs.bdev, &b);
1331 while (size >= block_size) {
1332 while (sblock < sblock_end) {
1333 r = ext4_fs_get_inode_data_block_index(&ref, sblock,
1340 if (!fblock_start) {
1341 fblock_start = fblock;
1344 if ((fblock_start + fblock_cnt) != fblock)
1350 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1355 size -= block_size * fblock_cnt;
1356 u8_buf += block_size * fblock_cnt;
1357 f->fpos += block_size * fblock_cnt;
1360 *rcnt += block_size * fblock_cnt;
1362 fblock_start = fblock;
1367 r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
1371 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1375 memcpy(u8_buf, b.data, size);
1377 r = ext4_block_set(f->mp->fs.bdev, &b);
1388 ext4_fs_put_inode_ref(&ref);
1389 EXT4_MP_UNLOCK(f->mp);
1393 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1399 uint32_t sblock_end;
1400 uint32_t file_blocks;
1401 uint32_t block_size;
1402 uint32_t fblock_start;
1403 uint32_t fblock_cnt;
1405 struct ext4_block b;
1406 struct ext4_inode_ref ref;
1407 const uint8_t *u8_buf = buf;
1410 ext4_assert(f && f->mp);
1412 if (f->flags & O_RDONLY)
1418 EXT4_MP_LOCK(f->mp);
1423 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1425 EXT4_MP_UNLOCK(f->mp);
1430 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1432 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1434 sblock_end = (f->fpos + size) > f->fsize ? (f->fpos + size) : f->fsize;
1435 sblock_end /= block_size;
1436 file_blocks = (f->fsize / block_size);
1438 if (f->fsize % block_size)
1441 sblock = (f->fpos) / block_size;
1443 u = (f->fpos) % block_size;
1446 uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
1448 r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
1452 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1456 memcpy(b.data + u, u8_buf, ll);
1459 r = ext4_block_set(f->mp->fs.bdev, &b);
1473 /*Start write back cache mode.*/
1474 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1480 while (size >= block_size) {
1482 while (sblock < sblock_end) {
1483 if (sblock < file_blocks) {
1484 r = ext4_fs_get_inode_data_block_index(
1485 &ref, sblock, &fblock);
1489 r = ext4_fs_append_inode_block(&ref, &fblock,
1497 if (!fblock_start) {
1498 fblock_start = fblock;
1501 if ((fblock_start + fblock_cnt) != fblock)
1507 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1512 size -= block_size * fblock_cnt;
1513 u8_buf += block_size * fblock_cnt;
1514 f->fpos += block_size * fblock_cnt;
1517 *wcnt += block_size * fblock_cnt;
1519 fblock_start = fblock;
1523 /*Stop write back cache mode*/
1524 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1530 if (sblock < file_blocks) {
1531 r = ext4_fs_get_inode_data_block_index(&ref, sblock,
1536 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1541 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1545 memcpy(b.data, u8_buf, size);
1548 r = ext4_block_set(f->mp->fs.bdev, &b);
1558 if (f->fpos > f->fsize) {
1560 ext4_inode_set_size(ref.inode, f->fsize);
1565 ext4_fs_put_inode_ref(&ref);
1566 EXT4_MP_UNLOCK(f->mp);
1570 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1574 if (offset > f->fsize)
1580 if ((offset + f->fpos) > f->fsize)
1586 if (offset > f->fsize)
1589 f->fpos = f->fsize - offset;
1595 uint64_t ext4_ftell(ext4_file *f) { return f->fpos; }
1597 uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
1599 int ext4_chmod(const char *path, uint32_t mode)
1604 struct ext4_sblock *sb;
1605 struct ext4_inode_ref inode_ref;
1606 struct ext4_mountpoint *mp = ext4_get_mount(path);
1613 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
1621 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1627 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1628 inode_ref.dirty = true;
1630 ext4_fs_put_inode_ref(&inode_ref);
1635 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1640 struct ext4_inode_ref inode_ref;
1641 struct ext4_mountpoint *mp = ext4_get_mount(path);
1648 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
1655 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1661 ext4_inode_set_uid(inode_ref.inode, uid);
1662 ext4_inode_set_gid(inode_ref.inode, gid);
1663 inode_ref.dirty = true;
1665 ext4_fs_put_inode_ref(&inode_ref);
1670 int ext4_file_set_atime(const char *path, uint32_t atime)
1675 struct ext4_inode_ref inode_ref;
1676 struct ext4_mountpoint *mp = ext4_get_mount(path);
1683 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
1690 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1696 ext4_inode_set_access_time(inode_ref.inode, atime);
1697 inode_ref.dirty = true;
1699 ext4_fs_put_inode_ref(&inode_ref);
1704 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1709 struct ext4_inode_ref inode_ref;
1710 struct ext4_mountpoint *mp = ext4_get_mount(path);
1717 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
1724 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1730 ext4_inode_set_modification_time(inode_ref.inode, mtime);
1731 inode_ref.dirty = true;
1733 ext4_fs_put_inode_ref(&inode_ref);
1738 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1743 struct ext4_inode_ref inode_ref;
1744 struct ext4_mountpoint *mp = ext4_get_mount(path);
1751 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
1758 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1764 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1765 inode_ref.dirty = true;
1767 ext4_fs_put_inode_ref(&inode_ref);
1772 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1774 struct ext4_block b;
1775 struct ext4_inode_ref ref;
1776 uint32_t sblock, fblock;
1777 uint32_t block_size;
1780 ext4_assert(f && f->mp);
1785 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1787 EXT4_MP_UNLOCK(f->mp);
1792 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1793 if (size > block_size) {
1797 r = ext4_ftruncate_no_lock(f, 0);
1801 /*Start write back cache mode.*/
1802 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1806 /*If the size of symlink is smaller than 60 bytes*/
1807 if (size < sizeof(ref.inode->blocks)) {
1808 char *content = (char *)ref.inode->blocks;
1809 memset(content, 0, sizeof(ref.inode->blocks));
1810 memcpy(content, buf, size);
1811 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1813 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1814 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1818 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1822 memcpy(b.data, buf, size);
1824 r = ext4_block_set(f->mp->fs.bdev, &b);
1829 /*Stop write back cache mode*/
1830 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1835 ext4_inode_set_size(ref.inode, size);
1843 ext4_fs_put_inode_ref(&ref);
1847 int ext4_fsymlink(const char *target, const char *path)
1849 struct ext4_mountpoint *mp = ext4_get_mount(path);
1857 filetype = EXT4_DIRECTORY_FILETYPE_SYMLINK;
1860 ext4_block_cache_write_back(mp->fs.bdev, 1);
1861 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
1863 r = ext4_fsymlink_set(&f, target, strlen(target));
1870 ext4_block_cache_write_back(mp->fs.bdev, 0);
1875 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1877 struct ext4_mountpoint *mp = ext4_get_mount(path);
1888 memset(buf, 0, sizeof(bufsize));
1890 filetype = EXT4_DIRECTORY_FILETYPE_SYMLINK;
1893 ext4_block_cache_write_back(mp->fs.bdev, 1);
1894 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, 0, 0);
1896 r = ext4_fread(&f, buf, bufsize, rcnt);
1903 ext4_block_cache_write_back(mp->fs.bdev, 0);
1908 /*********************************DIRECTORY OPERATION************************/
1910 int ext4_dir_rm(const char *path)
1916 struct ext4_mountpoint *mp = ext4_get_mount(path);
1917 struct ext4_inode_ref current;
1918 struct ext4_inode_ref child;
1919 struct ext4_directory_iterator it;
1923 uint32_t inode_current;
1936 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
1943 len = ext4_path_check(path, &is_goal);
1945 inode_current = f.inode;
1948 ext4_block_cache_write_back(mp->fs.bdev, 1);
1951 /*Load directory node.*/
1952 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, ¤t);
1957 /*Initialize iterator.*/
1958 r = ext4_dir_iterator_init(&it, ¤t, 0);
1960 ext4_fs_put_inode_ref(¤t);
1971 /*Get up directory inode when ".." entry*/
1972 if ((it.current->name_length == 2) &&
1973 ext4_is_dots(it.current->name,
1974 it.current->name_length)) {
1975 inode_up = ext4_dir_entry_ll_get_inode(it.current);
1978 /*If directory or file entry, but not "." ".." entry*/
1979 if (!ext4_is_dots(it.current->name,
1980 it.current->name_length)) {
1982 /*Get child inode reference do unlink
1984 r = ext4_fs_get_inode_ref(&f.mp->fs,
1985 ext4_dir_entry_ll_get_inode(it.current),
1990 /*If directory with no leaf children*/
1991 r = ext4_has_children(&has_children, &child);
1993 ext4_fs_put_inode_ref(&child);
1998 /*Has directory children. Go into this
2000 inode_up = inode_current;
2001 inode_current = ext4_dir_entry_ll_get_inode(it.current);
2003 ext4_fs_put_inode_ref(&child);
2007 /*No children in child directory or file. Just
2009 r = ext4_unlink(f.mp, ¤t, &child,
2010 (char *)it.current->name,
2011 it.current->name_length);
2013 ext4_fs_put_inode_ref(&child);
2017 ext4_inode_set_deletion_time(child.inode,
2019 ext4_inode_set_links_count(child.inode, 0);
2022 r = ext4_fs_truncate_inode(&child, 0);
2024 ext4_fs_put_inode_ref(&child);
2028 r = ext4_fs_free_inode(&child);
2030 ext4_fs_put_inode_ref(&child);
2034 r = ext4_fs_put_inode_ref(&child);
2039 r = ext4_dir_iterator_next(&it);
2043 /*Directory iterator reached last entry*/
2044 ext4_has_children(&has_children, ¤t);
2045 if (!has_children) {
2046 inode_current = inode_up;
2053 struct ext4_inode_ref parent;
2054 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2059 /* In this place all directories should be
2061 * Last unlink from root of current directory*/
2062 r = ext4_unlink(f.mp, &parent, ¤t,
2065 ext4_fs_put_inode_ref(&parent);
2069 if (ext4_inode_get_links_count(current.inode) ==
2071 ext4_inode_set_deletion_time(
2072 current.inode, 0xFFFFFFFF);
2073 ext4_inode_set_links_count(
2075 current.dirty = true;
2077 r = ext4_fs_truncate_inode(¤t, 0);
2079 ext4_fs_put_inode_ref(&parent);
2083 r = ext4_fs_free_inode(¤t);
2085 ext4_fs_put_inode_ref(&parent);
2090 r = ext4_fs_put_inode_ref(&parent);
2097 ext4_dir_iterator_fini(&it);
2098 ext4_fs_put_inode_ref(¤t);
2101 /*When something goes wrong. End loop.*/
2107 ext4_block_cache_write_back(mp->fs.bdev, 0);
2112 int ext4_dir_mk(const char *path)
2117 struct ext4_mountpoint *mp = ext4_get_mount(path);
2125 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2127 /*Directory already created*/
2133 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2143 int ext4_dir_open(ext4_dir *d, const char *path)
2145 struct ext4_mountpoint *mp = ext4_get_mount(path);
2152 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2158 int ext4_dir_close(ext4_dir *d) { return ext4_fclose(&d->f); }
2160 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2162 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2165 ext4_direntry *de = 0;
2166 struct ext4_inode_ref dir;
2167 struct ext4_directory_iterator it;
2169 EXT4_MP_LOCK(d->f.mp);
2171 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2172 EXT4_MP_UNLOCK(d->f.mp);
2176 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2181 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2183 ext4_fs_put_inode_ref(&dir);
2187 memcpy(&d->de, it.current, sizeof(ext4_direntry));
2190 ext4_dir_iterator_next(&it);
2193 it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
2195 ext4_dir_iterator_fini(&it);
2196 ext4_fs_put_inode_ref(&dir);
2199 EXT4_MP_UNLOCK(d->f.mp);