2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup lwext4
34 * @brief Ext4 high level operations (file, directory, mountpoints...)
37 #include "ext4_config.h"
39 #include "ext4_blockdev.h"
40 #include "ext4_types.h"
41 #include "ext4_debug.h"
42 #include "ext4_errno.h"
45 #include "ext4_inode.h"
46 #include "ext4_super.h"
47 #include "ext4_dir_idx.h"
48 #include "ext4_xattr.h"
54 /**@brief Mount point OS dependent lock*/
55 #define EXT4_MP_LOCK(_m) \
58 (_m)->os_locks->lock(); \
61 /**@brief Mount point OS dependent unlock*/
62 #define EXT4_MP_UNLOCK(_m) \
65 (_m)->os_locks->unlock(); \
68 /**@brief Mount point descriptor.*/
69 struct ext4_mountpoint {
71 /**@brief Mount done flag.*/
74 /**@brief Mount point name (@ref ext4_mount)*/
77 /**@brief OS dependent lock/unlock functions.*/
78 const struct ext4_lock *os_locks;
80 /**@brief Ext4 filesystem internals.*/
83 /**@brief Dynamic allocation cache flag.*/
87 /**@brief Block devices descriptor.*/
88 struct _ext4_devices {
90 /**@brief Block device name (@ref ext4_device_register)*/
93 /**@brief Block device handle.*/
94 struct ext4_blockdev *bd;
96 /**@brief Block cache handle.*/
97 struct ext4_bcache *bc;
100 /**@brief Block devices.*/
101 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
103 /**@brief Mountpoints.*/
104 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
106 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
107 const char *dev_name)
110 ext4_assert(bd && dev_name);
112 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
113 if (!_bdevices[i].bd) {
114 strcpy(_bdevices[i].name, dev_name);
115 _bdevices[i].bd = bd;
116 _bdevices[i].bc = bc;
120 if (!strcmp(_bdevices[i].name, dev_name))
126 /****************************************************************************/
128 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
130 if ((name_size == 1) && (name[0] == '.'))
133 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
139 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
141 struct ext4_fs *fs = enode->fs;
143 /* Check if node is directory */
144 if (!ext4_inode_is_type(&fs->sb, enode->inode,
145 EXT4_INODE_MODE_DIRECTORY)) {
146 *has_children = false;
150 struct ext4_dir_iterator it;
151 int rc = ext4_dir_iterator_init(&it, enode, 0);
155 /* Find a non-empty directory entry */
157 while (it.current != NULL) {
158 if (ext4_dir_entry_ll_get_inode(it.current) != 0) {
159 uint16_t name_size = ext4_dir_entry_ll_get_name_length(
160 &fs->sb, it.current);
161 if (!ext4_is_dots(it.current->name, name_size)) {
167 rc = ext4_dir_iterator_next(&it);
169 ext4_dir_iterator_fini(&it);
174 rc = ext4_dir_iterator_fini(&it);
178 *has_children = found;
183 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
184 struct ext4_inode_ref *child, const char *name,
185 uint32_t name_len, bool rename)
187 /* Check maximum name length */
188 if (name_len > EXT4_DIRECTORY_FILENAME_LEN)
191 /* Add entry to parent directory */
192 int rc = ext4_dir_add_entry(parent, name, name_len, child);
196 /* Fill new dir -> add '.' and '..' entries.
197 * Also newly allocated inode should have 0 link count.
199 if (ext4_inode_is_type(&mp->fs.sb, child->inode,
200 EXT4_INODE_MODE_DIRECTORY) && !rename) {
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_dir_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 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
589 struct ext4_mountpoint *mp = ext4_get_mount(path);
590 struct ext4_dir_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(&mp->fs, &child_ref,
647 is_goal ? filetype : EXT4_DIRENTRY_DIR);
652 /*Link with root dir.*/
653 r = ext4_link(mp, &ref, &child_ref, path, len, false);
655 /*Fail. Free new inode.*/
656 ext4_fs_free_inode(&child_ref);
657 /*We do not want to write new inode.
658 But block has to be released.*/
659 child_ref.dirty = false;
660 ext4_fs_put_inode_ref(&child_ref);
664 ext4_fs_put_inode_ref(&child_ref);
670 *parent_inode = ref.index;
672 next_inode = ext4_dir_entry_ll_get_inode(result.dentry);
673 if (ext4_sb_feature_incom(&mp->fs.sb, EXT4_FINCOM_FILETYPE)) {
675 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
677 inode_mode = ext4_fs_correspond_inode_mode(inode_type);
679 struct ext4_inode_ref child_ref;
680 r = ext4_fs_get_inode_ref(&mp->fs, next_inode,
685 inode_mode = ext4_inode_type(&mp->fs.sb,
688 ext4_fs_put_inode_ref(&child_ref);
691 r = ext4_dir_destroy_result(&ref, &result);
695 /*If expected file error*/
696 if (inode_mode != EXT4_INODE_MODE_DIRECTORY &&
701 if (filetype != EXT4_DIRENTRY_UNKNOWN) {
702 if ((inode_mode != ext4_fs_correspond_inode_mode(filetype)) &&
709 r = ext4_fs_put_inode_ref(&ref);
713 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
723 *name_off += len + 1;
727 ext4_fs_put_inode_ref(&ref);
733 if ((f->flags & O_TRUNC) &&
734 (inode_mode == EXT4_INODE_MODE_FILE)) {
736 r = ext4_fs_truncate_inode(&ref, 0);
738 ext4_fs_put_inode_ref(&ref);
744 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
745 f->inode = ref.index;
748 if (f->flags & O_APPEND)
753 r = ext4_fs_put_inode_ref(&ref);
757 /****************************************************************************/
759 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
760 bool file_expect, uint32_t *parent_inode,
765 if (ext4_parse_flags(flags, &iflags) == false)
768 if (file_expect == true)
769 filetype = EXT4_DIRENTRY_REG_FILE;
771 filetype = EXT4_DIRENTRY_DIR;
773 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
777 static int __ext4_create_hardlink(const char *path,
778 struct ext4_inode_ref *child_ref,
781 bool is_goal = false;
782 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
786 struct ext4_mountpoint *mp = ext4_get_mount(path);
787 struct ext4_dir_search_result result;
788 struct ext4_inode_ref ref;
794 path += strlen(mp->name);
797 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
802 int len = ext4_path_check(path, &is_goal);
806 len = ext4_path_check(path, &is_goal);
809 /*If root open was request.*/
817 r = ext4_dir_find_entry(&result, &ref, path, len);
820 /*Destroy last result*/
821 ext4_dir_destroy_result(&ref, &result);
823 if (r != ENOENT || !is_goal)
826 /*Link with root dir.*/
827 r = ext4_link(mp, &ref, child_ref, path, len, rename);
829 } else if (r == EOK && is_goal) {
830 /*Destroy last result*/
831 ext4_dir_destroy_result(&ref, &result);
836 next_inode = result.dentry->inode;
837 if (ext4_sb_feature_incom(&mp->fs.sb, EXT4_FINCOM_FILETYPE)) {
839 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
841 inode_mode = ext4_fs_correspond_inode_mode(inode_type);
843 struct ext4_inode_ref child_ref;
844 r = ext4_fs_get_inode_ref(&mp->fs, next_inode,
849 inode_mode = ext4_inode_type(&mp->fs.sb,
852 ext4_fs_put_inode_ref(&child_ref);
855 r = ext4_dir_destroy_result(&ref, &result);
859 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
868 r = ext4_fs_put_inode_ref(&ref);
872 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
883 ext4_fs_put_inode_ref(&ref);
887 r = ext4_fs_put_inode_ref(&ref);
891 static int ext4_remove_orig_reference(const char *path,
893 struct ext4_inode_ref *parent_ref,
894 struct ext4_inode_ref *child_ref)
899 struct ext4_mountpoint *mp = ext4_get_mount(path);
907 len = ext4_path_check(path, &is_goal);
909 /* Remove entry from parent directory */
910 r = ext4_dir_remove_entry(parent_ref, path, len);
914 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
915 EXT4_INODE_MODE_DIRECTORY)) {
916 ext4_fs_inode_links_count_dec(parent_ref);
917 parent_ref->dirty = true;
923 int ext4_flink(const char *path, const char *hardlink_path)
928 bool child_loaded = false;
929 uint32_t parent_inode, child_inode;
930 struct ext4_mountpoint *mp = ext4_get_mount(path);
931 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
932 struct ext4_inode_ref child_ref;
937 /* Will that happen? Anyway return EINVAL for such case. */
943 r = ext4_generic_open2(&f, path, O_RDONLY,
944 EXT4_DIRENTRY_UNKNOWN,
945 &parent_inode, &name_off);
949 child_inode = f.inode;
952 /*We have file to unlink. Load it.*/
953 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
959 /* Creating hardlink for directory is not allowed. */
960 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
961 EXT4_INODE_MODE_DIRECTORY)) {
966 r = __ext4_create_hardlink(hardlink_path, &child_ref, false);
970 ext4_fs_put_inode_ref(&child_ref);
977 int ext4_frename(const char *path, const char *new_path)
982 bool parent_loaded = false, child_loaded = false;
983 uint32_t parent_inode, child_inode;
984 struct ext4_mountpoint *mp = ext4_get_mount(path);
985 struct ext4_inode_ref child_ref, parent_ref;
992 r = ext4_generic_open2(&f, path, O_RDONLY,
993 EXT4_DIRENTRY_UNKNOWN,
994 &parent_inode, &name_off);
998 child_inode = f.inode;
1002 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1006 parent_loaded = true;
1008 /*We have file to unlink. Load it.*/
1009 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1013 child_loaded = true;
1015 r = __ext4_create_hardlink(new_path, &child_ref, true);
1019 r = ext4_remove_orig_reference(path, name_off,
1020 &parent_ref, &child_ref);
1026 ext4_fs_put_inode_ref(&parent_ref);
1029 ext4_fs_put_inode_ref(&child_ref);
1036 /****************************************************************************/
1038 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1040 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1049 int ext4_cache_write_back(const char *path, bool on)
1051 struct ext4_mountpoint *mp = ext4_get_mount(path);
1057 ext4_block_cache_write_back(mp->fs.bdev, on);
1062 int ext4_fremove(const char *path)
1065 uint32_t parent_inode;
1070 struct ext4_inode_ref child;
1071 struct ext4_inode_ref parent;
1072 struct ext4_mountpoint *mp = ext4_get_mount(path);
1078 r = ext4_generic_open2(&f, path, O_RDWR,
1079 EXT4_DIRENTRY_UNKNOWN,
1080 &parent_inode, &name_off);
1087 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1093 /*We have file to delete. Load it.*/
1094 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1096 ext4_fs_put_inode_ref(&parent);
1104 len = ext4_path_check(path, &is_goal);
1106 /*Unlink from parent*/
1107 r = ext4_unlink(mp, &parent, &child, path, len);
1111 /*Link count is zero, the inode should be freed. */
1112 if (!ext4_inode_get_links_count(child.inode)) {
1113 ext4_inode_set_deletion_time(child.inode, 0xFFFFFFFF);
1115 ext4_block_cache_write_back(mp->fs.bdev, 1);
1116 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1117 r = ext4_fs_truncate_inode(&child, 0);
1118 ext4_block_cache_write_back(mp->fs.bdev, 0);
1123 r = ext4_fs_free_inode(&child);
1129 ext4_fs_put_inode_ref(&child);
1130 ext4_fs_put_inode_ref(&parent);
1135 int ext4_fill_raw_inode(const char *path,
1137 struct ext4_inode *inode)
1141 struct ext4_inode_ref inode_ref;
1142 struct ext4_mountpoint *mp = ext4_get_mount(path);
1150 r = ext4_generic_open2(&f, path, O_RDONLY,
1151 EXT4_DIRENTRY_UNKNOWN,
1162 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1168 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1170 ext4_fs_put_inode_ref(&inode_ref);
1179 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1181 struct ext4_mountpoint *mp = ext4_get_mount(path);
1188 ext4_block_cache_write_back(mp->fs.bdev, 1);
1189 r = ext4_generic_open(f, path, flags, true, 0, 0);
1190 ext4_block_cache_write_back(mp->fs.bdev, 0);
1195 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1197 struct ext4_mountpoint *mp = ext4_get_mount(path);
1204 filetype = EXT4_DIRENTRY_REG_FILE;
1207 ext4_block_cache_write_back(mp->fs.bdev, 1);
1208 r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
1209 ext4_block_cache_write_back(mp->fs.bdev, 0);
1214 int ext4_fclose(ext4_file *f)
1216 ext4_assert(f && f->mp);
1221 f->fpos = f->fsize = 0;
1226 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1228 struct ext4_inode_ref ref;
1232 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1234 EXT4_MP_UNLOCK(f->mp);
1239 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1240 if (f->fsize <= size) {
1245 /*Start write back cache mode.*/
1246 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1250 r = ext4_fs_truncate_inode(&ref, size);
1258 /*Stop write back cache mode*/
1259 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1265 ext4_fs_put_inode_ref(&ref);
1270 int ext4_ftruncate(ext4_file *f, uint64_t size)
1273 ext4_assert(f && f->mp);
1275 if (f->flags & O_RDONLY)
1278 EXT4_MP_LOCK(f->mp);
1280 r = ext4_ftruncate_no_lock(f, size);
1282 EXT4_MP_UNLOCK(f->mp);
1286 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1289 uint32_t iblock_idx;
1290 uint32_t iblock_last;
1291 uint32_t block_size;
1293 ext4_fsblk_t fblock;
1294 ext4_fsblk_t fblock_start;
1295 uint32_t fblock_count;
1297 uint8_t *u8_buf = buf;
1299 struct ext4_block b;
1300 struct ext4_inode_ref ref;
1302 ext4_assert(f && f->mp);
1304 if (f->flags & O_WRONLY)
1310 EXT4_MP_LOCK(f->mp);
1315 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1317 EXT4_MP_UNLOCK(f->mp);
1322 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1324 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1325 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1327 iblock_idx = (f->fpos) / block_size;
1328 iblock_last = (f->fpos + size) / block_size;
1329 unaligned = (f->fpos) % block_size;
1331 /*If the size of symlink is smaller than 60 bytes*/
1332 if (ext4_inode_is_type(&f->mp->fs.sb, ref.inode, EXT4_INODE_MODE_SOFTLINK)
1333 && f->fsize < sizeof(ref.inode->blocks)
1334 && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
1335 char *content = (char *)ref.inode->blocks;
1336 if (f->fpos < f->fsize) {
1337 r = (unaligned + size > f->fsize)
1338 ?(f->fsize - unaligned)
1340 memcpy(buf, content + unaligned, r);
1356 uint32_t l = size > (block_size - unaligned) ?
1357 (block_size - unaligned) : size;
1358 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1363 /* Do we get an unwritten range? */
1365 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1369 memcpy(u8_buf, b.data + unaligned, l);
1371 r = ext4_block_set(f->mp->fs.bdev, &b);
1376 memset(u8_buf, 0, l);
1391 while (size >= block_size) {
1392 while (iblock_idx < iblock_last) {
1393 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1401 fblock_start = fblock;
1403 if ((fblock_start + fblock_count) != fblock)
1409 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1414 size -= block_size * fblock_count;
1415 u8_buf += block_size * fblock_count;
1416 f->fpos += block_size * fblock_count;
1419 *rcnt += block_size * fblock_count;
1421 fblock_start = fblock;
1426 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1431 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1435 memcpy(u8_buf, b.data, size);
1437 r = ext4_block_set(f->mp->fs.bdev, &b);
1448 ext4_fs_put_inode_ref(&ref);
1449 EXT4_MP_UNLOCK(f->mp);
1453 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1456 uint32_t iblock_idx;
1457 uint32_t iblock_last;
1458 uint32_t ifile_blocks;
1459 uint32_t block_size;
1461 uint32_t fblock_count;
1462 ext4_fsblk_t fblock;
1463 ext4_fsblk_t fblock_start;
1465 struct ext4_block b;
1466 struct ext4_inode_ref ref;
1467 const uint8_t *u8_buf = buf;
1470 ext4_assert(f && f->mp);
1472 if (f->flags & O_RDONLY)
1478 EXT4_MP_LOCK(f->mp);
1483 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1485 EXT4_MP_UNLOCK(f->mp);
1490 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1491 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1493 iblock_last = (f->fpos + size) / block_size;
1494 iblock_idx = (f->fpos) / block_size;
1495 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1497 unaligned = (f->fpos) % block_size;
1500 uint32_t l = size > (block_size - unaligned) ?
1501 (block_size - unaligned) : size;
1502 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx, &fblock);
1506 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1510 memcpy(b.data + unaligned, u8_buf, l);
1513 r = ext4_block_set(f->mp->fs.bdev, &b);
1527 /*Start write back cache mode.*/
1528 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1534 while (size >= block_size) {
1536 while (iblock_idx < iblock_last) {
1537 if (iblock_idx < ifile_blocks) {
1538 r = ext4_fs_init_inode_data_block_index(
1539 &ref, iblock_idx, &fblock);
1543 rr = ext4_fs_append_inode_block(&ref, &fblock,
1546 /* Unable to append more blocks. But some
1547 * block might be allocated already.
1555 if (!fblock_start) {
1556 fblock_start = fblock;
1559 if ((fblock_start + fblock_count) != fblock)
1565 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1570 size -= block_size * fblock_count;
1571 u8_buf += block_size * fblock_count;
1572 f->fpos += block_size * fblock_count;
1575 *wcnt += block_size * fblock_count;
1577 fblock_start = fblock;
1581 /*ext4_fs_append_inode_block has failed and no
1582 * more blocks might be written. But node size
1583 * should be updated.*/
1589 /*Stop write back cache mode*/
1590 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1596 if (iblock_idx < ifile_blocks) {
1597 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx,
1602 r = ext4_fs_append_inode_block(&ref, &fblock, &iblock_idx);
1604 /*Node size sholud be updated.*/
1608 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1612 memcpy(b.data, u8_buf, size);
1615 r = ext4_block_set(f->mp->fs.bdev, &b);
1626 if (f->fpos > f->fsize) {
1628 ext4_inode_set_size(ref.inode, f->fsize);
1633 ext4_fs_put_inode_ref(&ref);
1634 EXT4_MP_UNLOCK(f->mp);
1638 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1642 if (offset > f->fsize)
1648 if ((offset + f->fpos) > f->fsize)
1654 if (offset > f->fsize)
1657 f->fpos = f->fsize - offset;
1663 uint64_t ext4_ftell(ext4_file *f) { return f->fpos; }
1665 uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
1667 int ext4_chmod(const char *path, uint32_t mode)
1672 struct ext4_sblock *sb;
1673 struct ext4_inode_ref inode_ref;
1674 struct ext4_mountpoint *mp = ext4_get_mount(path);
1681 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1689 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1695 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1696 inode_ref.dirty = true;
1698 ext4_fs_put_inode_ref(&inode_ref);
1703 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1708 struct ext4_inode_ref inode_ref;
1709 struct ext4_mountpoint *mp = ext4_get_mount(path);
1716 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1723 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1729 ext4_inode_set_uid(inode_ref.inode, uid);
1730 ext4_inode_set_gid(inode_ref.inode, gid);
1731 inode_ref.dirty = true;
1733 ext4_fs_put_inode_ref(&inode_ref);
1738 int ext4_file_set_atime(const char *path, uint32_t atime)
1743 struct ext4_inode_ref inode_ref;
1744 struct ext4_mountpoint *mp = ext4_get_mount(path);
1751 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1758 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1764 ext4_inode_set_access_time(inode_ref.inode, atime);
1765 inode_ref.dirty = true;
1767 ext4_fs_put_inode_ref(&inode_ref);
1772 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1777 struct ext4_inode_ref inode_ref;
1778 struct ext4_mountpoint *mp = ext4_get_mount(path);
1785 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1792 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1798 ext4_inode_set_modification_time(inode_ref.inode, mtime);
1799 inode_ref.dirty = true;
1801 ext4_fs_put_inode_ref(&inode_ref);
1806 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1811 struct ext4_inode_ref inode_ref;
1812 struct ext4_mountpoint *mp = ext4_get_mount(path);
1819 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1826 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1832 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1833 inode_ref.dirty = true;
1835 ext4_fs_put_inode_ref(&inode_ref);
1840 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1842 struct ext4_block b;
1843 struct ext4_inode_ref ref;
1845 ext4_fsblk_t fblock;
1846 uint32_t block_size;
1849 ext4_assert(f && f->mp);
1854 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1856 EXT4_MP_UNLOCK(f->mp);
1861 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1862 if (size > block_size) {
1866 r = ext4_ftruncate_no_lock(f, 0);
1870 /*Start write back cache mode.*/
1871 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1875 /*If the size of symlink is smaller than 60 bytes*/
1876 if (size < sizeof(ref.inode->blocks)) {
1877 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1878 memcpy(ref.inode->blocks, buf, size);
1879 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1881 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1882 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1886 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1890 memcpy(b.data, buf, size);
1892 r = ext4_block_set(f->mp->fs.bdev, &b);
1897 /*Stop write back cache mode*/
1898 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1903 ext4_inode_set_size(ref.inode, size);
1911 ext4_fs_put_inode_ref(&ref);
1915 int ext4_fsymlink(const char *target, const char *path)
1917 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_RDWR|O_CREAT, filetype, 0, 0);
1931 r = ext4_fsymlink_set(&f, target, strlen(target));
1938 ext4_block_cache_write_back(mp->fs.bdev, 0);
1943 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1945 struct ext4_mountpoint *mp = ext4_get_mount(path);
1956 filetype = EXT4_DIRENTRY_SYMLINK;
1959 ext4_block_cache_write_back(mp->fs.bdev, 1);
1960 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, 0, 0);
1962 r = ext4_fread(&f, buf, bufsize, rcnt);
1969 ext4_block_cache_write_back(mp->fs.bdev, 0);
1974 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1975 const void *data, size_t data_size, bool replace)
1981 const char *dissected_name = NULL;
1982 size_t dissected_len = 0;
1983 struct ext4_xattr_ref xattr_ref;
1984 struct ext4_inode_ref inode_ref;
1985 struct ext4_mountpoint *mp = ext4_get_mount(path);
1989 dissected_name = ext4_extract_xattr_name(name, name_len,
1990 &name_index, &dissected_len);
1995 r = ext4_generic_open2(&f, path, O_RDWR,
1996 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2002 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2006 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2009 ext4_fs_put_inode_ref(&inode_ref);
2013 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2014 dissected_len, data, data_size, replace);
2016 ext4_fs_put_xattr_ref(&xattr_ref);
2017 ext4_fs_put_inode_ref(&inode_ref);
2023 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2024 void *buf, size_t buf_size, size_t *data_size)
2030 const char *dissected_name = NULL;
2031 size_t dissected_len = 0;
2032 struct ext4_xattr_ref xattr_ref;
2033 struct ext4_inode_ref inode_ref;
2034 struct ext4_mountpoint *mp = ext4_get_mount(path);
2038 dissected_name = ext4_extract_xattr_name(name, name_len,
2039 &name_index, &dissected_len);
2044 r = ext4_generic_open2(&f, path, O_RDWR,
2045 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2051 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2055 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2058 ext4_fs_put_inode_ref(&inode_ref);
2062 r = ext4_fs_get_xattr(&xattr_ref, name_index,
2063 dissected_name, dissected_len,
2064 buf, buf_size, data_size);
2066 ext4_fs_put_xattr_ref(&xattr_ref);
2067 ext4_fs_put_inode_ref(&inode_ref);
2073 struct ext4_listxattr_iterator {
2078 bool list_too_small;
2079 bool get_required_size;
2082 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2083 struct ext4_xattr_item *item)
2085 struct ext4_listxattr_iterator *lxi;
2086 lxi = ref->iter_arg;
2087 if (!lxi->get_required_size) {
2090 prefix = ext4_get_xattr_name_prefix(item->name_index,
2093 prefix_len + item->name_len + 1 >
2096 lxi->list_too_small = 1;
2097 return EXT4_XATTR_ITERATE_STOP;
2100 memcpy(lxi->list_ptr, prefix, prefix_len);
2101 lxi->list_ptr += prefix_len;
2102 lxi->ret_size += prefix_len;
2104 memcpy(lxi->list_ptr, item->name, item->name_len);
2105 lxi->list_ptr[item->name_len] = 0;
2106 lxi->list_ptr += item->name_len + 1;
2108 lxi->ret_size += item->name_len + 1;
2109 return EXT4_XATTR_ITERATE_CONT;
2112 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2117 struct ext4_xattr_ref xattr_ref;
2118 struct ext4_inode_ref inode_ref;
2119 struct ext4_listxattr_iterator lxi;
2120 struct ext4_mountpoint *mp = ext4_get_mount(path);
2125 lxi.list_ptr = list;
2128 lxi.list_too_small = false;
2129 lxi.get_required_size = (!size) ? true : false;
2132 r = ext4_generic_open2(&f, path, O_RDWR,
2133 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2139 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2143 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2146 ext4_fs_put_inode_ref(&inode_ref);
2150 xattr_ref.iter_arg = &lxi;
2151 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2152 if (lxi.list_too_small)
2157 *ret_size = lxi.ret_size;
2160 ext4_fs_put_xattr_ref(&xattr_ref);
2161 ext4_fs_put_inode_ref(&inode_ref);
2168 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2174 const char *dissected_name = NULL;
2175 size_t dissected_len = 0;
2176 struct ext4_xattr_ref xattr_ref;
2177 struct ext4_inode_ref inode_ref;
2178 struct ext4_mountpoint *mp = ext4_get_mount(path);
2182 dissected_name = ext4_extract_xattr_name(name, name_len,
2183 &name_index, &dissected_len);
2188 r = ext4_generic_open2(&f, path, O_RDWR,
2189 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2195 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2199 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2202 ext4_fs_put_inode_ref(&inode_ref);
2206 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2209 ext4_fs_put_xattr_ref(&xattr_ref);
2210 ext4_fs_put_inode_ref(&inode_ref);
2217 /*********************************DIRECTORY OPERATION************************/
2219 int ext4_dir_rm(const char *path)
2225 struct ext4_mountpoint *mp = ext4_get_mount(path);
2226 struct ext4_inode_ref current;
2227 struct ext4_inode_ref child;
2228 struct ext4_dir_iterator it;
2232 uint32_t inode_current;
2245 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2252 len = ext4_path_check(path, &is_goal);
2254 inode_current = f.inode;
2257 ext4_block_cache_write_back(mp->fs.bdev, 1);
2260 /*Load directory node.*/
2261 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, ¤t);
2266 /*Initialize iterator.*/
2267 r = ext4_dir_iterator_init(&it, ¤t, 0);
2269 ext4_fs_put_inode_ref(¤t);
2280 /*Get up directory inode when ".." entry*/
2281 if ((it.current->name_length == 2) &&
2282 ext4_is_dots(it.current->name,
2283 it.current->name_length)) {
2284 inode_up = ext4_dir_entry_ll_get_inode(it.current);
2287 /*If directory or file entry, but not "." ".." entry*/
2288 if (!ext4_is_dots(it.current->name,
2289 it.current->name_length)) {
2291 /*Get child inode reference do unlink
2293 r = ext4_fs_get_inode_ref(&f.mp->fs,
2294 ext4_dir_entry_ll_get_inode(it.current),
2299 /*If directory with no leaf children*/
2300 r = ext4_has_children(&has_children, &child);
2302 ext4_fs_put_inode_ref(&child);
2307 /*Has directory children. Go into this
2309 inode_up = inode_current;
2310 inode_current = ext4_dir_entry_ll_get_inode(it.current);
2312 ext4_fs_put_inode_ref(&child);
2316 /*No children in child directory or file. Just
2318 r = ext4_unlink(f.mp, ¤t, &child,
2319 (char *)it.current->name,
2320 it.current->name_length);
2322 ext4_fs_put_inode_ref(&child);
2326 ext4_inode_set_deletion_time(child.inode,
2328 ext4_inode_set_links_count(child.inode, 0);
2331 r = ext4_fs_truncate_inode(&child, 0);
2333 ext4_fs_put_inode_ref(&child);
2337 r = ext4_fs_free_inode(&child);
2339 ext4_fs_put_inode_ref(&child);
2343 r = ext4_fs_put_inode_ref(&child);
2348 r = ext4_dir_iterator_next(&it);
2352 /*Directory iterator reached last entry*/
2353 ext4_has_children(&has_children, ¤t);
2354 if (!has_children) {
2355 inode_current = inode_up;
2362 struct ext4_inode_ref parent;
2363 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2368 /* In this place all directories should be
2370 * Last unlink from root of current directory*/
2371 r = ext4_unlink(f.mp, &parent, ¤t,
2374 ext4_fs_put_inode_ref(&parent);
2378 if (ext4_inode_get_links_count(current.inode) ==
2380 ext4_inode_set_deletion_time(
2381 current.inode, 0xFFFFFFFF);
2382 ext4_inode_set_links_count(
2384 current.dirty = true;
2386 r = ext4_fs_truncate_inode(¤t, 0);
2388 ext4_fs_put_inode_ref(&parent);
2392 r = ext4_fs_free_inode(¤t);
2394 ext4_fs_put_inode_ref(&parent);
2399 r = ext4_fs_put_inode_ref(&parent);
2406 ext4_dir_iterator_fini(&it);
2407 ext4_fs_put_inode_ref(¤t);
2410 /*When something goes wrong. End loop.*/
2416 ext4_block_cache_write_back(mp->fs.bdev, 0);
2421 int ext4_dir_mk(const char *path)
2426 struct ext4_mountpoint *mp = ext4_get_mount(path);
2434 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2436 /*Directory already created*/
2442 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2452 int ext4_dir_open(ext4_dir *d, const char *path)
2454 struct ext4_mountpoint *mp = ext4_get_mount(path);
2461 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2467 int ext4_dir_close(ext4_dir *d)
2469 return ext4_fclose(&d->f);
2472 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2474 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2477 ext4_direntry *de = 0;
2478 struct ext4_inode_ref dir;
2479 struct ext4_dir_iterator it;
2481 EXT4_MP_LOCK(d->f.mp);
2483 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2484 EXT4_MP_UNLOCK(d->f.mp);
2488 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2493 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2495 ext4_fs_put_inode_ref(&dir);
2499 memcpy(&d->de, it.current, sizeof(ext4_direntry));
2502 ext4_dir_iterator_next(&it);
2505 it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
2507 ext4_dir_iterator_fini(&it);
2508 ext4_fs_put_inode_ref(&dir);
2511 EXT4_MP_UNLOCK(d->f.mp);
2515 void ext4_dir_entry_rewind(ext4_dir *d)