bio.c 40 KB

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