blob: 1d54c5308df59d361b6efaa41d6870ed32a1394b [file] [log] [blame]
Chris Masonc8b97812008-10-29 14:49:59 -04001/*
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
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Masonc8b97812008-10-29 14:49:59 -040029#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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050034#include "compat.h"
Chris Masonc8b97812008-10-29 14:49:59 -040035#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"
Chris Masonc8b97812008-10-29 14:49:59 -040041#include "compression.h"
42#include "extent_io.h"
43#include "extent_map.h"
44
45struct compressed_bio {
46 /* number of bios pending for this compressed extent */
47 atomic_t pending_bios;
48
49 /* the pages with the compressed data on them */
50 struct page **compressed_pages;
51
52 /* inode that owns this data */
53 struct inode *inode;
54
55 /* starting offset in the inode for our pages */
56 u64 start;
57
58 /* number of bytes in the inode we're working on */
59 unsigned long len;
60
61 /* number of bytes on disk */
62 unsigned long compressed_len;
63
64 /* number of compressed pages in the array */
65 unsigned long nr_pages;
66
67 /* IO errors */
68 int errors;
Chris Masond20f7042008-12-08 16:58:54 -050069 int mirror_num;
Chris Masonc8b97812008-10-29 14:49:59 -040070
71 /* for reads, this is the bio we are copying the data into */
72 struct bio *orig_bio;
Chris Masond20f7042008-12-08 16:58:54 -050073
74 /*
75 * the start of a variable length array of checksums only
76 * used by reads
77 */
78 u32 sums;
Chris Masonc8b97812008-10-29 14:49:59 -040079};
80
Chris Masond20f7042008-12-08 16:58:54 -050081static inline int compressed_bio_size(struct btrfs_root *root,
82 unsigned long disk_size)
83{
84 u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
85 return sizeof(struct compressed_bio) +
86 ((disk_size + root->sectorsize - 1) / root->sectorsize) *
87 csum_size;
88}
89
Chris Masonc8b97812008-10-29 14:49:59 -040090static struct bio *compressed_bio_alloc(struct block_device *bdev,
91 u64 first_byte, gfp_t gfp_flags)
92{
93 struct bio *bio;
94 int nr_vecs;
95
96 nr_vecs = bio_get_nr_vecs(bdev);
97 bio = bio_alloc(gfp_flags, nr_vecs);
98
99 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
100 while (!bio && (nr_vecs /= 2))
101 bio = bio_alloc(gfp_flags, nr_vecs);
102 }
103
104 if (bio) {
105 bio->bi_size = 0;
106 bio->bi_bdev = bdev;
107 bio->bi_sector = first_byte >> 9;
108 }
109 return bio;
110}
111
Chris Masond20f7042008-12-08 16:58:54 -0500112static int check_compressed_csum(struct inode *inode,
113 struct compressed_bio *cb,
114 u64 disk_start)
115{
116 int ret;
117 struct btrfs_root *root = BTRFS_I(inode)->root;
118 struct page *page;
119 unsigned long i;
120 char *kaddr;
121 u32 csum;
122 u32 *cb_sum = &cb->sums;
123
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200124 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masond20f7042008-12-08 16:58:54 -0500125 return 0;
126
127 for (i = 0; i < cb->nr_pages; i++) {
128 page = cb->compressed_pages[i];
129 csum = ~(u32)0;
130
131 kaddr = kmap_atomic(page, KM_USER0);
132 csum = btrfs_csum_data(root, kaddr, csum, PAGE_CACHE_SIZE);
133 btrfs_csum_final(csum, (char *)&csum);
134 kunmap_atomic(kaddr, KM_USER0);
135
136 if (csum != *cb_sum) {
Chris Masond3977122009-01-05 21:25:51 -0500137 printk(KERN_INFO "btrfs csum failed ino %lu "
138 "extent %llu csum %u "
Chris Masond20f7042008-12-08 16:58:54 -0500139 "wanted %u mirror %d\n", inode->i_ino,
140 (unsigned long long)disk_start,
141 csum, *cb_sum, cb->mirror_num);
142 ret = -EIO;
143 goto fail;
144 }
145 cb_sum++;
146
147 }
148 ret = 0;
149fail:
150 return ret;
151}
152
Chris Masonc8b97812008-10-29 14:49:59 -0400153/* when we finish reading compressed pages from the disk, we
154 * decompress them and then run the bio end_io routines on the
155 * decompressed pages (in the inode address space).
156 *
157 * This allows the checksumming and other IO error handling routines
158 * to work normally
159 *
160 * The compressed pages are freed here, and it must be run
161 * in process context
162 */
163static void end_compressed_bio_read(struct bio *bio, int err)
164{
165 struct extent_io_tree *tree;
166 struct compressed_bio *cb = bio->bi_private;
167 struct inode *inode;
168 struct page *page;
169 unsigned long index;
170 int ret;
171
172 if (err)
173 cb->errors = 1;
174
175 /* if there are more bios still pending for this compressed
176 * extent, just exit
177 */
178 if (!atomic_dec_and_test(&cb->pending_bios))
179 goto out;
180
Chris Masond20f7042008-12-08 16:58:54 -0500181 inode = cb->inode;
182 ret = check_compressed_csum(inode, cb, (u64)bio->bi_sector << 9);
183 if (ret)
184 goto csum_failed;
185
Chris Masonc8b97812008-10-29 14:49:59 -0400186 /* ok, we're the last bio for this extent, lets start
187 * the decompression.
188 */
Chris Masonc8b97812008-10-29 14:49:59 -0400189 tree = &BTRFS_I(inode)->io_tree;
190 ret = btrfs_zlib_decompress_biovec(cb->compressed_pages,
191 cb->start,
192 cb->orig_bio->bi_io_vec,
193 cb->orig_bio->bi_vcnt,
194 cb->compressed_len);
Chris Masond20f7042008-12-08 16:58:54 -0500195csum_failed:
Chris Masonc8b97812008-10-29 14:49:59 -0400196 if (ret)
197 cb->errors = 1;
198
199 /* release the compressed pages */
200 index = 0;
201 for (index = 0; index < cb->nr_pages; index++) {
202 page = cb->compressed_pages[index];
203 page->mapping = NULL;
204 page_cache_release(page);
205 }
206
207 /* do io completion on the original bio */
Chris Mason771ed682008-11-06 22:02:51 -0500208 if (cb->errors) {
Chris Masonc8b97812008-10-29 14:49:59 -0400209 bio_io_error(cb->orig_bio);
Chris Masond20f7042008-12-08 16:58:54 -0500210 } else {
211 int bio_index = 0;
212 struct bio_vec *bvec = cb->orig_bio->bi_io_vec;
213
214 /*
215 * we have verified the checksum already, set page
216 * checked so the end_io handlers know about it
217 */
Chris Masond3977122009-01-05 21:25:51 -0500218 while (bio_index < cb->orig_bio->bi_vcnt) {
Chris Masond20f7042008-12-08 16:58:54 -0500219 SetPageChecked(bvec->bv_page);
220 bvec++;
221 bio_index++;
222 }
Chris Masonc8b97812008-10-29 14:49:59 -0400223 bio_endio(cb->orig_bio, 0);
Chris Masond20f7042008-12-08 16:58:54 -0500224 }
Chris Masonc8b97812008-10-29 14:49:59 -0400225
226 /* finally free the cb struct */
227 kfree(cb->compressed_pages);
228 kfree(cb);
229out:
230 bio_put(bio);
231}
232
233/*
234 * Clear the writeback bits on all of the file
235 * pages for a compressed write
236 */
237static noinline int end_compressed_writeback(struct inode *inode, u64 start,
238 unsigned long ram_size)
239{
240 unsigned long index = start >> PAGE_CACHE_SHIFT;
241 unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
242 struct page *pages[16];
243 unsigned long nr_pages = end_index - index + 1;
244 int i;
245 int ret;
246
Chris Masond3977122009-01-05 21:25:51 -0500247 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400248 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -0500249 min_t(unsigned long,
250 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400251 if (ret == 0) {
252 nr_pages -= 1;
253 index += 1;
254 continue;
255 }
256 for (i = 0; i < ret; i++) {
257 end_page_writeback(pages[i]);
258 page_cache_release(pages[i]);
259 }
260 nr_pages -= ret;
261 index += ret;
262 }
263 /* the inode may be gone now */
264 return 0;
265}
266
267/*
268 * do the cleanup once all the compressed pages hit the disk.
269 * This will clear writeback on the file pages and free the compressed
270 * pages.
271 *
272 * This also calls the writeback end hooks for the file pages so that
273 * metadata and checksums can be updated in the file.
274 */
275static void end_compressed_bio_write(struct bio *bio, int err)
276{
277 struct extent_io_tree *tree;
278 struct compressed_bio *cb = bio->bi_private;
279 struct inode *inode;
280 struct page *page;
281 unsigned long index;
282
283 if (err)
284 cb->errors = 1;
285
286 /* if there are more bios still pending for this compressed
287 * extent, just exit
288 */
289 if (!atomic_dec_and_test(&cb->pending_bios))
290 goto out;
291
292 /* ok, we're the last bio for this extent, step one is to
293 * call back into the FS and do all the end_io operations
294 */
295 inode = cb->inode;
296 tree = &BTRFS_I(inode)->io_tree;
Chris Mason70b99e62008-10-31 12:46:39 -0400297 cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
Chris Masonc8b97812008-10-29 14:49:59 -0400298 tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
299 cb->start,
300 cb->start + cb->len - 1,
301 NULL, 1);
Chris Mason70b99e62008-10-31 12:46:39 -0400302 cb->compressed_pages[0]->mapping = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -0400303
304 end_compressed_writeback(inode, cb->start, cb->len);
305 /* note, our inode could be gone now */
306
307 /*
308 * release the compressed pages, these came from alloc_page and
309 * are not attached to the inode at all
310 */
311 index = 0;
312 for (index = 0; index < cb->nr_pages; index++) {
313 page = cb->compressed_pages[index];
314 page->mapping = NULL;
315 page_cache_release(page);
316 }
317
318 /* finally free the cb struct */
319 kfree(cb->compressed_pages);
320 kfree(cb);
321out:
322 bio_put(bio);
323}
324
325/*
326 * worker function to build and submit bios for previously compressed pages.
327 * The corresponding pages in the inode should be marked for writeback
328 * and the compressed pages should have a reference on them for dropping
329 * when the IO is complete.
330 *
331 * This also checksums the file bytes and gets things ready for
332 * the end io hooks.
333 */
334int btrfs_submit_compressed_write(struct inode *inode, u64 start,
335 unsigned long len, u64 disk_start,
336 unsigned long compressed_len,
337 struct page **compressed_pages,
338 unsigned long nr_pages)
339{
340 struct bio *bio = NULL;
341 struct btrfs_root *root = BTRFS_I(inode)->root;
342 struct compressed_bio *cb;
343 unsigned long bytes_left;
344 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
345 int page_index = 0;
346 struct page *page;
347 u64 first_byte = disk_start;
348 struct block_device *bdev;
349 int ret;
350
351 WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
Chris Masond20f7042008-12-08 16:58:54 -0500352 cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -0400353 atomic_set(&cb->pending_bios, 0);
354 cb->errors = 0;
355 cb->inode = inode;
356 cb->start = start;
357 cb->len = len;
Chris Masond20f7042008-12-08 16:58:54 -0500358 cb->mirror_num = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400359 cb->compressed_pages = compressed_pages;
360 cb->compressed_len = compressed_len;
361 cb->orig_bio = NULL;
362 cb->nr_pages = nr_pages;
363
364 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
365
Chris Masonc8b97812008-10-29 14:49:59 -0400366 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
367 bio->bi_private = cb;
368 bio->bi_end_io = end_compressed_bio_write;
369 atomic_inc(&cb->pending_bios);
370
371 /* create and submit bios for the compressed pages */
372 bytes_left = compressed_len;
Chris Masoncfbc2462008-10-30 13:22:14 -0400373 for (page_index = 0; page_index < cb->nr_pages; page_index++) {
Chris Masonc8b97812008-10-29 14:49:59 -0400374 page = compressed_pages[page_index];
375 page->mapping = inode->i_mapping;
376 if (bio->bi_size)
377 ret = io_tree->ops->merge_bio_hook(page, 0,
378 PAGE_CACHE_SIZE,
379 bio, 0);
380 else
381 ret = 0;
382
Chris Mason70b99e62008-10-31 12:46:39 -0400383 page->mapping = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -0400384 if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
385 PAGE_CACHE_SIZE) {
386 bio_get(bio);
387
Chris Masonaf09abf2008-11-07 12:35:44 -0500388 /*
389 * inc the count before we submit the bio so
390 * we know the end IO handler won't happen before
391 * we inc the count. Otherwise, the cb might get
392 * freed before we're done setting it up
393 */
394 atomic_inc(&cb->pending_bios);
Chris Masonc8b97812008-10-29 14:49:59 -0400395 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
396 BUG_ON(ret);
397
Chris Masond20f7042008-12-08 16:58:54 -0500398 ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
399 BUG_ON(ret);
400
Chris Masonc8b97812008-10-29 14:49:59 -0400401 ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
402 BUG_ON(ret);
403
404 bio_put(bio);
405
406 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -0400407 bio->bi_private = cb;
408 bio->bi_end_io = end_compressed_bio_write;
409 bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
410 }
Chris Masoncfbc2462008-10-30 13:22:14 -0400411 if (bytes_left < PAGE_CACHE_SIZE) {
412 printk("bytes left %lu compress len %lu nr %lu\n",
413 bytes_left, cb->compressed_len, cb->nr_pages);
414 }
Chris Masonc8b97812008-10-29 14:49:59 -0400415 bytes_left -= PAGE_CACHE_SIZE;
416 first_byte += PAGE_CACHE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500417 cond_resched();
Chris Masonc8b97812008-10-29 14:49:59 -0400418 }
419 bio_get(bio);
420
421 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
422 BUG_ON(ret);
423
Chris Masond20f7042008-12-08 16:58:54 -0500424 ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
425 BUG_ON(ret);
426
Chris Masonc8b97812008-10-29 14:49:59 -0400427 ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
428 BUG_ON(ret);
429
430 bio_put(bio);
431 return 0;
432}
433
Chris Mason771ed682008-11-06 22:02:51 -0500434static noinline int add_ra_bio_pages(struct inode *inode,
435 u64 compressed_end,
436 struct compressed_bio *cb)
437{
438 unsigned long end_index;
439 unsigned long page_index;
440 u64 last_offset;
441 u64 isize = i_size_read(inode);
442 int ret;
443 struct page *page;
444 unsigned long nr_pages = 0;
445 struct extent_map *em;
446 struct address_space *mapping = inode->i_mapping;
Chris Mason771ed682008-11-06 22:02:51 -0500447 struct extent_map_tree *em_tree;
448 struct extent_io_tree *tree;
449 u64 end;
450 int misses = 0;
451
452 page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
453 last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
454 em_tree = &BTRFS_I(inode)->extent_tree;
455 tree = &BTRFS_I(inode)->io_tree;
456
457 if (isize == 0)
458 return 0;
459
460 end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
461
Chris Masond3977122009-01-05 21:25:51 -0500462 while (last_offset < compressed_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500463 page_index = last_offset >> PAGE_CACHE_SHIFT;
464
465 if (page_index > end_index)
466 break;
467
468 rcu_read_lock();
469 page = radix_tree_lookup(&mapping->page_tree, page_index);
470 rcu_read_unlock();
471 if (page) {
472 misses++;
473 if (misses > 4)
474 break;
475 goto next;
476 }
477
Nick Piggin28ecb6092010-03-17 13:31:04 +0000478 page = __page_cache_alloc(mapping_gfp_mask(mapping) &
479 ~__GFP_FS);
Chris Mason771ed682008-11-06 22:02:51 -0500480 if (!page)
481 break;
482
Nick Piggin28ecb6092010-03-17 13:31:04 +0000483 if (add_to_page_cache_lru(page, mapping, page_index,
484 GFP_NOFS)) {
Chris Mason771ed682008-11-06 22:02:51 -0500485 page_cache_release(page);
486 goto next;
487 }
488
Chris Mason771ed682008-11-06 22:02:51 -0500489 end = last_offset + PAGE_CACHE_SIZE - 1;
490 /*
491 * at this point, we have a locked page in the page cache
492 * for these bytes in the file. But, we have to make
493 * sure they map to this compressed extent on disk.
494 */
495 set_page_extent_mapped(page);
496 lock_extent(tree, last_offset, end, GFP_NOFS);
Chris Mason890871b2009-09-02 16:24:52 -0400497 read_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500498 em = lookup_extent_mapping(em_tree, last_offset,
499 PAGE_CACHE_SIZE);
Chris Mason890871b2009-09-02 16:24:52 -0400500 read_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500501
502 if (!em || last_offset < em->start ||
503 (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
504 (em->block_start >> 9) != cb->orig_bio->bi_sector) {
505 free_extent_map(em);
506 unlock_extent(tree, last_offset, end, GFP_NOFS);
507 unlock_page(page);
508 page_cache_release(page);
509 break;
510 }
511 free_extent_map(em);
512
513 if (page->index == end_index) {
514 char *userpage;
515 size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
516
517 if (zero_offset) {
518 int zeros;
519 zeros = PAGE_CACHE_SIZE - zero_offset;
520 userpage = kmap_atomic(page, KM_USER0);
521 memset(userpage + zero_offset, 0, zeros);
522 flush_dcache_page(page);
523 kunmap_atomic(userpage, KM_USER0);
524 }
525 }
526
527 ret = bio_add_page(cb->orig_bio, page,
528 PAGE_CACHE_SIZE, 0);
529
530 if (ret == PAGE_CACHE_SIZE) {
531 nr_pages++;
532 page_cache_release(page);
533 } else {
534 unlock_extent(tree, last_offset, end, GFP_NOFS);
535 unlock_page(page);
536 page_cache_release(page);
537 break;
538 }
539next:
540 last_offset += PAGE_CACHE_SIZE;
541 }
Chris Mason771ed682008-11-06 22:02:51 -0500542 return 0;
543}
544
Chris Masonc8b97812008-10-29 14:49:59 -0400545/*
546 * for a compressed read, the bio we get passed has all the inode pages
547 * in it. We don't actually do IO on those pages but allocate new ones
548 * to hold the compressed pages on disk.
549 *
550 * bio->bi_sector points to the compressed extent on disk
551 * bio->bi_io_vec points to all of the inode pages
552 * bio->bi_vcnt is a count of pages
553 *
554 * After the compressed pages are read, we copy the bytes into the
555 * bio we were passed and then call the bio end_io calls
556 */
557int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
558 int mirror_num, unsigned long bio_flags)
559{
560 struct extent_io_tree *tree;
561 struct extent_map_tree *em_tree;
562 struct compressed_bio *cb;
563 struct btrfs_root *root = BTRFS_I(inode)->root;
564 unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
565 unsigned long compressed_len;
566 unsigned long nr_pages;
567 unsigned long page_index;
568 struct page *page;
569 struct block_device *bdev;
570 struct bio *comp_bio;
571 u64 cur_disk_byte = (u64)bio->bi_sector << 9;
Chris Masone04ca622008-11-10 11:44:58 -0500572 u64 em_len;
573 u64 em_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400574 struct extent_map *em;
575 int ret;
Chris Masond20f7042008-12-08 16:58:54 -0500576 u32 *sums;
Chris Masonc8b97812008-10-29 14:49:59 -0400577
578 tree = &BTRFS_I(inode)->io_tree;
579 em_tree = &BTRFS_I(inode)->extent_tree;
580
581 /* we need the actual starting offset of this extent in the file */
Chris Mason890871b2009-09-02 16:24:52 -0400582 read_lock(&em_tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -0400583 em = lookup_extent_mapping(em_tree,
584 page_offset(bio->bi_io_vec->bv_page),
585 PAGE_CACHE_SIZE);
Chris Mason890871b2009-09-02 16:24:52 -0400586 read_unlock(&em_tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -0400587
Chris Masond20f7042008-12-08 16:58:54 -0500588 compressed_len = em->block_len;
589 cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -0400590 atomic_set(&cb->pending_bios, 0);
591 cb->errors = 0;
592 cb->inode = inode;
Chris Masond20f7042008-12-08 16:58:54 -0500593 cb->mirror_num = mirror_num;
594 sums = &cb->sums;
Chris Masonc8b97812008-10-29 14:49:59 -0400595
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500596 cb->start = em->orig_start;
Chris Masone04ca622008-11-10 11:44:58 -0500597 em_len = em->len;
598 em_start = em->start;
Chris Masond20f7042008-12-08 16:58:54 -0500599
Chris Masonc8b97812008-10-29 14:49:59 -0400600 free_extent_map(em);
Chris Masone04ca622008-11-10 11:44:58 -0500601 em = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -0400602
603 cb->len = uncompressed_len;
604 cb->compressed_len = compressed_len;
605 cb->orig_bio = bio;
606
607 nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
608 PAGE_CACHE_SIZE;
609 cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
610 GFP_NOFS);
611 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
612
613 for (page_index = 0; page_index < nr_pages; page_index++) {
614 cb->compressed_pages[page_index] = alloc_page(GFP_NOFS |
615 __GFP_HIGHMEM);
616 }
617 cb->nr_pages = nr_pages;
618
Chris Masone04ca622008-11-10 11:44:58 -0500619 add_ra_bio_pages(inode, em_start + em_len, cb);
Chris Mason771ed682008-11-06 22:02:51 -0500620
Chris Mason771ed682008-11-06 22:02:51 -0500621 /* include any pages we added in add_ra-bio_pages */
622 uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
623 cb->len = uncompressed_len;
624
Chris Masonc8b97812008-10-29 14:49:59 -0400625 comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
626 comp_bio->bi_private = cb;
627 comp_bio->bi_end_io = end_compressed_bio_read;
628 atomic_inc(&cb->pending_bios);
629
630 for (page_index = 0; page_index < nr_pages; page_index++) {
631 page = cb->compressed_pages[page_index];
632 page->mapping = inode->i_mapping;
Chris Masond20f7042008-12-08 16:58:54 -0500633 page->index = em_start >> PAGE_CACHE_SHIFT;
634
Chris Masonc8b97812008-10-29 14:49:59 -0400635 if (comp_bio->bi_size)
636 ret = tree->ops->merge_bio_hook(page, 0,
637 PAGE_CACHE_SIZE,
638 comp_bio, 0);
639 else
640 ret = 0;
641
Chris Mason70b99e62008-10-31 12:46:39 -0400642 page->mapping = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -0400643 if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
644 PAGE_CACHE_SIZE) {
645 bio_get(comp_bio);
646
647 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
648 BUG_ON(ret);
649
Chris Masonaf09abf2008-11-07 12:35:44 -0500650 /*
651 * inc the count before we submit the bio so
652 * we know the end IO handler won't happen before
653 * we inc the count. Otherwise, the cb might get
654 * freed before we're done setting it up
655 */
656 atomic_inc(&cb->pending_bios);
657
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200658 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
Chris Masond20f7042008-12-08 16:58:54 -0500659 btrfs_lookup_bio_sums(root, inode, comp_bio,
660 sums);
661 }
662 sums += (comp_bio->bi_size + root->sectorsize - 1) /
663 root->sectorsize;
664
665 ret = btrfs_map_bio(root, READ, comp_bio,
666 mirror_num, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400667 BUG_ON(ret);
668
669 bio_put(comp_bio);
670
671 comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
672 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500673 comp_bio->bi_private = cb;
674 comp_bio->bi_end_io = end_compressed_bio_read;
675
676 bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400677 }
678 cur_disk_byte += PAGE_CACHE_SIZE;
679 }
680 bio_get(comp_bio);
681
682 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
683 BUG_ON(ret);
684
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200685 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
Chris Masond20f7042008-12-08 16:58:54 -0500686 btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
Chris Masond20f7042008-12-08 16:58:54 -0500687
688 ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400689 BUG_ON(ret);
690
691 bio_put(comp_bio);
692 return 0;
693}