bio.c 34 KB

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