2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup lwext4
34 * @brief Ext4 high level operations (file, directory, mountpoints...)
37 #include "ext4_config.h"
38 #include "ext4_types.h"
39 #include "ext4_misc.h"
40 #include "ext4_errno.h"
41 #include "ext4_oflags.h"
42 #include "ext4_debug.h"
45 #include "ext4_trans.h"
46 #include "ext4_blockdev.h"
49 #include "ext4_inode.h"
50 #include "ext4_super.h"
51 #include "ext4_dir_idx.h"
52 #include "ext4_xattr.h"
53 #include "ext4_journal.h"
59 /**@brief Mount point OS dependent lock*/
60 #define EXT4_MP_LOCK(_m) \
63 (_m)->os_locks->lock(); \
66 /**@brief Mount point OS dependent unlock*/
67 #define EXT4_MP_UNLOCK(_m) \
70 (_m)->os_locks->unlock(); \
73 /**@brief Mount point descriptor.*/
74 struct ext4_mountpoint {
76 /**@brief Mount done flag.*/
79 /**@brief Mount point name (@ref ext4_mount)*/
82 /**@brief OS dependent lock/unlock functions.*/
83 const struct ext4_lock *os_locks;
85 /**@brief Ext4 filesystem internals.*/
88 /**@brief Dynamic allocation cache flag.*/
92 struct jbd_journal jbd_journal;
95 /**@brief Block devices descriptor.*/
96 struct _ext4_devices {
98 /**@brief Block device name (@ref ext4_device_register)*/
101 /**@brief Block device handle.*/
102 struct ext4_blockdev *bd;
104 /**@brief Block cache handle.*/
105 struct ext4_bcache *bc;
108 /**@brief Block devices.*/
109 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
111 /**@brief Mountpoints.*/
112 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
114 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
115 const char *dev_name)
118 ext4_assert(bd && dev_name);
120 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
121 if (!_bdevices[i].bd) {
122 strcpy(_bdevices[i].name, dev_name);
123 _bdevices[i].bd = bd;
124 _bdevices[i].bc = bc;
128 if (!strcmp(_bdevices[i].name, dev_name))
134 /****************************************************************************/
136 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
138 if ((name_size == 1) && (name[0] == '.'))
141 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
147 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
149 struct ext4_sblock *sb = &enode->fs->sb;
151 /* Check if node is directory */
152 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
153 *has_children = false;
157 struct ext4_dir_iter it;
158 int rc = ext4_dir_iterator_init(&it, enode, 0);
162 /* Find a non-empty directory entry */
164 while (it.curr != NULL) {
165 if (ext4_dir_en_get_inode(it.curr) != 0) {
167 nsize = ext4_dir_en_get_name_len(sb, it.curr);
168 if (!ext4_is_dots(it.curr->name, nsize)) {
174 rc = ext4_dir_iterator_next(&it);
176 ext4_dir_iterator_fini(&it);
181 rc = ext4_dir_iterator_fini(&it);
185 *has_children = found;
190 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
191 struct ext4_inode_ref *ch, const char *n,
192 uint32_t len, bool rename)
194 /* Check maximum name length */
195 if (len > EXT4_DIRECTORY_FILENAME_LEN)
198 /* Add entry to parent directory */
199 int r = ext4_dir_add_entry(parent, n, len, ch);
203 /* Fill new dir -> add '.' and '..' entries.
204 * Also newly allocated inode should have 0 link count.
207 bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
208 EXT4_INODE_MODE_DIRECTORY);
209 if (is_dir && !rename) {
211 #if CONFIG_DIR_INDEX_ENABLE
212 /* Initialize directory index if supported */
213 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
214 r = ext4_dir_dx_init(ch, parent);
218 ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
223 r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
225 ext4_dir_remove_entry(parent, n, strlen(n));
229 r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
231 ext4_dir_remove_entry(parent, n, strlen(n));
232 ext4_dir_remove_entry(ch, ".", strlen("."));
237 /*New empty directory. Two links (. and ..) */
238 ext4_inode_set_links_cnt(ch->inode, 2);
239 ext4_fs_inode_links_count_inc(parent);
241 parent->dirty = true;
245 * In case we want to rename a directory,
246 * we reset the original '..' pointer.
250 idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
251 struct ext4_dir_search_result res;
253 r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
257 ext4_dir_en_set_inode(res.dentry, parent->index);
258 ext4_trans_set_block_dirty(res.block.buf);
259 r = ext4_dir_destroy_result(ch, &res);
264 #if CONFIG_DIR_INDEX_ENABLE
265 r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
272 ext4_fs_inode_links_count_inc(parent);
273 parent->dirty = true;
276 ext4_fs_inode_links_count_inc(ch);
283 static int ext4_unlink(struct ext4_mountpoint *mp,
284 struct ext4_inode_ref *parent,
285 struct ext4_inode_ref *child, const char *name,
289 int rc = ext4_has_children(&has_children, child);
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,
303 EXT4_INODE_MODE_DIRECTORY);
305 /* If directory - handle links from parent */
307 ext4_fs_inode_links_count_dec(parent);
308 parent->dirty = true;
312 * TODO: Update timestamps of the parent
313 * (when we have wall-clock time).
315 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
316 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
317 * parent->dirty = true;
321 * TODO: Update timestamp for inode.
323 * ext4_inode_set_change_inode_time(child->inode,
326 if (ext4_inode_get_links_cnt(child->inode)) {
327 ext4_fs_inode_links_count_dec(child);
334 /****************************************************************************/
336 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, read_only);
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);
414 ext4_bcache_cleanup(bc);
416 if (mp->cache_dynamic) {
417 ext4_bcache_fini_dynamic(bc);
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 ext4_bcache_cleanup(mp->fs.bdev->bc);
451 if (mp->cache_dynamic) {
452 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
453 free(mp->fs.bdev->bc);
455 r = ext4_block_fini(mp->fs.bdev);
457 mp->fs.bdev->fs = NULL;
461 static struct ext4_mountpoint *ext4_get_mount(const char *path)
464 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
469 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
476 static int __ext4_journal_start(const char *mount_point)
479 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
483 if (mp->fs.read_only)
486 if (ext4_sb_feature_com(&mp->fs.sb,
487 EXT4_FCOM_HAS_JOURNAL)) {
488 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
492 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
494 mp->jbd_fs.dirty = false;
495 jbd_put_fs(&mp->jbd_fs);
498 mp->fs.jbd_fs = &mp->jbd_fs;
499 mp->fs.jbd_journal = &mp->jbd_journal;
506 static int __ext4_journal_stop(const char *mount_point)
509 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
513 if (mp->fs.read_only)
516 if (ext4_sb_feature_com(&mp->fs.sb,
517 EXT4_FCOM_HAS_JOURNAL)) {
518 r = jbd_journal_stop(&mp->jbd_journal);
520 mp->jbd_fs.dirty = false;
521 jbd_put_fs(&mp->jbd_fs);
522 mp->fs.jbd_journal = NULL;
523 mp->fs.jbd_fs = NULL;
527 r = jbd_put_fs(&mp->jbd_fs);
529 mp->fs.jbd_journal = NULL;
530 mp->fs.jbd_fs = NULL;
534 mp->fs.jbd_journal = NULL;
535 mp->fs.jbd_fs = NULL;
542 static int __ext4_recover(const char *mount_point)
544 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
550 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
551 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
558 r = jbd_get_fs(&mp->fs, jbd_fs);
564 r = jbd_recover(jbd_fs);
576 static int __ext4_trans_start(struct ext4_mountpoint *mp)
579 if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
580 struct jbd_journal *journal = mp->fs.jbd_journal;
581 struct jbd_trans *trans;
582 trans = jbd_journal_new_trans(journal);
587 mp->fs.curr_trans = trans;
594 static int __ext4_trans_stop(struct ext4_mountpoint *mp)
597 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
598 struct jbd_journal *journal = mp->fs.jbd_journal;
599 struct jbd_trans *trans = mp->fs.curr_trans;
600 r = jbd_journal_commit_trans(journal, trans);
601 mp->fs.curr_trans = NULL;
607 static void __ext4_trans_abort(struct ext4_mountpoint *mp)
609 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
610 struct jbd_journal *journal = mp->fs.jbd_journal;
611 struct jbd_trans *trans = mp->fs.curr_trans;
612 jbd_journal_free_trans(journal, trans, true);
613 mp->fs.curr_trans = NULL;
617 int ext4_journal_start(const char *mount_point __unused)
620 #if CONFIG_JOURNALING_ENABLE
621 r = __ext4_journal_start(mount_point);
626 int ext4_journal_stop(const char *mount_point __unused)
629 #if CONFIG_JOURNALING_ENABLE
630 r = __ext4_journal_stop(mount_point);
635 int ext4_recover(const char *mount_point __unused)
638 #if CONFIG_JOURNALING_ENABLE
639 r = __ext4_recover(mount_point);
644 static int ext4_trans_start(struct ext4_mountpoint *mp __unused)
647 #if CONFIG_JOURNALING_ENABLE
648 r = __ext4_trans_start(mp);
653 static int ext4_trans_stop(struct ext4_mountpoint *mp __unused)
656 #if CONFIG_JOURNALING_ENABLE
657 r = __ext4_trans_stop(mp);
662 static void ext4_trans_abort(struct ext4_mountpoint *mp __unused)
664 #if CONFIG_JOURNALING_ENABLE
665 __ext4_trans_abort(mp);
670 int ext4_mount_point_stats(const char *mount_point,
671 struct ext4_mount_stats *stats)
673 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
679 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
680 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
681 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
682 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
683 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
685 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
686 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
687 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
689 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
695 int ext4_mount_setup_locks(const char *mount_point,
696 const struct ext4_lock *locks)
699 struct ext4_mountpoint *mp = 0;
701 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
702 if (!strcmp(_mp[i].name, mount_point)) {
710 mp->os_locks = locks;
714 /********************************FILE OPERATIONS*****************************/
716 static int ext4_path_check(const char *path, bool *is_goal)
720 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
722 if (path[i] == '/') {
736 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
741 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
742 *file_flags = O_RDONLY;
746 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
747 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
751 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
752 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
756 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
757 !strcmp(flags, "r+b")) {
758 *file_flags = O_RDWR;
762 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
763 !strcmp(flags, "w+b")) {
764 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
768 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
769 !strcmp(flags, "a+b")) {
770 *file_flags = O_RDWR | O_CREAT | O_APPEND;
777 static int ext4_trunc_inode(struct ext4_mountpoint *mp,
778 uint32_t index, uint64_t new_size)
781 struct ext4_fs *const fs = &mp->fs;
782 struct ext4_inode_ref inode_ref;
784 bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
785 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
789 inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
790 ext4_fs_put_inode_ref(&inode_ref);
794 while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
796 inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
798 ext4_trans_start(mp);
799 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
801 ext4_trans_abort(mp);
804 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
806 ext4_fs_put_inode_ref(&inode_ref);
808 r = ext4_fs_put_inode_ref(&inode_ref);
811 ext4_trans_abort(mp);
817 if (inode_size > new_size) {
819 inode_size = new_size;
821 ext4_trans_start(mp);
822 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
824 ext4_trans_abort(mp);
827 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
829 ext4_fs_put_inode_ref(&inode_ref);
831 r = ext4_fs_put_inode_ref(&inode_ref);
834 ext4_trans_abort(mp);
843 ext4_trans_start(mp);
848 static int ext4_trunc_dir(struct ext4_mountpoint *mp,
849 struct ext4_inode_ref *parent,
850 struct ext4_inode_ref *dir)
853 bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
854 EXT4_INODE_MODE_DIRECTORY);
855 uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
859 #if CONFIG_DIR_INDEX_ENABLE
860 /* Initialize directory index if supported */
861 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
862 r = ext4_dir_dx_init(dir, parent);
866 r = ext4_trunc_inode(mp, dir->index,
867 EXT4_DIR_DX_INIT_BCNT * block_size);
873 r = ext4_trunc_inode(mp, dir->index, block_size);
878 return ext4_fs_truncate_inode(dir, 0);
882 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
883 * any filetype of the target dir entry will be accepted.
885 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
886 int ftype, uint32_t *parent_inode,
889 bool is_goal = false;
890 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
895 struct ext4_mountpoint *mp = ext4_get_mount(path);
896 struct ext4_dir_search_result result;
897 struct ext4_inode_ref ref;
904 struct ext4_fs *const fs = &mp->fs;
905 struct ext4_sblock *const sb = &mp->fs.sb;
907 if (fs->read_only && flags & O_CREAT)
913 path += strlen(mp->name);
916 *name_off = strlen(mp->name);
919 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
924 *parent_inode = ref.index;
927 ext4_trans_start(mp);
929 len = ext4_path_check(path, &is_goal);
932 len = ext4_path_check(path, &is_goal);
934 /*If root open was request.*/
935 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
943 r = ext4_dir_find_entry(&result, &ref, path, len);
946 /*Destroy last result*/
947 ext4_dir_destroy_result(&ref, &result);
951 if (!(f->flags & O_CREAT))
954 /*O_CREAT allows create new entry*/
955 struct ext4_inode_ref child_ref;
956 r = ext4_fs_alloc_inode(fs, &child_ref,
957 is_goal ? ftype : EXT4_DE_DIR);
962 /*Link with root dir.*/
963 r = ext4_link(mp, &ref, &child_ref, path, len, false);
965 /*Fail. Free new inode.*/
966 ext4_fs_free_inode(&child_ref);
967 /*We do not want to write new inode.
968 But block has to be released.*/
969 child_ref.dirty = false;
970 ext4_fs_put_inode_ref(&child_ref);
974 ext4_fs_put_inode_ref(&child_ref);
979 *parent_inode = ref.index;
981 next_inode = ext4_dir_en_get_inode(result.dentry);
982 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
984 t = ext4_dir_en_get_inode_type(sb, result.dentry);
985 imode = ext4_fs_correspond_inode_mode(t);
987 struct ext4_inode_ref child_ref;
988 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
992 imode = ext4_inode_type(sb, child_ref.inode);
993 ext4_fs_put_inode_ref(&child_ref);
996 r = ext4_dir_destroy_result(&ref, &result);
1000 /*If expected file error*/
1001 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
1005 if (ftype != EXT4_DE_UNKNOWN) {
1006 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
1007 if (df && is_goal) {
1013 r = ext4_fs_put_inode_ref(&ref);
1017 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1027 *name_off += len + 1;
1031 ext4_fs_put_inode_ref(&ref);
1037 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
1038 r = ext4_trunc_inode(mp, ref.index, 0);
1040 ext4_fs_put_inode_ref(&ref);
1046 f->fsize = ext4_inode_get_size(sb, ref.inode);
1047 f->inode = ref.index;
1050 if (f->flags & O_APPEND)
1055 r = ext4_fs_put_inode_ref(&ref);
1056 if (flags & O_CREAT) {
1058 ext4_trans_stop(mp);
1060 ext4_trans_abort(mp);
1067 /****************************************************************************/
1069 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
1070 bool file_expect, uint32_t *parent_inode,
1075 if (ext4_parse_flags(flags, &iflags) == false)
1078 if (file_expect == true)
1079 filetype = EXT4_DE_REG_FILE;
1081 filetype = EXT4_DE_DIR;
1083 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
1087 static int ext4_create_hardlink(const char *path,
1088 struct ext4_inode_ref *child_ref, bool rename)
1090 bool is_goal = false;
1091 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
1092 uint32_t next_inode;
1096 struct ext4_mountpoint *mp = ext4_get_mount(path);
1097 struct ext4_dir_search_result result;
1098 struct ext4_inode_ref ref;
1103 struct ext4_fs *const fs = &mp->fs;
1104 struct ext4_sblock *const sb = &mp->fs.sb;
1106 /*Skip mount point*/
1107 path += strlen(mp->name);
1110 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
1114 len = ext4_path_check(path, &is_goal);
1117 len = ext4_path_check(path, &is_goal);
1119 /*If root open was request.*/
1120 r = is_goal ? EINVAL : ENOENT;
1124 r = ext4_dir_find_entry(&result, &ref, path, len);
1127 /*Destroy last result*/
1128 ext4_dir_destroy_result(&ref, &result);
1130 if (r != ENOENT || !is_goal)
1133 /*Link with root dir.*/
1134 r = ext4_link(mp, &ref, child_ref, path, len, rename);
1136 } else if (r == EOK && is_goal) {
1137 /*Destroy last result*/
1138 ext4_dir_destroy_result(&ref, &result);
1143 next_inode = result.dentry->inode;
1144 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1146 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1147 inode_mode = ext4_fs_correspond_inode_mode(t);
1149 struct ext4_inode_ref child_ref;
1150 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1154 inode_mode = ext4_inode_type(sb, child_ref.inode);
1155 ext4_fs_put_inode_ref(&child_ref);
1158 r = ext4_dir_destroy_result(&ref, &result);
1162 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1163 r = is_goal ? EEXIST : ENOENT;
1167 r = ext4_fs_put_inode_ref(&ref);
1171 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1182 ext4_fs_put_inode_ref(&ref);
1186 r = ext4_fs_put_inode_ref(&ref);
1190 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1191 struct ext4_inode_ref *parent_ref,
1192 struct ext4_inode_ref *child_ref)
1197 struct ext4_mountpoint *mp = ext4_get_mount(path);
1205 len = ext4_path_check(path, &is_goal);
1207 /* Remove entry from parent directory */
1208 r = ext4_dir_remove_entry(parent_ref, path, len);
1212 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1213 EXT4_INODE_MODE_DIRECTORY)) {
1214 ext4_fs_inode_links_count_dec(parent_ref);
1215 parent_ref->dirty = true;
1221 int ext4_flink(const char *path, const char *hardlink_path)
1226 bool child_loaded = false;
1227 uint32_t parent_inode, child_inode;
1228 struct ext4_mountpoint *mp = ext4_get_mount(path);
1229 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1230 struct ext4_inode_ref child_ref;
1235 if (mp->fs.read_only)
1238 /* Will that happen? Anyway return EINVAL for such case. */
1239 if (mp != target_mp)
1243 ext4_trans_start(mp);
1245 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1246 &parent_inode, &name_off);
1250 child_inode = f.inode;
1253 /*We have file to unlink. Load it.*/
1254 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1258 child_loaded = true;
1260 /* Creating hardlink for directory is not allowed. */
1261 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1262 EXT4_INODE_MODE_DIRECTORY)) {
1267 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1271 ext4_fs_put_inode_ref(&child_ref);
1274 ext4_trans_abort(mp);
1276 ext4_trans_stop(mp);
1283 int ext4_frename(const char *path, const char *new_path)
1288 bool parent_loaded = false, child_loaded = false;
1289 uint32_t parent_inode, child_inode;
1290 struct ext4_mountpoint *mp = ext4_get_mount(path);
1291 struct ext4_inode_ref child_ref, parent_ref;
1296 if (mp->fs.read_only)
1300 ext4_trans_start(mp);
1302 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1303 &parent_inode, &name_off);
1307 child_inode = f.inode;
1311 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1315 parent_loaded = true;
1317 /*We have file to unlink. Load it.*/
1318 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1322 child_loaded = true;
1324 r = ext4_create_hardlink(new_path, &child_ref, true);
1328 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1334 ext4_fs_put_inode_ref(&parent_ref);
1337 ext4_fs_put_inode_ref(&child_ref);
1340 ext4_trans_abort(mp);
1342 ext4_trans_stop(mp);
1349 /****************************************************************************/
1351 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1353 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1362 int ext4_cache_write_back(const char *path, bool on)
1364 struct ext4_mountpoint *mp = ext4_get_mount(path);
1370 ext4_block_cache_write_back(mp->fs.bdev, on);
1375 int ext4_fremove(const char *path)
1378 uint32_t parent_inode;
1383 struct ext4_inode_ref child;
1384 struct ext4_inode_ref parent;
1385 struct ext4_mountpoint *mp = ext4_get_mount(path);
1390 if (mp->fs.read_only)
1394 ext4_trans_start(mp);
1396 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1397 &parent_inode, &name_off);
1399 ext4_trans_abort(mp);
1405 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1407 ext4_trans_abort(mp);
1412 /*We have file to delete. Load it.*/
1413 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1415 ext4_fs_put_inode_ref(&parent);
1416 ext4_trans_abort(mp);
1420 /* We do not allow opening files here. */
1421 if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1422 EXT4_INODE_MODE_DIRECTORY) {
1423 ext4_fs_put_inode_ref(&parent);
1424 ext4_fs_put_inode_ref(&child);
1425 ext4_trans_abort(mp);
1430 /*Link count will be zero, the inode should be freed. */
1431 if (ext4_inode_get_links_cnt(child.inode) == 1) {
1432 ext4_block_cache_write_back(mp->fs.bdev, 1);
1433 r = ext4_trunc_inode(mp, child.index, 0);
1435 ext4_fs_put_inode_ref(&parent);
1436 ext4_fs_put_inode_ref(&child);
1437 ext4_trans_abort(mp);
1441 ext4_block_cache_write_back(mp->fs.bdev, 0);
1447 len = ext4_path_check(path, &is_goal);
1449 /*Unlink from parent*/
1450 r = ext4_unlink(mp, &parent, &child, path, len);
1454 /*Link count is zero, the inode should be freed. */
1455 if (!ext4_inode_get_links_cnt(child.inode)) {
1456 ext4_inode_set_del_time(child.inode, -1L);
1458 r = ext4_fs_free_inode(&child);
1464 ext4_fs_put_inode_ref(&child);
1465 ext4_fs_put_inode_ref(&parent);
1468 ext4_trans_abort(mp);
1470 ext4_trans_stop(mp);
1476 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1477 struct ext4_inode *inode)
1481 struct ext4_inode_ref inode_ref;
1482 struct ext4_mountpoint *mp = ext4_get_mount(path);
1490 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1500 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1506 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1507 ext4_fs_put_inode_ref(&inode_ref);
1516 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1518 struct ext4_mountpoint *mp = ext4_get_mount(path);
1526 ext4_block_cache_write_back(mp->fs.bdev, 1);
1527 r = ext4_generic_open(f, path, flags, true, 0, 0);
1528 ext4_block_cache_write_back(mp->fs.bdev, 0);
1534 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1536 struct ext4_mountpoint *mp = ext4_get_mount(path);
1543 filetype = EXT4_DE_REG_FILE;
1547 ext4_block_cache_write_back(mp->fs.bdev, 1);
1548 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1549 ext4_block_cache_write_back(mp->fs.bdev, 0);
1555 int ext4_fclose(ext4_file *f)
1557 ext4_assert(f && f->mp);
1562 f->fpos = f->fsize = 0;
1567 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1569 struct ext4_inode_ref ref;
1573 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1575 EXT4_MP_UNLOCK(f->mp);
1580 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1581 if (f->fsize <= size) {
1586 /*Start write back cache mode.*/
1587 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1591 r = ext4_trunc_inode(f->mp, ref.index, size);
1599 /*Stop write back cache mode*/
1600 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1606 ext4_fs_put_inode_ref(&ref);
1611 int ext4_ftruncate(ext4_file *f, uint64_t size)
1614 ext4_assert(f && f->mp);
1616 if (f->mp->fs.read_only)
1619 if (f->flags & O_RDONLY)
1622 EXT4_MP_LOCK(f->mp);
1624 ext4_trans_start(f->mp);
1625 r = ext4_ftruncate_no_lock(f, size);
1627 ext4_trans_abort(f->mp);
1629 ext4_trans_stop(f->mp);
1631 EXT4_MP_UNLOCK(f->mp);
1635 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1638 uint32_t iblock_idx;
1639 uint32_t iblock_last;
1640 uint32_t block_size;
1642 ext4_fsblk_t fblock;
1643 ext4_fsblk_t fblock_start;
1644 uint32_t fblock_count;
1646 uint8_t *u8_buf = buf;
1648 struct ext4_inode_ref ref;
1650 ext4_assert(f && f->mp);
1652 if (f->flags & O_WRONLY)
1658 EXT4_MP_LOCK(f->mp);
1660 struct ext4_fs *const fs = &f->mp->fs;
1661 struct ext4_sblock *const sb = &f->mp->fs.sb;
1666 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1668 EXT4_MP_UNLOCK(f->mp);
1673 f->fsize = ext4_inode_get_size(sb, ref.inode);
1675 block_size = ext4_sb_get_block_size(sb);
1676 size = ((uint64_t)size > (f->fsize - f->fpos))
1677 ? ((size_t)(f->fsize - f->fpos)) : size;
1679 iblock_idx = (uint32_t)((f->fpos) / block_size);
1680 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1681 unalg = (f->fpos) % block_size;
1683 /*If the size of symlink is smaller than 60 bytes*/
1685 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1686 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1687 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1689 char *content = (char *)ref.inode->blocks;
1690 if (f->fpos < f->fsize) {
1692 if (unalg + size > (uint32_t)f->fsize)
1693 len = (uint32_t)f->fsize - unalg;
1694 memcpy(buf, content + unalg, len);
1706 if (size > (block_size - unalg))
1707 len = block_size - unalg;
1709 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1713 /* Do we get an unwritten range? */
1715 uint64_t off = fblock * block_size + unalg;
1716 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1722 memset(u8_buf, 0, len);
1737 while (size >= block_size) {
1738 while (iblock_idx < iblock_last) {
1739 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1747 fblock_start = fblock;
1749 if ((fblock_start + fblock_count) != fblock)
1755 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1760 size -= block_size * fblock_count;
1761 u8_buf += block_size * fblock_count;
1762 f->fpos += block_size * fblock_count;
1765 *rcnt += block_size * fblock_count;
1767 fblock_start = fblock;
1773 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1777 off = fblock * block_size;
1778 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1789 ext4_fs_put_inode_ref(&ref);
1790 EXT4_MP_UNLOCK(f->mp);
1794 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1798 uint32_t iblock_last;
1799 uint32_t ifile_blocks;
1800 uint32_t block_size;
1802 uint32_t fblock_count;
1804 ext4_fsblk_t fblock_start;
1806 struct ext4_inode_ref ref;
1807 const uint8_t *u8_buf = buf;
1810 ext4_assert(f && f->mp);
1812 if (f->mp->fs.read_only)
1815 if (f->flags & O_RDONLY)
1821 EXT4_MP_LOCK(f->mp);
1822 ext4_trans_start(f->mp);
1824 struct ext4_fs *const fs = &f->mp->fs;
1825 struct ext4_sblock *const sb = &f->mp->fs.sb;
1830 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1832 ext4_trans_abort(f->mp);
1833 EXT4_MP_UNLOCK(f->mp);
1838 f->fsize = ext4_inode_get_size(sb, ref.inode);
1839 block_size = ext4_sb_get_block_size(sb);
1841 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1842 iblk_idx = (uint32_t)(f->fpos / block_size);
1843 ifile_blocks = (uint32_t)((f->fsize + block_size - 1) / block_size);
1845 unalg = (f->fpos) % block_size;
1850 if (size > (block_size - unalg))
1851 len = block_size - unalg;
1853 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1857 off = fblk * block_size + unalg;
1858 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1872 /*Start write back cache mode.*/
1873 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1879 while (size >= block_size) {
1881 while (iblk_idx < iblock_last) {
1882 if (iblk_idx < ifile_blocks) {
1883 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1888 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1891 /* Unable to append more blocks. But
1892 * some block might be allocated already
1900 if (!fblock_start) {
1901 fblock_start = fblk;
1904 if ((fblock_start + fblock_count) != fblk)
1910 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1915 size -= block_size * fblock_count;
1916 u8_buf += block_size * fblock_count;
1917 f->fpos += block_size * fblock_count;
1920 *wcnt += block_size * fblock_count;
1922 fblock_start = fblk;
1926 /*ext4_fs_append_inode_block has failed and no
1927 * more blocks might be written. But node size
1928 * should be updated.*/
1934 /*Stop write back cache mode*/
1935 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1942 if (iblk_idx < ifile_blocks) {
1943 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1947 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1949 /*Node size sholud be updated.*/
1953 off = fblk * block_size;
1954 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1965 if (f->fpos > f->fsize) {
1967 ext4_inode_set_size(ref.inode, f->fsize);
1972 r = ext4_fs_put_inode_ref(&ref);
1975 ext4_trans_abort(f->mp);
1977 ext4_trans_stop(f->mp);
1979 EXT4_MP_UNLOCK(f->mp);
1983 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1987 if (offset > f->fsize)
1993 if ((offset + f->fpos) > f->fsize)
1999 if (offset > f->fsize)
2002 f->fpos = f->fsize - offset;
2008 uint64_t ext4_ftell(ext4_file *f)
2013 uint64_t ext4_fsize(ext4_file *f)
2018 int ext4_chmod(const char *path, uint32_t mode)
2021 uint32_t ino, orig_mode;
2023 struct ext4_sblock *sb;
2024 struct ext4_inode_ref inode_ref;
2025 struct ext4_mountpoint *mp = ext4_get_mount(path);
2030 if (mp->fs.read_only)
2034 ext4_trans_start(mp);
2036 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2038 ext4_trans_abort(mp);
2045 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2047 ext4_trans_abort(mp);
2052 orig_mode = ext4_inode_get_mode(sb, inode_ref.inode);
2053 orig_mode &= ~0xFFF;
2054 orig_mode |= mode & 0xFFF;
2055 ext4_inode_set_mode(sb, inode_ref.inode, orig_mode);
2056 inode_ref.dirty = true;
2058 r = ext4_fs_put_inode_ref(&inode_ref);
2060 ext4_trans_abort(mp);
2062 ext4_trans_stop(mp);
2068 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
2073 struct ext4_inode_ref inode_ref;
2074 struct ext4_mountpoint *mp = ext4_get_mount(path);
2079 if (mp->fs.read_only)
2083 ext4_trans_start(mp);
2085 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2087 ext4_trans_abort(mp);
2093 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2095 ext4_trans_abort(mp);
2100 ext4_inode_set_uid(inode_ref.inode, uid);
2101 ext4_inode_set_gid(inode_ref.inode, gid);
2102 inode_ref.dirty = true;
2104 r = ext4_fs_put_inode_ref(&inode_ref);
2106 ext4_trans_abort(mp);
2108 ext4_trans_stop(mp);
2114 int ext4_file_set_atime(const char *path, uint32_t atime)
2119 struct ext4_inode_ref inode_ref;
2120 struct ext4_mountpoint *mp = ext4_get_mount(path);
2125 if (mp->fs.read_only)
2129 ext4_trans_start(mp);
2131 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2133 ext4_trans_abort(mp);
2139 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2141 ext4_trans_abort(mp);
2146 ext4_inode_set_access_time(inode_ref.inode, atime);
2147 inode_ref.dirty = true;
2149 r = ext4_fs_put_inode_ref(&inode_ref);
2151 ext4_trans_abort(mp);
2153 ext4_trans_stop(mp);
2159 int ext4_file_set_mtime(const char *path, uint32_t mtime)
2164 struct ext4_inode_ref inode_ref;
2165 struct ext4_mountpoint *mp = ext4_get_mount(path);
2170 if (mp->fs.read_only)
2174 ext4_trans_start(mp);
2176 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2178 ext4_trans_abort(mp);
2184 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2186 ext4_trans_abort(mp);
2191 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2192 inode_ref.dirty = true;
2194 r = ext4_fs_put_inode_ref(&inode_ref);
2196 ext4_trans_abort(mp);
2198 ext4_trans_stop(mp);
2204 int ext4_file_set_ctime(const char *path, uint32_t ctime)
2209 struct ext4_inode_ref inode_ref;
2210 struct ext4_mountpoint *mp = ext4_get_mount(path);
2215 if (mp->fs.read_only)
2219 ext4_trans_start(mp);
2221 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2223 ext4_trans_abort(mp);
2229 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2231 ext4_trans_abort(mp);
2236 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2237 inode_ref.dirty = true;
2239 r = ext4_fs_put_inode_ref(&inode_ref);
2241 ext4_trans_abort(mp);
2243 ext4_trans_stop(mp);
2249 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2251 struct ext4_inode_ref ref;
2253 ext4_fsblk_t fblock;
2254 uint32_t block_size;
2257 ext4_assert(f && f->mp);
2262 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2267 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2268 if (size > block_size) {
2272 r = ext4_ftruncate_no_lock(f, 0);
2276 /*Start write back cache mode.*/
2277 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2281 /*If the size of symlink is smaller than 60 bytes*/
2282 if (size < sizeof(ref.inode->blocks)) {
2283 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2284 memcpy(ref.inode->blocks, buf, size);
2285 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2287 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2288 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2292 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2298 /*Stop write back cache mode*/
2299 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2304 ext4_inode_set_size(ref.inode, size);
2312 ext4_fs_put_inode_ref(&ref);
2316 int ext4_fsymlink(const char *target, const char *path)
2318 struct ext4_mountpoint *mp = ext4_get_mount(path);
2326 if (mp->fs.read_only)
2329 filetype = EXT4_DE_SYMLINK;
2332 ext4_trans_start(mp);
2334 ext4_block_cache_write_back(mp->fs.bdev, 1);
2335 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2337 r = ext4_fsymlink_set(&f, target, strlen(target));
2344 ext4_block_cache_write_back(mp->fs.bdev, 0);
2347 ext4_trans_abort(mp);
2349 ext4_trans_stop(mp);
2355 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2357 struct ext4_mountpoint *mp = ext4_get_mount(path);
2368 filetype = EXT4_DE_SYMLINK;
2371 ext4_block_cache_write_back(mp->fs.bdev, 1);
2372 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2374 r = ext4_fread(&f, buf, bufsize, rcnt);
2381 ext4_block_cache_write_back(mp->fs.bdev, 0);
2383 ext4_trans_abort(mp);
2385 ext4_trans_stop(mp);
2391 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2392 const void *data, size_t data_size, bool replace)
2399 const char *dissected_name = NULL;
2400 size_t dissected_len = 0;
2401 struct ext4_xattr_ref xattr_ref;
2402 struct ext4_inode_ref inode_ref;
2403 struct ext4_mountpoint *mp = ext4_get_mount(path);
2407 if (mp->fs.read_only)
2410 dissected_name = ext4_extract_xattr_name(name, name_len,
2411 &name_index, &dissected_len,
2417 ext4_trans_start(mp);
2419 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2425 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2429 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2431 ext4_fs_put_inode_ref(&inode_ref);
2435 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2436 dissected_len, data, data_size, replace);
2438 ext4_fs_put_xattr_ref(&xattr_ref);
2439 ext4_fs_put_inode_ref(&inode_ref);
2442 ext4_trans_abort(mp);
2444 ext4_trans_stop(mp);
2450 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2451 void *buf, size_t buf_size, size_t *data_size)
2458 const char *dissected_name = NULL;
2459 size_t dissected_len = 0;
2460 struct ext4_xattr_ref xattr_ref;
2461 struct ext4_inode_ref inode_ref;
2462 struct ext4_mountpoint *mp = ext4_get_mount(path);
2466 dissected_name = ext4_extract_xattr_name(name, name_len,
2467 &name_index, &dissected_len,
2473 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2479 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2483 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2485 ext4_fs_put_inode_ref(&inode_ref);
2489 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2490 dissected_len, buf, buf_size, data_size);
2492 ext4_fs_put_xattr_ref(&xattr_ref);
2493 ext4_fs_put_inode_ref(&inode_ref);
2499 struct ext4_listxattr_iterator {
2504 bool list_too_small;
2505 bool get_required_size;
2508 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2509 struct ext4_xattr_item *item)
2511 struct ext4_listxattr_iterator *lxi;
2512 lxi = ref->iter_arg;
2513 if (!lxi->get_required_size) {
2516 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2517 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2518 lxi->list_too_small = 1;
2519 return EXT4_XATTR_ITERATE_STOP;
2522 memcpy(lxi->list_ptr, prefix, plen);
2523 lxi->list_ptr += plen;
2524 lxi->ret_size += plen;
2526 memcpy(lxi->list_ptr, item->name, item->name_len);
2527 lxi->list_ptr[item->name_len] = 0;
2528 lxi->list_ptr += item->name_len + 1;
2530 lxi->ret_size += item->name_len + 1;
2531 return EXT4_XATTR_ITERATE_CONT;
2534 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2539 struct ext4_xattr_ref xattr_ref;
2540 struct ext4_inode_ref inode_ref;
2541 struct ext4_listxattr_iterator lxi;
2542 struct ext4_mountpoint *mp = ext4_get_mount(path);
2547 lxi.list_ptr = list;
2550 lxi.list_too_small = false;
2551 lxi.get_required_size = (!size) ? true : false;
2554 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2560 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2564 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2566 ext4_fs_put_inode_ref(&inode_ref);
2570 xattr_ref.iter_arg = &lxi;
2571 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2572 if (lxi.list_too_small)
2577 *ret_size = lxi.ret_size;
2580 ext4_fs_put_xattr_ref(&xattr_ref);
2581 ext4_fs_put_inode_ref(&inode_ref);
2588 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2595 const char *dissected_name = NULL;
2596 size_t dissected_len = 0;
2597 struct ext4_xattr_ref xattr_ref;
2598 struct ext4_inode_ref inode_ref;
2599 struct ext4_mountpoint *mp = ext4_get_mount(path);
2603 if (mp->fs.read_only)
2606 dissected_name = ext4_extract_xattr_name(name, name_len,
2607 &name_index, &dissected_len,
2613 ext4_trans_start(mp);
2615 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2621 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2625 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2627 ext4_fs_put_inode_ref(&inode_ref);
2631 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2634 ext4_fs_put_xattr_ref(&xattr_ref);
2635 ext4_fs_put_inode_ref(&inode_ref);
2638 ext4_trans_abort(mp);
2640 ext4_trans_stop(mp);
2647 /*********************************DIRECTORY OPERATION************************/
2649 int ext4_dir_rm(const char *path)
2655 struct ext4_mountpoint *mp = ext4_get_mount(path);
2656 struct ext4_inode_ref act;
2657 struct ext4_inode_ref child;
2658 struct ext4_dir_iter it;
2662 uint32_t inode_current;
2672 if (mp->fs.read_only)
2677 struct ext4_fs *const fs = &mp->fs;
2680 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2682 ext4_trans_abort(mp);
2688 len = ext4_path_check(path, &is_goal);
2690 inode_current = f.inode;
2692 ext4_block_cache_write_back(mp->fs.bdev, 1);
2696 uint64_t act_curr_pos = 0;
2697 has_children = false;
2700 while (r == EOK && !has_children && !dir_end) {
2702 /*Load directory node.*/
2703 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2708 /*Initialize iterator.*/
2709 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2711 ext4_fs_put_inode_ref(&act);
2720 ext4_trans_start(mp);
2722 /*Get up directory inode when ".." entry*/
2723 if ((it.curr->name_len == 2) &&
2724 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2725 inode_up = ext4_dir_en_get_inode(it.curr);
2728 /*If directory or file entry, but not "." ".." entry*/
2729 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2731 /*Get child inode reference do unlink
2734 uint32_t inode_type;
2735 cinode = ext4_dir_en_get_inode(it.curr);
2736 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2740 /*If directory with no leaf children*/
2741 r = ext4_has_children(&has_children, &child);
2743 ext4_fs_put_inode_ref(&child);
2748 /*Has directory children. Go into this
2750 inode_up = inode_current;
2751 inode_current = cinode;
2753 ext4_fs_put_inode_ref(&child);
2756 inode_type = ext4_inode_type(&mp->fs.sb,
2760 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2761 r = ext4_trunc_inode(mp, child.index, 0);
2763 r = ext4_trunc_dir(mp, &act, &child);
2766 ext4_fs_put_inode_ref(&child);
2770 /*No children in child directory or file. Just
2772 r = ext4_unlink(f.mp, &act, &child,
2773 (char *)it.curr->name,
2776 ext4_fs_put_inode_ref(&child);
2780 ext4_inode_set_del_time(child.inode, -1L);
2781 ext4_inode_set_links_cnt(child.inode, 0);
2784 r = ext4_fs_free_inode(&child);
2786 ext4_fs_put_inode_ref(&child);
2790 r = ext4_fs_put_inode_ref(&child);
2796 r = ext4_dir_iterator_next(&it);
2800 act_curr_pos = it.curr_off;
2802 ext4_dir_iterator_fini(&it);
2804 r = ext4_fs_put_inode_ref(&act);
2806 ext4_fs_put_inode_ref(&act);
2809 ext4_trans_abort(mp);
2811 ext4_trans_stop(mp);
2815 /*Directory iterator reached last entry*/
2818 inode_current = inode_up;
2828 if (r == EOK && !depth) {
2830 struct ext4_inode_ref parent;
2831 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2835 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
2838 ext4_fs_put_inode_ref(&act);
2842 ext4_trans_start(mp);
2844 /* In this place all directories should be
2846 * Last unlink from root of current directory*/
2847 r = ext4_unlink(f.mp, &parent, &act,
2850 ext4_fs_put_inode_ref(&parent);
2851 ext4_fs_put_inode_ref(&act);
2855 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2856 ext4_inode_set_del_time(act.inode, -1L);
2857 ext4_inode_set_links_cnt(act.inode, 0);
2860 r = ext4_trunc_dir(mp, &parent, &act);
2862 ext4_fs_put_inode_ref(&parent);
2863 ext4_fs_put_inode_ref(&act);
2867 r = ext4_fs_free_inode(&act);
2869 ext4_fs_put_inode_ref(&parent);
2870 ext4_fs_put_inode_ref(&act);
2875 r = ext4_fs_put_inode_ref(&parent);
2879 r = ext4_fs_put_inode_ref(&act);
2882 ext4_trans_abort(mp);
2884 ext4_trans_stop(mp);
2887 ext4_block_cache_write_back(mp->fs.bdev, 0);
2893 int ext4_dir_mv(const char *path, const char *new_path)
2895 return ext4_frename(path, new_path);
2898 int ext4_dir_mk(const char *path)
2903 struct ext4_mountpoint *mp = ext4_get_mount(path);
2908 if (mp->fs.read_only)
2912 ext4_trans_start(mp);
2915 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2917 /*Directory already created*/
2918 ext4_trans_stop(mp);
2924 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2926 ext4_trans_abort(mp);
2931 ext4_trans_stop(mp);
2936 int ext4_dir_open(ext4_dir *d, const char *path)
2938 struct ext4_mountpoint *mp = ext4_get_mount(path);
2945 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2951 int ext4_dir_close(ext4_dir *d)
2953 return ext4_fclose(&d->f);
2956 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2958 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2961 ext4_direntry *de = 0;
2962 struct ext4_inode_ref dir;
2963 struct ext4_dir_iter it;
2965 EXT4_MP_LOCK(d->f.mp);
2967 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2968 EXT4_MP_UNLOCK(d->f.mp);
2972 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2977 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2979 ext4_fs_put_inode_ref(&dir);
2983 memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2986 ext4_dir_iterator_next(&it);
2988 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2990 ext4_dir_iterator_fini(&it);
2991 ext4_fs_put_inode_ref(&dir);
2994 EXT4_MP_UNLOCK(d->f.mp);
2998 void ext4_dir_entry_rewind(ext4_dir *d)