bio.c 49 KB

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