bio.c 29 KB

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