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);
1396 ret = ext4_block_cache_write_back(mp->fs.bdev, on);
1401 int ext4_cache_flush(const char *path)
1403 struct ext4_mountpoint *mp = ext4_get_mount(path);
1410 ret = ext4_block_cache_flush(mp->fs.bdev);
1415 int ext4_fremove(const char *path)
1418 uint32_t parent_inode;
1423 struct ext4_inode_ref child;
1424 struct ext4_inode_ref parent;
1425 struct ext4_mountpoint *mp = ext4_get_mount(path);
1430 if (mp->fs.read_only)
1434 ext4_trans_start(mp);
1436 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1437 &parent_inode, &name_off);
1439 ext4_trans_abort(mp);
1445 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1447 ext4_trans_abort(mp);
1452 /*We have file to delete. Load it.*/
1453 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1455 ext4_fs_put_inode_ref(&parent);
1456 ext4_trans_abort(mp);
1460 /* We do not allow opening files here. */
1461 if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1462 EXT4_INODE_MODE_DIRECTORY) {
1463 ext4_fs_put_inode_ref(&parent);
1464 ext4_fs_put_inode_ref(&child);
1465 ext4_trans_abort(mp);
1470 /*Link count will be zero, the inode should be freed. */
1471 if (ext4_inode_get_links_cnt(child.inode) == 1) {
1472 ext4_block_cache_write_back(mp->fs.bdev, 1);
1473 r = ext4_trunc_inode(mp, child.index, 0);
1475 ext4_fs_put_inode_ref(&parent);
1476 ext4_fs_put_inode_ref(&child);
1477 ext4_trans_abort(mp);
1481 ext4_block_cache_write_back(mp->fs.bdev, 0);
1487 len = ext4_path_check(path, &is_goal);
1489 /*Unlink from parent*/
1490 r = ext4_unlink(mp, &parent, &child, path, len);
1494 /*Link count is zero, the inode should be freed. */
1495 if (!ext4_inode_get_links_cnt(child.inode)) {
1496 ext4_inode_set_del_time(child.inode, -1L);
1498 r = ext4_fs_free_inode(&child);
1504 ext4_fs_put_inode_ref(&child);
1505 ext4_fs_put_inode_ref(&parent);
1508 ext4_trans_abort(mp);
1510 ext4_trans_stop(mp);
1516 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1517 struct ext4_inode *inode)
1521 struct ext4_inode_ref inode_ref;
1522 struct ext4_mountpoint *mp = ext4_get_mount(path);
1530 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1540 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1546 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1547 ext4_fs_put_inode_ref(&inode_ref);
1556 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1558 struct ext4_mountpoint *mp = ext4_get_mount(path);
1566 ext4_block_cache_write_back(mp->fs.bdev, 1);
1567 r = ext4_generic_open(f, path, flags, true, 0, 0);
1568 ext4_block_cache_write_back(mp->fs.bdev, 0);
1574 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1576 struct ext4_mountpoint *mp = ext4_get_mount(path);
1583 filetype = EXT4_DE_REG_FILE;
1587 ext4_block_cache_write_back(mp->fs.bdev, 1);
1588 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1589 ext4_block_cache_write_back(mp->fs.bdev, 0);
1595 int ext4_fclose(ext4_file *f)
1597 ext4_assert(f && f->mp);
1602 f->fpos = f->fsize = 0;
1607 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1609 struct ext4_inode_ref ref;
1613 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1615 EXT4_MP_UNLOCK(f->mp);
1620 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1621 if (f->fsize <= size) {
1626 /*Start write back cache mode.*/
1627 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1631 r = ext4_trunc_inode(f->mp, ref.index, size);
1639 /*Stop write back cache mode*/
1640 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1646 ext4_fs_put_inode_ref(&ref);
1651 int ext4_ftruncate(ext4_file *f, uint64_t size)
1654 ext4_assert(f && f->mp);
1656 if (f->mp->fs.read_only)
1659 if (f->flags & O_RDONLY)
1662 EXT4_MP_LOCK(f->mp);
1664 ext4_trans_start(f->mp);
1665 r = ext4_ftruncate_no_lock(f, size);
1667 ext4_trans_abort(f->mp);
1669 ext4_trans_stop(f->mp);
1671 EXT4_MP_UNLOCK(f->mp);
1675 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1678 uint32_t iblock_idx;
1679 uint32_t iblock_last;
1680 uint32_t block_size;
1682 ext4_fsblk_t fblock;
1683 ext4_fsblk_t fblock_start;
1684 uint32_t fblock_count;
1686 uint8_t *u8_buf = buf;
1688 struct ext4_inode_ref ref;
1690 ext4_assert(f && f->mp);
1692 if (f->flags & O_WRONLY)
1698 EXT4_MP_LOCK(f->mp);
1700 struct ext4_fs *const fs = &f->mp->fs;
1701 struct ext4_sblock *const sb = &f->mp->fs.sb;
1706 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1708 EXT4_MP_UNLOCK(f->mp);
1713 f->fsize = ext4_inode_get_size(sb, ref.inode);
1715 block_size = ext4_sb_get_block_size(sb);
1716 size = ((uint64_t)size > (f->fsize - f->fpos))
1717 ? ((size_t)(f->fsize - f->fpos)) : size;
1719 iblock_idx = (uint32_t)((f->fpos) / block_size);
1720 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1721 unalg = (f->fpos) % block_size;
1723 /*If the size of symlink is smaller than 60 bytes*/
1725 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1726 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1727 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1729 char *content = (char *)ref.inode->blocks;
1730 if (f->fpos < f->fsize) {
1732 if (unalg + size > (uint32_t)f->fsize)
1733 len = (uint32_t)f->fsize - unalg;
1734 memcpy(buf, content + unalg, len);
1746 if (size > (block_size - unalg))
1747 len = block_size - unalg;
1749 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1753 /* Do we get an unwritten range? */
1755 uint64_t off = fblock * block_size + unalg;
1756 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1762 memset(u8_buf, 0, len);
1777 while (size >= block_size) {
1778 while (iblock_idx < iblock_last) {
1779 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1787 fblock_start = fblock;
1789 if ((fblock_start + fblock_count) != fblock)
1795 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1800 size -= block_size * fblock_count;
1801 u8_buf += block_size * fblock_count;
1802 f->fpos += block_size * fblock_count;
1805 *rcnt += block_size * fblock_count;
1807 fblock_start = fblock;
1813 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1817 off = fblock * block_size;
1818 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1829 ext4_fs_put_inode_ref(&ref);
1830 EXT4_MP_UNLOCK(f->mp);
1834 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1838 uint32_t iblock_last;
1839 uint32_t ifile_blocks;
1840 uint32_t block_size;
1842 uint32_t fblock_count;
1844 ext4_fsblk_t fblock_start;
1846 struct ext4_inode_ref ref;
1847 const uint8_t *u8_buf = buf;
1850 ext4_assert(f && f->mp);
1852 if (f->mp->fs.read_only)
1855 if (f->flags & O_RDONLY)
1861 EXT4_MP_LOCK(f->mp);
1862 ext4_trans_start(f->mp);
1864 struct ext4_fs *const fs = &f->mp->fs;
1865 struct ext4_sblock *const sb = &f->mp->fs.sb;
1870 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1872 ext4_trans_abort(f->mp);
1873 EXT4_MP_UNLOCK(f->mp);
1878 f->fsize = ext4_inode_get_size(sb, ref.inode);
1879 block_size = ext4_sb_get_block_size(sb);
1881 iblock_last = (uint32_t)((f->fpos + size) / block_size);
1882 iblk_idx = (uint32_t)(f->fpos / block_size);
1883 ifile_blocks = (uint32_t)((f->fsize + block_size - 1) / block_size);
1885 unalg = (f->fpos) % block_size;
1890 if (size > (block_size - unalg))
1891 len = block_size - unalg;
1893 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1897 off = fblk * block_size + unalg;
1898 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1912 /*Start write back cache mode.*/
1913 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1919 while (size >= block_size) {
1921 while (iblk_idx < iblock_last) {
1922 if (iblk_idx < ifile_blocks) {
1923 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1928 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1931 /* Unable to append more blocks. But
1932 * some block might be allocated already
1940 if (!fblock_start) {
1941 fblock_start = fblk;
1944 if ((fblock_start + fblock_count) != fblk)
1950 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1955 size -= block_size * fblock_count;
1956 u8_buf += block_size * fblock_count;
1957 f->fpos += block_size * fblock_count;
1960 *wcnt += block_size * fblock_count;
1962 fblock_start = fblk;
1966 /*ext4_fs_append_inode_block has failed and no
1967 * more blocks might be written. But node size
1968 * should be updated.*/
1974 /*Stop write back cache mode*/
1975 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1982 if (iblk_idx < ifile_blocks) {
1983 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1987 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1989 /*Node size sholud be updated.*/
1993 off = fblk * block_size;
1994 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
2005 if (f->fpos > f->fsize) {
2007 ext4_inode_set_size(ref.inode, f->fsize);
2012 r = ext4_fs_put_inode_ref(&ref);
2015 ext4_trans_abort(f->mp);
2017 ext4_trans_stop(f->mp);
2019 EXT4_MP_UNLOCK(f->mp);
2023 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
2027 if (offset > f->fsize)
2033 if ((offset + f->fpos) > f->fsize)
2039 if (offset > f->fsize)
2042 f->fpos = f->fsize - offset;
2048 uint64_t ext4_ftell(ext4_file *f)
2053 uint64_t ext4_fsize(ext4_file *f)
2058 int ext4_chmod(const char *path, uint32_t mode)
2061 uint32_t ino, orig_mode;
2063 struct ext4_sblock *sb;
2064 struct ext4_inode_ref inode_ref;
2065 struct ext4_mountpoint *mp = ext4_get_mount(path);
2070 if (mp->fs.read_only)
2074 ext4_trans_start(mp);
2076 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2078 ext4_trans_abort(mp);
2085 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2087 ext4_trans_abort(mp);
2092 orig_mode = ext4_inode_get_mode(sb, inode_ref.inode);
2093 orig_mode &= ~0xFFF;
2094 orig_mode |= mode & 0xFFF;
2095 ext4_inode_set_mode(sb, inode_ref.inode, orig_mode);
2096 inode_ref.dirty = true;
2098 r = ext4_fs_put_inode_ref(&inode_ref);
2100 ext4_trans_abort(mp);
2102 ext4_trans_stop(mp);
2108 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
2113 struct ext4_inode_ref inode_ref;
2114 struct ext4_mountpoint *mp = ext4_get_mount(path);
2119 if (mp->fs.read_only)
2123 ext4_trans_start(mp);
2125 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2127 ext4_trans_abort(mp);
2133 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2135 ext4_trans_abort(mp);
2140 ext4_inode_set_uid(inode_ref.inode, uid);
2141 ext4_inode_set_gid(inode_ref.inode, gid);
2142 inode_ref.dirty = true;
2144 r = ext4_fs_put_inode_ref(&inode_ref);
2146 ext4_trans_abort(mp);
2148 ext4_trans_stop(mp);
2154 int ext4_file_set_atime(const char *path, uint32_t atime)
2159 struct ext4_inode_ref inode_ref;
2160 struct ext4_mountpoint *mp = ext4_get_mount(path);
2165 if (mp->fs.read_only)
2169 ext4_trans_start(mp);
2171 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2173 ext4_trans_abort(mp);
2179 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2181 ext4_trans_abort(mp);
2186 ext4_inode_set_access_time(inode_ref.inode, atime);
2187 inode_ref.dirty = true;
2189 r = ext4_fs_put_inode_ref(&inode_ref);
2191 ext4_trans_abort(mp);
2193 ext4_trans_stop(mp);
2199 int ext4_file_set_mtime(const char *path, uint32_t mtime)
2204 struct ext4_inode_ref inode_ref;
2205 struct ext4_mountpoint *mp = ext4_get_mount(path);
2210 if (mp->fs.read_only)
2214 ext4_trans_start(mp);
2216 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2218 ext4_trans_abort(mp);
2224 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2226 ext4_trans_abort(mp);
2231 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2232 inode_ref.dirty = true;
2234 r = ext4_fs_put_inode_ref(&inode_ref);
2236 ext4_trans_abort(mp);
2238 ext4_trans_stop(mp);
2244 int ext4_file_set_ctime(const char *path, uint32_t ctime)
2249 struct ext4_inode_ref inode_ref;
2250 struct ext4_mountpoint *mp = ext4_get_mount(path);
2255 if (mp->fs.read_only)
2259 ext4_trans_start(mp);
2261 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2263 ext4_trans_abort(mp);
2269 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2271 ext4_trans_abort(mp);
2276 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2277 inode_ref.dirty = true;
2279 r = ext4_fs_put_inode_ref(&inode_ref);
2281 ext4_trans_abort(mp);
2283 ext4_trans_stop(mp);
2289 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2291 struct ext4_inode_ref ref;
2293 ext4_fsblk_t fblock;
2294 uint32_t block_size;
2297 ext4_assert(f && f->mp);
2302 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2307 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2308 if (size > block_size) {
2312 r = ext4_ftruncate_no_lock(f, 0);
2316 /*Start write back cache mode.*/
2317 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2321 /*If the size of symlink is smaller than 60 bytes*/
2322 if (size < sizeof(ref.inode->blocks)) {
2323 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2324 memcpy(ref.inode->blocks, buf, size);
2325 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2327 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2328 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2332 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2338 /*Stop write back cache mode*/
2339 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2344 ext4_inode_set_size(ref.inode, size);
2352 ext4_fs_put_inode_ref(&ref);
2356 int ext4_fsymlink(const char *target, const char *path)
2358 struct ext4_mountpoint *mp = ext4_get_mount(path);
2366 if (mp->fs.read_only)
2369 filetype = EXT4_DE_SYMLINK;
2372 ext4_trans_start(mp);
2374 ext4_block_cache_write_back(mp->fs.bdev, 1);
2375 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2377 r = ext4_fsymlink_set(&f, target, strlen(target));
2384 ext4_block_cache_write_back(mp->fs.bdev, 0);
2387 ext4_trans_abort(mp);
2389 ext4_trans_stop(mp);
2395 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2397 struct ext4_mountpoint *mp = ext4_get_mount(path);
2408 filetype = EXT4_DE_SYMLINK;
2411 ext4_block_cache_write_back(mp->fs.bdev, 1);
2412 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2414 r = ext4_fread(&f, buf, bufsize, rcnt);
2421 ext4_block_cache_write_back(mp->fs.bdev, 0);
2423 ext4_trans_abort(mp);
2425 ext4_trans_stop(mp);
2431 static int ext4_mknod_set(ext4_file *f, uint32_t dev)
2433 struct ext4_inode_ref ref;
2436 ext4_assert(f && f->mp);
2438 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2442 ext4_inode_set_dev(ref.inode, dev);
2444 ext4_inode_set_size(ref.inode, 0);
2450 r = ext4_fs_put_inode_ref(&ref);
2454 int ext4_mknod(const char *path, int filetype, uint32_t dev)
2456 struct ext4_mountpoint *mp = ext4_get_mount(path);
2463 if (mp->fs.read_only)
2467 * The filetype shouldn't be normal file, directory or
2470 if (filetype == EXT4_DE_UNKNOWN ||
2471 filetype == EXT4_DE_REG_FILE ||
2472 filetype == EXT4_DE_DIR ||
2473 filetype == EXT4_DE_SYMLINK)
2477 * Nor should it be any bogus value.
2479 if (filetype != EXT4_DE_CHRDEV &&
2480 filetype != EXT4_DE_BLKDEV &&
2481 filetype != EXT4_DE_FIFO &&
2482 filetype != EXT4_DE_SOCK)
2486 ext4_trans_start(mp);
2488 ext4_block_cache_write_back(mp->fs.bdev, 1);
2489 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2491 if (filetype == EXT4_DE_CHRDEV ||
2492 filetype == EXT4_DE_BLKDEV)
2493 r = ext4_mknod_set(&f, dev);
2500 ext4_block_cache_write_back(mp->fs.bdev, 0);
2503 ext4_trans_abort(mp);
2505 ext4_trans_stop(mp);
2511 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2512 const void *data, size_t data_size, bool replace __unused)
2519 const char *dissected_name = NULL;
2520 size_t dissected_len = 0;
2521 struct ext4_inode_ref inode_ref;
2522 struct ext4_mountpoint *mp = ext4_get_mount(path);
2526 if (mp->fs.read_only)
2529 dissected_name = ext4_extract_xattr_name(name, name_len,
2530 &name_index, &dissected_len,
2536 ext4_trans_start(mp);
2538 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2544 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2548 r = ext4_xattr_set(&inode_ref, name_index, dissected_name,
2549 dissected_len, data, data_size);
2551 ext4_fs_put_inode_ref(&inode_ref);
2554 ext4_trans_abort(mp);
2556 ext4_trans_stop(mp);
2562 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2563 void *buf, size_t buf_size, size_t *data_size)
2570 const char *dissected_name = NULL;
2571 size_t dissected_len = 0;
2572 struct ext4_inode_ref inode_ref;
2573 struct ext4_mountpoint *mp = ext4_get_mount(path);
2577 dissected_name = ext4_extract_xattr_name(name, name_len,
2578 &name_index, &dissected_len,
2584 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2590 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2594 r = ext4_xattr_get(&inode_ref, name_index, dissected_name,
2595 dissected_len, buf, buf_size, data_size);
2597 ext4_fs_put_inode_ref(&inode_ref);
2603 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2608 size_t list_len, list_size = 0;
2609 struct ext4_inode_ref inode_ref;
2610 struct ext4_xattr_list_entry *xattr_list = NULL,
2612 struct ext4_mountpoint *mp = ext4_get_mount(path);
2617 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2623 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2627 r = ext4_xattr_list(&inode_ref, NULL, &list_len);
2628 if (r == EOK && list_len) {
2629 xattr_list = ext4_malloc(list_len);
2631 ext4_fs_put_inode_ref(&inode_ref);
2636 r = ext4_xattr_list(&inode_ref, entry, &list_len);
2638 ext4_fs_put_inode_ref(&inode_ref);
2642 for (;entry;entry = entry->next) {
2644 const char *prefix =
2645 ext4_get_xattr_name_prefix(entry->name_index,
2648 if (prefix_len + entry->name_len + 1 > size) {
2649 ext4_fs_put_inode_ref(&inode_ref);
2656 memcpy(list, prefix, prefix_len);
2658 memcpy(list, entry->name,
2660 list[entry->name_len] = 0;
2661 list += entry->name_len + 1;
2663 size -= prefix_len + entry->name_len + 1;
2666 list_size += prefix_len + entry->name_len + 1;
2669 *ret_size = list_size;
2672 ext4_fs_put_inode_ref(&inode_ref);
2676 ext4_free(xattr_list);
2682 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2689 const char *dissected_name = NULL;
2690 size_t dissected_len = 0;
2691 struct ext4_inode_ref inode_ref;
2692 struct ext4_mountpoint *mp = ext4_get_mount(path);
2696 if (mp->fs.read_only)
2699 dissected_name = ext4_extract_xattr_name(name, name_len,
2700 &name_index, &dissected_len,
2706 ext4_trans_start(mp);
2708 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2714 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2718 r = ext4_xattr_remove(&inode_ref, name_index, dissected_name,
2721 ext4_fs_put_inode_ref(&inode_ref);
2724 ext4_trans_abort(mp);
2726 ext4_trans_stop(mp);
2733 /*********************************DIRECTORY OPERATION************************/
2735 int ext4_dir_rm(const char *path)
2741 struct ext4_mountpoint *mp = ext4_get_mount(path);
2742 struct ext4_inode_ref act;
2743 struct ext4_inode_ref child;
2744 struct ext4_dir_iter it;
2748 uint32_t inode_current;
2758 if (mp->fs.read_only)
2763 struct ext4_fs *const fs = &mp->fs;
2766 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2768 ext4_trans_abort(mp);
2774 len = ext4_path_check(path, &is_goal);
2776 inode_current = f.inode;
2778 ext4_block_cache_write_back(mp->fs.bdev, 1);
2782 uint64_t act_curr_pos = 0;
2783 has_children = false;
2786 while (r == EOK && !has_children && !dir_end) {
2788 /*Load directory node.*/
2789 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2794 /*Initialize iterator.*/
2795 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2797 ext4_fs_put_inode_ref(&act);
2806 ext4_trans_start(mp);
2808 /*Get up directory inode when ".." entry*/
2809 if ((it.curr->name_len == 2) &&
2810 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2811 inode_up = ext4_dir_en_get_inode(it.curr);
2814 /*If directory or file entry, but not "." ".." entry*/
2815 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2817 /*Get child inode reference do unlink
2820 uint32_t inode_type;
2821 cinode = ext4_dir_en_get_inode(it.curr);
2822 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2826 /*If directory with no leaf children*/
2827 r = ext4_has_children(&has_children, &child);
2829 ext4_fs_put_inode_ref(&child);
2834 /*Has directory children. Go into this
2836 inode_up = inode_current;
2837 inode_current = cinode;
2839 ext4_fs_put_inode_ref(&child);
2842 inode_type = ext4_inode_type(&mp->fs.sb,
2846 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2847 r = ext4_trunc_inode(mp, child.index, 0);
2849 r = ext4_trunc_dir(mp, &act, &child);
2852 ext4_fs_put_inode_ref(&child);
2856 /*No children in child directory or file. Just
2858 r = ext4_unlink(f.mp, &act, &child,
2859 (char *)it.curr->name,
2862 ext4_fs_put_inode_ref(&child);
2866 ext4_inode_set_del_time(child.inode, -1L);
2867 ext4_inode_set_links_cnt(child.inode, 0);
2870 r = ext4_fs_free_inode(&child);
2872 ext4_fs_put_inode_ref(&child);
2876 r = ext4_fs_put_inode_ref(&child);
2882 r = ext4_dir_iterator_next(&it);
2886 act_curr_pos = it.curr_off;
2888 ext4_dir_iterator_fini(&it);
2890 r = ext4_fs_put_inode_ref(&act);
2892 ext4_fs_put_inode_ref(&act);
2895 ext4_trans_abort(mp);
2897 ext4_trans_stop(mp);
2901 /*Directory iterator reached last entry*/
2904 inode_current = inode_up;
2914 if (r == EOK && !depth) {
2916 struct ext4_inode_ref parent;
2917 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2921 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
2924 ext4_fs_put_inode_ref(&act);
2928 ext4_trans_start(mp);
2930 /* In this place all directories should be
2932 * Last unlink from root of current directory*/
2933 r = ext4_unlink(f.mp, &parent, &act,
2936 ext4_fs_put_inode_ref(&parent);
2937 ext4_fs_put_inode_ref(&act);
2941 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2942 ext4_inode_set_del_time(act.inode, -1L);
2943 ext4_inode_set_links_cnt(act.inode, 0);
2946 r = ext4_trunc_dir(mp, &parent, &act);
2948 ext4_fs_put_inode_ref(&parent);
2949 ext4_fs_put_inode_ref(&act);
2953 r = ext4_fs_free_inode(&act);
2955 ext4_fs_put_inode_ref(&parent);
2956 ext4_fs_put_inode_ref(&act);
2961 r = ext4_fs_put_inode_ref(&parent);
2965 r = ext4_fs_put_inode_ref(&act);
2968 ext4_trans_abort(mp);
2970 ext4_trans_stop(mp);
2973 ext4_block_cache_write_back(mp->fs.bdev, 0);
2979 int ext4_dir_mv(const char *path, const char *new_path)
2981 return ext4_frename(path, new_path);
2984 int ext4_dir_mk(const char *path)
2989 struct ext4_mountpoint *mp = ext4_get_mount(path);
2994 if (mp->fs.read_only)
2998 ext4_trans_start(mp);
3001 r = ext4_generic_open(&f, path, "r", false, 0, 0);
3003 /*Directory already created*/
3004 ext4_trans_stop(mp);
3010 r = ext4_generic_open(&f, path, "w", false, 0, 0);
3012 ext4_trans_abort(mp);
3017 ext4_trans_stop(mp);
3022 int ext4_dir_open(ext4_dir *d, const char *path)
3024 struct ext4_mountpoint *mp = ext4_get_mount(path);
3031 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
3037 int ext4_dir_close(ext4_dir *d)
3039 return ext4_fclose(&d->f);
3042 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
3044 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
3047 uint16_t name_length;
3048 ext4_direntry *de = 0;
3049 struct ext4_inode_ref dir;
3050 struct ext4_dir_iter it;
3052 EXT4_MP_LOCK(d->f.mp);
3054 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
3055 EXT4_MP_UNLOCK(d->f.mp);
3059 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
3064 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
3066 ext4_fs_put_inode_ref(&dir);
3070 memset(&d->de.name, 0, sizeof(d->de.name));
3071 name_length = ext4_dir_en_get_name_len(&d->f.mp->fs.sb,
3073 memcpy(&d->de.name, it.curr->name, name_length);
3075 /* Directly copying the content isn't safe for Big-endian targets*/
3076 d->de.inode = ext4_dir_en_get_inode(it.curr);
3077 d->de.entry_length = ext4_dir_en_get_entry_len(it.curr);
3078 d->de.name_length = name_length;
3079 d->de.inode_type = ext4_dir_en_get_inode_type(&d->f.mp->fs.sb,
3084 ext4_dir_iterator_next(&it);
3086 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
3088 ext4_dir_iterator_fini(&it);
3089 ext4_fs_put_inode_ref(&dir);
3092 EXT4_MP_UNLOCK(d->f.mp);
3096 void ext4_dir_entry_rewind(ext4_dir *d)