ref: c3b88a14d1ad01e8fb1ab27c60064fd207e00754
parent: 1ba0937309fac055b6c0eb5bbcdbb6f7d0947e0f
author: gkostka <kostka.grzegorz@gmail.com>
date: Tue Jun 16 06:01:17 EDT 2015
clang-format: ext4
--- a/lwext4/ext4.c
+++ b/lwext4/ext4.c
@@ -51,18 +51,25 @@
#include <ext4.h>
/**@brief Mount point OS dependent lock*/
-#define EXT4_MP_LOCK(_m) \
- do { if((_m)->os_locks) (_m)->os_locks->lock(); }while(0)
+#define EXT4_MP_LOCK(_m) \
+ do { \
+ if ((_m)->os_locks) \
+ (_m)->os_locks->lock(); \
+ } while (0)
/**@brief Mount point OS dependent unlock*/
-#define EXT4_MP_UNLOCK(_m) \
- do { if((_m)->os_locks) (_m)->os_locks->unlock(); }while(0)
+#define EXT4_MP_UNLOCK(_m) \
+ do { \
+ if ((_m)->os_locks) \
+ (_m)->os_locks->unlock(); \
+ } while (0)
/**@brief Mount point descrpitor.*/
-struct ext4_mountpoint {
+struct ext4_mountpoint
+{
/**@brief Mount done flag.*/
- bool mounted;
+ bool mounted;
/**@brief Mount point name (@ref ext4_mount)*/
char name[32];
@@ -78,7 +85,8 @@
};
/**@brief Block devices descriptor.*/
-struct _ext4_devices {
+struct _ext4_devices
+{
/**@brief Block device name (@ref ext4_device_register)*/
char name[32];
@@ -93,19 +101,17 @@
/**@brief Block devices.*/
struct _ext4_devices _bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT];
-
/**@brief Mountpoints.*/
struct ext4_mountpoint _mp[CONFIG_EXT4_MOUNTPOINTS_COUNT];
-
int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
- const char *dev_name)
+ const char *dev_name)
{
uint32_t i;
ext4_assert(bd && dev_name);
for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
- if(!_bdevices[i].bd){
+ if (!_bdevices[i].bd) {
strcpy(_bdevices[i].name, dev_name);
_bdevices[i].bd = bd;
_bdevices[i].bc = bc;
@@ -112,7 +118,7 @@
return EOK;
}
- if(!strcmp(_bdevices[i].name, dev_name))
+ if (!strcmp(_bdevices[i].name, dev_name))
return EOK;
}
return ENOSPC;
@@ -120,7 +126,6 @@
/****************************************************************************/
-
static bool ext4_is_dots(const uint8_t *name, size_t name_size)
{
if ((name_size == 1) && (name[0] == '.'))
@@ -137,8 +142,7 @@
struct ext4_fs *fs = enode->fs;
/* Check if node is directory */
- if (!ext4_inode_is_type(&fs->sb, enode->inode,
- EXT4_INODE_MODE_DIRECTORY)) {
+ if (!ext4_inode_is_type(&fs->sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) {
*has_children = false;
return EOK;
}
@@ -153,8 +157,7 @@
while (it.current != NULL) {
if (it.current->inode != 0) {
uint16_t name_size =
- ext4_dir_entry_ll_get_name_length(&fs->sb,
- it.current);
+ ext4_dir_entry_ll_get_name_length(&fs->sb, it.current);
if (!ext4_is_dots(it.current->name, name_size)) {
found = true;
break;
@@ -177,32 +180,29 @@
return EOK;
}
-
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 *child, const char *name,
+ uint32_t name_len)
{
/* Check maximum name length */
- if(name_len > EXT4_DIRECTORY_FILENAME_LEN)
+ if (name_len > EXT4_DIRECTORY_FILENAME_LEN)
return EINVAL;
/* Add entry to parent directory */
- int rc = ext4_dir_add_entry(parent, name, name_len,
- child);
+ int rc = ext4_dir_add_entry(parent, name, name_len, child);
if (rc != EOK)
return rc;
/* Fill new dir -> add '.' and '..' entries */
if (ext4_inode_is_type(&mp->fs.sb, child->inode,
- EXT4_INODE_MODE_DIRECTORY)) {
- rc = ext4_dir_add_entry(child, ".", strlen("."),
- child);
+ EXT4_INODE_MODE_DIRECTORY)) {
+ 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);
+ 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("."));
@@ -215,13 +215,12 @@
#if CONFIG_DIR_INDEX_ENABLE
/* Initialize directory index if supported */
if (ext4_sb_has_feature_compatible(&mp->fs.sb,
- EXT4_FEATURE_COMPAT_DIR_INDEX)) {
+ 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);
+ ext4_inode_set_flag(child->inode, EXT4_INODE_FLAG_INDEX);
child->dirty = true;
}
#endif
@@ -229,7 +228,6 @@
ext4_fs_inode_links_count_inc(parent);
child->dirty = true;
parent->dirty = true;
-
}
return EOK;
@@ -236,8 +234,9 @@
}
static int ext4_unlink(struct ext4_mountpoint *mp,
- struct ext4_inode_ref *parent, struct ext4_inode_ref *child_inode_ref,
- const char *name, uint32_t name_len)
+ struct ext4_inode_ref *parent,
+ struct ext4_inode_ref *child_inode_ref, const char *name,
+ uint32_t name_len)
{
bool has_children;
int rc = ext4_has_children(&has_children, child_inode_ref);
@@ -254,11 +253,11 @@
return rc;
bool is_dir = ext4_inode_is_type(&mp->fs.sb, child_inode_ref->inode,
- EXT4_INODE_MODE_DIRECTORY);
+ 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_assert(ext4_inode_get_links_count(child_inode_ref->inode) == 1);
ext4_fs_inode_links_count_dec(parent);
parent->dirty = true;
}
@@ -285,10 +284,9 @@
return EOK;
}
-
/****************************************************************************/
-int ext4_mount(const char * dev_name, char *mount_point)
+int ext4_mount(const char *dev_name, char *mount_point)
{
ext4_assert(mount_point && dev_name);
int r = EOK;
@@ -299,12 +297,12 @@
struct ext4_bcache *bc = 0;
struct ext4_mountpoint *mp = 0;
- if(mount_point[strlen(mount_point) - 1] != '/')
+ if (mount_point[strlen(mount_point) - 1] != '/')
return ENOTSUP;
for (i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) {
- if(_bdevices[i].name){
- if(!strcmp(dev_name, _bdevices[i].name)){
+ if (_bdevices[i].name) {
+ if (!strcmp(dev_name, _bdevices[i].name)) {
bd = _bdevices[i].bd;
bc = _bdevices[i].bc;
break;
@@ -312,11 +310,11 @@
}
}
- if(!bd)
+ if (!bd)
return ENODEV;
for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if(!_mp[i].mounted){
+ if (!_mp[i].mounted) {
strcpy(_mp[i].name, mount_point);
_mp[i].mounted = 1;
mp = &_mp[i];
@@ -323,20 +321,20 @@
break;
}
- if(!strcmp(_mp[i].name, mount_point)){
+ if (!strcmp(_mp[i].name, mount_point)) {
return EOK;
}
}
- if(!mp)
+ if (!mp)
return ENOMEM;
r = ext4_block_init(bd);
- if(r != EOK)
+ if (r != EOK)
return r;
r = ext4_fs_init(&mp->fs, bd);
- if(r != EOK){
+ if (r != EOK) {
ext4_block_fini(bd);
return r;
}
@@ -346,13 +344,13 @@
mp->cache_dynamic = 0;
- if(!bc){
+ if (!bc) {
/*Automatic block cache alloc.*/
mp->cache_dynamic = 1;
bc = malloc(sizeof(struct ext4_bcache));
r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE, bsize);
- if(r != EOK){
+ if (r != EOK) {
free(bc);
ext4_block_fini(bd);
return r;
@@ -359,14 +357,14 @@
}
}
- if(bsize != bc->itemsize)
+ if (bsize != bc->itemsize)
return ENOTSUP;
/*Bind block cache to block device*/
r = ext4_block_bind_bcache(bd, bc);
- if(r != EOK){
+ if (r != EOK) {
ext4_block_fini(bd);
- if(mp->cache_dynamic){
+ if (mp->cache_dynamic) {
ext4_bcache_fini_dynamic(bc);
free(bc);
}
@@ -376,7 +374,6 @@
return r;
}
-
int ext4_umount(char *mount_point)
{
int i;
@@ -384,22 +381,22 @@
struct ext4_mountpoint *mp = 0;
for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if(!strcmp(_mp[i].name, mount_point)){
+ if (!strcmp(_mp[i].name, mount_point)) {
mp = &_mp[i];
break;
}
}
- if(!mp)
+ if (!mp)
return ENODEV;
r = ext4_fs_fini(&mp->fs);
- if(r != EOK)
+ if (r != EOK)
return r;
mp->mounted = 0;
- if(mp->cache_dynamic){
+ if (mp->cache_dynamic) {
ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
free(mp->fs.bdev->bc);
}
@@ -408,30 +405,30 @@
}
int ext4_mount_point_stats(const char *mount_point,
- struct ext4_mount_stats *stats)
+ struct ext4_mount_stats *stats)
{
uint32_t i;
- struct ext4_mountpoint *mp = 0;
+ struct ext4_mountpoint *mp = 0;
for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if(!strcmp(_mp[i].name, mount_point)){
+ if (!strcmp(_mp[i].name, mount_point)) {
mp = &_mp[i];
break;
}
}
- if(!mp)
+ if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
- stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
+ stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count);
stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count);
- stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
+ stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb);
stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb);
- stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
+ stats->block_size = ext4_sb_get_block_size(&mp->fs.sb);
stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb);
- stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
- stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
+ stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group);
+ stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group);
memcpy(stats->volume_name, mp->fs.sb.volume_name, 16);
EXT4_MP_UNLOCK(mp);
@@ -439,19 +436,19 @@
return EOK;
}
-int ext4_mount_setup_locks(const char * mount_point,
- const struct ext4_lock *locks)
+int ext4_mount_setup_locks(const char *mount_point,
+ const struct ext4_lock *locks)
{
uint32_t i;
- struct ext4_mountpoint *mp = 0;
+ struct ext4_mountpoint *mp = 0;
for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) {
- if(!strcmp(_mp[i].name, mount_point)){
+ if (!strcmp(_mp[i].name, mount_point)) {
mp = &_mp[i];
break;
}
}
- if(!mp)
+ if (!mp)
return ENOENT;
mp->os_locks = locks;
@@ -460,32 +457,32 @@
/********************************FILE OPERATIONS*****************************/
-static struct ext4_mountpoint* ext4_get_mount(const char *path)
+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)
+ if (!_mp[i].mounted)
continue;
- if(!strncmp(_mp[i].name, path, strlen(_mp[i].name)))
+ 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)
+static int ext4_path_check(const char *path, bool *is_goal)
{
int i;
for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) {
- if(path[i] == '/'){
+ if (path[i] == '/') {
*is_goal = false;
return i;
}
- if(path[i] == 0){
+ if (path[i] == 0) {
*is_goal = true;
return i;
}
@@ -496,35 +493,38 @@
static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
{
- if(!flags)
+ if (!flags)
return false;
- if(!strcmp(flags, "r") || !strcmp(flags, "rb")){
+ if (!strcmp(flags, "r") || !strcmp(flags, "rb")) {
*file_flags = O_RDONLY;
return true;
}
- if(!strcmp(flags, "w") || !strcmp(flags, "wb")){
+ if (!strcmp(flags, "w") || !strcmp(flags, "wb")) {
*file_flags = O_WRONLY | O_CREAT | O_TRUNC;
return true;
}
- if(!strcmp(flags, "a") || !strcmp(flags, "ab")){
+ if (!strcmp(flags, "a") || !strcmp(flags, "ab")) {
*file_flags = O_WRONLY | O_CREAT | O_APPEND;
return true;
}
- if(!strcmp(flags, "r+") || !strcmp(flags, "rb+") || !strcmp(flags, "r+b")){
+ if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") ||
+ !strcmp(flags, "r+b")) {
*file_flags = O_RDWR;
return true;
}
- if(!strcmp(flags, "w+") || !strcmp(flags, "wb+") || !strcmp(flags, "w+b")){
+ if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") ||
+ !strcmp(flags, "w+b")) {
*file_flags = O_RDWR | O_CREAT | O_TRUNC;
return true;
}
- if(!strcmp(flags, "a+") || !strcmp(flags, "ab+") || !strcmp(flags, "a+b")){
+ if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") ||
+ !strcmp(flags, "a+b")) {
*file_flags = O_RDWR | O_CREAT | O_APPEND;
return true;
}
@@ -534,8 +534,9 @@
/****************************************************************************/
-static int ext4_generic_open (ext4_file *f, const char *path,
- const char *flags, bool file_expect, uint32_t *parent_inode, uint32_t *name_off)
+static int ext4_generic_open(ext4_file *f, const char *path, const char *flags,
+ bool file_expect, uint32_t *parent_inode,
+ uint32_t *name_off)
{
struct ext4_mountpoint *mp = ext4_get_mount(path);
struct ext4_directory_search_result result;
@@ -547,36 +548,36 @@
f->mp = 0;
- if(!mp)
+ if (!mp)
return ENOENT;
- if(ext4_parse_flags(flags, &f->flags) == false)
+ if (ext4_parse_flags(flags, &f->flags) == false)
return EINVAL;
/*Skip mount point*/
path += strlen(mp->name);
- if(name_off)
+ if (name_off)
*name_off = strlen(mp->name);
/*Load root*/
r = ext4_fs_get_inode_ref(&mp->fs, EXT4_INODE_ROOT_INDEX, &ref);
- if(r != EOK)
+ if (r != EOK)
return r;
- if(parent_inode)
+ if (parent_inode)
*parent_inode = ref.index;
int len = ext4_path_check(path, &is_goal);
- while(1){
+ while (1) {
len = ext4_path_check(path, &is_goal);
- if(!len){
+ if (!len) {
/*If root open was request.*/
- if(is_goal && !file_expect)
+ if (is_goal && !file_expect)
break;
r = ENOENT;
@@ -584,18 +585,19 @@
}
r = ext4_dir_find_entry(&result, &ref, path, len);
- if(r != EOK){
+ if (r != EOK) {
- if(r != ENOENT)
+ if (r != ENOENT)
break;
- if(!(f->flags & O_CREAT))
+ if (!(f->flags & O_CREAT))
break;
/*O_CREAT allows create new entry*/
struct ext4_inode_ref child_ref;
- r = ext4_fs_alloc_inode(&mp->fs, &child_ref, is_goal ? !file_expect : true);
- if(r != EOK)
+ r = ext4_fs_alloc_inode(&mp->fs, &child_ref,
+ is_goal ? !file_expect : true);
+ if (r != EOK)
break;
/*Destroy last result*/
@@ -603,7 +605,7 @@
/*Link with root dir.*/
r = ext4_link(mp, &ref, &child_ref, path, len);
- if(r != EOK){
+ if (r != EOK) {
/*Fali. Free new inode.*/
ext4_fs_free_inode(&child_ref);
/*We do not want to write new inode.
@@ -618,59 +620,60 @@
continue;
}
- if(parent_inode)
+ if (parent_inode)
*parent_inode = ref.index;
next_inode = result.dentry->inode;
- inode_type = ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
+ inode_type =
+ ext4_dir_entry_ll_get_inode_type(&mp->fs.sb, result.dentry);
r = ext4_dir_destroy_result(&ref, &result);
- if(r != EOK)
+ if (r != EOK)
break;
/*If expected file error*/
- if((inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE)
- && !file_expect && is_goal){
+ if ((inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE) && !file_expect &&
+ is_goal) {
r = ENOENT;
break;
}
/*If expected directory error*/
- if((inode_type == EXT4_DIRECTORY_FILETYPE_DIR)
- && file_expect && is_goal){
+ if ((inode_type == EXT4_DIRECTORY_FILETYPE_DIR) && file_expect &&
+ is_goal) {
r = ENOENT;
break;
}
r = ext4_fs_put_inode_ref(&ref);
- if(r != EOK)
+ if (r != EOK)
break;
r = ext4_fs_get_inode_ref(&mp->fs, next_inode, &ref);
- if(r != EOK)
+ if (r != EOK)
break;
- if(is_goal)
+ if (is_goal)
break;
path += len + 1;
- if(name_off)
+ if (name_off)
*name_off += len + 1;
};
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&ref);
return r;
}
- if(is_goal){
+ if (is_goal) {
- if((f->flags & O_TRUNC) &&
- (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE)){
+ if ((f->flags & O_TRUNC) &&
+ (inode_type == EXT4_DIRECTORY_FILETYPE_REG_FILE)) {
r = ext4_fs_truncate_inode(&ref, 0);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&ref);
return r;
}
@@ -679,9 +682,9 @@
f->mp = mp;
f->fsize = ext4_inode_get_size(&f->mp->fs.sb, ref.inode);
f->inode = ref.index;
- f->fpos = 0;
+ f->fpos = 0;
- if(f->flags & O_APPEND)
+ if (f->flags & O_APPEND)
f->fpos = f->fsize;
}
@@ -695,7 +698,7 @@
{
struct ext4_mountpoint *mp = ext4_get_mount(path);
- if(!mp)
+ if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
@@ -704,10 +707,9 @@
return EOK;
}
-
int ext4_fremove(const char *path)
{
- ext4_file f;
+ ext4_file f;
uint32_t parent_inode;
uint32_t name_off;
int r;
@@ -718,12 +720,12 @@
struct ext4_inode_ref child;
struct ext4_inode_ref parent;
- if(!mp)
+ if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
r = ext4_generic_open(&f, path, "r", true, &parent_inode, &name_off);
- if(r != EOK){
+ if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
@@ -730,7 +732,7 @@
/*Load parent*/
r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent);
- if(r != EOK){
+ if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
@@ -737,7 +739,7 @@
/*We have file to delete. Load it.*/
r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &child);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&parent);
EXT4_MP_UNLOCK(mp);
return r;
@@ -749,7 +751,7 @@
r = ext4_fs_truncate_inode(&child, 0);
ext4_block_cache_write_back(mp->fs.bdev, 0);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
/*Set path*/
@@ -759,14 +761,14 @@
/*Unlink from parent.*/
r = ext4_unlink(mp, &parent, &child, path, len);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
r = ext4_fs_free_inode(&child);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
- Finish:
+Finish:
ext4_fs_put_inode_ref(&child);
ext4_fs_put_inode_ref(&parent);
EXT4_MP_UNLOCK(mp);
@@ -773,12 +775,12 @@
return r;
}
-int ext4_fopen (ext4_file *f, const char *path, const char *flags)
+int ext4_fopen(ext4_file *f, const char *path, const char *flags)
{
struct ext4_mountpoint *mp = ext4_get_mount(path);
int r;
- if(!mp)
+ if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
@@ -793,10 +795,10 @@
{
ext4_assert(f && f->mp);
- f->mp = 0;
+ f->mp = 0;
f->flags = 0;
f->inode = 0;
- f->fpos = f->fsize = 0;
+ f->fpos = f->fsize = 0;
return EOK;
}
@@ -816,19 +818,19 @@
ext4_assert(f && f->mp);
- if(f->flags & O_WRONLY)
+ if (f->flags & O_WRONLY)
return EPERM;
- if(!size)
+ if (!size)
return EOK;
EXT4_MP_LOCK(f->mp);
- if(rcnt)
+ if (rcnt)
*rcnt = 0;
r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
- if(r != EOK){
+ if (r != EOK) {
EXT4_MP_UNLOCK(f->mp);
return r;
}
@@ -836,7 +838,6 @@
/*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);
size = size > (f->fsize - f->fpos) ? (f->fsize - f->fpos) : size;
sblock = (f->fpos) / block_size;
@@ -843,30 +844,29 @@
sblock_end = (f->fpos + size) / block_size;
u = (f->fpos) % block_size;
+ if (u) {
- 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(r != EOK)
+ if (r != EOK)
goto Finish;
r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
memcpy(u8_buf, b.data + u, ll);
r = ext4_block_set(f->mp->fs.bdev, &b);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
- u8_buf += ll;
- size -= ll;
+ u8_buf += ll;
+ size -= ll;
f->fpos += ll;
- if(rcnt)
+ if (rcnt)
*rcnt += ll;
sblock++;
@@ -874,33 +874,34 @@
fblock_start = 0;
fblock_cnt = 0;
- while(size >= block_size){
- while(sblock < sblock_end){
+ while (size >= block_size) {
+ while (sblock < sblock_end) {
r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
sblock++;
- if(!fblock_start){
+ if (!fblock_start) {
fblock_start = fblock;
}
- if((fblock_start + fblock_cnt) != fblock)
+ if ((fblock_start + fblock_cnt) != fblock)
break;
fblock_cnt++;
}
- r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start, fblock_cnt);
- if(r != EOK)
+ r = ext4_blocks_get_direct(f->mp->fs.bdev, u8_buf, fblock_start,
+ fblock_cnt);
+ if (r != EOK)
goto Finish;
- size -= block_size * fblock_cnt;
- u8_buf += block_size * fblock_cnt;
+ size -= block_size * fblock_cnt;
+ u8_buf += block_size * fblock_cnt;
f->fpos += block_size * fblock_cnt;
- if(rcnt)
+ if (rcnt)
*rcnt += block_size * fblock_cnt;
fblock_start = fblock;
@@ -907,28 +908,28 @@
fblock_cnt = 1;
}
- if(size){
+ if (size) {
r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
- memcpy(u8_buf, b.data , size);
+ memcpy(u8_buf, b.data, size);
r = ext4_block_set(f->mp->fs.bdev, &b);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
f->fpos += size;
- if(rcnt)
+ if (rcnt)
*rcnt += size;
}
- Finish:
+Finish:
ext4_fs_put_inode_ref(&ref);
EXT4_MP_UNLOCK(f->mp);
return r;
@@ -949,23 +950,24 @@
struct ext4_block b;
struct ext4_inode_ref ref;
const uint8_t *u8_buf = buf;
- int r;;
+ int r;
+ ;
ext4_assert(f && f->mp);
- if(f->flags & O_RDONLY)
+ if (f->flags & O_RDONLY)
return EPERM;
- if(!size)
+ if (!size)
return EOK;
EXT4_MP_LOCK(f->mp);
- if(wcnt)
+ if (wcnt)
*wcnt = 0;
r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref);
- if(r != EOK){
+ if (r != EOK) {
EXT4_MP_UNLOCK(f->mp);
return r;
}
@@ -979,7 +981,7 @@
sblock_end /= block_size;
file_blocks = (f->fsize / block_size);
- if(f->fsize % block_size)
+ if (f->fsize % block_size)
file_blocks++;
sblock = (f->fpos) / block_size;
@@ -986,16 +988,15 @@
u = (f->fpos) % block_size;
-
- if(u){
+ 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(r != EOK)
+ if (r != EOK)
goto Finish;
r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
memcpy(b.data + u, u8_buf, ll);
@@ -1002,62 +1003,61 @@
b.dirty = true;
r = ext4_block_set(f->mp->fs.bdev, &b);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
- u8_buf += ll;
- size -= ll;
+ u8_buf += ll;
+ size -= ll;
f->fpos += ll;
- if(wcnt)
+ if (wcnt)
*wcnt += ll;
sblock++;
}
-
/*Start write back cache mode.*/
r = ext4_block_cache_write_back(f->mp->fs.bdev, 1);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
fblock_start = 0;
fblock_cnt = 0;
- while(size >= block_size){
+ while (size >= block_size) {
- while(sblock < sblock_end){
- if(sblock < file_blocks){
+ while (sblock < sblock_end) {
+ if (sblock < file_blocks) {
r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
+ if (r != EOK)
break;
- }
- else {
+ } else {
r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
- if(r != EOK)
+ if (r != EOK)
break;
}
sblock++;
- if(!fblock_start){
+ if (!fblock_start) {
fblock_start = fblock;
}
- if((fblock_start + fblock_cnt) != fblock)
+ if ((fblock_start + fblock_cnt) != fblock)
break;
fblock_cnt++;
}
- r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start, fblock_cnt);
- if(r != EOK)
+ r = ext4_blocks_set_direct(f->mp->fs.bdev, u8_buf, fblock_start,
+ fblock_cnt);
+ if (r != EOK)
break;
- size -= block_size * fblock_cnt;
- u8_buf += block_size * fblock_cnt;
+ size -= block_size * fblock_cnt;
+ u8_buf += block_size * fblock_cnt;
f->fpos += block_size * fblock_cnt;
- if(wcnt)
+ if (wcnt)
*wcnt += block_size * fblock_cnt;
fblock_start = fblock;
@@ -1067,86 +1067,77 @@
/*Stop write back cache mode*/
ext4_block_cache_write_back(f->mp->fs.bdev, 0);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
- if(size){
- if(sblock < file_blocks){
+ if (size) {
+ if (sblock < file_blocks) {
r = ext4_fs_get_inode_data_block_index(&ref, sblock, &fblock);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
- }
- else {
+ } else {
r = ext4_fs_append_inode_block(&ref, &fblock, &sblock);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
}
r = ext4_block_get(f->mp->fs.bdev, &b, fblock);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
- memcpy(b.data, u8_buf , size);
+ memcpy(b.data, u8_buf, size);
b.dirty = true;
r = ext4_block_set(f->mp->fs.bdev, &b);
- if(r != EOK)
+ if (r != EOK)
goto Finish;
f->fpos += size;
- if(wcnt)
+ if (wcnt)
*wcnt += size;
}
- if(f->fpos > f->fsize){
+ if (f->fpos > f->fsize) {
f->fsize = f->fpos;
ext4_inode_set_size(ref.inode, f->fsize);
ref.dirty = true;
}
- Finish:
+Finish:
ext4_fs_put_inode_ref(&ref);
EXT4_MP_UNLOCK(f->mp);
return r;
-
}
int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin)
{
- switch(origin){
+ switch (origin) {
case SEEK_SET:
- if(offset > f->fsize)
+ if (offset > f->fsize)
return EINVAL;
f->fpos = offset;
return EOK;
case SEEK_CUR:
- if((offset + f->fpos) > f->fsize)
+ if ((offset + f->fpos) > f->fsize)
return EINVAL;
f->fpos += offset;
return EOK;
case SEEK_END:
- if(offset > f->fsize)
+ if (offset > f->fsize)
return EINVAL;
f->fpos = f->fsize - offset;
return EOK;
-
}
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; }
/*********************************DIRECTORY OPERATION************************/
@@ -1170,7 +1161,7 @@
bool is_goal;
bool dir_end;
- if(!mp)
+ if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
@@ -1177,7 +1168,7 @@
/*Check if exist.*/
r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
- if(r != EOK){
+ if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
@@ -1193,46 +1184,46 @@
do {
/*Load directory node.*/
r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, ¤t);
- if(r != EOK){
+ if (r != EOK) {
break;
}
/*Initialize iterator.*/
r = ext4_dir_iterator_init(&it, ¤t, 0);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(¤t);
break;
}
- while(r == EOK){
+ while (r == EOK) {
- if(!it.current){
+ 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)){
+ if ((it.current->name_length == 2) &&
+ ext4_is_dots(it.current->name, it.current->name_length)) {
inode_up = it.current->inode;
}
/*If directory or file entry, but not "." ".." entry*/
- if(!ext4_is_dots(it.current->name, it.current->name_length)){
+ 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, it.current->inode, &child);
- if(r != EOK)
+ if (r != EOK)
break;
/*If directory with no leaf children*/
r = ext4_has_children(&has_children, &child);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&child);
break;
}
- if(has_children){
+ if (has_children) {
/*Has directory children. Go into this tirectory.*/
inode_up = inode_current;
inode_current = it.current->inode;
@@ -1243,7 +1234,7 @@
/*Directory is empty. Truncate it.*/
r = ext4_fs_truncate_inode(&child, 0);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&child);
break;
}
@@ -1250,20 +1241,21 @@
/*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){
+ (char *)it.current->name,
+ it.current->name_length);
+ if (r != EOK) {
ext4_fs_put_inode_ref(&child);
break;
}
r = ext4_fs_free_inode(&child);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&child);
break;
}
r = ext4_fs_put_inode_ref(&child);
- if(r != EOK)
+ if (r != EOK)
break;
}
@@ -1270,24 +1262,24 @@
r = ext4_dir_iterator_next(&it);
}
- if(dir_end){
+ if (dir_end) {
/*Directory iterator reached last entry*/
ext4_has_children(&has_children, ¤t);
- if(!has_children){
+ if (!has_children) {
inode_current = inode_up;
- if(depth)
+ if (depth)
depth--;
}
/*Last unlink*/
- if(!depth){
+ if (!depth) {
/*Load parent.*/
struct ext4_inode_ref parent;
r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up, &parent);
- if(r != EOK)
+ if (r != EOK)
goto End;
r = ext4_fs_truncate_inode(¤t, 0);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&parent);
goto End;
}
@@ -1295,33 +1287,33 @@
/* 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){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&parent);
goto End;
}
r = ext4_fs_free_inode(¤t);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&parent);
goto End;
}
r = ext4_fs_put_inode_ref(&parent);
- if(r != EOK)
+ if (r != EOK)
goto End;
}
}
- End:
+ End:
ext4_dir_iterator_fini(&it);
ext4_fs_put_inode_ref(¤t);
dir_end = false;
/*When something goes wrong. End loop.*/
- if(r != EOK)
+ if (r != EOK)
break;
- }while(depth);
+ } while (depth);
ext4_block_cache_write_back(mp->fs.bdev, 0);
EXT4_MP_UNLOCK(mp);
@@ -1335,7 +1327,7 @@
struct ext4_mountpoint *mp = ext4_get_mount(path);
- if(!mp)
+ if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
@@ -1342,7 +1334,7 @@
/*Check if exist.*/
r = ext4_generic_open(&f, path, "r", false, 0, 0);
- if(r == EOK){
+ if (r == EOK) {
/*Directory already created*/
EXT4_MP_UNLOCK(mp);
return r;
@@ -1350,7 +1342,7 @@
/*Create new dir*/
r = ext4_generic_open(&f, path, "w", false, 0, 0);
- if(r != EOK){
+ if (r != EOK) {
EXT4_MP_UNLOCK(mp);
return r;
}
@@ -1359,12 +1351,12 @@
return r;
}
-int ext4_dir_open (ext4_dir *d, const char *path)
+int ext4_dir_open(ext4_dir *d, const char *path)
{
struct ext4_mountpoint *mp = ext4_get_mount(path);
int r;
- if(!mp)
+ if (!mp)
return ENOENT;
EXT4_MP_LOCK(mp);
@@ -1374,12 +1366,9 @@
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); }
-ext4_direntry* ext4_dir_entry_next(ext4_dir *d)
+ext4_direntry *ext4_dir_entry_next(ext4_dir *d)
{
#define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1)
@@ -1390,16 +1379,16 @@
EXT4_MP_LOCK(d->f.mp);
- if(d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM)
+ if (d->next_off == EXT4_DIR_ENTRY_OFFSET_TERM)
return 0;
r = ext4_fs_get_inode_ref(&d->f.mp->fs, d->f.inode, &dir);
- if(r != EOK){
+ if (r != EOK) {
goto Finish;
}
r = ext4_dir_iterator_init(&it, &dir, d->next_off);
- if(r != EOK){
+ if (r != EOK) {
ext4_fs_put_inode_ref(&dir);
goto Finish;
}
@@ -1414,7 +1403,7 @@
ext4_dir_iterator_fini(&it);
ext4_fs_put_inode_ref(&dir);
- Finish:
+Finish:
EXT4_MP_UNLOCK(d->f.mp);
return de;
}
--- a/lwext4/ext4.h
+++ b/lwext4/ext4.h
@@ -45,51 +45,52 @@
/********************************FILE OPEN FLAGS*****************************/
#ifndef O_RDONLY
-#define O_RDONLY 00
+#define O_RDONLY 00
#endif
#ifndef O_WRONLY
-#define O_WRONLY 01
+#define O_WRONLY 01
#endif
#ifndef O_RDWR
-#define O_RDWR 02
+#define O_RDWR 02
#endif
#ifndef O_CREAT
-#define O_CREAT 0100
+#define O_CREAT 0100
#endif
#ifndef O_EXCL
-#define O_EXCL 0200
+#define O_EXCL 0200
#endif
#ifndef O_TRUNC
-#define O_TRUNC 01000
+#define O_TRUNC 01000
#endif
#ifndef O_APPEND
-#define O_APPEND 02000
+#define O_APPEND 02000
#endif
/********************************FILE SEEK FLAGS*****************************/
#ifndef SEEK_SET
-#define SEEK_SET 0
+#define SEEK_SET 0
#endif
#ifndef SEEK_CUR
-#define SEEK_CUR 1
+#define SEEK_CUR 1
#endif
#ifndef SEEK_END
-#define SEEK_END 2
+#define SEEK_END 2
#endif
/********************************OS LOCK INFERFACE***************************/
/**@brief OS dependent lock interface.*/
-struct ext4_lock {
+struct ext4_lock
+{
/**@brief Lock access to mount point*/
void (*lock)(void);
@@ -98,11 +99,11 @@
void (*unlock)(void);
};
-
/********************************FILE DESCRIPTOR*****************************/
/**@brief File descriptor*/
-typedef struct ext4_file {
+typedef struct ext4_file
+{
/**@brief Mount point handle.*/
struct ext4_mountpoint *mp;
@@ -118,11 +119,11 @@
/**@brief File position*/
uint64_t fpos;
-}ext4_file;
+} ext4_file;
/*****************************DIRECTORY DESCRIPTOR***************************/
/**@brief Directory entry types. Copy from ext4_types.h*/
-enum {
+enum {
EXT4_DIRENTRY_UNKNOWN = 0,
EXT4_DIRENTRY_REG_FILE,
EXT4_DIRENTRY_DIR,
@@ -134,15 +135,17 @@
};
/**@brief Directory entry descriptor. Copy from ext4_types.h*/
-typedef struct {
+typedef struct
+{
uint32_t inode;
uint16_t entry_length;
uint8_t name_length;
uint8_t inode_type;
uint8_t name[255];
-}ext4_direntry;
+} ext4_direntry;
-typedef struct {
+typedef struct
+{
/**@brief File descriptor*/
ext4_file f;
/**@brief Current directory entry.*/
@@ -149,7 +152,7 @@
ext4_direntry de;
/**@brief Next entry offset*/
uint64_t next_off;
-}ext4_dir;
+} ext4_dir;
/********************************MOUNT OPERATIONS****************************/
@@ -163,7 +166,7 @@
* @param dev_name register name
* @param standard error code*/
int ext4_device_register(struct ext4_blockdev *bd, struct ext4_bcache *bc,
- const char *dev_name);
+ const char *dev_name);
/**@brief Mount a block device with EXT4 partition to the mount point.
* @param dev_name block device name (@ref ext4_device_register)
@@ -173,7 +176,7 @@
* - /my_second_partition/
*
* @return standard error code */
-int ext4_mount(const char * dev_name, char *mount_point);
+int ext4_mount(const char *dev_name, char *mount_point);
/**@brief Umount operation.
* @param mount_point mount name
@@ -180,9 +183,9 @@
* @return standard error code */
int ext4_umount(char *mount_point);
-
/**@brief Some of the filesystem stats.*/
-struct ext4_mount_stats {
+struct ext4_mount_stats
+{
uint32_t inodes_count;
uint32_t free_inodes_count;
uint64_t blocks_count;
@@ -201,14 +204,14 @@
* @param stats ext fs stats
* @return standard error code */
int ext4_mount_point_stats(const char *mount_point,
- struct ext4_mount_stats *stats);
+ struct ext4_mount_stats *stats);
/**@brief Setup OS lock routines.
* @param mount_point mount path
* @param locks - lock and unlock functions
* @return standard error code */
-int ext4_mount_setup_locks(const char * mount_point,
- const struct ext4_lock *locks);
+int ext4_mount_setup_locks(const char *mount_point,
+ const struct ext4_lock *locks);
/**@brief Enable/disable write back cache mode.
* @warning Default model of cache is write trough. It means that when You do:
@@ -273,7 +276,7 @@
* |---------------------------------------------------------------|
*
* @return standard error code*/
-int ext4_fopen (ext4_file *f, const char *path, const char *flags);
+int ext4_fopen(ext4_file *f, const char *path, const char *flags);
/**@brief File close function.
* @param f file handle
@@ -286,7 +289,7 @@
* @param size bytes to read
* @param rcnt bytes read (may be NULL)
* @return standard error code*/
-int ext4_fread (ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt);
+int ext4_fread(ext4_file *f, void *buf, uint32_t size, uint32_t *rcnt);
/**@brief Write data to file.
* @param f file handle
@@ -304,17 +307,17 @@
* @ref SEEK_CUR
* @ref SEEK_END
* @return standard error code*/
-int ext4_fseek (ext4_file *f, uint64_t offset, uint32_t origin);
+int ext4_fseek(ext4_file *f, uint64_t offset, uint32_t origin);
/**@brief Get file position.
* @param f file handle
* @return actual file position */
-uint64_t ext4_ftell (ext4_file *f);
+uint64_t ext4_ftell(ext4_file *f);
/**@brief Get file size.
* @param f file handle
* @return file size */
-uint64_t ext4_fsize (ext4_file *f);
+uint64_t ext4_fsize(ext4_file *f);
/*********************************DIRECTORY OPERATION***********************/
@@ -332,7 +335,7 @@
* @param d directory handle
* @param path directory path
* @return standard error code*/
-int ext4_dir_open (ext4_dir *d, const char *path);
+int ext4_dir_open(ext4_dir *d, const char *path);
/**@brief Directory close.
* @param d directory handle
@@ -343,7 +346,7 @@
* @param d directory handle
* @param id entry id
* @return directory entry id (NULL if no entry)*/
-ext4_direntry* ext4_dir_entry_next(ext4_dir *d);
+ext4_direntry *ext4_dir_entry_next(ext4_dir *d);
#endif /* EXT4_H_ */