ext4_xattr: allow more entries to be inserted when space is small
[lwext4.git] / src / ext4.c
index e0923e4700c0ceef31ec9d226e25e4426d0b964f..c51dd8cf660fa42d2e4b0617b5cfe9ac69d9dd72 100644 (file)
  */
 
 #include "ext4_config.h"
-#include "ext4.h"
-#include "ext4_blockdev.h"
 #include "ext4_types.h"
-#include "ext4_debug.h"
+#include "ext4_misc.h"
 #include "ext4_errno.h"
+#include "ext4_oflags.h"
+#include "ext4_debug.h"
+
+#include "ext4.h"
+#include "ext4_trans.h"
+#include "ext4_blockdev.h"
 #include "ext4_fs.h"
 #include "ext4_dir.h"
 #include "ext4_inode.h"
 #include "ext4_super.h"
+#include "ext4_block_group.h"
 #include "ext4_dir_idx.h"
 #include "ext4_xattr.h"
 #include "ext4_journal.h"
@@ -561,7 +566,31 @@ static int __ext4_recover(const char *mount_point)
                jbd_put_fs(jbd_fs);
                free(jbd_fs);
        }
+       if (r == EOK && !mp->fs.read_only) {
+               uint32_t bgid;
+               uint64_t free_blocks_count = 0;
+               uint32_t free_inodes_count = 0;
+               struct ext4_block_group_ref bg_ref;
+
+               /* Update superblock's stats */
+               for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) {
+                       r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref);
+                       if (r != EOK)
+                               goto Finish;
 
+                       free_blocks_count +=
+                               ext4_bg_get_free_blocks_count(bg_ref.block_group,
+                                               &mp->fs.sb);
+                       free_inodes_count +=
+                               ext4_bg_get_free_inodes_count(bg_ref.block_group,
+                                               &mp->fs.sb);
+
+                       ext4_fs_put_block_group_ref(&bg_ref);
+               }
+               ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count);
+               ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count);
+               /* We don't need to save the superblock stats immediately. */
+       }
 
 Finish:
        EXT4_MP_UNLOCK(mp);
@@ -1645,9 +1674,6 @@ int ext4_fread(ext4_file *f, void *buf, size_t size, size_t *rcnt)
 
        ext4_assert(f && f->mp);
 
-       if (f->mp->fs.read_only)
-               return EROFS;
-
        if (f->flags & O_WRONLY)
                return EPERM;
 
@@ -2390,6 +2416,7 @@ Finish:
 int ext4_setxattr(const char *path, const char *name, size_t name_len,
                  const void *data, size_t data_size, bool replace)
 {
+       bool found;
        int r = EOK;
        ext4_file f;
        uint32_t inode;
@@ -2406,8 +2433,9 @@ int ext4_setxattr(const char *path, const char *name, size_t name_len,
                return EROFS;
 
        dissected_name = ext4_extract_xattr_name(name, name_len,
-                               &name_index, &dissected_len);
-       if (!dissected_len)
+                               &name_index, &dissected_len,
+                               &found);
+       if (!found)
                return EINVAL;
 
        EXT4_MP_LOCK(mp);
@@ -2447,6 +2475,7 @@ Finish:
 int ext4_getxattr(const char *path, const char *name, size_t name_len,
                  void *buf, size_t buf_size, size_t *data_size)
 {
+       bool found;
        int r = EOK;
        ext4_file f;
        uint32_t inode;
@@ -2460,8 +2489,9 @@ int ext4_getxattr(const char *path, const char *name, size_t name_len,
                return ENOENT;
 
        dissected_name = ext4_extract_xattr_name(name, name_len,
-                               &name_index, &dissected_len);
-       if (!dissected_len)
+                               &name_index, &dissected_len,
+                               &found);
+       if (!found)
                return EINVAL;
 
        EXT4_MP_LOCK(mp);
@@ -2582,6 +2612,7 @@ Finish:
 
 int ext4_removexattr(const char *path, const char *name, size_t name_len)
 {
+       bool found;
        int r = EOK;
        ext4_file f;
        uint32_t inode;
@@ -2598,8 +2629,9 @@ int ext4_removexattr(const char *path, const char *name, size_t name_len)
                return EROFS;
 
        dissected_name = ext4_extract_xattr_name(name, name_len,
-                                               &name_index, &dissected_len);
-       if (!dissected_len)
+                                               &name_index, &dissected_len,
+                                               &found);
+       if (!found)
                return EINVAL;
 
        EXT4_MP_LOCK(mp);
@@ -2883,6 +2915,11 @@ End:
        return r;
 }
 
+int ext4_dir_mv(const char *path, const char *new_path)
+{
+       return ext4_frename(path, new_path);
+}
+
 int ext4_dir_mk(const char *path)
 {
        int r;