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