bio.c 50 KB

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