Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/bio.h> |
| 21 | #include <linux/buffer_head.h> |
| 22 | #include <linux/file.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/string.h> |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 29 | #include <linux/backing-dev.h> |
| 30 | #include <linux/mpage.h> |
| 31 | #include <linux/swap.h> |
| 32 | #include <linux/writeback.h> |
| 33 | #include <linux/bit_spinlock.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
David Sterba | fe30853 | 2017-05-31 17:14:56 +0200 | [diff] [blame] | 35 | #include <linux/sched/mm.h> |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 36 | #include <linux/log2.h> |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 37 | #include "ctree.h" |
| 38 | #include "disk-io.h" |
| 39 | #include "transaction.h" |
| 40 | #include "btrfs_inode.h" |
| 41 | #include "volumes.h" |
| 42 | #include "ordered-data.h" |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 43 | #include "compression.h" |
| 44 | #include "extent_io.h" |
| 45 | #include "extent_map.h" |
| 46 | |
David Sterba | e128f9c | 2017-10-31 17:24:26 +0100 | [diff] [blame] | 47 | static const char* const btrfs_compress_types[] = { "", "zlib", "lzo", "zstd" }; |
| 48 | |
| 49 | const char* btrfs_compress_type2str(enum btrfs_compression_type type) |
| 50 | { |
| 51 | switch (type) { |
| 52 | case BTRFS_COMPRESS_ZLIB: |
| 53 | case BTRFS_COMPRESS_LZO: |
| 54 | case BTRFS_COMPRESS_ZSTD: |
| 55 | case BTRFS_COMPRESS_NONE: |
| 56 | return btrfs_compress_types[type]; |
| 57 | } |
| 58 | |
| 59 | return NULL; |
| 60 | } |
| 61 | |
Anand Jain | 8140dc3 | 2017-05-26 15:44:58 +0800 | [diff] [blame] | 62 | static int btrfs_decompress_bio(struct compressed_bio *cb); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 63 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 64 | static inline int compressed_bio_size(struct btrfs_fs_info *fs_info, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 65 | unsigned long disk_size) |
| 66 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 67 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 68 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 69 | return sizeof(struct compressed_bio) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 70 | (DIV_ROUND_UP(disk_size, fs_info->sectorsize)) * csum_size; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Nikolay Borisov | f898ac6 | 2017-02-20 13:50:54 +0200 | [diff] [blame] | 73 | static int check_compressed_csum(struct btrfs_inode *inode, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 74 | struct compressed_bio *cb, |
| 75 | u64 disk_start) |
| 76 | { |
| 77 | int ret; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 78 | struct page *page; |
| 79 | unsigned long i; |
| 80 | char *kaddr; |
| 81 | u32 csum; |
| 82 | u32 *cb_sum = &cb->sums; |
| 83 | |
Nikolay Borisov | f898ac6 | 2017-02-20 13:50:54 +0200 | [diff] [blame] | 84 | if (inode->flags & BTRFS_INODE_NODATASUM) |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 85 | return 0; |
| 86 | |
| 87 | for (i = 0; i < cb->nr_pages; i++) { |
| 88 | page = cb->compressed_pages[i]; |
| 89 | csum = ~(u32)0; |
| 90 | |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 91 | kaddr = kmap_atomic(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 92 | csum = btrfs_csum_data(kaddr, csum, PAGE_SIZE); |
Domagoj Tršan | 0b5e3da | 2016-10-27 08:52:33 +0100 | [diff] [blame] | 93 | btrfs_csum_final(csum, (u8 *)&csum); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 94 | kunmap_atomic(kaddr); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 95 | |
| 96 | if (csum != *cb_sum) { |
Nikolay Borisov | f898ac6 | 2017-02-20 13:50:54 +0200 | [diff] [blame] | 97 | btrfs_print_data_csum_error(inode, disk_start, csum, |
Nikolay Borisov | 0970a22 | 2017-02-20 13:50:53 +0200 | [diff] [blame] | 98 | *cb_sum, cb->mirror_num); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 99 | ret = -EIO; |
| 100 | goto fail; |
| 101 | } |
| 102 | cb_sum++; |
| 103 | |
| 104 | } |
| 105 | ret = 0; |
| 106 | fail: |
| 107 | return ret; |
| 108 | } |
| 109 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 110 | /* when we finish reading compressed pages from the disk, we |
| 111 | * decompress them and then run the bio end_io routines on the |
| 112 | * decompressed pages (in the inode address space). |
| 113 | * |
| 114 | * This allows the checksumming and other IO error handling routines |
| 115 | * to work normally |
| 116 | * |
| 117 | * The compressed pages are freed here, and it must be run |
| 118 | * in process context |
| 119 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 120 | static void end_compressed_bio_read(struct bio *bio) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 121 | { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 122 | struct compressed_bio *cb = bio->bi_private; |
| 123 | struct inode *inode; |
| 124 | struct page *page; |
| 125 | unsigned long index; |
Liu Bo | cf1167d | 2017-09-20 17:50:18 -0600 | [diff] [blame] | 126 | unsigned int mirror = btrfs_io_bio(bio)->mirror_num; |
Liu Bo | e6311f2 | 2017-09-20 17:50:19 -0600 | [diff] [blame] | 127 | int ret = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 128 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 129 | if (bio->bi_status) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 130 | cb->errors = 1; |
| 131 | |
| 132 | /* if there are more bios still pending for this compressed |
| 133 | * extent, just exit |
| 134 | */ |
Elena Reshetova | a50299a | 2017-03-03 10:55:20 +0200 | [diff] [blame] | 135 | if (!refcount_dec_and_test(&cb->pending_bios)) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 136 | goto out; |
| 137 | |
Liu Bo | cf1167d | 2017-09-20 17:50:18 -0600 | [diff] [blame] | 138 | /* |
| 139 | * Record the correct mirror_num in cb->orig_bio so that |
| 140 | * read-repair can work properly. |
| 141 | */ |
| 142 | ASSERT(btrfs_io_bio(cb->orig_bio)); |
| 143 | btrfs_io_bio(cb->orig_bio)->mirror_num = mirror; |
| 144 | cb->mirror_num = mirror; |
| 145 | |
Liu Bo | e6311f2 | 2017-09-20 17:50:19 -0600 | [diff] [blame] | 146 | /* |
| 147 | * Some IO in this cb have failed, just skip checksum as there |
| 148 | * is no way it could be correct. |
| 149 | */ |
| 150 | if (cb->errors == 1) |
| 151 | goto csum_failed; |
| 152 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 153 | inode = cb->inode; |
Nikolay Borisov | f898ac6 | 2017-02-20 13:50:54 +0200 | [diff] [blame] | 154 | ret = check_compressed_csum(BTRFS_I(inode), cb, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 155 | (u64)bio->bi_iter.bi_sector << 9); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 156 | if (ret) |
| 157 | goto csum_failed; |
| 158 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 159 | /* ok, we're the last bio for this extent, lets start |
| 160 | * the decompression. |
| 161 | */ |
Anand Jain | 8140dc3 | 2017-05-26 15:44:58 +0800 | [diff] [blame] | 162 | ret = btrfs_decompress_bio(cb); |
| 163 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 164 | csum_failed: |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 165 | if (ret) |
| 166 | cb->errors = 1; |
| 167 | |
| 168 | /* release the compressed pages */ |
| 169 | index = 0; |
| 170 | for (index = 0; index < cb->nr_pages; index++) { |
| 171 | page = cb->compressed_pages[index]; |
| 172 | page->mapping = NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 173 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* do io completion on the original bio */ |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 177 | if (cb->errors) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | bio_io_error(cb->orig_bio); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 179 | } else { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 180 | int i; |
| 181 | struct bio_vec *bvec; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * we have verified the checksum already, set page |
| 185 | * checked so the end_io handlers know about it |
| 186 | */ |
David Sterba | c09abff | 2017-07-13 18:10:07 +0200 | [diff] [blame] | 187 | ASSERT(!bio_flagged(bio, BIO_CLONED)); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 188 | bio_for_each_segment_all(bvec, cb->orig_bio, i) |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 189 | SetPageChecked(bvec->bv_page); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 190 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 191 | bio_endio(cb->orig_bio); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 192 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 193 | |
| 194 | /* finally free the cb struct */ |
| 195 | kfree(cb->compressed_pages); |
| 196 | kfree(cb); |
| 197 | out: |
| 198 | bio_put(bio); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Clear the writeback bits on all of the file |
| 203 | * pages for a compressed write |
| 204 | */ |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 205 | static noinline void end_compressed_writeback(struct inode *inode, |
| 206 | const struct compressed_bio *cb) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 207 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 208 | unsigned long index = cb->start >> PAGE_SHIFT; |
| 209 | unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 210 | struct page *pages[16]; |
| 211 | unsigned long nr_pages = end_index - index + 1; |
| 212 | int i; |
| 213 | int ret; |
| 214 | |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 215 | if (cb->errors) |
| 216 | mapping_set_error(inode->i_mapping, -EIO); |
| 217 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 218 | while (nr_pages > 0) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 219 | ret = find_get_pages_contig(inode->i_mapping, index, |
Chris Mason | 5b050f0 | 2008-11-11 09:34:41 -0500 | [diff] [blame] | 220 | min_t(unsigned long, |
| 221 | nr_pages, ARRAY_SIZE(pages)), pages); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 222 | if (ret == 0) { |
| 223 | nr_pages -= 1; |
| 224 | index += 1; |
| 225 | continue; |
| 226 | } |
| 227 | for (i = 0; i < ret; i++) { |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 228 | if (cb->errors) |
| 229 | SetPageError(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 230 | end_page_writeback(pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 231 | put_page(pages[i]); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 232 | } |
| 233 | nr_pages -= ret; |
| 234 | index += ret; |
| 235 | } |
| 236 | /* the inode may be gone now */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | * do the cleanup once all the compressed pages hit the disk. |
| 241 | * This will clear writeback on the file pages and free the compressed |
| 242 | * pages. |
| 243 | * |
| 244 | * This also calls the writeback end hooks for the file pages so that |
| 245 | * metadata and checksums can be updated in the file. |
| 246 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 247 | static void end_compressed_bio_write(struct bio *bio) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 248 | { |
| 249 | struct extent_io_tree *tree; |
| 250 | struct compressed_bio *cb = bio->bi_private; |
| 251 | struct inode *inode; |
| 252 | struct page *page; |
| 253 | unsigned long index; |
| 254 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 255 | if (bio->bi_status) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 256 | cb->errors = 1; |
| 257 | |
| 258 | /* if there are more bios still pending for this compressed |
| 259 | * extent, just exit |
| 260 | */ |
Elena Reshetova | a50299a | 2017-03-03 10:55:20 +0200 | [diff] [blame] | 261 | if (!refcount_dec_and_test(&cb->pending_bios)) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 262 | goto out; |
| 263 | |
| 264 | /* ok, we're the last bio for this extent, step one is to |
| 265 | * call back into the FS and do all the end_io operations |
| 266 | */ |
| 267 | inode = cb->inode; |
| 268 | tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 269 | cb->compressed_pages[0]->mapping = cb->inode->i_mapping; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 270 | tree->ops->writepage_end_io_hook(cb->compressed_pages[0], |
| 271 | cb->start, |
| 272 | cb->start + cb->len - 1, |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 273 | NULL, |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 274 | bio->bi_status ? |
| 275 | BLK_STS_OK : BLK_STS_NOTSUPP); |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 276 | cb->compressed_pages[0]->mapping = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 277 | |
Filipe Manana | 7bdcefc | 2014-10-07 01:48:26 +0100 | [diff] [blame] | 278 | end_compressed_writeback(inode, cb); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 279 | /* note, our inode could be gone now */ |
| 280 | |
| 281 | /* |
| 282 | * release the compressed pages, these came from alloc_page and |
| 283 | * are not attached to the inode at all |
| 284 | */ |
| 285 | index = 0; |
| 286 | for (index = 0; index < cb->nr_pages; index++) { |
| 287 | page = cb->compressed_pages[index]; |
| 288 | page->mapping = NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 289 | put_page(page); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* finally free the cb struct */ |
| 293 | kfree(cb->compressed_pages); |
| 294 | kfree(cb); |
| 295 | out: |
| 296 | bio_put(bio); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * worker function to build and submit bios for previously compressed pages. |
| 301 | * The corresponding pages in the inode should be marked for writeback |
| 302 | * and the compressed pages should have a reference on them for dropping |
| 303 | * when the IO is complete. |
| 304 | * |
| 305 | * This also checksums the file bytes and gets things ready for |
| 306 | * the end io hooks. |
| 307 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 308 | blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 309 | unsigned long len, u64 disk_start, |
| 310 | unsigned long compressed_len, |
| 311 | struct page **compressed_pages, |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 312 | unsigned long nr_pages, |
| 313 | unsigned int write_flags) |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 314 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 315 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 316 | struct bio *bio = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 317 | struct compressed_bio *cb; |
| 318 | unsigned long bytes_left; |
| 319 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 320 | int pg_index = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 321 | struct page *page; |
| 322 | u64 first_byte = disk_start; |
| 323 | struct block_device *bdev; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 324 | blk_status_t ret; |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 325 | int skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 326 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 327 | WARN_ON(start & ((u64)PAGE_SIZE - 1)); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 328 | cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS); |
Yoshinori Sano | dac97e5 | 2011-02-15 12:01:42 +0000 | [diff] [blame] | 329 | if (!cb) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 330 | return BLK_STS_RESOURCE; |
Elena Reshetova | a50299a | 2017-03-03 10:55:20 +0200 | [diff] [blame] | 331 | refcount_set(&cb->pending_bios, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 332 | cb->errors = 0; |
| 333 | cb->inode = inode; |
| 334 | cb->start = start; |
| 335 | cb->len = len; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 336 | cb->mirror_num = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 337 | cb->compressed_pages = compressed_pages; |
| 338 | cb->compressed_len = compressed_len; |
| 339 | cb->orig_bio = NULL; |
| 340 | cb->nr_pages = nr_pages; |
| 341 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 342 | bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 343 | |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 344 | bio = btrfs_bio_alloc(bdev, first_byte); |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 345 | bio->bi_opf = REQ_OP_WRITE | write_flags; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 346 | bio->bi_private = cb; |
| 347 | bio->bi_end_io = end_compressed_bio_write; |
Elena Reshetova | a50299a | 2017-03-03 10:55:20 +0200 | [diff] [blame] | 348 | refcount_set(&cb->pending_bios, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 349 | |
| 350 | /* create and submit bios for the compressed pages */ |
| 351 | bytes_left = compressed_len; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 352 | for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 353 | int submit = 0; |
| 354 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 355 | page = compressed_pages[pg_index]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 356 | page->mapping = inode->i_mapping; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 357 | if (bio->bi_iter.bi_size) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 358 | submit = io_tree->ops->merge_bio_hook(page, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 359 | PAGE_SIZE, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 360 | bio, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 361 | |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 362 | page->mapping = NULL; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 363 | if (submit || bio_add_page(bio, page, PAGE_SIZE, 0) < |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 364 | PAGE_SIZE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 365 | bio_get(bio); |
| 366 | |
Chris Mason | af09abf | 2008-11-07 12:35:44 -0500 | [diff] [blame] | 367 | /* |
| 368 | * inc the count before we submit the bio so |
| 369 | * we know the end IO handler won't happen before |
| 370 | * we inc the count. Otherwise, the cb might get |
| 371 | * freed before we're done setting it up |
| 372 | */ |
Elena Reshetova | a50299a | 2017-03-03 10:55:20 +0200 | [diff] [blame] | 373 | refcount_inc(&cb->pending_bios); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 374 | ret = btrfs_bio_wq_end_io(fs_info, bio, |
| 375 | BTRFS_WQ_ENDIO_DATA); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 376 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 377 | |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 378 | if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 379 | ret = btrfs_csum_one_bio(inode, bio, start, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 380 | BUG_ON(ret); /* -ENOMEM */ |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 381 | } |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 382 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 383 | ret = btrfs_map_bio(fs_info, bio, 0, 1); |
Liu Bo | f5daf2c | 2016-06-22 18:32:06 -0700 | [diff] [blame] | 384 | if (ret) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 385 | bio->bi_status = ret; |
Liu Bo | f5daf2c | 2016-06-22 18:32:06 -0700 | [diff] [blame] | 386 | bio_endio(bio); |
| 387 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 388 | |
| 389 | bio_put(bio); |
| 390 | |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 391 | bio = btrfs_bio_alloc(bdev, first_byte); |
Liu Bo | f82b735 | 2017-10-23 23:18:16 -0600 | [diff] [blame] | 392 | bio->bi_opf = REQ_OP_WRITE | write_flags; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 393 | bio->bi_private = cb; |
| 394 | bio->bi_end_io = end_compressed_bio_write; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 395 | bio_add_page(bio, page, PAGE_SIZE, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 396 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 397 | if (bytes_left < PAGE_SIZE) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 398 | btrfs_info(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 399 | "bytes left %lu compress len %lu nr %lu", |
Chris Mason | cfbc246 | 2008-10-30 13:22:14 -0400 | [diff] [blame] | 400 | bytes_left, cb->compressed_len, cb->nr_pages); |
| 401 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 402 | bytes_left -= PAGE_SIZE; |
| 403 | first_byte += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 404 | cond_resched(); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 405 | } |
| 406 | bio_get(bio); |
| 407 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 408 | ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 409 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 410 | |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 411 | if (!skip_sum) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 412 | ret = btrfs_csum_one_bio(inode, bio, start, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 413 | BUG_ON(ret); /* -ENOMEM */ |
Li Zefan | e55179b | 2011-07-14 03:16:47 +0000 | [diff] [blame] | 414 | } |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 415 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 416 | ret = btrfs_map_bio(fs_info, bio, 0, 1); |
Liu Bo | f5daf2c | 2016-06-22 18:32:06 -0700 | [diff] [blame] | 417 | if (ret) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 418 | bio->bi_status = ret; |
Liu Bo | f5daf2c | 2016-06-22 18:32:06 -0700 | [diff] [blame] | 419 | bio_endio(bio); |
| 420 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 421 | |
| 422 | bio_put(bio); |
| 423 | return 0; |
| 424 | } |
| 425 | |
Christoph Hellwig | 2a4d0c9 | 2016-11-25 09:07:51 +0100 | [diff] [blame] | 426 | static u64 bio_end_offset(struct bio *bio) |
| 427 | { |
| 428 | struct bio_vec *last = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
| 429 | |
| 430 | return page_offset(last->bv_page) + last->bv_len + last->bv_offset; |
| 431 | } |
| 432 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 433 | static noinline int add_ra_bio_pages(struct inode *inode, |
| 434 | u64 compressed_end, |
| 435 | struct compressed_bio *cb) |
| 436 | { |
| 437 | unsigned long end_index; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 438 | unsigned long pg_index; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 439 | u64 last_offset; |
| 440 | u64 isize = i_size_read(inode); |
| 441 | int ret; |
| 442 | struct page *page; |
| 443 | unsigned long nr_pages = 0; |
| 444 | struct extent_map *em; |
| 445 | struct address_space *mapping = inode->i_mapping; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 446 | struct extent_map_tree *em_tree; |
| 447 | struct extent_io_tree *tree; |
| 448 | u64 end; |
| 449 | int misses = 0; |
| 450 | |
Christoph Hellwig | 2a4d0c9 | 2016-11-25 09:07:51 +0100 | [diff] [blame] | 451 | last_offset = bio_end_offset(cb->orig_bio); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 452 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 453 | tree = &BTRFS_I(inode)->io_tree; |
| 454 | |
| 455 | if (isize == 0) |
| 456 | return 0; |
| 457 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 458 | end_index = (i_size_read(inode) - 1) >> PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 459 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 460 | while (last_offset < compressed_end) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 461 | pg_index = last_offset >> PAGE_SHIFT; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 462 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 463 | if (pg_index > end_index) |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 464 | break; |
| 465 | |
| 466 | rcu_read_lock(); |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 467 | page = radix_tree_lookup(&mapping->page_tree, pg_index); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 468 | rcu_read_unlock(); |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 469 | if (page && !radix_tree_exceptional_entry(page)) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 470 | misses++; |
| 471 | if (misses > 4) |
| 472 | break; |
| 473 | goto next; |
| 474 | } |
| 475 | |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 476 | page = __page_cache_alloc(mapping_gfp_constraint(mapping, |
| 477 | ~__GFP_FS)); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 478 | if (!page) |
| 479 | break; |
| 480 | |
Michal Hocko | c62d255 | 2015-11-06 16:28:49 -0800 | [diff] [blame] | 481 | 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] | 482 | put_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 483 | goto next; |
| 484 | } |
| 485 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 486 | end = last_offset + PAGE_SIZE - 1; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 487 | /* |
| 488 | * at this point, we have a locked page in the page cache |
| 489 | * for these bytes in the file. But, we have to make |
| 490 | * sure they map to this compressed extent on disk. |
| 491 | */ |
| 492 | set_page_extent_mapped(page); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 493 | lock_extent(tree, last_offset, end); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 494 | read_lock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 495 | em = lookup_extent_mapping(em_tree, last_offset, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 496 | PAGE_SIZE); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 497 | read_unlock(&em_tree->lock); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 498 | |
| 499 | if (!em || last_offset < em->start || |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 500 | (last_offset + PAGE_SIZE > extent_map_end(em)) || |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 501 | (em->block_start >> 9) != cb->orig_bio->bi_iter.bi_sector) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 502 | free_extent_map(em); |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 503 | unlock_extent(tree, last_offset, end); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 504 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 505 | put_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | free_extent_map(em); |
| 509 | |
| 510 | if (page->index == end_index) { |
| 511 | char *userpage; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 512 | size_t zero_offset = isize & (PAGE_SIZE - 1); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 513 | |
| 514 | if (zero_offset) { |
| 515 | int zeros; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 516 | zeros = PAGE_SIZE - zero_offset; |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 517 | userpage = kmap_atomic(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 518 | memset(userpage + zero_offset, 0, zeros); |
| 519 | flush_dcache_page(page); |
Cong Wang | 7ac687d | 2011-11-25 23:14:28 +0800 | [diff] [blame] | 520 | kunmap_atomic(userpage); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | ret = bio_add_page(cb->orig_bio, page, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 525 | PAGE_SIZE, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 526 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 527 | if (ret == PAGE_SIZE) { |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 528 | nr_pages++; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 529 | put_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 530 | } else { |
Jeff Mahoney | d008237 | 2012-03-01 14:57:19 +0100 | [diff] [blame] | 531 | unlock_extent(tree, last_offset, end); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 532 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 533 | put_page(page); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 534 | break; |
| 535 | } |
| 536 | next: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 537 | last_offset += PAGE_SIZE; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 538 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 542 | /* |
| 543 | * for a compressed read, the bio we get passed has all the inode pages |
| 544 | * in it. We don't actually do IO on those pages but allocate new ones |
| 545 | * to hold the compressed pages on disk. |
| 546 | * |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 547 | * bio->bi_iter.bi_sector points to the compressed extent on disk |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 548 | * bio->bi_io_vec points to all of the inode pages |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 549 | * |
| 550 | * After the compressed pages are read, we copy the bytes into the |
| 551 | * bio we were passed and then call the bio end_io calls |
| 552 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 553 | 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] | 554 | int mirror_num, unsigned long bio_flags) |
| 555 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 556 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 557 | struct extent_io_tree *tree; |
| 558 | struct extent_map_tree *em_tree; |
| 559 | struct compressed_bio *cb; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 560 | unsigned long compressed_len; |
| 561 | unsigned long nr_pages; |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 562 | unsigned long pg_index; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 563 | struct page *page; |
| 564 | struct block_device *bdev; |
| 565 | struct bio *comp_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 566 | u64 cur_disk_byte = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | e04ca62 | 2008-11-10 11:44:58 -0500 | [diff] [blame] | 567 | u64 em_len; |
| 568 | u64 em_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 569 | struct extent_map *em; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 570 | blk_status_t ret = BLK_STS_RESOURCE; |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 571 | int faili = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 572 | u32 *sums; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 573 | |
| 574 | tree = &BTRFS_I(inode)->io_tree; |
| 575 | em_tree = &BTRFS_I(inode)->extent_tree; |
| 576 | |
| 577 | /* we need the actual starting offset of this extent in the file */ |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 578 | read_lock(&em_tree->lock); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 579 | em = lookup_extent_mapping(em_tree, |
| 580 | page_offset(bio->bi_io_vec->bv_page), |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 581 | PAGE_SIZE); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 582 | read_unlock(&em_tree->lock); |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 583 | if (!em) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 584 | return BLK_STS_IOERR; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 585 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 586 | compressed_len = em->block_len; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 587 | cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS); |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 588 | if (!cb) |
| 589 | goto out; |
| 590 | |
Elena Reshetova | a50299a | 2017-03-03 10:55:20 +0200 | [diff] [blame] | 591 | refcount_set(&cb->pending_bios, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 592 | cb->errors = 0; |
| 593 | cb->inode = inode; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 594 | cb->mirror_num = mirror_num; |
| 595 | sums = &cb->sums; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 596 | |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 597 | cb->start = em->orig_start; |
Chris Mason | e04ca62 | 2008-11-10 11:44:58 -0500 | [diff] [blame] | 598 | em_len = em->len; |
| 599 | em_start = em->start; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 600 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 601 | free_extent_map(em); |
Chris Mason | e04ca62 | 2008-11-10 11:44:58 -0500 | [diff] [blame] | 602 | em = NULL; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 603 | |
Christoph Hellwig | 8138105 | 2016-11-25 09:07:50 +0100 | [diff] [blame] | 604 | cb->len = bio->bi_iter.bi_size; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 605 | cb->compressed_len = compressed_len; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 606 | cb->compress_type = extent_compress_type(bio_flags); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 607 | cb->orig_bio = bio; |
| 608 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 609 | nr_pages = DIV_ROUND_UP(compressed_len, PAGE_SIZE); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 610 | cb->compressed_pages = kcalloc(nr_pages, sizeof(struct page *), |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 611 | GFP_NOFS); |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 612 | if (!cb->compressed_pages) |
| 613 | goto fail1; |
| 614 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 615 | bdev = fs_info->fs_devices->latest_bdev; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 616 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 617 | for (pg_index = 0; pg_index < nr_pages; pg_index++) { |
| 618 | cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 619 | __GFP_HIGHMEM); |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 620 | if (!cb->compressed_pages[pg_index]) { |
| 621 | faili = pg_index - 1; |
Dan Carpenter | 0e9350d | 2017-06-19 13:55:37 +0300 | [diff] [blame] | 622 | ret = BLK_STS_RESOURCE; |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 623 | goto fail2; |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 624 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 625 | } |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 626 | faili = nr_pages - 1; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 627 | cb->nr_pages = nr_pages; |
| 628 | |
Filipe Manana | 7f042a8 | 2016-01-27 19:17:20 +0000 | [diff] [blame] | 629 | add_ra_bio_pages(inode, em_start + em_len, cb); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 630 | |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 631 | /* include any pages we added in add_ra-bio_pages */ |
Christoph Hellwig | 8138105 | 2016-11-25 09:07:50 +0100 | [diff] [blame] | 632 | cb->len = bio->bi_iter.bi_size; |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 633 | |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 634 | comp_bio = btrfs_bio_alloc(bdev, cur_disk_byte); |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 635 | bio_set_op_attrs (comp_bio, REQ_OP_READ, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 636 | comp_bio->bi_private = cb; |
| 637 | comp_bio->bi_end_io = end_compressed_bio_read; |
Elena Reshetova | a50299a | 2017-03-03 10:55:20 +0200 | [diff] [blame] | 638 | refcount_set(&cb->pending_bios, 1); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 639 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 640 | for (pg_index = 0; pg_index < nr_pages; pg_index++) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 641 | int submit = 0; |
| 642 | |
David Sterba | 306e16c | 2011-04-19 14:29:38 +0200 | [diff] [blame] | 643 | page = cb->compressed_pages[pg_index]; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 644 | page->mapping = inode->i_mapping; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 645 | page->index = em_start >> PAGE_SHIFT; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 646 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 647 | if (comp_bio->bi_iter.bi_size) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 648 | submit = tree->ops->merge_bio_hook(page, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 649 | PAGE_SIZE, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 650 | comp_bio, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 651 | |
Chris Mason | 70b99e6 | 2008-10-31 12:46:39 -0400 | [diff] [blame] | 652 | page->mapping = NULL; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 653 | if (submit || bio_add_page(comp_bio, page, PAGE_SIZE, 0) < |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 654 | PAGE_SIZE) { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 655 | bio_get(comp_bio); |
| 656 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 657 | ret = btrfs_bio_wq_end_io(fs_info, comp_bio, |
| 658 | BTRFS_WQ_ENDIO_DATA); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 659 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 660 | |
Chris Mason | af09abf | 2008-11-07 12:35:44 -0500 | [diff] [blame] | 661 | /* |
| 662 | * inc the count before we submit the bio so |
| 663 | * we know the end IO handler won't happen before |
| 664 | * we inc the count. Otherwise, the cb might get |
| 665 | * freed before we're done setting it up |
| 666 | */ |
Elena Reshetova | a50299a | 2017-03-03 10:55:20 +0200 | [diff] [blame] | 667 | refcount_inc(&cb->pending_bios); |
Chris Mason | af09abf | 2008-11-07 12:35:44 -0500 | [diff] [blame] | 668 | |
Christoph Hellwig | 6cbff00 | 2009-04-17 10:37:41 +0200 | [diff] [blame] | 669 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 670 | ret = btrfs_lookup_bio_sums(inode, comp_bio, |
| 671 | sums); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 672 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 673 | } |
David Sterba | ed6078f | 2014-06-05 01:59:57 +0200 | [diff] [blame] | 674 | sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 675 | fs_info->sectorsize); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 676 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 677 | ret = btrfs_map_bio(fs_info, comp_bio, mirror_num, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 678 | if (ret) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 679 | comp_bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 680 | bio_endio(comp_bio); |
| 681 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 682 | |
| 683 | bio_put(comp_bio); |
| 684 | |
David Sterba | c821e7f3 | 2017-06-02 18:35:36 +0200 | [diff] [blame] | 685 | comp_bio = btrfs_bio_alloc(bdev, cur_disk_byte); |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 686 | bio_set_op_attrs(comp_bio, REQ_OP_READ, 0); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 687 | comp_bio->bi_private = cb; |
| 688 | comp_bio->bi_end_io = end_compressed_bio_read; |
| 689 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 690 | bio_add_page(comp_bio, page, PAGE_SIZE, 0); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 691 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 692 | cur_disk_byte += PAGE_SIZE; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 693 | } |
| 694 | bio_get(comp_bio); |
| 695 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 696 | ret = btrfs_bio_wq_end_io(fs_info, comp_bio, BTRFS_WQ_ENDIO_DATA); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 697 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 698 | |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 699 | if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 700 | ret = btrfs_lookup_bio_sums(inode, comp_bio, sums); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 701 | BUG_ON(ret); /* -ENOMEM */ |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 702 | } |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 703 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 704 | ret = btrfs_map_bio(fs_info, comp_bio, mirror_num, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 705 | if (ret) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 706 | comp_bio->bi_status = ret; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 707 | bio_endio(comp_bio); |
| 708 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 709 | |
| 710 | bio_put(comp_bio); |
| 711 | return 0; |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 712 | |
| 713 | fail2: |
Josef Bacik | 15e3004a | 2012-10-05 13:39:50 -0400 | [diff] [blame] | 714 | while (faili >= 0) { |
| 715 | __free_page(cb->compressed_pages[faili]); |
| 716 | faili--; |
| 717 | } |
liubo | 6b82ce8 | 2011-01-26 06:21:39 +0000 | [diff] [blame] | 718 | |
| 719 | kfree(cb->compressed_pages); |
| 720 | fail1: |
| 721 | kfree(cb); |
| 722 | out: |
| 723 | free_extent_map(em); |
| 724 | return ret; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 725 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 726 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 727 | /* |
| 728 | * Heuristic uses systematic sampling to collect data from the input data |
| 729 | * range, the logic can be tuned by the following constants: |
| 730 | * |
| 731 | * @SAMPLING_READ_SIZE - how many bytes will be copied from for each sample |
| 732 | * @SAMPLING_INTERVAL - range from which the sampled data can be collected |
| 733 | */ |
| 734 | #define SAMPLING_READ_SIZE (16) |
| 735 | #define SAMPLING_INTERVAL (256) |
| 736 | |
| 737 | /* |
| 738 | * For statistical analysis of the input data we consider bytes that form a |
| 739 | * Galois Field of 256 objects. Each object has an attribute count, ie. how |
| 740 | * many times the object appeared in the sample. |
| 741 | */ |
| 742 | #define BUCKET_SIZE (256) |
| 743 | |
| 744 | /* |
| 745 | * The size of the sample is based on a statistical sampling rule of thumb. |
| 746 | * The common way is to perform sampling tests as long as the number of |
| 747 | * elements in each cell is at least 5. |
| 748 | * |
| 749 | * Instead of 5, we choose 32 to obtain more accurate results. |
| 750 | * If the data contain the maximum number of symbols, which is 256, we obtain a |
| 751 | * sample size bound by 8192. |
| 752 | * |
| 753 | * For a sample of at most 8KB of data per data range: 16 consecutive bytes |
| 754 | * from up to 512 locations. |
| 755 | */ |
| 756 | #define MAX_SAMPLE_SIZE (BTRFS_MAX_UNCOMPRESSED * \ |
| 757 | SAMPLING_READ_SIZE / SAMPLING_INTERVAL) |
| 758 | |
| 759 | struct bucket_item { |
| 760 | u32 count; |
| 761 | }; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 762 | |
| 763 | struct heuristic_ws { |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 764 | /* Partial copy of input data */ |
| 765 | u8 *sample; |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 766 | u32 sample_size; |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 767 | /* Buckets store counters for each byte value */ |
| 768 | struct bucket_item *bucket; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 769 | /* Sorting buffer */ |
| 770 | struct bucket_item *bucket_b; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 771 | struct list_head list; |
| 772 | }; |
| 773 | |
| 774 | static void free_heuristic_ws(struct list_head *ws) |
| 775 | { |
| 776 | struct heuristic_ws *workspace; |
| 777 | |
| 778 | workspace = list_entry(ws, struct heuristic_ws, list); |
| 779 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 780 | kvfree(workspace->sample); |
| 781 | kfree(workspace->bucket); |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 782 | kfree(workspace->bucket_b); |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 783 | kfree(workspace); |
| 784 | } |
| 785 | |
| 786 | static struct list_head *alloc_heuristic_ws(void) |
| 787 | { |
| 788 | struct heuristic_ws *ws; |
| 789 | |
| 790 | ws = kzalloc(sizeof(*ws), GFP_KERNEL); |
| 791 | if (!ws) |
| 792 | return ERR_PTR(-ENOMEM); |
| 793 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 794 | ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL); |
| 795 | if (!ws->sample) |
| 796 | goto fail; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 797 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 798 | ws->bucket = kcalloc(BUCKET_SIZE, sizeof(*ws->bucket), GFP_KERNEL); |
| 799 | if (!ws->bucket) |
| 800 | goto fail; |
| 801 | |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 802 | ws->bucket_b = kcalloc(BUCKET_SIZE, sizeof(*ws->bucket_b), GFP_KERNEL); |
| 803 | if (!ws->bucket_b) |
| 804 | goto fail; |
| 805 | |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 806 | INIT_LIST_HEAD(&ws->list); |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 807 | return &ws->list; |
Timofey Titovets | 17b5a6c | 2017-09-28 17:33:37 +0300 | [diff] [blame] | 808 | fail: |
| 809 | free_heuristic_ws(&ws->list); |
| 810 | return ERR_PTR(-ENOMEM); |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | struct workspaces_list { |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 814 | struct list_head idle_ws; |
| 815 | spinlock_t ws_lock; |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 816 | /* Number of free workspaces */ |
| 817 | int free_ws; |
| 818 | /* Total number of allocated workspaces */ |
| 819 | atomic_t total_ws; |
| 820 | /* Waiters for a free workspace */ |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 821 | wait_queue_head_t ws_wait; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 822 | }; |
| 823 | |
| 824 | static struct workspaces_list btrfs_comp_ws[BTRFS_COMPRESS_TYPES]; |
| 825 | |
| 826 | static struct workspaces_list btrfs_heuristic_ws; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 827 | |
David Sterba | e8c9f18 | 2015-01-02 18:23:10 +0100 | [diff] [blame] | 828 | static const struct btrfs_compress_op * const btrfs_compress_op[] = { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 829 | &btrfs_zlib_compress, |
Li Zefan | a6fa6fa | 2010-10-25 15:12:26 +0800 | [diff] [blame] | 830 | &btrfs_lzo_compress, |
Nick Terrell | 5c1aab1 | 2017-08-09 19:39:02 -0700 | [diff] [blame] | 831 | &btrfs_zstd_compress, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 832 | }; |
| 833 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 834 | void __init btrfs_init_compress(void) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 835 | { |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 836 | struct list_head *workspace; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 837 | int i; |
| 838 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 839 | INIT_LIST_HEAD(&btrfs_heuristic_ws.idle_ws); |
| 840 | spin_lock_init(&btrfs_heuristic_ws.ws_lock); |
| 841 | atomic_set(&btrfs_heuristic_ws.total_ws, 0); |
| 842 | init_waitqueue_head(&btrfs_heuristic_ws.ws_wait); |
David Sterba | f77dd0d | 2016-04-27 02:55:15 +0200 | [diff] [blame] | 843 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 844 | workspace = alloc_heuristic_ws(); |
| 845 | if (IS_ERR(workspace)) { |
| 846 | pr_warn( |
| 847 | "BTRFS: cannot preallocate heuristic workspace, will try later\n"); |
| 848 | } else { |
| 849 | atomic_set(&btrfs_heuristic_ws.total_ws, 1); |
| 850 | btrfs_heuristic_ws.free_ws = 1; |
| 851 | list_add(workspace, &btrfs_heuristic_ws.idle_ws); |
| 852 | } |
| 853 | |
| 854 | for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) { |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 855 | INIT_LIST_HEAD(&btrfs_comp_ws[i].idle_ws); |
| 856 | spin_lock_init(&btrfs_comp_ws[i].ws_lock); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 857 | atomic_set(&btrfs_comp_ws[i].total_ws, 0); |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 858 | init_waitqueue_head(&btrfs_comp_ws[i].ws_wait); |
David Sterba | f77dd0d | 2016-04-27 02:55:15 +0200 | [diff] [blame] | 859 | |
| 860 | /* |
| 861 | * Preallocate one workspace for each compression type so |
| 862 | * we can guarantee forward progress in the worst case |
| 863 | */ |
| 864 | workspace = btrfs_compress_op[i]->alloc_workspace(); |
| 865 | if (IS_ERR(workspace)) { |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 866 | pr_warn("BTRFS: cannot preallocate compression workspace, will try later\n"); |
David Sterba | f77dd0d | 2016-04-27 02:55:15 +0200 | [diff] [blame] | 867 | } else { |
| 868 | atomic_set(&btrfs_comp_ws[i].total_ws, 1); |
| 869 | btrfs_comp_ws[i].free_ws = 1; |
| 870 | list_add(workspace, &btrfs_comp_ws[i].idle_ws); |
| 871 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 872 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /* |
David Sterba | e721e49 | 2016-04-27 02:41:17 +0200 | [diff] [blame] | 876 | * This finds an available workspace or allocates a new one. |
| 877 | * If it's not possible to allocate a new one, waits until there's one. |
| 878 | * Preallocation makes a forward progress guarantees and we do not return |
| 879 | * errors. |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 880 | */ |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 881 | static struct list_head *__find_workspace(int type, bool heuristic) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 882 | { |
| 883 | struct list_head *workspace; |
| 884 | int cpus = num_online_cpus(); |
| 885 | int idx = type - 1; |
David Sterba | fe30853 | 2017-05-31 17:14:56 +0200 | [diff] [blame] | 886 | unsigned nofs_flag; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 887 | struct list_head *idle_ws; |
| 888 | spinlock_t *ws_lock; |
| 889 | atomic_t *total_ws; |
| 890 | wait_queue_head_t *ws_wait; |
| 891 | int *free_ws; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 892 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 893 | if (heuristic) { |
| 894 | idle_ws = &btrfs_heuristic_ws.idle_ws; |
| 895 | ws_lock = &btrfs_heuristic_ws.ws_lock; |
| 896 | total_ws = &btrfs_heuristic_ws.total_ws; |
| 897 | ws_wait = &btrfs_heuristic_ws.ws_wait; |
| 898 | free_ws = &btrfs_heuristic_ws.free_ws; |
| 899 | } else { |
| 900 | idle_ws = &btrfs_comp_ws[idx].idle_ws; |
| 901 | ws_lock = &btrfs_comp_ws[idx].ws_lock; |
| 902 | total_ws = &btrfs_comp_ws[idx].total_ws; |
| 903 | ws_wait = &btrfs_comp_ws[idx].ws_wait; |
| 904 | free_ws = &btrfs_comp_ws[idx].free_ws; |
| 905 | } |
| 906 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 907 | again: |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 908 | spin_lock(ws_lock); |
| 909 | if (!list_empty(idle_ws)) { |
| 910 | workspace = idle_ws->next; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 911 | list_del(workspace); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 912 | (*free_ws)--; |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 913 | spin_unlock(ws_lock); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 914 | return workspace; |
| 915 | |
| 916 | } |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 917 | if (atomic_read(total_ws) > cpus) { |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 918 | DEFINE_WAIT(wait); |
| 919 | |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 920 | spin_unlock(ws_lock); |
| 921 | prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 922 | if (atomic_read(total_ws) > cpus && !*free_ws) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 923 | schedule(); |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 924 | finish_wait(ws_wait, &wait); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 925 | goto again; |
| 926 | } |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 927 | atomic_inc(total_ws); |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 928 | spin_unlock(ws_lock); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 929 | |
David Sterba | fe30853 | 2017-05-31 17:14:56 +0200 | [diff] [blame] | 930 | /* |
| 931 | * Allocation helpers call vmalloc that can't use GFP_NOFS, so we have |
| 932 | * to turn it off here because we might get called from the restricted |
| 933 | * context of btrfs_compress_bio/btrfs_compress_pages |
| 934 | */ |
| 935 | nofs_flag = memalloc_nofs_save(); |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 936 | if (heuristic) |
| 937 | workspace = alloc_heuristic_ws(); |
| 938 | else |
| 939 | workspace = btrfs_compress_op[idx]->alloc_workspace(); |
David Sterba | fe30853 | 2017-05-31 17:14:56 +0200 | [diff] [blame] | 940 | memalloc_nofs_restore(nofs_flag); |
| 941 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 942 | if (IS_ERR(workspace)) { |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 943 | atomic_dec(total_ws); |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 944 | wake_up(ws_wait); |
David Sterba | e721e49 | 2016-04-27 02:41:17 +0200 | [diff] [blame] | 945 | |
| 946 | /* |
| 947 | * Do not return the error but go back to waiting. There's a |
| 948 | * workspace preallocated for each type and the compression |
| 949 | * time is bounded so we get to a workspace eventually. This |
| 950 | * makes our caller's life easier. |
David Sterba | 52356716 | 2016-04-27 03:07:39 +0200 | [diff] [blame] | 951 | * |
| 952 | * To prevent silent and low-probability deadlocks (when the |
| 953 | * initial preallocation fails), check if there are any |
| 954 | * workspaces at all. |
David Sterba | e721e49 | 2016-04-27 02:41:17 +0200 | [diff] [blame] | 955 | */ |
David Sterba | 52356716 | 2016-04-27 03:07:39 +0200 | [diff] [blame] | 956 | if (atomic_read(total_ws) == 0) { |
| 957 | static DEFINE_RATELIMIT_STATE(_rs, |
| 958 | /* once per minute */ 60 * HZ, |
| 959 | /* no burst */ 1); |
| 960 | |
| 961 | if (__ratelimit(&_rs)) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 962 | pr_warn("BTRFS: no compression workspaces, low memory, retrying\n"); |
David Sterba | 52356716 | 2016-04-27 03:07:39 +0200 | [diff] [blame] | 963 | } |
| 964 | } |
David Sterba | e721e49 | 2016-04-27 02:41:17 +0200 | [diff] [blame] | 965 | goto again; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 966 | } |
| 967 | return workspace; |
| 968 | } |
| 969 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 970 | static struct list_head *find_workspace(int type) |
| 971 | { |
| 972 | return __find_workspace(type, false); |
| 973 | } |
| 974 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 975 | /* |
| 976 | * put a workspace struct back on the list or free it if we have enough |
| 977 | * idle ones sitting around |
| 978 | */ |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 979 | static void __free_workspace(int type, struct list_head *workspace, |
| 980 | bool heuristic) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 981 | { |
| 982 | int idx = type - 1; |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 983 | struct list_head *idle_ws; |
| 984 | spinlock_t *ws_lock; |
| 985 | atomic_t *total_ws; |
| 986 | wait_queue_head_t *ws_wait; |
| 987 | int *free_ws; |
| 988 | |
| 989 | if (heuristic) { |
| 990 | idle_ws = &btrfs_heuristic_ws.idle_ws; |
| 991 | ws_lock = &btrfs_heuristic_ws.ws_lock; |
| 992 | total_ws = &btrfs_heuristic_ws.total_ws; |
| 993 | ws_wait = &btrfs_heuristic_ws.ws_wait; |
| 994 | free_ws = &btrfs_heuristic_ws.free_ws; |
| 995 | } else { |
| 996 | idle_ws = &btrfs_comp_ws[idx].idle_ws; |
| 997 | ws_lock = &btrfs_comp_ws[idx].ws_lock; |
| 998 | total_ws = &btrfs_comp_ws[idx].total_ws; |
| 999 | ws_wait = &btrfs_comp_ws[idx].ws_wait; |
| 1000 | free_ws = &btrfs_comp_ws[idx].free_ws; |
| 1001 | } |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1002 | |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1003 | spin_lock(ws_lock); |
Nick Terrell | 26b28dc | 2017-06-29 10:57:26 -0700 | [diff] [blame] | 1004 | if (*free_ws <= num_online_cpus()) { |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1005 | list_add(workspace, idle_ws); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1006 | (*free_ws)++; |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1007 | spin_unlock(ws_lock); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1008 | goto wake; |
| 1009 | } |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1010 | spin_unlock(ws_lock); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1011 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1012 | if (heuristic) |
| 1013 | free_heuristic_ws(workspace); |
| 1014 | else |
| 1015 | btrfs_compress_op[idx]->free_workspace(workspace); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1016 | atomic_dec(total_ws); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1017 | wake: |
David Sterba | a83342a | 2015-02-16 19:36:47 +0100 | [diff] [blame] | 1018 | /* |
| 1019 | * Make sure counter is updated before we wake up waiters. |
| 1020 | */ |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 1021 | smp_mb(); |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1022 | if (waitqueue_active(ws_wait)) |
| 1023 | wake_up(ws_wait); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1024 | } |
| 1025 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1026 | static void free_workspace(int type, struct list_head *ws) |
| 1027 | { |
| 1028 | return __free_workspace(type, ws, false); |
| 1029 | } |
| 1030 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1031 | /* |
| 1032 | * cleanup function for module exit |
| 1033 | */ |
| 1034 | static void free_workspaces(void) |
| 1035 | { |
| 1036 | struct list_head *workspace; |
| 1037 | int i; |
| 1038 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1039 | while (!list_empty(&btrfs_heuristic_ws.idle_ws)) { |
| 1040 | workspace = btrfs_heuristic_ws.idle_ws.next; |
| 1041 | list_del(workspace); |
| 1042 | free_heuristic_ws(workspace); |
| 1043 | atomic_dec(&btrfs_heuristic_ws.total_ws); |
| 1044 | } |
| 1045 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1046 | for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) { |
Byongho Lee | d918764 | 2015-10-14 14:05:24 +0900 | [diff] [blame] | 1047 | while (!list_empty(&btrfs_comp_ws[i].idle_ws)) { |
| 1048 | workspace = btrfs_comp_ws[i].idle_ws.next; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1049 | list_del(workspace); |
| 1050 | btrfs_compress_op[i]->free_workspace(workspace); |
David Sterba | 6ac10a6 | 2016-04-27 02:15:15 +0200 | [diff] [blame] | 1051 | atomic_dec(&btrfs_comp_ws[i].total_ws); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /* |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1057 | * Given an address space and start and length, compress the bytes into @pages |
| 1058 | * that are allocated on demand. |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1059 | * |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1060 | * @type_level is encoded algorithm and level, where level 0 means whatever |
| 1061 | * default the algorithm chooses and is opaque here; |
| 1062 | * - compression algo are 0-3 |
| 1063 | * - the level are bits 4-7 |
| 1064 | * |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1065 | * @out_pages is an in/out parameter, holds maximum number of pages to allocate |
| 1066 | * and returns number of actually allocated pages |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1067 | * |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1068 | * @total_in is used to return the number of bytes actually read. It |
| 1069 | * 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] | 1070 | * ran out of room in the pages array or because we cross the |
| 1071 | * max_out threshold. |
| 1072 | * |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1073 | * @total_out is an in/out parameter, must be set to the input length and will |
| 1074 | * be also used to return the total number of compressed bytes |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1075 | * |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1076 | * @max_out tells us the max number of bytes that we're allowed to |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1077 | * stuff into pages |
| 1078 | */ |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1079 | int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1080 | u64 start, struct page **pages, |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1081 | unsigned long *out_pages, |
| 1082 | unsigned long *total_in, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 1083 | unsigned long *total_out) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1084 | { |
| 1085 | struct list_head *workspace; |
| 1086 | int ret; |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1087 | int type = type_level & 0xF; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1088 | |
| 1089 | workspace = find_workspace(type); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1090 | |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1091 | btrfs_compress_op[type - 1]->set_level(workspace, type_level); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1092 | ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping, |
David Sterba | 38c3146 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1093 | start, pages, |
David Sterba | 4d3a800 | 2017-02-14 19:04:07 +0100 | [diff] [blame] | 1094 | out_pages, |
David Sterba | e5d7490 | 2017-02-14 19:45:05 +0100 | [diff] [blame] | 1095 | total_in, total_out); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1096 | free_workspace(type, workspace); |
| 1097 | return ret; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * pages_in is an array of pages with compressed data. |
| 1102 | * |
| 1103 | * disk_start is the starting logical offset of this array in the file |
| 1104 | * |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1105 | * orig_bio contains the pages from the file that we want to decompress into |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1106 | * |
| 1107 | * srclen is the number of bytes in pages_in |
| 1108 | * |
| 1109 | * The basic idea is that we have a bio that was created by readpages. |
| 1110 | * The pages in the bio are for the uncompressed data, and they may not |
| 1111 | * be contiguous. They all correspond to the range of bytes covered by |
| 1112 | * the compressed extent. |
| 1113 | */ |
Anand Jain | 8140dc3 | 2017-05-26 15:44:58 +0800 | [diff] [blame] | 1114 | static int btrfs_decompress_bio(struct compressed_bio *cb) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1115 | { |
| 1116 | struct list_head *workspace; |
| 1117 | int ret; |
Anand Jain | 8140dc3 | 2017-05-26 15:44:58 +0800 | [diff] [blame] | 1118 | int type = cb->compress_type; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1119 | |
| 1120 | workspace = find_workspace(type); |
Anand Jain | e1ddce7 | 2017-05-26 15:44:59 +0800 | [diff] [blame] | 1121 | ret = btrfs_compress_op[type - 1]->decompress_bio(workspace, cb); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1122 | free_workspace(type, workspace); |
Anand Jain | e1ddce7 | 2017-05-26 15:44:59 +0800 | [diff] [blame] | 1123 | |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1124 | return ret; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * a less complex decompression routine. Our compressed data fits in a |
| 1129 | * single page, and we want to read a single page out of it. |
| 1130 | * start_byte tells us the offset into the compressed data we're interested in |
| 1131 | */ |
| 1132 | int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page, |
| 1133 | unsigned long start_byte, size_t srclen, size_t destlen) |
| 1134 | { |
| 1135 | struct list_head *workspace; |
| 1136 | int ret; |
| 1137 | |
| 1138 | workspace = find_workspace(type); |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1139 | |
| 1140 | ret = btrfs_compress_op[type-1]->decompress(workspace, data_in, |
| 1141 | dest_page, start_byte, |
| 1142 | srclen, destlen); |
| 1143 | |
| 1144 | free_workspace(type, workspace); |
| 1145 | return ret; |
| 1146 | } |
| 1147 | |
Alexey Charkov | 8e4eef7 | 2011-02-02 21:15:35 +0000 | [diff] [blame] | 1148 | void btrfs_exit_compress(void) |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 1149 | { |
| 1150 | free_workspaces(); |
| 1151 | } |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1152 | |
| 1153 | /* |
| 1154 | * Copy uncompressed data from working buffer to pages. |
| 1155 | * |
| 1156 | * buf_start is the byte offset we're of the start of our workspace buffer. |
| 1157 | * |
| 1158 | * total_out is the last byte of the buffer |
| 1159 | */ |
David Sterba | 14a3357 | 2017-02-14 17:58:04 +0100 | [diff] [blame] | 1160 | int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start, |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1161 | unsigned long total_out, u64 disk_start, |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1162 | struct bio *bio) |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1163 | { |
| 1164 | unsigned long buf_offset; |
| 1165 | unsigned long current_buf_start; |
| 1166 | unsigned long start_byte; |
Omar Sandoval | 6e78b3f | 2017-02-10 15:03:35 -0800 | [diff] [blame] | 1167 | unsigned long prev_start_byte; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1168 | unsigned long working_bytes = total_out - buf_start; |
| 1169 | unsigned long bytes; |
| 1170 | char *kaddr; |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1171 | struct bio_vec bvec = bio_iter_iovec(bio, bio->bi_iter); |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1172 | |
| 1173 | /* |
| 1174 | * start byte is the first byte of the page we're currently |
| 1175 | * copying into relative to the start of the compressed data. |
| 1176 | */ |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1177 | start_byte = page_offset(bvec.bv_page) - disk_start; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1178 | |
| 1179 | /* we haven't yet hit data corresponding to this page */ |
| 1180 | if (total_out <= start_byte) |
| 1181 | return 1; |
| 1182 | |
| 1183 | /* |
| 1184 | * the start of the data we care about is offset into |
| 1185 | * the middle of our working buffer |
| 1186 | */ |
| 1187 | if (total_out > start_byte && buf_start < start_byte) { |
| 1188 | buf_offset = start_byte - buf_start; |
| 1189 | working_bytes -= buf_offset; |
| 1190 | } else { |
| 1191 | buf_offset = 0; |
| 1192 | } |
| 1193 | current_buf_start = buf_start; |
| 1194 | |
| 1195 | /* copy bytes from the working buffer into the pages */ |
| 1196 | while (working_bytes > 0) { |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1197 | bytes = min_t(unsigned long, bvec.bv_len, |
| 1198 | PAGE_SIZE - buf_offset); |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1199 | bytes = min(bytes, working_bytes); |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1200 | |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1201 | kaddr = kmap_atomic(bvec.bv_page); |
| 1202 | memcpy(kaddr + bvec.bv_offset, buf + buf_offset, bytes); |
| 1203 | kunmap_atomic(kaddr); |
| 1204 | flush_dcache_page(bvec.bv_page); |
| 1205 | |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1206 | buf_offset += bytes; |
| 1207 | working_bytes -= bytes; |
| 1208 | current_buf_start += bytes; |
| 1209 | |
| 1210 | /* check if we need to pick another page */ |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1211 | bio_advance(bio, bytes); |
| 1212 | if (!bio->bi_iter.bi_size) |
| 1213 | return 0; |
| 1214 | bvec = bio_iter_iovec(bio, bio->bi_iter); |
Omar Sandoval | 6e78b3f | 2017-02-10 15:03:35 -0800 | [diff] [blame] | 1215 | prev_start_byte = start_byte; |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1216 | start_byte = page_offset(bvec.bv_page) - disk_start; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1217 | |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1218 | /* |
Omar Sandoval | 6e78b3f | 2017-02-10 15:03:35 -0800 | [diff] [blame] | 1219 | * We need to make sure we're only adjusting |
| 1220 | * our offset into compression working buffer when |
| 1221 | * we're switching pages. Otherwise we can incorrectly |
| 1222 | * keep copying when we were actually done. |
Christoph Hellwig | 974b1ad | 2016-11-25 09:07:46 +0100 | [diff] [blame] | 1223 | */ |
Omar Sandoval | 6e78b3f | 2017-02-10 15:03:35 -0800 | [diff] [blame] | 1224 | if (start_byte != prev_start_byte) { |
| 1225 | /* |
| 1226 | * make sure our new page is covered by this |
| 1227 | * working buffer |
| 1228 | */ |
| 1229 | if (total_out <= start_byte) |
| 1230 | return 1; |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1231 | |
Omar Sandoval | 6e78b3f | 2017-02-10 15:03:35 -0800 | [diff] [blame] | 1232 | /* |
| 1233 | * the next page in the biovec might not be adjacent |
| 1234 | * to the last page, but it might still be found |
| 1235 | * inside this working buffer. bump our offset pointer |
| 1236 | */ |
| 1237 | if (total_out > start_byte && |
| 1238 | current_buf_start < start_byte) { |
| 1239 | buf_offset = start_byte - buf_start; |
| 1240 | working_bytes = total_out - start_byte; |
| 1241 | current_buf_start = buf_start + buf_offset; |
| 1242 | } |
Li Zefan | 3a39c18 | 2010-11-08 15:22:19 +0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | return 1; |
| 1247 | } |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1248 | |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 1249 | /* |
| 1250 | * Shannon Entropy calculation |
| 1251 | * |
| 1252 | * Pure byte distribution analysis fails to determine compressiability of data. |
| 1253 | * Try calculating entropy to estimate the average minimum number of bits |
| 1254 | * needed to encode the sampled data. |
| 1255 | * |
| 1256 | * For convenience, return the percentage of needed bits, instead of amount of |
| 1257 | * bits directly. |
| 1258 | * |
| 1259 | * @ENTROPY_LVL_ACEPTABLE - below that threshold, sample has low byte entropy |
| 1260 | * and can be compressible with high probability |
| 1261 | * |
| 1262 | * @ENTROPY_LVL_HIGH - data are not compressible with high probability |
| 1263 | * |
| 1264 | * Use of ilog2() decreases precision, we lower the LVL to 5 to compensate. |
| 1265 | */ |
| 1266 | #define ENTROPY_LVL_ACEPTABLE (65) |
| 1267 | #define ENTROPY_LVL_HIGH (80) |
| 1268 | |
| 1269 | /* |
| 1270 | * For increasead precision in shannon_entropy calculation, |
| 1271 | * let's do pow(n, M) to save more digits after comma: |
| 1272 | * |
| 1273 | * - maximum int bit length is 64 |
| 1274 | * - ilog2(MAX_SAMPLE_SIZE) -> 13 |
| 1275 | * - 13 * 4 = 52 < 64 -> M = 4 |
| 1276 | * |
| 1277 | * So use pow(n, 4). |
| 1278 | */ |
| 1279 | static inline u32 ilog2_w(u64 n) |
| 1280 | { |
| 1281 | return ilog2(n * n * n * n); |
| 1282 | } |
| 1283 | |
| 1284 | static u32 shannon_entropy(struct heuristic_ws *ws) |
| 1285 | { |
| 1286 | const u32 entropy_max = 8 * ilog2_w(2); |
| 1287 | u32 entropy_sum = 0; |
| 1288 | u32 p, p_base, sz_base; |
| 1289 | u32 i; |
| 1290 | |
| 1291 | sz_base = ilog2_w(ws->sample_size); |
| 1292 | for (i = 0; i < BUCKET_SIZE && ws->bucket[i].count > 0; i++) { |
| 1293 | p = ws->bucket[i].count; |
| 1294 | p_base = ilog2_w(p); |
| 1295 | entropy_sum += p * (sz_base - p_base); |
| 1296 | } |
| 1297 | |
| 1298 | entropy_sum /= ws->sample_size; |
| 1299 | return entropy_sum * 100 / entropy_max; |
| 1300 | } |
| 1301 | |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1302 | #define RADIX_BASE 4U |
| 1303 | #define COUNTERS_SIZE (1U << RADIX_BASE) |
Timofey Titovets | 858177d | 2017-09-28 17:33:41 +0300 | [diff] [blame] | 1304 | |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1305 | static u8 get4bits(u64 num, int shift) { |
| 1306 | u8 low4bits; |
| 1307 | |
| 1308 | num >>= shift; |
| 1309 | /* Reverse order */ |
| 1310 | low4bits = (COUNTERS_SIZE - 1) - (num % COUNTERS_SIZE); |
| 1311 | return low4bits; |
| 1312 | } |
| 1313 | |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1314 | /* |
| 1315 | * Use 4 bits as radix base |
| 1316 | * Use 16 u32 counters for calculating new possition in buf array |
| 1317 | * |
| 1318 | * @array - array that will be sorted |
| 1319 | * @array_buf - buffer array to store sorting results |
| 1320 | * must be equal in size to @array |
| 1321 | * @num - array size |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1322 | */ |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1323 | 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^] | 1324 | int num) |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1325 | { |
| 1326 | u64 max_num; |
| 1327 | u64 buf_num; |
| 1328 | u32 counters[COUNTERS_SIZE]; |
| 1329 | u32 new_addr; |
| 1330 | u32 addr; |
| 1331 | int bitlen; |
| 1332 | int shift; |
| 1333 | int i; |
| 1334 | |
| 1335 | /* |
| 1336 | * Try avoid useless loop iterations for small numbers stored in big |
| 1337 | * counters. Example: 48 33 4 ... in 64bit array |
| 1338 | */ |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1339 | max_num = array[0].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1340 | for (i = 1; i < num; i++) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1341 | buf_num = array[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1342 | if (buf_num > max_num) |
| 1343 | max_num = buf_num; |
| 1344 | } |
| 1345 | |
| 1346 | buf_num = ilog2(max_num); |
| 1347 | bitlen = ALIGN(buf_num, RADIX_BASE * 2); |
| 1348 | |
| 1349 | shift = 0; |
| 1350 | while (shift < bitlen) { |
| 1351 | memset(counters, 0, sizeof(counters)); |
| 1352 | |
| 1353 | for (i = 0; i < num; i++) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1354 | buf_num = array[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1355 | addr = get4bits(buf_num, shift); |
| 1356 | counters[addr]++; |
| 1357 | } |
| 1358 | |
| 1359 | for (i = 1; i < COUNTERS_SIZE; i++) |
| 1360 | counters[i] += counters[i - 1]; |
| 1361 | |
| 1362 | for (i = num - 1; i >= 0; i--) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1363 | buf_num = array[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1364 | addr = get4bits(buf_num, shift); |
| 1365 | counters[addr]--; |
| 1366 | new_addr = counters[addr]; |
David Sterba | 7add17b | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1367 | array_buf[new_addr] = array[i]; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | shift += RADIX_BASE; |
| 1371 | |
| 1372 | /* |
| 1373 | * Normal radix expects to move data from a temporary array, to |
| 1374 | * the main one. But that requires some CPU time. Avoid that |
| 1375 | * by doing another sort iteration to original array instead of |
| 1376 | * memcpy() |
| 1377 | */ |
| 1378 | memset(counters, 0, sizeof(counters)); |
| 1379 | |
| 1380 | for (i = 0; i < num; i ++) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1381 | buf_num = array_buf[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1382 | addr = get4bits(buf_num, shift); |
| 1383 | counters[addr]++; |
| 1384 | } |
| 1385 | |
| 1386 | for (i = 1; i < COUNTERS_SIZE; i++) |
| 1387 | counters[i] += counters[i - 1]; |
| 1388 | |
| 1389 | for (i = num - 1; i >= 0; i--) { |
David Sterba | 23ae8c6 | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1390 | buf_num = array_buf[i].count; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1391 | addr = get4bits(buf_num, shift); |
| 1392 | counters[addr]--; |
| 1393 | new_addr = counters[addr]; |
David Sterba | 7add17b | 2017-12-12 20:35:02 +0100 | [diff] [blame] | 1394 | array[new_addr] = array_buf[i]; |
Timofey Titovets | 440c840 | 2017-12-04 00:30:33 +0300 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | shift += RADIX_BASE; |
| 1398 | } |
Timofey Titovets | 858177d | 2017-09-28 17:33:41 +0300 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Size of the core byte set - how many bytes cover 90% of the sample |
| 1403 | * |
| 1404 | * There are several types of structured binary data that use nearly all byte |
| 1405 | * values. The distribution can be uniform and counts in all buckets will be |
| 1406 | * nearly the same (eg. encrypted data). Unlikely to be compressible. |
| 1407 | * |
| 1408 | * Other possibility is normal (Gaussian) distribution, where the data could |
| 1409 | * be potentially compressible, but we have to take a few more steps to decide |
| 1410 | * how much. |
| 1411 | * |
| 1412 | * @BYTE_CORE_SET_LOW - main part of byte values repeated frequently, |
| 1413 | * compression algo can easy fix that |
| 1414 | * @BYTE_CORE_SET_HIGH - data have uniform distribution and with high |
| 1415 | * probability is not compressible |
| 1416 | */ |
| 1417 | #define BYTE_CORE_SET_LOW (64) |
| 1418 | #define BYTE_CORE_SET_HIGH (200) |
| 1419 | |
| 1420 | static int byte_core_set_size(struct heuristic_ws *ws) |
| 1421 | { |
| 1422 | u32 i; |
| 1423 | u32 coreset_sum = 0; |
| 1424 | const u32 core_set_threshold = ws->sample_size * 90 / 100; |
| 1425 | struct bucket_item *bucket = ws->bucket; |
| 1426 | |
| 1427 | /* Sort in reverse order */ |
David Sterba | 36243c9 | 2017-12-12 20:35:02 +0100 | [diff] [blame^] | 1428 | radix_sort(ws->bucket, ws->bucket_b, BUCKET_SIZE); |
Timofey Titovets | 858177d | 2017-09-28 17:33:41 +0300 | [diff] [blame] | 1429 | |
| 1430 | for (i = 0; i < BYTE_CORE_SET_LOW; i++) |
| 1431 | coreset_sum += bucket[i].count; |
| 1432 | |
| 1433 | if (coreset_sum > core_set_threshold) |
| 1434 | return i; |
| 1435 | |
| 1436 | for (; i < BYTE_CORE_SET_HIGH && bucket[i].count > 0; i++) { |
| 1437 | coreset_sum += bucket[i].count; |
| 1438 | if (coreset_sum > core_set_threshold) |
| 1439 | break; |
| 1440 | } |
| 1441 | |
| 1442 | return i; |
| 1443 | } |
| 1444 | |
Timofey Titovets | a288e92 | 2017-09-28 17:33:40 +0300 | [diff] [blame] | 1445 | /* |
| 1446 | * Count byte values in buckets. |
| 1447 | * This heuristic can detect textual data (configs, xml, json, html, etc). |
| 1448 | * Because in most text-like data byte set is restricted to limited number of |
| 1449 | * possible characters, and that restriction in most cases makes data easy to |
| 1450 | * compress. |
| 1451 | * |
| 1452 | * @BYTE_SET_THRESHOLD - consider all data within this byte set size: |
| 1453 | * less - compressible |
| 1454 | * more - need additional analysis |
| 1455 | */ |
| 1456 | #define BYTE_SET_THRESHOLD (64) |
| 1457 | |
| 1458 | static u32 byte_set_size(const struct heuristic_ws *ws) |
| 1459 | { |
| 1460 | u32 i; |
| 1461 | u32 byte_set_size = 0; |
| 1462 | |
| 1463 | for (i = 0; i < BYTE_SET_THRESHOLD; i++) { |
| 1464 | if (ws->bucket[i].count > 0) |
| 1465 | byte_set_size++; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * Continue collecting count of byte values in buckets. If the byte |
| 1470 | * set size is bigger then the threshold, it's pointless to continue, |
| 1471 | * the detection technique would fail for this type of data. |
| 1472 | */ |
| 1473 | for (; i < BUCKET_SIZE; i++) { |
| 1474 | if (ws->bucket[i].count > 0) { |
| 1475 | byte_set_size++; |
| 1476 | if (byte_set_size > BYTE_SET_THRESHOLD) |
| 1477 | return byte_set_size; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | return byte_set_size; |
| 1482 | } |
| 1483 | |
Timofey Titovets | 1fe4f6f | 2017-09-28 17:33:39 +0300 | [diff] [blame] | 1484 | static bool sample_repeated_patterns(struct heuristic_ws *ws) |
| 1485 | { |
| 1486 | const u32 half_of_sample = ws->sample_size / 2; |
| 1487 | const u8 *data = ws->sample; |
| 1488 | |
| 1489 | return memcmp(&data[0], &data[half_of_sample], half_of_sample) == 0; |
| 1490 | } |
| 1491 | |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1492 | static void heuristic_collect_sample(struct inode *inode, u64 start, u64 end, |
| 1493 | struct heuristic_ws *ws) |
| 1494 | { |
| 1495 | struct page *page; |
| 1496 | u64 index, index_end; |
| 1497 | u32 i, curr_sample_pos; |
| 1498 | u8 *in_data; |
| 1499 | |
| 1500 | /* |
| 1501 | * Compression handles the input data by chunks of 128KiB |
| 1502 | * (defined by BTRFS_MAX_UNCOMPRESSED) |
| 1503 | * |
| 1504 | * We do the same for the heuristic and loop over the whole range. |
| 1505 | * |
| 1506 | * MAX_SAMPLE_SIZE - calculated under assumption that heuristic will |
| 1507 | * process no more than BTRFS_MAX_UNCOMPRESSED at a time. |
| 1508 | */ |
| 1509 | if (end - start > BTRFS_MAX_UNCOMPRESSED) |
| 1510 | end = start + BTRFS_MAX_UNCOMPRESSED; |
| 1511 | |
| 1512 | index = start >> PAGE_SHIFT; |
| 1513 | index_end = end >> PAGE_SHIFT; |
| 1514 | |
| 1515 | /* Don't miss unaligned end */ |
| 1516 | if (!IS_ALIGNED(end, PAGE_SIZE)) |
| 1517 | index_end++; |
| 1518 | |
| 1519 | curr_sample_pos = 0; |
| 1520 | while (index < index_end) { |
| 1521 | page = find_get_page(inode->i_mapping, index); |
| 1522 | in_data = kmap(page); |
| 1523 | /* Handle case where the start is not aligned to PAGE_SIZE */ |
| 1524 | i = start % PAGE_SIZE; |
| 1525 | while (i < PAGE_SIZE - SAMPLING_READ_SIZE) { |
| 1526 | /* Don't sample any garbage from the last page */ |
| 1527 | if (start > end - SAMPLING_READ_SIZE) |
| 1528 | break; |
| 1529 | memcpy(&ws->sample[curr_sample_pos], &in_data[i], |
| 1530 | SAMPLING_READ_SIZE); |
| 1531 | i += SAMPLING_INTERVAL; |
| 1532 | start += SAMPLING_INTERVAL; |
| 1533 | curr_sample_pos += SAMPLING_READ_SIZE; |
| 1534 | } |
| 1535 | kunmap(page); |
| 1536 | put_page(page); |
| 1537 | |
| 1538 | index++; |
| 1539 | } |
| 1540 | |
| 1541 | ws->sample_size = curr_sample_pos; |
| 1542 | } |
| 1543 | |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1544 | /* |
| 1545 | * Compression heuristic. |
| 1546 | * |
| 1547 | * For now is's a naive and optimistic 'return true', we'll extend the logic to |
| 1548 | * quickly (compared to direct compression) detect data characteristics |
| 1549 | * (compressible/uncompressible) to avoid wasting CPU time on uncompressible |
| 1550 | * data. |
| 1551 | * |
| 1552 | * The following types of analysis can be performed: |
| 1553 | * - detect mostly zero data |
| 1554 | * - detect data with low "byte set" size (text, etc) |
| 1555 | * - detect data with low/high "core byte" set |
| 1556 | * |
| 1557 | * Return non-zero if the compression should be done, 0 otherwise. |
| 1558 | */ |
| 1559 | int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end) |
| 1560 | { |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1561 | struct list_head *ws_list = __find_workspace(0, true); |
| 1562 | struct heuristic_ws *ws; |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1563 | u32 i; |
| 1564 | u8 byte; |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 1565 | int ret = 0; |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1566 | |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1567 | ws = list_entry(ws_list, struct heuristic_ws, list); |
| 1568 | |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1569 | heuristic_collect_sample(inode, start, end, ws); |
| 1570 | |
Timofey Titovets | 1fe4f6f | 2017-09-28 17:33:39 +0300 | [diff] [blame] | 1571 | if (sample_repeated_patterns(ws)) { |
| 1572 | ret = 1; |
| 1573 | goto out; |
| 1574 | } |
| 1575 | |
Timofey Titovets | a440d48 | 2017-09-28 17:33:38 +0300 | [diff] [blame] | 1576 | memset(ws->bucket, 0, sizeof(*ws->bucket)*BUCKET_SIZE); |
| 1577 | |
| 1578 | for (i = 0; i < ws->sample_size; i++) { |
| 1579 | byte = ws->sample[i]; |
| 1580 | ws->bucket[byte].count++; |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1581 | } |
| 1582 | |
Timofey Titovets | a288e92 | 2017-09-28 17:33:40 +0300 | [diff] [blame] | 1583 | i = byte_set_size(ws); |
| 1584 | if (i < BYTE_SET_THRESHOLD) { |
| 1585 | ret = 2; |
| 1586 | goto out; |
| 1587 | } |
| 1588 | |
Timofey Titovets | 858177d | 2017-09-28 17:33:41 +0300 | [diff] [blame] | 1589 | i = byte_core_set_size(ws); |
| 1590 | if (i <= BYTE_CORE_SET_LOW) { |
| 1591 | ret = 3; |
| 1592 | goto out; |
| 1593 | } |
| 1594 | |
| 1595 | if (i >= BYTE_CORE_SET_HIGH) { |
| 1596 | ret = 0; |
| 1597 | goto out; |
| 1598 | } |
| 1599 | |
Timofey Titovets | 1956243 | 2017-10-08 16:11:59 +0300 | [diff] [blame] | 1600 | i = shannon_entropy(ws); |
| 1601 | if (i <= ENTROPY_LVL_ACEPTABLE) { |
| 1602 | ret = 4; |
| 1603 | goto out; |
| 1604 | } |
| 1605 | |
| 1606 | /* |
| 1607 | * For the levels below ENTROPY_LVL_HIGH, additional analysis would be |
| 1608 | * needed to give green light to compression. |
| 1609 | * |
| 1610 | * For now just assume that compression at that level is not worth the |
| 1611 | * resources because: |
| 1612 | * |
| 1613 | * 1. it is possible to defrag the data later |
| 1614 | * |
| 1615 | * 2. the data would turn out to be hardly compressible, eg. 150 byte |
| 1616 | * values, every bucket has counter at level ~54. The heuristic would |
| 1617 | * be confused. This can happen when data have some internal repeated |
| 1618 | * patterns like "abbacbbc...". This can be detected by analyzing |
| 1619 | * pairs of bytes, which is too costly. |
| 1620 | */ |
| 1621 | if (i < ENTROPY_LVL_HIGH) { |
| 1622 | ret = 5; |
| 1623 | goto out; |
| 1624 | } else { |
| 1625 | ret = 0; |
| 1626 | goto out; |
| 1627 | } |
| 1628 | |
Timofey Titovets | 1fe4f6f | 2017-09-28 17:33:39 +0300 | [diff] [blame] | 1629 | out: |
Timofey Titovets | 4e439a0 | 2017-09-28 17:33:36 +0300 | [diff] [blame] | 1630 | __free_workspace(0, ws_list, true); |
Timofey Titovets | c2fcdcd | 2017-07-17 16:52:58 +0300 | [diff] [blame] | 1631 | return ret; |
| 1632 | } |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1633 | |
| 1634 | unsigned int btrfs_compress_str2level(const char *str) |
| 1635 | { |
| 1636 | if (strncmp(str, "zlib", 4) != 0) |
| 1637 | return 0; |
| 1638 | |
Adam Borowski | fa4d885 | 2017-09-15 17:36:58 +0200 | [diff] [blame] | 1639 | /* Accepted form: zlib:1 up to zlib:9 and nothing left after the number */ |
| 1640 | if (str[4] == ':' && '1' <= str[5] && str[5] <= '9' && str[6] == 0) |
| 1641 | return str[5] - '0'; |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1642 | |
Qu Wenruo | eae8d82 | 2017-11-06 10:43:18 +0800 | [diff] [blame] | 1643 | return BTRFS_ZLIB_DEFAULT_LEVEL; |
David Sterba | f51d2b5 | 2017-09-15 17:36:57 +0200 | [diff] [blame] | 1644 | } |