bio.c 50 KB

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