bio.c 49 KB

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