^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2008 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/lzo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/refcount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "compression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define LZO_LEN 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Btrfs LZO compression format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Regular and inlined LZO compressed data extents consist of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * 1. Header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Fixed size. LZO_LEN (4) bytes long, LE32.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Records the total size (including the header) of compressed data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * 2. Segment(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Variable size. Each segment includes one segment header, followed by data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * One regular LZO compressed extent can have one or more segments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * For inlined LZO compressed extent, only one segment is allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * One segment represents at most one page of uncompressed data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * 2.1 Segment header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * Fixed size. LZO_LEN (4) bytes long, LE32.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Records the total size of the segment (not including the header).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * Segment header never crosses page boundary, thus it's possible to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * have at most 3 padding zeros at the end of the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * 2.2 Data Payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Variable size. Size up limit should be lzo1x_worst_compress(PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * which is 4419 for a 4KiB page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Page 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * 0 0x2 0x4 0x6 0x8 0xa 0xc 0xe 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * 0x0000 | Header | SegHdr 01 | Data payload 01 ... |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * 0x0ff0 | SegHdr N | Data payload N ... |00|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * ^^ padding zeros
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Page 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * 0x1000 | SegHdr N+1| Data payload N+1 ... |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct workspace {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) void *buf; /* where decompressed data goes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) void *cbuf; /* where compressed data goes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static struct workspace_manager wsm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) void lzo_free_workspace(struct list_head *ws)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct workspace *workspace = list_entry(ws, struct workspace, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) kvfree(workspace->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) kvfree(workspace->cbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) kvfree(workspace->mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) kfree(workspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct list_head *lzo_alloc_workspace(unsigned int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct workspace *workspace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) workspace = kzalloc(sizeof(*workspace), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!workspace)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) workspace->mem = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) workspace->buf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) workspace->cbuf = kvmalloc(lzo1x_worst_compress(PAGE_SIZE), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!workspace->mem || !workspace->buf || !workspace->cbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) INIT_LIST_HEAD(&workspace->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return &workspace->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) lzo_free_workspace(&workspace->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static inline void write_compress_length(char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) __le32 dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) dlen = cpu_to_le32(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) memcpy(buf, &dlen, LZO_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static inline size_t read_compress_length(const char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) __le32 dlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) memcpy(&dlen, buf, LZO_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return le32_to_cpu(dlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int lzo_compress_pages(struct list_head *ws, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) u64 start, struct page **pages, unsigned long *out_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned long *total_in, unsigned long *total_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct workspace *workspace = list_entry(ws, struct workspace, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) char *data_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) char *cpage_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct page *in_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct page *out_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) unsigned long bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) unsigned long len = *total_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned long nr_dest_pages = *out_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) const unsigned long max_out = nr_dest_pages * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) size_t in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) size_t out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) unsigned long tot_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) unsigned long tot_out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) unsigned long pg_bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) unsigned long out_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) unsigned long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *out_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *total_out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *total_in = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) in_page = find_get_page(mapping, start >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) data_in = kmap(in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * store the size of all chunks of compressed data in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * the first 4 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (out_page == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) cpage_out = kmap(out_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) out_offset = LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) tot_out = LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) pages[0] = out_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) nr_pages = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) pg_bytes_left = PAGE_SIZE - LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* compress at most one page of data each time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) in_len = min(len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) while (tot_in < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = lzo1x_1_compress(data_in, in_len, workspace->cbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) &out_len, workspace->mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret != LZO_E_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) pr_debug("BTRFS: lzo in loop returned %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* store the size of this chunk of compressed data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) write_compress_length(cpage_out + out_offset, out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) tot_out += LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) out_offset += LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) pg_bytes_left -= LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) tot_in += in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) tot_out += out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* copy bytes from the working buffer into the pages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) buf = workspace->cbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) while (out_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) bytes = min_t(unsigned long, pg_bytes_left, out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) memcpy(cpage_out + out_offset, buf, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) out_len -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pg_bytes_left -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) buf += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) out_offset += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * we need another page for writing out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * Note if there's less than 4 bytes left, we just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * skip to a new page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if ((out_len == 0 && pg_bytes_left < LZO_LEN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) pg_bytes_left == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (pg_bytes_left) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) memset(cpage_out + out_offset, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) pg_bytes_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) tot_out += pg_bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /* we're done, don't allocate new page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (out_len == 0 && tot_in >= len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) kunmap(out_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (nr_pages == nr_dest_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) out_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ret = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (out_page == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) cpage_out = kmap(out_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pages[nr_pages++] = out_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) pg_bytes_left = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) out_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* we're making it bigger, give up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (tot_in > 8192 && tot_in < tot_out) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ret = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* we're all done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (tot_in >= len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (tot_out > max_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) bytes_left = len - tot_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) kunmap(in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) put_page(in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) start += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) in_page = find_get_page(mapping, start >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) data_in = kmap(in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) in_len = min(bytes_left, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (tot_out >= tot_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ret = -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* store the size of all chunks of compressed data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) cpage_out = kmap(pages[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) write_compress_length(cpage_out, tot_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) kunmap(pages[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *total_out = tot_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) *total_in = tot_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) *out_pages = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (out_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) kunmap(out_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (in_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) kunmap(in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) put_page(in_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct workspace *workspace = list_entry(ws, struct workspace, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int ret = 0, ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) char *data_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) unsigned long page_in_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) size_t srclen = cb->compressed_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) unsigned long buf_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) unsigned long buf_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) unsigned long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) unsigned long working_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) size_t in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) size_t out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) const size_t max_segment_len = lzo1x_worst_compress(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) unsigned long in_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) unsigned long in_page_bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) unsigned long tot_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) unsigned long tot_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) unsigned long tot_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) bool may_late_unmap, need_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct page **pages_in = cb->compressed_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) u64 disk_start = cb->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct bio *orig_bio = cb->orig_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) data_in = kmap(pages_in[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) tot_len = read_compress_length(data_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * Compressed data header check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * The real compressed size can't exceed the maximum extent length, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * all pages should be used (whole unused page with just the segment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * header is not possible). If this happens it means the compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * extent is corrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (tot_len > min_t(size_t, BTRFS_MAX_COMPRESSED, srclen) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) tot_len < srclen - PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) tot_in = LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) in_offset = LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) in_page_bytes_left = PAGE_SIZE - LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) tot_out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) while (tot_in < tot_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) in_len = read_compress_length(data_in + in_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) in_page_bytes_left -= LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) in_offset += LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) tot_in += LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * Segment header check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * The segment length must not exceed the maximum LZO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * compression size, nor the total compressed size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (in_len > max_segment_len || tot_in + in_len > tot_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) tot_in += in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) working_bytes = in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) may_late_unmap = need_unmap = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* fast path: avoid using the working buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (in_page_bytes_left >= in_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) buf = data_in + in_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) bytes = in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) may_late_unmap = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) goto cont;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /* copy bytes from the pages into the working buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) buf = workspace->cbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) buf_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) while (working_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) bytes = min(working_bytes, in_page_bytes_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) memcpy(buf + buf_offset, data_in + in_offset, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) buf_offset += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) cont:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) working_bytes -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) in_page_bytes_left -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) in_offset += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /* check if we need to pick another page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if ((working_bytes == 0 && in_page_bytes_left < LZO_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) || in_page_bytes_left == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) tot_in += in_page_bytes_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (working_bytes == 0 && tot_in >= tot_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (page_in_index + 1 >= total_pages_in) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (may_late_unmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) need_unmap = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) kunmap(pages_in[page_in_index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) data_in = kmap(pages_in[++page_in_index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) in_page_bytes_left = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) in_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) out_len = max_segment_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ret = lzo1x_decompress_safe(buf, in_len, workspace->buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) &out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (need_unmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) kunmap(pages_in[page_in_index - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (ret != LZO_E_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) pr_warn("BTRFS: decompress failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) buf_start = tot_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) tot_out += out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) ret2 = btrfs_decompress_buf2page(workspace->buf, buf_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) tot_out, disk_start, orig_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (ret2 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) kunmap(pages_in[page_in_index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) zero_fill_bio(orig_bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) int lzo_decompress(struct list_head *ws, unsigned char *data_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct page *dest_page, unsigned long start_byte, size_t srclen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) size_t destlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct workspace *workspace = list_entry(ws, struct workspace, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) size_t in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) size_t out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) size_t max_segment_len = lzo1x_worst_compress(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) char *kaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) unsigned long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (srclen < LZO_LEN || srclen > max_segment_len + LZO_LEN * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) in_len = read_compress_length(data_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (in_len != srclen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) data_in += LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) in_len = read_compress_length(data_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (in_len != srclen - LZO_LEN * 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) ret = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) data_in += LZO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) out_len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) ret = lzo1x_decompress_safe(data_in, in_len, workspace->buf, &out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (ret != LZO_E_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) pr_warn("BTRFS: decompress failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (out_len < start_byte) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * the caller is already checking against PAGE_SIZE, but lets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * move this check closer to the memcpy/memset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) destlen = min_t(unsigned long, destlen, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) bytes = min_t(unsigned long, destlen, out_len - start_byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) kaddr = kmap_atomic(dest_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) memcpy(kaddr, workspace->buf + start_byte, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * btrfs_getblock is doing a zero on the tail of the page too,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * but this will cover anything missing from the decompressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (bytes < destlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) memset(kaddr+bytes, 0, destlen-bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) const struct btrfs_compress_op btrfs_lzo_compress = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) .workspace_manager = &wsm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) .max_level = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) .default_level = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) };