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"
47 #include "ext4_xattr.h"
53 /**@brief Mount point OS dependent lock*/
54 #define EXT4_MP_LOCK(_m) \
57 (_m)->os_locks->lock(); \
60 /**@brief Mount point OS dependent unlock*/
61 #define EXT4_MP_UNLOCK(_m) \
64 (_m)->os_locks->unlock(); \
67 /**@brief Mount point descriptor.*/
68 struct ext4_mountpoint {
70 /**@brief Mount done flag.*/
73 /**@brief Mount point name (@ref ext4_mount)*/
76 /**@brief OS dependent lock/unlock functions.*/
77 const struct ext4_lock *os_locks;
79 /**@brief Ext4 filesystem internals.*/
82 /**@brief Dynamic allocation cache flag.*/
86 /**@brief Block devices descriptor.*/
87 struct _ext4_devices {
89 /**@brief Block device name (@ref ext4_device_register)*/
92 /**@brief Block device handle.*/
93 struct ext4_blockdev *bd;
95 /**@brief Block cache handle.*/
96 struct ext4_bcache *bc;
99 /**@brief Block devices.*/
100 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
102 /**@brief Mountpoints.*/
103 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
105 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
106 const char *dev_name)
109 ext4_assert(bd && dev_name);
111 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
112 if (!_bdevices[i].bd) {
113 strcpy(_bdevices[i].name, dev_name);
114 _bdevices[i].bd = bd;
115 _bdevices[i].bc = bc;
119 if (!strcmp(_bdevices[i].name, dev_name))
125 /****************************************************************************/
127 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
129 if ((name_size == 1) && (name[0] == '.'))
132 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
138 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
140 struct ext4_fs *fs = enode->fs;
142 /* Check if node is directory */
143 if (!ext4_inode_is_type(&fs->sb, enode->inode,
144 EXT4_INODE_MODE_DIRECTORY)) {
145 *has_children = false;
149 struct ext4_directory_iterator it;
150 int rc = ext4_dir_iterator_init(&it, enode, 0);
154 /* Find a non-empty directory entry */
156 while (it.current != NULL) {
157 if (ext4_dir_entry_ll_get_inode(it.current) != 0) {
158 uint16_t name_size = ext4_dir_entry_ll_get_name_length(
159 &fs->sb, it.current);
160 if (!ext4_is_dots(it.current->name, name_size)) {
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 *name,
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 rc = ext4_dir_add_entry(parent, name, name_len, child);
195 /* Fill new dir -> add '.' and '..' entries.
196 * Also newly allocated inode should have 0 link count.
198 if (ext4_inode_is_type(&mp->fs.sb, child->inode,
199 EXT4_INODE_MODE_DIRECTORY) &&
202 #if CONFIG_DIR_INDEX_ENABLE
203 /* Initialize directory index if supported */
204 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
205 rc = ext4_dir_dx_init(child, parent);
209 ext4_inode_set_flag(child->inode,
210 EXT4_INODE_FLAG_INDEX);
215 rc = ext4_dir_add_entry(child, ".", strlen("."), child);
217 ext4_dir_remove_entry(parent, name, strlen(name));
221 rc = ext4_dir_add_entry(child, "..", strlen(".."), parent);
223 ext4_dir_remove_entry(parent, name, strlen(name));
224 ext4_dir_remove_entry(child, ".", strlen("."));
229 /*New empty directory. Two links (. and ..) */
230 ext4_inode_set_links_count(child->inode, 2);
232 ext4_fs_inode_links_count_inc(parent);
234 parent->dirty = true;
237 * In case we want to rename a directory,
238 * we reset the original '..' pointer.
240 if (ext4_inode_is_type(&mp->fs.sb, child->inode,
241 EXT4_INODE_MODE_DIRECTORY)) {
243 ext4_inode_has_flag(child->inode,
244 EXT4_INODE_FLAG_INDEX);
245 struct ext4_directory_search_result result;
246 if (!has_flag_index) {
247 rc = ext4_dir_find_entry(&result,
253 ext4_dir_entry_ll_set_inode(result.dentry,
255 result.block.dirty = true;
256 rc = ext4_dir_destroy_result(child, &result);
261 #if CONFIG_DIR_INDEX_ENABLE
262 rc = ext4_dir_dx_reset_parent_inode(child,
270 ext4_fs_inode_links_count_inc(parent);
271 parent->dirty = true;
274 ext4_fs_inode_links_count_inc(child);
282 static int ext4_unlink(struct ext4_mountpoint *mp,
283 struct ext4_inode_ref *parent,
284 struct ext4_inode_ref *child_inode_ref, const char *name,
288 int rc = ext4_has_children(&has_children, child_inode_ref);
292 /* Cannot unlink non-empty node */
296 /* Remove entry from parent directory */
297 rc = ext4_dir_remove_entry(parent, name, name_len);
301 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
302 EXT4_INODE_MODE_DIRECTORY);
304 /* If directory - handle links from parent */
306 // ext4_assert(ext4_inode_get_links_count(child_inode_ref->inode)
308 ext4_fs_inode_links_count_dec(parent);
309 parent->dirty = true;
313 * TODO: Update timestamps of the parent
314 * (when we have wall-clock time).
316 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
317 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
318 * parent->dirty = true;
322 * TODO: Update timestamp for inode.
324 * ext4_inode_set_change_inode_time(child_inode_ref->inode,
327 if (ext4_inode_get_links_count(child_inode_ref->inode)) {
328 ext4_fs_inode_links_count_dec(child_inode_ref);
329 child_inode_ref->dirty = true;
335 /****************************************************************************/
337 int ext4_mount(const char *dev_name, const char *mount_point)
339 ext4_assert(mount_point && dev_name);
344 struct ext4_blockdev *bd = 0;
345 struct ext4_bcache *bc = 0;
346 struct ext4_mountpoint *mp = 0;
348 if (mount_point[strlen(mount_point) - 1] != '/')
351 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
352 if (_bdevices[i].name) {
353 if (!strcmp(dev_name, _bdevices[i].name)) {
354 bd = _bdevices[i].bd;
355 bc = _bdevices[i].bc;
364 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
365 if (!_mp[i].mounted) {
366 strcpy(_mp[i].name, mount_point);
372 if (!strcmp(_mp[i].name, mount_point))
379 r = ext4_block_init(bd);
383 r = ext4_fs_init(&mp->fs, bd);
389 bsize = ext4_sb_get_block_size(&mp->fs.sb);
390 ext4_block_set_lb_size(bd, bsize);
392 mp->cache_dynamic = 0;
395 /*Automatic block cache alloc.*/
396 mp->cache_dynamic = 1;
397 bc = malloc(sizeof(struct ext4_bcache));
399 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
408 if (bsize != bc->itemsize)
411 /*Bind block cache to block device*/
412 r = ext4_block_bind_bcache(bd, bc);
415 if (mp->cache_dynamic) {
416 ext4_bcache_fini_dynamic(bc);
425 int ext4_umount(const char *mount_point)
429 struct ext4_mountpoint *mp = 0;
431 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
432 if (!strcmp(_mp[i].name, mount_point)) {
441 r = ext4_fs_fini(&mp->fs);
447 if (mp->cache_dynamic) {
448 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
449 free(mp->fs.bdev->bc);
452 return ext4_block_fini(mp->fs.bdev);
455 static struct ext4_mountpoint *ext4_get_mount(const char *path)
458 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
463 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
469 int ext4_mount_point_stats(const char *mount_point,
470 struct ext4_mount_stats *stats)
472 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
478 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
479 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
480 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
481 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
482 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
484 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
485 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
486 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
488 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
494 int ext4_mount_setup_locks(const char *mount_point,
495 const struct ext4_lock *locks)
498 struct ext4_mountpoint *mp = 0;
500 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
501 if (!strcmp(_mp[i].name, mount_point)) {
509 mp->os_locks = locks;
513 /********************************FILE OPERATIONS*****************************/
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_DIRENTRY_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;
585 uint8_t inode_type = EXT4_DIRENTRY_UNKNOWN;
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_DIRENTRY_DIR) ||
625 (filetype == EXT4_DIRENTRY_UNKNOWN)))
632 r = ext4_dir_find_entry(&result, &ref, path, len);
635 /*Destroy last result*/
636 ext4_dir_destroy_result(&ref, &result);
641 if (!(f->flags & O_CREAT))
644 /*O_CREAT allows create new entry*/
645 struct ext4_inode_ref child_ref;
646 r = ext4_fs_alloc_inode(
649 : EXT4_DIRENTRY_DIR);
654 /*Link with root dir.*/
655 r = ext4_link(mp, &ref, &child_ref, path, len, false);
657 /*Fail. Free new inode.*/
658 ext4_fs_free_inode(&child_ref);
659 /*We do not want to write new inode.
660 But block has to be released.*/
661 child_ref.dirty = false;
662 ext4_fs_put_inode_ref(&child_ref);
666 ext4_fs_put_inode_ref(&child_ref);
672 *parent_inode = ref.index;
674 next_inode = ext4_dir_entry_ll_get_inode(result.dentry);
676 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
678 r = ext4_dir_destroy_result(&ref, &result);
682 /*If expected file error*/
683 if (inode_type != EXT4_DIRENTRY_DIR && !is_goal) {
687 if (filetype != EXT4_DIRENTRY_UNKNOWN) {
688 if ((inode_type != filetype) && is_goal) {
694 r = ext4_fs_put_inode_ref(&ref);
698 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
708 *name_off += len + 1;
712 ext4_fs_put_inode_ref(&ref);
718 if ((f->flags & O_TRUNC) &&
719 (inode_type == EXT4_DIRENTRY_REG_FILE)) {
721 r = ext4_fs_truncate_inode(&ref, 0);
723 ext4_fs_put_inode_ref(&ref);
729 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
730 f->inode = ref.index;
733 if (f->flags & O_APPEND)
738 r = ext4_fs_put_inode_ref(&ref);
742 /****************************************************************************/
744 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
745 bool file_expect, uint32_t *parent_inode,
750 if (ext4_parse_flags(flags, &iflags) == false)
753 if (file_expect == true)
754 filetype = EXT4_DIRENTRY_REG_FILE;
756 filetype = EXT4_DIRENTRY_DIR;
758 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
762 static int __ext4_create_hardlink(const char *path,
763 struct ext4_inode_ref *child_ref,
766 bool is_goal = false;
767 uint8_t inode_type = EXT4_DIRENTRY_DIR;
771 struct ext4_mountpoint *mp = ext4_get_mount(path);
772 struct ext4_directory_search_result result;
773 struct ext4_inode_ref ref;
779 path += strlen(mp->name);
782 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
787 int len = ext4_path_check(path, &is_goal);
791 len = ext4_path_check(path, &is_goal);
794 /*If root open was request.*/
802 r = ext4_dir_find_entry(&result, &ref, path, len);
805 /*Destroy last result*/
806 ext4_dir_destroy_result(&ref, &result);
808 if (r != ENOENT || !is_goal)
811 /*Link with root dir.*/
812 r = ext4_link(mp, &ref, child_ref, path, len, rename);
814 } else if (r == EOK && is_goal) {
815 /*Destroy last result*/
816 ext4_dir_destroy_result(&ref, &result);
821 next_inode = result.dentry->inode;
823 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
825 r = ext4_dir_destroy_result(&ref, &result);
829 if (inode_type == EXT4_DIRENTRY_REG_FILE) {
838 r = ext4_fs_put_inode_ref(&ref);
842 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
853 ext4_fs_put_inode_ref(&ref);
857 r = ext4_fs_put_inode_ref(&ref);
861 static int ext4_remove_orig_reference(const char *path,
863 struct ext4_inode_ref *parent_ref,
864 struct ext4_inode_ref *child_ref)
869 struct ext4_mountpoint *mp = ext4_get_mount(path);
877 len = ext4_path_check(path, &is_goal);
879 /* Remove entry from parent directory */
880 r = ext4_dir_remove_entry(parent_ref, path, len);
884 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
885 EXT4_INODE_MODE_DIRECTORY)) {
886 ext4_fs_inode_links_count_dec(parent_ref);
887 parent_ref->dirty = true;
893 int ext4_flink(const char *path, const char *hardlink_path)
898 bool child_loaded = false;
899 uint32_t parent_inode, child_inode;
900 struct ext4_mountpoint *mp = ext4_get_mount(path);
901 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
902 struct ext4_inode_ref child_ref;
907 /* Will that happen? Anyway return EINVAL for such case. */
913 r = ext4_generic_open2(&f, path, O_RDONLY,
914 EXT4_DIRENTRY_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,
963 EXT4_DIRENTRY_UNKNOWN,
964 &parent_inode, &name_off);
968 child_inode = f.inode;
972 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
976 parent_loaded = true;
978 /*We have file to unlink. Load it.*/
979 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
985 r = __ext4_create_hardlink(new_path, &child_ref, true);
989 r = ext4_remove_orig_reference(path, name_off,
990 &parent_ref, &child_ref);
996 ext4_fs_put_inode_ref(&parent_ref);
999 ext4_fs_put_inode_ref(&child_ref);
1006 /****************************************************************************/
1008 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1010 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1019 int ext4_cache_write_back(const char *path, bool on)
1021 struct ext4_mountpoint *mp = ext4_get_mount(path);
1027 ext4_block_cache_write_back(mp->fs.bdev, on);
1032 int ext4_fremove(const char *path)
1035 uint32_t parent_inode;
1040 struct ext4_inode_ref child;
1041 struct ext4_inode_ref parent;
1042 struct ext4_mountpoint *mp = ext4_get_mount(path);
1048 r = ext4_generic_open2(&f, path, O_RDWR,
1049 EXT4_DIRENTRY_UNKNOWN,
1050 &parent_inode, &name_off);
1057 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1063 /*We have file to delete. Load it.*/
1064 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1066 ext4_fs_put_inode_ref(&parent);
1074 len = ext4_path_check(path, &is_goal);
1076 /*Unlink from parent*/
1077 r = ext4_unlink(mp, &parent, &child, path, len);
1081 /*Link count is zero, the inode should be freed. */
1082 if (!ext4_inode_get_links_count(child.inode)) {
1083 ext4_inode_set_deletion_time(child.inode, 0xFFFFFFFF);
1085 ext4_block_cache_write_back(mp->fs.bdev, 1);
1086 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1087 r = ext4_fs_truncate_inode(&child, 0);
1088 ext4_block_cache_write_back(mp->fs.bdev, 0);
1093 r = ext4_fs_free_inode(&child);
1099 ext4_fs_put_inode_ref(&child);
1100 ext4_fs_put_inode_ref(&parent);
1105 int ext4_fill_raw_inode(const char *path,
1107 struct ext4_inode *inode)
1111 struct ext4_inode_ref inode_ref;
1112 struct ext4_mountpoint *mp = ext4_get_mount(path);
1120 r = ext4_generic_open2(&f, path, O_RDONLY,
1121 EXT4_DIRENTRY_UNKNOWN,
1132 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1138 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1140 ext4_fs_put_inode_ref(&inode_ref);
1149 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1151 struct ext4_mountpoint *mp = ext4_get_mount(path);
1158 ext4_block_cache_write_back(mp->fs.bdev, 1);
1159 r = ext4_generic_open(f, path, flags, true, 0, 0);
1160 ext4_block_cache_write_back(mp->fs.bdev, 0);
1165 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1167 struct ext4_mountpoint *mp = ext4_get_mount(path);
1174 filetype = EXT4_DIRENTRY_REG_FILE;
1177 ext4_block_cache_write_back(mp->fs.bdev, 1);
1178 r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
1179 ext4_block_cache_write_back(mp->fs.bdev, 0);
1184 int ext4_fclose(ext4_file *f)
1186 ext4_assert(f && f->mp);
1191 f->fpos = f->fsize = 0;
1196 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1198 struct ext4_inode_ref ref;
1202 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1204 EXT4_MP_UNLOCK(f->mp);
1209 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1210 if (f->fsize <= size) {
1215 /*Start write back cache mode.*/
1216 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1220 r = ext4_fs_truncate_inode(&ref, size);
1228 /*Stop write back cache mode*/
1229 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1235 ext4_fs_put_inode_ref(&ref);
1240 int ext4_ftruncate(ext4_file *f, uint64_t size)
1243 ext4_assert(f && f->mp);
1245 if (f->flags & O_RDONLY)
1248 EXT4_MP_LOCK(f->mp);
1250 r = ext4_ftruncate_no_lock(f, size);
1252 EXT4_MP_UNLOCK(f->mp);
1256 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1259 uint32_t iblock_idx;
1260 uint32_t iblock_last;
1261 uint32_t block_size;
1263 ext4_fsblk_t fblock;
1264 ext4_fsblk_t fblock_start;
1265 uint32_t fblock_count;
1267 uint8_t *u8_buf = buf;
1269 struct ext4_block b;
1270 struct ext4_inode_ref ref;
1272 ext4_assert(f && f->mp);
1274 if (f->flags & O_WRONLY)
1280 EXT4_MP_LOCK(f->mp);
1285 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1287 EXT4_MP_UNLOCK(f->mp);
1292 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1294 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1295 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1297 iblock_idx = (f->fpos) / block_size;
1298 iblock_last = (f->fpos + size) / block_size;
1299 unaligned = (f->fpos) % block_size;
1301 /*If the size of symlink is smaller than 60 bytes*/
1302 if (ext4_inode_is_type(&f->mp->fs.sb, ref.inode, EXT4_INODE_MODE_SOFTLINK)
1303 && f->fsize < sizeof(ref.inode->blocks)
1304 && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
1305 char *content = (char *)ref.inode->blocks;
1306 if (f->fpos < f->fsize) {
1307 r = (unaligned + size > f->fsize)
1308 ?(f->fsize - unaligned)
1310 memcpy(buf, content + unaligned, r);
1326 uint32_t l = size > (block_size - unaligned) ?
1327 (block_size - unaligned) : size;
1328 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1333 /* Do we get an unwritten range? */
1335 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1339 memcpy(u8_buf, b.data + unaligned, l);
1341 r = ext4_block_set(f->mp->fs.bdev, &b);
1346 memset(u8_buf, 0, l);
1361 while (size >= block_size) {
1362 while (iblock_idx < iblock_last) {
1363 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1371 fblock_start = fblock;
1373 if ((fblock_start + fblock_count) != fblock)
1379 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1384 size -= block_size * fblock_count;
1385 u8_buf += block_size * fblock_count;
1386 f->fpos += block_size * fblock_count;
1389 *rcnt += block_size * fblock_count;
1391 fblock_start = fblock;
1396 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1401 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1405 memcpy(u8_buf, b.data, size);
1407 r = ext4_block_set(f->mp->fs.bdev, &b);
1418 ext4_fs_put_inode_ref(&ref);
1419 EXT4_MP_UNLOCK(f->mp);
1423 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1426 uint32_t iblock_idx;
1427 uint32_t iblock_last;
1428 uint32_t ifile_blocks;
1429 uint32_t block_size;
1431 uint32_t fblock_count;
1432 ext4_fsblk_t fblock;
1433 ext4_fsblk_t fblock_start;
1435 struct ext4_block b;
1436 struct ext4_inode_ref ref;
1437 const uint8_t *u8_buf = buf;
1440 ext4_assert(f && f->mp);
1442 if (f->flags & O_RDONLY)
1448 EXT4_MP_LOCK(f->mp);
1453 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1455 EXT4_MP_UNLOCK(f->mp);
1460 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1461 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1463 iblock_last = (f->fpos + size) / block_size;
1464 iblock_idx = (f->fpos) / block_size;
1465 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1467 unaligned = (f->fpos) % block_size;
1470 uint32_t l = size > (block_size - unaligned) ?
1471 (block_size - unaligned) : size;
1472 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx, &fblock);
1476 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1480 memcpy(b.data + unaligned, u8_buf, l);
1483 r = ext4_block_set(f->mp->fs.bdev, &b);
1497 /*Start write back cache mode.*/
1498 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1504 while (size >= block_size) {
1506 while (iblock_idx < iblock_last) {
1507 if (iblock_idx < ifile_blocks) {
1508 r = ext4_fs_init_inode_data_block_index(
1509 &ref, iblock_idx, &fblock);
1513 rr = ext4_fs_append_inode_block(&ref, &fblock,
1516 /* Unable to append more blocks. But some
1517 * block might be allocated already.
1525 if (!fblock_start) {
1526 fblock_start = fblock;
1529 if ((fblock_start + fblock_count) != fblock)
1535 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1540 size -= block_size * fblock_count;
1541 u8_buf += block_size * fblock_count;
1542 f->fpos += block_size * fblock_count;
1545 *wcnt += block_size * fblock_count;
1547 fblock_start = fblock;
1551 /*ext4_fs_append_inode_block has failed and no
1552 * more blocks might be written. But node size
1553 * should be updated.*/
1559 /*Stop write back cache mode*/
1560 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1566 if (iblock_idx < ifile_blocks) {
1567 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx,
1572 r = ext4_fs_append_inode_block(&ref, &fblock, &iblock_idx);
1574 /*Node size sholud be updated.*/
1578 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1582 memcpy(b.data, u8_buf, size);
1585 r = ext4_block_set(f->mp->fs.bdev, &b);
1596 if (f->fpos > f->fsize) {
1598 ext4_inode_set_size(ref.inode, f->fsize);
1603 ext4_fs_put_inode_ref(&ref);
1604 EXT4_MP_UNLOCK(f->mp);
1608 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1612 if (offset > f->fsize)
1618 if ((offset + f->fpos) > f->fsize)
1624 if (offset > f->fsize)
1627 f->fpos = f->fsize - offset;
1633 uint64_t ext4_ftell(ext4_file *f) { return f->fpos; }
1635 uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
1637 int ext4_chmod(const char *path, uint32_t mode)
1642 struct ext4_sblock *sb;
1643 struct ext4_inode_ref inode_ref;
1644 struct ext4_mountpoint *mp = ext4_get_mount(path);
1651 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1659 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1665 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1666 inode_ref.dirty = true;
1668 ext4_fs_put_inode_ref(&inode_ref);
1673 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1678 struct ext4_inode_ref inode_ref;
1679 struct ext4_mountpoint *mp = ext4_get_mount(path);
1686 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1693 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1699 ext4_inode_set_uid(inode_ref.inode, uid);
1700 ext4_inode_set_gid(inode_ref.inode, gid);
1701 inode_ref.dirty = true;
1703 ext4_fs_put_inode_ref(&inode_ref);
1708 int ext4_file_set_atime(const char *path, uint32_t atime)
1713 struct ext4_inode_ref inode_ref;
1714 struct ext4_mountpoint *mp = ext4_get_mount(path);
1721 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1728 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1734 ext4_inode_set_access_time(inode_ref.inode, atime);
1735 inode_ref.dirty = true;
1737 ext4_fs_put_inode_ref(&inode_ref);
1742 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1747 struct ext4_inode_ref inode_ref;
1748 struct ext4_mountpoint *mp = ext4_get_mount(path);
1755 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1762 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1768 ext4_inode_set_modification_time(inode_ref.inode, mtime);
1769 inode_ref.dirty = true;
1771 ext4_fs_put_inode_ref(&inode_ref);
1776 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1781 struct ext4_inode_ref inode_ref;
1782 struct ext4_mountpoint *mp = ext4_get_mount(path);
1789 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1796 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1802 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1803 inode_ref.dirty = true;
1805 ext4_fs_put_inode_ref(&inode_ref);
1810 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1812 struct ext4_block b;
1813 struct ext4_inode_ref ref;
1815 ext4_fsblk_t fblock;
1816 uint32_t block_size;
1819 ext4_assert(f && f->mp);
1824 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1826 EXT4_MP_UNLOCK(f->mp);
1831 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1832 if (size > block_size) {
1836 r = ext4_ftruncate_no_lock(f, 0);
1840 /*Start write back cache mode.*/
1841 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1845 /*If the size of symlink is smaller than 60 bytes*/
1846 if (size < sizeof(ref.inode->blocks)) {
1847 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1848 memcpy(ref.inode->blocks, buf, size);
1849 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1851 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1852 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1856 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1860 memcpy(b.data, buf, size);
1862 r = ext4_block_set(f->mp->fs.bdev, &b);
1867 /*Stop write back cache mode*/
1868 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1873 ext4_inode_set_size(ref.inode, size);
1881 ext4_fs_put_inode_ref(&ref);
1885 int ext4_fsymlink(const char *target, const char *path)
1887 struct ext4_mountpoint *mp = ext4_get_mount(path);
1895 filetype = EXT4_DIRENTRY_SYMLINK;
1898 ext4_block_cache_write_back(mp->fs.bdev, 1);
1899 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
1901 r = ext4_fsymlink_set(&f, target, strlen(target));
1908 ext4_block_cache_write_back(mp->fs.bdev, 0);
1913 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1915 struct ext4_mountpoint *mp = ext4_get_mount(path);
1926 filetype = EXT4_DIRENTRY_SYMLINK;
1929 ext4_block_cache_write_back(mp->fs.bdev, 1);
1930 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, 0, 0);
1932 r = ext4_fread(&f, buf, bufsize, rcnt);
1939 ext4_block_cache_write_back(mp->fs.bdev, 0);
1944 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1945 const void *data, size_t data_size, bool replace)
1951 const char *dissected_name = NULL;
1952 size_t dissected_len = 0;
1953 struct ext4_xattr_ref xattr_ref;
1954 struct ext4_inode_ref inode_ref;
1955 struct ext4_mountpoint *mp = ext4_get_mount(path);
1959 dissected_name = ext4_extract_xattr_name(name, name_len,
1960 &name_index, &dissected_len);
1965 r = ext4_generic_open2(&f, path, O_RDWR,
1966 EXT4_DIRENTRY_UNKNOWN, 0, 0);
1972 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
1976 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
1979 ext4_fs_put_inode_ref(&inode_ref);
1983 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
1984 dissected_len, data, data_size, replace);
1986 ext4_fs_put_xattr_ref(&xattr_ref);
1987 ext4_fs_put_inode_ref(&inode_ref);
1993 int ext4_getxattr(const char *path, const char *name, size_t name_len,
1994 void *buf, size_t buf_size, size_t *data_size)
2000 const char *dissected_name = NULL;
2001 size_t dissected_len = 0;
2002 struct ext4_xattr_ref xattr_ref;
2003 struct ext4_inode_ref inode_ref;
2004 struct ext4_mountpoint *mp = ext4_get_mount(path);
2008 dissected_name = ext4_extract_xattr_name(name, name_len,
2009 &name_index, &dissected_len);
2014 r = ext4_generic_open2(&f, path, O_RDWR,
2015 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2021 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2025 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2028 ext4_fs_put_inode_ref(&inode_ref);
2032 r = ext4_fs_get_xattr(&xattr_ref, name_index,
2033 dissected_name, dissected_len,
2034 buf, buf_size, data_size);
2036 ext4_fs_put_xattr_ref(&xattr_ref);
2037 ext4_fs_put_inode_ref(&inode_ref);
2043 struct ext4_listxattr_iterator {
2048 bool list_too_small;
2049 bool get_required_size;
2052 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2053 struct ext4_xattr_item *item)
2055 struct ext4_listxattr_iterator *lxi;
2056 lxi = ref->iter_arg;
2057 if (!lxi->get_required_size) {
2060 prefix = ext4_get_xattr_name_prefix(item->name_index,
2063 prefix_len + item->name_len + 1 >
2066 lxi->list_too_small = 1;
2067 return EXT4_XATTR_ITERATE_STOP;
2070 memcpy(lxi->list_ptr, prefix, prefix_len);
2071 lxi->list_ptr += prefix_len;
2072 lxi->ret_size += prefix_len;
2074 memcpy(lxi->list_ptr, item->name, item->name_len);
2075 lxi->list_ptr[item->name_len] = 0;
2076 lxi->list_ptr += item->name_len + 1;
2078 lxi->ret_size += item->name_len + 1;
2079 return EXT4_XATTR_ITERATE_CONT;
2082 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2087 struct ext4_xattr_ref xattr_ref;
2088 struct ext4_inode_ref inode_ref;
2089 struct ext4_listxattr_iterator lxi;
2090 struct ext4_mountpoint *mp = ext4_get_mount(path);
2095 lxi.list_ptr = list;
2098 lxi.list_too_small = false;
2099 lxi.get_required_size = (!size) ? true : false;
2102 r = ext4_generic_open2(&f, path, O_RDWR,
2103 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2109 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2113 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2116 ext4_fs_put_inode_ref(&inode_ref);
2120 xattr_ref.iter_arg = &lxi;
2121 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2122 if (lxi.list_too_small)
2127 *ret_size = lxi.ret_size;
2130 ext4_fs_put_xattr_ref(&xattr_ref);
2131 ext4_fs_put_inode_ref(&inode_ref);
2138 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2144 const char *dissected_name = NULL;
2145 size_t dissected_len = 0;
2146 struct ext4_xattr_ref xattr_ref;
2147 struct ext4_inode_ref inode_ref;
2148 struct ext4_mountpoint *mp = ext4_get_mount(path);
2152 dissected_name = ext4_extract_xattr_name(name, name_len,
2153 &name_index, &dissected_len);
2158 r = ext4_generic_open2(&f, path, O_RDWR,
2159 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2165 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2169 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2172 ext4_fs_put_inode_ref(&inode_ref);
2176 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2179 ext4_fs_put_xattr_ref(&xattr_ref);
2180 ext4_fs_put_inode_ref(&inode_ref);
2187 /*********************************DIRECTORY OPERATION************************/
2189 int ext4_dir_rm(const char *path)
2195 struct ext4_mountpoint *mp = ext4_get_mount(path);
2196 struct ext4_inode_ref current;
2197 struct ext4_inode_ref child;
2198 struct ext4_directory_iterator it;
2202 uint32_t inode_current;
2215 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2222 len = ext4_path_check(path, &is_goal);
2224 inode_current = f.inode;
2227 ext4_block_cache_write_back(mp->fs.bdev, 1);
2230 /*Load directory node.*/
2231 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, ¤t);
2236 /*Initialize iterator.*/
2237 r = ext4_dir_iterator_init(&it, ¤t, 0);
2239 ext4_fs_put_inode_ref(¤t);
2250 /*Get up directory inode when ".." entry*/
2251 if ((it.current->name_length == 2) &&
2252 ext4_is_dots(it.current->name,
2253 it.current->name_length)) {
2254 inode_up = ext4_dir_entry_ll_get_inode(it.current);
2257 /*If directory or file entry, but not "." ".." entry*/
2258 if (!ext4_is_dots(it.current->name,
2259 it.current->name_length)) {
2261 /*Get child inode reference do unlink
2263 r = ext4_fs_get_inode_ref(&f.mp->fs,
2264 ext4_dir_entry_ll_get_inode(it.current),
2269 /*If directory with no leaf children*/
2270 r = ext4_has_children(&has_children, &child);
2272 ext4_fs_put_inode_ref(&child);
2277 /*Has directory children. Go into this
2279 inode_up = inode_current;
2280 inode_current = ext4_dir_entry_ll_get_inode(it.current);
2282 ext4_fs_put_inode_ref(&child);
2286 /*No children in child directory or file. Just
2288 r = ext4_unlink(f.mp, ¤t, &child,
2289 (char *)it.current->name,
2290 it.current->name_length);
2292 ext4_fs_put_inode_ref(&child);
2296 ext4_inode_set_deletion_time(child.inode,
2298 ext4_inode_set_links_count(child.inode, 0);
2301 r = ext4_fs_truncate_inode(&child, 0);
2303 ext4_fs_put_inode_ref(&child);
2307 r = ext4_fs_free_inode(&child);
2309 ext4_fs_put_inode_ref(&child);
2313 r = ext4_fs_put_inode_ref(&child);
2318 r = ext4_dir_iterator_next(&it);
2322 /*Directory iterator reached last entry*/
2323 ext4_has_children(&has_children, ¤t);
2324 if (!has_children) {
2325 inode_current = inode_up;
2332 struct ext4_inode_ref parent;
2333 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2338 /* In this place all directories should be
2340 * Last unlink from root of current directory*/
2341 r = ext4_unlink(f.mp, &parent, ¤t,
2344 ext4_fs_put_inode_ref(&parent);
2348 if (ext4_inode_get_links_count(current.inode) ==
2350 ext4_inode_set_deletion_time(
2351 current.inode, 0xFFFFFFFF);
2352 ext4_inode_set_links_count(
2354 current.dirty = true;
2356 r = ext4_fs_truncate_inode(¤t, 0);
2358 ext4_fs_put_inode_ref(&parent);
2362 r = ext4_fs_free_inode(¤t);
2364 ext4_fs_put_inode_ref(&parent);
2369 r = ext4_fs_put_inode_ref(&parent);
2376 ext4_dir_iterator_fini(&it);
2377 ext4_fs_put_inode_ref(¤t);
2380 /*When something goes wrong. End loop.*/
2386 ext4_block_cache_write_back(mp->fs.bdev, 0);
2391 int ext4_dir_mk(const char *path)
2396 struct ext4_mountpoint *mp = ext4_get_mount(path);
2404 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2406 /*Directory already created*/
2412 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2422 int ext4_dir_open(ext4_dir *d, const char *path)
2424 struct ext4_mountpoint *mp = ext4_get_mount(path);
2431 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2437 int ext4_dir_close(ext4_dir *d)
2439 return ext4_fclose(&d->f);
2442 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2444 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2447 ext4_direntry *de = 0;
2448 struct ext4_inode_ref dir;
2449 struct ext4_directory_iterator it;
2451 EXT4_MP_LOCK(d->f.mp);
2453 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2454 EXT4_MP_UNLOCK(d->f.mp);
2458 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2463 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2465 ext4_fs_put_inode_ref(&dir);
2469 memcpy(&d->de, it.current, sizeof(ext4_direntry));
2472 ext4_dir_iterator_next(&it);
2475 it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
2477 ext4_dir_iterator_fini(&it);
2478 ext4_fs_put_inode_ref(&dir);
2481 EXT4_MP_UNLOCK(d->f.mp);
2485 void ext4_dir_entry_rewind(ext4_dir *d)