David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Oracle. All rights reserved. |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/slab.h> |
David Sterba | 6acafd1 | 2017-05-31 17:21:15 +0200 | [diff] [blame] | 8 | #include <linux/mm.h> |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 9 | #include <linux/init.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/pagemap.h> |
| 13 | #include <linux/bio.h> |
| 14 | #include <linux/lzo.h> |
Anand Jain | e1ddce7 | 2017-05-26 15:44:59 +0800 | [diff] [blame] | 15 | #include <linux/refcount.h> |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 16 | #include "compression.h" |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 17 | #include "ctree.h" |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 18 | |
| 19 | #define LZO_LEN 4 |
| 20 | |
Qu Wenruo | 2a1f7c0 | 2018-05-17 13:10:01 +0800 | [diff] [blame] | 21 | /* |
| 22 | * Btrfs LZO compression format |
| 23 | * |
| 24 | * Regular and inlined LZO compressed data extents consist of: |
| 25 | * |
| 26 | * 1. Header |
| 27 | * Fixed size. LZO_LEN (4) bytes long, LE32. |
| 28 | * Records the total size (including the header) of compressed data. |
| 29 | * |
| 30 | * 2. Segment(s) |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 31 | * Variable size. Each segment includes one segment header, followed by data |
Qu Wenruo | 2a1f7c0 | 2018-05-17 13:10:01 +0800 | [diff] [blame] | 32 | * payload. |
| 33 | * One regular LZO compressed extent can have one or more segments. |
| 34 | * For inlined LZO compressed extent, only one segment is allowed. |
| 35 | * One segment represents at most one page of uncompressed data. |
| 36 | * |
| 37 | * 2.1 Segment header |
| 38 | * Fixed size. LZO_LEN (4) bytes long, LE32. |
| 39 | * Records the total size of the segment (not including the header). |
| 40 | * Segment header never crosses page boundary, thus it's possible to |
| 41 | * have at most 3 padding zeros at the end of the page. |
| 42 | * |
| 43 | * 2.2 Data Payload |
| 44 | * Variable size. Size up limit should be lzo1x_worst_compress(PAGE_SIZE) |
| 45 | * which is 4419 for a 4KiB page. |
| 46 | * |
| 47 | * Example: |
| 48 | * Page 1: |
| 49 | * 0 0x2 0x4 0x6 0x8 0xa 0xc 0xe 0x10 |
| 50 | * 0x0000 | Header | SegHdr 01 | Data payload 01 ... | |
| 51 | * ... |
| 52 | * 0x0ff0 | SegHdr N | Data payload N ... |00| |
| 53 | * ^^ padding zeros |
| 54 | * Page 2: |
| 55 | * 0x1000 | SegHdr N+1| Data payload N+1 ... | |
| 56 | */ |
| 57 | |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 58 | struct workspace { |
| 59 | void *mem; |
Jie Liu | 3fb4037 | 2013-06-06 13:38:50 +0000 | [diff] [blame] | 60 | void *buf; /* where decompressed data goes */ |
| 61 | void *cbuf; /* where compressed data goes */ |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 62 | struct list_head list; |
| 63 | }; |
| 64 | |
Dennis Zhou | 92ee5530 | 2019-02-04 15:20:03 -0500 | [diff] [blame] | 65 | static struct workspace_manager wsm; |
| 66 | |
David Sterba | d20f395 | 2019-10-04 02:21:48 +0200 | [diff] [blame] | 67 | void lzo_free_workspace(struct list_head *ws) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 68 | { |
| 69 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
| 70 | |
David Sterba | 6acafd1 | 2017-05-31 17:21:15 +0200 | [diff] [blame] | 71 | kvfree(workspace->buf); |
| 72 | kvfree(workspace->cbuf); |
| 73 | kvfree(workspace->mem); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 74 | kfree(workspace); |
| 75 | } |
| 76 | |
David Sterba | d20f395 | 2019-10-04 02:21:48 +0200 | [diff] [blame] | 77 | struct list_head *lzo_alloc_workspace(unsigned int level) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 78 | { |
| 79 | struct workspace *workspace; |
| 80 | |
David Sterba | 389a6cf | 2017-05-31 17:21:15 +0200 | [diff] [blame] | 81 | workspace = kzalloc(sizeof(*workspace), GFP_KERNEL); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 82 | if (!workspace) |
| 83 | return ERR_PTR(-ENOMEM); |
| 84 | |
David Sterba | 6acafd1 | 2017-05-31 17:21:15 +0200 | [diff] [blame] | 85 | workspace->mem = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 86 | workspace->buf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL); |
| 87 | workspace->cbuf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 88 | if (!workspace->mem || !workspace->buf || !workspace->cbuf) |
| 89 | goto fail; |
| 90 | |
| 91 | INIT_LIST_HEAD(&workspace->list); |
| 92 | |
| 93 | return &workspace->list; |
| 94 | fail: |
| 95 | lzo_free_workspace(&workspace->list); |
| 96 | return ERR_PTR(-ENOMEM); |
| 97 | } |
| 98 | |
| 99 | static inline void write_compress_length(char *buf, size_t len) |
| 100 | { |
| 101 | __le32 dlen; |
| 102 | |
| 103 | dlen = cpu_to_le32(len); |
| 104 | memcpy(buf, &dlen, LZO_LEN); |
| 105 | } |
| 106 | |
David Sterba | 14a3357 | 2017-02-14 17:58:04 +0100 | [diff] [blame] | 107 | static inline size_t read_compress_length(const char *buf) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 108 | { |
| 109 | __le32 dlen; |
| 110 | |
| 111 | memcpy(&dlen, buf, LZO_LEN); |
| 112 | return le32_to_cpu(dlen); |
| 113 | } |
| 114 | |
David Sterba | c4bf665 | 2019-10-01 22:38:34 +0200 | [diff] [blame] | 115 | int lzo_compress_pages(struct list_head *ws, struct address_space *mapping, |
| 116 | u64 start, struct page **pages, unsigned long *out_pages, |
| 117 | unsigned long *total_in, unsigned long *total_out) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 118 | { |
| 119 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
| 120 | int ret = 0; |
| 121 | char *data_in; |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 122 | char *cpage_out, *sizes_ptr; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 123 | int nr_pages = 0; |
| 124 | struct page *in_page = NULL; |
| 125 | struct page *out_page = NULL; |
| 126 | unsigned long bytes_left; |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 127 | unsigned long len = *total_out; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 128 | unsigned long nr_dest_pages = *out_pages; |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 129 | const unsigned long max_out = nr_dest_pages * PAGE_SIZE; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 130 | size_t in_len; |
| 131 | size_t out_len; |
| 132 | char *buf; |
| 133 | unsigned long tot_in = 0; |
| 134 | unsigned long tot_out = 0; |
| 135 | unsigned long pg_bytes_left; |
| 136 | unsigned long out_offset; |
| 137 | unsigned long bytes; |
| 138 | |
| 139 | *out_pages = 0; |
| 140 | *total_out = 0; |
| 141 | *total_in = 0; |
| 142 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 143 | in_page = find_get_page(mapping, start >> PAGE_SHIFT); |
David Sterba | 8c945d3 | 2021-06-14 22:25:53 +0200 | [diff] [blame] | 144 | data_in = page_address(in_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * store the size of all chunks of compressed data in |
| 148 | * the first 4 bytes |
| 149 | */ |
David Sterba | b0ee5e1 | 2021-06-14 22:22:22 +0200 | [diff] [blame] | 150 | out_page = alloc_page(GFP_NOFS); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 151 | if (out_page == NULL) { |
| 152 | ret = -ENOMEM; |
| 153 | goto out; |
| 154 | } |
David Sterba | 8c945d3 | 2021-06-14 22:25:53 +0200 | [diff] [blame] | 155 | cpage_out = page_address(out_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 156 | out_offset = LZO_LEN; |
| 157 | tot_out = LZO_LEN; |
| 158 | pages[0] = out_page; |
| 159 | nr_pages = 1; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 160 | pg_bytes_left = PAGE_SIZE - LZO_LEN; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 161 | |
| 162 | /* compress at most one page of data each time */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 163 | in_len = min(len, PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 164 | while (tot_in < len) { |
| 165 | ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf, |
| 166 | &out_len, workspace->mem); |
| 167 | if (ret != LZO_E_OK) { |
Timofey Titovets | 036b021 | 2017-05-25 21:12:19 +0300 | [diff] [blame] | 168 | pr_debug("BTRFS: lzo in loop returned %d\n", |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 169 | ret); |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 170 | ret = -EIO; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 171 | goto out; |
| 172 | } |
| 173 | |
| 174 | /* store the size of this chunk of compressed data */ |
| 175 | write_compress_length(cpage_out + out_offset, out_len); |
| 176 | tot_out += LZO_LEN; |
| 177 | out_offset += LZO_LEN; |
| 178 | pg_bytes_left -= LZO_LEN; |
| 179 | |
| 180 | tot_in += in_len; |
| 181 | tot_out += out_len; |
| 182 | |
| 183 | /* copy bytes from the working buffer into the pages */ |
| 184 | buf = workspace->cbuf; |
| 185 | while (out_len) { |
| 186 | bytes = min_t(unsigned long, pg_bytes_left, out_len); |
| 187 | |
| 188 | memcpy(cpage_out + out_offset, buf, bytes); |
| 189 | |
| 190 | out_len -= bytes; |
| 191 | pg_bytes_left -= bytes; |
| 192 | buf += bytes; |
| 193 | out_offset += bytes; |
| 194 | |
| 195 | /* |
| 196 | * we need another page for writing out. |
| 197 | * |
| 198 | * Note if there's less than 4 bytes left, we just |
| 199 | * skip to a new page. |
| 200 | */ |
| 201 | if ((out_len == 0 && pg_bytes_left < LZO_LEN) || |
| 202 | pg_bytes_left == 0) { |
| 203 | if (pg_bytes_left) { |
| 204 | memset(cpage_out + out_offset, 0, |
| 205 | pg_bytes_left); |
| 206 | tot_out += pg_bytes_left; |
| 207 | } |
| 208 | |
| 209 | /* we're done, don't allocate new page */ |
| 210 | if (out_len == 0 && tot_in >= len) |
| 211 | break; |
| 212 | |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 213 | if (nr_pages == nr_dest_pages) { |
| 214 | out_page = NULL; |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 215 | ret = -E2BIG; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 216 | goto out; |
| 217 | } |
| 218 | |
David Sterba | b0ee5e1 | 2021-06-14 22:22:22 +0200 | [diff] [blame] | 219 | out_page = alloc_page(GFP_NOFS); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 220 | if (out_page == NULL) { |
| 221 | ret = -ENOMEM; |
| 222 | goto out; |
| 223 | } |
David Sterba | 8c945d3 | 2021-06-14 22:25:53 +0200 | [diff] [blame] | 224 | cpage_out = page_address(out_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 225 | pages[nr_pages++] = out_page; |
| 226 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 227 | pg_bytes_left = PAGE_SIZE; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 228 | out_offset = 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* we're making it bigger, give up */ |
Stefan Agner | 59516f6 | 2013-07-01 20:33:39 +0200 | [diff] [blame] | 233 | if (tot_in > 8192 && tot_in < tot_out) { |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 234 | ret = -E2BIG; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 235 | goto out; |
Stefan Agner | 59516f6 | 2013-07-01 20:33:39 +0200 | [diff] [blame] | 236 | } |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 237 | |
| 238 | /* we're all done */ |
| 239 | if (tot_in >= len) |
| 240 | break; |
| 241 | |
| 242 | if (tot_out > max_out) |
| 243 | break; |
| 244 | |
| 245 | bytes_left = len - tot_in; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 246 | put_page(in_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 247 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 248 | start += PAGE_SIZE; |
| 249 | in_page = find_get_page(mapping, start >> PAGE_SHIFT); |
David Sterba | 8c945d3 | 2021-06-14 22:25:53 +0200 | [diff] [blame] | 250 | data_in = page_address(in_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 251 | in_len = min(bytes_left, PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Timofey Titovets | 1e9d729 | 2017-05-30 02:18:04 +0300 | [diff] [blame] | 254 | if (tot_out >= tot_in) { |
| 255 | ret = -E2BIG; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 256 | goto out; |
Timofey Titovets | 1e9d729 | 2017-05-30 02:18:04 +0300 | [diff] [blame] | 257 | } |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 258 | |
| 259 | /* store the size of all chunks of compressed data */ |
David Sterba | 8c945d3 | 2021-06-14 22:25:53 +0200 | [diff] [blame] | 260 | sizes_ptr = page_address(pages[0]); |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 261 | write_compress_length(sizes_ptr, tot_out); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 262 | |
| 263 | ret = 0; |
| 264 | *total_out = tot_out; |
| 265 | *total_in = tot_in; |
| 266 | out: |
| 267 | *out_pages = nr_pages; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 268 | |
David Sterba | 8c945d3 | 2021-06-14 22:25:53 +0200 | [diff] [blame] | 269 | if (in_page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 270 | put_page(in_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 275 | /* |
| 276 | * Copy the compressed segment payload into @dest. |
| 277 | * |
| 278 | * For the payload there will be no padding, just need to do page switching. |
| 279 | */ |
| 280 | static void copy_compressed_segment(struct compressed_bio *cb, |
| 281 | char *dest, u32 len, u32 *cur_in) |
| 282 | { |
| 283 | u32 orig_in = *cur_in; |
| 284 | |
| 285 | while (*cur_in < orig_in + len) { |
| 286 | struct page *cur_page; |
| 287 | u32 copy_len = min_t(u32, PAGE_SIZE - offset_in_page(*cur_in), |
| 288 | orig_in + len - *cur_in); |
| 289 | |
| 290 | ASSERT(copy_len); |
| 291 | cur_page = cb->compressed_pages[*cur_in / PAGE_SIZE]; |
| 292 | |
| 293 | memcpy(dest + *cur_in - orig_in, |
| 294 | page_address(cur_page) + offset_in_page(*cur_in), |
| 295 | copy_len); |
| 296 | |
| 297 | *cur_in += copy_len; |
| 298 | } |
| 299 | } |
| 300 | |
David Sterba | c4bf665 | 2019-10-01 22:38:34 +0200 | [diff] [blame] | 301 | int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 302 | { |
| 303 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 304 | const struct btrfs_fs_info *fs_info = btrfs_sb(cb->inode->i_sb); |
| 305 | const u32 sectorsize = fs_info->sectorsize; |
| 306 | int ret; |
| 307 | /* Compressed data length, can be unaligned */ |
| 308 | u32 len_in; |
| 309 | /* Offset inside the compressed data */ |
| 310 | u32 cur_in = 0; |
| 311 | /* Bytes decompressed so far */ |
| 312 | u32 cur_out = 0; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 313 | |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 314 | len_in = read_compress_length(page_address(cb->compressed_pages[0])); |
| 315 | cur_in += LZO_LEN; |
| 316 | |
Qu Wenruo | 314bfa4 | 2018-05-15 14:57:51 +0800 | [diff] [blame] | 317 | /* |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 318 | * LZO header length check |
Qu Wenruo | 314bfa4 | 2018-05-15 14:57:51 +0800 | [diff] [blame] | 319 | * |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 320 | * The total length should not exceed the maximum extent length, |
| 321 | * and all sectors should be used. |
| 322 | * If this happens, it means the compressed extent is corrupted. |
Qu Wenruo | 314bfa4 | 2018-05-15 14:57:51 +0800 | [diff] [blame] | 323 | */ |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 324 | if (len_in > min_t(size_t, BTRFS_MAX_COMPRESSED, cb->compressed_len) || |
| 325 | round_up(len_in, sectorsize) < cb->compressed_len) { |
| 326 | btrfs_err(fs_info, |
| 327 | "invalid lzo header, lzo len %u compressed len %u", |
| 328 | len_in, cb->compressed_len); |
| 329 | return -EUCLEAN; |
Qu Wenruo | 314bfa4 | 2018-05-15 14:57:51 +0800 | [diff] [blame] | 330 | } |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 331 | |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 332 | /* Go through each lzo segment */ |
| 333 | while (cur_in < len_in) { |
| 334 | struct page *cur_page; |
| 335 | /* Length of the compressed segment */ |
| 336 | u32 seg_len; |
| 337 | u32 sector_bytes_left; |
| 338 | size_t out_len = lzo1x_worst_compress(sectorsize); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 339 | |
Qu Wenruo | 314bfa4 | 2018-05-15 14:57:51 +0800 | [diff] [blame] | 340 | /* |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 341 | * We should always have enough space for one segment header |
| 342 | * inside current sector. |
Qu Wenruo | 314bfa4 | 2018-05-15 14:57:51 +0800 | [diff] [blame] | 343 | */ |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 344 | ASSERT(cur_in / sectorsize == |
| 345 | (cur_in + LZO_LEN - 1) / sectorsize); |
| 346 | cur_page = cb->compressed_pages[cur_in / PAGE_SIZE]; |
| 347 | ASSERT(cur_page); |
| 348 | seg_len = read_compress_length(page_address(cur_page) + |
| 349 | offset_in_page(cur_in)); |
| 350 | cur_in += LZO_LEN; |
Qu Wenruo | 314bfa4 | 2018-05-15 14:57:51 +0800 | [diff] [blame] | 351 | |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 352 | /* Copy the compressed segment payload into workspace */ |
| 353 | copy_compressed_segment(cb, workspace->cbuf, seg_len, &cur_in); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 354 | |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 355 | /* Decompress the data */ |
| 356 | ret = lzo1x_decompress_safe(workspace->cbuf, seg_len, |
| 357 | workspace->buf, &out_len); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 358 | if (ret != LZO_E_OK) { |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 359 | btrfs_err(fs_info, "failed to decompress"); |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 360 | ret = -EIO; |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 361 | goto out; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 362 | } |
| 363 | |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 364 | /* Copy the data into inode pages */ |
| 365 | ret = btrfs_decompress_buf2page(workspace->buf, out_len, cb, cur_out); |
| 366 | cur_out += out_len; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 367 | |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 368 | /* All data read, exit */ |
| 369 | if (ret == 0) |
| 370 | goto out; |
| 371 | ret = 0; |
| 372 | |
| 373 | /* Check if the sector has enough space for a segment header */ |
| 374 | sector_bytes_left = sectorsize - (cur_in % sectorsize); |
| 375 | if (sector_bytes_left >= LZO_LEN) |
| 376 | continue; |
| 377 | |
| 378 | /* Skip the padding zeros */ |
| 379 | cur_in += sector_bytes_left; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 380 | } |
Qu Wenruo | a6e66e6 | 2021-07-26 14:34:55 +0800 | [diff] [blame] | 381 | out: |
Chris Mason | 2f19cad | 2014-11-30 08:56:33 -0500 | [diff] [blame] | 382 | if (!ret) |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 383 | zero_fill_bio(cb->orig_bio); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 384 | return ret; |
| 385 | } |
| 386 | |
David Sterba | c4bf665 | 2019-10-01 22:38:34 +0200 | [diff] [blame] | 387 | int lzo_decompress(struct list_head *ws, unsigned char *data_in, |
| 388 | struct page *dest_page, unsigned long start_byte, size_t srclen, |
| 389 | size_t destlen) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 390 | { |
| 391 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
| 392 | size_t in_len; |
| 393 | size_t out_len; |
Qu Wenruo | de885e3 | 2018-05-17 14:10:29 +0800 | [diff] [blame] | 394 | size_t max_segment_len = lzo1x_worst_compress(PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 395 | int ret = 0; |
| 396 | char *kaddr; |
| 397 | unsigned long bytes; |
| 398 | |
Qu Wenruo | de885e3 | 2018-05-17 14:10:29 +0800 | [diff] [blame] | 399 | if (srclen < LZO_LEN || srclen > max_segment_len + LZO_LEN * 2) |
| 400 | return -EUCLEAN; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 401 | |
Qu Wenruo | de885e3 | 2018-05-17 14:10:29 +0800 | [diff] [blame] | 402 | in_len = read_compress_length(data_in); |
| 403 | if (in_len != srclen) |
| 404 | return -EUCLEAN; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 405 | data_in += LZO_LEN; |
| 406 | |
| 407 | in_len = read_compress_length(data_in); |
Qu Wenruo | de885e3 | 2018-05-17 14:10:29 +0800 | [diff] [blame] | 408 | if (in_len != srclen - LZO_LEN * 2) { |
| 409 | ret = -EUCLEAN; |
| 410 | goto out; |
| 411 | } |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 412 | data_in += LZO_LEN; |
| 413 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 414 | out_len = PAGE_SIZE; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 415 | ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len); |
| 416 | if (ret != LZO_E_OK) { |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 417 | pr_warn("BTRFS: decompress failed!\n"); |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 418 | ret = -EIO; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 419 | goto out; |
| 420 | } |
| 421 | |
| 422 | if (out_len < start_byte) { |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 423 | ret = -EIO; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 424 | goto out; |
| 425 | } |
| 426 | |
Chris Mason | 2f19cad | 2014-11-30 08:56:33 -0500 | [diff] [blame] | 427 | /* |
| 428 | * the caller is already checking against PAGE_SIZE, but lets |
| 429 | * move this check closer to the memcpy/memset |
| 430 | */ |
| 431 | destlen = min_t(unsigned long, destlen, PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 432 | bytes = min_t(unsigned long, destlen, out_len - start_byte); |
| 433 | |
David Sterba | 8c945d3 | 2021-06-14 22:25:53 +0200 | [diff] [blame] | 434 | kaddr = page_address(dest_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 435 | memcpy(kaddr, workspace->buf + start_byte, bytes); |
Chris Mason | 2f19cad | 2014-11-30 08:56:33 -0500 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * btrfs_getblock is doing a zero on the tail of the page too, |
| 439 | * but this will cover anything missing from the decompressed |
| 440 | * data. |
| 441 | */ |
| 442 | if (bytes < destlen) |
| 443 | memset(kaddr+bytes, 0, destlen-bytes); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 444 | out: |
| 445 | return ret; |
| 446 | } |
| 447 | |
David Sterba | e8c9f18 | 2015-01-02 18:23:10 +0100 | [diff] [blame] | 448 | const struct btrfs_compress_op btrfs_lzo_compress = { |
David Sterba | be951045 | 2019-10-02 00:53:31 +0200 | [diff] [blame] | 449 | .workspace_manager = &wsm, |
David Sterba | e18333a | 2019-08-09 16:25:34 +0200 | [diff] [blame] | 450 | .max_level = 1, |
| 451 | .default_level = 1, |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 452 | }; |