bio.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. * @bio: destination bio
  357. * @page: page to add
  358. * @len: vec entry length
  359. * @offset: vec entry offset
  360. *
  361. * Attempt to add a page to the bio_vec maplist. This can fail for a
  362. * number of reasons, such as the bio being full or target block
  363. * device limitations. The target block device must allow bio's
  364. * smaller than PAGE_SIZE, so it is always possible to add a single
  365. * page to an empty bio. This should only be used by REQ_PC bios.
  366. */
  367. int bio_add_pc_page(request_queue_t *q, struct bio *bio, struct page *page,
  368. unsigned int len, unsigned int offset)
  369. {
  370. return __bio_add_page(q, bio, page, len, offset, q->max_hw_sectors);
  371. }
  372. /**
  373. * bio_add_page - attempt to add page to bio
  374. * @bio: destination bio
  375. * @page: page to add
  376. * @len: vec entry length
  377. * @offset: vec entry offset
  378. *
  379. * Attempt to add a page to the bio_vec maplist. This can fail for a
  380. * number of reasons, such as the bio being full or target block
  381. * device limitations. The target block device must allow bio's
  382. * smaller than PAGE_SIZE, so it is always possible to add a single
  383. * page to an empty bio.
  384. */
  385. int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
  386. unsigned int offset)
  387. {
  388. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  389. return __bio_add_page(q, bio, page, len, offset, q->max_sectors);
  390. }
  391. struct bio_map_data {
  392. struct bio_vec *iovecs;
  393. void __user *userptr;
  394. };
  395. static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio)
  396. {
  397. memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
  398. bio->bi_private = bmd;
  399. }
  400. static void bio_free_map_data(struct bio_map_data *bmd)
  401. {
  402. kfree(bmd->iovecs);
  403. kfree(bmd);
  404. }
  405. static struct bio_map_data *bio_alloc_map_data(int nr_segs)
  406. {
  407. struct bio_map_data *bmd = kmalloc(sizeof(*bmd), GFP_KERNEL);
  408. if (!bmd)
  409. return NULL;
  410. bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, GFP_KERNEL);
  411. if (bmd->iovecs)
  412. return bmd;
  413. kfree(bmd);
  414. return NULL;
  415. }
  416. /**
  417. * bio_uncopy_user - finish previously mapped bio
  418. * @bio: bio being terminated
  419. *
  420. * Free pages allocated from bio_copy_user() and write back data
  421. * to user space in case of a read.
  422. */
  423. int bio_uncopy_user(struct bio *bio)
  424. {
  425. struct bio_map_data *bmd = bio->bi_private;
  426. const int read = bio_data_dir(bio) == READ;
  427. struct bio_vec *bvec;
  428. int i, ret = 0;
  429. __bio_for_each_segment(bvec, bio, i, 0) {
  430. char *addr = page_address(bvec->bv_page);
  431. unsigned int len = bmd->iovecs[i].bv_len;
  432. if (read && !ret && copy_to_user(bmd->userptr, addr, len))
  433. ret = -EFAULT;
  434. __free_page(bvec->bv_page);
  435. bmd->userptr += len;
  436. }
  437. bio_free_map_data(bmd);
  438. bio_put(bio);
  439. return ret;
  440. }
  441. /**
  442. * bio_copy_user - copy user data to bio
  443. * @q: destination block queue
  444. * @uaddr: start of user address
  445. * @len: length in bytes
  446. * @write_to_vm: bool indicating writing to pages or not
  447. *
  448. * Prepares and returns a bio for indirect user io, bouncing data
  449. * to/from kernel pages as necessary. Must be paired with
  450. * call bio_uncopy_user() on io completion.
  451. */
  452. struct bio *bio_copy_user(request_queue_t *q, unsigned long uaddr,
  453. unsigned int len, int write_to_vm)
  454. {
  455. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  456. unsigned long start = uaddr >> PAGE_SHIFT;
  457. struct bio_map_data *bmd;
  458. struct bio_vec *bvec;
  459. struct page *page;
  460. struct bio *bio;
  461. int i, ret;
  462. bmd = bio_alloc_map_data(end - start);
  463. if (!bmd)
  464. return ERR_PTR(-ENOMEM);
  465. bmd->userptr = (void __user *) uaddr;
  466. ret = -ENOMEM;
  467. bio = bio_alloc(GFP_KERNEL, end - start);
  468. if (!bio)
  469. goto out_bmd;
  470. bio->bi_rw |= (!write_to_vm << BIO_RW);
  471. ret = 0;
  472. while (len) {
  473. unsigned int bytes = PAGE_SIZE;
  474. if (bytes > len)
  475. bytes = len;
  476. page = alloc_page(q->bounce_gfp | GFP_KERNEL);
  477. if (!page) {
  478. ret = -ENOMEM;
  479. break;
  480. }
  481. if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes) {
  482. ret = -EINVAL;
  483. break;
  484. }
  485. len -= bytes;
  486. }
  487. if (ret)
  488. goto cleanup;
  489. /*
  490. * success
  491. */
  492. if (!write_to_vm) {
  493. char __user *p = (char __user *) uaddr;
  494. /*
  495. * for a write, copy in data to kernel pages
  496. */
  497. ret = -EFAULT;
  498. bio_for_each_segment(bvec, bio, i) {
  499. char *addr = page_address(bvec->bv_page);
  500. if (copy_from_user(addr, p, bvec->bv_len))
  501. goto cleanup;
  502. p += bvec->bv_len;
  503. }
  504. }
  505. bio_set_map_data(bmd, bio);
  506. return bio;
  507. cleanup:
  508. bio_for_each_segment(bvec, bio, i)
  509. __free_page(bvec->bv_page);
  510. bio_put(bio);
  511. out_bmd:
  512. bio_free_map_data(bmd);
  513. return ERR_PTR(ret);
  514. }
  515. static struct bio *__bio_map_user_iov(request_queue_t *q,
  516. struct block_device *bdev,
  517. struct sg_iovec *iov, int iov_count,
  518. int write_to_vm)
  519. {
  520. int i, j;
  521. int nr_pages = 0;
  522. struct page **pages;
  523. struct bio *bio;
  524. int cur_page = 0;
  525. int ret, offset;
  526. for (i = 0; i < iov_count; i++) {
  527. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  528. unsigned long len = iov[i].iov_len;
  529. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  530. unsigned long start = uaddr >> PAGE_SHIFT;
  531. nr_pages += end - start;
  532. /*
  533. * transfer and buffer must be aligned to at least hardsector
  534. * size for now, in the future we can relax this restriction
  535. */
  536. if ((uaddr & queue_dma_alignment(q)) || (len & queue_dma_alignment(q)))
  537. return ERR_PTR(-EINVAL);
  538. }
  539. if (!nr_pages)
  540. return ERR_PTR(-EINVAL);
  541. bio = bio_alloc(GFP_KERNEL, nr_pages);
  542. if (!bio)
  543. return ERR_PTR(-ENOMEM);
  544. ret = -ENOMEM;
  545. pages = kmalloc(nr_pages * sizeof(struct page *), GFP_KERNEL);
  546. if (!pages)
  547. goto out;
  548. memset(pages, 0, nr_pages * sizeof(struct page *));
  549. for (i = 0; i < iov_count; i++) {
  550. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  551. unsigned long len = iov[i].iov_len;
  552. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  553. unsigned long start = uaddr >> PAGE_SHIFT;
  554. const int local_nr_pages = end - start;
  555. const int page_limit = cur_page + local_nr_pages;
  556. down_read(&current->mm->mmap_sem);
  557. ret = get_user_pages(current, current->mm, uaddr,
  558. local_nr_pages,
  559. write_to_vm, 0, &pages[cur_page], NULL);
  560. up_read(&current->mm->mmap_sem);
  561. if (ret < local_nr_pages)
  562. goto out_unmap;
  563. offset = uaddr & ~PAGE_MASK;
  564. for (j = cur_page; j < page_limit; j++) {
  565. unsigned int bytes = PAGE_SIZE - offset;
  566. if (len <= 0)
  567. break;
  568. if (bytes > len)
  569. bytes = len;
  570. /*
  571. * sorry...
  572. */
  573. if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
  574. bytes)
  575. break;
  576. len -= bytes;
  577. offset = 0;
  578. }
  579. cur_page = j;
  580. /*
  581. * release the pages we didn't map into the bio, if any
  582. */
  583. while (j < page_limit)
  584. page_cache_release(pages[j++]);
  585. }
  586. kfree(pages);
  587. /*
  588. * set data direction, and check if mapped pages need bouncing
  589. */
  590. if (!write_to_vm)
  591. bio->bi_rw |= (1 << BIO_RW);
  592. bio->bi_bdev = bdev;
  593. bio->bi_flags |= (1 << BIO_USER_MAPPED);
  594. return bio;
  595. out_unmap:
  596. for (i = 0; i < nr_pages; i++) {
  597. if(!pages[i])
  598. break;
  599. page_cache_release(pages[i]);
  600. }
  601. out:
  602. kfree(pages);
  603. bio_put(bio);
  604. return ERR_PTR(ret);
  605. }
  606. /**
  607. * bio_map_user - map user address into bio
  608. * @q: the request_queue_t for the bio
  609. * @bdev: destination block device
  610. * @uaddr: start of user address
  611. * @len: length in bytes
  612. * @write_to_vm: bool indicating writing to pages or not
  613. *
  614. * Map the user space address into a bio suitable for io to a block
  615. * device. Returns an error pointer in case of error.
  616. */
  617. struct bio *bio_map_user(request_queue_t *q, struct block_device *bdev,
  618. unsigned long uaddr, unsigned int len, int write_to_vm)
  619. {
  620. struct sg_iovec iov;
  621. iov.iov_base = (void __user *)uaddr;
  622. iov.iov_len = len;
  623. return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm);
  624. }
  625. /**
  626. * bio_map_user_iov - map user sg_iovec table into bio
  627. * @q: the request_queue_t for the bio
  628. * @bdev: destination block device
  629. * @iov: the iovec.
  630. * @iov_count: number of elements in the iovec
  631. * @write_to_vm: bool indicating writing to pages or not
  632. *
  633. * Map the user space address into a bio suitable for io to a block
  634. * device. Returns an error pointer in case of error.
  635. */
  636. struct bio *bio_map_user_iov(request_queue_t *q, struct block_device *bdev,
  637. struct sg_iovec *iov, int iov_count,
  638. int write_to_vm)
  639. {
  640. struct bio *bio;
  641. int len = 0, i;
  642. bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm);
  643. if (IS_ERR(bio))
  644. return bio;
  645. /*
  646. * subtle -- if __bio_map_user() ended up bouncing a bio,
  647. * it would normally disappear when its bi_end_io is run.
  648. * however, we need it for the unmap, so grab an extra
  649. * reference to it
  650. */
  651. bio_get(bio);
  652. for (i = 0; i < iov_count; i++)
  653. len += iov[i].iov_len;
  654. if (bio->bi_size == len)
  655. return bio;
  656. /*
  657. * don't support partial mappings
  658. */
  659. bio_endio(bio, bio->bi_size, 0);
  660. bio_unmap_user(bio);
  661. return ERR_PTR(-EINVAL);
  662. }
  663. static void __bio_unmap_user(struct bio *bio)
  664. {
  665. struct bio_vec *bvec;
  666. int i;
  667. /*
  668. * make sure we dirty pages we wrote to
  669. */
  670. __bio_for_each_segment(bvec, bio, i, 0) {
  671. if (bio_data_dir(bio) == READ)
  672. set_page_dirty_lock(bvec->bv_page);
  673. page_cache_release(bvec->bv_page);
  674. }
  675. bio_put(bio);
  676. }
  677. /**
  678. * bio_unmap_user - unmap a bio
  679. * @bio: the bio being unmapped
  680. *
  681. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  682. * a process context.
  683. *
  684. * bio_unmap_user() may sleep.
  685. */
  686. void bio_unmap_user(struct bio *bio)
  687. {
  688. __bio_unmap_user(bio);
  689. bio_put(bio);
  690. }
  691. static int bio_map_kern_endio(struct bio *bio, unsigned int bytes_done, int err)
  692. {
  693. if (bio->bi_size)
  694. return 1;
  695. bio_put(bio);
  696. return 0;
  697. }
  698. static struct bio *__bio_map_kern(request_queue_t *q, void *data,
  699. unsigned int len, gfp_t gfp_mask)
  700. {
  701. unsigned long kaddr = (unsigned long)data;
  702. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  703. unsigned long start = kaddr >> PAGE_SHIFT;
  704. const int nr_pages = end - start;
  705. int offset, i;
  706. struct bio *bio;
  707. bio = bio_alloc(gfp_mask, nr_pages);
  708. if (!bio)
  709. return ERR_PTR(-ENOMEM);
  710. offset = offset_in_page(kaddr);
  711. for (i = 0; i < nr_pages; i++) {
  712. unsigned int bytes = PAGE_SIZE - offset;
  713. if (len <= 0)
  714. break;
  715. if (bytes > len)
  716. bytes = len;
  717. if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
  718. offset) < bytes)
  719. break;
  720. data += bytes;
  721. len -= bytes;
  722. offset = 0;
  723. }
  724. bio->bi_end_io = bio_map_kern_endio;
  725. return bio;
  726. }
  727. /**
  728. * bio_map_kern - map kernel address into bio
  729. * @q: the request_queue_t for the bio
  730. * @data: pointer to buffer to map
  731. * @len: length in bytes
  732. * @gfp_mask: allocation flags for bio allocation
  733. *
  734. * Map the kernel address into a bio suitable for io to a block
  735. * device. Returns an error pointer in case of error.
  736. */
  737. struct bio *bio_map_kern(request_queue_t *q, void *data, unsigned int len,
  738. gfp_t gfp_mask)
  739. {
  740. struct bio *bio;
  741. bio = __bio_map_kern(q, data, len, gfp_mask);
  742. if (IS_ERR(bio))
  743. return bio;
  744. if (bio->bi_size == len)
  745. return bio;
  746. /*
  747. * Don't support partial mappings.
  748. */
  749. bio_put(bio);
  750. return ERR_PTR(-EINVAL);
  751. }
  752. /*
  753. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  754. * for performing direct-IO in BIOs.
  755. *
  756. * The problem is that we cannot run set_page_dirty() from interrupt context
  757. * because the required locks are not interrupt-safe. So what we can do is to
  758. * mark the pages dirty _before_ performing IO. And in interrupt context,
  759. * check that the pages are still dirty. If so, fine. If not, redirty them
  760. * in process context.
  761. *
  762. * We special-case compound pages here: normally this means reads into hugetlb
  763. * pages. The logic in here doesn't really work right for compound pages
  764. * because the VM does not uniformly chase down the head page in all cases.
  765. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  766. * handle them at all. So we skip compound pages here at an early stage.
  767. *
  768. * Note that this code is very hard to test under normal circumstances because
  769. * direct-io pins the pages with get_user_pages(). This makes
  770. * is_page_cache_freeable return false, and the VM will not clean the pages.
  771. * But other code (eg, pdflush) could clean the pages if they are mapped
  772. * pagecache.
  773. *
  774. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  775. * deferred bio dirtying paths.
  776. */
  777. /*
  778. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  779. */
  780. void bio_set_pages_dirty(struct bio *bio)
  781. {
  782. struct bio_vec *bvec = bio->bi_io_vec;
  783. int i;
  784. for (i = 0; i < bio->bi_vcnt; i++) {
  785. struct page *page = bvec[i].bv_page;
  786. if (page && !PageCompound(page))
  787. set_page_dirty_lock(page);
  788. }
  789. }
  790. static void bio_release_pages(struct bio *bio)
  791. {
  792. struct bio_vec *bvec = bio->bi_io_vec;
  793. int i;
  794. for (i = 0; i < bio->bi_vcnt; i++) {
  795. struct page *page = bvec[i].bv_page;
  796. if (page)
  797. put_page(page);
  798. }
  799. }
  800. /*
  801. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  802. * If they are, then fine. If, however, some pages are clean then they must
  803. * have been written out during the direct-IO read. So we take another ref on
  804. * the BIO and the offending pages and re-dirty the pages in process context.
  805. *
  806. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  807. * here on. It will run one page_cache_release() against each page and will
  808. * run one bio_put() against the BIO.
  809. */
  810. static void bio_dirty_fn(void *data);
  811. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn, NULL);
  812. static DEFINE_SPINLOCK(bio_dirty_lock);
  813. static struct bio *bio_dirty_list;
  814. /*
  815. * This runs in process context
  816. */
  817. static void bio_dirty_fn(void *data)
  818. {
  819. unsigned long flags;
  820. struct bio *bio;
  821. spin_lock_irqsave(&bio_dirty_lock, flags);
  822. bio = bio_dirty_list;
  823. bio_dirty_list = NULL;
  824. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  825. while (bio) {
  826. struct bio *next = bio->bi_private;
  827. bio_set_pages_dirty(bio);
  828. bio_release_pages(bio);
  829. bio_put(bio);
  830. bio = next;
  831. }
  832. }
  833. void bio_check_pages_dirty(struct bio *bio)
  834. {
  835. struct bio_vec *bvec = bio->bi_io_vec;
  836. int nr_clean_pages = 0;
  837. int i;
  838. for (i = 0; i < bio->bi_vcnt; i++) {
  839. struct page *page = bvec[i].bv_page;
  840. if (PageDirty(page) || PageCompound(page)) {
  841. page_cache_release(page);
  842. bvec[i].bv_page = NULL;
  843. } else {
  844. nr_clean_pages++;
  845. }
  846. }
  847. if (nr_clean_pages) {
  848. unsigned long flags;
  849. spin_lock_irqsave(&bio_dirty_lock, flags);
  850. bio->bi_private = bio_dirty_list;
  851. bio_dirty_list = bio;
  852. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  853. schedule_work(&bio_dirty_work);
  854. } else {
  855. bio_put(bio);
  856. }
  857. }
  858. /**
  859. * bio_endio - end I/O on a bio
  860. * @bio: bio
  861. * @bytes_done: number of bytes completed
  862. * @error: error, if any
  863. *
  864. * Description:
  865. * bio_endio() will end I/O on @bytes_done number of bytes. This may be
  866. * just a partial part of the bio, or it may be the whole bio. bio_endio()
  867. * is the preferred way to end I/O on a bio, it takes care of decrementing
  868. * bi_size and clearing BIO_UPTODATE on error. @error is 0 on success, and
  869. * and one of the established -Exxxx (-EIO, for instance) error values in
  870. * case something went wrong. Noone should call bi_end_io() directly on
  871. * a bio unless they own it and thus know that it has an end_io function.
  872. **/
  873. void bio_endio(struct bio *bio, unsigned int bytes_done, int error)
  874. {
  875. if (error)
  876. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  877. if (unlikely(bytes_done > bio->bi_size)) {
  878. printk("%s: want %u bytes done, only %u left\n", __FUNCTION__,
  879. bytes_done, bio->bi_size);
  880. bytes_done = bio->bi_size;
  881. }
  882. bio->bi_size -= bytes_done;
  883. bio->bi_sector += (bytes_done >> 9);
  884. if (bio->bi_end_io)
  885. bio->bi_end_io(bio, bytes_done, error);
  886. }
  887. void bio_pair_release(struct bio_pair *bp)
  888. {
  889. if (atomic_dec_and_test(&bp->cnt)) {
  890. struct bio *master = bp->bio1.bi_private;
  891. bio_endio(master, master->bi_size, bp->error);
  892. mempool_free(bp, bp->bio2.bi_private);
  893. }
  894. }
  895. static int bio_pair_end_1(struct bio * bi, unsigned int done, int err)
  896. {
  897. struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
  898. if (err)
  899. bp->error = err;
  900. if (bi->bi_size)
  901. return 1;
  902. bio_pair_release(bp);
  903. return 0;
  904. }
  905. static int bio_pair_end_2(struct bio * bi, unsigned int done, int err)
  906. {
  907. struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
  908. if (err)
  909. bp->error = err;
  910. if (bi->bi_size)
  911. return 1;
  912. bio_pair_release(bp);
  913. return 0;
  914. }
  915. /*
  916. * split a bio - only worry about a bio with a single page
  917. * in it's iovec
  918. */
  919. struct bio_pair *bio_split(struct bio *bi, mempool_t *pool, int first_sectors)
  920. {
  921. struct bio_pair *bp = mempool_alloc(pool, GFP_NOIO);
  922. if (!bp)
  923. return bp;
  924. BUG_ON(bi->bi_vcnt != 1);
  925. BUG_ON(bi->bi_idx != 0);
  926. atomic_set(&bp->cnt, 3);
  927. bp->error = 0;
  928. bp->bio1 = *bi;
  929. bp->bio2 = *bi;
  930. bp->bio2.bi_sector += first_sectors;
  931. bp->bio2.bi_size -= first_sectors << 9;
  932. bp->bio1.bi_size = first_sectors << 9;
  933. bp->bv1 = bi->bi_io_vec[0];
  934. bp->bv2 = bi->bi_io_vec[0];
  935. bp->bv2.bv_offset += first_sectors << 9;
  936. bp->bv2.bv_len -= first_sectors << 9;
  937. bp->bv1.bv_len = first_sectors << 9;
  938. bp->bio1.bi_io_vec = &bp->bv1;
  939. bp->bio2.bi_io_vec = &bp->bv2;
  940. bp->bio1.bi_end_io = bio_pair_end_1;
  941. bp->bio2.bi_end_io = bio_pair_end_2;
  942. bp->bio1.bi_private = bi;
  943. bp->bio2.bi_private = pool;
  944. return bp;
  945. }
  946. static void *bio_pair_alloc(gfp_t gfp_flags, void *data)
  947. {
  948. return kmalloc(sizeof(struct bio_pair), gfp_flags);
  949. }
  950. static void bio_pair_free(void *bp, void *data)
  951. {
  952. kfree(bp);
  953. }
  954. /*
  955. * create memory pools for biovec's in a bio_set.
  956. * use the global biovec slabs created for general use.
  957. */
  958. static int biovec_create_pools(struct bio_set *bs, int pool_entries, int scale)
  959. {
  960. int i;
  961. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  962. struct biovec_slab *bp = bvec_slabs + i;
  963. mempool_t **bvp = bs->bvec_pools + i;
  964. if (i >= scale)
  965. pool_entries >>= 1;
  966. *bvp = mempool_create(pool_entries, mempool_alloc_slab,
  967. mempool_free_slab, bp->slab);
  968. if (!*bvp)
  969. return -ENOMEM;
  970. }
  971. return 0;
  972. }
  973. static void biovec_free_pools(struct bio_set *bs)
  974. {
  975. int i;
  976. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  977. mempool_t *bvp = bs->bvec_pools[i];
  978. if (bvp)
  979. mempool_destroy(bvp);
  980. }
  981. }
  982. void bioset_free(struct bio_set *bs)
  983. {
  984. if (bs->bio_pool)
  985. mempool_destroy(bs->bio_pool);
  986. biovec_free_pools(bs);
  987. kfree(bs);
  988. }
  989. struct bio_set *bioset_create(int bio_pool_size, int bvec_pool_size, int scale)
  990. {
  991. struct bio_set *bs = kmalloc(sizeof(*bs), GFP_KERNEL);
  992. if (!bs)
  993. return NULL;
  994. memset(bs, 0, sizeof(*bs));
  995. bs->bio_pool = mempool_create(bio_pool_size, mempool_alloc_slab,
  996. mempool_free_slab, bio_slab);
  997. if (!bs->bio_pool)
  998. goto bad;
  999. if (!biovec_create_pools(bs, bvec_pool_size, scale))
  1000. return bs;
  1001. bad:
  1002. bioset_free(bs);
  1003. return NULL;
  1004. }
  1005. static void __init biovec_init_slabs(void)
  1006. {
  1007. int i;
  1008. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1009. int size;
  1010. struct biovec_slab *bvs = bvec_slabs + i;
  1011. size = bvs->nr_vecs * sizeof(struct bio_vec);
  1012. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  1013. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1014. }
  1015. }
  1016. static int __init init_bio(void)
  1017. {
  1018. int megabytes, bvec_pool_entries;
  1019. int scale = BIOVEC_NR_POOLS;
  1020. bio_slab = kmem_cache_create("bio", sizeof(struct bio), 0,
  1021. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
  1022. biovec_init_slabs();
  1023. megabytes = nr_free_pages() >> (20 - PAGE_SHIFT);
  1024. /*
  1025. * find out where to start scaling
  1026. */
  1027. if (megabytes <= 16)
  1028. scale = 0;
  1029. else if (megabytes <= 32)
  1030. scale = 1;
  1031. else if (megabytes <= 64)
  1032. scale = 2;
  1033. else if (megabytes <= 96)
  1034. scale = 3;
  1035. else if (megabytes <= 128)
  1036. scale = 4;
  1037. /*
  1038. * scale number of entries
  1039. */
  1040. bvec_pool_entries = megabytes * 2;
  1041. if (bvec_pool_entries > 256)
  1042. bvec_pool_entries = 256;
  1043. fs_bio_set = bioset_create(BIO_POOL_SIZE, bvec_pool_entries, scale);
  1044. if (!fs_bio_set)
  1045. panic("bio: can't allocate bios\n");
  1046. bio_split_pool = mempool_create(BIO_SPLIT_ENTRIES,
  1047. bio_pair_alloc, bio_pair_free, NULL);
  1048. if (!bio_split_pool)
  1049. panic("bio: can't create split pool\n");
  1050. return 0;
  1051. }
  1052. subsys_initcall(init_bio);
  1053. EXPORT_SYMBOL(bio_alloc);
  1054. EXPORT_SYMBOL(bio_put);
  1055. EXPORT_SYMBOL(bio_free);
  1056. EXPORT_SYMBOL(bio_endio);
  1057. EXPORT_SYMBOL(bio_init);
  1058. EXPORT_SYMBOL(__bio_clone);
  1059. EXPORT_SYMBOL(bio_clone);
  1060. EXPORT_SYMBOL(bio_phys_segments);
  1061. EXPORT_SYMBOL(bio_hw_segments);
  1062. EXPORT_SYMBOL(bio_add_page);
  1063. EXPORT_SYMBOL(bio_add_pc_page);
  1064. EXPORT_SYMBOL(bio_get_nr_vecs);
  1065. EXPORT_SYMBOL(bio_map_user);
  1066. EXPORT_SYMBOL(bio_unmap_user);
  1067. EXPORT_SYMBOL(bio_map_kern);
  1068. EXPORT_SYMBOL(bio_pair_release);
  1069. EXPORT_SYMBOL(bio_split);
  1070. EXPORT_SYMBOL(bio_split_pool);
  1071. EXPORT_SYMBOL(bio_copy_user);
  1072. EXPORT_SYMBOL(bio_uncopy_user);
  1073. EXPORT_SYMBOL(bioset_create);
  1074. EXPORT_SYMBOL(bioset_free);
  1075. EXPORT_SYMBOL(bio_alloc_bioset);