bio.c 40 KB

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