bio.c 48 KB

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