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