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_block_group.h"
52 #include "ext4_dir_idx.h"
53 #include "ext4_xattr.h"
54 #include "ext4_journal.h"
60 /**@brief Mount point OS dependent lock*/
61 #define EXT4_MP_LOCK(_m) \
64 (_m)->os_locks->lock(); \
67 /**@brief Mount point OS dependent unlock*/
68 #define EXT4_MP_UNLOCK(_m) \
71 (_m)->os_locks->unlock(); \
74 /**@brief Mount point descriptor.*/
75 struct ext4_mountpoint {
77 /**@brief Mount done flag.*/
80 /**@brief Mount point name (@ref ext4_mount)*/
81 char name[CONFIG_EXT4_MAX_MP_NAME + 1];
83 /**@brief OS dependent lock/unlock functions.*/
84 const struct ext4_lock *os_locks;
86 /**@brief Ext4 filesystem internals.*/
93 struct jbd_journal jbd_journal;
95 /**@brief Block cache.*/
96 struct ext4_bcache bc;
99 /**@brief Block devices descriptor.*/
100 struct ext4_block_devices {
102 /**@brief Block device name.*/
103 char name[CONFIG_EXT4_MAX_BLOCKDEV_NAME + 1];
105 /**@brief Block device handle.*/
106 struct ext4_blockdev *bd;
109 /**@brief Block devices.*/
110 static struct ext4_block_devices s_bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
112 /**@brief Mountpoints.*/
113 static struct ext4_mountpoint s_mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
115 int ext4_device_register(struct ext4_blockdev *bd,
116 const char *dev_name)
118 ext4_assert(bd && dev_name);
120 if (strlen(dev_name) > CONFIG_EXT4_MAX_BLOCKDEV_NAME)
123 for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
124 if (!strcmp(s_bdevices[i].name, dev_name))
128 for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
129 if (!s_bdevices[i].bd) {
130 strcpy(s_bdevices[i].name, dev_name);
131 s_bdevices[i].bd = bd;
139 int ext4_device_unregister(const char *dev_name)
141 ext4_assert(dev_name);
143 for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
144 if (strcmp(s_bdevices[i].name, dev_name))
147 memset(&s_bdevices[i], 0, sizeof(s_bdevices[i]));
153 int ext4_device_unregister_all(void)
155 memset(s_bdevices, 0, sizeof(s_bdevices));
160 /****************************************************************************/
162 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
164 if ((name_size == 1) && (name[0] == '.'))
167 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
173 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
175 struct ext4_sblock *sb = &enode->fs->sb;
177 /* Check if node is directory */
178 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
179 *has_children = false;
183 struct ext4_dir_iter it;
184 int rc = ext4_dir_iterator_init(&it, enode, 0);
188 /* Find a non-empty directory entry */
190 while (it.curr != NULL) {
191 if (ext4_dir_en_get_inode(it.curr) != 0) {
193 nsize = ext4_dir_en_get_name_len(sb, it.curr);
194 if (!ext4_is_dots(it.curr->name, nsize)) {
200 rc = ext4_dir_iterator_next(&it);
202 ext4_dir_iterator_fini(&it);
207 rc = ext4_dir_iterator_fini(&it);
211 *has_children = found;
216 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
217 struct ext4_inode_ref *ch, const char *n,
218 uint32_t len, bool rename)
220 /* Check maximum name length */
221 if (len > EXT4_DIRECTORY_FILENAME_LEN)
224 /* Add entry to parent directory */
225 int r = ext4_dir_add_entry(parent, n, len, ch);
229 /* Fill new dir -> add '.' and '..' entries.
230 * Also newly allocated inode should have 0 link count.
233 bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
234 EXT4_INODE_MODE_DIRECTORY);
235 if (is_dir && !rename) {
237 #if CONFIG_DIR_INDEX_ENABLE
238 /* Initialize directory index if supported */
239 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
240 r = ext4_dir_dx_init(ch, parent);
244 ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
249 r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
251 ext4_dir_remove_entry(parent, n, strlen(n));
255 r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
257 ext4_dir_remove_entry(parent, n, strlen(n));
258 ext4_dir_remove_entry(ch, ".", strlen("."));
263 /*New empty directory. Two links (. and ..) */
264 ext4_inode_set_links_cnt(ch->inode, 2);
265 ext4_fs_inode_links_count_inc(parent);
267 parent->dirty = true;
271 * In case we want to rename a directory,
272 * we reset the original '..' pointer.
276 idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
277 struct ext4_dir_search_result res;
279 r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
283 ext4_dir_en_set_inode(res.dentry, parent->index);
284 ext4_trans_set_block_dirty(res.block.buf);
285 r = ext4_dir_destroy_result(ch, &res);
290 #if CONFIG_DIR_INDEX_ENABLE
291 r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
298 ext4_fs_inode_links_count_inc(parent);
299 parent->dirty = true;
302 ext4_fs_inode_links_count_inc(ch);
309 static int ext4_unlink(struct ext4_mountpoint *mp,
310 struct ext4_inode_ref *parent,
311 struct ext4_inode_ref *child, const char *name,
315 int rc = ext4_has_children(&has_children, child);
319 /* Cannot unlink non-empty node */
323 /* Remove entry from parent directory */
324 rc = ext4_dir_remove_entry(parent, name, name_len);
328 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
329 EXT4_INODE_MODE_DIRECTORY);
331 /* If directory - handle links from parent */
333 ext4_fs_inode_links_count_dec(parent);
334 parent->dirty = true;
338 * TODO: Update timestamps of the parent
339 * (when we have wall-clock time).
341 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
342 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
343 * parent->dirty = true;
347 * TODO: Update timestamp for inode.
349 * ext4_inode_set_change_inode_time(child->inode,
352 if (ext4_inode_get_links_cnt(child->inode)) {
353 ext4_fs_inode_links_count_dec(child);
360 /****************************************************************************/
362 int ext4_mount(const char *dev_name, const char *mount_point,
367 struct ext4_bcache *bc;
368 struct ext4_blockdev *bd = 0;
369 struct ext4_mountpoint *mp = 0;
371 ext4_assert(mount_point && dev_name);
373 size_t mp_len = strlen(mount_point);
375 if (mp_len > CONFIG_EXT4_MAX_MP_NAME)
378 if (mount_point[mp_len - 1] != '/')
381 for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
382 if (s_bdevices[i].name) {
383 if (!strcmp(dev_name, s_bdevices[i].name)) {
384 bd = s_bdevices[i].bd;
393 for (size_t i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
394 if (!s_mp[i].mounted) {
395 strcpy(s_mp[i].name, mount_point);
401 if (!strcmp(s_mp[i].name, mount_point))
408 r = ext4_block_init(bd);
412 r = ext4_fs_init(&mp->fs, bd, read_only);
418 bsize = ext4_sb_get_block_size(&mp->fs.sb);
419 ext4_block_set_lb_size(bd, bsize);
422 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE, bsize);
428 if (bsize != bc->itemsize)
431 /*Bind block cache to block device*/
432 r = ext4_block_bind_bcache(bd, bc);
434 ext4_bcache_cleanup(bc);
436 ext4_bcache_fini_dynamic(bc);
445 int ext4_umount(const char *mount_point)
449 struct ext4_mountpoint *mp = 0;
451 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
452 if (!strcmp(s_mp[i].name, mount_point)) {
461 r = ext4_fs_fini(&mp->fs);
467 ext4_bcache_cleanup(mp->fs.bdev->bc);
468 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
470 r = ext4_block_fini(mp->fs.bdev);
472 mp->fs.bdev->fs = NULL;
476 static struct ext4_mountpoint *ext4_get_mount(const char *path)
479 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
481 if (!s_mp[i].mounted)
484 if (!strncmp(s_mp[i].name, path, strlen(s_mp[i].name)))
491 static int __ext4_journal_start(const char *mount_point)
494 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
498 if (mp->fs.read_only)
501 if (ext4_sb_feature_com(&mp->fs.sb,
502 EXT4_FCOM_HAS_JOURNAL)) {
503 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
507 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
509 mp->jbd_fs.dirty = false;
510 jbd_put_fs(&mp->jbd_fs);
513 mp->fs.jbd_fs = &mp->jbd_fs;
514 mp->fs.jbd_journal = &mp->jbd_journal;
521 static int __ext4_journal_stop(const char *mount_point)
524 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
528 if (mp->fs.read_only)
531 if (ext4_sb_feature_com(&mp->fs.sb,
532 EXT4_FCOM_HAS_JOURNAL)) {
533 r = jbd_journal_stop(&mp->jbd_journal);
535 mp->jbd_fs.dirty = false;
536 jbd_put_fs(&mp->jbd_fs);
537 mp->fs.jbd_journal = NULL;
538 mp->fs.jbd_fs = NULL;
542 r = jbd_put_fs(&mp->jbd_fs);
544 mp->fs.jbd_journal = NULL;
545 mp->fs.jbd_fs = NULL;
549 mp->fs.jbd_journal = NULL;
550 mp->fs.jbd_fs = NULL;
557 static int __ext4_recover(const char *mount_point)
559 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
565 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
566 struct jbd_fs *jbd_fs = ext4_calloc(1, sizeof(struct jbd_fs));
573 r = jbd_get_fs(&mp->fs, jbd_fs);
579 r = jbd_recover(jbd_fs);
583 if (r == EOK && !mp->fs.read_only) {
585 uint64_t free_blocks_count = 0;
586 uint32_t free_inodes_count = 0;
587 struct ext4_block_group_ref bg_ref;
589 /* Update superblock's stats */
590 for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) {
591 r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref);
596 ext4_bg_get_free_blocks_count(bg_ref.block_group,
599 ext4_bg_get_free_inodes_count(bg_ref.block_group,
602 ext4_fs_put_block_group_ref(&bg_ref);
604 ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count);
605 ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count);
606 /* We don't need to save the superblock stats immediately. */
615 static int __ext4_trans_start(struct ext4_mountpoint *mp)
618 if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
619 struct jbd_journal *journal = mp->fs.jbd_journal;
620 struct jbd_trans *trans;
621 trans = jbd_journal_new_trans(journal);
626 mp->fs.curr_trans = trans;
633 static int __ext4_trans_stop(struct ext4_mountpoint *mp)
636 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
637 struct jbd_journal *journal = mp->fs.jbd_journal;
638 struct jbd_trans *trans = mp->fs.curr_trans;
639 r = jbd_journal_commit_trans(journal, trans);
640 mp->fs.curr_trans = NULL;
646 static void __ext4_trans_abort(struct ext4_mountpoint *mp)
648 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
649 struct jbd_journal *journal = mp->fs.jbd_journal;
650 struct jbd_trans *trans = mp->fs.curr_trans;
651 jbd_journal_free_trans(journal, trans, true);
652 mp->fs.curr_trans = NULL;
656 int ext4_journal_start(const char *mount_point __unused)
659 #if CONFIG_JOURNALING_ENABLE
660 r = __ext4_journal_start(mount_point);
665 int ext4_journal_stop(const char *mount_point __unused)
668 #if CONFIG_JOURNALING_ENABLE
669 r = __ext4_journal_stop(mount_point);
674 int ext4_recover(const char *mount_point __unused)
677 #if CONFIG_JOURNALING_ENABLE
678 r = __ext4_recover(mount_point);
683 static int ext4_trans_start(struct ext4_mountpoint *mp __unused)
686 #if CONFIG_JOURNALING_ENABLE
687 r = __ext4_trans_start(mp);
692 static int ext4_trans_stop(struct ext4_mountpoint *mp __unused)
695 #if CONFIG_JOURNALING_ENABLE
696 r = __ext4_trans_stop(mp);
701 static void ext4_trans_abort(struct ext4_mountpoint *mp __unused)
703 #if CONFIG_JOURNALING_ENABLE
704 __ext4_trans_abort(mp);
709 int ext4_mount_point_stats(const char *mount_point,
710 struct ext4_mount_stats *stats)
712 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
718 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
719 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
720 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
721 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
722 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
724 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
725 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
726 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
728 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
734 int ext4_mount_setup_locks(const char *mount_point,
735 const struct ext4_lock *locks)
738 struct ext4_mountpoint *mp = 0;
740 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
741 if (!strcmp(s_mp[i].name, mount_point)) {
749 mp->os_locks = locks;
753 /********************************FILE OPERATIONS*****************************/
755 static int ext4_path_check(const char *path, bool *is_goal)
759 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
761 if (path[i] == '/') {
775 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
780 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
781 *file_flags = O_RDONLY;
785 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
786 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
790 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
791 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
795 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
796 !strcmp(flags, "r+b")) {
797 *file_flags = O_RDWR;
801 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
802 !strcmp(flags, "w+b")) {
803 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
807 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
808 !strcmp(flags, "a+b")) {
809 *file_flags = O_RDWR | O_CREAT | O_APPEND;
816 static int ext4_trunc_inode(struct ext4_mountpoint *mp,
817 uint32_t index, uint64_t new_size)
820 struct ext4_fs *const fs = &mp->fs;
821 struct ext4_inode_ref inode_ref;
823 bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
824 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
828 inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
829 ext4_fs_put_inode_ref(&inode_ref);
833 while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
835 inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
837 ext4_trans_start(mp);
838 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
840 ext4_trans_abort(mp);
843 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
845 ext4_fs_put_inode_ref(&inode_ref);
847 r = ext4_fs_put_inode_ref(&inode_ref);
850 ext4_trans_abort(mp);
856 if (inode_size > new_size) {
858 inode_size = new_size;
860 ext4_trans_start(mp);
861 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
863 ext4_trans_abort(mp);
866 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
868 ext4_fs_put_inode_ref(&inode_ref);
870 r = ext4_fs_put_inode_ref(&inode_ref);
873 ext4_trans_abort(mp);
882 ext4_trans_start(mp);
887 static int ext4_trunc_dir(struct ext4_mountpoint *mp,
888 struct ext4_inode_ref *parent,
889 struct ext4_inode_ref *dir)
892 bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
893 EXT4_INODE_MODE_DIRECTORY);
894 uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
898 #if CONFIG_DIR_INDEX_ENABLE
899 /* Initialize directory index if supported */
900 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
901 r = ext4_dir_dx_init(dir, parent);
905 r = ext4_trunc_inode(mp, dir->index,
906 EXT4_DIR_DX_INIT_BCNT * block_size);
912 r = ext4_trunc_inode(mp, dir->index, block_size);
917 return ext4_fs_truncate_inode(dir, 0);
921 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
922 * any filetype of the target dir entry will be accepted.
924 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
925 int ftype, uint32_t *parent_inode,
928 bool is_goal = false;
929 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
934 struct ext4_mountpoint *mp = ext4_get_mount(path);
935 struct ext4_dir_search_result result;
936 struct ext4_inode_ref ref;
943 struct ext4_fs *const fs = &mp->fs;
944 struct ext4_sblock *const sb = &mp->fs.sb;
946 if (fs->read_only && flags & O_CREAT)
952 path += strlen(mp->name);
955 *name_off = strlen(mp->name);
958 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
963 *parent_inode = ref.index;
966 ext4_trans_start(mp);
968 len = ext4_path_check(path, &is_goal);
971 len = ext4_path_check(path, &is_goal);
973 /*If root open was request.*/
974 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
982 r = ext4_dir_find_entry(&result, &ref, path, len);
985 /*Destroy last result*/
986 ext4_dir_destroy_result(&ref, &result);
990 if (!(f->flags & O_CREAT))
993 /*O_CREAT allows create new entry*/
994 struct ext4_inode_ref child_ref;
995 r = ext4_fs_alloc_inode(fs, &child_ref,
996 is_goal ? ftype : EXT4_DE_DIR);
1001 /*Link with root dir.*/
1002 r = ext4_link(mp, &ref, &child_ref, path, len, false);
1004 /*Fail. Free new inode.*/
1005 ext4_fs_free_inode(&child_ref);
1006 /*We do not want to write new inode.
1007 But block has to be released.*/
1008 child_ref.dirty = false;
1009 ext4_fs_put_inode_ref(&child_ref);
1013 ext4_fs_put_inode_ref(&child_ref);
1018 *parent_inode = ref.index;
1020 next_inode = ext4_dir_en_get_inode(result.dentry);
1021 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1023 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1024 imode = ext4_fs_correspond_inode_mode(t);
1026 struct ext4_inode_ref child_ref;
1027 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1031 imode = ext4_inode_type(sb, child_ref.inode);
1032 ext4_fs_put_inode_ref(&child_ref);
1035 r = ext4_dir_destroy_result(&ref, &result);
1039 /*If expected file error*/
1040 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
1044 if (ftype != EXT4_DE_UNKNOWN) {
1045 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
1046 if (df && is_goal) {
1052 r = ext4_fs_put_inode_ref(&ref);
1056 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1066 *name_off += len + 1;
1070 ext4_fs_put_inode_ref(&ref);
1076 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
1077 r = ext4_trunc_inode(mp, ref.index, 0);
1079 ext4_fs_put_inode_ref(&ref);
1085 f->fsize = ext4_inode_get_size(sb, ref.inode);
1086 f->inode = ref.index;
1089 if (f->flags & O_APPEND)
1094 r = ext4_fs_put_inode_ref(&ref);
1095 if (flags & O_CREAT) {
1097 ext4_trans_stop(mp);
1099 ext4_trans_abort(mp);
1106 /****************************************************************************/
1108 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
1109 bool file_expect, uint32_t *parent_inode,
1114 if (ext4_parse_flags(flags, &iflags) == false)
1117 if (file_expect == true)
1118 filetype = EXT4_DE_REG_FILE;
1120 filetype = EXT4_DE_DIR;
1122 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
1126 static int ext4_create_hardlink(const char *path,
1127 struct ext4_inode_ref *child_ref, bool rename)
1129 bool is_goal = false;
1130 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
1131 uint32_t next_inode;
1135 struct ext4_mountpoint *mp = ext4_get_mount(path);
1136 struct ext4_dir_search_result result;
1137 struct ext4_inode_ref ref;
1142 struct ext4_fs *const fs = &mp->fs;
1143 struct ext4_sblock *const sb = &mp->fs.sb;
1145 /*Skip mount point*/
1146 path += strlen(mp->name);
1149 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
1153 len = ext4_path_check(path, &is_goal);
1156 len = ext4_path_check(path, &is_goal);
1158 /*If root open was request.*/
1159 r = is_goal ? EINVAL : ENOENT;
1163 r = ext4_dir_find_entry(&result, &ref, path, len);
1166 /*Destroy last result*/
1167 ext4_dir_destroy_result(&ref, &result);
1169 if (r != ENOENT || !is_goal)
1172 /*Link with root dir.*/
1173 r = ext4_link(mp, &ref, child_ref, path, len, rename);
1175 } else if (r == EOK && is_goal) {
1176 /*Destroy last result*/
1177 ext4_dir_destroy_result(&ref, &result);
1182 next_inode = result.dentry->inode;
1183 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1185 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1186 inode_mode = ext4_fs_correspond_inode_mode(t);
1188 struct ext4_inode_ref child_ref;
1189 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1193 inode_mode = ext4_inode_type(sb, child_ref.inode);
1194 ext4_fs_put_inode_ref(&child_ref);
1197 r = ext4_dir_destroy_result(&ref, &result);
1201 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1202 r = is_goal ? EEXIST : ENOENT;
1206 r = ext4_fs_put_inode_ref(&ref);
1210 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1221 ext4_fs_put_inode_ref(&ref);
1225 r = ext4_fs_put_inode_ref(&ref);
1229 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1230 struct ext4_inode_ref *parent_ref,
1231 struct ext4_inode_ref *child_ref)
1236 struct ext4_mountpoint *mp = ext4_get_mount(path);
1244 len = ext4_path_check(path, &is_goal);
1246 /* Remove entry from parent directory */
1247 r = ext4_dir_remove_entry(parent_ref, path, len);
1251 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1252 EXT4_INODE_MODE_DIRECTORY)) {
1253 ext4_fs_inode_links_count_dec(parent_ref);
1254 parent_ref->dirty = true;
1260 int ext4_flink(const char *path, const char *hardlink_path)
1265 bool child_loaded = false;
1266 uint32_t parent_inode, child_inode;
1267 struct ext4_mountpoint *mp = ext4_get_mount(path);
1268 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1269 struct ext4_inode_ref child_ref;
1274 if (mp->fs.read_only)
1277 /* Will that happen? Anyway return EINVAL for such case. */
1278 if (mp != target_mp)
1282 ext4_trans_start(mp);
1284 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1285 &parent_inode, &name_off);
1289 child_inode = f.inode;
1292 /*We have file to unlink. Load it.*/
1293 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1297 child_loaded = true;
1299 /* Creating hardlink for directory is not allowed. */
1300 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1301 EXT4_INODE_MODE_DIRECTORY)) {
1306 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1310 ext4_fs_put_inode_ref(&child_ref);
1313 ext4_trans_abort(mp);
1315 ext4_trans_stop(mp);
1322 int ext4_frename(const char *path, const char *new_path)
1327 bool parent_loaded = false, child_loaded = false;
1328 uint32_t parent_inode, child_inode;
1329 struct ext4_mountpoint *mp = ext4_get_mount(path);
1330 struct ext4_inode_ref child_ref, parent_ref;
1335 if (mp->fs.read_only)
1339 ext4_trans_start(mp);
1341 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1342 &parent_inode, &name_off);
1346 child_inode = f.inode;
1350 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1354 parent_loaded = true;
1356 /*We have file to unlink. Load it.*/
1357 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1361 child_loaded = true;
1363 r = ext4_create_hardlink(new_path, &child_ref, true);
1367 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1373 ext4_fs_put_inode_ref(&parent_ref);
1376 ext4_fs_put_inode_ref(&child_ref);
1379 ext4_trans_abort(mp);
1381 ext4_trans_stop(mp);
1388 /****************************************************************************/
1390 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1392 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1401 int ext4_cache_write_back(const char *path, bool on)
1403 struct ext4_mountpoint *mp = ext4_get_mount(path);
1410 ret = ext4_block_cache_write_back(mp->fs.bdev, on);
1415 int ext4_cache_flush(const char *path)
1417 struct ext4_mountpoint *mp = ext4_get_mount(path);
1424 ret = ext4_block_cache_flush(mp->fs.bdev);
1429 int ext4_fremove(const char *path)
1432 uint32_t parent_inode;
1437 struct ext4_inode_ref child;
1438 struct ext4_inode_ref parent;
1439 struct ext4_mountpoint *mp = ext4_get_mount(path);
1444 if (mp->fs.read_only)
1448 ext4_trans_start(mp);
1450 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1451 &parent_inode, &name_off);
1453 ext4_trans_abort(mp);
1459 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1461 ext4_trans_abort(mp);
1466 /*We have file to delete. Load it.*/
1467 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1469 ext4_fs_put_inode_ref(&parent);
1470 ext4_trans_abort(mp);
1474 /* We do not allow opening files here. */
1475 if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1476 EXT4_INODE_MODE_DIRECTORY) {
1477 ext4_fs_put_inode_ref(&parent);
1478 ext4_fs_put_inode_ref(&child);
1479 ext4_trans_abort(mp);
1484 /*Link count will be zero, the inode should be freed. */
1485 if (ext4_inode_get_links_cnt(child.inode) == 1) {
1486 ext4_block_cache_write_back(mp->fs.bdev, 1);
1487 r = ext4_trunc_inode(mp, child.index, 0);
1489 ext4_fs_put_inode_ref(&parent);
1490 ext4_fs_put_inode_ref(&child);
1491 ext4_trans_abort(mp);
1495 ext4_block_cache_write_back(mp->fs.bdev, 0);
1501 len = ext4_path_check(path, &is_goal);
1503 /*Unlink from parent*/
1504 r = ext4_unlink(mp, &parent, &child, path, len);
1508 /*Link count is zero, the inode should be freed. */
1509 if (!ext4_inode_get_links_cnt(child.inode)) {
1510 ext4_inode_set_del_time(child.inode, -1L);
1512 r = ext4_fs_free_inode(&child);
1518 ext4_fs_put_inode_ref(&child);
1519 ext4_fs_put_inode_ref(&parent);
1522 ext4_trans_abort(mp);
1524 ext4_trans_stop(mp);
1530 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1531 struct ext4_inode *inode)
1535 struct ext4_inode_ref inode_ref;
1536 struct ext4_mountpoint *mp = ext4_get_mount(path);
1544 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1554 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1560 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1561 ext4_fs_put_inode_ref(&inode_ref);
1570 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1572 struct ext4_mountpoint *mp = ext4_get_mount(path);
1580 ext4_block_cache_write_back(mp->fs.bdev, 1);
1581 r = ext4_generic_open(f, path, flags, true, 0, 0);
1582 ext4_block_cache_write_back(mp->fs.bdev, 0);
1588 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1590 struct ext4_mountpoint *mp = ext4_get_mount(path);
1597 filetype = EXT4_DE_REG_FILE;
1601 ext4_block_cache_write_back(mp->fs.bdev, 1);
1602 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1603 ext4_block_cache_write_back(mp->fs.bdev, 0);
1609 int ext4_fclose(ext4_file *f)
1611 ext4_assert(f && f->mp);
1616 f->fpos = f->fsize = 0;
1621 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1623 struct ext4_inode_ref ref;
1627 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1629 EXT4_MP_UNLOCK(f->mp);
1634 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1635 if (f->fsize <= size) {
1640 /*Start write back cache mode.*/
1641 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1645 r = ext4_trunc_inode(f->mp, ref.index, size);
1653 /*Stop write back cache mode*/
1654 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1660 ext4_fs_put_inode_ref(&ref);
1665 int ext4_ftruncate(ext4_file *f, uint64_t size)
1668 ext4_assert(f && f->mp);
1670 if (f->mp->fs.read_only)
1673 if (f->flags & O_RDONLY)
1676 EXT4_MP_LOCK(f->mp);
1678 ext4_trans_start(f->mp);
1679 r = ext4_ftruncate_no_lock(f, size);
1681 ext4_trans_abort(f->mp);
1683 ext4_trans_stop(f->mp);
1685 EXT4_MP_UNLOCK(f->mp);
1689 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1692 uint32_t iblock_idx;
1693 uint32_t iblock_last;
1694 uint32_t block_size;
1696 ext4_fsblk_t fblock;
1697 ext4_fsblk_t fblock_start;
1698 uint32_t fblock_count;
1700 uint8_t *u8_buf = buf;
1702 struct ext4_inode_ref ref;
1704 ext4_assert(f && f->mp);
1706 if (f->flags & O_WRONLY)
1712 EXT4_MP_LOCK(f->mp);
1714 struct ext4_fs *const fs = &f->mp->fs;
1715 struct ext4_sblock *const sb = &f->mp->fs.sb;
1720 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1722 EXT4_MP_UNLOCK(f->mp);
1727 f->fsize = ext4_inode_get_size(sb, ref.inode);
1729 block_size = ext4_sb_get_block_size(sb);
1730 size = ((uint64_t)size > (f->fsize - f->fpos))
1731 ? ((size_t)(f->fsize - f->fpos)) : size;
1733 iblock_idx = (uint32_t)((f->fpos) / block_size);
1734 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1735 unalg = (f->fpos) % block_size;
1737 /*If the size of symlink is smaller than 60 bytes*/
1739 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1740 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1741 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1743 char *content = (char *)ref.inode->blocks;
1744 if (f->fpos < f->fsize) {
1746 if (unalg + size > (uint32_t)f->fsize)
1747 len = (uint32_t)f->fsize - unalg;
1748 memcpy(buf, content + unalg, len);
1760 if (size > (block_size - unalg))
1761 len = block_size - unalg;
1763 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1767 /* Do we get an unwritten range? */
1769 uint64_t off = fblock * block_size + unalg;
1770 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1776 memset(u8_buf, 0, len);
1791 while (size >= block_size) {
1792 while (iblock_idx < iblock_last) {
1793 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1801 fblock_start = fblock;
1803 if ((fblock_start + fblock_count) != fblock)
1809 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1814 size -= block_size * fblock_count;
1815 u8_buf += block_size * fblock_count;
1816 f->fpos += block_size * fblock_count;
1819 *rcnt += block_size * fblock_count;
1821 fblock_start = fblock;
1827 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1831 off = fblock * block_size;
1832 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1843 ext4_fs_put_inode_ref(&ref);
1844 EXT4_MP_UNLOCK(f->mp);
1848 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1852 uint32_t iblock_last;
1853 uint32_t ifile_blocks;
1854 uint32_t block_size;
1856 uint32_t fblock_count;
1858 ext4_fsblk_t fblock_start;
1860 struct ext4_inode_ref ref;
1861 const uint8_t *u8_buf = buf;
1864 ext4_assert(f && f->mp);
1866 if (f->mp->fs.read_only)
1869 if (f->flags & O_RDONLY)
1875 EXT4_MP_LOCK(f->mp);
1876 ext4_trans_start(f->mp);
1878 struct ext4_fs *const fs = &f->mp->fs;
1879 struct ext4_sblock *const sb = &f->mp->fs.sb;
1884 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1886 ext4_trans_abort(f->mp);
1887 EXT4_MP_UNLOCK(f->mp);
1892 f->fsize = ext4_inode_get_size(sb, ref.inode);
1893 block_size = ext4_sb_get_block_size(sb);
1895 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1896 iblk_idx = (uint32_t)(f->fpos / block_size);
1897 ifile_blocks = (uint32_t)((f->fsize + block_size - 1) / block_size);
1899 unalg = (f->fpos) % block_size;
1904 if (size > (block_size - unalg))
1905 len = block_size - unalg;
1907 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1911 off = fblk * block_size + unalg;
1912 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1926 /*Start write back cache mode.*/
1927 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1933 while (size >= block_size) {
1935 while (iblk_idx < iblock_last) {
1936 if (iblk_idx < ifile_blocks) {
1937 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1942 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1945 /* Unable to append more blocks. But
1946 * some block might be allocated already
1954 if (!fblock_start) {
1955 fblock_start = fblk;
1958 if ((fblock_start + fblock_count) != fblk)
1964 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1969 size -= block_size * fblock_count;
1970 u8_buf += block_size * fblock_count;
1971 f->fpos += block_size * fblock_count;
1974 *wcnt += block_size * fblock_count;
1976 fblock_start = fblk;
1980 /*ext4_fs_append_inode_block has failed and no
1981 * more blocks might be written. But node size
1982 * should be updated.*/
1988 /*Stop write back cache mode*/
1989 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1996 if (iblk_idx < ifile_blocks) {
1997 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
2001 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
2003 /*Node size sholud be updated.*/
2007 off = fblk * block_size;
2008 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
2019 if (f->fpos > f->fsize) {
2021 ext4_inode_set_size(ref.inode, f->fsize);
2026 r = ext4_fs_put_inode_ref(&ref);
2029 ext4_trans_abort(f->mp);
2031 ext4_trans_stop(f->mp);
2033 EXT4_MP_UNLOCK(f->mp);
2037 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
2041 if (offset > f->fsize)
2047 if ((offset + f->fpos) > f->fsize)
2053 if (offset > f->fsize)
2056 f->fpos = f->fsize - offset;
2062 uint64_t ext4_ftell(ext4_file *f)
2067 uint64_t ext4_fsize(ext4_file *f)
2073 static int ext4_trans_get_inode_ref(const char *path,
2074 struct ext4_mountpoint *mp,
2075 struct ext4_inode_ref *inode_ref)
2080 ext4_trans_start(mp);
2081 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2083 ext4_trans_abort(mp);
2087 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, inode_ref);
2089 ext4_trans_abort(mp);
2096 static int ext4_trans_put_inode_ref(struct ext4_mountpoint *mp,
2097 struct ext4_inode_ref *inode_ref)
2101 r = ext4_fs_put_inode_ref(inode_ref);
2103 ext4_trans_abort(mp);
2105 ext4_trans_stop(mp);
2110 int ext4_mode_set(const char *path, uint32_t mode)
2114 struct ext4_inode_ref inode_ref;
2115 struct ext4_mountpoint *mp = ext4_get_mount(path);
2120 if (mp->fs.read_only)
2125 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2129 orig_mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2130 orig_mode &= ~0xFFF;
2131 orig_mode |= mode & 0xFFF;
2132 ext4_inode_set_mode(&mp->fs.sb, inode_ref.inode, orig_mode);
2134 inode_ref.dirty = true;
2135 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2143 int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid)
2146 struct ext4_inode_ref inode_ref;
2147 struct ext4_mountpoint *mp = ext4_get_mount(path);
2152 if (mp->fs.read_only)
2157 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2161 ext4_inode_set_uid(inode_ref.inode, uid);
2162 ext4_inode_set_gid(inode_ref.inode, gid);
2164 inode_ref.dirty = true;
2165 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2173 int ext4_mode_get(const char *path, uint32_t *mode)
2175 struct ext4_inode_ref inode_ref;
2176 struct ext4_mountpoint *mp = ext4_get_mount(path);
2185 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2189 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2193 *mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2194 r = ext4_fs_put_inode_ref(&inode_ref);
2202 int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid)
2204 struct ext4_inode_ref inode_ref;
2205 struct ext4_mountpoint *mp = ext4_get_mount(path);
2214 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2218 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2222 *uid = ext4_inode_get_uid(inode_ref.inode);
2223 *gid = ext4_inode_get_gid(inode_ref.inode);
2224 r = ext4_fs_put_inode_ref(&inode_ref);
2232 int ext4_atime_set(const char *path, uint32_t atime)
2234 struct ext4_inode_ref inode_ref;
2235 struct ext4_mountpoint *mp = ext4_get_mount(path);
2241 if (mp->fs.read_only)
2246 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2250 ext4_inode_set_access_time(inode_ref.inode, atime);
2251 inode_ref.dirty = true;
2252 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2260 int ext4_mtime_set(const char *path, uint32_t mtime)
2262 struct ext4_inode_ref inode_ref;
2263 struct ext4_mountpoint *mp = ext4_get_mount(path);
2269 if (mp->fs.read_only)
2274 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2278 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2279 inode_ref.dirty = true;
2280 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2288 int ext4_ctime_set(const char *path, uint32_t ctime)
2290 struct ext4_inode_ref inode_ref;
2291 struct ext4_mountpoint *mp = ext4_get_mount(path);
2297 if (mp->fs.read_only)
2302 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2306 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2307 inode_ref.dirty = true;
2308 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2316 int ext4_atime_get(const char *path, uint32_t *atime)
2318 struct ext4_inode_ref inode_ref;
2319 struct ext4_mountpoint *mp = ext4_get_mount(path);
2328 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2332 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2336 *atime = ext4_inode_get_access_time(inode_ref.inode);
2337 r = ext4_fs_put_inode_ref(&inode_ref);
2345 int ext4_mtime_get(const char *path, uint32_t *mtime)
2347 struct ext4_inode_ref inode_ref;
2348 struct ext4_mountpoint *mp = ext4_get_mount(path);
2357 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2361 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2365 *mtime = ext4_inode_get_modif_time(inode_ref.inode);
2366 r = ext4_fs_put_inode_ref(&inode_ref);
2374 int ext4_ctime_get(const char *path, uint32_t *ctime)
2376 struct ext4_inode_ref inode_ref;
2377 struct ext4_mountpoint *mp = ext4_get_mount(path);
2386 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2390 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2394 *ctime = ext4_inode_get_change_inode_time(inode_ref.inode);
2395 r = ext4_fs_put_inode_ref(&inode_ref);
2403 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2405 struct ext4_inode_ref ref;
2407 ext4_fsblk_t fblock;
2408 uint32_t block_size;
2411 ext4_assert(f && f->mp);
2416 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2421 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2422 if (size > block_size) {
2426 r = ext4_ftruncate_no_lock(f, 0);
2430 /*Start write back cache mode.*/
2431 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2435 /*If the size of symlink is smaller than 60 bytes*/
2436 if (size < sizeof(ref.inode->blocks)) {
2437 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2438 memcpy(ref.inode->blocks, buf, size);
2439 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2441 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2442 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2446 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2452 /*Stop write back cache mode*/
2453 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2458 ext4_inode_set_size(ref.inode, size);
2466 ext4_fs_put_inode_ref(&ref);
2470 int ext4_fsymlink(const char *target, const char *path)
2472 struct ext4_mountpoint *mp = ext4_get_mount(path);
2480 if (mp->fs.read_only)
2483 filetype = EXT4_DE_SYMLINK;
2486 ext4_trans_start(mp);
2488 ext4_block_cache_write_back(mp->fs.bdev, 1);
2489 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2491 r = ext4_fsymlink_set(&f, target, strlen(target));
2498 ext4_block_cache_write_back(mp->fs.bdev, 0);
2501 ext4_trans_abort(mp);
2503 ext4_trans_stop(mp);
2509 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2511 struct ext4_mountpoint *mp = ext4_get_mount(path);
2522 filetype = EXT4_DE_SYMLINK;
2525 ext4_block_cache_write_back(mp->fs.bdev, 1);
2526 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2528 r = ext4_fread(&f, buf, bufsize, rcnt);
2535 ext4_block_cache_write_back(mp->fs.bdev, 0);
2537 ext4_trans_abort(mp);
2539 ext4_trans_stop(mp);
2545 static int ext4_mknod_set(ext4_file *f, uint32_t dev)
2547 struct ext4_inode_ref ref;
2550 ext4_assert(f && f->mp);
2552 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2556 ext4_inode_set_dev(ref.inode, dev);
2558 ext4_inode_set_size(ref.inode, 0);
2564 r = ext4_fs_put_inode_ref(&ref);
2568 int ext4_mknod(const char *path, int filetype, uint32_t dev)
2570 struct ext4_mountpoint *mp = ext4_get_mount(path);
2577 if (mp->fs.read_only)
2581 * The filetype shouldn't be normal file, directory or
2584 if (filetype == EXT4_DE_UNKNOWN ||
2585 filetype == EXT4_DE_REG_FILE ||
2586 filetype == EXT4_DE_DIR ||
2587 filetype == EXT4_DE_SYMLINK)
2591 * Nor should it be any bogus value.
2593 if (filetype != EXT4_DE_CHRDEV &&
2594 filetype != EXT4_DE_BLKDEV &&
2595 filetype != EXT4_DE_FIFO &&
2596 filetype != EXT4_DE_SOCK)
2600 ext4_trans_start(mp);
2602 ext4_block_cache_write_back(mp->fs.bdev, 1);
2603 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2605 if (filetype == EXT4_DE_CHRDEV ||
2606 filetype == EXT4_DE_BLKDEV)
2607 r = ext4_mknod_set(&f, dev);
2614 ext4_block_cache_write_back(mp->fs.bdev, 0);
2617 ext4_trans_abort(mp);
2619 ext4_trans_stop(mp);
2625 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2626 const void *data, size_t data_size, bool replace __unused)
2633 const char *dissected_name = NULL;
2634 size_t dissected_len = 0;
2635 struct ext4_inode_ref inode_ref;
2636 struct ext4_mountpoint *mp = ext4_get_mount(path);
2640 if (mp->fs.read_only)
2643 dissected_name = ext4_extract_xattr_name(name, name_len,
2644 &name_index, &dissected_len,
2650 ext4_trans_start(mp);
2652 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2658 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2662 r = ext4_xattr_set(&inode_ref, name_index, dissected_name,
2663 dissected_len, data, data_size);
2665 ext4_fs_put_inode_ref(&inode_ref);
2668 ext4_trans_abort(mp);
2670 ext4_trans_stop(mp);
2676 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2677 void *buf, size_t buf_size, size_t *data_size)
2684 const char *dissected_name = NULL;
2685 size_t dissected_len = 0;
2686 struct ext4_inode_ref inode_ref;
2687 struct ext4_mountpoint *mp = ext4_get_mount(path);
2691 dissected_name = ext4_extract_xattr_name(name, name_len,
2692 &name_index, &dissected_len,
2698 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2704 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2708 r = ext4_xattr_get(&inode_ref, name_index, dissected_name,
2709 dissected_len, buf, buf_size, data_size);
2711 ext4_fs_put_inode_ref(&inode_ref);
2717 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2722 size_t list_len, list_size = 0;
2723 struct ext4_inode_ref inode_ref;
2724 struct ext4_xattr_list_entry *xattr_list = NULL,
2726 struct ext4_mountpoint *mp = ext4_get_mount(path);
2731 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2737 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2741 r = ext4_xattr_list(&inode_ref, NULL, &list_len);
2742 if (r == EOK && list_len) {
2743 xattr_list = ext4_malloc(list_len);
2745 ext4_fs_put_inode_ref(&inode_ref);
2750 r = ext4_xattr_list(&inode_ref, entry, &list_len);
2752 ext4_fs_put_inode_ref(&inode_ref);
2756 for (;entry;entry = entry->next) {
2758 const char *prefix =
2759 ext4_get_xattr_name_prefix(entry->name_index,
2762 if (prefix_len + entry->name_len + 1 > size) {
2763 ext4_fs_put_inode_ref(&inode_ref);
2770 memcpy(list, prefix, prefix_len);
2772 memcpy(list, entry->name,
2774 list[entry->name_len] = 0;
2775 list += entry->name_len + 1;
2777 size -= prefix_len + entry->name_len + 1;
2780 list_size += prefix_len + entry->name_len + 1;
2783 *ret_size = list_size;
2786 ext4_fs_put_inode_ref(&inode_ref);
2790 ext4_free(xattr_list);
2796 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2803 const char *dissected_name = NULL;
2804 size_t dissected_len = 0;
2805 struct ext4_inode_ref inode_ref;
2806 struct ext4_mountpoint *mp = ext4_get_mount(path);
2810 if (mp->fs.read_only)
2813 dissected_name = ext4_extract_xattr_name(name, name_len,
2814 &name_index, &dissected_len,
2820 ext4_trans_start(mp);
2822 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2828 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2832 r = ext4_xattr_remove(&inode_ref, name_index, dissected_name,
2835 ext4_fs_put_inode_ref(&inode_ref);
2838 ext4_trans_abort(mp);
2840 ext4_trans_stop(mp);
2847 /*********************************DIRECTORY OPERATION************************/
2849 int ext4_dir_rm(const char *path)
2855 struct ext4_mountpoint *mp = ext4_get_mount(path);
2856 struct ext4_inode_ref act;
2857 struct ext4_inode_ref child;
2858 struct ext4_dir_iter it;
2862 uint32_t inode_current;
2872 if (mp->fs.read_only)
2877 struct ext4_fs *const fs = &mp->fs;
2880 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2882 ext4_trans_abort(mp);
2888 len = ext4_path_check(path, &is_goal);
2890 inode_current = f.inode;
2892 ext4_block_cache_write_back(mp->fs.bdev, 1);
2896 uint64_t act_curr_pos = 0;
2897 has_children = false;
2900 while (r == EOK && !has_children && !dir_end) {
2902 /*Load directory node.*/
2903 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2908 /*Initialize iterator.*/
2909 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2911 ext4_fs_put_inode_ref(&act);
2920 ext4_trans_start(mp);
2922 /*Get up directory inode when ".." entry*/
2923 if ((it.curr->name_len == 2) &&
2924 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2925 inode_up = ext4_dir_en_get_inode(it.curr);
2928 /*If directory or file entry, but not "." ".." entry*/
2929 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2931 /*Get child inode reference do unlink
2934 uint32_t inode_type;
2935 cinode = ext4_dir_en_get_inode(it.curr);
2936 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2940 /*If directory with no leaf children*/
2941 r = ext4_has_children(&has_children, &child);
2943 ext4_fs_put_inode_ref(&child);
2948 /*Has directory children. Go into this
2950 inode_up = inode_current;
2951 inode_current = cinode;
2953 ext4_fs_put_inode_ref(&child);
2956 inode_type = ext4_inode_type(&mp->fs.sb,
2960 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2961 r = ext4_trunc_inode(mp, child.index, 0);
2963 r = ext4_trunc_dir(mp, &act, &child);
2966 ext4_fs_put_inode_ref(&child);
2970 /*No children in child directory or file. Just
2972 r = ext4_unlink(f.mp, &act, &child,
2973 (char *)it.curr->name,
2976 ext4_fs_put_inode_ref(&child);
2980 ext4_inode_set_del_time(child.inode, -1L);
2981 ext4_inode_set_links_cnt(child.inode, 0);
2984 r = ext4_fs_free_inode(&child);
2986 ext4_fs_put_inode_ref(&child);
2990 r = ext4_fs_put_inode_ref(&child);
2996 r = ext4_dir_iterator_next(&it);
3000 act_curr_pos = it.curr_off;
3002 ext4_dir_iterator_fini(&it);
3004 r = ext4_fs_put_inode_ref(&act);
3006 ext4_fs_put_inode_ref(&act);
3009 ext4_trans_abort(mp);
3011 ext4_trans_stop(mp);
3015 /*Directory iterator reached last entry*/
3018 inode_current = inode_up;
3028 if (r == EOK && !depth) {
3030 struct ext4_inode_ref parent;
3031 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
3035 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
3038 ext4_fs_put_inode_ref(&act);
3042 ext4_trans_start(mp);
3044 /* In this place all directories should be
3046 * Last unlink from root of current directory*/
3047 r = ext4_unlink(f.mp, &parent, &act,
3050 ext4_fs_put_inode_ref(&parent);
3051 ext4_fs_put_inode_ref(&act);
3055 if (ext4_inode_get_links_cnt(act.inode) == 2) {
3056 ext4_inode_set_del_time(act.inode, -1L);
3057 ext4_inode_set_links_cnt(act.inode, 0);
3060 r = ext4_trunc_dir(mp, &parent, &act);
3062 ext4_fs_put_inode_ref(&parent);
3063 ext4_fs_put_inode_ref(&act);
3067 r = ext4_fs_free_inode(&act);
3069 ext4_fs_put_inode_ref(&parent);
3070 ext4_fs_put_inode_ref(&act);
3075 r = ext4_fs_put_inode_ref(&parent);
3079 r = ext4_fs_put_inode_ref(&act);
3082 ext4_trans_abort(mp);
3084 ext4_trans_stop(mp);
3087 ext4_block_cache_write_back(mp->fs.bdev, 0);
3093 int ext4_dir_mv(const char *path, const char *new_path)
3095 return ext4_frename(path, new_path);
3098 int ext4_dir_mk(const char *path)
3103 struct ext4_mountpoint *mp = ext4_get_mount(path);
3108 if (mp->fs.read_only)
3112 ext4_trans_start(mp);
3115 r = ext4_generic_open(&f, path, "r", false, 0, 0);
3117 /*Directory already created*/
3118 ext4_trans_stop(mp);
3124 r = ext4_generic_open(&f, path, "w", false, 0, 0);
3126 ext4_trans_abort(mp);
3131 ext4_trans_stop(mp);
3136 int ext4_dir_open(ext4_dir *d, const char *path)
3138 struct ext4_mountpoint *mp = ext4_get_mount(path);
3145 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
3151 int ext4_dir_close(ext4_dir *d)
3153 return ext4_fclose(&d->f);
3156 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
3158 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
3161 uint16_t name_length;
3162 ext4_direntry *de = 0;
3163 struct ext4_inode_ref dir;
3164 struct ext4_dir_iter it;
3166 EXT4_MP_LOCK(d->f.mp);
3168 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
3169 EXT4_MP_UNLOCK(d->f.mp);
3173 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
3178 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
3180 ext4_fs_put_inode_ref(&dir);
3184 memset(&d->de.name, 0, sizeof(d->de.name));
3185 name_length = ext4_dir_en_get_name_len(&d->f.mp->fs.sb,
3187 memcpy(&d->de.name, it.curr->name, name_length);
3189 /* Directly copying the content isn't safe for Big-endian targets*/
3190 d->de.inode = ext4_dir_en_get_inode(it.curr);
3191 d->de.entry_length = ext4_dir_en_get_entry_len(it.curr);
3192 d->de.name_length = name_length;
3193 d->de.inode_type = ext4_dir_en_get_inode_type(&d->f.mp->fs.sb,
3198 ext4_dir_iterator_next(&it);
3200 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
3202 ext4_dir_iterator_fini(&it);
3203 ext4_fs_put_inode_ref(&dir);
3206 EXT4_MP_UNLOCK(d->f.mp);
3210 void ext4_dir_entry_rewind(ext4_dir *d)