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)
478 for (size_t i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
480 if (!s_mp[i].mounted)
483 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);
499 if (mp->fs.read_only)
502 if (ext4_sb_feature_com(&mp->fs.sb,
503 EXT4_FCOM_HAS_JOURNAL)) {
504 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
508 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
510 mp->jbd_fs.dirty = false;
511 jbd_put_fs(&mp->jbd_fs);
514 mp->fs.jbd_fs = &mp->jbd_fs;
515 mp->fs.jbd_journal = &mp->jbd_journal;
522 static int __ext4_journal_stop(const char *mount_point)
525 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
530 if (mp->fs.read_only)
533 if (ext4_sb_feature_com(&mp->fs.sb,
534 EXT4_FCOM_HAS_JOURNAL)) {
535 r = jbd_journal_stop(&mp->jbd_journal);
537 mp->jbd_fs.dirty = false;
538 jbd_put_fs(&mp->jbd_fs);
539 mp->fs.jbd_journal = NULL;
540 mp->fs.jbd_fs = NULL;
544 r = jbd_put_fs(&mp->jbd_fs);
546 mp->fs.jbd_journal = NULL;
547 mp->fs.jbd_fs = NULL;
551 mp->fs.jbd_journal = NULL;
552 mp->fs.jbd_fs = NULL;
559 static int __ext4_recover(const char *mount_point)
561 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
568 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
569 struct jbd_fs *jbd_fs = ext4_calloc(1, sizeof(struct jbd_fs));
575 r = jbd_get_fs(&mp->fs, jbd_fs);
581 r = jbd_recover(jbd_fs);
585 if (r == EOK && !mp->fs.read_only) {
587 uint64_t free_blocks_count = 0;
588 uint32_t free_inodes_count = 0;
589 struct ext4_block_group_ref bg_ref;
591 /* Update superblock's stats */
592 for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) {
593 r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref);
598 ext4_bg_get_free_blocks_count(bg_ref.block_group,
601 ext4_bg_get_free_inodes_count(bg_ref.block_group,
604 ext4_fs_put_block_group_ref(&bg_ref);
606 ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count);
607 ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count);
608 /* We don't need to save the superblock stats immediately. */
617 static int __ext4_trans_start(struct ext4_mountpoint *mp)
621 if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
622 struct jbd_journal *journal = mp->fs.jbd_journal;
623 struct jbd_trans *trans;
624 trans = jbd_journal_new_trans(journal);
629 mp->fs.curr_trans = trans;
636 static int __ext4_trans_stop(struct ext4_mountpoint *mp)
640 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
641 struct jbd_journal *journal = mp->fs.jbd_journal;
642 struct jbd_trans *trans = mp->fs.curr_trans;
643 r = jbd_journal_commit_trans(journal, trans);
644 mp->fs.curr_trans = NULL;
650 static void __ext4_trans_abort(struct ext4_mountpoint *mp)
652 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
653 struct jbd_journal *journal = mp->fs.jbd_journal;
654 struct jbd_trans *trans = mp->fs.curr_trans;
655 jbd_journal_free_trans(journal, trans, true);
656 mp->fs.curr_trans = NULL;
660 int ext4_journal_start(const char *mount_point __unused)
663 #if CONFIG_JOURNALING_ENABLE
664 r = __ext4_journal_start(mount_point);
669 int ext4_journal_stop(const char *mount_point __unused)
672 #if CONFIG_JOURNALING_ENABLE
673 r = __ext4_journal_stop(mount_point);
678 int ext4_recover(const char *mount_point __unused)
681 #if CONFIG_JOURNALING_ENABLE
682 r = __ext4_recover(mount_point);
687 static int ext4_trans_start(struct ext4_mountpoint *mp __unused)
690 #if CONFIG_JOURNALING_ENABLE
691 r = __ext4_trans_start(mp);
696 static int ext4_trans_stop(struct ext4_mountpoint *mp __unused)
699 #if CONFIG_JOURNALING_ENABLE
700 r = __ext4_trans_stop(mp);
705 static void ext4_trans_abort(struct ext4_mountpoint *mp __unused)
707 #if CONFIG_JOURNALING_ENABLE
708 __ext4_trans_abort(mp);
713 int ext4_mount_point_stats(const char *mount_point,
714 struct ext4_mount_stats *stats)
716 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
722 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
723 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
724 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
725 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
726 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
728 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
729 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
730 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
732 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
738 int ext4_mount_setup_locks(const char *mount_point,
739 const struct ext4_lock *locks)
742 struct ext4_mountpoint *mp = 0;
744 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
745 if (!strcmp(s_mp[i].name, mount_point)) {
753 mp->os_locks = locks;
757 /********************************FILE OPERATIONS*****************************/
759 static int ext4_path_check(const char *path, bool *is_goal)
763 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
765 if (path[i] == '/') {
779 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
784 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
785 *file_flags = O_RDONLY;
789 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
790 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
794 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
795 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
799 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
800 !strcmp(flags, "r+b")) {
801 *file_flags = O_RDWR;
805 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
806 !strcmp(flags, "w+b")) {
807 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
811 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
812 !strcmp(flags, "a+b")) {
813 *file_flags = O_RDWR | O_CREAT | O_APPEND;
820 static int ext4_trunc_inode(struct ext4_mountpoint *mp,
821 uint32_t index, uint64_t new_size)
824 struct ext4_fs *const fs = &mp->fs;
825 struct ext4_inode_ref inode_ref;
827 bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
828 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
832 inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
833 ext4_fs_put_inode_ref(&inode_ref);
837 while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
839 inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
841 ext4_trans_start(mp);
842 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
844 ext4_trans_abort(mp);
847 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
849 ext4_fs_put_inode_ref(&inode_ref);
851 r = ext4_fs_put_inode_ref(&inode_ref);
854 ext4_trans_abort(mp);
860 if (inode_size > new_size) {
862 inode_size = new_size;
864 ext4_trans_start(mp);
865 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
867 ext4_trans_abort(mp);
870 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
872 ext4_fs_put_inode_ref(&inode_ref);
874 r = ext4_fs_put_inode_ref(&inode_ref);
877 ext4_trans_abort(mp);
886 ext4_trans_start(mp);
891 static int ext4_trunc_dir(struct ext4_mountpoint *mp,
892 struct ext4_inode_ref *parent,
893 struct ext4_inode_ref *dir)
896 bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
897 EXT4_INODE_MODE_DIRECTORY);
898 uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
902 #if CONFIG_DIR_INDEX_ENABLE
903 /* Initialize directory index if supported */
904 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
905 r = ext4_dir_dx_init(dir, parent);
909 r = ext4_trunc_inode(mp, dir->index,
910 EXT4_DIR_DX_INIT_BCNT * block_size);
916 r = ext4_trunc_inode(mp, dir->index, block_size);
921 return ext4_fs_truncate_inode(dir, 0);
925 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
926 * any filetype of the target dir entry will be accepted.
928 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
929 int ftype, uint32_t *parent_inode,
932 bool is_goal = false;
933 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
938 struct ext4_mountpoint *mp = ext4_get_mount(path);
939 struct ext4_dir_search_result result;
940 struct ext4_inode_ref ref;
947 struct ext4_fs *const fs = &mp->fs;
948 struct ext4_sblock *const sb = &mp->fs.sb;
950 if (fs->read_only && flags & O_CREAT)
956 path += strlen(mp->name);
959 *name_off = strlen(mp->name);
962 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
967 *parent_inode = ref.index;
970 ext4_trans_start(mp);
972 len = ext4_path_check(path, &is_goal);
975 len = ext4_path_check(path, &is_goal);
977 /*If root open was request.*/
978 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
986 r = ext4_dir_find_entry(&result, &ref, path, len);
989 /*Destroy last result*/
990 ext4_dir_destroy_result(&ref, &result);
994 if (!(f->flags & O_CREAT))
997 /*O_CREAT allows create new entry*/
998 struct ext4_inode_ref child_ref;
999 r = ext4_fs_alloc_inode(fs, &child_ref,
1000 is_goal ? ftype : EXT4_DE_DIR);
1005 /*Link with root dir.*/
1006 r = ext4_link(mp, &ref, &child_ref, path, len, false);
1008 /*Fail. Free new inode.*/
1009 ext4_fs_free_inode(&child_ref);
1010 /*We do not want to write new inode.
1011 But block has to be released.*/
1012 child_ref.dirty = false;
1013 ext4_fs_put_inode_ref(&child_ref);
1017 ext4_fs_put_inode_ref(&child_ref);
1022 *parent_inode = ref.index;
1024 next_inode = ext4_dir_en_get_inode(result.dentry);
1025 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1027 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1028 imode = ext4_fs_correspond_inode_mode(t);
1030 struct ext4_inode_ref child_ref;
1031 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1035 imode = ext4_inode_type(sb, child_ref.inode);
1036 ext4_fs_put_inode_ref(&child_ref);
1039 r = ext4_dir_destroy_result(&ref, &result);
1043 /*If expected file error*/
1044 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
1048 if (ftype != EXT4_DE_UNKNOWN) {
1049 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
1050 if (df && is_goal) {
1056 r = ext4_fs_put_inode_ref(&ref);
1060 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1070 *name_off += len + 1;
1074 ext4_fs_put_inode_ref(&ref);
1080 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
1081 r = ext4_trunc_inode(mp, ref.index, 0);
1083 ext4_fs_put_inode_ref(&ref);
1089 f->fsize = ext4_inode_get_size(sb, ref.inode);
1090 f->inode = ref.index;
1093 if (f->flags & O_APPEND)
1098 r = ext4_fs_put_inode_ref(&ref);
1099 if (flags & O_CREAT) {
1101 ext4_trans_stop(mp);
1103 ext4_trans_abort(mp);
1110 /****************************************************************************/
1112 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
1113 bool file_expect, uint32_t *parent_inode,
1118 if (ext4_parse_flags(flags, &iflags) == false)
1121 if (file_expect == true)
1122 filetype = EXT4_DE_REG_FILE;
1124 filetype = EXT4_DE_DIR;
1126 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
1130 static int ext4_create_hardlink(const char *path,
1131 struct ext4_inode_ref *child_ref, bool rename)
1133 bool is_goal = false;
1134 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
1135 uint32_t next_inode;
1139 struct ext4_mountpoint *mp = ext4_get_mount(path);
1140 struct ext4_dir_search_result result;
1141 struct ext4_inode_ref ref;
1146 struct ext4_fs *const fs = &mp->fs;
1147 struct ext4_sblock *const sb = &mp->fs.sb;
1149 /*Skip mount point*/
1150 path += strlen(mp->name);
1153 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
1157 len = ext4_path_check(path, &is_goal);
1160 len = ext4_path_check(path, &is_goal);
1162 /*If root open was request.*/
1163 r = is_goal ? EINVAL : ENOENT;
1167 r = ext4_dir_find_entry(&result, &ref, path, len);
1170 /*Destroy last result*/
1171 ext4_dir_destroy_result(&ref, &result);
1173 if (r != ENOENT || !is_goal)
1176 /*Link with root dir.*/
1177 r = ext4_link(mp, &ref, child_ref, path, len, rename);
1179 } else if (r == EOK && is_goal) {
1180 /*Destroy last result*/
1181 ext4_dir_destroy_result(&ref, &result);
1186 next_inode = result.dentry->inode;
1187 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1189 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1190 inode_mode = ext4_fs_correspond_inode_mode(t);
1192 struct ext4_inode_ref child_ref;
1193 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1197 inode_mode = ext4_inode_type(sb, child_ref.inode);
1198 ext4_fs_put_inode_ref(&child_ref);
1201 r = ext4_dir_destroy_result(&ref, &result);
1205 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1206 r = is_goal ? EEXIST : ENOENT;
1210 r = ext4_fs_put_inode_ref(&ref);
1214 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1225 ext4_fs_put_inode_ref(&ref);
1229 r = ext4_fs_put_inode_ref(&ref);
1233 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1234 struct ext4_inode_ref *parent_ref,
1235 struct ext4_inode_ref *child_ref)
1240 struct ext4_mountpoint *mp = ext4_get_mount(path);
1248 len = ext4_path_check(path, &is_goal);
1250 /* Remove entry from parent directory */
1251 r = ext4_dir_remove_entry(parent_ref, path, len);
1255 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1256 EXT4_INODE_MODE_DIRECTORY)) {
1257 ext4_fs_inode_links_count_dec(parent_ref);
1258 parent_ref->dirty = true;
1264 int ext4_flink(const char *path, const char *hardlink_path)
1269 bool child_loaded = false;
1270 uint32_t parent_inode, child_inode;
1271 struct ext4_mountpoint *mp = ext4_get_mount(path);
1272 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1273 struct ext4_inode_ref child_ref;
1278 if (mp->fs.read_only)
1281 /* Will that happen? Anyway return EINVAL for such case. */
1282 if (mp != target_mp)
1286 ext4_trans_start(mp);
1288 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1289 &parent_inode, &name_off);
1293 child_inode = f.inode;
1296 /*We have file to unlink. Load it.*/
1297 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1301 child_loaded = true;
1303 /* Creating hardlink for directory is not allowed. */
1304 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1305 EXT4_INODE_MODE_DIRECTORY)) {
1310 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1314 ext4_fs_put_inode_ref(&child_ref);
1317 ext4_trans_abort(mp);
1319 ext4_trans_stop(mp);
1326 int ext4_frename(const char *path, const char *new_path)
1331 bool parent_loaded = false, child_loaded = false;
1332 uint32_t parent_inode, child_inode;
1333 struct ext4_mountpoint *mp = ext4_get_mount(path);
1334 struct ext4_inode_ref child_ref, parent_ref;
1339 if (mp->fs.read_only)
1343 ext4_trans_start(mp);
1345 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1346 &parent_inode, &name_off);
1350 child_inode = f.inode;
1354 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1358 parent_loaded = true;
1360 /*We have file to unlink. Load it.*/
1361 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1365 child_loaded = true;
1367 r = ext4_create_hardlink(new_path, &child_ref, true);
1371 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1377 ext4_fs_put_inode_ref(&parent_ref);
1380 ext4_fs_put_inode_ref(&child_ref);
1383 ext4_trans_abort(mp);
1385 ext4_trans_stop(mp);
1392 /****************************************************************************/
1394 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1396 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1405 int ext4_cache_write_back(const char *path, bool on)
1407 struct ext4_mountpoint *mp = ext4_get_mount(path);
1414 ret = ext4_block_cache_write_back(mp->fs.bdev, on);
1419 int ext4_cache_flush(const char *path)
1421 struct ext4_mountpoint *mp = ext4_get_mount(path);
1428 ret = ext4_block_cache_flush(mp->fs.bdev);
1433 int ext4_fremove(const char *path)
1436 uint32_t parent_inode;
1441 struct ext4_inode_ref child;
1442 struct ext4_inode_ref parent;
1443 struct ext4_mountpoint *mp = ext4_get_mount(path);
1448 if (mp->fs.read_only)
1452 ext4_trans_start(mp);
1454 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1455 &parent_inode, &name_off);
1457 ext4_trans_abort(mp);
1463 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1465 ext4_trans_abort(mp);
1470 /*We have file to delete. Load it.*/
1471 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1473 ext4_fs_put_inode_ref(&parent);
1474 ext4_trans_abort(mp);
1478 /* We do not allow opening files here. */
1479 if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1480 EXT4_INODE_MODE_DIRECTORY) {
1481 ext4_fs_put_inode_ref(&parent);
1482 ext4_fs_put_inode_ref(&child);
1483 ext4_trans_abort(mp);
1488 /*Link count will be zero, the inode should be freed. */
1489 if (ext4_inode_get_links_cnt(child.inode) == 1) {
1490 ext4_block_cache_write_back(mp->fs.bdev, 1);
1491 r = ext4_trunc_inode(mp, child.index, 0);
1493 ext4_fs_put_inode_ref(&parent);
1494 ext4_fs_put_inode_ref(&child);
1495 ext4_trans_abort(mp);
1499 ext4_block_cache_write_back(mp->fs.bdev, 0);
1505 len = ext4_path_check(path, &is_goal);
1507 /*Unlink from parent*/
1508 r = ext4_unlink(mp, &parent, &child, path, len);
1512 /*Link count is zero, the inode should be freed. */
1513 if (!ext4_inode_get_links_cnt(child.inode)) {
1514 ext4_inode_set_del_time(child.inode, -1L);
1516 r = ext4_fs_free_inode(&child);
1522 ext4_fs_put_inode_ref(&child);
1523 ext4_fs_put_inode_ref(&parent);
1526 ext4_trans_abort(mp);
1528 ext4_trans_stop(mp);
1534 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1535 struct ext4_inode *inode)
1539 struct ext4_inode_ref inode_ref;
1540 struct ext4_mountpoint *mp = ext4_get_mount(path);
1548 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1558 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1564 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1565 ext4_fs_put_inode_ref(&inode_ref);
1574 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1576 struct ext4_mountpoint *mp = ext4_get_mount(path);
1584 ext4_block_cache_write_back(mp->fs.bdev, 1);
1585 r = ext4_generic_open(f, path, flags, true, 0, 0);
1586 ext4_block_cache_write_back(mp->fs.bdev, 0);
1592 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1594 struct ext4_mountpoint *mp = ext4_get_mount(path);
1601 filetype = EXT4_DE_REG_FILE;
1605 ext4_block_cache_write_back(mp->fs.bdev, 1);
1606 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1607 ext4_block_cache_write_back(mp->fs.bdev, 0);
1613 int ext4_fclose(ext4_file *f)
1615 ext4_assert(f && f->mp);
1620 f->fpos = f->fsize = 0;
1625 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1627 struct ext4_inode_ref ref;
1631 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1633 EXT4_MP_UNLOCK(f->mp);
1638 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1639 if (f->fsize <= size) {
1644 /*Start write back cache mode.*/
1645 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1649 r = ext4_trunc_inode(f->mp, ref.index, size);
1657 /*Stop write back cache mode*/
1658 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1664 ext4_fs_put_inode_ref(&ref);
1669 int ext4_ftruncate(ext4_file *f, uint64_t size)
1672 ext4_assert(f && f->mp);
1674 if (f->mp->fs.read_only)
1677 if (f->flags & O_RDONLY)
1680 EXT4_MP_LOCK(f->mp);
1682 ext4_trans_start(f->mp);
1683 r = ext4_ftruncate_no_lock(f, size);
1685 ext4_trans_abort(f->mp);
1687 ext4_trans_stop(f->mp);
1689 EXT4_MP_UNLOCK(f->mp);
1693 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1696 uint32_t iblock_idx;
1697 uint32_t iblock_last;
1698 uint32_t block_size;
1700 ext4_fsblk_t fblock;
1701 ext4_fsblk_t fblock_start;
1702 uint32_t fblock_count;
1704 uint8_t *u8_buf = buf;
1706 struct ext4_inode_ref ref;
1708 ext4_assert(f && f->mp);
1710 if (f->flags & O_WRONLY)
1716 EXT4_MP_LOCK(f->mp);
1718 struct ext4_fs *const fs = &f->mp->fs;
1719 struct ext4_sblock *const sb = &f->mp->fs.sb;
1724 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1726 EXT4_MP_UNLOCK(f->mp);
1731 f->fsize = ext4_inode_get_size(sb, ref.inode);
1733 block_size = ext4_sb_get_block_size(sb);
1734 size = ((uint64_t)size > (f->fsize - f->fpos))
1735 ? ((size_t)(f->fsize - f->fpos)) : size;
1737 iblock_idx = (uint32_t)((f->fpos) / block_size);
1738 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1739 unalg = (f->fpos) % block_size;
1741 /*If the size of symlink is smaller than 60 bytes*/
1743 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1744 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1745 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1747 char *content = (char *)ref.inode->blocks;
1748 if (f->fpos < f->fsize) {
1750 if (unalg + size > (uint32_t)f->fsize)
1751 len = (uint32_t)f->fsize - unalg;
1752 memcpy(buf, content + unalg, len);
1764 if (size > (block_size - unalg))
1765 len = block_size - unalg;
1767 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1771 /* Do we get an unwritten range? */
1773 uint64_t off = fblock * block_size + unalg;
1774 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1780 memset(u8_buf, 0, len);
1795 while (size >= block_size) {
1796 while (iblock_idx < iblock_last) {
1797 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1805 fblock_start = fblock;
1807 if ((fblock_start + fblock_count) != fblock)
1813 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1818 size -= block_size * fblock_count;
1819 u8_buf += block_size * fblock_count;
1820 f->fpos += block_size * fblock_count;
1823 *rcnt += block_size * fblock_count;
1825 fblock_start = fblock;
1831 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1835 off = fblock * block_size;
1836 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1847 ext4_fs_put_inode_ref(&ref);
1848 EXT4_MP_UNLOCK(f->mp);
1852 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1856 uint32_t iblock_last;
1857 uint32_t ifile_blocks;
1858 uint32_t block_size;
1860 uint32_t fblock_count;
1862 ext4_fsblk_t fblock_start;
1864 struct ext4_inode_ref ref;
1865 const uint8_t *u8_buf = buf;
1868 ext4_assert(f && f->mp);
1870 if (f->mp->fs.read_only)
1873 if (f->flags & O_RDONLY)
1879 EXT4_MP_LOCK(f->mp);
1880 ext4_trans_start(f->mp);
1882 struct ext4_fs *const fs = &f->mp->fs;
1883 struct ext4_sblock *const sb = &f->mp->fs.sb;
1888 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1890 ext4_trans_abort(f->mp);
1891 EXT4_MP_UNLOCK(f->mp);
1896 f->fsize = ext4_inode_get_size(sb, ref.inode);
1897 block_size = ext4_sb_get_block_size(sb);
1899 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1900 iblk_idx = (uint32_t)(f->fpos / block_size);
1901 ifile_blocks = (uint32_t)((f->fsize + block_size - 1) / block_size);
1903 unalg = (f->fpos) % block_size;
1908 if (size > (block_size - unalg))
1909 len = block_size - unalg;
1911 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1915 off = fblk * block_size + unalg;
1916 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1930 /*Start write back cache mode.*/
1931 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1937 while (size >= block_size) {
1939 while (iblk_idx < iblock_last) {
1940 if (iblk_idx < ifile_blocks) {
1941 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1946 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1949 /* Unable to append more blocks. But
1950 * some block might be allocated already
1958 if (!fblock_start) {
1959 fblock_start = fblk;
1962 if ((fblock_start + fblock_count) != fblk)
1968 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1973 size -= block_size * fblock_count;
1974 u8_buf += block_size * fblock_count;
1975 f->fpos += block_size * fblock_count;
1978 *wcnt += block_size * fblock_count;
1980 fblock_start = fblk;
1984 /*ext4_fs_append_inode_block has failed and no
1985 * more blocks might be written. But node size
1986 * should be updated.*/
1992 /*Stop write back cache mode*/
1993 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2000 if (iblk_idx < ifile_blocks) {
2001 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
2005 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
2007 /*Node size sholud be updated.*/
2011 off = fblk * block_size;
2012 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
2023 if (f->fpos > f->fsize) {
2025 ext4_inode_set_size(ref.inode, f->fsize);
2030 r = ext4_fs_put_inode_ref(&ref);
2033 ext4_trans_abort(f->mp);
2035 ext4_trans_stop(f->mp);
2037 EXT4_MP_UNLOCK(f->mp);
2041 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
2045 if (offset > f->fsize)
2051 if ((offset + f->fpos) > f->fsize)
2057 if (offset > f->fsize)
2060 f->fpos = f->fsize - offset;
2066 uint64_t ext4_ftell(ext4_file *f)
2071 uint64_t ext4_fsize(ext4_file *f)
2077 static int ext4_trans_get_inode_ref(const char *path,
2078 struct ext4_mountpoint *mp,
2079 struct ext4_inode_ref *inode_ref)
2084 ext4_trans_start(mp);
2085 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2087 ext4_trans_abort(mp);
2091 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, inode_ref);
2093 ext4_trans_abort(mp);
2100 static int ext4_trans_put_inode_ref(struct ext4_mountpoint *mp,
2101 struct ext4_inode_ref *inode_ref)
2105 r = ext4_fs_put_inode_ref(inode_ref);
2107 ext4_trans_abort(mp);
2109 ext4_trans_stop(mp);
2114 int ext4_mode_set(const char *path, uint32_t mode)
2118 struct ext4_inode_ref inode_ref;
2119 struct ext4_mountpoint *mp = ext4_get_mount(path);
2124 if (mp->fs.read_only)
2129 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2133 orig_mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2134 orig_mode &= ~0xFFF;
2135 orig_mode |= mode & 0xFFF;
2136 ext4_inode_set_mode(&mp->fs.sb, inode_ref.inode, orig_mode);
2138 inode_ref.dirty = true;
2139 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2147 int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid)
2150 struct ext4_inode_ref inode_ref;
2151 struct ext4_mountpoint *mp = ext4_get_mount(path);
2156 if (mp->fs.read_only)
2161 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2165 ext4_inode_set_uid(inode_ref.inode, uid);
2166 ext4_inode_set_gid(inode_ref.inode, gid);
2168 inode_ref.dirty = true;
2169 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2177 int ext4_mode_get(const char *path, uint32_t *mode)
2179 struct ext4_inode_ref inode_ref;
2180 struct ext4_mountpoint *mp = ext4_get_mount(path);
2189 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2193 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2197 *mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2198 r = ext4_fs_put_inode_ref(&inode_ref);
2206 int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid)
2208 struct ext4_inode_ref inode_ref;
2209 struct ext4_mountpoint *mp = ext4_get_mount(path);
2218 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2222 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2226 *uid = ext4_inode_get_uid(inode_ref.inode);
2227 *gid = ext4_inode_get_gid(inode_ref.inode);
2228 r = ext4_fs_put_inode_ref(&inode_ref);
2236 int ext4_atime_set(const char *path, uint32_t atime)
2238 struct ext4_inode_ref inode_ref;
2239 struct ext4_mountpoint *mp = ext4_get_mount(path);
2245 if (mp->fs.read_only)
2250 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2254 ext4_inode_set_access_time(inode_ref.inode, atime);
2255 inode_ref.dirty = true;
2256 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2264 int ext4_mtime_set(const char *path, uint32_t mtime)
2266 struct ext4_inode_ref inode_ref;
2267 struct ext4_mountpoint *mp = ext4_get_mount(path);
2273 if (mp->fs.read_only)
2278 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2282 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2283 inode_ref.dirty = true;
2284 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2292 int ext4_ctime_set(const char *path, uint32_t ctime)
2294 struct ext4_inode_ref inode_ref;
2295 struct ext4_mountpoint *mp = ext4_get_mount(path);
2301 if (mp->fs.read_only)
2306 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2310 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2311 inode_ref.dirty = true;
2312 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2320 int ext4_atime_get(const char *path, uint32_t *atime)
2322 struct ext4_inode_ref inode_ref;
2323 struct ext4_mountpoint *mp = ext4_get_mount(path);
2332 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2336 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2340 *atime = ext4_inode_get_access_time(inode_ref.inode);
2341 r = ext4_fs_put_inode_ref(&inode_ref);
2349 int ext4_mtime_get(const char *path, uint32_t *mtime)
2351 struct ext4_inode_ref inode_ref;
2352 struct ext4_mountpoint *mp = ext4_get_mount(path);
2361 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2365 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2369 *mtime = ext4_inode_get_modif_time(inode_ref.inode);
2370 r = ext4_fs_put_inode_ref(&inode_ref);
2378 int ext4_ctime_get(const char *path, uint32_t *ctime)
2380 struct ext4_inode_ref inode_ref;
2381 struct ext4_mountpoint *mp = ext4_get_mount(path);
2390 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2394 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2398 *ctime = ext4_inode_get_change_inode_time(inode_ref.inode);
2399 r = ext4_fs_put_inode_ref(&inode_ref);
2407 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2409 struct ext4_inode_ref ref;
2411 ext4_fsblk_t fblock;
2412 uint32_t block_size;
2415 ext4_assert(f && f->mp);
2420 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2425 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2426 if (size > block_size) {
2430 r = ext4_ftruncate_no_lock(f, 0);
2434 /*Start write back cache mode.*/
2435 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2439 /*If the size of symlink is smaller than 60 bytes*/
2440 if (size < sizeof(ref.inode->blocks)) {
2441 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2442 memcpy(ref.inode->blocks, buf, size);
2443 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2445 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2446 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2450 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2456 /*Stop write back cache mode*/
2457 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2462 ext4_inode_set_size(ref.inode, size);
2470 ext4_fs_put_inode_ref(&ref);
2474 int ext4_fsymlink(const char *target, const char *path)
2476 struct ext4_mountpoint *mp = ext4_get_mount(path);
2484 if (mp->fs.read_only)
2487 filetype = EXT4_DE_SYMLINK;
2490 ext4_trans_start(mp);
2492 ext4_block_cache_write_back(mp->fs.bdev, 1);
2493 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2495 r = ext4_fsymlink_set(&f, target, strlen(target));
2502 ext4_block_cache_write_back(mp->fs.bdev, 0);
2505 ext4_trans_abort(mp);
2507 ext4_trans_stop(mp);
2513 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2515 struct ext4_mountpoint *mp = ext4_get_mount(path);
2526 filetype = EXT4_DE_SYMLINK;
2529 ext4_block_cache_write_back(mp->fs.bdev, 1);
2530 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2532 r = ext4_fread(&f, buf, bufsize, rcnt);
2539 ext4_block_cache_write_back(mp->fs.bdev, 0);
2541 ext4_trans_abort(mp);
2543 ext4_trans_stop(mp);
2549 static int ext4_mknod_set(ext4_file *f, uint32_t dev)
2551 struct ext4_inode_ref ref;
2554 ext4_assert(f && f->mp);
2556 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2560 ext4_inode_set_dev(ref.inode, dev);
2562 ext4_inode_set_size(ref.inode, 0);
2568 r = ext4_fs_put_inode_ref(&ref);
2572 int ext4_mknod(const char *path, int filetype, uint32_t dev)
2574 struct ext4_mountpoint *mp = ext4_get_mount(path);
2581 if (mp->fs.read_only)
2585 * The filetype shouldn't be normal file, directory or
2588 if (filetype == EXT4_DE_UNKNOWN ||
2589 filetype == EXT4_DE_REG_FILE ||
2590 filetype == EXT4_DE_DIR ||
2591 filetype == EXT4_DE_SYMLINK)
2595 * Nor should it be any bogus value.
2597 if (filetype != EXT4_DE_CHRDEV &&
2598 filetype != EXT4_DE_BLKDEV &&
2599 filetype != EXT4_DE_FIFO &&
2600 filetype != EXT4_DE_SOCK)
2604 ext4_trans_start(mp);
2606 ext4_block_cache_write_back(mp->fs.bdev, 1);
2607 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2609 if (filetype == EXT4_DE_CHRDEV ||
2610 filetype == EXT4_DE_BLKDEV)
2611 r = ext4_mknod_set(&f, dev);
2618 ext4_block_cache_write_back(mp->fs.bdev, 0);
2621 ext4_trans_abort(mp);
2623 ext4_trans_stop(mp);
2629 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2630 const void *data, size_t data_size, bool replace __unused)
2637 const char *dissected_name = NULL;
2638 size_t dissected_len = 0;
2639 struct ext4_inode_ref inode_ref;
2640 struct ext4_mountpoint *mp = ext4_get_mount(path);
2644 if (mp->fs.read_only)
2647 dissected_name = ext4_extract_xattr_name(name, name_len,
2648 &name_index, &dissected_len,
2654 ext4_trans_start(mp);
2656 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2662 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2666 r = ext4_xattr_set(&inode_ref, name_index, dissected_name,
2667 dissected_len, data, data_size);
2669 ext4_fs_put_inode_ref(&inode_ref);
2672 ext4_trans_abort(mp);
2674 ext4_trans_stop(mp);
2680 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2681 void *buf, size_t buf_size, size_t *data_size)
2688 const char *dissected_name = NULL;
2689 size_t dissected_len = 0;
2690 struct ext4_inode_ref inode_ref;
2691 struct ext4_mountpoint *mp = ext4_get_mount(path);
2695 dissected_name = ext4_extract_xattr_name(name, name_len,
2696 &name_index, &dissected_len,
2702 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2708 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2712 r = ext4_xattr_get(&inode_ref, name_index, dissected_name,
2713 dissected_len, buf, buf_size, data_size);
2715 ext4_fs_put_inode_ref(&inode_ref);
2721 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2726 size_t list_len, list_size = 0;
2727 struct ext4_inode_ref inode_ref;
2728 struct ext4_xattr_list_entry *xattr_list = NULL,
2730 struct ext4_mountpoint *mp = ext4_get_mount(path);
2735 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2741 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2745 r = ext4_xattr_list(&inode_ref, NULL, &list_len);
2746 if (r == EOK && list_len) {
2747 xattr_list = ext4_malloc(list_len);
2749 ext4_fs_put_inode_ref(&inode_ref);
2754 r = ext4_xattr_list(&inode_ref, entry, &list_len);
2756 ext4_fs_put_inode_ref(&inode_ref);
2760 for (;entry;entry = entry->next) {
2762 const char *prefix =
2763 ext4_get_xattr_name_prefix(entry->name_index,
2766 if (prefix_len + entry->name_len + 1 > size) {
2767 ext4_fs_put_inode_ref(&inode_ref);
2774 memcpy(list, prefix, prefix_len);
2776 memcpy(list, entry->name,
2778 list[entry->name_len] = 0;
2779 list += entry->name_len + 1;
2781 size -= prefix_len + entry->name_len + 1;
2784 list_size += prefix_len + entry->name_len + 1;
2787 *ret_size = list_size;
2790 ext4_fs_put_inode_ref(&inode_ref);
2794 ext4_free(xattr_list);
2800 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2807 const char *dissected_name = NULL;
2808 size_t dissected_len = 0;
2809 struct ext4_inode_ref inode_ref;
2810 struct ext4_mountpoint *mp = ext4_get_mount(path);
2814 if (mp->fs.read_only)
2817 dissected_name = ext4_extract_xattr_name(name, name_len,
2818 &name_index, &dissected_len,
2824 ext4_trans_start(mp);
2826 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2832 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2836 r = ext4_xattr_remove(&inode_ref, name_index, dissected_name,
2839 ext4_fs_put_inode_ref(&inode_ref);
2842 ext4_trans_abort(mp);
2844 ext4_trans_stop(mp);
2851 /*********************************DIRECTORY OPERATION************************/
2853 int ext4_dir_rm(const char *path)
2859 struct ext4_mountpoint *mp = ext4_get_mount(path);
2860 struct ext4_inode_ref act;
2861 struct ext4_inode_ref child;
2862 struct ext4_dir_iter it;
2866 uint32_t inode_current;
2876 if (mp->fs.read_only)
2881 struct ext4_fs *const fs = &mp->fs;
2884 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2886 ext4_trans_abort(mp);
2892 len = ext4_path_check(path, &is_goal);
2894 inode_current = f.inode;
2896 ext4_block_cache_write_back(mp->fs.bdev, 1);
2900 uint64_t act_curr_pos = 0;
2901 has_children = false;
2904 while (r == EOK && !has_children && !dir_end) {
2906 /*Load directory node.*/
2907 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2912 /*Initialize iterator.*/
2913 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2915 ext4_fs_put_inode_ref(&act);
2924 ext4_trans_start(mp);
2926 /*Get up directory inode when ".." entry*/
2927 if ((it.curr->name_len == 2) &&
2928 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2929 inode_up = ext4_dir_en_get_inode(it.curr);
2932 /*If directory or file entry, but not "." ".." entry*/
2933 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2935 /*Get child inode reference do unlink
2938 uint32_t inode_type;
2939 cinode = ext4_dir_en_get_inode(it.curr);
2940 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2944 /*If directory with no leaf children*/
2945 r = ext4_has_children(&has_children, &child);
2947 ext4_fs_put_inode_ref(&child);
2952 /*Has directory children. Go into this
2954 inode_up = inode_current;
2955 inode_current = cinode;
2957 ext4_fs_put_inode_ref(&child);
2960 inode_type = ext4_inode_type(&mp->fs.sb,
2964 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2965 r = ext4_trunc_inode(mp, child.index, 0);
2967 r = ext4_trunc_dir(mp, &act, &child);
2970 ext4_fs_put_inode_ref(&child);
2974 /*No children in child directory or file. Just
2976 r = ext4_unlink(f.mp, &act, &child,
2977 (char *)it.curr->name,
2980 ext4_fs_put_inode_ref(&child);
2984 ext4_inode_set_del_time(child.inode, -1L);
2985 ext4_inode_set_links_cnt(child.inode, 0);
2988 r = ext4_fs_free_inode(&child);
2990 ext4_fs_put_inode_ref(&child);
2994 r = ext4_fs_put_inode_ref(&child);
3000 r = ext4_dir_iterator_next(&it);
3004 act_curr_pos = it.curr_off;
3006 ext4_dir_iterator_fini(&it);
3008 r = ext4_fs_put_inode_ref(&act);
3010 ext4_fs_put_inode_ref(&act);
3013 ext4_trans_abort(mp);
3015 ext4_trans_stop(mp);
3019 /*Directory iterator reached last entry*/
3022 inode_current = inode_up;
3032 if (r == EOK && !depth) {
3034 struct ext4_inode_ref parent;
3035 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
3039 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
3042 ext4_fs_put_inode_ref(&act);
3046 ext4_trans_start(mp);
3048 /* In this place all directories should be
3050 * Last unlink from root of current directory*/
3051 r = ext4_unlink(f.mp, &parent, &act,
3054 ext4_fs_put_inode_ref(&parent);
3055 ext4_fs_put_inode_ref(&act);
3059 if (ext4_inode_get_links_cnt(act.inode) == 2) {
3060 ext4_inode_set_del_time(act.inode, -1L);
3061 ext4_inode_set_links_cnt(act.inode, 0);
3064 r = ext4_trunc_dir(mp, &parent, &act);
3066 ext4_fs_put_inode_ref(&parent);
3067 ext4_fs_put_inode_ref(&act);
3071 r = ext4_fs_free_inode(&act);
3073 ext4_fs_put_inode_ref(&parent);
3074 ext4_fs_put_inode_ref(&act);
3079 r = ext4_fs_put_inode_ref(&parent);
3083 r = ext4_fs_put_inode_ref(&act);
3086 ext4_trans_abort(mp);
3088 ext4_trans_stop(mp);
3091 ext4_block_cache_write_back(mp->fs.bdev, 0);
3097 int ext4_dir_mv(const char *path, const char *new_path)
3099 return ext4_frename(path, new_path);
3102 int ext4_dir_mk(const char *path)
3107 struct ext4_mountpoint *mp = ext4_get_mount(path);
3112 if (mp->fs.read_only)
3116 ext4_trans_start(mp);
3119 r = ext4_generic_open(&f, path, "r", false, 0, 0);
3121 /*Directory already created*/
3122 ext4_trans_stop(mp);
3128 r = ext4_generic_open(&f, path, "w", false, 0, 0);
3130 ext4_trans_abort(mp);
3135 ext4_trans_stop(mp);
3140 int ext4_dir_open(ext4_dir *d, const char *path)
3142 struct ext4_mountpoint *mp = ext4_get_mount(path);
3149 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
3155 int ext4_dir_close(ext4_dir *d)
3157 return ext4_fclose(&d->f);
3160 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
3162 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
3165 uint16_t name_length;
3166 ext4_direntry *de = 0;
3167 struct ext4_inode_ref dir;
3168 struct ext4_dir_iter it;
3170 EXT4_MP_LOCK(d->f.mp);
3172 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
3173 EXT4_MP_UNLOCK(d->f.mp);
3177 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
3182 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
3184 ext4_fs_put_inode_ref(&dir);
3188 memset(&d->de.name, 0, sizeof(d->de.name));
3189 name_length = ext4_dir_en_get_name_len(&d->f.mp->fs.sb,
3191 memcpy(&d->de.name, it.curr->name, name_length);
3193 /* Directly copying the content isn't safe for Big-endian targets*/
3194 d->de.inode = ext4_dir_en_get_inode(it.curr);
3195 d->de.entry_length = ext4_dir_en_get_entry_len(it.curr);
3196 d->de.name_length = name_length;
3197 d->de.inode_type = ext4_dir_en_get_inode_type(&d->f.mp->fs.sb,
3202 ext4_dir_iterator_next(&it);
3204 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
3206 ext4_dir_iterator_fini(&it);
3207 ext4_fs_put_inode_ref(&dir);
3210 EXT4_MP_UNLOCK(d->f.mp);
3214 void ext4_dir_entry_rewind(ext4_dir *d)