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