Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
4 */
5#ifndef __LINUX_BIO_H
6#define __LINUX_BIO_H
7
8#include <linux/mempool.h>
9/* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
10#include <linux/blk_types.h>
11#include <linux/uio.h>
12
13#define BIO_MAX_VECS 256U
14#define BIO_MAX_INLINE_VECS UIO_MAXIOV
15
16struct queue_limits;
17
18static inline unsigned int bio_max_segs(unsigned int nr_segs)
19{
20 return min(nr_segs, BIO_MAX_VECS);
21}
22
23#define bio_iter_iovec(bio, iter) \
24 bvec_iter_bvec((bio)->bi_io_vec, (iter))
25
26#define bio_iter_page(bio, iter) \
27 bvec_iter_page((bio)->bi_io_vec, (iter))
28#define bio_iter_len(bio, iter) \
29 bvec_iter_len((bio)->bi_io_vec, (iter))
30#define bio_iter_offset(bio, iter) \
31 bvec_iter_offset((bio)->bi_io_vec, (iter))
32
33#define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
34#define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
35#define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
36
37#define bvec_iter_sectors(iter) ((iter).bi_size >> 9)
38#define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
39
40#define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
41#define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
42
43/*
44 * Return the data direction, READ or WRITE.
45 */
46#define bio_data_dir(bio) \
47 (op_is_write(bio_op(bio)) ? WRITE : READ)
48
49/*
50 * Check whether this bio carries any data or not. A NULL bio is allowed.
51 */
52static inline bool bio_has_data(struct bio *bio)
53{
54 if (bio &&
55 bio->bi_iter.bi_size &&
56 bio_op(bio) != REQ_OP_DISCARD &&
57 bio_op(bio) != REQ_OP_SECURE_ERASE &&
58 bio_op(bio) != REQ_OP_WRITE_ZEROES)
59 return true;
60
61 return false;
62}
63
64static inline bool bio_no_advance_iter(const struct bio *bio)
65{
66 return bio_op(bio) == REQ_OP_DISCARD ||
67 bio_op(bio) == REQ_OP_SECURE_ERASE ||
68 bio_op(bio) == REQ_OP_WRITE_ZEROES;
69}
70
71static inline void *bio_data(struct bio *bio)
72{
73 if (bio_has_data(bio))
74 return page_address(bio_page(bio)) + bio_offset(bio);
75
76 return NULL;
77}
78
79static inline bool bio_next_segment(const struct bio *bio,
80 struct bvec_iter_all *iter)
81{
82 if (iter->idx >= bio->bi_vcnt)
83 return false;
84
85 bvec_advance(&bio->bi_io_vec[iter->idx], iter);
86 return true;
87}
88
89/*
90 * drivers should _never_ use the all version - the bio may have been split
91 * before it got to the driver and the driver won't own all of it
92 */
93#define bio_for_each_segment_all(bvl, bio, iter) \
94 for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); )
95
96static inline void bio_advance_iter(const struct bio *bio,
97 struct bvec_iter *iter, unsigned int bytes)
98{
99 iter->bi_sector += bytes >> 9;
100
101 if (bio_no_advance_iter(bio))
102 iter->bi_size -= bytes;
103 else
104 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
105 /* TODO: It is reasonable to complete bio with error here. */
106}
107
108/* @bytes should be less or equal to bvec[i->bi_idx].bv_len */
109static inline void bio_advance_iter_single(const struct bio *bio,
110 struct bvec_iter *iter,
111 unsigned int bytes)
112{
113 iter->bi_sector += bytes >> 9;
114
115 if (bio_no_advance_iter(bio))
116 iter->bi_size -= bytes;
117 else
118 bvec_iter_advance_single(bio->bi_io_vec, iter, bytes);
119}
120
121void __bio_advance(struct bio *, unsigned bytes);
122
123/**
124 * bio_advance - increment/complete a bio by some number of bytes
125 * @bio: bio to advance
126 * @nbytes: number of bytes to complete
127 *
128 * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
129 * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
130 * be updated on the last bvec as well.
131 *
132 * @bio will then represent the remaining, uncompleted portion of the io.
133 */
134static inline void bio_advance(struct bio *bio, unsigned int nbytes)
135{
136 if (nbytes == bio->bi_iter.bi_size) {
137 bio->bi_iter.bi_size = 0;
138 return;
139 }
140 __bio_advance(bio, nbytes);
141}
142
143#define __bio_for_each_segment(bvl, bio, iter, start) \
144 for (iter = (start); \
145 (iter).bi_size && \
146 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
147 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
148
149#define bio_for_each_segment(bvl, bio, iter) \
150 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
151
152#define __bio_for_each_bvec(bvl, bio, iter, start) \
153 for (iter = (start); \
154 (iter).bi_size && \
155 ((bvl = mp_bvec_iter_bvec((bio)->bi_io_vec, (iter))), 1); \
156 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
157
158/* iterate over multi-page bvec */
159#define bio_for_each_bvec(bvl, bio, iter) \
160 __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)
161
162/*
163 * Iterate over all multi-page bvecs. Drivers shouldn't use this version for the
164 * same reasons as bio_for_each_segment_all().
165 */
166#define bio_for_each_bvec_all(bvl, bio, i) \
167 for (i = 0, bvl = bio_first_bvec_all(bio); \
168 i < (bio)->bi_vcnt; i++, bvl++)
169
170#define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
171
172static inline unsigned bio_segments(struct bio *bio)
173{
174 unsigned segs = 0;
175 struct bio_vec bv;
176 struct bvec_iter iter;
177
178 /*
179 * We special case discard/write same/write zeroes, because they
180 * interpret bi_size differently:
181 */
182
183 switch (bio_op(bio)) {
184 case REQ_OP_DISCARD:
185 case REQ_OP_SECURE_ERASE:
186 case REQ_OP_WRITE_ZEROES:
187 return 0;
188 default:
189 break;
190 }
191
192 bio_for_each_segment(bv, bio, iter)
193 segs++;
194
195 return segs;
196}
197
198/*
199 * get a reference to a bio, so it won't disappear. the intended use is
200 * something like:
201 *
202 * bio_get(bio);
203 * submit_bio(rw, bio);
204 * if (bio->bi_flags ...)
205 * do_something
206 * bio_put(bio);
207 *
208 * without the bio_get(), it could potentially complete I/O before submit_bio
209 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
210 * runs
211 */
212static inline void bio_get(struct bio *bio)
213{
214 bio->bi_flags |= (1 << BIO_REFFED);
215 smp_mb__before_atomic();
216 atomic_inc(&bio->__bi_cnt);
217}
218
219static inline void bio_cnt_set(struct bio *bio, unsigned int count)
220{
221 if (count != 1) {
222 bio->bi_flags |= (1 << BIO_REFFED);
223 smp_mb();
224 }
225 atomic_set(&bio->__bi_cnt, count);
226}
227
228static inline bool bio_flagged(struct bio *bio, unsigned int bit)
229{
230 return bio->bi_flags & (1U << bit);
231}
232
233static inline void bio_set_flag(struct bio *bio, unsigned int bit)
234{
235 bio->bi_flags |= (1U << bit);
236}
237
238static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
239{
240 bio->bi_flags &= ~(1U << bit);
241}
242
243static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
244{
245 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
246 return bio->bi_io_vec;
247}
248
249static inline struct page *bio_first_page_all(struct bio *bio)
250{
251 return bio_first_bvec_all(bio)->bv_page;
252}
253
254static inline struct folio *bio_first_folio_all(struct bio *bio)
255{
256 return page_folio(bio_first_page_all(bio));
257}
258
259static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
260{
261 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
262 return &bio->bi_io_vec[bio->bi_vcnt - 1];
263}
264
265/**
266 * struct folio_iter - State for iterating all folios in a bio.
267 * @folio: The current folio we're iterating. NULL after the last folio.
268 * @offset: The byte offset within the current folio.
269 * @length: The number of bytes in this iteration (will not cross folio
270 * boundary).
271 */
272struct folio_iter {
273 struct folio *folio;
274 size_t offset;
275 size_t length;
276 /* private: for use by the iterator */
277 struct folio *_next;
278 size_t _seg_count;
279 int _i;
280};
281
282static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
283 int i)
284{
285 struct bio_vec *bvec = bio_first_bvec_all(bio) + i;
286
287 if (unlikely(i >= bio->bi_vcnt)) {
288 fi->folio = NULL;
289 return;
290 }
291
292 fi->folio = page_folio(bvec->bv_page);
293 fi->offset = bvec->bv_offset +
294 PAGE_SIZE * (bvec->bv_page - &fi->folio->page);
295 fi->_seg_count = bvec->bv_len;
296 fi->length = min(folio_size(fi->folio) - fi->offset, fi->_seg_count);
297 fi->_next = folio_next(fi->folio);
298 fi->_i = i;
299}
300
301static inline void bio_next_folio(struct folio_iter *fi, struct bio *bio)
302{
303 fi->_seg_count -= fi->length;
304 if (fi->_seg_count) {
305 fi->folio = fi->_next;
306 fi->offset = 0;
307 fi->length = min(folio_size(fi->folio), fi->_seg_count);
308 fi->_next = folio_next(fi->folio);
309 } else {
310 bio_first_folio(fi, bio, fi->_i + 1);
311 }
312}
313
314/**
315 * bio_for_each_folio_all - Iterate over each folio in a bio.
316 * @fi: struct folio_iter which is updated for each folio.
317 * @bio: struct bio to iterate over.
318 */
319#define bio_for_each_folio_all(fi, bio) \
320 for (bio_first_folio(&fi, bio, 0); fi.folio; bio_next_folio(&fi, bio))
321
322void bio_trim(struct bio *bio, sector_t offset, sector_t size);
323extern struct bio *bio_split(struct bio *bio, int sectors,
324 gfp_t gfp, struct bio_set *bs);
325int bio_split_rw_at(struct bio *bio, const struct queue_limits *lim,
326 unsigned *segs, unsigned max_bytes);
327
328/**
329 * bio_next_split - get next @sectors from a bio, splitting if necessary
330 * @bio: bio to split
331 * @sectors: number of sectors to split from the front of @bio
332 * @gfp: gfp mask
333 * @bs: bio set to allocate from
334 *
335 * Return: a bio representing the next @sectors of @bio - if the bio is smaller
336 * than @sectors, returns the original bio unchanged.
337 */
338static inline struct bio *bio_next_split(struct bio *bio, int sectors,
339 gfp_t gfp, struct bio_set *bs)
340{
341 if (sectors >= bio_sectors(bio))
342 return bio;
343
344 return bio_split(bio, sectors, gfp, bs);
345}
346
347enum {
348 BIOSET_NEED_BVECS = BIT(0),
349 BIOSET_NEED_RESCUER = BIT(1),
350 BIOSET_PERCPU_CACHE = BIT(2),
351};
352extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
353extern void bioset_exit(struct bio_set *);
354extern int biovec_init_pool(mempool_t *pool, int pool_entries);
355
356struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
357 blk_opf_t opf, gfp_t gfp_mask,
358 struct bio_set *bs);
359struct bio *bio_kmalloc(unsigned short nr_vecs, gfp_t gfp_mask);
360extern void bio_put(struct bio *);
361
362struct bio *bio_alloc_clone(struct block_device *bdev, struct bio *bio_src,
363 gfp_t gfp, struct bio_set *bs);
364int bio_init_clone(struct block_device *bdev, struct bio *bio,
365 struct bio *bio_src, gfp_t gfp);
366
367extern struct bio_set fs_bio_set;
368
369static inline struct bio *bio_alloc(struct block_device *bdev,
370 unsigned short nr_vecs, blk_opf_t opf, gfp_t gfp_mask)
371{
372 return bio_alloc_bioset(bdev, nr_vecs, opf, gfp_mask, &fs_bio_set);
373}
374
375void submit_bio(struct bio *bio);
376
377extern void bio_endio(struct bio *);
378
379static inline void bio_io_error(struct bio *bio)
380{
381 bio->bi_status = BLK_STS_IOERR;
382 bio_endio(bio);
383}
384
385static inline void bio_wouldblock_error(struct bio *bio)
386{
387 bio_set_flag(bio, BIO_QUIET);
388 bio->bi_status = BLK_STS_AGAIN;
389 bio_endio(bio);
390}
391
392/*
393 * Calculate number of bvec segments that should be allocated to fit data
394 * pointed by @iter. If @iter is backed by bvec it's going to be reused
395 * instead of allocating a new one.
396 */
397static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
398{
399 if (iov_iter_is_bvec(iter))
400 return 0;
401 return iov_iter_npages(iter, max_segs);
402}
403
404struct request_queue;
405
406extern int submit_bio_wait(struct bio *bio);
407void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table,
408 unsigned short max_vecs, blk_opf_t opf);
409extern void bio_uninit(struct bio *);
410void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf);
411void bio_chain(struct bio *, struct bio *);
412
413int __must_check bio_add_page(struct bio *bio, struct page *page, unsigned len,
414 unsigned off);
415bool __must_check bio_add_folio(struct bio *bio, struct folio *folio,
416 size_t len, size_t off);
417void __bio_add_page(struct bio *bio, struct page *page,
418 unsigned int len, unsigned int off);
419void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len,
420 size_t off);
421int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
422void bio_iov_bvec_set(struct bio *bio, const struct iov_iter *iter);
423void __bio_release_pages(struct bio *bio, bool mark_dirty);
424extern void bio_set_pages_dirty(struct bio *bio);
425extern void bio_check_pages_dirty(struct bio *bio);
426
427extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
428 struct bio *src, struct bvec_iter *src_iter);
429extern void bio_copy_data(struct bio *dst, struct bio *src);
430extern void bio_free_pages(struct bio *bio);
431void guard_bio_eod(struct bio *bio);
432void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
433
434static inline void zero_fill_bio(struct bio *bio)
435{
436 zero_fill_bio_iter(bio, bio->bi_iter);
437}
438
439static inline void bio_release_pages(struct bio *bio, bool mark_dirty)
440{
441 if (bio_flagged(bio, BIO_PAGE_PINNED))
442 __bio_release_pages(bio, mark_dirty);
443}
444
445#define bio_dev(bio) \
446 disk_devt((bio)->bi_bdev->bd_disk)
447
448#ifdef CONFIG_BLK_CGROUP
449void bio_associate_blkg(struct bio *bio);
450void bio_associate_blkg_from_css(struct bio *bio,
451 struct cgroup_subsys_state *css);
452void bio_clone_blkg_association(struct bio *dst, struct bio *src);
453void blkcg_punt_bio_submit(struct bio *bio);
454#else /* CONFIG_BLK_CGROUP */
455static inline void bio_associate_blkg(struct bio *bio) { }
456static inline void bio_associate_blkg_from_css(struct bio *bio,
457 struct cgroup_subsys_state *css)
458{ }
459static inline void bio_clone_blkg_association(struct bio *dst,
460 struct bio *src) { }
461static inline void blkcg_punt_bio_submit(struct bio *bio)
462{
463 submit_bio(bio);
464}
465#endif /* CONFIG_BLK_CGROUP */
466
467static inline void bio_set_dev(struct bio *bio, struct block_device *bdev)
468{
469 bio_clear_flag(bio, BIO_REMAPPED);
470 if (bio->bi_bdev != bdev)
471 bio_clear_flag(bio, BIO_BPS_THROTTLED);
472 bio->bi_bdev = bdev;
473 bio_associate_blkg(bio);
474}
475
476/*
477 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
478 *
479 * A bio_list anchors a singly-linked list of bios chained through the bi_next
480 * member of the bio. The bio_list also caches the last list member to allow
481 * fast access to the tail.
482 */
483struct bio_list {
484 struct bio *head;
485 struct bio *tail;
486};
487
488static inline int bio_list_empty(const struct bio_list *bl)
489{
490 return bl->head == NULL;
491}
492
493static inline void bio_list_init(struct bio_list *bl)
494{
495 bl->head = bl->tail = NULL;
496}
497
498#define BIO_EMPTY_LIST { NULL, NULL }
499
500#define bio_list_for_each(bio, bl) \
501 for (bio = (bl)->head; bio; bio = bio->bi_next)
502
503static inline unsigned bio_list_size(const struct bio_list *bl)
504{
505 unsigned sz = 0;
506 struct bio *bio;
507
508 bio_list_for_each(bio, bl)
509 sz++;
510
511 return sz;
512}
513
514static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
515{
516 bio->bi_next = NULL;
517
518 if (bl->tail)
519 bl->tail->bi_next = bio;
520 else
521 bl->head = bio;
522
523 bl->tail = bio;
524}
525
526static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
527{
528 bio->bi_next = bl->head;
529
530 bl->head = bio;
531
532 if (!bl->tail)
533 bl->tail = bio;
534}
535
536static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
537{
538 if (!bl2->head)
539 return;
540
541 if (bl->tail)
542 bl->tail->bi_next = bl2->head;
543 else
544 bl->head = bl2->head;
545
546 bl->tail = bl2->tail;
547}
548
549static inline void bio_list_merge_init(struct bio_list *bl,
550 struct bio_list *bl2)
551{
552 bio_list_merge(bl, bl2);
553 bio_list_init(bl2);
554}
555
556static inline void bio_list_merge_head(struct bio_list *bl,
557 struct bio_list *bl2)
558{
559 if (!bl2->head)
560 return;
561
562 if (bl->head)
563 bl2->tail->bi_next = bl->head;
564 else
565 bl->tail = bl2->tail;
566
567 bl->head = bl2->head;
568}
569
570static inline struct bio *bio_list_peek(struct bio_list *bl)
571{
572 return bl->head;
573}
574
575static inline struct bio *bio_list_pop(struct bio_list *bl)
576{
577 struct bio *bio = bl->head;
578
579 if (bio) {
580 bl->head = bl->head->bi_next;
581 if (!bl->head)
582 bl->tail = NULL;
583
584 bio->bi_next = NULL;
585 }
586
587 return bio;
588}
589
590static inline struct bio *bio_list_get(struct bio_list *bl)
591{
592 struct bio *bio = bl->head;
593
594 bl->head = bl->tail = NULL;
595
596 return bio;
597}
598
599/*
600 * Increment chain count for the bio. Make sure the CHAIN flag update
601 * is visible before the raised count.
602 */
603static inline void bio_inc_remaining(struct bio *bio)
604{
605 bio_set_flag(bio, BIO_CHAIN);
606 smp_mb__before_atomic();
607 atomic_inc(&bio->__bi_remaining);
608}
609
610/*
611 * bio_set is used to allow other portions of the IO system to
612 * allocate their own private memory pools for bio and iovec structures.
613 * These memory pools in turn all allocate from the bio_slab
614 * and the bvec_slabs[].
615 */
616#define BIO_POOL_SIZE 2
617
618struct bio_set {
619 struct kmem_cache *bio_slab;
620 unsigned int front_pad;
621
622 /*
623 * per-cpu bio alloc cache
624 */
625 struct bio_alloc_cache __percpu *cache;
626
627 mempool_t bio_pool;
628 mempool_t bvec_pool;
629
630 unsigned int back_pad;
631 /*
632 * Deadlock avoidance for stacking block drivers: see comments in
633 * bio_alloc_bioset() for details
634 */
635 spinlock_t rescue_lock;
636 struct bio_list rescue_list;
637 struct work_struct rescue_work;
638 struct workqueue_struct *rescue_workqueue;
639
640 /*
641 * Hot un-plug notifier for the per-cpu cache, if used
642 */
643 struct hlist_node cpuhp_dead;
644};
645
646static inline bool bioset_initialized(struct bio_set *bs)
647{
648 return bs->bio_slab != NULL;
649}
650
651/*
652 * Mark a bio as polled. Note that for async polled IO, the caller must
653 * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
654 * We cannot block waiting for requests on polled IO, as those completions
655 * must be found by the caller. This is different than IRQ driven IO, where
656 * it's safe to wait for IO to complete.
657 */
658static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
659{
660 bio->bi_opf |= REQ_POLLED;
661 if (kiocb->ki_flags & IOCB_NOWAIT)
662 bio->bi_opf |= REQ_NOWAIT;
663}
664
665static inline void bio_clear_polled(struct bio *bio)
666{
667 bio->bi_opf &= ~REQ_POLLED;
668}
669
670/**
671 * bio_is_zone_append - is this a zone append bio?
672 * @bio: bio to check
673 *
674 * Check if @bio is a zone append operation. Core block layer code and end_io
675 * handlers must use this instead of an open coded REQ_OP_ZONE_APPEND check
676 * because the block layer can rewrite REQ_OP_ZONE_APPEND to REQ_OP_WRITE if
677 * it is not natively supported.
678 */
679static inline bool bio_is_zone_append(struct bio *bio)
680{
681 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED))
682 return false;
683 return bio_op(bio) == REQ_OP_ZONE_APPEND ||
684 bio_flagged(bio, BIO_EMULATES_ZONE_APPEND);
685}
686
687struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev,
688 unsigned int nr_pages, blk_opf_t opf, gfp_t gfp);
689struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new);
690
691struct bio *blk_alloc_discard_bio(struct block_device *bdev,
692 sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask);
693
694#endif /* __LINUX_BIO_H */