bio.c 49 KB

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