btrfs: add better -ENOSPC handling
PATCH 1/1 btrfs: add better -ENOSPC handling
This patch removes the large 85% brick wall that was keeping btrfs from panicing because it ran out of space, and instead puts a nice 95% drywall in its place. Instead of tracking delalloc bytes on a systemwide basis, just do it as per space info. For now that means basically just one space_info has anything set for bytes_delalloc, but in the future when there are different allocation schemes for different files this will be less overkill. We reserve space by using btrfs_delalloc_reserve_space, and then free space by using btrfs_delalloc_free_space. You need to make sure that these are always even, IOW for every reserve you must have a free, so either free if you aren't goign to use the reserved space, or free when the space is actually allocated.
For checkers we have btrfs_check_data_free_space which checks to make sure we have sufficient metadata space and space for bytes allocation. For metadata operations we have btrfs_check_metadata_free_space, which is where the 95% wall comes in. We make sure that we always have at least 5% metadata space free in order to make sure we have enough space to rm things if we run out of data space. So btrfs_check_data_free_space will call btrfs_check_metadata_free_space, but every metadata operation (like mknod/create/link etc) just needs to call btrfs_check_metadata_free_space. If the operation is going to result in free'd space, such as unlink and rmdir, you don't need to do the check.
Tested this with fs_mark, dd, fsx without a problem. Tested against head to make sure there wasn't a performance regression and everything came out even. Thanks,
--- fs/btrfs/ctree.h | 33 +++++++++---- fs/btrfs/extent-tree.c | 125 ++++++++++++++++++++++++++++++++++++++++++------ fs/btrfs/file.c | 11 +++- fs/btrfs/inode.c | 60 +++++------------------ fs/btrfs/ioctl.c | 16 +++++-- 5 files changed, 168 insertions(+), 77 deletions(-)
fs/btrfs/ctree.h +24 -9
@@ -600,13 +600,23 @@ struct btrfs_block_group_item {struct btrfs_space_info {u64 flags; -u64 total_bytes; -u64 bytes_used; -u64 bytes_pinned; -u64 bytes_reserved; -u64 bytes_readonly; -int full; -int force_alloc; + +u64 total_bytes;/* total bytes in the space */ +u64 bytes_used;/* total bytes used on disk */ +u64 bytes_pinned;/* total bytes pinned, will be freed when the +transaction finishes */ +u64 bytes_reserved;/* total bytes the allocator has reserved for +current allocations */ +u64 bytes_readonly;/* total bytes that are read only */ +u64 bytes_delalloc;/* number of bytes reserved for allocation, +this space is not necessarily reserved yet +by the allocator */ + +int full;/* indicates that we cannot allocate any more +chunks for this space */ +int force_alloc;/* set if we need to force a chunk alloc for +this space */ +struct list_head list;/* for block groups in our same type */
@@ -1786,6 +1796,13 @@ int btrfs_add_dead_reloc_root(struct btrfs_root *root);int btrfs_cleanup_reloc_trees (struct btrfs_root *root);int btrfs_reloc_clone_csums (struct inode *inode,u64 file_pos,u64 len);u64 btrfs_reduce_alloc_profile (struct btrfs_root *root,u64 flags); +int btrfs_check_metadata_free_space (struct btrfs_root *root); +int btrfs_check_data_free_space (struct btrfs_root *root,struct inode *inode, +u64 bytes); +int btrfs_delalloc_reserve_space (struct btrfs_root *root,struct inode *inode, +u64 bytes); +void btrfs_delalloc_free_space (struct btrfs_root *root,struct inode *inode, +u64 bytes);/* ctree.c */ int btrfs_previous_item(struct btrfs_root *root, struct btrfs_path *path, u64 min_objectid,
@@ -2029,8 +2046,6 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,unsigned long btrfs_force_ra (struct address_space *mapping,struct file_ra_state *ra,struct file *file,pgoff_t offset,pgoff_t last_index); -int btrfs_check_free_space (struct btrfs_root *root,u64 num_required, -int for_del);int btrfs_page_mkwrite (struct vm_area_struct *vma,struct page *page);int btrfs_readpage (struct file *file,struct page *page);void btrfs_delete_inode (struct inode *inode);
fs/btrfs/extent-tree.c +110 -15
146 lines not on this page · raw message
fs/btrfs/file.c +8 -3
@@ -1091,28 +1091,33 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,WARN_ON (num_pages > nrptrs);memset (pages,0 ,sizeof (struct page *) * nrptrs); - ret =btrfs_check_free_space (root, write_bytes,0 ); + ret =btrfs_delalloc_reserve_space (root, inode, write_bytes);if (ret) goto out; ret =prepare_pages (root, file, pages, num_pages, pos, first_index, last_index, write_bytes); -if (ret) +if (ret) { +btrfs_delalloc_free_space (root, inode, write_bytes); goto out; + } ret =btrfs_copy_from_user (pos, num_pages, write_bytes, pages, buf);if (ret) {btrfs_drop_pages (pages, num_pages); +btrfs_delalloc_free_space (root, inode, write_bytes); goto out; } ret =dirty_and_release_pages (NULL , root, file, pages, num_pages, pos, write_bytes);btrfs_drop_pages (pages, num_pages); -if (ret) +if (ret) { +btrfs_delalloc_free_space (root, inode, write_bytes); goto out; + }if (will_write) { btrfs_fdatawrite_range(inode->i_mapping, pos,
fs/btrfs/inode.c +14 -46
159 lines not on this page · raw message
fs/btrfs/ioctl.c +12 -4
@@ -70,7 +70,7 @@ static noinline int create_subvol(struct btrfs_root *root,u64 index =0 ;unsigned long nr =1 ; - ret =btrfs_check_free_space (root,1 ,0 ); + ret =btrfs_check_metadata_free_space (root);if (ret) goto fail_commit;
@@ -203,7 +203,7 @@ static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,if (!root->ref_cows)return -EINVAL ; - ret =btrfs_check_free_space (root,1 ,0 ); + ret =btrfs_check_metadata_free_space (root);if (ret) goto fail_unlock;
@@ -371,10 +371,11 @@ static int btrfs_defrag_file(struct file *file)unsigned long total_read =0 ;u64 page_start;u64 page_end; +u64 size = inode->i_size;unsigned long i;int ret; - ret =btrfs_check_free_space (root, inode->i_size,0 ); + ret =btrfs_delalloc_reserve_space (root, inode, size);if (ret)return -ENOSPC ;
@@ -388,14 +389,18 @@ static int btrfs_defrag_file(struct file *file) total_read++; again: page =grab_cache_page (inode->i_mapping, i); -if (!page) +if (!page) { +btrfs_delalloc_free_space (root, inode, size); goto out_unlock; + } +if (!PageUptodate (page)) {btrfs_readpage (NULL , page);lock_page (page);if (!PageUptodate (page)) {unlock_page (page);page_cache_release (page); +btrfs_delalloc_free_space (root, inode, size); goto out_unlock; } }
@@ -424,6 +429,9 @@ again:clear_page_dirty_for_io (page);btrfs_set_extent_delalloc (inode, page_start, page_end); +btrfs_delalloc_free_space (root, inode, size - + (page_end - page_start +1 )); + size -= page_end - page_start +1 ;unlock_extent (io_tree, page_start, page_end,GFP_NOFS );set_page_dirty (page);
Signature · 1 line
1.5.4.3