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