diff options
Diffstat (limited to 'fs/bio.c')
-rw-r--r-- | fs/bio.c | 125 |
1 files changed, 68 insertions, 57 deletions
@@ -175,14 +175,6 @@ struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx, | |||
175 | struct bio_vec *bvl; | 175 | struct bio_vec *bvl; |
176 | 176 | ||
177 | /* | 177 | /* |
178 | * If 'bs' is given, lookup the pool and do the mempool alloc. | ||
179 | * If not, this is a bio_kmalloc() allocation and just do a | ||
180 | * kzalloc() for the exact number of vecs right away. | ||
181 | */ | ||
182 | if (!bs) | ||
183 | bvl = kmalloc(nr * sizeof(struct bio_vec), gfp_mask); | ||
184 | |||
185 | /* | ||
186 | * see comment near bvec_array define! | 178 | * see comment near bvec_array define! |
187 | */ | 179 | */ |
188 | switch (nr) { | 180 | switch (nr) { |
@@ -260,21 +252,6 @@ void bio_free(struct bio *bio, struct bio_set *bs) | |||
260 | mempool_free(p, bs->bio_pool); | 252 | mempool_free(p, bs->bio_pool); |
261 | } | 253 | } |
262 | 254 | ||
263 | /* | ||
264 | * default destructor for a bio allocated with bio_alloc_bioset() | ||
265 | */ | ||
266 | static void bio_fs_destructor(struct bio *bio) | ||
267 | { | ||
268 | bio_free(bio, fs_bio_set); | ||
269 | } | ||
270 | |||
271 | static void bio_kmalloc_destructor(struct bio *bio) | ||
272 | { | ||
273 | if (bio_has_allocated_vec(bio)) | ||
274 | kfree(bio->bi_io_vec); | ||
275 | kfree(bio); | ||
276 | } | ||
277 | |||
278 | void bio_init(struct bio *bio) | 255 | void bio_init(struct bio *bio) |
279 | { | 256 | { |
280 | memset(bio, 0, sizeof(*bio)); | 257 | memset(bio, 0, sizeof(*bio)); |
@@ -301,21 +278,15 @@ void bio_init(struct bio *bio) | |||
301 | **/ | 278 | **/ |
302 | struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs) | 279 | struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs) |
303 | { | 280 | { |
281 | unsigned long idx = BIO_POOL_NONE; | ||
304 | struct bio_vec *bvl = NULL; | 282 | struct bio_vec *bvl = NULL; |
305 | struct bio *bio = NULL; | 283 | struct bio *bio; |
306 | unsigned long idx = 0; | 284 | void *p; |
307 | void *p = NULL; | 285 | |
308 | 286 | p = mempool_alloc(bs->bio_pool, gfp_mask); | |
309 | if (bs) { | 287 | if (unlikely(!p)) |
310 | p = mempool_alloc(bs->bio_pool, gfp_mask); | 288 | return NULL; |
311 | if (!p) | 289 | bio = p + bs->front_pad; |
312 | goto err; | ||
313 | bio = p + bs->front_pad; | ||
314 | } else { | ||
315 | bio = kmalloc(sizeof(*bio), gfp_mask); | ||
316 | if (!bio) | ||
317 | goto err; | ||
318 | } | ||
319 | 290 | ||
320 | bio_init(bio); | 291 | bio_init(bio); |
321 | 292 | ||
@@ -332,22 +303,33 @@ struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs) | |||
332 | 303 | ||
333 | nr_iovecs = bvec_nr_vecs(idx); | 304 | nr_iovecs = bvec_nr_vecs(idx); |
334 | } | 305 | } |
306 | out_set: | ||
335 | bio->bi_flags |= idx << BIO_POOL_OFFSET; | 307 | bio->bi_flags |= idx << BIO_POOL_OFFSET; |
336 | bio->bi_max_vecs = nr_iovecs; | 308 | bio->bi_max_vecs = nr_iovecs; |
337 | out_set: | ||
338 | bio->bi_io_vec = bvl; | 309 | bio->bi_io_vec = bvl; |
339 | |||
340 | return bio; | 310 | return bio; |
341 | 311 | ||
342 | err_free: | 312 | err_free: |
343 | if (bs) | 313 | mempool_free(p, bs->bio_pool); |
344 | mempool_free(p, bs->bio_pool); | ||
345 | else | ||
346 | kfree(bio); | ||
347 | err: | ||
348 | return NULL; | 314 | return NULL; |
349 | } | 315 | } |
350 | 316 | ||
317 | static void bio_fs_destructor(struct bio *bio) | ||
318 | { | ||
319 | bio_free(bio, fs_bio_set); | ||
320 | } | ||
321 | |||
322 | /** | ||
323 | * bio_alloc - allocate a new bio, memory pool backed | ||
324 | * @gfp_mask: allocation mask to use | ||
325 | * @nr_iovecs: number of iovecs | ||
326 | * | ||
327 | * Allocate a new bio with @nr_iovecs bvecs. If @gfp_mask | ||
328 | * contains __GFP_WAIT, the allocation is guaranteed to succeed. | ||
329 | * | ||
330 | * RETURNS: | ||
331 | * Pointer to new bio on success, NULL on failure. | ||
332 | */ | ||
351 | struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs) | 333 | struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs) |
352 | { | 334 | { |
353 | struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set); | 335 | struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set); |
@@ -358,19 +340,45 @@ struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs) | |||
358 | return bio; | 340 | return bio; |
359 | } | 341 | } |
360 | 342 | ||
361 | /* | 343 | static void bio_kmalloc_destructor(struct bio *bio) |
362 | * Like bio_alloc(), but doesn't use a mempool backing. This means that | 344 | { |
363 | * it CAN fail, but while bio_alloc() can only be used for allocations | 345 | if (bio_integrity(bio)) |
364 | * that have a short (finite) life span, bio_kmalloc() should be used | 346 | bio_integrity_free(bio); |
365 | * for more permanent bio allocations (like allocating some bio's for | 347 | kfree(bio); |
366 | * initalization or setup purposes). | 348 | } |
367 | */ | 349 | |
350 | /** | ||
351 | * bio_alloc - allocate a bio for I/O | ||
352 | * @gfp_mask: the GFP_ mask given to the slab allocator | ||
353 | * @nr_iovecs: number of iovecs to pre-allocate | ||
354 | * | ||
355 | * Description: | ||
356 | * bio_alloc will allocate a bio and associated bio_vec array that can hold | ||
357 | * at least @nr_iovecs entries. Allocations will be done from the | ||
358 | * fs_bio_set. Also see @bio_alloc_bioset. | ||
359 | * | ||
360 | * If %__GFP_WAIT is set, then bio_alloc will always be able to allocate | ||
361 | * a bio. This is due to the mempool guarantees. To make this work, callers | ||
362 | * must never allocate more than 1 bio at the time from this pool. Callers | ||
363 | * that need to allocate more than 1 bio must always submit the previously | ||
364 | * allocate bio for IO before attempting to allocate a new one. Failure to | ||
365 | * do so can cause livelocks under memory pressure. | ||
366 | * | ||
367 | **/ | ||
368 | struct bio *bio_kmalloc(gfp_t gfp_mask, int nr_iovecs) | 368 | struct bio *bio_kmalloc(gfp_t gfp_mask, int nr_iovecs) |
369 | { | 369 | { |
370 | struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, NULL); | 370 | struct bio *bio; |
371 | 371 | ||
372 | if (bio) | 372 | bio = kmalloc(sizeof(struct bio) + nr_iovecs * sizeof(struct bio_vec), |
373 | bio->bi_destructor = bio_kmalloc_destructor; | 373 | gfp_mask); |
374 | if (unlikely(!bio)) | ||
375 | return NULL; | ||
376 | |||
377 | bio_init(bio); | ||
378 | bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET; | ||
379 | bio->bi_max_vecs = nr_iovecs; | ||
380 | bio->bi_io_vec = bio->bi_inline_vecs; | ||
381 | bio->bi_destructor = bio_kmalloc_destructor; | ||
374 | 382 | ||
375 | return bio; | 383 | return bio; |
376 | } | 384 | } |
@@ -809,12 +817,15 @@ struct bio *bio_copy_user_iov(struct request_queue *q, | |||
809 | len += iov[i].iov_len; | 817 | len += iov[i].iov_len; |
810 | } | 818 | } |
811 | 819 | ||
820 | if (offset) | ||
821 | nr_pages++; | ||
822 | |||
812 | bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask); | 823 | bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask); |
813 | if (!bmd) | 824 | if (!bmd) |
814 | return ERR_PTR(-ENOMEM); | 825 | return ERR_PTR(-ENOMEM); |
815 | 826 | ||
816 | ret = -ENOMEM; | 827 | ret = -ENOMEM; |
817 | bio = bio_alloc(gfp_mask, nr_pages); | 828 | bio = bio_kmalloc(gfp_mask, nr_pages); |
818 | if (!bio) | 829 | if (!bio) |
819 | goto out_bmd; | 830 | goto out_bmd; |
820 | 831 | ||
@@ -938,7 +949,7 @@ static struct bio *__bio_map_user_iov(struct request_queue *q, | |||
938 | if (!nr_pages) | 949 | if (!nr_pages) |
939 | return ERR_PTR(-EINVAL); | 950 | return ERR_PTR(-EINVAL); |
940 | 951 | ||
941 | bio = bio_alloc(gfp_mask, nr_pages); | 952 | bio = bio_kmalloc(gfp_mask, nr_pages); |
942 | if (!bio) | 953 | if (!bio) |
943 | return ERR_PTR(-ENOMEM); | 954 | return ERR_PTR(-ENOMEM); |
944 | 955 | ||
@@ -1122,7 +1133,7 @@ static struct bio *__bio_map_kern(struct request_queue *q, void *data, | |||
1122 | int offset, i; | 1133 | int offset, i; |
1123 | struct bio *bio; | 1134 | struct bio *bio; |
1124 | 1135 | ||
1125 | bio = bio_alloc(gfp_mask, nr_pages); | 1136 | bio = bio_kmalloc(gfp_mask, nr_pages); |
1126 | if (!bio) | 1137 | if (!bio) |
1127 | return ERR_PTR(-ENOMEM); | 1138 | return ERR_PTR(-ENOMEM); |
1128 | 1139 | ||