| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
#include <linux/mm.h> |
|---|
| 19 |
#include <linux/swap.h> |
|---|
| 20 |
#include <linux/bio.h> |
|---|
| 21 |
#include <linux/blkdev.h> |
|---|
| 22 |
#include <linux/slab.h> |
|---|
| 23 |
#include <linux/init.h> |
|---|
| 24 |
#include <linux/kernel.h> |
|---|
| 25 |
#include <linux/module.h> |
|---|
| 26 |
#include <linux/mempool.h> |
|---|
| 27 |
#include <linux/workqueue.h> |
|---|
| 28 |
|
|---|
| 29 |
#define BIO_POOL_SIZE 256 |
|---|
| 30 |
|
|---|
| 31 |
static mempool_t *bio_pool; |
|---|
| 32 |
static kmem_cache_t *bio_slab; |
|---|
| 33 |
|
|---|
| 34 |
#define BIOVEC_NR_POOLS 6 |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
#define BIO_SPLIT_ENTRIES 8 |
|---|
| 41 |
mempool_t *bio_split_pool; |
|---|
| 42 |
|
|---|
| 43 |
struct biovec_pool { |
|---|
| 44 |
int nr_vecs; |
|---|
| 45 |
char *name; |
|---|
| 46 |
kmem_cache_t *slab; |
|---|
| 47 |
mempool_t *pool; |
|---|
| 48 |
}; |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
#define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) } |
|---|
| 57 |
static struct biovec_pool bvec_array[BIOVEC_NR_POOLS] = { |
|---|
| 58 |
BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES), |
|---|
| 59 |
}; |
|---|
| 60 |
#undef BV |
|---|
| 61 |
|
|---|
| 62 |
static inline struct bio_vec *bvec_alloc(int gfp_mask, int nr, unsigned long *idx) |
|---|
| 63 |
{ |
|---|
| 64 |
struct biovec_pool *bp; |
|---|
| 65 |
struct bio_vec *bvl; |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
switch (nr) { |
|---|
| 71 |
case 1 : *idx = 0; break; |
|---|
| 72 |
case 2 ... 4: *idx = 1; break; |
|---|
| 73 |
case 5 ... 16: *idx = 2; break; |
|---|
| 74 |
case 17 ... 64: *idx = 3; break; |
|---|
| 75 |
case 65 ... 128: *idx = 4; break; |
|---|
| 76 |
case 129 ... BIO_MAX_PAGES: *idx = 5; break; |
|---|
| 77 |
default: |
|---|
| 78 |
return NULL; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
bp = bvec_array + *idx; |
|---|
| 84 |
|
|---|
| 85 |
bvl = mempool_alloc(bp->pool, gfp_mask); |
|---|
| 86 |
if (bvl) |
|---|
| 87 |
memset(bvl, 0, bp->nr_vecs * sizeof(struct bio_vec)); |
|---|
| 88 |
return bvl; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
void bio_destructor(struct bio *bio) |
|---|
| 95 |
{ |
|---|
| 96 |
const int pool_idx = BIO_POOL_IDX(bio); |
|---|
| 97 |
struct biovec_pool *bp = bvec_array + pool_idx; |
|---|
| 98 |
|
|---|
| 99 |
BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS); |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
if (!bio_flagged(bio, BIO_CLONED)) |
|---|
| 105 |
mempool_free(bio->bi_io_vec, bp->pool); |
|---|
| 106 |
|
|---|
| 107 |
mempool_free(bio, bio_pool); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
inline void bio_init(struct bio *bio) |
|---|
| 111 |
{ |
|---|
| 112 |
bio->bi_next = NULL; |
|---|
| 113 |
bio->bi_flags = 1 << BIO_UPTODATE; |
|---|
| 114 |
bio->bi_rw = 0; |
|---|
| 115 |
bio->bi_vcnt = 0; |
|---|
| 116 |
bio->bi_idx = 0; |
|---|
| 117 |
bio->bi_phys_segments = 0; |
|---|
| 118 |
bio->bi_hw_segments = 0; |
|---|
| 119 |
bio->bi_size = 0; |
|---|
| 120 |
bio->bi_max_vecs = 0; |
|---|
| 121 |
bio->bi_end_io = NULL; |
|---|
| 122 |
atomic_set(&bio->bi_cnt, 1); |
|---|
| 123 |
bio->bi_private = NULL; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
struct bio *bio_alloc(int gfp_mask, int nr_iovecs) |
|---|
| 137 |
{ |
|---|
| 138 |
struct bio_vec *bvl = NULL; |
|---|
| 139 |
unsigned long idx; |
|---|
| 140 |
struct bio *bio; |
|---|
| 141 |
|
|---|
| 142 |
bio = mempool_alloc(bio_pool, gfp_mask); |
|---|
| 143 |
if (unlikely(!bio)) |
|---|
| 144 |
goto out; |
|---|
| 145 |
|
|---|
| 146 |
bio_init(bio); |
|---|
| 147 |
|
|---|
| 148 |
if (unlikely(!nr_iovecs)) |
|---|
| 149 |
goto noiovec; |
|---|
| 150 |
|
|---|
| 151 |
bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx); |
|---|
| 152 |
if (bvl) { |
|---|
| 153 |
bio->bi_flags |= idx << BIO_POOL_OFFSET; |
|---|
| 154 |
bio->bi_max_vecs = bvec_array[idx].nr_vecs; |
|---|
| 155 |
noiovec: |
|---|
| 156 |
bio->bi_io_vec = bvl; |
|---|
| 157 |
bio->bi_destructor = bio_destructor; |
|---|
| 158 |
out: |
|---|
| 159 |
return bio; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
mempool_free(bio, bio_pool); |
|---|
| 163 |
bio = NULL; |
|---|
| 164 |
goto out; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
void bio_put(struct bio *bio) |
|---|
| 176 |
{ |
|---|
| 177 |
BIO_BUG_ON(!atomic_read(&bio->bi_cnt)); |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
if (atomic_dec_and_test(&bio->bi_cnt)) { |
|---|
| 183 |
bio->bi_next = NULL; |
|---|
| 184 |
bio->bi_destructor(bio); |
|---|
| 185 |
} |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
inline int bio_phys_segments(request_queue_t *q, struct bio *bio) |
|---|
| 189 |
{ |
|---|
| 190 |
if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) |
|---|
| 191 |
blk_recount_segments(q, bio); |
|---|
| 192 |
|
|---|
| 193 |
return bio->bi_phys_segments; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
inline int bio_hw_segments(request_queue_t *q, struct bio *bio) |
|---|
| 197 |
{ |
|---|
| 198 |
if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) |
|---|
| 199 |
blk_recount_segments(q, bio); |
|---|
| 200 |
|
|---|
| 201 |
return bio->bi_hw_segments; |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
inline void __bio_clone(struct bio *bio, struct bio *bio_src) |
|---|
| 214 |
{ |
|---|
| 215 |
bio->bi_io_vec = bio_src->bi_io_vec; |
|---|
| 216 |
|
|---|
| 217 |
bio->bi_sector = bio_src->bi_sector; |
|---|
| 218 |
bio->bi_bdev = bio_src->bi_bdev; |
|---|
| 219 |
bio->bi_flags |= 1 << BIO_CLONED; |
|---|
| 220 |
bio->bi_rw = bio_src->bi_rw; |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
bio->bi_vcnt = bio_src->bi_vcnt; |
|---|
| 227 |
bio->bi_idx = bio_src->bi_idx; |
|---|
| 228 |
if (bio_flagged(bio, BIO_SEG_VALID)) { |
|---|
| 229 |
bio->bi_phys_segments = bio_src->bi_phys_segments; |
|---|
| 230 |
bio->bi_hw_segments = bio_src->bi_hw_segments; |
|---|
| 231 |
bio->bi_flags |= (1 << BIO_SEG_VALID); |
|---|
| 232 |
} |
|---|
| 233 |
bio->bi_size = bio_src->bi_size; |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
bio->bi_max_vecs = 0; |
|---|
| 241 |
bio->bi_flags &= (BIO_POOL_MASK - 1); |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
struct bio *bio_clone(struct bio *bio, int gfp_mask) |
|---|
| 252 |
{ |
|---|
| 253 |
struct bio *b = bio_alloc(gfp_mask, 0); |
|---|
| 254 |
|
|---|
| 255 |
if (b) |
|---|
| 256 |
__bio_clone(b, bio); |
|---|
| 257 |
|
|---|
| 258 |
return b; |
|---|
| 259 |
} |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
int bio_get_nr_vecs(struct block_device *bdev) |
|---|
| 271 |
{ |
|---|
| 272 |
request_queue_t *q = bdev_get_queue(bdev); |
|---|
| 273 |
int nr_pages; |
|---|
| 274 |
|
|---|
| 275 |
nr_pages = ((q->max_sectors << 9) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
|---|
| 276 |
if (nr_pages > q->max_phys_segments) |
|---|
| 277 |
nr_pages = q->max_phys_segments; |
|---|
| 278 |
if (nr_pages > q->max_hw_segments) |
|---|
| 279 |
nr_pages = q->max_hw_segments; |
|---|
| 280 |
|
|---|
| 281 |
return nr_pages; |
|---|
| 282 |
} |
|---|
| 283 |
|
|---|
| 284 |
static int __bio_add_page(request_queue_t *q, struct bio *bio, struct page |
|---|
| 285 |
*page, unsigned int len, unsigned int offset) |
|---|
| 286 |
{ |
|---|
| 287 |
int retried_segments = 0; |
|---|
| 288 |
struct bio_vec *bvec; |
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
if (unlikely(bio_flagged(bio, BIO_CLONED))) |
|---|
| 294 |
return 0; |
|---|
| 295 |
|
|---|
| 296 |
if (bio->bi_vcnt >= bio->bi_max_vecs) |
|---|
| 297 |
return 0; |
|---|
| 298 |
|
|---|
| 299 |
if (((bio->bi_size + len) >> 9) > q->max_sectors) |
|---|
| 300 |
return 0; |
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
while (bio_phys_segments(q, bio) >= q->max_phys_segments |
|---|
| 308 |
|| bio_hw_segments(q, bio) >= q->max_hw_segments) { |
|---|
| 309 |
|
|---|
| 310 |
if (retried_segments) |
|---|
| 311 |
return 0; |
|---|
| 312 |
|
|---|
| 313 |
bio->bi_flags &= ~(1 << BIO_SEG_VALID); |
|---|
| 314 |
retried_segments = 1; |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
bvec = &bio->bi_io_vec[bio->bi_vcnt]; |
|---|
| 322 |
bvec->bv_page = page; |
|---|
| 323 |
bvec->bv_len = len; |
|---|
| 324 |
bvec->bv_offset = offset; |
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
if (q->merge_bvec_fn) { |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
if (q->merge_bvec_fn(q, bio, bvec) < len) { |
|---|
| 337 |
bvec->bv_page = NULL; |
|---|
| 338 |
bvec->bv_len = 0; |
|---|
| 339 |
bvec->bv_offset = 0; |
|---|
| 340 |
return 0; |
|---|
| 341 |
} |
|---|
| 342 |
} |
|---|
| 343 |
|
|---|
| 344 |
bio->bi_vcnt++; |
|---|
| 345 |
bio->bi_phys_segments++; |
|---|
| 346 |
bio->bi_hw_segments++; |
|---|
| 347 |
bio->bi_size += len; |
|---|
| 348 |
return len; |
|---|
| 349 |
} |
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
int bio_add_page(struct bio *bio, struct page *page, unsigned int len, |
|---|
| 365 |
unsigned int offset) |
|---|
| 366 |
{ |
|---|
| 367 |
return __bio_add_page(bdev_get_queue(bio->bi_bdev), bio, page, |
|---|
| 368 |
len, offset); |
|---|
| 369 |
} |
|---|
| 370 |
|
|---|
| 371 |
static struct bio *__bio_map_user(request_queue_t *q, struct block_device *bdev, |
|---|
| 372 |
unsigned long uaddr, unsigned int len, |
|---|
| 373 |
int write_to_vm) |
|---|
| 374 |
{ |
|---|
| 375 |
unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
|---|
| 376 |
unsigned long start = uaddr >> PAGE_SHIFT; |
|---|
| 377 |
const int nr_pages = end - start; |
|---|
| 378 |
int ret, offset, i; |
|---|
| 379 |
struct page **pages; |
|---|
| 380 |
struct bio *bio; |
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
if ((uaddr & queue_dma_alignment(q)) || (len & queue_dma_alignment(q))) |
|---|
| 387 |
return NULL; |
|---|
| 388 |
|
|---|
| 389 |
bio = bio_alloc(GFP_KERNEL, nr_pages); |
|---|
| 390 |
if (!bio) |
|---|
| 391 |
return NULL; |
|---|
| 392 |
|
|---|
| 393 |
pages = kmalloc(nr_pages * sizeof(struct page *), GFP_KERNEL); |
|---|
| 394 |
if (!pages) |
|---|
| 395 |
goto out; |
|---|
| 396 |
|
|---|
| 397 |
down_read(¤t->mm->mmap_sem); |
|---|
| 398 |
ret = get_user_pages(current, current->mm, uaddr, nr_pages, |
|---|
| 399 |
write_to_vm, 0, pages, NULL); |
|---|
| 400 |
up_read(¤t->mm->mmap_sem); |
|---|
| 401 |
|
|---|
| 402 |
if (ret < nr_pages) |
|---|
| 403 |
goto out; |
|---|
| 404 |
|
|---|
| 405 |
bio->bi_bdev = bdev; |
|---|
| 406 |
|
|---|
| 407 |
offset = uaddr & ~PAGE_MASK; |
|---|
| 408 |
for (i = 0; i < nr_pages; i++) { |
|---|
| 409 |
unsigned int bytes = PAGE_SIZE - offset; |
|---|
| 410 |
|
|---|
| 411 |
if (len <= 0) |
|---|
| 412 |
break; |
|---|
| 413 |
|
|---|
| 414 |
if (bytes > len) |
|---|
| 415 |
bytes = len; |
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
if (__bio_add_page(q, bio, pages[i], bytes, offset) < bytes) |
|---|
| 421 |
break; |
|---|
| 422 |
|
|---|
| 423 |
len -= bytes; |
|---|
| 424 |
offset = 0; |
|---|
| 425 |
} |
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
while (i < nr_pages) |
|---|
| 431 |
page_cache_release(pages[i++]); |
|---|
| 432 |
|
|---|
| 433 |
kfree(pages); |
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
if (!write_to_vm) |
|---|
| 439 |
bio->bi_rw |= (1 << BIO_RW); |
|---|
| 440 |
|
|---|
| 441 |
blk_queue_bounce(q, &bio); |
|---|
| 442 |
return bio; |
|---|
| 443 |
out: |
|---|
| 444 |
kfree(pages); |
|---|
| 445 |
bio_put(bio); |
|---|
| 446 |
return NULL; |
|---|
| 447 |
} |
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
struct bio *bio_map_user(request_queue_t *q, struct block_device *bdev, |
|---|
| 460 |
unsigned long uaddr, unsigned int len, int write_to_vm) |
|---|
| 461 |
{ |
|---|
| 462 |
struct bio *bio; |
|---|
| 463 |
|
|---|
| 464 |
bio = __bio_map_user(q, bdev, uaddr, len, write_to_vm); |
|---|
| 465 |
|
|---|
| 466 |
if (bio) { |
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
bio_get(bio); |
|---|
| 474 |
|
|---|
| 475 |
if (bio->bi_size < len) { |
|---|
| 476 |
bio_endio(bio, bio->bi_size, 0); |
|---|
| 477 |
bio_unmap_user(bio, 0); |
|---|
| 478 |
return NULL; |
|---|
| 479 |
} |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
return bio; |
|---|
| 483 |
} |
|---|
| 484 |
|
|---|
| 485 |
static void __bio_unmap_user(struct bio *bio, int write_to_vm) |
|---|
| 486 |
{ |
|---|
| 487 |
struct bio_vec *bvec; |
|---|
| 488 |
int i; |
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
if (bio->bi_private) { |
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
BUG_ON(!bio_flagged(bio, BIO_BOUNCED)); |
|---|
| 498 |
|
|---|
| 499 |
bio = bio->bi_private; |
|---|
| 500 |
} |
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
__bio_for_each_segment(bvec, bio, i, 0) { |
|---|
| 506 |
if (write_to_vm) |
|---|
| 507 |
set_page_dirty_lock(bvec->bv_page); |
|---|
| 508 |
|
|---|
| 509 |
page_cache_release(bvec->bv_page); |
|---|
| 510 |
} |
|---|
| 511 |
|
|---|
| 512 |
bio_put(bio); |
|---|
| 513 |
} |
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 |
void bio_unmap_user(struct bio *bio, int write_to_vm) |
|---|
| 527 |
{ |
|---|
| 528 |
__bio_unmap_user(bio, write_to_vm); |
|---|
| 529 |
bio_put(bio); |
|---|
| 530 |
} |
|---|
| 531 |
|
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
|
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
void bio_set_pages_dirty(struct bio *bio) |
|---|
| 562 |
{ |
|---|
| 563 |
struct bio_vec *bvec = bio->bi_io_vec; |
|---|
| 564 |
int i; |
|---|
| 565 |
|
|---|
| 566 |
for (i = 0; i < bio->bi_vcnt; i++) { |
|---|
| 567 |
struct page *page = bvec[i].bv_page; |
|---|
| 568 |
|
|---|
| 569 |
if (page && !PageCompound(page)) |
|---|
| 570 |
set_page_dirty_lock(page); |
|---|
| 571 |
} |
|---|
| 572 |
} |
|---|
| 573 |
|
|---|
| 574 |
static void bio_release_pages(struct bio *bio) |
|---|
| 575 |
{ |
|---|
| 576 |
struct bio_vec *bvec = bio->bi_io_vec; |
|---|
| 577 |
int i; |
|---|
| 578 |
|
|---|
| 579 |
for (i = 0; i < bio->bi_vcnt; i++) { |
|---|
| 580 |
struct page *page = bvec[i].bv_page; |
|---|
| 581 |
|
|---|
| 582 |
if (page) |
|---|
| 583 |
put_page(page); |
|---|
| 584 |
} |
|---|
| 585 |
} |
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
static void bio_dirty_fn(void *data); |
|---|
| 599 |
|
|---|
| 600 |
static DECLARE_WORK(bio_dirty_work, bio_dirty_fn, NULL); |
|---|
| 601 |
static spinlock_t bio_dirty_lock = SPIN_LOCK_UNLOCKED; |
|---|
| 602 |
static struct bio *bio_dirty_list; |
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
|
|---|
| 606 |
|
|---|
| 607 |
static void bio_dirty_fn(void *data) |
|---|
| 608 |
{ |
|---|
| 609 |
unsigned long flags; |
|---|
| 610 |
struct bio *bio; |
|---|
| 611 |
|
|---|
| 612 |
spin_lock_irqsave(&bio_dirty_lock, flags); |
|---|
| 613 |
bio = bio_dirty_list; |
|---|
| 614 |
bio_dirty_list = NULL; |
|---|
| 615 |
spin_unlock_irqrestore(&bio_dirty_lock, flags); |
|---|
| 616 |
|
|---|
| 617 |
while (bio) { |
|---|
| 618 |
struct bio *next = bio->bi_private; |
|---|
| 619 |
|
|---|
| 620 |
bio_set_pages_dirty(bio); |
|---|
| 621 |
bio_release_pages(bio); |
|---|
| 622 |
bio_put(bio); |
|---|
| 623 |
bio = next; |
|---|
| 624 |
} |
|---|
| 625 |
} |
|---|
| 626 |
|
|---|
| 627 |
void bio_check_pages_dirty(struct bio *bio) |
|---|
| 628 |
{ |
|---|
| 629 |
struct bio_vec *bvec = bio->bi_io_vec; |
|---|
| 630 |
int nr_clean_pages = 0; |
|---|
| 631 |
int i; |
|---|
| 632 |
|
|---|
| 633 |
for (i = 0; i < bio->bi_vcnt; i++) { |
|---|
| 634 |
struct page *page = bvec[i].bv_page; |
|---|
| 635 |
|
|---|
| 636 |
if (PageDirty(page) || PageCompound(page)) { |
|---|
| 637 |
page_cache_release(page); |
|---|
| 638 |
bvec[i].bv_page = NULL; |
|---|
| 639 |
} else { |
|---|
| 640 |
nr_clean_pages++; |
|---|
| 641 |
} |
|---|
| 642 |
} |
|---|
| 643 |
|
|---|
| 644 |
if (nr_clean_pages) { |
|---|
| 645 |
unsigned long flags; |
|---|
| 646 |
|
|---|
| 647 |
spin_lock_irqsave(&bio_dirty_lock, flags); |
|---|
| 648 |
bio->bi_private = bio_dirty_list; |
|---|
| 649 |
bio_dirty_list = bio; |
|---|
| 650 |
spin_unlock_irqrestore(&bio_dirty_lock, flags); |
|---|
| 651 |
schedule_work(&bio_dirty_work); |
|---|
| 652 |
} else { |
|---|
| 653 |
bio_put(bio); |
|---|
| 654 |
} |
|---|
| 655 |
} |
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
|
|---|
| 668 |
|
|---|
| 669 |
|
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
void bio_endio(struct bio *bio, unsigned int bytes_done, int error) |
|---|
| 673 |
{ |
|---|
| 674 |
if (error) |
|---|
| 675 |
clear_bit(BIO_UPTODATE, &bio->bi_flags); |
|---|
| 676 |
|
|---|
| 677 |
if (unlikely(bytes_done > bio->bi_size)) { |
|---|
| 678 |
printk("%s: want %u bytes done, only %u left\n", __FUNCTION__, |
|---|
| 679 |
bytes_done, bio->bi_size); |
|---|
| 680 |
bytes_done = bio->bi_size; |
|---|
| 681 |
} |
|---|
| 682 |
|
|---|
| 683 |
bio->bi_size -= bytes_done; |
|---|
| 684 |
bio->bi_sector += (bytes_done >> 9); |
|---|
| 685 |
|
|---|
| 686 |
if (bio->bi_end_io) |
|---|
| 687 |
bio->bi_end_io(bio, bytes_done, error); |
|---|
| 688 |
} |
|---|
| 689 |
|
|---|
| 690 |
void bio_pair_release(struct bio_pair *bp) |
|---|
| 691 |
{ |
|---|
| 692 |
if (atomic_dec_and_test(&bp->cnt)) { |
|---|
| 693 |
struct bio *master = bp->bio1.bi_private; |
|---|
| 694 |
|
|---|
| 695 |
bio_endio(master, master->bi_size, bp->error); |
|---|
| 696 |
mempool_free(bp, bp->bio2.bi_private); |
|---|
| 697 |
} |
|---|
| 698 |
} |
|---|
| 699 |
|
|---|
| 700 |
static int bio_pair_end_1(struct bio * bi, unsigned int done, int err) |
|---|
| 701 |
{ |
|---|
| 702 |
struct bio_pair *bp = container_of(bi, struct bio_pair, bio1); |
|---|
| 703 |
|
|---|
| 704 |
if (err) |
|---|
| 705 |
bp->error = err; |
|---|
| 706 |
|
|---|
| 707 |
if (bi->bi_size) |
|---|
| 708 |
return 1; |
|---|
| 709 |
|
|---|
| 710 |
bio_pair_release(bp); |
|---|
| 711 |
return 0; |
|---|
| 712 |
} |
|---|
| 713 |
|
|---|
| 714 |
static int bio_pair_end_2(struct bio * bi, unsigned int done, int err) |
|---|
| 715 |
{ |
|---|
| 716 |
struct bio_pair *bp = container_of(bi, struct bio_pair, bio2); |
|---|
| 717 |
|
|---|
| 718 |
if (err) |
|---|
| 719 |
bp->error = err; |
|---|
| 720 |
|
|---|
| 721 |
if (bi->bi_size) |
|---|
| 722 |
return 1; |
|---|
| 723 |
|
|---|
| 724 |
bio_pair_release(bp); |
|---|
| 725 |
return 0; |
|---|
| 726 |
} |
|---|
| 727 |
|
|---|
| 728 |
|
|---|
| 729 |
|
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
struct bio_pair *bio_split(struct bio *bi, mempool_t *pool, int first_sectors) |
|---|
| 733 |
{ |
|---|
| 734 |
struct bio_pair *bp = mempool_alloc(pool, GFP_NOIO); |
|---|
| 735 |
|
|---|
| 736 |
if (!bp) |
|---|
| 737 |
return bp; |
|---|
| 738 |
|
|---|
| 739 |
BUG_ON(bi->bi_vcnt != 1); |
|---|
| 740 |
BUG_ON(bi->bi_idx != 0); |
|---|
| 741 |
atomic_set(&bp->cnt, 3); |
|---|
| 742 |
bp->error = 0; |
|---|
| 743 |
bp->bio1 = *bi; |
|---|
| 744 |
bp->bio2 = *bi; |
|---|
| 745 |
bp->bio2.bi_sector += first_sectors; |
|---|
| 746 |
bp->bio2.bi_size -= first_sectors << 9; |
|---|
| 747 |
bp->bio1.bi_size = first_sectors << 9; |
|---|
| 748 |
|
|---|
| 749 |
bp->bv1 = bi->bi_io_vec[0]; |
|---|
| 750 |
bp->bv2 = bi->bi_io_vec[0]; |
|---|
| 751 |
bp->bv2.bv_offset += first_sectors << 9; |
|---|
| 752 |
bp->bv2.bv_len -= first_sectors << 9; |
|---|
| 753 |
bp->bv1.bv_len = first_sectors << 9; |
|---|
| 754 |
|
|---|
| 755 |
bp->bio1.bi_io_vec = &bp->bv1; |
|---|
| 756 |
bp->bio2.bi_io_vec = &bp->bv2; |
|---|
| 757 |
|
|---|
| 758 |
bp->bio1.bi_end_io = bio_pair_end_1; |
|---|
| 759 |
bp->bio2.bi_end_io = bio_pair_end_2; |
|---|
| 760 |
|
|---|
| 761 |
bp->bio1.bi_private = bi; |
|---|
| 762 |
bp->bio2.bi_private = pool; |
|---|
| 763 |
|
|---|
| 764 |
return bp; |
|---|
| 765 |
} |
|---|
| 766 |
|
|---|
| 767 |
static void *bio_pair_alloc(int gfp_flags, void *data) |
|---|
| 768 |
{ |
|---|
| 769 |
return kmalloc(sizeof(struct bio_pair), gfp_flags); |
|---|
| 770 |
} |
|---|
| 771 |
|
|---|
| 772 |
static void bio_pair_free(void *bp, void *data) |
|---|
| 773 |
{ |
|---|
| 774 |
kfree(bp); |
|---|
| 775 |
} |
|---|
| 776 |
|
|---|
| 777 |
static void __init biovec_init_pools(void) |
|---|
| 778 |
{ |
|---|
| 779 |
int i, size, megabytes, pool_entries = BIO_POOL_SIZE; |
|---|
| 780 |
int scale = BIOVEC_NR_POOLS; |
|---|
| 781 |
|
|---|
| 782 |
megabytes = nr_free_pages() >> (20 - PAGE_SHIFT); |
|---|
| 783 |
|
|---|
| 784 |
|
|---|
| 785 |
|
|---|
| 786 |
|
|---|
| 787 |
if (megabytes <= 16) |
|---|
| 788 |
scale = 0; |
|---|
| 789 |
else if (megabytes <= 32) |
|---|
| 790 |
scale = 1; |
|---|
| 791 |
else if (megabytes <= 64) |
|---|
| 792 |
scale = 2; |
|---|
| 793 |
else if (megabytes <= 96) |
|---|
| 794 |
scale = 3; |
|---|
| 795 |
else if (megabytes <= 128) |
|---|
| 796 |
scale = 4; |
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
pool_entries = megabytes * 2; |
|---|
| 802 |
if (pool_entries > 256) |
|---|
| 803 |
pool_entries = 256; |
|---|
| 804 |
|
|---|
| 805 |
for (i = 0; i < BIOVEC_NR_POOLS; i++) { |
|---|
| 806 |
struct biovec_pool *bp = bvec_array + i; |
|---|
| 807 |
|
|---|
| 808 |
size = bp->nr_vecs * sizeof(struct bio_vec); |
|---|
| 809 |
|
|---|
| 810 |
bp->slab = kmem_cache_create(bp->name, size, 0, |
|---|
| 811 |
SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); |
|---|
| 812 |
|
|---|
| 813 |
if (i >= scale) |
|---|
| 814 |
pool_entries >>= 1; |
|---|
| 815 |
|
|---|
| 816 |
bp->pool = mempool_create(pool_entries, mempool_alloc_slab, |
|---|
| 817 |
mempool_free_slab, bp->slab); |
|---|
| 818 |
if (!bp->pool) |
|---|
| 819 |
panic("biovec: can't init mempool\n"); |
|---|
| 820 |
} |
|---|
| 821 |
} |
|---|
| 822 |
|
|---|
| 823 |
static int __init init_bio(void) |
|---|
| 824 |
{ |
|---|
| 825 |
bio_slab = kmem_cache_create("bio", sizeof(struct bio), 0, |
|---|
| 826 |
SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL); |
|---|
| 827 |
bio_pool = mempool_create(BIO_POOL_SIZE, mempool_alloc_slab, |
|---|
| 828 |
mempool_free_slab, bio_slab); |
|---|
| 829 |
if (!bio_pool) |
|---|
| 830 |
panic("bio: can't create mempool\n"); |
|---|
| 831 |
|
|---|
| 832 |
biovec_init_pools(); |
|---|
| 833 |
|
|---|
| 834 |
bio_split_pool = mempool_create(BIO_SPLIT_ENTRIES, |
|---|
| 835 |
bio_pair_alloc, bio_pair_free, NULL); |
|---|
| 836 |
if (!bio_split_pool) |
|---|
| 837 |
panic("bio: can't create split pool\n"); |
|---|
| 838 |
|
|---|
| 839 |
return 0; |
|---|
| 840 |
} |
|---|
| 841 |
|
|---|
| 842 |
subsys_initcall(init_bio); |
|---|
| 843 |
|
|---|
| 844 |
EXPORT_SYMBOL(bio_alloc); |
|---|
| 845 |
EXPORT_SYMBOL(bio_put); |
|---|
| 846 |
EXPORT_SYMBOL(bio_endio); |
|---|
| 847 |
EXPORT_SYMBOL(bio_init); |
|---|
| 848 |
EXPORT_SYMBOL(__bio_clone); |
|---|
| 849 |
EXPORT_SYMBOL(bio_clone); |
|---|
| 850 |
EXPORT_SYMBOL(bio_phys_segments); |
|---|
| 851 |
EXPORT_SYMBOL(bio_hw_segments); |
|---|
| 852 |
EXPORT_SYMBOL(bio_add_page); |
|---|
| 853 |
EXPORT_SYMBOL(bio_get_nr_vecs); |
|---|
| 854 |
EXPORT_SYMBOL(bio_map_user); |
|---|
| 855 |
EXPORT_SYMBOL(bio_unmap_user); |
|---|
| 856 |
EXPORT_SYMBOL(bio_pair_release); |
|---|
| 857 |
EXPORT_SYMBOL(bio_split); |
|---|
| 858 |
EXPORT_SYMBOL(bio_split_pool); |
|---|