Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
ec6b910f | 18 | #include <linux/sched.h> |
edbd8d4e | 19 | #include <linux/pagemap.h> |
ec44a35c | 20 | #include <linux/writeback.h> |
21af804c | 21 | #include <linux/blkdev.h> |
4b4e25f2 CM |
22 | #include <linux/version.h> |
23 | #include "compat.h" | |
74493f7a | 24 | #include "hash.h" |
a5eb62e3 | 25 | #include "crc32c.h" |
fec577fb CM |
26 | #include "ctree.h" |
27 | #include "disk-io.h" | |
28 | #include "print-tree.h" | |
e089f05c | 29 | #include "transaction.h" |
0b86a832 | 30 | #include "volumes.h" |
925baedd | 31 | #include "locking.h" |
31153d81 | 32 | #include "ref-cache.h" |
15916de8 | 33 | #include "compat.h" |
fec577fb | 34 | |
31840ae1 ZY |
35 | #define PENDING_EXTENT_INSERT 0 |
36 | #define PENDING_EXTENT_DELETE 1 | |
37 | #define PENDING_BACKREF_UPDATE 2 | |
38 | ||
39 | struct pending_extent_op { | |
40 | int type; | |
41 | u64 bytenr; | |
42 | u64 num_bytes; | |
43 | u64 parent; | |
44 | u64 orig_parent; | |
45 | u64 generation; | |
46 | u64 orig_generation; | |
47 | int level; | |
f3465ca4 JB |
48 | struct list_head list; |
49 | int del; | |
31840ae1 ZY |
50 | }; |
51 | ||
d397712b CM |
52 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
53 | struct btrfs_root *extent_root, int all); | |
54 | static int del_pending_extents(struct btrfs_trans_handle *trans, | |
55 | struct btrfs_root *extent_root, int all); | |
f3465ca4 JB |
56 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
57 | struct btrfs_root *root, | |
58 | u64 bytenr, u64 num_bytes, int is_data); | |
59 | static int update_block_group(struct btrfs_trans_handle *trans, | |
60 | struct btrfs_root *root, | |
61 | u64 bytenr, u64 num_bytes, int alloc, | |
62 | int mark_free); | |
d548ee51 | 63 | |
0f9dd46c JB |
64 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
65 | { | |
66 | return (cache->flags & bits) == bits; | |
67 | } | |
68 | ||
69 | /* | |
70 | * this adds the block group to the fs_info rb tree for the block group | |
71 | * cache | |
72 | */ | |
b2950863 | 73 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
0f9dd46c JB |
74 | struct btrfs_block_group_cache *block_group) |
75 | { | |
76 | struct rb_node **p; | |
77 | struct rb_node *parent = NULL; | |
78 | struct btrfs_block_group_cache *cache; | |
79 | ||
80 | spin_lock(&info->block_group_cache_lock); | |
81 | p = &info->block_group_cache_tree.rb_node; | |
82 | ||
83 | while (*p) { | |
84 | parent = *p; | |
85 | cache = rb_entry(parent, struct btrfs_block_group_cache, | |
86 | cache_node); | |
87 | if (block_group->key.objectid < cache->key.objectid) { | |
88 | p = &(*p)->rb_left; | |
89 | } else if (block_group->key.objectid > cache->key.objectid) { | |
90 | p = &(*p)->rb_right; | |
91 | } else { | |
92 | spin_unlock(&info->block_group_cache_lock); | |
93 | return -EEXIST; | |
94 | } | |
95 | } | |
96 | ||
97 | rb_link_node(&block_group->cache_node, parent, p); | |
98 | rb_insert_color(&block_group->cache_node, | |
99 | &info->block_group_cache_tree); | |
100 | spin_unlock(&info->block_group_cache_lock); | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
105 | /* | |
106 | * This will return the block group at or after bytenr if contains is 0, else | |
107 | * it will return the block group that contains the bytenr | |
108 | */ | |
109 | static struct btrfs_block_group_cache * | |
110 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, | |
111 | int contains) | |
112 | { | |
113 | struct btrfs_block_group_cache *cache, *ret = NULL; | |
114 | struct rb_node *n; | |
115 | u64 end, start; | |
116 | ||
117 | spin_lock(&info->block_group_cache_lock); | |
118 | n = info->block_group_cache_tree.rb_node; | |
119 | ||
120 | while (n) { | |
121 | cache = rb_entry(n, struct btrfs_block_group_cache, | |
122 | cache_node); | |
123 | end = cache->key.objectid + cache->key.offset - 1; | |
124 | start = cache->key.objectid; | |
125 | ||
126 | if (bytenr < start) { | |
127 | if (!contains && (!ret || start < ret->key.objectid)) | |
128 | ret = cache; | |
129 | n = n->rb_left; | |
130 | } else if (bytenr > start) { | |
131 | if (contains && bytenr <= end) { | |
132 | ret = cache; | |
133 | break; | |
134 | } | |
135 | n = n->rb_right; | |
136 | } else { | |
137 | ret = cache; | |
138 | break; | |
139 | } | |
140 | } | |
d2fb3437 YZ |
141 | if (ret) |
142 | atomic_inc(&ret->count); | |
0f9dd46c JB |
143 | spin_unlock(&info->block_group_cache_lock); |
144 | ||
145 | return ret; | |
146 | } | |
147 | ||
148 | /* | |
149 | * this is only called by cache_block_group, since we could have freed extents | |
150 | * we need to check the pinned_extents for any extents that can't be used yet | |
151 | * since their free space will be released as soon as the transaction commits. | |
152 | */ | |
153 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, | |
154 | struct btrfs_fs_info *info, u64 start, u64 end) | |
155 | { | |
156 | u64 extent_start, extent_end, size; | |
157 | int ret; | |
158 | ||
25179201 | 159 | mutex_lock(&info->pinned_mutex); |
0f9dd46c JB |
160 | while (start < end) { |
161 | ret = find_first_extent_bit(&info->pinned_extents, start, | |
162 | &extent_start, &extent_end, | |
163 | EXTENT_DIRTY); | |
164 | if (ret) | |
165 | break; | |
166 | ||
167 | if (extent_start == start) { | |
168 | start = extent_end + 1; | |
169 | } else if (extent_start > start && extent_start < end) { | |
170 | size = extent_start - start; | |
ea6a478e JB |
171 | ret = btrfs_add_free_space(block_group, start, |
172 | size); | |
0f9dd46c JB |
173 | BUG_ON(ret); |
174 | start = extent_end + 1; | |
175 | } else { | |
176 | break; | |
177 | } | |
178 | } | |
179 | ||
180 | if (start < end) { | |
181 | size = end - start; | |
ea6a478e | 182 | ret = btrfs_add_free_space(block_group, start, size); |
0f9dd46c JB |
183 | BUG_ON(ret); |
184 | } | |
25179201 | 185 | mutex_unlock(&info->pinned_mutex); |
0f9dd46c JB |
186 | |
187 | return 0; | |
188 | } | |
189 | ||
a512bbf8 YZ |
190 | static int remove_sb_from_cache(struct btrfs_root *root, |
191 | struct btrfs_block_group_cache *cache) | |
192 | { | |
193 | u64 bytenr; | |
194 | u64 *logical; | |
195 | int stripe_len; | |
196 | int i, nr, ret; | |
197 | ||
198 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { | |
199 | bytenr = btrfs_sb_offset(i); | |
200 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, | |
201 | cache->key.objectid, bytenr, 0, | |
202 | &logical, &nr, &stripe_len); | |
203 | BUG_ON(ret); | |
204 | while (nr--) { | |
205 | btrfs_remove_free_space(cache, logical[nr], | |
206 | stripe_len); | |
207 | } | |
208 | kfree(logical); | |
209 | } | |
210 | return 0; | |
211 | } | |
212 | ||
e37c9e69 CM |
213 | static int cache_block_group(struct btrfs_root *root, |
214 | struct btrfs_block_group_cache *block_group) | |
215 | { | |
216 | struct btrfs_path *path; | |
ef8bbdfe | 217 | int ret = 0; |
e37c9e69 | 218 | struct btrfs_key key; |
5f39d397 | 219 | struct extent_buffer *leaf; |
e37c9e69 | 220 | int slot; |
e4404d6e | 221 | u64 last; |
e37c9e69 | 222 | |
00f5c795 CM |
223 | if (!block_group) |
224 | return 0; | |
225 | ||
e37c9e69 | 226 | root = root->fs_info->extent_root; |
e37c9e69 CM |
227 | |
228 | if (block_group->cached) | |
229 | return 0; | |
f510cfec | 230 | |
e37c9e69 CM |
231 | path = btrfs_alloc_path(); |
232 | if (!path) | |
233 | return -ENOMEM; | |
7d7d6068 | 234 | |
2cc58cf2 | 235 | path->reada = 2; |
5cd57b2c CM |
236 | /* |
237 | * we get into deadlocks with paths held by callers of this function. | |
238 | * since the alloc_mutex is protecting things right now, just | |
239 | * skip the locking here | |
240 | */ | |
241 | path->skip_locking = 1; | |
e4404d6e YZ |
242 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
243 | key.objectid = last; | |
e37c9e69 CM |
244 | key.offset = 0; |
245 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
246 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
247 | if (ret < 0) | |
ef8bbdfe | 248 | goto err; |
a512bbf8 | 249 | |
d397712b | 250 | while (1) { |
5f39d397 | 251 | leaf = path->nodes[0]; |
e37c9e69 | 252 | slot = path->slots[0]; |
5f39d397 | 253 | if (slot >= btrfs_header_nritems(leaf)) { |
e37c9e69 | 254 | ret = btrfs_next_leaf(root, path); |
54aa1f4d CM |
255 | if (ret < 0) |
256 | goto err; | |
0f9dd46c | 257 | if (ret == 0) |
e37c9e69 | 258 | continue; |
0f9dd46c | 259 | else |
e37c9e69 | 260 | break; |
e37c9e69 | 261 | } |
5f39d397 | 262 | btrfs_item_key_to_cpu(leaf, &key, slot); |
0f9dd46c | 263 | if (key.objectid < block_group->key.objectid) |
7d7d6068 | 264 | goto next; |
0f9dd46c | 265 | |
e37c9e69 | 266 | if (key.objectid >= block_group->key.objectid + |
0f9dd46c | 267 | block_group->key.offset) |
e37c9e69 | 268 | break; |
7d7d6068 | 269 | |
e37c9e69 | 270 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
0f9dd46c JB |
271 | add_new_free_space(block_group, root->fs_info, last, |
272 | key.objectid); | |
273 | ||
7d7d6068 | 274 | last = key.objectid + key.offset; |
e37c9e69 | 275 | } |
7d7d6068 | 276 | next: |
e37c9e69 CM |
277 | path->slots[0]++; |
278 | } | |
279 | ||
0f9dd46c JB |
280 | add_new_free_space(block_group, root->fs_info, last, |
281 | block_group->key.objectid + | |
282 | block_group->key.offset); | |
283 | ||
a512bbf8 | 284 | remove_sb_from_cache(root, block_group); |
e37c9e69 | 285 | block_group->cached = 1; |
ef8bbdfe | 286 | ret = 0; |
54aa1f4d | 287 | err: |
e37c9e69 | 288 | btrfs_free_path(path); |
ef8bbdfe | 289 | return ret; |
e37c9e69 CM |
290 | } |
291 | ||
0f9dd46c JB |
292 | /* |
293 | * return the block group that starts at or after bytenr | |
294 | */ | |
d397712b CM |
295 | static struct btrfs_block_group_cache * |
296 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) | |
0ef3e66b | 297 | { |
0f9dd46c | 298 | struct btrfs_block_group_cache *cache; |
0ef3e66b | 299 | |
0f9dd46c | 300 | cache = block_group_cache_tree_search(info, bytenr, 0); |
0ef3e66b | 301 | |
0f9dd46c | 302 | return cache; |
0ef3e66b CM |
303 | } |
304 | ||
0f9dd46c JB |
305 | /* |
306 | * return the block group that contains teh given bytenr | |
307 | */ | |
d397712b CM |
308 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
309 | struct btrfs_fs_info *info, | |
310 | u64 bytenr) | |
be744175 | 311 | { |
0f9dd46c | 312 | struct btrfs_block_group_cache *cache; |
be744175 | 313 | |
0f9dd46c | 314 | cache = block_group_cache_tree_search(info, bytenr, 1); |
96b5179d | 315 | |
0f9dd46c | 316 | return cache; |
be744175 | 317 | } |
0b86a832 | 318 | |
d2fb3437 YZ |
319 | static inline void put_block_group(struct btrfs_block_group_cache *cache) |
320 | { | |
321 | if (atomic_dec_and_test(&cache->count)) | |
322 | kfree(cache); | |
323 | } | |
324 | ||
0f9dd46c JB |
325 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
326 | u64 flags) | |
6324fbf3 | 327 | { |
0f9dd46c JB |
328 | struct list_head *head = &info->space_info; |
329 | struct list_head *cur; | |
330 | struct btrfs_space_info *found; | |
331 | list_for_each(cur, head) { | |
332 | found = list_entry(cur, struct btrfs_space_info, list); | |
333 | if (found->flags == flags) | |
334 | return found; | |
335 | } | |
336 | return NULL; | |
6324fbf3 CM |
337 | } |
338 | ||
80eb234a JB |
339 | static u64 div_factor(u64 num, int factor) |
340 | { | |
341 | if (factor == 10) | |
342 | return num; | |
343 | num *= factor; | |
344 | do_div(num, 10); | |
345 | return num; | |
346 | } | |
347 | ||
d2fb3437 YZ |
348 | u64 btrfs_find_block_group(struct btrfs_root *root, |
349 | u64 search_start, u64 search_hint, int owner) | |
cd1bc465 | 350 | { |
96b5179d | 351 | struct btrfs_block_group_cache *cache; |
cd1bc465 | 352 | u64 used; |
d2fb3437 YZ |
353 | u64 last = max(search_hint, search_start); |
354 | u64 group_start = 0; | |
31f3c99b | 355 | int full_search = 0; |
d2fb3437 | 356 | int factor = 9; |
0ef3e66b | 357 | int wrapped = 0; |
31f3c99b | 358 | again: |
e8569813 ZY |
359 | while (1) { |
360 | cache = btrfs_lookup_first_block_group(root->fs_info, last); | |
0f9dd46c JB |
361 | if (!cache) |
362 | break; | |
96b5179d | 363 | |
c286ac48 | 364 | spin_lock(&cache->lock); |
96b5179d CM |
365 | last = cache->key.objectid + cache->key.offset; |
366 | used = btrfs_block_group_used(&cache->item); | |
367 | ||
d2fb3437 YZ |
368 | if ((full_search || !cache->ro) && |
369 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { | |
e8569813 | 370 | if (used + cache->pinned + cache->reserved < |
d2fb3437 YZ |
371 | div_factor(cache->key.offset, factor)) { |
372 | group_start = cache->key.objectid; | |
c286ac48 | 373 | spin_unlock(&cache->lock); |
d2fb3437 | 374 | put_block_group(cache); |
8790d502 CM |
375 | goto found; |
376 | } | |
6324fbf3 | 377 | } |
c286ac48 | 378 | spin_unlock(&cache->lock); |
d2fb3437 | 379 | put_block_group(cache); |
de428b63 | 380 | cond_resched(); |
cd1bc465 | 381 | } |
0ef3e66b CM |
382 | if (!wrapped) { |
383 | last = search_start; | |
384 | wrapped = 1; | |
385 | goto again; | |
386 | } | |
387 | if (!full_search && factor < 10) { | |
be744175 | 388 | last = search_start; |
31f3c99b | 389 | full_search = 1; |
0ef3e66b | 390 | factor = 10; |
31f3c99b CM |
391 | goto again; |
392 | } | |
be744175 | 393 | found: |
d2fb3437 | 394 | return group_start; |
925baedd | 395 | } |
0f9dd46c | 396 | |
e02119d5 | 397 | /* simple helper to search for an existing extent at a given offset */ |
31840ae1 | 398 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
e02119d5 CM |
399 | { |
400 | int ret; | |
401 | struct btrfs_key key; | |
31840ae1 | 402 | struct btrfs_path *path; |
e02119d5 | 403 | |
31840ae1 ZY |
404 | path = btrfs_alloc_path(); |
405 | BUG_ON(!path); | |
e02119d5 CM |
406 | key.objectid = start; |
407 | key.offset = len; | |
408 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
409 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, | |
410 | 0, 0); | |
31840ae1 | 411 | btrfs_free_path(path); |
7bb86316 CM |
412 | return ret; |
413 | } | |
414 | ||
d8d5f3e1 CM |
415 | /* |
416 | * Back reference rules. Back refs have three main goals: | |
417 | * | |
418 | * 1) differentiate between all holders of references to an extent so that | |
419 | * when a reference is dropped we can make sure it was a valid reference | |
420 | * before freeing the extent. | |
421 | * | |
422 | * 2) Provide enough information to quickly find the holders of an extent | |
423 | * if we notice a given block is corrupted or bad. | |
424 | * | |
425 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool | |
426 | * maintenance. This is actually the same as #2, but with a slightly | |
427 | * different use case. | |
428 | * | |
429 | * File extents can be referenced by: | |
430 | * | |
431 | * - multiple snapshots, subvolumes, or different generations in one subvol | |
31840ae1 | 432 | * - different files inside a single subvolume |
d8d5f3e1 CM |
433 | * - different offsets inside a file (bookend extents in file.c) |
434 | * | |
435 | * The extent ref structure has fields for: | |
436 | * | |
437 | * - Objectid of the subvolume root | |
438 | * - Generation number of the tree holding the reference | |
439 | * - objectid of the file holding the reference | |
31840ae1 ZY |
440 | * - number of references holding by parent node (alway 1 for tree blocks) |
441 | * | |
442 | * Btree leaf may hold multiple references to a file extent. In most cases, | |
443 | * these references are from same file and the corresponding offsets inside | |
3bb1a1bc | 444 | * the file are close together. |
d8d5f3e1 CM |
445 | * |
446 | * When a file extent is allocated the fields are filled in: | |
3bb1a1bc | 447 | * (root_key.objectid, trans->transid, inode objectid, 1) |
d8d5f3e1 CM |
448 | * |
449 | * When a leaf is cow'd new references are added for every file extent found | |
31840ae1 ZY |
450 | * in the leaf. It looks similar to the create case, but trans->transid will |
451 | * be different when the block is cow'd. | |
d8d5f3e1 | 452 | * |
3bb1a1bc | 453 | * (root_key.objectid, trans->transid, inode objectid, |
31840ae1 | 454 | * number of references in the leaf) |
d8d5f3e1 | 455 | * |
3bb1a1bc YZ |
456 | * When a file extent is removed either during snapshot deletion or |
457 | * file truncation, we find the corresponding back reference and check | |
458 | * the following fields: | |
d8d5f3e1 | 459 | * |
3bb1a1bc YZ |
460 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
461 | * inode objectid) | |
d8d5f3e1 CM |
462 | * |
463 | * Btree extents can be referenced by: | |
464 | * | |
465 | * - Different subvolumes | |
466 | * - Different generations of the same subvolume | |
467 | * | |
d8d5f3e1 CM |
468 | * When a tree block is created, back references are inserted: |
469 | * | |
3bb1a1bc | 470 | * (root->root_key.objectid, trans->transid, level, 1) |
d8d5f3e1 | 471 | * |
31840ae1 ZY |
472 | * When a tree block is cow'd, new back references are added for all the |
473 | * blocks it points to. If the tree block isn't in reference counted root, | |
474 | * the old back references are removed. These new back references are of | |
475 | * the form (trans->transid will have increased since creation): | |
d8d5f3e1 | 476 | * |
3bb1a1bc | 477 | * (root->root_key.objectid, trans->transid, level, 1) |
d8d5f3e1 | 478 | * |
31840ae1 | 479 | * When a backref is in deleting, the following fields are checked: |
d8d5f3e1 CM |
480 | * |
481 | * if backref was for a tree root: | |
3bb1a1bc | 482 | * (btrfs_header_owner(itself), btrfs_header_generation(itself), level) |
d8d5f3e1 | 483 | * else |
3bb1a1bc | 484 | * (btrfs_header_owner(parent), btrfs_header_generation(parent), level) |
d8d5f3e1 | 485 | * |
31840ae1 | 486 | * Back Reference Key composing: |
d8d5f3e1 | 487 | * |
31840ae1 ZY |
488 | * The key objectid corresponds to the first byte in the extent, the key |
489 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first | |
490 | * byte of parent extent. If a extent is tree root, the key offset is set | |
491 | * to the key objectid. | |
d8d5f3e1 | 492 | */ |
31840ae1 | 493 | |
d397712b | 494 | static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans, |
31840ae1 | 495 | struct btrfs_root *root, |
3bb1a1bc YZ |
496 | struct btrfs_path *path, |
497 | u64 bytenr, u64 parent, | |
498 | u64 ref_root, u64 ref_generation, | |
499 | u64 owner_objectid, int del) | |
7bb86316 | 500 | { |
7bb86316 | 501 | struct btrfs_key key; |
31840ae1 ZY |
502 | struct btrfs_extent_ref *ref; |
503 | struct extent_buffer *leaf; | |
3bb1a1bc | 504 | u64 ref_objectid; |
74493f7a CM |
505 | int ret; |
506 | ||
31840ae1 ZY |
507 | key.objectid = bytenr; |
508 | key.type = BTRFS_EXTENT_REF_KEY; | |
509 | key.offset = parent; | |
510 | ||
511 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); | |
512 | if (ret < 0) | |
513 | goto out; | |
514 | if (ret > 0) { | |
515 | ret = -ENOENT; | |
516 | goto out; | |
517 | } | |
518 | ||
519 | leaf = path->nodes[0]; | |
520 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | |
3bb1a1bc | 521 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
31840ae1 | 522 | if (btrfs_ref_root(leaf, ref) != ref_root || |
3bb1a1bc YZ |
523 | btrfs_ref_generation(leaf, ref) != ref_generation || |
524 | (ref_objectid != owner_objectid && | |
525 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { | |
31840ae1 ZY |
526 | ret = -EIO; |
527 | WARN_ON(1); | |
528 | goto out; | |
529 | } | |
530 | ret = 0; | |
531 | out: | |
532 | return ret; | |
533 | } | |
534 | ||
f3465ca4 JB |
535 | /* |
536 | * updates all the backrefs that are pending on update_list for the | |
537 | * extent_root | |
538 | */ | |
d397712b | 539 | static noinline int update_backrefs(struct btrfs_trans_handle *trans, |
f3465ca4 JB |
540 | struct btrfs_root *extent_root, |
541 | struct btrfs_path *path, | |
542 | struct list_head *update_list) | |
543 | { | |
544 | struct btrfs_key key; | |
545 | struct btrfs_extent_ref *ref; | |
546 | struct btrfs_fs_info *info = extent_root->fs_info; | |
547 | struct pending_extent_op *op; | |
548 | struct extent_buffer *leaf; | |
549 | int ret = 0; | |
550 | struct list_head *cur = update_list->next; | |
551 | u64 ref_objectid; | |
552 | u64 ref_root = extent_root->root_key.objectid; | |
553 | ||
554 | op = list_entry(cur, struct pending_extent_op, list); | |
555 | ||
556 | search: | |
557 | key.objectid = op->bytenr; | |
558 | key.type = BTRFS_EXTENT_REF_KEY; | |
559 | key.offset = op->orig_parent; | |
560 | ||
561 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1); | |
562 | BUG_ON(ret); | |
563 | ||
564 | leaf = path->nodes[0]; | |
565 | ||
566 | loop: | |
567 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | |
568 | ||
569 | ref_objectid = btrfs_ref_objectid(leaf, ref); | |
570 | ||
571 | if (btrfs_ref_root(leaf, ref) != ref_root || | |
572 | btrfs_ref_generation(leaf, ref) != op->orig_generation || | |
573 | (ref_objectid != op->level && | |
574 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { | |
d397712b CM |
575 | printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, " |
576 | "root %llu, owner %u\n", | |
577 | (unsigned long long)op->bytenr, | |
578 | (unsigned long long)op->orig_parent, | |
579 | (unsigned long long)ref_root, op->level); | |
f3465ca4 JB |
580 | btrfs_print_leaf(extent_root, leaf); |
581 | BUG(); | |
582 | } | |
583 | ||
584 | key.objectid = op->bytenr; | |
585 | key.offset = op->parent; | |
586 | key.type = BTRFS_EXTENT_REF_KEY; | |
587 | ret = btrfs_set_item_key_safe(trans, extent_root, path, &key); | |
588 | BUG_ON(ret); | |
589 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | |
590 | btrfs_set_ref_generation(leaf, ref, op->generation); | |
591 | ||
592 | cur = cur->next; | |
593 | ||
594 | list_del_init(&op->list); | |
595 | unlock_extent(&info->extent_ins, op->bytenr, | |
596 | op->bytenr + op->num_bytes - 1, GFP_NOFS); | |
597 | kfree(op); | |
598 | ||
599 | if (cur == update_list) { | |
600 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
601 | btrfs_release_path(extent_root, path); | |
602 | goto out; | |
603 | } | |
604 | ||
605 | op = list_entry(cur, struct pending_extent_op, list); | |
606 | ||
607 | path->slots[0]++; | |
608 | while (path->slots[0] < btrfs_header_nritems(leaf)) { | |
609 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
610 | if (key.objectid == op->bytenr && | |
611 | key.type == BTRFS_EXTENT_REF_KEY) | |
612 | goto loop; | |
613 | path->slots[0]++; | |
614 | } | |
615 | ||
616 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
617 | btrfs_release_path(extent_root, path); | |
618 | goto search; | |
619 | ||
620 | out: | |
621 | return 0; | |
622 | } | |
623 | ||
d397712b | 624 | static noinline int insert_extents(struct btrfs_trans_handle *trans, |
f3465ca4 JB |
625 | struct btrfs_root *extent_root, |
626 | struct btrfs_path *path, | |
627 | struct list_head *insert_list, int nr) | |
628 | { | |
629 | struct btrfs_key *keys; | |
630 | u32 *data_size; | |
631 | struct pending_extent_op *op; | |
632 | struct extent_buffer *leaf; | |
633 | struct list_head *cur = insert_list->next; | |
634 | struct btrfs_fs_info *info = extent_root->fs_info; | |
635 | u64 ref_root = extent_root->root_key.objectid; | |
636 | int i = 0, last = 0, ret; | |
637 | int total = nr * 2; | |
638 | ||
639 | if (!nr) | |
640 | return 0; | |
641 | ||
642 | keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS); | |
643 | if (!keys) | |
644 | return -ENOMEM; | |
645 | ||
646 | data_size = kzalloc(total * sizeof(u32), GFP_NOFS); | |
647 | if (!data_size) { | |
648 | kfree(keys); | |
649 | return -ENOMEM; | |
650 | } | |
651 | ||
652 | list_for_each_entry(op, insert_list, list) { | |
653 | keys[i].objectid = op->bytenr; | |
654 | keys[i].offset = op->num_bytes; | |
655 | keys[i].type = BTRFS_EXTENT_ITEM_KEY; | |
656 | data_size[i] = sizeof(struct btrfs_extent_item); | |
657 | i++; | |
658 | ||
659 | keys[i].objectid = op->bytenr; | |
660 | keys[i].offset = op->parent; | |
661 | keys[i].type = BTRFS_EXTENT_REF_KEY; | |
662 | data_size[i] = sizeof(struct btrfs_extent_ref); | |
663 | i++; | |
664 | } | |
665 | ||
666 | op = list_entry(cur, struct pending_extent_op, list); | |
667 | i = 0; | |
668 | while (i < total) { | |
669 | int c; | |
670 | ret = btrfs_insert_some_items(trans, extent_root, path, | |
671 | keys+i, data_size+i, total-i); | |
672 | BUG_ON(ret < 0); | |
673 | ||
674 | if (last && ret > 1) | |
675 | BUG(); | |
676 | ||
677 | leaf = path->nodes[0]; | |
678 | for (c = 0; c < ret; c++) { | |
679 | int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY; | |
680 | ||
681 | /* | |
682 | * if the first item we inserted was a backref, then | |
683 | * the EXTENT_ITEM will be the odd c's, else it will | |
684 | * be the even c's | |
685 | */ | |
686 | if ((ref_first && (c % 2)) || | |
687 | (!ref_first && !(c % 2))) { | |
688 | struct btrfs_extent_item *itm; | |
689 | ||
690 | itm = btrfs_item_ptr(leaf, path->slots[0] + c, | |
691 | struct btrfs_extent_item); | |
692 | btrfs_set_extent_refs(path->nodes[0], itm, 1); | |
693 | op->del++; | |
694 | } else { | |
695 | struct btrfs_extent_ref *ref; | |
696 | ||
697 | ref = btrfs_item_ptr(leaf, path->slots[0] + c, | |
698 | struct btrfs_extent_ref); | |
699 | btrfs_set_ref_root(leaf, ref, ref_root); | |
700 | btrfs_set_ref_generation(leaf, ref, | |
701 | op->generation); | |
702 | btrfs_set_ref_objectid(leaf, ref, op->level); | |
703 | btrfs_set_ref_num_refs(leaf, ref, 1); | |
704 | op->del++; | |
705 | } | |
706 | ||
707 | /* | |
708 | * using del to see when its ok to free up the | |
709 | * pending_extent_op. In the case where we insert the | |
710 | * last item on the list in order to help do batching | |
711 | * we need to not free the extent op until we actually | |
712 | * insert the extent_item | |
713 | */ | |
714 | if (op->del == 2) { | |
715 | unlock_extent(&info->extent_ins, op->bytenr, | |
716 | op->bytenr + op->num_bytes - 1, | |
717 | GFP_NOFS); | |
718 | cur = cur->next; | |
719 | list_del_init(&op->list); | |
720 | kfree(op); | |
721 | if (cur != insert_list) | |
722 | op = list_entry(cur, | |
723 | struct pending_extent_op, | |
724 | list); | |
725 | } | |
726 | } | |
727 | btrfs_mark_buffer_dirty(leaf); | |
728 | btrfs_release_path(extent_root, path); | |
729 | ||
730 | /* | |
731 | * Ok backref's and items usually go right next to eachother, | |
732 | * but if we could only insert 1 item that means that we | |
733 | * inserted on the end of a leaf, and we have no idea what may | |
734 | * be on the next leaf so we just play it safe. In order to | |
735 | * try and help this case we insert the last thing on our | |
736 | * insert list so hopefully it will end up being the last | |
737 | * thing on the leaf and everything else will be before it, | |
738 | * which will let us insert a whole bunch of items at the same | |
739 | * time. | |
740 | */ | |
741 | if (ret == 1 && !last && (i + ret < total)) { | |
742 | /* | |
743 | * last: where we will pick up the next time around | |
744 | * i: our current key to insert, will be total - 1 | |
745 | * cur: the current op we are screwing with | |
746 | * op: duh | |
747 | */ | |
748 | last = i + ret; | |
749 | i = total - 1; | |
750 | cur = insert_list->prev; | |
751 | op = list_entry(cur, struct pending_extent_op, list); | |
752 | } else if (last) { | |
753 | /* | |
754 | * ok we successfully inserted the last item on the | |
755 | * list, lets reset everything | |
756 | * | |
757 | * i: our current key to insert, so where we left off | |
758 | * last time | |
759 | * last: done with this | |
760 | * cur: the op we are messing with | |
761 | * op: duh | |
762 | * total: since we inserted the last key, we need to | |
763 | * decrement total so we dont overflow | |
764 | */ | |
765 | i = last; | |
766 | last = 0; | |
f3465ca4 | 767 | total--; |
b4eec2ca LH |
768 | if (i < total) { |
769 | cur = insert_list->next; | |
770 | op = list_entry(cur, struct pending_extent_op, | |
771 | list); | |
772 | } | |
f3465ca4 JB |
773 | } else { |
774 | i += ret; | |
775 | } | |
776 | ||
777 | cond_resched(); | |
778 | } | |
779 | ret = 0; | |
780 | kfree(keys); | |
781 | kfree(data_size); | |
782 | return ret; | |
783 | } | |
784 | ||
d397712b | 785 | static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
786 | struct btrfs_root *root, |
787 | struct btrfs_path *path, | |
788 | u64 bytenr, u64 parent, | |
789 | u64 ref_root, u64 ref_generation, | |
3bb1a1bc | 790 | u64 owner_objectid) |
31840ae1 ZY |
791 | { |
792 | struct btrfs_key key; | |
793 | struct extent_buffer *leaf; | |
794 | struct btrfs_extent_ref *ref; | |
795 | u32 num_refs; | |
796 | int ret; | |
74493f7a | 797 | |
74493f7a CM |
798 | key.objectid = bytenr; |
799 | key.type = BTRFS_EXTENT_REF_KEY; | |
31840ae1 | 800 | key.offset = parent; |
74493f7a | 801 | |
31840ae1 ZY |
802 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
803 | if (ret == 0) { | |
804 | leaf = path->nodes[0]; | |
805 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
806 | struct btrfs_extent_ref); | |
807 | btrfs_set_ref_root(leaf, ref, ref_root); | |
808 | btrfs_set_ref_generation(leaf, ref, ref_generation); | |
809 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); | |
31840ae1 ZY |
810 | btrfs_set_ref_num_refs(leaf, ref, 1); |
811 | } else if (ret == -EEXIST) { | |
812 | u64 existing_owner; | |
813 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); | |
814 | leaf = path->nodes[0]; | |
815 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
816 | struct btrfs_extent_ref); | |
817 | if (btrfs_ref_root(leaf, ref) != ref_root || | |
818 | btrfs_ref_generation(leaf, ref) != ref_generation) { | |
819 | ret = -EIO; | |
820 | WARN_ON(1); | |
7bb86316 | 821 | goto out; |
31840ae1 ZY |
822 | } |
823 | ||
824 | num_refs = btrfs_ref_num_refs(leaf, ref); | |
825 | BUG_ON(num_refs == 0); | |
826 | btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); | |
827 | ||
828 | existing_owner = btrfs_ref_objectid(leaf, ref); | |
3bb1a1bc YZ |
829 | if (existing_owner != owner_objectid && |
830 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { | |
31840ae1 ZY |
831 | btrfs_set_ref_objectid(leaf, ref, |
832 | BTRFS_MULTIPLE_OBJECTIDS); | |
31840ae1 ZY |
833 | } |
834 | ret = 0; | |
835 | } else { | |
7bb86316 | 836 | goto out; |
31840ae1 | 837 | } |
7bb86316 CM |
838 | btrfs_mark_buffer_dirty(path->nodes[0]); |
839 | out: | |
840 | btrfs_release_path(root, path); | |
841 | return ret; | |
74493f7a CM |
842 | } |
843 | ||
d397712b | 844 | static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
845 | struct btrfs_root *root, |
846 | struct btrfs_path *path) | |
847 | { | |
848 | struct extent_buffer *leaf; | |
849 | struct btrfs_extent_ref *ref; | |
850 | u32 num_refs; | |
851 | int ret = 0; | |
852 | ||
853 | leaf = path->nodes[0]; | |
854 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | |
855 | num_refs = btrfs_ref_num_refs(leaf, ref); | |
856 | BUG_ON(num_refs == 0); | |
857 | num_refs -= 1; | |
858 | if (num_refs == 0) { | |
859 | ret = btrfs_del_item(trans, root, path); | |
860 | } else { | |
861 | btrfs_set_ref_num_refs(leaf, ref, num_refs); | |
862 | btrfs_mark_buffer_dirty(leaf); | |
863 | } | |
864 | btrfs_release_path(root, path); | |
865 | return ret; | |
866 | } | |
867 | ||
4b4e25f2 | 868 | #ifdef BIO_RW_DISCARD |
15916de8 CM |
869 | static void btrfs_issue_discard(struct block_device *bdev, |
870 | u64 start, u64 len) | |
871 | { | |
15916de8 | 872 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
15916de8 | 873 | } |
4b4e25f2 | 874 | #endif |
15916de8 | 875 | |
1f3c79a2 LH |
876 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
877 | u64 num_bytes) | |
878 | { | |
879 | #ifdef BIO_RW_DISCARD | |
880 | int ret; | |
881 | u64 map_length = num_bytes; | |
882 | struct btrfs_multi_bio *multi = NULL; | |
883 | ||
884 | /* Tell the block device(s) that the sectors can be discarded */ | |
885 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, | |
886 | bytenr, &map_length, &multi, 0); | |
887 | if (!ret) { | |
888 | struct btrfs_bio_stripe *stripe = multi->stripes; | |
889 | int i; | |
890 | ||
891 | if (map_length > num_bytes) | |
892 | map_length = num_bytes; | |
893 | ||
894 | for (i = 0; i < multi->num_stripes; i++, stripe++) { | |
895 | btrfs_issue_discard(stripe->dev->bdev, | |
896 | stripe->physical, | |
897 | map_length); | |
898 | } | |
899 | kfree(multi); | |
900 | } | |
901 | ||
902 | return ret; | |
903 | #else | |
904 | return 0; | |
905 | #endif | |
906 | } | |
907 | ||
d397712b | 908 | static noinline int free_extents(struct btrfs_trans_handle *trans, |
f3465ca4 JB |
909 | struct btrfs_root *extent_root, |
910 | struct list_head *del_list) | |
911 | { | |
912 | struct btrfs_fs_info *info = extent_root->fs_info; | |
913 | struct btrfs_path *path; | |
914 | struct btrfs_key key, found_key; | |
915 | struct extent_buffer *leaf; | |
916 | struct list_head *cur; | |
917 | struct pending_extent_op *op; | |
918 | struct btrfs_extent_item *ei; | |
919 | int ret, num_to_del, extent_slot = 0, found_extent = 0; | |
920 | u32 refs; | |
921 | u64 bytes_freed = 0; | |
922 | ||
923 | path = btrfs_alloc_path(); | |
924 | if (!path) | |
925 | return -ENOMEM; | |
926 | path->reada = 1; | |
927 | ||
928 | search: | |
929 | /* search for the backref for the current ref we want to delete */ | |
930 | cur = del_list->next; | |
931 | op = list_entry(cur, struct pending_extent_op, list); | |
932 | ret = lookup_extent_backref(trans, extent_root, path, op->bytenr, | |
933 | op->orig_parent, | |
934 | extent_root->root_key.objectid, | |
935 | op->orig_generation, op->level, 1); | |
936 | if (ret) { | |
d397712b CM |
937 | printk(KERN_ERR "btrfs unable to find backref byte nr %llu " |
938 | "root %llu gen %llu owner %u\n", | |
939 | (unsigned long long)op->bytenr, | |
940 | (unsigned long long)extent_root->root_key.objectid, | |
941 | (unsigned long long)op->orig_generation, op->level); | |
f3465ca4 JB |
942 | btrfs_print_leaf(extent_root, path->nodes[0]); |
943 | WARN_ON(1); | |
944 | goto out; | |
945 | } | |
946 | ||
947 | extent_slot = path->slots[0]; | |
948 | num_to_del = 1; | |
949 | found_extent = 0; | |
950 | ||
951 | /* | |
952 | * if we aren't the first item on the leaf we can move back one and see | |
953 | * if our ref is right next to our extent item | |
954 | */ | |
955 | if (likely(extent_slot)) { | |
956 | extent_slot--; | |
957 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
958 | extent_slot); | |
959 | if (found_key.objectid == op->bytenr && | |
960 | found_key.type == BTRFS_EXTENT_ITEM_KEY && | |
961 | found_key.offset == op->num_bytes) { | |
962 | num_to_del++; | |
963 | found_extent = 1; | |
964 | } | |
965 | } | |
966 | ||
967 | /* | |
968 | * if we didn't find the extent we need to delete the backref and then | |
969 | * search for the extent item key so we can update its ref count | |
970 | */ | |
971 | if (!found_extent) { | |
972 | key.objectid = op->bytenr; | |
973 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
974 | key.offset = op->num_bytes; | |
975 | ||
976 | ret = remove_extent_backref(trans, extent_root, path); | |
977 | BUG_ON(ret); | |
978 | btrfs_release_path(extent_root, path); | |
979 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); | |
980 | BUG_ON(ret); | |
981 | extent_slot = path->slots[0]; | |
982 | } | |
983 | ||
984 | /* this is where we update the ref count for the extent */ | |
985 | leaf = path->nodes[0]; | |
986 | ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item); | |
987 | refs = btrfs_extent_refs(leaf, ei); | |
988 | BUG_ON(refs == 0); | |
989 | refs--; | |
990 | btrfs_set_extent_refs(leaf, ei, refs); | |
991 | ||
992 | btrfs_mark_buffer_dirty(leaf); | |
993 | ||
994 | /* | |
995 | * This extent needs deleting. The reason cur_slot is extent_slot + | |
996 | * num_to_del is because extent_slot points to the slot where the extent | |
997 | * is, and if the backref was not right next to the extent we will be | |
998 | * deleting at least 1 item, and will want to start searching at the | |
999 | * slot directly next to extent_slot. However if we did find the | |
1000 | * backref next to the extent item them we will be deleting at least 2 | |
1001 | * items and will want to start searching directly after the ref slot | |
1002 | */ | |
1003 | if (!refs) { | |
1004 | struct list_head *pos, *n, *end; | |
1005 | int cur_slot = extent_slot+num_to_del; | |
1006 | u64 super_used; | |
1007 | u64 root_used; | |
1008 | ||
1009 | path->slots[0] = extent_slot; | |
1010 | bytes_freed = op->num_bytes; | |
1011 | ||
e3e469f8 JB |
1012 | mutex_lock(&info->pinned_mutex); |
1013 | ret = pin_down_bytes(trans, extent_root, op->bytenr, | |
1014 | op->num_bytes, op->level >= | |
1015 | BTRFS_FIRST_FREE_OBJECTID); | |
1016 | mutex_unlock(&info->pinned_mutex); | |
1017 | BUG_ON(ret < 0); | |
1018 | op->del = ret; | |
1019 | ||
f3465ca4 JB |
1020 | /* |
1021 | * we need to see if we can delete multiple things at once, so | |
1022 | * start looping through the list of extents we are wanting to | |
1023 | * delete and see if their extent/backref's are right next to | |
1024 | * eachother and the extents only have 1 ref | |
1025 | */ | |
1026 | for (pos = cur->next; pos != del_list; pos = pos->next) { | |
1027 | struct pending_extent_op *tmp; | |
1028 | ||
1029 | tmp = list_entry(pos, struct pending_extent_op, list); | |
1030 | ||
1031 | /* we only want to delete extent+ref at this stage */ | |
1032 | if (cur_slot >= btrfs_header_nritems(leaf) - 1) | |
1033 | break; | |
1034 | ||
1035 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot); | |
1036 | if (found_key.objectid != tmp->bytenr || | |
1037 | found_key.type != BTRFS_EXTENT_ITEM_KEY || | |
1038 | found_key.offset != tmp->num_bytes) | |
1039 | break; | |
1040 | ||
1041 | /* check to make sure this extent only has one ref */ | |
1042 | ei = btrfs_item_ptr(leaf, cur_slot, | |
1043 | struct btrfs_extent_item); | |
1044 | if (btrfs_extent_refs(leaf, ei) != 1) | |
1045 | break; | |
1046 | ||
1047 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1); | |
1048 | if (found_key.objectid != tmp->bytenr || | |
1049 | found_key.type != BTRFS_EXTENT_REF_KEY || | |
1050 | found_key.offset != tmp->orig_parent) | |
1051 | break; | |
1052 | ||
1053 | /* | |
1054 | * the ref is right next to the extent, we can set the | |
1055 | * ref count to 0 since we will delete them both now | |
1056 | */ | |
1057 | btrfs_set_extent_refs(leaf, ei, 0); | |
1058 | ||
1059 | /* pin down the bytes for this extent */ | |
1060 | mutex_lock(&info->pinned_mutex); | |
1061 | ret = pin_down_bytes(trans, extent_root, tmp->bytenr, | |
1062 | tmp->num_bytes, tmp->level >= | |
1063 | BTRFS_FIRST_FREE_OBJECTID); | |
1064 | mutex_unlock(&info->pinned_mutex); | |
1065 | BUG_ON(ret < 0); | |
1066 | ||
1067 | /* | |
1068 | * use the del field to tell if we need to go ahead and | |
1069 | * free up the extent when we delete the item or not. | |
1070 | */ | |
1071 | tmp->del = ret; | |
1072 | bytes_freed += tmp->num_bytes; | |
1073 | ||
1074 | num_to_del += 2; | |
1075 | cur_slot += 2; | |
1076 | } | |
1077 | end = pos; | |
1078 | ||
1079 | /* update the free space counters */ | |
75eff68e | 1080 | spin_lock(&info->delalloc_lock); |
f3465ca4 JB |
1081 | super_used = btrfs_super_bytes_used(&info->super_copy); |
1082 | btrfs_set_super_bytes_used(&info->super_copy, | |
1083 | super_used - bytes_freed); | |
f3465ca4 JB |
1084 | |
1085 | root_used = btrfs_root_used(&extent_root->root_item); | |
1086 | btrfs_set_root_used(&extent_root->root_item, | |
1087 | root_used - bytes_freed); | |
34bf63c4 | 1088 | spin_unlock(&info->delalloc_lock); |
f3465ca4 JB |
1089 | |
1090 | /* delete the items */ | |
1091 | ret = btrfs_del_items(trans, extent_root, path, | |
1092 | path->slots[0], num_to_del); | |
1093 | BUG_ON(ret); | |
1094 | ||
1095 | /* | |
1096 | * loop through the extents we deleted and do the cleanup work | |
1097 | * on them | |
1098 | */ | |
1099 | for (pos = cur, n = pos->next; pos != end; | |
1100 | pos = n, n = pos->next) { | |
1101 | struct pending_extent_op *tmp; | |
f3465ca4 JB |
1102 | tmp = list_entry(pos, struct pending_extent_op, list); |
1103 | ||
1104 | /* | |
1105 | * remember tmp->del tells us wether or not we pinned | |
1106 | * down the extent | |
1107 | */ | |
1108 | ret = update_block_group(trans, extent_root, | |
1109 | tmp->bytenr, tmp->num_bytes, 0, | |
1110 | tmp->del); | |
1111 | BUG_ON(ret); | |
1112 | ||
f3465ca4 JB |
1113 | list_del_init(&tmp->list); |
1114 | unlock_extent(&info->extent_ins, tmp->bytenr, | |
1115 | tmp->bytenr + tmp->num_bytes - 1, | |
1116 | GFP_NOFS); | |
1117 | kfree(tmp); | |
1118 | } | |
1119 | } else if (refs && found_extent) { | |
1120 | /* | |
1121 | * the ref and extent were right next to eachother, but the | |
1122 | * extent still has a ref, so just free the backref and keep | |
1123 | * going | |
1124 | */ | |
1125 | ret = remove_extent_backref(trans, extent_root, path); | |
1126 | BUG_ON(ret); | |
1127 | ||
1128 | list_del_init(&op->list); | |
1129 | unlock_extent(&info->extent_ins, op->bytenr, | |
1130 | op->bytenr + op->num_bytes - 1, GFP_NOFS); | |
1131 | kfree(op); | |
1132 | } else { | |
1133 | /* | |
1134 | * the extent has multiple refs and the backref we were looking | |
1135 | * for was not right next to it, so just unlock and go next, | |
1136 | * we're good to go | |
1137 | */ | |
1138 | list_del_init(&op->list); | |
1139 | unlock_extent(&info->extent_ins, op->bytenr, | |
1140 | op->bytenr + op->num_bytes - 1, GFP_NOFS); | |
1141 | kfree(op); | |
1142 | } | |
1143 | ||
1144 | btrfs_release_path(extent_root, path); | |
1145 | if (!list_empty(del_list)) | |
1146 | goto search; | |
1147 | ||
1148 | out: | |
1149 | btrfs_free_path(path); | |
1150 | return ret; | |
1151 | } | |
1152 | ||
31840ae1 ZY |
1153 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
1154 | struct btrfs_root *root, u64 bytenr, | |
1155 | u64 orig_parent, u64 parent, | |
1156 | u64 orig_root, u64 ref_root, | |
1157 | u64 orig_generation, u64 ref_generation, | |
3bb1a1bc | 1158 | u64 owner_objectid) |
31840ae1 ZY |
1159 | { |
1160 | int ret; | |
1161 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
1162 | struct btrfs_path *path; | |
1163 | ||
1164 | if (root == root->fs_info->extent_root) { | |
1165 | struct pending_extent_op *extent_op; | |
1166 | u64 num_bytes; | |
1167 | ||
1168 | BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL); | |
1169 | num_bytes = btrfs_level_size(root, (int)owner_objectid); | |
25179201 | 1170 | mutex_lock(&root->fs_info->extent_ins_mutex); |
31840ae1 | 1171 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
25179201 | 1172 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
31840ae1 ZY |
1173 | u64 priv; |
1174 | ret = get_state_private(&root->fs_info->extent_ins, | |
1175 | bytenr, &priv); | |
1176 | BUG_ON(ret); | |
1177 | extent_op = (struct pending_extent_op *) | |
1178 | (unsigned long)priv; | |
1179 | BUG_ON(extent_op->parent != orig_parent); | |
1180 | BUG_ON(extent_op->generation != orig_generation); | |
25179201 | 1181 | |
31840ae1 ZY |
1182 | extent_op->parent = parent; |
1183 | extent_op->generation = ref_generation; | |
1184 | } else { | |
1185 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | |
1186 | BUG_ON(!extent_op); | |
1187 | ||
1188 | extent_op->type = PENDING_BACKREF_UPDATE; | |
1189 | extent_op->bytenr = bytenr; | |
1190 | extent_op->num_bytes = num_bytes; | |
1191 | extent_op->parent = parent; | |
1192 | extent_op->orig_parent = orig_parent; | |
1193 | extent_op->generation = ref_generation; | |
1194 | extent_op->orig_generation = orig_generation; | |
1195 | extent_op->level = (int)owner_objectid; | |
f3465ca4 JB |
1196 | INIT_LIST_HEAD(&extent_op->list); |
1197 | extent_op->del = 0; | |
31840ae1 ZY |
1198 | |
1199 | set_extent_bits(&root->fs_info->extent_ins, | |
1200 | bytenr, bytenr + num_bytes - 1, | |
25179201 | 1201 | EXTENT_WRITEBACK, GFP_NOFS); |
31840ae1 ZY |
1202 | set_state_private(&root->fs_info->extent_ins, |
1203 | bytenr, (unsigned long)extent_op); | |
1204 | } | |
25179201 | 1205 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
31840ae1 ZY |
1206 | return 0; |
1207 | } | |
1208 | ||
1209 | path = btrfs_alloc_path(); | |
1210 | if (!path) | |
1211 | return -ENOMEM; | |
1212 | ret = lookup_extent_backref(trans, extent_root, path, | |
1213 | bytenr, orig_parent, orig_root, | |
3bb1a1bc | 1214 | orig_generation, owner_objectid, 1); |
31840ae1 ZY |
1215 | if (ret) |
1216 | goto out; | |
1217 | ret = remove_extent_backref(trans, extent_root, path); | |
1218 | if (ret) | |
1219 | goto out; | |
1220 | ret = insert_extent_backref(trans, extent_root, path, bytenr, | |
1221 | parent, ref_root, ref_generation, | |
3bb1a1bc | 1222 | owner_objectid); |
31840ae1 | 1223 | BUG_ON(ret); |
87ef2bb4 CM |
1224 | finish_current_insert(trans, extent_root, 0); |
1225 | del_pending_extents(trans, extent_root, 0); | |
31840ae1 ZY |
1226 | out: |
1227 | btrfs_free_path(path); | |
1228 | return ret; | |
1229 | } | |
1230 | ||
1231 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, | |
1232 | struct btrfs_root *root, u64 bytenr, | |
1233 | u64 orig_parent, u64 parent, | |
1234 | u64 ref_root, u64 ref_generation, | |
3bb1a1bc | 1235 | u64 owner_objectid) |
31840ae1 ZY |
1236 | { |
1237 | int ret; | |
1238 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && | |
1239 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | |
1240 | return 0; | |
31840ae1 ZY |
1241 | ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, |
1242 | parent, ref_root, ref_root, | |
1243 | ref_generation, ref_generation, | |
3bb1a1bc | 1244 | owner_objectid); |
31840ae1 ZY |
1245 | return ret; |
1246 | } | |
1247 | ||
925baedd | 1248 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
1249 | struct btrfs_root *root, u64 bytenr, |
1250 | u64 orig_parent, u64 parent, | |
1251 | u64 orig_root, u64 ref_root, | |
1252 | u64 orig_generation, u64 ref_generation, | |
3bb1a1bc | 1253 | u64 owner_objectid) |
02217ed2 | 1254 | { |
5caf2a00 | 1255 | struct btrfs_path *path; |
02217ed2 | 1256 | int ret; |
e2fa7227 | 1257 | struct btrfs_key key; |
5f39d397 | 1258 | struct extent_buffer *l; |
234b63a0 | 1259 | struct btrfs_extent_item *item; |
cf27e1ee | 1260 | u32 refs; |
037e6390 | 1261 | |
5caf2a00 | 1262 | path = btrfs_alloc_path(); |
54aa1f4d CM |
1263 | if (!path) |
1264 | return -ENOMEM; | |
26b8003f | 1265 | |
3c12ac72 | 1266 | path->reada = 1; |
db94535d | 1267 | key.objectid = bytenr; |
31840ae1 ZY |
1268 | key.type = BTRFS_EXTENT_ITEM_KEY; |
1269 | key.offset = (u64)-1; | |
1270 | ||
5caf2a00 | 1271 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 1272 | 0, 1); |
54aa1f4d CM |
1273 | if (ret < 0) |
1274 | return ret; | |
31840ae1 ZY |
1275 | BUG_ON(ret == 0 || path->slots[0] == 0); |
1276 | ||
1277 | path->slots[0]--; | |
5f39d397 | 1278 | l = path->nodes[0]; |
31840ae1 ZY |
1279 | |
1280 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
771ed689 CM |
1281 | if (key.objectid != bytenr) { |
1282 | btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]); | |
d397712b CM |
1283 | printk(KERN_ERR "btrfs wanted %llu found %llu\n", |
1284 | (unsigned long long)bytenr, | |
1285 | (unsigned long long)key.objectid); | |
771ed689 CM |
1286 | BUG(); |
1287 | } | |
31840ae1 ZY |
1288 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
1289 | ||
5caf2a00 | 1290 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
5f39d397 CM |
1291 | refs = btrfs_extent_refs(l, item); |
1292 | btrfs_set_extent_refs(l, item, refs + 1); | |
5caf2a00 | 1293 | btrfs_mark_buffer_dirty(path->nodes[0]); |
a28ec197 | 1294 | |
5caf2a00 | 1295 | btrfs_release_path(root->fs_info->extent_root, path); |
7bb86316 | 1296 | |
3c12ac72 | 1297 | path->reada = 1; |
31840ae1 ZY |
1298 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
1299 | path, bytenr, parent, | |
1300 | ref_root, ref_generation, | |
3bb1a1bc | 1301 | owner_objectid); |
7bb86316 | 1302 | BUG_ON(ret); |
87ef2bb4 CM |
1303 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
1304 | del_pending_extents(trans, root->fs_info->extent_root, 0); | |
74493f7a CM |
1305 | |
1306 | btrfs_free_path(path); | |
02217ed2 CM |
1307 | return 0; |
1308 | } | |
1309 | ||
925baedd | 1310 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
1311 | struct btrfs_root *root, |
1312 | u64 bytenr, u64 num_bytes, u64 parent, | |
1313 | u64 ref_root, u64 ref_generation, | |
3bb1a1bc | 1314 | u64 owner_objectid) |
925baedd CM |
1315 | { |
1316 | int ret; | |
31840ae1 ZY |
1317 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
1318 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | |
1319 | return 0; | |
31840ae1 ZY |
1320 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, |
1321 | 0, ref_root, 0, ref_generation, | |
3bb1a1bc | 1322 | owner_objectid); |
925baedd CM |
1323 | return ret; |
1324 | } | |
1325 | ||
e9d0b13b CM |
1326 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
1327 | struct btrfs_root *root) | |
1328 | { | |
87ef2bb4 CM |
1329 | finish_current_insert(trans, root->fs_info->extent_root, 1); |
1330 | del_pending_extents(trans, root->fs_info->extent_root, 1); | |
e9d0b13b CM |
1331 | return 0; |
1332 | } | |
1333 | ||
31840ae1 ZY |
1334 | int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, |
1335 | struct btrfs_root *root, u64 bytenr, | |
1336 | u64 num_bytes, u32 *refs) | |
a28ec197 | 1337 | { |
5caf2a00 | 1338 | struct btrfs_path *path; |
a28ec197 | 1339 | int ret; |
e2fa7227 | 1340 | struct btrfs_key key; |
5f39d397 | 1341 | struct extent_buffer *l; |
234b63a0 | 1342 | struct btrfs_extent_item *item; |
5caf2a00 | 1343 | |
db94535d | 1344 | WARN_ON(num_bytes < root->sectorsize); |
5caf2a00 | 1345 | path = btrfs_alloc_path(); |
3c12ac72 | 1346 | path->reada = 1; |
db94535d CM |
1347 | key.objectid = bytenr; |
1348 | key.offset = num_bytes; | |
62e2749e | 1349 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
5caf2a00 | 1350 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 1351 | 0, 0); |
54aa1f4d CM |
1352 | if (ret < 0) |
1353 | goto out; | |
5f39d397 CM |
1354 | if (ret != 0) { |
1355 | btrfs_print_leaf(root, path->nodes[0]); | |
d397712b CM |
1356 | printk(KERN_INFO "btrfs failed to find block number %llu\n", |
1357 | (unsigned long long)bytenr); | |
a28ec197 | 1358 | BUG(); |
5f39d397 CM |
1359 | } |
1360 | l = path->nodes[0]; | |
5caf2a00 | 1361 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
5f39d397 | 1362 | *refs = btrfs_extent_refs(l, item); |
54aa1f4d | 1363 | out: |
5caf2a00 | 1364 | btrfs_free_path(path); |
a28ec197 CM |
1365 | return 0; |
1366 | } | |
1367 | ||
80ff3856 | 1368 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
17d217fe | 1369 | struct btrfs_root *root, u64 objectid, u64 bytenr) |
be20aa9d CM |
1370 | { |
1371 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
1372 | struct btrfs_path *path; | |
f321e491 YZ |
1373 | struct extent_buffer *leaf; |
1374 | struct btrfs_extent_ref *ref_item; | |
1375 | struct btrfs_key key; | |
1376 | struct btrfs_key found_key; | |
80ff3856 YZ |
1377 | u64 ref_root; |
1378 | u64 last_snapshot; | |
be20aa9d CM |
1379 | u32 nritems; |
1380 | int ret; | |
925baedd | 1381 | |
be20aa9d | 1382 | key.objectid = bytenr; |
31840ae1 | 1383 | key.offset = (u64)-1; |
f321e491 | 1384 | key.type = BTRFS_EXTENT_ITEM_KEY; |
be20aa9d | 1385 | |
f321e491 | 1386 | path = btrfs_alloc_path(); |
be20aa9d CM |
1387 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
1388 | if (ret < 0) | |
1389 | goto out; | |
1390 | BUG_ON(ret == 0); | |
80ff3856 YZ |
1391 | |
1392 | ret = -ENOENT; | |
1393 | if (path->slots[0] == 0) | |
31840ae1 | 1394 | goto out; |
be20aa9d | 1395 | |
31840ae1 | 1396 | path->slots[0]--; |
f321e491 YZ |
1397 | leaf = path->nodes[0]; |
1398 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
be20aa9d CM |
1399 | |
1400 | if (found_key.objectid != bytenr || | |
80ff3856 | 1401 | found_key.type != BTRFS_EXTENT_ITEM_KEY) |
be20aa9d | 1402 | goto out; |
f321e491 | 1403 | |
80ff3856 | 1404 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
be20aa9d | 1405 | while (1) { |
f321e491 YZ |
1406 | leaf = path->nodes[0]; |
1407 | nritems = btrfs_header_nritems(leaf); | |
be20aa9d CM |
1408 | if (path->slots[0] >= nritems) { |
1409 | ret = btrfs_next_leaf(extent_root, path); | |
f321e491 YZ |
1410 | if (ret < 0) |
1411 | goto out; | |
be20aa9d CM |
1412 | if (ret == 0) |
1413 | continue; | |
1414 | break; | |
1415 | } | |
f321e491 | 1416 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
be20aa9d CM |
1417 | if (found_key.objectid != bytenr) |
1418 | break; | |
bd09835d | 1419 | |
be20aa9d CM |
1420 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
1421 | path->slots[0]++; | |
1422 | continue; | |
1423 | } | |
1424 | ||
f321e491 | 1425 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
be20aa9d | 1426 | struct btrfs_extent_ref); |
80ff3856 | 1427 | ref_root = btrfs_ref_root(leaf, ref_item); |
17d217fe YZ |
1428 | if ((ref_root != root->root_key.objectid && |
1429 | ref_root != BTRFS_TREE_LOG_OBJECTID) || | |
1430 | objectid != btrfs_ref_objectid(leaf, ref_item)) { | |
80ff3856 | 1431 | ret = 1; |
f321e491 | 1432 | goto out; |
80ff3856 YZ |
1433 | } |
1434 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { | |
f321e491 YZ |
1435 | ret = 1; |
1436 | goto out; | |
1437 | } | |
80ff3856 YZ |
1438 | |
1439 | path->slots[0]++; | |
f321e491 YZ |
1440 | } |
1441 | ret = 0; | |
be20aa9d | 1442 | out: |
80ff3856 | 1443 | btrfs_free_path(path); |
f321e491 | 1444 | return ret; |
be20aa9d | 1445 | } |
c5739bba | 1446 | |
31840ae1 ZY |
1447 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
1448 | struct extent_buffer *buf, u32 nr_extents) | |
02217ed2 | 1449 | { |
5f39d397 | 1450 | struct btrfs_key key; |
6407bf6d | 1451 | struct btrfs_file_extent_item *fi; |
e4657689 ZY |
1452 | u64 root_gen; |
1453 | u32 nritems; | |
02217ed2 | 1454 | int i; |
db94535d | 1455 | int level; |
31840ae1 | 1456 | int ret = 0; |
e4657689 | 1457 | int shared = 0; |
a28ec197 | 1458 | |
3768f368 | 1459 | if (!root->ref_cows) |
a28ec197 | 1460 | return 0; |
5f39d397 | 1461 | |
e4657689 ZY |
1462 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
1463 | shared = 0; | |
1464 | root_gen = root->root_key.offset; | |
1465 | } else { | |
1466 | shared = 1; | |
1467 | root_gen = trans->transid - 1; | |
1468 | } | |
1469 | ||
db94535d | 1470 | level = btrfs_header_level(buf); |
5f39d397 | 1471 | nritems = btrfs_header_nritems(buf); |
4a096752 | 1472 | |
31840ae1 | 1473 | if (level == 0) { |
31153d81 YZ |
1474 | struct btrfs_leaf_ref *ref; |
1475 | struct btrfs_extent_info *info; | |
1476 | ||
31840ae1 | 1477 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
31153d81 | 1478 | if (!ref) { |
31840ae1 | 1479 | ret = -ENOMEM; |
31153d81 YZ |
1480 | goto out; |
1481 | } | |
1482 | ||
e4657689 | 1483 | ref->root_gen = root_gen; |
31153d81 YZ |
1484 | ref->bytenr = buf->start; |
1485 | ref->owner = btrfs_header_owner(buf); | |
1486 | ref->generation = btrfs_header_generation(buf); | |
31840ae1 | 1487 | ref->nritems = nr_extents; |
31153d81 | 1488 | info = ref->extents; |
bcc63abb | 1489 | |
31840ae1 | 1490 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
31153d81 YZ |
1491 | u64 disk_bytenr; |
1492 | btrfs_item_key_to_cpu(buf, &key, i); | |
1493 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
1494 | continue; | |
1495 | fi = btrfs_item_ptr(buf, i, | |
1496 | struct btrfs_file_extent_item); | |
1497 | if (btrfs_file_extent_type(buf, fi) == | |
1498 | BTRFS_FILE_EXTENT_INLINE) | |
1499 | continue; | |
1500 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | |
1501 | if (disk_bytenr == 0) | |
1502 | continue; | |
1503 | ||
1504 | info->bytenr = disk_bytenr; | |
1505 | info->num_bytes = | |
1506 | btrfs_file_extent_disk_num_bytes(buf, fi); | |
1507 | info->objectid = key.objectid; | |
1508 | info->offset = key.offset; | |
1509 | info++; | |
1510 | } | |
1511 | ||
e4657689 | 1512 | ret = btrfs_add_leaf_ref(root, ref, shared); |
5b84e8d6 YZ |
1513 | if (ret == -EEXIST && shared) { |
1514 | struct btrfs_leaf_ref *old; | |
1515 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); | |
1516 | BUG_ON(!old); | |
1517 | btrfs_remove_leaf_ref(root, old); | |
1518 | btrfs_free_leaf_ref(root, old); | |
1519 | ret = btrfs_add_leaf_ref(root, ref, shared); | |
1520 | } | |
31153d81 | 1521 | WARN_ON(ret); |
bcc63abb | 1522 | btrfs_free_leaf_ref(root, ref); |
31153d81 YZ |
1523 | } |
1524 | out: | |
31840ae1 ZY |
1525 | return ret; |
1526 | } | |
1527 | ||
1528 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
1529 | struct extent_buffer *orig_buf, struct extent_buffer *buf, | |
1530 | u32 *nr_extents) | |
1531 | { | |
1532 | u64 bytenr; | |
1533 | u64 ref_root; | |
1534 | u64 orig_root; | |
1535 | u64 ref_generation; | |
1536 | u64 orig_generation; | |
1537 | u32 nritems; | |
1538 | u32 nr_file_extents = 0; | |
1539 | struct btrfs_key key; | |
1540 | struct btrfs_file_extent_item *fi; | |
1541 | int i; | |
1542 | int level; | |
1543 | int ret = 0; | |
1544 | int faili = 0; | |
1545 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, | |
3bb1a1bc | 1546 | u64, u64, u64, u64, u64, u64, u64, u64); |
31840ae1 ZY |
1547 | |
1548 | ref_root = btrfs_header_owner(buf); | |
1549 | ref_generation = btrfs_header_generation(buf); | |
1550 | orig_root = btrfs_header_owner(orig_buf); | |
1551 | orig_generation = btrfs_header_generation(orig_buf); | |
1552 | ||
1553 | nritems = btrfs_header_nritems(buf); | |
1554 | level = btrfs_header_level(buf); | |
1555 | ||
1556 | if (root->ref_cows) { | |
1557 | process_func = __btrfs_inc_extent_ref; | |
1558 | } else { | |
1559 | if (level == 0 && | |
1560 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
1561 | goto out; | |
1562 | if (level != 0 && | |
1563 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) | |
1564 | goto out; | |
1565 | process_func = __btrfs_update_extent_ref; | |
1566 | } | |
1567 | ||
1568 | for (i = 0; i < nritems; i++) { | |
1569 | cond_resched(); | |
db94535d | 1570 | if (level == 0) { |
5f39d397 CM |
1571 | btrfs_item_key_to_cpu(buf, &key, i); |
1572 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
54aa1f4d | 1573 | continue; |
5f39d397 | 1574 | fi = btrfs_item_ptr(buf, i, |
54aa1f4d | 1575 | struct btrfs_file_extent_item); |
5f39d397 | 1576 | if (btrfs_file_extent_type(buf, fi) == |
54aa1f4d CM |
1577 | BTRFS_FILE_EXTENT_INLINE) |
1578 | continue; | |
31840ae1 ZY |
1579 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
1580 | if (bytenr == 0) | |
54aa1f4d | 1581 | continue; |
31840ae1 ZY |
1582 | |
1583 | nr_file_extents++; | |
1584 | ||
31840ae1 ZY |
1585 | ret = process_func(trans, root, bytenr, |
1586 | orig_buf->start, buf->start, | |
1587 | orig_root, ref_root, | |
1588 | orig_generation, ref_generation, | |
3bb1a1bc | 1589 | key.objectid); |
31840ae1 ZY |
1590 | |
1591 | if (ret) { | |
1592 | faili = i; | |
1593 | WARN_ON(1); | |
1594 | goto fail; | |
1595 | } | |
54aa1f4d | 1596 | } else { |
db94535d | 1597 | bytenr = btrfs_node_blockptr(buf, i); |
31840ae1 ZY |
1598 | ret = process_func(trans, root, bytenr, |
1599 | orig_buf->start, buf->start, | |
1600 | orig_root, ref_root, | |
1601 | orig_generation, ref_generation, | |
3bb1a1bc | 1602 | level - 1); |
31840ae1 ZY |
1603 | if (ret) { |
1604 | faili = i; | |
1605 | WARN_ON(1); | |
1606 | goto fail; | |
1607 | } | |
54aa1f4d CM |
1608 | } |
1609 | } | |
31840ae1 ZY |
1610 | out: |
1611 | if (nr_extents) { | |
1612 | if (level == 0) | |
1613 | *nr_extents = nr_file_extents; | |
1614 | else | |
1615 | *nr_extents = nritems; | |
1616 | } | |
1617 | return 0; | |
1618 | fail: | |
1619 | WARN_ON(1); | |
54aa1f4d | 1620 | return ret; |
02217ed2 CM |
1621 | } |
1622 | ||
31840ae1 ZY |
1623 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
1624 | struct btrfs_root *root, struct extent_buffer *orig_buf, | |
1625 | struct extent_buffer *buf, int start_slot, int nr) | |
1626 | ||
1627 | { | |
1628 | u64 bytenr; | |
1629 | u64 ref_root; | |
1630 | u64 orig_root; | |
1631 | u64 ref_generation; | |
1632 | u64 orig_generation; | |
1633 | struct btrfs_key key; | |
1634 | struct btrfs_file_extent_item *fi; | |
1635 | int i; | |
1636 | int ret; | |
1637 | int slot; | |
1638 | int level; | |
1639 | ||
1640 | BUG_ON(start_slot < 0); | |
1641 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); | |
1642 | ||
1643 | ref_root = btrfs_header_owner(buf); | |
1644 | ref_generation = btrfs_header_generation(buf); | |
1645 | orig_root = btrfs_header_owner(orig_buf); | |
1646 | orig_generation = btrfs_header_generation(orig_buf); | |
1647 | level = btrfs_header_level(buf); | |
1648 | ||
1649 | if (!root->ref_cows) { | |
1650 | if (level == 0 && | |
1651 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
1652 | return 0; | |
1653 | if (level != 0 && | |
1654 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) | |
1655 | return 0; | |
1656 | } | |
1657 | ||
1658 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { | |
1659 | cond_resched(); | |
1660 | if (level == 0) { | |
1661 | btrfs_item_key_to_cpu(buf, &key, slot); | |
1662 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
1663 | continue; | |
1664 | fi = btrfs_item_ptr(buf, slot, | |
1665 | struct btrfs_file_extent_item); | |
1666 | if (btrfs_file_extent_type(buf, fi) == | |
1667 | BTRFS_FILE_EXTENT_INLINE) | |
1668 | continue; | |
1669 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | |
1670 | if (bytenr == 0) | |
1671 | continue; | |
31840ae1 ZY |
1672 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
1673 | orig_buf->start, buf->start, | |
1674 | orig_root, ref_root, | |
1675 | orig_generation, ref_generation, | |
3bb1a1bc | 1676 | key.objectid); |
31840ae1 ZY |
1677 | if (ret) |
1678 | goto fail; | |
1679 | } else { | |
1680 | bytenr = btrfs_node_blockptr(buf, slot); | |
31840ae1 ZY |
1681 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
1682 | orig_buf->start, buf->start, | |
1683 | orig_root, ref_root, | |
1684 | orig_generation, ref_generation, | |
3bb1a1bc | 1685 | level - 1); |
31840ae1 ZY |
1686 | if (ret) |
1687 | goto fail; | |
1688 | } | |
1689 | } | |
1690 | return 0; | |
1691 | fail: | |
1692 | WARN_ON(1); | |
1693 | return -1; | |
1694 | } | |
1695 | ||
9078a3e1 CM |
1696 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
1697 | struct btrfs_root *root, | |
1698 | struct btrfs_path *path, | |
1699 | struct btrfs_block_group_cache *cache) | |
1700 | { | |
1701 | int ret; | |
1702 | int pending_ret; | |
1703 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
5f39d397 CM |
1704 | unsigned long bi; |
1705 | struct extent_buffer *leaf; | |
9078a3e1 | 1706 | |
9078a3e1 | 1707 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
54aa1f4d CM |
1708 | if (ret < 0) |
1709 | goto fail; | |
9078a3e1 | 1710 | BUG_ON(ret); |
5f39d397 CM |
1711 | |
1712 | leaf = path->nodes[0]; | |
1713 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
1714 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); | |
1715 | btrfs_mark_buffer_dirty(leaf); | |
9078a3e1 | 1716 | btrfs_release_path(extent_root, path); |
54aa1f4d | 1717 | fail: |
87ef2bb4 CM |
1718 | finish_current_insert(trans, extent_root, 0); |
1719 | pending_ret = del_pending_extents(trans, extent_root, 0); | |
9078a3e1 CM |
1720 | if (ret) |
1721 | return ret; | |
1722 | if (pending_ret) | |
1723 | return pending_ret; | |
1724 | return 0; | |
1725 | ||
1726 | } | |
1727 | ||
96b5179d CM |
1728 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
1729 | struct btrfs_root *root) | |
9078a3e1 | 1730 | { |
0f9dd46c JB |
1731 | struct btrfs_block_group_cache *cache, *entry; |
1732 | struct rb_node *n; | |
9078a3e1 CM |
1733 | int err = 0; |
1734 | int werr = 0; | |
9078a3e1 | 1735 | struct btrfs_path *path; |
96b5179d | 1736 | u64 last = 0; |
9078a3e1 CM |
1737 | |
1738 | path = btrfs_alloc_path(); | |
1739 | if (!path) | |
1740 | return -ENOMEM; | |
1741 | ||
d397712b | 1742 | while (1) { |
0f9dd46c JB |
1743 | cache = NULL; |
1744 | spin_lock(&root->fs_info->block_group_cache_lock); | |
1745 | for (n = rb_first(&root->fs_info->block_group_cache_tree); | |
1746 | n; n = rb_next(n)) { | |
1747 | entry = rb_entry(n, struct btrfs_block_group_cache, | |
1748 | cache_node); | |
1749 | if (entry->dirty) { | |
1750 | cache = entry; | |
1751 | break; | |
1752 | } | |
1753 | } | |
1754 | spin_unlock(&root->fs_info->block_group_cache_lock); | |
54aa1f4d | 1755 | |
0f9dd46c | 1756 | if (!cache) |
96b5179d | 1757 | break; |
0f9dd46c | 1758 | |
e8569813 | 1759 | cache->dirty = 0; |
0f9dd46c JB |
1760 | last += cache->key.offset; |
1761 | ||
96b5179d CM |
1762 | err = write_one_cache_group(trans, root, |
1763 | path, cache); | |
1764 | /* | |
1765 | * if we fail to write the cache group, we want | |
1766 | * to keep it marked dirty in hopes that a later | |
1767 | * write will work | |
1768 | */ | |
1769 | if (err) { | |
1770 | werr = err; | |
1771 | continue; | |
9078a3e1 CM |
1772 | } |
1773 | } | |
1774 | btrfs_free_path(path); | |
1775 | return werr; | |
1776 | } | |
1777 | ||
d2fb3437 YZ |
1778 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
1779 | { | |
1780 | struct btrfs_block_group_cache *block_group; | |
1781 | int readonly = 0; | |
1782 | ||
1783 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); | |
1784 | if (!block_group || block_group->ro) | |
1785 | readonly = 1; | |
1786 | if (block_group) | |
1787 | put_block_group(block_group); | |
1788 | return readonly; | |
1789 | } | |
1790 | ||
593060d7 CM |
1791 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
1792 | u64 total_bytes, u64 bytes_used, | |
1793 | struct btrfs_space_info **space_info) | |
1794 | { | |
1795 | struct btrfs_space_info *found; | |
1796 | ||
1797 | found = __find_space_info(info, flags); | |
1798 | if (found) { | |
25179201 | 1799 | spin_lock(&found->lock); |
593060d7 CM |
1800 | found->total_bytes += total_bytes; |
1801 | found->bytes_used += bytes_used; | |
8f18cf13 | 1802 | found->full = 0; |
25179201 | 1803 | spin_unlock(&found->lock); |
593060d7 CM |
1804 | *space_info = found; |
1805 | return 0; | |
1806 | } | |
c146afad | 1807 | found = kzalloc(sizeof(*found), GFP_NOFS); |
593060d7 CM |
1808 | if (!found) |
1809 | return -ENOMEM; | |
1810 | ||
1811 | list_add(&found->list, &info->space_info); | |
0f9dd46c | 1812 | INIT_LIST_HEAD(&found->block_groups); |
80eb234a | 1813 | init_rwsem(&found->groups_sem); |
0f9dd46c | 1814 | spin_lock_init(&found->lock); |
593060d7 CM |
1815 | found->flags = flags; |
1816 | found->total_bytes = total_bytes; | |
1817 | found->bytes_used = bytes_used; | |
1818 | found->bytes_pinned = 0; | |
e8569813 | 1819 | found->bytes_reserved = 0; |
c146afad | 1820 | found->bytes_readonly = 0; |
593060d7 | 1821 | found->full = 0; |
0ef3e66b | 1822 | found->force_alloc = 0; |
593060d7 CM |
1823 | *space_info = found; |
1824 | return 0; | |
1825 | } | |
1826 | ||
8790d502 CM |
1827 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
1828 | { | |
1829 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | | |
611f0e00 | 1830 | BTRFS_BLOCK_GROUP_RAID1 | |
321aecc6 | 1831 | BTRFS_BLOCK_GROUP_RAID10 | |
611f0e00 | 1832 | BTRFS_BLOCK_GROUP_DUP); |
8790d502 CM |
1833 | if (extra_flags) { |
1834 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
1835 | fs_info->avail_data_alloc_bits |= extra_flags; | |
1836 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
1837 | fs_info->avail_metadata_alloc_bits |= extra_flags; | |
1838 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
1839 | fs_info->avail_system_alloc_bits |= extra_flags; | |
1840 | } | |
1841 | } | |
593060d7 | 1842 | |
c146afad YZ |
1843 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
1844 | { | |
1845 | spin_lock(&cache->space_info->lock); | |
1846 | spin_lock(&cache->lock); | |
1847 | if (!cache->ro) { | |
1848 | cache->space_info->bytes_readonly += cache->key.offset - | |
1849 | btrfs_block_group_used(&cache->item); | |
1850 | cache->ro = 1; | |
1851 | } | |
1852 | spin_unlock(&cache->lock); | |
1853 | spin_unlock(&cache->space_info->lock); | |
1854 | } | |
1855 | ||
2b82032c | 1856 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
ec44a35c | 1857 | { |
2b82032c | 1858 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
a061fc8d CM |
1859 | |
1860 | if (num_devices == 1) | |
1861 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); | |
1862 | if (num_devices < 4) | |
1863 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; | |
1864 | ||
ec44a35c CM |
1865 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
1866 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
a061fc8d | 1867 | BTRFS_BLOCK_GROUP_RAID10))) { |
ec44a35c | 1868 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
a061fc8d | 1869 | } |
ec44a35c CM |
1870 | |
1871 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && | |
a061fc8d | 1872 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
ec44a35c | 1873 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
a061fc8d | 1874 | } |
ec44a35c CM |
1875 | |
1876 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && | |
1877 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | | |
1878 | (flags & BTRFS_BLOCK_GROUP_RAID10) | | |
1879 | (flags & BTRFS_BLOCK_GROUP_DUP))) | |
1880 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; | |
1881 | return flags; | |
1882 | } | |
1883 | ||
6324fbf3 CM |
1884 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
1885 | struct btrfs_root *extent_root, u64 alloc_bytes, | |
0ef3e66b | 1886 | u64 flags, int force) |
6324fbf3 CM |
1887 | { |
1888 | struct btrfs_space_info *space_info; | |
1889 | u64 thresh; | |
c146afad YZ |
1890 | int ret = 0; |
1891 | ||
1892 | mutex_lock(&extent_root->fs_info->chunk_mutex); | |
6324fbf3 | 1893 | |
2b82032c | 1894 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
ec44a35c | 1895 | |
6324fbf3 | 1896 | space_info = __find_space_info(extent_root->fs_info, flags); |
593060d7 CM |
1897 | if (!space_info) { |
1898 | ret = update_space_info(extent_root->fs_info, flags, | |
1899 | 0, 0, &space_info); | |
1900 | BUG_ON(ret); | |
1901 | } | |
6324fbf3 CM |
1902 | BUG_ON(!space_info); |
1903 | ||
25179201 | 1904 | spin_lock(&space_info->lock); |
0ef3e66b CM |
1905 | if (space_info->force_alloc) { |
1906 | force = 1; | |
1907 | space_info->force_alloc = 0; | |
1908 | } | |
25179201 JB |
1909 | if (space_info->full) { |
1910 | spin_unlock(&space_info->lock); | |
925baedd | 1911 | goto out; |
25179201 | 1912 | } |
6324fbf3 | 1913 | |
c146afad YZ |
1914 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
1915 | thresh = div_factor(thresh, 6); | |
0ef3e66b | 1916 | if (!force && |
e8569813 | 1917 | (space_info->bytes_used + space_info->bytes_pinned + |
25179201 JB |
1918 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
1919 | spin_unlock(&space_info->lock); | |
925baedd | 1920 | goto out; |
25179201 | 1921 | } |
25179201 JB |
1922 | spin_unlock(&space_info->lock); |
1923 | ||
2b82032c | 1924 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
d397712b | 1925 | if (ret) |
6324fbf3 | 1926 | space_info->full = 1; |
a74a4b97 | 1927 | out: |
c146afad | 1928 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
0f9dd46c | 1929 | return ret; |
6324fbf3 CM |
1930 | } |
1931 | ||
9078a3e1 CM |
1932 | static int update_block_group(struct btrfs_trans_handle *trans, |
1933 | struct btrfs_root *root, | |
db94535d | 1934 | u64 bytenr, u64 num_bytes, int alloc, |
0b86a832 | 1935 | int mark_free) |
9078a3e1 CM |
1936 | { |
1937 | struct btrfs_block_group_cache *cache; | |
1938 | struct btrfs_fs_info *info = root->fs_info; | |
db94535d | 1939 | u64 total = num_bytes; |
9078a3e1 | 1940 | u64 old_val; |
db94535d | 1941 | u64 byte_in_group; |
3e1ad54f | 1942 | |
d397712b | 1943 | while (total) { |
db94535d | 1944 | cache = btrfs_lookup_block_group(info, bytenr); |
f3465ca4 | 1945 | if (!cache) |
9078a3e1 | 1946 | return -1; |
db94535d CM |
1947 | byte_in_group = bytenr - cache->key.objectid; |
1948 | WARN_ON(byte_in_group > cache->key.offset); | |
9078a3e1 | 1949 | |
25179201 | 1950 | spin_lock(&cache->space_info->lock); |
c286ac48 | 1951 | spin_lock(&cache->lock); |
0f9dd46c | 1952 | cache->dirty = 1; |
9078a3e1 | 1953 | old_val = btrfs_block_group_used(&cache->item); |
db94535d | 1954 | num_bytes = min(total, cache->key.offset - byte_in_group); |
cd1bc465 | 1955 | if (alloc) { |
db94535d | 1956 | old_val += num_bytes; |
6324fbf3 | 1957 | cache->space_info->bytes_used += num_bytes; |
a512bbf8 | 1958 | if (cache->ro) |
c146afad | 1959 | cache->space_info->bytes_readonly -= num_bytes; |
c286ac48 CM |
1960 | btrfs_set_block_group_used(&cache->item, old_val); |
1961 | spin_unlock(&cache->lock); | |
25179201 | 1962 | spin_unlock(&cache->space_info->lock); |
cd1bc465 | 1963 | } else { |
db94535d | 1964 | old_val -= num_bytes; |
6324fbf3 | 1965 | cache->space_info->bytes_used -= num_bytes; |
c146afad YZ |
1966 | if (cache->ro) |
1967 | cache->space_info->bytes_readonly += num_bytes; | |
c286ac48 CM |
1968 | btrfs_set_block_group_used(&cache->item, old_val); |
1969 | spin_unlock(&cache->lock); | |
25179201 | 1970 | spin_unlock(&cache->space_info->lock); |
f510cfec | 1971 | if (mark_free) { |
0f9dd46c | 1972 | int ret; |
1f3c79a2 LH |
1973 | |
1974 | ret = btrfs_discard_extent(root, bytenr, | |
1975 | num_bytes); | |
1976 | WARN_ON(ret); | |
1977 | ||
0f9dd46c JB |
1978 | ret = btrfs_add_free_space(cache, bytenr, |
1979 | num_bytes); | |
d2fb3437 | 1980 | WARN_ON(ret); |
e37c9e69 | 1981 | } |
cd1bc465 | 1982 | } |
d2fb3437 | 1983 | put_block_group(cache); |
db94535d CM |
1984 | total -= num_bytes; |
1985 | bytenr += num_bytes; | |
9078a3e1 CM |
1986 | } |
1987 | return 0; | |
1988 | } | |
6324fbf3 | 1989 | |
a061fc8d CM |
1990 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
1991 | { | |
0f9dd46c | 1992 | struct btrfs_block_group_cache *cache; |
d2fb3437 | 1993 | u64 bytenr; |
0f9dd46c JB |
1994 | |
1995 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); | |
1996 | if (!cache) | |
a061fc8d | 1997 | return 0; |
0f9dd46c | 1998 | |
d2fb3437 YZ |
1999 | bytenr = cache->key.objectid; |
2000 | put_block_group(cache); | |
2001 | ||
2002 | return bytenr; | |
a061fc8d CM |
2003 | } |
2004 | ||
e02119d5 | 2005 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
324ae4df Y |
2006 | u64 bytenr, u64 num, int pin) |
2007 | { | |
2008 | u64 len; | |
2009 | struct btrfs_block_group_cache *cache; | |
2010 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2011 | ||
25179201 | 2012 | WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex)); |
324ae4df Y |
2013 | if (pin) { |
2014 | set_extent_dirty(&fs_info->pinned_extents, | |
2015 | bytenr, bytenr + num - 1, GFP_NOFS); | |
2016 | } else { | |
2017 | clear_extent_dirty(&fs_info->pinned_extents, | |
2018 | bytenr, bytenr + num - 1, GFP_NOFS); | |
2019 | } | |
2020 | while (num > 0) { | |
2021 | cache = btrfs_lookup_block_group(fs_info, bytenr); | |
e8569813 ZY |
2022 | BUG_ON(!cache); |
2023 | len = min(num, cache->key.offset - | |
2024 | (bytenr - cache->key.objectid)); | |
324ae4df | 2025 | if (pin) { |
25179201 | 2026 | spin_lock(&cache->space_info->lock); |
e8569813 ZY |
2027 | spin_lock(&cache->lock); |
2028 | cache->pinned += len; | |
2029 | cache->space_info->bytes_pinned += len; | |
2030 | spin_unlock(&cache->lock); | |
25179201 | 2031 | spin_unlock(&cache->space_info->lock); |
324ae4df Y |
2032 | fs_info->total_pinned += len; |
2033 | } else { | |
25179201 | 2034 | spin_lock(&cache->space_info->lock); |
e8569813 ZY |
2035 | spin_lock(&cache->lock); |
2036 | cache->pinned -= len; | |
2037 | cache->space_info->bytes_pinned -= len; | |
2038 | spin_unlock(&cache->lock); | |
25179201 | 2039 | spin_unlock(&cache->space_info->lock); |
324ae4df | 2040 | fs_info->total_pinned -= len; |
07103a3c JB |
2041 | if (cache->cached) |
2042 | btrfs_add_free_space(cache, bytenr, len); | |
324ae4df | 2043 | } |
d2fb3437 | 2044 | put_block_group(cache); |
324ae4df Y |
2045 | bytenr += len; |
2046 | num -= len; | |
2047 | } | |
2048 | return 0; | |
2049 | } | |
9078a3e1 | 2050 | |
e8569813 ZY |
2051 | static int update_reserved_extents(struct btrfs_root *root, |
2052 | u64 bytenr, u64 num, int reserve) | |
2053 | { | |
2054 | u64 len; | |
2055 | struct btrfs_block_group_cache *cache; | |
2056 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2057 | ||
e8569813 ZY |
2058 | while (num > 0) { |
2059 | cache = btrfs_lookup_block_group(fs_info, bytenr); | |
2060 | BUG_ON(!cache); | |
2061 | len = min(num, cache->key.offset - | |
2062 | (bytenr - cache->key.objectid)); | |
25179201 JB |
2063 | |
2064 | spin_lock(&cache->space_info->lock); | |
2065 | spin_lock(&cache->lock); | |
e8569813 | 2066 | if (reserve) { |
e8569813 ZY |
2067 | cache->reserved += len; |
2068 | cache->space_info->bytes_reserved += len; | |
e8569813 | 2069 | } else { |
e8569813 ZY |
2070 | cache->reserved -= len; |
2071 | cache->space_info->bytes_reserved -= len; | |
e8569813 | 2072 | } |
25179201 JB |
2073 | spin_unlock(&cache->lock); |
2074 | spin_unlock(&cache->space_info->lock); | |
d2fb3437 | 2075 | put_block_group(cache); |
e8569813 ZY |
2076 | bytenr += len; |
2077 | num -= len; | |
2078 | } | |
2079 | return 0; | |
2080 | } | |
2081 | ||
d1310b2e | 2082 | int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy) |
ccd467d6 | 2083 | { |
ccd467d6 | 2084 | u64 last = 0; |
1a5bc167 CM |
2085 | u64 start; |
2086 | u64 end; | |
d1310b2e | 2087 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
ccd467d6 | 2088 | int ret; |
ccd467d6 | 2089 | |
25179201 | 2090 | mutex_lock(&root->fs_info->pinned_mutex); |
d397712b | 2091 | while (1) { |
1a5bc167 CM |
2092 | ret = find_first_extent_bit(pinned_extents, last, |
2093 | &start, &end, EXTENT_DIRTY); | |
2094 | if (ret) | |
ccd467d6 | 2095 | break; |
1a5bc167 CM |
2096 | set_extent_dirty(copy, start, end, GFP_NOFS); |
2097 | last = end + 1; | |
ccd467d6 | 2098 | } |
25179201 | 2099 | mutex_unlock(&root->fs_info->pinned_mutex); |
ccd467d6 CM |
2100 | return 0; |
2101 | } | |
2102 | ||
2103 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |
2104 | struct btrfs_root *root, | |
d1310b2e | 2105 | struct extent_io_tree *unpin) |
a28ec197 | 2106 | { |
1a5bc167 CM |
2107 | u64 start; |
2108 | u64 end; | |
a28ec197 | 2109 | int ret; |
a28ec197 | 2110 | |
25179201 | 2111 | mutex_lock(&root->fs_info->pinned_mutex); |
d397712b | 2112 | while (1) { |
1a5bc167 CM |
2113 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
2114 | EXTENT_DIRTY); | |
2115 | if (ret) | |
a28ec197 | 2116 | break; |
1f3c79a2 LH |
2117 | |
2118 | ret = btrfs_discard_extent(root, start, end + 1 - start); | |
2119 | ||
e02119d5 | 2120 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
1a5bc167 | 2121 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
1f3c79a2 | 2122 | |
c286ac48 | 2123 | if (need_resched()) { |
25179201 | 2124 | mutex_unlock(&root->fs_info->pinned_mutex); |
c286ac48 | 2125 | cond_resched(); |
25179201 | 2126 | mutex_lock(&root->fs_info->pinned_mutex); |
c286ac48 | 2127 | } |
a28ec197 | 2128 | } |
25179201 | 2129 | mutex_unlock(&root->fs_info->pinned_mutex); |
1f3c79a2 | 2130 | return ret; |
a28ec197 CM |
2131 | } |
2132 | ||
98ed5174 | 2133 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
87ef2bb4 | 2134 | struct btrfs_root *extent_root, int all) |
037e6390 | 2135 | { |
7bb86316 CM |
2136 | u64 start; |
2137 | u64 end; | |
31840ae1 | 2138 | u64 priv; |
25179201 | 2139 | u64 search = 0; |
f3465ca4 | 2140 | u64 skipped = 0; |
7bb86316 CM |
2141 | struct btrfs_fs_info *info = extent_root->fs_info; |
2142 | struct btrfs_path *path; | |
f3465ca4 JB |
2143 | struct pending_extent_op *extent_op, *tmp; |
2144 | struct list_head insert_list, update_list; | |
037e6390 | 2145 | int ret; |
f3465ca4 | 2146 | int num_inserts = 0, max_inserts; |
037e6390 | 2147 | |
7bb86316 | 2148 | path = btrfs_alloc_path(); |
f3465ca4 JB |
2149 | INIT_LIST_HEAD(&insert_list); |
2150 | INIT_LIST_HEAD(&update_list); | |
037e6390 | 2151 | |
f3465ca4 JB |
2152 | max_inserts = extent_root->leafsize / |
2153 | (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) + | |
2154 | sizeof(struct btrfs_extent_ref) + | |
2155 | sizeof(struct btrfs_extent_item)); | |
2156 | again: | |
2157 | mutex_lock(&info->extent_ins_mutex); | |
2158 | while (1) { | |
25179201 JB |
2159 | ret = find_first_extent_bit(&info->extent_ins, search, &start, |
2160 | &end, EXTENT_WRITEBACK); | |
2161 | if (ret) { | |
f3465ca4 JB |
2162 | if (skipped && all && !num_inserts) { |
2163 | skipped = 0; | |
b4eec2ca | 2164 | search = 0; |
25179201 JB |
2165 | continue; |
2166 | } | |
f3465ca4 | 2167 | mutex_unlock(&info->extent_ins_mutex); |
26b8003f | 2168 | break; |
25179201 JB |
2169 | } |
2170 | ||
2171 | ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS); | |
2172 | if (!ret) { | |
f3465ca4 | 2173 | skipped = 1; |
87ef2bb4 | 2174 | search = end + 1; |
f3465ca4 JB |
2175 | if (need_resched()) { |
2176 | mutex_unlock(&info->extent_ins_mutex); | |
2177 | cond_resched(); | |
2178 | mutex_lock(&info->extent_ins_mutex); | |
2179 | } | |
25179201 JB |
2180 | continue; |
2181 | } | |
26b8003f | 2182 | |
31840ae1 ZY |
2183 | ret = get_state_private(&info->extent_ins, start, &priv); |
2184 | BUG_ON(ret); | |
f3465ca4 | 2185 | extent_op = (struct pending_extent_op *)(unsigned long) priv; |
25179201 | 2186 | |
31840ae1 | 2187 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
f3465ca4 JB |
2188 | num_inserts++; |
2189 | list_add_tail(&extent_op->list, &insert_list); | |
2190 | search = end + 1; | |
2191 | if (num_inserts == max_inserts) { | |
2192 | mutex_unlock(&info->extent_ins_mutex); | |
2193 | break; | |
2194 | } | |
2195 | } else if (extent_op->type == PENDING_BACKREF_UPDATE) { | |
2196 | list_add_tail(&extent_op->list, &update_list); | |
2197 | search = end + 1; | |
2198 | } else { | |
2199 | BUG(); | |
2200 | } | |
2201 | } | |
c286ac48 | 2202 | |
f3465ca4 | 2203 | /* |
b4eec2ca | 2204 | * process the update list, clear the writeback bit for it, and if |
f3465ca4 JB |
2205 | * somebody marked this thing for deletion then just unlock it and be |
2206 | * done, the free_extents will handle it | |
2207 | */ | |
2208 | mutex_lock(&info->extent_ins_mutex); | |
2209 | list_for_each_entry_safe(extent_op, tmp, &update_list, list) { | |
2210 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, | |
2211 | extent_op->bytenr + extent_op->num_bytes - 1, | |
2212 | EXTENT_WRITEBACK, GFP_NOFS); | |
2213 | if (extent_op->del) { | |
2214 | list_del_init(&extent_op->list); | |
2215 | unlock_extent(&info->extent_ins, extent_op->bytenr, | |
2216 | extent_op->bytenr + extent_op->num_bytes | |
2217 | - 1, GFP_NOFS); | |
2218 | kfree(extent_op); | |
2219 | } | |
2220 | } | |
2221 | mutex_unlock(&info->extent_ins_mutex); | |
c286ac48 | 2222 | |
f3465ca4 JB |
2223 | /* |
2224 | * still have things left on the update list, go ahead an update | |
2225 | * everything | |
2226 | */ | |
2227 | if (!list_empty(&update_list)) { | |
2228 | ret = update_backrefs(trans, extent_root, path, &update_list); | |
2229 | BUG_ON(ret); | |
2230 | } | |
c286ac48 | 2231 | |
f3465ca4 JB |
2232 | /* |
2233 | * if no inserts need to be done, but we skipped some extents and we | |
2234 | * need to make sure everything is cleaned then reset everything and | |
2235 | * go back to the beginning | |
2236 | */ | |
2237 | if (!num_inserts && all && skipped) { | |
2238 | search = 0; | |
2239 | skipped = 0; | |
2240 | INIT_LIST_HEAD(&update_list); | |
2241 | INIT_LIST_HEAD(&insert_list); | |
2242 | goto again; | |
2243 | } else if (!num_inserts) { | |
2244 | goto out; | |
2245 | } | |
31840ae1 | 2246 | |
f3465ca4 JB |
2247 | /* |
2248 | * process the insert extents list. Again if we are deleting this | |
2249 | * extent, then just unlock it, pin down the bytes if need be, and be | |
2250 | * done with it. Saves us from having to actually insert the extent | |
2251 | * into the tree and then subsequently come along and delete it | |
2252 | */ | |
2253 | mutex_lock(&info->extent_ins_mutex); | |
2254 | list_for_each_entry_safe(extent_op, tmp, &insert_list, list) { | |
2255 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, | |
2256 | extent_op->bytenr + extent_op->num_bytes - 1, | |
2257 | EXTENT_WRITEBACK, GFP_NOFS); | |
2258 | if (extent_op->del) { | |
34bf63c4 | 2259 | u64 used; |
f3465ca4 JB |
2260 | list_del_init(&extent_op->list); |
2261 | unlock_extent(&info->extent_ins, extent_op->bytenr, | |
2262 | extent_op->bytenr + extent_op->num_bytes | |
2263 | - 1, GFP_NOFS); | |
2264 | ||
2265 | mutex_lock(&extent_root->fs_info->pinned_mutex); | |
2266 | ret = pin_down_bytes(trans, extent_root, | |
2267 | extent_op->bytenr, | |
2268 | extent_op->num_bytes, 0); | |
2269 | mutex_unlock(&extent_root->fs_info->pinned_mutex); | |
2270 | ||
34bf63c4 YZ |
2271 | spin_lock(&info->delalloc_lock); |
2272 | used = btrfs_super_bytes_used(&info->super_copy); | |
2273 | btrfs_set_super_bytes_used(&info->super_copy, | |
2274 | used - extent_op->num_bytes); | |
2275 | used = btrfs_root_used(&extent_root->root_item); | |
2276 | btrfs_set_root_used(&extent_root->root_item, | |
2277 | used - extent_op->num_bytes); | |
2278 | spin_unlock(&info->delalloc_lock); | |
2279 | ||
f3465ca4 JB |
2280 | ret = update_block_group(trans, extent_root, |
2281 | extent_op->bytenr, | |
2282 | extent_op->num_bytes, | |
2283 | 0, ret > 0); | |
2284 | BUG_ON(ret); | |
2285 | kfree(extent_op); | |
2286 | num_inserts--; | |
d8d5f3e1 | 2287 | } |
f3465ca4 JB |
2288 | } |
2289 | mutex_unlock(&info->extent_ins_mutex); | |
31840ae1 | 2290 | |
f3465ca4 JB |
2291 | ret = insert_extents(trans, extent_root, path, &insert_list, |
2292 | num_inserts); | |
2293 | BUG_ON(ret); | |
2294 | ||
2295 | /* | |
2296 | * if we broke out of the loop in order to insert stuff because we hit | |
2297 | * the maximum number of inserts at a time we can handle, then loop | |
2298 | * back and pick up where we left off | |
2299 | */ | |
2300 | if (num_inserts == max_inserts) { | |
2301 | INIT_LIST_HEAD(&insert_list); | |
2302 | INIT_LIST_HEAD(&update_list); | |
2303 | num_inserts = 0; | |
2304 | goto again; | |
2305 | } | |
2306 | ||
2307 | /* | |
2308 | * again, if we need to make absolutely sure there are no more pending | |
2309 | * extent operations left and we know that we skipped some, go back to | |
2310 | * the beginning and do it all again | |
2311 | */ | |
2312 | if (all && skipped) { | |
2313 | INIT_LIST_HEAD(&insert_list); | |
2314 | INIT_LIST_HEAD(&update_list); | |
2315 | search = 0; | |
2316 | skipped = 0; | |
2317 | num_inserts = 0; | |
2318 | goto again; | |
037e6390 | 2319 | } |
f3465ca4 | 2320 | out: |
7bb86316 | 2321 | btrfs_free_path(path); |
037e6390 CM |
2322 | return 0; |
2323 | } | |
2324 | ||
31840ae1 ZY |
2325 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
2326 | struct btrfs_root *root, | |
2327 | u64 bytenr, u64 num_bytes, int is_data) | |
e20d96d6 | 2328 | { |
1a5bc167 | 2329 | int err = 0; |
31840ae1 | 2330 | struct extent_buffer *buf; |
8ef97622 | 2331 | |
31840ae1 ZY |
2332 | if (is_data) |
2333 | goto pinit; | |
2334 | ||
2335 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); | |
2336 | if (!buf) | |
2337 | goto pinit; | |
2338 | ||
2339 | /* we can reuse a block if it hasn't been written | |
2340 | * and it is from this transaction. We can't | |
2341 | * reuse anything from the tree log root because | |
2342 | * it has tiny sub-transactions. | |
2343 | */ | |
2344 | if (btrfs_buffer_uptodate(buf, 0) && | |
2345 | btrfs_try_tree_lock(buf)) { | |
2346 | u64 header_owner = btrfs_header_owner(buf); | |
2347 | u64 header_transid = btrfs_header_generation(buf); | |
2348 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && | |
1a40e23b | 2349 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
31840ae1 ZY |
2350 | header_transid == trans->transid && |
2351 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { | |
2352 | clean_tree_block(NULL, root, buf); | |
2353 | btrfs_tree_unlock(buf); | |
5f39d397 | 2354 | free_extent_buffer(buf); |
31840ae1 | 2355 | return 1; |
8ef97622 | 2356 | } |
31840ae1 | 2357 | btrfs_tree_unlock(buf); |
f4b9aa8d | 2358 | } |
31840ae1 ZY |
2359 | free_extent_buffer(buf); |
2360 | pinit: | |
2361 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); | |
2362 | ||
be744175 | 2363 | BUG_ON(err < 0); |
e20d96d6 CM |
2364 | return 0; |
2365 | } | |
2366 | ||
fec577fb | 2367 | /* |
a28ec197 | 2368 | * remove an extent from the root, returns 0 on success |
fec577fb | 2369 | */ |
31840ae1 ZY |
2370 | static int __free_extent(struct btrfs_trans_handle *trans, |
2371 | struct btrfs_root *root, | |
2372 | u64 bytenr, u64 num_bytes, u64 parent, | |
7bb86316 | 2373 | u64 root_objectid, u64 ref_generation, |
3bb1a1bc | 2374 | u64 owner_objectid, int pin, int mark_free) |
a28ec197 | 2375 | { |
5caf2a00 | 2376 | struct btrfs_path *path; |
e2fa7227 | 2377 | struct btrfs_key key; |
1261ec42 CM |
2378 | struct btrfs_fs_info *info = root->fs_info; |
2379 | struct btrfs_root *extent_root = info->extent_root; | |
5f39d397 | 2380 | struct extent_buffer *leaf; |
a28ec197 | 2381 | int ret; |
952fccac CM |
2382 | int extent_slot = 0; |
2383 | int found_extent = 0; | |
2384 | int num_to_del = 1; | |
234b63a0 | 2385 | struct btrfs_extent_item *ei; |
cf27e1ee | 2386 | u32 refs; |
037e6390 | 2387 | |
db94535d | 2388 | key.objectid = bytenr; |
62e2749e | 2389 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
db94535d | 2390 | key.offset = num_bytes; |
5caf2a00 | 2391 | path = btrfs_alloc_path(); |
54aa1f4d CM |
2392 | if (!path) |
2393 | return -ENOMEM; | |
5f26f772 | 2394 | |
3c12ac72 | 2395 | path->reada = 1; |
3bb1a1bc YZ |
2396 | ret = lookup_extent_backref(trans, extent_root, path, |
2397 | bytenr, parent, root_objectid, | |
2398 | ref_generation, owner_objectid, 1); | |
7bb86316 | 2399 | if (ret == 0) { |
952fccac CM |
2400 | struct btrfs_key found_key; |
2401 | extent_slot = path->slots[0]; | |
d397712b | 2402 | while (extent_slot > 0) { |
952fccac CM |
2403 | extent_slot--; |
2404 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
2405 | extent_slot); | |
2406 | if (found_key.objectid != bytenr) | |
2407 | break; | |
2408 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && | |
2409 | found_key.offset == num_bytes) { | |
2410 | found_extent = 1; | |
2411 | break; | |
2412 | } | |
2413 | if (path->slots[0] - extent_slot > 5) | |
2414 | break; | |
2415 | } | |
31840ae1 ZY |
2416 | if (!found_extent) { |
2417 | ret = remove_extent_backref(trans, extent_root, path); | |
2418 | BUG_ON(ret); | |
2419 | btrfs_release_path(extent_root, path); | |
2420 | ret = btrfs_search_slot(trans, extent_root, | |
2421 | &key, path, -1, 1); | |
f3465ca4 JB |
2422 | if (ret) { |
2423 | printk(KERN_ERR "umm, got %d back from search" | |
d397712b CM |
2424 | ", was looking for %llu\n", ret, |
2425 | (unsigned long long)bytenr); | |
f3465ca4 JB |
2426 | btrfs_print_leaf(extent_root, path->nodes[0]); |
2427 | } | |
31840ae1 ZY |
2428 | BUG_ON(ret); |
2429 | extent_slot = path->slots[0]; | |
2430 | } | |
7bb86316 CM |
2431 | } else { |
2432 | btrfs_print_leaf(extent_root, path->nodes[0]); | |
2433 | WARN_ON(1); | |
d397712b CM |
2434 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
2435 | "root %llu gen %llu owner %llu\n", | |
2436 | (unsigned long long)bytenr, | |
2437 | (unsigned long long)root_objectid, | |
2438 | (unsigned long long)ref_generation, | |
2439 | (unsigned long long)owner_objectid); | |
7bb86316 | 2440 | } |
5f39d397 CM |
2441 | |
2442 | leaf = path->nodes[0]; | |
952fccac | 2443 | ei = btrfs_item_ptr(leaf, extent_slot, |
123abc88 | 2444 | struct btrfs_extent_item); |
5f39d397 CM |
2445 | refs = btrfs_extent_refs(leaf, ei); |
2446 | BUG_ON(refs == 0); | |
2447 | refs -= 1; | |
2448 | btrfs_set_extent_refs(leaf, ei, refs); | |
952fccac | 2449 | |
5f39d397 CM |
2450 | btrfs_mark_buffer_dirty(leaf); |
2451 | ||
952fccac | 2452 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
31840ae1 ZY |
2453 | struct btrfs_extent_ref *ref; |
2454 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
2455 | struct btrfs_extent_ref); | |
2456 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); | |
952fccac CM |
2457 | /* if the back ref and the extent are next to each other |
2458 | * they get deleted below in one shot | |
2459 | */ | |
2460 | path->slots[0] = extent_slot; | |
2461 | num_to_del = 2; | |
2462 | } else if (found_extent) { | |
2463 | /* otherwise delete the extent back ref */ | |
31840ae1 | 2464 | ret = remove_extent_backref(trans, extent_root, path); |
952fccac CM |
2465 | BUG_ON(ret); |
2466 | /* if refs are 0, we need to setup the path for deletion */ | |
2467 | if (refs == 0) { | |
2468 | btrfs_release_path(extent_root, path); | |
2469 | ret = btrfs_search_slot(trans, extent_root, &key, path, | |
2470 | -1, 1); | |
952fccac CM |
2471 | BUG_ON(ret); |
2472 | } | |
2473 | } | |
2474 | ||
cf27e1ee | 2475 | if (refs == 0) { |
db94535d CM |
2476 | u64 super_used; |
2477 | u64 root_used; | |
78fae27e CM |
2478 | |
2479 | if (pin) { | |
25179201 | 2480 | mutex_lock(&root->fs_info->pinned_mutex); |
31840ae1 ZY |
2481 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, |
2482 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); | |
25179201 | 2483 | mutex_unlock(&root->fs_info->pinned_mutex); |
c549228f Y |
2484 | if (ret > 0) |
2485 | mark_free = 1; | |
2486 | BUG_ON(ret < 0); | |
78fae27e | 2487 | } |
58176a96 | 2488 | /* block accounting for super block */ |
75eff68e | 2489 | spin_lock(&info->delalloc_lock); |
db94535d CM |
2490 | super_used = btrfs_super_bytes_used(&info->super_copy); |
2491 | btrfs_set_super_bytes_used(&info->super_copy, | |
2492 | super_used - num_bytes); | |
58176a96 JB |
2493 | |
2494 | /* block accounting for root item */ | |
db94535d | 2495 | root_used = btrfs_root_used(&root->root_item); |
5f39d397 | 2496 | btrfs_set_root_used(&root->root_item, |
db94535d | 2497 | root_used - num_bytes); |
34bf63c4 | 2498 | spin_unlock(&info->delalloc_lock); |
952fccac CM |
2499 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
2500 | num_to_del); | |
31840ae1 | 2501 | BUG_ON(ret); |
25179201 | 2502 | btrfs_release_path(extent_root, path); |
21af804c | 2503 | |
459931ec CM |
2504 | if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
2505 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); | |
2506 | BUG_ON(ret); | |
2507 | } | |
2508 | ||
dcbdd4dc CM |
2509 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
2510 | mark_free); | |
2511 | BUG_ON(ret); | |
a28ec197 | 2512 | } |
5caf2a00 | 2513 | btrfs_free_path(path); |
87ef2bb4 | 2514 | finish_current_insert(trans, extent_root, 0); |
a28ec197 CM |
2515 | return ret; |
2516 | } | |
2517 | ||
a28ec197 CM |
2518 | /* |
2519 | * find all the blocks marked as pending in the radix tree and remove | |
2520 | * them from the extent map | |
2521 | */ | |
d397712b CM |
2522 | static int del_pending_extents(struct btrfs_trans_handle *trans, |
2523 | struct btrfs_root *extent_root, int all) | |
a28ec197 CM |
2524 | { |
2525 | int ret; | |
e20d96d6 | 2526 | int err = 0; |
1a5bc167 CM |
2527 | u64 start; |
2528 | u64 end; | |
31840ae1 | 2529 | u64 priv; |
25179201 | 2530 | u64 search = 0; |
f3465ca4 | 2531 | int nr = 0, skipped = 0; |
d1310b2e | 2532 | struct extent_io_tree *pending_del; |
31840ae1 ZY |
2533 | struct extent_io_tree *extent_ins; |
2534 | struct pending_extent_op *extent_op; | |
25179201 | 2535 | struct btrfs_fs_info *info = extent_root->fs_info; |
f3465ca4 | 2536 | struct list_head delete_list; |
8ef97622 | 2537 | |
f3465ca4 | 2538 | INIT_LIST_HEAD(&delete_list); |
31840ae1 | 2539 | extent_ins = &extent_root->fs_info->extent_ins; |
1a5bc167 | 2540 | pending_del = &extent_root->fs_info->pending_del; |
a28ec197 | 2541 | |
f3465ca4 JB |
2542 | again: |
2543 | mutex_lock(&info->extent_ins_mutex); | |
d397712b | 2544 | while (1) { |
25179201 JB |
2545 | ret = find_first_extent_bit(pending_del, search, &start, &end, |
2546 | EXTENT_WRITEBACK); | |
2547 | if (ret) { | |
f3465ca4 | 2548 | if (all && skipped && !nr) { |
25179201 JB |
2549 | search = 0; |
2550 | continue; | |
2551 | } | |
f3465ca4 | 2552 | mutex_unlock(&info->extent_ins_mutex); |
a28ec197 | 2553 | break; |
25179201 JB |
2554 | } |
2555 | ||
2556 | ret = try_lock_extent(extent_ins, start, end, GFP_NOFS); | |
2557 | if (!ret) { | |
2558 | search = end+1; | |
f3465ca4 JB |
2559 | skipped = 1; |
2560 | ||
2561 | if (need_resched()) { | |
2562 | mutex_unlock(&info->extent_ins_mutex); | |
2563 | cond_resched(); | |
2564 | mutex_lock(&info->extent_ins_mutex); | |
2565 | } | |
2566 | ||
25179201 JB |
2567 | continue; |
2568 | } | |
2569 | BUG_ON(ret < 0); | |
31840ae1 ZY |
2570 | |
2571 | ret = get_state_private(pending_del, start, &priv); | |
2572 | BUG_ON(ret); | |
2573 | extent_op = (struct pending_extent_op *)(unsigned long)priv; | |
2574 | ||
25179201 | 2575 | clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK, |
1a5bc167 | 2576 | GFP_NOFS); |
31840ae1 | 2577 | if (!test_range_bit(extent_ins, start, end, |
25179201 | 2578 | EXTENT_WRITEBACK, 0)) { |
f3465ca4 JB |
2579 | list_add_tail(&extent_op->list, &delete_list); |
2580 | nr++; | |
c286ac48 | 2581 | } else { |
31840ae1 | 2582 | kfree(extent_op); |
25179201 JB |
2583 | |
2584 | ret = get_state_private(&info->extent_ins, start, | |
2585 | &priv); | |
31840ae1 ZY |
2586 | BUG_ON(ret); |
2587 | extent_op = (struct pending_extent_op *) | |
25179201 JB |
2588 | (unsigned long)priv; |
2589 | ||
2590 | clear_extent_bits(&info->extent_ins, start, end, | |
2591 | EXTENT_WRITEBACK, GFP_NOFS); | |
31840ae1 | 2592 | |
f3465ca4 JB |
2593 | if (extent_op->type == PENDING_BACKREF_UPDATE) { |
2594 | list_add_tail(&extent_op->list, &delete_list); | |
2595 | search = end + 1; | |
2596 | nr++; | |
2597 | continue; | |
2598 | } | |
31840ae1 | 2599 | |
25179201 JB |
2600 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
2601 | ret = pin_down_bytes(trans, extent_root, start, | |
2602 | end + 1 - start, 0); | |
2603 | mutex_unlock(&extent_root->fs_info->pinned_mutex); | |
2604 | ||
31840ae1 | 2605 | ret = update_block_group(trans, extent_root, start, |
25179201 JB |
2606 | end + 1 - start, 0, ret > 0); |
2607 | ||
f3465ca4 | 2608 | unlock_extent(extent_ins, start, end, GFP_NOFS); |
31840ae1 ZY |
2609 | BUG_ON(ret); |
2610 | kfree(extent_op); | |
c286ac48 | 2611 | } |
1a5bc167 CM |
2612 | if (ret) |
2613 | err = ret; | |
c286ac48 | 2614 | |
f3465ca4 JB |
2615 | search = end + 1; |
2616 | ||
2617 | if (need_resched()) { | |
2618 | mutex_unlock(&info->extent_ins_mutex); | |
2619 | cond_resched(); | |
2620 | mutex_lock(&info->extent_ins_mutex); | |
2621 | } | |
2622 | } | |
2623 | ||
2624 | if (nr) { | |
2625 | ret = free_extents(trans, extent_root, &delete_list); | |
2626 | BUG_ON(ret); | |
2627 | } | |
2628 | ||
2629 | if (all && skipped) { | |
2630 | INIT_LIST_HEAD(&delete_list); | |
2631 | search = 0; | |
2632 | nr = 0; | |
2633 | goto again; | |
fec577fb | 2634 | } |
f3465ca4 | 2635 | |
e20d96d6 | 2636 | return err; |
fec577fb CM |
2637 | } |
2638 | ||
2639 | /* | |
2640 | * remove an extent from the root, returns 0 on success | |
2641 | */ | |
925baedd | 2642 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
2643 | struct btrfs_root *root, |
2644 | u64 bytenr, u64 num_bytes, u64 parent, | |
2645 | u64 root_objectid, u64 ref_generation, | |
3bb1a1bc | 2646 | u64 owner_objectid, int pin) |
fec577fb | 2647 | { |
9f5fae2f | 2648 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
fec577fb CM |
2649 | int pending_ret; |
2650 | int ret; | |
a28ec197 | 2651 | |
db94535d | 2652 | WARN_ON(num_bytes < root->sectorsize); |
fec577fb | 2653 | if (root == extent_root) { |
f3465ca4 JB |
2654 | struct pending_extent_op *extent_op = NULL; |
2655 | ||
2656 | mutex_lock(&root->fs_info->extent_ins_mutex); | |
2657 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, | |
2658 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { | |
2659 | u64 priv; | |
2660 | ret = get_state_private(&root->fs_info->extent_ins, | |
2661 | bytenr, &priv); | |
2662 | BUG_ON(ret); | |
2663 | extent_op = (struct pending_extent_op *) | |
2664 | (unsigned long)priv; | |
2665 | ||
2666 | extent_op->del = 1; | |
2667 | if (extent_op->type == PENDING_EXTENT_INSERT) { | |
2668 | mutex_unlock(&root->fs_info->extent_ins_mutex); | |
2669 | return 0; | |
2670 | } | |
2671 | } | |
2672 | ||
2673 | if (extent_op) { | |
2674 | ref_generation = extent_op->orig_generation; | |
2675 | parent = extent_op->orig_parent; | |
2676 | } | |
31840ae1 ZY |
2677 | |
2678 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | |
2679 | BUG_ON(!extent_op); | |
2680 | ||
2681 | extent_op->type = PENDING_EXTENT_DELETE; | |
2682 | extent_op->bytenr = bytenr; | |
2683 | extent_op->num_bytes = num_bytes; | |
2684 | extent_op->parent = parent; | |
2685 | extent_op->orig_parent = parent; | |
2686 | extent_op->generation = ref_generation; | |
2687 | extent_op->orig_generation = ref_generation; | |
2688 | extent_op->level = (int)owner_objectid; | |
f3465ca4 JB |
2689 | INIT_LIST_HEAD(&extent_op->list); |
2690 | extent_op->del = 0; | |
31840ae1 ZY |
2691 | |
2692 | set_extent_bits(&root->fs_info->pending_del, | |
2693 | bytenr, bytenr + num_bytes - 1, | |
25179201 | 2694 | EXTENT_WRITEBACK, GFP_NOFS); |
31840ae1 ZY |
2695 | set_state_private(&root->fs_info->pending_del, |
2696 | bytenr, (unsigned long)extent_op); | |
25179201 | 2697 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
fec577fb CM |
2698 | return 0; |
2699 | } | |
4bef0848 | 2700 | /* if metadata always pin */ |
d00aff00 CM |
2701 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
2702 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { | |
0f9dd46c JB |
2703 | struct btrfs_block_group_cache *cache; |
2704 | ||
d00aff00 | 2705 | /* btrfs_free_reserved_extent */ |
0f9dd46c JB |
2706 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
2707 | BUG_ON(!cache); | |
2708 | btrfs_add_free_space(cache, bytenr, num_bytes); | |
d2fb3437 | 2709 | put_block_group(cache); |
e8569813 | 2710 | update_reserved_extents(root, bytenr, num_bytes, 0); |
d00aff00 CM |
2711 | return 0; |
2712 | } | |
4bef0848 | 2713 | pin = 1; |
d00aff00 | 2714 | } |
4bef0848 CM |
2715 | |
2716 | /* if data pin when any transaction has committed this */ | |
2717 | if (ref_generation != trans->transid) | |
2718 | pin = 1; | |
2719 | ||
31840ae1 | 2720 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, |
3bb1a1bc YZ |
2721 | root_objectid, ref_generation, |
2722 | owner_objectid, pin, pin == 0); | |
ee6e6504 | 2723 | |
87ef2bb4 CM |
2724 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
2725 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); | |
fec577fb CM |
2726 | return ret ? ret : pending_ret; |
2727 | } | |
2728 | ||
925baedd | 2729 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
2730 | struct btrfs_root *root, |
2731 | u64 bytenr, u64 num_bytes, u64 parent, | |
2732 | u64 root_objectid, u64 ref_generation, | |
3bb1a1bc | 2733 | u64 owner_objectid, int pin) |
925baedd CM |
2734 | { |
2735 | int ret; | |
2736 | ||
31840ae1 | 2737 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, |
925baedd | 2738 | root_objectid, ref_generation, |
3bb1a1bc | 2739 | owner_objectid, pin); |
925baedd CM |
2740 | return ret; |
2741 | } | |
2742 | ||
87ee04eb CM |
2743 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
2744 | { | |
2745 | u64 mask = ((u64)root->stripesize - 1); | |
2746 | u64 ret = (val + mask) & ~mask; | |
2747 | return ret; | |
2748 | } | |
2749 | ||
fec577fb CM |
2750 | /* |
2751 | * walks the btree of allocated extents and find a hole of a given size. | |
2752 | * The key ins is changed to record the hole: | |
2753 | * ins->objectid == block start | |
62e2749e | 2754 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
fec577fb CM |
2755 | * ins->offset == number of blocks |
2756 | * Any available blocks before search_start are skipped. | |
2757 | */ | |
d397712b | 2758 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
98ed5174 CM |
2759 | struct btrfs_root *orig_root, |
2760 | u64 num_bytes, u64 empty_size, | |
2761 | u64 search_start, u64 search_end, | |
2762 | u64 hint_byte, struct btrfs_key *ins, | |
2763 | u64 exclude_start, u64 exclude_nr, | |
2764 | int data) | |
fec577fb | 2765 | { |
80eb234a | 2766 | int ret = 0; |
d397712b | 2767 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
db94535d | 2768 | u64 total_needed = num_bytes; |
239b14b3 | 2769 | u64 *last_ptr = NULL; |
4366211c | 2770 | u64 last_wanted = 0; |
80eb234a | 2771 | struct btrfs_block_group_cache *block_group = NULL; |
0ef3e66b | 2772 | int chunk_alloc_done = 0; |
239b14b3 | 2773 | int empty_cluster = 2 * 1024 * 1024; |
0ef3e66b | 2774 | int allowed_chunk_alloc = 0; |
80eb234a JB |
2775 | struct list_head *head = NULL, *cur = NULL; |
2776 | int loop = 0; | |
f5a31e16 | 2777 | int extra_loop = 0; |
80eb234a | 2778 | struct btrfs_space_info *space_info; |
fec577fb | 2779 | |
db94535d | 2780 | WARN_ON(num_bytes < root->sectorsize); |
b1a4d965 | 2781 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
80eb234a JB |
2782 | ins->objectid = 0; |
2783 | ins->offset = 0; | |
b1a4d965 | 2784 | |
0ef3e66b CM |
2785 | if (orig_root->ref_cows || empty_size) |
2786 | allowed_chunk_alloc = 1; | |
2787 | ||
239b14b3 CM |
2788 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
2789 | last_ptr = &root->fs_info->last_alloc; | |
4366211c | 2790 | empty_cluster = 64 * 1024; |
239b14b3 CM |
2791 | } |
2792 | ||
0f9dd46c | 2793 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
239b14b3 | 2794 | last_ptr = &root->fs_info->last_data_alloc; |
0f9dd46c | 2795 | |
239b14b3 | 2796 | if (last_ptr) { |
4366211c | 2797 | if (*last_ptr) { |
239b14b3 | 2798 | hint_byte = *last_ptr; |
4366211c CM |
2799 | last_wanted = *last_ptr; |
2800 | } else | |
239b14b3 | 2801 | empty_size += empty_cluster; |
4366211c CM |
2802 | } else { |
2803 | empty_cluster = 0; | |
239b14b3 | 2804 | } |
a061fc8d | 2805 | search_start = max(search_start, first_logical_byte(root, 0)); |
239b14b3 | 2806 | search_start = max(search_start, hint_byte); |
0b86a832 | 2807 | |
42e70e7a | 2808 | if (last_wanted && search_start != last_wanted) { |
4366211c | 2809 | last_wanted = 0; |
42e70e7a CM |
2810 | empty_size += empty_cluster; |
2811 | } | |
4366211c | 2812 | |
42e70e7a | 2813 | total_needed += empty_size; |
80eb234a | 2814 | block_group = btrfs_lookup_block_group(root->fs_info, search_start); |
d899e052 YZ |
2815 | if (!block_group) |
2816 | block_group = btrfs_lookup_first_block_group(root->fs_info, | |
2817 | search_start); | |
80eb234a | 2818 | space_info = __find_space_info(root->fs_info, data); |
0f9dd46c | 2819 | |
80eb234a JB |
2820 | down_read(&space_info->groups_sem); |
2821 | while (1) { | |
2822 | struct btrfs_free_space *free_space; | |
0f9dd46c | 2823 | /* |
80eb234a JB |
2824 | * the only way this happens if our hint points to a block |
2825 | * group thats not of the proper type, while looping this | |
2826 | * should never happen | |
0f9dd46c | 2827 | */ |
8a1413a2 CM |
2828 | if (empty_size) |
2829 | extra_loop = 1; | |
2830 | ||
42e70e7a CM |
2831 | if (!block_group) |
2832 | goto new_group_no_lock; | |
2833 | ||
ea6a478e JB |
2834 | if (unlikely(!block_group->cached)) { |
2835 | mutex_lock(&block_group->cache_mutex); | |
2836 | ret = cache_block_group(root, block_group); | |
2837 | mutex_unlock(&block_group->cache_mutex); | |
2838 | if (ret) | |
2839 | break; | |
2840 | } | |
2841 | ||
25179201 | 2842 | mutex_lock(&block_group->alloc_mutex); |
80eb234a | 2843 | if (unlikely(!block_group_bits(block_group, data))) |
0f9dd46c | 2844 | goto new_group; |
0f9dd46c | 2845 | |
ea6a478e | 2846 | if (unlikely(block_group->ro)) |
80eb234a | 2847 | goto new_group; |
0f9dd46c | 2848 | |
80eb234a JB |
2849 | free_space = btrfs_find_free_space(block_group, search_start, |
2850 | total_needed); | |
2851 | if (free_space) { | |
2852 | u64 start = block_group->key.objectid; | |
2853 | u64 end = block_group->key.objectid + | |
0f9dd46c | 2854 | block_group->key.offset; |
239b14b3 | 2855 | |
80eb234a | 2856 | search_start = stripe_align(root, free_space->offset); |
0f9dd46c | 2857 | |
80eb234a JB |
2858 | /* move on to the next group */ |
2859 | if (search_start + num_bytes >= search_end) | |
2860 | goto new_group; | |
e37c9e69 | 2861 | |
80eb234a JB |
2862 | /* move on to the next group */ |
2863 | if (search_start + num_bytes > end) | |
2864 | goto new_group; | |
2865 | ||
4366211c | 2866 | if (last_wanted && search_start != last_wanted) { |
3b7885bf | 2867 | total_needed += empty_cluster; |
8a1413a2 | 2868 | empty_size += empty_cluster; |
4366211c | 2869 | last_wanted = 0; |
3b7885bf CM |
2870 | /* |
2871 | * if search_start is still in this block group | |
2872 | * then we just re-search this block group | |
2873 | */ | |
2874 | if (search_start >= start && | |
2875 | search_start < end) { | |
2876 | mutex_unlock(&block_group->alloc_mutex); | |
2877 | continue; | |
2878 | } | |
2879 | ||
2880 | /* else we go to the next block group */ | |
2881 | goto new_group; | |
2882 | } | |
2883 | ||
80eb234a JB |
2884 | if (exclude_nr > 0 && |
2885 | (search_start + num_bytes > exclude_start && | |
2886 | search_start < exclude_start + exclude_nr)) { | |
2887 | search_start = exclude_start + exclude_nr; | |
2888 | /* | |
2889 | * if search_start is still in this block group | |
2890 | * then we just re-search this block group | |
2891 | */ | |
2892 | if (search_start >= start && | |
25179201 JB |
2893 | search_start < end) { |
2894 | mutex_unlock(&block_group->alloc_mutex); | |
4366211c | 2895 | last_wanted = 0; |
80eb234a | 2896 | continue; |
25179201 | 2897 | } |
80eb234a JB |
2898 | |
2899 | /* else we go to the next block group */ | |
2900 | goto new_group; | |
2901 | } | |
2902 | ||
2903 | ins->objectid = search_start; | |
2904 | ins->offset = num_bytes; | |
25179201 JB |
2905 | |
2906 | btrfs_remove_free_space_lock(block_group, search_start, | |
2907 | num_bytes); | |
80eb234a | 2908 | /* we are all good, lets return */ |
25179201 | 2909 | mutex_unlock(&block_group->alloc_mutex); |
80eb234a | 2910 | break; |
0f9dd46c | 2911 | } |
80eb234a | 2912 | new_group: |
42e70e7a | 2913 | mutex_unlock(&block_group->alloc_mutex); |
d2fb3437 YZ |
2914 | put_block_group(block_group); |
2915 | block_group = NULL; | |
42e70e7a | 2916 | new_group_no_lock: |
f5a31e16 CM |
2917 | /* don't try to compare new allocations against the |
2918 | * last allocation any more | |
2919 | */ | |
4366211c | 2920 | last_wanted = 0; |
f5a31e16 | 2921 | |
80eb234a JB |
2922 | /* |
2923 | * Here's how this works. | |
2924 | * loop == 0: we were searching a block group via a hint | |
2925 | * and didn't find anything, so we start at | |
2926 | * the head of the block groups and keep searching | |
2927 | * loop == 1: we're searching through all of the block groups | |
2928 | * if we hit the head again we have searched | |
2929 | * all of the block groups for this space and we | |
2930 | * need to try and allocate, if we cant error out. | |
2931 | * loop == 2: we allocated more space and are looping through | |
2932 | * all of the block groups again. | |
2933 | */ | |
2934 | if (loop == 0) { | |
2935 | head = &space_info->block_groups; | |
2936 | cur = head->next; | |
80eb234a JB |
2937 | loop++; |
2938 | } else if (loop == 1 && cur == head) { | |
f5a31e16 CM |
2939 | int keep_going; |
2940 | ||
2941 | /* at this point we give up on the empty_size | |
2942 | * allocations and just try to allocate the min | |
2943 | * space. | |
2944 | * | |
2945 | * The extra_loop field was set if an empty_size | |
2946 | * allocation was attempted above, and if this | |
2947 | * is try we need to try the loop again without | |
2948 | * the additional empty_size. | |
2949 | */ | |
5b7c3fcc CM |
2950 | total_needed -= empty_size; |
2951 | empty_size = 0; | |
f5a31e16 CM |
2952 | keep_going = extra_loop; |
2953 | loop++; | |
42e70e7a | 2954 | |
80eb234a JB |
2955 | if (allowed_chunk_alloc && !chunk_alloc_done) { |
2956 | up_read(&space_info->groups_sem); | |
2957 | ret = do_chunk_alloc(trans, root, num_bytes + | |
2958 | 2 * 1024 * 1024, data, 1); | |
80eb234a | 2959 | down_read(&space_info->groups_sem); |
2ed6d664 CM |
2960 | if (ret < 0) |
2961 | goto loop_check; | |
80eb234a | 2962 | head = &space_info->block_groups; |
f5a31e16 CM |
2963 | /* |
2964 | * we've allocated a new chunk, keep | |
2965 | * trying | |
2966 | */ | |
2967 | keep_going = 1; | |
80eb234a JB |
2968 | chunk_alloc_done = 1; |
2969 | } else if (!allowed_chunk_alloc) { | |
2970 | space_info->force_alloc = 1; | |
f5a31e16 | 2971 | } |
2ed6d664 | 2972 | loop_check: |
f5a31e16 CM |
2973 | if (keep_going) { |
2974 | cur = head->next; | |
2975 | extra_loop = 0; | |
80eb234a JB |
2976 | } else { |
2977 | break; | |
2978 | } | |
2979 | } else if (cur == head) { | |
2980 | break; | |
0f9dd46c | 2981 | } |
0b86a832 | 2982 | |
80eb234a JB |
2983 | block_group = list_entry(cur, struct btrfs_block_group_cache, |
2984 | list); | |
d2fb3437 YZ |
2985 | atomic_inc(&block_group->count); |
2986 | ||
80eb234a JB |
2987 | search_start = block_group->key.objectid; |
2988 | cur = cur->next; | |
f2654de4 | 2989 | } |
0b86a832 | 2990 | |
80eb234a JB |
2991 | /* we found what we needed */ |
2992 | if (ins->objectid) { | |
2993 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) | |
d2fb3437 | 2994 | trans->block_group = block_group->key.objectid; |
0f9dd46c | 2995 | |
80eb234a JB |
2996 | if (last_ptr) |
2997 | *last_ptr = ins->objectid + ins->offset; | |
2998 | ret = 0; | |
2999 | } else if (!ret) { | |
d397712b CM |
3000 | printk(KERN_ERR "btrfs searching for %llu bytes, " |
3001 | "num_bytes %llu, loop %d, allowed_alloc %d\n", | |
3002 | (unsigned long long)total_needed, | |
3003 | (unsigned long long)num_bytes, | |
4ce4cb52 | 3004 | loop, allowed_chunk_alloc); |
80eb234a | 3005 | ret = -ENOSPC; |
be744175 | 3006 | } |
d2fb3437 YZ |
3007 | if (block_group) |
3008 | put_block_group(block_group); | |
be744175 | 3009 | |
80eb234a | 3010 | up_read(&space_info->groups_sem); |
0f70abe2 | 3011 | return ret; |
fec577fb | 3012 | } |
ec44a35c | 3013 | |
0f9dd46c JB |
3014 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
3015 | { | |
3016 | struct btrfs_block_group_cache *cache; | |
3017 | struct list_head *l; | |
3018 | ||
d397712b CM |
3019 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
3020 | (unsigned long long)(info->total_bytes - info->bytes_used - | |
3021 | info->bytes_pinned - info->bytes_reserved), | |
3022 | (info->full) ? "" : "not "); | |
0f9dd46c | 3023 | |
80eb234a | 3024 | down_read(&info->groups_sem); |
0f9dd46c JB |
3025 | list_for_each(l, &info->block_groups) { |
3026 | cache = list_entry(l, struct btrfs_block_group_cache, list); | |
3027 | spin_lock(&cache->lock); | |
d397712b CM |
3028 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
3029 | "%llu pinned %llu reserved\n", | |
3030 | (unsigned long long)cache->key.objectid, | |
3031 | (unsigned long long)cache->key.offset, | |
3032 | (unsigned long long)btrfs_block_group_used(&cache->item), | |
3033 | (unsigned long long)cache->pinned, | |
3034 | (unsigned long long)cache->reserved); | |
0f9dd46c JB |
3035 | btrfs_dump_free_space(cache, bytes); |
3036 | spin_unlock(&cache->lock); | |
3037 | } | |
80eb234a | 3038 | up_read(&info->groups_sem); |
0f9dd46c | 3039 | } |
e8569813 | 3040 | |
e6dcd2dc CM |
3041 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
3042 | struct btrfs_root *root, | |
3043 | u64 num_bytes, u64 min_alloc_size, | |
3044 | u64 empty_size, u64 hint_byte, | |
3045 | u64 search_end, struct btrfs_key *ins, | |
3046 | u64 data) | |
fec577fb CM |
3047 | { |
3048 | int ret; | |
fbdc762b | 3049 | u64 search_start = 0; |
8790d502 | 3050 | u64 alloc_profile; |
1261ec42 | 3051 | struct btrfs_fs_info *info = root->fs_info; |
925baedd | 3052 | |
6324fbf3 | 3053 | if (data) { |
8790d502 | 3054 | alloc_profile = info->avail_data_alloc_bits & |
d397712b | 3055 | info->data_alloc_profile; |
8790d502 | 3056 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
6324fbf3 | 3057 | } else if (root == root->fs_info->chunk_root) { |
8790d502 | 3058 | alloc_profile = info->avail_system_alloc_bits & |
d397712b | 3059 | info->system_alloc_profile; |
8790d502 | 3060 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
6324fbf3 | 3061 | } else { |
8790d502 | 3062 | alloc_profile = info->avail_metadata_alloc_bits & |
d397712b | 3063 | info->metadata_alloc_profile; |
8790d502 | 3064 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
6324fbf3 | 3065 | } |
98d20f67 | 3066 | again: |
2b82032c | 3067 | data = btrfs_reduce_alloc_profile(root, data); |
0ef3e66b CM |
3068 | /* |
3069 | * the only place that sets empty_size is btrfs_realloc_node, which | |
3070 | * is not called recursively on allocations | |
3071 | */ | |
3072 | if (empty_size || root->ref_cows) { | |
593060d7 | 3073 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
6324fbf3 | 3074 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
0ef3e66b CM |
3075 | 2 * 1024 * 1024, |
3076 | BTRFS_BLOCK_GROUP_METADATA | | |
3077 | (info->metadata_alloc_profile & | |
3078 | info->avail_metadata_alloc_bits), 0); | |
6324fbf3 CM |
3079 | } |
3080 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, | |
0ef3e66b | 3081 | num_bytes + 2 * 1024 * 1024, data, 0); |
6324fbf3 | 3082 | } |
0b86a832 | 3083 | |
db94535d CM |
3084 | WARN_ON(num_bytes < root->sectorsize); |
3085 | ret = find_free_extent(trans, root, num_bytes, empty_size, | |
3086 | search_start, search_end, hint_byte, ins, | |
26b8003f CM |
3087 | trans->alloc_exclude_start, |
3088 | trans->alloc_exclude_nr, data); | |
3b951516 | 3089 | |
98d20f67 CM |
3090 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
3091 | num_bytes = num_bytes >> 1; | |
0f9dd46c | 3092 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
98d20f67 | 3093 | num_bytes = max(num_bytes, min_alloc_size); |
0ef3e66b CM |
3094 | do_chunk_alloc(trans, root->fs_info->extent_root, |
3095 | num_bytes, data, 1); | |
98d20f67 CM |
3096 | goto again; |
3097 | } | |
ec44a35c | 3098 | if (ret) { |
0f9dd46c JB |
3099 | struct btrfs_space_info *sinfo; |
3100 | ||
3101 | sinfo = __find_space_info(root->fs_info, data); | |
d397712b CM |
3102 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
3103 | "wanted %llu\n", (unsigned long long)data, | |
3104 | (unsigned long long)num_bytes); | |
0f9dd46c | 3105 | dump_space_info(sinfo, num_bytes); |
925baedd | 3106 | BUG(); |
925baedd | 3107 | } |
0f9dd46c JB |
3108 | |
3109 | return ret; | |
e6dcd2dc CM |
3110 | } |
3111 | ||
65b51a00 CM |
3112 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
3113 | { | |
0f9dd46c | 3114 | struct btrfs_block_group_cache *cache; |
1f3c79a2 | 3115 | int ret = 0; |
0f9dd46c | 3116 | |
0f9dd46c JB |
3117 | cache = btrfs_lookup_block_group(root->fs_info, start); |
3118 | if (!cache) { | |
d397712b CM |
3119 | printk(KERN_ERR "Unable to find block group for %llu\n", |
3120 | (unsigned long long)start); | |
0f9dd46c JB |
3121 | return -ENOSPC; |
3122 | } | |
1f3c79a2 LH |
3123 | |
3124 | ret = btrfs_discard_extent(root, start, len); | |
3125 | ||
0f9dd46c | 3126 | btrfs_add_free_space(cache, start, len); |
d2fb3437 | 3127 | put_block_group(cache); |
1a40e23b | 3128 | update_reserved_extents(root, start, len, 0); |
1f3c79a2 LH |
3129 | |
3130 | return ret; | |
65b51a00 CM |
3131 | } |
3132 | ||
e6dcd2dc CM |
3133 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
3134 | struct btrfs_root *root, | |
3135 | u64 num_bytes, u64 min_alloc_size, | |
3136 | u64 empty_size, u64 hint_byte, | |
3137 | u64 search_end, struct btrfs_key *ins, | |
3138 | u64 data) | |
3139 | { | |
3140 | int ret; | |
e6dcd2dc CM |
3141 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
3142 | empty_size, hint_byte, search_end, ins, | |
3143 | data); | |
e8569813 | 3144 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
e6dcd2dc CM |
3145 | return ret; |
3146 | } | |
3147 | ||
3148 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |
31840ae1 | 3149 | struct btrfs_root *root, u64 parent, |
e6dcd2dc | 3150 | u64 root_objectid, u64 ref_generation, |
3bb1a1bc | 3151 | u64 owner, struct btrfs_key *ins) |
e6dcd2dc CM |
3152 | { |
3153 | int ret; | |
3154 | int pending_ret; | |
3155 | u64 super_used; | |
3156 | u64 root_used; | |
3157 | u64 num_bytes = ins->offset; | |
3158 | u32 sizes[2]; | |
3159 | struct btrfs_fs_info *info = root->fs_info; | |
3160 | struct btrfs_root *extent_root = info->extent_root; | |
3161 | struct btrfs_extent_item *extent_item; | |
3162 | struct btrfs_extent_ref *ref; | |
3163 | struct btrfs_path *path; | |
3164 | struct btrfs_key keys[2]; | |
fec577fb | 3165 | |
31840ae1 ZY |
3166 | if (parent == 0) |
3167 | parent = ins->objectid; | |
3168 | ||
58176a96 | 3169 | /* block accounting for super block */ |
75eff68e | 3170 | spin_lock(&info->delalloc_lock); |
db94535d CM |
3171 | super_used = btrfs_super_bytes_used(&info->super_copy); |
3172 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); | |
26b8003f | 3173 | |
58176a96 | 3174 | /* block accounting for root item */ |
db94535d CM |
3175 | root_used = btrfs_root_used(&root->root_item); |
3176 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); | |
34bf63c4 | 3177 | spin_unlock(&info->delalloc_lock); |
58176a96 | 3178 | |
26b8003f | 3179 | if (root == extent_root) { |
31840ae1 ZY |
3180 | struct pending_extent_op *extent_op; |
3181 | ||
3182 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | |
3183 | BUG_ON(!extent_op); | |
3184 | ||
3185 | extent_op->type = PENDING_EXTENT_INSERT; | |
3186 | extent_op->bytenr = ins->objectid; | |
3187 | extent_op->num_bytes = ins->offset; | |
3188 | extent_op->parent = parent; | |
3189 | extent_op->orig_parent = 0; | |
3190 | extent_op->generation = ref_generation; | |
3191 | extent_op->orig_generation = 0; | |
3192 | extent_op->level = (int)owner; | |
f3465ca4 JB |
3193 | INIT_LIST_HEAD(&extent_op->list); |
3194 | extent_op->del = 0; | |
31840ae1 | 3195 | |
25179201 | 3196 | mutex_lock(&root->fs_info->extent_ins_mutex); |
1a5bc167 CM |
3197 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
3198 | ins->objectid + ins->offset - 1, | |
25179201 | 3199 | EXTENT_WRITEBACK, GFP_NOFS); |
31840ae1 ZY |
3200 | set_state_private(&root->fs_info->extent_ins, |
3201 | ins->objectid, (unsigned long)extent_op); | |
25179201 | 3202 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
26b8003f CM |
3203 | goto update_block; |
3204 | } | |
3205 | ||
47e4bb98 | 3206 | memcpy(&keys[0], ins, sizeof(*ins)); |
47e4bb98 CM |
3207 | keys[1].objectid = ins->objectid; |
3208 | keys[1].type = BTRFS_EXTENT_REF_KEY; | |
31840ae1 | 3209 | keys[1].offset = parent; |
47e4bb98 CM |
3210 | sizes[0] = sizeof(*extent_item); |
3211 | sizes[1] = sizeof(*ref); | |
7bb86316 CM |
3212 | |
3213 | path = btrfs_alloc_path(); | |
3214 | BUG_ON(!path); | |
47e4bb98 CM |
3215 | |
3216 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, | |
3217 | sizes, 2); | |
ccd467d6 | 3218 | BUG_ON(ret); |
0f9dd46c | 3219 | |
47e4bb98 CM |
3220 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
3221 | struct btrfs_extent_item); | |
3222 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); | |
3223 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
3224 | struct btrfs_extent_ref); | |
3225 | ||
3226 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); | |
3227 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); | |
3228 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); | |
31840ae1 | 3229 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); |
47e4bb98 CM |
3230 | |
3231 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
3232 | ||
3233 | trans->alloc_exclude_start = 0; | |
3234 | trans->alloc_exclude_nr = 0; | |
7bb86316 | 3235 | btrfs_free_path(path); |
87ef2bb4 CM |
3236 | finish_current_insert(trans, extent_root, 0); |
3237 | pending_ret = del_pending_extents(trans, extent_root, 0); | |
f510cfec | 3238 | |
925baedd CM |
3239 | if (ret) |
3240 | goto out; | |
e37c9e69 | 3241 | if (pending_ret) { |
925baedd CM |
3242 | ret = pending_ret; |
3243 | goto out; | |
e37c9e69 | 3244 | } |
26b8003f CM |
3245 | |
3246 | update_block: | |
d397712b CM |
3247 | ret = update_block_group(trans, root, ins->objectid, |
3248 | ins->offset, 1, 0); | |
f5947066 | 3249 | if (ret) { |
d397712b CM |
3250 | printk(KERN_ERR "btrfs update block group failed for %llu " |
3251 | "%llu\n", (unsigned long long)ins->objectid, | |
3252 | (unsigned long long)ins->offset); | |
f5947066 CM |
3253 | BUG(); |
3254 | } | |
925baedd | 3255 | out: |
e6dcd2dc CM |
3256 | return ret; |
3257 | } | |
3258 | ||
3259 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |
31840ae1 | 3260 | struct btrfs_root *root, u64 parent, |
e6dcd2dc | 3261 | u64 root_objectid, u64 ref_generation, |
3bb1a1bc | 3262 | u64 owner, struct btrfs_key *ins) |
e6dcd2dc CM |
3263 | { |
3264 | int ret; | |
1c2308f8 CM |
3265 | |
3266 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) | |
3267 | return 0; | |
3bb1a1bc YZ |
3268 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
3269 | ref_generation, owner, ins); | |
e8569813 | 3270 | update_reserved_extents(root, ins->objectid, ins->offset, 0); |
e6dcd2dc CM |
3271 | return ret; |
3272 | } | |
e02119d5 CM |
3273 | |
3274 | /* | |
3275 | * this is used by the tree logging recovery code. It records that | |
3276 | * an extent has been allocated and makes sure to clear the free | |
3277 | * space cache bits as well | |
3278 | */ | |
3279 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, | |
31840ae1 | 3280 | struct btrfs_root *root, u64 parent, |
e02119d5 | 3281 | u64 root_objectid, u64 ref_generation, |
3bb1a1bc | 3282 | u64 owner, struct btrfs_key *ins) |
e02119d5 CM |
3283 | { |
3284 | int ret; | |
3285 | struct btrfs_block_group_cache *block_group; | |
3286 | ||
e02119d5 | 3287 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
ea6a478e | 3288 | mutex_lock(&block_group->cache_mutex); |
e02119d5 | 3289 | cache_block_group(root, block_group); |
ea6a478e | 3290 | mutex_unlock(&block_group->cache_mutex); |
e02119d5 | 3291 | |
ea6a478e JB |
3292 | ret = btrfs_remove_free_space(block_group, ins->objectid, |
3293 | ins->offset); | |
0f9dd46c | 3294 | BUG_ON(ret); |
d2fb3437 | 3295 | put_block_group(block_group); |
3bb1a1bc YZ |
3296 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
3297 | ref_generation, owner, ins); | |
e02119d5 CM |
3298 | return ret; |
3299 | } | |
3300 | ||
e6dcd2dc CM |
3301 | /* |
3302 | * finds a free extent and does all the dirty work required for allocation | |
3303 | * returns the key for the extent through ins, and a tree buffer for | |
3304 | * the first block of the extent through buf. | |
3305 | * | |
3306 | * returns 0 if everything worked, non-zero otherwise. | |
3307 | */ | |
3308 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, | |
3309 | struct btrfs_root *root, | |
31840ae1 | 3310 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
e6dcd2dc | 3311 | u64 root_objectid, u64 ref_generation, |
3bb1a1bc | 3312 | u64 owner_objectid, u64 empty_size, u64 hint_byte, |
e6dcd2dc CM |
3313 | u64 search_end, struct btrfs_key *ins, u64 data) |
3314 | { | |
3315 | int ret; | |
3316 | ||
e6dcd2dc CM |
3317 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
3318 | min_alloc_size, empty_size, hint_byte, | |
3319 | search_end, ins, data); | |
3320 | BUG_ON(ret); | |
d00aff00 | 3321 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
31840ae1 ZY |
3322 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
3323 | root_objectid, ref_generation, | |
3bb1a1bc | 3324 | owner_objectid, ins); |
d00aff00 | 3325 | BUG_ON(ret); |
e6dcd2dc | 3326 | |
e8569813 ZY |
3327 | } else { |
3328 | update_reserved_extents(root, ins->objectid, ins->offset, 1); | |
d00aff00 | 3329 | } |
925baedd | 3330 | return ret; |
fec577fb | 3331 | } |
65b51a00 CM |
3332 | |
3333 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, | |
3334 | struct btrfs_root *root, | |
3335 | u64 bytenr, u32 blocksize) | |
3336 | { | |
3337 | struct extent_buffer *buf; | |
3338 | ||
3339 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); | |
3340 | if (!buf) | |
3341 | return ERR_PTR(-ENOMEM); | |
3342 | btrfs_set_header_generation(buf, trans->transid); | |
3343 | btrfs_tree_lock(buf); | |
3344 | clean_tree_block(trans, root, buf); | |
3345 | btrfs_set_buffer_uptodate(buf); | |
d0c803c4 CM |
3346 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
3347 | set_extent_dirty(&root->dirty_log_pages, buf->start, | |
3348 | buf->start + buf->len - 1, GFP_NOFS); | |
3349 | } else { | |
3350 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, | |
65b51a00 | 3351 | buf->start + buf->len - 1, GFP_NOFS); |
d0c803c4 | 3352 | } |
65b51a00 CM |
3353 | trans->blocks_used++; |
3354 | return buf; | |
3355 | } | |
3356 | ||
fec577fb CM |
3357 | /* |
3358 | * helper function to allocate a block for a given tree | |
3359 | * returns the tree buffer or NULL. | |
3360 | */ | |
5f39d397 | 3361 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
7bb86316 | 3362 | struct btrfs_root *root, |
31840ae1 | 3363 | u32 blocksize, u64 parent, |
7bb86316 CM |
3364 | u64 root_objectid, |
3365 | u64 ref_generation, | |
7bb86316 CM |
3366 | int level, |
3367 | u64 hint, | |
5f39d397 | 3368 | u64 empty_size) |
fec577fb | 3369 | { |
e2fa7227 | 3370 | struct btrfs_key ins; |
fec577fb | 3371 | int ret; |
5f39d397 | 3372 | struct extent_buffer *buf; |
fec577fb | 3373 | |
31840ae1 | 3374 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
3bb1a1bc | 3375 | root_objectid, ref_generation, level, |
31840ae1 | 3376 | empty_size, hint, (u64)-1, &ins, 0); |
fec577fb | 3377 | if (ret) { |
54aa1f4d CM |
3378 | BUG_ON(ret > 0); |
3379 | return ERR_PTR(ret); | |
fec577fb | 3380 | } |
55c69072 | 3381 | |
65b51a00 | 3382 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); |
fec577fb CM |
3383 | return buf; |
3384 | } | |
a28ec197 | 3385 | |
e02119d5 CM |
3386 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
3387 | struct btrfs_root *root, struct extent_buffer *leaf) | |
6407bf6d | 3388 | { |
7bb86316 CM |
3389 | u64 leaf_owner; |
3390 | u64 leaf_generation; | |
5f39d397 | 3391 | struct btrfs_key key; |
6407bf6d CM |
3392 | struct btrfs_file_extent_item *fi; |
3393 | int i; | |
3394 | int nritems; | |
3395 | int ret; | |
3396 | ||
5f39d397 CM |
3397 | BUG_ON(!btrfs_is_leaf(leaf)); |
3398 | nritems = btrfs_header_nritems(leaf); | |
7bb86316 CM |
3399 | leaf_owner = btrfs_header_owner(leaf); |
3400 | leaf_generation = btrfs_header_generation(leaf); | |
3401 | ||
6407bf6d | 3402 | for (i = 0; i < nritems; i++) { |
db94535d | 3403 | u64 disk_bytenr; |
e34a5b4f | 3404 | cond_resched(); |
5f39d397 CM |
3405 | |
3406 | btrfs_item_key_to_cpu(leaf, &key, i); | |
3407 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
6407bf6d CM |
3408 | continue; |
3409 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
5f39d397 CM |
3410 | if (btrfs_file_extent_type(leaf, fi) == |
3411 | BTRFS_FILE_EXTENT_INLINE) | |
236454df | 3412 | continue; |
6407bf6d CM |
3413 | /* |
3414 | * FIXME make sure to insert a trans record that | |
3415 | * repeats the snapshot del on crash | |
3416 | */ | |
db94535d CM |
3417 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
3418 | if (disk_bytenr == 0) | |
3a686375 | 3419 | continue; |
4a096752 | 3420 | |
925baedd | 3421 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
7bb86316 | 3422 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
31840ae1 | 3423 | leaf->start, leaf_owner, leaf_generation, |
3bb1a1bc | 3424 | key.objectid, 0); |
31840ae1 | 3425 | BUG_ON(ret); |
2dd3e67b CM |
3426 | |
3427 | atomic_inc(&root->fs_info->throttle_gen); | |
3428 | wake_up(&root->fs_info->transaction_throttle); | |
3429 | cond_resched(); | |
6407bf6d CM |
3430 | } |
3431 | return 0; | |
3432 | } | |
3433 | ||
d397712b | 3434 | static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
e02119d5 CM |
3435 | struct btrfs_root *root, |
3436 | struct btrfs_leaf_ref *ref) | |
31153d81 YZ |
3437 | { |
3438 | int i; | |
3439 | int ret; | |
3440 | struct btrfs_extent_info *info = ref->extents; | |
3441 | ||
31153d81 | 3442 | for (i = 0; i < ref->nritems; i++) { |
31840ae1 ZY |
3443 | ret = __btrfs_free_extent(trans, root, info->bytenr, |
3444 | info->num_bytes, ref->bytenr, | |
3445 | ref->owner, ref->generation, | |
3bb1a1bc | 3446 | info->objectid, 0); |
2dd3e67b CM |
3447 | |
3448 | atomic_inc(&root->fs_info->throttle_gen); | |
3449 | wake_up(&root->fs_info->transaction_throttle); | |
3450 | cond_resched(); | |
3451 | ||
31153d81 YZ |
3452 | BUG_ON(ret); |
3453 | info++; | |
3454 | } | |
31153d81 YZ |
3455 | |
3456 | return 0; | |
3457 | } | |
3458 | ||
d397712b CM |
3459 | static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, |
3460 | u64 len, u32 *refs) | |
333db94c | 3461 | { |
017e5369 | 3462 | int ret; |
f87f057b | 3463 | |
31840ae1 | 3464 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); |
f87f057b CM |
3465 | BUG_ON(ret); |
3466 | ||
d397712b | 3467 | #if 0 /* some debugging code in case we see problems here */ |
f87f057b CM |
3468 | /* if the refs count is one, it won't get increased again. But |
3469 | * if the ref count is > 1, someone may be decreasing it at | |
3470 | * the same time we are. | |
3471 | */ | |
3472 | if (*refs != 1) { | |
3473 | struct extent_buffer *eb = NULL; | |
3474 | eb = btrfs_find_create_tree_block(root, start, len); | |
3475 | if (eb) | |
3476 | btrfs_tree_lock(eb); | |
3477 | ||
3478 | mutex_lock(&root->fs_info->alloc_mutex); | |
3479 | ret = lookup_extent_ref(NULL, root, start, len, refs); | |
3480 | BUG_ON(ret); | |
3481 | mutex_unlock(&root->fs_info->alloc_mutex); | |
3482 | ||
3483 | if (eb) { | |
3484 | btrfs_tree_unlock(eb); | |
3485 | free_extent_buffer(eb); | |
3486 | } | |
3487 | if (*refs == 1) { | |
d397712b CM |
3488 | printk(KERN_ERR "btrfs block %llu went down to one " |
3489 | "during drop_snap\n", (unsigned long long)start); | |
f87f057b CM |
3490 | } |
3491 | ||
3492 | } | |
3493 | #endif | |
3494 | ||
e7a84565 | 3495 | cond_resched(); |
017e5369 | 3496 | return ret; |
333db94c CM |
3497 | } |
3498 | ||
9aca1d51 CM |
3499 | /* |
3500 | * helper function for drop_snapshot, this walks down the tree dropping ref | |
3501 | * counts as it goes. | |
3502 | */ | |
d397712b | 3503 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
98ed5174 CM |
3504 | struct btrfs_root *root, |
3505 | struct btrfs_path *path, int *level) | |
20524f02 | 3506 | { |
7bb86316 CM |
3507 | u64 root_owner; |
3508 | u64 root_gen; | |
3509 | u64 bytenr; | |
ca7a79ad | 3510 | u64 ptr_gen; |
5f39d397 CM |
3511 | struct extent_buffer *next; |
3512 | struct extent_buffer *cur; | |
7bb86316 | 3513 | struct extent_buffer *parent; |
31153d81 | 3514 | struct btrfs_leaf_ref *ref; |
db94535d | 3515 | u32 blocksize; |
20524f02 CM |
3516 | int ret; |
3517 | u32 refs; | |
3518 | ||
5caf2a00 CM |
3519 | WARN_ON(*level < 0); |
3520 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
333db94c | 3521 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
db94535d | 3522 | path->nodes[*level]->len, &refs); |
20524f02 CM |
3523 | BUG_ON(ret); |
3524 | if (refs > 1) | |
3525 | goto out; | |
e011599b | 3526 | |
9aca1d51 CM |
3527 | /* |
3528 | * walk down to the last node level and free all the leaves | |
3529 | */ | |
d397712b | 3530 | while (*level >= 0) { |
5caf2a00 CM |
3531 | WARN_ON(*level < 0); |
3532 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
20524f02 | 3533 | cur = path->nodes[*level]; |
e011599b | 3534 | |
5f39d397 | 3535 | if (btrfs_header_level(cur) != *level) |
2c90e5d6 | 3536 | WARN_ON(1); |
e011599b | 3537 | |
7518a238 | 3538 | if (path->slots[*level] >= |
5f39d397 | 3539 | btrfs_header_nritems(cur)) |
20524f02 | 3540 | break; |
6407bf6d | 3541 | if (*level == 0) { |
e02119d5 | 3542 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
6407bf6d CM |
3543 | BUG_ON(ret); |
3544 | break; | |
3545 | } | |
db94535d | 3546 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
ca7a79ad | 3547 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
db94535d | 3548 | blocksize = btrfs_level_size(root, *level - 1); |
925baedd | 3549 | |
333db94c | 3550 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
6407bf6d CM |
3551 | BUG_ON(ret); |
3552 | if (refs != 1) { | |
7bb86316 CM |
3553 | parent = path->nodes[*level]; |
3554 | root_owner = btrfs_header_owner(parent); | |
3555 | root_gen = btrfs_header_generation(parent); | |
20524f02 | 3556 | path->slots[*level]++; |
f87f057b | 3557 | |
925baedd | 3558 | ret = __btrfs_free_extent(trans, root, bytenr, |
31840ae1 | 3559 | blocksize, parent->start, |
3bb1a1bc YZ |
3560 | root_owner, root_gen, |
3561 | *level - 1, 1); | |
20524f02 | 3562 | BUG_ON(ret); |
18e35e0a CM |
3563 | |
3564 | atomic_inc(&root->fs_info->throttle_gen); | |
3565 | wake_up(&root->fs_info->transaction_throttle); | |
2dd3e67b | 3566 | cond_resched(); |
18e35e0a | 3567 | |
20524f02 CM |
3568 | continue; |
3569 | } | |
f87f057b CM |
3570 | /* |
3571 | * at this point, we have a single ref, and since the | |
3572 | * only place referencing this extent is a dead root | |
3573 | * the reference count should never go higher. | |
3574 | * So, we don't need to check it again | |
3575 | */ | |
31153d81 | 3576 | if (*level == 1) { |
017e5369 | 3577 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
1a40e23b ZY |
3578 | if (ref && ref->generation != ptr_gen) { |
3579 | btrfs_free_leaf_ref(root, ref); | |
3580 | ref = NULL; | |
3581 | } | |
31153d81 | 3582 | if (ref) { |
e02119d5 | 3583 | ret = cache_drop_leaf_ref(trans, root, ref); |
31153d81 YZ |
3584 | BUG_ON(ret); |
3585 | btrfs_remove_leaf_ref(root, ref); | |
bcc63abb | 3586 | btrfs_free_leaf_ref(root, ref); |
31153d81 YZ |
3587 | *level = 0; |
3588 | break; | |
3589 | } | |
3590 | } | |
db94535d | 3591 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
1259ab75 | 3592 | if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) { |
5f39d397 | 3593 | free_extent_buffer(next); |
333db94c | 3594 | |
ca7a79ad CM |
3595 | next = read_tree_block(root, bytenr, blocksize, |
3596 | ptr_gen); | |
e7a84565 | 3597 | cond_resched(); |
f87f057b CM |
3598 | #if 0 |
3599 | /* | |
3600 | * this is a debugging check and can go away | |
3601 | * the ref should never go all the way down to 1 | |
3602 | * at this point | |
3603 | */ | |
e6dcd2dc CM |
3604 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, |
3605 | &refs); | |
e9d0b13b | 3606 | BUG_ON(ret); |
f87f057b CM |
3607 | WARN_ON(refs != 1); |
3608 | #endif | |
e9d0b13b | 3609 | } |
5caf2a00 | 3610 | WARN_ON(*level <= 0); |
83e15a28 | 3611 | if (path->nodes[*level-1]) |
5f39d397 | 3612 | free_extent_buffer(path->nodes[*level-1]); |
20524f02 | 3613 | path->nodes[*level-1] = next; |
5f39d397 | 3614 | *level = btrfs_header_level(next); |
20524f02 | 3615 | path->slots[*level] = 0; |
2dd3e67b | 3616 | cond_resched(); |
20524f02 CM |
3617 | } |
3618 | out: | |
5caf2a00 CM |
3619 | WARN_ON(*level < 0); |
3620 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
7bb86316 CM |
3621 | |
3622 | if (path->nodes[*level] == root->node) { | |
7bb86316 | 3623 | parent = path->nodes[*level]; |
31153d81 | 3624 | bytenr = path->nodes[*level]->start; |
7bb86316 CM |
3625 | } else { |
3626 | parent = path->nodes[*level + 1]; | |
31153d81 | 3627 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
7bb86316 CM |
3628 | } |
3629 | ||
31153d81 YZ |
3630 | blocksize = btrfs_level_size(root, *level); |
3631 | root_owner = btrfs_header_owner(parent); | |
7bb86316 | 3632 | root_gen = btrfs_header_generation(parent); |
31153d81 YZ |
3633 | |
3634 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, | |
31840ae1 | 3635 | parent->start, root_owner, root_gen, |
3bb1a1bc | 3636 | *level, 1); |
5f39d397 | 3637 | free_extent_buffer(path->nodes[*level]); |
20524f02 CM |
3638 | path->nodes[*level] = NULL; |
3639 | *level += 1; | |
3640 | BUG_ON(ret); | |
f87f057b | 3641 | |
e7a84565 | 3642 | cond_resched(); |
20524f02 CM |
3643 | return 0; |
3644 | } | |
3645 | ||
f82d02d9 YZ |
3646 | /* |
3647 | * helper function for drop_subtree, this function is similar to | |
3648 | * walk_down_tree. The main difference is that it checks reference | |
3649 | * counts while tree blocks are locked. | |
3650 | */ | |
d397712b | 3651 | static noinline int walk_down_subtree(struct btrfs_trans_handle *trans, |
f82d02d9 YZ |
3652 | struct btrfs_root *root, |
3653 | struct btrfs_path *path, int *level) | |
3654 | { | |
3655 | struct extent_buffer *next; | |
3656 | struct extent_buffer *cur; | |
3657 | struct extent_buffer *parent; | |
3658 | u64 bytenr; | |
3659 | u64 ptr_gen; | |
3660 | u32 blocksize; | |
3661 | u32 refs; | |
3662 | int ret; | |
3663 | ||
3664 | cur = path->nodes[*level]; | |
3665 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, | |
3666 | &refs); | |
3667 | BUG_ON(ret); | |
3668 | if (refs > 1) | |
3669 | goto out; | |
3670 | ||
3671 | while (*level >= 0) { | |
3672 | cur = path->nodes[*level]; | |
3673 | if (*level == 0) { | |
3674 | ret = btrfs_drop_leaf_ref(trans, root, cur); | |
3675 | BUG_ON(ret); | |
3676 | clean_tree_block(trans, root, cur); | |
3677 | break; | |
3678 | } | |
3679 | if (path->slots[*level] >= btrfs_header_nritems(cur)) { | |
3680 | clean_tree_block(trans, root, cur); | |
3681 | break; | |
3682 | } | |
3683 | ||
3684 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); | |
3685 | blocksize = btrfs_level_size(root, *level - 1); | |
3686 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); | |
3687 | ||
3688 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); | |
3689 | btrfs_tree_lock(next); | |
3690 | ||
3691 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, | |
3692 | &refs); | |
3693 | BUG_ON(ret); | |
3694 | if (refs > 1) { | |
3695 | parent = path->nodes[*level]; | |
3696 | ret = btrfs_free_extent(trans, root, bytenr, | |
3697 | blocksize, parent->start, | |
3698 | btrfs_header_owner(parent), | |
3699 | btrfs_header_generation(parent), | |
3700 | *level - 1, 1); | |
3701 | BUG_ON(ret); | |
3702 | path->slots[*level]++; | |
3703 | btrfs_tree_unlock(next); | |
3704 | free_extent_buffer(next); | |
3705 | continue; | |
3706 | } | |
3707 | ||
3708 | *level = btrfs_header_level(next); | |
3709 | path->nodes[*level] = next; | |
3710 | path->slots[*level] = 0; | |
3711 | path->locks[*level] = 1; | |
3712 | cond_resched(); | |
3713 | } | |
3714 | out: | |
3715 | parent = path->nodes[*level + 1]; | |
3716 | bytenr = path->nodes[*level]->start; | |
3717 | blocksize = path->nodes[*level]->len; | |
3718 | ||
3719 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, | |
3720 | parent->start, btrfs_header_owner(parent), | |
3721 | btrfs_header_generation(parent), *level, 1); | |
3722 | BUG_ON(ret); | |
3723 | ||
3724 | if (path->locks[*level]) { | |
3725 | btrfs_tree_unlock(path->nodes[*level]); | |
3726 | path->locks[*level] = 0; | |
3727 | } | |
3728 | free_extent_buffer(path->nodes[*level]); | |
3729 | path->nodes[*level] = NULL; | |
3730 | *level += 1; | |
3731 | cond_resched(); | |
3732 | return 0; | |
3733 | } | |
3734 | ||
9aca1d51 CM |
3735 | /* |
3736 | * helper for dropping snapshots. This walks back up the tree in the path | |
3737 | * to find the first node higher up where we haven't yet gone through | |
3738 | * all the slots | |
3739 | */ | |
d397712b | 3740 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
98ed5174 | 3741 | struct btrfs_root *root, |
f82d02d9 YZ |
3742 | struct btrfs_path *path, |
3743 | int *level, int max_level) | |
20524f02 | 3744 | { |
7bb86316 CM |
3745 | u64 root_owner; |
3746 | u64 root_gen; | |
3747 | struct btrfs_root_item *root_item = &root->root_item; | |
20524f02 CM |
3748 | int i; |
3749 | int slot; | |
3750 | int ret; | |
9f3a7427 | 3751 | |
f82d02d9 | 3752 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
20524f02 | 3753 | slot = path->slots[i]; |
5f39d397 CM |
3754 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
3755 | struct extent_buffer *node; | |
3756 | struct btrfs_disk_key disk_key; | |
3757 | node = path->nodes[i]; | |
20524f02 CM |
3758 | path->slots[i]++; |
3759 | *level = i; | |
9f3a7427 | 3760 | WARN_ON(*level == 0); |
5f39d397 | 3761 | btrfs_node_key(node, &disk_key, path->slots[i]); |
9f3a7427 | 3762 | memcpy(&root_item->drop_progress, |
5f39d397 | 3763 | &disk_key, sizeof(disk_key)); |
9f3a7427 | 3764 | root_item->drop_level = i; |
20524f02 CM |
3765 | return 0; |
3766 | } else { | |
31840ae1 ZY |
3767 | struct extent_buffer *parent; |
3768 | if (path->nodes[*level] == root->node) | |
3769 | parent = path->nodes[*level]; | |
3770 | else | |
3771 | parent = path->nodes[*level + 1]; | |
3772 | ||
3773 | root_owner = btrfs_header_owner(parent); | |
3774 | root_gen = btrfs_header_generation(parent); | |
f82d02d9 YZ |
3775 | |
3776 | clean_tree_block(trans, root, path->nodes[*level]); | |
e089f05c | 3777 | ret = btrfs_free_extent(trans, root, |
db94535d | 3778 | path->nodes[*level]->start, |
7bb86316 | 3779 | path->nodes[*level]->len, |
3bb1a1bc YZ |
3780 | parent->start, root_owner, |
3781 | root_gen, *level, 1); | |
6407bf6d | 3782 | BUG_ON(ret); |
f82d02d9 YZ |
3783 | if (path->locks[*level]) { |
3784 | btrfs_tree_unlock(path->nodes[*level]); | |
3785 | path->locks[*level] = 0; | |
3786 | } | |
5f39d397 | 3787 | free_extent_buffer(path->nodes[*level]); |
83e15a28 | 3788 | path->nodes[*level] = NULL; |
20524f02 | 3789 | *level = i + 1; |
20524f02 CM |
3790 | } |
3791 | } | |
3792 | return 1; | |
3793 | } | |
3794 | ||
9aca1d51 CM |
3795 | /* |
3796 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
3797 | * the tree freeing any blocks that have a ref count of zero after being | |
3798 | * decremented. | |
3799 | */ | |
e089f05c | 3800 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
9f3a7427 | 3801 | *root) |
20524f02 | 3802 | { |
3768f368 | 3803 | int ret = 0; |
9aca1d51 | 3804 | int wret; |
20524f02 | 3805 | int level; |
5caf2a00 | 3806 | struct btrfs_path *path; |
20524f02 CM |
3807 | int i; |
3808 | int orig_level; | |
9f3a7427 | 3809 | struct btrfs_root_item *root_item = &root->root_item; |
20524f02 | 3810 | |
a2135011 | 3811 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
5caf2a00 CM |
3812 | path = btrfs_alloc_path(); |
3813 | BUG_ON(!path); | |
20524f02 | 3814 | |
5f39d397 | 3815 | level = btrfs_header_level(root->node); |
20524f02 | 3816 | orig_level = level; |
9f3a7427 CM |
3817 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
3818 | path->nodes[level] = root->node; | |
f510cfec | 3819 | extent_buffer_get(root->node); |
9f3a7427 CM |
3820 | path->slots[level] = 0; |
3821 | } else { | |
3822 | struct btrfs_key key; | |
5f39d397 CM |
3823 | struct btrfs_disk_key found_key; |
3824 | struct extent_buffer *node; | |
6702ed49 | 3825 | |
9f3a7427 | 3826 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
6702ed49 CM |
3827 | level = root_item->drop_level; |
3828 | path->lowest_level = level; | |
9f3a7427 | 3829 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
6702ed49 | 3830 | if (wret < 0) { |
9f3a7427 CM |
3831 | ret = wret; |
3832 | goto out; | |
3833 | } | |
5f39d397 CM |
3834 | node = path->nodes[level]; |
3835 | btrfs_node_key(node, &found_key, path->slots[level]); | |
3836 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, | |
3837 | sizeof(found_key))); | |
7d9eb12c CM |
3838 | /* |
3839 | * unlock our path, this is safe because only this | |
3840 | * function is allowed to delete this snapshot | |
3841 | */ | |
925baedd CM |
3842 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
3843 | if (path->nodes[i] && path->locks[i]) { | |
3844 | path->locks[i] = 0; | |
3845 | btrfs_tree_unlock(path->nodes[i]); | |
3846 | } | |
3847 | } | |
9f3a7427 | 3848 | } |
d397712b | 3849 | while (1) { |
5caf2a00 | 3850 | wret = walk_down_tree(trans, root, path, &level); |
9aca1d51 | 3851 | if (wret > 0) |
20524f02 | 3852 | break; |
9aca1d51 CM |
3853 | if (wret < 0) |
3854 | ret = wret; | |
3855 | ||
f82d02d9 YZ |
3856 | wret = walk_up_tree(trans, root, path, &level, |
3857 | BTRFS_MAX_LEVEL); | |
9aca1d51 | 3858 | if (wret > 0) |
20524f02 | 3859 | break; |
9aca1d51 CM |
3860 | if (wret < 0) |
3861 | ret = wret; | |
e7a84565 CM |
3862 | if (trans->transaction->in_commit) { |
3863 | ret = -EAGAIN; | |
3864 | break; | |
3865 | } | |
18e35e0a | 3866 | atomic_inc(&root->fs_info->throttle_gen); |
017e5369 | 3867 | wake_up(&root->fs_info->transaction_throttle); |
20524f02 | 3868 | } |
83e15a28 | 3869 | for (i = 0; i <= orig_level; i++) { |
5caf2a00 | 3870 | if (path->nodes[i]) { |
5f39d397 | 3871 | free_extent_buffer(path->nodes[i]); |
0f82731f | 3872 | path->nodes[i] = NULL; |
83e15a28 | 3873 | } |
20524f02 | 3874 | } |
9f3a7427 | 3875 | out: |
5caf2a00 | 3876 | btrfs_free_path(path); |
9aca1d51 | 3877 | return ret; |
20524f02 | 3878 | } |
9078a3e1 | 3879 | |
f82d02d9 YZ |
3880 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
3881 | struct btrfs_root *root, | |
3882 | struct extent_buffer *node, | |
3883 | struct extent_buffer *parent) | |
3884 | { | |
3885 | struct btrfs_path *path; | |
3886 | int level; | |
3887 | int parent_level; | |
3888 | int ret = 0; | |
3889 | int wret; | |
3890 | ||
3891 | path = btrfs_alloc_path(); | |
3892 | BUG_ON(!path); | |
3893 | ||
3894 | BUG_ON(!btrfs_tree_locked(parent)); | |
3895 | parent_level = btrfs_header_level(parent); | |
3896 | extent_buffer_get(parent); | |
3897 | path->nodes[parent_level] = parent; | |
3898 | path->slots[parent_level] = btrfs_header_nritems(parent); | |
3899 | ||
3900 | BUG_ON(!btrfs_tree_locked(node)); | |
3901 | level = btrfs_header_level(node); | |
3902 | extent_buffer_get(node); | |
3903 | path->nodes[level] = node; | |
3904 | path->slots[level] = 0; | |
3905 | ||
3906 | while (1) { | |
3907 | wret = walk_down_subtree(trans, root, path, &level); | |
3908 | if (wret < 0) | |
3909 | ret = wret; | |
3910 | if (wret != 0) | |
3911 | break; | |
3912 | ||
3913 | wret = walk_up_tree(trans, root, path, &level, parent_level); | |
3914 | if (wret < 0) | |
3915 | ret = wret; | |
3916 | if (wret != 0) | |
3917 | break; | |
3918 | } | |
3919 | ||
3920 | btrfs_free_path(path); | |
3921 | return ret; | |
3922 | } | |
3923 | ||
8e7bf94f CM |
3924 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
3925 | unsigned long nr) | |
3926 | { | |
3927 | return min(last, start + nr - 1); | |
3928 | } | |
3929 | ||
d397712b | 3930 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
98ed5174 | 3931 | u64 len) |
edbd8d4e CM |
3932 | { |
3933 | u64 page_start; | |
3934 | u64 page_end; | |
1a40e23b | 3935 | unsigned long first_index; |
edbd8d4e | 3936 | unsigned long last_index; |
edbd8d4e CM |
3937 | unsigned long i; |
3938 | struct page *page; | |
d1310b2e | 3939 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4313b399 | 3940 | struct file_ra_state *ra; |
3eaa2885 | 3941 | struct btrfs_ordered_extent *ordered; |
1a40e23b ZY |
3942 | unsigned int total_read = 0; |
3943 | unsigned int total_dirty = 0; | |
3944 | int ret = 0; | |
4313b399 CM |
3945 | |
3946 | ra = kzalloc(sizeof(*ra), GFP_NOFS); | |
edbd8d4e CM |
3947 | |
3948 | mutex_lock(&inode->i_mutex); | |
1a40e23b | 3949 | first_index = start >> PAGE_CACHE_SHIFT; |
edbd8d4e CM |
3950 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
3951 | ||
1a40e23b ZY |
3952 | /* make sure the dirty trick played by the caller work */ |
3953 | ret = invalidate_inode_pages2_range(inode->i_mapping, | |
3954 | first_index, last_index); | |
3955 | if (ret) | |
3956 | goto out_unlock; | |
8e7bf94f | 3957 | |
4313b399 | 3958 | file_ra_state_init(ra, inode->i_mapping); |
edbd8d4e | 3959 | |
1a40e23b ZY |
3960 | for (i = first_index ; i <= last_index; i++) { |
3961 | if (total_read % ra->ra_pages == 0) { | |
8e7bf94f | 3962 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
1a40e23b | 3963 | calc_ra(i, last_index, ra->ra_pages)); |
8e7bf94f CM |
3964 | } |
3965 | total_read++; | |
3eaa2885 CM |
3966 | again: |
3967 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) | |
1a40e23b | 3968 | BUG_ON(1); |
edbd8d4e | 3969 | page = grab_cache_page(inode->i_mapping, i); |
a061fc8d | 3970 | if (!page) { |
1a40e23b | 3971 | ret = -ENOMEM; |
edbd8d4e | 3972 | goto out_unlock; |
a061fc8d | 3973 | } |
edbd8d4e CM |
3974 | if (!PageUptodate(page)) { |
3975 | btrfs_readpage(NULL, page); | |
3976 | lock_page(page); | |
3977 | if (!PageUptodate(page)) { | |
3978 | unlock_page(page); | |
3979 | page_cache_release(page); | |
1a40e23b | 3980 | ret = -EIO; |
edbd8d4e CM |
3981 | goto out_unlock; |
3982 | } | |
3983 | } | |
ec44a35c | 3984 | wait_on_page_writeback(page); |
3eaa2885 | 3985 | |
edbd8d4e CM |
3986 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
3987 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
d1310b2e | 3988 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
edbd8d4e | 3989 | |
3eaa2885 CM |
3990 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
3991 | if (ordered) { | |
3992 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
3993 | unlock_page(page); | |
3994 | page_cache_release(page); | |
3995 | btrfs_start_ordered_extent(inode, ordered, 1); | |
3996 | btrfs_put_ordered_extent(ordered); | |
3997 | goto again; | |
3998 | } | |
3999 | set_page_extent_mapped(page); | |
4000 | ||
1a40e23b ZY |
4001 | if (i == first_index) |
4002 | set_extent_bits(io_tree, page_start, page_end, | |
4003 | EXTENT_BOUNDARY, GFP_NOFS); | |
1f80e4db | 4004 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
1a40e23b | 4005 | |
a061fc8d | 4006 | set_page_dirty(page); |
1a40e23b | 4007 | total_dirty++; |
edbd8d4e | 4008 | |
d1310b2e | 4009 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
edbd8d4e CM |
4010 | unlock_page(page); |
4011 | page_cache_release(page); | |
4012 | } | |
4013 | ||
4014 | out_unlock: | |
ec44a35c | 4015 | kfree(ra); |
edbd8d4e | 4016 | mutex_unlock(&inode->i_mutex); |
1a40e23b ZY |
4017 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
4018 | return ret; | |
edbd8d4e CM |
4019 | } |
4020 | ||
d397712b | 4021 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
1a40e23b ZY |
4022 | struct btrfs_key *extent_key, |
4023 | u64 offset) | |
4024 | { | |
4025 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; | |
4026 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; | |
4027 | struct extent_map *em; | |
6643558d YZ |
4028 | u64 start = extent_key->objectid - offset; |
4029 | u64 end = start + extent_key->offset - 1; | |
bf4ef679 | 4030 | |
1a40e23b ZY |
4031 | em = alloc_extent_map(GFP_NOFS); |
4032 | BUG_ON(!em || IS_ERR(em)); | |
bf4ef679 | 4033 | |
6643558d | 4034 | em->start = start; |
1a40e23b | 4035 | em->len = extent_key->offset; |
c8b97818 | 4036 | em->block_len = extent_key->offset; |
1a40e23b ZY |
4037 | em->block_start = extent_key->objectid; |
4038 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
4039 | set_bit(EXTENT_FLAG_PINNED, &em->flags); | |
4040 | ||
4041 | /* setup extent map to cheat btrfs_readpage */ | |
6643558d | 4042 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
1a40e23b ZY |
4043 | while (1) { |
4044 | int ret; | |
4045 | spin_lock(&em_tree->lock); | |
4046 | ret = add_extent_mapping(em_tree, em); | |
4047 | spin_unlock(&em_tree->lock); | |
4048 | if (ret != -EEXIST) { | |
4049 | free_extent_map(em); | |
bf4ef679 CM |
4050 | break; |
4051 | } | |
6643558d | 4052 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
bf4ef679 | 4053 | } |
6643558d | 4054 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
bf4ef679 | 4055 | |
6643558d | 4056 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
1a40e23b | 4057 | } |
edbd8d4e | 4058 | |
1a40e23b ZY |
4059 | struct btrfs_ref_path { |
4060 | u64 extent_start; | |
4061 | u64 nodes[BTRFS_MAX_LEVEL]; | |
4062 | u64 root_objectid; | |
4063 | u64 root_generation; | |
4064 | u64 owner_objectid; | |
1a40e23b ZY |
4065 | u32 num_refs; |
4066 | int lowest_level; | |
4067 | int current_level; | |
f82d02d9 YZ |
4068 | int shared_level; |
4069 | ||
4070 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; | |
4071 | u64 new_nodes[BTRFS_MAX_LEVEL]; | |
1a40e23b | 4072 | }; |
7d9eb12c | 4073 | |
1a40e23b | 4074 | struct disk_extent { |
c8b97818 | 4075 | u64 ram_bytes; |
1a40e23b ZY |
4076 | u64 disk_bytenr; |
4077 | u64 disk_num_bytes; | |
4078 | u64 offset; | |
4079 | u64 num_bytes; | |
c8b97818 CM |
4080 | u8 compression; |
4081 | u8 encryption; | |
4082 | u16 other_encoding; | |
1a40e23b | 4083 | }; |
4313b399 | 4084 | |
1a40e23b ZY |
4085 | static int is_cowonly_root(u64 root_objectid) |
4086 | { | |
4087 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || | |
4088 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || | |
4089 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || | |
4090 | root_objectid == BTRFS_DEV_TREE_OBJECTID || | |
0403e47e YZ |
4091 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
4092 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) | |
1a40e23b ZY |
4093 | return 1; |
4094 | return 0; | |
4095 | } | |
edbd8d4e | 4096 | |
d397712b | 4097 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
1a40e23b ZY |
4098 | struct btrfs_root *extent_root, |
4099 | struct btrfs_ref_path *ref_path, | |
4100 | int first_time) | |
4101 | { | |
4102 | struct extent_buffer *leaf; | |
4103 | struct btrfs_path *path; | |
4104 | struct btrfs_extent_ref *ref; | |
4105 | struct btrfs_key key; | |
4106 | struct btrfs_key found_key; | |
4107 | u64 bytenr; | |
4108 | u32 nritems; | |
4109 | int level; | |
4110 | int ret = 1; | |
edbd8d4e | 4111 | |
1a40e23b ZY |
4112 | path = btrfs_alloc_path(); |
4113 | if (!path) | |
4114 | return -ENOMEM; | |
bf4ef679 | 4115 | |
1a40e23b ZY |
4116 | if (first_time) { |
4117 | ref_path->lowest_level = -1; | |
4118 | ref_path->current_level = -1; | |
f82d02d9 | 4119 | ref_path->shared_level = -1; |
1a40e23b ZY |
4120 | goto walk_up; |
4121 | } | |
4122 | walk_down: | |
4123 | level = ref_path->current_level - 1; | |
4124 | while (level >= -1) { | |
4125 | u64 parent; | |
4126 | if (level < ref_path->lowest_level) | |
4127 | break; | |
bf4ef679 | 4128 | |
d397712b | 4129 | if (level >= 0) |
1a40e23b | 4130 | bytenr = ref_path->nodes[level]; |
d397712b | 4131 | else |
1a40e23b | 4132 | bytenr = ref_path->extent_start; |
1a40e23b | 4133 | BUG_ON(bytenr == 0); |
bf4ef679 | 4134 | |
1a40e23b ZY |
4135 | parent = ref_path->nodes[level + 1]; |
4136 | ref_path->nodes[level + 1] = 0; | |
4137 | ref_path->current_level = level; | |
4138 | BUG_ON(parent == 0); | |
0ef3e66b | 4139 | |
1a40e23b ZY |
4140 | key.objectid = bytenr; |
4141 | key.offset = parent + 1; | |
4142 | key.type = BTRFS_EXTENT_REF_KEY; | |
edbd8d4e | 4143 | |
1a40e23b ZY |
4144 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
4145 | if (ret < 0) | |
edbd8d4e | 4146 | goto out; |
1a40e23b | 4147 | BUG_ON(ret == 0); |
7d9eb12c | 4148 | |
1a40e23b ZY |
4149 | leaf = path->nodes[0]; |
4150 | nritems = btrfs_header_nritems(leaf); | |
4151 | if (path->slots[0] >= nritems) { | |
4152 | ret = btrfs_next_leaf(extent_root, path); | |
4153 | if (ret < 0) | |
4154 | goto out; | |
4155 | if (ret > 0) | |
4156 | goto next; | |
4157 | leaf = path->nodes[0]; | |
4158 | } | |
0ef3e66b | 4159 | |
1a40e23b ZY |
4160 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
4161 | if (found_key.objectid == bytenr && | |
f82d02d9 YZ |
4162 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
4163 | if (level < ref_path->shared_level) | |
4164 | ref_path->shared_level = level; | |
1a40e23b | 4165 | goto found; |
f82d02d9 | 4166 | } |
1a40e23b ZY |
4167 | next: |
4168 | level--; | |
4169 | btrfs_release_path(extent_root, path); | |
d899e052 | 4170 | cond_resched(); |
1a40e23b ZY |
4171 | } |
4172 | /* reached lowest level */ | |
4173 | ret = 1; | |
4174 | goto out; | |
4175 | walk_up: | |
4176 | level = ref_path->current_level; | |
4177 | while (level < BTRFS_MAX_LEVEL - 1) { | |
4178 | u64 ref_objectid; | |
d397712b CM |
4179 | |
4180 | if (level >= 0) | |
1a40e23b | 4181 | bytenr = ref_path->nodes[level]; |
d397712b | 4182 | else |
1a40e23b | 4183 | bytenr = ref_path->extent_start; |
d397712b | 4184 | |
1a40e23b | 4185 | BUG_ON(bytenr == 0); |
edbd8d4e | 4186 | |
1a40e23b ZY |
4187 | key.objectid = bytenr; |
4188 | key.offset = 0; | |
4189 | key.type = BTRFS_EXTENT_REF_KEY; | |
edbd8d4e | 4190 | |
1a40e23b ZY |
4191 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
4192 | if (ret < 0) | |
4193 | goto out; | |
edbd8d4e | 4194 | |
1a40e23b ZY |
4195 | leaf = path->nodes[0]; |
4196 | nritems = btrfs_header_nritems(leaf); | |
4197 | if (path->slots[0] >= nritems) { | |
4198 | ret = btrfs_next_leaf(extent_root, path); | |
4199 | if (ret < 0) | |
4200 | goto out; | |
4201 | if (ret > 0) { | |
4202 | /* the extent was freed by someone */ | |
4203 | if (ref_path->lowest_level == level) | |
4204 | goto out; | |
4205 | btrfs_release_path(extent_root, path); | |
4206 | goto walk_down; | |
4207 | } | |
4208 | leaf = path->nodes[0]; | |
4209 | } | |
edbd8d4e | 4210 | |
1a40e23b ZY |
4211 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
4212 | if (found_key.objectid != bytenr || | |
4213 | found_key.type != BTRFS_EXTENT_REF_KEY) { | |
4214 | /* the extent was freed by someone */ | |
4215 | if (ref_path->lowest_level == level) { | |
4216 | ret = 1; | |
4217 | goto out; | |
4218 | } | |
4219 | btrfs_release_path(extent_root, path); | |
4220 | goto walk_down; | |
4221 | } | |
4222 | found: | |
4223 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
4224 | struct btrfs_extent_ref); | |
4225 | ref_objectid = btrfs_ref_objectid(leaf, ref); | |
4226 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { | |
4227 | if (first_time) { | |
4228 | level = (int)ref_objectid; | |
4229 | BUG_ON(level >= BTRFS_MAX_LEVEL); | |
4230 | ref_path->lowest_level = level; | |
4231 | ref_path->current_level = level; | |
4232 | ref_path->nodes[level] = bytenr; | |
4233 | } else { | |
4234 | WARN_ON(ref_objectid != level); | |
4235 | } | |
4236 | } else { | |
4237 | WARN_ON(level != -1); | |
4238 | } | |
4239 | first_time = 0; | |
bf4ef679 | 4240 | |
1a40e23b ZY |
4241 | if (ref_path->lowest_level == level) { |
4242 | ref_path->owner_objectid = ref_objectid; | |
1a40e23b ZY |
4243 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
4244 | } | |
bf4ef679 | 4245 | |
7d9eb12c | 4246 | /* |
1a40e23b ZY |
4247 | * the block is tree root or the block isn't in reference |
4248 | * counted tree. | |
7d9eb12c | 4249 | */ |
1a40e23b ZY |
4250 | if (found_key.objectid == found_key.offset || |
4251 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { | |
4252 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); | |
4253 | ref_path->root_generation = | |
4254 | btrfs_ref_generation(leaf, ref); | |
4255 | if (level < 0) { | |
4256 | /* special reference from the tree log */ | |
4257 | ref_path->nodes[0] = found_key.offset; | |
4258 | ref_path->current_level = 0; | |
4259 | } | |
4260 | ret = 0; | |
4261 | goto out; | |
4262 | } | |
7d9eb12c | 4263 | |
1a40e23b ZY |
4264 | level++; |
4265 | BUG_ON(ref_path->nodes[level] != 0); | |
4266 | ref_path->nodes[level] = found_key.offset; | |
4267 | ref_path->current_level = level; | |
bf4ef679 | 4268 | |
1a40e23b ZY |
4269 | /* |
4270 | * the reference was created in the running transaction, | |
4271 | * no need to continue walking up. | |
4272 | */ | |
4273 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { | |
4274 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); | |
4275 | ref_path->root_generation = | |
4276 | btrfs_ref_generation(leaf, ref); | |
4277 | ret = 0; | |
4278 | goto out; | |
7d9eb12c CM |
4279 | } |
4280 | ||
1a40e23b | 4281 | btrfs_release_path(extent_root, path); |
d899e052 | 4282 | cond_resched(); |
7d9eb12c | 4283 | } |
1a40e23b ZY |
4284 | /* reached max tree level, but no tree root found. */ |
4285 | BUG(); | |
edbd8d4e | 4286 | out: |
1a40e23b ZY |
4287 | btrfs_free_path(path); |
4288 | return ret; | |
edbd8d4e CM |
4289 | } |
4290 | ||
1a40e23b ZY |
4291 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
4292 | struct btrfs_root *extent_root, | |
4293 | struct btrfs_ref_path *ref_path, | |
4294 | u64 extent_start) | |
a061fc8d | 4295 | { |
1a40e23b ZY |
4296 | memset(ref_path, 0, sizeof(*ref_path)); |
4297 | ref_path->extent_start = extent_start; | |
a061fc8d | 4298 | |
1a40e23b | 4299 | return __next_ref_path(trans, extent_root, ref_path, 1); |
a061fc8d CM |
4300 | } |
4301 | ||
1a40e23b ZY |
4302 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
4303 | struct btrfs_root *extent_root, | |
4304 | struct btrfs_ref_path *ref_path) | |
edbd8d4e | 4305 | { |
1a40e23b ZY |
4306 | return __next_ref_path(trans, extent_root, ref_path, 0); |
4307 | } | |
4308 | ||
d397712b | 4309 | static noinline int get_new_locations(struct inode *reloc_inode, |
1a40e23b ZY |
4310 | struct btrfs_key *extent_key, |
4311 | u64 offset, int no_fragment, | |
4312 | struct disk_extent **extents, | |
4313 | int *nr_extents) | |
4314 | { | |
4315 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; | |
4316 | struct btrfs_path *path; | |
4317 | struct btrfs_file_extent_item *fi; | |
edbd8d4e | 4318 | struct extent_buffer *leaf; |
1a40e23b ZY |
4319 | struct disk_extent *exts = *extents; |
4320 | struct btrfs_key found_key; | |
4321 | u64 cur_pos; | |
4322 | u64 last_byte; | |
edbd8d4e | 4323 | u32 nritems; |
1a40e23b ZY |
4324 | int nr = 0; |
4325 | int max = *nr_extents; | |
4326 | int ret; | |
edbd8d4e | 4327 | |
1a40e23b ZY |
4328 | WARN_ON(!no_fragment && *extents); |
4329 | if (!exts) { | |
4330 | max = 1; | |
4331 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); | |
4332 | if (!exts) | |
4333 | return -ENOMEM; | |
a061fc8d | 4334 | } |
edbd8d4e | 4335 | |
1a40e23b ZY |
4336 | path = btrfs_alloc_path(); |
4337 | BUG_ON(!path); | |
edbd8d4e | 4338 | |
1a40e23b ZY |
4339 | cur_pos = extent_key->objectid - offset; |
4340 | last_byte = extent_key->objectid + extent_key->offset; | |
4341 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, | |
4342 | cur_pos, 0); | |
4343 | if (ret < 0) | |
4344 | goto out; | |
4345 | if (ret > 0) { | |
4346 | ret = -ENOENT; | |
4347 | goto out; | |
4348 | } | |
edbd8d4e | 4349 | |
1a40e23b | 4350 | while (1) { |
edbd8d4e CM |
4351 | leaf = path->nodes[0]; |
4352 | nritems = btrfs_header_nritems(leaf); | |
1a40e23b ZY |
4353 | if (path->slots[0] >= nritems) { |
4354 | ret = btrfs_next_leaf(root, path); | |
a061fc8d CM |
4355 | if (ret < 0) |
4356 | goto out; | |
1a40e23b ZY |
4357 | if (ret > 0) |
4358 | break; | |
bf4ef679 | 4359 | leaf = path->nodes[0]; |
a061fc8d | 4360 | } |
edbd8d4e CM |
4361 | |
4362 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1a40e23b ZY |
4363 | if (found_key.offset != cur_pos || |
4364 | found_key.type != BTRFS_EXTENT_DATA_KEY || | |
4365 | found_key.objectid != reloc_inode->i_ino) | |
edbd8d4e CM |
4366 | break; |
4367 | ||
1a40e23b ZY |
4368 | fi = btrfs_item_ptr(leaf, path->slots[0], |
4369 | struct btrfs_file_extent_item); | |
4370 | if (btrfs_file_extent_type(leaf, fi) != | |
4371 | BTRFS_FILE_EXTENT_REG || | |
4372 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) | |
edbd8d4e | 4373 | break; |
1a40e23b ZY |
4374 | |
4375 | if (nr == max) { | |
4376 | struct disk_extent *old = exts; | |
4377 | max *= 2; | |
4378 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); | |
4379 | memcpy(exts, old, sizeof(*exts) * nr); | |
4380 | if (old != *extents) | |
4381 | kfree(old); | |
a061fc8d | 4382 | } |
edbd8d4e | 4383 | |
1a40e23b ZY |
4384 | exts[nr].disk_bytenr = |
4385 | btrfs_file_extent_disk_bytenr(leaf, fi); | |
4386 | exts[nr].disk_num_bytes = | |
4387 | btrfs_file_extent_disk_num_bytes(leaf, fi); | |
4388 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); | |
4389 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); | |
c8b97818 CM |
4390 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
4391 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); | |
4392 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); | |
4393 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, | |
4394 | fi); | |
d899e052 YZ |
4395 | BUG_ON(exts[nr].offset > 0); |
4396 | BUG_ON(exts[nr].compression || exts[nr].encryption); | |
4397 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); | |
edbd8d4e | 4398 | |
1a40e23b ZY |
4399 | cur_pos += exts[nr].num_bytes; |
4400 | nr++; | |
4401 | ||
4402 | if (cur_pos + offset >= last_byte) | |
4403 | break; | |
4404 | ||
4405 | if (no_fragment) { | |
4406 | ret = 1; | |
edbd8d4e | 4407 | goto out; |
1a40e23b ZY |
4408 | } |
4409 | path->slots[0]++; | |
4410 | } | |
4411 | ||
1f80e4db | 4412 | BUG_ON(cur_pos + offset > last_byte); |
1a40e23b ZY |
4413 | if (cur_pos + offset < last_byte) { |
4414 | ret = -ENOENT; | |
4415 | goto out; | |
edbd8d4e CM |
4416 | } |
4417 | ret = 0; | |
4418 | out: | |
1a40e23b ZY |
4419 | btrfs_free_path(path); |
4420 | if (ret) { | |
4421 | if (exts != *extents) | |
4422 | kfree(exts); | |
4423 | } else { | |
4424 | *extents = exts; | |
4425 | *nr_extents = nr; | |
4426 | } | |
4427 | return ret; | |
4428 | } | |
4429 | ||
d397712b | 4430 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
1a40e23b ZY |
4431 | struct btrfs_root *root, |
4432 | struct btrfs_path *path, | |
4433 | struct btrfs_key *extent_key, | |
4434 | struct btrfs_key *leaf_key, | |
4435 | struct btrfs_ref_path *ref_path, | |
4436 | struct disk_extent *new_extents, | |
4437 | int nr_extents) | |
4438 | { | |
4439 | struct extent_buffer *leaf; | |
4440 | struct btrfs_file_extent_item *fi; | |
4441 | struct inode *inode = NULL; | |
4442 | struct btrfs_key key; | |
4443 | u64 lock_start = 0; | |
4444 | u64 lock_end = 0; | |
4445 | u64 num_bytes; | |
4446 | u64 ext_offset; | |
4447 | u64 first_pos; | |
4448 | u32 nritems; | |
3bb1a1bc | 4449 | int nr_scaned = 0; |
1a40e23b | 4450 | int extent_locked = 0; |
d899e052 | 4451 | int extent_type; |
1a40e23b ZY |
4452 | int ret; |
4453 | ||
3bb1a1bc YZ |
4454 | memcpy(&key, leaf_key, sizeof(key)); |
4455 | first_pos = INT_LIMIT(loff_t) - extent_key->offset; | |
1a40e23b | 4456 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
3bb1a1bc YZ |
4457 | if (key.objectid < ref_path->owner_objectid || |
4458 | (key.objectid == ref_path->owner_objectid && | |
4459 | key.type < BTRFS_EXTENT_DATA_KEY)) { | |
4460 | key.objectid = ref_path->owner_objectid; | |
4461 | key.type = BTRFS_EXTENT_DATA_KEY; | |
4462 | key.offset = 0; | |
4463 | } | |
1a40e23b ZY |
4464 | } |
4465 | ||
4466 | while (1) { | |
4467 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); | |
4468 | if (ret < 0) | |
4469 | goto out; | |
4470 | ||
4471 | leaf = path->nodes[0]; | |
4472 | nritems = btrfs_header_nritems(leaf); | |
4473 | next: | |
4474 | if (extent_locked && ret > 0) { | |
4475 | /* | |
4476 | * the file extent item was modified by someone | |
4477 | * before the extent got locked. | |
4478 | */ | |
1a40e23b ZY |
4479 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
4480 | lock_end, GFP_NOFS); | |
4481 | extent_locked = 0; | |
4482 | } | |
4483 | ||
4484 | if (path->slots[0] >= nritems) { | |
3bb1a1bc | 4485 | if (++nr_scaned > 2) |
1a40e23b ZY |
4486 | break; |
4487 | ||
4488 | BUG_ON(extent_locked); | |
4489 | ret = btrfs_next_leaf(root, path); | |
4490 | if (ret < 0) | |
4491 | goto out; | |
4492 | if (ret > 0) | |
4493 | break; | |
4494 | leaf = path->nodes[0]; | |
4495 | nritems = btrfs_header_nritems(leaf); | |
4496 | } | |
4497 | ||
4498 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
4499 | ||
4500 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { | |
4501 | if ((key.objectid > ref_path->owner_objectid) || | |
4502 | (key.objectid == ref_path->owner_objectid && | |
4503 | key.type > BTRFS_EXTENT_DATA_KEY) || | |
4504 | (key.offset >= first_pos + extent_key->offset)) | |
4505 | break; | |
4506 | } | |
4507 | ||
4508 | if (inode && key.objectid != inode->i_ino) { | |
4509 | BUG_ON(extent_locked); | |
4510 | btrfs_release_path(root, path); | |
4511 | mutex_unlock(&inode->i_mutex); | |
4512 | iput(inode); | |
4513 | inode = NULL; | |
4514 | continue; | |
4515 | } | |
4516 | ||
4517 | if (key.type != BTRFS_EXTENT_DATA_KEY) { | |
4518 | path->slots[0]++; | |
4519 | ret = 1; | |
4520 | goto next; | |
4521 | } | |
4522 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
4523 | struct btrfs_file_extent_item); | |
d899e052 YZ |
4524 | extent_type = btrfs_file_extent_type(leaf, fi); |
4525 | if ((extent_type != BTRFS_FILE_EXTENT_REG && | |
4526 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || | |
1a40e23b ZY |
4527 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
4528 | extent_key->objectid)) { | |
4529 | path->slots[0]++; | |
4530 | ret = 1; | |
4531 | goto next; | |
4532 | } | |
4533 | ||
4534 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); | |
4535 | ext_offset = btrfs_file_extent_offset(leaf, fi); | |
4536 | ||
4537 | if (first_pos > key.offset - ext_offset) | |
4538 | first_pos = key.offset - ext_offset; | |
4539 | ||
4540 | if (!extent_locked) { | |
4541 | lock_start = key.offset; | |
4542 | lock_end = lock_start + num_bytes - 1; | |
4543 | } else { | |
6643558d YZ |
4544 | if (lock_start > key.offset || |
4545 | lock_end + 1 < key.offset + num_bytes) { | |
4546 | unlock_extent(&BTRFS_I(inode)->io_tree, | |
4547 | lock_start, lock_end, GFP_NOFS); | |
4548 | extent_locked = 0; | |
4549 | } | |
1a40e23b ZY |
4550 | } |
4551 | ||
4552 | if (!inode) { | |
4553 | btrfs_release_path(root, path); | |
4554 | ||
4555 | inode = btrfs_iget_locked(root->fs_info->sb, | |
4556 | key.objectid, root); | |
4557 | if (inode->i_state & I_NEW) { | |
4558 | BTRFS_I(inode)->root = root; | |
4559 | BTRFS_I(inode)->location.objectid = | |
4560 | key.objectid; | |
4561 | BTRFS_I(inode)->location.type = | |
4562 | BTRFS_INODE_ITEM_KEY; | |
4563 | BTRFS_I(inode)->location.offset = 0; | |
4564 | btrfs_read_locked_inode(inode); | |
4565 | unlock_new_inode(inode); | |
4566 | } | |
4567 | /* | |
4568 | * some code call btrfs_commit_transaction while | |
4569 | * holding the i_mutex, so we can't use mutex_lock | |
4570 | * here. | |
4571 | */ | |
4572 | if (is_bad_inode(inode) || | |
4573 | !mutex_trylock(&inode->i_mutex)) { | |
4574 | iput(inode); | |
4575 | inode = NULL; | |
4576 | key.offset = (u64)-1; | |
4577 | goto skip; | |
4578 | } | |
4579 | } | |
4580 | ||
4581 | if (!extent_locked) { | |
4582 | struct btrfs_ordered_extent *ordered; | |
4583 | ||
4584 | btrfs_release_path(root, path); | |
4585 | ||
4586 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, | |
4587 | lock_end, GFP_NOFS); | |
4588 | ordered = btrfs_lookup_first_ordered_extent(inode, | |
4589 | lock_end); | |
4590 | if (ordered && | |
4591 | ordered->file_offset <= lock_end && | |
4592 | ordered->file_offset + ordered->len > lock_start) { | |
4593 | unlock_extent(&BTRFS_I(inode)->io_tree, | |
4594 | lock_start, lock_end, GFP_NOFS); | |
4595 | btrfs_start_ordered_extent(inode, ordered, 1); | |
4596 | btrfs_put_ordered_extent(ordered); | |
4597 | key.offset += num_bytes; | |
4598 | goto skip; | |
4599 | } | |
4600 | if (ordered) | |
4601 | btrfs_put_ordered_extent(ordered); | |
4602 | ||
1a40e23b ZY |
4603 | extent_locked = 1; |
4604 | continue; | |
4605 | } | |
4606 | ||
4607 | if (nr_extents == 1) { | |
4608 | /* update extent pointer in place */ | |
1a40e23b ZY |
4609 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
4610 | new_extents[0].disk_bytenr); | |
4611 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, | |
4612 | new_extents[0].disk_num_bytes); | |
1a40e23b ZY |
4613 | btrfs_mark_buffer_dirty(leaf); |
4614 | ||
4615 | btrfs_drop_extent_cache(inode, key.offset, | |
4616 | key.offset + num_bytes - 1, 0); | |
4617 | ||
4618 | ret = btrfs_inc_extent_ref(trans, root, | |
4619 | new_extents[0].disk_bytenr, | |
4620 | new_extents[0].disk_num_bytes, | |
4621 | leaf->start, | |
4622 | root->root_key.objectid, | |
4623 | trans->transid, | |
3bb1a1bc | 4624 | key.objectid); |
1a40e23b ZY |
4625 | BUG_ON(ret); |
4626 | ||
4627 | ret = btrfs_free_extent(trans, root, | |
4628 | extent_key->objectid, | |
4629 | extent_key->offset, | |
4630 | leaf->start, | |
4631 | btrfs_header_owner(leaf), | |
4632 | btrfs_header_generation(leaf), | |
3bb1a1bc | 4633 | key.objectid, 0); |
1a40e23b ZY |
4634 | BUG_ON(ret); |
4635 | ||
4636 | btrfs_release_path(root, path); | |
4637 | key.offset += num_bytes; | |
4638 | } else { | |
d899e052 YZ |
4639 | BUG_ON(1); |
4640 | #if 0 | |
1a40e23b ZY |
4641 | u64 alloc_hint; |
4642 | u64 extent_len; | |
4643 | int i; | |
4644 | /* | |
4645 | * drop old extent pointer at first, then insert the | |
4646 | * new pointers one bye one | |
4647 | */ | |
4648 | btrfs_release_path(root, path); | |
4649 | ret = btrfs_drop_extents(trans, root, inode, key.offset, | |
4650 | key.offset + num_bytes, | |
4651 | key.offset, &alloc_hint); | |
4652 | BUG_ON(ret); | |
4653 | ||
4654 | for (i = 0; i < nr_extents; i++) { | |
4655 | if (ext_offset >= new_extents[i].num_bytes) { | |
4656 | ext_offset -= new_extents[i].num_bytes; | |
4657 | continue; | |
4658 | } | |
4659 | extent_len = min(new_extents[i].num_bytes - | |
4660 | ext_offset, num_bytes); | |
4661 | ||
4662 | ret = btrfs_insert_empty_item(trans, root, | |
4663 | path, &key, | |
4664 | sizeof(*fi)); | |
4665 | BUG_ON(ret); | |
4666 | ||
4667 | leaf = path->nodes[0]; | |
4668 | fi = btrfs_item_ptr(leaf, path->slots[0], | |
4669 | struct btrfs_file_extent_item); | |
4670 | btrfs_set_file_extent_generation(leaf, fi, | |
4671 | trans->transid); | |
4672 | btrfs_set_file_extent_type(leaf, fi, | |
4673 | BTRFS_FILE_EXTENT_REG); | |
4674 | btrfs_set_file_extent_disk_bytenr(leaf, fi, | |
4675 | new_extents[i].disk_bytenr); | |
4676 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, | |
4677 | new_extents[i].disk_num_bytes); | |
c8b97818 CM |
4678 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
4679 | new_extents[i].ram_bytes); | |
4680 | ||
4681 | btrfs_set_file_extent_compression(leaf, fi, | |
4682 | new_extents[i].compression); | |
4683 | btrfs_set_file_extent_encryption(leaf, fi, | |
4684 | new_extents[i].encryption); | |
4685 | btrfs_set_file_extent_other_encoding(leaf, fi, | |
4686 | new_extents[i].other_encoding); | |
4687 | ||
1a40e23b ZY |
4688 | btrfs_set_file_extent_num_bytes(leaf, fi, |
4689 | extent_len); | |
4690 | ext_offset += new_extents[i].offset; | |
4691 | btrfs_set_file_extent_offset(leaf, fi, | |
4692 | ext_offset); | |
4693 | btrfs_mark_buffer_dirty(leaf); | |
4694 | ||
4695 | btrfs_drop_extent_cache(inode, key.offset, | |
4696 | key.offset + extent_len - 1, 0); | |
4697 | ||
4698 | ret = btrfs_inc_extent_ref(trans, root, | |
4699 | new_extents[i].disk_bytenr, | |
4700 | new_extents[i].disk_num_bytes, | |
4701 | leaf->start, | |
4702 | root->root_key.objectid, | |
3bb1a1bc | 4703 | trans->transid, key.objectid); |
1a40e23b ZY |
4704 | BUG_ON(ret); |
4705 | btrfs_release_path(root, path); | |
4706 | ||
a76a3cd4 | 4707 | inode_add_bytes(inode, extent_len); |
1a40e23b ZY |
4708 | |
4709 | ext_offset = 0; | |
4710 | num_bytes -= extent_len; | |
4711 | key.offset += extent_len; | |
4712 | ||
4713 | if (num_bytes == 0) | |
4714 | break; | |
4715 | } | |
4716 | BUG_ON(i >= nr_extents); | |
d899e052 | 4717 | #endif |
1a40e23b ZY |
4718 | } |
4719 | ||
4720 | if (extent_locked) { | |
1a40e23b ZY |
4721 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
4722 | lock_end, GFP_NOFS); | |
4723 | extent_locked = 0; | |
4724 | } | |
4725 | skip: | |
4726 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && | |
4727 | key.offset >= first_pos + extent_key->offset) | |
4728 | break; | |
4729 | ||
4730 | cond_resched(); | |
4731 | } | |
4732 | ret = 0; | |
4733 | out: | |
4734 | btrfs_release_path(root, path); | |
4735 | if (inode) { | |
4736 | mutex_unlock(&inode->i_mutex); | |
4737 | if (extent_locked) { | |
1a40e23b ZY |
4738 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
4739 | lock_end, GFP_NOFS); | |
4740 | } | |
4741 | iput(inode); | |
4742 | } | |
4743 | return ret; | |
4744 | } | |
4745 | ||
1a40e23b ZY |
4746 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
4747 | struct btrfs_root *root, | |
4748 | struct extent_buffer *buf, u64 orig_start) | |
4749 | { | |
4750 | int level; | |
4751 | int ret; | |
4752 | ||
4753 | BUG_ON(btrfs_header_generation(buf) != trans->transid); | |
4754 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); | |
4755 | ||
4756 | level = btrfs_header_level(buf); | |
4757 | if (level == 0) { | |
4758 | struct btrfs_leaf_ref *ref; | |
4759 | struct btrfs_leaf_ref *orig_ref; | |
4760 | ||
4761 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); | |
4762 | if (!orig_ref) | |
4763 | return -ENOENT; | |
4764 | ||
4765 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); | |
4766 | if (!ref) { | |
4767 | btrfs_free_leaf_ref(root, orig_ref); | |
4768 | return -ENOMEM; | |
4769 | } | |
4770 | ||
4771 | ref->nritems = orig_ref->nritems; | |
4772 | memcpy(ref->extents, orig_ref->extents, | |
4773 | sizeof(ref->extents[0]) * ref->nritems); | |
4774 | ||
4775 | btrfs_free_leaf_ref(root, orig_ref); | |
4776 | ||
4777 | ref->root_gen = trans->transid; | |
4778 | ref->bytenr = buf->start; | |
4779 | ref->owner = btrfs_header_owner(buf); | |
4780 | ref->generation = btrfs_header_generation(buf); | |
4781 | ret = btrfs_add_leaf_ref(root, ref, 0); | |
4782 | WARN_ON(ret); | |
4783 | btrfs_free_leaf_ref(root, ref); | |
4784 | } | |
4785 | return 0; | |
4786 | } | |
4787 | ||
d397712b | 4788 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
1a40e23b ZY |
4789 | struct extent_buffer *leaf, |
4790 | struct btrfs_block_group_cache *group, | |
4791 | struct btrfs_root *target_root) | |
4792 | { | |
4793 | struct btrfs_key key; | |
4794 | struct inode *inode = NULL; | |
4795 | struct btrfs_file_extent_item *fi; | |
4796 | u64 num_bytes; | |
4797 | u64 skip_objectid = 0; | |
4798 | u32 nritems; | |
4799 | u32 i; | |
4800 | ||
4801 | nritems = btrfs_header_nritems(leaf); | |
4802 | for (i = 0; i < nritems; i++) { | |
4803 | btrfs_item_key_to_cpu(leaf, &key, i); | |
4804 | if (key.objectid == skip_objectid || | |
4805 | key.type != BTRFS_EXTENT_DATA_KEY) | |
4806 | continue; | |
4807 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
4808 | if (btrfs_file_extent_type(leaf, fi) == | |
4809 | BTRFS_FILE_EXTENT_INLINE) | |
4810 | continue; | |
4811 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) | |
4812 | continue; | |
4813 | if (!inode || inode->i_ino != key.objectid) { | |
4814 | iput(inode); | |
4815 | inode = btrfs_ilookup(target_root->fs_info->sb, | |
4816 | key.objectid, target_root, 1); | |
4817 | } | |
4818 | if (!inode) { | |
4819 | skip_objectid = key.objectid; | |
4820 | continue; | |
4821 | } | |
4822 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); | |
4823 | ||
4824 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, | |
4825 | key.offset + num_bytes - 1, GFP_NOFS); | |
1a40e23b ZY |
4826 | btrfs_drop_extent_cache(inode, key.offset, |
4827 | key.offset + num_bytes - 1, 1); | |
1a40e23b ZY |
4828 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
4829 | key.offset + num_bytes - 1, GFP_NOFS); | |
4830 | cond_resched(); | |
4831 | } | |
4832 | iput(inode); | |
4833 | return 0; | |
4834 | } | |
4835 | ||
d397712b | 4836 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
1a40e23b ZY |
4837 | struct btrfs_root *root, |
4838 | struct extent_buffer *leaf, | |
4839 | struct btrfs_block_group_cache *group, | |
4840 | struct inode *reloc_inode) | |
4841 | { | |
4842 | struct btrfs_key key; | |
4843 | struct btrfs_key extent_key; | |
4844 | struct btrfs_file_extent_item *fi; | |
4845 | struct btrfs_leaf_ref *ref; | |
4846 | struct disk_extent *new_extent; | |
4847 | u64 bytenr; | |
4848 | u64 num_bytes; | |
4849 | u32 nritems; | |
4850 | u32 i; | |
4851 | int ext_index; | |
4852 | int nr_extent; | |
4853 | int ret; | |
4854 | ||
4855 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); | |
4856 | BUG_ON(!new_extent); | |
4857 | ||
4858 | ref = btrfs_lookup_leaf_ref(root, leaf->start); | |
4859 | BUG_ON(!ref); | |
4860 | ||
4861 | ext_index = -1; | |
4862 | nritems = btrfs_header_nritems(leaf); | |
4863 | for (i = 0; i < nritems; i++) { | |
4864 | btrfs_item_key_to_cpu(leaf, &key, i); | |
4865 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
4866 | continue; | |
4867 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
4868 | if (btrfs_file_extent_type(leaf, fi) == | |
4869 | BTRFS_FILE_EXTENT_INLINE) | |
4870 | continue; | |
4871 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); | |
4872 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); | |
4873 | if (bytenr == 0) | |
4874 | continue; | |
4875 | ||
4876 | ext_index++; | |
4877 | if (bytenr >= group->key.objectid + group->key.offset || | |
4878 | bytenr + num_bytes <= group->key.objectid) | |
4879 | continue; | |
4880 | ||
4881 | extent_key.objectid = bytenr; | |
4882 | extent_key.offset = num_bytes; | |
4883 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; | |
4884 | nr_extent = 1; | |
4885 | ret = get_new_locations(reloc_inode, &extent_key, | |
4886 | group->key.objectid, 1, | |
4887 | &new_extent, &nr_extent); | |
4888 | if (ret > 0) | |
4889 | continue; | |
4890 | BUG_ON(ret < 0); | |
4891 | ||
4892 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); | |
4893 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); | |
4894 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; | |
4895 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; | |
4896 | ||
1a40e23b ZY |
4897 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
4898 | new_extent->disk_bytenr); | |
4899 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, | |
4900 | new_extent->disk_num_bytes); | |
1a40e23b ZY |
4901 | btrfs_mark_buffer_dirty(leaf); |
4902 | ||
4903 | ret = btrfs_inc_extent_ref(trans, root, | |
4904 | new_extent->disk_bytenr, | |
4905 | new_extent->disk_num_bytes, | |
4906 | leaf->start, | |
4907 | root->root_key.objectid, | |
3bb1a1bc | 4908 | trans->transid, key.objectid); |
1a40e23b ZY |
4909 | BUG_ON(ret); |
4910 | ret = btrfs_free_extent(trans, root, | |
4911 | bytenr, num_bytes, leaf->start, | |
4912 | btrfs_header_owner(leaf), | |
4913 | btrfs_header_generation(leaf), | |
3bb1a1bc | 4914 | key.objectid, 0); |
1a40e23b ZY |
4915 | BUG_ON(ret); |
4916 | cond_resched(); | |
4917 | } | |
4918 | kfree(new_extent); | |
4919 | BUG_ON(ext_index + 1 != ref->nritems); | |
4920 | btrfs_free_leaf_ref(root, ref); | |
4921 | return 0; | |
4922 | } | |
4923 | ||
f82d02d9 YZ |
4924 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
4925 | struct btrfs_root *root) | |
1a40e23b ZY |
4926 | { |
4927 | struct btrfs_root *reloc_root; | |
f82d02d9 | 4928 | int ret; |
1a40e23b ZY |
4929 | |
4930 | if (root->reloc_root) { | |
4931 | reloc_root = root->reloc_root; | |
4932 | root->reloc_root = NULL; | |
4933 | list_add(&reloc_root->dead_list, | |
4934 | &root->fs_info->dead_reloc_roots); | |
f82d02d9 YZ |
4935 | |
4936 | btrfs_set_root_bytenr(&reloc_root->root_item, | |
4937 | reloc_root->node->start); | |
4938 | btrfs_set_root_level(&root->root_item, | |
4939 | btrfs_header_level(reloc_root->node)); | |
4940 | memset(&reloc_root->root_item.drop_progress, 0, | |
4941 | sizeof(struct btrfs_disk_key)); | |
4942 | reloc_root->root_item.drop_level = 0; | |
4943 | ||
4944 | ret = btrfs_update_root(trans, root->fs_info->tree_root, | |
4945 | &reloc_root->root_key, | |
4946 | &reloc_root->root_item); | |
4947 | BUG_ON(ret); | |
1a40e23b ZY |
4948 | } |
4949 | return 0; | |
4950 | } | |
4951 | ||
4952 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) | |
4953 | { | |
4954 | struct btrfs_trans_handle *trans; | |
4955 | struct btrfs_root *reloc_root; | |
4956 | struct btrfs_root *prev_root = NULL; | |
4957 | struct list_head dead_roots; | |
4958 | int ret; | |
4959 | unsigned long nr; | |
4960 | ||
4961 | INIT_LIST_HEAD(&dead_roots); | |
4962 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); | |
4963 | ||
4964 | while (!list_empty(&dead_roots)) { | |
4965 | reloc_root = list_entry(dead_roots.prev, | |
4966 | struct btrfs_root, dead_list); | |
4967 | list_del_init(&reloc_root->dead_list); | |
4968 | ||
4969 | BUG_ON(reloc_root->commit_root != NULL); | |
4970 | while (1) { | |
4971 | trans = btrfs_join_transaction(root, 1); | |
4972 | BUG_ON(!trans); | |
4973 | ||
4974 | mutex_lock(&root->fs_info->drop_mutex); | |
4975 | ret = btrfs_drop_snapshot(trans, reloc_root); | |
4976 | if (ret != -EAGAIN) | |
4977 | break; | |
4978 | mutex_unlock(&root->fs_info->drop_mutex); | |
4979 | ||
4980 | nr = trans->blocks_used; | |
4981 | ret = btrfs_end_transaction(trans, root); | |
4982 | BUG_ON(ret); | |
4983 | btrfs_btree_balance_dirty(root, nr); | |
4984 | } | |
4985 | ||
4986 | free_extent_buffer(reloc_root->node); | |
4987 | ||
4988 | ret = btrfs_del_root(trans, root->fs_info->tree_root, | |
4989 | &reloc_root->root_key); | |
4990 | BUG_ON(ret); | |
4991 | mutex_unlock(&root->fs_info->drop_mutex); | |
4992 | ||
4993 | nr = trans->blocks_used; | |
4994 | ret = btrfs_end_transaction(trans, root); | |
4995 | BUG_ON(ret); | |
4996 | btrfs_btree_balance_dirty(root, nr); | |
4997 | ||
4998 | kfree(prev_root); | |
4999 | prev_root = reloc_root; | |
5000 | } | |
5001 | if (prev_root) { | |
5002 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); | |
5003 | kfree(prev_root); | |
5004 | } | |
5005 | return 0; | |
5006 | } | |
5007 | ||
5008 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) | |
5009 | { | |
5010 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); | |
5011 | return 0; | |
5012 | } | |
5013 | ||
5014 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) | |
5015 | { | |
5016 | struct btrfs_root *reloc_root; | |
5017 | struct btrfs_trans_handle *trans; | |
5018 | struct btrfs_key location; | |
5019 | int found; | |
5020 | int ret; | |
5021 | ||
5022 | mutex_lock(&root->fs_info->tree_reloc_mutex); | |
5023 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); | |
5024 | BUG_ON(ret); | |
5025 | found = !list_empty(&root->fs_info->dead_reloc_roots); | |
5026 | mutex_unlock(&root->fs_info->tree_reloc_mutex); | |
5027 | ||
5028 | if (found) { | |
5029 | trans = btrfs_start_transaction(root, 1); | |
5030 | BUG_ON(!trans); | |
5031 | ret = btrfs_commit_transaction(trans, root); | |
5032 | BUG_ON(ret); | |
5033 | } | |
5034 | ||
5035 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; | |
5036 | location.offset = (u64)-1; | |
5037 | location.type = BTRFS_ROOT_ITEM_KEY; | |
5038 | ||
5039 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); | |
5040 | BUG_ON(!reloc_root); | |
5041 | btrfs_orphan_cleanup(reloc_root); | |
5042 | return 0; | |
5043 | } | |
5044 | ||
d397712b | 5045 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
1a40e23b ZY |
5046 | struct btrfs_root *root) |
5047 | { | |
5048 | struct btrfs_root *reloc_root; | |
5049 | struct extent_buffer *eb; | |
5050 | struct btrfs_root_item *root_item; | |
5051 | struct btrfs_key root_key; | |
5052 | int ret; | |
5053 | ||
5054 | BUG_ON(!root->ref_cows); | |
5055 | if (root->reloc_root) | |
5056 | return 0; | |
5057 | ||
5058 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); | |
5059 | BUG_ON(!root_item); | |
5060 | ||
5061 | ret = btrfs_copy_root(trans, root, root->commit_root, | |
5062 | &eb, BTRFS_TREE_RELOC_OBJECTID); | |
5063 | BUG_ON(ret); | |
5064 | ||
5065 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; | |
5066 | root_key.offset = root->root_key.objectid; | |
5067 | root_key.type = BTRFS_ROOT_ITEM_KEY; | |
5068 | ||
5069 | memcpy(root_item, &root->root_item, sizeof(root_item)); | |
5070 | btrfs_set_root_refs(root_item, 0); | |
5071 | btrfs_set_root_bytenr(root_item, eb->start); | |
5072 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); | |
84234f3a | 5073 | btrfs_set_root_generation(root_item, trans->transid); |
1a40e23b ZY |
5074 | |
5075 | btrfs_tree_unlock(eb); | |
5076 | free_extent_buffer(eb); | |
5077 | ||
5078 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, | |
5079 | &root_key, root_item); | |
5080 | BUG_ON(ret); | |
5081 | kfree(root_item); | |
5082 | ||
5083 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, | |
5084 | &root_key); | |
5085 | BUG_ON(!reloc_root); | |
5086 | reloc_root->last_trans = trans->transid; | |
5087 | reloc_root->commit_root = NULL; | |
5088 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; | |
5089 | ||
5090 | root->reloc_root = reloc_root; | |
5091 | return 0; | |
5092 | } | |
5093 | ||
5094 | /* | |
5095 | * Core function of space balance. | |
5096 | * | |
5097 | * The idea is using reloc trees to relocate tree blocks in reference | |
f82d02d9 YZ |
5098 | * counted roots. There is one reloc tree for each subvol, and all |
5099 | * reloc trees share same root key objectid. Reloc trees are snapshots | |
5100 | * of the latest committed roots of subvols (root->commit_root). | |
5101 | * | |
5102 | * To relocate a tree block referenced by a subvol, there are two steps. | |
5103 | * COW the block through subvol's reloc tree, then update block pointer | |
5104 | * in the subvol to point to the new block. Since all reloc trees share | |
5105 | * same root key objectid, doing special handing for tree blocks owned | |
5106 | * by them is easy. Once a tree block has been COWed in one reloc tree, | |
5107 | * we can use the resulting new block directly when the same block is | |
5108 | * required to COW again through other reloc trees. By this way, relocated | |
5109 | * tree blocks are shared between reloc trees, so they are also shared | |
5110 | * between subvols. | |
1a40e23b | 5111 | */ |
d397712b | 5112 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
1a40e23b ZY |
5113 | struct btrfs_root *root, |
5114 | struct btrfs_path *path, | |
5115 | struct btrfs_key *first_key, | |
5116 | struct btrfs_ref_path *ref_path, | |
5117 | struct btrfs_block_group_cache *group, | |
5118 | struct inode *reloc_inode) | |
5119 | { | |
5120 | struct btrfs_root *reloc_root; | |
5121 | struct extent_buffer *eb = NULL; | |
5122 | struct btrfs_key *keys; | |
5123 | u64 *nodes; | |
5124 | int level; | |
f82d02d9 | 5125 | int shared_level; |
1a40e23b | 5126 | int lowest_level = 0; |
1a40e23b ZY |
5127 | int ret; |
5128 | ||
5129 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | |
5130 | lowest_level = ref_path->owner_objectid; | |
5131 | ||
f82d02d9 | 5132 | if (!root->ref_cows) { |
1a40e23b ZY |
5133 | path->lowest_level = lowest_level; |
5134 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); | |
5135 | BUG_ON(ret < 0); | |
5136 | path->lowest_level = 0; | |
5137 | btrfs_release_path(root, path); | |
5138 | return 0; | |
5139 | } | |
5140 | ||
1a40e23b ZY |
5141 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
5142 | ret = init_reloc_tree(trans, root); | |
5143 | BUG_ON(ret); | |
5144 | reloc_root = root->reloc_root; | |
5145 | ||
f82d02d9 YZ |
5146 | shared_level = ref_path->shared_level; |
5147 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; | |
1a40e23b | 5148 | |
f82d02d9 YZ |
5149 | keys = ref_path->node_keys; |
5150 | nodes = ref_path->new_nodes; | |
5151 | memset(&keys[shared_level + 1], 0, | |
5152 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); | |
5153 | memset(&nodes[shared_level + 1], 0, | |
5154 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); | |
1a40e23b | 5155 | |
f82d02d9 YZ |
5156 | if (nodes[lowest_level] == 0) { |
5157 | path->lowest_level = lowest_level; | |
5158 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, | |
5159 | 0, 1); | |
5160 | BUG_ON(ret); | |
5161 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { | |
5162 | eb = path->nodes[level]; | |
5163 | if (!eb || eb == reloc_root->node) | |
5164 | break; | |
5165 | nodes[level] = eb->start; | |
5166 | if (level == 0) | |
5167 | btrfs_item_key_to_cpu(eb, &keys[level], 0); | |
5168 | else | |
5169 | btrfs_node_key_to_cpu(eb, &keys[level], 0); | |
5170 | } | |
2b82032c YZ |
5171 | if (nodes[0] && |
5172 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | |
f82d02d9 YZ |
5173 | eb = path->nodes[0]; |
5174 | ret = replace_extents_in_leaf(trans, reloc_root, eb, | |
5175 | group, reloc_inode); | |
5176 | BUG_ON(ret); | |
5177 | } | |
5178 | btrfs_release_path(reloc_root, path); | |
5179 | } else { | |
1a40e23b | 5180 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
f82d02d9 | 5181 | lowest_level); |
1a40e23b ZY |
5182 | BUG_ON(ret); |
5183 | } | |
5184 | ||
1a40e23b ZY |
5185 | /* |
5186 | * replace tree blocks in the fs tree with tree blocks in | |
5187 | * the reloc tree. | |
5188 | */ | |
5189 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); | |
5190 | BUG_ON(ret < 0); | |
5191 | ||
5192 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | |
f82d02d9 YZ |
5193 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
5194 | 0, 0); | |
5195 | BUG_ON(ret); | |
5196 | extent_buffer_get(path->nodes[0]); | |
5197 | eb = path->nodes[0]; | |
5198 | btrfs_release_path(reloc_root, path); | |
1a40e23b ZY |
5199 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
5200 | BUG_ON(ret); | |
5201 | free_extent_buffer(eb); | |
5202 | } | |
1a40e23b | 5203 | |
f82d02d9 | 5204 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
1a40e23b | 5205 | path->lowest_level = 0; |
1a40e23b ZY |
5206 | return 0; |
5207 | } | |
5208 | ||
d397712b | 5209 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
1a40e23b ZY |
5210 | struct btrfs_root *root, |
5211 | struct btrfs_path *path, | |
5212 | struct btrfs_key *first_key, | |
5213 | struct btrfs_ref_path *ref_path) | |
5214 | { | |
5215 | int ret; | |
1a40e23b ZY |
5216 | |
5217 | ret = relocate_one_path(trans, root, path, first_key, | |
5218 | ref_path, NULL, NULL); | |
5219 | BUG_ON(ret); | |
5220 | ||
5221 | if (root == root->fs_info->extent_root) | |
5222 | btrfs_extent_post_op(trans, root); | |
1a40e23b ZY |
5223 | |
5224 | return 0; | |
5225 | } | |
5226 | ||
d397712b | 5227 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
1a40e23b ZY |
5228 | struct btrfs_root *extent_root, |
5229 | struct btrfs_path *path, | |
5230 | struct btrfs_key *extent_key) | |
5231 | { | |
5232 | int ret; | |
5233 | ||
1a40e23b ZY |
5234 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
5235 | if (ret) | |
5236 | goto out; | |
5237 | ret = btrfs_del_item(trans, extent_root, path); | |
5238 | out: | |
5239 | btrfs_release_path(extent_root, path); | |
1a40e23b ZY |
5240 | return ret; |
5241 | } | |
5242 | ||
d397712b | 5243 | static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info, |
1a40e23b ZY |
5244 | struct btrfs_ref_path *ref_path) |
5245 | { | |
5246 | struct btrfs_key root_key; | |
5247 | ||
5248 | root_key.objectid = ref_path->root_objectid; | |
5249 | root_key.type = BTRFS_ROOT_ITEM_KEY; | |
5250 | if (is_cowonly_root(ref_path->root_objectid)) | |
5251 | root_key.offset = 0; | |
5252 | else | |
5253 | root_key.offset = (u64)-1; | |
5254 | ||
5255 | return btrfs_read_fs_root_no_name(fs_info, &root_key); | |
5256 | } | |
5257 | ||
d397712b | 5258 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
1a40e23b ZY |
5259 | struct btrfs_path *path, |
5260 | struct btrfs_key *extent_key, | |
5261 | struct btrfs_block_group_cache *group, | |
5262 | struct inode *reloc_inode, int pass) | |
5263 | { | |
5264 | struct btrfs_trans_handle *trans; | |
5265 | struct btrfs_root *found_root; | |
5266 | struct btrfs_ref_path *ref_path = NULL; | |
5267 | struct disk_extent *new_extents = NULL; | |
5268 | int nr_extents = 0; | |
5269 | int loops; | |
5270 | int ret; | |
5271 | int level; | |
5272 | struct btrfs_key first_key; | |
5273 | u64 prev_block = 0; | |
5274 | ||
1a40e23b ZY |
5275 | |
5276 | trans = btrfs_start_transaction(extent_root, 1); | |
5277 | BUG_ON(!trans); | |
5278 | ||
5279 | if (extent_key->objectid == 0) { | |
5280 | ret = del_extent_zero(trans, extent_root, path, extent_key); | |
5281 | goto out; | |
5282 | } | |
5283 | ||
5284 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); | |
5285 | if (!ref_path) { | |
d397712b CM |
5286 | ret = -ENOMEM; |
5287 | goto out; | |
1a40e23b ZY |
5288 | } |
5289 | ||
5290 | for (loops = 0; ; loops++) { | |
5291 | if (loops == 0) { | |
5292 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, | |
5293 | extent_key->objectid); | |
5294 | } else { | |
5295 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); | |
5296 | } | |
5297 | if (ret < 0) | |
5298 | goto out; | |
5299 | if (ret > 0) | |
5300 | break; | |
5301 | ||
5302 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || | |
5303 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) | |
5304 | continue; | |
5305 | ||
5306 | found_root = read_ref_root(extent_root->fs_info, ref_path); | |
5307 | BUG_ON(!found_root); | |
5308 | /* | |
5309 | * for reference counted tree, only process reference paths | |
5310 | * rooted at the latest committed root. | |
5311 | */ | |
5312 | if (found_root->ref_cows && | |
5313 | ref_path->root_generation != found_root->root_key.offset) | |
5314 | continue; | |
5315 | ||
5316 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | |
5317 | if (pass == 0) { | |
5318 | /* | |
5319 | * copy data extents to new locations | |
5320 | */ | |
5321 | u64 group_start = group->key.objectid; | |
5322 | ret = relocate_data_extent(reloc_inode, | |
5323 | extent_key, | |
5324 | group_start); | |
5325 | if (ret < 0) | |
5326 | goto out; | |
5327 | break; | |
5328 | } | |
5329 | level = 0; | |
5330 | } else { | |
5331 | level = ref_path->owner_objectid; | |
5332 | } | |
5333 | ||
5334 | if (prev_block != ref_path->nodes[level]) { | |
5335 | struct extent_buffer *eb; | |
5336 | u64 block_start = ref_path->nodes[level]; | |
5337 | u64 block_size = btrfs_level_size(found_root, level); | |
5338 | ||
5339 | eb = read_tree_block(found_root, block_start, | |
5340 | block_size, 0); | |
5341 | btrfs_tree_lock(eb); | |
5342 | BUG_ON(level != btrfs_header_level(eb)); | |
5343 | ||
5344 | if (level == 0) | |
5345 | btrfs_item_key_to_cpu(eb, &first_key, 0); | |
5346 | else | |
5347 | btrfs_node_key_to_cpu(eb, &first_key, 0); | |
5348 | ||
5349 | btrfs_tree_unlock(eb); | |
5350 | free_extent_buffer(eb); | |
5351 | prev_block = block_start; | |
5352 | } | |
5353 | ||
e4404d6e YZ |
5354 | btrfs_record_root_in_trans(found_root); |
5355 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | |
5356 | /* | |
5357 | * try to update data extent references while | |
5358 | * keeping metadata shared between snapshots. | |
5359 | */ | |
5360 | if (pass == 1) { | |
5361 | ret = relocate_one_path(trans, found_root, | |
5362 | path, &first_key, ref_path, | |
5363 | group, reloc_inode); | |
5364 | if (ret < 0) | |
5365 | goto out; | |
5366 | continue; | |
5367 | } | |
1a40e23b ZY |
5368 | /* |
5369 | * use fallback method to process the remaining | |
5370 | * references. | |
5371 | */ | |
5372 | if (!new_extents) { | |
5373 | u64 group_start = group->key.objectid; | |
d899e052 YZ |
5374 | new_extents = kmalloc(sizeof(*new_extents), |
5375 | GFP_NOFS); | |
5376 | nr_extents = 1; | |
1a40e23b ZY |
5377 | ret = get_new_locations(reloc_inode, |
5378 | extent_key, | |
d899e052 | 5379 | group_start, 1, |
1a40e23b ZY |
5380 | &new_extents, |
5381 | &nr_extents); | |
d899e052 | 5382 | if (ret) |
1a40e23b ZY |
5383 | goto out; |
5384 | } | |
1a40e23b ZY |
5385 | ret = replace_one_extent(trans, found_root, |
5386 | path, extent_key, | |
5387 | &first_key, ref_path, | |
5388 | new_extents, nr_extents); | |
e4404d6e | 5389 | } else { |
1a40e23b ZY |
5390 | ret = relocate_tree_block(trans, found_root, path, |
5391 | &first_key, ref_path); | |
1a40e23b ZY |
5392 | } |
5393 | if (ret < 0) | |
5394 | goto out; | |
5395 | } | |
5396 | ret = 0; | |
5397 | out: | |
5398 | btrfs_end_transaction(trans, extent_root); | |
5399 | kfree(new_extents); | |
5400 | kfree(ref_path); | |
1a40e23b ZY |
5401 | return ret; |
5402 | } | |
5403 | ||
ec44a35c CM |
5404 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
5405 | { | |
5406 | u64 num_devices; | |
5407 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | | |
5408 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; | |
5409 | ||
2b82032c | 5410 | num_devices = root->fs_info->fs_devices->rw_devices; |
ec44a35c CM |
5411 | if (num_devices == 1) { |
5412 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
5413 | stripped = flags & ~stripped; | |
5414 | ||
5415 | /* turn raid0 into single device chunks */ | |
5416 | if (flags & BTRFS_BLOCK_GROUP_RAID0) | |
5417 | return stripped; | |
5418 | ||
5419 | /* turn mirroring into duplication */ | |
5420 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
5421 | BTRFS_BLOCK_GROUP_RAID10)) | |
5422 | return stripped | BTRFS_BLOCK_GROUP_DUP; | |
5423 | return flags; | |
5424 | } else { | |
5425 | /* they already had raid on here, just return */ | |
ec44a35c CM |
5426 | if (flags & stripped) |
5427 | return flags; | |
5428 | ||
5429 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
5430 | stripped = flags & ~stripped; | |
5431 | ||
5432 | /* switch duplicated blocks with raid1 */ | |
5433 | if (flags & BTRFS_BLOCK_GROUP_DUP) | |
5434 | return stripped | BTRFS_BLOCK_GROUP_RAID1; | |
5435 | ||
5436 | /* turn single device chunks into raid0 */ | |
5437 | return stripped | BTRFS_BLOCK_GROUP_RAID0; | |
5438 | } | |
5439 | return flags; | |
5440 | } | |
5441 | ||
b2950863 | 5442 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
0ef3e66b CM |
5443 | struct btrfs_block_group_cache *shrink_block_group, |
5444 | int force) | |
5445 | { | |
5446 | struct btrfs_trans_handle *trans; | |
5447 | u64 new_alloc_flags; | |
5448 | u64 calc; | |
5449 | ||
c286ac48 | 5450 | spin_lock(&shrink_block_group->lock); |
0ef3e66b | 5451 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
c286ac48 | 5452 | spin_unlock(&shrink_block_group->lock); |
c286ac48 | 5453 | |
0ef3e66b | 5454 | trans = btrfs_start_transaction(root, 1); |
c286ac48 | 5455 | spin_lock(&shrink_block_group->lock); |
7d9eb12c | 5456 | |
0ef3e66b CM |
5457 | new_alloc_flags = update_block_group_flags(root, |
5458 | shrink_block_group->flags); | |
5459 | if (new_alloc_flags != shrink_block_group->flags) { | |
5460 | calc = | |
5461 | btrfs_block_group_used(&shrink_block_group->item); | |
5462 | } else { | |
5463 | calc = shrink_block_group->key.offset; | |
5464 | } | |
c286ac48 CM |
5465 | spin_unlock(&shrink_block_group->lock); |
5466 | ||
0ef3e66b CM |
5467 | do_chunk_alloc(trans, root->fs_info->extent_root, |
5468 | calc + 2 * 1024 * 1024, new_alloc_flags, force); | |
7d9eb12c | 5469 | |
0ef3e66b | 5470 | btrfs_end_transaction(trans, root); |
c286ac48 CM |
5471 | } else |
5472 | spin_unlock(&shrink_block_group->lock); | |
0ef3e66b CM |
5473 | return 0; |
5474 | } | |
5475 | ||
1a40e23b ZY |
5476 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
5477 | struct btrfs_root *root, | |
5478 | u64 objectid, u64 size) | |
5479 | { | |
5480 | struct btrfs_path *path; | |
5481 | struct btrfs_inode_item *item; | |
5482 | struct extent_buffer *leaf; | |
5483 | int ret; | |
5484 | ||
5485 | path = btrfs_alloc_path(); | |
5486 | if (!path) | |
5487 | return -ENOMEM; | |
5488 | ||
5489 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); | |
5490 | if (ret) | |
5491 | goto out; | |
5492 | ||
5493 | leaf = path->nodes[0]; | |
5494 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); | |
5495 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); | |
5496 | btrfs_set_inode_generation(leaf, item, 1); | |
5497 | btrfs_set_inode_size(leaf, item, size); | |
5498 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); | |
0403e47e | 5499 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS); |
1a40e23b ZY |
5500 | btrfs_mark_buffer_dirty(leaf); |
5501 | btrfs_release_path(root, path); | |
5502 | out: | |
5503 | btrfs_free_path(path); | |
5504 | return ret; | |
5505 | } | |
5506 | ||
d397712b | 5507 | static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, |
1a40e23b ZY |
5508 | struct btrfs_block_group_cache *group) |
5509 | { | |
5510 | struct inode *inode = NULL; | |
5511 | struct btrfs_trans_handle *trans; | |
5512 | struct btrfs_root *root; | |
5513 | struct btrfs_key root_key; | |
5514 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; | |
5515 | int err = 0; | |
5516 | ||
5517 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; | |
5518 | root_key.type = BTRFS_ROOT_ITEM_KEY; | |
5519 | root_key.offset = (u64)-1; | |
5520 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); | |
5521 | if (IS_ERR(root)) | |
5522 | return ERR_CAST(root); | |
5523 | ||
5524 | trans = btrfs_start_transaction(root, 1); | |
5525 | BUG_ON(!trans); | |
5526 | ||
5527 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); | |
5528 | if (err) | |
5529 | goto out; | |
5530 | ||
5531 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); | |
5532 | BUG_ON(err); | |
5533 | ||
5534 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, | |
c8b97818 CM |
5535 | group->key.offset, 0, group->key.offset, |
5536 | 0, 0, 0); | |
1a40e23b ZY |
5537 | BUG_ON(err); |
5538 | ||
5539 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); | |
5540 | if (inode->i_state & I_NEW) { | |
5541 | BTRFS_I(inode)->root = root; | |
5542 | BTRFS_I(inode)->location.objectid = objectid; | |
5543 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
5544 | BTRFS_I(inode)->location.offset = 0; | |
5545 | btrfs_read_locked_inode(inode); | |
5546 | unlock_new_inode(inode); | |
5547 | BUG_ON(is_bad_inode(inode)); | |
5548 | } else { | |
5549 | BUG_ON(1); | |
5550 | } | |
17d217fe | 5551 | BTRFS_I(inode)->index_cnt = group->key.objectid; |
1a40e23b ZY |
5552 | |
5553 | err = btrfs_orphan_add(trans, inode); | |
5554 | out: | |
5555 | btrfs_end_transaction(trans, root); | |
5556 | if (err) { | |
5557 | if (inode) | |
5558 | iput(inode); | |
5559 | inode = ERR_PTR(err); | |
5560 | } | |
5561 | return inode; | |
5562 | } | |
5563 | ||
17d217fe YZ |
5564 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) |
5565 | { | |
5566 | ||
5567 | struct btrfs_ordered_sum *sums; | |
5568 | struct btrfs_sector_sum *sector_sum; | |
5569 | struct btrfs_ordered_extent *ordered; | |
5570 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5571 | struct list_head list; | |
5572 | size_t offset; | |
5573 | int ret; | |
5574 | u64 disk_bytenr; | |
5575 | ||
5576 | INIT_LIST_HEAD(&list); | |
5577 | ||
5578 | ordered = btrfs_lookup_ordered_extent(inode, file_pos); | |
5579 | BUG_ON(ordered->file_offset != file_pos || ordered->len != len); | |
5580 | ||
5581 | disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; | |
5582 | ret = btrfs_lookup_csums_range(root, disk_bytenr, | |
5583 | disk_bytenr + len - 1, &list); | |
5584 | ||
5585 | while (!list_empty(&list)) { | |
5586 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); | |
5587 | list_del_init(&sums->list); | |
5588 | ||
5589 | sector_sum = sums->sums; | |
5590 | sums->bytenr = ordered->start; | |
5591 | ||
5592 | offset = 0; | |
5593 | while (offset < sums->len) { | |
5594 | sector_sum->bytenr += ordered->start - disk_bytenr; | |
5595 | sector_sum++; | |
5596 | offset += root->sectorsize; | |
5597 | } | |
5598 | ||
5599 | btrfs_add_ordered_sum(inode, ordered, sums); | |
5600 | } | |
5601 | btrfs_put_ordered_extent(ordered); | |
5602 | return 0; | |
5603 | } | |
5604 | ||
1a40e23b | 5605 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
edbd8d4e CM |
5606 | { |
5607 | struct btrfs_trans_handle *trans; | |
edbd8d4e | 5608 | struct btrfs_path *path; |
1a40e23b ZY |
5609 | struct btrfs_fs_info *info = root->fs_info; |
5610 | struct extent_buffer *leaf; | |
5611 | struct inode *reloc_inode; | |
5612 | struct btrfs_block_group_cache *block_group; | |
5613 | struct btrfs_key key; | |
d899e052 | 5614 | u64 skipped; |
edbd8d4e CM |
5615 | u64 cur_byte; |
5616 | u64 total_found; | |
edbd8d4e CM |
5617 | u32 nritems; |
5618 | int ret; | |
a061fc8d | 5619 | int progress; |
1a40e23b | 5620 | int pass = 0; |
edbd8d4e | 5621 | |
1a40e23b ZY |
5622 | root = root->fs_info->extent_root; |
5623 | ||
5624 | block_group = btrfs_lookup_block_group(info, group_start); | |
5625 | BUG_ON(!block_group); | |
8f18cf13 | 5626 | |
d397712b | 5627 | printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n", |
1a40e23b ZY |
5628 | (unsigned long long)block_group->key.objectid, |
5629 | (unsigned long long)block_group->flags); | |
8f18cf13 | 5630 | |
edbd8d4e | 5631 | path = btrfs_alloc_path(); |
1a40e23b | 5632 | BUG_ON(!path); |
edbd8d4e | 5633 | |
1a40e23b ZY |
5634 | reloc_inode = create_reloc_inode(info, block_group); |
5635 | BUG_ON(IS_ERR(reloc_inode)); | |
323da79c | 5636 | |
1a40e23b | 5637 | __alloc_chunk_for_shrink(root, block_group, 1); |
c146afad | 5638 | set_block_group_readonly(block_group); |
323da79c | 5639 | |
1a40e23b ZY |
5640 | btrfs_start_delalloc_inodes(info->tree_root); |
5641 | btrfs_wait_ordered_extents(info->tree_root, 0); | |
5642 | again: | |
d899e052 | 5643 | skipped = 0; |
edbd8d4e | 5644 | total_found = 0; |
a061fc8d | 5645 | progress = 0; |
1a40e23b | 5646 | key.objectid = block_group->key.objectid; |
edbd8d4e CM |
5647 | key.offset = 0; |
5648 | key.type = 0; | |
73e48b27 | 5649 | cur_byte = key.objectid; |
4313b399 | 5650 | |
1a40e23b ZY |
5651 | trans = btrfs_start_transaction(info->tree_root, 1); |
5652 | btrfs_commit_transaction(trans, info->tree_root); | |
ea8c2819 | 5653 | |
1a40e23b ZY |
5654 | mutex_lock(&root->fs_info->cleaner_mutex); |
5655 | btrfs_clean_old_snapshots(info->tree_root); | |
5656 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); | |
5657 | mutex_unlock(&root->fs_info->cleaner_mutex); | |
ea8c2819 | 5658 | |
d397712b | 5659 | while (1) { |
edbd8d4e CM |
5660 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
5661 | if (ret < 0) | |
5662 | goto out; | |
7d9eb12c | 5663 | next: |
edbd8d4e | 5664 | leaf = path->nodes[0]; |
73e48b27 | 5665 | nritems = btrfs_header_nritems(leaf); |
73e48b27 Y |
5666 | if (path->slots[0] >= nritems) { |
5667 | ret = btrfs_next_leaf(root, path); | |
5668 | if (ret < 0) | |
5669 | goto out; | |
5670 | if (ret == 1) { | |
5671 | ret = 0; | |
5672 | break; | |
edbd8d4e | 5673 | } |
73e48b27 Y |
5674 | leaf = path->nodes[0]; |
5675 | nritems = btrfs_header_nritems(leaf); | |
edbd8d4e | 5676 | } |
73e48b27 | 5677 | |
1a40e23b | 5678 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
725c8463 | 5679 | |
1a40e23b ZY |
5680 | if (key.objectid >= block_group->key.objectid + |
5681 | block_group->key.offset) | |
8f18cf13 CM |
5682 | break; |
5683 | ||
725c8463 | 5684 | if (progress && need_resched()) { |
725c8463 | 5685 | btrfs_release_path(root, path); |
1a40e23b | 5686 | cond_resched(); |
725c8463 | 5687 | progress = 0; |
1a40e23b | 5688 | continue; |
725c8463 CM |
5689 | } |
5690 | progress = 1; | |
5691 | ||
1a40e23b ZY |
5692 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
5693 | key.objectid + key.offset <= cur_byte) { | |
edbd8d4e | 5694 | path->slots[0]++; |
edbd8d4e CM |
5695 | goto next; |
5696 | } | |
73e48b27 | 5697 | |
edbd8d4e | 5698 | total_found++; |
1a40e23b | 5699 | cur_byte = key.objectid + key.offset; |
edbd8d4e | 5700 | btrfs_release_path(root, path); |
edbd8d4e | 5701 | |
1a40e23b ZY |
5702 | __alloc_chunk_for_shrink(root, block_group, 0); |
5703 | ret = relocate_one_extent(root, path, &key, block_group, | |
5704 | reloc_inode, pass); | |
5705 | BUG_ON(ret < 0); | |
d899e052 YZ |
5706 | if (ret > 0) |
5707 | skipped++; | |
edbd8d4e | 5708 | |
1a40e23b ZY |
5709 | key.objectid = cur_byte; |
5710 | key.type = 0; | |
5711 | key.offset = 0; | |
edbd8d4e CM |
5712 | } |
5713 | ||
1a40e23b | 5714 | btrfs_release_path(root, path); |
7d9eb12c | 5715 | |
1a40e23b ZY |
5716 | if (pass == 0) { |
5717 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); | |
5718 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); | |
8e8a1e31 | 5719 | } |
73e48b27 | 5720 | |
1a40e23b | 5721 | if (total_found > 0) { |
d397712b | 5722 | printk(KERN_INFO "btrfs found %llu extents in pass %d\n", |
1a40e23b ZY |
5723 | (unsigned long long)total_found, pass); |
5724 | pass++; | |
d899e052 YZ |
5725 | if (total_found == skipped && pass > 2) { |
5726 | iput(reloc_inode); | |
5727 | reloc_inode = create_reloc_inode(info, block_group); | |
5728 | pass = 0; | |
5729 | } | |
1a40e23b | 5730 | goto again; |
0f9dd46c | 5731 | } |
0ef3e66b | 5732 | |
1a40e23b ZY |
5733 | /* delete reloc_inode */ |
5734 | iput(reloc_inode); | |
5735 | ||
5736 | /* unpin extents in this range */ | |
5737 | trans = btrfs_start_transaction(info->tree_root, 1); | |
5738 | btrfs_commit_transaction(trans, info->tree_root); | |
0ef3e66b | 5739 | |
1a40e23b ZY |
5740 | spin_lock(&block_group->lock); |
5741 | WARN_ON(block_group->pinned > 0); | |
5742 | WARN_ON(block_group->reserved > 0); | |
5743 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); | |
5744 | spin_unlock(&block_group->lock); | |
d2fb3437 | 5745 | put_block_group(block_group); |
1a40e23b | 5746 | ret = 0; |
edbd8d4e | 5747 | out: |
1a40e23b | 5748 | btrfs_free_path(path); |
edbd8d4e CM |
5749 | return ret; |
5750 | } | |
5751 | ||
b2950863 CH |
5752 | static int find_first_block_group(struct btrfs_root *root, |
5753 | struct btrfs_path *path, struct btrfs_key *key) | |
0b86a832 | 5754 | { |
925baedd | 5755 | int ret = 0; |
0b86a832 CM |
5756 | struct btrfs_key found_key; |
5757 | struct extent_buffer *leaf; | |
5758 | int slot; | |
edbd8d4e | 5759 | |
0b86a832 CM |
5760 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
5761 | if (ret < 0) | |
925baedd CM |
5762 | goto out; |
5763 | ||
d397712b | 5764 | while (1) { |
0b86a832 | 5765 | slot = path->slots[0]; |
edbd8d4e | 5766 | leaf = path->nodes[0]; |
0b86a832 CM |
5767 | if (slot >= btrfs_header_nritems(leaf)) { |
5768 | ret = btrfs_next_leaf(root, path); | |
5769 | if (ret == 0) | |
5770 | continue; | |
5771 | if (ret < 0) | |
925baedd | 5772 | goto out; |
0b86a832 | 5773 | break; |
edbd8d4e | 5774 | } |
0b86a832 | 5775 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
edbd8d4e | 5776 | |
0b86a832 | 5777 | if (found_key.objectid >= key->objectid && |
925baedd CM |
5778 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
5779 | ret = 0; | |
5780 | goto out; | |
5781 | } | |
0b86a832 | 5782 | path->slots[0]++; |
edbd8d4e | 5783 | } |
0b86a832 | 5784 | ret = -ENOENT; |
925baedd | 5785 | out: |
0b86a832 | 5786 | return ret; |
edbd8d4e CM |
5787 | } |
5788 | ||
1a40e23b ZY |
5789 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
5790 | { | |
5791 | struct btrfs_block_group_cache *block_group; | |
5792 | struct rb_node *n; | |
5793 | ||
1a40e23b ZY |
5794 | spin_lock(&info->block_group_cache_lock); |
5795 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { | |
5796 | block_group = rb_entry(n, struct btrfs_block_group_cache, | |
5797 | cache_node); | |
1a40e23b ZY |
5798 | rb_erase(&block_group->cache_node, |
5799 | &info->block_group_cache_tree); | |
d899e052 YZ |
5800 | spin_unlock(&info->block_group_cache_lock); |
5801 | ||
5802 | btrfs_remove_free_space_cache(block_group); | |
80eb234a | 5803 | down_write(&block_group->space_info->groups_sem); |
1a40e23b | 5804 | list_del(&block_group->list); |
80eb234a | 5805 | up_write(&block_group->space_info->groups_sem); |
d2fb3437 YZ |
5806 | |
5807 | WARN_ON(atomic_read(&block_group->count) != 1); | |
1a40e23b | 5808 | kfree(block_group); |
d899e052 YZ |
5809 | |
5810 | spin_lock(&info->block_group_cache_lock); | |
1a40e23b ZY |
5811 | } |
5812 | spin_unlock(&info->block_group_cache_lock); | |
1a40e23b ZY |
5813 | return 0; |
5814 | } | |
5815 | ||
9078a3e1 CM |
5816 | int btrfs_read_block_groups(struct btrfs_root *root) |
5817 | { | |
5818 | struct btrfs_path *path; | |
5819 | int ret; | |
9078a3e1 | 5820 | struct btrfs_block_group_cache *cache; |
be744175 | 5821 | struct btrfs_fs_info *info = root->fs_info; |
6324fbf3 | 5822 | struct btrfs_space_info *space_info; |
9078a3e1 CM |
5823 | struct btrfs_key key; |
5824 | struct btrfs_key found_key; | |
5f39d397 | 5825 | struct extent_buffer *leaf; |
96b5179d | 5826 | |
be744175 | 5827 | root = info->extent_root; |
9078a3e1 | 5828 | key.objectid = 0; |
0b86a832 | 5829 | key.offset = 0; |
9078a3e1 | 5830 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
9078a3e1 CM |
5831 | path = btrfs_alloc_path(); |
5832 | if (!path) | |
5833 | return -ENOMEM; | |
5834 | ||
d397712b | 5835 | while (1) { |
0b86a832 CM |
5836 | ret = find_first_block_group(root, path, &key); |
5837 | if (ret > 0) { | |
5838 | ret = 0; | |
5839 | goto error; | |
9078a3e1 | 5840 | } |
0b86a832 CM |
5841 | if (ret != 0) |
5842 | goto error; | |
5843 | ||
5f39d397 CM |
5844 | leaf = path->nodes[0]; |
5845 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
8f18cf13 | 5846 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
9078a3e1 | 5847 | if (!cache) { |
0b86a832 | 5848 | ret = -ENOMEM; |
9078a3e1 CM |
5849 | break; |
5850 | } | |
3e1ad54f | 5851 | |
d2fb3437 | 5852 | atomic_set(&cache->count, 1); |
c286ac48 | 5853 | spin_lock_init(&cache->lock); |
25179201 | 5854 | mutex_init(&cache->alloc_mutex); |
ea6a478e | 5855 | mutex_init(&cache->cache_mutex); |
0f9dd46c | 5856 | INIT_LIST_HEAD(&cache->list); |
5f39d397 CM |
5857 | read_extent_buffer(leaf, &cache->item, |
5858 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
5859 | sizeof(cache->item)); | |
9078a3e1 | 5860 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
0b86a832 | 5861 | |
9078a3e1 CM |
5862 | key.objectid = found_key.objectid + found_key.offset; |
5863 | btrfs_release_path(root, path); | |
0b86a832 | 5864 | cache->flags = btrfs_block_group_flags(&cache->item); |
96b5179d | 5865 | |
6324fbf3 CM |
5866 | ret = update_space_info(info, cache->flags, found_key.offset, |
5867 | btrfs_block_group_used(&cache->item), | |
5868 | &space_info); | |
5869 | BUG_ON(ret); | |
5870 | cache->space_info = space_info; | |
80eb234a JB |
5871 | down_write(&space_info->groups_sem); |
5872 | list_add_tail(&cache->list, &space_info->block_groups); | |
5873 | up_write(&space_info->groups_sem); | |
0f9dd46c JB |
5874 | |
5875 | ret = btrfs_add_block_group_cache(root->fs_info, cache); | |
5876 | BUG_ON(ret); | |
75ccf47d CM |
5877 | |
5878 | set_avail_alloc_bits(root->fs_info, cache->flags); | |
2b82032c YZ |
5879 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
5880 | set_block_group_readonly(cache); | |
9078a3e1 | 5881 | } |
0b86a832 CM |
5882 | ret = 0; |
5883 | error: | |
9078a3e1 | 5884 | btrfs_free_path(path); |
0b86a832 | 5885 | return ret; |
9078a3e1 | 5886 | } |
6324fbf3 CM |
5887 | |
5888 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |
5889 | struct btrfs_root *root, u64 bytes_used, | |
e17cade2 | 5890 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
6324fbf3 CM |
5891 | u64 size) |
5892 | { | |
5893 | int ret; | |
6324fbf3 CM |
5894 | struct btrfs_root *extent_root; |
5895 | struct btrfs_block_group_cache *cache; | |
6324fbf3 CM |
5896 | |
5897 | extent_root = root->fs_info->extent_root; | |
6324fbf3 | 5898 | |
e02119d5 CM |
5899 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
5900 | ||
8f18cf13 | 5901 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
0f9dd46c JB |
5902 | if (!cache) |
5903 | return -ENOMEM; | |
5904 | ||
e17cade2 | 5905 | cache->key.objectid = chunk_offset; |
6324fbf3 | 5906 | cache->key.offset = size; |
d2fb3437 YZ |
5907 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
5908 | atomic_set(&cache->count, 1); | |
c286ac48 | 5909 | spin_lock_init(&cache->lock); |
25179201 | 5910 | mutex_init(&cache->alloc_mutex); |
ea6a478e | 5911 | mutex_init(&cache->cache_mutex); |
0f9dd46c | 5912 | INIT_LIST_HEAD(&cache->list); |
0ef3e66b | 5913 | |
6324fbf3 | 5914 | btrfs_set_block_group_used(&cache->item, bytes_used); |
6324fbf3 CM |
5915 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
5916 | cache->flags = type; | |
5917 | btrfs_set_block_group_flags(&cache->item, type); | |
5918 | ||
5919 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, | |
5920 | &cache->space_info); | |
5921 | BUG_ON(ret); | |
80eb234a JB |
5922 | down_write(&cache->space_info->groups_sem); |
5923 | list_add_tail(&cache->list, &cache->space_info->block_groups); | |
5924 | up_write(&cache->space_info->groups_sem); | |
6324fbf3 | 5925 | |
0f9dd46c JB |
5926 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
5927 | BUG_ON(ret); | |
c286ac48 | 5928 | |
6324fbf3 CM |
5929 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
5930 | sizeof(cache->item)); | |
5931 | BUG_ON(ret); | |
5932 | ||
87ef2bb4 CM |
5933 | finish_current_insert(trans, extent_root, 0); |
5934 | ret = del_pending_extents(trans, extent_root, 0); | |
6324fbf3 | 5935 | BUG_ON(ret); |
d18a2c44 | 5936 | set_avail_alloc_bits(extent_root->fs_info, type); |
925baedd | 5937 | |
6324fbf3 CM |
5938 | return 0; |
5939 | } | |
1a40e23b ZY |
5940 | |
5941 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, | |
5942 | struct btrfs_root *root, u64 group_start) | |
5943 | { | |
5944 | struct btrfs_path *path; | |
5945 | struct btrfs_block_group_cache *block_group; | |
5946 | struct btrfs_key key; | |
5947 | int ret; | |
5948 | ||
1a40e23b ZY |
5949 | root = root->fs_info->extent_root; |
5950 | ||
5951 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); | |
5952 | BUG_ON(!block_group); | |
c146afad | 5953 | BUG_ON(!block_group->ro); |
1a40e23b ZY |
5954 | |
5955 | memcpy(&key, &block_group->key, sizeof(key)); | |
5956 | ||
5957 | path = btrfs_alloc_path(); | |
5958 | BUG_ON(!path); | |
5959 | ||
5960 | btrfs_remove_free_space_cache(block_group); | |
5961 | rb_erase(&block_group->cache_node, | |
5962 | &root->fs_info->block_group_cache_tree); | |
80eb234a | 5963 | down_write(&block_group->space_info->groups_sem); |
1a40e23b | 5964 | list_del(&block_group->list); |
80eb234a | 5965 | up_write(&block_group->space_info->groups_sem); |
1a40e23b | 5966 | |
c146afad YZ |
5967 | spin_lock(&block_group->space_info->lock); |
5968 | block_group->space_info->total_bytes -= block_group->key.offset; | |
5969 | block_group->space_info->bytes_readonly -= block_group->key.offset; | |
5970 | spin_unlock(&block_group->space_info->lock); | |
2b82032c | 5971 | block_group->space_info->full = 0; |
c146afad | 5972 | |
d2fb3437 YZ |
5973 | put_block_group(block_group); |
5974 | put_block_group(block_group); | |
1a40e23b ZY |
5975 | |
5976 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
5977 | if (ret > 0) | |
5978 | ret = -EIO; | |
5979 | if (ret < 0) | |
5980 | goto out; | |
5981 | ||
5982 | ret = btrfs_del_item(trans, root, path); | |
5983 | out: | |
5984 | btrfs_free_path(path); | |
5985 | return ret; | |
5986 | } |