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