Btrfs: snapshot-aware defragmentation
RFC PATCH 2/2 Btrfs: snapshot-aware defragmentation
Note this patch is not complete. I send it out early to make sure this is the right way to implement this feature.
When we defrag a file, btrfs will read the file data into memory and mark the memory dirty. When writeback kicks off, file data will be re-written to disk. This is how defragmentation works.
To make defrag snapshot-aware:
- when mark the memory dirty, we also add a defrag flag
- when writeback starts, we check the defrag flag
- If the flag is set, we find out all the backrefs for the extent that is to be re-written
- After writeback, we change the backrefs to point to the new extent
This patch makes use of extent backref iteration functions provided by Jan Schmidt.
--- fs/btrfs/extent_io.c | 16 ++- fs/btrfs/extent_io.h | 2 + fs/btrfs/file.c | 4 +- fs/btrfs/free-space-cache.c | 7 +- fs/btrfs/inode.c | 400 ++++++++++++++++++++++++++++++++++++++++++- fs/btrfs/ioctl.c | 13 +- 6 files changed, 419 insertions(+), 23 deletions(-)
fs/btrfs/extent_io.c +13 -3
@@ -928,7 +928,17 @@ int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, {return clear_extent_bit (tree, start, end,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING ,0 ,0 ,NULL , mask); +EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , +0 ,0 ,NULL , mask); +} + +int set_extent_defrag (struct extent_io_tree *tree,u64 start,u64 end, +struct extent_state **cached_state,gfp_t mask) +{ +return set_extent_bit (tree, start, end, +EXTENT_DELALLOC |EXTENT_DIRTY | +EXTENT_UPTODATE |EXTENT_DEFRAG , +0 ,NULL , cached_state, mask); } int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
@@ -2628,7 +2638,7 @@ int extent_invalidatepage(struct extent_io_tree *tree,wait_on_page_writeback (page);clear_extent_bit (tree, start, end,EXTENT_LOCKED |EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING , +EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG ,1 ,1 , &cached_state,GFP_NOFS );return 0 ; }
@@ -2703,7 +2713,7 @@ int try_release_extent_mapping(struct extent_map_tree *map, }if (!test_range_bit (tree, em->start,extent_map_end (em) -1 , -EXTENT_LOCKED |EXTENT_WRITEBACK , +EXTENT_LOCKED ,0 ,NULL )) {remove_extent_mapping (map, em);/* once for the rb tree */
fs/btrfs/extent_io.h +2 -0
@@ -216,6 +216,8 @@ int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask);int set_extent_delalloc (struct extent_io_tree *tree,u64 start,u64 end,struct extent_state **cached_state,gfp_t mask); +int set_extent_defrag (struct extent_io_tree *tree,u64 start,u64 end, +struct extent_state **cached_state,gfp_t mask);int find_first_extent_bit (struct extent_io_tree *tree,u64 start,u64 *start_ret,u64 *end_ret,int bits); struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
fs/btrfs/file.c +2 -2
@@ -1129,8 +1129,8 @@ again:clear_extent_bit (&BTRFS_I (inode)->io_tree, start_pos, last_pos -1 ,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING ,0 ,0 , &cached_state, -GFP_NOFS ); +EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , +0 ,0 , &cached_state,GFP_NOFS );unlock_extent_cached (&BTRFS_I (inode)->io_tree, start_pos, last_pos -1 , &cached_state,GFP_NOFS );
fs/btrfs/free-space-cache.c +4 -3
@@ -801,7 +801,8 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, ret =-1 ;clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , bytes -1 ,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING ,0 ,0 ,NULL ,GFP_NOFS ); +EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , +0 ,0 ,NULL ,GFP_NOFS ); goto out; } leaf = path->nodes[0 ];
@@ -815,8 +816,8 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, ret =-1 ;clear_extent_bit (&BTRFS_I (inode)->io_tree,0 , bytes -1 ,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING ,0 ,0 ,NULL , -GFP_NOFS ); +EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG , +0 ,0 ,NULL ,GFP_NOFS );btrfs_release_path (path); goto out; }
fs/btrfs/inode.c +392 -8
472 lines not on this page · raw message
fs/btrfs/ioctl.c +6 -7
@@ -924,10 +924,10 @@ again:if (ordered)btrfs_put_ordered_extent (ordered); -clear_extent_bit (&BTRFS_I (inode)->io_tree, page_start, - page_end -1 ,EXTENT_DIRTY |EXTENT_DELALLOC | -EXTENT_DO_ACCOUNTING ,0 ,0 , &cached_state, -GFP_NOFS ); +clear_extent_bit (&BTRFS_I (inode)->io_tree, page_start, page_end -1 , +EXTENT_DIRTY |EXTENT_DELALLOC | +EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG ,0 ,0 , + &cached_state,GFP_NOFS );if (i_done != num_pages) {spin_lock (&BTRFS_I (inode)->lock);
@@ -937,9 +937,8 @@ again: (num_pages - i_done) << PAGE_CACHE_SHIFT); } - -btrfs_set_extent_delalloc (inode, page_start, page_end -1 , - &cached_state); +set_extent_defrag (&BTRFS_I (inode)->io_tree, page_start, page_end -1 , + &cached_state,GFP_NOFS ); unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, &cached_state,
Signature · 1 line
1.7.3.1