ReactOS  0.4.15-dev-1177-g6cb3b62
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) {
52  case calc_thread_crc32c:
53  case calc_thread_xxhash:
54  case calc_thread_sha256:
55  case calc_thread_blake2:
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) {
74  case calc_thread_crc32c:
75  *(uint32_t*)dest = ~calc_crc32c(0xffffffff, src, Vcb->superblock.sector_size);
76  break;
77 
78  case calc_thread_xxhash:
79  *(uint64_t*)dest = XXH64(src, Vcb->superblock.sector_size, 0);
80  break;
81 
82  case calc_thread_sha256:
83  calc_sha256(dest, src, Vcb->superblock.sector_size);
84  break;
85 
86  case calc_thread_blake2:
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) {
204  cj->type = calc_thread_decomp_zlib;
205  break;
206 
208  cj->type = calc_thread_decomp_lzo;
209  break;
210 
212  cj->type = calc_thread_decomp_zstd;
213  break;
214 
215  default:
216  ERR("unexpected compression type %x\n", compression);
217  ExFreePool(cj);
218  return STATUS_NOT_SUPPORTED;
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) {
257  cj->type = calc_thread_comp_zlib;
258  break;
259 
261  cj->type = calc_thread_comp_lzo;
262  break;
263 
265  cj->type = calc_thread_comp_zstd;
266  break;
267 
268  default:
269  ERR("unexpected compression type %x\n", compression);
270  ExFreePool(cj);
271  return STATUS_NOT_SUPPORTED;
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)
291 void __stdcall calc_thread(void* context) {
293  device_extension* Vcb = thread->DeviceObject->DeviceExtension;
294 
295  ObReferenceObject(thread->DeviceObject);
296 
297  KeSetSystemAffinityThread((KAFFINITY)(1 << thread->number));
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 }
NTSTATUS zstd_decompress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen)
Definition: compress.c:676
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
LONG left
Definition: btrfs_drv.h:654
_In_ ULONG cj
Definition: winddi.h:3540
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
void * in
Definition: btrfs_drv.h:651
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
Definition: http.c:7094
#define CSUM_TYPE_BLAKE2
Definition: btrfs.h:128
_In_ ULONG * pcj
Definition: winddi.h:3642
void blake2b(void *out, size_t outlen, const void *in, size_t inlen)
Definition: blake2b-ref.c:237
LONG NTSTATUS
Definition: precomp.h:26
KEVENT event
Definition: btrfs_drv.h:655
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
#define BTRFS_COMPRESSION_LZO
Definition: btrfs.h:63
#define BTRFS_COMPRESSION_ZSTD
Definition: btrfs.h:64
#define CSUM_TYPE_CRC32C
Definition: btrfs.h:125
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:807
void calc_sha256(uint8_t *hash, const void *input, size_t len)
Definition: sha256.c:126
#define InsertTailList(ListHead, Entry)
void * out
Definition: btrfs_drv.h:652
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
#define BTRFS_COMPRESSION_ZLIB
Definition: btrfs.h:62
VOID NTAPI KeSetSystemAffinityThread(IN KAFFINITY Affinity)
Definition: thrdobj.c:1116
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
#define ALLOC_TAG
Definition: btrfs_drv.h:91
UCHAR KIRQL
Definition: env_spec_w32.h:591
smooth NULL
Definition: ftsmooth.c:416
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define Vcb
Definition: cdprocs.h:1415
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
static FILE * out
Definition: regtests2xml.c:44
void do_calc_job(device_extension *Vcb, uint8_t *data, uint32_t sectors, void *csum)
Definition: calcthread.c:141
#define __stdcall
Definition: typedefs.h:25
#define CSUM_TYPE_XXHASH
Definition: btrfs.h:126
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
static HANDLE thread
Definition: service.c:33
LONG not_started
Definition: btrfs_drv.h:654
#define InterlockedDecrement
Definition: armddk.h:52
_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:1357
NTSTATUS lzo_decompress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen, uint32_t inpageoff)
Definition: compress.c:278
crc_func calc_crc32c
Definition: crc32c.c:23
GLenum src
Definition: glext.h:6340
BYTE uint8_t
Definition: msvideo1.c:66
XXH_PUBLIC_API unsigned long long XXH64(const void *input, size_t len, unsigned long long seed)
Definition: xxhash.c:555
NTSTATUS lzo_compress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen, unsigned int *space_left)
Definition: compress.c:722
#define CSUM_TYPE_SHA256
Definition: btrfs.h:127
#define ERR(fmt,...)
Definition: debug.h:110
NTSTATUS Status
Definition: btrfs_drv.h:657
NTSTATUS zlib_decompress(uint8_t *inbuf, uint32_t inlen, uint8_t *outbuf, uint32_t outlen)
Definition: compress.c:377
UINT64 uint64_t
Definition: types.h:77
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
GLuint in
Definition: glext.h:9616
ULONG_PTR KAFFINITY
Definition: compat.h:85
unsigned int inlen
Definition: btrfs_drv.h:653
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1144
Definition: list.h:27
void calc_thread_main(device_extension *Vcb, calc_job *cj)
Definition: calcthread.c:22
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
__u8 sectors[2]
Definition: mkdosfs.c:366
UINT32 uint32_t
Definition: types.h:75
KIRQL irql
Definition: wave.h:1
Definition: fs.h:216
unsigned int outlen
Definition: btrfs_drv.h:653
#define BLAKE2_HASH_SIZE
Definition: btrfs_drv.h:1252
#define ObReferenceObject
Definition: obfuncs.h:204
LIST_ENTRY list_entry
Definition: btrfs_drv.h:650
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
_Function_class_(KSTART_ROUTINE)
Definition: calcthread.c:290
enum calc_thread_type type
Definition: btrfs_drv.h:656
static char * dest
Definition: rtl.c:135
unsigned int off
Definition: btrfs_drv.h:653
unsigned int space_left
Definition: btrfs_drv.h:653
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
return STATUS_SUCCESS
Definition: btrfs.c:3014
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
void * csum
Definition: write.c:2807
off
Definition: i386-dis.c:3909