compression.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as 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 GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/bit_spinlock.h>
  34. #include <linux/pagevec.h>
  35. #include "compat.h"
  36. #include "ctree.h"
  37. #include "disk-io.h"
  38. #include "transaction.h"
  39. #include "btrfs_inode.h"
  40. #include "volumes.h"
  41. #include "ordered-data.h"
  42. #include "compression.h"
  43. #include "extent_io.h"
  44. #include "extent_map.h"
  45. struct compressed_bio {
  46. /* number of bios pending for this compressed extent */
  47. atomic_t pending_bios;
  48. /* the pages with the compressed data on them */
  49. struct page **compressed_pages;
  50. /* inode that owns this data */
  51. struct inode *inode;
  52. /* starting offset in the inode for our pages */
  53. u64 start;
  54. /* number of bytes in the inode we're working on */
  55. unsigned long len;
  56. /* number of bytes on disk */
  57. unsigned long compressed_len;
  58. /* number of compressed pages in the array */
  59. unsigned long nr_pages;
  60. /* IO errors */
  61. int errors;
  62. int mirror_num;
  63. /* for reads, this is the bio we are copying the data into */
  64. struct bio *orig_bio;
  65. /*
  66. * the start of a variable length array of checksums only
  67. * used by reads
  68. */
  69. u32 sums;
  70. };
  71. static inline int compressed_bio_size(struct btrfs_root *root,
  72. unsigned long disk_size)
  73. {
  74. u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
  75. return sizeof(struct compressed_bio) +
  76. ((disk_size + root->sectorsize - 1) / root->sectorsize) *
  77. csum_size;
  78. }
  79. static struct bio *compressed_bio_alloc(struct block_device *bdev,
  80. u64 first_byte, gfp_t gfp_flags)
  81. {
  82. struct bio *bio;
  83. int nr_vecs;
  84. nr_vecs = bio_get_nr_vecs(bdev);
  85. bio = bio_alloc(gfp_flags, nr_vecs);
  86. if (bio == NULL && (current->flags & PF_MEMALLOC)) {
  87. while (!bio && (nr_vecs /= 2))
  88. bio = bio_alloc(gfp_flags, nr_vecs);
  89. }
  90. if (bio) {
  91. bio->bi_size = 0;
  92. bio->bi_bdev = bdev;
  93. bio->bi_sector = first_byte >> 9;
  94. }
  95. return bio;
  96. }
  97. static int check_compressed_csum(struct inode *inode,
  98. struct compressed_bio *cb,
  99. u64 disk_start)
  100. {
  101. int ret;
  102. struct btrfs_root *root = BTRFS_I(inode)->root;
  103. struct page *page;
  104. unsigned long i;
  105. char *kaddr;
  106. u32 csum;
  107. u32 *cb_sum = &cb->sums;
  108. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  109. return 0;
  110. for (i = 0; i < cb->nr_pages; i++) {
  111. page = cb->compressed_pages[i];
  112. csum = ~(u32)0;
  113. kaddr = kmap_atomic(page, KM_USER0);
  114. csum = btrfs_csum_data(root, kaddr, csum, PAGE_CACHE_SIZE);
  115. btrfs_csum_final(csum, (char *)&csum);
  116. kunmap_atomic(kaddr, KM_USER0);
  117. if (csum != *cb_sum) {
  118. printk(KERN_INFO "btrfs csum failed ino %lu "
  119. "extent %llu csum %u "
  120. "wanted %u mirror %d\n", inode->i_ino,
  121. (unsigned long long)disk_start,
  122. csum, *cb_sum, cb->mirror_num);
  123. ret = -EIO;
  124. goto fail;
  125. }
  126. cb_sum++;
  127. }
  128. ret = 0;
  129. fail:
  130. return ret;
  131. }
  132. /* when we finish reading compressed pages from the disk, we
  133. * decompress them and then run the bio end_io routines on the
  134. * decompressed pages (in the inode address space).
  135. *
  136. * This allows the checksumming and other IO error handling routines
  137. * to work normally
  138. *
  139. * The compressed pages are freed here, and it must be run
  140. * in process context
  141. */
  142. static void end_compressed_bio_read(struct bio *bio, int err)
  143. {
  144. struct extent_io_tree *tree;
  145. struct compressed_bio *cb = bio->bi_private;
  146. struct inode *inode;
  147. struct page *page;
  148. unsigned long index;
  149. int ret;
  150. if (err)
  151. cb->errors = 1;
  152. /* if there are more bios still pending for this compressed
  153. * extent, just exit
  154. */
  155. if (!atomic_dec_and_test(&cb->pending_bios))
  156. goto out;
  157. inode = cb->inode;
  158. ret = check_compressed_csum(inode, cb, (u64)bio->bi_sector << 9);
  159. if (ret)
  160. goto csum_failed;
  161. /* ok, we're the last bio for this extent, lets start
  162. * the decompression.
  163. */
  164. tree = &BTRFS_I(inode)->io_tree;
  165. ret = btrfs_zlib_decompress_biovec(cb->compressed_pages,
  166. cb->start,
  167. cb->orig_bio->bi_io_vec,
  168. cb->orig_bio->bi_vcnt,
  169. cb->compressed_len);
  170. csum_failed:
  171. if (ret)
  172. cb->errors = 1;
  173. /* release the compressed pages */
  174. index = 0;
  175. for (index = 0; index < cb->nr_pages; index++) {
  176. page = cb->compressed_pages[index];
  177. page->mapping = NULL;
  178. page_cache_release(page);
  179. }
  180. /* do io completion on the original bio */
  181. if (cb->errors) {
  182. bio_io_error(cb->orig_bio);
  183. } else {
  184. int bio_index = 0;
  185. struct bio_vec *bvec = cb->orig_bio->bi_io_vec;
  186. /*
  187. * we have verified the checksum already, set page
  188. * checked so the end_io handlers know about it
  189. */
  190. while (bio_index < cb->orig_bio->bi_vcnt) {
  191. SetPageChecked(bvec->bv_page);
  192. bvec++;
  193. bio_index++;
  194. }
  195. bio_endio(cb->orig_bio, 0);
  196. }
  197. /* finally free the cb struct */
  198. kfree(cb->compressed_pages);
  199. kfree(cb);
  200. out:
  201. bio_put(bio);
  202. }
  203. /*
  204. * Clear the writeback bits on all of the file
  205. * pages for a compressed write
  206. */
  207. static noinline int end_compressed_writeback(struct inode *inode, u64 start,
  208. unsigned long ram_size)
  209. {
  210. unsigned long index = start >> PAGE_CACHE_SHIFT;
  211. unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
  212. struct page *pages[16];
  213. unsigned long nr_pages = end_index - index + 1;
  214. int i;
  215. int ret;
  216. while (nr_pages > 0) {
  217. ret = find_get_pages_contig(inode->i_mapping, index,
  218. min_t(unsigned long,
  219. nr_pages, ARRAY_SIZE(pages)), pages);
  220. if (ret == 0) {
  221. nr_pages -= 1;
  222. index += 1;
  223. continue;
  224. }
  225. for (i = 0; i < ret; i++) {
  226. end_page_writeback(pages[i]);
  227. page_cache_release(pages[i]);
  228. }
  229. nr_pages -= ret;
  230. index += ret;
  231. }
  232. /* the inode may be gone now */
  233. return 0;
  234. }
  235. /*
  236. * do the cleanup once all the compressed pages hit the disk.
  237. * This will clear writeback on the file pages and free the compressed
  238. * pages.
  239. *
  240. * This also calls the writeback end hooks for the file pages so that
  241. * metadata and checksums can be updated in the file.
  242. */
  243. static void end_compressed_bio_write(struct bio *bio, int err)
  244. {
  245. struct extent_io_tree *tree;
  246. struct compressed_bio *cb = bio->bi_private;
  247. struct inode *inode;
  248. struct page *page;
  249. unsigned long index;
  250. if (err)
  251. cb->errors = 1;
  252. /* if there are more bios still pending for this compressed
  253. * extent, just exit
  254. */
  255. if (!atomic_dec_and_test(&cb->pending_bios))
  256. goto out;
  257. /* ok, we're the last bio for this extent, step one is to
  258. * call back into the FS and do all the end_io operations
  259. */
  260. inode = cb->inode;
  261. tree = &BTRFS_I(inode)->io_tree;
  262. cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
  263. tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
  264. cb->start,
  265. cb->start + cb->len - 1,
  266. NULL, 1);
  267. cb->compressed_pages[0]->mapping = NULL;
  268. end_compressed_writeback(inode, cb->start, cb->len);
  269. /* note, our inode could be gone now */
  270. /*
  271. * release the compressed pages, these came from alloc_page and
  272. * are not attached to the inode at all
  273. */
  274. index = 0;
  275. for (index = 0; index < cb->nr_pages; index++) {
  276. page = cb->compressed_pages[index];
  277. page->mapping = NULL;
  278. page_cache_release(page);
  279. }
  280. /* finally free the cb struct */
  281. kfree(cb->compressed_pages);
  282. kfree(cb);
  283. out:
  284. bio_put(bio);
  285. }
  286. /*
  287. * worker function to build and submit bios for previously compressed pages.
  288. * The corresponding pages in the inode should be marked for writeback
  289. * and the compressed pages should have a reference on them for dropping
  290. * when the IO is complete.
  291. *
  292. * This also checksums the file bytes and gets things ready for
  293. * the end io hooks.
  294. */
  295. int btrfs_submit_compressed_write(struct inode *inode, u64 start,
  296. unsigned long len, u64 disk_start,
  297. unsigned long compressed_len,
  298. struct page **compressed_pages,
  299. unsigned long nr_pages)
  300. {
  301. struct bio *bio = NULL;
  302. struct btrfs_root *root = BTRFS_I(inode)->root;
  303. struct compressed_bio *cb;
  304. unsigned long bytes_left;
  305. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  306. int page_index = 0;
  307. struct page *page;
  308. u64 first_byte = disk_start;
  309. struct block_device *bdev;
  310. int ret;
  311. WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
  312. cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
  313. atomic_set(&cb->pending_bios, 0);
  314. cb->errors = 0;
  315. cb->inode = inode;
  316. cb->start = start;
  317. cb->len = len;
  318. cb->mirror_num = 0;
  319. cb->compressed_pages = compressed_pages;
  320. cb->compressed_len = compressed_len;
  321. cb->orig_bio = NULL;
  322. cb->nr_pages = nr_pages;
  323. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  324. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  325. bio->bi_private = cb;
  326. bio->bi_end_io = end_compressed_bio_write;
  327. atomic_inc(&cb->pending_bios);
  328. /* create and submit bios for the compressed pages */
  329. bytes_left = compressed_len;
  330. for (page_index = 0; page_index < cb->nr_pages; page_index++) {
  331. page = compressed_pages[page_index];
  332. page->mapping = inode->i_mapping;
  333. if (bio->bi_size)
  334. ret = io_tree->ops->merge_bio_hook(page, 0,
  335. PAGE_CACHE_SIZE,
  336. bio, 0);
  337. else
  338. ret = 0;
  339. page->mapping = NULL;
  340. if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
  341. PAGE_CACHE_SIZE) {
  342. bio_get(bio);
  343. /*
  344. * inc the count before we submit the bio so
  345. * we know the end IO handler won't happen before
  346. * we inc the count. Otherwise, the cb might get
  347. * freed before we're done setting it up
  348. */
  349. atomic_inc(&cb->pending_bios);
  350. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  351. BUG_ON(ret);
  352. ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
  353. BUG_ON(ret);
  354. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  355. BUG_ON(ret);
  356. bio_put(bio);
  357. bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
  358. bio->bi_private = cb;
  359. bio->bi_end_io = end_compressed_bio_write;
  360. bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
  361. }
  362. if (bytes_left < PAGE_CACHE_SIZE) {
  363. printk("bytes left %lu compress len %lu nr %lu\n",
  364. bytes_left, cb->compressed_len, cb->nr_pages);
  365. }
  366. bytes_left -= PAGE_CACHE_SIZE;
  367. first_byte += PAGE_CACHE_SIZE;
  368. cond_resched();
  369. }
  370. bio_get(bio);
  371. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  372. BUG_ON(ret);
  373. ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
  374. BUG_ON(ret);
  375. ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
  376. BUG_ON(ret);
  377. bio_put(bio);
  378. return 0;
  379. }
  380. static noinline int add_ra_bio_pages(struct inode *inode,
  381. u64 compressed_end,
  382. struct compressed_bio *cb)
  383. {
  384. unsigned long end_index;
  385. unsigned long page_index;
  386. u64 last_offset;
  387. u64 isize = i_size_read(inode);
  388. int ret;
  389. struct page *page;
  390. unsigned long nr_pages = 0;
  391. struct extent_map *em;
  392. struct address_space *mapping = inode->i_mapping;
  393. struct pagevec pvec;
  394. struct extent_map_tree *em_tree;
  395. struct extent_io_tree *tree;
  396. u64 end;
  397. int misses = 0;
  398. page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
  399. last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
  400. em_tree = &BTRFS_I(inode)->extent_tree;
  401. tree = &BTRFS_I(inode)->io_tree;
  402. if (isize == 0)
  403. return 0;
  404. end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
  405. pagevec_init(&pvec, 0);
  406. while (last_offset < compressed_end) {
  407. page_index = last_offset >> PAGE_CACHE_SHIFT;
  408. if (page_index > end_index)
  409. break;
  410. rcu_read_lock();
  411. page = radix_tree_lookup(&mapping->page_tree, page_index);
  412. rcu_read_unlock();
  413. if (page) {
  414. misses++;
  415. if (misses > 4)
  416. break;
  417. goto next;
  418. }
  419. page = alloc_page(mapping_gfp_mask(mapping) | GFP_NOFS);
  420. if (!page)
  421. break;
  422. page->index = page_index;
  423. /*
  424. * what we want to do here is call add_to_page_cache_lru,
  425. * but that isn't exported, so we reproduce it here
  426. */
  427. if (add_to_page_cache(page, mapping,
  428. page->index, GFP_NOFS)) {
  429. page_cache_release(page);
  430. goto next;
  431. }
  432. /* open coding of lru_cache_add, also not exported */
  433. page_cache_get(page);
  434. if (!pagevec_add(&pvec, page))
  435. __pagevec_lru_add_file(&pvec);
  436. end = last_offset + PAGE_CACHE_SIZE - 1;
  437. /*
  438. * at this point, we have a locked page in the page cache
  439. * for these bytes in the file. But, we have to make
  440. * sure they map to this compressed extent on disk.
  441. */
  442. set_page_extent_mapped(page);
  443. lock_extent(tree, last_offset, end, GFP_NOFS);
  444. spin_lock(&em_tree->lock);
  445. em = lookup_extent_mapping(em_tree, last_offset,
  446. PAGE_CACHE_SIZE);
  447. spin_unlock(&em_tree->lock);
  448. if (!em || last_offset < em->start ||
  449. (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
  450. (em->block_start >> 9) != cb->orig_bio->bi_sector) {
  451. free_extent_map(em);
  452. unlock_extent(tree, last_offset, end, GFP_NOFS);
  453. unlock_page(page);
  454. page_cache_release(page);
  455. break;
  456. }
  457. free_extent_map(em);
  458. if (page->index == end_index) {
  459. char *userpage;
  460. size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
  461. if (zero_offset) {
  462. int zeros;
  463. zeros = PAGE_CACHE_SIZE - zero_offset;
  464. userpage = kmap_atomic(page, KM_USER0);
  465. memset(userpage + zero_offset, 0, zeros);
  466. flush_dcache_page(page);
  467. kunmap_atomic(userpage, KM_USER0);
  468. }
  469. }
  470. ret = bio_add_page(cb->orig_bio, page,
  471. PAGE_CACHE_SIZE, 0);
  472. if (ret == PAGE_CACHE_SIZE) {
  473. nr_pages++;
  474. page_cache_release(page);
  475. } else {
  476. unlock_extent(tree, last_offset, end, GFP_NOFS);
  477. unlock_page(page);
  478. page_cache_release(page);
  479. break;
  480. }
  481. next:
  482. last_offset += PAGE_CACHE_SIZE;
  483. }
  484. if (pagevec_count(&pvec))
  485. __pagevec_lru_add_file(&pvec);
  486. return 0;
  487. }
  488. /*
  489. * for a compressed read, the bio we get passed has all the inode pages
  490. * in it. We don't actually do IO on those pages but allocate new ones
  491. * to hold the compressed pages on disk.
  492. *
  493. * bio->bi_sector points to the compressed extent on disk
  494. * bio->bi_io_vec points to all of the inode pages
  495. * bio->bi_vcnt is a count of pages
  496. *
  497. * After the compressed pages are read, we copy the bytes into the
  498. * bio we were passed and then call the bio end_io calls
  499. */
  500. int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  501. int mirror_num, unsigned long bio_flags)
  502. {
  503. struct extent_io_tree *tree;
  504. struct extent_map_tree *em_tree;
  505. struct compressed_bio *cb;
  506. struct btrfs_root *root = BTRFS_I(inode)->root;
  507. unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  508. unsigned long compressed_len;
  509. unsigned long nr_pages;
  510. unsigned long page_index;
  511. struct page *page;
  512. struct block_device *bdev;
  513. struct bio *comp_bio;
  514. u64 cur_disk_byte = (u64)bio->bi_sector << 9;
  515. u64 em_len;
  516. u64 em_start;
  517. struct extent_map *em;
  518. int ret;
  519. u32 *sums;
  520. tree = &BTRFS_I(inode)->io_tree;
  521. em_tree = &BTRFS_I(inode)->extent_tree;
  522. /* we need the actual starting offset of this extent in the file */
  523. spin_lock(&em_tree->lock);
  524. em = lookup_extent_mapping(em_tree,
  525. page_offset(bio->bi_io_vec->bv_page),
  526. PAGE_CACHE_SIZE);
  527. spin_unlock(&em_tree->lock);
  528. compressed_len = em->block_len;
  529. cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
  530. atomic_set(&cb->pending_bios, 0);
  531. cb->errors = 0;
  532. cb->inode = inode;
  533. cb->mirror_num = mirror_num;
  534. sums = &cb->sums;
  535. cb->start = em->orig_start;
  536. em_len = em->len;
  537. em_start = em->start;
  538. free_extent_map(em);
  539. em = NULL;
  540. cb->len = uncompressed_len;
  541. cb->compressed_len = compressed_len;
  542. cb->orig_bio = bio;
  543. nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
  544. PAGE_CACHE_SIZE;
  545. cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
  546. GFP_NOFS);
  547. bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
  548. for (page_index = 0; page_index < nr_pages; page_index++) {
  549. cb->compressed_pages[page_index] = alloc_page(GFP_NOFS |
  550. __GFP_HIGHMEM);
  551. }
  552. cb->nr_pages = nr_pages;
  553. add_ra_bio_pages(inode, em_start + em_len, cb);
  554. /* include any pages we added in add_ra-bio_pages */
  555. uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
  556. cb->len = uncompressed_len;
  557. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
  558. comp_bio->bi_private = cb;
  559. comp_bio->bi_end_io = end_compressed_bio_read;
  560. atomic_inc(&cb->pending_bios);
  561. for (page_index = 0; page_index < nr_pages; page_index++) {
  562. page = cb->compressed_pages[page_index];
  563. page->mapping = inode->i_mapping;
  564. page->index = em_start >> PAGE_CACHE_SHIFT;
  565. if (comp_bio->bi_size)
  566. ret = tree->ops->merge_bio_hook(page, 0,
  567. PAGE_CACHE_SIZE,
  568. comp_bio, 0);
  569. else
  570. ret = 0;
  571. page->mapping = NULL;
  572. if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
  573. PAGE_CACHE_SIZE) {
  574. bio_get(comp_bio);
  575. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  576. BUG_ON(ret);
  577. /*
  578. * inc the count before we submit the bio so
  579. * we know the end IO handler won't happen before
  580. * we inc the count. Otherwise, the cb might get
  581. * freed before we're done setting it up
  582. */
  583. atomic_inc(&cb->pending_bios);
  584. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  585. btrfs_lookup_bio_sums(root, inode, comp_bio,
  586. sums);
  587. }
  588. sums += (comp_bio->bi_size + root->sectorsize - 1) /
  589. root->sectorsize;
  590. ret = btrfs_map_bio(root, READ, comp_bio,
  591. mirror_num, 0);
  592. BUG_ON(ret);
  593. bio_put(comp_bio);
  594. comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
  595. GFP_NOFS);
  596. comp_bio->bi_private = cb;
  597. comp_bio->bi_end_io = end_compressed_bio_read;
  598. bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
  599. }
  600. cur_disk_byte += PAGE_CACHE_SIZE;
  601. }
  602. bio_get(comp_bio);
  603. ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
  604. BUG_ON(ret);
  605. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
  606. btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
  607. ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0);
  608. BUG_ON(ret);
  609. bio_put(comp_bio);
  610. return 0;
  611. }