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);
1002 ext4_fs_inode_blocks_init(fs, &child_ref);
1007 /*Link with root dir.*/
1008 r = ext4_link(mp, &ref, &child_ref, path, len, false);
1010 /*Fail. Free new inode.*/
1011 ext4_fs_free_inode(&child_ref);
1012 /*We do not want to write new inode.
1013 But block has to be released.*/
1014 child_ref.dirty = false;
1015 ext4_fs_put_inode_ref(&child_ref);
1019 ext4_fs_put_inode_ref(&child_ref);
1024 *parent_inode = ref.index;
1026 next_inode = ext4_dir_en_get_inode(result.dentry);
1027 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1029 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1030 imode = ext4_fs_correspond_inode_mode(t);
1032 struct ext4_inode_ref child_ref;
1033 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1037 imode = ext4_inode_type(sb, child_ref.inode);
1038 ext4_fs_put_inode_ref(&child_ref);
1041 r = ext4_dir_destroy_result(&ref, &result);
1045 /*If expected file error*/
1046 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
1050 if (ftype != EXT4_DE_UNKNOWN) {
1051 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
1052 if (df && is_goal) {
1058 r = ext4_fs_put_inode_ref(&ref);
1062 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1072 *name_off += len + 1;
1076 ext4_fs_put_inode_ref(&ref);
1082 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
1083 r = ext4_trunc_inode(mp, ref.index, 0);
1085 ext4_fs_put_inode_ref(&ref);
1091 f->fsize = ext4_inode_get_size(sb, ref.inode);
1092 f->inode = ref.index;
1095 if (f->flags & O_APPEND)
1100 r = ext4_fs_put_inode_ref(&ref);
1101 if (flags & O_CREAT) {
1103 ext4_trans_stop(mp);
1105 ext4_trans_abort(mp);
1112 /****************************************************************************/
1114 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
1115 bool file_expect, uint32_t *parent_inode,
1120 if (ext4_parse_flags(flags, &iflags) == false)
1123 if (file_expect == true)
1124 filetype = EXT4_DE_REG_FILE;
1126 filetype = EXT4_DE_DIR;
1128 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
1132 static int ext4_create_hardlink(const char *path,
1133 struct ext4_inode_ref *child_ref, bool rename)
1135 bool is_goal = false;
1136 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
1137 uint32_t next_inode;
1141 struct ext4_mountpoint *mp = ext4_get_mount(path);
1142 struct ext4_dir_search_result result;
1143 struct ext4_inode_ref ref;
1148 struct ext4_fs *const fs = &mp->fs;
1149 struct ext4_sblock *const sb = &mp->fs.sb;
1151 /*Skip mount point*/
1152 path += strlen(mp->name);
1155 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
1159 len = ext4_path_check(path, &is_goal);
1162 len = ext4_path_check(path, &is_goal);
1164 /*If root open was request.*/
1165 r = is_goal ? EINVAL : ENOENT;
1169 r = ext4_dir_find_entry(&result, &ref, path, len);
1172 /*Destroy last result*/
1173 ext4_dir_destroy_result(&ref, &result);
1175 if (r != ENOENT || !is_goal)
1178 /*Link with root dir.*/
1179 r = ext4_link(mp, &ref, child_ref, path, len, rename);
1181 } else if (r == EOK && is_goal) {
1182 /*Destroy last result*/
1183 ext4_dir_destroy_result(&ref, &result);
1188 next_inode = result.dentry->inode;
1189 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1191 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1192 inode_mode = ext4_fs_correspond_inode_mode(t);
1194 struct ext4_inode_ref child_ref;
1195 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1199 inode_mode = ext4_inode_type(sb, child_ref.inode);
1200 ext4_fs_put_inode_ref(&child_ref);
1203 r = ext4_dir_destroy_result(&ref, &result);
1207 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1208 r = is_goal ? EEXIST : ENOENT;
1212 r = ext4_fs_put_inode_ref(&ref);
1216 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1227 ext4_fs_put_inode_ref(&ref);
1231 r = ext4_fs_put_inode_ref(&ref);
1235 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1236 struct ext4_inode_ref *parent_ref,
1237 struct ext4_inode_ref *child_ref)
1242 struct ext4_mountpoint *mp = ext4_get_mount(path);
1250 len = ext4_path_check(path, &is_goal);
1252 /* Remove entry from parent directory */
1253 r = ext4_dir_remove_entry(parent_ref, path, len);
1257 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1258 EXT4_INODE_MODE_DIRECTORY)) {
1259 ext4_fs_inode_links_count_dec(parent_ref);
1260 parent_ref->dirty = true;
1266 int ext4_flink(const char *path, const char *hardlink_path)
1271 bool child_loaded = false;
1272 uint32_t parent_inode, child_inode;
1273 struct ext4_mountpoint *mp = ext4_get_mount(path);
1274 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1275 struct ext4_inode_ref child_ref;
1280 if (mp->fs.read_only)
1283 /* Will that happen? Anyway return EINVAL for such case. */
1284 if (mp != target_mp)
1288 ext4_trans_start(mp);
1290 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1291 &parent_inode, &name_off);
1295 child_inode = f.inode;
1298 /*We have file to unlink. Load it.*/
1299 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1303 child_loaded = true;
1305 /* Creating hardlink for directory is not allowed. */
1306 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1307 EXT4_INODE_MODE_DIRECTORY)) {
1312 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1316 ext4_fs_put_inode_ref(&child_ref);
1319 ext4_trans_abort(mp);
1321 ext4_trans_stop(mp);
1328 int ext4_frename(const char *path, const char *new_path)
1333 bool parent_loaded = false, child_loaded = false;
1334 uint32_t parent_inode, child_inode;
1335 struct ext4_mountpoint *mp = ext4_get_mount(path);
1336 struct ext4_inode_ref child_ref, parent_ref;
1341 if (mp->fs.read_only)
1345 ext4_trans_start(mp);
1347 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1348 &parent_inode, &name_off);
1352 child_inode = f.inode;
1356 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1360 parent_loaded = true;
1362 /*We have file to unlink. Load it.*/
1363 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1367 child_loaded = true;
1369 r = ext4_create_hardlink(new_path, &child_ref, true);
1373 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1379 ext4_fs_put_inode_ref(&parent_ref);
1382 ext4_fs_put_inode_ref(&child_ref);
1385 ext4_trans_abort(mp);
1387 ext4_trans_stop(mp);
1394 /****************************************************************************/
1396 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1398 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1407 int ext4_cache_write_back(const char *path, bool on)
1409 struct ext4_mountpoint *mp = ext4_get_mount(path);
1416 ret = ext4_block_cache_write_back(mp->fs.bdev, on);
1421 int ext4_cache_flush(const char *path)
1423 struct ext4_mountpoint *mp = ext4_get_mount(path);
1430 ret = ext4_block_cache_flush(mp->fs.bdev);
1435 int ext4_fremove(const char *path)
1438 uint32_t parent_inode;
1443 struct ext4_inode_ref child;
1444 struct ext4_inode_ref parent;
1445 struct ext4_mountpoint *mp = ext4_get_mount(path);
1450 if (mp->fs.read_only)
1454 ext4_trans_start(mp);
1456 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1457 &parent_inode, &name_off);
1459 ext4_trans_abort(mp);
1465 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1467 ext4_trans_abort(mp);
1472 /*We have file to delete. Load it.*/
1473 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1475 ext4_fs_put_inode_ref(&parent);
1476 ext4_trans_abort(mp);
1480 /* We do not allow opening files here. */
1481 if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1482 EXT4_INODE_MODE_DIRECTORY) {
1483 ext4_fs_put_inode_ref(&parent);
1484 ext4_fs_put_inode_ref(&child);
1485 ext4_trans_abort(mp);
1490 /*Link count will be zero, the inode should be freed. */
1491 if (ext4_inode_get_links_cnt(child.inode) == 1) {
1492 ext4_block_cache_write_back(mp->fs.bdev, 1);
1493 r = ext4_trunc_inode(mp, child.index, 0);
1495 ext4_fs_put_inode_ref(&parent);
1496 ext4_fs_put_inode_ref(&child);
1497 ext4_trans_abort(mp);
1501 ext4_block_cache_write_back(mp->fs.bdev, 0);
1507 len = ext4_path_check(path, &is_goal);
1509 /*Unlink from parent*/
1510 r = ext4_unlink(mp, &parent, &child, path, len);
1514 /*Link count is zero, the inode should be freed. */
1515 if (!ext4_inode_get_links_cnt(child.inode)) {
1516 ext4_inode_set_del_time(child.inode, -1L);
1518 r = ext4_fs_free_inode(&child);
1524 ext4_fs_put_inode_ref(&child);
1525 ext4_fs_put_inode_ref(&parent);
1528 ext4_trans_abort(mp);
1530 ext4_trans_stop(mp);
1536 int ext4_fopen(ext4_file *file, const char *path, const char *flags)
1538 struct ext4_mountpoint *mp = ext4_get_mount(path);
1546 ext4_block_cache_write_back(mp->fs.bdev, 1);
1547 r = ext4_generic_open(file, path, flags, true, 0, 0);
1548 ext4_block_cache_write_back(mp->fs.bdev, 0);
1554 int ext4_fopen2(ext4_file *file, const char *path, int flags)
1556 struct ext4_mountpoint *mp = ext4_get_mount(path);
1563 filetype = EXT4_DE_REG_FILE;
1567 ext4_block_cache_write_back(mp->fs.bdev, 1);
1568 r = ext4_generic_open2(file, path, flags, filetype, NULL, NULL);
1569 ext4_block_cache_write_back(mp->fs.bdev, 0);
1575 int ext4_fclose(ext4_file *file)
1577 ext4_assert(file && file->mp);
1582 file->fpos = file->fsize = 0;
1587 static int ext4_ftruncate_no_lock(ext4_file *file, uint64_t size)
1589 struct ext4_inode_ref ref;
1593 r = ext4_fs_get_inode_ref(&file->mp->fs, file->inode, &ref);
1595 EXT4_MP_UNLOCK(file->mp);
1600 file->fsize = ext4_inode_get_size(&file->mp->fs.sb, ref.inode);
1601 if (file->fsize <= size) {
1606 /*Start write back cache mode.*/
1607 r = ext4_block_cache_write_back(file->mp->fs.bdev, 1);
1611 r = ext4_trunc_inode(file->mp, ref.index, size);
1616 if (file->fpos > size)
1619 /*Stop write back cache mode*/
1620 ext4_block_cache_write_back(file->mp->fs.bdev, 0);
1626 ext4_fs_put_inode_ref(&ref);
1631 int ext4_ftruncate(ext4_file *f, uint64_t size)
1634 ext4_assert(f && f->mp);
1636 if (f->mp->fs.read_only)
1639 if (f->flags & O_RDONLY)
1642 EXT4_MP_LOCK(f->mp);
1644 ext4_trans_start(f->mp);
1645 r = ext4_ftruncate_no_lock(f, size);
1647 ext4_trans_abort(f->mp);
1649 ext4_trans_stop(f->mp);
1651 EXT4_MP_UNLOCK(f->mp);
1655 int ext4_fread(ext4_file *file, void *buf, size_t size, size_t *rcnt)
1658 uint32_t iblock_idx;
1659 uint32_t iblock_last;
1660 uint32_t block_size;
1662 ext4_fsblk_t fblock;
1663 ext4_fsblk_t fblock_start;
1664 uint32_t fblock_count;
1666 uint8_t *u8_buf = buf;
1668 struct ext4_inode_ref ref;
1670 ext4_assert(file && file->mp);
1672 if (file->flags & O_WRONLY)
1678 EXT4_MP_LOCK(file->mp);
1680 struct ext4_fs *const fs = &file->mp->fs;
1681 struct ext4_sblock *const sb = &file->mp->fs.sb;
1686 r = ext4_fs_get_inode_ref(fs, file->inode, &ref);
1688 EXT4_MP_UNLOCK(file->mp);
1693 file->fsize = ext4_inode_get_size(sb, ref.inode);
1695 block_size = ext4_sb_get_block_size(sb);
1696 size = ((uint64_t)size > (file->fsize - file->fpos))
1697 ? ((size_t)(file->fsize - file->fpos)) : size;
1699 iblock_idx = (uint32_t)((file->fpos) / block_size);
1700 iblock_last = (uint32_t)((file->fpos + size) / block_size);
1701 unalg = (file->fpos) % block_size;
1703 /*If the size of symlink is smaller than 60 bytes*/
1705 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1706 if (softlink && file->fsize < sizeof(ref.inode->blocks)
1707 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1709 char *content = (char *)ref.inode->blocks;
1710 if (file->fpos < file->fsize) {
1712 if (unalg + size > (uint32_t)file->fsize)
1713 len = (uint32_t)file->fsize - unalg;
1714 memcpy(buf, content + unalg, len);
1726 if (size > (block_size - unalg))
1727 len = block_size - unalg;
1729 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1733 /* Do we get an unwritten range? */
1735 uint64_t off = fblock * block_size + unalg;
1736 r = ext4_block_readbytes(file->mp->fs.bdev, off, u8_buf, len);
1742 memset(u8_buf, 0, len);
1757 while (size >= block_size) {
1758 while (iblock_idx < iblock_last) {
1759 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1767 fblock_start = fblock;
1769 if ((fblock_start + fblock_count) != fblock)
1775 r = ext4_blocks_get_direct(file->mp->fs.bdev, u8_buf, fblock_start,
1780 size -= block_size * fblock_count;
1781 u8_buf += block_size * fblock_count;
1782 file->fpos += block_size * fblock_count;
1785 *rcnt += block_size * fblock_count;
1787 fblock_start = fblock;
1793 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1797 off = fblock * block_size;
1798 r = ext4_block_readbytes(file->mp->fs.bdev, off, u8_buf, size);
1809 ext4_fs_put_inode_ref(&ref);
1810 EXT4_MP_UNLOCK(file->mp);
1814 int ext4_fwrite(ext4_file *file, const void *buf, size_t size, size_t *wcnt)
1818 uint32_t iblock_last;
1819 uint32_t ifile_blocks;
1820 uint32_t block_size;
1822 uint32_t fblock_count;
1824 ext4_fsblk_t fblock_start;
1826 struct ext4_inode_ref ref;
1827 const uint8_t *u8_buf = buf;
1830 ext4_assert(file && file->mp);
1832 if (file->mp->fs.read_only)
1835 if (file->flags & O_RDONLY)
1841 EXT4_MP_LOCK(file->mp);
1842 ext4_trans_start(file->mp);
1844 struct ext4_fs *const fs = &file->mp->fs;
1845 struct ext4_sblock *const sb = &file->mp->fs.sb;
1850 r = ext4_fs_get_inode_ref(fs, file->inode, &ref);
1852 ext4_trans_abort(file->mp);
1853 EXT4_MP_UNLOCK(file->mp);
1858 file->fsize = ext4_inode_get_size(sb, ref.inode);
1859 block_size = ext4_sb_get_block_size(sb);
1861 iblock_last = (uint32_t)((file->fpos + size) / block_size);
1862 iblk_idx = (uint32_t)(file->fpos / block_size);
1863 ifile_blocks = (uint32_t)((file->fsize + block_size - 1) / block_size);
1865 unalg = (file->fpos) % block_size;
1870 if (size > (block_size - unalg))
1871 len = block_size - unalg;
1873 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1877 off = fblk * block_size + unalg;
1878 r = ext4_block_writebytes(file->mp->fs.bdev, off, u8_buf, len);
1892 /*Start write back cache mode.*/
1893 r = ext4_block_cache_write_back(file->mp->fs.bdev, 1);
1899 while (size >= block_size) {
1901 while (iblk_idx < iblock_last) {
1902 if (iblk_idx < ifile_blocks) {
1903 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1908 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1911 /* Unable to append more blocks. But
1912 * some block might be allocated already
1920 if (!fblock_start) {
1921 fblock_start = fblk;
1924 if ((fblock_start + fblock_count) != fblk)
1930 r = ext4_blocks_set_direct(file->mp->fs.bdev, u8_buf, fblock_start,
1935 size -= block_size * fblock_count;
1936 u8_buf += block_size * fblock_count;
1937 file->fpos += block_size * fblock_count;
1940 *wcnt += block_size * fblock_count;
1942 fblock_start = fblk;
1946 /*ext4_fs_append_inode_block has failed and no
1947 * more blocks might be written. But node size
1948 * should be updated.*/
1954 /*Stop write back cache mode*/
1955 ext4_block_cache_write_back(file->mp->fs.bdev, 0);
1962 if (iblk_idx < ifile_blocks) {
1963 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1967 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1969 /*Node size sholud be updated.*/
1973 off = fblk * block_size;
1974 r = ext4_block_writebytes(file->mp->fs.bdev, off, u8_buf, size);
1985 if (file->fpos > file->fsize) {
1986 file->fsize = file->fpos;
1987 ext4_inode_set_size(ref.inode, file->fsize);
1992 r = ext4_fs_put_inode_ref(&ref);
1995 ext4_trans_abort(file->mp);
1997 ext4_trans_stop(file->mp);
1999 EXT4_MP_UNLOCK(file->mp);
2003 int ext4_fseek(ext4_file *file, uint64_t offset, uint32_t origin)
2007 if (offset > file->fsize)
2010 file->fpos = offset;
2013 if ((offset + file->fpos) > file->fsize)
2016 file->fpos += offset;
2019 if (offset > file->fsize)
2022 file->fpos = file->fsize - offset;
2028 uint64_t ext4_ftell(ext4_file *file)
2033 uint64_t ext4_fsize(ext4_file *file)
2039 static int ext4_trans_get_inode_ref(const char *path,
2040 struct ext4_mountpoint *mp,
2041 struct ext4_inode_ref *inode_ref)
2046 ext4_trans_start(mp);
2047 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2049 ext4_trans_abort(mp);
2053 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, inode_ref);
2055 ext4_trans_abort(mp);
2062 static int ext4_trans_put_inode_ref(struct ext4_mountpoint *mp,
2063 struct ext4_inode_ref *inode_ref)
2067 r = ext4_fs_put_inode_ref(inode_ref);
2069 ext4_trans_abort(mp);
2071 ext4_trans_stop(mp);
2077 int ext4_raw_inode_fill(const char *path, uint32_t *ret_ino,
2078 struct ext4_inode *inode)
2082 struct ext4_inode_ref inode_ref;
2083 struct ext4_mountpoint *mp = ext4_get_mount(path);
2090 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2097 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2106 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
2107 ext4_fs_put_inode_ref(&inode_ref);
2113 int ext4_mode_set(const char *path, uint32_t mode)
2117 struct ext4_inode_ref inode_ref;
2118 struct ext4_mountpoint *mp = ext4_get_mount(path);
2123 if (mp->fs.read_only)
2128 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2132 orig_mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2133 orig_mode &= ~0xFFF;
2134 orig_mode |= mode & 0xFFF;
2135 ext4_inode_set_mode(&mp->fs.sb, inode_ref.inode, orig_mode);
2137 inode_ref.dirty = true;
2138 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2146 int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid)
2149 struct ext4_inode_ref inode_ref;
2150 struct ext4_mountpoint *mp = ext4_get_mount(path);
2155 if (mp->fs.read_only)
2160 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2164 ext4_inode_set_uid(inode_ref.inode, uid);
2165 ext4_inode_set_gid(inode_ref.inode, gid);
2167 inode_ref.dirty = true;
2168 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2176 int ext4_mode_get(const char *path, uint32_t *mode)
2178 struct ext4_inode_ref inode_ref;
2179 struct ext4_mountpoint *mp = ext4_get_mount(path);
2188 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2192 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2196 *mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2197 r = ext4_fs_put_inode_ref(&inode_ref);
2205 int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid)
2207 struct ext4_inode_ref inode_ref;
2208 struct ext4_mountpoint *mp = ext4_get_mount(path);
2217 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2221 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2225 *uid = ext4_inode_get_uid(inode_ref.inode);
2226 *gid = ext4_inode_get_gid(inode_ref.inode);
2227 r = ext4_fs_put_inode_ref(&inode_ref);
2235 int ext4_atime_set(const char *path, uint32_t atime)
2237 struct ext4_inode_ref inode_ref;
2238 struct ext4_mountpoint *mp = ext4_get_mount(path);
2244 if (mp->fs.read_only)
2249 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2253 ext4_inode_set_access_time(inode_ref.inode, atime);
2254 inode_ref.dirty = true;
2255 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2263 int ext4_mtime_set(const char *path, uint32_t mtime)
2265 struct ext4_inode_ref inode_ref;
2266 struct ext4_mountpoint *mp = ext4_get_mount(path);
2272 if (mp->fs.read_only)
2277 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2281 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2282 inode_ref.dirty = true;
2283 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2291 int ext4_ctime_set(const char *path, uint32_t ctime)
2293 struct ext4_inode_ref inode_ref;
2294 struct ext4_mountpoint *mp = ext4_get_mount(path);
2300 if (mp->fs.read_only)
2305 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2309 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2310 inode_ref.dirty = true;
2311 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2319 int ext4_atime_get(const char *path, uint32_t *atime)
2321 struct ext4_inode_ref inode_ref;
2322 struct ext4_mountpoint *mp = ext4_get_mount(path);
2331 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2335 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2339 *atime = ext4_inode_get_access_time(inode_ref.inode);
2340 r = ext4_fs_put_inode_ref(&inode_ref);
2348 int ext4_mtime_get(const char *path, uint32_t *mtime)
2350 struct ext4_inode_ref inode_ref;
2351 struct ext4_mountpoint *mp = ext4_get_mount(path);
2360 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2364 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2368 *mtime = ext4_inode_get_modif_time(inode_ref.inode);
2369 r = ext4_fs_put_inode_ref(&inode_ref);
2377 int ext4_ctime_get(const char *path, uint32_t *ctime)
2379 struct ext4_inode_ref inode_ref;
2380 struct ext4_mountpoint *mp = ext4_get_mount(path);
2389 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2393 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2397 *ctime = ext4_inode_get_change_inode_time(inode_ref.inode);
2398 r = ext4_fs_put_inode_ref(&inode_ref);
2406 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2408 struct ext4_inode_ref ref;
2410 ext4_fsblk_t fblock;
2411 uint32_t block_size;
2414 ext4_assert(f && f->mp);
2419 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2424 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2425 if (size > block_size) {
2429 r = ext4_ftruncate_no_lock(f, 0);
2433 /*Start write back cache mode.*/
2434 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2438 /*If the size of symlink is smaller than 60 bytes*/
2439 if (size < sizeof(ref.inode->blocks)) {
2440 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2441 memcpy(ref.inode->blocks, buf, size);
2442 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2444 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2445 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2449 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2455 /*Stop write back cache mode*/
2456 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2461 ext4_inode_set_size(ref.inode, size);
2469 ext4_fs_put_inode_ref(&ref);
2473 int ext4_fsymlink(const char *target, const char *path)
2475 struct ext4_mountpoint *mp = ext4_get_mount(path);
2483 if (mp->fs.read_only)
2486 filetype = EXT4_DE_SYMLINK;
2489 ext4_trans_start(mp);
2491 ext4_block_cache_write_back(mp->fs.bdev, 1);
2492 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2494 r = ext4_fsymlink_set(&f, target, strlen(target));
2501 ext4_block_cache_write_back(mp->fs.bdev, 0);
2504 ext4_trans_abort(mp);
2506 ext4_trans_stop(mp);
2512 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2514 struct ext4_mountpoint *mp = ext4_get_mount(path);
2525 filetype = EXT4_DE_SYMLINK;
2528 ext4_block_cache_write_back(mp->fs.bdev, 1);
2529 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2531 r = ext4_fread(&f, buf, bufsize, rcnt);
2538 ext4_block_cache_write_back(mp->fs.bdev, 0);
2540 ext4_trans_abort(mp);
2542 ext4_trans_stop(mp);
2548 static int ext4_mknod_set(ext4_file *f, uint32_t dev)
2550 struct ext4_inode_ref ref;
2553 ext4_assert(f && f->mp);
2555 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2559 ext4_inode_set_dev(ref.inode, dev);
2561 ext4_inode_set_size(ref.inode, 0);
2567 r = ext4_fs_put_inode_ref(&ref);
2571 int ext4_mknod(const char *path, int filetype, uint32_t dev)
2573 struct ext4_mountpoint *mp = ext4_get_mount(path);
2580 if (mp->fs.read_only)
2584 * The filetype shouldn't be normal file, directory or
2587 if (filetype == EXT4_DE_UNKNOWN ||
2588 filetype == EXT4_DE_REG_FILE ||
2589 filetype == EXT4_DE_DIR ||
2590 filetype == EXT4_DE_SYMLINK)
2594 * Nor should it be any bogus value.
2596 if (filetype != EXT4_DE_CHRDEV &&
2597 filetype != EXT4_DE_BLKDEV &&
2598 filetype != EXT4_DE_FIFO &&
2599 filetype != EXT4_DE_SOCK)
2603 ext4_trans_start(mp);
2605 ext4_block_cache_write_back(mp->fs.bdev, 1);
2606 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2608 if (filetype == EXT4_DE_CHRDEV ||
2609 filetype == EXT4_DE_BLKDEV)
2610 r = ext4_mknod_set(&f, dev);
2617 ext4_block_cache_write_back(mp->fs.bdev, 0);
2620 ext4_trans_abort(mp);
2622 ext4_trans_stop(mp);
2628 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2629 const void *data, size_t data_size)
2636 const char *dissected_name = NULL;
2637 size_t dissected_len = 0;
2638 struct ext4_inode_ref inode_ref;
2639 struct ext4_mountpoint *mp = ext4_get_mount(path);
2643 if (mp->fs.read_only)
2646 dissected_name = ext4_extract_xattr_name(name, name_len,
2647 &name_index, &dissected_len,
2653 ext4_trans_start(mp);
2655 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2661 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2665 r = ext4_xattr_set(&inode_ref, name_index, dissected_name,
2666 dissected_len, data, data_size);
2668 ext4_fs_put_inode_ref(&inode_ref);
2671 ext4_trans_abort(mp);
2673 ext4_trans_stop(mp);
2679 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2680 void *buf, size_t buf_size, size_t *data_size)
2687 const char *dissected_name = NULL;
2688 size_t dissected_len = 0;
2689 struct ext4_inode_ref inode_ref;
2690 struct ext4_mountpoint *mp = ext4_get_mount(path);
2694 dissected_name = ext4_extract_xattr_name(name, name_len,
2695 &name_index, &dissected_len,
2701 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2707 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2711 r = ext4_xattr_get(&inode_ref, name_index, dissected_name,
2712 dissected_len, buf, buf_size, data_size);
2714 ext4_fs_put_inode_ref(&inode_ref);
2720 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2725 size_t list_len, list_size = 0;
2726 struct ext4_inode_ref inode_ref;
2727 struct ext4_xattr_list_entry *xattr_list = NULL,
2729 struct ext4_mountpoint *mp = ext4_get_mount(path);
2734 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2740 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2744 r = ext4_xattr_list(&inode_ref, NULL, &list_len);
2745 if (r == EOK && list_len) {
2746 xattr_list = ext4_malloc(list_len);
2748 ext4_fs_put_inode_ref(&inode_ref);
2753 r = ext4_xattr_list(&inode_ref, entry, &list_len);
2755 ext4_fs_put_inode_ref(&inode_ref);
2759 for (;entry;entry = entry->next) {
2761 const char *prefix =
2762 ext4_get_xattr_name_prefix(entry->name_index,
2765 if (prefix_len + entry->name_len + 1 > size) {
2766 ext4_fs_put_inode_ref(&inode_ref);
2773 memcpy(list, prefix, prefix_len);
2775 memcpy(list, entry->name,
2777 list[entry->name_len] = 0;
2778 list += entry->name_len + 1;
2780 size -= prefix_len + entry->name_len + 1;
2783 list_size += prefix_len + entry->name_len + 1;
2786 *ret_size = list_size;
2789 ext4_fs_put_inode_ref(&inode_ref);
2793 ext4_free(xattr_list);
2799 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2806 const char *dissected_name = NULL;
2807 size_t dissected_len = 0;
2808 struct ext4_inode_ref inode_ref;
2809 struct ext4_mountpoint *mp = ext4_get_mount(path);
2813 if (mp->fs.read_only)
2816 dissected_name = ext4_extract_xattr_name(name, name_len,
2817 &name_index, &dissected_len,
2823 ext4_trans_start(mp);
2825 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2831 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2835 r = ext4_xattr_remove(&inode_ref, name_index, dissected_name,
2838 ext4_fs_put_inode_ref(&inode_ref);
2841 ext4_trans_abort(mp);
2843 ext4_trans_stop(mp);
2850 /*********************************DIRECTORY OPERATION************************/
2852 int ext4_dir_rm(const char *path)
2858 struct ext4_mountpoint *mp = ext4_get_mount(path);
2859 struct ext4_inode_ref act;
2860 struct ext4_inode_ref child;
2861 struct ext4_dir_iter it;
2865 uint32_t inode_current;
2875 if (mp->fs.read_only)
2880 struct ext4_fs *const fs = &mp->fs;
2883 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2885 ext4_trans_abort(mp);
2891 len = ext4_path_check(path, &is_goal);
2893 inode_current = f.inode;
2895 ext4_block_cache_write_back(mp->fs.bdev, 1);
2899 uint64_t act_curr_pos = 0;
2900 has_children = false;
2903 while (r == EOK && !has_children && !dir_end) {
2905 /*Load directory node.*/
2906 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2911 /*Initialize iterator.*/
2912 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2914 ext4_fs_put_inode_ref(&act);
2923 ext4_trans_start(mp);
2925 /*Get up directory inode when ".." entry*/
2926 if ((it.curr->name_len == 2) &&
2927 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2928 inode_up = ext4_dir_en_get_inode(it.curr);
2931 /*If directory or file entry, but not "." ".." entry*/
2932 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2934 /*Get child inode reference do unlink
2937 uint32_t inode_type;
2938 cinode = ext4_dir_en_get_inode(it.curr);
2939 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2943 /*If directory with no leaf children*/
2944 r = ext4_has_children(&has_children, &child);
2946 ext4_fs_put_inode_ref(&child);
2951 /*Has directory children. Go into this
2953 inode_up = inode_current;
2954 inode_current = cinode;
2956 ext4_fs_put_inode_ref(&child);
2959 inode_type = ext4_inode_type(&mp->fs.sb,
2963 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2964 r = ext4_trunc_inode(mp, child.index, 0);
2966 r = ext4_trunc_dir(mp, &act, &child);
2969 ext4_fs_put_inode_ref(&child);
2973 /*No children in child directory or file. Just
2975 r = ext4_unlink(f.mp, &act, &child,
2976 (char *)it.curr->name,
2979 ext4_fs_put_inode_ref(&child);
2983 ext4_inode_set_del_time(child.inode, -1L);
2984 ext4_inode_set_links_cnt(child.inode, 0);
2987 r = ext4_fs_free_inode(&child);
2989 ext4_fs_put_inode_ref(&child);
2993 r = ext4_fs_put_inode_ref(&child);
2999 r = ext4_dir_iterator_next(&it);
3003 act_curr_pos = it.curr_off;
3005 ext4_dir_iterator_fini(&it);
3007 r = ext4_fs_put_inode_ref(&act);
3009 ext4_fs_put_inode_ref(&act);
3012 ext4_trans_abort(mp);
3014 ext4_trans_stop(mp);
3018 /*Directory iterator reached last entry*/
3021 inode_current = inode_up;
3031 if (r == EOK && !depth) {
3033 struct ext4_inode_ref parent;
3034 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
3038 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
3041 ext4_fs_put_inode_ref(&act);
3045 ext4_trans_start(mp);
3047 /* In this place all directories should be
3049 * Last unlink from root of current directory*/
3050 r = ext4_unlink(f.mp, &parent, &act,
3053 ext4_fs_put_inode_ref(&parent);
3054 ext4_fs_put_inode_ref(&act);
3058 if (ext4_inode_get_links_cnt(act.inode) == 2) {
3059 ext4_inode_set_del_time(act.inode, -1L);
3060 ext4_inode_set_links_cnt(act.inode, 0);
3063 r = ext4_trunc_dir(mp, &parent, &act);
3065 ext4_fs_put_inode_ref(&parent);
3066 ext4_fs_put_inode_ref(&act);
3070 r = ext4_fs_free_inode(&act);
3072 ext4_fs_put_inode_ref(&parent);
3073 ext4_fs_put_inode_ref(&act);
3078 r = ext4_fs_put_inode_ref(&parent);
3082 r = ext4_fs_put_inode_ref(&act);
3085 ext4_trans_abort(mp);
3087 ext4_trans_stop(mp);
3090 ext4_block_cache_write_back(mp->fs.bdev, 0);
3096 int ext4_dir_mv(const char *path, const char *new_path)
3098 return ext4_frename(path, new_path);
3101 int ext4_dir_mk(const char *path)
3106 struct ext4_mountpoint *mp = ext4_get_mount(path);
3111 if (mp->fs.read_only)
3115 ext4_trans_start(mp);
3118 r = ext4_generic_open(&f, path, "r", false, 0, 0);
3120 /*Directory already created*/
3121 ext4_trans_stop(mp);
3127 r = ext4_generic_open(&f, path, "w", false, 0, 0);
3129 ext4_trans_abort(mp);
3134 ext4_trans_stop(mp);
3139 int ext4_dir_open(ext4_dir *dir, const char *path)
3141 struct ext4_mountpoint *mp = ext4_get_mount(path);
3148 r = ext4_generic_open(&dir->f, path, "r", false, 0, 0);
3154 int ext4_dir_close(ext4_dir *dir)
3156 return ext4_fclose(&dir->f);
3159 const ext4_direntry *ext4_dir_entry_next(ext4_dir *dir)
3161 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
3164 uint16_t name_length;
3165 ext4_direntry *de = 0;
3166 struct ext4_inode_ref dir_inode;
3167 struct ext4_dir_iter it;
3169 EXT4_MP_LOCK(dir->f.mp);
3171 if (dir->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
3172 EXT4_MP_UNLOCK(dir->f.mp);
3176 r = ext4_fs_get_inode_ref(&dir->f.mp->fs, dir->f.inode, &dir_inode);
3181 r = ext4_dir_iterator_init(&it, &dir_inode, dir->next_off);
3183 ext4_fs_put_inode_ref(&dir_inode);
3187 memset(&dir->de.name, 0, sizeof(dir->de.name));
3188 name_length = ext4_dir_en_get_name_len(&dir->f.mp->fs.sb,
3190 memcpy(&dir->de.name, it.curr->name, name_length);
3192 /* Directly copying the content isn't safe for Big-endian targets*/
3193 dir->de.inode = ext4_dir_en_get_inode(it.curr);
3194 dir->de.entry_length = ext4_dir_en_get_entry_len(it.curr);
3195 dir->de.name_length = name_length;
3196 dir->de.inode_type = ext4_dir_en_get_inode_type(&dir->f.mp->fs.sb,
3201 ext4_dir_iterator_next(&it);
3203 dir->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
3205 ext4_dir_iterator_fini(&it);
3206 ext4_fs_put_inode_ref(&dir_inode);
3209 EXT4_MP_UNLOCK(dir->f.mp);
3213 void ext4_dir_entry_rewind(ext4_dir *dir)