bio.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  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/uio.h>
  23. #include <linux/iocontext.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/export.h>
  28. #include <linux/mempool.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/cgroup.h>
  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. /*
  38. * if you change this list, also change bvec_alloc or things will
  39. * break badly! cannot be bigger than what you can fit into an
  40. * unsigned short
  41. */
  42. #define BV(x, n) { .nr_vecs = x, .name = "biovec-"#n }
  43. static struct biovec_slab bvec_slabs[BVEC_POOL_NR] __read_mostly = {
  44. BV(1, 1), BV(4, 4), BV(16, 16), BV(64, 64), BV(128, 128), BV(BIO_MAX_PAGES, max),
  45. };
  46. #undef BV
  47. /*
  48. * fs_bio_set is the bio_set containing bio and iovec memory pools used by
  49. * IO code that does not need private memory pools.
  50. */
  51. struct bio_set *fs_bio_set;
  52. EXPORT_SYMBOL(fs_bio_set);
  53. /*
  54. * Our slab pool management
  55. */
  56. struct bio_slab {
  57. struct kmem_cache *slab;
  58. unsigned int slab_ref;
  59. unsigned int slab_size;
  60. char name[8];
  61. };
  62. static DEFINE_MUTEX(bio_slab_lock);
  63. static struct bio_slab *bio_slabs;
  64. static unsigned int bio_slab_nr, bio_slab_max;
  65. static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
  66. {
  67. unsigned int sz = sizeof(struct bio) + extra_size;
  68. struct kmem_cache *slab = NULL;
  69. struct bio_slab *bslab, *new_bio_slabs;
  70. unsigned int new_bio_slab_max;
  71. unsigned int i, entry = -1;
  72. mutex_lock(&bio_slab_lock);
  73. i = 0;
  74. while (i < bio_slab_nr) {
  75. bslab = &bio_slabs[i];
  76. if (!bslab->slab && entry == -1)
  77. entry = i;
  78. else if (bslab->slab_size == sz) {
  79. slab = bslab->slab;
  80. bslab->slab_ref++;
  81. break;
  82. }
  83. i++;
  84. }
  85. if (slab)
  86. goto out_unlock;
  87. if (bio_slab_nr == bio_slab_max && entry == -1) {
  88. new_bio_slab_max = bio_slab_max << 1;
  89. new_bio_slabs = krealloc(bio_slabs,
  90. new_bio_slab_max * sizeof(struct bio_slab),
  91. GFP_KERNEL);
  92. if (!new_bio_slabs)
  93. goto out_unlock;
  94. bio_slab_max = new_bio_slab_max;
  95. bio_slabs = new_bio_slabs;
  96. }
  97. if (entry == -1)
  98. entry = bio_slab_nr++;
  99. bslab = &bio_slabs[entry];
  100. snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
  101. slab = kmem_cache_create(bslab->name, sz, ARCH_KMALLOC_MINALIGN,
  102. SLAB_HWCACHE_ALIGN, NULL);
  103. if (!slab)
  104. goto out_unlock;
  105. bslab->slab = slab;
  106. bslab->slab_ref = 1;
  107. bslab->slab_size = sz;
  108. out_unlock:
  109. mutex_unlock(&bio_slab_lock);
  110. return slab;
  111. }
  112. static void bio_put_slab(struct bio_set *bs)
  113. {
  114. struct bio_slab *bslab = NULL;
  115. unsigned int i;
  116. mutex_lock(&bio_slab_lock);
  117. for (i = 0; i < bio_slab_nr; i++) {
  118. if (bs->bio_slab == bio_slabs[i].slab) {
  119. bslab = &bio_slabs[i];
  120. break;
  121. }
  122. }
  123. if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
  124. goto out;
  125. WARN_ON(!bslab->slab_ref);
  126. if (--bslab->slab_ref)
  127. goto out;
  128. kmem_cache_destroy(bslab->slab);
  129. bslab->slab = NULL;
  130. out:
  131. mutex_unlock(&bio_slab_lock);
  132. }
  133. unsigned int bvec_nr_vecs(unsigned short idx)
  134. {
  135. return bvec_slabs[idx].nr_vecs;
  136. }
  137. void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
  138. {
  139. if (!idx)
  140. return;
  141. idx--;
  142. BIO_BUG_ON(idx >= BVEC_POOL_NR);
  143. if (idx == BVEC_POOL_MAX) {
  144. mempool_free(bv, pool);
  145. } else {
  146. struct biovec_slab *bvs = bvec_slabs + idx;
  147. kmem_cache_free(bvs->slab, bv);
  148. }
  149. }
  150. struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
  151. mempool_t *pool)
  152. {
  153. struct bio_vec *bvl;
  154. /*
  155. * see comment near bvec_array define!
  156. */
  157. switch (nr) {
  158. case 1:
  159. *idx = 0;
  160. break;
  161. case 2 ... 4:
  162. *idx = 1;
  163. break;
  164. case 5 ... 16:
  165. *idx = 2;
  166. break;
  167. case 17 ... 64:
  168. *idx = 3;
  169. break;
  170. case 65 ... 128:
  171. *idx = 4;
  172. break;
  173. case 129 ... BIO_MAX_PAGES:
  174. *idx = 5;
  175. break;
  176. default:
  177. return NULL;
  178. }
  179. /*
  180. * idx now points to the pool we want to allocate from. only the
  181. * 1-vec entry pool is mempool backed.
  182. */
  183. if (*idx == BVEC_POOL_MAX) {
  184. fallback:
  185. bvl = mempool_alloc(pool, gfp_mask);
  186. } else {
  187. struct biovec_slab *bvs = bvec_slabs + *idx;
  188. gfp_t __gfp_mask = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO);
  189. /*
  190. * Make this allocation restricted and don't dump info on
  191. * allocation failures, since we'll fallback to the mempool
  192. * in case of failure.
  193. */
  194. __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
  195. /*
  196. * Try a slab allocation. If this fails and __GFP_DIRECT_RECLAIM
  197. * is set, retry with the 1-entry mempool
  198. */
  199. bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
  200. if (unlikely(!bvl && (gfp_mask & __GFP_DIRECT_RECLAIM))) {
  201. *idx = BVEC_POOL_MAX;
  202. goto fallback;
  203. }
  204. }
  205. (*idx)++;
  206. return bvl;
  207. }
  208. static void __bio_free(struct bio *bio)
  209. {
  210. bio_disassociate_task(bio);
  211. if (bio_integrity(bio))
  212. bio_integrity_free(bio);
  213. }
  214. static void bio_free(struct bio *bio)
  215. {
  216. struct bio_set *bs = bio->bi_pool;
  217. void *p;
  218. __bio_free(bio);
  219. if (bs) {
  220. bvec_free(bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio));
  221. /*
  222. * If we have front padding, adjust the bio pointer before freeing
  223. */
  224. p = bio;
  225. p -= bs->front_pad;
  226. mempool_free(p, bs->bio_pool);
  227. } else {
  228. /* Bio was allocated by bio_kmalloc() */
  229. kfree(bio);
  230. }
  231. }
  232. void bio_init(struct bio *bio)
  233. {
  234. memset(bio, 0, sizeof(*bio));
  235. atomic_set(&bio->__bi_remaining, 1);
  236. atomic_set(&bio->__bi_cnt, 1);
  237. }
  238. EXPORT_SYMBOL(bio_init);
  239. /**
  240. * bio_reset - reinitialize a bio
  241. * @bio: bio to reset
  242. *
  243. * Description:
  244. * After calling bio_reset(), @bio will be in the same state as a freshly
  245. * allocated bio returned bio bio_alloc_bioset() - the only fields that are
  246. * preserved are the ones that are initialized by bio_alloc_bioset(). See
  247. * comment in struct bio.
  248. */
  249. void bio_reset(struct bio *bio)
  250. {
  251. unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
  252. __bio_free(bio);
  253. memset(bio, 0, BIO_RESET_BYTES);
  254. bio->bi_flags = flags;
  255. atomic_set(&bio->__bi_remaining, 1);
  256. }
  257. EXPORT_SYMBOL(bio_reset);
  258. static struct bio *__bio_chain_endio(struct bio *bio)
  259. {
  260. struct bio *parent = bio->bi_private;
  261. if (!parent->bi_error)
  262. parent->bi_error = bio->bi_error;
  263. bio_put(bio);
  264. return parent;
  265. }
  266. static void bio_chain_endio(struct bio *bio)
  267. {
  268. bio_endio(__bio_chain_endio(bio));
  269. }
  270. /**
  271. * bio_chain - chain bio completions
  272. * @bio: the target bio
  273. * @parent: the @bio's parent bio
  274. *
  275. * The caller won't have a bi_end_io called when @bio completes - instead,
  276. * @parent's bi_end_io won't be called until both @parent and @bio have
  277. * completed; the chained bio will also be freed when it completes.
  278. *
  279. * The caller must not set bi_private or bi_end_io in @bio.
  280. */
  281. void bio_chain(struct bio *bio, struct bio *parent)
  282. {
  283. BUG_ON(bio->bi_private || bio->bi_end_io);
  284. bio->bi_private = parent;
  285. bio->bi_end_io = bio_chain_endio;
  286. bio_inc_remaining(parent);
  287. }
  288. EXPORT_SYMBOL(bio_chain);
  289. static void bio_alloc_rescue(struct work_struct *work)
  290. {
  291. struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
  292. struct bio *bio;
  293. while (1) {
  294. spin_lock(&bs->rescue_lock);
  295. bio = bio_list_pop(&bs->rescue_list);
  296. spin_unlock(&bs->rescue_lock);
  297. if (!bio)
  298. break;
  299. generic_make_request(bio);
  300. }
  301. }
  302. static void punt_bios_to_rescuer(struct bio_set *bs)
  303. {
  304. struct bio_list punt, nopunt;
  305. struct bio *bio;
  306. /*
  307. * In order to guarantee forward progress we must punt only bios that
  308. * were allocated from this bio_set; otherwise, if there was a bio on
  309. * there for a stacking driver higher up in the stack, processing it
  310. * could require allocating bios from this bio_set, and doing that from
  311. * our own rescuer would be bad.
  312. *
  313. * Since bio lists are singly linked, pop them all instead of trying to
  314. * remove from the middle of the list:
  315. */
  316. bio_list_init(&punt);
  317. bio_list_init(&nopunt);
  318. while ((bio = bio_list_pop(&current->bio_list[0])))
  319. bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
  320. current->bio_list[0] = nopunt;
  321. bio_list_init(&nopunt);
  322. while ((bio = bio_list_pop(&current->bio_list[1])))
  323. bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
  324. current->bio_list[1] = nopunt;
  325. spin_lock(&bs->rescue_lock);
  326. bio_list_merge(&bs->rescue_list, &punt);
  327. spin_unlock(&bs->rescue_lock);
  328. queue_work(bs->rescue_workqueue, &bs->rescue_work);
  329. }
  330. /**
  331. * bio_alloc_bioset - allocate a bio for I/O
  332. * @gfp_mask: the GFP_ mask given to the slab allocator
  333. * @nr_iovecs: number of iovecs to pre-allocate
  334. * @bs: the bio_set to allocate from.
  335. *
  336. * Description:
  337. * If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
  338. * backed by the @bs's mempool.
  339. *
  340. * When @bs is not NULL, if %__GFP_DIRECT_RECLAIM is set then bio_alloc will
  341. * always be able to allocate a bio. This is due to the mempool guarantees.
  342. * To make this work, callers must never allocate more than 1 bio at a time
  343. * from this pool. Callers that need to allocate more than 1 bio must always
  344. * submit the previously allocated bio for IO before attempting to allocate
  345. * a new one. Failure to do so can cause deadlocks under memory pressure.
  346. *
  347. * Note that when running under generic_make_request() (i.e. any block
  348. * driver), bios are not submitted until after you return - see the code in
  349. * generic_make_request() that converts recursion into iteration, to prevent
  350. * stack overflows.
  351. *
  352. * This would normally mean allocating multiple bios under
  353. * generic_make_request() would be susceptible to deadlocks, but we have
  354. * deadlock avoidance code that resubmits any blocked bios from a rescuer
  355. * thread.
  356. *
  357. * However, we do not guarantee forward progress for allocations from other
  358. * mempools. Doing multiple allocations from the same mempool under
  359. * generic_make_request() should be avoided - instead, use bio_set's front_pad
  360. * for per bio allocations.
  361. *
  362. * RETURNS:
  363. * Pointer to new bio on success, NULL on failure.
  364. */
  365. struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
  366. {
  367. gfp_t saved_gfp = gfp_mask;
  368. unsigned front_pad;
  369. unsigned inline_vecs;
  370. struct bio_vec *bvl = NULL;
  371. struct bio *bio;
  372. void *p;
  373. if (!bs) {
  374. if (nr_iovecs > UIO_MAXIOV)
  375. return NULL;
  376. p = kmalloc(sizeof(struct bio) +
  377. nr_iovecs * sizeof(struct bio_vec),
  378. gfp_mask);
  379. front_pad = 0;
  380. inline_vecs = nr_iovecs;
  381. } else {
  382. /* should not use nobvec bioset for nr_iovecs > 0 */
  383. if (WARN_ON_ONCE(!bs->bvec_pool && nr_iovecs > 0))
  384. return NULL;
  385. /*
  386. * generic_make_request() converts recursion to iteration; this
  387. * means if we're running beneath it, any bios we allocate and
  388. * submit will not be submitted (and thus freed) until after we
  389. * return.
  390. *
  391. * This exposes us to a potential deadlock if we allocate
  392. * multiple bios from the same bio_set() while running
  393. * underneath generic_make_request(). If we were to allocate
  394. * multiple bios (say a stacking block driver that was splitting
  395. * bios), we would deadlock if we exhausted the mempool's
  396. * reserve.
  397. *
  398. * We solve this, and guarantee forward progress, with a rescuer
  399. * workqueue per bio_set. If we go to allocate and there are
  400. * bios on current->bio_list, we first try the allocation
  401. * without __GFP_DIRECT_RECLAIM; if that fails, we punt those
  402. * bios we would be blocking to the rescuer workqueue before
  403. * we retry with the original gfp_flags.
  404. */
  405. if (current->bio_list &&
  406. (!bio_list_empty(&current->bio_list[0]) ||
  407. !bio_list_empty(&current->bio_list[1])))
  408. gfp_mask &= ~__GFP_DIRECT_RECLAIM;
  409. p = mempool_alloc(bs->bio_pool, gfp_mask);
  410. if (!p && gfp_mask != saved_gfp) {
  411. punt_bios_to_rescuer(bs);
  412. gfp_mask = saved_gfp;
  413. p = mempool_alloc(bs->bio_pool, gfp_mask);
  414. }
  415. front_pad = bs->front_pad;
  416. inline_vecs = BIO_INLINE_VECS;
  417. }
  418. if (unlikely(!p))
  419. return NULL;
  420. bio = p + front_pad;
  421. bio_init(bio);
  422. if (nr_iovecs > inline_vecs) {
  423. unsigned long idx = 0;
  424. bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
  425. if (!bvl && gfp_mask != saved_gfp) {
  426. punt_bios_to_rescuer(bs);
  427. gfp_mask = saved_gfp;
  428. bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
  429. }
  430. if (unlikely(!bvl))
  431. goto err_free;
  432. bio->bi_flags |= idx << BVEC_POOL_OFFSET;
  433. } else if (nr_iovecs) {
  434. bvl = bio->bi_inline_vecs;
  435. }
  436. bio->bi_pool = bs;
  437. bio->bi_max_vecs = nr_iovecs;
  438. bio->bi_io_vec = bvl;
  439. return bio;
  440. err_free:
  441. mempool_free(p, bs->bio_pool);
  442. return NULL;
  443. }
  444. EXPORT_SYMBOL(bio_alloc_bioset);
  445. void zero_fill_bio(struct bio *bio)
  446. {
  447. unsigned long flags;
  448. struct bio_vec bv;
  449. struct bvec_iter iter;
  450. bio_for_each_segment(bv, bio, iter) {
  451. char *data = bvec_kmap_irq(&bv, &flags);
  452. memset(data, 0, bv.bv_len);
  453. flush_dcache_page(bv.bv_page);
  454. bvec_kunmap_irq(data, &flags);
  455. }
  456. }
  457. EXPORT_SYMBOL(zero_fill_bio);
  458. /**
  459. * bio_put - release a reference to a bio
  460. * @bio: bio to release reference to
  461. *
  462. * Description:
  463. * Put a reference to a &struct bio, either one you have gotten with
  464. * bio_alloc, bio_get or bio_clone. The last put of a bio will free it.
  465. **/
  466. void bio_put(struct bio *bio)
  467. {
  468. if (!bio_flagged(bio, BIO_REFFED))
  469. bio_free(bio);
  470. else {
  471. BIO_BUG_ON(!atomic_read(&bio->__bi_cnt));
  472. /*
  473. * last put frees it
  474. */
  475. if (atomic_dec_and_test(&bio->__bi_cnt))
  476. bio_free(bio);
  477. }
  478. }
  479. EXPORT_SYMBOL(bio_put);
  480. inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
  481. {
  482. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  483. blk_recount_segments(q, bio);
  484. return bio->bi_phys_segments;
  485. }
  486. EXPORT_SYMBOL(bio_phys_segments);
  487. /**
  488. * __bio_clone_fast - clone a bio that shares the original bio's biovec
  489. * @bio: destination bio
  490. * @bio_src: bio to clone
  491. *
  492. * Clone a &bio. Caller will own the returned bio, but not
  493. * the actual data it points to. Reference count of returned
  494. * bio will be one.
  495. *
  496. * Caller must ensure that @bio_src is not freed before @bio.
  497. */
  498. void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
  499. {
  500. BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio));
  501. /*
  502. * most users will be overriding ->bi_bdev with a new target,
  503. * so we don't set nor calculate new physical/hw segment counts here
  504. */
  505. bio->bi_bdev = bio_src->bi_bdev;
  506. bio_set_flag(bio, BIO_CLONED);
  507. bio->bi_opf = bio_src->bi_opf;
  508. bio->bi_iter = bio_src->bi_iter;
  509. bio->bi_io_vec = bio_src->bi_io_vec;
  510. bio_clone_blkcg_association(bio, bio_src);
  511. }
  512. EXPORT_SYMBOL(__bio_clone_fast);
  513. /**
  514. * bio_clone_fast - clone a bio that shares the original bio's biovec
  515. * @bio: bio to clone
  516. * @gfp_mask: allocation priority
  517. * @bs: bio_set to allocate from
  518. *
  519. * Like __bio_clone_fast, only also allocates the returned bio
  520. */
  521. struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
  522. {
  523. struct bio *b;
  524. b = bio_alloc_bioset(gfp_mask, 0, bs);
  525. if (!b)
  526. return NULL;
  527. __bio_clone_fast(b, bio);
  528. if (bio_integrity(bio)) {
  529. int ret;
  530. ret = bio_integrity_clone(b, bio, gfp_mask);
  531. if (ret < 0) {
  532. bio_put(b);
  533. return NULL;
  534. }
  535. }
  536. return b;
  537. }
  538. EXPORT_SYMBOL(bio_clone_fast);
  539. /**
  540. * bio_clone_bioset - clone a bio
  541. * @bio_src: bio to clone
  542. * @gfp_mask: allocation priority
  543. * @bs: bio_set to allocate from
  544. *
  545. * Clone bio. Caller will own the returned bio, but not the actual data it
  546. * points to. Reference count of returned bio will be one.
  547. */
  548. struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask,
  549. struct bio_set *bs)
  550. {
  551. struct bvec_iter iter;
  552. struct bio_vec bv;
  553. struct bio *bio;
  554. /*
  555. * Pre immutable biovecs, __bio_clone() used to just do a memcpy from
  556. * bio_src->bi_io_vec to bio->bi_io_vec.
  557. *
  558. * We can't do that anymore, because:
  559. *
  560. * - The point of cloning the biovec is to produce a bio with a biovec
  561. * the caller can modify: bi_idx and bi_bvec_done should be 0.
  562. *
  563. * - The original bio could've had more than BIO_MAX_PAGES biovecs; if
  564. * we tried to clone the whole thing bio_alloc_bioset() would fail.
  565. * But the clone should succeed as long as the number of biovecs we
  566. * actually need to allocate is fewer than BIO_MAX_PAGES.
  567. *
  568. * - Lastly, bi_vcnt should not be looked at or relied upon by code
  569. * that does not own the bio - reason being drivers don't use it for
  570. * iterating over the biovec anymore, so expecting it to be kept up
  571. * to date (i.e. for clones that share the parent biovec) is just
  572. * asking for trouble and would force extra work on
  573. * __bio_clone_fast() anyways.
  574. */
  575. bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs);
  576. if (!bio)
  577. return NULL;
  578. bio->bi_bdev = bio_src->bi_bdev;
  579. bio->bi_opf = bio_src->bi_opf;
  580. bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
  581. bio->bi_iter.bi_size = bio_src->bi_iter.bi_size;
  582. switch (bio_op(bio)) {
  583. case REQ_OP_DISCARD:
  584. case REQ_OP_SECURE_ERASE:
  585. break;
  586. case REQ_OP_WRITE_SAME:
  587. bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0];
  588. break;
  589. default:
  590. bio_for_each_segment(bv, bio_src, iter)
  591. bio->bi_io_vec[bio->bi_vcnt++] = bv;
  592. break;
  593. }
  594. if (bio_integrity(bio_src)) {
  595. int ret;
  596. ret = bio_integrity_clone(bio, bio_src, gfp_mask);
  597. if (ret < 0) {
  598. bio_put(bio);
  599. return NULL;
  600. }
  601. }
  602. bio_clone_blkcg_association(bio, bio_src);
  603. return bio;
  604. }
  605. EXPORT_SYMBOL(bio_clone_bioset);
  606. /**
  607. * bio_add_pc_page - attempt to add page to bio
  608. * @q: the target queue
  609. * @bio: destination bio
  610. * @page: page to add
  611. * @len: vec entry length
  612. * @offset: vec entry offset
  613. *
  614. * Attempt to add a page to the bio_vec maplist. This can fail for a
  615. * number of reasons, such as the bio being full or target block device
  616. * limitations. The target block device must allow bio's up to PAGE_SIZE,
  617. * so it is always possible to add a single page to an empty bio.
  618. *
  619. * This should only be used by REQ_PC bios.
  620. */
  621. int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page
  622. *page, unsigned int len, unsigned int offset)
  623. {
  624. int retried_segments = 0;
  625. struct bio_vec *bvec;
  626. /*
  627. * cloned bio must not modify vec list
  628. */
  629. if (unlikely(bio_flagged(bio, BIO_CLONED)))
  630. return 0;
  631. if (((bio->bi_iter.bi_size + len) >> 9) > queue_max_hw_sectors(q))
  632. return 0;
  633. /*
  634. * For filesystems with a blocksize smaller than the pagesize
  635. * we will often be called with the same page as last time and
  636. * a consecutive offset. Optimize this special case.
  637. */
  638. if (bio->bi_vcnt > 0) {
  639. struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  640. if (page == prev->bv_page &&
  641. offset == prev->bv_offset + prev->bv_len) {
  642. prev->bv_len += len;
  643. bio->bi_iter.bi_size += len;
  644. goto done;
  645. }
  646. /*
  647. * If the queue doesn't support SG gaps and adding this
  648. * offset would create a gap, disallow it.
  649. */
  650. if (bvec_gap_to_prev(q, prev, offset))
  651. return 0;
  652. }
  653. if (bio->bi_vcnt >= bio->bi_max_vecs)
  654. return 0;
  655. /*
  656. * setup the new entry, we might clear it again later if we
  657. * cannot add the page
  658. */
  659. bvec = &bio->bi_io_vec[bio->bi_vcnt];
  660. bvec->bv_page = page;
  661. bvec->bv_len = len;
  662. bvec->bv_offset = offset;
  663. bio->bi_vcnt++;
  664. bio->bi_phys_segments++;
  665. bio->bi_iter.bi_size += len;
  666. /*
  667. * Perform a recount if the number of segments is greater
  668. * than queue_max_segments(q).
  669. */
  670. while (bio->bi_phys_segments > queue_max_segments(q)) {
  671. if (retried_segments)
  672. goto failed;
  673. retried_segments = 1;
  674. blk_recount_segments(q, bio);
  675. }
  676. /* If we may be able to merge these biovecs, force a recount */
  677. if (bio->bi_vcnt > 1 && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
  678. bio_clear_flag(bio, BIO_SEG_VALID);
  679. done:
  680. return len;
  681. failed:
  682. bvec->bv_page = NULL;
  683. bvec->bv_len = 0;
  684. bvec->bv_offset = 0;
  685. bio->bi_vcnt--;
  686. bio->bi_iter.bi_size -= len;
  687. blk_recount_segments(q, bio);
  688. return 0;
  689. }
  690. EXPORT_SYMBOL(bio_add_pc_page);
  691. /**
  692. * bio_add_page - attempt to add page to bio
  693. * @bio: destination bio
  694. * @page: page to add
  695. * @len: vec entry length
  696. * @offset: vec entry offset
  697. *
  698. * Attempt to add a page to the bio_vec maplist. This will only fail
  699. * if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
  700. */
  701. int bio_add_page(struct bio *bio, struct page *page,
  702. unsigned int len, unsigned int offset)
  703. {
  704. struct bio_vec *bv;
  705. /*
  706. * cloned bio must not modify vec list
  707. */
  708. if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
  709. return 0;
  710. /*
  711. * For filesystems with a blocksize smaller than the pagesize
  712. * we will often be called with the same page as last time and
  713. * a consecutive offset. Optimize this special case.
  714. */
  715. if (bio->bi_vcnt > 0) {
  716. bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
  717. if (page == bv->bv_page &&
  718. offset == bv->bv_offset + bv->bv_len) {
  719. bv->bv_len += len;
  720. goto done;
  721. }
  722. }
  723. if (bio->bi_vcnt >= bio->bi_max_vecs)
  724. return 0;
  725. bv = &bio->bi_io_vec[bio->bi_vcnt];
  726. bv->bv_page = page;
  727. bv->bv_len = len;
  728. bv->bv_offset = offset;
  729. bio->bi_vcnt++;
  730. done:
  731. bio->bi_iter.bi_size += len;
  732. return len;
  733. }
  734. EXPORT_SYMBOL(bio_add_page);
  735. struct submit_bio_ret {
  736. struct completion event;
  737. int error;
  738. };
  739. static void submit_bio_wait_endio(struct bio *bio)
  740. {
  741. struct submit_bio_ret *ret = bio->bi_private;
  742. ret->error = bio->bi_error;
  743. complete(&ret->event);
  744. }
  745. /**
  746. * submit_bio_wait - submit a bio, and wait until it completes
  747. * @bio: The &struct bio which describes the I/O
  748. *
  749. * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
  750. * bio_endio() on failure.
  751. */
  752. int submit_bio_wait(struct bio *bio)
  753. {
  754. struct submit_bio_ret ret;
  755. init_completion(&ret.event);
  756. bio->bi_private = &ret;
  757. bio->bi_end_io = submit_bio_wait_endio;
  758. bio->bi_opf |= REQ_SYNC;
  759. submit_bio(bio);
  760. wait_for_completion_io(&ret.event);
  761. return ret.error;
  762. }
  763. EXPORT_SYMBOL(submit_bio_wait);
  764. /**
  765. * bio_advance - increment/complete a bio by some number of bytes
  766. * @bio: bio to advance
  767. * @bytes: number of bytes to complete
  768. *
  769. * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
  770. * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
  771. * be updated on the last bvec as well.
  772. *
  773. * @bio will then represent the remaining, uncompleted portion of the io.
  774. */
  775. void bio_advance(struct bio *bio, unsigned bytes)
  776. {
  777. if (bio_integrity(bio))
  778. bio_integrity_advance(bio, bytes);
  779. bio_advance_iter(bio, &bio->bi_iter, bytes);
  780. }
  781. EXPORT_SYMBOL(bio_advance);
  782. /**
  783. * bio_alloc_pages - allocates a single page for each bvec in a bio
  784. * @bio: bio to allocate pages for
  785. * @gfp_mask: flags for allocation
  786. *
  787. * Allocates pages up to @bio->bi_vcnt.
  788. *
  789. * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
  790. * freed.
  791. */
  792. int bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
  793. {
  794. int i;
  795. struct bio_vec *bv;
  796. bio_for_each_segment_all(bv, bio, i) {
  797. bv->bv_page = alloc_page(gfp_mask);
  798. if (!bv->bv_page) {
  799. while (--bv >= bio->bi_io_vec)
  800. __free_page(bv->bv_page);
  801. return -ENOMEM;
  802. }
  803. }
  804. return 0;
  805. }
  806. EXPORT_SYMBOL(bio_alloc_pages);
  807. /**
  808. * bio_copy_data - copy contents of data buffers from one chain of bios to
  809. * another
  810. * @src: source bio list
  811. * @dst: destination bio list
  812. *
  813. * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats
  814. * @src and @dst as linked lists of bios.
  815. *
  816. * Stops when it reaches the end of either @src or @dst - that is, copies
  817. * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
  818. */
  819. void bio_copy_data(struct bio *dst, struct bio *src)
  820. {
  821. struct bvec_iter src_iter, dst_iter;
  822. struct bio_vec src_bv, dst_bv;
  823. void *src_p, *dst_p;
  824. unsigned bytes;
  825. src_iter = src->bi_iter;
  826. dst_iter = dst->bi_iter;
  827. while (1) {
  828. if (!src_iter.bi_size) {
  829. src = src->bi_next;
  830. if (!src)
  831. break;
  832. src_iter = src->bi_iter;
  833. }
  834. if (!dst_iter.bi_size) {
  835. dst = dst->bi_next;
  836. if (!dst)
  837. break;
  838. dst_iter = dst->bi_iter;
  839. }
  840. src_bv = bio_iter_iovec(src, src_iter);
  841. dst_bv = bio_iter_iovec(dst, dst_iter);
  842. bytes = min(src_bv.bv_len, dst_bv.bv_len);
  843. src_p = kmap_atomic(src_bv.bv_page);
  844. dst_p = kmap_atomic(dst_bv.bv_page);
  845. memcpy(dst_p + dst_bv.bv_offset,
  846. src_p + src_bv.bv_offset,
  847. bytes);
  848. kunmap_atomic(dst_p);
  849. kunmap_atomic(src_p);
  850. bio_advance_iter(src, &src_iter, bytes);
  851. bio_advance_iter(dst, &dst_iter, bytes);
  852. }
  853. }
  854. EXPORT_SYMBOL(bio_copy_data);
  855. struct bio_map_data {
  856. int is_our_pages;
  857. struct iov_iter iter;
  858. struct iovec iov[];
  859. };
  860. static struct bio_map_data *bio_alloc_map_data(unsigned int iov_count,
  861. gfp_t gfp_mask)
  862. {
  863. if (iov_count > UIO_MAXIOV)
  864. return NULL;
  865. return kmalloc(sizeof(struct bio_map_data) +
  866. sizeof(struct iovec) * iov_count, gfp_mask);
  867. }
  868. /**
  869. * bio_copy_from_iter - copy all pages from iov_iter to bio
  870. * @bio: The &struct bio which describes the I/O as destination
  871. * @iter: iov_iter as source
  872. *
  873. * Copy all pages from iov_iter to bio.
  874. * Returns 0 on success, or error on failure.
  875. */
  876. static int bio_copy_from_iter(struct bio *bio, struct iov_iter iter)
  877. {
  878. int i;
  879. struct bio_vec *bvec;
  880. bio_for_each_segment_all(bvec, bio, i) {
  881. ssize_t ret;
  882. ret = copy_page_from_iter(bvec->bv_page,
  883. bvec->bv_offset,
  884. bvec->bv_len,
  885. &iter);
  886. if (!iov_iter_count(&iter))
  887. break;
  888. if (ret < bvec->bv_len)
  889. return -EFAULT;
  890. }
  891. return 0;
  892. }
  893. /**
  894. * bio_copy_to_iter - copy all pages from bio to iov_iter
  895. * @bio: The &struct bio which describes the I/O as source
  896. * @iter: iov_iter as destination
  897. *
  898. * Copy all pages from bio to iov_iter.
  899. * Returns 0 on success, or error on failure.
  900. */
  901. static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter)
  902. {
  903. int i;
  904. struct bio_vec *bvec;
  905. bio_for_each_segment_all(bvec, bio, i) {
  906. ssize_t ret;
  907. ret = copy_page_to_iter(bvec->bv_page,
  908. bvec->bv_offset,
  909. bvec->bv_len,
  910. &iter);
  911. if (!iov_iter_count(&iter))
  912. break;
  913. if (ret < bvec->bv_len)
  914. return -EFAULT;
  915. }
  916. return 0;
  917. }
  918. void bio_free_pages(struct bio *bio)
  919. {
  920. struct bio_vec *bvec;
  921. int i;
  922. bio_for_each_segment_all(bvec, bio, i)
  923. __free_page(bvec->bv_page);
  924. }
  925. EXPORT_SYMBOL(bio_free_pages);
  926. /**
  927. * bio_uncopy_user - finish previously mapped bio
  928. * @bio: bio being terminated
  929. *
  930. * Free pages allocated from bio_copy_user_iov() and write back data
  931. * to user space in case of a read.
  932. */
  933. int bio_uncopy_user(struct bio *bio)
  934. {
  935. struct bio_map_data *bmd = bio->bi_private;
  936. int ret = 0;
  937. if (!bio_flagged(bio, BIO_NULL_MAPPED)) {
  938. /*
  939. * if we're in a workqueue, the request is orphaned, so
  940. * don't copy into a random user address space, just free
  941. * and return -EINTR so user space doesn't expect any data.
  942. */
  943. if (!current->mm)
  944. ret = -EINTR;
  945. else if (bio_data_dir(bio) == READ)
  946. ret = bio_copy_to_iter(bio, bmd->iter);
  947. if (bmd->is_our_pages)
  948. bio_free_pages(bio);
  949. }
  950. kfree(bmd);
  951. bio_put(bio);
  952. return ret;
  953. }
  954. /**
  955. * bio_copy_user_iov - copy user data to bio
  956. * @q: destination block queue
  957. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  958. * @iter: iovec iterator
  959. * @gfp_mask: memory allocation flags
  960. *
  961. * Prepares and returns a bio for indirect user io, bouncing data
  962. * to/from kernel pages as necessary. Must be paired with
  963. * call bio_uncopy_user() on io completion.
  964. */
  965. struct bio *bio_copy_user_iov(struct request_queue *q,
  966. struct rq_map_data *map_data,
  967. const struct iov_iter *iter,
  968. gfp_t gfp_mask)
  969. {
  970. struct bio_map_data *bmd;
  971. struct page *page;
  972. struct bio *bio;
  973. int i, ret;
  974. int nr_pages = 0;
  975. unsigned int len = iter->count;
  976. unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0;
  977. for (i = 0; i < iter->nr_segs; i++) {
  978. unsigned long uaddr;
  979. unsigned long end;
  980. unsigned long start;
  981. uaddr = (unsigned long) iter->iov[i].iov_base;
  982. end = (uaddr + iter->iov[i].iov_len + PAGE_SIZE - 1)
  983. >> PAGE_SHIFT;
  984. start = uaddr >> PAGE_SHIFT;
  985. /*
  986. * Overflow, abort
  987. */
  988. if (end < start)
  989. return ERR_PTR(-EINVAL);
  990. nr_pages += end - start;
  991. }
  992. if (offset)
  993. nr_pages++;
  994. bmd = bio_alloc_map_data(iter->nr_segs, gfp_mask);
  995. if (!bmd)
  996. return ERR_PTR(-ENOMEM);
  997. /*
  998. * We need to do a deep copy of the iov_iter including the iovecs.
  999. * The caller provided iov might point to an on-stack or otherwise
  1000. * shortlived one.
  1001. */
  1002. bmd->is_our_pages = map_data ? 0 : 1;
  1003. memcpy(bmd->iov, iter->iov, sizeof(struct iovec) * iter->nr_segs);
  1004. bmd->iter = *iter;
  1005. bmd->iter.iov = bmd->iov;
  1006. ret = -ENOMEM;
  1007. bio = bio_kmalloc(gfp_mask, nr_pages);
  1008. if (!bio)
  1009. goto out_bmd;
  1010. if (iter->type & WRITE)
  1011. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  1012. ret = 0;
  1013. if (map_data) {
  1014. nr_pages = 1 << map_data->page_order;
  1015. i = map_data->offset / PAGE_SIZE;
  1016. }
  1017. while (len) {
  1018. unsigned int bytes = PAGE_SIZE;
  1019. bytes -= offset;
  1020. if (bytes > len)
  1021. bytes = len;
  1022. if (map_data) {
  1023. if (i == map_data->nr_entries * nr_pages) {
  1024. ret = -ENOMEM;
  1025. break;
  1026. }
  1027. page = map_data->pages[i / nr_pages];
  1028. page += (i % nr_pages);
  1029. i++;
  1030. } else {
  1031. page = alloc_page(q->bounce_gfp | gfp_mask);
  1032. if (!page) {
  1033. ret = -ENOMEM;
  1034. break;
  1035. }
  1036. }
  1037. if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
  1038. break;
  1039. len -= bytes;
  1040. offset = 0;
  1041. }
  1042. if (ret)
  1043. goto cleanup;
  1044. /*
  1045. * success
  1046. */
  1047. if (((iter->type & WRITE) && (!map_data || !map_data->null_mapped)) ||
  1048. (map_data && map_data->from_user)) {
  1049. ret = bio_copy_from_iter(bio, *iter);
  1050. if (ret)
  1051. goto cleanup;
  1052. }
  1053. bio->bi_private = bmd;
  1054. return bio;
  1055. cleanup:
  1056. if (!map_data)
  1057. bio_free_pages(bio);
  1058. bio_put(bio);
  1059. out_bmd:
  1060. kfree(bmd);
  1061. return ERR_PTR(ret);
  1062. }
  1063. /**
  1064. * bio_map_user_iov - map user iovec into bio
  1065. * @q: the struct request_queue for the bio
  1066. * @iter: iovec iterator
  1067. * @gfp_mask: memory allocation flags
  1068. *
  1069. * Map the user space address into a bio suitable for io to a block
  1070. * device. Returns an error pointer in case of error.
  1071. */
  1072. struct bio *bio_map_user_iov(struct request_queue *q,
  1073. const struct iov_iter *iter,
  1074. gfp_t gfp_mask)
  1075. {
  1076. int j;
  1077. int nr_pages = 0;
  1078. struct page **pages;
  1079. struct bio *bio;
  1080. int cur_page = 0;
  1081. int ret, offset;
  1082. struct iov_iter i;
  1083. struct iovec iov;
  1084. struct bio_vec *bvec;
  1085. iov_for_each(iov, i, *iter) {
  1086. unsigned long uaddr = (unsigned long) iov.iov_base;
  1087. unsigned long len = iov.iov_len;
  1088. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1089. unsigned long start = uaddr >> PAGE_SHIFT;
  1090. /*
  1091. * Overflow, abort
  1092. */
  1093. if (end < start)
  1094. return ERR_PTR(-EINVAL);
  1095. nr_pages += end - start;
  1096. /*
  1097. * buffer must be aligned to at least logical block size for now
  1098. */
  1099. if (uaddr & queue_dma_alignment(q))
  1100. return ERR_PTR(-EINVAL);
  1101. }
  1102. if (!nr_pages)
  1103. return ERR_PTR(-EINVAL);
  1104. bio = bio_kmalloc(gfp_mask, nr_pages);
  1105. if (!bio)
  1106. return ERR_PTR(-ENOMEM);
  1107. ret = -ENOMEM;
  1108. pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask);
  1109. if (!pages)
  1110. goto out;
  1111. iov_for_each(iov, i, *iter) {
  1112. unsigned long uaddr = (unsigned long) iov.iov_base;
  1113. unsigned long len = iov.iov_len;
  1114. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1115. unsigned long start = uaddr >> PAGE_SHIFT;
  1116. const int local_nr_pages = end - start;
  1117. const int page_limit = cur_page + local_nr_pages;
  1118. ret = get_user_pages_fast(uaddr, local_nr_pages,
  1119. (iter->type & WRITE) != WRITE,
  1120. &pages[cur_page]);
  1121. if (unlikely(ret < local_nr_pages)) {
  1122. for (j = cur_page; j < page_limit; j++) {
  1123. if (!pages[j])
  1124. break;
  1125. put_page(pages[j]);
  1126. }
  1127. ret = -EFAULT;
  1128. goto out_unmap;
  1129. }
  1130. offset = offset_in_page(uaddr);
  1131. for (j = cur_page; j < page_limit; j++) {
  1132. unsigned int bytes = PAGE_SIZE - offset;
  1133. unsigned short prev_bi_vcnt = bio->bi_vcnt;
  1134. if (len <= 0)
  1135. break;
  1136. if (bytes > len)
  1137. bytes = len;
  1138. /*
  1139. * sorry...
  1140. */
  1141. if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
  1142. bytes)
  1143. break;
  1144. /*
  1145. * check if vector was merged with previous
  1146. * drop page reference if needed
  1147. */
  1148. if (bio->bi_vcnt == prev_bi_vcnt)
  1149. put_page(pages[j]);
  1150. len -= bytes;
  1151. offset = 0;
  1152. }
  1153. cur_page = j;
  1154. /*
  1155. * release the pages we didn't map into the bio, if any
  1156. */
  1157. while (j < page_limit)
  1158. put_page(pages[j++]);
  1159. }
  1160. kfree(pages);
  1161. /*
  1162. * set data direction, and check if mapped pages need bouncing
  1163. */
  1164. if (iter->type & WRITE)
  1165. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  1166. bio_set_flag(bio, BIO_USER_MAPPED);
  1167. /*
  1168. * subtle -- if __bio_map_user() ended up bouncing a bio,
  1169. * it would normally disappear when its bi_end_io is run.
  1170. * however, we need it for the unmap, so grab an extra
  1171. * reference to it
  1172. */
  1173. bio_get(bio);
  1174. return bio;
  1175. out_unmap:
  1176. bio_for_each_segment_all(bvec, bio, j) {
  1177. put_page(bvec->bv_page);
  1178. }
  1179. out:
  1180. kfree(pages);
  1181. bio_put(bio);
  1182. return ERR_PTR(ret);
  1183. }
  1184. static void __bio_unmap_user(struct bio *bio)
  1185. {
  1186. struct bio_vec *bvec;
  1187. int i;
  1188. /*
  1189. * make sure we dirty pages we wrote to
  1190. */
  1191. bio_for_each_segment_all(bvec, bio, i) {
  1192. if (bio_data_dir(bio) == READ)
  1193. set_page_dirty_lock(bvec->bv_page);
  1194. put_page(bvec->bv_page);
  1195. }
  1196. bio_put(bio);
  1197. }
  1198. /**
  1199. * bio_unmap_user - unmap a bio
  1200. * @bio: the bio being unmapped
  1201. *
  1202. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  1203. * a process context.
  1204. *
  1205. * bio_unmap_user() may sleep.
  1206. */
  1207. void bio_unmap_user(struct bio *bio)
  1208. {
  1209. __bio_unmap_user(bio);
  1210. bio_put(bio);
  1211. }
  1212. static void bio_map_kern_endio(struct bio *bio)
  1213. {
  1214. bio_put(bio);
  1215. }
  1216. /**
  1217. * bio_map_kern - map kernel address into bio
  1218. * @q: the struct request_queue for the bio
  1219. * @data: pointer to buffer to map
  1220. * @len: length in bytes
  1221. * @gfp_mask: allocation flags for bio allocation
  1222. *
  1223. * Map the kernel address into a bio suitable for io to a block
  1224. * device. Returns an error pointer in case of error.
  1225. */
  1226. struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
  1227. gfp_t gfp_mask)
  1228. {
  1229. unsigned long kaddr = (unsigned long)data;
  1230. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1231. unsigned long start = kaddr >> PAGE_SHIFT;
  1232. const int nr_pages = end - start;
  1233. int offset, i;
  1234. struct bio *bio;
  1235. bio = bio_kmalloc(gfp_mask, nr_pages);
  1236. if (!bio)
  1237. return ERR_PTR(-ENOMEM);
  1238. offset = offset_in_page(kaddr);
  1239. for (i = 0; i < nr_pages; i++) {
  1240. unsigned int bytes = PAGE_SIZE - offset;
  1241. if (len <= 0)
  1242. break;
  1243. if (bytes > len)
  1244. bytes = len;
  1245. if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
  1246. offset) < bytes) {
  1247. /* we don't support partial mappings */
  1248. bio_put(bio);
  1249. return ERR_PTR(-EINVAL);
  1250. }
  1251. data += bytes;
  1252. len -= bytes;
  1253. offset = 0;
  1254. }
  1255. bio->bi_end_io = bio_map_kern_endio;
  1256. return bio;
  1257. }
  1258. EXPORT_SYMBOL(bio_map_kern);
  1259. static void bio_copy_kern_endio(struct bio *bio)
  1260. {
  1261. bio_free_pages(bio);
  1262. bio_put(bio);
  1263. }
  1264. static void bio_copy_kern_endio_read(struct bio *bio)
  1265. {
  1266. char *p = bio->bi_private;
  1267. struct bio_vec *bvec;
  1268. int i;
  1269. bio_for_each_segment_all(bvec, bio, i) {
  1270. memcpy(p, page_address(bvec->bv_page), bvec->bv_len);
  1271. p += bvec->bv_len;
  1272. }
  1273. bio_copy_kern_endio(bio);
  1274. }
  1275. /**
  1276. * bio_copy_kern - copy kernel address into bio
  1277. * @q: the struct request_queue for the bio
  1278. * @data: pointer to buffer to copy
  1279. * @len: length in bytes
  1280. * @gfp_mask: allocation flags for bio and page allocation
  1281. * @reading: data direction is READ
  1282. *
  1283. * copy the kernel address into a bio suitable for io to a block
  1284. * device. Returns an error pointer in case of error.
  1285. */
  1286. struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
  1287. gfp_t gfp_mask, int reading)
  1288. {
  1289. unsigned long kaddr = (unsigned long)data;
  1290. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1291. unsigned long start = kaddr >> PAGE_SHIFT;
  1292. struct bio *bio;
  1293. void *p = data;
  1294. int nr_pages = 0;
  1295. /*
  1296. * Overflow, abort
  1297. */
  1298. if (end < start)
  1299. return ERR_PTR(-EINVAL);
  1300. nr_pages = end - start;
  1301. bio = bio_kmalloc(gfp_mask, nr_pages);
  1302. if (!bio)
  1303. return ERR_PTR(-ENOMEM);
  1304. while (len) {
  1305. struct page *page;
  1306. unsigned int bytes = PAGE_SIZE;
  1307. if (bytes > len)
  1308. bytes = len;
  1309. page = alloc_page(q->bounce_gfp | gfp_mask);
  1310. if (!page)
  1311. goto cleanup;
  1312. if (!reading)
  1313. memcpy(page_address(page), p, bytes);
  1314. if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
  1315. break;
  1316. len -= bytes;
  1317. p += bytes;
  1318. }
  1319. if (reading) {
  1320. bio->bi_end_io = bio_copy_kern_endio_read;
  1321. bio->bi_private = data;
  1322. } else {
  1323. bio->bi_end_io = bio_copy_kern_endio;
  1324. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  1325. }
  1326. return bio;
  1327. cleanup:
  1328. bio_free_pages(bio);
  1329. bio_put(bio);
  1330. return ERR_PTR(-ENOMEM);
  1331. }
  1332. /*
  1333. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  1334. * for performing direct-IO in BIOs.
  1335. *
  1336. * The problem is that we cannot run set_page_dirty() from interrupt context
  1337. * because the required locks are not interrupt-safe. So what we can do is to
  1338. * mark the pages dirty _before_ performing IO. And in interrupt context,
  1339. * check that the pages are still dirty. If so, fine. If not, redirty them
  1340. * in process context.
  1341. *
  1342. * We special-case compound pages here: normally this means reads into hugetlb
  1343. * pages. The logic in here doesn't really work right for compound pages
  1344. * because the VM does not uniformly chase down the head page in all cases.
  1345. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  1346. * handle them at all. So we skip compound pages here at an early stage.
  1347. *
  1348. * Note that this code is very hard to test under normal circumstances because
  1349. * direct-io pins the pages with get_user_pages(). This makes
  1350. * is_page_cache_freeable return false, and the VM will not clean the pages.
  1351. * But other code (eg, flusher threads) could clean the pages if they are mapped
  1352. * pagecache.
  1353. *
  1354. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  1355. * deferred bio dirtying paths.
  1356. */
  1357. /*
  1358. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  1359. */
  1360. void bio_set_pages_dirty(struct bio *bio)
  1361. {
  1362. struct bio_vec *bvec;
  1363. int i;
  1364. bio_for_each_segment_all(bvec, bio, i) {
  1365. struct page *page = bvec->bv_page;
  1366. if (page && !PageCompound(page))
  1367. set_page_dirty_lock(page);
  1368. }
  1369. }
  1370. static void bio_release_pages(struct bio *bio)
  1371. {
  1372. struct bio_vec *bvec;
  1373. int i;
  1374. bio_for_each_segment_all(bvec, bio, i) {
  1375. struct page *page = bvec->bv_page;
  1376. if (page)
  1377. put_page(page);
  1378. }
  1379. }
  1380. /*
  1381. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  1382. * If they are, then fine. If, however, some pages are clean then they must
  1383. * have been written out during the direct-IO read. So we take another ref on
  1384. * the BIO and the offending pages and re-dirty the pages in process context.
  1385. *
  1386. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  1387. * here on. It will run one put_page() against each page and will run one
  1388. * bio_put() against the BIO.
  1389. */
  1390. static void bio_dirty_fn(struct work_struct *work);
  1391. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
  1392. static DEFINE_SPINLOCK(bio_dirty_lock);
  1393. static struct bio *bio_dirty_list;
  1394. /*
  1395. * This runs in process context
  1396. */
  1397. static void bio_dirty_fn(struct work_struct *work)
  1398. {
  1399. unsigned long flags;
  1400. struct bio *bio;
  1401. spin_lock_irqsave(&bio_dirty_lock, flags);
  1402. bio = bio_dirty_list;
  1403. bio_dirty_list = NULL;
  1404. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1405. while (bio) {
  1406. struct bio *next = bio->bi_private;
  1407. bio_set_pages_dirty(bio);
  1408. bio_release_pages(bio);
  1409. bio_put(bio);
  1410. bio = next;
  1411. }
  1412. }
  1413. void bio_check_pages_dirty(struct bio *bio)
  1414. {
  1415. struct bio_vec *bvec;
  1416. int nr_clean_pages = 0;
  1417. int i;
  1418. bio_for_each_segment_all(bvec, bio, i) {
  1419. struct page *page = bvec->bv_page;
  1420. if (PageDirty(page) || PageCompound(page)) {
  1421. put_page(page);
  1422. bvec->bv_page = NULL;
  1423. } else {
  1424. nr_clean_pages++;
  1425. }
  1426. }
  1427. if (nr_clean_pages) {
  1428. unsigned long flags;
  1429. spin_lock_irqsave(&bio_dirty_lock, flags);
  1430. bio->bi_private = bio_dirty_list;
  1431. bio_dirty_list = bio;
  1432. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1433. schedule_work(&bio_dirty_work);
  1434. } else {
  1435. bio_put(bio);
  1436. }
  1437. }
  1438. void generic_start_io_acct(int rw, unsigned long sectors,
  1439. struct hd_struct *part)
  1440. {
  1441. int cpu = part_stat_lock();
  1442. part_round_stats(cpu, part);
  1443. part_stat_inc(cpu, part, ios[rw]);
  1444. part_stat_add(cpu, part, sectors[rw], sectors);
  1445. part_inc_in_flight(part, rw);
  1446. part_stat_unlock();
  1447. }
  1448. EXPORT_SYMBOL(generic_start_io_acct);
  1449. void generic_end_io_acct(int rw, struct hd_struct *part,
  1450. unsigned long start_time)
  1451. {
  1452. unsigned long duration = jiffies - start_time;
  1453. int cpu = part_stat_lock();
  1454. part_stat_add(cpu, part, ticks[rw], duration);
  1455. part_round_stats(cpu, part);
  1456. part_dec_in_flight(part, rw);
  1457. part_stat_unlock();
  1458. }
  1459. EXPORT_SYMBOL(generic_end_io_acct);
  1460. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  1461. void bio_flush_dcache_pages(struct bio *bi)
  1462. {
  1463. struct bio_vec bvec;
  1464. struct bvec_iter iter;
  1465. bio_for_each_segment(bvec, bi, iter)
  1466. flush_dcache_page(bvec.bv_page);
  1467. }
  1468. EXPORT_SYMBOL(bio_flush_dcache_pages);
  1469. #endif
  1470. static inline bool bio_remaining_done(struct bio *bio)
  1471. {
  1472. /*
  1473. * If we're not chaining, then ->__bi_remaining is always 1 and
  1474. * we always end io on the first invocation.
  1475. */
  1476. if (!bio_flagged(bio, BIO_CHAIN))
  1477. return true;
  1478. BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
  1479. if (atomic_dec_and_test(&bio->__bi_remaining)) {
  1480. bio_clear_flag(bio, BIO_CHAIN);
  1481. return true;
  1482. }
  1483. return false;
  1484. }
  1485. /**
  1486. * bio_endio - end I/O on a bio
  1487. * @bio: bio
  1488. *
  1489. * Description:
  1490. * bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
  1491. * way to end I/O on a bio. No one should call bi_end_io() directly on a
  1492. * bio unless they own it and thus know that it has an end_io function.
  1493. **/
  1494. void bio_endio(struct bio *bio)
  1495. {
  1496. again:
  1497. if (!bio_remaining_done(bio))
  1498. return;
  1499. /*
  1500. * Need to have a real endio function for chained bios, otherwise
  1501. * various corner cases will break (like stacking block devices that
  1502. * save/restore bi_end_io) - however, we want to avoid unbounded
  1503. * recursion and blowing the stack. Tail call optimization would
  1504. * handle this, but compiling with frame pointers also disables
  1505. * gcc's sibling call optimization.
  1506. */
  1507. if (bio->bi_end_io == bio_chain_endio) {
  1508. bio = __bio_chain_endio(bio);
  1509. goto again;
  1510. }
  1511. if (bio->bi_end_io)
  1512. bio->bi_end_io(bio);
  1513. }
  1514. EXPORT_SYMBOL(bio_endio);
  1515. /**
  1516. * bio_split - split a bio
  1517. * @bio: bio to split
  1518. * @sectors: number of sectors to split from the front of @bio
  1519. * @gfp: gfp mask
  1520. * @bs: bio set to allocate from
  1521. *
  1522. * Allocates and returns a new bio which represents @sectors from the start of
  1523. * @bio, and updates @bio to represent the remaining sectors.
  1524. *
  1525. * Unless this is a discard request the newly allocated bio will point
  1526. * to @bio's bi_io_vec; it is the caller's responsibility to ensure that
  1527. * @bio is not freed before the split.
  1528. */
  1529. struct bio *bio_split(struct bio *bio, int sectors,
  1530. gfp_t gfp, struct bio_set *bs)
  1531. {
  1532. struct bio *split = NULL;
  1533. BUG_ON(sectors <= 0);
  1534. BUG_ON(sectors >= bio_sectors(bio));
  1535. /*
  1536. * Discards need a mutable bio_vec to accommodate the payload
  1537. * required by the DSM TRIM and UNMAP commands.
  1538. */
  1539. if (bio_op(bio) == REQ_OP_DISCARD || bio_op(bio) == REQ_OP_SECURE_ERASE)
  1540. split = bio_clone_bioset(bio, gfp, bs);
  1541. else
  1542. split = bio_clone_fast(bio, gfp, bs);
  1543. if (!split)
  1544. return NULL;
  1545. split->bi_iter.bi_size = sectors << 9;
  1546. if (bio_integrity(split))
  1547. bio_integrity_trim(split, 0, sectors);
  1548. bio_advance(bio, split->bi_iter.bi_size);
  1549. return split;
  1550. }
  1551. EXPORT_SYMBOL(bio_split);
  1552. /**
  1553. * bio_trim - trim a bio
  1554. * @bio: bio to trim
  1555. * @offset: number of sectors to trim from the front of @bio
  1556. * @size: size we want to trim @bio to, in sectors
  1557. */
  1558. void bio_trim(struct bio *bio, int offset, int size)
  1559. {
  1560. /* 'bio' is a cloned bio which we need to trim to match
  1561. * the given offset and size.
  1562. */
  1563. size <<= 9;
  1564. if (offset == 0 && size == bio->bi_iter.bi_size)
  1565. return;
  1566. bio_clear_flag(bio, BIO_SEG_VALID);
  1567. bio_advance(bio, offset << 9);
  1568. bio->bi_iter.bi_size = size;
  1569. }
  1570. EXPORT_SYMBOL_GPL(bio_trim);
  1571. /*
  1572. * create memory pools for biovec's in a bio_set.
  1573. * use the global biovec slabs created for general use.
  1574. */
  1575. mempool_t *biovec_create_pool(int pool_entries)
  1576. {
  1577. struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX;
  1578. return mempool_create_slab_pool(pool_entries, bp->slab);
  1579. }
  1580. void bioset_free(struct bio_set *bs)
  1581. {
  1582. if (bs->rescue_workqueue)
  1583. destroy_workqueue(bs->rescue_workqueue);
  1584. if (bs->bio_pool)
  1585. mempool_destroy(bs->bio_pool);
  1586. if (bs->bvec_pool)
  1587. mempool_destroy(bs->bvec_pool);
  1588. bioset_integrity_free(bs);
  1589. bio_put_slab(bs);
  1590. kfree(bs);
  1591. }
  1592. EXPORT_SYMBOL(bioset_free);
  1593. static struct bio_set *__bioset_create(unsigned int pool_size,
  1594. unsigned int front_pad,
  1595. bool create_bvec_pool)
  1596. {
  1597. unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
  1598. struct bio_set *bs;
  1599. bs = kzalloc(sizeof(*bs), GFP_KERNEL);
  1600. if (!bs)
  1601. return NULL;
  1602. bs->front_pad = front_pad;
  1603. spin_lock_init(&bs->rescue_lock);
  1604. bio_list_init(&bs->rescue_list);
  1605. INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
  1606. bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
  1607. if (!bs->bio_slab) {
  1608. kfree(bs);
  1609. return NULL;
  1610. }
  1611. bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
  1612. if (!bs->bio_pool)
  1613. goto bad;
  1614. if (create_bvec_pool) {
  1615. bs->bvec_pool = biovec_create_pool(pool_size);
  1616. if (!bs->bvec_pool)
  1617. goto bad;
  1618. }
  1619. bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
  1620. if (!bs->rescue_workqueue)
  1621. goto bad;
  1622. return bs;
  1623. bad:
  1624. bioset_free(bs);
  1625. return NULL;
  1626. }
  1627. /**
  1628. * bioset_create - Create a bio_set
  1629. * @pool_size: Number of bio and bio_vecs to cache in the mempool
  1630. * @front_pad: Number of bytes to allocate in front of the returned bio
  1631. *
  1632. * Description:
  1633. * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
  1634. * to ask for a number of bytes to be allocated in front of the bio.
  1635. * Front pad allocation is useful for embedding the bio inside
  1636. * another structure, to avoid allocating extra data to go with the bio.
  1637. * Note that the bio must be embedded at the END of that structure always,
  1638. * or things will break badly.
  1639. */
  1640. struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
  1641. {
  1642. return __bioset_create(pool_size, front_pad, true);
  1643. }
  1644. EXPORT_SYMBOL(bioset_create);
  1645. /**
  1646. * bioset_create_nobvec - Create a bio_set without bio_vec mempool
  1647. * @pool_size: Number of bio to cache in the mempool
  1648. * @front_pad: Number of bytes to allocate in front of the returned bio
  1649. *
  1650. * Description:
  1651. * Same functionality as bioset_create() except that mempool is not
  1652. * created for bio_vecs. Saving some memory for bio_clone_fast() users.
  1653. */
  1654. struct bio_set *bioset_create_nobvec(unsigned int pool_size, unsigned int front_pad)
  1655. {
  1656. return __bioset_create(pool_size, front_pad, false);
  1657. }
  1658. EXPORT_SYMBOL(bioset_create_nobvec);
  1659. #ifdef CONFIG_BLK_CGROUP
  1660. /**
  1661. * bio_associate_blkcg - associate a bio with the specified blkcg
  1662. * @bio: target bio
  1663. * @blkcg_css: css of the blkcg to associate
  1664. *
  1665. * Associate @bio with the blkcg specified by @blkcg_css. Block layer will
  1666. * treat @bio as if it were issued by a task which belongs to the blkcg.
  1667. *
  1668. * This function takes an extra reference of @blkcg_css which will be put
  1669. * when @bio is released. The caller must own @bio and is responsible for
  1670. * synchronizing calls to this function.
  1671. */
  1672. int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css)
  1673. {
  1674. if (unlikely(bio->bi_css))
  1675. return -EBUSY;
  1676. css_get(blkcg_css);
  1677. bio->bi_css = blkcg_css;
  1678. return 0;
  1679. }
  1680. EXPORT_SYMBOL_GPL(bio_associate_blkcg);
  1681. /**
  1682. * bio_associate_current - associate a bio with %current
  1683. * @bio: target bio
  1684. *
  1685. * Associate @bio with %current if it hasn't been associated yet. Block
  1686. * layer will treat @bio as if it were issued by %current no matter which
  1687. * task actually issues it.
  1688. *
  1689. * This function takes an extra reference of @task's io_context and blkcg
  1690. * which will be put when @bio is released. The caller must own @bio,
  1691. * ensure %current->io_context exists, and is responsible for synchronizing
  1692. * calls to this function.
  1693. */
  1694. int bio_associate_current(struct bio *bio)
  1695. {
  1696. struct io_context *ioc;
  1697. if (bio->bi_css)
  1698. return -EBUSY;
  1699. ioc = current->io_context;
  1700. if (!ioc)
  1701. return -ENOENT;
  1702. get_io_context_active(ioc);
  1703. bio->bi_ioc = ioc;
  1704. bio->bi_css = task_get_css(current, io_cgrp_id);
  1705. return 0;
  1706. }
  1707. EXPORT_SYMBOL_GPL(bio_associate_current);
  1708. /**
  1709. * bio_disassociate_task - undo bio_associate_current()
  1710. * @bio: target bio
  1711. */
  1712. void bio_disassociate_task(struct bio *bio)
  1713. {
  1714. if (bio->bi_ioc) {
  1715. put_io_context(bio->bi_ioc);
  1716. bio->bi_ioc = NULL;
  1717. }
  1718. if (bio->bi_css) {
  1719. css_put(bio->bi_css);
  1720. bio->bi_css = NULL;
  1721. }
  1722. }
  1723. /**
  1724. * bio_clone_blkcg_association - clone blkcg association from src to dst bio
  1725. * @dst: destination bio
  1726. * @src: source bio
  1727. */
  1728. void bio_clone_blkcg_association(struct bio *dst, struct bio *src)
  1729. {
  1730. if (src->bi_css)
  1731. WARN_ON(bio_associate_blkcg(dst, src->bi_css));
  1732. }
  1733. #endif /* CONFIG_BLK_CGROUP */
  1734. static void __init biovec_init_slabs(void)
  1735. {
  1736. int i;
  1737. for (i = 0; i < BVEC_POOL_NR; i++) {
  1738. int size;
  1739. struct biovec_slab *bvs = bvec_slabs + i;
  1740. if (bvs->nr_vecs <= BIO_INLINE_VECS) {
  1741. bvs->slab = NULL;
  1742. continue;
  1743. }
  1744. size = bvs->nr_vecs * sizeof(struct bio_vec);
  1745. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  1746. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1747. }
  1748. }
  1749. static int __init init_bio(void)
  1750. {
  1751. bio_slab_max = 2;
  1752. bio_slab_nr = 0;
  1753. bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
  1754. if (!bio_slabs)
  1755. panic("bio: can't allocate bios\n");
  1756. bio_integrity_init();
  1757. biovec_init_slabs();
  1758. fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
  1759. if (!fs_bio_set)
  1760. panic("bio: can't allocate bios\n");
  1761. if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE))
  1762. panic("bio: can't create integrity pool\n");
  1763. return 0;
  1764. }
  1765. subsys_initcall(init_bio);