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) &&
201 rc = ext4_dir_add_entry(child, ".", strlen("."), child);
203 ext4_dir_remove_entry(parent, name, strlen(name));
207 rc = ext4_dir_add_entry(child, "..", strlen(".."), parent);
209 ext4_dir_remove_entry(parent, name, strlen(name));
210 ext4_dir_remove_entry(child, ".", strlen("."));
214 /*New empty directory. Two links (. and ..) */
215 ext4_inode_set_links_count(child->inode, 2);
217 #if CONFIG_DIR_INDEX_ENABLE
218 /* Initialize directory index if supported */
219 if (ext4_sb_has_feature_compatible(
220 &mp->fs.sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
221 rc = ext4_dir_dx_init(child);
225 ext4_inode_set_flag(child->inode,
226 EXT4_INODE_FLAG_INDEX);
231 ext4_fs_inode_links_count_inc(parent);
233 parent->dirty = true;
236 * In case we want to rename a directory,
237 * we reset the original '..' pointer.
239 if (ext4_inode_is_type(&mp->fs.sb, child->inode,
240 EXT4_INODE_MODE_DIRECTORY)) {
242 ext4_inode_has_flag(child->inode,
243 EXT4_INODE_FLAG_INDEX);
244 struct ext4_directory_search_result result;
245 if (!has_flag_index) {
246 rc = ext4_dir_find_entry(&result,
252 ext4_dir_entry_ll_set_inode(result.dentry,
254 result.block.dirty = true;
255 rc = ext4_dir_destroy_result(child, &result);
260 #if CONFIG_DIR_INDEX_ENABLE
261 rc = ext4_dir_dx_reset_parent_inode(child,
269 ext4_fs_inode_links_count_inc(parent);
270 parent->dirty = true;
273 ext4_fs_inode_links_count_inc(child);
281 static int ext4_unlink(struct ext4_mountpoint *mp,
282 struct ext4_inode_ref *parent,
283 struct ext4_inode_ref *child_inode_ref, const char *name,
287 int rc = ext4_has_children(&has_children, child_inode_ref);
291 /* Cannot unlink non-empty node */
295 /* Remove entry from parent directory */
296 rc = ext4_dir_remove_entry(parent, name, name_len);
300 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
301 EXT4_INODE_MODE_DIRECTORY);
303 /* If directory - handle links from parent */
305 // ext4_assert(ext4_inode_get_links_count(child_inode_ref->inode)
307 ext4_fs_inode_links_count_dec(parent);
308 parent->dirty = true;
312 * TODO: Update timestamps of the parent
313 * (when we have wall-clock time).
315 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
316 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
317 * parent->dirty = true;
321 * TODO: Update timestamp for inode.
323 * ext4_inode_set_change_inode_time(child_inode_ref->inode,
326 if (ext4_inode_get_links_count(child_inode_ref->inode)) {
327 ext4_fs_inode_links_count_dec(child_inode_ref);
328 child_inode_ref->dirty = true;
334 /****************************************************************************/
336 int ext4_mount(const char *dev_name, const char *mount_point)
338 ext4_assert(mount_point && dev_name);
343 struct ext4_blockdev *bd = 0;
344 struct ext4_bcache *bc = 0;
345 struct ext4_mountpoint *mp = 0;
347 if (mount_point[strlen(mount_point) - 1] != '/')
350 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
351 if (_bdevices[i].name) {
352 if (!strcmp(dev_name, _bdevices[i].name)) {
353 bd = _bdevices[i].bd;
354 bc = _bdevices[i].bc;
363 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
364 if (!_mp[i].mounted) {
365 strcpy(_mp[i].name, mount_point);
371 if (!strcmp(_mp[i].name, mount_point))
378 r = ext4_block_init(bd);
382 r = ext4_fs_init(&mp->fs, bd);
388 bsize = ext4_sb_get_block_size(&mp->fs.sb);
389 ext4_block_set_lb_size(bd, bsize);
391 mp->cache_dynamic = 0;
394 /*Automatic block cache alloc.*/
395 mp->cache_dynamic = 1;
396 bc = malloc(sizeof(struct ext4_bcache));
398 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
407 if (bsize != bc->itemsize)
410 /*Bind block cache to block device*/
411 r = ext4_block_bind_bcache(bd, bc);
414 if (mp->cache_dynamic) {
415 ext4_bcache_fini_dynamic(bc);
424 int ext4_umount(const char *mount_point)
428 struct ext4_mountpoint *mp = 0;
430 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
431 if (!strcmp(_mp[i].name, mount_point)) {
440 r = ext4_fs_fini(&mp->fs);
446 if (mp->cache_dynamic) {
447 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
448 free(mp->fs.bdev->bc);
451 return ext4_block_fini(mp->fs.bdev);
454 static struct ext4_mountpoint *ext4_get_mount(const char *path)
457 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
462 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
468 int ext4_mount_point_stats(const char *mount_point,
469 struct ext4_mount_stats *stats)
471 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
477 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
478 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
479 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
480 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
481 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
483 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
484 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
485 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
487 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
493 int ext4_mount_setup_locks(const char *mount_point,
494 const struct ext4_lock *locks)
497 struct ext4_mountpoint *mp = 0;
499 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
500 if (!strcmp(_mp[i].name, mount_point)) {
508 mp->os_locks = locks;
512 /********************************FILE OPERATIONS*****************************/
514 static int ext4_path_check(const char *path, bool *is_goal)
518 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
520 if (path[i] == '/') {
534 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
539 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
540 *file_flags = O_RDONLY;
544 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
545 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
549 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
550 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
554 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
555 !strcmp(flags, "r+b")) {
556 *file_flags = O_RDWR;
560 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
561 !strcmp(flags, "w+b")) {
562 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
566 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
567 !strcmp(flags, "a+b")) {
568 *file_flags = O_RDWR | O_CREAT | O_APPEND;
576 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
577 * any filetype of the target dir entry will be accepted.
579 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
580 int filetype, uint32_t *parent_inode,
583 bool is_goal = false;
584 uint8_t inode_type = EXT4_DIRENTRY_UNKNOWN;
588 struct ext4_mountpoint *mp = ext4_get_mount(path);
589 struct ext4_directory_search_result result;
590 struct ext4_inode_ref ref;
600 path += strlen(mp->name);
603 *name_off = strlen(mp->name);
606 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
612 *parent_inode = ref.index;
614 int len = ext4_path_check(path, &is_goal);
618 len = ext4_path_check(path, &is_goal);
621 /*If root open was request.*/
623 ((filetype == EXT4_DIRENTRY_DIR) ||
624 (filetype == EXT4_DIRENTRY_UNKNOWN)))
631 r = ext4_dir_find_entry(&result, &ref, path, len);
634 /*Destroy last result*/
635 ext4_dir_destroy_result(&ref, &result);
640 if (!(f->flags & O_CREAT))
643 /*O_CREAT allows create new entry*/
644 struct ext4_inode_ref child_ref;
645 r = ext4_fs_alloc_inode(
648 : EXT4_DIRENTRY_DIR);
653 /*Link with root dir.*/
654 r = ext4_link(mp, &ref, &child_ref, path, len, false);
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_DIRENTRY_DIR && !is_goal) {
686 if (filetype != EXT4_DIRENTRY_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_DIRENTRY_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)
737 r = ext4_fs_put_inode_ref(&ref);
741 /****************************************************************************/
743 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
744 bool file_expect, uint32_t *parent_inode,
749 if (ext4_parse_flags(flags, &iflags) == false)
752 if (file_expect == true)
753 filetype = EXT4_DIRENTRY_REG_FILE;
755 filetype = EXT4_DIRENTRY_DIR;
757 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
761 static int __ext4_create_hardlink(const char *path,
762 struct ext4_inode_ref *child_ref,
765 bool is_goal = false;
766 uint8_t inode_type = EXT4_DIRENTRY_DIR;
770 struct ext4_mountpoint *mp = ext4_get_mount(path);
771 struct ext4_directory_search_result result;
772 struct ext4_inode_ref ref;
778 path += strlen(mp->name);
781 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
786 int len = ext4_path_check(path, &is_goal);
790 len = ext4_path_check(path, &is_goal);
793 /*If root open was request.*/
801 r = ext4_dir_find_entry(&result, &ref, path, len);
804 /*Destroy last result*/
805 ext4_dir_destroy_result(&ref, &result);
807 if (r != ENOENT || !is_goal)
810 /*Link with root dir.*/
811 r = ext4_link(mp, &ref, child_ref, path, len, rename);
813 } else if (r == EOK && is_goal) {
814 /*Destroy last result*/
815 ext4_dir_destroy_result(&ref, &result);
820 next_inode = result.dentry->inode;
822 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
824 r = ext4_dir_destroy_result(&ref, &result);
828 if (inode_type == EXT4_DIRENTRY_REG_FILE) {
837 r = ext4_fs_put_inode_ref(&ref);
841 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
852 ext4_fs_put_inode_ref(&ref);
856 r = ext4_fs_put_inode_ref(&ref);
860 static int ext4_remove_orig_reference(const char *path,
862 struct ext4_inode_ref *parent_ref,
863 struct ext4_inode_ref *child_ref)
868 struct ext4_mountpoint *mp = ext4_get_mount(path);
876 len = ext4_path_check(path, &is_goal);
878 /* Remove entry from parent directory */
879 r = ext4_dir_remove_entry(parent_ref, path, len);
883 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
884 EXT4_INODE_MODE_DIRECTORY)) {
885 ext4_fs_inode_links_count_dec(parent_ref);
886 parent_ref->dirty = true;
892 int ext4_flink(const char *path, const char *hardlink_path)
897 bool child_loaded = false;
898 uint32_t parent_inode, child_inode;
899 struct ext4_mountpoint *mp = ext4_get_mount(path);
900 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
901 struct ext4_inode_ref child_ref;
906 /* Will that happen? Anyway return EINVAL for such case. */
912 r = ext4_generic_open2(&f, path, O_RDONLY,
913 EXT4_DIRENTRY_UNKNOWN,
914 &parent_inode, &name_off);
918 child_inode = f.inode;
921 /*We have file to unlink. Load it.*/
922 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
928 /* Creating hardlink for directory is not allowed. */
929 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
930 EXT4_INODE_MODE_DIRECTORY)) {
935 r = __ext4_create_hardlink(hardlink_path, &child_ref, false);
939 ext4_fs_put_inode_ref(&child_ref);
946 int ext4_frename(const char *path, const char *new_path)
951 bool parent_loaded = false, child_loaded = false;
952 uint32_t parent_inode, child_inode;
953 struct ext4_mountpoint *mp = ext4_get_mount(path);
954 struct ext4_inode_ref child_ref, parent_ref;
961 r = ext4_generic_open2(&f, path, O_RDONLY,
962 EXT4_DIRENTRY_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,
989 &parent_ref, &child_ref);
995 ext4_fs_put_inode_ref(&parent_ref);
998 ext4_fs_put_inode_ref(&child_ref);
1005 /****************************************************************************/
1007 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1009 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1018 int ext4_cache_write_back(const char *path, bool on)
1020 struct ext4_mountpoint *mp = ext4_get_mount(path);
1026 ext4_block_cache_write_back(mp->fs.bdev, on);
1031 int ext4_fremove(const char *path)
1034 uint32_t parent_inode;
1039 struct ext4_inode_ref child;
1040 struct ext4_inode_ref parent;
1041 struct ext4_mountpoint *mp = ext4_get_mount(path);
1047 r = ext4_generic_open2(&f, path, O_RDWR,
1048 EXT4_DIRENTRY_UNKNOWN,
1049 &parent_inode, &name_off);
1056 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1062 /*We have file to delete. Load it.*/
1063 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1065 ext4_fs_put_inode_ref(&parent);
1073 len = ext4_path_check(path, &is_goal);
1075 /*Unlink from parent*/
1076 r = ext4_unlink(mp, &parent, &child, path, len);
1080 /*Link count is zero, the inode should be freed. */
1081 if (!ext4_inode_get_links_count(child.inode)) {
1082 ext4_inode_set_deletion_time(child.inode, 0xFFFFFFFF);
1084 ext4_block_cache_write_back(mp->fs.bdev, 1);
1085 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1086 r = ext4_fs_truncate_inode(&child, 0);
1087 ext4_block_cache_write_back(mp->fs.bdev, 0);
1092 r = ext4_fs_free_inode(&child);
1098 ext4_fs_put_inode_ref(&child);
1099 ext4_fs_put_inode_ref(&parent);
1104 int ext4_fill_raw_inode(const char *path,
1106 struct ext4_inode *inode)
1110 struct ext4_inode_ref inode_ref;
1111 struct ext4_mountpoint *mp = ext4_get_mount(path);
1119 r = ext4_generic_open2(&f, path, O_RDONLY,
1120 EXT4_DIRENTRY_UNKNOWN,
1131 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1137 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1139 ext4_fs_put_inode_ref(&inode_ref);
1148 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1150 struct ext4_mountpoint *mp = ext4_get_mount(path);
1157 ext4_block_cache_write_back(mp->fs.bdev, 1);
1158 r = ext4_generic_open(f, path, flags, true, 0, 0);
1159 ext4_block_cache_write_back(mp->fs.bdev, 0);
1164 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1166 struct ext4_mountpoint *mp = ext4_get_mount(path);
1173 filetype = EXT4_DIRENTRY_REG_FILE;
1176 ext4_block_cache_write_back(mp->fs.bdev, 1);
1177 r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
1178 ext4_block_cache_write_back(mp->fs.bdev, 0);
1183 int ext4_fclose(ext4_file *f)
1185 ext4_assert(f && f->mp);
1190 f->fpos = f->fsize = 0;
1195 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1197 struct ext4_inode_ref ref;
1201 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1203 EXT4_MP_UNLOCK(f->mp);
1208 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1209 if (f->fsize <= size) {
1214 /*Start write back cache mode.*/
1215 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1219 r = ext4_fs_truncate_inode(&ref, size);
1227 /*Stop write back cache mode*/
1228 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1234 ext4_fs_put_inode_ref(&ref);
1239 int ext4_ftruncate(ext4_file *f, uint64_t size)
1242 ext4_assert(f && f->mp);
1244 if (f->flags & O_RDONLY)
1247 EXT4_MP_LOCK(f->mp);
1249 r = ext4_ftruncate_no_lock(f, size);
1251 EXT4_MP_UNLOCK(f->mp);
1255 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1258 uint32_t iblock_idx;
1259 uint32_t iblock_last;
1260 uint32_t block_size;
1262 ext4_fsblk_t fblock;
1263 ext4_fsblk_t fblock_start;
1264 uint32_t fblock_count;
1266 uint8_t *u8_buf = buf;
1268 struct ext4_block b;
1269 struct ext4_inode_ref ref;
1271 ext4_assert(f && f->mp);
1273 if (f->flags & O_WRONLY)
1279 EXT4_MP_LOCK(f->mp);
1284 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1286 EXT4_MP_UNLOCK(f->mp);
1291 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1293 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1294 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1296 iblock_idx = (f->fpos) / block_size;
1297 iblock_last = (f->fpos + size) / block_size;
1298 unaligned = (f->fpos) % block_size;
1300 /*If the size of symlink is smaller than 60 bytes*/
1301 if (ext4_inode_is_type(&f->mp->fs.sb, ref.inode, EXT4_INODE_MODE_SOFTLINK)
1302 && f->fsize < sizeof(ref.inode->blocks)
1303 && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
1304 char *content = (char *)ref.inode->blocks;
1305 if (f->fpos < f->fsize) {
1306 r = (unaligned + size > f->fsize)
1307 ?(f->fsize - unaligned)
1309 memcpy(buf, content + unaligned, r);
1325 uint32_t l = size > (block_size - unaligned) ?
1326 (block_size - unaligned) : size;
1327 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1332 /* Do we get an unwritten range? */
1334 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1338 memcpy(u8_buf, b.data + unaligned, l);
1340 r = ext4_block_set(f->mp->fs.bdev, &b);
1345 memset(u8_buf, 0, l);
1360 while (size >= block_size) {
1361 while (iblock_idx < iblock_last) {
1362 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1370 fblock_start = fblock;
1372 if ((fblock_start + fblock_count) != fblock)
1378 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1383 size -= block_size * fblock_count;
1384 u8_buf += block_size * fblock_count;
1385 f->fpos += block_size * fblock_count;
1388 *rcnt += block_size * fblock_count;
1390 fblock_start = fblock;
1395 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1400 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1404 memcpy(u8_buf, b.data, size);
1406 r = ext4_block_set(f->mp->fs.bdev, &b);
1417 ext4_fs_put_inode_ref(&ref);
1418 EXT4_MP_UNLOCK(f->mp);
1422 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1425 uint32_t iblock_idx;
1426 uint32_t iblock_last;
1427 uint32_t ifile_blocks;
1428 uint32_t block_size;
1430 uint32_t fblock_count;
1431 ext4_fsblk_t fblock;
1432 ext4_fsblk_t fblock_start;
1434 struct ext4_block b;
1435 struct ext4_inode_ref ref;
1436 const uint8_t *u8_buf = buf;
1439 ext4_assert(f && f->mp);
1441 if (f->flags & O_RDONLY)
1447 EXT4_MP_LOCK(f->mp);
1452 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1454 EXT4_MP_UNLOCK(f->mp);
1459 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1460 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1462 iblock_last = (f->fpos + size) / block_size;
1463 iblock_idx = (f->fpos) / block_size;
1464 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1466 unaligned = (f->fpos) % block_size;
1469 uint32_t l = size > (block_size - unaligned) ?
1470 (block_size - unaligned) : size;
1471 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx, &fblock);
1475 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1479 memcpy(b.data + unaligned, u8_buf, l);
1482 r = ext4_block_set(f->mp->fs.bdev, &b);
1496 /*Start write back cache mode.*/
1497 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1503 while (size >= block_size) {
1505 while (iblock_idx < iblock_last) {
1506 if (iblock_idx < ifile_blocks) {
1507 r = ext4_fs_init_inode_data_block_index(
1508 &ref, iblock_idx, &fblock);
1512 rr = ext4_fs_append_inode_block(&ref, &fblock,
1515 /* Unable to append more blocks. But some
1516 * block might be allocated already.
1524 if (!fblock_start) {
1525 fblock_start = fblock;
1528 if ((fblock_start + fblock_count) != fblock)
1534 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1539 size -= block_size * fblock_count;
1540 u8_buf += block_size * fblock_count;
1541 f->fpos += block_size * fblock_count;
1544 *wcnt += block_size * fblock_count;
1546 fblock_start = fblock;
1550 /*ext4_fs_append_inode_block has failed and no
1551 * more blocks might be written. But node size
1552 * should be updated.*/
1558 /*Stop write back cache mode*/
1559 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1565 if (iblock_idx < ifile_blocks) {
1566 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx,
1571 r = ext4_fs_append_inode_block(&ref, &fblock, &iblock_idx);
1573 /*Node size sholud be updated.*/
1577 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1581 memcpy(b.data, u8_buf, size);
1584 r = ext4_block_set(f->mp->fs.bdev, &b);
1595 if (f->fpos > f->fsize) {
1597 ext4_inode_set_size(ref.inode, f->fsize);
1602 ext4_fs_put_inode_ref(&ref);
1603 EXT4_MP_UNLOCK(f->mp);
1607 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1611 if (offset > f->fsize)
1617 if ((offset + f->fpos) > f->fsize)
1623 if (offset > f->fsize)
1626 f->fpos = f->fsize - offset;
1632 uint64_t ext4_ftell(ext4_file *f) { return f->fpos; }
1634 uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
1636 int ext4_chmod(const char *path, uint32_t mode)
1641 struct ext4_sblock *sb;
1642 struct ext4_inode_ref inode_ref;
1643 struct ext4_mountpoint *mp = ext4_get_mount(path);
1650 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1658 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1664 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1665 inode_ref.dirty = true;
1667 ext4_fs_put_inode_ref(&inode_ref);
1672 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1677 struct ext4_inode_ref inode_ref;
1678 struct ext4_mountpoint *mp = ext4_get_mount(path);
1685 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1692 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1698 ext4_inode_set_uid(inode_ref.inode, uid);
1699 ext4_inode_set_gid(inode_ref.inode, gid);
1700 inode_ref.dirty = true;
1702 ext4_fs_put_inode_ref(&inode_ref);
1707 int ext4_file_set_atime(const char *path, uint32_t atime)
1712 struct ext4_inode_ref inode_ref;
1713 struct ext4_mountpoint *mp = ext4_get_mount(path);
1720 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1727 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1733 ext4_inode_set_access_time(inode_ref.inode, atime);
1734 inode_ref.dirty = true;
1736 ext4_fs_put_inode_ref(&inode_ref);
1741 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1746 struct ext4_inode_ref inode_ref;
1747 struct ext4_mountpoint *mp = ext4_get_mount(path);
1754 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1761 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1767 ext4_inode_set_modification_time(inode_ref.inode, mtime);
1768 inode_ref.dirty = true;
1770 ext4_fs_put_inode_ref(&inode_ref);
1775 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1780 struct ext4_inode_ref inode_ref;
1781 struct ext4_mountpoint *mp = ext4_get_mount(path);
1788 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1795 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1801 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1802 inode_ref.dirty = true;
1804 ext4_fs_put_inode_ref(&inode_ref);
1809 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1811 struct ext4_block b;
1812 struct ext4_inode_ref ref;
1814 ext4_fsblk_t fblock;
1815 uint32_t block_size;
1818 ext4_assert(f && f->mp);
1823 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1825 EXT4_MP_UNLOCK(f->mp);
1830 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1831 if (size > block_size) {
1835 r = ext4_ftruncate_no_lock(f, 0);
1839 /*Start write back cache mode.*/
1840 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1844 /*If the size of symlink is smaller than 60 bytes*/
1845 if (size < sizeof(ref.inode->blocks)) {
1846 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1847 memcpy(ref.inode->blocks, buf, size);
1848 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1850 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1851 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1855 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1859 memcpy(b.data, buf, size);
1861 r = ext4_block_set(f->mp->fs.bdev, &b);
1866 /*Stop write back cache mode*/
1867 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1872 ext4_inode_set_size(ref.inode, size);
1880 ext4_fs_put_inode_ref(&ref);
1884 int ext4_fsymlink(const char *target, const char *path)
1886 struct ext4_mountpoint *mp = ext4_get_mount(path);
1894 filetype = EXT4_DIRENTRY_SYMLINK;
1897 ext4_block_cache_write_back(mp->fs.bdev, 1);
1898 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
1900 r = ext4_fsymlink_set(&f, target, strlen(target));
1907 ext4_block_cache_write_back(mp->fs.bdev, 0);
1912 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1914 struct ext4_mountpoint *mp = ext4_get_mount(path);
1925 filetype = EXT4_DIRENTRY_SYMLINK;
1928 ext4_block_cache_write_back(mp->fs.bdev, 1);
1929 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, 0, 0);
1931 r = ext4_fread(&f, buf, bufsize, rcnt);
1938 ext4_block_cache_write_back(mp->fs.bdev, 0);
1943 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1944 const void *data, size_t data_size, bool replace)
1950 const char *dissected_name = NULL;
1951 size_t dissected_len = 0;
1952 struct ext4_xattr_ref xattr_ref;
1953 struct ext4_inode_ref inode_ref;
1954 struct ext4_mountpoint *mp = ext4_get_mount(path);
1958 dissected_name = ext4_extract_xattr_name(name, name_len,
1959 &name_index, &dissected_len);
1964 r = ext4_generic_open2(&f, path, O_RDWR,
1965 EXT4_DIRENTRY_UNKNOWN, 0, 0);
1971 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
1975 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
1978 ext4_fs_put_inode_ref(&inode_ref);
1982 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
1983 dissected_len, data, data_size, replace);
1985 ext4_fs_put_xattr_ref(&xattr_ref);
1986 ext4_fs_put_inode_ref(&inode_ref);
1992 int ext4_getxattr(const char *path, const char *name, size_t name_len,
1993 void *buf, size_t buf_size, size_t *data_size)
1999 const char *dissected_name = NULL;
2000 size_t dissected_len = 0;
2001 struct ext4_xattr_ref xattr_ref;
2002 struct ext4_inode_ref inode_ref;
2003 struct ext4_mountpoint *mp = ext4_get_mount(path);
2007 dissected_name = ext4_extract_xattr_name(name, name_len,
2008 &name_index, &dissected_len);
2013 r = ext4_generic_open2(&f, path, O_RDWR,
2014 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2020 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2024 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2027 ext4_fs_put_inode_ref(&inode_ref);
2031 r = ext4_fs_get_xattr(&xattr_ref, name_index,
2032 dissected_name, dissected_len,
2033 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,
2062 prefix_len + item->name_len + 1 >
2065 lxi->list_too_small = 1;
2066 return EXT4_XATTR_ITERATE_STOP;
2069 memcpy(lxi->list_ptr, prefix, prefix_len);
2070 lxi->list_ptr += prefix_len;
2071 lxi->ret_size += prefix_len;
2073 memcpy(lxi->list_ptr, item->name, item->name_len);
2074 lxi->list_ptr[item->name_len] = 0;
2075 lxi->list_ptr += item->name_len + 1;
2077 lxi->ret_size += item->name_len + 1;
2078 return EXT4_XATTR_ITERATE_CONT;
2081 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2086 struct ext4_xattr_ref xattr_ref;
2087 struct ext4_inode_ref inode_ref;
2088 struct ext4_listxattr_iterator lxi;
2089 struct ext4_mountpoint *mp = ext4_get_mount(path);
2094 lxi.list_ptr = list;
2097 lxi.list_too_small = false;
2098 lxi.get_required_size = (!size) ? true : false;
2101 r = ext4_generic_open2(&f, path, O_RDWR,
2102 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2108 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2112 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2115 ext4_fs_put_inode_ref(&inode_ref);
2119 xattr_ref.iter_arg = &lxi;
2120 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2121 if (lxi.list_too_small)
2126 *ret_size = lxi.ret_size;
2129 ext4_fs_put_xattr_ref(&xattr_ref);
2130 ext4_fs_put_inode_ref(&inode_ref);
2137 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2143 const char *dissected_name = NULL;
2144 size_t dissected_len = 0;
2145 struct ext4_xattr_ref xattr_ref;
2146 struct ext4_inode_ref inode_ref;
2147 struct ext4_mountpoint *mp = ext4_get_mount(path);
2151 dissected_name = ext4_extract_xattr_name(name, name_len,
2152 &name_index, &dissected_len);
2157 r = ext4_generic_open2(&f, path, O_RDWR,
2158 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2164 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2168 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2171 ext4_fs_put_inode_ref(&inode_ref);
2175 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2178 ext4_fs_put_xattr_ref(&xattr_ref);
2179 ext4_fs_put_inode_ref(&inode_ref);
2186 /*********************************DIRECTORY OPERATION************************/
2188 int ext4_dir_rm(const char *path)
2194 struct ext4_mountpoint *mp = ext4_get_mount(path);
2195 struct ext4_inode_ref current;
2196 struct ext4_inode_ref child;
2197 struct ext4_directory_iterator it;
2201 uint32_t inode_current;
2214 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2221 len = ext4_path_check(path, &is_goal);
2223 inode_current = f.inode;
2226 ext4_block_cache_write_back(mp->fs.bdev, 1);
2229 /*Load directory node.*/
2230 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, ¤t);
2235 /*Initialize iterator.*/
2236 r = ext4_dir_iterator_init(&it, ¤t, 0);
2238 ext4_fs_put_inode_ref(¤t);
2249 /*Get up directory inode when ".." entry*/
2250 if ((it.current->name_length == 2) &&
2251 ext4_is_dots(it.current->name,
2252 it.current->name_length)) {
2253 inode_up = ext4_dir_entry_ll_get_inode(it.current);
2256 /*If directory or file entry, but not "." ".." entry*/
2257 if (!ext4_is_dots(it.current->name,
2258 it.current->name_length)) {
2260 /*Get child inode reference do unlink
2262 r = ext4_fs_get_inode_ref(&f.mp->fs,
2263 ext4_dir_entry_ll_get_inode(it.current),
2268 /*If directory with no leaf children*/
2269 r = ext4_has_children(&has_children, &child);
2271 ext4_fs_put_inode_ref(&child);
2276 /*Has directory children. Go into this
2278 inode_up = inode_current;
2279 inode_current = ext4_dir_entry_ll_get_inode(it.current);
2281 ext4_fs_put_inode_ref(&child);
2285 /*No children in child directory or file. Just
2287 r = ext4_unlink(f.mp, ¤t, &child,
2288 (char *)it.current->name,
2289 it.current->name_length);
2291 ext4_fs_put_inode_ref(&child);
2295 ext4_inode_set_deletion_time(child.inode,
2297 ext4_inode_set_links_count(child.inode, 0);
2300 r = ext4_fs_truncate_inode(&child, 0);
2302 ext4_fs_put_inode_ref(&child);
2306 r = ext4_fs_free_inode(&child);
2308 ext4_fs_put_inode_ref(&child);
2312 r = ext4_fs_put_inode_ref(&child);
2317 r = ext4_dir_iterator_next(&it);
2321 /*Directory iterator reached last entry*/
2322 ext4_has_children(&has_children, ¤t);
2323 if (!has_children) {
2324 inode_current = inode_up;
2331 struct ext4_inode_ref parent;
2332 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2337 /* In this place all directories should be
2339 * Last unlink from root of current directory*/
2340 r = ext4_unlink(f.mp, &parent, ¤t,
2343 ext4_fs_put_inode_ref(&parent);
2347 if (ext4_inode_get_links_count(current.inode) ==
2349 ext4_inode_set_deletion_time(
2350 current.inode, 0xFFFFFFFF);
2351 ext4_inode_set_links_count(
2353 current.dirty = true;
2355 r = ext4_fs_truncate_inode(¤t, 0);
2357 ext4_fs_put_inode_ref(&parent);
2361 r = ext4_fs_free_inode(¤t);
2363 ext4_fs_put_inode_ref(&parent);
2368 r = ext4_fs_put_inode_ref(&parent);
2375 ext4_dir_iterator_fini(&it);
2376 ext4_fs_put_inode_ref(¤t);
2379 /*When something goes wrong. End loop.*/
2385 ext4_block_cache_write_back(mp->fs.bdev, 0);
2390 int ext4_dir_mk(const char *path)
2395 struct ext4_mountpoint *mp = ext4_get_mount(path);
2403 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2405 /*Directory already created*/
2411 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2421 int ext4_dir_open(ext4_dir *d, const char *path)
2423 struct ext4_mountpoint *mp = ext4_get_mount(path);
2430 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2436 int ext4_dir_close(ext4_dir *d)
2438 return ext4_fclose(&d->f);
2441 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2443 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2446 ext4_direntry *de = 0;
2447 struct ext4_inode_ref dir;
2448 struct ext4_directory_iterator it;
2450 EXT4_MP_LOCK(d->f.mp);
2452 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2453 EXT4_MP_UNLOCK(d->f.mp);
2457 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2462 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2464 ext4_fs_put_inode_ref(&dir);
2468 memcpy(&d->de, it.current, sizeof(ext4_direntry));
2471 ext4_dir_iterator_next(&it);
2474 it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
2476 ext4_dir_iterator_fini(&it);
2477 ext4_fs_put_inode_ref(&dir);
2480 EXT4_MP_UNLOCK(d->f.mp);
2484 void ext4_dir_entry_rewind(ext4_dir *d)