]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blobdiff - fs/btrfs/free-space-cache.c
Btrfs: fix broken free space cache after the system crashed
[mirror_ubuntu-hirsute-kernel.git] / fs / btrfs / free-space-cache.c
index a852e15173e547c7b60fd2ecff5b3b1b90a6bf80..2b0a627cb5f94e414d3c9751f5e8e384e39c42f9 100644 (file)
@@ -680,6 +680,13 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
        generation = btrfs_free_space_generation(leaf, header);
        btrfs_release_path(path);
 
+       if (!BTRFS_I(inode)->generation) {
+               btrfs_info(root->fs_info,
+                          "The free space cache file (%llu) is invalid. skip it\n",
+                          offset);
+               return 0;
+       }
+
        if (BTRFS_I(inode)->generation != generation) {
                btrfs_err(root->fs_info,
                        "free space inode generation (%llu) "
@@ -1107,6 +1114,20 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
        if (ret)
                return -1;
 
+       if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
+               down_write(&block_group->data_rwsem);
+               spin_lock(&block_group->lock);
+               if (block_group->delalloc_bytes) {
+                       block_group->disk_cache_state = BTRFS_DC_WRITTEN;
+                       spin_unlock(&block_group->lock);
+                       up_write(&block_group->data_rwsem);
+                       BTRFS_I(inode)->generation = 0;
+                       ret = 0;
+                       goto out;
+               }
+               spin_unlock(&block_group->lock);
+       }
+
        /* Lock all pages first so we can lock the extent safely. */
        io_ctl_prepare_pages(&io_ctl, inode, 0);
 
@@ -1145,6 +1166,8 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
        if (ret)
                goto out_nospc;
 
+       if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
+               up_write(&block_group->data_rwsem);
        /*
         * Release the pages and unlock the extent, we will flush
         * them out later
@@ -1173,6 +1196,10 @@ out:
 
 out_nospc:
        cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list);
+
+       if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
+               up_write(&block_group->data_rwsem);
+
        goto out;
 }
 
@@ -1192,6 +1219,12 @@ int btrfs_write_out_cache(struct btrfs_root *root,
                spin_unlock(&block_group->lock);
                return 0;
        }
+
+       if (block_group->delalloc_bytes) {
+               block_group->disk_cache_state = BTRFS_DC_WRITTEN;
+               spin_unlock(&block_group->lock);
+               return 0;
+       }
        spin_unlock(&block_group->lock);
 
        inode = lookup_free_space_inode(root, block_group, path);