• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * compress.c
3  *
4  * Copyright (c) 2020 Google Inc.
5  *   Robin Hsu <robinhsu@google.com>
6  *  : add sload compression support
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 /* for config.h for general environment (non-Android) */
14 #include "f2fs.h"
15 
16 #include "compress.h"
17 #ifdef HAVE_LIBLZO2
18 #include <lzo/lzo1x.h>	/* for lzo1x_1_15_compress() */
19 #endif
20 #ifdef HAVE_LIBLZ4
21 #include <lz4.h>	/* for LZ4_compress_fast_extState() */
22 #endif
23 
24 /*
25  * macro/constants borrowed from kernel header (GPL-2.0):
26  * include/linux/lzo.h, and include/linux/lz4.h
27  */
28 #ifdef HAVE_LIBLZO2
29 #define lzo1x_worst_compress(x)		((x) + (x) / 16 + 64 + 3 + 2)
30 #define LZO_WORK_SIZE			ALIGN_UP(LZO1X_1_15_MEM_COMPRESS, 8)
31 #endif
32 #ifdef HAVE_LIBLZ4
33 #define LZ4_MEMORY_USAGE		14
34 #define LZ4_MAX_INPUT_SIZE		0x7E000000 /* 2 113 929 216 bytes */
35 #define LZ4_MEM_COMPRESS		sizeof(LZ4_stream_t)
36 #define LZ4_ACCELERATION_DEFAULT	1
37 #define LZ4_WORK_SIZE			ALIGN_UP(LZ4_MEM_COMPRESS, 8)
38 #endif
39 
40 #if defined(HAVE_LIBLZO2) || defined(HAVE_LIBLZ4)
reset_cc(struct compress_ctx * cc)41 static void reset_cc(struct compress_ctx *cc)
42 {
43 	memset(cc->rbuf, 0, cc->cluster_size * F2FS_BLKSIZE);
44 	memset(cc->cbuf->cdata, 0, cc->cluster_size * F2FS_BLKSIZE
45 			- F2FS_BLKSIZE);
46 }
47 #endif
48 
49 #ifdef HAVE_LIBLZO2
lzo_compress_init(struct compress_ctx * cc)50 static void lzo_compress_init(struct compress_ctx *cc)
51 {
52 	size_t size = cc->cluster_size * F2FS_BLKSIZE;
53 	size_t alloc = size + lzo1x_worst_compress(size)
54 			+ COMPRESS_HEADER_SIZE + LZO_WORK_SIZE;
55 	cc->private = malloc(alloc);
56 	ASSERT(cc->private);
57 	cc->rbuf = (char *) cc->private + LZO_WORK_SIZE;
58 	cc->cbuf = (struct compress_data *)((char *) cc->rbuf + size);
59 }
60 
lzo_compress(struct compress_ctx * cc)61 static int lzo_compress(struct compress_ctx *cc)
62 {
63 	int ret = lzo1x_1_15_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
64 			(lzo_uintp)(&cc->clen), cc->private);
65 	cc->cbuf->clen = cpu_to_le32(cc->clen);
66 	return ret;
67 }
68 #endif
69 
70 #ifdef HAVE_LIBLZ4
lz4_compress_init(struct compress_ctx * cc)71 static void lz4_compress_init(struct compress_ctx *cc)
72 {
73 	size_t size = cc->cluster_size * F2FS_BLKSIZE;
74 	size_t alloc = size + LZ4_COMPRESSBOUND(size)
75 			+ COMPRESS_HEADER_SIZE + LZ4_WORK_SIZE;
76 	cc->private = malloc(alloc);
77 	ASSERT(cc->private);
78 	cc->rbuf = (char *) cc->private + LZ4_WORK_SIZE;
79 	cc->cbuf = (struct compress_data *)((char *) cc->rbuf + size);
80 }
81 
lz4_compress(struct compress_ctx * cc)82 static int lz4_compress(struct compress_ctx *cc)
83 {
84 	cc->clen = LZ4_compress_fast_extState(cc->private, cc->rbuf,
85 			(char *)cc->cbuf->cdata, cc->rlen,
86 			cc->rlen - F2FS_BLKSIZE * c.compress.min_blocks -
87 			COMPRESS_HEADER_SIZE,
88 			LZ4_ACCELERATION_DEFAULT);
89 
90 	if (!cc->clen)
91 		return 1;
92 
93 	cc->cbuf->clen = cpu_to_le32(cc->clen);
94 	return 0;
95 }
96 #endif
97 
98 const char *supported_comp_names[] = {
99 	"lzo",
100 	"lz4",
101 	"",
102 };
103 
104 compress_ops supported_comp_ops[] = {
105 #ifdef HAVE_LIBLZO2
106 	{lzo_compress_init, lzo_compress, reset_cc},
107 #else
108 	{NULL, NULL, NULL},
109 #endif
110 #ifdef HAVE_LIBLZ4
111 	{lz4_compress_init, lz4_compress, reset_cc},
112 #else
113 	{NULL, NULL, NULL},
114 #endif
115 };
116 
117 /* linked list */
118 typedef struct _ext_t {
119 	const char *ext;
120 	struct _ext_t *next;
121 } ext_t;
122 
123 static ext_t *extension_list;
124 
ext_found(const char * ext)125 static bool ext_found(const char *ext)
126 {
127 	ext_t *p = extension_list;
128 
129 	while (p != NULL && strcmp(ext, p->ext))
130 		p = p->next;
131 	return (p != NULL);
132 }
133 
get_ext(const char * path)134 static const char *get_ext(const char *path)
135 {
136 	char *p = strrchr(path, '.');
137 
138 	return p == NULL ? path + strlen(path) : p + 1;
139 }
140 
ext_do_filter(const char * path)141 static bool ext_do_filter(const char *path)
142 {
143 	return (ext_found(get_ext(path)) == true) ^
144 		(c.compress.filter == COMPR_FILTER_ALLOW);
145 }
146 
ext_filter_add(const char * ext)147 static void ext_filter_add(const char *ext)
148 {
149 	ext_t *node;
150 
151 	ASSERT(ext != NULL);
152 	if (ext_found(ext))
153 		return; /* ext was already registered */
154 	node = malloc(sizeof(ext_t));
155 	ASSERT(node != NULL);
156 	node->ext = ext;
157 	node->next = extension_list;
158 	extension_list = node;
159 }
160 
ext_filter_destroy(void)161 static void ext_filter_destroy(void)
162 {
163 	ext_t *p;
164 
165 	while (extension_list != NULL) {
166 		p = extension_list;
167 		extension_list = p->next;
168 		free(p);
169 	}
170 }
171 
172 filter_ops ext_filter = {
173 	.add = ext_filter_add,
174 	.destroy = ext_filter_destroy,
175 	.filter = ext_do_filter,
176 };
177