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