Skip to contentSkip to conversation

btrfs: btrfs_dec_test_*_ordered_extent() refactor

PATCH Superseded Qu Wenruo 3 participants 6 messages Dec 18, 2020 – Dec 22, 2020 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: btrfs_dec_test_*_ordered_extent() refactor

This small patchset is btrfs_dec_test_*_ordered_extent() refactor during subpage RW support development.

This is mostly to make btrfs_dev_test_* functions more human readable and prepare it for calling btrfs_dec_test_first_ordered_extent() in btrfs_writepage_endio_finish_ordered() where we can have one or more ordered extents for one bvec.

Qu Wenruo (2):
btrfs: make btrfs_dio_private::bytes to be u32
btrfs: refactor btrfs_dec_test_* functions for ordered extents

 fs/btrfs/btrfs_inode.h  |  2 +-
 fs/btrfs/inode.c        |  7 ++-
 fs/btrfs/ordered-data.c | 98 ++++++++++++++++++++++-------------------
 fs/btrfs/ordered-data.h | 10 ++---
 4 files changed, 62 insertions(+), 55 deletions(-)
Signature · 1 line
2.29.2
author
#1 lore raw

PATCH 1/2 btrfs: make btrfs_dio_private::bytes to be u32

btrfs_dio_private::bytes is only assigned from bio::bi_iter::bi_size, which is no larger than U32.

---
 fs/btrfs/btrfs_inode.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/btrfs_inode.h +1 -1
index d9bf53d9ff90..fbd65807f29d 100644
@@ -325,7 +325,7 @@ struct btrfs_dio_private {
 	struct inode *inode;
 	u64 logical_offset;
 	u64 disk_bytenr;
-	u64 bytes;
+	u32 bytes;
 
 	/*
 	 * References to this structure. There is one reference per in-flight
Signature · 1 line
2.29.2
author
#2 lore raw

PATCH 2/2 btrfs: refactor btrfs_dec_test_* functions for ordered extents

In reply to Qu Wenruo

The refactors involves the following modifications:
- Return bool instead of int

- Reduce the width of @io_size for btrfs_dec_test_ordered_pending() Function btrfs_dec_test_ordered_pending() is only supposed to handle at most one page, thus u32 for @io_size is enough.
Also added ASSERT()s for such width reduce.

For btrfs_dev_test_first_ordered_pending(), btrfs_dio_iomap_end() is the only blockage, since its @length is loff_t and I'm not that confident that @length can be contained in U32, thus btrfs_dev_test_first_ordered_pending() still uses u64 for its @iosize.

- Parameter rename for @cached of btrfs_dec_test_first_ordered_pending() For btrfs_dec_test_first_ordered_pending(), @cached is only used to return the finished ordered extent.
Rename it to @finished_ret.

- Comments update
* Change one stale comment
Which still refers to btrfs_dec_test_ordered_pending(), but the context is calling btrfs_dec_test_first_ordered_pending().
* Follow the common comment style for both functions
Add more detailed descriptions for parameters and the return value
* Move the reason why test_and_set_bit() is used into the call sites

- Change how the return value is calculated The most anti-human part of the return value is:

    if (...)
	ret = 1;
    ...
    return ret == 0;

This means, when we set ret to 1, the function returns 0. Change the local variable name to @finished, and directly return the value of it.

---
 fs/btrfs/inode.c        |  7 ++-
 fs/btrfs/ordered-data.c | 98 ++++++++++++++++++++++-------------------
 fs/btrfs/ordered-data.h | 10 ++---
 3 files changed, 61 insertions(+), 54 deletions(-)
fs/btrfs/inode.c +3 -4
index fa9fbed36ec9..ab110552daef 100644
@@ -2920,6 +2920,7 @@ void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
 	trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
 
 	ClearPagePrivate2(page);
+	ASSERT(end - start + 1 < U32_MAX);
 	if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
 					    end - start + 1, uptodate))
 		return;
@@ -7793,10 +7794,7 @@ static void __endio_write_update_ordered(struct btrfs_inode *inode,
 					NULL);
 			btrfs_queue_work(wq, &ordered->work);
 		}
-		/*
-		 * If btrfs_dec_test_ordered_pending does not find any ordered
-		 * extent in the range, we can exit.
-		 */
+		/* No ordered extent found in the range, exit. */
 		if (ordered_offset == last_offset)
 			return;
 		/*
@@ -8216,6 +8214,7 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
 				ordered->truncated_len = new_len;
 			spin_unlock_irq(&tree->lock);
 
+			ASSERT(end - start + 1 < U32_MAX);
 			if (btrfs_dec_test_ordered_pending(inode, &ordered,
 							   start,
 							   end - start + 1, 1)) {
fs/btrfs/ordered-data.c +53 -45

174 lines not on this page · raw message

fs/btrfs/ordered-data.h +5 -5
index 0bfa82b58e23..3ee987cd402d 100644
@@ -152,11 +152,11 @@ btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
 void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry);
 void btrfs_remove_ordered_extent(struct btrfs_inode *btrfs_inode,
 				struct btrfs_ordered_extent *entry);
-int btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
-				   struct btrfs_ordered_extent **cached,
-				   u64 file_offset, u64 io_size, int uptodate);
-int btrfs_dec_test_first_ordered_pending(struct btrfs_inode *inode,
-				   struct btrfs_ordered_extent **cached,
+bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
+				    struct btrfs_ordered_extent **cached,
+				    u64 file_offset, u32 io_size, int uptodate);
+bool btrfs_dec_test_first_ordered_pending(struct btrfs_inode *inode,
+				   struct btrfs_ordered_extent **finished_ret,
 				   u64 *file_offset, u64 io_size,
 				   int uptodate);
 int btrfs_add_ordered_extent(struct btrfs_inode *inode, u64 file_offset,
Signature · 1 line
2.29.2
#3 lore raw
In reply to Qu Wenruo
Quoted 11 lines · Qu Wenruo

This small patchset is btrfs_dec_test_*_ordered_extent() refactor during subpage RW support development.

This is mostly to make btrfs_dev_test_* functions more human readable and prepare it for calling btrfs_dec_test_first_ordered_extent() in btrfs_writepage_endio_finish_ordered() where we can have one or more ordered extents for one bvec.

Qu Wenruo (2):
btrfs: make btrfs_dio_private::bytes to be u32
btrfs: refactor btrfs_dec_test_* functions for ordered extents

The idea makes sense but the patches are IMO in wrong order and still leave some u64/u32, eg. in btrfs_dec_test_first_ordered_pending the io_size is still u64 while for btrfs_dec_test_ordered_pending it switches type to u32 (as expected).

The type cleanup should be done bottom-up, from the leaf functions upwards. After that, the structure type can be safely switched.

I'm not sure what to do with __endio_write_update_ordered, it can take u32 for bytes but internally uses u64 for ordered_bytes, that should be u32 as well. But

7711 if (ordered_offset < offset + bytes) {
7712 ordered_bytes = offset + bytes - ordered_offset;
7713 ordered = NULL;
7714 }

expression on line 7712 would need a temporary variable for the u64 calculation and then reassign. Maybe such conversions are inevitable so we have clean function API and not pass u64 just because.

I like that the hole btrfs_dio_private gets removed so the cleanups are worthwhile, but maybe not trivial.

#4 lore raw
Quoted 9 lines · David Sterba
Quoted 8 lines · Qu Wenruo

This small patchset is btrfs_dec_test_*_ordered_extent() refactor during subpage RW support development.

This is mostly to make btrfs_dev_test_* functions more human readable and prepare it for calling btrfs_dec_test_first_ordered_extent() in btrfs_writepage_endio_finish_ordered() where we can have one or more ordered extents for one bvec.

Quoted 8 lines
Quoted 3 lines

Qu Wenruo (2):
btrfs: make btrfs_dio_private::bytes to be u32
btrfs: refactor btrfs_dec_test_* functions for ordered extents

The idea makes sense but the patches are IMO in wrong order and still leave some u64/u32, eg. in btrfs_dec_test_first_ordered_pending the io_size is still u64 while for btrfs_dec_test_ordered_pending it switches type to u32 (as expected).

That u64 is left there and the reason is explained in the 2nd patch.

Mostly due to iomap requirement.

But I totally get your point.

Thanks,
Qu

Quoted 20 lines

The type cleanup should be done bottom-up, from the leaf functions upwards. After that, the structure type can be safely switched.

I'm not sure what to do with __endio_write_update_ordered, it can take u32 for bytes but internally uses u64 for ordered_bytes, that should be u32 as well. But

  7711                 if (ordered_offset < offset + bytes) {
  7712                         ordered_bytes = offset + bytes - ordered_offset;
  7713                         ordered = NULL;
  7714                 }

expression on line 7712 would need a temporary variable for the u64 calculation and then reassign. Maybe such conversions are inevitable so we have clean function API and not pass u64 just because.

I like that the hole btrfs_dio_private gets removed so the cleanups are worthwhile, but maybe not trivial.

#5 lore raw
Quoted 21 lines · Qu Wenruo
Quoted 9 lines · David Sterba
Quoted 8 lines · Qu Wenruo

This small patchset is btrfs_dec_test_*_ordered_extent() refactor during subpage RW support development.

This is mostly to make btrfs_dev_test_* functions more human readable and prepare it for calling btrfs_dec_test_first_ordered_extent() in btrfs_writepage_endio_finish_ordered() where we can have one or more ordered extents for one bvec.

Quoted 8 lines
Quoted 3 lines

Qu Wenruo (2):
   btrfs: make btrfs_dio_private::bytes to be u32
   btrfs: refactor btrfs_dec_test_* functions for ordered extents

The idea makes sense but the patches are IMO in wrong order and still leave some u64/u32, eg. in btrfs_dec_test_first_ordered_pending the io_size is still u64 while for btrfs_dec_test_ordered_pending it switches type to u32 (as expected).

Quoted 5 lines

That u64 is left there and the reason is explained in the 2nd patch.

Mostly due to iomap requirement.

But I totally get your point.

After digging more, I prefer to give up the width reduction, but just focus on the other cleanups in the patch.

The width reduction really needs more concern, especially when iomap is involved.

Thanks,
Qu

Quoted 25 lines

Thanks,
Qu

Quoted 21 lines

The type cleanup should be done bottom-up, from the leaf functions upwards. After that, the structure type can be safely switched.

I'm not sure what to do with __endio_write_update_ordered, it can take u32 for bytes but internally uses u64 for ordered_bytes, that should be u32 as well. But

  7711                 if (ordered_offset < offset + bytes) { 7712                         ordered_bytes = offset + bytes - ordered_offset;
  7713                         ordered = NULL;
  7714                 }

expression on line 7712 would need a temporary variable for the u64 calculation and then reassign. Maybe such conversions are inevitable so we have clean function API and not pass u64 just because.

I like that the hole btrfs_dio_private gets removed so the cleanups are worthwhile, but maybe not trivial.