Btrfs: allocator fixes for space balancing update
[linux-2.6-block.git] / fs / btrfs / extent-tree.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
ec6b910f 18#include <linux/sched.h>
edbd8d4e 19#include <linux/pagemap.h>
ec44a35c 20#include <linux/writeback.h>
21af804c 21#include <linux/blkdev.h>
74493f7a 22#include "hash.h"
a5eb62e3 23#include "crc32c.h"
fec577fb
CM
24#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
e089f05c 27#include "transaction.h"
0b86a832 28#include "volumes.h"
925baedd 29#include "locking.h"
31153d81 30#include "ref-cache.h"
fec577fb 31
31840ae1
ZY
32#define PENDING_EXTENT_INSERT 0
33#define PENDING_EXTENT_DELETE 1
34#define PENDING_BACKREF_UPDATE 2
35
36struct pending_extent_op {
37 int type;
38 u64 bytenr;
39 u64 num_bytes;
40 u64 parent;
41 u64 orig_parent;
42 u64 generation;
43 u64 orig_generation;
44 int level;
45};
46
e089f05c
CM
47static int finish_current_insert(struct btrfs_trans_handle *trans, struct
48 btrfs_root *extent_root);
e20d96d6
CM
49static int del_pending_extents(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root);
925baedd
CM
51static struct btrfs_block_group_cache *
52__btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
d548ee51 55
925baedd
CM
56void maybe_lock_mutex(struct btrfs_root *root)
57{
58 if (root != root->fs_info->extent_root &&
59 root != root->fs_info->chunk_root &&
60 root != root->fs_info->dev_root) {
61 mutex_lock(&root->fs_info->alloc_mutex);
62 }
63}
64
65void maybe_unlock_mutex(struct btrfs_root *root)
66{
67 if (root != root->fs_info->extent_root &&
68 root != root->fs_info->chunk_root &&
69 root != root->fs_info->dev_root) {
70 mutex_unlock(&root->fs_info->alloc_mutex);
71 }
72}
fec577fb 73
0f9dd46c
JB
74static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
75{
76 return (cache->flags & bits) == bits;
77}
78
79/*
80 * this adds the block group to the fs_info rb tree for the block group
81 * cache
82 */
83int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
84 struct btrfs_block_group_cache *block_group)
85{
86 struct rb_node **p;
87 struct rb_node *parent = NULL;
88 struct btrfs_block_group_cache *cache;
89
90 spin_lock(&info->block_group_cache_lock);
91 p = &info->block_group_cache_tree.rb_node;
92
93 while (*p) {
94 parent = *p;
95 cache = rb_entry(parent, struct btrfs_block_group_cache,
96 cache_node);
97 if (block_group->key.objectid < cache->key.objectid) {
98 p = &(*p)->rb_left;
99 } else if (block_group->key.objectid > cache->key.objectid) {
100 p = &(*p)->rb_right;
101 } else {
102 spin_unlock(&info->block_group_cache_lock);
103 return -EEXIST;
104 }
105 }
106
107 rb_link_node(&block_group->cache_node, parent, p);
108 rb_insert_color(&block_group->cache_node,
109 &info->block_group_cache_tree);
110 spin_unlock(&info->block_group_cache_lock);
111
112 return 0;
113}
114
115/*
116 * This will return the block group at or after bytenr if contains is 0, else
117 * it will return the block group that contains the bytenr
118 */
119static struct btrfs_block_group_cache *
120block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
121 int contains)
122{
123 struct btrfs_block_group_cache *cache, *ret = NULL;
124 struct rb_node *n;
125 u64 end, start;
126
127 spin_lock(&info->block_group_cache_lock);
128 n = info->block_group_cache_tree.rb_node;
129
130 while (n) {
131 cache = rb_entry(n, struct btrfs_block_group_cache,
132 cache_node);
133 end = cache->key.objectid + cache->key.offset - 1;
134 start = cache->key.objectid;
135
136 if (bytenr < start) {
137 if (!contains && (!ret || start < ret->key.objectid))
138 ret = cache;
139 n = n->rb_left;
140 } else if (bytenr > start) {
141 if (contains && bytenr <= end) {
142 ret = cache;
143 break;
144 }
145 n = n->rb_right;
146 } else {
147 ret = cache;
148 break;
149 }
150 }
151 spin_unlock(&info->block_group_cache_lock);
152
153 return ret;
154}
155
156/*
157 * this is only called by cache_block_group, since we could have freed extents
158 * we need to check the pinned_extents for any extents that can't be used yet
159 * since their free space will be released as soon as the transaction commits.
160 */
161static int add_new_free_space(struct btrfs_block_group_cache *block_group,
162 struct btrfs_fs_info *info, u64 start, u64 end)
163{
164 u64 extent_start, extent_end, size;
165 int ret;
166
167 while (start < end) {
168 ret = find_first_extent_bit(&info->pinned_extents, start,
169 &extent_start, &extent_end,
170 EXTENT_DIRTY);
171 if (ret)
172 break;
173
174 if (extent_start == start) {
175 start = extent_end + 1;
176 } else if (extent_start > start && extent_start < end) {
177 size = extent_start - start;
178 ret = btrfs_add_free_space(block_group, start, size);
179 BUG_ON(ret);
180 start = extent_end + 1;
181 } else {
182 break;
183 }
184 }
185
186 if (start < end) {
187 size = end - start;
188 ret = btrfs_add_free_space(block_group, start, size);
189 BUG_ON(ret);
190 }
191
192 return 0;
193}
194
e37c9e69
CM
195static int cache_block_group(struct btrfs_root *root,
196 struct btrfs_block_group_cache *block_group)
197{
198 struct btrfs_path *path;
ef8bbdfe 199 int ret = 0;
e37c9e69 200 struct btrfs_key key;
5f39d397 201 struct extent_buffer *leaf;
e37c9e69 202 int slot;
e37c9e69 203 u64 last = 0;
7d7d6068 204 u64 first_free;
e37c9e69
CM
205 int found = 0;
206
00f5c795
CM
207 if (!block_group)
208 return 0;
209
e37c9e69 210 root = root->fs_info->extent_root;
e37c9e69
CM
211
212 if (block_group->cached)
213 return 0;
f510cfec 214
e37c9e69
CM
215 path = btrfs_alloc_path();
216 if (!path)
217 return -ENOMEM;
7d7d6068 218
2cc58cf2 219 path->reada = 2;
5cd57b2c
CM
220 /*
221 * we get into deadlocks with paths held by callers of this function.
222 * since the alloc_mutex is protecting things right now, just
223 * skip the locking here
224 */
225 path->skip_locking = 1;
0f9dd46c
JB
226 first_free = max_t(u64, block_group->key.objectid,
227 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
e37c9e69 228 key.objectid = block_group->key.objectid;
e37c9e69
CM
229 key.offset = 0;
230 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
231 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
232 if (ret < 0)
ef8bbdfe 233 goto err;
0b86a832 234 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
d548ee51 235 if (ret < 0)
ef8bbdfe 236 goto err;
d548ee51
Y
237 if (ret == 0) {
238 leaf = path->nodes[0];
239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
240 if (key.objectid + key.offset > first_free)
241 first_free = key.objectid + key.offset;
242 }
e37c9e69 243 while(1) {
5f39d397 244 leaf = path->nodes[0];
e37c9e69 245 slot = path->slots[0];
5f39d397 246 if (slot >= btrfs_header_nritems(leaf)) {
e37c9e69 247 ret = btrfs_next_leaf(root, path);
54aa1f4d
CM
248 if (ret < 0)
249 goto err;
0f9dd46c 250 if (ret == 0)
e37c9e69 251 continue;
0f9dd46c 252 else
e37c9e69 253 break;
e37c9e69 254 }
5f39d397 255 btrfs_item_key_to_cpu(leaf, &key, slot);
0f9dd46c 256 if (key.objectid < block_group->key.objectid)
7d7d6068 257 goto next;
0f9dd46c 258
e37c9e69 259 if (key.objectid >= block_group->key.objectid +
0f9dd46c 260 block_group->key.offset)
e37c9e69 261 break;
7d7d6068 262
e37c9e69
CM
263 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
264 if (!found) {
7d7d6068 265 last = first_free;
e37c9e69 266 found = 1;
e37c9e69 267 }
0f9dd46c
JB
268
269 add_new_free_space(block_group, root->fs_info, last,
270 key.objectid);
271
7d7d6068 272 last = key.objectid + key.offset;
e37c9e69 273 }
7d7d6068 274next:
e37c9e69
CM
275 path->slots[0]++;
276 }
277
7d7d6068
Y
278 if (!found)
279 last = first_free;
0f9dd46c
JB
280
281 add_new_free_space(block_group, root->fs_info, last,
282 block_group->key.objectid +
283 block_group->key.offset);
284
e37c9e69 285 block_group->cached = 1;
ef8bbdfe 286 ret = 0;
54aa1f4d 287err:
e37c9e69 288 btrfs_free_path(path);
ef8bbdfe 289 return ret;
e37c9e69
CM
290}
291
0f9dd46c
JB
292/*
293 * return the block group that starts at or after bytenr
294 */
0ef3e66b
CM
295struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
296 btrfs_fs_info *info,
297 u64 bytenr)
298{
0f9dd46c 299 struct btrfs_block_group_cache *cache;
0ef3e66b 300
0f9dd46c 301 cache = block_group_cache_tree_search(info, bytenr, 0);
0ef3e66b 302
0f9dd46c 303 return cache;
0ef3e66b
CM
304}
305
0f9dd46c
JB
306/*
307 * return the block group that contains teh given bytenr
308 */
5276aeda
CM
309struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
310 btrfs_fs_info *info,
db94535d 311 u64 bytenr)
be744175 312{
0f9dd46c 313 struct btrfs_block_group_cache *cache;
be744175 314
0f9dd46c 315 cache = block_group_cache_tree_search(info, bytenr, 1);
96b5179d 316
0f9dd46c 317 return cache;
be744175 318}
0b86a832 319
0f9dd46c
JB
320static int noinline find_free_space(struct btrfs_root *root,
321 struct btrfs_block_group_cache **cache_ret,
322 u64 *start_ret, u64 num, int data)
e37c9e69 323{
e37c9e69
CM
324 int ret;
325 struct btrfs_block_group_cache *cache = *cache_ret;
0f9dd46c 326 struct btrfs_free_space *info = NULL;
e19caa5f 327 u64 last;
0b86a832 328 u64 search_start = *start_ret;
e37c9e69 329
7d9eb12c 330 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
0ef3e66b
CM
331 if (!cache)
332 goto out;
333
0f9dd46c
JB
334 last = max(search_start, cache->key.objectid);
335
e37c9e69 336again:
54aa1f4d 337 ret = cache_block_group(root, cache);
0f9dd46c 338 if (ret)
54aa1f4d 339 goto out;
f84a8b36 340
0f9dd46c 341 if (cache->ro || !block_group_bits(cache, data))
0b86a832 342 goto new_group;
e19caa5f 343
0f9dd46c
JB
344 info = btrfs_find_free_space(cache, last, num);
345 if (info) {
346 *start_ret = info->offset;
0b86a832 347 return 0;
8790d502 348 }
e37c9e69
CM
349
350new_group:
e19caa5f 351 last = cache->key.objectid + cache->key.offset;
0f9dd46c 352
0ef3e66b 353 cache = btrfs_lookup_first_block_group(root->fs_info, last);
e8569813 354 if (!cache)
1a2b2ac7 355 goto out;
0f9dd46c 356
e37c9e69
CM
357 *cache_ret = cache;
358 goto again;
0f9dd46c
JB
359
360out:
361 return -ENOSPC;
e37c9e69
CM
362}
363
84f54cfa
CM
364static u64 div_factor(u64 num, int factor)
365{
257d0ce3
CM
366 if (factor == 10)
367 return num;
84f54cfa
CM
368 num *= factor;
369 do_div(num, 10);
370 return num;
371}
372
0f9dd46c
JB
373static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
374 u64 flags)
6324fbf3 375{
0f9dd46c
JB
376 struct list_head *head = &info->space_info;
377 struct list_head *cur;
378 struct btrfs_space_info *found;
379 list_for_each(cur, head) {
380 found = list_entry(cur, struct btrfs_space_info, list);
381 if (found->flags == flags)
382 return found;
383 }
384 return NULL;
6324fbf3
CM
385}
386
925baedd
CM
387static struct btrfs_block_group_cache *
388__btrfs_find_block_group(struct btrfs_root *root,
389 struct btrfs_block_group_cache *hint,
390 u64 search_start, int data, int owner)
cd1bc465 391{
96b5179d 392 struct btrfs_block_group_cache *cache;
31f3c99b 393 struct btrfs_block_group_cache *found_group = NULL;
cd1bc465
CM
394 struct btrfs_fs_info *info = root->fs_info;
395 u64 used;
31f3c99b 396 u64 last = 0;
96b5179d 397 u64 free_check;
31f3c99b 398 int full_search = 0;
bce4eae9 399 int factor = 10;
0ef3e66b 400 int wrapped = 0;
de428b63 401
a236aed1
CM
402 if (data & BTRFS_BLOCK_GROUP_METADATA)
403 factor = 9;
be744175 404
0ef3e66b 405 if (search_start) {
be744175 406 struct btrfs_block_group_cache *shint;
0ef3e66b 407 shint = btrfs_lookup_first_block_group(info, search_start);
8f18cf13 408 if (shint && block_group_bits(shint, data) && !shint->ro) {
c286ac48 409 spin_lock(&shint->lock);
be744175 410 used = btrfs_block_group_used(&shint->item);
e8569813 411 if (used + shint->pinned + shint->reserved <
324ae4df 412 div_factor(shint->key.offset, factor)) {
c286ac48 413 spin_unlock(&shint->lock);
be744175
CM
414 return shint;
415 }
c286ac48 416 spin_unlock(&shint->lock);
be744175
CM
417 }
418 }
0ef3e66b 419 if (hint && !hint->ro && block_group_bits(hint, data)) {
c286ac48 420 spin_lock(&hint->lock);
31f3c99b 421 used = btrfs_block_group_used(&hint->item);
e8569813 422 if (used + hint->pinned + hint->reserved <
324ae4df 423 div_factor(hint->key.offset, factor)) {
c286ac48 424 spin_unlock(&hint->lock);
31f3c99b
CM
425 return hint;
426 }
c286ac48 427 spin_unlock(&hint->lock);
e19caa5f 428 last = hint->key.objectid + hint->key.offset;
31f3c99b 429 } else {
e37c9e69 430 if (hint)
0ef3e66b 431 last = max(hint->key.objectid, search_start);
e37c9e69 432 else
0ef3e66b 433 last = search_start;
31f3c99b 434 }
31f3c99b 435again:
e8569813
ZY
436 while (1) {
437 cache = btrfs_lookup_first_block_group(root->fs_info, last);
0f9dd46c
JB
438 if (!cache)
439 break;
96b5179d 440
c286ac48 441 spin_lock(&cache->lock);
96b5179d
CM
442 last = cache->key.objectid + cache->key.offset;
443 used = btrfs_block_group_used(&cache->item);
444
8f18cf13 445 if (!cache->ro && block_group_bits(cache, data)) {
0ef3e66b 446 free_check = div_factor(cache->key.offset, factor);
e8569813
ZY
447 if (used + cache->pinned + cache->reserved <
448 free_check) {
8790d502 449 found_group = cache;
c286ac48 450 spin_unlock(&cache->lock);
8790d502
CM
451 goto found;
452 }
6324fbf3 453 }
c286ac48 454 spin_unlock(&cache->lock);
de428b63 455 cond_resched();
cd1bc465 456 }
0ef3e66b
CM
457 if (!wrapped) {
458 last = search_start;
459 wrapped = 1;
460 goto again;
461 }
462 if (!full_search && factor < 10) {
be744175 463 last = search_start;
31f3c99b 464 full_search = 1;
0ef3e66b 465 factor = 10;
31f3c99b
CM
466 goto again;
467 }
be744175 468found:
31f3c99b 469 return found_group;
cd1bc465
CM
470}
471
925baedd
CM
472struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
473 struct btrfs_block_group_cache
474 *hint, u64 search_start,
475 int data, int owner)
476{
477
478 struct btrfs_block_group_cache *ret;
925baedd 479 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
925baedd
CM
480 return ret;
481}
0f9dd46c 482
e02119d5 483/* simple helper to search for an existing extent at a given offset */
31840ae1 484int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
e02119d5
CM
485{
486 int ret;
487 struct btrfs_key key;
31840ae1 488 struct btrfs_path *path;
e02119d5 489
31840ae1
ZY
490 path = btrfs_alloc_path();
491 BUG_ON(!path);
e02119d5
CM
492 maybe_lock_mutex(root);
493 key.objectid = start;
494 key.offset = len;
495 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
496 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
497 0, 0);
498 maybe_unlock_mutex(root);
31840ae1 499 btrfs_free_path(path);
7bb86316
CM
500 return ret;
501}
502
d8d5f3e1
CM
503/*
504 * Back reference rules. Back refs have three main goals:
505 *
506 * 1) differentiate between all holders of references to an extent so that
507 * when a reference is dropped we can make sure it was a valid reference
508 * before freeing the extent.
509 *
510 * 2) Provide enough information to quickly find the holders of an extent
511 * if we notice a given block is corrupted or bad.
512 *
513 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
514 * maintenance. This is actually the same as #2, but with a slightly
515 * different use case.
516 *
517 * File extents can be referenced by:
518 *
519 * - multiple snapshots, subvolumes, or different generations in one subvol
31840ae1 520 * - different files inside a single subvolume
d8d5f3e1
CM
521 * - different offsets inside a file (bookend extents in file.c)
522 *
523 * The extent ref structure has fields for:
524 *
525 * - Objectid of the subvolume root
526 * - Generation number of the tree holding the reference
527 * - objectid of the file holding the reference
528 * - offset in the file corresponding to the key holding the reference
31840ae1
ZY
529 * - number of references holding by parent node (alway 1 for tree blocks)
530 *
531 * Btree leaf may hold multiple references to a file extent. In most cases,
532 * these references are from same file and the corresponding offsets inside
533 * the file are close together. So inode objectid and offset in file are
534 * just hints, they provide hints about where in the btree the references
535 * can be found and when we can stop searching.
d8d5f3e1
CM
536 *
537 * When a file extent is allocated the fields are filled in:
31840ae1 538 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
d8d5f3e1
CM
539 *
540 * When a leaf is cow'd new references are added for every file extent found
31840ae1
ZY
541 * in the leaf. It looks similar to the create case, but trans->transid will
542 * be different when the block is cow'd.
d8d5f3e1 543 *
31840ae1
ZY
544 * (root_key.objectid, trans->transid, inode objectid, offset in file,
545 * number of references in the leaf)
d8d5f3e1 546 *
31840ae1
ZY
547 * Because inode objectid and offset in file are just hints, they are not
548 * used when backrefs are deleted. When a file extent is removed either
549 * during snapshot deletion or file truncation, we find the corresponding
550 * back back reference and check the following fields.
d8d5f3e1 551 *
31840ae1 552 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
d8d5f3e1
CM
553 *
554 * Btree extents can be referenced by:
555 *
556 * - Different subvolumes
557 * - Different generations of the same subvolume
558 *
d8d5f3e1
CM
559 * When a tree block is created, back references are inserted:
560 *
31840ae1 561 * (root->root_key.objectid, trans->transid, level, 0, 1)
d8d5f3e1 562 *
31840ae1
ZY
563 * When a tree block is cow'd, new back references are added for all the
564 * blocks it points to. If the tree block isn't in reference counted root,
565 * the old back references are removed. These new back references are of
566 * the form (trans->transid will have increased since creation):
d8d5f3e1 567 *
31840ae1 568 * (root->root_key.objectid, trans->transid, level, 0, 1)
d8d5f3e1 569 *
31840ae1 570 * When a backref is in deleting, the following fields are checked:
d8d5f3e1
CM
571 *
572 * if backref was for a tree root:
31840ae1 573 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
d8d5f3e1 574 * else
31840ae1 575 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
d8d5f3e1 576 *
31840ae1 577 * Back Reference Key composing:
d8d5f3e1 578 *
31840ae1
ZY
579 * The key objectid corresponds to the first byte in the extent, the key
580 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
581 * byte of parent extent. If a extent is tree root, the key offset is set
582 * to the key objectid.
d8d5f3e1 583 */
31840ae1
ZY
584
585static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root,
587 struct btrfs_path *path, u64 bytenr,
588 u64 parent, u64 ref_root,
589 u64 ref_generation, int del)
7bb86316 590{
7bb86316 591 struct btrfs_key key;
31840ae1
ZY
592 struct btrfs_extent_ref *ref;
593 struct extent_buffer *leaf;
74493f7a
CM
594 int ret;
595
31840ae1
ZY
596 key.objectid = bytenr;
597 key.type = BTRFS_EXTENT_REF_KEY;
598 key.offset = parent;
599
600 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
601 if (ret < 0)
602 goto out;
603 if (ret > 0) {
604 ret = -ENOENT;
605 goto out;
606 }
607
608 leaf = path->nodes[0];
609 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
610 if (btrfs_ref_root(leaf, ref) != ref_root ||
611 btrfs_ref_generation(leaf, ref) != ref_generation) {
612 ret = -EIO;
613 WARN_ON(1);
614 goto out;
615 }
616 ret = 0;
617out:
618 return ret;
619}
620
621static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root,
623 struct btrfs_path *path,
624 u64 bytenr, u64 parent,
625 u64 ref_root, u64 ref_generation,
626 u64 owner_objectid, u64 owner_offset)
627{
628 struct btrfs_key key;
629 struct extent_buffer *leaf;
630 struct btrfs_extent_ref *ref;
631 u32 num_refs;
632 int ret;
74493f7a 633
74493f7a
CM
634 key.objectid = bytenr;
635 key.type = BTRFS_EXTENT_REF_KEY;
31840ae1 636 key.offset = parent;
74493f7a 637
31840ae1
ZY
638 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
639 if (ret == 0) {
640 leaf = path->nodes[0];
641 ref = btrfs_item_ptr(leaf, path->slots[0],
642 struct btrfs_extent_ref);
643 btrfs_set_ref_root(leaf, ref, ref_root);
644 btrfs_set_ref_generation(leaf, ref, ref_generation);
645 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
646 btrfs_set_ref_offset(leaf, ref, owner_offset);
647 btrfs_set_ref_num_refs(leaf, ref, 1);
648 } else if (ret == -EEXIST) {
649 u64 existing_owner;
650 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
651 leaf = path->nodes[0];
652 ref = btrfs_item_ptr(leaf, path->slots[0],
653 struct btrfs_extent_ref);
654 if (btrfs_ref_root(leaf, ref) != ref_root ||
655 btrfs_ref_generation(leaf, ref) != ref_generation) {
656 ret = -EIO;
657 WARN_ON(1);
7bb86316 658 goto out;
31840ae1
ZY
659 }
660
661 num_refs = btrfs_ref_num_refs(leaf, ref);
662 BUG_ON(num_refs == 0);
663 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
664
665 existing_owner = btrfs_ref_objectid(leaf, ref);
666 if (existing_owner == owner_objectid &&
667 btrfs_ref_offset(leaf, ref) > owner_offset) {
668 btrfs_set_ref_offset(leaf, ref, owner_offset);
669 } else if (existing_owner != owner_objectid &&
670 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
671 btrfs_set_ref_objectid(leaf, ref,
672 BTRFS_MULTIPLE_OBJECTIDS);
673 btrfs_set_ref_offset(leaf, ref, 0);
674 }
675 ret = 0;
676 } else {
7bb86316 677 goto out;
31840ae1 678 }
7bb86316
CM
679 btrfs_mark_buffer_dirty(path->nodes[0]);
680out:
681 btrfs_release_path(root, path);
682 return ret;
74493f7a
CM
683}
684
31840ae1
ZY
685static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
686 struct btrfs_root *root,
687 struct btrfs_path *path)
688{
689 struct extent_buffer *leaf;
690 struct btrfs_extent_ref *ref;
691 u32 num_refs;
692 int ret = 0;
693
694 leaf = path->nodes[0];
695 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
696 num_refs = btrfs_ref_num_refs(leaf, ref);
697 BUG_ON(num_refs == 0);
698 num_refs -= 1;
699 if (num_refs == 0) {
700 ret = btrfs_del_item(trans, root, path);
701 } else {
702 btrfs_set_ref_num_refs(leaf, ref, num_refs);
703 btrfs_mark_buffer_dirty(leaf);
704 }
705 btrfs_release_path(root, path);
706 return ret;
707}
708
709static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root, u64 bytenr,
711 u64 orig_parent, u64 parent,
712 u64 orig_root, u64 ref_root,
713 u64 orig_generation, u64 ref_generation,
714 u64 owner_objectid, u64 owner_offset)
715{
716 int ret;
717 struct btrfs_root *extent_root = root->fs_info->extent_root;
718 struct btrfs_path *path;
719
720 if (root == root->fs_info->extent_root) {
721 struct pending_extent_op *extent_op;
722 u64 num_bytes;
723
724 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
725 num_bytes = btrfs_level_size(root, (int)owner_objectid);
726 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
727 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
728 u64 priv;
729 ret = get_state_private(&root->fs_info->extent_ins,
730 bytenr, &priv);
731 BUG_ON(ret);
732 extent_op = (struct pending_extent_op *)
733 (unsigned long)priv;
734 BUG_ON(extent_op->parent != orig_parent);
735 BUG_ON(extent_op->generation != orig_generation);
736 extent_op->parent = parent;
737 extent_op->generation = ref_generation;
738 } else {
739 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
740 BUG_ON(!extent_op);
741
742 extent_op->type = PENDING_BACKREF_UPDATE;
743 extent_op->bytenr = bytenr;
744 extent_op->num_bytes = num_bytes;
745 extent_op->parent = parent;
746 extent_op->orig_parent = orig_parent;
747 extent_op->generation = ref_generation;
748 extent_op->orig_generation = orig_generation;
749 extent_op->level = (int)owner_objectid;
750
751 set_extent_bits(&root->fs_info->extent_ins,
752 bytenr, bytenr + num_bytes - 1,
753 EXTENT_LOCKED, GFP_NOFS);
754 set_state_private(&root->fs_info->extent_ins,
755 bytenr, (unsigned long)extent_op);
756 }
757 return 0;
758 }
759
760 path = btrfs_alloc_path();
761 if (!path)
762 return -ENOMEM;
763 ret = lookup_extent_backref(trans, extent_root, path,
764 bytenr, orig_parent, orig_root,
765 orig_generation, 1);
766 if (ret)
767 goto out;
768 ret = remove_extent_backref(trans, extent_root, path);
769 if (ret)
770 goto out;
771 ret = insert_extent_backref(trans, extent_root, path, bytenr,
772 parent, ref_root, ref_generation,
773 owner_objectid, owner_offset);
774 BUG_ON(ret);
775 finish_current_insert(trans, extent_root);
776 del_pending_extents(trans, extent_root);
777out:
778 btrfs_free_path(path);
779 return ret;
780}
781
782int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
783 struct btrfs_root *root, u64 bytenr,
784 u64 orig_parent, u64 parent,
785 u64 ref_root, u64 ref_generation,
786 u64 owner_objectid, u64 owner_offset)
787{
788 int ret;
789 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
790 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
791 return 0;
792 maybe_lock_mutex(root);
793 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
794 parent, ref_root, ref_root,
795 ref_generation, ref_generation,
796 owner_objectid, owner_offset);
797 maybe_unlock_mutex(root);
798 return ret;
799}
800
925baedd 801static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
31840ae1
ZY
802 struct btrfs_root *root, u64 bytenr,
803 u64 orig_parent, u64 parent,
804 u64 orig_root, u64 ref_root,
805 u64 orig_generation, u64 ref_generation,
806 u64 owner_objectid, u64 owner_offset)
02217ed2 807{
5caf2a00 808 struct btrfs_path *path;
02217ed2 809 int ret;
e2fa7227 810 struct btrfs_key key;
5f39d397 811 struct extent_buffer *l;
234b63a0 812 struct btrfs_extent_item *item;
cf27e1ee 813 u32 refs;
037e6390 814
5caf2a00 815 path = btrfs_alloc_path();
54aa1f4d
CM
816 if (!path)
817 return -ENOMEM;
26b8003f 818
3c12ac72 819 path->reada = 1;
db94535d 820 key.objectid = bytenr;
31840ae1
ZY
821 key.type = BTRFS_EXTENT_ITEM_KEY;
822 key.offset = (u64)-1;
823
5caf2a00 824 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 825 0, 1);
54aa1f4d
CM
826 if (ret < 0)
827 return ret;
31840ae1
ZY
828 BUG_ON(ret == 0 || path->slots[0] == 0);
829
830 path->slots[0]--;
5f39d397 831 l = path->nodes[0];
31840ae1
ZY
832
833 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
834 BUG_ON(key.objectid != bytenr);
835 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
836
5caf2a00 837 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397
CM
838 refs = btrfs_extent_refs(l, item);
839 btrfs_set_extent_refs(l, item, refs + 1);
5caf2a00 840 btrfs_mark_buffer_dirty(path->nodes[0]);
a28ec197 841
5caf2a00 842 btrfs_release_path(root->fs_info->extent_root, path);
7bb86316 843
3c12ac72 844 path->reada = 1;
31840ae1
ZY
845 ret = insert_extent_backref(trans, root->fs_info->extent_root,
846 path, bytenr, parent,
847 ref_root, ref_generation,
848 owner_objectid, owner_offset);
7bb86316 849 BUG_ON(ret);
9f5fae2f 850 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 851 del_pending_extents(trans, root->fs_info->extent_root);
74493f7a
CM
852
853 btrfs_free_path(path);
02217ed2
CM
854 return 0;
855}
856
925baedd 857int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
31840ae1
ZY
858 struct btrfs_root *root,
859 u64 bytenr, u64 num_bytes, u64 parent,
860 u64 ref_root, u64 ref_generation,
861 u64 owner_objectid, u64 owner_offset)
925baedd
CM
862{
863 int ret;
31840ae1
ZY
864 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
865 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
866 return 0;
867 maybe_lock_mutex(root);
868 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
869 0, ref_root, 0, ref_generation,
870 owner_objectid, owner_offset);
871 maybe_unlock_mutex(root);
925baedd
CM
872 return ret;
873}
874
e9d0b13b
CM
875int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root)
877{
878 finish_current_insert(trans, root->fs_info->extent_root);
879 del_pending_extents(trans, root->fs_info->extent_root);
880 return 0;
881}
882
31840ae1
ZY
883int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
884 struct btrfs_root *root, u64 bytenr,
885 u64 num_bytes, u32 *refs)
a28ec197 886{
5caf2a00 887 struct btrfs_path *path;
a28ec197 888 int ret;
e2fa7227 889 struct btrfs_key key;
5f39d397 890 struct extent_buffer *l;
234b63a0 891 struct btrfs_extent_item *item;
5caf2a00 892
db94535d 893 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 894 path = btrfs_alloc_path();
3c12ac72 895 path->reada = 1;
db94535d
CM
896 key.objectid = bytenr;
897 key.offset = num_bytes;
62e2749e 898 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
5caf2a00 899 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 900 0, 0);
54aa1f4d
CM
901 if (ret < 0)
902 goto out;
5f39d397
CM
903 if (ret != 0) {
904 btrfs_print_leaf(root, path->nodes[0]);
db94535d 905 printk("failed to find block number %Lu\n", bytenr);
a28ec197 906 BUG();
5f39d397
CM
907 }
908 l = path->nodes[0];
5caf2a00 909 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397 910 *refs = btrfs_extent_refs(l, item);
54aa1f4d 911out:
5caf2a00 912 btrfs_free_path(path);
a28ec197
CM
913 return 0;
914}
915
f321e491
YZ
916static int get_reference_status(struct btrfs_root *root, u64 bytenr,
917 u64 parent_gen, u64 ref_objectid,
918 u64 *min_generation, u32 *ref_count)
be20aa9d
CM
919{
920 struct btrfs_root *extent_root = root->fs_info->extent_root;
921 struct btrfs_path *path;
f321e491
YZ
922 struct extent_buffer *leaf;
923 struct btrfs_extent_ref *ref_item;
924 struct btrfs_key key;
925 struct btrfs_key found_key;
56b453c9 926 u64 root_objectid = root->root_key.objectid;
f321e491 927 u64 ref_generation;
be20aa9d
CM
928 u32 nritems;
929 int ret;
925baedd 930
be20aa9d 931 key.objectid = bytenr;
31840ae1 932 key.offset = (u64)-1;
f321e491 933 key.type = BTRFS_EXTENT_ITEM_KEY;
be20aa9d 934
f321e491
YZ
935 path = btrfs_alloc_path();
936 mutex_lock(&root->fs_info->alloc_mutex);
be20aa9d
CM
937 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
938 if (ret < 0)
939 goto out;
940 BUG_ON(ret == 0);
31840ae1
ZY
941 if (ret < 0 || path->slots[0] == 0)
942 goto out;
be20aa9d 943
31840ae1 944 path->slots[0]--;
f321e491
YZ
945 leaf = path->nodes[0];
946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
be20aa9d
CM
947
948 if (found_key.objectid != bytenr ||
949 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
f321e491 950 ret = 1;
be20aa9d
CM
951 goto out;
952 }
953
f321e491
YZ
954 *ref_count = 0;
955 *min_generation = (u64)-1;
956
be20aa9d 957 while (1) {
f321e491
YZ
958 leaf = path->nodes[0];
959 nritems = btrfs_header_nritems(leaf);
be20aa9d
CM
960 if (path->slots[0] >= nritems) {
961 ret = btrfs_next_leaf(extent_root, path);
f321e491
YZ
962 if (ret < 0)
963 goto out;
be20aa9d
CM
964 if (ret == 0)
965 continue;
966 break;
967 }
f321e491 968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
be20aa9d
CM
969 if (found_key.objectid != bytenr)
970 break;
bd09835d 971
be20aa9d
CM
972 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
973 path->slots[0]++;
974 continue;
975 }
976
f321e491 977 ref_item = btrfs_item_ptr(leaf, path->slots[0],
be20aa9d 978 struct btrfs_extent_ref);
f321e491
YZ
979 ref_generation = btrfs_ref_generation(leaf, ref_item);
980 /*
31840ae1 981 * For (parent_gen > 0 && parent_gen > ref_generation):
f321e491 982 *
bcc63abb
Y
983 * we reach here through the oldest root, therefore
984 * all other reference from same snapshot should have
f321e491
YZ
985 * a larger generation.
986 */
987 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
988 (parent_gen > 0 && parent_gen > ref_generation) ||
989 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
990 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
31840ae1 991 *ref_count = 2;
f321e491 992 break;
a68d5933 993 }
f321e491
YZ
994
995 *ref_count = 1;
996 if (*min_generation > ref_generation)
997 *min_generation = ref_generation;
998
be20aa9d
CM
999 path->slots[0]++;
1000 }
f321e491
YZ
1001 ret = 0;
1002out:
1003 mutex_unlock(&root->fs_info->alloc_mutex);
1004 btrfs_free_path(path);
1005 return ret;
1006}
1007
7ea394f1
YZ
1008int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1009 struct btrfs_root *root,
f321e491
YZ
1010 struct btrfs_key *key, u64 bytenr)
1011{
f321e491
YZ
1012 struct btrfs_root *old_root;
1013 struct btrfs_path *path = NULL;
1014 struct extent_buffer *eb;
1015 struct btrfs_file_extent_item *item;
1016 u64 ref_generation;
1017 u64 min_generation;
1018 u64 extent_start;
1019 u32 ref_count;
1020 int level;
1021 int ret;
1022
7ea394f1 1023 BUG_ON(trans == NULL);
f321e491
YZ
1024 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
1025 ret = get_reference_status(root, bytenr, 0, key->objectid,
1026 &min_generation, &ref_count);
1027 if (ret)
1028 return ret;
1029
1030 if (ref_count != 1)
1031 return 1;
1032
f321e491
YZ
1033 old_root = root->dirty_root->root;
1034 ref_generation = old_root->root_key.offset;
1035
1036 /* all references are created in running transaction */
1037 if (min_generation > ref_generation) {
1038 ret = 0;
bbaf549e
CM
1039 goto out;
1040 }
f321e491
YZ
1041
1042 path = btrfs_alloc_path();
1043 if (!path) {
1044 ret = -ENOMEM;
be20aa9d
CM
1045 goto out;
1046 }
f321e491
YZ
1047
1048 path->skip_locking = 1;
1049 /* if no item found, the extent is referenced by other snapshot */
1050 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
1051 if (ret)
be20aa9d 1052 goto out;
be20aa9d 1053
f321e491
YZ
1054 eb = path->nodes[0];
1055 item = btrfs_item_ptr(eb, path->slots[0],
1056 struct btrfs_file_extent_item);
1057 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
1058 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
1059 ret = 1;
1060 goto out;
1061 }
1062
1063 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
1064 if (level >= 0) {
1065 eb = path->nodes[level];
1066 if (!eb)
1067 continue;
1068 extent_start = eb->start;
bcc63abb 1069 } else
f321e491
YZ
1070 extent_start = bytenr;
1071
1072 ret = get_reference_status(root, extent_start, ref_generation,
1073 0, &min_generation, &ref_count);
1074 if (ret)
1075 goto out;
1076
1077 if (ref_count != 1) {
1078 ret = 1;
1079 goto out;
1080 }
1081 if (level >= 0)
1082 ref_generation = btrfs_header_generation(eb);
1083 }
1084 ret = 0;
be20aa9d 1085out:
f321e491
YZ
1086 if (path)
1087 btrfs_free_path(path);
f321e491 1088 return ret;
be20aa9d 1089}
c5739bba 1090
31840ae1
ZY
1091int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1092 struct extent_buffer *buf, u32 nr_extents)
02217ed2 1093{
5f39d397
CM
1094 u32 nritems;
1095 struct btrfs_key key;
6407bf6d 1096 struct btrfs_file_extent_item *fi;
02217ed2 1097 int i;
db94535d 1098 int level;
31840ae1 1099 int ret = 0;
a28ec197 1100
3768f368 1101 if (!root->ref_cows)
a28ec197 1102 return 0;
5f39d397 1103
db94535d 1104 level = btrfs_header_level(buf);
5f39d397 1105 nritems = btrfs_header_nritems(buf);
4a096752 1106
31840ae1 1107 if (level == 0) {
31153d81
YZ
1108 struct btrfs_leaf_ref *ref;
1109 struct btrfs_extent_info *info;
1110
31840ae1 1111 ref = btrfs_alloc_leaf_ref(root, nr_extents);
31153d81 1112 if (!ref) {
31840ae1 1113 ret = -ENOMEM;
31153d81
YZ
1114 goto out;
1115 }
1116
47ac14fa 1117 ref->root_gen = root->root_key.offset;
31153d81
YZ
1118 ref->bytenr = buf->start;
1119 ref->owner = btrfs_header_owner(buf);
1120 ref->generation = btrfs_header_generation(buf);
31840ae1 1121 ref->nritems = nr_extents;
31153d81 1122 info = ref->extents;
bcc63abb 1123
31840ae1 1124 for (i = 0; nr_extents > 0 && i < nritems; i++) {
31153d81
YZ
1125 u64 disk_bytenr;
1126 btrfs_item_key_to_cpu(buf, &key, i);
1127 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1128 continue;
1129 fi = btrfs_item_ptr(buf, i,
1130 struct btrfs_file_extent_item);
1131 if (btrfs_file_extent_type(buf, fi) ==
1132 BTRFS_FILE_EXTENT_INLINE)
1133 continue;
1134 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1135 if (disk_bytenr == 0)
1136 continue;
1137
1138 info->bytenr = disk_bytenr;
1139 info->num_bytes =
1140 btrfs_file_extent_disk_num_bytes(buf, fi);
1141 info->objectid = key.objectid;
1142 info->offset = key.offset;
1143 info++;
1144 }
1145
1146 BUG_ON(!root->ref_tree);
1147 ret = btrfs_add_leaf_ref(root, ref);
1148 WARN_ON(ret);
bcc63abb 1149 btrfs_free_leaf_ref(root, ref);
31153d81
YZ
1150 }
1151out:
31840ae1
ZY
1152 return ret;
1153}
1154
1155int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1156 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1157 u32 *nr_extents)
1158{
1159 u64 bytenr;
1160 u64 ref_root;
1161 u64 orig_root;
1162 u64 ref_generation;
1163 u64 orig_generation;
1164 u32 nritems;
1165 u32 nr_file_extents = 0;
1166 struct btrfs_key key;
1167 struct btrfs_file_extent_item *fi;
1168 int i;
1169 int level;
1170 int ret = 0;
1171 int faili = 0;
1172 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1173 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1174
1175 ref_root = btrfs_header_owner(buf);
1176 ref_generation = btrfs_header_generation(buf);
1177 orig_root = btrfs_header_owner(orig_buf);
1178 orig_generation = btrfs_header_generation(orig_buf);
1179
1180 nritems = btrfs_header_nritems(buf);
1181 level = btrfs_header_level(buf);
1182
1183 if (root->ref_cows) {
1184 process_func = __btrfs_inc_extent_ref;
1185 } else {
1186 if (level == 0 &&
1187 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1188 goto out;
1189 if (level != 0 &&
1190 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1191 goto out;
1192 process_func = __btrfs_update_extent_ref;
1193 }
1194
1195 for (i = 0; i < nritems; i++) {
1196 cond_resched();
db94535d 1197 if (level == 0) {
5f39d397
CM
1198 btrfs_item_key_to_cpu(buf, &key, i);
1199 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
54aa1f4d 1200 continue;
5f39d397 1201 fi = btrfs_item_ptr(buf, i,
54aa1f4d 1202 struct btrfs_file_extent_item);
5f39d397 1203 if (btrfs_file_extent_type(buf, fi) ==
54aa1f4d
CM
1204 BTRFS_FILE_EXTENT_INLINE)
1205 continue;
31840ae1
ZY
1206 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1207 if (bytenr == 0)
54aa1f4d 1208 continue;
31840ae1
ZY
1209
1210 nr_file_extents++;
1211
1212 maybe_lock_mutex(root);
1213 ret = process_func(trans, root, bytenr,
1214 orig_buf->start, buf->start,
1215 orig_root, ref_root,
1216 orig_generation, ref_generation,
1217 key.objectid, key.offset);
1218 maybe_unlock_mutex(root);
1219
1220 if (ret) {
1221 faili = i;
1222 WARN_ON(1);
1223 goto fail;
1224 }
54aa1f4d 1225 } else {
db94535d 1226 bytenr = btrfs_node_blockptr(buf, i);
31840ae1
ZY
1227 maybe_lock_mutex(root);
1228 ret = process_func(trans, root, bytenr,
1229 orig_buf->start, buf->start,
1230 orig_root, ref_root,
1231 orig_generation, ref_generation,
1232 level - 1, 0);
1233 maybe_unlock_mutex(root);
1234 if (ret) {
1235 faili = i;
1236 WARN_ON(1);
1237 goto fail;
1238 }
54aa1f4d
CM
1239 }
1240 }
31840ae1
ZY
1241out:
1242 if (nr_extents) {
1243 if (level == 0)
1244 *nr_extents = nr_file_extents;
1245 else
1246 *nr_extents = nritems;
1247 }
1248 return 0;
1249fail:
1250 WARN_ON(1);
54aa1f4d 1251 return ret;
02217ed2
CM
1252}
1253
31840ae1
ZY
1254int btrfs_update_ref(struct btrfs_trans_handle *trans,
1255 struct btrfs_root *root, struct extent_buffer *orig_buf,
1256 struct extent_buffer *buf, int start_slot, int nr)
1257
1258{
1259 u64 bytenr;
1260 u64 ref_root;
1261 u64 orig_root;
1262 u64 ref_generation;
1263 u64 orig_generation;
1264 struct btrfs_key key;
1265 struct btrfs_file_extent_item *fi;
1266 int i;
1267 int ret;
1268 int slot;
1269 int level;
1270
1271 BUG_ON(start_slot < 0);
1272 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1273
1274 ref_root = btrfs_header_owner(buf);
1275 ref_generation = btrfs_header_generation(buf);
1276 orig_root = btrfs_header_owner(orig_buf);
1277 orig_generation = btrfs_header_generation(orig_buf);
1278 level = btrfs_header_level(buf);
1279
1280 if (!root->ref_cows) {
1281 if (level == 0 &&
1282 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1283 return 0;
1284 if (level != 0 &&
1285 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1286 return 0;
1287 }
1288
1289 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1290 cond_resched();
1291 if (level == 0) {
1292 btrfs_item_key_to_cpu(buf, &key, slot);
1293 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1294 continue;
1295 fi = btrfs_item_ptr(buf, slot,
1296 struct btrfs_file_extent_item);
1297 if (btrfs_file_extent_type(buf, fi) ==
1298 BTRFS_FILE_EXTENT_INLINE)
1299 continue;
1300 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1301 if (bytenr == 0)
1302 continue;
1303 maybe_lock_mutex(root);
1304 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1305 orig_buf->start, buf->start,
1306 orig_root, ref_root,
1307 orig_generation, ref_generation,
1308 key.objectid, key.offset);
1309 maybe_unlock_mutex(root);
1310 if (ret)
1311 goto fail;
1312 } else {
1313 bytenr = btrfs_node_blockptr(buf, slot);
1314 maybe_lock_mutex(root);
1315 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1316 orig_buf->start, buf->start,
1317 orig_root, ref_root,
1318 orig_generation, ref_generation,
1319 level - 1, 0);
1320 maybe_unlock_mutex(root);
1321 if (ret)
1322 goto fail;
1323 }
1324 }
1325 return 0;
1326fail:
1327 WARN_ON(1);
1328 return -1;
1329}
1330
9078a3e1
CM
1331static int write_one_cache_group(struct btrfs_trans_handle *trans,
1332 struct btrfs_root *root,
1333 struct btrfs_path *path,
1334 struct btrfs_block_group_cache *cache)
1335{
1336 int ret;
1337 int pending_ret;
1338 struct btrfs_root *extent_root = root->fs_info->extent_root;
5f39d397
CM
1339 unsigned long bi;
1340 struct extent_buffer *leaf;
9078a3e1 1341
9078a3e1 1342 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
54aa1f4d
CM
1343 if (ret < 0)
1344 goto fail;
9078a3e1 1345 BUG_ON(ret);
5f39d397
CM
1346
1347 leaf = path->nodes[0];
1348 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1349 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1350 btrfs_mark_buffer_dirty(leaf);
9078a3e1 1351 btrfs_release_path(extent_root, path);
54aa1f4d 1352fail:
9078a3e1
CM
1353 finish_current_insert(trans, extent_root);
1354 pending_ret = del_pending_extents(trans, extent_root);
1355 if (ret)
1356 return ret;
1357 if (pending_ret)
1358 return pending_ret;
1359 return 0;
1360
1361}
1362
96b5179d
CM
1363int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1364 struct btrfs_root *root)
9078a3e1 1365{
0f9dd46c
JB
1366 struct btrfs_block_group_cache *cache, *entry;
1367 struct rb_node *n;
9078a3e1
CM
1368 int err = 0;
1369 int werr = 0;
9078a3e1 1370 struct btrfs_path *path;
96b5179d 1371 u64 last = 0;
9078a3e1
CM
1372
1373 path = btrfs_alloc_path();
1374 if (!path)
1375 return -ENOMEM;
1376
925baedd 1377 mutex_lock(&root->fs_info->alloc_mutex);
9078a3e1 1378 while(1) {
0f9dd46c
JB
1379 cache = NULL;
1380 spin_lock(&root->fs_info->block_group_cache_lock);
1381 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1382 n; n = rb_next(n)) {
1383 entry = rb_entry(n, struct btrfs_block_group_cache,
1384 cache_node);
1385 if (entry->dirty) {
1386 cache = entry;
1387 break;
1388 }
1389 }
1390 spin_unlock(&root->fs_info->block_group_cache_lock);
54aa1f4d 1391
0f9dd46c 1392 if (!cache)
96b5179d 1393 break;
0f9dd46c 1394
e8569813 1395 cache->dirty = 0;
0f9dd46c
JB
1396 last += cache->key.offset;
1397
96b5179d
CM
1398 err = write_one_cache_group(trans, root,
1399 path, cache);
1400 /*
1401 * if we fail to write the cache group, we want
1402 * to keep it marked dirty in hopes that a later
1403 * write will work
1404 */
1405 if (err) {
1406 werr = err;
1407 continue;
9078a3e1
CM
1408 }
1409 }
1410 btrfs_free_path(path);
925baedd 1411 mutex_unlock(&root->fs_info->alloc_mutex);
9078a3e1
CM
1412 return werr;
1413}
1414
593060d7
CM
1415static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1416 u64 total_bytes, u64 bytes_used,
1417 struct btrfs_space_info **space_info)
1418{
1419 struct btrfs_space_info *found;
1420
1421 found = __find_space_info(info, flags);
1422 if (found) {
1423 found->total_bytes += total_bytes;
1424 found->bytes_used += bytes_used;
8f18cf13 1425 found->full = 0;
593060d7
CM
1426 *space_info = found;
1427 return 0;
1428 }
1429 found = kmalloc(sizeof(*found), GFP_NOFS);
1430 if (!found)
1431 return -ENOMEM;
1432
1433 list_add(&found->list, &info->space_info);
0f9dd46c
JB
1434 INIT_LIST_HEAD(&found->block_groups);
1435 spin_lock_init(&found->lock);
593060d7
CM
1436 found->flags = flags;
1437 found->total_bytes = total_bytes;
1438 found->bytes_used = bytes_used;
1439 found->bytes_pinned = 0;
e8569813 1440 found->bytes_reserved = 0;
593060d7 1441 found->full = 0;
0ef3e66b 1442 found->force_alloc = 0;
593060d7
CM
1443 *space_info = found;
1444 return 0;
1445}
1446
8790d502
CM
1447static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1448{
1449 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
611f0e00 1450 BTRFS_BLOCK_GROUP_RAID1 |
321aecc6 1451 BTRFS_BLOCK_GROUP_RAID10 |
611f0e00 1452 BTRFS_BLOCK_GROUP_DUP);
8790d502
CM
1453 if (extra_flags) {
1454 if (flags & BTRFS_BLOCK_GROUP_DATA)
1455 fs_info->avail_data_alloc_bits |= extra_flags;
1456 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1457 fs_info->avail_metadata_alloc_bits |= extra_flags;
1458 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1459 fs_info->avail_system_alloc_bits |= extra_flags;
1460 }
1461}
593060d7 1462
a061fc8d 1463static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
ec44a35c 1464{
a061fc8d
CM
1465 u64 num_devices = root->fs_info->fs_devices->num_devices;
1466
1467 if (num_devices == 1)
1468 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1469 if (num_devices < 4)
1470 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1471
ec44a35c
CM
1472 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1473 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
a061fc8d 1474 BTRFS_BLOCK_GROUP_RAID10))) {
ec44a35c 1475 flags &= ~BTRFS_BLOCK_GROUP_DUP;
a061fc8d 1476 }
ec44a35c
CM
1477
1478 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
a061fc8d 1479 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
ec44a35c 1480 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
a061fc8d 1481 }
ec44a35c
CM
1482
1483 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1484 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1485 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1486 (flags & BTRFS_BLOCK_GROUP_DUP)))
1487 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1488 return flags;
1489}
1490
6324fbf3
CM
1491static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1492 struct btrfs_root *extent_root, u64 alloc_bytes,
0ef3e66b 1493 u64 flags, int force)
6324fbf3
CM
1494{
1495 struct btrfs_space_info *space_info;
1496 u64 thresh;
1497 u64 start;
1498 u64 num_bytes;
0f9dd46c 1499 int ret = 0;
6324fbf3 1500
a061fc8d 1501 flags = reduce_alloc_profile(extent_root, flags);
ec44a35c 1502
6324fbf3 1503 space_info = __find_space_info(extent_root->fs_info, flags);
593060d7
CM
1504 if (!space_info) {
1505 ret = update_space_info(extent_root->fs_info, flags,
1506 0, 0, &space_info);
1507 BUG_ON(ret);
1508 }
6324fbf3
CM
1509 BUG_ON(!space_info);
1510
0ef3e66b
CM
1511 if (space_info->force_alloc) {
1512 force = 1;
1513 space_info->force_alloc = 0;
1514 }
6324fbf3 1515 if (space_info->full)
925baedd 1516 goto out;
6324fbf3 1517
8790d502 1518 thresh = div_factor(space_info->total_bytes, 6);
0ef3e66b 1519 if (!force &&
e8569813
ZY
1520 (space_info->bytes_used + space_info->bytes_pinned +
1521 space_info->bytes_reserved + alloc_bytes) < thresh)
925baedd 1522 goto out;
6324fbf3 1523
925baedd 1524 mutex_lock(&extent_root->fs_info->chunk_mutex);
6324fbf3
CM
1525 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1526 if (ret == -ENOSPC) {
1527printk("space info full %Lu\n", flags);
1528 space_info->full = 1;
a74a4b97 1529 goto out_unlock;
6324fbf3 1530 }
6324fbf3
CM
1531 BUG_ON(ret);
1532
1533 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
e17cade2 1534 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
6324fbf3 1535 BUG_ON(ret);
0f9dd46c 1536
a74a4b97 1537out_unlock:
333db94c 1538 mutex_unlock(&extent_root->fs_info->chunk_mutex);
a74a4b97 1539out:
0f9dd46c 1540 return ret;
6324fbf3
CM
1541}
1542
9078a3e1
CM
1543static int update_block_group(struct btrfs_trans_handle *trans,
1544 struct btrfs_root *root,
db94535d 1545 u64 bytenr, u64 num_bytes, int alloc,
0b86a832 1546 int mark_free)
9078a3e1
CM
1547{
1548 struct btrfs_block_group_cache *cache;
1549 struct btrfs_fs_info *info = root->fs_info;
db94535d 1550 u64 total = num_bytes;
9078a3e1 1551 u64 old_val;
db94535d 1552 u64 byte_in_group;
3e1ad54f 1553
7d9eb12c 1554 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
9078a3e1 1555 while(total) {
db94535d 1556 cache = btrfs_lookup_block_group(info, bytenr);
3e1ad54f 1557 if (!cache) {
9078a3e1 1558 return -1;
cd1bc465 1559 }
db94535d
CM
1560 byte_in_group = bytenr - cache->key.objectid;
1561 WARN_ON(byte_in_group > cache->key.offset);
9078a3e1 1562
c286ac48 1563 spin_lock(&cache->lock);
0f9dd46c 1564 cache->dirty = 1;
9078a3e1 1565 old_val = btrfs_block_group_used(&cache->item);
db94535d 1566 num_bytes = min(total, cache->key.offset - byte_in_group);
cd1bc465 1567 if (alloc) {
db94535d 1568 old_val += num_bytes;
6324fbf3 1569 cache->space_info->bytes_used += num_bytes;
c286ac48
CM
1570 btrfs_set_block_group_used(&cache->item, old_val);
1571 spin_unlock(&cache->lock);
cd1bc465 1572 } else {
db94535d 1573 old_val -= num_bytes;
6324fbf3 1574 cache->space_info->bytes_used -= num_bytes;
c286ac48
CM
1575 btrfs_set_block_group_used(&cache->item, old_val);
1576 spin_unlock(&cache->lock);
f510cfec 1577 if (mark_free) {
0f9dd46c
JB
1578 int ret;
1579 ret = btrfs_add_free_space(cache, bytenr,
1580 num_bytes);
1581 if (ret)
1582 return -1;
e37c9e69 1583 }
cd1bc465 1584 }
db94535d
CM
1585 total -= num_bytes;
1586 bytenr += num_bytes;
9078a3e1
CM
1587 }
1588 return 0;
1589}
6324fbf3 1590
a061fc8d
CM
1591static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1592{
0f9dd46c
JB
1593 struct btrfs_block_group_cache *cache;
1594
1595 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1596 if (!cache)
a061fc8d 1597 return 0;
0f9dd46c
JB
1598
1599 return cache->key.objectid;
a061fc8d
CM
1600}
1601
e02119d5 1602int btrfs_update_pinned_extents(struct btrfs_root *root,
324ae4df
Y
1603 u64 bytenr, u64 num, int pin)
1604{
1605 u64 len;
1606 struct btrfs_block_group_cache *cache;
1607 struct btrfs_fs_info *fs_info = root->fs_info;
1608
7d9eb12c 1609 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
324ae4df
Y
1610 if (pin) {
1611 set_extent_dirty(&fs_info->pinned_extents,
1612 bytenr, bytenr + num - 1, GFP_NOFS);
1613 } else {
1614 clear_extent_dirty(&fs_info->pinned_extents,
1615 bytenr, bytenr + num - 1, GFP_NOFS);
1616 }
1617 while (num > 0) {
1618 cache = btrfs_lookup_block_group(fs_info, bytenr);
e8569813
ZY
1619 BUG_ON(!cache);
1620 len = min(num, cache->key.offset -
1621 (bytenr - cache->key.objectid));
324ae4df 1622 if (pin) {
e8569813
ZY
1623 spin_lock(&cache->lock);
1624 cache->pinned += len;
1625 cache->space_info->bytes_pinned += len;
1626 spin_unlock(&cache->lock);
324ae4df
Y
1627 fs_info->total_pinned += len;
1628 } else {
e8569813
ZY
1629 spin_lock(&cache->lock);
1630 cache->pinned -= len;
1631 cache->space_info->bytes_pinned -= len;
1632 spin_unlock(&cache->lock);
324ae4df
Y
1633 fs_info->total_pinned -= len;
1634 }
1635 bytenr += len;
1636 num -= len;
1637 }
1638 return 0;
1639}
9078a3e1 1640
e8569813
ZY
1641static int update_reserved_extents(struct btrfs_root *root,
1642 u64 bytenr, u64 num, int reserve)
1643{
1644 u64 len;
1645 struct btrfs_block_group_cache *cache;
1646 struct btrfs_fs_info *fs_info = root->fs_info;
1647
1648 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1649 while (num > 0) {
1650 cache = btrfs_lookup_block_group(fs_info, bytenr);
1651 BUG_ON(!cache);
1652 len = min(num, cache->key.offset -
1653 (bytenr - cache->key.objectid));
1654 if (reserve) {
1655 spin_lock(&cache->lock);
1656 cache->reserved += len;
1657 cache->space_info->bytes_reserved += len;
1658 spin_unlock(&cache->lock);
1659 } else {
1660 spin_lock(&cache->lock);
1661 cache->reserved -= len;
1662 cache->space_info->bytes_reserved -= len;
1663 spin_unlock(&cache->lock);
1664 }
1665 bytenr += len;
1666 num -= len;
1667 }
1668 return 0;
1669}
1670
d1310b2e 1671int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
ccd467d6 1672{
ccd467d6 1673 u64 last = 0;
1a5bc167
CM
1674 u64 start;
1675 u64 end;
d1310b2e 1676 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
ccd467d6 1677 int ret;
ccd467d6
CM
1678
1679 while(1) {
1a5bc167
CM
1680 ret = find_first_extent_bit(pinned_extents, last,
1681 &start, &end, EXTENT_DIRTY);
1682 if (ret)
ccd467d6 1683 break;
1a5bc167
CM
1684 set_extent_dirty(copy, start, end, GFP_NOFS);
1685 last = end + 1;
ccd467d6
CM
1686 }
1687 return 0;
1688}
1689
1690int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1691 struct btrfs_root *root,
d1310b2e 1692 struct extent_io_tree *unpin)
a28ec197 1693{
1a5bc167
CM
1694 u64 start;
1695 u64 end;
a28ec197 1696 int ret;
0f9dd46c 1697 struct btrfs_block_group_cache *cache;
a28ec197 1698
925baedd 1699 mutex_lock(&root->fs_info->alloc_mutex);
a28ec197 1700 while(1) {
1a5bc167
CM
1701 ret = find_first_extent_bit(unpin, 0, &start, &end,
1702 EXTENT_DIRTY);
1703 if (ret)
a28ec197 1704 break;
e02119d5 1705 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
1a5bc167 1706 clear_extent_dirty(unpin, start, end, GFP_NOFS);
0f9dd46c
JB
1707 cache = btrfs_lookup_block_group(root->fs_info, start);
1708 if (cache->cached)
1709 btrfs_add_free_space(cache, start, end - start + 1);
c286ac48
CM
1710 if (need_resched()) {
1711 mutex_unlock(&root->fs_info->alloc_mutex);
1712 cond_resched();
1713 mutex_lock(&root->fs_info->alloc_mutex);
1714 }
a28ec197 1715 }
925baedd 1716 mutex_unlock(&root->fs_info->alloc_mutex);
a28ec197
CM
1717 return 0;
1718}
1719
98ed5174
CM
1720static int finish_current_insert(struct btrfs_trans_handle *trans,
1721 struct btrfs_root *extent_root)
037e6390 1722{
7bb86316
CM
1723 u64 start;
1724 u64 end;
31840ae1 1725 u64 priv;
7bb86316
CM
1726 struct btrfs_fs_info *info = extent_root->fs_info;
1727 struct btrfs_path *path;
31840ae1
ZY
1728 struct btrfs_extent_ref *ref;
1729 struct pending_extent_op *extent_op;
1730 struct btrfs_key key;
234b63a0 1731 struct btrfs_extent_item extent_item;
037e6390 1732 int ret;
1a5bc167 1733 int err = 0;
037e6390 1734
7d9eb12c 1735 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
5f39d397 1736 btrfs_set_stack_extent_refs(&extent_item, 1);
7bb86316 1737 path = btrfs_alloc_path();
037e6390 1738
26b8003f 1739 while(1) {
1a5bc167
CM
1740 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1741 &end, EXTENT_LOCKED);
1742 if (ret)
26b8003f
CM
1743 break;
1744
31840ae1
ZY
1745 ret = get_state_private(&info->extent_ins, start, &priv);
1746 BUG_ON(ret);
1747 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1748
1749 if (extent_op->type == PENDING_EXTENT_INSERT) {
1750 key.objectid = start;
1751 key.offset = end + 1 - start;
1752 key.type = BTRFS_EXTENT_ITEM_KEY;
1753 err = btrfs_insert_item(trans, extent_root, &key,
1a5bc167 1754 &extent_item, sizeof(extent_item));
31840ae1 1755 BUG_ON(err);
c286ac48 1756
31840ae1
ZY
1757 clear_extent_bits(&info->extent_ins, start, end,
1758 EXTENT_LOCKED, GFP_NOFS);
c286ac48 1759
31840ae1
ZY
1760 err = insert_extent_backref(trans, extent_root, path,
1761 start, extent_op->parent,
1762 extent_root->root_key.objectid,
1763 extent_op->generation,
1764 extent_op->level, 0);
1765 BUG_ON(err);
1766 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1767 err = lookup_extent_backref(trans, extent_root, path,
1768 start, extent_op->orig_parent,
1769 extent_root->root_key.objectid,
1770 extent_op->orig_generation, 0);
1771 BUG_ON(err);
c286ac48 1772
31840ae1
ZY
1773 clear_extent_bits(&info->extent_ins, start, end,
1774 EXTENT_LOCKED, GFP_NOFS);
1775
1776 key.objectid = start;
1777 key.offset = extent_op->parent;
1778 key.type = BTRFS_EXTENT_REF_KEY;
1779 err = btrfs_set_item_key_safe(trans, extent_root, path,
1780 &key);
1781 BUG_ON(err);
1782 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1783 struct btrfs_extent_ref);
1784 btrfs_set_ref_generation(path->nodes[0], ref,
1785 extent_op->generation);
1786 btrfs_mark_buffer_dirty(path->nodes[0]);
1787 btrfs_release_path(extent_root, path);
d8d5f3e1 1788 } else {
31840ae1 1789 BUG_ON(1);
d8d5f3e1 1790 }
31840ae1
ZY
1791 kfree(extent_op);
1792
c286ac48
CM
1793 if (need_resched()) {
1794 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1795 cond_resched();
1796 mutex_lock(&extent_root->fs_info->alloc_mutex);
1797 }
037e6390 1798 }
7bb86316 1799 btrfs_free_path(path);
037e6390
CM
1800 return 0;
1801}
1802
31840ae1
ZY
1803static int pin_down_bytes(struct btrfs_trans_handle *trans,
1804 struct btrfs_root *root,
1805 u64 bytenr, u64 num_bytes, int is_data)
e20d96d6 1806{
1a5bc167 1807 int err = 0;
31840ae1 1808 struct extent_buffer *buf;
8ef97622 1809
7d9eb12c 1810 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
31840ae1
ZY
1811 if (is_data)
1812 goto pinit;
1813
1814 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1815 if (!buf)
1816 goto pinit;
1817
1818 /* we can reuse a block if it hasn't been written
1819 * and it is from this transaction. We can't
1820 * reuse anything from the tree log root because
1821 * it has tiny sub-transactions.
1822 */
1823 if (btrfs_buffer_uptodate(buf, 0) &&
1824 btrfs_try_tree_lock(buf)) {
1825 u64 header_owner = btrfs_header_owner(buf);
1826 u64 header_transid = btrfs_header_generation(buf);
1827 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1828 header_transid == trans->transid &&
1829 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1830 clean_tree_block(NULL, root, buf);
1831 btrfs_tree_unlock(buf);
5f39d397 1832 free_extent_buffer(buf);
31840ae1 1833 return 1;
8ef97622 1834 }
31840ae1 1835 btrfs_tree_unlock(buf);
f4b9aa8d 1836 }
31840ae1
ZY
1837 free_extent_buffer(buf);
1838pinit:
1839 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1840
be744175 1841 BUG_ON(err < 0);
e20d96d6
CM
1842 return 0;
1843}
1844
fec577fb 1845/*
a28ec197 1846 * remove an extent from the root, returns 0 on success
fec577fb 1847 */
31840ae1
ZY
1848static int __free_extent(struct btrfs_trans_handle *trans,
1849 struct btrfs_root *root,
1850 u64 bytenr, u64 num_bytes, u64 parent,
7bb86316 1851 u64 root_objectid, u64 ref_generation,
31840ae1
ZY
1852 u64 owner_objectid, u64 owner_offset,
1853 int pin, int mark_free)
a28ec197 1854{
5caf2a00 1855 struct btrfs_path *path;
e2fa7227 1856 struct btrfs_key key;
1261ec42
CM
1857 struct btrfs_fs_info *info = root->fs_info;
1858 struct btrfs_root *extent_root = info->extent_root;
5f39d397 1859 struct extent_buffer *leaf;
a28ec197 1860 int ret;
952fccac
CM
1861 int extent_slot = 0;
1862 int found_extent = 0;
1863 int num_to_del = 1;
234b63a0 1864 struct btrfs_extent_item *ei;
cf27e1ee 1865 u32 refs;
037e6390 1866
7d9eb12c 1867 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
db94535d 1868 key.objectid = bytenr;
62e2749e 1869 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 1870 key.offset = num_bytes;
5caf2a00 1871 path = btrfs_alloc_path();
54aa1f4d
CM
1872 if (!path)
1873 return -ENOMEM;
5f26f772 1874
3c12ac72 1875 path->reada = 1;
31840ae1
ZY
1876 ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent,
1877 root_objectid, ref_generation, 1);
7bb86316 1878 if (ret == 0) {
952fccac
CM
1879 struct btrfs_key found_key;
1880 extent_slot = path->slots[0];
1881 while(extent_slot > 0) {
1882 extent_slot--;
1883 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1884 extent_slot);
1885 if (found_key.objectid != bytenr)
1886 break;
1887 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1888 found_key.offset == num_bytes) {
1889 found_extent = 1;
1890 break;
1891 }
1892 if (path->slots[0] - extent_slot > 5)
1893 break;
1894 }
31840ae1
ZY
1895 if (!found_extent) {
1896 ret = remove_extent_backref(trans, extent_root, path);
1897 BUG_ON(ret);
1898 btrfs_release_path(extent_root, path);
1899 ret = btrfs_search_slot(trans, extent_root,
1900 &key, path, -1, 1);
1901 BUG_ON(ret);
1902 extent_slot = path->slots[0];
1903 }
7bb86316
CM
1904 } else {
1905 btrfs_print_leaf(extent_root, path->nodes[0]);
1906 WARN_ON(1);
1907 printk("Unable to find ref byte nr %Lu root %Lu "
1908 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1909 root_objectid, ref_generation, owner_objectid,
1910 owner_offset);
1911 }
5f39d397
CM
1912
1913 leaf = path->nodes[0];
952fccac 1914 ei = btrfs_item_ptr(leaf, extent_slot,
123abc88 1915 struct btrfs_extent_item);
5f39d397
CM
1916 refs = btrfs_extent_refs(leaf, ei);
1917 BUG_ON(refs == 0);
1918 refs -= 1;
1919 btrfs_set_extent_refs(leaf, ei, refs);
952fccac 1920
5f39d397
CM
1921 btrfs_mark_buffer_dirty(leaf);
1922
952fccac 1923 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
31840ae1
ZY
1924 struct btrfs_extent_ref *ref;
1925 ref = btrfs_item_ptr(leaf, path->slots[0],
1926 struct btrfs_extent_ref);
1927 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
952fccac
CM
1928 /* if the back ref and the extent are next to each other
1929 * they get deleted below in one shot
1930 */
1931 path->slots[0] = extent_slot;
1932 num_to_del = 2;
1933 } else if (found_extent) {
1934 /* otherwise delete the extent back ref */
31840ae1 1935 ret = remove_extent_backref(trans, extent_root, path);
952fccac
CM
1936 BUG_ON(ret);
1937 /* if refs are 0, we need to setup the path for deletion */
1938 if (refs == 0) {
1939 btrfs_release_path(extent_root, path);
1940 ret = btrfs_search_slot(trans, extent_root, &key, path,
1941 -1, 1);
952fccac
CM
1942 BUG_ON(ret);
1943 }
1944 }
1945
cf27e1ee 1946 if (refs == 0) {
db94535d
CM
1947 u64 super_used;
1948 u64 root_used;
21af804c
DW
1949#ifdef BIO_RW_DISCARD
1950 u64 map_length = num_bytes;
1951 struct btrfs_multi_bio *multi = NULL;
1952#endif
78fae27e
CM
1953
1954 if (pin) {
31840ae1
ZY
1955 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1956 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
c549228f
Y
1957 if (ret > 0)
1958 mark_free = 1;
1959 BUG_ON(ret < 0);
78fae27e
CM
1960 }
1961
58176a96 1962 /* block accounting for super block */
a2135011 1963 spin_lock_irq(&info->delalloc_lock);
db94535d
CM
1964 super_used = btrfs_super_bytes_used(&info->super_copy);
1965 btrfs_set_super_bytes_used(&info->super_copy,
1966 super_used - num_bytes);
a2135011 1967 spin_unlock_irq(&info->delalloc_lock);
58176a96
JB
1968
1969 /* block accounting for root item */
db94535d 1970 root_used = btrfs_root_used(&root->root_item);
5f39d397 1971 btrfs_set_root_used(&root->root_item,
db94535d 1972 root_used - num_bytes);
952fccac
CM
1973 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1974 num_to_del);
31840ae1 1975 BUG_ON(ret);
db94535d 1976 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
0b86a832 1977 mark_free);
9078a3e1 1978 BUG_ON(ret);
21af804c
DW
1979
1980#ifdef BIO_RW_DISCARD
1981 /* Tell the block device(s) that the sectors can be discarded */
1982 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1983 bytenr, &map_length, &multi, 0);
1984 if (!ret) {
1985 struct btrfs_bio_stripe *stripe = multi->stripes;
1986 int i;
1987
1988 if (map_length > num_bytes)
1989 map_length = num_bytes;
1990
1991 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1992 blkdev_issue_discard(stripe->dev->bdev,
1993 stripe->physical >> 9,
1994 map_length >> 9);
1995 }
1996 kfree(multi);
1997 }
1998#endif
a28ec197 1999 }
5caf2a00 2000 btrfs_free_path(path);
e089f05c 2001 finish_current_insert(trans, extent_root);
a28ec197
CM
2002 return ret;
2003}
2004
a28ec197
CM
2005/*
2006 * find all the blocks marked as pending in the radix tree and remove
2007 * them from the extent map
2008 */
e089f05c
CM
2009static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2010 btrfs_root *extent_root)
a28ec197
CM
2011{
2012 int ret;
e20d96d6 2013 int err = 0;
31840ae1 2014 int mark_free = 0;
1a5bc167
CM
2015 u64 start;
2016 u64 end;
31840ae1 2017 u64 priv;
d1310b2e 2018 struct extent_io_tree *pending_del;
31840ae1
ZY
2019 struct extent_io_tree *extent_ins;
2020 struct pending_extent_op *extent_op;
8ef97622 2021
7d9eb12c 2022 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
31840ae1 2023 extent_ins = &extent_root->fs_info->extent_ins;
1a5bc167 2024 pending_del = &extent_root->fs_info->pending_del;
a28ec197
CM
2025
2026 while(1) {
1a5bc167
CM
2027 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2028 EXTENT_LOCKED);
2029 if (ret)
a28ec197 2030 break;
31840ae1
ZY
2031
2032 ret = get_state_private(pending_del, start, &priv);
2033 BUG_ON(ret);
2034 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2035
1a5bc167
CM
2036 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2037 GFP_NOFS);
31840ae1
ZY
2038
2039 ret = pin_down_bytes(trans, extent_root, start,
2040 end + 1 - start, 0);
2041 mark_free = ret > 0;
2042 if (!test_range_bit(extent_ins, start, end,
2043 EXTENT_LOCKED, 0)) {
2044free_extent:
c286ac48 2045 ret = __free_extent(trans, extent_root,
31840ae1
ZY
2046 start, end + 1 - start,
2047 extent_op->orig_parent,
2048 extent_root->root_key.objectid,
2049 extent_op->orig_generation,
2050 extent_op->level, 0, 0, mark_free);
2051 kfree(extent_op);
c286ac48 2052 } else {
31840ae1
ZY
2053 kfree(extent_op);
2054 ret = get_state_private(extent_ins, start, &priv);
2055 BUG_ON(ret);
2056 extent_op = (struct pending_extent_op *)
2057 (unsigned long)priv;
2058
2059 clear_extent_bits(extent_ins, start, end,
2060 EXTENT_LOCKED, GFP_NOFS);
2061
2062 if (extent_op->type == PENDING_BACKREF_UPDATE)
2063 goto free_extent;
2064
2065 ret = update_block_group(trans, extent_root, start,
2066 end + 1 - start, 0, mark_free);
2067 BUG_ON(ret);
2068 kfree(extent_op);
c286ac48 2069 }
1a5bc167
CM
2070 if (ret)
2071 err = ret;
c286ac48
CM
2072
2073 if (need_resched()) {
2074 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2075 cond_resched();
2076 mutex_lock(&extent_root->fs_info->alloc_mutex);
2077 }
fec577fb 2078 }
e20d96d6 2079 return err;
fec577fb
CM
2080}
2081
2082/*
2083 * remove an extent from the root, returns 0 on success
2084 */
925baedd 2085static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
31840ae1
ZY
2086 struct btrfs_root *root,
2087 u64 bytenr, u64 num_bytes, u64 parent,
2088 u64 root_objectid, u64 ref_generation,
2089 u64 owner_objectid, u64 owner_offset, int pin)
fec577fb 2090{
9f5fae2f 2091 struct btrfs_root *extent_root = root->fs_info->extent_root;
fec577fb
CM
2092 int pending_ret;
2093 int ret;
a28ec197 2094
db94535d 2095 WARN_ON(num_bytes < root->sectorsize);
fec577fb 2096 if (root == extent_root) {
31840ae1
ZY
2097 struct pending_extent_op *extent_op;
2098
2099 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2100 BUG_ON(!extent_op);
2101
2102 extent_op->type = PENDING_EXTENT_DELETE;
2103 extent_op->bytenr = bytenr;
2104 extent_op->num_bytes = num_bytes;
2105 extent_op->parent = parent;
2106 extent_op->orig_parent = parent;
2107 extent_op->generation = ref_generation;
2108 extent_op->orig_generation = ref_generation;
2109 extent_op->level = (int)owner_objectid;
2110
2111 set_extent_bits(&root->fs_info->pending_del,
2112 bytenr, bytenr + num_bytes - 1,
2113 EXTENT_LOCKED, GFP_NOFS);
2114 set_state_private(&root->fs_info->pending_del,
2115 bytenr, (unsigned long)extent_op);
fec577fb
CM
2116 return 0;
2117 }
4bef0848 2118 /* if metadata always pin */
d00aff00
CM
2119 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2120 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
0f9dd46c
JB
2121 struct btrfs_block_group_cache *cache;
2122
d00aff00 2123 /* btrfs_free_reserved_extent */
0f9dd46c
JB
2124 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2125 BUG_ON(!cache);
2126 btrfs_add_free_space(cache, bytenr, num_bytes);
e8569813 2127 update_reserved_extents(root, bytenr, num_bytes, 0);
d00aff00
CM
2128 return 0;
2129 }
4bef0848 2130 pin = 1;
d00aff00 2131 }
4bef0848
CM
2132
2133 /* if data pin when any transaction has committed this */
2134 if (ref_generation != trans->transid)
2135 pin = 1;
2136
31840ae1
ZY
2137 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2138 root_objectid, ref_generation, owner_objectid,
2139 owner_offset, pin, pin == 0);
ee6e6504
CM
2140
2141 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 2142 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
fec577fb
CM
2143 return ret ? ret : pending_ret;
2144}
2145
925baedd 2146int btrfs_free_extent(struct btrfs_trans_handle *trans,
31840ae1
ZY
2147 struct btrfs_root *root,
2148 u64 bytenr, u64 num_bytes, u64 parent,
2149 u64 root_objectid, u64 ref_generation,
2150 u64 owner_objectid, u64 owner_offset, int pin)
925baedd
CM
2151{
2152 int ret;
2153
2154 maybe_lock_mutex(root);
31840ae1 2155 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
925baedd
CM
2156 root_objectid, ref_generation,
2157 owner_objectid, owner_offset, pin);
2158 maybe_unlock_mutex(root);
2159 return ret;
2160}
2161
87ee04eb
CM
2162static u64 stripe_align(struct btrfs_root *root, u64 val)
2163{
2164 u64 mask = ((u64)root->stripesize - 1);
2165 u64 ret = (val + mask) & ~mask;
2166 return ret;
2167}
2168
fec577fb
CM
2169/*
2170 * walks the btree of allocated extents and find a hole of a given size.
2171 * The key ins is changed to record the hole:
2172 * ins->objectid == block start
62e2749e 2173 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
2174 * ins->offset == number of blocks
2175 * Any available blocks before search_start are skipped.
2176 */
98ed5174
CM
2177static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2178 struct btrfs_root *orig_root,
2179 u64 num_bytes, u64 empty_size,
2180 u64 search_start, u64 search_end,
2181 u64 hint_byte, struct btrfs_key *ins,
2182 u64 exclude_start, u64 exclude_nr,
2183 int data)
fec577fb 2184{
87ee04eb 2185 int ret;
a061fc8d 2186 u64 orig_search_start;
9f5fae2f 2187 struct btrfs_root * root = orig_root->fs_info->extent_root;
f2458e1d 2188 struct btrfs_fs_info *info = root->fs_info;
db94535d 2189 u64 total_needed = num_bytes;
239b14b3 2190 u64 *last_ptr = NULL;
be08c1b9 2191 struct btrfs_block_group_cache *block_group;
0ef3e66b 2192 int chunk_alloc_done = 0;
239b14b3 2193 int empty_cluster = 2 * 1024 * 1024;
0ef3e66b 2194 int allowed_chunk_alloc = 0;
fec577fb 2195
db94535d 2196 WARN_ON(num_bytes < root->sectorsize);
b1a4d965
CM
2197 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2198
0ef3e66b
CM
2199 if (orig_root->ref_cows || empty_size)
2200 allowed_chunk_alloc = 1;
2201
239b14b3
CM
2202 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2203 last_ptr = &root->fs_info->last_alloc;
8790d502 2204 empty_cluster = 256 * 1024;
239b14b3
CM
2205 }
2206
0f9dd46c 2207 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
239b14b3 2208 last_ptr = &root->fs_info->last_data_alloc;
0f9dd46c 2209
e02119d5
CM
2210 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2211 last_ptr = &root->fs_info->last_log_alloc;
2212 if (!last_ptr == 0 && root->fs_info->last_alloc) {
2213 *last_ptr = root->fs_info->last_alloc + empty_cluster;
2214 }
2215 }
239b14b3
CM
2216
2217 if (last_ptr) {
2218 if (*last_ptr)
2219 hint_byte = *last_ptr;
0f9dd46c 2220 else
239b14b3 2221 empty_size += empty_cluster;
239b14b3
CM
2222 }
2223
a061fc8d
CM
2224 search_start = max(search_start, first_logical_byte(root, 0));
2225 orig_search_start = search_start;
2226
239b14b3 2227 search_start = max(search_start, hint_byte);
6702ed49 2228 total_needed += empty_size;
0b86a832 2229
0f9dd46c 2230new_group:
e8569813 2231 block_group = btrfs_lookup_first_block_group(info, search_start);
0f9dd46c
JB
2232
2233 /*
2234 * Ok this looks a little tricky, buts its really simple. First if we
2235 * didn't find a block group obviously we want to start over.
2236 * Secondly, if the block group we found does not match the type we
2237 * need, and we have a last_ptr and its not 0, chances are the last
2238 * allocation we made was at the end of the block group, so lets go
2239 * ahead and skip the looking through the rest of the block groups and
2240 * start at the beginning. This helps with metadata allocations,
2241 * since you are likely to have a bunch of data block groups to search
2242 * through first before you realize that you need to start over, so go
2243 * ahead and start over and save the time.
2244 */
2245 if (!block_group || (!block_group_bits(block_group, data) &&
2246 last_ptr && *last_ptr)) {
2247 if (search_start != orig_search_start) {
2248 if (last_ptr && *last_ptr)
2249 *last_ptr = 0;
2250 search_start = orig_search_start;
2251 goto new_group;
2252 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2253 ret = do_chunk_alloc(trans, root,
2254 num_bytes + 2 * 1024 * 1024,
2255 data, 1);
e8569813 2256 if (ret < 0)
0f9dd46c 2257 goto error;
0f9dd46c
JB
2258 BUG_ON(ret);
2259 chunk_alloc_done = 1;
2260 search_start = orig_search_start;
2261 goto new_group;
2262 } else {
2263 ret = -ENOSPC;
2264 goto error;
0ef3e66b 2265 }
239b14b3 2266 }
e19caa5f 2267
0f9dd46c
JB
2268 /*
2269 * this is going to seach through all of the existing block groups it
2270 * can find, so if we don't find something we need to see if we can
2271 * allocate what we need.
2272 */
2273 ret = find_free_space(root, &block_group, &search_start,
2274 total_needed, data);
2275 if (ret == -ENOSPC) {
2276 /*
2277 * instead of allocating, start at the original search start
2278 * and see if there is something to be found, if not then we
2279 * allocate
2280 */
2281 if (search_start != orig_search_start) {
2282 if (last_ptr && *last_ptr) {
2283 *last_ptr = 0;
2284 total_needed += empty_cluster;
2285 }
2286 search_start = orig_search_start;
2287 goto new_group;
239b14b3 2288 }
0f9dd46c
JB
2289
2290 /*
2291 * we've already allocated, we're pretty screwed
2292 */
2293 if (chunk_alloc_done) {
239b14b3 2294 goto error;
0f9dd46c
JB
2295 } else if (!allowed_chunk_alloc && block_group &&
2296 block_group_bits(block_group, data)) {
2297 block_group->space_info->force_alloc = 1;
2298 goto error;
2299 } else if (!allowed_chunk_alloc) {
2300 goto error;
2301 }
2302
2303 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2304 data, 1);
2305 if (ret < 0)
2306 goto error;
2307
2308 BUG_ON(ret);
2309 chunk_alloc_done = 1;
2310 if (block_group)
2311 search_start = block_group->key.objectid +
2312 block_group->key.offset;
2313 else
2314 search_start = orig_search_start;
2315 goto new_group;
239b14b3
CM
2316 }
2317
0f9dd46c
JB
2318 if (ret)
2319 goto error;
2320
0b86a832
CM
2321 search_start = stripe_align(root, search_start);
2322 ins->objectid = search_start;
2323 ins->offset = num_bytes;
e37c9e69 2324
0f9dd46c
JB
2325 if (ins->objectid + num_bytes >= search_end) {
2326 search_start = orig_search_start;
2327 if (chunk_alloc_done) {
2328 ret = -ENOSPC;
2329 goto error;
2330 }
2331 goto new_group;
2332 }
0b86a832
CM
2333
2334 if (ins->objectid + num_bytes >
2335 block_group->key.objectid + block_group->key.offset) {
0f9dd46c
JB
2336 if (search_start == orig_search_start && chunk_alloc_done) {
2337 ret = -ENOSPC;
2338 goto error;
2339 }
e19caa5f
CM
2340 search_start = block_group->key.objectid +
2341 block_group->key.offset;
2342 goto new_group;
2343 }
0b86a832 2344
db94535d 2345 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
f2654de4
CM
2346 ins->objectid < exclude_start + exclude_nr)) {
2347 search_start = exclude_start + exclude_nr;
2348 goto new_group;
2349 }
0b86a832 2350
0f9dd46c
JB
2351 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2352 trans->block_group = block_group;
2353
db94535d 2354 ins->offset = num_bytes;
239b14b3
CM
2355 if (last_ptr) {
2356 *last_ptr = ins->objectid + ins->offset;
2357 if (*last_ptr ==
0f9dd46c 2358 btrfs_super_total_bytes(&root->fs_info->super_copy))
239b14b3 2359 *last_ptr = 0;
be744175 2360 }
be744175 2361
0f9dd46c 2362 ret = 0;
0f70abe2 2363error:
0f70abe2 2364 return ret;
fec577fb 2365}
ec44a35c 2366
0f9dd46c
JB
2367static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2368{
2369 struct btrfs_block_group_cache *cache;
2370 struct list_head *l;
2371
2372 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
e8569813
ZY
2373 info->total_bytes - info->bytes_used - info->bytes_pinned -
2374 info->bytes_reserved, (info->full) ? "" : "not ");
0f9dd46c
JB
2375
2376 spin_lock(&info->lock);
2377 list_for_each(l, &info->block_groups) {
2378 cache = list_entry(l, struct btrfs_block_group_cache, list);
2379 spin_lock(&cache->lock);
2380 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
e8569813 2381 "%Lu pinned %Lu reserved\n",
0f9dd46c 2382 cache->key.objectid, cache->key.offset,
e8569813
ZY
2383 btrfs_block_group_used(&cache->item),
2384 cache->pinned, cache->reserved);
0f9dd46c
JB
2385 btrfs_dump_free_space(cache, bytes);
2386 spin_unlock(&cache->lock);
2387 }
2388 spin_unlock(&info->lock);
2389}
e8569813 2390
e6dcd2dc
CM
2391static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2392 struct btrfs_root *root,
2393 u64 num_bytes, u64 min_alloc_size,
2394 u64 empty_size, u64 hint_byte,
2395 u64 search_end, struct btrfs_key *ins,
2396 u64 data)
fec577fb
CM
2397{
2398 int ret;
fbdc762b 2399 u64 search_start = 0;
8790d502 2400 u64 alloc_profile;
1261ec42 2401 struct btrfs_fs_info *info = root->fs_info;
0f9dd46c 2402 struct btrfs_block_group_cache *cache;
925baedd 2403
6324fbf3 2404 if (data) {
8790d502
CM
2405 alloc_profile = info->avail_data_alloc_bits &
2406 info->data_alloc_profile;
2407 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
6324fbf3 2408 } else if (root == root->fs_info->chunk_root) {
8790d502
CM
2409 alloc_profile = info->avail_system_alloc_bits &
2410 info->system_alloc_profile;
2411 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
6324fbf3 2412 } else {
8790d502
CM
2413 alloc_profile = info->avail_metadata_alloc_bits &
2414 info->metadata_alloc_profile;
2415 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
6324fbf3 2416 }
98d20f67 2417again:
a061fc8d 2418 data = reduce_alloc_profile(root, data);
0ef3e66b
CM
2419 /*
2420 * the only place that sets empty_size is btrfs_realloc_node, which
2421 * is not called recursively on allocations
2422 */
2423 if (empty_size || root->ref_cows) {
593060d7 2424 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
6324fbf3 2425 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
0ef3e66b
CM
2426 2 * 1024 * 1024,
2427 BTRFS_BLOCK_GROUP_METADATA |
2428 (info->metadata_alloc_profile &
2429 info->avail_metadata_alloc_bits), 0);
6324fbf3
CM
2430 }
2431 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
0ef3e66b 2432 num_bytes + 2 * 1024 * 1024, data, 0);
6324fbf3 2433 }
0b86a832 2434
db94535d
CM
2435 WARN_ON(num_bytes < root->sectorsize);
2436 ret = find_free_extent(trans, root, num_bytes, empty_size,
2437 search_start, search_end, hint_byte, ins,
26b8003f
CM
2438 trans->alloc_exclude_start,
2439 trans->alloc_exclude_nr, data);
3b951516 2440
98d20f67
CM
2441 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2442 num_bytes = num_bytes >> 1;
0f9dd46c 2443 num_bytes = num_bytes & ~(root->sectorsize - 1);
98d20f67 2444 num_bytes = max(num_bytes, min_alloc_size);
0ef3e66b
CM
2445 do_chunk_alloc(trans, root->fs_info->extent_root,
2446 num_bytes, data, 1);
98d20f67
CM
2447 goto again;
2448 }
ec44a35c 2449 if (ret) {
0f9dd46c
JB
2450 struct btrfs_space_info *sinfo;
2451
2452 sinfo = __find_space_info(root->fs_info, data);
2453 printk("allocation failed flags %Lu, wanted %Lu\n",
2454 data, num_bytes);
2455 dump_space_info(sinfo, num_bytes);
925baedd 2456 BUG();
925baedd 2457 }
0f9dd46c
JB
2458 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2459 if (!cache) {
2460 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2461 return -ENOSPC;
2462 }
2463
2464 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2465
2466 return ret;
e6dcd2dc
CM
2467}
2468
65b51a00
CM
2469int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2470{
0f9dd46c
JB
2471 struct btrfs_block_group_cache *cache;
2472
65b51a00 2473 maybe_lock_mutex(root);
0f9dd46c
JB
2474 cache = btrfs_lookup_block_group(root->fs_info, start);
2475 if (!cache) {
2476 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2477 maybe_unlock_mutex(root);
2478 return -ENOSPC;
2479 }
2480 btrfs_add_free_space(cache, start, len);
65b51a00
CM
2481 maybe_unlock_mutex(root);
2482 return 0;
2483}
2484
e6dcd2dc
CM
2485int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2486 struct btrfs_root *root,
2487 u64 num_bytes, u64 min_alloc_size,
2488 u64 empty_size, u64 hint_byte,
2489 u64 search_end, struct btrfs_key *ins,
2490 u64 data)
2491{
2492 int ret;
2493 maybe_lock_mutex(root);
2494 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2495 empty_size, hint_byte, search_end, ins,
2496 data);
e8569813 2497 update_reserved_extents(root, ins->objectid, ins->offset, 1);
e6dcd2dc
CM
2498 maybe_unlock_mutex(root);
2499 return ret;
2500}
2501
2502static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
31840ae1 2503 struct btrfs_root *root, u64 parent,
e6dcd2dc
CM
2504 u64 root_objectid, u64 ref_generation,
2505 u64 owner, u64 owner_offset,
2506 struct btrfs_key *ins)
2507{
2508 int ret;
2509 int pending_ret;
2510 u64 super_used;
2511 u64 root_used;
2512 u64 num_bytes = ins->offset;
2513 u32 sizes[2];
2514 struct btrfs_fs_info *info = root->fs_info;
2515 struct btrfs_root *extent_root = info->extent_root;
2516 struct btrfs_extent_item *extent_item;
2517 struct btrfs_extent_ref *ref;
2518 struct btrfs_path *path;
2519 struct btrfs_key keys[2];
fec577fb 2520
31840ae1
ZY
2521 if (parent == 0)
2522 parent = ins->objectid;
2523
58176a96 2524 /* block accounting for super block */
a2135011 2525 spin_lock_irq(&info->delalloc_lock);
db94535d
CM
2526 super_used = btrfs_super_bytes_used(&info->super_copy);
2527 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
a2135011 2528 spin_unlock_irq(&info->delalloc_lock);
26b8003f 2529
58176a96 2530 /* block accounting for root item */
db94535d
CM
2531 root_used = btrfs_root_used(&root->root_item);
2532 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
58176a96 2533
26b8003f 2534 if (root == extent_root) {
31840ae1
ZY
2535 struct pending_extent_op *extent_op;
2536
2537 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2538 BUG_ON(!extent_op);
2539
2540 extent_op->type = PENDING_EXTENT_INSERT;
2541 extent_op->bytenr = ins->objectid;
2542 extent_op->num_bytes = ins->offset;
2543 extent_op->parent = parent;
2544 extent_op->orig_parent = 0;
2545 extent_op->generation = ref_generation;
2546 extent_op->orig_generation = 0;
2547 extent_op->level = (int)owner;
2548
1a5bc167
CM
2549 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2550 ins->objectid + ins->offset - 1,
2551 EXTENT_LOCKED, GFP_NOFS);
31840ae1
ZY
2552 set_state_private(&root->fs_info->extent_ins,
2553 ins->objectid, (unsigned long)extent_op);
26b8003f
CM
2554 goto update_block;
2555 }
2556
47e4bb98 2557 memcpy(&keys[0], ins, sizeof(*ins));
47e4bb98
CM
2558 keys[1].objectid = ins->objectid;
2559 keys[1].type = BTRFS_EXTENT_REF_KEY;
31840ae1 2560 keys[1].offset = parent;
47e4bb98
CM
2561 sizes[0] = sizeof(*extent_item);
2562 sizes[1] = sizeof(*ref);
7bb86316
CM
2563
2564 path = btrfs_alloc_path();
2565 BUG_ON(!path);
47e4bb98
CM
2566
2567 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2568 sizes, 2);
ccd467d6 2569 BUG_ON(ret);
0f9dd46c 2570
47e4bb98
CM
2571 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2572 struct btrfs_extent_item);
2573 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2574 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2575 struct btrfs_extent_ref);
2576
2577 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2578 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2579 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2580 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
31840ae1 2581 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
47e4bb98
CM
2582
2583 btrfs_mark_buffer_dirty(path->nodes[0]);
2584
2585 trans->alloc_exclude_start = 0;
2586 trans->alloc_exclude_nr = 0;
7bb86316 2587 btrfs_free_path(path);
e089f05c 2588 finish_current_insert(trans, extent_root);
e20d96d6 2589 pending_ret = del_pending_extents(trans, extent_root);
f510cfec 2590
925baedd
CM
2591 if (ret)
2592 goto out;
e37c9e69 2593 if (pending_ret) {
925baedd
CM
2594 ret = pending_ret;
2595 goto out;
e37c9e69 2596 }
26b8003f
CM
2597
2598update_block:
0b86a832 2599 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
f5947066
CM
2600 if (ret) {
2601 printk("update block group failed for %Lu %Lu\n",
2602 ins->objectid, ins->offset);
2603 BUG();
2604 }
925baedd 2605out:
e6dcd2dc
CM
2606 return ret;
2607}
2608
2609int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
31840ae1 2610 struct btrfs_root *root, u64 parent,
e6dcd2dc
CM
2611 u64 root_objectid, u64 ref_generation,
2612 u64 owner, u64 owner_offset,
2613 struct btrfs_key *ins)
2614{
2615 int ret;
1c2308f8
CM
2616
2617 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2618 return 0;
e6dcd2dc 2619 maybe_lock_mutex(root);
31840ae1
ZY
2620 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2621 root_objectid, ref_generation,
2622 owner, owner_offset, ins);
e8569813 2623 update_reserved_extents(root, ins->objectid, ins->offset, 0);
e6dcd2dc
CM
2624 maybe_unlock_mutex(root);
2625 return ret;
2626}
e02119d5
CM
2627
2628/*
2629 * this is used by the tree logging recovery code. It records that
2630 * an extent has been allocated and makes sure to clear the free
2631 * space cache bits as well
2632 */
2633int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
31840ae1 2634 struct btrfs_root *root, u64 parent,
e02119d5
CM
2635 u64 root_objectid, u64 ref_generation,
2636 u64 owner, u64 owner_offset,
2637 struct btrfs_key *ins)
2638{
2639 int ret;
2640 struct btrfs_block_group_cache *block_group;
2641
2642 maybe_lock_mutex(root);
2643 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2644 cache_block_group(root, block_group);
2645
0f9dd46c
JB
2646 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2647 BUG_ON(ret);
31840ae1
ZY
2648 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2649 root_objectid, ref_generation,
2650 owner, owner_offset, ins);
e02119d5
CM
2651 maybe_unlock_mutex(root);
2652 return ret;
2653}
2654
e6dcd2dc
CM
2655/*
2656 * finds a free extent and does all the dirty work required for allocation
2657 * returns the key for the extent through ins, and a tree buffer for
2658 * the first block of the extent through buf.
2659 *
2660 * returns 0 if everything worked, non-zero otherwise.
2661 */
2662int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2663 struct btrfs_root *root,
31840ae1 2664 u64 num_bytes, u64 parent, u64 min_alloc_size,
e6dcd2dc 2665 u64 root_objectid, u64 ref_generation,
31840ae1 2666 u64 owner_objectid, u64 owner_offset,
e6dcd2dc
CM
2667 u64 empty_size, u64 hint_byte,
2668 u64 search_end, struct btrfs_key *ins, u64 data)
2669{
2670 int ret;
2671
2672 maybe_lock_mutex(root);
2673
2674 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2675 min_alloc_size, empty_size, hint_byte,
2676 search_end, ins, data);
2677 BUG_ON(ret);
d00aff00 2678 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
31840ae1
ZY
2679 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2680 root_objectid, ref_generation,
2681 owner_objectid, owner_offset, ins);
d00aff00 2682 BUG_ON(ret);
e6dcd2dc 2683
e8569813
ZY
2684 } else {
2685 update_reserved_extents(root, ins->objectid, ins->offset, 1);
d00aff00 2686 }
925baedd
CM
2687 maybe_unlock_mutex(root);
2688 return ret;
fec577fb 2689}
65b51a00
CM
2690
2691struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *root,
2693 u64 bytenr, u32 blocksize)
2694{
2695 struct extent_buffer *buf;
2696
2697 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2698 if (!buf)
2699 return ERR_PTR(-ENOMEM);
2700 btrfs_set_header_generation(buf, trans->transid);
2701 btrfs_tree_lock(buf);
2702 clean_tree_block(trans, root, buf);
2703 btrfs_set_buffer_uptodate(buf);
d0c803c4
CM
2704 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2705 set_extent_dirty(&root->dirty_log_pages, buf->start,
2706 buf->start + buf->len - 1, GFP_NOFS);
2707 } else {
2708 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
65b51a00 2709 buf->start + buf->len - 1, GFP_NOFS);
d0c803c4 2710 }
65b51a00
CM
2711 trans->blocks_used++;
2712 return buf;
2713}
2714
fec577fb
CM
2715/*
2716 * helper function to allocate a block for a given tree
2717 * returns the tree buffer or NULL.
2718 */
5f39d397 2719struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
7bb86316 2720 struct btrfs_root *root,
31840ae1 2721 u32 blocksize, u64 parent,
7bb86316
CM
2722 u64 root_objectid,
2723 u64 ref_generation,
7bb86316
CM
2724 int level,
2725 u64 hint,
5f39d397 2726 u64 empty_size)
fec577fb 2727{
e2fa7227 2728 struct btrfs_key ins;
fec577fb 2729 int ret;
5f39d397 2730 struct extent_buffer *buf;
fec577fb 2731
31840ae1
ZY
2732 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2733 root_objectid, ref_generation, level, 0,
2734 empty_size, hint, (u64)-1, &ins, 0);
fec577fb 2735 if (ret) {
54aa1f4d
CM
2736 BUG_ON(ret > 0);
2737 return ERR_PTR(ret);
fec577fb 2738 }
55c69072 2739
65b51a00 2740 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
fec577fb
CM
2741 return buf;
2742}
a28ec197 2743
e02119d5
CM
2744int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2745 struct btrfs_root *root, struct extent_buffer *leaf)
6407bf6d 2746{
7bb86316
CM
2747 u64 leaf_owner;
2748 u64 leaf_generation;
5f39d397 2749 struct btrfs_key key;
6407bf6d
CM
2750 struct btrfs_file_extent_item *fi;
2751 int i;
2752 int nritems;
2753 int ret;
2754
5f39d397
CM
2755 BUG_ON(!btrfs_is_leaf(leaf));
2756 nritems = btrfs_header_nritems(leaf);
7bb86316
CM
2757 leaf_owner = btrfs_header_owner(leaf);
2758 leaf_generation = btrfs_header_generation(leaf);
2759
6407bf6d 2760 for (i = 0; i < nritems; i++) {
db94535d 2761 u64 disk_bytenr;
e34a5b4f 2762 cond_resched();
5f39d397
CM
2763
2764 btrfs_item_key_to_cpu(leaf, &key, i);
2765 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d
CM
2766 continue;
2767 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5f39d397
CM
2768 if (btrfs_file_extent_type(leaf, fi) ==
2769 BTRFS_FILE_EXTENT_INLINE)
236454df 2770 continue;
6407bf6d
CM
2771 /*
2772 * FIXME make sure to insert a trans record that
2773 * repeats the snapshot del on crash
2774 */
db94535d
CM
2775 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2776 if (disk_bytenr == 0)
3a686375 2777 continue;
4a096752
CM
2778
2779 mutex_lock(&root->fs_info->alloc_mutex);
925baedd 2780 ret = __btrfs_free_extent(trans, root, disk_bytenr,
7bb86316 2781 btrfs_file_extent_disk_num_bytes(leaf, fi),
31840ae1 2782 leaf->start, leaf_owner, leaf_generation,
7bb86316 2783 key.objectid, key.offset, 0);
4a096752 2784 mutex_unlock(&root->fs_info->alloc_mutex);
31840ae1 2785 BUG_ON(ret);
2dd3e67b
CM
2786
2787 atomic_inc(&root->fs_info->throttle_gen);
2788 wake_up(&root->fs_info->transaction_throttle);
2789 cond_resched();
6407bf6d
CM
2790 }
2791 return 0;
2792}
2793
e02119d5
CM
2794static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2795 struct btrfs_root *root,
2796 struct btrfs_leaf_ref *ref)
31153d81
YZ
2797{
2798 int i;
2799 int ret;
2800 struct btrfs_extent_info *info = ref->extents;
2801
31153d81
YZ
2802 for (i = 0; i < ref->nritems; i++) {
2803 mutex_lock(&root->fs_info->alloc_mutex);
31840ae1
ZY
2804 ret = __btrfs_free_extent(trans, root, info->bytenr,
2805 info->num_bytes, ref->bytenr,
2806 ref->owner, ref->generation,
2807 info->objectid, info->offset, 0);
31153d81 2808 mutex_unlock(&root->fs_info->alloc_mutex);
2dd3e67b
CM
2809
2810 atomic_inc(&root->fs_info->throttle_gen);
2811 wake_up(&root->fs_info->transaction_throttle);
2812 cond_resched();
2813
31153d81
YZ
2814 BUG_ON(ret);
2815 info++;
2816 }
31153d81
YZ
2817
2818 return 0;
2819}
2820
333db94c
CM
2821int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2822 u32 *refs)
2823{
017e5369 2824 int ret;
f87f057b 2825
31840ae1 2826 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
f87f057b
CM
2827 BUG_ON(ret);
2828
2829#if 0 // some debugging code in case we see problems here
2830 /* if the refs count is one, it won't get increased again. But
2831 * if the ref count is > 1, someone may be decreasing it at
2832 * the same time we are.
2833 */
2834 if (*refs != 1) {
2835 struct extent_buffer *eb = NULL;
2836 eb = btrfs_find_create_tree_block(root, start, len);
2837 if (eb)
2838 btrfs_tree_lock(eb);
2839
2840 mutex_lock(&root->fs_info->alloc_mutex);
2841 ret = lookup_extent_ref(NULL, root, start, len, refs);
2842 BUG_ON(ret);
2843 mutex_unlock(&root->fs_info->alloc_mutex);
2844
2845 if (eb) {
2846 btrfs_tree_unlock(eb);
2847 free_extent_buffer(eb);
2848 }
2849 if (*refs == 1) {
2850 printk("block %llu went down to one during drop_snap\n",
2851 (unsigned long long)start);
2852 }
2853
2854 }
2855#endif
2856
e7a84565 2857 cond_resched();
017e5369 2858 return ret;
333db94c
CM
2859}
2860
9aca1d51
CM
2861/*
2862 * helper function for drop_snapshot, this walks down the tree dropping ref
2863 * counts as it goes.
2864 */
98ed5174
CM
2865static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2866 struct btrfs_root *root,
2867 struct btrfs_path *path, int *level)
20524f02 2868{
7bb86316
CM
2869 u64 root_owner;
2870 u64 root_gen;
2871 u64 bytenr;
ca7a79ad 2872 u64 ptr_gen;
5f39d397
CM
2873 struct extent_buffer *next;
2874 struct extent_buffer *cur;
7bb86316 2875 struct extent_buffer *parent;
31153d81 2876 struct btrfs_leaf_ref *ref;
db94535d 2877 u32 blocksize;
20524f02
CM
2878 int ret;
2879 u32 refs;
2880
5caf2a00
CM
2881 WARN_ON(*level < 0);
2882 WARN_ON(*level >= BTRFS_MAX_LEVEL);
333db94c 2883 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
db94535d 2884 path->nodes[*level]->len, &refs);
20524f02
CM
2885 BUG_ON(ret);
2886 if (refs > 1)
2887 goto out;
e011599b 2888
9aca1d51
CM
2889 /*
2890 * walk down to the last node level and free all the leaves
2891 */
6407bf6d 2892 while(*level >= 0) {
5caf2a00
CM
2893 WARN_ON(*level < 0);
2894 WARN_ON(*level >= BTRFS_MAX_LEVEL);
20524f02 2895 cur = path->nodes[*level];
e011599b 2896
5f39d397 2897 if (btrfs_header_level(cur) != *level)
2c90e5d6 2898 WARN_ON(1);
e011599b 2899
7518a238 2900 if (path->slots[*level] >=
5f39d397 2901 btrfs_header_nritems(cur))
20524f02 2902 break;
6407bf6d 2903 if (*level == 0) {
e02119d5 2904 ret = btrfs_drop_leaf_ref(trans, root, cur);
6407bf6d
CM
2905 BUG_ON(ret);
2906 break;
2907 }
db94535d 2908 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
ca7a79ad 2909 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
db94535d 2910 blocksize = btrfs_level_size(root, *level - 1);
925baedd 2911
333db94c 2912 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
6407bf6d
CM
2913 BUG_ON(ret);
2914 if (refs != 1) {
7bb86316
CM
2915 parent = path->nodes[*level];
2916 root_owner = btrfs_header_owner(parent);
2917 root_gen = btrfs_header_generation(parent);
20524f02 2918 path->slots[*level]++;
f87f057b
CM
2919
2920 mutex_lock(&root->fs_info->alloc_mutex);
925baedd 2921 ret = __btrfs_free_extent(trans, root, bytenr,
31840ae1
ZY
2922 blocksize, parent->start,
2923 root_owner, root_gen, 0, 0, 1);
20524f02 2924 BUG_ON(ret);
f87f057b 2925 mutex_unlock(&root->fs_info->alloc_mutex);
18e35e0a
CM
2926
2927 atomic_inc(&root->fs_info->throttle_gen);
2928 wake_up(&root->fs_info->transaction_throttle);
2dd3e67b 2929 cond_resched();
18e35e0a 2930
20524f02
CM
2931 continue;
2932 }
f87f057b
CM
2933 /*
2934 * at this point, we have a single ref, and since the
2935 * only place referencing this extent is a dead root
2936 * the reference count should never go higher.
2937 * So, we don't need to check it again
2938 */
31153d81 2939 if (*level == 1) {
017e5369 2940 ref = btrfs_lookup_leaf_ref(root, bytenr);
31153d81 2941 if (ref) {
e02119d5 2942 ret = cache_drop_leaf_ref(trans, root, ref);
31153d81
YZ
2943 BUG_ON(ret);
2944 btrfs_remove_leaf_ref(root, ref);
bcc63abb 2945 btrfs_free_leaf_ref(root, ref);
31153d81
YZ
2946 *level = 0;
2947 break;
2948 }
37d1aeee
CM
2949 if (printk_ratelimit())
2950 printk("leaf ref miss for bytenr %llu\n",
2951 (unsigned long long)bytenr);
31153d81 2952 }
db94535d 2953 next = btrfs_find_tree_block(root, bytenr, blocksize);
1259ab75 2954 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
5f39d397 2955 free_extent_buffer(next);
333db94c 2956
ca7a79ad
CM
2957 next = read_tree_block(root, bytenr, blocksize,
2958 ptr_gen);
e7a84565 2959 cond_resched();
f87f057b
CM
2960#if 0
2961 /*
2962 * this is a debugging check and can go away
2963 * the ref should never go all the way down to 1
2964 * at this point
2965 */
e6dcd2dc
CM
2966 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2967 &refs);
e9d0b13b 2968 BUG_ON(ret);
f87f057b
CM
2969 WARN_ON(refs != 1);
2970#endif
e9d0b13b 2971 }
5caf2a00 2972 WARN_ON(*level <= 0);
83e15a28 2973 if (path->nodes[*level-1])
5f39d397 2974 free_extent_buffer(path->nodes[*level-1]);
20524f02 2975 path->nodes[*level-1] = next;
5f39d397 2976 *level = btrfs_header_level(next);
20524f02 2977 path->slots[*level] = 0;
2dd3e67b 2978 cond_resched();
20524f02
CM
2979 }
2980out:
5caf2a00
CM
2981 WARN_ON(*level < 0);
2982 WARN_ON(*level >= BTRFS_MAX_LEVEL);
7bb86316
CM
2983
2984 if (path->nodes[*level] == root->node) {
7bb86316 2985 parent = path->nodes[*level];
31153d81 2986 bytenr = path->nodes[*level]->start;
7bb86316
CM
2987 } else {
2988 parent = path->nodes[*level + 1];
31153d81 2989 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
7bb86316
CM
2990 }
2991
31153d81
YZ
2992 blocksize = btrfs_level_size(root, *level);
2993 root_owner = btrfs_header_owner(parent);
7bb86316 2994 root_gen = btrfs_header_generation(parent);
31153d81 2995
f87f057b 2996 mutex_lock(&root->fs_info->alloc_mutex);
31153d81 2997 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
31840ae1
ZY
2998 parent->start, root_owner, root_gen,
2999 0, 0, 1);
3000 mutex_unlock(&root->fs_info->alloc_mutex);
5f39d397 3001 free_extent_buffer(path->nodes[*level]);
20524f02
CM
3002 path->nodes[*level] = NULL;
3003 *level += 1;
3004 BUG_ON(ret);
f87f057b 3005
e7a84565 3006 cond_resched();
20524f02
CM
3007 return 0;
3008}
3009
9aca1d51
CM
3010/*
3011 * helper for dropping snapshots. This walks back up the tree in the path
3012 * to find the first node higher up where we haven't yet gone through
3013 * all the slots
3014 */
98ed5174
CM
3015static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3016 struct btrfs_root *root,
3017 struct btrfs_path *path, int *level)
20524f02 3018{
7bb86316
CM
3019 u64 root_owner;
3020 u64 root_gen;
3021 struct btrfs_root_item *root_item = &root->root_item;
20524f02
CM
3022 int i;
3023 int slot;
3024 int ret;
9f3a7427 3025
234b63a0 3026 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
20524f02 3027 slot = path->slots[i];
5f39d397
CM
3028 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3029 struct extent_buffer *node;
3030 struct btrfs_disk_key disk_key;
3031 node = path->nodes[i];
20524f02
CM
3032 path->slots[i]++;
3033 *level = i;
9f3a7427 3034 WARN_ON(*level == 0);
5f39d397 3035 btrfs_node_key(node, &disk_key, path->slots[i]);
9f3a7427 3036 memcpy(&root_item->drop_progress,
5f39d397 3037 &disk_key, sizeof(disk_key));
9f3a7427 3038 root_item->drop_level = i;
20524f02
CM
3039 return 0;
3040 } else {
31840ae1
ZY
3041 struct extent_buffer *parent;
3042 if (path->nodes[*level] == root->node)
3043 parent = path->nodes[*level];
3044 else
3045 parent = path->nodes[*level + 1];
3046
3047 root_owner = btrfs_header_owner(parent);
3048 root_gen = btrfs_header_generation(parent);
e089f05c 3049 ret = btrfs_free_extent(trans, root,
db94535d 3050 path->nodes[*level]->start,
7bb86316 3051 path->nodes[*level]->len,
31840ae1 3052 parent->start,
7bb86316 3053 root_owner, root_gen, 0, 0, 1);
6407bf6d 3054 BUG_ON(ret);
5f39d397 3055 free_extent_buffer(path->nodes[*level]);
83e15a28 3056 path->nodes[*level] = NULL;
20524f02 3057 *level = i + 1;
20524f02
CM
3058 }
3059 }
3060 return 1;
3061}
3062
9aca1d51
CM
3063/*
3064 * drop the reference count on the tree rooted at 'snap'. This traverses
3065 * the tree freeing any blocks that have a ref count of zero after being
3066 * decremented.
3067 */
e089f05c 3068int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
9f3a7427 3069 *root)
20524f02 3070{
3768f368 3071 int ret = 0;
9aca1d51 3072 int wret;
20524f02 3073 int level;
5caf2a00 3074 struct btrfs_path *path;
20524f02
CM
3075 int i;
3076 int orig_level;
9f3a7427 3077 struct btrfs_root_item *root_item = &root->root_item;
20524f02 3078
a2135011 3079 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
5caf2a00
CM
3080 path = btrfs_alloc_path();
3081 BUG_ON(!path);
20524f02 3082
5f39d397 3083 level = btrfs_header_level(root->node);
20524f02 3084 orig_level = level;
9f3a7427
CM
3085 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3086 path->nodes[level] = root->node;
f510cfec 3087 extent_buffer_get(root->node);
9f3a7427
CM
3088 path->slots[level] = 0;
3089 } else {
3090 struct btrfs_key key;
5f39d397
CM
3091 struct btrfs_disk_key found_key;
3092 struct extent_buffer *node;
6702ed49 3093
9f3a7427 3094 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6702ed49
CM
3095 level = root_item->drop_level;
3096 path->lowest_level = level;
9f3a7427 3097 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6702ed49 3098 if (wret < 0) {
9f3a7427
CM
3099 ret = wret;
3100 goto out;
3101 }
5f39d397
CM
3102 node = path->nodes[level];
3103 btrfs_node_key(node, &found_key, path->slots[level]);
3104 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3105 sizeof(found_key)));
7d9eb12c
CM
3106 /*
3107 * unlock our path, this is safe because only this
3108 * function is allowed to delete this snapshot
3109 */
925baedd
CM
3110 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3111 if (path->nodes[i] && path->locks[i]) {
3112 path->locks[i] = 0;
3113 btrfs_tree_unlock(path->nodes[i]);
3114 }
3115 }
9f3a7427 3116 }
20524f02 3117 while(1) {
5caf2a00 3118 wret = walk_down_tree(trans, root, path, &level);
9aca1d51 3119 if (wret > 0)
20524f02 3120 break;
9aca1d51
CM
3121 if (wret < 0)
3122 ret = wret;
3123
5caf2a00 3124 wret = walk_up_tree(trans, root, path, &level);
9aca1d51 3125 if (wret > 0)
20524f02 3126 break;
9aca1d51
CM
3127 if (wret < 0)
3128 ret = wret;
e7a84565
CM
3129 if (trans->transaction->in_commit) {
3130 ret = -EAGAIN;
3131 break;
3132 }
18e35e0a 3133 atomic_inc(&root->fs_info->throttle_gen);
017e5369 3134 wake_up(&root->fs_info->transaction_throttle);
20524f02 3135 }
83e15a28 3136 for (i = 0; i <= orig_level; i++) {
5caf2a00 3137 if (path->nodes[i]) {
5f39d397 3138 free_extent_buffer(path->nodes[i]);
0f82731f 3139 path->nodes[i] = NULL;
83e15a28 3140 }
20524f02 3141 }
9f3a7427 3142out:
5caf2a00 3143 btrfs_free_path(path);
9aca1d51 3144 return ret;
20524f02 3145}
9078a3e1 3146
96b5179d 3147int btrfs_free_block_groups(struct btrfs_fs_info *info)
9078a3e1 3148{
0f9dd46c
JB
3149 struct btrfs_block_group_cache *block_group;
3150 struct rb_node *n;
925baedd
CM
3151
3152 mutex_lock(&info->alloc_mutex);
0f9dd46c
JB
3153 spin_lock(&info->block_group_cache_lock);
3154 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
3155 block_group = rb_entry(n, struct btrfs_block_group_cache,
3156 cache_node);
3157
4434c33c 3158 spin_unlock(&info->block_group_cache_lock);
0f9dd46c 3159 btrfs_remove_free_space_cache(block_group);
4434c33c
CM
3160 spin_lock(&info->block_group_cache_lock);
3161
0f9dd46c
JB
3162 rb_erase(&block_group->cache_node,
3163 &info->block_group_cache_tree);
4434c33c 3164
0f9dd46c
JB
3165 spin_lock(&block_group->space_info->lock);
3166 list_del(&block_group->list);
3167 spin_unlock(&block_group->space_info->lock);
3168 kfree(block_group);
3169 }
3170 spin_unlock(&info->block_group_cache_lock);
925baedd 3171 mutex_unlock(&info->alloc_mutex);
be744175
CM
3172 return 0;
3173}
3174
8e7bf94f
CM
3175static unsigned long calc_ra(unsigned long start, unsigned long last,
3176 unsigned long nr)
3177{
3178 return min(last, start + nr - 1);
3179}
3180
98ed5174
CM
3181static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3182 u64 len)
edbd8d4e
CM
3183{
3184 u64 page_start;
3185 u64 page_end;
edbd8d4e 3186 unsigned long last_index;
edbd8d4e
CM
3187 unsigned long i;
3188 struct page *page;
d1310b2e 3189 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4313b399 3190 struct file_ra_state *ra;
8e7bf94f
CM
3191 unsigned long total_read = 0;
3192 unsigned long ra_pages;
3eaa2885 3193 struct btrfs_ordered_extent *ordered;
a061fc8d 3194 struct btrfs_trans_handle *trans;
4313b399
CM
3195
3196 ra = kzalloc(sizeof(*ra), GFP_NOFS);
edbd8d4e
CM
3197
3198 mutex_lock(&inode->i_mutex);
4313b399 3199 i = start >> PAGE_CACHE_SHIFT;
edbd8d4e
CM
3200 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3201
8e7bf94f
CM
3202 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
3203
4313b399 3204 file_ra_state_init(ra, inode->i_mapping);
edbd8d4e 3205
4313b399 3206 for (; i <= last_index; i++) {
8e7bf94f
CM
3207 if (total_read % ra_pages == 0) {
3208 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3209 calc_ra(i, last_index, ra_pages));
3210 }
3211 total_read++;
3eaa2885
CM
3212again:
3213 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
a061fc8d 3214 goto truncate_racing;
edbd8d4e 3215 page = grab_cache_page(inode->i_mapping, i);
a061fc8d 3216 if (!page) {
edbd8d4e 3217 goto out_unlock;
a061fc8d 3218 }
edbd8d4e
CM
3219 if (!PageUptodate(page)) {
3220 btrfs_readpage(NULL, page);
3221 lock_page(page);
3222 if (!PageUptodate(page)) {
3223 unlock_page(page);
3224 page_cache_release(page);
3225 goto out_unlock;
3226 }
3227 }
ec44a35c 3228 wait_on_page_writeback(page);
3eaa2885 3229
edbd8d4e
CM
3230 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3231 page_end = page_start + PAGE_CACHE_SIZE - 1;
d1310b2e 3232 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
edbd8d4e 3233
3eaa2885
CM
3234 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3235 if (ordered) {
3236 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3237 unlock_page(page);
3238 page_cache_release(page);
3239 btrfs_start_ordered_extent(inode, ordered, 1);
3240 btrfs_put_ordered_extent(ordered);
3241 goto again;
3242 }
3243 set_page_extent_mapped(page);
3244
f87f057b
CM
3245 /*
3246 * make sure page_mkwrite is called for this page if userland
3247 * wants to change it from mmap
3248 */
3249 clear_page_dirty_for_io(page);
3eaa2885 3250
ea8c2819 3251 btrfs_set_extent_delalloc(inode, page_start, page_end);
a061fc8d 3252 set_page_dirty(page);
edbd8d4e 3253
d1310b2e 3254 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
edbd8d4e
CM
3255 unlock_page(page);
3256 page_cache_release(page);
3257 }
3258
3259out_unlock:
3eaa2885
CM
3260 /* we have to start the IO in order to get the ordered extents
3261 * instantiated. This allows the relocation to code to wait
3262 * for all the ordered extents to hit the disk.
3263 *
3264 * Otherwise, it would constantly loop over the same extents
3265 * because the old ones don't get deleted until the IO is
3266 * started
3267 */
3268 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
3269 WB_SYNC_NONE);
ec44a35c 3270 kfree(ra);
a061fc8d
CM
3271 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
3272 if (trans) {
a061fc8d
CM
3273 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
3274 mark_inode_dirty(inode);
3275 }
edbd8d4e
CM
3276 mutex_unlock(&inode->i_mutex);
3277 return 0;
a061fc8d
CM
3278
3279truncate_racing:
3280 vmtruncate(inode, inode->i_size);
3281 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
3282 total_read);
3283 goto out_unlock;
edbd8d4e
CM
3284}
3285
bf4ef679
CM
3286/*
3287 * The back references tell us which tree holds a ref on a block,
3288 * but it is possible for the tree root field in the reference to
3289 * reflect the original root before a snapshot was made. In this
3290 * case we should search through all the children of a given root
3291 * to find potential holders of references on a block.
3292 *
3293 * Instead, we do something a little less fancy and just search
3294 * all the roots for a given key/block combination.
3295 */
3296static int find_root_for_ref(struct btrfs_root *root,
3297 struct btrfs_path *path,
3298 struct btrfs_key *key0,
3299 int level,
3300 int file_key,
3301 struct btrfs_root **found_root,
3302 u64 bytenr)
3303{
3304 struct btrfs_key root_location;
3305 struct btrfs_root *cur_root = *found_root;
3306 struct btrfs_file_extent_item *file_extent;
3307 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
3308 u64 found_bytenr;
3309 int ret;
bf4ef679
CM
3310
3311 root_location.offset = (u64)-1;
3312 root_location.type = BTRFS_ROOT_ITEM_KEY;
3313 path->lowest_level = level;
3314 path->reada = 0;
3315 while(1) {
3316 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
3317 found_bytenr = 0;
3318 if (ret == 0 && file_key) {
3319 struct extent_buffer *leaf = path->nodes[0];
3320 file_extent = btrfs_item_ptr(leaf, path->slots[0],
3321 struct btrfs_file_extent_item);
3322 if (btrfs_file_extent_type(leaf, file_extent) ==
3323 BTRFS_FILE_EXTENT_REG) {
3324 found_bytenr =
3325 btrfs_file_extent_disk_bytenr(leaf,
3326 file_extent);
3327 }
323da79c 3328 } else if (!file_key) {
bf4ef679
CM
3329 if (path->nodes[level])
3330 found_bytenr = path->nodes[level]->start;
3331 }
3332
bf4ef679
CM
3333 btrfs_release_path(cur_root, path);
3334
3335 if (found_bytenr == bytenr) {
3336 *found_root = cur_root;
3337 ret = 0;
3338 goto out;
3339 }
3340 ret = btrfs_search_root(root->fs_info->tree_root,
3341 root_search_start, &root_search_start);
3342 if (ret)
3343 break;
3344
3345 root_location.objectid = root_search_start;
3346 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
3347 &root_location);
3348 if (!cur_root) {
3349 ret = 1;
3350 break;
3351 }
3352 }
3353out:
3354 path->lowest_level = 0;
3355 return ret;
3356}
3357
4313b399
CM
3358/*
3359 * note, this releases the path
3360 */
98ed5174 3361static int noinline relocate_one_reference(struct btrfs_root *extent_root,
edbd8d4e 3362 struct btrfs_path *path,
0ef3e66b
CM
3363 struct btrfs_key *extent_key,
3364 u64 *last_file_objectid,
3365 u64 *last_file_offset,
3366 u64 *last_file_root,
3367 u64 last_extent)
edbd8d4e
CM
3368{
3369 struct inode *inode;
3370 struct btrfs_root *found_root;
bf4ef679
CM
3371 struct btrfs_key root_location;
3372 struct btrfs_key found_key;
4313b399
CM
3373 struct btrfs_extent_ref *ref;
3374 u64 ref_root;
3375 u64 ref_gen;
3376 u64 ref_objectid;
3377 u64 ref_offset;
edbd8d4e 3378 int ret;
bf4ef679 3379 int level;
edbd8d4e 3380
7d9eb12c
CM
3381 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
3382
4313b399
CM
3383 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
3384 struct btrfs_extent_ref);
3385 ref_root = btrfs_ref_root(path->nodes[0], ref);
3386 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
3387 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
3388 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
3389 btrfs_release_path(extent_root, path);
3390
bf4ef679 3391 root_location.objectid = ref_root;
edbd8d4e 3392 if (ref_gen == 0)
bf4ef679 3393 root_location.offset = 0;
edbd8d4e 3394 else
bf4ef679
CM
3395 root_location.offset = (u64)-1;
3396 root_location.type = BTRFS_ROOT_ITEM_KEY;
edbd8d4e
CM
3397
3398 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
bf4ef679 3399 &root_location);
edbd8d4e 3400 BUG_ON(!found_root);
7d9eb12c 3401 mutex_unlock(&extent_root->fs_info->alloc_mutex);
edbd8d4e
CM
3402
3403 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
bf4ef679
CM
3404 found_key.objectid = ref_objectid;
3405 found_key.type = BTRFS_EXTENT_DATA_KEY;
3406 found_key.offset = ref_offset;
3407 level = 0;
3408
0ef3e66b
CM
3409 if (last_extent == extent_key->objectid &&
3410 *last_file_objectid == ref_objectid &&
3411 *last_file_offset == ref_offset &&
3412 *last_file_root == ref_root)
3413 goto out;
3414
bf4ef679
CM
3415 ret = find_root_for_ref(extent_root, path, &found_key,
3416 level, 1, &found_root,
3417 extent_key->objectid);
3418
3419 if (ret)
3420 goto out;
3421
0ef3e66b
CM
3422 if (last_extent == extent_key->objectid &&
3423 *last_file_objectid == ref_objectid &&
3424 *last_file_offset == ref_offset &&
3425 *last_file_root == ref_root)
3426 goto out;
3427
edbd8d4e
CM
3428 inode = btrfs_iget_locked(extent_root->fs_info->sb,
3429 ref_objectid, found_root);
3430 if (inode->i_state & I_NEW) {
3431 /* the inode and parent dir are two different roots */
3432 BTRFS_I(inode)->root = found_root;
3433 BTRFS_I(inode)->location.objectid = ref_objectid;
3434 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
3435 BTRFS_I(inode)->location.offset = 0;
3436 btrfs_read_locked_inode(inode);
3437 unlock_new_inode(inode);
3438
3439 }
3440 /* this can happen if the reference is not against
3441 * the latest version of the tree root
3442 */
7d9eb12c 3443 if (is_bad_inode(inode))
edbd8d4e 3444 goto out;
7d9eb12c 3445
0ef3e66b
CM
3446 *last_file_objectid = inode->i_ino;
3447 *last_file_root = found_root->root_key.objectid;
3448 *last_file_offset = ref_offset;
3449
edbd8d4e 3450 relocate_inode_pages(inode, ref_offset, extent_key->offset);
edbd8d4e 3451 iput(inode);
edbd8d4e
CM
3452 } else {
3453 struct btrfs_trans_handle *trans;
edbd8d4e 3454 struct extent_buffer *eb;
7d9eb12c 3455 int needs_lock = 0;
edbd8d4e 3456
edbd8d4e 3457 eb = read_tree_block(found_root, extent_key->objectid,
ca7a79ad 3458 extent_key->offset, 0);
925baedd 3459 btrfs_tree_lock(eb);
edbd8d4e
CM
3460 level = btrfs_header_level(eb);
3461
3462 if (level == 0)
3463 btrfs_item_key_to_cpu(eb, &found_key, 0);
3464 else
3465 btrfs_node_key_to_cpu(eb, &found_key, 0);
3466
925baedd 3467 btrfs_tree_unlock(eb);
edbd8d4e
CM
3468 free_extent_buffer(eb);
3469
bf4ef679
CM
3470 ret = find_root_for_ref(extent_root, path, &found_key,
3471 level, 0, &found_root,
3472 extent_key->objectid);
3473
3474 if (ret)
3475 goto out;
3476
7d9eb12c
CM
3477 /*
3478 * right here almost anything could happen to our key,
3479 * but that's ok. The cow below will either relocate it
3480 * or someone else will have relocated it. Either way,
3481 * it is in a different spot than it was before and
3482 * we're happy.
3483 */
3484
bf4ef679
CM
3485 trans = btrfs_start_transaction(found_root, 1);
3486
7d9eb12c
CM
3487 if (found_root == extent_root->fs_info->extent_root ||
3488 found_root == extent_root->fs_info->chunk_root ||
3489 found_root == extent_root->fs_info->dev_root) {
3490 needs_lock = 1;
3491 mutex_lock(&extent_root->fs_info->alloc_mutex);
3492 }
3493
edbd8d4e 3494 path->lowest_level = level;
8f662a76 3495 path->reada = 2;
edbd8d4e
CM
3496 ret = btrfs_search_slot(trans, found_root, &found_key, path,
3497 0, 1);
3498 path->lowest_level = 0;
edbd8d4e 3499 btrfs_release_path(found_root, path);
7d9eb12c 3500
0ef3e66b
CM
3501 if (found_root == found_root->fs_info->extent_root)
3502 btrfs_extent_post_op(trans, found_root);
7d9eb12c
CM
3503 if (needs_lock)
3504 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3505
edbd8d4e 3506 btrfs_end_transaction(trans, found_root);
edbd8d4e 3507
7d9eb12c 3508 }
edbd8d4e 3509out:
7d9eb12c 3510 mutex_lock(&extent_root->fs_info->alloc_mutex);
edbd8d4e
CM
3511 return 0;
3512}
3513
a061fc8d
CM
3514static int noinline del_extent_zero(struct btrfs_root *extent_root,
3515 struct btrfs_path *path,
3516 struct btrfs_key *extent_key)
3517{
3518 int ret;
3519 struct btrfs_trans_handle *trans;
3520
3521 trans = btrfs_start_transaction(extent_root, 1);
3522 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
3523 if (ret > 0) {
3524 ret = -EIO;
3525 goto out;
3526 }
3527 if (ret < 0)
3528 goto out;
3529 ret = btrfs_del_item(trans, extent_root, path);
3530out:
3531 btrfs_end_transaction(trans, extent_root);
3532 return ret;
3533}
3534
98ed5174
CM
3535static int noinline relocate_one_extent(struct btrfs_root *extent_root,
3536 struct btrfs_path *path,
3537 struct btrfs_key *extent_key)
edbd8d4e
CM
3538{
3539 struct btrfs_key key;
3540 struct btrfs_key found_key;
edbd8d4e 3541 struct extent_buffer *leaf;
0ef3e66b
CM
3542 u64 last_file_objectid = 0;
3543 u64 last_file_root = 0;
3544 u64 last_file_offset = (u64)-1;
3545 u64 last_extent = 0;
edbd8d4e
CM
3546 u32 nritems;
3547 u32 item_size;
3548 int ret = 0;
3549
a061fc8d
CM
3550 if (extent_key->objectid == 0) {
3551 ret = del_extent_zero(extent_root, path, extent_key);
3552 goto out;
3553 }
edbd8d4e
CM
3554 key.objectid = extent_key->objectid;
3555 key.type = BTRFS_EXTENT_REF_KEY;
3556 key.offset = 0;
3557
3558 while(1) {
3559 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3560
edbd8d4e
CM
3561 if (ret < 0)
3562 goto out;
3563
3564 ret = 0;
3565 leaf = path->nodes[0];
3566 nritems = btrfs_header_nritems(leaf);
a061fc8d
CM
3567 if (path->slots[0] == nritems) {
3568 ret = btrfs_next_leaf(extent_root, path);
3569 if (ret > 0) {
3570 ret = 0;
3571 goto out;
3572 }
3573 if (ret < 0)
3574 goto out;
bf4ef679 3575 leaf = path->nodes[0];
a061fc8d 3576 }
edbd8d4e
CM
3577
3578 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
a061fc8d 3579 if (found_key.objectid != extent_key->objectid) {
edbd8d4e 3580 break;
a061fc8d 3581 }
edbd8d4e 3582
a061fc8d 3583 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
edbd8d4e 3584 break;
a061fc8d 3585 }
edbd8d4e
CM
3586
3587 key.offset = found_key.offset + 1;
3588 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3589
0ef3e66b
CM
3590 ret = relocate_one_reference(extent_root, path, extent_key,
3591 &last_file_objectid,
3592 &last_file_offset,
3593 &last_file_root, last_extent);
edbd8d4e
CM
3594 if (ret)
3595 goto out;
0ef3e66b 3596 last_extent = extent_key->objectid;
edbd8d4e
CM
3597 }
3598 ret = 0;
3599out:
3600 btrfs_release_path(extent_root, path);
3601 return ret;
3602}
3603
ec44a35c
CM
3604static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3605{
3606 u64 num_devices;
3607 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3608 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3609
a061fc8d 3610 num_devices = root->fs_info->fs_devices->num_devices;
ec44a35c
CM
3611 if (num_devices == 1) {
3612 stripped |= BTRFS_BLOCK_GROUP_DUP;
3613 stripped = flags & ~stripped;
3614
3615 /* turn raid0 into single device chunks */
3616 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3617 return stripped;
3618
3619 /* turn mirroring into duplication */
3620 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3621 BTRFS_BLOCK_GROUP_RAID10))
3622 return stripped | BTRFS_BLOCK_GROUP_DUP;
3623 return flags;
3624 } else {
3625 /* they already had raid on here, just return */
ec44a35c
CM
3626 if (flags & stripped)
3627 return flags;
3628
3629 stripped |= BTRFS_BLOCK_GROUP_DUP;
3630 stripped = flags & ~stripped;
3631
3632 /* switch duplicated blocks with raid1 */
3633 if (flags & BTRFS_BLOCK_GROUP_DUP)
3634 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3635
3636 /* turn single device chunks into raid0 */
3637 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3638 }
3639 return flags;
3640}
3641
0ef3e66b
CM
3642int __alloc_chunk_for_shrink(struct btrfs_root *root,
3643 struct btrfs_block_group_cache *shrink_block_group,
3644 int force)
3645{
3646 struct btrfs_trans_handle *trans;
3647 u64 new_alloc_flags;
3648 u64 calc;
3649
c286ac48 3650 spin_lock(&shrink_block_group->lock);
0ef3e66b 3651 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
c286ac48 3652 spin_unlock(&shrink_block_group->lock);
7d9eb12c 3653 mutex_unlock(&root->fs_info->alloc_mutex);
c286ac48 3654
0ef3e66b 3655 trans = btrfs_start_transaction(root, 1);
7d9eb12c 3656 mutex_lock(&root->fs_info->alloc_mutex);
c286ac48 3657 spin_lock(&shrink_block_group->lock);
7d9eb12c 3658
0ef3e66b
CM
3659 new_alloc_flags = update_block_group_flags(root,
3660 shrink_block_group->flags);
3661 if (new_alloc_flags != shrink_block_group->flags) {
3662 calc =
3663 btrfs_block_group_used(&shrink_block_group->item);
3664 } else {
3665 calc = shrink_block_group->key.offset;
3666 }
c286ac48
CM
3667 spin_unlock(&shrink_block_group->lock);
3668
0ef3e66b
CM
3669 do_chunk_alloc(trans, root->fs_info->extent_root,
3670 calc + 2 * 1024 * 1024, new_alloc_flags, force);
7d9eb12c
CM
3671
3672 mutex_unlock(&root->fs_info->alloc_mutex);
0ef3e66b 3673 btrfs_end_transaction(trans, root);
7d9eb12c 3674 mutex_lock(&root->fs_info->alloc_mutex);
c286ac48
CM
3675 } else
3676 spin_unlock(&shrink_block_group->lock);
0ef3e66b
CM
3677 return 0;
3678}
3679
8f18cf13 3680int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
edbd8d4e
CM
3681{
3682 struct btrfs_trans_handle *trans;
3683 struct btrfs_root *tree_root = root->fs_info->tree_root;
3684 struct btrfs_path *path;
3685 u64 cur_byte;
3686 u64 total_found;
8f18cf13
CM
3687 u64 shrink_last_byte;
3688 struct btrfs_block_group_cache *shrink_block_group;
edbd8d4e 3689 struct btrfs_key key;
73e48b27 3690 struct btrfs_key found_key;
edbd8d4e
CM
3691 struct extent_buffer *leaf;
3692 u32 nritems;
3693 int ret;
a061fc8d 3694 int progress;
edbd8d4e 3695
925baedd 3696 mutex_lock(&root->fs_info->alloc_mutex);
8f18cf13
CM
3697 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3698 shrink_start);
3699 BUG_ON(!shrink_block_group);
3700
0ef3e66b
CM
3701 shrink_last_byte = shrink_block_group->key.objectid +
3702 shrink_block_group->key.offset;
8f18cf13
CM
3703
3704 shrink_block_group->space_info->total_bytes -=
3705 shrink_block_group->key.offset;
edbd8d4e
CM
3706 path = btrfs_alloc_path();
3707 root = root->fs_info->extent_root;
8f662a76 3708 path->reada = 2;
edbd8d4e 3709
323da79c
CM
3710 printk("btrfs relocating block group %llu flags %llu\n",
3711 (unsigned long long)shrink_start,
3712 (unsigned long long)shrink_block_group->flags);
3713
0ef3e66b
CM
3714 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
3715
edbd8d4e 3716again:
323da79c 3717
8f18cf13
CM
3718 shrink_block_group->ro = 1;
3719
edbd8d4e 3720 total_found = 0;
a061fc8d 3721 progress = 0;
8f18cf13 3722 key.objectid = shrink_start;
edbd8d4e
CM
3723 key.offset = 0;
3724 key.type = 0;
73e48b27 3725 cur_byte = key.objectid;
4313b399 3726
ea8c2819
CM
3727 mutex_unlock(&root->fs_info->alloc_mutex);
3728
3729 btrfs_start_delalloc_inodes(root);
7ea394f1 3730 btrfs_wait_ordered_extents(tree_root, 0);
ea8c2819
CM
3731
3732 mutex_lock(&root->fs_info->alloc_mutex);
3733
73e48b27
Y
3734 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3735 if (ret < 0)
3736 goto out;
3737
0b86a832 3738 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
73e48b27
Y
3739 if (ret < 0)
3740 goto out;
8f18cf13 3741
73e48b27
Y
3742 if (ret == 0) {
3743 leaf = path->nodes[0];
3744 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8f18cf13
CM
3745 if (found_key.objectid + found_key.offset > shrink_start &&
3746 found_key.objectid < shrink_last_byte) {
73e48b27
Y
3747 cur_byte = found_key.objectid;
3748 key.objectid = cur_byte;
3749 }
3750 }
3751 btrfs_release_path(root, path);
3752
3753 while(1) {
edbd8d4e
CM
3754 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3755 if (ret < 0)
3756 goto out;
73e48b27 3757
7d9eb12c 3758next:
edbd8d4e 3759 leaf = path->nodes[0];
73e48b27 3760 nritems = btrfs_header_nritems(leaf);
73e48b27
Y
3761 if (path->slots[0] >= nritems) {
3762 ret = btrfs_next_leaf(root, path);
3763 if (ret < 0)
3764 goto out;
3765 if (ret == 1) {
3766 ret = 0;
3767 break;
edbd8d4e 3768 }
73e48b27
Y
3769 leaf = path->nodes[0];
3770 nritems = btrfs_header_nritems(leaf);
edbd8d4e 3771 }
73e48b27
Y
3772
3773 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
725c8463 3774
8f18cf13
CM
3775 if (found_key.objectid >= shrink_last_byte)
3776 break;
3777
725c8463
CM
3778 if (progress && need_resched()) {
3779 memcpy(&key, &found_key, sizeof(key));
725c8463 3780 cond_resched();
725c8463
CM
3781 btrfs_release_path(root, path);
3782 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3783 progress = 0;
3784 goto next;
3785 }
3786 progress = 1;
3787
73e48b27
Y
3788 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3789 found_key.objectid + found_key.offset <= cur_byte) {
0ef3e66b
CM
3790 memcpy(&key, &found_key, sizeof(key));
3791 key.offset++;
edbd8d4e 3792 path->slots[0]++;
edbd8d4e
CM
3793 goto next;
3794 }
73e48b27 3795
edbd8d4e
CM
3796 total_found++;
3797 cur_byte = found_key.objectid + found_key.offset;
3798 key.objectid = cur_byte;
3799 btrfs_release_path(root, path);
3800 ret = relocate_one_extent(root, path, &found_key);
0ef3e66b 3801 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
edbd8d4e
CM
3802 }
3803
3804 btrfs_release_path(root, path);
3805
3806 if (total_found > 0) {
323da79c
CM
3807 printk("btrfs relocate found %llu last extent was %llu\n",
3808 (unsigned long long)total_found,
3809 (unsigned long long)found_key.objectid);
7d9eb12c 3810 mutex_unlock(&root->fs_info->alloc_mutex);
edbd8d4e
CM
3811 trans = btrfs_start_transaction(tree_root, 1);
3812 btrfs_commit_transaction(trans, tree_root);
3813
edbd8d4e 3814 btrfs_clean_old_snapshots(tree_root);
edbd8d4e 3815
ea8c2819 3816 btrfs_start_delalloc_inodes(root);
7ea394f1 3817 btrfs_wait_ordered_extents(tree_root, 0);
3eaa2885 3818
edbd8d4e
CM
3819 trans = btrfs_start_transaction(tree_root, 1);
3820 btrfs_commit_transaction(trans, tree_root);
7d9eb12c 3821 mutex_lock(&root->fs_info->alloc_mutex);
edbd8d4e
CM
3822 goto again;
3823 }
3824
8f18cf13
CM
3825 /*
3826 * we've freed all the extents, now remove the block
3827 * group item from the tree
3828 */
7d9eb12c
CM
3829 mutex_unlock(&root->fs_info->alloc_mutex);
3830
edbd8d4e 3831 trans = btrfs_start_transaction(root, 1);
c286ac48 3832
7d9eb12c 3833 mutex_lock(&root->fs_info->alloc_mutex);
8f18cf13 3834 memcpy(&key, &shrink_block_group->key, sizeof(key));
1372f8e6 3835
8f18cf13
CM
3836 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3837 if (ret > 0)
3838 ret = -EIO;
8e8a1e31
JB
3839 if (ret < 0) {
3840 btrfs_end_transaction(trans, root);
8f18cf13 3841 goto out;
8e8a1e31 3842 }
73e48b27 3843
0f9dd46c
JB
3844 spin_lock(&root->fs_info->block_group_cache_lock);
3845 rb_erase(&shrink_block_group->cache_node,
3846 &root->fs_info->block_group_cache_tree);
3847 spin_unlock(&root->fs_info->block_group_cache_lock);
0ef3e66b 3848
0f9dd46c
JB
3849 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3850 key.offset);
3851 if (ret) {
3852 btrfs_end_transaction(trans, root);
3853 goto out;
3854 }
d7a029a8 3855 /*
0ef3e66b
CM
3856 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3857 kfree(shrink_block_group);
d7a029a8 3858 */
0ef3e66b 3859
8f18cf13 3860 btrfs_del_item(trans, root, path);
7d9eb12c
CM
3861 btrfs_release_path(root, path);
3862 mutex_unlock(&root->fs_info->alloc_mutex);
edbd8d4e 3863 btrfs_commit_transaction(trans, root);
0ef3e66b 3864
7d9eb12c
CM
3865 mutex_lock(&root->fs_info->alloc_mutex);
3866
0ef3e66b
CM
3867 /* the code to unpin extents might set a few bits in the free
3868 * space cache for this range again
3869 */
0f9dd46c
JB
3870 /* XXX? */
3871 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3872 key.offset);
edbd8d4e
CM
3873out:
3874 btrfs_free_path(path);
925baedd 3875 mutex_unlock(&root->fs_info->alloc_mutex);
edbd8d4e
CM
3876 return ret;
3877}
3878
0b86a832
CM
3879int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3880 struct btrfs_key *key)
3881{
925baedd 3882 int ret = 0;
0b86a832
CM
3883 struct btrfs_key found_key;
3884 struct extent_buffer *leaf;
3885 int slot;
edbd8d4e 3886
0b86a832
CM
3887 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3888 if (ret < 0)
925baedd
CM
3889 goto out;
3890
0b86a832
CM
3891 while(1) {
3892 slot = path->slots[0];
edbd8d4e 3893 leaf = path->nodes[0];
0b86a832
CM
3894 if (slot >= btrfs_header_nritems(leaf)) {
3895 ret = btrfs_next_leaf(root, path);
3896 if (ret == 0)
3897 continue;
3898 if (ret < 0)
925baedd 3899 goto out;
0b86a832 3900 break;
edbd8d4e 3901 }
0b86a832 3902 btrfs_item_key_to_cpu(leaf, &found_key, slot);
edbd8d4e 3903
0b86a832 3904 if (found_key.objectid >= key->objectid &&
925baedd
CM
3905 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3906 ret = 0;
3907 goto out;
3908 }
0b86a832 3909 path->slots[0]++;
edbd8d4e 3910 }
0b86a832 3911 ret = -ENOENT;
925baedd 3912out:
0b86a832 3913 return ret;
edbd8d4e
CM
3914}
3915
9078a3e1
CM
3916int btrfs_read_block_groups(struct btrfs_root *root)
3917{
3918 struct btrfs_path *path;
3919 int ret;
9078a3e1 3920 struct btrfs_block_group_cache *cache;
be744175 3921 struct btrfs_fs_info *info = root->fs_info;
6324fbf3 3922 struct btrfs_space_info *space_info;
9078a3e1
CM
3923 struct btrfs_key key;
3924 struct btrfs_key found_key;
5f39d397 3925 struct extent_buffer *leaf;
96b5179d 3926
be744175 3927 root = info->extent_root;
9078a3e1 3928 key.objectid = 0;
0b86a832 3929 key.offset = 0;
9078a3e1 3930 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
9078a3e1
CM
3931 path = btrfs_alloc_path();
3932 if (!path)
3933 return -ENOMEM;
3934
925baedd 3935 mutex_lock(&root->fs_info->alloc_mutex);
9078a3e1 3936 while(1) {
0b86a832
CM
3937 ret = find_first_block_group(root, path, &key);
3938 if (ret > 0) {
3939 ret = 0;
3940 goto error;
9078a3e1 3941 }
0b86a832
CM
3942 if (ret != 0)
3943 goto error;
3944
5f39d397
CM
3945 leaf = path->nodes[0];
3946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8f18cf13 3947 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9078a3e1 3948 if (!cache) {
0b86a832 3949 ret = -ENOMEM;
9078a3e1
CM
3950 break;
3951 }
3e1ad54f 3952
c286ac48 3953 spin_lock_init(&cache->lock);
0f9dd46c 3954 INIT_LIST_HEAD(&cache->list);
5f39d397
CM
3955 read_extent_buffer(leaf, &cache->item,
3956 btrfs_item_ptr_offset(leaf, path->slots[0]),
3957 sizeof(cache->item));
9078a3e1 3958 memcpy(&cache->key, &found_key, sizeof(found_key));
0b86a832 3959
9078a3e1
CM
3960 key.objectid = found_key.objectid + found_key.offset;
3961 btrfs_release_path(root, path);
0b86a832 3962 cache->flags = btrfs_block_group_flags(&cache->item);
96b5179d 3963
6324fbf3
CM
3964 ret = update_space_info(info, cache->flags, found_key.offset,
3965 btrfs_block_group_used(&cache->item),
3966 &space_info);
3967 BUG_ON(ret);
3968 cache->space_info = space_info;
0f9dd46c
JB
3969 spin_lock(&space_info->lock);
3970 list_add(&cache->list, &space_info->block_groups);
3971 spin_unlock(&space_info->lock);
3972
3973 ret = btrfs_add_block_group_cache(root->fs_info, cache);
3974 BUG_ON(ret);
9078a3e1 3975 }
0b86a832
CM
3976 ret = 0;
3977error:
9078a3e1 3978 btrfs_free_path(path);
925baedd 3979 mutex_unlock(&root->fs_info->alloc_mutex);
0b86a832 3980 return ret;
9078a3e1 3981}
6324fbf3
CM
3982
3983int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3984 struct btrfs_root *root, u64 bytes_used,
e17cade2 3985 u64 type, u64 chunk_objectid, u64 chunk_offset,
6324fbf3
CM
3986 u64 size)
3987{
3988 int ret;
6324fbf3
CM
3989 struct btrfs_root *extent_root;
3990 struct btrfs_block_group_cache *cache;
6324fbf3 3991
7d9eb12c 3992 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
6324fbf3 3993 extent_root = root->fs_info->extent_root;
6324fbf3 3994
e02119d5
CM
3995 root->fs_info->last_trans_new_blockgroup = trans->transid;
3996
8f18cf13 3997 cache = kzalloc(sizeof(*cache), GFP_NOFS);
0f9dd46c
JB
3998 if (!cache)
3999 return -ENOMEM;
4000
e17cade2 4001 cache->key.objectid = chunk_offset;
6324fbf3 4002 cache->key.offset = size;
c286ac48 4003 spin_lock_init(&cache->lock);
0f9dd46c 4004 INIT_LIST_HEAD(&cache->list);
6324fbf3 4005 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
0ef3e66b 4006
6324fbf3 4007 btrfs_set_block_group_used(&cache->item, bytes_used);
6324fbf3
CM
4008 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
4009 cache->flags = type;
4010 btrfs_set_block_group_flags(&cache->item, type);
4011
4012 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
4013 &cache->space_info);
4014 BUG_ON(ret);
0f9dd46c
JB
4015 spin_lock(&cache->space_info->lock);
4016 list_add(&cache->list, &cache->space_info->block_groups);
4017 spin_unlock(&cache->space_info->lock);
6324fbf3 4018
0f9dd46c
JB
4019 ret = btrfs_add_block_group_cache(root->fs_info, cache);
4020 BUG_ON(ret);
c286ac48 4021
6324fbf3
CM
4022 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
4023 sizeof(cache->item));
4024 BUG_ON(ret);
4025
4026 finish_current_insert(trans, extent_root);
4027 ret = del_pending_extents(trans, extent_root);
4028 BUG_ON(ret);
d18a2c44 4029 set_avail_alloc_bits(extent_root->fs_info, type);
925baedd 4030
6324fbf3
CM
4031 return 0;
4032}