bio.c 50 KB

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