bio.c 40 KB

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