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_fopen(ext4_file *file, const char *path, const char *flags)
1536 struct ext4_mountpoint *mp = ext4_get_mount(path);
1544 ext4_block_cache_write_back(mp->fs.bdev, 1);
1545 r = ext4_generic_open(file, path, flags, true, 0, 0);
1546 ext4_block_cache_write_back(mp->fs.bdev, 0);
1552 int ext4_fopen2(ext4_file *file, const char *path, int flags)
1554 struct ext4_mountpoint *mp = ext4_get_mount(path);
1561 filetype = EXT4_DE_REG_FILE;
1565 ext4_block_cache_write_back(mp->fs.bdev, 1);
1566 r = ext4_generic_open2(file, path, flags, filetype, NULL, NULL);
1567 ext4_block_cache_write_back(mp->fs.bdev, 0);
1573 int ext4_fclose(ext4_file *file)
1575 ext4_assert(file && file->mp);
1580 file->fpos = file->fsize = 0;
1585 static int ext4_ftruncate_no_lock(ext4_file *file, uint64_t size)
1587 struct ext4_inode_ref ref;
1591 r = ext4_fs_get_inode_ref(&file->mp->fs, file->inode, &ref);
1593 EXT4_MP_UNLOCK(file->mp);
1598 file->fsize = ext4_inode_get_size(&file->mp->fs.sb, ref.inode);
1599 if (file->fsize <= size) {
1604 /*Start write back cache mode.*/
1605 r = ext4_block_cache_write_back(file->mp->fs.bdev, 1);
1609 r = ext4_trunc_inode(file->mp, ref.index, size);
1614 if (file->fpos > size)
1617 /*Stop write back cache mode*/
1618 ext4_block_cache_write_back(file->mp->fs.bdev, 0);
1624 ext4_fs_put_inode_ref(&ref);
1629 int ext4_ftruncate(ext4_file *f, uint64_t size)
1632 ext4_assert(f && f->mp);
1634 if (f->mp->fs.read_only)
1637 if (f->flags & O_RDONLY)
1640 EXT4_MP_LOCK(f->mp);
1642 ext4_trans_start(f->mp);
1643 r = ext4_ftruncate_no_lock(f, size);
1645 ext4_trans_abort(f->mp);
1647 ext4_trans_stop(f->mp);
1649 EXT4_MP_UNLOCK(f->mp);
1653 int ext4_fread(ext4_file *file, void *buf, size_t size, size_t *rcnt)
1656 uint32_t iblock_idx;
1657 uint32_t iblock_last;
1658 uint32_t block_size;
1660 ext4_fsblk_t fblock;
1661 ext4_fsblk_t fblock_start;
1662 uint32_t fblock_count;
1664 uint8_t *u8_buf = buf;
1666 struct ext4_inode_ref ref;
1668 ext4_assert(file && file->mp);
1670 if (file->flags & O_WRONLY)
1676 EXT4_MP_LOCK(file->mp);
1678 struct ext4_fs *const fs = &file->mp->fs;
1679 struct ext4_sblock *const sb = &file->mp->fs.sb;
1684 r = ext4_fs_get_inode_ref(fs, file->inode, &ref);
1686 EXT4_MP_UNLOCK(file->mp);
1691 file->fsize = ext4_inode_get_size(sb, ref.inode);
1693 block_size = ext4_sb_get_block_size(sb);
1694 size = ((uint64_t)size > (file->fsize - file->fpos))
1695 ? ((size_t)(file->fsize - file->fpos)) : size;
1697 iblock_idx = (uint32_t)((file->fpos) / block_size);
1698 iblock_last = (uint32_t)((file->fpos + size) / block_size);
1699 unalg = (file->fpos) % block_size;
1701 /*If the size of symlink is smaller than 60 bytes*/
1703 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1704 if (softlink && file->fsize < sizeof(ref.inode->blocks)
1705 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1707 char *content = (char *)ref.inode->blocks;
1708 if (file->fpos < file->fsize) {
1710 if (unalg + size > (uint32_t)file->fsize)
1711 len = (uint32_t)file->fsize - unalg;
1712 memcpy(buf, content + unalg, len);
1724 if (size > (block_size - unalg))
1725 len = block_size - unalg;
1727 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1731 /* Do we get an unwritten range? */
1733 uint64_t off = fblock * block_size + unalg;
1734 r = ext4_block_readbytes(file->mp->fs.bdev, off, u8_buf, len);
1740 memset(u8_buf, 0, len);
1755 while (size >= block_size) {
1756 while (iblock_idx < iblock_last) {
1757 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1765 fblock_start = fblock;
1767 if ((fblock_start + fblock_count) != fblock)
1773 r = ext4_blocks_get_direct(file->mp->fs.bdev, u8_buf, fblock_start,
1778 size -= block_size * fblock_count;
1779 u8_buf += block_size * fblock_count;
1780 file->fpos += block_size * fblock_count;
1783 *rcnt += block_size * fblock_count;
1785 fblock_start = fblock;
1791 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1795 off = fblock * block_size;
1796 r = ext4_block_readbytes(file->mp->fs.bdev, off, u8_buf, size);
1807 ext4_fs_put_inode_ref(&ref);
1808 EXT4_MP_UNLOCK(file->mp);
1812 int ext4_fwrite(ext4_file *file, const void *buf, size_t size, size_t *wcnt)
1816 uint32_t iblock_last;
1817 uint32_t ifile_blocks;
1818 uint32_t block_size;
1820 uint32_t fblock_count;
1822 ext4_fsblk_t fblock_start;
1824 struct ext4_inode_ref ref;
1825 const uint8_t *u8_buf = buf;
1828 ext4_assert(file && file->mp);
1830 if (file->mp->fs.read_only)
1833 if (file->flags & O_RDONLY)
1839 EXT4_MP_LOCK(file->mp);
1840 ext4_trans_start(file->mp);
1842 struct ext4_fs *const fs = &file->mp->fs;
1843 struct ext4_sblock *const sb = &file->mp->fs.sb;
1848 r = ext4_fs_get_inode_ref(fs, file->inode, &ref);
1850 ext4_trans_abort(file->mp);
1851 EXT4_MP_UNLOCK(file->mp);
1856 file->fsize = ext4_inode_get_size(sb, ref.inode);
1857 block_size = ext4_sb_get_block_size(sb);
1859 iblock_last = (uint32_t)((file->fpos + size) / block_size);
1860 iblk_idx = (uint32_t)(file->fpos / block_size);
1861 ifile_blocks = (uint32_t)((file->fsize + block_size - 1) / block_size);
1863 unalg = (file->fpos) % block_size;
1868 if (size > (block_size - unalg))
1869 len = block_size - unalg;
1871 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1875 off = fblk * block_size + unalg;
1876 r = ext4_block_writebytes(file->mp->fs.bdev, off, u8_buf, len);
1890 /*Start write back cache mode.*/
1891 r = ext4_block_cache_write_back(file->mp->fs.bdev, 1);
1897 while (size >= block_size) {
1899 while (iblk_idx < iblock_last) {
1900 if (iblk_idx < ifile_blocks) {
1901 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1906 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1909 /* Unable to append more blocks. But
1910 * some block might be allocated already
1918 if (!fblock_start) {
1919 fblock_start = fblk;
1922 if ((fblock_start + fblock_count) != fblk)
1928 r = ext4_blocks_set_direct(file->mp->fs.bdev, u8_buf, fblock_start,
1933 size -= block_size * fblock_count;
1934 u8_buf += block_size * fblock_count;
1935 file->fpos += block_size * fblock_count;
1938 *wcnt += block_size * fblock_count;
1940 fblock_start = fblk;
1944 /*ext4_fs_append_inode_block has failed and no
1945 * more blocks might be written. But node size
1946 * should be updated.*/
1952 /*Stop write back cache mode*/
1953 ext4_block_cache_write_back(file->mp->fs.bdev, 0);
1960 if (iblk_idx < ifile_blocks) {
1961 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1965 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1967 /*Node size sholud be updated.*/
1971 off = fblk * block_size;
1972 r = ext4_block_writebytes(file->mp->fs.bdev, off, u8_buf, size);
1983 if (file->fpos > file->fsize) {
1984 file->fsize = file->fpos;
1985 ext4_inode_set_size(ref.inode, file->fsize);
1990 r = ext4_fs_put_inode_ref(&ref);
1993 ext4_trans_abort(file->mp);
1995 ext4_trans_stop(file->mp);
1997 EXT4_MP_UNLOCK(file->mp);
2001 int ext4_fseek(ext4_file *file, uint64_t offset, uint32_t origin)
2005 if (offset > file->fsize)
2008 file->fpos = offset;
2011 if ((offset + file->fpos) > file->fsize)
2014 file->fpos += offset;
2017 if (offset > file->fsize)
2020 file->fpos = file->fsize - offset;
2026 uint64_t ext4_ftell(ext4_file *file)
2031 uint64_t ext4_fsize(ext4_file *file)
2037 static int ext4_trans_get_inode_ref(const char *path,
2038 struct ext4_mountpoint *mp,
2039 struct ext4_inode_ref *inode_ref)
2044 ext4_trans_start(mp);
2045 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2047 ext4_trans_abort(mp);
2051 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, inode_ref);
2053 ext4_trans_abort(mp);
2060 static int ext4_trans_put_inode_ref(struct ext4_mountpoint *mp,
2061 struct ext4_inode_ref *inode_ref)
2065 r = ext4_fs_put_inode_ref(inode_ref);
2067 ext4_trans_abort(mp);
2069 ext4_trans_stop(mp);
2075 int ext4_raw_inode_fill(const char *path, uint32_t *ret_ino,
2076 struct ext4_inode *inode)
2080 struct ext4_inode_ref inode_ref;
2081 struct ext4_mountpoint *mp = ext4_get_mount(path);
2088 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2095 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2104 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
2105 ext4_fs_put_inode_ref(&inode_ref);
2111 int ext4_mode_set(const char *path, uint32_t mode)
2115 struct ext4_inode_ref inode_ref;
2116 struct ext4_mountpoint *mp = ext4_get_mount(path);
2121 if (mp->fs.read_only)
2126 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2130 orig_mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2131 orig_mode &= ~0xFFF;
2132 orig_mode |= mode & 0xFFF;
2133 ext4_inode_set_mode(&mp->fs.sb, inode_ref.inode, orig_mode);
2135 inode_ref.dirty = true;
2136 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2144 int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid)
2147 struct ext4_inode_ref inode_ref;
2148 struct ext4_mountpoint *mp = ext4_get_mount(path);
2153 if (mp->fs.read_only)
2158 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2162 ext4_inode_set_uid(inode_ref.inode, uid);
2163 ext4_inode_set_gid(inode_ref.inode, gid);
2165 inode_ref.dirty = true;
2166 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2174 int ext4_mode_get(const char *path, uint32_t *mode)
2176 struct ext4_inode_ref inode_ref;
2177 struct ext4_mountpoint *mp = ext4_get_mount(path);
2186 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2190 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2194 *mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2195 r = ext4_fs_put_inode_ref(&inode_ref);
2203 int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid)
2205 struct ext4_inode_ref inode_ref;
2206 struct ext4_mountpoint *mp = ext4_get_mount(path);
2215 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2219 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2223 *uid = ext4_inode_get_uid(inode_ref.inode);
2224 *gid = ext4_inode_get_gid(inode_ref.inode);
2225 r = ext4_fs_put_inode_ref(&inode_ref);
2233 int ext4_atime_set(const char *path, uint32_t atime)
2235 struct ext4_inode_ref inode_ref;
2236 struct ext4_mountpoint *mp = ext4_get_mount(path);
2242 if (mp->fs.read_only)
2247 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2251 ext4_inode_set_access_time(inode_ref.inode, atime);
2252 inode_ref.dirty = true;
2253 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2261 int ext4_mtime_set(const char *path, uint32_t mtime)
2263 struct ext4_inode_ref inode_ref;
2264 struct ext4_mountpoint *mp = ext4_get_mount(path);
2270 if (mp->fs.read_only)
2275 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2279 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2280 inode_ref.dirty = true;
2281 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2289 int ext4_ctime_set(const char *path, uint32_t ctime)
2291 struct ext4_inode_ref inode_ref;
2292 struct ext4_mountpoint *mp = ext4_get_mount(path);
2298 if (mp->fs.read_only)
2303 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2307 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2308 inode_ref.dirty = true;
2309 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2317 int ext4_atime_get(const char *path, uint32_t *atime)
2319 struct ext4_inode_ref inode_ref;
2320 struct ext4_mountpoint *mp = ext4_get_mount(path);
2329 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2333 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2337 *atime = ext4_inode_get_access_time(inode_ref.inode);
2338 r = ext4_fs_put_inode_ref(&inode_ref);
2346 int ext4_mtime_get(const char *path, uint32_t *mtime)
2348 struct ext4_inode_ref inode_ref;
2349 struct ext4_mountpoint *mp = ext4_get_mount(path);
2358 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2362 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2366 *mtime = ext4_inode_get_modif_time(inode_ref.inode);
2367 r = ext4_fs_put_inode_ref(&inode_ref);
2375 int ext4_ctime_get(const char *path, uint32_t *ctime)
2377 struct ext4_inode_ref inode_ref;
2378 struct ext4_mountpoint *mp = ext4_get_mount(path);
2387 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2391 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2395 *ctime = ext4_inode_get_change_inode_time(inode_ref.inode);
2396 r = ext4_fs_put_inode_ref(&inode_ref);
2404 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2406 struct ext4_inode_ref ref;
2408 ext4_fsblk_t fblock;
2409 uint32_t block_size;
2412 ext4_assert(f && f->mp);
2417 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2422 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2423 if (size > block_size) {
2427 r = ext4_ftruncate_no_lock(f, 0);
2431 /*Start write back cache mode.*/
2432 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2436 /*If the size of symlink is smaller than 60 bytes*/
2437 if (size < sizeof(ref.inode->blocks)) {
2438 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2439 memcpy(ref.inode->blocks, buf, size);
2440 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2442 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2443 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2447 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2453 /*Stop write back cache mode*/
2454 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2459 ext4_inode_set_size(ref.inode, size);
2467 ext4_fs_put_inode_ref(&ref);
2471 int ext4_fsymlink(const char *target, const char *path)
2473 struct ext4_mountpoint *mp = ext4_get_mount(path);
2481 if (mp->fs.read_only)
2484 filetype = EXT4_DE_SYMLINK;
2487 ext4_trans_start(mp);
2489 ext4_block_cache_write_back(mp->fs.bdev, 1);
2490 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2492 r = ext4_fsymlink_set(&f, target, strlen(target));
2499 ext4_block_cache_write_back(mp->fs.bdev, 0);
2502 ext4_trans_abort(mp);
2504 ext4_trans_stop(mp);
2510 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2512 struct ext4_mountpoint *mp = ext4_get_mount(path);
2523 filetype = EXT4_DE_SYMLINK;
2526 ext4_block_cache_write_back(mp->fs.bdev, 1);
2527 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2529 r = ext4_fread(&f, buf, bufsize, rcnt);
2536 ext4_block_cache_write_back(mp->fs.bdev, 0);
2538 ext4_trans_abort(mp);
2540 ext4_trans_stop(mp);
2546 static int ext4_mknod_set(ext4_file *f, uint32_t dev)
2548 struct ext4_inode_ref ref;
2551 ext4_assert(f && f->mp);
2553 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2557 ext4_inode_set_dev(ref.inode, dev);
2559 ext4_inode_set_size(ref.inode, 0);
2565 r = ext4_fs_put_inode_ref(&ref);
2569 int ext4_mknod(const char *path, int filetype, uint32_t dev)
2571 struct ext4_mountpoint *mp = ext4_get_mount(path);
2578 if (mp->fs.read_only)
2582 * The filetype shouldn't be normal file, directory or
2585 if (filetype == EXT4_DE_UNKNOWN ||
2586 filetype == EXT4_DE_REG_FILE ||
2587 filetype == EXT4_DE_DIR ||
2588 filetype == EXT4_DE_SYMLINK)
2592 * Nor should it be any bogus value.
2594 if (filetype != EXT4_DE_CHRDEV &&
2595 filetype != EXT4_DE_BLKDEV &&
2596 filetype != EXT4_DE_FIFO &&
2597 filetype != EXT4_DE_SOCK)
2601 ext4_trans_start(mp);
2603 ext4_block_cache_write_back(mp->fs.bdev, 1);
2604 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2606 if (filetype == EXT4_DE_CHRDEV ||
2607 filetype == EXT4_DE_BLKDEV)
2608 r = ext4_mknod_set(&f, dev);
2615 ext4_block_cache_write_back(mp->fs.bdev, 0);
2618 ext4_trans_abort(mp);
2620 ext4_trans_stop(mp);
2626 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2627 const void *data, size_t data_size)
2634 const char *dissected_name = NULL;
2635 size_t dissected_len = 0;
2636 struct ext4_inode_ref inode_ref;
2637 struct ext4_mountpoint *mp = ext4_get_mount(path);
2641 if (mp->fs.read_only)
2644 dissected_name = ext4_extract_xattr_name(name, name_len,
2645 &name_index, &dissected_len,
2651 ext4_trans_start(mp);
2653 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2659 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2663 r = ext4_xattr_set(&inode_ref, name_index, dissected_name,
2664 dissected_len, data, data_size);
2666 ext4_fs_put_inode_ref(&inode_ref);
2669 ext4_trans_abort(mp);
2671 ext4_trans_stop(mp);
2677 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2678 void *buf, size_t buf_size, size_t *data_size)
2685 const char *dissected_name = NULL;
2686 size_t dissected_len = 0;
2687 struct ext4_inode_ref inode_ref;
2688 struct ext4_mountpoint *mp = ext4_get_mount(path);
2692 dissected_name = ext4_extract_xattr_name(name, name_len,
2693 &name_index, &dissected_len,
2699 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2705 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2709 r = ext4_xattr_get(&inode_ref, name_index, dissected_name,
2710 dissected_len, buf, buf_size, data_size);
2712 ext4_fs_put_inode_ref(&inode_ref);
2718 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2723 size_t list_len, list_size = 0;
2724 struct ext4_inode_ref inode_ref;
2725 struct ext4_xattr_list_entry *xattr_list = NULL,
2727 struct ext4_mountpoint *mp = ext4_get_mount(path);
2732 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2738 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2742 r = ext4_xattr_list(&inode_ref, NULL, &list_len);
2743 if (r == EOK && list_len) {
2744 xattr_list = ext4_malloc(list_len);
2746 ext4_fs_put_inode_ref(&inode_ref);
2751 r = ext4_xattr_list(&inode_ref, entry, &list_len);
2753 ext4_fs_put_inode_ref(&inode_ref);
2757 for (;entry;entry = entry->next) {
2759 const char *prefix =
2760 ext4_get_xattr_name_prefix(entry->name_index,
2763 if (prefix_len + entry->name_len + 1 > size) {
2764 ext4_fs_put_inode_ref(&inode_ref);
2771 memcpy(list, prefix, prefix_len);
2773 memcpy(list, entry->name,
2775 list[entry->name_len] = 0;
2776 list += entry->name_len + 1;
2778 size -= prefix_len + entry->name_len + 1;
2781 list_size += prefix_len + entry->name_len + 1;
2784 *ret_size = list_size;
2787 ext4_fs_put_inode_ref(&inode_ref);
2791 ext4_free(xattr_list);
2797 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2804 const char *dissected_name = NULL;
2805 size_t dissected_len = 0;
2806 struct ext4_inode_ref inode_ref;
2807 struct ext4_mountpoint *mp = ext4_get_mount(path);
2811 if (mp->fs.read_only)
2814 dissected_name = ext4_extract_xattr_name(name, name_len,
2815 &name_index, &dissected_len,
2821 ext4_trans_start(mp);
2823 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2829 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2833 r = ext4_xattr_remove(&inode_ref, name_index, dissected_name,
2836 ext4_fs_put_inode_ref(&inode_ref);
2839 ext4_trans_abort(mp);
2841 ext4_trans_stop(mp);
2848 /*********************************DIRECTORY OPERATION************************/
2850 int ext4_dir_rm(const char *path)
2856 struct ext4_mountpoint *mp = ext4_get_mount(path);
2857 struct ext4_inode_ref act;
2858 struct ext4_inode_ref child;
2859 struct ext4_dir_iter it;
2863 uint32_t inode_current;
2873 if (mp->fs.read_only)
2878 struct ext4_fs *const fs = &mp->fs;
2881 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2883 ext4_trans_abort(mp);
2889 len = ext4_path_check(path, &is_goal);
2891 inode_current = f.inode;
2893 ext4_block_cache_write_back(mp->fs.bdev, 1);
2897 uint64_t act_curr_pos = 0;
2898 has_children = false;
2901 while (r == EOK && !has_children && !dir_end) {
2903 /*Load directory node.*/
2904 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2909 /*Initialize iterator.*/
2910 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2912 ext4_fs_put_inode_ref(&act);
2921 ext4_trans_start(mp);
2923 /*Get up directory inode when ".." entry*/
2924 if ((it.curr->name_len == 2) &&
2925 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2926 inode_up = ext4_dir_en_get_inode(it.curr);
2929 /*If directory or file entry, but not "." ".." entry*/
2930 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2932 /*Get child inode reference do unlink
2935 uint32_t inode_type;
2936 cinode = ext4_dir_en_get_inode(it.curr);
2937 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2941 /*If directory with no leaf children*/
2942 r = ext4_has_children(&has_children, &child);
2944 ext4_fs_put_inode_ref(&child);
2949 /*Has directory children. Go into this
2951 inode_up = inode_current;
2952 inode_current = cinode;
2954 ext4_fs_put_inode_ref(&child);
2957 inode_type = ext4_inode_type(&mp->fs.sb,
2961 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2962 r = ext4_trunc_inode(mp, child.index, 0);
2964 r = ext4_trunc_dir(mp, &act, &child);
2967 ext4_fs_put_inode_ref(&child);
2971 /*No children in child directory or file. Just
2973 r = ext4_unlink(f.mp, &act, &child,
2974 (char *)it.curr->name,
2977 ext4_fs_put_inode_ref(&child);
2981 ext4_inode_set_del_time(child.inode, -1L);
2982 ext4_inode_set_links_cnt(child.inode, 0);
2985 r = ext4_fs_free_inode(&child);
2987 ext4_fs_put_inode_ref(&child);
2991 r = ext4_fs_put_inode_ref(&child);
2997 r = ext4_dir_iterator_next(&it);
3001 act_curr_pos = it.curr_off;
3003 ext4_dir_iterator_fini(&it);
3005 r = ext4_fs_put_inode_ref(&act);
3007 ext4_fs_put_inode_ref(&act);
3010 ext4_trans_abort(mp);
3012 ext4_trans_stop(mp);
3016 /*Directory iterator reached last entry*/
3019 inode_current = inode_up;
3029 if (r == EOK && !depth) {
3031 struct ext4_inode_ref parent;
3032 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
3036 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
3039 ext4_fs_put_inode_ref(&act);
3043 ext4_trans_start(mp);
3045 /* In this place all directories should be
3047 * Last unlink from root of current directory*/
3048 r = ext4_unlink(f.mp, &parent, &act,
3051 ext4_fs_put_inode_ref(&parent);
3052 ext4_fs_put_inode_ref(&act);
3056 if (ext4_inode_get_links_cnt(act.inode) == 2) {
3057 ext4_inode_set_del_time(act.inode, -1L);
3058 ext4_inode_set_links_cnt(act.inode, 0);
3061 r = ext4_trunc_dir(mp, &parent, &act);
3063 ext4_fs_put_inode_ref(&parent);
3064 ext4_fs_put_inode_ref(&act);
3068 r = ext4_fs_free_inode(&act);
3070 ext4_fs_put_inode_ref(&parent);
3071 ext4_fs_put_inode_ref(&act);
3076 r = ext4_fs_put_inode_ref(&parent);
3080 r = ext4_fs_put_inode_ref(&act);
3083 ext4_trans_abort(mp);
3085 ext4_trans_stop(mp);
3088 ext4_block_cache_write_back(mp->fs.bdev, 0);
3094 int ext4_dir_mv(const char *path, const char *new_path)
3096 return ext4_frename(path, new_path);
3099 int ext4_dir_mk(const char *path)
3104 struct ext4_mountpoint *mp = ext4_get_mount(path);
3109 if (mp->fs.read_only)
3113 ext4_trans_start(mp);
3116 r = ext4_generic_open(&f, path, "r", false, 0, 0);
3118 /*Directory already created*/
3119 ext4_trans_stop(mp);
3125 r = ext4_generic_open(&f, path, "w", false, 0, 0);
3127 ext4_trans_abort(mp);
3132 ext4_trans_stop(mp);
3137 int ext4_dir_open(ext4_dir *dir, const char *path)
3139 struct ext4_mountpoint *mp = ext4_get_mount(path);
3146 r = ext4_generic_open(&dir->f, path, "r", false, 0, 0);
3152 int ext4_dir_close(ext4_dir *dir)
3154 return ext4_fclose(&dir->f);
3157 const ext4_direntry *ext4_dir_entry_next(ext4_dir *dir)
3159 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
3162 uint16_t name_length;
3163 ext4_direntry *de = 0;
3164 struct ext4_inode_ref dir_inode;
3165 struct ext4_dir_iter it;
3167 EXT4_MP_LOCK(dir->f.mp);
3169 if (dir->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
3170 EXT4_MP_UNLOCK(dir->f.mp);
3174 r = ext4_fs_get_inode_ref(&dir->f.mp->fs, dir->f.inode, &dir_inode);
3179 r = ext4_dir_iterator_init(&it, &dir_inode, dir->next_off);
3181 ext4_fs_put_inode_ref(&dir_inode);
3185 memset(&dir->de.name, 0, sizeof(dir->de.name));
3186 name_length = ext4_dir_en_get_name_len(&dir->f.mp->fs.sb,
3188 memcpy(&dir->de.name, it.curr->name, name_length);
3190 /* Directly copying the content isn't safe for Big-endian targets*/
3191 dir->de.inode = ext4_dir_en_get_inode(it.curr);
3192 dir->de.entry_length = ext4_dir_en_get_entry_len(it.curr);
3193 dir->de.name_length = name_length;
3194 dir->de.inode_type = ext4_dir_en_get_inode_type(&dir->f.mp->fs.sb,
3199 ext4_dir_iterator_next(&it);
3201 dir->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
3203 ext4_dir_iterator_fini(&it);
3204 ext4_fs_put_inode_ref(&dir_inode);
3207 EXT4_MP_UNLOCK(dir->f.mp);
3211 void ext4_dir_entry_rewind(ext4_dir *dir)