bio.c 34 KB

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