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 /**@brief Block devices descriptor.*/
89 struct _ext4_devices {
91 /**@brief Block device name (@ref ext4_device_register)*/
94 /**@brief Block device handle.*/
95 struct ext4_blockdev *bd;
97 /**@brief Block cache handle.*/
98 struct ext4_bcache *bc;
101 /**@brief Block devices.*/
102 struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
104 /**@brief Mountpoints.*/
105 struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
107 int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
108 const char *dev_name)
111 ext4_assert(bd && dev_name);
113 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
114 if (!_bdevices[i].bd) {
115 strcpy(_bdevices[i].name, dev_name);
116 _bdevices[i].bd = bd;
117 _bdevices[i].bc = bc;
121 if (!strcmp(_bdevices[i].name, dev_name))
127 /****************************************************************************/
129 static bool ext4_is_dots(const uint8_t *name, size_t name_size)
131 if ((name_size == 1) && (name[0] == '.'))
134 if ((name_size == 2) && (name[0] == '.') && (name[1] == '.'))
140 static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
142 struct ext4_sblock *sb = &enode->fs->sb;
144 /* Check if node is directory */
145 if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
146 *has_children = false;
150 struct ext4_dir_iter it;
151 int rc = ext4_dir_iterator_init(&it, enode, 0);
155 /* Find a non-empty directory entry */
157 while (it.curr != NULL) {
158 if (ext4_dir_en_get_inode(it.curr) != 0) {
160 nsize = ext4_dir_en_get_name_len(sb, it.curr);
161 if (!ext4_is_dots(it.curr->name, nsize)) {
167 rc = ext4_dir_iterator_next(&it);
169 ext4_dir_iterator_fini(&it);
174 rc = ext4_dir_iterator_fini(&it);
178 *has_children = found;
183 static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
184 struct ext4_inode_ref *ch, const char *n,
185 uint32_t len, bool rename)
187 /* Check maximum name length */
188 if (len > EXT4_DIRECTORY_FILENAME_LEN)
191 /* Add entry to parent directory */
192 int r = ext4_dir_add_entry(parent, n, len, ch);
196 /* Fill new dir -> add '.' and '..' entries.
197 * Also newly allocated inode should have 0 link count.
200 bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
201 EXT4_INODE_MODE_DIRECTORY);
202 if (is_dir && !rename) {
204 #if CONFIG_DIR_INDEX_ENABLE
205 /* Initialize directory index if supported */
206 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
207 r = ext4_dir_dx_init(ch, parent);
211 ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
216 r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
218 ext4_dir_remove_entry(parent, n, strlen(n));
222 r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
224 ext4_dir_remove_entry(parent, n, strlen(n));
225 ext4_dir_remove_entry(ch, ".", strlen("."));
230 /*New empty directory. Two links (. and ..) */
231 ext4_inode_set_links_cnt(ch->inode, 2);
232 ext4_fs_inode_links_count_inc(parent);
234 parent->dirty = true;
238 * In case we want to rename a directory,
239 * we reset the original '..' pointer.
243 idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
244 struct ext4_dir_search_result res;
246 r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
250 ext4_dir_en_set_inode(res.dentry, parent->index);
251 ext4_bcache_set_dirty(res.block.buf);
252 r = ext4_dir_destroy_result(ch, &res);
257 #if CONFIG_DIR_INDEX_ENABLE
258 r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
265 ext4_fs_inode_links_count_inc(parent);
266 parent->dirty = true;
269 ext4_fs_inode_links_count_inc(ch);
276 static int ext4_unlink(struct ext4_mountpoint *mp,
277 struct ext4_inode_ref *parent,
278 struct ext4_inode_ref *child, const char *name,
282 int rc = ext4_has_children(&has_children, child);
286 /* Cannot unlink non-empty node */
290 /* Remove entry from parent directory */
291 rc = ext4_dir_remove_entry(parent, name, name_len);
295 bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
296 EXT4_INODE_MODE_DIRECTORY);
298 /* If directory - handle links from parent */
300 ext4_fs_inode_links_count_dec(parent);
301 parent->dirty = true;
305 * TODO: Update timestamps of the parent
306 * (when we have wall-clock time).
308 * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now);
309 * ext4_inode_set_modification_time(parent->inode, (uint32_t) now);
310 * parent->dirty = true;
314 * TODO: Update timestamp for inode.
316 * ext4_inode_set_change_inode_time(child->inode,
319 if (ext4_inode_get_links_cnt(child->inode)) {
320 ext4_fs_inode_links_count_dec(child);
327 /****************************************************************************/
329 int ext4_mount(const char *dev_name, const char *mount_point)
331 ext4_assert(mount_point && dev_name);
336 struct ext4_blockdev *bd = 0;
337 struct ext4_bcache *bc = 0;
338 struct ext4_mountpoint *mp = 0;
340 if (mount_point[strlen(mount_point) - 1] != '/')
343 for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
344 if (_bdevices[i].name) {
345 if (!strcmp(dev_name, _bdevices[i].name)) {
346 bd = _bdevices[i].bd;
347 bc = _bdevices[i].bc;
356 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
357 if (!_mp[i].mounted) {
358 strcpy(_mp[i].name, mount_point);
364 if (!strcmp(_mp[i].name, mount_point))
371 r = ext4_block_init(bd);
375 r = ext4_fs_init(&mp->fs, bd);
381 bsize = ext4_sb_get_block_size(&mp->fs.sb);
382 ext4_block_set_lb_size(bd, bsize);
384 mp->cache_dynamic = 0;
387 /*Automatic block cache alloc.*/
388 mp->cache_dynamic = 1;
389 bc = malloc(sizeof(struct ext4_bcache));
391 r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE,
400 if (bsize != bc->itemsize)
403 /*Bind block cache to block device*/
404 r = ext4_block_bind_bcache(bd, bc);
406 ext4_bcache_cleanup(bc);
408 if (mp->cache_dynamic) {
409 ext4_bcache_fini_dynamic(bc);
419 int ext4_umount(const char *mount_point)
423 struct ext4_mountpoint *mp = 0;
425 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
426 if (!strcmp(_mp[i].name, mount_point)) {
435 r = ext4_fs_fini(&mp->fs);
441 ext4_bcache_cleanup(mp->fs.bdev->bc);
442 if (mp->cache_dynamic) {
443 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
444 free(mp->fs.bdev->bc);
447 return ext4_block_fini(mp->fs.bdev);
450 static struct ext4_mountpoint *ext4_get_mount(const char *path)
453 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
458 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
464 int ext4_recover(const char *mount_point)
466 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
472 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
473 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
480 r = jbd_get_fs(&mp->fs, jbd_fs);
486 r = jbd_recover(jbd_fs);
498 int ext4_mount_point_stats(const char *mount_point,
499 struct ext4_mount_stats *stats)
501 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
507 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
508 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
509 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
510 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
511 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
513 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
514 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
515 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
517 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
523 int ext4_mount_setup_locks(const char *mount_point,
524 const struct ext4_lock *locks)
527 struct ext4_mountpoint *mp = 0;
529 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
530 if (!strcmp(_mp[i].name, mount_point)) {
538 mp->os_locks = locks;
542 /********************************FILE OPERATIONS*****************************/
544 static int ext4_path_check(const char *path, bool *is_goal)
548 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
550 if (path[i] == '/') {
564 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
569 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
570 *file_flags = O_RDONLY;
574 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
575 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
579 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
580 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
584 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
585 !strcmp(flags, "r+b")) {
586 *file_flags = O_RDWR;
590 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
591 !strcmp(flags, "w+b")) {
592 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
596 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
597 !strcmp(flags, "a+b")) {
598 *file_flags = O_RDWR | O_CREAT | O_APPEND;
606 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
607 * any filetype of the target dir entry will be accepted.
609 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
610 int ftype, uint32_t *parent_inode,
613 bool is_goal = false;
614 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
619 struct ext4_mountpoint *mp = ext4_get_mount(path);
620 struct ext4_dir_search_result result;
621 struct ext4_inode_ref ref;
628 struct ext4_fs *const fs = &mp->fs;
629 struct ext4_sblock *const sb = &mp->fs.sb;
634 path += strlen(mp->name);
637 *name_off = strlen(mp->name);
640 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
645 *parent_inode = ref.index;
647 len = ext4_path_check(path, &is_goal);
650 len = ext4_path_check(path, &is_goal);
652 /*If root open was request.*/
653 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
661 r = ext4_dir_find_entry(&result, &ref, path, len);
664 /*Destroy last result*/
665 ext4_dir_destroy_result(&ref, &result);
669 if (!(f->flags & O_CREAT))
672 /*O_CREAT allows create new entry*/
673 struct ext4_inode_ref child_ref;
674 r = ext4_fs_alloc_inode(fs, &child_ref,
675 is_goal ? ftype : EXT4_DE_DIR);
680 /*Link with root dir.*/
681 r = ext4_link(mp, &ref, &child_ref, path, len, false);
683 /*Fail. Free new inode.*/
684 ext4_fs_free_inode(&child_ref);
685 /*We do not want to write new inode.
686 But block has to be released.*/
687 child_ref.dirty = false;
688 ext4_fs_put_inode_ref(&child_ref);
692 ext4_fs_put_inode_ref(&child_ref);
697 *parent_inode = ref.index;
699 next_inode = ext4_dir_en_get_inode(result.dentry);
700 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
702 t = ext4_dir_en_get_inode_type(sb, result.dentry);
703 imode = ext4_fs_correspond_inode_mode(t);
705 struct ext4_inode_ref child_ref;
706 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
710 imode = ext4_inode_type(sb, child_ref.inode);
711 ext4_fs_put_inode_ref(&child_ref);
714 r = ext4_dir_destroy_result(&ref, &result);
718 /*If expected file error*/
719 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
723 if (ftype != EXT4_DE_UNKNOWN) {
724 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
731 r = ext4_fs_put_inode_ref(&ref);
735 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
745 *name_off += len + 1;
749 ext4_fs_put_inode_ref(&ref);
755 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
756 r = ext4_fs_truncate_inode(&ref, 0);
758 ext4_fs_put_inode_ref(&ref);
764 f->fsize = ext4_inode_get_size(sb, ref.inode);
765 f->inode = ref.index;
768 if (f->flags & O_APPEND)
773 r = ext4_fs_put_inode_ref(&ref);
777 /****************************************************************************/
779 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
780 bool file_expect, uint32_t *parent_inode,
785 if (ext4_parse_flags(flags, &iflags) == false)
788 if (file_expect == true)
789 filetype = EXT4_DE_REG_FILE;
791 filetype = EXT4_DE_DIR;
793 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
797 static int ext4_create_hardlink(const char *path,
798 struct ext4_inode_ref *child_ref, bool rename)
800 bool is_goal = false;
801 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
806 struct ext4_mountpoint *mp = ext4_get_mount(path);
807 struct ext4_dir_search_result result;
808 struct ext4_inode_ref ref;
813 struct ext4_fs *const fs = &mp->fs;
814 struct ext4_sblock *const sb = &mp->fs.sb;
817 path += strlen(mp->name);
820 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
824 len = ext4_path_check(path, &is_goal);
827 len = ext4_path_check(path, &is_goal);
829 /*If root open was request.*/
830 r = is_goal ? EINVAL : ENOENT;
834 r = ext4_dir_find_entry(&result, &ref, path, len);
837 /*Destroy last result*/
838 ext4_dir_destroy_result(&ref, &result);
840 if (r != ENOENT || !is_goal)
843 /*Link with root dir.*/
844 r = ext4_link(mp, &ref, child_ref, path, len, rename);
846 } else if (r == EOK && is_goal) {
847 /*Destroy last result*/
848 ext4_dir_destroy_result(&ref, &result);
853 next_inode = result.dentry->inode;
854 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
856 t = ext4_dir_en_get_inode_type(sb, result.dentry);
857 inode_mode = ext4_fs_correspond_inode_mode(t);
859 struct ext4_inode_ref child_ref;
860 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
864 inode_mode = ext4_inode_type(sb, child_ref.inode);
865 ext4_fs_put_inode_ref(&child_ref);
868 r = ext4_dir_destroy_result(&ref, &result);
872 if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
873 r = is_goal ? EEXIST : ENOENT;
877 r = ext4_fs_put_inode_ref(&ref);
881 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
892 ext4_fs_put_inode_ref(&ref);
896 r = ext4_fs_put_inode_ref(&ref);
900 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
901 struct ext4_inode_ref *parent_ref,
902 struct ext4_inode_ref *child_ref)
907 struct ext4_mountpoint *mp = ext4_get_mount(path);
915 len = ext4_path_check(path, &is_goal);
917 /* Remove entry from parent directory */
918 r = ext4_dir_remove_entry(parent_ref, path, len);
922 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
923 EXT4_INODE_MODE_DIRECTORY)) {
924 ext4_fs_inode_links_count_dec(parent_ref);
925 parent_ref->dirty = true;
931 int ext4_flink(const char *path, const char *hardlink_path)
936 bool child_loaded = false;
937 uint32_t parent_inode, child_inode;
938 struct ext4_mountpoint *mp = ext4_get_mount(path);
939 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
940 struct ext4_inode_ref child_ref;
945 /* Will that happen? Anyway return EINVAL for such case. */
951 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
952 &parent_inode, &name_off);
956 child_inode = f.inode;
959 /*We have file to unlink. Load it.*/
960 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
966 /* Creating hardlink for directory is not allowed. */
967 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
968 EXT4_INODE_MODE_DIRECTORY)) {
973 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
977 ext4_fs_put_inode_ref(&child_ref);
984 int ext4_frename(const char *path, const char *new_path)
989 bool parent_loaded = false, child_loaded = false;
990 uint32_t parent_inode, child_inode;
991 struct ext4_mountpoint *mp = ext4_get_mount(path);
992 struct ext4_inode_ref child_ref, parent_ref;
999 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
1000 &parent_inode, &name_off);
1004 child_inode = f.inode;
1008 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1012 parent_loaded = true;
1014 /*We have file to unlink. Load it.*/
1015 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1019 child_loaded = true;
1021 r = ext4_create_hardlink(new_path, &child_ref, true);
1025 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1031 ext4_fs_put_inode_ref(&parent_ref);
1034 ext4_fs_put_inode_ref(&child_ref);
1041 /****************************************************************************/
1043 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1045 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1054 int ext4_cache_write_back(const char *path, bool on)
1056 struct ext4_mountpoint *mp = ext4_get_mount(path);
1062 ext4_block_cache_write_back(mp->fs.bdev, on);
1067 int ext4_fremove(const char *path)
1070 uint32_t parent_inode;
1075 struct ext4_inode_ref child;
1076 struct ext4_inode_ref parent;
1077 struct ext4_mountpoint *mp = ext4_get_mount(path);
1083 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1084 &parent_inode, &name_off);
1091 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1097 /*We have file to delete. Load it.*/
1098 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1100 ext4_fs_put_inode_ref(&parent);
1108 len = ext4_path_check(path, &is_goal);
1110 /*Unlink from parent*/
1111 r = ext4_unlink(mp, &parent, &child, path, len);
1115 /*Link count is zero, the inode should be freed. */
1116 if (!ext4_inode_get_links_cnt(child.inode)) {
1117 ext4_inode_set_del_time(child.inode, -1L);
1119 ext4_block_cache_write_back(mp->fs.bdev, 1);
1120 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1121 r = ext4_fs_truncate_inode(&child, 0);
1122 ext4_block_cache_write_back(mp->fs.bdev, 0);
1127 r = ext4_fs_free_inode(&child);
1133 ext4_fs_put_inode_ref(&child);
1134 ext4_fs_put_inode_ref(&parent);
1139 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1140 struct ext4_inode *inode)
1144 struct ext4_inode_ref inode_ref;
1145 struct ext4_mountpoint *mp = ext4_get_mount(path);
1153 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1163 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1169 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1170 ext4_fs_put_inode_ref(&inode_ref);
1179 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1181 struct ext4_mountpoint *mp = ext4_get_mount(path);
1188 ext4_block_cache_write_back(mp->fs.bdev, 1);
1189 r = ext4_generic_open(f, path, flags, true, 0, 0);
1190 ext4_block_cache_write_back(mp->fs.bdev, 0);
1195 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1197 struct ext4_mountpoint *mp = ext4_get_mount(path);
1204 filetype = EXT4_DE_REG_FILE;
1207 ext4_block_cache_write_back(mp->fs.bdev, 1);
1208 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1209 ext4_block_cache_write_back(mp->fs.bdev, 0);
1214 int ext4_fclose(ext4_file *f)
1216 ext4_assert(f && f->mp);
1221 f->fpos = f->fsize = 0;
1226 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1228 struct ext4_inode_ref ref;
1232 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1234 EXT4_MP_UNLOCK(f->mp);
1239 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1240 if (f->fsize <= size) {
1245 /*Start write back cache mode.*/
1246 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1250 r = ext4_fs_truncate_inode(&ref, size);
1258 /*Stop write back cache mode*/
1259 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1265 ext4_fs_put_inode_ref(&ref);
1270 int ext4_ftruncate(ext4_file *f, uint64_t size)
1273 ext4_assert(f && f->mp);
1275 if (f->flags & O_RDONLY)
1278 EXT4_MP_LOCK(f->mp);
1279 r = ext4_ftruncate_no_lock(f, size);
1280 EXT4_MP_UNLOCK(f->mp);
1284 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1287 uint32_t iblock_idx;
1288 uint32_t iblock_last;
1289 uint32_t block_size;
1291 ext4_fsblk_t fblock;
1292 ext4_fsblk_t fblock_start;
1293 uint32_t fblock_count;
1295 uint8_t *u8_buf = buf;
1297 struct ext4_inode_ref ref;
1299 ext4_assert(f && f->mp);
1301 if (f->flags & O_WRONLY)
1307 EXT4_MP_LOCK(f->mp);
1309 struct ext4_fs *const fs = &f->mp->fs;
1310 struct ext4_sblock *const sb = &f->mp->fs.sb;
1315 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1317 EXT4_MP_UNLOCK(f->mp);
1322 f->fsize = ext4_inode_get_size(sb, ref.inode);
1324 block_size = ext4_sb_get_block_size(sb);
1325 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1327 iblock_idx = (f->fpos) / block_size;
1328 iblock_last = (f->fpos + size) / block_size;
1329 unalg = (f->fpos) % block_size;
1331 /*If the size of symlink is smaller than 60 bytes*/
1333 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1334 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1335 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1337 char *content = (char *)ref.inode->blocks;
1338 if (f->fpos < f->fsize) {
1340 if (unalg + size > f->fsize)
1341 len = f->fsize - unalg;
1342 memcpy(buf, content + unalg, len);
1354 if (size > (block_size - unalg))
1355 len = block_size - unalg;
1357 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1361 /* Do we get an unwritten range? */
1363 uint64_t off = fblock * block_size + unalg;
1364 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
1370 memset(u8_buf, 0, len);
1385 while (size >= block_size) {
1386 while (iblock_idx < iblock_last) {
1387 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1395 fblock_start = fblock;
1397 if ((fblock_start + fblock_count) != fblock)
1403 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1408 size -= block_size * fblock_count;
1409 u8_buf += block_size * fblock_count;
1410 f->fpos += block_size * fblock_count;
1413 *rcnt += block_size * fblock_count;
1415 fblock_start = fblock;
1421 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1425 off = fblock * block_size;
1426 r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
1437 ext4_fs_put_inode_ref(&ref);
1438 EXT4_MP_UNLOCK(f->mp);
1442 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1446 uint32_t iblock_last;
1447 uint32_t ifile_blocks;
1448 uint32_t block_size;
1450 uint32_t fblock_count;
1452 ext4_fsblk_t fblock_start;
1454 struct ext4_inode_ref ref;
1455 const uint8_t *u8_buf = buf;
1458 ext4_assert(f && f->mp);
1460 if (f->flags & O_RDONLY)
1466 EXT4_MP_LOCK(f->mp);
1468 struct ext4_fs *const fs = &f->mp->fs;
1469 struct ext4_sblock *const sb = &f->mp->fs.sb;
1474 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1476 EXT4_MP_UNLOCK(f->mp);
1481 f->fsize = ext4_inode_get_size(sb, ref.inode);
1482 block_size = ext4_sb_get_block_size(sb);
1484 iblock_last = (f->fpos + size) / block_size;
1485 iblk_idx = (f->fpos) / block_size;
1486 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1488 unalg = (f->fpos) % block_size;
1493 if (size > (block_size - unalg))
1494 len = block_size - unalg;
1496 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1500 off = fblk * block_size + unalg;
1501 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
1515 /*Start write back cache mode.*/
1516 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1522 while (size >= block_size) {
1524 while (iblk_idx < iblock_last) {
1525 if (iblk_idx < ifile_blocks) {
1526 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1531 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1534 /* Unable to append more blocks. But
1535 * some block might be allocated already
1543 if (!fblock_start) {
1544 fblock_start = fblk;
1547 if ((fblock_start + fblock_count) != fblk)
1553 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1558 size -= block_size * fblock_count;
1559 u8_buf += block_size * fblock_count;
1560 f->fpos += block_size * fblock_count;
1563 *wcnt += block_size * fblock_count;
1565 fblock_start = fblk;
1569 /*ext4_fs_append_inode_block has failed and no
1570 * more blocks might be written. But node size
1571 * should be updated.*/
1577 /*Stop write back cache mode*/
1578 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1585 if (iblk_idx < ifile_blocks) {
1586 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1590 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1592 /*Node size sholud be updated.*/
1596 off = fblk * block_size;
1597 r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
1608 if (f->fpos > f->fsize) {
1610 ext4_inode_set_size(ref.inode, f->fsize);
1615 ext4_fs_put_inode_ref(&ref);
1616 EXT4_MP_UNLOCK(f->mp);
1620 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1624 if (offset > f->fsize)
1630 if ((offset + f->fpos) > f->fsize)
1636 if (offset > f->fsize)
1639 f->fpos = f->fsize - offset;
1645 uint64_t ext4_ftell(ext4_file *f)
1650 uint64_t ext4_fsize(ext4_file *f)
1655 int ext4_chmod(const char *path, uint32_t mode)
1660 struct ext4_sblock *sb;
1661 struct ext4_inode_ref inode_ref;
1662 struct ext4_mountpoint *mp = ext4_get_mount(path);
1669 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1677 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1683 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1684 inode_ref.dirty = true;
1686 ext4_fs_put_inode_ref(&inode_ref);
1691 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1696 struct ext4_inode_ref inode_ref;
1697 struct ext4_mountpoint *mp = ext4_get_mount(path);
1704 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1711 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1717 ext4_inode_set_uid(inode_ref.inode, uid);
1718 ext4_inode_set_gid(inode_ref.inode, gid);
1719 inode_ref.dirty = true;
1721 ext4_fs_put_inode_ref(&inode_ref);
1726 int ext4_file_set_atime(const char *path, uint32_t atime)
1731 struct ext4_inode_ref inode_ref;
1732 struct ext4_mountpoint *mp = ext4_get_mount(path);
1739 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1746 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1752 ext4_inode_set_access_time(inode_ref.inode, atime);
1753 inode_ref.dirty = true;
1755 ext4_fs_put_inode_ref(&inode_ref);
1760 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1765 struct ext4_inode_ref inode_ref;
1766 struct ext4_mountpoint *mp = ext4_get_mount(path);
1773 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1780 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1786 ext4_inode_set_modif_time(inode_ref.inode, mtime);
1787 inode_ref.dirty = true;
1789 ext4_fs_put_inode_ref(&inode_ref);
1794 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1799 struct ext4_inode_ref inode_ref;
1800 struct ext4_mountpoint *mp = ext4_get_mount(path);
1807 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1814 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1820 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1821 inode_ref.dirty = true;
1823 ext4_fs_put_inode_ref(&inode_ref);
1828 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1830 struct ext4_inode_ref ref;
1832 ext4_fsblk_t fblock;
1833 uint32_t block_size;
1836 ext4_assert(f && f->mp);
1841 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1846 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1847 if (size > block_size) {
1851 r = ext4_ftruncate_no_lock(f, 0);
1855 /*Start write back cache mode.*/
1856 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1860 /*If the size of symlink is smaller than 60 bytes*/
1861 if (size < sizeof(ref.inode->blocks)) {
1862 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1863 memcpy(ref.inode->blocks, buf, size);
1864 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1866 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1867 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
1871 r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
1877 /*Stop write back cache mode*/
1878 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1883 ext4_inode_set_size(ref.inode, size);
1891 ext4_fs_put_inode_ref(&ref);
1895 int ext4_fsymlink(const char *target, const char *path)
1897 struct ext4_mountpoint *mp = ext4_get_mount(path);
1905 filetype = EXT4_DE_SYMLINK;
1908 ext4_block_cache_write_back(mp->fs.bdev, 1);
1909 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
1911 r = ext4_fsymlink_set(&f, target, strlen(target));
1918 ext4_block_cache_write_back(mp->fs.bdev, 0);
1923 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1925 struct ext4_mountpoint *mp = ext4_get_mount(path);
1936 filetype = EXT4_DE_SYMLINK;
1939 ext4_block_cache_write_back(mp->fs.bdev, 1);
1940 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
1942 r = ext4_fread(&f, buf, bufsize, rcnt);
1949 ext4_block_cache_write_back(mp->fs.bdev, 0);
1954 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1955 const void *data, size_t data_size, bool replace)
1961 const char *dissected_name = NULL;
1962 size_t dissected_len = 0;
1963 struct ext4_xattr_ref xattr_ref;
1964 struct ext4_inode_ref inode_ref;
1965 struct ext4_mountpoint *mp = ext4_get_mount(path);
1969 dissected_name = ext4_extract_xattr_name(name, name_len,
1970 &name_index, &dissected_len);
1975 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1981 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
1985 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
1987 ext4_fs_put_inode_ref(&inode_ref);
1991 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
1992 dissected_len, data, data_size, replace);
1994 ext4_fs_put_xattr_ref(&xattr_ref);
1995 ext4_fs_put_inode_ref(&inode_ref);
2001 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2002 void *buf, size_t buf_size, size_t *data_size)
2008 const char *dissected_name = NULL;
2009 size_t dissected_len = 0;
2010 struct ext4_xattr_ref xattr_ref;
2011 struct ext4_inode_ref inode_ref;
2012 struct ext4_mountpoint *mp = ext4_get_mount(path);
2016 dissected_name = ext4_extract_xattr_name(name, name_len,
2017 &name_index, &dissected_len);
2022 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2028 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2032 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2034 ext4_fs_put_inode_ref(&inode_ref);
2038 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2039 dissected_len, buf, buf_size, data_size);
2041 ext4_fs_put_xattr_ref(&xattr_ref);
2042 ext4_fs_put_inode_ref(&inode_ref);
2048 struct ext4_listxattr_iterator {
2053 bool list_too_small;
2054 bool get_required_size;
2057 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2058 struct ext4_xattr_item *item)
2060 struct ext4_listxattr_iterator *lxi;
2061 lxi = ref->iter_arg;
2062 if (!lxi->get_required_size) {
2065 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2066 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2067 lxi->list_too_small = 1;
2068 return EXT4_XATTR_ITERATE_STOP;
2071 memcpy(lxi->list_ptr, prefix, plen);
2072 lxi->list_ptr += plen;
2073 lxi->ret_size += plen;
2075 memcpy(lxi->list_ptr, item->name, item->name_len);
2076 lxi->list_ptr[item->name_len] = 0;
2077 lxi->list_ptr += item->name_len + 1;
2079 lxi->ret_size += item->name_len + 1;
2080 return EXT4_XATTR_ITERATE_CONT;
2083 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2088 struct ext4_xattr_ref xattr_ref;
2089 struct ext4_inode_ref inode_ref;
2090 struct ext4_listxattr_iterator lxi;
2091 struct ext4_mountpoint *mp = ext4_get_mount(path);
2096 lxi.list_ptr = list;
2099 lxi.list_too_small = false;
2100 lxi.get_required_size = (!size) ? true : false;
2103 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2109 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2113 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2115 ext4_fs_put_inode_ref(&inode_ref);
2119 xattr_ref.iter_arg = &lxi;
2120 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2121 if (lxi.list_too_small)
2126 *ret_size = lxi.ret_size;
2129 ext4_fs_put_xattr_ref(&xattr_ref);
2130 ext4_fs_put_inode_ref(&inode_ref);
2137 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2143 const char *dissected_name = NULL;
2144 size_t dissected_len = 0;
2145 struct ext4_xattr_ref xattr_ref;
2146 struct ext4_inode_ref inode_ref;
2147 struct ext4_mountpoint *mp = ext4_get_mount(path);
2151 dissected_name = ext4_extract_xattr_name(name, name_len,
2152 &name_index, &dissected_len);
2157 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2163 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2167 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2169 ext4_fs_put_inode_ref(&inode_ref);
2173 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2176 ext4_fs_put_xattr_ref(&xattr_ref);
2177 ext4_fs_put_inode_ref(&inode_ref);
2184 /*********************************DIRECTORY OPERATION************************/
2186 int ext4_dir_rm(const char *path)
2192 struct ext4_mountpoint *mp = ext4_get_mount(path);
2193 struct ext4_inode_ref act;
2194 struct ext4_inode_ref child;
2195 struct ext4_dir_iter it;
2199 uint32_t inode_current;
2211 struct ext4_fs *const fs = &mp->fs;
2214 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2221 len = ext4_path_check(path, &is_goal);
2223 inode_current = f.inode;
2226 ext4_block_cache_write_back(mp->fs.bdev, 1);
2229 /*Load directory node.*/
2230 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2235 /*Initialize iterator.*/
2236 r = ext4_dir_iterator_init(&it, &act, 0);
2238 ext4_fs_put_inode_ref(&act);
2249 /*Get up directory inode when ".." entry*/
2250 if ((it.curr->name_len == 2) &&
2251 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2252 inode_up = ext4_dir_en_get_inode(it.curr);
2255 /*If directory or file entry, but not "." ".." entry*/
2256 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2258 /*Get child inode reference do unlink
2261 cinode = ext4_dir_en_get_inode(it.curr);
2262 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2266 /*If directory with no leaf children*/
2267 r = ext4_has_children(&has_children, &child);
2269 ext4_fs_put_inode_ref(&child);
2274 /*Has directory children. Go into this
2276 inode_up = inode_current;
2277 inode_current = cinode;
2279 ext4_fs_put_inode_ref(&child);
2283 /*No children in child directory or file. Just
2285 r = ext4_unlink(f.mp, &act, &child,
2286 (char *)it.curr->name,
2289 ext4_fs_put_inode_ref(&child);
2293 ext4_inode_set_del_time(child.inode, -1L);
2294 ext4_inode_set_links_cnt(child.inode, 0);
2297 r = ext4_fs_truncate_inode(&child, 0);
2299 ext4_fs_put_inode_ref(&child);
2303 r = ext4_fs_free_inode(&child);
2305 ext4_fs_put_inode_ref(&child);
2309 r = ext4_fs_put_inode_ref(&child);
2314 r = ext4_dir_iterator_next(&it);
2318 /*Directory iterator reached last entry*/
2319 ext4_has_children(&has_children, &act);
2320 if (!has_children) {
2321 inode_current = inode_up;
2328 struct ext4_inode_ref parent;
2329 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2334 /* In this place all directories should be
2336 * Last unlink from root of current directory*/
2337 r = ext4_unlink(f.mp, &parent, &act,
2340 ext4_fs_put_inode_ref(&parent);
2344 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2345 ext4_inode_set_del_time(act.inode, -1L);
2346 ext4_inode_set_links_cnt(act.inode, 0);
2349 r = ext4_fs_truncate_inode(&act, 0);
2351 ext4_fs_put_inode_ref(&parent);
2355 r = ext4_fs_free_inode(&act);
2357 ext4_fs_put_inode_ref(&parent);
2362 r = ext4_fs_put_inode_ref(&parent);
2369 ext4_dir_iterator_fini(&it);
2370 ext4_fs_put_inode_ref(&act);
2373 /*When something goes wrong. End loop.*/
2379 ext4_block_cache_write_back(mp->fs.bdev, 0);
2384 int ext4_dir_mk(const char *path)
2389 struct ext4_mountpoint *mp = ext4_get_mount(path);
2397 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2399 /*Directory already created*/
2405 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2415 int ext4_dir_open(ext4_dir *d, const char *path)
2417 struct ext4_mountpoint *mp = ext4_get_mount(path);
2424 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2430 int ext4_dir_close(ext4_dir *d)
2432 return ext4_fclose(&d->f);
2435 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2437 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2440 ext4_direntry *de = 0;
2441 struct ext4_inode_ref dir;
2442 struct ext4_dir_iter it;
2444 EXT4_MP_LOCK(d->f.mp);
2446 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2447 EXT4_MP_UNLOCK(d->f.mp);
2451 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2456 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2458 ext4_fs_put_inode_ref(&dir);
2462 memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2465 ext4_dir_iterator_next(&it);
2467 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2469 ext4_dir_iterator_fini(&it);
2470 ext4_fs_put_inode_ref(&dir);
2473 EXT4_MP_UNLOCK(d->f.mp);
2477 void ext4_dir_entry_rewind(ext4_dir *d)
2482 int ext4_test_journal(const char *mount_point)
2484 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
2490 ext4_block_cache_write_back(mp->fs.bdev, 1);
2491 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
2492 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
2493 struct jbd_journal *journal;
2498 journal = calloc(1, sizeof(struct jbd_journal));
2505 r = jbd_get_fs(&mp->fs, jbd_fs);
2510 r = jbd_journal_start(jbd_fs, journal);
2519 for (i = 0;i < 50;i++) {
2520 ext4_fsblk_t rand_block = rand() % 4096;
2523 struct ext4_block block;
2524 r = ext4_block_get(mp->fs.bdev, &block, rand_block);
2528 struct jbd_trans *t = jbd_journal_new_trans(journal);
2530 ext4_block_set(mp->fs.bdev, &block);
2535 switch (rand() % 2) {
2537 r = jbd_trans_get_access(journal, t, &block);
2539 jbd_journal_free_trans(journal, t,
2541 ext4_block_set(mp->fs.bdev, &block);
2545 r = jbd_trans_set_block_dirty(t, &block);
2547 jbd_journal_free_trans(journal, t,
2549 ext4_block_set(mp->fs.bdev, &block);
2555 r = jbd_trans_try_revoke_block(t, rand_block);
2557 jbd_journal_free_trans(journal, t,
2559 ext4_block_set(mp->fs.bdev, &block);
2564 ext4_block_set(mp->fs.bdev, &block);
2565 jbd_journal_submit_trans(journal, t);
2566 jbd_journal_commit_one(journal);
2569 jbd_journal_stop(journal);
2576 ext4_block_cache_write_back(mp->fs.bdev, 0);