bio.c 50 KB

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