bio.c 50 KB

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