Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (C) 2008 Oracle. All rights reserved.
4 : */
5 :
6 : #include <linux/kernel.h>
7 : #include <linux/bio.h>
8 : #include <linux/file.h>
9 : #include <linux/fs.h>
10 : #include <linux/pagemap.h>
11 : #include <linux/pagevec.h>
12 : #include <linux/highmem.h>
13 : #include <linux/kthread.h>
14 : #include <linux/time.h>
15 : #include <linux/init.h>
16 : #include <linux/string.h>
17 : #include <linux/backing-dev.h>
18 : #include <linux/writeback.h>
19 : #include <linux/psi.h>
20 : #include <linux/slab.h>
21 : #include <linux/sched/mm.h>
22 : #include <linux/log2.h>
23 : #include <crypto/hash.h>
24 : #include "misc.h"
25 : #include "ctree.h"
26 : #include "fs.h"
27 : #include "disk-io.h"
28 : #include "transaction.h"
29 : #include "btrfs_inode.h"
30 : #include "bio.h"
31 : #include "ordered-data.h"
32 : #include "compression.h"
33 : #include "extent_io.h"
34 : #include "extent_map.h"
35 : #include "subpage.h"
36 : #include "zoned.h"
37 : #include "file-item.h"
38 : #include "super.h"
39 :
40 : static struct bio_set btrfs_compressed_bioset;
41 :
42 : static const char* const btrfs_compress_types[] = { "", "zlib", "lzo", "zstd" };
43 :
44 359 : const char* btrfs_compress_type2str(enum btrfs_compression_type type)
45 : {
46 359 : switch (type) {
47 359 : case BTRFS_COMPRESS_ZLIB:
48 : case BTRFS_COMPRESS_LZO:
49 : case BTRFS_COMPRESS_ZSTD:
50 : case BTRFS_COMPRESS_NONE:
51 359 : return btrfs_compress_types[type];
52 : default:
53 : break;
54 : }
55 :
56 : return NULL;
57 : }
58 :
59 : static inline struct compressed_bio *to_compressed_bio(struct btrfs_bio *bbio)
60 : {
61 160595 : return container_of(bbio, struct compressed_bio, bbio);
62 : }
63 :
64 159439 : static struct compressed_bio *alloc_compressed_bio(struct btrfs_inode *inode,
65 : u64 start, blk_opf_t op,
66 : btrfs_bio_end_io_t end_io)
67 : {
68 159439 : struct btrfs_bio *bbio;
69 :
70 159439 : bbio = btrfs_bio(bio_alloc_bioset(NULL, BTRFS_MAX_COMPRESSED_PAGES, op,
71 : GFP_NOFS, &btrfs_compressed_bioset));
72 159439 : btrfs_bio_init(bbio, inode->root->fs_info, end_io, NULL);
73 159439 : bbio->inode = inode;
74 159439 : bbio->file_offset = start;
75 159439 : return to_compressed_bio(bbio);
76 : }
77 :
78 42 : bool btrfs_compress_is_valid_type(const char *str, size_t len)
79 : {
80 42 : int i;
81 :
82 102 : for (i = 1; i < ARRAY_SIZE(btrfs_compress_types); i++) {
83 91 : size_t comp_len = strlen(btrfs_compress_types[i]);
84 :
85 91 : if (len < comp_len)
86 38 : continue;
87 :
88 53 : if (!strncmp(btrfs_compress_types[i], str, comp_len))
89 : return true;
90 : }
91 : return false;
92 : }
93 :
94 158304 : static int compression_compress_pages(int type, struct list_head *ws,
95 : struct address_space *mapping, u64 start, struct page **pages,
96 : unsigned long *out_pages, unsigned long *total_in,
97 : unsigned long *total_out)
98 : {
99 158304 : switch (type) {
100 141531 : case BTRFS_COMPRESS_ZLIB:
101 141531 : return zlib_compress_pages(ws, mapping, start, pages,
102 : out_pages, total_in, total_out);
103 8559 : case BTRFS_COMPRESS_LZO:
104 8559 : return lzo_compress_pages(ws, mapping, start, pages,
105 : out_pages, total_in, total_out);
106 8214 : case BTRFS_COMPRESS_ZSTD:
107 8214 : return zstd_compress_pages(ws, mapping, start, pages,
108 : out_pages, total_in, total_out);
109 0 : case BTRFS_COMPRESS_NONE:
110 : default:
111 : /*
112 : * This can happen when compression races with remount setting
113 : * it to 'no compress', while caller doesn't call
114 : * inode_need_compress() to check if we really need to
115 : * compress.
116 : *
117 : * Not a big deal, just need to inform caller that we
118 : * haven't allocated any pages yet.
119 : */
120 0 : *out_pages = 0;
121 0 : return -E2BIG;
122 : }
123 : }
124 :
125 1157 : static int compression_decompress_bio(struct list_head *ws,
126 : struct compressed_bio *cb)
127 : {
128 1157 : switch (cb->compress_type) {
129 1094 : case BTRFS_COMPRESS_ZLIB: return zlib_decompress_bio(ws, cb);
130 59 : case BTRFS_COMPRESS_LZO: return lzo_decompress_bio(ws, cb);
131 4 : case BTRFS_COMPRESS_ZSTD: return zstd_decompress_bio(ws, cb);
132 0 : case BTRFS_COMPRESS_NONE:
133 : default:
134 : /*
135 : * This can't happen, the type is validated several times
136 : * before we get here.
137 : */
138 0 : BUG();
139 : }
140 : }
141 :
142 27 : static int compression_decompress(int type, struct list_head *ws,
143 : const u8 *data_in, struct page *dest_page,
144 : unsigned long start_byte, size_t srclen, size_t destlen)
145 : {
146 27 : switch (type) {
147 25 : case BTRFS_COMPRESS_ZLIB: return zlib_decompress(ws, data_in, dest_page,
148 : start_byte, srclen, destlen);
149 2 : case BTRFS_COMPRESS_LZO: return lzo_decompress(ws, data_in, dest_page,
150 : start_byte, srclen, destlen);
151 0 : case BTRFS_COMPRESS_ZSTD: return zstd_decompress(ws, data_in, dest_page,
152 : start_byte, srclen, destlen);
153 0 : case BTRFS_COMPRESS_NONE:
154 : default:
155 : /*
156 : * This can't happen, the type is validated several times
157 : * before we get here.
158 : */
159 0 : BUG();
160 : }
161 : }
162 :
163 159437 : static void btrfs_free_compressed_pages(struct compressed_bio *cb)
164 : {
165 711892 : for (unsigned int i = 0; i < cb->nr_pages; i++)
166 552497 : put_page(cb->compressed_pages[i]);
167 159395 : kfree(cb->compressed_pages);
168 159418 : }
169 :
170 : static int btrfs_decompress_bio(struct compressed_bio *cb);
171 :
172 1156 : static void end_compressed_bio_read(struct btrfs_bio *bbio)
173 : {
174 1156 : struct compressed_bio *cb = to_compressed_bio(bbio);
175 1156 : blk_status_t status = bbio->bio.bi_status;
176 :
177 1156 : if (!status)
178 1157 : status = errno_to_blk_status(btrfs_decompress_bio(cb));
179 :
180 1156 : btrfs_free_compressed_pages(cb);
181 1157 : btrfs_bio_end_io(cb->orig_bbio, status);
182 1157 : bio_put(&bbio->bio);
183 1157 : }
184 :
185 : /*
186 : * Clear the writeback bits on all of the file
187 : * pages for a compressed write
188 : */
189 158277 : static noinline void end_compressed_writeback(const struct compressed_bio *cb)
190 : {
191 158277 : struct inode *inode = &cb->bbio.inode->vfs_inode;
192 158277 : struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
193 158277 : unsigned long index = cb->start >> PAGE_SHIFT;
194 158277 : unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT;
195 158277 : struct folio_batch fbatch;
196 158277 : const int errno = blk_status_to_errno(cb->bbio.bio.bi_status);
197 158276 : int i;
198 158276 : int ret;
199 :
200 158276 : if (errno)
201 0 : mapping_set_error(inode->i_mapping, errno);
202 :
203 158276 : folio_batch_init(&fbatch);
204 632646 : while (index <= end_index) {
205 474365 : ret = filemap_get_folios(inode->i_mapping, &index, end_index,
206 : &fbatch);
207 :
208 474366 : if (ret == 0)
209 0 : return;
210 :
211 5535086 : for (i = 0; i < ret; i++) {
212 5060586 : struct folio *folio = fbatch.folios[i];
213 :
214 5060586 : btrfs_page_clamp_clear_writeback(fs_info, &folio->page,
215 5060586 : cb->start, cb->len);
216 : }
217 474500 : folio_batch_release(&fbatch);
218 : }
219 : /* the inode may be gone now */
220 : }
221 :
222 158282 : static void btrfs_finish_compressed_write_work(struct work_struct *work)
223 : {
224 158282 : struct compressed_bio *cb =
225 158282 : container_of(work, struct compressed_bio, write_end_work);
226 :
227 158282 : btrfs_finish_ordered_extent(cb->bbio.ordered, NULL, cb->start, cb->len,
228 158282 : cb->bbio.bio.bi_status == BLK_STS_OK);
229 :
230 158282 : if (cb->writeback)
231 158282 : end_compressed_writeback(cb);
232 : /* Note, our inode could be gone now */
233 :
234 158282 : btrfs_free_compressed_pages(cb);
235 158262 : bio_put(&cb->bbio.bio);
236 158262 : }
237 :
238 : /*
239 : * Do the cleanup once all the compressed pages hit the disk. This will clear
240 : * writeback on the file pages and free the compressed pages.
241 : *
242 : * This also calls the writeback end hooks for the file pages so that metadata
243 : * and checksums can be updated in the file.
244 : */
245 158282 : static void end_compressed_bio_write(struct btrfs_bio *bbio)
246 : {
247 158282 : struct compressed_bio *cb = to_compressed_bio(bbio);
248 158282 : struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
249 :
250 158282 : queue_work(fs_info->compressed_write_workers, &cb->write_end_work);
251 158282 : }
252 :
253 159439 : static void btrfs_add_compressed_bio_pages(struct compressed_bio *cb)
254 : {
255 159439 : struct bio *bio = &cb->bbio.bio;
256 159439 : u32 offset = 0;
257 :
258 712281 : while (offset < cb->compressed_len) {
259 552842 : u32 len = min_t(u32, cb->compressed_len - offset, PAGE_SIZE);
260 :
261 : /* Maximum compressed extent is smaller than bio size limit. */
262 552842 : __bio_add_page(bio, cb->compressed_pages[offset >> PAGE_SHIFT],
263 : len, 0);
264 552842 : offset += len;
265 : }
266 159439 : }
267 :
268 : /*
269 : * worker function to build and submit bios for previously compressed pages.
270 : * The corresponding pages in the inode should be marked for writeback
271 : * and the compressed pages should have a reference on them for dropping
272 : * when the IO is complete.
273 : *
274 : * This also checksums the file bytes and gets things ready for
275 : * the end io hooks.
276 : */
277 158282 : void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered,
278 : struct page **compressed_pages,
279 : unsigned int nr_pages,
280 : blk_opf_t write_flags,
281 : bool writeback)
282 : {
283 158282 : struct btrfs_inode *inode = BTRFS_I(ordered->inode);
284 158282 : struct btrfs_fs_info *fs_info = inode->root->fs_info;
285 158282 : struct compressed_bio *cb;
286 :
287 158282 : ASSERT(IS_ALIGNED(ordered->file_offset, fs_info->sectorsize));
288 158282 : ASSERT(IS_ALIGNED(ordered->num_bytes, fs_info->sectorsize));
289 :
290 158282 : cb = alloc_compressed_bio(inode, ordered->file_offset,
291 : REQ_OP_WRITE | write_flags,
292 : end_compressed_bio_write);
293 158282 : cb->start = ordered->file_offset;
294 158282 : cb->len = ordered->num_bytes;
295 158282 : cb->compressed_pages = compressed_pages;
296 158282 : cb->compressed_len = ordered->disk_num_bytes;
297 158282 : cb->writeback = writeback;
298 158282 : INIT_WORK(&cb->write_end_work, btrfs_finish_compressed_write_work);
299 158282 : cb->nr_pages = nr_pages;
300 158282 : cb->bbio.bio.bi_iter.bi_sector = ordered->disk_bytenr >> SECTOR_SHIFT;
301 158282 : cb->bbio.ordered = ordered;
302 158282 : btrfs_add_compressed_bio_pages(cb);
303 :
304 158282 : btrfs_submit_bio(&cb->bbio, 0);
305 158282 : }
306 :
307 : /*
308 : * Add extra pages in the same compressed file extent so that we don't need to
309 : * re-read the same extent again and again.
310 : *
311 : * NOTE: this won't work well for subpage, as for subpage read, we lock the
312 : * full page then submit bio for each compressed/regular extents.
313 : *
314 : * This means, if we have several sectors in the same page points to the same
315 : * on-disk compressed data, we will re-read the same extent many times and
316 : * this function can only help for the next page.
317 : */
318 1157 : static noinline int add_ra_bio_pages(struct inode *inode,
319 : u64 compressed_end,
320 : struct compressed_bio *cb,
321 : int *memstall, unsigned long *pflags)
322 : {
323 1157 : struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
324 1157 : unsigned long end_index;
325 1157 : struct bio *orig_bio = &cb->orig_bbio->bio;
326 1157 : u64 cur = cb->orig_bbio->file_offset + orig_bio->bi_iter.bi_size;
327 1157 : u64 isize = i_size_read(inode);
328 1157 : int ret;
329 1157 : struct page *page;
330 1157 : struct extent_map *em;
331 1157 : struct address_space *mapping = inode->i_mapping;
332 1157 : struct extent_map_tree *em_tree;
333 1157 : struct extent_io_tree *tree;
334 1157 : int sectors_missed = 0;
335 :
336 1157 : em_tree = &BTRFS_I(inode)->extent_tree;
337 1157 : tree = &BTRFS_I(inode)->io_tree;
338 :
339 1157 : if (isize == 0)
340 : return 0;
341 :
342 : /*
343 : * For current subpage support, we only support 64K page size,
344 : * which means maximum compressed extent size (128K) is just 2x page
345 : * size.
346 : * This makes readahead less effective, so here disable readahead for
347 : * subpage for now, until full compressed write is supported.
348 : */
349 1157 : if (btrfs_sb(inode->i_sb)->sectorsize < PAGE_SIZE)
350 : return 0;
351 :
352 1157 : end_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
353 :
354 1695 : while (cur < compressed_end) {
355 538 : u64 page_end;
356 538 : u64 pg_index = cur >> PAGE_SHIFT;
357 538 : u32 add_size;
358 :
359 538 : if (pg_index > end_index)
360 : break;
361 :
362 538 : page = xa_load(&mapping->i_pages, pg_index);
363 538 : if (page && !xa_is_value(page)) {
364 0 : sectors_missed += (PAGE_SIZE - offset_in_page(cur)) >>
365 0 : fs_info->sectorsize_bits;
366 :
367 : /* Beyond threshold, no need to continue */
368 0 : if (sectors_missed > 4)
369 : break;
370 :
371 : /*
372 : * Jump to next page start as we already have page for
373 : * current offset.
374 : */
375 0 : cur = (pg_index << PAGE_SHIFT) + PAGE_SIZE;
376 0 : continue;
377 : }
378 :
379 538 : page = __page_cache_alloc(mapping_gfp_constraint(mapping,
380 : ~__GFP_FS));
381 538 : if (!page)
382 : break;
383 :
384 538 : if (add_to_page_cache_lru(page, mapping, pg_index, GFP_NOFS)) {
385 0 : put_page(page);
386 : /* There is already a page, skip to page end */
387 0 : cur = (pg_index << PAGE_SHIFT) + PAGE_SIZE;
388 0 : continue;
389 : }
390 :
391 1076 : if (!*memstall && PageWorkingset(page)) {
392 0 : psi_memstall_enter(pflags);
393 0 : *memstall = 1;
394 : }
395 :
396 538 : ret = set_page_extent_mapped(page);
397 538 : if (ret < 0) {
398 0 : unlock_page(page);
399 0 : put_page(page);
400 0 : break;
401 : }
402 :
403 538 : page_end = (pg_index << PAGE_SHIFT) + PAGE_SIZE - 1;
404 538 : lock_extent(tree, cur, page_end, NULL);
405 538 : read_lock(&em_tree->lock);
406 538 : em = lookup_extent_mapping(em_tree, cur, page_end + 1 - cur);
407 538 : read_unlock(&em_tree->lock);
408 :
409 : /*
410 : * At this point, we have a locked page in the page cache for
411 : * these bytes in the file. But, we have to make sure they map
412 : * to this compressed extent on disk.
413 : */
414 1076 : if (!em || cur < em->start ||
415 538 : (cur + fs_info->sectorsize > extent_map_end(em)) ||
416 538 : (em->block_start >> SECTOR_SHIFT) != orig_bio->bi_iter.bi_sector) {
417 0 : free_extent_map(em);
418 0 : unlock_extent(tree, cur, page_end, NULL);
419 0 : unlock_page(page);
420 0 : put_page(page);
421 0 : break;
422 : }
423 538 : free_extent_map(em);
424 :
425 538 : if (page->index == end_index) {
426 6 : size_t zero_offset = offset_in_page(isize);
427 :
428 6 : if (zero_offset) {
429 0 : int zeros;
430 0 : zeros = PAGE_SIZE - zero_offset;
431 0 : memzero_page(page, zero_offset, zeros);
432 : }
433 : }
434 :
435 538 : add_size = min(em->start + em->len, page_end + 1) - cur;
436 538 : ret = bio_add_page(orig_bio, page, add_size, offset_in_page(cur));
437 538 : if (ret != add_size) {
438 0 : unlock_extent(tree, cur, page_end, NULL);
439 0 : unlock_page(page);
440 0 : put_page(page);
441 0 : break;
442 : }
443 : /*
444 : * If it's subpage, we also need to increase its
445 : * subpage::readers number, as at endio we will decrease
446 : * subpage::readers and to unlock the page.
447 : */
448 538 : if (fs_info->sectorsize < PAGE_SIZE)
449 0 : btrfs_subpage_start_reader(fs_info, page, cur, add_size);
450 538 : put_page(page);
451 538 : cur += add_size;
452 : }
453 : return 0;
454 : }
455 :
456 : /*
457 : * for a compressed read, the bio we get passed has all the inode pages
458 : * in it. We don't actually do IO on those pages but allocate new ones
459 : * to hold the compressed pages on disk.
460 : *
461 : * bio->bi_iter.bi_sector points to the compressed extent on disk
462 : * bio->bi_io_vec points to all of the inode pages
463 : *
464 : * After the compressed pages are read, we copy the bytes into the
465 : * bio we were passed and then call the bio end_io calls
466 : */
467 1157 : void btrfs_submit_compressed_read(struct btrfs_bio *bbio)
468 : {
469 1157 : struct btrfs_inode *inode = bbio->inode;
470 1157 : struct btrfs_fs_info *fs_info = inode->root->fs_info;
471 1157 : struct extent_map_tree *em_tree = &inode->extent_tree;
472 1157 : struct compressed_bio *cb;
473 1157 : unsigned int compressed_len;
474 1157 : u64 file_offset = bbio->file_offset;
475 1157 : u64 em_len;
476 1157 : u64 em_start;
477 1157 : struct extent_map *em;
478 1157 : unsigned long pflags;
479 1157 : int memstall = 0;
480 1157 : blk_status_t ret;
481 1157 : int ret2;
482 :
483 : /* we need the actual starting offset of this extent in the file */
484 1157 : read_lock(&em_tree->lock);
485 1157 : em = lookup_extent_mapping(em_tree, file_offset, fs_info->sectorsize);
486 1157 : read_unlock(&em_tree->lock);
487 1157 : if (!em) {
488 0 : ret = BLK_STS_IOERR;
489 0 : goto out;
490 : }
491 :
492 1157 : ASSERT(em->compress_type != BTRFS_COMPRESS_NONE);
493 1157 : compressed_len = em->block_len;
494 :
495 1157 : cb = alloc_compressed_bio(inode, file_offset, REQ_OP_READ,
496 : end_compressed_bio_read);
497 :
498 1157 : cb->start = em->orig_start;
499 1157 : em_len = em->len;
500 1157 : em_start = em->start;
501 :
502 1157 : cb->len = bbio->bio.bi_iter.bi_size;
503 1157 : cb->compressed_len = compressed_len;
504 1157 : cb->compress_type = em->compress_type;
505 1157 : cb->orig_bbio = bbio;
506 :
507 1157 : free_extent_map(em);
508 :
509 1157 : cb->nr_pages = DIV_ROUND_UP(compressed_len, PAGE_SIZE);
510 1157 : cb->compressed_pages = kcalloc(cb->nr_pages, sizeof(struct page *), GFP_NOFS);
511 1157 : if (!cb->compressed_pages) {
512 0 : ret = BLK_STS_RESOURCE;
513 0 : goto out_free_bio;
514 : }
515 :
516 1157 : ret2 = btrfs_alloc_page_array(cb->nr_pages, cb->compressed_pages);
517 1157 : if (ret2) {
518 0 : ret = BLK_STS_RESOURCE;
519 0 : goto out_free_compressed_pages;
520 : }
521 :
522 1157 : add_ra_bio_pages(&inode->vfs_inode, em_start + em_len, cb, &memstall,
523 : &pflags);
524 :
525 : /* include any pages we added in add_ra-bio_pages */
526 1157 : cb->len = bbio->bio.bi_iter.bi_size;
527 1157 : cb->bbio.bio.bi_iter.bi_sector = bbio->bio.bi_iter.bi_sector;
528 1157 : btrfs_add_compressed_bio_pages(cb);
529 :
530 1157 : if (memstall)
531 0 : psi_memstall_leave(&pflags);
532 :
533 1157 : btrfs_submit_bio(&cb->bbio, 0);
534 1157 : return;
535 :
536 : out_free_compressed_pages:
537 0 : kfree(cb->compressed_pages);
538 0 : out_free_bio:
539 0 : bio_put(&cb->bbio.bio);
540 0 : out:
541 0 : btrfs_bio_end_io(bbio, ret);
542 : }
543 :
544 : /*
545 : * Heuristic uses systematic sampling to collect data from the input data
546 : * range, the logic can be tuned by the following constants:
547 : *
548 : * @SAMPLING_READ_SIZE - how many bytes will be copied from for each sample
549 : * @SAMPLING_INTERVAL - range from which the sampled data can be collected
550 : */
551 : #define SAMPLING_READ_SIZE (16)
552 : #define SAMPLING_INTERVAL (256)
553 :
554 : /*
555 : * For statistical analysis of the input data we consider bytes that form a
556 : * Galois Field of 256 objects. Each object has an attribute count, ie. how
557 : * many times the object appeared in the sample.
558 : */
559 : #define BUCKET_SIZE (256)
560 :
561 : /*
562 : * The size of the sample is based on a statistical sampling rule of thumb.
563 : * The common way is to perform sampling tests as long as the number of
564 : * elements in each cell is at least 5.
565 : *
566 : * Instead of 5, we choose 32 to obtain more accurate results.
567 : * If the data contain the maximum number of symbols, which is 256, we obtain a
568 : * sample size bound by 8192.
569 : *
570 : * For a sample of at most 8KB of data per data range: 16 consecutive bytes
571 : * from up to 512 locations.
572 : */
573 : #define MAX_SAMPLE_SIZE (BTRFS_MAX_UNCOMPRESSED * \
574 : SAMPLING_READ_SIZE / SAMPLING_INTERVAL)
575 :
576 : struct bucket_item {
577 : u32 count;
578 : };
579 :
580 : struct heuristic_ws {
581 : /* Partial copy of input data */
582 : u8 *sample;
583 : u32 sample_size;
584 : /* Buckets store counters for each byte value */
585 : struct bucket_item *bucket;
586 : /* Sorting buffer */
587 : struct bucket_item *bucket_b;
588 : struct list_head list;
589 : };
590 :
591 : static struct workspace_manager heuristic_wsm;
592 :
593 0 : static void free_heuristic_ws(struct list_head *ws)
594 : {
595 0 : struct heuristic_ws *workspace;
596 :
597 0 : workspace = list_entry(ws, struct heuristic_ws, list);
598 :
599 0 : kvfree(workspace->sample);
600 0 : kfree(workspace->bucket);
601 0 : kfree(workspace->bucket_b);
602 0 : kfree(workspace);
603 0 : }
604 :
605 15 : static struct list_head *alloc_heuristic_ws(unsigned int level)
606 : {
607 15 : struct heuristic_ws *ws;
608 :
609 15 : ws = kzalloc(sizeof(*ws), GFP_KERNEL);
610 15 : if (!ws)
611 : return ERR_PTR(-ENOMEM);
612 :
613 15 : ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL);
614 15 : if (!ws->sample)
615 0 : goto fail;
616 :
617 15 : ws->bucket = kcalloc(BUCKET_SIZE, sizeof(*ws->bucket), GFP_KERNEL);
618 15 : if (!ws->bucket)
619 0 : goto fail;
620 :
621 15 : ws->bucket_b = kcalloc(BUCKET_SIZE, sizeof(*ws->bucket_b), GFP_KERNEL);
622 15 : if (!ws->bucket_b)
623 0 : goto fail;
624 :
625 15 : INIT_LIST_HEAD(&ws->list);
626 15 : return &ws->list;
627 0 : fail:
628 0 : free_heuristic_ws(&ws->list);
629 0 : return ERR_PTR(-ENOMEM);
630 : }
631 :
632 : const struct btrfs_compress_op btrfs_heuristic_compress = {
633 : .workspace_manager = &heuristic_wsm,
634 : };
635 :
636 : static const struct btrfs_compress_op * const btrfs_compress_op[] = {
637 : /* The heuristic is represented as compression type 0 */
638 : &btrfs_heuristic_compress,
639 : &btrfs_zlib_compress,
640 : &btrfs_lzo_compress,
641 : &btrfs_zstd_compress,
642 : };
643 :
644 45 : static struct list_head *alloc_workspace(int type, unsigned int level)
645 : {
646 45 : switch (type) {
647 15 : case BTRFS_COMPRESS_NONE: return alloc_heuristic_ws(level);
648 15 : case BTRFS_COMPRESS_ZLIB: return zlib_alloc_workspace(level);
649 15 : case BTRFS_COMPRESS_LZO: return lzo_alloc_workspace(level);
650 0 : case BTRFS_COMPRESS_ZSTD: return zstd_alloc_workspace(level);
651 0 : default:
652 : /*
653 : * This can't happen, the type is validated several times
654 : * before we get here.
655 : */
656 0 : BUG();
657 : }
658 : }
659 :
660 0 : static void free_workspace(int type, struct list_head *ws)
661 : {
662 0 : switch (type) {
663 0 : case BTRFS_COMPRESS_NONE: return free_heuristic_ws(ws);
664 0 : case BTRFS_COMPRESS_ZLIB: return zlib_free_workspace(ws);
665 0 : case BTRFS_COMPRESS_LZO: return lzo_free_workspace(ws);
666 0 : case BTRFS_COMPRESS_ZSTD: return zstd_free_workspace(ws);
667 0 : default:
668 : /*
669 : * This can't happen, the type is validated several times
670 : * before we get here.
671 : */
672 0 : BUG();
673 : }
674 : }
675 :
676 33 : static void btrfs_init_workspace_manager(int type)
677 : {
678 33 : struct workspace_manager *wsm;
679 33 : struct list_head *workspace;
680 :
681 33 : wsm = btrfs_compress_op[type]->workspace_manager;
682 33 : INIT_LIST_HEAD(&wsm->idle_ws);
683 33 : spin_lock_init(&wsm->ws_lock);
684 33 : atomic_set(&wsm->total_ws, 0);
685 33 : init_waitqueue_head(&wsm->ws_wait);
686 :
687 : /*
688 : * Preallocate one workspace for each compression type so we can
689 : * guarantee forward progress in the worst case
690 : */
691 33 : workspace = alloc_workspace(type, 0);
692 33 : if (IS_ERR(workspace)) {
693 0 : pr_warn(
694 : "BTRFS: cannot preallocate compression workspace, will try later\n");
695 : } else {
696 33 : atomic_set(&wsm->total_ws, 1);
697 33 : wsm->free_ws = 1;
698 33 : list_add(workspace, &wsm->idle_ws);
699 : }
700 33 : }
701 :
702 0 : static void btrfs_cleanup_workspace_manager(int type)
703 : {
704 0 : struct workspace_manager *wsman;
705 0 : struct list_head *ws;
706 :
707 0 : wsman = btrfs_compress_op[type]->workspace_manager;
708 0 : while (!list_empty(&wsman->idle_ws)) {
709 0 : ws = wsman->idle_ws.next;
710 0 : list_del(ws);
711 0 : free_workspace(type, ws);
712 0 : atomic_dec(&wsman->total_ws);
713 : }
714 0 : }
715 :
716 : /*
717 : * This finds an available workspace or allocates a new one.
718 : * If it's not possible to allocate a new one, waits until there's one.
719 : * Preallocation makes a forward progress guarantees and we do not return
720 : * errors.
721 : */
722 309904 : struct list_head *btrfs_get_workspace(int type, unsigned int level)
723 : {
724 309904 : struct workspace_manager *wsm;
725 309904 : struct list_head *workspace;
726 309904 : int cpus = num_online_cpus();
727 309904 : unsigned nofs_flag;
728 309904 : struct list_head *idle_ws;
729 309904 : spinlock_t *ws_lock;
730 309904 : atomic_t *total_ws;
731 309904 : wait_queue_head_t *ws_wait;
732 309904 : int *free_ws;
733 :
734 309904 : wsm = btrfs_compress_op[type]->workspace_manager;
735 309904 : idle_ws = &wsm->idle_ws;
736 309904 : ws_lock = &wsm->ws_lock;
737 309904 : total_ws = &wsm->total_ws;
738 309904 : ws_wait = &wsm->ws_wait;
739 309904 : free_ws = &wsm->free_ws;
740 :
741 : again:
742 317155 : spin_lock(ws_lock);
743 317384 : if (!list_empty(idle_ws)) {
744 310121 : workspace = idle_ws->next;
745 310121 : list_del(workspace);
746 310127 : (*free_ws)--;
747 310127 : spin_unlock(ws_lock);
748 310127 : return workspace;
749 :
750 : }
751 7263 : if (atomic_read(total_ws) > cpus) {
752 7251 : DEFINE_WAIT(wait);
753 :
754 7251 : spin_unlock(ws_lock);
755 7251 : prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE);
756 7251 : if (atomic_read(total_ws) > cpus && !*free_ws)
757 7248 : schedule();
758 7251 : finish_wait(ws_wait, &wait);
759 7251 : goto again;
760 : }
761 12 : atomic_inc(total_ws);
762 12 : spin_unlock(ws_lock);
763 :
764 : /*
765 : * Allocation helpers call vmalloc that can't use GFP_NOFS, so we have
766 : * to turn it off here because we might get called from the restricted
767 : * context of btrfs_compress_bio/btrfs_compress_pages
768 : */
769 12 : nofs_flag = memalloc_nofs_save();
770 12 : workspace = alloc_workspace(type, level);
771 12 : memalloc_nofs_restore(nofs_flag);
772 :
773 12 : if (IS_ERR(workspace)) {
774 0 : atomic_dec(total_ws);
775 0 : wake_up(ws_wait);
776 :
777 : /*
778 : * Do not return the error but go back to waiting. There's a
779 : * workspace preallocated for each type and the compression
780 : * time is bounded so we get to a workspace eventually. This
781 : * makes our caller's life easier.
782 : *
783 : * To prevent silent and low-probability deadlocks (when the
784 : * initial preallocation fails), check if there are any
785 : * workspaces at all.
786 : */
787 0 : if (atomic_read(total_ws) == 0) {
788 0 : static DEFINE_RATELIMIT_STATE(_rs,
789 : /* once per minute */ 60 * HZ,
790 : /* no burst */ 1);
791 :
792 0 : if (__ratelimit(&_rs)) {
793 0 : pr_warn("BTRFS: no compression workspaces, low memory, retrying\n");
794 : }
795 : }
796 0 : goto again;
797 : }
798 : return workspace;
799 : }
800 :
801 159467 : static struct list_head *get_workspace(int type, int level)
802 : {
803 159467 : switch (type) {
804 0 : case BTRFS_COMPRESS_NONE: return btrfs_get_workspace(type, level);
805 142631 : case BTRFS_COMPRESS_ZLIB: return zlib_get_workspace(level);
806 8620 : case BTRFS_COMPRESS_LZO: return btrfs_get_workspace(type, level);
807 8216 : case BTRFS_COMPRESS_ZSTD: return zstd_get_workspace(level);
808 0 : default:
809 : /*
810 : * This can't happen, the type is validated several times
811 : * before we get here.
812 : */
813 0 : BUG();
814 : }
815 : }
816 :
817 : /*
818 : * put a workspace struct back on the list or free it if we have enough
819 : * idle ones sitting around
820 : */
821 309753 : void btrfs_put_workspace(int type, struct list_head *ws)
822 : {
823 309753 : struct workspace_manager *wsm;
824 309753 : struct list_head *idle_ws;
825 309753 : spinlock_t *ws_lock;
826 309753 : atomic_t *total_ws;
827 309753 : wait_queue_head_t *ws_wait;
828 309753 : int *free_ws;
829 :
830 309753 : wsm = btrfs_compress_op[type]->workspace_manager;
831 309753 : idle_ws = &wsm->idle_ws;
832 309753 : ws_lock = &wsm->ws_lock;
833 309753 : total_ws = &wsm->total_ws;
834 309753 : ws_wait = &wsm->ws_wait;
835 309753 : free_ws = &wsm->free_ws;
836 :
837 309753 : spin_lock(ws_lock);
838 310144 : if (*free_ws <= num_online_cpus()) {
839 310144 : list_add(ws, idle_ws);
840 310131 : (*free_ws)++;
841 310131 : spin_unlock(ws_lock);
842 310139 : goto wake;
843 : }
844 0 : spin_unlock(ws_lock);
845 :
846 0 : free_workspace(type, ws);
847 0 : atomic_dec(total_ws);
848 310139 : wake:
849 310139 : cond_wake_up(ws_wait);
850 310140 : }
851 :
852 159154 : static void put_workspace(int type, struct list_head *ws)
853 : {
854 159154 : switch (type) {
855 0 : case BTRFS_COMPRESS_NONE: return btrfs_put_workspace(type, ws);
856 142320 : case BTRFS_COMPRESS_ZLIB: return btrfs_put_workspace(type, ws);
857 8618 : case BTRFS_COMPRESS_LZO: return btrfs_put_workspace(type, ws);
858 8216 : case BTRFS_COMPRESS_ZSTD: return zstd_put_workspace(ws);
859 0 : default:
860 : /*
861 : * This can't happen, the type is validated several times
862 : * before we get here.
863 : */
864 0 : BUG();
865 : }
866 : }
867 :
868 : /*
869 : * Adjust @level according to the limits of the compression algorithm or
870 : * fallback to default
871 : */
872 : static unsigned int btrfs_compress_set_level(int type, unsigned level)
873 : {
874 158307 : const struct btrfs_compress_op *ops = btrfs_compress_op[type];
875 :
876 158307 : if (level == 0)
877 25004 : level = ops->default_level;
878 : else
879 133303 : level = min(level, ops->max_level);
880 :
881 158307 : return level;
882 : }
883 :
884 : /*
885 : * Given an address space and start and length, compress the bytes into @pages
886 : * that are allocated on demand.
887 : *
888 : * @type_level is encoded algorithm and level, where level 0 means whatever
889 : * default the algorithm chooses and is opaque here;
890 : * - compression algo are 0-3
891 : * - the level are bits 4-7
892 : *
893 : * @out_pages is an in/out parameter, holds maximum number of pages to allocate
894 : * and returns number of actually allocated pages
895 : *
896 : * @total_in is used to return the number of bytes actually read. It
897 : * may be smaller than the input length if we had to exit early because we
898 : * ran out of room in the pages array or because we cross the
899 : * max_out threshold.
900 : *
901 : * @total_out is an in/out parameter, must be set to the input length and will
902 : * be also used to return the total number of compressed bytes
903 : */
904 158287 : int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
905 : u64 start, struct page **pages,
906 : unsigned long *out_pages,
907 : unsigned long *total_in,
908 : unsigned long *total_out)
909 : {
910 158287 : int type = btrfs_compress_type(type_level);
911 158287 : int level = btrfs_compress_level(type_level);
912 158287 : struct list_head *workspace;
913 158287 : int ret;
914 :
915 158287 : level = btrfs_compress_set_level(type, level);
916 158287 : workspace = get_workspace(type, level);
917 158304 : ret = compression_compress_pages(type, workspace, mapping, start, pages,
918 : out_pages, total_in, total_out);
919 157951 : put_workspace(type, workspace);
920 158302 : return ret;
921 : }
922 :
923 1157 : static int btrfs_decompress_bio(struct compressed_bio *cb)
924 : {
925 1157 : struct list_head *workspace;
926 1157 : int ret;
927 1157 : int type = cb->compress_type;
928 :
929 1157 : workspace = get_workspace(type, 0);
930 1157 : ret = compression_decompress_bio(workspace, cb);
931 1154 : put_workspace(type, workspace);
932 :
933 1157 : if (!ret)
934 1157 : zero_fill_bio(&cb->orig_bbio->bio);
935 1157 : return ret;
936 : }
937 :
938 : /*
939 : * a less complex decompression routine. Our compressed data fits in a
940 : * single page, and we want to read a single page out of it.
941 : * start_byte tells us the offset into the compressed data we're interested in
942 : */
943 27 : int btrfs_decompress(int type, const u8 *data_in, struct page *dest_page,
944 : unsigned long start_byte, size_t srclen, size_t destlen)
945 : {
946 27 : struct list_head *workspace;
947 27 : int ret;
948 :
949 27 : workspace = get_workspace(type, 0);
950 27 : ret = compression_decompress(type, workspace, data_in, dest_page,
951 : start_byte, srclen, destlen);
952 27 : put_workspace(type, workspace);
953 :
954 27 : return ret;
955 : }
956 :
957 11 : int __init btrfs_init_compress(void)
958 : {
959 11 : if (bioset_init(&btrfs_compressed_bioset, BIO_POOL_SIZE,
960 : offsetof(struct compressed_bio, bbio.bio),
961 : BIOSET_NEED_BVECS))
962 : return -ENOMEM;
963 11 : btrfs_init_workspace_manager(BTRFS_COMPRESS_NONE);
964 11 : btrfs_init_workspace_manager(BTRFS_COMPRESS_ZLIB);
965 11 : btrfs_init_workspace_manager(BTRFS_COMPRESS_LZO);
966 11 : zstd_init_workspace_manager();
967 11 : return 0;
968 : }
969 :
970 0 : void __cold btrfs_exit_compress(void)
971 : {
972 0 : btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_NONE);
973 0 : btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_ZLIB);
974 0 : btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_LZO);
975 0 : zstd_cleanup_workspace_manager();
976 0 : bioset_exit(&btrfs_compressed_bioset);
977 0 : }
978 :
979 : /*
980 : * Copy decompressed data from working buffer to pages.
981 : *
982 : * @buf: The decompressed data buffer
983 : * @buf_len: The decompressed data length
984 : * @decompressed: Number of bytes that are already decompressed inside the
985 : * compressed extent
986 : * @cb: The compressed extent descriptor
987 : * @orig_bio: The original bio that the caller wants to read for
988 : *
989 : * An easier to understand graph is like below:
990 : *
991 : * |<- orig_bio ->| |<- orig_bio->|
992 : * |<------- full decompressed extent ----->|
993 : * |<----------- @cb range ---->|
994 : * | |<-- @buf_len -->|
995 : * |<--- @decompressed --->|
996 : *
997 : * Note that, @cb can be a subpage of the full decompressed extent, but
998 : * @cb->start always has the same as the orig_file_offset value of the full
999 : * decompressed extent.
1000 : *
1001 : * When reading compressed extent, we have to read the full compressed extent,
1002 : * while @orig_bio may only want part of the range.
1003 : * Thus this function will ensure only data covered by @orig_bio will be copied
1004 : * to.
1005 : *
1006 : * Return 0 if we have copied all needed contents for @orig_bio.
1007 : * Return >0 if we need continue decompress.
1008 : */
1009 34867 : int btrfs_decompress_buf2page(const char *buf, u32 buf_len,
1010 : struct compressed_bio *cb, u32 decompressed)
1011 : {
1012 34867 : struct bio *orig_bio = &cb->orig_bbio->bio;
1013 : /* Offset inside the full decompressed extent */
1014 34867 : u32 cur_offset;
1015 :
1016 34867 : cur_offset = decompressed;
1017 : /* The main loop to do the copy */
1018 68583 : while (cur_offset < decompressed + buf_len) {
1019 34918 : struct bio_vec bvec;
1020 34918 : size_t copy_len;
1021 34918 : u32 copy_start;
1022 : /* Offset inside the full decompressed extent */
1023 34918 : u32 bvec_offset;
1024 :
1025 34918 : bvec = bio_iter_iovec(orig_bio, orig_bio->bi_iter);
1026 : /*
1027 : * cb->start may underflow, but subtracting that value can still
1028 : * give us correct offset inside the full decompressed extent.
1029 : */
1030 34918 : bvec_offset = page_offset(bvec.bv_page) + bvec.bv_offset - cb->start;
1031 :
1032 : /* Haven't reached the bvec range, exit */
1033 34918 : if (decompressed + buf_len <= bvec_offset)
1034 : return 1;
1035 :
1036 34653 : copy_start = max(cur_offset, bvec_offset);
1037 34653 : copy_len = min(bvec_offset + bvec.bv_len,
1038 34653 : decompressed + buf_len) - copy_start;
1039 34653 : ASSERT(copy_len);
1040 :
1041 : /*
1042 : * Extra range check to ensure we didn't go beyond
1043 : * @buf + @buf_len.
1044 : */
1045 34653 : ASSERT(copy_start - decompressed < buf_len);
1046 34653 : memcpy_to_page(bvec.bv_page, bvec.bv_offset,
1047 34653 : buf + copy_start - decompressed, copy_len);
1048 34847 : cur_offset += copy_len;
1049 :
1050 34847 : bio_advance(orig_bio, copy_len);
1051 : /* Finished the bio */
1052 34842 : if (!orig_bio->bi_iter.bi_size)
1053 : return 0;
1054 : }
1055 : return 1;
1056 : }
1057 :
1058 : /*
1059 : * Shannon Entropy calculation
1060 : *
1061 : * Pure byte distribution analysis fails to determine compressibility of data.
1062 : * Try calculating entropy to estimate the average minimum number of bits
1063 : * needed to encode the sampled data.
1064 : *
1065 : * For convenience, return the percentage of needed bits, instead of amount of
1066 : * bits directly.
1067 : *
1068 : * @ENTROPY_LVL_ACEPTABLE - below that threshold, sample has low byte entropy
1069 : * and can be compressible with high probability
1070 : *
1071 : * @ENTROPY_LVL_HIGH - data are not compressible with high probability
1072 : *
1073 : * Use of ilog2() decreases precision, we lower the LVL to 5 to compensate.
1074 : */
1075 : #define ENTROPY_LVL_ACEPTABLE (65)
1076 : #define ENTROPY_LVL_HIGH (80)
1077 :
1078 : /*
1079 : * For increasead precision in shannon_entropy calculation,
1080 : * let's do pow(n, M) to save more digits after comma:
1081 : *
1082 : * - maximum int bit length is 64
1083 : * - ilog2(MAX_SAMPLE_SIZE) -> 13
1084 : * - 13 * 4 = 52 < 64 -> M = 4
1085 : *
1086 : * So use pow(n, 4).
1087 : */
1088 0 : static inline u32 ilog2_w(u64 n)
1089 : {
1090 0 : return ilog2(n * n * n * n);
1091 : }
1092 :
1093 0 : static u32 shannon_entropy(struct heuristic_ws *ws)
1094 : {
1095 0 : const u32 entropy_max = 8 * ilog2_w(2);
1096 0 : u32 entropy_sum = 0;
1097 0 : u32 p, p_base, sz_base;
1098 0 : u32 i;
1099 :
1100 0 : sz_base = ilog2_w(ws->sample_size);
1101 0 : for (i = 0; i < BUCKET_SIZE && ws->bucket[i].count > 0; i++) {
1102 0 : p = ws->bucket[i].count;
1103 0 : p_base = ilog2_w(p);
1104 0 : entropy_sum += p * (sz_base - p_base);
1105 : }
1106 :
1107 0 : entropy_sum /= ws->sample_size;
1108 0 : return entropy_sum * 100 / entropy_max;
1109 : }
1110 :
1111 : #define RADIX_BASE 4U
1112 : #define COUNTERS_SIZE (1U << RADIX_BASE)
1113 :
1114 : static u8 get4bits(u64 num, int shift) {
1115 0 : u8 low4bits;
1116 :
1117 0 : num >>= shift;
1118 : /* Reverse order */
1119 0 : low4bits = (COUNTERS_SIZE - 1) - (num % COUNTERS_SIZE);
1120 0 : return low4bits;
1121 : }
1122 :
1123 : /*
1124 : * Use 4 bits as radix base
1125 : * Use 16 u32 counters for calculating new position in buf array
1126 : *
1127 : * @array - array that will be sorted
1128 : * @array_buf - buffer array to store sorting results
1129 : * must be equal in size to @array
1130 : * @num - array size
1131 : */
1132 0 : static void radix_sort(struct bucket_item *array, struct bucket_item *array_buf,
1133 : int num)
1134 : {
1135 0 : u64 max_num;
1136 0 : u64 buf_num;
1137 0 : u32 counters[COUNTERS_SIZE];
1138 0 : u32 new_addr;
1139 0 : u32 addr;
1140 0 : int bitlen;
1141 0 : int shift;
1142 0 : int i;
1143 :
1144 : /*
1145 : * Try avoid useless loop iterations for small numbers stored in big
1146 : * counters. Example: 48 33 4 ... in 64bit array
1147 : */
1148 0 : max_num = array[0].count;
1149 0 : for (i = 1; i < num; i++) {
1150 0 : buf_num = array[i].count;
1151 0 : if (buf_num > max_num)
1152 : max_num = buf_num;
1153 : }
1154 :
1155 0 : buf_num = ilog2(max_num);
1156 0 : bitlen = ALIGN(buf_num, RADIX_BASE * 2);
1157 :
1158 0 : shift = 0;
1159 0 : while (shift < bitlen) {
1160 0 : memset(counters, 0, sizeof(counters));
1161 :
1162 0 : for (i = 0; i < num; i++) {
1163 0 : buf_num = array[i].count;
1164 0 : addr = get4bits(buf_num, shift);
1165 0 : counters[addr]++;
1166 : }
1167 :
1168 0 : for (i = 1; i < COUNTERS_SIZE; i++)
1169 0 : counters[i] += counters[i - 1];
1170 :
1171 0 : for (i = num - 1; i >= 0; i--) {
1172 0 : buf_num = array[i].count;
1173 0 : addr = get4bits(buf_num, shift);
1174 0 : counters[addr]--;
1175 0 : new_addr = counters[addr];
1176 0 : array_buf[new_addr] = array[i];
1177 : }
1178 :
1179 0 : shift += RADIX_BASE;
1180 :
1181 : /*
1182 : * Normal radix expects to move data from a temporary array, to
1183 : * the main one. But that requires some CPU time. Avoid that
1184 : * by doing another sort iteration to original array instead of
1185 : * memcpy()
1186 : */
1187 0 : memset(counters, 0, sizeof(counters));
1188 :
1189 0 : for (i = 0; i < num; i ++) {
1190 0 : buf_num = array_buf[i].count;
1191 0 : addr = get4bits(buf_num, shift);
1192 0 : counters[addr]++;
1193 : }
1194 :
1195 0 : for (i = 1; i < COUNTERS_SIZE; i++)
1196 0 : counters[i] += counters[i - 1];
1197 :
1198 0 : for (i = num - 1; i >= 0; i--) {
1199 0 : buf_num = array_buf[i].count;
1200 0 : addr = get4bits(buf_num, shift);
1201 0 : counters[addr]--;
1202 0 : new_addr = counters[addr];
1203 0 : array[new_addr] = array_buf[i];
1204 : }
1205 :
1206 0 : shift += RADIX_BASE;
1207 : }
1208 0 : }
1209 :
1210 : /*
1211 : * Size of the core byte set - how many bytes cover 90% of the sample
1212 : *
1213 : * There are several types of structured binary data that use nearly all byte
1214 : * values. The distribution can be uniform and counts in all buckets will be
1215 : * nearly the same (eg. encrypted data). Unlikely to be compressible.
1216 : *
1217 : * Other possibility is normal (Gaussian) distribution, where the data could
1218 : * be potentially compressible, but we have to take a few more steps to decide
1219 : * how much.
1220 : *
1221 : * @BYTE_CORE_SET_LOW - main part of byte values repeated frequently,
1222 : * compression algo can easy fix that
1223 : * @BYTE_CORE_SET_HIGH - data have uniform distribution and with high
1224 : * probability is not compressible
1225 : */
1226 : #define BYTE_CORE_SET_LOW (64)
1227 : #define BYTE_CORE_SET_HIGH (200)
1228 :
1229 0 : static int byte_core_set_size(struct heuristic_ws *ws)
1230 : {
1231 0 : u32 i;
1232 0 : u32 coreset_sum = 0;
1233 0 : const u32 core_set_threshold = ws->sample_size * 90 / 100;
1234 0 : struct bucket_item *bucket = ws->bucket;
1235 :
1236 : /* Sort in reverse order */
1237 0 : radix_sort(ws->bucket, ws->bucket_b, BUCKET_SIZE);
1238 :
1239 0 : for (i = 0; i < BYTE_CORE_SET_LOW; i++)
1240 0 : coreset_sum += bucket[i].count;
1241 :
1242 0 : if (coreset_sum > core_set_threshold)
1243 : return i;
1244 :
1245 0 : for (; i < BYTE_CORE_SET_HIGH && bucket[i].count > 0; i++) {
1246 0 : coreset_sum += bucket[i].count;
1247 0 : if (coreset_sum > core_set_threshold)
1248 : break;
1249 : }
1250 :
1251 0 : return i;
1252 : }
1253 :
1254 : /*
1255 : * Count byte values in buckets.
1256 : * This heuristic can detect textual data (configs, xml, json, html, etc).
1257 : * Because in most text-like data byte set is restricted to limited number of
1258 : * possible characters, and that restriction in most cases makes data easy to
1259 : * compress.
1260 : *
1261 : * @BYTE_SET_THRESHOLD - consider all data within this byte set size:
1262 : * less - compressible
1263 : * more - need additional analysis
1264 : */
1265 : #define BYTE_SET_THRESHOLD (64)
1266 :
1267 24955 : static u32 byte_set_size(const struct heuristic_ws *ws)
1268 : {
1269 24955 : u32 i;
1270 24955 : u32 byte_set_size = 0;
1271 :
1272 1621566 : for (i = 0; i < BYTE_SET_THRESHOLD; i++) {
1273 1596611 : if (ws->bucket[i].count > 0)
1274 246445 : byte_set_size++;
1275 : }
1276 :
1277 : /*
1278 : * Continue collecting count of byte values in buckets. If the byte
1279 : * set size is bigger then the threshold, it's pointless to continue,
1280 : * the detection technique would fail for this type of data.
1281 : */
1282 4812920 : for (; i < BUCKET_SIZE; i++) {
1283 4787965 : if (ws->bucket[i].count > 0) {
1284 281 : byte_set_size++;
1285 281 : if (byte_set_size > BYTE_SET_THRESHOLD)
1286 0 : return byte_set_size;
1287 : }
1288 : }
1289 :
1290 : return byte_set_size;
1291 : }
1292 :
1293 158862 : static bool sample_repeated_patterns(struct heuristic_ws *ws)
1294 : {
1295 158862 : const u32 half_of_sample = ws->sample_size / 2;
1296 158862 : const u8 *data = ws->sample;
1297 :
1298 158862 : return memcmp(&data[0], &data[half_of_sample], half_of_sample) == 0;
1299 : }
1300 :
1301 158875 : static void heuristic_collect_sample(struct inode *inode, u64 start, u64 end,
1302 : struct heuristic_ws *ws)
1303 : {
1304 158875 : struct page *page;
1305 158875 : u64 index, index_end;
1306 158875 : u32 i, curr_sample_pos;
1307 158875 : u8 *in_data;
1308 :
1309 : /*
1310 : * Compression handles the input data by chunks of 128KiB
1311 : * (defined by BTRFS_MAX_UNCOMPRESSED)
1312 : *
1313 : * We do the same for the heuristic and loop over the whole range.
1314 : *
1315 : * MAX_SAMPLE_SIZE - calculated under assumption that heuristic will
1316 : * process no more than BTRFS_MAX_UNCOMPRESSED at a time.
1317 : */
1318 158875 : if (end - start > BTRFS_MAX_UNCOMPRESSED)
1319 118718 : end = start + BTRFS_MAX_UNCOMPRESSED;
1320 :
1321 158875 : index = start >> PAGE_SHIFT;
1322 158875 : index_end = end >> PAGE_SHIFT;
1323 :
1324 : /* Don't miss unaligned end */
1325 158875 : if (!PAGE_ALIGNED(end))
1326 40157 : index_end++;
1327 :
1328 158875 : curr_sample_pos = 0;
1329 5224700 : while (index < index_end) {
1330 5065836 : page = find_get_page(inode->i_mapping, index);
1331 5067180 : in_data = kmap_local_page(page);
1332 : /* Handle case where the start is not aligned to PAGE_SIZE */
1333 5067180 : i = start % PAGE_SIZE;
1334 84822321 : while (i < PAGE_SIZE - SAMPLING_READ_SIZE) {
1335 : /* Don't sample any garbage from the last page */
1336 79761581 : if (start > end - SAMPLING_READ_SIZE)
1337 : break;
1338 159516722 : memcpy(&ws->sample[curr_sample_pos], &in_data[i],
1339 : SAMPLING_READ_SIZE);
1340 79755141 : i += SAMPLING_INTERVAL;
1341 79755141 : start += SAMPLING_INTERVAL;
1342 79755141 : curr_sample_pos += SAMPLING_READ_SIZE;
1343 : }
1344 5060740 : kunmap_local(in_data);
1345 5060740 : put_page(page);
1346 :
1347 5065825 : index++;
1348 : }
1349 :
1350 158864 : ws->sample_size = curr_sample_pos;
1351 158864 : }
1352 :
1353 : /*
1354 : * Compression heuristic.
1355 : *
1356 : * For now is's a naive and optimistic 'return true', we'll extend the logic to
1357 : * quickly (compared to direct compression) detect data characteristics
1358 : * (compressible/incompressible) to avoid wasting CPU time on incompressible
1359 : * data.
1360 : *
1361 : * The following types of analysis can be performed:
1362 : * - detect mostly zero data
1363 : * - detect data with low "byte set" size (text, etc)
1364 : * - detect data with low/high "core byte" set
1365 : *
1366 : * Return non-zero if the compression should be done, 0 otherwise.
1367 : */
1368 158696 : int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end)
1369 : {
1370 158696 : struct list_head *ws_list = get_workspace(0, 0);
1371 158875 : struct heuristic_ws *ws;
1372 158875 : u32 i;
1373 158875 : u8 byte;
1374 158875 : int ret = 0;
1375 :
1376 158875 : ws = list_entry(ws_list, struct heuristic_ws, list);
1377 :
1378 158875 : heuristic_collect_sample(inode, start, end, ws);
1379 :
1380 158864 : if (sample_repeated_patterns(ws)) {
1381 133875 : ret = 1;
1382 133875 : goto out;
1383 : }
1384 :
1385 24955 : memset(ws->bucket, 0, sizeof(*ws->bucket)*BUCKET_SIZE);
1386 :
1387 193159981 : for (i = 0; i < ws->sample_size; i++) {
1388 193135026 : byte = ws->sample[i];
1389 193135026 : ws->bucket[byte].count++;
1390 : }
1391 :
1392 24955 : i = byte_set_size(ws);
1393 24955 : if (i < BYTE_SET_THRESHOLD) {
1394 24955 : ret = 2;
1395 24955 : goto out;
1396 : }
1397 :
1398 0 : i = byte_core_set_size(ws);
1399 0 : if (i <= BYTE_CORE_SET_LOW) {
1400 0 : ret = 3;
1401 0 : goto out;
1402 : }
1403 :
1404 0 : if (i >= BYTE_CORE_SET_HIGH) {
1405 0 : ret = 0;
1406 0 : goto out;
1407 : }
1408 :
1409 0 : i = shannon_entropy(ws);
1410 0 : if (i <= ENTROPY_LVL_ACEPTABLE) {
1411 0 : ret = 4;
1412 0 : goto out;
1413 : }
1414 :
1415 : /*
1416 : * For the levels below ENTROPY_LVL_HIGH, additional analysis would be
1417 : * needed to give green light to compression.
1418 : *
1419 : * For now just assume that compression at that level is not worth the
1420 : * resources because:
1421 : *
1422 : * 1. it is possible to defrag the data later
1423 : *
1424 : * 2. the data would turn out to be hardly compressible, eg. 150 byte
1425 : * values, every bucket has counter at level ~54. The heuristic would
1426 : * be confused. This can happen when data have some internal repeated
1427 : * patterns like "abbacbbc...". This can be detected by analyzing
1428 : * pairs of bytes, which is too costly.
1429 : */
1430 0 : if (i < ENTROPY_LVL_HIGH) {
1431 0 : ret = 5;
1432 0 : goto out;
1433 : } else {
1434 0 : ret = 0;
1435 0 : goto out;
1436 : }
1437 :
1438 158830 : out:
1439 158830 : put_workspace(0, ws_list);
1440 158874 : return ret;
1441 : }
1442 :
1443 : /*
1444 : * Convert the compression suffix (eg. after "zlib" starting with ":") to
1445 : * level, unrecognized string will set the default level
1446 : */
1447 20 : unsigned int btrfs_compress_str2level(unsigned int type, const char *str)
1448 : {
1449 20 : unsigned int level = 0;
1450 20 : int ret;
1451 :
1452 20 : if (!type)
1453 : return 0;
1454 :
1455 20 : if (str[0] == ':') {
1456 9 : ret = kstrtouint(str + 1, 10, &level);
1457 9 : if (ret)
1458 0 : level = 0;
1459 : }
1460 :
1461 20 : level = btrfs_compress_set_level(type, level);
1462 :
1463 20 : return level;
1464 : }
|