ReactOS 0.4.15-dev-7988-g06a3508
calcthread.c
Go to the documentation of this file.
1/* Copyright (c) Mark Harmstone 2016-17
2 *
3 * This file is part of WinBtrfs.
4 *
5 * WinBtrfs is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public Licence as published by
7 * the Free Software Foundation, either version 3 of the Licence, or
8 * (at your option) any later version.
9 *
10 * WinBtrfs is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public Licence for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public Licence
16 * along with WinBtrfs. If not, see <http://www.gnu.org/licenses/>. */
17
18#include "btrfs_drv.h"
19#include "xxhash.h"
20#include "crc32c.h"
21
23 while (true) {
24 KIRQL irql;
25 calc_job* cj2;
26 uint8_t* src;
27 void* dest;
28 bool last_one = false;
29
30 KeAcquireSpinLock(&Vcb->calcthreads.spinlock, &irql);
31
32 if (cj && cj->not_started == 0) {
33 KeReleaseSpinLock(&Vcb->calcthreads.spinlock, irql);
34 break;
35 }
36
37 if (cj)
38 cj2 = cj;
39 else {
40 if (IsListEmpty(&Vcb->calcthreads.job_list)) {
41 KeReleaseSpinLock(&Vcb->calcthreads.spinlock, irql);
42 break;
43 }
44
45 cj2 = CONTAINING_RECORD(Vcb->calcthreads.job_list.Flink, calc_job, list_entry);
46 }
47
48 src = cj2->in;
49 dest = cj2->out;
50
51 switch (cj2->type) {
56 cj2->in = (uint8_t*)cj2->in + Vcb->superblock.sector_size;
57 cj2->out = (uint8_t*)cj2->out + Vcb->csum_size;
58 break;
59
60 default:
61 break;
62 }
63
64 cj2->not_started--;
65
66 if (cj2->not_started == 0) {
68 last_one = true;
69 }
70
71 KeReleaseSpinLock(&Vcb->calcthreads.spinlock, irql);
72
73 switch (cj2->type) {
75 *(uint32_t*)dest = ~calc_crc32c(0xffffffff, src, Vcb->superblock.sector_size);
76 break;
77
79 *(uint64_t*)dest = XXH64(src, Vcb->superblock.sector_size, 0);
80 break;
81
83 calc_sha256(dest, src, Vcb->superblock.sector_size);
84 break;
85
87 blake2b(dest, BLAKE2_HASH_SIZE, src, Vcb->superblock.sector_size);
88 break;
89
91 cj2->Status = zlib_decompress(src, cj2->inlen, dest, cj2->outlen);
92
93 if (!NT_SUCCESS(cj2->Status))
94 ERR("zlib_decompress returned %08lx\n", cj2->Status);
95 break;
96
98 cj2->Status = lzo_decompress(src, cj2->inlen, dest, cj2->outlen, cj2->off);
99
100 if (!NT_SUCCESS(cj2->Status))
101 ERR("lzo_decompress returned %08lx\n", cj2->Status);
102 break;
103
105 cj2->Status = zstd_decompress(src, cj2->inlen, dest, cj2->outlen);
106
107 if (!NT_SUCCESS(cj2->Status))
108 ERR("zstd_decompress returned %08lx\n", cj2->Status);
109 break;
110
112 cj2->Status = zlib_compress(src, cj2->inlen, dest, cj2->outlen, Vcb->options.zlib_level, &cj2->space_left);
113
114 if (!NT_SUCCESS(cj2->Status))
115 ERR("zlib_compress returned %08lx\n", cj2->Status);
116 break;
117
119 cj2->Status = lzo_compress(src, cj2->inlen, dest, cj2->outlen, &cj2->space_left);
120
121 if (!NT_SUCCESS(cj2->Status))
122 ERR("lzo_compress returned %08lx\n", cj2->Status);
123 break;
124
126 cj2->Status = zstd_compress(src, cj2->inlen, dest, cj2->outlen, Vcb->options.zstd_level, &cj2->space_left);
127
128 if (!NT_SUCCESS(cj2->Status))
129 ERR("zstd_compress returned %08lx\n", cj2->Status);
130 break;
131 }
132
133 if (InterlockedDecrement(&cj2->left) == 0)
134 KeSetEvent(&cj2->event, 0, false);
135
136 if (last_one)
137 break;
138 }
139}
140
142 KIRQL irql;
143 calc_job cj;
144
145 cj.in = data;
146 cj.out = csum;
147 cj.left = cj.not_started = sectors;
148
149 switch (Vcb->superblock.csum_type) {
150 case CSUM_TYPE_CRC32C:
151 cj.type = calc_thread_crc32c;
152 break;
153
154 case CSUM_TYPE_XXHASH:
155 cj.type = calc_thread_xxhash;
156 break;
157
158 case CSUM_TYPE_SHA256:
159 cj.type = calc_thread_sha256;
160 break;
161
162 case CSUM_TYPE_BLAKE2:
163 cj.type = calc_thread_blake2;
164 break;
165 }
166
167 KeInitializeEvent(&cj.event, NotificationEvent, false);
168
169 KeAcquireSpinLock(&Vcb->calcthreads.spinlock, &irql);
170
171 InsertTailList(&Vcb->calcthreads.job_list, &cj.list_entry);
172
173 KeSetEvent(&Vcb->calcthreads.event, 0, false);
174 KeClearEvent(&Vcb->calcthreads.event);
175
176 KeReleaseSpinLock(&Vcb->calcthreads.spinlock, irql);
177
179
181}
182
184 void* out, unsigned int outlen, unsigned int off, calc_job** pcj) {
185 calc_job* cj;
186 KIRQL irql;
187
189 if (!cj) {
190 ERR("out of memory\n");
192 }
193
194 cj->in = in;
195 cj->inlen = inlen;
196 cj->out = out;
197 cj->outlen = outlen;
198 cj->off = off;
199 cj->left = cj->not_started = 1;
200 cj->Status = STATUS_SUCCESS;
201
202 switch (compression) {
205 break;
206
209 break;
210
213 break;
214
215 default:
216 ERR("unexpected compression type %x\n", compression);
217 ExFreePool(cj);
219 }
220
221 KeInitializeEvent(&cj->event, NotificationEvent, false);
222
223 KeAcquireSpinLock(&Vcb->calcthreads.spinlock, &irql);
224
225 InsertTailList(&Vcb->calcthreads.job_list, &cj->list_entry);
226
227 KeSetEvent(&Vcb->calcthreads.event, 0, false);
228 KeClearEvent(&Vcb->calcthreads.event);
229
230 KeReleaseSpinLock(&Vcb->calcthreads.spinlock, irql);
231
232 *pcj = cj;
233
234 return STATUS_SUCCESS;
235}
236
238 void* out, unsigned int outlen, calc_job** pcj) {
239 calc_job* cj;
240 KIRQL irql;
241
243 if (!cj) {
244 ERR("out of memory\n");
246 }
247
248 cj->in = in;
249 cj->inlen = inlen;
250 cj->out = out;
251 cj->outlen = outlen;
252 cj->left = cj->not_started = 1;
253 cj->Status = STATUS_SUCCESS;
254
255 switch (compression) {
258 break;
259
261 cj->type = calc_thread_comp_lzo;
262 break;
263
266 break;
267
268 default:
269 ERR("unexpected compression type %x\n", compression);
270 ExFreePool(cj);
272 }
273
274 KeInitializeEvent(&cj->event, NotificationEvent, false);
275
276 KeAcquireSpinLock(&Vcb->calcthreads.spinlock, &irql);
277
278 InsertTailList(&Vcb->calcthreads.job_list, &cj->list_entry);
279
280 KeSetEvent(&Vcb->calcthreads.event, 0, false);
281 KeClearEvent(&Vcb->calcthreads.event);
282
283 KeReleaseSpinLock(&Vcb->calcthreads.spinlock, irql);
284
285 *pcj = cj;
286
287 return STATUS_SUCCESS;
288}
289
290_Function_class_(KSTART_ROUTINE)
291void __stdcall calc_thread(void* context) {
293 device_extension* Vcb = thread->DeviceObject->DeviceExtension;
294
295 ObReferenceObject(thread->DeviceObject);
296
298
299 while (true) {
300 KeWaitForSingleObject(&Vcb->calcthreads.event, Executive, KernelMode, false, NULL);
301
303
304 if (thread->quit)
305 break;
306 }
307
308 ObDereferenceObject(thread->DeviceObject);
309
310 KeSetEvent(&thread->finished, 0, false);
311
313}
#define InterlockedDecrement
Definition: armddk.h:52
LONG NTSTATUS
Definition: precomp.h:26
static HANDLE thread
Definition: service.c:33
void blake2b(void *out, size_t outlen, const void *in, size_t inlen)
Definition: blake2b-ref.c:237
#define ERR(fmt,...)
Definition: debug.h:110
NTSTATUS zlib_compress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen, unsigned int level, unsigned int *space_left)
Definition: compress.c:336
NTSTATUS zlib_decompress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen)
Definition: compress.c:377
NTSTATUS zstd_decompress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen)
Definition: compress.c:676
NTSTATUS lzo_decompress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen, uint32_t inpageoff)
Definition: compress.c:278
#define ALLOC_TAG
Definition: btrfs_drv.h:87
#define BLAKE2_HASH_SIZE
Definition: btrfs_drv.h:1252
_In_ fcb _In_ chunk _In_ uint64_t _In_ uint64_t _In_ bool _In_opt_ void _In_opt_ PIRP _In_ LIST_ENTRY _In_ uint8_t compression
Definition: btrfs_drv.h:1365
@ calc_thread_comp_lzo
Definition: btrfs_drv.h:636
@ calc_thread_sha256
Definition: btrfs_drv.h:630
@ calc_thread_decomp_zlib
Definition: btrfs_drv.h:632
@ calc_thread_crc32c
Definition: btrfs_drv.h:628
@ calc_thread_blake2
Definition: btrfs_drv.h:631
@ calc_thread_xxhash
Definition: btrfs_drv.h:629
@ calc_thread_decomp_zstd
Definition: btrfs_drv.h:634
@ calc_thread_comp_zstd
Definition: btrfs_drv.h:637
@ calc_thread_comp_zlib
Definition: btrfs_drv.h:635
@ calc_thread_decomp_lzo
Definition: btrfs_drv.h:633
void calc_sha256(uint8_t *hash, const void *input, size_t len)
Definition: sha256.c:126
NTSTATUS lzo_compress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen, unsigned int *space_left)
Definition: compress.c:727
NTSTATUS zstd_compress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen, uint32_t level, unsigned int *space_left)
Definition: compress.c:805
void do_calc_job(device_extension *Vcb, uint8_t *data, uint32_t sectors, void *csum)
Definition: calcthread.c:141
NTSTATUS add_calc_job_comp(device_extension *Vcb, uint8_t compression, void *in, unsigned int inlen, void *out, unsigned int outlen, calc_job **pcj)
Definition: calcthread.c:237
NTSTATUS add_calc_job_decomp(device_extension *Vcb, uint8_t compression, void *in, unsigned int inlen, void *out, unsigned int outlen, unsigned int off, calc_job **pcj)
Definition: calcthread.c:183
void calc_thread_main(device_extension *Vcb, calc_job *cj)
Definition: calcthread.c:22
crc_func calc_crc32c
Definition: crc32c.c:23
#define NULL
Definition: types.h:112
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
ULONG_PTR KAFFINITY
Definition: compat.h:85
#define BTRFS_COMPRESSION_LZO
Definition: btrfs.h:67
#define CSUM_TYPE_SHA256
Definition: btrfs.h:134
#define CSUM_TYPE_XXHASH
Definition: btrfs.h:133
#define BTRFS_COMPRESSION_ZLIB
Definition: btrfs.h:66
#define BTRFS_COMPRESSION_ZSTD
Definition: btrfs.h:68
#define CSUM_TYPE_BLAKE2
Definition: btrfs.h:135
#define CSUM_TYPE_CRC32C
Definition: btrfs.h:132
KIRQL irql
Definition: wave.h:1
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#define InsertTailList(ListHead, Entry)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define NonPagedPool
Definition: env_spec_w32.h:307
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLenum src
Definition: glext.h:6340
GLuint in
Definition: glext.h:9616
__u8 sectors[2]
Definition: mkdosfs.c:8
static char * dest
Definition: rtl.c:135
#define _Function_class_(x)
Definition: ms_sal.h:2946
BYTE uint8_t
Definition: msvideo1.c:66
#define KernelMode
Definition: asm.h:34
@ NotificationEvent
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1145
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
#define Vcb
Definition: cdprocs.h:1415
static FILE * out
Definition: regtests2xml.c:44
#define STATUS_SUCCESS
Definition: shellext.h:65
NTSTATUS Status
Definition: btrfs_drv.h:648
unsigned int space_left
Definition: btrfs_drv.h:644
void * in
Definition: btrfs_drv.h:642
LONG not_started
Definition: btrfs_drv.h:645
void * out
Definition: btrfs_drv.h:643
unsigned int inlen
Definition: btrfs_drv.h:644
LIST_ENTRY list_entry
Definition: btrfs_drv.h:641
unsigned int off
Definition: btrfs_drv.h:644
unsigned int outlen
Definition: btrfs_drv.h:644
KEVENT event
Definition: btrfs_drv.h:646
LONG left
Definition: btrfs_drv.h:645
enum calc_thread_type type
Definition: btrfs_drv.h:647
Definition: http.c:7252
Definition: ffs.h:52
Definition: list.h:27
VOID NTAPI KeSetSystemAffinityThread(IN KAFFINITY Affinity)
Definition: thrdobj.c:1107
#define __stdcall
Definition: typedefs.h:25
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ ULONG * pcj
Definition: winddi.h:3642
_In_ ULONG cj
Definition: winddi.h:3540
@ Executive
Definition: ketypes.h:415
#define ObDereferenceObject
Definition: obfuncs.h:203
#define ObReferenceObject
Definition: obfuncs.h:204
XXH_PUBLIC_API unsigned long long XXH64(const void *input, size_t len, unsigned long long seed)
Definition: xxhash.c:555