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;
707 static int ext4_trunc_inode(struct ext4_mountpoint *mp,
708 struct ext4_inode_ref *inode_ref, uint64_t new_size)
711 struct ext4_fs *const fs = &mp->fs;
712 uint64_t inode_size = ext4_inode_get_size(&fs->sb, inode_ref->inode);
713 uint32_t index = inode_ref->index;
716 while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
718 inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
719 ext4_trans_start(mp);
720 r = ext4_fs_truncate_inode(inode_ref, inode_size);
724 r = ext4_fs_put_inode_ref(inode_ref);
726 ext4_trans_abort(mp);
730 r = ext4_fs_get_inode_ref(fs, index, inode_ref);
735 if (inode_size > new_size) {
737 inode_size = new_size;
738 ext4_trans_start(mp);
739 r = ext4_fs_truncate_inode(inode_ref, inode_size);
743 r = ext4_fs_put_inode_ref(inode_ref);
745 ext4_trans_abort(mp);
749 r = ext4_fs_get_inode_ref(fs, index, inode_ref);
758 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
759 * any filetype of the target dir entry will be accepted.
761 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
762 int ftype, uint32_t *parent_inode,
765 bool is_goal = false;
766 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
771 struct ext4_mountpoint *mp = ext4_get_mount(path);
772 struct ext4_dir_search_result result;
773 struct ext4_inode_ref ref;
780 struct ext4_fs *const fs = &mp->fs;
781 struct ext4_sblock *const sb = &mp->fs.sb;
786 path += strlen(mp->name);
789 *name_off = strlen(mp->name);
792 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
797 *parent_inode = ref.index;
799 len = ext4_path_check(path, &is_goal);
802 len = ext4_path_check(path, &is_goal);
804 /*If root open was request.*/
805 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
813 r = ext4_dir_find_entry(&result, &ref, path, len);
816 /*Destroy last result*/
817 ext4_dir_destroy_result(&ref, &result);
821 if (!(f->flags & O_CREAT))
824 /*O_CREAT allows create new entry*/
825 struct ext4_inode_ref child_ref;
826 r = ext4_fs_alloc_inode(fs, &child_ref,
827 is_goal ? ftype : EXT4_DE_DIR);
832 /*Link with root dir.*/
833 r = ext4_link(mp, &ref, &child_ref, path, len, false);
835 /*Fail. Free new inode.*/
836 ext4_fs_free_inode(&child_ref);
837 /*We do not want to write new inode.
838 But block has to be released.*/
839 child_ref.dirty = false;
840 ext4_fs_put_inode_ref(&child_ref);
844 ext4_fs_put_inode_ref(&child_ref);
849 *parent_inode = ref.index;
851 next_inode = ext4_dir_en_get_inode(result.dentry);
852 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
854 t = ext4_dir_en_get_inode_type(sb, result.dentry);
855 imode = ext4_fs_correspond_inode_mode(t);
857 struct ext4_inode_ref child_ref;
858 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
862 imode = ext4_inode_type(sb, child_ref.inode);
863 ext4_fs_put_inode_ref(&child_ref);
866 r = ext4_dir_destroy_result(&ref, &result);
870 /*If expected file error*/
871 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
875 if (ftype != EXT4_DE_UNKNOWN) {
876 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
883 r = ext4_fs_put_inode_ref(&ref);
887 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
897 *name_off += len + 1;
901 ext4_fs_put_inode_ref(&ref);
907 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
908 r = ext4_trunc_inode(mp, &ref, 0);
910 ext4_fs_put_inode_ref(&ref);
916 f->fsize = ext4_inode_get_size(sb, ref.inode);
917 f->inode = ref.index;
920 if (f->flags & O_APPEND)
925 r = ext4_fs_put_inode_ref(&ref);
929 /****************************************************************************/
931 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
932 bool file_expect, uint32_t *parent_inode,
937 if (ext4_parse_flags(flags, &iflags) == false)
940 if (file_expect == true)
941 filetype = EXT4_DE_REG_FILE;
943 filetype = EXT4_DE_DIR;
945 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
949 static int ext4_create_hardlink(const char *path,
950 struct ext4_inode_ref *child_ref, bool rename)
952 bool is_goal = false;
953 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
958 struct ext4_mountpoint *mp = ext4_get_mount(path);
959 struct ext4_dir_search_result result;
960 struct ext4_inode_ref ref;
965 struct ext4_fs *const fs = &mp->fs;
966 struct ext4_sblock *const sb = &mp->fs.sb;
969 path += strlen(mp->name);
972 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
976 len = ext4_path_check(path, &is_goal);
979 len = ext4_path_check(path, &is_goal);
981 /*If root open was request.*/
982 r = is_goal ? EINVAL : ENOENT;
986 r = ext4_dir_find_entry(&result, &ref, path, len);
989 /*Destroy last result*/
990 ext4_dir_destroy_result(&ref, &result);
992 if (r != ENOENT || !is_goal)
995 /*Link with root dir.*/
996 r = ext4_link(mp, &ref, child_ref, path, len, rename);
998 } else if (r == EOK && is_goal) {
999 /*Destroy last result*/
1000 ext4_dir_destroy_result(&ref, &result);
1005 next_inode = result.dentry->inode;
1006 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
1008 t = ext4_dir_en_get_inode_type(sb, result.dentry);
1009 inode_mode = ext4_fs_correspond_inode_mode(t);
1011 struct ext4_inode_ref child_ref;
1012 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
1016 inode_mode = ext4_inode_type(sb, child_ref.inode);
1017 ext4_fs_put_inode_ref(&child_ref);
1020 r = ext4_dir_destroy_result(&ref, &result);
1024 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
1025 r = is_goal ? EEXIST : ENOENT;
1029 r = ext4_fs_put_inode_ref(&ref);
1033 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
1044 ext4_fs_put_inode_ref(&ref);
1048 r = ext4_fs_put_inode_ref(&ref);
1052 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1053 struct ext4_inode_ref *parent_ref,
1054 struct ext4_inode_ref *child_ref)
1059 struct ext4_mountpoint *mp = ext4_get_mount(path);
1067 len = ext4_path_check(path, &is_goal);
1069 /* Remove entry from parent directory */
1070 r = ext4_dir_remove_entry(parent_ref, path, len);
1074 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1075 EXT4_INODE_MODE_DIRECTORY)) {
1076 ext4_fs_inode_links_count_dec(parent_ref);
1077 parent_ref->dirty = true;
1083 int ext4_flink(const char *path, const char *hardlink_path)
1088 bool child_loaded = false;
1089 uint32_t parent_inode, child_inode;
1090 struct ext4_mountpoint *mp = ext4_get_mount(path);
1091 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1092 struct ext4_inode_ref child_ref;
1097 /* Will that happen? Anyway return EINVAL for such case. */
1098 if (mp != target_mp)
1102 ext4_trans_start(mp);
1104 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1105 &parent_inode, &name_off);
1109 child_inode = f.inode;
1112 /*We have file to unlink. Load it.*/
1113 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1117 child_loaded = true;
1119 /* Creating hardlink for directory is not allowed. */
1120 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1121 EXT4_INODE_MODE_DIRECTORY)) {
1126 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1130 ext4_fs_put_inode_ref(&child_ref);
1133 ext4_trans_abort(mp);
1135 ext4_trans_stop(mp);
1142 int ext4_frename(const char *path, const char *new_path)
1147 bool parent_loaded = false, child_loaded = false;
1148 uint32_t parent_inode, child_inode;
1149 struct ext4_mountpoint *mp = ext4_get_mount(path);
1150 struct ext4_inode_ref child_ref, parent_ref;
1156 ext4_trans_start(mp);
1158 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1159 &parent_inode, &name_off);
1163 child_inode = f.inode;
1167 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1171 parent_loaded = true;
1173 /*We have file to unlink. Load it.*/
1174 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1178 child_loaded = true;
1180 r = ext4_create_hardlink(new_path, &child_ref, true);
1184 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1190 ext4_fs_put_inode_ref(&parent_ref);
1193 ext4_fs_put_inode_ref(&child_ref);
1196 ext4_trans_abort(mp);
1198 ext4_trans_stop(mp);
1205 /****************************************************************************/
1207 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1209 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1218 int ext4_cache_write_back(const char *path, bool on)
1220 struct ext4_mountpoint *mp = ext4_get_mount(path);
1226 ext4_block_cache_write_back(mp->fs.bdev, on);
1231 int ext4_fremove(const char *path)
1234 uint32_t parent_inode;
1239 struct ext4_inode_ref child;
1240 struct ext4_inode_ref parent;
1241 struct ext4_mountpoint *mp = ext4_get_mount(path);
1247 ext4_trans_start(mp);
1249 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1250 &parent_inode, &name_off);
1252 ext4_trans_abort(mp);
1258 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1260 ext4_trans_abort(mp);
1265 /*We have file to delete. Load it.*/
1266 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1268 ext4_fs_put_inode_ref(&parent);
1269 ext4_trans_abort(mp);
1277 len = ext4_path_check(path, &is_goal);
1279 /*Unlink from parent*/
1280 r = ext4_unlink(mp, &parent, &child, path, len);
1284 /*Link count is zero, the inode should be freed. */
1285 if (!ext4_inode_get_links_cnt(child.inode)) {
1286 ext4_inode_set_del_time(child.inode, -1L);
1288 ext4_block_cache_write_back(mp->fs.bdev, 1);
1289 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1290 r = ext4_trunc_inode(mp, &child, 0);
1291 ext4_block_cache_write_back(mp->fs.bdev, 0);
1296 r = ext4_fs_free_inode(&child);
1302 ext4_fs_put_inode_ref(&child);
1303 ext4_fs_put_inode_ref(&parent);
1306 ext4_trans_abort(mp);
1308 ext4_trans_stop(mp);
1314 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1315 struct ext4_inode *inode)
1319 struct ext4_inode_ref inode_ref;
1320 struct ext4_mountpoint *mp = ext4_get_mount(path);
1328 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1338 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1344 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1345 ext4_fs_put_inode_ref(&inode_ref);
1354 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1356 struct ext4_mountpoint *mp = ext4_get_mount(path);
1363 ext4_trans_start(mp);
1365 ext4_block_cache_write_back(mp->fs.bdev, 1);
1366 r = ext4_generic_open(f, path, flags, true, 0, 0);
1367 ext4_block_cache_write_back(mp->fs.bdev, 0);
1370 ext4_trans_abort(mp);
1372 ext4_trans_stop(mp);
1378 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1380 struct ext4_mountpoint *mp = ext4_get_mount(path);
1387 filetype = EXT4_DE_REG_FILE;
1390 ext4_trans_start(mp);
1392 ext4_block_cache_write_back(mp->fs.bdev, 1);
1393 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1394 ext4_block_cache_write_back(mp->fs.bdev, 0);
1397 ext4_trans_abort(mp);
1399 ext4_trans_stop(mp);
1405 int ext4_fclose(ext4_file *f)
1407 ext4_assert(f && f->mp);
1412 f->fpos = f->fsize = 0;
1417 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1419 struct ext4_inode_ref ref;
1423 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1425 EXT4_MP_UNLOCK(f->mp);
1430 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1431 if (f->fsize <= size) {
1436 /*Start write back cache mode.*/
1437 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1441 r = ext4_trunc_inode(f->mp, &ref, size);
1449 /*Stop write back cache mode*/
1450 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1456 ext4_fs_put_inode_ref(&ref);
1461 int ext4_ftruncate(ext4_file *f, uint64_t size)
1464 ext4_assert(f && f->mp);
1466 if (f->flags & O_RDONLY)
1469 EXT4_MP_LOCK(f->mp);
1471 ext4_trans_start(f->mp);
1472 r = ext4_ftruncate_no_lock(f, size);
1474 ext4_trans_abort(f->mp);
1476 ext4_trans_stop(f->mp);
1478 EXT4_MP_UNLOCK(f->mp);
1482 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1485 uint32_t iblock_idx;
1486 uint32_t iblock_last;
1487 uint32_t block_size;
1489 ext4_fsblk_t fblock;
1490 ext4_fsblk_t fblock_start;
1491 uint32_t fblock_count;
1493 uint8_t *u8_buf = buf;
1495 struct ext4_inode_ref ref;
1497 ext4_assert(f && f->mp);
1499 if (f->flags & O_WRONLY)
1505 EXT4_MP_LOCK(f->mp);
1507 struct ext4_fs *const fs = &f->mp->fs;
1508 struct ext4_sblock *const sb = &f->mp->fs.sb;
1513 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1515 EXT4_MP_UNLOCK(f->mp);
1520 f->fsize = ext4_inode_get_size(sb, ref.inode);
1522 block_size = ext4_sb_get_block_size(sb);
1523 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1525 iblock_idx = (f->fpos) / block_size;
1526 iblock_last = (f->fpos + size) / block_size;
1527 unalg = (f->fpos) % block_size;
1529 /*If the size of symlink is smaller than 60 bytes*/
1531 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1532 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1533 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1535 char *content = (char *)ref.inode->blocks;
1536 if (f->fpos < f->fsize) {
1538 if (unalg + size > f->fsize)
1539 len = f->fsize - unalg;
1540 memcpy(buf, content + unalg, len);
1552 if (size > (block_size - unalg))
1553 len = block_size - unalg;
1555 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1559 /* Do we get an unwritten range? */
1561 uint64_t off = fblock * block_size + unalg;
1562 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1568 memset(u8_buf, 0, len);
1583 while (size >= block_size) {
1584 while (iblock_idx < iblock_last) {
1585 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1593 fblock_start = fblock;
1595 if ((fblock_start + fblock_count) != fblock)
1601 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1606 size -= block_size * fblock_count;
1607 u8_buf += block_size * fblock_count;
1608 f->fpos += block_size * fblock_count;
1611 *rcnt += block_size * fblock_count;
1613 fblock_start = fblock;
1619 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1623 off = fblock * block_size;
1624 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1635 ext4_fs_put_inode_ref(&ref);
1636 EXT4_MP_UNLOCK(f->mp);
1640 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1644 uint32_t iblock_last;
1645 uint32_t ifile_blocks;
1646 uint32_t block_size;
1648 uint32_t fblock_count;
1650 ext4_fsblk_t fblock_start;
1652 struct ext4_inode_ref ref;
1653 const uint8_t *u8_buf = buf;
1656 ext4_assert(f && f->mp);
1658 if (f->flags & O_RDONLY)
1664 EXT4_MP_LOCK(f->mp);
1665 ext4_trans_start(f->mp);
1667 struct ext4_fs *const fs = &f->mp->fs;
1668 struct ext4_sblock *const sb = &f->mp->fs.sb;
1673 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1675 ext4_trans_abort(f->mp);
1676 EXT4_MP_UNLOCK(f->mp);
1681 f->fsize = ext4_inode_get_size(sb, ref.inode);
1682 block_size = ext4_sb_get_block_size(sb);
1684 iblock_last = (f->fpos + size) / block_size;
1685 iblk_idx = (f->fpos) / block_size;
1686 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1688 unalg = (f->fpos) % block_size;
1693 if (size > (block_size - unalg))
1694 len = block_size - unalg;
1696 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1700 off = fblk * block_size + unalg;
1701 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1715 /*Start write back cache mode.*/
1716 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1722 while (size >= block_size) {
1724 while (iblk_idx < iblock_last) {
1725 if (iblk_idx < ifile_blocks) {
1726 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1731 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1734 /* Unable to append more blocks. But
1735 * some block might be allocated already
1743 if (!fblock_start) {
1744 fblock_start = fblk;
1747 if ((fblock_start + fblock_count) != fblk)
1753 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1758 size -= block_size * fblock_count;
1759 u8_buf += block_size * fblock_count;
1760 f->fpos += block_size * fblock_count;
1763 *wcnt += block_size * fblock_count;
1765 fblock_start = fblk;
1769 /*ext4_fs_append_inode_block has failed and no
1770 * more blocks might be written. But node size
1771 * should be updated.*/
1777 /*Stop write back cache mode*/
1778 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1785 if (iblk_idx < ifile_blocks) {
1786 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1790 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1792 /*Node size sholud be updated.*/
1796 off = fblk * block_size;
1797 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1808 if (f->fpos > f->fsize) {
1810 ext4_inode_set_size(ref.inode, f->fsize);
1815 r = ext4_fs_put_inode_ref(&ref);
1818 ext4_trans_abort(f->mp);
1820 ext4_trans_stop(f->mp);
1822 EXT4_MP_UNLOCK(f->mp);
1826 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1830 if (offset > f->fsize)
1836 if ((offset + f->fpos) > f->fsize)
1842 if (offset > f->fsize)
1845 f->fpos = f->fsize - offset;
1851 uint64_t ext4_ftell(ext4_file *f)
1856 uint64_t ext4_fsize(ext4_file *f)
1861 int ext4_chmod(const char *path, uint32_t mode)
1866 struct ext4_sblock *sb;
1867 struct ext4_inode_ref inode_ref;
1868 struct ext4_mountpoint *mp = ext4_get_mount(path);
1874 ext4_trans_start(mp);
1876 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1878 ext4_trans_abort(mp);
1885 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1887 ext4_trans_abort(mp);
1892 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1893 inode_ref.dirty = true;
1895 r = ext4_fs_put_inode_ref(&inode_ref);
1897 ext4_trans_abort(mp);
1899 ext4_trans_stop(mp);
1905 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1910 struct ext4_inode_ref inode_ref;
1911 struct ext4_mountpoint *mp = ext4_get_mount(path);
1917 ext4_trans_start(mp);
1919 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1921 ext4_trans_abort(mp);
1927 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1929 ext4_trans_abort(mp);
1934 ext4_inode_set_uid(inode_ref.inode, uid);
1935 ext4_inode_set_gid(inode_ref.inode, gid);
1936 inode_ref.dirty = true;
1938 r = ext4_fs_put_inode_ref(&inode_ref);
1940 ext4_trans_abort(mp);
1942 ext4_trans_stop(mp);
1948 int ext4_file_set_atime(const char *path, uint32_t atime)
1953 struct ext4_inode_ref inode_ref;
1954 struct ext4_mountpoint *mp = ext4_get_mount(path);
1960 ext4_trans_start(mp);
1962 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1964 ext4_trans_abort(mp);
1970 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1972 ext4_trans_abort(mp);
1977 ext4_inode_set_access_time(inode_ref.inode, atime);
1978 inode_ref.dirty = true;
1980 r = ext4_fs_put_inode_ref(&inode_ref);
1982 ext4_trans_abort(mp);
1984 ext4_trans_stop(mp);
1990 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1995 struct ext4_inode_ref inode_ref;
1996 struct ext4_mountpoint *mp = ext4_get_mount(path);
2002 ext4_trans_start(mp);
2004 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2006 ext4_trans_abort(mp);
2012 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2014 ext4_trans_abort(mp);
2019 ext4_inode_set_modif_time(inode_ref.inode, mtime);
2020 inode_ref.dirty = true;
2022 r = ext4_fs_put_inode_ref(&inode_ref);
2024 ext4_trans_abort(mp);
2026 ext4_trans_stop(mp);
2032 int ext4_file_set_ctime(const char *path, uint32_t ctime)
2037 struct ext4_inode_ref inode_ref;
2038 struct ext4_mountpoint *mp = ext4_get_mount(path);
2044 ext4_trans_start(mp);
2046 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2048 ext4_trans_abort(mp);
2054 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2056 ext4_trans_abort(mp);
2061 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2062 inode_ref.dirty = true;
2064 r = ext4_fs_put_inode_ref(&inode_ref);
2066 ext4_trans_abort(mp);
2068 ext4_trans_stop(mp);
2074 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2076 struct ext4_inode_ref ref;
2078 ext4_fsblk_t fblock;
2079 uint32_t block_size;
2082 ext4_assert(f && f->mp);
2087 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2092 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2093 if (size > block_size) {
2097 r = ext4_ftruncate_no_lock(f, 0);
2101 /*Start write back cache mode.*/
2102 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2106 /*If the size of symlink is smaller than 60 bytes*/
2107 if (size < sizeof(ref.inode->blocks)) {
2108 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2109 memcpy(ref.inode->blocks, buf, size);
2110 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2112 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2113 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2117 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2123 /*Stop write back cache mode*/
2124 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2129 ext4_inode_set_size(ref.inode, size);
2137 ext4_fs_put_inode_ref(&ref);
2141 int ext4_fsymlink(const char *target, const char *path)
2143 struct ext4_mountpoint *mp = ext4_get_mount(path);
2151 filetype = EXT4_DE_SYMLINK;
2154 ext4_trans_start(mp);
2156 ext4_block_cache_write_back(mp->fs.bdev, 1);
2157 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2159 r = ext4_fsymlink_set(&f, target, strlen(target));
2166 ext4_block_cache_write_back(mp->fs.bdev, 0);
2169 ext4_trans_abort(mp);
2171 ext4_trans_stop(mp);
2177 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2179 struct ext4_mountpoint *mp = ext4_get_mount(path);
2190 filetype = EXT4_DE_SYMLINK;
2193 ext4_block_cache_write_back(mp->fs.bdev, 1);
2194 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2196 r = ext4_fread(&f, buf, bufsize, rcnt);
2203 ext4_block_cache_write_back(mp->fs.bdev, 0);
2205 ext4_trans_abort(mp);
2207 ext4_trans_stop(mp);
2213 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2214 const void *data, size_t data_size, bool replace)
2220 const char *dissected_name = NULL;
2221 size_t dissected_len = 0;
2222 struct ext4_xattr_ref xattr_ref;
2223 struct ext4_inode_ref inode_ref;
2224 struct ext4_mountpoint *mp = ext4_get_mount(path);
2228 dissected_name = ext4_extract_xattr_name(name, name_len,
2229 &name_index, &dissected_len);
2234 ext4_trans_start(mp);
2236 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2242 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2246 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2248 ext4_fs_put_inode_ref(&inode_ref);
2252 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2253 dissected_len, data, data_size, replace);
2255 ext4_fs_put_xattr_ref(&xattr_ref);
2256 ext4_fs_put_inode_ref(&inode_ref);
2259 ext4_trans_abort(mp);
2261 ext4_trans_stop(mp);
2267 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2268 void *buf, size_t buf_size, size_t *data_size)
2274 const char *dissected_name = NULL;
2275 size_t dissected_len = 0;
2276 struct ext4_xattr_ref xattr_ref;
2277 struct ext4_inode_ref inode_ref;
2278 struct ext4_mountpoint *mp = ext4_get_mount(path);
2282 dissected_name = ext4_extract_xattr_name(name, name_len,
2283 &name_index, &dissected_len);
2288 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2294 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2298 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2300 ext4_fs_put_inode_ref(&inode_ref);
2304 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2305 dissected_len, buf, buf_size, data_size);
2307 ext4_fs_put_xattr_ref(&xattr_ref);
2308 ext4_fs_put_inode_ref(&inode_ref);
2314 struct ext4_listxattr_iterator {
2319 bool list_too_small;
2320 bool get_required_size;
2323 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2324 struct ext4_xattr_item *item)
2326 struct ext4_listxattr_iterator *lxi;
2327 lxi = ref->iter_arg;
2328 if (!lxi->get_required_size) {
2331 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2332 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2333 lxi->list_too_small = 1;
2334 return EXT4_XATTR_ITERATE_STOP;
2337 memcpy(lxi->list_ptr, prefix, plen);
2338 lxi->list_ptr += plen;
2339 lxi->ret_size += plen;
2341 memcpy(lxi->list_ptr, item->name, item->name_len);
2342 lxi->list_ptr[item->name_len] = 0;
2343 lxi->list_ptr += item->name_len + 1;
2345 lxi->ret_size += item->name_len + 1;
2346 return EXT4_XATTR_ITERATE_CONT;
2349 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2354 struct ext4_xattr_ref xattr_ref;
2355 struct ext4_inode_ref inode_ref;
2356 struct ext4_listxattr_iterator lxi;
2357 struct ext4_mountpoint *mp = ext4_get_mount(path);
2362 lxi.list_ptr = list;
2365 lxi.list_too_small = false;
2366 lxi.get_required_size = (!size) ? true : false;
2369 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2375 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2379 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2381 ext4_fs_put_inode_ref(&inode_ref);
2385 xattr_ref.iter_arg = &lxi;
2386 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2387 if (lxi.list_too_small)
2392 *ret_size = lxi.ret_size;
2395 ext4_fs_put_xattr_ref(&xattr_ref);
2396 ext4_fs_put_inode_ref(&inode_ref);
2403 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2409 const char *dissected_name = NULL;
2410 size_t dissected_len = 0;
2411 struct ext4_xattr_ref xattr_ref;
2412 struct ext4_inode_ref inode_ref;
2413 struct ext4_mountpoint *mp = ext4_get_mount(path);
2417 dissected_name = ext4_extract_xattr_name(name, name_len,
2418 &name_index, &dissected_len);
2423 ext4_trans_start(mp);
2425 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2431 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2435 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2437 ext4_fs_put_inode_ref(&inode_ref);
2441 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2444 ext4_fs_put_xattr_ref(&xattr_ref);
2445 ext4_fs_put_inode_ref(&inode_ref);
2448 ext4_trans_abort(mp);
2450 ext4_trans_stop(mp);
2457 /*********************************DIRECTORY OPERATION************************/
2459 int ext4_dir_rm(const char *path)
2465 struct ext4_mountpoint *mp = ext4_get_mount(path);
2466 struct ext4_inode_ref act;
2467 struct ext4_inode_ref child;
2468 struct ext4_dir_iter it;
2472 uint32_t inode_current;
2484 struct ext4_fs *const fs = &mp->fs;
2487 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2489 ext4_trans_abort(mp);
2495 len = ext4_path_check(path, &is_goal);
2497 inode_current = f.inode;
2499 ext4_block_cache_write_back(mp->fs.bdev, 1);
2503 uint64_t act_curr_pos = 0;
2504 has_children = false;
2507 while (r == EOK && !has_children && !dir_end) {
2509 /*Load directory node.*/
2510 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2515 /*Initialize iterator.*/
2516 r = ext4_dir_iterator_init(&it, &act, act_curr_pos);
2518 ext4_fs_put_inode_ref(&act);
2527 ext4_trans_start(mp);
2529 /*Get up directory inode when ".." entry*/
2530 if ((it.curr->name_len == 2) &&
2531 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2532 inode_up = ext4_dir_en_get_inode(it.curr);
2535 /*If directory or file entry, but not "." ".." entry*/
2536 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2538 /*Get child inode reference do unlink
2541 cinode = ext4_dir_en_get_inode(it.curr);
2542 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2546 /*If directory with no leaf children*/
2547 r = ext4_has_children(&has_children, &child);
2549 ext4_fs_put_inode_ref(&child);
2554 /*Has directory children. Go into this
2556 inode_up = inode_current;
2557 inode_current = cinode;
2559 ext4_fs_put_inode_ref(&child);
2564 r = ext4_trunc_inode(mp, &child, 0);
2566 ext4_fs_put_inode_ref(&child);
2570 /*No children in child directory or file. Just
2572 r = ext4_unlink(f.mp, &act, &child,
2573 (char *)it.curr->name,
2576 ext4_fs_put_inode_ref(&child);
2580 ext4_inode_set_del_time(child.inode, -1L);
2581 ext4_inode_set_links_cnt(child.inode, 0);
2584 r = ext4_fs_free_inode(&child);
2586 ext4_fs_put_inode_ref(&child);
2590 r = ext4_fs_put_inode_ref(&child);
2596 r = ext4_dir_iterator_next(&it);
2600 act_curr_pos = it.curr_off;
2602 ext4_dir_iterator_fini(&it);
2604 r = ext4_fs_put_inode_ref(&act);
2606 ext4_fs_put_inode_ref(&act);
2609 ext4_trans_abort(mp);
2611 ext4_trans_stop(mp);
2615 /*Directory iterator reached last entry*/
2618 inode_current = inode_up;
2625 if (r == EOK && !depth) {
2627 struct ext4_inode_ref parent;
2628 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2632 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
2635 ext4_fs_put_inode_ref(&act);
2639 ext4_trans_start(mp);
2641 /* In this place all directories should be
2643 * Last unlink from root of current directory*/
2644 r = ext4_unlink(f.mp, &parent, &act,
2647 ext4_fs_put_inode_ref(&parent);
2648 ext4_fs_put_inode_ref(&act);
2652 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2653 ext4_inode_set_del_time(act.inode, -1L);
2654 ext4_inode_set_links_cnt(act.inode, 0);
2657 r = ext4_trunc_inode(mp, &act, 0);
2659 ext4_fs_put_inode_ref(&parent);
2660 ext4_fs_put_inode_ref(&act);
2664 r = ext4_fs_free_inode(&act);
2666 ext4_fs_put_inode_ref(&parent);
2667 ext4_fs_put_inode_ref(&act);
2672 r = ext4_fs_put_inode_ref(&parent);
2676 r = ext4_fs_put_inode_ref(&act);
2679 ext4_trans_abort(mp);
2681 ext4_trans_stop(mp);
2684 ext4_block_cache_write_back(mp->fs.bdev, 0);
2690 int ext4_dir_mk(const char *path)
2695 struct ext4_mountpoint *mp = ext4_get_mount(path);
2701 ext4_trans_start(mp);
2704 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2706 /*Directory already created*/
2707 ext4_trans_stop(mp);
2713 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2715 ext4_trans_abort(mp);
2720 ext4_trans_stop(mp);
2725 int ext4_dir_open(ext4_dir *d, const char *path)
2727 struct ext4_mountpoint *mp = ext4_get_mount(path);
2734 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2740 int ext4_dir_close(ext4_dir *d)
2742 return ext4_fclose(&d->f);
2745 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2747 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2750 ext4_direntry *de = 0;
2751 struct ext4_inode_ref dir;
2752 struct ext4_dir_iter it;
2754 EXT4_MP_LOCK(d->f.mp);
2756 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2757 EXT4_MP_UNLOCK(d->f.mp);
2761 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2766 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2768 ext4_fs_put_inode_ref(&dir);
2772 memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2775 ext4_dir_iterator_next(&it);
2777 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2779 ext4_dir_iterator_fini(&it);
2780 ext4_fs_put_inode_ref(&dir);
2783 EXT4_MP_UNLOCK(d->f.mp);
2787 void ext4_dir_entry_rewind(ext4_dir *d)
2792 int ext4_test_journal(const char *mount_point)
2794 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
2800 ext4_block_cache_write_back(mp->fs.bdev, 1);
2801 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
2802 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
2803 struct jbd_journal *journal;
2808 journal = calloc(1, sizeof(struct jbd_journal));
2815 r = jbd_get_fs(&mp->fs, jbd_fs);
2820 r = jbd_journal_start(jbd_fs, journal);
2829 for (i = 0;i < 50;i++) {
2830 ext4_fsblk_t rand_block = rand() % 4096;
2833 struct ext4_block block;
2834 r = ext4_block_get(mp->fs.bdev, &block, rand_block);
2838 struct jbd_trans *t = jbd_journal_new_trans(journal);
2840 ext4_block_set(mp->fs.bdev, &block);
2845 switch (rand() % 2) {
2847 r = jbd_trans_get_access(journal, t, &block);
2849 jbd_journal_free_trans(journal, t,
2851 ext4_block_set(mp->fs.bdev, &block);
2855 r = jbd_trans_set_block_dirty(t, &block);
2857 jbd_journal_free_trans(journal, t,
2859 ext4_block_set(mp->fs.bdev, &block);
2865 r = jbd_trans_try_revoke_block(t, rand_block);
2867 jbd_journal_free_trans(journal, t,
2869 ext4_block_set(mp->fs.bdev, &block);
2874 ext4_block_set(mp->fs.bdev, &block);
2875 jbd_journal_submit_trans(journal, t);
2876 jbd_journal_commit_one(journal);
2879 jbd_journal_stop(journal);
2886 ext4_block_cache_write_back(mp->fs.bdev, 0);