bio.c 30 KB

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