2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup lwext4
34 * @brief Ext4 high level operations (file, directory, mountpoints...)
37 #include <ext4_config.h>
38 #include <ext4_types.h>
39 #include <ext4_misc.h>
40 #include <ext4_errno.h>
41 #include <ext4_oflags.h>
42 #include <ext4_debug.h>
45 #include <ext4_trans.h>
46 #include <ext4_blockdev.h>
49 #include <ext4_inode.h>
50 #include <ext4_super.h>
51 #include <ext4_block_group.h>
52 #include <ext4_dir_idx.h>
53 #include <ext4_xattr.h>
54 #include <ext4_journal.h>
60 /**@brief Mount point OS dependent lock*/
61 #define EXT4_MP_LOCK(_m) \
64 (_m)->os_locks->lock(); \
67 /**@brief Mount point OS dependent unlock*/
68 #define EXT4_MP_UNLOCK(_m) \
71 (_m)->os_locks->unlock(); \
74 /**@brief Mount point descriptor.*/
75 struct ext4_mountpoint {
77 /**@brief Mount done flag.*/
80 /**@brief Mount point name (@ref ext4_mount)*/
81 char name[CONFIG_EXT4_MAX_MP_NAME + 1];
83 /**@brief OS dependent lock/unlock functions.*/
84 const struct ext4_lock *os_locks;
86 /**@brief Ext4 filesystem internals.*/
93 struct jbd_journal jbd_journal;
95 /**@brief Block cache.*/
96 struct ext4_bcache bc;
99 /**@brief Block devices descriptor.*/
100 struct ext4_block_devices {
102 /**@brief Block device name.*/
103 char name[CONFIG_EXT4_MAX_BLOCKDEV_NAME + 1];
105 /**@brief Block device handle.*/
106 struct ext4_blockdev *bd;
109 /**@brief Block devices.*/
110 static struct ext4_block_devices s_bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
112 /**@brief Mountpoints.*/
113 static struct ext4_mountpoint s_mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
115 int ext4_device_register(struct ext4_blockdev *bd,
116 const char *dev_name)
118 ext4_assert(bd && dev_name);
120 if (strlen(dev_name) > CONFIG_EXT4_MAX_BLOCKDEV_NAME)
123 for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
124 if (!strcmp(s_bdevices[i].name, dev_name))
128 for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
129 if (!s_bdevices[i].bd) {
130 strcpy(s_bdevices[i].name, dev_name);
131 s_bdevices[i].bd = bd;
139 int ext4_device_unregister(const char *dev_name)
141 ext4_assert(dev_name);
143 for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
144 if (strcmp(s_bdevices[i].name, dev_name))
147 memset(&s_bdevices[i], 0, sizeof(s_bdevices[i]));
153 int ext4_device_unregister_all(void)
155 memset(s_bdevices, 0, sizeof(s_bdevices));
160 /****************************************************************************/
162 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
164 if ((name_size == 1) && (name[0] == '.'))
167 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
173 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
175 struct ext4_sblock *sb = &enode->fs->sb;
177 /* Check if node is directory */
178 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
179 *has_children = false;
183 struct ext4_dir_iter it;
184 int rc = ext4_dir_iterator_init(&it, enode, 0);
188 /* Find a non-empty directory entry */
190 while (it.curr != NULL) {
191 if (ext4_dir_en_get_inode(it.curr) != 0) {
193 nsize = ext4_dir_en_get_name_len(sb, it.curr);
194 if (!ext4_is_dots(it.curr->name, nsize)) {
200 rc = ext4_dir_iterator_next(&it);
202 ext4_dir_iterator_fini(&it);
207 rc = ext4_dir_iterator_fini(&it);
211 *has_children = found;
216 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
217 struct ext4_inode_ref *ch, const char *n,
218 uint32_t len, bool rename)
220 /* Check maximum name length */
221 if (len > EXT4_DIRECTORY_FILENAME_LEN)
224 /* Add entry to parent directory */
225 int r = ext4_dir_add_entry(parent, n, len, ch);
229 /* Fill new dir -> add '.' and '..' entries.
230 * Also newly allocated inode should have 0 link count.
233 bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
234 EXT4_INODE_MODE_DIRECTORY);
235 if (is_dir && !rename) {
237 #if CONFIG_DIR_INDEX_ENABLE
238 /* Initialize directory index if supported */
239 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
240 r = ext4_dir_dx_init(ch, parent);
244 ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
249 r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
251 ext4_dir_remove_entry(parent, n, strlen(n));
255 r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
257 ext4_dir_remove_entry(parent, n, strlen(n));
258 ext4_dir_remove_entry(ch, ".", strlen("."));
263 /*New empty directory. Two links (. and ..) */
264 ext4_inode_set_links_cnt(ch->inode, 2);
265 ext4_fs_inode_links_count_inc(parent);
267 parent->dirty = true;
271 * In case we want to rename a directory,
272 * we reset the original '..' pointer.
276 idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
277 struct ext4_dir_search_result res;
279 r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
283 ext4_dir_en_set_inode(res.dentry, parent->index);
284 ext4_trans_set_block_dirty(res.block.buf);
285 r = ext4_dir_destroy_result(ch, &res);
290 #if CONFIG_DIR_INDEX_ENABLE
291 r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
298 ext4_fs_inode_links_count_inc(parent);
299 parent->dirty = true;
302 ext4_fs_inode_links_count_inc(ch);
309 static int ext4_unlink(struct ext4_mountpoint *mp,
310 struct ext4_inode_ref *parent,
311 struct ext4_inode_ref *child, const char *name,
315 int rc = ext4_has_children(&has_children, child);
319 /* Cannot unlink non-empty node */
323 /* Remove entry from parent directory */
324 rc = ext4_dir_remove_entry(parent, name, name_len);
328 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
329 EXT4_INODE_MODE_DIRECTORY);
331 /* If directory - handle links from parent */
333 ext4_fs_inode_links_count_dec(parent);
334 parent->dirty = true;
338 * TODO: Update timestamps of the parent
339 * (when we have wall-clock time).
341 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
342 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
343 * parent->dirty = true;
347 * TODO: Update timestamp for inode.
349 * ext4_inode_set_change_inode_time(child->inode,
352 if (ext4_inode_get_links_cnt(child->inode)) {
353 ext4_fs_inode_links_count_dec(child);
360 /****************************************************************************/
362 int ext4_mount(const char *dev_name, const char *mount_point,
367 struct ext4_bcache *bc;
368 struct ext4_blockdev *bd = 0;
369 struct ext4_mountpoint *mp = 0;
371 ext4_assert(mount_point && dev_name);
373 size_t mp_len = strlen(mount_point);
375 if (mp_len > CONFIG_EXT4_MAX_MP_NAME)
378 if (mount_point[mp_len - 1] != '/')
381 for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
382 if (s_bdevices[i].name) {
383 if (!strcmp(dev_name, s_bdevices[i].name)) {
384 bd = s_bdevices[i].bd;
393 for (size_t i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
394 if (!s_mp[i].mounted) {
395 strcpy(s_mp[i].name, mount_point);
401 if (!strcmp(s_mp[i].name, mount_point))
408 r = ext4_block_init(bd);
412 r = ext4_fs_init(&mp->fs, bd, read_only);
418 bsize = ext4_sb_get_block_size(&mp->fs.sb);
419 ext4_block_set_lb_size(bd, bsize);
422 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE, bsize);
428 if (bsize != bc->itemsize)
431 /*Bind block cache to block device*/
432 r = ext4_block_bind_bcache(bd, bc);
434 ext4_bcache_cleanup(bc);
436 ext4_bcache_fini_dynamic(bc);
445 int ext4_umount(const char *mount_point)
449 struct ext4_mountpoint *mp = 0;
451 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
452 if (!strcmp(s_mp[i].name, mount_point)) {
461 r = ext4_fs_fini(&mp->fs);
467 ext4_bcache_cleanup(mp->fs.bdev->bc);
468 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
470 r = ext4_block_fini(mp->fs.bdev);
472 mp->fs.bdev->fs = NULL;
476 static struct ext4_mountpoint *ext4_get_mount(const char *path)
478 for (size_t i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
480 if (!s_mp[i].mounted)
483 if (!strncmp(s_mp[i].name, path, strlen(s_mp[i].name)))
491 static int __ext4_journal_start(const char *mount_point)
494 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
499 if (mp->fs.read_only)
502 if (ext4_sb_feature_com(&mp->fs.sb,
503 EXT4_FCOM_HAS_JOURNAL)) {
504 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
508 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
510 mp->jbd_fs.dirty = false;
511 jbd_put_fs(&mp->jbd_fs);
514 mp->fs.jbd_fs = &mp->jbd_fs;
515 mp->fs.jbd_journal = &mp->jbd_journal;
522 static int __ext4_journal_stop(const char *mount_point)
525 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
530 if (mp->fs.read_only)
533 if (ext4_sb_feature_com(&mp->fs.sb,
534 EXT4_FCOM_HAS_JOURNAL)) {
535 r = jbd_journal_stop(&mp->jbd_journal);
537 mp->jbd_fs.dirty = false;
538 jbd_put_fs(&mp->jbd_fs);
539 mp->fs.jbd_journal = NULL;
540 mp->fs.jbd_fs = NULL;
544 r = jbd_put_fs(&mp->jbd_fs);
546 mp->fs.jbd_journal = NULL;
547 mp->fs.jbd_fs = NULL;
551 mp->fs.jbd_journal = NULL;
552 mp->fs.jbd_fs = NULL;
559 static int __ext4_recover(const char *mount_point)
561 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
568 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
569 struct jbd_fs *jbd_fs = ext4_calloc(1, sizeof(struct jbd_fs));
575 r = jbd_get_fs(&mp->fs, jbd_fs);
581 r = jbd_recover(jbd_fs);
585 if (r == EOK && !mp->fs.read_only) {
587 uint64_t free_blocks_count = 0;
588 uint32_t free_inodes_count = 0;
589 struct ext4_block_group_ref bg_ref;
591 /* Update superblock's stats */
592 for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) {
593 r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref);
598 ext4_bg_get_free_blocks_count(bg_ref.block_group,
601 ext4_bg_get_free_inodes_count(bg_ref.block_group,
604 ext4_fs_put_block_group_ref(&bg_ref);
606 ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count);
607 ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count);
608 /* We don't need to save the superblock stats immediately. */
617 static int __ext4_trans_start(struct ext4_mountpoint *mp)
621 if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
622 struct jbd_journal *journal = mp->fs.jbd_journal;
623 struct jbd_trans *trans;
624 trans = jbd_journal_new_trans(journal);
629 mp->fs.curr_trans = trans;
636 static int __ext4_trans_stop(struct ext4_mountpoint *mp)
640 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
641 struct jbd_journal *journal = mp->fs.jbd_journal;
642 struct jbd_trans *trans = mp->fs.curr_trans;
643 r = jbd_journal_commit_trans(journal, trans);
644 mp->fs.curr_trans = NULL;
650 static void __ext4_trans_abort(struct ext4_mountpoint *mp)
652 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
653 struct jbd_journal *journal = mp->fs.jbd_journal;
654 struct jbd_trans *trans = mp->fs.curr_trans;
655 jbd_journal_free_trans(journal, trans, true);
656 mp->fs.curr_trans = NULL;
660 int ext4_journal_start(const char *mount_point __unused)
663 #if CONFIG_JOURNALING_ENABLE
664 r = __ext4_journal_start(mount_point);
669 int ext4_journal_stop(const char *mount_point __unused)
672 #if CONFIG_JOURNALING_ENABLE
673 r = __ext4_journal_stop(mount_point);
678 int ext4_recover(const char *mount_point __unused)
681 #if CONFIG_JOURNALING_ENABLE
682 r = __ext4_recover(mount_point);
687 static int ext4_trans_start(struct ext4_mountpoint *mp __unused)
690 #if CONFIG_JOURNALING_ENABLE
691 r = __ext4_trans_start(mp);
696 static int ext4_trans_stop(struct ext4_mountpoint *mp __unused)
699 #if CONFIG_JOURNALING_ENABLE
700 r = __ext4_trans_stop(mp);
705 static void ext4_trans_abort(struct ext4_mountpoint *mp __unused)
707 #if CONFIG_JOURNALING_ENABLE
708 __ext4_trans_abort(mp);
713 int ext4_mount_point_stats(const char *mount_point,
714 struct ext4_mount_stats *stats)
716 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
722 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
723 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
724 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
725 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
726 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
728 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
729 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
730 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
732 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
738 int ext4_mount_setup_locks(const char *mount_point,
739 const struct ext4_lock *locks)
742 struct ext4_mountpoint *mp = 0;
744 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
745 if (!strcmp(s_mp[i].name, mount_point)) {
753 mp->os_locks = locks;
757 /********************************FILE OPERATIONS*****************************/
759 static int ext4_path_check(const char *path, bool *is_goal)
763 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
765 if (path[i] == '/') {
779 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
784 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
785 *file_flags = O_RDONLY;
789 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
790 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
794 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
795 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
799 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
800 !strcmp(flags, "r+b")) {
801 *file_flags = O_RDWR;
805 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
806 !strcmp(flags, "w+b")) {
807 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
811 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
812 !strcmp(flags, "a+b")) {
813 *file_flags = O_RDWR | O_CREAT | O_APPEND;
820 static int ext4_trunc_inode(struct ext4_mountpoint *mp,
821 uint32_t index, uint64_t new_size)
824 struct ext4_fs *const fs = &mp->fs;
825 struct ext4_inode_ref inode_ref;
827 bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
828 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
832 inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
833 ext4_fs_put_inode_ref(&inode_ref);
837 while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
839 inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
841 ext4_trans_start(mp);
842 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
844 ext4_trans_abort(mp);
847 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
849 ext4_fs_put_inode_ref(&inode_ref);
851 r = ext4_fs_put_inode_ref(&inode_ref);
854 ext4_trans_abort(mp);
860 if (inode_size > new_size) {
862 inode_size = new_size;
864 ext4_trans_start(mp);
865 r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
867 ext4_trans_abort(mp);
870 r = ext4_fs_truncate_inode(&inode_ref, inode_size);
872 ext4_fs_put_inode_ref(&inode_ref);
874 r = ext4_fs_put_inode_ref(&inode_ref);
877 ext4_trans_abort(mp);
886 ext4_trans_start(mp);
891 static int ext4_trunc_dir(struct ext4_mountpoint *mp,
892 struct ext4_inode_ref *parent,
893 struct ext4_inode_ref *dir)
896 bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
897 EXT4_INODE_MODE_DIRECTORY);
898 uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
902 #if CONFIG_DIR_INDEX_ENABLE
903 /* Initialize directory index if supported */
904 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
905 r = ext4_dir_dx_init(dir, parent);
909 r = ext4_trunc_inode(mp, dir->index,
910 EXT4_DIR_DX_INIT_BCNT * block_size);
916 r = ext4_trunc_inode(mp, dir->index, block_size);
921 return ext4_fs_truncate_inode(dir, 0);
925 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
926 * any filetype of the target dir entry will be accepted.
928 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
929 int ftype, uint32_t *parent_inode,
932 bool is_goal = false;
933 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
938 struct ext4_mountpoint *mp = ext4_get_mount(path);
939 struct ext4_dir_search_result result;
940 struct ext4_inode_ref ref;
947 struct ext4_fs *const fs = &mp->fs;
948 struct ext4_sblock *const sb = &mp->fs.sb;
950 if (fs->read_only && flags & O_CREAT)
956 path += strlen(mp->name);
959 *name_off = strlen(mp->name);
962 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
967 *parent_inode = ref.index;
969 len = ext4_path_check(path, &is_goal);
972 len = ext4_path_check(path, &is_goal);
974 /*If root open was request.*/
975 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
983 r = ext4_dir_find_entry(&result, &ref, path, len);
986 /*Destroy last result*/
987 ext4_dir_destroy_result(&ref, &result);
991 if (!(f->flags & O_CREAT))
994 /*O_CREAT allows create new entry*/
995 struct ext4_inode_ref child_ref;
996 r = ext4_fs_alloc_inode(fs, &child_ref,
997 is_goal ? ftype : EXT4_DE_DIR);
999 ext4_fs_inode_blocks_init(fs, &child_ref);
1004 /*Link with root dir.*/
1005 r = ext4_link(mp, &ref, &child_ref, path, len, false);
1007 /*Fail. Free new inode.*/
1008 ext4_fs_free_inode(&child_ref);
1009 /*We do not want to write new inode.
1010 But block has to be released.*/
1011 child_ref.dirty = false;
1012 ext4_fs_put_inode_ref(&child_ref);
1016 ext4_fs_put_inode_ref(&child_ref);
1021 *parent_inode = ref.index;
1023 next_inode = ext4_dir_en_get_inode(result.dentry);
1024 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1026 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1027 imode = ext4_fs_correspond_inode_mode(t);
1029 struct ext4_inode_ref child_ref;
1030 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1034 imode = ext4_inode_type(sb, child_ref.inode);
1035 ext4_fs_put_inode_ref(&child_ref);
1038 r = ext4_dir_destroy_result(&ref, &result);
1042 /*If expected file error*/
1043 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
1047 if (ftype != EXT4_DE_UNKNOWN) {
1048 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
1049 if (df && is_goal) {
1055 r = ext4_fs_put_inode_ref(&ref);
1059 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1069 *name_off += len + 1;
1073 ext4_fs_put_inode_ref(&ref);
1079 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
1080 r = ext4_trunc_inode(mp, ref.index, 0);
1082 ext4_fs_put_inode_ref(&ref);
1088 f->fsize = ext4_inode_get_size(sb, ref.inode);
1089 f->inode = ref.index;
1092 if (f->flags & O_APPEND)
1096 return ext4_fs_put_inode_ref(&ref);
1099 /****************************************************************************/
1101 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
1102 bool file_expect, uint32_t *parent_inode,
1108 struct ext4_mountpoint *mp = ext4_get_mount(path);
1110 if (ext4_parse_flags(flags, &iflags) == false)
1113 if (file_expect == true)
1114 filetype = EXT4_DE_REG_FILE;
1116 filetype = EXT4_DE_DIR;
1118 if (iflags & O_CREAT)
1119 ext4_trans_start(mp);
1121 r = ext4_generic_open2(f, path, iflags, filetype, parent_inode,
1124 if (iflags & O_CREAT) {
1126 ext4_trans_stop(mp);
1128 ext4_trans_abort(mp);
1134 static int ext4_create_hardlink(const char *path,
1135 struct ext4_inode_ref *child_ref, bool rename)
1137 bool is_goal = false;
1138 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
1139 uint32_t next_inode;
1143 struct ext4_mountpoint *mp = ext4_get_mount(path);
1144 struct ext4_dir_search_result result;
1145 struct ext4_inode_ref ref;
1150 struct ext4_fs *const fs = &mp->fs;
1151 struct ext4_sblock *const sb = &mp->fs.sb;
1153 /*Skip mount point*/
1154 path += strlen(mp->name);
1157 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
1161 len = ext4_path_check(path, &is_goal);
1164 len = ext4_path_check(path, &is_goal);
1166 /*If root open was request.*/
1167 r = is_goal ? EINVAL : ENOENT;
1171 r = ext4_dir_find_entry(&result, &ref, path, len);
1174 /*Destroy last result*/
1175 ext4_dir_destroy_result(&ref, &result);
1177 if (r != ENOENT || !is_goal)
1180 /*Link with root dir.*/
1181 r = ext4_link(mp, &ref, child_ref, path, len, rename);
1183 } else if (r == EOK && is_goal) {
1184 /*Destroy last result*/
1185 ext4_dir_destroy_result(&ref, &result);
1190 next_inode = result.dentry->inode;
1191 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1193 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1194 inode_mode = ext4_fs_correspond_inode_mode(t);
1196 struct ext4_inode_ref child_ref;
1197 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1201 inode_mode = ext4_inode_type(sb, child_ref.inode);
1202 ext4_fs_put_inode_ref(&child_ref);
1205 r = ext4_dir_destroy_result(&ref, &result);
1209 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1210 r = is_goal ? EEXIST : ENOENT;
1214 r = ext4_fs_put_inode_ref(&ref);
1218 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1229 ext4_fs_put_inode_ref(&ref);
1233 r = ext4_fs_put_inode_ref(&ref);
1237 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1238 struct ext4_inode_ref *parent_ref,
1239 struct ext4_inode_ref *child_ref)
1244 struct ext4_mountpoint *mp = ext4_get_mount(path);
1252 len = ext4_path_check(path, &is_goal);
1254 /* Remove entry from parent directory */
1255 r = ext4_dir_remove_entry(parent_ref, path, len);
1259 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1260 EXT4_INODE_MODE_DIRECTORY)) {
1261 ext4_fs_inode_links_count_dec(parent_ref);
1262 parent_ref->dirty = true;
1268 int ext4_flink(const char *path, const char *hardlink_path)
1273 bool child_loaded = false;
1274 uint32_t parent_inode, child_inode;
1275 struct ext4_mountpoint *mp = ext4_get_mount(path);
1276 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1277 struct ext4_inode_ref child_ref;
1282 if (mp->fs.read_only)
1285 /* Will that happen? Anyway return EINVAL for such case. */
1286 if (mp != target_mp)
1290 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1291 &parent_inode, &name_off);
1297 child_inode = f.inode;
1299 ext4_trans_start(mp);
1301 /*We have file to unlink. Load it.*/
1302 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1306 child_loaded = true;
1308 /* Creating hardlink for directory is not allowed. */
1309 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1310 EXT4_INODE_MODE_DIRECTORY)) {
1315 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1319 ext4_fs_put_inode_ref(&child_ref);
1322 ext4_trans_abort(mp);
1324 ext4_trans_stop(mp);
1331 int ext4_frename(const char *path, const char *new_path)
1336 bool parent_loaded = false, child_loaded = false;
1337 uint32_t parent_inode, child_inode;
1338 struct ext4_mountpoint *mp = ext4_get_mount(path);
1339 struct ext4_inode_ref child_ref, parent_ref;
1344 if (mp->fs.read_only)
1349 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1350 &parent_inode, &name_off);
1356 child_inode = f.inode;
1358 ext4_trans_start(mp);
1361 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1365 parent_loaded = true;
1367 /*We have file to unlink. Load it.*/
1368 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1372 child_loaded = true;
1374 r = ext4_create_hardlink(new_path, &child_ref, true);
1378 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1384 ext4_fs_put_inode_ref(&parent_ref);
1387 ext4_fs_put_inode_ref(&child_ref);
1390 ext4_trans_abort(mp);
1392 ext4_trans_stop(mp);
1399 /****************************************************************************/
1401 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1403 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1412 int ext4_cache_write_back(const char *path, bool on)
1414 struct ext4_mountpoint *mp = ext4_get_mount(path);
1421 ret = ext4_block_cache_write_back(mp->fs.bdev, on);
1426 int ext4_cache_flush(const char *path)
1428 struct ext4_mountpoint *mp = ext4_get_mount(path);
1435 ret = ext4_block_cache_flush(mp->fs.bdev);
1440 int ext4_fremove(const char *path)
1443 uint32_t parent_inode;
1444 uint32_t child_inode;
1449 struct ext4_inode_ref child;
1450 struct ext4_inode_ref parent;
1451 struct ext4_mountpoint *mp = ext4_get_mount(path);
1456 if (mp->fs.read_only)
1460 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1461 &parent_inode, &name_off);
1467 child_inode = f.inode;
1469 ext4_trans_start(mp);
1472 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1474 ext4_trans_abort(mp);
1479 /*We have file to delete. Load it.*/
1480 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child);
1482 ext4_fs_put_inode_ref(&parent);
1483 ext4_trans_abort(mp);
1487 /* We do not allow opening files here. */
1488 if (ext4_inode_type(&mp->fs.sb, child.inode) ==
1489 EXT4_INODE_MODE_DIRECTORY) {
1490 ext4_fs_put_inode_ref(&parent);
1491 ext4_fs_put_inode_ref(&child);
1492 ext4_trans_abort(mp);
1497 /*Link count will be zero, the inode should be freed. */
1498 if (ext4_inode_get_links_cnt(child.inode) == 1) {
1499 ext4_block_cache_write_back(mp->fs.bdev, 1);
1500 r = ext4_trunc_inode(mp, child.index, 0);
1502 ext4_fs_put_inode_ref(&parent);
1503 ext4_fs_put_inode_ref(&child);
1504 ext4_trans_abort(mp);
1508 ext4_block_cache_write_back(mp->fs.bdev, 0);
1514 len = ext4_path_check(path, &is_goal);
1516 /*Unlink from parent*/
1517 r = ext4_unlink(mp, &parent, &child, path, len);
1521 /*Link count is zero, the inode should be freed. */
1522 if (!ext4_inode_get_links_cnt(child.inode)) {
1523 ext4_inode_set_del_time(child.inode, -1L);
1525 r = ext4_fs_free_inode(&child);
1531 ext4_fs_put_inode_ref(&child);
1532 ext4_fs_put_inode_ref(&parent);
1535 ext4_trans_abort(mp);
1537 ext4_trans_stop(mp);
1543 int ext4_fopen(ext4_file *file, const char *path, const char *flags)
1545 struct ext4_mountpoint *mp = ext4_get_mount(path);
1553 ext4_block_cache_write_back(mp->fs.bdev, 1);
1554 r = ext4_generic_open(file, path, flags, true, 0, 0);
1555 ext4_block_cache_write_back(mp->fs.bdev, 0);
1561 int ext4_fopen2(ext4_file *file, const char *path, int flags)
1563 struct ext4_mountpoint *mp = ext4_get_mount(path);
1570 filetype = EXT4_DE_REG_FILE;
1573 ext4_block_cache_write_back(mp->fs.bdev, 1);
1575 if (flags & O_CREAT)
1576 ext4_trans_start(mp);
1578 r = ext4_generic_open2(file, path, flags, filetype, NULL, NULL);
1580 if (flags & O_CREAT) {
1582 ext4_trans_stop(mp);
1584 ext4_trans_abort(mp);
1587 ext4_block_cache_write_back(mp->fs.bdev, 0);
1593 int ext4_fclose(ext4_file *file)
1595 ext4_assert(file && file->mp);
1600 file->fpos = file->fsize = 0;
1605 static int ext4_ftruncate_no_lock(ext4_file *file, uint64_t size)
1607 struct ext4_inode_ref ref;
1611 r = ext4_fs_get_inode_ref(&file->mp->fs, file->inode, &ref);
1613 EXT4_MP_UNLOCK(file->mp);
1618 file->fsize = ext4_inode_get_size(&file->mp->fs.sb, ref.inode);
1619 if (file->fsize <= size) {
1624 /*Start write back cache mode.*/
1625 r = ext4_block_cache_write_back(file->mp->fs.bdev, 1);
1629 r = ext4_trunc_inode(file->mp, ref.index, size);
1634 if (file->fpos > size)
1637 /*Stop write back cache mode*/
1638 ext4_block_cache_write_back(file->mp->fs.bdev, 0);
1644 ext4_fs_put_inode_ref(&ref);
1649 int ext4_ftruncate(ext4_file *f, uint64_t size)
1652 ext4_assert(f && f->mp);
1654 if (f->mp->fs.read_only)
1657 if (f->flags & O_RDONLY)
1660 EXT4_MP_LOCK(f->mp);
1662 ext4_trans_start(f->mp);
1663 r = ext4_ftruncate_no_lock(f, size);
1665 ext4_trans_abort(f->mp);
1667 ext4_trans_stop(f->mp);
1669 EXT4_MP_UNLOCK(f->mp);
1673 int ext4_fread(ext4_file *file, void *buf, size_t size, size_t *rcnt)
1676 uint32_t iblock_idx;
1677 uint32_t iblock_last;
1678 uint32_t block_size;
1680 ext4_fsblk_t fblock;
1681 ext4_fsblk_t fblock_start;
1682 uint32_t fblock_count;
1684 uint8_t *u8_buf = buf;
1686 struct ext4_inode_ref ref;
1688 ext4_assert(file && file->mp);
1690 if (file->flags & O_WRONLY)
1696 EXT4_MP_LOCK(file->mp);
1698 struct ext4_fs *const fs = &file->mp->fs;
1699 struct ext4_sblock *const sb = &file->mp->fs.sb;
1704 r = ext4_fs_get_inode_ref(fs, file->inode, &ref);
1706 EXT4_MP_UNLOCK(file->mp);
1711 file->fsize = ext4_inode_get_size(sb, ref.inode);
1713 block_size = ext4_sb_get_block_size(sb);
1714 size = ((uint64_t)size > (file->fsize - file->fpos))
1715 ? ((size_t)(file->fsize - file->fpos)) : size;
1717 iblock_idx = (uint32_t)((file->fpos) / block_size);
1718 iblock_last = (uint32_t)((file->fpos + size) / block_size);
1719 unalg = (file->fpos) % block_size;
1721 /*If the size of symlink is smaller than 60 bytes*/
1723 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1724 if (softlink && file->fsize < sizeof(ref.inode->blocks)
1725 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1727 char *content = (char *)ref.inode->blocks;
1728 if (file->fpos < file->fsize) {
1730 if (unalg + size > (uint32_t)file->fsize)
1731 len = (uint32_t)file->fsize - unalg;
1732 memcpy(buf, content + unalg, len);
1744 if (size > (block_size - unalg))
1745 len = block_size - unalg;
1747 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1751 /* Do we get an unwritten range? */
1753 uint64_t off = fblock * block_size + unalg;
1754 r = ext4_block_readbytes(file->mp->fs.bdev, off, u8_buf, len);
1760 memset(u8_buf, 0, len);
1775 while (size >= block_size) {
1776 while (iblock_idx < iblock_last) {
1777 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1785 fblock_start = fblock;
1787 if ((fblock_start + fblock_count) != fblock)
1793 r = ext4_blocks_get_direct(file->mp->fs.bdev, u8_buf, fblock_start,
1798 size -= block_size * fblock_count;
1799 u8_buf += block_size * fblock_count;
1800 file->fpos += block_size * fblock_count;
1803 *rcnt += block_size * fblock_count;
1805 fblock_start = fblock;
1811 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1815 off = fblock * block_size;
1816 r = ext4_block_readbytes(file->mp->fs.bdev, off, u8_buf, size);
1827 ext4_fs_put_inode_ref(&ref);
1828 EXT4_MP_UNLOCK(file->mp);
1832 int ext4_fwrite(ext4_file *file, const void *buf, size_t size, size_t *wcnt)
1836 uint32_t iblock_last;
1837 uint32_t ifile_blocks;
1838 uint32_t block_size;
1840 uint32_t fblock_count;
1842 ext4_fsblk_t fblock_start;
1844 struct ext4_inode_ref ref;
1845 const uint8_t *u8_buf = buf;
1848 ext4_assert(file && file->mp);
1850 if (file->mp->fs.read_only)
1853 if (file->flags & O_RDONLY)
1859 EXT4_MP_LOCK(file->mp);
1860 ext4_trans_start(file->mp);
1862 struct ext4_fs *const fs = &file->mp->fs;
1863 struct ext4_sblock *const sb = &file->mp->fs.sb;
1868 r = ext4_fs_get_inode_ref(fs, file->inode, &ref);
1870 ext4_trans_abort(file->mp);
1871 EXT4_MP_UNLOCK(file->mp);
1876 file->fsize = ext4_inode_get_size(sb, ref.inode);
1877 block_size = ext4_sb_get_block_size(sb);
1879 iblock_last = (uint32_t)((file->fpos + size) / block_size);
1880 iblk_idx = (uint32_t)(file->fpos / block_size);
1881 ifile_blocks = (uint32_t)((file->fsize + block_size - 1) / block_size);
1883 unalg = (file->fpos) % block_size;
1888 if (size > (block_size - unalg))
1889 len = block_size - unalg;
1891 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1895 off = fblk * block_size + unalg;
1896 r = ext4_block_writebytes(file->mp->fs.bdev, off, u8_buf, len);
1910 /*Start write back cache mode.*/
1911 r = ext4_block_cache_write_back(file->mp->fs.bdev, 1);
1917 while (size >= block_size) {
1919 while (iblk_idx < iblock_last) {
1920 if (iblk_idx < ifile_blocks) {
1921 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1926 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1929 /* Unable to append more blocks. But
1930 * some block might be allocated already
1938 if (!fblock_start) {
1939 fblock_start = fblk;
1942 if ((fblock_start + fblock_count) != fblk)
1948 r = ext4_blocks_set_direct(file->mp->fs.bdev, u8_buf, fblock_start,
1953 size -= block_size * fblock_count;
1954 u8_buf += block_size * fblock_count;
1955 file->fpos += block_size * fblock_count;
1958 *wcnt += block_size * fblock_count;
1960 fblock_start = fblk;
1964 /*ext4_fs_append_inode_block has failed and no
1965 * more blocks might be written. But node size
1966 * should be updated.*/
1972 /*Stop write back cache mode*/
1973 ext4_block_cache_write_back(file->mp->fs.bdev, 0);
1980 if (iblk_idx < ifile_blocks) {
1981 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1985 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1987 /*Node size sholud be updated.*/
1991 off = fblk * block_size;
1992 r = ext4_block_writebytes(file->mp->fs.bdev, off, u8_buf, size);
2003 if (file->fpos > file->fsize) {
2004 file->fsize = file->fpos;
2005 ext4_inode_set_size(ref.inode, file->fsize);
2010 r = ext4_fs_put_inode_ref(&ref);
2013 ext4_trans_abort(file->mp);
2015 ext4_trans_stop(file->mp);
2017 EXT4_MP_UNLOCK(file->mp);
2021 int ext4_fseek(ext4_file *file, uint64_t offset, uint32_t origin)
2025 if (offset > file->fsize)
2028 file->fpos = offset;
2031 if ((offset + file->fpos) > file->fsize)
2034 file->fpos += offset;
2037 if (offset > file->fsize)
2040 file->fpos = file->fsize - offset;
2046 uint64_t ext4_ftell(ext4_file *file)
2051 uint64_t ext4_fsize(ext4_file *file)
2057 static int ext4_trans_get_inode_ref(const char *path,
2058 struct ext4_mountpoint *mp,
2059 struct ext4_inode_ref *inode_ref)
2064 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2068 ext4_trans_start(mp);
2070 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, inode_ref);
2072 ext4_trans_abort(mp);
2079 static int ext4_trans_put_inode_ref(struct ext4_mountpoint *mp,
2080 struct ext4_inode_ref *inode_ref)
2084 r = ext4_fs_put_inode_ref(inode_ref);
2086 ext4_trans_abort(mp);
2088 ext4_trans_stop(mp);
2094 int ext4_raw_inode_fill(const char *path, uint32_t *ret_ino,
2095 struct ext4_inode *inode)
2099 struct ext4_inode_ref inode_ref;
2100 struct ext4_mountpoint *mp = ext4_get_mount(path);
2107 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2114 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2123 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
2124 ext4_fs_put_inode_ref(&inode_ref);
2130 int ext4_inode_exist(const char *path, int type)
2134 struct ext4_mountpoint *mp = ext4_get_mount(path);
2140 r = ext4_generic_open2(&f, path, O_RDONLY, type, NULL, NULL);
2146 int ext4_mode_set(const char *path, uint32_t mode)
2150 struct ext4_inode_ref inode_ref;
2151 struct ext4_mountpoint *mp = ext4_get_mount(path);
2156 if (mp->fs.read_only)
2161 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2165 orig_mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2166 orig_mode &= ~0xFFF;
2167 orig_mode |= mode & 0xFFF;
2168 ext4_inode_set_mode(&mp->fs.sb, inode_ref.inode, orig_mode);
2170 inode_ref.dirty = true;
2171 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2179 int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid)
2182 struct ext4_inode_ref inode_ref;
2183 struct ext4_mountpoint *mp = ext4_get_mount(path);
2188 if (mp->fs.read_only)
2193 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2197 ext4_inode_set_uid(inode_ref.inode, uid);
2198 ext4_inode_set_gid(inode_ref.inode, gid);
2200 inode_ref.dirty = true;
2201 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2209 int ext4_mode_get(const char *path, uint32_t *mode)
2211 struct ext4_inode_ref inode_ref;
2212 struct ext4_mountpoint *mp = ext4_get_mount(path);
2221 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2225 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2229 *mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode);
2230 r = ext4_fs_put_inode_ref(&inode_ref);
2238 int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid)
2240 struct ext4_inode_ref inode_ref;
2241 struct ext4_mountpoint *mp = ext4_get_mount(path);
2250 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2254 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2258 *uid = ext4_inode_get_uid(inode_ref.inode);
2259 *gid = ext4_inode_get_gid(inode_ref.inode);
2260 r = ext4_fs_put_inode_ref(&inode_ref);
2268 int ext4_atime_set(const char *path, uint32_t atime)
2270 struct ext4_inode_ref inode_ref;
2271 struct ext4_mountpoint *mp = ext4_get_mount(path);
2277 if (mp->fs.read_only)
2282 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2286 ext4_inode_set_access_time(inode_ref.inode, atime);
2287 inode_ref.dirty = true;
2288 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2296 int ext4_mtime_set(const char *path, uint32_t mtime)
2298 struct ext4_inode_ref inode_ref;
2299 struct ext4_mountpoint *mp = ext4_get_mount(path);
2305 if (mp->fs.read_only)
2310 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2314 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2315 inode_ref.dirty = true;
2316 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2324 int ext4_ctime_set(const char *path, uint32_t ctime)
2326 struct ext4_inode_ref inode_ref;
2327 struct ext4_mountpoint *mp = ext4_get_mount(path);
2333 if (mp->fs.read_only)
2338 r = ext4_trans_get_inode_ref(path, mp, &inode_ref);
2342 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2343 inode_ref.dirty = true;
2344 r = ext4_trans_put_inode_ref(mp, &inode_ref);
2352 int ext4_atime_get(const char *path, uint32_t *atime)
2354 struct ext4_inode_ref inode_ref;
2355 struct ext4_mountpoint *mp = ext4_get_mount(path);
2364 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2368 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2372 *atime = ext4_inode_get_access_time(inode_ref.inode);
2373 r = ext4_fs_put_inode_ref(&inode_ref);
2381 int ext4_mtime_get(const char *path, uint32_t *mtime)
2383 struct ext4_inode_ref inode_ref;
2384 struct ext4_mountpoint *mp = ext4_get_mount(path);
2393 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2397 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2401 *mtime = ext4_inode_get_modif_time(inode_ref.inode);
2402 r = ext4_fs_put_inode_ref(&inode_ref);
2410 int ext4_ctime_get(const char *path, uint32_t *ctime)
2412 struct ext4_inode_ref inode_ref;
2413 struct ext4_mountpoint *mp = ext4_get_mount(path);
2422 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2426 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref);
2430 *ctime = ext4_inode_get_change_inode_time(inode_ref.inode);
2431 r = ext4_fs_put_inode_ref(&inode_ref);
2439 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2441 struct ext4_inode_ref ref;
2443 ext4_fsblk_t fblock;
2444 uint32_t block_size;
2447 ext4_assert(f && f->mp);
2452 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2457 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2458 if (size > block_size) {
2462 r = ext4_ftruncate_no_lock(f, 0);
2466 /*Start write back cache mode.*/
2467 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2471 /*If the size of symlink is smaller than 60 bytes*/
2472 if (size < sizeof(ref.inode->blocks)) {
2473 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2474 memcpy(ref.inode->blocks, buf, size);
2475 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2477 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2478 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2482 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2488 /*Stop write back cache mode*/
2489 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2494 ext4_inode_set_size(ref.inode, size);
2502 ext4_fs_put_inode_ref(&ref);
2506 int ext4_fsymlink(const char *target, const char *path)
2508 struct ext4_mountpoint *mp = ext4_get_mount(path);
2516 if (mp->fs.read_only)
2519 filetype = EXT4_DE_SYMLINK;
2522 ext4_block_cache_write_back(mp->fs.bdev, 1);
2523 ext4_trans_start(mp);
2525 r = ext4_generic_open2(&f, path, O_RDWR | O_CREAT, filetype, NULL, NULL);
2527 r = ext4_fsymlink_set(&f, target, strlen(target));
2535 ext4_trans_abort(mp);
2537 ext4_trans_stop(mp);
2539 ext4_block_cache_write_back(mp->fs.bdev, 0);
2544 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2546 struct ext4_mountpoint *mp = ext4_get_mount(path);
2557 filetype = EXT4_DE_SYMLINK;
2560 ext4_block_cache_write_back(mp->fs.bdev, 1);
2561 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2563 r = ext4_fread(&f, buf, bufsize, rcnt);
2570 ext4_block_cache_write_back(mp->fs.bdev, 0);
2575 static int ext4_mknod_set(ext4_file *f, uint32_t dev)
2577 struct ext4_inode_ref ref;
2580 ext4_assert(f && f->mp);
2582 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2586 ext4_inode_set_dev(ref.inode, dev);
2588 ext4_inode_set_size(ref.inode, 0);
2594 r = ext4_fs_put_inode_ref(&ref);
2598 int ext4_mknod(const char *path, int filetype, uint32_t dev)
2600 struct ext4_mountpoint *mp = ext4_get_mount(path);
2607 if (mp->fs.read_only)
2611 * The filetype shouldn't be normal file, directory or
2614 if (filetype == EXT4_DE_UNKNOWN ||
2615 filetype == EXT4_DE_REG_FILE ||
2616 filetype == EXT4_DE_DIR ||
2617 filetype == EXT4_DE_SYMLINK)
2621 * Nor should it be any bogus value.
2623 if (filetype != EXT4_DE_CHRDEV &&
2624 filetype != EXT4_DE_BLKDEV &&
2625 filetype != EXT4_DE_FIFO &&
2626 filetype != EXT4_DE_SOCK)
2630 ext4_block_cache_write_back(mp->fs.bdev, 1);
2631 ext4_trans_start(mp);
2633 r = ext4_generic_open2(&f, path, O_RDWR | O_CREAT, filetype, NULL, NULL);
2635 if (filetype == EXT4_DE_CHRDEV ||
2636 filetype == EXT4_DE_BLKDEV)
2637 r = ext4_mknod_set(&f, dev);
2646 ext4_trans_abort(mp);
2648 ext4_trans_stop(mp);
2650 ext4_block_cache_write_back(mp->fs.bdev, 0);
2655 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2656 const void *data, size_t data_size)
2663 const char *dissected_name = NULL;
2664 size_t dissected_len = 0;
2665 struct ext4_inode_ref inode_ref;
2666 struct ext4_mountpoint *mp = ext4_get_mount(path);
2670 if (mp->fs.read_only)
2673 dissected_name = ext4_extract_xattr_name(name, name_len,
2674 &name_index, &dissected_len,
2680 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2688 ext4_trans_start(mp);
2690 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2694 r = ext4_xattr_set(&inode_ref, name_index, dissected_name,
2695 dissected_len, data, data_size);
2697 ext4_fs_put_inode_ref(&inode_ref);
2700 ext4_trans_abort(mp);
2702 ext4_trans_stop(mp);
2708 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2709 void *buf, size_t buf_size, size_t *data_size)
2716 const char *dissected_name = NULL;
2717 size_t dissected_len = 0;
2718 struct ext4_inode_ref inode_ref;
2719 struct ext4_mountpoint *mp = ext4_get_mount(path);
2723 dissected_name = ext4_extract_xattr_name(name, name_len,
2724 &name_index, &dissected_len,
2730 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2737 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2741 r = ext4_xattr_get(&inode_ref, name_index, dissected_name,
2742 dissected_len, buf, buf_size, data_size);
2744 ext4_fs_put_inode_ref(&inode_ref);
2750 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2755 size_t list_len, list_size = 0;
2756 struct ext4_inode_ref inode_ref;
2757 struct ext4_xattr_list_entry *xattr_list = NULL,
2759 struct ext4_mountpoint *mp = ext4_get_mount(path);
2764 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2770 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2774 r = ext4_xattr_list(&inode_ref, NULL, &list_len);
2775 if (r == EOK && list_len) {
2776 xattr_list = ext4_malloc(list_len);
2778 ext4_fs_put_inode_ref(&inode_ref);
2783 r = ext4_xattr_list(&inode_ref, entry, &list_len);
2785 ext4_fs_put_inode_ref(&inode_ref);
2789 for (;entry;entry = entry->next) {
2791 const char *prefix =
2792 ext4_get_xattr_name_prefix(entry->name_index,
2795 if (prefix_len + entry->name_len + 1 > size) {
2796 ext4_fs_put_inode_ref(&inode_ref);
2803 memcpy(list, prefix, prefix_len);
2805 memcpy(list, entry->name,
2807 list[entry->name_len] = 0;
2808 list += entry->name_len + 1;
2810 size -= prefix_len + entry->name_len + 1;
2813 list_size += prefix_len + entry->name_len + 1;
2816 *ret_size = list_size;
2819 ext4_fs_put_inode_ref(&inode_ref);
2823 ext4_free(xattr_list);
2829 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2836 const char *dissected_name = NULL;
2837 size_t dissected_len = 0;
2838 struct ext4_inode_ref inode_ref;
2839 struct ext4_mountpoint *mp = ext4_get_mount(path);
2843 if (mp->fs.read_only)
2846 dissected_name = ext4_extract_xattr_name(name, name_len,
2847 &name_index, &dissected_len,
2853 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
2861 ext4_trans_start(mp);
2863 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2867 r = ext4_xattr_remove(&inode_ref, name_index, dissected_name,
2870 ext4_fs_put_inode_ref(&inode_ref);
2873 ext4_trans_abort(mp);
2875 ext4_trans_stop(mp);
2882 /*********************************DIRECTORY OPERATION************************/
2884 int ext4_dir_rm(const char *path)
2890 struct ext4_mountpoint *mp = ext4_get_mount(path);
2891 struct ext4_inode_ref act;
2892 struct ext4_inode_ref child;
2893 struct ext4_dir_iter it;
2897 uint32_t inode_current;
2907 if (mp->fs.read_only)
2912 struct ext4_fs *const fs = &mp->fs;
2915 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2922 len = ext4_path_check(path, &is_goal);
2923 inode_current = f.inode;
2925 ext4_block_cache_write_back(mp->fs.bdev, 1);
2929 uint64_t act_curr_pos = 0;
2930 has_children = false;
2933 while (r == EOK && !has_children && !dir_end) {
2935 /*Load directory node.*/
2936 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2941 /*Initialize iterator.*/
2942 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2944 ext4_fs_put_inode_ref(&act);
2953 ext4_trans_start(mp);
2955 /*Get up directory inode when ".." entry*/
2956 if ((it.curr->name_len == 2) &&
2957 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2958 inode_up = ext4_dir_en_get_inode(it.curr);
2961 /*If directory or file entry, but not "." ".." entry*/
2962 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2964 /*Get child inode reference do unlink
2967 uint32_t inode_type;
2968 cinode = ext4_dir_en_get_inode(it.curr);
2969 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2973 /*If directory with no leaf children*/
2974 r = ext4_has_children(&has_children, &child);
2976 ext4_fs_put_inode_ref(&child);
2981 /*Has directory children. Go into this
2983 inode_up = inode_current;
2984 inode_current = cinode;
2986 ext4_fs_put_inode_ref(&child);
2989 inode_type = ext4_inode_type(&mp->fs.sb,
2993 if (inode_type != EXT4_INODE_MODE_DIRECTORY)
2994 r = ext4_trunc_inode(mp, child.index, 0);
2996 r = ext4_trunc_dir(mp, &act, &child);
2999 ext4_fs_put_inode_ref(&child);
3003 /*No children in child directory or file. Just
3005 r = ext4_unlink(f.mp, &act, &child,
3006 (char *)it.curr->name,
3009 ext4_fs_put_inode_ref(&child);
3013 ext4_inode_set_del_time(child.inode, -1L);
3014 ext4_inode_set_links_cnt(child.inode, 0);
3017 r = ext4_fs_free_inode(&child);
3019 ext4_fs_put_inode_ref(&child);
3023 r = ext4_fs_put_inode_ref(&child);
3029 r = ext4_dir_iterator_next(&it);
3033 act_curr_pos = it.curr_off;
3035 ext4_dir_iterator_fini(&it);
3037 r = ext4_fs_put_inode_ref(&act);
3039 ext4_fs_put_inode_ref(&act);
3042 ext4_trans_abort(mp);
3044 ext4_trans_stop(mp);
3048 /*Directory iterator reached last entry*/
3051 inode_current = inode_up;
3061 if (r == EOK && !depth) {
3063 struct ext4_inode_ref parent;
3064 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
3068 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
3071 ext4_fs_put_inode_ref(&act);
3075 ext4_trans_start(mp);
3077 /* In this place all directories should be
3079 * Last unlink from root of current directory*/
3080 r = ext4_unlink(f.mp, &parent, &act,
3083 ext4_fs_put_inode_ref(&parent);
3084 ext4_fs_put_inode_ref(&act);
3088 if (ext4_inode_get_links_cnt(act.inode) == 2) {
3089 ext4_inode_set_del_time(act.inode, -1L);
3090 ext4_inode_set_links_cnt(act.inode, 0);
3093 r = ext4_trunc_dir(mp, &parent, &act);
3095 ext4_fs_put_inode_ref(&parent);
3096 ext4_fs_put_inode_ref(&act);
3100 r = ext4_fs_free_inode(&act);
3102 ext4_fs_put_inode_ref(&parent);
3103 ext4_fs_put_inode_ref(&act);
3108 r = ext4_fs_put_inode_ref(&parent);
3112 r = ext4_fs_put_inode_ref(&act);
3115 ext4_trans_abort(mp);
3117 ext4_trans_stop(mp);
3120 ext4_block_cache_write_back(mp->fs.bdev, 0);
3126 int ext4_dir_mv(const char *path, const char *new_path)
3128 return ext4_frename(path, new_path);
3131 int ext4_dir_mk(const char *path)
3135 struct ext4_mountpoint *mp = ext4_get_mount(path);
3140 if (mp->fs.read_only)
3146 r = ext4_generic_open(&f, path, "r", false, 0, 0);
3150 /*Create new directory.*/
3151 r = ext4_generic_open(&f, path, "w", false, 0, 0);
3158 int ext4_dir_open(ext4_dir *dir, const char *path)
3160 struct ext4_mountpoint *mp = ext4_get_mount(path);
3167 r = ext4_generic_open(&dir->f, path, "r", false, 0, 0);
3173 int ext4_dir_close(ext4_dir *dir)
3175 return ext4_fclose(&dir->f);
3178 const ext4_direntry *ext4_dir_entry_next(ext4_dir *dir)
3180 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
3183 uint16_t name_length;
3184 ext4_direntry *de = 0;
3185 struct ext4_inode_ref dir_inode;
3186 struct ext4_dir_iter it;
3188 EXT4_MP_LOCK(dir->f.mp);
3190 if (dir->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
3191 EXT4_MP_UNLOCK(dir->f.mp);
3195 r = ext4_fs_get_inode_ref(&dir->f.mp->fs, dir->f.inode, &dir_inode);
3200 r = ext4_dir_iterator_init(&it, &dir_inode, dir->next_off);
3202 ext4_fs_put_inode_ref(&dir_inode);
3206 memset(&dir->de.name, 0, sizeof(dir->de.name));
3207 name_length = ext4_dir_en_get_name_len(&dir->f.mp->fs.sb,
3209 memcpy(&dir->de.name, it.curr->name, name_length);
3211 /* Directly copying the content isn't safe for Big-endian targets*/
3212 dir->de.inode = ext4_dir_en_get_inode(it.curr);
3213 dir->de.entry_length = ext4_dir_en_get_entry_len(it.curr);
3214 dir->de.name_length = name_length;
3215 dir->de.inode_type = ext4_dir_en_get_inode_type(&dir->f.mp->fs.sb,
3220 ext4_dir_iterator_next(&it);
3222 dir->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
3224 ext4_dir_iterator_fini(&it);
3225 ext4_fs_put_inode_ref(&dir_inode);
3228 EXT4_MP_UNLOCK(dir->f.mp);
3232 void ext4_dir_entry_rewind(ext4_dir *dir)