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