bio.c 50 KB

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