bio.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  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/iocontext.h>
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/export.h>
  27. #include <linux/mempool.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/cgroup.h>
  30. #include <scsi/sg.h> /* for struct sg_iovec */
  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. static mempool_t *bio_split_pool __read_mostly;
  38. /*
  39. * if you change this list, also change bvec_alloc or things will
  40. * break badly! cannot be bigger than what you can fit into an
  41. * unsigned short
  42. */
  43. #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
  44. static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
  45. BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
  46. };
  47. #undef BV
  48. /*
  49. * fs_bio_set is the bio_set containing bio and iovec memory pools used by
  50. * IO code that does not need private memory pools.
  51. */
  52. struct bio_set *fs_bio_set;
  53. EXPORT_SYMBOL(fs_bio_set);
  54. /*
  55. * Our slab pool management
  56. */
  57. struct bio_slab {
  58. struct kmem_cache *slab;
  59. unsigned int slab_ref;
  60. unsigned int slab_size;
  61. char name[8];
  62. };
  63. static DEFINE_MUTEX(bio_slab_lock);
  64. static struct bio_slab *bio_slabs;
  65. static unsigned int bio_slab_nr, bio_slab_max;
  66. static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
  67. {
  68. unsigned int sz = sizeof(struct bio) + extra_size;
  69. struct kmem_cache *slab = NULL;
  70. struct bio_slab *bslab, *new_bio_slabs;
  71. unsigned int new_bio_slab_max;
  72. unsigned int i, entry = -1;
  73. mutex_lock(&bio_slab_lock);
  74. i = 0;
  75. while (i < bio_slab_nr) {
  76. bslab = &bio_slabs[i];
  77. if (!bslab->slab && entry == -1)
  78. entry = i;
  79. else if (bslab->slab_size == sz) {
  80. slab = bslab->slab;
  81. bslab->slab_ref++;
  82. break;
  83. }
  84. i++;
  85. }
  86. if (slab)
  87. goto out_unlock;
  88. if (bio_slab_nr == bio_slab_max && entry == -1) {
  89. new_bio_slab_max = bio_slab_max << 1;
  90. new_bio_slabs = krealloc(bio_slabs,
  91. new_bio_slab_max * sizeof(struct bio_slab),
  92. GFP_KERNEL);
  93. if (!new_bio_slabs)
  94. goto out_unlock;
  95. bio_slab_max = new_bio_slab_max;
  96. bio_slabs = new_bio_slabs;
  97. }
  98. if (entry == -1)
  99. entry = bio_slab_nr++;
  100. bslab = &bio_slabs[entry];
  101. snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
  102. slab = kmem_cache_create(bslab->name, sz, 0, SLAB_HWCACHE_ALIGN, NULL);
  103. if (!slab)
  104. goto out_unlock;
  105. printk(KERN_INFO "bio: create slab <%s> at %d\n", bslab->name, entry);
  106. bslab->slab = slab;
  107. bslab->slab_ref = 1;
  108. bslab->slab_size = sz;
  109. out_unlock:
  110. mutex_unlock(&bio_slab_lock);
  111. return slab;
  112. }
  113. static void bio_put_slab(struct bio_set *bs)
  114. {
  115. struct bio_slab *bslab = NULL;
  116. unsigned int i;
  117. mutex_lock(&bio_slab_lock);
  118. for (i = 0; i < bio_slab_nr; i++) {
  119. if (bs->bio_slab == bio_slabs[i].slab) {
  120. bslab = &bio_slabs[i];
  121. break;
  122. }
  123. }
  124. if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
  125. goto out;
  126. WARN_ON(!bslab->slab_ref);
  127. if (--bslab->slab_ref)
  128. goto out;
  129. kmem_cache_destroy(bslab->slab);
  130. bslab->slab = NULL;
  131. out:
  132. mutex_unlock(&bio_slab_lock);
  133. }
  134. unsigned int bvec_nr_vecs(unsigned short idx)
  135. {
  136. return bvec_slabs[idx].nr_vecs;
  137. }
  138. void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
  139. {
  140. BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
  141. if (idx == BIOVEC_MAX_IDX)
  142. mempool_free(bv, pool);
  143. else {
  144. struct biovec_slab *bvs = bvec_slabs + idx;
  145. kmem_cache_free(bvs->slab, bv);
  146. }
  147. }
  148. struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
  149. mempool_t *pool)
  150. {
  151. struct bio_vec *bvl;
  152. /*
  153. * see comment near bvec_array define!
  154. */
  155. switch (nr) {
  156. case 1:
  157. *idx = 0;
  158. break;
  159. case 2 ... 4:
  160. *idx = 1;
  161. break;
  162. case 5 ... 16:
  163. *idx = 2;
  164. break;
  165. case 17 ... 64:
  166. *idx = 3;
  167. break;
  168. case 65 ... 128:
  169. *idx = 4;
  170. break;
  171. case 129 ... BIO_MAX_PAGES:
  172. *idx = 5;
  173. break;
  174. default:
  175. return NULL;
  176. }
  177. /*
  178. * idx now points to the pool we want to allocate from. only the
  179. * 1-vec entry pool is mempool backed.
  180. */
  181. if (*idx == BIOVEC_MAX_IDX) {
  182. fallback:
  183. bvl = mempool_alloc(pool, gfp_mask);
  184. } else {
  185. struct biovec_slab *bvs = bvec_slabs + *idx;
  186. gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
  187. /*
  188. * Make this allocation restricted and don't dump info on
  189. * allocation failures, since we'll fallback to the mempool
  190. * in case of failure.
  191. */
  192. __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
  193. /*
  194. * Try a slab allocation. If this fails and __GFP_WAIT
  195. * is set, retry with the 1-entry mempool
  196. */
  197. bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
  198. if (unlikely(!bvl && (gfp_mask & __GFP_WAIT))) {
  199. *idx = BIOVEC_MAX_IDX;
  200. goto fallback;
  201. }
  202. }
  203. return bvl;
  204. }
  205. static void __bio_free(struct bio *bio)
  206. {
  207. bio_disassociate_task(bio);
  208. if (bio_integrity(bio))
  209. bio_integrity_free(bio);
  210. }
  211. static void bio_free(struct bio *bio)
  212. {
  213. struct bio_set *bs = bio->bi_pool;
  214. void *p;
  215. __bio_free(bio);
  216. if (bs) {
  217. if (bio_has_allocated_vec(bio))
  218. bvec_free(bs->bvec_pool, bio->bi_io_vec, BIO_POOL_IDX(bio));
  219. /*
  220. * If we have front padding, adjust the bio pointer before freeing
  221. */
  222. p = bio;
  223. p -= bs->front_pad;
  224. mempool_free(p, bs->bio_pool);
  225. } else {
  226. /* Bio was allocated by bio_kmalloc() */
  227. kfree(bio);
  228. }
  229. }
  230. void bio_init(struct bio *bio)
  231. {
  232. memset(bio, 0, sizeof(*bio));
  233. bio->bi_flags = 1 << BIO_UPTODATE;
  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. }
  254. EXPORT_SYMBOL(bio_reset);
  255. static void bio_alloc_rescue(struct work_struct *work)
  256. {
  257. struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
  258. struct bio *bio;
  259. while (1) {
  260. spin_lock(&bs->rescue_lock);
  261. bio = bio_list_pop(&bs->rescue_list);
  262. spin_unlock(&bs->rescue_lock);
  263. if (!bio)
  264. break;
  265. generic_make_request(bio);
  266. }
  267. }
  268. static void punt_bios_to_rescuer(struct bio_set *bs)
  269. {
  270. struct bio_list punt, nopunt;
  271. struct bio *bio;
  272. /*
  273. * In order to guarantee forward progress we must punt only bios that
  274. * were allocated from this bio_set; otherwise, if there was a bio on
  275. * there for a stacking driver higher up in the stack, processing it
  276. * could require allocating bios from this bio_set, and doing that from
  277. * our own rescuer would be bad.
  278. *
  279. * Since bio lists are singly linked, pop them all instead of trying to
  280. * remove from the middle of the list:
  281. */
  282. bio_list_init(&punt);
  283. bio_list_init(&nopunt);
  284. while ((bio = bio_list_pop(current->bio_list)))
  285. bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
  286. *current->bio_list = nopunt;
  287. spin_lock(&bs->rescue_lock);
  288. bio_list_merge(&bs->rescue_list, &punt);
  289. spin_unlock(&bs->rescue_lock);
  290. queue_work(bs->rescue_workqueue, &bs->rescue_work);
  291. }
  292. /**
  293. * bio_alloc_bioset - allocate a bio for I/O
  294. * @gfp_mask: the GFP_ mask given to the slab allocator
  295. * @nr_iovecs: number of iovecs to pre-allocate
  296. * @bs: the bio_set to allocate from.
  297. *
  298. * Description:
  299. * If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
  300. * backed by the @bs's mempool.
  301. *
  302. * When @bs is not NULL, if %__GFP_WAIT is set then bio_alloc will always be
  303. * able to allocate a bio. This is due to the mempool guarantees. To make this
  304. * work, callers must never allocate more than 1 bio at a time from this pool.
  305. * Callers that need to allocate more than 1 bio must always submit the
  306. * previously allocated bio for IO before attempting to allocate a new one.
  307. * Failure to do so can cause deadlocks under memory pressure.
  308. *
  309. * Note that when running under generic_make_request() (i.e. any block
  310. * driver), bios are not submitted until after you return - see the code in
  311. * generic_make_request() that converts recursion into iteration, to prevent
  312. * stack overflows.
  313. *
  314. * This would normally mean allocating multiple bios under
  315. * generic_make_request() would be susceptible to deadlocks, but we have
  316. * deadlock avoidance code that resubmits any blocked bios from a rescuer
  317. * thread.
  318. *
  319. * However, we do not guarantee forward progress for allocations from other
  320. * mempools. Doing multiple allocations from the same mempool under
  321. * generic_make_request() should be avoided - instead, use bio_set's front_pad
  322. * for per bio allocations.
  323. *
  324. * RETURNS:
  325. * Pointer to new bio on success, NULL on failure.
  326. */
  327. struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
  328. {
  329. gfp_t saved_gfp = gfp_mask;
  330. unsigned front_pad;
  331. unsigned inline_vecs;
  332. unsigned long idx = BIO_POOL_NONE;
  333. struct bio_vec *bvl = NULL;
  334. struct bio *bio;
  335. void *p;
  336. if (!bs) {
  337. if (nr_iovecs > UIO_MAXIOV)
  338. return NULL;
  339. p = kmalloc(sizeof(struct bio) +
  340. nr_iovecs * sizeof(struct bio_vec),
  341. gfp_mask);
  342. front_pad = 0;
  343. inline_vecs = nr_iovecs;
  344. } else {
  345. /*
  346. * generic_make_request() converts recursion to iteration; this
  347. * means if we're running beneath it, any bios we allocate and
  348. * submit will not be submitted (and thus freed) until after we
  349. * return.
  350. *
  351. * This exposes us to a potential deadlock if we allocate
  352. * multiple bios from the same bio_set() while running
  353. * underneath generic_make_request(). If we were to allocate
  354. * multiple bios (say a stacking block driver that was splitting
  355. * bios), we would deadlock if we exhausted the mempool's
  356. * reserve.
  357. *
  358. * We solve this, and guarantee forward progress, with a rescuer
  359. * workqueue per bio_set. If we go to allocate and there are
  360. * bios on current->bio_list, we first try the allocation
  361. * without __GFP_WAIT; if that fails, we punt those bios we
  362. * would be blocking to the rescuer workqueue before we retry
  363. * with the original gfp_flags.
  364. */
  365. if (current->bio_list && !bio_list_empty(current->bio_list))
  366. gfp_mask &= ~__GFP_WAIT;
  367. p = mempool_alloc(bs->bio_pool, gfp_mask);
  368. if (!p && gfp_mask != saved_gfp) {
  369. punt_bios_to_rescuer(bs);
  370. gfp_mask = saved_gfp;
  371. p = mempool_alloc(bs->bio_pool, gfp_mask);
  372. }
  373. front_pad = bs->front_pad;
  374. inline_vecs = BIO_INLINE_VECS;
  375. }
  376. if (unlikely(!p))
  377. return NULL;
  378. bio = p + front_pad;
  379. bio_init(bio);
  380. if (nr_iovecs > inline_vecs) {
  381. bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
  382. if (!bvl && gfp_mask != saved_gfp) {
  383. punt_bios_to_rescuer(bs);
  384. gfp_mask = saved_gfp;
  385. bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
  386. }
  387. if (unlikely(!bvl))
  388. goto err_free;
  389. } else if (nr_iovecs) {
  390. bvl = bio->bi_inline_vecs;
  391. }
  392. bio->bi_pool = bs;
  393. bio->bi_flags |= idx << BIO_POOL_OFFSET;
  394. bio->bi_max_vecs = nr_iovecs;
  395. bio->bi_io_vec = bvl;
  396. return bio;
  397. err_free:
  398. mempool_free(p, bs->bio_pool);
  399. return NULL;
  400. }
  401. EXPORT_SYMBOL(bio_alloc_bioset);
  402. void zero_fill_bio(struct bio *bio)
  403. {
  404. unsigned long flags;
  405. struct bio_vec *bv;
  406. int i;
  407. bio_for_each_segment(bv, bio, i) {
  408. char *data = bvec_kmap_irq(bv, &flags);
  409. memset(data, 0, bv->bv_len);
  410. flush_dcache_page(bv->bv_page);
  411. bvec_kunmap_irq(data, &flags);
  412. }
  413. }
  414. EXPORT_SYMBOL(zero_fill_bio);
  415. /**
  416. * bio_put - release a reference to a bio
  417. * @bio: bio to release reference to
  418. *
  419. * Description:
  420. * Put a reference to a &struct bio, either one you have gotten with
  421. * bio_alloc, bio_get or bio_clone. The last put of a bio will free it.
  422. **/
  423. void bio_put(struct bio *bio)
  424. {
  425. BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
  426. /*
  427. * last put frees it
  428. */
  429. if (atomic_dec_and_test(&bio->bi_cnt))
  430. bio_free(bio);
  431. }
  432. EXPORT_SYMBOL(bio_put);
  433. inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
  434. {
  435. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  436. blk_recount_segments(q, bio);
  437. return bio->bi_phys_segments;
  438. }
  439. EXPORT_SYMBOL(bio_phys_segments);
  440. /**
  441. * __bio_clone - clone a bio
  442. * @bio: destination bio
  443. * @bio_src: bio to clone
  444. *
  445. * Clone a &bio. Caller will own the returned bio, but not
  446. * the actual data it points to. Reference count of returned
  447. * bio will be one.
  448. */
  449. void __bio_clone(struct bio *bio, struct bio *bio_src)
  450. {
  451. memcpy(bio->bi_io_vec, bio_src->bi_io_vec,
  452. bio_src->bi_max_vecs * sizeof(struct bio_vec));
  453. /*
  454. * most users will be overriding ->bi_bdev with a new target,
  455. * so we don't set nor calculate new physical/hw segment counts here
  456. */
  457. bio->bi_sector = bio_src->bi_sector;
  458. bio->bi_bdev = bio_src->bi_bdev;
  459. bio->bi_flags |= 1 << BIO_CLONED;
  460. bio->bi_rw = bio_src->bi_rw;
  461. bio->bi_vcnt = bio_src->bi_vcnt;
  462. bio->bi_size = bio_src->bi_size;
  463. bio->bi_idx = bio_src->bi_idx;
  464. }
  465. EXPORT_SYMBOL(__bio_clone);
  466. /**
  467. * bio_clone_bioset - clone a bio
  468. * @bio: bio to clone
  469. * @gfp_mask: allocation priority
  470. * @bs: bio_set to allocate from
  471. *
  472. * Like __bio_clone, only also allocates the returned bio
  473. */
  474. struct bio *bio_clone_bioset(struct bio *bio, gfp_t gfp_mask,
  475. struct bio_set *bs)
  476. {
  477. struct bio *b;
  478. b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, bs);
  479. if (!b)
  480. return NULL;
  481. __bio_clone(b, bio);
  482. if (bio_integrity(bio)) {
  483. int ret;
  484. ret = bio_integrity_clone(b, bio, gfp_mask);
  485. if (ret < 0) {
  486. bio_put(b);
  487. return NULL;
  488. }
  489. }
  490. return b;
  491. }
  492. EXPORT_SYMBOL(bio_clone_bioset);
  493. /**
  494. * bio_get_nr_vecs - return approx number of vecs
  495. * @bdev: I/O target
  496. *
  497. * Return the approximate number of pages we can send to this target.
  498. * There's no guarantee that you will be able to fit this number of pages
  499. * into a bio, it does not account for dynamic restrictions that vary
  500. * on offset.
  501. */
  502. int bio_get_nr_vecs(struct block_device *bdev)
  503. {
  504. struct request_queue *q = bdev_get_queue(bdev);
  505. int nr_pages;
  506. nr_pages = min_t(unsigned,
  507. queue_max_segments(q),
  508. queue_max_sectors(q) / (PAGE_SIZE >> 9) + 1);
  509. return min_t(unsigned, nr_pages, BIO_MAX_PAGES);
  510. }
  511. EXPORT_SYMBOL(bio_get_nr_vecs);
  512. static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
  513. *page, unsigned int len, unsigned int offset,
  514. unsigned short max_sectors)
  515. {
  516. int retried_segments = 0;
  517. struct bio_vec *bvec;
  518. /*
  519. * cloned bio must not modify vec list
  520. */
  521. if (unlikely(bio_flagged(bio, BIO_CLONED)))
  522. return 0;
  523. if (((bio->bi_size + len) >> 9) > max_sectors)
  524. return 0;
  525. /*
  526. * For filesystems with a blocksize smaller than the pagesize
  527. * we will often be called with the same page as last time and
  528. * a consecutive offset. Optimize this special case.
  529. */
  530. if (bio->bi_vcnt > 0) {
  531. struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  532. if (page == prev->bv_page &&
  533. offset == prev->bv_offset + prev->bv_len) {
  534. unsigned int prev_bv_len = prev->bv_len;
  535. prev->bv_len += len;
  536. if (q->merge_bvec_fn) {
  537. struct bvec_merge_data bvm = {
  538. /* prev_bvec is already charged in
  539. bi_size, discharge it in order to
  540. simulate merging updated prev_bvec
  541. as new bvec. */
  542. .bi_bdev = bio->bi_bdev,
  543. .bi_sector = bio->bi_sector,
  544. .bi_size = bio->bi_size - prev_bv_len,
  545. .bi_rw = bio->bi_rw,
  546. };
  547. if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) {
  548. prev->bv_len -= len;
  549. return 0;
  550. }
  551. }
  552. goto done;
  553. }
  554. }
  555. if (bio->bi_vcnt >= bio->bi_max_vecs)
  556. return 0;
  557. /*
  558. * we might lose a segment or two here, but rather that than
  559. * make this too complex.
  560. */
  561. while (bio->bi_phys_segments >= queue_max_segments(q)) {
  562. if (retried_segments)
  563. return 0;
  564. retried_segments = 1;
  565. blk_recount_segments(q, bio);
  566. }
  567. /*
  568. * setup the new entry, we might clear it again later if we
  569. * cannot add the page
  570. */
  571. bvec = &bio->bi_io_vec[bio->bi_vcnt];
  572. bvec->bv_page = page;
  573. bvec->bv_len = len;
  574. bvec->bv_offset = offset;
  575. /*
  576. * if queue has other restrictions (eg varying max sector size
  577. * depending on offset), it can specify a merge_bvec_fn in the
  578. * queue to get further control
  579. */
  580. if (q->merge_bvec_fn) {
  581. struct bvec_merge_data bvm = {
  582. .bi_bdev = bio->bi_bdev,
  583. .bi_sector = bio->bi_sector,
  584. .bi_size = bio->bi_size,
  585. .bi_rw = bio->bi_rw,
  586. };
  587. /*
  588. * merge_bvec_fn() returns number of bytes it can accept
  589. * at this offset
  590. */
  591. if (q->merge_bvec_fn(q, &bvm, bvec) < bvec->bv_len) {
  592. bvec->bv_page = NULL;
  593. bvec->bv_len = 0;
  594. bvec->bv_offset = 0;
  595. return 0;
  596. }
  597. }
  598. /* If we may be able to merge these biovecs, force a recount */
  599. if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
  600. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  601. bio->bi_vcnt++;
  602. bio->bi_phys_segments++;
  603. done:
  604. bio->bi_size += len;
  605. return len;
  606. }
  607. /**
  608. * bio_add_pc_page - attempt to add page to bio
  609. * @q: the target queue
  610. * @bio: destination bio
  611. * @page: page to add
  612. * @len: vec entry length
  613. * @offset: vec entry offset
  614. *
  615. * Attempt to add a page to the bio_vec maplist. This can fail for a
  616. * number of reasons, such as the bio being full or target block device
  617. * limitations. The target block device must allow bio's up to PAGE_SIZE,
  618. * so it is always possible to add a single page to an empty bio.
  619. *
  620. * This should only be used by REQ_PC bios.
  621. */
  622. int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
  623. unsigned int len, unsigned int offset)
  624. {
  625. return __bio_add_page(q, bio, page, len, offset,
  626. queue_max_hw_sectors(q));
  627. }
  628. EXPORT_SYMBOL(bio_add_pc_page);
  629. /**
  630. * bio_add_page - attempt to add page to bio
  631. * @bio: destination bio
  632. * @page: page to add
  633. * @len: vec entry length
  634. * @offset: vec entry offset
  635. *
  636. * Attempt to add a page to the bio_vec maplist. This can fail for a
  637. * number of reasons, such as the bio being full or target block device
  638. * limitations. The target block device must allow bio's up to PAGE_SIZE,
  639. * so it is always possible to add a single page to an empty bio.
  640. */
  641. int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
  642. unsigned int offset)
  643. {
  644. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  645. return __bio_add_page(q, bio, page, len, offset, queue_max_sectors(q));
  646. }
  647. EXPORT_SYMBOL(bio_add_page);
  648. struct submit_bio_ret {
  649. struct completion event;
  650. int error;
  651. };
  652. static void submit_bio_wait_endio(struct bio *bio, int error)
  653. {
  654. struct submit_bio_ret *ret = bio->bi_private;
  655. ret->error = error;
  656. complete(&ret->event);
  657. }
  658. /**
  659. * submit_bio_wait - submit a bio, and wait until it completes
  660. * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  661. * @bio: The &struct bio which describes the I/O
  662. *
  663. * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
  664. * bio_endio() on failure.
  665. */
  666. int submit_bio_wait(int rw, struct bio *bio)
  667. {
  668. struct submit_bio_ret ret;
  669. rw |= REQ_SYNC;
  670. init_completion(&ret.event);
  671. bio->bi_private = &ret;
  672. bio->bi_end_io = submit_bio_wait_endio;
  673. submit_bio(rw, bio);
  674. wait_for_completion(&ret.event);
  675. return ret.error;
  676. }
  677. EXPORT_SYMBOL(submit_bio_wait);
  678. /**
  679. * bio_advance - increment/complete a bio by some number of bytes
  680. * @bio: bio to advance
  681. * @bytes: number of bytes to complete
  682. *
  683. * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
  684. * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
  685. * be updated on the last bvec as well.
  686. *
  687. * @bio will then represent the remaining, uncompleted portion of the io.
  688. */
  689. void bio_advance(struct bio *bio, unsigned bytes)
  690. {
  691. if (bio_integrity(bio))
  692. bio_integrity_advance(bio, bytes);
  693. bio->bi_sector += bytes >> 9;
  694. bio->bi_size -= bytes;
  695. if (bio->bi_rw & BIO_NO_ADVANCE_ITER_MASK)
  696. return;
  697. while (bytes) {
  698. if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
  699. WARN_ONCE(1, "bio idx %d >= vcnt %d\n",
  700. bio->bi_idx, bio->bi_vcnt);
  701. break;
  702. }
  703. if (bytes >= bio_iovec(bio)->bv_len) {
  704. bytes -= bio_iovec(bio)->bv_len;
  705. bio->bi_idx++;
  706. } else {
  707. bio_iovec(bio)->bv_len -= bytes;
  708. bio_iovec(bio)->bv_offset += bytes;
  709. bytes = 0;
  710. }
  711. }
  712. }
  713. EXPORT_SYMBOL(bio_advance);
  714. /**
  715. * bio_copy_data - copy contents of data buffers from one chain of bios to
  716. * another
  717. * @src: source bio list
  718. * @dst: destination bio list
  719. *
  720. * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats
  721. * @src and @dst as linked lists of bios.
  722. *
  723. * Stops when it reaches the end of either @src or @dst - that is, copies
  724. * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
  725. */
  726. void bio_copy_data(struct bio *dst, struct bio *src)
  727. {
  728. struct bio_vec *src_bv, *dst_bv;
  729. unsigned src_offset, dst_offset, bytes;
  730. void *src_p, *dst_p;
  731. src_bv = bio_iovec(src);
  732. dst_bv = bio_iovec(dst);
  733. src_offset = src_bv->bv_offset;
  734. dst_offset = dst_bv->bv_offset;
  735. while (1) {
  736. if (src_offset == src_bv->bv_offset + src_bv->bv_len) {
  737. src_bv++;
  738. if (src_bv == bio_iovec_idx(src, src->bi_vcnt)) {
  739. src = src->bi_next;
  740. if (!src)
  741. break;
  742. src_bv = bio_iovec(src);
  743. }
  744. src_offset = src_bv->bv_offset;
  745. }
  746. if (dst_offset == dst_bv->bv_offset + dst_bv->bv_len) {
  747. dst_bv++;
  748. if (dst_bv == bio_iovec_idx(dst, dst->bi_vcnt)) {
  749. dst = dst->bi_next;
  750. if (!dst)
  751. break;
  752. dst_bv = bio_iovec(dst);
  753. }
  754. dst_offset = dst_bv->bv_offset;
  755. }
  756. bytes = min(dst_bv->bv_offset + dst_bv->bv_len - dst_offset,
  757. src_bv->bv_offset + src_bv->bv_len - src_offset);
  758. src_p = kmap_atomic(src_bv->bv_page);
  759. dst_p = kmap_atomic(dst_bv->bv_page);
  760. memcpy(dst_p + dst_bv->bv_offset,
  761. src_p + src_bv->bv_offset,
  762. bytes);
  763. kunmap_atomic(dst_p);
  764. kunmap_atomic(src_p);
  765. src_offset += bytes;
  766. dst_offset += bytes;
  767. }
  768. }
  769. EXPORT_SYMBOL(bio_copy_data);
  770. struct bio_map_data {
  771. struct bio_vec *iovecs;
  772. struct sg_iovec *sgvecs;
  773. int nr_sgvecs;
  774. int is_our_pages;
  775. };
  776. static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio,
  777. struct sg_iovec *iov, int iov_count,
  778. int is_our_pages)
  779. {
  780. memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
  781. memcpy(bmd->sgvecs, iov, sizeof(struct sg_iovec) * iov_count);
  782. bmd->nr_sgvecs = iov_count;
  783. bmd->is_our_pages = is_our_pages;
  784. bio->bi_private = bmd;
  785. }
  786. static void bio_free_map_data(struct bio_map_data *bmd)
  787. {
  788. kfree(bmd->iovecs);
  789. kfree(bmd->sgvecs);
  790. kfree(bmd);
  791. }
  792. static struct bio_map_data *bio_alloc_map_data(int nr_segs,
  793. unsigned int iov_count,
  794. gfp_t gfp_mask)
  795. {
  796. struct bio_map_data *bmd;
  797. if (iov_count > UIO_MAXIOV)
  798. return NULL;
  799. bmd = kmalloc(sizeof(*bmd), gfp_mask);
  800. if (!bmd)
  801. return NULL;
  802. bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, gfp_mask);
  803. if (!bmd->iovecs) {
  804. kfree(bmd);
  805. return NULL;
  806. }
  807. bmd->sgvecs = kmalloc(sizeof(struct sg_iovec) * iov_count, gfp_mask);
  808. if (bmd->sgvecs)
  809. return bmd;
  810. kfree(bmd->iovecs);
  811. kfree(bmd);
  812. return NULL;
  813. }
  814. static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
  815. struct sg_iovec *iov, int iov_count,
  816. int to_user, int from_user, int do_free_page)
  817. {
  818. int ret = 0, i;
  819. struct bio_vec *bvec;
  820. int iov_idx = 0;
  821. unsigned int iov_off = 0;
  822. bio_for_each_segment_all(bvec, bio, i) {
  823. char *bv_addr = page_address(bvec->bv_page);
  824. unsigned int bv_len = iovecs[i].bv_len;
  825. while (bv_len && iov_idx < iov_count) {
  826. unsigned int bytes;
  827. char __user *iov_addr;
  828. bytes = min_t(unsigned int,
  829. iov[iov_idx].iov_len - iov_off, bv_len);
  830. iov_addr = iov[iov_idx].iov_base + iov_off;
  831. if (!ret) {
  832. if (to_user)
  833. ret = copy_to_user(iov_addr, bv_addr,
  834. bytes);
  835. if (from_user)
  836. ret = copy_from_user(bv_addr, iov_addr,
  837. bytes);
  838. if (ret)
  839. ret = -EFAULT;
  840. }
  841. bv_len -= bytes;
  842. bv_addr += bytes;
  843. iov_addr += bytes;
  844. iov_off += bytes;
  845. if (iov[iov_idx].iov_len == iov_off) {
  846. iov_idx++;
  847. iov_off = 0;
  848. }
  849. }
  850. if (do_free_page)
  851. __free_page(bvec->bv_page);
  852. }
  853. return ret;
  854. }
  855. /**
  856. * bio_uncopy_user - finish previously mapped bio
  857. * @bio: bio being terminated
  858. *
  859. * Free pages allocated from bio_copy_user() and write back data
  860. * to user space in case of a read.
  861. */
  862. int bio_uncopy_user(struct bio *bio)
  863. {
  864. struct bio_map_data *bmd = bio->bi_private;
  865. int ret = 0;
  866. if (!bio_flagged(bio, BIO_NULL_MAPPED))
  867. ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs,
  868. bmd->nr_sgvecs, bio_data_dir(bio) == READ,
  869. 0, bmd->is_our_pages);
  870. bio_free_map_data(bmd);
  871. bio_put(bio);
  872. return ret;
  873. }
  874. EXPORT_SYMBOL(bio_uncopy_user);
  875. /**
  876. * bio_copy_user_iov - copy user data to bio
  877. * @q: destination block queue
  878. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  879. * @iov: the iovec.
  880. * @iov_count: number of elements in the iovec
  881. * @write_to_vm: bool indicating writing to pages or not
  882. * @gfp_mask: memory allocation flags
  883. *
  884. * Prepares and returns a bio for indirect user io, bouncing data
  885. * to/from kernel pages as necessary. Must be paired with
  886. * call bio_uncopy_user() on io completion.
  887. */
  888. struct bio *bio_copy_user_iov(struct request_queue *q,
  889. struct rq_map_data *map_data,
  890. struct sg_iovec *iov, int iov_count,
  891. int write_to_vm, gfp_t gfp_mask)
  892. {
  893. struct bio_map_data *bmd;
  894. struct bio_vec *bvec;
  895. struct page *page;
  896. struct bio *bio;
  897. int i, ret;
  898. int nr_pages = 0;
  899. unsigned int len = 0;
  900. unsigned int offset = map_data ? map_data->offset & ~PAGE_MASK : 0;
  901. for (i = 0; i < iov_count; i++) {
  902. unsigned long uaddr;
  903. unsigned long end;
  904. unsigned long start;
  905. uaddr = (unsigned long)iov[i].iov_base;
  906. end = (uaddr + iov[i].iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  907. start = uaddr >> PAGE_SHIFT;
  908. /*
  909. * Overflow, abort
  910. */
  911. if (end < start)
  912. return ERR_PTR(-EINVAL);
  913. nr_pages += end - start;
  914. len += iov[i].iov_len;
  915. }
  916. if (offset)
  917. nr_pages++;
  918. bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask);
  919. if (!bmd)
  920. return ERR_PTR(-ENOMEM);
  921. ret = -ENOMEM;
  922. bio = bio_kmalloc(gfp_mask, nr_pages);
  923. if (!bio)
  924. goto out_bmd;
  925. if (!write_to_vm)
  926. bio->bi_rw |= REQ_WRITE;
  927. ret = 0;
  928. if (map_data) {
  929. nr_pages = 1 << map_data->page_order;
  930. i = map_data->offset / PAGE_SIZE;
  931. }
  932. while (len) {
  933. unsigned int bytes = PAGE_SIZE;
  934. bytes -= offset;
  935. if (bytes > len)
  936. bytes = len;
  937. if (map_data) {
  938. if (i == map_data->nr_entries * nr_pages) {
  939. ret = -ENOMEM;
  940. break;
  941. }
  942. page = map_data->pages[i / nr_pages];
  943. page += (i % nr_pages);
  944. i++;
  945. } else {
  946. page = alloc_page(q->bounce_gfp | gfp_mask);
  947. if (!page) {
  948. ret = -ENOMEM;
  949. break;
  950. }
  951. }
  952. if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
  953. break;
  954. len -= bytes;
  955. offset = 0;
  956. }
  957. if (ret)
  958. goto cleanup;
  959. /*
  960. * success
  961. */
  962. if ((!write_to_vm && (!map_data || !map_data->null_mapped)) ||
  963. (map_data && map_data->from_user)) {
  964. ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0, 1, 0);
  965. if (ret)
  966. goto cleanup;
  967. }
  968. bio_set_map_data(bmd, bio, iov, iov_count, map_data ? 0 : 1);
  969. return bio;
  970. cleanup:
  971. if (!map_data)
  972. bio_for_each_segment_all(bvec, bio, i)
  973. __free_page(bvec->bv_page);
  974. bio_put(bio);
  975. out_bmd:
  976. bio_free_map_data(bmd);
  977. return ERR_PTR(ret);
  978. }
  979. /**
  980. * bio_copy_user - copy user data to bio
  981. * @q: destination block queue
  982. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  983. * @uaddr: start of user address
  984. * @len: length in bytes
  985. * @write_to_vm: bool indicating writing to pages or not
  986. * @gfp_mask: memory allocation flags
  987. *
  988. * Prepares and returns a bio for indirect user io, bouncing data
  989. * to/from kernel pages as necessary. Must be paired with
  990. * call bio_uncopy_user() on io completion.
  991. */
  992. struct bio *bio_copy_user(struct request_queue *q, struct rq_map_data *map_data,
  993. unsigned long uaddr, unsigned int len,
  994. int write_to_vm, gfp_t gfp_mask)
  995. {
  996. struct sg_iovec iov;
  997. iov.iov_base = (void __user *)uaddr;
  998. iov.iov_len = len;
  999. return bio_copy_user_iov(q, map_data, &iov, 1, write_to_vm, gfp_mask);
  1000. }
  1001. EXPORT_SYMBOL(bio_copy_user);
  1002. static struct bio *__bio_map_user_iov(struct request_queue *q,
  1003. struct block_device *bdev,
  1004. struct sg_iovec *iov, int iov_count,
  1005. int write_to_vm, gfp_t gfp_mask)
  1006. {
  1007. int i, j;
  1008. int nr_pages = 0;
  1009. struct page **pages;
  1010. struct bio *bio;
  1011. int cur_page = 0;
  1012. int ret, offset;
  1013. for (i = 0; i < iov_count; i++) {
  1014. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  1015. unsigned long len = iov[i].iov_len;
  1016. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1017. unsigned long start = uaddr >> PAGE_SHIFT;
  1018. /*
  1019. * Overflow, abort
  1020. */
  1021. if (end < start)
  1022. return ERR_PTR(-EINVAL);
  1023. nr_pages += end - start;
  1024. /*
  1025. * buffer must be aligned to at least hardsector size for now
  1026. */
  1027. if (uaddr & queue_dma_alignment(q))
  1028. return ERR_PTR(-EINVAL);
  1029. }
  1030. if (!nr_pages)
  1031. return ERR_PTR(-EINVAL);
  1032. bio = bio_kmalloc(gfp_mask, nr_pages);
  1033. if (!bio)
  1034. return ERR_PTR(-ENOMEM);
  1035. ret = -ENOMEM;
  1036. pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask);
  1037. if (!pages)
  1038. goto out;
  1039. for (i = 0; i < iov_count; i++) {
  1040. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  1041. unsigned long len = iov[i].iov_len;
  1042. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1043. unsigned long start = uaddr >> PAGE_SHIFT;
  1044. const int local_nr_pages = end - start;
  1045. const int page_limit = cur_page + local_nr_pages;
  1046. ret = get_user_pages_fast(uaddr, local_nr_pages,
  1047. write_to_vm, &pages[cur_page]);
  1048. if (ret < local_nr_pages) {
  1049. ret = -EFAULT;
  1050. goto out_unmap;
  1051. }
  1052. offset = uaddr & ~PAGE_MASK;
  1053. for (j = cur_page; j < page_limit; j++) {
  1054. unsigned int bytes = PAGE_SIZE - offset;
  1055. if (len <= 0)
  1056. break;
  1057. if (bytes > len)
  1058. bytes = len;
  1059. /*
  1060. * sorry...
  1061. */
  1062. if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
  1063. bytes)
  1064. break;
  1065. len -= bytes;
  1066. offset = 0;
  1067. }
  1068. cur_page = j;
  1069. /*
  1070. * release the pages we didn't map into the bio, if any
  1071. */
  1072. while (j < page_limit)
  1073. page_cache_release(pages[j++]);
  1074. }
  1075. kfree(pages);
  1076. /*
  1077. * set data direction, and check if mapped pages need bouncing
  1078. */
  1079. if (!write_to_vm)
  1080. bio->bi_rw |= REQ_WRITE;
  1081. bio->bi_bdev = bdev;
  1082. bio->bi_flags |= (1 << BIO_USER_MAPPED);
  1083. return bio;
  1084. out_unmap:
  1085. for (i = 0; i < nr_pages; i++) {
  1086. if(!pages[i])
  1087. break;
  1088. page_cache_release(pages[i]);
  1089. }
  1090. out:
  1091. kfree(pages);
  1092. bio_put(bio);
  1093. return ERR_PTR(ret);
  1094. }
  1095. /**
  1096. * bio_map_user - map user address into bio
  1097. * @q: the struct request_queue for the bio
  1098. * @bdev: destination block device
  1099. * @uaddr: start of user address
  1100. * @len: length in bytes
  1101. * @write_to_vm: bool indicating writing to pages or not
  1102. * @gfp_mask: memory allocation flags
  1103. *
  1104. * Map the user space address into a bio suitable for io to a block
  1105. * device. Returns an error pointer in case of error.
  1106. */
  1107. struct bio *bio_map_user(struct request_queue *q, struct block_device *bdev,
  1108. unsigned long uaddr, unsigned int len, int write_to_vm,
  1109. gfp_t gfp_mask)
  1110. {
  1111. struct sg_iovec iov;
  1112. iov.iov_base = (void __user *)uaddr;
  1113. iov.iov_len = len;
  1114. return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm, gfp_mask);
  1115. }
  1116. EXPORT_SYMBOL(bio_map_user);
  1117. /**
  1118. * bio_map_user_iov - map user sg_iovec table into bio
  1119. * @q: the struct request_queue for the bio
  1120. * @bdev: destination block device
  1121. * @iov: the iovec.
  1122. * @iov_count: number of elements in the iovec
  1123. * @write_to_vm: bool indicating writing to pages or not
  1124. * @gfp_mask: memory allocation flags
  1125. *
  1126. * Map the user space address into a bio suitable for io to a block
  1127. * device. Returns an error pointer in case of error.
  1128. */
  1129. struct bio *bio_map_user_iov(struct request_queue *q, struct block_device *bdev,
  1130. struct sg_iovec *iov, int iov_count,
  1131. int write_to_vm, gfp_t gfp_mask)
  1132. {
  1133. struct bio *bio;
  1134. bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm,
  1135. gfp_mask);
  1136. if (IS_ERR(bio))
  1137. return bio;
  1138. /*
  1139. * subtle -- if __bio_map_user() ended up bouncing a bio,
  1140. * it would normally disappear when its bi_end_io is run.
  1141. * however, we need it for the unmap, so grab an extra
  1142. * reference to it
  1143. */
  1144. bio_get(bio);
  1145. return bio;
  1146. }
  1147. static void __bio_unmap_user(struct bio *bio)
  1148. {
  1149. struct bio_vec *bvec;
  1150. int i;
  1151. /*
  1152. * make sure we dirty pages we wrote to
  1153. */
  1154. bio_for_each_segment_all(bvec, bio, i) {
  1155. if (bio_data_dir(bio) == READ)
  1156. set_page_dirty_lock(bvec->bv_page);
  1157. page_cache_release(bvec->bv_page);
  1158. }
  1159. bio_put(bio);
  1160. }
  1161. /**
  1162. * bio_unmap_user - unmap a bio
  1163. * @bio: the bio being unmapped
  1164. *
  1165. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  1166. * a process context.
  1167. *
  1168. * bio_unmap_user() may sleep.
  1169. */
  1170. void bio_unmap_user(struct bio *bio)
  1171. {
  1172. __bio_unmap_user(bio);
  1173. bio_put(bio);
  1174. }
  1175. EXPORT_SYMBOL(bio_unmap_user);
  1176. static void bio_map_kern_endio(struct bio *bio, int err)
  1177. {
  1178. bio_put(bio);
  1179. }
  1180. static struct bio *__bio_map_kern(struct request_queue *q, void *data,
  1181. unsigned int len, gfp_t gfp_mask)
  1182. {
  1183. unsigned long kaddr = (unsigned long)data;
  1184. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1185. unsigned long start = kaddr >> PAGE_SHIFT;
  1186. const int nr_pages = end - start;
  1187. int offset, i;
  1188. struct bio *bio;
  1189. bio = bio_kmalloc(gfp_mask, nr_pages);
  1190. if (!bio)
  1191. return ERR_PTR(-ENOMEM);
  1192. offset = offset_in_page(kaddr);
  1193. for (i = 0; i < nr_pages; i++) {
  1194. unsigned int bytes = PAGE_SIZE - offset;
  1195. if (len <= 0)
  1196. break;
  1197. if (bytes > len)
  1198. bytes = len;
  1199. if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
  1200. offset) < bytes)
  1201. break;
  1202. data += bytes;
  1203. len -= bytes;
  1204. offset = 0;
  1205. }
  1206. bio->bi_end_io = bio_map_kern_endio;
  1207. return bio;
  1208. }
  1209. /**
  1210. * bio_map_kern - map kernel address into bio
  1211. * @q: the struct request_queue for the bio
  1212. * @data: pointer to buffer to map
  1213. * @len: length in bytes
  1214. * @gfp_mask: allocation flags for bio allocation
  1215. *
  1216. * Map the kernel address into a bio suitable for io to a block
  1217. * device. Returns an error pointer in case of error.
  1218. */
  1219. struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
  1220. gfp_t gfp_mask)
  1221. {
  1222. struct bio *bio;
  1223. bio = __bio_map_kern(q, data, len, gfp_mask);
  1224. if (IS_ERR(bio))
  1225. return bio;
  1226. if (bio->bi_size == len)
  1227. return bio;
  1228. /*
  1229. * Don't support partial mappings.
  1230. */
  1231. bio_put(bio);
  1232. return ERR_PTR(-EINVAL);
  1233. }
  1234. EXPORT_SYMBOL(bio_map_kern);
  1235. static void bio_copy_kern_endio(struct bio *bio, int err)
  1236. {
  1237. struct bio_vec *bvec;
  1238. const int read = bio_data_dir(bio) == READ;
  1239. struct bio_map_data *bmd = bio->bi_private;
  1240. int i;
  1241. char *p = bmd->sgvecs[0].iov_base;
  1242. bio_for_each_segment_all(bvec, bio, i) {
  1243. char *addr = page_address(bvec->bv_page);
  1244. int len = bmd->iovecs[i].bv_len;
  1245. if (read)
  1246. memcpy(p, addr, len);
  1247. __free_page(bvec->bv_page);
  1248. p += len;
  1249. }
  1250. bio_free_map_data(bmd);
  1251. bio_put(bio);
  1252. }
  1253. /**
  1254. * bio_copy_kern - copy kernel address into bio
  1255. * @q: the struct request_queue for the bio
  1256. * @data: pointer to buffer to copy
  1257. * @len: length in bytes
  1258. * @gfp_mask: allocation flags for bio and page allocation
  1259. * @reading: data direction is READ
  1260. *
  1261. * copy the kernel address into a bio suitable for io to a block
  1262. * device. Returns an error pointer in case of error.
  1263. */
  1264. struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
  1265. gfp_t gfp_mask, int reading)
  1266. {
  1267. struct bio *bio;
  1268. struct bio_vec *bvec;
  1269. int i;
  1270. bio = bio_copy_user(q, NULL, (unsigned long)data, len, 1, gfp_mask);
  1271. if (IS_ERR(bio))
  1272. return bio;
  1273. if (!reading) {
  1274. void *p = data;
  1275. bio_for_each_segment_all(bvec, bio, i) {
  1276. char *addr = page_address(bvec->bv_page);
  1277. memcpy(addr, p, bvec->bv_len);
  1278. p += bvec->bv_len;
  1279. }
  1280. }
  1281. bio->bi_end_io = bio_copy_kern_endio;
  1282. return bio;
  1283. }
  1284. EXPORT_SYMBOL(bio_copy_kern);
  1285. /*
  1286. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  1287. * for performing direct-IO in BIOs.
  1288. *
  1289. * The problem is that we cannot run set_page_dirty() from interrupt context
  1290. * because the required locks are not interrupt-safe. So what we can do is to
  1291. * mark the pages dirty _before_ performing IO. And in interrupt context,
  1292. * check that the pages are still dirty. If so, fine. If not, redirty them
  1293. * in process context.
  1294. *
  1295. * We special-case compound pages here: normally this means reads into hugetlb
  1296. * pages. The logic in here doesn't really work right for compound pages
  1297. * because the VM does not uniformly chase down the head page in all cases.
  1298. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  1299. * handle them at all. So we skip compound pages here at an early stage.
  1300. *
  1301. * Note that this code is very hard to test under normal circumstances because
  1302. * direct-io pins the pages with get_user_pages(). This makes
  1303. * is_page_cache_freeable return false, and the VM will not clean the pages.
  1304. * But other code (eg, flusher threads) could clean the pages if they are mapped
  1305. * pagecache.
  1306. *
  1307. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  1308. * deferred bio dirtying paths.
  1309. */
  1310. /*
  1311. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  1312. */
  1313. void bio_set_pages_dirty(struct bio *bio)
  1314. {
  1315. struct bio_vec *bvec = bio->bi_io_vec;
  1316. int i;
  1317. for (i = 0; i < bio->bi_vcnt; i++) {
  1318. struct page *page = bvec[i].bv_page;
  1319. if (page && !PageCompound(page))
  1320. set_page_dirty_lock(page);
  1321. }
  1322. }
  1323. static void bio_release_pages(struct bio *bio)
  1324. {
  1325. struct bio_vec *bvec = bio->bi_io_vec;
  1326. int i;
  1327. for (i = 0; i < bio->bi_vcnt; i++) {
  1328. struct page *page = bvec[i].bv_page;
  1329. if (page)
  1330. put_page(page);
  1331. }
  1332. }
  1333. /*
  1334. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  1335. * If they are, then fine. If, however, some pages are clean then they must
  1336. * have been written out during the direct-IO read. So we take another ref on
  1337. * the BIO and the offending pages and re-dirty the pages in process context.
  1338. *
  1339. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  1340. * here on. It will run one page_cache_release() against each page and will
  1341. * run one bio_put() against the BIO.
  1342. */
  1343. static void bio_dirty_fn(struct work_struct *work);
  1344. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
  1345. static DEFINE_SPINLOCK(bio_dirty_lock);
  1346. static struct bio *bio_dirty_list;
  1347. /*
  1348. * This runs in process context
  1349. */
  1350. static void bio_dirty_fn(struct work_struct *work)
  1351. {
  1352. unsigned long flags;
  1353. struct bio *bio;
  1354. spin_lock_irqsave(&bio_dirty_lock, flags);
  1355. bio = bio_dirty_list;
  1356. bio_dirty_list = NULL;
  1357. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1358. while (bio) {
  1359. struct bio *next = bio->bi_private;
  1360. bio_set_pages_dirty(bio);
  1361. bio_release_pages(bio);
  1362. bio_put(bio);
  1363. bio = next;
  1364. }
  1365. }
  1366. void bio_check_pages_dirty(struct bio *bio)
  1367. {
  1368. struct bio_vec *bvec = bio->bi_io_vec;
  1369. int nr_clean_pages = 0;
  1370. int i;
  1371. for (i = 0; i < bio->bi_vcnt; i++) {
  1372. struct page *page = bvec[i].bv_page;
  1373. if (PageDirty(page) || PageCompound(page)) {
  1374. page_cache_release(page);
  1375. bvec[i].bv_page = NULL;
  1376. } else {
  1377. nr_clean_pages++;
  1378. }
  1379. }
  1380. if (nr_clean_pages) {
  1381. unsigned long flags;
  1382. spin_lock_irqsave(&bio_dirty_lock, flags);
  1383. bio->bi_private = bio_dirty_list;
  1384. bio_dirty_list = bio;
  1385. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1386. schedule_work(&bio_dirty_work);
  1387. } else {
  1388. bio_put(bio);
  1389. }
  1390. }
  1391. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  1392. void bio_flush_dcache_pages(struct bio *bi)
  1393. {
  1394. int i;
  1395. struct bio_vec *bvec;
  1396. bio_for_each_segment(bvec, bi, i)
  1397. flush_dcache_page(bvec->bv_page);
  1398. }
  1399. EXPORT_SYMBOL(bio_flush_dcache_pages);
  1400. #endif
  1401. /**
  1402. * bio_endio - end I/O on a bio
  1403. * @bio: bio
  1404. * @error: error, if any
  1405. *
  1406. * Description:
  1407. * bio_endio() will end I/O on the whole bio. bio_endio() is the
  1408. * preferred way to end I/O on a bio, it takes care of clearing
  1409. * BIO_UPTODATE on error. @error is 0 on success, and and one of the
  1410. * established -Exxxx (-EIO, for instance) error values in case
  1411. * something went wrong. No one should call bi_end_io() directly on a
  1412. * bio unless they own it and thus know that it has an end_io
  1413. * function.
  1414. **/
  1415. void bio_endio(struct bio *bio, int error)
  1416. {
  1417. if (error)
  1418. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  1419. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1420. error = -EIO;
  1421. trace_block_bio_complete(bio, error);
  1422. if (bio->bi_end_io)
  1423. bio->bi_end_io(bio, error);
  1424. }
  1425. EXPORT_SYMBOL(bio_endio);
  1426. void bio_pair_release(struct bio_pair *bp)
  1427. {
  1428. if (atomic_dec_and_test(&bp->cnt)) {
  1429. struct bio *master = bp->bio1.bi_private;
  1430. bio_endio(master, bp->error);
  1431. mempool_free(bp, bp->bio2.bi_private);
  1432. }
  1433. }
  1434. EXPORT_SYMBOL(bio_pair_release);
  1435. static void bio_pair_end_1(struct bio *bi, int err)
  1436. {
  1437. struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
  1438. if (err)
  1439. bp->error = err;
  1440. bio_pair_release(bp);
  1441. }
  1442. static void bio_pair_end_2(struct bio *bi, int err)
  1443. {
  1444. struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
  1445. if (err)
  1446. bp->error = err;
  1447. bio_pair_release(bp);
  1448. }
  1449. /*
  1450. * split a bio - only worry about a bio with a single page in its iovec
  1451. */
  1452. struct bio_pair *bio_split(struct bio *bi, int first_sectors)
  1453. {
  1454. struct bio_pair *bp = mempool_alloc(bio_split_pool, GFP_NOIO);
  1455. if (!bp)
  1456. return bp;
  1457. trace_block_split(bdev_get_queue(bi->bi_bdev), bi,
  1458. bi->bi_sector + first_sectors);
  1459. BUG_ON(bio_segments(bi) > 1);
  1460. atomic_set(&bp->cnt, 3);
  1461. bp->error = 0;
  1462. bp->bio1 = *bi;
  1463. bp->bio2 = *bi;
  1464. bp->bio2.bi_sector += first_sectors;
  1465. bp->bio2.bi_size -= first_sectors << 9;
  1466. bp->bio1.bi_size = first_sectors << 9;
  1467. if (bi->bi_vcnt != 0) {
  1468. bp->bv1 = *bio_iovec(bi);
  1469. bp->bv2 = *bio_iovec(bi);
  1470. if (bio_is_rw(bi)) {
  1471. bp->bv2.bv_offset += first_sectors << 9;
  1472. bp->bv2.bv_len -= first_sectors << 9;
  1473. bp->bv1.bv_len = first_sectors << 9;
  1474. }
  1475. bp->bio1.bi_io_vec = &bp->bv1;
  1476. bp->bio2.bi_io_vec = &bp->bv2;
  1477. bp->bio1.bi_max_vecs = 1;
  1478. bp->bio2.bi_max_vecs = 1;
  1479. }
  1480. bp->bio1.bi_end_io = bio_pair_end_1;
  1481. bp->bio2.bi_end_io = bio_pair_end_2;
  1482. bp->bio1.bi_private = bi;
  1483. bp->bio2.bi_private = bio_split_pool;
  1484. if (bio_integrity(bi))
  1485. bio_integrity_split(bi, bp, first_sectors);
  1486. return bp;
  1487. }
  1488. EXPORT_SYMBOL(bio_split);
  1489. /**
  1490. * bio_sector_offset - Find hardware sector offset in bio
  1491. * @bio: bio to inspect
  1492. * @index: bio_vec index
  1493. * @offset: offset in bv_page
  1494. *
  1495. * Return the number of hardware sectors between beginning of bio
  1496. * and an end point indicated by a bio_vec index and an offset
  1497. * within that vector's page.
  1498. */
  1499. sector_t bio_sector_offset(struct bio *bio, unsigned short index,
  1500. unsigned int offset)
  1501. {
  1502. unsigned int sector_sz;
  1503. struct bio_vec *bv;
  1504. sector_t sectors;
  1505. int i;
  1506. sector_sz = queue_logical_block_size(bio->bi_bdev->bd_disk->queue);
  1507. sectors = 0;
  1508. if (index >= bio->bi_idx)
  1509. index = bio->bi_vcnt - 1;
  1510. bio_for_each_segment_all(bv, bio, i) {
  1511. if (i == index) {
  1512. if (offset > bv->bv_offset)
  1513. sectors += (offset - bv->bv_offset) / sector_sz;
  1514. break;
  1515. }
  1516. sectors += bv->bv_len / sector_sz;
  1517. }
  1518. return sectors;
  1519. }
  1520. EXPORT_SYMBOL(bio_sector_offset);
  1521. /*
  1522. * create memory pools for biovec's in a bio_set.
  1523. * use the global biovec slabs created for general use.
  1524. */
  1525. mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries)
  1526. {
  1527. struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
  1528. return mempool_create_slab_pool(pool_entries, bp->slab);
  1529. }
  1530. void bioset_free(struct bio_set *bs)
  1531. {
  1532. if (bs->rescue_workqueue)
  1533. destroy_workqueue(bs->rescue_workqueue);
  1534. if (bs->bio_pool)
  1535. mempool_destroy(bs->bio_pool);
  1536. if (bs->bvec_pool)
  1537. mempool_destroy(bs->bvec_pool);
  1538. bioset_integrity_free(bs);
  1539. bio_put_slab(bs);
  1540. kfree(bs);
  1541. }
  1542. EXPORT_SYMBOL(bioset_free);
  1543. /**
  1544. * bioset_create - Create a bio_set
  1545. * @pool_size: Number of bio and bio_vecs to cache in the mempool
  1546. * @front_pad: Number of bytes to allocate in front of the returned bio
  1547. *
  1548. * Description:
  1549. * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
  1550. * to ask for a number of bytes to be allocated in front of the bio.
  1551. * Front pad allocation is useful for embedding the bio inside
  1552. * another structure, to avoid allocating extra data to go with the bio.
  1553. * Note that the bio must be embedded at the END of that structure always,
  1554. * or things will break badly.
  1555. */
  1556. struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
  1557. {
  1558. unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
  1559. struct bio_set *bs;
  1560. bs = kzalloc(sizeof(*bs), GFP_KERNEL);
  1561. if (!bs)
  1562. return NULL;
  1563. bs->front_pad = front_pad;
  1564. spin_lock_init(&bs->rescue_lock);
  1565. bio_list_init(&bs->rescue_list);
  1566. INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
  1567. bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
  1568. if (!bs->bio_slab) {
  1569. kfree(bs);
  1570. return NULL;
  1571. }
  1572. bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
  1573. if (!bs->bio_pool)
  1574. goto bad;
  1575. bs->bvec_pool = biovec_create_pool(bs, pool_size);
  1576. if (!bs->bvec_pool)
  1577. goto bad;
  1578. bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
  1579. if (!bs->rescue_workqueue)
  1580. goto bad;
  1581. return bs;
  1582. bad:
  1583. bioset_free(bs);
  1584. return NULL;
  1585. }
  1586. EXPORT_SYMBOL(bioset_create);
  1587. #ifdef CONFIG_BLK_CGROUP
  1588. /**
  1589. * bio_associate_current - associate a bio with %current
  1590. * @bio: target bio
  1591. *
  1592. * Associate @bio with %current if it hasn't been associated yet. Block
  1593. * layer will treat @bio as if it were issued by %current no matter which
  1594. * task actually issues it.
  1595. *
  1596. * This function takes an extra reference of @task's io_context and blkcg
  1597. * which will be put when @bio is released. The caller must own @bio,
  1598. * ensure %current->io_context exists, and is responsible for synchronizing
  1599. * calls to this function.
  1600. */
  1601. int bio_associate_current(struct bio *bio)
  1602. {
  1603. struct io_context *ioc;
  1604. struct cgroup_subsys_state *css;
  1605. if (bio->bi_ioc)
  1606. return -EBUSY;
  1607. ioc = current->io_context;
  1608. if (!ioc)
  1609. return -ENOENT;
  1610. /* acquire active ref on @ioc and associate */
  1611. get_io_context_active(ioc);
  1612. bio->bi_ioc = ioc;
  1613. /* associate blkcg if exists */
  1614. rcu_read_lock();
  1615. css = task_subsys_state(current, blkio_subsys_id);
  1616. if (css && css_tryget(css))
  1617. bio->bi_css = css;
  1618. rcu_read_unlock();
  1619. return 0;
  1620. }
  1621. /**
  1622. * bio_disassociate_task - undo bio_associate_current()
  1623. * @bio: target bio
  1624. */
  1625. void bio_disassociate_task(struct bio *bio)
  1626. {
  1627. if (bio->bi_ioc) {
  1628. put_io_context(bio->bi_ioc);
  1629. bio->bi_ioc = NULL;
  1630. }
  1631. if (bio->bi_css) {
  1632. css_put(bio->bi_css);
  1633. bio->bi_css = NULL;
  1634. }
  1635. }
  1636. #endif /* CONFIG_BLK_CGROUP */
  1637. static void __init biovec_init_slabs(void)
  1638. {
  1639. int i;
  1640. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1641. int size;
  1642. struct biovec_slab *bvs = bvec_slabs + i;
  1643. if (bvs->nr_vecs <= BIO_INLINE_VECS) {
  1644. bvs->slab = NULL;
  1645. continue;
  1646. }
  1647. size = bvs->nr_vecs * sizeof(struct bio_vec);
  1648. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  1649. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1650. }
  1651. }
  1652. static int __init init_bio(void)
  1653. {
  1654. bio_slab_max = 2;
  1655. bio_slab_nr = 0;
  1656. bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
  1657. if (!bio_slabs)
  1658. panic("bio: can't allocate bios\n");
  1659. bio_integrity_init();
  1660. biovec_init_slabs();
  1661. fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
  1662. if (!fs_bio_set)
  1663. panic("bio: can't allocate bios\n");
  1664. if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE))
  1665. panic("bio: can't create integrity pool\n");
  1666. bio_split_pool = mempool_create_kmalloc_pool(BIO_SPLIT_ENTRIES,
  1667. sizeof(struct bio_pair));
  1668. if (!bio_split_pool)
  1669. panic("bio: can't create split pool\n");
  1670. return 0;
  1671. }
  1672. subsys_initcall(init_bio);