*/
#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 <stdlib.h>
#include <string.h>
return EOK;
}
- struct ext4_directory_iterator it;
+ struct ext4_dir_iterator it;
int rc = ext4_dir_iterator_init(&it, enode, 0);
if (rc != EOK)
return rc;
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)
+ uint32_t name_len, bool rename)
{
/* Check maximum name length */
if (name_len > EXT4_DIRECTORY_FILENAME_LEN)
*/
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);
+ !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 (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
+ rc = ext4_dir_dx_init(child, parent);
if (rc != EOK)
return rc;
ext4_inode_set_flag(child->inode,
EXT4_INODE_FLAG_INDEX);
child->dirty = true;
- }
+ } else
#endif
+ {
+ 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);
ext4_fs_inode_links_count_inc(parent);
child->dirty = true;
parent->dirty = true;
} else {
+ /*
+ * In case we want to rename a directory,
+ * we reset the original '..' pointer.
+ */
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;
+ struct ext4_dir_search_result result;
if (!has_flag_index) {
rc = ext4_dir_find_entry(&result,
child, "..",
ext4_fs_inode_links_count_inc(parent);
parent->dirty = true;
- } else {
+ }
+ if (!rename) {
ext4_fs_inode_links_count_inc(child);
child->dirty = true;
}
/* Cannot unlink non-empty node */
if (has_children)
- return ENOTSUP;
+ return ENOTEMPTY;
/* Remove entry from parent directory */
rc = ext4_dir_remove_entry(parent, name, name_len);
return ext4_block_fini(mp->fs.bdev);
}
+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_mount_point_stats(const char *mount_point,
struct ext4_mount_stats *stats)
{
- uint32_t i;
- struct ext4_mountpoint *mp = 0;
+ struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
- for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if (!strcmp(_mp[i].name, mount_point)) {
- mp = &_mp[i];
- break;
- }
- }
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;
}
/*
- * 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,
uint32_t *name_off)
{
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;
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 (!len) {
/*If root open was request.*/
if (is_goal &&
- ((filetype == EXT4_DIRECTORY_FILETYPE_DIR) ||
- (filetype == EXT4_DIRECTORY_FILETYPE_UNKNOWN)))
+ ((filetype == EXT4_DIRENTRY_DIR) ||
+ (filetype == EXT4_DIRENTRY_UNKNOWN)))
break;
r = ENOENT;
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;
struct ext4_inode_ref child_ref;
r = ext4_fs_alloc_inode(
&mp->fs, &child_ref,
- is_goal ? (filetype == EXT4_DIRECTORY_FILETYPE_DIR)
- : true);
+ is_goal ? filetype
+ : EXT4_DIRENTRY_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);
*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);
+ if (ext4_sb_feature_incom(&mp->fs.sb, EXT4_FINCOM_FILETYPE)) {
+ int inode_type =
+ ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
+ result.dentry);
+ inode_mode = ext4_fs_correspond_inode_mode(inode_type);
+ } else {
+ struct ext4_inode_ref child_ref;
+ r = ext4_fs_get_inode_ref(&mp->fs, next_inode,
+ &child_ref);
+ if (r != EOK)
+ break;
+
+ inode_mode = ext4_inode_type(&mp->fs.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 (inode_mode != EXT4_INODE_MODE_DIRECTORY &&
+ !is_goal) {
r = ENOENT;
break;
}
- if (filetype != EXT4_DIRECTORY_FILETYPE_UNKNOWN) {
- if ((inode_type != filetype) && is_goal) {
+ if (filetype != EXT4_DIRENTRY_UNKNOWN) {
+ if ((inode_mode != ext4_fs_correspond_inode_mode(filetype)) &&
+ is_goal) {
r = ENOENT;
break;
}
if (is_goal) {
if ((f->flags & O_TRUNC) &&
- (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE)) {
+ (inode_mode == EXT4_INODE_MODE_FILE)) {
r = ext4_fs_truncate_inode(&ref, 0);
if (r != EOK) {
if (f->flags & O_APPEND)
f->fpos = f->fsize;
+
}
r = ext4_fs_put_inode_ref(&ref);
return EINVAL;
if (file_expect == true)
- filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
+ filetype = EXT4_DIRENTRY_REG_FILE;
else
- filetype = EXT4_DIRECTORY_FILETYPE_DIR;
+ filetype = EXT4_DIRENTRY_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)
+ 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;
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)
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(&mp->fs.sb, EXT4_FINCOM_FILETYPE)) {
+ int inode_type =
+ ext4_dir_entry_ll_get_inode_type(&mp->fs.sb,
+ result.dentry);
+ inode_mode = ext4_fs_correspond_inode_mode(inode_type);
+ } else {
+ struct ext4_inode_ref child_ref;
+ r = ext4_fs_get_inode_ref(&mp->fs, next_inode,
+ &child_ref);
+ if (r != EOK)
+ break;
+
+ inode_mode = ext4_inode_type(&mp->fs.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 (inode_mode != EXT4_INODE_MODE_DIRECTORY) {
if (is_goal)
r = EEXIST;
else
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:
- if (r != EOK)
- ext4_fs_put_inode_ref(child_ref);
-
- ext4_fs_put_inode_ref(parent_ref);
return r;
}
EXT4_MP_LOCK(mp);
r = ext4_generic_open2(&f, path, O_RDONLY,
- EXT4_DIRECTORY_FILETYPE_UNKNOWN,
+ EXT4_DIRENTRY_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_MP_LOCK(mp);
r = ext4_generic_open2(&f, path, O_RDONLY,
- EXT4_DIRECTORY_FILETYPE_UNKNOWN,
+ EXT4_DIRENTRY_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;
return ENOENT;
EXT4_MP_LOCK(mp);
- r = ext4_generic_open(&f, path, "r", true, &parent_inode, &name_off);
+ r = ext4_generic_open2(&f, path, O_RDWR,
+ EXT4_DIRENTRY_UNKNOWN,
+ &parent_inode, &name_off);
if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
return r;
}
-int ext4_fill_raw_inode(const char *mount_point, uint32_t ino,
+int ext4_fill_raw_inode(const char *path,
+ uint32_t *ret_ino,
struct ext4_inode *inode)
{
int r;
+ ext4_file f;
struct ext4_inode_ref inode_ref;
- struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ uint32_t ino;
if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
+ r = ext4_generic_open2(&f, path, O_RDONLY,
+ EXT4_DIRENTRY_UNKNOWN,
+ NULL, NULL);
+ if (r != EOK) {
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+
+ ino = f.inode;
+ ext4_fclose(&f);
+
/*Load parent*/
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
ext4_fs_put_inode_ref(&inode_ref);
EXT4_MP_UNLOCK(mp);
+
+ if (ret_ino)
+ *ret_ino = ino;
+
return r;
}
return r;
}
-int ext4_fopen2(ext4_file *f, const char *path, int flags, bool file_expect)
+int ext4_fopen2(ext4_file *f, const char *path, int flags)
{
struct ext4_mountpoint *mp = ext4_get_mount(path);
int r;
if (!mp)
return ENOENT;
- if (file_expect == true)
- filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
- else
- filetype = EXT4_DIRECTORY_FILETYPE_DIR;
+ filetype = EXT4_DIRENTRY_REG_FILE;
EXT4_MP_LOCK(mp);
ext4_block_cache_write_back(mp->fs.bdev, 1);
goto Finish;
}
- if ((ext4_inode_get_mode(&f->mp->fs.sb, ref.inode) & EXT4_INODE_MODE_SOFTLINK)
- == EXT4_INODE_MODE_SOFTLINK
- && f->fsize < sizeof(ref.inode->blocks)
- && !ext4_inode_get_blocks_count(&f->mp->fs.sb, ref.inode)) {
- char *content = (char *)ref.inode->blocks;
- memset(content + size, 0, sizeof(ref.inode->blocks) - size);
- ext4_inode_set_size(ref.inode, size);
- ref.dirty = true;
-
- f->fsize = size;
- if (f->fpos > size)
- f->fpos = size;
-
- r = EOK;
- goto Finish;
- }
-
/*Start write back cache mode.*/
r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
if (r != EOK)
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 unaligned;
+ 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;
block_size = ext4_sb_get_block_size(&f->mp->fs.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;
- if (u) {
+ iblock_idx = (f->fpos) / block_size;
+ iblock_last = (f->fpos + size) / block_size;
+ unaligned = (f->fpos) % block_size;
- uint32_t ll = size > (block_size - u) ? (block_size - u) : 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)) {
+ char *content = (char *)ref.inode->blocks;
+ if (f->fpos < f->fsize) {
+ r = (unaligned + size > f->fsize)
+ ?(f->fsize - unaligned)
+ :(size);
+ memcpy(buf, content + unaligned, r);
+ if (rcnt)
+ *rcnt = r;
- r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if (r != EOK)
- goto Finish;
+ } else {
+ r = 0;
+ if (rcnt)
+ *rcnt = 0;
- r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if (r != EOK)
- goto Finish;
+ }
- memcpy(u8_buf, b.data + u, ll);
+ r = EOK;
+ goto Finish;
+ }
- r = ext4_block_set(f->mp->fs.bdev, &b);
+ if (unaligned) {
+ uint32_t l = size > (block_size - unaligned) ?
+ (block_size - unaligned) : size;
+ r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
+ true);
if (r != EOK)
goto Finish;
- u8_buf += ll;
- size -= ll;
- f->fpos += ll;
+ /* Do we get an unwritten range? */
+ if (fblock != 0) {
+ r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ if (r != EOK)
+ goto Finish;
+
+ memcpy(u8_buf, b.data + unaligned, l);
+
+ r = ext4_block_set(f->mp->fs.bdev, &b);
+ if (r != EOK)
+ goto Finish;
+ } else {
+ /* Yes, we do. */
+ memset(u8_buf, 0, l);
+ }
+
+ u8_buf += l;
+ size -= l;
+ f->fpos += l;
if (rcnt)
- *rcnt += ll;
+ *rcnt += l;
- 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_data_block_index(&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);
+ r = ext4_fs_get_inode_data_block_index(&ref, iblock_idx, &fblock,
+ true);
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 unaligned;
+ uint32_t iblock_idx;
+ uint32_t iblock_last;
+ uint32_t ifile_blocks;
uint32_t block_size;
- uint32_t fblock_start;
- uint32_t fblock_cnt;
+
+ uint32_t fblock_count;
+ ext4_fsblk_t fblock;
+ ext4_fsblk_t fblock_start;
struct ext4_block b;
struct ext4_inode_ref ref;
const uint8_t *u8_buf = buf;
- int r;
+ int r, rr = EOK;
ext4_assert(f && f->mp);
/*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);
- sblock_end = (f->fpos + size) > f->fsize ? (f->fpos + size) : f->fsize;
- sblock_end /= block_size;
- file_blocks = (f->fsize / block_size);
-
- if (f->fsize % block_size)
- file_blocks++;
-
- sblock = (f->fpos) / block_size;
+ iblock_last = (f->fpos + size) / block_size;
+ iblock_idx = (f->fpos) / block_size;
+ ifile_blocks = (f->fsize + block_size - 1) / block_size;
- u = (f->fpos) % block_size;
+ unaligned = (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);
+ if (unaligned) {
+ uint32_t l = size > (block_size - unaligned) ?
+ (block_size - unaligned) : size;
+ r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx, &fblock);
if (r != EOK)
goto Finish;
if (r != EOK)
goto Finish;
- memcpy(b.data + u, u8_buf, ll);
+ memcpy(b.data + unaligned, u8_buf, l);
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 += l;
+ size -= l;
+ f->fpos += l;
if (wcnt)
- *wcnt += ll;
+ *wcnt += l;
- sblock++;
+ iblock_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 (iblock_idx < iblock_last) {
+ if (iblock_idx < ifile_blocks) {
+ r = ext4_fs_init_inode_data_block_index(
+ &ref, iblock_idx, &fblock);
if (r != EOK)
- break;
+ goto Finish;
} else {
- r = ext4_fs_append_inode_block(&ref, &fblock,
- &sblock);
- if (r != EOK)
+ rr = ext4_fs_append_inode_block(&ref, &fblock,
+ &iblock_idx);
+ if (rr != EOK) {
+ /* Unable to append more blocks. But some
+ * block might be allocated already.
+ * */
break;
+ }
}
- sblock++;
+ iblock_idx++;
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_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_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,
+ if (iblock_idx < ifile_blocks) {
+ r = ext4_fs_init_inode_data_block_index(&ref, iblock_idx,
&fblock);
if (r != EOK)
goto Finish;
} else {
- r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
+ r = ext4_fs_append_inode_block(&ref, &fblock, &iblock_idx);
if (r != EOK)
- goto Finish;
+ /*Node size sholud be updated.*/
+ goto out_fsize;
}
r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
*wcnt += size;
}
+out_fsize:
if (f->fpos > f->fsize) {
f->fsize = f->fpos;
ext4_inode_set_size(ref.inode, f->fsize);
uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
-int ext4_fchmod(ext4_file *f, uint32_t mode)
+int ext4_chmod(const char *path, uint32_t mode)
{
int r;
uint32_t ino;
+ ext4_file f;
struct ext4_sblock *sb;
struct ext4_inode_ref inode_ref;
- struct ext4_mountpoint *mp = f->mp;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
- ino = f->inode;
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
+ if (r != EOK) {
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+ ino = f.inode;
+ sb = &mp->fs.sb;
+ ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
- sb = &f->mp->fs.sb;
ext4_inode_set_mode(sb, inode_ref.inode, mode);
inode_ref.dirty = true;
return r;
}
-int ext4_fchown(ext4_file *f, uint32_t uid, uint32_t gid)
+int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
{
int r;
+ ext4_file f;
uint32_t ino;
struct ext4_inode_ref inode_ref;
- struct ext4_mountpoint *mp = f->mp;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
- ino = f->inode;
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
+ if (r != EOK) {
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+ ino = f.inode;
+ ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
-int ext4_file_set_atime(ext4_file *f, uint32_t atime)
+int ext4_file_set_atime(const char *path, uint32_t atime)
{
int r;
+ ext4_file f;
uint32_t ino;
struct ext4_inode_ref inode_ref;
- struct ext4_mountpoint *mp = f->mp;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
- ino = f->inode;
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
+ if (r != EOK) {
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+ ino = f.inode;
+ ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
-int ext4_file_set_mtime(ext4_file *f, uint32_t mtime)
+int ext4_file_set_mtime(const char *path, uint32_t mtime)
{
int r;
+ ext4_file f;
uint32_t ino;
struct ext4_inode_ref inode_ref;
- struct ext4_mountpoint *mp = f->mp;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
- ino = f->inode;
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
+ if (r != EOK) {
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+ ino = f.inode;
+ ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
-int ext4_file_set_ctime(ext4_file *f, uint32_t ctime)
+int ext4_file_set_ctime(const char *path, uint32_t ctime)
{
int r;
+ ext4_file f;
uint32_t ino;
struct ext4_inode_ref inode_ref;
- struct ext4_mountpoint *mp = f->mp;
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
- ino = f->inode;
+ r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRENTRY_UNKNOWN, 0, 0);
+ if (r != EOK) {
+ EXT4_MP_UNLOCK(mp);
+ return r;
+ }
+ ino = f.inode;
+ ext4_fclose(&f);
r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
if (r != EOK) {
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;
+ ext4_fsblk_t fblock;
+ uint32_t block_size;
+ int r;
+
+ ext4_assert(f && f->mp);
+
+ if (!size)
+ return EOK;
+
+ r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
+ if (r != EOK) {
+ EXT4_MP_UNLOCK(f->mp);
+ return r;
+ }
+
+ /*Sync file size*/
+ block_size = ext4_sb_get_block_size(&f->mp->fs.sb);
+ if (size > block_size) {
+ r = EINVAL;
+ goto Finish;
+ }
+ r = ext4_ftruncate_no_lock(f, 0);
+ if (r != EOK)
+ goto Finish;
+
+ /*Start write back cache mode.*/
+ r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
+ if (r != EOK)
+ goto Finish;
+
+ /*If the size of symlink is smaller than 60 bytes*/
+ if (size < sizeof(ref.inode->blocks)) {
+ 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);
+ if (r != EOK)
+ goto Finish;
+
+ r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
+ 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*/
+ ext4_block_cache_write_back(f->mp->fs.bdev, 0);
+
+ if (r != EOK)
+ goto Finish;
+
+ ext4_inode_set_size(ref.inode, size);
+ ref.dirty = true;
+
+ f->fsize = size;
+ if (f->fpos > size)
+ f->fpos = size;
+
+Finish:
+ ext4_fs_put_inode_ref(&ref);
+ return r;
+}
+
+int ext4_fsymlink(const char *target, const char *path)
+{
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ int r;
+ ext4_file f;
+ int filetype;
+
+ if (!mp)
+ return ENOENT;
+
+ filetype = EXT4_DIRENTRY_SYMLINK;
+
+ EXT4_MP_LOCK(mp);
+ ext4_block_cache_write_back(mp->fs.bdev, 1);
+ r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
+ if (r == EOK)
+ r = ext4_fsymlink_set(&f, target, strlen(target));
+ else
+ goto Finish;
+
+ ext4_fclose(&f);
+
+Finish:
+ ext4_block_cache_write_back(mp->fs.bdev, 0);
+ EXT4_MP_UNLOCK(mp);
+ return r;
+}
+
+int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
+{
+ struct ext4_mountpoint *mp = ext4_get_mount(path);
+ int r;
+ ext4_file f;
+ int filetype;
+
+ if (!mp)
+ return ENOENT;
+
+ if (!buf)
+ return EINVAL;
+
+ filetype = EXT4_DIRENTRY_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);
+ if (r == EOK)
+ r = ext4_fread(&f, buf, bufsize, rcnt);
+ else
+ goto Finish;
+
+ ext4_fclose(&f);
+
+Finish:
+ ext4_block_cache_write_back(mp->fs.bdev, 0);
+ 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);
+ r = ext4_generic_open2(&f, path, O_RDWR,
+ EXT4_DIRENTRY_UNKNOWN, 0, 0);
+ 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:
+ 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_DIRENTRY_UNKNOWN, 0, 0);
+ 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 prefix_len;
+ const char *prefix;
+ prefix = ext4_get_xattr_name_prefix(item->name_index,
+ &prefix_len);
+ if (lxi->ret_size +
+ prefix_len + item->name_len + 1 >
+ lxi->size) {
+
+ lxi->list_too_small = 1;
+ return EXT4_XATTR_ITERATE_STOP;
+ }
+ if (prefix) {
+ memcpy(lxi->list_ptr, prefix, prefix_len);
+ lxi->list_ptr += prefix_len;
+ lxi->ret_size += prefix_len;
+ }
+ 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_DIRENTRY_UNKNOWN, 0, 0);
+ 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);
+ r = ext4_generic_open2(&f, path, O_RDWR,
+ EXT4_DIRENTRY_UNKNOWN, 0, 0);
+ 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:
+ EXT4_MP_UNLOCK(mp);
+ return r;
+
+}
+
/*********************************DIRECTORY OPERATION************************/
int ext4_dir_rm(const char *path)
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_dir_iterator it;
uint32_t name_off;
uint32_t inode_up;
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_iterator it;
EXT4_MP_LOCK(d->f.mp);
return de;
}
+void ext4_dir_entry_rewind(ext4_dir *d)
+{
+ d->next_off = 0;
+}
+
/**
* @}
*/