2 * Copyright (C) 2008 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.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>
38 #include "transaction.h"
39 #include "btrfs_inode.h"
41 #include "ordered-data.h"
43 #include "compression.h"
44 #include "extent_io.h"
45 #include "extent_map.h"
47 struct compressed_bio {
48 /* number of bios pending for this compressed extent */
49 atomic_t pending_bios;
51 /* the pages with the compressed data on them */
52 struct page **compressed_pages;
54 /* inode that owns this data */
57 /* starting offset in the inode for our pages */
60 /* number of bytes in the inode we're working on */
63 /* number of bytes on disk */
64 unsigned long compressed_len;
66 /* number of compressed pages in the array */
67 unsigned long nr_pages;
72 /* for reads, this is the bio we are copying the data into */
76 static struct bio *compressed_bio_alloc(struct block_device *bdev,
77 u64 first_byte, gfp_t gfp_flags)
82 nr_vecs = bio_get_nr_vecs(bdev);
83 bio = bio_alloc(gfp_flags, nr_vecs);
85 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
86 while (!bio && (nr_vecs /= 2))
87 bio = bio_alloc(gfp_flags, nr_vecs);
93 bio->bi_sector = first_byte >> 9;
98 /* when we finish reading compressed pages from the disk, we
99 * decompress them and then run the bio end_io routines on the
100 * decompressed pages (in the inode address space).
102 * This allows the checksumming and other IO error handling routines
105 * The compressed pages are freed here, and it must be run
108 static void end_compressed_bio_read(struct bio *bio, int err)
110 struct extent_io_tree *tree;
111 struct compressed_bio *cb = bio->bi_private;
120 /* if there are more bios still pending for this compressed
123 if (!atomic_dec_and_test(&cb->pending_bios))
126 /* ok, we're the last bio for this extent, lets start
130 tree = &BTRFS_I(inode)->io_tree;
131 ret = btrfs_zlib_decompress_biovec(cb->compressed_pages,
133 cb->orig_bio->bi_io_vec,
134 cb->orig_bio->bi_vcnt,
139 /* release the compressed pages */
141 for (index = 0; index < cb->nr_pages; index++) {
142 page = cb->compressed_pages[index];
143 page->mapping = NULL;
144 page_cache_release(page);
147 /* do io completion on the original bio */
149 bio_io_error(cb->orig_bio);
151 bio_endio(cb->orig_bio, 0);
153 /* finally free the cb struct */
154 kfree(cb->compressed_pages);
161 * Clear the writeback bits on all of the file
162 * pages for a compressed write
164 static noinline int end_compressed_writeback(struct inode *inode, u64 start,
165 unsigned long ram_size)
167 unsigned long index = start >> PAGE_CACHE_SHIFT;
168 unsigned long end_index = (start + ram_size - 1) >> PAGE_CACHE_SHIFT;
169 struct page *pages[16];
170 unsigned long nr_pages = end_index - index + 1;
174 while(nr_pages > 0) {
175 ret = find_get_pages_contig(inode->i_mapping, index,
176 min(nr_pages, ARRAY_SIZE(pages)), pages);
182 for (i = 0; i < ret; i++) {
183 end_page_writeback(pages[i]);
184 page_cache_release(pages[i]);
189 /* the inode may be gone now */
194 * do the cleanup once all the compressed pages hit the disk.
195 * This will clear writeback on the file pages and free the compressed
198 * This also calls the writeback end hooks for the file pages so that
199 * metadata and checksums can be updated in the file.
201 static void end_compressed_bio_write(struct bio *bio, int err)
203 struct extent_io_tree *tree;
204 struct compressed_bio *cb = bio->bi_private;
212 /* if there are more bios still pending for this compressed
215 if (!atomic_dec_and_test(&cb->pending_bios))
218 /* ok, we're the last bio for this extent, step one is to
219 * call back into the FS and do all the end_io operations
222 tree = &BTRFS_I(inode)->io_tree;
223 tree->ops->writepage_end_io_hook(cb->compressed_pages[0],
225 cb->start + cb->len - 1,
228 end_compressed_writeback(inode, cb->start, cb->len);
229 /* note, our inode could be gone now */
232 * release the compressed pages, these came from alloc_page and
233 * are not attached to the inode at all
236 for (index = 0; index < cb->nr_pages; index++) {
237 page = cb->compressed_pages[index];
238 page->mapping = NULL;
239 page_cache_release(page);
242 /* finally free the cb struct */
243 kfree(cb->compressed_pages);
250 * worker function to build and submit bios for previously compressed pages.
251 * The corresponding pages in the inode should be marked for writeback
252 * and the compressed pages should have a reference on them for dropping
253 * when the IO is complete.
255 * This also checksums the file bytes and gets things ready for
258 int btrfs_submit_compressed_write(struct inode *inode, u64 start,
259 unsigned long len, u64 disk_start,
260 unsigned long compressed_len,
261 struct page **compressed_pages,
262 unsigned long nr_pages)
264 struct bio *bio = NULL;
265 struct btrfs_root *root = BTRFS_I(inode)->root;
266 struct compressed_bio *cb;
267 unsigned long bytes_left;
268 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
271 u64 first_byte = disk_start;
272 struct block_device *bdev;
275 WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1));
276 cb = kmalloc(sizeof(*cb), GFP_NOFS);
277 atomic_set(&cb->pending_bios, 0);
282 cb->compressed_pages = compressed_pages;
283 cb->compressed_len = compressed_len;
285 cb->nr_pages = nr_pages;
287 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
289 ret = btrfs_csum_file_bytes(root, inode, start, len);
292 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
293 bio->bi_private = cb;
294 bio->bi_end_io = end_compressed_bio_write;
295 atomic_inc(&cb->pending_bios);
297 /* create and submit bios for the compressed pages */
298 bytes_left = compressed_len;
299 while(bytes_left > 0) {
300 page = compressed_pages[page_index];
301 page->mapping = inode->i_mapping;
303 ret = io_tree->ops->merge_bio_hook(page, 0,
309 if (ret || bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) <
313 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
316 ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
321 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
322 atomic_inc(&cb->pending_bios);
323 bio->bi_private = cb;
324 bio->bi_end_io = end_compressed_bio_write;
325 bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
328 bytes_left -= PAGE_CACHE_SIZE;
329 first_byte += PAGE_CACHE_SIZE;
333 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
336 ret = btrfs_map_bio(root, WRITE, bio, 0, 1);
344 * for a compressed read, the bio we get passed has all the inode pages
345 * in it. We don't actually do IO on those pages but allocate new ones
346 * to hold the compressed pages on disk.
348 * bio->bi_sector points to the compressed extent on disk
349 * bio->bi_io_vec points to all of the inode pages
350 * bio->bi_vcnt is a count of pages
352 * After the compressed pages are read, we copy the bytes into the
353 * bio we were passed and then call the bio end_io calls
355 int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
356 int mirror_num, unsigned long bio_flags)
358 struct extent_io_tree *tree;
359 struct extent_map_tree *em_tree;
360 struct compressed_bio *cb;
361 struct btrfs_root *root = BTRFS_I(inode)->root;
362 unsigned long uncompressed_len = bio->bi_vcnt * PAGE_CACHE_SIZE;
363 unsigned long compressed_len;
364 unsigned long nr_pages;
365 unsigned long page_index;
367 struct block_device *bdev;
368 struct bio *comp_bio;
369 u64 cur_disk_byte = (u64)bio->bi_sector << 9;
370 struct extent_map *em;
373 tree = &BTRFS_I(inode)->io_tree;
374 em_tree = &BTRFS_I(inode)->extent_tree;
376 /* we need the actual starting offset of this extent in the file */
377 spin_lock(&em_tree->lock);
378 em = lookup_extent_mapping(em_tree,
379 page_offset(bio->bi_io_vec->bv_page),
381 spin_unlock(&em_tree->lock);
383 cb = kmalloc(sizeof(*cb), GFP_NOFS);
384 atomic_set(&cb->pending_bios, 0);
388 cb->start = em->start;
389 compressed_len = em->block_len;
392 cb->len = uncompressed_len;
393 cb->compressed_len = compressed_len;
396 nr_pages = (compressed_len + PAGE_CACHE_SIZE - 1) /
398 cb->compressed_pages = kmalloc(sizeof(struct page *) * nr_pages,
400 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
402 for (page_index = 0; page_index < nr_pages; page_index++) {
403 cb->compressed_pages[page_index] = alloc_page(GFP_NOFS |
406 cb->nr_pages = nr_pages;
408 comp_bio = compressed_bio_alloc(bdev, cur_disk_byte, GFP_NOFS);
409 comp_bio->bi_private = cb;
410 comp_bio->bi_end_io = end_compressed_bio_read;
411 atomic_inc(&cb->pending_bios);
413 for (page_index = 0; page_index < nr_pages; page_index++) {
414 page = cb->compressed_pages[page_index];
415 page->mapping = inode->i_mapping;
416 if (comp_bio->bi_size)
417 ret = tree->ops->merge_bio_hook(page, 0,
423 if (ret || bio_add_page(comp_bio, page, PAGE_CACHE_SIZE, 0) <
427 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
430 ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);
435 comp_bio = compressed_bio_alloc(bdev, cur_disk_byte,
437 atomic_inc(&cb->pending_bios);
438 bio->bi_private = cb;
439 bio->bi_end_io = end_compressed_bio_write;
440 bio_add_page(bio, page, PAGE_CACHE_SIZE, 0);
442 cur_disk_byte += PAGE_CACHE_SIZE;
446 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0);
449 ret = btrfs_map_bio(root, READ, comp_bio, 0, 0);