-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompression.c
979 lines (823 loc) · 22 KB
/
compression.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
#include <linux/zstd.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/sched/mm.h>
#include <linux/ratelimit.h>
#include <linux/uio.h>
#include "aeon.h"
#include "aeon_compression.h"
#define COMPRESSION_TYPE (1 & 0xf)
struct workspace {
void *mem;
size_t size;
char *buf;
struct list_head list;
ZSTD_inBuffer in_buf;
ZSTD_outBuffer out_buf;
};
#define ZSTD_AEON_MAX_WINDOWLOG 17
#define ZSTD_AEON_MAX_INPUT (1 << ZSTD_AEON_MAX_WINDOWLOG)
#define ZSTD_AEON_DEFAULT_LEVEL 1
static ZSTD_parameters zstd_get_aeon_parameters(size_t src_len)
{
ZSTD_parameters params = ZSTD_getParams(ZSTD_AEON_DEFAULT_LEVEL,
src_len, 0);
if (params.cParams.windowLog > ZSTD_AEON_MAX_WINDOWLOG)
params.cParams.windowLog = ZSTD_AEON_MAX_WINDOWLOG;
return params;
}
static void zstd_free_workspace(struct list_head *ws)
{
struct workspace *workspace = list_entry(ws, struct workspace, list);
kvfree(workspace->mem);
kfree(workspace->buf);
kfree(workspace);
}
static struct list_head *zstd_alloc_workspace(void)
{
ZSTD_parameters params = zstd_get_aeon_parameters(ZSTD_AEON_MAX_INPUT);
struct workspace *workspace;
workspace = kzalloc(sizeof(struct workspace), GFP_KERNEL);
if (!workspace)
return ERR_PTR(-ENOMEM);
workspace->size = max_t(size_t,
ZSTD_CStreamWorkspaceBound(params.cParams),
ZSTD_DStreamWorkspaceBound(1<<17));
workspace->mem = kvmalloc(workspace->size, GFP_KERNEL);
workspace->buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
if (!workspace->mem || !workspace->buf)
goto fail;
INIT_LIST_HEAD(&workspace->list);
return &workspace->list;
fail:
zstd_free_workspace(&workspace->list);
return ERR_PTR(-ENOMEM);
}
static int zstd_compress_pages(struct list_head *ws,
struct address_space *mapping,
u64 start, struct page **pages,
unsigned long *out_pages,
unsigned long *total_in,
unsigned long *total_out)
{
struct workspace *workspace = list_entry(ws, struct workspace, list);
ZSTD_CStream *stream;
int ret = 0;
int nr_pages = 0;
struct page *in_page = NULL; /* The current page to read */
struct page *out_page = NULL; /* The current page to write to */
unsigned long tot_in = 0;
unsigned long tot_out = 0;
unsigned long len = *total_out;
const unsigned long nr_dest_pages = *out_pages;
unsigned long max_out = nr_dest_pages * PAGE_SIZE;
ZSTD_parameters params = zstd_get_aeon_parameters(len);
*out_pages = 0;
*total_out = 0;
*total_in = 0;
/* Initialize the stream */
stream = ZSTD_initCStream(params, len, workspace->mem, workspace->size);
if (!stream) {
aeon_warn("ZSTD_initCStream failed\n");
ret = -EIO;
goto out;
}
/* map in the first page of input data */
/**
* find_get_page - find and get a page reference
*/
in_page = find_get_page(mapping, start >> PAGE_SHIFT);
// here could be changed
workspace->in_buf.src = kmap(in_page);
workspace->in_buf.pos = 0;
workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
/* Allocate and map in the output buffer */
out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
if (out_page == NULL) {
ret = -ENOMEM;
goto out;
}
pages[nr_pages++] = out_page;
workspace->out_buf.dst = kmap(out_page);
workspace->out_buf.pos = 0;
workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
while (1) {
size_t ret2;
ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
&workspace->in_buf);
if (ZSTD_isError(ret2)) {
aeon_dbg("ZSTD_compressionStream returned %d\n",
ZSTD_getErrorCode(ret2));
ret = -EIO;
goto out;
}
/* Check to see if we are making it bigger */
if (tot_in + workspace->in_buf.pos > 8192 &&
tot_in + workspace->in_buf.pos <
tot_out + workspace->out_buf.pos) {
ret = -E2BIG;
goto out;
}
/* Check if we need more output space */
if (workspace->out_buf.pos == workspace->out_buf.size) {
tot_out += PAGE_SIZE;
max_out -= PAGE_SIZE;
// here should be replaced by another one
kunmap(out_page);
if (nr_pages == nr_dest_pages) {
out_page = NULL;
ret = -E2BIG;
goto out;
}
out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
if (out_page == NULL) {
ret = -ENOMEM;
goto out;
}
pages[nr_pages++] = out_page;
workspace->out_buf.dst = kmap(out_page);
workspace->out_buf.pos = 0;
workspace->out_buf.size = min_t(size_t, max_out,
PAGE_SIZE);
}
/* We've reached the end of the input */
if (workspace->in_buf.pos >= len) {
tot_in += workspace->in_buf.pos;
break;
}
/* Check if we need more input */
// what means need more input?
if (workspace->in_buf.pos == workspace->in_buf.size) {
tot_in += PAGE_SIZE;
kunmap(in_page);
put_page(in_page);
start += PAGE_SIZE;
len -= PAGE_SIZE;
// find_get_page could be replaced by something like find_extent
in_page = find_get_page(mapping, start >> PAGE_SHIFT);
workspace->in_buf.src = kmap(in_page);
workspace->in_buf.pos = 0;
workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
}
}
while (1) {
size_t ret2;
ret2 = ZSTD_endStream(stream, &workspace->out_buf);
if (ZSTD_isError(ret2)) {
aeon_warn("ZSTD_endstream returned %d\n",
ZSTD_getErrorCode(ret2));
ret = -EIO;
goto out;
}
if (ret2 == 0) {
tot_out += workspace->out_buf.pos;
break;
}
if (workspace->out_buf.pos > max_out) {
tot_out += workspace->out_buf.pos;
ret = -E2BIG;
goto out;
}
tot_out += PAGE_SIZE;
max_out -= PAGE_SIZE;
kunmap(out_page);
if (nr_pages == nr_dest_pages) {
out_page = NULL;
ret = -E2BIG;
goto out;
}
out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
if (out_page == NULL) {
ret = -ENOMEM;
goto out;
}
pages[nr_pages++] = out_page;
workspace->out_buf.dst = kmap(out_page);
workspace->out_buf.pos = 0;
workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
}
if (tot_out >= tot_in) {
ret = -E2BIG;
goto out;
}
ret = 0;
*total_in = tot_in;
*total_out = tot_out;
out:
*out_pages = nr_pages;
/* Cleanup */
if (in_page) {
kunmap(in_page);
put_page(in_page);
}
if (out_page)
kunmap(out_page);
return ret;
}
static int
zstd_decompress(struct list_head *ws, const void *data_in,
unsigned long start_byte, size_t srclen,
size_t destlen, void *tmp, size_t *outlen)
{
struct workspace *workspace = list_entry(ws, struct workspace, list);
ZSTD_DStream *stream;
int err = 0;
size_t ret;
unsigned long total_out = 0;
unsigned long pg_offset = 0;
stream = ZSTD_initDStream(ZSTD_AEON_MAX_INPUT,
workspace->mem, workspace->size);
if (!stream) {
aeon_warn("ZSTD_initDStream failed\n");
err = -EIO;
goto finish;
}
destlen = max_t(size_t, destlen, PAGE_SIZE);
workspace->in_buf.src = data_in;
workspace->in_buf.pos = 0;
workspace->in_buf.size = srclen;
workspace->out_buf.dst = workspace->buf;
workspace->out_buf.pos = 0;
workspace->out_buf.size = PAGE_SIZE;
aeon_dbgv("%s: 0x%llx : %lu\n", __func__, (u64)data_in, srclen);
ret = 1;
while (pg_offset < destlen &&
workspace->in_buf.pos < workspace->in_buf.size) {
unsigned long buf_start;
unsigned long buf_offset;
unsigned long bytes;
/* Check if the frame is over and we still need more input */
if (ret == 0) {
aeon_dbg("ZSTD_decompressStream ended early\n");
err = -EIO;
goto finish;
}
ret = ZSTD_decompressStream(stream, &workspace->out_buf,
&workspace->in_buf);
if (ZSTD_isError(ret)) {
err = -EIO;
goto finish;
}
buf_start = total_out;
total_out += workspace->out_buf.pos;
workspace->out_buf.pos = 0;
if (total_out <= start_byte)
continue;
if (total_out > start_byte && buf_start < start_byte)
buf_offset = start_byte - buf_start;
else
buf_offset = 0;
bytes = min_t(unsigned long, destlen - pg_offset,
workspace->out_buf.size - buf_offset);
/* this is the point which can be changed
* when using pmem.
* */
memcpy(tmp + pg_offset,
workspace->out_buf.dst + buf_offset, bytes);
pg_offset += bytes;
*outlen = total_out;
}
finish:
aeon_dbgv("%lu %lu", pg_offset, destlen);
if (pg_offset < destlen)
memset(tmp + pg_offset, 0, destlen - pg_offset);
return err;
}
const struct aeon_compress_op aeon_zstd_compress = {
.alloc_workspace = zstd_alloc_workspace,
.compress_pages = zstd_compress_pages,
};
static int
zstd_do_compress_pages(struct list_head *ws, const void *src, size_t len,
unsigned long *out_pages, size_t *total_in,
size_t *total_out, void *tmp)
{
struct workspace *workspace = list_entry(ws, struct workspace, list);
ZSTD_CStream *stream;
int err = 0;
int nr_pages = 0;
struct page *out_page = NULL; /* The current page to write to */
size_t tot_in = 0;
size_t tot_out = 0;
const unsigned long nr_dest_pages = *out_pages;
size_t max_out = nr_dest_pages * PAGE_SIZE;
ZSTD_parameters params = zstd_get_aeon_parameters(len);
*out_pages = 0;
*total_out = 0;
*total_in = 0;
stream = ZSTD_initCStream(params, len, workspace->mem, workspace->size);
if (!stream) {
aeon_warn("ZSTD_initCStream failed\n");
err = -EIO;
goto out;
}
workspace->in_buf.src = src;
workspace->in_buf.pos = 0;
workspace->in_buf.size = max_t(size_t, len, PAGE_SIZE);
out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
if (out_page == NULL) {
err = -ENOMEM;
goto out;
}
nr_pages++;
workspace->out_buf.dst = kmap(out_page);
workspace->out_buf.pos = 0;
workspace->out_buf.size = max_t(size_t, max_out, PAGE_SIZE);
while (1) {
size_t ret;
ret = ZSTD_compressStream(stream, &workspace->out_buf,
&workspace->in_buf);
if (ZSTD_isError(ret)) {
aeon_dbg("ZSTD_compressionStream returned %d\n",
ZSTD_getErrorCode(ret));
err = -EIO;
goto out;
}
/* Check to see if we are making it bigger */
if (tot_in + workspace->in_buf.pos > 8192 &&
tot_in + workspace->in_buf.pos <
tot_out + workspace->out_buf.pos) {
err = -E2BIG;
goto out;
}
/* Check if we need more output space */
if (workspace->out_buf.pos == workspace->out_buf.size) {
tot_out += PAGE_SIZE;
max_out -= PAGE_SIZE;
// here should be replaced by another one
kunmap(out_page);
if (nr_pages == nr_dest_pages) {
out_page = NULL;
err = -E2BIG;
goto out;
}
out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
if (out_page == NULL) {
err = -ENOMEM;
goto out;
}
nr_pages++;
workspace->out_buf.dst = kmap(out_page);
workspace->out_buf.pos = 0;
workspace->out_buf.size = min_t(size_t, max_out,
PAGE_SIZE);
}
/* We've reached the end of the input */
if (workspace->in_buf.pos >= len) {
tot_in += workspace->in_buf.pos;
break;
}
/* Check if we need more input */
if (workspace->in_buf.pos == workspace->in_buf.size) {
tot_in += PAGE_SIZE;
len -= PAGE_SIZE;
// find_get_page could be replaced by something like find_extent
workspace->in_buf.pos = 0;
workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
workspace->in_buf.src = src + PAGE_SIZE;
}
}
while (1) {
size_t ret;
ret = ZSTD_endStream(stream, &workspace->out_buf);
if (ZSTD_isError(ret)) {
if (ZSTD_getErrorCode(ret) == ZSTD_error_srcSize_wrong)
return len;
aeon_warn("ZSTD_endstream returned %d\n",
ZSTD_getErrorCode(ret));
err = -EIO;
goto out;
}
if (ret == 0) {
tot_out += workspace->out_buf.pos;
break;
}
if (workspace->out_buf.pos > max_out) {
tot_out += workspace->out_buf.pos;
err = -E2BIG;
goto out;
}
tot_out += PAGE_SIZE;
max_out -= PAGE_SIZE;
kunmap(out_page);
out_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
if (out_page == NULL) {
err = -ENOMEM;
goto out;
}
nr_pages++;
workspace->out_buf.dst = kmap(out_page);
workspace->out_buf.pos = 0;
workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
}
if (tot_out >= tot_in) {
err = -E2BIG;
aeon_dbg("Can't compress data\n");
aeon_dbg("%lu <- %lu\n", tot_out, tot_in);
goto out;
}
*total_in = tot_in;
*total_out = tot_out;
memcpy(tmp, kmap(out_page), *total_out);
out:
*out_pages = nr_pages;
/* Cleanup */
if (out_page)
kunmap(out_page);
return err;
}
const struct aeon_compress_op aeon_zstd_on_pmem_compress = {
.alloc_workspace = zstd_alloc_workspace,
};
enum aeon_compression_type {
AEON_COMPRESS_ZSTD = 0,
AEON_COMPRESS_ZSTD_ON_PMEM,
AEON_COMPRESS_TYPES,
};
struct workspace_list {
struct list_head idle_ws;
spinlock_t ws_lock;
int free_ws;
atomic_t total_ws;
wait_queue_head_t ws_wait;
};
static struct workspace_list aeon_comp_ws[AEON_COMPRESS_TYPES];
static const struct aeon_compress_op * const aeon_compress_op[] = {
&aeon_zstd_compress,
&aeon_zstd_on_pmem_compress,
};
void __init aeon_init_compress(void)
{
struct list_head *workspace;
int i;
for (i = 0; i < AEON_COMPRESS_TYPES; i++) {
INIT_LIST_HEAD(&aeon_comp_ws[i].idle_ws);
spin_lock_init(&aeon_comp_ws[i].ws_lock);
atomic_set(&aeon_comp_ws[i].total_ws, 0);
init_waitqueue_head(&aeon_comp_ws[i].ws_wait);
workspace = aeon_compress_op[i]->alloc_workspace();
if (IS_ERR(workspace))
aeon_warn("cannot preallocate compression workspace\n");
else {
atomic_set(&aeon_comp_ws[i].total_ws, 1);
aeon_comp_ws[i].free_ws = 1;
list_add(workspace, &aeon_comp_ws[i].idle_ws);
}
}
}
/**
* This finds an available workspace or allocates a new one.
* If it is not possible to alllocate a new one, waits until there's one.
* Preallocation makes a forward progress guarantees and we do not return
* errors.
*/
static struct list_head *find_workspace(int type)
{
struct list_head *workspace;
unsigned nofs_flag;
int cpus = num_online_cpus();
int idx = type - 1;
struct list_head *idle_ws;
spinlock_t *ws_lock;
atomic_t *total_ws;
wait_queue_head_t *ws_wait;
int *free_ws;
idle_ws = &aeon_comp_ws[idx].idle_ws;
ws_lock = &aeon_comp_ws[idx].ws_lock;
total_ws = &aeon_comp_ws[idx].total_ws;
ws_wait = &aeon_comp_ws[idx].ws_wait;
free_ws = &aeon_comp_ws[idx].free_ws;
again:
spin_lock(ws_lock);
if (!list_empty(idle_ws)) {
workspace = idle_ws->next;
list_del(workspace);
(*free_ws)--;
spin_unlock(ws_lock);
return workspace;
}
if (atomic_read(total_ws) > cpus) {
DEFINE_WAIT(wait);
spin_unlock(ws_lock);
prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE);
if (atomic_read(total_ws) > cpus && !*free_ws)
schedule();
finish_wait(ws_wait, &wait);
goto again;
}
atomic_inc(total_ws);
spin_unlock(ws_lock);
nofs_flag = memalloc_nofs_save();
workspace = aeon_compress_op[idx]->alloc_workspace();
memalloc_nofs_restore(nofs_flag);
if (IS_ERR(workspace)) {
atomic_dec(total_ws);
wake_up(ws_wait);
if (atomic_read(total_ws) == 0) {
static DEFINE_RATELIMIT_STATE(_rs,
/* once per minute */ 60 * HZ,
/* no burst */ 1);
if (__ratelimit(&_rs))
pr_warn("BTRFS: no compression workspaces, low memory, retrying\n");
}
goto again;
}
return workspace;
}
static void free_workspace(int type, struct list_head *workspace)
{
struct list_head *idle_ws;
spinlock_t *ws_lock;
atomic_t *total_ws;
wait_queue_head_t *ws_wait;
int *free_ws;
int idx = type - 1;
idle_ws = &aeon_comp_ws[idx].idle_ws;
ws_lock = &aeon_comp_ws[idx].ws_lock;
total_ws = &aeon_comp_ws[idx].total_ws;
ws_wait = &aeon_comp_ws[idx].ws_wait;
free_ws = &aeon_comp_ws[idx].free_ws;
spin_lock(ws_lock);
if (*free_ws <= num_online_cpus()) {
list_add(workspace, idle_ws);
(*free_ws)++;
spin_unlock(ws_lock);
goto wake;
}
spin_unlock(ws_lock);
aeon_compress_op[idx]->free_workspace(workspace);
atomic_dec(total_ws);
wake:
if (wq_has_sleeper(ws_wait))
wake_up(ws_wait);
}
static void free_workspaces(void)
{
struct list_head *workspace;
int i;
for (i = 0; i < AEON_COMPRESS_TYPES; i++) {
while (!list_empty(&aeon_comp_ws[i].idle_ws)) {
workspace = aeon_comp_ws[i].idle_ws.next;
list_del(workspace);
/* TODO: Define it */
//aeon_compress_op[i]->free_workspace(workspace);
atomic_dec(&aeon_comp_ws[i].total_ws);
}
}
}
void __cold aeon_exit_compress(void)
{
free_workspaces();
}
static const char* const aeon_compress_types[] = { "zstd" };
/**
* Given an address space and start and length, compress the bytes into @pages
* that are allocated on demand.
*
* @type_level is encoded algorithm and level, where level 0 means whatever
* default the algorithm chooses and is opaque here;
* - compression algo are 0-3
* - the level are bits 4-7
*
* @out_pages is an in/out parameter, holds maximum number of pages to allocate
* and returns number of actually allocated pages
*
* @total_in is used to return the number of bytes actually read. It may be
* smaller than the input length if we had to exit early because we ran out of
* room in the pages array or because we cross the max_out threshold.
*
* @total_out is an in/out parameter, must be set to the input length and will
* be also used to return the total number of compressed bytes
*
* @max_out tells us the max number of bytes that we're allowed to
* stuff into pages
*/
int aeon_compress_pages(unsigned int type_level, struct address_space *mapping,
u64 start, struct page **pages,
unsigned long *out_pages,
unsigned long *total_in,
unsigned long *total_out)
{
struct list_head *workspace;
int ret;
int type = type_level & 0xF;
workspace = find_workspace(type);
ret = aeon_compress_op[type-1]->compress_pages(workspace, mapping,
start, pages,
out_pages,
total_in, total_out);
free_workspace(type, workspace);
return ret;
}
static int aeon_compress_pmem(void *src, struct iov_iter *i)
{
struct list_head *workspace;
size_t len = i->count;
size_t total_in = 0;
size_t total_out = 0;
unsigned long out_pages = (len >> PAGE_SHIFT) + 1;
int type = 1 & 0xF;
int err;
void *tmp;
tmp = kzalloc(sizeof(char) * i->count, GFP_KERNEL);
if (!tmp) {
AEON_ERR(-ENOMEM);
return -ENOMEM;
}
aeon_dbg("---COMPRESS START---\n");
aeon_dbg("%lu\n", len);
workspace = find_workspace(type);
err = zstd_do_compress_pages(workspace, src, len,
&out_pages, &total_in, &total_out, tmp);
free_workspace(type, workspace);
aeon_dbg("---COMPRESS FINISH---\n");
if (err) {
AEON_ERR(err);
goto out;
}
aeon_dbg("---FINISH(log start)---\n");
aeon_dbg("%lu\n", total_out);
aeon_dbg("%lu %lu %lu\n", out_pages, total_in, total_out);
i->count = total_out;
memcpy(src, tmp, i->count);
aeon_dbg("---FINISH(log end)---\n");
out:
kfree(tmp);
return err;
}
static int
aeon_decompress_pmem(void *src, size_t len, struct iov_iter *i, size_t *outlen)
{
struct list_head *workspace;
int type = 1 & 0xF;
int err = 0;
size_t destlen = ((len >> PAGE_SHIFT) + 1) * PAGE_SIZE; /*TODO*/
void *tmp;
tmp = kzalloc(sizeof(char) * destlen, GFP_KERNEL);
if (!tmp) {
AEON_ERR(-ENOMEM);
return -ENOMEM;
}
aeon_dbg("---DECOMPRESS START---\n");
workspace = find_workspace(type);
err = zstd_decompress(workspace, src, 0, len, destlen, tmp, outlen);
free_workspace(type, workspace);
if (err) {
AEON_ERR(err);
return err;
}
aeon_dbg("---DECOMPRESS FINISH---\n");
aeon_dbg("---FINISH(log start)---\n");
aeon_dbg("%s\n", (char *)tmp);
memcpy(src, tmp, destlen);
aeon_dbg("src %s\n", (char *)src);
aeon_dbg("---FINISH(log end)---\n");
kfree(tmp);
return err;
}
void *aeon_compress(const char __user *data, size_t len,
size_t *outlen, int *compressed)
{
void *ret;
void *buf;
struct list_head *workspace;
size_t total_in = 0;
size_t total_out = 0;
unsigned long out_pages = (len >> PAGE_SHIFT) + 1;
int err = -ENOMEM;
aeon_dbgv("size %lu\n", len);
ret = kzalloc(sizeof(char) * len, GFP_KERNEL);
if (!ret)
return ERR_PTR(err);
buf = kzalloc(sizeof(char) * len, GFP_KERNEL);
if (!buf)
goto out;
err = copy_from_user(buf, data, len);
if (err) {
AEON_ERR(err);
goto out1;
}
workspace = find_workspace(COMPRESSION_TYPE);
err = zstd_do_compress_pages(workspace, buf, len,
&out_pages, &total_in, &total_out, ret);
free_workspace(COMPRESSION_TYPE, workspace);
if (err == len) {
*compressed = 0;
*outlen = len;
kfree(ret);
return buf;
} else if (err) {
AEON_ERR(err);
goto out1;
}
*compressed = 1;
*outlen = total_out;
kfree(buf);
aeon_dbgv("%s: len %lu : outlen %lu\n", __func__, len, *outlen);
return ret;
out1:
kfree(buf);
out:
kfree(ret);
return ERR_PTR(err);
}
int aeon_compress_data_iter(struct inode *inode, struct iov_iter *i)
{
struct aeon_inode *pi;
void *buf;
int err = 0;
size_t tmp;
if (!i->count)
goto out;
buf = kzalloc(sizeof(char) * i->count, GFP_KERNEL);
if (!buf) {
err = -ENOMEM;
goto out1;
}
pi = aeon_get_inode(inode->i_sb, &AEON_I(inode)->header);
aeon_dbg("3!\n");
err = copy_from_user(buf, i->iov->iov_base, i->count);
if (err) {
AEON_ERR(err);
goto out1;
}
tmp = i->count;
err = aeon_compress_pmem(buf, i);
if (!err)
pi->compressed = 1;
aeon_dbg("new %lu\n", i->count);
aeon_dbg("buf %s\n", (char *)buf);
err = copy_to_user(i->iov->iov_base, buf, i->count);
if (err) {
AEON_ERR(err);
goto out1;
}
out1:
kfree(buf);
buf = NULL;
out:
return err;
}
void *aeon_decompress(const void *data, struct aeon_extent *ae,
struct aeon_inode *pi)
{
struct list_head *workspace;
void *ret;
int compressed_length = le16_to_cpu(ae->ex_original_length);
size_t o_len = le32_to_cpu(ae->ex_original_length);
size_t c_len = compressed_length<<AEON_SHIFT;
size_t outlen = 0;
int err = -ENOMEM;
aeon_dbgv("%lu :%d\n", o_len, le16_to_cpu(ae->ex_length));
ret = kzalloc(sizeof(char) * (o_len+1), GFP_KERNEL);
if (!ret)
return ERR_PTR(err);
workspace = find_workspace(COMPRESSION_TYPE);
err = zstd_decompress(workspace, data, 0, c_len, o_len, ret, &outlen);
free_workspace(COMPRESSION_TYPE, workspace);
if (err) {
AEON_ERR(err);
goto out;
}
return ret;
out:
kfree(ret);
return ERR_PTR(err);
}
ssize_t aeon_decompress_data_iter(ssize_t len, struct iov_iter *i)
{
void *buf;
size_t ret = 0;
size_t outlen = 0;
int err = 0;
buf = kzalloc(sizeof(char) *
(((len >> PAGE_SHIFT)+1) * PAGE_SIZE), GFP_KERNEL);
if (!buf) {
err = -ENOMEM;
goto out1;
}
err = copy_from_user(buf, i->iov->iov_base, len);
if (err) {
AEON_ERR(err);
ret = 0;
goto out1;
}
err = aeon_decompress_pmem(buf, len, i, &outlen);
if (err) {
AEON_ERR(err);
ret = 0;
goto out1;
}
err = copy_to_user(i->iov->iov_base, buf, outlen);
if (err) {
AEON_ERR(err);
ret = 0;
goto out1;
}
ret = outlen;
out1:
kfree(buf);
buf = NULL;
return ret;
}