bio.c 38 KB

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