diff options
Diffstat (limited to 'fs/bio.c')
| -rw-r--r-- | fs/bio.c | 127 |
1 files changed, 60 insertions, 67 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,50 @@ 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 | */ | ||
| 333 | struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs) | ||
| 334 | { | ||
| 335 | struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set); | ||
| 336 | |||
| 337 | if (bio) | ||
| 338 | bio->bi_destructor = bio_fs_destructor; | ||
| 339 | |||
| 340 | return bio; | ||
| 341 | } | ||
| 342 | |||
| 343 | static void bio_kmalloc_destructor(struct bio *bio) | ||
| 344 | { | ||
| 345 | if (bio_integrity(bio)) | ||
| 346 | bio_integrity_free(bio); | ||
| 347 | kfree(bio); | ||
| 348 | } | ||
| 349 | |||
| 351 | /** | 350 | /** |
| 352 | * bio_alloc - allocate a bio for I/O | 351 | * bio_alloc - allocate a bio for I/O |
| 353 | * @gfp_mask: the GFP_ mask given to the slab allocator | 352 | * @gfp_mask: the GFP_ mask given to the slab allocator |
| @@ -366,29 +365,20 @@ err: | |||
| 366 | * do so can cause livelocks under memory pressure. | 365 | * do so can cause livelocks under memory pressure. |
| 367 | * | 366 | * |
| 368 | **/ | 367 | **/ |
| 369 | struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs) | ||
| 370 | { | ||
| 371 | struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set); | ||
| 372 | |||
| 373 | if (bio) | ||
| 374 | bio->bi_destructor = bio_fs_destructor; | ||
| 375 | |||
| 376 | return bio; | ||
| 377 | } | ||
| 378 | |||
| 379 | /* | ||
| 380 | * Like bio_alloc(), but doesn't use a mempool backing. This means that | ||
| 381 | * it CAN fail, but while bio_alloc() can only be used for allocations | ||
| 382 | * that have a short (finite) life span, bio_kmalloc() should be used | ||
| 383 | * for more permanent bio allocations (like allocating some bio's for | ||
| 384 | * initalization or setup purposes). | ||
| 385 | */ | ||
| 386 | struct bio *bio_kmalloc(gfp_t gfp_mask, int nr_iovecs) | 368 | struct bio *bio_kmalloc(gfp_t gfp_mask, int nr_iovecs) |
| 387 | { | 369 | { |
| 388 | struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, NULL); | 370 | struct bio *bio; |
| 389 | 371 | ||
| 390 | if (bio) | 372 | bio = kmalloc(sizeof(struct bio) + nr_iovecs * sizeof(struct bio_vec), |
| 391 | 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; | ||
| 392 | 382 | ||
| 393 | return bio; | 383 | return bio; |
| 394 | } | 384 | } |
| @@ -827,12 +817,15 @@ struct bio *bio_copy_user_iov(struct request_queue *q, | |||
| 827 | len += iov[i].iov_len; | 817 | len += iov[i].iov_len; |
| 828 | } | 818 | } |
| 829 | 819 | ||
| 820 | if (offset) | ||
| 821 | nr_pages++; | ||
| 822 | |||
| 830 | bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask); | 823 | bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask); |
| 831 | if (!bmd) | 824 | if (!bmd) |
| 832 | return ERR_PTR(-ENOMEM); | 825 | return ERR_PTR(-ENOMEM); |
| 833 | 826 | ||
| 834 | ret = -ENOMEM; | 827 | ret = -ENOMEM; |
| 835 | bio = bio_alloc(gfp_mask, nr_pages); | 828 | bio = bio_kmalloc(gfp_mask, nr_pages); |
| 836 | if (!bio) | 829 | if (!bio) |
| 837 | goto out_bmd; | 830 | goto out_bmd; |
| 838 | 831 | ||
| @@ -956,7 +949,7 @@ static struct bio *__bio_map_user_iov(struct request_queue *q, | |||
| 956 | if (!nr_pages) | 949 | if (!nr_pages) |
| 957 | return ERR_PTR(-EINVAL); | 950 | return ERR_PTR(-EINVAL); |
| 958 | 951 | ||
| 959 | bio = bio_alloc(gfp_mask, nr_pages); | 952 | bio = bio_kmalloc(gfp_mask, nr_pages); |
| 960 | if (!bio) | 953 | if (!bio) |
| 961 | return ERR_PTR(-ENOMEM); | 954 | return ERR_PTR(-ENOMEM); |
| 962 | 955 | ||
| @@ -1140,7 +1133,7 @@ static struct bio *__bio_map_kern(struct request_queue *q, void *data, | |||
| 1140 | int offset, i; | 1133 | int offset, i; |
| 1141 | struct bio *bio; | 1134 | struct bio *bio; |
| 1142 | 1135 | ||
| 1143 | bio = bio_alloc(gfp_mask, nr_pages); | 1136 | bio = bio_kmalloc(gfp_mask, nr_pages); |
| 1144 | if (!bio) | 1137 | if (!bio) |
| 1145 | return ERR_PTR(-ENOMEM); | 1138 | return ERR_PTR(-ENOMEM); |
| 1146 | 1139 | ||
