2 * Copyright (C) 2008 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.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/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/slab.h>
38 #include "transaction.h"
39 #include "btrfs_inode.h"
41 #include "ordered-data.h"
42 #include "compression.h"
43 #include "extent_io.h"
44 #include "extent_map.h"
46 struct compressed_bio
{
47 /* number of bios pending for this compressed extent */
48 atomic_t pending_bios
;
50 /* the pages with the compressed data on them */
51 struct page
**compressed_pages
;
53 /* inode that owns this data */
56 /* starting offset in the inode for our pages */
59 /* number of bytes in the inode we're working on */
62 /* number of bytes on disk */
63 unsigned long compressed_len
;
65 /* number of compressed pages in the array */
66 unsigned long nr_pages
;
72 /* for reads, this is the bio we are copying the data into */
76 * the start of a variable length array of checksums only
82 static inline int compressed_bio_size(struct btrfs_root
*root
,
83 unsigned long disk_size
)
85 u16 csum_size
= btrfs_super_csum_size(&root
->fs_info
->super_copy
);
86 return sizeof(struct compressed_bio
) +
87 ((disk_size
+ root
->sectorsize
- 1) / root
->sectorsize
) *
91 static struct bio
*compressed_bio_alloc(struct block_device
*bdev
,
92 u64 first_byte
, gfp_t gfp_flags
)
97 nr_vecs
= bio_get_nr_vecs(bdev
);
98 bio
= bio_alloc(gfp_flags
, nr_vecs
);
100 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
101 while (!bio
&& (nr_vecs
/= 2))
102 bio
= bio_alloc(gfp_flags
, nr_vecs
);
108 bio
->bi_sector
= first_byte
>> 9;
113 static int check_compressed_csum(struct inode
*inode
,
114 struct compressed_bio
*cb
,
118 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
123 u32
*cb_sum
= &cb
->sums
;
125 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
128 for (i
= 0; i
< cb
->nr_pages
; i
++) {
129 page
= cb
->compressed_pages
[i
];
132 kaddr
= kmap_atomic(page
, KM_USER0
);
133 csum
= btrfs_csum_data(root
, kaddr
, csum
, PAGE_CACHE_SIZE
);
134 btrfs_csum_final(csum
, (char *)&csum
);
135 kunmap_atomic(kaddr
, KM_USER0
);
137 if (csum
!= *cb_sum
) {
138 printk(KERN_INFO
"btrfs csum failed ino %lu "
139 "extent %llu csum %u "
140 "wanted %u mirror %d\n", inode
->i_ino
,
141 (unsigned long long)disk_start
,
142 csum
, *cb_sum
, cb
->mirror_num
);
154 /* when we finish reading compressed pages from the disk, we
155 * decompress them and then run the bio end_io routines on the
156 * decompressed pages (in the inode address space).
158 * This allows the checksumming and other IO error handling routines
161 * The compressed pages are freed here, and it must be run
164 static void end_compressed_bio_read(struct bio
*bio
, int err
)
166 struct compressed_bio
*cb
= bio
->bi_private
;
175 /* if there are more bios still pending for this compressed
178 if (!atomic_dec_and_test(&cb
->pending_bios
))
182 ret
= check_compressed_csum(inode
, cb
, (u64
)bio
->bi_sector
<< 9);
186 /* ok, we're the last bio for this extent, lets start
189 ret
= btrfs_zlib_decompress_biovec(cb
->compressed_pages
,
191 cb
->orig_bio
->bi_io_vec
,
192 cb
->orig_bio
->bi_vcnt
,
198 /* release the compressed pages */
200 for (index
= 0; index
< cb
->nr_pages
; index
++) {
201 page
= cb
->compressed_pages
[index
];
202 page
->mapping
= NULL
;
203 page_cache_release(page
);
206 /* do io completion on the original bio */
208 bio_io_error(cb
->orig_bio
);
211 struct bio_vec
*bvec
= cb
->orig_bio
->bi_io_vec
;
214 * we have verified the checksum already, set page
215 * checked so the end_io handlers know about it
217 while (bio_index
< cb
->orig_bio
->bi_vcnt
) {
218 SetPageChecked(bvec
->bv_page
);
222 bio_endio(cb
->orig_bio
, 0);
225 /* finally free the cb struct */
226 kfree(cb
->compressed_pages
);
233 * Clear the writeback bits on all of the file
234 * pages for a compressed write
236 static noinline
int end_compressed_writeback(struct inode
*inode
, u64 start
,
237 unsigned long ram_size
)
239 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
240 unsigned long end_index
= (start
+ ram_size
- 1) >> PAGE_CACHE_SHIFT
;
241 struct page
*pages
[16];
242 unsigned long nr_pages
= end_index
- index
+ 1;
246 while (nr_pages
> 0) {
247 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
249 nr_pages
, ARRAY_SIZE(pages
)), pages
);
255 for (i
= 0; i
< ret
; i
++) {
256 end_page_writeback(pages
[i
]);
257 page_cache_release(pages
[i
]);
262 /* the inode may be gone now */
267 * do the cleanup once all the compressed pages hit the disk.
268 * This will clear writeback on the file pages and free the compressed
271 * This also calls the writeback end hooks for the file pages so that
272 * metadata and checksums can be updated in the file.
274 static void end_compressed_bio_write(struct bio
*bio
, int err
)
276 struct extent_io_tree
*tree
;
277 struct compressed_bio
*cb
= bio
->bi_private
;
285 /* if there are more bios still pending for this compressed
288 if (!atomic_dec_and_test(&cb
->pending_bios
))
291 /* ok, we're the last bio for this extent, step one is to
292 * call back into the FS and do all the end_io operations
295 tree
= &BTRFS_I(inode
)->io_tree
;
296 cb
->compressed_pages
[0]->mapping
= cb
->inode
->i_mapping
;
297 tree
->ops
->writepage_end_io_hook(cb
->compressed_pages
[0],
299 cb
->start
+ cb
->len
- 1,
301 cb
->compressed_pages
[0]->mapping
= NULL
;
303 end_compressed_writeback(inode
, cb
->start
, cb
->len
);
304 /* note, our inode could be gone now */
307 * release the compressed pages, these came from alloc_page and
308 * are not attached to the inode at all
311 for (index
= 0; index
< cb
->nr_pages
; index
++) {
312 page
= cb
->compressed_pages
[index
];
313 page
->mapping
= NULL
;
314 page_cache_release(page
);
317 /* finally free the cb struct */
318 kfree(cb
->compressed_pages
);
325 * worker function to build and submit bios for previously compressed pages.
326 * The corresponding pages in the inode should be marked for writeback
327 * and the compressed pages should have a reference on them for dropping
328 * when the IO is complete.
330 * This also checksums the file bytes and gets things ready for
333 int btrfs_submit_compressed_write(struct inode
*inode
, u64 start
,
334 unsigned long len
, u64 disk_start
,
335 unsigned long compressed_len
,
336 struct page
**compressed_pages
,
337 unsigned long nr_pages
)
339 struct bio
*bio
= NULL
;
340 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
341 struct compressed_bio
*cb
;
342 unsigned long bytes_left
;
343 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
346 u64 first_byte
= disk_start
;
347 struct block_device
*bdev
;
350 WARN_ON(start
& ((u64
)PAGE_CACHE_SIZE
- 1));
351 cb
= kmalloc(compressed_bio_size(root
, compressed_len
), GFP_NOFS
);
352 atomic_set(&cb
->pending_bios
, 0);
358 cb
->compressed_pages
= compressed_pages
;
359 cb
->compressed_len
= compressed_len
;
361 cb
->nr_pages
= nr_pages
;
363 bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
365 bio
= compressed_bio_alloc(bdev
, first_byte
, GFP_NOFS
);
366 bio
->bi_private
= cb
;
367 bio
->bi_end_io
= end_compressed_bio_write
;
368 atomic_inc(&cb
->pending_bios
);
370 /* create and submit bios for the compressed pages */
371 bytes_left
= compressed_len
;
372 for (page_index
= 0; page_index
< cb
->nr_pages
; page_index
++) {
373 page
= compressed_pages
[page_index
];
374 page
->mapping
= inode
->i_mapping
;
376 ret
= io_tree
->ops
->merge_bio_hook(page
, 0,
382 page
->mapping
= NULL
;
383 if (ret
|| bio_add_page(bio
, page
, PAGE_CACHE_SIZE
, 0) <
388 * inc the count before we submit the bio so
389 * we know the end IO handler won't happen before
390 * we inc the count. Otherwise, the cb might get
391 * freed before we're done setting it up
393 atomic_inc(&cb
->pending_bios
);
394 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
397 ret
= btrfs_csum_one_bio(root
, inode
, bio
, start
, 1);
400 ret
= btrfs_map_bio(root
, WRITE
, bio
, 0, 1);
405 bio
= compressed_bio_alloc(bdev
, first_byte
, GFP_NOFS
);
406 bio
->bi_private
= cb
;
407 bio
->bi_end_io
= end_compressed_bio_write
;
408 bio_add_page(bio
, page
, PAGE_CACHE_SIZE
, 0);
410 if (bytes_left
< PAGE_CACHE_SIZE
) {
411 printk("bytes left %lu compress len %lu nr %lu\n",
412 bytes_left
, cb
->compressed_len
, cb
->nr_pages
);
414 bytes_left
-= PAGE_CACHE_SIZE
;
415 first_byte
+= PAGE_CACHE_SIZE
;
420 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
423 ret
= btrfs_csum_one_bio(root
, inode
, bio
, start
, 1);
426 ret
= btrfs_map_bio(root
, WRITE
, bio
, 0, 1);
433 static noinline
int add_ra_bio_pages(struct inode
*inode
,
435 struct compressed_bio
*cb
)
437 unsigned long end_index
;
438 unsigned long page_index
;
440 u64 isize
= i_size_read(inode
);
443 unsigned long nr_pages
= 0;
444 struct extent_map
*em
;
445 struct address_space
*mapping
= inode
->i_mapping
;
446 struct extent_map_tree
*em_tree
;
447 struct extent_io_tree
*tree
;
451 page
= cb
->orig_bio
->bi_io_vec
[cb
->orig_bio
->bi_vcnt
- 1].bv_page
;
452 last_offset
= (page_offset(page
) + PAGE_CACHE_SIZE
);
453 em_tree
= &BTRFS_I(inode
)->extent_tree
;
454 tree
= &BTRFS_I(inode
)->io_tree
;
459 end_index
= (i_size_read(inode
) - 1) >> PAGE_CACHE_SHIFT
;
461 while (last_offset
< compressed_end
) {
462 page_index
= last_offset
>> PAGE_CACHE_SHIFT
;
464 if (page_index
> end_index
)
468 page
= radix_tree_lookup(&mapping
->page_tree
, page_index
);
477 page
= __page_cache_alloc(mapping_gfp_mask(mapping
) &
482 if (add_to_page_cache_lru(page
, mapping
, page_index
,
484 page_cache_release(page
);
488 end
= last_offset
+ PAGE_CACHE_SIZE
- 1;
490 * at this point, we have a locked page in the page cache
491 * for these bytes in the file. But, we have to make
492 * sure they map to this compressed extent on disk.
494 set_page_extent_mapped(page
);
495 lock_extent(tree
, last_offset
, end
, GFP_NOFS
);
496 read_lock(&em_tree
->lock
);
497 em
= lookup_extent_mapping(em_tree
, last_offset
,
499 read_unlock(&em_tree
->lock
);
501 if (!em
|| last_offset
< em
->start
||
502 (last_offset
+ PAGE_CACHE_SIZE
> extent_map_end(em
)) ||
503 (em
->block_start
>> 9) != cb
->orig_bio
->bi_sector
) {
505 unlock_extent(tree
, last_offset
, end
, GFP_NOFS
);
507 page_cache_release(page
);
512 if (page
->index
== end_index
) {
514 size_t zero_offset
= isize
& (PAGE_CACHE_SIZE
- 1);
518 zeros
= PAGE_CACHE_SIZE
- zero_offset
;
519 userpage
= kmap_atomic(page
, KM_USER0
);
520 memset(userpage
+ zero_offset
, 0, zeros
);
521 flush_dcache_page(page
);
522 kunmap_atomic(userpage
, KM_USER0
);
526 ret
= bio_add_page(cb
->orig_bio
, page
,
529 if (ret
== PAGE_CACHE_SIZE
) {
531 page_cache_release(page
);
533 unlock_extent(tree
, last_offset
, end
, GFP_NOFS
);
535 page_cache_release(page
);
539 last_offset
+= PAGE_CACHE_SIZE
;
545 * for a compressed read, the bio we get passed has all the inode pages
546 * in it. We don't actually do IO on those pages but allocate new ones
547 * to hold the compressed pages on disk.
549 * bio->bi_sector points to the compressed extent on disk
550 * bio->bi_io_vec points to all of the inode pages
551 * bio->bi_vcnt is a count of pages
553 * After the compressed pages are read, we copy the bytes into the
554 * bio we were passed and then call the bio end_io calls
556 int btrfs_submit_compressed_read(struct inode
*inode
, struct bio
*bio
,
557 int mirror_num
, unsigned long bio_flags
)
559 struct extent_io_tree
*tree
;
560 struct extent_map_tree
*em_tree
;
561 struct compressed_bio
*cb
;
562 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
563 unsigned long uncompressed_len
= bio
->bi_vcnt
* PAGE_CACHE_SIZE
;
564 unsigned long compressed_len
;
565 unsigned long nr_pages
;
566 unsigned long page_index
;
568 struct block_device
*bdev
;
569 struct bio
*comp_bio
;
570 u64 cur_disk_byte
= (u64
)bio
->bi_sector
<< 9;
573 struct extent_map
*em
;
577 tree
= &BTRFS_I(inode
)->io_tree
;
578 em_tree
= &BTRFS_I(inode
)->extent_tree
;
580 /* we need the actual starting offset of this extent in the file */
581 read_lock(&em_tree
->lock
);
582 em
= lookup_extent_mapping(em_tree
,
583 page_offset(bio
->bi_io_vec
->bv_page
),
585 read_unlock(&em_tree
->lock
);
587 compressed_len
= em
->block_len
;
588 cb
= kmalloc(compressed_bio_size(root
, compressed_len
), GFP_NOFS
);
589 atomic_set(&cb
->pending_bios
, 0);
592 cb
->mirror_num
= mirror_num
;
595 cb
->start
= em
->orig_start
;
597 em_start
= em
->start
;
602 cb
->len
= uncompressed_len
;
603 cb
->compressed_len
= compressed_len
;
606 nr_pages
= (compressed_len
+ PAGE_CACHE_SIZE
- 1) /
608 cb
->compressed_pages
= kmalloc(sizeof(struct page
*) * nr_pages
,
610 bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
612 for (page_index
= 0; page_index
< nr_pages
; page_index
++) {
613 cb
->compressed_pages
[page_index
] = alloc_page(GFP_NOFS
|
616 cb
->nr_pages
= nr_pages
;
618 add_ra_bio_pages(inode
, em_start
+ em_len
, cb
);
620 /* include any pages we added in add_ra-bio_pages */
621 uncompressed_len
= bio
->bi_vcnt
* PAGE_CACHE_SIZE
;
622 cb
->len
= uncompressed_len
;
624 comp_bio
= compressed_bio_alloc(bdev
, cur_disk_byte
, GFP_NOFS
);
625 comp_bio
->bi_private
= cb
;
626 comp_bio
->bi_end_io
= end_compressed_bio_read
;
627 atomic_inc(&cb
->pending_bios
);
629 for (page_index
= 0; page_index
< nr_pages
; page_index
++) {
630 page
= cb
->compressed_pages
[page_index
];
631 page
->mapping
= inode
->i_mapping
;
632 page
->index
= em_start
>> PAGE_CACHE_SHIFT
;
634 if (comp_bio
->bi_size
)
635 ret
= tree
->ops
->merge_bio_hook(page
, 0,
641 page
->mapping
= NULL
;
642 if (ret
|| bio_add_page(comp_bio
, page
, PAGE_CACHE_SIZE
, 0) <
646 ret
= btrfs_bio_wq_end_io(root
->fs_info
, comp_bio
, 0);
650 * inc the count before we submit the bio so
651 * we know the end IO handler won't happen before
652 * we inc the count. Otherwise, the cb might get
653 * freed before we're done setting it up
655 atomic_inc(&cb
->pending_bios
);
657 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
658 btrfs_lookup_bio_sums(root
, inode
, comp_bio
,
661 sums
+= (comp_bio
->bi_size
+ root
->sectorsize
- 1) /
664 ret
= btrfs_map_bio(root
, READ
, comp_bio
,
670 comp_bio
= compressed_bio_alloc(bdev
, cur_disk_byte
,
672 comp_bio
->bi_private
= cb
;
673 comp_bio
->bi_end_io
= end_compressed_bio_read
;
675 bio_add_page(comp_bio
, page
, PAGE_CACHE_SIZE
, 0);
677 cur_disk_byte
+= PAGE_CACHE_SIZE
;
681 ret
= btrfs_bio_wq_end_io(root
->fs_info
, comp_bio
, 0);
684 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
))
685 btrfs_lookup_bio_sums(root
, inode
, comp_bio
, sums
);
687 ret
= btrfs_map_bio(root
, READ
, comp_bio
, mirror_num
, 0);
This page took 0.076818 seconds and 6 git commands to generate.