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);
407 if (mp->cache_dynamic) {
408 ext4_bcache_fini_dynamic(bc);
418 int ext4_umount(const char *mount_point)
422 struct ext4_mountpoint *mp = 0;
424 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
425 if (!strcmp(_mp[i].name, mount_point)) {
434 r = ext4_fs_fini(&mp->fs);
440 if (mp->cache_dynamic) {
441 ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
442 free(mp->fs.bdev->bc);
445 return ext4_block_fini(mp->fs.bdev);
448 static struct ext4_mountpoint *ext4_get_mount(const char *path)
451 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
456 if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
462 int ext4_recover(const char *mount_point)
464 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
470 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
471 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
478 r = jbd_get_fs(&mp->fs, jbd_fs);
484 r = jbd_recover(jbd_fs);
496 int ext4_mount_point_stats(const char *mount_point,
497 struct ext4_mount_stats *stats)
499 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
505 stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
506 stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
507 stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
508 stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
509 stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
511 stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
512 stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
513 stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
515 memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
521 int ext4_mount_setup_locks(const char *mount_point,
522 const struct ext4_lock *locks)
525 struct ext4_mountpoint *mp = 0;
527 for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
528 if (!strcmp(_mp[i].name, mount_point)) {
536 mp->os_locks = locks;
540 /********************************FILE OPERATIONS*****************************/
542 static int ext4_path_check(const char *path, bool *is_goal)
546 for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
548 if (path[i] == '/') {
562 static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
567 if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
568 *file_flags = O_RDONLY;
572 if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
573 *file_flags = O_WRONLY | O_CREAT | O_TRUNC;
577 if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
578 *file_flags = O_WRONLY | O_CREAT | O_APPEND;
582 if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
583 !strcmp(flags, "r+b")) {
584 *file_flags = O_RDWR;
588 if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
589 !strcmp(flags, "w+b")) {
590 *file_flags = O_RDWR | O_CREAT | O_TRUNC;
594 if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
595 !strcmp(flags, "a+b")) {
596 *file_flags = O_RDWR | O_CREAT | O_APPEND;
604 * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
605 * any filetype of the target dir entry will be accepted.
607 static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
608 int ftype, uint32_t *parent_inode,
611 bool is_goal = false;
612 uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
617 struct ext4_mountpoint *mp = ext4_get_mount(path);
618 struct ext4_dir_search_result result;
619 struct ext4_inode_ref ref;
626 struct ext4_fs *const fs = &mp->fs;
627 struct ext4_sblock *const sb = &mp->fs.sb;
632 path += strlen(mp->name);
635 *name_off = strlen(mp->name);
638 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
643 *parent_inode = ref.index;
645 len = ext4_path_check(path, &is_goal);
648 len = ext4_path_check(path, &is_goal);
650 /*If root open was request.*/
651 if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
659 r = ext4_dir_find_entry(&result, &ref, path, len);
662 /*Destroy last result*/
663 ext4_dir_destroy_result(&ref, &result);
667 if (!(f->flags & O_CREAT))
670 /*O_CREAT allows create new entry*/
671 struct ext4_inode_ref child_ref;
672 r = ext4_fs_alloc_inode(fs, &child_ref,
673 is_goal ? ftype : EXT4_DE_DIR);
678 /*Link with root dir.*/
679 r = ext4_link(mp, &ref, &child_ref, path, len, false);
681 /*Fail. Free new inode.*/
682 ext4_fs_free_inode(&child_ref);
683 /*We do not want to write new inode.
684 But block has to be released.*/
685 child_ref.dirty = false;
686 ext4_fs_put_inode_ref(&child_ref);
690 ext4_fs_put_inode_ref(&child_ref);
695 *parent_inode = ref.index;
697 next_inode = ext4_dir_en_get_inode(result.dentry);
698 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
700 t = ext4_dir_en_get_inode_type(sb, result.dentry);
701 imode = ext4_fs_correspond_inode_mode(t);
703 struct ext4_inode_ref child_ref;
704 r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
708 imode = ext4_inode_type(sb, child_ref.inode);
709 ext4_fs_put_inode_ref(&child_ref);
712 r = ext4_dir_destroy_result(&ref, &result);
716 /*If expected file error*/
717 if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
721 if (ftype != EXT4_DE_UNKNOWN) {
722 bool df = imode != ext4_fs_correspond_inode_mode(ftype);
729 r = ext4_fs_put_inode_ref(&ref);
733 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
743 *name_off += len + 1;
747 ext4_fs_put_inode_ref(&ref);
753 if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
754 r = ext4_fs_truncate_inode(&ref, 0);
756 ext4_fs_put_inode_ref(&ref);
762 f->fsize = ext4_inode_get_size(sb, ref.inode);
763 f->inode = ref.index;
766 if (f->flags & O_APPEND)
771 r = ext4_fs_put_inode_ref(&ref);
775 /****************************************************************************/
777 static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
778 bool file_expect, uint32_t *parent_inode,
783 if (ext4_parse_flags(flags, &iflags) == false)
786 if (file_expect == true)
787 filetype = EXT4_DE_REG_FILE;
789 filetype = EXT4_DE_DIR;
791 return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
795 static int ext4_create_hardlink(const char *path,
796 struct ext4_inode_ref *child_ref, bool rename)
798 bool is_goal = false;
799 uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
804 struct ext4_mountpoint *mp = ext4_get_mount(path);
805 struct ext4_dir_search_result result;
806 struct ext4_inode_ref ref;
811 struct ext4_fs *const fs = &mp->fs;
812 struct ext4_sblock *const sb = &mp->fs.sb;
815 path += strlen(mp->name);
818 r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
822 len = ext4_path_check(path, &is_goal);
825 len = ext4_path_check(path, &is_goal);
827 /*If root open was request.*/
828 r = is_goal ? EINVAL : ENOENT;
832 r = ext4_dir_find_entry(&result, &ref, path, len);
835 /*Destroy last result*/
836 ext4_dir_destroy_result(&ref, &result);
838 if (r != ENOENT || !is_goal)
841 /*Link with root dir.*/
842 r = ext4_link(mp, &ref, child_ref, path, len, rename);
844 } else if (r == EOK && is_goal) {
845 /*Destroy last result*/
846 ext4_dir_destroy_result(&ref, &result);
851 next_inode = result.dentry->inode;
852 if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
854 t = ext4_dir_en_get_inode_type(sb, result.dentry);
855 inode_mode = 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 inode_mode = 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 (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
871 r = is_goal ? EEXIST : ENOENT;
875 r = ext4_fs_put_inode_ref(&ref);
879 r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
890 ext4_fs_put_inode_ref(&ref);
894 r = ext4_fs_put_inode_ref(&ref);
898 static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
899 struct ext4_inode_ref *parent_ref,
900 struct ext4_inode_ref *child_ref)
905 struct ext4_mountpoint *mp = ext4_get_mount(path);
913 len = ext4_path_check(path, &is_goal);
915 /* Remove entry from parent directory */
916 r = ext4_dir_remove_entry(parent_ref, path, len);
920 if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
921 EXT4_INODE_MODE_DIRECTORY)) {
922 ext4_fs_inode_links_count_dec(parent_ref);
923 parent_ref->dirty = true;
929 int ext4_flink(const char *path, const char *hardlink_path)
934 bool child_loaded = false;
935 uint32_t parent_inode, child_inode;
936 struct ext4_mountpoint *mp = ext4_get_mount(path);
937 struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path);
938 struct ext4_inode_ref child_ref;
943 /* Will that happen? Anyway return EINVAL for such case. */
949 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
950 &parent_inode, &name_off);
954 child_inode = f.inode;
957 /*We have file to unlink. Load it.*/
958 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
964 /* Creating hardlink for directory is not allowed. */
965 if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
966 EXT4_INODE_MODE_DIRECTORY)) {
971 r = ext4_create_hardlink(hardlink_path, &child_ref, false);
975 ext4_fs_put_inode_ref(&child_ref);
982 int ext4_frename(const char *path, const char *new_path)
987 bool parent_loaded = false, child_loaded = false;
988 uint32_t parent_inode, child_inode;
989 struct ext4_mountpoint *mp = ext4_get_mount(path);
990 struct ext4_inode_ref child_ref, parent_ref;
997 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
998 &parent_inode, &name_off);
1002 child_inode = f.inode;
1006 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref);
1010 parent_loaded = true;
1012 /*We have file to unlink. Load it.*/
1013 r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref);
1017 child_loaded = true;
1019 r = ext4_create_hardlink(new_path, &child_ref, true);
1023 r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
1029 ext4_fs_put_inode_ref(&parent_ref);
1032 ext4_fs_put_inode_ref(&child_ref);
1039 /****************************************************************************/
1041 int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb)
1043 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
1052 int ext4_cache_write_back(const char *path, bool on)
1054 struct ext4_mountpoint *mp = ext4_get_mount(path);
1060 ext4_block_cache_write_back(mp->fs.bdev, on);
1065 int ext4_fremove(const char *path)
1068 uint32_t parent_inode;
1073 struct ext4_inode_ref child;
1074 struct ext4_inode_ref parent;
1075 struct ext4_mountpoint *mp = ext4_get_mount(path);
1081 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
1082 &parent_inode, &name_off);
1089 r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
1095 /*We have file to delete. Load it.*/
1096 r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
1098 ext4_fs_put_inode_ref(&parent);
1106 len = ext4_path_check(path, &is_goal);
1108 /*Unlink from parent*/
1109 r = ext4_unlink(mp, &parent, &child, path, len);
1113 /*Link count is zero, the inode should be freed. */
1114 if (!ext4_inode_get_links_cnt(child.inode)) {
1115 ext4_inode_set_del_time(child.inode, -1L);
1117 ext4_block_cache_write_back(mp->fs.bdev, 1);
1118 /*Truncate may be IO heavy. Do it writeback cache mode.*/
1119 r = ext4_fs_truncate_inode(&child, 0);
1120 ext4_block_cache_write_back(mp->fs.bdev, 0);
1125 r = ext4_fs_free_inode(&child);
1131 ext4_fs_put_inode_ref(&child);
1132 ext4_fs_put_inode_ref(&parent);
1137 int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
1138 struct ext4_inode *inode)
1142 struct ext4_inode_ref inode_ref;
1143 struct ext4_mountpoint *mp = ext4_get_mount(path);
1151 r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
1161 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1167 memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
1168 ext4_fs_put_inode_ref(&inode_ref);
1177 int ext4_fopen(ext4_file *f, const char *path, const char *flags)
1179 struct ext4_mountpoint *mp = ext4_get_mount(path);
1186 ext4_block_cache_write_back(mp->fs.bdev, 1);
1187 r = ext4_generic_open(f, path, flags, true, 0, 0);
1188 ext4_block_cache_write_back(mp->fs.bdev, 0);
1193 int ext4_fopen2(ext4_file *f, const char *path, int flags)
1195 struct ext4_mountpoint *mp = ext4_get_mount(path);
1202 filetype = EXT4_DE_REG_FILE;
1205 ext4_block_cache_write_back(mp->fs.bdev, 1);
1206 r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
1207 ext4_block_cache_write_back(mp->fs.bdev, 0);
1212 int ext4_fclose(ext4_file *f)
1214 ext4_assert(f && f->mp);
1219 f->fpos = f->fsize = 0;
1224 static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
1226 struct ext4_inode_ref ref;
1230 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1232 EXT4_MP_UNLOCK(f->mp);
1237 f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
1238 if (f->fsize <= size) {
1243 /*Start write back cache mode.*/
1244 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1248 r = ext4_fs_truncate_inode(&ref, size);
1256 /*Stop write back cache mode*/
1257 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1263 ext4_fs_put_inode_ref(&ref);
1268 int ext4_ftruncate(ext4_file *f, uint64_t size)
1271 ext4_assert(f && f->mp);
1273 if (f->flags & O_RDONLY)
1276 EXT4_MP_LOCK(f->mp);
1277 r = ext4_ftruncate_no_lock(f, size);
1278 EXT4_MP_UNLOCK(f->mp);
1282 int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
1285 uint32_t iblock_idx;
1286 uint32_t iblock_last;
1287 uint32_t block_size;
1289 ext4_fsblk_t fblock;
1290 ext4_fsblk_t fblock_start;
1291 uint32_t fblock_count;
1293 uint8_t *u8_buf = buf;
1295 struct ext4_block b;
1296 struct ext4_inode_ref ref;
1298 ext4_assert(f && f->mp);
1300 if (f->flags & O_WRONLY)
1306 EXT4_MP_LOCK(f->mp);
1308 struct ext4_fs *const fs = &f->mp->fs;
1309 struct ext4_sblock *const sb = &f->mp->fs.sb;
1314 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1316 EXT4_MP_UNLOCK(f->mp);
1321 f->fsize = ext4_inode_get_size(sb, ref.inode);
1323 block_size = ext4_sb_get_block_size(sb);
1324 size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
1326 iblock_idx = (f->fpos) / block_size;
1327 iblock_last = (f->fpos + size) / block_size;
1328 unalg = (f->fpos) % block_size;
1330 /*If the size of symlink is smaller than 60 bytes*/
1332 softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
1333 if (softlink && f->fsize < sizeof(ref.inode->blocks)
1334 && !ext4_inode_get_blocks_count(sb, ref.inode)) {
1336 char *content = (char *)ref.inode->blocks;
1337 if (f->fpos < f->fsize) {
1339 if (unalg + size > f->fsize)
1340 len = f->fsize - unalg;
1341 memcpy(buf, content + unalg, len);
1353 if (size > (block_size - unalg))
1354 len = block_size - unalg;
1356 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1360 /* Do we get an unwritten range? */
1362 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1366 memcpy(u8_buf, b.data + unalg, len);
1368 r = ext4_block_set(f->mp->fs.bdev, &b);
1373 memset(u8_buf, 0, len);
1388 while (size >= block_size) {
1389 while (iblock_idx < iblock_last) {
1390 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
1398 fblock_start = fblock;
1400 if ((fblock_start + fblock_count) != fblock)
1406 r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1411 size -= block_size * fblock_count;
1412 u8_buf += block_size * fblock_count;
1413 f->fpos += block_size * fblock_count;
1416 *rcnt += block_size * fblock_count;
1418 fblock_start = fblock;
1423 r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
1427 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1431 memcpy(u8_buf, b.data, size);
1433 r = ext4_block_set(f->mp->fs.bdev, &b);
1444 ext4_fs_put_inode_ref(&ref);
1445 EXT4_MP_UNLOCK(f->mp);
1449 int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
1453 uint32_t iblock_last;
1454 uint32_t ifile_blocks;
1455 uint32_t block_size;
1457 uint32_t fblock_count;
1459 ext4_fsblk_t fblock_start;
1461 struct ext4_block b;
1462 struct ext4_inode_ref ref;
1463 const uint8_t *u8_buf = buf;
1466 ext4_assert(f && f->mp);
1468 if (f->flags & O_RDONLY)
1474 EXT4_MP_LOCK(f->mp);
1476 struct ext4_fs *const fs = &f->mp->fs;
1477 struct ext4_sblock *const sb = &f->mp->fs.sb;
1482 r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
1484 EXT4_MP_UNLOCK(f->mp);
1489 f->fsize = ext4_inode_get_size(sb, ref.inode);
1490 block_size = ext4_sb_get_block_size(sb);
1492 iblock_last = (f->fpos + size) / block_size;
1493 iblk_idx = (f->fpos) / block_size;
1494 ifile_blocks = (f->fsize + block_size - 1) / block_size;
1496 unalg = (f->fpos) % block_size;
1500 if (size > (block_size - unalg))
1501 len = block_size - unalg;
1503 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1507 r = ext4_block_get(f->mp->fs.bdev, &b, fblk);
1511 memcpy(b.data + unalg, u8_buf, len);
1512 ext4_bcache_set_dirty(b.buf);
1514 r = ext4_block_set(f->mp->fs.bdev, &b);
1528 /*Start write back cache mode.*/
1529 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1535 while (size >= block_size) {
1537 while (iblk_idx < iblock_last) {
1538 if (iblk_idx < ifile_blocks) {
1539 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
1544 rr = ext4_fs_append_inode_dblk(&ref, &fblk,
1547 /* Unable to append more blocks. But
1548 * some block might be allocated already
1556 if (!fblock_start) {
1557 fblock_start = fblk;
1560 if ((fblock_start + fblock_count) != fblk)
1566 r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
1571 size -= block_size * fblock_count;
1572 u8_buf += block_size * fblock_count;
1573 f->fpos += block_size * fblock_count;
1576 *wcnt += block_size * fblock_count;
1578 fblock_start = fblk;
1582 /*ext4_fs_append_inode_block has failed and no
1583 * more blocks might be written. But node size
1584 * should be updated.*/
1590 /*Stop write back cache mode*/
1591 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1597 if (iblk_idx < ifile_blocks) {
1598 r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
1602 r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
1604 /*Node size sholud be updated.*/
1608 r = ext4_block_get(f->mp->fs.bdev, &b, fblk);
1612 memcpy(b.data, u8_buf, size);
1613 ext4_bcache_set_dirty(b.buf);
1615 r = ext4_block_set(f->mp->fs.bdev, &b);
1626 if (f->fpos > f->fsize) {
1628 ext4_inode_set_size(ref.inode, f->fsize);
1633 ext4_fs_put_inode_ref(&ref);
1634 EXT4_MP_UNLOCK(f->mp);
1638 int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
1642 if (offset > f->fsize)
1648 if ((offset + f->fpos) > f->fsize)
1654 if (offset > f->fsize)
1657 f->fpos = f->fsize - offset;
1663 uint64_t ext4_ftell(ext4_file *f)
1668 uint64_t ext4_fsize(ext4_file *f)
1673 int ext4_chmod(const char *path, uint32_t mode)
1678 struct ext4_sblock *sb;
1679 struct ext4_inode_ref inode_ref;
1680 struct ext4_mountpoint *mp = ext4_get_mount(path);
1687 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1695 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1701 ext4_inode_set_mode(sb, inode_ref.inode, mode);
1702 inode_ref.dirty = true;
1704 ext4_fs_put_inode_ref(&inode_ref);
1709 int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
1714 struct ext4_inode_ref inode_ref;
1715 struct ext4_mountpoint *mp = ext4_get_mount(path);
1722 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1729 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1735 ext4_inode_set_uid(inode_ref.inode, uid);
1736 ext4_inode_set_gid(inode_ref.inode, gid);
1737 inode_ref.dirty = true;
1739 ext4_fs_put_inode_ref(&inode_ref);
1744 int ext4_file_set_atime(const char *path, uint32_t atime)
1749 struct ext4_inode_ref inode_ref;
1750 struct ext4_mountpoint *mp = ext4_get_mount(path);
1757 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1764 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1770 ext4_inode_set_access_time(inode_ref.inode, atime);
1771 inode_ref.dirty = true;
1773 ext4_fs_put_inode_ref(&inode_ref);
1778 int ext4_file_set_mtime(const char *path, uint32_t mtime)
1783 struct ext4_inode_ref inode_ref;
1784 struct ext4_mountpoint *mp = ext4_get_mount(path);
1791 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1798 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1804 ext4_inode_set_modif_time(inode_ref.inode, mtime);
1805 inode_ref.dirty = true;
1807 ext4_fs_put_inode_ref(&inode_ref);
1812 int ext4_file_set_ctime(const char *path, uint32_t ctime)
1817 struct ext4_inode_ref inode_ref;
1818 struct ext4_mountpoint *mp = ext4_get_mount(path);
1825 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
1832 r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
1838 ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
1839 inode_ref.dirty = true;
1841 ext4_fs_put_inode_ref(&inode_ref);
1846 static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
1848 struct ext4_block b;
1849 struct ext4_inode_ref ref;
1851 ext4_fsblk_t fblock;
1852 uint32_t block_size;
1855 ext4_assert(f && f->mp);
1860 r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
1862 EXT4_MP_UNLOCK(f->mp);
1867 block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
1868 if (size > block_size) {
1872 r = ext4_ftruncate_no_lock(f, 0);
1876 /*Start write back cache mode.*/
1877 r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
1881 /*If the size of symlink is smaller than 60 bytes*/
1882 if (size < sizeof(ref.inode->blocks)) {
1883 memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
1884 memcpy(ref.inode->blocks, buf, size);
1885 ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
1887 ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
1888 r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
1892 r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
1896 memcpy(b.data, buf, size);
1897 ext4_bcache_set_dirty(b.buf);
1898 r = ext4_block_set(f->mp->fs.bdev, &b);
1903 /*Stop write back cache mode*/
1904 ext4_block_cache_write_back(f->mp->fs.bdev, 0);
1909 ext4_inode_set_size(ref.inode, size);
1917 ext4_fs_put_inode_ref(&ref);
1921 int ext4_fsymlink(const char *target, const char *path)
1923 struct ext4_mountpoint *mp = ext4_get_mount(path);
1931 filetype = EXT4_DE_SYMLINK;
1934 ext4_block_cache_write_back(mp->fs.bdev, 1);
1935 r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
1937 r = ext4_fsymlink_set(&f, target, strlen(target));
1944 ext4_block_cache_write_back(mp->fs.bdev, 0);
1949 int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
1951 struct ext4_mountpoint *mp = ext4_get_mount(path);
1962 filetype = EXT4_DE_SYMLINK;
1965 ext4_block_cache_write_back(mp->fs.bdev, 1);
1966 r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
1968 r = ext4_fread(&f, buf, bufsize, rcnt);
1975 ext4_block_cache_write_back(mp->fs.bdev, 0);
1980 int ext4_setxattr(const char *path, const char *name, size_t name_len,
1981 const void *data, size_t data_size, bool replace)
1987 const char *dissected_name = NULL;
1988 size_t dissected_len = 0;
1989 struct ext4_xattr_ref xattr_ref;
1990 struct ext4_inode_ref inode_ref;
1991 struct ext4_mountpoint *mp = ext4_get_mount(path);
1995 dissected_name = ext4_extract_xattr_name(name, name_len,
1996 &name_index, &dissected_len);
2001 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2007 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2011 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2013 ext4_fs_put_inode_ref(&inode_ref);
2017 r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
2018 dissected_len, data, data_size, replace);
2020 ext4_fs_put_xattr_ref(&xattr_ref);
2021 ext4_fs_put_inode_ref(&inode_ref);
2027 int ext4_getxattr(const char *path, const char *name, size_t name_len,
2028 void *buf, size_t buf_size, size_t *data_size)
2034 const char *dissected_name = NULL;
2035 size_t dissected_len = 0;
2036 struct ext4_xattr_ref xattr_ref;
2037 struct ext4_inode_ref inode_ref;
2038 struct ext4_mountpoint *mp = ext4_get_mount(path);
2042 dissected_name = ext4_extract_xattr_name(name, name_len,
2043 &name_index, &dissected_len);
2048 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2054 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2058 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2060 ext4_fs_put_inode_ref(&inode_ref);
2064 r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
2065 dissected_len, buf, buf_size, data_size);
2067 ext4_fs_put_xattr_ref(&xattr_ref);
2068 ext4_fs_put_inode_ref(&inode_ref);
2074 struct ext4_listxattr_iterator {
2079 bool list_too_small;
2080 bool get_required_size;
2083 static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
2084 struct ext4_xattr_item *item)
2086 struct ext4_listxattr_iterator *lxi;
2087 lxi = ref->iter_arg;
2088 if (!lxi->get_required_size) {
2091 prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
2092 if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
2093 lxi->list_too_small = 1;
2094 return EXT4_XATTR_ITERATE_STOP;
2097 memcpy(lxi->list_ptr, prefix, plen);
2098 lxi->list_ptr += plen;
2099 lxi->ret_size += plen;
2101 memcpy(lxi->list_ptr, item->name, item->name_len);
2102 lxi->list_ptr[item->name_len] = 0;
2103 lxi->list_ptr += item->name_len + 1;
2105 lxi->ret_size += item->name_len + 1;
2106 return EXT4_XATTR_ITERATE_CONT;
2109 int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
2114 struct ext4_xattr_ref xattr_ref;
2115 struct ext4_inode_ref inode_ref;
2116 struct ext4_listxattr_iterator lxi;
2117 struct ext4_mountpoint *mp = ext4_get_mount(path);
2122 lxi.list_ptr = list;
2125 lxi.list_too_small = false;
2126 lxi.get_required_size = (!size) ? true : false;
2129 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2135 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2139 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2141 ext4_fs_put_inode_ref(&inode_ref);
2145 xattr_ref.iter_arg = &lxi;
2146 ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
2147 if (lxi.list_too_small)
2152 *ret_size = lxi.ret_size;
2155 ext4_fs_put_xattr_ref(&xattr_ref);
2156 ext4_fs_put_inode_ref(&inode_ref);
2163 int ext4_removexattr(const char *path, const char *name, size_t name_len)
2169 const char *dissected_name = NULL;
2170 size_t dissected_len = 0;
2171 struct ext4_xattr_ref xattr_ref;
2172 struct ext4_inode_ref inode_ref;
2173 struct ext4_mountpoint *mp = ext4_get_mount(path);
2177 dissected_name = ext4_extract_xattr_name(name, name_len,
2178 &name_index, &dissected_len);
2183 r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
2189 r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
2193 r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
2195 ext4_fs_put_inode_ref(&inode_ref);
2199 r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
2202 ext4_fs_put_xattr_ref(&xattr_ref);
2203 ext4_fs_put_inode_ref(&inode_ref);
2210 /*********************************DIRECTORY OPERATION************************/
2212 int ext4_dir_rm(const char *path)
2218 struct ext4_mountpoint *mp = ext4_get_mount(path);
2219 struct ext4_inode_ref act;
2220 struct ext4_inode_ref child;
2221 struct ext4_dir_iter it;
2225 uint32_t inode_current;
2237 struct ext4_fs *const fs = &mp->fs;
2240 r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
2247 len = ext4_path_check(path, &is_goal);
2249 inode_current = f.inode;
2252 ext4_block_cache_write_back(mp->fs.bdev, 1);
2255 /*Load directory node.*/
2256 r = ext4_fs_get_inode_ref(fs, inode_current, &act);
2261 /*Initialize iterator.*/
2262 r = ext4_dir_iterator_init(&it, &act, 0);
2264 ext4_fs_put_inode_ref(&act);
2275 /*Get up directory inode when ".." entry*/
2276 if ((it.curr->name_len == 2) &&
2277 ext4_is_dots(it.curr->name, it.curr->name_len)) {
2278 inode_up = ext4_dir_en_get_inode(it.curr);
2281 /*If directory or file entry, but not "." ".." entry*/
2282 if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
2284 /*Get child inode reference do unlink
2287 cinode = ext4_dir_en_get_inode(it.curr);
2288 r = ext4_fs_get_inode_ref(fs, cinode, &child);
2292 /*If directory with no leaf children*/
2293 r = ext4_has_children(&has_children, &child);
2295 ext4_fs_put_inode_ref(&child);
2300 /*Has directory children. Go into this
2302 inode_up = inode_current;
2303 inode_current = cinode;
2305 ext4_fs_put_inode_ref(&child);
2309 /*No children in child directory or file. Just
2311 r = ext4_unlink(f.mp, &act, &child,
2312 (char *)it.curr->name,
2315 ext4_fs_put_inode_ref(&child);
2319 ext4_inode_set_del_time(child.inode, -1L);
2320 ext4_inode_set_links_cnt(child.inode, 0);
2323 r = ext4_fs_truncate_inode(&child, 0);
2325 ext4_fs_put_inode_ref(&child);
2329 r = ext4_fs_free_inode(&child);
2331 ext4_fs_put_inode_ref(&child);
2335 r = ext4_fs_put_inode_ref(&child);
2340 r = ext4_dir_iterator_next(&it);
2344 /*Directory iterator reached last entry*/
2345 ext4_has_children(&has_children, &act);
2346 if (!has_children) {
2347 inode_current = inode_up;
2354 struct ext4_inode_ref parent;
2355 r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
2360 /* In this place all directories should be
2362 * Last unlink from root of current directory*/
2363 r = ext4_unlink(f.mp, &parent, &act,
2366 ext4_fs_put_inode_ref(&parent);
2370 if (ext4_inode_get_links_cnt(act.inode) == 2) {
2371 ext4_inode_set_del_time(act.inode, -1L);
2372 ext4_inode_set_links_cnt(act.inode, 0);
2375 r = ext4_fs_truncate_inode(&act, 0);
2377 ext4_fs_put_inode_ref(&parent);
2381 r = ext4_fs_free_inode(&act);
2383 ext4_fs_put_inode_ref(&parent);
2388 r = ext4_fs_put_inode_ref(&parent);
2395 ext4_dir_iterator_fini(&it);
2396 ext4_fs_put_inode_ref(&act);
2399 /*When something goes wrong. End loop.*/
2405 ext4_block_cache_write_back(mp->fs.bdev, 0);
2410 int ext4_dir_mk(const char *path)
2415 struct ext4_mountpoint *mp = ext4_get_mount(path);
2423 r = ext4_generic_open(&f, path, "r", false, 0, 0);
2425 /*Directory already created*/
2431 r = ext4_generic_open(&f, path, "w", false, 0, 0);
2441 int ext4_dir_open(ext4_dir *d, const char *path)
2443 struct ext4_mountpoint *mp = ext4_get_mount(path);
2450 r = ext4_generic_open(&d->f, path, "r", false, 0, 0);
2456 int ext4_dir_close(ext4_dir *d)
2458 return ext4_fclose(&d->f);
2461 const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
2463 #define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
2466 ext4_direntry *de = 0;
2467 struct ext4_inode_ref dir;
2468 struct ext4_dir_iter it;
2470 EXT4_MP_LOCK(d->f.mp);
2472 if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) {
2473 EXT4_MP_UNLOCK(d->f.mp);
2477 r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
2482 r = ext4_dir_iterator_init(&it, &dir, d->next_off);
2484 ext4_fs_put_inode_ref(&dir);
2488 memcpy(&d->de, it.curr, sizeof(ext4_direntry));
2491 ext4_dir_iterator_next(&it);
2493 d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
2495 ext4_dir_iterator_fini(&it);
2496 ext4_fs_put_inode_ref(&dir);
2499 EXT4_MP_UNLOCK(d->f.mp);
2503 void ext4_dir_entry_rewind(ext4_dir *d)
2508 int ext4_test_journal(const char *mount_point)
2510 struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
2516 if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
2517 struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
2518 struct jbd_journal *journal;
2523 journal = calloc(1, sizeof(struct jbd_journal));
2530 r = jbd_get_fs(&mp->fs, jbd_fs);
2535 r = jbd_journal_start(jbd_fs, journal);
2543 jbd_journal_stop(journal);