bio.c 39 KB

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