bio.c 28 KB

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