1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/kernel.h>
20#include <linux/bio.h>
21#include <linux/buffer_head.h>
22#include <linux/file.h>
23#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/bit_spinlock.h>
34#include <linux/slab.h>
35#include <linux/sched/mm.h>
36#include <linux/sort.h>
37#include <linux/log2.h>
38#include "ctree.h"
39#include "disk-io.h"
40#include "transaction.h"
41#include "btrfs_inode.h"
42#include "volumes.h"
43#include "ordered-data.h"
44#include "compression.h"
45#include "extent_io.h"
46#include "extent_map.h"
47
48static int btrfs_decompress_bio(struct compressed_bio *cb);
49
50static inline int compressed_bio_size(struct btrfs_fs_info *fs_info,
51 unsigned long disk_size)
52{
53 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
54
55 return sizeof(struct compressed_bio) +
56 (DIV_ROUND_UP(disk_size, fs_info->sectorsize)) * csum_size;
57}
58
59static int check_compressed_csum(struct btrfs_inode *inode,
60 struct compressed_bio *cb,
61 u64 disk_start)
62{
63 int ret;
64 struct page *page;
65 unsigned long i;
66 char *kaddr;
67 u32 csum;
68 u32 *cb_sum = &cb->sums;
69
70 if (inode->flags & BTRFS_INODE_NODATASUM)
71 return 0;
72
73 for (i = 0; i < cb->nr_pages; i++) {
74 page = cb->compressed_pages[i];
75 csum = ~(u32)0;
76
77 kaddr = kmap_atomic(page);
78 csum = btrfs_csum_data(kaddr, csum, PAGE_SIZE);
79 btrfs_csum_final(csum, (u8 *)&csum);
80 kunmap_atomic(kaddr);
81
82 if (csum != *cb_sum) {
83 btrfs_print_data_csum_error(inode, disk_start, csum,
84 *cb_sum, cb->mirror_num);
85 ret = -EIO;
86 goto fail;
87 }
88 cb_sum++;
89
90 }
91 ret = 0;
92fail:
93 return ret;
94}
95
96
97
98
99
100
101
102
103
104
105
106static void end_compressed_bio_read(struct bio *bio)
107{
108 struct compressed_bio *cb = bio->bi_private;
109 struct inode *inode;
110 struct page *page;
111 unsigned long index;
112 unsigned int mirror = btrfs_io_bio(bio)->mirror_num;
113 int ret = 0;
114
115 if (bio->bi_status)
116 cb->errors = 1;
117
118
119
120
121 if (!refcount_dec_and_test(&cb->pending_bios))
122 goto out;
123
124
125
126
127
128 ASSERT(btrfs_io_bio(cb->orig_bio));
129 btrfs_io_bio(cb->orig_bio)->mirror_num = mirror;
130 cb->mirror_num = mirror;
131
132
133
134
135
136 if (cb->errors == 1)
137 goto csum_failed;
138
139 inode = cb->inode;
140 ret = check_compressed_csum(BTRFS_I(inode), cb,
141 (u64)bio->bi_iter.bi_sector << 9);
142 if (ret)
143 goto csum_failed;
144
145
146
147
148 ret = btrfs_decompress_bio(cb);
149
150csum_failed:
151 if (ret)
152 cb->errors = 1;
153
154
155 index = 0;
156 for (index = 0; index < cb->nr_pages; index++) {
157 page = cb->compressed_pages[index];
158 page->mapping = NULL;
159 put_page(page);
160 }
161
162
163 if (cb->errors) {
164 bio_io_error(cb->orig_bio);
165 } else {
166 int i;
167 struct bio_vec *bvec;
168
169
170
171
172
173 ASSERT(!bio_flagged(bio, BIO_CLONED));
174 bio_for_each_segment_all(bvec, cb->orig_bio, i)
175 SetPageChecked(bvec->bv_page);
176
177 bio_endio(cb->orig_bio);
178 }
179
180
181 kfree(cb->compressed_pages);
182 kfree(cb);
183out:
184 bio_put(bio);
185}
186
187
188
189
190
191static noinline void end_compressed_writeback(struct inode *inode,
192 const struct compressed_bio *cb)
193{
194 unsigned long index = cb->start >> PAGE_SHIFT;
195 unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT;
196 struct page *pages[16];
197 unsigned long nr_pages = end_index - index + 1;
198 int i;
199 int ret;
200
201 if (cb->errors)
202 mapping_set_error(inode->i_mapping, -EIO);
203
204 while (nr_pages > 0) {
205 ret = find_get_pages_contig(inode->i_mapping, index,
206 min_t(unsigned long,
207 nr_pages, ARRAY_SIZE(pages)), pages);
208 if (ret == 0) {
209 nr_pages -= 1;
210 index += 1;
211 continue;
212 }
213 for (i = 0; i < ret; i++) {
214 if (cb->errors)
215 SetPageError(pages[i]);
216 end_page_writeback(pages[i]);
217 put_page(pages[i]);
218 }
219 nr_pages -= ret;
220 index += ret;
221 }
222
223}
224
225
226
227
228
229
230
231
232
233static void end_compressed_bio_write(struct bio *bio)
234{
235 struct extent_io_tree *tree;
236 struct compressed_bio *cb = bio->bi_private;
237 struct inode *inode;
238 struct page *page;
239 unsigned long index;
240
241 if (bio->bi_status)
242 cb->errors = 1;
243
244
245
246
247 if (!refcount_dec_and_test(&cb->pending_bios))
248 goto out;
249
250
251
252
253 inode = cb->inode;
254 tree = &BTRFS_I(inode)->io_tree;
255 cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
256 tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
257 cb->start,
258 cb->start + cb->len - 1,
259 NULL,
260 bio->bi_status ?
261 BLK_STS_OK : BLK_STS_NOTSUPP);
262 cb->compressed_pages[0]->mapping = NULL;
263
264 end_compressed_writeback(inode, cb);
265
266
267
268
269
270
271 index = 0;
272 for (index = 0; index < cb->nr_pages; index++) {
273 page = cb->compressed_pages[index];
274 page->mapping = NULL;
275 put_page(page);
276 }
277
278
279 kfree(cb->compressed_pages);
280 kfree(cb);
281out:
282 bio_put(bio);
283}
284
285
286
287
288
289
290
291
292
293
294blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start,
295 unsigned long len, u64 disk_start,
296 unsigned long compressed_len,
297 struct page **compressed_pages,
298 unsigned long nr_pages,
299 unsigned int write_flags)
300{
301 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
302 struct bio *bio = NULL;
303 struct compressed_bio *cb;
304 unsigned long bytes_left;
305 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
306 int pg_index = 0;
307 struct page *page;
308 u64 first_byte = disk_start;
309 struct block_device *bdev;
310 blk_status_t ret;
311 int skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
312
313 WARN_ON(start & ((u64)PAGE_SIZE - 1));
314 cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS);
315 if (!cb)
316 return BLK_STS_RESOURCE;
317 refcount_set(&cb->pending_bios, 0);
318 cb->errors = 0;
319 cb->inode = inode;
320 cb->start = start;
321 cb->len = len;
322 cb->mirror_num = 0;
323 cb->compressed_pages = compressed_pages;
324 cb->compressed_len = compressed_len;
325 cb->orig_bio = NULL;
326 cb->nr_pages = nr_pages;
327
328 bdev = fs_info->fs_devices->latest_bdev;
329
330 bio = btrfs_bio_alloc(bdev, first_byte);
331 bio->bi_opf = REQ_OP_WRITE | write_flags;
332 bio->bi_private = cb;
333 bio->bi_end_io = end_compressed_bio_write;
334 refcount_set(&cb->pending_bios, 1);
335
336
337 bytes_left = compressed_len;
338 for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) {
339 int submit = 0;
340
341 page = compressed_pages[pg_index];
342 page->mapping = inode->i_mapping;
343 if (bio->bi_iter.bi_size)
344 submit = io_tree->ops->merge_bio_hook(page, 0,
345 PAGE_SIZE,
346 bio, 0);
347
348 page->mapping = NULL;
349 if (submit || bio_add_page(bio, page, PAGE_SIZE, 0) <
350 PAGE_SIZE) {
351 bio_get(bio);
352
353
354
355
356
357
358
359 refcount_inc(&cb->pending_bios);
360 ret = btrfs_bio_wq_end_io(fs_info, bio,
361 BTRFS_WQ_ENDIO_DATA);
362 BUG_ON(ret);
363
364 if (!skip_sum) {
365 ret = btrfs_csum_one_bio(inode, bio, start, 1);
366 BUG_ON(ret);
367 }
368
369 ret = btrfs_map_bio(fs_info, bio, 0, 1);
370 if (ret) {
371 bio->bi_status = ret;
372 bio_endio(bio);
373 }
374
375 bio_put(bio);
376
377 bio = btrfs_bio_alloc(bdev, first_byte);
378 bio->bi_opf = REQ_OP_WRITE | write_flags;
379 bio->bi_private = cb;
380 bio->bi_end_io = end_compressed_bio_write;
381 bio_add_page(bio, page, PAGE_SIZE, 0);
382 }
383 if (bytes_left < PAGE_SIZE) {
384 btrfs_info(fs_info,
385 "bytes left %lu compress len %lu nr %lu",
386 bytes_left, cb->compressed_len, cb->nr_pages);
387 }
388 bytes_left -= PAGE_SIZE;
389 first_byte += PAGE_SIZE;
390 cond_resched();
391 }
392 bio_get(bio);
393
394 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
395 BUG_ON(ret);
396
397 if (!skip_sum) {
398 ret = btrfs_csum_one_bio(inode, bio, start, 1);
399 BUG_ON(ret);
400 }
401
402 ret = btrfs_map_bio(fs_info, bio, 0, 1);
403 if (ret) {
404 bio->bi_status = ret;
405 bio_endio(bio);
406 }
407
408 bio_put(bio);
409 return 0;
410}
411
412static u64 bio_end_offset(struct bio *bio)
413{
414 struct bio_vec *last = &bio->bi_io_vec[bio->bi_vcnt - 1];
415
416 return page_offset(last->bv_page) + last->bv_len + last->bv_offset;
417}
418
419static noinline int add_ra_bio_pages(struct inode *inode,
420 u64 compressed_end,
421 struct compressed_bio *cb)
422{
423 unsigned long end_index;
424 unsigned long pg_index;
425 u64 last_offset;
426 u64 isize = i_size_read(inode);
427 int ret;
428 struct page *page;
429 unsigned long nr_pages = 0;
430 struct extent_map *em;
431 struct address_space *mapping = inode->i_mapping;
432 struct extent_map_tree *em_tree;
433 struct extent_io_tree *tree;
434 u64 end;
435 int misses = 0;
436
437 last_offset = bio_end_offset(cb->orig_bio);
438 em_tree = &BTRFS_I(inode)->extent_tree;
439 tree = &BTRFS_I(inode)->io_tree;
440
441 if (isize == 0)
442 return 0;
443
444 end_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
445
446 while (last_offset < compressed_end) {
447 pg_index = last_offset >> PAGE_SHIFT;
448
449 if (pg_index > end_index)
450 break;
451
452 rcu_read_lock();
453 page = radix_tree_lookup(&mapping->page_tree, pg_index);
454 rcu_read_unlock();
455 if (page && !radix_tree_exceptional_entry(page)) {
456 misses++;
457 if (misses > 4)
458 break;
459 goto next;
460 }
461
462 page = __page_cache_alloc(mapping_gfp_constraint(mapping,
463 ~__GFP_FS));
464 if (!page)
465 break;
466
467 if (add_to_page_cache_lru(page, mapping, pg_index, GFP_NOFS)) {
468 put_page(page);
469 goto next;
470 }
471
472 end = last_offset + PAGE_SIZE - 1;
473
474
475
476
477
478 set_page_extent_mapped(page);
479 lock_extent(tree, last_offset, end);
480 read_lock(&em_tree->lock);
481 em = lookup_extent_mapping(em_tree, last_offset,
482 PAGE_SIZE);
483 read_unlock(&em_tree->lock);
484
485 if (!em || last_offset < em->start ||
486 (last_offset + PAGE_SIZE > extent_map_end(em)) ||
487 (em->block_start >> 9) != cb->orig_bio->bi_iter.bi_sector) {
488 free_extent_map(em);
489 unlock_extent(tree, last_offset, end);
490 unlock_page(page);
491 put_page(page);
492 break;
493 }
494 free_extent_map(em);
495
496 if (page->index == end_index) {
497 char *userpage;
498 size_t zero_offset = isize & (PAGE_SIZE - 1);
499
500 if (zero_offset) {
501 int zeros;
502 zeros = PAGE_SIZE - zero_offset;
503 userpage = kmap_atomic(page);
504 memset(userpage + zero_offset, 0, zeros);
505 flush_dcache_page(page);
506 kunmap_atomic(userpage);
507 }
508 }
509
510 ret = bio_add_page(cb->orig_bio, page,
511 PAGE_SIZE, 0);
512
513 if (ret == PAGE_SIZE) {
514 nr_pages++;
515 put_page(page);
516 } else {
517 unlock_extent(tree, last_offset, end);
518 unlock_page(page);
519 put_page(page);
520 break;
521 }
522next:
523 last_offset += PAGE_SIZE;
524 }
525 return 0;
526}
527
528
529
530
531
532
533
534
535
536
537
538
539blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
540 int mirror_num, unsigned long bio_flags)
541{
542 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
543 struct extent_io_tree *tree;
544 struct extent_map_tree *em_tree;
545 struct compressed_bio *cb;
546 unsigned long compressed_len;
547 unsigned long nr_pages;
548 unsigned long pg_index;
549 struct page *page;
550 struct block_device *bdev;
551 struct bio *comp_bio;
552 u64 cur_disk_byte = (u64)bio->bi_iter.bi_sector << 9;
553 u64 em_len;
554 u64 em_start;
555 struct extent_map *em;
556 blk_status_t ret = BLK_STS_RESOURCE;
557 int faili = 0;
558 u32 *sums;
559
560 tree = &BTRFS_I(inode)->io_tree;
561 em_tree = &BTRFS_I(inode)->extent_tree;
562
563
564 read_lock(&em_tree->lock);
565 em = lookup_extent_mapping(em_tree,
566 page_offset(bio->bi_io_vec->bv_page),
567 PAGE_SIZE);
568 read_unlock(&em_tree->lock);
569 if (!em)
570 return BLK_STS_IOERR;
571
572 compressed_len = em->block_len;
573 cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS);
574 if (!cb)
575 goto out;
576
577 refcount_set(&cb->pending_bios, 0);
578 cb->errors = 0;
579 cb->inode = inode;
580 cb->mirror_num = mirror_num;
581 sums = &cb->sums;
582
583 cb->start = em->orig_start;
584 em_len = em->len;
585 em_start = em->start;
586
587 free_extent_map(em);
588 em = NULL;
589
590 cb->len = bio->bi_iter.bi_size;
591 cb->compressed_len = compressed_len;
592 cb->compress_type = extent_compress_type(bio_flags);
593 cb->orig_bio = bio;
594
595 nr_pages = DIV_ROUND_UP(compressed_len, PAGE_SIZE);
596 cb->compressed_pages = kcalloc(nr_pages, sizeof(struct page *),
597 GFP_NOFS);
598 if (!cb->compressed_pages)
599 goto fail1;
600
601 bdev = fs_info->fs_devices->latest_bdev;
602
603 for (pg_index = 0; pg_index < nr_pages; pg_index++) {
604 cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
605 __GFP_HIGHMEM);
606 if (!cb->compressed_pages[pg_index]) {
607 faili = pg_index - 1;
608 ret = BLK_STS_RESOURCE;
609 goto fail2;
610 }
611 }
612 faili = nr_pages - 1;
613 cb->nr_pages = nr_pages;
614
615 add_ra_bio_pages(inode, em_start + em_len, cb);
616
617
618 cb->len = bio->bi_iter.bi_size;
619
620 comp_bio = btrfs_bio_alloc(bdev, cur_disk_byte);
621 bio_set_op_attrs (comp_bio, REQ_OP_READ, 0);
622 comp_bio->bi_private = cb;
623 comp_bio->bi_end_io = end_compressed_bio_read;
624 refcount_set(&cb->pending_bios, 1);
625
626 for (pg_index = 0; pg_index < nr_pages; pg_index++) {
627 int submit = 0;
628
629 page = cb->compressed_pages[pg_index];
630 page->mapping = inode->i_mapping;
631 page->index = em_start >> PAGE_SHIFT;
632
633 if (comp_bio->bi_iter.bi_size)
634 submit = tree->ops->merge_bio_hook(page, 0,
635 PAGE_SIZE,
636 comp_bio, 0);
637
638 page->mapping = NULL;
639 if (submit || bio_add_page(comp_bio, page, PAGE_SIZE, 0) <
640 PAGE_SIZE) {
641 bio_get(comp_bio);
642
643 ret = btrfs_bio_wq_end_io(fs_info, comp_bio,
644 BTRFS_WQ_ENDIO_DATA);
645 BUG_ON(ret);
646
647
648
649
650
651
652
653 refcount_inc(&cb->pending_bios);
654
655 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
656 ret = btrfs_lookup_bio_sums(inode, comp_bio,
657 sums);
658 BUG_ON(ret);
659 }
660 sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size,
661 fs_info->sectorsize);
662
663 ret = btrfs_map_bio(fs_info, comp_bio, mirror_num, 0);
664 if (ret) {
665 comp_bio->bi_status = ret;
666 bio_endio(comp_bio);
667 }
668
669 bio_put(comp_bio);
670
671 comp_bio = btrfs_bio_alloc(bdev, cur_disk_byte);
672 bio_set_op_attrs(comp_bio, REQ_OP_READ, 0);
673 comp_bio->bi_private = cb;
674 comp_bio->bi_end_io = end_compressed_bio_read;
675
676 bio_add_page(comp_bio, page, PAGE_SIZE, 0);
677 }
678 cur_disk_byte += PAGE_SIZE;
679 }
680 bio_get(comp_bio);
681
682 ret = btrfs_bio_wq_end_io(fs_info, comp_bio, BTRFS_WQ_ENDIO_DATA);
683 BUG_ON(ret);
684
685 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
686 ret = btrfs_lookup_bio_sums(inode, comp_bio, sums);
687 BUG_ON(ret);
688 }
689
690 ret = btrfs_map_bio(fs_info, comp_bio, mirror_num, 0);
691 if (ret) {
692 comp_bio->bi_status = ret;
693 bio_endio(comp_bio);
694 }
695
696 bio_put(comp_bio);
697 return 0;
698
699fail2:
700 while (faili >= 0) {
701 __free_page(cb->compressed_pages[faili]);
702 faili--;
703 }
704
705 kfree(cb->compressed_pages);
706fail1:
707 kfree(cb);
708out:
709 free_extent_map(em);
710 return ret;
711}
712
713
714
715
716
717
718
719
720#define SAMPLING_READ_SIZE (16)
721#define SAMPLING_INTERVAL (256)
722
723
724
725
726
727
728#define BUCKET_SIZE (256)
729
730
731
732
733
734
735
736
737
738
739
740
741
742#define MAX_SAMPLE_SIZE (BTRFS_MAX_UNCOMPRESSED * \
743 SAMPLING_READ_SIZE / SAMPLING_INTERVAL)
744
745struct bucket_item {
746 u32 count;
747};
748
749struct heuristic_ws {
750
751 u8 *sample;
752 u32 sample_size;
753
754 struct bucket_item *bucket;
755 struct list_head list;
756};
757
758static void free_heuristic_ws(struct list_head *ws)
759{
760 struct heuristic_ws *workspace;
761
762 workspace = list_entry(ws, struct heuristic_ws, list);
763
764 kvfree(workspace->sample);
765 kfree(workspace->bucket);
766 kfree(workspace);
767}
768
769static struct list_head *alloc_heuristic_ws(void)
770{
771 struct heuristic_ws *ws;
772
773 ws = kzalloc(sizeof(*ws), GFP_KERNEL);
774 if (!ws)
775 return ERR_PTR(-ENOMEM);
776
777 ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL);
778 if (!ws->sample)
779 goto fail;
780
781 ws->bucket = kcalloc(BUCKET_SIZE, sizeof(*ws->bucket), GFP_KERNEL);
782 if (!ws->bucket)
783 goto fail;
784
785 INIT_LIST_HEAD(&ws->list);
786 return &ws->list;
787fail:
788 free_heuristic_ws(&ws->list);
789 return ERR_PTR(-ENOMEM);
790}
791
792struct workspaces_list {
793 struct list_head idle_ws;
794 spinlock_t ws_lock;
795
796 int free_ws;
797
798 atomic_t total_ws;
799
800 wait_queue_head_t ws_wait;
801};
802
803static struct workspaces_list btrfs_comp_ws[BTRFS_COMPRESS_TYPES];
804
805static struct workspaces_list btrfs_heuristic_ws;
806
807static const struct btrfs_compress_op * const btrfs_compress_op[] = {
808 &btrfs_zlib_compress,
809 &btrfs_lzo_compress,
810 &btrfs_zstd_compress,
811};
812
813void __init btrfs_init_compress(void)
814{
815 struct list_head *workspace;
816 int i;
817
818 INIT_LIST_HEAD(&btrfs_heuristic_ws.idle_ws);
819 spin_lock_init(&btrfs_heuristic_ws.ws_lock);
820 atomic_set(&btrfs_heuristic_ws.total_ws, 0);
821 init_waitqueue_head(&btrfs_heuristic_ws.ws_wait);
822
823 workspace = alloc_heuristic_ws();
824 if (IS_ERR(workspace)) {
825 pr_warn(
826 "BTRFS: cannot preallocate heuristic workspace, will try later\n");
827 } else {
828 atomic_set(&btrfs_heuristic_ws.total_ws, 1);
829 btrfs_heuristic_ws.free_ws = 1;
830 list_add(workspace, &btrfs_heuristic_ws.idle_ws);
831 }
832
833 for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
834 INIT_LIST_HEAD(&btrfs_comp_ws[i].idle_ws);
835 spin_lock_init(&btrfs_comp_ws[i].ws_lock);
836 atomic_set(&btrfs_comp_ws[i].total_ws, 0);
837 init_waitqueue_head(&btrfs_comp_ws[i].ws_wait);
838
839
840
841
842
843 workspace = btrfs_compress_op[i]->alloc_workspace();
844 if (IS_ERR(workspace)) {
845 pr_warn("BTRFS: cannot preallocate compression workspace, will try later\n");
846 } else {
847 atomic_set(&btrfs_comp_ws[i].total_ws, 1);
848 btrfs_comp_ws[i].free_ws = 1;
849 list_add(workspace, &btrfs_comp_ws[i].idle_ws);
850 }
851 }
852}
853
854
855
856
857
858
859
860static struct list_head *__find_workspace(int type, bool heuristic)
861{
862 struct list_head *workspace;
863 int cpus = num_online_cpus();
864 int idx = type - 1;
865 unsigned nofs_flag;
866 struct list_head *idle_ws;
867 spinlock_t *ws_lock;
868 atomic_t *total_ws;
869 wait_queue_head_t *ws_wait;
870 int *free_ws;
871
872 if (heuristic) {
873 idle_ws = &btrfs_heuristic_ws.idle_ws;
874 ws_lock = &btrfs_heuristic_ws.ws_lock;
875 total_ws = &btrfs_heuristic_ws.total_ws;
876 ws_wait = &btrfs_heuristic_ws.ws_wait;
877 free_ws = &btrfs_heuristic_ws.free_ws;
878 } else {
879 idle_ws = &btrfs_comp_ws[idx].idle_ws;
880 ws_lock = &btrfs_comp_ws[idx].ws_lock;
881 total_ws = &btrfs_comp_ws[idx].total_ws;
882 ws_wait = &btrfs_comp_ws[idx].ws_wait;
883 free_ws = &btrfs_comp_ws[idx].free_ws;
884 }
885
886again:
887 spin_lock(ws_lock);
888 if (!list_empty(idle_ws)) {
889 workspace = idle_ws->next;
890 list_del(workspace);
891 (*free_ws)--;
892 spin_unlock(ws_lock);
893 return workspace;
894
895 }
896 if (atomic_read(total_ws) > cpus) {
897 DEFINE_WAIT(wait);
898
899 spin_unlock(ws_lock);
900 prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE);
901 if (atomic_read(total_ws) > cpus && !*free_ws)
902 schedule();
903 finish_wait(ws_wait, &wait);
904 goto again;
905 }
906 atomic_inc(total_ws);
907 spin_unlock(ws_lock);
908
909
910
911
912
913
914 nofs_flag = memalloc_nofs_save();
915 if (heuristic)
916 workspace = alloc_heuristic_ws();
917 else
918 workspace = btrfs_compress_op[idx]->alloc_workspace();
919 memalloc_nofs_restore(nofs_flag);
920
921 if (IS_ERR(workspace)) {
922 atomic_dec(total_ws);
923 wake_up(ws_wait);
924
925
926
927
928
929
930
931
932
933
934
935 if (atomic_read(total_ws) == 0) {
936 static DEFINE_RATELIMIT_STATE(_rs,
937 60 * HZ,
938 1);
939
940 if (__ratelimit(&_rs)) {
941 pr_warn("BTRFS: no compression workspaces, low memory, retrying\n");
942 }
943 }
944 goto again;
945 }
946 return workspace;
947}
948
949static struct list_head *find_workspace(int type)
950{
951 return __find_workspace(type, false);
952}
953
954
955
956
957
958static void __free_workspace(int type, struct list_head *workspace,
959 bool heuristic)
960{
961 int idx = type - 1;
962 struct list_head *idle_ws;
963 spinlock_t *ws_lock;
964 atomic_t *total_ws;
965 wait_queue_head_t *ws_wait;
966 int *free_ws;
967
968 if (heuristic) {
969 idle_ws = &btrfs_heuristic_ws.idle_ws;
970 ws_lock = &btrfs_heuristic_ws.ws_lock;
971 total_ws = &btrfs_heuristic_ws.total_ws;
972 ws_wait = &btrfs_heuristic_ws.ws_wait;
973 free_ws = &btrfs_heuristic_ws.free_ws;
974 } else {
975 idle_ws = &btrfs_comp_ws[idx].idle_ws;
976 ws_lock = &btrfs_comp_ws[idx].ws_lock;
977 total_ws = &btrfs_comp_ws[idx].total_ws;
978 ws_wait = &btrfs_comp_ws[idx].ws_wait;
979 free_ws = &btrfs_comp_ws[idx].free_ws;
980 }
981
982 spin_lock(ws_lock);
983 if (*free_ws <= num_online_cpus()) {
984 list_add(workspace, idle_ws);
985 (*free_ws)++;
986 spin_unlock(ws_lock);
987 goto wake;
988 }
989 spin_unlock(ws_lock);
990
991 if (heuristic)
992 free_heuristic_ws(workspace);
993 else
994 btrfs_compress_op[idx]->free_workspace(workspace);
995 atomic_dec(total_ws);
996wake:
997
998
999
1000 smp_mb();
1001 if (waitqueue_active(ws_wait))
1002 wake_up(ws_wait);
1003}
1004
1005static void free_workspace(int type, struct list_head *ws)
1006{
1007 return __free_workspace(type, ws, false);
1008}
1009
1010
1011
1012
1013static void free_workspaces(void)
1014{
1015 struct list_head *workspace;
1016 int i;
1017
1018 while (!list_empty(&btrfs_heuristic_ws.idle_ws)) {
1019 workspace = btrfs_heuristic_ws.idle_ws.next;
1020 list_del(workspace);
1021 free_heuristic_ws(workspace);
1022 atomic_dec(&btrfs_heuristic_ws.total_ws);
1023 }
1024
1025 for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
1026 while (!list_empty(&btrfs_comp_ws[i].idle_ws)) {
1027 workspace = btrfs_comp_ws[i].idle_ws.next;
1028 list_del(workspace);
1029 btrfs_compress_op[i]->free_workspace(workspace);
1030 atomic_dec(&btrfs_comp_ws[i].total_ws);
1031 }
1032 }
1033}
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
1059 u64 start, struct page **pages,
1060 unsigned long *out_pages,
1061 unsigned long *total_in,
1062 unsigned long *total_out)
1063{
1064 struct list_head *workspace;
1065 int ret;
1066 int type = type_level & 0xF;
1067
1068 workspace = find_workspace(type);
1069
1070 btrfs_compress_op[type - 1]->set_level(workspace, type_level);
1071 ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
1072 start, pages,
1073 out_pages,
1074 total_in, total_out);
1075 free_workspace(type, workspace);
1076 return ret;
1077}
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093static int btrfs_decompress_bio(struct compressed_bio *cb)
1094{
1095 struct list_head *workspace;
1096 int ret;
1097 int type = cb->compress_type;
1098
1099 workspace = find_workspace(type);
1100 ret = btrfs_compress_op[type - 1]->decompress_bio(workspace, cb);
1101 free_workspace(type, workspace);
1102
1103 return ret;
1104}
1105
1106
1107
1108
1109
1110
1111int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
1112 unsigned long start_byte, size_t srclen, size_t destlen)
1113{
1114 struct list_head *workspace;
1115 int ret;
1116
1117 workspace = find_workspace(type);
1118
1119 ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
1120 dest_page, start_byte,
1121 srclen, destlen);
1122
1123 free_workspace(type, workspace);
1124 return ret;
1125}
1126
1127void btrfs_exit_compress(void)
1128{
1129 free_workspaces();
1130}
1131
1132
1133
1134
1135
1136
1137
1138
1139int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
1140 unsigned long total_out, u64 disk_start,
1141 struct bio *bio)
1142{
1143 unsigned long buf_offset;
1144 unsigned long current_buf_start;
1145 unsigned long start_byte;
1146 unsigned long prev_start_byte;
1147 unsigned long working_bytes = total_out - buf_start;
1148 unsigned long bytes;
1149 char *kaddr;
1150 struct bio_vec bvec = bio_iter_iovec(bio, bio->bi_iter);
1151
1152
1153
1154
1155
1156 start_byte = page_offset(bvec.bv_page) - disk_start;
1157
1158
1159 if (total_out <= start_byte)
1160 return 1;
1161
1162
1163
1164
1165
1166 if (total_out > start_byte && buf_start < start_byte) {
1167 buf_offset = start_byte - buf_start;
1168 working_bytes -= buf_offset;
1169 } else {
1170 buf_offset = 0;
1171 }
1172 current_buf_start = buf_start;
1173
1174
1175 while (working_bytes > 0) {
1176 bytes = min_t(unsigned long, bvec.bv_len,
1177 PAGE_SIZE - buf_offset);
1178 bytes = min(bytes, working_bytes);
1179
1180 kaddr = kmap_atomic(bvec.bv_page);
1181 memcpy(kaddr + bvec.bv_offset, buf + buf_offset, bytes);
1182 kunmap_atomic(kaddr);
1183 flush_dcache_page(bvec.bv_page);
1184
1185 buf_offset += bytes;
1186 working_bytes -= bytes;
1187 current_buf_start += bytes;
1188
1189
1190 bio_advance(bio, bytes);
1191 if (!bio->bi_iter.bi_size)
1192 return 0;
1193 bvec = bio_iter_iovec(bio, bio->bi_iter);
1194 prev_start_byte = start_byte;
1195 start_byte = page_offset(bvec.bv_page) - disk_start;
1196
1197
1198
1199
1200
1201
1202
1203 if (start_byte != prev_start_byte) {
1204
1205
1206
1207
1208 if (total_out <= start_byte)
1209 return 1;
1210
1211
1212
1213
1214
1215
1216 if (total_out > start_byte &&
1217 current_buf_start < start_byte) {
1218 buf_offset = start_byte - buf_start;
1219 working_bytes = total_out - start_byte;
1220 current_buf_start = buf_start + buf_offset;
1221 }
1222 }
1223 }
1224
1225 return 1;
1226}
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245#define ENTROPY_LVL_ACEPTABLE (65)
1246#define ENTROPY_LVL_HIGH (80)
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258static inline u32 ilog2_w(u64 n)
1259{
1260 return ilog2(n * n * n * n);
1261}
1262
1263static u32 shannon_entropy(struct heuristic_ws *ws)
1264{
1265 const u32 entropy_max = 8 * ilog2_w(2);
1266 u32 entropy_sum = 0;
1267 u32 p, p_base, sz_base;
1268 u32 i;
1269
1270 sz_base = ilog2_w(ws->sample_size);
1271 for (i = 0; i < BUCKET_SIZE && ws->bucket[i].count > 0; i++) {
1272 p = ws->bucket[i].count;
1273 p_base = ilog2_w(p);
1274 entropy_sum += p * (sz_base - p_base);
1275 }
1276
1277 entropy_sum /= ws->sample_size;
1278 return entropy_sum * 100 / entropy_max;
1279}
1280
1281
1282static int bucket_comp_rev(const void *lv, const void *rv)
1283{
1284 const struct bucket_item *l = (const struct bucket_item *)lv;
1285 const struct bucket_item *r = (const struct bucket_item *)rv;
1286
1287 return r->count - l->count;
1288}
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306#define BYTE_CORE_SET_LOW (64)
1307#define BYTE_CORE_SET_HIGH (200)
1308
1309static int byte_core_set_size(struct heuristic_ws *ws)
1310{
1311 u32 i;
1312 u32 coreset_sum = 0;
1313 const u32 core_set_threshold = ws->sample_size * 90 / 100;
1314 struct bucket_item *bucket = ws->bucket;
1315
1316
1317 sort(bucket, BUCKET_SIZE, sizeof(*bucket), &bucket_comp_rev, NULL);
1318
1319 for (i = 0; i < BYTE_CORE_SET_LOW; i++)
1320 coreset_sum += bucket[i].count;
1321
1322 if (coreset_sum > core_set_threshold)
1323 return i;
1324
1325 for (; i < BYTE_CORE_SET_HIGH && bucket[i].count > 0; i++) {
1326 coreset_sum += bucket[i].count;
1327 if (coreset_sum > core_set_threshold)
1328 break;
1329 }
1330
1331 return i;
1332}
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345#define BYTE_SET_THRESHOLD (64)
1346
1347static u32 byte_set_size(const struct heuristic_ws *ws)
1348{
1349 u32 i;
1350 u32 byte_set_size = 0;
1351
1352 for (i = 0; i < BYTE_SET_THRESHOLD; i++) {
1353 if (ws->bucket[i].count > 0)
1354 byte_set_size++;
1355 }
1356
1357
1358
1359
1360
1361
1362 for (; i < BUCKET_SIZE; i++) {
1363 if (ws->bucket[i].count > 0) {
1364 byte_set_size++;
1365 if (byte_set_size > BYTE_SET_THRESHOLD)
1366 return byte_set_size;
1367 }
1368 }
1369
1370 return byte_set_size;
1371}
1372
1373static bool sample_repeated_patterns(struct heuristic_ws *ws)
1374{
1375 const u32 half_of_sample = ws->sample_size / 2;
1376 const u8 *data = ws->sample;
1377
1378 return memcmp(&data[0], &data[half_of_sample], half_of_sample) == 0;
1379}
1380
1381static void heuristic_collect_sample(struct inode *inode, u64 start, u64 end,
1382 struct heuristic_ws *ws)
1383{
1384 struct page *page;
1385 u64 index, index_end;
1386 u32 i, curr_sample_pos;
1387 u8 *in_data;
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398 if (end - start > BTRFS_MAX_UNCOMPRESSED)
1399 end = start + BTRFS_MAX_UNCOMPRESSED;
1400
1401 index = start >> PAGE_SHIFT;
1402 index_end = end >> PAGE_SHIFT;
1403
1404
1405 if (!IS_ALIGNED(end, PAGE_SIZE))
1406 index_end++;
1407
1408 curr_sample_pos = 0;
1409 while (index < index_end) {
1410 page = find_get_page(inode->i_mapping, index);
1411 in_data = kmap(page);
1412
1413 i = start % PAGE_SIZE;
1414 while (i < PAGE_SIZE - SAMPLING_READ_SIZE) {
1415
1416 if (start > end - SAMPLING_READ_SIZE)
1417 break;
1418 memcpy(&ws->sample[curr_sample_pos], &in_data[i],
1419 SAMPLING_READ_SIZE);
1420 i += SAMPLING_INTERVAL;
1421 start += SAMPLING_INTERVAL;
1422 curr_sample_pos += SAMPLING_READ_SIZE;
1423 }
1424 kunmap(page);
1425 put_page(page);
1426
1427 index++;
1428 }
1429
1430 ws->sample_size = curr_sample_pos;
1431}
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end)
1449{
1450 struct list_head *ws_list = __find_workspace(0, true);
1451 struct heuristic_ws *ws;
1452 u32 i;
1453 u8 byte;
1454 int ret = 0;
1455
1456 ws = list_entry(ws_list, struct heuristic_ws, list);
1457
1458 heuristic_collect_sample(inode, start, end, ws);
1459
1460 if (sample_repeated_patterns(ws)) {
1461 ret = 1;
1462 goto out;
1463 }
1464
1465 memset(ws->bucket, 0, sizeof(*ws->bucket)*BUCKET_SIZE);
1466
1467 for (i = 0; i < ws->sample_size; i++) {
1468 byte = ws->sample[i];
1469 ws->bucket[byte].count++;
1470 }
1471
1472 i = byte_set_size(ws);
1473 if (i < BYTE_SET_THRESHOLD) {
1474 ret = 2;
1475 goto out;
1476 }
1477
1478 i = byte_core_set_size(ws);
1479 if (i <= BYTE_CORE_SET_LOW) {
1480 ret = 3;
1481 goto out;
1482 }
1483
1484 if (i >= BYTE_CORE_SET_HIGH) {
1485 ret = 0;
1486 goto out;
1487 }
1488
1489 i = shannon_entropy(ws);
1490 if (i <= ENTROPY_LVL_ACEPTABLE) {
1491 ret = 4;
1492 goto out;
1493 }
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510 if (i < ENTROPY_LVL_HIGH) {
1511 ret = 5;
1512 goto out;
1513 } else {
1514 ret = 0;
1515 goto out;
1516 }
1517
1518out:
1519 __free_workspace(0, ws_list, true);
1520 return ret;
1521}
1522
1523unsigned int btrfs_compress_str2level(const char *str)
1524{
1525 if (strncmp(str, "zlib", 4) != 0)
1526 return 0;
1527
1528
1529 if (str[4] == ':' && '1' <= str[5] && str[5] <= '9' && str[6] == 0)
1530 return str[5] - '0';
1531
1532 return BTRFS_ZLIB_DEFAULT_LEVEL;
1533}
1534