]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - drivers/staging/erofs/decompressor.c
staging: erofs: introduce LZ4 decompression inplace
[linux.git] / drivers / staging / erofs / decompressor.c
index df8fd68a338b9d9d4b6d0fc036122062e831117e..80f1f39719ba2825d1ef0a66ddcd646498aba7cb 100644 (file)
@@ -14,6 +14,9 @@
 #endif
 
 #define LZ4_MAX_DISTANCE_PAGES DIV_ROUND_UP(LZ4_DISTANCE_MAX, PAGE_SIZE)
+#ifndef LZ4_DECOMPRESS_INPLACE_MARGIN
+#define LZ4_DECOMPRESS_INPLACE_MARGIN(srcsize)  (((srcsize) >> 8) + 32)
+#endif
 
 struct z_erofs_decompressor {
        /*
@@ -112,7 +115,7 @@ static int lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
 {
        unsigned int inputmargin, inlen;
        u8 *src;
-       bool copied;
+       bool copied, support_0padding;
        int ret;
 
        if (rq->inputsize > PAGE_SIZE)
@@ -120,13 +123,38 @@ static int lz4_decompress(struct z_erofs_decompress_req *rq, u8 *out)
 
        src = kmap_atomic(*rq->in);
        inputmargin = 0;
+       support_0padding = false;
+
+       /* decompression inplace is only safe when 0padding is enabled */
+       if (EROFS_SB(rq->sb)->requirements & EROFS_REQUIREMENT_LZ4_0PADDING) {
+               support_0padding = true;
+
+               while (!src[inputmargin & ~PAGE_MASK])
+                       if (!(++inputmargin & ~PAGE_MASK))
+                               break;
+
+               if (inputmargin >= rq->inputsize) {
+                       kunmap_atomic(src);
+                       return -EIO;
+               }
+       }
 
        copied = false;
        inlen = rq->inputsize - inputmargin;
        if (rq->inplace_io) {
-               src = generic_copy_inplace_data(rq, src, inputmargin);
-               inputmargin = 0;
-               copied = true;
+               const uint oend = (rq->pageofs_out +
+                                  rq->outputsize) & ~PAGE_MASK;
+               const uint nr = PAGE_ALIGN(rq->pageofs_out +
+                                          rq->outputsize) >> PAGE_SHIFT;
+
+               if (rq->partial_decoding || !support_0padding ||
+                   rq->out[nr - 1] != rq->in[0] ||
+                   rq->inputsize - oend <
+                     LZ4_DECOMPRESS_INPLACE_MARGIN(inlen)) {
+                       src = generic_copy_inplace_data(rq, src, inputmargin);
+                       inputmargin = 0;
+                       copied = true;
+               }
        }
 
        ret = LZ4_decompress_safe_partial(src + inputmargin, out,