bio.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public Licens
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  16. *
  17. */
  18. #include <linux/mm.h>
  19. #include <linux/swap.h>
  20. #include <linux/bio.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/mempool.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/blktrace_api.h>
  29. #include <scsi/sg.h> /* for struct sg_iovec */
  30. static struct kmem_cache *bio_slab __read_mostly;
  31. mempool_t *bio_split_pool __read_mostly;
  32. /*
  33. * if you change this list, also change bvec_alloc or things will
  34. * break badly! cannot be bigger than what you can fit into an
  35. * unsigned short
  36. */
  37. #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
  38. static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
  39. BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
  40. };
  41. #undef BV
  42. /*
  43. * fs_bio_set is the bio_set containing bio and iovec memory pools used by
  44. * IO code that does not need private memory pools.
  45. */
  46. struct bio_set *fs_bio_set;
  47. struct bio_vec *bvec_alloc_bs(gfp_t gfp_mask, int nr, unsigned long *idx, struct bio_set *bs)
  48. {
  49. struct bio_vec *bvl;
  50. /*
  51. * see comment near bvec_array define!
  52. */
  53. switch (nr) {
  54. case 1 : *idx = 0; break;
  55. case 2 ... 4: *idx = 1; break;
  56. case 5 ... 16: *idx = 2; break;
  57. case 17 ... 64: *idx = 3; break;
  58. case 65 ... 128: *idx = 4; break;
  59. case 129 ... BIO_MAX_PAGES: *idx = 5; break;
  60. default:
  61. return NULL;
  62. }
  63. /*
  64. * idx now points to the pool we want to allocate from
  65. */
  66. bvl = mempool_alloc(bs->bvec_pools[*idx], gfp_mask);
  67. if (bvl) {
  68. struct biovec_slab *bp = bvec_slabs + *idx;
  69. memset(bvl, 0, bp->nr_vecs * sizeof(struct bio_vec));
  70. }
  71. return bvl;
  72. }
  73. void bio_free(struct bio *bio, struct bio_set *bio_set)
  74. {
  75. if (bio->bi_io_vec) {
  76. const int pool_idx = BIO_POOL_IDX(bio);
  77. BIO_BUG_ON(pool_idx >= BIOVEC_NR_POOLS);
  78. mempool_free(bio->bi_io_vec, bio_set->bvec_pools[pool_idx]);
  79. }
  80. mempool_free(bio, bio_set->bio_pool);
  81. }
  82. /*
  83. * default destructor for a bio allocated with bio_alloc_bioset()
  84. */
  85. static void bio_fs_destructor(struct bio *bio)
  86. {
  87. bio_free(bio, fs_bio_set);
  88. }
  89. void bio_init(struct bio *bio)
  90. {
  91. memset(bio, 0, sizeof(*bio));
  92. bio->bi_flags = 1 << BIO_UPTODATE;
  93. atomic_set(&bio->bi_cnt, 1);
  94. }
  95. /**
  96. * bio_alloc_bioset - allocate a bio for I/O
  97. * @gfp_mask: the GFP_ mask given to the slab allocator
  98. * @nr_iovecs: number of iovecs to pre-allocate
  99. * @bs: the bio_set to allocate from
  100. *
  101. * Description:
  102. * bio_alloc_bioset will first try it's on mempool to satisfy the allocation.
  103. * If %__GFP_WAIT is set then we will block on the internal pool waiting
  104. * for a &struct bio to become free.
  105. *
  106. * allocate bio and iovecs from the memory pools specified by the
  107. * bio_set structure.
  108. **/
  109. struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
  110. {
  111. struct bio *bio = mempool_alloc(bs->bio_pool, gfp_mask);
  112. if (likely(bio)) {
  113. struct bio_vec *bvl = NULL;
  114. bio_init(bio);
  115. if (likely(nr_iovecs)) {
  116. unsigned long uninitialized_var(idx);
  117. bvl = bvec_alloc_bs(gfp_mask, nr_iovecs, &idx, bs);
  118. if (unlikely(!bvl)) {
  119. mempool_free(bio, bs->bio_pool);
  120. bio = NULL;
  121. goto out;
  122. }
  123. bio->bi_flags |= idx << BIO_POOL_OFFSET;
  124. bio->bi_max_vecs = bvec_slabs[idx].nr_vecs;
  125. }
  126. bio->bi_io_vec = bvl;
  127. }
  128. out:
  129. return bio;
  130. }
  131. struct bio *bio_alloc(gfp_t gfp_mask, int nr_iovecs)
  132. {
  133. struct bio *bio = bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
  134. if (bio)
  135. bio->bi_destructor = bio_fs_destructor;
  136. return bio;
  137. }
  138. void zero_fill_bio(struct bio *bio)
  139. {
  140. unsigned long flags;
  141. struct bio_vec *bv;
  142. int i;
  143. bio_for_each_segment(bv, bio, i) {
  144. char *data = bvec_kmap_irq(bv, &flags);
  145. memset(data, 0, bv->bv_len);
  146. flush_dcache_page(bv->bv_page);
  147. bvec_kunmap_irq(data, &flags);
  148. }
  149. }
  150. EXPORT_SYMBOL(zero_fill_bio);
  151. /**
  152. * bio_put - release a reference to a bio
  153. * @bio: bio to release reference to
  154. *
  155. * Description:
  156. * Put a reference to a &struct bio, either one you have gotten with
  157. * bio_alloc or bio_get. The last put of a bio will free it.
  158. **/
  159. void bio_put(struct bio *bio)
  160. {
  161. BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
  162. /*
  163. * last put frees it
  164. */
  165. if (atomic_dec_and_test(&bio->bi_cnt)) {
  166. bio->bi_next = NULL;
  167. bio->bi_destructor(bio);
  168. }
  169. }
  170. inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
  171. {
  172. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  173. blk_recount_segments(q, bio);
  174. return bio->bi_phys_segments;
  175. }
  176. inline int bio_hw_segments(struct request_queue *q, struct bio *bio)
  177. {
  178. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  179. blk_recount_segments(q, bio);
  180. return bio->bi_hw_segments;
  181. }
  182. /**
  183. * __bio_clone - clone a bio
  184. * @bio: destination bio
  185. * @bio_src: bio to clone
  186. *
  187. * Clone a &bio. Caller will own the returned bio, but not
  188. * the actual data it points to. Reference count of returned
  189. * bio will be one.
  190. */
  191. void __bio_clone(struct bio *bio, struct bio *bio_src)
  192. {
  193. memcpy(bio->bi_io_vec, bio_src->bi_io_vec,
  194. bio_src->bi_max_vecs * sizeof(struct bio_vec));
  195. /*
  196. * most users will be overriding ->bi_bdev with a new target,
  197. * so we don't set nor calculate new physical/hw segment counts here
  198. */
  199. bio->bi_sector = bio_src->bi_sector;
  200. bio->bi_bdev = bio_src->bi_bdev;
  201. bio->bi_flags |= 1 << BIO_CLONED;
  202. bio->bi_rw = bio_src->bi_rw;
  203. bio->bi_vcnt = bio_src->bi_vcnt;
  204. bio->bi_size = bio_src->bi_size;
  205. bio->bi_idx = bio_src->bi_idx;
  206. }
  207. /**
  208. * bio_clone - clone a bio
  209. * @bio: bio to clone
  210. * @gfp_mask: allocation priority
  211. *
  212. * Like __bio_clone, only also allocates the returned bio
  213. */
  214. struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
  215. {
  216. struct bio *b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, fs_bio_set);
  217. if (b) {
  218. b->bi_destructor = bio_fs_destructor;
  219. __bio_clone(b, bio);
  220. }
  221. return b;
  222. }
  223. /**
  224. * bio_get_nr_vecs - return approx number of vecs
  225. * @bdev: I/O target
  226. *
  227. * Return the approximate number of pages we can send to this target.
  228. * There's no guarantee that you will be able to fit this number of pages
  229. * into a bio, it does not account for dynamic restrictions that vary
  230. * on offset.
  231. */
  232. int bio_get_nr_vecs(struct block_device *bdev)
  233. {
  234. struct request_queue *q = bdev_get_queue(bdev);
  235. int nr_pages;
  236. nr_pages = ((q->max_sectors << 9) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  237. if (nr_pages > q->max_phys_segments)
  238. nr_pages = q->max_phys_segments;
  239. if (nr_pages > q->max_hw_segments)
  240. nr_pages = q->max_hw_segments;
  241. return nr_pages;
  242. }
  243. static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
  244. *page, unsigned int len, unsigned int offset,
  245. unsigned short max_sectors)
  246. {
  247. int retried_segments = 0;
  248. struct bio_vec *bvec;
  249. /*
  250. * cloned bio must not modify vec list
  251. */
  252. if (unlikely(bio_flagged(bio, BIO_CLONED)))
  253. return 0;
  254. if (((bio->bi_size + len) >> 9) > max_sectors)
  255. return 0;
  256. /*
  257. * For filesystems with a blocksize smaller than the pagesize
  258. * we will often be called with the same page as last time and
  259. * a consecutive offset. Optimize this special case.
  260. */
  261. if (bio->bi_vcnt > 0) {
  262. struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  263. if (page == prev->bv_page &&
  264. offset == prev->bv_offset + prev->bv_len) {
  265. prev->bv_len += len;
  266. if (q->merge_bvec_fn &&
  267. q->merge_bvec_fn(q, bio, prev) < len) {
  268. prev->bv_len -= len;
  269. return 0;
  270. }
  271. goto done;
  272. }
  273. }
  274. if (bio->bi_vcnt >= bio->bi_max_vecs)
  275. return 0;
  276. /*
  277. * we might lose a segment or two here, but rather that than
  278. * make this too complex.
  279. */
  280. while (bio->bi_phys_segments >= q->max_phys_segments
  281. || bio->bi_hw_segments >= q->max_hw_segments
  282. || BIOVEC_VIRT_OVERSIZE(bio->bi_size)) {
  283. if (retried_segments)
  284. return 0;
  285. retried_segments = 1;
  286. blk_recount_segments(q, bio);
  287. }
  288. /*
  289. * setup the new entry, we might clear it again later if we
  290. * cannot add the page
  291. */
  292. bvec = &bio->bi_io_vec[bio->bi_vcnt];
  293. bvec->bv_page = page;
  294. bvec->bv_len = len;
  295. bvec->bv_offset = offset;
  296. /*
  297. * if queue has other restrictions (eg varying max sector size
  298. * depending on offset), it can specify a merge_bvec_fn in the
  299. * queue to get further control
  300. */
  301. if (q->merge_bvec_fn) {
  302. /*
  303. * merge_bvec_fn() returns number of bytes it can accept
  304. * at this offset
  305. */
  306. if (q->merge_bvec_fn(q, bio, bvec) < len) {
  307. bvec->bv_page = NULL;
  308. bvec->bv_len = 0;
  309. bvec->bv_offset = 0;
  310. return 0;
  311. }
  312. }
  313. /* If we may be able to merge these biovecs, force a recount */
  314. if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec) ||
  315. BIOVEC_VIRT_MERGEABLE(bvec-1, bvec)))
  316. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  317. bio->bi_vcnt++;
  318. bio->bi_phys_segments++;
  319. bio->bi_hw_segments++;
  320. done:
  321. bio->bi_size += len;
  322. return len;
  323. }
  324. /**
  325. * bio_add_pc_page - attempt to add page to bio
  326. * @q: the target queue
  327. * @bio: destination bio
  328. * @page: page to add
  329. * @len: vec entry length
  330. * @offset: vec entry offset
  331. *
  332. * Attempt to add a page to the bio_vec maplist. This can fail for a
  333. * number of reasons, such as the bio being full or target block
  334. * device limitations. The target block device must allow bio's
  335. * smaller than PAGE_SIZE, so it is always possible to add a single
  336. * page to an empty bio. This should only be used by REQ_PC bios.
  337. */
  338. int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
  339. unsigned int len, unsigned int offset)
  340. {
  341. return __bio_add_page(q, bio, page, len, offset, q->max_hw_sectors);
  342. }
  343. /**
  344. * bio_add_page - attempt to add page to bio
  345. * @bio: destination bio
  346. * @page: page to add
  347. * @len: vec entry length
  348. * @offset: vec entry offset
  349. *
  350. * Attempt to add a page to the bio_vec maplist. This can fail for a
  351. * number of reasons, such as the bio being full or target block
  352. * device limitations. The target block device must allow bio's
  353. * smaller than PAGE_SIZE, so it is always possible to add a single
  354. * page to an empty bio.
  355. */
  356. int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
  357. unsigned int offset)
  358. {
  359. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  360. return __bio_add_page(q, bio, page, len, offset, q->max_sectors);
  361. }
  362. struct bio_map_data {
  363. struct bio_vec *iovecs;
  364. int nr_sgvecs;
  365. struct sg_iovec *sgvecs;
  366. };
  367. static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio,
  368. struct sg_iovec *iov, int iov_count)
  369. {
  370. memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
  371. memcpy(bmd->sgvecs, iov, sizeof(struct sg_iovec) * iov_count);
  372. bmd->nr_sgvecs = iov_count;
  373. bio->bi_private = bmd;
  374. }
  375. static void bio_free_map_data(struct bio_map_data *bmd)
  376. {
  377. kfree(bmd->iovecs);
  378. kfree(bmd->sgvecs);
  379. kfree(bmd);
  380. }
  381. static struct bio_map_data *bio_alloc_map_data(int nr_segs, int iov_count)
  382. {
  383. struct bio_map_data *bmd = kmalloc(sizeof(*bmd), GFP_KERNEL);
  384. if (!bmd)
  385. return NULL;
  386. bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, GFP_KERNEL);
  387. if (!bmd->iovecs) {
  388. kfree(bmd);
  389. return NULL;
  390. }
  391. bmd->sgvecs = kmalloc(sizeof(struct sg_iovec) * iov_count, GFP_KERNEL);
  392. if (bmd->sgvecs)
  393. return bmd;
  394. kfree(bmd->iovecs);
  395. kfree(bmd);
  396. return NULL;
  397. }
  398. static int __bio_copy_iov(struct bio *bio, struct sg_iovec *iov, int iov_count,
  399. int uncopy)
  400. {
  401. int ret = 0, i;
  402. struct bio_vec *bvec;
  403. int iov_idx = 0;
  404. unsigned int iov_off = 0;
  405. int read = bio_data_dir(bio) == READ;
  406. __bio_for_each_segment(bvec, bio, i, 0) {
  407. char *bv_addr = page_address(bvec->bv_page);
  408. unsigned int bv_len = bvec->bv_len;
  409. while (bv_len && iov_idx < iov_count) {
  410. unsigned int bytes;
  411. char *iov_addr;
  412. bytes = min_t(unsigned int,
  413. iov[iov_idx].iov_len - iov_off, bv_len);
  414. iov_addr = iov[iov_idx].iov_base + iov_off;
  415. if (!ret) {
  416. if (!read && !uncopy)
  417. ret = copy_from_user(bv_addr, iov_addr,
  418. bytes);
  419. if (read && uncopy)
  420. ret = copy_to_user(iov_addr, bv_addr,
  421. bytes);
  422. if (ret)
  423. ret = -EFAULT;
  424. }
  425. bv_len -= bytes;
  426. bv_addr += bytes;
  427. iov_addr += bytes;
  428. iov_off += bytes;
  429. if (iov[iov_idx].iov_len == iov_off) {
  430. iov_idx++;
  431. iov_off = 0;
  432. }
  433. }
  434. if (uncopy)
  435. __free_page(bvec->bv_page);
  436. }
  437. return ret;
  438. }
  439. /**
  440. * bio_uncopy_user - finish previously mapped bio
  441. * @bio: bio being terminated
  442. *
  443. * Free pages allocated from bio_copy_user() and write back data
  444. * to user space in case of a read.
  445. */
  446. int bio_uncopy_user(struct bio *bio)
  447. {
  448. struct bio_map_data *bmd = bio->bi_private;
  449. int ret;
  450. ret = __bio_copy_iov(bio, bmd->sgvecs, bmd->nr_sgvecs, 1);
  451. bio_free_map_data(bmd);
  452. bio_put(bio);
  453. return ret;
  454. }
  455. /**
  456. * bio_copy_user_iov - copy user data to bio
  457. * @q: destination block queue
  458. * @iov: the iovec.
  459. * @iov_count: number of elements in the iovec
  460. * @write_to_vm: bool indicating writing to pages or not
  461. *
  462. * Prepares and returns a bio for indirect user io, bouncing data
  463. * to/from kernel pages as necessary. Must be paired with
  464. * call bio_uncopy_user() on io completion.
  465. */
  466. struct bio *bio_copy_user_iov(struct request_queue *q, struct sg_iovec *iov,
  467. int iov_count, int write_to_vm)
  468. {
  469. struct bio_map_data *bmd;
  470. struct bio_vec *bvec;
  471. struct page *page;
  472. struct bio *bio;
  473. int i, ret;
  474. int nr_pages = 0;
  475. unsigned int len = 0;
  476. for (i = 0; i < iov_count; i++) {
  477. unsigned long uaddr;
  478. unsigned long end;
  479. unsigned long start;
  480. uaddr = (unsigned long)iov[i].iov_base;
  481. end = (uaddr + iov[i].iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  482. start = uaddr >> PAGE_SHIFT;
  483. nr_pages += end - start;
  484. len += iov[i].iov_len;
  485. }
  486. bmd = bio_alloc_map_data(nr_pages, iov_count);
  487. if (!bmd)
  488. return ERR_PTR(-ENOMEM);
  489. ret = -ENOMEM;
  490. bio = bio_alloc(GFP_KERNEL, nr_pages);
  491. if (!bio)
  492. goto out_bmd;
  493. bio->bi_rw |= (!write_to_vm << BIO_RW);
  494. ret = 0;
  495. while (len) {
  496. unsigned int bytes = PAGE_SIZE;
  497. if (bytes > len)
  498. bytes = len;
  499. page = alloc_page(q->bounce_gfp | GFP_KERNEL);
  500. if (!page) {
  501. ret = -ENOMEM;
  502. break;
  503. }
  504. if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
  505. break;
  506. len -= bytes;
  507. }
  508. if (ret)
  509. goto cleanup;
  510. /*
  511. * success
  512. */
  513. if (!write_to_vm) {
  514. ret = __bio_copy_iov(bio, iov, iov_count, 0);
  515. if (ret)
  516. goto cleanup;
  517. }
  518. bio_set_map_data(bmd, bio, iov, iov_count);
  519. return bio;
  520. cleanup:
  521. bio_for_each_segment(bvec, bio, i)
  522. __free_page(bvec->bv_page);
  523. bio_put(bio);
  524. out_bmd:
  525. bio_free_map_data(bmd);
  526. return ERR_PTR(ret);
  527. }
  528. /**
  529. * bio_copy_user - copy user data to bio
  530. * @q: destination block queue
  531. * @uaddr: start of user address
  532. * @len: length in bytes
  533. * @write_to_vm: bool indicating writing to pages or not
  534. *
  535. * Prepares and returns a bio for indirect user io, bouncing data
  536. * to/from kernel pages as necessary. Must be paired with
  537. * call bio_uncopy_user() on io completion.
  538. */
  539. struct bio *bio_copy_user(struct request_queue *q, unsigned long uaddr,
  540. unsigned int len, int write_to_vm)
  541. {
  542. struct sg_iovec iov;
  543. iov.iov_base = (void __user *)uaddr;
  544. iov.iov_len = len;
  545. return bio_copy_user_iov(q, &iov, 1, write_to_vm);
  546. }
  547. static struct bio *__bio_map_user_iov(struct request_queue *q,
  548. struct block_device *bdev,
  549. struct sg_iovec *iov, int iov_count,
  550. int write_to_vm)
  551. {
  552. int i, j;
  553. int nr_pages = 0;
  554. struct page **pages;
  555. struct bio *bio;
  556. int cur_page = 0;
  557. int ret, offset;
  558. for (i = 0; i < iov_count; i++) {
  559. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  560. unsigned long len = iov[i].iov_len;
  561. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  562. unsigned long start = uaddr >> PAGE_SHIFT;
  563. nr_pages += end - start;
  564. /*
  565. * buffer must be aligned to at least hardsector size for now
  566. */
  567. if (uaddr & queue_dma_alignment(q))
  568. return ERR_PTR(-EINVAL);
  569. }
  570. if (!nr_pages)
  571. return ERR_PTR(-EINVAL);
  572. bio = bio_alloc(GFP_KERNEL, nr_pages);
  573. if (!bio)
  574. return ERR_PTR(-ENOMEM);
  575. ret = -ENOMEM;
  576. pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  577. if (!pages)
  578. goto out;
  579. for (i = 0; i < iov_count; i++) {
  580. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  581. unsigned long len = iov[i].iov_len;
  582. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  583. unsigned long start = uaddr >> PAGE_SHIFT;
  584. const int local_nr_pages = end - start;
  585. const int page_limit = cur_page + local_nr_pages;
  586. down_read(&current->mm->mmap_sem);
  587. ret = get_user_pages(current, current->mm, uaddr,
  588. local_nr_pages,
  589. write_to_vm, 0, &pages[cur_page], NULL);
  590. up_read(&current->mm->mmap_sem);
  591. if (ret < local_nr_pages) {
  592. ret = -EFAULT;
  593. goto out_unmap;
  594. }
  595. offset = uaddr & ~PAGE_MASK;
  596. for (j = cur_page; j < page_limit; j++) {
  597. unsigned int bytes = PAGE_SIZE - offset;
  598. if (len <= 0)
  599. break;
  600. if (bytes > len)
  601. bytes = len;
  602. /*
  603. * sorry...
  604. */
  605. if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
  606. bytes)
  607. break;
  608. len -= bytes;
  609. offset = 0;
  610. }
  611. cur_page = j;
  612. /*
  613. * release the pages we didn't map into the bio, if any
  614. */
  615. while (j < page_limit)
  616. page_cache_release(pages[j++]);
  617. }
  618. kfree(pages);
  619. /*
  620. * set data direction, and check if mapped pages need bouncing
  621. */
  622. if (!write_to_vm)
  623. bio->bi_rw |= (1 << BIO_RW);
  624. bio->bi_bdev = bdev;
  625. bio->bi_flags |= (1 << BIO_USER_MAPPED);
  626. return bio;
  627. out_unmap:
  628. for (i = 0; i < nr_pages; i++) {
  629. if(!pages[i])
  630. break;
  631. page_cache_release(pages[i]);
  632. }
  633. out:
  634. kfree(pages);
  635. bio_put(bio);
  636. return ERR_PTR(ret);
  637. }
  638. /**
  639. * bio_map_user - map user address into bio
  640. * @q: the struct request_queue for the bio
  641. * @bdev: destination block device
  642. * @uaddr: start of user address
  643. * @len: length in bytes
  644. * @write_to_vm: bool indicating writing to pages or not
  645. *
  646. * Map the user space address into a bio suitable for io to a block
  647. * device. Returns an error pointer in case of error.
  648. */
  649. struct bio *bio_map_user(struct request_queue *q, struct block_device *bdev,
  650. unsigned long uaddr, unsigned int len, int write_to_vm)
  651. {
  652. struct sg_iovec iov;
  653. iov.iov_base = (void __user *)uaddr;
  654. iov.iov_len = len;
  655. return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm);
  656. }
  657. /**
  658. * bio_map_user_iov - map user sg_iovec table into bio
  659. * @q: the struct request_queue for the bio
  660. * @bdev: destination block device
  661. * @iov: the iovec.
  662. * @iov_count: number of elements in the iovec
  663. * @write_to_vm: bool indicating writing to pages or not
  664. *
  665. * Map the user space address into a bio suitable for io to a block
  666. * device. Returns an error pointer in case of error.
  667. */
  668. struct bio *bio_map_user_iov(struct request_queue *q, struct block_device *bdev,
  669. struct sg_iovec *iov, int iov_count,
  670. int write_to_vm)
  671. {
  672. struct bio *bio;
  673. bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm);
  674. if (IS_ERR(bio))
  675. return bio;
  676. /*
  677. * subtle -- if __bio_map_user() ended up bouncing a bio,
  678. * it would normally disappear when its bi_end_io is run.
  679. * however, we need it for the unmap, so grab an extra
  680. * reference to it
  681. */
  682. bio_get(bio);
  683. return bio;
  684. }
  685. static void __bio_unmap_user(struct bio *bio)
  686. {
  687. struct bio_vec *bvec;
  688. int i;
  689. /*
  690. * make sure we dirty pages we wrote to
  691. */
  692. __bio_for_each_segment(bvec, bio, i, 0) {
  693. if (bio_data_dir(bio) == READ)
  694. set_page_dirty_lock(bvec->bv_page);
  695. page_cache_release(bvec->bv_page);
  696. }
  697. bio_put(bio);
  698. }
  699. /**
  700. * bio_unmap_user - unmap a bio
  701. * @bio: the bio being unmapped
  702. *
  703. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  704. * a process context.
  705. *
  706. * bio_unmap_user() may sleep.
  707. */
  708. void bio_unmap_user(struct bio *bio)
  709. {
  710. __bio_unmap_user(bio);
  711. bio_put(bio);
  712. }
  713. static void bio_map_kern_endio(struct bio *bio, int err)
  714. {
  715. bio_put(bio);
  716. }
  717. static struct bio *__bio_map_kern(struct request_queue *q, void *data,
  718. unsigned int len, gfp_t gfp_mask)
  719. {
  720. unsigned long kaddr = (unsigned long)data;
  721. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  722. unsigned long start = kaddr >> PAGE_SHIFT;
  723. const int nr_pages = end - start;
  724. int offset, i;
  725. struct bio *bio;
  726. bio = bio_alloc(gfp_mask, nr_pages);
  727. if (!bio)
  728. return ERR_PTR(-ENOMEM);
  729. offset = offset_in_page(kaddr);
  730. for (i = 0; i < nr_pages; i++) {
  731. unsigned int bytes = PAGE_SIZE - offset;
  732. if (len <= 0)
  733. break;
  734. if (bytes > len)
  735. bytes = len;
  736. if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
  737. offset) < bytes)
  738. break;
  739. data += bytes;
  740. len -= bytes;
  741. offset = 0;
  742. }
  743. bio->bi_end_io = bio_map_kern_endio;
  744. return bio;
  745. }
  746. /**
  747. * bio_map_kern - map kernel address into bio
  748. * @q: the struct request_queue for the bio
  749. * @data: pointer to buffer to map
  750. * @len: length in bytes
  751. * @gfp_mask: allocation flags for bio allocation
  752. *
  753. * Map the kernel address into a bio suitable for io to a block
  754. * device. Returns an error pointer in case of error.
  755. */
  756. struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
  757. gfp_t gfp_mask)
  758. {
  759. struct bio *bio;
  760. bio = __bio_map_kern(q, data, len, gfp_mask);
  761. if (IS_ERR(bio))
  762. return bio;
  763. if (bio->bi_size == len)
  764. return bio;
  765. /*
  766. * Don't support partial mappings.
  767. */
  768. bio_put(bio);
  769. return ERR_PTR(-EINVAL);
  770. }
  771. static void bio_copy_kern_endio(struct bio *bio, int err)
  772. {
  773. struct bio_vec *bvec;
  774. const int read = bio_data_dir(bio) == READ;
  775. char *p = bio->bi_private;
  776. int i;
  777. __bio_for_each_segment(bvec, bio, i, 0) {
  778. char *addr = page_address(bvec->bv_page);
  779. if (read && !err)
  780. memcpy(p, addr, bvec->bv_len);
  781. __free_page(bvec->bv_page);
  782. p += bvec->bv_len;
  783. }
  784. bio_put(bio);
  785. }
  786. /**
  787. * bio_copy_kern - copy kernel address into bio
  788. * @q: the struct request_queue for the bio
  789. * @data: pointer to buffer to copy
  790. * @len: length in bytes
  791. * @gfp_mask: allocation flags for bio and page allocation
  792. * @reading: data direction is READ
  793. *
  794. * copy the kernel address into a bio suitable for io to a block
  795. * device. Returns an error pointer in case of error.
  796. */
  797. struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
  798. gfp_t gfp_mask, int reading)
  799. {
  800. unsigned long kaddr = (unsigned long)data;
  801. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  802. unsigned long start = kaddr >> PAGE_SHIFT;
  803. const int nr_pages = end - start;
  804. struct bio *bio;
  805. struct bio_vec *bvec;
  806. int i, ret;
  807. bio = bio_alloc(gfp_mask, nr_pages);
  808. if (!bio)
  809. return ERR_PTR(-ENOMEM);
  810. while (len) {
  811. struct page *page;
  812. unsigned int bytes = PAGE_SIZE;
  813. if (bytes > len)
  814. bytes = len;
  815. page = alloc_page(q->bounce_gfp | gfp_mask);
  816. if (!page) {
  817. ret = -ENOMEM;
  818. goto cleanup;
  819. }
  820. if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes) {
  821. ret = -EINVAL;
  822. goto cleanup;
  823. }
  824. len -= bytes;
  825. }
  826. if (!reading) {
  827. void *p = data;
  828. bio_for_each_segment(bvec, bio, i) {
  829. char *addr = page_address(bvec->bv_page);
  830. memcpy(addr, p, bvec->bv_len);
  831. p += bvec->bv_len;
  832. }
  833. }
  834. bio->bi_private = data;
  835. bio->bi_end_io = bio_copy_kern_endio;
  836. return bio;
  837. cleanup:
  838. bio_for_each_segment(bvec, bio, i)
  839. __free_page(bvec->bv_page);
  840. bio_put(bio);
  841. return ERR_PTR(ret);
  842. }
  843. /*
  844. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  845. * for performing direct-IO in BIOs.
  846. *
  847. * The problem is that we cannot run set_page_dirty() from interrupt context
  848. * because the required locks are not interrupt-safe. So what we can do is to
  849. * mark the pages dirty _before_ performing IO. And in interrupt context,
  850. * check that the pages are still dirty. If so, fine. If not, redirty them
  851. * in process context.
  852. *
  853. * We special-case compound pages here: normally this means reads into hugetlb
  854. * pages. The logic in here doesn't really work right for compound pages
  855. * because the VM does not uniformly chase down the head page in all cases.
  856. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  857. * handle them at all. So we skip compound pages here at an early stage.
  858. *
  859. * Note that this code is very hard to test under normal circumstances because
  860. * direct-io pins the pages with get_user_pages(). This makes
  861. * is_page_cache_freeable return false, and the VM will not clean the pages.
  862. * But other code (eg, pdflush) could clean the pages if they are mapped
  863. * pagecache.
  864. *
  865. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  866. * deferred bio dirtying paths.
  867. */
  868. /*
  869. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  870. */
  871. void bio_set_pages_dirty(struct bio *bio)
  872. {
  873. struct bio_vec *bvec = bio->bi_io_vec;
  874. int i;
  875. for (i = 0; i < bio->bi_vcnt; i++) {
  876. struct page *page = bvec[i].bv_page;
  877. if (page && !PageCompound(page))
  878. set_page_dirty_lock(page);
  879. }
  880. }
  881. static void bio_release_pages(struct bio *bio)
  882. {
  883. struct bio_vec *bvec = bio->bi_io_vec;
  884. int i;
  885. for (i = 0; i < bio->bi_vcnt; i++) {
  886. struct page *page = bvec[i].bv_page;
  887. if (page)
  888. put_page(page);
  889. }
  890. }
  891. /*
  892. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  893. * If they are, then fine. If, however, some pages are clean then they must
  894. * have been written out during the direct-IO read. So we take another ref on
  895. * the BIO and the offending pages and re-dirty the pages in process context.
  896. *
  897. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  898. * here on. It will run one page_cache_release() against each page and will
  899. * run one bio_put() against the BIO.
  900. */
  901. static void bio_dirty_fn(struct work_struct *work);
  902. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
  903. static DEFINE_SPINLOCK(bio_dirty_lock);
  904. static struct bio *bio_dirty_list;
  905. /*
  906. * This runs in process context
  907. */
  908. static void bio_dirty_fn(struct work_struct *work)
  909. {
  910. unsigned long flags;
  911. struct bio *bio;
  912. spin_lock_irqsave(&bio_dirty_lock, flags);
  913. bio = bio_dirty_list;
  914. bio_dirty_list = NULL;
  915. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  916. while (bio) {
  917. struct bio *next = bio->bi_private;
  918. bio_set_pages_dirty(bio);
  919. bio_release_pages(bio);
  920. bio_put(bio);
  921. bio = next;
  922. }
  923. }
  924. void bio_check_pages_dirty(struct bio *bio)
  925. {
  926. struct bio_vec *bvec = bio->bi_io_vec;
  927. int nr_clean_pages = 0;
  928. int i;
  929. for (i = 0; i < bio->bi_vcnt; i++) {
  930. struct page *page = bvec[i].bv_page;
  931. if (PageDirty(page) || PageCompound(page)) {
  932. page_cache_release(page);
  933. bvec[i].bv_page = NULL;
  934. } else {
  935. nr_clean_pages++;
  936. }
  937. }
  938. if (nr_clean_pages) {
  939. unsigned long flags;
  940. spin_lock_irqsave(&bio_dirty_lock, flags);
  941. bio->bi_private = bio_dirty_list;
  942. bio_dirty_list = bio;
  943. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  944. schedule_work(&bio_dirty_work);
  945. } else {
  946. bio_put(bio);
  947. }
  948. }
  949. /**
  950. * bio_endio - end I/O on a bio
  951. * @bio: bio
  952. * @error: error, if any
  953. *
  954. * Description:
  955. * bio_endio() will end I/O on the whole bio. bio_endio() is the
  956. * preferred way to end I/O on a bio, it takes care of clearing
  957. * BIO_UPTODATE on error. @error is 0 on success, and and one of the
  958. * established -Exxxx (-EIO, for instance) error values in case
  959. * something went wrong. Noone should call bi_end_io() directly on a
  960. * bio unless they own it and thus know that it has an end_io
  961. * function.
  962. **/
  963. void bio_endio(struct bio *bio, int error)
  964. {
  965. if (error)
  966. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  967. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  968. error = -EIO;
  969. if (bio->bi_end_io)
  970. bio->bi_end_io(bio, error);
  971. }
  972. void bio_pair_release(struct bio_pair *bp)
  973. {
  974. if (atomic_dec_and_test(&bp->cnt)) {
  975. struct bio *master = bp->bio1.bi_private;
  976. bio_endio(master, bp->error);
  977. mempool_free(bp, bp->bio2.bi_private);
  978. }
  979. }
  980. static void bio_pair_end_1(struct bio *bi, int err)
  981. {
  982. struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
  983. if (err)
  984. bp->error = err;
  985. bio_pair_release(bp);
  986. }
  987. static void bio_pair_end_2(struct bio *bi, int err)
  988. {
  989. struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
  990. if (err)
  991. bp->error = err;
  992. bio_pair_release(bp);
  993. }
  994. /*
  995. * split a bio - only worry about a bio with a single page
  996. * in it's iovec
  997. */
  998. struct bio_pair *bio_split(struct bio *bi, mempool_t *pool, int first_sectors)
  999. {
  1000. struct bio_pair *bp = mempool_alloc(pool, GFP_NOIO);
  1001. if (!bp)
  1002. return bp;
  1003. blk_add_trace_pdu_int(bdev_get_queue(bi->bi_bdev), BLK_TA_SPLIT, bi,
  1004. bi->bi_sector + first_sectors);
  1005. BUG_ON(bi->bi_vcnt != 1);
  1006. BUG_ON(bi->bi_idx != 0);
  1007. atomic_set(&bp->cnt, 3);
  1008. bp->error = 0;
  1009. bp->bio1 = *bi;
  1010. bp->bio2 = *bi;
  1011. bp->bio2.bi_sector += first_sectors;
  1012. bp->bio2.bi_size -= first_sectors << 9;
  1013. bp->bio1.bi_size = first_sectors << 9;
  1014. bp->bv1 = bi->bi_io_vec[0];
  1015. bp->bv2 = bi->bi_io_vec[0];
  1016. bp->bv2.bv_offset += first_sectors << 9;
  1017. bp->bv2.bv_len -= first_sectors << 9;
  1018. bp->bv1.bv_len = first_sectors << 9;
  1019. bp->bio1.bi_io_vec = &bp->bv1;
  1020. bp->bio2.bi_io_vec = &bp->bv2;
  1021. bp->bio1.bi_max_vecs = 1;
  1022. bp->bio2.bi_max_vecs = 1;
  1023. bp->bio1.bi_end_io = bio_pair_end_1;
  1024. bp->bio2.bi_end_io = bio_pair_end_2;
  1025. bp->bio1.bi_private = bi;
  1026. bp->bio2.bi_private = pool;
  1027. return bp;
  1028. }
  1029. /*
  1030. * create memory pools for biovec's in a bio_set.
  1031. * use the global biovec slabs created for general use.
  1032. */
  1033. static int biovec_create_pools(struct bio_set *bs, int pool_entries)
  1034. {
  1035. int i;
  1036. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1037. struct biovec_slab *bp = bvec_slabs + i;
  1038. mempool_t **bvp = bs->bvec_pools + i;
  1039. *bvp = mempool_create_slab_pool(pool_entries, bp->slab);
  1040. if (!*bvp)
  1041. return -ENOMEM;
  1042. }
  1043. return 0;
  1044. }
  1045. static void biovec_free_pools(struct bio_set *bs)
  1046. {
  1047. int i;
  1048. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1049. mempool_t *bvp = bs->bvec_pools[i];
  1050. if (bvp)
  1051. mempool_destroy(bvp);
  1052. }
  1053. }
  1054. void bioset_free(struct bio_set *bs)
  1055. {
  1056. if (bs->bio_pool)
  1057. mempool_destroy(bs->bio_pool);
  1058. biovec_free_pools(bs);
  1059. kfree(bs);
  1060. }
  1061. struct bio_set *bioset_create(int bio_pool_size, int bvec_pool_size)
  1062. {
  1063. struct bio_set *bs = kzalloc(sizeof(*bs), GFP_KERNEL);
  1064. if (!bs)
  1065. return NULL;
  1066. bs->bio_pool = mempool_create_slab_pool(bio_pool_size, bio_slab);
  1067. if (!bs->bio_pool)
  1068. goto bad;
  1069. if (!biovec_create_pools(bs, bvec_pool_size))
  1070. return bs;
  1071. bad:
  1072. bioset_free(bs);
  1073. return NULL;
  1074. }
  1075. static void __init biovec_init_slabs(void)
  1076. {
  1077. int i;
  1078. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1079. int size;
  1080. struct biovec_slab *bvs = bvec_slabs + i;
  1081. size = bvs->nr_vecs * sizeof(struct bio_vec);
  1082. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  1083. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1084. }
  1085. }
  1086. static int __init init_bio(void)
  1087. {
  1088. bio_slab = KMEM_CACHE(bio, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  1089. biovec_init_slabs();
  1090. fs_bio_set = bioset_create(BIO_POOL_SIZE, 2);
  1091. if (!fs_bio_set)
  1092. panic("bio: can't allocate bios\n");
  1093. bio_split_pool = mempool_create_kmalloc_pool(BIO_SPLIT_ENTRIES,
  1094. sizeof(struct bio_pair));
  1095. if (!bio_split_pool)
  1096. panic("bio: can't create split pool\n");
  1097. return 0;
  1098. }
  1099. subsys_initcall(init_bio);
  1100. EXPORT_SYMBOL(bio_alloc);
  1101. EXPORT_SYMBOL(bio_put);
  1102. EXPORT_SYMBOL(bio_free);
  1103. EXPORT_SYMBOL(bio_endio);
  1104. EXPORT_SYMBOL(bio_init);
  1105. EXPORT_SYMBOL(__bio_clone);
  1106. EXPORT_SYMBOL(bio_clone);
  1107. EXPORT_SYMBOL(bio_phys_segments);
  1108. EXPORT_SYMBOL(bio_hw_segments);
  1109. EXPORT_SYMBOL(bio_add_page);
  1110. EXPORT_SYMBOL(bio_add_pc_page);
  1111. EXPORT_SYMBOL(bio_get_nr_vecs);
  1112. EXPORT_SYMBOL(bio_map_user);
  1113. EXPORT_SYMBOL(bio_unmap_user);
  1114. EXPORT_SYMBOL(bio_map_kern);
  1115. EXPORT_SYMBOL(bio_copy_kern);
  1116. EXPORT_SYMBOL(bio_pair_release);
  1117. EXPORT_SYMBOL(bio_split);
  1118. EXPORT_SYMBOL(bio_split_pool);
  1119. EXPORT_SYMBOL(bio_copy_user);
  1120. EXPORT_SYMBOL(bio_uncopy_user);
  1121. EXPORT_SYMBOL(bioset_create);
  1122. EXPORT_SYMBOL(bioset_free);
  1123. EXPORT_SYMBOL(bio_alloc_bioset);