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)*/
83 /**@brief OS dependent lock/unlock functions.*/
84 const struct ext4_lock *os_locks;
86 /**@brief Ext4 filesystem internals.*/
89 /**@brief Dynamic allocation cache flag.*/
93 struct jbd_journal jbd_journal;
96 /**@brief Block devices descriptor.*/
97 struct _ext4_devices {
99 /**@brief Block device name (@ref ext4_device_register)*/
102 /**@brief Block device handle.*/
103 struct ext4_blockdev *bd;
105 /**@brief Block cache handle.*/
106 struct ext4_bcache *bc;
109 /**@brief Block devices.*/
110 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
112 /**@brief Mountpoints.*/
113 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
115 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
116 const char *dev_name)
119 ext4_assert(bd && dev_name);
121 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
122 if (!_bdevices[i].bd) {
123 strcpy(_bdevices[i].name, dev_name);
124 _bdevices[i].bd = bd;
125 _bdevices[i].bc = bc;
129 if (!strcmp(_bdevices[i].name, dev_name))
135 /****************************************************************************/
137 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
139 if ((name_size == 1) && (name[0] == '.'))
142 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
148 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
150 struct ext4_sblock *sb = &enode->fs->sb;
152 /* Check if node is directory */
153 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
154 *has_children = false;
158 struct ext4_dir_iter it;
159 int rc = ext4_dir_iterator_init(&it, enode, 0);
163 /* Find a non-empty directory entry */
165 while (it.curr != NULL) {
166 if (ext4_dir_en_get_inode(it.curr) != 0) {
168 nsize = ext4_dir_en_get_name_len(sb, it.curr);
169 if (!ext4_is_dots(it.curr->name, nsize)) {
175 rc = ext4_dir_iterator_next(&it);
177 ext4_dir_iterator_fini(&it);
182 rc = ext4_dir_iterator_fini(&it);
186 *has_children = found;
191 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
192 struct ext4_inode_ref *ch, const char *n,
193 uint32_t len, bool rename)
195 /* Check maximum name length */
196 if (len > EXT4_DIRECTORY_FILENAME_LEN)
199 /* Add entry to parent directory */
200 int r = ext4_dir_add_entry(parent, n, len, ch);
204 /* Fill new dir -> add '.' and '..' entries.
205 * Also newly allocated inode should have 0 link count.
208 bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
209 EXT4_INODE_MODE_DIRECTORY);
210 if (is_dir && !rename) {
212 #if CONFIG_DIR_INDEX_ENABLE
213 /* Initialize directory index if supported */
214 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
215 r = ext4_dir_dx_init(ch, parent);
219 ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
224 r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
226 ext4_dir_remove_entry(parent, n, strlen(n));
230 r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
232 ext4_dir_remove_entry(parent, n, strlen(n));
233 ext4_dir_remove_entry(ch, ".", strlen("."));
238 /*New empty directory. Two links (. and ..) */
239 ext4_inode_set_links_cnt(ch->inode, 2);
240 ext4_fs_inode_links_count_inc(parent);
242 parent->dirty = true;
246 * In case we want to rename a directory,
247 * we reset the original '..' pointer.
251 idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
252 struct ext4_dir_search_result res;
254 r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
258 ext4_dir_en_set_inode(res.dentry, parent->index);
259 ext4_trans_set_block_dirty(res.block.buf);
260 r = ext4_dir_destroy_result(ch, &res);
265 #if CONFIG_DIR_INDEX_ENABLE
266 r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
273 ext4_fs_inode_links_count_inc(parent);
274 parent->dirty = true;
277 ext4_fs_inode_links_count_inc(ch);
284 static int ext4_unlink(struct ext4_mountpoint *mp,
285 struct ext4_inode_ref *parent,
286 struct ext4_inode_ref *child, const char *name,
290 int rc = ext4_has_children(&has_children, child);
294 /* Cannot unlink non-empty node */
298 /* Remove entry from parent directory */
299 rc = ext4_dir_remove_entry(parent, name, name_len);
303 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
304 EXT4_INODE_MODE_DIRECTORY);
306 /* If directory - handle links from parent */
308 ext4_fs_inode_links_count_dec(parent);
309 parent->dirty = true;
313 * TODO: Update timestamps of the parent
314 * (when we have wall-clock time).
316 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
317 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
318 * parent->dirty = true;
322 * TODO: Update timestamp for inode.
324 * ext4_inode_set_change_inode_time(child->inode,
327 if (ext4_inode_get_links_cnt(child->inode)) {
328 ext4_fs_inode_links_count_dec(child);
335 /****************************************************************************/
337 int ext4_mount(const char *dev_name, const char *mount_point,
340 ext4_assert(mount_point && dev_name);
345 struct ext4_blockdev *bd = 0;
346 struct ext4_bcache *bc = 0;
347 struct ext4_mountpoint *mp = 0;
349 if (mount_point[strlen(mount_point) - 1] != '/')
352 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
353 if (_bdevices[i].name) {
354 if (!strcmp(dev_name, _bdevices[i].name)) {
355 bd = _bdevices[i].bd;
356 bc = _bdevices[i].bc;
365 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
366 if (!_mp[i].mounted) {
367 strcpy(_mp[i].name, mount_point);
373 if (!strcmp(_mp[i].name, mount_point))
380 r = ext4_block_init(bd);
384 r = ext4_fs_init(&mp->fs, bd, read_only);
390 bsize = ext4_sb_get_block_size(&mp->fs.sb);
391 ext4_block_set_lb_size(bd, bsize);
393 mp->cache_dynamic = 0;
396 /*Automatic block cache alloc.*/
397 mp->cache_dynamic = 1;
398 bc = ext4_malloc(sizeof(struct ext4_bcache));
400 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
409 if (bsize != bc->itemsize)
412 /*Bind block cache to block device*/
413 r = ext4_block_bind_bcache(bd, bc);
415 ext4_bcache_cleanup(bc);
417 if (mp->cache_dynamic) {
418 ext4_bcache_fini_dynamic(bc);
429 int ext4_umount(const char *mount_point)
433 struct ext4_mountpoint *mp = 0;
435 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
436 if (!strcmp(_mp[i].name, mount_point)) {
445 r = ext4_fs_fini(&mp->fs);
451 ext4_bcache_cleanup(mp->fs.bdev->bc);
452 if (mp->cache_dynamic) {
453 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
454 ext4_free(mp->fs.bdev->bc);
456 r = ext4_block_fini(mp->fs.bdev);
458 mp->fs.bdev->fs = NULL;
462 static struct ext4_mountpoint *ext4_get_mount(const char *path)
465 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
470 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
477 static int __ext4_journal_start(const char *mount_point)
480 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
484 if (mp->fs.read_only)
487 if (ext4_sb_feature_com(&mp->fs.sb,
488 EXT4_FCOM_HAS_JOURNAL)) {
489 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
493 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
495 mp->jbd_fs.dirty = false;
496 jbd_put_fs(&mp->jbd_fs);
499 mp->fs.jbd_fs = &mp->jbd_fs;
500 mp->fs.jbd_journal = &mp->jbd_journal;
507 static int __ext4_journal_stop(const char *mount_point)
510 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
514 if (mp->fs.read_only)
517 if (ext4_sb_feature_com(&mp->fs.sb,
518 EXT4_FCOM_HAS_JOURNAL)) {
519 r = jbd_journal_stop(&mp->jbd_journal);
521 mp->jbd_fs.dirty = false;
522 jbd_put_fs(&mp->jbd_fs);
523 mp->fs.jbd_journal = NULL;
524 mp->fs.jbd_fs = NULL;
528 r = jbd_put_fs(&mp->jbd_fs);
530 mp->fs.jbd_journal = NULL;
531 mp->fs.jbd_fs = NULL;
535 mp->fs.jbd_journal = NULL;
536 mp->fs.jbd_fs = NULL;
543 static int __ext4_recover(const char *mount_point)
545 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
551 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
552 struct jbd_fs *jbd_fs = ext4_calloc(1, sizeof(struct jbd_fs));
559 r = jbd_get_fs(&mp->fs, jbd_fs);
565 r = jbd_recover(jbd_fs);
569 if (r == EOK && !mp->fs.read_only) {
571 uint64_t free_blocks_count = 0;
572 uint32_t free_inodes_count = 0;
573 struct ext4_block_group_ref bg_ref;
575 /* Update superblock's stats */
576 for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) {
577 r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref);
582 ext4_bg_get_free_blocks_count(bg_ref.block_group,
585 ext4_bg_get_free_inodes_count(bg_ref.block_group,
588 ext4_fs_put_block_group_ref(&bg_ref);
590 ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count);
591 ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count);
592 /* We don't need to save the superblock stats immediately. */
601 static int __ext4_trans_start(struct ext4_mountpoint *mp)
604 if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
605 struct jbd_journal *journal = mp->fs.jbd_journal;
606 struct jbd_trans *trans;
607 trans = jbd_journal_new_trans(journal);
612 mp->fs.curr_trans = trans;
619 static int __ext4_trans_stop(struct ext4_mountpoint *mp)
622 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
623 struct jbd_journal *journal = mp->fs.jbd_journal;
624 struct jbd_trans *trans = mp->fs.curr_trans;
625 r = jbd_journal_commit_trans(journal, trans);
626 mp->fs.curr_trans = NULL;
632 static void __ext4_trans_abort(struct ext4_mountpoint *mp)
634 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
635 struct jbd_journal *journal = mp->fs.jbd_journal;
636 struct jbd_trans *trans = mp->fs.curr_trans;
637 jbd_journal_free_trans(journal, trans, true);
638 mp->fs.curr_trans = NULL;
642 int ext4_journal_start(const char *mount_point __unused)
645 #if CONFIG_JOURNALING_ENABLE
646 r = __ext4_journal_start(mount_point);
651 int ext4_journal_stop(const char *mount_point __unused)
654 #if CONFIG_JOURNALING_ENABLE
655 r = __ext4_journal_stop(mount_point);
660 int ext4_recover(const char *mount_point __unused)
663 #if CONFIG_JOURNALING_ENABLE
664 r = __ext4_recover(mount_point);
669 static int ext4_trans_start(struct ext4_mountpoint *mp __unused)
672 #if CONFIG_JOURNALING_ENABLE
673 r = __ext4_trans_start(mp);
678 static int ext4_trans_stop(struct ext4_mountpoint *mp __unused)
681 #if CONFIG_JOURNALING_ENABLE
682 r = __ext4_trans_stop(mp);
687 static void ext4_trans_abort(struct ext4_mountpoint *mp __unused)
689 #if CONFIG_JOURNALING_ENABLE
690 __ext4_trans_abort(mp);
695 int ext4_mount_point_stats(const char *mount_point,
696 struct ext4_mount_stats *stats)
698 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
704 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
705 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
706 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
707 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
708 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
710 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
711 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
712 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
714 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
720 int ext4_mount_setup_locks(const char *mount_point,
721 const struct ext4_lock *locks)
724 struct ext4_mountpoint *mp = 0;
726 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
727 if (!strcmp(_mp[i].name, mount_point)) {
735 mp->os_locks = locks;
739 /********************************FILE OPERATIONS*****************************/
741 static int ext4_path_check(const char *path, bool *is_goal)
745 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
747 if (path[i] == '/') {
761 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
766 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
767 *file_flags = O_RDONLY;
771 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
772 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
776 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
777 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
781 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
782 !strcmp(flags, "r+b")) {
783 *file_flags = O_RDWR;
787 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
788 !strcmp(flags, "w+b")) {
789 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
793 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
794 !strcmp(flags, "a+b")) {
795 *file_flags = O_RDWR | O_CREAT | O_APPEND;
802 static int ext4_trunc_inode(struct ext4_mountpoint *mp,
803 uint32_t index, uint64_t new_size)
806 struct ext4_fs *const fs = &mp->fs;
807 struct ext4_inode_ref inode_ref;
809 bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
810 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
814 inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
815 ext4_fs_put_inode_ref(&inode_ref);
819 while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
821 inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
823 ext4_trans_start(mp);
824 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
826 ext4_trans_abort(mp);
829 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
831 ext4_fs_put_inode_ref(&inode_ref);
833 r = ext4_fs_put_inode_ref(&inode_ref);
836 ext4_trans_abort(mp);
842 if (inode_size > new_size) {
844 inode_size = new_size;
846 ext4_trans_start(mp);
847 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
849 ext4_trans_abort(mp);
852 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
854 ext4_fs_put_inode_ref(&inode_ref);
856 r = ext4_fs_put_inode_ref(&inode_ref);
859 ext4_trans_abort(mp);
868 ext4_trans_start(mp);
873 static int ext4_trunc_dir(struct ext4_mountpoint *mp,
874 struct ext4_inode_ref *parent,
875 struct ext4_inode_ref *dir)
878 bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
879 EXT4_INODE_MODE_DIRECTORY);
880 uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
884 #if CONFIG_DIR_INDEX_ENABLE
885 /* Initialize directory index if supported */
886 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
887 r = ext4_dir_dx_init(dir, parent);
891 r = ext4_trunc_inode(mp, dir->index,
892 EXT4_DIR_DX_INIT_BCNT * block_size);
898 r = ext4_trunc_inode(mp, dir->index, block_size);
903 return ext4_fs_truncate_inode(dir, 0);
907 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
908 * any filetype of the target dir entry will be accepted.
910 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
911 int ftype, uint32_t *parent_inode,
914 bool is_goal = false;
915 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
920 struct ext4_mountpoint *mp = ext4_get_mount(path);
921 struct ext4_dir_search_result result;
922 struct ext4_inode_ref ref;
929 struct ext4_fs *const fs = &mp->fs;
930 struct ext4_sblock *const sb = &mp->fs.sb;
932 if (fs->read_only && flags & O_CREAT)
938 path += strlen(mp->name);
941 *name_off = strlen(mp->name);
944 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
949 *parent_inode = ref.index;
952 ext4_trans_start(mp);
954 len = ext4_path_check(path, &is_goal);
957 len = ext4_path_check(path, &is_goal);
959 /*If root open was request.*/
960 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
968 r = ext4_dir_find_entry(&result, &ref, path, len);
971 /*Destroy last result*/
972 ext4_dir_destroy_result(&ref, &result);
976 if (!(f->flags & O_CREAT))
979 /*O_CREAT allows create new entry*/
980 struct ext4_inode_ref child_ref;
981 r = ext4_fs_alloc_inode(fs, &child_ref,
982 is_goal ? ftype : EXT4_DE_DIR);
987 /*Link with root dir.*/
988 r = ext4_link(mp, &ref, &child_ref, path, len, false);
990 /*Fail. Free new inode.*/
991 ext4_fs_free_inode(&child_ref);
992 /*We do not want to write new inode.
993 But block has to be released.*/
994 child_ref.dirty = false;
995 ext4_fs_put_inode_ref(&child_ref);
999 ext4_fs_put_inode_ref(&child_ref);
1004 *parent_inode = ref.index;
1006 next_inode = ext4_dir_en_get_inode(result.dentry);
1007 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1009 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1010 imode = ext4_fs_correspond_inode_mode(t);
1012 struct ext4_inode_ref child_ref;
1013 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1017 imode = ext4_inode_type(sb, child_ref.inode);
1018 ext4_fs_put_inode_ref(&child_ref);
1021 r = ext4_dir_destroy_result(&ref, &result);
1025 /*If expected file error*/
1026 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
1030 if (ftype != EXT4_DE_UNKNOWN) {
1031 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
1032 if (df && is_goal) {
1038 r = ext4_fs_put_inode_ref(&ref);
1042 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1052 *name_off += len + 1;
1056 ext4_fs_put_inode_ref(&ref);
1062 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
1063 r = ext4_trunc_inode(mp, ref.index, 0);
1065 ext4_fs_put_inode_ref(&ref);
1071 f->fsize = ext4_inode_get_size(sb, ref.inode);
1072 f->inode = ref.index;
1075 if (f->flags & O_APPEND)
1080 r = ext4_fs_put_inode_ref(&ref);
1081 if (flags & O_CREAT) {
1083 ext4_trans_stop(mp);
1085 ext4_trans_abort(mp);
1092 /****************************************************************************/
1094 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
1095 bool file_expect, uint32_t *parent_inode,
1100 if (ext4_parse_flags(flags, &iflags) == false)
1103 if (file_expect == true)
1104 filetype = EXT4_DE_REG_FILE;
1106 filetype = EXT4_DE_DIR;
1108 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
1112 static int ext4_create_hardlink(const char *path,
1113 struct ext4_inode_ref *child_ref, bool rename)
1115 bool is_goal = false;
1116 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
1117 uint32_t next_inode;
1121 struct ext4_mountpoint *mp = ext4_get_mount(path);
1122 struct ext4_dir_search_result result;
1123 struct ext4_inode_ref ref;
1128 struct ext4_fs *const fs = &mp->fs;
1129 struct ext4_sblock *const sb = &mp->fs.sb;
1131 /*Skip mount point*/
1132 path += strlen(mp->name);
1135 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
1139 len = ext4_path_check(path, &is_goal);
1142 len = ext4_path_check(path, &is_goal);
1144 /*If root open was request.*/
1145 r = is_goal ? EINVAL : ENOENT;
1149 r = ext4_dir_find_entry(&result, &ref, path, len);
1152 /*Destroy last result*/
1153 ext4_dir_destroy_result(&ref, &result);
1155 if (r != ENOENT || !is_goal)
1158 /*Link with root dir.*/
1159 r = ext4_link(mp, &ref, child_ref, path, len, rename);
1161 } else if (r == EOK && is_goal) {
1162 /*Destroy last result*/
1163 ext4_dir_destroy_result(&ref, &result);
1168 next_inode = result.dentry->inode;
1169 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1171 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1172 inode_mode = ext4_fs_correspond_inode_mode(t);
1174 struct ext4_inode_ref child_ref;
1175 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1179 inode_mode = ext4_inode_type(sb, child_ref.inode);
1180 ext4_fs_put_inode_ref(&child_ref);
1183 r = ext4_dir_destroy_result(&ref, &result);
1187 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1188 r = is_goal ? EEXIST : ENOENT;
1192 r = ext4_fs_put_inode_ref(&ref);
1196 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1207 ext4_fs_put_inode_ref(&ref);
1211 r = ext4_fs_put_inode_ref(&ref);
1215 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1216 struct ext4_inode_ref *parent_ref,
1217 struct ext4_inode_ref *child_ref)
1222 struct ext4_mountpoint *mp = ext4_get_mount(path);
1230 len = ext4_path_check(path, &is_goal);
1232 /* Remove entry from parent directory */
1233 r = ext4_dir_remove_entry(parent_ref, path, len);
1237 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1238 EXT4_INODE_MODE_DIRECTORY)) {
1239 ext4_fs_inode_links_count_dec(parent_ref);
1240 parent_ref->dirty = true;
1246 int ext4_flink(const char *path, const char *hardlink_path)
1251 bool child_loaded = false;
1252 uint32_t parent_inode, child_inode;
1253 struct ext4_mountpoint *mp = ext4_get_mount(path);
1254 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1255 struct ext4_inode_ref child_ref;
1260 if (mp->fs.read_only)
1263 /* Will that happen? Anyway return EINVAL for such case. */
1264 if (mp != target_mp)
1268 ext4_trans_start(mp);
1270 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1271 &parent_inode, &name_off);
1275 child_inode = f.inode;
1278 /*We have file to unlink. Load it.*/
1279 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1283 child_loaded = true;
1285 /* Creating hardlink for directory is not allowed. */
1286 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1287 EXT4_INODE_MODE_DIRECTORY)) {
1292 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1296 ext4_fs_put_inode_ref(&child_ref);
1299 ext4_trans_abort(mp);
1301 ext4_trans_stop(mp);
1308 int ext4_frename(const char *path, const char *new_path)
1313 bool parent_loaded = false, child_loaded = false;
1314 uint32_t parent_inode, child_inode;
1315 struct ext4_mountpoint *mp = ext4_get_mount(path);
1316 struct ext4_inode_ref child_ref, parent_ref;
1321 if (mp->fs.read_only)
1325 ext4_trans_start(mp);
1327 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1328 &parent_inode, &name_off);
1332 child_inode = f.inode;
1336 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1340 parent_loaded = true;
1342 /*We have file to unlink. Load it.*/
1343 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1347 child_loaded = true;
1349 r = ext4_create_hardlink(new_path, &child_ref, true);
1353 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1359 ext4_fs_put_inode_ref(&parent_ref);
1362 ext4_fs_put_inode_ref(&child_ref);
1365 ext4_trans_abort(mp);
1367 ext4_trans_stop(mp);
1374 /****************************************************************************/
1376 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1378 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1387 int ext4_cache_write_back(const char *path, bool on)
1389 struct ext4_mountpoint *mp = ext4_get_mount(path);
1395 ext4_block_cache_write_back(mp->fs.bdev, on);
1400 int ext4_fremove(const char *path)
1403 uint32_t parent_inode;
1408 struct ext4_inode_ref child;
1409 struct ext4_inode_ref parent;
1410 struct ext4_mountpoint *mp = ext4_get_mount(path);
1415 if (mp->fs.read_only)
1419 ext4_trans_start(mp);
1421 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1422 &parent_inode, &name_off);
1424 ext4_trans_abort(mp);
1430 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1432 ext4_trans_abort(mp);
1437 /*We have file to delete. Load it.*/
1438 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1440 ext4_fs_put_inode_ref(&parent);
1441 ext4_trans_abort(mp);
1445 /* We do not allow opening files here. */
1446 if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1447 EXT4_INODE_MODE_DIRECTORY) {
1448 ext4_fs_put_inode_ref(&parent);
1449 ext4_fs_put_inode_ref(&child);
1450 ext4_trans_abort(mp);
1455 /*Link count will be zero, the inode should be freed. */
1456 if (ext4_inode_get_links_cnt(child.inode) == 1) {
1457 ext4_block_cache_write_back(mp->fs.bdev, 1);
1458 r = ext4_trunc_inode(mp, child.index, 0);
1460 ext4_fs_put_inode_ref(&parent);
1461 ext4_fs_put_inode_ref(&child);
1462 ext4_trans_abort(mp);
1466 ext4_block_cache_write_back(mp->fs.bdev, 0);
1472 len = ext4_path_check(path, &is_goal);
1474 /*Unlink from parent*/
1475 r = ext4_unlink(mp, &parent, &child, path, len);
1479 /*Link count is zero, the inode should be freed. */
1480 if (!ext4_inode_get_links_cnt(child.inode)) {
1481 ext4_inode_set_del_time(child.inode, -1L);
1483 r = ext4_fs_free_inode(&child);
1489 ext4_fs_put_inode_ref(&child);
1490 ext4_fs_put_inode_ref(&parent);
1493 ext4_trans_abort(mp);
1495 ext4_trans_stop(mp);
1501 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1502 struct ext4_inode *inode)
1506 struct ext4_inode_ref inode_ref;
1507 struct ext4_mountpoint *mp = ext4_get_mount(path);
1515 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1525 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1531 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1532 ext4_fs_put_inode_ref(&inode_ref);
1541 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1543 struct ext4_mountpoint *mp = ext4_get_mount(path);
1551 ext4_block_cache_write_back(mp->fs.bdev, 1);
1552 r = ext4_generic_open(f, path, flags, true, 0, 0);
1553 ext4_block_cache_write_back(mp->fs.bdev, 0);
1559 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1561 struct ext4_mountpoint *mp = ext4_get_mount(path);
1568 filetype = EXT4_DE_REG_FILE;
1572 ext4_block_cache_write_back(mp->fs.bdev, 1);
1573 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1574 ext4_block_cache_write_back(mp->fs.bdev, 0);
1580 int ext4_fclose(ext4_file *f)
1582 ext4_assert(f && f->mp);
1587 f->fpos = f->fsize = 0;
1592 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1594 struct ext4_inode_ref ref;
1598 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1600 EXT4_MP_UNLOCK(f->mp);
1605 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1606 if (f->fsize <= size) {
1611 /*Start write back cache mode.*/
1612 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1616 r = ext4_trunc_inode(f->mp, ref.index, size);
1624 /*Stop write back cache mode*/
1625 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1631 ext4_fs_put_inode_ref(&ref);
1636 int ext4_ftruncate(ext4_file *f, uint64_t size)
1639 ext4_assert(f && f->mp);
1641 if (f->mp->fs.read_only)
1644 if (f->flags & O_RDONLY)
1647 EXT4_MP_LOCK(f->mp);
1649 ext4_trans_start(f->mp);
1650 r = ext4_ftruncate_no_lock(f, size);
1652 ext4_trans_abort(f->mp);
1654 ext4_trans_stop(f->mp);
1656 EXT4_MP_UNLOCK(f->mp);
1660 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1663 uint32_t iblock_idx;
1664 uint32_t iblock_last;
1665 uint32_t block_size;
1667 ext4_fsblk_t fblock;
1668 ext4_fsblk_t fblock_start;
1669 uint32_t fblock_count;
1671 uint8_t *u8_buf = buf;
1673 struct ext4_inode_ref ref;
1675 ext4_assert(f && f->mp);
1677 if (f->flags & O_WRONLY)
1683 EXT4_MP_LOCK(f->mp);
1685 struct ext4_fs *const fs = &f->mp->fs;
1686 struct ext4_sblock *const sb = &f->mp->fs.sb;
1691 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1693 EXT4_MP_UNLOCK(f->mp);
1698 f->fsize = ext4_inode_get_size(sb, ref.inode);
1700 block_size = ext4_sb_get_block_size(sb);
1701 size = ((uint64_t)size > (f->fsize - f->fpos))
1702 ? ((size_t)(f->fsize - f->fpos)) : size;
1704 iblock_idx = (uint32_t)((f->fpos) / block_size);
1705 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1706 unalg = (f->fpos) % block_size;
1708 /*If the size of symlink is smaller than 60 bytes*/
1710 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1711 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1712 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1714 char *content = (char *)ref.inode->blocks;
1715 if (f->fpos < f->fsize) {
1717 if (unalg + size > (uint32_t)f->fsize)
1718 len = (uint32_t)f->fsize - unalg;
1719 memcpy(buf, content + unalg, len);
1731 if (size > (block_size - unalg))
1732 len = block_size - unalg;
1734 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1738 /* Do we get an unwritten range? */
1740 uint64_t off = fblock * block_size + unalg;
1741 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1747 memset(u8_buf, 0, len);
1762 while (size >= block_size) {
1763 while (iblock_idx < iblock_last) {
1764 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1772 fblock_start = fblock;
1774 if ((fblock_start + fblock_count) != fblock)
1780 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1785 size -= block_size * fblock_count;
1786 u8_buf += block_size * fblock_count;
1787 f->fpos += block_size * fblock_count;
1790 *rcnt += block_size * fblock_count;
1792 fblock_start = fblock;
1798 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1802 off = fblock * block_size;
1803 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1814 ext4_fs_put_inode_ref(&ref);
1815 EXT4_MP_UNLOCK(f->mp);
1819 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1823 uint32_t iblock_last;
1824 uint32_t ifile_blocks;
1825 uint32_t block_size;
1827 uint32_t fblock_count;
1829 ext4_fsblk_t fblock_start;
1831 struct ext4_inode_ref ref;
1832 const uint8_t *u8_buf = buf;
1835 ext4_assert(f && f->mp);
1837 if (f->mp->fs.read_only)
1840 if (f->flags & O_RDONLY)
1846 EXT4_MP_LOCK(f->mp);
1847 ext4_trans_start(f->mp);
1849 struct ext4_fs *const fs = &f->mp->fs;
1850 struct ext4_sblock *const sb = &f->mp->fs.sb;
1855 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1857 ext4_trans_abort(f->mp);
1858 EXT4_MP_UNLOCK(f->mp);
1863 f->fsize = ext4_inode_get_size(sb, ref.inode);
1864 block_size = ext4_sb_get_block_size(sb);
1866 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1867 iblk_idx = (uint32_t)(f->fpos / block_size);
1868 ifile_blocks = (uint32_t)((f->fsize + block_size - 1) / block_size);
1870 unalg = (f->fpos) % block_size;
1875 if (size > (block_size - unalg))
1876 len = block_size - unalg;
1878 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1882 off = fblk * block_size + unalg;
1883 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1897 /*Start write back cache mode.*/
1898 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1904 while (size >= block_size) {
1906 while (iblk_idx < iblock_last) {
1907 if (iblk_idx < ifile_blocks) {
1908 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1913 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1916 /* Unable to append more blocks. But
1917 * some block might be allocated already
1925 if (!fblock_start) {
1926 fblock_start = fblk;
1929 if ((fblock_start + fblock_count) != fblk)
1935 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1940 size -= block_size * fblock_count;
1941 u8_buf += block_size * fblock_count;
1942 f->fpos += block_size * fblock_count;
1945 *wcnt += block_size * fblock_count;
1947 fblock_start = fblk;
1951 /*ext4_fs_append_inode_block has failed and no
1952 * more blocks might be written. But node size
1953 * should be updated.*/
1959 /*Stop write back cache mode*/
1960 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1967 if (iblk_idx < ifile_blocks) {
1968 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1972 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1974 /*Node size sholud be updated.*/
1978 off = fblk * block_size;
1979 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1990 if (f->fpos > f->fsize) {
1992 ext4_inode_set_size(ref.inode, f->fsize);
1997 r = ext4_fs_put_inode_ref(&ref);
2000 ext4_trans_abort(f->mp);
2002 ext4_trans_stop(f->mp);
2004 EXT4_MP_UNLOCK(f->mp);
2008 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
2012 if (offset > f->fsize)
2018 if ((offset + f->fpos) > f->fsize)
2024 if (offset > f->fsize)
2027 f->fpos = f->fsize - offset;
2033 uint64_t ext4_ftell(ext4_file *f)
2038 uint64_t ext4_fsize(ext4_file *f)
2043 int ext4_chmod(const char *path, uint32_t mode)
2046 uint32_t ino, orig_mode;
2048 struct ext4_sblock *sb;
2049 struct ext4_inode_ref inode_ref;
2050 struct ext4_mountpoint *mp = ext4_get_mount(path);
2055 if (mp->fs.read_only)
2059 ext4_trans_start(mp);
2061 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2063 ext4_trans_abort(mp);
2070 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2072 ext4_trans_abort(mp);
2077 orig_mode = ext4_inode_get_mode(sb, inode_ref.inode);
2078 orig_mode &= ~0xFFF;
2079 orig_mode |= mode & 0xFFF;
2080 ext4_inode_set_mode(sb, inode_ref.inode, orig_mode);
2081 inode_ref.dirty = true;
2083 r = ext4_fs_put_inode_ref(&inode_ref);
2085 ext4_trans_abort(mp);
2087 ext4_trans_stop(mp);
2093 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
2098 struct ext4_inode_ref inode_ref;
2099 struct ext4_mountpoint *mp = ext4_get_mount(path);
2104 if (mp->fs.read_only)
2108 ext4_trans_start(mp);
2110 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2112 ext4_trans_abort(mp);
2118 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2120 ext4_trans_abort(mp);
2125 ext4_inode_set_uid(inode_ref.inode, uid);
2126 ext4_inode_set_gid(inode_ref.inode, gid);
2127 inode_ref.dirty = true;
2129 r = ext4_fs_put_inode_ref(&inode_ref);
2131 ext4_trans_abort(mp);
2133 ext4_trans_stop(mp);
2139 int ext4_file_set_atime(const char *path, uint32_t atime)
2144 struct ext4_inode_ref inode_ref;
2145 struct ext4_mountpoint *mp = ext4_get_mount(path);
2150 if (mp->fs.read_only)
2154 ext4_trans_start(mp);
2156 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2158 ext4_trans_abort(mp);
2164 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2166 ext4_trans_abort(mp);
2171 ext4_inode_set_access_time(inode_ref.inode, atime);
2172 inode_ref.dirty = true;
2174 r = ext4_fs_put_inode_ref(&inode_ref);
2176 ext4_trans_abort(mp);
2178 ext4_trans_stop(mp);
2184 int ext4_file_set_mtime(const char *path, uint32_t mtime)
2189 struct ext4_inode_ref inode_ref;
2190 struct ext4_mountpoint *mp = ext4_get_mount(path);
2195 if (mp->fs.read_only)
2199 ext4_trans_start(mp);
2201 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2203 ext4_trans_abort(mp);
2209 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2211 ext4_trans_abort(mp);
2216 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2217 inode_ref.dirty = true;
2219 r = ext4_fs_put_inode_ref(&inode_ref);
2221 ext4_trans_abort(mp);
2223 ext4_trans_stop(mp);
2229 int ext4_file_set_ctime(const char *path, uint32_t ctime)
2234 struct ext4_inode_ref inode_ref;
2235 struct ext4_mountpoint *mp = ext4_get_mount(path);
2240 if (mp->fs.read_only)
2244 ext4_trans_start(mp);
2246 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2248 ext4_trans_abort(mp);
2254 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2256 ext4_trans_abort(mp);
2261 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2262 inode_ref.dirty = true;
2264 r = ext4_fs_put_inode_ref(&inode_ref);
2266 ext4_trans_abort(mp);
2268 ext4_trans_stop(mp);
2274 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2276 struct ext4_inode_ref ref;
2278 ext4_fsblk_t fblock;
2279 uint32_t block_size;
2282 ext4_assert(f && f->mp);
2287 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2292 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2293 if (size > block_size) {
2297 r = ext4_ftruncate_no_lock(f, 0);
2301 /*Start write back cache mode.*/
2302 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2306 /*If the size of symlink is smaller than 60 bytes*/
2307 if (size < sizeof(ref.inode->blocks)) {
2308 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2309 memcpy(ref.inode->blocks, buf, size);
2310 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2312 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2313 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2317 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2323 /*Stop write back cache mode*/
2324 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2329 ext4_inode_set_size(ref.inode, size);
2337 ext4_fs_put_inode_ref(&ref);
2341 int ext4_fsymlink(const char *target, const char *path)
2343 struct ext4_mountpoint *mp = ext4_get_mount(path);
2351 if (mp->fs.read_only)
2354 filetype = EXT4_DE_SYMLINK;
2357 ext4_trans_start(mp);
2359 ext4_block_cache_write_back(mp->fs.bdev, 1);
2360 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2362 r = ext4_fsymlink_set(&f, target, strlen(target));
2369 ext4_block_cache_write_back(mp->fs.bdev, 0);
2372 ext4_trans_abort(mp);
2374 ext4_trans_stop(mp);
2380 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2382 struct ext4_mountpoint *mp = ext4_get_mount(path);
2393 filetype = EXT4_DE_SYMLINK;
2396 ext4_block_cache_write_back(mp->fs.bdev, 1);
2397 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2399 r = ext4_fread(&f, buf, bufsize, rcnt);
2406 ext4_block_cache_write_back(mp->fs.bdev, 0);
2408 ext4_trans_abort(mp);
2410 ext4_trans_stop(mp);
2416 static int ext4_mknod_set(ext4_file *f, uint32_t dev)
2418 struct ext4_inode_ref ref;
2421 ext4_assert(f && f->mp);
2423 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2427 ext4_inode_set_dev(ref.inode, dev);
2429 ext4_inode_set_size(ref.inode, 0);
2435 r = ext4_fs_put_inode_ref(&ref);
2439 int ext4_mknod(const char *path, int filetype, uint32_t dev)
2441 struct ext4_mountpoint *mp = ext4_get_mount(path);
2448 if (mp->fs.read_only)
2452 * The filetype shouldn't be normal file, directory or
2455 if (filetype == EXT4_DE_UNKNOWN ||
2456 filetype == EXT4_DE_REG_FILE ||
2457 filetype == EXT4_DE_DIR ||
2458 filetype == EXT4_DE_SYMLINK)
2462 * Nor should it be any bogus value.
2464 if (filetype != EXT4_DE_CHRDEV &&
2465 filetype != EXT4_DE_BLKDEV &&
2466 filetype != EXT4_DE_FIFO &&
2467 filetype != EXT4_DE_SOCK)
2471 ext4_trans_start(mp);
2473 ext4_block_cache_write_back(mp->fs.bdev, 1);
2474 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2476 if (filetype == EXT4_DE_CHRDEV ||
2477 filetype == EXT4_DE_BLKDEV)
2478 r = ext4_mknod_set(&f, dev);
2485 ext4_block_cache_write_back(mp->fs.bdev, 0);
2488 ext4_trans_abort(mp);
2490 ext4_trans_stop(mp);
2496 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2497 const void *data, size_t data_size, bool replace __unused)
2504 const char *dissected_name = NULL;
2505 size_t dissected_len = 0;
2506 struct ext4_inode_ref inode_ref;
2507 struct ext4_mountpoint *mp = ext4_get_mount(path);
2511 if (mp->fs.read_only)
2514 dissected_name = ext4_extract_xattr_name(name, name_len,
2515 &name_index, &dissected_len,
2521 ext4_trans_start(mp);
2523 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2529 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2533 r = ext4_xattr_set(&inode_ref, name_index, dissected_name,
2534 dissected_len, data, data_size);
2536 ext4_fs_put_inode_ref(&inode_ref);
2539 ext4_trans_abort(mp);
2541 ext4_trans_stop(mp);
2547 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2548 void *buf, size_t buf_size, size_t *data_size)
2555 const char *dissected_name = NULL;
2556 size_t dissected_len = 0;
2557 struct ext4_inode_ref inode_ref;
2558 struct ext4_mountpoint *mp = ext4_get_mount(path);
2562 dissected_name = ext4_extract_xattr_name(name, name_len,
2563 &name_index, &dissected_len,
2569 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2575 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2579 r = ext4_xattr_get(&inode_ref, name_index, dissected_name,
2580 dissected_len, buf, buf_size, data_size);
2582 ext4_fs_put_inode_ref(&inode_ref);
2588 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2593 size_t list_len, list_size = 0;
2594 struct ext4_inode_ref inode_ref;
2595 struct ext4_xattr_list_entry *xattr_list = NULL,
2597 struct ext4_mountpoint *mp = ext4_get_mount(path);
2602 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2608 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2612 r = ext4_xattr_list(&inode_ref, NULL, &list_len);
2613 if (r == EOK && list_len) {
2614 xattr_list = ext4_malloc(list_len);
2616 ext4_fs_put_inode_ref(&inode_ref);
2621 r = ext4_xattr_list(&inode_ref, entry, &list_len);
2623 ext4_fs_put_inode_ref(&inode_ref);
2627 for (;entry;entry = entry->next) {
2629 const char *prefix =
2630 ext4_get_xattr_name_prefix(entry->name_index,
2633 if (prefix_len + entry->name_len + 1 > size) {
2634 ext4_fs_put_inode_ref(&inode_ref);
2641 memcpy(list, prefix, prefix_len);
2643 memcpy(list, entry->name,
2645 list[entry->name_len] = 0;
2646 list += entry->name_len + 1;
2648 size -= prefix_len + entry->name_len + 1;
2651 list_size += prefix_len + entry->name_len + 1;
2654 *ret_size = list_size;
2657 ext4_fs_put_inode_ref(&inode_ref);
2661 ext4_free(xattr_list);
2667 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2674 const char *dissected_name = NULL;
2675 size_t dissected_len = 0;
2676 struct ext4_inode_ref inode_ref;
2677 struct ext4_mountpoint *mp = ext4_get_mount(path);
2681 if (mp->fs.read_only)
2684 dissected_name = ext4_extract_xattr_name(name, name_len,
2685 &name_index, &dissected_len,
2691 ext4_trans_start(mp);
2693 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2699 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2703 r = ext4_xattr_remove(&inode_ref, name_index, dissected_name,
2706 ext4_fs_put_inode_ref(&inode_ref);
2709 ext4_trans_abort(mp);
2711 ext4_trans_stop(mp);
2718 /*********************************DIRECTORY OPERATION************************/
2720 int ext4_dir_rm(const char *path)
2726 struct ext4_mountpoint *mp = ext4_get_mount(path);
2727 struct ext4_inode_ref act;
2728 struct ext4_inode_ref child;
2729 struct ext4_dir_iter it;
2733 uint32_t inode_current;
2743 if (mp->fs.read_only)
2748 struct ext4_fs *const fs = &mp->fs;
2751 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2753 ext4_trans_abort(mp);
2759 len = ext4_path_check(path, &is_goal);
2761 inode_current = f.inode;
2763 ext4_block_cache_write_back(mp->fs.bdev, 1);
2767 uint64_t act_curr_pos = 0;
2768 has_children = false;
2771 while (r == EOK && !has_children && !dir_end) {
2773 /*Load directory node.*/
2774 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2779 /*Initialize iterator.*/
2780 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2782 ext4_fs_put_inode_ref(&act);
2791 ext4_trans_start(mp);
2793 /*Get up directory inode when ".." entry*/
2794 if ((it.curr->name_len == 2) &&
2795 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2796 inode_up = ext4_dir_en_get_inode(it.curr);
2799 /*If directory or file entry, but not "." ".." entry*/
2800 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2802 /*Get child inode reference do unlink
2805 uint32_t inode_type;
2806 cinode = ext4_dir_en_get_inode(it.curr);
2807 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2811 /*If directory with no leaf children*/
2812 r = ext4_has_children(&has_children, &child);
2814 ext4_fs_put_inode_ref(&child);
2819 /*Has directory children. Go into this
2821 inode_up = inode_current;
2822 inode_current = cinode;
2824 ext4_fs_put_inode_ref(&child);
2827 inode_type = ext4_inode_type(&mp->fs.sb,
2831 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2832 r = ext4_trunc_inode(mp, child.index, 0);
2834 r = ext4_trunc_dir(mp, &act, &child);
2837 ext4_fs_put_inode_ref(&child);
2841 /*No children in child directory or file. Just
2843 r = ext4_unlink(f.mp, &act, &child,
2844 (char *)it.curr->name,
2847 ext4_fs_put_inode_ref(&child);
2851 ext4_inode_set_del_time(child.inode, -1L);
2852 ext4_inode_set_links_cnt(child.inode, 0);
2855 r = ext4_fs_free_inode(&child);
2857 ext4_fs_put_inode_ref(&child);
2861 r = ext4_fs_put_inode_ref(&child);
2867 r = ext4_dir_iterator_next(&it);
2871 act_curr_pos = it.curr_off;
2873 ext4_dir_iterator_fini(&it);
2875 r = ext4_fs_put_inode_ref(&act);
2877 ext4_fs_put_inode_ref(&act);
2880 ext4_trans_abort(mp);
2882 ext4_trans_stop(mp);
2886 /*Directory iterator reached last entry*/
2889 inode_current = inode_up;
2899 if (r == EOK && !depth) {
2901 struct ext4_inode_ref parent;
2902 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2906 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
2909 ext4_fs_put_inode_ref(&act);
2913 ext4_trans_start(mp);
2915 /* In this place all directories should be
2917 * Last unlink from root of current directory*/
2918 r = ext4_unlink(f.mp, &parent, &act,
2921 ext4_fs_put_inode_ref(&parent);
2922 ext4_fs_put_inode_ref(&act);
2926 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2927 ext4_inode_set_del_time(act.inode, -1L);
2928 ext4_inode_set_links_cnt(act.inode, 0);
2931 r = ext4_trunc_dir(mp, &parent, &act);
2933 ext4_fs_put_inode_ref(&parent);
2934 ext4_fs_put_inode_ref(&act);
2938 r = ext4_fs_free_inode(&act);
2940 ext4_fs_put_inode_ref(&parent);
2941 ext4_fs_put_inode_ref(&act);
2946 r = ext4_fs_put_inode_ref(&parent);
2950 r = ext4_fs_put_inode_ref(&act);
2953 ext4_trans_abort(mp);
2955 ext4_trans_stop(mp);
2958 ext4_block_cache_write_back(mp->fs.bdev, 0);
2964 int ext4_dir_mv(const char *path, const char *new_path)
2966 return ext4_frename(path, new_path);
2969 int ext4_dir_mk(const char *path)
2974 struct ext4_mountpoint *mp = ext4_get_mount(path);
2979 if (mp->fs.read_only)
2983 ext4_trans_start(mp);
2986 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2988 /*Directory already created*/
2989 ext4_trans_stop(mp);
2995 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2997 ext4_trans_abort(mp);
3002 ext4_trans_stop(mp);
3007 int ext4_dir_open(ext4_dir *d, const char *path)
3009 struct ext4_mountpoint *mp = ext4_get_mount(path);
3016 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
3022 int ext4_dir_close(ext4_dir *d)
3024 return ext4_fclose(&d->f);
3027 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
3029 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
3032 uint16_t name_length;
3033 ext4_direntry *de = 0;
3034 struct ext4_inode_ref dir;
3035 struct ext4_dir_iter it;
3037 EXT4_MP_LOCK(d->f.mp);
3039 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
3040 EXT4_MP_UNLOCK(d->f.mp);
3044 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
3049 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
3051 ext4_fs_put_inode_ref(&dir);
3055 memset(&d->de.name, 0, sizeof(d->de.name));
3056 name_length = ext4_dir_en_get_name_len(&d->f.mp->fs.sb,
3058 memcpy(&d->de.name, it.curr->name, name_length);
3060 /* Directly copying the content isn't safe for Big-endian targets*/
3061 d->de.inode = ext4_dir_en_get_inode(it.curr);
3062 d->de.entry_length = ext4_dir_en_get_entry_len(it.curr);
3063 d->de.name_length = name_length;
3064 d->de.inode_type = ext4_dir_en_get_inode_type(&d->f.mp->fs.sb,
3069 ext4_dir_iterator_next(&it);
3071 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
3073 ext4_dir_iterator_fini(&it);
3074 ext4_fs_put_inode_ref(&dir);
3077 EXT4_MP_UNLOCK(d->f.mp);
3081 void ext4_dir_entry_rewind(ext4_dir *d)