]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/btrfs/compression.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / compression.c
CommitLineData
c8b97818
CM
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>
c8b97818
CM
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>
5a0e3ad6 34#include <linux/slab.h>
fe308533 35#include <linux/sched/mm.h>
c8b97818
CM
36#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
40#include "volumes.h"
41#include "ordered-data.h"
c8b97818
CM
42#include "compression.h"
43#include "extent_io.h"
44#include "extent_map.h"
45
8140dc30 46static int btrfs_decompress_bio(struct compressed_bio *cb);
48a3b636 47
2ff7e61e 48static inline int compressed_bio_size(struct btrfs_fs_info *fs_info,
d20f7043
CM
49 unsigned long disk_size)
50{
0b246afa 51 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
6c41761f 52
d20f7043 53 return sizeof(struct compressed_bio) +
0b246afa 54 (DIV_ROUND_UP(disk_size, fs_info->sectorsize)) * csum_size;
d20f7043
CM
55}
56
f898ac6a 57static int check_compressed_csum(struct btrfs_inode *inode,
d20f7043
CM
58 struct compressed_bio *cb,
59 u64 disk_start)
60{
61 int ret;
d20f7043
CM
62 struct page *page;
63 unsigned long i;
64 char *kaddr;
65 u32 csum;
66 u32 *cb_sum = &cb->sums;
67
f898ac6a 68 if (inode->flags & BTRFS_INODE_NODATASUM)
d20f7043
CM
69 return 0;
70
71 for (i = 0; i < cb->nr_pages; i++) {
72 page = cb->compressed_pages[i];
73 csum = ~(u32)0;
74
7ac687d9 75 kaddr = kmap_atomic(page);
09cbfeaf 76 csum = btrfs_csum_data(kaddr, csum, PAGE_SIZE);
0b5e3daf 77 btrfs_csum_final(csum, (u8 *)&csum);
7ac687d9 78 kunmap_atomic(kaddr);
d20f7043
CM
79
80 if (csum != *cb_sum) {
f898ac6a 81 btrfs_print_data_csum_error(inode, disk_start, csum,
0970a22e 82 *cb_sum, cb->mirror_num);
d20f7043
CM
83 ret = -EIO;
84 goto fail;
85 }
86 cb_sum++;
87
88 }
89 ret = 0;
90fail:
91 return ret;
92}
93
c8b97818
CM
94/* when we finish reading compressed pages from the disk, we
95 * decompress them and then run the bio end_io routines on the
96 * decompressed pages (in the inode address space).
97 *
98 * This allows the checksumming and other IO error handling routines
99 * to work normally
100 *
101 * The compressed pages are freed here, and it must be run
102 * in process context
103 */
4246a0b6 104static void end_compressed_bio_read(struct bio *bio)
c8b97818 105{
c8b97818
CM
106 struct compressed_bio *cb = bio->bi_private;
107 struct inode *inode;
108 struct page *page;
109 unsigned long index;
110 int ret;
111
4e4cbee9 112 if (bio->bi_status)
c8b97818
CM
113 cb->errors = 1;
114
115 /* if there are more bios still pending for this compressed
116 * extent, just exit
117 */
a50299ae 118 if (!refcount_dec_and_test(&cb->pending_bios))
c8b97818
CM
119 goto out;
120
d20f7043 121 inode = cb->inode;
f898ac6a 122 ret = check_compressed_csum(BTRFS_I(inode), cb,
4f024f37 123 (u64)bio->bi_iter.bi_sector << 9);
d20f7043
CM
124 if (ret)
125 goto csum_failed;
126
c8b97818
CM
127 /* ok, we're the last bio for this extent, lets start
128 * the decompression.
129 */
8140dc30
AJ
130 ret = btrfs_decompress_bio(cb);
131
d20f7043 132csum_failed:
c8b97818
CM
133 if (ret)
134 cb->errors = 1;
135
136 /* release the compressed pages */
137 index = 0;
138 for (index = 0; index < cb->nr_pages; index++) {
139 page = cb->compressed_pages[index];
140 page->mapping = NULL;
09cbfeaf 141 put_page(page);
c8b97818
CM
142 }
143
144 /* do io completion on the original bio */
771ed689 145 if (cb->errors) {
c8b97818 146 bio_io_error(cb->orig_bio);
d20f7043 147 } else {
2c30c71b
KO
148 int i;
149 struct bio_vec *bvec;
d20f7043
CM
150
151 /*
152 * we have verified the checksum already, set page
153 * checked so the end_io handlers know about it
154 */
c09abff8 155 ASSERT(!bio_flagged(bio, BIO_CLONED));
2c30c71b 156 bio_for_each_segment_all(bvec, cb->orig_bio, i)
d20f7043 157 SetPageChecked(bvec->bv_page);
2c30c71b 158
4246a0b6 159 bio_endio(cb->orig_bio);
d20f7043 160 }
c8b97818
CM
161
162 /* finally free the cb struct */
163 kfree(cb->compressed_pages);
164 kfree(cb);
165out:
166 bio_put(bio);
167}
168
169/*
170 * Clear the writeback bits on all of the file
171 * pages for a compressed write
172 */
7bdcefc1
FM
173static noinline void end_compressed_writeback(struct inode *inode,
174 const struct compressed_bio *cb)
c8b97818 175{
09cbfeaf
KS
176 unsigned long index = cb->start >> PAGE_SHIFT;
177 unsigned long end_index = (cb->start + cb->len - 1) >> PAGE_SHIFT;
c8b97818
CM
178 struct page *pages[16];
179 unsigned long nr_pages = end_index - index + 1;
180 int i;
181 int ret;
182
7bdcefc1
FM
183 if (cb->errors)
184 mapping_set_error(inode->i_mapping, -EIO);
185
d397712b 186 while (nr_pages > 0) {
c8b97818 187 ret = find_get_pages_contig(inode->i_mapping, index,
5b050f04
CM
188 min_t(unsigned long,
189 nr_pages, ARRAY_SIZE(pages)), pages);
c8b97818
CM
190 if (ret == 0) {
191 nr_pages -= 1;
192 index += 1;
193 continue;
194 }
195 for (i = 0; i < ret; i++) {
7bdcefc1
FM
196 if (cb->errors)
197 SetPageError(pages[i]);
c8b97818 198 end_page_writeback(pages[i]);
09cbfeaf 199 put_page(pages[i]);
c8b97818
CM
200 }
201 nr_pages -= ret;
202 index += ret;
203 }
204 /* the inode may be gone now */
c8b97818
CM
205}
206
207/*
208 * do the cleanup once all the compressed pages hit the disk.
209 * This will clear writeback on the file pages and free the compressed
210 * pages.
211 *
212 * This also calls the writeback end hooks for the file pages so that
213 * metadata and checksums can be updated in the file.
214 */
4246a0b6 215static void end_compressed_bio_write(struct bio *bio)
c8b97818
CM
216{
217 struct extent_io_tree *tree;
218 struct compressed_bio *cb = bio->bi_private;
219 struct inode *inode;
220 struct page *page;
221 unsigned long index;
222
4e4cbee9 223 if (bio->bi_status)
c8b97818
CM
224 cb->errors = 1;
225
226 /* if there are more bios still pending for this compressed
227 * extent, just exit
228 */
a50299ae 229 if (!refcount_dec_and_test(&cb->pending_bios))
c8b97818
CM
230 goto out;
231
232 /* ok, we're the last bio for this extent, step one is to
233 * call back into the FS and do all the end_io operations
234 */
235 inode = cb->inode;
236 tree = &BTRFS_I(inode)->io_tree;
70b99e69 237 cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
c8b97818
CM
238 tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
239 cb->start,
240 cb->start + cb->len - 1,
7bdcefc1 241 NULL,
4e4cbee9 242 bio->bi_status ? 0 : 1);
70b99e69 243 cb->compressed_pages[0]->mapping = NULL;
c8b97818 244
7bdcefc1 245 end_compressed_writeback(inode, cb);
c8b97818
CM
246 /* note, our inode could be gone now */
247
248 /*
249 * release the compressed pages, these came from alloc_page and
250 * are not attached to the inode at all
251 */
252 index = 0;
253 for (index = 0; index < cb->nr_pages; index++) {
254 page = cb->compressed_pages[index];
255 page->mapping = NULL;
09cbfeaf 256 put_page(page);
c8b97818
CM
257 }
258
259 /* finally free the cb struct */
260 kfree(cb->compressed_pages);
261 kfree(cb);
262out:
263 bio_put(bio);
264}
265
266/*
267 * worker function to build and submit bios for previously compressed pages.
268 * The corresponding pages in the inode should be marked for writeback
269 * and the compressed pages should have a reference on them for dropping
270 * when the IO is complete.
271 *
272 * This also checksums the file bytes and gets things ready for
273 * the end io hooks.
274 */
4e4cbee9 275blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start,
c8b97818
CM
276 unsigned long len, u64 disk_start,
277 unsigned long compressed_len,
278 struct page **compressed_pages,
279 unsigned long nr_pages)
280{
0b246afa 281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
c8b97818 282 struct bio *bio = NULL;
c8b97818
CM
283 struct compressed_bio *cb;
284 unsigned long bytes_left;
285 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
306e16ce 286 int pg_index = 0;
c8b97818
CM
287 struct page *page;
288 u64 first_byte = disk_start;
289 struct block_device *bdev;
4e4cbee9 290 blk_status_t ret;
e55179b3 291 int skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
c8b97818 292
09cbfeaf 293 WARN_ON(start & ((u64)PAGE_SIZE - 1));
2ff7e61e 294 cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS);
dac97e51 295 if (!cb)
4e4cbee9 296 return BLK_STS_RESOURCE;
a50299ae 297 refcount_set(&cb->pending_bios, 0);
c8b97818
CM
298 cb->errors = 0;
299 cb->inode = inode;
300 cb->start = start;
301 cb->len = len;
d20f7043 302 cb->mirror_num = 0;
c8b97818
CM
303 cb->compressed_pages = compressed_pages;
304 cb->compressed_len = compressed_len;
305 cb->orig_bio = NULL;
306 cb->nr_pages = nr_pages;
307
0b246afa 308 bdev = fs_info->fs_devices->latest_bdev;
c8b97818 309
c821e7f3 310 bio = btrfs_bio_alloc(bdev, first_byte);
37226b21 311 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
c8b97818
CM
312 bio->bi_private = cb;
313 bio->bi_end_io = end_compressed_bio_write;
a50299ae 314 refcount_set(&cb->pending_bios, 1);
c8b97818
CM
315
316 /* create and submit bios for the compressed pages */
317 bytes_left = compressed_len;
306e16ce 318 for (pg_index = 0; pg_index < cb->nr_pages; pg_index++) {
4e4cbee9
CH
319 int submit = 0;
320
306e16ce 321 page = compressed_pages[pg_index];
c8b97818 322 page->mapping = inode->i_mapping;
4f024f37 323 if (bio->bi_iter.bi_size)
4e4cbee9 324 submit = io_tree->ops->merge_bio_hook(page, 0,
09cbfeaf 325 PAGE_SIZE,
c8b97818 326 bio, 0);
c8b97818 327
70b99e69 328 page->mapping = NULL;
4e4cbee9 329 if (submit || bio_add_page(bio, page, PAGE_SIZE, 0) <
09cbfeaf 330 PAGE_SIZE) {
c8b97818
CM
331 bio_get(bio);
332
af09abfe
CM
333 /*
334 * inc the count before we submit the bio so
335 * we know the end IO handler won't happen before
336 * we inc the count. Otherwise, the cb might get
337 * freed before we're done setting it up
338 */
a50299ae 339 refcount_inc(&cb->pending_bios);
0b246afa
JM
340 ret = btrfs_bio_wq_end_io(fs_info, bio,
341 BTRFS_WQ_ENDIO_DATA);
79787eaa 342 BUG_ON(ret); /* -ENOMEM */
c8b97818 343
e55179b3 344 if (!skip_sum) {
2ff7e61e 345 ret = btrfs_csum_one_bio(inode, bio, start, 1);
79787eaa 346 BUG_ON(ret); /* -ENOMEM */
e55179b3 347 }
d20f7043 348
2ff7e61e 349 ret = btrfs_map_bio(fs_info, bio, 0, 1);
f5daf2c7 350 if (ret) {
4e4cbee9 351 bio->bi_status = ret;
f5daf2c7
LB
352 bio_endio(bio);
353 }
c8b97818
CM
354
355 bio_put(bio);
356
c821e7f3 357 bio = btrfs_bio_alloc(bdev, first_byte);
37226b21 358 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
c8b97818
CM
359 bio->bi_private = cb;
360 bio->bi_end_io = end_compressed_bio_write;
09cbfeaf 361 bio_add_page(bio, page, PAGE_SIZE, 0);
c8b97818 362 }
09cbfeaf 363 if (bytes_left < PAGE_SIZE) {
0b246afa 364 btrfs_info(fs_info,
efe120a0 365 "bytes left %lu compress len %lu nr %lu",
cfbc246e
CM
366 bytes_left, cb->compressed_len, cb->nr_pages);
367 }
09cbfeaf
KS
368 bytes_left -= PAGE_SIZE;
369 first_byte += PAGE_SIZE;
771ed689 370 cond_resched();
c8b97818
CM
371 }
372 bio_get(bio);
373
0b246afa 374 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
79787eaa 375 BUG_ON(ret); /* -ENOMEM */
c8b97818 376
e55179b3 377 if (!skip_sum) {
2ff7e61e 378 ret = btrfs_csum_one_bio(inode, bio, start, 1);
79787eaa 379 BUG_ON(ret); /* -ENOMEM */
e55179b3 380 }
d20f7043 381
2ff7e61e 382 ret = btrfs_map_bio(fs_info, bio, 0, 1);
f5daf2c7 383 if (ret) {
4e4cbee9 384 bio->bi_status = ret;
f5daf2c7
LB
385 bio_endio(bio);
386 }
c8b97818
CM
387
388 bio_put(bio);
389 return 0;
390}
391
2a4d0c90
CH
392static u64 bio_end_offset(struct bio *bio)
393{
394 struct bio_vec *last = &bio->bi_io_vec[bio->bi_vcnt - 1];
395
396 return page_offset(last->bv_page) + last->bv_len + last->bv_offset;
397}
398
771ed689
CM
399static noinline int add_ra_bio_pages(struct inode *inode,
400 u64 compressed_end,
401 struct compressed_bio *cb)
402{
403 unsigned long end_index;
306e16ce 404 unsigned long pg_index;
771ed689
CM
405 u64 last_offset;
406 u64 isize = i_size_read(inode);
407 int ret;
408 struct page *page;
409 unsigned long nr_pages = 0;
410 struct extent_map *em;
411 struct address_space *mapping = inode->i_mapping;
771ed689
CM
412 struct extent_map_tree *em_tree;
413 struct extent_io_tree *tree;
414 u64 end;
415 int misses = 0;
416
2a4d0c90 417 last_offset = bio_end_offset(cb->orig_bio);
771ed689
CM
418 em_tree = &BTRFS_I(inode)->extent_tree;
419 tree = &BTRFS_I(inode)->io_tree;
420
421 if (isize == 0)
422 return 0;
423
09cbfeaf 424 end_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
771ed689 425
d397712b 426 while (last_offset < compressed_end) {
09cbfeaf 427 pg_index = last_offset >> PAGE_SHIFT;
771ed689 428
306e16ce 429 if (pg_index > end_index)
771ed689
CM
430 break;
431
432 rcu_read_lock();
306e16ce 433 page = radix_tree_lookup(&mapping->page_tree, pg_index);
771ed689 434 rcu_read_unlock();
0cd6144a 435 if (page && !radix_tree_exceptional_entry(page)) {
771ed689
CM
436 misses++;
437 if (misses > 4)
438 break;
439 goto next;
440 }
441
c62d2555
MH
442 page = __page_cache_alloc(mapping_gfp_constraint(mapping,
443 ~__GFP_FS));
771ed689
CM
444 if (!page)
445 break;
446
c62d2555 447 if (add_to_page_cache_lru(page, mapping, pg_index, GFP_NOFS)) {
09cbfeaf 448 put_page(page);
771ed689
CM
449 goto next;
450 }
451
09cbfeaf 452 end = last_offset + PAGE_SIZE - 1;
771ed689
CM
453 /*
454 * at this point, we have a locked page in the page cache
455 * for these bytes in the file. But, we have to make
456 * sure they map to this compressed extent on disk.
457 */
458 set_page_extent_mapped(page);
d0082371 459 lock_extent(tree, last_offset, end);
890871be 460 read_lock(&em_tree->lock);
771ed689 461 em = lookup_extent_mapping(em_tree, last_offset,
09cbfeaf 462 PAGE_SIZE);
890871be 463 read_unlock(&em_tree->lock);
771ed689
CM
464
465 if (!em || last_offset < em->start ||
09cbfeaf 466 (last_offset + PAGE_SIZE > extent_map_end(em)) ||
4f024f37 467 (em->block_start >> 9) != cb->orig_bio->bi_iter.bi_sector) {
771ed689 468 free_extent_map(em);
d0082371 469 unlock_extent(tree, last_offset, end);
771ed689 470 unlock_page(page);
09cbfeaf 471 put_page(page);
771ed689
CM
472 break;
473 }
474 free_extent_map(em);
475
476 if (page->index == end_index) {
477 char *userpage;
09cbfeaf 478 size_t zero_offset = isize & (PAGE_SIZE - 1);
771ed689
CM
479
480 if (zero_offset) {
481 int zeros;
09cbfeaf 482 zeros = PAGE_SIZE - zero_offset;
7ac687d9 483 userpage = kmap_atomic(page);
771ed689
CM
484 memset(userpage + zero_offset, 0, zeros);
485 flush_dcache_page(page);
7ac687d9 486 kunmap_atomic(userpage);
771ed689
CM
487 }
488 }
489
490 ret = bio_add_page(cb->orig_bio, page,
09cbfeaf 491 PAGE_SIZE, 0);
771ed689 492
09cbfeaf 493 if (ret == PAGE_SIZE) {
771ed689 494 nr_pages++;
09cbfeaf 495 put_page(page);
771ed689 496 } else {
d0082371 497 unlock_extent(tree, last_offset, end);
771ed689 498 unlock_page(page);
09cbfeaf 499 put_page(page);
771ed689
CM
500 break;
501 }
502next:
09cbfeaf 503 last_offset += PAGE_SIZE;
771ed689 504 }
771ed689
CM
505 return 0;
506}
507
c8b97818
CM
508/*
509 * for a compressed read, the bio we get passed has all the inode pages
510 * in it. We don't actually do IO on those pages but allocate new ones
511 * to hold the compressed pages on disk.
512 *
4f024f37 513 * bio->bi_iter.bi_sector points to the compressed extent on disk
c8b97818 514 * bio->bi_io_vec points to all of the inode pages
c8b97818
CM
515 *
516 * After the compressed pages are read, we copy the bytes into the
517 * bio we were passed and then call the bio end_io calls
518 */
4e4cbee9 519blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
c8b97818
CM
520 int mirror_num, unsigned long bio_flags)
521{
0b246afa 522 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
c8b97818
CM
523 struct extent_io_tree *tree;
524 struct extent_map_tree *em_tree;
525 struct compressed_bio *cb;
c8b97818
CM
526 unsigned long compressed_len;
527 unsigned long nr_pages;
306e16ce 528 unsigned long pg_index;
c8b97818
CM
529 struct page *page;
530 struct block_device *bdev;
531 struct bio *comp_bio;
4f024f37 532 u64 cur_disk_byte = (u64)bio->bi_iter.bi_sector << 9;
e04ca626
CM
533 u64 em_len;
534 u64 em_start;
c8b97818 535 struct extent_map *em;
4e4cbee9 536 blk_status_t ret = BLK_STS_RESOURCE;
15e3004a 537 int faili = 0;
d20f7043 538 u32 *sums;
c8b97818
CM
539
540 tree = &BTRFS_I(inode)->io_tree;
541 em_tree = &BTRFS_I(inode)->extent_tree;
542
543 /* we need the actual starting offset of this extent in the file */
890871be 544 read_lock(&em_tree->lock);
c8b97818
CM
545 em = lookup_extent_mapping(em_tree,
546 page_offset(bio->bi_io_vec->bv_page),
09cbfeaf 547 PAGE_SIZE);
890871be 548 read_unlock(&em_tree->lock);
285190d9 549 if (!em)
4e4cbee9 550 return BLK_STS_IOERR;
c8b97818 551
d20f7043 552 compressed_len = em->block_len;
2ff7e61e 553 cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS);
6b82ce8d 554 if (!cb)
555 goto out;
556
a50299ae 557 refcount_set(&cb->pending_bios, 0);
c8b97818
CM
558 cb->errors = 0;
559 cb->inode = inode;
d20f7043
CM
560 cb->mirror_num = mirror_num;
561 sums = &cb->sums;
c8b97818 562
ff5b7ee3 563 cb->start = em->orig_start;
e04ca626
CM
564 em_len = em->len;
565 em_start = em->start;
d20f7043 566
c8b97818 567 free_extent_map(em);
e04ca626 568 em = NULL;
c8b97818 569
81381053 570 cb->len = bio->bi_iter.bi_size;
c8b97818 571 cb->compressed_len = compressed_len;
261507a0 572 cb->compress_type = extent_compress_type(bio_flags);
c8b97818
CM
573 cb->orig_bio = bio;
574
09cbfeaf 575 nr_pages = DIV_ROUND_UP(compressed_len, PAGE_SIZE);
31e818fe 576 cb->compressed_pages = kcalloc(nr_pages, sizeof(struct page *),
c8b97818 577 GFP_NOFS);
6b82ce8d 578 if (!cb->compressed_pages)
579 goto fail1;
580
0b246afa 581 bdev = fs_info->fs_devices->latest_bdev;
c8b97818 582
306e16ce
DS
583 for (pg_index = 0; pg_index < nr_pages; pg_index++) {
584 cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
c8b97818 585 __GFP_HIGHMEM);
15e3004a
JB
586 if (!cb->compressed_pages[pg_index]) {
587 faili = pg_index - 1;
0e9350de 588 ret = BLK_STS_RESOURCE;
6b82ce8d 589 goto fail2;
15e3004a 590 }
c8b97818 591 }
15e3004a 592 faili = nr_pages - 1;
c8b97818
CM
593 cb->nr_pages = nr_pages;
594
7f042a83 595 add_ra_bio_pages(inode, em_start + em_len, cb);
771ed689 596
771ed689 597 /* include any pages we added in add_ra-bio_pages */
81381053 598 cb->len = bio->bi_iter.bi_size;
771ed689 599
c821e7f3 600 comp_bio = btrfs_bio_alloc(bdev, cur_disk_byte);
37226b21 601 bio_set_op_attrs (comp_bio, REQ_OP_READ, 0);
c8b97818
CM
602 comp_bio->bi_private = cb;
603 comp_bio->bi_end_io = end_compressed_bio_read;
a50299ae 604 refcount_set(&cb->pending_bios, 1);
c8b97818 605
306e16ce 606 for (pg_index = 0; pg_index < nr_pages; pg_index++) {
4e4cbee9
CH
607 int submit = 0;
608
306e16ce 609 page = cb->compressed_pages[pg_index];
c8b97818 610 page->mapping = inode->i_mapping;
09cbfeaf 611 page->index = em_start >> PAGE_SHIFT;
d20f7043 612
4f024f37 613 if (comp_bio->bi_iter.bi_size)
4e4cbee9 614 submit = tree->ops->merge_bio_hook(page, 0,
09cbfeaf 615 PAGE_SIZE,
c8b97818 616 comp_bio, 0);
c8b97818 617
70b99e69 618 page->mapping = NULL;
4e4cbee9 619 if (submit || bio_add_page(comp_bio, page, PAGE_SIZE, 0) <
09cbfeaf 620 PAGE_SIZE) {
c8b97818
CM
621 bio_get(comp_bio);
622
0b246afa
JM
623 ret = btrfs_bio_wq_end_io(fs_info, comp_bio,
624 BTRFS_WQ_ENDIO_DATA);
79787eaa 625 BUG_ON(ret); /* -ENOMEM */
c8b97818 626
af09abfe
CM
627 /*
628 * inc the count before we submit the bio so
629 * we know the end IO handler won't happen before
630 * we inc the count. Otherwise, the cb might get
631 * freed before we're done setting it up
632 */
a50299ae 633 refcount_inc(&cb->pending_bios);
af09abfe 634
6cbff00f 635 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
2ff7e61e
JM
636 ret = btrfs_lookup_bio_sums(inode, comp_bio,
637 sums);
79787eaa 638 BUG_ON(ret); /* -ENOMEM */
d20f7043 639 }
ed6078f7 640 sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size,
0b246afa 641 fs_info->sectorsize);
d20f7043 642
2ff7e61e 643 ret = btrfs_map_bio(fs_info, comp_bio, mirror_num, 0);
4246a0b6 644 if (ret) {
4e4cbee9 645 comp_bio->bi_status = ret;
4246a0b6
CH
646 bio_endio(comp_bio);
647 }
c8b97818
CM
648
649 bio_put(comp_bio);
650
c821e7f3 651 comp_bio = btrfs_bio_alloc(bdev, cur_disk_byte);
37226b21 652 bio_set_op_attrs(comp_bio, REQ_OP_READ, 0);
771ed689
CM
653 comp_bio->bi_private = cb;
654 comp_bio->bi_end_io = end_compressed_bio_read;
655
09cbfeaf 656 bio_add_page(comp_bio, page, PAGE_SIZE, 0);
c8b97818 657 }
09cbfeaf 658 cur_disk_byte += PAGE_SIZE;
c8b97818
CM
659 }
660 bio_get(comp_bio);
661
0b246afa 662 ret = btrfs_bio_wq_end_io(fs_info, comp_bio, BTRFS_WQ_ENDIO_DATA);
79787eaa 663 BUG_ON(ret); /* -ENOMEM */
c8b97818 664
c2db1073 665 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
2ff7e61e 666 ret = btrfs_lookup_bio_sums(inode, comp_bio, sums);
79787eaa 667 BUG_ON(ret); /* -ENOMEM */
c2db1073 668 }
d20f7043 669
2ff7e61e 670 ret = btrfs_map_bio(fs_info, comp_bio, mirror_num, 0);
4246a0b6 671 if (ret) {
4e4cbee9 672 comp_bio->bi_status = ret;
4246a0b6
CH
673 bio_endio(comp_bio);
674 }
c8b97818
CM
675
676 bio_put(comp_bio);
677 return 0;
6b82ce8d 678
679fail2:
15e3004a
JB
680 while (faili >= 0) {
681 __free_page(cb->compressed_pages[faili]);
682 faili--;
683 }
6b82ce8d 684
685 kfree(cb->compressed_pages);
686fail1:
687 kfree(cb);
688out:
689 free_extent_map(em);
690 return ret;
c8b97818 691}
261507a0 692
d9187649
BL
693static struct {
694 struct list_head idle_ws;
695 spinlock_t ws_lock;
6ac10a6a
DS
696 /* Number of free workspaces */
697 int free_ws;
698 /* Total number of allocated workspaces */
699 atomic_t total_ws;
700 /* Waiters for a free workspace */
d9187649
BL
701 wait_queue_head_t ws_wait;
702} btrfs_comp_ws[BTRFS_COMPRESS_TYPES];
261507a0 703
e8c9f186 704static const struct btrfs_compress_op * const btrfs_compress_op[] = {
261507a0 705 &btrfs_zlib_compress,
a6fa6fae 706 &btrfs_lzo_compress,
261507a0
LZ
707};
708
143bede5 709void __init btrfs_init_compress(void)
261507a0
LZ
710{
711 int i;
712
713 for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
f77dd0d6
DS
714 struct list_head *workspace;
715
d9187649
BL
716 INIT_LIST_HEAD(&btrfs_comp_ws[i].idle_ws);
717 spin_lock_init(&btrfs_comp_ws[i].ws_lock);
6ac10a6a 718 atomic_set(&btrfs_comp_ws[i].total_ws, 0);
d9187649 719 init_waitqueue_head(&btrfs_comp_ws[i].ws_wait);
f77dd0d6
DS
720
721 /*
722 * Preallocate one workspace for each compression type so
723 * we can guarantee forward progress in the worst case
724 */
725 workspace = btrfs_compress_op[i]->alloc_workspace();
726 if (IS_ERR(workspace)) {
62e85577 727 pr_warn("BTRFS: cannot preallocate compression workspace, will try later\n");
f77dd0d6
DS
728 } else {
729 atomic_set(&btrfs_comp_ws[i].total_ws, 1);
730 btrfs_comp_ws[i].free_ws = 1;
731 list_add(workspace, &btrfs_comp_ws[i].idle_ws);
732 }
261507a0 733 }
261507a0
LZ
734}
735
736/*
e721e49d
DS
737 * This finds an available workspace or allocates a new one.
738 * If it's not possible to allocate a new one, waits until there's one.
739 * Preallocation makes a forward progress guarantees and we do not return
740 * errors.
261507a0
LZ
741 */
742static struct list_head *find_workspace(int type)
743{
744 struct list_head *workspace;
745 int cpus = num_online_cpus();
746 int idx = type - 1;
fe308533 747 unsigned nofs_flag;
261507a0 748
d9187649
BL
749 struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
750 spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
6ac10a6a 751 atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
d9187649 752 wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
6ac10a6a 753 int *free_ws = &btrfs_comp_ws[idx].free_ws;
261507a0 754again:
d9187649
BL
755 spin_lock(ws_lock);
756 if (!list_empty(idle_ws)) {
757 workspace = idle_ws->next;
261507a0 758 list_del(workspace);
6ac10a6a 759 (*free_ws)--;
d9187649 760 spin_unlock(ws_lock);
261507a0
LZ
761 return workspace;
762
763 }
6ac10a6a 764 if (atomic_read(total_ws) > cpus) {
261507a0
LZ
765 DEFINE_WAIT(wait);
766
d9187649
BL
767 spin_unlock(ws_lock);
768 prepare_to_wait(ws_wait, &wait, TASK_UNINTERRUPTIBLE);
6ac10a6a 769 if (atomic_read(total_ws) > cpus && !*free_ws)
261507a0 770 schedule();
d9187649 771 finish_wait(ws_wait, &wait);
261507a0
LZ
772 goto again;
773 }
6ac10a6a 774 atomic_inc(total_ws);
d9187649 775 spin_unlock(ws_lock);
261507a0 776
fe308533
DS
777 /*
778 * Allocation helpers call vmalloc that can't use GFP_NOFS, so we have
779 * to turn it off here because we might get called from the restricted
780 * context of btrfs_compress_bio/btrfs_compress_pages
781 */
782 nofs_flag = memalloc_nofs_save();
261507a0 783 workspace = btrfs_compress_op[idx]->alloc_workspace();
fe308533
DS
784 memalloc_nofs_restore(nofs_flag);
785
261507a0 786 if (IS_ERR(workspace)) {
6ac10a6a 787 atomic_dec(total_ws);
d9187649 788 wake_up(ws_wait);
e721e49d
DS
789
790 /*
791 * Do not return the error but go back to waiting. There's a
792 * workspace preallocated for each type and the compression
793 * time is bounded so we get to a workspace eventually. This
794 * makes our caller's life easier.
52356716
DS
795 *
796 * To prevent silent and low-probability deadlocks (when the
797 * initial preallocation fails), check if there are any
798 * workspaces at all.
e721e49d 799 */
52356716
DS
800 if (atomic_read(total_ws) == 0) {
801 static DEFINE_RATELIMIT_STATE(_rs,
802 /* once per minute */ 60 * HZ,
803 /* no burst */ 1);
804
805 if (__ratelimit(&_rs)) {
ab8d0fc4 806 pr_warn("BTRFS: no compression workspaces, low memory, retrying\n");
52356716
DS
807 }
808 }
e721e49d 809 goto again;
261507a0
LZ
810 }
811 return workspace;
812}
813
814/*
815 * put a workspace struct back on the list or free it if we have enough
816 * idle ones sitting around
817 */
818static void free_workspace(int type, struct list_head *workspace)
819{
820 int idx = type - 1;
d9187649
BL
821 struct list_head *idle_ws = &btrfs_comp_ws[idx].idle_ws;
822 spinlock_t *ws_lock = &btrfs_comp_ws[idx].ws_lock;
6ac10a6a 823 atomic_t *total_ws = &btrfs_comp_ws[idx].total_ws;
d9187649 824 wait_queue_head_t *ws_wait = &btrfs_comp_ws[idx].ws_wait;
6ac10a6a 825 int *free_ws = &btrfs_comp_ws[idx].free_ws;
d9187649
BL
826
827 spin_lock(ws_lock);
6ac10a6a 828 if (*free_ws < num_online_cpus()) {
d9187649 829 list_add(workspace, idle_ws);
6ac10a6a 830 (*free_ws)++;
d9187649 831 spin_unlock(ws_lock);
261507a0
LZ
832 goto wake;
833 }
d9187649 834 spin_unlock(ws_lock);
261507a0
LZ
835
836 btrfs_compress_op[idx]->free_workspace(workspace);
6ac10a6a 837 atomic_dec(total_ws);
261507a0 838wake:
a83342aa
DS
839 /*
840 * Make sure counter is updated before we wake up waiters.
841 */
66657b31 842 smp_mb();
d9187649
BL
843 if (waitqueue_active(ws_wait))
844 wake_up(ws_wait);
261507a0
LZ
845}
846
847/*
848 * cleanup function for module exit
849 */
850static void free_workspaces(void)
851{
852 struct list_head *workspace;
853 int i;
854
855 for (i = 0; i < BTRFS_COMPRESS_TYPES; i++) {
d9187649
BL
856 while (!list_empty(&btrfs_comp_ws[i].idle_ws)) {
857 workspace = btrfs_comp_ws[i].idle_ws.next;
261507a0
LZ
858 list_del(workspace);
859 btrfs_compress_op[i]->free_workspace(workspace);
6ac10a6a 860 atomic_dec(&btrfs_comp_ws[i].total_ws);
261507a0
LZ
861 }
862 }
863}
864
865/*
38c31464
DS
866 * Given an address space and start and length, compress the bytes into @pages
867 * that are allocated on demand.
261507a0 868 *
4d3a800e
DS
869 * @out_pages is an in/out parameter, holds maximum number of pages to allocate
870 * and returns number of actually allocated pages
261507a0 871 *
38c31464
DS
872 * @total_in is used to return the number of bytes actually read. It
873 * may be smaller than the input length if we had to exit early because we
261507a0
LZ
874 * ran out of room in the pages array or because we cross the
875 * max_out threshold.
876 *
38c31464
DS
877 * @total_out is an in/out parameter, must be set to the input length and will
878 * be also used to return the total number of compressed bytes
261507a0 879 *
38c31464 880 * @max_out tells us the max number of bytes that we're allowed to
261507a0
LZ
881 * stuff into pages
882 */
883int btrfs_compress_pages(int type, struct address_space *mapping,
38c31464 884 u64 start, struct page **pages,
261507a0
LZ
885 unsigned long *out_pages,
886 unsigned long *total_in,
e5d74902 887 unsigned long *total_out)
261507a0
LZ
888{
889 struct list_head *workspace;
890 int ret;
891
892 workspace = find_workspace(type);
261507a0
LZ
893
894 ret = btrfs_compress_op[type-1]->compress_pages(workspace, mapping,
38c31464 895 start, pages,
4d3a800e 896 out_pages,
e5d74902 897 total_in, total_out);
261507a0
LZ
898 free_workspace(type, workspace);
899 return ret;
900}
901
902/*
903 * pages_in is an array of pages with compressed data.
904 *
905 * disk_start is the starting logical offset of this array in the file
906 *
974b1adc 907 * orig_bio contains the pages from the file that we want to decompress into
261507a0
LZ
908 *
909 * srclen is the number of bytes in pages_in
910 *
911 * The basic idea is that we have a bio that was created by readpages.
912 * The pages in the bio are for the uncompressed data, and they may not
913 * be contiguous. They all correspond to the range of bytes covered by
914 * the compressed extent.
915 */
8140dc30 916static int btrfs_decompress_bio(struct compressed_bio *cb)
261507a0
LZ
917{
918 struct list_head *workspace;
919 int ret;
8140dc30 920 int type = cb->compress_type;
261507a0
LZ
921
922 workspace = find_workspace(type);
e1ddce71 923 ret = btrfs_compress_op[type - 1]->decompress_bio(workspace, cb);
261507a0 924 free_workspace(type, workspace);
e1ddce71 925
261507a0
LZ
926 return ret;
927}
928
929/*
930 * a less complex decompression routine. Our compressed data fits in a
931 * single page, and we want to read a single page out of it.
932 * start_byte tells us the offset into the compressed data we're interested in
933 */
934int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
935 unsigned long start_byte, size_t srclen, size_t destlen)
936{
937 struct list_head *workspace;
938 int ret;
939
940 workspace = find_workspace(type);
261507a0
LZ
941
942 ret = btrfs_compress_op[type-1]->decompress(workspace, data_in,
943 dest_page, start_byte,
944 srclen, destlen);
945
946 free_workspace(type, workspace);
947 return ret;
948}
949
8e4eef7a 950void btrfs_exit_compress(void)
261507a0
LZ
951{
952 free_workspaces();
953}
3a39c18d
LZ
954
955/*
956 * Copy uncompressed data from working buffer to pages.
957 *
958 * buf_start is the byte offset we're of the start of our workspace buffer.
959 *
960 * total_out is the last byte of the buffer
961 */
14a3357b 962int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
3a39c18d 963 unsigned long total_out, u64 disk_start,
974b1adc 964 struct bio *bio)
3a39c18d
LZ
965{
966 unsigned long buf_offset;
967 unsigned long current_buf_start;
968 unsigned long start_byte;
6e78b3f7 969 unsigned long prev_start_byte;
3a39c18d
LZ
970 unsigned long working_bytes = total_out - buf_start;
971 unsigned long bytes;
972 char *kaddr;
974b1adc 973 struct bio_vec bvec = bio_iter_iovec(bio, bio->bi_iter);
3a39c18d
LZ
974
975 /*
976 * start byte is the first byte of the page we're currently
977 * copying into relative to the start of the compressed data.
978 */
974b1adc 979 start_byte = page_offset(bvec.bv_page) - disk_start;
3a39c18d
LZ
980
981 /* we haven't yet hit data corresponding to this page */
982 if (total_out <= start_byte)
983 return 1;
984
985 /*
986 * the start of the data we care about is offset into
987 * the middle of our working buffer
988 */
989 if (total_out > start_byte && buf_start < start_byte) {
990 buf_offset = start_byte - buf_start;
991 working_bytes -= buf_offset;
992 } else {
993 buf_offset = 0;
994 }
995 current_buf_start = buf_start;
996
997 /* copy bytes from the working buffer into the pages */
998 while (working_bytes > 0) {
974b1adc
CH
999 bytes = min_t(unsigned long, bvec.bv_len,
1000 PAGE_SIZE - buf_offset);
3a39c18d 1001 bytes = min(bytes, working_bytes);
974b1adc
CH
1002
1003 kaddr = kmap_atomic(bvec.bv_page);
1004 memcpy(kaddr + bvec.bv_offset, buf + buf_offset, bytes);
7ac687d9 1005 kunmap_atomic(kaddr);
974b1adc 1006 flush_dcache_page(bvec.bv_page);
3a39c18d 1007
3a39c18d
LZ
1008 buf_offset += bytes;
1009 working_bytes -= bytes;
1010 current_buf_start += bytes;
1011
1012 /* check if we need to pick another page */
974b1adc
CH
1013 bio_advance(bio, bytes);
1014 if (!bio->bi_iter.bi_size)
1015 return 0;
1016 bvec = bio_iter_iovec(bio, bio->bi_iter);
6e78b3f7 1017 prev_start_byte = start_byte;
974b1adc 1018 start_byte = page_offset(bvec.bv_page) - disk_start;
3a39c18d 1019
974b1adc 1020 /*
6e78b3f7
OS
1021 * We need to make sure we're only adjusting
1022 * our offset into compression working buffer when
1023 * we're switching pages. Otherwise we can incorrectly
1024 * keep copying when we were actually done.
974b1adc 1025 */
6e78b3f7
OS
1026 if (start_byte != prev_start_byte) {
1027 /*
1028 * make sure our new page is covered by this
1029 * working buffer
1030 */
1031 if (total_out <= start_byte)
1032 return 1;
3a39c18d 1033
6e78b3f7
OS
1034 /*
1035 * the next page in the biovec might not be adjacent
1036 * to the last page, but it might still be found
1037 * inside this working buffer. bump our offset pointer
1038 */
1039 if (total_out > start_byte &&
1040 current_buf_start < start_byte) {
1041 buf_offset = start_byte - buf_start;
1042 working_bytes = total_out - start_byte;
1043 current_buf_start = buf_start + buf_offset;
1044 }
3a39c18d
LZ
1045 }
1046 }
1047
1048 return 1;
1049}