bio.c 50 KB

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