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 struct jbd_journal jbd_journal;
91 /**@brief Block devices descriptor.*/
92 struct _ext4_devices {
94 /**@brief Block device name (@ref ext4_device_register)*/
97 /**@brief Block device handle.*/
98 struct ext4_blockdev *bd;
100 /**@brief Block cache handle.*/
101 struct ext4_bcache *bc;
104 /**@brief Block devices.*/
105 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
107 /**@brief Mountpoints.*/
108 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
110 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
111 const char *dev_name)
114 ext4_assert(bd && dev_name);
116 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
117 if (!_bdevices[i].bd) {
118 strcpy(_bdevices[i].name, dev_name);
119 _bdevices[i].bd = bd;
120 _bdevices[i].bc = bc;
124 if (!strcmp(_bdevices[i].name, dev_name))
130 /****************************************************************************/
132 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
134 if ((name_size == 1) && (name[0] == '.'))
137 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
143 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
145 struct ext4_sblock *sb = &enode->fs->sb;
147 /* Check if node is directory */
148 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
149 *has_children = false;
153 struct ext4_dir_iter it;
154 int rc = ext4_dir_iterator_init(&it, enode, 0);
158 /* Find a non-empty directory entry */
160 while (it.curr != NULL) {
161 if (ext4_dir_en_get_inode(it.curr) != 0) {
163 nsize = ext4_dir_en_get_name_len(sb, it.curr);
164 if (!ext4_is_dots(it.curr->name, nsize)) {
170 rc = ext4_dir_iterator_next(&it);
172 ext4_dir_iterator_fini(&it);
177 rc = ext4_dir_iterator_fini(&it);
181 *has_children = found;
186 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
187 struct ext4_inode_ref *ch, const char *n,
188 uint32_t len, bool rename)
190 /* Check maximum name length */
191 if (len > EXT4_DIRECTORY_FILENAME_LEN)
194 /* Add entry to parent directory */
195 int r = ext4_dir_add_entry(parent, n, len, ch);
199 /* Fill new dir -> add '.' and '..' entries.
200 * Also newly allocated inode should have 0 link count.
203 bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
204 EXT4_INODE_MODE_DIRECTORY);
205 if (is_dir && !rename) {
207 #if CONFIG_DIR_INDEX_ENABLE
208 /* Initialize directory index if supported */
209 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
210 r = ext4_dir_dx_init(ch, parent);
214 ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
219 r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
221 ext4_dir_remove_entry(parent, n, strlen(n));
225 r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
227 ext4_dir_remove_entry(parent, n, strlen(n));
228 ext4_dir_remove_entry(ch, ".", strlen("."));
233 /*New empty directory. Two links (. and ..) */
234 ext4_inode_set_links_cnt(ch->inode, 2);
235 ext4_fs_inode_links_count_inc(parent);
237 parent->dirty = true;
241 * In case we want to rename a directory,
242 * we reset the original '..' pointer.
246 idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
247 struct ext4_dir_search_result res;
249 r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
253 ext4_dir_en_set_inode(res.dentry, parent->index);
254 ext4_trans_set_block_dirty(res.block.buf);
255 r = ext4_dir_destroy_result(ch, &res);
260 #if CONFIG_DIR_INDEX_ENABLE
261 r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
268 ext4_fs_inode_links_count_inc(parent);
269 parent->dirty = true;
272 ext4_fs_inode_links_count_inc(ch);
279 static int ext4_unlink(struct ext4_mountpoint *mp,
280 struct ext4_inode_ref *parent,
281 struct ext4_inode_ref *child, const char *name,
285 int rc = ext4_has_children(&has_children, child);
289 /* Cannot unlink non-empty node */
293 /* Remove entry from parent directory */
294 rc = ext4_dir_remove_entry(parent, name, name_len);
298 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
299 EXT4_INODE_MODE_DIRECTORY);
301 /* If directory - handle links from parent */
303 ext4_fs_inode_links_count_dec(parent);
304 parent->dirty = true;
308 * TODO: Update timestamps of the parent
309 * (when we have wall-clock time).
311 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
312 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
313 * parent->dirty = true;
317 * TODO: Update timestamp for inode.
319 * ext4_inode_set_change_inode_time(child->inode,
322 if (ext4_inode_get_links_cnt(child->inode)) {
323 ext4_fs_inode_links_count_dec(child);
330 /****************************************************************************/
332 int ext4_mount(const char *dev_name, const char *mount_point,
335 ext4_assert(mount_point && dev_name);
340 struct ext4_blockdev *bd = 0;
341 struct ext4_bcache *bc = 0;
342 struct ext4_mountpoint *mp = 0;
344 if (mount_point[strlen(mount_point) - 1] != '/')
347 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
348 if (_bdevices[i].name) {
349 if (!strcmp(dev_name, _bdevices[i].name)) {
350 bd = _bdevices[i].bd;
351 bc = _bdevices[i].bc;
360 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
361 if (!_mp[i].mounted) {
362 strcpy(_mp[i].name, mount_point);
368 if (!strcmp(_mp[i].name, mount_point))
375 r = ext4_block_init(bd);
379 r = ext4_fs_init(&mp->fs, bd, read_only);
385 bsize = ext4_sb_get_block_size(&mp->fs.sb);
386 ext4_block_set_lb_size(bd, bsize);
388 mp->cache_dynamic = 0;
391 /*Automatic block cache alloc.*/
392 mp->cache_dynamic = 1;
393 bc = malloc(sizeof(struct ext4_bcache));
395 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
404 if (bsize != bc->itemsize)
407 /*Bind block cache to block device*/
408 r = ext4_block_bind_bcache(bd, bc);
410 ext4_bcache_cleanup(bc);
412 if (mp->cache_dynamic) {
413 ext4_bcache_fini_dynamic(bc);
424 int ext4_umount(const char *mount_point)
428 struct ext4_mountpoint *mp = 0;
430 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
431 if (!strcmp(_mp[i].name, mount_point)) {
440 r = ext4_fs_fini(&mp->fs);
446 ext4_bcache_cleanup(mp->fs.bdev->bc);
447 if (mp->cache_dynamic) {
448 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
449 free(mp->fs.bdev->bc);
451 r = ext4_block_fini(mp->fs.bdev);
453 mp->fs.bdev->fs = NULL;
457 static struct ext4_mountpoint *ext4_get_mount(const char *path)
460 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
465 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
472 static int __ext4_journal_start(const char *mount_point)
475 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
479 if (mp->fs.read_only)
482 if (ext4_sb_feature_com(&mp->fs.sb,
483 EXT4_FCOM_HAS_JOURNAL)) {
484 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
488 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
490 mp->jbd_fs.dirty = false;
491 jbd_put_fs(&mp->jbd_fs);
494 mp->fs.jbd_fs = &mp->jbd_fs;
495 mp->fs.jbd_journal = &mp->jbd_journal;
502 static int __ext4_journal_stop(const char *mount_point)
505 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
509 if (mp->fs.read_only)
512 if (ext4_sb_feature_com(&mp->fs.sb,
513 EXT4_FCOM_HAS_JOURNAL)) {
514 r = jbd_journal_stop(&mp->jbd_journal);
516 mp->jbd_fs.dirty = false;
517 jbd_put_fs(&mp->jbd_fs);
518 mp->fs.jbd_journal = NULL;
519 mp->fs.jbd_fs = NULL;
523 r = jbd_put_fs(&mp->jbd_fs);
525 mp->fs.jbd_journal = NULL;
526 mp->fs.jbd_fs = NULL;
530 mp->fs.jbd_journal = NULL;
531 mp->fs.jbd_fs = NULL;
538 static int __ext4_recover(const char *mount_point)
540 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
546 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
547 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
554 r = jbd_get_fs(&mp->fs, jbd_fs);
560 r = jbd_recover(jbd_fs);
572 static int __ext4_trans_start(struct ext4_mountpoint *mp)
575 if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
576 struct jbd_journal *journal = mp->fs.jbd_journal;
577 struct jbd_trans *trans;
578 trans = jbd_journal_new_trans(journal);
583 mp->fs.curr_trans = trans;
590 static int __ext4_trans_stop(struct ext4_mountpoint *mp)
593 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
594 struct jbd_journal *journal = mp->fs.jbd_journal;
595 struct jbd_trans *trans = mp->fs.curr_trans;
596 r = jbd_journal_commit_trans(journal, trans);
597 mp->fs.curr_trans = NULL;
603 static void __ext4_trans_abort(struct ext4_mountpoint *mp)
605 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
606 struct jbd_journal *journal = mp->fs.jbd_journal;
607 struct jbd_trans *trans = mp->fs.curr_trans;
608 jbd_journal_free_trans(journal, trans, true);
609 mp->fs.curr_trans = NULL;
613 int ext4_journal_start(const char *mount_point __unused)
616 #if CONFIG_JOURNALING_ENABLE
617 r = __ext4_journal_start(mount_point);
622 int ext4_journal_stop(const char *mount_point __unused)
625 #if CONFIG_JOURNALING_ENABLE
626 r = __ext4_journal_stop(mount_point);
631 int ext4_recover(const char *mount_point __unused)
634 #if CONFIG_JOURNALING_ENABLE
635 r = __ext4_recover(mount_point);
640 static int ext4_trans_start(struct ext4_mountpoint *mp __unused)
643 #if CONFIG_JOURNALING_ENABLE
644 r = __ext4_trans_start(mp);
649 static int ext4_trans_stop(struct ext4_mountpoint *mp __unused)
652 #if CONFIG_JOURNALING_ENABLE
653 r = __ext4_trans_stop(mp);
658 static void ext4_trans_abort(struct ext4_mountpoint *mp __unused)
660 #if CONFIG_JOURNALING_ENABLE
661 __ext4_trans_abort(mp);
666 int ext4_mount_point_stats(const char *mount_point,
667 struct ext4_mount_stats *stats)
669 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
675 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
676 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
677 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
678 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
679 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
681 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
682 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
683 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
685 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
691 int ext4_mount_setup_locks(const char *mount_point,
692 const struct ext4_lock *locks)
695 struct ext4_mountpoint *mp = 0;
697 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
698 if (!strcmp(_mp[i].name, mount_point)) {
706 mp->os_locks = locks;
710 /********************************FILE OPERATIONS*****************************/
712 static int ext4_path_check(const char *path, bool *is_goal)
716 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
718 if (path[i] == '/') {
732 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
737 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
738 *file_flags = O_RDONLY;
742 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
743 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
747 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
748 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
752 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
753 !strcmp(flags, "r+b")) {
754 *file_flags = O_RDWR;
758 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
759 !strcmp(flags, "w+b")) {
760 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
764 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
765 !strcmp(flags, "a+b")) {
766 *file_flags = O_RDWR | O_CREAT | O_APPEND;
773 static int ext4_trunc_inode(struct ext4_mountpoint *mp,
774 uint32_t index, uint64_t new_size)
777 struct ext4_fs *const fs = &mp->fs;
778 struct ext4_inode_ref inode_ref;
780 bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
781 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
785 inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
786 ext4_fs_put_inode_ref(&inode_ref);
790 while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
792 inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
794 ext4_trans_start(mp);
795 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
797 ext4_trans_abort(mp);
800 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
802 ext4_fs_put_inode_ref(&inode_ref);
804 r = ext4_fs_put_inode_ref(&inode_ref);
807 ext4_trans_abort(mp);
813 if (inode_size > new_size) {
815 inode_size = new_size;
817 ext4_trans_start(mp);
818 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
820 ext4_trans_abort(mp);
823 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
825 ext4_fs_put_inode_ref(&inode_ref);
827 r = ext4_fs_put_inode_ref(&inode_ref);
830 ext4_trans_abort(mp);
839 ext4_trans_start(mp);
844 static int ext4_trunc_dir(struct ext4_mountpoint *mp,
845 struct ext4_inode_ref *parent,
846 struct ext4_inode_ref *dir)
849 bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
850 EXT4_INODE_MODE_DIRECTORY);
851 uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
855 #if CONFIG_DIR_INDEX_ENABLE
856 /* Initialize directory index if supported */
857 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
858 r = ext4_dir_dx_init(dir, parent);
862 r = ext4_trunc_inode(mp, dir->index,
863 EXT4_DIR_DX_INIT_BCNT * block_size);
869 r = ext4_trunc_inode(mp, dir->index, block_size);
874 return ext4_fs_truncate_inode(dir, 0);
878 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
879 * any filetype of the target dir entry will be accepted.
881 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
882 int ftype, uint32_t *parent_inode,
885 bool is_goal = false;
886 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
891 struct ext4_mountpoint *mp = ext4_get_mount(path);
892 struct ext4_dir_search_result result;
893 struct ext4_inode_ref ref;
900 struct ext4_fs *const fs = &mp->fs;
901 struct ext4_sblock *const sb = &mp->fs.sb;
903 if (fs->read_only && flags & O_CREAT)
909 path += strlen(mp->name);
912 *name_off = strlen(mp->name);
915 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
920 *parent_inode = ref.index;
923 ext4_trans_start(mp);
925 len = ext4_path_check(path, &is_goal);
928 len = ext4_path_check(path, &is_goal);
930 /*If root open was request.*/
931 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
939 r = ext4_dir_find_entry(&result, &ref, path, len);
942 /*Destroy last result*/
943 ext4_dir_destroy_result(&ref, &result);
947 if (!(f->flags & O_CREAT))
950 /*O_CREAT allows create new entry*/
951 struct ext4_inode_ref child_ref;
952 r = ext4_fs_alloc_inode(fs, &child_ref,
953 is_goal ? ftype : EXT4_DE_DIR);
958 /*Link with root dir.*/
959 r = ext4_link(mp, &ref, &child_ref, path, len, false);
961 /*Fail. Free new inode.*/
962 ext4_fs_free_inode(&child_ref);
963 /*We do not want to write new inode.
964 But block has to be released.*/
965 child_ref.dirty = false;
966 ext4_fs_put_inode_ref(&child_ref);
970 ext4_fs_put_inode_ref(&child_ref);
975 *parent_inode = ref.index;
977 next_inode = ext4_dir_en_get_inode(result.dentry);
978 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
980 t = ext4_dir_en_get_inode_type(sb, result.dentry);
981 imode = ext4_fs_correspond_inode_mode(t);
983 struct ext4_inode_ref child_ref;
984 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
988 imode = ext4_inode_type(sb, child_ref.inode);
989 ext4_fs_put_inode_ref(&child_ref);
992 r = ext4_dir_destroy_result(&ref, &result);
996 /*If expected file error*/
997 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
1001 if (ftype != EXT4_DE_UNKNOWN) {
1002 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
1003 if (df && is_goal) {
1009 r = ext4_fs_put_inode_ref(&ref);
1013 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1023 *name_off += len + 1;
1027 ext4_fs_put_inode_ref(&ref);
1033 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
1034 r = ext4_trunc_inode(mp, ref.index, 0);
1036 ext4_fs_put_inode_ref(&ref);
1042 f->fsize = ext4_inode_get_size(sb, ref.inode);
1043 f->inode = ref.index;
1046 if (f->flags & O_APPEND)
1051 r = ext4_fs_put_inode_ref(&ref);
1052 if (flags & O_CREAT) {
1054 ext4_trans_stop(mp);
1056 ext4_trans_abort(mp);
1063 /****************************************************************************/
1065 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
1066 bool file_expect, uint32_t *parent_inode,
1071 if (ext4_parse_flags(flags, &iflags) == false)
1074 if (file_expect == true)
1075 filetype = EXT4_DE_REG_FILE;
1077 filetype = EXT4_DE_DIR;
1079 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
1083 static int ext4_create_hardlink(const char *path,
1084 struct ext4_inode_ref *child_ref, bool rename)
1086 bool is_goal = false;
1087 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
1088 uint32_t next_inode;
1092 struct ext4_mountpoint *mp = ext4_get_mount(path);
1093 struct ext4_dir_search_result result;
1094 struct ext4_inode_ref ref;
1099 struct ext4_fs *const fs = &mp->fs;
1100 struct ext4_sblock *const sb = &mp->fs.sb;
1102 /*Skip mount point*/
1103 path += strlen(mp->name);
1106 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
1110 len = ext4_path_check(path, &is_goal);
1113 len = ext4_path_check(path, &is_goal);
1115 /*If root open was request.*/
1116 r = is_goal ? EINVAL : ENOENT;
1120 r = ext4_dir_find_entry(&result, &ref, path, len);
1123 /*Destroy last result*/
1124 ext4_dir_destroy_result(&ref, &result);
1126 if (r != ENOENT || !is_goal)
1129 /*Link with root dir.*/
1130 r = ext4_link(mp, &ref, child_ref, path, len, rename);
1132 } else if (r == EOK && is_goal) {
1133 /*Destroy last result*/
1134 ext4_dir_destroy_result(&ref, &result);
1139 next_inode = result.dentry->inode;
1140 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1142 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1143 inode_mode = ext4_fs_correspond_inode_mode(t);
1145 struct ext4_inode_ref child_ref;
1146 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1150 inode_mode = ext4_inode_type(sb, child_ref.inode);
1151 ext4_fs_put_inode_ref(&child_ref);
1154 r = ext4_dir_destroy_result(&ref, &result);
1158 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1159 r = is_goal ? EEXIST : ENOENT;
1163 r = ext4_fs_put_inode_ref(&ref);
1167 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1178 ext4_fs_put_inode_ref(&ref);
1182 r = ext4_fs_put_inode_ref(&ref);
1186 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1187 struct ext4_inode_ref *parent_ref,
1188 struct ext4_inode_ref *child_ref)
1193 struct ext4_mountpoint *mp = ext4_get_mount(path);
1201 len = ext4_path_check(path, &is_goal);
1203 /* Remove entry from parent directory */
1204 r = ext4_dir_remove_entry(parent_ref, path, len);
1208 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1209 EXT4_INODE_MODE_DIRECTORY)) {
1210 ext4_fs_inode_links_count_dec(parent_ref);
1211 parent_ref->dirty = true;
1217 int ext4_flink(const char *path, const char *hardlink_path)
1222 bool child_loaded = false;
1223 uint32_t parent_inode, child_inode;
1224 struct ext4_mountpoint *mp = ext4_get_mount(path);
1225 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1226 struct ext4_inode_ref child_ref;
1231 if (mp->fs.read_only)
1234 /* Will that happen? Anyway return EINVAL for such case. */
1235 if (mp != target_mp)
1239 ext4_trans_start(mp);
1241 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1242 &parent_inode, &name_off);
1246 child_inode = f.inode;
1249 /*We have file to unlink. Load it.*/
1250 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1254 child_loaded = true;
1256 /* Creating hardlink for directory is not allowed. */
1257 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1258 EXT4_INODE_MODE_DIRECTORY)) {
1263 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1267 ext4_fs_put_inode_ref(&child_ref);
1270 ext4_trans_abort(mp);
1272 ext4_trans_stop(mp);
1279 int ext4_frename(const char *path, const char *new_path)
1284 bool parent_loaded = false, child_loaded = false;
1285 uint32_t parent_inode, child_inode;
1286 struct ext4_mountpoint *mp = ext4_get_mount(path);
1287 struct ext4_inode_ref child_ref, parent_ref;
1292 if (mp->fs.read_only)
1296 ext4_trans_start(mp);
1298 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1299 &parent_inode, &name_off);
1303 child_inode = f.inode;
1307 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1311 parent_loaded = true;
1313 /*We have file to unlink. Load it.*/
1314 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1318 child_loaded = true;
1320 r = ext4_create_hardlink(new_path, &child_ref, true);
1324 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1330 ext4_fs_put_inode_ref(&parent_ref);
1333 ext4_fs_put_inode_ref(&child_ref);
1336 ext4_trans_abort(mp);
1338 ext4_trans_stop(mp);
1345 /****************************************************************************/
1347 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1349 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1358 int ext4_cache_write_back(const char *path, bool on)
1360 struct ext4_mountpoint *mp = ext4_get_mount(path);
1366 ext4_block_cache_write_back(mp->fs.bdev, on);
1371 int ext4_fremove(const char *path)
1374 uint32_t parent_inode;
1379 struct ext4_inode_ref child;
1380 struct ext4_inode_ref parent;
1381 struct ext4_mountpoint *mp = ext4_get_mount(path);
1386 if (mp->fs.read_only)
1390 ext4_trans_start(mp);
1392 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1393 &parent_inode, &name_off);
1395 ext4_trans_abort(mp);
1401 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1403 ext4_trans_abort(mp);
1408 /*We have file to delete. Load it.*/
1409 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1411 ext4_fs_put_inode_ref(&parent);
1412 ext4_trans_abort(mp);
1416 /* We do not allow opening files here. */
1417 if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1418 EXT4_INODE_MODE_DIRECTORY) {
1419 ext4_fs_put_inode_ref(&parent);
1420 ext4_fs_put_inode_ref(&child);
1421 ext4_trans_abort(mp);
1426 /*Link count will be zero, the inode should be freed. */
1427 if (ext4_inode_get_links_cnt(child.inode) == 1) {
1428 ext4_block_cache_write_back(mp->fs.bdev, 1);
1429 r = ext4_trunc_inode(mp, child.index, 0);
1431 ext4_fs_put_inode_ref(&parent);
1432 ext4_fs_put_inode_ref(&child);
1433 ext4_trans_abort(mp);
1437 ext4_block_cache_write_back(mp->fs.bdev, 0);
1443 len = ext4_path_check(path, &is_goal);
1445 /*Unlink from parent*/
1446 r = ext4_unlink(mp, &parent, &child, path, len);
1450 /*Link count is zero, the inode should be freed. */
1451 if (!ext4_inode_get_links_cnt(child.inode)) {
1452 ext4_inode_set_del_time(child.inode, -1L);
1454 r = ext4_fs_free_inode(&child);
1460 ext4_fs_put_inode_ref(&child);
1461 ext4_fs_put_inode_ref(&parent);
1464 ext4_trans_abort(mp);
1466 ext4_trans_stop(mp);
1472 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1473 struct ext4_inode *inode)
1477 struct ext4_inode_ref inode_ref;
1478 struct ext4_mountpoint *mp = ext4_get_mount(path);
1486 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1496 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1502 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1503 ext4_fs_put_inode_ref(&inode_ref);
1512 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1514 struct ext4_mountpoint *mp = ext4_get_mount(path);
1522 ext4_block_cache_write_back(mp->fs.bdev, 1);
1523 r = ext4_generic_open(f, path, flags, true, 0, 0);
1524 ext4_block_cache_write_back(mp->fs.bdev, 0);
1530 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1532 struct ext4_mountpoint *mp = ext4_get_mount(path);
1539 filetype = EXT4_DE_REG_FILE;
1543 ext4_block_cache_write_back(mp->fs.bdev, 1);
1544 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1545 ext4_block_cache_write_back(mp->fs.bdev, 0);
1551 int ext4_fclose(ext4_file *f)
1553 ext4_assert(f && f->mp);
1558 f->fpos = f->fsize = 0;
1563 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1565 struct ext4_inode_ref ref;
1569 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1571 EXT4_MP_UNLOCK(f->mp);
1576 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1577 if (f->fsize <= size) {
1582 /*Start write back cache mode.*/
1583 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1587 r = ext4_trunc_inode(f->mp, ref.index, size);
1595 /*Stop write back cache mode*/
1596 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1602 ext4_fs_put_inode_ref(&ref);
1607 int ext4_ftruncate(ext4_file *f, uint64_t size)
1610 ext4_assert(f && f->mp);
1612 if (f->mp->fs.read_only)
1615 if (f->flags & O_RDONLY)
1618 EXT4_MP_LOCK(f->mp);
1620 ext4_trans_start(f->mp);
1621 r = ext4_ftruncate_no_lock(f, size);
1623 ext4_trans_abort(f->mp);
1625 ext4_trans_stop(f->mp);
1627 EXT4_MP_UNLOCK(f->mp);
1631 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1634 uint32_t iblock_idx;
1635 uint32_t iblock_last;
1636 uint32_t block_size;
1638 ext4_fsblk_t fblock;
1639 ext4_fsblk_t fblock_start;
1640 uint32_t fblock_count;
1642 uint8_t *u8_buf = buf;
1644 struct ext4_inode_ref ref;
1646 ext4_assert(f && f->mp);
1648 if (f->mp->fs.read_only)
1651 if (f->flags & O_WRONLY)
1657 EXT4_MP_LOCK(f->mp);
1659 struct ext4_fs *const fs = &f->mp->fs;
1660 struct ext4_sblock *const sb = &f->mp->fs.sb;
1665 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1667 EXT4_MP_UNLOCK(f->mp);
1672 f->fsize = ext4_inode_get_size(sb, ref.inode);
1674 block_size = ext4_sb_get_block_size(sb);
1675 size = ((uint64_t)size > (f->fsize - f->fpos))
1676 ? ((size_t)(f->fsize - f->fpos)) : size;
1678 iblock_idx = (uint32_t)((f->fpos) / block_size);
1679 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1680 unalg = (f->fpos) % block_size;
1682 /*If the size of symlink is smaller than 60 bytes*/
1684 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1685 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1686 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1688 char *content = (char *)ref.inode->blocks;
1689 if (f->fpos < f->fsize) {
1691 if (unalg + size > (uint32_t)f->fsize)
1692 len = (uint32_t)f->fsize - unalg;
1693 memcpy(buf, content + unalg, len);
1705 if (size > (block_size - unalg))
1706 len = block_size - unalg;
1708 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1712 /* Do we get an unwritten range? */
1714 uint64_t off = fblock * block_size + unalg;
1715 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1721 memset(u8_buf, 0, len);
1736 while (size >= block_size) {
1737 while (iblock_idx < iblock_last) {
1738 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1746 fblock_start = fblock;
1748 if ((fblock_start + fblock_count) != fblock)
1754 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1759 size -= block_size * fblock_count;
1760 u8_buf += block_size * fblock_count;
1761 f->fpos += block_size * fblock_count;
1764 *rcnt += block_size * fblock_count;
1766 fblock_start = fblock;
1772 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1776 off = fblock * block_size;
1777 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1788 ext4_fs_put_inode_ref(&ref);
1789 EXT4_MP_UNLOCK(f->mp);
1793 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1797 uint32_t iblock_last;
1798 uint32_t ifile_blocks;
1799 uint32_t block_size;
1801 uint32_t fblock_count;
1803 ext4_fsblk_t fblock_start;
1805 struct ext4_inode_ref ref;
1806 const uint8_t *u8_buf = buf;
1809 ext4_assert(f && f->mp);
1811 if (f->mp->fs.read_only)
1814 if (f->flags & O_RDONLY)
1820 EXT4_MP_LOCK(f->mp);
1821 ext4_trans_start(f->mp);
1823 struct ext4_fs *const fs = &f->mp->fs;
1824 struct ext4_sblock *const sb = &f->mp->fs.sb;
1829 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1831 ext4_trans_abort(f->mp);
1832 EXT4_MP_UNLOCK(f->mp);
1837 f->fsize = ext4_inode_get_size(sb, ref.inode);
1838 block_size = ext4_sb_get_block_size(sb);
1840 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1841 iblk_idx = (uint32_t)(f->fpos / block_size);
1842 ifile_blocks = (uint32_t)((f->fsize + block_size - 1) / block_size);
1844 unalg = (f->fpos) % block_size;
1849 if (size > (block_size - unalg))
1850 len = block_size - unalg;
1852 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1856 off = fblk * block_size + unalg;
1857 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1871 /*Start write back cache mode.*/
1872 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1878 while (size >= block_size) {
1880 while (iblk_idx < iblock_last) {
1881 if (iblk_idx < ifile_blocks) {
1882 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1887 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1890 /* Unable to append more blocks. But
1891 * some block might be allocated already
1899 if (!fblock_start) {
1900 fblock_start = fblk;
1903 if ((fblock_start + fblock_count) != fblk)
1909 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1914 size -= block_size * fblock_count;
1915 u8_buf += block_size * fblock_count;
1916 f->fpos += block_size * fblock_count;
1919 *wcnt += block_size * fblock_count;
1921 fblock_start = fblk;
1925 /*ext4_fs_append_inode_block has failed and no
1926 * more blocks might be written. But node size
1927 * should be updated.*/
1933 /*Stop write back cache mode*/
1934 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1941 if (iblk_idx < ifile_blocks) {
1942 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1946 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1948 /*Node size sholud be updated.*/
1952 off = fblk * block_size;
1953 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1964 if (f->fpos > f->fsize) {
1966 ext4_inode_set_size(ref.inode, f->fsize);
1971 r = ext4_fs_put_inode_ref(&ref);
1974 ext4_trans_abort(f->mp);
1976 ext4_trans_stop(f->mp);
1978 EXT4_MP_UNLOCK(f->mp);
1982 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1986 if (offset > f->fsize)
1992 if ((offset + f->fpos) > f->fsize)
1998 if (offset > f->fsize)
2001 f->fpos = f->fsize - offset;
2007 uint64_t ext4_ftell(ext4_file *f)
2012 uint64_t ext4_fsize(ext4_file *f)
2017 int ext4_chmod(const char *path, uint32_t mode)
2020 uint32_t ino, orig_mode;
2022 struct ext4_sblock *sb;
2023 struct ext4_inode_ref inode_ref;
2024 struct ext4_mountpoint *mp = ext4_get_mount(path);
2029 if (mp->fs.read_only)
2033 ext4_trans_start(mp);
2035 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2037 ext4_trans_abort(mp);
2044 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2046 ext4_trans_abort(mp);
2051 orig_mode = ext4_inode_get_mode(sb, inode_ref.inode);
2052 orig_mode &= ~0xFFF;
2053 orig_mode |= mode & 0xFFF;
2054 ext4_inode_set_mode(sb, inode_ref.inode, orig_mode);
2055 inode_ref.dirty = true;
2057 r = ext4_fs_put_inode_ref(&inode_ref);
2059 ext4_trans_abort(mp);
2061 ext4_trans_stop(mp);
2067 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
2072 struct ext4_inode_ref inode_ref;
2073 struct ext4_mountpoint *mp = ext4_get_mount(path);
2078 if (mp->fs.read_only)
2082 ext4_trans_start(mp);
2084 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2086 ext4_trans_abort(mp);
2092 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2094 ext4_trans_abort(mp);
2099 ext4_inode_set_uid(inode_ref.inode, uid);
2100 ext4_inode_set_gid(inode_ref.inode, gid);
2101 inode_ref.dirty = true;
2103 r = ext4_fs_put_inode_ref(&inode_ref);
2105 ext4_trans_abort(mp);
2107 ext4_trans_stop(mp);
2113 int ext4_file_set_atime(const char *path, uint32_t atime)
2118 struct ext4_inode_ref inode_ref;
2119 struct ext4_mountpoint *mp = ext4_get_mount(path);
2124 if (mp->fs.read_only)
2128 ext4_trans_start(mp);
2130 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2132 ext4_trans_abort(mp);
2138 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2140 ext4_trans_abort(mp);
2145 ext4_inode_set_access_time(inode_ref.inode, atime);
2146 inode_ref.dirty = true;
2148 r = ext4_fs_put_inode_ref(&inode_ref);
2150 ext4_trans_abort(mp);
2152 ext4_trans_stop(mp);
2158 int ext4_file_set_mtime(const char *path, uint32_t mtime)
2163 struct ext4_inode_ref inode_ref;
2164 struct ext4_mountpoint *mp = ext4_get_mount(path);
2169 if (mp->fs.read_only)
2173 ext4_trans_start(mp);
2175 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2177 ext4_trans_abort(mp);
2183 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2185 ext4_trans_abort(mp);
2190 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2191 inode_ref.dirty = true;
2193 r = ext4_fs_put_inode_ref(&inode_ref);
2195 ext4_trans_abort(mp);
2197 ext4_trans_stop(mp);
2203 int ext4_file_set_ctime(const char *path, uint32_t ctime)
2208 struct ext4_inode_ref inode_ref;
2209 struct ext4_mountpoint *mp = ext4_get_mount(path);
2214 if (mp->fs.read_only)
2218 ext4_trans_start(mp);
2220 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2222 ext4_trans_abort(mp);
2228 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2230 ext4_trans_abort(mp);
2235 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2236 inode_ref.dirty = true;
2238 r = ext4_fs_put_inode_ref(&inode_ref);
2240 ext4_trans_abort(mp);
2242 ext4_trans_stop(mp);
2248 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2250 struct ext4_inode_ref ref;
2252 ext4_fsblk_t fblock;
2253 uint32_t block_size;
2256 ext4_assert(f && f->mp);
2261 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2266 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2267 if (size > block_size) {
2271 r = ext4_ftruncate_no_lock(f, 0);
2275 /*Start write back cache mode.*/
2276 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2280 /*If the size of symlink is smaller than 60 bytes*/
2281 if (size < sizeof(ref.inode->blocks)) {
2282 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2283 memcpy(ref.inode->blocks, buf, size);
2284 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2286 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2287 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2291 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2297 /*Stop write back cache mode*/
2298 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2303 ext4_inode_set_size(ref.inode, size);
2311 ext4_fs_put_inode_ref(&ref);
2315 int ext4_fsymlink(const char *target, const char *path)
2317 struct ext4_mountpoint *mp = ext4_get_mount(path);
2325 if (mp->fs.read_only)
2328 filetype = EXT4_DE_SYMLINK;
2331 ext4_trans_start(mp);
2333 ext4_block_cache_write_back(mp->fs.bdev, 1);
2334 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2336 r = ext4_fsymlink_set(&f, target, strlen(target));
2343 ext4_block_cache_write_back(mp->fs.bdev, 0);
2346 ext4_trans_abort(mp);
2348 ext4_trans_stop(mp);
2354 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2356 struct ext4_mountpoint *mp = ext4_get_mount(path);
2367 filetype = EXT4_DE_SYMLINK;
2370 ext4_block_cache_write_back(mp->fs.bdev, 1);
2371 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2373 r = ext4_fread(&f, buf, bufsize, rcnt);
2380 ext4_block_cache_write_back(mp->fs.bdev, 0);
2382 ext4_trans_abort(mp);
2384 ext4_trans_stop(mp);
2390 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2391 const void *data, size_t data_size, bool replace)
2397 const char *dissected_name = NULL;
2398 size_t dissected_len = 0;
2399 struct ext4_xattr_ref xattr_ref;
2400 struct ext4_inode_ref inode_ref;
2401 struct ext4_mountpoint *mp = ext4_get_mount(path);
2405 if (mp->fs.read_only)
2408 dissected_name = ext4_extract_xattr_name(name, name_len,
2409 &name_index, &dissected_len);
2414 ext4_trans_start(mp);
2416 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2422 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2426 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2428 ext4_fs_put_inode_ref(&inode_ref);
2432 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2433 dissected_len, data, data_size, replace);
2435 ext4_fs_put_xattr_ref(&xattr_ref);
2436 ext4_fs_put_inode_ref(&inode_ref);
2439 ext4_trans_abort(mp);
2441 ext4_trans_stop(mp);
2447 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2448 void *buf, size_t buf_size, size_t *data_size)
2454 const char *dissected_name = NULL;
2455 size_t dissected_len = 0;
2456 struct ext4_xattr_ref xattr_ref;
2457 struct ext4_inode_ref inode_ref;
2458 struct ext4_mountpoint *mp = ext4_get_mount(path);
2462 dissected_name = ext4_extract_xattr_name(name, name_len,
2463 &name_index, &dissected_len);
2468 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2474 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2478 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2480 ext4_fs_put_inode_ref(&inode_ref);
2484 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2485 dissected_len, buf, buf_size, data_size);
2487 ext4_fs_put_xattr_ref(&xattr_ref);
2488 ext4_fs_put_inode_ref(&inode_ref);
2494 struct ext4_listxattr_iterator {
2499 bool list_too_small;
2500 bool get_required_size;
2503 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2504 struct ext4_xattr_item *item)
2506 struct ext4_listxattr_iterator *lxi;
2507 lxi = ref->iter_arg;
2508 if (!lxi->get_required_size) {
2511 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2512 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2513 lxi->list_too_small = 1;
2514 return EXT4_XATTR_ITERATE_STOP;
2517 memcpy(lxi->list_ptr, prefix, plen);
2518 lxi->list_ptr += plen;
2519 lxi->ret_size += plen;
2521 memcpy(lxi->list_ptr, item->name, item->name_len);
2522 lxi->list_ptr[item->name_len] = 0;
2523 lxi->list_ptr += item->name_len + 1;
2525 lxi->ret_size += item->name_len + 1;
2526 return EXT4_XATTR_ITERATE_CONT;
2529 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2534 struct ext4_xattr_ref xattr_ref;
2535 struct ext4_inode_ref inode_ref;
2536 struct ext4_listxattr_iterator lxi;
2537 struct ext4_mountpoint *mp = ext4_get_mount(path);
2542 lxi.list_ptr = list;
2545 lxi.list_too_small = false;
2546 lxi.get_required_size = (!size) ? true : false;
2549 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2555 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2559 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2561 ext4_fs_put_inode_ref(&inode_ref);
2565 xattr_ref.iter_arg = &lxi;
2566 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2567 if (lxi.list_too_small)
2572 *ret_size = lxi.ret_size;
2575 ext4_fs_put_xattr_ref(&xattr_ref);
2576 ext4_fs_put_inode_ref(&inode_ref);
2583 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2589 const char *dissected_name = NULL;
2590 size_t dissected_len = 0;
2591 struct ext4_xattr_ref xattr_ref;
2592 struct ext4_inode_ref inode_ref;
2593 struct ext4_mountpoint *mp = ext4_get_mount(path);
2597 if (mp->fs.read_only)
2600 dissected_name = ext4_extract_xattr_name(name, name_len,
2601 &name_index, &dissected_len);
2606 ext4_trans_start(mp);
2608 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2614 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2618 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2620 ext4_fs_put_inode_ref(&inode_ref);
2624 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2627 ext4_fs_put_xattr_ref(&xattr_ref);
2628 ext4_fs_put_inode_ref(&inode_ref);
2631 ext4_trans_abort(mp);
2633 ext4_trans_stop(mp);
2640 /*********************************DIRECTORY OPERATION************************/
2642 int ext4_dir_rm(const char *path)
2648 struct ext4_mountpoint *mp = ext4_get_mount(path);
2649 struct ext4_inode_ref act;
2650 struct ext4_inode_ref child;
2651 struct ext4_dir_iter it;
2655 uint32_t inode_current;
2665 if (mp->fs.read_only)
2670 struct ext4_fs *const fs = &mp->fs;
2673 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2675 ext4_trans_abort(mp);
2681 len = ext4_path_check(path, &is_goal);
2683 inode_current = f.inode;
2685 ext4_block_cache_write_back(mp->fs.bdev, 1);
2689 uint64_t act_curr_pos = 0;
2690 has_children = false;
2693 while (r == EOK && !has_children && !dir_end) {
2695 /*Load directory node.*/
2696 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2701 /*Initialize iterator.*/
2702 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2704 ext4_fs_put_inode_ref(&act);
2713 ext4_trans_start(mp);
2715 /*Get up directory inode when ".." entry*/
2716 if ((it.curr->name_len == 2) &&
2717 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2718 inode_up = ext4_dir_en_get_inode(it.curr);
2721 /*If directory or file entry, but not "." ".." entry*/
2722 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2724 /*Get child inode reference do unlink
2727 uint32_t inode_type;
2728 cinode = ext4_dir_en_get_inode(it.curr);
2729 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2733 /*If directory with no leaf children*/
2734 r = ext4_has_children(&has_children, &child);
2736 ext4_fs_put_inode_ref(&child);
2741 /*Has directory children. Go into this
2743 inode_up = inode_current;
2744 inode_current = cinode;
2746 ext4_fs_put_inode_ref(&child);
2749 inode_type = ext4_inode_type(&mp->fs.sb,
2753 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2754 r = ext4_trunc_inode(mp, child.index, 0);
2756 r = ext4_trunc_dir(mp, &act, &child);
2759 ext4_fs_put_inode_ref(&child);
2763 /*No children in child directory or file. Just
2765 r = ext4_unlink(f.mp, &act, &child,
2766 (char *)it.curr->name,
2769 ext4_fs_put_inode_ref(&child);
2773 ext4_inode_set_del_time(child.inode, -1L);
2774 ext4_inode_set_links_cnt(child.inode, 0);
2777 r = ext4_fs_free_inode(&child);
2779 ext4_fs_put_inode_ref(&child);
2783 r = ext4_fs_put_inode_ref(&child);
2789 r = ext4_dir_iterator_next(&it);
2793 act_curr_pos = it.curr_off;
2795 ext4_dir_iterator_fini(&it);
2797 r = ext4_fs_put_inode_ref(&act);
2799 ext4_fs_put_inode_ref(&act);
2802 ext4_trans_abort(mp);
2804 ext4_trans_stop(mp);
2808 /*Directory iterator reached last entry*/
2811 inode_current = inode_up;
2821 if (r == EOK && !depth) {
2823 struct ext4_inode_ref parent;
2824 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2828 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
2831 ext4_fs_put_inode_ref(&act);
2835 ext4_trans_start(mp);
2837 /* In this place all directories should be
2839 * Last unlink from root of current directory*/
2840 r = ext4_unlink(f.mp, &parent, &act,
2843 ext4_fs_put_inode_ref(&parent);
2844 ext4_fs_put_inode_ref(&act);
2848 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2849 ext4_inode_set_del_time(act.inode, -1L);
2850 ext4_inode_set_links_cnt(act.inode, 0);
2853 r = ext4_trunc_dir(mp, &parent, &act);
2855 ext4_fs_put_inode_ref(&parent);
2856 ext4_fs_put_inode_ref(&act);
2860 r = ext4_fs_free_inode(&act);
2862 ext4_fs_put_inode_ref(&parent);
2863 ext4_fs_put_inode_ref(&act);
2868 r = ext4_fs_put_inode_ref(&parent);
2872 r = ext4_fs_put_inode_ref(&act);
2875 ext4_trans_abort(mp);
2877 ext4_trans_stop(mp);
2880 ext4_block_cache_write_back(mp->fs.bdev, 0);
2886 int ext4_dir_mk(const char *path)
2891 struct ext4_mountpoint *mp = ext4_get_mount(path);
2896 if (mp->fs.read_only)
2900 ext4_trans_start(mp);
2903 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2905 /*Directory already created*/
2906 ext4_trans_stop(mp);
2912 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2914 ext4_trans_abort(mp);
2919 ext4_trans_stop(mp);
2924 int ext4_dir_open(ext4_dir *d, const char *path)
2926 struct ext4_mountpoint *mp = ext4_get_mount(path);
2933 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2939 int ext4_dir_close(ext4_dir *d)
2941 return ext4_fclose(&d->f);
2944 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2946 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2949 ext4_direntry *de = 0;
2950 struct ext4_inode_ref dir;
2951 struct ext4_dir_iter it;
2953 EXT4_MP_LOCK(d->f.mp);
2955 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2956 EXT4_MP_UNLOCK(d->f.mp);
2960 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2965 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2967 ext4_fs_put_inode_ref(&dir);
2971 memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2974 ext4_dir_iterator_next(&it);
2976 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2978 ext4_dir_iterator_fini(&it);
2979 ext4_fs_put_inode_ref(&dir);
2982 EXT4_MP_UNLOCK(d->f.mp);
2986 void ext4_dir_entry_rewind(ext4_dir *d)