bio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*
  2. * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
  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. #define BIO_POOL_SIZE 256
  29. static kmem_cache_t *bio_slab;
  30. #define BIOVEC_NR_POOLS 6
  31. /*
  32. * a small number of entries is fine, not going to be performance critical.
  33. * basically we just need to survive
  34. */
  35. #define BIO_SPLIT_ENTRIES 8
  36. mempool_t *bio_split_pool;
  37. struct biovec_slab {
  38. int nr_vecs;
  39. char *name;
  40. kmem_cache_t *slab;
  41. };
  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. */
  47. #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
  48. static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] = {
  49. BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
  50. };
  51. #undef BV
  52. /*
  53. * bio_set is used to allow other portions of the IO system to
  54. * allocate their own private memory pools for bio and iovec structures.
  55. * These memory pools in turn all allocate from the bio_slab
  56. * and the bvec_slabs[].
  57. */
  58. struct bio_set {
  59. mempool_t *bio_pool;
  60. mempool_t *bvec_pools[BIOVEC_NR_POOLS];
  61. };
  62. /*
  63. * fs_bio_set is the bio_set containing bio and iovec memory pools used by
  64. * IO code that does not need private memory pools.
  65. */
  66. static struct bio_set *fs_bio_set;
  67. static inline struct bio_vec *bvec_alloc_bs(unsigned int __nocast gfp_mask, int nr, unsigned long *idx, struct bio_set *bs)
  68. {
  69. struct bio_vec *bvl;
  70. struct biovec_slab *bp;
  71. /*
  72. * see comment near bvec_array define!
  73. */
  74. switch (nr) {
  75. case 1 : *idx = 0; break;
  76. case 2 ... 4: *idx = 1; break;
  77. case 5 ... 16: *idx = 2; break;
  78. case 17 ... 64: *idx = 3; break;
  79. case 65 ... 128: *idx = 4; break;
  80. case 129 ... BIO_MAX_PAGES: *idx = 5; break;
  81. default:
  82. return NULL;
  83. }
  84. /*
  85. * idx now points to the pool we want to allocate from
  86. */
  87. bp = bvec_slabs + *idx;
  88. bvl = mempool_alloc(bs->bvec_pools[*idx], gfp_mask);
  89. if (bvl)
  90. memset(bvl, 0, bp->nr_vecs * sizeof(struct bio_vec));
  91. return bvl;
  92. }
  93. /*
  94. * default destructor for a bio allocated with bio_alloc_bioset()
  95. */
  96. static void bio_destructor(struct bio *bio)
  97. {
  98. const int pool_idx = BIO_POOL_IDX(bio);
  99. struct bio_set *bs = bio->bi_set;
  100. BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS);
  101. mempool_free(bio->bi_io_vec, bs->bvec_pools[pool_idx]);
  102. mempool_free(bio, bs->bio_pool);
  103. }
  104. inline void bio_init(struct bio *bio)
  105. {
  106. bio->bi_next = NULL;
  107. bio->bi_flags = 1 << BIO_UPTODATE;
  108. bio->bi_rw = 0;
  109. bio->bi_vcnt = 0;
  110. bio->bi_idx = 0;
  111. bio->bi_phys_segments = 0;
  112. bio->bi_hw_segments = 0;
  113. bio->bi_hw_front_size = 0;
  114. bio->bi_hw_back_size = 0;
  115. bio->bi_size = 0;
  116. bio->bi_max_vecs = 0;
  117. bio->bi_end_io = NULL;
  118. atomic_set(&bio->bi_cnt, 1);
  119. bio->bi_private = NULL;
  120. }
  121. /**
  122. * bio_alloc_bioset - allocate a bio for I/O
  123. * @gfp_mask: the GFP_ mask given to the slab allocator
  124. * @nr_iovecs: number of iovecs to pre-allocate
  125. * @bs: the bio_set to allocate from
  126. *
  127. * Description:
  128. * bio_alloc_bioset will first try it's on mempool to satisfy the allocation.
  129. * If %__GFP_WAIT is set then we will block on the internal pool waiting
  130. * for a &struct bio to become free.
  131. *
  132. * allocate bio and iovecs from the memory pools specified by the
  133. * bio_set structure.
  134. **/
  135. struct bio *bio_alloc_bioset(unsigned int __nocast gfp_mask, int nr_iovecs, struct bio_set *bs)
  136. {
  137. struct bio *bio = mempool_alloc(bs->bio_pool, gfp_mask);
  138. if (likely(bio)) {
  139. struct bio_vec *bvl = NULL;
  140. bio_init(bio);
  141. if (likely(nr_iovecs)) {
  142. unsigned long idx;
  143. bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
  144. if (unlikely(!bvl)) {
  145. mempool_free(bio, bs->bio_pool);
  146. bio = NULL;
  147. goto out;
  148. }
  149. bio->bi_flags |= idx << BIO_POOL_OFFSET;
  150. bio->bi_max_vecs = bvec_slabs[idx].nr_vecs;
  151. }
  152. bio->bi_io_vec = bvl;
  153. bio->bi_destructor = bio_destructor;
  154. bio->bi_set = bs;
  155. }
  156. out:
  157. return bio;
  158. }
  159. struct bio *bio_alloc(unsigned int __nocast gfp_mask, int nr_iovecs)
  160. {
  161. return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
  162. }
  163. void zero_fill_bio(struct bio *bio)
  164. {
  165. unsigned long flags;
  166. struct bio_vec *bv;
  167. int i;
  168. bio_for_each_segment(bv, bio, i) {
  169. char *data = bvec_kmap_irq(bv, &flags);
  170. memset(data, 0, bv->bv_len);
  171. flush_dcache_page(bv->bv_page);
  172. bvec_kunmap_irq(data, &flags);
  173. }
  174. }
  175. EXPORT_SYMBOL(zero_fill_bio);
  176. /**
  177. * bio_put - release a reference to a bio
  178. * @bio: bio to release reference to
  179. *
  180. * Description:
  181. * Put a reference to a &struct bio, either one you have gotten with
  182. * bio_alloc or bio_get. The last put of a bio will free it.
  183. **/
  184. void bio_put(struct bio *bio)
  185. {
  186. BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
  187. /*
  188. * last put frees it
  189. */
  190. if (atomic_dec_and_test(&bio->bi_cnt)) {
  191. bio->bi_next = NULL;
  192. bio->bi_destructor(bio);
  193. }
  194. }
  195. inline int bio_phys_segments(request_queue_t *q, struct bio *bio)
  196. {
  197. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  198. blk_recount_segments(q, bio);
  199. return bio->bi_phys_segments;
  200. }
  201. inline int bio_hw_segments(request_queue_t *q, struct bio *bio)
  202. {
  203. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  204. blk_recount_segments(q, bio);
  205. return bio->bi_hw_segments;
  206. }
  207. /**
  208. * __bio_clone - clone a bio
  209. * @bio: destination bio
  210. * @bio_src: bio to clone
  211. *
  212. * Clone a &bio. Caller will own the returned bio, but not
  213. * the actual data it points to. Reference count of returned
  214. * bio will be one.
  215. */
  216. inline void __bio_clone(struct bio *bio, struct bio *bio_src)
  217. {
  218. request_queue_t *q = bdev_get_queue(bio_src->bi_bdev);
  219. memcpy(bio->bi_io_vec, bio_src->bi_io_vec, bio_src->bi_max_vecs * sizeof(struct bio_vec));
  220. bio->bi_sector = bio_src->bi_sector;
  221. bio->bi_bdev = bio_src->bi_bdev;
  222. bio->bi_flags |= 1 << BIO_CLONED;
  223. bio->bi_rw = bio_src->bi_rw;
  224. /*
  225. * notes -- maybe just leave bi_idx alone. assume identical mapping
  226. * for the clone
  227. */
  228. bio->bi_vcnt = bio_src->bi_vcnt;
  229. bio->bi_size = bio_src->bi_size;
  230. bio_phys_segments(q, bio);
  231. bio_hw_segments(q, bio);
  232. }
  233. /**
  234. * bio_clone - clone a bio
  235. * @bio: bio to clone
  236. * @gfp_mask: allocation priority
  237. *
  238. * Like __bio_clone, only also allocates the returned bio
  239. */
  240. struct bio *bio_clone(struct bio *bio, unsigned int __nocast gfp_mask)
  241. {
  242. struct bio *b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, fs_bio_set);
  243. if (b)
  244. __bio_clone(b, bio);
  245. return b;
  246. }
  247. /**
  248. * bio_get_nr_vecs - return approx number of vecs
  249. * @bdev: I/O target
  250. *
  251. * Return the approximate number of pages we can send to this target.
  252. * There's no guarantee that you will be able to fit this number of pages
  253. * into a bio, it does not account for dynamic restrictions that vary
  254. * on offset.
  255. */
  256. int bio_get_nr_vecs(struct block_device *bdev)
  257. {
  258. request_queue_t *q = bdev_get_queue(bdev);
  259. int nr_pages;
  260. nr_pages = ((q->max_sectors << 9) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  261. if (nr_pages > q->max_phys_segments)
  262. nr_pages = q->max_phys_segments;
  263. if (nr_pages > q->max_hw_segments)
  264. nr_pages = q->max_hw_segments;
  265. return nr_pages;
  266. }
  267. static int __bio_add_page(request_queue_t *q, struct bio *bio, struct page
  268. *page, unsigned int len, unsigned int offset)
  269. {
  270. int retried_segments = 0;
  271. struct bio_vec *bvec;
  272. /*
  273. * cloned bio must not modify vec list
  274. */
  275. if (unlikely(bio_flagged(bio, BIO_CLONED)))
  276. return 0;
  277. if (bio->bi_vcnt >= bio->bi_max_vecs)
  278. return 0;
  279. if (((bio->bi_size + len) >> 9) > q->max_sectors)
  280. return 0;
  281. /*
  282. * we might lose a segment or two here, but rather that than
  283. * make this too complex.
  284. */
  285. while (bio->bi_phys_segments >= q->max_phys_segments
  286. || bio->bi_hw_segments >= q->max_hw_segments
  287. || BIOVEC_VIRT_OVERSIZE(bio->bi_size)) {
  288. if (retried_segments)
  289. return 0;
  290. retried_segments = 1;
  291. blk_recount_segments(q, bio);
  292. }
  293. /*
  294. * setup the new entry, we might clear it again later if we
  295. * cannot add the page
  296. */
  297. bvec = &bio->bi_io_vec[bio->bi_vcnt];
  298. bvec->bv_page = page;
  299. bvec->bv_len = len;
  300. bvec->bv_offset = offset;
  301. /*
  302. * if queue has other restrictions (eg varying max sector size
  303. * depending on offset), it can specify a merge_bvec_fn in the
  304. * queue to get further control
  305. */
  306. if (q->merge_bvec_fn) {
  307. /*
  308. * merge_bvec_fn() returns number of bytes it can accept
  309. * at this offset
  310. */
  311. if (q->merge_bvec_fn(q, bio, bvec) < len) {
  312. bvec->bv_page = NULL;
  313. bvec->bv_len = 0;
  314. bvec->bv_offset = 0;
  315. return 0;
  316. }
  317. }
  318. /* If we may be able to merge these biovecs, force a recount */
  319. if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec) ||
  320. BIOVEC_VIRT_MERGEABLE(bvec-1, bvec)))
  321. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  322. bio->bi_vcnt++;
  323. bio->bi_phys_segments++;
  324. bio->bi_hw_segments++;
  325. bio->bi_size += len;
  326. return len;
  327. }
  328. /**
  329. * bio_add_page - attempt to add page to bio
  330. * @bio: destination bio
  331. * @page: page to add
  332. * @len: vec entry length
  333. * @offset: vec entry offset
  334. *
  335. * Attempt to add a page to the bio_vec maplist. This can fail for a
  336. * number of reasons, such as the bio being full or target block
  337. * device limitations. The target block device must allow bio's
  338. * smaller than PAGE_SIZE, so it is always possible to add a single
  339. * page to an empty bio.
  340. */
  341. int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
  342. unsigned int offset)
  343. {
  344. return __bio_add_page(bdev_get_queue(bio->bi_bdev), bio, page,
  345. len, offset);
  346. }
  347. struct bio_map_data {
  348. struct bio_vec *iovecs;
  349. void __user *userptr;
  350. };
  351. static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio)
  352. {
  353. memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
  354. bio->bi_private = bmd;
  355. }
  356. static void bio_free_map_data(struct bio_map_data *bmd)
  357. {
  358. kfree(bmd->iovecs);
  359. kfree(bmd);
  360. }
  361. static struct bio_map_data *bio_alloc_map_data(int nr_segs)
  362. {
  363. struct bio_map_data *bmd = kmalloc(sizeof(*bmd), GFP_KERNEL);
  364. if (!bmd)
  365. return NULL;
  366. bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, GFP_KERNEL);
  367. if (bmd->iovecs)
  368. return bmd;
  369. kfree(bmd);
  370. return NULL;
  371. }
  372. /**
  373. * bio_uncopy_user - finish previously mapped bio
  374. * @bio: bio being terminated
  375. *
  376. * Free pages allocated from bio_copy_user() and write back data
  377. * to user space in case of a read.
  378. */
  379. int bio_uncopy_user(struct bio *bio)
  380. {
  381. struct bio_map_data *bmd = bio->bi_private;
  382. const int read = bio_data_dir(bio) == READ;
  383. struct bio_vec *bvec;
  384. int i, ret = 0;
  385. __bio_for_each_segment(bvec, bio, i, 0) {
  386. char *addr = page_address(bvec->bv_page);
  387. unsigned int len = bmd->iovecs[i].bv_len;
  388. if (read && !ret && copy_to_user(bmd->userptr, addr, len))
  389. ret = -EFAULT;
  390. __free_page(bvec->bv_page);
  391. bmd->userptr += len;
  392. }
  393. bio_free_map_data(bmd);
  394. bio_put(bio);
  395. return ret;
  396. }
  397. /**
  398. * bio_copy_user - copy user data to bio
  399. * @q: destination block queue
  400. * @uaddr: start of user address
  401. * @len: length in bytes
  402. * @write_to_vm: bool indicating writing to pages or not
  403. *
  404. * Prepares and returns a bio for indirect user io, bouncing data
  405. * to/from kernel pages as necessary. Must be paired with
  406. * call bio_uncopy_user() on io completion.
  407. */
  408. struct bio *bio_copy_user(request_queue_t *q, unsigned long uaddr,
  409. unsigned int len, int write_to_vm)
  410. {
  411. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  412. unsigned long start = uaddr >> PAGE_SHIFT;
  413. struct bio_map_data *bmd;
  414. struct bio_vec *bvec;
  415. struct page *page;
  416. struct bio *bio;
  417. int i, ret;
  418. bmd = bio_alloc_map_data(end - start);
  419. if (!bmd)
  420. return ERR_PTR(-ENOMEM);
  421. bmd->userptr = (void __user *) uaddr;
  422. ret = -ENOMEM;
  423. bio = bio_alloc(GFP_KERNEL, end - start);
  424. if (!bio)
  425. goto out_bmd;
  426. bio->bi_rw |= (!write_to_vm << BIO_RW);
  427. ret = 0;
  428. while (len) {
  429. unsigned int bytes = PAGE_SIZE;
  430. if (bytes > len)
  431. bytes = len;
  432. page = alloc_page(q->bounce_gfp | GFP_KERNEL);
  433. if (!page) {
  434. ret = -ENOMEM;
  435. break;
  436. }
  437. if (__bio_add_page(q, bio, page, bytes, 0) < bytes) {
  438. ret = -EINVAL;
  439. break;
  440. }
  441. len -= bytes;
  442. }
  443. if (ret)
  444. goto cleanup;
  445. /*
  446. * success
  447. */
  448. if (!write_to_vm) {
  449. char __user *p = (char __user *) uaddr;
  450. /*
  451. * for a write, copy in data to kernel pages
  452. */
  453. ret = -EFAULT;
  454. bio_for_each_segment(bvec, bio, i) {
  455. char *addr = page_address(bvec->bv_page);
  456. if (copy_from_user(addr, p, bvec->bv_len))
  457. goto cleanup;
  458. p += bvec->bv_len;
  459. }
  460. }
  461. bio_set_map_data(bmd, bio);
  462. return bio;
  463. cleanup:
  464. bio_for_each_segment(bvec, bio, i)
  465. __free_page(bvec->bv_page);
  466. bio_put(bio);
  467. out_bmd:
  468. bio_free_map_data(bmd);
  469. return ERR_PTR(ret);
  470. }
  471. static struct bio *__bio_map_user(request_queue_t *q, struct block_device *bdev,
  472. unsigned long uaddr, unsigned int len,
  473. int write_to_vm)
  474. {
  475. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  476. unsigned long start = uaddr >> PAGE_SHIFT;
  477. const int nr_pages = end - start;
  478. int ret, offset, i;
  479. struct page **pages;
  480. struct bio *bio;
  481. /*
  482. * transfer and buffer must be aligned to at least hardsector
  483. * size for now, in the future we can relax this restriction
  484. */
  485. if ((uaddr & queue_dma_alignment(q)) || (len & queue_dma_alignment(q)))
  486. return ERR_PTR(-EINVAL);
  487. bio = bio_alloc(GFP_KERNEL, nr_pages);
  488. if (!bio)
  489. return ERR_PTR(-ENOMEM);
  490. ret = -ENOMEM;
  491. pages = kmalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
  492. if (!pages)
  493. goto out;
  494. down_read(&current->mm->mmap_sem);
  495. ret = get_user_pages(current, current->mm, uaddr, nr_pages,
  496. write_to_vm, 0, pages, NULL);
  497. up_read(&current->mm->mmap_sem);
  498. if (ret < nr_pages)
  499. goto out;
  500. bio->bi_bdev = bdev;
  501. offset = uaddr & ~PAGE_MASK;
  502. for (i = 0; i < nr_pages; i++) {
  503. unsigned int bytes = PAGE_SIZE - offset;
  504. if (len <= 0)
  505. break;
  506. if (bytes > len)
  507. bytes = len;
  508. /*
  509. * sorry...
  510. */
  511. if (__bio_add_page(q, bio, pages[i], bytes, offset) < bytes)
  512. break;
  513. len -= bytes;
  514. offset = 0;
  515. }
  516. /*
  517. * release the pages we didn't map into the bio, if any
  518. */
  519. while (i < nr_pages)
  520. page_cache_release(pages[i++]);
  521. kfree(pages);
  522. /*
  523. * set data direction, and check if mapped pages need bouncing
  524. */
  525. if (!write_to_vm)
  526. bio->bi_rw |= (1 << BIO_RW);
  527. bio->bi_flags |= (1 << BIO_USER_MAPPED);
  528. return bio;
  529. out:
  530. kfree(pages);
  531. bio_put(bio);
  532. return ERR_PTR(ret);
  533. }
  534. /**
  535. * bio_map_user - map user address into bio
  536. * @q: the request_queue_t for the bio
  537. * @bdev: destination block device
  538. * @uaddr: start of user address
  539. * @len: length in bytes
  540. * @write_to_vm: bool indicating writing to pages or not
  541. *
  542. * Map the user space address into a bio suitable for io to a block
  543. * device. Returns an error pointer in case of error.
  544. */
  545. struct bio *bio_map_user(request_queue_t *q, struct block_device *bdev,
  546. unsigned long uaddr, unsigned int len, int write_to_vm)
  547. {
  548. struct bio *bio;
  549. bio = __bio_map_user(q, bdev, uaddr, len, write_to_vm);
  550. if (IS_ERR(bio))
  551. return bio;
  552. /*
  553. * subtle -- if __bio_map_user() ended up bouncing a bio,
  554. * it would normally disappear when its bi_end_io is run.
  555. * however, we need it for the unmap, so grab an extra
  556. * reference to it
  557. */
  558. bio_get(bio);
  559. if (bio->bi_size == len)
  560. return bio;
  561. /*
  562. * don't support partial mappings
  563. */
  564. bio_endio(bio, bio->bi_size, 0);
  565. bio_unmap_user(bio);
  566. return ERR_PTR(-EINVAL);
  567. }
  568. static void __bio_unmap_user(struct bio *bio)
  569. {
  570. struct bio_vec *bvec;
  571. int i;
  572. /*
  573. * make sure we dirty pages we wrote to
  574. */
  575. __bio_for_each_segment(bvec, bio, i, 0) {
  576. if (bio_data_dir(bio) == READ)
  577. set_page_dirty_lock(bvec->bv_page);
  578. page_cache_release(bvec->bv_page);
  579. }
  580. bio_put(bio);
  581. }
  582. /**
  583. * bio_unmap_user - unmap a bio
  584. * @bio: the bio being unmapped
  585. *
  586. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  587. * a process context.
  588. *
  589. * bio_unmap_user() may sleep.
  590. */
  591. void bio_unmap_user(struct bio *bio)
  592. {
  593. __bio_unmap_user(bio);
  594. bio_put(bio);
  595. }
  596. /*
  597. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  598. * for performing direct-IO in BIOs.
  599. *
  600. * The problem is that we cannot run set_page_dirty() from interrupt context
  601. * because the required locks are not interrupt-safe. So what we can do is to
  602. * mark the pages dirty _before_ performing IO. And in interrupt context,
  603. * check that the pages are still dirty. If so, fine. If not, redirty them
  604. * in process context.
  605. *
  606. * We special-case compound pages here: normally this means reads into hugetlb
  607. * pages. The logic in here doesn't really work right for compound pages
  608. * because the VM does not uniformly chase down the head page in all cases.
  609. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  610. * handle them at all. So we skip compound pages here at an early stage.
  611. *
  612. * Note that this code is very hard to test under normal circumstances because
  613. * direct-io pins the pages with get_user_pages(). This makes
  614. * is_page_cache_freeable return false, and the VM will not clean the pages.
  615. * But other code (eg, pdflush) could clean the pages if they are mapped
  616. * pagecache.
  617. *
  618. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  619. * deferred bio dirtying paths.
  620. */
  621. /*
  622. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  623. */
  624. void bio_set_pages_dirty(struct bio *bio)
  625. {
  626. struct bio_vec *bvec = bio->bi_io_vec;
  627. int i;
  628. for (i = 0; i < bio->bi_vcnt; i++) {
  629. struct page *page = bvec[i].bv_page;
  630. if (page && !PageCompound(page))
  631. set_page_dirty_lock(page);
  632. }
  633. }
  634. static void bio_release_pages(struct bio *bio)
  635. {
  636. struct bio_vec *bvec = bio->bi_io_vec;
  637. int i;
  638. for (i = 0; i < bio->bi_vcnt; i++) {
  639. struct page *page = bvec[i].bv_page;
  640. if (page)
  641. put_page(page);
  642. }
  643. }
  644. /*
  645. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  646. * If they are, then fine. If, however, some pages are clean then they must
  647. * have been written out during the direct-IO read. So we take another ref on
  648. * the BIO and the offending pages and re-dirty the pages in process context.
  649. *
  650. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  651. * here on. It will run one page_cache_release() against each page and will
  652. * run one bio_put() against the BIO.
  653. */
  654. static void bio_dirty_fn(void *data);
  655. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn, NULL);
  656. static DEFINE_SPINLOCK(bio_dirty_lock);
  657. static struct bio *bio_dirty_list;
  658. /*
  659. * This runs in process context
  660. */
  661. static void bio_dirty_fn(void *data)
  662. {
  663. unsigned long flags;
  664. struct bio *bio;
  665. spin_lock_irqsave(&bio_dirty_lock, flags);
  666. bio = bio_dirty_list;
  667. bio_dirty_list = NULL;
  668. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  669. while (bio) {
  670. struct bio *next = bio->bi_private;
  671. bio_set_pages_dirty(bio);
  672. bio_release_pages(bio);
  673. bio_put(bio);
  674. bio = next;
  675. }
  676. }
  677. void bio_check_pages_dirty(struct bio *bio)
  678. {
  679. struct bio_vec *bvec = bio->bi_io_vec;
  680. int nr_clean_pages = 0;
  681. int i;
  682. for (i = 0; i < bio->bi_vcnt; i++) {
  683. struct page *page = bvec[i].bv_page;
  684. if (PageDirty(page) || PageCompound(page)) {
  685. page_cache_release(page);
  686. bvec[i].bv_page = NULL;
  687. } else {
  688. nr_clean_pages++;
  689. }
  690. }
  691. if (nr_clean_pages) {
  692. unsigned long flags;
  693. spin_lock_irqsave(&bio_dirty_lock, flags);
  694. bio->bi_private = bio_dirty_list;
  695. bio_dirty_list = bio;
  696. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  697. schedule_work(&bio_dirty_work);
  698. } else {
  699. bio_put(bio);
  700. }
  701. }
  702. /**
  703. * bio_endio - end I/O on a bio
  704. * @bio: bio
  705. * @bytes_done: number of bytes completed
  706. * @error: error, if any
  707. *
  708. * Description:
  709. * bio_endio() will end I/O on @bytes_done number of bytes. This may be
  710. * just a partial part of the bio, or it may be the whole bio. bio_endio()
  711. * is the preferred way to end I/O on a bio, it takes care of decrementing
  712. * bi_size and clearing BIO_UPTODATE on error. @error is 0 on success, and
  713. * and one of the established -Exxxx (-EIO, for instance) error values in
  714. * case something went wrong. Noone should call bi_end_io() directly on
  715. * a bio unless they own it and thus know that it has an end_io function.
  716. **/
  717. void bio_endio(struct bio *bio, unsigned int bytes_done, int error)
  718. {
  719. if (error)
  720. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  721. if (unlikely(bytes_done > bio->bi_size)) {
  722. printk("%s: want %u bytes done, only %u left\n", __FUNCTION__,
  723. bytes_done, bio->bi_size);
  724. bytes_done = bio->bi_size;
  725. }
  726. bio->bi_size -= bytes_done;
  727. bio->bi_sector += (bytes_done >> 9);
  728. if (bio->bi_end_io)
  729. bio->bi_end_io(bio, bytes_done, error);
  730. }
  731. void bio_pair_release(struct bio_pair *bp)
  732. {
  733. if (atomic_dec_and_test(&bp->cnt)) {
  734. struct bio *master = bp->bio1.bi_private;
  735. bio_endio(master, master->bi_size, bp->error);
  736. mempool_free(bp, bp->bio2.bi_private);
  737. }
  738. }
  739. static int bio_pair_end_1(struct bio * bi, unsigned int done, int err)
  740. {
  741. struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
  742. if (err)
  743. bp->error = err;
  744. if (bi->bi_size)
  745. return 1;
  746. bio_pair_release(bp);
  747. return 0;
  748. }
  749. static int bio_pair_end_2(struct bio * bi, unsigned int done, int err)
  750. {
  751. struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
  752. if (err)
  753. bp->error = err;
  754. if (bi->bi_size)
  755. return 1;
  756. bio_pair_release(bp);
  757. return 0;
  758. }
  759. /*
  760. * split a bio - only worry about a bio with a single page
  761. * in it's iovec
  762. */
  763. struct bio_pair *bio_split(struct bio *bi, mempool_t *pool, int first_sectors)
  764. {
  765. struct bio_pair *bp = mempool_alloc(pool, GFP_NOIO);
  766. if (!bp)
  767. return bp;
  768. BUG_ON(bi->bi_vcnt != 1);
  769. BUG_ON(bi->bi_idx != 0);
  770. atomic_set(&bp->cnt, 3);
  771. bp->error = 0;
  772. bp->bio1 = *bi;
  773. bp->bio2 = *bi;
  774. bp->bio2.bi_sector += first_sectors;
  775. bp->bio2.bi_size -= first_sectors << 9;
  776. bp->bio1.bi_size = first_sectors << 9;
  777. bp->bv1 = bi->bi_io_vec[0];
  778. bp->bv2 = bi->bi_io_vec[0];
  779. bp->bv2.bv_offset += first_sectors << 9;
  780. bp->bv2.bv_len -= first_sectors << 9;
  781. bp->bv1.bv_len = first_sectors << 9;
  782. bp->bio1.bi_io_vec = &bp->bv1;
  783. bp->bio2.bi_io_vec = &bp->bv2;
  784. bp->bio1.bi_end_io = bio_pair_end_1;
  785. bp->bio2.bi_end_io = bio_pair_end_2;
  786. bp->bio1.bi_private = bi;
  787. bp->bio2.bi_private = pool;
  788. return bp;
  789. }
  790. static void *bio_pair_alloc(unsigned int __nocast gfp_flags, void *data)
  791. {
  792. return kmalloc(sizeof(struct bio_pair), gfp_flags);
  793. }
  794. static void bio_pair_free(void *bp, void *data)
  795. {
  796. kfree(bp);
  797. }
  798. /*
  799. * create memory pools for biovec's in a bio_set.
  800. * use the global biovec slabs created for general use.
  801. */
  802. static int biovec_create_pools(struct bio_set *bs, int pool_entries, int scale)
  803. {
  804. int i;
  805. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  806. struct biovec_slab *bp = bvec_slabs + i;
  807. mempool_t **bvp = bs->bvec_pools + i;
  808. if (i >= scale)
  809. pool_entries >>= 1;
  810. *bvp = mempool_create(pool_entries, mempool_alloc_slab,
  811. mempool_free_slab, bp->slab);
  812. if (!*bvp)
  813. return -ENOMEM;
  814. }
  815. return 0;
  816. }
  817. static void biovec_free_pools(struct bio_set *bs)
  818. {
  819. int i;
  820. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  821. mempool_t *bvp = bs->bvec_pools[i];
  822. if (bvp)
  823. mempool_destroy(bvp);
  824. }
  825. }
  826. void bioset_free(struct bio_set *bs)
  827. {
  828. if (bs->bio_pool)
  829. mempool_destroy(bs->bio_pool);
  830. biovec_free_pools(bs);
  831. kfree(bs);
  832. }
  833. struct bio_set *bioset_create(int bio_pool_size, int bvec_pool_size, int scale)
  834. {
  835. struct bio_set *bs = kmalloc(sizeof(*bs), GFP_KERNEL);
  836. if (!bs)
  837. return NULL;
  838. memset(bs, 0, sizeof(*bs));
  839. bs->bio_pool = mempool_create(bio_pool_size, mempool_alloc_slab,
  840. mempool_free_slab, bio_slab);
  841. if (!bs->bio_pool)
  842. goto bad;
  843. if (!biovec_create_pools(bs, bvec_pool_size, scale))
  844. return bs;
  845. bad:
  846. bioset_free(bs);
  847. return NULL;
  848. }
  849. static void __init biovec_init_slabs(void)
  850. {
  851. int i;
  852. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  853. int size;
  854. struct biovec_slab *bvs = bvec_slabs + i;
  855. size = bvs->nr_vecs * sizeof(struct bio_vec);
  856. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  857. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  858. }
  859. }
  860. static int __init init_bio(void)
  861. {
  862. int megabytes, bvec_pool_entries;
  863. int scale = BIOVEC_NR_POOLS;
  864. bio_slab = kmem_cache_create("bio", sizeof(struct bio), 0,
  865. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  866. biovec_init_slabs();
  867. megabytes = nr_free_pages() >> (20 - PAGE_SHIFT);
  868. /*
  869. * find out where to start scaling
  870. */
  871. if (megabytes <= 16)
  872. scale = 0;
  873. else if (megabytes <= 32)
  874. scale = 1;
  875. else if (megabytes <= 64)
  876. scale = 2;
  877. else if (megabytes <= 96)
  878. scale = 3;
  879. else if (megabytes <= 128)
  880. scale = 4;
  881. /*
  882. * scale number of entries
  883. */
  884. bvec_pool_entries = megabytes * 2;
  885. if (bvec_pool_entries > 256)
  886. bvec_pool_entries = 256;
  887. fs_bio_set = bioset_create(BIO_POOL_SIZE, bvec_pool_entries, scale);
  888. if (!fs_bio_set)
  889. panic("bio: can't allocate bios\n");
  890. bio_split_pool = mempool_create(BIO_SPLIT_ENTRIES,
  891. bio_pair_alloc, bio_pair_free, NULL);
  892. if (!bio_split_pool)
  893. panic("bio: can't create split pool\n");
  894. return 0;
  895. }
  896. subsys_initcall(init_bio);
  897. EXPORT_SYMBOL(bio_alloc);
  898. EXPORT_SYMBOL(bio_put);
  899. EXPORT_SYMBOL(bio_endio);
  900. EXPORT_SYMBOL(bio_init);
  901. EXPORT_SYMBOL(__bio_clone);
  902. EXPORT_SYMBOL(bio_clone);
  903. EXPORT_SYMBOL(bio_phys_segments);
  904. EXPORT_SYMBOL(bio_hw_segments);
  905. EXPORT_SYMBOL(bio_add_page);
  906. EXPORT_SYMBOL(bio_get_nr_vecs);
  907. EXPORT_SYMBOL(bio_map_user);
  908. EXPORT_SYMBOL(bio_unmap_user);
  909. EXPORT_SYMBOL(bio_pair_release);
  910. EXPORT_SYMBOL(bio_split);
  911. EXPORT_SYMBOL(bio_split_pool);
  912. EXPORT_SYMBOL(bio_copy_user);
  913. EXPORT_SYMBOL(bio_uncopy_user);
  914. EXPORT_SYMBOL(bioset_create);
  915. EXPORT_SYMBOL(bioset_free);
  916. EXPORT_SYMBOL(bio_alloc_bioset);