*/
#include "ext4_config.h"
+#include "ext4.h"
#include "ext4_blockdev.h"
#include "ext4_types.h"
#include "ext4_debug.h"
#include "ext4_inode.h"
#include "ext4_super.h"
#include "ext4_dir_idx.h"
-#include "ext4.h"
+#include "ext4_xattr.h"
+#include "ext4_journal.h"
+
#include <stdlib.h>
#include <string.h>
/**@brief Dynamic allocation cache flag.*/
bool cache_dynamic;
+
+ struct jbd_fs jbd_fs;
+ struct jbd_journal jbd_journal;
};
/**@brief Block devices descriptor.*/
static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode)
{
- struct ext4_fs *fs = enode->fs;
+ struct ext4_sblock *sb = &enode->fs->sb;
/* Check if node is directory */
- if (!ext4_inode_is_type(&fs->sb, enode->inode,
- EXT4_INODE_MODE_DIRECTORY)) {
+ if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
*has_children = false;
return EOK;
}
- struct ext4_directory_iterator it;
+ struct ext4_dir_iter it;
int rc = ext4_dir_iterator_init(&it, enode, 0);
if (rc != EOK)
return rc;
/* Find a non-empty directory entry */
bool found = false;
- while (it.current != NULL) {
- if (ext4_dir_entry_ll_get_inode(it.current) != 0) {
- uint16_t name_size = ext4_dir_entry_ll_get_name_length(
- &fs->sb, it.current);
- if (!ext4_is_dots(it.current->name, name_size)) {
+ while (it.curr != NULL) {
+ if (ext4_dir_en_get_inode(it.curr) != 0) {
+ uint16_t nsize;
+ nsize = ext4_dir_en_get_name_len(sb, it.curr);
+ if (!ext4_is_dots(it.curr->name, nsize)) {
found = true;
break;
}
}
static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
- struct ext4_inode_ref *child, const char *name,
- uint32_t name_len)
+ struct ext4_inode_ref *ch, const char *n,
+ uint32_t len, bool rename)
{
/* Check maximum name length */
- if (name_len > EXT4_DIRECTORY_FILENAME_LEN)
+ if (len > EXT4_DIRECTORY_FILENAME_LEN)
return EINVAL;
/* Add entry to parent directory */
- int rc = ext4_dir_add_entry(parent, name, name_len, child);
- if (rc != EOK)
- return rc;
+ int r = ext4_dir_add_entry(parent, n, len, ch);
+ if (r != EOK)
+ return r;
/* Fill new dir -> add '.' and '..' entries.
* Also newly allocated inode should have 0 link count.
*/
- if (ext4_inode_is_type(&mp->fs.sb, child->inode,
- EXT4_INODE_MODE_DIRECTORY) &&
- ext4_inode_get_links_count(child->inode) == 0) {
- rc = ext4_dir_add_entry(child, ".", strlen("."), child);
- if (rc != EOK) {
- ext4_dir_remove_entry(parent, name, strlen(name));
- return rc;
- }
-
- rc = ext4_dir_add_entry(child, "..", strlen(".."), parent);
- if (rc != EOK) {
- ext4_dir_remove_entry(parent, name, strlen(name));
- ext4_dir_remove_entry(child, ".", strlen("."));
- return rc;
- }
- /*New empty directory. Two links (. and ..) */
- ext4_inode_set_links_count(child->inode, 2);
+ bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode,
+ EXT4_INODE_MODE_DIRECTORY);
+ if (is_dir && !rename) {
#if CONFIG_DIR_INDEX_ENABLE
/* Initialize directory index if supported */
- if (ext4_sb_has_feature_compatible(
- &mp->fs.sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
- rc = ext4_dir_dx_init(child);
- if (rc != EOK)
- return rc;
-
- ext4_inode_set_flag(child->inode,
- EXT4_INODE_FLAG_INDEX);
- child->dirty = true;
- }
+ if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
+ r = ext4_dir_dx_init(ch, parent);
+ if (r != EOK)
+ return r;
+
+ ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
+ ch->dirty = true;
+ } else
#endif
+ {
+ r = ext4_dir_add_entry(ch, ".", strlen("."), ch);
+ if (r != EOK) {
+ ext4_dir_remove_entry(parent, n, strlen(n));
+ return r;
+ }
+
+ r = ext4_dir_add_entry(ch, "..", strlen(".."), parent);
+ if (r != EOK) {
+ ext4_dir_remove_entry(parent, n, strlen(n));
+ ext4_dir_remove_entry(ch, ".", strlen("."));
+ return r;
+ }
+ }
+ /*New empty directory. Two links (. and ..) */
+ ext4_inode_set_links_cnt(ch->inode, 2);
ext4_fs_inode_links_count_inc(parent);
- child->dirty = true;
+ ch->dirty = true;
parent->dirty = true;
- } else {
- if (ext4_inode_is_type(&mp->fs.sb, child->inode,
- EXT4_INODE_MODE_DIRECTORY)) {
- int has_flag_index =
- ext4_inode_has_flag(child->inode,
- EXT4_INODE_FLAG_INDEX);
- struct ext4_directory_search_result result;
- if (!has_flag_index) {
- rc = ext4_dir_find_entry(&result,
- child, "..",
- strlen(".."));
- if (rc != EOK)
- return EIO;
-
- ext4_dir_entry_ll_set_inode(result.dentry,
- parent->index);
- result.block.dirty = true;
- rc = ext4_dir_destroy_result(child, &result);
- if (rc != EOK)
- return rc;
+ return r;
+ }
+ /*
+ * In case we want to rename a directory,
+ * we reset the original '..' pointer.
+ */
+ if (is_dir) {
+ bool idx;
+ idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX);
+ struct ext4_dir_search_result res;
+ if (!idx) {
+ r = ext4_dir_find_entry(&res, ch, "..", strlen(".."));
+ if (r != EOK)
+ return EIO;
- } else {
+ ext4_dir_en_set_inode(res.dentry, parent->index);
+ ext4_trans_set_block_dirty(res.block.buf);
+ r = ext4_dir_destroy_result(ch, &res);
+ if (r != EOK)
+ return r;
+
+ } else {
#if CONFIG_DIR_INDEX_ENABLE
- rc = ext4_dir_dx_reset_parent_inode(child,
- parent->index);
- if (rc != EOK)
- return rc;
+ r = ext4_dir_dx_reset_parent_inode(ch, parent->index);
+ if (r != EOK)
+ return r;
#endif
- }
-
- ext4_fs_inode_links_count_inc(parent);
- parent->dirty = true;
- } else {
- ext4_fs_inode_links_count_inc(child);
- child->dirty = true;
}
+
+ ext4_fs_inode_links_count_inc(parent);
+ parent->dirty = true;
+ }
+ if (!rename) {
+ ext4_fs_inode_links_count_inc(ch);
+ ch->dirty = true;
}
- return rc;
+ return r;
}
static int ext4_unlink(struct ext4_mountpoint *mp,
struct ext4_inode_ref *parent,
- struct ext4_inode_ref *child_inode_ref, const char *name,
+ struct ext4_inode_ref *child, const char *name,
uint32_t name_len)
{
bool has_children;
- int rc = ext4_has_children(&has_children, child_inode_ref);
+ int rc = ext4_has_children(&has_children, child);
if (rc != EOK)
return rc;
if (rc != EOK)
return rc;
- bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
+ bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode,
EXT4_INODE_MODE_DIRECTORY);
/* If directory - handle links from parent */
if (is_dir) {
- // ext4_assert(ext4_inode_get_links_count(child_inode_ref->inode)
- // == 1);
ext4_fs_inode_links_count_dec(parent);
parent->dirty = true;
}
/*
* TODO: Update timestamp for inode.
*
- * ext4_inode_set_change_inode_time(child_inode_ref->inode,
+ * ext4_inode_set_change_inode_time(child->inode,
* (uint32_t) now);
*/
- if (ext4_inode_get_links_count(child_inode_ref->inode)) {
- ext4_fs_inode_links_count_dec(child_inode_ref);
- child_inode_ref->dirty = true;
+ if (ext4_inode_get_links_cnt(child->inode)) {
+ ext4_fs_inode_links_count_dec(child);
+ child->dirty = true;
}
return EOK;
/*Bind block cache to block device*/
r = ext4_block_bind_bcache(bd, bc);
if (r != EOK) {
+ ext4_bcache_cleanup(bc);
ext4_block_fini(bd);
if (mp->cache_dynamic) {
ext4_bcache_fini_dynamic(bc);
}
return r;
}
+ bd->fs = &mp->fs;
return r;
}
+
int ext4_umount(const char *mount_point)
{
int i;
r = ext4_fs_fini(&mp->fs);
if (r != EOK)
- return r;
+ goto Finish;
mp->mounted = 0;
+ ext4_bcache_cleanup(mp->fs.bdev->bc);
if (mp->cache_dynamic) {
ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
free(mp->fs.bdev->bc);
}
+ r = ext4_block_fini(mp->fs.bdev);
+Finish:
+ mp->fs.bdev->fs = NULL;
+ return r;
+}
+
+static struct ext4_mountpoint *ext4_get_mount(const char *path)
+{
+ int i;
+ for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
+
+ if (!_mp[i].mounted)
+ continue;
+
+ if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
+ return &_mp[i];
+ }
+ return 0;
+}
+
+int ext4_journal_start(const char *mount_point)
+{
+ int r = EOK;
+ struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
+ if (!mp)
+ return ENOENT;
+
+ if (ext4_sb_feature_com(&mp->fs.sb,
+ EXT4_FCOM_HAS_JOURNAL)) {
+ r = jbd_get_fs(&mp->fs, &mp->jbd_fs);
+ if (r != EOK)
+ goto Finish;
- return ext4_block_fini(mp->fs.bdev);
+ r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal);
+ if (r != EOK) {
+ mp->jbd_fs.dirty = false;
+ jbd_put_fs(&mp->jbd_fs);
+ goto Finish;
+ }
+ mp->fs.jbd_fs = &mp->jbd_fs;
+ mp->fs.jbd_journal = &mp->jbd_journal;
+ }
+Finish:
+ return r;
}
-int ext4_mount_point_stats(const char *mount_point,
- struct ext4_mount_stats *stats)
+int ext4_journal_stop(const char *mount_point)
{
- uint32_t i;
- struct ext4_mountpoint *mp = 0;
+ int r = EOK;
+ struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
+ if (!mp)
+ return ENOENT;
- for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if (!strcmp(_mp[i].name, mount_point)) {
- mp = &_mp[i];
- break;
+ if (ext4_sb_feature_com(&mp->fs.sb,
+ EXT4_FCOM_HAS_JOURNAL)) {
+ r = jbd_journal_stop(&mp->jbd_journal);
+ if (r != EOK) {
+ mp->jbd_fs.dirty = false;
+ jbd_put_fs(&mp->jbd_fs);
+ mp->fs.jbd_journal = NULL;
+ mp->fs.jbd_fs = NULL;
+ goto Finish;
+ }
+
+ r = jbd_put_fs(&mp->jbd_fs);
+ if (r != EOK) {
+ mp->fs.jbd_journal = NULL;
+ mp->fs.jbd_fs = NULL;
+ goto Finish;
+ }
+
+ mp->fs.jbd_journal = NULL;
+ mp->fs.jbd_fs = NULL;
+ }
+Finish:
+ return r;
+}
+
+int ext4_recover(const char *mount_point)
+{
+ struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
+ if (!mp)
+ return ENOENT;
+
+ int r = ENOTSUP;
+ EXT4_MP_LOCK(mp);
+ if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
+ struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
+ if (!jbd_fs) {
+ r = ENOMEM;
+ goto Finish;
+ }
+
+
+ r = jbd_get_fs(&mp->fs, jbd_fs);
+ if (r != EOK) {
+ free(jbd_fs);
+ goto Finish;
+ }
+
+ r = jbd_recover(jbd_fs);
+ jbd_put_fs(jbd_fs);
+ free(jbd_fs);
+ }
+
+
+Finish:
+ EXT4_MP_UNLOCK(mp);
+ return r;
+}
+
+static int ext4_trans_start(struct ext4_mountpoint *mp)
+{
+ int r = EOK;
+ if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
+ struct jbd_journal *journal = mp->fs.jbd_journal;
+ struct jbd_trans *trans;
+ trans = jbd_journal_new_trans(journal);
+ if (!trans) {
+ r = ENOMEM;
+ goto Finish;
}
+ mp->fs.curr_trans = trans;
+ }
+Finish:
+ return r;
+}
+
+static int ext4_trans_stop(struct ext4_mountpoint *mp)
+{
+ int r = EOK;
+ if (mp->fs.jbd_journal && mp->fs.curr_trans) {
+ struct jbd_journal *journal = mp->fs.jbd_journal;
+ struct jbd_trans *trans = mp->fs.curr_trans;
+ r = jbd_journal_commit_trans(journal, trans);
+ mp->fs.curr_trans = NULL;
+ }
+ return r;
+}
+
+static void ext4_trans_abort(struct ext4_mountpoint *mp)
+{
+ if (mp->fs.jbd_journal && mp->fs.curr_trans) {
+ struct jbd_journal *journal = mp->fs.jbd_journal;
+ struct jbd_trans *trans = mp->fs.curr_trans;
+ jbd_journal_free_trans(journal, trans, true);
+ mp->fs.curr_trans = NULL;
}
+}
+
+int ext4_mount_point_stats(const char *mount_point,
+ struct ext4_mount_stats *stats)
+{
+ struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
+
if (!mp)
return ENOENT;
/********************************FILE OPERATIONS*****************************/
-static struct ext4_mountpoint *ext4_get_mount(const char *path)
-{
- int i;
- for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
-
- if (!_mp[i].mounted)
- continue;
-
- if (!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
- return &_mp[i];
- }
- return 0;
-}
-
static int ext4_path_check(const char *path, bool *is_goal)
{
int i;
return false;
}
+static int ext4_trunc_inode(struct ext4_mountpoint *mp,
+ uint32_t index, uint64_t new_size)
+{
+ int r = EOK;
+ struct ext4_fs *const fs = &mp->fs;
+ struct ext4_inode_ref inode_ref;
+ uint64_t inode_size;
+ bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans;
+ r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
+ if (r != EOK)
+ return r;
+
+ inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode);
+ ext4_fs_put_inode_ref(&inode_ref);
+ if (has_trans)
+ ext4_trans_stop(mp);
+
+ while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) {
+
+ inode_size -= CONFIG_MAX_TRUNCATE_SIZE;
+
+ ext4_trans_start(mp);
+ r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
+ if (r != EOK) {
+ ext4_trans_abort(mp);
+ break;
+ }
+ r = ext4_fs_truncate_inode(&inode_ref, inode_size);
+ if (r != EOK)
+ ext4_fs_put_inode_ref(&inode_ref);
+ else
+ r = ext4_fs_put_inode_ref(&inode_ref);
+
+ if (r != EOK) {
+ ext4_trans_abort(mp);
+ goto Finish;
+ } else
+ ext4_trans_stop(mp);
+ }
+
+ if (inode_size > new_size) {
+
+ inode_size = new_size;
+
+ ext4_trans_start(mp);
+ r = ext4_fs_get_inode_ref(fs, index, &inode_ref);
+ if (r != EOK) {
+ ext4_trans_abort(mp);
+ goto Finish;
+ }
+ r = ext4_fs_truncate_inode(&inode_ref, inode_size);
+ if (r != EOK)
+ ext4_fs_put_inode_ref(&inode_ref);
+ else
+ r = ext4_fs_put_inode_ref(&inode_ref);
+
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
+ }
+
+Finish:
+
+ if (has_trans)
+ ext4_trans_start(mp);
+
+ return r;
+}
+
/*
- * NOTICE: if filetype is equal to EXT4_DIRECTORY_FILETYPE_UNKNOWN,
+ * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
* any filetype of the target dir entry will be accepted.
*/
static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
- int filetype, uint32_t *parent_inode,
+ int ftype, uint32_t *parent_inode,
uint32_t *name_off)
{
bool is_goal = false;
- uint8_t inode_type;
+ uint32_t imode = EXT4_INODE_MODE_DIRECTORY;
uint32_t next_inode;
int r;
+ int len;
struct ext4_mountpoint *mp = ext4_get_mount(path);
- struct ext4_directory_search_result result;
+ struct ext4_dir_search_result result;
struct ext4_inode_ref ref;
f->mp = 0;
if (!mp)
return ENOENT;
+ struct ext4_fs *const fs = &mp->fs;
+ struct ext4_sblock *const sb = &mp->fs.sb;
+
f->flags = flags;
/*Skip mount point*/
*name_off = strlen(mp->name);
/*Load root*/
- r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
-
+ r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
if (r != EOK)
return r;
if (parent_inode)
*parent_inode = ref.index;
- int len = ext4_path_check(path, &is_goal);
+ if (flags & O_CREAT)
+ ext4_trans_start(mp);
+ len = ext4_path_check(path, &is_goal);
while (1) {
len = ext4_path_check(path, &is_goal);
-
if (!len) {
/*If root open was request.*/
- if (is_goal &&
- ((filetype == EXT4_DIRECTORY_FILETYPE_DIR) ||
- (filetype == EXT4_DIRECTORY_FILETYPE_UNKNOWN)))
- break;
+ if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN)
+ if (is_goal)
+ break;
r = ENOENT;
break;
r = ext4_dir_find_entry(&result, &ref, path, len);
if (r != EOK) {
+ /*Destroy last result*/
+ ext4_dir_destroy_result(&ref, &result);
if (r != ENOENT)
break;
/*O_CREAT allows create new entry*/
struct ext4_inode_ref child_ref;
- r = ext4_fs_alloc_inode(
- &mp->fs, &child_ref,
- is_goal ? filetype
- : EXT4_DIRECTORY_FILETYPE_DIR);
+ r = ext4_fs_alloc_inode(fs, &child_ref,
+ is_goal ? ftype : EXT4_DE_DIR);
if (r != EOK)
break;
- /*Destroy last result*/
- ext4_dir_destroy_result(&ref, &result);
/*Link with root dir.*/
- r = ext4_link(mp, &ref, &child_ref, path, len);
+ r = ext4_link(mp, &ref, &child_ref, path, len, false);
if (r != EOK) {
/*Fail. Free new inode.*/
ext4_fs_free_inode(&child_ref);
}
ext4_fs_put_inode_ref(&child_ref);
-
continue;
}
if (parent_inode)
*parent_inode = ref.index;
- next_inode = ext4_dir_entry_ll_get_inode(result.dentry);
- inode_type =
- ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
+ next_inode = ext4_dir_en_get_inode(result.dentry);
+ if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
+ uint8_t t;
+ t = ext4_dir_en_get_inode_type(sb, result.dentry);
+ imode = ext4_fs_correspond_inode_mode(t);
+ } else {
+ struct ext4_inode_ref child_ref;
+ r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
+ if (r != EOK)
+ break;
+
+ imode = ext4_inode_type(sb, child_ref.inode);
+ ext4_fs_put_inode_ref(&child_ref);
+ }
r = ext4_dir_destroy_result(&ref, &result);
if (r != EOK)
break;
/*If expected file error*/
- if (inode_type != EXT4_DIRECTORY_FILETYPE_DIR && !is_goal) {
+ if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) {
r = ENOENT;
break;
}
- if (filetype != EXT4_DIRECTORY_FILETYPE_UNKNOWN) {
- if ((inode_type != filetype) && is_goal) {
+ if (ftype != EXT4_DE_UNKNOWN) {
+ bool df = imode != ext4_fs_correspond_inode_mode(ftype);
+ if (df && is_goal) {
r = ENOENT;
break;
}
if (r != EOK)
break;
- r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
+ r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
if (r != EOK)
break;
if (is_goal) {
- if ((f->flags & O_TRUNC) &&
- (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE)) {
-
- r = ext4_fs_truncate_inode(&ref, 0);
+ if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
+ r = ext4_trunc_inode(mp, ref.index, 0);
if (r != EOK) {
ext4_fs_put_inode_ref(&ref);
return r;
}
f->mp = mp;
- f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
+ f->fsize = ext4_inode_get_size(sb, ref.inode);
f->inode = ref.index;
f->fpos = 0;
if (f->flags & O_APPEND)
f->fpos = f->fsize;
+
}
r = ext4_fs_put_inode_ref(&ref);
+ if (flags & O_CREAT) {
+ if (r == EOK)
+ ext4_trans_stop(mp);
+ else
+ ext4_trans_abort(mp);
+
+ }
+
return r;
}
return EINVAL;
if (file_expect == true)
- filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
+ filetype = EXT4_DE_REG_FILE;
else
- filetype = EXT4_DIRECTORY_FILETYPE_DIR;
+ filetype = EXT4_DE_DIR;
return ext4_generic_open2(f, path, iflags, filetype, parent_inode,
name_off);
}
-static int __ext4_create_hardlink(const char *path,
- struct ext4_inode_ref *child_ref)
+static int ext4_create_hardlink(const char *path,
+ struct ext4_inode_ref *child_ref, bool rename)
{
bool is_goal = false;
- uint8_t inode_type = EXT4_DIRECTORY_FILETYPE_DIR;
+ uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY;
uint32_t next_inode;
int r;
+ int len;
struct ext4_mountpoint *mp = ext4_get_mount(path);
- struct ext4_directory_search_result result;
+ struct ext4_dir_search_result result;
struct ext4_inode_ref ref;
if (!mp)
return ENOENT;
+ struct ext4_fs *const fs = &mp->fs;
+ struct ext4_sblock *const sb = &mp->fs.sb;
+
/*Skip mount point*/
path += strlen(mp->name);
/*Load root*/
- r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
-
+ r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref);
if (r != EOK)
return r;
- int len = ext4_path_check(path, &is_goal);
-
+ len = ext4_path_check(path, &is_goal);
while (1) {
len = ext4_path_check(path, &is_goal);
-
if (!len) {
/*If root open was request.*/
- if (is_goal)
- r = EINVAL;
- else
- r = ENOENT;
+ r = is_goal ? EINVAL : ENOENT;
break;
}
r = ext4_dir_find_entry(&result, &ref, path, len);
if (r != EOK) {
+ /*Destroy last result*/
+ ext4_dir_destroy_result(&ref, &result);
+
if (r != ENOENT || !is_goal)
break;
+ /*Link with root dir.*/
+ r = ext4_link(mp, &ref, child_ref, path, len, rename);
+ break;
+ } else if (r == EOK && is_goal) {
/*Destroy last result*/
ext4_dir_destroy_result(&ref, &result);
-
- /*Link with root dir.*/
- r = ext4_link(mp, &ref, child_ref, path, len);
+ r = EEXIST;
break;
}
next_inode = result.dentry->inode;
- inode_type =
- ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
+ if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) {
+ uint8_t t;
+ t = ext4_dir_en_get_inode_type(sb, result.dentry);
+ inode_mode = ext4_fs_correspond_inode_mode(t);
+ } else {
+ struct ext4_inode_ref child_ref;
+ r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref);
+ if (r != EOK)
+ break;
+
+ inode_mode = ext4_inode_type(sb, child_ref.inode);
+ ext4_fs_put_inode_ref(&child_ref);
+ }
r = ext4_dir_destroy_result(&ref, &result);
if (r != EOK)
break;
- if (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE) {
- if (is_goal)
- r = EEXIST;
- else
- r = ENOENT;
-
+ if (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
+ r = is_goal ? EEXIST : ENOENT;
break;
}
if (r != EOK)
break;
- r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
+ r = ext4_fs_get_inode_ref(fs, next_inode, &ref);
if (r != EOK)
break;
return r;
}
-static int __ext4_remove_hardlink(const char *path,
- uint32_t name_off,
- struct ext4_inode_ref *parent_ref,
- struct ext4_inode_ref *child_ref)
+static int ext4_remove_orig_reference(const char *path, uint32_t name_off,
+ struct ext4_inode_ref *parent_ref,
+ struct ext4_inode_ref *child_ref)
{
bool is_goal;
int r;
len = ext4_path_check(path, &is_goal);
- /*Unlink from parent*/
- r = ext4_unlink(mp, parent_ref, child_ref, path, len);
+ /* Remove entry from parent directory */
+ r = ext4_dir_remove_entry(parent_ref, path, len);
if (r != EOK)
goto Finish;
+ if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode,
+ EXT4_INODE_MODE_DIRECTORY)) {
+ ext4_fs_inode_links_count_dec(parent_ref);
+ parent_ref->dirty = true;
+ }
Finish:
return r;
}
return EINVAL;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
- r = ext4_generic_open2(&f, path, O_RDONLY,
- EXT4_DIRECTORY_FILETYPE_UNKNOWN,
+ r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
&parent_inode, &name_off);
if (r != EOK)
goto Finish;
child_loaded = true;
- r = __ext4_create_hardlink(hardlink_path, &child_ref);
+ /* Creating hardlink for directory is not allowed. */
+ if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode,
+ EXT4_INODE_MODE_DIRECTORY)) {
+ r = EINVAL;
+ goto Finish;
+ }
+
+ r = ext4_create_hardlink(hardlink_path, &child_ref, false);
Finish:
if (child_loaded)
ext4_fs_put_inode_ref(&child_ref);
- EXT4_MP_UNLOCK(mp);
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
+ EXT4_MP_UNLOCK(mp);
return r;
}
return ENOENT;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
- r = ext4_generic_open2(&f, path, O_RDONLY,
- EXT4_DIRECTORY_FILETYPE_UNKNOWN,
- &parent_inode, &name_off);
+ r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN,
+ &parent_inode, &name_off);
if (r != EOK)
goto Finish;
child_loaded = true;
- r = __ext4_create_hardlink(new_path, &child_ref);
+ r = ext4_create_hardlink(new_path, &child_ref, true);
if (r != EOK)
goto Finish;
- r = __ext4_remove_hardlink(path, name_off, &parent_ref, &child_ref);
+ r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref);
if (r != EOK)
goto Finish;
if (child_loaded)
ext4_fs_put_inode_ref(&child_ref);
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
return ENOENT;
EXT4_MP_LOCK(mp);
- r = ext4_generic_open2(&f, path, O_RDWR,
- EXT4_DIRECTORY_FILETYPE_UNKNOWN,
+ ext4_trans_start(mp);
+
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN,
&parent_inode, &name_off);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
/*Load parent*/
r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
if (r != EOK) {
ext4_fs_put_inode_ref(&parent);
+ ext4_trans_abort(mp);
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+ /* We do not allow opening files here. */
+ if (ext4_inode_type(&mp->fs.sb, child.inode) ==
+ EXT4_INODE_MODE_DIRECTORY) {
+ ext4_fs_put_inode_ref(&parent);
+ ext4_fs_put_inode_ref(&child);
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
+ /*Link count will be zero, the inode should be freed. */
+ if (ext4_inode_get_links_cnt(child.inode) == 1) {
+ ext4_block_cache_write_back(mp->fs.bdev, 1);
+ r = ext4_trunc_inode(mp, child.index, 0);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&parent);
+ ext4_fs_put_inode_ref(&child);
+ ext4_trans_abort(mp);
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+ ext4_block_cache_write_back(mp->fs.bdev, 0);
+ }
+
/*Set path*/
path += name_off;
goto Finish;
/*Link count is zero, the inode should be freed. */
- if (!ext4_inode_get_links_count(child.inode)) {
- ext4_inode_set_deletion_time(child.inode, 0xFFFFFFFF);
- /*Turncate*/
- ext4_block_cache_write_back(mp->fs.bdev, 1);
- /*Truncate may be IO heavy. Do it writeback cache mode.*/
- r = ext4_fs_truncate_inode(&child, 0);
- ext4_block_cache_write_back(mp->fs.bdev, 0);
-
- if (r != EOK)
- goto Finish;
+ if (!ext4_inode_get_links_cnt(child.inode)) {
+ ext4_inode_set_del_time(child.inode, -1L);
r = ext4_fs_free_inode(&child);
if (r != EOK)
Finish:
ext4_fs_put_inode_ref(&child);
ext4_fs_put_inode_ref(&parent);
+
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
}
-int ext4_fill_raw_inode(const char *path,
- uint32_t *ret_ino,
+int ext4_fill_raw_inode(const char *path, uint32_t *ret_ino,
struct ext4_inode *inode)
{
int r;
EXT4_MP_LOCK(mp);
- r = ext4_generic_open2(&f, path, O_RDONLY,
- EXT4_DIRECTORY_FILETYPE_UNKNOWN,
- NULL, NULL);
+ r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL);
if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode));
-
ext4_fs_put_inode_ref(&inode_ref);
EXT4_MP_UNLOCK(mp);
return ENOENT;
EXT4_MP_LOCK(mp);
+
ext4_block_cache_write_back(mp->fs.bdev, 1);
r = ext4_generic_open(f, path, flags, true, 0, 0);
ext4_block_cache_write_back(mp->fs.bdev, 0);
+
EXT4_MP_UNLOCK(mp);
return r;
}
if (!mp)
return ENOENT;
- filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
+ filetype = EXT4_DE_REG_FILE;
EXT4_MP_LOCK(mp);
+
ext4_block_cache_write_back(mp->fs.bdev, 1);
- r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
+ r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
ext4_block_cache_write_back(mp->fs.bdev, 0);
+
EXT4_MP_UNLOCK(mp);
return r;
}
if (r != EOK)
goto Finish;
- r = ext4_fs_truncate_inode(&ref, size);
+ r = ext4_trunc_inode(f->mp, ref.index, size);
if (r != EOK)
goto Finish;
EXT4_MP_LOCK(f->mp);
+ ext4_trans_start(f->mp);
r = ext4_ftruncate_no_lock(f, size);
+ if (r != EOK)
+ ext4_trans_abort(f->mp);
+ else
+ ext4_trans_stop(f->mp);
EXT4_MP_UNLOCK(f->mp);
return r;
int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
{
- uint32_t u;
- uint32_t fblock;
- uint32_t fblock_start;
- uint32_t fblock_cnt;
- uint32_t sblock;
- uint32_t sblock_end;
+ uint32_t unalg;
+ uint32_t iblock_idx;
+ uint32_t iblock_last;
uint32_t block_size;
+
+ ext4_fsblk_t fblock;
+ ext4_fsblk_t fblock_start;
+ uint32_t fblock_count;
+
uint8_t *u8_buf = buf;
int r;
- struct ext4_block b;
struct ext4_inode_ref ref;
ext4_assert(f && f->mp);
EXT4_MP_LOCK(f->mp);
+ struct ext4_fs *const fs = &f->mp->fs;
+ struct ext4_sblock *const sb = &f->mp->fs.sb;
+
if (rcnt)
*rcnt = 0;
- r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
+ r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
if (r != EOK) {
EXT4_MP_UNLOCK(f->mp);
return r;
}
/*Sync file size*/
- f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
+ f->fsize = ext4_inode_get_size(sb, ref.inode);
- block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
+ block_size = ext4_sb_get_block_size(sb);
size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
- sblock = (f->fpos) / block_size;
- sblock_end = (f->fpos + size) / block_size;
- u = (f->fpos) % block_size;
+
+ iblock_idx = (f->fpos) / block_size;
+ iblock_last = (f->fpos + size) / block_size;
+ unalg = (f->fpos) % block_size;
/*If the size of symlink is smaller than 60 bytes*/
- if (ext4_inode_is_type(&f->mp->fs.sb, ref.inode, EXT4_INODE_MODE_SOFTLINK)
- && f->fsize < sizeof(ref.inode->blocks)
- && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
+ bool softlink;
+ softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK);
+ if (softlink && f->fsize < sizeof(ref.inode->blocks)
+ && !ext4_inode_get_blocks_count(sb, ref.inode)) {
+
char *content = (char *)ref.inode->blocks;
if (f->fpos < f->fsize) {
- r = (u + size > f->fsize)
- ?(f->fsize - u)
- :(size);
- memcpy(buf, content + u, r);
+ size_t len = size;
+ if (unalg + size > f->fsize)
+ len = f->fsize - unalg;
+ memcpy(buf, content + unalg, len);
if (rcnt)
- *rcnt = r;
-
- } else {
- r = 0;
- if (rcnt)
- *rcnt = 0;
+ *rcnt = len;
}
goto Finish;
}
- if (u) {
+ if (unalg) {
+ size_t len = size;
+ if (size > (block_size - unalg))
+ len = block_size - unalg;
- uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
-
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
+ r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
if (r != EOK)
goto Finish;
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if (r != EOK)
- goto Finish;
-
- memcpy(u8_buf, b.data + u, ll);
+ /* Do we get an unwritten range? */
+ if (fblock != 0) {
+ uint64_t off = fblock * block_size + unalg;
+ r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, len);
+ if (r != EOK)
+ goto Finish;
- r = ext4_block_set(f->mp->fs.bdev, &b);
- if (r != EOK)
- goto Finish;
+ } else {
+ /* Yes, we do. */
+ memset(u8_buf, 0, len);
+ }
- u8_buf += ll;
- size -= ll;
- f->fpos += ll;
+ u8_buf += len;
+ size -= len;
+ f->fpos += len;
if (rcnt)
- *rcnt += ll;
+ *rcnt += len;
- sblock++;
+ iblock_idx++;
}
fblock_start = 0;
- fblock_cnt = 0;
+ fblock_count = 0;
while (size >= block_size) {
- while (sblock < sblock_end) {
- r = ext4_fs_get_inode_data_block_index(&ref, sblock,
- &fblock);
+ while (iblock_idx < iblock_last) {
+ r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx,
+ &fblock, true);
if (r != EOK)
goto Finish;
- sblock++;
+ iblock_idx++;
- if (!fblock_start) {
+ if (!fblock_start)
fblock_start = fblock;
- }
- if ((fblock_start + fblock_cnt) != fblock)
+ if ((fblock_start + fblock_count) != fblock)
break;
- fblock_cnt++;
+ fblock_count++;
}
r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
- fblock_cnt);
+ fblock_count);
if (r != EOK)
goto Finish;
- size -= block_size * fblock_cnt;
- u8_buf += block_size * fblock_cnt;
- f->fpos += block_size * fblock_cnt;
+ size -= block_size * fblock_count;
+ u8_buf += block_size * fblock_count;
+ f->fpos += block_size * fblock_count;
if (rcnt)
- *rcnt += block_size * fblock_cnt;
+ *rcnt += block_size * fblock_count;
fblock_start = fblock;
- fblock_cnt = 1;
+ fblock_count = 1;
}
if (size) {
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if (r != EOK)
- goto Finish;
-
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ uint64_t off;
+ r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true);
if (r != EOK)
goto Finish;
- memcpy(u8_buf, b.data, size);
-
- r = ext4_block_set(f->mp->fs.bdev, &b);
+ off = fblock * block_size;
+ r = ext4_block_readbytes(f->mp->fs.bdev, off, u8_buf, size);
if (r != EOK)
goto Finish;
int ext4_fwrite(ext4_file *f, const void *buf, size_t size, size_t *wcnt)
{
- uint32_t u;
- uint32_t fblock;
-
- uint32_t sblock;
- uint32_t sblock_end;
- uint32_t file_blocks;
+ uint32_t unalg;
+ uint32_t iblk_idx;
+ uint32_t iblock_last;
+ uint32_t ifile_blocks;
uint32_t block_size;
- uint32_t fblock_start;
- uint32_t fblock_cnt;
- struct ext4_block b;
+ uint32_t fblock_count;
+ ext4_fsblk_t fblk;
+ ext4_fsblk_t fblock_start;
+
struct ext4_inode_ref ref;
const uint8_t *u8_buf = buf;
- int r;
+ int r, rr = EOK;
ext4_assert(f && f->mp);
return EOK;
EXT4_MP_LOCK(f->mp);
+ ext4_trans_start(f->mp);
+
+ struct ext4_fs *const fs = &f->mp->fs;
+ struct ext4_sblock *const sb = &f->mp->fs.sb;
if (wcnt)
*wcnt = 0;
- r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
+ r = ext4_fs_get_inode_ref(fs, f->inode, &ref);
if (r != EOK) {
+ ext4_trans_abort(f->mp);
EXT4_MP_UNLOCK(f->mp);
return r;
}
/*Sync file size*/
- f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
-
- block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
+ f->fsize = ext4_inode_get_size(sb, ref.inode);
+ block_size = ext4_sb_get_block_size(sb);
- sblock_end = (f->fpos + size) > f->fsize ? (f->fpos + size) : f->fsize;
- sblock_end /= block_size;
- file_blocks = (f->fsize / block_size);
+ iblock_last = (f->fpos + size) / block_size;
+ iblk_idx = (f->fpos) / block_size;
+ ifile_blocks = (f->fsize + block_size - 1) / block_size;
- if (f->fsize % block_size)
- file_blocks++;
+ unalg = (f->fpos) % block_size;
- sblock = (f->fpos) / block_size;
+ if (unalg) {
+ size_t len = size;
+ uint64_t off;
+ if (size > (block_size - unalg))
+ len = block_size - unalg;
- u = (f->fpos) % block_size;
-
- if (u) {
- uint32_t ll = size > (block_size - u) ? (block_size - u) : size;
-
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
+ r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
if (r != EOK)
goto Finish;
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ off = fblk * block_size + unalg;
+ r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, len);
if (r != EOK)
goto Finish;
- memcpy(b.data + u, u8_buf, ll);
- b.dirty = true;
-
- r = ext4_block_set(f->mp->fs.bdev, &b);
- if (r != EOK)
- goto Finish;
-
- u8_buf += ll;
- size -= ll;
- f->fpos += ll;
+ u8_buf += len;
+ size -= len;
+ f->fpos += len;
if (wcnt)
- *wcnt += ll;
+ *wcnt += len;
- sblock++;
+ iblk_idx++;
}
/*Start write back cache mode.*/
goto Finish;
fblock_start = 0;
- fblock_cnt = 0;
+ fblock_count = 0;
while (size >= block_size) {
- while (sblock < sblock_end) {
- if (sblock < file_blocks) {
- r = ext4_fs_get_inode_data_block_index(
- &ref, sblock, &fblock);
+ while (iblk_idx < iblock_last) {
+ if (iblk_idx < ifile_blocks) {
+ r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx,
+ &fblk);
if (r != EOK)
- break;
+ goto Finish;
} else {
- r = ext4_fs_append_inode_block(&ref, &fblock,
- &sblock);
- if (r != EOK)
+ rr = ext4_fs_append_inode_dblk(&ref, &fblk,
+ &iblk_idx);
+ if (rr != EOK) {
+ /* Unable to append more blocks. But
+ * some block might be allocated already
+ * */
break;
+ }
}
- sblock++;
+ iblk_idx++;
if (!fblock_start) {
- fblock_start = fblock;
+ fblock_start = fblk;
}
- if ((fblock_start + fblock_cnt) != fblock)
+ if ((fblock_start + fblock_count) != fblk)
break;
- fblock_cnt++;
+ fblock_count++;
}
r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
- fblock_cnt);
+ fblock_count);
if (r != EOK)
break;
- size -= block_size * fblock_cnt;
- u8_buf += block_size * fblock_cnt;
- f->fpos += block_size * fblock_cnt;
+ size -= block_size * fblock_count;
+ u8_buf += block_size * fblock_count;
+ f->fpos += block_size * fblock_count;
if (wcnt)
- *wcnt += block_size * fblock_cnt;
+ *wcnt += block_size * fblock_count;
- fblock_start = fblock;
- fblock_cnt = 1;
+ fblock_start = fblk;
+ fblock_count = 1;
+
+ if (rr != EOK) {
+ /*ext4_fs_append_inode_block has failed and no
+ * more blocks might be written. But node size
+ * should be updated.*/
+ r = rr;
+ goto out_fsize;
+ }
}
/*Stop write back cache mode*/
goto Finish;
if (size) {
- if (sblock < file_blocks) {
- r = ext4_fs_get_inode_data_block_index(&ref, sblock,
- &fblock);
+ uint64_t off;
+ if (iblk_idx < ifile_blocks) {
+ r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk);
if (r != EOK)
goto Finish;
} else {
- r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
+ r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx);
if (r != EOK)
- goto Finish;
+ /*Node size sholud be updated.*/
+ goto out_fsize;
}
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if (r != EOK)
- goto Finish;
-
- memcpy(b.data, u8_buf, size);
- b.dirty = true;
-
- r = ext4_block_set(f->mp->fs.bdev, &b);
+ off = fblk * block_size;
+ r = ext4_block_writebytes(f->mp->fs.bdev, off, u8_buf, size);
if (r != EOK)
goto Finish;
*wcnt += size;
}
+out_fsize:
if (f->fpos > f->fsize) {
f->fsize = f->fpos;
ext4_inode_set_size(ref.inode, f->fsize);
}
Finish:
- ext4_fs_put_inode_ref(&ref);
+ r = ext4_fs_put_inode_ref(&ref);
+
+ if (r != EOK)
+ ext4_trans_abort(f->mp);
+ else
+ ext4_trans_stop(f->mp);
+
EXT4_MP_UNLOCK(f->mp);
return r;
}
return EINVAL;
}
-uint64_t ext4_ftell(ext4_file *f) { return f->fpos; }
+uint64_t ext4_ftell(ext4_file *f)
+{
+ return f->fpos;
+}
-uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
+uint64_t ext4_fsize(ext4_file *f)
+{
+ return f->fsize;
+}
int ext4_chmod(const char *path, uint32_t mode)
{
return ENOENT;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
- r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_inode_set_mode(sb, inode_ref.inode, mode);
inode_ref.dirty = true;
- ext4_fs_put_inode_ref(&inode_ref);
+ r = ext4_fs_put_inode_ref(&inode_ref);
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
}
return ENOENT;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
- r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_inode_set_gid(inode_ref.inode, gid);
inode_ref.dirty = true;
- ext4_fs_put_inode_ref(&inode_ref);
+ r = ext4_fs_put_inode_ref(&inode_ref);
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
}
return ENOENT;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
- r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_inode_set_access_time(inode_ref.inode, atime);
inode_ref.dirty = true;
- ext4_fs_put_inode_ref(&inode_ref);
+ r = ext4_fs_put_inode_ref(&inode_ref);
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
}
return ENOENT;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
- r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
- ext4_inode_set_modification_time(inode_ref.inode, mtime);
+ ext4_inode_set_modif_time(inode_ref.inode, mtime);
inode_ref.dirty = true;
- ext4_fs_put_inode_ref(&inode_ref);
+ r = ext4_fs_put_inode_ref(&inode_ref);
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
}
return ENOENT;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
- r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
ext4_inode_set_change_inode_time(inode_ref.inode, ctime);
inode_ref.dirty = true;
- ext4_fs_put_inode_ref(&inode_ref);
+ r = ext4_fs_put_inode_ref(&inode_ref);
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
}
static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size)
{
- struct ext4_block b;
struct ext4_inode_ref ref;
- uint32_t sblock, fblock;
+ uint32_t sblock;
+ ext4_fsblk_t fblock;
uint32_t block_size;
int r;
return EOK;
r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
- if (r != EOK) {
- EXT4_MP_UNLOCK(f->mp);
+ if (r != EOK)
return r;
- }
/*Sync file size*/
block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
/*If the size of symlink is smaller than 60 bytes*/
if (size < sizeof(ref.inode->blocks)) {
- char *content = (char *)ref.inode->blocks;
- memset(content, 0, sizeof(ref.inode->blocks));
- memcpy(content, buf, size);
+ memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks));
+ memcpy(ref.inode->blocks, buf, size);
ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS);
} else {
ext4_fs_inode_blocks_init(&f->mp->fs, &ref);
- r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
+ r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock);
if (r != EOK)
goto Finish;
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ r = ext4_block_writebytes(f->mp->fs.bdev, 0, buf, size);
if (r != EOK)
goto Finish;
- memcpy(b.data, buf, size);
- b.dirty = true;
- r = ext4_block_set(f->mp->fs.bdev, &b);
- if (r != EOK)
- goto Finish;
}
/*Stop write back cache mode*/
if (!mp)
return ENOENT;
- filetype = EXT4_DIRECTORY_FILETYPE_SYMLINK;
+ filetype = EXT4_DE_SYMLINK;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
+
ext4_block_cache_write_back(mp->fs.bdev, 1);
- r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
+ r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, NULL, NULL);
if (r == EOK)
r = ext4_fsymlink_set(&f, target, strlen(target));
else
Finish:
ext4_block_cache_write_back(mp->fs.bdev, 0);
+
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
}
if (!buf)
return EINVAL;
- memset(buf, 0, sizeof(bufsize));
-
- filetype = EXT4_DIRECTORY_FILETYPE_SYMLINK;
+ filetype = EXT4_DE_SYMLINK;
EXT4_MP_LOCK(mp);
ext4_block_cache_write_back(mp->fs.bdev, 1);
- r = ext4_generic_open2(&f, path, O_RDONLY, filetype, 0, 0);
+ r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL);
if (r == EOK)
r = ext4_fread(&f, buf, bufsize, rcnt);
else
Finish:
ext4_block_cache_write_back(mp->fs.bdev, 0);
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
EXT4_MP_UNLOCK(mp);
return r;
}
+int ext4_setxattr(const char *path, const char *name, size_t name_len,
+ const void *data, size_t data_size, bool replace)
+{
+ int r = EOK;
+ ext4_file f;
+ uint32_t inode;
+ uint8_t name_index;
+ const char *dissected_name = NULL;
+ size_t dissected_len = 0;
+ struct ext4_xattr_ref xattr_ref;
+ struct ext4_inode_ref inode_ref;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ if (!mp)
+ return ENOENT;
+
+ dissected_name = ext4_extract_xattr_name(name, name_len,
+ &name_index, &dissected_len);
+ if (!dissected_len)
+ return EINVAL;
+
+ EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
+
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
+ if (r != EOK)
+ goto Finish;
+ inode = f.inode;
+ ext4_fclose(&f);
+
+ r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
+ if (r != EOK)
+ goto Finish;
+
+ r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&inode_ref);
+ goto Finish;
+ }
+
+ r = ext4_fs_set_xattr(&xattr_ref, name_index, dissected_name,
+ dissected_len, data, data_size, replace);
+
+ ext4_fs_put_xattr_ref(&xattr_ref);
+ ext4_fs_put_inode_ref(&inode_ref);
+Finish:
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
+ EXT4_MP_UNLOCK(mp);
+ return r;
+}
+
+int ext4_getxattr(const char *path, const char *name, size_t name_len,
+ void *buf, size_t buf_size, size_t *data_size)
+{
+ int r = EOK;
+ ext4_file f;
+ uint32_t inode;
+ uint8_t name_index;
+ const char *dissected_name = NULL;
+ size_t dissected_len = 0;
+ struct ext4_xattr_ref xattr_ref;
+ struct ext4_inode_ref inode_ref;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ if (!mp)
+ return ENOENT;
+
+ dissected_name = ext4_extract_xattr_name(name, name_len,
+ &name_index, &dissected_len);
+ if (!dissected_len)
+ return EINVAL;
+
+ EXT4_MP_LOCK(mp);
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
+ if (r != EOK)
+ goto Finish;
+ inode = f.inode;
+ ext4_fclose(&f);
+
+ r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
+ if (r != EOK)
+ goto Finish;
+
+ r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&inode_ref);
+ goto Finish;
+ }
+
+ r = ext4_fs_get_xattr(&xattr_ref, name_index, dissected_name,
+ dissected_len, buf, buf_size, data_size);
+
+ ext4_fs_put_xattr_ref(&xattr_ref);
+ ext4_fs_put_inode_ref(&inode_ref);
+Finish:
+ EXT4_MP_UNLOCK(mp);
+ return r;
+}
+
+struct ext4_listxattr_iterator {
+ char *list;
+ char *list_ptr;
+ size_t size;
+ size_t ret_size;
+ bool list_too_small;
+ bool get_required_size;
+};
+
+static int ext4_iterate_ea_list(struct ext4_xattr_ref *ref,
+ struct ext4_xattr_item *item)
+{
+ struct ext4_listxattr_iterator *lxi;
+ lxi = ref->iter_arg;
+ if (!lxi->get_required_size) {
+ size_t plen;
+ const char *prefix;
+ prefix = ext4_get_xattr_name_prefix(item->name_index, &plen);
+ if (lxi->ret_size + plen + item->name_len + 1 > lxi->size) {
+ lxi->list_too_small = 1;
+ return EXT4_XATTR_ITERATE_STOP;
+ }
+ if (prefix) {
+ memcpy(lxi->list_ptr, prefix, plen);
+ lxi->list_ptr += plen;
+ lxi->ret_size += plen;
+ }
+ memcpy(lxi->list_ptr, item->name, item->name_len);
+ lxi->list_ptr[item->name_len] = 0;
+ lxi->list_ptr += item->name_len + 1;
+ }
+ lxi->ret_size += item->name_len + 1;
+ return EXT4_XATTR_ITERATE_CONT;
+}
+
+int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size)
+{
+ int r = EOK;
+ ext4_file f;
+ uint32_t inode;
+ struct ext4_xattr_ref xattr_ref;
+ struct ext4_inode_ref inode_ref;
+ struct ext4_listxattr_iterator lxi;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ if (!mp)
+ return ENOENT;
+
+ lxi.list = list;
+ lxi.list_ptr = list;
+ lxi.size = size;
+ lxi.ret_size = 0;
+ lxi.list_too_small = false;
+ lxi.get_required_size = (!size) ? true : false;
+
+ EXT4_MP_LOCK(mp);
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
+ if (r != EOK)
+ goto Finish;
+ inode = f.inode;
+ ext4_fclose(&f);
+
+ r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
+ if (r != EOK)
+ goto Finish;
+
+ r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&inode_ref);
+ goto Finish;
+ }
+
+ xattr_ref.iter_arg = &lxi;
+ ext4_fs_xattr_iterate(&xattr_ref, ext4_iterate_ea_list);
+ if (lxi.list_too_small)
+ r = ERANGE;
+
+ if (r == EOK) {
+ if (ret_size)
+ *ret_size = lxi.ret_size;
+
+ }
+ ext4_fs_put_xattr_ref(&xattr_ref);
+ ext4_fs_put_inode_ref(&inode_ref);
+Finish:
+ EXT4_MP_UNLOCK(mp);
+ return r;
+
+}
+
+int ext4_removexattr(const char *path, const char *name, size_t name_len)
+{
+ int r = EOK;
+ ext4_file f;
+ uint32_t inode;
+ uint8_t name_index;
+ const char *dissected_name = NULL;
+ size_t dissected_len = 0;
+ struct ext4_xattr_ref xattr_ref;
+ struct ext4_inode_ref inode_ref;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ if (!mp)
+ return ENOENT;
+
+ dissected_name = ext4_extract_xattr_name(name, name_len,
+ &name_index, &dissected_len);
+ if (!dissected_len)
+ return EINVAL;
+
+ EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
+
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DE_UNKNOWN, NULL, NULL);
+ if (r != EOK)
+ goto Finish;
+ inode = f.inode;
+ ext4_fclose(&f);
+
+ r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref);
+ if (r != EOK)
+ goto Finish;
+
+ r = ext4_fs_get_xattr_ref(&mp->fs, &inode_ref, &xattr_ref);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&inode_ref);
+ goto Finish;
+ }
+
+ r = ext4_fs_remove_xattr(&xattr_ref, name_index, dissected_name,
+ dissected_len);
+
+ ext4_fs_put_xattr_ref(&xattr_ref);
+ ext4_fs_put_inode_ref(&inode_ref);
+Finish:
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
+
+ EXT4_MP_UNLOCK(mp);
+ return r;
+
+}
+
/*********************************DIRECTORY OPERATION************************/
int ext4_dir_rm(const char *path)
ext4_file f;
struct ext4_mountpoint *mp = ext4_get_mount(path);
- struct ext4_inode_ref current;
- struct ext4_inode_ref child;
- struct ext4_directory_iterator it;
+ struct ext4_inode_ref act, parent;
uint32_t name_off;
- uint32_t inode_up;
- uint32_t inode_current;
- uint32_t depth = 1;
+ uint32_t inode_up, inode_current;
- bool has_children;
+ bool has_children = false;
bool is_goal;
- bool dir_end;
if (!mp)
return ENOENT;
/*Check if exist.*/
r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
len = ext4_path_check(path, &is_goal);
inode_current = f.inode;
- dir_end = false;
ext4_block_cache_write_back(mp->fs.bdev, 1);
- do {
- /*Load directory node.*/
- r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, ¤t);
- if (r != EOK) {
- break;
- }
-
- /*Initialize iterator.*/
- r = ext4_dir_iterator_init(&it, ¤t, 0);
- if (r != EOK) {
- ext4_fs_put_inode_ref(¤t);
- break;
- }
-
- while (r == EOK) {
-
- if (!it.current) {
- dir_end = true;
- break;
- }
-
- /*Get up directory inode when ".." entry*/
- if ((it.current->name_length == 2) &&
- ext4_is_dots(it.current->name,
- it.current->name_length)) {
- inode_up = ext4_dir_entry_ll_get_inode(it.current);
- }
-
- /*If directory or file entry, but not "." ".." entry*/
- if (!ext4_is_dots(it.current->name,
- it.current->name_length)) {
-
- /*Get child inode reference do unlink
- * directory/file.*/
- r = ext4_fs_get_inode_ref(&f.mp->fs,
- ext4_dir_entry_ll_get_inode(it.current),
- &child);
- if (r != EOK)
- break;
-
- /*If directory with no leaf children*/
- r = ext4_has_children(&has_children, &child);
- if (r != EOK) {
- ext4_fs_put_inode_ref(&child);
- break;
- }
-
- if (has_children) {
- /*Has directory children. Go into this
- * directory.*/
- inode_up = inode_current;
- inode_current = ext4_dir_entry_ll_get_inode(it.current);
- depth++;
- ext4_fs_put_inode_ref(&child);
- break;
- }
-
- /*No children in child directory or file. Just
- * unlink.*/
- r = ext4_unlink(f.mp, ¤t, &child,
- (char *)it.current->name,
- it.current->name_length);
- if (r != EOK) {
- ext4_fs_put_inode_ref(&child);
- break;
- }
-
- ext4_inode_set_deletion_time(child.inode,
- 0xFFFFFFFF);
- ext4_inode_set_links_count(child.inode, 0);
- child.dirty = true;
- /*Turncate*/
- r = ext4_fs_truncate_inode(&child, 0);
- if (r != EOK) {
- ext4_fs_put_inode_ref(&child);
- break;
- }
+ /*Load parent.*/
+ r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
+ &parent);
+ if (r != EOK)
+ goto Finish;
+ r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current,
+ &act);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&parent);
+ goto Finish;
+ }
+ r = ext4_has_children(&has_children, &act);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&parent);
+ ext4_fs_put_inode_ref(&act);
+ goto Finish;
+ }
+ if (has_children) {
+ r = ENOTEMPTY;
+ ext4_fs_put_inode_ref(&parent);
+ ext4_fs_put_inode_ref(&act);
+ goto Finish;
+ }
- r = ext4_fs_free_inode(&child);
- if (r != EOK) {
- ext4_fs_put_inode_ref(&child);
- break;
- }
+ ext4_trans_start(mp);
- r = ext4_fs_put_inode_ref(&child);
- if (r != EOK)
- break;
- }
-
- r = ext4_dir_iterator_next(&it);
+ if (ext4_inode_get_links_cnt(act.inode) == 2) {
+ /*Truncate*/
+ r = ext4_fs_truncate_inode(&act, 0);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&parent);
+ ext4_fs_put_inode_ref(&act);
+ goto Finish;
}
+ }
- if (dir_end) {
- /*Directory iterator reached last entry*/
- ext4_has_children(&has_children, ¤t);
- if (!has_children) {
- inode_current = inode_up;
- if (depth)
- depth--;
- }
- /*Last unlink*/
- if (!depth) {
- /*Load parent.*/
- struct ext4_inode_ref parent;
- r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up,
- &parent);
- if (r != EOK)
- goto End;
-
- /* In this place all directories should be
- * unlinked.
- * Last unlink from root of current directory*/
- r = ext4_unlink(f.mp, &parent, ¤t,
- (char *)path, len);
- if (r != EOK) {
- ext4_fs_put_inode_ref(&parent);
- goto End;
- }
+ /* Unlink from root of current directory*/
+ r = ext4_unlink(f.mp, &parent, &act,
+ (char *)path, len);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&parent);
+ ext4_fs_put_inode_ref(&act);
+ goto Finish;
+ }
- if (ext4_inode_get_links_count(current.inode) ==
- 2) {
- ext4_inode_set_deletion_time(
- current.inode, 0xFFFFFFFF);
- ext4_inode_set_links_count(
- current.inode, 0);
- current.dirty = true;
- /*Turncate*/
- r = ext4_fs_truncate_inode(¤t, 0);
- if (r != EOK) {
- ext4_fs_put_inode_ref(&parent);
- goto End;
- }
-
- r = ext4_fs_free_inode(¤t);
- if (r != EOK) {
- ext4_fs_put_inode_ref(&parent);
- goto End;
- }
- }
+ if (ext4_inode_get_links_cnt(act.inode) == 2) {
+ ext4_inode_set_del_time(act.inode, -1L);
+ ext4_inode_set_links_cnt(act.inode, 0);
+ act.dirty = true;
- r = ext4_fs_put_inode_ref(&parent);
- if (r != EOK)
- goto End;
- }
+ r = ext4_fs_free_inode(&act);
+ if (r != EOK) {
+ ext4_fs_put_inode_ref(&parent);
+ ext4_fs_put_inode_ref(&act);
+ goto Finish;
}
+ }
- End:
- ext4_dir_iterator_fini(&it);
- ext4_fs_put_inode_ref(¤t);
- dir_end = false;
-
- /*When something goes wrong. End loop.*/
- if (r != EOK)
- break;
+ r = ext4_fs_put_inode_ref(&parent);
+ if (r != EOK)
+ goto Finish;
- } while (depth);
+ r = ext4_fs_put_inode_ref(&act);
+Finish:
+ if (r != EOK)
+ ext4_trans_abort(mp);
+ else
+ ext4_trans_stop(mp);
ext4_block_cache_write_back(mp->fs.bdev, 0);
EXT4_MP_UNLOCK(mp);
+
return r;
}
return ENOENT;
EXT4_MP_LOCK(mp);
+ ext4_trans_start(mp);
/*Check if exist.*/
r = ext4_generic_open(&f, path, "r", false, 0, 0);
if (r == EOK) {
/*Directory already created*/
+ ext4_trans_stop(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
/*Create new dir*/
r = ext4_generic_open(&f, path, "w", false, 0, 0);
if (r != EOK) {
+ ext4_trans_abort(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
+ ext4_trans_stop(mp);
EXT4_MP_UNLOCK(mp);
return r;
}
return r;
}
-int ext4_dir_close(ext4_dir *d) { return ext4_fclose(&d->f); }
+int ext4_dir_close(ext4_dir *d)
+{
+ return ext4_fclose(&d->f);
+}
const ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
{
int r;
ext4_direntry *de = 0;
struct ext4_inode_ref dir;
- struct ext4_directory_iterator it;
+ struct ext4_dir_iter it;
EXT4_MP_LOCK(d->f.mp);
goto Finish;
}
- memcpy(&d->de, it.current, sizeof(ext4_direntry));
+ memcpy(&d->de, it.curr, sizeof(ext4_direntry));
de = &d->de;
ext4_dir_iterator_next(&it);
- d->next_off =
- it.current ? it.current_offset : EXT4_DIR_ENTRY_OFFSET_TERM;
+ d->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM;
ext4_dir_iterator_fini(&it);
ext4_fs_put_inode_ref(&dir);
return de;
}
+void ext4_dir_entry_rewind(ext4_dir *d)
+{
+ d->next_off = 0;
+}
+
+int ext4_test_journal(const char *mount_point)
+{
+ struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
+ if (!mp)
+ return ENOENT;
+
+ int r = ENOTSUP;
+ EXT4_MP_LOCK(mp);
+ ext4_block_cache_write_back(mp->fs.bdev, 1);
+ if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) {
+ struct jbd_fs *jbd_fs = calloc(1, sizeof(struct jbd_fs));
+ struct jbd_journal *journal;
+ if (!jbd_fs) {
+ r = ENOMEM;
+ goto Finish;
+ }
+ journal = calloc(1, sizeof(struct jbd_journal));
+ if (!journal) {
+ free(jbd_fs);
+ r = ENOMEM;
+ goto Finish;
+ }
+
+ r = jbd_get_fs(&mp->fs, jbd_fs);
+ if (r != EOK) {
+ free(jbd_fs);
+ goto Finish;
+ }
+ r = jbd_journal_start(jbd_fs, journal);
+ if (r != EOK) {
+ jbd_put_fs(jbd_fs);
+ free(journal);
+ free(jbd_fs);
+ goto Finish;
+ }
+
+ int i;
+ for (i = 0;i < 50;i++) {
+ ext4_fsblk_t rand_block = rand() % 4096;
+ if (!rand_block)
+ rand_block = 1;
+ struct ext4_block block;
+ r = ext4_block_get(mp->fs.bdev, &block, rand_block);
+ if (r != EOK)
+ goto out;
+
+ struct jbd_trans *t = jbd_journal_new_trans(journal);
+ if (!t) {
+ ext4_block_set(mp->fs.bdev, &block);
+ r = ENOMEM;
+ goto out;
+ }
+
+ switch (rand() % 2) {
+ case 0:
+ r = jbd_trans_get_access(journal, t, &block);
+ if (r != EOK) {
+ jbd_journal_free_trans(journal, t,
+ true);
+ ext4_block_set(mp->fs.bdev, &block);
+ r = ENOMEM;
+ goto out;
+ }
+ r = jbd_trans_set_block_dirty(t, &block);
+ if (r != EOK) {
+ jbd_journal_free_trans(journal, t,
+ true);
+ ext4_block_set(mp->fs.bdev, &block);
+ r = ENOMEM;
+ goto out;
+ }
+ break;
+ case 1:
+ r = jbd_trans_try_revoke_block(t, rand_block);
+ if (r != EOK) {
+ jbd_journal_free_trans(journal, t,
+ true);
+ ext4_block_set(mp->fs.bdev, &block);
+ r = ENOMEM;
+ goto out;
+ }
+ }
+ ext4_block_set(mp->fs.bdev, &block);
+ jbd_journal_submit_trans(journal, t);
+ jbd_journal_commit_one(journal);
+ }
+out:
+ jbd_journal_stop(journal);
+ jbd_put_fs(jbd_fs);
+ free(journal);
+ free(jbd_fs);
+ }
+
+Finish:
+ ext4_block_cache_write_back(mp->fs.bdev, 0);
+ EXT4_MP_UNLOCK(mp);
+ return r;
+}
+
/**
* @}
*/