David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Oracle. All rights reserved. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/bio.h> |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 8 | #include <linux/file.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/pagemap.h> |
| 11 | #include <linux/highmem.h> |
| 12 | #include <linux/time.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/string.h> |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 15 | #include <linux/backing-dev.h> |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 16 | #include <linux/writeback.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David Sterba | fe30853 | 2017-05-31 17:14:56 +0200 | [diff] [blame] | 18 | #include <linux/sched/mm.h> |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 19 | #include <linux/log2.h> |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 20 | #include <crypto/hash.h> |
David Sterba | 602cbe9 | 2019-08-21 18:48:25 +0200 | [diff] [blame] | 21 | #include "misc.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 22 | #include "ctree.h" |
| 23 | #include "disk-io.h" |
| 24 | #include "transaction.h" |
| 25 | #include "btrfs_inode.h" |
| 26 | #include "volumes.h" |
| 27 | #include "ordered-data.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 28 | #include "compression.h" |
| 29 | #include "extent_io.h" |
| 30 | #include "extent_map.h" |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 31 | #include "subpage.h" |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 32 | #include "zoned.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 33 | |
David Sterba | e128f9c | 2017-10-31 17:24:26 +0100 | [diff] [blame] | 34 | static const char* const btrfs_compress_types[] = { "", "zlib", "lzo", "zstd" }; |
| 35 | |
| 36 | const char* btrfs_compress_type2str(enum btrfs_compression_type type) |
| 37 | { |
| 38 | switch (type) { |
| 39 | case BTRFS_COMPRESS_ZLIB: |
| 40 | case BTRFS_COMPRESS_LZO: |
| 41 | case BTRFS_COMPRESS_ZSTD: |
| 42 | case BTRFS_COMPRESS_NONE: |
| 43 | return btrfs_compress_types[type]; |
Chengguang Xu | ce96b7f | 2019-10-10 15:59:57 +0800 | [diff] [blame] | 44 | default: |
| 45 | break; |
David Sterba | e128f9c | 2017-10-31 17:24:26 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Johannes Thumshirn | aa53e3b | 2019-06-06 12:07:15 +0200 | [diff] [blame] | 51 | bool btrfs_compress_is_valid_type(const char *str, size_t len) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | for (i = 1; i < ARRAY_SIZE(btrfs_compress_types); i++) { |
| 56 | size_t comp_len = strlen(btrfs_compress_types[i]); |
| 57 | |
| 58 | if (len < comp_len) |
| 59 | continue; |
| 60 | |
| 61 | if (!strncmp(btrfs_compress_types[i], str, comp_len)) |
| 62 | return true; |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
David Sterba | 1e4eb74 | 2019-10-02 00:06:15 +0200 | [diff] [blame] | 67 | static int compression_compress_pages(int type, struct list_head *ws, |
| 68 | struct address_space *mapping, u64 start, struct page **pages, |
| 69 | unsigned long *out_pages, unsigned long *total_in, |
| 70 | unsigned long *total_out) |
| 71 | { |
| 72 | switch (type) { |
| 73 | case BTRFS_COMPRESS_ZLIB: |
| 74 | return zlib_compress_pages(ws, mapping, start, pages, |
| 75 | out_pages, total_in, total_out); |
| 76 | case BTRFS_COMPRESS_LZO: |
| 77 | return lzo_compress_pages(ws, mapping, start, pages, |
| 78 | out_pages, total_in, total_out); |
| 79 | case BTRFS_COMPRESS_ZSTD: |
| 80 | return zstd_compress_pages(ws, mapping, start, pages, |
| 81 | out_pages, total_in, total_out); |
| 82 | case BTRFS_COMPRESS_NONE: |
| 83 | default: |
| 84 | /* |
Qu Wenruo | 1d8ba9e | 2020-08-04 15:25:47 +0800 | [diff] [blame] | 85 | * This can happen when compression races with remount setting |
| 86 | * it to 'no compress', while caller doesn't call |
| 87 | * inode_need_compress() to check if we really need to |
| 88 | * compress. |
| 89 | * |
| 90 | * Not a big deal, just need to inform caller that we |
| 91 | * haven't allocated any pages yet. |
David Sterba | 1e4eb74 | 2019-10-02 00:06:15 +0200 | [diff] [blame] | 92 | */ |
Qu Wenruo | 1d8ba9e | 2020-08-04 15:25:47 +0800 | [diff] [blame] | 93 | *out_pages = 0; |
David Sterba | 1e4eb74 | 2019-10-02 00:06:15 +0200 | [diff] [blame] | 94 | return -E2BIG; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static int compression_decompress_bio(int type, struct list_head *ws, |
| 99 | struct compressed_bio *cb) |
| 100 | { |
| 101 | switch (type) { |
| 102 | case BTRFS_COMPRESS_ZLIB: return zlib_decompress_bio(ws, cb); |
| 103 | case BTRFS_COMPRESS_LZO: return lzo_decompress_bio(ws, cb); |
| 104 | case BTRFS_COMPRESS_ZSTD: return zstd_decompress_bio(ws, cb); |
| 105 | case BTRFS_COMPRESS_NONE: |
| 106 | default: |
| 107 | /* |
| 108 | * This can't happen, the type is validated several times |
| 109 | * before we get here. |
| 110 | */ |
| 111 | BUG(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static int compression_decompress(int type, struct list_head *ws, |
| 116 | unsigned char *data_in, struct page *dest_page, |
| 117 | unsigned long start_byte, size_t srclen, size_t destlen) |
| 118 | { |
| 119 | switch (type) { |
| 120 | case BTRFS_COMPRESS_ZLIB: return zlib_decompress(ws, data_in, dest_page, |
| 121 | start_byte, srclen, destlen); |
| 122 | case BTRFS_COMPRESS_LZO: return lzo_decompress(ws, data_in, dest_page, |
| 123 | start_byte, srclen, destlen); |
| 124 | case BTRFS_COMPRESS_ZSTD: return zstd_decompress(ws, data_in, dest_page, |
| 125 | start_byte, srclen, destlen); |
| 126 | case BTRFS_COMPRESS_NONE: |
| 127 | default: |
| 128 | /* |
| 129 | * This can't happen, the type is validated several times |
| 130 | * before we get here. |
| 131 | */ |
| 132 | BUG(); |
| 133 | } |
| 134 | } |
| 135 | |
Anand Jain | 8140dc3 | 2017-05-26 15:44:58 +0800 | [diff] [blame] | 136 | static int btrfs_decompress_bio(struct compressed_bio *cb); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 137 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 138 | static inline int compressed_bio_size(struct btrfs_fs_info *fs_info, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 139 | unsigned long disk_size) |
| 140 | { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 141 | return sizeof(struct compressed_bio) + |
David Sterba | 713cebf | 2020-06-30 18:04:02 +0200 | [diff] [blame] | 142 | (DIV_ROUND_UP(disk_size, fs_info->sectorsize)) * fs_info->csum_size; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Nikolay Borisov | 5a9472f | 2020-07-02 15:23:34 +0300 | [diff] [blame] | 145 | static int check_compressed_csum(struct btrfs_inode *inode, struct bio *bio, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 146 | u64 disk_start) |
| 147 | { |
Johannes Thumshirn | 10fe6ca | 2019-05-22 10:19:02 +0200 | [diff] [blame] | 148 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 149 | SHASH_DESC_ON_STACK(shash, fs_info->csum_shash); |
David Sterba | 223486c | 2020-07-02 11:27:30 +0200 | [diff] [blame] | 150 | const u32 csum_size = fs_info->csum_size; |
Qu Wenruo | 04d4ba4c | 2021-02-04 15:03:24 +0800 | [diff] [blame] | 151 | const u32 sectorsize = fs_info->sectorsize; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 152 | struct page *page; |
Anand Jain | 1d08ce58 | 2021-05-29 17:48:33 +0800 | [diff] [blame] | 153 | unsigned int i; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 154 | char *kaddr; |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 155 | u8 csum[BTRFS_CSUM_SIZE]; |
Nikolay Borisov | 5a9472f | 2020-07-02 15:23:34 +0300 | [diff] [blame] | 156 | struct compressed_bio *cb = bio->bi_private; |
Johannes Thumshirn | 10fe6ca | 2019-05-22 10:19:02 +0200 | [diff] [blame] | 157 | u8 *cb_sum = cb->sums; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 158 | |
Josef Bacik | 42437a6 | 2020-10-16 11:29:18 -0400 | [diff] [blame] | 159 | if (!fs_info->csum_root || (inode->flags & BTRFS_INODE_NODATASUM)) |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 160 | return 0; |
| 161 | |
Johannes Thumshirn | d517857 | 2019-06-03 16:58:57 +0200 | [diff] [blame] | 162 | shash->tfm = fs_info->csum_shash; |
| 163 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 164 | for (i = 0; i < cb->nr_pages; i++) { |
Qu Wenruo | 04d4ba4c | 2021-02-04 15:03:24 +0800 | [diff] [blame] | 165 | u32 pg_offset; |
| 166 | u32 bytes_left = PAGE_SIZE; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 167 | page = cb->compressed_pages[i]; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 168 | |
Qu Wenruo | 04d4ba4c | 2021-02-04 15:03:24 +0800 | [diff] [blame] | 169 | /* Determine the remaining bytes inside the page first */ |
| 170 | if (i == cb->nr_pages - 1) |
| 171 | bytes_left = cb->compressed_len - i * PAGE_SIZE; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 172 | |
Qu Wenruo | 04d4ba4c | 2021-02-04 15:03:24 +0800 | [diff] [blame] | 173 | /* Hash through the page sector by sector */ |
| 174 | for (pg_offset = 0; pg_offset < bytes_left; |
| 175 | pg_offset += sectorsize) { |
David Sterba | 4c2bf27 | 2021-06-15 17:15:38 +0200 | [diff] [blame] | 176 | kaddr = page_address(page); |
Qu Wenruo | 04d4ba4c | 2021-02-04 15:03:24 +0800 | [diff] [blame] | 177 | crypto_shash_digest(shash, kaddr + pg_offset, |
| 178 | sectorsize, csum); |
Qu Wenruo | 04d4ba4c | 2021-02-04 15:03:24 +0800 | [diff] [blame] | 179 | |
| 180 | if (memcmp(&csum, cb_sum, csum_size) != 0) { |
| 181 | btrfs_print_data_csum_error(inode, disk_start, |
| 182 | csum, cb_sum, cb->mirror_num); |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 183 | if (btrfs_bio(bio)->device) |
Qu Wenruo | 04d4ba4c | 2021-02-04 15:03:24 +0800 | [diff] [blame] | 184 | btrfs_dev_stat_inc_and_print( |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 185 | btrfs_bio(bio)->device, |
Qu Wenruo | 04d4ba4c | 2021-02-04 15:03:24 +0800 | [diff] [blame] | 186 | BTRFS_DEV_STAT_CORRUPTION_ERRS); |
| 187 | return -EIO; |
| 188 | } |
| 189 | cb_sum += csum_size; |
| 190 | disk_start += sectorsize; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 191 | } |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 192 | } |
Nikolay Borisov | 93c4c03 | 2020-07-02 16:46:47 +0300 | [diff] [blame] | 193 | return 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 196 | /* |
| 197 | * Reduce bio and io accounting for a compressed_bio with its corresponding bio. |
| 198 | * |
| 199 | * Return true if there is no pending bio nor io. |
| 200 | * Return false otherwise. |
| 201 | */ |
| 202 | static bool dec_and_test_compressed_bio(struct compressed_bio *cb, struct bio *bio) |
| 203 | { |
| 204 | struct btrfs_fs_info *fs_info = btrfs_sb(cb->inode->i_sb); |
| 205 | unsigned int bi_size = 0; |
| 206 | bool last_io = false; |
| 207 | struct bio_vec *bvec; |
| 208 | struct bvec_iter_all iter_all; |
| 209 | |
| 210 | /* |
| 211 | * At endio time, bi_iter.bi_size doesn't represent the real bio size. |
| 212 | * Thus here we have to iterate through all segments to grab correct |
| 213 | * bio size. |
| 214 | */ |
| 215 | bio_for_each_segment_all(bvec, bio, iter_all) |
| 216 | bi_size += bvec->bv_len; |
| 217 | |
| 218 | if (bio->bi_status) |
| 219 | cb->errors = 1; |
| 220 | |
| 221 | ASSERT(bi_size && bi_size <= cb->compressed_len); |
| 222 | last_io = refcount_sub_and_test(bi_size >> fs_info->sectorsize_bits, |
| 223 | &cb->pending_sectors); |
| 224 | atomic_dec(&cb->pending_bios); |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 225 | /* |
| 226 | * Here we must wake up the possible error handler after all other |
| 227 | * operations on @cb finished, or we can race with |
| 228 | * finish_compressed_bio_*() which may free @cb. |
| 229 | */ |
| 230 | wake_up_var(cb); |
| 231 | |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 232 | return last_io; |
| 233 | } |
| 234 | |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 235 | static void finish_compressed_bio_read(struct compressed_bio *cb, struct bio *bio) |
| 236 | { |
| 237 | unsigned int index; |
| 238 | struct page *page; |
| 239 | |
| 240 | /* Release the compressed pages */ |
| 241 | for (index = 0; index < cb->nr_pages; index++) { |
| 242 | page = cb->compressed_pages[index]; |
| 243 | page->mapping = NULL; |
| 244 | put_page(page); |
| 245 | } |
| 246 | |
| 247 | /* Do io completion on the original bio */ |
| 248 | if (cb->errors) { |
| 249 | bio_io_error(cb->orig_bio); |
| 250 | } else { |
| 251 | struct bio_vec *bvec; |
| 252 | struct bvec_iter_all iter_all; |
| 253 | |
| 254 | ASSERT(bio); |
| 255 | ASSERT(!bio->bi_status); |
| 256 | /* |
| 257 | * We have verified the checksum already, set page checked so |
| 258 | * the end_io handlers know about it |
| 259 | */ |
| 260 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
| 261 | bio_for_each_segment_all(bvec, cb->orig_bio, iter_all) { |
| 262 | u64 bvec_start = page_offset(bvec->bv_page) + |
| 263 | bvec->bv_offset; |
| 264 | |
| 265 | btrfs_page_set_checked(btrfs_sb(cb->inode->i_sb), |
| 266 | bvec->bv_page, bvec_start, |
| 267 | bvec->bv_len); |
| 268 | } |
| 269 | |
| 270 | bio_endio(cb->orig_bio); |
| 271 | } |
| 272 | |
| 273 | /* Finally free the cb struct */ |
| 274 | kfree(cb->compressed_pages); |
| 275 | kfree(cb); |
| 276 | } |
| 277 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 278 | /* when we finish reading compressed pages from the disk, we |
| 279 | * decompress them and then run the bio end_io routines on the |
| 280 | * decompressed pages (in the inode address space). |
| 281 | * |
| 282 | * This allows the checksumming and other IO error handling routines |
| 283 | * to work normally |
| 284 | * |
| 285 | * The compressed pages are freed here, and it must be run |
| 286 | * in process context |
| 287 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 288 | static void end_compressed_bio_read(struct bio *bio) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 289 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 290 | struct compressed_bio *cb = bio->bi_private; |
| 291 | struct inode *inode; |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 292 | unsigned int mirror = btrfs_bio(bio)->mirror_num; |
Liu Bo | e6311f2 | 2017-09-20 17:50:19 -0600 | [diff] [blame] | 293 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 294 | |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 295 | if (!dec_and_test_compressed_bio(cb, bio)) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 296 | goto out; |
| 297 | |
Liu Bo | cf1167d | 2017-09-20 17:50:18 -0600 | [diff] [blame] | 298 | /* |
| 299 | * Record the correct mirror_num in cb->orig_bio so that |
| 300 | * read-repair can work properly. |
| 301 | */ |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 302 | btrfs_bio(cb->orig_bio)->mirror_num = mirror; |
Liu Bo | cf1167d | 2017-09-20 17:50:18 -0600 | [diff] [blame] | 303 | cb->mirror_num = mirror; |
| 304 | |
Liu Bo | e6311f2 | 2017-09-20 17:50:19 -0600 | [diff] [blame] | 305 | /* |
| 306 | * Some IO in this cb have failed, just skip checksum as there |
| 307 | * is no way it could be correct. |
| 308 | */ |
| 309 | if (cb->errors == 1) |
| 310 | goto csum_failed; |
| 311 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 312 | inode = cb->inode; |
Nikolay Borisov | 5a9472f | 2020-07-02 15:23:34 +0300 | [diff] [blame] | 313 | ret = check_compressed_csum(BTRFS_I(inode), bio, |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 314 | bio->bi_iter.bi_sector << 9); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 315 | if (ret) |
| 316 | goto csum_failed; |
| 317 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 318 | /* ok, we're the last bio for this extent, lets start |
| 319 | * the decompression. |
| 320 | */ |
Anand Jain | 8140dc3 | 2017-05-26 15:44:58 +0800 | [diff] [blame] | 321 | ret = btrfs_decompress_bio(cb); |
| 322 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 323 | csum_failed: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 324 | if (ret) |
| 325 | cb->errors = 1; |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 326 | finish_compressed_bio_read(cb, bio); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 327 | out: |
| 328 | bio_put(bio); |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Clear the writeback bits on all of the file |
| 333 | * pages for a compressed write |
| 334 | */ |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 335 | static noinline void end_compressed_writeback(struct inode *inode, |
| 336 | const struct compressed_bio *cb) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 337 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 338 | unsigned long index = cb->start >> PAGE_SHIFT; |
| 339 | unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 340 | struct page *pages[16]; |
| 341 | unsigned long nr_pages = end_index - index + 1; |
| 342 | int i; |
| 343 | int ret; |
| 344 | |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 345 | if (cb->errors) |
| 346 | mapping_set_error(inode->i_mapping, -EIO); |
| 347 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 348 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 349 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 350 | min_t(unsigned long, |
| 351 | nr_pages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 352 | if (ret == 0) { |
| 353 | nr_pages -= 1; |
| 354 | index += 1; |
| 355 | continue; |
| 356 | } |
| 357 | for (i = 0; i < ret; i++) { |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 358 | if (cb->errors) |
| 359 | SetPageError(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 360 | end_page_writeback(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 361 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 362 | } |
| 363 | nr_pages -= ret; |
| 364 | index += ret; |
| 365 | } |
| 366 | /* the inode may be gone now */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 369 | static void finish_compressed_bio_write(struct compressed_bio *cb) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 370 | { |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 371 | struct inode *inode = cb->inode; |
Anand Jain | 1d08ce58 | 2021-05-29 17:48:33 +0800 | [diff] [blame] | 372 | unsigned int index; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 373 | |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 374 | /* |
| 375 | * Ok, we're the last bio for this extent, step one is to call back |
| 376 | * into the FS and do all the end_io operations. |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 377 | */ |
Qu Wenruo | 38a39ac7 | 2021-04-08 20:32:27 +0800 | [diff] [blame] | 378 | btrfs_writepage_endio_finish_ordered(BTRFS_I(inode), NULL, |
Nikolay Borisov | c629732 | 2018-11-08 10:18:08 +0200 | [diff] [blame] | 379 | cb->start, cb->start + cb->len - 1, |
Goldwyn Rodrigues | 240246f | 2021-07-09 11:29:22 -0500 | [diff] [blame] | 380 | !cb->errors); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 381 | |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 382 | end_compressed_writeback(inode, cb); |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 383 | /* Note, our inode could be gone now */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 384 | |
| 385 | /* |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 386 | * Release the compressed pages, these came from alloc_page and |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 387 | * are not attached to the inode at all |
| 388 | */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 389 | for (index = 0; index < cb->nr_pages; index++) { |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 390 | struct page *page = cb->compressed_pages[index]; |
| 391 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 392 | page->mapping = NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 393 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 394 | } |
| 395 | |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 396 | /* Finally free the cb struct */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 397 | kfree(cb->compressed_pages); |
| 398 | kfree(cb); |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Do the cleanup once all the compressed pages hit the disk. This will clear |
| 403 | * writeback on the file pages and free the compressed pages. |
| 404 | * |
| 405 | * This also calls the writeback end hooks for the file pages so that metadata |
| 406 | * and checksums can be updated in the file. |
| 407 | */ |
| 408 | static void end_compressed_bio_write(struct bio *bio) |
| 409 | { |
| 410 | struct compressed_bio *cb = bio->bi_private; |
| 411 | |
| 412 | if (!dec_and_test_compressed_bio(cb, bio)) |
| 413 | goto out; |
| 414 | |
| 415 | btrfs_record_physical_zoned(cb->inode, cb->start, bio); |
| 416 | |
| 417 | finish_compressed_bio_write(cb); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 418 | out: |
| 419 | bio_put(bio); |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * worker function to build and submit bios for previously compressed pages. |
| 424 | * The corresponding pages in the inode should be marked for writeback |
| 425 | * and the compressed pages should have a reference on them for dropping |
| 426 | * when the IO is complete. |
| 427 | * |
| 428 | * This also checksums the file bytes and gets things ready for |
| 429 | * the end io hooks. |
| 430 | */ |
Nikolay Borisov | c7ee181 | 2020-06-03 08:55:16 +0300 | [diff] [blame] | 431 | blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start, |
Anand Jain | 65b5355 | 2021-05-29 17:48:35 +0800 | [diff] [blame] | 432 | unsigned int len, u64 disk_start, |
| 433 | unsigned int compressed_len, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 434 | struct page **compressed_pages, |
Anand Jain | 65b5355 | 2021-05-29 17:48:35 +0800 | [diff] [blame] | 435 | unsigned int nr_pages, |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 436 | unsigned int write_flags, |
| 437 | struct cgroup_subsys_state *blkcg_css) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 438 | { |
Nikolay Borisov | c7ee181 | 2020-06-03 08:55:16 +0300 | [diff] [blame] | 439 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 440 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 441 | struct compressed_bio *cb; |
| 442 | unsigned long bytes_left; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 443 | int pg_index = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 444 | struct page *page; |
| 445 | u64 first_byte = disk_start; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 446 | blk_status_t ret; |
Nikolay Borisov | c7ee181 | 2020-06-03 08:55:16 +0300 | [diff] [blame] | 447 | int skip_sum = inode->flags & BTRFS_INODE_NODATASUM; |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 448 | const bool use_append = btrfs_use_zone_append(inode, disk_start); |
| 449 | const unsigned int bio_op = use_append ? REQ_OP_ZONE_APPEND : REQ_OP_WRITE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 450 | |
Johannes Thumshirn | fdb1e12 | 2018-12-05 15:23:04 +0100 | [diff] [blame] | 451 | WARN_ON(!PAGE_ALIGNED(start)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 452 | cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS); |
Yoshinori Sano | dac97e5 | 2011-02-15 12:01:42 +0000 | [diff] [blame] | 453 | if (!cb) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 454 | return BLK_STS_RESOURCE; |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 455 | atomic_set(&cb->pending_bios, 0); |
| 456 | refcount_set(&cb->pending_sectors, compressed_len >> fs_info->sectorsize_bits); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 457 | cb->errors = 0; |
Nikolay Borisov | c7ee181 | 2020-06-03 08:55:16 +0300 | [diff] [blame] | 458 | cb->inode = &inode->vfs_inode; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 459 | cb->start = start; |
| 460 | cb->len = len; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 461 | cb->mirror_num = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 462 | cb->compressed_pages = compressed_pages; |
| 463 | cb->compressed_len = compressed_len; |
| 464 | cb->orig_bio = NULL; |
| 465 | cb->nr_pages = nr_pages; |
| 466 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 467 | bio = btrfs_bio_alloc(BIO_MAX_VECS); |
Qu Wenruo | cd8e0cc | 2021-09-15 15:17:17 +0800 | [diff] [blame] | 468 | bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT; |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 469 | bio->bi_opf = bio_op | write_flags; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 470 | bio->bi_private = cb; |
| 471 | bio->bi_end_io = end_compressed_bio_write; |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 472 | |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 473 | if (use_append) { |
Johannes Thumshirn | e7ff9e6 | 2021-05-19 00:40:29 +0900 | [diff] [blame] | 474 | struct btrfs_device *device; |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 475 | |
Johannes Thumshirn | e7ff9e6 | 2021-05-19 00:40:29 +0900 | [diff] [blame] | 476 | device = btrfs_zoned_get_device(fs_info, disk_start, PAGE_SIZE); |
| 477 | if (IS_ERR(device)) { |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 478 | kfree(cb); |
| 479 | bio_put(bio); |
| 480 | return BLK_STS_NOTSUPP; |
| 481 | } |
| 482 | |
Johannes Thumshirn | e7ff9e6 | 2021-05-19 00:40:29 +0900 | [diff] [blame] | 483 | bio_set_dev(bio, device->bdev); |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 484 | } |
| 485 | |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 486 | if (blkcg_css) { |
| 487 | bio->bi_opf |= REQ_CGROUP_PUNT; |
Dennis Zhou | 46bcff2b | 2019-12-11 15:20:15 -0800 | [diff] [blame] | 488 | kthread_associate_blkcg(blkcg_css); |
Chris Mason | ec39f76 | 2019-07-10 12:28:17 -0700 | [diff] [blame] | 489 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 490 | |
| 491 | /* create and submit bios for the compressed pages */ |
| 492 | bytes_left = compressed_len; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 493 | for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 494 | int submit = 0; |
Qu Wenruo | 4c80a97 | 2021-05-25 13:52:43 +0800 | [diff] [blame] | 495 | int len = 0; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 496 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 497 | page = compressed_pages[pg_index]; |
Nikolay Borisov | c7ee181 | 2020-06-03 08:55:16 +0300 | [diff] [blame] | 498 | page->mapping = inode->vfs_inode.i_mapping; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 499 | if (bio->bi_iter.bi_size) |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 500 | submit = btrfs_bio_fits_in_stripe(page, PAGE_SIZE, bio, |
| 501 | 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 502 | |
Qu Wenruo | 4c80a97 | 2021-05-25 13:52:43 +0800 | [diff] [blame] | 503 | /* |
| 504 | * Page can only be added to bio if the current bio fits in |
| 505 | * stripe. |
| 506 | */ |
| 507 | if (!submit) { |
| 508 | if (pg_index == 0 && use_append) |
| 509 | len = bio_add_zone_append_page(bio, page, |
| 510 | PAGE_SIZE, 0); |
| 511 | else |
| 512 | len = bio_add_page(bio, page, PAGE_SIZE, 0); |
| 513 | } |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 514 | |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 515 | page->mapping = NULL; |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 516 | if (submit || len < PAGE_SIZE) { |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 517 | atomic_inc(&cb->pending_bios); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 518 | ret = btrfs_bio_wq_end_io(fs_info, bio, |
| 519 | BTRFS_WQ_ENDIO_DATA); |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 520 | if (ret) |
| 521 | goto finish_cb; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 522 | |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 523 | if (!skip_sum) { |
Nikolay Borisov | c7ee181 | 2020-06-03 08:55:16 +0300 | [diff] [blame] | 524 | ret = btrfs_csum_one_bio(inode, bio, start, 1); |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 525 | if (ret) |
| 526 | goto finish_cb; |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 527 | } |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 528 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 529 | ret = btrfs_map_bio(fs_info, bio, 0); |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 530 | if (ret) |
| 531 | goto finish_cb; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 532 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 533 | bio = btrfs_bio_alloc(BIO_MAX_VECS); |
Qu Wenruo | cd8e0cc | 2021-09-15 15:17:17 +0800 | [diff] [blame] | 534 | bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT; |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 535 | bio->bi_opf = bio_op | write_flags; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 536 | bio->bi_private = cb; |
| 537 | bio->bi_end_io = end_compressed_bio_write; |
Dennis Zhou | 46bcff2b | 2019-12-11 15:20:15 -0800 | [diff] [blame] | 538 | if (blkcg_css) |
Dennis Zhou | 7b62e66 | 2019-12-11 16:07:06 -0800 | [diff] [blame] | 539 | bio->bi_opf |= REQ_CGROUP_PUNT; |
Johannes Thumshirn | 764c7c9 | 2021-05-19 00:40:28 +0900 | [diff] [blame] | 540 | /* |
| 541 | * Use bio_add_page() to ensure the bio has at least one |
| 542 | * page. |
| 543 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 544 | bio_add_page(bio, page, PAGE_SIZE, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 545 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 546 | if (bytes_left < PAGE_SIZE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 547 | btrfs_info(fs_info, |
David Sterba | 282ab3f | 2019-10-14 14:38:33 +0200 | [diff] [blame] | 548 | "bytes left %lu compress len %u nr %u", |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 549 | bytes_left, cb->compressed_len, cb->nr_pages); |
| 550 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 551 | bytes_left -= PAGE_SIZE; |
| 552 | first_byte += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 553 | cond_resched(); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 554 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 555 | |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 556 | atomic_inc(&cb->pending_bios); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 557 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 558 | if (ret) |
| 559 | goto last_bio; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 560 | |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 561 | if (!skip_sum) { |
Nikolay Borisov | c7ee181 | 2020-06-03 08:55:16 +0300 | [diff] [blame] | 562 | ret = btrfs_csum_one_bio(inode, bio, start, 1); |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 563 | if (ret) |
| 564 | goto last_bio; |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 565 | } |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 566 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 567 | ret = btrfs_map_bio(fs_info, bio, 0); |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 568 | if (ret) |
| 569 | goto last_bio; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 570 | |
Dennis Zhou | 46bcff2b | 2019-12-11 15:20:15 -0800 | [diff] [blame] | 571 | if (blkcg_css) |
| 572 | kthread_associate_blkcg(NULL); |
| 573 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 574 | return 0; |
Qu Wenruo | 6853c64 | 2021-09-27 15:21:51 +0800 | [diff] [blame^] | 575 | last_bio: |
| 576 | bio->bi_status = ret; |
| 577 | /* One of the bios' endio function will free @cb. */ |
| 578 | bio_endio(bio); |
| 579 | return ret; |
| 580 | |
| 581 | finish_cb: |
| 582 | if (bio) { |
| 583 | bio->bi_status = ret; |
| 584 | bio_endio(bio); |
| 585 | } |
| 586 | |
| 587 | wait_var_event(cb, atomic_read(&cb->pending_bios) == 0); |
| 588 | /* |
| 589 | * Even with previous bio ended, we should still have io not yet |
| 590 | * submitted, thus need to finish manually. |
| 591 | */ |
| 592 | ASSERT(refcount_read(&cb->pending_sectors)); |
| 593 | /* Now we are the only one referring @cb, can finish it safely. */ |
| 594 | finish_compressed_bio_write(cb); |
| 595 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 596 | } |
| 597 | |
Christoph Hellwig | 2a4d0c9 | 2016-11-25 09:07:51 +0100 | [diff] [blame] | 598 | static u64 bio_end_offset(struct bio *bio) |
| 599 | { |
Ming Lei | c45a8f2 | 2017-12-18 20:22:05 +0800 | [diff] [blame] | 600 | struct bio_vec *last = bio_last_bvec_all(bio); |
Christoph Hellwig | 2a4d0c9 | 2016-11-25 09:07:51 +0100 | [diff] [blame] | 601 | |
| 602 | return page_offset(last->bv_page) + last->bv_len + last->bv_offset; |
| 603 | } |
| 604 | |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 605 | /* |
| 606 | * Add extra pages in the same compressed file extent so that we don't need to |
| 607 | * re-read the same extent again and again. |
| 608 | * |
| 609 | * NOTE: this won't work well for subpage, as for subpage read, we lock the |
| 610 | * full page then submit bio for each compressed/regular extents. |
| 611 | * |
| 612 | * This means, if we have several sectors in the same page points to the same |
| 613 | * on-disk compressed data, we will re-read the same extent many times and |
| 614 | * this function can only help for the next page. |
| 615 | */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 616 | static noinline int add_ra_bio_pages(struct inode *inode, |
| 617 | u64 compressed_end, |
| 618 | struct compressed_bio *cb) |
| 619 | { |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 620 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 621 | unsigned long end_index; |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 622 | u64 cur = bio_end_offset(cb->orig_bio); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 623 | u64 isize = i_size_read(inode); |
| 624 | int ret; |
| 625 | struct page *page; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 626 | struct extent_map *em; |
| 627 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 628 | struct extent_map_tree *em_tree; |
| 629 | struct extent_io_tree *tree; |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 630 | int sectors_missed = 0; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 631 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 632 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 633 | tree = &BTRFS_I(inode)->io_tree; |
| 634 | |
| 635 | if (isize == 0) |
| 636 | return 0; |
| 637 | |
Qu Wenruo | ca62e85 | 2021-07-26 14:34:52 +0800 | [diff] [blame] | 638 | /* |
| 639 | * For current subpage support, we only support 64K page size, |
| 640 | * which means maximum compressed extent size (128K) is just 2x page |
| 641 | * size. |
| 642 | * This makes readahead less effective, so here disable readahead for |
| 643 | * subpage for now, until full compressed write is supported. |
| 644 | */ |
| 645 | if (btrfs_sb(inode->i_sb)->sectorsize < PAGE_SIZE) |
| 646 | return 0; |
| 647 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 648 | end_index = (i_size_read(inode) - 1) >> PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 649 | |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 650 | while (cur < compressed_end) { |
| 651 | u64 page_end; |
| 652 | u64 pg_index = cur >> PAGE_SHIFT; |
| 653 | u32 add_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 654 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 655 | if (pg_index > end_index) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 656 | break; |
| 657 | |
Matthew Wilcox | 0a943c6 | 2017-12-04 10:37:22 -0500 | [diff] [blame] | 658 | page = xa_load(&mapping->i_pages, pg_index); |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 659 | if (page && !xa_is_value(page)) { |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 660 | sectors_missed += (PAGE_SIZE - offset_in_page(cur)) >> |
| 661 | fs_info->sectorsize_bits; |
| 662 | |
| 663 | /* Beyond threshold, no need to continue */ |
| 664 | if (sectors_missed > 4) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 665 | break; |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 666 | |
| 667 | /* |
| 668 | * Jump to next page start as we already have page for |
| 669 | * current offset. |
| 670 | */ |
| 671 | cur = (pg_index << PAGE_SHIFT) + PAGE_SIZE; |
| 672 | continue; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 673 | } |
| 674 | |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 675 | page = __page_cache_alloc(mapping_gfp_constraint(mapping, |
| 676 | ~__GFP_FS)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 677 | if (!page) |
| 678 | break; |
| 679 | |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 680 | if (add_to_page_cache_lru(page, mapping, pg_index, GFP_NOFS)) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 681 | put_page(page); |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 682 | /* There is already a page, skip to page end */ |
| 683 | cur = (pg_index << PAGE_SHIFT) + PAGE_SIZE; |
| 684 | continue; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 685 | } |
| 686 | |
Qu Wenruo | 32443de | 2021-01-26 16:34:00 +0800 | [diff] [blame] | 687 | ret = set_page_extent_mapped(page); |
| 688 | if (ret < 0) { |
| 689 | unlock_page(page); |
| 690 | put_page(page); |
| 691 | break; |
| 692 | } |
| 693 | |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 694 | page_end = (pg_index << PAGE_SHIFT) + PAGE_SIZE - 1; |
| 695 | lock_extent(tree, cur, page_end); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 696 | read_lock(&em_tree->lock); |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 697 | em = lookup_extent_mapping(em_tree, cur, page_end + 1 - cur); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 698 | read_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 699 | |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 700 | /* |
| 701 | * At this point, we have a locked page in the page cache for |
| 702 | * these bytes in the file. But, we have to make sure they map |
| 703 | * to this compressed extent on disk. |
| 704 | */ |
| 705 | if (!em || cur < em->start || |
| 706 | (cur + fs_info->sectorsize > extent_map_end(em)) || |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 707 | (em->block_start >> 9) != cb->orig_bio->bi_iter.bi_sector) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 708 | free_extent_map(em); |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 709 | unlock_extent(tree, cur, page_end); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 710 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 711 | put_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 712 | break; |
| 713 | } |
| 714 | free_extent_map(em); |
| 715 | |
| 716 | if (page->index == end_index) { |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 717 | size_t zero_offset = offset_in_page(isize); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 718 | |
| 719 | if (zero_offset) { |
| 720 | int zeros; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 721 | zeros = PAGE_SIZE - zero_offset; |
Ira Weiny | d048b9c | 2021-05-04 18:40:07 -0700 | [diff] [blame] | 722 | memzero_page(page, zero_offset, zeros); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 723 | flush_dcache_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 727 | add_size = min(em->start + em->len, page_end + 1) - cur; |
| 728 | ret = bio_add_page(cb->orig_bio, page, add_size, offset_in_page(cur)); |
| 729 | if (ret != add_size) { |
| 730 | unlock_extent(tree, cur, page_end); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 731 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 732 | put_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 733 | break; |
| 734 | } |
Qu Wenruo | 6a40491 | 2021-09-27 15:21:47 +0800 | [diff] [blame] | 735 | /* |
| 736 | * If it's subpage, we also need to increase its |
| 737 | * subpage::readers number, as at endio we will decrease |
| 738 | * subpage::readers and to unlock the page. |
| 739 | */ |
| 740 | if (fs_info->sectorsize < PAGE_SIZE) |
| 741 | btrfs_subpage_start_reader(fs_info, page, cur, add_size); |
| 742 | put_page(page); |
| 743 | cur += add_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 744 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 748 | /* |
| 749 | * for a compressed read, the bio we get passed has all the inode pages |
| 750 | * in it. We don't actually do IO on those pages but allocate new ones |
| 751 | * to hold the compressed pages on disk. |
| 752 | * |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 753 | * bio->bi_iter.bi_sector points to the compressed extent on disk |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 754 | * bio->bi_io_vec points to all of the inode pages |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 755 | * |
| 756 | * After the compressed pages are read, we copy the bytes into the |
| 757 | * bio we were passed and then call the bio end_io calls |
| 758 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 759 | blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 760 | int mirror_num, unsigned long bio_flags) |
| 761 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 762 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 763 | struct extent_map_tree *em_tree; |
| 764 | struct compressed_bio *cb; |
Anand Jain | 356b4a2 | 2021-05-29 17:48:34 +0800 | [diff] [blame] | 765 | unsigned int compressed_len; |
| 766 | unsigned int nr_pages; |
| 767 | unsigned int pg_index; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 768 | struct page *page; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 769 | struct bio *comp_bio; |
David Sterba | 1201b58 | 2020-11-26 15:41:27 +0100 | [diff] [blame] | 770 | u64 cur_disk_byte = bio->bi_iter.bi_sector << 9; |
Qu Wenruo | 557023e | 2021-07-05 10:00:56 +0800 | [diff] [blame] | 771 | u64 file_offset; |
Chris Mason | e04ca62 | 2008-11-10 11:44:58 -0500 | [diff] [blame] | 772 | u64 em_len; |
| 773 | u64 em_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 774 | struct extent_map *em; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 775 | blk_status_t ret = BLK_STS_RESOURCE; |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 776 | int faili = 0; |
Johannes Thumshirn | 10fe6ca | 2019-05-22 10:19:02 +0200 | [diff] [blame] | 777 | u8 *sums; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 778 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 779 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 780 | |
Qu Wenruo | 557023e | 2021-07-05 10:00:56 +0800 | [diff] [blame] | 781 | file_offset = bio_first_bvec_all(bio)->bv_offset + |
| 782 | page_offset(bio_first_page_all(bio)); |
| 783 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 784 | /* we need the actual starting offset of this extent in the file */ |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 785 | read_lock(&em_tree->lock); |
Qu Wenruo | 557023e | 2021-07-05 10:00:56 +0800 | [diff] [blame] | 786 | em = lookup_extent_mapping(em_tree, file_offset, fs_info->sectorsize); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 787 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 788 | if (!em) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 789 | return BLK_STS_IOERR; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 790 | |
Qu Wenruo | 557023e | 2021-07-05 10:00:56 +0800 | [diff] [blame] | 791 | ASSERT(em->compress_type != BTRFS_COMPRESS_NONE); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 792 | compressed_len = em->block_len; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 793 | cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS); |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 794 | if (!cb) |
| 795 | goto out; |
| 796 | |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 797 | atomic_set(&cb->pending_bios, 0); |
| 798 | refcount_set(&cb->pending_sectors, compressed_len >> fs_info->sectorsize_bits); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 799 | cb->errors = 0; |
| 800 | cb->inode = inode; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 801 | cb->mirror_num = mirror_num; |
Johannes Thumshirn | 10fe6ca | 2019-05-22 10:19:02 +0200 | [diff] [blame] | 802 | sums = cb->sums; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 803 | |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 804 | cb->start = em->orig_start; |
Chris Mason | e04ca62 | 2008-11-10 11:44:58 -0500 | [diff] [blame] | 805 | em_len = em->len; |
| 806 | em_start = em->start; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 807 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 808 | free_extent_map(em); |
Chris Mason | e04ca62 | 2008-11-10 11:44:58 -0500 | [diff] [blame] | 809 | em = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 810 | |
Christoph Hellwig | 8138105 | 2016-11-25 09:07:50 +0100 | [diff] [blame] | 811 | cb->len = bio->bi_iter.bi_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 812 | cb->compressed_len = compressed_len; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 813 | cb->compress_type = extent_compress_type(bio_flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 814 | cb->orig_bio = bio; |
| 815 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 816 | nr_pages = DIV_ROUND_UP(compressed_len, PAGE_SIZE); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 817 | cb->compressed_pages = kcalloc(nr_pages, sizeof(struct page *), |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 818 | GFP_NOFS); |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 819 | if (!cb->compressed_pages) |
| 820 | goto fail1; |
| 821 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 822 | for (pg_index = 0; pg_index < nr_pages; pg_index++) { |
David Sterba | b0ee5e1 | 2021-06-14 22:22:22 +0200 | [diff] [blame] | 823 | cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS); |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 824 | if (!cb->compressed_pages[pg_index]) { |
| 825 | faili = pg_index - 1; |
Dan Carpenter | 0e9350d | 2017-06-19 13:55:37 +0300 | [diff] [blame] | 826 | ret = BLK_STS_RESOURCE; |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 827 | goto fail2; |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 828 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 829 | } |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 830 | faili = nr_pages - 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 831 | cb->nr_pages = nr_pages; |
| 832 | |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 833 | add_ra_bio_pages(inode, em_start + em_len, cb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 834 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 835 | /* include any pages we added in add_ra-bio_pages */ |
Christoph Hellwig | 8138105 | 2016-11-25 09:07:50 +0100 | [diff] [blame] | 836 | cb->len = bio->bi_iter.bi_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 837 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 838 | comp_bio = btrfs_bio_alloc(BIO_MAX_VECS); |
Qu Wenruo | cd8e0cc | 2021-09-15 15:17:17 +0800 | [diff] [blame] | 839 | comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT; |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 840 | comp_bio->bi_opf = REQ_OP_READ; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 841 | comp_bio->bi_private = cb; |
| 842 | comp_bio->bi_end_io = end_compressed_bio_read; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 843 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 844 | for (pg_index = 0; pg_index < nr_pages; pg_index++) { |
Qu Wenruo | be6a136 | 2021-02-04 15:03:23 +0800 | [diff] [blame] | 845 | u32 pg_len = PAGE_SIZE; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 846 | int submit = 0; |
| 847 | |
Qu Wenruo | be6a136 | 2021-02-04 15:03:23 +0800 | [diff] [blame] | 848 | /* |
| 849 | * To handle subpage case, we need to make sure the bio only |
| 850 | * covers the range we need. |
| 851 | * |
| 852 | * If we're at the last page, truncate the length to only cover |
| 853 | * the remaining part. |
| 854 | */ |
| 855 | if (pg_index == nr_pages - 1) |
| 856 | pg_len = min_t(u32, PAGE_SIZE, |
| 857 | compressed_len - pg_index * PAGE_SIZE); |
| 858 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 859 | page = cb->compressed_pages[pg_index]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 860 | page->mapping = inode->i_mapping; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 861 | page->index = em_start >> PAGE_SHIFT; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 862 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 863 | if (comp_bio->bi_iter.bi_size) |
Qu Wenruo | be6a136 | 2021-02-04 15:03:23 +0800 | [diff] [blame] | 864 | submit = btrfs_bio_fits_in_stripe(page, pg_len, |
Nikolay Borisov | da12fe5 | 2018-11-27 20:57:58 +0200 | [diff] [blame] | 865 | comp_bio, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 866 | |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 867 | page->mapping = NULL; |
Qu Wenruo | be6a136 | 2021-02-04 15:03:23 +0800 | [diff] [blame] | 868 | if (submit || bio_add_page(comp_bio, page, pg_len, 0) < pg_len) { |
Johannes Thumshirn | 10fe6ca | 2019-05-22 10:19:02 +0200 | [diff] [blame] | 869 | unsigned int nr_sectors; |
| 870 | |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 871 | atomic_inc(&cb->pending_bios); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 872 | ret = btrfs_bio_wq_end_io(fs_info, comp_bio, |
| 873 | BTRFS_WQ_ENDIO_DATA); |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 874 | if (ret) |
| 875 | goto finish_cb; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 876 | |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 877 | ret = btrfs_lookup_bio_sums(inode, comp_bio, sums); |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 878 | if (ret) |
| 879 | goto finish_cb; |
Johannes Thumshirn | 10fe6ca | 2019-05-22 10:19:02 +0200 | [diff] [blame] | 880 | |
| 881 | nr_sectors = DIV_ROUND_UP(comp_bio->bi_iter.bi_size, |
| 882 | fs_info->sectorsize); |
David Sterba | 713cebf | 2020-06-30 18:04:02 +0200 | [diff] [blame] | 883 | sums += fs_info->csum_size * nr_sectors; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 884 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 885 | ret = btrfs_map_bio(fs_info, comp_bio, mirror_num); |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 886 | if (ret) |
| 887 | goto finish_cb; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 888 | |
Qu Wenruo | c3a3b19 | 2021-09-15 15:17:18 +0800 | [diff] [blame] | 889 | comp_bio = btrfs_bio_alloc(BIO_MAX_VECS); |
Qu Wenruo | cd8e0cc | 2021-09-15 15:17:17 +0800 | [diff] [blame] | 890 | comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT; |
David Sterba | ebcc326 | 2018-06-29 10:56:53 +0200 | [diff] [blame] | 891 | comp_bio->bi_opf = REQ_OP_READ; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 892 | comp_bio->bi_private = cb; |
| 893 | comp_bio->bi_end_io = end_compressed_bio_read; |
| 894 | |
Qu Wenruo | be6a136 | 2021-02-04 15:03:23 +0800 | [diff] [blame] | 895 | bio_add_page(comp_bio, page, pg_len, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 896 | } |
Qu Wenruo | be6a136 | 2021-02-04 15:03:23 +0800 | [diff] [blame] | 897 | cur_disk_byte += pg_len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 898 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 899 | |
Qu Wenruo | 6ec9765 | 2021-09-27 15:21:48 +0800 | [diff] [blame] | 900 | atomic_inc(&cb->pending_bios); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 901 | ret = btrfs_bio_wq_end_io(fs_info, comp_bio, BTRFS_WQ_ENDIO_DATA); |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 902 | if (ret) |
| 903 | goto last_bio; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 904 | |
Qu Wenruo | 6275193 | 2020-12-02 14:48:06 +0800 | [diff] [blame] | 905 | ret = btrfs_lookup_bio_sums(inode, comp_bio, sums); |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 906 | if (ret) |
| 907 | goto last_bio; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 908 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 909 | ret = btrfs_map_bio(fs_info, comp_bio, mirror_num); |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 910 | if (ret) |
| 911 | goto last_bio; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 912 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 913 | return 0; |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 914 | |
| 915 | fail2: |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 916 | while (faili >= 0) { |
| 917 | __free_page(cb->compressed_pages[faili]); |
| 918 | faili--; |
| 919 | } |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 920 | |
| 921 | kfree(cb->compressed_pages); |
| 922 | fail1: |
| 923 | kfree(cb); |
| 924 | out: |
| 925 | free_extent_map(em); |
| 926 | return ret; |
Qu Wenruo | 86ccbb4 | 2021-09-27 15:21:50 +0800 | [diff] [blame] | 927 | last_bio: |
| 928 | comp_bio->bi_status = ret; |
| 929 | /* This is the last bio, endio functions will free @cb */ |
| 930 | bio_endio(comp_bio); |
| 931 | return ret; |
| 932 | |
| 933 | finish_cb: |
| 934 | if (comp_bio) { |
| 935 | comp_bio->bi_status = ret; |
| 936 | bio_endio(comp_bio); |
| 937 | } |
| 938 | wait_var_event(cb, atomic_read(&cb->pending_bios) == 0); |
| 939 | /* |
| 940 | * Even with previous bio ended, we should still have io not yet |
| 941 | * submitted, thus need to finish @cb manually. |
| 942 | */ |
| 943 | ASSERT(refcount_read(&cb->pending_sectors)); |
| 944 | /* Now we are the only one referring @cb, can finish it safely. */ |
| 945 | finish_compressed_bio_read(cb, NULL); |
| 946 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 947 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 948 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 949 | /* |
| 950 | * Heuristic uses systematic sampling to collect data from the input data |
| 951 | * range, the logic can be tuned by the following constants: |
| 952 | * |
| 953 | * @SAMPLING_READ_SIZE - how many bytes will be copied from for each sample |
| 954 | * @SAMPLING_INTERVAL - range from which the sampled data can be collected |
| 955 | */ |
| 956 | #define SAMPLING_READ_SIZE (16) |
| 957 | #define SAMPLING_INTERVAL (256) |
| 958 | |
| 959 | /* |
| 960 | * For statistical analysis of the input data we consider bytes that form a |
| 961 | * Galois Field of 256 objects. Each object has an attribute count, ie. how |
| 962 | * many times the object appeared in the sample. |
| 963 | */ |
| 964 | #define BUCKET_SIZE (256) |
| 965 | |
| 966 | /* |
| 967 | * The size of the sample is based on a statistical sampling rule of thumb. |
| 968 | * The common way is to perform sampling tests as long as the number of |
| 969 | * elements in each cell is at least 5. |
| 970 | * |
| 971 | * Instead of 5, we choose 32 to obtain more accurate results. |
| 972 | * If the data contain the maximum number of symbols, which is 256, we obtain a |
| 973 | * sample size bound by 8192. |
| 974 | * |
| 975 | * For a sample of at most 8KB of data per data range: 16 consecutive bytes |
| 976 | * from up to 512 locations. |
| 977 | */ |
| 978 | #define MAX_SAMPLE_SIZE (BTRFS_MAX_UNCOMPRESSED * \ |
| 979 | SAMPLING_READ_SIZE / SAMPLING_INTERVAL) |
| 980 | |
| 981 | struct bucket_item { |
| 982 | u32 count; |
| 983 | }; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 984 | |
| 985 | struct heuristic_ws { |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 986 | /* Partial copy of input data */ |
| 987 | u8 *sample; |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 988 | u32 sample_size; |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 989 | /* Buckets store counters for each byte value */ |
| 990 | struct bucket_item *bucket; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 991 | /* Sorting buffer */ |
| 992 | struct bucket_item *bucket_b; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 993 | struct list_head list; |
| 994 | }; |
| 995 | |
Dennis Zhou | 92ee5530 | 2019-02-04 15:20:03 -0500 | [diff] [blame] | 996 | static struct workspace_manager heuristic_wsm; |
| 997 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 998 | static void free_heuristic_ws(struct list_head *ws) |
| 999 | { |
| 1000 | struct heuristic_ws *workspace; |
| 1001 | |
| 1002 | workspace = list_entry(ws, struct heuristic_ws, list); |
| 1003 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 1004 | kvfree(workspace->sample); |
| 1005 | kfree(workspace->bucket); |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1006 | kfree(workspace->bucket_b); |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1007 | kfree(workspace); |
| 1008 | } |
| 1009 | |
Dennis Zhou | 7bf4994 | 2019-02-04 15:20:04 -0500 | [diff] [blame] | 1010 | static struct list_head *alloc_heuristic_ws(unsigned int level) |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1011 | { |
| 1012 | struct heuristic_ws *ws; |
| 1013 | |
| 1014 | ws = kzalloc(sizeof(*ws), GFP_KERNEL); |
| 1015 | if (!ws) |
| 1016 | return ERR_PTR(-ENOMEM); |
| 1017 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 1018 | ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL); |
| 1019 | if (!ws->sample) |
| 1020 | goto fail; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1021 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 1022 | ws->bucket = kcalloc(BUCKET_SIZE, sizeof(*ws->bucket), GFP_KERNEL); |
| 1023 | if (!ws->bucket) |
| 1024 | goto fail; |
| 1025 | |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1026 | ws->bucket_b = kcalloc(BUCKET_SIZE, sizeof(*ws->bucket_b), GFP_KERNEL); |
| 1027 | if (!ws->bucket_b) |
| 1028 | goto fail; |
| 1029 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 1030 | INIT_LIST_HEAD(&ws->list); |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1031 | return &ws->list; |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 1032 | fail: |
| 1033 | free_heuristic_ws(&ws->list); |
| 1034 | return ERR_PTR(-ENOMEM); |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1035 | } |
| 1036 | |
Dennis Zhou | ca4ac36 | 2019-02-04 15:19:59 -0500 | [diff] [blame] | 1037 | const struct btrfs_compress_op btrfs_heuristic_compress = { |
David Sterba | be951045 | 2019-10-02 00:53:31 +0200 | [diff] [blame] | 1038 | .workspace_manager = &heuristic_wsm, |
Dennis Zhou | ca4ac36 | 2019-02-04 15:19:59 -0500 | [diff] [blame] | 1039 | }; |
| 1040 | |
David Sterba | e8c9f18 | 2015-01-02 18:23:10 +0100 | [diff] [blame] | 1041 | static const struct btrfs_compress_op * const btrfs_compress_op[] = { |
Dennis Zhou | ca4ac36 | 2019-02-04 15:19:59 -0500 | [diff] [blame] | 1042 | /* The heuristic is represented as compression type 0 */ |
| 1043 | &btrfs_heuristic_compress, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1044 | &btrfs_zlib_compress, |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 1045 | &btrfs_lzo_compress, |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 1046 | &btrfs_zstd_compress, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1047 | }; |
| 1048 | |
David Sterba | c778df1 | 2019-10-04 02:47:39 +0200 | [diff] [blame] | 1049 | static struct list_head *alloc_workspace(int type, unsigned int level) |
| 1050 | { |
| 1051 | switch (type) { |
| 1052 | case BTRFS_COMPRESS_NONE: return alloc_heuristic_ws(level); |
| 1053 | case BTRFS_COMPRESS_ZLIB: return zlib_alloc_workspace(level); |
| 1054 | case BTRFS_COMPRESS_LZO: return lzo_alloc_workspace(level); |
| 1055 | case BTRFS_COMPRESS_ZSTD: return zstd_alloc_workspace(level); |
| 1056 | default: |
| 1057 | /* |
| 1058 | * This can't happen, the type is validated several times |
| 1059 | * before we get here. |
| 1060 | */ |
| 1061 | BUG(); |
| 1062 | } |
| 1063 | } |
| 1064 | |
David Sterba | 1e00235 | 2019-10-04 02:57:22 +0200 | [diff] [blame] | 1065 | static void free_workspace(int type, struct list_head *ws) |
| 1066 | { |
| 1067 | switch (type) { |
| 1068 | case BTRFS_COMPRESS_NONE: return free_heuristic_ws(ws); |
| 1069 | case BTRFS_COMPRESS_ZLIB: return zlib_free_workspace(ws); |
| 1070 | case BTRFS_COMPRESS_LZO: return lzo_free_workspace(ws); |
| 1071 | case BTRFS_COMPRESS_ZSTD: return zstd_free_workspace(ws); |
| 1072 | default: |
| 1073 | /* |
| 1074 | * This can't happen, the type is validated several times |
| 1075 | * before we get here. |
| 1076 | */ |
| 1077 | BUG(); |
| 1078 | } |
| 1079 | } |
| 1080 | |
David Sterba | d551703 | 2019-10-02 01:08:03 +0200 | [diff] [blame] | 1081 | static void btrfs_init_workspace_manager(int type) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1082 | { |
David Sterba | 0cf2521 | 2019-10-04 03:09:55 +0200 | [diff] [blame] | 1083 | struct workspace_manager *wsm; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1084 | struct list_head *workspace; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1085 | |
David Sterba | 0cf2521 | 2019-10-04 03:09:55 +0200 | [diff] [blame] | 1086 | wsm = btrfs_compress_op[type]->workspace_manager; |
Dennis Zhou | 92ee5530 | 2019-02-04 15:20:03 -0500 | [diff] [blame] | 1087 | INIT_LIST_HEAD(&wsm->idle_ws); |
| 1088 | spin_lock_init(&wsm->ws_lock); |
| 1089 | atomic_set(&wsm->total_ws, 0); |
| 1090 | init_waitqueue_head(&wsm->ws_wait); |
David Sterba | f77dd0d | 2016-04-27 02:55:15 +0200 | [diff] [blame] | 1091 | |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1092 | /* |
| 1093 | * Preallocate one workspace for each compression type so we can |
| 1094 | * guarantee forward progress in the worst case |
| 1095 | */ |
David Sterba | c778df1 | 2019-10-04 02:47:39 +0200 | [diff] [blame] | 1096 | workspace = alloc_workspace(type, 0); |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1097 | if (IS_ERR(workspace)) { |
| 1098 | pr_warn( |
| 1099 | "BTRFS: cannot preallocate compression workspace, will try later\n"); |
| 1100 | } else { |
Dennis Zhou | 92ee5530 | 2019-02-04 15:20:03 -0500 | [diff] [blame] | 1101 | atomic_set(&wsm->total_ws, 1); |
| 1102 | wsm->free_ws = 1; |
| 1103 | list_add(workspace, &wsm->idle_ws); |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
David Sterba | 2510307 | 2019-10-02 01:08:03 +0200 | [diff] [blame] | 1107 | static void btrfs_cleanup_workspace_manager(int type) |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1108 | { |
David Sterba | 2dba714 | 2019-10-04 01:40:58 +0200 | [diff] [blame] | 1109 | struct workspace_manager *wsman; |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1110 | struct list_head *ws; |
| 1111 | |
David Sterba | 2dba714 | 2019-10-04 01:40:58 +0200 | [diff] [blame] | 1112 | wsman = btrfs_compress_op[type]->workspace_manager; |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1113 | while (!list_empty(&wsman->idle_ws)) { |
| 1114 | ws = wsman->idle_ws.next; |
| 1115 | list_del(ws); |
David Sterba | 1e00235 | 2019-10-04 02:57:22 +0200 | [diff] [blame] | 1116 | free_workspace(type, ws); |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1117 | atomic_dec(&wsman->total_ws); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1118 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* |
David Sterba | e721e49 | 2016-04-27 02:41:17 +0200 | [diff] [blame] | 1122 | * This finds an available workspace or allocates a new one. |
| 1123 | * If it's not possible to allocate a new one, waits until there's one. |
| 1124 | * Preallocation makes a forward progress guarantees and we do not return |
| 1125 | * errors. |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1126 | */ |
David Sterba | 5907a9b | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1127 | struct list_head *btrfs_get_workspace(int type, unsigned int level) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1128 | { |
David Sterba | 5907a9b | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1129 | struct workspace_manager *wsm; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1130 | struct list_head *workspace; |
| 1131 | int cpus = num_online_cpus(); |
David Sterba | fe30853 | 2017-05-31 17:14:56 +0200 | [diff] [blame] | 1132 | unsigned nofs_flag; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1133 | struct list_head *idle_ws; |
| 1134 | spinlock_t *ws_lock; |
| 1135 | atomic_t *total_ws; |
| 1136 | wait_queue_head_t *ws_wait; |
| 1137 | int *free_ws; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1138 | |
David Sterba | 5907a9b | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1139 | wsm = btrfs_compress_op[type]->workspace_manager; |
Dennis Zhou | 92ee5530 | 2019-02-04 15:20:03 -0500 | [diff] [blame] | 1140 | idle_ws = &wsm->idle_ws; |
| 1141 | ws_lock = &wsm->ws_lock; |
| 1142 | total_ws = &wsm->total_ws; |
| 1143 | ws_wait = &wsm->ws_wait; |
| 1144 | free_ws = &wsm->free_ws; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1145 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1146 | again: |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1147 | spin_lock(ws_lock); |
| 1148 | if (!list_empty(idle_ws)) { |
| 1149 | workspace = idle_ws->next; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1150 | list_del(workspace); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1151 | (*free_ws)--; |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1152 | spin_unlock(ws_lock); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1153 | return workspace; |
| 1154 | |
| 1155 | } |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1156 | if (atomic_read(total_ws) > cpus) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1157 | DEFINE_WAIT(wait); |
| 1158 | |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1159 | spin_unlock(ws_lock); |
| 1160 | prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1161 | if (atomic_read(total_ws) > cpus && !*free_ws) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1162 | schedule(); |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1163 | finish_wait(ws_wait, &wait); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1164 | goto again; |
| 1165 | } |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1166 | atomic_inc(total_ws); |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1167 | spin_unlock(ws_lock); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1168 | |
David Sterba | fe30853 | 2017-05-31 17:14:56 +0200 | [diff] [blame] | 1169 | /* |
| 1170 | * Allocation helpers call vmalloc that can't use GFP_NOFS, so we have |
| 1171 | * to turn it off here because we might get called from the restricted |
| 1172 | * context of btrfs_compress_bio/btrfs_compress_pages |
| 1173 | */ |
| 1174 | nofs_flag = memalloc_nofs_save(); |
David Sterba | c778df1 | 2019-10-04 02:47:39 +0200 | [diff] [blame] | 1175 | workspace = alloc_workspace(type, level); |
David Sterba | fe30853 | 2017-05-31 17:14:56 +0200 | [diff] [blame] | 1176 | memalloc_nofs_restore(nofs_flag); |
| 1177 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1178 | if (IS_ERR(workspace)) { |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1179 | atomic_dec(total_ws); |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1180 | wake_up(ws_wait); |
David Sterba | e721e49 | 2016-04-27 02:41:17 +0200 | [diff] [blame] | 1181 | |
| 1182 | /* |
| 1183 | * Do not return the error but go back to waiting. There's a |
| 1184 | * workspace preallocated for each type and the compression |
| 1185 | * time is bounded so we get to a workspace eventually. This |
| 1186 | * makes our caller's life easier. |
David Sterba | 52356716 | 2016-04-27 03:07:39 +0200 | [diff] [blame] | 1187 | * |
| 1188 | * To prevent silent and low-probability deadlocks (when the |
| 1189 | * initial preallocation fails), check if there are any |
| 1190 | * workspaces at all. |
David Sterba | e721e49 | 2016-04-27 02:41:17 +0200 | [diff] [blame] | 1191 | */ |
David Sterba | 52356716 | 2016-04-27 03:07:39 +0200 | [diff] [blame] | 1192 | if (atomic_read(total_ws) == 0) { |
| 1193 | static DEFINE_RATELIMIT_STATE(_rs, |
| 1194 | /* once per minute */ 60 * HZ, |
| 1195 | /* no burst */ 1); |
| 1196 | |
| 1197 | if (__ratelimit(&_rs)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 1198 | pr_warn("BTRFS: no compression workspaces, low memory, retrying\n"); |
David Sterba | 52356716 | 2016-04-27 03:07:39 +0200 | [diff] [blame] | 1199 | } |
| 1200 | } |
David Sterba | e721e49 | 2016-04-27 02:41:17 +0200 | [diff] [blame] | 1201 | goto again; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1202 | } |
| 1203 | return workspace; |
| 1204 | } |
| 1205 | |
Dennis Zhou | 7bf4994 | 2019-02-04 15:20:04 -0500 | [diff] [blame] | 1206 | static struct list_head *get_workspace(int type, int level) |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1207 | { |
David Sterba | 6a0d127 | 2019-10-04 02:36:16 +0200 | [diff] [blame] | 1208 | switch (type) { |
David Sterba | 5907a9b | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1209 | case BTRFS_COMPRESS_NONE: return btrfs_get_workspace(type, level); |
David Sterba | 6a0d127 | 2019-10-04 02:36:16 +0200 | [diff] [blame] | 1210 | case BTRFS_COMPRESS_ZLIB: return zlib_get_workspace(level); |
David Sterba | 5907a9b | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1211 | case BTRFS_COMPRESS_LZO: return btrfs_get_workspace(type, level); |
David Sterba | 6a0d127 | 2019-10-04 02:36:16 +0200 | [diff] [blame] | 1212 | case BTRFS_COMPRESS_ZSTD: return zstd_get_workspace(level); |
| 1213 | default: |
| 1214 | /* |
| 1215 | * This can't happen, the type is validated several times |
| 1216 | * before we get here. |
| 1217 | */ |
| 1218 | BUG(); |
| 1219 | } |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1220 | } |
| 1221 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1222 | /* |
| 1223 | * put a workspace struct back on the list or free it if we have enough |
| 1224 | * idle ones sitting around |
| 1225 | */ |
David Sterba | a3bbd2a | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1226 | void btrfs_put_workspace(int type, struct list_head *ws) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1227 | { |
David Sterba | a3bbd2a | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1228 | struct workspace_manager *wsm; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1229 | struct list_head *idle_ws; |
| 1230 | spinlock_t *ws_lock; |
| 1231 | atomic_t *total_ws; |
| 1232 | wait_queue_head_t *ws_wait; |
| 1233 | int *free_ws; |
| 1234 | |
David Sterba | a3bbd2a | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1235 | wsm = btrfs_compress_op[type]->workspace_manager; |
Dennis Zhou | 92ee5530 | 2019-02-04 15:20:03 -0500 | [diff] [blame] | 1236 | idle_ws = &wsm->idle_ws; |
| 1237 | ws_lock = &wsm->ws_lock; |
| 1238 | total_ws = &wsm->total_ws; |
| 1239 | ws_wait = &wsm->ws_wait; |
| 1240 | free_ws = &wsm->free_ws; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1241 | |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1242 | spin_lock(ws_lock); |
Nick Terrell | 26b28dc | 2017-06-29 10:57:26 -0700 | [diff] [blame] | 1243 | if (*free_ws <= num_online_cpus()) { |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1244 | list_add(ws, idle_ws); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1245 | (*free_ws)++; |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1246 | spin_unlock(ws_lock); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1247 | goto wake; |
| 1248 | } |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1249 | spin_unlock(ws_lock); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1250 | |
David Sterba | 1e00235 | 2019-10-04 02:57:22 +0200 | [diff] [blame] | 1251 | free_workspace(type, ws); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1252 | atomic_dec(total_ws); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1253 | wake: |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 1254 | cond_wake_up(ws_wait); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1257 | static void put_workspace(int type, struct list_head *ws) |
| 1258 | { |
David Sterba | bd3a528 | 2019-10-04 02:42:03 +0200 | [diff] [blame] | 1259 | switch (type) { |
David Sterba | a3bbd2a | 2019-10-04 02:50:28 +0200 | [diff] [blame] | 1260 | case BTRFS_COMPRESS_NONE: return btrfs_put_workspace(type, ws); |
| 1261 | case BTRFS_COMPRESS_ZLIB: return btrfs_put_workspace(type, ws); |
| 1262 | case BTRFS_COMPRESS_LZO: return btrfs_put_workspace(type, ws); |
David Sterba | bd3a528 | 2019-10-04 02:42:03 +0200 | [diff] [blame] | 1263 | case BTRFS_COMPRESS_ZSTD: return zstd_put_workspace(ws); |
| 1264 | default: |
| 1265 | /* |
| 1266 | * This can't happen, the type is validated several times |
| 1267 | * before we get here. |
| 1268 | */ |
| 1269 | BUG(); |
| 1270 | } |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1271 | } |
| 1272 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1273 | /* |
Anand Jain | adbab64 | 2020-05-11 22:37:51 -0700 | [diff] [blame] | 1274 | * Adjust @level according to the limits of the compression algorithm or |
| 1275 | * fallback to default |
| 1276 | */ |
| 1277 | static unsigned int btrfs_compress_set_level(int type, unsigned level) |
| 1278 | { |
| 1279 | const struct btrfs_compress_op *ops = btrfs_compress_op[type]; |
| 1280 | |
| 1281 | if (level == 0) |
| 1282 | level = ops->default_level; |
| 1283 | else |
| 1284 | level = min(level, ops->max_level); |
| 1285 | |
| 1286 | return level; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1290 | * Given an address space and start and length, compress the bytes into @pages |
| 1291 | * that are allocated on demand. |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1292 | * |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1293 | * @type_level is encoded algorithm and level, where level 0 means whatever |
| 1294 | * default the algorithm chooses and is opaque here; |
| 1295 | * - compression algo are 0-3 |
| 1296 | * - the level are bits 4-7 |
| 1297 | * |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1298 | * @out_pages is an in/out parameter, holds maximum number of pages to allocate |
| 1299 | * and returns number of actually allocated pages |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1300 | * |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1301 | * @total_in is used to return the number of bytes actually read. It |
| 1302 | * may be smaller than the input length if we had to exit early because we |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1303 | * ran out of room in the pages array or because we cross the |
| 1304 | * max_out threshold. |
| 1305 | * |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1306 | * @total_out is an in/out parameter, must be set to the input length and will |
| 1307 | * be also used to return the total number of compressed bytes |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1308 | */ |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1309 | int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1310 | u64 start, struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1311 | unsigned long *out_pages, |
| 1312 | unsigned long *total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 1313 | unsigned long *total_out) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1314 | { |
Dennis Zhou | 1972708 | 2019-02-04 15:19:57 -0500 | [diff] [blame] | 1315 | int type = btrfs_compress_type(type_level); |
Dennis Zhou | 7bf4994 | 2019-02-04 15:20:04 -0500 | [diff] [blame] | 1316 | int level = btrfs_compress_level(type_level); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1317 | struct list_head *workspace; |
| 1318 | int ret; |
| 1319 | |
David Sterba | b0c1fe1 | 2019-08-09 16:49:06 +0200 | [diff] [blame] | 1320 | level = btrfs_compress_set_level(type, level); |
Dennis Zhou | 7bf4994 | 2019-02-04 15:20:04 -0500 | [diff] [blame] | 1321 | workspace = get_workspace(type, level); |
David Sterba | 1e4eb74 | 2019-10-02 00:06:15 +0200 | [diff] [blame] | 1322 | ret = compression_compress_pages(type, workspace, mapping, start, pages, |
| 1323 | out_pages, total_in, total_out); |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1324 | put_workspace(type, workspace); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1325 | return ret; |
| 1326 | } |
| 1327 | |
Anand Jain | 8140dc3 | 2017-05-26 15:44:58 +0800 | [diff] [blame] | 1328 | static int btrfs_decompress_bio(struct compressed_bio *cb) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1329 | { |
| 1330 | struct list_head *workspace; |
| 1331 | int ret; |
Anand Jain | 8140dc3 | 2017-05-26 15:44:58 +0800 | [diff] [blame] | 1332 | int type = cb->compress_type; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1333 | |
Dennis Zhou | 7bf4994 | 2019-02-04 15:20:04 -0500 | [diff] [blame] | 1334 | workspace = get_workspace(type, 0); |
David Sterba | 1e4eb74 | 2019-10-02 00:06:15 +0200 | [diff] [blame] | 1335 | ret = compression_decompress_bio(type, workspace, cb); |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1336 | put_workspace(type, workspace); |
Anand Jain | e1ddce7 | 2017-05-26 15:44:59 +0800 | [diff] [blame] | 1337 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1338 | return ret; |
| 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * a less complex decompression routine. Our compressed data fits in a |
| 1343 | * single page, and we want to read a single page out of it. |
| 1344 | * start_byte tells us the offset into the compressed data we're interested in |
| 1345 | */ |
| 1346 | int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page, |
| 1347 | unsigned long start_byte, size_t srclen, size_t destlen) |
| 1348 | { |
| 1349 | struct list_head *workspace; |
| 1350 | int ret; |
| 1351 | |
Dennis Zhou | 7bf4994 | 2019-02-04 15:20:04 -0500 | [diff] [blame] | 1352 | workspace = get_workspace(type, 0); |
David Sterba | 1e4eb74 | 2019-10-02 00:06:15 +0200 | [diff] [blame] | 1353 | ret = compression_decompress(type, workspace, data_in, dest_page, |
| 1354 | start_byte, srclen, destlen); |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1355 | put_workspace(type, workspace); |
Dennis Zhou | 7bf4994 | 2019-02-04 15:20:04 -0500 | [diff] [blame] | 1356 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1357 | return ret; |
| 1358 | } |
| 1359 | |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1360 | void __init btrfs_init_compress(void) |
| 1361 | { |
David Sterba | d551703 | 2019-10-02 01:08:03 +0200 | [diff] [blame] | 1362 | btrfs_init_workspace_manager(BTRFS_COMPRESS_NONE); |
| 1363 | btrfs_init_workspace_manager(BTRFS_COMPRESS_ZLIB); |
| 1364 | btrfs_init_workspace_manager(BTRFS_COMPRESS_LZO); |
| 1365 | zstd_init_workspace_manager(); |
Dennis Zhou | 1666eda | 2019-02-04 15:20:01 -0500 | [diff] [blame] | 1366 | } |
| 1367 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 1368 | void __cold btrfs_exit_compress(void) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1369 | { |
David Sterba | 2510307 | 2019-10-02 01:08:03 +0200 | [diff] [blame] | 1370 | btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_NONE); |
| 1371 | btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_ZLIB); |
| 1372 | btrfs_cleanup_workspace_manager(BTRFS_COMPRESS_LZO); |
| 1373 | zstd_cleanup_workspace_manager(); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1374 | } |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1375 | |
| 1376 | /* |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1377 | * Copy decompressed data from working buffer to pages. |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1378 | * |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1379 | * @buf: The decompressed data buffer |
| 1380 | * @buf_len: The decompressed data length |
| 1381 | * @decompressed: Number of bytes that are already decompressed inside the |
| 1382 | * compressed extent |
| 1383 | * @cb: The compressed extent descriptor |
| 1384 | * @orig_bio: The original bio that the caller wants to read for |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1385 | * |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1386 | * An easier to understand graph is like below: |
| 1387 | * |
| 1388 | * |<- orig_bio ->| |<- orig_bio->| |
| 1389 | * |<------- full decompressed extent ----->| |
| 1390 | * |<----------- @cb range ---->| |
| 1391 | * | |<-- @buf_len -->| |
| 1392 | * |<--- @decompressed --->| |
| 1393 | * |
| 1394 | * Note that, @cb can be a subpage of the full decompressed extent, but |
| 1395 | * @cb->start always has the same as the orig_file_offset value of the full |
| 1396 | * decompressed extent. |
| 1397 | * |
| 1398 | * When reading compressed extent, we have to read the full compressed extent, |
| 1399 | * while @orig_bio may only want part of the range. |
| 1400 | * Thus this function will ensure only data covered by @orig_bio will be copied |
| 1401 | * to. |
| 1402 | * |
| 1403 | * Return 0 if we have copied all needed contents for @orig_bio. |
| 1404 | * Return >0 if we need continue decompress. |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1405 | */ |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1406 | int btrfs_decompress_buf2page(const char *buf, u32 buf_len, |
| 1407 | struct compressed_bio *cb, u32 decompressed) |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1408 | { |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1409 | struct bio *orig_bio = cb->orig_bio; |
| 1410 | /* Offset inside the full decompressed extent */ |
| 1411 | u32 cur_offset; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1412 | |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1413 | cur_offset = decompressed; |
| 1414 | /* The main loop to do the copy */ |
| 1415 | while (cur_offset < decompressed + buf_len) { |
| 1416 | struct bio_vec bvec; |
| 1417 | size_t copy_len; |
| 1418 | u32 copy_start; |
| 1419 | /* Offset inside the full decompressed extent */ |
| 1420 | u32 bvec_offset; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1421 | |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1422 | bvec = bio_iter_iovec(orig_bio, orig_bio->bi_iter); |
| 1423 | /* |
| 1424 | * cb->start may underflow, but subtracting that value can still |
| 1425 | * give us correct offset inside the full decompressed extent. |
| 1426 | */ |
| 1427 | bvec_offset = page_offset(bvec.bv_page) + bvec.bv_offset - cb->start; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1428 | |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1429 | /* Haven't reached the bvec range, exit */ |
| 1430 | if (decompressed + buf_len <= bvec_offset) |
| 1431 | return 1; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1432 | |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1433 | copy_start = max(cur_offset, bvec_offset); |
| 1434 | copy_len = min(bvec_offset + bvec.bv_len, |
| 1435 | decompressed + buf_len) - copy_start; |
| 1436 | ASSERT(copy_len); |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1437 | |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1438 | /* |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1439 | * Extra range check to ensure we didn't go beyond |
| 1440 | * @buf + @buf_len. |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1441 | */ |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1442 | ASSERT(copy_start - decompressed < buf_len); |
| 1443 | memcpy_to_page(bvec.bv_page, bvec.bv_offset, |
| 1444 | buf + copy_start - decompressed, copy_len); |
| 1445 | flush_dcache_page(bvec.bv_page); |
| 1446 | cur_offset += copy_len; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1447 | |
Qu Wenruo | 1c3dc17 | 2021-07-05 10:00:58 +0800 | [diff] [blame] | 1448 | bio_advance(orig_bio, copy_len); |
| 1449 | /* Finished the bio */ |
| 1450 | if (!orig_bio->bi_iter.bi_size) |
| 1451 | return 0; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1452 | } |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1453 | return 1; |
| 1454 | } |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1455 | |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 1456 | /* |
| 1457 | * Shannon Entropy calculation |
| 1458 | * |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1459 | * Pure byte distribution analysis fails to determine compressibility of data. |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 1460 | * Try calculating entropy to estimate the average minimum number of bits |
| 1461 | * needed to encode the sampled data. |
| 1462 | * |
| 1463 | * For convenience, return the percentage of needed bits, instead of amount of |
| 1464 | * bits directly. |
| 1465 | * |
| 1466 | * @ENTROPY_LVL_ACEPTABLE - below that threshold, sample has low byte entropy |
| 1467 | * and can be compressible with high probability |
| 1468 | * |
| 1469 | * @ENTROPY_LVL_HIGH - data are not compressible with high probability |
| 1470 | * |
| 1471 | * Use of ilog2() decreases precision, we lower the LVL to 5 to compensate. |
| 1472 | */ |
| 1473 | #define ENTROPY_LVL_ACEPTABLE (65) |
| 1474 | #define ENTROPY_LVL_HIGH (80) |
| 1475 | |
| 1476 | /* |
| 1477 | * For increasead precision in shannon_entropy calculation, |
| 1478 | * let's do pow(n, M) to save more digits after comma: |
| 1479 | * |
| 1480 | * - maximum int bit length is 64 |
| 1481 | * - ilog2(MAX_SAMPLE_SIZE) -> 13 |
| 1482 | * - 13 * 4 = 52 < 64 -> M = 4 |
| 1483 | * |
| 1484 | * So use pow(n, 4). |
| 1485 | */ |
| 1486 | static inline u32 ilog2_w(u64 n) |
| 1487 | { |
| 1488 | return ilog2(n * n * n * n); |
| 1489 | } |
| 1490 | |
| 1491 | static u32 shannon_entropy(struct heuristic_ws *ws) |
| 1492 | { |
| 1493 | const u32 entropy_max = 8 * ilog2_w(2); |
| 1494 | u32 entropy_sum = 0; |
| 1495 | u32 p, p_base, sz_base; |
| 1496 | u32 i; |
| 1497 | |
| 1498 | sz_base = ilog2_w(ws->sample_size); |
| 1499 | for (i = 0; i < BUCKET_SIZE && ws->bucket[i].count > 0; i++) { |
| 1500 | p = ws->bucket[i].count; |
| 1501 | p_base = ilog2_w(p); |
| 1502 | entropy_sum += p * (sz_base - p_base); |
| 1503 | } |
| 1504 | |
| 1505 | entropy_sum /= ws->sample_size; |
| 1506 | return entropy_sum * 100 / entropy_max; |
| 1507 | } |
| 1508 | |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1509 | #define RADIX_BASE 4U |
| 1510 | #define COUNTERS_SIZE (1U << RADIX_BASE) |
Timofey Titovets | 858177d | 2017-09-28 17:33:41 +0300 | [diff] [blame] | 1511 | |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1512 | static u8 get4bits(u64 num, int shift) { |
| 1513 | u8 low4bits; |
| 1514 | |
| 1515 | num >>= shift; |
| 1516 | /* Reverse order */ |
| 1517 | low4bits = (COUNTERS_SIZE - 1) - (num % COUNTERS_SIZE); |
| 1518 | return low4bits; |
| 1519 | } |
| 1520 | |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1521 | /* |
| 1522 | * Use 4 bits as radix base |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1523 | * Use 16 u32 counters for calculating new position in buf array |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1524 | * |
| 1525 | * @array - array that will be sorted |
| 1526 | * @array_buf - buffer array to store sorting results |
| 1527 | * must be equal in size to @array |
| 1528 | * @num - array size |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1529 | */ |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1530 | static void radix_sort(struct bucket_item *array, struct bucket_item *array_buf, |
David Sterba | 36243c9 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1531 | int num) |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1532 | { |
| 1533 | u64 max_num; |
| 1534 | u64 buf_num; |
| 1535 | u32 counters[COUNTERS_SIZE]; |
| 1536 | u32 new_addr; |
| 1537 | u32 addr; |
| 1538 | int bitlen; |
| 1539 | int shift; |
| 1540 | int i; |
| 1541 | |
| 1542 | /* |
| 1543 | * Try avoid useless loop iterations for small numbers stored in big |
| 1544 | * counters. Example: 48 33 4 ... in 64bit array |
| 1545 | */ |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1546 | max_num = array[0].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1547 | for (i = 1; i < num; i++) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1548 | buf_num = array[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1549 | if (buf_num > max_num) |
| 1550 | max_num = buf_num; |
| 1551 | } |
| 1552 | |
| 1553 | buf_num = ilog2(max_num); |
| 1554 | bitlen = ALIGN(buf_num, RADIX_BASE * 2); |
| 1555 | |
| 1556 | shift = 0; |
| 1557 | while (shift < bitlen) { |
| 1558 | memset(counters, 0, sizeof(counters)); |
| 1559 | |
| 1560 | for (i = 0; i < num; i++) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1561 | buf_num = array[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1562 | addr = get4bits(buf_num, shift); |
| 1563 | counters[addr]++; |
| 1564 | } |
| 1565 | |
| 1566 | for (i = 1; i < COUNTERS_SIZE; i++) |
| 1567 | counters[i] += counters[i - 1]; |
| 1568 | |
| 1569 | for (i = num - 1; i >= 0; i--) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1570 | buf_num = array[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1571 | addr = get4bits(buf_num, shift); |
| 1572 | counters[addr]--; |
| 1573 | new_addr = counters[addr]; |
David Sterba | 7add17b | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1574 | array_buf[new_addr] = array[i]; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | shift += RADIX_BASE; |
| 1578 | |
| 1579 | /* |
| 1580 | * Normal radix expects to move data from a temporary array, to |
| 1581 | * the main one. But that requires some CPU time. Avoid that |
| 1582 | * by doing another sort iteration to original array instead of |
| 1583 | * memcpy() |
| 1584 | */ |
| 1585 | memset(counters, 0, sizeof(counters)); |
| 1586 | |
| 1587 | for (i = 0; i < num; i ++) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1588 | buf_num = array_buf[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1589 | addr = get4bits(buf_num, shift); |
| 1590 | counters[addr]++; |
| 1591 | } |
| 1592 | |
| 1593 | for (i = 1; i < COUNTERS_SIZE; i++) |
| 1594 | counters[i] += counters[i - 1]; |
| 1595 | |
| 1596 | for (i = num - 1; i >= 0; i--) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1597 | buf_num = array_buf[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1598 | addr = get4bits(buf_num, shift); |
| 1599 | counters[addr]--; |
| 1600 | new_addr = counters[addr]; |
David Sterba | 7add17b | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1601 | array[new_addr] = array_buf[i]; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | shift += RADIX_BASE; |
| 1605 | } |
Timofey Titovets | 858177d | 2017-09-28 17:33:41 +0300 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * Size of the core byte set - how many bytes cover 90% of the sample |
| 1610 | * |
| 1611 | * There are several types of structured binary data that use nearly all byte |
| 1612 | * values. The distribution can be uniform and counts in all buckets will be |
| 1613 | * nearly the same (eg. encrypted data). Unlikely to be compressible. |
| 1614 | * |
| 1615 | * Other possibility is normal (Gaussian) distribution, where the data could |
| 1616 | * be potentially compressible, but we have to take a few more steps to decide |
| 1617 | * how much. |
| 1618 | * |
| 1619 | * @BYTE_CORE_SET_LOW - main part of byte values repeated frequently, |
| 1620 | * compression algo can easy fix that |
| 1621 | * @BYTE_CORE_SET_HIGH - data have uniform distribution and with high |
| 1622 | * probability is not compressible |
| 1623 | */ |
| 1624 | #define BYTE_CORE_SET_LOW (64) |
| 1625 | #define BYTE_CORE_SET_HIGH (200) |
| 1626 | |
| 1627 | static int byte_core_set_size(struct heuristic_ws *ws) |
| 1628 | { |
| 1629 | u32 i; |
| 1630 | u32 coreset_sum = 0; |
| 1631 | const u32 core_set_threshold = ws->sample_size * 90 / 100; |
| 1632 | struct bucket_item *bucket = ws->bucket; |
| 1633 | |
| 1634 | /* Sort in reverse order */ |
David Sterba | 36243c9 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1635 | radix_sort(ws->bucket, ws->bucket_b, BUCKET_SIZE); |
Timofey Titovets | 858177d | 2017-09-28 17:33:41 +0300 | [diff] [blame] | 1636 | |
| 1637 | for (i = 0; i < BYTE_CORE_SET_LOW; i++) |
| 1638 | coreset_sum += bucket[i].count; |
| 1639 | |
| 1640 | if (coreset_sum > core_set_threshold) |
| 1641 | return i; |
| 1642 | |
| 1643 | for (; i < BYTE_CORE_SET_HIGH && bucket[i].count > 0; i++) { |
| 1644 | coreset_sum += bucket[i].count; |
| 1645 | if (coreset_sum > core_set_threshold) |
| 1646 | break; |
| 1647 | } |
| 1648 | |
| 1649 | return i; |
| 1650 | } |
| 1651 | |
Timofey Titovets | a288e92 | 2017-09-28 17:33:40 +0300 | [diff] [blame] | 1652 | /* |
| 1653 | * Count byte values in buckets. |
| 1654 | * This heuristic can detect textual data (configs, xml, json, html, etc). |
| 1655 | * Because in most text-like data byte set is restricted to limited number of |
| 1656 | * possible characters, and that restriction in most cases makes data easy to |
| 1657 | * compress. |
| 1658 | * |
| 1659 | * @BYTE_SET_THRESHOLD - consider all data within this byte set size: |
| 1660 | * less - compressible |
| 1661 | * more - need additional analysis |
| 1662 | */ |
| 1663 | #define BYTE_SET_THRESHOLD (64) |
| 1664 | |
| 1665 | static u32 byte_set_size(const struct heuristic_ws *ws) |
| 1666 | { |
| 1667 | u32 i; |
| 1668 | u32 byte_set_size = 0; |
| 1669 | |
| 1670 | for (i = 0; i < BYTE_SET_THRESHOLD; i++) { |
| 1671 | if (ws->bucket[i].count > 0) |
| 1672 | byte_set_size++; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | * Continue collecting count of byte values in buckets. If the byte |
| 1677 | * set size is bigger then the threshold, it's pointless to continue, |
| 1678 | * the detection technique would fail for this type of data. |
| 1679 | */ |
| 1680 | for (; i < BUCKET_SIZE; i++) { |
| 1681 | if (ws->bucket[i].count > 0) { |
| 1682 | byte_set_size++; |
| 1683 | if (byte_set_size > BYTE_SET_THRESHOLD) |
| 1684 | return byte_set_size; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | return byte_set_size; |
| 1689 | } |
| 1690 | |
Timofey Titovets | 1fe4f6f | 2017-09-28 17:33:39 +0300 | [diff] [blame] | 1691 | static bool sample_repeated_patterns(struct heuristic_ws *ws) |
| 1692 | { |
| 1693 | const u32 half_of_sample = ws->sample_size / 2; |
| 1694 | const u8 *data = ws->sample; |
| 1695 | |
| 1696 | return memcmp(&data[0], &data[half_of_sample], half_of_sample) == 0; |
| 1697 | } |
| 1698 | |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1699 | static void heuristic_collect_sample(struct inode *inode, u64 start, u64 end, |
| 1700 | struct heuristic_ws *ws) |
| 1701 | { |
| 1702 | struct page *page; |
| 1703 | u64 index, index_end; |
| 1704 | u32 i, curr_sample_pos; |
| 1705 | u8 *in_data; |
| 1706 | |
| 1707 | /* |
| 1708 | * Compression handles the input data by chunks of 128KiB |
| 1709 | * (defined by BTRFS_MAX_UNCOMPRESSED) |
| 1710 | * |
| 1711 | * We do the same for the heuristic and loop over the whole range. |
| 1712 | * |
| 1713 | * MAX_SAMPLE_SIZE - calculated under assumption that heuristic will |
| 1714 | * process no more than BTRFS_MAX_UNCOMPRESSED at a time. |
| 1715 | */ |
| 1716 | if (end - start > BTRFS_MAX_UNCOMPRESSED) |
| 1717 | end = start + BTRFS_MAX_UNCOMPRESSED; |
| 1718 | |
| 1719 | index = start >> PAGE_SHIFT; |
| 1720 | index_end = end >> PAGE_SHIFT; |
| 1721 | |
| 1722 | /* Don't miss unaligned end */ |
| 1723 | if (!IS_ALIGNED(end, PAGE_SIZE)) |
| 1724 | index_end++; |
| 1725 | |
| 1726 | curr_sample_pos = 0; |
| 1727 | while (index < index_end) { |
| 1728 | page = find_get_page(inode->i_mapping, index); |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 1729 | in_data = kmap_local_page(page); |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1730 | /* Handle case where the start is not aligned to PAGE_SIZE */ |
| 1731 | i = start % PAGE_SIZE; |
| 1732 | while (i < PAGE_SIZE - SAMPLING_READ_SIZE) { |
| 1733 | /* Don't sample any garbage from the last page */ |
| 1734 | if (start > end - SAMPLING_READ_SIZE) |
| 1735 | break; |
| 1736 | memcpy(&ws->sample[curr_sample_pos], &in_data[i], |
| 1737 | SAMPLING_READ_SIZE); |
| 1738 | i += SAMPLING_INTERVAL; |
| 1739 | start += SAMPLING_INTERVAL; |
| 1740 | curr_sample_pos += SAMPLING_READ_SIZE; |
| 1741 | } |
Ira Weiny | 58c1a35 | 2021-02-16 18:48:23 -0800 | [diff] [blame] | 1742 | kunmap_local(in_data); |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1743 | put_page(page); |
| 1744 | |
| 1745 | index++; |
| 1746 | } |
| 1747 | |
| 1748 | ws->sample_size = curr_sample_pos; |
| 1749 | } |
| 1750 | |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1751 | /* |
| 1752 | * Compression heuristic. |
| 1753 | * |
| 1754 | * For now is's a naive and optimistic 'return true', we'll extend the logic to |
| 1755 | * quickly (compared to direct compression) detect data characteristics |
| 1756 | * (compressible/uncompressible) to avoid wasting CPU time on uncompressible |
| 1757 | * data. |
| 1758 | * |
| 1759 | * The following types of analysis can be performed: |
| 1760 | * - detect mostly zero data |
| 1761 | * - detect data with low "byte set" size (text, etc) |
| 1762 | * - detect data with low/high "core byte" set |
| 1763 | * |
| 1764 | * Return non-zero if the compression should be done, 0 otherwise. |
| 1765 | */ |
| 1766 | int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end) |
| 1767 | { |
Dennis Zhou | 7bf4994 | 2019-02-04 15:20:04 -0500 | [diff] [blame] | 1768 | struct list_head *ws_list = get_workspace(0, 0); |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1769 | struct heuristic_ws *ws; |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1770 | u32 i; |
| 1771 | u8 byte; |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 1772 | int ret = 0; |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1773 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1774 | ws = list_entry(ws_list, struct heuristic_ws, list); |
| 1775 | |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1776 | heuristic_collect_sample(inode, start, end, ws); |
| 1777 | |
Timofey Titovets | 1fe4f6f | 2017-09-28 17:33:39 +0300 | [diff] [blame] | 1778 | if (sample_repeated_patterns(ws)) { |
| 1779 | ret = 1; |
| 1780 | goto out; |
| 1781 | } |
| 1782 | |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1783 | memset(ws->bucket, 0, sizeof(*ws->bucket)*BUCKET_SIZE); |
| 1784 | |
| 1785 | for (i = 0; i < ws->sample_size; i++) { |
| 1786 | byte = ws->sample[i]; |
| 1787 | ws->bucket[byte].count++; |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1788 | } |
| 1789 | |
Timofey Titovets | a288e92 | 2017-09-28 17:33:40 +0300 | [diff] [blame] | 1790 | i = byte_set_size(ws); |
| 1791 | if (i < BYTE_SET_THRESHOLD) { |
| 1792 | ret = 2; |
| 1793 | goto out; |
| 1794 | } |
| 1795 | |
Timofey Titovets | 858177d | 2017-09-28 17:33:41 +0300 | [diff] [blame] | 1796 | i = byte_core_set_size(ws); |
| 1797 | if (i <= BYTE_CORE_SET_LOW) { |
| 1798 | ret = 3; |
| 1799 | goto out; |
| 1800 | } |
| 1801 | |
| 1802 | if (i >= BYTE_CORE_SET_HIGH) { |
| 1803 | ret = 0; |
| 1804 | goto out; |
| 1805 | } |
| 1806 | |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 1807 | i = shannon_entropy(ws); |
| 1808 | if (i <= ENTROPY_LVL_ACEPTABLE) { |
| 1809 | ret = 4; |
| 1810 | goto out; |
| 1811 | } |
| 1812 | |
| 1813 | /* |
| 1814 | * For the levels below ENTROPY_LVL_HIGH, additional analysis would be |
| 1815 | * needed to give green light to compression. |
| 1816 | * |
| 1817 | * For now just assume that compression at that level is not worth the |
| 1818 | * resources because: |
| 1819 | * |
| 1820 | * 1. it is possible to defrag the data later |
| 1821 | * |
| 1822 | * 2. the data would turn out to be hardly compressible, eg. 150 byte |
| 1823 | * values, every bucket has counter at level ~54. The heuristic would |
| 1824 | * be confused. This can happen when data have some internal repeated |
| 1825 | * patterns like "abbacbbc...". This can be detected by analyzing |
| 1826 | * pairs of bytes, which is too costly. |
| 1827 | */ |
| 1828 | if (i < ENTROPY_LVL_HIGH) { |
| 1829 | ret = 5; |
| 1830 | goto out; |
| 1831 | } else { |
| 1832 | ret = 0; |
| 1833 | goto out; |
| 1834 | } |
| 1835 | |
Timofey Titovets | 1fe4f6f | 2017-09-28 17:33:39 +0300 | [diff] [blame] | 1836 | out: |
Dennis Zhou | 929f4ba | 2019-02-04 15:20:02 -0500 | [diff] [blame] | 1837 | put_workspace(0, ws_list); |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1838 | return ret; |
| 1839 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1840 | |
Dennis Zhou | d0ab62c | 2019-02-04 15:20:05 -0500 | [diff] [blame] | 1841 | /* |
| 1842 | * Convert the compression suffix (eg. after "zlib" starting with ":") to |
| 1843 | * level, unrecognized string will set the default level |
| 1844 | */ |
| 1845 | unsigned int btrfs_compress_str2level(unsigned int type, const char *str) |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1846 | { |
Dennis Zhou | d0ab62c | 2019-02-04 15:20:05 -0500 | [diff] [blame] | 1847 | unsigned int level = 0; |
| 1848 | int ret; |
| 1849 | |
| 1850 | if (!type) |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1851 | return 0; |
| 1852 | |
Dennis Zhou | d0ab62c | 2019-02-04 15:20:05 -0500 | [diff] [blame] | 1853 | if (str[0] == ':') { |
| 1854 | ret = kstrtouint(str + 1, 10, &level); |
| 1855 | if (ret) |
| 1856 | level = 0; |
| 1857 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1858 | |
David Sterba | b0c1fe1 | 2019-08-09 16:49:06 +0200 | [diff] [blame] | 1859 | level = btrfs_compress_set_level(type, level); |
| 1860 | |
| 1861 | return level; |
| 1862 | } |