]>
Commit | Line | Data |
---|---|---|
8c16567d | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
0fe23479 | 3 | * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk> |
1da177e4 LT |
4 | */ |
5 | #include <linux/mm.h> | |
6 | #include <linux/swap.h> | |
7 | #include <linux/bio.h> | |
8 | #include <linux/blkdev.h> | |
a27bb332 | 9 | #include <linux/uio.h> |
852c788f | 10 | #include <linux/iocontext.h> |
1da177e4 LT |
11 | #include <linux/slab.h> |
12 | #include <linux/init.h> | |
13 | #include <linux/kernel.h> | |
630d9c47 | 14 | #include <linux/export.h> |
1da177e4 LT |
15 | #include <linux/mempool.h> |
16 | #include <linux/workqueue.h> | |
852c788f | 17 | #include <linux/cgroup.h> |
08e18eab | 18 | #include <linux/blk-cgroup.h> |
b4c5875d | 19 | #include <linux/highmem.h> |
de6a78b6 | 20 | #include <linux/sched/sysctl.h> |
a892c8d5 | 21 | #include <linux/blk-crypto.h> |
49d1ec85 | 22 | #include <linux/xarray.h> |
1da177e4 | 23 | |
55782138 | 24 | #include <trace/events/block.h> |
9e234eea | 25 | #include "blk.h" |
67b42d0b | 26 | #include "blk-rq-qos.h" |
0bfc2455 | 27 | |
1da177e4 LT |
28 | /* |
29 | * if you change this list, also change bvec_alloc or things will | |
30 | * break badly! cannot be bigger than what you can fit into an | |
31 | * unsigned short | |
32 | */ | |
bd5c4fac | 33 | #define BV(x, n) { .nr_vecs = x, .name = "biovec-"#n } |
ed996a52 | 34 | static struct biovec_slab bvec_slabs[BVEC_POOL_NR] __read_mostly = { |
bd5c4fac | 35 | BV(1, 1), BV(4, 4), BV(16, 16), BV(64, 64), BV(128, 128), BV(BIO_MAX_PAGES, max), |
1da177e4 LT |
36 | }; |
37 | #undef BV | |
38 | ||
1da177e4 LT |
39 | /* |
40 | * fs_bio_set is the bio_set containing bio and iovec memory pools used by | |
41 | * IO code that does not need private memory pools. | |
42 | */ | |
f4f8154a | 43 | struct bio_set fs_bio_set; |
3f86a82a | 44 | EXPORT_SYMBOL(fs_bio_set); |
1da177e4 | 45 | |
bb799ca0 JA |
46 | /* |
47 | * Our slab pool management | |
48 | */ | |
49 | struct bio_slab { | |
50 | struct kmem_cache *slab; | |
51 | unsigned int slab_ref; | |
52 | unsigned int slab_size; | |
53 | char name[8]; | |
54 | }; | |
55 | static DEFINE_MUTEX(bio_slab_lock); | |
49d1ec85 | 56 | static DEFINE_XARRAY(bio_slabs); |
bb799ca0 | 57 | |
49d1ec85 | 58 | static struct bio_slab *create_bio_slab(unsigned int size) |
bb799ca0 | 59 | { |
49d1ec85 | 60 | struct bio_slab *bslab = kzalloc(sizeof(*bslab), GFP_KERNEL); |
bb799ca0 | 61 | |
49d1ec85 ML |
62 | if (!bslab) |
63 | return NULL; | |
bb799ca0 | 64 | |
49d1ec85 ML |
65 | snprintf(bslab->name, sizeof(bslab->name), "bio-%d", size); |
66 | bslab->slab = kmem_cache_create(bslab->name, size, | |
67 | ARCH_KMALLOC_MINALIGN, SLAB_HWCACHE_ALIGN, NULL); | |
68 | if (!bslab->slab) | |
69 | goto fail_alloc_slab; | |
bb799ca0 | 70 | |
49d1ec85 ML |
71 | bslab->slab_ref = 1; |
72 | bslab->slab_size = size; | |
bb799ca0 | 73 | |
49d1ec85 ML |
74 | if (!xa_err(xa_store(&bio_slabs, size, bslab, GFP_KERNEL))) |
75 | return bslab; | |
bb799ca0 | 76 | |
49d1ec85 | 77 | kmem_cache_destroy(bslab->slab); |
bb799ca0 | 78 | |
49d1ec85 ML |
79 | fail_alloc_slab: |
80 | kfree(bslab); | |
81 | return NULL; | |
82 | } | |
bb799ca0 | 83 | |
49d1ec85 ML |
84 | static inline unsigned int bs_bio_slab_size(struct bio_set *bs) |
85 | { | |
9f180e31 | 86 | return bs->front_pad + sizeof(struct bio) + bs->back_pad; |
49d1ec85 ML |
87 | } |
88 | ||
89 | static struct kmem_cache *bio_find_or_create_slab(struct bio_set *bs) | |
90 | { | |
91 | unsigned int size = bs_bio_slab_size(bs); | |
92 | struct bio_slab *bslab; | |
93 | ||
94 | mutex_lock(&bio_slab_lock); | |
95 | bslab = xa_load(&bio_slabs, size); | |
96 | if (bslab) | |
97 | bslab->slab_ref++; | |
98 | else | |
99 | bslab = create_bio_slab(size); | |
bb799ca0 | 100 | mutex_unlock(&bio_slab_lock); |
49d1ec85 ML |
101 | |
102 | if (bslab) | |
103 | return bslab->slab; | |
104 | return NULL; | |
bb799ca0 JA |
105 | } |
106 | ||
107 | static void bio_put_slab(struct bio_set *bs) | |
108 | { | |
109 | struct bio_slab *bslab = NULL; | |
49d1ec85 | 110 | unsigned int slab_size = bs_bio_slab_size(bs); |
bb799ca0 JA |
111 | |
112 | mutex_lock(&bio_slab_lock); | |
113 | ||
49d1ec85 | 114 | bslab = xa_load(&bio_slabs, slab_size); |
bb799ca0 JA |
115 | if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n")) |
116 | goto out; | |
117 | ||
49d1ec85 ML |
118 | WARN_ON_ONCE(bslab->slab != bs->bio_slab); |
119 | ||
bb799ca0 JA |
120 | WARN_ON(!bslab->slab_ref); |
121 | ||
122 | if (--bslab->slab_ref) | |
123 | goto out; | |
124 | ||
49d1ec85 ML |
125 | xa_erase(&bio_slabs, slab_size); |
126 | ||
bb799ca0 | 127 | kmem_cache_destroy(bslab->slab); |
49d1ec85 | 128 | kfree(bslab); |
bb799ca0 JA |
129 | |
130 | out: | |
131 | mutex_unlock(&bio_slab_lock); | |
132 | } | |
133 | ||
7ba1ba12 MP |
134 | unsigned int bvec_nr_vecs(unsigned short idx) |
135 | { | |
d6c02a9b | 136 | return bvec_slabs[--idx].nr_vecs; |
7ba1ba12 MP |
137 | } |
138 | ||
9f060e22 | 139 | void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx) |
bb799ca0 | 140 | { |
ed996a52 CH |
141 | if (!idx) |
142 | return; | |
143 | idx--; | |
144 | ||
145 | BIO_BUG_ON(idx >= BVEC_POOL_NR); | |
bb799ca0 | 146 | |
ed996a52 | 147 | if (idx == BVEC_POOL_MAX) { |
9f060e22 | 148 | mempool_free(bv, pool); |
ed996a52 | 149 | } else { |
bb799ca0 JA |
150 | struct biovec_slab *bvs = bvec_slabs + idx; |
151 | ||
152 | kmem_cache_free(bvs->slab, bv); | |
153 | } | |
154 | } | |
155 | ||
9f060e22 KO |
156 | struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx, |
157 | mempool_t *pool) | |
1da177e4 LT |
158 | { |
159 | struct bio_vec *bvl; | |
1da177e4 | 160 | |
7ff9345f JA |
161 | /* |
162 | * see comment near bvec_array define! | |
163 | */ | |
164 | switch (nr) { | |
165 | case 1: | |
166 | *idx = 0; | |
167 | break; | |
168 | case 2 ... 4: | |
169 | *idx = 1; | |
170 | break; | |
171 | case 5 ... 16: | |
172 | *idx = 2; | |
173 | break; | |
174 | case 17 ... 64: | |
175 | *idx = 3; | |
176 | break; | |
177 | case 65 ... 128: | |
178 | *idx = 4; | |
179 | break; | |
180 | case 129 ... BIO_MAX_PAGES: | |
181 | *idx = 5; | |
182 | break; | |
183 | default: | |
184 | return NULL; | |
185 | } | |
186 | ||
187 | /* | |
188 | * idx now points to the pool we want to allocate from. only the | |
189 | * 1-vec entry pool is mempool backed. | |
190 | */ | |
ed996a52 | 191 | if (*idx == BVEC_POOL_MAX) { |
7ff9345f | 192 | fallback: |
9f060e22 | 193 | bvl = mempool_alloc(pool, gfp_mask); |
7ff9345f JA |
194 | } else { |
195 | struct biovec_slab *bvs = bvec_slabs + *idx; | |
d0164adc | 196 | gfp_t __gfp_mask = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO); |
7ff9345f | 197 | |
0a0d96b0 | 198 | /* |
7ff9345f JA |
199 | * Make this allocation restricted and don't dump info on |
200 | * allocation failures, since we'll fallback to the mempool | |
201 | * in case of failure. | |
0a0d96b0 | 202 | */ |
7ff9345f | 203 | __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; |
1da177e4 | 204 | |
0a0d96b0 | 205 | /* |
d0164adc | 206 | * Try a slab allocation. If this fails and __GFP_DIRECT_RECLAIM |
7ff9345f | 207 | * is set, retry with the 1-entry mempool |
0a0d96b0 | 208 | */ |
7ff9345f | 209 | bvl = kmem_cache_alloc(bvs->slab, __gfp_mask); |
d0164adc | 210 | if (unlikely(!bvl && (gfp_mask & __GFP_DIRECT_RECLAIM))) { |
ed996a52 | 211 | *idx = BVEC_POOL_MAX; |
7ff9345f JA |
212 | goto fallback; |
213 | } | |
214 | } | |
215 | ||
ed996a52 | 216 | (*idx)++; |
1da177e4 LT |
217 | return bvl; |
218 | } | |
219 | ||
9ae3b3f5 | 220 | void bio_uninit(struct bio *bio) |
1da177e4 | 221 | { |
db9819c7 CH |
222 | #ifdef CONFIG_BLK_CGROUP |
223 | if (bio->bi_blkg) { | |
224 | blkg_put(bio->bi_blkg); | |
225 | bio->bi_blkg = NULL; | |
226 | } | |
227 | #endif | |
ece841ab JT |
228 | if (bio_integrity(bio)) |
229 | bio_integrity_free(bio); | |
a892c8d5 ST |
230 | |
231 | bio_crypt_free_ctx(bio); | |
4254bba1 | 232 | } |
9ae3b3f5 | 233 | EXPORT_SYMBOL(bio_uninit); |
7ba1ba12 | 234 | |
4254bba1 KO |
235 | static void bio_free(struct bio *bio) |
236 | { | |
237 | struct bio_set *bs = bio->bi_pool; | |
238 | void *p; | |
239 | ||
9ae3b3f5 | 240 | bio_uninit(bio); |
4254bba1 KO |
241 | |
242 | if (bs) { | |
8aa6ba2f | 243 | bvec_free(&bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio)); |
4254bba1 KO |
244 | |
245 | /* | |
246 | * If we have front padding, adjust the bio pointer before freeing | |
247 | */ | |
248 | p = bio; | |
bb799ca0 JA |
249 | p -= bs->front_pad; |
250 | ||
8aa6ba2f | 251 | mempool_free(p, &bs->bio_pool); |
4254bba1 KO |
252 | } else { |
253 | /* Bio was allocated by bio_kmalloc() */ | |
254 | kfree(bio); | |
255 | } | |
3676347a PO |
256 | } |
257 | ||
9ae3b3f5 JA |
258 | /* |
259 | * Users of this function have their own bio allocation. Subsequently, | |
260 | * they must remember to pair any call to bio_init() with bio_uninit() | |
261 | * when IO has completed, or when the bio is released. | |
262 | */ | |
3a83f467 ML |
263 | void bio_init(struct bio *bio, struct bio_vec *table, |
264 | unsigned short max_vecs) | |
1da177e4 | 265 | { |
2b94de55 | 266 | memset(bio, 0, sizeof(*bio)); |
c4cf5261 | 267 | atomic_set(&bio->__bi_remaining, 1); |
dac56212 | 268 | atomic_set(&bio->__bi_cnt, 1); |
3a83f467 ML |
269 | |
270 | bio->bi_io_vec = table; | |
271 | bio->bi_max_vecs = max_vecs; | |
1da177e4 | 272 | } |
a112a71d | 273 | EXPORT_SYMBOL(bio_init); |
1da177e4 | 274 | |
f44b48c7 KO |
275 | /** |
276 | * bio_reset - reinitialize a bio | |
277 | * @bio: bio to reset | |
278 | * | |
279 | * Description: | |
280 | * After calling bio_reset(), @bio will be in the same state as a freshly | |
281 | * allocated bio returned bio bio_alloc_bioset() - the only fields that are | |
282 | * preserved are the ones that are initialized by bio_alloc_bioset(). See | |
283 | * comment in struct bio. | |
284 | */ | |
285 | void bio_reset(struct bio *bio) | |
286 | { | |
287 | unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS); | |
288 | ||
9ae3b3f5 | 289 | bio_uninit(bio); |
f44b48c7 KO |
290 | |
291 | memset(bio, 0, BIO_RESET_BYTES); | |
4246a0b6 | 292 | bio->bi_flags = flags; |
c4cf5261 | 293 | atomic_set(&bio->__bi_remaining, 1); |
f44b48c7 KO |
294 | } |
295 | EXPORT_SYMBOL(bio_reset); | |
296 | ||
38f8baae | 297 | static struct bio *__bio_chain_endio(struct bio *bio) |
196d38bc | 298 | { |
4246a0b6 CH |
299 | struct bio *parent = bio->bi_private; |
300 | ||
4e4cbee9 CH |
301 | if (!parent->bi_status) |
302 | parent->bi_status = bio->bi_status; | |
196d38bc | 303 | bio_put(bio); |
38f8baae CH |
304 | return parent; |
305 | } | |
306 | ||
307 | static void bio_chain_endio(struct bio *bio) | |
308 | { | |
309 | bio_endio(__bio_chain_endio(bio)); | |
196d38bc KO |
310 | } |
311 | ||
312 | /** | |
313 | * bio_chain - chain bio completions | |
1051a902 | 314 | * @bio: the target bio |
5b874af6 | 315 | * @parent: the parent bio of @bio |
196d38bc KO |
316 | * |
317 | * The caller won't have a bi_end_io called when @bio completes - instead, | |
318 | * @parent's bi_end_io won't be called until both @parent and @bio have | |
319 | * completed; the chained bio will also be freed when it completes. | |
320 | * | |
321 | * The caller must not set bi_private or bi_end_io in @bio. | |
322 | */ | |
323 | void bio_chain(struct bio *bio, struct bio *parent) | |
324 | { | |
325 | BUG_ON(bio->bi_private || bio->bi_end_io); | |
326 | ||
327 | bio->bi_private = parent; | |
328 | bio->bi_end_io = bio_chain_endio; | |
c4cf5261 | 329 | bio_inc_remaining(parent); |
196d38bc KO |
330 | } |
331 | EXPORT_SYMBOL(bio_chain); | |
332 | ||
df2cb6da KO |
333 | static void bio_alloc_rescue(struct work_struct *work) |
334 | { | |
335 | struct bio_set *bs = container_of(work, struct bio_set, rescue_work); | |
336 | struct bio *bio; | |
337 | ||
338 | while (1) { | |
339 | spin_lock(&bs->rescue_lock); | |
340 | bio = bio_list_pop(&bs->rescue_list); | |
341 | spin_unlock(&bs->rescue_lock); | |
342 | ||
343 | if (!bio) | |
344 | break; | |
345 | ||
ed00aabd | 346 | submit_bio_noacct(bio); |
df2cb6da KO |
347 | } |
348 | } | |
349 | ||
350 | static void punt_bios_to_rescuer(struct bio_set *bs) | |
351 | { | |
352 | struct bio_list punt, nopunt; | |
353 | struct bio *bio; | |
354 | ||
47e0fb46 N |
355 | if (WARN_ON_ONCE(!bs->rescue_workqueue)) |
356 | return; | |
df2cb6da KO |
357 | /* |
358 | * In order to guarantee forward progress we must punt only bios that | |
359 | * were allocated from this bio_set; otherwise, if there was a bio on | |
360 | * there for a stacking driver higher up in the stack, processing it | |
361 | * could require allocating bios from this bio_set, and doing that from | |
362 | * our own rescuer would be bad. | |
363 | * | |
364 | * Since bio lists are singly linked, pop them all instead of trying to | |
365 | * remove from the middle of the list: | |
366 | */ | |
367 | ||
368 | bio_list_init(&punt); | |
369 | bio_list_init(&nopunt); | |
370 | ||
f5fe1b51 | 371 | while ((bio = bio_list_pop(¤t->bio_list[0]))) |
df2cb6da | 372 | bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio); |
f5fe1b51 | 373 | current->bio_list[0] = nopunt; |
df2cb6da | 374 | |
f5fe1b51 N |
375 | bio_list_init(&nopunt); |
376 | while ((bio = bio_list_pop(¤t->bio_list[1]))) | |
377 | bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio); | |
378 | current->bio_list[1] = nopunt; | |
df2cb6da KO |
379 | |
380 | spin_lock(&bs->rescue_lock); | |
381 | bio_list_merge(&bs->rescue_list, &punt); | |
382 | spin_unlock(&bs->rescue_lock); | |
383 | ||
384 | queue_work(bs->rescue_workqueue, &bs->rescue_work); | |
385 | } | |
386 | ||
1da177e4 LT |
387 | /** |
388 | * bio_alloc_bioset - allocate a bio for I/O | |
519c8e9f | 389 | * @gfp_mask: the GFP_* mask given to the slab allocator |
1da177e4 | 390 | * @nr_iovecs: number of iovecs to pre-allocate |
db18efac | 391 | * @bs: the bio_set to allocate from. |
1da177e4 | 392 | * |
3175199a | 393 | * Allocate a bio from the mempools in @bs. |
3f86a82a | 394 | * |
3175199a CH |
395 | * If %__GFP_DIRECT_RECLAIM is set then bio_alloc will always be able to |
396 | * allocate a bio. This is due to the mempool guarantees. To make this work, | |
397 | * callers must never allocate more than 1 bio at a time from the general pool. | |
398 | * Callers that need to allocate more than 1 bio must always submit the | |
399 | * previously allocated bio for IO before attempting to allocate a new one. | |
400 | * Failure to do so can cause deadlocks under memory pressure. | |
3f86a82a | 401 | * |
3175199a CH |
402 | * Note that when running under submit_bio_noacct() (i.e. any block driver), |
403 | * bios are not submitted until after you return - see the code in | |
404 | * submit_bio_noacct() that converts recursion into iteration, to prevent | |
405 | * stack overflows. | |
df2cb6da | 406 | * |
3175199a CH |
407 | * This would normally mean allocating multiple bios under submit_bio_noacct() |
408 | * would be susceptible to deadlocks, but we have | |
409 | * deadlock avoidance code that resubmits any blocked bios from a rescuer | |
410 | * thread. | |
df2cb6da | 411 | * |
3175199a CH |
412 | * However, we do not guarantee forward progress for allocations from other |
413 | * mempools. Doing multiple allocations from the same mempool under | |
414 | * submit_bio_noacct() should be avoided - instead, use bio_set's front_pad | |
415 | * for per bio allocations. | |
df2cb6da | 416 | * |
3175199a | 417 | * Returns: Pointer to new bio on success, NULL on failure. |
3f86a82a | 418 | */ |
7a88fa19 DC |
419 | struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs, |
420 | struct bio_set *bs) | |
1da177e4 | 421 | { |
df2cb6da | 422 | gfp_t saved_gfp = gfp_mask; |
451a9ebf TH |
423 | struct bio *bio; |
424 | void *p; | |
425 | ||
3175199a CH |
426 | /* should not use nobvec bioset for nr_iovecs > 0 */ |
427 | if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && nr_iovecs > 0)) | |
428 | return NULL; | |
df2cb6da | 429 | |
3175199a CH |
430 | /* |
431 | * submit_bio_noacct() converts recursion to iteration; this means if | |
432 | * we're running beneath it, any bios we allocate and submit will not be | |
433 | * submitted (and thus freed) until after we return. | |
434 | * | |
435 | * This exposes us to a potential deadlock if we allocate multiple bios | |
436 | * from the same bio_set() while running underneath submit_bio_noacct(). | |
437 | * If we were to allocate multiple bios (say a stacking block driver | |
438 | * that was splitting bios), we would deadlock if we exhausted the | |
439 | * mempool's reserve. | |
440 | * | |
441 | * We solve this, and guarantee forward progress, with a rescuer | |
442 | * workqueue per bio_set. If we go to allocate and there are bios on | |
443 | * current->bio_list, we first try the allocation without | |
444 | * __GFP_DIRECT_RECLAIM; if that fails, we punt those bios we would be | |
445 | * blocking to the rescuer workqueue before we retry with the original | |
446 | * gfp_flags. | |
447 | */ | |
448 | if (current->bio_list && | |
449 | (!bio_list_empty(¤t->bio_list[0]) || | |
450 | !bio_list_empty(¤t->bio_list[1])) && | |
451 | bs->rescue_workqueue) | |
452 | gfp_mask &= ~__GFP_DIRECT_RECLAIM; | |
453 | ||
454 | p = mempool_alloc(&bs->bio_pool, gfp_mask); | |
455 | if (!p && gfp_mask != saved_gfp) { | |
456 | punt_bios_to_rescuer(bs); | |
457 | gfp_mask = saved_gfp; | |
8aa6ba2f | 458 | p = mempool_alloc(&bs->bio_pool, gfp_mask); |
3f86a82a | 459 | } |
451a9ebf TH |
460 | if (unlikely(!p)) |
461 | return NULL; | |
1da177e4 | 462 | |
3175199a CH |
463 | bio = p + bs->front_pad; |
464 | if (nr_iovecs > BIO_INLINE_VECS) { | |
ed996a52 | 465 | unsigned long idx = 0; |
3175199a | 466 | struct bio_vec *bvl = NULL; |
ed996a52 | 467 | |
8aa6ba2f | 468 | bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool); |
df2cb6da KO |
469 | if (!bvl && gfp_mask != saved_gfp) { |
470 | punt_bios_to_rescuer(bs); | |
471 | gfp_mask = saved_gfp; | |
3175199a CH |
472 | bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, |
473 | &bs->bvec_pool); | |
df2cb6da KO |
474 | } |
475 | ||
34053979 IM |
476 | if (unlikely(!bvl)) |
477 | goto err_free; | |
a38352e0 | 478 | |
3175199a | 479 | bio_init(bio, bvl, bvec_nr_vecs(idx)); |
8358c28a | 480 | bio->bi_flags |= idx << BVEC_POOL_OFFSET; |
3f86a82a | 481 | } else if (nr_iovecs) { |
3175199a CH |
482 | bio_init(bio, bio->bi_inline_vecs, BIO_INLINE_VECS); |
483 | } else { | |
484 | bio_init(bio, NULL, 0); | |
1da177e4 | 485 | } |
3f86a82a KO |
486 | |
487 | bio->bi_pool = bs; | |
1da177e4 | 488 | return bio; |
34053979 IM |
489 | |
490 | err_free: | |
8aa6ba2f | 491 | mempool_free(p, &bs->bio_pool); |
34053979 | 492 | return NULL; |
1da177e4 | 493 | } |
a112a71d | 494 | EXPORT_SYMBOL(bio_alloc_bioset); |
1da177e4 | 495 | |
3175199a CH |
496 | /** |
497 | * bio_kmalloc - kmalloc a bio for I/O | |
498 | * @gfp_mask: the GFP_* mask given to the slab allocator | |
499 | * @nr_iovecs: number of iovecs to pre-allocate | |
500 | * | |
501 | * Use kmalloc to allocate and initialize a bio. | |
502 | * | |
503 | * Returns: Pointer to new bio on success, NULL on failure. | |
504 | */ | |
505 | struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs) | |
506 | { | |
507 | struct bio *bio; | |
508 | ||
509 | if (nr_iovecs > UIO_MAXIOV) | |
510 | return NULL; | |
511 | ||
512 | bio = kmalloc(struct_size(bio, bi_inline_vecs, nr_iovecs), gfp_mask); | |
513 | if (unlikely(!bio)) | |
514 | return NULL; | |
515 | bio_init(bio, nr_iovecs ? bio->bi_inline_vecs : NULL, nr_iovecs); | |
516 | bio->bi_pool = NULL; | |
517 | return bio; | |
518 | } | |
519 | EXPORT_SYMBOL(bio_kmalloc); | |
520 | ||
38a72dac | 521 | void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start) |
1da177e4 LT |
522 | { |
523 | unsigned long flags; | |
7988613b KO |
524 | struct bio_vec bv; |
525 | struct bvec_iter iter; | |
1da177e4 | 526 | |
38a72dac | 527 | __bio_for_each_segment(bv, bio, iter, start) { |
7988613b KO |
528 | char *data = bvec_kmap_irq(&bv, &flags); |
529 | memset(data, 0, bv.bv_len); | |
530 | flush_dcache_page(bv.bv_page); | |
1da177e4 LT |
531 | bvec_kunmap_irq(data, &flags); |
532 | } | |
533 | } | |
38a72dac | 534 | EXPORT_SYMBOL(zero_fill_bio_iter); |
1da177e4 | 535 | |
83c9c547 ML |
536 | /** |
537 | * bio_truncate - truncate the bio to small size of @new_size | |
538 | * @bio: the bio to be truncated | |
539 | * @new_size: new size for truncating the bio | |
540 | * | |
541 | * Description: | |
542 | * Truncate the bio to new size of @new_size. If bio_op(bio) is | |
543 | * REQ_OP_READ, zero the truncated part. This function should only | |
544 | * be used for handling corner cases, such as bio eod. | |
545 | */ | |
85a8ce62 ML |
546 | void bio_truncate(struct bio *bio, unsigned new_size) |
547 | { | |
548 | struct bio_vec bv; | |
549 | struct bvec_iter iter; | |
550 | unsigned int done = 0; | |
551 | bool truncated = false; | |
552 | ||
553 | if (new_size >= bio->bi_iter.bi_size) | |
554 | return; | |
555 | ||
83c9c547 | 556 | if (bio_op(bio) != REQ_OP_READ) |
85a8ce62 ML |
557 | goto exit; |
558 | ||
559 | bio_for_each_segment(bv, bio, iter) { | |
560 | if (done + bv.bv_len > new_size) { | |
561 | unsigned offset; | |
562 | ||
563 | if (!truncated) | |
564 | offset = new_size - done; | |
565 | else | |
566 | offset = 0; | |
567 | zero_user(bv.bv_page, offset, bv.bv_len - offset); | |
568 | truncated = true; | |
569 | } | |
570 | done += bv.bv_len; | |
571 | } | |
572 | ||
573 | exit: | |
574 | /* | |
575 | * Don't touch bvec table here and make it really immutable, since | |
576 | * fs bio user has to retrieve all pages via bio_for_each_segment_all | |
577 | * in its .end_bio() callback. | |
578 | * | |
579 | * It is enough to truncate bio by updating .bi_size since we can make | |
580 | * correct bvec with the updated .bi_size for drivers. | |
581 | */ | |
582 | bio->bi_iter.bi_size = new_size; | |
583 | } | |
584 | ||
29125ed6 CH |
585 | /** |
586 | * guard_bio_eod - truncate a BIO to fit the block device | |
587 | * @bio: bio to truncate | |
588 | * | |
589 | * This allows us to do IO even on the odd last sectors of a device, even if the | |
590 | * block size is some multiple of the physical sector size. | |
591 | * | |
592 | * We'll just truncate the bio to the size of the device, and clear the end of | |
593 | * the buffer head manually. Truly out-of-range accesses will turn into actual | |
594 | * I/O errors, this only handles the "we need to be able to do I/O at the final | |
595 | * sector" case. | |
596 | */ | |
597 | void guard_bio_eod(struct bio *bio) | |
598 | { | |
309dca30 | 599 | sector_t maxsector = bdev_nr_sectors(bio->bi_bdev); |
29125ed6 CH |
600 | |
601 | if (!maxsector) | |
602 | return; | |
603 | ||
604 | /* | |
605 | * If the *whole* IO is past the end of the device, | |
606 | * let it through, and the IO layer will turn it into | |
607 | * an EIO. | |
608 | */ | |
609 | if (unlikely(bio->bi_iter.bi_sector >= maxsector)) | |
610 | return; | |
611 | ||
612 | maxsector -= bio->bi_iter.bi_sector; | |
613 | if (likely((bio->bi_iter.bi_size >> 9) <= maxsector)) | |
614 | return; | |
615 | ||
616 | bio_truncate(bio, maxsector << 9); | |
617 | } | |
618 | ||
1da177e4 LT |
619 | /** |
620 | * bio_put - release a reference to a bio | |
621 | * @bio: bio to release reference to | |
622 | * | |
623 | * Description: | |
624 | * Put a reference to a &struct bio, either one you have gotten with | |
9b10f6a9 | 625 | * bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it. |
1da177e4 LT |
626 | **/ |
627 | void bio_put(struct bio *bio) | |
628 | { | |
dac56212 | 629 | if (!bio_flagged(bio, BIO_REFFED)) |
4254bba1 | 630 | bio_free(bio); |
dac56212 JA |
631 | else { |
632 | BIO_BUG_ON(!atomic_read(&bio->__bi_cnt)); | |
633 | ||
634 | /* | |
635 | * last put frees it | |
636 | */ | |
637 | if (atomic_dec_and_test(&bio->__bi_cnt)) | |
638 | bio_free(bio); | |
639 | } | |
1da177e4 | 640 | } |
a112a71d | 641 | EXPORT_SYMBOL(bio_put); |
1da177e4 | 642 | |
59d276fe KO |
643 | /** |
644 | * __bio_clone_fast - clone a bio that shares the original bio's biovec | |
645 | * @bio: destination bio | |
646 | * @bio_src: bio to clone | |
647 | * | |
648 | * Clone a &bio. Caller will own the returned bio, but not | |
649 | * the actual data it points to. Reference count of returned | |
650 | * bio will be one. | |
651 | * | |
652 | * Caller must ensure that @bio_src is not freed before @bio. | |
653 | */ | |
654 | void __bio_clone_fast(struct bio *bio, struct bio *bio_src) | |
655 | { | |
ed996a52 | 656 | BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio)); |
59d276fe KO |
657 | |
658 | /* | |
309dca30 | 659 | * most users will be overriding ->bi_bdev with a new target, |
59d276fe KO |
660 | * so we don't set nor calculate new physical/hw segment counts here |
661 | */ | |
309dca30 | 662 | bio->bi_bdev = bio_src->bi_bdev; |
b7c44ed9 | 663 | bio_set_flag(bio, BIO_CLONED); |
111be883 SL |
664 | if (bio_flagged(bio_src, BIO_THROTTLED)) |
665 | bio_set_flag(bio, BIO_THROTTLED); | |
46bbf653 CH |
666 | if (bio_flagged(bio_src, BIO_REMAPPED)) |
667 | bio_set_flag(bio, BIO_REMAPPED); | |
1eff9d32 | 668 | bio->bi_opf = bio_src->bi_opf; |
ca474b73 | 669 | bio->bi_ioprio = bio_src->bi_ioprio; |
cb6934f8 | 670 | bio->bi_write_hint = bio_src->bi_write_hint; |
59d276fe KO |
671 | bio->bi_iter = bio_src->bi_iter; |
672 | bio->bi_io_vec = bio_src->bi_io_vec; | |
20bd723e | 673 | |
db6638d7 | 674 | bio_clone_blkg_association(bio, bio_src); |
e439bedf | 675 | blkcg_bio_issue_init(bio); |
59d276fe KO |
676 | } |
677 | EXPORT_SYMBOL(__bio_clone_fast); | |
678 | ||
679 | /** | |
680 | * bio_clone_fast - clone a bio that shares the original bio's biovec | |
681 | * @bio: bio to clone | |
682 | * @gfp_mask: allocation priority | |
683 | * @bs: bio_set to allocate from | |
684 | * | |
685 | * Like __bio_clone_fast, only also allocates the returned bio | |
686 | */ | |
687 | struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs) | |
688 | { | |
689 | struct bio *b; | |
690 | ||
691 | b = bio_alloc_bioset(gfp_mask, 0, bs); | |
692 | if (!b) | |
693 | return NULL; | |
694 | ||
695 | __bio_clone_fast(b, bio); | |
696 | ||
07560151 EB |
697 | if (bio_crypt_clone(b, bio, gfp_mask) < 0) |
698 | goto err_put; | |
a892c8d5 | 699 | |
07560151 EB |
700 | if (bio_integrity(bio) && |
701 | bio_integrity_clone(b, bio, gfp_mask) < 0) | |
702 | goto err_put; | |
59d276fe KO |
703 | |
704 | return b; | |
07560151 EB |
705 | |
706 | err_put: | |
707 | bio_put(b); | |
708 | return NULL; | |
59d276fe KO |
709 | } |
710 | EXPORT_SYMBOL(bio_clone_fast); | |
711 | ||
5cbd28e3 CH |
712 | const char *bio_devname(struct bio *bio, char *buf) |
713 | { | |
309dca30 | 714 | return bdevname(bio->bi_bdev, buf); |
5cbd28e3 CH |
715 | } |
716 | EXPORT_SYMBOL(bio_devname); | |
717 | ||
5919482e ML |
718 | static inline bool page_is_mergeable(const struct bio_vec *bv, |
719 | struct page *page, unsigned int len, unsigned int off, | |
ff896738 | 720 | bool *same_page) |
5919482e | 721 | { |
d8166519 MWO |
722 | size_t bv_end = bv->bv_offset + bv->bv_len; |
723 | phys_addr_t vec_end_addr = page_to_phys(bv->bv_page) + bv_end - 1; | |
5919482e ML |
724 | phys_addr_t page_addr = page_to_phys(page); |
725 | ||
726 | if (vec_end_addr + 1 != page_addr + off) | |
727 | return false; | |
728 | if (xen_domain() && !xen_biovec_phys_mergeable(bv, page)) | |
729 | return false; | |
52d52d1c | 730 | |
ff896738 | 731 | *same_page = ((vec_end_addr & PAGE_MASK) == page_addr); |
d8166519 MWO |
732 | if (*same_page) |
733 | return true; | |
734 | return (bv->bv_page + bv_end / PAGE_SIZE) == (page + off / PAGE_SIZE); | |
5919482e ML |
735 | } |
736 | ||
e4581105 CH |
737 | /* |
738 | * Try to merge a page into a segment, while obeying the hardware segment | |
739 | * size limit. This is not for normal read/write bios, but for passthrough | |
740 | * or Zone Append operations that we can't split. | |
741 | */ | |
742 | static bool bio_try_merge_hw_seg(struct request_queue *q, struct bio *bio, | |
743 | struct page *page, unsigned len, | |
744 | unsigned offset, bool *same_page) | |
489fbbcb | 745 | { |
384209cd | 746 | struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
489fbbcb ML |
747 | unsigned long mask = queue_segment_boundary(q); |
748 | phys_addr_t addr1 = page_to_phys(bv->bv_page) + bv->bv_offset; | |
749 | phys_addr_t addr2 = page_to_phys(page) + offset + len - 1; | |
750 | ||
751 | if ((addr1 | mask) != (addr2 | mask)) | |
752 | return false; | |
489fbbcb ML |
753 | if (bv->bv_len + len > queue_max_segment_size(q)) |
754 | return false; | |
384209cd | 755 | return __bio_try_merge_page(bio, page, len, offset, same_page); |
489fbbcb ML |
756 | } |
757 | ||
1da177e4 | 758 | /** |
e4581105 CH |
759 | * bio_add_hw_page - attempt to add a page to a bio with hw constraints |
760 | * @q: the target queue | |
761 | * @bio: destination bio | |
762 | * @page: page to add | |
763 | * @len: vec entry length | |
764 | * @offset: vec entry offset | |
765 | * @max_sectors: maximum number of sectors that can be added | |
766 | * @same_page: return if the segment has been merged inside the same page | |
c66a14d0 | 767 | * |
e4581105 CH |
768 | * Add a page to a bio while respecting the hardware max_sectors, max_segment |
769 | * and gap limitations. | |
1da177e4 | 770 | */ |
e4581105 | 771 | int bio_add_hw_page(struct request_queue *q, struct bio *bio, |
19047087 | 772 | struct page *page, unsigned int len, unsigned int offset, |
e4581105 | 773 | unsigned int max_sectors, bool *same_page) |
1da177e4 | 774 | { |
1da177e4 LT |
775 | struct bio_vec *bvec; |
776 | ||
e4581105 | 777 | if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED))) |
1da177e4 LT |
778 | return 0; |
779 | ||
e4581105 | 780 | if (((bio->bi_iter.bi_size + len) >> 9) > max_sectors) |
1da177e4 LT |
781 | return 0; |
782 | ||
80cfd548 | 783 | if (bio->bi_vcnt > 0) { |
e4581105 | 784 | if (bio_try_merge_hw_seg(q, bio, page, len, offset, same_page)) |
384209cd | 785 | return len; |
320ea869 CH |
786 | |
787 | /* | |
788 | * If the queue doesn't support SG gaps and adding this segment | |
789 | * would create a gap, disallow it. | |
790 | */ | |
384209cd | 791 | bvec = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
320ea869 CH |
792 | if (bvec_gap_to_prev(q, bvec, offset)) |
793 | return 0; | |
80cfd548 JA |
794 | } |
795 | ||
79d08f89 | 796 | if (bio_full(bio, len)) |
1da177e4 LT |
797 | return 0; |
798 | ||
14ccb66b | 799 | if (bio->bi_vcnt >= queue_max_segments(q)) |
489fbbcb ML |
800 | return 0; |
801 | ||
fcbf6a08 ML |
802 | bvec = &bio->bi_io_vec[bio->bi_vcnt]; |
803 | bvec->bv_page = page; | |
804 | bvec->bv_len = len; | |
805 | bvec->bv_offset = offset; | |
806 | bio->bi_vcnt++; | |
dcdca753 | 807 | bio->bi_iter.bi_size += len; |
1da177e4 LT |
808 | return len; |
809 | } | |
19047087 | 810 | |
e4581105 CH |
811 | /** |
812 | * bio_add_pc_page - attempt to add page to passthrough bio | |
813 | * @q: the target queue | |
814 | * @bio: destination bio | |
815 | * @page: page to add | |
816 | * @len: vec entry length | |
817 | * @offset: vec entry offset | |
818 | * | |
819 | * Attempt to add a page to the bio_vec maplist. This can fail for a | |
820 | * number of reasons, such as the bio being full or target block device | |
821 | * limitations. The target block device must allow bio's up to PAGE_SIZE, | |
822 | * so it is always possible to add a single page to an empty bio. | |
823 | * | |
824 | * This should only be used by passthrough bios. | |
825 | */ | |
19047087 ML |
826 | int bio_add_pc_page(struct request_queue *q, struct bio *bio, |
827 | struct page *page, unsigned int len, unsigned int offset) | |
828 | { | |
d1916c86 | 829 | bool same_page = false; |
e4581105 CH |
830 | return bio_add_hw_page(q, bio, page, len, offset, |
831 | queue_max_hw_sectors(q), &same_page); | |
19047087 | 832 | } |
a112a71d | 833 | EXPORT_SYMBOL(bio_add_pc_page); |
6e68af66 | 834 | |
1da177e4 | 835 | /** |
0aa69fd3 CH |
836 | * __bio_try_merge_page - try appending data to an existing bvec. |
837 | * @bio: destination bio | |
551879a4 | 838 | * @page: start page to add |
0aa69fd3 | 839 | * @len: length of the data to add |
551879a4 | 840 | * @off: offset of the data relative to @page |
ff896738 | 841 | * @same_page: return if the segment has been merged inside the same page |
1da177e4 | 842 | * |
0aa69fd3 | 843 | * Try to add the data at @page + @off to the last bvec of @bio. This is a |
3cf14889 | 844 | * useful optimisation for file systems with a block size smaller than the |
0aa69fd3 CH |
845 | * page size. |
846 | * | |
551879a4 ML |
847 | * Warn if (@len, @off) crosses pages in case that @same_page is true. |
848 | * | |
0aa69fd3 | 849 | * Return %true on success or %false on failure. |
1da177e4 | 850 | */ |
0aa69fd3 | 851 | bool __bio_try_merge_page(struct bio *bio, struct page *page, |
ff896738 | 852 | unsigned int len, unsigned int off, bool *same_page) |
1da177e4 | 853 | { |
c66a14d0 | 854 | if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED))) |
0aa69fd3 | 855 | return false; |
762380ad | 856 | |
cc90bc68 | 857 | if (bio->bi_vcnt > 0) { |
0aa69fd3 | 858 | struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
5919482e ML |
859 | |
860 | if (page_is_mergeable(bv, page, len, off, same_page)) { | |
2cd896a5 RH |
861 | if (bio->bi_iter.bi_size > UINT_MAX - len) { |
862 | *same_page = false; | |
cc90bc68 | 863 | return false; |
2cd896a5 | 864 | } |
5919482e ML |
865 | bv->bv_len += len; |
866 | bio->bi_iter.bi_size += len; | |
867 | return true; | |
868 | } | |
c66a14d0 | 869 | } |
0aa69fd3 CH |
870 | return false; |
871 | } | |
872 | EXPORT_SYMBOL_GPL(__bio_try_merge_page); | |
c66a14d0 | 873 | |
0aa69fd3 | 874 | /** |
551879a4 | 875 | * __bio_add_page - add page(s) to a bio in a new segment |
0aa69fd3 | 876 | * @bio: destination bio |
551879a4 ML |
877 | * @page: start page to add |
878 | * @len: length of the data to add, may cross pages | |
879 | * @off: offset of the data relative to @page, may cross pages | |
0aa69fd3 CH |
880 | * |
881 | * Add the data at @page + @off to @bio as a new bvec. The caller must ensure | |
882 | * that @bio has space for another bvec. | |
883 | */ | |
884 | void __bio_add_page(struct bio *bio, struct page *page, | |
885 | unsigned int len, unsigned int off) | |
886 | { | |
887 | struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt]; | |
c66a14d0 | 888 | |
0aa69fd3 | 889 | WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); |
79d08f89 | 890 | WARN_ON_ONCE(bio_full(bio, len)); |
0aa69fd3 CH |
891 | |
892 | bv->bv_page = page; | |
893 | bv->bv_offset = off; | |
894 | bv->bv_len = len; | |
c66a14d0 | 895 | |
c66a14d0 | 896 | bio->bi_iter.bi_size += len; |
0aa69fd3 | 897 | bio->bi_vcnt++; |
b8e24a93 JW |
898 | |
899 | if (!bio_flagged(bio, BIO_WORKINGSET) && unlikely(PageWorkingset(page))) | |
900 | bio_set_flag(bio, BIO_WORKINGSET); | |
0aa69fd3 CH |
901 | } |
902 | EXPORT_SYMBOL_GPL(__bio_add_page); | |
903 | ||
904 | /** | |
551879a4 | 905 | * bio_add_page - attempt to add page(s) to bio |
0aa69fd3 | 906 | * @bio: destination bio |
551879a4 ML |
907 | * @page: start page to add |
908 | * @len: vec entry length, may cross pages | |
909 | * @offset: vec entry offset relative to @page, may cross pages | |
0aa69fd3 | 910 | * |
551879a4 | 911 | * Attempt to add page(s) to the bio_vec maplist. This will only fail |
0aa69fd3 CH |
912 | * if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio. |
913 | */ | |
914 | int bio_add_page(struct bio *bio, struct page *page, | |
915 | unsigned int len, unsigned int offset) | |
916 | { | |
ff896738 CH |
917 | bool same_page = false; |
918 | ||
919 | if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) { | |
79d08f89 | 920 | if (bio_full(bio, len)) |
0aa69fd3 CH |
921 | return 0; |
922 | __bio_add_page(bio, page, len, offset); | |
923 | } | |
c66a14d0 | 924 | return len; |
1da177e4 | 925 | } |
a112a71d | 926 | EXPORT_SYMBOL(bio_add_page); |
1da177e4 | 927 | |
d241a95f | 928 | void bio_release_pages(struct bio *bio, bool mark_dirty) |
7321ecbf CH |
929 | { |
930 | struct bvec_iter_all iter_all; | |
931 | struct bio_vec *bvec; | |
7321ecbf | 932 | |
b2d0d991 CH |
933 | if (bio_flagged(bio, BIO_NO_PAGE_REF)) |
934 | return; | |
935 | ||
d241a95f CH |
936 | bio_for_each_segment_all(bvec, bio, iter_all) { |
937 | if (mark_dirty && !PageCompound(bvec->bv_page)) | |
938 | set_page_dirty_lock(bvec->bv_page); | |
7321ecbf | 939 | put_page(bvec->bv_page); |
d241a95f | 940 | } |
7321ecbf | 941 | } |
29b2a3aa | 942 | EXPORT_SYMBOL_GPL(bio_release_pages); |
7321ecbf | 943 | |
c42bca92 | 944 | static int bio_iov_bvec_set(struct bio *bio, struct iov_iter *iter) |
6d0c48ae | 945 | { |
c42bca92 PB |
946 | WARN_ON_ONCE(BVEC_POOL_IDX(bio) != 0); |
947 | ||
948 | bio->bi_vcnt = iter->nr_segs; | |
949 | bio->bi_max_vecs = iter->nr_segs; | |
950 | bio->bi_io_vec = (struct bio_vec *)iter->bvec; | |
951 | bio->bi_iter.bi_bvec_done = iter->iov_offset; | |
952 | bio->bi_iter.bi_size = iter->count; | |
953 | ||
954 | iov_iter_advance(iter, iter->count); | |
a10584c3 | 955 | return 0; |
6d0c48ae JA |
956 | } |
957 | ||
576ed913 CH |
958 | #define PAGE_PTRS_PER_BVEC (sizeof(struct bio_vec) / sizeof(struct page *)) |
959 | ||
2cefe4db | 960 | /** |
17d51b10 | 961 | * __bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio |
2cefe4db KO |
962 | * @bio: bio to add pages to |
963 | * @iter: iov iterator describing the region to be mapped | |
964 | * | |
17d51b10 | 965 | * Pins pages from *iter and appends them to @bio's bvec array. The |
2cefe4db | 966 | * pages will have to be released using put_page() when done. |
17d51b10 | 967 | * For multi-segment *iter, this function only adds pages from the |
3cf14889 | 968 | * next non-empty segment of the iov iterator. |
2cefe4db | 969 | */ |
17d51b10 | 970 | static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) |
2cefe4db | 971 | { |
576ed913 CH |
972 | unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt; |
973 | unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt; | |
2cefe4db KO |
974 | struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt; |
975 | struct page **pages = (struct page **)bv; | |
45691804 | 976 | bool same_page = false; |
576ed913 CH |
977 | ssize_t size, left; |
978 | unsigned len, i; | |
b403ea24 | 979 | size_t offset; |
576ed913 CH |
980 | |
981 | /* | |
982 | * Move page array up in the allocated memory for the bio vecs as far as | |
983 | * possible so that we can start filling biovecs from the beginning | |
984 | * without overwriting the temporary page array. | |
985 | */ | |
986 | BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2); | |
987 | pages += entries_left * (PAGE_PTRS_PER_BVEC - 1); | |
2cefe4db KO |
988 | |
989 | size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset); | |
990 | if (unlikely(size <= 0)) | |
991 | return size ? size : -EFAULT; | |
2cefe4db | 992 | |
576ed913 CH |
993 | for (left = size, i = 0; left > 0; left -= len, i++) { |
994 | struct page *page = pages[i]; | |
2cefe4db | 995 | |
576ed913 | 996 | len = min_t(size_t, PAGE_SIZE - offset, left); |
45691804 CH |
997 | |
998 | if (__bio_try_merge_page(bio, page, len, offset, &same_page)) { | |
999 | if (same_page) | |
1000 | put_page(page); | |
1001 | } else { | |
79d08f89 | 1002 | if (WARN_ON_ONCE(bio_full(bio, len))) |
45691804 CH |
1003 | return -EINVAL; |
1004 | __bio_add_page(bio, page, len, offset); | |
1005 | } | |
576ed913 | 1006 | offset = 0; |
2cefe4db KO |
1007 | } |
1008 | ||
2cefe4db KO |
1009 | iov_iter_advance(iter, size); |
1010 | return 0; | |
1011 | } | |
17d51b10 | 1012 | |
0512a75b KB |
1013 | static int __bio_iov_append_get_pages(struct bio *bio, struct iov_iter *iter) |
1014 | { | |
1015 | unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt; | |
1016 | unsigned short entries_left = bio->bi_max_vecs - bio->bi_vcnt; | |
309dca30 | 1017 | struct request_queue *q = bio->bi_bdev->bd_disk->queue; |
0512a75b KB |
1018 | unsigned int max_append_sectors = queue_max_zone_append_sectors(q); |
1019 | struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt; | |
1020 | struct page **pages = (struct page **)bv; | |
1021 | ssize_t size, left; | |
1022 | unsigned len, i; | |
1023 | size_t offset; | |
4977d121 | 1024 | int ret = 0; |
0512a75b KB |
1025 | |
1026 | if (WARN_ON_ONCE(!max_append_sectors)) | |
1027 | return 0; | |
1028 | ||
1029 | /* | |
1030 | * Move page array up in the allocated memory for the bio vecs as far as | |
1031 | * possible so that we can start filling biovecs from the beginning | |
1032 | * without overwriting the temporary page array. | |
1033 | */ | |
1034 | BUILD_BUG_ON(PAGE_PTRS_PER_BVEC < 2); | |
1035 | pages += entries_left * (PAGE_PTRS_PER_BVEC - 1); | |
1036 | ||
1037 | size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset); | |
1038 | if (unlikely(size <= 0)) | |
1039 | return size ? size : -EFAULT; | |
1040 | ||
1041 | for (left = size, i = 0; left > 0; left -= len, i++) { | |
1042 | struct page *page = pages[i]; | |
1043 | bool same_page = false; | |
1044 | ||
1045 | len = min_t(size_t, PAGE_SIZE - offset, left); | |
1046 | if (bio_add_hw_page(q, bio, page, len, offset, | |
4977d121 NA |
1047 | max_append_sectors, &same_page) != len) { |
1048 | ret = -EINVAL; | |
1049 | break; | |
1050 | } | |
0512a75b KB |
1051 | if (same_page) |
1052 | put_page(page); | |
1053 | offset = 0; | |
1054 | } | |
1055 | ||
4977d121 NA |
1056 | iov_iter_advance(iter, size - left); |
1057 | return ret; | |
0512a75b KB |
1058 | } |
1059 | ||
17d51b10 | 1060 | /** |
6d0c48ae | 1061 | * bio_iov_iter_get_pages - add user or kernel pages to a bio |
17d51b10 | 1062 | * @bio: bio to add pages to |
6d0c48ae JA |
1063 | * @iter: iov iterator describing the region to be added |
1064 | * | |
1065 | * This takes either an iterator pointing to user memory, or one pointing to | |
1066 | * kernel pages (BVEC iterator). If we're adding user pages, we pin them and | |
1067 | * map them into the kernel. On IO completion, the caller should put those | |
c42bca92 PB |
1068 | * pages. For bvec based iterators bio_iov_iter_get_pages() uses the provided |
1069 | * bvecs rather than copying them. Hence anyone issuing kiocb based IO needs | |
1070 | * to ensure the bvecs and pages stay referenced until the submitted I/O is | |
1071 | * completed by a call to ->ki_complete() or returns with an error other than | |
1072 | * -EIOCBQUEUED. The caller needs to check if the bio is flagged BIO_NO_PAGE_REF | |
1073 | * on IO completion. If it isn't, then pages should be released. | |
17d51b10 | 1074 | * |
17d51b10 | 1075 | * The function tries, but does not guarantee, to pin as many pages as |
5cd3ddc1 | 1076 | * fit into the bio, or are requested in @iter, whatever is smaller. If |
6d0c48ae JA |
1077 | * MM encounters an error pinning the requested pages, it stops. Error |
1078 | * is returned only if 0 pages could be pinned. | |
0cf41e5e PB |
1079 | * |
1080 | * It's intended for direct IO, so doesn't do PSI tracking, the caller is | |
1081 | * responsible for setting BIO_WORKINGSET if necessary. | |
17d51b10 MW |
1082 | */ |
1083 | int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) | |
1084 | { | |
c42bca92 | 1085 | int ret = 0; |
17d51b10 | 1086 | |
c42bca92 PB |
1087 | if (iov_iter_is_bvec(iter)) { |
1088 | if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND)) | |
1089 | return -EINVAL; | |
1090 | bio_iov_bvec_set(bio, iter); | |
1091 | bio_set_flag(bio, BIO_NO_PAGE_REF); | |
1092 | return 0; | |
1093 | } else { | |
1094 | do { | |
1095 | if (bio_op(bio) == REQ_OP_ZONE_APPEND) | |
1096 | ret = __bio_iov_append_get_pages(bio, iter); | |
0512a75b KB |
1097 | else |
1098 | ret = __bio_iov_iter_get_pages(bio, iter); | |
c42bca92 PB |
1099 | } while (!ret && iov_iter_count(iter) && !bio_full(bio, 0)); |
1100 | } | |
0cf41e5e PB |
1101 | |
1102 | /* don't account direct I/O as memory stall */ | |
1103 | bio_clear_flag(bio, BIO_WORKINGSET); | |
14eacf12 | 1104 | return bio->bi_vcnt ? 0 : ret; |
17d51b10 | 1105 | } |
29b2a3aa | 1106 | EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages); |
2cefe4db | 1107 | |
4246a0b6 | 1108 | static void submit_bio_wait_endio(struct bio *bio) |
9e882242 | 1109 | { |
65e53aab | 1110 | complete(bio->bi_private); |
9e882242 KO |
1111 | } |
1112 | ||
1113 | /** | |
1114 | * submit_bio_wait - submit a bio, and wait until it completes | |
9e882242 KO |
1115 | * @bio: The &struct bio which describes the I/O |
1116 | * | |
1117 | * Simple wrapper around submit_bio(). Returns 0 on success, or the error from | |
1118 | * bio_endio() on failure. | |
3d289d68 JK |
1119 | * |
1120 | * WARNING: Unlike to how submit_bio() is usually used, this function does not | |
1121 | * result in bio reference to be consumed. The caller must drop the reference | |
1122 | * on his own. | |
9e882242 | 1123 | */ |
4e49ea4a | 1124 | int submit_bio_wait(struct bio *bio) |
9e882242 | 1125 | { |
309dca30 CH |
1126 | DECLARE_COMPLETION_ONSTACK_MAP(done, |
1127 | bio->bi_bdev->bd_disk->lockdep_map); | |
de6a78b6 | 1128 | unsigned long hang_check; |
9e882242 | 1129 | |
65e53aab | 1130 | bio->bi_private = &done; |
9e882242 | 1131 | bio->bi_end_io = submit_bio_wait_endio; |
1eff9d32 | 1132 | bio->bi_opf |= REQ_SYNC; |
4e49ea4a | 1133 | submit_bio(bio); |
de6a78b6 ML |
1134 | |
1135 | /* Prevent hang_check timer from firing at us during very long I/O */ | |
1136 | hang_check = sysctl_hung_task_timeout_secs; | |
1137 | if (hang_check) | |
1138 | while (!wait_for_completion_io_timeout(&done, | |
1139 | hang_check * (HZ/2))) | |
1140 | ; | |
1141 | else | |
1142 | wait_for_completion_io(&done); | |
9e882242 | 1143 | |
65e53aab | 1144 | return blk_status_to_errno(bio->bi_status); |
9e882242 KO |
1145 | } |
1146 | EXPORT_SYMBOL(submit_bio_wait); | |
1147 | ||
054bdf64 KO |
1148 | /** |
1149 | * bio_advance - increment/complete a bio by some number of bytes | |
1150 | * @bio: bio to advance | |
1151 | * @bytes: number of bytes to complete | |
1152 | * | |
1153 | * This updates bi_sector, bi_size and bi_idx; if the number of bytes to | |
1154 | * complete doesn't align with a bvec boundary, then bv_len and bv_offset will | |
1155 | * be updated on the last bvec as well. | |
1156 | * | |
1157 | * @bio will then represent the remaining, uncompleted portion of the io. | |
1158 | */ | |
1159 | void bio_advance(struct bio *bio, unsigned bytes) | |
1160 | { | |
1161 | if (bio_integrity(bio)) | |
1162 | bio_integrity_advance(bio, bytes); | |
1163 | ||
a892c8d5 | 1164 | bio_crypt_advance(bio, bytes); |
4550dd6c | 1165 | bio_advance_iter(bio, &bio->bi_iter, bytes); |
054bdf64 KO |
1166 | } |
1167 | EXPORT_SYMBOL(bio_advance); | |
1168 | ||
45db54d5 KO |
1169 | void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter, |
1170 | struct bio *src, struct bvec_iter *src_iter) | |
16ac3d63 | 1171 | { |
1cb9dda4 | 1172 | struct bio_vec src_bv, dst_bv; |
16ac3d63 | 1173 | void *src_p, *dst_p; |
1cb9dda4 | 1174 | unsigned bytes; |
16ac3d63 | 1175 | |
45db54d5 KO |
1176 | while (src_iter->bi_size && dst_iter->bi_size) { |
1177 | src_bv = bio_iter_iovec(src, *src_iter); | |
1178 | dst_bv = bio_iter_iovec(dst, *dst_iter); | |
1cb9dda4 KO |
1179 | |
1180 | bytes = min(src_bv.bv_len, dst_bv.bv_len); | |
16ac3d63 | 1181 | |
1cb9dda4 KO |
1182 | src_p = kmap_atomic(src_bv.bv_page); |
1183 | dst_p = kmap_atomic(dst_bv.bv_page); | |
16ac3d63 | 1184 | |
1cb9dda4 KO |
1185 | memcpy(dst_p + dst_bv.bv_offset, |
1186 | src_p + src_bv.bv_offset, | |
16ac3d63 KO |
1187 | bytes); |
1188 | ||
1189 | kunmap_atomic(dst_p); | |
1190 | kunmap_atomic(src_p); | |
1191 | ||
6e6e811d KO |
1192 | flush_dcache_page(dst_bv.bv_page); |
1193 | ||
22b56c29 PB |
1194 | bio_advance_iter_single(src, src_iter, bytes); |
1195 | bio_advance_iter_single(dst, dst_iter, bytes); | |
16ac3d63 KO |
1196 | } |
1197 | } | |
38a72dac KO |
1198 | EXPORT_SYMBOL(bio_copy_data_iter); |
1199 | ||
1200 | /** | |
45db54d5 KO |
1201 | * bio_copy_data - copy contents of data buffers from one bio to another |
1202 | * @src: source bio | |
1203 | * @dst: destination bio | |
38a72dac KO |
1204 | * |
1205 | * Stops when it reaches the end of either @src or @dst - that is, copies | |
1206 | * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios). | |
1207 | */ | |
1208 | void bio_copy_data(struct bio *dst, struct bio *src) | |
1209 | { | |
45db54d5 KO |
1210 | struct bvec_iter src_iter = src->bi_iter; |
1211 | struct bvec_iter dst_iter = dst->bi_iter; | |
1212 | ||
1213 | bio_copy_data_iter(dst, &dst_iter, src, &src_iter); | |
38a72dac | 1214 | } |
16ac3d63 KO |
1215 | EXPORT_SYMBOL(bio_copy_data); |
1216 | ||
45db54d5 KO |
1217 | /** |
1218 | * bio_list_copy_data - copy contents of data buffers from one chain of bios to | |
1219 | * another | |
1220 | * @src: source bio list | |
1221 | * @dst: destination bio list | |
1222 | * | |
1223 | * Stops when it reaches the end of either the @src list or @dst list - that is, | |
1224 | * copies min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of | |
1225 | * bios). | |
1226 | */ | |
1227 | void bio_list_copy_data(struct bio *dst, struct bio *src) | |
1228 | { | |
1229 | struct bvec_iter src_iter = src->bi_iter; | |
1230 | struct bvec_iter dst_iter = dst->bi_iter; | |
1231 | ||
1232 | while (1) { | |
1233 | if (!src_iter.bi_size) { | |
1234 | src = src->bi_next; | |
1235 | if (!src) | |
1236 | break; | |
1237 | ||
1238 | src_iter = src->bi_iter; | |
1239 | } | |
1240 | ||
1241 | if (!dst_iter.bi_size) { | |
1242 | dst = dst->bi_next; | |
1243 | if (!dst) | |
1244 | break; | |
1245 | ||
1246 | dst_iter = dst->bi_iter; | |
1247 | } | |
1248 | ||
1249 | bio_copy_data_iter(dst, &dst_iter, src, &src_iter); | |
1250 | } | |
1251 | } | |
1252 | EXPORT_SYMBOL(bio_list_copy_data); | |
1253 | ||
491221f8 | 1254 | void bio_free_pages(struct bio *bio) |
1dfa0f68 CH |
1255 | { |
1256 | struct bio_vec *bvec; | |
6dc4f100 | 1257 | struct bvec_iter_all iter_all; |
1dfa0f68 | 1258 | |
2b070cfe | 1259 | bio_for_each_segment_all(bvec, bio, iter_all) |
1dfa0f68 CH |
1260 | __free_page(bvec->bv_page); |
1261 | } | |
491221f8 | 1262 | EXPORT_SYMBOL(bio_free_pages); |
1dfa0f68 | 1263 | |
1da177e4 LT |
1264 | /* |
1265 | * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions | |
1266 | * for performing direct-IO in BIOs. | |
1267 | * | |
1268 | * The problem is that we cannot run set_page_dirty() from interrupt context | |
1269 | * because the required locks are not interrupt-safe. So what we can do is to | |
1270 | * mark the pages dirty _before_ performing IO. And in interrupt context, | |
1271 | * check that the pages are still dirty. If so, fine. If not, redirty them | |
1272 | * in process context. | |
1273 | * | |
1274 | * We special-case compound pages here: normally this means reads into hugetlb | |
1275 | * pages. The logic in here doesn't really work right for compound pages | |
1276 | * because the VM does not uniformly chase down the head page in all cases. | |
1277 | * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't | |
1278 | * handle them at all. So we skip compound pages here at an early stage. | |
1279 | * | |
1280 | * Note that this code is very hard to test under normal circumstances because | |
1281 | * direct-io pins the pages with get_user_pages(). This makes | |
1282 | * is_page_cache_freeable return false, and the VM will not clean the pages. | |
0d5c3eba | 1283 | * But other code (eg, flusher threads) could clean the pages if they are mapped |
1da177e4 LT |
1284 | * pagecache. |
1285 | * | |
1286 | * Simply disabling the call to bio_set_pages_dirty() is a good way to test the | |
1287 | * deferred bio dirtying paths. | |
1288 | */ | |
1289 | ||
1290 | /* | |
1291 | * bio_set_pages_dirty() will mark all the bio's pages as dirty. | |
1292 | */ | |
1293 | void bio_set_pages_dirty(struct bio *bio) | |
1294 | { | |
cb34e057 | 1295 | struct bio_vec *bvec; |
6dc4f100 | 1296 | struct bvec_iter_all iter_all; |
1da177e4 | 1297 | |
2b070cfe | 1298 | bio_for_each_segment_all(bvec, bio, iter_all) { |
3bb50983 CH |
1299 | if (!PageCompound(bvec->bv_page)) |
1300 | set_page_dirty_lock(bvec->bv_page); | |
1da177e4 LT |
1301 | } |
1302 | } | |
1303 | ||
1da177e4 LT |
1304 | /* |
1305 | * bio_check_pages_dirty() will check that all the BIO's pages are still dirty. | |
1306 | * If they are, then fine. If, however, some pages are clean then they must | |
1307 | * have been written out during the direct-IO read. So we take another ref on | |
24d5493f | 1308 | * the BIO and re-dirty the pages in process context. |
1da177e4 LT |
1309 | * |
1310 | * It is expected that bio_check_pages_dirty() will wholly own the BIO from | |
ea1754a0 KS |
1311 | * here on. It will run one put_page() against each page and will run one |
1312 | * bio_put() against the BIO. | |
1da177e4 LT |
1313 | */ |
1314 | ||
65f27f38 | 1315 | static void bio_dirty_fn(struct work_struct *work); |
1da177e4 | 1316 | |
65f27f38 | 1317 | static DECLARE_WORK(bio_dirty_work, bio_dirty_fn); |
1da177e4 LT |
1318 | static DEFINE_SPINLOCK(bio_dirty_lock); |
1319 | static struct bio *bio_dirty_list; | |
1320 | ||
1321 | /* | |
1322 | * This runs in process context | |
1323 | */ | |
65f27f38 | 1324 | static void bio_dirty_fn(struct work_struct *work) |
1da177e4 | 1325 | { |
24d5493f | 1326 | struct bio *bio, *next; |
1da177e4 | 1327 | |
24d5493f CH |
1328 | spin_lock_irq(&bio_dirty_lock); |
1329 | next = bio_dirty_list; | |
1da177e4 | 1330 | bio_dirty_list = NULL; |
24d5493f | 1331 | spin_unlock_irq(&bio_dirty_lock); |
1da177e4 | 1332 | |
24d5493f CH |
1333 | while ((bio = next) != NULL) { |
1334 | next = bio->bi_private; | |
1da177e4 | 1335 | |
d241a95f | 1336 | bio_release_pages(bio, true); |
1da177e4 | 1337 | bio_put(bio); |
1da177e4 LT |
1338 | } |
1339 | } | |
1340 | ||
1341 | void bio_check_pages_dirty(struct bio *bio) | |
1342 | { | |
cb34e057 | 1343 | struct bio_vec *bvec; |
24d5493f | 1344 | unsigned long flags; |
6dc4f100 | 1345 | struct bvec_iter_all iter_all; |
1da177e4 | 1346 | |
2b070cfe | 1347 | bio_for_each_segment_all(bvec, bio, iter_all) { |
24d5493f CH |
1348 | if (!PageDirty(bvec->bv_page) && !PageCompound(bvec->bv_page)) |
1349 | goto defer; | |
1da177e4 LT |
1350 | } |
1351 | ||
d241a95f | 1352 | bio_release_pages(bio, false); |
24d5493f CH |
1353 | bio_put(bio); |
1354 | return; | |
1355 | defer: | |
1356 | spin_lock_irqsave(&bio_dirty_lock, flags); | |
1357 | bio->bi_private = bio_dirty_list; | |
1358 | bio_dirty_list = bio; | |
1359 | spin_unlock_irqrestore(&bio_dirty_lock, flags); | |
1360 | schedule_work(&bio_dirty_work); | |
1da177e4 LT |
1361 | } |
1362 | ||
c4cf5261 JA |
1363 | static inline bool bio_remaining_done(struct bio *bio) |
1364 | { | |
1365 | /* | |
1366 | * If we're not chaining, then ->__bi_remaining is always 1 and | |
1367 | * we always end io on the first invocation. | |
1368 | */ | |
1369 | if (!bio_flagged(bio, BIO_CHAIN)) | |
1370 | return true; | |
1371 | ||
1372 | BUG_ON(atomic_read(&bio->__bi_remaining) <= 0); | |
1373 | ||
326e1dbb | 1374 | if (atomic_dec_and_test(&bio->__bi_remaining)) { |
b7c44ed9 | 1375 | bio_clear_flag(bio, BIO_CHAIN); |
c4cf5261 | 1376 | return true; |
326e1dbb | 1377 | } |
c4cf5261 JA |
1378 | |
1379 | return false; | |
1380 | } | |
1381 | ||
1da177e4 LT |
1382 | /** |
1383 | * bio_endio - end I/O on a bio | |
1384 | * @bio: bio | |
1da177e4 LT |
1385 | * |
1386 | * Description: | |
4246a0b6 CH |
1387 | * bio_endio() will end I/O on the whole bio. bio_endio() is the preferred |
1388 | * way to end I/O on a bio. No one should call bi_end_io() directly on a | |
1389 | * bio unless they own it and thus know that it has an end_io function. | |
fbbaf700 N |
1390 | * |
1391 | * bio_endio() can be called several times on a bio that has been chained | |
1392 | * using bio_chain(). The ->bi_end_io() function will only be called the | |
1393 | * last time. At this point the BLK_TA_COMPLETE tracing event will be | |
1394 | * generated if BIO_TRACE_COMPLETION is set. | |
1da177e4 | 1395 | **/ |
4246a0b6 | 1396 | void bio_endio(struct bio *bio) |
1da177e4 | 1397 | { |
ba8c6967 | 1398 | again: |
2b885517 | 1399 | if (!bio_remaining_done(bio)) |
ba8c6967 | 1400 | return; |
7c20f116 CH |
1401 | if (!bio_integrity_endio(bio)) |
1402 | return; | |
1da177e4 | 1403 | |
309dca30 CH |
1404 | if (bio->bi_bdev) |
1405 | rq_qos_done_bio(bio->bi_bdev->bd_disk->queue, bio); | |
67b42d0b | 1406 | |
ba8c6967 CH |
1407 | /* |
1408 | * Need to have a real endio function for chained bios, otherwise | |
1409 | * various corner cases will break (like stacking block devices that | |
1410 | * save/restore bi_end_io) - however, we want to avoid unbounded | |
1411 | * recursion and blowing the stack. Tail call optimization would | |
1412 | * handle this, but compiling with frame pointers also disables | |
1413 | * gcc's sibling call optimization. | |
1414 | */ | |
1415 | if (bio->bi_end_io == bio_chain_endio) { | |
1416 | bio = __bio_chain_endio(bio); | |
1417 | goto again; | |
196d38bc | 1418 | } |
ba8c6967 | 1419 | |
309dca30 CH |
1420 | if (bio->bi_bdev && bio_flagged(bio, BIO_TRACE_COMPLETION)) { |
1421 | trace_block_bio_complete(bio->bi_bdev->bd_disk->queue, bio); | |
fbbaf700 N |
1422 | bio_clear_flag(bio, BIO_TRACE_COMPLETION); |
1423 | } | |
1424 | ||
9e234eea | 1425 | blk_throtl_bio_endio(bio); |
b222dd2f SL |
1426 | /* release cgroup info */ |
1427 | bio_uninit(bio); | |
ba8c6967 CH |
1428 | if (bio->bi_end_io) |
1429 | bio->bi_end_io(bio); | |
1da177e4 | 1430 | } |
a112a71d | 1431 | EXPORT_SYMBOL(bio_endio); |
1da177e4 | 1432 | |
20d0189b KO |
1433 | /** |
1434 | * bio_split - split a bio | |
1435 | * @bio: bio to split | |
1436 | * @sectors: number of sectors to split from the front of @bio | |
1437 | * @gfp: gfp mask | |
1438 | * @bs: bio set to allocate from | |
1439 | * | |
1440 | * Allocates and returns a new bio which represents @sectors from the start of | |
1441 | * @bio, and updates @bio to represent the remaining sectors. | |
1442 | * | |
f3f5da62 | 1443 | * Unless this is a discard request the newly allocated bio will point |
dad77584 BVA |
1444 | * to @bio's bi_io_vec. It is the caller's responsibility to ensure that |
1445 | * neither @bio nor @bs are freed before the split bio. | |
20d0189b KO |
1446 | */ |
1447 | struct bio *bio_split(struct bio *bio, int sectors, | |
1448 | gfp_t gfp, struct bio_set *bs) | |
1449 | { | |
f341a4d3 | 1450 | struct bio *split; |
20d0189b KO |
1451 | |
1452 | BUG_ON(sectors <= 0); | |
1453 | BUG_ON(sectors >= bio_sectors(bio)); | |
1454 | ||
0512a75b KB |
1455 | /* Zone append commands cannot be split */ |
1456 | if (WARN_ON_ONCE(bio_op(bio) == REQ_OP_ZONE_APPEND)) | |
1457 | return NULL; | |
1458 | ||
f9d03f96 | 1459 | split = bio_clone_fast(bio, gfp, bs); |
20d0189b KO |
1460 | if (!split) |
1461 | return NULL; | |
1462 | ||
1463 | split->bi_iter.bi_size = sectors << 9; | |
1464 | ||
1465 | if (bio_integrity(split)) | |
fbd08e76 | 1466 | bio_integrity_trim(split); |
20d0189b KO |
1467 | |
1468 | bio_advance(bio, split->bi_iter.bi_size); | |
1469 | ||
fbbaf700 | 1470 | if (bio_flagged(bio, BIO_TRACE_COMPLETION)) |
20d59023 | 1471 | bio_set_flag(split, BIO_TRACE_COMPLETION); |
fbbaf700 | 1472 | |
20d0189b KO |
1473 | return split; |
1474 | } | |
1475 | EXPORT_SYMBOL(bio_split); | |
1476 | ||
6678d83f KO |
1477 | /** |
1478 | * bio_trim - trim a bio | |
1479 | * @bio: bio to trim | |
1480 | * @offset: number of sectors to trim from the front of @bio | |
1481 | * @size: size we want to trim @bio to, in sectors | |
1482 | */ | |
1483 | void bio_trim(struct bio *bio, int offset, int size) | |
1484 | { | |
1485 | /* 'bio' is a cloned bio which we need to trim to match | |
1486 | * the given offset and size. | |
6678d83f | 1487 | */ |
6678d83f KO |
1488 | |
1489 | size <<= 9; | |
4f024f37 | 1490 | if (offset == 0 && size == bio->bi_iter.bi_size) |
6678d83f KO |
1491 | return; |
1492 | ||
6678d83f | 1493 | bio_advance(bio, offset << 9); |
4f024f37 | 1494 | bio->bi_iter.bi_size = size; |
376a78ab DM |
1495 | |
1496 | if (bio_integrity(bio)) | |
fbd08e76 | 1497 | bio_integrity_trim(bio); |
376a78ab | 1498 | |
6678d83f KO |
1499 | } |
1500 | EXPORT_SYMBOL_GPL(bio_trim); | |
1501 | ||
1da177e4 LT |
1502 | /* |
1503 | * create memory pools for biovec's in a bio_set. | |
1504 | * use the global biovec slabs created for general use. | |
1505 | */ | |
8aa6ba2f | 1506 | int biovec_init_pool(mempool_t *pool, int pool_entries) |
1da177e4 | 1507 | { |
ed996a52 | 1508 | struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX; |
1da177e4 | 1509 | |
8aa6ba2f | 1510 | return mempool_init_slab_pool(pool, pool_entries, bp->slab); |
1da177e4 LT |
1511 | } |
1512 | ||
917a38c7 KO |
1513 | /* |
1514 | * bioset_exit - exit a bioset initialized with bioset_init() | |
1515 | * | |
1516 | * May be called on a zeroed but uninitialized bioset (i.e. allocated with | |
1517 | * kzalloc()). | |
1518 | */ | |
1519 | void bioset_exit(struct bio_set *bs) | |
1da177e4 | 1520 | { |
df2cb6da KO |
1521 | if (bs->rescue_workqueue) |
1522 | destroy_workqueue(bs->rescue_workqueue); | |
917a38c7 | 1523 | bs->rescue_workqueue = NULL; |
df2cb6da | 1524 | |
8aa6ba2f KO |
1525 | mempool_exit(&bs->bio_pool); |
1526 | mempool_exit(&bs->bvec_pool); | |
9f060e22 | 1527 | |
7878cba9 | 1528 | bioset_integrity_free(bs); |
917a38c7 KO |
1529 | if (bs->bio_slab) |
1530 | bio_put_slab(bs); | |
1531 | bs->bio_slab = NULL; | |
1532 | } | |
1533 | EXPORT_SYMBOL(bioset_exit); | |
1da177e4 | 1534 | |
917a38c7 KO |
1535 | /** |
1536 | * bioset_init - Initialize a bio_set | |
dad08527 | 1537 | * @bs: pool to initialize |
917a38c7 KO |
1538 | * @pool_size: Number of bio and bio_vecs to cache in the mempool |
1539 | * @front_pad: Number of bytes to allocate in front of the returned bio | |
1540 | * @flags: Flags to modify behavior, currently %BIOSET_NEED_BVECS | |
1541 | * and %BIOSET_NEED_RESCUER | |
1542 | * | |
dad08527 KO |
1543 | * Description: |
1544 | * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller | |
1545 | * to ask for a number of bytes to be allocated in front of the bio. | |
1546 | * Front pad allocation is useful for embedding the bio inside | |
1547 | * another structure, to avoid allocating extra data to go with the bio. | |
1548 | * Note that the bio must be embedded at the END of that structure always, | |
1549 | * or things will break badly. | |
1550 | * If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated | |
1551 | * for allocating iovecs. This pool is not needed e.g. for bio_clone_fast(). | |
1552 | * If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used to | |
1553 | * dispatch queued requests when the mempool runs out of space. | |
1554 | * | |
917a38c7 KO |
1555 | */ |
1556 | int bioset_init(struct bio_set *bs, | |
1557 | unsigned int pool_size, | |
1558 | unsigned int front_pad, | |
1559 | int flags) | |
1560 | { | |
917a38c7 | 1561 | bs->front_pad = front_pad; |
9f180e31 ML |
1562 | if (flags & BIOSET_NEED_BVECS) |
1563 | bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec); | |
1564 | else | |
1565 | bs->back_pad = 0; | |
917a38c7 KO |
1566 | |
1567 | spin_lock_init(&bs->rescue_lock); | |
1568 | bio_list_init(&bs->rescue_list); | |
1569 | INIT_WORK(&bs->rescue_work, bio_alloc_rescue); | |
1570 | ||
49d1ec85 | 1571 | bs->bio_slab = bio_find_or_create_slab(bs); |
917a38c7 KO |
1572 | if (!bs->bio_slab) |
1573 | return -ENOMEM; | |
1574 | ||
1575 | if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab)) | |
1576 | goto bad; | |
1577 | ||
1578 | if ((flags & BIOSET_NEED_BVECS) && | |
1579 | biovec_init_pool(&bs->bvec_pool, pool_size)) | |
1580 | goto bad; | |
1581 | ||
1582 | if (!(flags & BIOSET_NEED_RESCUER)) | |
1583 | return 0; | |
1584 | ||
1585 | bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0); | |
1586 | if (!bs->rescue_workqueue) | |
1587 | goto bad; | |
1588 | ||
1589 | return 0; | |
1590 | bad: | |
1591 | bioset_exit(bs); | |
1592 | return -ENOMEM; | |
1593 | } | |
1594 | EXPORT_SYMBOL(bioset_init); | |
1595 | ||
28e89fd9 JA |
1596 | /* |
1597 | * Initialize and setup a new bio_set, based on the settings from | |
1598 | * another bio_set. | |
1599 | */ | |
1600 | int bioset_init_from_src(struct bio_set *bs, struct bio_set *src) | |
1601 | { | |
1602 | int flags; | |
1603 | ||
1604 | flags = 0; | |
1605 | if (src->bvec_pool.min_nr) | |
1606 | flags |= BIOSET_NEED_BVECS; | |
1607 | if (src->rescue_workqueue) | |
1608 | flags |= BIOSET_NEED_RESCUER; | |
1609 | ||
1610 | return bioset_init(bs, src->bio_pool.min_nr, src->front_pad, flags); | |
1611 | } | |
1612 | EXPORT_SYMBOL(bioset_init_from_src); | |
1613 | ||
1da177e4 LT |
1614 | static void __init biovec_init_slabs(void) |
1615 | { | |
1616 | int i; | |
1617 | ||
ed996a52 | 1618 | for (i = 0; i < BVEC_POOL_NR; i++) { |
1da177e4 LT |
1619 | int size; |
1620 | struct biovec_slab *bvs = bvec_slabs + i; | |
1621 | ||
a7fcd37c JA |
1622 | if (bvs->nr_vecs <= BIO_INLINE_VECS) { |
1623 | bvs->slab = NULL; | |
1624 | continue; | |
1625 | } | |
a7fcd37c | 1626 | |
1da177e4 LT |
1627 | size = bvs->nr_vecs * sizeof(struct bio_vec); |
1628 | bvs->slab = kmem_cache_create(bvs->name, size, 0, | |
20c2df83 | 1629 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
1da177e4 LT |
1630 | } |
1631 | } | |
1632 | ||
1633 | static int __init init_bio(void) | |
1634 | { | |
2b24e6f6 JT |
1635 | BUILD_BUG_ON(BIO_FLAG_LAST > BVEC_POOL_OFFSET); |
1636 | ||
7878cba9 | 1637 | bio_integrity_init(); |
1da177e4 LT |
1638 | biovec_init_slabs(); |
1639 | ||
f4f8154a | 1640 | if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS)) |
1da177e4 LT |
1641 | panic("bio: can't allocate bios\n"); |
1642 | ||
f4f8154a | 1643 | if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE)) |
a91a2785 MP |
1644 | panic("bio: can't create integrity pool\n"); |
1645 | ||
1da177e4 LT |
1646 | return 0; |
1647 | } | |
1da177e4 | 1648 | subsys_initcall(init_bio); |