bio.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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 <linux/blktrace_api.h>
  29. #include <scsi/sg.h> /* for struct sg_iovec */
  30. static struct kmem_cache *bio_slab __read_mostly;
  31. mempool_t *bio_split_pool __read_mostly;
  32. /*
  33. * if you change this list, also change bvec_alloc or things will
  34. * break badly! cannot be bigger than what you can fit into an
  35. * unsigned short
  36. */
  37. #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
  38. static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
  39. BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
  40. };
  41. #undef BV
  42. /*
  43. * fs_bio_set is the bio_set containing bio and iovec memory pools used by
  44. * IO code that does not need private memory pools.
  45. */
  46. struct bio_set *fs_bio_set;
  47. unsigned int bvec_nr_vecs(unsigned short idx)
  48. {
  49. return bvec_slabs[idx].nr_vecs;
  50. }
  51. struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx, struct bio_set *bs)
  52. {
  53. struct bio_vec *bvl;
  54. /*
  55. * see comment near bvec_array define!
  56. */
  57. switch (nr) {
  58. case 1 : *idx = 0; break;
  59. case 2 ... 4: *idx = 1; break;
  60. case 5 ... 16: *idx = 2; break;
  61. case 17 ... 64: *idx = 3; break;
  62. case 65 ... 128: *idx = 4; break;
  63. case 129 ... BIO_MAX_PAGES: *idx = 5; break;
  64. default:
  65. return NULL;
  66. }
  67. /*
  68. * idx now points to the pool we want to allocate from
  69. */
  70. bvl = mempool_alloc(bs->bvec_pools[*idx], gfp_mask);
  71. if (bvl)
  72. memset(bvl, 0, bvec_nr_vecs(*idx) * sizeof(struct bio_vec));
  73. return bvl;
  74. }
  75. void bio_free(struct bio *bio, struct bio_set *bio_set)
  76. {
  77. if (bio->bi_io_vec) {
  78. const int pool_idx = BIO_POOL_IDX(bio);
  79. BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS);
  80. mempool_free(bio->bi_io_vec, bio_set->bvec_pools[pool_idx]);
  81. }
  82. if (bio_integrity(bio))
  83. bio_integrity_free(bio, bio_set);
  84. mempool_free(bio, bio_set->bio_pool);
  85. }
  86. /*
  87. * default destructor for a bio allocated with bio_alloc_bioset()
  88. */
  89. static void bio_fs_destructor(struct bio *bio)
  90. {
  91. bio_free(bio, fs_bio_set);
  92. }
  93. void bio_init(struct bio *bio)
  94. {
  95. memset(bio, 0, sizeof(*bio));
  96. bio->bi_flags = 1 << BIO_UPTODATE;
  97. atomic_set(&bio->bi_cnt, 1);
  98. }
  99. /**
  100. * bio_alloc_bioset - allocate a bio for I/O
  101. * @gfp_mask: the GFP_ mask given to the slab allocator
  102. * @nr_iovecs: number of iovecs to pre-allocate
  103. * @bs: the bio_set to allocate from
  104. *
  105. * Description:
  106. * bio_alloc_bioset will first try it's on mempool to satisfy the allocation.
  107. * If %__GFP_WAIT is set then we will block on the internal pool waiting
  108. * for a &struct bio to become free.
  109. *
  110. * allocate bio and iovecs from the memory pools specified by the
  111. * bio_set structure.
  112. **/
  113. struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
  114. {
  115. struct bio *bio = mempool_alloc(bs->bio_pool, gfp_mask);
  116. if (likely(bio)) {
  117. struct bio_vec *bvl = NULL;
  118. bio_init(bio);
  119. if (likely(nr_iovecs)) {
  120. unsigned long uninitialized_var(idx);
  121. bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
  122. if (unlikely(!bvl)) {
  123. mempool_free(bio, bs->bio_pool);
  124. bio = NULL;
  125. goto out;
  126. }
  127. bio->bi_flags |= idx << BIO_POOL_OFFSET;
  128. bio->bi_max_vecs = bvec_nr_vecs(idx);
  129. }
  130. bio->bi_io_vec = bvl;
  131. }
  132. out:
  133. return bio;
  134. }
  135. struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs)
  136. {
  137. struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
  138. if (bio)
  139. bio->bi_destructor = bio_fs_destructor;
  140. return bio;
  141. }
  142. void zero_fill_bio(struct bio *bio)
  143. {
  144. unsigned long flags;
  145. struct bio_vec *bv;
  146. int i;
  147. bio_for_each_segment(bv, bio, i) {
  148. char *data = bvec_kmap_irq(bv, &flags);
  149. memset(data, 0, bv->bv_len);
  150. flush_dcache_page(bv->bv_page);
  151. bvec_kunmap_irq(data, &flags);
  152. }
  153. }
  154. EXPORT_SYMBOL(zero_fill_bio);
  155. /**
  156. * bio_put - release a reference to a bio
  157. * @bio: bio to release reference to
  158. *
  159. * Description:
  160. * Put a reference to a &struct bio, either one you have gotten with
  161. * bio_alloc or bio_get. The last put of a bio will free it.
  162. **/
  163. void bio_put(struct bio *bio)
  164. {
  165. BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
  166. /*
  167. * last put frees it
  168. */
  169. if (atomic_dec_and_test(&bio->bi_cnt)) {
  170. bio->bi_next = NULL;
  171. bio->bi_destructor(bio);
  172. }
  173. }
  174. inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
  175. {
  176. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  177. blk_recount_segments(q, bio);
  178. return bio->bi_phys_segments;
  179. }
  180. inline int bio_hw_segments(struct request_queue *q, struct bio *bio)
  181. {
  182. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  183. blk_recount_segments(q, bio);
  184. return bio->bi_hw_segments;
  185. }
  186. /**
  187. * __bio_clone - clone a bio
  188. * @bio: destination bio
  189. * @bio_src: bio to clone
  190. *
  191. * Clone a &bio. Caller will own the returned bio, but not
  192. * the actual data it points to. Reference count of returned
  193. * bio will be one.
  194. */
  195. void __bio_clone(struct bio *bio, struct bio *bio_src)
  196. {
  197. memcpy(bio->bi_io_vec, bio_src->bi_io_vec,
  198. bio_src->bi_max_vecs * sizeof(struct bio_vec));
  199. /*
  200. * most users will be overriding ->bi_bdev with a new target,
  201. * so we don't set nor calculate new physical/hw segment counts here
  202. */
  203. bio->bi_sector = bio_src->bi_sector;
  204. bio->bi_bdev = bio_src->bi_bdev;
  205. bio->bi_flags |= 1 << BIO_CLONED;
  206. bio->bi_rw = bio_src->bi_rw;
  207. bio->bi_vcnt = bio_src->bi_vcnt;
  208. bio->bi_size = bio_src->bi_size;
  209. bio->bi_idx = bio_src->bi_idx;
  210. }
  211. /**
  212. * bio_clone - clone a bio
  213. * @bio: bio to clone
  214. * @gfp_mask: allocation priority
  215. *
  216. * Like __bio_clone, only also allocates the returned bio
  217. */
  218. struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
  219. {
  220. struct bio *b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, fs_bio_set);
  221. if (!b)
  222. return NULL;
  223. b->bi_destructor = bio_fs_destructor;
  224. __bio_clone(b, bio);
  225. if (bio_integrity(bio)) {
  226. int ret;
  227. ret = bio_integrity_clone(b, bio, fs_bio_set);
  228. if (ret < 0)
  229. return NULL;
  230. }
  231. return b;
  232. }
  233. /**
  234. * bio_get_nr_vecs - return approx number of vecs
  235. * @bdev: I/O target
  236. *
  237. * Return the approximate number of pages we can send to this target.
  238. * There's no guarantee that you will be able to fit this number of pages
  239. * into a bio, it does not account for dynamic restrictions that vary
  240. * on offset.
  241. */
  242. int bio_get_nr_vecs(struct block_device *bdev)
  243. {
  244. struct request_queue *q = bdev_get_queue(bdev);
  245. int nr_pages;
  246. nr_pages = ((q->max_sectors << 9) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  247. if (nr_pages > q->max_phys_segments)
  248. nr_pages = q->max_phys_segments;
  249. if (nr_pages > q->max_hw_segments)
  250. nr_pages = q->max_hw_segments;
  251. return nr_pages;
  252. }
  253. static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
  254. *page, unsigned int len, unsigned int offset,
  255. unsigned short max_sectors)
  256. {
  257. int retried_segments = 0;
  258. struct bio_vec *bvec;
  259. /*
  260. * cloned bio must not modify vec list
  261. */
  262. if (unlikely(bio_flagged(bio, BIO_CLONED)))
  263. return 0;
  264. if (((bio->bi_size + len) >> 9) > max_sectors)
  265. return 0;
  266. /*
  267. * For filesystems with a blocksize smaller than the pagesize
  268. * we will often be called with the same page as last time and
  269. * a consecutive offset. Optimize this special case.
  270. */
  271. if (bio->bi_vcnt > 0) {
  272. struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  273. if (page == prev->bv_page &&
  274. offset == prev->bv_offset + prev->bv_len) {
  275. prev->bv_len += len;
  276. if (q->merge_bvec_fn) {
  277. struct bvec_merge_data bvm = {
  278. .bi_bdev = bio->bi_bdev,
  279. .bi_sector = bio->bi_sector,
  280. .bi_size = bio->bi_size,
  281. .bi_rw = bio->bi_rw,
  282. };
  283. if (q->merge_bvec_fn(q, &bvm, prev) < len) {
  284. prev->bv_len -= len;
  285. return 0;
  286. }
  287. }
  288. goto done;
  289. }
  290. }
  291. if (bio->bi_vcnt >= bio->bi_max_vecs)
  292. return 0;
  293. /*
  294. * we might lose a segment or two here, but rather that than
  295. * make this too complex.
  296. */
  297. while (bio->bi_phys_segments >= q->max_phys_segments
  298. || bio->bi_hw_segments >= q->max_hw_segments
  299. || BIOVEC_VIRT_OVERSIZE(bio->bi_size)) {
  300. if (retried_segments)
  301. return 0;
  302. retried_segments = 1;
  303. blk_recount_segments(q, bio);
  304. }
  305. /*
  306. * setup the new entry, we might clear it again later if we
  307. * cannot add the page
  308. */
  309. bvec = &bio->bi_io_vec[bio->bi_vcnt];
  310. bvec->bv_page = page;
  311. bvec->bv_len = len;
  312. bvec->bv_offset = offset;
  313. /*
  314. * if queue has other restrictions (eg varying max sector size
  315. * depending on offset), it can specify a merge_bvec_fn in the
  316. * queue to get further control
  317. */
  318. if (q->merge_bvec_fn) {
  319. struct bvec_merge_data bvm = {
  320. .bi_bdev = bio->bi_bdev,
  321. .bi_sector = bio->bi_sector,
  322. .bi_size = bio->bi_size,
  323. .bi_rw = bio->bi_rw,
  324. };
  325. /*
  326. * merge_bvec_fn() returns number of bytes it can accept
  327. * at this offset
  328. */
  329. if (q->merge_bvec_fn(q, &bvm, bvec) < len) {
  330. bvec->bv_page = NULL;
  331. bvec->bv_len = 0;
  332. bvec->bv_offset = 0;
  333. return 0;
  334. }
  335. }
  336. /* If we may be able to merge these biovecs, force a recount */
  337. if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec) ||
  338. BIOVEC_VIRT_MERGEABLE(bvec-1, bvec)))
  339. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  340. bio->bi_vcnt++;
  341. bio->bi_phys_segments++;
  342. bio->bi_hw_segments++;
  343. done:
  344. bio->bi_size += len;
  345. return len;
  346. }
  347. /**
  348. * bio_add_pc_page - attempt to add page to bio
  349. * @q: the target queue
  350. * @bio: destination bio
  351. * @page: page to add
  352. * @len: vec entry length
  353. * @offset: vec entry offset
  354. *
  355. * Attempt to add a page to the bio_vec maplist. This can fail for a
  356. * number of reasons, such as the bio being full or target block
  357. * device limitations. The target block device must allow bio's
  358. * smaller than PAGE_SIZE, so it is always possible to add a single
  359. * page to an empty bio. This should only be used by REQ_PC bios.
  360. */
  361. int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
  362. unsigned int len, unsigned int offset)
  363. {
  364. return __bio_add_page(q, bio, page, len, offset, q->max_hw_sectors);
  365. }
  366. /**
  367. * bio_add_page - attempt to add page to bio
  368. * @bio: destination bio
  369. * @page: page to add
  370. * @len: vec entry length
  371. * @offset: vec entry offset
  372. *
  373. * Attempt to add a page to the bio_vec maplist. This can fail for a
  374. * number of reasons, such as the bio being full or target block
  375. * device limitations. The target block device must allow bio's
  376. * smaller than PAGE_SIZE, so it is always possible to add a single
  377. * page to an empty bio.
  378. */
  379. int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
  380. unsigned int offset)
  381. {
  382. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  383. return __bio_add_page(q, bio, page, len, offset, q->max_sectors);
  384. }
  385. struct bio_map_data {
  386. struct bio_vec *iovecs;
  387. int nr_sgvecs;
  388. struct sg_iovec *sgvecs;
  389. };
  390. static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio,
  391. struct sg_iovec *iov, int iov_count)
  392. {
  393. memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
  394. memcpy(bmd->sgvecs, iov, sizeof(struct sg_iovec) * iov_count);
  395. bmd->nr_sgvecs = iov_count;
  396. bio->bi_private = bmd;
  397. }
  398. static void bio_free_map_data(struct bio_map_data *bmd)
  399. {
  400. kfree(bmd->iovecs);
  401. kfree(bmd->sgvecs);
  402. kfree(bmd);
  403. }
  404. static struct bio_map_data *bio_alloc_map_data(int nr_segs, int iov_count,
  405. gfp_t gfp_mask)
  406. {
  407. struct bio_map_data *bmd = kmalloc(sizeof(*bmd), gfp_mask);
  408. if (!bmd)
  409. return NULL;
  410. bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, gfp_mask);
  411. if (!bmd->iovecs) {
  412. kfree(bmd);
  413. return NULL;
  414. }
  415. bmd->sgvecs = kmalloc(sizeof(struct sg_iovec) * iov_count, gfp_mask);
  416. if (bmd->sgvecs)
  417. return bmd;
  418. kfree(bmd->iovecs);
  419. kfree(bmd);
  420. return NULL;
  421. }
  422. static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
  423. struct sg_iovec *iov, int iov_count, int uncopy)
  424. {
  425. int ret = 0, i;
  426. struct bio_vec *bvec;
  427. int iov_idx = 0;
  428. unsigned int iov_off = 0;
  429. int read = bio_data_dir(bio) == READ;
  430. __bio_for_each_segment(bvec, bio, i, 0) {
  431. char *bv_addr = page_address(bvec->bv_page);
  432. unsigned int bv_len = iovecs[i].bv_len;
  433. while (bv_len && iov_idx < iov_count) {
  434. unsigned int bytes;
  435. char *iov_addr;
  436. bytes = min_t(unsigned int,
  437. iov[iov_idx].iov_len - iov_off, bv_len);
  438. iov_addr = iov[iov_idx].iov_base + iov_off;
  439. if (!ret) {
  440. if (!read && !uncopy)
  441. ret = copy_from_user(bv_addr, iov_addr,
  442. bytes);
  443. if (read && uncopy)
  444. ret = copy_to_user(iov_addr, bv_addr,
  445. bytes);
  446. if (ret)
  447. ret = -EFAULT;
  448. }
  449. bv_len -= bytes;
  450. bv_addr += bytes;
  451. iov_addr += bytes;
  452. iov_off += bytes;
  453. if (iov[iov_idx].iov_len == iov_off) {
  454. iov_idx++;
  455. iov_off = 0;
  456. }
  457. }
  458. if (uncopy)
  459. __free_page(bvec->bv_page);
  460. }
  461. return ret;
  462. }
  463. /**
  464. * bio_uncopy_user - finish previously mapped bio
  465. * @bio: bio being terminated
  466. *
  467. * Free pages allocated from bio_copy_user() and write back data
  468. * to user space in case of a read.
  469. */
  470. int bio_uncopy_user(struct bio *bio)
  471. {
  472. struct bio_map_data *bmd = bio->bi_private;
  473. int ret;
  474. ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs, bmd->nr_sgvecs, 1);
  475. bio_free_map_data(bmd);
  476. bio_put(bio);
  477. return ret;
  478. }
  479. /**
  480. * bio_copy_user_iov - copy user data to bio
  481. * @q: destination block queue
  482. * @iov: the iovec.
  483. * @iov_count: number of elements in the iovec
  484. * @write_to_vm: bool indicating writing to pages or not
  485. *
  486. * Prepares and returns a bio for indirect user io, bouncing data
  487. * to/from kernel pages as necessary. Must be paired with
  488. * call bio_uncopy_user() on io completion.
  489. */
  490. struct bio *bio_copy_user_iov(struct request_queue *q, struct sg_iovec *iov,
  491. int iov_count, int write_to_vm)
  492. {
  493. struct bio_map_data *bmd;
  494. struct bio_vec *bvec;
  495. struct page *page;
  496. struct bio *bio;
  497. int i, ret;
  498. int nr_pages = 0;
  499. unsigned int len = 0;
  500. for (i = 0; i < iov_count; i++) {
  501. unsigned long uaddr;
  502. unsigned long end;
  503. unsigned long start;
  504. uaddr = (unsigned long)iov[i].iov_base;
  505. end = (uaddr + iov[i].iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  506. start = uaddr >> PAGE_SHIFT;
  507. nr_pages += end - start;
  508. len += iov[i].iov_len;
  509. }
  510. bmd = bio_alloc_map_data(nr_pages, iov_count, GFP_KERNEL);
  511. if (!bmd)
  512. return ERR_PTR(-ENOMEM);
  513. ret = -ENOMEM;
  514. bio = bio_alloc(GFP_KERNEL, nr_pages);
  515. if (!bio)
  516. goto out_bmd;
  517. bio->bi_rw |= (!write_to_vm << BIO_RW);
  518. ret = 0;
  519. while (len) {
  520. unsigned int bytes = PAGE_SIZE;
  521. if (bytes > len)
  522. bytes = len;
  523. page = alloc_page(q->bounce_gfp | GFP_KERNEL);
  524. if (!page) {
  525. ret = -ENOMEM;
  526. break;
  527. }
  528. if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
  529. break;
  530. len -= bytes;
  531. }
  532. if (ret)
  533. goto cleanup;
  534. /*
  535. * success
  536. */
  537. if (!write_to_vm) {
  538. ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0);
  539. if (ret)
  540. goto cleanup;
  541. }
  542. bio_set_map_data(bmd, bio, iov, iov_count);
  543. return bio;
  544. cleanup:
  545. bio_for_each_segment(bvec, bio, i)
  546. __free_page(bvec->bv_page);
  547. bio_put(bio);
  548. out_bmd:
  549. bio_free_map_data(bmd);
  550. return ERR_PTR(ret);
  551. }
  552. /**
  553. * bio_copy_user - copy user data to bio
  554. * @q: destination block queue
  555. * @uaddr: start of user address
  556. * @len: length in bytes
  557. * @write_to_vm: bool indicating writing to pages or not
  558. *
  559. * Prepares and returns a bio for indirect user io, bouncing data
  560. * to/from kernel pages as necessary. Must be paired with
  561. * call bio_uncopy_user() on io completion.
  562. */
  563. struct bio *bio_copy_user(struct request_queue *q, unsigned long uaddr,
  564. unsigned int len, int write_to_vm)
  565. {
  566. struct sg_iovec iov;
  567. iov.iov_base = (void __user *)uaddr;
  568. iov.iov_len = len;
  569. return bio_copy_user_iov(q, &iov, 1, write_to_vm);
  570. }
  571. static struct bio *__bio_map_user_iov(struct request_queue *q,
  572. struct block_device *bdev,
  573. struct sg_iovec *iov, int iov_count,
  574. int write_to_vm)
  575. {
  576. int i, j;
  577. int nr_pages = 0;
  578. struct page **pages;
  579. struct bio *bio;
  580. int cur_page = 0;
  581. int ret, offset;
  582. for (i = 0; i < iov_count; i++) {
  583. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  584. unsigned long len = iov[i].iov_len;
  585. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  586. unsigned long start = uaddr >> PAGE_SHIFT;
  587. nr_pages += end - start;
  588. /*
  589. * buffer must be aligned to at least hardsector size for now
  590. */
  591. if (uaddr & queue_dma_alignment(q))
  592. return ERR_PTR(-EINVAL);
  593. }
  594. if (!nr_pages)
  595. return ERR_PTR(-EINVAL);
  596. bio = bio_alloc(GFP_KERNEL, nr_pages);
  597. if (!bio)
  598. return ERR_PTR(-ENOMEM);
  599. ret = -ENOMEM;
  600. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  601. if (!pages)
  602. goto out;
  603. for (i = 0; i < iov_count; i++) {
  604. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  605. unsigned long len = iov[i].iov_len;
  606. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  607. unsigned long start = uaddr >> PAGE_SHIFT;
  608. const int local_nr_pages = end - start;
  609. const int page_limit = cur_page + local_nr_pages;
  610. ret = get_user_pages_fast(uaddr, local_nr_pages,
  611. write_to_vm, &pages[cur_page]);
  612. if (ret < local_nr_pages) {
  613. ret = -EFAULT;
  614. goto out_unmap;
  615. }
  616. offset = uaddr & ~PAGE_MASK;
  617. for (j = cur_page; j < page_limit; j++) {
  618. unsigned int bytes = PAGE_SIZE - offset;
  619. if (len <= 0)
  620. break;
  621. if (bytes > len)
  622. bytes = len;
  623. /*
  624. * sorry...
  625. */
  626. if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
  627. bytes)
  628. break;
  629. len -= bytes;
  630. offset = 0;
  631. }
  632. cur_page = j;
  633. /*
  634. * release the pages we didn't map into the bio, if any
  635. */
  636. while (j < page_limit)
  637. page_cache_release(pages[j++]);
  638. }
  639. kfree(pages);
  640. /*
  641. * set data direction, and check if mapped pages need bouncing
  642. */
  643. if (!write_to_vm)
  644. bio->bi_rw |= (1 << BIO_RW);
  645. bio->bi_bdev = bdev;
  646. bio->bi_flags |= (1 << BIO_USER_MAPPED);
  647. return bio;
  648. out_unmap:
  649. for (i = 0; i < nr_pages; i++) {
  650. if(!pages[i])
  651. break;
  652. page_cache_release(pages[i]);
  653. }
  654. out:
  655. kfree(pages);
  656. bio_put(bio);
  657. return ERR_PTR(ret);
  658. }
  659. /**
  660. * bio_map_user - map user address into bio
  661. * @q: the struct request_queue for the bio
  662. * @bdev: destination block device
  663. * @uaddr: start of user address
  664. * @len: length in bytes
  665. * @write_to_vm: bool indicating writing to pages or not
  666. *
  667. * Map the user space address into a bio suitable for io to a block
  668. * device. Returns an error pointer in case of error.
  669. */
  670. struct bio *bio_map_user(struct request_queue *q, struct block_device *bdev,
  671. unsigned long uaddr, unsigned int len, int write_to_vm)
  672. {
  673. struct sg_iovec iov;
  674. iov.iov_base = (void __user *)uaddr;
  675. iov.iov_len = len;
  676. return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm);
  677. }
  678. /**
  679. * bio_map_user_iov - map user sg_iovec table into bio
  680. * @q: the struct request_queue for the bio
  681. * @bdev: destination block device
  682. * @iov: the iovec.
  683. * @iov_count: number of elements in the iovec
  684. * @write_to_vm: bool indicating writing to pages or not
  685. *
  686. * Map the user space address into a bio suitable for io to a block
  687. * device. Returns an error pointer in case of error.
  688. */
  689. struct bio *bio_map_user_iov(struct request_queue *q, struct block_device *bdev,
  690. struct sg_iovec *iov, int iov_count,
  691. int write_to_vm)
  692. {
  693. struct bio *bio;
  694. bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm);
  695. if (IS_ERR(bio))
  696. return bio;
  697. /*
  698. * subtle -- if __bio_map_user() ended up bouncing a bio,
  699. * it would normally disappear when its bi_end_io is run.
  700. * however, we need it for the unmap, so grab an extra
  701. * reference to it
  702. */
  703. bio_get(bio);
  704. return bio;
  705. }
  706. static void __bio_unmap_user(struct bio *bio)
  707. {
  708. struct bio_vec *bvec;
  709. int i;
  710. /*
  711. * make sure we dirty pages we wrote to
  712. */
  713. __bio_for_each_segment(bvec, bio, i, 0) {
  714. if (bio_data_dir(bio) == READ)
  715. set_page_dirty_lock(bvec->bv_page);
  716. page_cache_release(bvec->bv_page);
  717. }
  718. bio_put(bio);
  719. }
  720. /**
  721. * bio_unmap_user - unmap a bio
  722. * @bio: the bio being unmapped
  723. *
  724. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  725. * a process context.
  726. *
  727. * bio_unmap_user() may sleep.
  728. */
  729. void bio_unmap_user(struct bio *bio)
  730. {
  731. __bio_unmap_user(bio);
  732. bio_put(bio);
  733. }
  734. static void bio_map_kern_endio(struct bio *bio, int err)
  735. {
  736. bio_put(bio);
  737. }
  738. static struct bio *__bio_map_kern(struct request_queue *q, void *data,
  739. unsigned int len, gfp_t gfp_mask)
  740. {
  741. unsigned long kaddr = (unsigned long)data;
  742. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  743. unsigned long start = kaddr >> PAGE_SHIFT;
  744. const int nr_pages = end - start;
  745. int offset, i;
  746. struct bio *bio;
  747. bio = bio_alloc(gfp_mask, nr_pages);
  748. if (!bio)
  749. return ERR_PTR(-ENOMEM);
  750. offset = offset_in_page(kaddr);
  751. for (i = 0; i < nr_pages; i++) {
  752. unsigned int bytes = PAGE_SIZE - offset;
  753. if (len <= 0)
  754. break;
  755. if (bytes > len)
  756. bytes = len;
  757. if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
  758. offset) < bytes)
  759. break;
  760. data += bytes;
  761. len -= bytes;
  762. offset = 0;
  763. }
  764. bio->bi_end_io = bio_map_kern_endio;
  765. return bio;
  766. }
  767. /**
  768. * bio_map_kern - map kernel address into bio
  769. * @q: the struct request_queue for the bio
  770. * @data: pointer to buffer to map
  771. * @len: length in bytes
  772. * @gfp_mask: allocation flags for bio allocation
  773. *
  774. * Map the kernel address into a bio suitable for io to a block
  775. * device. Returns an error pointer in case of error.
  776. */
  777. struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
  778. gfp_t gfp_mask)
  779. {
  780. struct bio *bio;
  781. bio = __bio_map_kern(q, data, len, gfp_mask);
  782. if (IS_ERR(bio))
  783. return bio;
  784. if (bio->bi_size == len)
  785. return bio;
  786. /*
  787. * Don't support partial mappings.
  788. */
  789. bio_put(bio);
  790. return ERR_PTR(-EINVAL);
  791. }
  792. static void bio_copy_kern_endio(struct bio *bio, int err)
  793. {
  794. struct bio_vec *bvec;
  795. const int read = bio_data_dir(bio) == READ;
  796. struct bio_map_data *bmd = bio->bi_private;
  797. int i;
  798. char *p = bmd->sgvecs[0].iov_base;
  799. __bio_for_each_segment(bvec, bio, i, 0) {
  800. char *addr = page_address(bvec->bv_page);
  801. int len = bmd->iovecs[i].bv_len;
  802. if (read && !err)
  803. memcpy(p, addr, len);
  804. __free_page(bvec->bv_page);
  805. p += len;
  806. }
  807. bio_free_map_data(bmd);
  808. bio_put(bio);
  809. }
  810. /**
  811. * bio_copy_kern - copy kernel address into bio
  812. * @q: the struct request_queue for the bio
  813. * @data: pointer to buffer to copy
  814. * @len: length in bytes
  815. * @gfp_mask: allocation flags for bio and page allocation
  816. * @reading: data direction is READ
  817. *
  818. * copy the kernel address into a bio suitable for io to a block
  819. * device. Returns an error pointer in case of error.
  820. */
  821. struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
  822. gfp_t gfp_mask, int reading)
  823. {
  824. unsigned long kaddr = (unsigned long)data;
  825. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  826. unsigned long start = kaddr >> PAGE_SHIFT;
  827. const int nr_pages = end - start;
  828. struct bio *bio;
  829. struct bio_vec *bvec;
  830. struct bio_map_data *bmd;
  831. int i, ret;
  832. struct sg_iovec iov;
  833. iov.iov_base = data;
  834. iov.iov_len = len;
  835. bmd = bio_alloc_map_data(nr_pages, 1, gfp_mask);
  836. if (!bmd)
  837. return ERR_PTR(-ENOMEM);
  838. ret = -ENOMEM;
  839. bio = bio_alloc(gfp_mask, nr_pages);
  840. if (!bio)
  841. goto out_bmd;
  842. while (len) {
  843. struct page *page;
  844. unsigned int bytes = PAGE_SIZE;
  845. if (bytes > len)
  846. bytes = len;
  847. page = alloc_page(q->bounce_gfp | gfp_mask);
  848. if (!page) {
  849. ret = -ENOMEM;
  850. goto cleanup;
  851. }
  852. if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes) {
  853. ret = -EINVAL;
  854. goto cleanup;
  855. }
  856. len -= bytes;
  857. }
  858. if (!reading) {
  859. void *p = data;
  860. bio_for_each_segment(bvec, bio, i) {
  861. char *addr = page_address(bvec->bv_page);
  862. memcpy(addr, p, bvec->bv_len);
  863. p += bvec->bv_len;
  864. }
  865. }
  866. bio->bi_private = bmd;
  867. bio->bi_end_io = bio_copy_kern_endio;
  868. bio_set_map_data(bmd, bio, &iov, 1);
  869. return bio;
  870. cleanup:
  871. bio_for_each_segment(bvec, bio, i)
  872. __free_page(bvec->bv_page);
  873. bio_put(bio);
  874. out_bmd:
  875. bio_free_map_data(bmd);
  876. return ERR_PTR(ret);
  877. }
  878. /*
  879. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  880. * for performing direct-IO in BIOs.
  881. *
  882. * The problem is that we cannot run set_page_dirty() from interrupt context
  883. * because the required locks are not interrupt-safe. So what we can do is to
  884. * mark the pages dirty _before_ performing IO. And in interrupt context,
  885. * check that the pages are still dirty. If so, fine. If not, redirty them
  886. * in process context.
  887. *
  888. * We special-case compound pages here: normally this means reads into hugetlb
  889. * pages. The logic in here doesn't really work right for compound pages
  890. * because the VM does not uniformly chase down the head page in all cases.
  891. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  892. * handle them at all. So we skip compound pages here at an early stage.
  893. *
  894. * Note that this code is very hard to test under normal circumstances because
  895. * direct-io pins the pages with get_user_pages(). This makes
  896. * is_page_cache_freeable return false, and the VM will not clean the pages.
  897. * But other code (eg, pdflush) could clean the pages if they are mapped
  898. * pagecache.
  899. *
  900. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  901. * deferred bio dirtying paths.
  902. */
  903. /*
  904. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  905. */
  906. void bio_set_pages_dirty(struct bio *bio)
  907. {
  908. struct bio_vec *bvec = bio->bi_io_vec;
  909. int i;
  910. for (i = 0; i < bio->bi_vcnt; i++) {
  911. struct page *page = bvec[i].bv_page;
  912. if (page && !PageCompound(page))
  913. set_page_dirty_lock(page);
  914. }
  915. }
  916. static void bio_release_pages(struct bio *bio)
  917. {
  918. struct bio_vec *bvec = bio->bi_io_vec;
  919. int i;
  920. for (i = 0; i < bio->bi_vcnt; i++) {
  921. struct page *page = bvec[i].bv_page;
  922. if (page)
  923. put_page(page);
  924. }
  925. }
  926. /*
  927. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  928. * If they are, then fine. If, however, some pages are clean then they must
  929. * have been written out during the direct-IO read. So we take another ref on
  930. * the BIO and the offending pages and re-dirty the pages in process context.
  931. *
  932. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  933. * here on. It will run one page_cache_release() against each page and will
  934. * run one bio_put() against the BIO.
  935. */
  936. static void bio_dirty_fn(struct work_struct *work);
  937. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
  938. static DEFINE_SPINLOCK(bio_dirty_lock);
  939. static struct bio *bio_dirty_list;
  940. /*
  941. * This runs in process context
  942. */
  943. static void bio_dirty_fn(struct work_struct *work)
  944. {
  945. unsigned long flags;
  946. struct bio *bio;
  947. spin_lock_irqsave(&bio_dirty_lock, flags);
  948. bio = bio_dirty_list;
  949. bio_dirty_list = NULL;
  950. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  951. while (bio) {
  952. struct bio *next = bio->bi_private;
  953. bio_set_pages_dirty(bio);
  954. bio_release_pages(bio);
  955. bio_put(bio);
  956. bio = next;
  957. }
  958. }
  959. void bio_check_pages_dirty(struct bio *bio)
  960. {
  961. struct bio_vec *bvec = bio->bi_io_vec;
  962. int nr_clean_pages = 0;
  963. int i;
  964. for (i = 0; i < bio->bi_vcnt; i++) {
  965. struct page *page = bvec[i].bv_page;
  966. if (PageDirty(page) || PageCompound(page)) {
  967. page_cache_release(page);
  968. bvec[i].bv_page = NULL;
  969. } else {
  970. nr_clean_pages++;
  971. }
  972. }
  973. if (nr_clean_pages) {
  974. unsigned long flags;
  975. spin_lock_irqsave(&bio_dirty_lock, flags);
  976. bio->bi_private = bio_dirty_list;
  977. bio_dirty_list = bio;
  978. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  979. schedule_work(&bio_dirty_work);
  980. } else {
  981. bio_put(bio);
  982. }
  983. }
  984. /**
  985. * bio_endio - end I/O on a bio
  986. * @bio: bio
  987. * @error: error, if any
  988. *
  989. * Description:
  990. * bio_endio() will end I/O on the whole bio. bio_endio() is the
  991. * preferred way to end I/O on a bio, it takes care of clearing
  992. * BIO_UPTODATE on error. @error is 0 on success, and and one of the
  993. * established -Exxxx (-EIO, for instance) error values in case
  994. * something went wrong. Noone should call bi_end_io() directly on a
  995. * bio unless they own it and thus know that it has an end_io
  996. * function.
  997. **/
  998. void bio_endio(struct bio *bio, int error)
  999. {
  1000. if (error)
  1001. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  1002. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1003. error = -EIO;
  1004. if (bio->bi_end_io)
  1005. bio->bi_end_io(bio, error);
  1006. }
  1007. void bio_pair_release(struct bio_pair *bp)
  1008. {
  1009. if (atomic_dec_and_test(&bp->cnt)) {
  1010. struct bio *master = bp->bio1.bi_private;
  1011. bio_endio(master, bp->error);
  1012. mempool_free(bp, bp->bio2.bi_private);
  1013. }
  1014. }
  1015. static void bio_pair_end_1(struct bio *bi, int err)
  1016. {
  1017. struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
  1018. if (err)
  1019. bp->error = err;
  1020. bio_pair_release(bp);
  1021. }
  1022. static void bio_pair_end_2(struct bio *bi, int err)
  1023. {
  1024. struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
  1025. if (err)
  1026. bp->error = err;
  1027. bio_pair_release(bp);
  1028. }
  1029. /*
  1030. * split a bio - only worry about a bio with a single page
  1031. * in it's iovec
  1032. */
  1033. struct bio_pair *bio_split(struct bio *bi, mempool_t *pool, int first_sectors)
  1034. {
  1035. struct bio_pair *bp = mempool_alloc(pool, GFP_NOIO);
  1036. if (!bp)
  1037. return bp;
  1038. blk_add_trace_pdu_int(bdev_get_queue(bi->bi_bdev), BLK_TA_SPLIT, bi,
  1039. bi->bi_sector + first_sectors);
  1040. BUG_ON(bi->bi_vcnt != 1);
  1041. BUG_ON(bi->bi_idx != 0);
  1042. atomic_set(&bp->cnt, 3);
  1043. bp->error = 0;
  1044. bp->bio1 = *bi;
  1045. bp->bio2 = *bi;
  1046. bp->bio2.bi_sector += first_sectors;
  1047. bp->bio2.bi_size -= first_sectors << 9;
  1048. bp->bio1.bi_size = first_sectors << 9;
  1049. bp->bv1 = bi->bi_io_vec[0];
  1050. bp->bv2 = bi->bi_io_vec[0];
  1051. bp->bv2.bv_offset += first_sectors << 9;
  1052. bp->bv2.bv_len -= first_sectors << 9;
  1053. bp->bv1.bv_len = first_sectors << 9;
  1054. bp->bio1.bi_io_vec = &bp->bv1;
  1055. bp->bio2.bi_io_vec = &bp->bv2;
  1056. bp->bio1.bi_max_vecs = 1;
  1057. bp->bio2.bi_max_vecs = 1;
  1058. bp->bio1.bi_end_io = bio_pair_end_1;
  1059. bp->bio2.bi_end_io = bio_pair_end_2;
  1060. bp->bio1.bi_private = bi;
  1061. bp->bio2.bi_private = pool;
  1062. if (bio_integrity(bi))
  1063. bio_integrity_split(bi, bp, first_sectors);
  1064. return bp;
  1065. }
  1066. /*
  1067. * create memory pools for biovec's in a bio_set.
  1068. * use the global biovec slabs created for general use.
  1069. */
  1070. static int biovec_create_pools(struct bio_set *bs, int pool_entries)
  1071. {
  1072. int i;
  1073. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1074. struct biovec_slab *bp = bvec_slabs + i;
  1075. mempool_t **bvp = bs->bvec_pools + i;
  1076. *bvp = mempool_create_slab_pool(pool_entries, bp->slab);
  1077. if (!*bvp)
  1078. return -ENOMEM;
  1079. }
  1080. return 0;
  1081. }
  1082. static void biovec_free_pools(struct bio_set *bs)
  1083. {
  1084. int i;
  1085. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1086. mempool_t *bvp = bs->bvec_pools[i];
  1087. if (bvp)
  1088. mempool_destroy(bvp);
  1089. }
  1090. }
  1091. void bioset_free(struct bio_set *bs)
  1092. {
  1093. if (bs->bio_pool)
  1094. mempool_destroy(bs->bio_pool);
  1095. bioset_integrity_free(bs);
  1096. biovec_free_pools(bs);
  1097. kfree(bs);
  1098. }
  1099. struct bio_set *bioset_create(int bio_pool_size, int bvec_pool_size)
  1100. {
  1101. struct bio_set *bs = kzalloc(sizeof(*bs), GFP_KERNEL);
  1102. if (!bs)
  1103. return NULL;
  1104. bs->bio_pool = mempool_create_slab_pool(bio_pool_size, bio_slab);
  1105. if (!bs->bio_pool)
  1106. goto bad;
  1107. if (bioset_integrity_create(bs, bio_pool_size))
  1108. goto bad;
  1109. if (!biovec_create_pools(bs, bvec_pool_size))
  1110. return bs;
  1111. bad:
  1112. bioset_free(bs);
  1113. return NULL;
  1114. }
  1115. static void __init biovec_init_slabs(void)
  1116. {
  1117. int i;
  1118. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1119. int size;
  1120. struct biovec_slab *bvs = bvec_slabs + i;
  1121. size = bvs->nr_vecs * sizeof(struct bio_vec);
  1122. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  1123. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1124. }
  1125. }
  1126. static int __init init_bio(void)
  1127. {
  1128. bio_slab = KMEM_CACHE(bio, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  1129. bio_integrity_init_slab();
  1130. biovec_init_slabs();
  1131. fs_bio_set = bioset_create(BIO_POOL_SIZE, 2);
  1132. if (!fs_bio_set)
  1133. panic("bio: can't allocate bios\n");
  1134. bio_split_pool = mempool_create_kmalloc_pool(BIO_SPLIT_ENTRIES,
  1135. sizeof(struct bio_pair));
  1136. if (!bio_split_pool)
  1137. panic("bio: can't create split pool\n");
  1138. return 0;
  1139. }
  1140. subsys_initcall(init_bio);
  1141. EXPORT_SYMBOL(bio_alloc);
  1142. EXPORT_SYMBOL(bio_put);
  1143. EXPORT_SYMBOL(bio_free);
  1144. EXPORT_SYMBOL(bio_endio);
  1145. EXPORT_SYMBOL(bio_init);
  1146. EXPORT_SYMBOL(__bio_clone);
  1147. EXPORT_SYMBOL(bio_clone);
  1148. EXPORT_SYMBOL(bio_phys_segments);
  1149. EXPORT_SYMBOL(bio_hw_segments);
  1150. EXPORT_SYMBOL(bio_add_page);
  1151. EXPORT_SYMBOL(bio_add_pc_page);
  1152. EXPORT_SYMBOL(bio_get_nr_vecs);
  1153. EXPORT_SYMBOL(bio_map_user);
  1154. EXPORT_SYMBOL(bio_unmap_user);
  1155. EXPORT_SYMBOL(bio_map_kern);
  1156. EXPORT_SYMBOL(bio_copy_kern);
  1157. EXPORT_SYMBOL(bio_pair_release);
  1158. EXPORT_SYMBOL(bio_split);
  1159. EXPORT_SYMBOL(bio_split_pool);
  1160. EXPORT_SYMBOL(bio_copy_user);
  1161. EXPORT_SYMBOL(bio_uncopy_user);
  1162. EXPORT_SYMBOL(bioset_create);
  1163. EXPORT_SYMBOL(bioset_free);
  1164. EXPORT_SYMBOL(bio_alloc_bioset);