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