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) &&
203 #if CONFIG_DIR_INDEX_ENABLE
204 /* Initialize directory index if supported */
205 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
206 rc = ext4_dir_dx_init(child, parent);
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_dir_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 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
590 struct ext4_mountpoint *mp = ext4_get_mount(path);
591 struct ext4_dir_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);
676 if (ext4_sb_feature_incom(&mp->fs.sb, EXT4_FINCOM_FILETYPE)) {
678 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
680 inode_mode = ext4_fs_correspond_inode_mode(inode_type);
682 struct ext4_inode_ref child_ref;
683 r = ext4_fs_get_inode_ref(&mp->fs, next_inode,
688 inode_mode = ext4_inode_type(&mp->fs.sb,
691 ext4_fs_put_inode_ref(&child_ref);
694 r = ext4_dir_destroy_result(&ref, &result);
698 /*If expected file error*/
699 if (inode_mode != EXT4_INODE_MODE_DIRECTORY &&
704 if (filetype != EXT4_DIRENTRY_UNKNOWN) {
705 if ((inode_mode != ext4_fs_correspond_inode_mode(filetype)) &&
712 r = ext4_fs_put_inode_ref(&ref);
716 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
726 *name_off += len + 1;
730 ext4_fs_put_inode_ref(&ref);
736 if ((f->flags & O_TRUNC) &&
737 (inode_mode == EXT4_INODE_MODE_FILE)) {
739 r = ext4_fs_truncate_inode(&ref, 0);
741 ext4_fs_put_inode_ref(&ref);
747 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
748 f->inode = ref.index;
751 if (f->flags & O_APPEND)
756 r = ext4_fs_put_inode_ref(&ref);
760 /****************************************************************************/
762 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
763 bool file_expect, uint32_t *parent_inode,
768 if (ext4_parse_flags(flags, &iflags) == false)
771 if (file_expect == true)
772 filetype = EXT4_DIRENTRY_REG_FILE;
774 filetype = EXT4_DIRENTRY_DIR;
776 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
780 static int __ext4_create_hardlink(const char *path,
781 struct ext4_inode_ref *child_ref,
784 bool is_goal = false;
785 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
789 struct ext4_mountpoint *mp = ext4_get_mount(path);
790 struct ext4_dir_search_result result;
791 struct ext4_inode_ref ref;
797 path += strlen(mp->name);
800 r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
805 int len = ext4_path_check(path, &is_goal);
809 len = ext4_path_check(path, &is_goal);
812 /*If root open was request.*/
820 r = ext4_dir_find_entry(&result, &ref, path, len);
823 /*Destroy last result*/
824 ext4_dir_destroy_result(&ref, &result);
826 if (r != ENOENT || !is_goal)
829 /*Link with root dir.*/
830 r = ext4_link(mp, &ref, child_ref, path, len, rename);
832 } else if (r == EOK && is_goal) {
833 /*Destroy last result*/
834 ext4_dir_destroy_result(&ref, &result);
839 next_inode = result.dentry->inode;
840 if (ext4_sb_feature_incom(&mp->fs.sb, EXT4_FINCOM_FILETYPE)) {
842 ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
844 inode_mode = ext4_fs_correspond_inode_mode(inode_type);
846 struct ext4_inode_ref child_ref;
847 r = ext4_fs_get_inode_ref(&mp->fs, next_inode,
852 inode_mode = ext4_inode_type(&mp->fs.sb,
855 ext4_fs_put_inode_ref(&child_ref);
858 r = ext4_dir_destroy_result(&ref, &result);
862 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
871 r = ext4_fs_put_inode_ref(&ref);
875 r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
886 ext4_fs_put_inode_ref(&ref);
890 r = ext4_fs_put_inode_ref(&ref);
894 static int ext4_remove_orig_reference(const char *path,
896 struct ext4_inode_ref *parent_ref,
897 struct ext4_inode_ref *child_ref)
902 struct ext4_mountpoint *mp = ext4_get_mount(path);
910 len = ext4_path_check(path, &is_goal);
912 /* Remove entry from parent directory */
913 r = ext4_dir_remove_entry(parent_ref, path, len);
917 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
918 EXT4_INODE_MODE_DIRECTORY)) {
919 ext4_fs_inode_links_count_dec(parent_ref);
920 parent_ref->dirty = true;
926 int ext4_flink(const char *path, const char *hardlink_path)
931 bool child_loaded = false;
932 uint32_t parent_inode, child_inode;
933 struct ext4_mountpoint *mp = ext4_get_mount(path);
934 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
935 struct ext4_inode_ref child_ref;
940 /* Will that happen? Anyway return EINVAL for such case. */
946 r = ext4_generic_open2(&f, path, O_RDONLY,
947 EXT4_DIRENTRY_UNKNOWN,
948 &parent_inode, &name_off);
952 child_inode = f.inode;
955 /*We have file to unlink. Load it.*/
956 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
962 /* Creating hardlink for directory is not allowed. */
963 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
964 EXT4_INODE_MODE_DIRECTORY)) {
969 r = __ext4_create_hardlink(hardlink_path, &child_ref, false);
973 ext4_fs_put_inode_ref(&child_ref);
980 int ext4_frename(const char *path, const char *new_path)
985 bool parent_loaded = false, child_loaded = false;
986 uint32_t parent_inode, child_inode;
987 struct ext4_mountpoint *mp = ext4_get_mount(path);
988 struct ext4_inode_ref child_ref, parent_ref;
995 r = ext4_generic_open2(&f, path, O_RDONLY,
996 EXT4_DIRENTRY_UNKNOWN,
997 &parent_inode, &name_off);
1001 child_inode = f.inode;
1005 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1009 parent_loaded = true;
1011 /*We have file to unlink. Load it.*/
1012 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1016 child_loaded = true;
1018 r = __ext4_create_hardlink(new_path, &child_ref, true);
1022 r = ext4_remove_orig_reference(path, name_off,
1023 &parent_ref, &child_ref);
1029 ext4_fs_put_inode_ref(&parent_ref);
1032 ext4_fs_put_inode_ref(&child_ref);
1039 /****************************************************************************/
1041 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1043 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1052 int ext4_cache_write_back(const char *path, bool on)
1054 struct ext4_mountpoint *mp = ext4_get_mount(path);
1060 ext4_block_cache_write_back(mp->fs.bdev, on);
1065 int ext4_fremove(const char *path)
1068 uint32_t parent_inode;
1073 struct ext4_inode_ref child;
1074 struct ext4_inode_ref parent;
1075 struct ext4_mountpoint *mp = ext4_get_mount(path);
1081 r = ext4_generic_open2(&f, path, O_RDWR,
1082 EXT4_DIRENTRY_UNKNOWN,
1083 &parent_inode, &name_off);
1090 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1096 /*We have file to delete. Load it.*/
1097 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1099 ext4_fs_put_inode_ref(&parent);
1107 len = ext4_path_check(path, &is_goal);
1109 /*Unlink from parent*/
1110 r = ext4_unlink(mp, &parent, &child, path, len);
1114 /*Link count is zero, the inode should be freed. */
1115 if (!ext4_inode_get_links_count(child.inode)) {
1116 ext4_inode_set_deletion_time(child.inode, 0xFFFFFFFF);
1118 ext4_block_cache_write_back(mp->fs.bdev, 1);
1119 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1120 r = ext4_fs_truncate_inode(&child, 0);
1121 ext4_block_cache_write_back(mp->fs.bdev, 0);
1126 r = ext4_fs_free_inode(&child);
1132 ext4_fs_put_inode_ref(&child);
1133 ext4_fs_put_inode_ref(&parent);
1138 int ext4_fill_raw_inode(const char *path,
1140 struct ext4_inode *inode)
1144 struct ext4_inode_ref inode_ref;
1145 struct ext4_mountpoint *mp = ext4_get_mount(path);
1153 r = ext4_generic_open2(&f, path, O_RDONLY,
1154 EXT4_DIRENTRY_UNKNOWN,
1165 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1171 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1173 ext4_fs_put_inode_ref(&inode_ref);
1182 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1184 struct ext4_mountpoint *mp = ext4_get_mount(path);
1191 ext4_block_cache_write_back(mp->fs.bdev, 1);
1192 r = ext4_generic_open(f, path, flags, true, 0, 0);
1193 ext4_block_cache_write_back(mp->fs.bdev, 0);
1198 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1200 struct ext4_mountpoint *mp = ext4_get_mount(path);
1207 filetype = EXT4_DIRENTRY_REG_FILE;
1210 ext4_block_cache_write_back(mp->fs.bdev, 1);
1211 r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
1212 ext4_block_cache_write_back(mp->fs.bdev, 0);
1217 int ext4_fclose(ext4_file *f)
1219 ext4_assert(f && f->mp);
1224 f->fpos = f->fsize = 0;
1229 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1231 struct ext4_inode_ref ref;
1235 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1237 EXT4_MP_UNLOCK(f->mp);
1242 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1243 if (f->fsize <= size) {
1248 /*Start write back cache mode.*/
1249 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1253 r = ext4_fs_truncate_inode(&ref, size);
1261 /*Stop write back cache mode*/
1262 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1268 ext4_fs_put_inode_ref(&ref);
1273 int ext4_ftruncate(ext4_file *f, uint64_t size)
1276 ext4_assert(f && f->mp);
1278 if (f->flags & O_RDONLY)
1281 EXT4_MP_LOCK(f->mp);
1283 r = ext4_ftruncate_no_lock(f, size);
1285 EXT4_MP_UNLOCK(f->mp);
1289 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1292 uint32_t iblock_idx;
1293 uint32_t iblock_last;
1294 uint32_t block_size;
1296 ext4_fsblk_t fblock;
1297 ext4_fsblk_t fblock_start;
1298 uint32_t fblock_count;
1300 uint8_t *u8_buf = buf;
1302 struct ext4_block b;
1303 struct ext4_inode_ref ref;
1305 ext4_assert(f && f->mp);
1307 if (f->flags & O_WRONLY)
1313 EXT4_MP_LOCK(f->mp);
1318 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1320 EXT4_MP_UNLOCK(f->mp);
1325 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1327 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1328 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1330 iblock_idx = (f->fpos) / block_size;
1331 iblock_last = (f->fpos + size) / block_size;
1332 unaligned = (f->fpos) % block_size;
1334 /*If the size of symlink is smaller than 60 bytes*/
1335 if (ext4_inode_is_type(&f->mp->fs.sb, ref.inode, EXT4_INODE_MODE_SOFTLINK)
1336 && f->fsize < sizeof(ref.inode->blocks)
1337 && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
1338 char *content = (char *)ref.inode->blocks;
1339 if (f->fpos < f->fsize) {
1340 r = (unaligned + size > f->fsize)
1341 ?(f->fsize - unaligned)
1343 memcpy(buf, content + unaligned, r);
1359 uint32_t l = size > (block_size - unaligned) ?
1360 (block_size - unaligned) : size;
1361 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1366 /* Do we get an unwritten range? */
1368 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1372 memcpy(u8_buf, b.data + unaligned, l);
1374 r = ext4_block_set(f->mp->fs.bdev, &b);
1379 memset(u8_buf, 0, l);
1394 while (size >= block_size) {
1395 while (iblock_idx < iblock_last) {
1396 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx,
1404 fblock_start = fblock;
1406 if ((fblock_start + fblock_count) != fblock)
1412 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1417 size -= block_size * fblock_count;
1418 u8_buf += block_size * fblock_count;
1419 f->fpos += block_size * fblock_count;
1422 *rcnt += block_size * fblock_count;
1424 fblock_start = fblock;
1429 r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
1434 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1438 memcpy(u8_buf, b.data, size);
1440 r = ext4_block_set(f->mp->fs.bdev, &b);
1451 ext4_fs_put_inode_ref(&ref);
1452 EXT4_MP_UNLOCK(f->mp);
1456 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1459 uint32_t iblock_idx;
1460 uint32_t iblock_last;
1461 uint32_t ifile_blocks;
1462 uint32_t block_size;
1464 uint32_t fblock_count;
1465 ext4_fsblk_t fblock;
1466 ext4_fsblk_t fblock_start;
1468 struct ext4_block b;
1469 struct ext4_inode_ref ref;
1470 const uint8_t *u8_buf = buf;
1473 ext4_assert(f && f->mp);
1475 if (f->flags & O_RDONLY)
1481 EXT4_MP_LOCK(f->mp);
1486 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1488 EXT4_MP_UNLOCK(f->mp);
1493 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1494 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1496 iblock_last = (f->fpos + size) / block_size;
1497 iblock_idx = (f->fpos) / block_size;
1498 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1500 unaligned = (f->fpos) % block_size;
1503 uint32_t l = size > (block_size - unaligned) ?
1504 (block_size - unaligned) : size;
1505 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx, &fblock);
1509 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1513 memcpy(b.data + unaligned, u8_buf, l);
1516 r = ext4_block_set(f->mp->fs.bdev, &b);
1530 /*Start write back cache mode.*/
1531 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1537 while (size >= block_size) {
1539 while (iblock_idx < iblock_last) {
1540 if (iblock_idx < ifile_blocks) {
1541 r = ext4_fs_init_inode_data_block_index(
1542 &ref, iblock_idx, &fblock);
1546 rr = ext4_fs_append_inode_block(&ref, &fblock,
1549 /* Unable to append more blocks. But some
1550 * block might be allocated already.
1558 if (!fblock_start) {
1559 fblock_start = fblock;
1562 if ((fblock_start + fblock_count) != fblock)
1568 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1573 size -= block_size * fblock_count;
1574 u8_buf += block_size * fblock_count;
1575 f->fpos += block_size * fblock_count;
1578 *wcnt += block_size * fblock_count;
1580 fblock_start = fblock;
1584 /*ext4_fs_append_inode_block has failed and no
1585 * more blocks might be written. But node size
1586 * should be updated.*/
1592 /*Stop write back cache mode*/
1593 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1599 if (iblock_idx < ifile_blocks) {
1600 r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx,
1605 r = ext4_fs_append_inode_block(&ref, &fblock, &iblock_idx);
1607 /*Node size sholud be updated.*/
1611 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1615 memcpy(b.data, u8_buf, size);
1618 r = ext4_block_set(f->mp->fs.bdev, &b);
1629 if (f->fpos > f->fsize) {
1631 ext4_inode_set_size(ref.inode, f->fsize);
1636 ext4_fs_put_inode_ref(&ref);
1637 EXT4_MP_UNLOCK(f->mp);
1641 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1645 if (offset > f->fsize)
1651 if ((offset + f->fpos) > f->fsize)
1657 if (offset > f->fsize)
1660 f->fpos = f->fsize - offset;
1666 uint64_t ext4_ftell(ext4_file *f) { return f->fpos; }
1668 uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
1670 int ext4_chmod(const char *path, uint32_t mode)
1675 struct ext4_sblock *sb;
1676 struct ext4_inode_ref inode_ref;
1677 struct ext4_mountpoint *mp = ext4_get_mount(path);
1684 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1692 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1698 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1699 inode_ref.dirty = true;
1701 ext4_fs_put_inode_ref(&inode_ref);
1706 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1711 struct ext4_inode_ref inode_ref;
1712 struct ext4_mountpoint *mp = ext4_get_mount(path);
1719 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1726 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1732 ext4_inode_set_uid(inode_ref.inode, uid);
1733 ext4_inode_set_gid(inode_ref.inode, gid);
1734 inode_ref.dirty = true;
1736 ext4_fs_put_inode_ref(&inode_ref);
1741 int ext4_file_set_atime(const char *path, uint32_t atime)
1746 struct ext4_inode_ref inode_ref;
1747 struct ext4_mountpoint *mp = ext4_get_mount(path);
1754 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1761 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1767 ext4_inode_set_access_time(inode_ref.inode, atime);
1768 inode_ref.dirty = true;
1770 ext4_fs_put_inode_ref(&inode_ref);
1775 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1780 struct ext4_inode_ref inode_ref;
1781 struct ext4_mountpoint *mp = ext4_get_mount(path);
1788 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1795 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1801 ext4_inode_set_modification_time(inode_ref.inode, mtime);
1802 inode_ref.dirty = true;
1804 ext4_fs_put_inode_ref(&inode_ref);
1809 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1814 struct ext4_inode_ref inode_ref;
1815 struct ext4_mountpoint *mp = ext4_get_mount(path);
1822 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
1829 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1835 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1836 inode_ref.dirty = true;
1838 ext4_fs_put_inode_ref(&inode_ref);
1843 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1845 struct ext4_block b;
1846 struct ext4_inode_ref ref;
1848 ext4_fsblk_t fblock;
1849 uint32_t block_size;
1852 ext4_assert(f && f->mp);
1857 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1859 EXT4_MP_UNLOCK(f->mp);
1864 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1865 if (size > block_size) {
1869 r = ext4_ftruncate_no_lock(f, 0);
1873 /*Start write back cache mode.*/
1874 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1878 /*If the size of symlink is smaller than 60 bytes*/
1879 if (size < sizeof(ref.inode->blocks)) {
1880 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1881 memcpy(ref.inode->blocks, buf, size);
1882 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1884 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1885 r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
1889 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1893 memcpy(b.data, buf, size);
1895 r = ext4_block_set(f->mp->fs.bdev, &b);
1900 /*Stop write back cache mode*/
1901 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1906 ext4_inode_set_size(ref.inode, size);
1914 ext4_fs_put_inode_ref(&ref);
1918 int ext4_fsymlink(const char *target, const char *path)
1920 struct ext4_mountpoint *mp = ext4_get_mount(path);
1928 filetype = EXT4_DIRENTRY_SYMLINK;
1931 ext4_block_cache_write_back(mp->fs.bdev, 1);
1932 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
1934 r = ext4_fsymlink_set(&f, target, strlen(target));
1941 ext4_block_cache_write_back(mp->fs.bdev, 0);
1946 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1948 struct ext4_mountpoint *mp = ext4_get_mount(path);
1959 filetype = EXT4_DIRENTRY_SYMLINK;
1962 ext4_block_cache_write_back(mp->fs.bdev, 1);
1963 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, 0, 0);
1965 r = ext4_fread(&f, buf, bufsize, rcnt);
1972 ext4_block_cache_write_back(mp->fs.bdev, 0);
1977 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1978 const void *data, size_t data_size, bool replace)
1984 const char *dissected_name = NULL;
1985 size_t dissected_len = 0;
1986 struct ext4_xattr_ref xattr_ref;
1987 struct ext4_inode_ref inode_ref;
1988 struct ext4_mountpoint *mp = ext4_get_mount(path);
1992 dissected_name = ext4_extract_xattr_name(name, name_len,
1993 &name_index, &dissected_len);
1998 r = ext4_generic_open2(&f, path, O_RDWR,
1999 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2005 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2009 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2012 ext4_fs_put_inode_ref(&inode_ref);
2016 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2017 dissected_len, data, data_size, replace);
2019 ext4_fs_put_xattr_ref(&xattr_ref);
2020 ext4_fs_put_inode_ref(&inode_ref);
2026 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2027 void *buf, size_t buf_size, size_t *data_size)
2033 const char *dissected_name = NULL;
2034 size_t dissected_len = 0;
2035 struct ext4_xattr_ref xattr_ref;
2036 struct ext4_inode_ref inode_ref;
2037 struct ext4_mountpoint *mp = ext4_get_mount(path);
2041 dissected_name = ext4_extract_xattr_name(name, name_len,
2042 &name_index, &dissected_len);
2047 r = ext4_generic_open2(&f, path, O_RDWR,
2048 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2054 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2058 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2061 ext4_fs_put_inode_ref(&inode_ref);
2065 r = ext4_fs_get_xattr(&xattr_ref, name_index,
2066 dissected_name, dissected_len,
2067 buf, buf_size, data_size);
2069 ext4_fs_put_xattr_ref(&xattr_ref);
2070 ext4_fs_put_inode_ref(&inode_ref);
2076 struct ext4_listxattr_iterator {
2081 bool list_too_small;
2082 bool get_required_size;
2085 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2086 struct ext4_xattr_item *item)
2088 struct ext4_listxattr_iterator *lxi;
2089 lxi = ref->iter_arg;
2090 if (!lxi->get_required_size) {
2093 prefix = ext4_get_xattr_name_prefix(item->name_index,
2096 prefix_len + item->name_len + 1 >
2099 lxi->list_too_small = 1;
2100 return EXT4_XATTR_ITERATE_STOP;
2103 memcpy(lxi->list_ptr, prefix, prefix_len);
2104 lxi->list_ptr += prefix_len;
2105 lxi->ret_size += prefix_len;
2107 memcpy(lxi->list_ptr, item->name, item->name_len);
2108 lxi->list_ptr[item->name_len] = 0;
2109 lxi->list_ptr += item->name_len + 1;
2111 lxi->ret_size += item->name_len + 1;
2112 return EXT4_XATTR_ITERATE_CONT;
2115 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2120 struct ext4_xattr_ref xattr_ref;
2121 struct ext4_inode_ref inode_ref;
2122 struct ext4_listxattr_iterator lxi;
2123 struct ext4_mountpoint *mp = ext4_get_mount(path);
2128 lxi.list_ptr = list;
2131 lxi.list_too_small = false;
2132 lxi.get_required_size = (!size) ? true : false;
2135 r = ext4_generic_open2(&f, path, O_RDWR,
2136 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2142 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2146 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2149 ext4_fs_put_inode_ref(&inode_ref);
2153 xattr_ref.iter_arg = &lxi;
2154 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2155 if (lxi.list_too_small)
2160 *ret_size = lxi.ret_size;
2163 ext4_fs_put_xattr_ref(&xattr_ref);
2164 ext4_fs_put_inode_ref(&inode_ref);
2171 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2177 const char *dissected_name = NULL;
2178 size_t dissected_len = 0;
2179 struct ext4_xattr_ref xattr_ref;
2180 struct ext4_inode_ref inode_ref;
2181 struct ext4_mountpoint *mp = ext4_get_mount(path);
2185 dissected_name = ext4_extract_xattr_name(name, name_len,
2186 &name_index, &dissected_len);
2191 r = ext4_generic_open2(&f, path, O_RDWR,
2192 EXT4_DIRENTRY_UNKNOWN, 0, 0);
2198 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2202 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref,
2205 ext4_fs_put_inode_ref(&inode_ref);
2209 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2212 ext4_fs_put_xattr_ref(&xattr_ref);
2213 ext4_fs_put_inode_ref(&inode_ref);
2220 /*********************************DIRECTORY OPERATION************************/
2222 int ext4_dir_rm(const char *path)
2228 struct ext4_mountpoint *mp = ext4_get_mount(path);
2229 struct ext4_inode_ref current;
2230 struct ext4_inode_ref child;
2231 struct ext4_dir_iterator it;
2235 uint32_t inode_current;
2248 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2255 len = ext4_path_check(path, &is_goal);
2257 inode_current = f.inode;
2260 ext4_block_cache_write_back(mp->fs.bdev, 1);
2263 /*Load directory node.*/
2264 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, ¤t);
2269 /*Initialize iterator.*/
2270 r = ext4_dir_iterator_init(&it, ¤t, 0);
2272 ext4_fs_put_inode_ref(¤t);
2283 /*Get up directory inode when ".." entry*/
2284 if ((it.current->name_length == 2) &&
2285 ext4_is_dots(it.current->name,
2286 it.current->name_length)) {
2287 inode_up = ext4_dir_entry_ll_get_inode(it.current);
2290 /*If directory or file entry, but not "." ".." entry*/
2291 if (!ext4_is_dots(it.current->name,
2292 it.current->name_length)) {
2294 /*Get child inode reference do unlink
2296 r = ext4_fs_get_inode_ref(&f.mp->fs,
2297 ext4_dir_entry_ll_get_inode(it.current),
2302 /*If directory with no leaf children*/
2303 r = ext4_has_children(&has_children, &child);
2305 ext4_fs_put_inode_ref(&child);
2310 /*Has directory children. Go into this
2312 inode_up = inode_current;
2313 inode_current = ext4_dir_entry_ll_get_inode(it.current);
2315 ext4_fs_put_inode_ref(&child);
2319 /*No children in child directory or file. Just
2321 r = ext4_unlink(f.mp, ¤t, &child,
2322 (char *)it.current->name,
2323 it.current->name_length);
2325 ext4_fs_put_inode_ref(&child);
2329 ext4_inode_set_deletion_time(child.inode,
2331 ext4_inode_set_links_count(child.inode, 0);
2334 r = ext4_fs_truncate_inode(&child, 0);
2336 ext4_fs_put_inode_ref(&child);
2340 r = ext4_fs_free_inode(&child);
2342 ext4_fs_put_inode_ref(&child);
2346 r = ext4_fs_put_inode_ref(&child);
2351 r = ext4_dir_iterator_next(&it);
2355 /*Directory iterator reached last entry*/
2356 ext4_has_children(&has_children, ¤t);
2357 if (!has_children) {
2358 inode_current = inode_up;
2365 struct ext4_inode_ref parent;
2366 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2371 /* In this place all directories should be
2373 * Last unlink from root of current directory*/
2374 r = ext4_unlink(f.mp, &parent, ¤t,
2377 ext4_fs_put_inode_ref(&parent);
2381 if (ext4_inode_get_links_count(current.inode) ==
2383 ext4_inode_set_deletion_time(
2384 current.inode, 0xFFFFFFFF);
2385 ext4_inode_set_links_count(
2387 current.dirty = true;
2389 r = ext4_fs_truncate_inode(¤t, 0);
2391 ext4_fs_put_inode_ref(&parent);
2395 r = ext4_fs_free_inode(¤t);
2397 ext4_fs_put_inode_ref(&parent);
2402 r = ext4_fs_put_inode_ref(&parent);
2409 ext4_dir_iterator_fini(&it);
2410 ext4_fs_put_inode_ref(¤t);
2413 /*When something goes wrong. End loop.*/
2419 ext4_block_cache_write_back(mp->fs.bdev, 0);
2424 int ext4_dir_mk(const char *path)
2429 struct ext4_mountpoint *mp = ext4_get_mount(path);
2437 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2439 /*Directory already created*/
2445 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2455 int ext4_dir_open(ext4_dir *d, const char *path)
2457 struct ext4_mountpoint *mp = ext4_get_mount(path);
2464 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2470 int ext4_dir_close(ext4_dir *d)
2472 return ext4_fclose(&d->f);
2475 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2477 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2480 ext4_direntry *de = 0;
2481 struct ext4_inode_ref dir;
2482 struct ext4_dir_iterator it;
2484 EXT4_MP_LOCK(d->f.mp);
2486 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2487 EXT4_MP_UNLOCK(d->f.mp);
2491 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2496 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2498 ext4_fs_put_inode_ref(&dir);
2502 memcpy(&d->de, it.current, sizeof(ext4_direntry));
2505 ext4_dir_iterator_next(&it);
2508 it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
2510 ext4_dir_iterator_fini(&it);
2511 ext4_fs_put_inode_ref(&dir);
2514 EXT4_MP_UNLOCK(d->f.mp);
2518 void ext4_dir_entry_rewind(ext4_dir *d)