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"
49 #include "ext4_journal.h"
55 /**@brief Mount point OS dependent lock*/
56 #define EXT4_MP_LOCK(_m) \
59 (_m)->os_locks->lock(); \
62 /**@brief Mount point OS dependent unlock*/
63 #define EXT4_MP_UNLOCK(_m) \
66 (_m)->os_locks->unlock(); \
69 /**@brief Mount point descriptor.*/
70 struct ext4_mountpoint {
72 /**@brief Mount done flag.*/
75 /**@brief Mount point name (@ref ext4_mount)*/
78 /**@brief OS dependent lock/unlock functions.*/
79 const struct ext4_lock *os_locks;
81 /**@brief Ext4 filesystem internals.*/
84 /**@brief Dynamic allocation cache flag.*/
88 /**@brief Block devices descriptor.*/
89 struct _ext4_devices {
91 /**@brief Block device name (@ref ext4_device_register)*/
94 /**@brief Block device handle.*/
95 struct ext4_blockdev *bd;
97 /**@brief Block cache handle.*/
98 struct ext4_bcache *bc;
101 /**@brief Block devices.*/
102 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
104 /**@brief Mountpoints.*/
105 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
107 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
108 const char *dev_name)
111 ext4_assert(bd && dev_name);
113 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
114 if (!_bdevices[i].bd) {
115 strcpy(_bdevices[i].name, dev_name);
116 _bdevices[i].bd = bd;
117 _bdevices[i].bc = bc;
121 if (!strcmp(_bdevices[i].name, dev_name))
127 /****************************************************************************/
129 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
131 if ((name_size == 1) && (name[0] == '.'))
134 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
140 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
142 struct ext4_sblock *sb = &enode->fs->sb;
144 /* Check if node is directory */
145 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
146 *has_children = false;
150 struct ext4_dir_iter it;
151 int rc = ext4_dir_iterator_init(&it, enode, 0);
155 /* Find a non-empty directory entry */
157 while (it.curr != NULL) {
158 if (ext4_dir_en_get_inode(it.curr) != 0) {
160 nsize = ext4_dir_en_get_name_len(sb, it.curr);
161 if (!ext4_is_dots(it.curr->name, nsize)) {
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 *nam,
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 r = ext4_dir_add_entry(parent, nam, name_len, child);
196 /* Fill new dir -> add '.' and '..' entries.
197 * Also newly allocated inode should have 0 link count.
200 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
201 EXT4_INODE_MODE_DIRECTORY);
202 if (is_dir && !rename) {
204 #if CONFIG_DIR_INDEX_ENABLE
205 /* Initialize directory index if supported */
206 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
207 r = ext4_dir_dx_init(child, parent);
211 ext4_inode_set_flag(child->inode, EXT4_INODE_FLAG_INDEX);
216 r = ext4_dir_add_entry(child, ".", strlen("."), child);
218 ext4_dir_remove_entry(parent, nam, strlen(nam));
222 r = ext4_dir_add_entry(child, "..", strlen(".."), parent);
224 ext4_dir_remove_entry(parent, nam, strlen(nam));
225 ext4_dir_remove_entry(child, ".", strlen("."));
230 /*New empty directory. Two links (. and ..) */
231 ext4_inode_set_links_cnt(child->inode, 2);
232 ext4_fs_inode_links_count_inc(parent);
234 parent->dirty = true;
238 * In case we want to rename a directory,
239 * we reset the original '..' pointer.
243 idx = ext4_inode_has_flag(child->inode, EXT4_INODE_FLAG_INDEX);
244 struct ext4_dir_search_result res;
246 r = ext4_dir_find_entry(&res, child, "..", strlen(".."));
250 ext4_dir_en_set_inode(res.dentry, parent->index);
251 res.block.dirty = true;
252 r = ext4_dir_destroy_result(child, &res);
257 #if CONFIG_DIR_INDEX_ENABLE
258 r = ext4_dir_dx_reset_parent_inode(child, parent->index);
265 ext4_fs_inode_links_count_inc(parent);
266 parent->dirty = true;
269 ext4_fs_inode_links_count_inc(child);
276 static int ext4_unlink(struct ext4_mountpoint *mp,
277 struct ext4_inode_ref *parent,
278 struct ext4_inode_ref *child_inode_ref, const char *name,
282 int rc = ext4_has_children(&has_children, child_inode_ref);
286 /* Cannot unlink non-empty node */
290 /* Remove entry from parent directory */
291 rc = ext4_dir_remove_entry(parent, name, name_len);
295 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
296 EXT4_INODE_MODE_DIRECTORY);
298 /* If directory - handle links from parent */
300 ext4_fs_inode_links_count_dec(parent);
301 parent->dirty = true;
305 * TODO: Update timestamps of the parent
306 * (when we have wall-clock time).
308 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
309 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
310 * parent->dirty = true;
314 * TODO: Update timestamp for inode.
316 * ext4_inode_set_change_inode_time(child_inode_ref->inode,
319 if (ext4_inode_get_links_cnt(child_inode_ref->inode)) {
320 ext4_fs_inode_links_count_dec(child_inode_ref);
321 child_inode_ref->dirty = true;
327 /****************************************************************************/
329 int ext4_mount(const char *dev_name, const char *mount_point)
331 ext4_assert(mount_point && dev_name);
336 struct ext4_blockdev *bd = 0;
337 struct ext4_bcache *bc = 0;
338 struct ext4_mountpoint *mp = 0;
340 if (mount_point[strlen(mount_point) - 1] != '/')
343 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
344 if (_bdevices[i].name) {
345 if (!strcmp(dev_name, _bdevices[i].name)) {
346 bd = _bdevices[i].bd;
347 bc = _bdevices[i].bc;
356 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
357 if (!_mp[i].mounted) {
358 strcpy(_mp[i].name, mount_point);
364 if (!strcmp(_mp[i].name, mount_point))
371 r = ext4_block_init(bd);
375 r = ext4_fs_init(&mp->fs, bd);
381 bsize = ext4_sb_get_block_size(&mp->fs.sb);
382 ext4_block_set_lb_size(bd, bsize);
384 mp->cache_dynamic = 0;
387 /*Automatic block cache alloc.*/
388 mp->cache_dynamic = 1;
389 bc = malloc(sizeof(struct ext4_bcache));
391 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
400 if (bsize != bc->itemsize)
403 /*Bind block cache to block device*/
404 r = ext4_block_bind_bcache(bd, bc);
407 if (mp->cache_dynamic) {
408 ext4_bcache_fini_dynamic(bc);
414 /* TODO: journal mount checking. */
415 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
416 struct jbd_fs jbd_fs = {0};
417 r = jbd_get_fs(&mp->fs, &jbd_fs);
421 r = jbd_recover(&jbd_fs);
428 int ext4_umount(const char *mount_point)
432 struct ext4_mountpoint *mp = 0;
434 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
435 if (!strcmp(_mp[i].name, mount_point)) {
444 r = ext4_fs_fini(&mp->fs);
450 if (mp->cache_dynamic) {
451 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
452 free(mp->fs.bdev->bc);
455 return ext4_block_fini(mp->fs.bdev);
458 static struct ext4_mountpoint *ext4_get_mount(const char *path)
461 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
466 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
472 int ext4_mount_point_stats(const char *mount_point,
473 struct ext4_mount_stats *stats)
475 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
481 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
482 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
483 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
484 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
485 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
487 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
488 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
489 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
491 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
497 int ext4_mount_setup_locks(const char *mount_point,
498 const struct ext4_lock *locks)
501 struct ext4_mountpoint *mp = 0;
503 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
504 if (!strcmp(_mp[i].name, mount_point)) {
512 mp->os_locks = locks;
516 /********************************FILE OPERATIONS*****************************/
518 static int ext4_path_check(const char *path, bool *is_goal)
522 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
524 if (path[i] == '/') {
538 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
543 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
544 *file_flags = O_RDONLY;
548 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
549 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
553 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
554 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
558 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
559 !strcmp(flags, "r+b")) {
560 *file_flags = O_RDWR;
564 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
565 !strcmp(flags, "w+b")) {
566 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
570 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
571 !strcmp(flags, "a+b")) {
572 *file_flags = O_RDWR | O_CREAT | O_APPEND;
580 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
581 * any filetype of the target dir entry will be accepted.
583 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
584 int ftype, uint32_t *parent_inode,
587 bool is_goal = false;
588 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
593 struct ext4_mountpoint *mp = ext4_get_mount(path);
594 struct ext4_dir_search_result result;
595 struct ext4_inode_ref ref;
602 struct ext4_fs *const fs = &mp->fs;
603 struct ext4_sblock *const sb = &mp->fs.sb;
608 path += strlen(mp->name);
611 *name_off = strlen(mp->name);
614 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
619 *parent_inode = ref.index;
621 len = ext4_path_check(path, &is_goal);
624 len = ext4_path_check(path, &is_goal);
626 /*If root open was request.*/
628 ((ftype == EXT4_DE_DIR) ||
629 (ftype == EXT4_DE_UNKNOWN)))
636 r = ext4_dir_find_entry(&result, &ref, path, len);
639 /*Destroy last result*/
640 ext4_dir_destroy_result(&ref, &result);
644 if (!(f->flags & O_CREAT))
647 /*O_CREAT allows create new entry*/
648 struct ext4_inode_ref child_ref;
649 r = ext4_fs_alloc_inode(fs, &child_ref,
650 is_goal ? ftype : EXT4_DE_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);
672 *parent_inode = ref.index;
674 next_inode = ext4_dir_en_get_inode(result.dentry);
675 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
677 t = ext4_dir_en_get_inode_type(sb, result.dentry);
678 imode = ext4_fs_correspond_inode_mode(t);
680 struct ext4_inode_ref child_ref;
681 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
685 imode = ext4_inode_type(sb, child_ref.inode);
686 ext4_fs_put_inode_ref(&child_ref);
689 r = ext4_dir_destroy_result(&ref, &result);
693 /*If expected file error*/
694 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
698 if (ftype != EXT4_DE_UNKNOWN) {
699 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
706 r = ext4_fs_put_inode_ref(&ref);
710 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
720 *name_off += len + 1;
724 ext4_fs_put_inode_ref(&ref);
730 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
731 r = ext4_fs_truncate_inode(&ref, 0);
733 ext4_fs_put_inode_ref(&ref);
739 f->fsize = ext4_inode_get_size(sb, ref.inode);
740 f->inode = ref.index;
743 if (f->flags & O_APPEND)
748 r = ext4_fs_put_inode_ref(&ref);
752 /****************************************************************************/
754 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
755 bool file_expect, uint32_t *parent_inode,
760 if (ext4_parse_flags(flags, &iflags) == false)
763 if (file_expect == true)
764 filetype = EXT4_DE_REG_FILE;
766 filetype = EXT4_DE_DIR;
768 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
772 static int ext4_create_hardlink(const char *path,
773 struct ext4_inode_ref *child_ref, bool rename)
775 bool is_goal = false;
776 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
781 struct ext4_mountpoint *mp = ext4_get_mount(path);
782 struct ext4_dir_search_result result;
783 struct ext4_inode_ref ref;
788 struct ext4_fs *const fs = &mp->fs;
789 struct ext4_sblock *const sb = &mp->fs.sb;
792 path += strlen(mp->name);
795 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
799 len = ext4_path_check(path, &is_goal);
802 len = ext4_path_check(path, &is_goal);
804 /*If root open was request.*/
805 r = is_goal ? EINVAL : ENOENT;
809 r = ext4_dir_find_entry(&result, &ref, path, len);
812 /*Destroy last result*/
813 ext4_dir_destroy_result(&ref, &result);
815 if (r != ENOENT || !is_goal)
818 /*Link with root dir.*/
819 r = ext4_link(mp, &ref, child_ref, path, len, rename);
821 } else if (r == EOK && is_goal) {
822 /*Destroy last result*/
823 ext4_dir_destroy_result(&ref, &result);
828 next_inode = result.dentry->inode;
829 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
831 t = ext4_dir_en_get_inode_type(sb, result.dentry);
832 inode_mode = ext4_fs_correspond_inode_mode(t);
834 struct ext4_inode_ref child_ref;
835 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
839 inode_mode = ext4_inode_type(sb, child_ref.inode);
840 ext4_fs_put_inode_ref(&child_ref);
843 r = ext4_dir_destroy_result(&ref, &result);
847 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
848 r = is_goal ? EEXIST : ENOENT;
852 r = ext4_fs_put_inode_ref(&ref);
856 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
867 ext4_fs_put_inode_ref(&ref);
871 r = ext4_fs_put_inode_ref(&ref);
875 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
876 struct ext4_inode_ref *parent_ref,
877 struct ext4_inode_ref *child_ref)
882 struct ext4_mountpoint *mp = ext4_get_mount(path);
890 len = ext4_path_check(path, &is_goal);
892 /* Remove entry from parent directory */
893 r = ext4_dir_remove_entry(parent_ref, path, len);
897 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
898 EXT4_INODE_MODE_DIRECTORY)) {
899 ext4_fs_inode_links_count_dec(parent_ref);
900 parent_ref->dirty = true;
906 int ext4_flink(const char *path, const char *hardlink_path)
911 bool child_loaded = false;
912 uint32_t parent_inode, child_inode;
913 struct ext4_mountpoint *mp = ext4_get_mount(path);
914 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
915 struct ext4_inode_ref child_ref;
920 /* Will that happen? Anyway return EINVAL for such case. */
926 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
927 &parent_inode, &name_off);
931 child_inode = f.inode;
934 /*We have file to unlink. Load it.*/
935 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
941 /* Creating hardlink for directory is not allowed. */
942 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
943 EXT4_INODE_MODE_DIRECTORY)) {
948 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
952 ext4_fs_put_inode_ref(&child_ref);
959 int ext4_frename(const char *path, const char *new_path)
964 bool parent_loaded = false, child_loaded = false;
965 uint32_t parent_inode, child_inode;
966 struct ext4_mountpoint *mp = ext4_get_mount(path);
967 struct ext4_inode_ref child_ref, parent_ref;
974 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
975 &parent_inode, &name_off);
979 child_inode = f.inode;
983 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
987 parent_loaded = true;
989 /*We have file to unlink. Load it.*/
990 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
996 r = ext4_create_hardlink(new_path, &child_ref, true);
1000 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1006 ext4_fs_put_inode_ref(&parent_ref);
1009 ext4_fs_put_inode_ref(&child_ref);
1016 /****************************************************************************/
1018 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1020 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1029 int ext4_cache_write_back(const char *path, bool on)
1031 struct ext4_mountpoint *mp = ext4_get_mount(path);
1037 ext4_block_cache_write_back(mp->fs.bdev, on);
1042 int ext4_fremove(const char *path)
1045 uint32_t parent_inode;
1050 struct ext4_inode_ref child;
1051 struct ext4_inode_ref parent;
1052 struct ext4_mountpoint *mp = ext4_get_mount(path);
1058 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1059 &parent_inode, &name_off);
1066 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1072 /*We have file to delete. Load it.*/
1073 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1075 ext4_fs_put_inode_ref(&parent);
1083 len = ext4_path_check(path, &is_goal);
1085 /*Unlink from parent*/
1086 r = ext4_unlink(mp, &parent, &child, path, len);
1090 /*Link count is zero, the inode should be freed. */
1091 if (!ext4_inode_get_links_cnt(child.inode)) {
1092 ext4_inode_set_del_time(child.inode, -1L);
1094 ext4_block_cache_write_back(mp->fs.bdev, 1);
1095 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1096 r = ext4_fs_truncate_inode(&child, 0);
1097 ext4_block_cache_write_back(mp->fs.bdev, 0);
1102 r = ext4_fs_free_inode(&child);
1108 ext4_fs_put_inode_ref(&child);
1109 ext4_fs_put_inode_ref(&parent);
1114 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1115 struct ext4_inode *inode)
1119 struct ext4_inode_ref inode_ref;
1120 struct ext4_mountpoint *mp = ext4_get_mount(path);
1128 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1138 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1144 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1145 ext4_fs_put_inode_ref(&inode_ref);
1154 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1156 struct ext4_mountpoint *mp = ext4_get_mount(path);
1163 ext4_block_cache_write_back(mp->fs.bdev, 1);
1164 r = ext4_generic_open(f, path, flags, true, 0, 0);
1165 ext4_block_cache_write_back(mp->fs.bdev, 0);
1170 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1172 struct ext4_mountpoint *mp = ext4_get_mount(path);
1179 filetype = EXT4_DE_REG_FILE;
1182 ext4_block_cache_write_back(mp->fs.bdev, 1);
1183 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1184 ext4_block_cache_write_back(mp->fs.bdev, 0);
1189 int ext4_fclose(ext4_file *f)
1191 ext4_assert(f && f->mp);
1196 f->fpos = f->fsize = 0;
1201 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1203 struct ext4_inode_ref ref;
1207 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1209 EXT4_MP_UNLOCK(f->mp);
1214 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1215 if (f->fsize <= size) {
1220 /*Start write back cache mode.*/
1221 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1225 r = ext4_fs_truncate_inode(&ref, size);
1233 /*Stop write back cache mode*/
1234 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1240 ext4_fs_put_inode_ref(&ref);
1245 int ext4_ftruncate(ext4_file *f, uint64_t size)
1248 ext4_assert(f && f->mp);
1250 if (f->flags & O_RDONLY)
1253 EXT4_MP_LOCK(f->mp);
1254 r = ext4_ftruncate_no_lock(f, size);
1255 EXT4_MP_UNLOCK(f->mp);
1259 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1262 uint32_t iblock_idx;
1263 uint32_t iblock_last;
1264 uint32_t block_size;
1266 ext4_fsblk_t fblock;
1267 ext4_fsblk_t fblock_start;
1268 uint32_t fblock_count;
1270 uint8_t *u8_buf = buf;
1272 struct ext4_block b;
1273 struct ext4_inode_ref ref;
1275 ext4_assert(f && f->mp);
1277 if (f->flags & O_WRONLY)
1283 EXT4_MP_LOCK(f->mp);
1285 struct ext4_fs *const fs = &f->mp->fs;
1286 struct ext4_sblock *const sb = &f->mp->fs.sb;
1291 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1293 EXT4_MP_UNLOCK(f->mp);
1298 f->fsize = ext4_inode_get_size(sb, ref.inode);
1300 block_size = ext4_sb_get_block_size(sb);
1301 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1303 iblock_idx = (f->fpos) / block_size;
1304 iblock_last = (f->fpos + size) / block_size;
1305 unalg = (f->fpos) % block_size;
1307 /*If the size of symlink is smaller than 60 bytes*/
1309 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1310 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1311 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1313 char *content = (char *)ref.inode->blocks;
1314 if (f->fpos < f->fsize) {
1316 if (unalg + size > f->fsize)
1317 len = f->fsize - unalg;
1318 memcpy(buf, content + unalg, len);
1330 if (size > (block_size - unalg))
1331 len = block_size - unalg;
1333 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1338 /* Do we get an unwritten range? */
1340 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1344 memcpy(u8_buf, b.data + unalg, len);
1346 r = ext4_block_set(f->mp->fs.bdev, &b);
1351 memset(u8_buf, 0, len);
1366 while (size >= block_size) {
1367 while (iblock_idx < iblock_last) {
1368 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1376 fblock_start = fblock;
1378 if ((fblock_start + fblock_count) != fblock)
1384 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1389 size -= block_size * fblock_count;
1390 u8_buf += block_size * fblock_count;
1391 f->fpos += block_size * fblock_count;
1394 *rcnt += block_size * fblock_count;
1396 fblock_start = fblock;
1401 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1406 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1410 memcpy(u8_buf, b.data, size);
1412 r = ext4_block_set(f->mp->fs.bdev, &b);
1423 ext4_fs_put_inode_ref(&ref);
1424 EXT4_MP_UNLOCK(f->mp);
1428 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1432 uint32_t iblock_last;
1433 uint32_t ifile_blocks;
1434 uint32_t block_size;
1436 uint32_t fblock_count;
1437 ext4_fsblk_t fblock;
1438 ext4_fsblk_t fblock_start;
1440 struct ext4_block b;
1441 struct ext4_inode_ref ref;
1442 const uint8_t *u8_buf = buf;
1445 ext4_assert(f && f->mp);
1447 if (f->flags & O_RDONLY)
1453 EXT4_MP_LOCK(f->mp);
1455 struct ext4_fs *const fs = &f->mp->fs;
1456 struct ext4_sblock *const sb = &f->mp->fs.sb;
1461 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1463 EXT4_MP_UNLOCK(f->mp);
1468 f->fsize = ext4_inode_get_size(sb, ref.inode);
1469 block_size = ext4_sb_get_block_size(sb);
1471 iblock_last = (f->fpos + size) / block_size;
1472 iblk_idx = (f->fpos) / block_size;
1473 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1475 unalg = (f->fpos) % block_size;
1479 if (size > (block_size - unalg))
1480 len = block_size - unalg;
1482 r = ext4_fs_init_inode_data_block_index(&ref, iblk_idx, &fblock);
1486 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1490 memcpy(b.data + unalg, u8_buf, len);
1493 r = ext4_block_set(f->mp->fs.bdev, &b);
1507 /*Start write back cache mode.*/
1508 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1514 while (size >= block_size) {
1516 while (iblk_idx < iblock_last) {
1517 if (iblk_idx < ifile_blocks) {
1518 r = ext4_fs_init_inode_data_block_index(
1519 &ref, iblk_idx, &fblock);
1523 rr = ext4_fs_append_inode_block(&ref, &fblock,
1526 /* Unable to append more blocks. But
1527 * some block might be allocated already
1535 if (!fblock_start) {
1536 fblock_start = fblock;
1539 if ((fblock_start + fblock_count) != fblock)
1545 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1550 size -= block_size * fblock_count;
1551 u8_buf += block_size * fblock_count;
1552 f->fpos += block_size * fblock_count;
1555 *wcnt += block_size * fblock_count;
1557 fblock_start = fblock;
1561 /*ext4_fs_append_inode_block has failed and no
1562 * more blocks might be written. But node size
1563 * should be updated.*/
1569 /*Stop write back cache mode*/
1570 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1576 if (iblk_idx < ifile_blocks) {
1577 r = ext4_fs_init_inode_data_block_index(&ref, iblk_idx,
1582 r = ext4_fs_append_inode_block(&ref, &fblock, &iblk_idx);
1584 /*Node size sholud be updated.*/
1588 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1592 memcpy(b.data, u8_buf, size);
1595 r = ext4_block_set(f->mp->fs.bdev, &b);
1606 if (f->fpos > f->fsize) {
1608 ext4_inode_set_size(ref.inode, f->fsize);
1613 ext4_fs_put_inode_ref(&ref);
1614 EXT4_MP_UNLOCK(f->mp);
1618 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1622 if (offset > f->fsize)
1628 if ((offset + f->fpos) > f->fsize)
1634 if (offset > f->fsize)
1637 f->fpos = f->fsize - offset;
1643 uint64_t ext4_ftell(ext4_file *f)
1648 uint64_t ext4_fsize(ext4_file *f)
1653 int ext4_chmod(const char *path, uint32_t mode)
1658 struct ext4_sblock *sb;
1659 struct ext4_inode_ref inode_ref;
1660 struct ext4_mountpoint *mp = ext4_get_mount(path);
1667 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1675 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1681 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1682 inode_ref.dirty = true;
1684 ext4_fs_put_inode_ref(&inode_ref);
1689 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1694 struct ext4_inode_ref inode_ref;
1695 struct ext4_mountpoint *mp = ext4_get_mount(path);
1702 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1709 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1715 ext4_inode_set_uid(inode_ref.inode, uid);
1716 ext4_inode_set_gid(inode_ref.inode, gid);
1717 inode_ref.dirty = true;
1719 ext4_fs_put_inode_ref(&inode_ref);
1724 int ext4_file_set_atime(const char *path, uint32_t atime)
1729 struct ext4_inode_ref inode_ref;
1730 struct ext4_mountpoint *mp = ext4_get_mount(path);
1737 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1744 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1750 ext4_inode_set_access_time(inode_ref.inode, atime);
1751 inode_ref.dirty = true;
1753 ext4_fs_put_inode_ref(&inode_ref);
1758 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1763 struct ext4_inode_ref inode_ref;
1764 struct ext4_mountpoint *mp = ext4_get_mount(path);
1771 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1778 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1784 ext4_inode_set_modif_time(inode_ref.inode, mtime);
1785 inode_ref.dirty = true;
1787 ext4_fs_put_inode_ref(&inode_ref);
1792 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1797 struct ext4_inode_ref inode_ref;
1798 struct ext4_mountpoint *mp = ext4_get_mount(path);
1805 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, 0, 0);
1812 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1818 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1819 inode_ref.dirty = true;
1821 ext4_fs_put_inode_ref(&inode_ref);
1826 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1828 struct ext4_block b;
1829 struct ext4_inode_ref ref;
1831 ext4_fsblk_t fblock;
1832 uint32_t block_size;
1835 ext4_assert(f && f->mp);
1840 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1842 EXT4_MP_UNLOCK(f->mp);
1847 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1848 if (size > block_size) {
1852 r = ext4_ftruncate_no_lock(f, 0);
1856 /*Start write back cache mode.*/
1857 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1861 /*If the size of symlink is smaller than 60 bytes*/
1862 if (size < sizeof(ref.inode->blocks)) {
1863 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1864 memcpy(ref.inode->blocks, buf, size);
1865 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1867 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1868 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1872 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1876 memcpy(b.data, buf, size);
1878 r = ext4_block_set(f->mp->fs.bdev, &b);
1883 /*Stop write back cache mode*/
1884 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1889 ext4_inode_set_size(ref.inode, size);
1897 ext4_fs_put_inode_ref(&ref);
1901 int ext4_fsymlink(const char *target, const char *path)
1903 struct ext4_mountpoint *mp = ext4_get_mount(path);
1911 filetype = EXT4_DE_SYMLINK;
1914 ext4_block_cache_write_back(mp->fs.bdev, 1);
1915 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
1917 r = ext4_fsymlink_set(&f, target, strlen(target));
1924 ext4_block_cache_write_back(mp->fs.bdev, 0);
1929 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1931 struct ext4_mountpoint *mp = ext4_get_mount(path);
1942 filetype = EXT4_DE_SYMLINK;
1945 ext4_block_cache_write_back(mp->fs.bdev, 1);
1946 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
1948 r = ext4_fread(&f, buf, bufsize, rcnt);
1955 ext4_block_cache_write_back(mp->fs.bdev, 0);
1960 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1961 const void *data, size_t data_size, bool replace)
1967 const char *dissected_name = NULL;
1968 size_t dissected_len = 0;
1969 struct ext4_xattr_ref xattr_ref;
1970 struct ext4_inode_ref inode_ref;
1971 struct ext4_mountpoint *mp = ext4_get_mount(path);
1975 dissected_name = ext4_extract_xattr_name(name, name_len,
1976 &name_index, &dissected_len);
1981 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1987 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
1991 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
1993 ext4_fs_put_inode_ref(&inode_ref);
1997 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
1998 dissected_len, data, data_size, replace);
2000 ext4_fs_put_xattr_ref(&xattr_ref);
2001 ext4_fs_put_inode_ref(&inode_ref);
2007 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2008 void *buf, size_t buf_size, size_t *data_size)
2014 const char *dissected_name = NULL;
2015 size_t dissected_len = 0;
2016 struct ext4_xattr_ref xattr_ref;
2017 struct ext4_inode_ref inode_ref;
2018 struct ext4_mountpoint *mp = ext4_get_mount(path);
2022 dissected_name = ext4_extract_xattr_name(name, name_len,
2023 &name_index, &dissected_len);
2028 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2034 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2038 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2040 ext4_fs_put_inode_ref(&inode_ref);
2044 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2045 dissected_len, buf, buf_size, data_size);
2047 ext4_fs_put_xattr_ref(&xattr_ref);
2048 ext4_fs_put_inode_ref(&inode_ref);
2054 struct ext4_listxattr_iterator {
2059 bool list_too_small;
2060 bool get_required_size;
2063 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2064 struct ext4_xattr_item *item)
2066 struct ext4_listxattr_iterator *lxi;
2067 lxi = ref->iter_arg;
2068 if (!lxi->get_required_size) {
2071 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2072 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2073 lxi->list_too_small = 1;
2074 return EXT4_XATTR_ITERATE_STOP;
2077 memcpy(lxi->list_ptr, prefix, plen);
2078 lxi->list_ptr += plen;
2079 lxi->ret_size += plen;
2081 memcpy(lxi->list_ptr, item->name, item->name_len);
2082 lxi->list_ptr[item->name_len] = 0;
2083 lxi->list_ptr += item->name_len + 1;
2085 lxi->ret_size += item->name_len + 1;
2086 return EXT4_XATTR_ITERATE_CONT;
2089 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2094 struct ext4_xattr_ref xattr_ref;
2095 struct ext4_inode_ref inode_ref;
2096 struct ext4_listxattr_iterator lxi;
2097 struct ext4_mountpoint *mp = ext4_get_mount(path);
2102 lxi.list_ptr = list;
2105 lxi.list_too_small = false;
2106 lxi.get_required_size = (!size) ? true : false;
2109 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2115 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2119 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2121 ext4_fs_put_inode_ref(&inode_ref);
2125 xattr_ref.iter_arg = &lxi;
2126 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2127 if (lxi.list_too_small)
2132 *ret_size = lxi.ret_size;
2135 ext4_fs_put_xattr_ref(&xattr_ref);
2136 ext4_fs_put_inode_ref(&inode_ref);
2143 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2149 const char *dissected_name = NULL;
2150 size_t dissected_len = 0;
2151 struct ext4_xattr_ref xattr_ref;
2152 struct ext4_inode_ref inode_ref;
2153 struct ext4_mountpoint *mp = ext4_get_mount(path);
2157 dissected_name = ext4_extract_xattr_name(name, name_len,
2158 &name_index, &dissected_len);
2163 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2169 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2173 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2175 ext4_fs_put_inode_ref(&inode_ref);
2179 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2182 ext4_fs_put_xattr_ref(&xattr_ref);
2183 ext4_fs_put_inode_ref(&inode_ref);
2190 /*********************************DIRECTORY OPERATION************************/
2192 int ext4_dir_rm(const char *path)
2198 struct ext4_mountpoint *mp = ext4_get_mount(path);
2199 struct ext4_inode_ref act;
2200 struct ext4_inode_ref child;
2201 struct ext4_dir_iter it;
2205 uint32_t inode_current;
2217 struct ext4_fs *const fs = &mp->fs;
2220 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2227 len = ext4_path_check(path, &is_goal);
2229 inode_current = f.inode;
2232 ext4_block_cache_write_back(mp->fs.bdev, 1);
2235 /*Load directory node.*/
2236 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2241 /*Initialize iterator.*/
2242 r = ext4_dir_iterator_init(&it, &act, 0);
2244 ext4_fs_put_inode_ref(&act);
2255 /*Get up directory inode when ".." entry*/
2256 if ((it.curr->name_len == 2) &&
2257 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2258 inode_up = ext4_dir_en_get_inode(it.curr);
2261 /*If directory or file entry, but not "." ".." entry*/
2262 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2264 /*Get child inode reference do unlink
2267 cinode = ext4_dir_en_get_inode(it.curr);
2268 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2272 /*If directory with no leaf children*/
2273 r = ext4_has_children(&has_children, &child);
2275 ext4_fs_put_inode_ref(&child);
2280 /*Has directory children. Go into this
2282 inode_up = inode_current;
2283 inode_current = cinode;
2285 ext4_fs_put_inode_ref(&child);
2289 /*No children in child directory or file. Just
2291 r = ext4_unlink(f.mp, &act, &child,
2292 (char *)it.curr->name,
2295 ext4_fs_put_inode_ref(&child);
2299 ext4_inode_set_del_time(child.inode, -1L);
2300 ext4_inode_set_links_cnt(child.inode, 0);
2303 r = ext4_fs_truncate_inode(&child, 0);
2305 ext4_fs_put_inode_ref(&child);
2309 r = ext4_fs_free_inode(&child);
2311 ext4_fs_put_inode_ref(&child);
2315 r = ext4_fs_put_inode_ref(&child);
2320 r = ext4_dir_iterator_next(&it);
2324 /*Directory iterator reached last entry*/
2325 ext4_has_children(&has_children, &act);
2326 if (!has_children) {
2327 inode_current = inode_up;
2334 struct ext4_inode_ref parent;
2335 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2340 /* In this place all directories should be
2342 * Last unlink from root of current directory*/
2343 r = ext4_unlink(f.mp, &parent, &act,
2346 ext4_fs_put_inode_ref(&parent);
2350 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2351 ext4_inode_set_del_time(act.inode, -1L);
2352 ext4_inode_set_links_cnt(act.inode, 0);
2355 r = ext4_fs_truncate_inode(&act, 0);
2357 ext4_fs_put_inode_ref(&parent);
2361 r = ext4_fs_free_inode(&act);
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(&act);
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_dir_iter 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.curr, sizeof(ext4_direntry));
2471 ext4_dir_iterator_next(&it);
2473 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2475 ext4_dir_iterator_fini(&it);
2476 ext4_fs_put_inode_ref(&dir);
2479 EXT4_MP_UNLOCK(d->f.mp);
2483 void ext4_dir_entry_rewind(ext4_dir *d)