Numorous changes. See below:
[lwext4.git] / lwext4 / ext4.c
index 203d7d44656b192320efcf279712f45248d32b47..1684edb0f3511f7bf2f880507f158432ea850dea 100644 (file)
@@ -251,7 +251,7 @@ static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent,
                                return EIO;
 
                        ext4_dir_en_set_inode(res.dentry, parent->index);
-                       ext4_bcache_set_dirty(res.block.buf);
+                       ext4_trans_set_block_dirty(res.block.buf);
                        r = ext4_dir_destroy_result(ch, &res);
                        if (r != EOK)
                                return r;
@@ -414,6 +414,7 @@ int ext4_mount(const char *dev_name, const char *mount_point)
                }
                return r;
        }
+       bd->fs = &mp->fs;
 
        return r;
 }
@@ -437,7 +438,7 @@ int ext4_umount(const char *mount_point)
 
        r = ext4_fs_fini(&mp->fs);
        if (r != EOK)
-               return r;
+               goto Finish;
 
        mp->mounted = 0;
 
@@ -446,8 +447,10 @@ int ext4_umount(const char *mount_point)
                ext4_bcache_fini_dynamic(mp->fs.bdev->bc);
                free(mp->fs.bdev->bc);
        }
-
-       return ext4_block_fini(mp->fs.bdev);
+       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)
@@ -555,7 +558,7 @@ Finish:
        return r;
 }
 
-int ext4_trans_start(struct ext4_mountpoint *mp)
+static int ext4_trans_start(struct ext4_mountpoint *mp)
 {
        int r = EOK;
        if (mp->fs.jbd_journal && !mp->fs.curr_trans) {
@@ -572,7 +575,7 @@ Finish:
        return r;
 }
 
-int ext4_trans_stop(struct ext4_mountpoint *mp)
+static int ext4_trans_stop(struct ext4_mountpoint *mp)
 {
        int r = EOK;
        if (mp->fs.jbd_journal && mp->fs.curr_trans) {
@@ -584,7 +587,7 @@ int ext4_trans_stop(struct ext4_mountpoint *mp)
        return r;
 }
 
-void ext4_trans_abort(struct ext4_mountpoint *mp)
+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;
@@ -594,29 +597,6 @@ void ext4_trans_abort(struct ext4_mountpoint *mp)
        }
 }
 
-int ext4_trans_get_write_access(struct ext4_fs *fs,
-                               struct ext4_block *block)
-{
-       int r = EOK;
-       if (fs->jbd_journal && fs->curr_trans) {
-               struct jbd_journal *journal = fs->jbd_journal;
-               struct jbd_trans *trans = fs->curr_trans;
-               r = jbd_trans_get_access(journal, trans, block);
-       }
-       return r;
-}
-
-int ext4_trans_set_block_dirty(struct ext4_fs *fs,
-                        struct ext4_block *block)
-{
-       int r = EOK;
-       if (fs->jbd_journal && fs->curr_trans) {
-               struct jbd_trans *trans = fs->curr_trans;
-               r = jbd_trans_set_block_dirty(trans, block);
-       }
-       return r;
-}
-
 int ext4_mount_point_stats(const char *mount_point,
                           struct ext4_mount_stats *stats)
 {
@@ -724,6 +704,77 @@ static bool ext4_parse_flags(const char *flags, uint32_t *file_flags)
        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_DIRENTRY_UNKNOWN,
  * any filetype of the target dir entry will be accepted.
@@ -766,6 +817,9 @@ static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
        if (parent_inode)
                *parent_inode = ref.index;
 
+       if (flags & O_CREAT)
+               ext4_trans_start(mp);
+
        len = ext4_path_check(path, &is_goal);
        while (1) {
 
@@ -875,7 +929,7 @@ static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
        if (is_goal) {
 
                if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) {
-                       r = ext4_fs_truncate_inode(&ref, 0);
+                       r = ext4_trunc_inode(mp, ref.index, 0);
                        if (r != EOK) {
                                ext4_fs_put_inode_ref(&ref);
                                return r;
@@ -893,6 +947,14 @@ static int ext4_generic_open2(ext4_file *f, const char *path, int flags,
        }
 
        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;
 }
 
@@ -1240,6 +1302,29 @@ int ext4_fremove(const char *path)
                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;
@@ -1254,14 +1339,6 @@ int ext4_fremove(const char *path)
        /*Link count is zero, the inode should be freed. */
        if (!ext4_inode_get_links_cnt(child.inode)) {
                ext4_inode_set_del_time(child.inode, -1L);
-               /*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;
 
                r = ext4_fs_free_inode(&child);
                if (r != EOK)
@@ -1330,17 +1407,11 @@ int ext4_fopen(ext4_file *f, const char *path, const char *flags)
                return ENOENT;
 
        EXT4_MP_LOCK(mp);
-       ext4_trans_start(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);
 
-       if (r != EOK)
-               ext4_trans_abort(mp);
-       else
-               ext4_trans_stop(mp);
-
        EXT4_MP_UNLOCK(mp);
        return r;
 }
@@ -1357,17 +1428,11 @@ int ext4_fopen2(ext4_file *f, const char *path, int flags)
         filetype = EXT4_DE_REG_FILE;
 
        EXT4_MP_LOCK(mp);
-       ext4_trans_start(mp);
 
        ext4_block_cache_write_back(mp->fs.bdev, 1);
        r = ext4_generic_open2(f, path, flags, filetype, NULL, NULL);
        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;
 }
@@ -1408,7 +1473,7 @@ static int ext4_ftruncate_no_lock(ext4_file *f, uint64_t size)
        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;
 
@@ -2134,6 +2199,12 @@ int ext4_fsymlink(const char *target, const char *path)
 
 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;
 }
@@ -2195,6 +2266,8 @@ int ext4_setxattr(const char *path, const char *name, size_t name_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;
@@ -2217,6 +2290,11 @@ int ext4_setxattr(const char *path, const char *name, size_t name_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;
 }
@@ -2377,6 +2455,8 @@ int ext4_removexattr(const char *path, const char *name, size_t name_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;
@@ -2399,6 +2479,11 @@ int ext4_removexattr(const char *path, const char *name, size_t name_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;
 
@@ -2413,26 +2498,18 @@ int ext4_dir_rm(const char *path)
        ext4_file f;
 
        struct ext4_mountpoint *mp = ext4_get_mount(path);
-       struct ext4_inode_ref act;
-       struct ext4_inode_ref child;
-       struct ext4_dir_iter 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;
 
        EXT4_MP_LOCK(mp);
-       ext4_trans_start(mp);
-
-       struct ext4_fs *const fs = &mp->fs;
 
        /*Check if exist.*/
        r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off);
@@ -2446,169 +2523,81 @@ int ext4_dir_rm(const char *path)
        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(fs, inode_current, &act);
-               if (r != EOK) {
-                       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;
+       }
+
+       ext4_trans_start(mp);
 
-               /*Initialize iterator.*/
-               r = ext4_dir_iterator_init(&it, &act, 0);
+       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);
-                       break;
-               }
-
-               while (r == EOK) {
-
-                       if (!it.curr) {
-                               dir_end = true;
-                               break;
-                       }
-
-                       /*Get up directory inode when ".." entry*/
-                       if ((it.curr->name_len == 2) &&
-                           ext4_is_dots(it.curr->name, it.curr->name_len)) {
-                               inode_up = ext4_dir_en_get_inode(it.curr);
-                       }
-
-                       /*If directory or file entry,  but not "." ".." entry*/
-                       if (!ext4_is_dots(it.curr->name, it.curr->name_len)) {
-
-                               /*Get child inode reference do unlink
-                                * directory/file.*/
-                               uint32_t cinode;
-                               cinode = ext4_dir_en_get_inode(it.curr);
-                               r = ext4_fs_get_inode_ref(fs, cinode, &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 = cinode;
-                                       depth++;
-                                       ext4_fs_put_inode_ref(&child);
-                                       break;
-                               }
-
-                               /*No children in child directory or file. Just
-                                * unlink.*/
-                               r = ext4_unlink(f.mp, &act, &child,
-                                               (char *)it.curr->name,
-                                               it.curr->name_len);
-                               if (r != EOK) {
-                                       ext4_fs_put_inode_ref(&child);
-                                       break;
-                               }
-
-                               ext4_inode_set_del_time(child.inode, -1L);
-                               ext4_inode_set_links_cnt(child.inode, 0);
-                               child.dirty = true;
-                               /*Turncate*/
-                               r = ext4_fs_truncate_inode(&child, 0);
-                               if (r != EOK) {
-                                       ext4_fs_put_inode_ref(&child);
-                                       break;
-                               }
-
-                               r = ext4_fs_free_inode(&child);
-                               if (r != EOK) {
-                                       ext4_fs_put_inode_ref(&child);
-                                       break;
-                               }
-
-                               r = ext4_fs_put_inode_ref(&child);
-                               if (r != EOK)
-                                       break;
-                       }
-
-                       r = ext4_dir_iterator_next(&it);
-               }
-
-               if (dir_end) {
-                       /*Directory iterator reached last entry*/
-                       ext4_has_children(&has_children, &act);
-                       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, &act,
-                                               (char *)path, len);
-                               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;
-                                       /*Turncate*/
-                                       r = ext4_fs_truncate_inode(&act, 0);
-                                       if (r != EOK) {
-                                               ext4_fs_put_inode_ref(&parent);
-                                               goto End;
-                                       }
-
-                                       r = ext4_fs_free_inode(&act);
-                                       if (r != EOK) {
-                                               ext4_fs_put_inode_ref(&parent);
-                                               goto End;
-                                       }
-                               }
-
-                               r = ext4_fs_put_inode_ref(&parent);
-                               if (r != EOK)
-                                       goto End;
-                       }
+                       goto Finish;
                }
+       }
 
-       End:
-               ext4_dir_iterator_fini(&it);
+        /* 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);
-               dir_end = false;
+               goto Finish;
+       }
 
-               /*When something goes wrong. End loop.*/
-               if (r != EOK)
-                       break;
+       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;
 
-       } while (depth);
+               r = ext4_fs_free_inode(&act);
+               if (r != EOK) {
+                       ext4_fs_put_inode_ref(&parent);
+                       ext4_fs_put_inode_ref(&act);
+                       goto Finish;
+               }
+       }
 
-       ext4_block_cache_write_back(mp->fs.bdev, 0);
-       EXT4_MP_UNLOCK(mp);
+       r = ext4_fs_put_inode_ref(&parent);
+       if (r != EOK)
+               goto Finish;
 
+       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;
 }