diff mbox series

[03/21] block: Add bio_for_each_folio_all()

Message ID 20211101203929.954622-4-willy@infradead.org (mailing list archive)
State New, archived
Headers show
Series iomap/xfs folio patches | expand

Commit Message

Matthew Wilcox Nov. 1, 2021, 8:39 p.m. UTC
Allow callers to iterate over each folio instead of each page.  The
bio need not have been constructed using folios originally.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 Documentation/core-api/kernel-api.rst |  1 +
 include/linux/bio.h                   | 53 ++++++++++++++++++++++++++-
 2 files changed, 53 insertions(+), 1 deletion(-)

Comments

Jens Axboe Nov. 1, 2021, 8:51 p.m. UTC | #1
On 11/1/21 2:39 PM, Matthew Wilcox (Oracle) wrote:
> Allow callers to iterate over each folio instead of each page.  The
> bio need not have been constructed using folios originally.

Reviewed-by: Jens Axboe <axboe@kernel.dk>
Christoph Hellwig Nov. 2, 2021, 7:13 a.m. UTC | #2
On Mon, Nov 01, 2021 at 08:39:11PM +0000, Matthew Wilcox (Oracle) wrote:
> +static inline
> +void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)

Please fix the weird prototype formatting here.

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>
Matthew Wilcox Nov. 2, 2021, 8:24 p.m. UTC | #3
On Tue, Nov 02, 2021 at 12:13:22AM -0700, Christoph Hellwig wrote:
> On Mon, Nov 01, 2021 at 08:39:11PM +0000, Matthew Wilcox (Oracle) wrote:
> > +static inline
> > +void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
> 
> Please fix the weird prototype formatting here.

I dunno, it looks weirder this way:

-static inline
-void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
+static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
+               int i)

Anyway, I've made that change, but I still prefer it the way I had it.

> Otherwise looks good:
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
Darrick J. Wong Nov. 2, 2021, 10:24 p.m. UTC | #4
On Tue, Nov 02, 2021 at 08:24:14PM +0000, Matthew Wilcox wrote:
> On Tue, Nov 02, 2021 at 12:13:22AM -0700, Christoph Hellwig wrote:
> > On Mon, Nov 01, 2021 at 08:39:11PM +0000, Matthew Wilcox (Oracle) wrote:
> > > +static inline
> > > +void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
> > 
> > Please fix the weird prototype formatting here.
> 
> I dunno, it looks weirder this way:
> 
> -static inline
> -void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
> +static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
> +               int i)
> 
> Anyway, I've made that change, but I still prefer it the way I had it.

I /think/ Christoph meant:

static inline void
bio_first_folio(...)

Though the form that you've changed it to is also fine.

--D

> > Otherwise looks good:
> > 
> > Reviewed-by: Christoph Hellwig <hch@lst.de>
Jens Axboe Nov. 2, 2021, 10:33 p.m. UTC | #5
On 11/2/21 4:24 PM, Darrick J. Wong wrote:
> On Tue, Nov 02, 2021 at 08:24:14PM +0000, Matthew Wilcox wrote:
>> On Tue, Nov 02, 2021 at 12:13:22AM -0700, Christoph Hellwig wrote:
>>> On Mon, Nov 01, 2021 at 08:39:11PM +0000, Matthew Wilcox (Oracle) wrote:
>>>> +static inline
>>>> +void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
>>>
>>> Please fix the weird prototype formatting here.
>>
>> I dunno, it looks weirder this way:
>>
>> -static inline
>> -void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
>> +static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
>> +               int i)
>>
>> Anyway, I've made that change, but I still prefer it the way I had it.
> 
> I /think/ Christoph meant:
> 
> static inline void
> bio_first_folio(...)
> 
> Though the form that you've changed it to is also fine.

I won't speak for Christoph, but basically everything else in block
follows the:

static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
                                   int i)
{
}

format, and this one should as well.
Darrick J. Wong Nov. 2, 2021, 10:37 p.m. UTC | #6
On Tue, Nov 02, 2021 at 04:33:39PM -0600, Jens Axboe wrote:
> On 11/2/21 4:24 PM, Darrick J. Wong wrote:
> > On Tue, Nov 02, 2021 at 08:24:14PM +0000, Matthew Wilcox wrote:
> >> On Tue, Nov 02, 2021 at 12:13:22AM -0700, Christoph Hellwig wrote:
> >>> On Mon, Nov 01, 2021 at 08:39:11PM +0000, Matthew Wilcox (Oracle) wrote:
> >>>> +static inline
> >>>> +void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
> >>>
> >>> Please fix the weird prototype formatting here.
> >>
> >> I dunno, it looks weirder this way:
> >>
> >> -static inline
> >> -void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
> >> +static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
> >> +               int i)
> >>
> >> Anyway, I've made that change, but I still prefer it the way I had it.
> > 
> > I /think/ Christoph meant:
> > 
> > static inline void
> > bio_first_folio(...)
> > 
> > Though the form that you've changed it to is also fine.
> 
> I won't speak for Christoph, but basically everything else in block
> follows the:
> 
> static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
>                                    int i)
> {
> }
> 
> format, and this one should as well.

Durrr, /me forgot he was looking at block patches, not fs/iomap/. :(

Sorry for the noise.

--D

> -- 
> Jens Axboe
>
diff mbox series

Patch

diff --git a/Documentation/core-api/kernel-api.rst b/Documentation/core-api/kernel-api.rst
index 2e7186805148..7f0cb604b6ab 100644
--- a/Documentation/core-api/kernel-api.rst
+++ b/Documentation/core-api/kernel-api.rst
@@ -279,6 +279,7 @@  Accounting Framework
 Block Devices
 =============
 
+.. kernel-doc:: include/linux/bio.h
 .. kernel-doc:: block/blk-core.c
    :export:
 
diff --git a/include/linux/bio.h b/include/linux/bio.h
index a783cac49978..43b252a99334 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -166,7 +166,7 @@  static inline void bio_advance(struct bio *bio, unsigned int nbytes)
  */
 #define bio_for_each_bvec_all(bvl, bio, i)		\
 	for (i = 0, bvl = bio_first_bvec_all(bio);	\
-	     i < (bio)->bi_vcnt; i++, bvl++)		\
+	     i < (bio)->bi_vcnt; i++, bvl++)
 
 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
 
@@ -260,6 +260,57 @@  static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
 	return &bio->bi_io_vec[bio->bi_vcnt - 1];
 }
 
+/**
+ * struct folio_iter - State for iterating all folios in a bio.
+ * @folio: The current folio we're iterating.  NULL after the last folio.
+ * @offset: The byte offset within the current folio.
+ * @length: The number of bytes in this iteration (will not cross folio
+ *	boundary).
+ */
+struct folio_iter {
+	struct folio *folio;
+	size_t offset;
+	size_t length;
+	/* private: for use by the iterator */
+	size_t _seg_count;
+	int _i;
+};
+
+static inline
+void bio_first_folio(struct folio_iter *fi, struct bio *bio, int i)
+{
+	struct bio_vec *bvec = bio_first_bvec_all(bio) + i;
+
+	fi->folio = page_folio(bvec->bv_page);
+	fi->offset = bvec->bv_offset +
+			PAGE_SIZE * (bvec->bv_page - &fi->folio->page);
+	fi->_seg_count = bvec->bv_len;
+	fi->length = min(folio_size(fi->folio) - fi->offset, fi->_seg_count);
+	fi->_i = i;
+}
+
+static inline void bio_next_folio(struct folio_iter *fi, struct bio *bio)
+{
+	fi->_seg_count -= fi->length;
+	if (fi->_seg_count) {
+		fi->folio = folio_next(fi->folio);
+		fi->offset = 0;
+		fi->length = min(folio_size(fi->folio), fi->_seg_count);
+	} else if (fi->_i + 1 < bio->bi_vcnt) {
+		bio_first_folio(fi, bio, fi->_i + 1);
+	} else {
+		fi->folio = NULL;
+	}
+}
+
+/**
+ * bio_for_each_folio_all - Iterate over each folio in a bio.
+ * @fi: struct folio_iter which is updated for each folio.
+ * @bio: struct bio to iterate over.
+ */
+#define bio_for_each_folio_all(fi, bio)				\
+	for (bio_first_folio(&fi, bio, 0); fi.folio; bio_next_folio(&fi, bio))
+
 enum bip_flags {
 	BIP_BLOCK_INTEGRITY	= 1 << 0, /* block layer owns integrity data */
 	BIP_MAPPED_INTEGRITY	= 1 << 1, /* ref tag has been remapped */