bio.c 49 KB

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