compression.c 18 KB

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