]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
0fe23479 | 2 | * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk> |
1da177e4 LT |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public Licens | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- | |
16 | * | |
17 | */ | |
18 | #include <linux/mm.h> | |
19 | #include <linux/swap.h> | |
20 | #include <linux/bio.h> | |
21 | #include <linux/blkdev.h> | |
a27bb332 | 22 | #include <linux/uio.h> |
852c788f | 23 | #include <linux/iocontext.h> |
1da177e4 LT |
24 | #include <linux/slab.h> |
25 | #include <linux/init.h> | |
26 | #include <linux/kernel.h> | |
630d9c47 | 27 | #include <linux/export.h> |
1da177e4 LT |
28 | #include <linux/mempool.h> |
29 | #include <linux/workqueue.h> | |
852c788f | 30 | #include <linux/cgroup.h> |
08e18eab | 31 | #include <linux/blk-cgroup.h> |
1da177e4 | 32 | |
55782138 | 33 | #include <trace/events/block.h> |
9e234eea | 34 | #include "blk.h" |
0bfc2455 | 35 | |
392ddc32 JA |
36 | /* |
37 | * Test patch to inline a certain number of bi_io_vec's inside the bio | |
38 | * itself, to shrink a bio data allocation from two mempool calls to one | |
39 | */ | |
40 | #define BIO_INLINE_VECS 4 | |
41 | ||
1da177e4 LT |
42 | /* |
43 | * if you change this list, also change bvec_alloc or things will | |
44 | * break badly! cannot be bigger than what you can fit into an | |
45 | * unsigned short | |
46 | */ | |
bd5c4fac | 47 | #define BV(x, n) { .nr_vecs = x, .name = "biovec-"#n } |
ed996a52 | 48 | static struct biovec_slab bvec_slabs[BVEC_POOL_NR] __read_mostly = { |
bd5c4fac | 49 | BV(1, 1), BV(4, 4), BV(16, 16), BV(64, 64), BV(128, 128), BV(BIO_MAX_PAGES, max), |
1da177e4 LT |
50 | }; |
51 | #undef BV | |
52 | ||
1da177e4 LT |
53 | /* |
54 | * fs_bio_set is the bio_set containing bio and iovec memory pools used by | |
55 | * IO code that does not need private memory pools. | |
56 | */ | |
f4f8154a | 57 | struct bio_set fs_bio_set; |
3f86a82a | 58 | EXPORT_SYMBOL(fs_bio_set); |
1da177e4 | 59 | |
bb799ca0 JA |
60 | /* |
61 | * Our slab pool management | |
62 | */ | |
63 | struct bio_slab { | |
64 | struct kmem_cache *slab; | |
65 | unsigned int slab_ref; | |
66 | unsigned int slab_size; | |
67 | char name[8]; | |
68 | }; | |
69 | static DEFINE_MUTEX(bio_slab_lock); | |
70 | static struct bio_slab *bio_slabs; | |
71 | static unsigned int bio_slab_nr, bio_slab_max; | |
72 | ||
73 | static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size) | |
74 | { | |
75 | unsigned int sz = sizeof(struct bio) + extra_size; | |
76 | struct kmem_cache *slab = NULL; | |
389d7b26 | 77 | struct bio_slab *bslab, *new_bio_slabs; |
386bc35a | 78 | unsigned int new_bio_slab_max; |
bb799ca0 JA |
79 | unsigned int i, entry = -1; |
80 | ||
81 | mutex_lock(&bio_slab_lock); | |
82 | ||
83 | i = 0; | |
84 | while (i < bio_slab_nr) { | |
f06f135d | 85 | bslab = &bio_slabs[i]; |
bb799ca0 JA |
86 | |
87 | if (!bslab->slab && entry == -1) | |
88 | entry = i; | |
89 | else if (bslab->slab_size == sz) { | |
90 | slab = bslab->slab; | |
91 | bslab->slab_ref++; | |
92 | break; | |
93 | } | |
94 | i++; | |
95 | } | |
96 | ||
97 | if (slab) | |
98 | goto out_unlock; | |
99 | ||
100 | if (bio_slab_nr == bio_slab_max && entry == -1) { | |
386bc35a | 101 | new_bio_slab_max = bio_slab_max << 1; |
389d7b26 | 102 | new_bio_slabs = krealloc(bio_slabs, |
386bc35a | 103 | new_bio_slab_max * sizeof(struct bio_slab), |
389d7b26 AK |
104 | GFP_KERNEL); |
105 | if (!new_bio_slabs) | |
bb799ca0 | 106 | goto out_unlock; |
386bc35a | 107 | bio_slab_max = new_bio_slab_max; |
389d7b26 | 108 | bio_slabs = new_bio_slabs; |
bb799ca0 JA |
109 | } |
110 | if (entry == -1) | |
111 | entry = bio_slab_nr++; | |
112 | ||
113 | bslab = &bio_slabs[entry]; | |
114 | ||
115 | snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry); | |
6a241483 MP |
116 | slab = kmem_cache_create(bslab->name, sz, ARCH_KMALLOC_MINALIGN, |
117 | SLAB_HWCACHE_ALIGN, NULL); | |
bb799ca0 JA |
118 | if (!slab) |
119 | goto out_unlock; | |
120 | ||
bb799ca0 JA |
121 | bslab->slab = slab; |
122 | bslab->slab_ref = 1; | |
123 | bslab->slab_size = sz; | |
124 | out_unlock: | |
125 | mutex_unlock(&bio_slab_lock); | |
126 | return slab; | |
127 | } | |
128 | ||
129 | static void bio_put_slab(struct bio_set *bs) | |
130 | { | |
131 | struct bio_slab *bslab = NULL; | |
132 | unsigned int i; | |
133 | ||
134 | mutex_lock(&bio_slab_lock); | |
135 | ||
136 | for (i = 0; i < bio_slab_nr; i++) { | |
137 | if (bs->bio_slab == bio_slabs[i].slab) { | |
138 | bslab = &bio_slabs[i]; | |
139 | break; | |
140 | } | |
141 | } | |
142 | ||
143 | if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n")) | |
144 | goto out; | |
145 | ||
146 | WARN_ON(!bslab->slab_ref); | |
147 | ||
148 | if (--bslab->slab_ref) | |
149 | goto out; | |
150 | ||
151 | kmem_cache_destroy(bslab->slab); | |
152 | bslab->slab = NULL; | |
153 | ||
154 | out: | |
155 | mutex_unlock(&bio_slab_lock); | |
156 | } | |
157 | ||
7ba1ba12 MP |
158 | unsigned int bvec_nr_vecs(unsigned short idx) |
159 | { | |
160 | return bvec_slabs[idx].nr_vecs; | |
161 | } | |
162 | ||
9f060e22 | 163 | void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx) |
bb799ca0 | 164 | { |
ed996a52 CH |
165 | if (!idx) |
166 | return; | |
167 | idx--; | |
168 | ||
169 | BIO_BUG_ON(idx >= BVEC_POOL_NR); | |
bb799ca0 | 170 | |
ed996a52 | 171 | if (idx == BVEC_POOL_MAX) { |
9f060e22 | 172 | mempool_free(bv, pool); |
ed996a52 | 173 | } else { |
bb799ca0 JA |
174 | struct biovec_slab *bvs = bvec_slabs + idx; |
175 | ||
176 | kmem_cache_free(bvs->slab, bv); | |
177 | } | |
178 | } | |
179 | ||
9f060e22 KO |
180 | struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx, |
181 | mempool_t *pool) | |
1da177e4 LT |
182 | { |
183 | struct bio_vec *bvl; | |
1da177e4 | 184 | |
7ff9345f JA |
185 | /* |
186 | * see comment near bvec_array define! | |
187 | */ | |
188 | switch (nr) { | |
189 | case 1: | |
190 | *idx = 0; | |
191 | break; | |
192 | case 2 ... 4: | |
193 | *idx = 1; | |
194 | break; | |
195 | case 5 ... 16: | |
196 | *idx = 2; | |
197 | break; | |
198 | case 17 ... 64: | |
199 | *idx = 3; | |
200 | break; | |
201 | case 65 ... 128: | |
202 | *idx = 4; | |
203 | break; | |
204 | case 129 ... BIO_MAX_PAGES: | |
205 | *idx = 5; | |
206 | break; | |
207 | default: | |
208 | return NULL; | |
209 | } | |
210 | ||
211 | /* | |
212 | * idx now points to the pool we want to allocate from. only the | |
213 | * 1-vec entry pool is mempool backed. | |
214 | */ | |
ed996a52 | 215 | if (*idx == BVEC_POOL_MAX) { |
7ff9345f | 216 | fallback: |
9f060e22 | 217 | bvl = mempool_alloc(pool, gfp_mask); |
7ff9345f JA |
218 | } else { |
219 | struct biovec_slab *bvs = bvec_slabs + *idx; | |
d0164adc | 220 | gfp_t __gfp_mask = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO); |
7ff9345f | 221 | |
0a0d96b0 | 222 | /* |
7ff9345f JA |
223 | * Make this allocation restricted and don't dump info on |
224 | * allocation failures, since we'll fallback to the mempool | |
225 | * in case of failure. | |
0a0d96b0 | 226 | */ |
7ff9345f | 227 | __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; |
1da177e4 | 228 | |
0a0d96b0 | 229 | /* |
d0164adc | 230 | * Try a slab allocation. If this fails and __GFP_DIRECT_RECLAIM |
7ff9345f | 231 | * is set, retry with the 1-entry mempool |
0a0d96b0 | 232 | */ |
7ff9345f | 233 | bvl = kmem_cache_alloc(bvs->slab, __gfp_mask); |
d0164adc | 234 | if (unlikely(!bvl && (gfp_mask & __GFP_DIRECT_RECLAIM))) { |
ed996a52 | 235 | *idx = BVEC_POOL_MAX; |
7ff9345f JA |
236 | goto fallback; |
237 | } | |
238 | } | |
239 | ||
ed996a52 | 240 | (*idx)++; |
1da177e4 LT |
241 | return bvl; |
242 | } | |
243 | ||
9ae3b3f5 | 244 | void bio_uninit(struct bio *bio) |
1da177e4 | 245 | { |
4254bba1 | 246 | bio_disassociate_task(bio); |
4254bba1 | 247 | } |
9ae3b3f5 | 248 | EXPORT_SYMBOL(bio_uninit); |
7ba1ba12 | 249 | |
4254bba1 KO |
250 | static void bio_free(struct bio *bio) |
251 | { | |
252 | struct bio_set *bs = bio->bi_pool; | |
253 | void *p; | |
254 | ||
9ae3b3f5 | 255 | bio_uninit(bio); |
4254bba1 KO |
256 | |
257 | if (bs) { | |
8aa6ba2f | 258 | bvec_free(&bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio)); |
4254bba1 KO |
259 | |
260 | /* | |
261 | * If we have front padding, adjust the bio pointer before freeing | |
262 | */ | |
263 | p = bio; | |
bb799ca0 JA |
264 | p -= bs->front_pad; |
265 | ||
8aa6ba2f | 266 | mempool_free(p, &bs->bio_pool); |
4254bba1 KO |
267 | } else { |
268 | /* Bio was allocated by bio_kmalloc() */ | |
269 | kfree(bio); | |
270 | } | |
3676347a PO |
271 | } |
272 | ||
9ae3b3f5 JA |
273 | /* |
274 | * Users of this function have their own bio allocation. Subsequently, | |
275 | * they must remember to pair any call to bio_init() with bio_uninit() | |
276 | * when IO has completed, or when the bio is released. | |
277 | */ | |
3a83f467 ML |
278 | void bio_init(struct bio *bio, struct bio_vec *table, |
279 | unsigned short max_vecs) | |
1da177e4 | 280 | { |
2b94de55 | 281 | memset(bio, 0, sizeof(*bio)); |
c4cf5261 | 282 | atomic_set(&bio->__bi_remaining, 1); |
dac56212 | 283 | atomic_set(&bio->__bi_cnt, 1); |
3a83f467 ML |
284 | |
285 | bio->bi_io_vec = table; | |
286 | bio->bi_max_vecs = max_vecs; | |
1da177e4 | 287 | } |
a112a71d | 288 | EXPORT_SYMBOL(bio_init); |
1da177e4 | 289 | |
f44b48c7 KO |
290 | /** |
291 | * bio_reset - reinitialize a bio | |
292 | * @bio: bio to reset | |
293 | * | |
294 | * Description: | |
295 | * After calling bio_reset(), @bio will be in the same state as a freshly | |
296 | * allocated bio returned bio bio_alloc_bioset() - the only fields that are | |
297 | * preserved are the ones that are initialized by bio_alloc_bioset(). See | |
298 | * comment in struct bio. | |
299 | */ | |
300 | void bio_reset(struct bio *bio) | |
301 | { | |
302 | unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS); | |
303 | ||
9ae3b3f5 | 304 | bio_uninit(bio); |
f44b48c7 KO |
305 | |
306 | memset(bio, 0, BIO_RESET_BYTES); | |
4246a0b6 | 307 | bio->bi_flags = flags; |
c4cf5261 | 308 | atomic_set(&bio->__bi_remaining, 1); |
f44b48c7 KO |
309 | } |
310 | EXPORT_SYMBOL(bio_reset); | |
311 | ||
38f8baae | 312 | static struct bio *__bio_chain_endio(struct bio *bio) |
196d38bc | 313 | { |
4246a0b6 CH |
314 | struct bio *parent = bio->bi_private; |
315 | ||
4e4cbee9 CH |
316 | if (!parent->bi_status) |
317 | parent->bi_status = bio->bi_status; | |
196d38bc | 318 | bio_put(bio); |
38f8baae CH |
319 | return parent; |
320 | } | |
321 | ||
322 | static void bio_chain_endio(struct bio *bio) | |
323 | { | |
324 | bio_endio(__bio_chain_endio(bio)); | |
196d38bc KO |
325 | } |
326 | ||
327 | /** | |
328 | * bio_chain - chain bio completions | |
1051a902 RD |
329 | * @bio: the target bio |
330 | * @parent: the @bio's parent bio | |
196d38bc KO |
331 | * |
332 | * The caller won't have a bi_end_io called when @bio completes - instead, | |
333 | * @parent's bi_end_io won't be called until both @parent and @bio have | |
334 | * completed; the chained bio will also be freed when it completes. | |
335 | * | |
336 | * The caller must not set bi_private or bi_end_io in @bio. | |
337 | */ | |
338 | void bio_chain(struct bio *bio, struct bio *parent) | |
339 | { | |
340 | BUG_ON(bio->bi_private || bio->bi_end_io); | |
341 | ||
342 | bio->bi_private = parent; | |
343 | bio->bi_end_io = bio_chain_endio; | |
c4cf5261 | 344 | bio_inc_remaining(parent); |
196d38bc KO |
345 | } |
346 | EXPORT_SYMBOL(bio_chain); | |
347 | ||
df2cb6da KO |
348 | static void bio_alloc_rescue(struct work_struct *work) |
349 | { | |
350 | struct bio_set *bs = container_of(work, struct bio_set, rescue_work); | |
351 | struct bio *bio; | |
352 | ||
353 | while (1) { | |
354 | spin_lock(&bs->rescue_lock); | |
355 | bio = bio_list_pop(&bs->rescue_list); | |
356 | spin_unlock(&bs->rescue_lock); | |
357 | ||
358 | if (!bio) | |
359 | break; | |
360 | ||
361 | generic_make_request(bio); | |
362 | } | |
363 | } | |
364 | ||
365 | static void punt_bios_to_rescuer(struct bio_set *bs) | |
366 | { | |
367 | struct bio_list punt, nopunt; | |
368 | struct bio *bio; | |
369 | ||
47e0fb46 N |
370 | if (WARN_ON_ONCE(!bs->rescue_workqueue)) |
371 | return; | |
df2cb6da KO |
372 | /* |
373 | * In order to guarantee forward progress we must punt only bios that | |
374 | * were allocated from this bio_set; otherwise, if there was a bio on | |
375 | * there for a stacking driver higher up in the stack, processing it | |
376 | * could require allocating bios from this bio_set, and doing that from | |
377 | * our own rescuer would be bad. | |
378 | * | |
379 | * Since bio lists are singly linked, pop them all instead of trying to | |
380 | * remove from the middle of the list: | |
381 | */ | |
382 | ||
383 | bio_list_init(&punt); | |
384 | bio_list_init(&nopunt); | |
385 | ||
f5fe1b51 | 386 | while ((bio = bio_list_pop(¤t->bio_list[0]))) |
df2cb6da | 387 | bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio); |
f5fe1b51 | 388 | current->bio_list[0] = nopunt; |
df2cb6da | 389 | |
f5fe1b51 N |
390 | bio_list_init(&nopunt); |
391 | while ((bio = bio_list_pop(¤t->bio_list[1]))) | |
392 | bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio); | |
393 | current->bio_list[1] = nopunt; | |
df2cb6da KO |
394 | |
395 | spin_lock(&bs->rescue_lock); | |
396 | bio_list_merge(&bs->rescue_list, &punt); | |
397 | spin_unlock(&bs->rescue_lock); | |
398 | ||
399 | queue_work(bs->rescue_workqueue, &bs->rescue_work); | |
400 | } | |
401 | ||
1da177e4 LT |
402 | /** |
403 | * bio_alloc_bioset - allocate a bio for I/O | |
519c8e9f | 404 | * @gfp_mask: the GFP_* mask given to the slab allocator |
1da177e4 | 405 | * @nr_iovecs: number of iovecs to pre-allocate |
db18efac | 406 | * @bs: the bio_set to allocate from. |
1da177e4 LT |
407 | * |
408 | * Description: | |
3f86a82a KO |
409 | * If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is |
410 | * backed by the @bs's mempool. | |
411 | * | |
d0164adc MG |
412 | * When @bs is not NULL, if %__GFP_DIRECT_RECLAIM is set then bio_alloc will |
413 | * always be able to allocate a bio. This is due to the mempool guarantees. | |
414 | * To make this work, callers must never allocate more than 1 bio at a time | |
415 | * from this pool. Callers that need to allocate more than 1 bio must always | |
416 | * submit the previously allocated bio for IO before attempting to allocate | |
417 | * a new one. Failure to do so can cause deadlocks under memory pressure. | |
3f86a82a | 418 | * |
df2cb6da KO |
419 | * Note that when running under generic_make_request() (i.e. any block |
420 | * driver), bios are not submitted until after you return - see the code in | |
421 | * generic_make_request() that converts recursion into iteration, to prevent | |
422 | * stack overflows. | |
423 | * | |
424 | * This would normally mean allocating multiple bios under | |
425 | * generic_make_request() would be susceptible to deadlocks, but we have | |
426 | * deadlock avoidance code that resubmits any blocked bios from a rescuer | |
427 | * thread. | |
428 | * | |
429 | * However, we do not guarantee forward progress for allocations from other | |
430 | * mempools. Doing multiple allocations from the same mempool under | |
431 | * generic_make_request() should be avoided - instead, use bio_set's front_pad | |
432 | * for per bio allocations. | |
433 | * | |
3f86a82a KO |
434 | * RETURNS: |
435 | * Pointer to new bio on success, NULL on failure. | |
436 | */ | |
7a88fa19 DC |
437 | struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs, |
438 | struct bio_set *bs) | |
1da177e4 | 439 | { |
df2cb6da | 440 | gfp_t saved_gfp = gfp_mask; |
3f86a82a KO |
441 | unsigned front_pad; |
442 | unsigned inline_vecs; | |
34053979 | 443 | struct bio_vec *bvl = NULL; |
451a9ebf TH |
444 | struct bio *bio; |
445 | void *p; | |
446 | ||
3f86a82a KO |
447 | if (!bs) { |
448 | if (nr_iovecs > UIO_MAXIOV) | |
449 | return NULL; | |
450 | ||
451 | p = kmalloc(sizeof(struct bio) + | |
452 | nr_iovecs * sizeof(struct bio_vec), | |
453 | gfp_mask); | |
454 | front_pad = 0; | |
455 | inline_vecs = nr_iovecs; | |
456 | } else { | |
d8f429e1 | 457 | /* should not use nobvec bioset for nr_iovecs > 0 */ |
8aa6ba2f KO |
458 | if (WARN_ON_ONCE(!mempool_initialized(&bs->bvec_pool) && |
459 | nr_iovecs > 0)) | |
d8f429e1 | 460 | return NULL; |
df2cb6da KO |
461 | /* |
462 | * generic_make_request() converts recursion to iteration; this | |
463 | * means if we're running beneath it, any bios we allocate and | |
464 | * submit will not be submitted (and thus freed) until after we | |
465 | * return. | |
466 | * | |
467 | * This exposes us to a potential deadlock if we allocate | |
468 | * multiple bios from the same bio_set() while running | |
469 | * underneath generic_make_request(). If we were to allocate | |
470 | * multiple bios (say a stacking block driver that was splitting | |
471 | * bios), we would deadlock if we exhausted the mempool's | |
472 | * reserve. | |
473 | * | |
474 | * We solve this, and guarantee forward progress, with a rescuer | |
475 | * workqueue per bio_set. If we go to allocate and there are | |
476 | * bios on current->bio_list, we first try the allocation | |
d0164adc MG |
477 | * without __GFP_DIRECT_RECLAIM; if that fails, we punt those |
478 | * bios we would be blocking to the rescuer workqueue before | |
479 | * we retry with the original gfp_flags. | |
df2cb6da KO |
480 | */ |
481 | ||
f5fe1b51 N |
482 | if (current->bio_list && |
483 | (!bio_list_empty(¤t->bio_list[0]) || | |
47e0fb46 N |
484 | !bio_list_empty(¤t->bio_list[1])) && |
485 | bs->rescue_workqueue) | |
d0164adc | 486 | gfp_mask &= ~__GFP_DIRECT_RECLAIM; |
df2cb6da | 487 | |
8aa6ba2f | 488 | p = mempool_alloc(&bs->bio_pool, gfp_mask); |
df2cb6da KO |
489 | if (!p && gfp_mask != saved_gfp) { |
490 | punt_bios_to_rescuer(bs); | |
491 | gfp_mask = saved_gfp; | |
8aa6ba2f | 492 | p = mempool_alloc(&bs->bio_pool, gfp_mask); |
df2cb6da KO |
493 | } |
494 | ||
3f86a82a KO |
495 | front_pad = bs->front_pad; |
496 | inline_vecs = BIO_INLINE_VECS; | |
497 | } | |
498 | ||
451a9ebf TH |
499 | if (unlikely(!p)) |
500 | return NULL; | |
1da177e4 | 501 | |
3f86a82a | 502 | bio = p + front_pad; |
3a83f467 | 503 | bio_init(bio, NULL, 0); |
34053979 | 504 | |
3f86a82a | 505 | if (nr_iovecs > inline_vecs) { |
ed996a52 CH |
506 | unsigned long idx = 0; |
507 | ||
8aa6ba2f | 508 | bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool); |
df2cb6da KO |
509 | if (!bvl && gfp_mask != saved_gfp) { |
510 | punt_bios_to_rescuer(bs); | |
511 | gfp_mask = saved_gfp; | |
8aa6ba2f | 512 | bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, &bs->bvec_pool); |
df2cb6da KO |
513 | } |
514 | ||
34053979 IM |
515 | if (unlikely(!bvl)) |
516 | goto err_free; | |
a38352e0 | 517 | |
ed996a52 | 518 | bio->bi_flags |= idx << BVEC_POOL_OFFSET; |
3f86a82a KO |
519 | } else if (nr_iovecs) { |
520 | bvl = bio->bi_inline_vecs; | |
1da177e4 | 521 | } |
3f86a82a KO |
522 | |
523 | bio->bi_pool = bs; | |
34053979 | 524 | bio->bi_max_vecs = nr_iovecs; |
34053979 | 525 | bio->bi_io_vec = bvl; |
1da177e4 | 526 | return bio; |
34053979 IM |
527 | |
528 | err_free: | |
8aa6ba2f | 529 | mempool_free(p, &bs->bio_pool); |
34053979 | 530 | return NULL; |
1da177e4 | 531 | } |
a112a71d | 532 | EXPORT_SYMBOL(bio_alloc_bioset); |
1da177e4 | 533 | |
38a72dac | 534 | void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start) |
1da177e4 LT |
535 | { |
536 | unsigned long flags; | |
7988613b KO |
537 | struct bio_vec bv; |
538 | struct bvec_iter iter; | |
1da177e4 | 539 | |
38a72dac | 540 | __bio_for_each_segment(bv, bio, iter, start) { |
7988613b KO |
541 | char *data = bvec_kmap_irq(&bv, &flags); |
542 | memset(data, 0, bv.bv_len); | |
543 | flush_dcache_page(bv.bv_page); | |
1da177e4 LT |
544 | bvec_kunmap_irq(data, &flags); |
545 | } | |
546 | } | |
38a72dac | 547 | EXPORT_SYMBOL(zero_fill_bio_iter); |
1da177e4 LT |
548 | |
549 | /** | |
550 | * bio_put - release a reference to a bio | |
551 | * @bio: bio to release reference to | |
552 | * | |
553 | * Description: | |
554 | * Put a reference to a &struct bio, either one you have gotten with | |
9b10f6a9 | 555 | * bio_alloc, bio_get or bio_clone_*. The last put of a bio will free it. |
1da177e4 LT |
556 | **/ |
557 | void bio_put(struct bio *bio) | |
558 | { | |
dac56212 | 559 | if (!bio_flagged(bio, BIO_REFFED)) |
4254bba1 | 560 | bio_free(bio); |
dac56212 JA |
561 | else { |
562 | BIO_BUG_ON(!atomic_read(&bio->__bi_cnt)); | |
563 | ||
564 | /* | |
565 | * last put frees it | |
566 | */ | |
567 | if (atomic_dec_and_test(&bio->__bi_cnt)) | |
568 | bio_free(bio); | |
569 | } | |
1da177e4 | 570 | } |
a112a71d | 571 | EXPORT_SYMBOL(bio_put); |
1da177e4 | 572 | |
165125e1 | 573 | inline int bio_phys_segments(struct request_queue *q, struct bio *bio) |
1da177e4 LT |
574 | { |
575 | if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) | |
576 | blk_recount_segments(q, bio); | |
577 | ||
578 | return bio->bi_phys_segments; | |
579 | } | |
a112a71d | 580 | EXPORT_SYMBOL(bio_phys_segments); |
1da177e4 | 581 | |
59d276fe KO |
582 | /** |
583 | * __bio_clone_fast - clone a bio that shares the original bio's biovec | |
584 | * @bio: destination bio | |
585 | * @bio_src: bio to clone | |
586 | * | |
587 | * Clone a &bio. Caller will own the returned bio, but not | |
588 | * the actual data it points to. Reference count of returned | |
589 | * bio will be one. | |
590 | * | |
591 | * Caller must ensure that @bio_src is not freed before @bio. | |
592 | */ | |
593 | void __bio_clone_fast(struct bio *bio, struct bio *bio_src) | |
594 | { | |
ed996a52 | 595 | BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio)); |
59d276fe KO |
596 | |
597 | /* | |
74d46992 | 598 | * most users will be overriding ->bi_disk with a new target, |
59d276fe KO |
599 | * so we don't set nor calculate new physical/hw segment counts here |
600 | */ | |
74d46992 | 601 | bio->bi_disk = bio_src->bi_disk; |
62530ed8 | 602 | bio->bi_partno = bio_src->bi_partno; |
b7c44ed9 | 603 | bio_set_flag(bio, BIO_CLONED); |
111be883 SL |
604 | if (bio_flagged(bio_src, BIO_THROTTLED)) |
605 | bio_set_flag(bio, BIO_THROTTLED); | |
1eff9d32 | 606 | bio->bi_opf = bio_src->bi_opf; |
cb6934f8 | 607 | bio->bi_write_hint = bio_src->bi_write_hint; |
59d276fe KO |
608 | bio->bi_iter = bio_src->bi_iter; |
609 | bio->bi_io_vec = bio_src->bi_io_vec; | |
20bd723e PV |
610 | |
611 | bio_clone_blkcg_association(bio, bio_src); | |
59d276fe KO |
612 | } |
613 | EXPORT_SYMBOL(__bio_clone_fast); | |
614 | ||
615 | /** | |
616 | * bio_clone_fast - clone a bio that shares the original bio's biovec | |
617 | * @bio: bio to clone | |
618 | * @gfp_mask: allocation priority | |
619 | * @bs: bio_set to allocate from | |
620 | * | |
621 | * Like __bio_clone_fast, only also allocates the returned bio | |
622 | */ | |
623 | struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs) | |
624 | { | |
625 | struct bio *b; | |
626 | ||
627 | b = bio_alloc_bioset(gfp_mask, 0, bs); | |
628 | if (!b) | |
629 | return NULL; | |
630 | ||
631 | __bio_clone_fast(b, bio); | |
632 | ||
633 | if (bio_integrity(bio)) { | |
634 | int ret; | |
635 | ||
636 | ret = bio_integrity_clone(b, bio, gfp_mask); | |
637 | ||
638 | if (ret < 0) { | |
639 | bio_put(b); | |
640 | return NULL; | |
641 | } | |
642 | } | |
643 | ||
644 | return b; | |
645 | } | |
646 | EXPORT_SYMBOL(bio_clone_fast); | |
647 | ||
f4595875 SL |
648 | /** |
649 | * bio_clone_bioset - clone a bio | |
650 | * @bio_src: bio to clone | |
651 | * @gfp_mask: allocation priority | |
652 | * @bs: bio_set to allocate from | |
653 | * | |
654 | * Clone bio. Caller will own the returned bio, but not the actual data it | |
655 | * points to. Reference count of returned bio will be one. | |
656 | */ | |
657 | struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask, | |
658 | struct bio_set *bs) | |
1da177e4 | 659 | { |
bdb53207 KO |
660 | struct bvec_iter iter; |
661 | struct bio_vec bv; | |
662 | struct bio *bio; | |
1da177e4 | 663 | |
bdb53207 KO |
664 | /* |
665 | * Pre immutable biovecs, __bio_clone() used to just do a memcpy from | |
666 | * bio_src->bi_io_vec to bio->bi_io_vec. | |
667 | * | |
668 | * We can't do that anymore, because: | |
669 | * | |
670 | * - The point of cloning the biovec is to produce a bio with a biovec | |
671 | * the caller can modify: bi_idx and bi_bvec_done should be 0. | |
672 | * | |
673 | * - The original bio could've had more than BIO_MAX_PAGES biovecs; if | |
674 | * we tried to clone the whole thing bio_alloc_bioset() would fail. | |
675 | * But the clone should succeed as long as the number of biovecs we | |
676 | * actually need to allocate is fewer than BIO_MAX_PAGES. | |
677 | * | |
678 | * - Lastly, bi_vcnt should not be looked at or relied upon by code | |
679 | * that does not own the bio - reason being drivers don't use it for | |
680 | * iterating over the biovec anymore, so expecting it to be kept up | |
681 | * to date (i.e. for clones that share the parent biovec) is just | |
682 | * asking for trouble and would force extra work on | |
683 | * __bio_clone_fast() anyways. | |
684 | */ | |
685 | ||
f4595875 | 686 | bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs); |
bdb53207 | 687 | if (!bio) |
7ba1ba12 | 688 | return NULL; |
74d46992 | 689 | bio->bi_disk = bio_src->bi_disk; |
1eff9d32 | 690 | bio->bi_opf = bio_src->bi_opf; |
cb6934f8 | 691 | bio->bi_write_hint = bio_src->bi_write_hint; |
bdb53207 KO |
692 | bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; |
693 | bio->bi_iter.bi_size = bio_src->bi_iter.bi_size; | |
7ba1ba12 | 694 | |
7afafc8a AH |
695 | switch (bio_op(bio)) { |
696 | case REQ_OP_DISCARD: | |
697 | case REQ_OP_SECURE_ERASE: | |
a6f0788e | 698 | case REQ_OP_WRITE_ZEROES: |
7afafc8a AH |
699 | break; |
700 | case REQ_OP_WRITE_SAME: | |
8423ae3d | 701 | bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0]; |
7afafc8a AH |
702 | break; |
703 | default: | |
f4595875 | 704 | bio_for_each_segment(bv, bio_src, iter) |
7afafc8a AH |
705 | bio->bi_io_vec[bio->bi_vcnt++] = bv; |
706 | break; | |
8423ae3d KO |
707 | } |
708 | ||
bdb53207 KO |
709 | if (bio_integrity(bio_src)) { |
710 | int ret; | |
7ba1ba12 | 711 | |
bdb53207 | 712 | ret = bio_integrity_clone(bio, bio_src, gfp_mask); |
059ea331 | 713 | if (ret < 0) { |
bdb53207 | 714 | bio_put(bio); |
7ba1ba12 | 715 | return NULL; |
059ea331 | 716 | } |
3676347a | 717 | } |
1da177e4 | 718 | |
20bd723e PV |
719 | bio_clone_blkcg_association(bio, bio_src); |
720 | ||
bdb53207 | 721 | return bio; |
1da177e4 | 722 | } |
bf800ef1 | 723 | EXPORT_SYMBOL(bio_clone_bioset); |
1da177e4 LT |
724 | |
725 | /** | |
c66a14d0 KO |
726 | * bio_add_pc_page - attempt to add page to bio |
727 | * @q: the target queue | |
728 | * @bio: destination bio | |
729 | * @page: page to add | |
730 | * @len: vec entry length | |
731 | * @offset: vec entry offset | |
1da177e4 | 732 | * |
c66a14d0 KO |
733 | * Attempt to add a page to the bio_vec maplist. This can fail for a |
734 | * number of reasons, such as the bio being full or target block device | |
735 | * limitations. The target block device must allow bio's up to PAGE_SIZE, | |
736 | * so it is always possible to add a single page to an empty bio. | |
737 | * | |
738 | * This should only be used by REQ_PC bios. | |
1da177e4 | 739 | */ |
c66a14d0 KO |
740 | int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page |
741 | *page, unsigned int len, unsigned int offset) | |
1da177e4 LT |
742 | { |
743 | int retried_segments = 0; | |
744 | struct bio_vec *bvec; | |
745 | ||
746 | /* | |
747 | * cloned bio must not modify vec list | |
748 | */ | |
749 | if (unlikely(bio_flagged(bio, BIO_CLONED))) | |
750 | return 0; | |
751 | ||
c66a14d0 | 752 | if (((bio->bi_iter.bi_size + len) >> 9) > queue_max_hw_sectors(q)) |
1da177e4 LT |
753 | return 0; |
754 | ||
80cfd548 JA |
755 | /* |
756 | * For filesystems with a blocksize smaller than the pagesize | |
757 | * we will often be called with the same page as last time and | |
758 | * a consecutive offset. Optimize this special case. | |
759 | */ | |
760 | if (bio->bi_vcnt > 0) { | |
761 | struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; | |
762 | ||
763 | if (page == prev->bv_page && | |
764 | offset == prev->bv_offset + prev->bv_len) { | |
765 | prev->bv_len += len; | |
fcbf6a08 | 766 | bio->bi_iter.bi_size += len; |
80cfd548 JA |
767 | goto done; |
768 | } | |
66cb45aa JA |
769 | |
770 | /* | |
771 | * If the queue doesn't support SG gaps and adding this | |
772 | * offset would create a gap, disallow it. | |
773 | */ | |
03100aad | 774 | if (bvec_gap_to_prev(q, prev, offset)) |
66cb45aa | 775 | return 0; |
80cfd548 JA |
776 | } |
777 | ||
0aa69fd3 | 778 | if (bio_full(bio)) |
1da177e4 LT |
779 | return 0; |
780 | ||
781 | /* | |
fcbf6a08 ML |
782 | * setup the new entry, we might clear it again later if we |
783 | * cannot add the page | |
784 | */ | |
785 | bvec = &bio->bi_io_vec[bio->bi_vcnt]; | |
786 | bvec->bv_page = page; | |
787 | bvec->bv_len = len; | |
788 | bvec->bv_offset = offset; | |
789 | bio->bi_vcnt++; | |
790 | bio->bi_phys_segments++; | |
791 | bio->bi_iter.bi_size += len; | |
792 | ||
793 | /* | |
794 | * Perform a recount if the number of segments is greater | |
795 | * than queue_max_segments(q). | |
1da177e4 LT |
796 | */ |
797 | ||
fcbf6a08 | 798 | while (bio->bi_phys_segments > queue_max_segments(q)) { |
1da177e4 LT |
799 | |
800 | if (retried_segments) | |
fcbf6a08 | 801 | goto failed; |
1da177e4 LT |
802 | |
803 | retried_segments = 1; | |
804 | blk_recount_segments(q, bio); | |
805 | } | |
806 | ||
1da177e4 | 807 | /* If we may be able to merge these biovecs, force a recount */ |
fcbf6a08 | 808 | if (bio->bi_vcnt > 1 && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec))) |
b7c44ed9 | 809 | bio_clear_flag(bio, BIO_SEG_VALID); |
1da177e4 | 810 | |
80cfd548 | 811 | done: |
1da177e4 | 812 | return len; |
fcbf6a08 ML |
813 | |
814 | failed: | |
815 | bvec->bv_page = NULL; | |
816 | bvec->bv_len = 0; | |
817 | bvec->bv_offset = 0; | |
818 | bio->bi_vcnt--; | |
819 | bio->bi_iter.bi_size -= len; | |
820 | blk_recount_segments(q, bio); | |
821 | return 0; | |
1da177e4 | 822 | } |
a112a71d | 823 | EXPORT_SYMBOL(bio_add_pc_page); |
6e68af66 | 824 | |
1da177e4 | 825 | /** |
0aa69fd3 CH |
826 | * __bio_try_merge_page - try appending data to an existing bvec. |
827 | * @bio: destination bio | |
828 | * @page: page to add | |
829 | * @len: length of the data to add | |
830 | * @off: offset of the data in @page | |
1da177e4 | 831 | * |
0aa69fd3 CH |
832 | * Try to add the data at @page + @off to the last bvec of @bio. This is a |
833 | * a useful optimisation for file systems with a block size smaller than the | |
834 | * page size. | |
835 | * | |
836 | * Return %true on success or %false on failure. | |
1da177e4 | 837 | */ |
0aa69fd3 CH |
838 | bool __bio_try_merge_page(struct bio *bio, struct page *page, |
839 | unsigned int len, unsigned int off) | |
1da177e4 | 840 | { |
c66a14d0 | 841 | if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED))) |
0aa69fd3 | 842 | return false; |
762380ad | 843 | |
c66a14d0 | 844 | if (bio->bi_vcnt > 0) { |
0aa69fd3 | 845 | struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
58a4915a | 846 | |
0aa69fd3 | 847 | if (page == bv->bv_page && off == bv->bv_offset + bv->bv_len) { |
c66a14d0 | 848 | bv->bv_len += len; |
0aa69fd3 CH |
849 | bio->bi_iter.bi_size += len; |
850 | return true; | |
c66a14d0 KO |
851 | } |
852 | } | |
0aa69fd3 CH |
853 | return false; |
854 | } | |
855 | EXPORT_SYMBOL_GPL(__bio_try_merge_page); | |
c66a14d0 | 856 | |
0aa69fd3 CH |
857 | /** |
858 | * __bio_add_page - add page to a bio in a new segment | |
859 | * @bio: destination bio | |
860 | * @page: page to add | |
861 | * @len: length of the data to add | |
862 | * @off: offset of the data in @page | |
863 | * | |
864 | * Add the data at @page + @off to @bio as a new bvec. The caller must ensure | |
865 | * that @bio has space for another bvec. | |
866 | */ | |
867 | void __bio_add_page(struct bio *bio, struct page *page, | |
868 | unsigned int len, unsigned int off) | |
869 | { | |
870 | struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt]; | |
c66a14d0 | 871 | |
0aa69fd3 CH |
872 | WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); |
873 | WARN_ON_ONCE(bio_full(bio)); | |
874 | ||
875 | bv->bv_page = page; | |
876 | bv->bv_offset = off; | |
877 | bv->bv_len = len; | |
c66a14d0 | 878 | |
c66a14d0 | 879 | bio->bi_iter.bi_size += len; |
0aa69fd3 CH |
880 | bio->bi_vcnt++; |
881 | } | |
882 | EXPORT_SYMBOL_GPL(__bio_add_page); | |
883 | ||
884 | /** | |
885 | * bio_add_page - attempt to add page to bio | |
886 | * @bio: destination bio | |
887 | * @page: page to add | |
888 | * @len: vec entry length | |
889 | * @offset: vec entry offset | |
890 | * | |
891 | * Attempt to add a page to the bio_vec maplist. This will only fail | |
892 | * if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio. | |
893 | */ | |
894 | int bio_add_page(struct bio *bio, struct page *page, | |
895 | unsigned int len, unsigned int offset) | |
896 | { | |
897 | if (!__bio_try_merge_page(bio, page, len, offset)) { | |
898 | if (bio_full(bio)) | |
899 | return 0; | |
900 | __bio_add_page(bio, page, len, offset); | |
901 | } | |
c66a14d0 | 902 | return len; |
1da177e4 | 903 | } |
a112a71d | 904 | EXPORT_SYMBOL(bio_add_page); |
1da177e4 | 905 | |
2cefe4db KO |
906 | /** |
907 | * bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio | |
908 | * @bio: bio to add pages to | |
909 | * @iter: iov iterator describing the region to be mapped | |
910 | * | |
911 | * Pins as many pages from *iter and appends them to @bio's bvec array. The | |
912 | * pages will have to be released using put_page() when done. | |
913 | */ | |
914 | int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) | |
915 | { | |
916 | unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt; | |
917 | struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt; | |
918 | struct page **pages = (struct page **)bv; | |
919 | size_t offset, diff; | |
920 | ssize_t size; | |
921 | ||
922 | size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset); | |
923 | if (unlikely(size <= 0)) | |
924 | return size ? size : -EFAULT; | |
925 | nr_pages = (size + offset + PAGE_SIZE - 1) / PAGE_SIZE; | |
926 | ||
927 | /* | |
928 | * Deep magic below: We need to walk the pinned pages backwards | |
929 | * because we are abusing the space allocated for the bio_vecs | |
930 | * for the page array. Because the bio_vecs are larger than the | |
931 | * page pointers by definition this will always work. But it also | |
932 | * means we can't use bio_add_page, so any changes to it's semantics | |
933 | * need to be reflected here as well. | |
934 | */ | |
935 | bio->bi_iter.bi_size += size; | |
936 | bio->bi_vcnt += nr_pages; | |
937 | ||
938 | diff = (nr_pages * PAGE_SIZE - offset) - size; | |
939 | while (nr_pages--) { | |
940 | bv[nr_pages].bv_page = pages[nr_pages]; | |
941 | bv[nr_pages].bv_len = PAGE_SIZE; | |
942 | bv[nr_pages].bv_offset = 0; | |
943 | } | |
944 | ||
945 | bv[0].bv_offset += offset; | |
946 | bv[0].bv_len -= offset; | |
947 | if (diff) | |
948 | bv[bio->bi_vcnt - 1].bv_len -= diff; | |
949 | ||
950 | iov_iter_advance(iter, size); | |
951 | return 0; | |
952 | } | |
953 | EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages); | |
954 | ||
4246a0b6 | 955 | static void submit_bio_wait_endio(struct bio *bio) |
9e882242 | 956 | { |
65e53aab | 957 | complete(bio->bi_private); |
9e882242 KO |
958 | } |
959 | ||
960 | /** | |
961 | * submit_bio_wait - submit a bio, and wait until it completes | |
9e882242 KO |
962 | * @bio: The &struct bio which describes the I/O |
963 | * | |
964 | * Simple wrapper around submit_bio(). Returns 0 on success, or the error from | |
965 | * bio_endio() on failure. | |
3d289d68 JK |
966 | * |
967 | * WARNING: Unlike to how submit_bio() is usually used, this function does not | |
968 | * result in bio reference to be consumed. The caller must drop the reference | |
969 | * on his own. | |
9e882242 | 970 | */ |
4e49ea4a | 971 | int submit_bio_wait(struct bio *bio) |
9e882242 | 972 | { |
e319e1fb | 973 | DECLARE_COMPLETION_ONSTACK_MAP(done, bio->bi_disk->lockdep_map); |
9e882242 | 974 | |
65e53aab | 975 | bio->bi_private = &done; |
9e882242 | 976 | bio->bi_end_io = submit_bio_wait_endio; |
1eff9d32 | 977 | bio->bi_opf |= REQ_SYNC; |
4e49ea4a | 978 | submit_bio(bio); |
65e53aab | 979 | wait_for_completion_io(&done); |
9e882242 | 980 | |
65e53aab | 981 | return blk_status_to_errno(bio->bi_status); |
9e882242 KO |
982 | } |
983 | EXPORT_SYMBOL(submit_bio_wait); | |
984 | ||
054bdf64 KO |
985 | /** |
986 | * bio_advance - increment/complete a bio by some number of bytes | |
987 | * @bio: bio to advance | |
988 | * @bytes: number of bytes to complete | |
989 | * | |
990 | * This updates bi_sector, bi_size and bi_idx; if the number of bytes to | |
991 | * complete doesn't align with a bvec boundary, then bv_len and bv_offset will | |
992 | * be updated on the last bvec as well. | |
993 | * | |
994 | * @bio will then represent the remaining, uncompleted portion of the io. | |
995 | */ | |
996 | void bio_advance(struct bio *bio, unsigned bytes) | |
997 | { | |
998 | if (bio_integrity(bio)) | |
999 | bio_integrity_advance(bio, bytes); | |
1000 | ||
4550dd6c | 1001 | bio_advance_iter(bio, &bio->bi_iter, bytes); |
054bdf64 KO |
1002 | } |
1003 | EXPORT_SYMBOL(bio_advance); | |
1004 | ||
45db54d5 KO |
1005 | void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter, |
1006 | struct bio *src, struct bvec_iter *src_iter) | |
16ac3d63 | 1007 | { |
1cb9dda4 | 1008 | struct bio_vec src_bv, dst_bv; |
16ac3d63 | 1009 | void *src_p, *dst_p; |
1cb9dda4 | 1010 | unsigned bytes; |
16ac3d63 | 1011 | |
45db54d5 KO |
1012 | while (src_iter->bi_size && dst_iter->bi_size) { |
1013 | src_bv = bio_iter_iovec(src, *src_iter); | |
1014 | dst_bv = bio_iter_iovec(dst, *dst_iter); | |
1cb9dda4 KO |
1015 | |
1016 | bytes = min(src_bv.bv_len, dst_bv.bv_len); | |
16ac3d63 | 1017 | |
1cb9dda4 KO |
1018 | src_p = kmap_atomic(src_bv.bv_page); |
1019 | dst_p = kmap_atomic(dst_bv.bv_page); | |
16ac3d63 | 1020 | |
1cb9dda4 KO |
1021 | memcpy(dst_p + dst_bv.bv_offset, |
1022 | src_p + src_bv.bv_offset, | |
16ac3d63 KO |
1023 | bytes); |
1024 | ||
1025 | kunmap_atomic(dst_p); | |
1026 | kunmap_atomic(src_p); | |
1027 | ||
6e6e811d KO |
1028 | flush_dcache_page(dst_bv.bv_page); |
1029 | ||
45db54d5 KO |
1030 | bio_advance_iter(src, src_iter, bytes); |
1031 | bio_advance_iter(dst, dst_iter, bytes); | |
16ac3d63 KO |
1032 | } |
1033 | } | |
38a72dac KO |
1034 | EXPORT_SYMBOL(bio_copy_data_iter); |
1035 | ||
1036 | /** | |
45db54d5 KO |
1037 | * bio_copy_data - copy contents of data buffers from one bio to another |
1038 | * @src: source bio | |
1039 | * @dst: destination bio | |
38a72dac KO |
1040 | * |
1041 | * Stops when it reaches the end of either @src or @dst - that is, copies | |
1042 | * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios). | |
1043 | */ | |
1044 | void bio_copy_data(struct bio *dst, struct bio *src) | |
1045 | { | |
45db54d5 KO |
1046 | struct bvec_iter src_iter = src->bi_iter; |
1047 | struct bvec_iter dst_iter = dst->bi_iter; | |
1048 | ||
1049 | bio_copy_data_iter(dst, &dst_iter, src, &src_iter); | |
38a72dac | 1050 | } |
16ac3d63 KO |
1051 | EXPORT_SYMBOL(bio_copy_data); |
1052 | ||
45db54d5 KO |
1053 | /** |
1054 | * bio_list_copy_data - copy contents of data buffers from one chain of bios to | |
1055 | * another | |
1056 | * @src: source bio list | |
1057 | * @dst: destination bio list | |
1058 | * | |
1059 | * Stops when it reaches the end of either the @src list or @dst list - that is, | |
1060 | * copies min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of | |
1061 | * bios). | |
1062 | */ | |
1063 | void bio_list_copy_data(struct bio *dst, struct bio *src) | |
1064 | { | |
1065 | struct bvec_iter src_iter = src->bi_iter; | |
1066 | struct bvec_iter dst_iter = dst->bi_iter; | |
1067 | ||
1068 | while (1) { | |
1069 | if (!src_iter.bi_size) { | |
1070 | src = src->bi_next; | |
1071 | if (!src) | |
1072 | break; | |
1073 | ||
1074 | src_iter = src->bi_iter; | |
1075 | } | |
1076 | ||
1077 | if (!dst_iter.bi_size) { | |
1078 | dst = dst->bi_next; | |
1079 | if (!dst) | |
1080 | break; | |
1081 | ||
1082 | dst_iter = dst->bi_iter; | |
1083 | } | |
1084 | ||
1085 | bio_copy_data_iter(dst, &dst_iter, src, &src_iter); | |
1086 | } | |
1087 | } | |
1088 | EXPORT_SYMBOL(bio_list_copy_data); | |
1089 | ||
1da177e4 | 1090 | struct bio_map_data { |
152e283f | 1091 | int is_our_pages; |
26e49cfc KO |
1092 | struct iov_iter iter; |
1093 | struct iovec iov[]; | |
1da177e4 LT |
1094 | }; |
1095 | ||
0e5b935d | 1096 | static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data, |
76029ff3 | 1097 | gfp_t gfp_mask) |
1da177e4 | 1098 | { |
0e5b935d AV |
1099 | struct bio_map_data *bmd; |
1100 | if (data->nr_segs > UIO_MAXIOV) | |
f3f63c1c | 1101 | return NULL; |
1da177e4 | 1102 | |
0e5b935d AV |
1103 | bmd = kmalloc(sizeof(struct bio_map_data) + |
1104 | sizeof(struct iovec) * data->nr_segs, gfp_mask); | |
1105 | if (!bmd) | |
1106 | return NULL; | |
1107 | memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs); | |
1108 | bmd->iter = *data; | |
1109 | bmd->iter.iov = bmd->iov; | |
1110 | return bmd; | |
1da177e4 LT |
1111 | } |
1112 | ||
9124d3fe DP |
1113 | /** |
1114 | * bio_copy_from_iter - copy all pages from iov_iter to bio | |
1115 | * @bio: The &struct bio which describes the I/O as destination | |
1116 | * @iter: iov_iter as source | |
1117 | * | |
1118 | * Copy all pages from iov_iter to bio. | |
1119 | * Returns 0 on success, or error on failure. | |
1120 | */ | |
98a09d61 | 1121 | static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter) |
c5dec1c3 | 1122 | { |
9124d3fe | 1123 | int i; |
c5dec1c3 | 1124 | struct bio_vec *bvec; |
c5dec1c3 | 1125 | |
d74c6d51 | 1126 | bio_for_each_segment_all(bvec, bio, i) { |
9124d3fe | 1127 | ssize_t ret; |
c5dec1c3 | 1128 | |
9124d3fe DP |
1129 | ret = copy_page_from_iter(bvec->bv_page, |
1130 | bvec->bv_offset, | |
1131 | bvec->bv_len, | |
98a09d61 | 1132 | iter); |
9124d3fe | 1133 | |
98a09d61 | 1134 | if (!iov_iter_count(iter)) |
9124d3fe DP |
1135 | break; |
1136 | ||
1137 | if (ret < bvec->bv_len) | |
1138 | return -EFAULT; | |
c5dec1c3 FT |
1139 | } |
1140 | ||
9124d3fe DP |
1141 | return 0; |
1142 | } | |
1143 | ||
1144 | /** | |
1145 | * bio_copy_to_iter - copy all pages from bio to iov_iter | |
1146 | * @bio: The &struct bio which describes the I/O as source | |
1147 | * @iter: iov_iter as destination | |
1148 | * | |
1149 | * Copy all pages from bio to iov_iter. | |
1150 | * Returns 0 on success, or error on failure. | |
1151 | */ | |
1152 | static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter) | |
1153 | { | |
1154 | int i; | |
1155 | struct bio_vec *bvec; | |
1156 | ||
1157 | bio_for_each_segment_all(bvec, bio, i) { | |
1158 | ssize_t ret; | |
1159 | ||
1160 | ret = copy_page_to_iter(bvec->bv_page, | |
1161 | bvec->bv_offset, | |
1162 | bvec->bv_len, | |
1163 | &iter); | |
1164 | ||
1165 | if (!iov_iter_count(&iter)) | |
1166 | break; | |
1167 | ||
1168 | if (ret < bvec->bv_len) | |
1169 | return -EFAULT; | |
1170 | } | |
1171 | ||
1172 | return 0; | |
c5dec1c3 FT |
1173 | } |
1174 | ||
491221f8 | 1175 | void bio_free_pages(struct bio *bio) |
1dfa0f68 CH |
1176 | { |
1177 | struct bio_vec *bvec; | |
1178 | int i; | |
1179 | ||
1180 | bio_for_each_segment_all(bvec, bio, i) | |
1181 | __free_page(bvec->bv_page); | |
1182 | } | |
491221f8 | 1183 | EXPORT_SYMBOL(bio_free_pages); |
1dfa0f68 | 1184 | |
1da177e4 LT |
1185 | /** |
1186 | * bio_uncopy_user - finish previously mapped bio | |
1187 | * @bio: bio being terminated | |
1188 | * | |
ddad8dd0 | 1189 | * Free pages allocated from bio_copy_user_iov() and write back data |
1da177e4 LT |
1190 | * to user space in case of a read. |
1191 | */ | |
1192 | int bio_uncopy_user(struct bio *bio) | |
1193 | { | |
1194 | struct bio_map_data *bmd = bio->bi_private; | |
1dfa0f68 | 1195 | int ret = 0; |
1da177e4 | 1196 | |
35dc2483 RD |
1197 | if (!bio_flagged(bio, BIO_NULL_MAPPED)) { |
1198 | /* | |
1199 | * if we're in a workqueue, the request is orphaned, so | |
2d99b55d HR |
1200 | * don't copy into a random user address space, just free |
1201 | * and return -EINTR so user space doesn't expect any data. | |
35dc2483 | 1202 | */ |
2d99b55d HR |
1203 | if (!current->mm) |
1204 | ret = -EINTR; | |
1205 | else if (bio_data_dir(bio) == READ) | |
9124d3fe | 1206 | ret = bio_copy_to_iter(bio, bmd->iter); |
1dfa0f68 CH |
1207 | if (bmd->is_our_pages) |
1208 | bio_free_pages(bio); | |
35dc2483 | 1209 | } |
c8db4448 | 1210 | kfree(bmd); |
1da177e4 LT |
1211 | bio_put(bio); |
1212 | return ret; | |
1213 | } | |
1214 | ||
1215 | /** | |
c5dec1c3 | 1216 | * bio_copy_user_iov - copy user data to bio |
26e49cfc KO |
1217 | * @q: destination block queue |
1218 | * @map_data: pointer to the rq_map_data holding pages (if necessary) | |
1219 | * @iter: iovec iterator | |
1220 | * @gfp_mask: memory allocation flags | |
1da177e4 LT |
1221 | * |
1222 | * Prepares and returns a bio for indirect user io, bouncing data | |
1223 | * to/from kernel pages as necessary. Must be paired with | |
1224 | * call bio_uncopy_user() on io completion. | |
1225 | */ | |
152e283f FT |
1226 | struct bio *bio_copy_user_iov(struct request_queue *q, |
1227 | struct rq_map_data *map_data, | |
e81cef5d | 1228 | struct iov_iter *iter, |
26e49cfc | 1229 | gfp_t gfp_mask) |
1da177e4 | 1230 | { |
1da177e4 | 1231 | struct bio_map_data *bmd; |
1da177e4 LT |
1232 | struct page *page; |
1233 | struct bio *bio; | |
d16d44eb AV |
1234 | int i = 0, ret; |
1235 | int nr_pages; | |
26e49cfc | 1236 | unsigned int len = iter->count; |
bd5cecea | 1237 | unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0; |
1da177e4 | 1238 | |
0e5b935d | 1239 | bmd = bio_alloc_map_data(iter, gfp_mask); |
1da177e4 LT |
1240 | if (!bmd) |
1241 | return ERR_PTR(-ENOMEM); | |
1242 | ||
26e49cfc KO |
1243 | /* |
1244 | * We need to do a deep copy of the iov_iter including the iovecs. | |
1245 | * The caller provided iov might point to an on-stack or otherwise | |
1246 | * shortlived one. | |
1247 | */ | |
1248 | bmd->is_our_pages = map_data ? 0 : 1; | |
26e49cfc | 1249 | |
d16d44eb AV |
1250 | nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE); |
1251 | if (nr_pages > BIO_MAX_PAGES) | |
1252 | nr_pages = BIO_MAX_PAGES; | |
26e49cfc | 1253 | |
1da177e4 | 1254 | ret = -ENOMEM; |
a9e9dc24 | 1255 | bio = bio_kmalloc(gfp_mask, nr_pages); |
1da177e4 LT |
1256 | if (!bio) |
1257 | goto out_bmd; | |
1258 | ||
1da177e4 | 1259 | ret = 0; |
56c451f4 FT |
1260 | |
1261 | if (map_data) { | |
e623ddb4 | 1262 | nr_pages = 1 << map_data->page_order; |
56c451f4 FT |
1263 | i = map_data->offset / PAGE_SIZE; |
1264 | } | |
1da177e4 | 1265 | while (len) { |
e623ddb4 | 1266 | unsigned int bytes = PAGE_SIZE; |
1da177e4 | 1267 | |
56c451f4 FT |
1268 | bytes -= offset; |
1269 | ||
1da177e4 LT |
1270 | if (bytes > len) |
1271 | bytes = len; | |
1272 | ||
152e283f | 1273 | if (map_data) { |
e623ddb4 | 1274 | if (i == map_data->nr_entries * nr_pages) { |
152e283f FT |
1275 | ret = -ENOMEM; |
1276 | break; | |
1277 | } | |
e623ddb4 FT |
1278 | |
1279 | page = map_data->pages[i / nr_pages]; | |
1280 | page += (i % nr_pages); | |
1281 | ||
1282 | i++; | |
1283 | } else { | |
152e283f | 1284 | page = alloc_page(q->bounce_gfp | gfp_mask); |
e623ddb4 FT |
1285 | if (!page) { |
1286 | ret = -ENOMEM; | |
1287 | break; | |
1288 | } | |
1da177e4 LT |
1289 | } |
1290 | ||
56c451f4 | 1291 | if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes) |
1da177e4 | 1292 | break; |
1da177e4 LT |
1293 | |
1294 | len -= bytes; | |
56c451f4 | 1295 | offset = 0; |
1da177e4 LT |
1296 | } |
1297 | ||
1298 | if (ret) | |
1299 | goto cleanup; | |
1300 | ||
2884d0be AV |
1301 | if (map_data) |
1302 | map_data->offset += bio->bi_iter.bi_size; | |
1303 | ||
1da177e4 LT |
1304 | /* |
1305 | * success | |
1306 | */ | |
26e49cfc | 1307 | if (((iter->type & WRITE) && (!map_data || !map_data->null_mapped)) || |
ecb554a8 | 1308 | (map_data && map_data->from_user)) { |
98a09d61 | 1309 | ret = bio_copy_from_iter(bio, iter); |
c5dec1c3 FT |
1310 | if (ret) |
1311 | goto cleanup; | |
98a09d61 AV |
1312 | } else { |
1313 | iov_iter_advance(iter, bio->bi_iter.bi_size); | |
1da177e4 LT |
1314 | } |
1315 | ||
26e49cfc | 1316 | bio->bi_private = bmd; |
2884d0be AV |
1317 | if (map_data && map_data->null_mapped) |
1318 | bio_set_flag(bio, BIO_NULL_MAPPED); | |
1da177e4 LT |
1319 | return bio; |
1320 | cleanup: | |
152e283f | 1321 | if (!map_data) |
1dfa0f68 | 1322 | bio_free_pages(bio); |
1da177e4 LT |
1323 | bio_put(bio); |
1324 | out_bmd: | |
c8db4448 | 1325 | kfree(bmd); |
1da177e4 LT |
1326 | return ERR_PTR(ret); |
1327 | } | |
1328 | ||
37f19e57 CH |
1329 | /** |
1330 | * bio_map_user_iov - map user iovec into bio | |
1331 | * @q: the struct request_queue for the bio | |
1332 | * @iter: iovec iterator | |
1333 | * @gfp_mask: memory allocation flags | |
1334 | * | |
1335 | * Map the user space address into a bio suitable for io to a block | |
1336 | * device. Returns an error pointer in case of error. | |
1337 | */ | |
1338 | struct bio *bio_map_user_iov(struct request_queue *q, | |
e81cef5d | 1339 | struct iov_iter *iter, |
37f19e57 | 1340 | gfp_t gfp_mask) |
1da177e4 | 1341 | { |
26e49cfc | 1342 | int j; |
1da177e4 | 1343 | struct bio *bio; |
076098e5 | 1344 | int ret; |
2b04e8f6 | 1345 | struct bio_vec *bvec; |
1da177e4 | 1346 | |
b282cc76 | 1347 | if (!iov_iter_count(iter)) |
1da177e4 LT |
1348 | return ERR_PTR(-EINVAL); |
1349 | ||
b282cc76 | 1350 | bio = bio_kmalloc(gfp_mask, iov_iter_npages(iter, BIO_MAX_PAGES)); |
1da177e4 LT |
1351 | if (!bio) |
1352 | return ERR_PTR(-ENOMEM); | |
1353 | ||
0a0f1513 | 1354 | while (iov_iter_count(iter)) { |
629e42bc | 1355 | struct page **pages; |
076098e5 AV |
1356 | ssize_t bytes; |
1357 | size_t offs, added = 0; | |
1358 | int npages; | |
1da177e4 | 1359 | |
0a0f1513 | 1360 | bytes = iov_iter_get_pages_alloc(iter, &pages, LONG_MAX, &offs); |
076098e5 AV |
1361 | if (unlikely(bytes <= 0)) { |
1362 | ret = bytes ? bytes : -EFAULT; | |
f1970baf | 1363 | goto out_unmap; |
99172157 | 1364 | } |
f1970baf | 1365 | |
076098e5 | 1366 | npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE); |
f1970baf | 1367 | |
98f0bc99 AV |
1368 | if (unlikely(offs & queue_dma_alignment(q))) { |
1369 | ret = -EINVAL; | |
1370 | j = 0; | |
1371 | } else { | |
1372 | for (j = 0; j < npages; j++) { | |
1373 | struct page *page = pages[j]; | |
1374 | unsigned int n = PAGE_SIZE - offs; | |
1375 | unsigned short prev_bi_vcnt = bio->bi_vcnt; | |
f1970baf | 1376 | |
98f0bc99 AV |
1377 | if (n > bytes) |
1378 | n = bytes; | |
95d78c28 | 1379 | |
98f0bc99 AV |
1380 | if (!bio_add_pc_page(q, bio, page, n, offs)) |
1381 | break; | |
1da177e4 | 1382 | |
98f0bc99 AV |
1383 | /* |
1384 | * check if vector was merged with previous | |
1385 | * drop page reference if needed | |
1386 | */ | |
1387 | if (bio->bi_vcnt == prev_bi_vcnt) | |
1388 | put_page(page); | |
1389 | ||
1390 | added += n; | |
1391 | bytes -= n; | |
1392 | offs = 0; | |
1393 | } | |
0a0f1513 | 1394 | iov_iter_advance(iter, added); |
f1970baf | 1395 | } |
1da177e4 | 1396 | /* |
f1970baf | 1397 | * release the pages we didn't map into the bio, if any |
1da177e4 | 1398 | */ |
629e42bc | 1399 | while (j < npages) |
09cbfeaf | 1400 | put_page(pages[j++]); |
629e42bc | 1401 | kvfree(pages); |
e2e115d1 AV |
1402 | /* couldn't stuff something into bio? */ |
1403 | if (bytes) | |
1404 | break; | |
1da177e4 LT |
1405 | } |
1406 | ||
b7c44ed9 | 1407 | bio_set_flag(bio, BIO_USER_MAPPED); |
37f19e57 CH |
1408 | |
1409 | /* | |
5fad1b64 | 1410 | * subtle -- if bio_map_user_iov() ended up bouncing a bio, |
37f19e57 CH |
1411 | * it would normally disappear when its bi_end_io is run. |
1412 | * however, we need it for the unmap, so grab an extra | |
1413 | * reference to it | |
1414 | */ | |
1415 | bio_get(bio); | |
1da177e4 | 1416 | return bio; |
f1970baf JB |
1417 | |
1418 | out_unmap: | |
2b04e8f6 AV |
1419 | bio_for_each_segment_all(bvec, bio, j) { |
1420 | put_page(bvec->bv_page); | |
f1970baf | 1421 | } |
1da177e4 LT |
1422 | bio_put(bio); |
1423 | return ERR_PTR(ret); | |
1424 | } | |
1425 | ||
1da177e4 LT |
1426 | static void __bio_unmap_user(struct bio *bio) |
1427 | { | |
1428 | struct bio_vec *bvec; | |
1429 | int i; | |
1430 | ||
1431 | /* | |
1432 | * make sure we dirty pages we wrote to | |
1433 | */ | |
d74c6d51 | 1434 | bio_for_each_segment_all(bvec, bio, i) { |
1da177e4 LT |
1435 | if (bio_data_dir(bio) == READ) |
1436 | set_page_dirty_lock(bvec->bv_page); | |
1437 | ||
09cbfeaf | 1438 | put_page(bvec->bv_page); |
1da177e4 LT |
1439 | } |
1440 | ||
1441 | bio_put(bio); | |
1442 | } | |
1443 | ||
1444 | /** | |
1445 | * bio_unmap_user - unmap a bio | |
1446 | * @bio: the bio being unmapped | |
1447 | * | |
5fad1b64 BVA |
1448 | * Unmap a bio previously mapped by bio_map_user_iov(). Must be called from |
1449 | * process context. | |
1da177e4 LT |
1450 | * |
1451 | * bio_unmap_user() may sleep. | |
1452 | */ | |
1453 | void bio_unmap_user(struct bio *bio) | |
1454 | { | |
1455 | __bio_unmap_user(bio); | |
1456 | bio_put(bio); | |
1457 | } | |
1458 | ||
4246a0b6 | 1459 | static void bio_map_kern_endio(struct bio *bio) |
b823825e | 1460 | { |
b823825e | 1461 | bio_put(bio); |
b823825e JA |
1462 | } |
1463 | ||
75c72b83 CH |
1464 | /** |
1465 | * bio_map_kern - map kernel address into bio | |
1466 | * @q: the struct request_queue for the bio | |
1467 | * @data: pointer to buffer to map | |
1468 | * @len: length in bytes | |
1469 | * @gfp_mask: allocation flags for bio allocation | |
1470 | * | |
1471 | * Map the kernel address into a bio suitable for io to a block | |
1472 | * device. Returns an error pointer in case of error. | |
1473 | */ | |
1474 | struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len, | |
1475 | gfp_t gfp_mask) | |
df46b9a4 MC |
1476 | { |
1477 | unsigned long kaddr = (unsigned long)data; | |
1478 | unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1479 | unsigned long start = kaddr >> PAGE_SHIFT; | |
1480 | const int nr_pages = end - start; | |
1481 | int offset, i; | |
1482 | struct bio *bio; | |
1483 | ||
a9e9dc24 | 1484 | bio = bio_kmalloc(gfp_mask, nr_pages); |
df46b9a4 MC |
1485 | if (!bio) |
1486 | return ERR_PTR(-ENOMEM); | |
1487 | ||
1488 | offset = offset_in_page(kaddr); | |
1489 | for (i = 0; i < nr_pages; i++) { | |
1490 | unsigned int bytes = PAGE_SIZE - offset; | |
1491 | ||
1492 | if (len <= 0) | |
1493 | break; | |
1494 | ||
1495 | if (bytes > len) | |
1496 | bytes = len; | |
1497 | ||
defd94b7 | 1498 | if (bio_add_pc_page(q, bio, virt_to_page(data), bytes, |
75c72b83 CH |
1499 | offset) < bytes) { |
1500 | /* we don't support partial mappings */ | |
1501 | bio_put(bio); | |
1502 | return ERR_PTR(-EINVAL); | |
1503 | } | |
df46b9a4 MC |
1504 | |
1505 | data += bytes; | |
1506 | len -= bytes; | |
1507 | offset = 0; | |
1508 | } | |
1509 | ||
b823825e | 1510 | bio->bi_end_io = bio_map_kern_endio; |
df46b9a4 MC |
1511 | return bio; |
1512 | } | |
a112a71d | 1513 | EXPORT_SYMBOL(bio_map_kern); |
df46b9a4 | 1514 | |
4246a0b6 | 1515 | static void bio_copy_kern_endio(struct bio *bio) |
68154e90 | 1516 | { |
1dfa0f68 CH |
1517 | bio_free_pages(bio); |
1518 | bio_put(bio); | |
1519 | } | |
1520 | ||
4246a0b6 | 1521 | static void bio_copy_kern_endio_read(struct bio *bio) |
1dfa0f68 | 1522 | { |
42d2683a | 1523 | char *p = bio->bi_private; |
1dfa0f68 | 1524 | struct bio_vec *bvec; |
68154e90 FT |
1525 | int i; |
1526 | ||
d74c6d51 | 1527 | bio_for_each_segment_all(bvec, bio, i) { |
1dfa0f68 | 1528 | memcpy(p, page_address(bvec->bv_page), bvec->bv_len); |
c8db4448 | 1529 | p += bvec->bv_len; |
68154e90 FT |
1530 | } |
1531 | ||
4246a0b6 | 1532 | bio_copy_kern_endio(bio); |
68154e90 FT |
1533 | } |
1534 | ||
1535 | /** | |
1536 | * bio_copy_kern - copy kernel address into bio | |
1537 | * @q: the struct request_queue for the bio | |
1538 | * @data: pointer to buffer to copy | |
1539 | * @len: length in bytes | |
1540 | * @gfp_mask: allocation flags for bio and page allocation | |
ffee0259 | 1541 | * @reading: data direction is READ |
68154e90 FT |
1542 | * |
1543 | * copy the kernel address into a bio suitable for io to a block | |
1544 | * device. Returns an error pointer in case of error. | |
1545 | */ | |
1546 | struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len, | |
1547 | gfp_t gfp_mask, int reading) | |
1548 | { | |
42d2683a CH |
1549 | unsigned long kaddr = (unsigned long)data; |
1550 | unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1551 | unsigned long start = kaddr >> PAGE_SHIFT; | |
42d2683a CH |
1552 | struct bio *bio; |
1553 | void *p = data; | |
1dfa0f68 | 1554 | int nr_pages = 0; |
68154e90 | 1555 | |
42d2683a CH |
1556 | /* |
1557 | * Overflow, abort | |
1558 | */ | |
1559 | if (end < start) | |
1560 | return ERR_PTR(-EINVAL); | |
68154e90 | 1561 | |
42d2683a CH |
1562 | nr_pages = end - start; |
1563 | bio = bio_kmalloc(gfp_mask, nr_pages); | |
1564 | if (!bio) | |
1565 | return ERR_PTR(-ENOMEM); | |
68154e90 | 1566 | |
42d2683a CH |
1567 | while (len) { |
1568 | struct page *page; | |
1569 | unsigned int bytes = PAGE_SIZE; | |
68154e90 | 1570 | |
42d2683a CH |
1571 | if (bytes > len) |
1572 | bytes = len; | |
1573 | ||
1574 | page = alloc_page(q->bounce_gfp | gfp_mask); | |
1575 | if (!page) | |
1576 | goto cleanup; | |
1577 | ||
1578 | if (!reading) | |
1579 | memcpy(page_address(page), p, bytes); | |
1580 | ||
1581 | if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes) | |
1582 | break; | |
1583 | ||
1584 | len -= bytes; | |
1585 | p += bytes; | |
68154e90 FT |
1586 | } |
1587 | ||
1dfa0f68 CH |
1588 | if (reading) { |
1589 | bio->bi_end_io = bio_copy_kern_endio_read; | |
1590 | bio->bi_private = data; | |
1591 | } else { | |
1592 | bio->bi_end_io = bio_copy_kern_endio; | |
1dfa0f68 | 1593 | } |
76029ff3 | 1594 | |
68154e90 | 1595 | return bio; |
42d2683a CH |
1596 | |
1597 | cleanup: | |
1dfa0f68 | 1598 | bio_free_pages(bio); |
42d2683a CH |
1599 | bio_put(bio); |
1600 | return ERR_PTR(-ENOMEM); | |
68154e90 FT |
1601 | } |
1602 | ||
1da177e4 LT |
1603 | /* |
1604 | * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions | |
1605 | * for performing direct-IO in BIOs. | |
1606 | * | |
1607 | * The problem is that we cannot run set_page_dirty() from interrupt context | |
1608 | * because the required locks are not interrupt-safe. So what we can do is to | |
1609 | * mark the pages dirty _before_ performing IO. And in interrupt context, | |
1610 | * check that the pages are still dirty. If so, fine. If not, redirty them | |
1611 | * in process context. | |
1612 | * | |
1613 | * We special-case compound pages here: normally this means reads into hugetlb | |
1614 | * pages. The logic in here doesn't really work right for compound pages | |
1615 | * because the VM does not uniformly chase down the head page in all cases. | |
1616 | * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't | |
1617 | * handle them at all. So we skip compound pages here at an early stage. | |
1618 | * | |
1619 | * Note that this code is very hard to test under normal circumstances because | |
1620 | * direct-io pins the pages with get_user_pages(). This makes | |
1621 | * is_page_cache_freeable return false, and the VM will not clean the pages. | |
0d5c3eba | 1622 | * But other code (eg, flusher threads) could clean the pages if they are mapped |
1da177e4 LT |
1623 | * pagecache. |
1624 | * | |
1625 | * Simply disabling the call to bio_set_pages_dirty() is a good way to test the | |
1626 | * deferred bio dirtying paths. | |
1627 | */ | |
1628 | ||
1629 | /* | |
1630 | * bio_set_pages_dirty() will mark all the bio's pages as dirty. | |
1631 | */ | |
1632 | void bio_set_pages_dirty(struct bio *bio) | |
1633 | { | |
cb34e057 | 1634 | struct bio_vec *bvec; |
1da177e4 LT |
1635 | int i; |
1636 | ||
cb34e057 KO |
1637 | bio_for_each_segment_all(bvec, bio, i) { |
1638 | struct page *page = bvec->bv_page; | |
1da177e4 LT |
1639 | |
1640 | if (page && !PageCompound(page)) | |
1641 | set_page_dirty_lock(page); | |
1642 | } | |
1643 | } | |
1900fcc4 | 1644 | EXPORT_SYMBOL_GPL(bio_set_pages_dirty); |
1da177e4 | 1645 | |
86b6c7a7 | 1646 | static void bio_release_pages(struct bio *bio) |
1da177e4 | 1647 | { |
cb34e057 | 1648 | struct bio_vec *bvec; |
1da177e4 LT |
1649 | int i; |
1650 | ||
cb34e057 KO |
1651 | bio_for_each_segment_all(bvec, bio, i) { |
1652 | struct page *page = bvec->bv_page; | |
1da177e4 LT |
1653 | |
1654 | if (page) | |
1655 | put_page(page); | |
1656 | } | |
1657 | } | |
1658 | ||
1659 | /* | |
1660 | * bio_check_pages_dirty() will check that all the BIO's pages are still dirty. | |
1661 | * If they are, then fine. If, however, some pages are clean then they must | |
1662 | * have been written out during the direct-IO read. So we take another ref on | |
1663 | * the BIO and the offending pages and re-dirty the pages in process context. | |
1664 | * | |
1665 | * It is expected that bio_check_pages_dirty() will wholly own the BIO from | |
ea1754a0 KS |
1666 | * here on. It will run one put_page() against each page and will run one |
1667 | * bio_put() against the BIO. | |
1da177e4 LT |
1668 | */ |
1669 | ||
65f27f38 | 1670 | static void bio_dirty_fn(struct work_struct *work); |
1da177e4 | 1671 | |
65f27f38 | 1672 | static DECLARE_WORK(bio_dirty_work, bio_dirty_fn); |
1da177e4 LT |
1673 | static DEFINE_SPINLOCK(bio_dirty_lock); |
1674 | static struct bio *bio_dirty_list; | |
1675 | ||
1676 | /* | |
1677 | * This runs in process context | |
1678 | */ | |
65f27f38 | 1679 | static void bio_dirty_fn(struct work_struct *work) |
1da177e4 LT |
1680 | { |
1681 | unsigned long flags; | |
1682 | struct bio *bio; | |
1683 | ||
1684 | spin_lock_irqsave(&bio_dirty_lock, flags); | |
1685 | bio = bio_dirty_list; | |
1686 | bio_dirty_list = NULL; | |
1687 | spin_unlock_irqrestore(&bio_dirty_lock, flags); | |
1688 | ||
1689 | while (bio) { | |
1690 | struct bio *next = bio->bi_private; | |
1691 | ||
1692 | bio_set_pages_dirty(bio); | |
1693 | bio_release_pages(bio); | |
1694 | bio_put(bio); | |
1695 | bio = next; | |
1696 | } | |
1697 | } | |
1698 | ||
1699 | void bio_check_pages_dirty(struct bio *bio) | |
1700 | { | |
cb34e057 | 1701 | struct bio_vec *bvec; |
1da177e4 LT |
1702 | int nr_clean_pages = 0; |
1703 | int i; | |
1704 | ||
cb34e057 KO |
1705 | bio_for_each_segment_all(bvec, bio, i) { |
1706 | struct page *page = bvec->bv_page; | |
1da177e4 LT |
1707 | |
1708 | if (PageDirty(page) || PageCompound(page)) { | |
09cbfeaf | 1709 | put_page(page); |
cb34e057 | 1710 | bvec->bv_page = NULL; |
1da177e4 LT |
1711 | } else { |
1712 | nr_clean_pages++; | |
1713 | } | |
1714 | } | |
1715 | ||
1716 | if (nr_clean_pages) { | |
1717 | unsigned long flags; | |
1718 | ||
1719 | spin_lock_irqsave(&bio_dirty_lock, flags); | |
1720 | bio->bi_private = bio_dirty_list; | |
1721 | bio_dirty_list = bio; | |
1722 | spin_unlock_irqrestore(&bio_dirty_lock, flags); | |
1723 | schedule_work(&bio_dirty_work); | |
1724 | } else { | |
1725 | bio_put(bio); | |
1726 | } | |
1727 | } | |
1900fcc4 | 1728 | EXPORT_SYMBOL_GPL(bio_check_pages_dirty); |
1da177e4 | 1729 | |
d62e26b3 JA |
1730 | void generic_start_io_acct(struct request_queue *q, int rw, |
1731 | unsigned long sectors, struct hd_struct *part) | |
394ffa50 GZ |
1732 | { |
1733 | int cpu = part_stat_lock(); | |
1734 | ||
d62e26b3 | 1735 | part_round_stats(q, cpu, part); |
394ffa50 GZ |
1736 | part_stat_inc(cpu, part, ios[rw]); |
1737 | part_stat_add(cpu, part, sectors[rw], sectors); | |
d62e26b3 | 1738 | part_inc_in_flight(q, part, rw); |
394ffa50 GZ |
1739 | |
1740 | part_stat_unlock(); | |
1741 | } | |
1742 | EXPORT_SYMBOL(generic_start_io_acct); | |
1743 | ||
d62e26b3 JA |
1744 | void generic_end_io_acct(struct request_queue *q, int rw, |
1745 | struct hd_struct *part, unsigned long start_time) | |
394ffa50 GZ |
1746 | { |
1747 | unsigned long duration = jiffies - start_time; | |
1748 | int cpu = part_stat_lock(); | |
1749 | ||
1750 | part_stat_add(cpu, part, ticks[rw], duration); | |
d62e26b3 JA |
1751 | part_round_stats(q, cpu, part); |
1752 | part_dec_in_flight(q, part, rw); | |
394ffa50 GZ |
1753 | |
1754 | part_stat_unlock(); | |
1755 | } | |
1756 | EXPORT_SYMBOL(generic_end_io_acct); | |
1757 | ||
2d4dc890 IL |
1758 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE |
1759 | void bio_flush_dcache_pages(struct bio *bi) | |
1760 | { | |
7988613b KO |
1761 | struct bio_vec bvec; |
1762 | struct bvec_iter iter; | |
2d4dc890 | 1763 | |
7988613b KO |
1764 | bio_for_each_segment(bvec, bi, iter) |
1765 | flush_dcache_page(bvec.bv_page); | |
2d4dc890 IL |
1766 | } |
1767 | EXPORT_SYMBOL(bio_flush_dcache_pages); | |
1768 | #endif | |
1769 | ||
c4cf5261 JA |
1770 | static inline bool bio_remaining_done(struct bio *bio) |
1771 | { | |
1772 | /* | |
1773 | * If we're not chaining, then ->__bi_remaining is always 1 and | |
1774 | * we always end io on the first invocation. | |
1775 | */ | |
1776 | if (!bio_flagged(bio, BIO_CHAIN)) | |
1777 | return true; | |
1778 | ||
1779 | BUG_ON(atomic_read(&bio->__bi_remaining) <= 0); | |
1780 | ||
326e1dbb | 1781 | if (atomic_dec_and_test(&bio->__bi_remaining)) { |
b7c44ed9 | 1782 | bio_clear_flag(bio, BIO_CHAIN); |
c4cf5261 | 1783 | return true; |
326e1dbb | 1784 | } |
c4cf5261 JA |
1785 | |
1786 | return false; | |
1787 | } | |
1788 | ||
1da177e4 LT |
1789 | /** |
1790 | * bio_endio - end I/O on a bio | |
1791 | * @bio: bio | |
1da177e4 LT |
1792 | * |
1793 | * Description: | |
4246a0b6 CH |
1794 | * bio_endio() will end I/O on the whole bio. bio_endio() is the preferred |
1795 | * way to end I/O on a bio. No one should call bi_end_io() directly on a | |
1796 | * bio unless they own it and thus know that it has an end_io function. | |
fbbaf700 N |
1797 | * |
1798 | * bio_endio() can be called several times on a bio that has been chained | |
1799 | * using bio_chain(). The ->bi_end_io() function will only be called the | |
1800 | * last time. At this point the BLK_TA_COMPLETE tracing event will be | |
1801 | * generated if BIO_TRACE_COMPLETION is set. | |
1da177e4 | 1802 | **/ |
4246a0b6 | 1803 | void bio_endio(struct bio *bio) |
1da177e4 | 1804 | { |
ba8c6967 | 1805 | again: |
2b885517 | 1806 | if (!bio_remaining_done(bio)) |
ba8c6967 | 1807 | return; |
7c20f116 CH |
1808 | if (!bio_integrity_endio(bio)) |
1809 | return; | |
1da177e4 | 1810 | |
ba8c6967 CH |
1811 | /* |
1812 | * Need to have a real endio function for chained bios, otherwise | |
1813 | * various corner cases will break (like stacking block devices that | |
1814 | * save/restore bi_end_io) - however, we want to avoid unbounded | |
1815 | * recursion and blowing the stack. Tail call optimization would | |
1816 | * handle this, but compiling with frame pointers also disables | |
1817 | * gcc's sibling call optimization. | |
1818 | */ | |
1819 | if (bio->bi_end_io == bio_chain_endio) { | |
1820 | bio = __bio_chain_endio(bio); | |
1821 | goto again; | |
196d38bc | 1822 | } |
ba8c6967 | 1823 | |
74d46992 CH |
1824 | if (bio->bi_disk && bio_flagged(bio, BIO_TRACE_COMPLETION)) { |
1825 | trace_block_bio_complete(bio->bi_disk->queue, bio, | |
a462b950 | 1826 | blk_status_to_errno(bio->bi_status)); |
fbbaf700 N |
1827 | bio_clear_flag(bio, BIO_TRACE_COMPLETION); |
1828 | } | |
1829 | ||
9e234eea | 1830 | blk_throtl_bio_endio(bio); |
b222dd2f SL |
1831 | /* release cgroup info */ |
1832 | bio_uninit(bio); | |
ba8c6967 CH |
1833 | if (bio->bi_end_io) |
1834 | bio->bi_end_io(bio); | |
1da177e4 | 1835 | } |
a112a71d | 1836 | EXPORT_SYMBOL(bio_endio); |
1da177e4 | 1837 | |
20d0189b KO |
1838 | /** |
1839 | * bio_split - split a bio | |
1840 | * @bio: bio to split | |
1841 | * @sectors: number of sectors to split from the front of @bio | |
1842 | * @gfp: gfp mask | |
1843 | * @bs: bio set to allocate from | |
1844 | * | |
1845 | * Allocates and returns a new bio which represents @sectors from the start of | |
1846 | * @bio, and updates @bio to represent the remaining sectors. | |
1847 | * | |
f3f5da62 MP |
1848 | * Unless this is a discard request the newly allocated bio will point |
1849 | * to @bio's bi_io_vec; it is the caller's responsibility to ensure that | |
1850 | * @bio is not freed before the split. | |
20d0189b KO |
1851 | */ |
1852 | struct bio *bio_split(struct bio *bio, int sectors, | |
1853 | gfp_t gfp, struct bio_set *bs) | |
1854 | { | |
f341a4d3 | 1855 | struct bio *split; |
20d0189b KO |
1856 | |
1857 | BUG_ON(sectors <= 0); | |
1858 | BUG_ON(sectors >= bio_sectors(bio)); | |
1859 | ||
f9d03f96 | 1860 | split = bio_clone_fast(bio, gfp, bs); |
20d0189b KO |
1861 | if (!split) |
1862 | return NULL; | |
1863 | ||
1864 | split->bi_iter.bi_size = sectors << 9; | |
1865 | ||
1866 | if (bio_integrity(split)) | |
fbd08e76 | 1867 | bio_integrity_trim(split); |
20d0189b KO |
1868 | |
1869 | bio_advance(bio, split->bi_iter.bi_size); | |
1870 | ||
fbbaf700 | 1871 | if (bio_flagged(bio, BIO_TRACE_COMPLETION)) |
20d59023 | 1872 | bio_set_flag(split, BIO_TRACE_COMPLETION); |
fbbaf700 | 1873 | |
20d0189b KO |
1874 | return split; |
1875 | } | |
1876 | EXPORT_SYMBOL(bio_split); | |
1877 | ||
6678d83f KO |
1878 | /** |
1879 | * bio_trim - trim a bio | |
1880 | * @bio: bio to trim | |
1881 | * @offset: number of sectors to trim from the front of @bio | |
1882 | * @size: size we want to trim @bio to, in sectors | |
1883 | */ | |
1884 | void bio_trim(struct bio *bio, int offset, int size) | |
1885 | { | |
1886 | /* 'bio' is a cloned bio which we need to trim to match | |
1887 | * the given offset and size. | |
6678d83f | 1888 | */ |
6678d83f KO |
1889 | |
1890 | size <<= 9; | |
4f024f37 | 1891 | if (offset == 0 && size == bio->bi_iter.bi_size) |
6678d83f KO |
1892 | return; |
1893 | ||
b7c44ed9 | 1894 | bio_clear_flag(bio, BIO_SEG_VALID); |
6678d83f KO |
1895 | |
1896 | bio_advance(bio, offset << 9); | |
1897 | ||
4f024f37 | 1898 | bio->bi_iter.bi_size = size; |
376a78ab DM |
1899 | |
1900 | if (bio_integrity(bio)) | |
fbd08e76 | 1901 | bio_integrity_trim(bio); |
376a78ab | 1902 | |
6678d83f KO |
1903 | } |
1904 | EXPORT_SYMBOL_GPL(bio_trim); | |
1905 | ||
1da177e4 LT |
1906 | /* |
1907 | * create memory pools for biovec's in a bio_set. | |
1908 | * use the global biovec slabs created for general use. | |
1909 | */ | |
8aa6ba2f | 1910 | int biovec_init_pool(mempool_t *pool, int pool_entries) |
1da177e4 | 1911 | { |
ed996a52 | 1912 | struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX; |
1da177e4 | 1913 | |
8aa6ba2f | 1914 | return mempool_init_slab_pool(pool, pool_entries, bp->slab); |
1da177e4 LT |
1915 | } |
1916 | ||
917a38c7 KO |
1917 | /* |
1918 | * bioset_exit - exit a bioset initialized with bioset_init() | |
1919 | * | |
1920 | * May be called on a zeroed but uninitialized bioset (i.e. allocated with | |
1921 | * kzalloc()). | |
1922 | */ | |
1923 | void bioset_exit(struct bio_set *bs) | |
1da177e4 | 1924 | { |
df2cb6da KO |
1925 | if (bs->rescue_workqueue) |
1926 | destroy_workqueue(bs->rescue_workqueue); | |
917a38c7 | 1927 | bs->rescue_workqueue = NULL; |
df2cb6da | 1928 | |
8aa6ba2f KO |
1929 | mempool_exit(&bs->bio_pool); |
1930 | mempool_exit(&bs->bvec_pool); | |
9f060e22 | 1931 | |
7878cba9 | 1932 | bioset_integrity_free(bs); |
917a38c7 KO |
1933 | if (bs->bio_slab) |
1934 | bio_put_slab(bs); | |
1935 | bs->bio_slab = NULL; | |
1936 | } | |
1937 | EXPORT_SYMBOL(bioset_exit); | |
1da177e4 | 1938 | |
917a38c7 KO |
1939 | /** |
1940 | * bioset_init - Initialize a bio_set | |
dad08527 | 1941 | * @bs: pool to initialize |
917a38c7 KO |
1942 | * @pool_size: Number of bio and bio_vecs to cache in the mempool |
1943 | * @front_pad: Number of bytes to allocate in front of the returned bio | |
1944 | * @flags: Flags to modify behavior, currently %BIOSET_NEED_BVECS | |
1945 | * and %BIOSET_NEED_RESCUER | |
1946 | * | |
dad08527 KO |
1947 | * Description: |
1948 | * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller | |
1949 | * to ask for a number of bytes to be allocated in front of the bio. | |
1950 | * Front pad allocation is useful for embedding the bio inside | |
1951 | * another structure, to avoid allocating extra data to go with the bio. | |
1952 | * Note that the bio must be embedded at the END of that structure always, | |
1953 | * or things will break badly. | |
1954 | * If %BIOSET_NEED_BVECS is set in @flags, a separate pool will be allocated | |
1955 | * for allocating iovecs. This pool is not needed e.g. for bio_clone_fast(). | |
1956 | * If %BIOSET_NEED_RESCUER is set, a workqueue is created which can be used to | |
1957 | * dispatch queued requests when the mempool runs out of space. | |
1958 | * | |
917a38c7 KO |
1959 | */ |
1960 | int bioset_init(struct bio_set *bs, | |
1961 | unsigned int pool_size, | |
1962 | unsigned int front_pad, | |
1963 | int flags) | |
1964 | { | |
1965 | unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec); | |
1966 | ||
1967 | bs->front_pad = front_pad; | |
1968 | ||
1969 | spin_lock_init(&bs->rescue_lock); | |
1970 | bio_list_init(&bs->rescue_list); | |
1971 | INIT_WORK(&bs->rescue_work, bio_alloc_rescue); | |
1972 | ||
1973 | bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad); | |
1974 | if (!bs->bio_slab) | |
1975 | return -ENOMEM; | |
1976 | ||
1977 | if (mempool_init_slab_pool(&bs->bio_pool, pool_size, bs->bio_slab)) | |
1978 | goto bad; | |
1979 | ||
1980 | if ((flags & BIOSET_NEED_BVECS) && | |
1981 | biovec_init_pool(&bs->bvec_pool, pool_size)) | |
1982 | goto bad; | |
1983 | ||
1984 | if (!(flags & BIOSET_NEED_RESCUER)) | |
1985 | return 0; | |
1986 | ||
1987 | bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0); | |
1988 | if (!bs->rescue_workqueue) | |
1989 | goto bad; | |
1990 | ||
1991 | return 0; | |
1992 | bad: | |
1993 | bioset_exit(bs); | |
1994 | return -ENOMEM; | |
1995 | } | |
1996 | EXPORT_SYMBOL(bioset_init); | |
1997 | ||
28e89fd9 JA |
1998 | /* |
1999 | * Initialize and setup a new bio_set, based on the settings from | |
2000 | * another bio_set. | |
2001 | */ | |
2002 | int bioset_init_from_src(struct bio_set *bs, struct bio_set *src) | |
2003 | { | |
2004 | int flags; | |
2005 | ||
2006 | flags = 0; | |
2007 | if (src->bvec_pool.min_nr) | |
2008 | flags |= BIOSET_NEED_BVECS; | |
2009 | if (src->rescue_workqueue) | |
2010 | flags |= BIOSET_NEED_RESCUER; | |
2011 | ||
2012 | return bioset_init(bs, src->bio_pool.min_nr, src->front_pad, flags); | |
2013 | } | |
2014 | EXPORT_SYMBOL(bioset_init_from_src); | |
2015 | ||
852c788f | 2016 | #ifdef CONFIG_BLK_CGROUP |
1d933cf0 | 2017 | |
0d3bd88d TH |
2018 | #ifdef CONFIG_MEMCG |
2019 | /** | |
2020 | * bio_associate_blkcg_from_page - associate a bio with the page's blkcg | |
2021 | * @bio: target bio | |
2022 | * @page: the page to lookup the blkcg from | |
2023 | * | |
2024 | * Associate @bio with the blkcg from @page's owning memcg. This works like | |
2025 | * every other associate function wrt references. | |
2026 | */ | |
2027 | int bio_associate_blkcg_from_page(struct bio *bio, struct page *page) | |
2028 | { | |
2029 | struct cgroup_subsys_state *blkcg_css; | |
2030 | ||
2031 | if (unlikely(bio->bi_css)) | |
2032 | return -EBUSY; | |
2033 | if (!page->mem_cgroup) | |
2034 | return 0; | |
2035 | blkcg_css = cgroup_get_e_css(page->mem_cgroup->css.cgroup, | |
2036 | &io_cgrp_subsys); | |
2037 | bio->bi_css = blkcg_css; | |
2038 | return 0; | |
2039 | } | |
2040 | #endif /* CONFIG_MEMCG */ | |
2041 | ||
1d933cf0 TH |
2042 | /** |
2043 | * bio_associate_blkcg - associate a bio with the specified blkcg | |
2044 | * @bio: target bio | |
2045 | * @blkcg_css: css of the blkcg to associate | |
2046 | * | |
2047 | * Associate @bio with the blkcg specified by @blkcg_css. Block layer will | |
2048 | * treat @bio as if it were issued by a task which belongs to the blkcg. | |
2049 | * | |
2050 | * This function takes an extra reference of @blkcg_css which will be put | |
2051 | * when @bio is released. The caller must own @bio and is responsible for | |
2052 | * synchronizing calls to this function. | |
2053 | */ | |
2054 | int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css) | |
2055 | { | |
2056 | if (unlikely(bio->bi_css)) | |
2057 | return -EBUSY; | |
2058 | css_get(blkcg_css); | |
2059 | bio->bi_css = blkcg_css; | |
2060 | return 0; | |
2061 | } | |
5aa2a96b | 2062 | EXPORT_SYMBOL_GPL(bio_associate_blkcg); |
1d933cf0 | 2063 | |
08e18eab JB |
2064 | /** |
2065 | * bio_associate_blkg - associate a bio with the specified blkg | |
2066 | * @bio: target bio | |
2067 | * @blkg: the blkg to associate | |
2068 | * | |
2069 | * Associate @bio with the blkg specified by @blkg. This is the queue specific | |
2070 | * blkcg information associated with the @bio, a reference will be taken on the | |
2071 | * @blkg and will be freed when the bio is freed. | |
2072 | */ | |
2073 | int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg) | |
2074 | { | |
2075 | if (unlikely(bio->bi_blkg)) | |
2076 | return -EBUSY; | |
2077 | blkg_get(blkg); | |
2078 | bio->bi_blkg = blkg; | |
2079 | return 0; | |
2080 | } | |
2081 | ||
852c788f TH |
2082 | /** |
2083 | * bio_disassociate_task - undo bio_associate_current() | |
2084 | * @bio: target bio | |
2085 | */ | |
2086 | void bio_disassociate_task(struct bio *bio) | |
2087 | { | |
2088 | if (bio->bi_ioc) { | |
2089 | put_io_context(bio->bi_ioc); | |
2090 | bio->bi_ioc = NULL; | |
2091 | } | |
2092 | if (bio->bi_css) { | |
2093 | css_put(bio->bi_css); | |
2094 | bio->bi_css = NULL; | |
2095 | } | |
08e18eab JB |
2096 | if (bio->bi_blkg) { |
2097 | blkg_put(bio->bi_blkg); | |
2098 | bio->bi_blkg = NULL; | |
2099 | } | |
852c788f TH |
2100 | } |
2101 | ||
20bd723e PV |
2102 | /** |
2103 | * bio_clone_blkcg_association - clone blkcg association from src to dst bio | |
2104 | * @dst: destination bio | |
2105 | * @src: source bio | |
2106 | */ | |
2107 | void bio_clone_blkcg_association(struct bio *dst, struct bio *src) | |
2108 | { | |
2109 | if (src->bi_css) | |
2110 | WARN_ON(bio_associate_blkcg(dst, src->bi_css)); | |
2111 | } | |
8a8e6f84 | 2112 | EXPORT_SYMBOL_GPL(bio_clone_blkcg_association); |
852c788f TH |
2113 | #endif /* CONFIG_BLK_CGROUP */ |
2114 | ||
1da177e4 LT |
2115 | static void __init biovec_init_slabs(void) |
2116 | { | |
2117 | int i; | |
2118 | ||
ed996a52 | 2119 | for (i = 0; i < BVEC_POOL_NR; i++) { |
1da177e4 LT |
2120 | int size; |
2121 | struct biovec_slab *bvs = bvec_slabs + i; | |
2122 | ||
a7fcd37c JA |
2123 | if (bvs->nr_vecs <= BIO_INLINE_VECS) { |
2124 | bvs->slab = NULL; | |
2125 | continue; | |
2126 | } | |
a7fcd37c | 2127 | |
1da177e4 LT |
2128 | size = bvs->nr_vecs * sizeof(struct bio_vec); |
2129 | bvs->slab = kmem_cache_create(bvs->name, size, 0, | |
20c2df83 | 2130 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
1da177e4 LT |
2131 | } |
2132 | } | |
2133 | ||
2134 | static int __init init_bio(void) | |
2135 | { | |
bb799ca0 JA |
2136 | bio_slab_max = 2; |
2137 | bio_slab_nr = 0; | |
6396bb22 KC |
2138 | bio_slabs = kcalloc(bio_slab_max, sizeof(struct bio_slab), |
2139 | GFP_KERNEL); | |
bb799ca0 JA |
2140 | if (!bio_slabs) |
2141 | panic("bio: can't allocate bios\n"); | |
1da177e4 | 2142 | |
7878cba9 | 2143 | bio_integrity_init(); |
1da177e4 LT |
2144 | biovec_init_slabs(); |
2145 | ||
f4f8154a | 2146 | if (bioset_init(&fs_bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS)) |
1da177e4 LT |
2147 | panic("bio: can't allocate bios\n"); |
2148 | ||
f4f8154a | 2149 | if (bioset_integrity_create(&fs_bio_set, BIO_POOL_SIZE)) |
a91a2785 MP |
2150 | panic("bio: can't create integrity pool\n"); |
2151 | ||
1da177e4 LT |
2152 | return 0; |
2153 | } | |
1da177e4 | 2154 | subsys_initcall(init_bio); |