bio.c 35 KB

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