ReactOS 0.4.16-dev-91-g764881a
calcthread.c File Reference
#include "btrfs_drv.h"
#include "xxhash.h"
#include "crc32c.h"
Include dependency graph for calcthread.c:

Go to the source code of this file.

Functions

void calc_thread_main (device_extension *Vcb, calc_job *cj)
 
void do_calc_job (device_extension *Vcb, uint8_t *data, uint32_t sectors, void *csum)
 
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)
 
NTSTATUS add_calc_job_comp (device_extension *Vcb, uint8_t compression, void *in, unsigned int inlen, void *out, unsigned int outlen, calc_job **pcj)
 
 _Function_class_ (KSTART_ROUTINE)
 

Function Documentation

◆ _Function_class_()

_Function_class_ ( KSTART_ROUTINE  )

Definition at line 290 of file calcthread.c.

291 {
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}
static HANDLE thread
Definition: service.c:33
void calc_thread_main(device_extension *Vcb, calc_job *cj)
Definition: calcthread.c:22
#define NULL
Definition: types.h:112
ULONG_PTR KAFFINITY
Definition: compat.h:85
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define KernelMode
Definition: asm.h:34
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1145
#define Vcb
Definition: cdprocs.h:1415
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: http.c:7252
VOID NTAPI KeSetSystemAffinityThread(IN KAFFINITY Affinity)
Definition: thrdobj.c:1107
@ Executive
Definition: ketypes.h:415
#define ObDereferenceObject
Definition: obfuncs.h:203
#define ObReferenceObject
Definition: obfuncs.h:204

◆ add_calc_job_comp()

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 at line 237 of file calcthread.c.

238 {
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}
#define ERR(fmt,...)
Definition: precomp.h:57
#define ALLOC_TAG
Definition: btrfs_drv.h:87
_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_comp_zstd
Definition: btrfs_drv.h:637
@ calc_thread_comp_zlib
Definition: btrfs_drv.h:635
#define BTRFS_COMPRESSION_LZO
Definition: btrfs.h:67
#define BTRFS_COMPRESSION_ZLIB
Definition: btrfs.h:66
#define BTRFS_COMPRESSION_ZSTD
Definition: btrfs.h:68
KIRQL irql
Definition: wave.h:1
#define InsertTailList(ListHead, Entry)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#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
GLuint in
Definition: glext.h:9616
@ NotificationEvent
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
static FILE * out
Definition: regtests2xml.c:44
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ ULONG * pcj
Definition: winddi.h:3642
_In_ ULONG cj
Definition: winddi.h:3540

Referenced by write_compressed().

◆ add_calc_job_decomp()

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 at line 183 of file calcthread.c.

184 {
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}
@ calc_thread_decomp_zlib
Definition: btrfs_drv.h:632
@ calc_thread_decomp_zstd
Definition: btrfs_drv.h:634
@ calc_thread_decomp_lzo
Definition: btrfs_drv.h:633

◆ calc_thread_main()

void calc_thread_main ( device_extension Vcb,
calc_job cj 
)

Definition at line 22 of file calcthread.c.

22 {
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}
#define InterlockedDecrement
Definition: armddk.h:52
void blake2b(void *out, size_t outlen, const void *in, size_t inlen)
Definition: blake2b-ref.c:237
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 BLAKE2_HASH_SIZE
Definition: btrfs_drv.h:1252
@ calc_thread_sha256
Definition: btrfs_drv.h:630
@ calc_thread_crc32c
Definition: btrfs_drv.h:628
@ calc_thread_blake2
Definition: btrfs_drv.h:631
@ calc_thread_xxhash
Definition: btrfs_drv.h:629
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
crc_func calc_crc32c
Definition: crc32c.c:23
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
GLenum src
Definition: glext.h:6340
static char * dest
Definition: rtl.c:135
BYTE uint8_t
Definition: msvideo1.c:66
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: list.h:27
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
XXH_PUBLIC_API unsigned long long XXH64(const void *input, size_t len, unsigned long long seed)
Definition: xxhash.c:555

Referenced by _Function_class_(), do_calc_job(), and write_compressed().

◆ do_calc_job()

void do_calc_job ( device_extension Vcb,
uint8_t data,
uint32_t  sectors,
void csum 
)

Definition at line 141 of file calcthread.c.

141 {
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}
#define CSUM_TYPE_SHA256
Definition: btrfs.h:134
#define CSUM_TYPE_XXHASH
Definition: btrfs.h:133
#define CSUM_TYPE_BLAKE2
Definition: btrfs.h:135
#define CSUM_TYPE_CRC32C
Definition: btrfs.h:132
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
__u8 sectors[2]
Definition: mkdosfs.c:8
Definition: ffs.h:52

Referenced by __attribute__(), check_csum(), insert_extent_chunk(), scrub_extent_dup(), scrub_extent_raid10(), and write_compressed().