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