bio.c 49 KB

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