Btrfs: Subpagesize-blocksize: Allow I/O on blocks whose size is less than page size.
Subsystems (2)
v12 cover Btrfs: Subpagesize-blocksize: Allow I/O on blocks whose size is less than page size.
Btrfs assumes block size to be the same as the machine's page size. This would mean that a Btrfs instance created on a 4k page size machine (e.g. x86) will not be mountable on machines with larger page sizes (e.g. PPC64/AARCH64). This patchset aims to resolve this incompatibility.
This patchset continues with the work posted previously at
https://lwn.net/Articles/646937/.
I have reverted the upstream commit "btrfs: fix lockups from btrfs_clear_path_blocking" (f82c458a2c3ffb94b431fc6ad791a79df1b3713e) since this led to soft-lockups when the patch "Btrfs:
subpagesize-blocksize: Prevent writes to an extent buffer when PG_writeback flag is set" is applied.
The commits for the Btrfs kernel module can be found at
https://github.com/chandanr/linux/tree/btrfs/subpagesize-blocksize.
The commits for Btrfs-progs can be found at
https://github.com/chandanr/btrfs-progs/tree/btrfs/subpagesize-blocksize.
Changes from V11:
1. Addressed the review comments provided by Liu Bo for version V11.
2. Fixed file defragmentation code to work in subpagesize-blocksize scenario.
3. Many "hard to reproduce" bugs were fixed.
fstests run status:
1. On x86_64, all the tests (i.e. both generic and btrfs specific tests) pass
- With 2k and 4k data blocksize combinations.
- With and without nodatacow option.
2. On ppc64, generic tests were run with 4k data block size and 16k metadata block size. generic/300 fails because of an issue with the fio tool. The failure is seen even when running the test for other filesystems (i.e. ext4 and xfs).
The following is a list of known TODO items which will be implemented in future revisions of this patchset:
1. Currently, We read one metadata block at a time. Pages mapping multiple extent buffers are currently marked with PG_Uptodate flag only when all the extent buffers of the page have EXTENT_UPTODATE flag set. Some of the blocks mapped by the page may not even contain a valid metadata block. Hence such pages can never have PG_Uptodate flag set.
2. Fix btrfs_assert_tree_locked() invocation (inside "#if DEBUG") in btree_set_page_dirty().
Chandan Rajendra (14):
Btrfs: subpagesize-blocksize: Fix whole page read.
Btrfs: subpagesize-blocksize: Fix whole page write
Btrfs: subpagesize-blocksize: Make sure delalloc range intersects with
the locked page's range
Btrfs: subpagesize-blocksize: Define extent_buffer_head.
Btrfs: subpagesize-blocksize: Read tree blocks whose size is <
PAGE_SIZE.
Btrfs: subpagesize-blocksize: Write only dirty extent buffers
belonging to a page
Btrfs: subpagesize-blocksize: Allow mounting filesystems where
sectorsize != PAGE_SIZE
Btrfs: subpagesize-blocksize: Deal with partial ordered extent
allocations.
Btrfs: subpagesize-blocksize: Explicitly track I/O status of blocks of
an ordered extent.
Btrfs: subpagesize-blocksize: btrfs_punch_hole: Use EXTENT_UPTODATE
bit to check for the presence of block in page cache
Btrfs: subpagesize-blocksize: Prevent writes to an extent buffer when
PG_writeback flag is set.
Revert "btrfs: fix lockups from btrfs_clear_path_blocking"
Btrfs: subpagesize-blocksize: Fix file defragmentation code
Btrfs: subpagesize-blocksize: extent_clear_unlock_delalloc: Prevent
page from being unlocked more than once
fs/btrfs/backref.c | 2 +- fs/btrfs/btrfs_inode.h | 2 - fs/btrfs/ctree.c | 37 +- fs/btrfs/ctree.h | 6 +- fs/btrfs/disk-io.c | 161 +++-- fs/btrfs/disk-io.h | 3 + fs/btrfs/extent-tree.c | 17 +- fs/btrfs/extent_io.c | 1608 ++++++++++++++++++++++++++++++------------ fs/btrfs/extent_io.h | 102 ++- fs/btrfs/file.c | 20 +- fs/btrfs/inode.c | 494 +++++++++---- fs/btrfs/ioctl.c | 194 +++-- fs/btrfs/locking.c | 24 +- fs/btrfs/locking.h | 2 - fs/btrfs/ordered-data.c | 19 + fs/btrfs/ordered-data.h | 4 + fs/btrfs/relocation.c | 3 + fs/btrfs/root-tree.c | 2 +- fs/btrfs/volumes.c | 2 +- include/trace/events/btrfs.h | 2 +- 20 files changed, 1907 insertions(+), 797 deletions(-)
Signature · 1 line
2.1.0
RFC PATCH v12 1/14 Btrfs: subpagesize-blocksize: Fix whole page read.
For the subpagesize-blocksize scenario, a page can contain multiple blocks. In such cases, this patch handles reading data from files.
To track the status of individual blocks of a page, this patch makes use of a bitmap pointed to by page->private.
--- fs/btrfs/extent_io.c | 307 ++++++++++++++++++++++++++++++++++----------------- fs/btrfs/extent_io.h | 28 ++++- fs/btrfs/inode.c | 13 +-- 3 files changed, 231 insertions(+), 117 deletions(-)
fs/btrfs/extent_io.c +203 -104
@@ -1393,6 +1393,95 @@ int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, cached_state, mask); } +static int modify_page_blks_state (struct page *page, +unsigned long blk_states, +u64 start,u64 end,int set) +{ +struct inode *inode = page->mapping->host; +unsigned long *bitmap; +unsigned long first_state; +unsigned long state; +u64 nr_blks; +u64 blk; + +BUG_ON (!PagePrivate (page)); + + bitmap = ((struct btrfs_page_private *)page->private)->bstate; + + blk =BTRFS_BYTES_TO_BLKS (BTRFS_I (inode)->root->fs_info, + start & (PAGE_CACHE_SIZE -1 )); + nr_blks =BTRFS_BYTES_TO_BLKS (BTRFS_I (inode)->root->fs_info, + (end - start +1 )); + + first_state =find_next_bit (&blk_states,BLK_NR_STATE ,0 ); + +while (nr_blks--) { + state = first_state; + +while (state <BLK_NR_STATE ) { +if (set) +set_bit ((blk *BLK_NR_STATE ) + state, bitmap); +else +clear_bit ((blk *BLK_NR_STATE ) + state, bitmap); + + state =find_next_bit (&blk_states,BLK_NR_STATE , + state +1 ); + } + + ++blk; + } + +return 0 ; +} + +int set_page_blks_state (struct page *page,unsigned long blk_states, +u64 start,u64 end) +{ +return modify_page_blks_state (page, blk_states, start, end,1 ); +} + +int clear_page_blks_state (struct page *page,unsigned long blk_states, +u64 start,u64 end) +{ +return modify_page_blks_state (page, blk_states, start, end,0 ); +} + +int test_page_blks_state (struct page *page,enum blk_state blk_state, +u64 start,u64 end,int check_all) +{ +struct inode *inode = page->mapping->host; +unsigned long *bitmap; +unsigned long blk; +u64 nr_blks; +int found =0 ; + +BUG_ON (!PagePrivate (page)); + + bitmap = ((struct btrfs_page_private *)page->private)->bstate; + + blk =BTRFS_BYTES_TO_BLKS (BTRFS_I (inode)->root->fs_info, + start & (PAGE_CACHE_SIZE -1 )); + nr_blks =BTRFS_BYTES_TO_BLKS (BTRFS_I (inode)->root->fs_info, + (end - start +1 )); + +while (nr_blks--) { +if (test_bit ((blk *BLK_NR_STATE ) + blk_state, bitmap)) { +if (!check_all) +return 1 ; + found =1 ; + }else if (check_all) { +return 0 ; + } + + ++blk; + } + +if (!check_all && !found) +return 0 ; + +return 1 ; +} +/* * either insert or lock state struct between start and end use mask to tell * us if waiting is desired.
@@ -2050,14 +2139,22 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,* helper function to set a given page up to date if all the * extents in the tree for that page are up to date */ -static void check_page_uptodate (struct extent_io_tree *tree,struct page *page) +static void check_page_uptodate (struct page *page) {u64 start =page_offset (page);u64 end = start +PAGE_CACHE_SIZE -1 ; -if (test_range_bit (tree, start, end,EXTENT_UPTODATE ,1 ,NULL )) +if (test_page_blks_state (page,BLK_STATE_UPTODATE , start, end,1 ))SetPageUptodate (page); } +static int page_read_complete (struct page *page) +{ +u64 start =page_offset (page); +u64 end = start +PAGE_CACHE_SIZE -1 ; + +return !test_page_blks_state (page,BLK_STATE_IO , start, end,0 ); +} +int free_io_failure (struct inode *inode,struct io_failure_record *rec) {int ret;
@@ -2379,7 +2476,9 @@ int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,* a) deliver good data to the caller * b) correct the bad sectors on disk */ -if (failed_bio->bi_vcnt >1 ) { +if ((failed_bio->bi_vcnt >1 ) + || (failed_bio->bi_io_vec->bv_len + >BTRFS_I (inode)->root->sectorsize)) {/* * to fulfill b), we need to know the exact failing sectors, as * we don't want to rewrite any more than the failed ones. thus,
@@ -2588,18 +2687,6 @@ static void end_bio_extent_writepage(struct bio *bio)bio_put (bio); } -static void -endio_readpage_release_extent (struct extent_io_tree *tree,u64 start,u64 len, -int uptodate) -{ -struct extent_state *cached =NULL ; -u64 end = start + len -1 ; - -if (uptodate && tree->track_uptodate) -set_extent_uptodate (tree, start, end, &cached,GFP_ATOMIC ); -unlock_extent_cached (tree, start, end, &cached,GFP_ATOMIC ); -} -/* * after a readpage IO is done, we need to: * clear the uptodate bits on error
@@ -2616,67 +2703,49 @@ static void end_bio_extent_readpage(struct bio *bio)struct bio_vec *bvec;int uptodate = !bio->bi_error;struct btrfs_io_bio *io_bio =btrfs_io_bio (bio); +struct extent_state *cached =NULL ; +struct btrfs_page_private *pg_private;struct extent_io_tree *tree; +unsigned long flags;u64 offset =0 ;u64 start;u64 end; -u64 len; -u64 extent_start =0 ; -u64 extent_len =0 ; +int nr_sectors;int mirror; +int unlock;int ret;int i;bio_for_each_segment_all (bvec, bio, i) {struct page *page = bvec->bv_page;struct inode *inode = page->mapping->host; +struct btrfs_root *root =BTRFS_I (inode)->root;pr_debug ("end_bio_extent_readpage: bi_sector=%llu, err=%d, " "mirror=%u\n" , (u64 )bio->bi_iter.bi_sector, bio->bi_error, io_bio->mirror_num); tree = &BTRFS_I (inode)->io_tree; -/* We always issue full-page reads, but if some block -* in a page fails to read, blk_update_request() will -* advance bv_offset and adjust bv_len to compensate. -* Print a warning for nonzero offsets, and an error -* if they don't add up to a full page. */ -if (bvec->bv_offset || bvec->bv_len !=PAGE_CACHE_SIZE ) { -if (bvec->bv_offset + bvec->bv_len !=PAGE_CACHE_SIZE ) -btrfs_err (BTRFS_I (page->mapping->host)->root->fs_info, -"partial page read in btrfs with offset %u and length %u" , - bvec->bv_offset, bvec->bv_len); -else -btrfs_info (BTRFS_I (page->mapping->host)->root->fs_info, -"incomplete page read in btrfs with offset %u and " -"length %u" , - bvec->bv_offset, bvec->bv_len); - } - - start =page_offset (page); - end = start + bvec->bv_offset + bvec->bv_len -1 ; - len = bvec->bv_len; - + start =page_offset (page) + bvec->bv_offset; + end = start + bvec->bv_len -1 ; + nr_sectors =BTRFS_BYTES_TO_BLKS (root->fs_info, + bvec->bv_len); mirror = io_bio->mirror_num; + +next_block:if (likely (uptodate && tree->ops && - tree->ops->readpage_end_io_hook)) { + tree->ops->readpage_end_io_hook)) { ret = tree->ops->readpage_end_io_hook (io_bio, offset, - page, start, end, - mirror); + page, start, + start + root->sectorsize -1 , + mirror);if (ret) uptodate =0 ;else clean_io_failure (inode, start, page,0 ); } -if (likely (uptodate)) - goto readpage_ok; - -if (tree->ops && tree->ops->readpage_io_failed_hook) { - ret = tree->ops->readpage_io_failed_hook (page, mirror); -if (!ret && !bio->bi_error) - uptodate =1 ; - }else { +if (!uptodate) {/* * The generic bio_readpage_error handles errors the * following way: If possible, new read requests are
@@ -2687,58 +2756,61 @@ static void end_bio_extent_readpage(struct bio *bio)* can't handle the error it will return -EIO and we * remain responsible for that page. */ - ret =bio_readpage_error (bio, offset, page, start, end, - mirror); + ret =bio_readpage_error (bio, offset, page, + start, start + root->sectorsize -1 , + mirror);if (ret ==0 ) { uptodate = !bio->bi_error; - offset += len; -continue ; + offset += root->sectorsize; +if (--nr_sectors) { + start += root->sectorsize; + goto next_block; + }else { +continue ; + } } } -readpage_ok: -if (likely (uptodate)) { -loff_t i_size =i_size_read (inode); -pgoff_t end_index = i_size >>PAGE_CACHE_SHIFT ; -unsigned off; - -/* Zero out the end if this page straddles i_size */ - off = i_size & (PAGE_CACHE_SIZE -1 ); -if (page->index == end_index && off) -zero_user_segment (page, off,PAGE_CACHE_SIZE ); -SetPageUptodate (page); + +if (uptodate) { +set_page_blks_state (page,1 <<BLK_STATE_UPTODATE , start, + start + root->sectorsize -1 ); +check_page_uptodate (page); }else {ClearPageUptodate (page);SetPageError (page); } -unlock_page (page); - offset += len; - -if (unlikely (!uptodate)) { -if (extent_len) { -endio_readpage_release_extent (tree, - extent_start, - extent_len,1 ); - extent_start =0 ; - extent_len =0 ; - } -endio_readpage_release_extent (tree, start, - end - start +1 ,0 ); - }else if (!extent_len) { - extent_start = start; - extent_len = end +1 - start; - }else if (extent_start + extent_len == start) { - extent_len += end +1 - start; - }else { -endio_readpage_release_extent (tree, extent_start, - extent_len, uptodate); - extent_start = start; - extent_len = end +1 - start; + + offset += root->sectorsize; + +if (--nr_sectors) { +clear_page_blks_state (page,1 <<BLK_STATE_IO , + start, start + root->sectorsize -1 ); +clear_extent_bit (tree, start, start + root->sectorsize -1 , +EXTENT_LOCKED ,1 ,0 , &cached,GFP_ATOMIC ); + start += root->sectorsize; + goto next_block; } + +WARN_ON (!PagePrivate (page)); + + pg_private = (struct btrfs_page_private *)page->private; + +spin_lock_irqsave (&pg_private->io_lock, flags); + +clear_page_blks_state (page,1 <<BLK_STATE_IO , + start, start + root->sectorsize -1 ); + + unlock =page_read_complete (page); + +spin_unlock_irqrestore (&pg_private->io_lock, flags); + +clear_extent_bit (tree, start, start + root->sectorsize -1 , +EXTENT_LOCKED ,1 ,0 , &cached,GFP_ATOMIC ); + +if (unlock) +unlock_page (page); } -if (extent_len) -endio_readpage_release_extent (tree, extent_start, extent_len, - uptodate);if (io_bio->end_io) io_bio->end_io (io_bio, bio->bi_error);bio_put (bio);
@@ -2928,13 +3000,36 @@ static void attach_extent_buffer_page(struct extent_buffer *eb, } } -void set_page_extent_mapped (struct page *page) +int set_page_extent_mapped (struct page *page) { +struct btrfs_page_private *pg_private; +if (!PagePrivate (page)) { + pg_private =kzalloc (sizeof (*pg_private),GFP_NOFS ); +if (!pg_private) +return -ENOMEM ; + +spin_lock_init (&pg_private->io_lock); +SetPagePrivate (page);page_cache_get (page); -set_page_private (page,EXTENT_PAGE_PRIVATE ); + +set_page_private (page, (unsigned long )pg_private); + } + +return 0 ; +} + +int clear_page_extent_mapped (struct page *page) +{ +if (PagePrivate (page)) { +kfree ((struct btrfs_page_private *)(page->private)); +ClearPagePrivate (page); +set_page_private (page,0 ); +page_cache_release (page); } + +return 0 ; } static struct extent_map *
@@ -2979,6 +3074,7 @@ static int __do_readpage(struct extent_io_tree *tree, u64 *prev_em_start) {struct inode *inode = page->mapping->host; +struct extent_state *cached =NULL ;u64 start =page_offset (page);u64 page_end = start +PAGE_CACHE_SIZE -1 ;u64 end;
@@ -3035,8 +3131,8 @@ static int __do_readpage(struct extent_io_tree *tree,memset (userpage + pg_offset,0 , iosize);flush_dcache_page (page);kunmap_atomic (userpage); -set_extent_uptodate (tree, cur, cur + iosize -1 , - &cached,GFP_NOFS ); +set_page_blks_state (page,1 <<BLK_STATE_UPTODATE , cur, + cur + iosize -1 );if (!parent_locked) unlock_extent_cached(tree, cur, cur + iosize - 1,
@@ -3131,11 +3227,9 @@ static int __do_readpage(struct extent_io_tree *tree,flush_dcache_page (page);kunmap_atomic (userpage); -set_extent_uptodate (tree, cur, cur + iosize -1 , - &cached,GFP_NOFS ); -if (parent_locked) -free_extent_state (cached); -else +set_page_blks_state (page,1 <<BLK_STATE_UPTODATE , cur, + cur + iosize -1 ); +if (!parent_locked)unlock_extent_cached (tree, cur, cur + iosize -1 , &cached,GFP_NOFS );
@@ -3144,9 +3238,9 @@ static int __do_readpage(struct extent_io_tree *tree,continue ; }/* the get_extent function already copied into the page */ -if (test_range_bit (tree, cur, cur_end, -EXTENT_UPTODATE ,1 ,NULL )) { -check_page_uptodate (tree, page); +if (test_page_blks_state (page,BLK_STATE_UPTODATE , cur, + cur_end,1 )) { +check_page_uptodate (page);if (!parent_locked)unlock_extent (tree, cur, cur + iosize -1 ); cur = cur + iosize;
@@ -3166,6 +3260,8 @@ static int __do_readpage(struct extent_io_tree *tree, } pnr -= page->index; +set_page_blks_state (page,1 <<BLK_STATE_IO , cur, + cur + iosize -1 ); ret = submit_extent_page(rw, tree, NULL, page, sector, disk_io_size, pg_offset, bdev, bio, pnr,
@@ -3178,8 +3274,11 @@ static int __do_readpage(struct extent_io_tree *tree, *bio_flags = this_bio_flag; }else {SetPageError (page); +clear_page_blks_state (page,1 <<BLK_STATE_IO , cur, + cur + iosize -1 );if (!parent_locked) -unlock_extent (tree, cur, cur + iosize -1 ); +unlock_extent_cached (tree, cur, cur + iosize -1 , + &cached,GFP_NOFS ); } cur = cur + iosize; pg_offset += iosize;
fs/btrfs/extent_io.h +23 -5
@@ -53,11 +53,22 @@#define PAGE_SET_PRIVATE2 (1 << 4)#define PAGE_SET_ERROR (1 << 5) +enum blk_state { +BLK_STATE_UPTODATE , +BLK_STATE_DIRTY , +BLK_STATE_IO , +BLK_NR_STATE , +}; +/* -* page->private values. Every page that is controlled by the extent -* map has page->private set to one. -*/ -#define EXTENT_PAGE_PRIVATE 1 +The maximum number of blocks per page (i.e. 32) occurs when using 2k +as the block size and having 64k as the page size. +*/ +#define BLK_STATE_NR_LONGS DIV_ROUND_UP(BLK_NR_STATE * 32, BITS_PER_LONG) +struct btrfs_page_private { +spinlock_t io_lock; +unsigned long bstate[BLK_STATE_NR_LONGS ]; +};struct extent_state ;struct btrfs_root ;
@@ -278,7 +289,14 @@ int extent_readpages(struct extent_io_tree *tree,int extent_fiemap (struct inode *inode,struct fiemap_extent_info *fieinfo,__u64 start,__u64 len,get_extent_t *get_extent);int get_state_private (struct extent_io_tree *tree,u64 start,u64 *private); -void set_page_extent_mapped (struct page *page); +int set_page_extent_mapped (struct page *page); +int clear_page_extent_mapped (struct page *page); +int set_page_blks_state (struct page *page,unsigned long blk_states, +u64 start,u64 end); +int clear_page_blks_state (struct page *page,unsigned long blk_states, +u64 start,u64 end); +int test_page_blks_state (struct page *page,enum blk_state blk_state, +u64 start,u64 end,int check_all);struct extent_buffer *alloc_extent_buffer (struct btrfs_fs_info *fs_info,u64 start);
fs/btrfs/inode.c +5 -8
@@ -6768,7 +6768,6 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,struct btrfs_key found_key;struct extent_map *em =NULL ;struct extent_map_tree *em_tree = &BTRFS_I (inode)->extent_tree; -struct extent_io_tree *io_tree = &BTRFS_I (inode)->io_tree;struct btrfs_trans_handle *trans =NULL ;const bool new_inline = !page || create;
@@ -6946,8 +6945,8 @@ next:kunmap (page);btrfs_mark_buffer_dirty (leaf); } -set_extent_uptodate (io_tree, em->start, -extent_map_end (em) -1 ,NULL ,GFP_NOFS ); +set_page_blks_state (page,1 <<BLK_STATE_UPTODATE , em->start, +extent_map_end (em) -1 ); goto insert; } not_found:
@@ -8635,11 +8634,9 @@ static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) tree = &BTRFS_I (page->mapping->host)->io_tree; map = &BTRFS_I (page->mapping->host)->extent_tree; ret =try_release_extent_mapping (map, tree, page, gfp_flags); -if (ret ==1 ) { -ClearPagePrivate (page); -set_page_private (page,0 ); -page_cache_release (page); - } +if (ret ==1 ) +clear_page_extent_mapped (page); +return ret; }
Signature · 1 line
2.1.0
RFC PATCH v12 2/14 Btrfs: subpagesize-blocksize: Fix whole page write
In reply to Chandan RajendraFor the subpagesize-blocksize scenario, a page can contain multiple blocks. In such cases, this patch handles writing data to files.
Also, When setting EXTENT_DELALLOC, we no longer set EXTENT_UPTODATE bit on the extent_io_tree since uptodate status is being tracked by the bitmap pointed to by page->private.
--- fs/btrfs/extent_io.c | 154 ++++++++++++++++++++++++-------------------------- fs/btrfs/file.c | 16 ++++++ fs/btrfs/inode.c | 64 +++++++++++++++++---- fs/btrfs/relocation.c | 3 + 4 files changed, 146 insertions(+), 91 deletions(-)
fs/btrfs/extent_io.c +75 -79
@@ -1351,9 +1351,8 @@ int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,int set_extent_delalloc (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_UPTODATE , -NULL , cached_state, mask); +return set_extent_bit (tree, start, end,EXTENT_DELALLOC , +NULL , cached_state, mask); } int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
@@ -1573,25 +1572,6 @@ int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)return 0 ; } -/* -* helper function to set both pages and extents in the tree writeback -*/ -static int set_range_writeback (struct extent_io_tree *tree,u64 start,u64 end) -{ -unsigned long index = start >>PAGE_CACHE_SHIFT ; -unsigned long end_index = end >>PAGE_CACHE_SHIFT ; -struct page *page; - -while (index <= end_index) { - page =find_get_page (tree->mapping, index); -BUG_ON (!page);/* Pages should be in the extent_io_tree */ -set_page_writeback (page); -page_cache_release (page); - index++; - } -return 0 ; -} -/* find the first state struct with 'bits' set after 'start', and * return it. tree->lock must be held. NULL will returned if * nothing was found after 'start'
@@ -2155,6 +2135,14 @@ static int page_read_complete(struct page *page)return !test_page_blks_state (page,BLK_STATE_IO , start, end,0 ); } +static int page_write_complete (struct page *page) +{ +u64 start =page_offset (page); +u64 end = start +PAGE_CACHE_SIZE -1 ; + +return !test_page_blks_state (page,BLK_STATE_IO , start, end,0 ); +} +int free_io_failure (struct inode *inode,struct io_failure_record *rec) {int ret;
@@ -2650,38 +2638,37 @@ int end_extent_writepage(struct page *page, int err, u64 start, u64 end)*/ static void end_bio_extent_writepage (struct bio *bio) { +struct btrfs_page_private *pg_private;struct bio_vec *bvec; +unsigned long flags;u64 start;u64 end; +int clear_writeback;int i;bio_for_each_segment_all (bvec, bio, i) {struct page *page = bvec->bv_page; -/* We always issue full-page reads, but if some block -* in a page fails to read, blk_update_request() will -* advance bv_offset and adjust bv_len to compensate. -* Print a warning for nonzero offsets, and an error -* if they don't add up to a full page. */ -if (bvec->bv_offset || bvec->bv_len !=PAGE_CACHE_SIZE ) { -if (bvec->bv_offset + bvec->bv_len !=PAGE_CACHE_SIZE ) -btrfs_err (BTRFS_I (page->mapping->host)->root->fs_info, -"partial page write in btrfs with offset %u and length %u" , - bvec->bv_offset, bvec->bv_len); -else -btrfs_info (BTRFS_I (page->mapping->host)->root->fs_info, -"incomplete page write in btrfs with offset %u and " -"length %u" , - bvec->bv_offset, bvec->bv_len); - } + start =page_offset (page) + bvec->bv_offset; + end = start + bvec->bv_len -1 ; - start =page_offset (page); - end = start + bvec->bv_offset + bvec->bv_len -1 ; + pg_private = (struct btrfs_page_private *)page->private; -if (end_extent_writepage (page, bio->bi_error, start, end)) +spin_lock_irqsave (&pg_private->io_lock, flags); + +if (end_extent_writepage (page, bio->bi_error, start, end)) { +spin_unlock_irqrestore (&pg_private->io_lock, flags);continue ; + } -end_page_writeback (page); +clear_page_blks_state (page,1 <<BLK_STATE_IO , start, end); + + clear_writeback =page_write_complete (page); + +spin_unlock_irqrestore (&pg_private->io_lock, flags); + +if (clear_writeback) +end_page_writeback (page); }bio_put (bio);
@@ -3544,14 +3531,14 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode,u64 page_end = start +PAGE_CACHE_SIZE -1 ;u64 end;u64 cur = start; +u64 next;u64 extent_offset;u64 block_start;u64 iosize;sector_t sector; -struct extent_state *cached_state =NULL ;struct extent_map *em;struct block_device *bdev; -size_t pg_offset =0 ; +size_t pg_offset;size_t blocksize;int ret =0 ;int nr =0 ;
@@ -3598,20 +3585,38 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode, page_end, NULL, 1);break ; } - em = epd->get_extent (inode, page, pg_offset, cur, - end - cur +1 ,1 ); + +if (!test_page_blks_state (page,BLK_STATE_DIRTY , cur, + cur + blocksize -1 ,1 )) { + cur += blocksize; +continue ; + } + + pg_offset = cur & (PAGE_CACHE_SIZE -1 ); + + em = epd->get_extent (inode, page, pg_offset, cur, blocksize,1 );if (IS_ERR_OR_NULL (em)) {SetPageError (page); ret =PTR_ERR_OR_ZERO (em);break ; } - extent_offset = cur - em->start; em_end =extent_map_end (em);BUG_ON (em_end <= cur);BUG_ON (end < cur); - iosize =min (em_end - cur, end - cur +1 ); + + iosize = blocksize; + next = cur + blocksize; +while ((next < end) + && (next < em_end) + &&test_page_blks_state (page,BLK_STATE_DIRTY , next, + next + blocksize -1 ,1 )) { + iosize += blocksize; + next += blocksize; + } + iosize =ALIGN (iosize, blocksize); + extent_offset = cur - em->start; sector = (em->block_start + extent_offset) >>9 ; bdev = em->bdev; block_start = em->block_start;
@@ -3619,32 +3624,20 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode,free_extent_map (em); em =NULL ; -/* -* compressed and inline extents are written through other -* paths in the FS -*/ -if (compressed || block_start ==EXTENT_MAP_HOLE || - block_start ==EXTENT_MAP_INLINE ) { -/* -* end_io notification does not happen here for -* compressed extents -*/ -if (!compressed && tree->ops && - tree->ops->writepage_end_io_hook) - tree->ops->writepage_end_io_hook (page, cur, - cur + iosize -1 , -NULL ,1 ); -else if (compressed) { -/* we don't want to end_page_writeback on -* a compressed extent. this happens -* elsewhere -*/ - nr++; - } +BUG_ON (compressed); +BUG_ON (block_start ==EXTENT_MAP_INLINE ); - cur += iosize; - pg_offset += iosize; -continue ; +if (block_start ==EXTENT_MAP_HOLE ) { +if (test_page_blks_state (page,BLK_STATE_UPTODATE , cur, + cur + iosize -1 ,1 )) { +clear_page_blks_state (page, +1 <<BLK_STATE_DIRTY , cur, + cur + iosize -1 ); + cur += iosize; +continue ; + }else { +BUG (); + } }if (tree->ops && tree->ops->writepage_io_hook) {
@@ -3658,7 +3651,13 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode, }else {unsigned long max_nr = (i_size >>PAGE_CACHE_SHIFT ) +1 ; -set_range_writeback (tree, cur, cur + iosize -1 ); +clear_page_blks_state (page,1 <<BLK_STATE_DIRTY , cur, + cur + iosize -1 ); +set_page_writeback (page); + +set_page_blks_state (page,1 <<BLK_STATE_IO , cur, + cur + iosize -1 ); +if (!PageWriteback (page)) { btrfs_err(BTRFS_I(inode)->root->fs_info, "page %lu not writeback, cur %llu end %llu",
@@ -3673,17 +3672,14 @@ static noinline_for_stack int __extent_writepage_io(struct inode *inode,if (ret)SetPageError (page); } - cur = cur + iosize; - pg_offset += iosize; + + cur += iosize; nr++; } done: *nr_ret = nr; done_unlocked: - -/* drop our reference on any cached states */ -free_extent_state (cached_state);return ret; }
fs/btrfs/file.c +16 -0
@@ -495,6 +495,9 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,u64 num_bytes;u64 start_pos;u64 end_of_last_block; +u64 start; +u64 end; +u64 page_end;u64 end_pos = pos + write_bytes;loff_t isize =i_size_read (inode);
@@ -507,11 +510,24 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,if (err)return err; + start = start_pos; +for (i =0 ; i < num_pages; i++) {struct page *p = pages[i];SetPageUptodate (p);ClearPageChecked (p); + + end = page_end =page_offset (p) +PAGE_CACHE_SIZE -1 ; + +if (i == num_pages -1 ) + end =min_t (u64, page_end, end_of_last_block); + +set_page_blks_state (p, +1 <<BLK_STATE_DIRTY |1 <<BLK_STATE_UPTODATE , + start, end);set_page_dirty (p); + + start = page_end +1 ; }/*
fs/btrfs/inode.c +52 -12
108 lines not on this page · raw message
fs/btrfs/relocation.c +3 -0
@@ -3185,6 +3185,9 @@ static int relocate_file_extent_cluster(struct inode *inode, }btrfs_set_extent_delalloc (inode, page_start, page_end,NULL ); +set_page_blks_state (page, +1 <<BLK_STATE_DIRTY |1 <<BLK_STATE_UPTODATE , + page_start, page_end);set_page_dirty (page); unlock_extent(&BTRFS_I(inode)->io_tree,
Signature · 1 line
2.1.0
RFC PATCH v12 3/14 Btrfs: subpagesize-blocksize: Make sure delalloc range intersects with the locked page's range
In reply to Chandan Rajendrafind_delalloc_range indirectly depends on EXTENT_UPTODDATE to make sure that the delalloc range returned intersects with the file range mapped by the page. Since we now track "uptodate" state in a per-page
bitmap (i.e. in btrfs_page_private->bstate), this commit makes an explicit check to make sure that the delalloc range starts from within the file range mapped by the page.
--- fs/btrfs/extent_io.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
fs/btrfs/extent_io.c +9 -3
@@ -1659,6 +1659,7 @@ out:* 1 is returned if we find something, 0 if nothing was in the tree */ static noinline u64 find_delalloc_range(struct extent_io_tree *tree, +struct page *locked_page,u64 *start,u64 *end,u64 max_bytes,struct extent_state **cached_state) {
@@ -1667,6 +1668,9 @@ static noinline u64 find_delalloc_range(struct extent_io_tree *tree,u64 cur_start = *start;u64 found =0 ;u64 total_bytes =0 ; +u64 page_end; + + page_end =page_offset (locked_page) +PAGE_CACHE_SIZE -1 ;spin_lock (&tree->lock);
@@ -1687,7 +1691,8 @@ static noinline u64 find_delalloc_range(struct extent_io_tree *tree, (state->state & EXTENT_BOUNDARY))) { goto out; } -if (!(state->state &EXTENT_DELALLOC )) { +if (!(state->state &EXTENT_DELALLOC ) + || (page_end < state->start)) {if (!found) *end = state->end; goto out;
@@ -1825,8 +1830,9 @@ again:/* step one, find a bunch of delalloc bytes starting at start */ delalloc_start = *start; delalloc_end =0 ; - found =find_delalloc_range (tree, &delalloc_start, &delalloc_end, - max_bytes, &cached_state); + found =find_delalloc_range (tree, locked_page, + &delalloc_start, &delalloc_end, + max_bytes, &cached_state);if (!found || delalloc_end <= *start) { *start = delalloc_start; *end = delalloc_end;
Signature · 1 line
2.1.0
RFC PATCH v12 4/14 Btrfs: subpagesize-blocksize: Define extent_buffer_head.
In reply to Chandan RajendraIn order to handle multiple extent buffers per page, first we need to create a way to handle all the extent buffers that are attached to a page.
This patch creates a new data structure 'struct extent_buffer_head', and moves fields that are common to all extent buffers from 'struct extent buffer' to 'struct extent_buffer_head'
Also, this patch moves EXTENT_BUFFER_TREE_REF, EXTENT_BUFFER_DUMMY and EXTENT_BUFFER_IN_TREE flags from extent_buffer->ebflags to extent_buffer_head->bflags.
--- fs/btrfs/backref.c | 2 +- fs/btrfs/ctree.c | 2 +- fs/btrfs/ctree.h | 6 +- fs/btrfs/disk-io.c | 78 +++--- fs/btrfs/extent-tree.c | 6 +- fs/btrfs/extent_io.c | 581 +++++++++++++++++++++++++++++-------------- fs/btrfs/extent_io.h | 63 +++-- fs/btrfs/root-tree.c | 2 +- fs/btrfs/volumes.c | 2 +- include/trace/events/btrfs.h | 2 +- 10 files changed, 491 insertions(+), 253 deletions(-)
fs/btrfs/backref.c +1 -1
@@ -1437,7 +1437,7 @@ char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, eb = path->nodes[0 ];/* make sure we can use eb after releasing the path */ if (eb != eb_in) { -atomic_inc (&eb->refs); +atomic_inc (&eb_head (eb)->refs);btrfs_tree_read_lock (eb);btrfs_set_lock_blocking_rw (eb,BTRFS_READ_LOCK ); }
fs/btrfs/ctree.c +1 -1
@@ -159,7 +159,7 @@ struct extent_buffer *btrfs_root_node(struct btrfs_root *root)* the inc_not_zero dance and if it doesn't work then * synchronize_rcu and try again. */ -if (atomic_inc_not_zero (&eb->refs)) { +if (atomic_inc_not_zero (&eb_head (eb)->refs)) {rcu_read_unlock ();break ; }
fs/btrfs/ctree.h +4 -2
@@ -2367,14 +2367,16 @@ static inline void btrfs_set_token_##name(struct extent_buffer *eb, \#define BTRFS_SETGET_HEADER_FUNCS (name, type, member, bits) \ static inline u##bits btrfs_##name(struct extent_buffer *eb) \ { \ - type *p = page_address(eb->pages[0]); \ + type *p = page_address(eb_head(eb)->pages[0]) + \ + (eb->start & (PAGE_CACHE_SIZE -1)); \ u##bits res = le##bits##_to_cpu(p->member); \ return res; \ } \ static inline void btrfs_set_##name(struct extent_buffer *eb, \ u##bits val) \ { \ - type *p = page_address(eb->pages[0]); \ + type *p = page_address(eb_head(eb)->pages[0]) + \ + (eb->start & (PAGE_CACHE_SIZE -1)); \ p->member = cpu_to_le##bits(val); \ }
fs/btrfs/disk-io.c +46 -32
213 lines not on this page · raw message
fs/btrfs/extent-tree.c +3 -3
@@ -6780,7 +6780,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, goto out; } -WARN_ON (test_bit (EXTENT_BUFFER_DIRTY , &buf->bflags)); +WARN_ON (test_bit (EXTENT_BUFFER_DIRTY , &buf->ebflags));btrfs_add_free_space (cache, buf->start, buf->len);btrfs_update_reserved_bytes (cache, buf->len,RESERVE_FREE ,0 );
@@ -6798,7 +6798,7 @@ out:* Deleting the buffer, clear the corrupt flag since it doesn't matter * anymore. */ -clear_bit (EXTENT_BUFFER_CORRUPT , &buf->bflags); +clear_bit (EXTENT_BUFFER_CORRUPT , &buf->ebflags); }/* Can return -ENOMEM */
@@ -7828,7 +7828,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,btrfs_set_buffer_lockdep_class (root->root_key.objectid, buf, level);btrfs_tree_lock (buf);clean_tree_block (trans, root->fs_info, buf); -clear_bit (EXTENT_BUFFER_STALE , &buf->bflags); +clear_bit (EXTENT_BUFFER_STALE , &buf->ebflags);btrfs_set_lock_blocking (buf);btrfs_set_buffer_uptodate (buf);
fs/btrfs/extent_io.c +389 -192
1,130 lines not on this page · raw message
fs/btrfs/extent_io.h +44 -19
105 lines not on this page · raw message
fs/btrfs/root-tree.c +1 -1
@@ -45,7 +45,7 @@ static void btrfs_read_root_item(struct extent_buffer *eb, int slot,if (!need_reset &&btrfs_root_generation (item) !=btrfs_root_generation_v2 (item)) {if (btrfs_root_generation_v2 (item) !=0 ) { -btrfs_warn (eb->fs_info, +btrfs_warn (eb_head (eb)->fs_info,"mismatching " "generation and generation_v2 " "found in root item. This root "
fs/btrfs/volumes.c +1 -1
@@ -6461,7 +6461,7 @@ int btrfs_read_sys_array(struct btrfs_root *root)* to silence the warning eg. on PowerPC 64. */ if (PAGE_CACHE_SIZE >BTRFS_SUPER_INFO_SIZE ) -SetPageUptodate (sb->pages[0 ]); +SetPageUptodate (eb_head (sb)->pages[0 ]);write_extent_buffer (sb, super_copy,0 ,BTRFS_SUPER_INFO_SIZE ); array_size =btrfs_super_sys_array_size (super_copy);
include/trace/events/btrfs.h +1 -1
@@ -699,7 +699,7 @@ TRACE_EVENT(btrfs_cow_block,TP_fast_assign ( __entry->root_objectid = root->root_key.objectid; __entry->buf_start = buf->start; - __entry->refs =atomic_read (&buf->refs); + __entry->refs =atomic_read (&eb_head (buf)->refs); __entry->cow_start = cow->start; __entry->buf_level =btrfs_header_level (buf); __entry->cow_level =btrfs_header_level (cow);
Signature · 1 line
2.1.0
RFC PATCH v12 5/14 Btrfs: subpagesize-blocksize: Read tree blocks whose size is < PAGE_SIZE.
In reply to Chandan RajendraIn the case of subpagesize-blocksize, this patch makes it possible to read only a single metadata block from the disk instead of all the metadata blocks that map into a page.
--- fs/btrfs/disk-io.c | 62 +++++++++++++++++---------- fs/btrfs/disk-io.h | 3 ++ fs/btrfs/extent_io.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 153 insertions(+), 30 deletions(-)
fs/btrfs/disk-io.c +40 -22
@@ -595,28 +595,41 @@ static noinline int check_leaf(struct btrfs_root *root,return 0 ; } -static int btree_readpage_end_io_hook (struct btrfs_io_bio *io_bio, -u64 phy_offset,struct page *page, -u64 start,u64 end,int mirror) +int verify_extent_buffer_read (struct btrfs_io_bio *io_bio, +struct page *page, +u64 start,u64 end,int mirror) { -u64 found_start; -int found_level; +struct address_space *mapping = (io_bio->bio).bi_io_vec->bv_page->mapping; +struct extent_io_tree *tree = &BTRFS_I (mapping->host)->io_tree; +struct extent_buffer_head *ebh;struct extent_buffer *eb;struct btrfs_root *root =BTRFS_I (page->mapping->host)->root; -int ret =0 ; +unsigned long num_pages; +unsigned long i; +u64 found_start; +int found_level;int reads_done; +int ret =0 ;if (!page->private) goto out; eb = (struct extent_buffer *)page->private; +do { +if ((eb->start <= start) && (eb->start + eb->len -1 > start)) +break ; + }while ((eb = eb->eb_next) !=NULL ); + +ASSERT (eb); + + ebh =eb_head (eb);/* the pending IO might have been the only thing that kept this buffer * in memory. Make sure we have a ref for all this other checks */ extent_buffer_get (eb); - reads_done =atomic_dec_and_test (&eb_head (eb)->io_bvecs); + reads_done =atomic_dec_and_test (&ebh->io_bvecs);if (!reads_done) goto err;
@@ -682,24 +695,31 @@ err:atomic_inc (&eb_head (eb)->io_bvecs);clear_extent_buffer_uptodate (eb); } + +/* +We never read more than one extent buffer from a page at a time. +So unlocking the page here should be fine. +*/ +if (reads_done) { + num_pages =num_extent_pages (eb->start, eb->len); +for (i =0 ; i < num_pages; i++) { + page =eb_head (eb)->pages[i]; +unlock_page (page); + } + +/* +We don't need to add a check to see if +extent_io_tree->track_uptodate is set or not, Since +this function only deals with extent buffers. +*/ +unlock_extent (tree, eb->start, eb->start + eb->len -1 ); + } +free_extent_buffer (eb); out:return ret; } -static int btree_io_failed_hook (struct page *page,int failed_mirror) -{ -struct extent_buffer *eb; -struct btrfs_root *root =BTRFS_I (page->mapping->host)->root; - - eb = (struct extent_buffer *)page->private; -set_bit (EXTENT_BUFFER_READ_ERR , &eb->ebflags); - eb->read_mirror = failed_mirror; -atomic_dec (&eb_head (eb)->io_bvecs); -if (test_and_clear_bit (EXTENT_BUFFER_READAHEAD , &eb->ebflags)) -btree_readahead_hook (root, eb, eb->start, -EIO ); -return -EIO ;/* we fixed nothing */ -}static void end_workqueue_bio (struct bio *bio) {
@@ -4424,8 +4444,6 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root) }static const struct extent_io_ops btree_extent_io_ops = { - .readpage_end_io_hook = btree_readpage_end_io_hook, - .readpage_io_failed_hook = btree_io_failed_hook, .submit_bio_hook = btree_submit_bio_hook,/* note we're sharing with inode.c for the merge bio hook */ .merge_bio_hook = btrfs_merge_bio_hook,
fs/btrfs/disk-io.h +3 -0
@@ -113,6 +113,9 @@ static inline void btrfs_put_fs_root(struct btrfs_root *root)kfree (root); } +int verify_extent_buffer_read (struct btrfs_io_bio *io_bio, +struct page *page, +u64 start,u64 end,int mirror);void btrfs_mark_buffer_dirty (struct extent_buffer *buf);int btrfs_buffer_uptodate (struct extent_buffer *buf,u64 parent_transid,int atomic);
fs/btrfs/extent_io.c +110 -8
165 lines not on this page · raw message
Signature · 1 line
2.1.0
RFC PATCH v12 6/14 Btrfs: subpagesize-blocksize: Write only dirty extent buffers belonging to a page
In reply to Chandan RajendraFor the subpagesize-blocksize scenario, this patch adds the ability to write a single extent buffer to the disk.
--- fs/btrfs/disk-io.c | 19 ++-- fs/btrfs/extent_io.c | 277 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 238 insertions(+), 58 deletions(-)
fs/btrfs/disk-io.c +13 -6
@@ -497,17 +497,24 @@ static int btree_read_extent_buffer_pages(struct btrfs_root *root,static int csum_dirty_buffer (struct btrfs_fs_info *fs_info,struct page *page) { -u64 start =page_offset (page); -u64 found_start;struct extent_buffer *eb; +u64 found_start; eb = (struct extent_buffer *)page->private;if (page !=eb_head (eb)->pages[0 ])return 0 ; - found_start =btrfs_header_bytenr (eb); -if (WARN_ON (found_start != start || !PageUptodate (page))) -return 0 ; -csum_tree_block (fs_info, eb,0 ); + +do { +if (!test_bit (EXTENT_BUFFER_WRITEBACK , &eb->ebflags)) +continue ; +if (WARN_ON (!test_bit (EXTENT_BUFFER_UPTODATE , &eb->ebflags))) +continue ; + found_start =btrfs_header_bytenr (eb); +if (WARN_ON (found_start != eb->start)) +return 0 ; +csum_tree_block (fs_info, eb,0 ); + }while ((eb = eb->eb_next) !=NULL ); +return 0 ; }
fs/btrfs/extent_io.c +225 -52
394 lines not on this page · raw message
Signature · 1 line
2.1.0
RFC PATCH v12 7/14 Btrfs: subpagesize-blocksize: Allow mounting filesystems where sectorsize != PAGE_SIZE
In reply to Chandan RajendraThis patch allows mounting filesystems with blocksize smaller than the PAGE_SIZE.
--- fs/btrfs/disk-io.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
fs/btrfs/disk-io.c +2 -8
@@ -2863,12 +2863,6 @@ int open_ctree(struct super_block *sb, goto fail_sb_buffer; } -if (sectorsize !=PAGE_SIZE ) { -printk (KERN_ERR "BTRFS: incompatible sector size (%lu) " -"found on %s\n" , (unsigned long )sectorsize, sb->s_id); - goto fail_sb_buffer; - } -mutex_lock (&fs_info->chunk_mutex); ret =btrfs_read_sys_array (tree_root);mutex_unlock (&fs_info->chunk_mutex);
@@ -4064,8 +4058,8 @@ static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, btrfs_super_nodesize(sb)); ret = -EINVAL ; } -if (btrfs_super_sectorsize (sb) <4096 ) { -printk (KERN_ERR "BTRFS: sectorsize too small: %u < 4096\n" , +if (btrfs_super_sectorsize (sb) <2048 ) { +printk (KERN_ERR "BTRFS: sectorsize too small: %u < 2048\n" ,btrfs_super_sectorsize (sb)); ret = -EINVAL ; }
Signature · 1 line
2.1.0
RFC PATCH v12 8/14 Btrfs: subpagesize-blocksize: Deal with partial ordered extent allocations.
In reply to Chandan RajendraIn subpagesize-blocksize scenario, extent allocations for only some of the dirty blocks of a page can succeed, while allocation for rest of the blocks can fail. This patch allows I/O against such pages to be submitted.
--- fs/btrfs/extent_io.c | 27 ++++++++++++++------------- fs/btrfs/inode.c | 39 ++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 26 deletions(-)
fs/btrfs/extent_io.c +14 -13
@@ -1942,17 +1942,23 @@ int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,if (page_ops &PAGE_SET_PRIVATE2 )SetPagePrivate2 (pages[i]); +if (page_ops &PAGE_SET_ERROR ) +SetPageError (pages[i]); +if (pages[i] == locked_page) {page_cache_release (pages[i]);continue ; } -if (page_ops &PAGE_CLEAR_DIRTY ) + +if ((page_ops &PAGE_CLEAR_DIRTY ) + && !PagePrivate2 (pages[i]))clear_page_dirty_for_io (pages[i]); -if (page_ops &PAGE_SET_WRITEBACK ) +if ((page_ops &PAGE_SET_WRITEBACK ) + && !PagePrivate2 (pages[i]))set_page_writeback (pages[i]); -if (page_ops &PAGE_SET_ERROR ) -SetPageError (pages[i]); -if (page_ops &PAGE_END_WRITEBACK ) + +if ((page_ops &PAGE_END_WRITEBACK ) + && !PagePrivate2 (pages[i]))end_page_writeback (pages[i]);if (page_ops &PAGE_UNLOCK )unlock_page (pages[i]);
@@ -2642,7 +2648,7 @@ int end_extent_writepage(struct page *page, int err, u64 start, u64 end) uptodate =0 ; } -if (!uptodate) { +if (!uptodate ||PageError (page)) {ClearPageUptodate (page);SetPageError (page); ret = ret <0 ? ret : -EIO ;
@@ -3487,7 +3493,6 @@ static noinline_for_stack int writepage_delalloc(struct inode *inode, nr_written);/* File system has been set read-only */ if (ret) { -SetPageError (page);/* fill_delalloc should be return < 0 for error * but just in case, we use > 0 here meaning the * IO is started, so we don't want to return > 0
@@ -3719,7 +3724,6 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc,struct inode *inode = page->mapping->host;struct extent_page_data *epd = data;u64 start =page_offset (page); -u64 page_end = start +PAGE_CACHE_SIZE -1 ;int ret;int nr =0 ;size_t pg_offset =0 ;
@@ -3764,7 +3768,7 @@ static int __extent_writepage(struct page *page, struct writeback_control *wbc, ret =writepage_delalloc (inode, page, wbc, epd, start, &nr_written);if (ret ==1 ) goto done_unlocked; -if (ret) +if (ret && !PagePrivate2 (page)) goto done; ret = __extent_writepage_io(inode, page, wbc, epd,
@@ -3778,10 +3782,7 @@ done:set_page_writeback (page);end_page_writeback (page); } -if (PageError (page)) { - ret = ret <0 ? ret : -EIO ; -end_extent_writepage (page, ret, start, page_end); - } +unlock_page (page);return ret;
fs/btrfs/inode.c +26 -13
@@ -934,6 +934,8 @@ static noinline int cow_file_range(struct inode *inode,struct btrfs_key ins;struct extent_map *em;struct extent_map_tree *em_tree = &BTRFS_I (inode)->extent_tree; +struct btrfs_ordered_extent *ordered; +unsigned long page_ops, extent_ops;int ret =0 ;if (btrfs_is_free_space_inode (inode)) {
@@ -978,8 +980,6 @@ static noinline int cow_file_range(struct inode *inode,btrfs_drop_extent_cache (inode, start, start + num_bytes -1 ,0 );while (disk_num_bytes >0 ) { -unsigned long op; - cur_alloc_size = disk_num_bytes; ret = btrfs_reserve_extent(root, cur_alloc_size, root->sectorsize, 0, alloc_hint,
@@ -1032,7 +1032,7 @@ static noinline int cow_file_range(struct inode *inode, ret =btrfs_reloc_clone_csums (inode, start, cur_alloc_size);if (ret) - goto out_drop_extent_cache; + goto out_remove_ordered_extent; }if (disk_num_bytes < cur_alloc_size)
@@ -1045,13 +1045,12 @@ static noinline int cow_file_range(struct inode *inode,* Do set the Private2 bit so we know this page was properly * setup for writepage */ - op = unlock ?PAGE_UNLOCK :0 ; - op |=PAGE_SET_PRIVATE2 ; - + page_ops = unlock ?PAGE_UNLOCK :0 ; + page_ops |=PAGE_SET_PRIVATE2 ; + extent_ops =EXTENT_LOCKED |EXTENT_DELALLOC ;extent_clear_unlock_delalloc (inode, start, - start + ram_size -1 , locked_page, -EXTENT_LOCKED |EXTENT_DELALLOC , - op); + start + ram_size -1 , locked_page, + extent_ops, page_ops); disk_num_bytes -= cur_alloc_size; num_bytes -= cur_alloc_size; alloc_hint = ins.objectid + ins.offset;
@@ -1060,16 +1059,30 @@ static noinline int cow_file_range(struct inode *inode, out:return ret; +out_remove_ordered_extent: + ordered =btrfs_lookup_ordered_extent (inode, start); +BUG_ON (!ordered); +btrfs_remove_ordered_extent (inode, ordered); +/* once for us */ +btrfs_put_ordered_extent (ordered); +/* once for the tree */ +btrfs_put_ordered_extent (ordered); + out_drop_extent_cache:btrfs_drop_extent_cache (inode, start, start + ram_size -1 ,0 ); + out_reserve:btrfs_free_reserved_extent (root, ins.objectid, ins.offset,1 ); + out_unlock: + page_ops = unlock ?PAGE_UNLOCK :0 ; + page_ops |=PAGE_CLEAR_DIRTY |PAGE_SET_WRITEBACK |PAGE_END_WRITEBACK + |PAGE_SET_ERROR ; + extent_ops =EXTENT_LOCKED |EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING + |EXTENT_DEFRAG ; +extent_clear_unlock_delalloc (inode, start, end, locked_page, -EXTENT_LOCKED |EXTENT_DO_ACCOUNTING | -EXTENT_DELALLOC |EXTENT_DEFRAG , -PAGE_UNLOCK |PAGE_CLEAR_DIRTY | -PAGE_SET_WRITEBACK |PAGE_END_WRITEBACK ); + extent_ops, page_ops); goto out; }
Signature · 1 line
2.1.0
RFC PATCH v12 9/14 Btrfs: subpagesize-blocksize: Explicitly track I/O status of blocks of an ordered extent.
In reply to Chandan RajendraIn subpagesize-blocksize scenario a page can have more than one block. So in addition to PagePrivate2 flag, we would have to track the I/O status of each block of a page to reliably mark the ordered extent as complete.
--- fs/btrfs/extent_io.c | 19 +-- fs/btrfs/extent_io.h | 5 +- fs/btrfs/inode.c | 365 ++++++++++++++++++++++++++++++++++-------------- fs/btrfs/ordered-data.c | 19 +++ fs/btrfs/ordered-data.h | 4 + 5 files changed, 297 insertions(+), 115 deletions(-)
fs/btrfs/extent_io.c +10 -9
@@ -4705,11 +4705,10 @@ int extent_invalidatepage(struct extent_io_tree *tree,* to drop the page. */ static int try_release_extent_state (struct extent_map_tree *map, -struct extent_io_tree *tree, -struct page *page,gfp_t mask) +struct extent_io_tree *tree, +struct page *page,u64 start,u64 end, +gfp_t mask) { -u64 start =page_offset (page); -u64 end = start +PAGE_CACHE_SIZE -1 ;int ret =1 ; if (test_range_bit(tree, start, end,
@@ -4743,12 +4742,12 @@ static int try_release_extent_state(struct extent_map_tree *map,* map records are removed */ int try_release_extent_mapping (struct extent_map_tree *map, -struct extent_io_tree *tree,struct page *page, -gfp_t mask) +struct extent_io_tree *tree,struct page *page, +u64 start,u64 end,gfp_t mask) {struct extent_map *em; -u64 start =page_offset (page); -u64 end = start +PAGE_CACHE_SIZE -1 ; +u64 orig_start = start; +u64 orig_end = end;if ((mask & __GFP_WAIT) && page->mapping->host->i_size >16 *1024 *1024 ) {
@@ -4782,7 +4781,9 @@ int try_release_extent_mapping(struct extent_map_tree *map,free_extent_map (em); } } -return try_release_extent_state (map, tree, page, mask); +return try_release_extent_state (map, tree, page, + orig_start, orig_end, + mask); }/*
fs/btrfs/extent_io.h +3 -2
@@ -232,8 +232,9 @@ typedef struct extent_map *(get_extent_t)(struct inode *inode,void extent_io_tree_init (struct extent_io_tree *tree,struct address_space *mapping);int try_release_extent_mapping (struct extent_map_tree *map, -struct extent_io_tree *tree,struct page *page, -gfp_t mask); +struct extent_io_tree *tree,struct page *page, +u64 start,u64 end, +gfp_t mask);int try_release_extent_buffer (struct page *page);int lock_extent (struct extent_io_tree *tree,u64 start,u64 end); int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
fs/btrfs/inode.c +261 -104
444 lines not on this page · raw message
fs/btrfs/ordered-data.c +19 -0
@@ -189,12 +189,27 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,struct btrfs_ordered_inode_tree *tree;struct rb_node *node;struct btrfs_ordered_extent *entry; +u64 nr_longs; +u64 nr_blks; tree = &BTRFS_I (inode)->ordered_tree; entry =kmem_cache_zalloc (btrfs_ordered_extent_cache,GFP_NOFS );if (!entry)return -ENOMEM ; + nr_blks =BTRFS_BYTES_TO_BLKS (root->fs_info, len); + nr_longs =BITS_TO_LONGS (nr_blks); +if (nr_longs ==1 ) { + entry->blocks_done = &entry->blocks_bitmap; + }else { + entry->blocks_done =kzalloc (nr_longs *sizeof (unsigned long ), +GFP_NOFS ); +if (!entry->blocks_done) { +kmem_cache_free (btrfs_ordered_extent_cache, entry); +return -ENOMEM ; + } + } + entry->file_offset = file_offset; entry->start = start; entry->len = len;
@@ -576,6 +591,10 @@ void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)list_del (&sum->list);kfree (sum); } + +if (entry->blocks_done != &entry->blocks_bitmap) +kfree (entry->blocks_done); +kmem_cache_free (btrfs_ordered_extent_cache, entry); } }
fs/btrfs/ordered-data.h +4 -0
@@ -139,6 +139,10 @@ struct btrfs_ordered_extent {struct completion completion;struct btrfs_work flush_work;struct list_head work_list; + +/* bitmap to track the blocks that have been written to disk */ +unsigned long *blocks_done; +unsigned long blocks_bitmap; };/*
Signature · 1 line
2.1.0
RFC PATCH v12 10/14 Btrfs: subpagesize-blocksize: btrfs_punch_hole: Use EXTENT_UPTODATE bit to check for the presence of block in page cache
In reply to Chandan RajendraIn subpagesize-blocksize, we have multiple blocks in a page. Checking for existence of a page in the page cache isn't a sufficient check, since we could be truncating a subset of the blocks mapped by the page. So if the blocks that are neighboring the truncated block exist in the page cache, btrfs_page_exists_in_range() would always return 1. Hence check for the existence of EXTENT_UPTODATE bit on the file range mapped by the block being punched out.
--- fs/btrfs/btrfs_inode.h | 2 -- fs/btrfs/file.c | 4 +++- fs/btrfs/inode.c | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-)
fs/btrfs/btrfs_inode.h +0 -2
@@ -323,6 +323,4 @@ static inline void btrfs_inode_resume_unlocked_dio(struct inode *inode) &BTRFS_I(inode)->runtime_flags); } -bool btrfs_page_exists_in_range (struct inode *inode,loff_t start,loff_t end); -#endif
fs/btrfs/file.c +3 -1
@@ -2439,7 +2439,9 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)if ((!ordered || (ordered->file_offset + ordered->len <= lockstart || ordered->file_offset > lockend)) && - !btrfs_page_exists_in_range (inode, lockstart, lockend)) { + !test_range_bit (&BTRFS_I (inode)->io_tree, lockstart, + lockend,EXTENT_UPTODATE ,0 , + cached_state)) {if (ordered)btrfs_put_ordered_extent (ordered);break ;
fs/btrfs/inode.c +2 -1
@@ -7434,7 +7434,8 @@ out:return ret; } -bool btrfs_page_exists_in_range (struct inode *inode,loff_t start,loff_t end) +static bool btrfs_page_exists_in_range (struct inode *inode,loff_t start, +loff_t end) {struct radix_tree_root *root = &inode->i_mapping->page_tree;int found = false;
Signature · 1 line
2.1.0
RFC PATCH v12 11/14 Btrfs: subpagesize-blocksize: Prevent writes to an extent buffer when PG_writeback flag is set.
In reply to Chandan RajendraIn non-subpagesize-blocksize scenario, BTRFS_HEADER_FLAG_WRITTEN flag prevents Btrfs code from writing into an extent buffer whose pages are under writeback. This facility isn't sufficient for achieving the same in subpagesize-blocksize scenario, since we have more than one extent buffer mapped to a page.
Hence this patch adds a new flag (i.e. EXTENT_BUFFER_HEAD_WRITEBACK) and corresponding code to track the writeback status of the page and to prevent writes to any of the extent buffers mapped to the page while writeback is going on.
--- fs/btrfs/ctree.c | 21 ++++++- fs/btrfs/extent-tree.c | 11 ++++ fs/btrfs/extent_io.c | 150 ++++++++++++++++++++++++++++++++++++++++--------- fs/btrfs/extent_io.h | 1 + 4 files changed, 155 insertions(+), 28 deletions(-)
fs/btrfs/ctree.c +20 -1
@@ -1538,6 +1538,7 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, struct extent_buffer *parent, int parent_slot, struct extent_buffer **cow_ret) { + struct extent_buffer_head *ebh = eb_head(buf);u64 search_start;int ret;
@@ -1551,6 +1552,14 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, trans->transid, root->fs_info->generation);if (!should_cow_block (trans, root, buf)) { +if (test_bit (EXTENT_BUFFER_HEAD_WRITEBACK , &ebh->bflags)) { +if (parent) +btrfs_set_lock_blocking (parent); +btrfs_set_lock_blocking (buf); +wait_on_bit_io (&ebh->bflags, +EXTENT_BUFFER_HEAD_WRITEBACK , +TASK_UNINTERRUPTIBLE ); + } *cow_ret = buf;return 0 ; }
@@ -2671,6 +2680,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_key *key, struct btrfs_path *p, int ins_len, int cow) { +struct extent_buffer_head *ebh;struct extent_buffer *b;int slot;int ret;
@@ -2773,8 +2783,17 @@ again:* then we don't want to set the path blocking, * so we test it here */ -if (!should_cow_block (trans, root, b)) +if (!should_cow_block (trans, root, b)) { + ebh =eb_head (b); +if (test_bit (EXTENT_BUFFER_HEAD_WRITEBACK , + &ebh->bflags)) { +btrfs_set_path_blocking (p); +wait_on_bit_io (&ebh->bflags, +EXTENT_BUFFER_HEAD_WRITEBACK , +TASK_UNINTERRUPTIBLE ); + } goto cow_done; + }/* * must have write locks on this node and the
fs/btrfs/extent-tree.c +11 -0
@@ -7819,14 +7819,25 @@ static struct extent_buffer * btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 bytenr, int level) { + struct extent_buffer_head *ebh;struct extent_buffer *buf; buf =btrfs_find_create_tree_block (root, bytenr);if (!buf)return ERR_PTR (-ENOMEM ); + + ebh =eb_head (buf);btrfs_set_header_generation (buf, trans->transid);btrfs_set_buffer_lockdep_class (root->root_key.objectid, buf, level);btrfs_tree_lock (buf); + +if (test_bit (EXTENT_BUFFER_HEAD_WRITEBACK , + &ebh->bflags)) { +btrfs_set_lock_blocking (buf); +wait_on_bit_io (&ebh->bflags,EXTENT_BUFFER_HEAD_WRITEBACK , +TASK_UNINTERRUPTIBLE ); + } +clean_tree_block (trans, root->fs_info, buf);clear_bit (EXTENT_BUFFER_STALE , &buf->ebflags);
fs/btrfs/extent_io.c +123 -27
299 lines not on this page · raw message
fs/btrfs/extent_io.h +1 -0
@@ -36,6 +36,7 @@#define EXTENT_BUFFER_HEAD_TREE_REF 0#define EXTENT_BUFFER_HEAD_DUMMY 1#define EXTENT_BUFFER_HEAD_IN_TREE 2 +#define EXTENT_BUFFER_HEAD_WRITEBACK 3/* these are bit numbers for test/set bit on extent buffer */ #define EXTENT_BUFFER_UPTODATE 0
Signature · 1 line
2.1.0
RFC PATCH v12 12/14 Revert "btrfs: fix lockups from btrfs_clear_path_blocking"
In reply to Chandan RajendraThis reverts commit f82c458a2c3ffb94b431fc6ad791a79df1b3713e.
--- fs/btrfs/ctree.c | 14 ++++++++++++-- fs/btrfs/locking.c | 24 +++--------------------- fs/btrfs/locking.h | 2 -- 3 files changed, 15 insertions(+), 25 deletions(-)
fs/btrfs/ctree.c +12 -2
@@ -80,6 +80,13 @@ noinline void btrfs_clear_path_blocking(struct btrfs_path *p, {int i; +#ifdef CONFIG_DEBUG_LOCK_ALLOC +/* lockdep really cares that we take all of these spinlocks +* in the right order. If any of the locks in the path are not +* currently blocking, it is going to complain. So, make really +* really sure by forcing the path to blocking before we clear +* the path blocking. +*/ if (held) {btrfs_set_lock_blocking_rw (held, held_rw);if (held_rw ==BTRFS_WRITE_LOCK )
@@ -88,6 +95,7 @@ noinline void btrfs_clear_path_blocking(struct btrfs_path *p, held_rw = BTRFS_READ_LOCK_BLOCKING; }btrfs_set_path_blocking (p); +#endif for (i =BTRFS_MAX_LEVEL -1 ; i >=0 ; i--) {if (p->nodes[i] && p->locks[i]) {
@@ -99,8 +107,10 @@ noinline void btrfs_clear_path_blocking(struct btrfs_path *p, } } +#ifdef CONFIG_DEBUG_LOCK_ALLOC if (held)btrfs_clear_lock_blocking_rw (held, held_rw); +#endif }/* this also releases the path */
@@ -2904,7 +2914,7 @@ cow_done: } p->locks[level] =BTRFS_WRITE_LOCK ; }else { - err =btrfs_tree_read_lock_atomic (b); + err =btrfs_try_tree_read_lock (b);if (!err) {btrfs_set_path_blocking (p);btrfs_tree_read_lock (b);
@@ -3036,7 +3046,7 @@ again: } level =btrfs_header_level (b); - err =btrfs_tree_read_lock_atomic (b); + err =btrfs_try_tree_read_lock (b);if (!err) {btrfs_set_path_blocking (p);btrfs_tree_read_lock (b);
fs/btrfs/locking.c +3 -21
@@ -134,26 +134,6 @@ again: }/* -* take a spinning read lock. -* returns 1 if we get the read lock and 0 if we don't -* this won't wait for blocking writers -*/ -int btrfs_tree_read_lock_atomic (struct extent_buffer *eb) -{ -if (atomic_read (&eb->blocking_writers)) -return 0 ; - -read_lock (&eb->lock); -if (atomic_read (&eb->blocking_writers)) { -read_unlock (&eb->lock); -return 0 ; - } -atomic_inc (&eb->read_locks); -atomic_inc (&eb->spinning_readers); -return 1 ; -} - -/* * returns 1 if we get the read lock and 0 if we don't * this won't wait for blocking writers */
@@ -184,7 +164,9 @@ int btrfs_try_tree_write_lock(struct extent_buffer *eb) atomic_read(&eb->blocking_readers))return 0 ; -write_lock (&eb->lock); +if (!write_trylock (&eb->lock)) +return 0 ; +if (atomic_read (&eb->blocking_writers) ||atomic_read (&eb->blocking_readers)) {write_unlock (&eb->lock);
fs/btrfs/locking.h +0 -2
@@ -35,8 +35,6 @@ void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw);void btrfs_assert_tree_locked (struct extent_buffer *eb);int btrfs_try_tree_read_lock (struct extent_buffer *eb);int btrfs_try_tree_write_lock (struct extent_buffer *eb); -int btrfs_tree_read_lock_atomic (struct extent_buffer *eb); -static inline void btrfs_tree_unlock_rw (struct extent_buffer *eb,int rw) {
Signature · 1 line
2.1.0
RFC PATCH v12 13/14 Btrfs: subpagesize-blocksize: Fix file defragmentation code
In reply to Chandan RajendraThis commit gets file defragmentation code to work in subpagesize-blocksize scenario. It does this by keeping track of page offsets that mark block boundaries and passing them as arguments to the functions that implement the defragmentation logic.
--- fs/btrfs/ioctl.c | 194 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 132 insertions(+), 62 deletions(-)
fs/btrfs/ioctl.c +132 -62
@@ -882,12 +882,13 @@ out_unlock:static int check_defrag_in_cache (struct inode *inode,u64 offset,u32 thresh) {struct extent_io_tree *io_tree = &BTRFS_I (inode)->io_tree; +struct btrfs_root *root =BTRFS_I (inode)->root;struct extent_map *em =NULL ;struct extent_map_tree *em_tree = &BTRFS_I (inode)->extent_tree;u64 end;read_lock (&em_tree->lock); - em =lookup_extent_mapping (em_tree, offset,PAGE_CACHE_SIZE ); + em =lookup_extent_mapping (em_tree, offset, root->sectorsize);read_unlock (&em_tree->lock);if (em) {
@@ -977,7 +978,7 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start)struct extent_map_tree *em_tree = &BTRFS_I (inode)->extent_tree;struct extent_io_tree *io_tree = &BTRFS_I (inode)->io_tree;struct extent_map *em; -u64 len =PAGE_CACHE_SIZE ; +u64 len =BTRFS_I (inode)->root->sectorsize;/* * hopefully we have this extent in the tree already, try without
@@ -1096,15 +1097,18 @@ out:* before calling this. */ static int cluster_pages_for_defrag (struct inode *inode, -struct page **pages, -unsigned long start_index, -unsigned long num_pages) +struct page **pages, +unsigned long start_index, +size_t pg_offset, +unsigned long num_blks) { -unsigned long file_end;u64 isize =i_size_read (inode); +u64 start_blk; +u64 end_blk;u64 page_start;u64 page_end;u64 page_cnt; +u64 blk_cnt;int ret;int i;int i_done;
@@ -1113,20 +1117,25 @@ static int cluster_pages_for_defrag(struct inode *inode,struct extent_io_tree *tree;gfp_t mask =btrfs_alloc_write_mask (inode->i_mapping); - file_end = (isize -1 ) >>PAGE_CACHE_SHIFT ; -if (!isize || start_index > file_end) + start_blk = (start_index <<PAGE_CACHE_SHIFT ) + pg_offset; + start_blk >>= inode->i_blkbits; + end_blk = (isize -1 ) >> inode->i_blkbits; +if (!isize || start_blk > end_blk)return 0 ; - page_cnt =min_t (u64, (u64 )num_pages, (u64 )file_end - start_index +1 ); + blk_cnt =min_t (u64, (u64 )num_blks, (u64 )end_blk - start_blk +1 ); ret =btrfs_delalloc_reserve_space (inode, - start_index <<PAGE_CACHE_SHIFT , - page_cnt <<PAGE_CACHE_SHIFT ); + start_blk << inode->i_blkbits, + blk_cnt << inode->i_blkbits);if (ret)return ret; i_done =0 ; tree = &BTRFS_I (inode)->io_tree; + page_cnt =DIV_ROUND_UP (pg_offset + (blk_cnt << inode->i_blkbits), +PAGE_CACHE_SIZE ); +/* step one, lock all the pages */ for (i =0 ; i < page_cnt; i++) {struct page *page;
@@ -1137,12 +1146,21 @@ again:break ; page_start =page_offset (page); - page_end = page_start +PAGE_CACHE_SIZE -1 ; +if (i ==0 ) + page_start += pg_offset; + +if (i == page_cnt -1 ) { + page_end = (start_index <<PAGE_CACHE_SHIFT ) + pg_offset; + page_end += (blk_cnt << inode->i_blkbits) -1 ; + }else { + page_end =page_offset (page) +PAGE_CACHE_SIZE -1 ; + } +while (1 ) {lock_extent_bits (tree, page_start, page_end,0 , &cached_state); - ordered =btrfs_lookup_ordered_extent (inode, - page_start); + ordered =btrfs_lookup_ordered_range (inode, page_start, + page_end - page_start +1 );unlock_extent_cached (tree, page_start, page_end, &cached_state,GFP_NOFS );if (!ordered)
@@ -1181,7 +1199,7 @@ again: } pages[i] = page; - i_done++; + i_done += (page_end - page_start +1 ) >> inode->i_blkbits; }if (!i_done || ret) goto out;
@@ -1193,55 +1211,76 @@ again:* so now we have a nice long stream of locked * and up to date pages, lets wait on them */ -for (i =0 ; i < i_done; i++) + page_cnt =DIV_ROUND_UP (pg_offset + (i_done << inode->i_blkbits), +PAGE_CACHE_SIZE ); +for (i =0 ; i < page_cnt; i++)wait_on_page_writeback (pages[i]); - page_start =page_offset (pages[0 ]); - page_end =page_offset (pages[i_done -1 ]) +PAGE_CACHE_SIZE ; + page_start =page_offset (pages[0 ]) + pg_offset; + page_end = page_start + (i_done << inode->i_blkbits) -1 ;lock_extent_bits (&BTRFS_I (inode)->io_tree, - page_start, page_end -1 ,0 , &cached_state); + page_start, page_end,0 , &cached_state);clear_extent_bit (&BTRFS_I (inode)->io_tree, page_start, - page_end -1 ,EXTENT_DIRTY |EXTENT_DELALLOC | + page_end,EXTENT_DIRTY |EXTENT_DELALLOC |EXTENT_DO_ACCOUNTING |EXTENT_DEFRAG ,0 ,0 , &cached_state,GFP_NOFS ); -if (i_done != page_cnt) { +if (i_done != blk_cnt) {spin_lock (&BTRFS_I (inode)->lock);BTRFS_I (inode)->outstanding_extents++;spin_unlock (&BTRFS_I (inode)->lock);btrfs_delalloc_release_space (inode, - start_index <<PAGE_CACHE_SHIFT , - (page_cnt - i_done) <<PAGE_CACHE_SHIFT ); + start_blk << inode->i_blkbits, + (blk_cnt - i_done) << inode->i_blkbits); } -set_extent_defrag (&BTRFS_I (inode)->io_tree, page_start, page_end -1 , +set_extent_defrag (&BTRFS_I (inode)->io_tree, page_start, page_end, &cached_state,GFP_NOFS );unlock_extent_cached (&BTRFS_I (inode)->io_tree, - page_start, page_end -1 , &cached_state, + page_start, page_end, &cached_state,GFP_NOFS ); -for (i =0 ; i < i_done; i++) { +for (i =0 ; i < page_cnt; i++) {clear_page_dirty_for_io (pages[i]);ClearPageChecked (pages[i]);set_page_extent_mapped (pages[i]); + + page_start =page_offset (pages[i]); +if (i ==0 ) + page_start += pg_offset; + +if (i == page_cnt -1 ) { + page_end =page_offset (pages[0 ]) + pg_offset; + page_end += (i_done << inode->i_blkbits) -1 ; + }else { + page_end =page_offset (pages[i]) +PAGE_CACHE_SIZE -1 ; + } + +set_page_blks_state (pages[i], +1 <<BLK_STATE_UPTODATE |1 <<BLK_STATE_DIRTY , + page_start, page_end);set_page_dirty (pages[i]);unlock_page (pages[i]);page_cache_release (pages[i]); }return i_done; out: -for (i =0 ; i < i_done; i++) { -unlock_page (pages[i]); -page_cache_release (pages[i]); +if (i_done) { + page_cnt =DIV_ROUND_UP (pg_offset + (i_done << inode->i_blkbits), +PAGE_CACHE_SIZE ); +for (i =0 ; i < page_cnt; i++) { +unlock_page (pages[i]); +page_cache_release (pages[i]); + } } +btrfs_delalloc_release_space (inode, - start_index <<PAGE_CACHE_SHIFT , - page_cnt <<PAGE_CACHE_SHIFT ); + start_blk << inode->i_blkbits, + blk_cnt << inode->i_blkbits);return ret; - } int btrfs_defrag_file(struct inode *inode, struct file *file,
@@ -1250,19 +1289,24 @@ int btrfs_defrag_file(struct inode *inode, struct file *file, {struct btrfs_root *root =BTRFS_I (inode)->root;struct file_ra_state *ra =NULL ; +unsigned long first_off, last_off; +unsigned long first_block, last_block;unsigned long last_index;u64 isize =i_size_read (inode);u64 last_len =0 ;u64 skip =0 ;u64 defrag_end =0 ;u64 newer_off = range->start; +u64 start; +u64 page_cnt;unsigned long i;unsigned long ra_index =0 ; +size_t pg_offset;int ret;int defrag_count =0 ;int compress_type =BTRFS_COMPRESS_ZLIB ;u32 extent_thresh = range->extent_thresh; -unsigned long max_cluster = (256 *1024 ) >>PAGE_CACHE_SHIFT ; +unsigned long max_cluster = (256 *1024 ) >> inode->i_blkbits;unsigned long cluster = max_cluster;u64 new_align = ~((u64 )128 *1024 -1 );struct page **pages =NULL ;
@@ -1296,8 +1340,14 @@ int btrfs_defrag_file(struct inode *inode, struct file *file, ra = &file->f_ra; } - pages =kmalloc_array (max_cluster,sizeof (struct page *), -GFP_NOFS ); +/* +In subpagesize-blocksize scenario the first of "max_cluster" blocks +may start on a non-zero page offset. In such scenarios we need one +page more than what would be needed in the case where the first block +maps to first block of a page. +*/ + page_cnt = (max_cluster >> (PAGE_CACHE_SHIFT - inode->i_blkbits)) +1 ; + pages =kmalloc_array (page_cnt,sizeof (struct page *),GFP_NOFS );if (!pages) { ret = -ENOMEM ; goto out_ra;
@@ -1305,12 +1355,15 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,/* find the last page to defrag */ if (range->start + range->len > range->start) { - last_index =min_t (u64, isize -1 , - range->start + range->len -1 ) >>PAGE_CACHE_SHIFT ; + last_off =min_t (u64, isize -1 , range->start + range->len -1 ); }else { - last_index = (isize -1 ) >>PAGE_CACHE_SHIFT ; + last_off = isize -1 ; } + last_off =round_up (last_off, root->sectorsize) -1 ; + last_block = last_off >> inode->i_blkbits; + last_index = last_off >>PAGE_CACHE_SHIFT ; +if (newer_than) { ret =find_new_extents (root, inode, newer_than, &newer_off,64 *1024 );
@@ -1320,14 +1373,20 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,* we always align our defrag to help keep * the extents in the file evenly spaced */ - i = (newer_off & new_align) >>PAGE_CACHE_SHIFT ; + first_off = newer_off & new_align; }else goto out_ra; }else { - i = range->start >>PAGE_CACHE_SHIFT ; + first_off = range->start; } + + first_off =round_down (first_off, root->sectorsize); + first_block = first_off >> inode->i_blkbits; + i = first_off >>PAGE_CACHE_SHIFT ; + pg_offset = first_off & (PAGE_CACHE_SIZE -1 ); +if (!max_to_defrag) - max_to_defrag = last_index - i +1 ; + max_to_defrag = last_block - first_block +1 ;/* * make writeback starts from i, so the defrag range can be
@@ -1337,7 +1396,7 @@ int btrfs_defrag_file(struct inode *inode, struct file *file, inode->i_mapping->writeback_index = i;while (i <= last_index && defrag_count < max_to_defrag && - (i <DIV_ROUND_UP (i_size_read (inode),PAGE_CACHE_SIZE ))) { + (i <DIV_ROUND_UP (i_size_read (inode),PAGE_CACHE_SIZE ))) {/* * make sure we stop running if someone unmounts * the FS
@@ -1351,39 +1410,50 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,break ; } -if (!should_defrag_range (inode, (u64 )i <<PAGE_CACHE_SHIFT , - extent_thresh, &last_len, &skip, - &defrag_end, range->flags & -BTRFS_DEFRAG_RANGE_COMPRESS )) { + start = pg_offset + ((u64 )i <<PAGE_CACHE_SHIFT ); +if (!should_defrag_range (inode, start, + extent_thresh, &last_len, &skip, + &defrag_end, range->flags & +BTRFS_DEFRAG_RANGE_COMPRESS )) {unsigned long next;/* * the should_defrag function tells us how much to skip * bump our counter by the suggested amount */ - next =DIV_ROUND_UP (skip,PAGE_CACHE_SIZE ); - i =max (i +1 , next); + next =max (skip, start + root->sectorsize); + next >>= inode->i_blkbits; + + first_off = next << inode->i_blkbits; + i = first_off >>PAGE_CACHE_SHIFT ; + pg_offset = first_off & (PAGE_CACHE_SIZE -1 );continue ; }if (!newer_than) { - cluster = (PAGE_CACHE_ALIGN (defrag_end) >> -PAGE_CACHE_SHIFT ) - i; + cluster = (defrag_end >> inode->i_blkbits) + - (start >> inode->i_blkbits); + cluster =min (cluster, max_cluster); }else { cluster = max_cluster; } -if (i + cluster > ra_index) { + page_cnt = pg_offset + (cluster << inode->i_blkbits) -1 ; + page_cnt =DIV_ROUND_UP (page_cnt,PAGE_CACHE_SIZE ); +if (i + page_cnt > ra_index) { ra_index =max (i, ra_index);btrfs_force_ra (inode->i_mapping, ra, file, ra_index, - cluster); - ra_index += cluster; + page_cnt); + ra_index +=DIV_ROUND_UP (pg_offset + + (cluster << inode->i_blkbits), +PAGE_CACHE_SIZE ); }mutex_lock (&inode->i_mutex);if (range->flags &BTRFS_DEFRAG_RANGE_COMPRESS )BTRFS_I (inode)->force_compress = compress_type; - ret =cluster_pages_for_defrag (inode, pages, i, cluster); + ret =cluster_pages_for_defrag (inode, pages, i, pg_offset, + cluster);if (ret <0 ) {mutex_unlock (&inode->i_mutex); goto out_ra;
@@ -1397,30 +1467,30 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,if (newer_off == (u64)-1 )break ; -if (ret >0 ) - i += ret; - newer_off =max (newer_off +1 , - (u64 )i <<PAGE_CACHE_SHIFT ); + start + (ret << inode->i_blkbits)); ret =find_new_extents (root, inode, newer_than, &newer_off,64 *1024 );if (!ret) { range->start = newer_off; - i = (newer_off & new_align) >>PAGE_CACHE_SHIFT ; + first_off = newer_off & new_align; }else {break ; } }else {if (ret >0 ) { - i += ret; - last_len += ret <<PAGE_CACHE_SHIFT ; + first_off = start + (ret << inode->i_blkbits); + last_len += ret << inode->i_blkbits; }else { - i++; + first_off = start + root->sectorsize; last_len =0 ; } } + first_off =round_down (first_off, root->sectorsize); + i = first_off >>PAGE_CACHE_SHIFT ; + pg_offset = first_off & (PAGE_CACHE_SIZE -1 ); }if ((range->flags &BTRFS_DEFRAG_RANGE_START_IO )) {
Signature · 1 line
2.1.0
RFC PATCH v12 14/14 Btrfs: subpagesize-blocksize: extent_clear_unlock_delalloc: Prevent page from being unlocked more than once
In reply to Chandan Rajendraextent_clear_unlock_delalloc() can unlock a page more than once as shown below (assume 4k as the block size and 64k as the page size).
cow_file_range
create 4k ordered extent corresponding to page offsets 0 - 4095 extent_clear_unlock_delalloc corresponding to page offsets 0 - 4095
unlock page
create 4k ordered extent corresponding to page offsets 4096 - 8191 extent_clear_unlock_delalloc corresponding to page offsets 4096 - 8191
unlock page
To prevent such a scenario this commit passes "delalloc end" to extent_clear_unlock_delalloc() to help decide whether the page can be unlocked or not.
NOTE: Since extent_clear_unlock_delalloc() is used by compression code as well, the commit passes ordered extent "end" as the value for the argument corresponding to "delalloc end" for invocations made from compression code path. This will be fixed by a future commit that gets compression to work in subpagesize-blocksize scenario.
--- fs/btrfs/extent_io.c | 15 ++++++---- fs/btrfs/extent_io.h | 5 ++-- fs/btrfs/inode.c | 84 ++++++++++++++++++++++++++++++---------------------- 3 files changed, 60 insertions(+), 44 deletions(-)
fs/btrfs/extent_io.c +10 -5
@@ -1914,9 +1914,8 @@ out_failed: }int extent_clear_unlock_delalloc (struct inode *inode,u64 start,u64 end, -struct page *locked_page, -unsigned clear_bits, -unsigned long page_ops) +u64 delalloc_end,struct page *locked_page, +unsigned clear_bits,unsigned long page_ops) {struct extent_io_tree *tree = &BTRFS_I (inode)->io_tree;int ret;
@@ -1924,6 +1923,7 @@ int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,unsigned long index = start >>PAGE_CACHE_SHIFT ;unsigned long end_index = end >>PAGE_CACHE_SHIFT ;unsigned long nr_pages = end_index - index +1 ; +u64 page_end;int i;clear_extent_bit (tree, start, end, clear_bits,1 ,0 ,NULL ,GFP_NOFS );
@@ -1960,8 +1960,13 @@ int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,if ((page_ops &PAGE_END_WRITEBACK ) && !PagePrivate2 (pages[i]))end_page_writeback (pages[i]); -if (page_ops &PAGE_UNLOCK ) -unlock_page (pages[i]); +if (page_ops &PAGE_UNLOCK ) { + page_end =page_offset (pages[i]) + +PAGE_CACHE_SIZE -1 ; +if ((page_end <= end) + || (end == delalloc_end)) +unlock_page (pages[i]); + }page_cache_release (pages[i]); } nr_pages -= ret;
fs/btrfs/extent_io.h +2 -3
@@ -386,9 +386,8 @@ int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset,int extent_range_clear_dirty_for_io (struct inode *inode,u64 start,u64 end);int extent_range_redirty_for_io (struct inode *inode,u64 start,u64 end);int extent_clear_unlock_delalloc (struct inode *inode,u64 start,u64 end, -struct page *locked_page, -unsigned bits_to_clear, -unsigned long page_ops); +u64 delalloc_end,struct page *locked_page, +unsigned bits_to_clear,unsigned long page_ops);struct bio *btrfs_bio_alloc (struct block_device *bdev,u64 first_sector,int nr_vecs,gfp_t gfp_flags);
fs/btrfs/inode.c +48 -36
192 lines not on this page · raw message
Signature · 1 line
2.1.0
Quoted 27 lines · On Monday 16 Nov 2015 12:38:27 Chandan Rajendra wrote:
Btrfs assumes block size to be the same as the machine's page size. This would mean that a Btrfs instance created on a 4k page size machine (e.g. x86) will not be mountable on machines with larger page sizes (e.g. PPC64/AARCH64). This patchset aims to resolve this incompatibility.
This patchset continues with the work posted previously at
https://lwn.net/Articles/646937/.I have reverted the upstream commit "btrfs: fix lockups from
btrfs_clear_path_blocking" (f82c458a2c3ffb94b431fc6ad791a79df1b3713e) since this led to soft-lockups when the patch "Btrfs:
subpagesize-blocksize: Prevent writes to an extent buffer whenPG_writebackflag is set" is applied.The commits for the Btrfs kernel module can be found at
https://github.com/chandanr/linux/tree/btrfs/subpagesize-blocksize.The commits for Btrfs-progs can be found at
https://github.com/chandanr/btrfs-progs/tree/btrfs/subpagesize-blocksize.Changes from V11:
1. Addressed the review comments provided by Liu Bo for version V11.
2. Fixed file defragmentation code to work in subpagesize-blocksize scenario.
3. Many "hard to reproduce" bugs were fixed.
Quoted 9 lines
fstests run status:
1. Onx86_64, all the tests (i.e. both generic and btrfs specific tests) pass
- With 2k and 4k data blocksize combinations.
- With and without nodatacow option.
2. On ppc64, generic tests were run with 4k data block size and 16k metadata block size. generic/300 fails because of an issue with the fio tool. The failure is seen even when running the test for other filesystems (i.e. ext4 and xfs).
Sorry, I forgot to mention that the fact that nospace_cache mount option was used when running tests on ppc64. The space cache code still needs to be fixed to work in subpagesize-blocksize scenario.
Signature · 1 line
chandan
Quoted 27 lines · On Monday 16 Nov 2015 12:38:27 Chandan Rajendra wrote:
Btrfs assumes block size to be the same as the machine's page size. This would mean that a Btrfs instance created on a 4k page size machine (e.g. x86) will not be mountable on machines with larger page sizes (e.g. PPC64/AARCH64). This patchset aims to resolve this incompatibility.
This patchset continues with the work posted previously at
https://lwn.net/Articles/646937/.I have reverted the upstream commit "btrfs: fix lockups from
btrfs_clear_path_blocking" (f82c458a2c3ffb94b431fc6ad791a79df1b3713e) since this led to soft-lockups when the patch "Btrfs:
subpagesize-blocksize: Prevent writes to an extent buffer whenPG_writebackflag is set" is applied.The commits for the Btrfs kernel module can be found at
https://github.com/chandanr/linux/tree/btrfs/subpagesize-blocksize.The commits for Btrfs-progs can be found at
https://github.com/chandanr/btrfs-progs/tree/btrfs/subpagesize-blocksize.Changes from V11:
1. Addressed the review comments provided by Liu Bo for version V11.
2. Fixed file defragmentation code to work in subpagesize-blocksize scenario.
3. Many "hard to reproduce" bugs were fixed.
Quoted 9 lines
fstests run status:
1. Onx86_64, all the tests (i.e. both generic and btrfs specific tests) pass
- With 2k and 4k data blocksize combinations.
- With and without nodatacow option.
2. On ppc64, generic tests were run with 4k data block size and 16k metadata block size. generic/300 fails because of an issue with the fio tool. The failure is seen even when running the test for other filesystems (i.e. ext4 and xfs).
One more mistake from my side. On x86_64, For 2k block size, only generic tests were run (since the goal was to get regular I/O upstreamed first and then get the features upstreamed in subsequent releases).
However, On x86_64, with 4k block size, all the tests (i.e. generic and btrfs specific) pass indicating that the changes do not cause any regression in other Btrfs features.
Signature · 1 line
chandan