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"
39 #include "ext4_blockdev.h"
40 #include "ext4_types.h"
41 #include "ext4_debug.h"
42 #include "ext4_errno.h"
45 #include "ext4_inode.h"
46 #include "ext4_super.h"
47 #include "ext4_dir_idx.h"
48 #include "ext4_xattr.h"
54 /**@brief Mount point OS dependent lock*/
55 #define EXT4_MP_LOCK(_m) \
58 (_m)->os_locks->lock(); \
61 /**@brief Mount point OS dependent unlock*/
62 #define EXT4_MP_UNLOCK(_m) \
65 (_m)->os_locks->unlock(); \
68 /**@brief Mount point descriptor.*/
69 struct ext4_mountpoint {
71 /**@brief Mount done flag.*/
74 /**@brief Mount point name (@ref ext4_mount)*/
77 /**@brief OS dependent lock/unlock functions.*/
78 const struct ext4_lock *os_locks;
80 /**@brief Ext4 filesystem internals.*/
83 /**@brief Dynamic allocation cache flag.*/
87 /**@brief Block devices descriptor.*/
88 struct _ext4_devices {
90 /**@brief Block device name (@ref ext4_device_register)*/
93 /**@brief Block device handle.*/
94 struct ext4_blockdev *bd;
96 /**@brief Block cache handle.*/
97 struct ext4_bcache *bc;
100 /**@brief Block devices.*/
101 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
103 /**@brief Mountpoints.*/
104 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
106 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
107 const char *dev_name)
110 ext4_assert(bd && dev_name);
112 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
113 if (!_bdevices[i].bd) {
114 strcpy(_bdevices[i].name, dev_name);
115 _bdevices[i].bd = bd;
116 _bdevices[i].bc = bc;
120 if (!strcmp(_bdevices[i].name, dev_name))
126 /****************************************************************************/
128 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
130 if ((name_size == 1) && (name[0] == '.'))
133 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
139 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
141 struct ext4_sblock *sb = &enode->fs->sb;
143 /* Check if node is directory */
144 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
145 *has_children = false;
149 struct ext4_dir_iter it;
150 int rc = ext4_dir_iterator_init(&it, enode, 0);
154 /* Find a non-empty directory entry */
156 while (it.curr != NULL) {
157 if (ext4_dir_en_get_inode(it.curr) != 0) {
159 nsize = ext4_dir_en_get_name_len(sb, it.curr);
160 if (!ext4_is_dots(it.curr->name, nsize)) {
166 rc = ext4_dir_iterator_next(&it);
168 ext4_dir_iterator_fini(&it);
173 rc = ext4_dir_iterator_fini(&it);
177 *has_children = found;
182 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
183 struct ext4_inode_ref *child, const char *nam,
184 uint32_t name_len, bool rename)
186 /* Check maximum name length */
187 if (name_len > EXT4_DIRECTORY_FILENAME_LEN)
190 /* Add entry to parent directory */
191 int r = ext4_dir_add_entry(parent, nam, name_len, child);
195 /* Fill new dir -> add '.' and '..' entries.
196 * Also newly allocated inode should have 0 link count.
199 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
200 EXT4_INODE_MODE_DIRECTORY);
201 if (is_dir && !rename) {
203 #if CONFIG_DIR_INDEX_ENABLE
204 /* Initialize directory index if supported */
205 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
206 r = ext4_dir_dx_init(child, parent);
210 ext4_inode_set_flag(child->inode, EXT4_INODE_FLAG_INDEX);
215 r = ext4_dir_add_entry(child, ".", strlen("."), child);
217 ext4_dir_remove_entry(parent, nam, strlen(nam));
221 r = ext4_dir_add_entry(child, "..", strlen(".."), parent);
223 ext4_dir_remove_entry(parent, nam, strlen(nam));
224 ext4_dir_remove_entry(child, ".", strlen("."));
229 /*New empty directory. Two links (. and ..) */
230 ext4_inode_set_links_cnt(child->inode, 2);
231 ext4_fs_inode_links_count_inc(parent);
233 parent->dirty = true;
237 * In case we want to rename a directory,
238 * we reset the original '..' pointer.
242 idx = ext4_inode_has_flag(child->inode, EXT4_INODE_FLAG_INDEX);
243 struct ext4_dir_search_result res;
245 r = ext4_dir_find_entry(&res, child, "..", strlen(".."));
249 ext4_dir_en_set_inode(res.dentry, parent->index);
250 res.block.dirty = true;
251 r = ext4_dir_destroy_result(child, &res);
256 #if CONFIG_DIR_INDEX_ENABLE
257 r = ext4_dir_dx_reset_parent_inode(child, parent->index);
264 ext4_fs_inode_links_count_inc(parent);
265 parent->dirty = true;
268 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_fs_inode_links_count_dec(parent);
300 parent->dirty = true;
304 * TODO: Update timestamps of the parent
305 * (when we have wall-clock time).
307 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
308 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
309 * parent->dirty = true;
313 * TODO: Update timestamp for inode.
315 * ext4_inode_set_change_inode_time(child_inode_ref->inode,
318 if (ext4_inode_get_links_cnt(child_inode_ref->inode)) {
319 ext4_fs_inode_links_count_dec(child_inode_ref);
320 child_inode_ref->dirty = true;
326 /****************************************************************************/
328 int ext4_mount(const char *dev_name, const char *mount_point)
330 ext4_assert(mount_point && dev_name);
335 struct ext4_blockdev *bd = 0;
336 struct ext4_bcache *bc = 0;
337 struct ext4_mountpoint *mp = 0;
339 if (mount_point[strlen(mount_point) - 1] != '/')
342 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
343 if (_bdevices[i].name) {
344 if (!strcmp(dev_name, _bdevices[i].name)) {
345 bd = _bdevices[i].bd;
346 bc = _bdevices[i].bc;
355 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
356 if (!_mp[i].mounted) {
357 strcpy(_mp[i].name, mount_point);
363 if (!strcmp(_mp[i].name, mount_point))
370 r = ext4_block_init(bd);
374 r = ext4_fs_init(&mp->fs, bd);
380 bsize = ext4_sb_get_block_size(&mp->fs.sb);
381 ext4_block_set_lb_size(bd, bsize);
383 mp->cache_dynamic = 0;
386 /*Automatic block cache alloc.*/
387 mp->cache_dynamic = 1;
388 bc = malloc(sizeof(struct ext4_bcache));
390 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
399 if (bsize != bc->itemsize)
402 /*Bind block cache to block device*/
403 r = ext4_block_bind_bcache(bd, bc);
406 if (mp->cache_dynamic) {
407 ext4_bcache_fini_dynamic(bc);
416 int ext4_umount(const char *mount_point)
420 struct ext4_mountpoint *mp = 0;
422 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
423 if (!strcmp(_mp[i].name, mount_point)) {
432 r = ext4_fs_fini(&mp->fs);
438 if (mp->cache_dynamic) {
439 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
440 free(mp->fs.bdev->bc);
443 return ext4_block_fini(mp->fs.bdev);
446 static struct ext4_mountpoint *ext4_get_mount(const char *path)
449 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
454 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
460 int ext4_mount_point_stats(const char *mount_point,
461 struct ext4_mount_stats *stats)
463 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
469 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
470 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
471 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
472 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
473 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
475 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
476 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
477 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
479 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
485 int ext4_mount_setup_locks(const char *mount_point,
486 const struct ext4_lock *locks)
489 struct ext4_mountpoint *mp = 0;
491 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
492 if (!strcmp(_mp[i].name, mount_point)) {
500 mp->os_locks = locks;
504 /********************************FILE OPERATIONS*****************************/
506 static int ext4_path_check(const char *path, bool *is_goal)
510 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
512 if (path[i] == '/') {
526 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
531 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
532 *file_flags = O_RDONLY;
536 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
537 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
541 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
542 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
546 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
547 !strcmp(flags, "r+b")) {
548 *file_flags = O_RDWR;
552 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
553 !strcmp(flags, "w+b")) {
554 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
558 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
559 !strcmp(flags, "a+b")) {
560 *file_flags = O_RDWR | O_CREAT | O_APPEND;
568 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
569 * any filetype of the target dir entry will be accepted.
571 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
572 int ftype, uint32_t *parent_inode,
575 bool is_goal = false;
576 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
581 struct ext4_mountpoint *mp = ext4_get_mount(path);
582 struct ext4_dir_search_result result;
583 struct ext4_inode_ref ref;
590 struct ext4_fs *const fs = &mp->fs;
591 struct ext4_sblock *const sb = &mp->fs.sb;
596 path += strlen(mp->name);
599 *name_off = strlen(mp->name);
602 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
607 *parent_inode = ref.index;
609 len = ext4_path_check(path, &is_goal);
612 len = ext4_path_check(path, &is_goal);
614 /*If root open was request.*/
616 ((ftype == EXT4_DE_DIR) ||
617 (ftype == EXT4_DE_UNKNOWN)))
624 r = ext4_dir_find_entry(&result, &ref, path, len);
627 /*Destroy last result*/
628 ext4_dir_destroy_result(&ref, &result);
632 if (!(f->flags & O_CREAT))
635 /*O_CREAT allows create new entry*/
636 struct ext4_inode_ref child_ref;
637 r = ext4_fs_alloc_inode(fs, &child_ref,
638 is_goal ? ftype : EXT4_DE_DIR);
643 /*Link with root dir.*/
644 r = ext4_link(mp, &ref, &child_ref, path, len, false);
646 /*Fail. Free new inode.*/
647 ext4_fs_free_inode(&child_ref);
648 /*We do not want to write new inode.
649 But block has to be released.*/
650 child_ref.dirty = false;
651 ext4_fs_put_inode_ref(&child_ref);
655 ext4_fs_put_inode_ref(&child_ref);
660 *parent_inode = ref.index;
662 next_inode = ext4_dir_en_get_inode(result.dentry);
663 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
665 t = ext4_dir_en_get_inode_type(sb, result.dentry);
666 imode = ext4_fs_correspond_inode_mode(t);
668 struct ext4_inode_ref child_ref;
669 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
673 imode = ext4_inode_type(sb, child_ref.inode);
674 ext4_fs_put_inode_ref(&child_ref);
677 r = ext4_dir_destroy_result(&ref, &result);
681 /*If expected file error*/
682 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
686 if (ftype != EXT4_DE_UNKNOWN) {
687 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
694 r = ext4_fs_put_inode_ref(&ref);
698 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
708 *name_off += len + 1;
712 ext4_fs_put_inode_ref(&ref);
718 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
719 r = ext4_fs_truncate_inode(&ref, 0);
721 ext4_fs_put_inode_ref(&ref);
727 f->fsize = ext4_inode_get_size(sb, ref.inode);
728 f->inode = ref.index;
731 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_DE_REG_FILE;
754 filetype = EXT4_DE_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, bool rename)
763 bool is_goal = false;
764 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
769 struct ext4_mountpoint *mp = ext4_get_mount(path);
770 struct ext4_dir_search_result result;
771 struct ext4_inode_ref ref;
776 struct ext4_fs *const fs = &mp->fs;
777 struct ext4_sblock *const sb = &mp->fs.sb;
780 path += strlen(mp->name);
783 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
787 len = ext4_path_check(path, &is_goal);
790 len = ext4_path_check(path, &is_goal);
792 /*If root open was request.*/
793 r = is_goal ? EINVAL : ENOENT;
797 r = ext4_dir_find_entry(&result, &ref, path, len);
800 /*Destroy last result*/
801 ext4_dir_destroy_result(&ref, &result);
803 if (r != ENOENT || !is_goal)
806 /*Link with root dir.*/
807 r = ext4_link(mp, &ref, child_ref, path, len, rename);
809 } else if (r == EOK && is_goal) {
810 /*Destroy last result*/
811 ext4_dir_destroy_result(&ref, &result);
816 next_inode = result.dentry->inode;
817 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
819 t = ext4_dir_en_get_inode_type(sb, result.dentry);
820 inode_mode = ext4_fs_correspond_inode_mode(t);
822 struct ext4_inode_ref child_ref;
823 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
827 inode_mode = ext4_inode_type(sb, child_ref.inode);
828 ext4_fs_put_inode_ref(&child_ref);
831 r = ext4_dir_destroy_result(&ref, &result);
835 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
836 r = is_goal ? EEXIST : ENOENT;
840 r = ext4_fs_put_inode_ref(&ref);
844 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
855 ext4_fs_put_inode_ref(&ref);
859 r = ext4_fs_put_inode_ref(&ref);
863 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
864 struct ext4_inode_ref *parent_ref,
865 struct ext4_inode_ref *child_ref)
870 struct ext4_mountpoint *mp = ext4_get_mount(path);
878 len = ext4_path_check(path, &is_goal);
880 /* Remove entry from parent directory */
881 r = ext4_dir_remove_entry(parent_ref, path, len);
885 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
886 EXT4_INODE_MODE_DIRECTORY)) {
887 ext4_fs_inode_links_count_dec(parent_ref);
888 parent_ref->dirty = true;
894 int ext4_flink(const char *path, const char *hardlink_path)
899 bool child_loaded = false;
900 uint32_t parent_inode, child_inode;
901 struct ext4_mountpoint *mp = ext4_get_mount(path);
902 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
903 struct ext4_inode_ref child_ref;
908 /* Will that happen? Anyway return EINVAL for such case. */
914 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
915 &parent_inode, &name_off);
919 child_inode = f.inode;
922 /*We have file to unlink. Load it.*/
923 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
929 /* Creating hardlink for directory is not allowed. */
930 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
931 EXT4_INODE_MODE_DIRECTORY)) {
936 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
940 ext4_fs_put_inode_ref(&child_ref);
947 int ext4_frename(const char *path, const char *new_path)
952 bool parent_loaded = false, child_loaded = false;
953 uint32_t parent_inode, child_inode;
954 struct ext4_mountpoint *mp = ext4_get_mount(path);
955 struct ext4_inode_ref child_ref, parent_ref;
962 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
963 &parent_inode, &name_off);
967 child_inode = f.inode;
971 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
975 parent_loaded = true;
977 /*We have file to unlink. Load it.*/
978 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
984 r = ext4_create_hardlink(new_path, &child_ref, true);
988 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
994 ext4_fs_put_inode_ref(&parent_ref);
997 ext4_fs_put_inode_ref(&child_ref);
1004 /****************************************************************************/
1006 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1008 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1017 int ext4_cache_write_back(const char *path, bool on)
1019 struct ext4_mountpoint *mp = ext4_get_mount(path);
1025 ext4_block_cache_write_back(mp->fs.bdev, on);
1030 int ext4_fremove(const char *path)
1033 uint32_t parent_inode;
1038 struct ext4_inode_ref child;
1039 struct ext4_inode_ref parent;
1040 struct ext4_mountpoint *mp = ext4_get_mount(path);
1046 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1047 &parent_inode, &name_off);
1054 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1060 /*We have file to delete. Load it.*/
1061 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1063 ext4_fs_put_inode_ref(&parent);
1071 len = ext4_path_check(path, &is_goal);
1073 /*Unlink from parent*/
1074 r = ext4_unlink(mp, &parent, &child, path, len);
1078 /*Link count is zero, the inode should be freed. */
1079 if (!ext4_inode_get_links_cnt(child.inode)) {
1080 ext4_inode_set_del_time(child.inode, -1L);
1082 ext4_block_cache_write_back(mp->fs.bdev, 1);
1083 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1084 r = ext4_fs_truncate_inode(&child, 0);
1085 ext4_block_cache_write_back(mp->fs.bdev, 0);
1090 r = ext4_fs_free_inode(&child);
1096 ext4_fs_put_inode_ref(&child);
1097 ext4_fs_put_inode_ref(&parent);
1102 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1103 struct ext4_inode *inode)
1107 struct ext4_inode_ref inode_ref;
1108 struct ext4_mountpoint *mp = ext4_get_mount(path);
1116 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1126 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1132 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1133 ext4_fs_put_inode_ref(&inode_ref);
1142 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1144 struct ext4_mountpoint *mp = ext4_get_mount(path);
1151 ext4_block_cache_write_back(mp->fs.bdev, 1);
1152 r = ext4_generic_open(f, path, flags, true, 0, 0);
1153 ext4_block_cache_write_back(mp->fs.bdev, 0);
1158 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1160 struct ext4_mountpoint *mp = ext4_get_mount(path);
1167 filetype = EXT4_DE_REG_FILE;
1170 ext4_block_cache_write_back(mp->fs.bdev, 1);
1171 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1172 ext4_block_cache_write_back(mp->fs.bdev, 0);
1177 int ext4_fclose(ext4_file *f)
1179 ext4_assert(f && f->mp);
1184 f->fpos = f->fsize = 0;
1189 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1191 struct ext4_inode_ref ref;
1195 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1197 EXT4_MP_UNLOCK(f->mp);
1202 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1203 if (f->fsize <= size) {
1208 /*Start write back cache mode.*/
1209 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1213 r = ext4_fs_truncate_inode(&ref, size);
1221 /*Stop write back cache mode*/
1222 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1228 ext4_fs_put_inode_ref(&ref);
1233 int ext4_ftruncate(ext4_file *f, uint64_t size)
1236 ext4_assert(f && f->mp);
1238 if (f->flags & O_RDONLY)
1241 EXT4_MP_LOCK(f->mp);
1242 r = ext4_ftruncate_no_lock(f, size);
1243 EXT4_MP_UNLOCK(f->mp);
1247 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1250 uint32_t iblock_idx;
1251 uint32_t iblock_last;
1252 uint32_t block_size;
1254 ext4_fsblk_t fblock;
1255 ext4_fsblk_t fblock_start;
1256 uint32_t fblock_count;
1258 uint8_t *u8_buf = buf;
1260 struct ext4_block b;
1261 struct ext4_inode_ref ref;
1263 ext4_assert(f && f->mp);
1265 if (f->flags & O_WRONLY)
1271 EXT4_MP_LOCK(f->mp);
1273 struct ext4_fs *const fs = &f->mp->fs;
1274 struct ext4_sblock *const sb = &f->mp->fs.sb;
1279 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1281 EXT4_MP_UNLOCK(f->mp);
1286 f->fsize = ext4_inode_get_size(sb, ref.inode);
1288 block_size = ext4_sb_get_block_size(sb);
1289 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1291 iblock_idx = (f->fpos) / block_size;
1292 iblock_last = (f->fpos + size) / block_size;
1293 unalg = (f->fpos) % block_size;
1295 /*If the size of symlink is smaller than 60 bytes*/
1297 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1298 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1299 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1301 char *content = (char *)ref.inode->blocks;
1302 if (f->fpos < f->fsize) {
1304 if (unalg + size > f->fsize)
1305 len = f->fsize - unalg;
1306 memcpy(buf, content + unalg, len);
1318 if (size > (block_size - unalg))
1319 len = block_size - unalg;
1321 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1326 /* Do we get an unwritten range? */
1328 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1332 memcpy(u8_buf, b.data + unalg, len);
1334 r = ext4_block_set(f->mp->fs.bdev, &b);
1339 memset(u8_buf, 0, len);
1354 while (size >= block_size) {
1355 while (iblock_idx < iblock_last) {
1356 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1364 fblock_start = fblock;
1366 if ((fblock_start + fblock_count) != fblock)
1372 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1377 size -= block_size * fblock_count;
1378 u8_buf += block_size * fblock_count;
1379 f->fpos += block_size * fblock_count;
1382 *rcnt += block_size * fblock_count;
1384 fblock_start = fblock;
1389 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1394 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1398 memcpy(u8_buf, b.data, size);
1400 r = ext4_block_set(f->mp->fs.bdev, &b);
1411 ext4_fs_put_inode_ref(&ref);
1412 EXT4_MP_UNLOCK(f->mp);
1416 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1420 uint32_t iblock_last;
1421 uint32_t ifile_blocks;
1422 uint32_t block_size;
1424 uint32_t fblock_count;
1425 ext4_fsblk_t fblock;
1426 ext4_fsblk_t fblock_start;
1428 struct ext4_block b;
1429 struct ext4_inode_ref ref;
1430 const uint8_t *u8_buf = buf;
1433 ext4_assert(f && f->mp);
1435 if (f->flags & O_RDONLY)
1441 EXT4_MP_LOCK(f->mp);
1443 struct ext4_fs *const fs = &f->mp->fs;
1444 struct ext4_sblock *const sb = &f->mp->fs.sb;
1449 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1451 EXT4_MP_UNLOCK(f->mp);
1456 f->fsize = ext4_inode_get_size(sb, ref.inode);
1457 block_size = ext4_sb_get_block_size(sb);
1459 iblock_last = (f->fpos + size) / block_size;
1460 iblk_idx = (f->fpos) / block_size;
1461 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1463 unalg = (f->fpos) % block_size;
1467 if (size > (block_size - unalg))
1468 len = block_size - unalg;
1470 r = ext4_fs_init_inode_data_block_index(&ref, iblk_idx, &fblock);
1474 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1478 memcpy(b.data + unalg, u8_buf, len);
1481 r = ext4_block_set(f->mp->fs.bdev, &b);
1495 /*Start write back cache mode.*/
1496 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1502 while (size >= block_size) {
1504 while (iblk_idx < iblock_last) {
1505 if (iblk_idx < ifile_blocks) {
1506 r = ext4_fs_init_inode_data_block_index(
1507 &ref, iblk_idx, &fblock);
1511 rr = ext4_fs_append_inode_block(&ref, &fblock,
1514 /* Unable to append more blocks. But
1515 * some block might be allocated already
1523 if (!fblock_start) {
1524 fblock_start = fblock;
1527 if ((fblock_start + fblock_count) != fblock)
1533 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1538 size -= block_size * fblock_count;
1539 u8_buf += block_size * fblock_count;
1540 f->fpos += block_size * fblock_count;
1543 *wcnt += block_size * fblock_count;
1545 fblock_start = fblock;
1549 /*ext4_fs_append_inode_block has failed and no
1550 * more blocks might be written. But node size
1551 * should be updated.*/
1557 /*Stop write back cache mode*/
1558 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1564 if (iblk_idx < ifile_blocks) {
1565 r = ext4_fs_init_inode_data_block_index(&ref, iblk_idx,
1570 r = ext4_fs_append_inode_block(&ref, &fblock, &iblk_idx);
1572 /*Node size sholud be updated.*/
1576 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1580 memcpy(b.data, u8_buf, size);
1583 r = ext4_block_set(f->mp->fs.bdev, &b);
1594 if (f->fpos > f->fsize) {
1596 ext4_inode_set_size(ref.inode, f->fsize);
1601 ext4_fs_put_inode_ref(&ref);
1602 EXT4_MP_UNLOCK(f->mp);
1606 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1610 if (offset > f->fsize)
1616 if ((offset + f->fpos) > f->fsize)
1622 if (offset > f->fsize)
1625 f->fpos = f->fsize - offset;
1631 uint64_t ext4_ftell(ext4_file *f)
1636 uint64_t ext4_fsize(ext4_file *f)
1641 int ext4_chmod(const char *path, uint32_t mode)
1646 struct ext4_sblock *sb;
1647 struct ext4_inode_ref inode_ref;
1648 struct ext4_mountpoint *mp = ext4_get_mount(path);
1655 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1663 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1669 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1670 inode_ref.dirty = true;
1672 ext4_fs_put_inode_ref(&inode_ref);
1677 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1682 struct ext4_inode_ref inode_ref;
1683 struct ext4_mountpoint *mp = ext4_get_mount(path);
1690 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1697 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1703 ext4_inode_set_uid(inode_ref.inode, uid);
1704 ext4_inode_set_gid(inode_ref.inode, gid);
1705 inode_ref.dirty = true;
1707 ext4_fs_put_inode_ref(&inode_ref);
1712 int ext4_file_set_atime(const char *path, uint32_t atime)
1717 struct ext4_inode_ref inode_ref;
1718 struct ext4_mountpoint *mp = ext4_get_mount(path);
1725 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1732 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1738 ext4_inode_set_access_time(inode_ref.inode, atime);
1739 inode_ref.dirty = true;
1741 ext4_fs_put_inode_ref(&inode_ref);
1746 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1751 struct ext4_inode_ref inode_ref;
1752 struct ext4_mountpoint *mp = ext4_get_mount(path);
1759 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1766 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1772 ext4_inode_set_modif_time(inode_ref.inode, mtime);
1773 inode_ref.dirty = true;
1775 ext4_fs_put_inode_ref(&inode_ref);
1780 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1785 struct ext4_inode_ref inode_ref;
1786 struct ext4_mountpoint *mp = ext4_get_mount(path);
1793 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1800 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1806 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1807 inode_ref.dirty = true;
1809 ext4_fs_put_inode_ref(&inode_ref);
1814 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1816 struct ext4_block b;
1817 struct ext4_inode_ref ref;
1819 ext4_fsblk_t fblock;
1820 uint32_t block_size;
1823 ext4_assert(f && f->mp);
1828 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1830 EXT4_MP_UNLOCK(f->mp);
1835 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1836 if (size > block_size) {
1840 r = ext4_ftruncate_no_lock(f, 0);
1844 /*Start write back cache mode.*/
1845 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1849 /*If the size of symlink is smaller than 60 bytes*/
1850 if (size < sizeof(ref.inode->blocks)) {
1851 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1852 memcpy(ref.inode->blocks, buf, size);
1853 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1855 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1856 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1860 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1864 memcpy(b.data, buf, size);
1866 r = ext4_block_set(f->mp->fs.bdev, &b);
1871 /*Stop write back cache mode*/
1872 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1877 ext4_inode_set_size(ref.inode, size);
1885 ext4_fs_put_inode_ref(&ref);
1889 int ext4_fsymlink(const char *target, const char *path)
1891 struct ext4_mountpoint *mp = ext4_get_mount(path);
1899 filetype = EXT4_DE_SYMLINK;
1902 ext4_block_cache_write_back(mp->fs.bdev, 1);
1903 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
1905 r = ext4_fsymlink_set(&f, target, strlen(target));
1912 ext4_block_cache_write_back(mp->fs.bdev, 0);
1917 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1919 struct ext4_mountpoint *mp = ext4_get_mount(path);
1930 filetype = EXT4_DE_SYMLINK;
1933 ext4_block_cache_write_back(mp->fs.bdev, 1);
1934 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
1936 r = ext4_fread(&f, buf, bufsize, rcnt);
1943 ext4_block_cache_write_back(mp->fs.bdev, 0);
1948 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1949 const void *data, size_t data_size, bool replace)
1955 const char *dissected_name = NULL;
1956 size_t dissected_len = 0;
1957 struct ext4_xattr_ref xattr_ref;
1958 struct ext4_inode_ref inode_ref;
1959 struct ext4_mountpoint *mp = ext4_get_mount(path);
1963 dissected_name = ext4_extract_xattr_name(name, name_len,
1964 &name_index, &dissected_len);
1969 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1975 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
1979 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
1981 ext4_fs_put_inode_ref(&inode_ref);
1985 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
1986 dissected_len, data, data_size, replace);
1988 ext4_fs_put_xattr_ref(&xattr_ref);
1989 ext4_fs_put_inode_ref(&inode_ref);
1995 int ext4_getxattr(const char *path, const char *name, size_t name_len,
1996 void *buf, size_t buf_size, size_t *data_size)
2002 const char *dissected_name = NULL;
2003 size_t dissected_len = 0;
2004 struct ext4_xattr_ref xattr_ref;
2005 struct ext4_inode_ref inode_ref;
2006 struct ext4_mountpoint *mp = ext4_get_mount(path);
2010 dissected_name = ext4_extract_xattr_name(name, name_len,
2011 &name_index, &dissected_len);
2016 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2022 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2026 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2028 ext4_fs_put_inode_ref(&inode_ref);
2032 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2033 dissected_len, buf, buf_size, data_size);
2035 ext4_fs_put_xattr_ref(&xattr_ref);
2036 ext4_fs_put_inode_ref(&inode_ref);
2042 struct ext4_listxattr_iterator {
2047 bool list_too_small;
2048 bool get_required_size;
2051 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2052 struct ext4_xattr_item *item)
2054 struct ext4_listxattr_iterator *lxi;
2055 lxi = ref->iter_arg;
2056 if (!lxi->get_required_size) {
2059 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2060 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2061 lxi->list_too_small = 1;
2062 return EXT4_XATTR_ITERATE_STOP;
2065 memcpy(lxi->list_ptr, prefix, plen);
2066 lxi->list_ptr += plen;
2067 lxi->ret_size += plen;
2069 memcpy(lxi->list_ptr, item->name, item->name_len);
2070 lxi->list_ptr[item->name_len] = 0;
2071 lxi->list_ptr += item->name_len + 1;
2073 lxi->ret_size += item->name_len + 1;
2074 return EXT4_XATTR_ITERATE_CONT;
2077 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2082 struct ext4_xattr_ref xattr_ref;
2083 struct ext4_inode_ref inode_ref;
2084 struct ext4_listxattr_iterator lxi;
2085 struct ext4_mountpoint *mp = ext4_get_mount(path);
2090 lxi.list_ptr = list;
2093 lxi.list_too_small = false;
2094 lxi.get_required_size = (!size) ? true : false;
2097 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2103 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2107 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2109 ext4_fs_put_inode_ref(&inode_ref);
2113 xattr_ref.iter_arg = &lxi;
2114 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2115 if (lxi.list_too_small)
2120 *ret_size = lxi.ret_size;
2123 ext4_fs_put_xattr_ref(&xattr_ref);
2124 ext4_fs_put_inode_ref(&inode_ref);
2131 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2137 const char *dissected_name = NULL;
2138 size_t dissected_len = 0;
2139 struct ext4_xattr_ref xattr_ref;
2140 struct ext4_inode_ref inode_ref;
2141 struct ext4_mountpoint *mp = ext4_get_mount(path);
2145 dissected_name = ext4_extract_xattr_name(name, name_len,
2146 &name_index, &dissected_len);
2151 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2157 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2161 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2163 ext4_fs_put_inode_ref(&inode_ref);
2167 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2170 ext4_fs_put_xattr_ref(&xattr_ref);
2171 ext4_fs_put_inode_ref(&inode_ref);
2178 /*********************************DIRECTORY OPERATION************************/
2180 int ext4_dir_rm(const char *path)
2186 struct ext4_mountpoint *mp = ext4_get_mount(path);
2187 struct ext4_inode_ref act;
2188 struct ext4_inode_ref child;
2189 struct ext4_dir_iter it;
2193 uint32_t inode_current;
2205 struct ext4_fs *const fs = &mp->fs;
2208 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2215 len = ext4_path_check(path, &is_goal);
2217 inode_current = f.inode;
2220 ext4_block_cache_write_back(mp->fs.bdev, 1);
2223 /*Load directory node.*/
2224 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2229 /*Initialize iterator.*/
2230 r = ext4_dir_iterator_init(&it, &act, 0);
2232 ext4_fs_put_inode_ref(&act);
2243 /*Get up directory inode when ".." entry*/
2244 if ((it.curr->name_len == 2) &&
2245 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2246 inode_up = ext4_dir_en_get_inode(it.curr);
2249 /*If directory or file entry, but not "." ".." entry*/
2250 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2252 /*Get child inode reference do unlink
2255 cinode = ext4_dir_en_get_inode(it.curr);
2256 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2260 /*If directory with no leaf children*/
2261 r = ext4_has_children(&has_children, &child);
2263 ext4_fs_put_inode_ref(&child);
2268 /*Has directory children. Go into this
2270 inode_up = inode_current;
2271 inode_current = cinode;
2273 ext4_fs_put_inode_ref(&child);
2277 /*No children in child directory or file. Just
2279 r = ext4_unlink(f.mp, &act, &child,
2280 (char *)it.curr->name,
2283 ext4_fs_put_inode_ref(&child);
2287 ext4_inode_set_del_time(child.inode, -1L);
2288 ext4_inode_set_links_cnt(child.inode, 0);
2291 r = ext4_fs_truncate_inode(&child, 0);
2293 ext4_fs_put_inode_ref(&child);
2297 r = ext4_fs_free_inode(&child);
2299 ext4_fs_put_inode_ref(&child);
2303 r = ext4_fs_put_inode_ref(&child);
2308 r = ext4_dir_iterator_next(&it);
2312 /*Directory iterator reached last entry*/
2313 ext4_has_children(&has_children, &act);
2314 if (!has_children) {
2315 inode_current = inode_up;
2322 struct ext4_inode_ref parent;
2323 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2328 /* In this place all directories should be
2330 * Last unlink from root of current directory*/
2331 r = ext4_unlink(f.mp, &parent, &act,
2334 ext4_fs_put_inode_ref(&parent);
2338 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2339 ext4_inode_set_del_time(act.inode, -1L);
2340 ext4_inode_set_links_cnt(act.inode, 0);
2343 r = ext4_fs_truncate_inode(&act, 0);
2345 ext4_fs_put_inode_ref(&parent);
2349 r = ext4_fs_free_inode(&act);
2351 ext4_fs_put_inode_ref(&parent);
2356 r = ext4_fs_put_inode_ref(&parent);
2363 ext4_dir_iterator_fini(&it);
2364 ext4_fs_put_inode_ref(&act);
2367 /*When something goes wrong. End loop.*/
2373 ext4_block_cache_write_back(mp->fs.bdev, 0);
2378 int ext4_dir_mk(const char *path)
2383 struct ext4_mountpoint *mp = ext4_get_mount(path);
2391 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2393 /*Directory already created*/
2399 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2409 int ext4_dir_open(ext4_dir *d, const char *path)
2411 struct ext4_mountpoint *mp = ext4_get_mount(path);
2418 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2424 int ext4_dir_close(ext4_dir *d)
2426 return ext4_fclose(&d->f);
2429 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2431 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2434 ext4_direntry *de = 0;
2435 struct ext4_inode_ref dir;
2436 struct ext4_dir_iter it;
2438 EXT4_MP_LOCK(d->f.mp);
2440 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2441 EXT4_MP_UNLOCK(d->f.mp);
2445 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2450 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2452 ext4_fs_put_inode_ref(&dir);
2456 memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2459 ext4_dir_iterator_next(&it);
2461 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2463 ext4_dir_iterator_fini(&it);
2464 ext4_fs_put_inode_ref(&dir);
2467 EXT4_MP_UNLOCK(d->f.mp);
2471 void ext4_dir_entry_rewind(ext4_dir *d)