bio.c 45 KB

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