bio.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. /*
  2. * Copyright (C) 2001 Jens Axboe <[email protected]>
  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. static inline void bio_clone_crypt_key(struct bio *dst, const struct bio *src)
  488. {
  489. #ifdef CONFIG_PFK
  490. dst->bi_iter.bi_dun = src->bi_iter.bi_dun;
  491. #ifdef CONFIG_DM_DEFAULT_KEY
  492. dst->bi_crypt_key = src->bi_crypt_key;
  493. dst->bi_crypt_skip = src->bi_crypt_skip;
  494. #endif
  495. dst->bi_dio_inode = src->bi_dio_inode;
  496. #endif
  497. }
  498. /**
  499. * __bio_clone_fast - clone a bio that shares the original bio's biovec
  500. * @bio: destination bio
  501. * @bio_src: bio to clone
  502. *
  503. * Clone a &bio. Caller will own the returned bio, but not
  504. * the actual data it points to. Reference count of returned
  505. * bio will be one.
  506. *
  507. * Caller must ensure that @bio_src is not freed before @bio.
  508. */
  509. void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
  510. {
  511. BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio));
  512. /*
  513. * most users will be overriding ->bi_bdev with a new target,
  514. * so we don't set nor calculate new physical/hw segment counts here
  515. */
  516. bio->bi_bdev = bio_src->bi_bdev;
  517. bio_set_flag(bio, BIO_CLONED);
  518. bio->bi_opf = bio_src->bi_opf;
  519. bio->bi_iter = bio_src->bi_iter;
  520. bio->bi_io_vec = bio_src->bi_io_vec;
  521. bio_clone_crypt_key(bio, bio_src);
  522. bio_clone_blkcg_association(bio, bio_src);
  523. }
  524. EXPORT_SYMBOL(__bio_clone_fast);
  525. /**
  526. * bio_clone_fast - clone a bio that shares the original bio's biovec
  527. * @bio: bio to clone
  528. * @gfp_mask: allocation priority
  529. * @bs: bio_set to allocate from
  530. *
  531. * Like __bio_clone_fast, only also allocates the returned bio
  532. */
  533. struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
  534. {
  535. struct bio *b;
  536. b = bio_alloc_bioset(gfp_mask, 0, bs);
  537. if (!b)
  538. return NULL;
  539. __bio_clone_fast(b, bio);
  540. if (bio_integrity(bio)) {
  541. int ret;
  542. ret = bio_integrity_clone(b, bio, gfp_mask);
  543. if (ret < 0) {
  544. bio_put(b);
  545. return NULL;
  546. }
  547. }
  548. return b;
  549. }
  550. EXPORT_SYMBOL(bio_clone_fast);
  551. /**
  552. * bio_clone_bioset - clone a bio
  553. * @bio_src: bio to clone
  554. * @gfp_mask: allocation priority
  555. * @bs: bio_set to allocate from
  556. *
  557. * Clone bio. Caller will own the returned bio, but not the actual data it
  558. * points to. Reference count of returned bio will be one.
  559. */
  560. struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask,
  561. struct bio_set *bs)
  562. {
  563. struct bvec_iter iter;
  564. struct bio_vec bv;
  565. struct bio *bio;
  566. /*
  567. * Pre immutable biovecs, __bio_clone() used to just do a memcpy from
  568. * bio_src->bi_io_vec to bio->bi_io_vec.
  569. *
  570. * We can't do that anymore, because:
  571. *
  572. * - The point of cloning the biovec is to produce a bio with a biovec
  573. * the caller can modify: bi_idx and bi_bvec_done should be 0.
  574. *
  575. * - The original bio could've had more than BIO_MAX_PAGES biovecs; if
  576. * we tried to clone the whole thing bio_alloc_bioset() would fail.
  577. * But the clone should succeed as long as the number of biovecs we
  578. * actually need to allocate is fewer than BIO_MAX_PAGES.
  579. *
  580. * - Lastly, bi_vcnt should not be looked at or relied upon by code
  581. * that does not own the bio - reason being drivers don't use it for
  582. * iterating over the biovec anymore, so expecting it to be kept up
  583. * to date (i.e. for clones that share the parent biovec) is just
  584. * asking for trouble and would force extra work on
  585. * __bio_clone_fast() anyways.
  586. */
  587. bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs);
  588. if (!bio)
  589. return NULL;
  590. bio->bi_bdev = bio_src->bi_bdev;
  591. bio->bi_opf = bio_src->bi_opf;
  592. bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
  593. bio->bi_iter.bi_size = bio_src->bi_iter.bi_size;
  594. switch (bio_op(bio)) {
  595. case REQ_OP_DISCARD:
  596. case REQ_OP_SECURE_ERASE:
  597. break;
  598. case REQ_OP_WRITE_SAME:
  599. bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0];
  600. break;
  601. default:
  602. bio_for_each_segment(bv, bio_src, iter)
  603. bio->bi_io_vec[bio->bi_vcnt++] = bv;
  604. break;
  605. }
  606. if (bio_integrity(bio_src)) {
  607. int ret;
  608. ret = bio_integrity_clone(bio, bio_src, gfp_mask);
  609. if (ret < 0) {
  610. bio_put(bio);
  611. return NULL;
  612. }
  613. }
  614. bio_clone_crypt_key(bio, bio_src);
  615. bio_clone_blkcg_association(bio, bio_src);
  616. return bio;
  617. }
  618. EXPORT_SYMBOL(bio_clone_bioset);
  619. /**
  620. * bio_add_pc_page - attempt to add page to bio
  621. * @q: the target queue
  622. * @bio: destination bio
  623. * @page: page to add
  624. * @len: vec entry length
  625. * @offset: vec entry offset
  626. *
  627. * Attempt to add a page to the bio_vec maplist. This can fail for a
  628. * number of reasons, such as the bio being full or target block device
  629. * limitations. The target block device must allow bio's up to PAGE_SIZE,
  630. * so it is always possible to add a single page to an empty bio.
  631. *
  632. * This should only be used by REQ_PC bios.
  633. */
  634. int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page
  635. *page, unsigned int len, unsigned int offset)
  636. {
  637. int retried_segments = 0;
  638. struct bio_vec *bvec;
  639. /*
  640. * cloned bio must not modify vec list
  641. */
  642. if (unlikely(bio_flagged(bio, BIO_CLONED)))
  643. return 0;
  644. if (((bio->bi_iter.bi_size + len) >> 9) > queue_max_hw_sectors(q))
  645. return 0;
  646. /*
  647. * For filesystems with a blocksize smaller than the pagesize
  648. * we will often be called with the same page as last time and
  649. * a consecutive offset. Optimize this special case.
  650. */
  651. if (bio->bi_vcnt > 0) {
  652. struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  653. if (page == prev->bv_page &&
  654. offset == prev->bv_offset + prev->bv_len) {
  655. prev->bv_len += len;
  656. bio->bi_iter.bi_size += len;
  657. goto done;
  658. }
  659. /*
  660. * If the queue doesn't support SG gaps and adding this
  661. * offset would create a gap, disallow it.
  662. */
  663. if (bvec_gap_to_prev(q, prev, offset))
  664. return 0;
  665. }
  666. if (bio->bi_vcnt >= bio->bi_max_vecs)
  667. return 0;
  668. /*
  669. * setup the new entry, we might clear it again later if we
  670. * cannot add the page
  671. */
  672. bvec = &bio->bi_io_vec[bio->bi_vcnt];
  673. bvec->bv_page = page;
  674. bvec->bv_len = len;
  675. bvec->bv_offset = offset;
  676. bio->bi_vcnt++;
  677. bio->bi_phys_segments++;
  678. bio->bi_iter.bi_size += len;
  679. /*
  680. * Perform a recount if the number of segments is greater
  681. * than queue_max_segments(q).
  682. */
  683. while (bio->bi_phys_segments > queue_max_segments(q)) {
  684. if (retried_segments)
  685. goto failed;
  686. retried_segments = 1;
  687. blk_recount_segments(q, bio);
  688. }
  689. /* If we may be able to merge these biovecs, force a recount */
  690. if (bio->bi_vcnt > 1 && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
  691. bio_clear_flag(bio, BIO_SEG_VALID);
  692. done:
  693. return len;
  694. failed:
  695. bvec->bv_page = NULL;
  696. bvec->bv_len = 0;
  697. bvec->bv_offset = 0;
  698. bio->bi_vcnt--;
  699. bio->bi_iter.bi_size -= len;
  700. blk_recount_segments(q, bio);
  701. return 0;
  702. }
  703. EXPORT_SYMBOL(bio_add_pc_page);
  704. /**
  705. * bio_add_page - attempt to add page to bio
  706. * @bio: destination bio
  707. * @page: page to add
  708. * @len: vec entry length
  709. * @offset: vec entry offset
  710. *
  711. * Attempt to add a page to the bio_vec maplist. This will only fail
  712. * if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
  713. */
  714. int bio_add_page(struct bio *bio, struct page *page,
  715. unsigned int len, unsigned int offset)
  716. {
  717. struct bio_vec *bv;
  718. /*
  719. * cloned bio must not modify vec list
  720. */
  721. if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
  722. return 0;
  723. /*
  724. * For filesystems with a blocksize smaller than the pagesize
  725. * we will often be called with the same page as last time and
  726. * a consecutive offset. Optimize this special case.
  727. */
  728. if (bio->bi_vcnt > 0) {
  729. bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
  730. if (page == bv->bv_page &&
  731. offset == bv->bv_offset + bv->bv_len) {
  732. bv->bv_len += len;
  733. goto done;
  734. }
  735. }
  736. if (bio->bi_vcnt >= bio->bi_max_vecs)
  737. return 0;
  738. bv = &bio->bi_io_vec[bio->bi_vcnt];
  739. bv->bv_page = page;
  740. bv->bv_len = len;
  741. bv->bv_offset = offset;
  742. bio->bi_vcnt++;
  743. done:
  744. bio->bi_iter.bi_size += len;
  745. if (!bio_flagged(bio, BIO_WORKINGSET) && unlikely(PageWorkingset(page)))
  746. bio_set_flag(bio, BIO_WORKINGSET);
  747. return len;
  748. }
  749. EXPORT_SYMBOL(bio_add_page);
  750. struct submit_bio_ret {
  751. struct completion event;
  752. int error;
  753. };
  754. static void submit_bio_wait_endio(struct bio *bio)
  755. {
  756. struct submit_bio_ret *ret = bio->bi_private;
  757. ret->error = bio->bi_error;
  758. complete(&ret->event);
  759. }
  760. /**
  761. * submit_bio_wait - submit a bio, and wait until it completes
  762. * @bio: The &struct bio which describes the I/O
  763. *
  764. * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
  765. * bio_endio() on failure.
  766. */
  767. int submit_bio_wait(struct bio *bio)
  768. {
  769. struct submit_bio_ret ret;
  770. init_completion(&ret.event);
  771. bio->bi_private = &ret;
  772. bio->bi_end_io = submit_bio_wait_endio;
  773. bio->bi_opf |= REQ_SYNC;
  774. submit_bio(bio);
  775. wait_for_completion_io(&ret.event);
  776. return ret.error;
  777. }
  778. EXPORT_SYMBOL(submit_bio_wait);
  779. /**
  780. * bio_advance - increment/complete a bio by some number of bytes
  781. * @bio: bio to advance
  782. * @bytes: number of bytes to complete
  783. *
  784. * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
  785. * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
  786. * be updated on the last bvec as well.
  787. *
  788. * @bio will then represent the remaining, uncompleted portion of the io.
  789. */
  790. void bio_advance(struct bio *bio, unsigned bytes)
  791. {
  792. if (bio_integrity(bio))
  793. bio_integrity_advance(bio, bytes);
  794. bio_advance_iter(bio, &bio->bi_iter, bytes);
  795. }
  796. EXPORT_SYMBOL(bio_advance);
  797. /**
  798. * bio_alloc_pages - allocates a single page for each bvec in a bio
  799. * @bio: bio to allocate pages for
  800. * @gfp_mask: flags for allocation
  801. *
  802. * Allocates pages up to @bio->bi_vcnt.
  803. *
  804. * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
  805. * freed.
  806. */
  807. int bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
  808. {
  809. int i;
  810. struct bio_vec *bv;
  811. bio_for_each_segment_all(bv, bio, i) {
  812. bv->bv_page = alloc_page(gfp_mask);
  813. if (!bv->bv_page) {
  814. while (--bv >= bio->bi_io_vec)
  815. __free_page(bv->bv_page);
  816. return -ENOMEM;
  817. }
  818. }
  819. return 0;
  820. }
  821. EXPORT_SYMBOL(bio_alloc_pages);
  822. /**
  823. * bio_copy_data - copy contents of data buffers from one chain of bios to
  824. * another
  825. * @src: source bio list
  826. * @dst: destination bio list
  827. *
  828. * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats
  829. * @src and @dst as linked lists of bios.
  830. *
  831. * Stops when it reaches the end of either @src or @dst - that is, copies
  832. * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
  833. */
  834. void bio_copy_data(struct bio *dst, struct bio *src)
  835. {
  836. struct bvec_iter src_iter, dst_iter;
  837. struct bio_vec src_bv, dst_bv;
  838. void *src_p, *dst_p;
  839. unsigned bytes;
  840. src_iter = src->bi_iter;
  841. dst_iter = dst->bi_iter;
  842. while (1) {
  843. if (!src_iter.bi_size) {
  844. src = src->bi_next;
  845. if (!src)
  846. break;
  847. src_iter = src->bi_iter;
  848. }
  849. if (!dst_iter.bi_size) {
  850. dst = dst->bi_next;
  851. if (!dst)
  852. break;
  853. dst_iter = dst->bi_iter;
  854. }
  855. src_bv = bio_iter_iovec(src, src_iter);
  856. dst_bv = bio_iter_iovec(dst, dst_iter);
  857. bytes = min(src_bv.bv_len, dst_bv.bv_len);
  858. src_p = kmap_atomic(src_bv.bv_page);
  859. dst_p = kmap_atomic(dst_bv.bv_page);
  860. memcpy(dst_p + dst_bv.bv_offset,
  861. src_p + src_bv.bv_offset,
  862. bytes);
  863. kunmap_atomic(dst_p);
  864. kunmap_atomic(src_p);
  865. bio_advance_iter(src, &src_iter, bytes);
  866. bio_advance_iter(dst, &dst_iter, bytes);
  867. }
  868. }
  869. EXPORT_SYMBOL(bio_copy_data);
  870. struct bio_map_data {
  871. int is_our_pages;
  872. struct iov_iter iter;
  873. struct iovec iov[];
  874. };
  875. static struct bio_map_data *bio_alloc_map_data(unsigned int iov_count,
  876. gfp_t gfp_mask)
  877. {
  878. if (iov_count > UIO_MAXIOV)
  879. return NULL;
  880. return kmalloc(sizeof(struct bio_map_data) +
  881. sizeof(struct iovec) * iov_count, gfp_mask);
  882. }
  883. /**
  884. * bio_copy_from_iter - copy all pages from iov_iter to bio
  885. * @bio: The &struct bio which describes the I/O as destination
  886. * @iter: iov_iter as source
  887. *
  888. * Copy all pages from iov_iter to bio.
  889. * Returns 0 on success, or error on failure.
  890. */
  891. static int bio_copy_from_iter(struct bio *bio, struct iov_iter iter)
  892. {
  893. int i;
  894. struct bio_vec *bvec;
  895. bio_for_each_segment_all(bvec, bio, i) {
  896. ssize_t ret;
  897. ret = copy_page_from_iter(bvec->bv_page,
  898. bvec->bv_offset,
  899. bvec->bv_len,
  900. &iter);
  901. if (!iov_iter_count(&iter))
  902. break;
  903. if (ret < bvec->bv_len)
  904. return -EFAULT;
  905. }
  906. return 0;
  907. }
  908. /**
  909. * bio_copy_to_iter - copy all pages from bio to iov_iter
  910. * @bio: The &struct bio which describes the I/O as source
  911. * @iter: iov_iter as destination
  912. *
  913. * Copy all pages from bio to iov_iter.
  914. * Returns 0 on success, or error on failure.
  915. */
  916. static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter)
  917. {
  918. int i;
  919. struct bio_vec *bvec;
  920. bio_for_each_segment_all(bvec, bio, i) {
  921. ssize_t ret;
  922. ret = copy_page_to_iter(bvec->bv_page,
  923. bvec->bv_offset,
  924. bvec->bv_len,
  925. &iter);
  926. if (!iov_iter_count(&iter))
  927. break;
  928. if (ret < bvec->bv_len)
  929. return -EFAULT;
  930. }
  931. return 0;
  932. }
  933. void bio_free_pages(struct bio *bio)
  934. {
  935. struct bio_vec *bvec;
  936. int i;
  937. bio_for_each_segment_all(bvec, bio, i)
  938. __free_page(bvec->bv_page);
  939. }
  940. EXPORT_SYMBOL(bio_free_pages);
  941. /**
  942. * bio_uncopy_user - finish previously mapped bio
  943. * @bio: bio being terminated
  944. *
  945. * Free pages allocated from bio_copy_user_iov() and write back data
  946. * to user space in case of a read.
  947. */
  948. int bio_uncopy_user(struct bio *bio)
  949. {
  950. struct bio_map_data *bmd = bio->bi_private;
  951. int ret = 0;
  952. if (!bio_flagged(bio, BIO_NULL_MAPPED)) {
  953. /*
  954. * if we're in a workqueue, the request is orphaned, so
  955. * don't copy into a random user address space, just free
  956. * and return -EINTR so user space doesn't expect any data.
  957. */
  958. if (!current->mm)
  959. ret = -EINTR;
  960. else if (bio_data_dir(bio) == READ)
  961. ret = bio_copy_to_iter(bio, bmd->iter);
  962. if (bmd->is_our_pages)
  963. bio_free_pages(bio);
  964. }
  965. kfree(bmd);
  966. bio_put(bio);
  967. return ret;
  968. }
  969. /**
  970. * bio_copy_user_iov - copy user data to bio
  971. * @q: destination block queue
  972. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  973. * @iter: iovec iterator
  974. * @gfp_mask: memory allocation flags
  975. *
  976. * Prepares and returns a bio for indirect user io, bouncing data
  977. * to/from kernel pages as necessary. Must be paired with
  978. * call bio_uncopy_user() on io completion.
  979. */
  980. struct bio *bio_copy_user_iov(struct request_queue *q,
  981. struct rq_map_data *map_data,
  982. const struct iov_iter *iter,
  983. gfp_t gfp_mask)
  984. {
  985. struct bio_map_data *bmd;
  986. struct page *page;
  987. struct bio *bio;
  988. int i, ret;
  989. int nr_pages = 0;
  990. unsigned int len = iter->count;
  991. unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0;
  992. for (i = 0; i < iter->nr_segs; i++) {
  993. unsigned long uaddr;
  994. unsigned long end;
  995. unsigned long start;
  996. uaddr = (unsigned long) iter->iov[i].iov_base;
  997. end = (uaddr + iter->iov[i].iov_len + PAGE_SIZE - 1)
  998. >> PAGE_SHIFT;
  999. start = uaddr >> PAGE_SHIFT;
  1000. /*
  1001. * Overflow, abort
  1002. */
  1003. if (end < start)
  1004. return ERR_PTR(-EINVAL);
  1005. nr_pages += end - start;
  1006. }
  1007. if (offset)
  1008. nr_pages++;
  1009. bmd = bio_alloc_map_data(iter->nr_segs, gfp_mask);
  1010. if (!bmd)
  1011. return ERR_PTR(-ENOMEM);
  1012. /*
  1013. * We need to do a deep copy of the iov_iter including the iovecs.
  1014. * The caller provided iov might point to an on-stack or otherwise
  1015. * shortlived one.
  1016. */
  1017. bmd->is_our_pages = map_data ? 0 : 1;
  1018. memcpy(bmd->iov, iter->iov, sizeof(struct iovec) * iter->nr_segs);
  1019. bmd->iter = *iter;
  1020. bmd->iter.iov = bmd->iov;
  1021. ret = -ENOMEM;
  1022. bio = bio_kmalloc(gfp_mask, nr_pages);
  1023. if (!bio)
  1024. goto out_bmd;
  1025. if (iter->type & WRITE)
  1026. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  1027. ret = 0;
  1028. if (map_data) {
  1029. nr_pages = 1 << map_data->page_order;
  1030. i = map_data->offset / PAGE_SIZE;
  1031. }
  1032. while (len) {
  1033. unsigned int bytes = PAGE_SIZE;
  1034. bytes -= offset;
  1035. if (bytes > len)
  1036. bytes = len;
  1037. if (map_data) {
  1038. if (i == map_data->nr_entries * nr_pages) {
  1039. ret = -ENOMEM;
  1040. break;
  1041. }
  1042. page = map_data->pages[i / nr_pages];
  1043. page += (i % nr_pages);
  1044. i++;
  1045. } else {
  1046. page = alloc_page(q->bounce_gfp | gfp_mask);
  1047. if (!page) {
  1048. ret = -ENOMEM;
  1049. break;
  1050. }
  1051. }
  1052. if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes) {
  1053. if (!map_data)
  1054. __free_page(page);
  1055. break;
  1056. }
  1057. len -= bytes;
  1058. offset = 0;
  1059. }
  1060. if (ret)
  1061. goto cleanup;
  1062. /*
  1063. * success
  1064. */
  1065. if (((iter->type & WRITE) && (!map_data || !map_data->null_mapped)) ||
  1066. (map_data && map_data->from_user)) {
  1067. ret = bio_copy_from_iter(bio, *iter);
  1068. if (ret)
  1069. goto cleanup;
  1070. }
  1071. bio->bi_private = bmd;
  1072. return bio;
  1073. cleanup:
  1074. if (!map_data)
  1075. bio_free_pages(bio);
  1076. bio_put(bio);
  1077. out_bmd:
  1078. kfree(bmd);
  1079. return ERR_PTR(ret);
  1080. }
  1081. /**
  1082. * bio_map_user_iov - map user iovec into bio
  1083. * @q: the struct request_queue for the bio
  1084. * @iter: iovec iterator
  1085. * @gfp_mask: memory allocation flags
  1086. *
  1087. * Map the user space address into a bio suitable for io to a block
  1088. * device. Returns an error pointer in case of error.
  1089. */
  1090. struct bio *bio_map_user_iov(struct request_queue *q,
  1091. const struct iov_iter *iter,
  1092. gfp_t gfp_mask)
  1093. {
  1094. int j;
  1095. int nr_pages = 0;
  1096. struct page **pages;
  1097. struct bio *bio;
  1098. int cur_page = 0;
  1099. int ret, offset;
  1100. struct iov_iter i;
  1101. struct iovec iov;
  1102. struct bio_vec *bvec;
  1103. iov_for_each(iov, i, *iter) {
  1104. unsigned long uaddr = (unsigned long) iov.iov_base;
  1105. unsigned long len = iov.iov_len;
  1106. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1107. unsigned long start = uaddr >> PAGE_SHIFT;
  1108. /*
  1109. * Overflow, abort
  1110. */
  1111. if (end < start)
  1112. return ERR_PTR(-EINVAL);
  1113. nr_pages += end - start;
  1114. /*
  1115. * buffer must be aligned to at least logical block size for now
  1116. */
  1117. if (uaddr & queue_dma_alignment(q))
  1118. return ERR_PTR(-EINVAL);
  1119. }
  1120. if (!nr_pages)
  1121. return ERR_PTR(-EINVAL);
  1122. bio = bio_kmalloc(gfp_mask, nr_pages);
  1123. if (!bio)
  1124. return ERR_PTR(-ENOMEM);
  1125. ret = -ENOMEM;
  1126. pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask);
  1127. if (!pages)
  1128. goto out;
  1129. iov_for_each(iov, i, *iter) {
  1130. unsigned long uaddr = (unsigned long) iov.iov_base;
  1131. unsigned long len = iov.iov_len;
  1132. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1133. unsigned long start = uaddr >> PAGE_SHIFT;
  1134. const int local_nr_pages = end - start;
  1135. const int page_limit = cur_page + local_nr_pages;
  1136. ret = get_user_pages_fast(uaddr, local_nr_pages,
  1137. (iter->type & WRITE) != WRITE,
  1138. &pages[cur_page]);
  1139. if (unlikely(ret < local_nr_pages)) {
  1140. for (j = cur_page; j < page_limit; j++) {
  1141. if (!pages[j])
  1142. break;
  1143. put_page(pages[j]);
  1144. }
  1145. ret = -EFAULT;
  1146. goto out_unmap;
  1147. }
  1148. offset = offset_in_page(uaddr);
  1149. for (j = cur_page; j < page_limit; j++) {
  1150. unsigned int bytes = PAGE_SIZE - offset;
  1151. unsigned short prev_bi_vcnt = bio->bi_vcnt;
  1152. if (len <= 0)
  1153. break;
  1154. if (bytes > len)
  1155. bytes = len;
  1156. /*
  1157. * sorry...
  1158. */
  1159. if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
  1160. bytes)
  1161. break;
  1162. /*
  1163. * check if vector was merged with previous
  1164. * drop page reference if needed
  1165. */
  1166. if (bio->bi_vcnt == prev_bi_vcnt)
  1167. put_page(pages[j]);
  1168. len -= bytes;
  1169. offset = 0;
  1170. }
  1171. cur_page = j;
  1172. /*
  1173. * release the pages we didn't map into the bio, if any
  1174. */
  1175. while (j < page_limit)
  1176. put_page(pages[j++]);
  1177. }
  1178. kfree(pages);
  1179. /*
  1180. * set data direction, and check if mapped pages need bouncing
  1181. */
  1182. if (iter->type & WRITE)
  1183. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  1184. bio_set_flag(bio, BIO_USER_MAPPED);
  1185. /*
  1186. * subtle -- if __bio_map_user() ended up bouncing a bio,
  1187. * it would normally disappear when its bi_end_io is run.
  1188. * however, we need it for the unmap, so grab an extra
  1189. * reference to it
  1190. */
  1191. bio_get(bio);
  1192. return bio;
  1193. out_unmap:
  1194. bio_for_each_segment_all(bvec, bio, j) {
  1195. put_page(bvec->bv_page);
  1196. }
  1197. out:
  1198. kfree(pages);
  1199. bio_put(bio);
  1200. return ERR_PTR(ret);
  1201. }
  1202. static void __bio_unmap_user(struct bio *bio)
  1203. {
  1204. struct bio_vec *bvec;
  1205. int i;
  1206. /*
  1207. * make sure we dirty pages we wrote to
  1208. */
  1209. bio_for_each_segment_all(bvec, bio, i) {
  1210. if (bio_data_dir(bio) == READ)
  1211. set_page_dirty_lock(bvec->bv_page);
  1212. put_page(bvec->bv_page);
  1213. }
  1214. bio_put(bio);
  1215. }
  1216. /**
  1217. * bio_unmap_user - unmap a bio
  1218. * @bio: the bio being unmapped
  1219. *
  1220. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  1221. * a process context.
  1222. *
  1223. * bio_unmap_user() may sleep.
  1224. */
  1225. void bio_unmap_user(struct bio *bio)
  1226. {
  1227. __bio_unmap_user(bio);
  1228. bio_put(bio);
  1229. }
  1230. static void bio_map_kern_endio(struct bio *bio)
  1231. {
  1232. bio_put(bio);
  1233. }
  1234. /**
  1235. * bio_map_kern - map kernel address into bio
  1236. * @q: the struct request_queue for the bio
  1237. * @data: pointer to buffer to map
  1238. * @len: length in bytes
  1239. * @gfp_mask: allocation flags for bio allocation
  1240. *
  1241. * Map the kernel address into a bio suitable for io to a block
  1242. * device. Returns an error pointer in case of error.
  1243. */
  1244. struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
  1245. gfp_t gfp_mask)
  1246. {
  1247. unsigned long kaddr = (unsigned long)data;
  1248. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1249. unsigned long start = kaddr >> PAGE_SHIFT;
  1250. const int nr_pages = end - start;
  1251. int offset, i;
  1252. struct bio *bio;
  1253. bio = bio_kmalloc(gfp_mask, nr_pages);
  1254. if (!bio)
  1255. return ERR_PTR(-ENOMEM);
  1256. offset = offset_in_page(kaddr);
  1257. for (i = 0; i < nr_pages; i++) {
  1258. unsigned int bytes = PAGE_SIZE - offset;
  1259. if (len <= 0)
  1260. break;
  1261. if (bytes > len)
  1262. bytes = len;
  1263. if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
  1264. offset) < bytes) {
  1265. /* we don't support partial mappings */
  1266. bio_put(bio);
  1267. return ERR_PTR(-EINVAL);
  1268. }
  1269. data += bytes;
  1270. len -= bytes;
  1271. offset = 0;
  1272. }
  1273. bio->bi_end_io = bio_map_kern_endio;
  1274. return bio;
  1275. }
  1276. EXPORT_SYMBOL(bio_map_kern);
  1277. static void bio_copy_kern_endio(struct bio *bio)
  1278. {
  1279. bio_free_pages(bio);
  1280. bio_put(bio);
  1281. }
  1282. static void bio_copy_kern_endio_read(struct bio *bio)
  1283. {
  1284. char *p = bio->bi_private;
  1285. struct bio_vec *bvec;
  1286. int i;
  1287. bio_for_each_segment_all(bvec, bio, i) {
  1288. memcpy(p, page_address(bvec->bv_page), bvec->bv_len);
  1289. p += bvec->bv_len;
  1290. }
  1291. bio_copy_kern_endio(bio);
  1292. }
  1293. /**
  1294. * bio_copy_kern - copy kernel address into bio
  1295. * @q: the struct request_queue for the bio
  1296. * @data: pointer to buffer to copy
  1297. * @len: length in bytes
  1298. * @gfp_mask: allocation flags for bio and page allocation
  1299. * @reading: data direction is READ
  1300. *
  1301. * copy the kernel address into a bio suitable for io to a block
  1302. * device. Returns an error pointer in case of error.
  1303. */
  1304. struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
  1305. gfp_t gfp_mask, int reading)
  1306. {
  1307. unsigned long kaddr = (unsigned long)data;
  1308. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1309. unsigned long start = kaddr >> PAGE_SHIFT;
  1310. struct bio *bio;
  1311. void *p = data;
  1312. int nr_pages = 0;
  1313. /*
  1314. * Overflow, abort
  1315. */
  1316. if (end < start)
  1317. return ERR_PTR(-EINVAL);
  1318. nr_pages = end - start;
  1319. bio = bio_kmalloc(gfp_mask, nr_pages);
  1320. if (!bio)
  1321. return ERR_PTR(-ENOMEM);
  1322. while (len) {
  1323. struct page *page;
  1324. unsigned int bytes = PAGE_SIZE;
  1325. if (bytes > len)
  1326. bytes = len;
  1327. page = alloc_page(q->bounce_gfp | gfp_mask);
  1328. if (!page)
  1329. goto cleanup;
  1330. if (!reading)
  1331. memcpy(page_address(page), p, bytes);
  1332. if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
  1333. break;
  1334. len -= bytes;
  1335. p += bytes;
  1336. }
  1337. if (reading) {
  1338. bio->bi_end_io = bio_copy_kern_endio_read;
  1339. bio->bi_private = data;
  1340. } else {
  1341. bio->bi_end_io = bio_copy_kern_endio;
  1342. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  1343. }
  1344. return bio;
  1345. cleanup:
  1346. bio_free_pages(bio);
  1347. bio_put(bio);
  1348. return ERR_PTR(-ENOMEM);
  1349. }
  1350. /*
  1351. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  1352. * for performing direct-IO in BIOs.
  1353. *
  1354. * The problem is that we cannot run set_page_dirty() from interrupt context
  1355. * because the required locks are not interrupt-safe. So what we can do is to
  1356. * mark the pages dirty _before_ performing IO. And in interrupt context,
  1357. * check that the pages are still dirty. If so, fine. If not, redirty them
  1358. * in process context.
  1359. *
  1360. * We special-case compound pages here: normally this means reads into hugetlb
  1361. * pages. The logic in here doesn't really work right for compound pages
  1362. * because the VM does not uniformly chase down the head page in all cases.
  1363. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  1364. * handle them at all. So we skip compound pages here at an early stage.
  1365. *
  1366. * Note that this code is very hard to test under normal circumstances because
  1367. * direct-io pins the pages with get_user_pages(). This makes
  1368. * is_page_cache_freeable return false, and the VM will not clean the pages.
  1369. * But other code (eg, flusher threads) could clean the pages if they are mapped
  1370. * pagecache.
  1371. *
  1372. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  1373. * deferred bio dirtying paths.
  1374. */
  1375. /*
  1376. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  1377. */
  1378. void bio_set_pages_dirty(struct bio *bio)
  1379. {
  1380. struct bio_vec *bvec;
  1381. int i;
  1382. bio_for_each_segment_all(bvec, bio, i) {
  1383. struct page *page = bvec->bv_page;
  1384. if (page && !PageCompound(page))
  1385. set_page_dirty_lock(page);
  1386. }
  1387. }
  1388. static void bio_release_pages(struct bio *bio)
  1389. {
  1390. struct bio_vec *bvec;
  1391. int i;
  1392. bio_for_each_segment_all(bvec, bio, i) {
  1393. struct page *page = bvec->bv_page;
  1394. if (page)
  1395. put_page(page);
  1396. }
  1397. }
  1398. /*
  1399. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  1400. * If they are, then fine. If, however, some pages are clean then they must
  1401. * have been written out during the direct-IO read. So we take another ref on
  1402. * the BIO and the offending pages and re-dirty the pages in process context.
  1403. *
  1404. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  1405. * here on. It will run one put_page() against each page and will run one
  1406. * bio_put() against the BIO.
  1407. */
  1408. static void bio_dirty_fn(struct work_struct *work);
  1409. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
  1410. static DEFINE_SPINLOCK(bio_dirty_lock);
  1411. static struct bio *bio_dirty_list;
  1412. /*
  1413. * This runs in process context
  1414. */
  1415. static void bio_dirty_fn(struct work_struct *work)
  1416. {
  1417. unsigned long flags;
  1418. struct bio *bio;
  1419. spin_lock_irqsave(&bio_dirty_lock, flags);
  1420. bio = bio_dirty_list;
  1421. bio_dirty_list = NULL;
  1422. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1423. while (bio) {
  1424. struct bio *next = bio->bi_private;
  1425. bio_set_pages_dirty(bio);
  1426. bio_release_pages(bio);
  1427. bio_put(bio);
  1428. bio = next;
  1429. }
  1430. }
  1431. void bio_check_pages_dirty(struct bio *bio)
  1432. {
  1433. struct bio_vec *bvec;
  1434. int nr_clean_pages = 0;
  1435. int i;
  1436. bio_for_each_segment_all(bvec, bio, i) {
  1437. struct page *page = bvec->bv_page;
  1438. if (PageDirty(page) || PageCompound(page)) {
  1439. put_page(page);
  1440. bvec->bv_page = NULL;
  1441. } else {
  1442. nr_clean_pages++;
  1443. }
  1444. }
  1445. if (nr_clean_pages) {
  1446. unsigned long flags;
  1447. spin_lock_irqsave(&bio_dirty_lock, flags);
  1448. bio->bi_private = bio_dirty_list;
  1449. bio_dirty_list = bio;
  1450. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1451. schedule_work(&bio_dirty_work);
  1452. } else {
  1453. bio_put(bio);
  1454. }
  1455. }
  1456. void generic_start_io_acct(int rw, unsigned long sectors,
  1457. struct hd_struct *part)
  1458. {
  1459. int cpu = part_stat_lock();
  1460. part_round_stats(cpu, part);
  1461. part_stat_inc(cpu, part, ios[rw]);
  1462. part_stat_add(cpu, part, sectors[rw], sectors);
  1463. part_inc_in_flight(part, rw);
  1464. part_stat_unlock();
  1465. }
  1466. EXPORT_SYMBOL(generic_start_io_acct);
  1467. void generic_end_io_acct(int rw, struct hd_struct *part,
  1468. unsigned long start_time)
  1469. {
  1470. unsigned long duration = jiffies - start_time;
  1471. int cpu = part_stat_lock();
  1472. part_stat_add(cpu, part, ticks[rw], duration);
  1473. part_round_stats(cpu, part);
  1474. part_dec_in_flight(part, rw);
  1475. part_stat_unlock();
  1476. }
  1477. EXPORT_SYMBOL(generic_end_io_acct);
  1478. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  1479. void bio_flush_dcache_pages(struct bio *bi)
  1480. {
  1481. struct bio_vec bvec;
  1482. struct bvec_iter iter;
  1483. bio_for_each_segment(bvec, bi, iter)
  1484. flush_dcache_page(bvec.bv_page);
  1485. }
  1486. EXPORT_SYMBOL(bio_flush_dcache_pages);
  1487. #endif
  1488. static inline bool bio_remaining_done(struct bio *bio)
  1489. {
  1490. /*
  1491. * If we're not chaining, then ->__bi_remaining is always 1 and
  1492. * we always end io on the first invocation.
  1493. */
  1494. if (!bio_flagged(bio, BIO_CHAIN))
  1495. return true;
  1496. BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
  1497. if (atomic_dec_and_test(&bio->__bi_remaining)) {
  1498. bio_clear_flag(bio, BIO_CHAIN);
  1499. return true;
  1500. }
  1501. return false;
  1502. }
  1503. /**
  1504. * bio_endio - end I/O on a bio
  1505. * @bio: bio
  1506. *
  1507. * Description:
  1508. * bio_endio() will end I/O on the whole bio. bio_endio() is the preferred
  1509. * way to end I/O on a bio. No one should call bi_end_io() directly on a
  1510. * bio unless they own it and thus know that it has an end_io function.
  1511. **/
  1512. void bio_endio(struct bio *bio)
  1513. {
  1514. again:
  1515. if (!bio_remaining_done(bio))
  1516. return;
  1517. /*
  1518. * Need to have a real endio function for chained bios, otherwise
  1519. * various corner cases will break (like stacking block devices that
  1520. * save/restore bi_end_io) - however, we want to avoid unbounded
  1521. * recursion and blowing the stack. Tail call optimization would
  1522. * handle this, but compiling with frame pointers also disables
  1523. * gcc's sibling call optimization.
  1524. */
  1525. if (bio->bi_end_io == bio_chain_endio) {
  1526. bio = __bio_chain_endio(bio);
  1527. goto again;
  1528. }
  1529. if (bio->bi_end_io)
  1530. bio->bi_end_io(bio);
  1531. }
  1532. EXPORT_SYMBOL(bio_endio);
  1533. /**
  1534. * bio_split - split a bio
  1535. * @bio: bio to split
  1536. * @sectors: number of sectors to split from the front of @bio
  1537. * @gfp: gfp mask
  1538. * @bs: bio set to allocate from
  1539. *
  1540. * Allocates and returns a new bio which represents @sectors from the start of
  1541. * @bio, and updates @bio to represent the remaining sectors.
  1542. *
  1543. * Unless this is a discard request the newly allocated bio will point
  1544. * to @bio's bi_io_vec; it is the caller's responsibility to ensure that
  1545. * @bio is not freed before the split.
  1546. */
  1547. struct bio *bio_split(struct bio *bio, int sectors,
  1548. gfp_t gfp, struct bio_set *bs)
  1549. {
  1550. struct bio *split = NULL;
  1551. BUG_ON(sectors <= 0);
  1552. BUG_ON(sectors >= bio_sectors(bio));
  1553. /*
  1554. * Discards need a mutable bio_vec to accommodate the payload
  1555. * required by the DSM TRIM and UNMAP commands.
  1556. */
  1557. if (bio_op(bio) == REQ_OP_DISCARD || bio_op(bio) == REQ_OP_SECURE_ERASE)
  1558. split = bio_clone_bioset(bio, gfp, bs);
  1559. else
  1560. split = bio_clone_fast(bio, gfp, bs);
  1561. if (!split)
  1562. return NULL;
  1563. split->bi_iter.bi_size = sectors << 9;
  1564. if (bio_integrity(split))
  1565. bio_integrity_trim(split, 0, sectors);
  1566. bio_advance(bio, split->bi_iter.bi_size);
  1567. return split;
  1568. }
  1569. EXPORT_SYMBOL(bio_split);
  1570. /**
  1571. * bio_trim - trim a bio
  1572. * @bio: bio to trim
  1573. * @offset: number of sectors to trim from the front of @bio
  1574. * @size: size we want to trim @bio to, in sectors
  1575. */
  1576. void bio_trim(struct bio *bio, int offset, int size)
  1577. {
  1578. /* 'bio' is a cloned bio which we need to trim to match
  1579. * the given offset and size.
  1580. */
  1581. size <<= 9;
  1582. if (offset == 0 && size == bio->bi_iter.bi_size)
  1583. return;
  1584. bio_clear_flag(bio, BIO_SEG_VALID);
  1585. bio_advance(bio, offset << 9);
  1586. bio->bi_iter.bi_size = size;
  1587. }
  1588. EXPORT_SYMBOL_GPL(bio_trim);
  1589. /*
  1590. * create memory pools for biovec's in a bio_set.
  1591. * use the global biovec slabs created for general use.
  1592. */
  1593. mempool_t *biovec_create_pool(int pool_entries)
  1594. {
  1595. struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX;
  1596. return mempool_create_slab_pool(pool_entries, bp->slab);
  1597. }
  1598. void bioset_free(struct bio_set *bs)
  1599. {
  1600. if (bs->rescue_workqueue)
  1601. destroy_workqueue(bs->rescue_workqueue);
  1602. if (bs->bio_pool)
  1603. mempool_destroy(bs->bio_pool);
  1604. if (bs->bvec_pool)
  1605. mempool_destroy(bs->bvec_pool);
  1606. bioset_integrity_free(bs);
  1607. bio_put_slab(bs);
  1608. kfree(bs);
  1609. }
  1610. EXPORT_SYMBOL(bioset_free);
  1611. static struct bio_set *__bioset_create(unsigned int pool_size,
  1612. unsigned int front_pad,
  1613. bool create_bvec_pool)
  1614. {
  1615. unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
  1616. struct bio_set *bs;
  1617. bs = kzalloc(sizeof(*bs), GFP_KERNEL);
  1618. if (!bs)
  1619. return NULL;
  1620. bs->front_pad = front_pad;
  1621. spin_lock_init(&bs->rescue_lock);
  1622. bio_list_init(&bs->rescue_list);
  1623. INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
  1624. bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
  1625. if (!bs->bio_slab) {
  1626. kfree(bs);
  1627. return NULL;
  1628. }
  1629. bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
  1630. if (!bs->bio_pool)
  1631. goto bad;
  1632. if (create_bvec_pool) {
  1633. bs->bvec_pool = biovec_create_pool(pool_size);
  1634. if (!bs->bvec_pool)
  1635. goto bad;
  1636. }
  1637. bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
  1638. if (!bs->rescue_workqueue)
  1639. goto bad;
  1640. return bs;
  1641. bad:
  1642. bioset_free(bs);
  1643. return NULL;
  1644. }
  1645. /**
  1646. * bioset_create - Create a bio_set
  1647. * @pool_size: Number of bio and bio_vecs to cache in the mempool
  1648. * @front_pad: Number of bytes to allocate in front of the returned bio
  1649. *
  1650. * Description:
  1651. * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
  1652. * to ask for a number of bytes to be allocated in front of the bio.
  1653. * Front pad allocation is useful for embedding the bio inside
  1654. * another structure, to avoid allocating extra data to go with the bio.
  1655. * Note that the bio must be embedded at the END of that structure always,
  1656. * or things will break badly.
  1657. */
  1658. struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
  1659. {
  1660. return __bioset_create(pool_size, front_pad, true);
  1661. }
  1662. EXPORT_SYMBOL(bioset_create);
  1663. /**
  1664. * bioset_create_nobvec - Create a bio_set without bio_vec mempool
  1665. * @pool_size: Number of bio to cache in the mempool
  1666. * @front_pad: Number of bytes to allocate in front of the returned bio
  1667. *
  1668. * Description:
  1669. * Same functionality as bioset_create() except that mempool is not
  1670. * created for bio_vecs. Saving some memory for bio_clone_fast() users.
  1671. */
  1672. struct bio_set *bioset_create_nobvec(unsigned int pool_size, unsigned int front_pad)
  1673. {
  1674. return __bioset_create(pool_size, front_pad, false);
  1675. }
  1676. EXPORT_SYMBOL(bioset_create_nobvec);
  1677. #ifdef CONFIG_BLK_CGROUP
  1678. /**
  1679. * bio_associate_blkcg - associate a bio with the specified blkcg
  1680. * @bio: target bio
  1681. * @blkcg_css: css of the blkcg to associate
  1682. *
  1683. * Associate @bio with the blkcg specified by @blkcg_css. Block layer will
  1684. * treat @bio as if it were issued by a task which belongs to the blkcg.
  1685. *
  1686. * This function takes an extra reference of @blkcg_css which will be put
  1687. * when @bio is released. The caller must own @bio and is responsible for
  1688. * synchronizing calls to this function.
  1689. */
  1690. int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css)
  1691. {
  1692. if (unlikely(bio->bi_css))
  1693. return -EBUSY;
  1694. css_get(blkcg_css);
  1695. bio->bi_css = blkcg_css;
  1696. return 0;
  1697. }
  1698. EXPORT_SYMBOL_GPL(bio_associate_blkcg);
  1699. /**
  1700. * bio_associate_current - associate a bio with %current
  1701. * @bio: target bio
  1702. *
  1703. * Associate @bio with %current if it hasn't been associated yet. Block
  1704. * layer will treat @bio as if it were issued by %current no matter which
  1705. * task actually issues it.
  1706. *
  1707. * This function takes an extra reference of @task's io_context and blkcg
  1708. * which will be put when @bio is released. The caller must own @bio,
  1709. * ensure %current->io_context exists, and is responsible for synchronizing
  1710. * calls to this function.
  1711. */
  1712. int bio_associate_current(struct bio *bio)
  1713. {
  1714. struct io_context *ioc;
  1715. if (bio->bi_css)
  1716. return -EBUSY;
  1717. ioc = current->io_context;
  1718. if (!ioc)
  1719. return -ENOENT;
  1720. get_io_context_active(ioc);
  1721. bio->bi_ioc = ioc;
  1722. bio->bi_css = task_get_css(current, io_cgrp_id);
  1723. return 0;
  1724. }
  1725. EXPORT_SYMBOL_GPL(bio_associate_current);
  1726. /**
  1727. * bio_disassociate_task - undo bio_associate_current()
  1728. * @bio: target bio
  1729. */
  1730. void bio_disassociate_task(struct bio *bio)
  1731. {
  1732. if (bio->bi_ioc) {
  1733. put_io_context(bio->bi_ioc);
  1734. bio->bi_ioc = NULL;
  1735. }
  1736. if (bio->bi_css) {
  1737. css_put(bio->bi_css);
  1738. bio->bi_css = NULL;
  1739. }
  1740. }
  1741. /**
  1742. * bio_clone_blkcg_association - clone blkcg association from src to dst bio
  1743. * @dst: destination bio
  1744. * @src: source bio
  1745. */
  1746. void bio_clone_blkcg_association(struct bio *dst, struct bio *src)
  1747. {
  1748. if (src->bi_css)
  1749. WARN_ON(bio_associate_blkcg(dst, src->bi_css));
  1750. }
  1751. #endif /* CONFIG_BLK_CGROUP */
  1752. static void __init biovec_init_slabs(void)
  1753. {
  1754. int i;
  1755. for (i = 0; i < BVEC_POOL_NR; i++) {
  1756. int size;
  1757. struct biovec_slab *bvs = bvec_slabs + i;
  1758. if (bvs->nr_vecs <= BIO_INLINE_VECS) {
  1759. bvs->slab = NULL;
  1760. continue;
  1761. }
  1762. size = bvs->nr_vecs * sizeof(struct bio_vec);
  1763. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  1764. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1765. }
  1766. }
  1767. static int __init init_bio(void)
  1768. {
  1769. bio_slab_max = 2;
  1770. bio_slab_nr = 0;
  1771. bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
  1772. if (!bio_slabs)
  1773. panic("bio: can't allocate bios\n");
  1774. bio_integrity_init();
  1775. biovec_init_slabs();
  1776. fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
  1777. if (!fs_bio_set)
  1778. panic("bio: can't allocate bios\n");
  1779. if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE))
  1780. panic("bio: can't create integrity pool\n");
  1781. return 0;
  1782. }
  1783. subsys_initcall(init_bio);