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/highmem.h>
9 #include <linux/mempool.h>
10 #include <linux/ioprio.h>
11 #include <linux/bio-crypt-ctx.h>
12
13 #ifdef CONFIG_BLOCK
14 /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
15 #include <linux/blk_types.h>
16
17 #define BIO_DEBUG
18
19 #ifdef BIO_DEBUG
20 #define BIO_BUG_ON BUG_ON
21 #else
22 #define BIO_BUG_ON
23 #endif
24
25 #define BIO_MAX_PAGES 256
26
27 #define bio_prio(bio) (bio)->bi_ioprio
28 #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
29
30 #define bio_iter_iovec(bio, iter) \
31 bvec_iter_bvec((bio)->bi_io_vec, (iter))
32
33 #define bio_iter_page(bio, iter) \
34 bvec_iter_page((bio)->bi_io_vec, (iter))
35 #define bio_iter_len(bio, iter) \
36 bvec_iter_len((bio)->bi_io_vec, (iter))
37 #define bio_iter_offset(bio, iter) \
38 bvec_iter_offset((bio)->bi_io_vec, (iter))
39
40 #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
41 #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
42 #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
43
44 #define bio_multiple_segments(bio) \
45 ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
46
47 #define bvec_iter_sectors(iter) ((iter).bi_size >> 9)
48 #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
49
50 #define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
51 #define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
52
53 /*
54 * Return the data direction, READ or WRITE.
55 */
56 #define bio_data_dir(bio) \
57 (op_is_write(bio_op(bio)) ? WRITE : READ)
58
59 /*
60 * Check whether this bio carries any data or not. A NULL bio is allowed.
61 */
bio_has_data(struct bio * bio)62 static inline bool bio_has_data(struct bio *bio)
63 {
64 if (bio &&
65 bio->bi_iter.bi_size &&
66 bio_op(bio) != REQ_OP_DISCARD &&
67 bio_op(bio) != REQ_OP_SECURE_ERASE &&
68 bio_op(bio) != REQ_OP_WRITE_ZEROES)
69 return true;
70
71 return false;
72 }
73
bio_no_advance_iter(struct bio * bio)74 static inline bool bio_no_advance_iter(struct bio *bio)
75 {
76 return bio_op(bio) == REQ_OP_DISCARD ||
77 bio_op(bio) == REQ_OP_SECURE_ERASE ||
78 bio_op(bio) == REQ_OP_WRITE_SAME ||
79 bio_op(bio) == REQ_OP_WRITE_ZEROES;
80 }
81
bio_mergeable(struct bio * bio)82 static inline bool bio_mergeable(struct bio *bio)
83 {
84 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
85 return false;
86
87 return true;
88 }
89
bio_cur_bytes(struct bio * bio)90 static inline unsigned int bio_cur_bytes(struct bio *bio)
91 {
92 if (bio_has_data(bio))
93 return bio_iovec(bio).bv_len;
94 else /* dataless requests such as discard */
95 return bio->bi_iter.bi_size;
96 }
97
bio_data(struct bio * bio)98 static inline void *bio_data(struct bio *bio)
99 {
100 if (bio_has_data(bio))
101 return page_address(bio_page(bio)) + bio_offset(bio);
102
103 return NULL;
104 }
105
106 /**
107 * bio_full - check if the bio is full
108 * @bio: bio to check
109 * @len: length of one segment to be added
110 *
111 * Return true if @bio is full and one segment with @len bytes can't be
112 * added to the bio, otherwise return false
113 */
bio_full(struct bio * bio,unsigned len)114 static inline bool bio_full(struct bio *bio, unsigned len)
115 {
116 if (bio->bi_vcnt >= bio->bi_max_vecs)
117 return true;
118
119 if (bio->bi_iter.bi_size > UINT_MAX - len)
120 return true;
121
122 return false;
123 }
124
bio_next_segment(const struct bio * bio,struct bvec_iter_all * iter)125 static inline bool bio_next_segment(const struct bio *bio,
126 struct bvec_iter_all *iter)
127 {
128 if (iter->idx >= bio->bi_vcnt)
129 return false;
130
131 bvec_advance(&bio->bi_io_vec[iter->idx], iter);
132 return true;
133 }
134
135 /*
136 * drivers should _never_ use the all version - the bio may have been split
137 * before it got to the driver and the driver won't own all of it
138 */
139 #define bio_for_each_segment_all(bvl, bio, iter) \
140 for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); )
141
bio_advance_iter(struct bio * bio,struct bvec_iter * iter,unsigned bytes)142 static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
143 unsigned bytes)
144 {
145 iter->bi_sector += bytes >> 9;
146
147 if (bio_no_advance_iter(bio))
148 iter->bi_size -= bytes;
149 else
150 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
151 /* TODO: It is reasonable to complete bio with error here. */
152 }
153
154 #define __bio_for_each_segment(bvl, bio, iter, start) \
155 for (iter = (start); \
156 (iter).bi_size && \
157 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
158 bio_advance_iter((bio), &(iter), (bvl).bv_len))
159
160 #define bio_for_each_segment(bvl, bio, iter) \
161 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
162
163 #define __bio_for_each_bvec(bvl, bio, iter, start) \
164 for (iter = (start); \
165 (iter).bi_size && \
166 ((bvl = mp_bvec_iter_bvec((bio)->bi_io_vec, (iter))), 1); \
167 bio_advance_iter((bio), &(iter), (bvl).bv_len))
168
169 /* iterate over multi-page bvec */
170 #define bio_for_each_bvec(bvl, bio, iter) \
171 __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)
172
173 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
174
bio_segments(struct bio * bio)175 static inline unsigned bio_segments(struct bio *bio)
176 {
177 unsigned segs = 0;
178 struct bio_vec bv;
179 struct bvec_iter iter;
180
181 /*
182 * We special case discard/write same/write zeroes, because they
183 * interpret bi_size differently:
184 */
185
186 switch (bio_op(bio)) {
187 case REQ_OP_DISCARD:
188 case REQ_OP_SECURE_ERASE:
189 case REQ_OP_WRITE_ZEROES:
190 return 0;
191 case REQ_OP_WRITE_SAME:
192 return 1;
193 default:
194 break;
195 }
196
197 bio_for_each_segment(bv, bio, iter)
198 segs++;
199
200 return segs;
201 }
202
203 /*
204 * get a reference to a bio, so it won't disappear. the intended use is
205 * something like:
206 *
207 * bio_get(bio);
208 * submit_bio(rw, bio);
209 * if (bio->bi_flags ...)
210 * do_something
211 * bio_put(bio);
212 *
213 * without the bio_get(), it could potentially complete I/O before submit_bio
214 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
215 * runs
216 */
bio_get(struct bio * bio)217 static inline void bio_get(struct bio *bio)
218 {
219 bio->bi_flags |= (1 << BIO_REFFED);
220 smp_mb__before_atomic();
221 atomic_inc(&bio->__bi_cnt);
222 }
223
bio_cnt_set(struct bio * bio,unsigned int count)224 static inline void bio_cnt_set(struct bio *bio, unsigned int count)
225 {
226 if (count != 1) {
227 bio->bi_flags |= (1 << BIO_REFFED);
228 smp_mb();
229 }
230 atomic_set(&bio->__bi_cnt, count);
231 }
232
bio_flagged(struct bio * bio,unsigned int bit)233 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
234 {
235 return (bio->bi_flags & (1U << bit)) != 0;
236 }
237
bio_set_flag(struct bio * bio,unsigned int bit)238 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
239 {
240 bio->bi_flags |= (1U << bit);
241 }
242
bio_clear_flag(struct bio * bio,unsigned int bit)243 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
244 {
245 bio->bi_flags &= ~(1U << bit);
246 }
247
bio_get_first_bvec(struct bio * bio,struct bio_vec * bv)248 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
249 {
250 *bv = bio_iovec(bio);
251 }
252
bio_get_last_bvec(struct bio * bio,struct bio_vec * bv)253 static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
254 {
255 struct bvec_iter iter = bio->bi_iter;
256 int idx;
257
258 if (unlikely(!bio_multiple_segments(bio))) {
259 *bv = bio_iovec(bio);
260 return;
261 }
262
263 bio_advance_iter(bio, &iter, iter.bi_size);
264
265 if (!iter.bi_bvec_done)
266 idx = iter.bi_idx - 1;
267 else /* in the middle of bvec */
268 idx = iter.bi_idx;
269
270 *bv = bio->bi_io_vec[idx];
271
272 /*
273 * iter.bi_bvec_done records actual length of the last bvec
274 * if this bio ends in the middle of one io vector
275 */
276 if (iter.bi_bvec_done)
277 bv->bv_len = iter.bi_bvec_done;
278 }
279
bio_first_bvec_all(struct bio * bio)280 static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
281 {
282 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
283 return bio->bi_io_vec;
284 }
285
bio_first_page_all(struct bio * bio)286 static inline struct page *bio_first_page_all(struct bio *bio)
287 {
288 return bio_first_bvec_all(bio)->bv_page;
289 }
290
bio_last_bvec_all(struct bio * bio)291 static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
292 {
293 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
294 return &bio->bi_io_vec[bio->bi_vcnt - 1];
295 }
296
297 enum bip_flags {
298 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
299 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
300 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
301 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
302 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
303 };
304
305 /*
306 * bio integrity payload
307 */
308 struct bio_integrity_payload {
309 struct bio *bip_bio; /* parent bio */
310
311 struct bvec_iter bip_iter;
312
313 unsigned short bip_slab; /* slab the bip came from */
314 unsigned short bip_vcnt; /* # of integrity bio_vecs */
315 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
316 unsigned short bip_flags; /* control flags */
317
318 struct bvec_iter bio_iter; /* for rewinding parent bio */
319
320 struct work_struct bip_work; /* I/O completion */
321
322 struct bio_vec *bip_vec;
323 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
324 };
325
326 #if defined(CONFIG_BLK_DEV_INTEGRITY)
327
bio_integrity(struct bio * bio)328 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
329 {
330 if (bio->bi_opf & REQ_INTEGRITY)
331 return bio->bi_integrity;
332
333 return NULL;
334 }
335
bio_integrity_flagged(struct bio * bio,enum bip_flags flag)336 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
337 {
338 struct bio_integrity_payload *bip = bio_integrity(bio);
339
340 if (bip)
341 return bip->bip_flags & flag;
342
343 return false;
344 }
345
bip_get_seed(struct bio_integrity_payload * bip)346 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
347 {
348 return bip->bip_iter.bi_sector;
349 }
350
bip_set_seed(struct bio_integrity_payload * bip,sector_t seed)351 static inline void bip_set_seed(struct bio_integrity_payload *bip,
352 sector_t seed)
353 {
354 bip->bip_iter.bi_sector = seed;
355 }
356
357 #endif /* CONFIG_BLK_DEV_INTEGRITY */
358
359 extern void bio_trim(struct bio *bio, int offset, int size);
360 extern struct bio *bio_split(struct bio *bio, int sectors,
361 gfp_t gfp, struct bio_set *bs);
362
363 /**
364 * bio_next_split - get next @sectors from a bio, splitting if necessary
365 * @bio: bio to split
366 * @sectors: number of sectors to split from the front of @bio
367 * @gfp: gfp mask
368 * @bs: bio set to allocate from
369 *
370 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
371 * than @sectors, returns the original bio unchanged.
372 */
bio_next_split(struct bio * bio,int sectors,gfp_t gfp,struct bio_set * bs)373 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
374 gfp_t gfp, struct bio_set *bs)
375 {
376 if (sectors >= bio_sectors(bio))
377 return bio;
378
379 return bio_split(bio, sectors, gfp, bs);
380 }
381
382 enum {
383 BIOSET_NEED_BVECS = BIT(0),
384 BIOSET_NEED_RESCUER = BIT(1),
385 };
386 extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
387 extern void bioset_exit(struct bio_set *);
388 extern int biovec_init_pool(mempool_t *pool, int pool_entries);
389 extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
390
391 extern struct bio *bio_alloc_bioset(gfp_t, unsigned int, struct bio_set *);
392 extern void bio_put(struct bio *);
393
394 extern void __bio_clone_fast(struct bio *, struct bio *);
395 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
396
397 extern struct bio_set fs_bio_set;
398
bio_alloc(gfp_t gfp_mask,unsigned int nr_iovecs)399 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
400 {
401 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
402 }
403
bio_kmalloc(gfp_t gfp_mask,unsigned int nr_iovecs)404 static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
405 {
406 return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
407 }
408
409 extern blk_qc_t submit_bio(struct bio *);
410
411 extern void bio_endio(struct bio *);
412
bio_io_error(struct bio * bio)413 static inline void bio_io_error(struct bio *bio)
414 {
415 bio->bi_status = BLK_STS_IOERR;
416 bio_endio(bio);
417 }
418
bio_wouldblock_error(struct bio * bio)419 static inline void bio_wouldblock_error(struct bio *bio)
420 {
421 bio->bi_status = BLK_STS_AGAIN;
422 bio_endio(bio);
423 }
424
425 struct request_queue;
426
427 extern int submit_bio_wait(struct bio *bio);
428 extern void bio_advance(struct bio *, unsigned);
429
430 extern void bio_init(struct bio *bio, struct bio_vec *table,
431 unsigned short max_vecs);
432 extern void bio_uninit(struct bio *);
433 extern void bio_reset(struct bio *);
434 void bio_chain(struct bio *, struct bio *);
435
436 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
437 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
438 unsigned int, unsigned int);
439 bool __bio_try_merge_page(struct bio *bio, struct page *page,
440 unsigned int len, unsigned int off, bool *same_page);
441 void __bio_add_page(struct bio *bio, struct page *page,
442 unsigned int len, unsigned int off);
443 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
444 void bio_release_pages(struct bio *bio, bool mark_dirty);
445 struct rq_map_data;
446 extern struct bio *bio_map_user_iov(struct request_queue *,
447 struct iov_iter *, gfp_t);
448 extern void bio_unmap_user(struct bio *);
449 extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
450 gfp_t);
451 extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
452 gfp_t, int);
453 extern void bio_set_pages_dirty(struct bio *bio);
454 extern void bio_check_pages_dirty(struct bio *bio);
455
456 void generic_start_io_acct(struct request_queue *q, int op,
457 unsigned long sectors, struct hd_struct *part);
458 void generic_end_io_acct(struct request_queue *q, int op,
459 struct hd_struct *part,
460 unsigned long start_time);
461
462 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
463 struct bio *src, struct bvec_iter *src_iter);
464 extern void bio_copy_data(struct bio *dst, struct bio *src);
465 extern void bio_list_copy_data(struct bio *dst, struct bio *src);
466 extern void bio_free_pages(struct bio *bio);
467
468 extern struct bio *bio_copy_user_iov(struct request_queue *,
469 struct rq_map_data *,
470 struct iov_iter *,
471 gfp_t);
472 extern int bio_uncopy_user(struct bio *);
473 void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
474 void bio_truncate(struct bio *bio, unsigned new_size);
475
zero_fill_bio(struct bio * bio)476 static inline void zero_fill_bio(struct bio *bio)
477 {
478 zero_fill_bio_iter(bio, bio->bi_iter);
479 }
480
481 extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
482 extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
483 extern unsigned int bvec_nr_vecs(unsigned short idx);
484 extern const char *bio_devname(struct bio *bio, char *buffer);
485
486 #define bio_set_dev(bio, bdev) \
487 do { \
488 if ((bio)->bi_disk != (bdev)->bd_disk) \
489 bio_clear_flag(bio, BIO_THROTTLED);\
490 (bio)->bi_disk = (bdev)->bd_disk; \
491 (bio)->bi_partno = (bdev)->bd_partno; \
492 bio_associate_blkg(bio); \
493 } while (0)
494
495 #define bio_copy_dev(dst, src) \
496 do { \
497 (dst)->bi_disk = (src)->bi_disk; \
498 (dst)->bi_partno = (src)->bi_partno; \
499 bio_clone_blkg_association(dst, src); \
500 } while (0)
501
502 #define bio_dev(bio) \
503 disk_devt((bio)->bi_disk)
504
505 #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
506 void bio_associate_blkg_from_page(struct bio *bio, struct page *page);
507 #else
bio_associate_blkg_from_page(struct bio * bio,struct page * page)508 static inline void bio_associate_blkg_from_page(struct bio *bio,
509 struct page *page) { }
510 #endif
511
512 #ifdef CONFIG_BLK_CGROUP
513 void bio_disassociate_blkg(struct bio *bio);
514 void bio_associate_blkg(struct bio *bio);
515 void bio_associate_blkg_from_css(struct bio *bio,
516 struct cgroup_subsys_state *css);
517 void bio_clone_blkg_association(struct bio *dst, struct bio *src);
518 #else /* CONFIG_BLK_CGROUP */
bio_disassociate_blkg(struct bio * bio)519 static inline void bio_disassociate_blkg(struct bio *bio) { }
bio_associate_blkg(struct bio * bio)520 static inline void bio_associate_blkg(struct bio *bio) { }
bio_associate_blkg_from_css(struct bio * bio,struct cgroup_subsys_state * css)521 static inline void bio_associate_blkg_from_css(struct bio *bio,
522 struct cgroup_subsys_state *css)
523 { }
bio_clone_blkg_association(struct bio * dst,struct bio * src)524 static inline void bio_clone_blkg_association(struct bio *dst,
525 struct bio *src) { }
526 #endif /* CONFIG_BLK_CGROUP */
527
528 #ifdef CONFIG_HIGHMEM
529 /*
530 * remember never ever reenable interrupts between a bvec_kmap_irq and
531 * bvec_kunmap_irq!
532 */
bvec_kmap_irq(struct bio_vec * bvec,unsigned long * flags)533 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
534 {
535 unsigned long addr;
536
537 /*
538 * might not be a highmem page, but the preempt/irq count
539 * balancing is a lot nicer this way
540 */
541 local_irq_save(*flags);
542 addr = (unsigned long) kmap_atomic(bvec->bv_page);
543
544 BUG_ON(addr & ~PAGE_MASK);
545
546 return (char *) addr + bvec->bv_offset;
547 }
548
bvec_kunmap_irq(char * buffer,unsigned long * flags)549 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
550 {
551 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
552
553 kunmap_atomic((void *) ptr);
554 local_irq_restore(*flags);
555 }
556
557 #else
bvec_kmap_irq(struct bio_vec * bvec,unsigned long * flags)558 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
559 {
560 return page_address(bvec->bv_page) + bvec->bv_offset;
561 }
562
bvec_kunmap_irq(char * buffer,unsigned long * flags)563 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
564 {
565 *flags = 0;
566 }
567 #endif
568
569 /*
570 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
571 *
572 * A bio_list anchors a singly-linked list of bios chained through the bi_next
573 * member of the bio. The bio_list also caches the last list member to allow
574 * fast access to the tail.
575 */
576 struct bio_list {
577 struct bio *head;
578 struct bio *tail;
579 };
580
bio_list_empty(const struct bio_list * bl)581 static inline int bio_list_empty(const struct bio_list *bl)
582 {
583 return bl->head == NULL;
584 }
585
bio_list_init(struct bio_list * bl)586 static inline void bio_list_init(struct bio_list *bl)
587 {
588 bl->head = bl->tail = NULL;
589 }
590
591 #define BIO_EMPTY_LIST { NULL, NULL }
592
593 #define bio_list_for_each(bio, bl) \
594 for (bio = (bl)->head; bio; bio = bio->bi_next)
595
bio_list_size(const struct bio_list * bl)596 static inline unsigned bio_list_size(const struct bio_list *bl)
597 {
598 unsigned sz = 0;
599 struct bio *bio;
600
601 bio_list_for_each(bio, bl)
602 sz++;
603
604 return sz;
605 }
606
bio_list_add(struct bio_list * bl,struct bio * bio)607 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
608 {
609 bio->bi_next = NULL;
610
611 if (bl->tail)
612 bl->tail->bi_next = bio;
613 else
614 bl->head = bio;
615
616 bl->tail = bio;
617 }
618
bio_list_add_head(struct bio_list * bl,struct bio * bio)619 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
620 {
621 bio->bi_next = bl->head;
622
623 bl->head = bio;
624
625 if (!bl->tail)
626 bl->tail = bio;
627 }
628
bio_list_merge(struct bio_list * bl,struct bio_list * bl2)629 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
630 {
631 if (!bl2->head)
632 return;
633
634 if (bl->tail)
635 bl->tail->bi_next = bl2->head;
636 else
637 bl->head = bl2->head;
638
639 bl->tail = bl2->tail;
640 }
641
bio_list_merge_head(struct bio_list * bl,struct bio_list * bl2)642 static inline void bio_list_merge_head(struct bio_list *bl,
643 struct bio_list *bl2)
644 {
645 if (!bl2->head)
646 return;
647
648 if (bl->head)
649 bl2->tail->bi_next = bl->head;
650 else
651 bl->tail = bl2->tail;
652
653 bl->head = bl2->head;
654 }
655
bio_list_peek(struct bio_list * bl)656 static inline struct bio *bio_list_peek(struct bio_list *bl)
657 {
658 return bl->head;
659 }
660
bio_list_pop(struct bio_list * bl)661 static inline struct bio *bio_list_pop(struct bio_list *bl)
662 {
663 struct bio *bio = bl->head;
664
665 if (bio) {
666 bl->head = bl->head->bi_next;
667 if (!bl->head)
668 bl->tail = NULL;
669
670 bio->bi_next = NULL;
671 }
672
673 return bio;
674 }
675
bio_list_get(struct bio_list * bl)676 static inline struct bio *bio_list_get(struct bio_list *bl)
677 {
678 struct bio *bio = bl->head;
679
680 bl->head = bl->tail = NULL;
681
682 return bio;
683 }
684
685 /*
686 * Increment chain count for the bio. Make sure the CHAIN flag update
687 * is visible before the raised count.
688 */
bio_inc_remaining(struct bio * bio)689 static inline void bio_inc_remaining(struct bio *bio)
690 {
691 bio_set_flag(bio, BIO_CHAIN);
692 smp_mb__before_atomic();
693 atomic_inc(&bio->__bi_remaining);
694 }
695
696 /*
697 * bio_set is used to allow other portions of the IO system to
698 * allocate their own private memory pools for bio and iovec structures.
699 * These memory pools in turn all allocate from the bio_slab
700 * and the bvec_slabs[].
701 */
702 #define BIO_POOL_SIZE 2
703
704 struct bio_set {
705 struct kmem_cache *bio_slab;
706 unsigned int front_pad;
707
708 mempool_t bio_pool;
709 mempool_t bvec_pool;
710 #if defined(CONFIG_BLK_DEV_INTEGRITY)
711 mempool_t bio_integrity_pool;
712 mempool_t bvec_integrity_pool;
713 #endif
714
715 /*
716 * Deadlock avoidance for stacking block drivers: see comments in
717 * bio_alloc_bioset() for details
718 */
719 spinlock_t rescue_lock;
720 struct bio_list rescue_list;
721 struct work_struct rescue_work;
722 struct workqueue_struct *rescue_workqueue;
723 };
724
725 struct biovec_slab {
726 int nr_vecs;
727 char *name;
728 struct kmem_cache *slab;
729 };
730
bioset_initialized(struct bio_set * bs)731 static inline bool bioset_initialized(struct bio_set *bs)
732 {
733 return bs->bio_slab != NULL;
734 }
735
736 /*
737 * a small number of entries is fine, not going to be performance critical.
738 * basically we just need to survive
739 */
740 #define BIO_SPLIT_ENTRIES 2
741
742 #if defined(CONFIG_BLK_DEV_INTEGRITY)
743
744 #define bip_for_each_vec(bvl, bip, iter) \
745 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
746
747 #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
748 for_each_bio(_bio) \
749 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
750
751 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
752 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
753 extern bool bio_integrity_prep(struct bio *);
754 extern void bio_integrity_advance(struct bio *, unsigned int);
755 extern void bio_integrity_trim(struct bio *);
756 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
757 extern int bioset_integrity_create(struct bio_set *, int);
758 extern void bioset_integrity_free(struct bio_set *);
759 extern void bio_integrity_init(void);
760
761 #else /* CONFIG_BLK_DEV_INTEGRITY */
762
bio_integrity(struct bio * bio)763 static inline void *bio_integrity(struct bio *bio)
764 {
765 return NULL;
766 }
767
bioset_integrity_create(struct bio_set * bs,int pool_size)768 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
769 {
770 return 0;
771 }
772
bioset_integrity_free(struct bio_set * bs)773 static inline void bioset_integrity_free (struct bio_set *bs)
774 {
775 return;
776 }
777
bio_integrity_prep(struct bio * bio)778 static inline bool bio_integrity_prep(struct bio *bio)
779 {
780 return true;
781 }
782
bio_integrity_clone(struct bio * bio,struct bio * bio_src,gfp_t gfp_mask)783 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
784 gfp_t gfp_mask)
785 {
786 return 0;
787 }
788
bio_integrity_advance(struct bio * bio,unsigned int bytes_done)789 static inline void bio_integrity_advance(struct bio *bio,
790 unsigned int bytes_done)
791 {
792 return;
793 }
794
bio_integrity_trim(struct bio * bio)795 static inline void bio_integrity_trim(struct bio *bio)
796 {
797 return;
798 }
799
bio_integrity_init(void)800 static inline void bio_integrity_init(void)
801 {
802 return;
803 }
804
bio_integrity_flagged(struct bio * bio,enum bip_flags flag)805 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
806 {
807 return false;
808 }
809
bio_integrity_alloc(struct bio * bio,gfp_t gfp,unsigned int nr)810 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
811 unsigned int nr)
812 {
813 return ERR_PTR(-EINVAL);
814 }
815
bio_integrity_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)816 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
817 unsigned int len, unsigned int offset)
818 {
819 return 0;
820 }
821
822 #endif /* CONFIG_BLK_DEV_INTEGRITY */
823
824 /*
825 * Mark a bio as polled. Note that for async polled IO, the caller must
826 * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
827 * We cannot block waiting for requests on polled IO, as those completions
828 * must be found by the caller. This is different than IRQ driven IO, where
829 * it's safe to wait for IO to complete.
830 */
bio_set_polled(struct bio * bio,struct kiocb * kiocb)831 static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
832 {
833 bio->bi_opf |= REQ_HIPRI;
834 if (!is_sync_kiocb(kiocb))
835 bio->bi_opf |= REQ_NOWAIT;
836 }
837
838 #endif /* CONFIG_BLOCK */
839 #endif /* __LINUX_BIO_H */
840