Linux Audio

Check our new training course

Loading...
v4.6
 
 1/*
 2 * Copyright (c) 2013
 3 * Phillip Lougher <phillip@squashfs.org.uk>
 4 *
 5 * This work is licensed under the terms of the GNU GPL, version 2. See
 6 * the COPYING file in the top-level directory.
 7 */
 8
 9#include <linux/types.h>
10#include <linux/slab.h>
11#include <linux/percpu.h>
12#include <linux/buffer_head.h>
13
14#include "squashfs_fs.h"
15#include "squashfs_fs_sb.h"
16#include "decompressor.h"
17#include "squashfs.h"
18
19/*
20 * This file implements multi-threaded decompression using percpu
21 * variables, one thread per cpu core.
22 */
23
24struct squashfs_stream {
25	void		*stream;
26};
27
28void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
29						void *comp_opts)
30{
31	struct squashfs_stream *stream;
32	struct squashfs_stream __percpu *percpu;
33	int err, cpu;
34
35	percpu = alloc_percpu(struct squashfs_stream);
36	if (percpu == NULL)
37		return ERR_PTR(-ENOMEM);
38
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);
44			goto out;
45		}
46	}
47
48	kfree(comp_opts);
49	return (__force void *) percpu;
50
51out:
52	for_each_possible_cpu(cpu) {
53		stream = per_cpu_ptr(percpu, cpu);
54		if (!IS_ERR_OR_NULL(stream->stream))
55			msblk->decompressor->free(stream->stream);
56	}
57	free_percpu(percpu);
58	return ERR_PTR(err);
59}
60
61void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
62{
63	struct squashfs_stream __percpu *percpu =
64			(struct squashfs_stream __percpu *) msblk->stream;
65	struct squashfs_stream *stream;
66	int cpu;
67
68	if (msblk->stream) {
69		for_each_possible_cpu(cpu) {
70			stream = per_cpu_ptr(percpu, cpu);
71			msblk->decompressor->free(stream->stream);
72		}
73		free_percpu(percpu);
74	}
75}
76
77int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh,
78	int b, int offset, int length, struct squashfs_page_actor *output)
79{
80	struct squashfs_stream __percpu *percpu =
81			(struct squashfs_stream __percpu *) msblk->stream;
82	struct squashfs_stream *stream = get_cpu_ptr(percpu);
83	int res = msblk->decompressor->decompress(msblk, stream->stream, bh, b,
84		offset, length, output);
85	put_cpu_ptr(stream);
86
87	if (res < 0)
88		ERROR("%s decompression failed, data probably corrupt\n",
89			msblk->decompressor->name);
90
91	return res;
92}
93
94int squashfs_max_decompressors(void)
95{
96	return num_possible_cpus();
97}
v5.4
 1// SPDX-License-Identifier: GPL-2.0-only
 2/*
 3 * Copyright (c) 2013
 4 * Phillip Lougher <phillip@squashfs.org.uk>
 
 
 
 5 */
 6
 7#include <linux/types.h>
 8#include <linux/slab.h>
 9#include <linux/percpu.h>
10#include <linux/buffer_head.h>
11
12#include "squashfs_fs.h"
13#include "squashfs_fs_sb.h"
14#include "decompressor.h"
15#include "squashfs.h"
16
17/*
18 * This file implements multi-threaded decompression using percpu
19 * variables, one thread per cpu core.
20 */
21
22struct squashfs_stream {
23	void		*stream;
24};
25
26void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
27						void *comp_opts)
28{
29	struct squashfs_stream *stream;
30	struct squashfs_stream __percpu *percpu;
31	int err, cpu;
32
33	percpu = alloc_percpu(struct squashfs_stream);
34	if (percpu == NULL)
35		return ERR_PTR(-ENOMEM);
36
37	for_each_possible_cpu(cpu) {
38		stream = per_cpu_ptr(percpu, cpu);
39		stream->stream = msblk->decompressor->init(msblk, comp_opts);
40		if (IS_ERR(stream->stream)) {
41			err = PTR_ERR(stream->stream);
42			goto out;
43		}
44	}
45
46	kfree(comp_opts);
47	return (__force void *) percpu;
48
49out:
50	for_each_possible_cpu(cpu) {
51		stream = per_cpu_ptr(percpu, cpu);
52		if (!IS_ERR_OR_NULL(stream->stream))
53			msblk->decompressor->free(stream->stream);
54	}
55	free_percpu(percpu);
56	return ERR_PTR(err);
57}
58
59void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
60{
61	struct squashfs_stream __percpu *percpu =
62			(struct squashfs_stream __percpu *) msblk->stream;
63	struct squashfs_stream *stream;
64	int cpu;
65
66	if (msblk->stream) {
67		for_each_possible_cpu(cpu) {
68			stream = per_cpu_ptr(percpu, cpu);
69			msblk->decompressor->free(stream->stream);
70		}
71		free_percpu(percpu);
72	}
73}
74
75int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh,
76	int b, int offset, int length, struct squashfs_page_actor *output)
77{
78	struct squashfs_stream __percpu *percpu =
79			(struct squashfs_stream __percpu *) msblk->stream;
80	struct squashfs_stream *stream = get_cpu_ptr(percpu);
81	int res = msblk->decompressor->decompress(msblk, stream->stream, bh, b,
82		offset, length, output);
83	put_cpu_ptr(stream);
84
85	if (res < 0)
86		ERROR("%s decompression failed, data probably corrupt\n",
87			msblk->decompressor->name);
88
89	return res;
90}
91
92int squashfs_max_decompressors(void)
93{
94	return num_possible_cpus();
95}