bio.c 50 KB

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