bio.c 44 KB

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