Commit | Line | Data |
---|---|---|
d1310b2e CM |
1 | #include <linux/bitops.h> |
2 | #include <linux/slab.h> | |
3 | #include <linux/bio.h> | |
4 | #include <linux/mm.h> | |
d1310b2e CM |
5 | #include <linux/pagemap.h> |
6 | #include <linux/page-flags.h> | |
d1310b2e CM |
7 | #include <linux/spinlock.h> |
8 | #include <linux/blkdev.h> | |
9 | #include <linux/swap.h> | |
d1310b2e CM |
10 | #include <linux/writeback.h> |
11 | #include <linux/pagevec.h> | |
268bb0ce | 12 | #include <linux/prefetch.h> |
90a887c9 | 13 | #include <linux/cleancache.h> |
d1310b2e CM |
14 | #include "extent_io.h" |
15 | #include "extent_map.h" | |
2db04966 | 16 | #include "compat.h" |
902b22f3 DW |
17 | #include "ctree.h" |
18 | #include "btrfs_inode.h" | |
4a54c8c1 | 19 | #include "volumes.h" |
21adbd5c | 20 | #include "check-integrity.h" |
0b32f4bb | 21 | #include "locking.h" |
606686ee | 22 | #include "rcu-string.h" |
d1310b2e | 23 | |
d1310b2e CM |
24 | static struct kmem_cache *extent_state_cache; |
25 | static struct kmem_cache *extent_buffer_cache; | |
9be3395b | 26 | static struct bio_set *btrfs_bioset; |
d1310b2e | 27 | |
6d49ba1b | 28 | #ifdef CONFIG_BTRFS_DEBUG |
d1310b2e CM |
29 | static LIST_HEAD(buffers); |
30 | static LIST_HEAD(states); | |
4bef0848 | 31 | |
d397712b | 32 | static DEFINE_SPINLOCK(leak_lock); |
6d49ba1b ES |
33 | |
34 | static inline | |
35 | void btrfs_leak_debug_add(struct list_head *new, struct list_head *head) | |
36 | { | |
37 | unsigned long flags; | |
38 | ||
39 | spin_lock_irqsave(&leak_lock, flags); | |
40 | list_add(new, head); | |
41 | spin_unlock_irqrestore(&leak_lock, flags); | |
42 | } | |
43 | ||
44 | static inline | |
45 | void btrfs_leak_debug_del(struct list_head *entry) | |
46 | { | |
47 | unsigned long flags; | |
48 | ||
49 | spin_lock_irqsave(&leak_lock, flags); | |
50 | list_del(entry); | |
51 | spin_unlock_irqrestore(&leak_lock, flags); | |
52 | } | |
53 | ||
54 | static inline | |
55 | void btrfs_leak_debug_check(void) | |
56 | { | |
57 | struct extent_state *state; | |
58 | struct extent_buffer *eb; | |
59 | ||
60 | while (!list_empty(&states)) { | |
61 | state = list_entry(states.next, struct extent_state, leak_list); | |
62 | printk(KERN_ERR "btrfs state leak: start %llu end %llu " | |
63 | "state %lu in tree %p refs %d\n", | |
64 | (unsigned long long)state->start, | |
65 | (unsigned long long)state->end, | |
66 | state->state, state->tree, atomic_read(&state->refs)); | |
67 | list_del(&state->leak_list); | |
68 | kmem_cache_free(extent_state_cache, state); | |
69 | } | |
70 | ||
71 | while (!list_empty(&buffers)) { | |
72 | eb = list_entry(buffers.next, struct extent_buffer, leak_list); | |
73 | printk(KERN_ERR "btrfs buffer leak start %llu len %lu " | |
74 | "refs %d\n", (unsigned long long)eb->start, | |
75 | eb->len, atomic_read(&eb->refs)); | |
76 | list_del(&eb->leak_list); | |
77 | kmem_cache_free(extent_buffer_cache, eb); | |
78 | } | |
79 | } | |
8d599ae1 DS |
80 | |
81 | #define btrfs_debug_check_extent_io_range(inode, start, end) \ | |
82 | __btrfs_debug_check_extent_io_range(__func__, (inode), (start), (end)) | |
83 | static inline void __btrfs_debug_check_extent_io_range(const char *caller, | |
84 | struct inode *inode, u64 start, u64 end) | |
85 | { | |
86 | u64 isize = i_size_read(inode); | |
87 | ||
88 | if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) { | |
89 | printk_ratelimited(KERN_DEBUG | |
90 | "btrfs: %s: ino %llu isize %llu odd range [%llu,%llu]\n", | |
91 | caller, | |
92 | (unsigned long long)btrfs_ino(inode), | |
93 | (unsigned long long)isize, | |
94 | (unsigned long long)start, | |
95 | (unsigned long long)end); | |
96 | } | |
97 | } | |
6d49ba1b ES |
98 | #else |
99 | #define btrfs_leak_debug_add(new, head) do {} while (0) | |
100 | #define btrfs_leak_debug_del(entry) do {} while (0) | |
101 | #define btrfs_leak_debug_check() do {} while (0) | |
8d599ae1 | 102 | #define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0) |
4bef0848 | 103 | #endif |
d1310b2e | 104 | |
d1310b2e CM |
105 | #define BUFFER_LRU_MAX 64 |
106 | ||
107 | struct tree_entry { | |
108 | u64 start; | |
109 | u64 end; | |
d1310b2e CM |
110 | struct rb_node rb_node; |
111 | }; | |
112 | ||
113 | struct extent_page_data { | |
114 | struct bio *bio; | |
115 | struct extent_io_tree *tree; | |
116 | get_extent_t *get_extent; | |
de0022b9 | 117 | unsigned long bio_flags; |
771ed689 CM |
118 | |
119 | /* tells writepage not to lock the state bits for this range | |
120 | * it still does the unlocking | |
121 | */ | |
ffbd517d CM |
122 | unsigned int extent_locked:1; |
123 | ||
124 | /* tells the submit_bio code to use a WRITE_SYNC */ | |
125 | unsigned int sync_io:1; | |
d1310b2e CM |
126 | }; |
127 | ||
0b32f4bb | 128 | static noinline void flush_write_bio(void *data); |
c2d904e0 JM |
129 | static inline struct btrfs_fs_info * |
130 | tree_fs_info(struct extent_io_tree *tree) | |
131 | { | |
132 | return btrfs_sb(tree->mapping->host->i_sb); | |
133 | } | |
0b32f4bb | 134 | |
d1310b2e CM |
135 | int __init extent_io_init(void) |
136 | { | |
837e1972 | 137 | extent_state_cache = kmem_cache_create("btrfs_extent_state", |
9601e3f6 CH |
138 | sizeof(struct extent_state), 0, |
139 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
d1310b2e CM |
140 | if (!extent_state_cache) |
141 | return -ENOMEM; | |
142 | ||
837e1972 | 143 | extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer", |
9601e3f6 CH |
144 | sizeof(struct extent_buffer), 0, |
145 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); | |
d1310b2e CM |
146 | if (!extent_buffer_cache) |
147 | goto free_state_cache; | |
9be3395b CM |
148 | |
149 | btrfs_bioset = bioset_create(BIO_POOL_SIZE, | |
150 | offsetof(struct btrfs_io_bio, bio)); | |
151 | if (!btrfs_bioset) | |
152 | goto free_buffer_cache; | |
d1310b2e CM |
153 | return 0; |
154 | ||
9be3395b CM |
155 | free_buffer_cache: |
156 | kmem_cache_destroy(extent_buffer_cache); | |
157 | extent_buffer_cache = NULL; | |
158 | ||
d1310b2e CM |
159 | free_state_cache: |
160 | kmem_cache_destroy(extent_state_cache); | |
9be3395b | 161 | extent_state_cache = NULL; |
d1310b2e CM |
162 | return -ENOMEM; |
163 | } | |
164 | ||
165 | void extent_io_exit(void) | |
166 | { | |
6d49ba1b | 167 | btrfs_leak_debug_check(); |
8c0a8537 KS |
168 | |
169 | /* | |
170 | * Make sure all delayed rcu free are flushed before we | |
171 | * destroy caches. | |
172 | */ | |
173 | rcu_barrier(); | |
d1310b2e CM |
174 | if (extent_state_cache) |
175 | kmem_cache_destroy(extent_state_cache); | |
176 | if (extent_buffer_cache) | |
177 | kmem_cache_destroy(extent_buffer_cache); | |
9be3395b CM |
178 | if (btrfs_bioset) |
179 | bioset_free(btrfs_bioset); | |
d1310b2e CM |
180 | } |
181 | ||
182 | void extent_io_tree_init(struct extent_io_tree *tree, | |
f993c883 | 183 | struct address_space *mapping) |
d1310b2e | 184 | { |
6bef4d31 | 185 | tree->state = RB_ROOT; |
19fe0a8b | 186 | INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC); |
d1310b2e CM |
187 | tree->ops = NULL; |
188 | tree->dirty_bytes = 0; | |
70dec807 | 189 | spin_lock_init(&tree->lock); |
6af118ce | 190 | spin_lock_init(&tree->buffer_lock); |
d1310b2e | 191 | tree->mapping = mapping; |
d1310b2e | 192 | } |
d1310b2e | 193 | |
b2950863 | 194 | static struct extent_state *alloc_extent_state(gfp_t mask) |
d1310b2e CM |
195 | { |
196 | struct extent_state *state; | |
d1310b2e CM |
197 | |
198 | state = kmem_cache_alloc(extent_state_cache, mask); | |
2b114d1d | 199 | if (!state) |
d1310b2e CM |
200 | return state; |
201 | state->state = 0; | |
d1310b2e | 202 | state->private = 0; |
70dec807 | 203 | state->tree = NULL; |
6d49ba1b | 204 | btrfs_leak_debug_add(&state->leak_list, &states); |
d1310b2e CM |
205 | atomic_set(&state->refs, 1); |
206 | init_waitqueue_head(&state->wq); | |
143bede5 | 207 | trace_alloc_extent_state(state, mask, _RET_IP_); |
d1310b2e CM |
208 | return state; |
209 | } | |
d1310b2e | 210 | |
4845e44f | 211 | void free_extent_state(struct extent_state *state) |
d1310b2e | 212 | { |
d1310b2e CM |
213 | if (!state) |
214 | return; | |
215 | if (atomic_dec_and_test(&state->refs)) { | |
70dec807 | 216 | WARN_ON(state->tree); |
6d49ba1b | 217 | btrfs_leak_debug_del(&state->leak_list); |
143bede5 | 218 | trace_free_extent_state(state, _RET_IP_); |
d1310b2e CM |
219 | kmem_cache_free(extent_state_cache, state); |
220 | } | |
221 | } | |
d1310b2e CM |
222 | |
223 | static struct rb_node *tree_insert(struct rb_root *root, u64 offset, | |
224 | struct rb_node *node) | |
225 | { | |
d397712b CM |
226 | struct rb_node **p = &root->rb_node; |
227 | struct rb_node *parent = NULL; | |
d1310b2e CM |
228 | struct tree_entry *entry; |
229 | ||
d397712b | 230 | while (*p) { |
d1310b2e CM |
231 | parent = *p; |
232 | entry = rb_entry(parent, struct tree_entry, rb_node); | |
233 | ||
234 | if (offset < entry->start) | |
235 | p = &(*p)->rb_left; | |
236 | else if (offset > entry->end) | |
237 | p = &(*p)->rb_right; | |
238 | else | |
239 | return parent; | |
240 | } | |
241 | ||
d1310b2e CM |
242 | rb_link_node(node, parent, p); |
243 | rb_insert_color(node, root); | |
244 | return NULL; | |
245 | } | |
246 | ||
80ea96b1 | 247 | static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset, |
d1310b2e CM |
248 | struct rb_node **prev_ret, |
249 | struct rb_node **next_ret) | |
250 | { | |
80ea96b1 | 251 | struct rb_root *root = &tree->state; |
d397712b | 252 | struct rb_node *n = root->rb_node; |
d1310b2e CM |
253 | struct rb_node *prev = NULL; |
254 | struct rb_node *orig_prev = NULL; | |
255 | struct tree_entry *entry; | |
256 | struct tree_entry *prev_entry = NULL; | |
257 | ||
d397712b | 258 | while (n) { |
d1310b2e CM |
259 | entry = rb_entry(n, struct tree_entry, rb_node); |
260 | prev = n; | |
261 | prev_entry = entry; | |
262 | ||
263 | if (offset < entry->start) | |
264 | n = n->rb_left; | |
265 | else if (offset > entry->end) | |
266 | n = n->rb_right; | |
d397712b | 267 | else |
d1310b2e CM |
268 | return n; |
269 | } | |
270 | ||
271 | if (prev_ret) { | |
272 | orig_prev = prev; | |
d397712b | 273 | while (prev && offset > prev_entry->end) { |
d1310b2e CM |
274 | prev = rb_next(prev); |
275 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); | |
276 | } | |
277 | *prev_ret = prev; | |
278 | prev = orig_prev; | |
279 | } | |
280 | ||
281 | if (next_ret) { | |
282 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); | |
d397712b | 283 | while (prev && offset < prev_entry->start) { |
d1310b2e CM |
284 | prev = rb_prev(prev); |
285 | prev_entry = rb_entry(prev, struct tree_entry, rb_node); | |
286 | } | |
287 | *next_ret = prev; | |
288 | } | |
289 | return NULL; | |
290 | } | |
291 | ||
80ea96b1 CM |
292 | static inline struct rb_node *tree_search(struct extent_io_tree *tree, |
293 | u64 offset) | |
d1310b2e | 294 | { |
70dec807 | 295 | struct rb_node *prev = NULL; |
d1310b2e | 296 | struct rb_node *ret; |
70dec807 | 297 | |
80ea96b1 | 298 | ret = __etree_search(tree, offset, &prev, NULL); |
d397712b | 299 | if (!ret) |
d1310b2e CM |
300 | return prev; |
301 | return ret; | |
302 | } | |
303 | ||
9ed74f2d JB |
304 | static void merge_cb(struct extent_io_tree *tree, struct extent_state *new, |
305 | struct extent_state *other) | |
306 | { | |
307 | if (tree->ops && tree->ops->merge_extent_hook) | |
308 | tree->ops->merge_extent_hook(tree->mapping->host, new, | |
309 | other); | |
310 | } | |
311 | ||
d1310b2e CM |
312 | /* |
313 | * utility function to look for merge candidates inside a given range. | |
314 | * Any extents with matching state are merged together into a single | |
315 | * extent in the tree. Extents with EXTENT_IO in their state field | |
316 | * are not merged because the end_io handlers need to be able to do | |
317 | * operations on them without sleeping (or doing allocations/splits). | |
318 | * | |
319 | * This should be called with the tree lock held. | |
320 | */ | |
1bf85046 JM |
321 | static void merge_state(struct extent_io_tree *tree, |
322 | struct extent_state *state) | |
d1310b2e CM |
323 | { |
324 | struct extent_state *other; | |
325 | struct rb_node *other_node; | |
326 | ||
5b21f2ed | 327 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
1bf85046 | 328 | return; |
d1310b2e CM |
329 | |
330 | other_node = rb_prev(&state->rb_node); | |
331 | if (other_node) { | |
332 | other = rb_entry(other_node, struct extent_state, rb_node); | |
333 | if (other->end == state->start - 1 && | |
334 | other->state == state->state) { | |
9ed74f2d | 335 | merge_cb(tree, state, other); |
d1310b2e | 336 | state->start = other->start; |
70dec807 | 337 | other->tree = NULL; |
d1310b2e CM |
338 | rb_erase(&other->rb_node, &tree->state); |
339 | free_extent_state(other); | |
340 | } | |
341 | } | |
342 | other_node = rb_next(&state->rb_node); | |
343 | if (other_node) { | |
344 | other = rb_entry(other_node, struct extent_state, rb_node); | |
345 | if (other->start == state->end + 1 && | |
346 | other->state == state->state) { | |
9ed74f2d | 347 | merge_cb(tree, state, other); |
df98b6e2 JB |
348 | state->end = other->end; |
349 | other->tree = NULL; | |
350 | rb_erase(&other->rb_node, &tree->state); | |
351 | free_extent_state(other); | |
d1310b2e CM |
352 | } |
353 | } | |
d1310b2e CM |
354 | } |
355 | ||
1bf85046 | 356 | static void set_state_cb(struct extent_io_tree *tree, |
41074888 | 357 | struct extent_state *state, unsigned long *bits) |
291d673e | 358 | { |
1bf85046 JM |
359 | if (tree->ops && tree->ops->set_bit_hook) |
360 | tree->ops->set_bit_hook(tree->mapping->host, state, bits); | |
291d673e CM |
361 | } |
362 | ||
363 | static void clear_state_cb(struct extent_io_tree *tree, | |
41074888 | 364 | struct extent_state *state, unsigned long *bits) |
291d673e | 365 | { |
9ed74f2d JB |
366 | if (tree->ops && tree->ops->clear_bit_hook) |
367 | tree->ops->clear_bit_hook(tree->mapping->host, state, bits); | |
291d673e CM |
368 | } |
369 | ||
3150b699 | 370 | static void set_state_bits(struct extent_io_tree *tree, |
41074888 | 371 | struct extent_state *state, unsigned long *bits); |
3150b699 | 372 | |
d1310b2e CM |
373 | /* |
374 | * insert an extent_state struct into the tree. 'bits' are set on the | |
375 | * struct before it is inserted. | |
376 | * | |
377 | * This may return -EEXIST if the extent is already there, in which case the | |
378 | * state struct is freed. | |
379 | * | |
380 | * The tree lock is not taken internally. This is a utility function and | |
381 | * probably isn't what you want to call (see set/clear_extent_bit). | |
382 | */ | |
383 | static int insert_state(struct extent_io_tree *tree, | |
384 | struct extent_state *state, u64 start, u64 end, | |
41074888 | 385 | unsigned long *bits) |
d1310b2e CM |
386 | { |
387 | struct rb_node *node; | |
388 | ||
31b1a2bd JL |
389 | if (end < start) |
390 | WARN(1, KERN_ERR "btrfs end < start %llu %llu\n", | |
d397712b CM |
391 | (unsigned long long)end, |
392 | (unsigned long long)start); | |
d1310b2e CM |
393 | state->start = start; |
394 | state->end = end; | |
9ed74f2d | 395 | |
3150b699 XG |
396 | set_state_bits(tree, state, bits); |
397 | ||
d1310b2e CM |
398 | node = tree_insert(&tree->state, end, &state->rb_node); |
399 | if (node) { | |
400 | struct extent_state *found; | |
401 | found = rb_entry(node, struct extent_state, rb_node); | |
d397712b CM |
402 | printk(KERN_ERR "btrfs found node %llu %llu on insert of " |
403 | "%llu %llu\n", (unsigned long long)found->start, | |
404 | (unsigned long long)found->end, | |
405 | (unsigned long long)start, (unsigned long long)end); | |
d1310b2e CM |
406 | return -EEXIST; |
407 | } | |
70dec807 | 408 | state->tree = tree; |
d1310b2e CM |
409 | merge_state(tree, state); |
410 | return 0; | |
411 | } | |
412 | ||
1bf85046 | 413 | static void split_cb(struct extent_io_tree *tree, struct extent_state *orig, |
9ed74f2d JB |
414 | u64 split) |
415 | { | |
416 | if (tree->ops && tree->ops->split_extent_hook) | |
1bf85046 | 417 | tree->ops->split_extent_hook(tree->mapping->host, orig, split); |
9ed74f2d JB |
418 | } |
419 | ||
d1310b2e CM |
420 | /* |
421 | * split a given extent state struct in two, inserting the preallocated | |
422 | * struct 'prealloc' as the newly created second half. 'split' indicates an | |
423 | * offset inside 'orig' where it should be split. | |
424 | * | |
425 | * Before calling, | |
426 | * the tree has 'orig' at [orig->start, orig->end]. After calling, there | |
427 | * are two extent state structs in the tree: | |
428 | * prealloc: [orig->start, split - 1] | |
429 | * orig: [ split, orig->end ] | |
430 | * | |
431 | * The tree locks are not taken by this function. They need to be held | |
432 | * by the caller. | |
433 | */ | |
434 | static int split_state(struct extent_io_tree *tree, struct extent_state *orig, | |
435 | struct extent_state *prealloc, u64 split) | |
436 | { | |
437 | struct rb_node *node; | |
9ed74f2d JB |
438 | |
439 | split_cb(tree, orig, split); | |
440 | ||
d1310b2e CM |
441 | prealloc->start = orig->start; |
442 | prealloc->end = split - 1; | |
443 | prealloc->state = orig->state; | |
444 | orig->start = split; | |
445 | ||
446 | node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node); | |
447 | if (node) { | |
d1310b2e CM |
448 | free_extent_state(prealloc); |
449 | return -EEXIST; | |
450 | } | |
70dec807 | 451 | prealloc->tree = tree; |
d1310b2e CM |
452 | return 0; |
453 | } | |
454 | ||
cdc6a395 LZ |
455 | static struct extent_state *next_state(struct extent_state *state) |
456 | { | |
457 | struct rb_node *next = rb_next(&state->rb_node); | |
458 | if (next) | |
459 | return rb_entry(next, struct extent_state, rb_node); | |
460 | else | |
461 | return NULL; | |
462 | } | |
463 | ||
d1310b2e CM |
464 | /* |
465 | * utility function to clear some bits in an extent state struct. | |
1b303fc0 | 466 | * it will optionally wake up any one waiting on this state (wake == 1). |
d1310b2e CM |
467 | * |
468 | * If no bits are set on the state struct after clearing things, the | |
469 | * struct is freed and removed from the tree | |
470 | */ | |
cdc6a395 LZ |
471 | static struct extent_state *clear_state_bit(struct extent_io_tree *tree, |
472 | struct extent_state *state, | |
41074888 | 473 | unsigned long *bits, int wake) |
d1310b2e | 474 | { |
cdc6a395 | 475 | struct extent_state *next; |
41074888 | 476 | unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS; |
d1310b2e | 477 | |
0ca1f7ce | 478 | if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) { |
d1310b2e CM |
479 | u64 range = state->end - state->start + 1; |
480 | WARN_ON(range > tree->dirty_bytes); | |
481 | tree->dirty_bytes -= range; | |
482 | } | |
291d673e | 483 | clear_state_cb(tree, state, bits); |
32c00aff | 484 | state->state &= ~bits_to_clear; |
d1310b2e CM |
485 | if (wake) |
486 | wake_up(&state->wq); | |
0ca1f7ce | 487 | if (state->state == 0) { |
cdc6a395 | 488 | next = next_state(state); |
70dec807 | 489 | if (state->tree) { |
d1310b2e | 490 | rb_erase(&state->rb_node, &tree->state); |
70dec807 | 491 | state->tree = NULL; |
d1310b2e CM |
492 | free_extent_state(state); |
493 | } else { | |
494 | WARN_ON(1); | |
495 | } | |
496 | } else { | |
497 | merge_state(tree, state); | |
cdc6a395 | 498 | next = next_state(state); |
d1310b2e | 499 | } |
cdc6a395 | 500 | return next; |
d1310b2e CM |
501 | } |
502 | ||
8233767a XG |
503 | static struct extent_state * |
504 | alloc_extent_state_atomic(struct extent_state *prealloc) | |
505 | { | |
506 | if (!prealloc) | |
507 | prealloc = alloc_extent_state(GFP_ATOMIC); | |
508 | ||
509 | return prealloc; | |
510 | } | |
511 | ||
48a3b636 | 512 | static void extent_io_tree_panic(struct extent_io_tree *tree, int err) |
c2d904e0 JM |
513 | { |
514 | btrfs_panic(tree_fs_info(tree), err, "Locking error: " | |
515 | "Extent tree was modified by another " | |
516 | "thread while locked."); | |
517 | } | |
518 | ||
d1310b2e CM |
519 | /* |
520 | * clear some bits on a range in the tree. This may require splitting | |
521 | * or inserting elements in the tree, so the gfp mask is used to | |
522 | * indicate which allocations or sleeping are allowed. | |
523 | * | |
524 | * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove | |
525 | * the given range from the tree regardless of state (ie for truncate). | |
526 | * | |
527 | * the range [start, end] is inclusive. | |
528 | * | |
6763af84 | 529 | * This takes the tree lock, and returns 0 on success and < 0 on error. |
d1310b2e CM |
530 | */ |
531 | int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, | |
41074888 | 532 | unsigned long bits, int wake, int delete, |
2c64c53d CM |
533 | struct extent_state **cached_state, |
534 | gfp_t mask) | |
d1310b2e CM |
535 | { |
536 | struct extent_state *state; | |
2c64c53d | 537 | struct extent_state *cached; |
d1310b2e CM |
538 | struct extent_state *prealloc = NULL; |
539 | struct rb_node *node; | |
5c939df5 | 540 | u64 last_end; |
d1310b2e | 541 | int err; |
2ac55d41 | 542 | int clear = 0; |
d1310b2e | 543 | |
8d599ae1 DS |
544 | btrfs_debug_check_extent_io_range(tree->mapping->host, start, end); |
545 | ||
7ee9e440 JB |
546 | if (bits & EXTENT_DELALLOC) |
547 | bits |= EXTENT_NORESERVE; | |
548 | ||
0ca1f7ce YZ |
549 | if (delete) |
550 | bits |= ~EXTENT_CTLBITS; | |
551 | bits |= EXTENT_FIRST_DELALLOC; | |
552 | ||
2ac55d41 JB |
553 | if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) |
554 | clear = 1; | |
d1310b2e CM |
555 | again: |
556 | if (!prealloc && (mask & __GFP_WAIT)) { | |
557 | prealloc = alloc_extent_state(mask); | |
558 | if (!prealloc) | |
559 | return -ENOMEM; | |
560 | } | |
561 | ||
cad321ad | 562 | spin_lock(&tree->lock); |
2c64c53d CM |
563 | if (cached_state) { |
564 | cached = *cached_state; | |
2ac55d41 JB |
565 | |
566 | if (clear) { | |
567 | *cached_state = NULL; | |
568 | cached_state = NULL; | |
569 | } | |
570 | ||
df98b6e2 JB |
571 | if (cached && cached->tree && cached->start <= start && |
572 | cached->end > start) { | |
2ac55d41 JB |
573 | if (clear) |
574 | atomic_dec(&cached->refs); | |
2c64c53d | 575 | state = cached; |
42daec29 | 576 | goto hit_next; |
2c64c53d | 577 | } |
2ac55d41 JB |
578 | if (clear) |
579 | free_extent_state(cached); | |
2c64c53d | 580 | } |
d1310b2e CM |
581 | /* |
582 | * this search will find the extents that end after | |
583 | * our range starts | |
584 | */ | |
80ea96b1 | 585 | node = tree_search(tree, start); |
d1310b2e CM |
586 | if (!node) |
587 | goto out; | |
588 | state = rb_entry(node, struct extent_state, rb_node); | |
2c64c53d | 589 | hit_next: |
d1310b2e CM |
590 | if (state->start > end) |
591 | goto out; | |
592 | WARN_ON(state->end < start); | |
5c939df5 | 593 | last_end = state->end; |
d1310b2e | 594 | |
0449314a | 595 | /* the state doesn't have the wanted bits, go ahead */ |
cdc6a395 LZ |
596 | if (!(state->state & bits)) { |
597 | state = next_state(state); | |
0449314a | 598 | goto next; |
cdc6a395 | 599 | } |
0449314a | 600 | |
d1310b2e CM |
601 | /* |
602 | * | ---- desired range ---- | | |
603 | * | state | or | |
604 | * | ------------- state -------------- | | |
605 | * | |
606 | * We need to split the extent we found, and may flip | |
607 | * bits on second half. | |
608 | * | |
609 | * If the extent we found extends past our range, we | |
610 | * just split and search again. It'll get split again | |
611 | * the next time though. | |
612 | * | |
613 | * If the extent we found is inside our range, we clear | |
614 | * the desired bit on it. | |
615 | */ | |
616 | ||
617 | if (state->start < start) { | |
8233767a XG |
618 | prealloc = alloc_extent_state_atomic(prealloc); |
619 | BUG_ON(!prealloc); | |
d1310b2e | 620 | err = split_state(tree, state, prealloc, start); |
c2d904e0 JM |
621 | if (err) |
622 | extent_io_tree_panic(tree, err); | |
623 | ||
d1310b2e CM |
624 | prealloc = NULL; |
625 | if (err) | |
626 | goto out; | |
627 | if (state->end <= end) { | |
d1ac6e41 LB |
628 | state = clear_state_bit(tree, state, &bits, wake); |
629 | goto next; | |
d1310b2e CM |
630 | } |
631 | goto search_again; | |
632 | } | |
633 | /* | |
634 | * | ---- desired range ---- | | |
635 | * | state | | |
636 | * We need to split the extent, and clear the bit | |
637 | * on the first half | |
638 | */ | |
639 | if (state->start <= end && state->end > end) { | |
8233767a XG |
640 | prealloc = alloc_extent_state_atomic(prealloc); |
641 | BUG_ON(!prealloc); | |
d1310b2e | 642 | err = split_state(tree, state, prealloc, end + 1); |
c2d904e0 JM |
643 | if (err) |
644 | extent_io_tree_panic(tree, err); | |
645 | ||
d1310b2e CM |
646 | if (wake) |
647 | wake_up(&state->wq); | |
42daec29 | 648 | |
6763af84 | 649 | clear_state_bit(tree, prealloc, &bits, wake); |
9ed74f2d | 650 | |
d1310b2e CM |
651 | prealloc = NULL; |
652 | goto out; | |
653 | } | |
42daec29 | 654 | |
cdc6a395 | 655 | state = clear_state_bit(tree, state, &bits, wake); |
0449314a | 656 | next: |
5c939df5 YZ |
657 | if (last_end == (u64)-1) |
658 | goto out; | |
659 | start = last_end + 1; | |
cdc6a395 | 660 | if (start <= end && state && !need_resched()) |
692e5759 | 661 | goto hit_next; |
d1310b2e CM |
662 | goto search_again; |
663 | ||
664 | out: | |
cad321ad | 665 | spin_unlock(&tree->lock); |
d1310b2e CM |
666 | if (prealloc) |
667 | free_extent_state(prealloc); | |
668 | ||
6763af84 | 669 | return 0; |
d1310b2e CM |
670 | |
671 | search_again: | |
672 | if (start > end) | |
673 | goto out; | |
cad321ad | 674 | spin_unlock(&tree->lock); |
d1310b2e CM |
675 | if (mask & __GFP_WAIT) |
676 | cond_resched(); | |
677 | goto again; | |
678 | } | |
d1310b2e | 679 | |
143bede5 JM |
680 | static void wait_on_state(struct extent_io_tree *tree, |
681 | struct extent_state *state) | |
641f5219 CH |
682 | __releases(tree->lock) |
683 | __acquires(tree->lock) | |
d1310b2e CM |
684 | { |
685 | DEFINE_WAIT(wait); | |
686 | prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE); | |
cad321ad | 687 | spin_unlock(&tree->lock); |
d1310b2e | 688 | schedule(); |
cad321ad | 689 | spin_lock(&tree->lock); |
d1310b2e | 690 | finish_wait(&state->wq, &wait); |
d1310b2e CM |
691 | } |
692 | ||
693 | /* | |
694 | * waits for one or more bits to clear on a range in the state tree. | |
695 | * The range [start, end] is inclusive. | |
696 | * The tree lock is taken by this function | |
697 | */ | |
41074888 DS |
698 | static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
699 | unsigned long bits) | |
d1310b2e CM |
700 | { |
701 | struct extent_state *state; | |
702 | struct rb_node *node; | |
703 | ||
8d599ae1 DS |
704 | btrfs_debug_check_extent_io_range(tree->mapping->host, start, end); |
705 | ||
cad321ad | 706 | spin_lock(&tree->lock); |
d1310b2e CM |
707 | again: |
708 | while (1) { | |
709 | /* | |
710 | * this search will find all the extents that end after | |
711 | * our range starts | |
712 | */ | |
80ea96b1 | 713 | node = tree_search(tree, start); |
d1310b2e CM |
714 | if (!node) |
715 | break; | |
716 | ||
717 | state = rb_entry(node, struct extent_state, rb_node); | |
718 | ||
719 | if (state->start > end) | |
720 | goto out; | |
721 | ||
722 | if (state->state & bits) { | |
723 | start = state->start; | |
724 | atomic_inc(&state->refs); | |
725 | wait_on_state(tree, state); | |
726 | free_extent_state(state); | |
727 | goto again; | |
728 | } | |
729 | start = state->end + 1; | |
730 | ||
731 | if (start > end) | |
732 | break; | |
733 | ||
ded91f08 | 734 | cond_resched_lock(&tree->lock); |
d1310b2e CM |
735 | } |
736 | out: | |
cad321ad | 737 | spin_unlock(&tree->lock); |
d1310b2e | 738 | } |
d1310b2e | 739 | |
1bf85046 | 740 | static void set_state_bits(struct extent_io_tree *tree, |
d1310b2e | 741 | struct extent_state *state, |
41074888 | 742 | unsigned long *bits) |
d1310b2e | 743 | { |
41074888 | 744 | unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS; |
9ed74f2d | 745 | |
1bf85046 | 746 | set_state_cb(tree, state, bits); |
0ca1f7ce | 747 | if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) { |
d1310b2e CM |
748 | u64 range = state->end - state->start + 1; |
749 | tree->dirty_bytes += range; | |
750 | } | |
0ca1f7ce | 751 | state->state |= bits_to_set; |
d1310b2e CM |
752 | } |
753 | ||
2c64c53d CM |
754 | static void cache_state(struct extent_state *state, |
755 | struct extent_state **cached_ptr) | |
756 | { | |
757 | if (cached_ptr && !(*cached_ptr)) { | |
758 | if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) { | |
759 | *cached_ptr = state; | |
760 | atomic_inc(&state->refs); | |
761 | } | |
762 | } | |
763 | } | |
764 | ||
507903b8 AJ |
765 | static void uncache_state(struct extent_state **cached_ptr) |
766 | { | |
767 | if (cached_ptr && (*cached_ptr)) { | |
768 | struct extent_state *state = *cached_ptr; | |
109b36a2 CM |
769 | *cached_ptr = NULL; |
770 | free_extent_state(state); | |
507903b8 AJ |
771 | } |
772 | } | |
773 | ||
d1310b2e | 774 | /* |
1edbb734 CM |
775 | * set some bits on a range in the tree. This may require allocations or |
776 | * sleeping, so the gfp mask is used to indicate what is allowed. | |
d1310b2e | 777 | * |
1edbb734 CM |
778 | * If any of the exclusive bits are set, this will fail with -EEXIST if some |
779 | * part of the range already has the desired bits set. The start of the | |
780 | * existing range is returned in failed_start in this case. | |
d1310b2e | 781 | * |
1edbb734 | 782 | * [start, end] is inclusive This takes the tree lock. |
d1310b2e | 783 | */ |
1edbb734 | 784 | |
3fbe5c02 JM |
785 | static int __must_check |
786 | __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, | |
41074888 DS |
787 | unsigned long bits, unsigned long exclusive_bits, |
788 | u64 *failed_start, struct extent_state **cached_state, | |
789 | gfp_t mask) | |
d1310b2e CM |
790 | { |
791 | struct extent_state *state; | |
792 | struct extent_state *prealloc = NULL; | |
793 | struct rb_node *node; | |
d1310b2e | 794 | int err = 0; |
d1310b2e CM |
795 | u64 last_start; |
796 | u64 last_end; | |
42daec29 | 797 | |
8d599ae1 DS |
798 | btrfs_debug_check_extent_io_range(tree->mapping->host, start, end); |
799 | ||
0ca1f7ce | 800 | bits |= EXTENT_FIRST_DELALLOC; |
d1310b2e CM |
801 | again: |
802 | if (!prealloc && (mask & __GFP_WAIT)) { | |
803 | prealloc = alloc_extent_state(mask); | |
8233767a | 804 | BUG_ON(!prealloc); |
d1310b2e CM |
805 | } |
806 | ||
cad321ad | 807 | spin_lock(&tree->lock); |
9655d298 CM |
808 | if (cached_state && *cached_state) { |
809 | state = *cached_state; | |
df98b6e2 JB |
810 | if (state->start <= start && state->end > start && |
811 | state->tree) { | |
9655d298 CM |
812 | node = &state->rb_node; |
813 | goto hit_next; | |
814 | } | |
815 | } | |
d1310b2e CM |
816 | /* |
817 | * this search will find all the extents that end after | |
818 | * our range starts. | |
819 | */ | |
80ea96b1 | 820 | node = tree_search(tree, start); |
d1310b2e | 821 | if (!node) { |
8233767a XG |
822 | prealloc = alloc_extent_state_atomic(prealloc); |
823 | BUG_ON(!prealloc); | |
0ca1f7ce | 824 | err = insert_state(tree, prealloc, start, end, &bits); |
c2d904e0 JM |
825 | if (err) |
826 | extent_io_tree_panic(tree, err); | |
827 | ||
d1310b2e | 828 | prealloc = NULL; |
d1310b2e CM |
829 | goto out; |
830 | } | |
d1310b2e | 831 | state = rb_entry(node, struct extent_state, rb_node); |
40431d6c | 832 | hit_next: |
d1310b2e CM |
833 | last_start = state->start; |
834 | last_end = state->end; | |
835 | ||
836 | /* | |
837 | * | ---- desired range ---- | | |
838 | * | state | | |
839 | * | |
840 | * Just lock what we found and keep going | |
841 | */ | |
842 | if (state->start == start && state->end <= end) { | |
1edbb734 | 843 | if (state->state & exclusive_bits) { |
d1310b2e CM |
844 | *failed_start = state->start; |
845 | err = -EEXIST; | |
846 | goto out; | |
847 | } | |
42daec29 | 848 | |
1bf85046 | 849 | set_state_bits(tree, state, &bits); |
2c64c53d | 850 | cache_state(state, cached_state); |
d1310b2e | 851 | merge_state(tree, state); |
5c939df5 YZ |
852 | if (last_end == (u64)-1) |
853 | goto out; | |
854 | start = last_end + 1; | |
d1ac6e41 LB |
855 | state = next_state(state); |
856 | if (start < end && state && state->start == start && | |
857 | !need_resched()) | |
858 | goto hit_next; | |
d1310b2e CM |
859 | goto search_again; |
860 | } | |
861 | ||
862 | /* | |
863 | * | ---- desired range ---- | | |
864 | * | state | | |
865 | * or | |
866 | * | ------------- state -------------- | | |
867 | * | |
868 | * We need to split the extent we found, and may flip bits on | |
869 | * second half. | |
870 | * | |
871 | * If the extent we found extends past our | |
872 | * range, we just split and search again. It'll get split | |
873 | * again the next time though. | |
874 | * | |
875 | * If the extent we found is inside our range, we set the | |
876 | * desired bit on it. | |
877 | */ | |
878 | if (state->start < start) { | |
1edbb734 | 879 | if (state->state & exclusive_bits) { |
d1310b2e CM |
880 | *failed_start = start; |
881 | err = -EEXIST; | |
882 | goto out; | |
883 | } | |
8233767a XG |
884 | |
885 | prealloc = alloc_extent_state_atomic(prealloc); | |
886 | BUG_ON(!prealloc); | |
d1310b2e | 887 | err = split_state(tree, state, prealloc, start); |
c2d904e0 JM |
888 | if (err) |
889 | extent_io_tree_panic(tree, err); | |
890 | ||
d1310b2e CM |
891 | prealloc = NULL; |
892 | if (err) | |
893 | goto out; | |
894 | if (state->end <= end) { | |
1bf85046 | 895 | set_state_bits(tree, state, &bits); |
2c64c53d | 896 | cache_state(state, cached_state); |
d1310b2e | 897 | merge_state(tree, state); |
5c939df5 YZ |
898 | if (last_end == (u64)-1) |
899 | goto out; | |
900 | start = last_end + 1; | |
d1ac6e41 LB |
901 | state = next_state(state); |
902 | if (start < end && state && state->start == start && | |
903 | !need_resched()) | |
904 | goto hit_next; | |
d1310b2e CM |
905 | } |
906 | goto search_again; | |
907 | } | |
908 | /* | |
909 | * | ---- desired range ---- | | |
910 | * | state | or | state | | |
911 | * | |
912 | * There's a hole, we need to insert something in it and | |
913 | * ignore the extent we found. | |
914 | */ | |
915 | if (state->start > start) { | |
916 | u64 this_end; | |
917 | if (end < last_start) | |
918 | this_end = end; | |
919 | else | |
d397712b | 920 | this_end = last_start - 1; |
8233767a XG |
921 | |
922 | prealloc = alloc_extent_state_atomic(prealloc); | |
923 | BUG_ON(!prealloc); | |
c7f895a2 XG |
924 | |
925 | /* | |
926 | * Avoid to free 'prealloc' if it can be merged with | |
927 | * the later extent. | |
928 | */ | |
d1310b2e | 929 | err = insert_state(tree, prealloc, start, this_end, |
0ca1f7ce | 930 | &bits); |
c2d904e0 JM |
931 | if (err) |
932 | extent_io_tree_panic(tree, err); | |
933 | ||
9ed74f2d JB |
934 | cache_state(prealloc, cached_state); |
935 | prealloc = NULL; | |
d1310b2e CM |
936 | start = this_end + 1; |
937 | goto search_again; | |
938 | } | |
939 | /* | |
940 | * | ---- desired range ---- | | |
941 | * | state | | |
942 | * We need to split the extent, and set the bit | |
943 | * on the first half | |
944 | */ | |
945 | if (state->start <= end && state->end > end) { | |
1edbb734 | 946 | if (state->state & exclusive_bits) { |
d1310b2e CM |
947 | *failed_start = start; |
948 | err = -EEXIST; | |
949 | goto out; | |
950 | } | |
8233767a XG |
951 | |
952 | prealloc = alloc_extent_state_atomic(prealloc); | |
953 | BUG_ON(!prealloc); | |
d1310b2e | 954 | err = split_state(tree, state, prealloc, end + 1); |
c2d904e0 JM |
955 | if (err) |
956 | extent_io_tree_panic(tree, err); | |
d1310b2e | 957 | |
1bf85046 | 958 | set_state_bits(tree, prealloc, &bits); |
2c64c53d | 959 | cache_state(prealloc, cached_state); |
d1310b2e CM |
960 | merge_state(tree, prealloc); |
961 | prealloc = NULL; | |
962 | goto out; | |
963 | } | |
964 | ||
965 | goto search_again; | |
966 | ||
967 | out: | |
cad321ad | 968 | spin_unlock(&tree->lock); |
d1310b2e CM |
969 | if (prealloc) |
970 | free_extent_state(prealloc); | |
971 | ||
972 | return err; | |
973 | ||
974 | search_again: | |
975 | if (start > end) | |
976 | goto out; | |
cad321ad | 977 | spin_unlock(&tree->lock); |
d1310b2e CM |
978 | if (mask & __GFP_WAIT) |
979 | cond_resched(); | |
980 | goto again; | |
981 | } | |
d1310b2e | 982 | |
41074888 DS |
983 | int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, |
984 | unsigned long bits, u64 * failed_start, | |
985 | struct extent_state **cached_state, gfp_t mask) | |
3fbe5c02 JM |
986 | { |
987 | return __set_extent_bit(tree, start, end, bits, 0, failed_start, | |
988 | cached_state, mask); | |
989 | } | |
990 | ||
991 | ||
462d6fac | 992 | /** |
10983f2e LB |
993 | * convert_extent_bit - convert all bits in a given range from one bit to |
994 | * another | |
462d6fac JB |
995 | * @tree: the io tree to search |
996 | * @start: the start offset in bytes | |
997 | * @end: the end offset in bytes (inclusive) | |
998 | * @bits: the bits to set in this range | |
999 | * @clear_bits: the bits to clear in this range | |
e6138876 | 1000 | * @cached_state: state that we're going to cache |
462d6fac JB |
1001 | * @mask: the allocation mask |
1002 | * | |
1003 | * This will go through and set bits for the given range. If any states exist | |
1004 | * already in this range they are set with the given bit and cleared of the | |
1005 | * clear_bits. This is only meant to be used by things that are mergeable, ie | |
1006 | * converting from say DELALLOC to DIRTY. This is not meant to be used with | |
1007 | * boundary bits like LOCK. | |
1008 | */ | |
1009 | int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, | |
41074888 | 1010 | unsigned long bits, unsigned long clear_bits, |
e6138876 | 1011 | struct extent_state **cached_state, gfp_t mask) |
462d6fac JB |
1012 | { |
1013 | struct extent_state *state; | |
1014 | struct extent_state *prealloc = NULL; | |
1015 | struct rb_node *node; | |
1016 | int err = 0; | |
1017 | u64 last_start; | |
1018 | u64 last_end; | |
1019 | ||
8d599ae1 DS |
1020 | btrfs_debug_check_extent_io_range(tree->mapping->host, start, end); |
1021 | ||
462d6fac JB |
1022 | again: |
1023 | if (!prealloc && (mask & __GFP_WAIT)) { | |
1024 | prealloc = alloc_extent_state(mask); | |
1025 | if (!prealloc) | |
1026 | return -ENOMEM; | |
1027 | } | |
1028 | ||
1029 | spin_lock(&tree->lock); | |
e6138876 JB |
1030 | if (cached_state && *cached_state) { |
1031 | state = *cached_state; | |
1032 | if (state->start <= start && state->end > start && | |
1033 | state->tree) { | |
1034 | node = &state->rb_node; | |
1035 | goto hit_next; | |
1036 | } | |
1037 | } | |
1038 | ||
462d6fac JB |
1039 | /* |
1040 | * this search will find all the extents that end after | |
1041 | * our range starts. | |
1042 | */ | |
1043 | node = tree_search(tree, start); | |
1044 | if (!node) { | |
1045 | prealloc = alloc_extent_state_atomic(prealloc); | |
1cf4ffdb LB |
1046 | if (!prealloc) { |
1047 | err = -ENOMEM; | |
1048 | goto out; | |
1049 | } | |
462d6fac JB |
1050 | err = insert_state(tree, prealloc, start, end, &bits); |
1051 | prealloc = NULL; | |
c2d904e0 JM |
1052 | if (err) |
1053 | extent_io_tree_panic(tree, err); | |
462d6fac JB |
1054 | goto out; |
1055 | } | |
1056 | state = rb_entry(node, struct extent_state, rb_node); | |
1057 | hit_next: | |
1058 | last_start = state->start; | |
1059 | last_end = state->end; | |
1060 | ||
1061 | /* | |
1062 | * | ---- desired range ---- | | |
1063 | * | state | | |
1064 | * | |
1065 | * Just lock what we found and keep going | |
1066 | */ | |
1067 | if (state->start == start && state->end <= end) { | |
462d6fac | 1068 | set_state_bits(tree, state, &bits); |
e6138876 | 1069 | cache_state(state, cached_state); |
d1ac6e41 | 1070 | state = clear_state_bit(tree, state, &clear_bits, 0); |
462d6fac JB |
1071 | if (last_end == (u64)-1) |
1072 | goto out; | |
462d6fac | 1073 | start = last_end + 1; |
d1ac6e41 LB |
1074 | if (start < end && state && state->start == start && |
1075 | !need_resched()) | |
1076 | goto hit_next; | |
462d6fac JB |
1077 | goto search_again; |
1078 | } | |
1079 | ||
1080 | /* | |
1081 | * | ---- desired range ---- | | |
1082 | * | state | | |
1083 | * or | |
1084 | * | ------------- state -------------- | | |
1085 | * | |
1086 | * We need to split the extent we found, and may flip bits on | |
1087 | * second half. | |
1088 | * | |
1089 | * If the extent we found extends past our | |
1090 | * range, we just split and search again. It'll get split | |
1091 | * again the next time though. | |
1092 | * | |
1093 | * If the extent we found is inside our range, we set the | |
1094 | * desired bit on it. | |
1095 | */ | |
1096 | if (state->start < start) { | |
1097 | prealloc = alloc_extent_state_atomic(prealloc); | |
1cf4ffdb LB |
1098 | if (!prealloc) { |
1099 | err = -ENOMEM; | |
1100 | goto out; | |
1101 | } | |
462d6fac | 1102 | err = split_state(tree, state, prealloc, start); |
c2d904e0 JM |
1103 | if (err) |
1104 | extent_io_tree_panic(tree, err); | |
462d6fac JB |
1105 | prealloc = NULL; |
1106 | if (err) | |
1107 | goto out; | |
1108 | if (state->end <= end) { | |
1109 | set_state_bits(tree, state, &bits); | |
e6138876 | 1110 | cache_state(state, cached_state); |
d1ac6e41 | 1111 | state = clear_state_bit(tree, state, &clear_bits, 0); |
462d6fac JB |
1112 | if (last_end == (u64)-1) |
1113 | goto out; | |
1114 | start = last_end + 1; | |
d1ac6e41 LB |
1115 | if (start < end && state && state->start == start && |
1116 | !need_resched()) | |
1117 | goto hit_next; | |
462d6fac JB |
1118 | } |
1119 | goto search_again; | |
1120 | } | |
1121 | /* | |
1122 | * | ---- desired range ---- | | |
1123 | * | state | or | state | | |
1124 | * | |
1125 | * There's a hole, we need to insert something in it and | |
1126 | * ignore the extent we found. | |
1127 | */ | |
1128 | if (state->start > start) { | |
1129 | u64 this_end; | |
1130 | if (end < last_start) | |
1131 | this_end = end; | |
1132 | else | |
1133 | this_end = last_start - 1; | |
1134 | ||
1135 | prealloc = alloc_extent_state_atomic(prealloc); | |
1cf4ffdb LB |
1136 | if (!prealloc) { |
1137 | err = -ENOMEM; | |
1138 | goto out; | |
1139 | } | |
462d6fac JB |
1140 | |
1141 | /* | |
1142 | * Avoid to free 'prealloc' if it can be merged with | |
1143 | * the later extent. | |
1144 | */ | |
1145 | err = insert_state(tree, prealloc, start, this_end, | |
1146 | &bits); | |
c2d904e0 JM |
1147 | if (err) |
1148 | extent_io_tree_panic(tree, err); | |
e6138876 | 1149 | cache_state(prealloc, cached_state); |
462d6fac JB |
1150 | prealloc = NULL; |
1151 | start = this_end + 1; | |
1152 | goto search_again; | |
1153 | } | |
1154 | /* | |
1155 | * | ---- desired range ---- | | |
1156 | * | state | | |
1157 | * We need to split the extent, and set the bit | |
1158 | * on the first half | |
1159 | */ | |
1160 | if (state->start <= end && state->end > end) { | |
1161 | prealloc = alloc_extent_state_atomic(prealloc); | |
1cf4ffdb LB |
1162 | if (!prealloc) { |
1163 | err = -ENOMEM; | |
1164 | goto out; | |
1165 | } | |
462d6fac JB |
1166 | |
1167 | err = split_state(tree, state, prealloc, end + 1); | |
c2d904e0 JM |
1168 | if (err) |
1169 | extent_io_tree_panic(tree, err); | |
462d6fac JB |
1170 | |
1171 | set_state_bits(tree, prealloc, &bits); | |
e6138876 | 1172 | cache_state(prealloc, cached_state); |
462d6fac | 1173 | clear_state_bit(tree, prealloc, &clear_bits, 0); |
462d6fac JB |
1174 | prealloc = NULL; |
1175 | goto out; | |
1176 | } | |
1177 | ||
1178 | goto search_again; | |
1179 | ||
1180 | out: | |
1181 | spin_unlock(&tree->lock); | |
1182 | if (prealloc) | |
1183 | free_extent_state(prealloc); | |
1184 | ||
1185 | return err; | |
1186 | ||
1187 | search_again: | |
1188 | if (start > end) | |
1189 | goto out; | |
1190 | spin_unlock(&tree->lock); | |
1191 | if (mask & __GFP_WAIT) | |
1192 | cond_resched(); | |
1193 | goto again; | |
1194 | } | |
1195 | ||
d1310b2e CM |
1196 | /* wrappers around set/clear extent bit */ |
1197 | int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, | |
1198 | gfp_t mask) | |
1199 | { | |
3fbe5c02 | 1200 | return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL, |
2c64c53d | 1201 | NULL, mask); |
d1310b2e | 1202 | } |
d1310b2e CM |
1203 | |
1204 | int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, | |
41074888 | 1205 | unsigned long bits, gfp_t mask) |
d1310b2e | 1206 | { |
3fbe5c02 | 1207 | return set_extent_bit(tree, start, end, bits, NULL, |
2c64c53d | 1208 | NULL, mask); |
d1310b2e | 1209 | } |
d1310b2e CM |
1210 | |
1211 | int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, | |
41074888 | 1212 | unsigned long bits, gfp_t mask) |
d1310b2e | 1213 | { |
2c64c53d | 1214 | return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask); |
d1310b2e | 1215 | } |
d1310b2e CM |
1216 | |
1217 | int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, | |
2ac55d41 | 1218 | struct extent_state **cached_state, gfp_t mask) |
d1310b2e CM |
1219 | { |
1220 | return set_extent_bit(tree, start, end, | |
fee187d9 | 1221 | EXTENT_DELALLOC | EXTENT_UPTODATE, |
3fbe5c02 | 1222 | NULL, cached_state, mask); |
d1310b2e | 1223 | } |
d1310b2e | 1224 | |
9e8a4a8b LB |
1225 | int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end, |
1226 | struct extent_state **cached_state, gfp_t mask) | |
1227 | { | |
1228 | return set_extent_bit(tree, start, end, | |
1229 | EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG, | |
1230 | NULL, cached_state, mask); | |
1231 | } | |
1232 | ||
d1310b2e CM |
1233 | int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, |
1234 | gfp_t mask) | |
1235 | { | |
1236 | return clear_extent_bit(tree, start, end, | |
32c00aff | 1237 | EXTENT_DIRTY | EXTENT_DELALLOC | |
0ca1f7ce | 1238 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask); |
d1310b2e | 1239 | } |
d1310b2e CM |
1240 | |
1241 | int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end, | |
1242 | gfp_t mask) | |
1243 | { | |
3fbe5c02 | 1244 | return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, |
2c64c53d | 1245 | NULL, mask); |
d1310b2e | 1246 | } |
d1310b2e | 1247 | |
d1310b2e | 1248 | int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
507903b8 | 1249 | struct extent_state **cached_state, gfp_t mask) |
d1310b2e | 1250 | { |
6b67a320 | 1251 | return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL, |
3fbe5c02 | 1252 | cached_state, mask); |
d1310b2e | 1253 | } |
d1310b2e | 1254 | |
5fd02043 JB |
1255 | int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end, |
1256 | struct extent_state **cached_state, gfp_t mask) | |
d1310b2e | 1257 | { |
2c64c53d | 1258 | return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, |
2ac55d41 | 1259 | cached_state, mask); |
d1310b2e | 1260 | } |
d1310b2e | 1261 | |
d352ac68 CM |
1262 | /* |
1263 | * either insert or lock state struct between start and end use mask to tell | |
1264 | * us if waiting is desired. | |
1265 | */ | |
1edbb734 | 1266 | int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, |
41074888 | 1267 | unsigned long bits, struct extent_state **cached_state) |
d1310b2e CM |
1268 | { |
1269 | int err; | |
1270 | u64 failed_start; | |
1271 | while (1) { | |
3fbe5c02 JM |
1272 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits, |
1273 | EXTENT_LOCKED, &failed_start, | |
1274 | cached_state, GFP_NOFS); | |
d0082371 | 1275 | if (err == -EEXIST) { |
d1310b2e CM |
1276 | wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED); |
1277 | start = failed_start; | |
d0082371 | 1278 | } else |
d1310b2e | 1279 | break; |
d1310b2e CM |
1280 | WARN_ON(start > end); |
1281 | } | |
1282 | return err; | |
1283 | } | |
d1310b2e | 1284 | |
d0082371 | 1285 | int lock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
1edbb734 | 1286 | { |
d0082371 | 1287 | return lock_extent_bits(tree, start, end, 0, NULL); |
1edbb734 CM |
1288 | } |
1289 | ||
d0082371 | 1290 | int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
25179201 JB |
1291 | { |
1292 | int err; | |
1293 | u64 failed_start; | |
1294 | ||
3fbe5c02 JM |
1295 | err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED, |
1296 | &failed_start, NULL, GFP_NOFS); | |
6643558d YZ |
1297 | if (err == -EEXIST) { |
1298 | if (failed_start > start) | |
1299 | clear_extent_bit(tree, start, failed_start - 1, | |
d0082371 | 1300 | EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS); |
25179201 | 1301 | return 0; |
6643558d | 1302 | } |
25179201 JB |
1303 | return 1; |
1304 | } | |
25179201 | 1305 | |
2c64c53d CM |
1306 | int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end, |
1307 | struct extent_state **cached, gfp_t mask) | |
1308 | { | |
1309 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached, | |
1310 | mask); | |
1311 | } | |
1312 | ||
d0082371 | 1313 | int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end) |
d1310b2e | 1314 | { |
2c64c53d | 1315 | return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL, |
d0082371 | 1316 | GFP_NOFS); |
d1310b2e | 1317 | } |
d1310b2e | 1318 | |
4adaa611 CM |
1319 | int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end) |
1320 | { | |
1321 | unsigned long index = start >> PAGE_CACHE_SHIFT; | |
1322 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; | |
1323 | struct page *page; | |
1324 | ||
1325 | while (index <= end_index) { | |
1326 | page = find_get_page(inode->i_mapping, index); | |
1327 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ | |
1328 | clear_page_dirty_for_io(page); | |
1329 | page_cache_release(page); | |
1330 | index++; | |
1331 | } | |
1332 | return 0; | |
1333 | } | |
1334 | ||
1335 | int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end) | |
1336 | { | |
1337 | unsigned long index = start >> PAGE_CACHE_SHIFT; | |
1338 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; | |
1339 | struct page *page; | |
1340 | ||
1341 | while (index <= end_index) { | |
1342 | page = find_get_page(inode->i_mapping, index); | |
1343 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ | |
1344 | account_page_redirty(page); | |
1345 | __set_page_dirty_nobuffers(page); | |
1346 | page_cache_release(page); | |
1347 | index++; | |
1348 | } | |
1349 | return 0; | |
1350 | } | |
1351 | ||
d1310b2e CM |
1352 | /* |
1353 | * helper function to set both pages and extents in the tree writeback | |
1354 | */ | |
b2950863 | 1355 | static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end) |
d1310b2e CM |
1356 | { |
1357 | unsigned long index = start >> PAGE_CACHE_SHIFT; | |
1358 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; | |
1359 | struct page *page; | |
1360 | ||
1361 | while (index <= end_index) { | |
1362 | page = find_get_page(tree->mapping, index); | |
79787eaa | 1363 | BUG_ON(!page); /* Pages should be in the extent_io_tree */ |
d1310b2e CM |
1364 | set_page_writeback(page); |
1365 | page_cache_release(page); | |
1366 | index++; | |
1367 | } | |
d1310b2e CM |
1368 | return 0; |
1369 | } | |
d1310b2e | 1370 | |
d352ac68 CM |
1371 | /* find the first state struct with 'bits' set after 'start', and |
1372 | * return it. tree->lock must be held. NULL will returned if | |
1373 | * nothing was found after 'start' | |
1374 | */ | |
48a3b636 ES |
1375 | static struct extent_state * |
1376 | find_first_extent_bit_state(struct extent_io_tree *tree, | |
41074888 | 1377 | u64 start, unsigned long bits) |
d7fc640e CM |
1378 | { |
1379 | struct rb_node *node; | |
1380 | struct extent_state *state; | |
1381 | ||
1382 | /* | |
1383 | * this search will find all the extents that end after | |
1384 | * our range starts. | |
1385 | */ | |
1386 | node = tree_search(tree, start); | |
d397712b | 1387 | if (!node) |
d7fc640e | 1388 | goto out; |
d7fc640e | 1389 | |
d397712b | 1390 | while (1) { |
d7fc640e | 1391 | state = rb_entry(node, struct extent_state, rb_node); |
d397712b | 1392 | if (state->end >= start && (state->state & bits)) |
d7fc640e | 1393 | return state; |
d397712b | 1394 | |
d7fc640e CM |
1395 | node = rb_next(node); |
1396 | if (!node) | |
1397 | break; | |
1398 | } | |
1399 | out: | |
1400 | return NULL; | |
1401 | } | |
d7fc640e | 1402 | |
69261c4b XG |
1403 | /* |
1404 | * find the first offset in the io tree with 'bits' set. zero is | |
1405 | * returned if we find something, and *start_ret and *end_ret are | |
1406 | * set to reflect the state struct that was found. | |
1407 | * | |
477d7eaf | 1408 | * If nothing was found, 1 is returned. If found something, return 0. |
69261c4b XG |
1409 | */ |
1410 | int find_first_extent_bit(struct extent_io_tree *tree, u64 start, | |
41074888 | 1411 | u64 *start_ret, u64 *end_ret, unsigned long bits, |
e6138876 | 1412 | struct extent_state **cached_state) |
69261c4b XG |
1413 | { |
1414 | struct extent_state *state; | |
e6138876 | 1415 | struct rb_node *n; |
69261c4b XG |
1416 | int ret = 1; |
1417 | ||
1418 | spin_lock(&tree->lock); | |
e6138876 JB |
1419 | if (cached_state && *cached_state) { |
1420 | state = *cached_state; | |
1421 | if (state->end == start - 1 && state->tree) { | |
1422 | n = rb_next(&state->rb_node); | |
1423 | while (n) { | |
1424 | state = rb_entry(n, struct extent_state, | |
1425 | rb_node); | |
1426 | if (state->state & bits) | |
1427 | goto got_it; | |
1428 | n = rb_next(n); | |
1429 | } | |
1430 | free_extent_state(*cached_state); | |
1431 | *cached_state = NULL; | |
1432 | goto out; | |
1433 | } | |
1434 | free_extent_state(*cached_state); | |
1435 | *cached_state = NULL; | |
1436 | } | |
1437 | ||
69261c4b | 1438 | state = find_first_extent_bit_state(tree, start, bits); |
e6138876 | 1439 | got_it: |
69261c4b | 1440 | if (state) { |
e6138876 | 1441 | cache_state(state, cached_state); |
69261c4b XG |
1442 | *start_ret = state->start; |
1443 | *end_ret = state->end; | |
1444 | ret = 0; | |
1445 | } | |
e6138876 | 1446 | out: |
69261c4b XG |
1447 | spin_unlock(&tree->lock); |
1448 | return ret; | |
1449 | } | |
1450 | ||
d352ac68 CM |
1451 | /* |
1452 | * find a contiguous range of bytes in the file marked as delalloc, not | |
1453 | * more than 'max_bytes'. start and end are used to return the range, | |
1454 | * | |
1455 | * 1 is returned if we find something, 0 if nothing was in the tree | |
1456 | */ | |
c8b97818 | 1457 | static noinline u64 find_delalloc_range(struct extent_io_tree *tree, |
c2a128d2 JB |
1458 | u64 *start, u64 *end, u64 max_bytes, |
1459 | struct extent_state **cached_state) | |
d1310b2e CM |
1460 | { |
1461 | struct rb_node *node; | |
1462 | struct extent_state *state; | |
1463 | u64 cur_start = *start; | |
1464 | u64 found = 0; | |
1465 | u64 total_bytes = 0; | |
1466 | ||
cad321ad | 1467 | spin_lock(&tree->lock); |
c8b97818 | 1468 | |
d1310b2e CM |
1469 | /* |
1470 | * this search will find all the extents that end after | |
1471 | * our range starts. | |
1472 | */ | |
80ea96b1 | 1473 | node = tree_search(tree, cur_start); |
2b114d1d | 1474 | if (!node) { |
3b951516 CM |
1475 | if (!found) |
1476 | *end = (u64)-1; | |
d1310b2e CM |
1477 | goto out; |
1478 | } | |
1479 | ||
d397712b | 1480 | while (1) { |
d1310b2e | 1481 | state = rb_entry(node, struct extent_state, rb_node); |
5b21f2ed ZY |
1482 | if (found && (state->start != cur_start || |
1483 | (state->state & EXTENT_BOUNDARY))) { | |
d1310b2e CM |
1484 | goto out; |
1485 | } | |
1486 | if (!(state->state & EXTENT_DELALLOC)) { | |
1487 | if (!found) | |
1488 | *end = state->end; | |
1489 | goto out; | |
1490 | } | |
c2a128d2 | 1491 | if (!found) { |
d1310b2e | 1492 | *start = state->start; |
c2a128d2 JB |
1493 | *cached_state = state; |
1494 | atomic_inc(&state->refs); | |
1495 | } | |
d1310b2e CM |
1496 | found++; |
1497 | *end = state->end; | |
1498 | cur_start = state->end + 1; | |
1499 | node = rb_next(node); | |
1500 | if (!node) | |
1501 | break; | |
1502 | total_bytes += state->end - state->start + 1; | |
1503 | if (total_bytes >= max_bytes) | |
1504 | break; | |
1505 | } | |
1506 | out: | |
cad321ad | 1507 | spin_unlock(&tree->lock); |
d1310b2e CM |
1508 | return found; |
1509 | } | |
1510 | ||
143bede5 JM |
1511 | static noinline void __unlock_for_delalloc(struct inode *inode, |
1512 | struct page *locked_page, | |
1513 | u64 start, u64 end) | |
c8b97818 CM |
1514 | { |
1515 | int ret; | |
1516 | struct page *pages[16]; | |
1517 | unsigned long index = start >> PAGE_CACHE_SHIFT; | |
1518 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; | |
1519 | unsigned long nr_pages = end_index - index + 1; | |
1520 | int i; | |
1521 | ||
1522 | if (index == locked_page->index && end_index == index) | |
143bede5 | 1523 | return; |
c8b97818 | 1524 | |
d397712b | 1525 | while (nr_pages > 0) { |
c8b97818 | 1526 | ret = find_get_pages_contig(inode->i_mapping, index, |
5b050f04 CM |
1527 | min_t(unsigned long, nr_pages, |
1528 | ARRAY_SIZE(pages)), pages); | |
c8b97818 CM |
1529 | for (i = 0; i < ret; i++) { |
1530 | if (pages[i] != locked_page) | |
1531 | unlock_page(pages[i]); | |
1532 | page_cache_release(pages[i]); | |
1533 | } | |
1534 | nr_pages -= ret; | |
1535 | index += ret; | |
1536 | cond_resched(); | |
1537 | } | |
c8b97818 CM |
1538 | } |
1539 | ||
1540 | static noinline int lock_delalloc_pages(struct inode *inode, | |
1541 | struct page *locked_page, | |
1542 | u64 delalloc_start, | |
1543 | u64 delalloc_end) | |
1544 | { | |
1545 | unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT; | |
1546 | unsigned long start_index = index; | |
1547 | unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT; | |
1548 | unsigned long pages_locked = 0; | |
1549 | struct page *pages[16]; | |
1550 | unsigned long nrpages; | |
1551 | int ret; | |
1552 | int i; | |
1553 | ||
1554 | /* the caller is responsible for locking the start index */ | |
1555 | if (index == locked_page->index && index == end_index) | |
1556 | return 0; | |
1557 | ||
1558 | /* skip the page at the start index */ | |
1559 | nrpages = end_index - index + 1; | |
d397712b | 1560 | while (nrpages > 0) { |
c8b97818 | 1561 | ret = find_get_pages_contig(inode->i_mapping, index, |
5b050f04 CM |
1562 | min_t(unsigned long, |
1563 | nrpages, ARRAY_SIZE(pages)), pages); | |
c8b97818 CM |
1564 | if (ret == 0) { |
1565 | ret = -EAGAIN; | |
1566 | goto done; | |
1567 | } | |
1568 | /* now we have an array of pages, lock them all */ | |
1569 | for (i = 0; i < ret; i++) { | |
1570 | /* | |
1571 | * the caller is taking responsibility for | |
1572 | * locked_page | |
1573 | */ | |
771ed689 | 1574 | if (pages[i] != locked_page) { |
c8b97818 | 1575 | lock_page(pages[i]); |
f2b1c41c CM |
1576 | if (!PageDirty(pages[i]) || |
1577 | pages[i]->mapping != inode->i_mapping) { | |
771ed689 CM |
1578 | ret = -EAGAIN; |
1579 | unlock_page(pages[i]); | |
1580 | page_cache_release(pages[i]); | |
1581 | goto done; | |
1582 | } | |
1583 | } | |
c8b97818 | 1584 | page_cache_release(pages[i]); |
771ed689 | 1585 | pages_locked++; |
c8b97818 | 1586 | } |
c8b97818 CM |
1587 | nrpages -= ret; |
1588 | index += ret; | |
1589 | cond_resched(); | |
1590 | } | |
1591 | ret = 0; | |
1592 | done: | |
1593 | if (ret && pages_locked) { | |
1594 | __unlock_for_delalloc(inode, locked_page, | |
1595 | delalloc_start, | |
1596 | ((u64)(start_index + pages_locked - 1)) << | |
1597 | PAGE_CACHE_SHIFT); | |
1598 | } | |
1599 | return ret; | |
1600 | } | |
1601 | ||
1602 | /* | |
1603 | * find a contiguous range of bytes in the file marked as delalloc, not | |
1604 | * more than 'max_bytes'. start and end are used to return the range, | |
1605 | * | |
1606 | * 1 is returned if we find something, 0 if nothing was in the tree | |
1607 | */ | |
1608 | static noinline u64 find_lock_delalloc_range(struct inode *inode, | |
1609 | struct extent_io_tree *tree, | |
1610 | struct page *locked_page, | |
1611 | u64 *start, u64 *end, | |
1612 | u64 max_bytes) | |
1613 | { | |
1614 | u64 delalloc_start; | |
1615 | u64 delalloc_end; | |
1616 | u64 found; | |
9655d298 | 1617 | struct extent_state *cached_state = NULL; |
c8b97818 CM |
1618 | int ret; |
1619 | int loops = 0; | |
1620 | ||
1621 | again: | |
1622 | /* step one, find a bunch of delalloc bytes starting at start */ | |
1623 | delalloc_start = *start; | |
1624 | delalloc_end = 0; | |
1625 | found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, | |
c2a128d2 | 1626 | max_bytes, &cached_state); |
70b99e69 | 1627 | if (!found || delalloc_end <= *start) { |
c8b97818 CM |
1628 | *start = delalloc_start; |
1629 | *end = delalloc_end; | |
c2a128d2 | 1630 | free_extent_state(cached_state); |
c8b97818 CM |
1631 | return found; |
1632 | } | |
1633 | ||
70b99e69 CM |
1634 | /* |
1635 | * start comes from the offset of locked_page. We have to lock | |
1636 | * pages in order, so we can't process delalloc bytes before | |
1637 | * locked_page | |
1638 | */ | |
d397712b | 1639 | if (delalloc_start < *start) |
70b99e69 | 1640 | delalloc_start = *start; |
70b99e69 | 1641 | |
c8b97818 CM |
1642 | /* |
1643 | * make sure to limit the number of pages we try to lock down | |
1644 | * if we're looping. | |
1645 | */ | |
d397712b | 1646 | if (delalloc_end + 1 - delalloc_start > max_bytes && loops) |
771ed689 | 1647 | delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1; |
d397712b | 1648 | |
c8b97818 CM |
1649 | /* step two, lock all the pages after the page that has start */ |
1650 | ret = lock_delalloc_pages(inode, locked_page, | |
1651 | delalloc_start, delalloc_end); | |
1652 | if (ret == -EAGAIN) { | |
1653 | /* some of the pages are gone, lets avoid looping by | |
1654 | * shortening the size of the delalloc range we're searching | |
1655 | */ | |
9655d298 | 1656 | free_extent_state(cached_state); |
c8b97818 CM |
1657 | if (!loops) { |
1658 | unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1); | |
1659 | max_bytes = PAGE_CACHE_SIZE - offset; | |
1660 | loops = 1; | |
1661 | goto again; | |
1662 | } else { | |
1663 | found = 0; | |
1664 | goto out_failed; | |
1665 | } | |
1666 | } | |
79787eaa | 1667 | BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */ |
c8b97818 CM |
1668 | |
1669 | /* step three, lock the state bits for the whole range */ | |
d0082371 | 1670 | lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state); |
c8b97818 CM |
1671 | |
1672 | /* then test to make sure it is all still delalloc */ | |
1673 | ret = test_range_bit(tree, delalloc_start, delalloc_end, | |
9655d298 | 1674 | EXTENT_DELALLOC, 1, cached_state); |
c8b97818 | 1675 | if (!ret) { |
9655d298 CM |
1676 | unlock_extent_cached(tree, delalloc_start, delalloc_end, |
1677 | &cached_state, GFP_NOFS); | |
c8b97818 CM |
1678 | __unlock_for_delalloc(inode, locked_page, |
1679 | delalloc_start, delalloc_end); | |
1680 | cond_resched(); | |
1681 | goto again; | |
1682 | } | |
9655d298 | 1683 | free_extent_state(cached_state); |
c8b97818 CM |
1684 | *start = delalloc_start; |
1685 | *end = delalloc_end; | |
1686 | out_failed: | |
1687 | return found; | |
1688 | } | |
1689 | ||
1690 | int extent_clear_unlock_delalloc(struct inode *inode, | |
1691 | struct extent_io_tree *tree, | |
1692 | u64 start, u64 end, struct page *locked_page, | |
a791e35e | 1693 | unsigned long op) |
c8b97818 CM |
1694 | { |
1695 | int ret; | |
1696 | struct page *pages[16]; | |
1697 | unsigned long index = start >> PAGE_CACHE_SHIFT; | |
1698 | unsigned long end_index = end >> PAGE_CACHE_SHIFT; | |
1699 | unsigned long nr_pages = end_index - index + 1; | |
1700 | int i; | |
41074888 | 1701 | unsigned long clear_bits = 0; |
c8b97818 | 1702 | |
a791e35e | 1703 | if (op & EXTENT_CLEAR_UNLOCK) |
771ed689 | 1704 | clear_bits |= EXTENT_LOCKED; |
a791e35e | 1705 | if (op & EXTENT_CLEAR_DIRTY) |
c8b97818 CM |
1706 | clear_bits |= EXTENT_DIRTY; |
1707 | ||
a791e35e | 1708 | if (op & EXTENT_CLEAR_DELALLOC) |
771ed689 CM |
1709 | clear_bits |= EXTENT_DELALLOC; |
1710 | ||
2c64c53d | 1711 | clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS); |
32c00aff JB |
1712 | if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY | |
1713 | EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK | | |
1714 | EXTENT_SET_PRIVATE2))) | |
771ed689 | 1715 | return 0; |
c8b97818 | 1716 | |
d397712b | 1717 | while (nr_pages > 0) { |
c8b97818 | 1718 | ret = find_get_pages_contig(inode->i_mapping, index, |
5b050f04 CM |
1719 | min_t(unsigned long, |
1720 | nr_pages, ARRAY_SIZE(pages)), pages); | |
c8b97818 | 1721 | for (i = 0; i < ret; i++) { |
8b62b72b | 1722 | |
a791e35e | 1723 | if (op & EXTENT_SET_PRIVATE2) |
8b62b72b CM |
1724 | SetPagePrivate2(pages[i]); |
1725 | ||
c8b97818 CM |
1726 | if (pages[i] == locked_page) { |
1727 | page_cache_release(pages[i]); | |
1728 | continue; | |
1729 | } | |
a791e35e | 1730 | if (op & EXTENT_CLEAR_DIRTY) |
c8b97818 | 1731 | clear_page_dirty_for_io(pages[i]); |
a791e35e | 1732 | if (op & EXTENT_SET_WRITEBACK) |
c8b97818 | 1733 | set_page_writeback(pages[i]); |
a791e35e | 1734 | if (op & EXTENT_END_WRITEBACK) |
c8b97818 | 1735 | end_page_writeback(pages[i]); |
a791e35e | 1736 | if (op & EXTENT_CLEAR_UNLOCK_PAGE) |
771ed689 | 1737 | unlock_page(pages[i]); |
c8b97818 CM |
1738 | page_cache_release(pages[i]); |
1739 | } | |
1740 | nr_pages -= ret; | |
1741 | index += ret; | |
1742 | cond_resched(); | |
1743 | } | |
1744 | return 0; | |
1745 | } | |
c8b97818 | 1746 | |
d352ac68 CM |
1747 | /* |
1748 | * count the number of bytes in the tree that have a given bit(s) | |
1749 | * set. This can be fairly slow, except for EXTENT_DIRTY which is | |
1750 | * cached. The total number found is returned. | |
1751 | */ | |
d1310b2e CM |
1752 | u64 count_range_bits(struct extent_io_tree *tree, |
1753 | u64 *start, u64 search_end, u64 max_bytes, | |
ec29ed5b | 1754 | unsigned long bits, int contig) |
d1310b2e CM |
1755 | { |
1756 | struct rb_node *node; | |
1757 | struct extent_state *state; | |
1758 | u64 cur_start = *start; | |
1759 | u64 total_bytes = 0; | |
ec29ed5b | 1760 | u64 last = 0; |
d1310b2e CM |
1761 | int found = 0; |
1762 | ||
1763 | if (search_end <= cur_start) { | |
d1310b2e CM |
1764 | WARN_ON(1); |
1765 | return 0; | |
1766 | } | |
1767 | ||
cad321ad | 1768 | spin_lock(&tree->lock); |
d1310b2e CM |
1769 | if (cur_start == 0 && bits == EXTENT_DIRTY) { |
1770 | total_bytes = tree->dirty_bytes; | |
1771 | goto out; | |
1772 | } | |
1773 | /* | |
1774 | * this search will find all the extents that end after | |
1775 | * our range starts. | |
1776 | */ | |
80ea96b1 | 1777 | node = tree_search(tree, cur_start); |
d397712b | 1778 | if (!node) |
d1310b2e | 1779 | goto out; |
d1310b2e | 1780 | |
d397712b | 1781 | while (1) { |
d1310b2e CM |
1782 | state = rb_entry(node, struct extent_state, rb_node); |
1783 | if (state->start > search_end) | |
1784 | break; | |
ec29ed5b CM |
1785 | if (contig && found && state->start > last + 1) |
1786 | break; | |
1787 | if (state->end >= cur_start && (state->state & bits) == bits) { | |
d1310b2e CM |
1788 | total_bytes += min(search_end, state->end) + 1 - |
1789 | max(cur_start, state->start); | |
1790 | if (total_bytes >= max_bytes) | |
1791 | break; | |
1792 | if (!found) { | |
af60bed2 | 1793 | *start = max(cur_start, state->start); |
d1310b2e CM |
1794 | found = 1; |
1795 | } | |
ec29ed5b CM |
1796 | last = state->end; |
1797 | } else if (contig && found) { | |
1798 | break; | |
d1310b2e CM |
1799 | } |
1800 | node = rb_next(node); | |
1801 | if (!node) | |
1802 | break; | |
1803 | } | |
1804 | out: | |
cad321ad | 1805 | spin_unlock(&tree->lock); |
d1310b2e CM |
1806 | return total_bytes; |
1807 | } | |
b2950863 | 1808 | |
d352ac68 CM |
1809 | /* |
1810 | * set the private field for a given byte offset in the tree. If there isn't | |
1811 | * an extent_state there already, this does nothing. | |
1812 | */ | |
d1310b2e CM |
1813 | int set_state_private(struct extent_io_tree *tree, u64 start, u64 private) |
1814 | { | |
1815 | struct rb_node *node; | |
1816 | struct extent_state *state; | |
1817 | int ret = 0; | |
1818 | ||
cad321ad | 1819 | spin_lock(&tree->lock); |
d1310b2e CM |
1820 | /* |
1821 | * this search will find all the extents that end after | |
1822 | * our range starts. | |
1823 | */ | |
80ea96b1 | 1824 | node = tree_search(tree, start); |
2b114d1d | 1825 | if (!node) { |
d1310b2e CM |
1826 | ret = -ENOENT; |
1827 | goto out; | |
1828 | } | |
1829 | state = rb_entry(node, struct extent_state, rb_node); | |
1830 | if (state->start != start) { | |
1831 | ret = -ENOENT; | |
1832 | goto out; | |
1833 | } | |
1834 | state->private = private; | |
1835 | out: | |
cad321ad | 1836 | spin_unlock(&tree->lock); |
d1310b2e CM |
1837 | return ret; |
1838 | } | |
1839 | ||
e4100d98 MX |
1840 | void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[], |
1841 | int count) | |
1842 | { | |
1843 | struct rb_node *node; | |
1844 | struct extent_state *state; | |
1845 | ||
1846 | spin_lock(&tree->lock); | |
1847 | /* | |
1848 | * this search will find all the extents that end after | |
1849 | * our range starts. | |
1850 | */ | |
1851 | node = tree_search(tree, start); | |
1852 | BUG_ON(!node); | |
1853 | ||
1854 | state = rb_entry(node, struct extent_state, rb_node); | |
1855 | BUG_ON(state->start != start); | |
1856 | ||
1857 | while (count) { | |
1858 | state->private = *csums++; | |
1859 | count--; | |
1860 | state = next_state(state); | |
1861 | } | |
1862 | spin_unlock(&tree->lock); | |
1863 | } | |
1864 | ||
1865 | static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index) | |
1866 | { | |
1867 | struct bio_vec *bvec = bio->bi_io_vec + bio_index; | |
1868 | ||
1869 | return page_offset(bvec->bv_page) + bvec->bv_offset; | |
1870 | } | |
1871 | ||
1872 | void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index, | |
1873 | u32 csums[], int count) | |
1874 | { | |
1875 | struct rb_node *node; | |
1876 | struct extent_state *state = NULL; | |
1877 | u64 start; | |
1878 | ||
1879 | spin_lock(&tree->lock); | |
1880 | do { | |
1881 | start = __btrfs_get_bio_offset(bio, bio_index); | |
1882 | if (state == NULL || state->start != start) { | |
1883 | node = tree_search(tree, start); | |
1884 | BUG_ON(!node); | |
1885 | ||
1886 | state = rb_entry(node, struct extent_state, rb_node); | |
1887 | BUG_ON(state->start != start); | |
1888 | } | |
1889 | state->private = *csums++; | |
1890 | count--; | |
1891 | bio_index++; | |
1892 | ||
1893 | state = next_state(state); | |
1894 | } while (count); | |
1895 | spin_unlock(&tree->lock); | |
1896 | } | |
1897 | ||
d1310b2e CM |
1898 | int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private) |
1899 | { | |
1900 | struct rb_node *node; | |
1901 | struct extent_state *state; | |
1902 | int ret = 0; | |
1903 | ||
cad321ad | 1904 | spin_lock(&tree->lock); |
d1310b2e CM |
1905 | /* |
1906 | * this search will find all the extents that end after | |
1907 | * our range starts. | |
1908 | */ | |
80ea96b1 | 1909 | node = tree_search(tree, start); |
2b114d1d | 1910 | if (!node) { |
d1310b2e CM |
1911 | ret = -ENOENT; |
1912 | goto out; | |
1913 | } | |
1914 | state = rb_entry(node, struct extent_state, rb_node); | |
1915 | if (state->start != start) { | |
1916 | ret = -ENOENT; | |
1917 | goto out; | |
1918 | } | |
1919 | *private = state->private; | |
1920 | out: | |
cad321ad | 1921 | spin_unlock(&tree->lock); |
d1310b2e CM |
1922 | return ret; |
1923 | } | |
1924 | ||
1925 | /* | |
1926 | * searches a range in the state tree for a given mask. | |
70dec807 | 1927 | * If 'filled' == 1, this returns 1 only if every extent in the tree |
d1310b2e CM |
1928 | * has the bits set. Otherwise, 1 is returned if any bit in the |
1929 | * range is found set. | |
1930 | */ | |
1931 | int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, | |
41074888 | 1932 | unsigned long bits, int filled, struct extent_state *cached) |
d1310b2e CM |
1933 | { |
1934 | struct extent_state *state = NULL; | |
1935 | struct rb_node *node; | |
1936 | int bitset = 0; | |
d1310b2e | 1937 | |
cad321ad | 1938 | spin_lock(&tree->lock); |
df98b6e2 JB |
1939 | if (cached && cached->tree && cached->start <= start && |
1940 | cached->end > start) | |
9655d298 CM |
1941 | node = &cached->rb_node; |
1942 | else | |
1943 | node = tree_search(tree, start); | |
d1310b2e CM |
1944 | while (node && start <= end) { |
1945 | state = rb_entry(node, struct extent_state, rb_node); | |
1946 | ||
1947 | if (filled && state->start > start) { | |
1948 | bitset = 0; | |
1949 | break; | |
1950 | } | |
1951 | ||
1952 | if (state->start > end) | |
1953 | break; | |
1954 | ||
1955 | if (state->state & bits) { | |
1956 | bitset = 1; | |
1957 | if (!filled) | |
1958 | break; | |
1959 | } else if (filled) { | |
1960 | bitset = 0; | |
1961 | break; | |
1962 | } | |
46562cec CM |
1963 | |
1964 | if (state->end == (u64)-1) | |
1965 | break; | |
1966 | ||
d1310b2e CM |
1967 | start = state->end + 1; |
1968 | if (start > end) | |
1969 | break; | |
1970 | node = rb_next(node); | |
1971 | if (!node) { | |
1972 | if (filled) | |
1973 | bitset = 0; | |
1974 | break; | |
1975 | } | |
1976 | } | |
cad321ad | 1977 | spin_unlock(&tree->lock); |
d1310b2e CM |
1978 | return bitset; |
1979 | } | |
d1310b2e CM |
1980 | |
1981 | /* | |
1982 | * helper function to set a given page up to date if all the | |
1983 | * extents in the tree for that page are up to date | |
1984 | */ | |
143bede5 | 1985 | static void check_page_uptodate(struct extent_io_tree *tree, struct page *page) |
d1310b2e | 1986 | { |
4eee4fa4 | 1987 | u64 start = page_offset(page); |
d1310b2e | 1988 | u64 end = start + PAGE_CACHE_SIZE - 1; |
9655d298 | 1989 | if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL)) |
d1310b2e | 1990 | SetPageUptodate(page); |
d1310b2e CM |
1991 | } |
1992 | ||
4a54c8c1 JS |
1993 | /* |
1994 | * When IO fails, either with EIO or csum verification fails, we | |
1995 | * try other mirrors that might have a good copy of the data. This | |
1996 | * io_failure_record is used to record state as we go through all the | |
1997 | * mirrors. If another mirror has good data, the page is set up to date | |
1998 | * and things continue. If a good mirror can't be found, the original | |
1999 | * bio end_io callback is called to indicate things have failed. | |
2000 | */ | |
2001 | struct io_failure_record { | |
2002 | struct page *page; | |
2003 | u64 start; | |
2004 | u64 len; | |
2005 | u64 logical; | |
2006 | unsigned long bio_flags; | |
2007 | int this_mirror; | |
2008 | int failed_mirror; | |
2009 | int in_validation; | |
2010 | }; | |
2011 | ||
2012 | static int free_io_failure(struct inode *inode, struct io_failure_record *rec, | |
2013 | int did_repair) | |
2014 | { | |
2015 | int ret; | |
2016 | int err = 0; | |
2017 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
2018 | ||
2019 | set_state_private(failure_tree, rec->start, 0); | |
2020 | ret = clear_extent_bits(failure_tree, rec->start, | |
2021 | rec->start + rec->len - 1, | |
2022 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); | |
2023 | if (ret) | |
2024 | err = ret; | |
2025 | ||
53b381b3 DW |
2026 | ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start, |
2027 | rec->start + rec->len - 1, | |
2028 | EXTENT_DAMAGED, GFP_NOFS); | |
2029 | if (ret && !err) | |
2030 | err = ret; | |
4a54c8c1 JS |
2031 | |
2032 | kfree(rec); | |
2033 | return err; | |
2034 | } | |
2035 | ||
2036 | static void repair_io_failure_callback(struct bio *bio, int err) | |
2037 | { | |
2038 | complete(bio->bi_private); | |
2039 | } | |
2040 | ||
2041 | /* | |
2042 | * this bypasses the standard btrfs submit functions deliberately, as | |
2043 | * the standard behavior is to write all copies in a raid setup. here we only | |
2044 | * want to write the one bad copy. so we do the mapping for ourselves and issue | |
2045 | * submit_bio directly. | |
3ec706c8 | 2046 | * to avoid any synchronization issues, wait for the data after writing, which |
4a54c8c1 JS |
2047 | * actually prevents the read that triggered the error from finishing. |
2048 | * currently, there can be no more than two copies of every data bit. thus, | |
2049 | * exactly one rewrite is required. | |
2050 | */ | |
3ec706c8 | 2051 | int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start, |
4a54c8c1 JS |
2052 | u64 length, u64 logical, struct page *page, |
2053 | int mirror_num) | |
2054 | { | |
2055 | struct bio *bio; | |
2056 | struct btrfs_device *dev; | |
2057 | DECLARE_COMPLETION_ONSTACK(compl); | |
2058 | u64 map_length = 0; | |
2059 | u64 sector; | |
2060 | struct btrfs_bio *bbio = NULL; | |
53b381b3 | 2061 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
4a54c8c1 JS |
2062 | int ret; |
2063 | ||
2064 | BUG_ON(!mirror_num); | |
2065 | ||
53b381b3 DW |
2066 | /* we can't repair anything in raid56 yet */ |
2067 | if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num)) | |
2068 | return 0; | |
2069 | ||
9be3395b | 2070 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
4a54c8c1 JS |
2071 | if (!bio) |
2072 | return -EIO; | |
2073 | bio->bi_private = &compl; | |
2074 | bio->bi_end_io = repair_io_failure_callback; | |
2075 | bio->bi_size = 0; | |
2076 | map_length = length; | |
2077 | ||
3ec706c8 | 2078 | ret = btrfs_map_block(fs_info, WRITE, logical, |
4a54c8c1 JS |
2079 | &map_length, &bbio, mirror_num); |
2080 | if (ret) { | |
2081 | bio_put(bio); | |
2082 | return -EIO; | |
2083 | } | |
2084 | BUG_ON(mirror_num != bbio->mirror_num); | |
2085 | sector = bbio->stripes[mirror_num-1].physical >> 9; | |
2086 | bio->bi_sector = sector; | |
2087 | dev = bbio->stripes[mirror_num-1].dev; | |
2088 | kfree(bbio); | |
2089 | if (!dev || !dev->bdev || !dev->writeable) { | |
2090 | bio_put(bio); | |
2091 | return -EIO; | |
2092 | } | |
2093 | bio->bi_bdev = dev->bdev; | |
4eee4fa4 | 2094 | bio_add_page(bio, page, length, start - page_offset(page)); |
21adbd5c | 2095 | btrfsic_submit_bio(WRITE_SYNC, bio); |
4a54c8c1 JS |
2096 | wait_for_completion(&compl); |
2097 | ||
2098 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) { | |
2099 | /* try to remap that extent elsewhere? */ | |
2100 | bio_put(bio); | |
442a4f63 | 2101 | btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS); |
4a54c8c1 JS |
2102 | return -EIO; |
2103 | } | |
2104 | ||
d5b025d5 | 2105 | printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu " |
606686ee JB |
2106 | "(dev %s sector %llu)\n", page->mapping->host->i_ino, |
2107 | start, rcu_str_deref(dev->name), sector); | |
4a54c8c1 JS |
2108 | |
2109 | bio_put(bio); | |
2110 | return 0; | |
2111 | } | |
2112 | ||
ea466794 JB |
2113 | int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, |
2114 | int mirror_num) | |
2115 | { | |
ea466794 JB |
2116 | u64 start = eb->start; |
2117 | unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); | |
d95603b2 | 2118 | int ret = 0; |
ea466794 JB |
2119 | |
2120 | for (i = 0; i < num_pages; i++) { | |
2121 | struct page *p = extent_buffer_page(eb, i); | |
3ec706c8 | 2122 | ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE, |
ea466794 JB |
2123 | start, p, mirror_num); |
2124 | if (ret) | |
2125 | break; | |
2126 | start += PAGE_CACHE_SIZE; | |
2127 | } | |
2128 | ||
2129 | return ret; | |
2130 | } | |
2131 | ||
4a54c8c1 JS |
2132 | /* |
2133 | * each time an IO finishes, we do a fast check in the IO failure tree | |
2134 | * to see if we need to process or clean up an io_failure_record | |
2135 | */ | |
2136 | static int clean_io_failure(u64 start, struct page *page) | |
2137 | { | |
2138 | u64 private; | |
2139 | u64 private_failure; | |
2140 | struct io_failure_record *failrec; | |
3ec706c8 | 2141 | struct btrfs_fs_info *fs_info; |
4a54c8c1 JS |
2142 | struct extent_state *state; |
2143 | int num_copies; | |
2144 | int did_repair = 0; | |
2145 | int ret; | |
2146 | struct inode *inode = page->mapping->host; | |
2147 | ||
2148 | private = 0; | |
2149 | ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, | |
2150 | (u64)-1, 1, EXTENT_DIRTY, 0); | |
2151 | if (!ret) | |
2152 | return 0; | |
2153 | ||
2154 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start, | |
2155 | &private_failure); | |
2156 | if (ret) | |
2157 | return 0; | |
2158 | ||
2159 | failrec = (struct io_failure_record *)(unsigned long) private_failure; | |
2160 | BUG_ON(!failrec->this_mirror); | |
2161 | ||
2162 | if (failrec->in_validation) { | |
2163 | /* there was no real error, just free the record */ | |
2164 | pr_debug("clean_io_failure: freeing dummy error at %llu\n", | |
2165 | failrec->start); | |
2166 | did_repair = 1; | |
2167 | goto out; | |
2168 | } | |
2169 | ||
2170 | spin_lock(&BTRFS_I(inode)->io_tree.lock); | |
2171 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, | |
2172 | failrec->start, | |
2173 | EXTENT_LOCKED); | |
2174 | spin_unlock(&BTRFS_I(inode)->io_tree.lock); | |
2175 | ||
2176 | if (state && state->start == failrec->start) { | |
3ec706c8 SB |
2177 | fs_info = BTRFS_I(inode)->root->fs_info; |
2178 | num_copies = btrfs_num_copies(fs_info, failrec->logical, | |
2179 | failrec->len); | |
4a54c8c1 | 2180 | if (num_copies > 1) { |
3ec706c8 | 2181 | ret = repair_io_failure(fs_info, start, failrec->len, |
4a54c8c1 JS |
2182 | failrec->logical, page, |
2183 | failrec->failed_mirror); | |
2184 | did_repair = !ret; | |
2185 | } | |
53b381b3 | 2186 | ret = 0; |
4a54c8c1 JS |
2187 | } |
2188 | ||
2189 | out: | |
2190 | if (!ret) | |
2191 | ret = free_io_failure(inode, failrec, did_repair); | |
2192 | ||
2193 | return ret; | |
2194 | } | |
2195 | ||
2196 | /* | |
2197 | * this is a generic handler for readpage errors (default | |
2198 | * readpage_io_failed_hook). if other copies exist, read those and write back | |
2199 | * good data to the failed position. does not investigate in remapping the | |
2200 | * failed extent elsewhere, hoping the device will be smart enough to do this as | |
2201 | * needed | |
2202 | */ | |
2203 | ||
2204 | static int bio_readpage_error(struct bio *failed_bio, struct page *page, | |
2205 | u64 start, u64 end, int failed_mirror, | |
2206 | struct extent_state *state) | |
2207 | { | |
2208 | struct io_failure_record *failrec = NULL; | |
2209 | u64 private; | |
2210 | struct extent_map *em; | |
2211 | struct inode *inode = page->mapping->host; | |
2212 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
2213 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
2214 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
2215 | struct bio *bio; | |
2216 | int num_copies; | |
2217 | int ret; | |
2218 | int read_mode; | |
2219 | u64 logical; | |
2220 | ||
2221 | BUG_ON(failed_bio->bi_rw & REQ_WRITE); | |
2222 | ||
2223 | ret = get_state_private(failure_tree, start, &private); | |
2224 | if (ret) { | |
2225 | failrec = kzalloc(sizeof(*failrec), GFP_NOFS); | |
2226 | if (!failrec) | |
2227 | return -ENOMEM; | |
2228 | failrec->start = start; | |
2229 | failrec->len = end - start + 1; | |
2230 | failrec->this_mirror = 0; | |
2231 | failrec->bio_flags = 0; | |
2232 | failrec->in_validation = 0; | |
2233 | ||
2234 | read_lock(&em_tree->lock); | |
2235 | em = lookup_extent_mapping(em_tree, start, failrec->len); | |
2236 | if (!em) { | |
2237 | read_unlock(&em_tree->lock); | |
2238 | kfree(failrec); | |
2239 | return -EIO; | |
2240 | } | |
2241 | ||
2242 | if (em->start > start || em->start + em->len < start) { | |
2243 | free_extent_map(em); | |
2244 | em = NULL; | |
2245 | } | |
2246 | read_unlock(&em_tree->lock); | |
2247 | ||
7a2d6a64 | 2248 | if (!em) { |
4a54c8c1 JS |
2249 | kfree(failrec); |
2250 | return -EIO; | |
2251 | } | |
2252 | logical = start - em->start; | |
2253 | logical = em->block_start + logical; | |
2254 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { | |
2255 | logical = em->block_start; | |
2256 | failrec->bio_flags = EXTENT_BIO_COMPRESSED; | |
2257 | extent_set_compress_type(&failrec->bio_flags, | |
2258 | em->compress_type); | |
2259 | } | |
2260 | pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, " | |
2261 | "len=%llu\n", logical, start, failrec->len); | |
2262 | failrec->logical = logical; | |
2263 | free_extent_map(em); | |
2264 | ||
2265 | /* set the bits in the private failure tree */ | |
2266 | ret = set_extent_bits(failure_tree, start, end, | |
2267 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); | |
2268 | if (ret >= 0) | |
2269 | ret = set_state_private(failure_tree, start, | |
2270 | (u64)(unsigned long)failrec); | |
2271 | /* set the bits in the inode's tree */ | |
2272 | if (ret >= 0) | |
2273 | ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED, | |
2274 | GFP_NOFS); | |
2275 | if (ret < 0) { | |
2276 | kfree(failrec); | |
2277 | return ret; | |
2278 | } | |
2279 | } else { | |
2280 | failrec = (struct io_failure_record *)(unsigned long)private; | |
2281 | pr_debug("bio_readpage_error: (found) logical=%llu, " | |
2282 | "start=%llu, len=%llu, validation=%d\n", | |
2283 | failrec->logical, failrec->start, failrec->len, | |
2284 | failrec->in_validation); | |
2285 | /* | |
2286 | * when data can be on disk more than twice, add to failrec here | |
2287 | * (e.g. with a list for failed_mirror) to make | |
2288 | * clean_io_failure() clean all those errors at once. | |
2289 | */ | |
2290 | } | |
5d964051 SB |
2291 | num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info, |
2292 | failrec->logical, failrec->len); | |
4a54c8c1 JS |
2293 | if (num_copies == 1) { |
2294 | /* | |
2295 | * we only have a single copy of the data, so don't bother with | |
2296 | * all the retry and error correction code that follows. no | |
2297 | * matter what the error is, it is very likely to persist. | |
2298 | */ | |
2299 | pr_debug("bio_readpage_error: cannot repair, num_copies == 1. " | |
2300 | "state=%p, num_copies=%d, next_mirror %d, " | |
2301 | "failed_mirror %d\n", state, num_copies, | |
2302 | failrec->this_mirror, failed_mirror); | |
2303 | free_io_failure(inode, failrec, 0); | |
2304 | return -EIO; | |
2305 | } | |
2306 | ||
2307 | if (!state) { | |
2308 | spin_lock(&tree->lock); | |
2309 | state = find_first_extent_bit_state(tree, failrec->start, | |
2310 | EXTENT_LOCKED); | |
2311 | if (state && state->start != failrec->start) | |
2312 | state = NULL; | |
2313 | spin_unlock(&tree->lock); | |
2314 | } | |
2315 | ||
2316 | /* | |
2317 | * there are two premises: | |
2318 | * a) deliver good data to the caller | |
2319 | * b) correct the bad sectors on disk | |
2320 | */ | |
2321 | if (failed_bio->bi_vcnt > 1) { | |
2322 | /* | |
2323 | * to fulfill b), we need to know the exact failing sectors, as | |
2324 | * we don't want to rewrite any more than the failed ones. thus, | |
2325 | * we need separate read requests for the failed bio | |
2326 | * | |
2327 | * if the following BUG_ON triggers, our validation request got | |
2328 | * merged. we need separate requests for our algorithm to work. | |
2329 | */ | |
2330 | BUG_ON(failrec->in_validation); | |
2331 | failrec->in_validation = 1; | |
2332 | failrec->this_mirror = failed_mirror; | |
2333 | read_mode = READ_SYNC | REQ_FAILFAST_DEV; | |
2334 | } else { | |
2335 | /* | |
2336 | * we're ready to fulfill a) and b) alongside. get a good copy | |
2337 | * of the failed sector and if we succeed, we have setup | |
2338 | * everything for repair_io_failure to do the rest for us. | |
2339 | */ | |
2340 | if (failrec->in_validation) { | |
2341 | BUG_ON(failrec->this_mirror != failed_mirror); | |
2342 | failrec->in_validation = 0; | |
2343 | failrec->this_mirror = 0; | |
2344 | } | |
2345 | failrec->failed_mirror = failed_mirror; | |
2346 | failrec->this_mirror++; | |
2347 | if (failrec->this_mirror == failed_mirror) | |
2348 | failrec->this_mirror++; | |
2349 | read_mode = READ_SYNC; | |
2350 | } | |
2351 | ||
2352 | if (!state || failrec->this_mirror > num_copies) { | |
2353 | pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, " | |
2354 | "next_mirror %d, failed_mirror %d\n", state, | |
2355 | num_copies, failrec->this_mirror, failed_mirror); | |
2356 | free_io_failure(inode, failrec, 0); | |
2357 | return -EIO; | |
2358 | } | |
2359 | ||
9be3395b | 2360 | bio = btrfs_io_bio_alloc(GFP_NOFS, 1); |
e627ee7b TI |
2361 | if (!bio) { |
2362 | free_io_failure(inode, failrec, 0); | |
2363 | return -EIO; | |
2364 | } | |
4a54c8c1 JS |
2365 | bio->bi_private = state; |
2366 | bio->bi_end_io = failed_bio->bi_end_io; | |
2367 | bio->bi_sector = failrec->logical >> 9; | |
2368 | bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; | |
2369 | bio->bi_size = 0; | |
2370 | ||
2371 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); | |
2372 | ||
2373 | pr_debug("bio_readpage_error: submitting new read[%#x] to " | |
2374 | "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode, | |
2375 | failrec->this_mirror, num_copies, failrec->in_validation); | |
2376 | ||
013bd4c3 TI |
2377 | ret = tree->ops->submit_bio_hook(inode, read_mode, bio, |
2378 | failrec->this_mirror, | |
2379 | failrec->bio_flags, 0); | |
2380 | return ret; | |
4a54c8c1 JS |
2381 | } |
2382 | ||
d1310b2e CM |
2383 | /* lots and lots of room for performance fixes in the end_bio funcs */ |
2384 | ||
87826df0 JM |
2385 | int end_extent_writepage(struct page *page, int err, u64 start, u64 end) |
2386 | { | |
2387 | int uptodate = (err == 0); | |
2388 | struct extent_io_tree *tree; | |
2389 | int ret; | |
2390 | ||
2391 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
2392 | ||
2393 | if (tree->ops && tree->ops->writepage_end_io_hook) { | |
2394 | ret = tree->ops->writepage_end_io_hook(page, start, | |
2395 | end, NULL, uptodate); | |
2396 | if (ret) | |
2397 | uptodate = 0; | |
2398 | } | |
2399 | ||
87826df0 | 2400 | if (!uptodate) { |
87826df0 JM |
2401 | ClearPageUptodate(page); |
2402 | SetPageError(page); | |
2403 | } | |
2404 | return 0; | |
2405 | } | |
2406 | ||
d1310b2e CM |
2407 | /* |
2408 | * after a writepage IO is done, we need to: | |
2409 | * clear the uptodate bits on error | |
2410 | * clear the writeback bits in the extent tree for this IO | |
2411 | * end_page_writeback if the page has no more pending IO | |
2412 | * | |
2413 | * Scheduling is not allowed, so the extent state tree is expected | |
2414 | * to have one and only one object corresponding to this IO. | |
2415 | */ | |
d1310b2e | 2416 | static void end_bio_extent_writepage(struct bio *bio, int err) |
d1310b2e | 2417 | { |
d1310b2e | 2418 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
902b22f3 | 2419 | struct extent_io_tree *tree; |
d1310b2e CM |
2420 | u64 start; |
2421 | u64 end; | |
d1310b2e | 2422 | |
d1310b2e CM |
2423 | do { |
2424 | struct page *page = bvec->bv_page; | |
902b22f3 DW |
2425 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
2426 | ||
17a5adcc AO |
2427 | /* We always issue full-page reads, but if some block |
2428 | * in a page fails to read, blk_update_request() will | |
2429 | * advance bv_offset and adjust bv_len to compensate. | |
2430 | * Print a warning for nonzero offsets, and an error | |
2431 | * if they don't add up to a full page. */ | |
2432 | if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) | |
2433 | printk("%s page write in btrfs with offset %u and length %u\n", | |
2434 | bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE | |
2435 | ? KERN_ERR "partial" : KERN_INFO "incomplete", | |
2436 | bvec->bv_offset, bvec->bv_len); | |
d1310b2e | 2437 | |
17a5adcc AO |
2438 | start = page_offset(page); |
2439 | end = start + bvec->bv_offset + bvec->bv_len - 1; | |
d1310b2e CM |
2440 | |
2441 | if (--bvec >= bio->bi_io_vec) | |
2442 | prefetchw(&bvec->bv_page->flags); | |
1259ab75 | 2443 | |
87826df0 JM |
2444 | if (end_extent_writepage(page, err, start, end)) |
2445 | continue; | |
70dec807 | 2446 | |
17a5adcc | 2447 | end_page_writeback(page); |
d1310b2e | 2448 | } while (bvec >= bio->bi_io_vec); |
2b1f55b0 | 2449 | |
d1310b2e | 2450 | bio_put(bio); |
d1310b2e CM |
2451 | } |
2452 | ||
2453 | /* | |
2454 | * after a readpage IO is done, we need to: | |
2455 | * clear the uptodate bits on error | |
2456 | * set the uptodate bits if things worked | |
2457 | * set the page up to date if all extents in the tree are uptodate | |
2458 | * clear the lock bit in the extent tree | |
2459 | * unlock the page if there are no other extents locked for it | |
2460 | * | |
2461 | * Scheduling is not allowed, so the extent state tree is expected | |
2462 | * to have one and only one object corresponding to this IO. | |
2463 | */ | |
d1310b2e | 2464 | static void end_bio_extent_readpage(struct bio *bio, int err) |
d1310b2e CM |
2465 | { |
2466 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); | |
4125bf76 CM |
2467 | struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; |
2468 | struct bio_vec *bvec = bio->bi_io_vec; | |
902b22f3 | 2469 | struct extent_io_tree *tree; |
d1310b2e CM |
2470 | u64 start; |
2471 | u64 end; | |
5cf1ab56 | 2472 | int mirror; |
d1310b2e CM |
2473 | int ret; |
2474 | ||
d20f7043 CM |
2475 | if (err) |
2476 | uptodate = 0; | |
2477 | ||
d1310b2e CM |
2478 | do { |
2479 | struct page *page = bvec->bv_page; | |
507903b8 AJ |
2480 | struct extent_state *cached = NULL; |
2481 | struct extent_state *state; | |
9be3395b | 2482 | struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); |
a71754fc | 2483 | struct inode *inode = page->mapping->host; |
507903b8 | 2484 | |
be3940c0 | 2485 | pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, " |
9be3395b CM |
2486 | "mirror=%lu\n", (u64)bio->bi_sector, err, |
2487 | io_bio->mirror_num); | |
a71754fc | 2488 | tree = &BTRFS_I(inode)->io_tree; |
902b22f3 | 2489 | |
17a5adcc AO |
2490 | /* We always issue full-page reads, but if some block |
2491 | * in a page fails to read, blk_update_request() will | |
2492 | * advance bv_offset and adjust bv_len to compensate. | |
2493 | * Print a warning for nonzero offsets, and an error | |
2494 | * if they don't add up to a full page. */ | |
2495 | if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE) | |
2496 | printk("%s page read in btrfs with offset %u and length %u\n", | |
2497 | bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE | |
2498 | ? KERN_ERR "partial" : KERN_INFO "incomplete", | |
2499 | bvec->bv_offset, bvec->bv_len); | |
d1310b2e | 2500 | |
17a5adcc AO |
2501 | start = page_offset(page); |
2502 | end = start + bvec->bv_offset + bvec->bv_len - 1; | |
d1310b2e | 2503 | |
4125bf76 | 2504 | if (++bvec <= bvec_end) |
d1310b2e CM |
2505 | prefetchw(&bvec->bv_page->flags); |
2506 | ||
507903b8 | 2507 | spin_lock(&tree->lock); |
0d399205 | 2508 | state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED); |
109b36a2 | 2509 | if (state && state->start == start) { |
507903b8 AJ |
2510 | /* |
2511 | * take a reference on the state, unlock will drop | |
2512 | * the ref | |
2513 | */ | |
2514 | cache_state(state, &cached); | |
2515 | } | |
2516 | spin_unlock(&tree->lock); | |
2517 | ||
9be3395b | 2518 | mirror = io_bio->mirror_num; |
d1310b2e | 2519 | if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { |
70dec807 | 2520 | ret = tree->ops->readpage_end_io_hook(page, start, end, |
5cf1ab56 | 2521 | state, mirror); |
5ee0844d | 2522 | if (ret) |
d1310b2e | 2523 | uptodate = 0; |
5ee0844d | 2524 | else |
4a54c8c1 | 2525 | clean_io_failure(start, page); |
d1310b2e | 2526 | } |
ea466794 | 2527 | |
ea466794 | 2528 | if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) { |
5cf1ab56 | 2529 | ret = tree->ops->readpage_io_failed_hook(page, mirror); |
ea466794 JB |
2530 | if (!ret && !err && |
2531 | test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
2532 | uptodate = 1; | |
2533 | } else if (!uptodate) { | |
f4a8e656 JS |
2534 | /* |
2535 | * The generic bio_readpage_error handles errors the | |
2536 | * following way: If possible, new read requests are | |
2537 | * created and submitted and will end up in | |
2538 | * end_bio_extent_readpage as well (if we're lucky, not | |
2539 | * in the !uptodate case). In that case it returns 0 and | |
2540 | * we just go on with the next page in our bio. If it | |
2541 | * can't handle the error it will return -EIO and we | |
2542 | * remain responsible for that page. | |
2543 | */ | |
5cf1ab56 | 2544 | ret = bio_readpage_error(bio, page, start, end, mirror, NULL); |
7e38326f | 2545 | if (ret == 0) { |
3b951516 CM |
2546 | uptodate = |
2547 | test_bit(BIO_UPTODATE, &bio->bi_flags); | |
d20f7043 CM |
2548 | if (err) |
2549 | uptodate = 0; | |
507903b8 | 2550 | uncache_state(&cached); |
7e38326f CM |
2551 | continue; |
2552 | } | |
2553 | } | |
d1310b2e | 2554 | |
0b32f4bb | 2555 | if (uptodate && tree->track_uptodate) { |
507903b8 | 2556 | set_extent_uptodate(tree, start, end, &cached, |
902b22f3 | 2557 | GFP_ATOMIC); |
771ed689 | 2558 | } |
507903b8 | 2559 | unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); |
d1310b2e | 2560 | |
17a5adcc | 2561 | if (uptodate) { |
a71754fc JB |
2562 | loff_t i_size = i_size_read(inode); |
2563 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; | |
2564 | unsigned offset; | |
2565 | ||
2566 | /* Zero out the end if this page straddles i_size */ | |
2567 | offset = i_size & (PAGE_CACHE_SIZE-1); | |
2568 | if (page->index == end_index && offset) | |
2569 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); | |
17a5adcc | 2570 | SetPageUptodate(page); |
70dec807 | 2571 | } else { |
17a5adcc AO |
2572 | ClearPageUptodate(page); |
2573 | SetPageError(page); | |
70dec807 | 2574 | } |
17a5adcc | 2575 | unlock_page(page); |
4125bf76 | 2576 | } while (bvec <= bvec_end); |
d1310b2e CM |
2577 | |
2578 | bio_put(bio); | |
d1310b2e CM |
2579 | } |
2580 | ||
9be3395b CM |
2581 | /* |
2582 | * this allocates from the btrfs_bioset. We're returning a bio right now | |
2583 | * but you can call btrfs_io_bio for the appropriate container_of magic | |
2584 | */ | |
88f794ed MX |
2585 | struct bio * |
2586 | btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs, | |
2587 | gfp_t gfp_flags) | |
d1310b2e CM |
2588 | { |
2589 | struct bio *bio; | |
2590 | ||
9be3395b | 2591 | bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset); |
d1310b2e CM |
2592 | |
2593 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { | |
9be3395b CM |
2594 | while (!bio && (nr_vecs /= 2)) { |
2595 | bio = bio_alloc_bioset(gfp_flags, | |
2596 | nr_vecs, btrfs_bioset); | |
2597 | } | |
d1310b2e CM |
2598 | } |
2599 | ||
2600 | if (bio) { | |
e1c4b745 | 2601 | bio->bi_size = 0; |
d1310b2e CM |
2602 | bio->bi_bdev = bdev; |
2603 | bio->bi_sector = first_sector; | |
2604 | } | |
2605 | return bio; | |
2606 | } | |
2607 | ||
9be3395b CM |
2608 | struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask) |
2609 | { | |
2610 | return bio_clone_bioset(bio, gfp_mask, btrfs_bioset); | |
2611 | } | |
2612 | ||
2613 | ||
2614 | /* this also allocates from the btrfs_bioset */ | |
2615 | struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) | |
2616 | { | |
2617 | return bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset); | |
2618 | } | |
2619 | ||
2620 | ||
355808c2 JM |
2621 | static int __must_check submit_one_bio(int rw, struct bio *bio, |
2622 | int mirror_num, unsigned long bio_flags) | |
d1310b2e | 2623 | { |
d1310b2e | 2624 | int ret = 0; |
70dec807 CM |
2625 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; |
2626 | struct page *page = bvec->bv_page; | |
2627 | struct extent_io_tree *tree = bio->bi_private; | |
70dec807 | 2628 | u64 start; |
70dec807 | 2629 | |
4eee4fa4 | 2630 | start = page_offset(page) + bvec->bv_offset; |
70dec807 | 2631 | |
902b22f3 | 2632 | bio->bi_private = NULL; |
d1310b2e CM |
2633 | |
2634 | bio_get(bio); | |
2635 | ||
065631f6 | 2636 | if (tree->ops && tree->ops->submit_bio_hook) |
6b82ce8d | 2637 | ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio, |
eaf25d93 | 2638 | mirror_num, bio_flags, start); |
0b86a832 | 2639 | else |
21adbd5c | 2640 | btrfsic_submit_bio(rw, bio); |
4a54c8c1 | 2641 | |
d1310b2e CM |
2642 | if (bio_flagged(bio, BIO_EOPNOTSUPP)) |
2643 | ret = -EOPNOTSUPP; | |
2644 | bio_put(bio); | |
2645 | return ret; | |
2646 | } | |
2647 | ||
64a16701 | 2648 | static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page, |
3444a972 JM |
2649 | unsigned long offset, size_t size, struct bio *bio, |
2650 | unsigned long bio_flags) | |
2651 | { | |
2652 | int ret = 0; | |
2653 | if (tree->ops && tree->ops->merge_bio_hook) | |
64a16701 | 2654 | ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio, |
3444a972 JM |
2655 | bio_flags); |
2656 | BUG_ON(ret < 0); | |
2657 | return ret; | |
2658 | ||
2659 | } | |
2660 | ||
d1310b2e CM |
2661 | static int submit_extent_page(int rw, struct extent_io_tree *tree, |
2662 | struct page *page, sector_t sector, | |
2663 | size_t size, unsigned long offset, | |
2664 | struct block_device *bdev, | |
2665 | struct bio **bio_ret, | |
2666 | unsigned long max_pages, | |
f188591e | 2667 | bio_end_io_t end_io_func, |
c8b97818 CM |
2668 | int mirror_num, |
2669 | unsigned long prev_bio_flags, | |
2670 | unsigned long bio_flags) | |
d1310b2e CM |
2671 | { |
2672 | int ret = 0; | |
2673 | struct bio *bio; | |
2674 | int nr; | |
c8b97818 CM |
2675 | int contig = 0; |
2676 | int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED; | |
2677 | int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED; | |
5b050f04 | 2678 | size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE); |
d1310b2e CM |
2679 | |
2680 | if (bio_ret && *bio_ret) { | |
2681 | bio = *bio_ret; | |
c8b97818 CM |
2682 | if (old_compressed) |
2683 | contig = bio->bi_sector == sector; | |
2684 | else | |
f73a1c7d | 2685 | contig = bio_end_sector(bio) == sector; |
c8b97818 CM |
2686 | |
2687 | if (prev_bio_flags != bio_flags || !contig || | |
64a16701 | 2688 | merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) || |
c8b97818 CM |
2689 | bio_add_page(bio, page, page_size, offset) < page_size) { |
2690 | ret = submit_one_bio(rw, bio, mirror_num, | |
2691 | prev_bio_flags); | |
79787eaa JM |
2692 | if (ret < 0) |
2693 | return ret; | |
d1310b2e CM |
2694 | bio = NULL; |
2695 | } else { | |
2696 | return 0; | |
2697 | } | |
2698 | } | |
c8b97818 CM |
2699 | if (this_compressed) |
2700 | nr = BIO_MAX_PAGES; | |
2701 | else | |
2702 | nr = bio_get_nr_vecs(bdev); | |
2703 | ||
88f794ed | 2704 | bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH); |
5df67083 TI |
2705 | if (!bio) |
2706 | return -ENOMEM; | |
70dec807 | 2707 | |
c8b97818 | 2708 | bio_add_page(bio, page, page_size, offset); |
d1310b2e CM |
2709 | bio->bi_end_io = end_io_func; |
2710 | bio->bi_private = tree; | |
70dec807 | 2711 | |
d397712b | 2712 | if (bio_ret) |
d1310b2e | 2713 | *bio_ret = bio; |
d397712b | 2714 | else |
c8b97818 | 2715 | ret = submit_one_bio(rw, bio, mirror_num, bio_flags); |
d1310b2e CM |
2716 | |
2717 | return ret; | |
2718 | } | |
2719 | ||
48a3b636 ES |
2720 | static void attach_extent_buffer_page(struct extent_buffer *eb, |
2721 | struct page *page) | |
d1310b2e CM |
2722 | { |
2723 | if (!PagePrivate(page)) { | |
2724 | SetPagePrivate(page); | |
d1310b2e | 2725 | page_cache_get(page); |
4f2de97a JB |
2726 | set_page_private(page, (unsigned long)eb); |
2727 | } else { | |
2728 | WARN_ON(page->private != (unsigned long)eb); | |
d1310b2e CM |
2729 | } |
2730 | } | |
2731 | ||
4f2de97a | 2732 | void set_page_extent_mapped(struct page *page) |
d1310b2e | 2733 | { |
4f2de97a JB |
2734 | if (!PagePrivate(page)) { |
2735 | SetPagePrivate(page); | |
2736 | page_cache_get(page); | |
2737 | set_page_private(page, EXTENT_PAGE_PRIVATE); | |
2738 | } | |
d1310b2e CM |
2739 | } |
2740 | ||
2741 | /* | |
2742 | * basic readpage implementation. Locked extent state structs are inserted | |
2743 | * into the tree that are removed when the IO is done (by the end_io | |
2744 | * handlers) | |
79787eaa | 2745 | * XXX JDM: This needs looking at to ensure proper page locking |
d1310b2e CM |
2746 | */ |
2747 | static int __extent_read_full_page(struct extent_io_tree *tree, | |
2748 | struct page *page, | |
2749 | get_extent_t *get_extent, | |
c8b97818 | 2750 | struct bio **bio, int mirror_num, |
d4c7ca86 | 2751 | unsigned long *bio_flags, int rw) |
d1310b2e CM |
2752 | { |
2753 | struct inode *inode = page->mapping->host; | |
4eee4fa4 | 2754 | u64 start = page_offset(page); |
d1310b2e CM |
2755 | u64 page_end = start + PAGE_CACHE_SIZE - 1; |
2756 | u64 end; | |
2757 | u64 cur = start; | |
2758 | u64 extent_offset; | |
2759 | u64 last_byte = i_size_read(inode); | |
2760 | u64 block_start; | |
2761 | u64 cur_end; | |
2762 | sector_t sector; | |
2763 | struct extent_map *em; | |
2764 | struct block_device *bdev; | |
11c65dcc | 2765 | struct btrfs_ordered_extent *ordered; |
d1310b2e CM |
2766 | int ret; |
2767 | int nr = 0; | |
306e16ce | 2768 | size_t pg_offset = 0; |
d1310b2e | 2769 | size_t iosize; |
c8b97818 | 2770 | size_t disk_io_size; |
d1310b2e | 2771 | size_t blocksize = inode->i_sb->s_blocksize; |
c8b97818 | 2772 | unsigned long this_bio_flag = 0; |
d1310b2e CM |
2773 | |
2774 | set_page_extent_mapped(page); | |
2775 | ||
90a887c9 DM |
2776 | if (!PageUptodate(page)) { |
2777 | if (cleancache_get_page(page) == 0) { | |
2778 | BUG_ON(blocksize != PAGE_SIZE); | |
2779 | goto out; | |
2780 | } | |
2781 | } | |
2782 | ||
d1310b2e | 2783 | end = page_end; |
11c65dcc | 2784 | while (1) { |
d0082371 | 2785 | lock_extent(tree, start, end); |
11c65dcc JB |
2786 | ordered = btrfs_lookup_ordered_extent(inode, start); |
2787 | if (!ordered) | |
2788 | break; | |
d0082371 | 2789 | unlock_extent(tree, start, end); |
11c65dcc JB |
2790 | btrfs_start_ordered_extent(inode, ordered, 1); |
2791 | btrfs_put_ordered_extent(ordered); | |
2792 | } | |
d1310b2e | 2793 | |
c8b97818 CM |
2794 | if (page->index == last_byte >> PAGE_CACHE_SHIFT) { |
2795 | char *userpage; | |
2796 | size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1); | |
2797 | ||
2798 | if (zero_offset) { | |
2799 | iosize = PAGE_CACHE_SIZE - zero_offset; | |
7ac687d9 | 2800 | userpage = kmap_atomic(page); |
c8b97818 CM |
2801 | memset(userpage + zero_offset, 0, iosize); |
2802 | flush_dcache_page(page); | |
7ac687d9 | 2803 | kunmap_atomic(userpage); |
c8b97818 CM |
2804 | } |
2805 | } | |
d1310b2e | 2806 | while (cur <= end) { |
c8f2f24b JB |
2807 | unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1; |
2808 | ||
d1310b2e CM |
2809 | if (cur >= last_byte) { |
2810 | char *userpage; | |
507903b8 AJ |
2811 | struct extent_state *cached = NULL; |
2812 | ||
306e16ce | 2813 | iosize = PAGE_CACHE_SIZE - pg_offset; |
7ac687d9 | 2814 | userpage = kmap_atomic(page); |
306e16ce | 2815 | memset(userpage + pg_offset, 0, iosize); |
d1310b2e | 2816 | flush_dcache_page(page); |
7ac687d9 | 2817 | kunmap_atomic(userpage); |
d1310b2e | 2818 | set_extent_uptodate(tree, cur, cur + iosize - 1, |
507903b8 AJ |
2819 | &cached, GFP_NOFS); |
2820 | unlock_extent_cached(tree, cur, cur + iosize - 1, | |
2821 | &cached, GFP_NOFS); | |
d1310b2e CM |
2822 | break; |
2823 | } | |
306e16ce | 2824 | em = get_extent(inode, page, pg_offset, cur, |
d1310b2e | 2825 | end - cur + 1, 0); |
c704005d | 2826 | if (IS_ERR_OR_NULL(em)) { |
d1310b2e | 2827 | SetPageError(page); |
d0082371 | 2828 | unlock_extent(tree, cur, end); |
d1310b2e CM |
2829 | break; |
2830 | } | |
d1310b2e CM |
2831 | extent_offset = cur - em->start; |
2832 | BUG_ON(extent_map_end(em) <= cur); | |
2833 | BUG_ON(end < cur); | |
2834 | ||
261507a0 | 2835 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
c8b97818 | 2836 | this_bio_flag = EXTENT_BIO_COMPRESSED; |
261507a0 LZ |
2837 | extent_set_compress_type(&this_bio_flag, |
2838 | em->compress_type); | |
2839 | } | |
c8b97818 | 2840 | |
d1310b2e CM |
2841 | iosize = min(extent_map_end(em) - cur, end - cur + 1); |
2842 | cur_end = min(extent_map_end(em) - 1, end); | |
fda2832f | 2843 | iosize = ALIGN(iosize, blocksize); |
c8b97818 CM |
2844 | if (this_bio_flag & EXTENT_BIO_COMPRESSED) { |
2845 | disk_io_size = em->block_len; | |
2846 | sector = em->block_start >> 9; | |
2847 | } else { | |
2848 | sector = (em->block_start + extent_offset) >> 9; | |
2849 | disk_io_size = iosize; | |
2850 | } | |
d1310b2e CM |
2851 | bdev = em->bdev; |
2852 | block_start = em->block_start; | |
d899e052 YZ |
2853 | if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) |
2854 | block_start = EXTENT_MAP_HOLE; | |
d1310b2e CM |
2855 | free_extent_map(em); |
2856 | em = NULL; | |
2857 | ||
2858 | /* we've found a hole, just zero and go on */ | |
2859 | if (block_start == EXTENT_MAP_HOLE) { | |
2860 | char *userpage; | |
507903b8 AJ |
2861 | struct extent_state *cached = NULL; |
2862 | ||
7ac687d9 | 2863 | userpage = kmap_atomic(page); |
306e16ce | 2864 | memset(userpage + pg_offset, 0, iosize); |
d1310b2e | 2865 | flush_dcache_page(page); |
7ac687d9 | 2866 | kunmap_atomic(userpage); |
d1310b2e CM |
2867 | |
2868 | set_extent_uptodate(tree, cur, cur + iosize - 1, | |
507903b8 AJ |
2869 | &cached, GFP_NOFS); |
2870 | unlock_extent_cached(tree, cur, cur + iosize - 1, | |
2871 | &cached, GFP_NOFS); | |
d1310b2e | 2872 | cur = cur + iosize; |
306e16ce | 2873 | pg_offset += iosize; |
d1310b2e CM |
2874 | continue; |
2875 | } | |
2876 | /* the get_extent function already copied into the page */ | |
9655d298 CM |
2877 | if (test_range_bit(tree, cur, cur_end, |
2878 | EXTENT_UPTODATE, 1, NULL)) { | |
a1b32a59 | 2879 | check_page_uptodate(tree, page); |
d0082371 | 2880 | unlock_extent(tree, cur, cur + iosize - 1); |
d1310b2e | 2881 | cur = cur + iosize; |
306e16ce | 2882 | pg_offset += iosize; |
d1310b2e CM |
2883 | continue; |
2884 | } | |
70dec807 CM |
2885 | /* we have an inline extent but it didn't get marked up |
2886 | * to date. Error out | |
2887 | */ | |
2888 | if (block_start == EXTENT_MAP_INLINE) { | |
2889 | SetPageError(page); | |
d0082371 | 2890 | unlock_extent(tree, cur, cur + iosize - 1); |
70dec807 | 2891 | cur = cur + iosize; |
306e16ce | 2892 | pg_offset += iosize; |
70dec807 CM |
2893 | continue; |
2894 | } | |
d1310b2e | 2895 | |
c8f2f24b | 2896 | pnr -= page->index; |
d4c7ca86 | 2897 | ret = submit_extent_page(rw, tree, page, |
306e16ce | 2898 | sector, disk_io_size, pg_offset, |
89642229 | 2899 | bdev, bio, pnr, |
c8b97818 CM |
2900 | end_bio_extent_readpage, mirror_num, |
2901 | *bio_flags, | |
2902 | this_bio_flag); | |
c8f2f24b JB |
2903 | if (!ret) { |
2904 | nr++; | |
2905 | *bio_flags = this_bio_flag; | |
2906 | } else { | |
d1310b2e | 2907 | SetPageError(page); |
edd33c99 JB |
2908 | unlock_extent(tree, cur, cur + iosize - 1); |
2909 | } | |
d1310b2e | 2910 | cur = cur + iosize; |
306e16ce | 2911 | pg_offset += iosize; |
d1310b2e | 2912 | } |
90a887c9 | 2913 | out: |
d1310b2e CM |
2914 | if (!nr) { |
2915 | if (!PageError(page)) | |
2916 | SetPageUptodate(page); | |
2917 | unlock_page(page); | |
2918 | } | |
2919 | return 0; | |
2920 | } | |
2921 | ||
2922 | int extent_read_full_page(struct extent_io_tree *tree, struct page *page, | |
8ddc7d9c | 2923 | get_extent_t *get_extent, int mirror_num) |
d1310b2e CM |
2924 | { |
2925 | struct bio *bio = NULL; | |
c8b97818 | 2926 | unsigned long bio_flags = 0; |
d1310b2e CM |
2927 | int ret; |
2928 | ||
8ddc7d9c | 2929 | ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num, |
d4c7ca86 | 2930 | &bio_flags, READ); |
d1310b2e | 2931 | if (bio) |
8ddc7d9c | 2932 | ret = submit_one_bio(READ, bio, mirror_num, bio_flags); |
d1310b2e CM |
2933 | return ret; |
2934 | } | |
d1310b2e | 2935 | |
11c8349b CM |
2936 | static noinline void update_nr_written(struct page *page, |
2937 | struct writeback_control *wbc, | |
2938 | unsigned long nr_written) | |
2939 | { | |
2940 | wbc->nr_to_write -= nr_written; | |
2941 | if (wbc->range_cyclic || (wbc->nr_to_write > 0 && | |
2942 | wbc->range_start == 0 && wbc->range_end == LLONG_MAX)) | |
2943 | page->mapping->writeback_index = page->index + nr_written; | |
2944 | } | |
2945 | ||
d1310b2e CM |
2946 | /* |
2947 | * the writepage semantics are similar to regular writepage. extent | |
2948 | * records are inserted to lock ranges in the tree, and as dirty areas | |
2949 | * are found, they are marked writeback. Then the lock bits are removed | |
2950 | * and the end_io handler clears the writeback ranges | |
2951 | */ | |
2952 | static int __extent_writepage(struct page *page, struct writeback_control *wbc, | |
2953 | void *data) | |
2954 | { | |
2955 | struct inode *inode = page->mapping->host; | |
2956 | struct extent_page_data *epd = data; | |
2957 | struct extent_io_tree *tree = epd->tree; | |
4eee4fa4 | 2958 | u64 start = page_offset(page); |
d1310b2e CM |
2959 | u64 delalloc_start; |
2960 | u64 page_end = start + PAGE_CACHE_SIZE - 1; | |
2961 | u64 end; | |
2962 | u64 cur = start; | |
2963 | u64 extent_offset; | |
2964 | u64 last_byte = i_size_read(inode); | |
2965 | u64 block_start; | |
2966 | u64 iosize; | |
2967 | sector_t sector; | |
2c64c53d | 2968 | struct extent_state *cached_state = NULL; |
d1310b2e CM |
2969 | struct extent_map *em; |
2970 | struct block_device *bdev; | |
2971 | int ret; | |
2972 | int nr = 0; | |
7f3c74fb | 2973 | size_t pg_offset = 0; |
d1310b2e CM |
2974 | size_t blocksize; |
2975 | loff_t i_size = i_size_read(inode); | |
2976 | unsigned long end_index = i_size >> PAGE_CACHE_SHIFT; | |
2977 | u64 nr_delalloc; | |
2978 | u64 delalloc_end; | |
c8b97818 CM |
2979 | int page_started; |
2980 | int compressed; | |
ffbd517d | 2981 | int write_flags; |
771ed689 | 2982 | unsigned long nr_written = 0; |
9e487107 | 2983 | bool fill_delalloc = true; |
d1310b2e | 2984 | |
ffbd517d | 2985 | if (wbc->sync_mode == WB_SYNC_ALL) |
721a9602 | 2986 | write_flags = WRITE_SYNC; |
ffbd517d CM |
2987 | else |
2988 | write_flags = WRITE; | |
2989 | ||
1abe9b8a | 2990 | trace___extent_writepage(page, inode, wbc); |
2991 | ||
d1310b2e | 2992 | WARN_ON(!PageLocked(page)); |
bf0da8c1 CM |
2993 | |
2994 | ClearPageError(page); | |
2995 | ||
7f3c74fb | 2996 | pg_offset = i_size & (PAGE_CACHE_SIZE - 1); |
211c17f5 | 2997 | if (page->index > end_index || |
7f3c74fb | 2998 | (page->index == end_index && !pg_offset)) { |
d47992f8 | 2999 | page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE); |
d1310b2e CM |
3000 | unlock_page(page); |
3001 | return 0; | |
3002 | } | |
3003 | ||
3004 | if (page->index == end_index) { | |
3005 | char *userpage; | |
3006 | ||
7ac687d9 | 3007 | userpage = kmap_atomic(page); |
7f3c74fb CM |
3008 | memset(userpage + pg_offset, 0, |
3009 | PAGE_CACHE_SIZE - pg_offset); | |
7ac687d9 | 3010 | kunmap_atomic(userpage); |
211c17f5 | 3011 | flush_dcache_page(page); |
d1310b2e | 3012 | } |
7f3c74fb | 3013 | pg_offset = 0; |
d1310b2e CM |
3014 | |
3015 | set_page_extent_mapped(page); | |
3016 | ||
9e487107 JB |
3017 | if (!tree->ops || !tree->ops->fill_delalloc) |
3018 | fill_delalloc = false; | |
3019 | ||
d1310b2e CM |
3020 | delalloc_start = start; |
3021 | delalloc_end = 0; | |
c8b97818 | 3022 | page_started = 0; |
9e487107 | 3023 | if (!epd->extent_locked && fill_delalloc) { |
f85d7d6c | 3024 | u64 delalloc_to_write = 0; |
11c8349b CM |
3025 | /* |
3026 | * make sure the wbc mapping index is at least updated | |
3027 | * to this page. | |
3028 | */ | |
3029 | update_nr_written(page, wbc, 0); | |
3030 | ||
d397712b | 3031 | while (delalloc_end < page_end) { |
771ed689 | 3032 | nr_delalloc = find_lock_delalloc_range(inode, tree, |
c8b97818 CM |
3033 | page, |
3034 | &delalloc_start, | |
d1310b2e CM |
3035 | &delalloc_end, |
3036 | 128 * 1024 * 1024); | |
771ed689 CM |
3037 | if (nr_delalloc == 0) { |
3038 | delalloc_start = delalloc_end + 1; | |
3039 | continue; | |
3040 | } | |
013bd4c3 TI |
3041 | ret = tree->ops->fill_delalloc(inode, page, |
3042 | delalloc_start, | |
3043 | delalloc_end, | |
3044 | &page_started, | |
3045 | &nr_written); | |
79787eaa JM |
3046 | /* File system has been set read-only */ |
3047 | if (ret) { | |
3048 | SetPageError(page); | |
3049 | goto done; | |
3050 | } | |
f85d7d6c CM |
3051 | /* |
3052 | * delalloc_end is already one less than the total | |
3053 | * length, so we don't subtract one from | |
3054 | * PAGE_CACHE_SIZE | |
3055 | */ | |
3056 | delalloc_to_write += (delalloc_end - delalloc_start + | |
3057 | PAGE_CACHE_SIZE) >> | |
3058 | PAGE_CACHE_SHIFT; | |
d1310b2e | 3059 | delalloc_start = delalloc_end + 1; |
d1310b2e | 3060 | } |
f85d7d6c CM |
3061 | if (wbc->nr_to_write < delalloc_to_write) { |
3062 | int thresh = 8192; | |
3063 | ||
3064 | if (delalloc_to_write < thresh * 2) | |
3065 | thresh = delalloc_to_write; | |
3066 | wbc->nr_to_write = min_t(u64, delalloc_to_write, | |
3067 | thresh); | |
3068 | } | |
c8b97818 | 3069 | |
771ed689 CM |
3070 | /* did the fill delalloc function already unlock and start |
3071 | * the IO? | |
3072 | */ | |
3073 | if (page_started) { | |
3074 | ret = 0; | |
11c8349b CM |
3075 | /* |
3076 | * we've unlocked the page, so we can't update | |
3077 | * the mapping's writeback index, just update | |
3078 | * nr_to_write. | |
3079 | */ | |
3080 | wbc->nr_to_write -= nr_written; | |
3081 | goto done_unlocked; | |
771ed689 | 3082 | } |
c8b97818 | 3083 | } |
247e743c | 3084 | if (tree->ops && tree->ops->writepage_start_hook) { |
c8b97818 CM |
3085 | ret = tree->ops->writepage_start_hook(page, start, |
3086 | page_end); | |
87826df0 JM |
3087 | if (ret) { |
3088 | /* Fixup worker will requeue */ | |
3089 | if (ret == -EBUSY) | |
3090 | wbc->pages_skipped++; | |
3091 | else | |
3092 | redirty_page_for_writepage(wbc, page); | |
11c8349b | 3093 | update_nr_written(page, wbc, nr_written); |
247e743c | 3094 | unlock_page(page); |
771ed689 | 3095 | ret = 0; |
11c8349b | 3096 | goto done_unlocked; |
247e743c CM |
3097 | } |
3098 | } | |
3099 | ||
11c8349b CM |
3100 | /* |
3101 | * we don't want to touch the inode after unlocking the page, | |
3102 | * so we update the mapping writeback index now | |
3103 | */ | |
3104 | update_nr_written(page, wbc, nr_written + 1); | |
771ed689 | 3105 | |
d1310b2e | 3106 | end = page_end; |
d1310b2e | 3107 | if (last_byte <= start) { |
e6dcd2dc CM |
3108 | if (tree->ops && tree->ops->writepage_end_io_hook) |
3109 | tree->ops->writepage_end_io_hook(page, start, | |
3110 | page_end, NULL, 1); | |
d1310b2e CM |
3111 | goto done; |
3112 | } | |
3113 | ||
d1310b2e CM |
3114 | blocksize = inode->i_sb->s_blocksize; |
3115 | ||
3116 | while (cur <= end) { | |
3117 | if (cur >= last_byte) { | |
e6dcd2dc CM |
3118 | if (tree->ops && tree->ops->writepage_end_io_hook) |
3119 | tree->ops->writepage_end_io_hook(page, cur, | |
3120 | page_end, NULL, 1); | |
d1310b2e CM |
3121 | break; |
3122 | } | |
7f3c74fb | 3123 | em = epd->get_extent(inode, page, pg_offset, cur, |
d1310b2e | 3124 | end - cur + 1, 1); |
c704005d | 3125 | if (IS_ERR_OR_NULL(em)) { |
d1310b2e CM |
3126 | SetPageError(page); |
3127 | break; | |
3128 | } | |
3129 | ||
3130 | extent_offset = cur - em->start; | |
3131 | BUG_ON(extent_map_end(em) <= cur); | |
3132 | BUG_ON(end < cur); | |
3133 | iosize = min(extent_map_end(em) - cur, end - cur + 1); | |
fda2832f | 3134 | iosize = ALIGN(iosize, blocksize); |
d1310b2e CM |
3135 | sector = (em->block_start + extent_offset) >> 9; |
3136 | bdev = em->bdev; | |
3137 | block_start = em->block_start; | |
c8b97818 | 3138 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
d1310b2e CM |
3139 | free_extent_map(em); |
3140 | em = NULL; | |
3141 | ||
c8b97818 CM |
3142 | /* |
3143 | * compressed and inline extents are written through other | |
3144 | * paths in the FS | |
3145 | */ | |
3146 | if (compressed || block_start == EXTENT_MAP_HOLE || | |
d1310b2e | 3147 | block_start == EXTENT_MAP_INLINE) { |
c8b97818 CM |
3148 | /* |
3149 | * end_io notification does not happen here for | |
3150 | * compressed extents | |
3151 | */ | |
3152 | if (!compressed && tree->ops && | |
3153 | tree->ops->writepage_end_io_hook) | |
e6dcd2dc CM |
3154 | tree->ops->writepage_end_io_hook(page, cur, |
3155 | cur + iosize - 1, | |
3156 | NULL, 1); | |
c8b97818 CM |
3157 | else if (compressed) { |
3158 | /* we don't want to end_page_writeback on | |
3159 | * a compressed extent. this happens | |
3160 | * elsewhere | |
3161 | */ | |
3162 | nr++; | |
3163 | } | |
3164 | ||
3165 | cur += iosize; | |
7f3c74fb | 3166 | pg_offset += iosize; |
d1310b2e CM |
3167 | continue; |
3168 | } | |
d1310b2e CM |
3169 | /* leave this out until we have a page_mkwrite call */ |
3170 | if (0 && !test_range_bit(tree, cur, cur + iosize - 1, | |
9655d298 | 3171 | EXTENT_DIRTY, 0, NULL)) { |
d1310b2e | 3172 | cur = cur + iosize; |
7f3c74fb | 3173 | pg_offset += iosize; |
d1310b2e CM |
3174 | continue; |
3175 | } | |
c8b97818 | 3176 | |
d1310b2e CM |
3177 | if (tree->ops && tree->ops->writepage_io_hook) { |
3178 | ret = tree->ops->writepage_io_hook(page, cur, | |
3179 | cur + iosize - 1); | |
3180 | } else { | |
3181 | ret = 0; | |
3182 | } | |
1259ab75 | 3183 | if (ret) { |
d1310b2e | 3184 | SetPageError(page); |
1259ab75 | 3185 | } else { |
d1310b2e | 3186 | unsigned long max_nr = end_index + 1; |
7f3c74fb | 3187 | |
d1310b2e CM |
3188 | set_range_writeback(tree, cur, cur + iosize - 1); |
3189 | if (!PageWriteback(page)) { | |
d397712b CM |
3190 | printk(KERN_ERR "btrfs warning page %lu not " |
3191 | "writeback, cur %llu end %llu\n", | |
3192 | page->index, (unsigned long long)cur, | |
d1310b2e CM |
3193 | (unsigned long long)end); |
3194 | } | |
3195 | ||
ffbd517d CM |
3196 | ret = submit_extent_page(write_flags, tree, page, |
3197 | sector, iosize, pg_offset, | |
3198 | bdev, &epd->bio, max_nr, | |
c8b97818 CM |
3199 | end_bio_extent_writepage, |
3200 | 0, 0, 0); | |
d1310b2e CM |
3201 | if (ret) |
3202 | SetPageError(page); | |
3203 | } | |
3204 | cur = cur + iosize; | |
7f3c74fb | 3205 | pg_offset += iosize; |
d1310b2e CM |
3206 | nr++; |
3207 | } | |
3208 | done: | |
3209 | if (nr == 0) { | |
3210 | /* make sure the mapping tag for page dirty gets cleared */ | |
3211 | set_page_writeback(page); | |
3212 | end_page_writeback(page); | |
3213 | } | |
d1310b2e | 3214 | unlock_page(page); |
771ed689 | 3215 | |
11c8349b CM |
3216 | done_unlocked: |
3217 | ||
2c64c53d CM |
3218 | /* drop our reference on any cached states */ |
3219 | free_extent_state(cached_state); | |
d1310b2e CM |
3220 | return 0; |
3221 | } | |
3222 | ||
0b32f4bb JB |
3223 | static int eb_wait(void *word) |
3224 | { | |
3225 | io_schedule(); | |
3226 | return 0; | |
3227 | } | |
3228 | ||
fd8b2b61 | 3229 | void wait_on_extent_buffer_writeback(struct extent_buffer *eb) |
0b32f4bb JB |
3230 | { |
3231 | wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait, | |
3232 | TASK_UNINTERRUPTIBLE); | |
3233 | } | |
3234 | ||
3235 | static int lock_extent_buffer_for_io(struct extent_buffer *eb, | |
3236 | struct btrfs_fs_info *fs_info, | |
3237 | struct extent_page_data *epd) | |
3238 | { | |
3239 | unsigned long i, num_pages; | |
3240 | int flush = 0; | |
3241 | int ret = 0; | |
3242 | ||
3243 | if (!btrfs_try_tree_write_lock(eb)) { | |
3244 | flush = 1; | |
3245 | flush_write_bio(epd); | |
3246 | btrfs_tree_lock(eb); | |
3247 | } | |
3248 | ||
3249 | if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) { | |
3250 | btrfs_tree_unlock(eb); | |
3251 | if (!epd->sync_io) | |
3252 | return 0; | |
3253 | if (!flush) { | |
3254 | flush_write_bio(epd); | |
3255 | flush = 1; | |
3256 | } | |
a098d8e8 CM |
3257 | while (1) { |
3258 | wait_on_extent_buffer_writeback(eb); | |
3259 | btrfs_tree_lock(eb); | |
3260 | if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) | |
3261 | break; | |
0b32f4bb | 3262 | btrfs_tree_unlock(eb); |
0b32f4bb JB |
3263 | } |
3264 | } | |
3265 | ||
51561ffe JB |
3266 | /* |
3267 | * We need to do this to prevent races in people who check if the eb is | |
3268 | * under IO since we can end up having no IO bits set for a short period | |
3269 | * of time. | |
3270 | */ | |
3271 | spin_lock(&eb->refs_lock); | |
0b32f4bb JB |
3272 | if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) { |
3273 | set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); | |
51561ffe | 3274 | spin_unlock(&eb->refs_lock); |
0b32f4bb | 3275 | btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN); |
e2d84521 MX |
3276 | __percpu_counter_add(&fs_info->dirty_metadata_bytes, |
3277 | -eb->len, | |
3278 | fs_info->dirty_metadata_batch); | |
0b32f4bb | 3279 | ret = 1; |
51561ffe JB |
3280 | } else { |
3281 | spin_unlock(&eb->refs_lock); | |
0b32f4bb JB |
3282 | } |
3283 | ||
3284 | btrfs_tree_unlock(eb); | |
3285 | ||
3286 | if (!ret) | |
3287 | return ret; | |
3288 | ||
3289 | num_pages = num_extent_pages(eb->start, eb->len); | |
3290 | for (i = 0; i < num_pages; i++) { | |
3291 | struct page *p = extent_buffer_page(eb, i); | |
3292 | ||
3293 | if (!trylock_page(p)) { | |
3294 | if (!flush) { | |
3295 | flush_write_bio(epd); | |
3296 | flush = 1; | |
3297 | } | |
3298 | lock_page(p); | |
3299 | } | |
3300 | } | |
3301 | ||
3302 | return ret; | |
3303 | } | |
3304 | ||
3305 | static void end_extent_buffer_writeback(struct extent_buffer *eb) | |
3306 | { | |
3307 | clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags); | |
3308 | smp_mb__after_clear_bit(); | |
3309 | wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK); | |
3310 | } | |
3311 | ||
3312 | static void end_bio_extent_buffer_writepage(struct bio *bio, int err) | |
3313 | { | |
3314 | int uptodate = err == 0; | |
3315 | struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; | |
3316 | struct extent_buffer *eb; | |
3317 | int done; | |
3318 | ||
3319 | do { | |
3320 | struct page *page = bvec->bv_page; | |
3321 | ||
3322 | bvec--; | |
3323 | eb = (struct extent_buffer *)page->private; | |
3324 | BUG_ON(!eb); | |
3325 | done = atomic_dec_and_test(&eb->io_pages); | |
3326 | ||
3327 | if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) { | |
3328 | set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); | |
3329 | ClearPageUptodate(page); | |
3330 | SetPageError(page); | |
3331 | } | |
3332 | ||
3333 | end_page_writeback(page); | |
3334 | ||
3335 | if (!done) | |
3336 | continue; | |
3337 | ||
3338 | end_extent_buffer_writeback(eb); | |
3339 | } while (bvec >= bio->bi_io_vec); | |
3340 | ||
3341 | bio_put(bio); | |
3342 | ||
3343 | } | |
3344 | ||
3345 | static int write_one_eb(struct extent_buffer *eb, | |
3346 | struct btrfs_fs_info *fs_info, | |
3347 | struct writeback_control *wbc, | |
3348 | struct extent_page_data *epd) | |
3349 | { | |
3350 | struct block_device *bdev = fs_info->fs_devices->latest_bdev; | |
3351 | u64 offset = eb->start; | |
3352 | unsigned long i, num_pages; | |
de0022b9 | 3353 | unsigned long bio_flags = 0; |
d4c7ca86 | 3354 | int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META; |
d7dbe9e7 | 3355 | int ret = 0; |
0b32f4bb JB |
3356 | |
3357 | clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags); | |
3358 | num_pages = num_extent_pages(eb->start, eb->len); | |
3359 | atomic_set(&eb->io_pages, num_pages); | |
de0022b9 JB |
3360 | if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID) |
3361 | bio_flags = EXTENT_BIO_TREE_LOG; | |
3362 | ||
0b32f4bb JB |
3363 | for (i = 0; i < num_pages; i++) { |
3364 | struct page *p = extent_buffer_page(eb, i); | |
3365 | ||
3366 | clear_page_dirty_for_io(p); | |
3367 | set_page_writeback(p); | |
3368 | ret = submit_extent_page(rw, eb->tree, p, offset >> 9, | |
3369 | PAGE_CACHE_SIZE, 0, bdev, &epd->bio, | |
3370 | -1, end_bio_extent_buffer_writepage, | |
de0022b9 JB |
3371 | 0, epd->bio_flags, bio_flags); |
3372 | epd->bio_flags = bio_flags; | |
0b32f4bb JB |
3373 | if (ret) { |
3374 | set_bit(EXTENT_BUFFER_IOERR, &eb->bflags); | |
3375 | SetPageError(p); | |
3376 | if (atomic_sub_and_test(num_pages - i, &eb->io_pages)) | |
3377 | end_extent_buffer_writeback(eb); | |
3378 | ret = -EIO; | |
3379 | break; | |
3380 | } | |
3381 | offset += PAGE_CACHE_SIZE; | |
3382 | update_nr_written(p, wbc, 1); | |
3383 | unlock_page(p); | |
3384 | } | |
3385 | ||
3386 | if (unlikely(ret)) { | |
3387 | for (; i < num_pages; i++) { | |
3388 | struct page *p = extent_buffer_page(eb, i); | |
3389 | unlock_page(p); | |
3390 | } | |
3391 | } | |
3392 | ||
3393 | return ret; | |
3394 | } | |
3395 | ||
3396 | int btree_write_cache_pages(struct address_space *mapping, | |
3397 | struct writeback_control *wbc) | |
3398 | { | |
3399 | struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree; | |
3400 | struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info; | |
3401 | struct extent_buffer *eb, *prev_eb = NULL; | |
3402 | struct extent_page_data epd = { | |
3403 | .bio = NULL, | |
3404 | .tree = tree, | |
3405 | .extent_locked = 0, | |
3406 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, | |
de0022b9 | 3407 | .bio_flags = 0, |
0b32f4bb JB |
3408 | }; |
3409 | int ret = 0; | |
3410 | int done = 0; | |
3411 | int nr_to_write_done = 0; | |
3412 | struct pagevec pvec; | |
3413 | int nr_pages; | |
3414 | pgoff_t index; | |
3415 | pgoff_t end; /* Inclusive */ | |
3416 | int scanned = 0; | |
3417 | int tag; | |
3418 | ||
3419 | pagevec_init(&pvec, 0); | |
3420 | if (wbc->range_cyclic) { | |
3421 | index = mapping->writeback_index; /* Start from prev offset */ | |
3422 | end = -1; | |
3423 | } else { | |
3424 | index = wbc->range_start >> PAGE_CACHE_SHIFT; | |
3425 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | |
3426 | scanned = 1; | |
3427 | } | |
3428 | if (wbc->sync_mode == WB_SYNC_ALL) | |
3429 | tag = PAGECACHE_TAG_TOWRITE; | |
3430 | else | |
3431 | tag = PAGECACHE_TAG_DIRTY; | |
3432 | retry: | |
3433 | if (wbc->sync_mode == WB_SYNC_ALL) | |
3434 | tag_pages_for_writeback(mapping, index, end); | |
3435 | while (!done && !nr_to_write_done && (index <= end) && | |
3436 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, | |
3437 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { | |
3438 | unsigned i; | |
3439 | ||
3440 | scanned = 1; | |
3441 | for (i = 0; i < nr_pages; i++) { | |
3442 | struct page *page = pvec.pages[i]; | |
3443 | ||
3444 | if (!PagePrivate(page)) | |
3445 | continue; | |
3446 | ||
3447 | if (!wbc->range_cyclic && page->index > end) { | |
3448 | done = 1; | |
3449 | break; | |
3450 | } | |
3451 | ||
b5bae261 JB |
3452 | spin_lock(&mapping->private_lock); |
3453 | if (!PagePrivate(page)) { | |
3454 | spin_unlock(&mapping->private_lock); | |
3455 | continue; | |
3456 | } | |
3457 | ||
0b32f4bb | 3458 | eb = (struct extent_buffer *)page->private; |
b5bae261 JB |
3459 | |
3460 | /* | |
3461 | * Shouldn't happen and normally this would be a BUG_ON | |
3462 | * but no sense in crashing the users box for something | |
3463 | * we can survive anyway. | |
3464 | */ | |
0b32f4bb | 3465 | if (!eb) { |
b5bae261 | 3466 | spin_unlock(&mapping->private_lock); |
0b32f4bb JB |
3467 | WARN_ON(1); |
3468 | continue; | |
3469 | } | |
3470 | ||
b5bae261 JB |
3471 | if (eb == prev_eb) { |
3472 | spin_unlock(&mapping->private_lock); | |
0b32f4bb | 3473 | continue; |
b5bae261 | 3474 | } |
0b32f4bb | 3475 | |
b5bae261 JB |
3476 | ret = atomic_inc_not_zero(&eb->refs); |
3477 | spin_unlock(&mapping->private_lock); | |
3478 | if (!ret) | |
0b32f4bb | 3479 | continue; |
0b32f4bb JB |
3480 | |
3481 | prev_eb = eb; | |
3482 | ret = lock_extent_buffer_for_io(eb, fs_info, &epd); | |
3483 | if (!ret) { | |
3484 | free_extent_buffer(eb); | |
3485 | continue; | |
3486 | } | |
3487 | ||
3488 | ret = write_one_eb(eb, fs_info, wbc, &epd); | |
3489 | if (ret) { | |
3490 | done = 1; | |
3491 | free_extent_buffer(eb); | |
3492 | break; | |
3493 | } | |
3494 | free_extent_buffer(eb); | |
3495 | ||
3496 | /* | |
3497 | * the filesystem may choose to bump up nr_to_write. | |
3498 | * We have to make sure to honor the new nr_to_write | |
3499 | * at any time | |
3500 | */ | |
3501 | nr_to_write_done = wbc->nr_to_write <= 0; | |
3502 | } | |
3503 | pagevec_release(&pvec); | |
3504 | cond_resched(); | |
3505 | } | |
3506 | if (!scanned && !done) { | |
3507 | /* | |
3508 | * We hit the last page and there is more work to be done: wrap | |
3509 | * back to the start of the file | |
3510 | */ | |
3511 | scanned = 1; | |
3512 | index = 0; | |
3513 | goto retry; | |
3514 | } | |
3515 | flush_write_bio(&epd); | |
3516 | return ret; | |
3517 | } | |
3518 | ||
d1310b2e | 3519 | /** |
4bef0848 | 3520 | * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. |
d1310b2e CM |
3521 | * @mapping: address space structure to write |
3522 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write | |
3523 | * @writepage: function called for each page | |
3524 | * @data: data passed to writepage function | |
3525 | * | |
3526 | * If a page is already under I/O, write_cache_pages() skips it, even | |
3527 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, | |
3528 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() | |
3529 | * and msync() need to guarantee that all the data which was dirty at the time | |
3530 | * the call was made get new I/O started against them. If wbc->sync_mode is | |
3531 | * WB_SYNC_ALL then we were called for data integrity and we must wait for | |
3532 | * existing IO to complete. | |
3533 | */ | |
b2950863 | 3534 | static int extent_write_cache_pages(struct extent_io_tree *tree, |
4bef0848 CM |
3535 | struct address_space *mapping, |
3536 | struct writeback_control *wbc, | |
d2c3f4f6 CM |
3537 | writepage_t writepage, void *data, |
3538 | void (*flush_fn)(void *)) | |
d1310b2e | 3539 | { |
7fd1a3f7 | 3540 | struct inode *inode = mapping->host; |
d1310b2e CM |
3541 | int ret = 0; |
3542 | int done = 0; | |
f85d7d6c | 3543 | int nr_to_write_done = 0; |
d1310b2e CM |
3544 | struct pagevec pvec; |
3545 | int nr_pages; | |
3546 | pgoff_t index; | |
3547 | pgoff_t end; /* Inclusive */ | |
3548 | int scanned = 0; | |
f7aaa06b | 3549 | int tag; |
d1310b2e | 3550 | |
7fd1a3f7 JB |
3551 | /* |
3552 | * We have to hold onto the inode so that ordered extents can do their | |
3553 | * work when the IO finishes. The alternative to this is failing to add | |
3554 | * an ordered extent if the igrab() fails there and that is a huge pain | |
3555 | * to deal with, so instead just hold onto the inode throughout the | |
3556 | * writepages operation. If it fails here we are freeing up the inode | |
3557 | * anyway and we'd rather not waste our time writing out stuff that is | |
3558 | * going to be truncated anyway. | |
3559 | */ | |
3560 | if (!igrab(inode)) | |
3561 | return 0; | |
3562 | ||
d1310b2e CM |
3563 | pagevec_init(&pvec, 0); |
3564 | if (wbc->range_cyclic) { | |
3565 | index = mapping->writeback_index; /* Start from prev offset */ | |
3566 | end = -1; | |
3567 | } else { | |
3568 | index = wbc->range_start >> PAGE_CACHE_SHIFT; | |
3569 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | |
d1310b2e CM |
3570 | scanned = 1; |
3571 | } | |
f7aaa06b JB |
3572 | if (wbc->sync_mode == WB_SYNC_ALL) |
3573 | tag = PAGECACHE_TAG_TOWRITE; | |
3574 | else | |
3575 | tag = PAGECACHE_TAG_DIRTY; | |
d1310b2e | 3576 | retry: |
f7aaa06b JB |
3577 | if (wbc->sync_mode == WB_SYNC_ALL) |
3578 | tag_pages_for_writeback(mapping, index, end); | |
f85d7d6c | 3579 | while (!done && !nr_to_write_done && (index <= end) && |
f7aaa06b JB |
3580 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
3581 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) { | |
d1310b2e CM |
3582 | unsigned i; |
3583 | ||
3584 | scanned = 1; | |
3585 | for (i = 0; i < nr_pages; i++) { | |
3586 | struct page *page = pvec.pages[i]; | |
3587 | ||
3588 | /* | |
3589 | * At this point we hold neither mapping->tree_lock nor | |
3590 | * lock on the page itself: the page may be truncated or | |
3591 | * invalidated (changing page->mapping to NULL), or even | |
3592 | * swizzled back from swapper_space to tmpfs file | |
3593 | * mapping | |
3594 | */ | |
c8f2f24b JB |
3595 | if (!trylock_page(page)) { |
3596 | flush_fn(data); | |
3597 | lock_page(page); | |
01d658f2 | 3598 | } |
d1310b2e CM |
3599 | |
3600 | if (unlikely(page->mapping != mapping)) { | |
3601 | unlock_page(page); | |
3602 | continue; | |
3603 | } | |
3604 | ||
3605 | if (!wbc->range_cyclic && page->index > end) { | |
3606 | done = 1; | |
3607 | unlock_page(page); | |
3608 | continue; | |
3609 | } | |
3610 | ||
d2c3f4f6 | 3611 | if (wbc->sync_mode != WB_SYNC_NONE) { |
0e6bd956 CM |
3612 | if (PageWriteback(page)) |
3613 | flush_fn(data); | |
d1310b2e | 3614 | wait_on_page_writeback(page); |
d2c3f4f6 | 3615 | } |
d1310b2e CM |
3616 | |
3617 | if (PageWriteback(page) || | |
3618 | !clear_page_dirty_for_io(page)) { | |
3619 | unlock_page(page); | |
3620 | continue; | |
3621 | } | |
3622 | ||
3623 | ret = (*writepage)(page, wbc, data); | |
3624 | ||
3625 | if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) { | |
3626 | unlock_page(page); | |
3627 | ret = 0; | |
3628 | } | |
f85d7d6c | 3629 | if (ret) |
d1310b2e | 3630 | done = 1; |
f85d7d6c CM |
3631 | |
3632 | /* | |
3633 | * the filesystem may choose to bump up nr_to_write. | |
3634 | * We have to make sure to honor the new nr_to_write | |
3635 | * at any time | |
3636 | */ | |
3637 | nr_to_write_done = wbc->nr_to_write <= 0; | |
d1310b2e CM |
3638 | } |
3639 | pagevec_release(&pvec); | |
3640 | cond_resched(); | |
3641 | } | |
3642 | if (!scanned && !done) { | |
3643 | /* | |
3644 | * We hit the last page and there is more work to be done: wrap | |
3645 | * back to the start of the file | |
3646 | */ | |
3647 | scanned = 1; | |
3648 | index = 0; | |
3649 | goto retry; | |
3650 | } | |
7fd1a3f7 | 3651 | btrfs_add_delayed_iput(inode); |
d1310b2e CM |
3652 | return ret; |
3653 | } | |
d1310b2e | 3654 | |
ffbd517d | 3655 | static void flush_epd_write_bio(struct extent_page_data *epd) |
d2c3f4f6 | 3656 | { |
d2c3f4f6 | 3657 | if (epd->bio) { |
355808c2 JM |
3658 | int rw = WRITE; |
3659 | int ret; | |
3660 | ||
ffbd517d | 3661 | if (epd->sync_io) |
355808c2 JM |
3662 | rw = WRITE_SYNC; |
3663 | ||
de0022b9 | 3664 | ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags); |
79787eaa | 3665 | BUG_ON(ret < 0); /* -ENOMEM */ |
d2c3f4f6 CM |
3666 | epd->bio = NULL; |
3667 | } | |
3668 | } | |
3669 | ||
ffbd517d CM |
3670 | static noinline void flush_write_bio(void *data) |
3671 | { | |
3672 | struct extent_page_data *epd = data; | |
3673 | flush_epd_write_bio(epd); | |
3674 | } | |
3675 | ||
d1310b2e CM |
3676 | int extent_write_full_page(struct extent_io_tree *tree, struct page *page, |
3677 | get_extent_t *get_extent, | |
3678 | struct writeback_control *wbc) | |
3679 | { | |
3680 | int ret; | |
d1310b2e CM |
3681 | struct extent_page_data epd = { |
3682 | .bio = NULL, | |
3683 | .tree = tree, | |
3684 | .get_extent = get_extent, | |
771ed689 | 3685 | .extent_locked = 0, |
ffbd517d | 3686 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
de0022b9 | 3687 | .bio_flags = 0, |
d1310b2e | 3688 | }; |
d1310b2e | 3689 | |
d1310b2e CM |
3690 | ret = __extent_writepage(page, wbc, &epd); |
3691 | ||
ffbd517d | 3692 | flush_epd_write_bio(&epd); |
d1310b2e CM |
3693 | return ret; |
3694 | } | |
d1310b2e | 3695 | |
771ed689 CM |
3696 | int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode, |
3697 | u64 start, u64 end, get_extent_t *get_extent, | |
3698 | int mode) | |
3699 | { | |
3700 | int ret = 0; | |
3701 | struct address_space *mapping = inode->i_mapping; | |
3702 | struct page *page; | |
3703 | unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >> | |
3704 | PAGE_CACHE_SHIFT; | |
3705 | ||
3706 | struct extent_page_data epd = { | |
3707 | .bio = NULL, | |
3708 | .tree = tree, | |
3709 | .get_extent = get_extent, | |
3710 | .extent_locked = 1, | |
ffbd517d | 3711 | .sync_io = mode == WB_SYNC_ALL, |
de0022b9 | 3712 | .bio_flags = 0, |
771ed689 CM |
3713 | }; |
3714 | struct writeback_control wbc_writepages = { | |
771ed689 | 3715 | .sync_mode = mode, |
771ed689 CM |
3716 | .nr_to_write = nr_pages * 2, |
3717 | .range_start = start, | |
3718 | .range_end = end + 1, | |
3719 | }; | |
3720 | ||
d397712b | 3721 | while (start <= end) { |
771ed689 CM |
3722 | page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT); |
3723 | if (clear_page_dirty_for_io(page)) | |
3724 | ret = __extent_writepage(page, &wbc_writepages, &epd); | |
3725 | else { | |
3726 | if (tree->ops && tree->ops->writepage_end_io_hook) | |
3727 | tree->ops->writepage_end_io_hook(page, start, | |
3728 | start + PAGE_CACHE_SIZE - 1, | |
3729 | NULL, 1); | |
3730 | unlock_page(page); | |
3731 | } | |
3732 | page_cache_release(page); | |
3733 | start += PAGE_CACHE_SIZE; | |
3734 | } | |
3735 | ||
ffbd517d | 3736 | flush_epd_write_bio(&epd); |
771ed689 CM |
3737 | return ret; |
3738 | } | |
d1310b2e CM |
3739 | |
3740 | int extent_writepages(struct extent_io_tree *tree, | |
3741 | struct address_space *mapping, | |
3742 | get_extent_t *get_extent, | |
3743 | struct writeback_control *wbc) | |
3744 | { | |
3745 | int ret = 0; | |
3746 | struct extent_page_data epd = { | |
3747 | .bio = NULL, | |
3748 | .tree = tree, | |
3749 | .get_extent = get_extent, | |
771ed689 | 3750 | .extent_locked = 0, |
ffbd517d | 3751 | .sync_io = wbc->sync_mode == WB_SYNC_ALL, |
de0022b9 | 3752 | .bio_flags = 0, |
d1310b2e CM |
3753 | }; |
3754 | ||
4bef0848 | 3755 | ret = extent_write_cache_pages(tree, mapping, wbc, |
d2c3f4f6 CM |
3756 | __extent_writepage, &epd, |
3757 | flush_write_bio); | |
ffbd517d | 3758 | flush_epd_write_bio(&epd); |
d1310b2e CM |
3759 | return ret; |
3760 | } | |
d1310b2e CM |
3761 | |
3762 | int extent_readpages(struct extent_io_tree *tree, | |
3763 | struct address_space *mapping, | |
3764 | struct list_head *pages, unsigned nr_pages, | |
3765 | get_extent_t get_extent) | |
3766 | { | |
3767 | struct bio *bio = NULL; | |
3768 | unsigned page_idx; | |
c8b97818 | 3769 | unsigned long bio_flags = 0; |
67c9684f LB |
3770 | struct page *pagepool[16]; |
3771 | struct page *page; | |
3772 | int i = 0; | |
3773 | int nr = 0; | |
d1310b2e | 3774 | |
d1310b2e | 3775 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
67c9684f | 3776 | page = list_entry(pages->prev, struct page, lru); |
d1310b2e CM |
3777 | |
3778 | prefetchw(&page->flags); | |
3779 | list_del(&page->lru); | |
67c9684f | 3780 | if (add_to_page_cache_lru(page, mapping, |
43e817a1 | 3781 | page->index, GFP_NOFS)) { |
67c9684f LB |
3782 | page_cache_release(page); |
3783 | continue; | |
d1310b2e | 3784 | } |
67c9684f LB |
3785 | |
3786 | pagepool[nr++] = page; | |
3787 | if (nr < ARRAY_SIZE(pagepool)) | |
3788 | continue; | |
3789 | for (i = 0; i < nr; i++) { | |
3790 | __extent_read_full_page(tree, pagepool[i], get_extent, | |
d4c7ca86 | 3791 | &bio, 0, &bio_flags, READ); |
67c9684f LB |
3792 | page_cache_release(pagepool[i]); |
3793 | } | |
3794 | nr = 0; | |
d1310b2e | 3795 | } |
67c9684f LB |
3796 | for (i = 0; i < nr; i++) { |
3797 | __extent_read_full_page(tree, pagepool[i], get_extent, | |
d4c7ca86 | 3798 | &bio, 0, &bio_flags, READ); |
67c9684f | 3799 | page_cache_release(pagepool[i]); |
d1310b2e | 3800 | } |
67c9684f | 3801 | |
d1310b2e CM |
3802 | BUG_ON(!list_empty(pages)); |
3803 | if (bio) | |
79787eaa | 3804 | return submit_one_bio(READ, bio, 0, bio_flags); |
d1310b2e CM |
3805 | return 0; |
3806 | } | |
d1310b2e CM |
3807 | |
3808 | /* | |
3809 | * basic invalidatepage code, this waits on any locked or writeback | |
3810 | * ranges corresponding to the page, and then deletes any extent state | |
3811 | * records from the tree | |
3812 | */ | |
3813 | int extent_invalidatepage(struct extent_io_tree *tree, | |
3814 | struct page *page, unsigned long offset) | |
3815 | { | |
2ac55d41 | 3816 | struct extent_state *cached_state = NULL; |
4eee4fa4 | 3817 | u64 start = page_offset(page); |
d1310b2e CM |
3818 | u64 end = start + PAGE_CACHE_SIZE - 1; |
3819 | size_t blocksize = page->mapping->host->i_sb->s_blocksize; | |
3820 | ||
fda2832f | 3821 | start += ALIGN(offset, blocksize); |
d1310b2e CM |
3822 | if (start > end) |
3823 | return 0; | |
3824 | ||
d0082371 | 3825 | lock_extent_bits(tree, start, end, 0, &cached_state); |
1edbb734 | 3826 | wait_on_page_writeback(page); |
d1310b2e | 3827 | clear_extent_bit(tree, start, end, |
32c00aff JB |
3828 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | |
3829 | EXTENT_DO_ACCOUNTING, | |
2ac55d41 | 3830 | 1, 1, &cached_state, GFP_NOFS); |
d1310b2e CM |
3831 | return 0; |
3832 | } | |
d1310b2e | 3833 | |
7b13b7b1 CM |
3834 | /* |
3835 | * a helper for releasepage, this tests for areas of the page that | |
3836 | * are locked or under IO and drops the related state bits if it is safe | |
3837 | * to drop the page. | |
3838 | */ | |
48a3b636 ES |
3839 | static int try_release_extent_state(struct extent_map_tree *map, |
3840 | struct extent_io_tree *tree, | |
3841 | struct page *page, gfp_t mask) | |
7b13b7b1 | 3842 | { |
4eee4fa4 | 3843 | u64 start = page_offset(page); |
7b13b7b1 CM |
3844 | u64 end = start + PAGE_CACHE_SIZE - 1; |
3845 | int ret = 1; | |
3846 | ||
211f90e6 | 3847 | if (test_range_bit(tree, start, end, |
8b62b72b | 3848 | EXTENT_IOBITS, 0, NULL)) |
7b13b7b1 CM |
3849 | ret = 0; |
3850 | else { | |
3851 | if ((mask & GFP_NOFS) == GFP_NOFS) | |
3852 | mask = GFP_NOFS; | |
11ef160f CM |
3853 | /* |
3854 | * at this point we can safely clear everything except the | |
3855 | * locked bit and the nodatasum bit | |
3856 | */ | |
e3f24cc5 | 3857 | ret = clear_extent_bit(tree, start, end, |
11ef160f CM |
3858 | ~(EXTENT_LOCKED | EXTENT_NODATASUM), |
3859 | 0, 0, NULL, mask); | |
e3f24cc5 CM |
3860 | |
3861 | /* if clear_extent_bit failed for enomem reasons, | |
3862 | * we can't allow the release to continue. | |
3863 | */ | |
3864 | if (ret < 0) | |
3865 | ret = 0; | |
3866 | else | |
3867 | ret = 1; | |
7b13b7b1 CM |
3868 | } |
3869 | return ret; | |
3870 | } | |
7b13b7b1 | 3871 | |
d1310b2e CM |
3872 | /* |
3873 | * a helper for releasepage. As long as there are no locked extents | |
3874 | * in the range corresponding to the page, both state records and extent | |
3875 | * map records are removed | |
3876 | */ | |
3877 | int try_release_extent_mapping(struct extent_map_tree *map, | |
70dec807 CM |
3878 | struct extent_io_tree *tree, struct page *page, |
3879 | gfp_t mask) | |
d1310b2e CM |
3880 | { |
3881 | struct extent_map *em; | |
4eee4fa4 | 3882 | u64 start = page_offset(page); |
d1310b2e | 3883 | u64 end = start + PAGE_CACHE_SIZE - 1; |
7b13b7b1 | 3884 | |
70dec807 CM |
3885 | if ((mask & __GFP_WAIT) && |
3886 | page->mapping->host->i_size > 16 * 1024 * 1024) { | |
39b5637f | 3887 | u64 len; |
70dec807 | 3888 | while (start <= end) { |
39b5637f | 3889 | len = end - start + 1; |
890871be | 3890 | write_lock(&map->lock); |
39b5637f | 3891 | em = lookup_extent_mapping(map, start, len); |
285190d9 | 3892 | if (!em) { |
890871be | 3893 | write_unlock(&map->lock); |
70dec807 CM |
3894 | break; |
3895 | } | |
7f3c74fb CM |
3896 | if (test_bit(EXTENT_FLAG_PINNED, &em->flags) || |
3897 | em->start != start) { | |
890871be | 3898 | write_unlock(&map->lock); |
70dec807 CM |
3899 | free_extent_map(em); |
3900 | break; | |
3901 | } | |
3902 | if (!test_range_bit(tree, em->start, | |
3903 | extent_map_end(em) - 1, | |
8b62b72b | 3904 | EXTENT_LOCKED | EXTENT_WRITEBACK, |
9655d298 | 3905 | 0, NULL)) { |
70dec807 CM |
3906 | remove_extent_mapping(map, em); |
3907 | /* once for the rb tree */ | |
3908 | free_extent_map(em); | |
3909 | } | |
3910 | start = extent_map_end(em); | |
890871be | 3911 | write_unlock(&map->lock); |
70dec807 CM |
3912 | |
3913 | /* once for us */ | |
d1310b2e CM |
3914 | free_extent_map(em); |
3915 | } | |
d1310b2e | 3916 | } |
7b13b7b1 | 3917 | return try_release_extent_state(map, tree, page, mask); |
d1310b2e | 3918 | } |
d1310b2e | 3919 | |
ec29ed5b CM |
3920 | /* |
3921 | * helper function for fiemap, which doesn't want to see any holes. | |
3922 | * This maps until we find something past 'last' | |
3923 | */ | |
3924 | static struct extent_map *get_extent_skip_holes(struct inode *inode, | |
3925 | u64 offset, | |
3926 | u64 last, | |
3927 | get_extent_t *get_extent) | |
3928 | { | |
3929 | u64 sectorsize = BTRFS_I(inode)->root->sectorsize; | |
3930 | struct extent_map *em; | |
3931 | u64 len; | |
3932 | ||
3933 | if (offset >= last) | |
3934 | return NULL; | |
3935 | ||
3936 | while(1) { | |
3937 | len = last - offset; | |
3938 | if (len == 0) | |
3939 | break; | |
fda2832f | 3940 | len = ALIGN(len, sectorsize); |
ec29ed5b | 3941 | em = get_extent(inode, NULL, 0, offset, len, 0); |
c704005d | 3942 | if (IS_ERR_OR_NULL(em)) |
ec29ed5b CM |
3943 | return em; |
3944 | ||
3945 | /* if this isn't a hole return it */ | |
3946 | if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) && | |
3947 | em->block_start != EXTENT_MAP_HOLE) { | |
3948 | return em; | |
3949 | } | |
3950 | ||
3951 | /* this is a hole, advance to the next extent */ | |
3952 | offset = extent_map_end(em); | |
3953 | free_extent_map(em); | |
3954 | if (offset >= last) | |
3955 | break; | |
3956 | } | |
3957 | return NULL; | |
3958 | } | |
3959 | ||
1506fcc8 YS |
3960 | int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
3961 | __u64 start, __u64 len, get_extent_t *get_extent) | |
3962 | { | |
975f84fe | 3963 | int ret = 0; |
1506fcc8 YS |
3964 | u64 off = start; |
3965 | u64 max = start + len; | |
3966 | u32 flags = 0; | |
975f84fe JB |
3967 | u32 found_type; |
3968 | u64 last; | |
ec29ed5b | 3969 | u64 last_for_get_extent = 0; |
1506fcc8 | 3970 | u64 disko = 0; |
ec29ed5b | 3971 | u64 isize = i_size_read(inode); |
975f84fe | 3972 | struct btrfs_key found_key; |
1506fcc8 | 3973 | struct extent_map *em = NULL; |
2ac55d41 | 3974 | struct extent_state *cached_state = NULL; |
975f84fe JB |
3975 | struct btrfs_path *path; |
3976 | struct btrfs_file_extent_item *item; | |
1506fcc8 | 3977 | int end = 0; |
ec29ed5b CM |
3978 | u64 em_start = 0; |
3979 | u64 em_len = 0; | |
3980 | u64 em_end = 0; | |
1506fcc8 | 3981 | unsigned long emflags; |
1506fcc8 YS |
3982 | |
3983 | if (len == 0) | |
3984 | return -EINVAL; | |
3985 | ||
975f84fe JB |
3986 | path = btrfs_alloc_path(); |
3987 | if (!path) | |
3988 | return -ENOMEM; | |
3989 | path->leave_spinning = 1; | |
3990 | ||
4d479cf0 JB |
3991 | start = ALIGN(start, BTRFS_I(inode)->root->sectorsize); |
3992 | len = ALIGN(len, BTRFS_I(inode)->root->sectorsize); | |
3993 | ||
ec29ed5b CM |
3994 | /* |
3995 | * lookup the last file extent. We're not using i_size here | |
3996 | * because there might be preallocation past i_size | |
3997 | */ | |
975f84fe | 3998 | ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root, |
33345d01 | 3999 | path, btrfs_ino(inode), -1, 0); |
975f84fe JB |
4000 | if (ret < 0) { |
4001 | btrfs_free_path(path); | |
4002 | return ret; | |
4003 | } | |
4004 | WARN_ON(!ret); | |
4005 | path->slots[0]--; | |
4006 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
4007 | struct btrfs_file_extent_item); | |
4008 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]); | |
4009 | found_type = btrfs_key_type(&found_key); | |
4010 | ||
ec29ed5b | 4011 | /* No extents, but there might be delalloc bits */ |
33345d01 | 4012 | if (found_key.objectid != btrfs_ino(inode) || |
975f84fe | 4013 | found_type != BTRFS_EXTENT_DATA_KEY) { |
ec29ed5b CM |
4014 | /* have to trust i_size as the end */ |
4015 | last = (u64)-1; | |
4016 | last_for_get_extent = isize; | |
4017 | } else { | |
4018 | /* | |
4019 | * remember the start of the last extent. There are a | |
4020 | * bunch of different factors that go into the length of the | |
4021 | * extent, so its much less complex to remember where it started | |
4022 | */ | |
4023 | last = found_key.offset; | |
4024 | last_for_get_extent = last + 1; | |
975f84fe | 4025 | } |
975f84fe JB |
4026 | btrfs_free_path(path); |
4027 | ||
ec29ed5b CM |
4028 | /* |
4029 | * we might have some extents allocated but more delalloc past those | |
4030 | * extents. so, we trust isize unless the start of the last extent is | |
4031 | * beyond isize | |
4032 | */ | |
4033 | if (last < isize) { | |
4034 | last = (u64)-1; | |
4035 | last_for_get_extent = isize; | |
4036 | } | |
4037 | ||
a52f4cd2 | 4038 | lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0, |
d0082371 | 4039 | &cached_state); |
ec29ed5b | 4040 | |
4d479cf0 | 4041 | em = get_extent_skip_holes(inode, start, last_for_get_extent, |
ec29ed5b | 4042 | get_extent); |
1506fcc8 YS |
4043 | if (!em) |
4044 | goto out; | |
4045 | if (IS_ERR(em)) { | |
4046 | ret = PTR_ERR(em); | |
4047 | goto out; | |
4048 | } | |
975f84fe | 4049 | |
1506fcc8 | 4050 | while (!end) { |
b76bb701 | 4051 | u64 offset_in_extent = 0; |
ea8efc74 CM |
4052 | |
4053 | /* break if the extent we found is outside the range */ | |
4054 | if (em->start >= max || extent_map_end(em) < off) | |
4055 | break; | |
4056 | ||
4057 | /* | |
4058 | * get_extent may return an extent that starts before our | |
4059 | * requested range. We have to make sure the ranges | |
4060 | * we return to fiemap always move forward and don't | |
4061 | * overlap, so adjust the offsets here | |
4062 | */ | |
4063 | em_start = max(em->start, off); | |
1506fcc8 | 4064 | |
ea8efc74 CM |
4065 | /* |
4066 | * record the offset from the start of the extent | |
b76bb701 JB |
4067 | * for adjusting the disk offset below. Only do this if the |
4068 | * extent isn't compressed since our in ram offset may be past | |
4069 | * what we have actually allocated on disk. | |
ea8efc74 | 4070 | */ |
b76bb701 JB |
4071 | if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) |
4072 | offset_in_extent = em_start - em->start; | |
ec29ed5b | 4073 | em_end = extent_map_end(em); |
ea8efc74 | 4074 | em_len = em_end - em_start; |
ec29ed5b | 4075 | emflags = em->flags; |
1506fcc8 YS |
4076 | disko = 0; |
4077 | flags = 0; | |
4078 | ||
ea8efc74 CM |
4079 | /* |
4080 | * bump off for our next call to get_extent | |
4081 | */ | |
4082 | off = extent_map_end(em); | |
4083 | if (off >= max) | |
4084 | end = 1; | |
4085 | ||
93dbfad7 | 4086 | if (em->block_start == EXTENT_MAP_LAST_BYTE) { |
1506fcc8 YS |
4087 | end = 1; |
4088 | flags |= FIEMAP_EXTENT_LAST; | |
93dbfad7 | 4089 | } else if (em->block_start == EXTENT_MAP_INLINE) { |
1506fcc8 YS |
4090 | flags |= (FIEMAP_EXTENT_DATA_INLINE | |
4091 | FIEMAP_EXTENT_NOT_ALIGNED); | |
93dbfad7 | 4092 | } else if (em->block_start == EXTENT_MAP_DELALLOC) { |
1506fcc8 YS |
4093 | flags |= (FIEMAP_EXTENT_DELALLOC | |
4094 | FIEMAP_EXTENT_UNKNOWN); | |
93dbfad7 | 4095 | } else { |
ea8efc74 | 4096 | disko = em->block_start + offset_in_extent; |
1506fcc8 YS |
4097 | } |
4098 | if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) | |
4099 | flags |= FIEMAP_EXTENT_ENCODED; | |
4100 | ||
1506fcc8 YS |
4101 | free_extent_map(em); |
4102 | em = NULL; | |
ec29ed5b CM |
4103 | if ((em_start >= last) || em_len == (u64)-1 || |
4104 | (last == (u64)-1 && isize <= em_end)) { | |
1506fcc8 YS |
4105 | flags |= FIEMAP_EXTENT_LAST; |
4106 | end = 1; | |
4107 | } | |
4108 | ||
ec29ed5b CM |
4109 | /* now scan forward to see if this is really the last extent. */ |
4110 | em = get_extent_skip_holes(inode, off, last_for_get_extent, | |
4111 | get_extent); | |
4112 | if (IS_ERR(em)) { | |
4113 | ret = PTR_ERR(em); | |
4114 | goto out; | |
4115 | } | |
4116 | if (!em) { | |
975f84fe JB |
4117 | flags |= FIEMAP_EXTENT_LAST; |
4118 | end = 1; | |
4119 | } | |
ec29ed5b CM |
4120 | ret = fiemap_fill_next_extent(fieinfo, em_start, disko, |
4121 | em_len, flags); | |
4122 | if (ret) | |
4123 | goto out_free; | |
1506fcc8 YS |
4124 | } |
4125 | out_free: | |
4126 | free_extent_map(em); | |
4127 | out: | |
a52f4cd2 | 4128 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1, |
2ac55d41 | 4129 | &cached_state, GFP_NOFS); |
1506fcc8 YS |
4130 | return ret; |
4131 | } | |
4132 | ||
727011e0 CM |
4133 | static void __free_extent_buffer(struct extent_buffer *eb) |
4134 | { | |
6d49ba1b | 4135 | btrfs_leak_debug_del(&eb->leak_list); |
727011e0 CM |
4136 | kmem_cache_free(extent_buffer_cache, eb); |
4137 | } | |
4138 | ||
d1310b2e CM |
4139 | static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree, |
4140 | u64 start, | |
4141 | unsigned long len, | |
4142 | gfp_t mask) | |
4143 | { | |
4144 | struct extent_buffer *eb = NULL; | |
4145 | ||
d1310b2e | 4146 | eb = kmem_cache_zalloc(extent_buffer_cache, mask); |
91ca338d TI |
4147 | if (eb == NULL) |
4148 | return NULL; | |
d1310b2e CM |
4149 | eb->start = start; |
4150 | eb->len = len; | |
4f2de97a | 4151 | eb->tree = tree; |
815a51c7 | 4152 | eb->bflags = 0; |
bd681513 CM |
4153 | rwlock_init(&eb->lock); |
4154 | atomic_set(&eb->write_locks, 0); | |
4155 | atomic_set(&eb->read_locks, 0); | |
4156 | atomic_set(&eb->blocking_readers, 0); | |
4157 | atomic_set(&eb->blocking_writers, 0); | |
4158 | atomic_set(&eb->spinning_readers, 0); | |
4159 | atomic_set(&eb->spinning_writers, 0); | |
5b25f70f | 4160 | eb->lock_nested = 0; |
bd681513 CM |
4161 | init_waitqueue_head(&eb->write_lock_wq); |
4162 | init_waitqueue_head(&eb->read_lock_wq); | |
b4ce94de | 4163 | |
6d49ba1b ES |
4164 | btrfs_leak_debug_add(&eb->leak_list, &buffers); |
4165 | ||
3083ee2e | 4166 | spin_lock_init(&eb->refs_lock); |
d1310b2e | 4167 | atomic_set(&eb->refs, 1); |
0b32f4bb | 4168 | atomic_set(&eb->io_pages, 0); |
727011e0 | 4169 | |
b8dae313 DS |
4170 | /* |
4171 | * Sanity checks, currently the maximum is 64k covered by 16x 4k pages | |
4172 | */ | |
4173 | BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE | |
4174 | > MAX_INLINE_EXTENT_BUFFER_SIZE); | |
4175 | BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE); | |
d1310b2e CM |
4176 | |
4177 | return eb; | |
4178 | } | |
4179 | ||
815a51c7 JS |
4180 | struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src) |
4181 | { | |
4182 | unsigned long i; | |
4183 | struct page *p; | |
4184 | struct extent_buffer *new; | |
4185 | unsigned long num_pages = num_extent_pages(src->start, src->len); | |
4186 | ||
4187 | new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC); | |
4188 | if (new == NULL) | |
4189 | return NULL; | |
4190 | ||
4191 | for (i = 0; i < num_pages; i++) { | |
4192 | p = alloc_page(GFP_ATOMIC); | |
4193 | BUG_ON(!p); | |
4194 | attach_extent_buffer_page(new, p); | |
4195 | WARN_ON(PageDirty(p)); | |
4196 | SetPageUptodate(p); | |
4197 | new->pages[i] = p; | |
4198 | } | |
4199 | ||
4200 | copy_extent_buffer(new, src, 0, 0, src->len); | |
4201 | set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags); | |
4202 | set_bit(EXTENT_BUFFER_DUMMY, &new->bflags); | |
4203 | ||
4204 | return new; | |
4205 | } | |
4206 | ||
4207 | struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len) | |
4208 | { | |
4209 | struct extent_buffer *eb; | |
4210 | unsigned long num_pages = num_extent_pages(0, len); | |
4211 | unsigned long i; | |
4212 | ||
4213 | eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC); | |
4214 | if (!eb) | |
4215 | return NULL; | |
4216 | ||
4217 | for (i = 0; i < num_pages; i++) { | |
4218 | eb->pages[i] = alloc_page(GFP_ATOMIC); | |
4219 | if (!eb->pages[i]) | |
4220 | goto err; | |
4221 | } | |
4222 | set_extent_buffer_uptodate(eb); | |
4223 | btrfs_set_header_nritems(eb, 0); | |
4224 | set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); | |
4225 | ||
4226 | return eb; | |
4227 | err: | |
84167d19 SB |
4228 | for (; i > 0; i--) |
4229 | __free_page(eb->pages[i - 1]); | |
815a51c7 JS |
4230 | __free_extent_buffer(eb); |
4231 | return NULL; | |
4232 | } | |
4233 | ||
0b32f4bb | 4234 | static int extent_buffer_under_io(struct extent_buffer *eb) |
d1310b2e | 4235 | { |
0b32f4bb JB |
4236 | return (atomic_read(&eb->io_pages) || |
4237 | test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) || | |
4238 | test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); | |
d1310b2e CM |
4239 | } |
4240 | ||
897ca6e9 MX |
4241 | /* |
4242 | * Helper for releasing extent buffer page. | |
4243 | */ | |
4244 | static void btrfs_release_extent_buffer_page(struct extent_buffer *eb, | |
4245 | unsigned long start_idx) | |
4246 | { | |
4247 | unsigned long index; | |
39bab87b | 4248 | unsigned long num_pages; |
897ca6e9 | 4249 | struct page *page; |
815a51c7 | 4250 | int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags); |
897ca6e9 | 4251 | |
0b32f4bb | 4252 | BUG_ON(extent_buffer_under_io(eb)); |
897ca6e9 | 4253 | |
39bab87b WSH |
4254 | num_pages = num_extent_pages(eb->start, eb->len); |
4255 | index = start_idx + num_pages; | |
897ca6e9 MX |
4256 | if (start_idx >= index) |
4257 | return; | |
4258 | ||
4259 | do { | |
4260 | index--; | |
4261 | page = extent_buffer_page(eb, index); | |
815a51c7 | 4262 | if (page && mapped) { |
4f2de97a JB |
4263 | spin_lock(&page->mapping->private_lock); |
4264 | /* | |
4265 | * We do this since we'll remove the pages after we've | |
4266 | * removed the eb from the radix tree, so we could race | |
4267 | * and have this page now attached to the new eb. So | |
4268 | * only clear page_private if it's still connected to | |
4269 | * this eb. | |
4270 | */ | |
4271 | if (PagePrivate(page) && | |
4272 | page->private == (unsigned long)eb) { | |
0b32f4bb | 4273 | BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)); |
3083ee2e JB |
4274 | BUG_ON(PageDirty(page)); |
4275 | BUG_ON(PageWriteback(page)); | |
4f2de97a JB |
4276 | /* |
4277 | * We need to make sure we haven't be attached | |
4278 | * to a new eb. | |
4279 | */ | |
4280 | ClearPagePrivate(page); | |
4281 | set_page_private(page, 0); | |
4282 | /* One for the page private */ | |
4283 | page_cache_release(page); | |
4284 | } | |
4285 | spin_unlock(&page->mapping->private_lock); | |
4286 | ||
815a51c7 JS |
4287 | } |
4288 | if (page) { | |
4f2de97a | 4289 | /* One for when we alloced the page */ |
897ca6e9 | 4290 | page_cache_release(page); |
4f2de97a | 4291 | } |
897ca6e9 MX |
4292 | } while (index != start_idx); |
4293 | } | |
4294 | ||
4295 | /* | |
4296 | * Helper for releasing the extent buffer. | |
4297 | */ | |
4298 | static inline void btrfs_release_extent_buffer(struct extent_buffer *eb) | |
4299 | { | |
4300 | btrfs_release_extent_buffer_page(eb, 0); | |
4301 | __free_extent_buffer(eb); | |
4302 | } | |
4303 | ||
0b32f4bb JB |
4304 | static void check_buffer_tree_ref(struct extent_buffer *eb) |
4305 | { | |
242e18c7 | 4306 | int refs; |
0b32f4bb JB |
4307 | /* the ref bit is tricky. We have to make sure it is set |
4308 | * if we have the buffer dirty. Otherwise the | |
4309 | * code to free a buffer can end up dropping a dirty | |
4310 | * page | |
4311 | * | |
4312 | * Once the ref bit is set, it won't go away while the | |
4313 | * buffer is dirty or in writeback, and it also won't | |
4314 | * go away while we have the reference count on the | |
4315 | * eb bumped. | |
4316 | * | |
4317 | * We can't just set the ref bit without bumping the | |
4318 | * ref on the eb because free_extent_buffer might | |
4319 | * see the ref bit and try to clear it. If this happens | |
4320 | * free_extent_buffer might end up dropping our original | |
4321 | * ref by mistake and freeing the page before we are able | |
4322 | * to add one more ref. | |
4323 | * | |
4324 | * So bump the ref count first, then set the bit. If someone | |
4325 | * beat us to it, drop the ref we added. | |
4326 | */ | |
242e18c7 CM |
4327 | refs = atomic_read(&eb->refs); |
4328 | if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) | |
4329 | return; | |
4330 | ||
594831c4 JB |
4331 | spin_lock(&eb->refs_lock); |
4332 | if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) | |
0b32f4bb | 4333 | atomic_inc(&eb->refs); |
594831c4 | 4334 | spin_unlock(&eb->refs_lock); |
0b32f4bb JB |
4335 | } |
4336 | ||
5df4235e JB |
4337 | static void mark_extent_buffer_accessed(struct extent_buffer *eb) |
4338 | { | |
4339 | unsigned long num_pages, i; | |
4340 | ||
0b32f4bb JB |
4341 | check_buffer_tree_ref(eb); |
4342 | ||
5df4235e JB |
4343 | num_pages = num_extent_pages(eb->start, eb->len); |
4344 | for (i = 0; i < num_pages; i++) { | |
4345 | struct page *p = extent_buffer_page(eb, i); | |
4346 | mark_page_accessed(p); | |
4347 | } | |
4348 | } | |
4349 | ||
d1310b2e | 4350 | struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree, |
727011e0 | 4351 | u64 start, unsigned long len) |
d1310b2e CM |
4352 | { |
4353 | unsigned long num_pages = num_extent_pages(start, len); | |
4354 | unsigned long i; | |
4355 | unsigned long index = start >> PAGE_CACHE_SHIFT; | |
4356 | struct extent_buffer *eb; | |
6af118ce | 4357 | struct extent_buffer *exists = NULL; |
d1310b2e CM |
4358 | struct page *p; |
4359 | struct address_space *mapping = tree->mapping; | |
4360 | int uptodate = 1; | |
19fe0a8b | 4361 | int ret; |
d1310b2e | 4362 | |
19fe0a8b MX |
4363 | rcu_read_lock(); |
4364 | eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT); | |
4365 | if (eb && atomic_inc_not_zero(&eb->refs)) { | |
4366 | rcu_read_unlock(); | |
5df4235e | 4367 | mark_extent_buffer_accessed(eb); |
6af118ce CM |
4368 | return eb; |
4369 | } | |
19fe0a8b | 4370 | rcu_read_unlock(); |
6af118ce | 4371 | |
ba144192 | 4372 | eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS); |
2b114d1d | 4373 | if (!eb) |
d1310b2e CM |
4374 | return NULL; |
4375 | ||
727011e0 | 4376 | for (i = 0; i < num_pages; i++, index++) { |
a6591715 | 4377 | p = find_or_create_page(mapping, index, GFP_NOFS); |
4804b382 | 4378 | if (!p) |
6af118ce | 4379 | goto free_eb; |
4f2de97a JB |
4380 | |
4381 | spin_lock(&mapping->private_lock); | |
4382 | if (PagePrivate(p)) { | |
4383 | /* | |
4384 | * We could have already allocated an eb for this page | |
4385 | * and attached one so lets see if we can get a ref on | |
4386 | * the existing eb, and if we can we know it's good and | |
4387 | * we can just return that one, else we know we can just | |
4388 | * overwrite page->private. | |
4389 | */ | |
4390 | exists = (struct extent_buffer *)p->private; | |
4391 | if (atomic_inc_not_zero(&exists->refs)) { | |
4392 | spin_unlock(&mapping->private_lock); | |
4393 | unlock_page(p); | |
17de39ac | 4394 | page_cache_release(p); |
5df4235e | 4395 | mark_extent_buffer_accessed(exists); |
4f2de97a JB |
4396 | goto free_eb; |
4397 | } | |
4398 | ||
0b32f4bb | 4399 | /* |
4f2de97a JB |
4400 | * Do this so attach doesn't complain and we need to |
4401 | * drop the ref the old guy had. | |
4402 | */ | |
4403 | ClearPagePrivate(p); | |
0b32f4bb | 4404 | WARN_ON(PageDirty(p)); |
4f2de97a | 4405 | page_cache_release(p); |
d1310b2e | 4406 | } |
4f2de97a JB |
4407 | attach_extent_buffer_page(eb, p); |
4408 | spin_unlock(&mapping->private_lock); | |
0b32f4bb | 4409 | WARN_ON(PageDirty(p)); |
d1310b2e | 4410 | mark_page_accessed(p); |
727011e0 | 4411 | eb->pages[i] = p; |
d1310b2e CM |
4412 | if (!PageUptodate(p)) |
4413 | uptodate = 0; | |
eb14ab8e CM |
4414 | |
4415 | /* | |
4416 | * see below about how we avoid a nasty race with release page | |
4417 | * and why we unlock later | |
4418 | */ | |
d1310b2e CM |
4419 | } |
4420 | if (uptodate) | |
b4ce94de | 4421 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
115391d2 | 4422 | again: |
19fe0a8b MX |
4423 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
4424 | if (ret) | |
4425 | goto free_eb; | |
4426 | ||
6af118ce | 4427 | spin_lock(&tree->buffer_lock); |
19fe0a8b MX |
4428 | ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb); |
4429 | if (ret == -EEXIST) { | |
4430 | exists = radix_tree_lookup(&tree->buffer, | |
4431 | start >> PAGE_CACHE_SHIFT); | |
115391d2 JB |
4432 | if (!atomic_inc_not_zero(&exists->refs)) { |
4433 | spin_unlock(&tree->buffer_lock); | |
4434 | radix_tree_preload_end(); | |
115391d2 JB |
4435 | exists = NULL; |
4436 | goto again; | |
4437 | } | |
6af118ce | 4438 | spin_unlock(&tree->buffer_lock); |
19fe0a8b | 4439 | radix_tree_preload_end(); |
5df4235e | 4440 | mark_extent_buffer_accessed(exists); |
6af118ce CM |
4441 | goto free_eb; |
4442 | } | |
6af118ce | 4443 | /* add one reference for the tree */ |
0b32f4bb | 4444 | check_buffer_tree_ref(eb); |
f044ba78 | 4445 | spin_unlock(&tree->buffer_lock); |
19fe0a8b | 4446 | radix_tree_preload_end(); |
eb14ab8e CM |
4447 | |
4448 | /* | |
4449 | * there is a race where release page may have | |
4450 | * tried to find this extent buffer in the radix | |
4451 | * but failed. It will tell the VM it is safe to | |
4452 | * reclaim the, and it will clear the page private bit. | |
4453 | * We must make sure to set the page private bit properly | |
4454 | * after the extent buffer is in the radix tree so | |
4455 | * it doesn't get lost | |
4456 | */ | |
727011e0 CM |
4457 | SetPageChecked(eb->pages[0]); |
4458 | for (i = 1; i < num_pages; i++) { | |
4459 | p = extent_buffer_page(eb, i); | |
727011e0 CM |
4460 | ClearPageChecked(p); |
4461 | unlock_page(p); | |
4462 | } | |
4463 | unlock_page(eb->pages[0]); | |
d1310b2e CM |
4464 | return eb; |
4465 | ||
6af118ce | 4466 | free_eb: |
727011e0 CM |
4467 | for (i = 0; i < num_pages; i++) { |
4468 | if (eb->pages[i]) | |
4469 | unlock_page(eb->pages[i]); | |
4470 | } | |
eb14ab8e | 4471 | |
17de39ac | 4472 | WARN_ON(!atomic_dec_and_test(&eb->refs)); |
897ca6e9 | 4473 | btrfs_release_extent_buffer(eb); |
6af118ce | 4474 | return exists; |
d1310b2e | 4475 | } |
d1310b2e CM |
4476 | |
4477 | struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree, | |
f09d1f60 | 4478 | u64 start, unsigned long len) |
d1310b2e | 4479 | { |
d1310b2e | 4480 | struct extent_buffer *eb; |
d1310b2e | 4481 | |
19fe0a8b MX |
4482 | rcu_read_lock(); |
4483 | eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT); | |
4484 | if (eb && atomic_inc_not_zero(&eb->refs)) { | |
4485 | rcu_read_unlock(); | |
5df4235e | 4486 | mark_extent_buffer_accessed(eb); |
19fe0a8b MX |
4487 | return eb; |
4488 | } | |
4489 | rcu_read_unlock(); | |
0f9dd46c | 4490 | |
19fe0a8b | 4491 | return NULL; |
d1310b2e | 4492 | } |
d1310b2e | 4493 | |
3083ee2e JB |
4494 | static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head) |
4495 | { | |
4496 | struct extent_buffer *eb = | |
4497 | container_of(head, struct extent_buffer, rcu_head); | |
4498 | ||
4499 | __free_extent_buffer(eb); | |
4500 | } | |
4501 | ||
3083ee2e | 4502 | /* Expects to have eb->eb_lock already held */ |
f7a52a40 | 4503 | static int release_extent_buffer(struct extent_buffer *eb) |
3083ee2e JB |
4504 | { |
4505 | WARN_ON(atomic_read(&eb->refs) == 0); | |
4506 | if (atomic_dec_and_test(&eb->refs)) { | |
815a51c7 JS |
4507 | if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) { |
4508 | spin_unlock(&eb->refs_lock); | |
4509 | } else { | |
4510 | struct extent_io_tree *tree = eb->tree; | |
3083ee2e | 4511 | |
815a51c7 | 4512 | spin_unlock(&eb->refs_lock); |
3083ee2e | 4513 | |
815a51c7 JS |
4514 | spin_lock(&tree->buffer_lock); |
4515 | radix_tree_delete(&tree->buffer, | |
4516 | eb->start >> PAGE_CACHE_SHIFT); | |
4517 | spin_unlock(&tree->buffer_lock); | |
4518 | } | |
3083ee2e JB |
4519 | |
4520 | /* Should be safe to release our pages at this point */ | |
4521 | btrfs_release_extent_buffer_page(eb, 0); | |
3083ee2e | 4522 | call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu); |
e64860aa | 4523 | return 1; |
3083ee2e JB |
4524 | } |
4525 | spin_unlock(&eb->refs_lock); | |
e64860aa JB |
4526 | |
4527 | return 0; | |
3083ee2e JB |
4528 | } |
4529 | ||
d1310b2e CM |
4530 | void free_extent_buffer(struct extent_buffer *eb) |
4531 | { | |
242e18c7 CM |
4532 | int refs; |
4533 | int old; | |
d1310b2e CM |
4534 | if (!eb) |
4535 | return; | |
4536 | ||
242e18c7 CM |
4537 | while (1) { |
4538 | refs = atomic_read(&eb->refs); | |
4539 | if (refs <= 3) | |
4540 | break; | |
4541 | old = atomic_cmpxchg(&eb->refs, refs, refs - 1); | |
4542 | if (old == refs) | |
4543 | return; | |
4544 | } | |
4545 | ||
3083ee2e | 4546 | spin_lock(&eb->refs_lock); |
815a51c7 JS |
4547 | if (atomic_read(&eb->refs) == 2 && |
4548 | test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) | |
4549 | atomic_dec(&eb->refs); | |
4550 | ||
3083ee2e JB |
4551 | if (atomic_read(&eb->refs) == 2 && |
4552 | test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && | |
0b32f4bb | 4553 | !extent_buffer_under_io(eb) && |
3083ee2e JB |
4554 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
4555 | atomic_dec(&eb->refs); | |
4556 | ||
4557 | /* | |
4558 | * I know this is terrible, but it's temporary until we stop tracking | |
4559 | * the uptodate bits and such for the extent buffers. | |
4560 | */ | |
f7a52a40 | 4561 | release_extent_buffer(eb); |
3083ee2e JB |
4562 | } |
4563 | ||
4564 | void free_extent_buffer_stale(struct extent_buffer *eb) | |
4565 | { | |
4566 | if (!eb) | |
d1310b2e CM |
4567 | return; |
4568 | ||
3083ee2e JB |
4569 | spin_lock(&eb->refs_lock); |
4570 | set_bit(EXTENT_BUFFER_STALE, &eb->bflags); | |
4571 | ||
0b32f4bb | 4572 | if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) && |
3083ee2e JB |
4573 | test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) |
4574 | atomic_dec(&eb->refs); | |
f7a52a40 | 4575 | release_extent_buffer(eb); |
d1310b2e | 4576 | } |
d1310b2e | 4577 | |
1d4284bd | 4578 | void clear_extent_buffer_dirty(struct extent_buffer *eb) |
d1310b2e | 4579 | { |
d1310b2e CM |
4580 | unsigned long i; |
4581 | unsigned long num_pages; | |
4582 | struct page *page; | |
4583 | ||
d1310b2e CM |
4584 | num_pages = num_extent_pages(eb->start, eb->len); |
4585 | ||
4586 | for (i = 0; i < num_pages; i++) { | |
4587 | page = extent_buffer_page(eb, i); | |
b9473439 | 4588 | if (!PageDirty(page)) |
d2c3f4f6 CM |
4589 | continue; |
4590 | ||
a61e6f29 | 4591 | lock_page(page); |
eb14ab8e CM |
4592 | WARN_ON(!PagePrivate(page)); |
4593 | ||
d1310b2e | 4594 | clear_page_dirty_for_io(page); |
0ee0fda0 | 4595 | spin_lock_irq(&page->mapping->tree_lock); |
d1310b2e CM |
4596 | if (!PageDirty(page)) { |
4597 | radix_tree_tag_clear(&page->mapping->page_tree, | |
4598 | page_index(page), | |
4599 | PAGECACHE_TAG_DIRTY); | |
4600 | } | |
0ee0fda0 | 4601 | spin_unlock_irq(&page->mapping->tree_lock); |
bf0da8c1 | 4602 | ClearPageError(page); |
a61e6f29 | 4603 | unlock_page(page); |
d1310b2e | 4604 | } |
0b32f4bb | 4605 | WARN_ON(atomic_read(&eb->refs) == 0); |
d1310b2e | 4606 | } |
d1310b2e | 4607 | |
0b32f4bb | 4608 | int set_extent_buffer_dirty(struct extent_buffer *eb) |
d1310b2e CM |
4609 | { |
4610 | unsigned long i; | |
4611 | unsigned long num_pages; | |
b9473439 | 4612 | int was_dirty = 0; |
d1310b2e | 4613 | |
0b32f4bb JB |
4614 | check_buffer_tree_ref(eb); |
4615 | ||
b9473439 | 4616 | was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); |
0b32f4bb | 4617 | |
d1310b2e | 4618 | num_pages = num_extent_pages(eb->start, eb->len); |
3083ee2e | 4619 | WARN_ON(atomic_read(&eb->refs) == 0); |
0b32f4bb JB |
4620 | WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)); |
4621 | ||
b9473439 | 4622 | for (i = 0; i < num_pages; i++) |
0b32f4bb | 4623 | set_page_dirty(extent_buffer_page(eb, i)); |
b9473439 | 4624 | return was_dirty; |
d1310b2e | 4625 | } |
d1310b2e | 4626 | |
0b32f4bb | 4627 | int clear_extent_buffer_uptodate(struct extent_buffer *eb) |
1259ab75 CM |
4628 | { |
4629 | unsigned long i; | |
4630 | struct page *page; | |
4631 | unsigned long num_pages; | |
4632 | ||
b4ce94de | 4633 | clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
0b32f4bb | 4634 | num_pages = num_extent_pages(eb->start, eb->len); |
1259ab75 CM |
4635 | for (i = 0; i < num_pages; i++) { |
4636 | page = extent_buffer_page(eb, i); | |
33958dc6 CM |
4637 | if (page) |
4638 | ClearPageUptodate(page); | |
1259ab75 CM |
4639 | } |
4640 | return 0; | |
4641 | } | |
4642 | ||
0b32f4bb | 4643 | int set_extent_buffer_uptodate(struct extent_buffer *eb) |
d1310b2e CM |
4644 | { |
4645 | unsigned long i; | |
4646 | struct page *page; | |
4647 | unsigned long num_pages; | |
4648 | ||
0b32f4bb | 4649 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
d1310b2e | 4650 | num_pages = num_extent_pages(eb->start, eb->len); |
d1310b2e CM |
4651 | for (i = 0; i < num_pages; i++) { |
4652 | page = extent_buffer_page(eb, i); | |
d1310b2e CM |
4653 | SetPageUptodate(page); |
4654 | } | |
4655 | return 0; | |
4656 | } | |
d1310b2e | 4657 | |
0b32f4bb | 4658 | int extent_buffer_uptodate(struct extent_buffer *eb) |
d1310b2e | 4659 | { |
0b32f4bb | 4660 | return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
d1310b2e | 4661 | } |
d1310b2e CM |
4662 | |
4663 | int read_extent_buffer_pages(struct extent_io_tree *tree, | |
bb82ab88 | 4664 | struct extent_buffer *eb, u64 start, int wait, |
f188591e | 4665 | get_extent_t *get_extent, int mirror_num) |
d1310b2e CM |
4666 | { |
4667 | unsigned long i; | |
4668 | unsigned long start_i; | |
4669 | struct page *page; | |
4670 | int err; | |
4671 | int ret = 0; | |
ce9adaa5 CM |
4672 | int locked_pages = 0; |
4673 | int all_uptodate = 1; | |
d1310b2e | 4674 | unsigned long num_pages; |
727011e0 | 4675 | unsigned long num_reads = 0; |
a86c12c7 | 4676 | struct bio *bio = NULL; |
c8b97818 | 4677 | unsigned long bio_flags = 0; |
a86c12c7 | 4678 | |
b4ce94de | 4679 | if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) |
d1310b2e CM |
4680 | return 0; |
4681 | ||
d1310b2e CM |
4682 | if (start) { |
4683 | WARN_ON(start < eb->start); | |
4684 | start_i = (start >> PAGE_CACHE_SHIFT) - | |
4685 | (eb->start >> PAGE_CACHE_SHIFT); | |
4686 | } else { | |
4687 | start_i = 0; | |
4688 | } | |
4689 | ||
4690 | num_pages = num_extent_pages(eb->start, eb->len); | |
4691 | for (i = start_i; i < num_pages; i++) { | |
4692 | page = extent_buffer_page(eb, i); | |
bb82ab88 | 4693 | if (wait == WAIT_NONE) { |
2db04966 | 4694 | if (!trylock_page(page)) |
ce9adaa5 | 4695 | goto unlock_exit; |
d1310b2e CM |
4696 | } else { |
4697 | lock_page(page); | |
4698 | } | |
ce9adaa5 | 4699 | locked_pages++; |
727011e0 CM |
4700 | if (!PageUptodate(page)) { |
4701 | num_reads++; | |
ce9adaa5 | 4702 | all_uptodate = 0; |
727011e0 | 4703 | } |
ce9adaa5 CM |
4704 | } |
4705 | if (all_uptodate) { | |
4706 | if (start_i == 0) | |
b4ce94de | 4707 | set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); |
ce9adaa5 CM |
4708 | goto unlock_exit; |
4709 | } | |
4710 | ||
ea466794 | 4711 | clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags); |
5cf1ab56 | 4712 | eb->read_mirror = 0; |
0b32f4bb | 4713 | atomic_set(&eb->io_pages, num_reads); |
ce9adaa5 CM |
4714 | for (i = start_i; i < num_pages; i++) { |
4715 | page = extent_buffer_page(eb, i); | |
ce9adaa5 | 4716 | if (!PageUptodate(page)) { |
f188591e | 4717 | ClearPageError(page); |
a86c12c7 | 4718 | err = __extent_read_full_page(tree, page, |
f188591e | 4719 | get_extent, &bio, |
d4c7ca86 JB |
4720 | mirror_num, &bio_flags, |
4721 | READ | REQ_META); | |
d397712b | 4722 | if (err) |
d1310b2e | 4723 | ret = err; |
d1310b2e CM |
4724 | } else { |
4725 | unlock_page(page); | |
4726 | } | |
4727 | } | |
4728 | ||
355808c2 | 4729 | if (bio) { |
d4c7ca86 JB |
4730 | err = submit_one_bio(READ | REQ_META, bio, mirror_num, |
4731 | bio_flags); | |
79787eaa JM |
4732 | if (err) |
4733 | return err; | |
355808c2 | 4734 | } |
a86c12c7 | 4735 | |
bb82ab88 | 4736 | if (ret || wait != WAIT_COMPLETE) |
d1310b2e | 4737 | return ret; |
d397712b | 4738 | |
d1310b2e CM |
4739 | for (i = start_i; i < num_pages; i++) { |
4740 | page = extent_buffer_page(eb, i); | |
4741 | wait_on_page_locked(page); | |
d397712b | 4742 | if (!PageUptodate(page)) |
d1310b2e | 4743 | ret = -EIO; |
d1310b2e | 4744 | } |
d397712b | 4745 | |
d1310b2e | 4746 | return ret; |
ce9adaa5 CM |
4747 | |
4748 | unlock_exit: | |
4749 | i = start_i; | |
d397712b | 4750 | while (locked_pages > 0) { |
ce9adaa5 CM |
4751 | page = extent_buffer_page(eb, i); |
4752 | i++; | |
4753 | unlock_page(page); | |
4754 | locked_pages--; | |
4755 | } | |
4756 | return ret; | |
d1310b2e | 4757 | } |
d1310b2e CM |
4758 | |
4759 | void read_extent_buffer(struct extent_buffer *eb, void *dstv, | |
4760 | unsigned long start, | |
4761 | unsigned long len) | |
4762 | { | |
4763 | size_t cur; | |
4764 | size_t offset; | |
4765 | struct page *page; | |
4766 | char *kaddr; | |
4767 | char *dst = (char *)dstv; | |
4768 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); | |
4769 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; | |
d1310b2e CM |
4770 | |
4771 | WARN_ON(start > eb->len); | |
4772 | WARN_ON(start + len > eb->start + eb->len); | |
4773 | ||
4774 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); | |
4775 | ||
d397712b | 4776 | while (len > 0) { |
d1310b2e | 4777 | page = extent_buffer_page(eb, i); |
d1310b2e CM |
4778 | |
4779 | cur = min(len, (PAGE_CACHE_SIZE - offset)); | |
a6591715 | 4780 | kaddr = page_address(page); |
d1310b2e | 4781 | memcpy(dst, kaddr + offset, cur); |
d1310b2e CM |
4782 | |
4783 | dst += cur; | |
4784 | len -= cur; | |
4785 | offset = 0; | |
4786 | i++; | |
4787 | } | |
4788 | } | |
d1310b2e CM |
4789 | |
4790 | int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start, | |
a6591715 | 4791 | unsigned long min_len, char **map, |
d1310b2e | 4792 | unsigned long *map_start, |
a6591715 | 4793 | unsigned long *map_len) |
d1310b2e CM |
4794 | { |
4795 | size_t offset = start & (PAGE_CACHE_SIZE - 1); | |
4796 | char *kaddr; | |
4797 | struct page *p; | |
4798 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); | |
4799 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; | |
4800 | unsigned long end_i = (start_offset + start + min_len - 1) >> | |
4801 | PAGE_CACHE_SHIFT; | |
4802 | ||
4803 | if (i != end_i) | |
4804 | return -EINVAL; | |
4805 | ||
4806 | if (i == 0) { | |
4807 | offset = start_offset; | |
4808 | *map_start = 0; | |
4809 | } else { | |
4810 | offset = 0; | |
4811 | *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset; | |
4812 | } | |
d397712b | 4813 | |
d1310b2e | 4814 | if (start + min_len > eb->len) { |
31b1a2bd | 4815 | WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, " |
d397712b CM |
4816 | "wanted %lu %lu\n", (unsigned long long)eb->start, |
4817 | eb->len, start, min_len); | |
85026533 | 4818 | return -EINVAL; |
d1310b2e CM |
4819 | } |
4820 | ||
4821 | p = extent_buffer_page(eb, i); | |
a6591715 | 4822 | kaddr = page_address(p); |
d1310b2e CM |
4823 | *map = kaddr + offset; |
4824 | *map_len = PAGE_CACHE_SIZE - offset; | |
4825 | return 0; | |
4826 | } | |
d1310b2e | 4827 | |
d1310b2e CM |
4828 | int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv, |
4829 | unsigned long start, | |
4830 | unsigned long len) | |
4831 | { | |
4832 | size_t cur; | |
4833 | size_t offset; | |
4834 | struct page *page; | |
4835 | char *kaddr; | |
4836 | char *ptr = (char *)ptrv; | |
4837 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); | |
4838 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; | |
4839 | int ret = 0; | |
4840 | ||
4841 | WARN_ON(start > eb->len); | |
4842 | WARN_ON(start + len > eb->start + eb->len); | |
4843 | ||
4844 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); | |
4845 | ||
d397712b | 4846 | while (len > 0) { |
d1310b2e | 4847 | page = extent_buffer_page(eb, i); |
d1310b2e CM |
4848 | |
4849 | cur = min(len, (PAGE_CACHE_SIZE - offset)); | |
4850 | ||
a6591715 | 4851 | kaddr = page_address(page); |
d1310b2e | 4852 | ret = memcmp(ptr, kaddr + offset, cur); |
d1310b2e CM |
4853 | if (ret) |
4854 | break; | |
4855 | ||
4856 | ptr += cur; | |
4857 | len -= cur; | |
4858 | offset = 0; | |
4859 | i++; | |
4860 | } | |
4861 | return ret; | |
4862 | } | |
d1310b2e CM |
4863 | |
4864 | void write_extent_buffer(struct extent_buffer *eb, const void *srcv, | |
4865 | unsigned long start, unsigned long len) | |
4866 | { | |
4867 | size_t cur; | |
4868 | size_t offset; | |
4869 | struct page *page; | |
4870 | char *kaddr; | |
4871 | char *src = (char *)srcv; | |
4872 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); | |
4873 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; | |
4874 | ||
4875 | WARN_ON(start > eb->len); | |
4876 | WARN_ON(start + len > eb->start + eb->len); | |
4877 | ||
4878 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); | |
4879 | ||
d397712b | 4880 | while (len > 0) { |
d1310b2e CM |
4881 | page = extent_buffer_page(eb, i); |
4882 | WARN_ON(!PageUptodate(page)); | |
4883 | ||
4884 | cur = min(len, PAGE_CACHE_SIZE - offset); | |
a6591715 | 4885 | kaddr = page_address(page); |
d1310b2e | 4886 | memcpy(kaddr + offset, src, cur); |
d1310b2e CM |
4887 | |
4888 | src += cur; | |
4889 | len -= cur; | |
4890 | offset = 0; | |
4891 | i++; | |
4892 | } | |
4893 | } | |
d1310b2e CM |
4894 | |
4895 | void memset_extent_buffer(struct extent_buffer *eb, char c, | |
4896 | unsigned long start, unsigned long len) | |
4897 | { | |
4898 | size_t cur; | |
4899 | size_t offset; | |
4900 | struct page *page; | |
4901 | char *kaddr; | |
4902 | size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1); | |
4903 | unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT; | |
4904 | ||
4905 | WARN_ON(start > eb->len); | |
4906 | WARN_ON(start + len > eb->start + eb->len); | |
4907 | ||
4908 | offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1); | |
4909 | ||
d397712b | 4910 | while (len > 0) { |
d1310b2e CM |
4911 | page = extent_buffer_page(eb, i); |
4912 | WARN_ON(!PageUptodate(page)); | |
4913 | ||
4914 | cur = min(len, PAGE_CACHE_SIZE - offset); | |
a6591715 | 4915 | kaddr = page_address(page); |
d1310b2e | 4916 | memset(kaddr + offset, c, cur); |
d1310b2e CM |
4917 | |
4918 | len -= cur; | |
4919 | offset = 0; | |
4920 | i++; | |
4921 | } | |
4922 | } | |
d1310b2e CM |
4923 | |
4924 | void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src, | |
4925 | unsigned long dst_offset, unsigned long src_offset, | |
4926 | unsigned long len) | |
4927 | { | |
4928 | u64 dst_len = dst->len; | |
4929 | size_t cur; | |
4930 | size_t offset; | |
4931 | struct page *page; | |
4932 | char *kaddr; | |
4933 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); | |
4934 | unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; | |
4935 | ||
4936 | WARN_ON(src->len != dst_len); | |
4937 | ||
4938 | offset = (start_offset + dst_offset) & | |
4939 | ((unsigned long)PAGE_CACHE_SIZE - 1); | |
4940 | ||
d397712b | 4941 | while (len > 0) { |
d1310b2e CM |
4942 | page = extent_buffer_page(dst, i); |
4943 | WARN_ON(!PageUptodate(page)); | |
4944 | ||
4945 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset)); | |
4946 | ||
a6591715 | 4947 | kaddr = page_address(page); |
d1310b2e | 4948 | read_extent_buffer(src, kaddr + offset, src_offset, cur); |
d1310b2e CM |
4949 | |
4950 | src_offset += cur; | |
4951 | len -= cur; | |
4952 | offset = 0; | |
4953 | i++; | |
4954 | } | |
4955 | } | |
d1310b2e CM |
4956 | |
4957 | static void move_pages(struct page *dst_page, struct page *src_page, | |
4958 | unsigned long dst_off, unsigned long src_off, | |
4959 | unsigned long len) | |
4960 | { | |
a6591715 | 4961 | char *dst_kaddr = page_address(dst_page); |
d1310b2e CM |
4962 | if (dst_page == src_page) { |
4963 | memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len); | |
4964 | } else { | |
a6591715 | 4965 | char *src_kaddr = page_address(src_page); |
d1310b2e CM |
4966 | char *p = dst_kaddr + dst_off + len; |
4967 | char *s = src_kaddr + src_off + len; | |
4968 | ||
4969 | while (len--) | |
4970 | *--p = *--s; | |
d1310b2e | 4971 | } |
d1310b2e CM |
4972 | } |
4973 | ||
3387206f ST |
4974 | static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len) |
4975 | { | |
4976 | unsigned long distance = (src > dst) ? src - dst : dst - src; | |
4977 | return distance < len; | |
4978 | } | |
4979 | ||
d1310b2e CM |
4980 | static void copy_pages(struct page *dst_page, struct page *src_page, |
4981 | unsigned long dst_off, unsigned long src_off, | |
4982 | unsigned long len) | |
4983 | { | |
a6591715 | 4984 | char *dst_kaddr = page_address(dst_page); |
d1310b2e | 4985 | char *src_kaddr; |
727011e0 | 4986 | int must_memmove = 0; |
d1310b2e | 4987 | |
3387206f | 4988 | if (dst_page != src_page) { |
a6591715 | 4989 | src_kaddr = page_address(src_page); |
3387206f | 4990 | } else { |
d1310b2e | 4991 | src_kaddr = dst_kaddr; |
727011e0 CM |
4992 | if (areas_overlap(src_off, dst_off, len)) |
4993 | must_memmove = 1; | |
3387206f | 4994 | } |
d1310b2e | 4995 | |
727011e0 CM |
4996 | if (must_memmove) |
4997 | memmove(dst_kaddr + dst_off, src_kaddr + src_off, len); | |
4998 | else | |
4999 | memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len); | |
d1310b2e CM |
5000 | } |
5001 | ||
5002 | void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, | |
5003 | unsigned long src_offset, unsigned long len) | |
5004 | { | |
5005 | size_t cur; | |
5006 | size_t dst_off_in_page; | |
5007 | size_t src_off_in_page; | |
5008 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); | |
5009 | unsigned long dst_i; | |
5010 | unsigned long src_i; | |
5011 | ||
5012 | if (src_offset + len > dst->len) { | |
d397712b CM |
5013 | printk(KERN_ERR "btrfs memmove bogus src_offset %lu move " |
5014 | "len %lu dst len %lu\n", src_offset, len, dst->len); | |
d1310b2e CM |
5015 | BUG_ON(1); |
5016 | } | |
5017 | if (dst_offset + len > dst->len) { | |
d397712b CM |
5018 | printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move " |
5019 | "len %lu dst len %lu\n", dst_offset, len, dst->len); | |
d1310b2e CM |
5020 | BUG_ON(1); |
5021 | } | |
5022 | ||
d397712b | 5023 | while (len > 0) { |
d1310b2e CM |
5024 | dst_off_in_page = (start_offset + dst_offset) & |
5025 | ((unsigned long)PAGE_CACHE_SIZE - 1); | |
5026 | src_off_in_page = (start_offset + src_offset) & | |
5027 | ((unsigned long)PAGE_CACHE_SIZE - 1); | |
5028 | ||
5029 | dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT; | |
5030 | src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT; | |
5031 | ||
5032 | cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - | |
5033 | src_off_in_page)); | |
5034 | cur = min_t(unsigned long, cur, | |
5035 | (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page)); | |
5036 | ||
5037 | copy_pages(extent_buffer_page(dst, dst_i), | |
5038 | extent_buffer_page(dst, src_i), | |
5039 | dst_off_in_page, src_off_in_page, cur); | |
5040 | ||
5041 | src_offset += cur; | |
5042 | dst_offset += cur; | |
5043 | len -= cur; | |
5044 | } | |
5045 | } | |
d1310b2e CM |
5046 | |
5047 | void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, | |
5048 | unsigned long src_offset, unsigned long len) | |
5049 | { | |
5050 | size_t cur; | |
5051 | size_t dst_off_in_page; | |
5052 | size_t src_off_in_page; | |
5053 | unsigned long dst_end = dst_offset + len - 1; | |
5054 | unsigned long src_end = src_offset + len - 1; | |
5055 | size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1); | |
5056 | unsigned long dst_i; | |
5057 | unsigned long src_i; | |
5058 | ||
5059 | if (src_offset + len > dst->len) { | |
d397712b CM |
5060 | printk(KERN_ERR "btrfs memmove bogus src_offset %lu move " |
5061 | "len %lu len %lu\n", src_offset, len, dst->len); | |
d1310b2e CM |
5062 | BUG_ON(1); |
5063 | } | |
5064 | if (dst_offset + len > dst->len) { | |
d397712b CM |
5065 | printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move " |
5066 | "len %lu len %lu\n", dst_offset, len, dst->len); | |
d1310b2e CM |
5067 | BUG_ON(1); |
5068 | } | |
727011e0 | 5069 | if (dst_offset < src_offset) { |
d1310b2e CM |
5070 | memcpy_extent_buffer(dst, dst_offset, src_offset, len); |
5071 | return; | |
5072 | } | |
d397712b | 5073 | while (len > 0) { |
d1310b2e CM |
5074 | dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT; |
5075 | src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT; | |
5076 | ||
5077 | dst_off_in_page = (start_offset + dst_end) & | |
5078 | ((unsigned long)PAGE_CACHE_SIZE - 1); | |
5079 | src_off_in_page = (start_offset + src_end) & | |
5080 | ((unsigned long)PAGE_CACHE_SIZE - 1); | |
5081 | ||
5082 | cur = min_t(unsigned long, len, src_off_in_page + 1); | |
5083 | cur = min(cur, dst_off_in_page + 1); | |
5084 | move_pages(extent_buffer_page(dst, dst_i), | |
5085 | extent_buffer_page(dst, src_i), | |
5086 | dst_off_in_page - cur + 1, | |
5087 | src_off_in_page - cur + 1, cur); | |
5088 | ||
5089 | dst_end -= cur; | |
5090 | src_end -= cur; | |
5091 | len -= cur; | |
5092 | } | |
5093 | } | |
6af118ce | 5094 | |
f7a52a40 | 5095 | int try_release_extent_buffer(struct page *page) |
19fe0a8b | 5096 | { |
6af118ce | 5097 | struct extent_buffer *eb; |
6af118ce | 5098 | |
3083ee2e JB |
5099 | /* |
5100 | * We need to make sure noboody is attaching this page to an eb right | |
5101 | * now. | |
5102 | */ | |
5103 | spin_lock(&page->mapping->private_lock); | |
5104 | if (!PagePrivate(page)) { | |
5105 | spin_unlock(&page->mapping->private_lock); | |
4f2de97a | 5106 | return 1; |
45f49bce | 5107 | } |
6af118ce | 5108 | |
3083ee2e JB |
5109 | eb = (struct extent_buffer *)page->private; |
5110 | BUG_ON(!eb); | |
19fe0a8b MX |
5111 | |
5112 | /* | |
3083ee2e JB |
5113 | * This is a little awful but should be ok, we need to make sure that |
5114 | * the eb doesn't disappear out from under us while we're looking at | |
5115 | * this page. | |
19fe0a8b | 5116 | */ |
3083ee2e | 5117 | spin_lock(&eb->refs_lock); |
0b32f4bb | 5118 | if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) { |
3083ee2e JB |
5119 | spin_unlock(&eb->refs_lock); |
5120 | spin_unlock(&page->mapping->private_lock); | |
5121 | return 0; | |
b9473439 | 5122 | } |
3083ee2e | 5123 | spin_unlock(&page->mapping->private_lock); |
897ca6e9 | 5124 | |
19fe0a8b | 5125 | /* |
3083ee2e JB |
5126 | * If tree ref isn't set then we know the ref on this eb is a real ref, |
5127 | * so just return, this page will likely be freed soon anyway. | |
19fe0a8b | 5128 | */ |
3083ee2e JB |
5129 | if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) { |
5130 | spin_unlock(&eb->refs_lock); | |
5131 | return 0; | |
b9473439 | 5132 | } |
19fe0a8b | 5133 | |
f7a52a40 | 5134 | return release_extent_buffer(eb); |
6af118ce | 5135 | } |