Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Oracle. All rights reserved. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/slab.h> |
David Sterba | 6acafd1 | 2017-05-31 17:21:15 +0200 | [diff] [blame] | 21 | #include <linux/mm.h> |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 22 | #include <linux/init.h> |
| 23 | #include <linux/err.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/bio.h> |
| 27 | #include <linux/lzo.h> |
Anand Jain | e1ddce7 | 2017-05-26 15:44:59 +0800 | [diff] [blame] | 28 | #include <linux/refcount.h> |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 29 | #include "compression.h" |
| 30 | |
| 31 | #define LZO_LEN 4 |
| 32 | |
| 33 | struct workspace { |
| 34 | void *mem; |
Jie Liu | 3fb4037 | 2013-06-06 13:38:50 +0000 | [diff] [blame] | 35 | void *buf; /* where decompressed data goes */ |
| 36 | void *cbuf; /* where compressed data goes */ |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 37 | struct list_head list; |
| 38 | }; |
| 39 | |
| 40 | static void lzo_free_workspace(struct list_head *ws) |
| 41 | { |
| 42 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
| 43 | |
David Sterba | 6acafd1 | 2017-05-31 17:21:15 +0200 | [diff] [blame] | 44 | kvfree(workspace->buf); |
| 45 | kvfree(workspace->cbuf); |
| 46 | kvfree(workspace->mem); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 47 | kfree(workspace); |
| 48 | } |
| 49 | |
| 50 | static struct list_head *lzo_alloc_workspace(void) |
| 51 | { |
| 52 | struct workspace *workspace; |
| 53 | |
David Sterba | 389a6cf | 2017-05-31 17:21:15 +0200 | [diff] [blame] | 54 | workspace = kzalloc(sizeof(*workspace), GFP_KERNEL); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 55 | if (!workspace) |
| 56 | return ERR_PTR(-ENOMEM); |
| 57 | |
David Sterba | 6acafd1 | 2017-05-31 17:21:15 +0200 | [diff] [blame] | 58 | workspace->mem = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 59 | workspace->buf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL); |
| 60 | workspace->cbuf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 61 | if (!workspace->mem || !workspace->buf || !workspace->cbuf) |
| 62 | goto fail; |
| 63 | |
| 64 | INIT_LIST_HEAD(&workspace->list); |
| 65 | |
| 66 | return &workspace->list; |
| 67 | fail: |
| 68 | lzo_free_workspace(&workspace->list); |
| 69 | return ERR_PTR(-ENOMEM); |
| 70 | } |
| 71 | |
| 72 | static inline void write_compress_length(char *buf, size_t len) |
| 73 | { |
| 74 | __le32 dlen; |
| 75 | |
| 76 | dlen = cpu_to_le32(len); |
| 77 | memcpy(buf, &dlen, LZO_LEN); |
| 78 | } |
| 79 | |
David Sterba | 14a3357 | 2017-02-14 17:58:04 +0100 | [diff] [blame] | 80 | static inline size_t read_compress_length(const char *buf) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 81 | { |
| 82 | __le32 dlen; |
| 83 | |
| 84 | memcpy(&dlen, buf, LZO_LEN); |
| 85 | return le32_to_cpu(dlen); |
| 86 | } |
| 87 | |
| 88 | static int lzo_compress_pages(struct list_head *ws, |
| 89 | struct address_space *mapping, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 90 | u64 start, |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 91 | struct page **pages, |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 92 | unsigned long *out_pages, |
| 93 | unsigned long *total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 94 | unsigned long *total_out) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 95 | { |
| 96 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
| 97 | int ret = 0; |
| 98 | char *data_in; |
| 99 | char *cpage_out; |
| 100 | int nr_pages = 0; |
| 101 | struct page *in_page = NULL; |
| 102 | struct page *out_page = NULL; |
| 103 | unsigned long bytes_left; |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 104 | unsigned long len = *total_out; |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 105 | unsigned long nr_dest_pages = *out_pages; |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 106 | const unsigned long max_out = nr_dest_pages * PAGE_SIZE; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 107 | size_t in_len; |
| 108 | size_t out_len; |
| 109 | char *buf; |
| 110 | unsigned long tot_in = 0; |
| 111 | unsigned long tot_out = 0; |
| 112 | unsigned long pg_bytes_left; |
| 113 | unsigned long out_offset; |
| 114 | unsigned long bytes; |
| 115 | |
| 116 | *out_pages = 0; |
| 117 | *total_out = 0; |
| 118 | *total_in = 0; |
| 119 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 120 | in_page = find_get_page(mapping, start >> PAGE_SHIFT); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 121 | data_in = kmap(in_page); |
| 122 | |
| 123 | /* |
| 124 | * store the size of all chunks of compressed data in |
| 125 | * the first 4 bytes |
| 126 | */ |
| 127 | out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); |
| 128 | if (out_page == NULL) { |
| 129 | ret = -ENOMEM; |
| 130 | goto out; |
| 131 | } |
| 132 | cpage_out = kmap(out_page); |
| 133 | out_offset = LZO_LEN; |
| 134 | tot_out = LZO_LEN; |
| 135 | pages[0] = out_page; |
| 136 | nr_pages = 1; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 137 | pg_bytes_left = PAGE_SIZE - LZO_LEN; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 138 | |
| 139 | /* compress at most one page of data each time */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 140 | in_len = min(len, PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 141 | while (tot_in < len) { |
| 142 | ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf, |
| 143 | &out_len, workspace->mem); |
| 144 | if (ret != LZO_E_OK) { |
Timofey Titovets | 036b021 | 2017-05-25 21:12:19 +0300 | [diff] [blame] | 145 | pr_debug("BTRFS: lzo in loop returned %d\n", |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 146 | ret); |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 147 | ret = -EIO; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | /* store the size of this chunk of compressed data */ |
| 152 | write_compress_length(cpage_out + out_offset, out_len); |
| 153 | tot_out += LZO_LEN; |
| 154 | out_offset += LZO_LEN; |
| 155 | pg_bytes_left -= LZO_LEN; |
| 156 | |
| 157 | tot_in += in_len; |
| 158 | tot_out += out_len; |
| 159 | |
| 160 | /* copy bytes from the working buffer into the pages */ |
| 161 | buf = workspace->cbuf; |
| 162 | while (out_len) { |
| 163 | bytes = min_t(unsigned long, pg_bytes_left, out_len); |
| 164 | |
| 165 | memcpy(cpage_out + out_offset, buf, bytes); |
| 166 | |
| 167 | out_len -= bytes; |
| 168 | pg_bytes_left -= bytes; |
| 169 | buf += bytes; |
| 170 | out_offset += bytes; |
| 171 | |
| 172 | /* |
| 173 | * we need another page for writing out. |
| 174 | * |
| 175 | * Note if there's less than 4 bytes left, we just |
| 176 | * skip to a new page. |
| 177 | */ |
| 178 | if ((out_len == 0 && pg_bytes_left < LZO_LEN) || |
| 179 | pg_bytes_left == 0) { |
| 180 | if (pg_bytes_left) { |
| 181 | memset(cpage_out + out_offset, 0, |
| 182 | pg_bytes_left); |
| 183 | tot_out += pg_bytes_left; |
| 184 | } |
| 185 | |
| 186 | /* we're done, don't allocate new page */ |
| 187 | if (out_len == 0 && tot_in >= len) |
| 188 | break; |
| 189 | |
| 190 | kunmap(out_page); |
| 191 | if (nr_pages == nr_dest_pages) { |
| 192 | out_page = NULL; |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 193 | ret = -E2BIG; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 194 | goto out; |
| 195 | } |
| 196 | |
| 197 | out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM); |
| 198 | if (out_page == NULL) { |
| 199 | ret = -ENOMEM; |
| 200 | goto out; |
| 201 | } |
| 202 | cpage_out = kmap(out_page); |
| 203 | pages[nr_pages++] = out_page; |
| 204 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 205 | pg_bytes_left = PAGE_SIZE; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 206 | out_offset = 0; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* we're making it bigger, give up */ |
Stefan Agner | 59516f6 | 2013-07-01 20:33:39 +0200 | [diff] [blame] | 211 | if (tot_in > 8192 && tot_in < tot_out) { |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 212 | ret = -E2BIG; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 213 | goto out; |
Stefan Agner | 59516f6 | 2013-07-01 20:33:39 +0200 | [diff] [blame] | 214 | } |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 215 | |
| 216 | /* we're all done */ |
| 217 | if (tot_in >= len) |
| 218 | break; |
| 219 | |
| 220 | if (tot_out > max_out) |
| 221 | break; |
| 222 | |
| 223 | bytes_left = len - tot_in; |
| 224 | kunmap(in_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 225 | put_page(in_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 226 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 227 | start += PAGE_SIZE; |
| 228 | in_page = find_get_page(mapping, start >> PAGE_SHIFT); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 229 | data_in = kmap(in_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 230 | in_len = min(bytes_left, PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 231 | } |
| 232 | |
Timofey Titovets | 1e9d729 | 2017-05-30 02:18:04 +0300 | [diff] [blame] | 233 | if (tot_out >= tot_in) { |
| 234 | ret = -E2BIG; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 235 | goto out; |
Timofey Titovets | 1e9d729 | 2017-05-30 02:18:04 +0300 | [diff] [blame] | 236 | } |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 237 | |
| 238 | /* store the size of all chunks of compressed data */ |
| 239 | cpage_out = kmap(pages[0]); |
| 240 | write_compress_length(cpage_out, tot_out); |
| 241 | |
| 242 | kunmap(pages[0]); |
| 243 | |
| 244 | ret = 0; |
| 245 | *total_out = tot_out; |
| 246 | *total_in = tot_in; |
| 247 | out: |
| 248 | *out_pages = nr_pages; |
| 249 | if (out_page) |
| 250 | kunmap(out_page); |
| 251 | |
| 252 | if (in_page) { |
| 253 | kunmap(in_page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 254 | put_page(in_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
Anand Jain | e1ddce7 | 2017-05-26 15:44:59 +0800 | [diff] [blame] | 260 | static int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 261 | { |
| 262 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 263 | int ret = 0, ret2; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 264 | char *data_in; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 265 | unsigned long page_in_index = 0; |
Anand Jain | e1ddce7 | 2017-05-26 15:44:59 +0800 | [diff] [blame] | 266 | size_t srclen = cb->compressed_len; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 267 | unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 268 | unsigned long buf_start; |
| 269 | unsigned long buf_offset = 0; |
| 270 | unsigned long bytes; |
| 271 | unsigned long working_bytes; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 272 | size_t in_len; |
| 273 | size_t out_len; |
| 274 | unsigned long in_offset; |
| 275 | unsigned long in_page_bytes_left; |
| 276 | unsigned long tot_in; |
| 277 | unsigned long tot_out; |
| 278 | unsigned long tot_len; |
| 279 | char *buf; |
Li Zefan | ca9b688 | 2011-02-16 06:06:41 +0000 | [diff] [blame] | 280 | bool may_late_unmap, need_unmap; |
Anand Jain | e1ddce7 | 2017-05-26 15:44:59 +0800 | [diff] [blame] | 281 | struct page **pages_in = cb->compressed_pages; |
| 282 | u64 disk_start = cb->start; |
| 283 | struct bio *orig_bio = cb->orig_bio; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 284 | |
| 285 | data_in = kmap(pages_in[0]); |
| 286 | tot_len = read_compress_length(data_in); |
| 287 | |
| 288 | tot_in = LZO_LEN; |
| 289 | in_offset = LZO_LEN; |
| 290 | tot_len = min_t(size_t, srclen, tot_len); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 291 | in_page_bytes_left = PAGE_SIZE - LZO_LEN; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 292 | |
| 293 | tot_out = 0; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 294 | |
| 295 | while (tot_in < tot_len) { |
| 296 | in_len = read_compress_length(data_in + in_offset); |
| 297 | in_page_bytes_left -= LZO_LEN; |
| 298 | in_offset += LZO_LEN; |
| 299 | tot_in += LZO_LEN; |
| 300 | |
| 301 | tot_in += in_len; |
| 302 | working_bytes = in_len; |
Li Zefan | ca9b688 | 2011-02-16 06:06:41 +0000 | [diff] [blame] | 303 | may_late_unmap = need_unmap = false; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 304 | |
| 305 | /* fast path: avoid using the working buffer */ |
| 306 | if (in_page_bytes_left >= in_len) { |
| 307 | buf = data_in + in_offset; |
| 308 | bytes = in_len; |
Li Zefan | ca9b688 | 2011-02-16 06:06:41 +0000 | [diff] [blame] | 309 | may_late_unmap = true; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 310 | goto cont; |
| 311 | } |
| 312 | |
| 313 | /* copy bytes from the pages into the working buffer */ |
| 314 | buf = workspace->cbuf; |
| 315 | buf_offset = 0; |
| 316 | while (working_bytes) { |
| 317 | bytes = min(working_bytes, in_page_bytes_left); |
| 318 | |
| 319 | memcpy(buf + buf_offset, data_in + in_offset, bytes); |
| 320 | buf_offset += bytes; |
| 321 | cont: |
| 322 | working_bytes -= bytes; |
| 323 | in_page_bytes_left -= bytes; |
| 324 | in_offset += bytes; |
| 325 | |
| 326 | /* check if we need to pick another page */ |
| 327 | if ((working_bytes == 0 && in_page_bytes_left < LZO_LEN) |
| 328 | || in_page_bytes_left == 0) { |
| 329 | tot_in += in_page_bytes_left; |
| 330 | |
| 331 | if (working_bytes == 0 && tot_in >= tot_len) |
| 332 | break; |
| 333 | |
Li Zefan | ca9b688 | 2011-02-16 06:06:41 +0000 | [diff] [blame] | 334 | if (page_in_index + 1 >= total_pages_in) { |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 335 | ret = -EIO; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 336 | goto done; |
| 337 | } |
Li Zefan | ca9b688 | 2011-02-16 06:06:41 +0000 | [diff] [blame] | 338 | |
| 339 | if (may_late_unmap) |
| 340 | need_unmap = true; |
| 341 | else |
| 342 | kunmap(pages_in[page_in_index]); |
| 343 | |
| 344 | data_in = kmap(pages_in[++page_in_index]); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 345 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 346 | in_page_bytes_left = PAGE_SIZE; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 347 | in_offset = 0; |
| 348 | } |
| 349 | } |
| 350 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 351 | out_len = lzo1x_worst_compress(PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 352 | ret = lzo1x_decompress_safe(buf, in_len, workspace->buf, |
| 353 | &out_len); |
Li Zefan | ca9b688 | 2011-02-16 06:06:41 +0000 | [diff] [blame] | 354 | if (need_unmap) |
| 355 | kunmap(pages_in[page_in_index - 1]); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 356 | if (ret != LZO_E_OK) { |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 357 | pr_warn("BTRFS: decompress failed\n"); |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 358 | ret = -EIO; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 359 | break; |
| 360 | } |
| 361 | |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 362 | buf_start = tot_out; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 363 | tot_out += out_len; |
| 364 | |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 365 | ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start, |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 366 | tot_out, disk_start, orig_bio); |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 367 | if (ret2 == 0) |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 368 | break; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 369 | } |
| 370 | done: |
Li Zefan | ca9b688 | 2011-02-16 06:06:41 +0000 | [diff] [blame] | 371 | kunmap(pages_in[page_in_index]); |
Chris Mason | 2f19cad | 2014-11-30 08:56:33 -0500 | [diff] [blame] | 372 | if (!ret) |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 373 | zero_fill_bio(orig_bio); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | static int lzo_decompress(struct list_head *ws, unsigned char *data_in, |
| 378 | struct page *dest_page, |
| 379 | unsigned long start_byte, |
| 380 | size_t srclen, size_t destlen) |
| 381 | { |
| 382 | struct workspace *workspace = list_entry(ws, struct workspace, list); |
| 383 | size_t in_len; |
| 384 | size_t out_len; |
| 385 | size_t tot_len; |
| 386 | int ret = 0; |
| 387 | char *kaddr; |
| 388 | unsigned long bytes; |
| 389 | |
| 390 | BUG_ON(srclen < LZO_LEN); |
| 391 | |
| 392 | tot_len = read_compress_length(data_in); |
| 393 | data_in += LZO_LEN; |
| 394 | |
| 395 | in_len = read_compress_length(data_in); |
| 396 | data_in += LZO_LEN; |
| 397 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 398 | out_len = PAGE_SIZE; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 399 | ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len); |
| 400 | if (ret != LZO_E_OK) { |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 401 | pr_warn("BTRFS: decompress failed!\n"); |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 402 | ret = -EIO; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 403 | goto out; |
| 404 | } |
| 405 | |
| 406 | if (out_len < start_byte) { |
Zach Brown | 60e1975 | 2014-05-09 17:15:08 -0400 | [diff] [blame] | 407 | ret = -EIO; |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 408 | goto out; |
| 409 | } |
| 410 | |
Chris Mason | 2f19cad | 2014-11-30 08:56:33 -0500 | [diff] [blame] | 411 | /* |
| 412 | * the caller is already checking against PAGE_SIZE, but lets |
| 413 | * move this check closer to the memcpy/memset |
| 414 | */ |
| 415 | destlen = min_t(unsigned long, destlen, PAGE_SIZE); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 416 | bytes = min_t(unsigned long, destlen, out_len - start_byte); |
| 417 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 418 | kaddr = kmap_atomic(dest_page); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 419 | memcpy(kaddr, workspace->buf + start_byte, bytes); |
Chris Mason | 2f19cad | 2014-11-30 08:56:33 -0500 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * btrfs_getblock is doing a zero on the tail of the page too, |
| 423 | * but this will cover anything missing from the decompressed |
| 424 | * data. |
| 425 | */ |
| 426 | if (bytes < destlen) |
| 427 | memset(kaddr+bytes, 0, destlen-bytes); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 428 | kunmap_atomic(kaddr); |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 429 | out: |
| 430 | return ret; |
| 431 | } |
| 432 | |
David Sterba | e8c9f18 | 2015-01-02 18:23:10 +0100 | [diff] [blame] | 433 | const struct btrfs_compress_op btrfs_lzo_compress = { |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 434 | .alloc_workspace = lzo_alloc_workspace, |
| 435 | .free_workspace = lzo_free_workspace, |
| 436 | .compress_pages = lzo_compress_pages, |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 437 | .decompress_bio = lzo_decompress_bio, |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 438 | .decompress = lzo_decompress, |
| 439 | }; |