bio.c 54 KB

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