]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/btrfs/compression.c
Merge branch 'omap-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-bionic-kernel.git] / fs / btrfs / compression.c
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>
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>
34 #include <linux/slab.h>
35 #include "compat.h"
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"
42 #include "compression.h"
43 #include "extent_io.h"
44 #include "extent_map.h"
45
46 struct compressed_bio {
47 /* number of bios pending for this compressed extent */
48 atomic_t pending_bios;
49
50 /* the pages with the compressed data on them */
51 struct page **compressed_pages;
52
53 /* inode that owns this data */
54 struct inode *inode;
55
56 /* starting offset in the inode for our pages */
57 u64 start;
58
59 /* number of bytes in the inode we're working on */
60 unsigned long len;
61
62 /* number of bytes on disk */
63 unsigned long compressed_len;
64
65 /* number of compressed pages in the array */
66 unsigned long nr_pages;
67
68 /* IO errors */
69 int errors;
70 int mirror_num;
71
72 /* for reads, this is the bio we are copying the data into */
73 struct bio *orig_bio;
74
75 /*
76 * the start of a variable length array of checksums only
77 * used by reads
78 */
79 u32 sums;
80 };
81
82 static inline int compressed_bio_size(struct btrfs_root *root,
83 unsigned long disk_size)
84 {
85 u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
86 return sizeof(struct compressed_bio) +
87 ((disk_size + root->sectorsize - 1) / root->sectorsize) *
88 csum_size;
89 }
90
91 static struct bio *compressed_bio_alloc(struct block_device *bdev,
92 u64 first_byte, gfp_t gfp_flags)
93 {
94 int nr_vecs;
95
96 nr_vecs = bio_get_nr_vecs(bdev);
97 return btrfs_bio_alloc(bdev, first_byte >> 9, nr_vecs, gfp_flags);
98 }
99
100 static int check_compressed_csum(struct inode *inode,
101 struct compressed_bio *cb,
102 u64 disk_start)
103 {
104 int ret;
105 struct btrfs_root *root = BTRFS_I(inode)->root;
106 struct page *page;
107 unsigned long i;
108 char *kaddr;
109 u32 csum;
110 u32 *cb_sum = &cb->sums;
111
112 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
113 return 0;
114
115 for (i = 0; i < cb->nr_pages; i++) {
116 page = cb->compressed_pages[i];
117 csum = ~(u32)0;
118
119 kaddr = kmap_atomic(page, KM_USER0);
120 csum = btrfs_csum_data(root, kaddr, csum, PAGE_CACHE_SIZE);
121 btrfs_csum_final(csum, (char *)&csum);
122 kunmap_atomic(kaddr, KM_USER0);
123
124 if (csum != *cb_sum) {
125 printk(KERN_INFO "btrfs csum failed ino %lu "
126 "extent %llu csum %u "
127 "wanted %u mirror %d\n", inode->i_ino,
128 (unsigned long long)disk_start,
129 csum, *cb_sum, cb->mirror_num);
130 ret = -EIO;
131 goto fail;
132 }
133 cb_sum++;
134
135 }
136 ret = 0;
137 fail:
138 return ret;
139 }
140
141 /* when we finish reading compressed pages from the disk, we
142 * decompress them and then run the bio end_io routines on the
143 * decompressed pages (in the inode address space).
144 *
145 * This allows the checksumming and other IO error handling routines
146 * to work normally
147 *
148 * The compressed pages are freed here, and it must be run
149 * in process context
150 */
151 static void end_compressed_bio_read(struct bio *bio, int err)
152 {
153 struct compressed_bio *cb = bio->bi_private;
154 struct inode *inode;
155 struct page *page;
156 unsigned long index;
157 int ret;
158
159 if (err)
160 cb->errors = 1;
161
162 /* if there are more bios still pending for this compressed
163 * extent, just exit
164 */
165 if (!atomic_dec_and_test(&cb->pending_bios))
166 goto out;
167
168 inode = cb->inode;
169 ret = check_compressed_csum(inode, cb, (u64)bio->bi_sector << 9);
170 if (ret)
171 goto csum_failed;
172
173 /* ok, we're the last bio for this extent, lets start
174 * the decompression.
175 */
176 ret = btrfs_zlib_decompress_biovec(cb->compressed_pages,
177 cb->start,
178 cb->orig_bio->bi_io_vec,
179 cb->orig_bio->bi_vcnt,
180 cb->compressed_len);
181 csum_failed:
182 if (ret)
183 cb->errors = 1;
184
185 /* release the compressed pages */
186 index = 0;
187 for (index = 0; index < cb->nr_pages; index++) {
188 page = cb->compressed_pages[index];
189 page->mapping = NULL;
190 page_cache_release(page);
191 }
192
193 /* do io completion on the original bio */
194 if (cb->errors) {
195 bio_io_error(cb->orig_bio);
196 } else {
197 int bio_index = 0;
198 struct bio_vec *bvec = cb->orig_bio->bi_io_vec;
199
200 /*
201 * we have verified the checksum already, set page
202 * checked so the end_io handlers know about it
203 */
204 while (bio_index < cb->orig_bio->bi_vcnt) {
205 SetPageChecked(bvec->bv_page);
206 bvec++;
207 bio_index++;
208 }
209 bio_endio(cb->orig_bio, 0);
210 }
211
212 /* finally free the cb struct */
213 kfree(cb->compressed_pages);
214 kfree(cb);
215 out:
216 bio_put(bio);
217 }
218
219 /*
220 * Clear the writeback bits on all of the file
221 * pages for a compressed write
222 */
223 static noinline int end_compressed_writeback(struct inode *inode, u64 start,
224 unsigned long ram_size)
225 {
226 unsigned long index = start >> PAGE_CACHE_SHIFT;
227 unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
228 struct page *pages[16];
229 unsigned long nr_pages = end_index - index + 1;
230 int i;
231 int ret;
232
233 while (nr_pages > 0) {
234 ret = find_get_pages_contig(inode->i_mapping, index,
235 min_t(unsigned long,
236 nr_pages, ARRAY_SIZE(pages)), pages);
237 if (ret == 0) {
238 nr_pages -= 1;
239 index += 1;
240 continue;
241 }
242 for (i = 0; i < ret; i++) {
243 end_page_writeback(pages[i]);
244 page_cache_release(pages[i]);
245 }
246 nr_pages -= ret;
247 index += ret;
248 }
249 /* the inode may be gone now */
250 return 0;
251 }
252
253 /*
254 * do the cleanup once all the compressed pages hit the disk.
255 * This will clear writeback on the file pages and free the compressed
256 * pages.
257 *
258 * This also calls the writeback end hooks for the file pages so that
259 * metadata and checksums can be updated in the file.
260 */
261 static void end_compressed_bio_write(struct bio *bio, int err)
262 {
263 struct extent_io_tree *tree;
264 struct compressed_bio *cb = bio->bi_private;
265 struct inode *inode;
266 struct page *page;
267 unsigned long index;
268
269 if (err)
270 cb->errors = 1;
271
272 /* if there are more bios still pending for this compressed
273 * extent, just exit
274 */
275 if (!atomic_dec_and_test(&cb->pending_bios))
276 goto out;
277
278 /* ok, we're the last bio for this extent, step one is to
279 * call back into the FS and do all the end_io operations
280 */
281 inode = cb->inode;
282 tree = &BTRFS_I(inode)->io_tree;
283 cb->compressed_pages[0]->mapping = cb->inode->i_mapping;
284 tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
285 cb->start,
286 cb->start + cb->len - 1,
287 NULL, 1);
288 cb->compressed_pages[0]->mapping = NULL;
289
290 end_compressed_writeback(inode, cb->start, cb->len);
291 /* note, our inode could be gone now */
292
293 /*
294 * release the compressed pages, these came from alloc_page and
295 * are not attached to the inode at all
296 */
297 index = 0;
298 for (index = 0; index < cb->nr_pages; index++) {
299 page = cb->compressed_pages[index];
300 page->mapping = NULL;
301 page_cache_release(page);
302 }
303
304 /* finally free the cb struct */
305 kfree(cb->compressed_pages);
306 kfree(cb);
307 out:
308 bio_put(bio);
309 }
310
311 /*
312 * worker function to build and submit bios for previously compressed pages.
313 * The corresponding pages in the inode should be marked for writeback
314 * and the compressed pages should have a reference on them for dropping
315 * when the IO is complete.
316 *
317 * This also checksums the file bytes and gets things ready for
318 * the end io hooks.
319 */
320 int btrfs_submit_compressed_write(struct inode *inode, u64 start,
321 unsigned long len, u64 disk_start,
322 unsigned long compressed_len,
323 struct page **compressed_pages,
324 unsigned long nr_pages)
325 {
326 struct bio *bio = NULL;
327 struct btrfs_root *root = BTRFS_I(inode)->root;
328 struct compressed_bio *cb;
329 unsigned long bytes_left;
330 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
331 int page_index = 0;
332 struct page *page;
333 u64 first_byte = disk_start;
334 struct block_device *bdev;
335 int ret;
336
337 WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
338 cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
339 atomic_set(&cb->pending_bios, 0);
340 cb->errors = 0;
341 cb->inode = inode;
342 cb->start = start;
343 cb->len = len;
344 cb->mirror_num = 0;
345 cb->compressed_pages = compressed_pages;
346 cb->compressed_len = compressed_len;
347 cb->orig_bio = NULL;
348 cb->nr_pages = nr_pages;
349
350 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
351
352 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
353 bio->bi_private = cb;
354 bio->bi_end_io = end_compressed_bio_write;
355 atomic_inc(&cb->pending_bios);
356
357 /* create and submit bios for the compressed pages */
358 bytes_left = compressed_len;
359 for (page_index = 0; page_index < cb->nr_pages; page_index++) {
360 page = compressed_pages[page_index];
361 page->mapping = inode->i_mapping;
362 if (bio->bi_size)
363 ret = io_tree->ops->merge_bio_hook(page, 0,
364 PAGE_CACHE_SIZE,
365 bio, 0);
366 else
367 ret = 0;
368
369 page->mapping = NULL;
370 if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
371 PAGE_CACHE_SIZE) {
372 bio_get(bio);
373
374 /*
375 * inc the count before we submit the bio so
376 * we know the end IO handler won't happen before
377 * we inc the count. Otherwise, the cb might get
378 * freed before we're done setting it up
379 */
380 atomic_inc(&cb->pending_bios);
381 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
382 BUG_ON(ret);
383
384 ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
385 BUG_ON(ret);
386
387 ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
388 BUG_ON(ret);
389
390 bio_put(bio);
391
392 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
393 bio->bi_private = cb;
394 bio->bi_end_io = end_compressed_bio_write;
395 bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
396 }
397 if (bytes_left < PAGE_CACHE_SIZE) {
398 printk("bytes left %lu compress len %lu nr %lu\n",
399 bytes_left, cb->compressed_len, cb->nr_pages);
400 }
401 bytes_left -= PAGE_CACHE_SIZE;
402 first_byte += PAGE_CACHE_SIZE;
403 cond_resched();
404 }
405 bio_get(bio);
406
407 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
408 BUG_ON(ret);
409
410 ret = btrfs_csum_one_bio(root, inode, bio, start, 1);
411 BUG_ON(ret);
412
413 ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
414 BUG_ON(ret);
415
416 bio_put(bio);
417 return 0;
418 }
419
420 static noinline int add_ra_bio_pages(struct inode *inode,
421 u64 compressed_end,
422 struct compressed_bio *cb)
423 {
424 unsigned long end_index;
425 unsigned long page_index;
426 u64 last_offset;
427 u64 isize = i_size_read(inode);
428 int ret;
429 struct page *page;
430 unsigned long nr_pages = 0;
431 struct extent_map *em;
432 struct address_space *mapping = inode->i_mapping;
433 struct extent_map_tree *em_tree;
434 struct extent_io_tree *tree;
435 u64 end;
436 int misses = 0;
437
438 page = cb->orig_bio->bi_io_vec[cb->orig_bio->bi_vcnt - 1].bv_page;
439 last_offset = (page_offset(page) + PAGE_CACHE_SIZE);
440 em_tree = &BTRFS_I(inode)->extent_tree;
441 tree = &BTRFS_I(inode)->io_tree;
442
443 if (isize == 0)
444 return 0;
445
446 end_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
447
448 while (last_offset < compressed_end) {
449 page_index = last_offset >> PAGE_CACHE_SHIFT;
450
451 if (page_index > end_index)
452 break;
453
454 rcu_read_lock();
455 page = radix_tree_lookup(&mapping->page_tree, page_index);
456 rcu_read_unlock();
457 if (page) {
458 misses++;
459 if (misses > 4)
460 break;
461 goto next;
462 }
463
464 page = __page_cache_alloc(mapping_gfp_mask(mapping) &
465 ~__GFP_FS);
466 if (!page)
467 break;
468
469 if (add_to_page_cache_lru(page, mapping, page_index,
470 GFP_NOFS)) {
471 page_cache_release(page);
472 goto next;
473 }
474
475 end = last_offset + PAGE_CACHE_SIZE - 1;
476 /*
477 * at this point, we have a locked page in the page cache
478 * for these bytes in the file. But, we have to make
479 * sure they map to this compressed extent on disk.
480 */
481 set_page_extent_mapped(page);
482 lock_extent(tree, last_offset, end, GFP_NOFS);
483 read_lock(&em_tree->lock);
484 em = lookup_extent_mapping(em_tree, last_offset,
485 PAGE_CACHE_SIZE);
486 read_unlock(&em_tree->lock);
487
488 if (!em || last_offset < em->start ||
489 (last_offset + PAGE_CACHE_SIZE > extent_map_end(em)) ||
490 (em->block_start >> 9) != cb->orig_bio->bi_sector) {
491 free_extent_map(em);
492 unlock_extent(tree, last_offset, end, GFP_NOFS);
493 unlock_page(page);
494 page_cache_release(page);
495 break;
496 }
497 free_extent_map(em);
498
499 if (page->index == end_index) {
500 char *userpage;
501 size_t zero_offset = isize & (PAGE_CACHE_SIZE - 1);
502
503 if (zero_offset) {
504 int zeros;
505 zeros = PAGE_CACHE_SIZE - zero_offset;
506 userpage = kmap_atomic(page, KM_USER0);
507 memset(userpage + zero_offset, 0, zeros);
508 flush_dcache_page(page);
509 kunmap_atomic(userpage, KM_USER0);
510 }
511 }
512
513 ret = bio_add_page(cb->orig_bio, page,
514 PAGE_CACHE_SIZE, 0);
515
516 if (ret == PAGE_CACHE_SIZE) {
517 nr_pages++;
518 page_cache_release(page);
519 } else {
520 unlock_extent(tree, last_offset, end, GFP_NOFS);
521 unlock_page(page);
522 page_cache_release(page);
523 break;
524 }
525 next:
526 last_offset += PAGE_CACHE_SIZE;
527 }
528 return 0;
529 }
530
531 /*
532 * for a compressed read, the bio we get passed has all the inode pages
533 * in it. We don't actually do IO on those pages but allocate new ones
534 * to hold the compressed pages on disk.
535 *
536 * bio->bi_sector points to the compressed extent on disk
537 * bio->bi_io_vec points to all of the inode pages
538 * bio->bi_vcnt is a count of pages
539 *
540 * After the compressed pages are read, we copy the bytes into the
541 * bio we were passed and then call the bio end_io calls
542 */
543 int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
544 int mirror_num, unsigned long bio_flags)
545 {
546 struct extent_io_tree *tree;
547 struct extent_map_tree *em_tree;
548 struct compressed_bio *cb;
549 struct btrfs_root *root = BTRFS_I(inode)->root;
550 unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
551 unsigned long compressed_len;
552 unsigned long nr_pages;
553 unsigned long page_index;
554 struct page *page;
555 struct block_device *bdev;
556 struct bio *comp_bio;
557 u64 cur_disk_byte = (u64)bio->bi_sector << 9;
558 u64 em_len;
559 u64 em_start;
560 struct extent_map *em;
561 int ret;
562 u32 *sums;
563
564 tree = &BTRFS_I(inode)->io_tree;
565 em_tree = &BTRFS_I(inode)->extent_tree;
566
567 /* we need the actual starting offset of this extent in the file */
568 read_lock(&em_tree->lock);
569 em = lookup_extent_mapping(em_tree,
570 page_offset(bio->bi_io_vec->bv_page),
571 PAGE_CACHE_SIZE);
572 read_unlock(&em_tree->lock);
573
574 compressed_len = em->block_len;
575 cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS);
576 atomic_set(&cb->pending_bios, 0);
577 cb->errors = 0;
578 cb->inode = inode;
579 cb->mirror_num = mirror_num;
580 sums = &cb->sums;
581
582 cb->start = em->orig_start;
583 em_len = em->len;
584 em_start = em->start;
585
586 free_extent_map(em);
587 em = NULL;
588
589 cb->len = uncompressed_len;
590 cb->compressed_len = compressed_len;
591 cb->orig_bio = bio;
592
593 nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
594 PAGE_CACHE_SIZE;
595 cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
596 GFP_NOFS);
597 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
598
599 for (page_index = 0; page_index < nr_pages; page_index++) {
600 cb->compressed_pages[page_index] = alloc_page(GFP_NOFS |
601 __GFP_HIGHMEM);
602 }
603 cb->nr_pages = nr_pages;
604
605 add_ra_bio_pages(inode, em_start + em_len, cb);
606
607 /* include any pages we added in add_ra-bio_pages */
608 uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
609 cb->len = uncompressed_len;
610
611 comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
612 comp_bio->bi_private = cb;
613 comp_bio->bi_end_io = end_compressed_bio_read;
614 atomic_inc(&cb->pending_bios);
615
616 for (page_index = 0; page_index < nr_pages; page_index++) {
617 page = cb->compressed_pages[page_index];
618 page->mapping = inode->i_mapping;
619 page->index = em_start >> PAGE_CACHE_SHIFT;
620
621 if (comp_bio->bi_size)
622 ret = tree->ops->merge_bio_hook(page, 0,
623 PAGE_CACHE_SIZE,
624 comp_bio, 0);
625 else
626 ret = 0;
627
628 page->mapping = NULL;
629 if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
630 PAGE_CACHE_SIZE) {
631 bio_get(comp_bio);
632
633 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
634 BUG_ON(ret);
635
636 /*
637 * inc the count before we submit the bio so
638 * we know the end IO handler won't happen before
639 * we inc the count. Otherwise, the cb might get
640 * freed before we're done setting it up
641 */
642 atomic_inc(&cb->pending_bios);
643
644 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
645 btrfs_lookup_bio_sums(root, inode, comp_bio,
646 sums);
647 }
648 sums += (comp_bio->bi_size + root->sectorsize - 1) /
649 root->sectorsize;
650
651 ret = btrfs_map_bio(root, READ, comp_bio,
652 mirror_num, 0);
653 BUG_ON(ret);
654
655 bio_put(comp_bio);
656
657 comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
658 GFP_NOFS);
659 comp_bio->bi_private = cb;
660 comp_bio->bi_end_io = end_compressed_bio_read;
661
662 bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0);
663 }
664 cur_disk_byte += PAGE_CACHE_SIZE;
665 }
666 bio_get(comp_bio);
667
668 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
669 BUG_ON(ret);
670
671 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
672 btrfs_lookup_bio_sums(root, inode, comp_bio, sums);
673
674 ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0);
675 BUG_ON(ret);
676
677 bio_put(comp_bio);
678 return 0;
679 }