Skip to content

Commit 261507a

Browse files
author
Li Zefan
committed
btrfs: Allow to add new compression algorithm
Make the code aware of compression type, instead of always assuming zlib compression. Also make the zlib workspace function as common code for all compression types. Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
1 parent 4b72029 commit 261507a

15 files changed

Lines changed: 473 additions & 282 deletions

‎fs/btrfs/btrfs_inode.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct btrfs_inode {
157157
/*
158158
* always compress this one file
159159
*/
160-
unsigned force_compress:1;
160+
unsigned force_compress:4;
161161

162162
struct inode vfs_inode;
163163
};

‎fs/btrfs/compression.c‎

Lines changed: 231 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ struct compressed_bio {
6262
/* number of bytes on disk */
6363
unsigned long compressed_len;
6464

65+
/* the compression algorithm for this bio */
66+
int compress_type;
67+
6568
/* number of compressed pages in the array */
6669
unsigned long nr_pages;
6770

@@ -173,11 +176,12 @@ static void end_compressed_bio_read(struct bio *bio, int err)
173176
/* ok, we're the last bio for this extent, lets start
174177
* the decompression.
175178
*/
176-
ret = btrfs_zlib_decompress_biovec(cb->compressed_pages,
177-
cb->start,
178-
cb->orig_bio->bi_io_vec,
179-
cb->orig_bio->bi_vcnt,
180-
cb->compressed_len);
179+
ret = btrfs_decompress_biovec(cb->compress_type,
180+
cb->compressed_pages,
181+
cb->start,
182+
cb->orig_bio->bi_io_vec,
183+
cb->orig_bio->bi_vcnt,
184+
cb->compressed_len);
181185
csum_failed:
182186
if (ret)
183187
cb->errors = 1;
@@ -588,6 +592,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
588592

589593
cb->len = uncompressed_len;
590594
cb->compressed_len = compressed_len;
595+
cb->compress_type = extent_compress_type(bio_flags);
591596
cb->orig_bio = bio;
592597

593598
nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
@@ -677,3 +682,224 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
677682
bio_put(comp_bio);
678683
return 0;
679684
}
685+
686+
static struct list_head comp_idle_workspace[BTRFS_COMPRESS_TYPES];
687+
static spinlock_t comp_workspace_lock[BTRFS_COMPRESS_TYPES];
688+
static int comp_num_workspace[BTRFS_COMPRESS_TYPES];
689+
static atomic_t comp_alloc_workspace[BTRFS_COMPRESS_TYPES];
690+
static wait_queue_head_t comp_workspace_wait[BTRFS_COMPRESS_TYPES];
691+
692+
struct btrfs_compress_op *btrfs_compress_op[] = {
693+
&btrfs_zlib_compress,
694+
};
695+
696+
int __init btrfs_init_compress(void)
697+
{
698+
int i;
699+
700+
for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
701+
INIT_LIST_HEAD(&comp_idle_workspace[i]);
702+
spin_lock_init(&comp_workspace_lock[i]);
703+
atomic_set(&comp_alloc_workspace[i], 0);
704+
init_waitqueue_head(&comp_workspace_wait[i]);
705+
}
706+
return 0;
707+
}
708+
709+
/*
710+
* this finds an available workspace or allocates a new one
711+
* ERR_PTR is returned if things go bad.
712+
*/
713+
static struct list_head *find_workspace(int type)
714+
{
715+
struct list_head *workspace;
716+
int cpus = num_online_cpus();
717+
int idx = type - 1;
718+
719+
struct list_head *idle_workspace = &comp_idle_workspace[idx];
720+
spinlock_t *workspace_lock = &comp_workspace_lock[idx];
721+
atomic_t *alloc_workspace = &comp_alloc_workspace[idx];
722+
wait_queue_head_t *workspace_wait = &comp_workspace_wait[idx];
723+
int *num_workspace = &comp_num_workspace[idx];
724+
again:
725+
spin_lock(workspace_lock);
726+
if (!list_empty(idle_workspace)) {
727+
workspace = idle_workspace->next;
728+
list_del(workspace);
729+
(*num_workspace)--;
730+
spin_unlock(workspace_lock);
731+
return workspace;
732+
733+
}
734+
if (atomic_read(alloc_workspace) > cpus) {
735+
DEFINE_WAIT(wait);
736+
737+
spin_unlock(workspace_lock);
738+
prepare_to_wait(workspace_wait, &wait, TASK_UNINTERRUPTIBLE);
739+
if (atomic_read(alloc_workspace) > cpus && !*num_workspace)
740+
schedule();
741+
finish_wait(workspace_wait, &wait);
742+
goto again;
743+
}
744+
atomic_inc(alloc_workspace);
745+
spin_unlock(workspace_lock);
746+
747+
workspace = btrfs_compress_op[idx]->alloc_workspace();
748+
if (IS_ERR(workspace)) {
749+
atomic_dec(alloc_workspace);
750+
wake_up(workspace_wait);
751+
}
752+
return workspace;
753+
}
754+
755+
/*
756+
* put a workspace struct back on the list or free it if we have enough
757+
* idle ones sitting around
758+
*/
759+
static void free_workspace(int type, struct list_head *workspace)
760+
{
761+
int idx = type - 1;
762+
struct list_head *idle_workspace = &comp_idle_workspace[idx];
763+
spinlock_t *workspace_lock = &comp_workspace_lock[idx];
764+
atomic_t *alloc_workspace = &comp_alloc_workspace[idx];
765+
wait_queue_head_t *workspace_wait = &comp_workspace_wait[idx];
766+
int *num_workspace = &comp_num_workspace[idx];
767+
768+
spin_lock(workspace_lock);
769+
if (*num_workspace < num_online_cpus()) {
770+
list_add_tail(workspace, idle_workspace);
771+
(*num_workspace)++;
772+
spin_unlock(workspace_lock);
773+
goto wake;
774+
}
775+
spin_unlock(workspace_lock);
776+
777+
btrfs_compress_op[idx]->free_workspace(workspace);
778+
atomic_dec(alloc_workspace);
779+
wake:
780+
if (waitqueue_active(workspace_wait))
781+
wake_up(workspace_wait);
782+
}
783+
784+
/*
785+
* cleanup function for module exit
786+
*/
787+
static void free_workspaces(void)
788+
{
789+
struct list_head *workspace;
790+
int i;
791+
792+
for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
793+
while (!list_empty(&comp_idle_workspace[i])) {
794+
workspace = comp_idle_workspace[i].next;
795+
list_del(workspace);
796+
btrfs_compress_op[i]->free_workspace(workspace);
797+
atomic_dec(&comp_alloc_workspace[i]);
798+
}
799+
}
800+
}
801+
802+
/*
803+
* given an address space and start/len, compress the bytes.
804+
*
805+
* pages are allocated to hold the compressed result and stored
806+
* in 'pages'
807+
*
808+
* out_pages is used to return the number of pages allocated. There
809+
* may be pages allocated even if we return an error
810+
*
811+
* total_in is used to return the number of bytes actually read. It
812+
* may be smaller then len if we had to exit early because we
813+
* ran out of room in the pages array or because we cross the
814+
* max_out threshold.
815+
*
816+
* total_out is used to return the total number of compressed bytes
817+
*
818+
* max_out tells us the max number of bytes that we're allowed to
819+
* stuff into pages
820+
*/
821+
int btrfs_compress_pages(int type, struct address_space *mapping,
822+
u64 start, unsigned long len,
823+
struct page **pages,
824+
unsigned long nr_dest_pages,
825+
unsigned long *out_pages,
826+
unsigned long *total_in,
827+
unsigned long *total_out,
828+
unsigned long max_out)
829+
{
830+
struct list_head *workspace;
831+
int ret;
832+
833+
workspace = find_workspace(type);
834+
if (IS_ERR(workspace))
835+
return -1;
836+
837+
ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
838+
start, len, pages,
839+
nr_dest_pages, out_pages,
840+
total_in, total_out,
841+
max_out);
842+
free_workspace(type, workspace);
843+
return ret;
844+
}
845+
846+
/*
847+
* pages_in is an array of pages with compressed data.
848+
*
849+
* disk_start is the starting logical offset of this array in the file
850+
*
851+
* bvec is a bio_vec of pages from the file that we want to decompress into
852+
*
853+
* vcnt is the count of pages in the biovec
854+
*
855+
* srclen is the number of bytes in pages_in
856+
*
857+
* The basic idea is that we have a bio that was created by readpages.
858+
* The pages in the bio are for the uncompressed data, and they may not
859+
* be contiguous. They all correspond to the range of bytes covered by
860+
* the compressed extent.
861+
*/
862+
int btrfs_decompress_biovec(int type, struct page **pages_in, u64 disk_start,
863+
struct bio_vec *bvec, int vcnt, size_t srclen)
864+
{
865+
struct list_head *workspace;
866+
int ret;
867+
868+
workspace = find_workspace(type);
869+
if (IS_ERR(workspace))
870+
return -ENOMEM;
871+
872+
ret = btrfs_compress_op[type-1]->decompress_biovec(workspace, pages_in,
873+
disk_start,
874+
bvec, vcnt, srclen);
875+
free_workspace(type, workspace);
876+
return ret;
877+
}
878+
879+
/*
880+
* a less complex decompression routine. Our compressed data fits in a
881+
* single page, and we want to read a single page out of it.
882+
* start_byte tells us the offset into the compressed data we're interested in
883+
*/
884+
int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
885+
unsigned long start_byte, size_t srclen, size_t destlen)
886+
{
887+
struct list_head *workspace;
888+
int ret;
889+
890+
workspace = find_workspace(type);
891+
if (IS_ERR(workspace))
892+
return -ENOMEM;
893+
894+
ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
895+
dest_page, start_byte,
896+
srclen, destlen);
897+
898+
free_workspace(type, workspace);
899+
return ret;
900+
}
901+
902+
void __exit btrfs_exit_compress(void)
903+
{
904+
free_workspaces();
905+
}

‎fs/btrfs/compression.h‎

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,59 @@
1919
#ifndef __BTRFS_COMPRESSION_
2020
#define __BTRFS_COMPRESSION_
2121

22-
int btrfs_zlib_decompress(unsigned char *data_in,
23-
struct page *dest_page,
24-
unsigned long start_byte,
25-
size_t srclen, size_t destlen);
26-
int btrfs_zlib_compress_pages(struct address_space *mapping,
27-
u64 start, unsigned long len,
28-
struct page **pages,
29-
unsigned long nr_dest_pages,
30-
unsigned long *out_pages,
31-
unsigned long *total_in,
32-
unsigned long *total_out,
33-
unsigned long max_out);
34-
int btrfs_zlib_decompress_biovec(struct page **pages_in,
35-
u64 disk_start,
36-
struct bio_vec *bvec,
37-
int vcnt,
38-
size_t srclen);
39-
void btrfs_zlib_exit(void);
22+
int btrfs_init_compress(void);
23+
void btrfs_exit_compress(void);
24+
25+
int btrfs_compress_pages(int type, struct address_space *mapping,
26+
u64 start, unsigned long len,
27+
struct page **pages,
28+
unsigned long nr_dest_pages,
29+
unsigned long *out_pages,
30+
unsigned long *total_in,
31+
unsigned long *total_out,
32+
unsigned long max_out);
33+
int btrfs_decompress_biovec(int type, struct page **pages_in, u64 disk_start,
34+
struct bio_vec *bvec, int vcnt, size_t srclen);
35+
int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
36+
unsigned long start_byte, size_t srclen, size_t destlen);
37+
4038
int btrfs_submit_compressed_write(struct inode *inode, u64 start,
4139
unsigned long len, u64 disk_start,
4240
unsigned long compressed_len,
4341
struct page **compressed_pages,
4442
unsigned long nr_pages);
4543
int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
4644
int mirror_num, unsigned long bio_flags);
45+
46+
struct btrfs_compress_op {
47+
struct list_head *(*alloc_workspace)(void);
48+
49+
void (*free_workspace)(struct list_head *workspace);
50+
51+
int (*compress_pages)(struct list_head *workspace,
52+
struct address_space *mapping,
53+
u64 start, unsigned long len,
54+
struct page **pages,
55+
unsigned long nr_dest_pages,
56+
unsigned long *out_pages,
57+
unsigned long *total_in,
58+
unsigned long *total_out,
59+
unsigned long max_out);
60+
61+
int (*decompress_biovec)(struct list_head *workspace,
62+
struct page **pages_in,
63+
u64 disk_start,
64+
struct bio_vec *bvec,
65+
int vcnt,
66+
size_t srclen);
67+
68+
int (*decompress)(struct list_head *workspace,
69+
unsigned char *data_in,
70+
struct page *dest_page,
71+
unsigned long start_byte,
72+
size_t srclen, size_t destlen);
73+
};
74+
75+
extern struct btrfs_compress_op btrfs_zlib_compress;
76+
4777
#endif

‎fs/btrfs/ctree.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,10 @@ struct btrfs_timespec {
551551
} __attribute__ ((__packed__));
552552

553553
enum btrfs_compression_type {
554-
BTRFS_COMPRESS_NONE = 0,
555-
BTRFS_COMPRESS_ZLIB = 1,
556-
BTRFS_COMPRESS_LAST = 2,
554+
BTRFS_COMPRESS_NONE = 0,
555+
BTRFS_COMPRESS_ZLIB = 1,
556+
BTRFS_COMPRESS_TYPES = 1,
557+
BTRFS_COMPRESS_LAST = 2,
557558
};
558559

559560
struct btrfs_inode_item {
@@ -895,7 +896,8 @@ struct btrfs_fs_info {
895896
*/
896897
u64 last_trans_log_full_commit;
897898
u64 open_ioctl_trans;
898-
unsigned long mount_opt;
899+
unsigned long mount_opt:20;
900+
unsigned long compress_type:4;
899901
u64 max_inline;
900902
u64 alloc_start;
901903
struct btrfs_transaction *running_transaction;

‎fs/btrfs/extent_io.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2028,8 +2028,11 @@ static int __extent_read_full_page(struct extent_io_tree *tree,
20282028
BUG_ON(extent_map_end(em) <= cur);
20292029
BUG_ON(end < cur);
20302030

2031-
if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2031+
if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
20322032
this_bio_flag = EXTENT_BIO_COMPRESSED;
2033+
extent_set_compress_type(&this_bio_flag,
2034+
em->compress_type);
2035+
}
20332036

20342037
iosize = min(extent_map_end(em) - cur, end - cur + 1);
20352038
cur_end = min(extent_map_end(em) - 1, end);

0 commit comments

Comments
 (0)