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_has_feature_compatible(
205 &mp->fs.sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
206 rc = ext4_dir_dx_init(child);
210 ext4_inode_set_flag(child->inode,
211 EXT4_INODE_FLAG_INDEX);
216 rc = ext4_dir_add_entry(child, ".", strlen("."), child);
218 ext4_dir_remove_entry(parent, name, strlen(name));
222 rc = ext4_dir_add_entry(child, "..", strlen(".."), parent);
224 ext4_dir_remove_entry(parent, name, strlen(name));
225 ext4_dir_remove_entry(child, ".", strlen("."));
230 /*New empty directory. Two links (. and ..) */
231 ext4_inode_set_links_count(child->inode, 2);
233 ext4_fs_inode_links_count_inc(parent);
235 parent->dirty = true;
238 * In case we want to rename a directory,
239 * we reset the original '..' pointer.
241 if (ext4_inode_is_type(&mp->fs.sb, child->inode,
242 EXT4_INODE_MODE_DIRECTORY)) {
244 ext4_inode_has_flag(child->inode,
245 EXT4_INODE_FLAG_INDEX);
246 struct ext4_directory_search_result result;
247 if (!has_flag_index) {
248 rc = ext4_dir_find_entry(&result,
254 ext4_dir_entry_ll_set_inode(result.dentry,
256 result.block.dirty = true;
257 rc = ext4_dir_destroy_result(child, &result);
262 #if CONFIG_DIR_INDEX_ENABLE
263 rc = ext4_dir_dx_reset_parent_inode(child,
271 ext4_fs_inode_links_count_inc(parent);
272 parent->dirty = true;
275 ext4_fs_inode_links_count_inc(child);
283 static int ext4_unlink(struct ext4_mountpoint *mp,
284 struct ext4_inode_ref *parent,
285 struct ext4_inode_ref *child_inode_ref, const char *name,
289 int rc = ext4_has_children(&has_children, child_inode_ref);
293 /* Cannot unlink non-empty node */
297 /* Remove entry from parent directory */
298 rc = ext4_dir_remove_entry(parent, name, name_len);
302 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
303 EXT4_INODE_MODE_DIRECTORY);
305 /* If directory - handle links from parent */
307 // ext4_assert(ext4_inode_get_links_count(child_inode_ref->inode)
309 ext4_fs_inode_links_count_dec(parent);
310 parent->dirty = true;
314 * TODO: Update timestamps of the parent
315 * (when we have wall-clock time).
317 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
318 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
319 * parent->dirty = true;
323 * TODO: Update timestamp for inode.
325 * ext4_inode_set_change_inode_time(child_inode_ref->inode,
328 if (ext4_inode_get_links_count(child_inode_ref->inode)) {
329 ext4_fs_inode_links_count_dec(child_inode_ref);
330 child_inode_ref->dirty = true;
336 /****************************************************************************/
338 int ext4_mount(const char *dev_name, const char *mount_point)
340 ext4_assert(mount_point && dev_name);
345 struct ext4_blockdev *bd = 0;
346 struct ext4_bcache *bc = 0;
347 struct ext4_mountpoint *mp = 0;
349 if (mount_point[strlen(mount_point) - 1] != '/')
352 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
353 if (_bdevices[i].name) {
354 if (!strcmp(dev_name, _bdevices[i].name)) {
355 bd = _bdevices[i].bd;
356 bc = _bdevices[i].bc;
365 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
366 if (!_mp[i].mounted) {
367 strcpy(_mp[i].name, mount_point);
373 if (!strcmp(_mp[i].name, mount_point))
380 r = ext4_block_init(bd);
384 r = ext4_fs_init(&mp->fs, bd);
390 bsize = ext4_sb_get_block_size(&mp->fs.sb);
391 ext4_block_set_lb_size(bd, bsize);
393 mp->cache_dynamic = 0;
396 /*Automatic block cache alloc.*/
397 mp->cache_dynamic = 1;
398 bc = malloc(sizeof(struct ext4_bcache));
400 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
409 if (bsize != bc->itemsize)
412 /*Bind block cache to block device*/
413 r = ext4_block_bind_bcache(bd, bc);
416 if (mp->cache_dynamic) {
417 ext4_bcache_fini_dynamic(bc);
426 int ext4_umount(const char *mount_point)
430 struct ext4_mountpoint *mp = 0;
432 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
433 if (!strcmp(_mp[i].name, mount_point)) {
442 r = ext4_fs_fini(&mp->fs);
448 if (mp->cache_dynamic) {
449 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
450 free(mp->fs.bdev->bc);
453 return ext4_block_fini(mp->fs.bdev);
456 static struct ext4_mountpoint *ext4_get_mount(const char *path)
459 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
464 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
470 int ext4_mount_point_stats(const char *mount_point,
471 struct ext4_mount_stats *stats)
473 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
479 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
480 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
481 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
482 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
483 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
485 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
486 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
487 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
489 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
495 int ext4_mount_setup_locks(const char *mount_point,
496 const struct ext4_lock *locks)
499 struct ext4_mountpoint *mp = 0;
501 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
502 if (!strcmp(_mp[i].name, mount_point)) {
510 mp->os_locks = locks;
514 /********************************FILE OPERATIONS*****************************/
516 static int ext4_path_check(const char *path, bool *is_goal)
520 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
522 if (path[i] == '/') {
536 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
541 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
542 *file_flags = O_RDONLY;
546 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
547 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
551 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
552 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
556 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
557 !strcmp(flags, "r+b")) {
558 *file_flags = O_RDWR;
562 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
563 !strcmp(flags, "w+b")) {
564 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
568 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
569 !strcmp(flags, "a+b")) {
570 *file_flags = O_RDWR | O_CREAT | O_APPEND;
578 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
579 * any filetype of the target dir entry will be accepted.
581 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
582 int filetype, uint32_t *parent_inode,
585 bool is_goal = false;
586 uint8_t inode_type = EXT4_DIRENTRY_UNKNOWN;
590 struct ext4_mountpoint *mp = ext4_get_mount(path);
591 struct ext4_directory_search_result result;
592 struct ext4_inode_ref ref;
602 path += strlen(mp->name);
605 *name_off = strlen(mp->name);
608 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
614 *parent_inode = ref.index;
616 int len = ext4_path_check(path, &is_goal);
620 len = ext4_path_check(path, &is_goal);
623 /*If root open was request.*/
625 ((filetype == EXT4_DIRENTRY_DIR) ||
626 (filetype == EXT4_DIRENTRY_UNKNOWN)))
633 r = ext4_dir_find_entry(&result, &ref, path, len);
636 /*Destroy last result*/
637 ext4_dir_destroy_result(&ref, &result);
642 if (!(f->flags & O_CREAT))
645 /*O_CREAT allows create new entry*/
646 struct ext4_inode_ref child_ref;
647 r = ext4_fs_alloc_inode(
650 : EXT4_DIRENTRY_DIR);
655 /*Link with root dir.*/
656 r = ext4_link(mp, &ref, &child_ref, path, len, false);
658 /*Fail. Free new inode.*/
659 ext4_fs_free_inode(&child_ref);
660 /*We do not want to write new inode.
661 But block has to be released.*/
662 child_ref.dirty = false;
663 ext4_fs_put_inode_ref(&child_ref);
667 ext4_fs_put_inode_ref(&child_ref);
673 *parent_inode = ref.index;
675 next_inode = ext4_dir_entry_ll_get_inode(result.dentry);
677 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
679 r = ext4_dir_destroy_result(&ref, &result);
683 /*If expected file error*/
684 if (inode_type != EXT4_DIRENTRY_DIR && !is_goal) {
688 if (filetype != EXT4_DIRENTRY_UNKNOWN) {
689 if ((inode_type != filetype) && is_goal) {
695 r = ext4_fs_put_inode_ref(&ref);
699 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
709 *name_off += len + 1;
713 ext4_fs_put_inode_ref(&ref);
719 if ((f->flags & O_TRUNC) &&
720 (inode_type == EXT4_DIRENTRY_REG_FILE)) {
722 r = ext4_fs_truncate_inode(&ref, 0);
724 ext4_fs_put_inode_ref(&ref);
730 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
731 f->inode = ref.index;
734 if (f->flags & O_APPEND)
739 r = ext4_fs_put_inode_ref(&ref);
743 /****************************************************************************/
745 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
746 bool file_expect, uint32_t *parent_inode,
751 if (ext4_parse_flags(flags, &iflags) == false)
754 if (file_expect == true)
755 filetype = EXT4_DIRENTRY_REG_FILE;
757 filetype = EXT4_DIRENTRY_DIR;
759 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
763 static int __ext4_create_hardlink(const char *path,
764 struct ext4_inode_ref *child_ref,
767 bool is_goal = false;
768 uint8_t inode_type = EXT4_DIRENTRY_DIR;
772 struct ext4_mountpoint *mp = ext4_get_mount(path);
773 struct ext4_directory_search_result result;
774 struct ext4_inode_ref ref;
780 path += strlen(mp->name);
783 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
788 int len = ext4_path_check(path, &is_goal);
792 len = ext4_path_check(path, &is_goal);
795 /*If root open was request.*/
803 r = ext4_dir_find_entry(&result, &ref, path, len);
806 /*Destroy last result*/
807 ext4_dir_destroy_result(&ref, &result);
809 if (r != ENOENT || !is_goal)
812 /*Link with root dir.*/
813 r = ext4_link(mp, &ref, child_ref, path, len, rename);
815 } else if (r == EOK && is_goal) {
816 /*Destroy last result*/
817 ext4_dir_destroy_result(&ref, &result);
822 next_inode = result.dentry->inode;
824 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
826 r = ext4_dir_destroy_result(&ref, &result);
830 if (inode_type == EXT4_DIRENTRY_REG_FILE) {
839 r = ext4_fs_put_inode_ref(&ref);
843 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
854 ext4_fs_put_inode_ref(&ref);
858 r = ext4_fs_put_inode_ref(&ref);
862 static int ext4_remove_orig_reference(const char *path,
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,
915 EXT4_DIRENTRY_UNKNOWN,
916 &parent_inode, &name_off);
920 child_inode = f.inode;
923 /*We have file to unlink. Load it.*/
924 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
930 /* Creating hardlink for directory is not allowed. */
931 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
932 EXT4_INODE_MODE_DIRECTORY)) {
937 r = __ext4_create_hardlink(hardlink_path, &child_ref, false);
941 ext4_fs_put_inode_ref(&child_ref);
948 int ext4_frename(const char *path, const char *new_path)
953 bool parent_loaded = false, child_loaded = false;
954 uint32_t parent_inode, child_inode;
955 struct ext4_mountpoint *mp = ext4_get_mount(path);
956 struct ext4_inode_ref child_ref, parent_ref;
963 r = ext4_generic_open2(&f, path, O_RDONLY,
964 EXT4_DIRENTRY_UNKNOWN,
965 &parent_inode, &name_off);
969 child_inode = f.inode;
973 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
977 parent_loaded = true;
979 /*We have file to unlink. Load it.*/
980 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
986 r = __ext4_create_hardlink(new_path, &child_ref, true);
990 r = ext4_remove_orig_reference(path, name_off,
991 &parent_ref, &child_ref);
997 ext4_fs_put_inode_ref(&parent_ref);
1000 ext4_fs_put_inode_ref(&child_ref);
1007 /****************************************************************************/
1009 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1011 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1020 int ext4_cache_write_back(const char *path, bool on)
1022 struct ext4_mountpoint *mp = ext4_get_mount(path);
1028 ext4_block_cache_write_back(mp->fs.bdev, on);
1033 int ext4_fremove(const char *path)
1036 uint32_t parent_inode;
1041 struct ext4_inode_ref child;
1042 struct ext4_inode_ref parent;
1043 struct ext4_mountpoint *mp = ext4_get_mount(path);
1049 r = ext4_generic_open2(&f, path, O_RDWR,
1050 EXT4_DIRENTRY_UNKNOWN,
1051 &parent_inode, &name_off);
1058 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1064 /*We have file to delete. Load it.*/
1065 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1067 ext4_fs_put_inode_ref(&parent);
1075 len = ext4_path_check(path, &is_goal);
1077 /*Unlink from parent*/
1078 r = ext4_unlink(mp, &parent, &child, path, len);
1082 /*Link count is zero, the inode should be freed. */
1083 if (!ext4_inode_get_links_count(child.inode)) {
1084 ext4_inode_set_deletion_time(child.inode, 0xFFFFFFFF);
1086 ext4_block_cache_write_back(mp->fs.bdev, 1);
1087 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1088 r = ext4_fs_truncate_inode(&child, 0);
1089 ext4_block_cache_write_back(mp->fs.bdev, 0);
1094 r = ext4_fs_free_inode(&child);
1100 ext4_fs_put_inode_ref(&child);
1101 ext4_fs_put_inode_ref(&parent);
1106 int ext4_fill_raw_inode(const char *path,
1108 struct ext4_inode *inode)
1112 struct ext4_inode_ref inode_ref;
1113 struct ext4_mountpoint *mp = ext4_get_mount(path);
1121 r = ext4_generic_open2(&f, path, O_RDONLY,
1122 EXT4_DIRENTRY_UNKNOWN,
1133 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1139 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1141 ext4_fs_put_inode_ref(&inode_ref);
1150 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1152 struct ext4_mountpoint *mp = ext4_get_mount(path);
1159 ext4_block_cache_write_back(mp->fs.bdev, 1);
1160 r = ext4_generic_open(f, path, flags, true, 0, 0);
1161 ext4_block_cache_write_back(mp->fs.bdev, 0);
1166 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1168 struct ext4_mountpoint *mp = ext4_get_mount(path);
1175 filetype = EXT4_DIRENTRY_REG_FILE;
1178 ext4_block_cache_write_back(mp->fs.bdev, 1);
1179 r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
1180 ext4_block_cache_write_back(mp->fs.bdev, 0);
1185 int ext4_fclose(ext4_file *f)
1187 ext4_assert(f && f->mp);
1192 f->fpos = f->fsize = 0;
1197 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1199 struct ext4_inode_ref ref;
1203 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1205 EXT4_MP_UNLOCK(f->mp);
1210 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1211 if (f->fsize <= size) {
1216 /*Start write back cache mode.*/
1217 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1221 r = ext4_fs_truncate_inode(&ref, size);
1229 /*Stop write back cache mode*/
1230 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1236 ext4_fs_put_inode_ref(&ref);
1241 int ext4_ftruncate(ext4_file *f, uint64_t size)
1244 ext4_assert(f && f->mp);
1246 if (f->flags & O_RDONLY)
1249 EXT4_MP_LOCK(f->mp);
1251 r = ext4_ftruncate_no_lock(f, size);
1253 EXT4_MP_UNLOCK(f->mp);
1257 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1260 uint32_t iblock_idx;
1261 uint32_t iblock_last;
1262 uint32_t block_size;
1264 ext4_fsblk_t fblock;
1265 ext4_fsblk_t fblock_start;
1266 uint32_t fblock_count;
1268 uint8_t *u8_buf = buf;
1270 struct ext4_block b;
1271 struct ext4_inode_ref ref;
1273 ext4_assert(f && f->mp);
1275 if (f->flags & O_WRONLY)
1281 EXT4_MP_LOCK(f->mp);
1286 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1288 EXT4_MP_UNLOCK(f->mp);
1293 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1295 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1296 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1298 iblock_idx = (f->fpos) / block_size;
1299 iblock_last = (f->fpos + size) / block_size;
1300 unaligned = (f->fpos) % block_size;
1302 /*If the size of symlink is smaller than 60 bytes*/
1303 if (ext4_inode_is_type(&f->mp->fs.sb, ref.inode, EXT4_INODE_MODE_SOFTLINK)
1304 && f->fsize < sizeof(ref.inode->blocks)
1305 && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
1306 char *content = (char *)ref.inode->blocks;
1307 if (f->fpos < f->fsize) {
1308 r = (unaligned + size > f->fsize)
1309 ?(f->fsize - unaligned)
1311 memcpy(buf, content + unaligned, r);
1327 uint32_t l = size > (block_size - unaligned) ?
1328 (block_size - unaligned) : size;
1329 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1334 /* Do we get an unwritten range? */
1336 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1340 memcpy(u8_buf, b.data + unaligned, l);
1342 r = ext4_block_set(f->mp->fs.bdev, &b);
1347 memset(u8_buf, 0, l);
1362 while (size >= block_size) {
1363 while (iblock_idx < iblock_last) {
1364 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1372 fblock_start = fblock;
1374 if ((fblock_start + fblock_count) != fblock)
1380 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1385 size -= block_size * fblock_count;
1386 u8_buf += block_size * fblock_count;
1387 f->fpos += block_size * fblock_count;
1390 *rcnt += block_size * fblock_count;
1392 fblock_start = fblock;
1397 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1402 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1406 memcpy(u8_buf, b.data, size);
1408 r = ext4_block_set(f->mp->fs.bdev, &b);
1419 ext4_fs_put_inode_ref(&ref);
1420 EXT4_MP_UNLOCK(f->mp);
1424 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1427 uint32_t iblock_idx;
1428 uint32_t iblock_last;
1429 uint32_t ifile_blocks;
1430 uint32_t block_size;
1432 uint32_t fblock_count;
1433 ext4_fsblk_t fblock;
1434 ext4_fsblk_t fblock_start;
1436 struct ext4_block b;
1437 struct ext4_inode_ref ref;
1438 const uint8_t *u8_buf = buf;
1441 ext4_assert(f && f->mp);
1443 if (f->flags & O_RDONLY)
1449 EXT4_MP_LOCK(f->mp);
1454 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1456 EXT4_MP_UNLOCK(f->mp);
1461 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1462 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1464 iblock_last = (f->fpos + size) / block_size;
1465 iblock_idx = (f->fpos) / block_size;
1466 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1468 unaligned = (f->fpos) % block_size;
1471 uint32_t l = size > (block_size - unaligned) ?
1472 (block_size - unaligned) : size;
1473 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx, &fblock);
1477 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1481 memcpy(b.data + unaligned, u8_buf, l);
1484 r = ext4_block_set(f->mp->fs.bdev, &b);
1498 /*Start write back cache mode.*/
1499 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1505 while (size >= block_size) {
1507 while (iblock_idx < iblock_last) {
1508 if (iblock_idx < ifile_blocks) {
1509 r = ext4_fs_init_inode_data_block_index(
1510 &ref, iblock_idx, &fblock);
1514 rr = ext4_fs_append_inode_block(&ref, &fblock,
1517 /* Unable to append more blocks. But some
1518 * block might be allocated already.
1526 if (!fblock_start) {
1527 fblock_start = fblock;
1530 if ((fblock_start + fblock_count) != fblock)
1536 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1541 size -= block_size * fblock_count;
1542 u8_buf += block_size * fblock_count;
1543 f->fpos += block_size * fblock_count;
1546 *wcnt += block_size * fblock_count;
1548 fblock_start = fblock;
1552 /*ext4_fs_append_inode_block has failed and no
1553 * more blocks might be written. But node size
1554 * should be updated.*/
1560 /*Stop write back cache mode*/
1561 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1567 if (iblock_idx < ifile_blocks) {
1568 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx,
1573 r = ext4_fs_append_inode_block(&ref, &fblock, &iblock_idx);
1575 /*Node size sholud be updated.*/
1579 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1583 memcpy(b.data, u8_buf, size);
1586 r = ext4_block_set(f->mp->fs.bdev, &b);
1597 if (f->fpos > f->fsize) {
1599 ext4_inode_set_size(ref.inode, f->fsize);
1604 ext4_fs_put_inode_ref(&ref);
1605 EXT4_MP_UNLOCK(f->mp);
1609 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1613 if (offset > f->fsize)
1619 if ((offset + f->fpos) > f->fsize)
1625 if (offset > f->fsize)
1628 f->fpos = f->fsize - offset;
1634 uint64_t ext4_ftell(ext4_file *f) { return f->fpos; }
1636 uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
1638 int ext4_chmod(const char *path, uint32_t mode)
1643 struct ext4_sblock *sb;
1644 struct ext4_inode_ref inode_ref;
1645 struct ext4_mountpoint *mp = ext4_get_mount(path);
1652 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1660 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1666 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1667 inode_ref.dirty = true;
1669 ext4_fs_put_inode_ref(&inode_ref);
1674 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1679 struct ext4_inode_ref inode_ref;
1680 struct ext4_mountpoint *mp = ext4_get_mount(path);
1687 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1694 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1700 ext4_inode_set_uid(inode_ref.inode, uid);
1701 ext4_inode_set_gid(inode_ref.inode, gid);
1702 inode_ref.dirty = true;
1704 ext4_fs_put_inode_ref(&inode_ref);
1709 int ext4_file_set_atime(const char *path, uint32_t atime)
1714 struct ext4_inode_ref inode_ref;
1715 struct ext4_mountpoint *mp = ext4_get_mount(path);
1722 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1729 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1735 ext4_inode_set_access_time(inode_ref.inode, atime);
1736 inode_ref.dirty = true;
1738 ext4_fs_put_inode_ref(&inode_ref);
1743 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1748 struct ext4_inode_ref inode_ref;
1749 struct ext4_mountpoint *mp = ext4_get_mount(path);
1756 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1763 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1769 ext4_inode_set_modification_time(inode_ref.inode, mtime);
1770 inode_ref.dirty = true;
1772 ext4_fs_put_inode_ref(&inode_ref);
1777 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1782 struct ext4_inode_ref inode_ref;
1783 struct ext4_mountpoint *mp = ext4_get_mount(path);
1790 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1797 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1803 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1804 inode_ref.dirty = true;
1806 ext4_fs_put_inode_ref(&inode_ref);
1811 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1813 struct ext4_block b;
1814 struct ext4_inode_ref ref;
1816 ext4_fsblk_t fblock;
1817 uint32_t block_size;
1820 ext4_assert(f && f->mp);
1825 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1827 EXT4_MP_UNLOCK(f->mp);
1832 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1833 if (size > block_size) {
1837 r = ext4_ftruncate_no_lock(f, 0);
1841 /*Start write back cache mode.*/
1842 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1846 /*If the size of symlink is smaller than 60 bytes*/
1847 if (size < sizeof(ref.inode->blocks)) {
1848 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1849 memcpy(ref.inode->blocks, buf, size);
1850 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1852 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1853 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1857 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1861 memcpy(b.data, buf, size);
1863 r = ext4_block_set(f->mp->fs.bdev, &b);
1868 /*Stop write back cache mode*/
1869 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1874 ext4_inode_set_size(ref.inode, size);
1882 ext4_fs_put_inode_ref(&ref);
1886 int ext4_fsymlink(const char *target, const char *path)
1888 struct ext4_mountpoint *mp = ext4_get_mount(path);
1896 filetype = EXT4_DIRENTRY_SYMLINK;
1899 ext4_block_cache_write_back(mp->fs.bdev, 1);
1900 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
1902 r = ext4_fsymlink_set(&f, target, strlen(target));
1909 ext4_block_cache_write_back(mp->fs.bdev, 0);
1914 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1916 struct ext4_mountpoint *mp = ext4_get_mount(path);
1927 filetype = EXT4_DIRENTRY_SYMLINK;
1930 ext4_block_cache_write_back(mp->fs.bdev, 1);
1931 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, 0, 0);
1933 r = ext4_fread(&f, buf, bufsize, rcnt);
1940 ext4_block_cache_write_back(mp->fs.bdev, 0);
1945 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1946 const void *data, size_t data_size, bool replace)
1952 const char *dissected_name = NULL;
1953 size_t dissected_len = 0;
1954 struct ext4_xattr_ref xattr_ref;
1955 struct ext4_inode_ref inode_ref;
1956 struct ext4_mountpoint *mp = ext4_get_mount(path);
1960 dissected_name = ext4_extract_xattr_name(name, name_len,
1961 &name_index, &dissected_len);
1966 r = ext4_generic_open2(&f, path, O_RDWR,
1967 EXT4_DIRENTRY_UNKNOWN, 0, 0);
1973 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
1977 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
1980 ext4_fs_put_inode_ref(&inode_ref);
1984 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
1985 dissected_len, data, data_size, replace);
1987 ext4_fs_put_xattr_ref(&xattr_ref);
1988 ext4_fs_put_inode_ref(&inode_ref);
1994 int ext4_getxattr(const char *path, const char *name, size_t name_len,
1995 void *buf, size_t buf_size, size_t *data_size)
2001 const char *dissected_name = NULL;
2002 size_t dissected_len = 0;
2003 struct ext4_xattr_ref xattr_ref;
2004 struct ext4_inode_ref inode_ref;
2005 struct ext4_mountpoint *mp = ext4_get_mount(path);
2009 dissected_name = ext4_extract_xattr_name(name, name_len,
2010 &name_index, &dissected_len);
2015 r = ext4_generic_open2(&f, path, O_RDWR,
2016 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2022 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2026 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2029 ext4_fs_put_inode_ref(&inode_ref);
2033 r = ext4_fs_get_xattr(&xattr_ref, name_index,
2034 dissected_name, dissected_len,
2035 buf, buf_size, data_size);
2037 ext4_fs_put_xattr_ref(&xattr_ref);
2038 ext4_fs_put_inode_ref(&inode_ref);
2044 struct ext4_listxattr_iterator {
2049 bool list_too_small;
2050 bool get_required_size;
2053 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2054 struct ext4_xattr_item *item)
2056 struct ext4_listxattr_iterator *lxi;
2057 lxi = ref->iter_arg;
2058 if (!lxi->get_required_size) {
2061 prefix = ext4_get_xattr_name_prefix(item->name_index,
2064 prefix_len + item->name_len + 1 >
2067 lxi->list_too_small = 1;
2068 return EXT4_XATTR_ITERATE_STOP;
2071 memcpy(lxi->list_ptr, prefix, prefix_len);
2072 lxi->list_ptr += prefix_len;
2073 lxi->ret_size += prefix_len;
2075 memcpy(lxi->list_ptr, item->name, item->name_len);
2076 lxi->list_ptr[item->name_len] = 0;
2077 lxi->list_ptr += item->name_len + 1;
2079 lxi->ret_size += item->name_len + 1;
2080 return EXT4_XATTR_ITERATE_CONT;
2083 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2088 struct ext4_xattr_ref xattr_ref;
2089 struct ext4_inode_ref inode_ref;
2090 struct ext4_listxattr_iterator lxi;
2091 struct ext4_mountpoint *mp = ext4_get_mount(path);
2096 lxi.list_ptr = list;
2099 lxi.list_too_small = false;
2100 lxi.get_required_size = (!size) ? true : false;
2103 r = ext4_generic_open2(&f, path, O_RDWR,
2104 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2110 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2114 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2117 ext4_fs_put_inode_ref(&inode_ref);
2121 xattr_ref.iter_arg = &lxi;
2122 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2123 if (lxi.list_too_small)
2128 *ret_size = lxi.ret_size;
2131 ext4_fs_put_xattr_ref(&xattr_ref);
2132 ext4_fs_put_inode_ref(&inode_ref);
2139 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2145 const char *dissected_name = NULL;
2146 size_t dissected_len = 0;
2147 struct ext4_xattr_ref xattr_ref;
2148 struct ext4_inode_ref inode_ref;
2149 struct ext4_mountpoint *mp = ext4_get_mount(path);
2153 dissected_name = ext4_extract_xattr_name(name, name_len,
2154 &name_index, &dissected_len);
2159 r = ext4_generic_open2(&f, path, O_RDWR,
2160 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2166 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2170 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2173 ext4_fs_put_inode_ref(&inode_ref);
2177 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2180 ext4_fs_put_xattr_ref(&xattr_ref);
2181 ext4_fs_put_inode_ref(&inode_ref);
2188 /*********************************DIRECTORY OPERATION************************/
2190 int ext4_dir_rm(const char *path)
2196 struct ext4_mountpoint *mp = ext4_get_mount(path);
2197 struct ext4_inode_ref current;
2198 struct ext4_inode_ref child;
2199 struct ext4_directory_iterator it;
2203 uint32_t inode_current;
2216 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2223 len = ext4_path_check(path, &is_goal);
2225 inode_current = f.inode;
2228 ext4_block_cache_write_back(mp->fs.bdev, 1);
2231 /*Load directory node.*/
2232 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, ¤t);
2237 /*Initialize iterator.*/
2238 r = ext4_dir_iterator_init(&it, ¤t, 0);
2240 ext4_fs_put_inode_ref(¤t);
2251 /*Get up directory inode when ".." entry*/
2252 if ((it.current->name_length == 2) &&
2253 ext4_is_dots(it.current->name,
2254 it.current->name_length)) {
2255 inode_up = ext4_dir_entry_ll_get_inode(it.current);
2258 /*If directory or file entry, but not "." ".." entry*/
2259 if (!ext4_is_dots(it.current->name,
2260 it.current->name_length)) {
2262 /*Get child inode reference do unlink
2264 r = ext4_fs_get_inode_ref(&f.mp->fs,
2265 ext4_dir_entry_ll_get_inode(it.current),
2270 /*If directory with no leaf children*/
2271 r = ext4_has_children(&has_children, &child);
2273 ext4_fs_put_inode_ref(&child);
2278 /*Has directory children. Go into this
2280 inode_up = inode_current;
2281 inode_current = ext4_dir_entry_ll_get_inode(it.current);
2283 ext4_fs_put_inode_ref(&child);
2287 /*No children in child directory or file. Just
2289 r = ext4_unlink(f.mp, ¤t, &child,
2290 (char *)it.current->name,
2291 it.current->name_length);
2293 ext4_fs_put_inode_ref(&child);
2297 ext4_inode_set_deletion_time(child.inode,
2299 ext4_inode_set_links_count(child.inode, 0);
2302 r = ext4_fs_truncate_inode(&child, 0);
2304 ext4_fs_put_inode_ref(&child);
2308 r = ext4_fs_free_inode(&child);
2310 ext4_fs_put_inode_ref(&child);
2314 r = ext4_fs_put_inode_ref(&child);
2319 r = ext4_dir_iterator_next(&it);
2323 /*Directory iterator reached last entry*/
2324 ext4_has_children(&has_children, ¤t);
2325 if (!has_children) {
2326 inode_current = inode_up;
2333 struct ext4_inode_ref parent;
2334 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2339 /* In this place all directories should be
2341 * Last unlink from root of current directory*/
2342 r = ext4_unlink(f.mp, &parent, ¤t,
2345 ext4_fs_put_inode_ref(&parent);
2349 if (ext4_inode_get_links_count(current.inode) ==
2351 ext4_inode_set_deletion_time(
2352 current.inode, 0xFFFFFFFF);
2353 ext4_inode_set_links_count(
2355 current.dirty = true;
2357 r = ext4_fs_truncate_inode(¤t, 0);
2359 ext4_fs_put_inode_ref(&parent);
2363 r = ext4_fs_free_inode(¤t);
2365 ext4_fs_put_inode_ref(&parent);
2370 r = ext4_fs_put_inode_ref(&parent);
2377 ext4_dir_iterator_fini(&it);
2378 ext4_fs_put_inode_ref(¤t);
2381 /*When something goes wrong. End loop.*/
2387 ext4_block_cache_write_back(mp->fs.bdev, 0);
2392 int ext4_dir_mk(const char *path)
2397 struct ext4_mountpoint *mp = ext4_get_mount(path);
2405 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2407 /*Directory already created*/
2413 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2423 int ext4_dir_open(ext4_dir *d, const char *path)
2425 struct ext4_mountpoint *mp = ext4_get_mount(path);
2432 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2438 int ext4_dir_close(ext4_dir *d)
2440 return ext4_fclose(&d->f);
2443 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2445 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2448 ext4_direntry *de = 0;
2449 struct ext4_inode_ref dir;
2450 struct ext4_directory_iterator it;
2452 EXT4_MP_LOCK(d->f.mp);
2454 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2455 EXT4_MP_UNLOCK(d->f.mp);
2459 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2464 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2466 ext4_fs_put_inode_ref(&dir);
2470 memcpy(&d->de, it.current, sizeof(ext4_direntry));
2473 ext4_dir_iterator_next(&it);
2476 it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
2478 ext4_dir_iterator_fini(&it);
2479 ext4_fs_put_inode_ref(&dir);
2482 EXT4_MP_UNLOCK(d->f.mp);
2486 void ext4_dir_entry_rewind(ext4_dir *d)