bio.c 49 KB

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