btrfs: add better -ENOSPC handling VERSION 2
PATCH 1/1 btrfs: add better -ENOSPC handling VERSION 2
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.
There was a slight problem with the first version of this patch, basically if our writes were fast enough we could error out because we used up all of the space that the space_info knows about, not necessarily all the space on the disk. So what we do in this case is check space_info->full. If its set then we return, otherwise we force a chunk allocation to handle the allocation and then recheck to make sure we are ok.
NOTE: there is also a fix in this patch where we weren't actually returning the right error back to userspace. If we ran out of space we'd end up returning -EEXIST instead of -ENOSPC. The fix is simply to set err to ret in btrfs_file_write if ret is set. I tested this patch much more thoroughly than the original patch and I've not seen problems with it yet. Thanks,
--- fs/btrfs/ctree.h | 33 ++++++++--- fs/btrfs/extent-tree.c | 145 +++++++++++++++++++++++++++++++++++++++++++----- fs/btrfs/file.c | 13 +++- fs/btrfs/inode.c | 60 +++++--------------- fs/btrfs/ioctl.c | 16 ++++- 5 files changed, 190 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 */
@@ -1787,6 +1797,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,
@@ -2030,8 +2047,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 +130 -15
172 lines not on this page · raw message
fs/btrfs/file.c +10 -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,
@@ -1136,6 +1141,8 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf, } out:mutex_unlock (&inode->i_mutex); +if (ret) + err = ret; out_nolock:kfree (pages);
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