bio.c 49 KB

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