bio.c 40 KB

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