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