PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/bio.c

https://github.com/mstsirkin/linux
C | 1692 lines | 1025 code | 269 blank | 398 comment | 170 complexity | d163c570500eabcdda6dacafc49a8e39 MD5 | raw file
  1. /*
  2. * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
  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>
  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. #include <scsi/sg.h> /* for struct sg_iovec */
  29. #include <trace/events/block.h>
  30. /*
  31. * Test patch to inline a certain number of bi_io_vec's inside the bio
  32. * itself, to shrink a bio data allocation from two mempool calls to one
  33. */
  34. #define BIO_INLINE_VECS 4
  35. static mempool_t *bio_split_pool __read_mostly;
  36. /*
  37. * if you change this list, also change bvec_alloc or things will
  38. * break badly! cannot be bigger than what you can fit into an
  39. * unsigned short
  40. */
  41. #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
  42. static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
  43. BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
  44. };
  45. #undef BV
  46. /*
  47. * fs_bio_set is the bio_set containing bio and iovec memory pools used by
  48. * IO code that does not need private memory pools.
  49. */
  50. struct bio_set *fs_bio_set;
  51. /*
  52. * Our slab pool management
  53. */
  54. struct bio_slab {
  55. struct kmem_cache *slab;
  56. unsigned int slab_ref;
  57. unsigned int slab_size;
  58. char name[8];
  59. };
  60. static DEFINE_MUTEX(bio_slab_lock);
  61. static struct bio_slab *bio_slabs;
  62. static unsigned int bio_slab_nr, bio_slab_max;
  63. static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
  64. {
  65. unsigned int sz = sizeof(struct bio) + extra_size;
  66. struct kmem_cache *slab = NULL;
  67. struct bio_slab *bslab;
  68. unsigned int i, entry = -1;
  69. mutex_lock(&bio_slab_lock);
  70. i = 0;
  71. while (i < bio_slab_nr) {
  72. bslab = &bio_slabs[i];
  73. if (!bslab->slab && entry == -1)
  74. entry = i;
  75. else if (bslab->slab_size == sz) {
  76. slab = bslab->slab;
  77. bslab->slab_ref++;
  78. break;
  79. }
  80. i++;
  81. }
  82. if (slab)
  83. goto out_unlock;
  84. if (bio_slab_nr == bio_slab_max && entry == -1) {
  85. bio_slab_max <<= 1;
  86. bio_slabs = krealloc(bio_slabs,
  87. bio_slab_max * sizeof(struct bio_slab),
  88. GFP_KERNEL);
  89. if (!bio_slabs)
  90. goto out_unlock;
  91. }
  92. if (entry == -1)
  93. entry = bio_slab_nr++;
  94. bslab = &bio_slabs[entry];
  95. snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
  96. slab = kmem_cache_create(bslab->name, sz, 0, SLAB_HWCACHE_ALIGN, NULL);
  97. if (!slab)
  98. goto out_unlock;
  99. printk(KERN_INFO "bio: create slab <%s> at %d\n", bslab->name, entry);
  100. bslab->slab = slab;
  101. bslab->slab_ref = 1;
  102. bslab->slab_size = sz;
  103. out_unlock:
  104. mutex_unlock(&bio_slab_lock);
  105. return slab;
  106. }
  107. static void bio_put_slab(struct bio_set *bs)
  108. {
  109. struct bio_slab *bslab = NULL;
  110. unsigned int i;
  111. mutex_lock(&bio_slab_lock);
  112. for (i = 0; i < bio_slab_nr; i++) {
  113. if (bs->bio_slab == bio_slabs[i].slab) {
  114. bslab = &bio_slabs[i];
  115. break;
  116. }
  117. }
  118. if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
  119. goto out;
  120. WARN_ON(!bslab->slab_ref);
  121. if (--bslab->slab_ref)
  122. goto out;
  123. kmem_cache_destroy(bslab->slab);
  124. bslab->slab = NULL;
  125. out:
  126. mutex_unlock(&bio_slab_lock);
  127. }
  128. unsigned int bvec_nr_vecs(unsigned short idx)
  129. {
  130. return bvec_slabs[idx].nr_vecs;
  131. }
  132. void bvec_free_bs(struct bio_set *bs, struct bio_vec *bv, unsigned int idx)
  133. {
  134. BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
  135. if (idx == BIOVEC_MAX_IDX)
  136. mempool_free(bv, bs->bvec_pool);
  137. else {
  138. struct biovec_slab *bvs = bvec_slabs + idx;
  139. kmem_cache_free(bvs->slab, bv);
  140. }
  141. }
  142. struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx,
  143. struct bio_set *bs)
  144. {
  145. struct bio_vec *bvl;
  146. /*
  147. * see comment near bvec_array define!
  148. */
  149. switch (nr) {
  150. case 1:
  151. *idx = 0;
  152. break;
  153. case 2 ... 4:
  154. *idx = 1;
  155. break;
  156. case 5 ... 16:
  157. *idx = 2;
  158. break;
  159. case 17 ... 64:
  160. *idx = 3;
  161. break;
  162. case 65 ... 128:
  163. *idx = 4;
  164. break;
  165. case 129 ... BIO_MAX_PAGES:
  166. *idx = 5;
  167. break;
  168. default:
  169. return NULL;
  170. }
  171. /*
  172. * idx now points to the pool we want to allocate from. only the
  173. * 1-vec entry pool is mempool backed.
  174. */
  175. if (*idx == BIOVEC_MAX_IDX) {
  176. fallback:
  177. bvl = mempool_alloc(bs->bvec_pool, gfp_mask);
  178. } else {
  179. struct biovec_slab *bvs = bvec_slabs + *idx;
  180. gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
  181. /*
  182. * Make this allocation restricted and don't dump info on
  183. * allocation failures, since we'll fallback to the mempool
  184. * in case of failure.
  185. */
  186. __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
  187. /*
  188. * Try a slab allocation. If this fails and __GFP_WAIT
  189. * is set, retry with the 1-entry mempool
  190. */
  191. bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
  192. if (unlikely(!bvl && (gfp_mask & __GFP_WAIT))) {
  193. *idx = BIOVEC_MAX_IDX;
  194. goto fallback;
  195. }
  196. }
  197. return bvl;
  198. }
  199. void bio_free(struct bio *bio, struct bio_set *bs)
  200. {
  201. void *p;
  202. if (bio_has_allocated_vec(bio))
  203. bvec_free_bs(bs, bio->bi_io_vec, BIO_POOL_IDX(bio));
  204. if (bio_integrity(bio))
  205. bio_integrity_free(bio, bs);
  206. /*
  207. * If we have front padding, adjust the bio pointer before freeing
  208. */
  209. p = bio;
  210. if (bs->front_pad)
  211. p -= bs->front_pad;
  212. mempool_free(p, bs->bio_pool);
  213. }
  214. EXPORT_SYMBOL(bio_free);
  215. void bio_init(struct bio *bio)
  216. {
  217. memset(bio, 0, sizeof(*bio));
  218. bio->bi_flags = 1 << BIO_UPTODATE;
  219. bio->bi_comp_cpu = -1;
  220. atomic_set(&bio->bi_cnt, 1);
  221. }
  222. EXPORT_SYMBOL(bio_init);
  223. /**
  224. * bio_alloc_bioset - allocate a bio for I/O
  225. * @gfp_mask: the GFP_ mask given to the slab allocator
  226. * @nr_iovecs: number of iovecs to pre-allocate
  227. * @bs: the bio_set to allocate from.
  228. *
  229. * Description:
  230. * bio_alloc_bioset will try its own mempool to satisfy the allocation.
  231. * If %__GFP_WAIT is set then we will block on the internal pool waiting
  232. * for a &struct bio to become free.
  233. *
  234. * Note that the caller must set ->bi_destructor on successful return
  235. * of a bio, to do the appropriate freeing of the bio once the reference
  236. * count drops to zero.
  237. **/
  238. struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
  239. {
  240. unsigned long idx = BIO_POOL_NONE;
  241. struct bio_vec *bvl = NULL;
  242. struct bio *bio;
  243. void *p;
  244. p = mempool_alloc(bs->bio_pool, gfp_mask);
  245. if (unlikely(!p))
  246. return NULL;
  247. bio = p + bs->front_pad;
  248. bio_init(bio);
  249. if (unlikely(!nr_iovecs))
  250. goto out_set;
  251. if (nr_iovecs <= BIO_INLINE_VECS) {
  252. bvl = bio->bi_inline_vecs;
  253. nr_iovecs = BIO_INLINE_VECS;
  254. } else {
  255. bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
  256. if (unlikely(!bvl))
  257. goto err_free;
  258. nr_iovecs = bvec_nr_vecs(idx);
  259. }
  260. out_set:
  261. bio->bi_flags |= idx << BIO_POOL_OFFSET;
  262. bio->bi_max_vecs = nr_iovecs;
  263. bio->bi_io_vec = bvl;
  264. return bio;
  265. err_free:
  266. mempool_free(p, bs->bio_pool);
  267. return NULL;
  268. }
  269. EXPORT_SYMBOL(bio_alloc_bioset);
  270. static void bio_fs_destructor(struct bio *bio)
  271. {
  272. bio_free(bio, fs_bio_set);
  273. }
  274. /**
  275. * bio_alloc - allocate a new bio, memory pool backed
  276. * @gfp_mask: allocation mask to use
  277. * @nr_iovecs: number of iovecs
  278. *
  279. * bio_alloc will allocate a bio and associated bio_vec array that can hold
  280. * at least @nr_iovecs entries. Allocations will be done from the
  281. * fs_bio_set. Also see @bio_alloc_bioset and @bio_kmalloc.
  282. *
  283. * If %__GFP_WAIT is set, then bio_alloc will always be able to allocate
  284. * a bio. This is due to the mempool guarantees. To make this work, callers
  285. * must never allocate more than 1 bio at a time from this pool. Callers
  286. * that need to allocate more than 1 bio must always submit the previously
  287. * allocated bio for IO before attempting to allocate a new one. Failure to
  288. * do so can cause livelocks under memory pressure.
  289. *
  290. * RETURNS:
  291. * Pointer to new bio on success, NULL on failure.
  292. */
  293. struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs)
  294. {
  295. struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
  296. if (bio)
  297. bio->bi_destructor = bio_fs_destructor;
  298. return bio;
  299. }
  300. EXPORT_SYMBOL(bio_alloc);
  301. static void bio_kmalloc_destructor(struct bio *bio)
  302. {
  303. if (bio_integrity(bio))
  304. bio_integrity_free(bio, fs_bio_set);
  305. kfree(bio);
  306. }
  307. /**
  308. * bio_kmalloc - allocate a bio for I/O using kmalloc()
  309. * @gfp_mask: the GFP_ mask given to the slab allocator
  310. * @nr_iovecs: number of iovecs to pre-allocate
  311. *
  312. * Description:
  313. * Allocate a new bio with @nr_iovecs bvecs. If @gfp_mask contains
  314. * %__GFP_WAIT, the allocation is guaranteed to succeed.
  315. *
  316. **/
  317. struct bio *bio_kmalloc(gfp_t gfp_mask, int nr_iovecs)
  318. {
  319. struct bio *bio;
  320. if (nr_iovecs > UIO_MAXIOV)
  321. return NULL;
  322. bio = kmalloc(sizeof(struct bio) + nr_iovecs * sizeof(struct bio_vec),
  323. gfp_mask);
  324. if (unlikely(!bio))
  325. return NULL;
  326. bio_init(bio);
  327. bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
  328. bio->bi_max_vecs = nr_iovecs;
  329. bio->bi_io_vec = bio->bi_inline_vecs;
  330. bio->bi_destructor = bio_kmalloc_destructor;
  331. return bio;
  332. }
  333. EXPORT_SYMBOL(bio_kmalloc);
  334. void zero_fill_bio(struct bio *bio)
  335. {
  336. unsigned long flags;
  337. struct bio_vec *bv;
  338. int i;
  339. bio_for_each_segment(bv, bio, i) {
  340. char *data = bvec_kmap_irq(bv, &flags);
  341. memset(data, 0, bv->bv_len);
  342. flush_dcache_page(bv->bv_page);
  343. bvec_kunmap_irq(data, &flags);
  344. }
  345. }
  346. EXPORT_SYMBOL(zero_fill_bio);
  347. /**
  348. * bio_put - release a reference to a bio
  349. * @bio: bio to release reference to
  350. *
  351. * Description:
  352. * Put a reference to a &struct bio, either one you have gotten with
  353. * bio_alloc, bio_get or bio_clone. The last put of a bio will free it.
  354. **/
  355. void bio_put(struct bio *bio)
  356. {
  357. BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
  358. /*
  359. * last put frees it
  360. */
  361. if (atomic_dec_and_test(&bio->bi_cnt)) {
  362. bio->bi_next = NULL;
  363. bio->bi_destructor(bio);
  364. }
  365. }
  366. EXPORT_SYMBOL(bio_put);
  367. inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
  368. {
  369. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  370. blk_recount_segments(q, bio);
  371. return bio->bi_phys_segments;
  372. }
  373. EXPORT_SYMBOL(bio_phys_segments);
  374. /**
  375. * __bio_clone - clone a bio
  376. * @bio: destination bio
  377. * @bio_src: bio to clone
  378. *
  379. * Clone a &bio. Caller will own the returned bio, but not
  380. * the actual data it points to. Reference count of returned
  381. * bio will be one.
  382. */
  383. void __bio_clone(struct bio *bio, struct bio *bio_src)
  384. {
  385. memcpy(bio->bi_io_vec, bio_src->bi_io_vec,
  386. bio_src->bi_max_vecs * sizeof(struct bio_vec));
  387. /*
  388. * most users will be overriding ->bi_bdev with a new target,
  389. * so we don't set nor calculate new physical/hw segment counts here
  390. */
  391. bio->bi_sector = bio_src->bi_sector;
  392. bio->bi_bdev = bio_src->bi_bdev;
  393. bio->bi_flags |= 1 << BIO_CLONED;
  394. bio->bi_rw = bio_src->bi_rw;
  395. bio->bi_vcnt = bio_src->bi_vcnt;
  396. bio->bi_size = bio_src->bi_size;
  397. bio->bi_idx = bio_src->bi_idx;
  398. }
  399. EXPORT_SYMBOL(__bio_clone);
  400. /**
  401. * bio_clone - clone a bio
  402. * @bio: bio to clone
  403. * @gfp_mask: allocation priority
  404. *
  405. * Like __bio_clone, only also allocates the returned bio
  406. */
  407. struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
  408. {
  409. struct bio *b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, fs_bio_set);
  410. if (!b)
  411. return NULL;
  412. b->bi_destructor = bio_fs_destructor;
  413. __bio_clone(b, bio);
  414. if (bio_integrity(bio)) {
  415. int ret;
  416. ret = bio_integrity_clone(b, bio, gfp_mask, fs_bio_set);
  417. if (ret < 0) {
  418. bio_put(b);
  419. return NULL;
  420. }
  421. }
  422. return b;
  423. }
  424. EXPORT_SYMBOL(bio_clone);
  425. /**
  426. * bio_get_nr_vecs - return approx number of vecs
  427. * @bdev: I/O target
  428. *
  429. * Return the approximate number of pages we can send to this target.
  430. * There's no guarantee that you will be able to fit this number of pages
  431. * into a bio, it does not account for dynamic restrictions that vary
  432. * on offset.
  433. */
  434. int bio_get_nr_vecs(struct block_device *bdev)
  435. {
  436. struct request_queue *q = bdev_get_queue(bdev);
  437. int nr_pages;
  438. nr_pages = ((queue_max_sectors(q) << 9) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  439. if (nr_pages > queue_max_segments(q))
  440. nr_pages = queue_max_segments(q);
  441. return nr_pages;
  442. }
  443. EXPORT_SYMBOL(bio_get_nr_vecs);
  444. static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
  445. *page, unsigned int len, unsigned int offset,
  446. unsigned short max_sectors)
  447. {
  448. int retried_segments = 0;
  449. struct bio_vec *bvec;
  450. /*
  451. * cloned bio must not modify vec list
  452. */
  453. if (unlikely(bio_flagged(bio, BIO_CLONED)))
  454. return 0;
  455. if (((bio->bi_size + len) >> 9) > max_sectors)
  456. return 0;
  457. /*
  458. * For filesystems with a blocksize smaller than the pagesize
  459. * we will often be called with the same page as last time and
  460. * a consecutive offset. Optimize this special case.
  461. */
  462. if (bio->bi_vcnt > 0) {
  463. struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  464. if (page == prev->bv_page &&
  465. offset == prev->bv_offset + prev->bv_len) {
  466. unsigned int prev_bv_len = prev->bv_len;
  467. prev->bv_len += len;
  468. if (q->merge_bvec_fn) {
  469. struct bvec_merge_data bvm = {
  470. /* prev_bvec is already charged in
  471. bi_size, discharge it in order to
  472. simulate merging updated prev_bvec
  473. as new bvec. */
  474. .bi_bdev = bio->bi_bdev,
  475. .bi_sector = bio->bi_sector,
  476. .bi_size = bio->bi_size - prev_bv_len,
  477. .bi_rw = bio->bi_rw,
  478. };
  479. if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) {
  480. prev->bv_len -= len;
  481. return 0;
  482. }
  483. }
  484. goto done;
  485. }
  486. }
  487. if (bio->bi_vcnt >= bio->bi_max_vecs)
  488. return 0;
  489. /*
  490. * we might lose a segment or two here, but rather that than
  491. * make this too complex.
  492. */
  493. while (bio->bi_phys_segments >= queue_max_segments(q)) {
  494. if (retried_segments)
  495. return 0;
  496. retried_segments = 1;
  497. blk_recount_segments(q, bio);
  498. }
  499. /*
  500. * setup the new entry, we might clear it again later if we
  501. * cannot add the page
  502. */
  503. bvec = &bio->bi_io_vec[bio->bi_vcnt];
  504. bvec->bv_page = page;
  505. bvec->bv_len = len;
  506. bvec->bv_offset = offset;
  507. /*
  508. * if queue has other restrictions (eg varying max sector size
  509. * depending on offset), it can specify a merge_bvec_fn in the
  510. * queue to get further control
  511. */
  512. if (q->merge_bvec_fn) {
  513. struct bvec_merge_data bvm = {
  514. .bi_bdev = bio->bi_bdev,
  515. .bi_sector = bio->bi_sector,
  516. .bi_size = bio->bi_size,
  517. .bi_rw = bio->bi_rw,
  518. };
  519. /*
  520. * merge_bvec_fn() returns number of bytes it can accept
  521. * at this offset
  522. */
  523. if (q->merge_bvec_fn(q, &bvm, bvec) < bvec->bv_len) {
  524. bvec->bv_page = NULL;
  525. bvec->bv_len = 0;
  526. bvec->bv_offset = 0;
  527. return 0;
  528. }
  529. }
  530. /* If we may be able to merge these biovecs, force a recount */
  531. if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
  532. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  533. bio->bi_vcnt++;
  534. bio->bi_phys_segments++;
  535. done:
  536. bio->bi_size += len;
  537. return len;
  538. }
  539. /**
  540. * bio_add_pc_page - attempt to add page to bio
  541. * @q: the target queue
  542. * @bio: destination bio
  543. * @page: page to add
  544. * @len: vec entry length
  545. * @offset: vec entry offset
  546. *
  547. * Attempt to add a page to the bio_vec maplist. This can fail for a
  548. * number of reasons, such as the bio being full or target block device
  549. * limitations. The target block device must allow bio's up to PAGE_SIZE,
  550. * so it is always possible to add a single page to an empty bio.
  551. *
  552. * This should only be used by REQ_PC bios.
  553. */
  554. int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
  555. unsigned int len, unsigned int offset)
  556. {
  557. return __bio_add_page(q, bio, page, len, offset,
  558. queue_max_hw_sectors(q));
  559. }
  560. EXPORT_SYMBOL(bio_add_pc_page);
  561. /**
  562. * bio_add_page - attempt to add page to bio
  563. * @bio: destination bio
  564. * @page: page to add
  565. * @len: vec entry length
  566. * @offset: vec entry offset
  567. *
  568. * Attempt to add a page to the bio_vec maplist. This can fail for a
  569. * number of reasons, such as the bio being full or target block device
  570. * limitations. The target block device must allow bio's up to PAGE_SIZE,
  571. * so it is always possible to add a single page to an empty bio.
  572. */
  573. int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
  574. unsigned int offset)
  575. {
  576. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  577. return __bio_add_page(q, bio, page, len, offset, queue_max_sectors(q));
  578. }
  579. EXPORT_SYMBOL(bio_add_page);
  580. struct bio_map_data {
  581. struct bio_vec *iovecs;
  582. struct sg_iovec *sgvecs;
  583. int nr_sgvecs;
  584. int is_our_pages;
  585. };
  586. static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio,
  587. struct sg_iovec *iov, int iov_count,
  588. int is_our_pages)
  589. {
  590. memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
  591. memcpy(bmd->sgvecs, iov, sizeof(struct sg_iovec) * iov_count);
  592. bmd->nr_sgvecs = iov_count;
  593. bmd->is_our_pages = is_our_pages;
  594. bio->bi_private = bmd;
  595. }
  596. static void bio_free_map_data(struct bio_map_data *bmd)
  597. {
  598. kfree(bmd->iovecs);
  599. kfree(bmd->sgvecs);
  600. kfree(bmd);
  601. }
  602. static struct bio_map_data *bio_alloc_map_data(int nr_segs, int iov_count,
  603. gfp_t gfp_mask)
  604. {
  605. struct bio_map_data *bmd;
  606. if (iov_count > UIO_MAXIOV)
  607. return NULL;
  608. bmd = kmalloc(sizeof(*bmd), gfp_mask);
  609. if (!bmd)
  610. return NULL;
  611. bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, gfp_mask);
  612. if (!bmd->iovecs) {
  613. kfree(bmd);
  614. return NULL;
  615. }
  616. bmd->sgvecs = kmalloc(sizeof(struct sg_iovec) * iov_count, gfp_mask);
  617. if (bmd->sgvecs)
  618. return bmd;
  619. kfree(bmd->iovecs);
  620. kfree(bmd);
  621. return NULL;
  622. }
  623. static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
  624. struct sg_iovec *iov, int iov_count,
  625. int to_user, int from_user, int do_free_page)
  626. {
  627. int ret = 0, i;
  628. struct bio_vec *bvec;
  629. int iov_idx = 0;
  630. unsigned int iov_off = 0;
  631. __bio_for_each_segment(bvec, bio, i, 0) {
  632. char *bv_addr = page_address(bvec->bv_page);
  633. unsigned int bv_len = iovecs[i].bv_len;
  634. while (bv_len && iov_idx < iov_count) {
  635. unsigned int bytes;
  636. char __user *iov_addr;
  637. bytes = min_t(unsigned int,
  638. iov[iov_idx].iov_len - iov_off, bv_len);
  639. iov_addr = iov[iov_idx].iov_base + iov_off;
  640. if (!ret) {
  641. if (to_user)
  642. ret = copy_to_user(iov_addr, bv_addr,
  643. bytes);
  644. if (from_user)
  645. ret = copy_from_user(bv_addr, iov_addr,
  646. bytes);
  647. if (ret)
  648. ret = -EFAULT;
  649. }
  650. bv_len -= bytes;
  651. bv_addr += bytes;
  652. iov_addr += bytes;
  653. iov_off += bytes;
  654. if (iov[iov_idx].iov_len == iov_off) {
  655. iov_idx++;
  656. iov_off = 0;
  657. }
  658. }
  659. if (do_free_page)
  660. __free_page(bvec->bv_page);
  661. }
  662. return ret;
  663. }
  664. /**
  665. * bio_uncopy_user - finish previously mapped bio
  666. * @bio: bio being terminated
  667. *
  668. * Free pages allocated from bio_copy_user() and write back data
  669. * to user space in case of a read.
  670. */
  671. int bio_uncopy_user(struct bio *bio)
  672. {
  673. struct bio_map_data *bmd = bio->bi_private;
  674. int ret = 0;
  675. if (!bio_flagged(bio, BIO_NULL_MAPPED))
  676. ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs,
  677. bmd->nr_sgvecs, bio_data_dir(bio) == READ,
  678. 0, bmd->is_our_pages);
  679. bio_free_map_data(bmd);
  680. bio_put(bio);
  681. return ret;
  682. }
  683. EXPORT_SYMBOL(bio_uncopy_user);
  684. /**
  685. * bio_copy_user_iov - copy user data to bio
  686. * @q: destination block queue
  687. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  688. * @iov: the iovec.
  689. * @iov_count: number of elements in the iovec
  690. * @write_to_vm: bool indicating writing to pages or not
  691. * @gfp_mask: memory allocation flags
  692. *
  693. * Prepares and returns a bio for indirect user io, bouncing data
  694. * to/from kernel pages as necessary. Must be paired with
  695. * call bio_uncopy_user() on io completion.
  696. */
  697. struct bio *bio_copy_user_iov(struct request_queue *q,
  698. struct rq_map_data *map_data,
  699. struct sg_iovec *iov, int iov_count,
  700. int write_to_vm, gfp_t gfp_mask)
  701. {
  702. struct bio_map_data *bmd;
  703. struct bio_vec *bvec;
  704. struct page *page;
  705. struct bio *bio;
  706. int i, ret;
  707. int nr_pages = 0;
  708. unsigned int len = 0;
  709. unsigned int offset = map_data ? map_data->offset & ~PAGE_MASK : 0;
  710. for (i = 0; i < iov_count; i++) {
  711. unsigned long uaddr;
  712. unsigned long end;
  713. unsigned long start;
  714. uaddr = (unsigned long)iov[i].iov_base;
  715. end = (uaddr + iov[i].iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  716. start = uaddr >> PAGE_SHIFT;
  717. /*
  718. * Overflow, abort
  719. */
  720. if (end < start)
  721. return ERR_PTR(-EINVAL);
  722. nr_pages += end - start;
  723. len += iov[i].iov_len;
  724. }
  725. if (offset)
  726. nr_pages++;
  727. bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask);
  728. if (!bmd)
  729. return ERR_PTR(-ENOMEM);
  730. ret = -ENOMEM;
  731. bio = bio_kmalloc(gfp_mask, nr_pages);
  732. if (!bio)
  733. goto out_bmd;
  734. if (!write_to_vm)
  735. bio->bi_rw |= REQ_WRITE;
  736. ret = 0;
  737. if (map_data) {
  738. nr_pages = 1 << map_data->page_order;
  739. i = map_data->offset / PAGE_SIZE;
  740. }
  741. while (len) {
  742. unsigned int bytes = PAGE_SIZE;
  743. bytes -= offset;
  744. if (bytes > len)
  745. bytes = len;
  746. if (map_data) {
  747. if (i == map_data->nr_entries * nr_pages) {
  748. ret = -ENOMEM;
  749. break;
  750. }
  751. page = map_data->pages[i / nr_pages];
  752. page += (i % nr_pages);
  753. i++;
  754. } else {
  755. page = alloc_page(q->bounce_gfp | gfp_mask);
  756. if (!page) {
  757. ret = -ENOMEM;
  758. break;
  759. }
  760. }
  761. if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
  762. break;
  763. len -= bytes;
  764. offset = 0;
  765. }
  766. if (ret)
  767. goto cleanup;
  768. /*
  769. * success
  770. */
  771. if ((!write_to_vm && (!map_data || !map_data->null_mapped)) ||
  772. (map_data && map_data->from_user)) {
  773. ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0, 1, 0);
  774. if (ret)
  775. goto cleanup;
  776. }
  777. bio_set_map_data(bmd, bio, iov, iov_count, map_data ? 0 : 1);
  778. return bio;
  779. cleanup:
  780. if (!map_data)
  781. bio_for_each_segment(bvec, bio, i)
  782. __free_page(bvec->bv_page);
  783. bio_put(bio);
  784. out_bmd:
  785. bio_free_map_data(bmd);
  786. return ERR_PTR(ret);
  787. }
  788. /**
  789. * bio_copy_user - copy user data to bio
  790. * @q: destination block queue
  791. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  792. * @uaddr: start of user address
  793. * @len: length in bytes
  794. * @write_to_vm: bool indicating writing to pages or not
  795. * @gfp_mask: memory allocation flags
  796. *
  797. * Prepares and returns a bio for indirect user io, bouncing data
  798. * to/from kernel pages as necessary. Must be paired with
  799. * call bio_uncopy_user() on io completion.
  800. */
  801. struct bio *bio_copy_user(struct request_queue *q, struct rq_map_data *map_data,
  802. unsigned long uaddr, unsigned int len,
  803. int write_to_vm, gfp_t gfp_mask)
  804. {
  805. struct sg_iovec iov;
  806. iov.iov_base = (void __user *)uaddr;
  807. iov.iov_len = len;
  808. return bio_copy_user_iov(q, map_data, &iov, 1, write_to_vm, gfp_mask);
  809. }
  810. EXPORT_SYMBOL(bio_copy_user);
  811. static struct bio *__bio_map_user_iov(struct request_queue *q,
  812. struct block_device *bdev,
  813. struct sg_iovec *iov, int iov_count,
  814. int write_to_vm, gfp_t gfp_mask)
  815. {
  816. int i, j;
  817. int nr_pages = 0;
  818. struct page **pages;
  819. struct bio *bio;
  820. int cur_page = 0;
  821. int ret, offset;
  822. for (i = 0; i < iov_count; i++) {
  823. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  824. unsigned long len = iov[i].iov_len;
  825. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  826. unsigned long start = uaddr >> PAGE_SHIFT;
  827. /*
  828. * Overflow, abort
  829. */
  830. if (end < start)
  831. return ERR_PTR(-EINVAL);
  832. nr_pages += end - start;
  833. /*
  834. * buffer must be aligned to at least hardsector size for now
  835. */
  836. if (uaddr & queue_dma_alignment(q))
  837. return ERR_PTR(-EINVAL);
  838. }
  839. if (!nr_pages)
  840. return ERR_PTR(-EINVAL);
  841. bio = bio_kmalloc(gfp_mask, nr_pages);
  842. if (!bio)
  843. return ERR_PTR(-ENOMEM);
  844. ret = -ENOMEM;
  845. pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask);
  846. if (!pages)
  847. goto out;
  848. for (i = 0; i < iov_count; i++) {
  849. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  850. unsigned long len = iov[i].iov_len;
  851. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  852. unsigned long start = uaddr >> PAGE_SHIFT;
  853. const int local_nr_pages = end - start;
  854. const int page_limit = cur_page + local_nr_pages;
  855. ret = get_user_pages_fast(uaddr, local_nr_pages,
  856. write_to_vm, &pages[cur_page]);
  857. if (ret < local_nr_pages) {
  858. ret = -EFAULT;
  859. goto out_unmap;
  860. }
  861. offset = uaddr & ~PAGE_MASK;
  862. for (j = cur_page; j < page_limit; j++) {
  863. unsigned int bytes = PAGE_SIZE - offset;
  864. if (len <= 0)
  865. break;
  866. if (bytes > len)
  867. bytes = len;
  868. /*
  869. * sorry...
  870. */
  871. if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
  872. bytes)
  873. break;
  874. len -= bytes;
  875. offset = 0;
  876. }
  877. cur_page = j;
  878. /*
  879. * release the pages we didn't map into the bio, if any
  880. */
  881. while (j < page_limit)
  882. page_cache_release(pages[j++]);
  883. }
  884. kfree(pages);
  885. /*
  886. * set data direction, and check if mapped pages need bouncing
  887. */
  888. if (!write_to_vm)
  889. bio->bi_rw |= REQ_WRITE;
  890. bio->bi_bdev = bdev;
  891. bio->bi_flags |= (1 << BIO_USER_MAPPED);
  892. return bio;
  893. out_unmap:
  894. for (i = 0; i < nr_pages; i++) {
  895. if(!pages[i])
  896. break;
  897. page_cache_release(pages[i]);
  898. }
  899. out:
  900. kfree(pages);
  901. bio_put(bio);
  902. return ERR_PTR(ret);
  903. }
  904. /**
  905. * bio_map_user - map user address into bio
  906. * @q: the struct request_queue for the bio
  907. * @bdev: destination block device
  908. * @uaddr: start of user address
  909. * @len: length in bytes
  910. * @write_to_vm: bool indicating writing to pages or not
  911. * @gfp_mask: memory allocation flags
  912. *
  913. * Map the user space address into a bio suitable for io to a block
  914. * device. Returns an error pointer in case of error.
  915. */
  916. struct bio *bio_map_user(struct request_queue *q, struct block_device *bdev,
  917. unsigned long uaddr, unsigned int len, int write_to_vm,
  918. gfp_t gfp_mask)
  919. {
  920. struct sg_iovec iov;
  921. iov.iov_base = (void __user *)uaddr;
  922. iov.iov_len = len;
  923. return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm, gfp_mask);
  924. }
  925. EXPORT_SYMBOL(bio_map_user);
  926. /**
  927. * bio_map_user_iov - map user sg_iovec table into bio
  928. * @q: the struct request_queue for the bio
  929. * @bdev: destination block device
  930. * @iov: the iovec.
  931. * @iov_count: number of elements in the iovec
  932. * @write_to_vm: bool indicating writing to pages or not
  933. * @gfp_mask: memory allocation flags
  934. *
  935. * Map the user space address into a bio suitable for io to a block
  936. * device. Returns an error pointer in case of error.
  937. */
  938. struct bio *bio_map_user_iov(struct request_queue *q, struct block_device *bdev,
  939. struct sg_iovec *iov, int iov_count,
  940. int write_to_vm, gfp_t gfp_mask)
  941. {
  942. struct bio *bio;
  943. bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm,
  944. gfp_mask);
  945. if (IS_ERR(bio))
  946. return bio;
  947. /*
  948. * subtle -- if __bio_map_user() ended up bouncing a bio,
  949. * it would normally disappear when its bi_end_io is run.
  950. * however, we need it for the unmap, so grab an extra
  951. * reference to it
  952. */
  953. bio_get(bio);
  954. return bio;
  955. }
  956. static void __bio_unmap_user(struct bio *bio)
  957. {
  958. struct bio_vec *bvec;
  959. int i;
  960. /*
  961. * make sure we dirty pages we wrote to
  962. */
  963. __bio_for_each_segment(bvec, bio, i, 0) {
  964. if (bio_data_dir(bio) == READ)
  965. set_page_dirty_lock(bvec->bv_page);
  966. page_cache_release(bvec->bv_page);
  967. }
  968. bio_put(bio);
  969. }
  970. /**
  971. * bio_unmap_user - unmap a bio
  972. * @bio: the bio being unmapped
  973. *
  974. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  975. * a process context.
  976. *
  977. * bio_unmap_user() may sleep.
  978. */
  979. void bio_unmap_user(struct bio *bio)
  980. {
  981. __bio_unmap_user(bio);
  982. bio_put(bio);
  983. }
  984. EXPORT_SYMBOL(bio_unmap_user);
  985. static void bio_map_kern_endio(struct bio *bio, int err)
  986. {
  987. bio_put(bio);
  988. }
  989. static struct bio *__bio_map_kern(struct request_queue *q, void *data,
  990. unsigned int len, gfp_t gfp_mask)
  991. {
  992. unsigned long kaddr = (unsigned long)data;
  993. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  994. unsigned long start = kaddr >> PAGE_SHIFT;
  995. const int nr_pages = end - start;
  996. int offset, i;
  997. struct bio *bio;
  998. bio = bio_kmalloc(gfp_mask, nr_pages);
  999. if (!bio)
  1000. return ERR_PTR(-ENOMEM);
  1001. offset = offset_in_page(kaddr);
  1002. for (i = 0; i < nr_pages; i++) {
  1003. unsigned int bytes = PAGE_SIZE - offset;
  1004. if (len <= 0)
  1005. break;
  1006. if (bytes > len)
  1007. bytes = len;
  1008. if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
  1009. offset) < bytes)
  1010. break;
  1011. data += bytes;
  1012. len -= bytes;
  1013. offset = 0;
  1014. }
  1015. bio->bi_end_io = bio_map_kern_endio;
  1016. return bio;
  1017. }
  1018. /**
  1019. * bio_map_kern - map kernel address into bio
  1020. * @q: the struct request_queue for the bio
  1021. * @data: pointer to buffer to map
  1022. * @len: length in bytes
  1023. * @gfp_mask: allocation flags for bio allocation
  1024. *
  1025. * Map the kernel address into a bio suitable for io to a block
  1026. * device. Returns an error pointer in case of error.
  1027. */
  1028. struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
  1029. gfp_t gfp_mask)
  1030. {
  1031. struct bio *bio;
  1032. bio = __bio_map_kern(q, data, len, gfp_mask);
  1033. if (IS_ERR(bio))
  1034. return bio;
  1035. if (bio->bi_size == len)
  1036. return bio;
  1037. /*
  1038. * Don't support partial mappings.
  1039. */
  1040. bio_put(bio);
  1041. return ERR_PTR(-EINVAL);
  1042. }
  1043. EXPORT_SYMBOL(bio_map_kern);
  1044. static void bio_copy_kern_endio(struct bio *bio, int err)
  1045. {
  1046. struct bio_vec *bvec;
  1047. const int read = bio_data_dir(bio) == READ;
  1048. struct bio_map_data *bmd = bio->bi_private;
  1049. int i;
  1050. char *p = bmd->sgvecs[0].iov_base;
  1051. __bio_for_each_segment(bvec, bio, i, 0) {
  1052. char *addr = page_address(bvec->bv_page);
  1053. int len = bmd->iovecs[i].bv_len;
  1054. if (read)
  1055. memcpy(p, addr, len);
  1056. __free_page(bvec->bv_page);
  1057. p += len;
  1058. }
  1059. bio_free_map_data(bmd);
  1060. bio_put(bio);
  1061. }
  1062. /**
  1063. * bio_copy_kern - copy kernel address into bio
  1064. * @q: the struct request_queue for the bio
  1065. * @data: pointer to buffer to copy
  1066. * @len: length in bytes
  1067. * @gfp_mask: allocation flags for bio and page allocation
  1068. * @reading: data direction is READ
  1069. *
  1070. * copy the kernel address into a bio suitable for io to a block
  1071. * device. Returns an error pointer in case of error.
  1072. */
  1073. struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
  1074. gfp_t gfp_mask, int reading)
  1075. {
  1076. struct bio *bio;
  1077. struct bio_vec *bvec;
  1078. int i;
  1079. bio = bio_copy_user(q, NULL, (unsigned long)data, len, 1, gfp_mask);
  1080. if (IS_ERR(bio))
  1081. return bio;
  1082. if (!reading) {
  1083. void *p = data;
  1084. bio_for_each_segment(bvec, bio, i) {
  1085. char *addr = page_address(bvec->bv_page);
  1086. memcpy(addr, p, bvec->bv_len);
  1087. p += bvec->bv_len;
  1088. }
  1089. }
  1090. bio->bi_end_io = bio_copy_kern_endio;
  1091. return bio;
  1092. }
  1093. EXPORT_SYMBOL(bio_copy_kern);
  1094. /*
  1095. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  1096. * for performing direct-IO in BIOs.
  1097. *
  1098. * The problem is that we cannot run set_page_dirty() from interrupt context
  1099. * because the required locks are not interrupt-safe. So what we can do is to
  1100. * mark the pages dirty _before_ performing IO. And in interrupt context,
  1101. * check that the pages are still dirty. If so, fine. If not, redirty them
  1102. * in process context.
  1103. *
  1104. * We special-case compound pages here: normally this means reads into hugetlb
  1105. * pages. The logic in here doesn't really work right for compound pages
  1106. * because the VM does not uniformly chase down the head page in all cases.
  1107. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  1108. * handle them at all. So we skip compound pages here at an early stage.
  1109. *
  1110. * Note that this code is very hard to test under normal circumstances because
  1111. * direct-io pins the pages with get_user_pages(). This makes
  1112. * is_page_cache_freeable return false, and the VM will not clean the pages.
  1113. * But other code (eg, pdflush) could clean the pages if they are mapped
  1114. * pagecache.
  1115. *
  1116. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  1117. * deferred bio dirtying paths.
  1118. */
  1119. /*
  1120. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  1121. */
  1122. void bio_set_pages_dirty(struct bio *bio)
  1123. {
  1124. struct bio_vec *bvec = bio->bi_io_vec;
  1125. int i;
  1126. for (i = 0; i < bio->bi_vcnt; i++) {
  1127. struct page *page = bvec[i].bv_page;
  1128. if (page && !PageCompound(page))
  1129. set_page_dirty_lock(page);
  1130. }
  1131. }
  1132. static void bio_release_pages(struct bio *bio)
  1133. {
  1134. struct bio_vec *bvec = bio->bi_io_vec;
  1135. int i;
  1136. for (i = 0; i < bio->bi_vcnt; i++) {
  1137. struct page *page = bvec[i].bv_page;
  1138. if (page)
  1139. put_page(page);
  1140. }
  1141. }
  1142. /*
  1143. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  1144. * If they are, then fine. If, however, some pages are clean then they must
  1145. * have been written out during the direct-IO read. So we take another ref on
  1146. * the BIO and the offending pages and re-dirty the pages in process context.
  1147. *
  1148. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  1149. * here on. It will run one page_cache_release() against each page and will
  1150. * run one bio_put() against the BIO.
  1151. */
  1152. static void bio_dirty_fn(struct work_struct *work);
  1153. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
  1154. static DEFINE_SPINLOCK(bio_dirty_lock);
  1155. static struct bio *bio_dirty_list;
  1156. /*
  1157. * This runs in process context
  1158. */
  1159. static void bio_dirty_fn(struct work_struct *work)
  1160. {
  1161. unsigned long flags;
  1162. struct bio *bio;
  1163. spin_lock_irqsave(&bio_dirty_lock, flags);
  1164. bio = bio_dirty_list;
  1165. bio_dirty_list = NULL;
  1166. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1167. while (bio) {
  1168. struct bio *next = bio->bi_private;
  1169. bio_set_pages_dirty(bio);
  1170. bio_release_pages(bio);
  1171. bio_put(bio);
  1172. bio = next;
  1173. }
  1174. }
  1175. void bio_check_pages_dirty(struct bio *bio)
  1176. {
  1177. struct bio_vec *bvec = bio->bi_io_vec;
  1178. int nr_clean_pages = 0;
  1179. int i;
  1180. for (i = 0; i < bio->bi_vcnt; i++) {
  1181. struct page *page = bvec[i].bv_page;
  1182. if (PageDirty(page) || PageCompound(page)) {
  1183. page_cache_release(page);
  1184. bvec[i].bv_page = NULL;
  1185. } else {
  1186. nr_clean_pages++;
  1187. }
  1188. }
  1189. if (nr_clean_pages) {
  1190. unsigned long flags;
  1191. spin_lock_irqsave(&bio_dirty_lock, flags);
  1192. bio->bi_private = bio_dirty_list;
  1193. bio_dirty_list = bio;
  1194. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1195. schedule_work(&bio_dirty_work);
  1196. } else {
  1197. bio_put(bio);
  1198. }
  1199. }
  1200. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  1201. void bio_flush_dcache_pages(struct bio *bi)
  1202. {
  1203. int i;
  1204. struct bio_vec *bvec;
  1205. bio_for_each_segment(bvec, bi, i)
  1206. flush_dcache_page(bvec->bv_page);
  1207. }
  1208. EXPORT_SYMBOL(bio_flush_dcache_pages);
  1209. #endif
  1210. /**
  1211. * bio_endio - end I/O on a bio
  1212. * @bio: bio
  1213. * @error: error, if any
  1214. *
  1215. * Description:
  1216. * bio_endio() will end I/O on the whole bio. bio_endio() is the
  1217. * preferred way to end I/O on a bio, it takes care of clearing
  1218. * BIO_UPTODATE on error. @error is 0 on success, and and one of the
  1219. * established -Exxxx (-EIO, for instance) error values in case
  1220. * something went wrong. No one should call bi_end_io() directly on a
  1221. * bio unless they own it and thus know that it has an end_io
  1222. * function.
  1223. **/
  1224. void bio_endio(struct bio *bio, int error)
  1225. {
  1226. if (error)
  1227. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  1228. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1229. error = -EIO;
  1230. if (bio->bi_end_io)
  1231. bio->bi_end_io(bio, error);
  1232. }
  1233. EXPORT_SYMBOL(bio_endio);
  1234. void bio_pair_release(struct bio_pair *bp)
  1235. {
  1236. if (atomic_dec_and_test(&bp->cnt)) {
  1237. struct bio *master = bp->bio1.bi_private;
  1238. bio_endio(master, bp->error);
  1239. mempool_free(bp, bp->bio2.bi_private);
  1240. }
  1241. }
  1242. EXPORT_SYMBOL(bio_pair_release);
  1243. static void bio_pair_end_1(struct bio *bi, int err)
  1244. {
  1245. struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
  1246. if (err)
  1247. bp->error = err;
  1248. bio_pair_release(bp);
  1249. }
  1250. static void bio_pair_end_2(struct bio *bi, int err)
  1251. {
  1252. struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
  1253. if (err)
  1254. bp->error = err;
  1255. bio_pair_release(bp);
  1256. }
  1257. /*
  1258. * split a bio - only worry about a bio with a single page in its iovec
  1259. */
  1260. struct bio_pair *bio_split(struct bio *bi, int first_sectors)
  1261. {
  1262. struct bio_pair *bp = mempool_alloc(bio_split_pool, GFP_NOIO);
  1263. if (!bp)
  1264. return bp;
  1265. trace_block_split(bdev_get_queue(bi->bi_bdev), bi,
  1266. bi->bi_sector + first_sectors);
  1267. BUG_ON(bi->bi_vcnt != 1);
  1268. BUG_ON(bi->bi_idx != 0);
  1269. atomic_set(&bp->cnt, 3);
  1270. bp->error = 0;
  1271. bp->bio1 = *bi;
  1272. bp->bio2 = *bi;
  1273. bp->bio2.bi_sector += first_sectors;
  1274. bp->bio2.bi_size -= first_sectors << 9;
  1275. bp->bio1.bi_size = first_sectors << 9;
  1276. bp->bv1 = bi->bi_io_vec[0];
  1277. bp->bv2 = bi->bi_io_vec[0];
  1278. bp->bv2.bv_offset += first_sectors << 9;
  1279. bp->bv2.bv_len -= first_sectors << 9;
  1280. bp->bv1.bv_len = first_sectors << 9;
  1281. bp->bio1.bi_io_vec = &bp->bv1;
  1282. bp->bio2.bi_io_vec = &bp->bv2;
  1283. bp->bio1.bi_max_vecs = 1;
  1284. bp->bio2.bi_max_vecs = 1;
  1285. bp->bio1.bi_end_io = bio_pair_end_1;
  1286. bp->bio2.bi_end_io = bio_pair_end_2;
  1287. bp->bio1.bi_private = bi;
  1288. bp->bio2.bi_private = bio_split_pool;
  1289. if (bio_integrity(bi))
  1290. bio_integrity_split(bi, bp, first_sectors);
  1291. return bp;
  1292. }
  1293. EXPORT_SYMBOL(bio_split);
  1294. /**
  1295. * bio_sector_offset - Find hardware sector offset in bio
  1296. * @bio: bio to inspect
  1297. * @index: bio_vec index
  1298. * @offset: offset in bv_page
  1299. *
  1300. * Return the number of hardware sectors between beginning of bio
  1301. * and an end point indicated by a bio_vec index and an offset
  1302. * within that vector's page.
  1303. */
  1304. sector_t bio_sector_offset(struct bio *bio, unsigned short index,
  1305. unsigned int offset)
  1306. {
  1307. unsigned int sector_sz;
  1308. struct bio_vec *bv;
  1309. sector_t sectors;
  1310. int i;
  1311. sector_sz = queue_logical_block_size(bio->bi_bdev->bd_disk->queue);
  1312. sectors = 0;
  1313. if (index >= bio->bi_idx)
  1314. index = bio->bi_vcnt - 1;
  1315. __bio_for_each_segment(bv, bio, i, 0) {
  1316. if (i == index) {
  1317. if (offset > bv->bv_offset)
  1318. sectors += (offset - bv->bv_offset) / sector_sz;
  1319. break;
  1320. }
  1321. sectors += bv->bv_len / sector_sz;
  1322. }
  1323. return sectors;
  1324. }
  1325. EXPORT_SYMBOL(bio_sector_offset);
  1326. /*
  1327. * create memory pools for biovec's in a bio_set.
  1328. * use the global biovec slabs created for general use.
  1329. */
  1330. static int biovec_create_pools(struct bio_set *bs, int pool_entries)
  1331. {
  1332. struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
  1333. bs->bvec_pool = mempool_create_slab_pool(pool_entries, bp->slab);
  1334. if (!bs->bvec_pool)
  1335. return -ENOMEM;
  1336. return 0;
  1337. }
  1338. static void biovec_free_pools(struct bio_set *bs)
  1339. {
  1340. mempool_destroy(bs->bvec_pool);
  1341. }
  1342. void bioset_free(struct bio_set *bs)
  1343. {
  1344. if (bs->bio_pool)
  1345. mempool_destroy(bs->bio_pool);
  1346. bioset_integrity_free(bs);
  1347. biovec_free_pools(bs);
  1348. bio_put_slab(bs);
  1349. kfree(bs);
  1350. }
  1351. EXPORT_SYMBOL(bioset_free);
  1352. /**
  1353. * bioset_create - Create a bio_set
  1354. * @pool_size: Number of bio and bio_vecs to cache in the mempool
  1355. * @front_pad: Number of bytes to allocate in front of the returned bio
  1356. *
  1357. * Description:
  1358. * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
  1359. * to ask for a number of bytes to be allocated in front of the bio.
  1360. * Front pad allocation is useful for embedding the bio inside
  1361. * another structure, to avoid allocating extra data to go with the bio.
  1362. * Note that the bio must be embedded at the END of that structure always,
  1363. * or things will break badly.
  1364. */
  1365. struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
  1366. {
  1367. unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
  1368. struct bio_set *bs;
  1369. bs = kzalloc(sizeof(*bs), GFP_KERNEL);
  1370. if (!bs)
  1371. return NULL;
  1372. bs->front_pad = front_pad;
  1373. bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
  1374. if (!bs->bio_slab) {
  1375. kfree(bs);
  1376. return NULL;
  1377. }
  1378. bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
  1379. if (!bs->bio_pool)
  1380. goto bad;
  1381. if (!biovec_create_pools(bs, pool_size))
  1382. return bs;
  1383. bad:
  1384. bioset_free(bs);
  1385. return NULL;
  1386. }
  1387. EXPORT_SYMBOL(bioset_create);
  1388. static void __init biovec_init_slabs(void)
  1389. {
  1390. int i;
  1391. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1392. int size;
  1393. struct biovec_slab *bvs = bvec_slabs + i;
  1394. if (bvs->nr_vecs <= BIO_INLINE_VECS) {
  1395. bvs->slab = NULL;
  1396. continue;
  1397. }
  1398. size = bvs->nr_vecs * sizeof(struct bio_vec);
  1399. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  1400. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1401. }
  1402. }
  1403. static int __init init_bio(void)
  1404. {
  1405. bio_slab_max = 2;
  1406. bio_slab_nr = 0;
  1407. bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
  1408. if (!bio_slabs)
  1409. panic("bio: can't allocate bios\n");
  1410. bio_integrity_init();
  1411. biovec_init_slabs();
  1412. fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
  1413. if (!fs_bio_set)
  1414. panic("bio: can't allocate bios\n");
  1415. if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE))
  1416. panic("bio: can't create integrity pool\n");
  1417. bio_split_pool = mempool_create_kmalloc_pool(BIO_SPLIT_ENTRIES,
  1418. sizeof(struct bio_pair));
  1419. if (!bio_split_pool)
  1420. panic("bio: can't create split pool\n");
  1421. return 0;
  1422. }
  1423. subsys_initcall(init_bio);