bio.c 30 KB

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