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"
39 #include "ext4_blockdev.h"
40 #include "ext4_types.h"
41 #include "ext4_debug.h"
42 #include "ext4_errno.h"
45 #include "ext4_inode.h"
46 #include "ext4_super.h"
47 #include "ext4_dir_idx.h"
48 #include "ext4_xattr.h"
49 #include "ext4_journal.h"
55 /**@brief Mount point OS dependent lock*/
56 #define EXT4_MP_LOCK(_m) \
59 (_m)->os_locks->lock(); \
62 /**@brief Mount point OS dependent unlock*/
63 #define EXT4_MP_UNLOCK(_m) \
66 (_m)->os_locks->unlock(); \
69 /**@brief Mount point descriptor.*/
70 struct ext4_mountpoint {
72 /**@brief Mount done flag.*/
75 /**@brief Mount point name (@ref ext4_mount)*/
78 /**@brief OS dependent lock/unlock functions.*/
79 const struct ext4_lock *os_locks;
81 /**@brief Ext4 filesystem internals.*/
84 /**@brief Dynamic allocation cache flag.*/
88 struct jbd_journal jbd_journal;
91 /**@brief Block devices descriptor.*/
92 struct _ext4_devices {
94 /**@brief Block device name (@ref ext4_device_register)*/
97 /**@brief Block device handle.*/
98 struct ext4_blockdev *bd;
100 /**@brief Block cache handle.*/
101 struct ext4_bcache *bc;
104 /**@brief Block devices.*/
105 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
107 /**@brief Mountpoints.*/
108 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
110 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
111 const char *dev_name)
114 ext4_assert(bd && dev_name);
116 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
117 if (!_bdevices[i].bd) {
118 strcpy(_bdevices[i].name, dev_name);
119 _bdevices[i].bd = bd;
120 _bdevices[i].bc = bc;
124 if (!strcmp(_bdevices[i].name, dev_name))
130 /****************************************************************************/
132 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
134 if ((name_size == 1) && (name[0] == '.'))
137 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
143 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
145 struct ext4_sblock *sb = &enode->fs->sb;
147 /* Check if node is directory */
148 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
149 *has_children = false;
153 struct ext4_dir_iter it;
154 int rc = ext4_dir_iterator_init(&it, enode, 0);
158 /* Find a non-empty directory entry */
160 while (it.curr != NULL) {
161 if (ext4_dir_en_get_inode(it.curr) != 0) {
163 nsize = ext4_dir_en_get_name_len(sb, it.curr);
164 if (!ext4_is_dots(it.curr->name, nsize)) {
170 rc = ext4_dir_iterator_next(&it);
172 ext4_dir_iterator_fini(&it);
177 rc = ext4_dir_iterator_fini(&it);
181 *has_children = found;
186 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
187 struct ext4_inode_ref *ch, const char *n,
188 uint32_t len, bool rename)
190 /* Check maximum name length */
191 if (len > EXT4_DIRECTORY_FILENAME_LEN)
194 /* Add entry to parent directory */
195 int r = ext4_dir_add_entry(parent, n, len, ch);
199 /* Fill new dir -> add '.' and '..' entries.
200 * Also newly allocated inode should have 0 link count.
203 bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
204 EXT4_INODE_MODE_DIRECTORY);
205 if (is_dir && !rename) {
207 #if CONFIG_DIR_INDEX_ENABLE
208 /* Initialize directory index if supported */
209 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
210 r = ext4_dir_dx_init(ch, parent);
214 ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
219 r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
221 ext4_dir_remove_entry(parent, n, strlen(n));
225 r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
227 ext4_dir_remove_entry(parent, n, strlen(n));
228 ext4_dir_remove_entry(ch, ".", strlen("."));
233 /*New empty directory. Two links (. and ..) */
234 ext4_inode_set_links_cnt(ch->inode, 2);
235 ext4_fs_inode_links_count_inc(parent);
237 parent->dirty = true;
241 * In case we want to rename a directory,
242 * we reset the original '..' pointer.
246 idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
247 struct ext4_dir_search_result res;
249 r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
253 ext4_dir_en_set_inode(res.dentry, parent->index);
254 ext4_trans_set_block_dirty(res.block.buf);
255 r = ext4_dir_destroy_result(ch, &res);
260 #if CONFIG_DIR_INDEX_ENABLE
261 r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
268 ext4_fs_inode_links_count_inc(parent);
269 parent->dirty = true;
272 ext4_fs_inode_links_count_inc(ch);
279 static int ext4_unlink(struct ext4_mountpoint *mp,
280 struct ext4_inode_ref *parent,
281 struct ext4_inode_ref *child, const char *name,
285 int rc = ext4_has_children(&has_children, child);
289 /* Cannot unlink non-empty node */
293 /* Remove entry from parent directory */
294 rc = ext4_dir_remove_entry(parent, name, name_len);
298 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
299 EXT4_INODE_MODE_DIRECTORY);
301 /* If directory - handle links from parent */
303 ext4_fs_inode_links_count_dec(parent);
304 parent->dirty = true;
308 * TODO: Update timestamps of the parent
309 * (when we have wall-clock time).
311 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
312 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
313 * parent->dirty = true;
317 * TODO: Update timestamp for inode.
319 * ext4_inode_set_change_inode_time(child->inode,
322 if (ext4_inode_get_links_cnt(child->inode)) {
323 ext4_fs_inode_links_count_dec(child);
330 /****************************************************************************/
332 int ext4_mount(const char *dev_name, const char *mount_point)
334 ext4_assert(mount_point && dev_name);
339 struct ext4_blockdev *bd = 0;
340 struct ext4_bcache *bc = 0;
341 struct ext4_mountpoint *mp = 0;
343 if (mount_point[strlen(mount_point) - 1] != '/')
346 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
347 if (_bdevices[i].name) {
348 if (!strcmp(dev_name, _bdevices[i].name)) {
349 bd = _bdevices[i].bd;
350 bc = _bdevices[i].bc;
359 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
360 if (!_mp[i].mounted) {
361 strcpy(_mp[i].name, mount_point);
367 if (!strcmp(_mp[i].name, mount_point))
374 r = ext4_block_init(bd);
378 r = ext4_fs_init(&mp->fs, bd);
384 bsize = ext4_sb_get_block_size(&mp->fs.sb);
385 ext4_block_set_lb_size(bd, bsize);
387 mp->cache_dynamic = 0;
390 /*Automatic block cache alloc.*/
391 mp->cache_dynamic = 1;
392 bc = malloc(sizeof(struct ext4_bcache));
394 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
403 if (bsize != bc->itemsize)
406 /*Bind block cache to block device*/
407 r = ext4_block_bind_bcache(bd, bc);
409 ext4_bcache_cleanup(bc);
411 if (mp->cache_dynamic) {
412 ext4_bcache_fini_dynamic(bc);
423 int ext4_umount(const char *mount_point)
427 struct ext4_mountpoint *mp = 0;
429 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
430 if (!strcmp(_mp[i].name, mount_point)) {
439 r = ext4_fs_fini(&mp->fs);
445 ext4_bcache_cleanup(mp->fs.bdev->bc);
446 if (mp->cache_dynamic) {
447 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
448 free(mp->fs.bdev->bc);
450 r = ext4_block_fini(mp->fs.bdev);
452 mp->fs.bdev->fs = NULL;
456 static struct ext4_mountpoint *ext4_get_mount(const char *path)
459 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
464 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
470 int ext4_journal_start(const char *mount_point)
473 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
477 if (ext4_sb_feature_com(&mp->fs.sb,
478 EXT4_FCOM_HAS_JOURNAL)) {
479 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
483 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
485 mp->jbd_fs.dirty = false;
486 jbd_put_fs(&mp->jbd_fs);
489 mp->fs.jbd_fs = &mp->jbd_fs;
490 mp->fs.jbd_journal = &mp->jbd_journal;
496 int ext4_journal_stop(const char *mount_point)
499 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
503 if (ext4_sb_feature_com(&mp->fs.sb,
504 EXT4_FCOM_HAS_JOURNAL)) {
505 r = jbd_journal_stop(&mp->jbd_journal);
507 mp->jbd_fs.dirty = false;
508 jbd_put_fs(&mp->jbd_fs);
509 mp->fs.jbd_journal = NULL;
510 mp->fs.jbd_fs = NULL;
514 r = jbd_put_fs(&mp->jbd_fs);
516 mp->fs.jbd_journal = NULL;
517 mp->fs.jbd_fs = NULL;
521 mp->fs.jbd_journal = NULL;
522 mp->fs.jbd_fs = NULL;
528 int ext4_recover(const char *mount_point)
530 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
536 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
537 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
544 r = jbd_get_fs(&mp->fs, jbd_fs);
550 r = jbd_recover(jbd_fs);
561 static int ext4_trans_start(struct ext4_mountpoint *mp)
564 if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
565 struct jbd_journal *journal = mp->fs.jbd_journal;
566 struct jbd_trans *trans;
567 trans = jbd_journal_new_trans(journal);
572 mp->fs.curr_trans = trans;
578 static int ext4_trans_stop(struct ext4_mountpoint *mp)
581 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
582 struct jbd_journal *journal = mp->fs.jbd_journal;
583 struct jbd_trans *trans = mp->fs.curr_trans;
584 r = jbd_journal_commit_trans(journal, trans);
585 mp->fs.curr_trans = NULL;
590 static void ext4_trans_abort(struct ext4_mountpoint *mp)
592 if (mp->fs.jbd_journal && mp->fs.curr_trans) {
593 struct jbd_journal *journal = mp->fs.jbd_journal;
594 struct jbd_trans *trans = mp->fs.curr_trans;
595 jbd_journal_free_trans(journal, trans, true);
596 mp->fs.curr_trans = NULL;
600 int ext4_mount_point_stats(const char *mount_point,
601 struct ext4_mount_stats *stats)
603 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
609 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
610 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
611 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
612 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
613 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
615 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
616 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
617 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
619 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
625 int ext4_mount_setup_locks(const char *mount_point,
626 const struct ext4_lock *locks)
629 struct ext4_mountpoint *mp = 0;
631 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
632 if (!strcmp(_mp[i].name, mount_point)) {
640 mp->os_locks = locks;
644 /********************************FILE OPERATIONS*****************************/
646 static int ext4_path_check(const char *path, bool *is_goal)
650 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
652 if (path[i] == '/') {
666 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
671 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
672 *file_flags = O_RDONLY;
676 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
677 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
681 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
682 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
686 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
687 !strcmp(flags, "r+b")) {
688 *file_flags = O_RDWR;
692 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
693 !strcmp(flags, "w+b")) {
694 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
698 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
699 !strcmp(flags, "a+b")) {
700 *file_flags = O_RDWR | O_CREAT | O_APPEND;
708 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
709 * any filetype of the target dir entry will be accepted.
711 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
712 int ftype, uint32_t *parent_inode,
715 bool is_goal = false;
716 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
721 struct ext4_mountpoint *mp = ext4_get_mount(path);
722 struct ext4_dir_search_result result;
723 struct ext4_inode_ref ref;
730 struct ext4_fs *const fs = &mp->fs;
731 struct ext4_sblock *const sb = &mp->fs.sb;
736 path += strlen(mp->name);
739 *name_off = strlen(mp->name);
742 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
747 *parent_inode = ref.index;
749 len = ext4_path_check(path, &is_goal);
752 len = ext4_path_check(path, &is_goal);
754 /*If root open was request.*/
755 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
763 r = ext4_dir_find_entry(&result, &ref, path, len);
766 /*Destroy last result*/
767 ext4_dir_destroy_result(&ref, &result);
771 if (!(f->flags & O_CREAT))
774 /*O_CREAT allows create new entry*/
775 struct ext4_inode_ref child_ref;
776 r = ext4_fs_alloc_inode(fs, &child_ref,
777 is_goal ? ftype : EXT4_DE_DIR);
782 /*Link with root dir.*/
783 r = ext4_link(mp, &ref, &child_ref, path, len, false);
785 /*Fail. Free new inode.*/
786 ext4_fs_free_inode(&child_ref);
787 /*We do not want to write new inode.
788 But block has to be released.*/
789 child_ref.dirty = false;
790 ext4_fs_put_inode_ref(&child_ref);
794 ext4_fs_put_inode_ref(&child_ref);
799 *parent_inode = ref.index;
801 next_inode = ext4_dir_en_get_inode(result.dentry);
802 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
804 t = ext4_dir_en_get_inode_type(sb, result.dentry);
805 imode = ext4_fs_correspond_inode_mode(t);
807 struct ext4_inode_ref child_ref;
808 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
812 imode = ext4_inode_type(sb, child_ref.inode);
813 ext4_fs_put_inode_ref(&child_ref);
816 r = ext4_dir_destroy_result(&ref, &result);
820 /*If expected file error*/
821 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
825 if (ftype != EXT4_DE_UNKNOWN) {
826 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
833 r = ext4_fs_put_inode_ref(&ref);
837 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
847 *name_off += len + 1;
851 ext4_fs_put_inode_ref(&ref);
857 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
858 r = ext4_fs_truncate_inode(&ref, 0);
860 ext4_fs_put_inode_ref(&ref);
866 f->fsize = ext4_inode_get_size(sb, ref.inode);
867 f->inode = ref.index;
870 if (f->flags & O_APPEND)
875 r = ext4_fs_put_inode_ref(&ref);
879 /****************************************************************************/
881 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
882 bool file_expect, uint32_t *parent_inode,
887 if (ext4_parse_flags(flags, &iflags) == false)
890 if (file_expect == true)
891 filetype = EXT4_DE_REG_FILE;
893 filetype = EXT4_DE_DIR;
895 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
899 static int ext4_create_hardlink(const char *path,
900 struct ext4_inode_ref *child_ref, bool rename)
902 bool is_goal = false;
903 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
908 struct ext4_mountpoint *mp = ext4_get_mount(path);
909 struct ext4_dir_search_result result;
910 struct ext4_inode_ref ref;
915 struct ext4_fs *const fs = &mp->fs;
916 struct ext4_sblock *const sb = &mp->fs.sb;
919 path += strlen(mp->name);
922 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
926 len = ext4_path_check(path, &is_goal);
929 len = ext4_path_check(path, &is_goal);
931 /*If root open was request.*/
932 r = is_goal ? EINVAL : ENOENT;
936 r = ext4_dir_find_entry(&result, &ref, path, len);
939 /*Destroy last result*/
940 ext4_dir_destroy_result(&ref, &result);
942 if (r != ENOENT || !is_goal)
945 /*Link with root dir.*/
946 r = ext4_link(mp, &ref, child_ref, path, len, rename);
948 } else if (r == EOK && is_goal) {
949 /*Destroy last result*/
950 ext4_dir_destroy_result(&ref, &result);
955 next_inode = result.dentry->inode;
956 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
958 t = ext4_dir_en_get_inode_type(sb, result.dentry);
959 inode_mode = ext4_fs_correspond_inode_mode(t);
961 struct ext4_inode_ref child_ref;
962 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
966 inode_mode = ext4_inode_type(sb, child_ref.inode);
967 ext4_fs_put_inode_ref(&child_ref);
970 r = ext4_dir_destroy_result(&ref, &result);
974 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
975 r = is_goal ? EEXIST : ENOENT;
979 r = ext4_fs_put_inode_ref(&ref);
983 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
994 ext4_fs_put_inode_ref(&ref);
998 r = ext4_fs_put_inode_ref(&ref);
1002 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1003 struct ext4_inode_ref *parent_ref,
1004 struct ext4_inode_ref *child_ref)
1009 struct ext4_mountpoint *mp = ext4_get_mount(path);
1017 len = ext4_path_check(path, &is_goal);
1019 /* Remove entry from parent directory */
1020 r = ext4_dir_remove_entry(parent_ref, path, len);
1024 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1025 EXT4_INODE_MODE_DIRECTORY)) {
1026 ext4_fs_inode_links_count_dec(parent_ref);
1027 parent_ref->dirty = true;
1033 int ext4_flink(const char *path, const char *hardlink_path)
1038 bool child_loaded = false;
1039 uint32_t parent_inode, child_inode;
1040 struct ext4_mountpoint *mp = ext4_get_mount(path);
1041 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1042 struct ext4_inode_ref child_ref;
1047 /* Will that happen? Anyway return EINVAL for such case. */
1048 if (mp != target_mp)
1052 ext4_trans_start(mp);
1054 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1055 &parent_inode, &name_off);
1059 child_inode = f.inode;
1062 /*We have file to unlink. Load it.*/
1063 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1067 child_loaded = true;
1069 /* Creating hardlink for directory is not allowed. */
1070 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1071 EXT4_INODE_MODE_DIRECTORY)) {
1076 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1080 ext4_fs_put_inode_ref(&child_ref);
1083 ext4_trans_abort(mp);
1085 ext4_trans_stop(mp);
1092 int ext4_frename(const char *path, const char *new_path)
1097 bool parent_loaded = false, child_loaded = false;
1098 uint32_t parent_inode, child_inode;
1099 struct ext4_mountpoint *mp = ext4_get_mount(path);
1100 struct ext4_inode_ref child_ref, parent_ref;
1106 ext4_trans_start(mp);
1108 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1109 &parent_inode, &name_off);
1113 child_inode = f.inode;
1117 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1121 parent_loaded = true;
1123 /*We have file to unlink. Load it.*/
1124 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1128 child_loaded = true;
1130 r = ext4_create_hardlink(new_path, &child_ref, true);
1134 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1140 ext4_fs_put_inode_ref(&parent_ref);
1143 ext4_fs_put_inode_ref(&child_ref);
1146 ext4_trans_abort(mp);
1148 ext4_trans_stop(mp);
1155 /****************************************************************************/
1157 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1159 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1168 int ext4_cache_write_back(const char *path, bool on)
1170 struct ext4_mountpoint *mp = ext4_get_mount(path);
1176 ext4_block_cache_write_back(mp->fs.bdev, on);
1181 int ext4_fremove(const char *path)
1184 uint32_t parent_inode;
1189 struct ext4_inode_ref child;
1190 struct ext4_inode_ref parent;
1191 struct ext4_mountpoint *mp = ext4_get_mount(path);
1197 ext4_trans_start(mp);
1199 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1200 &parent_inode, &name_off);
1202 ext4_trans_abort(mp);
1208 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1210 ext4_trans_abort(mp);
1215 /*We have file to delete. Load it.*/
1216 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1218 ext4_fs_put_inode_ref(&parent);
1219 ext4_trans_abort(mp);
1227 len = ext4_path_check(path, &is_goal);
1229 /*Unlink from parent*/
1230 r = ext4_unlink(mp, &parent, &child, path, len);
1234 /*Link count is zero, the inode should be freed. */
1235 if (!ext4_inode_get_links_cnt(child.inode)) {
1236 ext4_inode_set_del_time(child.inode, -1L);
1238 ext4_block_cache_write_back(mp->fs.bdev, 1);
1239 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1240 r = ext4_fs_truncate_inode(&child, 0);
1241 ext4_block_cache_write_back(mp->fs.bdev, 0);
1246 r = ext4_fs_free_inode(&child);
1252 ext4_fs_put_inode_ref(&child);
1253 ext4_fs_put_inode_ref(&parent);
1256 ext4_trans_abort(mp);
1258 ext4_trans_stop(mp);
1264 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1265 struct ext4_inode *inode)
1269 struct ext4_inode_ref inode_ref;
1270 struct ext4_mountpoint *mp = ext4_get_mount(path);
1278 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1288 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1294 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1295 ext4_fs_put_inode_ref(&inode_ref);
1304 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1306 struct ext4_mountpoint *mp = ext4_get_mount(path);
1313 ext4_trans_start(mp);
1315 ext4_block_cache_write_back(mp->fs.bdev, 1);
1316 r = ext4_generic_open(f, path, flags, true, 0, 0);
1317 ext4_block_cache_write_back(mp->fs.bdev, 0);
1320 ext4_trans_abort(mp);
1322 ext4_trans_stop(mp);
1328 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1330 struct ext4_mountpoint *mp = ext4_get_mount(path);
1337 filetype = EXT4_DE_REG_FILE;
1340 ext4_trans_start(mp);
1342 ext4_block_cache_write_back(mp->fs.bdev, 1);
1343 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1344 ext4_block_cache_write_back(mp->fs.bdev, 0);
1347 ext4_trans_abort(mp);
1349 ext4_trans_stop(mp);
1355 int ext4_fclose(ext4_file *f)
1357 ext4_assert(f && f->mp);
1362 f->fpos = f->fsize = 0;
1367 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1369 struct ext4_inode_ref ref;
1373 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1375 EXT4_MP_UNLOCK(f->mp);
1380 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1381 if (f->fsize <= size) {
1386 /*Start write back cache mode.*/
1387 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1391 r = ext4_fs_truncate_inode(&ref, size);
1399 /*Stop write back cache mode*/
1400 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1406 ext4_fs_put_inode_ref(&ref);
1411 int ext4_ftruncate(ext4_file *f, uint64_t size)
1414 ext4_assert(f && f->mp);
1416 if (f->flags & O_RDONLY)
1419 EXT4_MP_LOCK(f->mp);
1421 ext4_trans_start(f->mp);
1422 r = ext4_ftruncate_no_lock(f, size);
1424 ext4_trans_abort(f->mp);
1426 ext4_trans_stop(f->mp);
1428 EXT4_MP_UNLOCK(f->mp);
1432 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1435 uint32_t iblock_idx;
1436 uint32_t iblock_last;
1437 uint32_t block_size;
1439 ext4_fsblk_t fblock;
1440 ext4_fsblk_t fblock_start;
1441 uint32_t fblock_count;
1443 uint8_t *u8_buf = buf;
1445 struct ext4_inode_ref ref;
1447 ext4_assert(f && f->mp);
1449 if (f->flags & O_WRONLY)
1455 EXT4_MP_LOCK(f->mp);
1457 struct ext4_fs *const fs = &f->mp->fs;
1458 struct ext4_sblock *const sb = &f->mp->fs.sb;
1463 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1465 EXT4_MP_UNLOCK(f->mp);
1470 f->fsize = ext4_inode_get_size(sb, ref.inode);
1472 block_size = ext4_sb_get_block_size(sb);
1473 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1475 iblock_idx = (f->fpos) / block_size;
1476 iblock_last = (f->fpos + size) / block_size;
1477 unalg = (f->fpos) % block_size;
1479 /*If the size of symlink is smaller than 60 bytes*/
1481 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1482 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1483 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1485 char *content = (char *)ref.inode->blocks;
1486 if (f->fpos < f->fsize) {
1488 if (unalg + size > f->fsize)
1489 len = f->fsize - unalg;
1490 memcpy(buf, content + unalg, len);
1502 if (size > (block_size - unalg))
1503 len = block_size - unalg;
1505 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1509 /* Do we get an unwritten range? */
1511 uint64_t off = fblock * block_size + unalg;
1512 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1518 memset(u8_buf, 0, len);
1533 while (size >= block_size) {
1534 while (iblock_idx < iblock_last) {
1535 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1543 fblock_start = fblock;
1545 if ((fblock_start + fblock_count) != fblock)
1551 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1556 size -= block_size * fblock_count;
1557 u8_buf += block_size * fblock_count;
1558 f->fpos += block_size * fblock_count;
1561 *rcnt += block_size * fblock_count;
1563 fblock_start = fblock;
1569 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1573 off = fblock * block_size;
1574 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1585 ext4_fs_put_inode_ref(&ref);
1586 EXT4_MP_UNLOCK(f->mp);
1590 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1594 uint32_t iblock_last;
1595 uint32_t ifile_blocks;
1596 uint32_t block_size;
1598 uint32_t fblock_count;
1600 ext4_fsblk_t fblock_start;
1602 struct ext4_inode_ref ref;
1603 const uint8_t *u8_buf = buf;
1606 ext4_assert(f && f->mp);
1608 if (f->flags & O_RDONLY)
1614 EXT4_MP_LOCK(f->mp);
1615 ext4_trans_start(f->mp);
1617 struct ext4_fs *const fs = &f->mp->fs;
1618 struct ext4_sblock *const sb = &f->mp->fs.sb;
1623 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1625 ext4_trans_abort(f->mp);
1626 EXT4_MP_UNLOCK(f->mp);
1631 f->fsize = ext4_inode_get_size(sb, ref.inode);
1632 block_size = ext4_sb_get_block_size(sb);
1634 iblock_last = (f->fpos + size) / block_size;
1635 iblk_idx = (f->fpos) / block_size;
1636 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1638 unalg = (f->fpos) % block_size;
1643 if (size > (block_size - unalg))
1644 len = block_size - unalg;
1646 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1650 off = fblk * block_size + unalg;
1651 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1665 /*Start write back cache mode.*/
1666 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1672 while (size >= block_size) {
1674 while (iblk_idx < iblock_last) {
1675 if (iblk_idx < ifile_blocks) {
1676 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1681 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1684 /* Unable to append more blocks. But
1685 * some block might be allocated already
1693 if (!fblock_start) {
1694 fblock_start = fblk;
1697 if ((fblock_start + fblock_count) != fblk)
1703 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1708 size -= block_size * fblock_count;
1709 u8_buf += block_size * fblock_count;
1710 f->fpos += block_size * fblock_count;
1713 *wcnt += block_size * fblock_count;
1715 fblock_start = fblk;
1719 /*ext4_fs_append_inode_block has failed and no
1720 * more blocks might be written. But node size
1721 * should be updated.*/
1727 /*Stop write back cache mode*/
1728 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1735 if (iblk_idx < ifile_blocks) {
1736 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1740 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1742 /*Node size sholud be updated.*/
1746 off = fblk * block_size;
1747 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1758 if (f->fpos > f->fsize) {
1760 ext4_inode_set_size(ref.inode, f->fsize);
1765 r = ext4_fs_put_inode_ref(&ref);
1768 ext4_trans_abort(f->mp);
1770 ext4_trans_stop(f->mp);
1772 EXT4_MP_UNLOCK(f->mp);
1776 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1780 if (offset > f->fsize)
1786 if ((offset + f->fpos) > f->fsize)
1792 if (offset > f->fsize)
1795 f->fpos = f->fsize - offset;
1801 uint64_t ext4_ftell(ext4_file *f)
1806 uint64_t ext4_fsize(ext4_file *f)
1811 int ext4_chmod(const char *path, uint32_t mode)
1816 struct ext4_sblock *sb;
1817 struct ext4_inode_ref inode_ref;
1818 struct ext4_mountpoint *mp = ext4_get_mount(path);
1824 ext4_trans_start(mp);
1826 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1828 ext4_trans_abort(mp);
1835 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1837 ext4_trans_abort(mp);
1842 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1843 inode_ref.dirty = true;
1845 r = ext4_fs_put_inode_ref(&inode_ref);
1847 ext4_trans_abort(mp);
1849 ext4_trans_stop(mp);
1855 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1860 struct ext4_inode_ref inode_ref;
1861 struct ext4_mountpoint *mp = ext4_get_mount(path);
1867 ext4_trans_start(mp);
1869 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1871 ext4_trans_abort(mp);
1877 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1879 ext4_trans_abort(mp);
1884 ext4_inode_set_uid(inode_ref.inode, uid);
1885 ext4_inode_set_gid(inode_ref.inode, gid);
1886 inode_ref.dirty = true;
1888 r = ext4_fs_put_inode_ref(&inode_ref);
1890 ext4_trans_abort(mp);
1892 ext4_trans_stop(mp);
1898 int ext4_file_set_atime(const char *path, uint32_t atime)
1903 struct ext4_inode_ref inode_ref;
1904 struct ext4_mountpoint *mp = ext4_get_mount(path);
1910 ext4_trans_start(mp);
1912 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1914 ext4_trans_abort(mp);
1920 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1922 ext4_trans_abort(mp);
1927 ext4_inode_set_access_time(inode_ref.inode, atime);
1928 inode_ref.dirty = true;
1930 r = ext4_fs_put_inode_ref(&inode_ref);
1932 ext4_trans_abort(mp);
1934 ext4_trans_stop(mp);
1940 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1945 struct ext4_inode_ref inode_ref;
1946 struct ext4_mountpoint *mp = ext4_get_mount(path);
1952 ext4_trans_start(mp);
1954 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1956 ext4_trans_abort(mp);
1962 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1964 ext4_trans_abort(mp);
1969 ext4_inode_set_modif_time(inode_ref.inode, mtime);
1970 inode_ref.dirty = true;
1972 r = ext4_fs_put_inode_ref(&inode_ref);
1974 ext4_trans_abort(mp);
1976 ext4_trans_stop(mp);
1982 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1987 struct ext4_inode_ref inode_ref;
1988 struct ext4_mountpoint *mp = ext4_get_mount(path);
1994 ext4_trans_start(mp);
1996 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1998 ext4_trans_abort(mp);
2004 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2006 ext4_trans_abort(mp);
2011 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2012 inode_ref.dirty = true;
2014 r = ext4_fs_put_inode_ref(&inode_ref);
2016 ext4_trans_abort(mp);
2018 ext4_trans_stop(mp);
2024 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2026 struct ext4_inode_ref ref;
2028 ext4_fsblk_t fblock;
2029 uint32_t block_size;
2032 ext4_assert(f && f->mp);
2037 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2042 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2043 if (size > block_size) {
2047 r = ext4_ftruncate_no_lock(f, 0);
2051 /*Start write back cache mode.*/
2052 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2056 /*If the size of symlink is smaller than 60 bytes*/
2057 if (size < sizeof(ref.inode->blocks)) {
2058 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2059 memcpy(ref.inode->blocks, buf, size);
2060 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2062 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2063 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2067 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2073 /*Stop write back cache mode*/
2074 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2079 ext4_inode_set_size(ref.inode, size);
2087 ext4_fs_put_inode_ref(&ref);
2091 int ext4_fsymlink(const char *target, const char *path)
2093 struct ext4_mountpoint *mp = ext4_get_mount(path);
2101 filetype = EXT4_DE_SYMLINK;
2104 ext4_trans_start(mp);
2106 ext4_block_cache_write_back(mp->fs.bdev, 1);
2107 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2109 r = ext4_fsymlink_set(&f, target, strlen(target));
2116 ext4_block_cache_write_back(mp->fs.bdev, 0);
2119 ext4_trans_abort(mp);
2121 ext4_trans_stop(mp);
2127 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2129 struct ext4_mountpoint *mp = ext4_get_mount(path);
2140 filetype = EXT4_DE_SYMLINK;
2143 ext4_block_cache_write_back(mp->fs.bdev, 1);
2144 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2146 r = ext4_fread(&f, buf, bufsize, rcnt);
2153 ext4_block_cache_write_back(mp->fs.bdev, 0);
2155 ext4_trans_abort(mp);
2157 ext4_trans_stop(mp);
2163 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2164 const void *data, size_t data_size, bool replace)
2170 const char *dissected_name = NULL;
2171 size_t dissected_len = 0;
2172 struct ext4_xattr_ref xattr_ref;
2173 struct ext4_inode_ref inode_ref;
2174 struct ext4_mountpoint *mp = ext4_get_mount(path);
2178 dissected_name = ext4_extract_xattr_name(name, name_len,
2179 &name_index, &dissected_len);
2184 ext4_trans_start(mp);
2186 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2192 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2196 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2198 ext4_fs_put_inode_ref(&inode_ref);
2202 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2203 dissected_len, data, data_size, replace);
2205 ext4_fs_put_xattr_ref(&xattr_ref);
2206 ext4_fs_put_inode_ref(&inode_ref);
2209 ext4_trans_abort(mp);
2211 ext4_trans_stop(mp);
2217 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2218 void *buf, size_t buf_size, size_t *data_size)
2224 const char *dissected_name = NULL;
2225 size_t dissected_len = 0;
2226 struct ext4_xattr_ref xattr_ref;
2227 struct ext4_inode_ref inode_ref;
2228 struct ext4_mountpoint *mp = ext4_get_mount(path);
2232 dissected_name = ext4_extract_xattr_name(name, name_len,
2233 &name_index, &dissected_len);
2238 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2244 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2248 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2250 ext4_fs_put_inode_ref(&inode_ref);
2254 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2255 dissected_len, buf, buf_size, data_size);
2257 ext4_fs_put_xattr_ref(&xattr_ref);
2258 ext4_fs_put_inode_ref(&inode_ref);
2264 struct ext4_listxattr_iterator {
2269 bool list_too_small;
2270 bool get_required_size;
2273 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2274 struct ext4_xattr_item *item)
2276 struct ext4_listxattr_iterator *lxi;
2277 lxi = ref->iter_arg;
2278 if (!lxi->get_required_size) {
2281 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2282 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2283 lxi->list_too_small = 1;
2284 return EXT4_XATTR_ITERATE_STOP;
2287 memcpy(lxi->list_ptr, prefix, plen);
2288 lxi->list_ptr += plen;
2289 lxi->ret_size += plen;
2291 memcpy(lxi->list_ptr, item->name, item->name_len);
2292 lxi->list_ptr[item->name_len] = 0;
2293 lxi->list_ptr += item->name_len + 1;
2295 lxi->ret_size += item->name_len + 1;
2296 return EXT4_XATTR_ITERATE_CONT;
2299 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2304 struct ext4_xattr_ref xattr_ref;
2305 struct ext4_inode_ref inode_ref;
2306 struct ext4_listxattr_iterator lxi;
2307 struct ext4_mountpoint *mp = ext4_get_mount(path);
2312 lxi.list_ptr = list;
2315 lxi.list_too_small = false;
2316 lxi.get_required_size = (!size) ? true : false;
2319 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2325 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2329 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2331 ext4_fs_put_inode_ref(&inode_ref);
2335 xattr_ref.iter_arg = &lxi;
2336 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2337 if (lxi.list_too_small)
2342 *ret_size = lxi.ret_size;
2345 ext4_fs_put_xattr_ref(&xattr_ref);
2346 ext4_fs_put_inode_ref(&inode_ref);
2353 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2359 const char *dissected_name = NULL;
2360 size_t dissected_len = 0;
2361 struct ext4_xattr_ref xattr_ref;
2362 struct ext4_inode_ref inode_ref;
2363 struct ext4_mountpoint *mp = ext4_get_mount(path);
2367 dissected_name = ext4_extract_xattr_name(name, name_len,
2368 &name_index, &dissected_len);
2373 ext4_trans_start(mp);
2375 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2381 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2385 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2387 ext4_fs_put_inode_ref(&inode_ref);
2391 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2394 ext4_fs_put_xattr_ref(&xattr_ref);
2395 ext4_fs_put_inode_ref(&inode_ref);
2398 ext4_trans_abort(mp);
2400 ext4_trans_stop(mp);
2407 /*********************************DIRECTORY OPERATION************************/
2409 int ext4_dir_rm(const char *path)
2415 struct ext4_mountpoint *mp = ext4_get_mount(path);
2416 struct ext4_inode_ref act;
2417 struct ext4_inode_ref child;
2418 struct ext4_dir_iter it;
2422 uint32_t inode_current;
2433 ext4_trans_start(mp);
2435 struct ext4_fs *const fs = &mp->fs;
2438 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2440 ext4_trans_abort(mp);
2446 len = ext4_path_check(path, &is_goal);
2448 inode_current = f.inode;
2451 ext4_block_cache_write_back(mp->fs.bdev, 1);
2454 /*Load directory node.*/
2455 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2460 /*Initialize iterator.*/
2461 r = ext4_dir_iterator_init(&it, &act, 0);
2463 ext4_fs_put_inode_ref(&act);
2474 /*Get up directory inode when ".." entry*/
2475 if ((it.curr->name_len == 2) &&
2476 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2477 inode_up = ext4_dir_en_get_inode(it.curr);
2480 /*If directory or file entry, but not "." ".." entry*/
2481 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2483 /*Get child inode reference do unlink
2486 cinode = ext4_dir_en_get_inode(it.curr);
2487 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2491 /*If directory with no leaf children*/
2492 r = ext4_has_children(&has_children, &child);
2494 ext4_fs_put_inode_ref(&child);
2499 /*Has directory children. Go into this
2501 inode_up = inode_current;
2502 inode_current = cinode;
2504 ext4_fs_put_inode_ref(&child);
2508 /*No children in child directory or file. Just
2510 r = ext4_unlink(f.mp, &act, &child,
2511 (char *)it.curr->name,
2514 ext4_fs_put_inode_ref(&child);
2518 ext4_inode_set_del_time(child.inode, -1L);
2519 ext4_inode_set_links_cnt(child.inode, 0);
2522 r = ext4_fs_truncate_inode(&child, 0);
2524 ext4_fs_put_inode_ref(&child);
2528 r = ext4_fs_free_inode(&child);
2530 ext4_fs_put_inode_ref(&child);
2534 r = ext4_fs_put_inode_ref(&child);
2539 r = ext4_dir_iterator_next(&it);
2543 /*Directory iterator reached last entry*/
2544 ext4_has_children(&has_children, &act);
2545 if (!has_children) {
2546 inode_current = inode_up;
2553 struct ext4_inode_ref parent;
2554 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2559 /* In this place all directories should be
2561 * Last unlink from root of current directory*/
2562 r = ext4_unlink(f.mp, &parent, &act,
2565 ext4_fs_put_inode_ref(&parent);
2569 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2570 ext4_inode_set_del_time(act.inode, -1L);
2571 ext4_inode_set_links_cnt(act.inode, 0);
2574 r = ext4_fs_truncate_inode(&act, 0);
2576 ext4_fs_put_inode_ref(&parent);
2580 r = ext4_fs_free_inode(&act);
2582 ext4_fs_put_inode_ref(&parent);
2587 r = ext4_fs_put_inode_ref(&parent);
2594 ext4_dir_iterator_fini(&it);
2595 ext4_fs_put_inode_ref(&act);
2598 /*When something goes wrong. End loop.*/
2604 ext4_block_cache_write_back(mp->fs.bdev, 0);
2608 ext4_trans_abort(mp);
2610 ext4_trans_stop(mp);
2615 int ext4_dir_mk(const char *path)
2620 struct ext4_mountpoint *mp = ext4_get_mount(path);
2626 ext4_trans_start(mp);
2629 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2631 /*Directory already created*/
2632 ext4_trans_stop(mp);
2638 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2640 ext4_trans_abort(mp);
2645 ext4_trans_stop(mp);
2650 int ext4_dir_open(ext4_dir *d, const char *path)
2652 struct ext4_mountpoint *mp = ext4_get_mount(path);
2659 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2665 int ext4_dir_close(ext4_dir *d)
2667 return ext4_fclose(&d->f);
2670 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2672 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2675 ext4_direntry *de = 0;
2676 struct ext4_inode_ref dir;
2677 struct ext4_dir_iter it;
2679 EXT4_MP_LOCK(d->f.mp);
2681 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2682 EXT4_MP_UNLOCK(d->f.mp);
2686 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2691 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2693 ext4_fs_put_inode_ref(&dir);
2697 memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2700 ext4_dir_iterator_next(&it);
2702 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2704 ext4_dir_iterator_fini(&it);
2705 ext4_fs_put_inode_ref(&dir);
2708 EXT4_MP_UNLOCK(d->f.mp);
2712 void ext4_dir_entry_rewind(ext4_dir *d)
2717 int ext4_test_journal(const char *mount_point)
2719 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
2725 ext4_block_cache_write_back(mp->fs.bdev, 1);
2726 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
2727 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
2728 struct jbd_journal *journal;
2733 journal = calloc(1, sizeof(struct jbd_journal));
2740 r = jbd_get_fs(&mp->fs, jbd_fs);
2745 r = jbd_journal_start(jbd_fs, journal);
2754 for (i = 0;i < 50;i++) {
2755 ext4_fsblk_t rand_block = rand() % 4096;
2758 struct ext4_block block;
2759 r = ext4_block_get(mp->fs.bdev, &block, rand_block);
2763 struct jbd_trans *t = jbd_journal_new_trans(journal);
2765 ext4_block_set(mp->fs.bdev, &block);
2770 switch (rand() % 2) {
2772 r = jbd_trans_get_access(journal, t, &block);
2774 jbd_journal_free_trans(journal, t,
2776 ext4_block_set(mp->fs.bdev, &block);
2780 r = jbd_trans_set_block_dirty(t, &block);
2782 jbd_journal_free_trans(journal, t,
2784 ext4_block_set(mp->fs.bdev, &block);
2790 r = jbd_trans_try_revoke_block(t, rand_block);
2792 jbd_journal_free_trans(journal, t,
2794 ext4_block_set(mp->fs.bdev, &block);
2799 ext4_block_set(mp->fs.bdev, &block);
2800 jbd_journal_submit_trans(journal, t);
2801 jbd_journal_commit_one(journal);
2804 jbd_journal_stop(journal);
2811 ext4_block_cache_write_back(mp->fs.bdev, 0);