compression.c 18 KB

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