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> |
74493f7a | 22 | #include "hash.h" |
a5eb62e3 | 23 | #include "crc32c.h" |
fec577fb CM |
24 | #include "ctree.h" |
25 | #include "disk-io.h" | |
26 | #include "print-tree.h" | |
e089f05c | 27 | #include "transaction.h" |
0b86a832 | 28 | #include "volumes.h" |
925baedd | 29 | #include "locking.h" |
31153d81 | 30 | #include "ref-cache.h" |
fec577fb | 31 | |
31840ae1 ZY |
32 | #define PENDING_EXTENT_INSERT 0 |
33 | #define PENDING_EXTENT_DELETE 1 | |
34 | #define PENDING_BACKREF_UPDATE 2 | |
35 | ||
36 | struct pending_extent_op { | |
37 | int type; | |
38 | u64 bytenr; | |
39 | u64 num_bytes; | |
40 | u64 parent; | |
41 | u64 orig_parent; | |
42 | u64 generation; | |
43 | u64 orig_generation; | |
44 | int level; | |
45 | }; | |
46 | ||
e089f05c CM |
47 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
48 | btrfs_root *extent_root); | |
e20d96d6 CM |
49 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
50 | btrfs_root *extent_root); | |
925baedd CM |
51 | static struct btrfs_block_group_cache * |
52 | __btrfs_find_block_group(struct btrfs_root *root, | |
53 | struct btrfs_block_group_cache *hint, | |
54 | u64 search_start, int data, int owner); | |
d548ee51 | 55 | |
925baedd CM |
56 | void maybe_lock_mutex(struct btrfs_root *root) |
57 | { | |
58 | if (root != root->fs_info->extent_root && | |
59 | root != root->fs_info->chunk_root && | |
60 | root != root->fs_info->dev_root) { | |
61 | mutex_lock(&root->fs_info->alloc_mutex); | |
62 | } | |
63 | } | |
64 | ||
65 | void maybe_unlock_mutex(struct btrfs_root *root) | |
66 | { | |
67 | if (root != root->fs_info->extent_root && | |
68 | root != root->fs_info->chunk_root && | |
69 | root != root->fs_info->dev_root) { | |
70 | mutex_unlock(&root->fs_info->alloc_mutex); | |
71 | } | |
72 | } | |
fec577fb | 73 | |
0f9dd46c JB |
74 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
75 | { | |
76 | return (cache->flags & bits) == bits; | |
77 | } | |
78 | ||
79 | /* | |
80 | * this adds the block group to the fs_info rb tree for the block group | |
81 | * cache | |
82 | */ | |
83 | int btrfs_add_block_group_cache(struct btrfs_fs_info *info, | |
84 | struct btrfs_block_group_cache *block_group) | |
85 | { | |
86 | struct rb_node **p; | |
87 | struct rb_node *parent = NULL; | |
88 | struct btrfs_block_group_cache *cache; | |
89 | ||
90 | spin_lock(&info->block_group_cache_lock); | |
91 | p = &info->block_group_cache_tree.rb_node; | |
92 | ||
93 | while (*p) { | |
94 | parent = *p; | |
95 | cache = rb_entry(parent, struct btrfs_block_group_cache, | |
96 | cache_node); | |
97 | if (block_group->key.objectid < cache->key.objectid) { | |
98 | p = &(*p)->rb_left; | |
99 | } else if (block_group->key.objectid > cache->key.objectid) { | |
100 | p = &(*p)->rb_right; | |
101 | } else { | |
102 | spin_unlock(&info->block_group_cache_lock); | |
103 | return -EEXIST; | |
104 | } | |
105 | } | |
106 | ||
107 | rb_link_node(&block_group->cache_node, parent, p); | |
108 | rb_insert_color(&block_group->cache_node, | |
109 | &info->block_group_cache_tree); | |
110 | spin_unlock(&info->block_group_cache_lock); | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
115 | /* | |
116 | * This will return the block group at or after bytenr if contains is 0, else | |
117 | * it will return the block group that contains the bytenr | |
118 | */ | |
119 | static struct btrfs_block_group_cache * | |
120 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, | |
121 | int contains) | |
122 | { | |
123 | struct btrfs_block_group_cache *cache, *ret = NULL; | |
124 | struct rb_node *n; | |
125 | u64 end, start; | |
126 | ||
127 | spin_lock(&info->block_group_cache_lock); | |
128 | n = info->block_group_cache_tree.rb_node; | |
129 | ||
130 | while (n) { | |
131 | cache = rb_entry(n, struct btrfs_block_group_cache, | |
132 | cache_node); | |
133 | end = cache->key.objectid + cache->key.offset - 1; | |
134 | start = cache->key.objectid; | |
135 | ||
136 | if (bytenr < start) { | |
137 | if (!contains && (!ret || start < ret->key.objectid)) | |
138 | ret = cache; | |
139 | n = n->rb_left; | |
140 | } else if (bytenr > start) { | |
141 | if (contains && bytenr <= end) { | |
142 | ret = cache; | |
143 | break; | |
144 | } | |
145 | n = n->rb_right; | |
146 | } else { | |
147 | ret = cache; | |
148 | break; | |
149 | } | |
150 | } | |
151 | spin_unlock(&info->block_group_cache_lock); | |
152 | ||
153 | return ret; | |
154 | } | |
155 | ||
156 | /* | |
157 | * this is only called by cache_block_group, since we could have freed extents | |
158 | * we need to check the pinned_extents for any extents that can't be used yet | |
159 | * since their free space will be released as soon as the transaction commits. | |
160 | */ | |
161 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, | |
162 | struct btrfs_fs_info *info, u64 start, u64 end) | |
163 | { | |
164 | u64 extent_start, extent_end, size; | |
165 | int ret; | |
166 | ||
167 | while (start < end) { | |
168 | ret = find_first_extent_bit(&info->pinned_extents, start, | |
169 | &extent_start, &extent_end, | |
170 | EXTENT_DIRTY); | |
171 | if (ret) | |
172 | break; | |
173 | ||
174 | if (extent_start == start) { | |
175 | start = extent_end + 1; | |
176 | } else if (extent_start > start && extent_start < end) { | |
177 | size = extent_start - start; | |
178 | ret = btrfs_add_free_space(block_group, start, size); | |
179 | BUG_ON(ret); | |
180 | start = extent_end + 1; | |
181 | } else { | |
182 | break; | |
183 | } | |
184 | } | |
185 | ||
186 | if (start < end) { | |
187 | size = end - start; | |
188 | ret = btrfs_add_free_space(block_group, start, size); | |
189 | BUG_ON(ret); | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
e37c9e69 CM |
195 | static int cache_block_group(struct btrfs_root *root, |
196 | struct btrfs_block_group_cache *block_group) | |
197 | { | |
198 | struct btrfs_path *path; | |
ef8bbdfe | 199 | int ret = 0; |
e37c9e69 | 200 | struct btrfs_key key; |
5f39d397 | 201 | struct extent_buffer *leaf; |
e37c9e69 | 202 | int slot; |
e37c9e69 | 203 | u64 last = 0; |
7d7d6068 | 204 | u64 first_free; |
e37c9e69 CM |
205 | int found = 0; |
206 | ||
00f5c795 CM |
207 | if (!block_group) |
208 | return 0; | |
209 | ||
e37c9e69 | 210 | root = root->fs_info->extent_root; |
e37c9e69 CM |
211 | |
212 | if (block_group->cached) | |
213 | return 0; | |
f510cfec | 214 | |
e37c9e69 CM |
215 | path = btrfs_alloc_path(); |
216 | if (!path) | |
217 | return -ENOMEM; | |
7d7d6068 | 218 | |
2cc58cf2 | 219 | path->reada = 2; |
5cd57b2c CM |
220 | /* |
221 | * we get into deadlocks with paths held by callers of this function. | |
222 | * since the alloc_mutex is protecting things right now, just | |
223 | * skip the locking here | |
224 | */ | |
225 | path->skip_locking = 1; | |
0f9dd46c JB |
226 | first_free = max_t(u64, block_group->key.objectid, |
227 | BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE); | |
e37c9e69 | 228 | key.objectid = block_group->key.objectid; |
e37c9e69 CM |
229 | key.offset = 0; |
230 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
231 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
232 | if (ret < 0) | |
ef8bbdfe | 233 | goto err; |
0b86a832 | 234 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
d548ee51 | 235 | if (ret < 0) |
ef8bbdfe | 236 | goto err; |
d548ee51 Y |
237 | if (ret == 0) { |
238 | leaf = path->nodes[0]; | |
239 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
240 | if (key.objectid + key.offset > first_free) | |
241 | first_free = key.objectid + key.offset; | |
242 | } | |
e37c9e69 | 243 | while(1) { |
5f39d397 | 244 | leaf = path->nodes[0]; |
e37c9e69 | 245 | slot = path->slots[0]; |
5f39d397 | 246 | if (slot >= btrfs_header_nritems(leaf)) { |
e37c9e69 | 247 | ret = btrfs_next_leaf(root, path); |
54aa1f4d CM |
248 | if (ret < 0) |
249 | goto err; | |
0f9dd46c | 250 | if (ret == 0) |
e37c9e69 | 251 | continue; |
0f9dd46c | 252 | else |
e37c9e69 | 253 | break; |
e37c9e69 | 254 | } |
5f39d397 | 255 | btrfs_item_key_to_cpu(leaf, &key, slot); |
0f9dd46c | 256 | if (key.objectid < block_group->key.objectid) |
7d7d6068 | 257 | goto next; |
0f9dd46c | 258 | |
e37c9e69 | 259 | if (key.objectid >= block_group->key.objectid + |
0f9dd46c | 260 | block_group->key.offset) |
e37c9e69 | 261 | break; |
7d7d6068 | 262 | |
e37c9e69 CM |
263 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
264 | if (!found) { | |
7d7d6068 | 265 | last = first_free; |
e37c9e69 | 266 | found = 1; |
e37c9e69 | 267 | } |
0f9dd46c JB |
268 | |
269 | add_new_free_space(block_group, root->fs_info, last, | |
270 | key.objectid); | |
271 | ||
7d7d6068 | 272 | last = key.objectid + key.offset; |
e37c9e69 | 273 | } |
7d7d6068 | 274 | next: |
e37c9e69 CM |
275 | path->slots[0]++; |
276 | } | |
277 | ||
7d7d6068 Y |
278 | if (!found) |
279 | last = first_free; | |
0f9dd46c JB |
280 | |
281 | add_new_free_space(block_group, root->fs_info, last, | |
282 | block_group->key.objectid + | |
283 | block_group->key.offset); | |
284 | ||
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 | */ | |
0ef3e66b CM |
295 | struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct |
296 | btrfs_fs_info *info, | |
297 | u64 bytenr) | |
298 | { | |
0f9dd46c | 299 | struct btrfs_block_group_cache *cache; |
0ef3e66b | 300 | |
0f9dd46c | 301 | cache = block_group_cache_tree_search(info, bytenr, 0); |
0ef3e66b | 302 | |
0f9dd46c | 303 | return cache; |
0ef3e66b CM |
304 | } |
305 | ||
0f9dd46c JB |
306 | /* |
307 | * return the block group that contains teh given bytenr | |
308 | */ | |
5276aeda CM |
309 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
310 | btrfs_fs_info *info, | |
db94535d | 311 | u64 bytenr) |
be744175 | 312 | { |
0f9dd46c | 313 | struct btrfs_block_group_cache *cache; |
be744175 | 314 | |
0f9dd46c | 315 | cache = block_group_cache_tree_search(info, bytenr, 1); |
96b5179d | 316 | |
0f9dd46c | 317 | return cache; |
be744175 | 318 | } |
0b86a832 | 319 | |
0f9dd46c JB |
320 | static int noinline find_free_space(struct btrfs_root *root, |
321 | struct btrfs_block_group_cache **cache_ret, | |
322 | u64 *start_ret, u64 num, int data) | |
e37c9e69 | 323 | { |
e37c9e69 CM |
324 | int ret; |
325 | struct btrfs_block_group_cache *cache = *cache_ret; | |
0f9dd46c | 326 | struct btrfs_free_space *info = NULL; |
e19caa5f | 327 | u64 last; |
c31f8830 | 328 | u64 total_fs_bytes; |
0b86a832 | 329 | u64 search_start = *start_ret; |
e37c9e69 | 330 | |
7d9eb12c | 331 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
c31f8830 | 332 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
d7fc640e | 333 | |
0ef3e66b CM |
334 | if (!cache) |
335 | goto out; | |
336 | ||
0f9dd46c JB |
337 | last = max(search_start, cache->key.objectid); |
338 | ||
e37c9e69 | 339 | again: |
54aa1f4d | 340 | ret = cache_block_group(root, cache); |
0f9dd46c | 341 | if (ret) |
54aa1f4d | 342 | goto out; |
f84a8b36 | 343 | |
0f9dd46c | 344 | if (cache->ro || !block_group_bits(cache, data)) |
0b86a832 | 345 | goto new_group; |
e19caa5f | 346 | |
0f9dd46c JB |
347 | info = btrfs_find_free_space(cache, last, num); |
348 | if (info) { | |
349 | *start_ret = info->offset; | |
0b86a832 | 350 | return 0; |
8790d502 | 351 | } |
e37c9e69 CM |
352 | |
353 | new_group: | |
e19caa5f | 354 | last = cache->key.objectid + cache->key.offset; |
0f9dd46c | 355 | |
0ef3e66b | 356 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
0f9dd46c | 357 | if (!cache || cache->key.objectid >= total_fs_bytes) |
1a2b2ac7 | 358 | goto out; |
0f9dd46c | 359 | |
e37c9e69 CM |
360 | *cache_ret = cache; |
361 | goto again; | |
0f9dd46c JB |
362 | |
363 | out: | |
364 | return -ENOSPC; | |
e37c9e69 CM |
365 | } |
366 | ||
84f54cfa CM |
367 | static u64 div_factor(u64 num, int factor) |
368 | { | |
257d0ce3 CM |
369 | if (factor == 10) |
370 | return num; | |
84f54cfa CM |
371 | num *= factor; |
372 | do_div(num, 10); | |
373 | return num; | |
374 | } | |
375 | ||
0f9dd46c JB |
376 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
377 | u64 flags) | |
6324fbf3 | 378 | { |
0f9dd46c JB |
379 | struct list_head *head = &info->space_info; |
380 | struct list_head *cur; | |
381 | struct btrfs_space_info *found; | |
382 | list_for_each(cur, head) { | |
383 | found = list_entry(cur, struct btrfs_space_info, list); | |
384 | if (found->flags == flags) | |
385 | return found; | |
386 | } | |
387 | return NULL; | |
388 | ||
6324fbf3 CM |
389 | } |
390 | ||
925baedd CM |
391 | static struct btrfs_block_group_cache * |
392 | __btrfs_find_block_group(struct btrfs_root *root, | |
393 | struct btrfs_block_group_cache *hint, | |
394 | u64 search_start, int data, int owner) | |
cd1bc465 | 395 | { |
96b5179d | 396 | struct btrfs_block_group_cache *cache; |
31f3c99b | 397 | struct btrfs_block_group_cache *found_group = NULL; |
cd1bc465 | 398 | struct btrfs_fs_info *info = root->fs_info; |
0f9dd46c | 399 | struct btrfs_space_info *sinfo; |
cd1bc465 | 400 | u64 used; |
31f3c99b | 401 | u64 last = 0; |
96b5179d | 402 | u64 free_check; |
31f3c99b | 403 | int full_search = 0; |
bce4eae9 | 404 | int factor = 10; |
0ef3e66b | 405 | int wrapped = 0; |
de428b63 | 406 | |
a236aed1 CM |
407 | if (data & BTRFS_BLOCK_GROUP_METADATA) |
408 | factor = 9; | |
be744175 | 409 | |
0ef3e66b | 410 | if (search_start) { |
be744175 | 411 | struct btrfs_block_group_cache *shint; |
0ef3e66b | 412 | shint = btrfs_lookup_first_block_group(info, search_start); |
8f18cf13 | 413 | if (shint && block_group_bits(shint, data) && !shint->ro) { |
c286ac48 | 414 | spin_lock(&shint->lock); |
be744175 | 415 | used = btrfs_block_group_used(&shint->item); |
324ae4df Y |
416 | if (used + shint->pinned < |
417 | div_factor(shint->key.offset, factor)) { | |
c286ac48 | 418 | spin_unlock(&shint->lock); |
be744175 CM |
419 | return shint; |
420 | } | |
c286ac48 | 421 | spin_unlock(&shint->lock); |
be744175 CM |
422 | } |
423 | } | |
0ef3e66b | 424 | if (hint && !hint->ro && block_group_bits(hint, data)) { |
c286ac48 | 425 | spin_lock(&hint->lock); |
31f3c99b | 426 | used = btrfs_block_group_used(&hint->item); |
324ae4df Y |
427 | if (used + hint->pinned < |
428 | div_factor(hint->key.offset, factor)) { | |
c286ac48 | 429 | spin_unlock(&hint->lock); |
31f3c99b CM |
430 | return hint; |
431 | } | |
c286ac48 | 432 | spin_unlock(&hint->lock); |
e19caa5f | 433 | last = hint->key.objectid + hint->key.offset; |
31f3c99b | 434 | } else { |
e37c9e69 | 435 | if (hint) |
0ef3e66b | 436 | last = max(hint->key.objectid, search_start); |
e37c9e69 | 437 | else |
0ef3e66b | 438 | last = search_start; |
31f3c99b | 439 | } |
0f9dd46c JB |
440 | sinfo = __find_space_info(root->fs_info, data); |
441 | if (!sinfo) | |
442 | goto found; | |
31f3c99b | 443 | again: |
cd1bc465 | 444 | while(1) { |
0f9dd46c | 445 | struct list_head *l; |
96b5179d | 446 | |
0f9dd46c JB |
447 | cache = NULL; |
448 | ||
449 | spin_lock(&sinfo->lock); | |
450 | list_for_each(l, &sinfo->block_groups) { | |
451 | struct btrfs_block_group_cache *entry; | |
452 | entry = list_entry(l, struct btrfs_block_group_cache, | |
453 | list); | |
454 | if ((entry->key.objectid >= last) && | |
455 | (!cache || (entry->key.objectid < | |
456 | cache->key.objectid))) | |
457 | cache = entry; | |
0ef3e66b | 458 | } |
0f9dd46c JB |
459 | spin_unlock(&sinfo->lock); |
460 | ||
461 | if (!cache) | |
462 | break; | |
96b5179d | 463 | |
c286ac48 | 464 | spin_lock(&cache->lock); |
96b5179d CM |
465 | last = cache->key.objectid + cache->key.offset; |
466 | used = btrfs_block_group_used(&cache->item); | |
467 | ||
8f18cf13 | 468 | if (!cache->ro && block_group_bits(cache, data)) { |
0ef3e66b | 469 | free_check = div_factor(cache->key.offset, factor); |
8790d502 CM |
470 | if (used + cache->pinned < free_check) { |
471 | found_group = cache; | |
c286ac48 | 472 | spin_unlock(&cache->lock); |
8790d502 CM |
473 | goto found; |
474 | } | |
6324fbf3 | 475 | } |
c286ac48 | 476 | spin_unlock(&cache->lock); |
de428b63 | 477 | cond_resched(); |
cd1bc465 | 478 | } |
0ef3e66b CM |
479 | if (!wrapped) { |
480 | last = search_start; | |
481 | wrapped = 1; | |
482 | goto again; | |
483 | } | |
484 | if (!full_search && factor < 10) { | |
be744175 | 485 | last = search_start; |
31f3c99b | 486 | full_search = 1; |
0ef3e66b | 487 | factor = 10; |
31f3c99b CM |
488 | goto again; |
489 | } | |
be744175 | 490 | found: |
31f3c99b | 491 | return found_group; |
cd1bc465 CM |
492 | } |
493 | ||
925baedd CM |
494 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
495 | struct btrfs_block_group_cache | |
496 | *hint, u64 search_start, | |
497 | int data, int owner) | |
498 | { | |
499 | ||
500 | struct btrfs_block_group_cache *ret; | |
925baedd | 501 | ret = __btrfs_find_block_group(root, hint, search_start, data, owner); |
925baedd CM |
502 | return ret; |
503 | } | |
0f9dd46c | 504 | |
e02119d5 | 505 | /* simple helper to search for an existing extent at a given offset */ |
31840ae1 | 506 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
e02119d5 CM |
507 | { |
508 | int ret; | |
509 | struct btrfs_key key; | |
31840ae1 | 510 | struct btrfs_path *path; |
e02119d5 | 511 | |
31840ae1 ZY |
512 | path = btrfs_alloc_path(); |
513 | BUG_ON(!path); | |
e02119d5 CM |
514 | maybe_lock_mutex(root); |
515 | key.objectid = start; | |
516 | key.offset = len; | |
517 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
518 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, | |
519 | 0, 0); | |
520 | maybe_unlock_mutex(root); | |
31840ae1 | 521 | btrfs_free_path(path); |
7bb86316 CM |
522 | return ret; |
523 | } | |
524 | ||
d8d5f3e1 CM |
525 | /* |
526 | * Back reference rules. Back refs have three main goals: | |
527 | * | |
528 | * 1) differentiate between all holders of references to an extent so that | |
529 | * when a reference is dropped we can make sure it was a valid reference | |
530 | * before freeing the extent. | |
531 | * | |
532 | * 2) Provide enough information to quickly find the holders of an extent | |
533 | * if we notice a given block is corrupted or bad. | |
534 | * | |
535 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool | |
536 | * maintenance. This is actually the same as #2, but with a slightly | |
537 | * different use case. | |
538 | * | |
539 | * File extents can be referenced by: | |
540 | * | |
541 | * - multiple snapshots, subvolumes, or different generations in one subvol | |
31840ae1 | 542 | * - different files inside a single subvolume |
d8d5f3e1 CM |
543 | * - different offsets inside a file (bookend extents in file.c) |
544 | * | |
545 | * The extent ref structure has fields for: | |
546 | * | |
547 | * - Objectid of the subvolume root | |
548 | * - Generation number of the tree holding the reference | |
549 | * - objectid of the file holding the reference | |
550 | * - offset in the file corresponding to the key holding the reference | |
31840ae1 ZY |
551 | * - number of references holding by parent node (alway 1 for tree blocks) |
552 | * | |
553 | * Btree leaf may hold multiple references to a file extent. In most cases, | |
554 | * these references are from same file and the corresponding offsets inside | |
555 | * the file are close together. So inode objectid and offset in file are | |
556 | * just hints, they provide hints about where in the btree the references | |
557 | * can be found and when we can stop searching. | |
d8d5f3e1 CM |
558 | * |
559 | * When a file extent is allocated the fields are filled in: | |
31840ae1 | 560 | * (root_key.objectid, trans->transid, inode objectid, offset in file, 1) |
d8d5f3e1 CM |
561 | * |
562 | * When a leaf is cow'd new references are added for every file extent found | |
31840ae1 ZY |
563 | * in the leaf. It looks similar to the create case, but trans->transid will |
564 | * be different when the block is cow'd. | |
d8d5f3e1 | 565 | * |
31840ae1 ZY |
566 | * (root_key.objectid, trans->transid, inode objectid, offset in file, |
567 | * number of references in the leaf) | |
d8d5f3e1 | 568 | * |
31840ae1 ZY |
569 | * Because inode objectid and offset in file are just hints, they are not |
570 | * used when backrefs are deleted. When a file extent is removed either | |
571 | * during snapshot deletion or file truncation, we find the corresponding | |
572 | * back back reference and check the following fields. | |
d8d5f3e1 | 573 | * |
31840ae1 | 574 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf)) |
d8d5f3e1 CM |
575 | * |
576 | * Btree extents can be referenced by: | |
577 | * | |
578 | * - Different subvolumes | |
579 | * - Different generations of the same subvolume | |
580 | * | |
d8d5f3e1 CM |
581 | * When a tree block is created, back references are inserted: |
582 | * | |
31840ae1 | 583 | * (root->root_key.objectid, trans->transid, level, 0, 1) |
d8d5f3e1 | 584 | * |
31840ae1 ZY |
585 | * When a tree block is cow'd, new back references are added for all the |
586 | * blocks it points to. If the tree block isn't in reference counted root, | |
587 | * the old back references are removed. These new back references are of | |
588 | * the form (trans->transid will have increased since creation): | |
d8d5f3e1 | 589 | * |
31840ae1 | 590 | * (root->root_key.objectid, trans->transid, level, 0, 1) |
d8d5f3e1 | 591 | * |
31840ae1 | 592 | * When a backref is in deleting, the following fields are checked: |
d8d5f3e1 CM |
593 | * |
594 | * if backref was for a tree root: | |
31840ae1 | 595 | * (btrfs_header_owner(itself), btrfs_header_generation(itself)) |
d8d5f3e1 | 596 | * else |
31840ae1 | 597 | * (btrfs_header_owner(parent), btrfs_header_generation(parent)) |
d8d5f3e1 | 598 | * |
31840ae1 | 599 | * Back Reference Key composing: |
d8d5f3e1 | 600 | * |
31840ae1 ZY |
601 | * The key objectid corresponds to the first byte in the extent, the key |
602 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first | |
603 | * byte of parent extent. If a extent is tree root, the key offset is set | |
604 | * to the key objectid. | |
d8d5f3e1 | 605 | */ |
31840ae1 ZY |
606 | |
607 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, | |
608 | struct btrfs_root *root, | |
609 | struct btrfs_path *path, u64 bytenr, | |
610 | u64 parent, u64 ref_root, | |
611 | u64 ref_generation, int del) | |
7bb86316 | 612 | { |
7bb86316 | 613 | struct btrfs_key key; |
31840ae1 ZY |
614 | struct btrfs_extent_ref *ref; |
615 | struct extent_buffer *leaf; | |
74493f7a CM |
616 | int ret; |
617 | ||
31840ae1 ZY |
618 | key.objectid = bytenr; |
619 | key.type = BTRFS_EXTENT_REF_KEY; | |
620 | key.offset = parent; | |
621 | ||
622 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); | |
623 | if (ret < 0) | |
624 | goto out; | |
625 | if (ret > 0) { | |
626 | ret = -ENOENT; | |
627 | goto out; | |
628 | } | |
629 | ||
630 | leaf = path->nodes[0]; | |
631 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | |
632 | if (btrfs_ref_root(leaf, ref) != ref_root || | |
633 | btrfs_ref_generation(leaf, ref) != ref_generation) { | |
634 | ret = -EIO; | |
635 | WARN_ON(1); | |
636 | goto out; | |
637 | } | |
638 | ret = 0; | |
639 | out: | |
640 | return ret; | |
641 | } | |
642 | ||
643 | static int noinline insert_extent_backref(struct btrfs_trans_handle *trans, | |
644 | struct btrfs_root *root, | |
645 | struct btrfs_path *path, | |
646 | u64 bytenr, u64 parent, | |
647 | u64 ref_root, u64 ref_generation, | |
648 | u64 owner_objectid, u64 owner_offset) | |
649 | { | |
650 | struct btrfs_key key; | |
651 | struct extent_buffer *leaf; | |
652 | struct btrfs_extent_ref *ref; | |
653 | u32 num_refs; | |
654 | int ret; | |
74493f7a | 655 | |
74493f7a CM |
656 | key.objectid = bytenr; |
657 | key.type = BTRFS_EXTENT_REF_KEY; | |
31840ae1 | 658 | key.offset = parent; |
74493f7a | 659 | |
31840ae1 ZY |
660 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
661 | if (ret == 0) { | |
662 | leaf = path->nodes[0]; | |
663 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
664 | struct btrfs_extent_ref); | |
665 | btrfs_set_ref_root(leaf, ref, ref_root); | |
666 | btrfs_set_ref_generation(leaf, ref, ref_generation); | |
667 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); | |
668 | btrfs_set_ref_offset(leaf, ref, owner_offset); | |
669 | btrfs_set_ref_num_refs(leaf, ref, 1); | |
670 | } else if (ret == -EEXIST) { | |
671 | u64 existing_owner; | |
672 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); | |
673 | leaf = path->nodes[0]; | |
674 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
675 | struct btrfs_extent_ref); | |
676 | if (btrfs_ref_root(leaf, ref) != ref_root || | |
677 | btrfs_ref_generation(leaf, ref) != ref_generation) { | |
678 | ret = -EIO; | |
679 | WARN_ON(1); | |
7bb86316 | 680 | goto out; |
31840ae1 ZY |
681 | } |
682 | ||
683 | num_refs = btrfs_ref_num_refs(leaf, ref); | |
684 | BUG_ON(num_refs == 0); | |
685 | btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); | |
686 | ||
687 | existing_owner = btrfs_ref_objectid(leaf, ref); | |
688 | if (existing_owner == owner_objectid && | |
689 | btrfs_ref_offset(leaf, ref) > owner_offset) { | |
690 | btrfs_set_ref_offset(leaf, ref, owner_offset); | |
691 | } else if (existing_owner != owner_objectid && | |
692 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { | |
693 | btrfs_set_ref_objectid(leaf, ref, | |
694 | BTRFS_MULTIPLE_OBJECTIDS); | |
695 | btrfs_set_ref_offset(leaf, ref, 0); | |
696 | } | |
697 | ret = 0; | |
698 | } else { | |
7bb86316 | 699 | goto out; |
31840ae1 | 700 | } |
7bb86316 CM |
701 | btrfs_mark_buffer_dirty(path->nodes[0]); |
702 | out: | |
703 | btrfs_release_path(root, path); | |
704 | return ret; | |
74493f7a CM |
705 | } |
706 | ||
31840ae1 ZY |
707 | static int noinline remove_extent_backref(struct btrfs_trans_handle *trans, |
708 | struct btrfs_root *root, | |
709 | struct btrfs_path *path) | |
710 | { | |
711 | struct extent_buffer *leaf; | |
712 | struct btrfs_extent_ref *ref; | |
713 | u32 num_refs; | |
714 | int ret = 0; | |
715 | ||
716 | leaf = path->nodes[0]; | |
717 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); | |
718 | num_refs = btrfs_ref_num_refs(leaf, ref); | |
719 | BUG_ON(num_refs == 0); | |
720 | num_refs -= 1; | |
721 | if (num_refs == 0) { | |
722 | ret = btrfs_del_item(trans, root, path); | |
723 | } else { | |
724 | btrfs_set_ref_num_refs(leaf, ref, num_refs); | |
725 | btrfs_mark_buffer_dirty(leaf); | |
726 | } | |
727 | btrfs_release_path(root, path); | |
728 | return ret; | |
729 | } | |
730 | ||
731 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, | |
732 | struct btrfs_root *root, u64 bytenr, | |
733 | u64 orig_parent, u64 parent, | |
734 | u64 orig_root, u64 ref_root, | |
735 | u64 orig_generation, u64 ref_generation, | |
736 | u64 owner_objectid, u64 owner_offset) | |
737 | { | |
738 | int ret; | |
739 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
740 | struct btrfs_path *path; | |
741 | ||
742 | if (root == root->fs_info->extent_root) { | |
743 | struct pending_extent_op *extent_op; | |
744 | u64 num_bytes; | |
745 | ||
746 | BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL); | |
747 | num_bytes = btrfs_level_size(root, (int)owner_objectid); | |
748 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, | |
749 | bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) { | |
750 | u64 priv; | |
751 | ret = get_state_private(&root->fs_info->extent_ins, | |
752 | bytenr, &priv); | |
753 | BUG_ON(ret); | |
754 | extent_op = (struct pending_extent_op *) | |
755 | (unsigned long)priv; | |
756 | BUG_ON(extent_op->parent != orig_parent); | |
757 | BUG_ON(extent_op->generation != orig_generation); | |
758 | extent_op->parent = parent; | |
759 | extent_op->generation = ref_generation; | |
760 | } else { | |
761 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | |
762 | BUG_ON(!extent_op); | |
763 | ||
764 | extent_op->type = PENDING_BACKREF_UPDATE; | |
765 | extent_op->bytenr = bytenr; | |
766 | extent_op->num_bytes = num_bytes; | |
767 | extent_op->parent = parent; | |
768 | extent_op->orig_parent = orig_parent; | |
769 | extent_op->generation = ref_generation; | |
770 | extent_op->orig_generation = orig_generation; | |
771 | extent_op->level = (int)owner_objectid; | |
772 | ||
773 | set_extent_bits(&root->fs_info->extent_ins, | |
774 | bytenr, bytenr + num_bytes - 1, | |
775 | EXTENT_LOCKED, GFP_NOFS); | |
776 | set_state_private(&root->fs_info->extent_ins, | |
777 | bytenr, (unsigned long)extent_op); | |
778 | } | |
779 | return 0; | |
780 | } | |
781 | ||
782 | path = btrfs_alloc_path(); | |
783 | if (!path) | |
784 | return -ENOMEM; | |
785 | ret = lookup_extent_backref(trans, extent_root, path, | |
786 | bytenr, orig_parent, orig_root, | |
787 | orig_generation, 1); | |
788 | if (ret) | |
789 | goto out; | |
790 | ret = remove_extent_backref(trans, extent_root, path); | |
791 | if (ret) | |
792 | goto out; | |
793 | ret = insert_extent_backref(trans, extent_root, path, bytenr, | |
794 | parent, ref_root, ref_generation, | |
795 | owner_objectid, owner_offset); | |
796 | BUG_ON(ret); | |
797 | finish_current_insert(trans, extent_root); | |
798 | del_pending_extents(trans, extent_root); | |
799 | out: | |
800 | btrfs_free_path(path); | |
801 | return ret; | |
802 | } | |
803 | ||
804 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, | |
805 | struct btrfs_root *root, u64 bytenr, | |
806 | u64 orig_parent, u64 parent, | |
807 | u64 ref_root, u64 ref_generation, | |
808 | u64 owner_objectid, u64 owner_offset) | |
809 | { | |
810 | int ret; | |
811 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && | |
812 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | |
813 | return 0; | |
814 | maybe_lock_mutex(root); | |
815 | ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, | |
816 | parent, ref_root, ref_root, | |
817 | ref_generation, ref_generation, | |
818 | owner_objectid, owner_offset); | |
819 | maybe_unlock_mutex(root); | |
820 | return ret; | |
821 | } | |
822 | ||
925baedd | 823 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
824 | struct btrfs_root *root, u64 bytenr, |
825 | u64 orig_parent, u64 parent, | |
826 | u64 orig_root, u64 ref_root, | |
827 | u64 orig_generation, u64 ref_generation, | |
828 | u64 owner_objectid, u64 owner_offset) | |
02217ed2 | 829 | { |
5caf2a00 | 830 | struct btrfs_path *path; |
02217ed2 | 831 | int ret; |
e2fa7227 | 832 | struct btrfs_key key; |
5f39d397 | 833 | struct extent_buffer *l; |
234b63a0 | 834 | struct btrfs_extent_item *item; |
cf27e1ee | 835 | u32 refs; |
037e6390 | 836 | |
5caf2a00 | 837 | path = btrfs_alloc_path(); |
54aa1f4d CM |
838 | if (!path) |
839 | return -ENOMEM; | |
26b8003f | 840 | |
3c12ac72 | 841 | path->reada = 1; |
db94535d | 842 | key.objectid = bytenr; |
31840ae1 ZY |
843 | key.type = BTRFS_EXTENT_ITEM_KEY; |
844 | key.offset = (u64)-1; | |
845 | ||
5caf2a00 | 846 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 847 | 0, 1); |
54aa1f4d CM |
848 | if (ret < 0) |
849 | return ret; | |
31840ae1 ZY |
850 | BUG_ON(ret == 0 || path->slots[0] == 0); |
851 | ||
852 | path->slots[0]--; | |
5f39d397 | 853 | l = path->nodes[0]; |
31840ae1 ZY |
854 | |
855 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); | |
856 | BUG_ON(key.objectid != bytenr); | |
857 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); | |
858 | ||
5caf2a00 | 859 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
5f39d397 CM |
860 | refs = btrfs_extent_refs(l, item); |
861 | btrfs_set_extent_refs(l, item, refs + 1); | |
5caf2a00 | 862 | btrfs_mark_buffer_dirty(path->nodes[0]); |
a28ec197 | 863 | |
5caf2a00 | 864 | btrfs_release_path(root->fs_info->extent_root, path); |
7bb86316 | 865 | |
3c12ac72 | 866 | path->reada = 1; |
31840ae1 ZY |
867 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
868 | path, bytenr, parent, | |
869 | ref_root, ref_generation, | |
870 | owner_objectid, owner_offset); | |
7bb86316 | 871 | BUG_ON(ret); |
9f5fae2f | 872 | finish_current_insert(trans, root->fs_info->extent_root); |
e20d96d6 | 873 | del_pending_extents(trans, root->fs_info->extent_root); |
74493f7a CM |
874 | |
875 | btrfs_free_path(path); | |
02217ed2 CM |
876 | return 0; |
877 | } | |
878 | ||
925baedd | 879 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
880 | struct btrfs_root *root, |
881 | u64 bytenr, u64 num_bytes, u64 parent, | |
882 | u64 ref_root, u64 ref_generation, | |
883 | u64 owner_objectid, u64 owner_offset) | |
925baedd CM |
884 | { |
885 | int ret; | |
31840ae1 ZY |
886 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
887 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) | |
888 | return 0; | |
889 | maybe_lock_mutex(root); | |
890 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, | |
891 | 0, ref_root, 0, ref_generation, | |
892 | owner_objectid, owner_offset); | |
893 | maybe_unlock_mutex(root); | |
925baedd CM |
894 | return ret; |
895 | } | |
896 | ||
e9d0b13b CM |
897 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
898 | struct btrfs_root *root) | |
899 | { | |
900 | finish_current_insert(trans, root->fs_info->extent_root); | |
901 | del_pending_extents(trans, root->fs_info->extent_root); | |
902 | return 0; | |
903 | } | |
904 | ||
31840ae1 ZY |
905 | int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, |
906 | struct btrfs_root *root, u64 bytenr, | |
907 | u64 num_bytes, u32 *refs) | |
a28ec197 | 908 | { |
5caf2a00 | 909 | struct btrfs_path *path; |
a28ec197 | 910 | int ret; |
e2fa7227 | 911 | struct btrfs_key key; |
5f39d397 | 912 | struct extent_buffer *l; |
234b63a0 | 913 | struct btrfs_extent_item *item; |
5caf2a00 | 914 | |
db94535d | 915 | WARN_ON(num_bytes < root->sectorsize); |
5caf2a00 | 916 | path = btrfs_alloc_path(); |
3c12ac72 | 917 | path->reada = 1; |
db94535d CM |
918 | key.objectid = bytenr; |
919 | key.offset = num_bytes; | |
62e2749e | 920 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
5caf2a00 | 921 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 922 | 0, 0); |
54aa1f4d CM |
923 | if (ret < 0) |
924 | goto out; | |
5f39d397 CM |
925 | if (ret != 0) { |
926 | btrfs_print_leaf(root, path->nodes[0]); | |
db94535d | 927 | printk("failed to find block number %Lu\n", bytenr); |
a28ec197 | 928 | BUG(); |
5f39d397 CM |
929 | } |
930 | l = path->nodes[0]; | |
5caf2a00 | 931 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
5f39d397 | 932 | *refs = btrfs_extent_refs(l, item); |
54aa1f4d | 933 | out: |
5caf2a00 | 934 | btrfs_free_path(path); |
a28ec197 CM |
935 | return 0; |
936 | } | |
937 | ||
f321e491 YZ |
938 | static int get_reference_status(struct btrfs_root *root, u64 bytenr, |
939 | u64 parent_gen, u64 ref_objectid, | |
940 | u64 *min_generation, u32 *ref_count) | |
be20aa9d CM |
941 | { |
942 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
943 | struct btrfs_path *path; | |
f321e491 YZ |
944 | struct extent_buffer *leaf; |
945 | struct btrfs_extent_ref *ref_item; | |
946 | struct btrfs_key key; | |
947 | struct btrfs_key found_key; | |
56b453c9 | 948 | u64 root_objectid = root->root_key.objectid; |
f321e491 | 949 | u64 ref_generation; |
be20aa9d CM |
950 | u32 nritems; |
951 | int ret; | |
925baedd | 952 | |
be20aa9d | 953 | key.objectid = bytenr; |
31840ae1 | 954 | key.offset = (u64)-1; |
f321e491 | 955 | key.type = BTRFS_EXTENT_ITEM_KEY; |
be20aa9d | 956 | |
f321e491 YZ |
957 | path = btrfs_alloc_path(); |
958 | mutex_lock(&root->fs_info->alloc_mutex); | |
be20aa9d CM |
959 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
960 | if (ret < 0) | |
961 | goto out; | |
962 | BUG_ON(ret == 0); | |
31840ae1 ZY |
963 | if (ret < 0 || path->slots[0] == 0) |
964 | goto out; | |
be20aa9d | 965 | |
31840ae1 | 966 | path->slots[0]--; |
f321e491 YZ |
967 | leaf = path->nodes[0]; |
968 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
be20aa9d CM |
969 | |
970 | if (found_key.objectid != bytenr || | |
971 | found_key.type != BTRFS_EXTENT_ITEM_KEY) { | |
f321e491 | 972 | ret = 1; |
be20aa9d CM |
973 | goto out; |
974 | } | |
975 | ||
f321e491 YZ |
976 | *ref_count = 0; |
977 | *min_generation = (u64)-1; | |
978 | ||
be20aa9d | 979 | while (1) { |
f321e491 YZ |
980 | leaf = path->nodes[0]; |
981 | nritems = btrfs_header_nritems(leaf); | |
be20aa9d CM |
982 | if (path->slots[0] >= nritems) { |
983 | ret = btrfs_next_leaf(extent_root, path); | |
f321e491 YZ |
984 | if (ret < 0) |
985 | goto out; | |
be20aa9d CM |
986 | if (ret == 0) |
987 | continue; | |
988 | break; | |
989 | } | |
f321e491 | 990 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
be20aa9d CM |
991 | if (found_key.objectid != bytenr) |
992 | break; | |
bd09835d | 993 | |
be20aa9d CM |
994 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
995 | path->slots[0]++; | |
996 | continue; | |
997 | } | |
998 | ||
f321e491 | 999 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
be20aa9d | 1000 | struct btrfs_extent_ref); |
f321e491 YZ |
1001 | ref_generation = btrfs_ref_generation(leaf, ref_item); |
1002 | /* | |
31840ae1 | 1003 | * For (parent_gen > 0 && parent_gen > ref_generation): |
f321e491 | 1004 | * |
bcc63abb Y |
1005 | * we reach here through the oldest root, therefore |
1006 | * all other reference from same snapshot should have | |
f321e491 YZ |
1007 | * a larger generation. |
1008 | */ | |
1009 | if ((root_objectid != btrfs_ref_root(leaf, ref_item)) || | |
1010 | (parent_gen > 0 && parent_gen > ref_generation) || | |
1011 | (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID && | |
1012 | ref_objectid != btrfs_ref_objectid(leaf, ref_item))) { | |
31840ae1 | 1013 | *ref_count = 2; |
f321e491 | 1014 | break; |
a68d5933 | 1015 | } |
f321e491 YZ |
1016 | |
1017 | *ref_count = 1; | |
1018 | if (*min_generation > ref_generation) | |
1019 | *min_generation = ref_generation; | |
1020 | ||
be20aa9d CM |
1021 | path->slots[0]++; |
1022 | } | |
f321e491 YZ |
1023 | ret = 0; |
1024 | out: | |
1025 | mutex_unlock(&root->fs_info->alloc_mutex); | |
1026 | btrfs_free_path(path); | |
1027 | return ret; | |
1028 | } | |
1029 | ||
7ea394f1 YZ |
1030 | int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans, |
1031 | struct btrfs_root *root, | |
f321e491 YZ |
1032 | struct btrfs_key *key, u64 bytenr) |
1033 | { | |
f321e491 YZ |
1034 | struct btrfs_root *old_root; |
1035 | struct btrfs_path *path = NULL; | |
1036 | struct extent_buffer *eb; | |
1037 | struct btrfs_file_extent_item *item; | |
1038 | u64 ref_generation; | |
1039 | u64 min_generation; | |
1040 | u64 extent_start; | |
1041 | u32 ref_count; | |
1042 | int level; | |
1043 | int ret; | |
1044 | ||
7ea394f1 | 1045 | BUG_ON(trans == NULL); |
f321e491 YZ |
1046 | BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY); |
1047 | ret = get_reference_status(root, bytenr, 0, key->objectid, | |
1048 | &min_generation, &ref_count); | |
1049 | if (ret) | |
1050 | return ret; | |
1051 | ||
1052 | if (ref_count != 1) | |
1053 | return 1; | |
1054 | ||
f321e491 YZ |
1055 | old_root = root->dirty_root->root; |
1056 | ref_generation = old_root->root_key.offset; | |
1057 | ||
1058 | /* all references are created in running transaction */ | |
1059 | if (min_generation > ref_generation) { | |
1060 | ret = 0; | |
bbaf549e CM |
1061 | goto out; |
1062 | } | |
f321e491 YZ |
1063 | |
1064 | path = btrfs_alloc_path(); | |
1065 | if (!path) { | |
1066 | ret = -ENOMEM; | |
be20aa9d CM |
1067 | goto out; |
1068 | } | |
f321e491 YZ |
1069 | |
1070 | path->skip_locking = 1; | |
1071 | /* if no item found, the extent is referenced by other snapshot */ | |
1072 | ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0); | |
1073 | if (ret) | |
be20aa9d | 1074 | goto out; |
be20aa9d | 1075 | |
f321e491 YZ |
1076 | eb = path->nodes[0]; |
1077 | item = btrfs_item_ptr(eb, path->slots[0], | |
1078 | struct btrfs_file_extent_item); | |
1079 | if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG || | |
1080 | btrfs_file_extent_disk_bytenr(eb, item) != bytenr) { | |
1081 | ret = 1; | |
1082 | goto out; | |
1083 | } | |
1084 | ||
1085 | for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) { | |
1086 | if (level >= 0) { | |
1087 | eb = path->nodes[level]; | |
1088 | if (!eb) | |
1089 | continue; | |
1090 | extent_start = eb->start; | |
bcc63abb | 1091 | } else |
f321e491 YZ |
1092 | extent_start = bytenr; |
1093 | ||
1094 | ret = get_reference_status(root, extent_start, ref_generation, | |
1095 | 0, &min_generation, &ref_count); | |
1096 | if (ret) | |
1097 | goto out; | |
1098 | ||
1099 | if (ref_count != 1) { | |
1100 | ret = 1; | |
1101 | goto out; | |
1102 | } | |
1103 | if (level >= 0) | |
1104 | ref_generation = btrfs_header_generation(eb); | |
1105 | } | |
1106 | ret = 0; | |
be20aa9d | 1107 | out: |
f321e491 YZ |
1108 | if (path) |
1109 | btrfs_free_path(path); | |
f321e491 | 1110 | return ret; |
be20aa9d | 1111 | } |
c5739bba | 1112 | |
31840ae1 ZY |
1113 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
1114 | struct extent_buffer *buf, u32 nr_extents) | |
02217ed2 | 1115 | { |
5f39d397 CM |
1116 | u32 nritems; |
1117 | struct btrfs_key key; | |
6407bf6d | 1118 | struct btrfs_file_extent_item *fi; |
02217ed2 | 1119 | int i; |
db94535d | 1120 | int level; |
31840ae1 | 1121 | int ret = 0; |
a28ec197 | 1122 | |
3768f368 | 1123 | if (!root->ref_cows) |
a28ec197 | 1124 | return 0; |
5f39d397 | 1125 | |
db94535d | 1126 | level = btrfs_header_level(buf); |
5f39d397 | 1127 | nritems = btrfs_header_nritems(buf); |
4a096752 | 1128 | |
31840ae1 | 1129 | if (level == 0) { |
31153d81 YZ |
1130 | struct btrfs_leaf_ref *ref; |
1131 | struct btrfs_extent_info *info; | |
1132 | ||
31840ae1 | 1133 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
31153d81 | 1134 | if (!ref) { |
31840ae1 | 1135 | ret = -ENOMEM; |
31153d81 YZ |
1136 | goto out; |
1137 | } | |
1138 | ||
47ac14fa | 1139 | ref->root_gen = root->root_key.offset; |
31153d81 YZ |
1140 | ref->bytenr = buf->start; |
1141 | ref->owner = btrfs_header_owner(buf); | |
1142 | ref->generation = btrfs_header_generation(buf); | |
31840ae1 | 1143 | ref->nritems = nr_extents; |
31153d81 | 1144 | info = ref->extents; |
bcc63abb | 1145 | |
31840ae1 | 1146 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
31153d81 YZ |
1147 | u64 disk_bytenr; |
1148 | btrfs_item_key_to_cpu(buf, &key, i); | |
1149 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
1150 | continue; | |
1151 | fi = btrfs_item_ptr(buf, i, | |
1152 | struct btrfs_file_extent_item); | |
1153 | if (btrfs_file_extent_type(buf, fi) == | |
1154 | BTRFS_FILE_EXTENT_INLINE) | |
1155 | continue; | |
1156 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | |
1157 | if (disk_bytenr == 0) | |
1158 | continue; | |
1159 | ||
1160 | info->bytenr = disk_bytenr; | |
1161 | info->num_bytes = | |
1162 | btrfs_file_extent_disk_num_bytes(buf, fi); | |
1163 | info->objectid = key.objectid; | |
1164 | info->offset = key.offset; | |
1165 | info++; | |
1166 | } | |
1167 | ||
1168 | BUG_ON(!root->ref_tree); | |
1169 | ret = btrfs_add_leaf_ref(root, ref); | |
1170 | WARN_ON(ret); | |
bcc63abb | 1171 | btrfs_free_leaf_ref(root, ref); |
31153d81 YZ |
1172 | } |
1173 | out: | |
31840ae1 ZY |
1174 | return ret; |
1175 | } | |
1176 | ||
1177 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
1178 | struct extent_buffer *orig_buf, struct extent_buffer *buf, | |
1179 | u32 *nr_extents) | |
1180 | { | |
1181 | u64 bytenr; | |
1182 | u64 ref_root; | |
1183 | u64 orig_root; | |
1184 | u64 ref_generation; | |
1185 | u64 orig_generation; | |
1186 | u32 nritems; | |
1187 | u32 nr_file_extents = 0; | |
1188 | struct btrfs_key key; | |
1189 | struct btrfs_file_extent_item *fi; | |
1190 | int i; | |
1191 | int level; | |
1192 | int ret = 0; | |
1193 | int faili = 0; | |
1194 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, | |
1195 | u64, u64, u64, u64, u64, u64, u64, u64, u64); | |
1196 | ||
1197 | ref_root = btrfs_header_owner(buf); | |
1198 | ref_generation = btrfs_header_generation(buf); | |
1199 | orig_root = btrfs_header_owner(orig_buf); | |
1200 | orig_generation = btrfs_header_generation(orig_buf); | |
1201 | ||
1202 | nritems = btrfs_header_nritems(buf); | |
1203 | level = btrfs_header_level(buf); | |
1204 | ||
1205 | if (root->ref_cows) { | |
1206 | process_func = __btrfs_inc_extent_ref; | |
1207 | } else { | |
1208 | if (level == 0 && | |
1209 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
1210 | goto out; | |
1211 | if (level != 0 && | |
1212 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) | |
1213 | goto out; | |
1214 | process_func = __btrfs_update_extent_ref; | |
1215 | } | |
1216 | ||
1217 | for (i = 0; i < nritems; i++) { | |
1218 | cond_resched(); | |
db94535d | 1219 | if (level == 0) { |
5f39d397 CM |
1220 | btrfs_item_key_to_cpu(buf, &key, i); |
1221 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
54aa1f4d | 1222 | continue; |
5f39d397 | 1223 | fi = btrfs_item_ptr(buf, i, |
54aa1f4d | 1224 | struct btrfs_file_extent_item); |
5f39d397 | 1225 | if (btrfs_file_extent_type(buf, fi) == |
54aa1f4d CM |
1226 | BTRFS_FILE_EXTENT_INLINE) |
1227 | continue; | |
31840ae1 ZY |
1228 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
1229 | if (bytenr == 0) | |
54aa1f4d | 1230 | continue; |
31840ae1 ZY |
1231 | |
1232 | nr_file_extents++; | |
1233 | ||
1234 | maybe_lock_mutex(root); | |
1235 | ret = process_func(trans, root, bytenr, | |
1236 | orig_buf->start, buf->start, | |
1237 | orig_root, ref_root, | |
1238 | orig_generation, ref_generation, | |
1239 | key.objectid, key.offset); | |
1240 | maybe_unlock_mutex(root); | |
1241 | ||
1242 | if (ret) { | |
1243 | faili = i; | |
1244 | WARN_ON(1); | |
1245 | goto fail; | |
1246 | } | |
54aa1f4d | 1247 | } else { |
db94535d | 1248 | bytenr = btrfs_node_blockptr(buf, i); |
31840ae1 ZY |
1249 | maybe_lock_mutex(root); |
1250 | ret = process_func(trans, root, bytenr, | |
1251 | orig_buf->start, buf->start, | |
1252 | orig_root, ref_root, | |
1253 | orig_generation, ref_generation, | |
1254 | level - 1, 0); | |
1255 | maybe_unlock_mutex(root); | |
1256 | if (ret) { | |
1257 | faili = i; | |
1258 | WARN_ON(1); | |
1259 | goto fail; | |
1260 | } | |
54aa1f4d CM |
1261 | } |
1262 | } | |
31840ae1 ZY |
1263 | out: |
1264 | if (nr_extents) { | |
1265 | if (level == 0) | |
1266 | *nr_extents = nr_file_extents; | |
1267 | else | |
1268 | *nr_extents = nritems; | |
1269 | } | |
1270 | return 0; | |
1271 | fail: | |
1272 | WARN_ON(1); | |
54aa1f4d | 1273 | return ret; |
02217ed2 CM |
1274 | } |
1275 | ||
31840ae1 ZY |
1276 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
1277 | struct btrfs_root *root, struct extent_buffer *orig_buf, | |
1278 | struct extent_buffer *buf, int start_slot, int nr) | |
1279 | ||
1280 | { | |
1281 | u64 bytenr; | |
1282 | u64 ref_root; | |
1283 | u64 orig_root; | |
1284 | u64 ref_generation; | |
1285 | u64 orig_generation; | |
1286 | struct btrfs_key key; | |
1287 | struct btrfs_file_extent_item *fi; | |
1288 | int i; | |
1289 | int ret; | |
1290 | int slot; | |
1291 | int level; | |
1292 | ||
1293 | BUG_ON(start_slot < 0); | |
1294 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); | |
1295 | ||
1296 | ref_root = btrfs_header_owner(buf); | |
1297 | ref_generation = btrfs_header_generation(buf); | |
1298 | orig_root = btrfs_header_owner(orig_buf); | |
1299 | orig_generation = btrfs_header_generation(orig_buf); | |
1300 | level = btrfs_header_level(buf); | |
1301 | ||
1302 | if (!root->ref_cows) { | |
1303 | if (level == 0 && | |
1304 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) | |
1305 | return 0; | |
1306 | if (level != 0 && | |
1307 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) | |
1308 | return 0; | |
1309 | } | |
1310 | ||
1311 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { | |
1312 | cond_resched(); | |
1313 | if (level == 0) { | |
1314 | btrfs_item_key_to_cpu(buf, &key, slot); | |
1315 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
1316 | continue; | |
1317 | fi = btrfs_item_ptr(buf, slot, | |
1318 | struct btrfs_file_extent_item); | |
1319 | if (btrfs_file_extent_type(buf, fi) == | |
1320 | BTRFS_FILE_EXTENT_INLINE) | |
1321 | continue; | |
1322 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | |
1323 | if (bytenr == 0) | |
1324 | continue; | |
1325 | maybe_lock_mutex(root); | |
1326 | ret = __btrfs_update_extent_ref(trans, root, bytenr, | |
1327 | orig_buf->start, buf->start, | |
1328 | orig_root, ref_root, | |
1329 | orig_generation, ref_generation, | |
1330 | key.objectid, key.offset); | |
1331 | maybe_unlock_mutex(root); | |
1332 | if (ret) | |
1333 | goto fail; | |
1334 | } else { | |
1335 | bytenr = btrfs_node_blockptr(buf, slot); | |
1336 | maybe_lock_mutex(root); | |
1337 | ret = __btrfs_update_extent_ref(trans, root, bytenr, | |
1338 | orig_buf->start, buf->start, | |
1339 | orig_root, ref_root, | |
1340 | orig_generation, ref_generation, | |
1341 | level - 1, 0); | |
1342 | maybe_unlock_mutex(root); | |
1343 | if (ret) | |
1344 | goto fail; | |
1345 | } | |
1346 | } | |
1347 | return 0; | |
1348 | fail: | |
1349 | WARN_ON(1); | |
1350 | return -1; | |
1351 | } | |
1352 | ||
9078a3e1 CM |
1353 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
1354 | struct btrfs_root *root, | |
1355 | struct btrfs_path *path, | |
1356 | struct btrfs_block_group_cache *cache) | |
1357 | { | |
1358 | int ret; | |
1359 | int pending_ret; | |
1360 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
5f39d397 CM |
1361 | unsigned long bi; |
1362 | struct extent_buffer *leaf; | |
9078a3e1 | 1363 | |
9078a3e1 | 1364 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
54aa1f4d CM |
1365 | if (ret < 0) |
1366 | goto fail; | |
9078a3e1 | 1367 | BUG_ON(ret); |
5f39d397 CM |
1368 | |
1369 | leaf = path->nodes[0]; | |
1370 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
1371 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); | |
1372 | btrfs_mark_buffer_dirty(leaf); | |
9078a3e1 | 1373 | btrfs_release_path(extent_root, path); |
54aa1f4d | 1374 | fail: |
9078a3e1 CM |
1375 | finish_current_insert(trans, extent_root); |
1376 | pending_ret = del_pending_extents(trans, extent_root); | |
1377 | if (ret) | |
1378 | return ret; | |
1379 | if (pending_ret) | |
1380 | return pending_ret; | |
1381 | return 0; | |
1382 | ||
1383 | } | |
1384 | ||
96b5179d CM |
1385 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
1386 | struct btrfs_root *root) | |
9078a3e1 | 1387 | { |
0f9dd46c JB |
1388 | struct btrfs_block_group_cache *cache, *entry; |
1389 | struct rb_node *n; | |
9078a3e1 CM |
1390 | int err = 0; |
1391 | int werr = 0; | |
9078a3e1 | 1392 | struct btrfs_path *path; |
96b5179d | 1393 | u64 last = 0; |
9078a3e1 CM |
1394 | |
1395 | path = btrfs_alloc_path(); | |
1396 | if (!path) | |
1397 | return -ENOMEM; | |
1398 | ||
925baedd | 1399 | mutex_lock(&root->fs_info->alloc_mutex); |
9078a3e1 | 1400 | while(1) { |
0f9dd46c JB |
1401 | cache = NULL; |
1402 | spin_lock(&root->fs_info->block_group_cache_lock); | |
1403 | for (n = rb_first(&root->fs_info->block_group_cache_tree); | |
1404 | n; n = rb_next(n)) { | |
1405 | entry = rb_entry(n, struct btrfs_block_group_cache, | |
1406 | cache_node); | |
1407 | if (entry->dirty) { | |
1408 | cache = entry; | |
1409 | break; | |
1410 | } | |
1411 | } | |
1412 | spin_unlock(&root->fs_info->block_group_cache_lock); | |
54aa1f4d | 1413 | |
0f9dd46c | 1414 | if (!cache) |
96b5179d | 1415 | break; |
0f9dd46c JB |
1416 | |
1417 | last += cache->key.offset; | |
1418 | ||
96b5179d CM |
1419 | err = write_one_cache_group(trans, root, |
1420 | path, cache); | |
1421 | /* | |
1422 | * if we fail to write the cache group, we want | |
1423 | * to keep it marked dirty in hopes that a later | |
1424 | * write will work | |
1425 | */ | |
1426 | if (err) { | |
1427 | werr = err; | |
1428 | continue; | |
9078a3e1 | 1429 | } |
0f9dd46c JB |
1430 | |
1431 | cache->dirty = 0; | |
9078a3e1 CM |
1432 | } |
1433 | btrfs_free_path(path); | |
925baedd | 1434 | mutex_unlock(&root->fs_info->alloc_mutex); |
9078a3e1 CM |
1435 | return werr; |
1436 | } | |
1437 | ||
593060d7 CM |
1438 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
1439 | u64 total_bytes, u64 bytes_used, | |
1440 | struct btrfs_space_info **space_info) | |
1441 | { | |
1442 | struct btrfs_space_info *found; | |
1443 | ||
1444 | found = __find_space_info(info, flags); | |
1445 | if (found) { | |
1446 | found->total_bytes += total_bytes; | |
1447 | found->bytes_used += bytes_used; | |
8f18cf13 | 1448 | found->full = 0; |
593060d7 CM |
1449 | *space_info = found; |
1450 | return 0; | |
1451 | } | |
1452 | found = kmalloc(sizeof(*found), GFP_NOFS); | |
1453 | if (!found) | |
1454 | return -ENOMEM; | |
1455 | ||
1456 | list_add(&found->list, &info->space_info); | |
0f9dd46c JB |
1457 | INIT_LIST_HEAD(&found->block_groups); |
1458 | spin_lock_init(&found->lock); | |
593060d7 CM |
1459 | found->flags = flags; |
1460 | found->total_bytes = total_bytes; | |
1461 | found->bytes_used = bytes_used; | |
1462 | found->bytes_pinned = 0; | |
1463 | found->full = 0; | |
0ef3e66b | 1464 | found->force_alloc = 0; |
593060d7 CM |
1465 | *space_info = found; |
1466 | return 0; | |
1467 | } | |
1468 | ||
8790d502 CM |
1469 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
1470 | { | |
1471 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | | |
611f0e00 | 1472 | BTRFS_BLOCK_GROUP_RAID1 | |
321aecc6 | 1473 | BTRFS_BLOCK_GROUP_RAID10 | |
611f0e00 | 1474 | BTRFS_BLOCK_GROUP_DUP); |
8790d502 CM |
1475 | if (extra_flags) { |
1476 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
1477 | fs_info->avail_data_alloc_bits |= extra_flags; | |
1478 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
1479 | fs_info->avail_metadata_alloc_bits |= extra_flags; | |
1480 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
1481 | fs_info->avail_system_alloc_bits |= extra_flags; | |
1482 | } | |
1483 | } | |
593060d7 | 1484 | |
a061fc8d | 1485 | static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
ec44a35c | 1486 | { |
a061fc8d CM |
1487 | u64 num_devices = root->fs_info->fs_devices->num_devices; |
1488 | ||
1489 | if (num_devices == 1) | |
1490 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); | |
1491 | if (num_devices < 4) | |
1492 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; | |
1493 | ||
ec44a35c CM |
1494 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
1495 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
a061fc8d | 1496 | BTRFS_BLOCK_GROUP_RAID10))) { |
ec44a35c | 1497 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
a061fc8d | 1498 | } |
ec44a35c CM |
1499 | |
1500 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && | |
a061fc8d | 1501 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
ec44a35c | 1502 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
a061fc8d | 1503 | } |
ec44a35c CM |
1504 | |
1505 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && | |
1506 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | | |
1507 | (flags & BTRFS_BLOCK_GROUP_RAID10) | | |
1508 | (flags & BTRFS_BLOCK_GROUP_DUP))) | |
1509 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; | |
1510 | return flags; | |
1511 | } | |
1512 | ||
6324fbf3 CM |
1513 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
1514 | struct btrfs_root *extent_root, u64 alloc_bytes, | |
0ef3e66b | 1515 | u64 flags, int force) |
6324fbf3 CM |
1516 | { |
1517 | struct btrfs_space_info *space_info; | |
1518 | u64 thresh; | |
1519 | u64 start; | |
1520 | u64 num_bytes; | |
0f9dd46c | 1521 | int ret = 0; |
6324fbf3 | 1522 | |
a061fc8d | 1523 | flags = reduce_alloc_profile(extent_root, flags); |
ec44a35c | 1524 | |
6324fbf3 | 1525 | space_info = __find_space_info(extent_root->fs_info, flags); |
593060d7 CM |
1526 | if (!space_info) { |
1527 | ret = update_space_info(extent_root->fs_info, flags, | |
1528 | 0, 0, &space_info); | |
1529 | BUG_ON(ret); | |
1530 | } | |
6324fbf3 CM |
1531 | BUG_ON(!space_info); |
1532 | ||
0ef3e66b CM |
1533 | if (space_info->force_alloc) { |
1534 | force = 1; | |
1535 | space_info->force_alloc = 0; | |
1536 | } | |
6324fbf3 | 1537 | if (space_info->full) |
925baedd | 1538 | goto out; |
6324fbf3 | 1539 | |
8790d502 | 1540 | thresh = div_factor(space_info->total_bytes, 6); |
0ef3e66b CM |
1541 | if (!force && |
1542 | (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) < | |
6324fbf3 | 1543 | thresh) |
925baedd | 1544 | goto out; |
6324fbf3 | 1545 | |
925baedd | 1546 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
6324fbf3 CM |
1547 | ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags); |
1548 | if (ret == -ENOSPC) { | |
1549 | printk("space info full %Lu\n", flags); | |
1550 | space_info->full = 1; | |
a74a4b97 | 1551 | goto out_unlock; |
6324fbf3 | 1552 | } |
6324fbf3 CM |
1553 | BUG_ON(ret); |
1554 | ||
1555 | ret = btrfs_make_block_group(trans, extent_root, 0, flags, | |
e17cade2 | 1556 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes); |
6324fbf3 | 1557 | BUG_ON(ret); |
0f9dd46c | 1558 | |
a74a4b97 | 1559 | out_unlock: |
333db94c | 1560 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
a74a4b97 | 1561 | out: |
0f9dd46c | 1562 | return ret; |
6324fbf3 CM |
1563 | } |
1564 | ||
9078a3e1 CM |
1565 | static int update_block_group(struct btrfs_trans_handle *trans, |
1566 | struct btrfs_root *root, | |
db94535d | 1567 | u64 bytenr, u64 num_bytes, int alloc, |
0b86a832 | 1568 | int mark_free) |
9078a3e1 CM |
1569 | { |
1570 | struct btrfs_block_group_cache *cache; | |
1571 | struct btrfs_fs_info *info = root->fs_info; | |
db94535d | 1572 | u64 total = num_bytes; |
9078a3e1 | 1573 | u64 old_val; |
db94535d | 1574 | u64 byte_in_group; |
3e1ad54f | 1575 | |
7d9eb12c | 1576 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
9078a3e1 | 1577 | while(total) { |
db94535d | 1578 | cache = btrfs_lookup_block_group(info, bytenr); |
3e1ad54f | 1579 | if (!cache) { |
9078a3e1 | 1580 | return -1; |
cd1bc465 | 1581 | } |
db94535d CM |
1582 | byte_in_group = bytenr - cache->key.objectid; |
1583 | WARN_ON(byte_in_group > cache->key.offset); | |
9078a3e1 | 1584 | |
c286ac48 | 1585 | spin_lock(&cache->lock); |
0f9dd46c | 1586 | cache->dirty = 1; |
9078a3e1 | 1587 | old_val = btrfs_block_group_used(&cache->item); |
db94535d | 1588 | num_bytes = min(total, cache->key.offset - byte_in_group); |
cd1bc465 | 1589 | if (alloc) { |
db94535d | 1590 | old_val += num_bytes; |
6324fbf3 | 1591 | cache->space_info->bytes_used += num_bytes; |
c286ac48 CM |
1592 | btrfs_set_block_group_used(&cache->item, old_val); |
1593 | spin_unlock(&cache->lock); | |
cd1bc465 | 1594 | } else { |
db94535d | 1595 | old_val -= num_bytes; |
6324fbf3 | 1596 | cache->space_info->bytes_used -= num_bytes; |
c286ac48 CM |
1597 | btrfs_set_block_group_used(&cache->item, old_val); |
1598 | spin_unlock(&cache->lock); | |
f510cfec | 1599 | if (mark_free) { |
0f9dd46c JB |
1600 | int ret; |
1601 | ret = btrfs_add_free_space(cache, bytenr, | |
1602 | num_bytes); | |
1603 | if (ret) | |
1604 | return -1; | |
e37c9e69 | 1605 | } |
cd1bc465 | 1606 | } |
db94535d CM |
1607 | total -= num_bytes; |
1608 | bytenr += num_bytes; | |
9078a3e1 CM |
1609 | } |
1610 | return 0; | |
1611 | } | |
6324fbf3 | 1612 | |
a061fc8d CM |
1613 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
1614 | { | |
0f9dd46c JB |
1615 | struct btrfs_block_group_cache *cache; |
1616 | ||
1617 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); | |
1618 | if (!cache) | |
a061fc8d | 1619 | return 0; |
0f9dd46c JB |
1620 | |
1621 | return cache->key.objectid; | |
a061fc8d CM |
1622 | } |
1623 | ||
1624 | ||
e02119d5 | 1625 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
324ae4df Y |
1626 | u64 bytenr, u64 num, int pin) |
1627 | { | |
1628 | u64 len; | |
1629 | struct btrfs_block_group_cache *cache; | |
1630 | struct btrfs_fs_info *fs_info = root->fs_info; | |
1631 | ||
7d9eb12c | 1632 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
324ae4df Y |
1633 | if (pin) { |
1634 | set_extent_dirty(&fs_info->pinned_extents, | |
1635 | bytenr, bytenr + num - 1, GFP_NOFS); | |
1636 | } else { | |
1637 | clear_extent_dirty(&fs_info->pinned_extents, | |
1638 | bytenr, bytenr + num - 1, GFP_NOFS); | |
1639 | } | |
1640 | while (num > 0) { | |
1641 | cache = btrfs_lookup_block_group(fs_info, bytenr); | |
a061fc8d CM |
1642 | if (!cache) { |
1643 | u64 first = first_logical_byte(root, bytenr); | |
1644 | WARN_ON(first < bytenr); | |
1645 | len = min(first - bytenr, num); | |
1646 | } else { | |
1647 | len = min(num, cache->key.offset - | |
1648 | (bytenr - cache->key.objectid)); | |
1649 | } | |
324ae4df | 1650 | if (pin) { |
a061fc8d | 1651 | if (cache) { |
c286ac48 | 1652 | spin_lock(&cache->lock); |
a061fc8d CM |
1653 | cache->pinned += len; |
1654 | cache->space_info->bytes_pinned += len; | |
c286ac48 | 1655 | spin_unlock(&cache->lock); |
a061fc8d | 1656 | } |
324ae4df Y |
1657 | fs_info->total_pinned += len; |
1658 | } else { | |
a061fc8d | 1659 | if (cache) { |
c286ac48 | 1660 | spin_lock(&cache->lock); |
a061fc8d CM |
1661 | cache->pinned -= len; |
1662 | cache->space_info->bytes_pinned -= len; | |
c286ac48 | 1663 | spin_unlock(&cache->lock); |
a061fc8d | 1664 | } |
324ae4df Y |
1665 | fs_info->total_pinned -= len; |
1666 | } | |
1667 | bytenr += len; | |
1668 | num -= len; | |
1669 | } | |
1670 | return 0; | |
1671 | } | |
9078a3e1 | 1672 | |
d1310b2e | 1673 | int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy) |
ccd467d6 | 1674 | { |
ccd467d6 | 1675 | u64 last = 0; |
1a5bc167 CM |
1676 | u64 start; |
1677 | u64 end; | |
d1310b2e | 1678 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
ccd467d6 | 1679 | int ret; |
ccd467d6 CM |
1680 | |
1681 | while(1) { | |
1a5bc167 CM |
1682 | ret = find_first_extent_bit(pinned_extents, last, |
1683 | &start, &end, EXTENT_DIRTY); | |
1684 | if (ret) | |
ccd467d6 | 1685 | break; |
1a5bc167 CM |
1686 | set_extent_dirty(copy, start, end, GFP_NOFS); |
1687 | last = end + 1; | |
ccd467d6 CM |
1688 | } |
1689 | return 0; | |
1690 | } | |
1691 | ||
1692 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |
1693 | struct btrfs_root *root, | |
d1310b2e | 1694 | struct extent_io_tree *unpin) |
a28ec197 | 1695 | { |
1a5bc167 CM |
1696 | u64 start; |
1697 | u64 end; | |
a28ec197 | 1698 | int ret; |
0f9dd46c | 1699 | struct btrfs_block_group_cache *cache; |
a28ec197 | 1700 | |
925baedd | 1701 | mutex_lock(&root->fs_info->alloc_mutex); |
a28ec197 | 1702 | while(1) { |
1a5bc167 CM |
1703 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
1704 | EXTENT_DIRTY); | |
1705 | if (ret) | |
a28ec197 | 1706 | break; |
e02119d5 | 1707 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
1a5bc167 | 1708 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
0f9dd46c JB |
1709 | cache = btrfs_lookup_block_group(root->fs_info, start); |
1710 | if (cache->cached) | |
1711 | btrfs_add_free_space(cache, start, end - start + 1); | |
c286ac48 CM |
1712 | if (need_resched()) { |
1713 | mutex_unlock(&root->fs_info->alloc_mutex); | |
1714 | cond_resched(); | |
1715 | mutex_lock(&root->fs_info->alloc_mutex); | |
1716 | } | |
a28ec197 | 1717 | } |
925baedd | 1718 | mutex_unlock(&root->fs_info->alloc_mutex); |
a28ec197 CM |
1719 | return 0; |
1720 | } | |
1721 | ||
98ed5174 CM |
1722 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
1723 | struct btrfs_root *extent_root) | |
037e6390 | 1724 | { |
7bb86316 CM |
1725 | u64 start; |
1726 | u64 end; | |
31840ae1 | 1727 | u64 priv; |
7bb86316 CM |
1728 | struct btrfs_fs_info *info = extent_root->fs_info; |
1729 | struct btrfs_path *path; | |
31840ae1 ZY |
1730 | struct btrfs_extent_ref *ref; |
1731 | struct pending_extent_op *extent_op; | |
1732 | struct btrfs_key key; | |
234b63a0 | 1733 | struct btrfs_extent_item extent_item; |
037e6390 | 1734 | int ret; |
1a5bc167 | 1735 | int err = 0; |
037e6390 | 1736 | |
7d9eb12c | 1737 | WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex)); |
5f39d397 | 1738 | btrfs_set_stack_extent_refs(&extent_item, 1); |
7bb86316 | 1739 | path = btrfs_alloc_path(); |
037e6390 | 1740 | |
26b8003f | 1741 | while(1) { |
1a5bc167 CM |
1742 | ret = find_first_extent_bit(&info->extent_ins, 0, &start, |
1743 | &end, EXTENT_LOCKED); | |
1744 | if (ret) | |
26b8003f CM |
1745 | break; |
1746 | ||
31840ae1 ZY |
1747 | ret = get_state_private(&info->extent_ins, start, &priv); |
1748 | BUG_ON(ret); | |
1749 | extent_op = (struct pending_extent_op *)(unsigned long)priv; | |
1750 | ||
1751 | if (extent_op->type == PENDING_EXTENT_INSERT) { | |
1752 | key.objectid = start; | |
1753 | key.offset = end + 1 - start; | |
1754 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
1755 | err = btrfs_insert_item(trans, extent_root, &key, | |
1a5bc167 | 1756 | &extent_item, sizeof(extent_item)); |
31840ae1 | 1757 | BUG_ON(err); |
c286ac48 | 1758 | |
31840ae1 ZY |
1759 | clear_extent_bits(&info->extent_ins, start, end, |
1760 | EXTENT_LOCKED, GFP_NOFS); | |
c286ac48 | 1761 | |
31840ae1 ZY |
1762 | err = insert_extent_backref(trans, extent_root, path, |
1763 | start, extent_op->parent, | |
1764 | extent_root->root_key.objectid, | |
1765 | extent_op->generation, | |
1766 | extent_op->level, 0); | |
1767 | BUG_ON(err); | |
1768 | } else if (extent_op->type == PENDING_BACKREF_UPDATE) { | |
1769 | err = lookup_extent_backref(trans, extent_root, path, | |
1770 | start, extent_op->orig_parent, | |
1771 | extent_root->root_key.objectid, | |
1772 | extent_op->orig_generation, 0); | |
1773 | BUG_ON(err); | |
c286ac48 | 1774 | |
31840ae1 ZY |
1775 | clear_extent_bits(&info->extent_ins, start, end, |
1776 | EXTENT_LOCKED, GFP_NOFS); | |
1777 | ||
1778 | key.objectid = start; | |
1779 | key.offset = extent_op->parent; | |
1780 | key.type = BTRFS_EXTENT_REF_KEY; | |
1781 | err = btrfs_set_item_key_safe(trans, extent_root, path, | |
1782 | &key); | |
1783 | BUG_ON(err); | |
1784 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
1785 | struct btrfs_extent_ref); | |
1786 | btrfs_set_ref_generation(path->nodes[0], ref, | |
1787 | extent_op->generation); | |
1788 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
1789 | btrfs_release_path(extent_root, path); | |
d8d5f3e1 | 1790 | } else { |
31840ae1 | 1791 | BUG_ON(1); |
d8d5f3e1 | 1792 | } |
31840ae1 ZY |
1793 | kfree(extent_op); |
1794 | ||
c286ac48 CM |
1795 | if (need_resched()) { |
1796 | mutex_unlock(&extent_root->fs_info->alloc_mutex); | |
1797 | cond_resched(); | |
1798 | mutex_lock(&extent_root->fs_info->alloc_mutex); | |
1799 | } | |
037e6390 | 1800 | } |
7bb86316 | 1801 | btrfs_free_path(path); |
037e6390 CM |
1802 | return 0; |
1803 | } | |
1804 | ||
31840ae1 ZY |
1805 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
1806 | struct btrfs_root *root, | |
1807 | u64 bytenr, u64 num_bytes, int is_data) | |
e20d96d6 | 1808 | { |
1a5bc167 | 1809 | int err = 0; |
31840ae1 | 1810 | struct extent_buffer *buf; |
8ef97622 | 1811 | |
7d9eb12c | 1812 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
31840ae1 ZY |
1813 | if (is_data) |
1814 | goto pinit; | |
1815 | ||
1816 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); | |
1817 | if (!buf) | |
1818 | goto pinit; | |
1819 | ||
1820 | /* we can reuse a block if it hasn't been written | |
1821 | * and it is from this transaction. We can't | |
1822 | * reuse anything from the tree log root because | |
1823 | * it has tiny sub-transactions. | |
1824 | */ | |
1825 | if (btrfs_buffer_uptodate(buf, 0) && | |
1826 | btrfs_try_tree_lock(buf)) { | |
1827 | u64 header_owner = btrfs_header_owner(buf); | |
1828 | u64 header_transid = btrfs_header_generation(buf); | |
1829 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && | |
1830 | header_transid == trans->transid && | |
1831 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { | |
1832 | clean_tree_block(NULL, root, buf); | |
1833 | btrfs_tree_unlock(buf); | |
5f39d397 | 1834 | free_extent_buffer(buf); |
31840ae1 | 1835 | return 1; |
8ef97622 | 1836 | } |
31840ae1 | 1837 | btrfs_tree_unlock(buf); |
f4b9aa8d | 1838 | } |
31840ae1 ZY |
1839 | free_extent_buffer(buf); |
1840 | pinit: | |
1841 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); | |
1842 | ||
be744175 | 1843 | BUG_ON(err < 0); |
e20d96d6 CM |
1844 | return 0; |
1845 | } | |
1846 | ||
fec577fb | 1847 | /* |
a28ec197 | 1848 | * remove an extent from the root, returns 0 on success |
fec577fb | 1849 | */ |
31840ae1 ZY |
1850 | static int __free_extent(struct btrfs_trans_handle *trans, |
1851 | struct btrfs_root *root, | |
1852 | u64 bytenr, u64 num_bytes, u64 parent, | |
7bb86316 | 1853 | u64 root_objectid, u64 ref_generation, |
31840ae1 ZY |
1854 | u64 owner_objectid, u64 owner_offset, |
1855 | int pin, int mark_free) | |
a28ec197 | 1856 | { |
5caf2a00 | 1857 | struct btrfs_path *path; |
e2fa7227 | 1858 | struct btrfs_key key; |
1261ec42 CM |
1859 | struct btrfs_fs_info *info = root->fs_info; |
1860 | struct btrfs_root *extent_root = info->extent_root; | |
5f39d397 | 1861 | struct extent_buffer *leaf; |
a28ec197 | 1862 | int ret; |
952fccac CM |
1863 | int extent_slot = 0; |
1864 | int found_extent = 0; | |
1865 | int num_to_del = 1; | |
234b63a0 | 1866 | struct btrfs_extent_item *ei; |
cf27e1ee | 1867 | u32 refs; |
037e6390 | 1868 | |
7d9eb12c | 1869 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
db94535d | 1870 | key.objectid = bytenr; |
62e2749e | 1871 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
db94535d | 1872 | key.offset = num_bytes; |
5caf2a00 | 1873 | path = btrfs_alloc_path(); |
54aa1f4d CM |
1874 | if (!path) |
1875 | return -ENOMEM; | |
5f26f772 | 1876 | |
3c12ac72 | 1877 | path->reada = 1; |
31840ae1 ZY |
1878 | ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent, |
1879 | root_objectid, ref_generation, 1); | |
7bb86316 | 1880 | if (ret == 0) { |
952fccac CM |
1881 | struct btrfs_key found_key; |
1882 | extent_slot = path->slots[0]; | |
1883 | while(extent_slot > 0) { | |
1884 | extent_slot--; | |
1885 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1886 | extent_slot); | |
1887 | if (found_key.objectid != bytenr) | |
1888 | break; | |
1889 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && | |
1890 | found_key.offset == num_bytes) { | |
1891 | found_extent = 1; | |
1892 | break; | |
1893 | } | |
1894 | if (path->slots[0] - extent_slot > 5) | |
1895 | break; | |
1896 | } | |
31840ae1 ZY |
1897 | if (!found_extent) { |
1898 | ret = remove_extent_backref(trans, extent_root, path); | |
1899 | BUG_ON(ret); | |
1900 | btrfs_release_path(extent_root, path); | |
1901 | ret = btrfs_search_slot(trans, extent_root, | |
1902 | &key, path, -1, 1); | |
1903 | BUG_ON(ret); | |
1904 | extent_slot = path->slots[0]; | |
1905 | } | |
7bb86316 CM |
1906 | } else { |
1907 | btrfs_print_leaf(extent_root, path->nodes[0]); | |
1908 | WARN_ON(1); | |
1909 | printk("Unable to find ref byte nr %Lu root %Lu " | |
1910 | " gen %Lu owner %Lu offset %Lu\n", bytenr, | |
1911 | root_objectid, ref_generation, owner_objectid, | |
1912 | owner_offset); | |
1913 | } | |
5f39d397 CM |
1914 | |
1915 | leaf = path->nodes[0]; | |
952fccac | 1916 | ei = btrfs_item_ptr(leaf, extent_slot, |
123abc88 | 1917 | struct btrfs_extent_item); |
5f39d397 CM |
1918 | refs = btrfs_extent_refs(leaf, ei); |
1919 | BUG_ON(refs == 0); | |
1920 | refs -= 1; | |
1921 | btrfs_set_extent_refs(leaf, ei, refs); | |
952fccac | 1922 | |
5f39d397 CM |
1923 | btrfs_mark_buffer_dirty(leaf); |
1924 | ||
952fccac | 1925 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
31840ae1 ZY |
1926 | struct btrfs_extent_ref *ref; |
1927 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
1928 | struct btrfs_extent_ref); | |
1929 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); | |
952fccac CM |
1930 | /* if the back ref and the extent are next to each other |
1931 | * they get deleted below in one shot | |
1932 | */ | |
1933 | path->slots[0] = extent_slot; | |
1934 | num_to_del = 2; | |
1935 | } else if (found_extent) { | |
1936 | /* otherwise delete the extent back ref */ | |
31840ae1 | 1937 | ret = remove_extent_backref(trans, extent_root, path); |
952fccac CM |
1938 | BUG_ON(ret); |
1939 | /* if refs are 0, we need to setup the path for deletion */ | |
1940 | if (refs == 0) { | |
1941 | btrfs_release_path(extent_root, path); | |
1942 | ret = btrfs_search_slot(trans, extent_root, &key, path, | |
1943 | -1, 1); | |
952fccac CM |
1944 | BUG_ON(ret); |
1945 | } | |
1946 | } | |
1947 | ||
cf27e1ee | 1948 | if (refs == 0) { |
db94535d CM |
1949 | u64 super_used; |
1950 | u64 root_used; | |
21af804c DW |
1951 | #ifdef BIO_RW_DISCARD |
1952 | u64 map_length = num_bytes; | |
1953 | struct btrfs_multi_bio *multi = NULL; | |
1954 | #endif | |
78fae27e CM |
1955 | |
1956 | if (pin) { | |
31840ae1 ZY |
1957 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, |
1958 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); | |
c549228f Y |
1959 | if (ret > 0) |
1960 | mark_free = 1; | |
1961 | BUG_ON(ret < 0); | |
78fae27e CM |
1962 | } |
1963 | ||
58176a96 | 1964 | /* block accounting for super block */ |
a2135011 | 1965 | spin_lock_irq(&info->delalloc_lock); |
db94535d CM |
1966 | super_used = btrfs_super_bytes_used(&info->super_copy); |
1967 | btrfs_set_super_bytes_used(&info->super_copy, | |
1968 | super_used - num_bytes); | |
a2135011 | 1969 | spin_unlock_irq(&info->delalloc_lock); |
58176a96 JB |
1970 | |
1971 | /* block accounting for root item */ | |
db94535d | 1972 | root_used = btrfs_root_used(&root->root_item); |
5f39d397 | 1973 | btrfs_set_root_used(&root->root_item, |
db94535d | 1974 | root_used - num_bytes); |
952fccac CM |
1975 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
1976 | num_to_del); | |
31840ae1 | 1977 | BUG_ON(ret); |
db94535d | 1978 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
0b86a832 | 1979 | mark_free); |
9078a3e1 | 1980 | BUG_ON(ret); |
21af804c DW |
1981 | |
1982 | #ifdef BIO_RW_DISCARD | |
1983 | /* Tell the block device(s) that the sectors can be discarded */ | |
1984 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, | |
1985 | bytenr, &map_length, &multi, 0); | |
1986 | if (!ret) { | |
1987 | struct btrfs_bio_stripe *stripe = multi->stripes; | |
1988 | int i; | |
1989 | ||
1990 | if (map_length > num_bytes) | |
1991 | map_length = num_bytes; | |
1992 | ||
1993 | for (i = 0; i < multi->num_stripes; i++, stripe++) { | |
1994 | blkdev_issue_discard(stripe->dev->bdev, | |
1995 | stripe->physical >> 9, | |
1996 | map_length >> 9); | |
1997 | } | |
1998 | kfree(multi); | |
1999 | } | |
2000 | #endif | |
a28ec197 | 2001 | } |
5caf2a00 | 2002 | btrfs_free_path(path); |
e089f05c | 2003 | finish_current_insert(trans, extent_root); |
a28ec197 CM |
2004 | return ret; |
2005 | } | |
2006 | ||
a28ec197 CM |
2007 | /* |
2008 | * find all the blocks marked as pending in the radix tree and remove | |
2009 | * them from the extent map | |
2010 | */ | |
e089f05c CM |
2011 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
2012 | btrfs_root *extent_root) | |
a28ec197 CM |
2013 | { |
2014 | int ret; | |
e20d96d6 | 2015 | int err = 0; |
31840ae1 | 2016 | int mark_free = 0; |
1a5bc167 CM |
2017 | u64 start; |
2018 | u64 end; | |
31840ae1 | 2019 | u64 priv; |
d1310b2e | 2020 | struct extent_io_tree *pending_del; |
31840ae1 ZY |
2021 | struct extent_io_tree *extent_ins; |
2022 | struct pending_extent_op *extent_op; | |
8ef97622 | 2023 | |
7d9eb12c | 2024 | WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex)); |
31840ae1 | 2025 | extent_ins = &extent_root->fs_info->extent_ins; |
1a5bc167 | 2026 | pending_del = &extent_root->fs_info->pending_del; |
a28ec197 CM |
2027 | |
2028 | while(1) { | |
1a5bc167 CM |
2029 | ret = find_first_extent_bit(pending_del, 0, &start, &end, |
2030 | EXTENT_LOCKED); | |
2031 | if (ret) | |
a28ec197 | 2032 | break; |
31840ae1 ZY |
2033 | |
2034 | ret = get_state_private(pending_del, start, &priv); | |
2035 | BUG_ON(ret); | |
2036 | extent_op = (struct pending_extent_op *)(unsigned long)priv; | |
2037 | ||
1a5bc167 CM |
2038 | clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, |
2039 | GFP_NOFS); | |
31840ae1 ZY |
2040 | |
2041 | ret = pin_down_bytes(trans, extent_root, start, | |
2042 | end + 1 - start, 0); | |
2043 | mark_free = ret > 0; | |
2044 | if (!test_range_bit(extent_ins, start, end, | |
2045 | EXTENT_LOCKED, 0)) { | |
2046 | free_extent: | |
c286ac48 | 2047 | ret = __free_extent(trans, extent_root, |
31840ae1 ZY |
2048 | start, end + 1 - start, |
2049 | extent_op->orig_parent, | |
2050 | extent_root->root_key.objectid, | |
2051 | extent_op->orig_generation, | |
2052 | extent_op->level, 0, 0, mark_free); | |
2053 | kfree(extent_op); | |
c286ac48 | 2054 | } else { |
31840ae1 ZY |
2055 | kfree(extent_op); |
2056 | ret = get_state_private(extent_ins, start, &priv); | |
2057 | BUG_ON(ret); | |
2058 | extent_op = (struct pending_extent_op *) | |
2059 | (unsigned long)priv; | |
2060 | ||
2061 | clear_extent_bits(extent_ins, start, end, | |
2062 | EXTENT_LOCKED, GFP_NOFS); | |
2063 | ||
2064 | if (extent_op->type == PENDING_BACKREF_UPDATE) | |
2065 | goto free_extent; | |
2066 | ||
2067 | ret = update_block_group(trans, extent_root, start, | |
2068 | end + 1 - start, 0, mark_free); | |
2069 | BUG_ON(ret); | |
2070 | kfree(extent_op); | |
c286ac48 | 2071 | } |
1a5bc167 CM |
2072 | if (ret) |
2073 | err = ret; | |
c286ac48 CM |
2074 | |
2075 | if (need_resched()) { | |
2076 | mutex_unlock(&extent_root->fs_info->alloc_mutex); | |
2077 | cond_resched(); | |
2078 | mutex_lock(&extent_root->fs_info->alloc_mutex); | |
2079 | } | |
fec577fb | 2080 | } |
e20d96d6 | 2081 | return err; |
fec577fb CM |
2082 | } |
2083 | ||
2084 | /* | |
2085 | * remove an extent from the root, returns 0 on success | |
2086 | */ | |
925baedd | 2087 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
2088 | struct btrfs_root *root, |
2089 | u64 bytenr, u64 num_bytes, u64 parent, | |
2090 | u64 root_objectid, u64 ref_generation, | |
2091 | u64 owner_objectid, u64 owner_offset, int pin) | |
fec577fb | 2092 | { |
9f5fae2f | 2093 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
fec577fb CM |
2094 | int pending_ret; |
2095 | int ret; | |
a28ec197 | 2096 | |
db94535d | 2097 | WARN_ON(num_bytes < root->sectorsize); |
fec577fb | 2098 | if (root == extent_root) { |
31840ae1 ZY |
2099 | struct pending_extent_op *extent_op; |
2100 | ||
2101 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | |
2102 | BUG_ON(!extent_op); | |
2103 | ||
2104 | extent_op->type = PENDING_EXTENT_DELETE; | |
2105 | extent_op->bytenr = bytenr; | |
2106 | extent_op->num_bytes = num_bytes; | |
2107 | extent_op->parent = parent; | |
2108 | extent_op->orig_parent = parent; | |
2109 | extent_op->generation = ref_generation; | |
2110 | extent_op->orig_generation = ref_generation; | |
2111 | extent_op->level = (int)owner_objectid; | |
2112 | ||
2113 | set_extent_bits(&root->fs_info->pending_del, | |
2114 | bytenr, bytenr + num_bytes - 1, | |
2115 | EXTENT_LOCKED, GFP_NOFS); | |
2116 | set_state_private(&root->fs_info->pending_del, | |
2117 | bytenr, (unsigned long)extent_op); | |
fec577fb CM |
2118 | return 0; |
2119 | } | |
4bef0848 | 2120 | /* if metadata always pin */ |
d00aff00 CM |
2121 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
2122 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { | |
0f9dd46c JB |
2123 | struct btrfs_block_group_cache *cache; |
2124 | ||
d00aff00 | 2125 | /* btrfs_free_reserved_extent */ |
0f9dd46c JB |
2126 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
2127 | BUG_ON(!cache); | |
2128 | btrfs_add_free_space(cache, bytenr, num_bytes); | |
d00aff00 CM |
2129 | return 0; |
2130 | } | |
4bef0848 | 2131 | pin = 1; |
d00aff00 | 2132 | } |
4bef0848 CM |
2133 | |
2134 | /* if data pin when any transaction has committed this */ | |
2135 | if (ref_generation != trans->transid) | |
2136 | pin = 1; | |
2137 | ||
31840ae1 ZY |
2138 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, |
2139 | root_objectid, ref_generation, owner_objectid, | |
2140 | owner_offset, pin, pin == 0); | |
ee6e6504 CM |
2141 | |
2142 | finish_current_insert(trans, root->fs_info->extent_root); | |
e20d96d6 | 2143 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
fec577fb CM |
2144 | return ret ? ret : pending_ret; |
2145 | } | |
2146 | ||
925baedd | 2147 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
2148 | struct btrfs_root *root, |
2149 | u64 bytenr, u64 num_bytes, u64 parent, | |
2150 | u64 root_objectid, u64 ref_generation, | |
2151 | u64 owner_objectid, u64 owner_offset, int pin) | |
925baedd CM |
2152 | { |
2153 | int ret; | |
2154 | ||
2155 | maybe_lock_mutex(root); | |
31840ae1 | 2156 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, |
925baedd CM |
2157 | root_objectid, ref_generation, |
2158 | owner_objectid, owner_offset, pin); | |
2159 | maybe_unlock_mutex(root); | |
2160 | return ret; | |
2161 | } | |
2162 | ||
87ee04eb CM |
2163 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
2164 | { | |
2165 | u64 mask = ((u64)root->stripesize - 1); | |
2166 | u64 ret = (val + mask) & ~mask; | |
2167 | return ret; | |
2168 | } | |
2169 | ||
fec577fb CM |
2170 | /* |
2171 | * walks the btree of allocated extents and find a hole of a given size. | |
2172 | * The key ins is changed to record the hole: | |
2173 | * ins->objectid == block start | |
62e2749e | 2174 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
fec577fb CM |
2175 | * ins->offset == number of blocks |
2176 | * Any available blocks before search_start are skipped. | |
2177 | */ | |
98ed5174 CM |
2178 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
2179 | struct btrfs_root *orig_root, | |
2180 | u64 num_bytes, u64 empty_size, | |
2181 | u64 search_start, u64 search_end, | |
2182 | u64 hint_byte, struct btrfs_key *ins, | |
2183 | u64 exclude_start, u64 exclude_nr, | |
2184 | int data) | |
fec577fb | 2185 | { |
87ee04eb | 2186 | int ret; |
a061fc8d | 2187 | u64 orig_search_start; |
9f5fae2f | 2188 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
f2458e1d | 2189 | struct btrfs_fs_info *info = root->fs_info; |
db94535d | 2190 | u64 total_needed = num_bytes; |
239b14b3 | 2191 | u64 *last_ptr = NULL; |
be08c1b9 | 2192 | struct btrfs_block_group_cache *block_group; |
0ef3e66b | 2193 | int chunk_alloc_done = 0; |
239b14b3 | 2194 | int empty_cluster = 2 * 1024 * 1024; |
0ef3e66b | 2195 | int allowed_chunk_alloc = 0; |
fec577fb | 2196 | |
db94535d | 2197 | WARN_ON(num_bytes < root->sectorsize); |
b1a4d965 CM |
2198 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
2199 | ||
0ef3e66b CM |
2200 | if (orig_root->ref_cows || empty_size) |
2201 | allowed_chunk_alloc = 1; | |
2202 | ||
239b14b3 CM |
2203 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
2204 | last_ptr = &root->fs_info->last_alloc; | |
8790d502 | 2205 | empty_cluster = 256 * 1024; |
239b14b3 CM |
2206 | } |
2207 | ||
0f9dd46c | 2208 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
239b14b3 | 2209 | last_ptr = &root->fs_info->last_data_alloc; |
0f9dd46c | 2210 | |
e02119d5 CM |
2211 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
2212 | last_ptr = &root->fs_info->last_log_alloc; | |
2213 | if (!last_ptr == 0 && root->fs_info->last_alloc) { | |
2214 | *last_ptr = root->fs_info->last_alloc + empty_cluster; | |
2215 | } | |
2216 | } | |
239b14b3 CM |
2217 | |
2218 | if (last_ptr) { | |
2219 | if (*last_ptr) | |
2220 | hint_byte = *last_ptr; | |
0f9dd46c | 2221 | else |
239b14b3 | 2222 | empty_size += empty_cluster; |
239b14b3 CM |
2223 | } |
2224 | ||
a061fc8d CM |
2225 | search_start = max(search_start, first_logical_byte(root, 0)); |
2226 | orig_search_start = search_start; | |
2227 | ||
7f93bf8d CM |
2228 | if (search_end == (u64)-1) |
2229 | search_end = btrfs_super_total_bytes(&info->super_copy); | |
0b86a832 | 2230 | |
239b14b3 | 2231 | search_start = max(search_start, hint_byte); |
6702ed49 | 2232 | total_needed += empty_size; |
0b86a832 | 2233 | |
0f9dd46c JB |
2234 | new_group: |
2235 | block_group = btrfs_lookup_block_group(info, search_start); | |
2236 | ||
2237 | /* | |
2238 | * Ok this looks a little tricky, buts its really simple. First if we | |
2239 | * didn't find a block group obviously we want to start over. | |
2240 | * Secondly, if the block group we found does not match the type we | |
2241 | * need, and we have a last_ptr and its not 0, chances are the last | |
2242 | * allocation we made was at the end of the block group, so lets go | |
2243 | * ahead and skip the looking through the rest of the block groups and | |
2244 | * start at the beginning. This helps with metadata allocations, | |
2245 | * since you are likely to have a bunch of data block groups to search | |
2246 | * through first before you realize that you need to start over, so go | |
2247 | * ahead and start over and save the time. | |
2248 | */ | |
2249 | if (!block_group || (!block_group_bits(block_group, data) && | |
2250 | last_ptr && *last_ptr)) { | |
2251 | if (search_start != orig_search_start) { | |
2252 | if (last_ptr && *last_ptr) | |
2253 | *last_ptr = 0; | |
2254 | search_start = orig_search_start; | |
2255 | goto new_group; | |
2256 | } else if (!chunk_alloc_done && allowed_chunk_alloc) { | |
2257 | ret = do_chunk_alloc(trans, root, | |
2258 | num_bytes + 2 * 1024 * 1024, | |
2259 | data, 1); | |
2260 | if (ret < 0) { | |
2261 | struct btrfs_space_info *info; | |
2262 | ||
2263 | info = __find_space_info(root->fs_info, data); | |
2264 | goto error; | |
2265 | } | |
2266 | BUG_ON(ret); | |
2267 | chunk_alloc_done = 1; | |
2268 | search_start = orig_search_start; | |
2269 | goto new_group; | |
2270 | } else { | |
2271 | ret = -ENOSPC; | |
2272 | goto error; | |
0ef3e66b | 2273 | } |
239b14b3 | 2274 | } |
e19caa5f | 2275 | |
0f9dd46c JB |
2276 | /* |
2277 | * this is going to seach through all of the existing block groups it | |
2278 | * can find, so if we don't find something we need to see if we can | |
2279 | * allocate what we need. | |
2280 | */ | |
2281 | ret = find_free_space(root, &block_group, &search_start, | |
2282 | total_needed, data); | |
2283 | if (ret == -ENOSPC) { | |
2284 | /* | |
2285 | * instead of allocating, start at the original search start | |
2286 | * and see if there is something to be found, if not then we | |
2287 | * allocate | |
2288 | */ | |
2289 | if (search_start != orig_search_start) { | |
2290 | if (last_ptr && *last_ptr) { | |
2291 | *last_ptr = 0; | |
2292 | total_needed += empty_cluster; | |
2293 | } | |
2294 | search_start = orig_search_start; | |
2295 | goto new_group; | |
239b14b3 | 2296 | } |
0f9dd46c JB |
2297 | |
2298 | /* | |
2299 | * we've already allocated, we're pretty screwed | |
2300 | */ | |
2301 | if (chunk_alloc_done) { | |
239b14b3 | 2302 | goto error; |
0f9dd46c JB |
2303 | } else if (!allowed_chunk_alloc && block_group && |
2304 | block_group_bits(block_group, data)) { | |
2305 | block_group->space_info->force_alloc = 1; | |
2306 | goto error; | |
2307 | } else if (!allowed_chunk_alloc) { | |
2308 | goto error; | |
2309 | } | |
2310 | ||
2311 | ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024, | |
2312 | data, 1); | |
2313 | if (ret < 0) | |
2314 | goto error; | |
2315 | ||
2316 | BUG_ON(ret); | |
2317 | chunk_alloc_done = 1; | |
2318 | if (block_group) | |
2319 | search_start = block_group->key.objectid + | |
2320 | block_group->key.offset; | |
2321 | else | |
2322 | search_start = orig_search_start; | |
2323 | goto new_group; | |
239b14b3 CM |
2324 | } |
2325 | ||
0f9dd46c JB |
2326 | if (ret) |
2327 | goto error; | |
2328 | ||
0b86a832 CM |
2329 | search_start = stripe_align(root, search_start); |
2330 | ins->objectid = search_start; | |
2331 | ins->offset = num_bytes; | |
e37c9e69 | 2332 | |
0f9dd46c JB |
2333 | if (ins->objectid + num_bytes >= search_end) { |
2334 | search_start = orig_search_start; | |
2335 | if (chunk_alloc_done) { | |
2336 | ret = -ENOSPC; | |
2337 | goto error; | |
2338 | } | |
2339 | goto new_group; | |
2340 | } | |
0b86a832 CM |
2341 | |
2342 | if (ins->objectid + num_bytes > | |
2343 | block_group->key.objectid + block_group->key.offset) { | |
0f9dd46c JB |
2344 | if (search_start == orig_search_start && chunk_alloc_done) { |
2345 | ret = -ENOSPC; | |
2346 | goto error; | |
2347 | } | |
e19caa5f CM |
2348 | search_start = block_group->key.objectid + |
2349 | block_group->key.offset; | |
2350 | goto new_group; | |
2351 | } | |
0b86a832 | 2352 | |
db94535d | 2353 | if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && |
f2654de4 CM |
2354 | ins->objectid < exclude_start + exclude_nr)) { |
2355 | search_start = exclude_start + exclude_nr; | |
2356 | goto new_group; | |
2357 | } | |
0b86a832 | 2358 | |
0f9dd46c JB |
2359 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
2360 | trans->block_group = block_group; | |
2361 | ||
db94535d | 2362 | ins->offset = num_bytes; |
239b14b3 CM |
2363 | if (last_ptr) { |
2364 | *last_ptr = ins->objectid + ins->offset; | |
2365 | if (*last_ptr == | |
0f9dd46c | 2366 | btrfs_super_total_bytes(&root->fs_info->super_copy)) |
239b14b3 | 2367 | *last_ptr = 0; |
be744175 | 2368 | } |
be744175 | 2369 | |
0f9dd46c | 2370 | ret = 0; |
0f70abe2 | 2371 | error: |
0f70abe2 | 2372 | return ret; |
fec577fb | 2373 | } |
ec44a35c | 2374 | |
0f9dd46c JB |
2375 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
2376 | { | |
2377 | struct btrfs_block_group_cache *cache; | |
2378 | struct list_head *l; | |
2379 | ||
2380 | printk(KERN_INFO "space_info has %Lu free, is %sfull\n", | |
2381 | info->total_bytes - info->bytes_used - info->bytes_pinned, | |
2382 | (info->full) ? "" : "not "); | |
2383 | ||
2384 | spin_lock(&info->lock); | |
2385 | list_for_each(l, &info->block_groups) { | |
2386 | cache = list_entry(l, struct btrfs_block_group_cache, list); | |
2387 | spin_lock(&cache->lock); | |
2388 | printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used " | |
2389 | "%Lu pinned\n", | |
2390 | cache->key.objectid, cache->key.offset, | |
2391 | btrfs_block_group_used(&cache->item), cache->pinned); | |
2392 | btrfs_dump_free_space(cache, bytes); | |
2393 | spin_unlock(&cache->lock); | |
2394 | } | |
2395 | spin_unlock(&info->lock); | |
2396 | } | |
e6dcd2dc CM |
2397 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
2398 | struct btrfs_root *root, | |
2399 | u64 num_bytes, u64 min_alloc_size, | |
2400 | u64 empty_size, u64 hint_byte, | |
2401 | u64 search_end, struct btrfs_key *ins, | |
2402 | u64 data) | |
fec577fb CM |
2403 | { |
2404 | int ret; | |
fbdc762b | 2405 | u64 search_start = 0; |
8790d502 | 2406 | u64 alloc_profile; |
1261ec42 | 2407 | struct btrfs_fs_info *info = root->fs_info; |
0f9dd46c | 2408 | struct btrfs_block_group_cache *cache; |
925baedd | 2409 | |
6324fbf3 | 2410 | if (data) { |
8790d502 CM |
2411 | alloc_profile = info->avail_data_alloc_bits & |
2412 | info->data_alloc_profile; | |
2413 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; | |
6324fbf3 | 2414 | } else if (root == root->fs_info->chunk_root) { |
8790d502 CM |
2415 | alloc_profile = info->avail_system_alloc_bits & |
2416 | info->system_alloc_profile; | |
2417 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; | |
6324fbf3 | 2418 | } else { |
8790d502 CM |
2419 | alloc_profile = info->avail_metadata_alloc_bits & |
2420 | info->metadata_alloc_profile; | |
2421 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; | |
6324fbf3 | 2422 | } |
98d20f67 | 2423 | again: |
a061fc8d | 2424 | data = reduce_alloc_profile(root, data); |
0ef3e66b CM |
2425 | /* |
2426 | * the only place that sets empty_size is btrfs_realloc_node, which | |
2427 | * is not called recursively on allocations | |
2428 | */ | |
2429 | if (empty_size || root->ref_cows) { | |
593060d7 | 2430 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
6324fbf3 | 2431 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
0ef3e66b CM |
2432 | 2 * 1024 * 1024, |
2433 | BTRFS_BLOCK_GROUP_METADATA | | |
2434 | (info->metadata_alloc_profile & | |
2435 | info->avail_metadata_alloc_bits), 0); | |
6324fbf3 CM |
2436 | } |
2437 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, | |
0ef3e66b | 2438 | num_bytes + 2 * 1024 * 1024, data, 0); |
6324fbf3 | 2439 | } |
0b86a832 | 2440 | |
db94535d CM |
2441 | WARN_ON(num_bytes < root->sectorsize); |
2442 | ret = find_free_extent(trans, root, num_bytes, empty_size, | |
2443 | search_start, search_end, hint_byte, ins, | |
26b8003f CM |
2444 | trans->alloc_exclude_start, |
2445 | trans->alloc_exclude_nr, data); | |
3b951516 | 2446 | |
98d20f67 CM |
2447 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
2448 | num_bytes = num_bytes >> 1; | |
0f9dd46c | 2449 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
98d20f67 | 2450 | num_bytes = max(num_bytes, min_alloc_size); |
0ef3e66b CM |
2451 | do_chunk_alloc(trans, root->fs_info->extent_root, |
2452 | num_bytes, data, 1); | |
98d20f67 CM |
2453 | goto again; |
2454 | } | |
ec44a35c | 2455 | if (ret) { |
0f9dd46c JB |
2456 | struct btrfs_space_info *sinfo; |
2457 | ||
2458 | sinfo = __find_space_info(root->fs_info, data); | |
2459 | printk("allocation failed flags %Lu, wanted %Lu\n", | |
2460 | data, num_bytes); | |
2461 | dump_space_info(sinfo, num_bytes); | |
925baedd | 2462 | BUG(); |
925baedd | 2463 | } |
0f9dd46c JB |
2464 | cache = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
2465 | if (!cache) { | |
2466 | printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid); | |
2467 | return -ENOSPC; | |
2468 | } | |
2469 | ||
2470 | ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset); | |
2471 | ||
2472 | return ret; | |
e6dcd2dc CM |
2473 | } |
2474 | ||
65b51a00 CM |
2475 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
2476 | { | |
0f9dd46c JB |
2477 | struct btrfs_block_group_cache *cache; |
2478 | ||
65b51a00 | 2479 | maybe_lock_mutex(root); |
0f9dd46c JB |
2480 | cache = btrfs_lookup_block_group(root->fs_info, start); |
2481 | if (!cache) { | |
2482 | printk(KERN_ERR "Unable to find block group for %Lu\n", start); | |
2483 | maybe_unlock_mutex(root); | |
2484 | return -ENOSPC; | |
2485 | } | |
2486 | btrfs_add_free_space(cache, start, len); | |
65b51a00 CM |
2487 | maybe_unlock_mutex(root); |
2488 | return 0; | |
2489 | } | |
2490 | ||
e6dcd2dc CM |
2491 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
2492 | struct btrfs_root *root, | |
2493 | u64 num_bytes, u64 min_alloc_size, | |
2494 | u64 empty_size, u64 hint_byte, | |
2495 | u64 search_end, struct btrfs_key *ins, | |
2496 | u64 data) | |
2497 | { | |
2498 | int ret; | |
2499 | maybe_lock_mutex(root); | |
2500 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, | |
2501 | empty_size, hint_byte, search_end, ins, | |
2502 | data); | |
2503 | maybe_unlock_mutex(root); | |
2504 | return ret; | |
2505 | } | |
2506 | ||
2507 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |
31840ae1 | 2508 | struct btrfs_root *root, u64 parent, |
e6dcd2dc CM |
2509 | u64 root_objectid, u64 ref_generation, |
2510 | u64 owner, u64 owner_offset, | |
2511 | struct btrfs_key *ins) | |
2512 | { | |
2513 | int ret; | |
2514 | int pending_ret; | |
2515 | u64 super_used; | |
2516 | u64 root_used; | |
2517 | u64 num_bytes = ins->offset; | |
2518 | u32 sizes[2]; | |
2519 | struct btrfs_fs_info *info = root->fs_info; | |
2520 | struct btrfs_root *extent_root = info->extent_root; | |
2521 | struct btrfs_extent_item *extent_item; | |
2522 | struct btrfs_extent_ref *ref; | |
2523 | struct btrfs_path *path; | |
2524 | struct btrfs_key keys[2]; | |
fec577fb | 2525 | |
31840ae1 ZY |
2526 | if (parent == 0) |
2527 | parent = ins->objectid; | |
2528 | ||
58176a96 | 2529 | /* block accounting for super block */ |
a2135011 | 2530 | spin_lock_irq(&info->delalloc_lock); |
db94535d CM |
2531 | super_used = btrfs_super_bytes_used(&info->super_copy); |
2532 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); | |
a2135011 | 2533 | spin_unlock_irq(&info->delalloc_lock); |
26b8003f | 2534 | |
58176a96 | 2535 | /* block accounting for root item */ |
db94535d CM |
2536 | root_used = btrfs_root_used(&root->root_item); |
2537 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); | |
58176a96 | 2538 | |
26b8003f | 2539 | if (root == extent_root) { |
31840ae1 ZY |
2540 | struct pending_extent_op *extent_op; |
2541 | ||
2542 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | |
2543 | BUG_ON(!extent_op); | |
2544 | ||
2545 | extent_op->type = PENDING_EXTENT_INSERT; | |
2546 | extent_op->bytenr = ins->objectid; | |
2547 | extent_op->num_bytes = ins->offset; | |
2548 | extent_op->parent = parent; | |
2549 | extent_op->orig_parent = 0; | |
2550 | extent_op->generation = ref_generation; | |
2551 | extent_op->orig_generation = 0; | |
2552 | extent_op->level = (int)owner; | |
2553 | ||
1a5bc167 CM |
2554 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
2555 | ins->objectid + ins->offset - 1, | |
2556 | EXTENT_LOCKED, GFP_NOFS); | |
31840ae1 ZY |
2557 | set_state_private(&root->fs_info->extent_ins, |
2558 | ins->objectid, (unsigned long)extent_op); | |
26b8003f CM |
2559 | goto update_block; |
2560 | } | |
2561 | ||
47e4bb98 | 2562 | memcpy(&keys[0], ins, sizeof(*ins)); |
47e4bb98 CM |
2563 | keys[1].objectid = ins->objectid; |
2564 | keys[1].type = BTRFS_EXTENT_REF_KEY; | |
31840ae1 | 2565 | keys[1].offset = parent; |
47e4bb98 CM |
2566 | sizes[0] = sizeof(*extent_item); |
2567 | sizes[1] = sizeof(*ref); | |
7bb86316 CM |
2568 | |
2569 | path = btrfs_alloc_path(); | |
2570 | BUG_ON(!path); | |
47e4bb98 CM |
2571 | |
2572 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, | |
2573 | sizes, 2); | |
ccd467d6 | 2574 | BUG_ON(ret); |
0f9dd46c | 2575 | |
47e4bb98 CM |
2576 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
2577 | struct btrfs_extent_item); | |
2578 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); | |
2579 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
2580 | struct btrfs_extent_ref); | |
2581 | ||
2582 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); | |
2583 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); | |
2584 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); | |
2585 | btrfs_set_ref_offset(path->nodes[0], ref, owner_offset); | |
31840ae1 | 2586 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); |
47e4bb98 CM |
2587 | |
2588 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
2589 | ||
2590 | trans->alloc_exclude_start = 0; | |
2591 | trans->alloc_exclude_nr = 0; | |
7bb86316 | 2592 | btrfs_free_path(path); |
e089f05c | 2593 | finish_current_insert(trans, extent_root); |
e20d96d6 | 2594 | pending_ret = del_pending_extents(trans, extent_root); |
f510cfec | 2595 | |
925baedd CM |
2596 | if (ret) |
2597 | goto out; | |
e37c9e69 | 2598 | if (pending_ret) { |
925baedd CM |
2599 | ret = pending_ret; |
2600 | goto out; | |
e37c9e69 | 2601 | } |
26b8003f CM |
2602 | |
2603 | update_block: | |
0b86a832 | 2604 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
f5947066 CM |
2605 | if (ret) { |
2606 | printk("update block group failed for %Lu %Lu\n", | |
2607 | ins->objectid, ins->offset); | |
2608 | BUG(); | |
2609 | } | |
925baedd | 2610 | out: |
e6dcd2dc CM |
2611 | return ret; |
2612 | } | |
2613 | ||
2614 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, | |
31840ae1 | 2615 | struct btrfs_root *root, u64 parent, |
e6dcd2dc CM |
2616 | u64 root_objectid, u64 ref_generation, |
2617 | u64 owner, u64 owner_offset, | |
2618 | struct btrfs_key *ins) | |
2619 | { | |
2620 | int ret; | |
1c2308f8 CM |
2621 | |
2622 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) | |
2623 | return 0; | |
e6dcd2dc | 2624 | maybe_lock_mutex(root); |
31840ae1 ZY |
2625 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
2626 | root_objectid, ref_generation, | |
2627 | owner, owner_offset, ins); | |
e6dcd2dc CM |
2628 | maybe_unlock_mutex(root); |
2629 | return ret; | |
2630 | } | |
e02119d5 CM |
2631 | |
2632 | /* | |
2633 | * this is used by the tree logging recovery code. It records that | |
2634 | * an extent has been allocated and makes sure to clear the free | |
2635 | * space cache bits as well | |
2636 | */ | |
2637 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, | |
31840ae1 | 2638 | struct btrfs_root *root, u64 parent, |
e02119d5 CM |
2639 | u64 root_objectid, u64 ref_generation, |
2640 | u64 owner, u64 owner_offset, | |
2641 | struct btrfs_key *ins) | |
2642 | { | |
2643 | int ret; | |
2644 | struct btrfs_block_group_cache *block_group; | |
2645 | ||
2646 | maybe_lock_mutex(root); | |
2647 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); | |
2648 | cache_block_group(root, block_group); | |
2649 | ||
0f9dd46c JB |
2650 | ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset); |
2651 | BUG_ON(ret); | |
31840ae1 ZY |
2652 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
2653 | root_objectid, ref_generation, | |
2654 | owner, owner_offset, ins); | |
e02119d5 CM |
2655 | maybe_unlock_mutex(root); |
2656 | return ret; | |
2657 | } | |
2658 | ||
e6dcd2dc CM |
2659 | /* |
2660 | * finds a free extent and does all the dirty work required for allocation | |
2661 | * returns the key for the extent through ins, and a tree buffer for | |
2662 | * the first block of the extent through buf. | |
2663 | * | |
2664 | * returns 0 if everything worked, non-zero otherwise. | |
2665 | */ | |
2666 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, | |
2667 | struct btrfs_root *root, | |
31840ae1 | 2668 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
e6dcd2dc | 2669 | u64 root_objectid, u64 ref_generation, |
31840ae1 | 2670 | u64 owner_objectid, u64 owner_offset, |
e6dcd2dc CM |
2671 | u64 empty_size, u64 hint_byte, |
2672 | u64 search_end, struct btrfs_key *ins, u64 data) | |
2673 | { | |
2674 | int ret; | |
2675 | ||
2676 | maybe_lock_mutex(root); | |
2677 | ||
2678 | ret = __btrfs_reserve_extent(trans, root, num_bytes, | |
2679 | min_alloc_size, empty_size, hint_byte, | |
2680 | search_end, ins, data); | |
2681 | BUG_ON(ret); | |
d00aff00 | 2682 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
31840ae1 ZY |
2683 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
2684 | root_objectid, ref_generation, | |
2685 | owner_objectid, owner_offset, ins); | |
d00aff00 | 2686 | BUG_ON(ret); |
e6dcd2dc | 2687 | |
d00aff00 | 2688 | } |
925baedd CM |
2689 | maybe_unlock_mutex(root); |
2690 | return ret; | |
fec577fb | 2691 | } |
65b51a00 CM |
2692 | |
2693 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, | |
2694 | struct btrfs_root *root, | |
2695 | u64 bytenr, u32 blocksize) | |
2696 | { | |
2697 | struct extent_buffer *buf; | |
2698 | ||
2699 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); | |
2700 | if (!buf) | |
2701 | return ERR_PTR(-ENOMEM); | |
2702 | btrfs_set_header_generation(buf, trans->transid); | |
2703 | btrfs_tree_lock(buf); | |
2704 | clean_tree_block(trans, root, buf); | |
2705 | btrfs_set_buffer_uptodate(buf); | |
d0c803c4 CM |
2706 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
2707 | set_extent_dirty(&root->dirty_log_pages, buf->start, | |
2708 | buf->start + buf->len - 1, GFP_NOFS); | |
2709 | } else { | |
2710 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, | |
65b51a00 | 2711 | buf->start + buf->len - 1, GFP_NOFS); |
d0c803c4 | 2712 | } |
65b51a00 CM |
2713 | trans->blocks_used++; |
2714 | return buf; | |
2715 | } | |
2716 | ||
fec577fb CM |
2717 | /* |
2718 | * helper function to allocate a block for a given tree | |
2719 | * returns the tree buffer or NULL. | |
2720 | */ | |
5f39d397 | 2721 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
7bb86316 | 2722 | struct btrfs_root *root, |
31840ae1 | 2723 | u32 blocksize, u64 parent, |
7bb86316 CM |
2724 | u64 root_objectid, |
2725 | u64 ref_generation, | |
7bb86316 CM |
2726 | int level, |
2727 | u64 hint, | |
5f39d397 | 2728 | u64 empty_size) |
fec577fb | 2729 | { |
e2fa7227 | 2730 | struct btrfs_key ins; |
fec577fb | 2731 | int ret; |
5f39d397 | 2732 | struct extent_buffer *buf; |
fec577fb | 2733 | |
31840ae1 ZY |
2734 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
2735 | root_objectid, ref_generation, level, 0, | |
2736 | empty_size, hint, (u64)-1, &ins, 0); | |
fec577fb | 2737 | if (ret) { |
54aa1f4d CM |
2738 | BUG_ON(ret > 0); |
2739 | return ERR_PTR(ret); | |
fec577fb | 2740 | } |
55c69072 | 2741 | |
65b51a00 | 2742 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); |
fec577fb CM |
2743 | return buf; |
2744 | } | |
a28ec197 | 2745 | |
e02119d5 CM |
2746 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
2747 | struct btrfs_root *root, struct extent_buffer *leaf) | |
6407bf6d | 2748 | { |
7bb86316 CM |
2749 | u64 leaf_owner; |
2750 | u64 leaf_generation; | |
5f39d397 | 2751 | struct btrfs_key key; |
6407bf6d CM |
2752 | struct btrfs_file_extent_item *fi; |
2753 | int i; | |
2754 | int nritems; | |
2755 | int ret; | |
2756 | ||
5f39d397 CM |
2757 | BUG_ON(!btrfs_is_leaf(leaf)); |
2758 | nritems = btrfs_header_nritems(leaf); | |
7bb86316 CM |
2759 | leaf_owner = btrfs_header_owner(leaf); |
2760 | leaf_generation = btrfs_header_generation(leaf); | |
2761 | ||
6407bf6d | 2762 | for (i = 0; i < nritems; i++) { |
db94535d | 2763 | u64 disk_bytenr; |
e34a5b4f | 2764 | cond_resched(); |
5f39d397 CM |
2765 | |
2766 | btrfs_item_key_to_cpu(leaf, &key, i); | |
2767 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
6407bf6d CM |
2768 | continue; |
2769 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
5f39d397 CM |
2770 | if (btrfs_file_extent_type(leaf, fi) == |
2771 | BTRFS_FILE_EXTENT_INLINE) | |
236454df | 2772 | continue; |
6407bf6d CM |
2773 | /* |
2774 | * FIXME make sure to insert a trans record that | |
2775 | * repeats the snapshot del on crash | |
2776 | */ | |
db94535d CM |
2777 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
2778 | if (disk_bytenr == 0) | |
3a686375 | 2779 | continue; |
4a096752 CM |
2780 | |
2781 | mutex_lock(&root->fs_info->alloc_mutex); | |
925baedd | 2782 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
7bb86316 | 2783 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
31840ae1 | 2784 | leaf->start, leaf_owner, leaf_generation, |
7bb86316 | 2785 | key.objectid, key.offset, 0); |
4a096752 | 2786 | mutex_unlock(&root->fs_info->alloc_mutex); |
31840ae1 | 2787 | BUG_ON(ret); |
2dd3e67b CM |
2788 | |
2789 | atomic_inc(&root->fs_info->throttle_gen); | |
2790 | wake_up(&root->fs_info->transaction_throttle); | |
2791 | cond_resched(); | |
6407bf6d CM |
2792 | } |
2793 | return 0; | |
2794 | } | |
2795 | ||
e02119d5 CM |
2796 | static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
2797 | struct btrfs_root *root, | |
2798 | struct btrfs_leaf_ref *ref) | |
31153d81 YZ |
2799 | { |
2800 | int i; | |
2801 | int ret; | |
2802 | struct btrfs_extent_info *info = ref->extents; | |
2803 | ||
31153d81 YZ |
2804 | for (i = 0; i < ref->nritems; i++) { |
2805 | mutex_lock(&root->fs_info->alloc_mutex); | |
31840ae1 ZY |
2806 | ret = __btrfs_free_extent(trans, root, info->bytenr, |
2807 | info->num_bytes, ref->bytenr, | |
2808 | ref->owner, ref->generation, | |
2809 | info->objectid, info->offset, 0); | |
31153d81 | 2810 | mutex_unlock(&root->fs_info->alloc_mutex); |
2dd3e67b CM |
2811 | |
2812 | atomic_inc(&root->fs_info->throttle_gen); | |
2813 | wake_up(&root->fs_info->transaction_throttle); | |
2814 | cond_resched(); | |
2815 | ||
31153d81 YZ |
2816 | BUG_ON(ret); |
2817 | info++; | |
2818 | } | |
31153d81 YZ |
2819 | |
2820 | return 0; | |
2821 | } | |
2822 | ||
333db94c CM |
2823 | int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len, |
2824 | u32 *refs) | |
2825 | { | |
017e5369 | 2826 | int ret; |
f87f057b | 2827 | |
31840ae1 | 2828 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); |
f87f057b CM |
2829 | BUG_ON(ret); |
2830 | ||
2831 | #if 0 // some debugging code in case we see problems here | |
2832 | /* if the refs count is one, it won't get increased again. But | |
2833 | * if the ref count is > 1, someone may be decreasing it at | |
2834 | * the same time we are. | |
2835 | */ | |
2836 | if (*refs != 1) { | |
2837 | struct extent_buffer *eb = NULL; | |
2838 | eb = btrfs_find_create_tree_block(root, start, len); | |
2839 | if (eb) | |
2840 | btrfs_tree_lock(eb); | |
2841 | ||
2842 | mutex_lock(&root->fs_info->alloc_mutex); | |
2843 | ret = lookup_extent_ref(NULL, root, start, len, refs); | |
2844 | BUG_ON(ret); | |
2845 | mutex_unlock(&root->fs_info->alloc_mutex); | |
2846 | ||
2847 | if (eb) { | |
2848 | btrfs_tree_unlock(eb); | |
2849 | free_extent_buffer(eb); | |
2850 | } | |
2851 | if (*refs == 1) { | |
2852 | printk("block %llu went down to one during drop_snap\n", | |
2853 | (unsigned long long)start); | |
2854 | } | |
2855 | ||
2856 | } | |
2857 | #endif | |
2858 | ||
e7a84565 | 2859 | cond_resched(); |
017e5369 | 2860 | return ret; |
333db94c CM |
2861 | } |
2862 | ||
9aca1d51 CM |
2863 | /* |
2864 | * helper function for drop_snapshot, this walks down the tree dropping ref | |
2865 | * counts as it goes. | |
2866 | */ | |
98ed5174 CM |
2867 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
2868 | struct btrfs_root *root, | |
2869 | struct btrfs_path *path, int *level) | |
20524f02 | 2870 | { |
7bb86316 CM |
2871 | u64 root_owner; |
2872 | u64 root_gen; | |
2873 | u64 bytenr; | |
ca7a79ad | 2874 | u64 ptr_gen; |
5f39d397 CM |
2875 | struct extent_buffer *next; |
2876 | struct extent_buffer *cur; | |
7bb86316 | 2877 | struct extent_buffer *parent; |
31153d81 | 2878 | struct btrfs_leaf_ref *ref; |
db94535d | 2879 | u32 blocksize; |
20524f02 CM |
2880 | int ret; |
2881 | u32 refs; | |
2882 | ||
5caf2a00 CM |
2883 | WARN_ON(*level < 0); |
2884 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
333db94c | 2885 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
db94535d | 2886 | path->nodes[*level]->len, &refs); |
20524f02 CM |
2887 | BUG_ON(ret); |
2888 | if (refs > 1) | |
2889 | goto out; | |
e011599b | 2890 | |
9aca1d51 CM |
2891 | /* |
2892 | * walk down to the last node level and free all the leaves | |
2893 | */ | |
6407bf6d | 2894 | while(*level >= 0) { |
5caf2a00 CM |
2895 | WARN_ON(*level < 0); |
2896 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
20524f02 | 2897 | cur = path->nodes[*level]; |
e011599b | 2898 | |
5f39d397 | 2899 | if (btrfs_header_level(cur) != *level) |
2c90e5d6 | 2900 | WARN_ON(1); |
e011599b | 2901 | |
7518a238 | 2902 | if (path->slots[*level] >= |
5f39d397 | 2903 | btrfs_header_nritems(cur)) |
20524f02 | 2904 | break; |
6407bf6d | 2905 | if (*level == 0) { |
e02119d5 | 2906 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
6407bf6d CM |
2907 | BUG_ON(ret); |
2908 | break; | |
2909 | } | |
db94535d | 2910 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
ca7a79ad | 2911 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
db94535d | 2912 | blocksize = btrfs_level_size(root, *level - 1); |
925baedd | 2913 | |
333db94c | 2914 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
6407bf6d CM |
2915 | BUG_ON(ret); |
2916 | if (refs != 1) { | |
7bb86316 CM |
2917 | parent = path->nodes[*level]; |
2918 | root_owner = btrfs_header_owner(parent); | |
2919 | root_gen = btrfs_header_generation(parent); | |
20524f02 | 2920 | path->slots[*level]++; |
f87f057b CM |
2921 | |
2922 | mutex_lock(&root->fs_info->alloc_mutex); | |
925baedd | 2923 | ret = __btrfs_free_extent(trans, root, bytenr, |
31840ae1 ZY |
2924 | blocksize, parent->start, |
2925 | root_owner, root_gen, 0, 0, 1); | |
20524f02 | 2926 | BUG_ON(ret); |
f87f057b | 2927 | mutex_unlock(&root->fs_info->alloc_mutex); |
18e35e0a CM |
2928 | |
2929 | atomic_inc(&root->fs_info->throttle_gen); | |
2930 | wake_up(&root->fs_info->transaction_throttle); | |
2dd3e67b | 2931 | cond_resched(); |
18e35e0a | 2932 | |
20524f02 CM |
2933 | continue; |
2934 | } | |
f87f057b CM |
2935 | /* |
2936 | * at this point, we have a single ref, and since the | |
2937 | * only place referencing this extent is a dead root | |
2938 | * the reference count should never go higher. | |
2939 | * So, we don't need to check it again | |
2940 | */ | |
31153d81 | 2941 | if (*level == 1) { |
017e5369 | 2942 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
31153d81 | 2943 | if (ref) { |
e02119d5 | 2944 | ret = cache_drop_leaf_ref(trans, root, ref); |
31153d81 YZ |
2945 | BUG_ON(ret); |
2946 | btrfs_remove_leaf_ref(root, ref); | |
bcc63abb | 2947 | btrfs_free_leaf_ref(root, ref); |
31153d81 YZ |
2948 | *level = 0; |
2949 | break; | |
2950 | } | |
37d1aeee CM |
2951 | if (printk_ratelimit()) |
2952 | printk("leaf ref miss for bytenr %llu\n", | |
2953 | (unsigned long long)bytenr); | |
31153d81 | 2954 | } |
db94535d | 2955 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
1259ab75 | 2956 | if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) { |
5f39d397 | 2957 | free_extent_buffer(next); |
333db94c | 2958 | |
ca7a79ad CM |
2959 | next = read_tree_block(root, bytenr, blocksize, |
2960 | ptr_gen); | |
e7a84565 | 2961 | cond_resched(); |
f87f057b CM |
2962 | #if 0 |
2963 | /* | |
2964 | * this is a debugging check and can go away | |
2965 | * the ref should never go all the way down to 1 | |
2966 | * at this point | |
2967 | */ | |
e6dcd2dc CM |
2968 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, |
2969 | &refs); | |
e9d0b13b | 2970 | BUG_ON(ret); |
f87f057b CM |
2971 | WARN_ON(refs != 1); |
2972 | #endif | |
e9d0b13b | 2973 | } |
5caf2a00 | 2974 | WARN_ON(*level <= 0); |
83e15a28 | 2975 | if (path->nodes[*level-1]) |
5f39d397 | 2976 | free_extent_buffer(path->nodes[*level-1]); |
20524f02 | 2977 | path->nodes[*level-1] = next; |
5f39d397 | 2978 | *level = btrfs_header_level(next); |
20524f02 | 2979 | path->slots[*level] = 0; |
2dd3e67b | 2980 | cond_resched(); |
20524f02 CM |
2981 | } |
2982 | out: | |
5caf2a00 CM |
2983 | WARN_ON(*level < 0); |
2984 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
7bb86316 CM |
2985 | |
2986 | if (path->nodes[*level] == root->node) { | |
7bb86316 | 2987 | parent = path->nodes[*level]; |
31153d81 | 2988 | bytenr = path->nodes[*level]->start; |
7bb86316 CM |
2989 | } else { |
2990 | parent = path->nodes[*level + 1]; | |
31153d81 | 2991 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
7bb86316 CM |
2992 | } |
2993 | ||
31153d81 YZ |
2994 | blocksize = btrfs_level_size(root, *level); |
2995 | root_owner = btrfs_header_owner(parent); | |
7bb86316 | 2996 | root_gen = btrfs_header_generation(parent); |
31153d81 | 2997 | |
f87f057b | 2998 | mutex_lock(&root->fs_info->alloc_mutex); |
31153d81 | 2999 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, |
31840ae1 ZY |
3000 | parent->start, root_owner, root_gen, |
3001 | 0, 0, 1); | |
3002 | mutex_unlock(&root->fs_info->alloc_mutex); | |
5f39d397 | 3003 | free_extent_buffer(path->nodes[*level]); |
20524f02 CM |
3004 | path->nodes[*level] = NULL; |
3005 | *level += 1; | |
3006 | BUG_ON(ret); | |
f87f057b | 3007 | |
e7a84565 | 3008 | cond_resched(); |
20524f02 CM |
3009 | return 0; |
3010 | } | |
3011 | ||
9aca1d51 CM |
3012 | /* |
3013 | * helper for dropping snapshots. This walks back up the tree in the path | |
3014 | * to find the first node higher up where we haven't yet gone through | |
3015 | * all the slots | |
3016 | */ | |
98ed5174 CM |
3017 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
3018 | struct btrfs_root *root, | |
3019 | struct btrfs_path *path, int *level) | |
20524f02 | 3020 | { |
7bb86316 CM |
3021 | u64 root_owner; |
3022 | u64 root_gen; | |
3023 | struct btrfs_root_item *root_item = &root->root_item; | |
20524f02 CM |
3024 | int i; |
3025 | int slot; | |
3026 | int ret; | |
9f3a7427 | 3027 | |
234b63a0 | 3028 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
20524f02 | 3029 | slot = path->slots[i]; |
5f39d397 CM |
3030 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
3031 | struct extent_buffer *node; | |
3032 | struct btrfs_disk_key disk_key; | |
3033 | node = path->nodes[i]; | |
20524f02 CM |
3034 | path->slots[i]++; |
3035 | *level = i; | |
9f3a7427 | 3036 | WARN_ON(*level == 0); |
5f39d397 | 3037 | btrfs_node_key(node, &disk_key, path->slots[i]); |
9f3a7427 | 3038 | memcpy(&root_item->drop_progress, |
5f39d397 | 3039 | &disk_key, sizeof(disk_key)); |
9f3a7427 | 3040 | root_item->drop_level = i; |
20524f02 CM |
3041 | return 0; |
3042 | } else { | |
31840ae1 ZY |
3043 | struct extent_buffer *parent; |
3044 | if (path->nodes[*level] == root->node) | |
3045 | parent = path->nodes[*level]; | |
3046 | else | |
3047 | parent = path->nodes[*level + 1]; | |
3048 | ||
3049 | root_owner = btrfs_header_owner(parent); | |
3050 | root_gen = btrfs_header_generation(parent); | |
e089f05c | 3051 | ret = btrfs_free_extent(trans, root, |
db94535d | 3052 | path->nodes[*level]->start, |
7bb86316 | 3053 | path->nodes[*level]->len, |
31840ae1 | 3054 | parent->start, |
7bb86316 | 3055 | root_owner, root_gen, 0, 0, 1); |
6407bf6d | 3056 | BUG_ON(ret); |
5f39d397 | 3057 | free_extent_buffer(path->nodes[*level]); |
83e15a28 | 3058 | path->nodes[*level] = NULL; |
20524f02 | 3059 | *level = i + 1; |
20524f02 CM |
3060 | } |
3061 | } | |
3062 | return 1; | |
3063 | } | |
3064 | ||
9aca1d51 CM |
3065 | /* |
3066 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
3067 | * the tree freeing any blocks that have a ref count of zero after being | |
3068 | * decremented. | |
3069 | */ | |
e089f05c | 3070 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
9f3a7427 | 3071 | *root) |
20524f02 | 3072 | { |
3768f368 | 3073 | int ret = 0; |
9aca1d51 | 3074 | int wret; |
20524f02 | 3075 | int level; |
5caf2a00 | 3076 | struct btrfs_path *path; |
20524f02 CM |
3077 | int i; |
3078 | int orig_level; | |
9f3a7427 | 3079 | struct btrfs_root_item *root_item = &root->root_item; |
20524f02 | 3080 | |
a2135011 | 3081 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
5caf2a00 CM |
3082 | path = btrfs_alloc_path(); |
3083 | BUG_ON(!path); | |
20524f02 | 3084 | |
5f39d397 | 3085 | level = btrfs_header_level(root->node); |
20524f02 | 3086 | orig_level = level; |
9f3a7427 CM |
3087 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
3088 | path->nodes[level] = root->node; | |
f510cfec | 3089 | extent_buffer_get(root->node); |
9f3a7427 CM |
3090 | path->slots[level] = 0; |
3091 | } else { | |
3092 | struct btrfs_key key; | |
5f39d397 CM |
3093 | struct btrfs_disk_key found_key; |
3094 | struct extent_buffer *node; | |
6702ed49 | 3095 | |
9f3a7427 | 3096 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
6702ed49 CM |
3097 | level = root_item->drop_level; |
3098 | path->lowest_level = level; | |
9f3a7427 | 3099 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
6702ed49 | 3100 | if (wret < 0) { |
9f3a7427 CM |
3101 | ret = wret; |
3102 | goto out; | |
3103 | } | |
5f39d397 CM |
3104 | node = path->nodes[level]; |
3105 | btrfs_node_key(node, &found_key, path->slots[level]); | |
3106 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, | |
3107 | sizeof(found_key))); | |
7d9eb12c CM |
3108 | /* |
3109 | * unlock our path, this is safe because only this | |
3110 | * function is allowed to delete this snapshot | |
3111 | */ | |
925baedd CM |
3112 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
3113 | if (path->nodes[i] && path->locks[i]) { | |
3114 | path->locks[i] = 0; | |
3115 | btrfs_tree_unlock(path->nodes[i]); | |
3116 | } | |
3117 | } | |
9f3a7427 | 3118 | } |
20524f02 | 3119 | while(1) { |
5caf2a00 | 3120 | wret = walk_down_tree(trans, root, path, &level); |
9aca1d51 | 3121 | if (wret > 0) |
20524f02 | 3122 | break; |
9aca1d51 CM |
3123 | if (wret < 0) |
3124 | ret = wret; | |
3125 | ||
5caf2a00 | 3126 | wret = walk_up_tree(trans, root, path, &level); |
9aca1d51 | 3127 | if (wret > 0) |
20524f02 | 3128 | break; |
9aca1d51 CM |
3129 | if (wret < 0) |
3130 | ret = wret; | |
e7a84565 CM |
3131 | if (trans->transaction->in_commit) { |
3132 | ret = -EAGAIN; | |
3133 | break; | |
3134 | } | |
18e35e0a | 3135 | atomic_inc(&root->fs_info->throttle_gen); |
017e5369 | 3136 | wake_up(&root->fs_info->transaction_throttle); |
20524f02 | 3137 | } |
83e15a28 | 3138 | for (i = 0; i <= orig_level; i++) { |
5caf2a00 | 3139 | if (path->nodes[i]) { |
5f39d397 | 3140 | free_extent_buffer(path->nodes[i]); |
0f82731f | 3141 | path->nodes[i] = NULL; |
83e15a28 | 3142 | } |
20524f02 | 3143 | } |
9f3a7427 | 3144 | out: |
5caf2a00 | 3145 | btrfs_free_path(path); |
9aca1d51 | 3146 | return ret; |
20524f02 | 3147 | } |
9078a3e1 | 3148 | |
96b5179d | 3149 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
9078a3e1 | 3150 | { |
0f9dd46c JB |
3151 | struct btrfs_block_group_cache *block_group; |
3152 | struct rb_node *n; | |
925baedd CM |
3153 | |
3154 | mutex_lock(&info->alloc_mutex); | |
0f9dd46c JB |
3155 | spin_lock(&info->block_group_cache_lock); |
3156 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { | |
3157 | block_group = rb_entry(n, struct btrfs_block_group_cache, | |
3158 | cache_node); | |
3159 | ||
4434c33c | 3160 | spin_unlock(&info->block_group_cache_lock); |
0f9dd46c | 3161 | btrfs_remove_free_space_cache(block_group); |
4434c33c CM |
3162 | spin_lock(&info->block_group_cache_lock); |
3163 | ||
0f9dd46c JB |
3164 | rb_erase(&block_group->cache_node, |
3165 | &info->block_group_cache_tree); | |
4434c33c | 3166 | |
0f9dd46c JB |
3167 | spin_lock(&block_group->space_info->lock); |
3168 | list_del(&block_group->list); | |
3169 | spin_unlock(&block_group->space_info->lock); | |
3170 | kfree(block_group); | |
3171 | } | |
3172 | spin_unlock(&info->block_group_cache_lock); | |
925baedd | 3173 | mutex_unlock(&info->alloc_mutex); |
be744175 CM |
3174 | return 0; |
3175 | } | |
3176 | ||
8e7bf94f CM |
3177 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
3178 | unsigned long nr) | |
3179 | { | |
3180 | return min(last, start + nr - 1); | |
3181 | } | |
3182 | ||
98ed5174 CM |
3183 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
3184 | u64 len) | |
edbd8d4e CM |
3185 | { |
3186 | u64 page_start; | |
3187 | u64 page_end; | |
edbd8d4e | 3188 | unsigned long last_index; |
edbd8d4e CM |
3189 | unsigned long i; |
3190 | struct page *page; | |
d1310b2e | 3191 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4313b399 | 3192 | struct file_ra_state *ra; |
8e7bf94f CM |
3193 | unsigned long total_read = 0; |
3194 | unsigned long ra_pages; | |
3eaa2885 | 3195 | struct btrfs_ordered_extent *ordered; |
a061fc8d | 3196 | struct btrfs_trans_handle *trans; |
4313b399 CM |
3197 | |
3198 | ra = kzalloc(sizeof(*ra), GFP_NOFS); | |
edbd8d4e CM |
3199 | |
3200 | mutex_lock(&inode->i_mutex); | |
4313b399 | 3201 | i = start >> PAGE_CACHE_SHIFT; |
edbd8d4e CM |
3202 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
3203 | ||
8e7bf94f CM |
3204 | ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages; |
3205 | ||
4313b399 | 3206 | file_ra_state_init(ra, inode->i_mapping); |
edbd8d4e | 3207 | |
4313b399 | 3208 | for (; i <= last_index; i++) { |
8e7bf94f CM |
3209 | if (total_read % ra_pages == 0) { |
3210 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, | |
3211 | calc_ra(i, last_index, ra_pages)); | |
3212 | } | |
3213 | total_read++; | |
3eaa2885 CM |
3214 | again: |
3215 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) | |
a061fc8d | 3216 | goto truncate_racing; |
edbd8d4e | 3217 | page = grab_cache_page(inode->i_mapping, i); |
a061fc8d | 3218 | if (!page) { |
edbd8d4e | 3219 | goto out_unlock; |
a061fc8d | 3220 | } |
edbd8d4e CM |
3221 | if (!PageUptodate(page)) { |
3222 | btrfs_readpage(NULL, page); | |
3223 | lock_page(page); | |
3224 | if (!PageUptodate(page)) { | |
3225 | unlock_page(page); | |
3226 | page_cache_release(page); | |
3227 | goto out_unlock; | |
3228 | } | |
3229 | } | |
ec44a35c | 3230 | wait_on_page_writeback(page); |
3eaa2885 | 3231 | |
edbd8d4e CM |
3232 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
3233 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
d1310b2e | 3234 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
edbd8d4e | 3235 | |
3eaa2885 CM |
3236 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
3237 | if (ordered) { | |
3238 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
3239 | unlock_page(page); | |
3240 | page_cache_release(page); | |
3241 | btrfs_start_ordered_extent(inode, ordered, 1); | |
3242 | btrfs_put_ordered_extent(ordered); | |
3243 | goto again; | |
3244 | } | |
3245 | set_page_extent_mapped(page); | |
3246 | ||
f87f057b CM |
3247 | /* |
3248 | * make sure page_mkwrite is called for this page if userland | |
3249 | * wants to change it from mmap | |
3250 | */ | |
3251 | clear_page_dirty_for_io(page); | |
3eaa2885 | 3252 | |
ea8c2819 | 3253 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
a061fc8d | 3254 | set_page_dirty(page); |
edbd8d4e | 3255 | |
d1310b2e | 3256 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
edbd8d4e CM |
3257 | unlock_page(page); |
3258 | page_cache_release(page); | |
3259 | } | |
3260 | ||
3261 | out_unlock: | |
3eaa2885 CM |
3262 | /* we have to start the IO in order to get the ordered extents |
3263 | * instantiated. This allows the relocation to code to wait | |
3264 | * for all the ordered extents to hit the disk. | |
3265 | * | |
3266 | * Otherwise, it would constantly loop over the same extents | |
3267 | * because the old ones don't get deleted until the IO is | |
3268 | * started | |
3269 | */ | |
3270 | btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1, | |
3271 | WB_SYNC_NONE); | |
ec44a35c | 3272 | kfree(ra); |
a061fc8d CM |
3273 | trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1); |
3274 | if (trans) { | |
a061fc8d CM |
3275 | btrfs_end_transaction(trans, BTRFS_I(inode)->root); |
3276 | mark_inode_dirty(inode); | |
3277 | } | |
edbd8d4e CM |
3278 | mutex_unlock(&inode->i_mutex); |
3279 | return 0; | |
a061fc8d CM |
3280 | |
3281 | truncate_racing: | |
3282 | vmtruncate(inode, inode->i_size); | |
3283 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, | |
3284 | total_read); | |
3285 | goto out_unlock; | |
edbd8d4e CM |
3286 | } |
3287 | ||
bf4ef679 CM |
3288 | /* |
3289 | * The back references tell us which tree holds a ref on a block, | |
3290 | * but it is possible for the tree root field in the reference to | |
3291 | * reflect the original root before a snapshot was made. In this | |
3292 | * case we should search through all the children of a given root | |
3293 | * to find potential holders of references on a block. | |
3294 | * | |
3295 | * Instead, we do something a little less fancy and just search | |
3296 | * all the roots for a given key/block combination. | |
3297 | */ | |
3298 | static int find_root_for_ref(struct btrfs_root *root, | |
3299 | struct btrfs_path *path, | |
3300 | struct btrfs_key *key0, | |
3301 | int level, | |
3302 | int file_key, | |
3303 | struct btrfs_root **found_root, | |
3304 | u64 bytenr) | |
3305 | { | |
3306 | struct btrfs_key root_location; | |
3307 | struct btrfs_root *cur_root = *found_root; | |
3308 | struct btrfs_file_extent_item *file_extent; | |
3309 | u64 root_search_start = BTRFS_FS_TREE_OBJECTID; | |
3310 | u64 found_bytenr; | |
3311 | int ret; | |
bf4ef679 CM |
3312 | |
3313 | root_location.offset = (u64)-1; | |
3314 | root_location.type = BTRFS_ROOT_ITEM_KEY; | |
3315 | path->lowest_level = level; | |
3316 | path->reada = 0; | |
3317 | while(1) { | |
3318 | ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0); | |
3319 | found_bytenr = 0; | |
3320 | if (ret == 0 && file_key) { | |
3321 | struct extent_buffer *leaf = path->nodes[0]; | |
3322 | file_extent = btrfs_item_ptr(leaf, path->slots[0], | |
3323 | struct btrfs_file_extent_item); | |
3324 | if (btrfs_file_extent_type(leaf, file_extent) == | |
3325 | BTRFS_FILE_EXTENT_REG) { | |
3326 | found_bytenr = | |
3327 | btrfs_file_extent_disk_bytenr(leaf, | |
3328 | file_extent); | |
3329 | } | |
323da79c | 3330 | } else if (!file_key) { |
bf4ef679 CM |
3331 | if (path->nodes[level]) |
3332 | found_bytenr = path->nodes[level]->start; | |
3333 | } | |
3334 | ||
bf4ef679 CM |
3335 | btrfs_release_path(cur_root, path); |
3336 | ||
3337 | if (found_bytenr == bytenr) { | |
3338 | *found_root = cur_root; | |
3339 | ret = 0; | |
3340 | goto out; | |
3341 | } | |
3342 | ret = btrfs_search_root(root->fs_info->tree_root, | |
3343 | root_search_start, &root_search_start); | |
3344 | if (ret) | |
3345 | break; | |
3346 | ||
3347 | root_location.objectid = root_search_start; | |
3348 | cur_root = btrfs_read_fs_root_no_name(root->fs_info, | |
3349 | &root_location); | |
3350 | if (!cur_root) { | |
3351 | ret = 1; | |
3352 | break; | |
3353 | } | |
3354 | } | |
3355 | out: | |
3356 | path->lowest_level = 0; | |
3357 | return ret; | |
3358 | } | |
3359 | ||
4313b399 CM |
3360 | /* |
3361 | * note, this releases the path | |
3362 | */ | |
98ed5174 | 3363 | static int noinline relocate_one_reference(struct btrfs_root *extent_root, |
edbd8d4e | 3364 | struct btrfs_path *path, |
0ef3e66b CM |
3365 | struct btrfs_key *extent_key, |
3366 | u64 *last_file_objectid, | |
3367 | u64 *last_file_offset, | |
3368 | u64 *last_file_root, | |
3369 | u64 last_extent) | |
edbd8d4e CM |
3370 | { |
3371 | struct inode *inode; | |
3372 | struct btrfs_root *found_root; | |
bf4ef679 CM |
3373 | struct btrfs_key root_location; |
3374 | struct btrfs_key found_key; | |
4313b399 CM |
3375 | struct btrfs_extent_ref *ref; |
3376 | u64 ref_root; | |
3377 | u64 ref_gen; | |
3378 | u64 ref_objectid; | |
3379 | u64 ref_offset; | |
edbd8d4e | 3380 | int ret; |
bf4ef679 | 3381 | int level; |
edbd8d4e | 3382 | |
7d9eb12c CM |
3383 | WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex)); |
3384 | ||
4313b399 CM |
3385 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
3386 | struct btrfs_extent_ref); | |
3387 | ref_root = btrfs_ref_root(path->nodes[0], ref); | |
3388 | ref_gen = btrfs_ref_generation(path->nodes[0], ref); | |
3389 | ref_objectid = btrfs_ref_objectid(path->nodes[0], ref); | |
3390 | ref_offset = btrfs_ref_offset(path->nodes[0], ref); | |
3391 | btrfs_release_path(extent_root, path); | |
3392 | ||
bf4ef679 | 3393 | root_location.objectid = ref_root; |
edbd8d4e | 3394 | if (ref_gen == 0) |
bf4ef679 | 3395 | root_location.offset = 0; |
edbd8d4e | 3396 | else |
bf4ef679 CM |
3397 | root_location.offset = (u64)-1; |
3398 | root_location.type = BTRFS_ROOT_ITEM_KEY; | |
edbd8d4e CM |
3399 | |
3400 | found_root = btrfs_read_fs_root_no_name(extent_root->fs_info, | |
bf4ef679 | 3401 | &root_location); |
edbd8d4e | 3402 | BUG_ON(!found_root); |
7d9eb12c | 3403 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
edbd8d4e CM |
3404 | |
3405 | if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | |
bf4ef679 CM |
3406 | found_key.objectid = ref_objectid; |
3407 | found_key.type = BTRFS_EXTENT_DATA_KEY; | |
3408 | found_key.offset = ref_offset; | |
3409 | level = 0; | |
3410 | ||
0ef3e66b CM |
3411 | if (last_extent == extent_key->objectid && |
3412 | *last_file_objectid == ref_objectid && | |
3413 | *last_file_offset == ref_offset && | |
3414 | *last_file_root == ref_root) | |
3415 | goto out; | |
3416 | ||
bf4ef679 CM |
3417 | ret = find_root_for_ref(extent_root, path, &found_key, |
3418 | level, 1, &found_root, | |
3419 | extent_key->objectid); | |
3420 | ||
3421 | if (ret) | |
3422 | goto out; | |
3423 | ||
0ef3e66b CM |
3424 | if (last_extent == extent_key->objectid && |
3425 | *last_file_objectid == ref_objectid && | |
3426 | *last_file_offset == ref_offset && | |
3427 | *last_file_root == ref_root) | |
3428 | goto out; | |
3429 | ||
edbd8d4e CM |
3430 | inode = btrfs_iget_locked(extent_root->fs_info->sb, |
3431 | ref_objectid, found_root); | |
3432 | if (inode->i_state & I_NEW) { | |
3433 | /* the inode and parent dir are two different roots */ | |
3434 | BTRFS_I(inode)->root = found_root; | |
3435 | BTRFS_I(inode)->location.objectid = ref_objectid; | |
3436 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
3437 | BTRFS_I(inode)->location.offset = 0; | |
3438 | btrfs_read_locked_inode(inode); | |
3439 | unlock_new_inode(inode); | |
3440 | ||
3441 | } | |
3442 | /* this can happen if the reference is not against | |
3443 | * the latest version of the tree root | |
3444 | */ | |
7d9eb12c | 3445 | if (is_bad_inode(inode)) |
edbd8d4e | 3446 | goto out; |
7d9eb12c | 3447 | |
0ef3e66b CM |
3448 | *last_file_objectid = inode->i_ino; |
3449 | *last_file_root = found_root->root_key.objectid; | |
3450 | *last_file_offset = ref_offset; | |
3451 | ||
edbd8d4e | 3452 | relocate_inode_pages(inode, ref_offset, extent_key->offset); |
edbd8d4e | 3453 | iput(inode); |
edbd8d4e CM |
3454 | } else { |
3455 | struct btrfs_trans_handle *trans; | |
edbd8d4e | 3456 | struct extent_buffer *eb; |
7d9eb12c | 3457 | int needs_lock = 0; |
edbd8d4e | 3458 | |
edbd8d4e | 3459 | eb = read_tree_block(found_root, extent_key->objectid, |
ca7a79ad | 3460 | extent_key->offset, 0); |
925baedd | 3461 | btrfs_tree_lock(eb); |
edbd8d4e CM |
3462 | level = btrfs_header_level(eb); |
3463 | ||
3464 | if (level == 0) | |
3465 | btrfs_item_key_to_cpu(eb, &found_key, 0); | |
3466 | else | |
3467 | btrfs_node_key_to_cpu(eb, &found_key, 0); | |
3468 | ||
925baedd | 3469 | btrfs_tree_unlock(eb); |
edbd8d4e CM |
3470 | free_extent_buffer(eb); |
3471 | ||
bf4ef679 CM |
3472 | ret = find_root_for_ref(extent_root, path, &found_key, |
3473 | level, 0, &found_root, | |
3474 | extent_key->objectid); | |
3475 | ||
3476 | if (ret) | |
3477 | goto out; | |
3478 | ||
7d9eb12c CM |
3479 | /* |
3480 | * right here almost anything could happen to our key, | |
3481 | * but that's ok. The cow below will either relocate it | |
3482 | * or someone else will have relocated it. Either way, | |
3483 | * it is in a different spot than it was before and | |
3484 | * we're happy. | |
3485 | */ | |
3486 | ||
bf4ef679 CM |
3487 | trans = btrfs_start_transaction(found_root, 1); |
3488 | ||
7d9eb12c CM |
3489 | if (found_root == extent_root->fs_info->extent_root || |
3490 | found_root == extent_root->fs_info->chunk_root || | |
3491 | found_root == extent_root->fs_info->dev_root) { | |
3492 | needs_lock = 1; | |
3493 | mutex_lock(&extent_root->fs_info->alloc_mutex); | |
3494 | } | |
3495 | ||
edbd8d4e | 3496 | path->lowest_level = level; |
8f662a76 | 3497 | path->reada = 2; |
edbd8d4e CM |
3498 | ret = btrfs_search_slot(trans, found_root, &found_key, path, |
3499 | 0, 1); | |
3500 | path->lowest_level = 0; | |
edbd8d4e | 3501 | btrfs_release_path(found_root, path); |
7d9eb12c | 3502 | |
0ef3e66b CM |
3503 | if (found_root == found_root->fs_info->extent_root) |
3504 | btrfs_extent_post_op(trans, found_root); | |
7d9eb12c CM |
3505 | if (needs_lock) |
3506 | mutex_unlock(&extent_root->fs_info->alloc_mutex); | |
3507 | ||
edbd8d4e | 3508 | btrfs_end_transaction(trans, found_root); |
edbd8d4e | 3509 | |
7d9eb12c | 3510 | } |
edbd8d4e | 3511 | out: |
7d9eb12c | 3512 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
edbd8d4e CM |
3513 | return 0; |
3514 | } | |
3515 | ||
a061fc8d CM |
3516 | static int noinline del_extent_zero(struct btrfs_root *extent_root, |
3517 | struct btrfs_path *path, | |
3518 | struct btrfs_key *extent_key) | |
3519 | { | |
3520 | int ret; | |
3521 | struct btrfs_trans_handle *trans; | |
3522 | ||
3523 | trans = btrfs_start_transaction(extent_root, 1); | |
3524 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); | |
3525 | if (ret > 0) { | |
3526 | ret = -EIO; | |
3527 | goto out; | |
3528 | } | |
3529 | if (ret < 0) | |
3530 | goto out; | |
3531 | ret = btrfs_del_item(trans, extent_root, path); | |
3532 | out: | |
3533 | btrfs_end_transaction(trans, extent_root); | |
3534 | return ret; | |
3535 | } | |
3536 | ||
98ed5174 CM |
3537 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
3538 | struct btrfs_path *path, | |
3539 | struct btrfs_key *extent_key) | |
edbd8d4e CM |
3540 | { |
3541 | struct btrfs_key key; | |
3542 | struct btrfs_key found_key; | |
edbd8d4e | 3543 | struct extent_buffer *leaf; |
0ef3e66b CM |
3544 | u64 last_file_objectid = 0; |
3545 | u64 last_file_root = 0; | |
3546 | u64 last_file_offset = (u64)-1; | |
3547 | u64 last_extent = 0; | |
edbd8d4e CM |
3548 | u32 nritems; |
3549 | u32 item_size; | |
3550 | int ret = 0; | |
3551 | ||
a061fc8d CM |
3552 | if (extent_key->objectid == 0) { |
3553 | ret = del_extent_zero(extent_root, path, extent_key); | |
3554 | goto out; | |
3555 | } | |
edbd8d4e CM |
3556 | key.objectid = extent_key->objectid; |
3557 | key.type = BTRFS_EXTENT_REF_KEY; | |
3558 | key.offset = 0; | |
3559 | ||
3560 | while(1) { | |
3561 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); | |
3562 | ||
edbd8d4e CM |
3563 | if (ret < 0) |
3564 | goto out; | |
3565 | ||
3566 | ret = 0; | |
3567 | leaf = path->nodes[0]; | |
3568 | nritems = btrfs_header_nritems(leaf); | |
a061fc8d CM |
3569 | if (path->slots[0] == nritems) { |
3570 | ret = btrfs_next_leaf(extent_root, path); | |
3571 | if (ret > 0) { | |
3572 | ret = 0; | |
3573 | goto out; | |
3574 | } | |
3575 | if (ret < 0) | |
3576 | goto out; | |
bf4ef679 | 3577 | leaf = path->nodes[0]; |
a061fc8d | 3578 | } |
edbd8d4e CM |
3579 | |
3580 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
a061fc8d | 3581 | if (found_key.objectid != extent_key->objectid) { |
edbd8d4e | 3582 | break; |
a061fc8d | 3583 | } |
edbd8d4e | 3584 | |
a061fc8d | 3585 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
edbd8d4e | 3586 | break; |
a061fc8d | 3587 | } |
edbd8d4e CM |
3588 | |
3589 | key.offset = found_key.offset + 1; | |
3590 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
3591 | ||
0ef3e66b CM |
3592 | ret = relocate_one_reference(extent_root, path, extent_key, |
3593 | &last_file_objectid, | |
3594 | &last_file_offset, | |
3595 | &last_file_root, last_extent); | |
edbd8d4e CM |
3596 | if (ret) |
3597 | goto out; | |
0ef3e66b | 3598 | last_extent = extent_key->objectid; |
edbd8d4e CM |
3599 | } |
3600 | ret = 0; | |
3601 | out: | |
3602 | btrfs_release_path(extent_root, path); | |
3603 | return ret; | |
3604 | } | |
3605 | ||
ec44a35c CM |
3606 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
3607 | { | |
3608 | u64 num_devices; | |
3609 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | | |
3610 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; | |
3611 | ||
a061fc8d | 3612 | num_devices = root->fs_info->fs_devices->num_devices; |
ec44a35c CM |
3613 | if (num_devices == 1) { |
3614 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
3615 | stripped = flags & ~stripped; | |
3616 | ||
3617 | /* turn raid0 into single device chunks */ | |
3618 | if (flags & BTRFS_BLOCK_GROUP_RAID0) | |
3619 | return stripped; | |
3620 | ||
3621 | /* turn mirroring into duplication */ | |
3622 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
3623 | BTRFS_BLOCK_GROUP_RAID10)) | |
3624 | return stripped | BTRFS_BLOCK_GROUP_DUP; | |
3625 | return flags; | |
3626 | } else { | |
3627 | /* they already had raid on here, just return */ | |
ec44a35c CM |
3628 | if (flags & stripped) |
3629 | return flags; | |
3630 | ||
3631 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
3632 | stripped = flags & ~stripped; | |
3633 | ||
3634 | /* switch duplicated blocks with raid1 */ | |
3635 | if (flags & BTRFS_BLOCK_GROUP_DUP) | |
3636 | return stripped | BTRFS_BLOCK_GROUP_RAID1; | |
3637 | ||
3638 | /* turn single device chunks into raid0 */ | |
3639 | return stripped | BTRFS_BLOCK_GROUP_RAID0; | |
3640 | } | |
3641 | return flags; | |
3642 | } | |
3643 | ||
0ef3e66b CM |
3644 | int __alloc_chunk_for_shrink(struct btrfs_root *root, |
3645 | struct btrfs_block_group_cache *shrink_block_group, | |
3646 | int force) | |
3647 | { | |
3648 | struct btrfs_trans_handle *trans; | |
3649 | u64 new_alloc_flags; | |
3650 | u64 calc; | |
3651 | ||
c286ac48 | 3652 | spin_lock(&shrink_block_group->lock); |
0ef3e66b | 3653 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
c286ac48 | 3654 | spin_unlock(&shrink_block_group->lock); |
7d9eb12c | 3655 | mutex_unlock(&root->fs_info->alloc_mutex); |
c286ac48 | 3656 | |
0ef3e66b | 3657 | trans = btrfs_start_transaction(root, 1); |
7d9eb12c | 3658 | mutex_lock(&root->fs_info->alloc_mutex); |
c286ac48 | 3659 | spin_lock(&shrink_block_group->lock); |
7d9eb12c | 3660 | |
0ef3e66b CM |
3661 | new_alloc_flags = update_block_group_flags(root, |
3662 | shrink_block_group->flags); | |
3663 | if (new_alloc_flags != shrink_block_group->flags) { | |
3664 | calc = | |
3665 | btrfs_block_group_used(&shrink_block_group->item); | |
3666 | } else { | |
3667 | calc = shrink_block_group->key.offset; | |
3668 | } | |
c286ac48 CM |
3669 | spin_unlock(&shrink_block_group->lock); |
3670 | ||
0ef3e66b CM |
3671 | do_chunk_alloc(trans, root->fs_info->extent_root, |
3672 | calc + 2 * 1024 * 1024, new_alloc_flags, force); | |
7d9eb12c CM |
3673 | |
3674 | mutex_unlock(&root->fs_info->alloc_mutex); | |
0ef3e66b | 3675 | btrfs_end_transaction(trans, root); |
7d9eb12c | 3676 | mutex_lock(&root->fs_info->alloc_mutex); |
c286ac48 CM |
3677 | } else |
3678 | spin_unlock(&shrink_block_group->lock); | |
0ef3e66b CM |
3679 | return 0; |
3680 | } | |
3681 | ||
8f18cf13 | 3682 | int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start) |
edbd8d4e CM |
3683 | { |
3684 | struct btrfs_trans_handle *trans; | |
3685 | struct btrfs_root *tree_root = root->fs_info->tree_root; | |
3686 | struct btrfs_path *path; | |
3687 | u64 cur_byte; | |
3688 | u64 total_found; | |
8f18cf13 CM |
3689 | u64 shrink_last_byte; |
3690 | struct btrfs_block_group_cache *shrink_block_group; | |
edbd8d4e | 3691 | struct btrfs_key key; |
73e48b27 | 3692 | struct btrfs_key found_key; |
edbd8d4e CM |
3693 | struct extent_buffer *leaf; |
3694 | u32 nritems; | |
3695 | int ret; | |
a061fc8d | 3696 | int progress; |
edbd8d4e | 3697 | |
925baedd | 3698 | mutex_lock(&root->fs_info->alloc_mutex); |
8f18cf13 CM |
3699 | shrink_block_group = btrfs_lookup_block_group(root->fs_info, |
3700 | shrink_start); | |
3701 | BUG_ON(!shrink_block_group); | |
3702 | ||
0ef3e66b CM |
3703 | shrink_last_byte = shrink_block_group->key.objectid + |
3704 | shrink_block_group->key.offset; | |
8f18cf13 CM |
3705 | |
3706 | shrink_block_group->space_info->total_bytes -= | |
3707 | shrink_block_group->key.offset; | |
edbd8d4e CM |
3708 | path = btrfs_alloc_path(); |
3709 | root = root->fs_info->extent_root; | |
8f662a76 | 3710 | path->reada = 2; |
edbd8d4e | 3711 | |
323da79c CM |
3712 | printk("btrfs relocating block group %llu flags %llu\n", |
3713 | (unsigned long long)shrink_start, | |
3714 | (unsigned long long)shrink_block_group->flags); | |
3715 | ||
0ef3e66b CM |
3716 | __alloc_chunk_for_shrink(root, shrink_block_group, 1); |
3717 | ||
edbd8d4e | 3718 | again: |
323da79c | 3719 | |
8f18cf13 CM |
3720 | shrink_block_group->ro = 1; |
3721 | ||
edbd8d4e | 3722 | total_found = 0; |
a061fc8d | 3723 | progress = 0; |
8f18cf13 | 3724 | key.objectid = shrink_start; |
edbd8d4e CM |
3725 | key.offset = 0; |
3726 | key.type = 0; | |
73e48b27 | 3727 | cur_byte = key.objectid; |
4313b399 | 3728 | |
ea8c2819 CM |
3729 | mutex_unlock(&root->fs_info->alloc_mutex); |
3730 | ||
3731 | btrfs_start_delalloc_inodes(root); | |
7ea394f1 | 3732 | btrfs_wait_ordered_extents(tree_root, 0); |
ea8c2819 CM |
3733 | |
3734 | mutex_lock(&root->fs_info->alloc_mutex); | |
3735 | ||
73e48b27 Y |
3736 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
3737 | if (ret < 0) | |
3738 | goto out; | |
3739 | ||
0b86a832 | 3740 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
73e48b27 Y |
3741 | if (ret < 0) |
3742 | goto out; | |
8f18cf13 | 3743 | |
73e48b27 Y |
3744 | if (ret == 0) { |
3745 | leaf = path->nodes[0]; | |
3746 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
8f18cf13 CM |
3747 | if (found_key.objectid + found_key.offset > shrink_start && |
3748 | found_key.objectid < shrink_last_byte) { | |
73e48b27 Y |
3749 | cur_byte = found_key.objectid; |
3750 | key.objectid = cur_byte; | |
3751 | } | |
3752 | } | |
3753 | btrfs_release_path(root, path); | |
3754 | ||
3755 | while(1) { | |
edbd8d4e CM |
3756 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
3757 | if (ret < 0) | |
3758 | goto out; | |
73e48b27 | 3759 | |
7d9eb12c | 3760 | next: |
edbd8d4e | 3761 | leaf = path->nodes[0]; |
73e48b27 | 3762 | nritems = btrfs_header_nritems(leaf); |
73e48b27 Y |
3763 | if (path->slots[0] >= nritems) { |
3764 | ret = btrfs_next_leaf(root, path); | |
3765 | if (ret < 0) | |
3766 | goto out; | |
3767 | if (ret == 1) { | |
3768 | ret = 0; | |
3769 | break; | |
edbd8d4e | 3770 | } |
73e48b27 Y |
3771 | leaf = path->nodes[0]; |
3772 | nritems = btrfs_header_nritems(leaf); | |
edbd8d4e | 3773 | } |
73e48b27 Y |
3774 | |
3775 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
725c8463 | 3776 | |
8f18cf13 CM |
3777 | if (found_key.objectid >= shrink_last_byte) |
3778 | break; | |
3779 | ||
725c8463 CM |
3780 | if (progress && need_resched()) { |
3781 | memcpy(&key, &found_key, sizeof(key)); | |
725c8463 | 3782 | cond_resched(); |
725c8463 CM |
3783 | btrfs_release_path(root, path); |
3784 | btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
3785 | progress = 0; | |
3786 | goto next; | |
3787 | } | |
3788 | progress = 1; | |
3789 | ||
73e48b27 Y |
3790 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY || |
3791 | found_key.objectid + found_key.offset <= cur_byte) { | |
0ef3e66b CM |
3792 | memcpy(&key, &found_key, sizeof(key)); |
3793 | key.offset++; | |
edbd8d4e | 3794 | path->slots[0]++; |
edbd8d4e CM |
3795 | goto next; |
3796 | } | |
73e48b27 | 3797 | |
edbd8d4e CM |
3798 | total_found++; |
3799 | cur_byte = found_key.objectid + found_key.offset; | |
3800 | key.objectid = cur_byte; | |
3801 | btrfs_release_path(root, path); | |
3802 | ret = relocate_one_extent(root, path, &found_key); | |
0ef3e66b | 3803 | __alloc_chunk_for_shrink(root, shrink_block_group, 0); |
edbd8d4e CM |
3804 | } |
3805 | ||
3806 | btrfs_release_path(root, path); | |
3807 | ||
3808 | if (total_found > 0) { | |
323da79c CM |
3809 | printk("btrfs relocate found %llu last extent was %llu\n", |
3810 | (unsigned long long)total_found, | |
3811 | (unsigned long long)found_key.objectid); | |
7d9eb12c | 3812 | mutex_unlock(&root->fs_info->alloc_mutex); |
edbd8d4e CM |
3813 | trans = btrfs_start_transaction(tree_root, 1); |
3814 | btrfs_commit_transaction(trans, tree_root); | |
3815 | ||
edbd8d4e | 3816 | btrfs_clean_old_snapshots(tree_root); |
edbd8d4e | 3817 | |
ea8c2819 | 3818 | btrfs_start_delalloc_inodes(root); |
7ea394f1 | 3819 | btrfs_wait_ordered_extents(tree_root, 0); |
3eaa2885 | 3820 | |
edbd8d4e CM |
3821 | trans = btrfs_start_transaction(tree_root, 1); |
3822 | btrfs_commit_transaction(trans, tree_root); | |
7d9eb12c | 3823 | mutex_lock(&root->fs_info->alloc_mutex); |
edbd8d4e CM |
3824 | goto again; |
3825 | } | |
3826 | ||
8f18cf13 CM |
3827 | /* |
3828 | * we've freed all the extents, now remove the block | |
3829 | * group item from the tree | |
3830 | */ | |
7d9eb12c CM |
3831 | mutex_unlock(&root->fs_info->alloc_mutex); |
3832 | ||
edbd8d4e | 3833 | trans = btrfs_start_transaction(root, 1); |
c286ac48 | 3834 | |
7d9eb12c | 3835 | mutex_lock(&root->fs_info->alloc_mutex); |
8f18cf13 | 3836 | memcpy(&key, &shrink_block_group->key, sizeof(key)); |
1372f8e6 | 3837 | |
8f18cf13 CM |
3838 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
3839 | if (ret > 0) | |
3840 | ret = -EIO; | |
8e8a1e31 JB |
3841 | if (ret < 0) { |
3842 | btrfs_end_transaction(trans, root); | |
8f18cf13 | 3843 | goto out; |
8e8a1e31 | 3844 | } |
73e48b27 | 3845 | |
0f9dd46c JB |
3846 | spin_lock(&root->fs_info->block_group_cache_lock); |
3847 | rb_erase(&shrink_block_group->cache_node, | |
3848 | &root->fs_info->block_group_cache_tree); | |
3849 | spin_unlock(&root->fs_info->block_group_cache_lock); | |
0ef3e66b | 3850 | |
0f9dd46c JB |
3851 | ret = btrfs_remove_free_space(shrink_block_group, key.objectid, |
3852 | key.offset); | |
3853 | if (ret) { | |
3854 | btrfs_end_transaction(trans, root); | |
3855 | goto out; | |
3856 | } | |
d7a029a8 | 3857 | /* |
0ef3e66b CM |
3858 | memset(shrink_block_group, 0, sizeof(*shrink_block_group)); |
3859 | kfree(shrink_block_group); | |
d7a029a8 | 3860 | */ |
0ef3e66b | 3861 | |
8f18cf13 | 3862 | btrfs_del_item(trans, root, path); |
7d9eb12c CM |
3863 | btrfs_release_path(root, path); |
3864 | mutex_unlock(&root->fs_info->alloc_mutex); | |
edbd8d4e | 3865 | btrfs_commit_transaction(trans, root); |
0ef3e66b | 3866 | |
7d9eb12c CM |
3867 | mutex_lock(&root->fs_info->alloc_mutex); |
3868 | ||
0ef3e66b CM |
3869 | /* the code to unpin extents might set a few bits in the free |
3870 | * space cache for this range again | |
3871 | */ | |
0f9dd46c JB |
3872 | /* XXX? */ |
3873 | ret = btrfs_remove_free_space(shrink_block_group, key.objectid, | |
3874 | key.offset); | |
edbd8d4e CM |
3875 | out: |
3876 | btrfs_free_path(path); | |
925baedd | 3877 | mutex_unlock(&root->fs_info->alloc_mutex); |
edbd8d4e CM |
3878 | return ret; |
3879 | } | |
3880 | ||
0b86a832 CM |
3881 | int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, |
3882 | struct btrfs_key *key) | |
3883 | { | |
925baedd | 3884 | int ret = 0; |
0b86a832 CM |
3885 | struct btrfs_key found_key; |
3886 | struct extent_buffer *leaf; | |
3887 | int slot; | |
edbd8d4e | 3888 | |
0b86a832 CM |
3889 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
3890 | if (ret < 0) | |
925baedd CM |
3891 | goto out; |
3892 | ||
0b86a832 CM |
3893 | while(1) { |
3894 | slot = path->slots[0]; | |
edbd8d4e | 3895 | leaf = path->nodes[0]; |
0b86a832 CM |
3896 | if (slot >= btrfs_header_nritems(leaf)) { |
3897 | ret = btrfs_next_leaf(root, path); | |
3898 | if (ret == 0) | |
3899 | continue; | |
3900 | if (ret < 0) | |
925baedd | 3901 | goto out; |
0b86a832 | 3902 | break; |
edbd8d4e | 3903 | } |
0b86a832 | 3904 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
edbd8d4e | 3905 | |
0b86a832 | 3906 | if (found_key.objectid >= key->objectid && |
925baedd CM |
3907 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
3908 | ret = 0; | |
3909 | goto out; | |
3910 | } | |
0b86a832 | 3911 | path->slots[0]++; |
edbd8d4e | 3912 | } |
0b86a832 | 3913 | ret = -ENOENT; |
925baedd | 3914 | out: |
0b86a832 | 3915 | return ret; |
edbd8d4e CM |
3916 | } |
3917 | ||
9078a3e1 CM |
3918 | int btrfs_read_block_groups(struct btrfs_root *root) |
3919 | { | |
3920 | struct btrfs_path *path; | |
3921 | int ret; | |
9078a3e1 | 3922 | struct btrfs_block_group_cache *cache; |
be744175 | 3923 | struct btrfs_fs_info *info = root->fs_info; |
6324fbf3 | 3924 | struct btrfs_space_info *space_info; |
9078a3e1 CM |
3925 | struct btrfs_key key; |
3926 | struct btrfs_key found_key; | |
5f39d397 | 3927 | struct extent_buffer *leaf; |
96b5179d | 3928 | |
be744175 | 3929 | root = info->extent_root; |
9078a3e1 | 3930 | key.objectid = 0; |
0b86a832 | 3931 | key.offset = 0; |
9078a3e1 | 3932 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
9078a3e1 CM |
3933 | path = btrfs_alloc_path(); |
3934 | if (!path) | |
3935 | return -ENOMEM; | |
3936 | ||
925baedd | 3937 | mutex_lock(&root->fs_info->alloc_mutex); |
9078a3e1 | 3938 | while(1) { |
0b86a832 CM |
3939 | ret = find_first_block_group(root, path, &key); |
3940 | if (ret > 0) { | |
3941 | ret = 0; | |
3942 | goto error; | |
9078a3e1 | 3943 | } |
0b86a832 CM |
3944 | if (ret != 0) |
3945 | goto error; | |
3946 | ||
5f39d397 CM |
3947 | leaf = path->nodes[0]; |
3948 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
8f18cf13 | 3949 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
9078a3e1 | 3950 | if (!cache) { |
0b86a832 | 3951 | ret = -ENOMEM; |
9078a3e1 CM |
3952 | break; |
3953 | } | |
3e1ad54f | 3954 | |
c286ac48 | 3955 | spin_lock_init(&cache->lock); |
0f9dd46c | 3956 | INIT_LIST_HEAD(&cache->list); |
5f39d397 CM |
3957 | read_extent_buffer(leaf, &cache->item, |
3958 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
3959 | sizeof(cache->item)); | |
9078a3e1 | 3960 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
0b86a832 | 3961 | |
9078a3e1 CM |
3962 | key.objectid = found_key.objectid + found_key.offset; |
3963 | btrfs_release_path(root, path); | |
0b86a832 | 3964 | cache->flags = btrfs_block_group_flags(&cache->item); |
96b5179d | 3965 | |
6324fbf3 CM |
3966 | ret = update_space_info(info, cache->flags, found_key.offset, |
3967 | btrfs_block_group_used(&cache->item), | |
3968 | &space_info); | |
3969 | BUG_ON(ret); | |
3970 | cache->space_info = space_info; | |
0f9dd46c JB |
3971 | spin_lock(&space_info->lock); |
3972 | list_add(&cache->list, &space_info->block_groups); | |
3973 | spin_unlock(&space_info->lock); | |
3974 | ||
3975 | ret = btrfs_add_block_group_cache(root->fs_info, cache); | |
3976 | BUG_ON(ret); | |
6324fbf3 | 3977 | |
9078a3e1 | 3978 | if (key.objectid >= |
db94535d | 3979 | btrfs_super_total_bytes(&info->super_copy)) |
9078a3e1 CM |
3980 | break; |
3981 | } | |
0b86a832 CM |
3982 | ret = 0; |
3983 | error: | |
9078a3e1 | 3984 | btrfs_free_path(path); |
925baedd | 3985 | mutex_unlock(&root->fs_info->alloc_mutex); |
0b86a832 | 3986 | return ret; |
9078a3e1 | 3987 | } |
6324fbf3 CM |
3988 | |
3989 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |
3990 | struct btrfs_root *root, u64 bytes_used, | |
e17cade2 | 3991 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
6324fbf3 CM |
3992 | u64 size) |
3993 | { | |
3994 | int ret; | |
6324fbf3 CM |
3995 | struct btrfs_root *extent_root; |
3996 | struct btrfs_block_group_cache *cache; | |
6324fbf3 | 3997 | |
7d9eb12c | 3998 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
6324fbf3 | 3999 | extent_root = root->fs_info->extent_root; |
6324fbf3 | 4000 | |
e02119d5 CM |
4001 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
4002 | ||
8f18cf13 | 4003 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
0f9dd46c JB |
4004 | if (!cache) |
4005 | return -ENOMEM; | |
4006 | ||
e17cade2 | 4007 | cache->key.objectid = chunk_offset; |
6324fbf3 | 4008 | cache->key.offset = size; |
c286ac48 | 4009 | spin_lock_init(&cache->lock); |
0f9dd46c | 4010 | INIT_LIST_HEAD(&cache->list); |
6324fbf3 | 4011 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
0ef3e66b | 4012 | |
6324fbf3 | 4013 | btrfs_set_block_group_used(&cache->item, bytes_used); |
6324fbf3 CM |
4014 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
4015 | cache->flags = type; | |
4016 | btrfs_set_block_group_flags(&cache->item, type); | |
4017 | ||
4018 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, | |
4019 | &cache->space_info); | |
4020 | BUG_ON(ret); | |
0f9dd46c JB |
4021 | spin_lock(&cache->space_info->lock); |
4022 | list_add(&cache->list, &cache->space_info->block_groups); | |
4023 | spin_unlock(&cache->space_info->lock); | |
6324fbf3 | 4024 | |
0f9dd46c JB |
4025 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
4026 | BUG_ON(ret); | |
c286ac48 | 4027 | |
6324fbf3 CM |
4028 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
4029 | sizeof(cache->item)); | |
4030 | BUG_ON(ret); | |
4031 | ||
4032 | finish_current_insert(trans, extent_root); | |
4033 | ret = del_pending_extents(trans, extent_root); | |
4034 | BUG_ON(ret); | |
d18a2c44 | 4035 | set_avail_alloc_bits(extent_root->fs_info, type); |
925baedd | 4036 | |
6324fbf3 CM |
4037 | return 0; |
4038 | } |