bio.c 49 KB

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