bio.c 50 KB

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