19 #include <linux/kernel.h>
26 #include <linux/time.h>
28 #include <linux/string.h>
34 #include <linux/slab.h>
85 static inline int compressed_bio_size(
struct btrfs_root *root,
86 unsigned long disk_size)
88 u16 csum_size = btrfs_super_csum_size(root->
fs_info->super_copy);
91 ((disk_size + root->sectorsize - 1) / root->sectorsize) *
95 static struct bio *compressed_bio_alloc(
struct block_device *bdev,
104 static int check_compressed_csum(
struct inode *
inode,
119 for (i = 0; i < cb->
nr_pages; i++) {
128 if (csum != *cb_sum) {
130 "extent %llu csum %u "
131 "wanted %u mirror %d\n",
132 (
unsigned long long)btrfs_ino(inode),
133 (
unsigned long long)disk_start,
156 static void end_compressed_bio_read(
struct bio *bio,
int err)
174 ret = check_compressed_csum(inode, cb, (
u64)bio->bi_sector << 9);
193 for (index = 0; index < cb->
nr_pages; index++) {
204 struct bio_vec *bvec = cb->
orig_bio->bi_io_vec;
210 while (bio_index < cb->orig_bio->bi_vcnt) {
211 SetPageChecked(bvec->bv_page);
229 static noinline void end_compressed_writeback(
struct inode *inode,
u64 start,
233 unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
234 struct page *
pages[16];
235 unsigned long nr_pages = end_index - index + 1;
239 while (nr_pages > 0) {
248 for (i = 0; i <
ret; i++) {
266 static void end_compressed_bio_write(
struct bio *bio,
int err)
287 tree = &BTRFS_I(inode)->io_tree;
295 end_compressed_writeback(inode, cb->
start, cb->
len);
303 for (index = 0; index < cb->
nr_pages; index++) {
326 unsigned long len,
u64 disk_start,
327 unsigned long compressed_len,
328 struct page **compressed_pages,
329 unsigned long nr_pages)
331 struct bio *bio =
NULL;
332 struct btrfs_root *root = BTRFS_I(inode)->root;
334 unsigned long bytes_left;
338 u64 first_byte = disk_start;
358 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
360 bio = compressed_bio_alloc(bdev, first_byte,
GFP_NOFS);
365 bio->bi_private = cb;
366 bio->bi_end_io = end_compressed_bio_write;
370 bytes_left = compressed_len;
371 for (pg_index = 0; pg_index < cb->
nr_pages; pg_index++) {
372 page = compressed_pages[pg_index];
375 ret = io_tree->
ops->merge_bio_hook(page, 0,
407 bio = compressed_bio_alloc(bdev, first_byte,
GFP_NOFS);
409 bio->bi_private = cb;
410 bio->bi_end_io = end_compressed_bio_write;
414 printk(
"bytes left %lu compress len %lu nr %lu\n",
438 static noinline int add_ra_bio_pages(
struct inode *inode,
442 unsigned long end_index;
443 unsigned long pg_index;
445 u64 isize = i_size_read(inode);
448 unsigned long nr_pages = 0;
458 em_tree = &BTRFS_I(inode)->extent_tree;
459 tree = &BTRFS_I(inode)->io_tree;
464 end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
466 while (last_offset < compressed_end) {
469 if (pg_index > end_index)
482 page = __page_cache_alloc(mapping_gfp_mask(mapping) &
506 if (!em || last_offset < em->start ||
507 (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
517 if (page->
index == end_index) {
519 size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
523 zeros = PAGE_CACHE_SIZE - zero_offset;
525 memset(userpage + zero_offset, 0, zeros);
534 if (ret == PAGE_CACHE_SIZE) {
562 int mirror_num,
unsigned long bio_flags)
567 struct btrfs_root *root = BTRFS_I(inode)->root;
569 unsigned long compressed_len;
570 unsigned long nr_pages;
571 unsigned long pg_index;
574 struct bio *comp_bio;
575 u64 cur_disk_byte = (
u64)bio->bi_sector << 9;
583 tree = &BTRFS_I(inode)->io_tree;
584 em_tree = &BTRFS_I(inode)->extent_tree;
608 em_start = em->
start;
613 cb->
len = uncompressed_len;
618 nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
625 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
627 for (pg_index = 0; pg_index < nr_pages; pg_index++) {
631 faili = pg_index - 1;
636 faili = nr_pages - 1;
639 add_ra_bio_pages(inode, em_start + em_len, cb);
643 cb->
len = uncompressed_len;
645 comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
GFP_NOFS);
648 comp_bio->bi_private = cb;
649 comp_bio->bi_end_io = end_compressed_bio_read;
652 for (pg_index = 0; pg_index < nr_pages; pg_index++) {
657 if (comp_bio->bi_size)
658 ret = tree->
ops->merge_bio_hook(page, 0,
665 if (ret ||
bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
680 if (!(BTRFS_I(inode)->
flags & BTRFS_INODE_NODATASUM)) {
685 sums += (comp_bio->bi_size + root->
sectorsize - 1) /
694 comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
697 comp_bio->bi_private = cb;
698 comp_bio->bi_end_io = end_compressed_bio_read;
709 if (!(BTRFS_I(inode)->
flags & BTRFS_INODE_NODATASUM)) {
750 INIT_LIST_HEAD(&comp_idle_workspace[i]);
767 struct list_head *idle_workspace = &comp_idle_workspace[
idx];
769 atomic_t *alloc_workspace = &comp_alloc_workspace[
idx];
771 int *num_workspace = &comp_num_workspace[
idx];
773 spin_lock(workspace_lock);
774 if (!list_empty(idle_workspace)) {
775 workspace = idle_workspace->
next;
778 spin_unlock(workspace_lock);
785 spin_unlock(workspace_lock);
787 if (
atomic_read(alloc_workspace) > cpus && !*num_workspace)
793 spin_unlock(workspace_lock);
796 if (IS_ERR(workspace)) {
807 static void free_workspace(
int type,
struct list_head *workspace)
810 struct list_head *idle_workspace = &comp_idle_workspace[
idx];
812 atomic_t *alloc_workspace = &comp_alloc_workspace[
idx];
814 int *num_workspace = &comp_num_workspace[
idx];
816 spin_lock(workspace_lock);
820 spin_unlock(workspace_lock);
823 spin_unlock(workspace_lock);
829 if (waitqueue_active(workspace_wait))
842 while (!list_empty(&comp_idle_workspace[i])) {
843 workspace = comp_idle_workspace[
i].next;
871 u64 start,
unsigned long len,
873 unsigned long nr_dest_pages,
874 unsigned long *out_pages,
875 unsigned long *total_in,
876 unsigned long *total_out,
877 unsigned long max_out)
882 workspace = find_workspace(type);
883 if (IS_ERR(workspace))
886 ret = btrfs_compress_op[type-1]->
compress_pages(workspace, mapping,
888 nr_dest_pages, out_pages,
891 free_workspace(type, workspace);
912 struct bio_vec *bvec,
int vcnt,
size_t srclen)
917 workspace = find_workspace(type);
918 if (IS_ERR(workspace))
924 free_workspace(type, workspace);
934 unsigned long start_byte,
size_t srclen,
size_t destlen)
939 workspace = find_workspace(type);
940 if (IS_ERR(workspace))
943 ret = btrfs_compress_op[type-1]->
decompress(workspace, data_in,
944 dest_page, start_byte,
947 free_workspace(type, workspace);
964 unsigned long total_out,
u64 disk_start,
965 struct bio_vec *bvec,
int vcnt,
966 unsigned long *pg_index,
967 unsigned long *pg_offset)
969 unsigned long buf_offset;
970 unsigned long current_buf_start;
971 unsigned long start_byte;
972 unsigned long working_bytes = total_out - buf_start;
975 struct page *page_out = bvec[*pg_index].bv_page;
984 if (total_out <= start_byte)
991 if (total_out > start_byte && buf_start < start_byte) {
992 buf_offset = start_byte - buf_start;
993 working_bytes -= buf_offset;
997 current_buf_start = buf_start;
1000 while (working_bytes > 0) {
1001 bytes =
min(PAGE_CACHE_SIZE - *pg_offset,
1002 PAGE_CACHE_SIZE - buf_offset);
1003 bytes =
min(bytes, working_bytes);
1005 memcpy(kaddr + *pg_offset, buf + buf_offset, bytes);
1009 *pg_offset +=
bytes;
1010 buf_offset +=
bytes;
1011 working_bytes -=
bytes;
1012 current_buf_start +=
bytes;
1015 if (*pg_offset == PAGE_CACHE_SIZE) {
1017 if (*pg_index >= vcnt)
1020 page_out = bvec[*pg_index].bv_page;
1028 if (total_out <= start_byte)
1036 if (total_out > start_byte &&
1037 current_buf_start < start_byte) {
1038 buf_offset = start_byte - buf_start;
1039 working_bytes = total_out - start_byte;
1040 current_buf_start = buf_start + buf_offset;