squashfs: Add zstd support

Add zstd compression and decompression support to SquashFS. zstd is a
great fit for SquashFS because it can compress at ratios approaching xz,
while decompressing twice as fast as zlib. For SquashFS in particular,
it can decompress as fast as lzo and lz4. It also has the flexibility
to turn down the compression ratio for faster compression times.

The compression benchmark is run on the file tree from the SquashFS archive
found in ubuntu-16.10-desktop-amd64.iso [1]. It uses `mksquashfs` with the
default block size (128 KB) and and various compression algorithms/levels.
xz and zstd are also benchmarked with 256 KB blocks. The decompression
benchmark times how long it takes to `tar` the file tree into `/dev/null`.
See the benchmark file in the upstream zstd source repository located under
`contrib/linux-kernel/squashfs-benchmark.sh` [2] for details.

I ran the benchmarks on a Ubuntu 14.04 VM with 2 cores and 4 GiB of RAM.
The VM is running on a MacBook Pro with a 3.1 GHz Intel Core i7 processor,
16 GB of RAM, and a SSD.

| Method | Ratio | Compression MB/s | Decompression MB/s |
|----------------|-------|------------------|--------------------|
| gzip | 2.92 | 15 | 128 |
| lzo | 2.64 | 9.5 | 217 |
| lz4 | 2.12 | 94 | 218 |
| xz | 3.43 | 5.5 | 35 |
| xz 256 KB | 3.53 | 5.4 | 40 |
| zstd 1 | 2.71 | 96 | 210 |
| zstd 5 | 2.93 | 69 | 198 |
| zstd 10 | 3.01 | 41 | 225 |
| zstd 15 | 3.13 | 11.4 | 224 |
| zstd 16 256 KB | 3.24 | 8.1 | 210 |

This patch was written by Sean Purcell <me@seanp.xyz>, but I will be
taking over the submission process.

[1] http://releases.ubuntu.com/16.10/
[2] https://github.com/facebook/zstd/blob/dev/contrib/linux-kernel/squashfs-benchmark.sh

zstd source repository: https://github.com/facebook/zstd

Signed-off-by: Sean Purcell <me@seanp.xyz>
Signed-off-by: Nick Terrell <terrelln@fb.com>
Signed-off-by: Chris Mason <clm@fb.com>
Acked-by: Phillip Lougher <phillip@squashfs.org.uk>

authored by

Sean Purcell and committed by
Chris Mason
87bf54bb 5c1aab1d

+178
+14
fs/squashfs/Kconfig
··· 165 165 166 166 If unsure, say N. 167 167 168 + config SQUASHFS_ZSTD 169 + bool "Include support for ZSTD compressed file systems" 170 + depends on SQUASHFS 171 + select ZSTD_DECOMPRESS 172 + help 173 + Saying Y here includes support for reading Squashfs file systems 174 + compressed with ZSTD compression. ZSTD gives better compression than 175 + the default ZLIB compression, while using less CPU. 176 + 177 + ZSTD is not the standard compression used in Squashfs and so most 178 + file systems will be readable without selecting this option. 179 + 180 + If unsure, say N. 181 + 168 182 config SQUASHFS_4K_DEVBLK_SIZE 169 183 bool "Use 4K device block size?" 170 184 depends on SQUASHFS
+1
fs/squashfs/Makefile
··· 15 15 squashfs-$(CONFIG_SQUASHFS_LZO) += lzo_wrapper.o 16 16 squashfs-$(CONFIG_SQUASHFS_XZ) += xz_wrapper.o 17 17 squashfs-$(CONFIG_SQUASHFS_ZLIB) += zlib_wrapper.o 18 + squashfs-$(CONFIG_SQUASHFS_ZSTD) += zstd_wrapper.o
+7
fs/squashfs/decompressor.c
··· 65 65 }; 66 66 #endif 67 67 68 + #ifndef CONFIG_SQUASHFS_ZSTD 69 + static const struct squashfs_decompressor squashfs_zstd_comp_ops = { 70 + NULL, NULL, NULL, NULL, ZSTD_COMPRESSION, "zstd", 0 71 + }; 72 + #endif 73 + 68 74 static const struct squashfs_decompressor squashfs_unknown_comp_ops = { 69 75 NULL, NULL, NULL, NULL, 0, "unknown", 0 70 76 }; ··· 81 75 &squashfs_lzo_comp_ops, 82 76 &squashfs_xz_comp_ops, 83 77 &squashfs_lzma_unsupported_comp_ops, 78 + &squashfs_zstd_comp_ops, 84 79 &squashfs_unknown_comp_ops 85 80 }; 86 81
+4
fs/squashfs/decompressor.h
··· 58 58 extern const struct squashfs_decompressor squashfs_zlib_comp_ops; 59 59 #endif 60 60 61 + #ifdef CONFIG_SQUASHFS_ZSTD 62 + extern const struct squashfs_decompressor squashfs_zstd_comp_ops; 63 + #endif 64 + 61 65 #endif
+1
fs/squashfs/squashfs_fs.h
··· 241 241 #define LZO_COMPRESSION 3 242 242 #define XZ_COMPRESSION 4 243 243 #define LZ4_COMPRESSION 5 244 + #define ZSTD_COMPRESSION 6 244 245 245 246 struct squashfs_super_block { 246 247 __le32 s_magic;
+151
fs/squashfs/zstd_wrapper.c
··· 1 + /* 2 + * Squashfs - a compressed read only filesystem for Linux 3 + * 4 + * Copyright (c) 2016-present, Facebook, Inc. 5 + * All rights reserved. 6 + * 7 + * This program is free software; you can redistribute it and/or 8 + * modify it under the terms of the GNU General Public License 9 + * as published by the Free Software Foundation; either version 2, 10 + * or (at your option) any later version. 11 + * 12 + * This program is distributed in the hope that it will be useful, 13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 + * GNU General Public License for more details. 16 + * 17 + * zstd_wrapper.c 18 + */ 19 + 20 + #include <linux/mutex.h> 21 + #include <linux/buffer_head.h> 22 + #include <linux/slab.h> 23 + #include <linux/zstd.h> 24 + #include <linux/vmalloc.h> 25 + 26 + #include "squashfs_fs.h" 27 + #include "squashfs_fs_sb.h" 28 + #include "squashfs.h" 29 + #include "decompressor.h" 30 + #include "page_actor.h" 31 + 32 + struct workspace { 33 + void *mem; 34 + size_t mem_size; 35 + size_t window_size; 36 + }; 37 + 38 + static void *zstd_init(struct squashfs_sb_info *msblk, void *buff) 39 + { 40 + struct workspace *wksp = kmalloc(sizeof(*wksp), GFP_KERNEL); 41 + 42 + if (wksp == NULL) 43 + goto failed; 44 + wksp->window_size = max_t(size_t, 45 + msblk->block_size, SQUASHFS_METADATA_SIZE); 46 + wksp->mem_size = ZSTD_DStreamWorkspaceBound(wksp->window_size); 47 + wksp->mem = vmalloc(wksp->mem_size); 48 + if (wksp->mem == NULL) 49 + goto failed; 50 + 51 + return wksp; 52 + 53 + failed: 54 + ERROR("Failed to allocate zstd workspace\n"); 55 + kfree(wksp); 56 + return ERR_PTR(-ENOMEM); 57 + } 58 + 59 + 60 + static void zstd_free(void *strm) 61 + { 62 + struct workspace *wksp = strm; 63 + 64 + if (wksp) 65 + vfree(wksp->mem); 66 + kfree(wksp); 67 + } 68 + 69 + 70 + static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm, 71 + struct buffer_head **bh, int b, int offset, int length, 72 + struct squashfs_page_actor *output) 73 + { 74 + struct workspace *wksp = strm; 75 + ZSTD_DStream *stream; 76 + size_t total_out = 0; 77 + size_t zstd_err; 78 + int k = 0; 79 + ZSTD_inBuffer in_buf = { NULL, 0, 0 }; 80 + ZSTD_outBuffer out_buf = { NULL, 0, 0 }; 81 + 82 + stream = ZSTD_initDStream(wksp->window_size, wksp->mem, wksp->mem_size); 83 + 84 + if (!stream) { 85 + ERROR("Failed to initialize zstd decompressor\n"); 86 + goto out; 87 + } 88 + 89 + out_buf.size = PAGE_SIZE; 90 + out_buf.dst = squashfs_first_page(output); 91 + 92 + do { 93 + if (in_buf.pos == in_buf.size && k < b) { 94 + int avail = min(length, msblk->devblksize - offset); 95 + 96 + length -= avail; 97 + in_buf.src = bh[k]->b_data + offset; 98 + in_buf.size = avail; 99 + in_buf.pos = 0; 100 + offset = 0; 101 + } 102 + 103 + if (out_buf.pos == out_buf.size) { 104 + out_buf.dst = squashfs_next_page(output); 105 + if (out_buf.dst == NULL) { 106 + /* Shouldn't run out of pages 107 + * before stream is done. 108 + */ 109 + squashfs_finish_page(output); 110 + goto out; 111 + } 112 + out_buf.pos = 0; 113 + out_buf.size = PAGE_SIZE; 114 + } 115 + 116 + total_out -= out_buf.pos; 117 + zstd_err = ZSTD_decompressStream(stream, &out_buf, &in_buf); 118 + total_out += out_buf.pos; /* add the additional data produced */ 119 + 120 + if (in_buf.pos == in_buf.size && k < b) 121 + put_bh(bh[k++]); 122 + } while (zstd_err != 0 && !ZSTD_isError(zstd_err)); 123 + 124 + squashfs_finish_page(output); 125 + 126 + if (ZSTD_isError(zstd_err)) { 127 + ERROR("zstd decompression error: %d\n", 128 + (int)ZSTD_getErrorCode(zstd_err)); 129 + goto out; 130 + } 131 + 132 + if (k < b) 133 + goto out; 134 + 135 + return (int)total_out; 136 + 137 + out: 138 + for (; k < b; k++) 139 + put_bh(bh[k]); 140 + 141 + return -EIO; 142 + } 143 + 144 + const struct squashfs_decompressor squashfs_zstd_comp_ops = { 145 + .init = zstd_init, 146 + .free = zstd_free, 147 + .decompress = zstd_uncompress, 148 + .id = ZSTD_COMPRESSION, 149 + .name = "zstd", 150 + .supported = 1 151 + };