1 // SPDX-License-Identifier: GPL-2.0-only
4 * Phillip Lougher <phillip@squashfs.org.uk>
7 #include <linux/types.h>
8 #include <linux/slab.h>
9 #include <linux/percpu.h>
10 #include <linux/buffer_head.h>
11 #include <linux/local_lock.h>
13 #include "squashfs_fs.h"
14 #include "squashfs_fs_sb.h"
15 #include "decompressor.h"
19 * This file implements multi-threaded decompression using percpu
20 * variables, one thread per cpu core.
23 struct squashfs_stream {
28 void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
31 struct squashfs_stream *stream;
32 struct squashfs_stream __percpu *percpu;
35 percpu = alloc_percpu(struct squashfs_stream);
37 return ERR_PTR(-ENOMEM);
39 for_each_possible_cpu(cpu) {
40 stream = per_cpu_ptr(percpu, cpu);
41 stream->stream = msblk->decompressor->init(msblk, comp_opts);
42 if (IS_ERR(stream->stream)) {
43 err = PTR_ERR(stream->stream);
46 local_lock_init(&stream->lock);
50 return (__force void *) percpu;
53 for_each_possible_cpu(cpu) {
54 stream = per_cpu_ptr(percpu, cpu);
55 if (!IS_ERR_OR_NULL(stream->stream))
56 msblk->decompressor->free(stream->stream);
62 void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
64 struct squashfs_stream __percpu *percpu =
65 (struct squashfs_stream __percpu *) msblk->stream;
66 struct squashfs_stream *stream;
70 for_each_possible_cpu(cpu) {
71 stream = per_cpu_ptr(percpu, cpu);
72 msblk->decompressor->free(stream->stream);
78 int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
79 int offset, int length, struct squashfs_page_actor *output)
81 struct squashfs_stream *stream;
84 local_lock(&msblk->stream->lock);
85 stream = this_cpu_ptr(msblk->stream);
87 res = msblk->decompressor->decompress(msblk, stream->stream, bio,
88 offset, length, output);
90 local_unlock(&msblk->stream->lock);
93 ERROR("%s decompression failed, data probably corrupt\n",
94 msblk->decompressor->name);
99 int squashfs_max_decompressors(void)
101 return num_possible_cpus();