bio.c 25 KB

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