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