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_bcache_set_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);
422 int ext4_umount(const char *mount_point)
426 struct ext4_mountpoint *mp = 0;
428 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
429 if (!strcmp(_mp[i].name, mount_point)) {
438 r = ext4_fs_fini(&mp->fs);
444 ext4_bcache_cleanup(mp->fs.bdev->bc);
445 if (mp->cache_dynamic) {
446 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
447 free(mp->fs.bdev->bc);
450 return ext4_block_fini(mp->fs.bdev);
453 static struct ext4_mountpoint *ext4_get_mount(const char *path)
456 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
461 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
467 int ext4_journal_start(const char *mount_point)
470 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
474 if (ext4_sb_feature_com(&mp->fs.sb,
475 EXT4_FCOM_HAS_JOURNAL)) {
476 r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
480 r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
482 mp->jbd_fs.dirty = false;
483 jbd_put_fs(&mp->jbd_fs);
486 mp->fs.jbd_fs = &mp->jbd_fs;
487 mp->fs.jbd_journal = &mp->jbd_journal;
493 int ext4_journal_stop(const char *mount_point)
496 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
500 if (ext4_sb_feature_com(&mp->fs.sb,
501 EXT4_FCOM_HAS_JOURNAL)) {
502 r = jbd_journal_stop(&mp->jbd_journal);
504 mp->jbd_fs.dirty = false;
505 jbd_put_fs(&mp->jbd_fs);
506 mp->fs.jbd_journal = NULL;
507 mp->fs.jbd_fs = NULL;
511 r = jbd_put_fs(&mp->jbd_fs);
513 mp->fs.jbd_journal = NULL;
514 mp->fs.jbd_fs = NULL;
518 mp->fs.jbd_journal = NULL;
519 mp->fs.jbd_fs = NULL;
525 int ext4_recover(const char *mount_point)
527 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
533 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
534 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
541 r = jbd_get_fs(&mp->fs, jbd_fs);
547 r = jbd_recover(jbd_fs);
558 int ext4_trans_start(struct ext4_mountpoint *mp)
561 if (mp->fs.jbd_journal) {
562 struct jbd_journal *journal = mp->fs.jbd_journal;
563 struct jbd_trans *trans;
564 trans = jbd_journal_new_trans(journal);
569 mp->fs.curr_trans = trans;
575 int ext4_trans_stop(struct ext4_mountpoint *mp)
578 if (mp->fs.jbd_journal) {
579 struct jbd_journal *journal = mp->fs.jbd_journal;
580 struct jbd_trans *trans = mp->fs.curr_trans;
581 r = jbd_journal_commit_trans(journal, trans);
582 mp->fs.curr_trans = NULL;
587 int ext4_trans_abort(struct ext4_mountpoint *mp)
590 if (mp->fs.jbd_journal) {
591 struct jbd_journal *journal = mp->fs.jbd_journal;
592 struct jbd_trans *trans = mp->fs.curr_trans;
593 r = jbd_journal_commit_trans(journal, trans);
594 mp->fs.curr_trans = NULL;
599 int ext4_mount_point_stats(const char *mount_point,
600 struct ext4_mount_stats *stats)
602 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
608 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
609 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
610 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
611 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
612 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
614 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
615 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
616 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
618 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
624 int ext4_mount_setup_locks(const char *mount_point,
625 const struct ext4_lock *locks)
628 struct ext4_mountpoint *mp = 0;
630 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
631 if (!strcmp(_mp[i].name, mount_point)) {
639 mp->os_locks = locks;
643 /********************************FILE OPERATIONS*****************************/
645 static int ext4_path_check(const char *path, bool *is_goal)
649 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
651 if (path[i] == '/') {
665 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
670 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
671 *file_flags = O_RDONLY;
675 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
676 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
680 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
681 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
685 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
686 !strcmp(flags, "r+b")) {
687 *file_flags = O_RDWR;
691 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
692 !strcmp(flags, "w+b")) {
693 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
697 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
698 !strcmp(flags, "a+b")) {
699 *file_flags = O_RDWR | O_CREAT | O_APPEND;
707 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
708 * any filetype of the target dir entry will be accepted.
710 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
711 int ftype, uint32_t *parent_inode,
714 bool is_goal = false;
715 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
720 struct ext4_mountpoint *mp = ext4_get_mount(path);
721 struct ext4_dir_search_result result;
722 struct ext4_inode_ref ref;
729 struct ext4_fs *const fs = &mp->fs;
730 struct ext4_sblock *const sb = &mp->fs.sb;
735 path += strlen(mp->name);
738 *name_off = strlen(mp->name);
741 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
746 *parent_inode = ref.index;
748 len = ext4_path_check(path, &is_goal);
751 len = ext4_path_check(path, &is_goal);
753 /*If root open was request.*/
754 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
762 r = ext4_dir_find_entry(&result, &ref, path, len);
765 /*Destroy last result*/
766 ext4_dir_destroy_result(&ref, &result);
770 if (!(f->flags & O_CREAT))
773 /*O_CREAT allows create new entry*/
774 struct ext4_inode_ref child_ref;
775 r = ext4_fs_alloc_inode(fs, &child_ref,
776 is_goal ? ftype : EXT4_DE_DIR);
781 /*Link with root dir.*/
782 r = ext4_link(mp, &ref, &child_ref, path, len, false);
784 /*Fail. Free new inode.*/
785 ext4_fs_free_inode(&child_ref);
786 /*We do not want to write new inode.
787 But block has to be released.*/
788 child_ref.dirty = false;
789 ext4_fs_put_inode_ref(&child_ref);
793 ext4_fs_put_inode_ref(&child_ref);
798 *parent_inode = ref.index;
800 next_inode = ext4_dir_en_get_inode(result.dentry);
801 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
803 t = ext4_dir_en_get_inode_type(sb, result.dentry);
804 imode = ext4_fs_correspond_inode_mode(t);
806 struct ext4_inode_ref child_ref;
807 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
811 imode = ext4_inode_type(sb, child_ref.inode);
812 ext4_fs_put_inode_ref(&child_ref);
815 r = ext4_dir_destroy_result(&ref, &result);
819 /*If expected file error*/
820 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
824 if (ftype != EXT4_DE_UNKNOWN) {
825 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
832 r = ext4_fs_put_inode_ref(&ref);
836 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
846 *name_off += len + 1;
850 ext4_fs_put_inode_ref(&ref);
856 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
857 r = ext4_fs_truncate_inode(&ref, 0);
859 ext4_fs_put_inode_ref(&ref);
865 f->fsize = ext4_inode_get_size(sb, ref.inode);
866 f->inode = ref.index;
869 if (f->flags & O_APPEND)
874 r = ext4_fs_put_inode_ref(&ref);
878 /****************************************************************************/
880 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
881 bool file_expect, uint32_t *parent_inode,
886 if (ext4_parse_flags(flags, &iflags) == false)
889 if (file_expect == true)
890 filetype = EXT4_DE_REG_FILE;
892 filetype = EXT4_DE_DIR;
894 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
898 static int ext4_create_hardlink(const char *path,
899 struct ext4_inode_ref *child_ref, bool rename)
901 bool is_goal = false;
902 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
907 struct ext4_mountpoint *mp = ext4_get_mount(path);
908 struct ext4_dir_search_result result;
909 struct ext4_inode_ref ref;
914 struct ext4_fs *const fs = &mp->fs;
915 struct ext4_sblock *const sb = &mp->fs.sb;
918 path += strlen(mp->name);
921 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
925 len = ext4_path_check(path, &is_goal);
928 len = ext4_path_check(path, &is_goal);
930 /*If root open was request.*/
931 r = is_goal ? EINVAL : ENOENT;
935 r = ext4_dir_find_entry(&result, &ref, path, len);
938 /*Destroy last result*/
939 ext4_dir_destroy_result(&ref, &result);
941 if (r != ENOENT || !is_goal)
944 /*Link with root dir.*/
945 r = ext4_link(mp, &ref, child_ref, path, len, rename);
947 } else if (r == EOK && is_goal) {
948 /*Destroy last result*/
949 ext4_dir_destroy_result(&ref, &result);
954 next_inode = result.dentry->inode;
955 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
957 t = ext4_dir_en_get_inode_type(sb, result.dentry);
958 inode_mode = ext4_fs_correspond_inode_mode(t);
960 struct ext4_inode_ref child_ref;
961 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
965 inode_mode = ext4_inode_type(sb, child_ref.inode);
966 ext4_fs_put_inode_ref(&child_ref);
969 r = ext4_dir_destroy_result(&ref, &result);
973 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
974 r = is_goal ? EEXIST : ENOENT;
978 r = ext4_fs_put_inode_ref(&ref);
982 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
993 ext4_fs_put_inode_ref(&ref);
997 r = ext4_fs_put_inode_ref(&ref);
1001 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
1002 struct ext4_inode_ref *parent_ref,
1003 struct ext4_inode_ref *child_ref)
1008 struct ext4_mountpoint *mp = ext4_get_mount(path);
1016 len = ext4_path_check(path, &is_goal);
1018 /* Remove entry from parent directory */
1019 r = ext4_dir_remove_entry(parent_ref, path, len);
1023 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
1024 EXT4_INODE_MODE_DIRECTORY)) {
1025 ext4_fs_inode_links_count_dec(parent_ref);
1026 parent_ref->dirty = true;
1032 int ext4_flink(const char *path, const char *hardlink_path)
1037 bool child_loaded = false;
1038 uint32_t parent_inode, child_inode;
1039 struct ext4_mountpoint *mp = ext4_get_mount(path);
1040 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
1041 struct ext4_inode_ref child_ref;
1046 /* Will that happen? Anyway return EINVAL for such case. */
1047 if (mp != target_mp)
1051 ext4_trans_start(mp);
1053 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1054 &parent_inode, &name_off);
1058 child_inode = f.inode;
1061 /*We have file to unlink. Load it.*/
1062 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1066 child_loaded = true;
1068 /* Creating hardlink for directory is not allowed. */
1069 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
1070 EXT4_INODE_MODE_DIRECTORY)) {
1075 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
1079 ext4_fs_put_inode_ref(&child_ref);
1082 ext4_trans_abort(mp);
1084 ext4_trans_stop(mp);
1091 int ext4_frename(const char *path, const char *new_path)
1096 bool parent_loaded = false, child_loaded = false;
1097 uint32_t parent_inode, child_inode;
1098 struct ext4_mountpoint *mp = ext4_get_mount(path);
1099 struct ext4_inode_ref child_ref, parent_ref;
1105 ext4_trans_start(mp);
1107 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1108 &parent_inode, &name_off);
1112 child_inode = f.inode;
1116 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1120 parent_loaded = true;
1122 /*We have file to unlink. Load it.*/
1123 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1127 child_loaded = true;
1129 r = ext4_create_hardlink(new_path, &child_ref, true);
1133 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1139 ext4_fs_put_inode_ref(&parent_ref);
1142 ext4_fs_put_inode_ref(&child_ref);
1145 ext4_trans_abort(mp);
1147 ext4_trans_stop(mp);
1154 /****************************************************************************/
1156 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1158 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1167 int ext4_cache_write_back(const char *path, bool on)
1169 struct ext4_mountpoint *mp = ext4_get_mount(path);
1175 ext4_block_cache_write_back(mp->fs.bdev, on);
1180 int ext4_fremove(const char *path)
1183 uint32_t parent_inode;
1188 struct ext4_inode_ref child;
1189 struct ext4_inode_ref parent;
1190 struct ext4_mountpoint *mp = ext4_get_mount(path);
1196 ext4_trans_start(mp);
1198 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1199 &parent_inode, &name_off);
1201 ext4_trans_abort(mp);
1207 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1209 ext4_trans_abort(mp);
1214 /*We have file to delete. Load it.*/
1215 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1217 ext4_fs_put_inode_ref(&parent);
1218 ext4_trans_abort(mp);
1226 len = ext4_path_check(path, &is_goal);
1228 /*Unlink from parent*/
1229 r = ext4_unlink(mp, &parent, &child, path, len);
1233 /*Link count is zero, the inode should be freed. */
1234 if (!ext4_inode_get_links_cnt(child.inode)) {
1235 ext4_inode_set_del_time(child.inode, -1L);
1237 ext4_block_cache_write_back(mp->fs.bdev, 1);
1238 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1239 r = ext4_fs_truncate_inode(&child, 0);
1240 ext4_block_cache_write_back(mp->fs.bdev, 0);
1245 r = ext4_fs_free_inode(&child);
1251 ext4_fs_put_inode_ref(&child);
1252 ext4_fs_put_inode_ref(&parent);
1255 ext4_trans_abort(mp);
1257 ext4_trans_stop(mp);
1263 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1264 struct ext4_inode *inode)
1268 struct ext4_inode_ref inode_ref;
1269 struct ext4_mountpoint *mp = ext4_get_mount(path);
1277 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1287 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1293 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1294 ext4_fs_put_inode_ref(&inode_ref);
1303 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1305 struct ext4_mountpoint *mp = ext4_get_mount(path);
1312 ext4_trans_start(mp);
1314 ext4_block_cache_write_back(mp->fs.bdev, 1);
1315 r = ext4_generic_open(f, path, flags, true, 0, 0);
1316 ext4_block_cache_write_back(mp->fs.bdev, 0);
1319 ext4_trans_abort(mp);
1321 ext4_trans_stop(mp);
1327 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1329 struct ext4_mountpoint *mp = ext4_get_mount(path);
1336 filetype = EXT4_DE_REG_FILE;
1339 ext4_trans_start(mp);
1341 ext4_block_cache_write_back(mp->fs.bdev, 1);
1342 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1343 ext4_block_cache_write_back(mp->fs.bdev, 0);
1346 ext4_trans_abort(mp);
1348 ext4_trans_stop(mp);
1354 int ext4_fclose(ext4_file *f)
1356 ext4_assert(f && f->mp);
1361 f->fpos = f->fsize = 0;
1366 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1368 struct ext4_inode_ref ref;
1372 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1374 EXT4_MP_UNLOCK(f->mp);
1379 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1380 if (f->fsize <= size) {
1385 /*Start write back cache mode.*/
1386 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1390 r = ext4_fs_truncate_inode(&ref, size);
1398 /*Stop write back cache mode*/
1399 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1405 ext4_fs_put_inode_ref(&ref);
1410 int ext4_ftruncate(ext4_file *f, uint64_t size)
1413 ext4_assert(f && f->mp);
1415 if (f->flags & O_RDONLY)
1418 EXT4_MP_LOCK(f->mp);
1420 ext4_trans_start(f->mp);
1421 r = ext4_ftruncate_no_lock(f, size);
1423 ext4_trans_abort(f->mp);
1425 ext4_trans_stop(f->mp);
1427 EXT4_MP_UNLOCK(f->mp);
1431 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1434 uint32_t iblock_idx;
1435 uint32_t iblock_last;
1436 uint32_t block_size;
1438 ext4_fsblk_t fblock;
1439 ext4_fsblk_t fblock_start;
1440 uint32_t fblock_count;
1442 uint8_t *u8_buf = buf;
1444 struct ext4_inode_ref ref;
1446 ext4_assert(f && f->mp);
1448 if (f->flags & O_WRONLY)
1454 EXT4_MP_LOCK(f->mp);
1456 struct ext4_fs *const fs = &f->mp->fs;
1457 struct ext4_sblock *const sb = &f->mp->fs.sb;
1462 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1464 EXT4_MP_UNLOCK(f->mp);
1469 f->fsize = ext4_inode_get_size(sb, ref.inode);
1471 block_size = ext4_sb_get_block_size(sb);
1472 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1474 iblock_idx = (f->fpos) / block_size;
1475 iblock_last = (f->fpos + size) / block_size;
1476 unalg = (f->fpos) % block_size;
1478 /*If the size of symlink is smaller than 60 bytes*/
1480 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1481 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1482 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1484 char *content = (char *)ref.inode->blocks;
1485 if (f->fpos < f->fsize) {
1487 if (unalg + size > f->fsize)
1488 len = f->fsize - unalg;
1489 memcpy(buf, content + unalg, len);
1501 if (size > (block_size - unalg))
1502 len = block_size - unalg;
1504 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1508 /* Do we get an unwritten range? */
1510 uint64_t off = fblock * block_size + unalg;
1511 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1517 memset(u8_buf, 0, len);
1532 while (size >= block_size) {
1533 while (iblock_idx < iblock_last) {
1534 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1542 fblock_start = fblock;
1544 if ((fblock_start + fblock_count) != fblock)
1550 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1555 size -= block_size * fblock_count;
1556 u8_buf += block_size * fblock_count;
1557 f->fpos += block_size * fblock_count;
1560 *rcnt += block_size * fblock_count;
1562 fblock_start = fblock;
1568 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1572 off = fblock * block_size;
1573 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1584 ext4_fs_put_inode_ref(&ref);
1585 EXT4_MP_UNLOCK(f->mp);
1589 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1593 uint32_t iblock_last;
1594 uint32_t ifile_blocks;
1595 uint32_t block_size;
1597 uint32_t fblock_count;
1599 ext4_fsblk_t fblock_start;
1601 struct ext4_inode_ref ref;
1602 const uint8_t *u8_buf = buf;
1605 ext4_assert(f && f->mp);
1607 if (f->flags & O_RDONLY)
1613 EXT4_MP_LOCK(f->mp);
1614 ext4_trans_start(f->mp);
1616 struct ext4_fs *const fs = &f->mp->fs;
1617 struct ext4_sblock *const sb = &f->mp->fs.sb;
1622 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1624 ext4_trans_abort(f->mp);
1625 EXT4_MP_UNLOCK(f->mp);
1630 f->fsize = ext4_inode_get_size(sb, ref.inode);
1631 block_size = ext4_sb_get_block_size(sb);
1633 iblock_last = (f->fpos + size) / block_size;
1634 iblk_idx = (f->fpos) / block_size;
1635 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1637 unalg = (f->fpos) % block_size;
1642 if (size > (block_size - unalg))
1643 len = block_size - unalg;
1645 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1649 off = fblk * block_size + unalg;
1650 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1664 /*Start write back cache mode.*/
1665 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1671 while (size >= block_size) {
1673 while (iblk_idx < iblock_last) {
1674 if (iblk_idx < ifile_blocks) {
1675 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1680 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1683 /* Unable to append more blocks. But
1684 * some block might be allocated already
1692 if (!fblock_start) {
1693 fblock_start = fblk;
1696 if ((fblock_start + fblock_count) != fblk)
1702 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1707 size -= block_size * fblock_count;
1708 u8_buf += block_size * fblock_count;
1709 f->fpos += block_size * fblock_count;
1712 *wcnt += block_size * fblock_count;
1714 fblock_start = fblk;
1718 /*ext4_fs_append_inode_block has failed and no
1719 * more blocks might be written. But node size
1720 * should be updated.*/
1726 /*Stop write back cache mode*/
1727 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1734 if (iblk_idx < ifile_blocks) {
1735 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1739 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1741 /*Node size sholud be updated.*/
1745 off = fblk * block_size;
1746 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1757 if (f->fpos > f->fsize) {
1759 ext4_inode_set_size(ref.inode, f->fsize);
1764 r = ext4_fs_put_inode_ref(&ref);
1767 ext4_trans_abort(f->mp);
1769 ext4_trans_stop(f->mp);
1771 EXT4_MP_UNLOCK(f->mp);
1775 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1779 if (offset > f->fsize)
1785 if ((offset + f->fpos) > f->fsize)
1791 if (offset > f->fsize)
1794 f->fpos = f->fsize - offset;
1800 uint64_t ext4_ftell(ext4_file *f)
1805 uint64_t ext4_fsize(ext4_file *f)
1810 int ext4_chmod(const char *path, uint32_t mode)
1815 struct ext4_sblock *sb;
1816 struct ext4_inode_ref inode_ref;
1817 struct ext4_mountpoint *mp = ext4_get_mount(path);
1823 ext4_trans_start(mp);
1825 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1827 ext4_trans_abort(mp);
1834 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1836 ext4_trans_abort(mp);
1841 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1842 inode_ref.dirty = true;
1844 r = ext4_fs_put_inode_ref(&inode_ref);
1846 ext4_trans_abort(mp);
1848 ext4_trans_stop(mp);
1854 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1859 struct ext4_inode_ref inode_ref;
1860 struct ext4_mountpoint *mp = ext4_get_mount(path);
1866 ext4_trans_start(mp);
1868 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1870 ext4_trans_abort(mp);
1876 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1878 ext4_trans_abort(mp);
1883 ext4_inode_set_uid(inode_ref.inode, uid);
1884 ext4_inode_set_gid(inode_ref.inode, gid);
1885 inode_ref.dirty = true;
1887 r = ext4_fs_put_inode_ref(&inode_ref);
1889 ext4_trans_abort(mp);
1891 ext4_trans_stop(mp);
1897 int ext4_file_set_atime(const char *path, uint32_t atime)
1902 struct ext4_inode_ref inode_ref;
1903 struct ext4_mountpoint *mp = ext4_get_mount(path);
1909 ext4_trans_start(mp);
1911 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1913 ext4_trans_abort(mp);
1919 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1921 ext4_trans_abort(mp);
1926 ext4_inode_set_access_time(inode_ref.inode, atime);
1927 inode_ref.dirty = true;
1929 r = ext4_fs_put_inode_ref(&inode_ref);
1931 ext4_trans_abort(mp);
1933 ext4_trans_stop(mp);
1939 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1944 struct ext4_inode_ref inode_ref;
1945 struct ext4_mountpoint *mp = ext4_get_mount(path);
1951 ext4_trans_start(mp);
1953 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1955 ext4_trans_abort(mp);
1961 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1963 ext4_trans_abort(mp);
1968 ext4_inode_set_modif_time(inode_ref.inode, mtime);
1969 inode_ref.dirty = true;
1971 r = ext4_fs_put_inode_ref(&inode_ref);
1973 ext4_trans_abort(mp);
1975 ext4_trans_stop(mp);
1981 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1986 struct ext4_inode_ref inode_ref;
1987 struct ext4_mountpoint *mp = ext4_get_mount(path);
1993 ext4_trans_start(mp);
1995 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1997 ext4_trans_abort(mp);
2003 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
2005 ext4_trans_abort(mp);
2010 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
2011 inode_ref.dirty = true;
2013 r = ext4_fs_put_inode_ref(&inode_ref);
2015 ext4_trans_abort(mp);
2017 ext4_trans_stop(mp);
2023 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
2025 struct ext4_inode_ref ref;
2027 ext4_fsblk_t fblock;
2028 uint32_t block_size;
2031 ext4_assert(f && f->mp);
2036 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
2041 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
2042 if (size > block_size) {
2046 r = ext4_ftruncate_no_lock(f, 0);
2050 /*Start write back cache mode.*/
2051 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
2055 /*If the size of symlink is smaller than 60 bytes*/
2056 if (size < sizeof(ref.inode->blocks)) {
2057 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
2058 memcpy(ref.inode->blocks, buf, size);
2059 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
2061 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
2062 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
2066 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
2072 /*Stop write back cache mode*/
2073 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
2078 ext4_inode_set_size(ref.inode, size);
2086 ext4_fs_put_inode_ref(&ref);
2090 int ext4_fsymlink(const char *target, const char *path)
2092 struct ext4_mountpoint *mp = ext4_get_mount(path);
2100 filetype = EXT4_DE_SYMLINK;
2103 ext4_trans_start(mp);
2105 ext4_block_cache_write_back(mp->fs.bdev, 1);
2106 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
2108 r = ext4_fsymlink_set(&f, target, strlen(target));
2115 ext4_block_cache_write_back(mp->fs.bdev, 0);
2120 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
2122 struct ext4_mountpoint *mp = ext4_get_mount(path);
2133 filetype = EXT4_DE_SYMLINK;
2136 ext4_block_cache_write_back(mp->fs.bdev, 1);
2137 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
2139 r = ext4_fread(&f, buf, bufsize, rcnt);
2146 ext4_block_cache_write_back(mp->fs.bdev, 0);
2148 ext4_trans_abort(mp);
2150 ext4_trans_stop(mp);
2156 int ext4_setxattr(const char *path, const char *name, size_t name_len,
2157 const void *data, size_t data_size, bool replace)
2163 const char *dissected_name = NULL;
2164 size_t dissected_len = 0;
2165 struct ext4_xattr_ref xattr_ref;
2166 struct ext4_inode_ref inode_ref;
2167 struct ext4_mountpoint *mp = ext4_get_mount(path);
2171 dissected_name = ext4_extract_xattr_name(name, name_len,
2172 &name_index, &dissected_len);
2177 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2183 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2187 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2189 ext4_fs_put_inode_ref(&inode_ref);
2193 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2194 dissected_len, data, data_size, replace);
2196 ext4_fs_put_xattr_ref(&xattr_ref);
2197 ext4_fs_put_inode_ref(&inode_ref);
2203 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2204 void *buf, size_t buf_size, size_t *data_size)
2210 const char *dissected_name = NULL;
2211 size_t dissected_len = 0;
2212 struct ext4_xattr_ref xattr_ref;
2213 struct ext4_inode_ref inode_ref;
2214 struct ext4_mountpoint *mp = ext4_get_mount(path);
2218 dissected_name = ext4_extract_xattr_name(name, name_len,
2219 &name_index, &dissected_len);
2224 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2230 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2234 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2236 ext4_fs_put_inode_ref(&inode_ref);
2240 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2241 dissected_len, buf, buf_size, data_size);
2243 ext4_fs_put_xattr_ref(&xattr_ref);
2244 ext4_fs_put_inode_ref(&inode_ref);
2250 struct ext4_listxattr_iterator {
2255 bool list_too_small;
2256 bool get_required_size;
2259 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2260 struct ext4_xattr_item *item)
2262 struct ext4_listxattr_iterator *lxi;
2263 lxi = ref->iter_arg;
2264 if (!lxi->get_required_size) {
2267 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2268 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2269 lxi->list_too_small = 1;
2270 return EXT4_XATTR_ITERATE_STOP;
2273 memcpy(lxi->list_ptr, prefix, plen);
2274 lxi->list_ptr += plen;
2275 lxi->ret_size += plen;
2277 memcpy(lxi->list_ptr, item->name, item->name_len);
2278 lxi->list_ptr[item->name_len] = 0;
2279 lxi->list_ptr += item->name_len + 1;
2281 lxi->ret_size += item->name_len + 1;
2282 return EXT4_XATTR_ITERATE_CONT;
2285 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2290 struct ext4_xattr_ref xattr_ref;
2291 struct ext4_inode_ref inode_ref;
2292 struct ext4_listxattr_iterator lxi;
2293 struct ext4_mountpoint *mp = ext4_get_mount(path);
2298 lxi.list_ptr = list;
2301 lxi.list_too_small = false;
2302 lxi.get_required_size = (!size) ? true : false;
2305 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2311 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2315 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2317 ext4_fs_put_inode_ref(&inode_ref);
2321 xattr_ref.iter_arg = &lxi;
2322 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2323 if (lxi.list_too_small)
2328 *ret_size = lxi.ret_size;
2331 ext4_fs_put_xattr_ref(&xattr_ref);
2332 ext4_fs_put_inode_ref(&inode_ref);
2339 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2345 const char *dissected_name = NULL;
2346 size_t dissected_len = 0;
2347 struct ext4_xattr_ref xattr_ref;
2348 struct ext4_inode_ref inode_ref;
2349 struct ext4_mountpoint *mp = ext4_get_mount(path);
2353 dissected_name = ext4_extract_xattr_name(name, name_len,
2354 &name_index, &dissected_len);
2359 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2365 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2369 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2371 ext4_fs_put_inode_ref(&inode_ref);
2375 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2378 ext4_fs_put_xattr_ref(&xattr_ref);
2379 ext4_fs_put_inode_ref(&inode_ref);
2386 /*********************************DIRECTORY OPERATION************************/
2388 int ext4_dir_rm(const char *path)
2394 struct ext4_mountpoint *mp = ext4_get_mount(path);
2395 struct ext4_inode_ref act;
2396 struct ext4_inode_ref child;
2397 struct ext4_dir_iter it;
2401 uint32_t inode_current;
2412 ext4_trans_start(mp);
2414 struct ext4_fs *const fs = &mp->fs;
2417 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2419 ext4_trans_abort(mp);
2425 len = ext4_path_check(path, &is_goal);
2427 inode_current = f.inode;
2430 ext4_block_cache_write_back(mp->fs.bdev, 1);
2433 /*Load directory node.*/
2434 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2439 /*Initialize iterator.*/
2440 r = ext4_dir_iterator_init(&it, &act, 0);
2442 ext4_fs_put_inode_ref(&act);
2453 /*Get up directory inode when ".." entry*/
2454 if ((it.curr->name_len == 2) &&
2455 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2456 inode_up = ext4_dir_en_get_inode(it.curr);
2459 /*If directory or file entry, but not "." ".." entry*/
2460 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2462 /*Get child inode reference do unlink
2465 cinode = ext4_dir_en_get_inode(it.curr);
2466 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2470 /*If directory with no leaf children*/
2471 r = ext4_has_children(&has_children, &child);
2473 ext4_fs_put_inode_ref(&child);
2478 /*Has directory children. Go into this
2480 inode_up = inode_current;
2481 inode_current = cinode;
2483 ext4_fs_put_inode_ref(&child);
2487 /*No children in child directory or file. Just
2489 r = ext4_unlink(f.mp, &act, &child,
2490 (char *)it.curr->name,
2493 ext4_fs_put_inode_ref(&child);
2497 ext4_inode_set_del_time(child.inode, -1L);
2498 ext4_inode_set_links_cnt(child.inode, 0);
2501 r = ext4_fs_truncate_inode(&child, 0);
2503 ext4_fs_put_inode_ref(&child);
2507 r = ext4_fs_free_inode(&child);
2509 ext4_fs_put_inode_ref(&child);
2513 r = ext4_fs_put_inode_ref(&child);
2518 r = ext4_dir_iterator_next(&it);
2522 /*Directory iterator reached last entry*/
2523 ext4_has_children(&has_children, &act);
2524 if (!has_children) {
2525 inode_current = inode_up;
2532 struct ext4_inode_ref parent;
2533 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2538 /* In this place all directories should be
2540 * Last unlink from root of current directory*/
2541 r = ext4_unlink(f.mp, &parent, &act,
2544 ext4_fs_put_inode_ref(&parent);
2548 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2549 ext4_inode_set_del_time(act.inode, -1L);
2550 ext4_inode_set_links_cnt(act.inode, 0);
2553 r = ext4_fs_truncate_inode(&act, 0);
2555 ext4_fs_put_inode_ref(&parent);
2559 r = ext4_fs_free_inode(&act);
2561 ext4_fs_put_inode_ref(&parent);
2566 r = ext4_fs_put_inode_ref(&parent);
2573 ext4_dir_iterator_fini(&it);
2574 ext4_fs_put_inode_ref(&act);
2577 /*When something goes wrong. End loop.*/
2583 ext4_block_cache_write_back(mp->fs.bdev, 0);
2587 ext4_trans_abort(mp);
2589 ext4_trans_stop(mp);
2594 int ext4_dir_mk(const char *path)
2599 struct ext4_mountpoint *mp = ext4_get_mount(path);
2605 ext4_trans_start(mp);
2608 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2610 /*Directory already created*/
2611 ext4_trans_stop(mp);
2617 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2619 ext4_trans_abort(mp);
2624 ext4_trans_stop(mp);
2629 int ext4_dir_open(ext4_dir *d, const char *path)
2631 struct ext4_mountpoint *mp = ext4_get_mount(path);
2638 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2644 int ext4_dir_close(ext4_dir *d)
2646 return ext4_fclose(&d->f);
2649 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2651 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2654 ext4_direntry *de = 0;
2655 struct ext4_inode_ref dir;
2656 struct ext4_dir_iter it;
2658 EXT4_MP_LOCK(d->f.mp);
2660 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2661 EXT4_MP_UNLOCK(d->f.mp);
2665 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2670 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2672 ext4_fs_put_inode_ref(&dir);
2676 memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2679 ext4_dir_iterator_next(&it);
2681 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2683 ext4_dir_iterator_fini(&it);
2684 ext4_fs_put_inode_ref(&dir);
2687 EXT4_MP_UNLOCK(d->f.mp);
2691 void ext4_dir_entry_rewind(ext4_dir *d)
2696 int ext4_test_journal(const char *mount_point)
2698 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
2704 ext4_block_cache_write_back(mp->fs.bdev, 1);
2705 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
2706 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
2707 struct jbd_journal *journal;
2712 journal = calloc(1, sizeof(struct jbd_journal));
2719 r = jbd_get_fs(&mp->fs, jbd_fs);
2724 r = jbd_journal_start(jbd_fs, journal);
2733 for (i = 0;i < 50;i++) {
2734 ext4_fsblk_t rand_block = rand() % 4096;
2737 struct ext4_block block;
2738 r = ext4_block_get(mp->fs.bdev, &block, rand_block);
2742 struct jbd_trans *t = jbd_journal_new_trans(journal);
2744 ext4_block_set(mp->fs.bdev, &block);
2749 switch (rand() % 2) {
2751 r = jbd_trans_get_access(journal, t, &block);
2753 jbd_journal_free_trans(journal, t,
2755 ext4_block_set(mp->fs.bdev, &block);
2759 r = jbd_trans_set_block_dirty(t, &block);
2761 jbd_journal_free_trans(journal, t,
2763 ext4_block_set(mp->fs.bdev, &block);
2769 r = jbd_trans_try_revoke_block(t, rand_block);
2771 jbd_journal_free_trans(journal, t,
2773 ext4_block_set(mp->fs.bdev, &block);
2778 ext4_block_set(mp->fs.bdev, &block);
2779 jbd_journal_submit_trans(journal, t);
2780 jbd_journal_commit_one(journal);
2783 jbd_journal_stop(journal);
2790 ext4_block_cache_write_back(mp->fs.bdev, 0);