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