Btrfs: Fix failure cleanups when allocating extent buffers fail
[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 */
18
ec6b910f 19#include <linux/sched.h>
fec577fb
CM
20#include "ctree.h"
21#include "disk-io.h"
22#include "print-tree.h"
e089f05c 23#include "transaction.h"
fec577fb 24
96b5179d
CM
25#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
26#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
27#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
28
e089f05c
CM
29static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30 btrfs_root *extent_root);
e20d96d6
CM
31static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
fec577fb 33
e37c9e69
CM
34static int cache_block_group(struct btrfs_root *root,
35 struct btrfs_block_group_cache *block_group)
36{
37 struct btrfs_path *path;
38 int ret;
39 struct btrfs_key key;
5f39d397 40 struct extent_buffer *leaf;
f510cfec 41 struct extent_map_tree *free_space_cache;
e37c9e69 42 int slot;
e37c9e69
CM
43 u64 last = 0;
44 u64 hole_size;
7d7d6068 45 u64 first_free;
e37c9e69
CM
46 int found = 0;
47
48 root = root->fs_info->extent_root;
f510cfec 49 free_space_cache = &root->fs_info->free_space_cache;
e37c9e69
CM
50
51 if (block_group->cached)
52 return 0;
f510cfec 53
e37c9e69
CM
54 path = btrfs_alloc_path();
55 if (!path)
56 return -ENOMEM;
7d7d6068 57
2cc58cf2 58 path->reada = 2;
7d7d6068 59 first_free = block_group->key.objectid;
e37c9e69 60 key.objectid = block_group->key.objectid;
e37c9e69 61 key.offset = 0;
7d7d6068 62
e37c9e69
CM
63 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
64 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7d7d6068 65
e37c9e69
CM
66 if (ret < 0)
67 return ret;
7d7d6068 68
e37c9e69
CM
69 if (ret && path->slots[0] > 0)
70 path->slots[0]--;
7d7d6068 71
e37c9e69 72 while(1) {
5f39d397 73 leaf = path->nodes[0];
e37c9e69 74 slot = path->slots[0];
5f39d397 75 if (slot >= btrfs_header_nritems(leaf)) {
e37c9e69 76 ret = btrfs_next_leaf(root, path);
54aa1f4d
CM
77 if (ret < 0)
78 goto err;
de428b63 79 if (ret == 0) {
e37c9e69 80 continue;
de428b63 81 } else {
e37c9e69
CM
82 break;
83 }
84 }
7d7d6068 85
5f39d397 86 btrfs_item_key_to_cpu(leaf, &key, slot);
7d7d6068
Y
87 if (key.objectid < block_group->key.objectid) {
88 if (key.objectid + key.offset > first_free)
89 first_free = key.objectid + key.offset;
90 goto next;
91 }
92
e37c9e69
CM
93 if (key.objectid >= block_group->key.objectid +
94 block_group->key.offset) {
e37c9e69
CM
95 break;
96 }
7d7d6068 97
e37c9e69
CM
98 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
99 if (!found) {
7d7d6068 100 last = first_free;
e37c9e69 101 found = 1;
e37c9e69 102 }
f510cfec
CM
103 if (key.objectid > last) {
104 hole_size = key.objectid - last;
105 set_extent_dirty(free_space_cache, last,
106 last + hole_size - 1,
107 GFP_NOFS);
7d7d6068
Y
108 }
109 last = key.objectid + key.offset;
e37c9e69 110 }
7d7d6068 111next:
e37c9e69
CM
112 path->slots[0]++;
113 }
114
7d7d6068
Y
115 if (!found)
116 last = first_free;
117 if (block_group->key.objectid +
118 block_group->key.offset > last) {
119 hole_size = block_group->key.objectid +
120 block_group->key.offset - last;
f510cfec
CM
121 set_extent_dirty(free_space_cache, last,
122 last + hole_size - 1, GFP_NOFS);
7d7d6068 123 }
e37c9e69 124 block_group->cached = 1;
54aa1f4d 125err:
e37c9e69
CM
126 btrfs_free_path(path);
127 return 0;
128}
129
5276aeda
CM
130struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
131 btrfs_fs_info *info,
db94535d 132 u64 bytenr)
be744175 133{
96b5179d
CM
134 struct extent_map_tree *block_group_cache;
135 struct btrfs_block_group_cache *block_group = NULL;
136 u64 ptr;
137 u64 start;
138 u64 end;
be744175
CM
139 int ret;
140
96b5179d
CM
141 block_group_cache = &info->block_group_cache;
142 ret = find_first_extent_bit(block_group_cache,
db94535d 143 bytenr, &start, &end,
96b5179d 144 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
be744175 145 if (ret) {
96b5179d 146 return NULL;
be744175 147 }
96b5179d
CM
148 ret = get_state_private(block_group_cache, start, &ptr);
149 if (ret)
150 return NULL;
151
ae2f5411 152 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
153
154
db94535d 155 if (block_group->key.objectid <= bytenr && bytenr <=
96b5179d
CM
156 block_group->key.objectid + block_group->key.offset)
157 return block_group;
158
be744175
CM
159 return NULL;
160}
161
e37c9e69
CM
162static u64 find_search_start(struct btrfs_root *root,
163 struct btrfs_block_group_cache **cache_ret,
f510cfec 164 u64 search_start, int num, int data)
e37c9e69 165{
e37c9e69
CM
166 int ret;
167 struct btrfs_block_group_cache *cache = *cache_ret;
e19caa5f 168 u64 last;
f510cfec
CM
169 u64 start = 0;
170 u64 end = 0;
f84a8b36 171 int wrapped = 0;
e37c9e69 172
e37c9e69 173again:
54aa1f4d
CM
174 ret = cache_block_group(root, cache);
175 if (ret)
176 goto out;
f84a8b36 177
e19caa5f
CM
178 last = max(search_start, cache->key.objectid);
179
e37c9e69 180 while(1) {
f510cfec
CM
181 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
182 last, &start, &end, EXTENT_DIRTY);
e19caa5f
CM
183 if (ret) {
184 goto new_group;
185 }
f510cfec
CM
186
187 start = max(last, start);
188 last = end + 1;
189 if (end + 1 - start < num)
190 continue;
96b5179d 191 if (start + num >= cache->key.objectid + cache->key.offset)
e37c9e69 192 goto new_group;
f510cfec 193 return start;
e37c9e69
CM
194 }
195out:
1a5bc167 196 return search_start;
e37c9e69
CM
197
198new_group:
e19caa5f 199 last = cache->key.objectid + cache->key.offset;
f84a8b36 200wrapped:
e19caa5f 201 cache = btrfs_lookup_block_group(root->fs_info, last);
e37c9e69 202 if (!cache) {
f84a8b36
CM
203 if (!wrapped) {
204 wrapped = 1;
205 last = search_start;
206 data = BTRFS_BLOCK_GROUP_MIXED;
207 goto wrapped;
208 }
1a5bc167 209 return search_start;
e37c9e69 210 }
e19caa5f 211 cache = btrfs_find_block_group(root, cache, last, data, 0);
e37c9e69
CM
212 *cache_ret = cache;
213 goto again;
214}
215
84f54cfa
CM
216static u64 div_factor(u64 num, int factor)
217{
218 num *= factor;
219 do_div(num, 10);
220 return num;
221}
222
31f3c99b
CM
223struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
224 struct btrfs_block_group_cache
be744175 225 *hint, u64 search_start,
de428b63 226 int data, int owner)
cd1bc465 227{
96b5179d
CM
228 struct btrfs_block_group_cache *cache;
229 struct extent_map_tree *block_group_cache;
31f3c99b 230 struct btrfs_block_group_cache *found_group = NULL;
cd1bc465
CM
231 struct btrfs_fs_info *info = root->fs_info;
232 u64 used;
31f3c99b
CM
233 u64 last = 0;
234 u64 hint_last;
96b5179d
CM
235 u64 start;
236 u64 end;
237 u64 free_check;
238 u64 ptr;
239 int bit;
cd1bc465 240 int ret;
31f3c99b 241 int full_search = 0;
de428b63 242 int factor = 8;
1e2677e0 243 int data_swap = 0;
de428b63 244
96b5179d
CM
245 block_group_cache = &info->block_group_cache;
246
de428b63 247 if (!owner)
f84a8b36 248 factor = 8;
be744175 249
f84a8b36
CM
250 if (data == BTRFS_BLOCK_GROUP_MIXED)
251 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
252 else if (data)
96b5179d
CM
253 bit = BLOCK_GROUP_DATA;
254 else
255 bit = BLOCK_GROUP_METADATA;
be744175
CM
256
257 if (search_start) {
258 struct btrfs_block_group_cache *shint;
5276aeda 259 shint = btrfs_lookup_block_group(info, search_start);
f84a8b36
CM
260 if (shint && (shint->data == data ||
261 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
be744175 262 used = btrfs_block_group_used(&shint->item);
1a5bc167 263 if (used < div_factor(shint->key.offset, factor)) {
be744175
CM
264 return shint;
265 }
266 }
267 }
f84a8b36
CM
268 if (hint && (hint->data == data ||
269 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
31f3c99b 270 used = btrfs_block_group_used(&hint->item);
1a5bc167 271 if (used < div_factor(hint->key.offset, factor)) {
31f3c99b
CM
272 return hint;
273 }
e19caa5f 274 last = hint->key.objectid + hint->key.offset;
31f3c99b
CM
275 hint_last = last;
276 } else {
e37c9e69
CM
277 if (hint)
278 hint_last = max(hint->key.objectid, search_start);
279 else
280 hint_last = search_start;
281
282 last = hint_last;
31f3c99b 283 }
31f3c99b 284again:
cd1bc465 285 while(1) {
96b5179d
CM
286 ret = find_first_extent_bit(block_group_cache, last,
287 &start, &end, bit);
288 if (ret)
cd1bc465 289 break;
96b5179d
CM
290
291 ret = get_state_private(block_group_cache, start, &ptr);
292 if (ret)
293 break;
294
ae2f5411 295 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
296 last = cache->key.objectid + cache->key.offset;
297 used = btrfs_block_group_used(&cache->item);
298
299 if (full_search)
300 free_check = cache->key.offset;
301 else
302 free_check = div_factor(cache->key.offset, factor);
303
1a5bc167 304 if (used < free_check) {
96b5179d
CM
305 found_group = cache;
306 goto found;
cd1bc465 307 }
de428b63 308 cond_resched();
cd1bc465 309 }
31f3c99b 310 if (!full_search) {
be744175 311 last = search_start;
31f3c99b
CM
312 full_search = 1;
313 goto again;
314 }
1e2677e0 315 if (!data_swap) {
1e2677e0 316 data_swap = 1;
96b5179d 317 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1e2677e0
CM
318 last = search_start;
319 goto again;
320 }
be744175 321found:
31f3c99b 322 return found_group;
cd1bc465
CM
323}
324
b18c6685
CM
325int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root,
db94535d 327 u64 bytenr, u64 num_bytes)
02217ed2 328{
5caf2a00 329 struct btrfs_path *path;
02217ed2 330 int ret;
e2fa7227 331 struct btrfs_key key;
5f39d397 332 struct extent_buffer *l;
234b63a0 333 struct btrfs_extent_item *item;
cf27e1ee 334 u32 refs;
037e6390 335
db94535d 336 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 337 path = btrfs_alloc_path();
54aa1f4d
CM
338 if (!path)
339 return -ENOMEM;
26b8003f 340
db94535d 341 key.objectid = bytenr;
62e2749e 342 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 343 key.offset = num_bytes;
5caf2a00 344 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 345 0, 1);
54aa1f4d
CM
346 if (ret < 0)
347 return ret;
a429e513 348 if (ret != 0) {
a28ec197 349 BUG();
a429e513 350 }
02217ed2 351 BUG_ON(ret != 0);
5f39d397 352 l = path->nodes[0];
5caf2a00 353 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397
CM
354 refs = btrfs_extent_refs(l, item);
355 btrfs_set_extent_refs(l, item, refs + 1);
5caf2a00 356 btrfs_mark_buffer_dirty(path->nodes[0]);
a28ec197 357
5caf2a00
CM
358 btrfs_release_path(root->fs_info->extent_root, path);
359 btrfs_free_path(path);
9f5fae2f 360 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 361 del_pending_extents(trans, root->fs_info->extent_root);
02217ed2
CM
362 return 0;
363}
364
e9d0b13b
CM
365int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
366 struct btrfs_root *root)
367{
368 finish_current_insert(trans, root->fs_info->extent_root);
369 del_pending_extents(trans, root->fs_info->extent_root);
370 return 0;
371}
372
b18c6685 373static int lookup_extent_ref(struct btrfs_trans_handle *trans,
db94535d
CM
374 struct btrfs_root *root, u64 bytenr,
375 u64 num_bytes, u32 *refs)
a28ec197 376{
5caf2a00 377 struct btrfs_path *path;
a28ec197 378 int ret;
e2fa7227 379 struct btrfs_key key;
5f39d397 380 struct extent_buffer *l;
234b63a0 381 struct btrfs_extent_item *item;
5caf2a00 382
db94535d 383 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 384 path = btrfs_alloc_path();
db94535d
CM
385 key.objectid = bytenr;
386 key.offset = num_bytes;
62e2749e 387 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
5caf2a00 388 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 389 0, 0);
54aa1f4d
CM
390 if (ret < 0)
391 goto out;
5f39d397
CM
392 if (ret != 0) {
393 btrfs_print_leaf(root, path->nodes[0]);
db94535d 394 printk("failed to find block number %Lu\n", bytenr);
a28ec197 395 BUG();
5f39d397
CM
396 }
397 l = path->nodes[0];
5caf2a00 398 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397 399 *refs = btrfs_extent_refs(l, item);
54aa1f4d 400out:
5caf2a00 401 btrfs_free_path(path);
a28ec197
CM
402 return 0;
403}
404
c5739bba
CM
405int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
406 struct btrfs_root *root)
407{
db94535d
CM
408 return btrfs_inc_extent_ref(trans, root, root->node->start,
409 root->node->len);
c5739bba
CM
410}
411
e089f05c 412int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 413 struct extent_buffer *buf)
02217ed2 414{
db94535d 415 u64 bytenr;
5f39d397
CM
416 u32 nritems;
417 struct btrfs_key key;
6407bf6d 418 struct btrfs_file_extent_item *fi;
02217ed2 419 int i;
db94535d 420 int level;
6407bf6d 421 int ret;
54aa1f4d
CM
422 int faili;
423 int err;
a28ec197 424
3768f368 425 if (!root->ref_cows)
a28ec197 426 return 0;
5f39d397 427
db94535d 428 level = btrfs_header_level(buf);
5f39d397
CM
429 nritems = btrfs_header_nritems(buf);
430 for (i = 0; i < nritems; i++) {
db94535d
CM
431 if (level == 0) {
432 u64 disk_bytenr;
5f39d397
CM
433 btrfs_item_key_to_cpu(buf, &key, i);
434 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d 435 continue;
5f39d397 436 fi = btrfs_item_ptr(buf, i,
6407bf6d 437 struct btrfs_file_extent_item);
5f39d397 438 if (btrfs_file_extent_type(buf, fi) ==
236454df
CM
439 BTRFS_FILE_EXTENT_INLINE)
440 continue;
db94535d
CM
441 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
442 if (disk_bytenr == 0)
3a686375 443 continue;
db94535d
CM
444 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
445 btrfs_file_extent_disk_num_bytes(buf, fi));
54aa1f4d
CM
446 if (ret) {
447 faili = i;
448 goto fail;
449 }
6407bf6d 450 } else {
db94535d
CM
451 bytenr = btrfs_node_blockptr(buf, i);
452 ret = btrfs_inc_extent_ref(trans, root, bytenr,
453 btrfs_level_size(root, level - 1));
54aa1f4d
CM
454 if (ret) {
455 faili = i;
456 goto fail;
457 }
6407bf6d 458 }
02217ed2
CM
459 }
460 return 0;
54aa1f4d 461fail:
ccd467d6 462 WARN_ON(1);
54aa1f4d 463 for (i =0; i < faili; i++) {
db94535d
CM
464 if (level == 0) {
465 u64 disk_bytenr;
5f39d397
CM
466 btrfs_item_key_to_cpu(buf, &key, i);
467 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
54aa1f4d 468 continue;
5f39d397 469 fi = btrfs_item_ptr(buf, i,
54aa1f4d 470 struct btrfs_file_extent_item);
5f39d397 471 if (btrfs_file_extent_type(buf, fi) ==
54aa1f4d
CM
472 BTRFS_FILE_EXTENT_INLINE)
473 continue;
db94535d
CM
474 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
475 if (disk_bytenr == 0)
54aa1f4d 476 continue;
db94535d
CM
477 err = btrfs_free_extent(trans, root, disk_bytenr,
478 btrfs_file_extent_disk_num_bytes(buf,
5f39d397 479 fi), 0);
54aa1f4d
CM
480 BUG_ON(err);
481 } else {
db94535d
CM
482 bytenr = btrfs_node_blockptr(buf, i);
483 err = btrfs_free_extent(trans, root, bytenr,
484 btrfs_level_size(root, level - 1), 0);
54aa1f4d
CM
485 BUG_ON(err);
486 }
487 }
488 return ret;
02217ed2
CM
489}
490
9078a3e1
CM
491static int write_one_cache_group(struct btrfs_trans_handle *trans,
492 struct btrfs_root *root,
493 struct btrfs_path *path,
494 struct btrfs_block_group_cache *cache)
495{
496 int ret;
497 int pending_ret;
498 struct btrfs_root *extent_root = root->fs_info->extent_root;
5f39d397
CM
499 unsigned long bi;
500 struct extent_buffer *leaf;
9078a3e1 501
9078a3e1 502 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
54aa1f4d
CM
503 if (ret < 0)
504 goto fail;
9078a3e1 505 BUG_ON(ret);
5f39d397
CM
506
507 leaf = path->nodes[0];
508 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
509 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
510 btrfs_mark_buffer_dirty(leaf);
9078a3e1 511 btrfs_release_path(extent_root, path);
54aa1f4d 512fail:
9078a3e1
CM
513 finish_current_insert(trans, extent_root);
514 pending_ret = del_pending_extents(trans, extent_root);
515 if (ret)
516 return ret;
517 if (pending_ret)
518 return pending_ret;
519 return 0;
520
521}
522
96b5179d
CM
523int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
524 struct btrfs_root *root)
9078a3e1 525{
96b5179d
CM
526 struct extent_map_tree *block_group_cache;
527 struct btrfs_block_group_cache *cache;
9078a3e1
CM
528 int ret;
529 int err = 0;
530 int werr = 0;
9078a3e1 531 struct btrfs_path *path;
96b5179d
CM
532 u64 last = 0;
533 u64 start;
534 u64 end;
535 u64 ptr;
9078a3e1 536
96b5179d 537 block_group_cache = &root->fs_info->block_group_cache;
9078a3e1
CM
538 path = btrfs_alloc_path();
539 if (!path)
540 return -ENOMEM;
541
542 while(1) {
96b5179d
CM
543 ret = find_first_extent_bit(block_group_cache, last,
544 &start, &end, BLOCK_GROUP_DIRTY);
545 if (ret)
9078a3e1 546 break;
54aa1f4d 547
96b5179d
CM
548 last = end + 1;
549 ret = get_state_private(block_group_cache, start, &ptr);
550 if (ret)
551 break;
552
ae2f5411 553 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
554 err = write_one_cache_group(trans, root,
555 path, cache);
556 /*
557 * if we fail to write the cache group, we want
558 * to keep it marked dirty in hopes that a later
559 * write will work
560 */
561 if (err) {
562 werr = err;
563 continue;
9078a3e1 564 }
96b5179d
CM
565 clear_extent_bits(block_group_cache, start, end,
566 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
567 }
568 btrfs_free_path(path);
569 return werr;
570}
571
572static int update_block_group(struct btrfs_trans_handle *trans,
573 struct btrfs_root *root,
db94535d
CM
574 u64 bytenr, u64 num_bytes, int alloc,
575 int mark_free, int data)
9078a3e1
CM
576{
577 struct btrfs_block_group_cache *cache;
578 struct btrfs_fs_info *info = root->fs_info;
db94535d 579 u64 total = num_bytes;
9078a3e1 580 u64 old_val;
db94535d 581 u64 byte_in_group;
96b5179d
CM
582 u64 start;
583 u64 end;
3e1ad54f 584
9078a3e1 585 while(total) {
db94535d 586 cache = btrfs_lookup_block_group(info, bytenr);
3e1ad54f 587 if (!cache) {
9078a3e1 588 return -1;
cd1bc465 589 }
db94535d
CM
590 byte_in_group = bytenr - cache->key.objectid;
591 WARN_ON(byte_in_group > cache->key.offset);
96b5179d
CM
592 start = cache->key.objectid;
593 end = start + cache->key.offset - 1;
594 set_extent_bits(&info->block_group_cache, start, end,
595 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
596
597 old_val = btrfs_block_group_used(&cache->item);
db94535d 598 num_bytes = min(total, cache->key.offset - byte_in_group);
cd1bc465 599 if (alloc) {
1e2677e0 600 if (cache->data != data &&
84f54cfa 601 old_val < (cache->key.offset >> 1)) {
96b5179d
CM
602 int bit_to_clear;
603 int bit_to_set;
96b5179d 604 cache->data = data;
1e2677e0 605 if (data) {
b97f9203
Y
606 bit_to_clear = BLOCK_GROUP_METADATA;
607 bit_to_set = BLOCK_GROUP_DATA;
f84a8b36
CM
608 cache->item.flags &=
609 ~BTRFS_BLOCK_GROUP_MIXED;
1e2677e0
CM
610 cache->item.flags |=
611 BTRFS_BLOCK_GROUP_DATA;
612 } else {
b97f9203
Y
613 bit_to_clear = BLOCK_GROUP_DATA;
614 bit_to_set = BLOCK_GROUP_METADATA;
f84a8b36
CM
615 cache->item.flags &=
616 ~BTRFS_BLOCK_GROUP_MIXED;
1e2677e0
CM
617 cache->item.flags &=
618 ~BTRFS_BLOCK_GROUP_DATA;
619 }
96b5179d
CM
620 clear_extent_bits(&info->block_group_cache,
621 start, end, bit_to_clear,
622 GFP_NOFS);
623 set_extent_bits(&info->block_group_cache,
624 start, end, bit_to_set,
625 GFP_NOFS);
f84a8b36
CM
626 } else if (cache->data != data &&
627 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
628 cache->data = BTRFS_BLOCK_GROUP_MIXED;
629 set_extent_bits(&info->block_group_cache,
630 start, end,
631 BLOCK_GROUP_DATA |
632 BLOCK_GROUP_METADATA,
633 GFP_NOFS);
1e2677e0 634 }
db94535d 635 old_val += num_bytes;
cd1bc465 636 } else {
db94535d 637 old_val -= num_bytes;
f510cfec
CM
638 if (mark_free) {
639 set_extent_dirty(&info->free_space_cache,
db94535d 640 bytenr, bytenr + num_bytes - 1,
f510cfec 641 GFP_NOFS);
e37c9e69 642 }
cd1bc465 643 }
9078a3e1 644 btrfs_set_block_group_used(&cache->item, old_val);
db94535d
CM
645 total -= num_bytes;
646 bytenr += num_bytes;
9078a3e1
CM
647 }
648 return 0;
649}
650
1a5bc167 651int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
ccd467d6 652{
ccd467d6 653 u64 last = 0;
1a5bc167
CM
654 u64 start;
655 u64 end;
656 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
ccd467d6 657 int ret;
ccd467d6
CM
658
659 while(1) {
1a5bc167
CM
660 ret = find_first_extent_bit(pinned_extents, last,
661 &start, &end, EXTENT_DIRTY);
662 if (ret)
ccd467d6 663 break;
1a5bc167
CM
664 set_extent_dirty(copy, start, end, GFP_NOFS);
665 last = end + 1;
ccd467d6
CM
666 }
667 return 0;
668}
669
670int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
671 struct btrfs_root *root,
1a5bc167 672 struct extent_map_tree *unpin)
a28ec197 673{
1a5bc167
CM
674 u64 start;
675 u64 end;
a28ec197 676 int ret;
1a5bc167 677 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
f510cfec
CM
678 struct extent_map_tree *free_space_cache;
679
680 free_space_cache = &root->fs_info->free_space_cache;
a28ec197
CM
681
682 while(1) {
1a5bc167
CM
683 ret = find_first_extent_bit(unpin, 0, &start, &end,
684 EXTENT_DIRTY);
685 if (ret)
a28ec197 686 break;
1a5bc167
CM
687
688 clear_extent_dirty(pinned_extents, start, end,
689 GFP_NOFS);
690 clear_extent_dirty(unpin, start, end, GFP_NOFS);
691 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
a28ec197
CM
692 }
693 return 0;
694}
695
e089f05c
CM
696static int finish_current_insert(struct btrfs_trans_handle *trans, struct
697 btrfs_root *extent_root)
037e6390 698{
e2fa7227 699 struct btrfs_key ins;
234b63a0 700 struct btrfs_extent_item extent_item;
037e6390 701 int ret;
1a5bc167
CM
702 int err = 0;
703 u64 start;
704 u64 end;
1261ec42 705 struct btrfs_fs_info *info = extent_root->fs_info;
037e6390 706
5f39d397 707 btrfs_set_stack_extent_refs(&extent_item, 1);
62e2749e 708 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
5f39d397
CM
709 btrfs_set_stack_extent_owner(&extent_item,
710 extent_root->root_key.objectid);
037e6390 711
26b8003f 712 while(1) {
1a5bc167
CM
713 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
714 &end, EXTENT_LOCKED);
715 if (ret)
26b8003f
CM
716 break;
717
1a5bc167
CM
718 ins.objectid = start;
719 ins.offset = end + 1 - start;
720 err = btrfs_insert_item(trans, extent_root, &ins,
721 &extent_item, sizeof(extent_item));
722 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
723 GFP_NOFS);
037e6390 724 }
037e6390
CM
725 return 0;
726}
727
db94535d
CM
728static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
729 int pending)
e20d96d6 730{
1a5bc167 731 int err = 0;
5f39d397 732 struct extent_buffer *buf;
8ef97622 733
f4b9aa8d 734 if (!pending) {
db94535d 735 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
5f39d397
CM
736 if (buf) {
737 if (btrfs_buffer_uptodate(buf)) {
2c90e5d6
CM
738 u64 transid =
739 root->fs_info->running_transaction->transid;
5f39d397
CM
740 if (btrfs_header_generation(buf) == transid) {
741 free_extent_buffer(buf);
c549228f 742 return 1;
2c90e5d6 743 }
f4b9aa8d 744 }
5f39d397 745 free_extent_buffer(buf);
8ef97622 746 }
1a5bc167 747 set_extent_dirty(&root->fs_info->pinned_extents,
db94535d 748 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
f4b9aa8d 749 } else {
1a5bc167 750 set_extent_bits(&root->fs_info->pending_del,
db94535d
CM
751 bytenr, bytenr + num_bytes - 1,
752 EXTENT_LOCKED, GFP_NOFS);
f4b9aa8d 753 }
be744175 754 BUG_ON(err < 0);
e20d96d6
CM
755 return 0;
756}
757
fec577fb 758/*
a28ec197 759 * remove an extent from the root, returns 0 on success
fec577fb 760 */
e089f05c 761static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
db94535d 762 *root, u64 bytenr, u64 num_bytes, int pin,
e37c9e69 763 int mark_free)
a28ec197 764{
5caf2a00 765 struct btrfs_path *path;
e2fa7227 766 struct btrfs_key key;
1261ec42
CM
767 struct btrfs_fs_info *info = root->fs_info;
768 struct btrfs_root *extent_root = info->extent_root;
5f39d397 769 struct extent_buffer *leaf;
a28ec197 770 int ret;
234b63a0 771 struct btrfs_extent_item *ei;
cf27e1ee 772 u32 refs;
037e6390 773
db94535d 774 key.objectid = bytenr;
62e2749e 775 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 776 key.offset = num_bytes;
a28ec197 777
5caf2a00 778 path = btrfs_alloc_path();
54aa1f4d
CM
779 if (!path)
780 return -ENOMEM;
5f26f772 781
54aa1f4d
CM
782 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
783 if (ret < 0)
784 return ret;
785 BUG_ON(ret);
5f39d397
CM
786
787 leaf = path->nodes[0];
788 ei = btrfs_item_ptr(leaf, path->slots[0],
123abc88 789 struct btrfs_extent_item);
5f39d397
CM
790 refs = btrfs_extent_refs(leaf, ei);
791 BUG_ON(refs == 0);
792 refs -= 1;
793 btrfs_set_extent_refs(leaf, ei, refs);
794 btrfs_mark_buffer_dirty(leaf);
795
cf27e1ee 796 if (refs == 0) {
db94535d
CM
797 u64 super_used;
798 u64 root_used;
78fae27e
CM
799
800 if (pin) {
db94535d 801 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
c549228f
Y
802 if (ret > 0)
803 mark_free = 1;
804 BUG_ON(ret < 0);
78fae27e
CM
805 }
806
58176a96 807 /* block accounting for super block */
db94535d
CM
808 super_used = btrfs_super_bytes_used(&info->super_copy);
809 btrfs_set_super_bytes_used(&info->super_copy,
810 super_used - num_bytes);
58176a96
JB
811
812 /* block accounting for root item */
db94535d 813 root_used = btrfs_root_used(&root->root_item);
5f39d397 814 btrfs_set_root_used(&root->root_item,
db94535d 815 root_used - num_bytes);
58176a96 816
5caf2a00 817 ret = btrfs_del_item(trans, extent_root, path);
54aa1f4d
CM
818 if (ret) {
819 return ret;
820 }
db94535d 821 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1e2677e0 822 mark_free, 0);
9078a3e1 823 BUG_ON(ret);
a28ec197 824 }
5caf2a00 825 btrfs_free_path(path);
e089f05c 826 finish_current_insert(trans, extent_root);
a28ec197
CM
827 return ret;
828}
829
a28ec197
CM
830/*
831 * find all the blocks marked as pending in the radix tree and remove
832 * them from the extent map
833 */
e089f05c
CM
834static int del_pending_extents(struct btrfs_trans_handle *trans, struct
835 btrfs_root *extent_root)
a28ec197
CM
836{
837 int ret;
e20d96d6 838 int err = 0;
1a5bc167
CM
839 u64 start;
840 u64 end;
841 struct extent_map_tree *pending_del;
842 struct extent_map_tree *pinned_extents;
8ef97622 843
1a5bc167
CM
844 pending_del = &extent_root->fs_info->pending_del;
845 pinned_extents = &extent_root->fs_info->pinned_extents;
a28ec197
CM
846
847 while(1) {
1a5bc167
CM
848 ret = find_first_extent_bit(pending_del, 0, &start, &end,
849 EXTENT_LOCKED);
850 if (ret)
a28ec197 851 break;
1a5bc167
CM
852
853 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
854 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
855 GFP_NOFS);
856 ret = __free_extent(trans, extent_root,
857 start, end + 1 - start, 0, 0);
858 if (ret)
859 err = ret;
fec577fb 860 }
e20d96d6 861 return err;
fec577fb
CM
862}
863
864/*
865 * remove an extent from the root, returns 0 on success
866 */
e089f05c 867int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
db94535d 868 *root, u64 bytenr, u64 num_bytes, int pin)
fec577fb 869{
9f5fae2f 870 struct btrfs_root *extent_root = root->fs_info->extent_root;
fec577fb
CM
871 int pending_ret;
872 int ret;
a28ec197 873
db94535d 874 WARN_ON(num_bytes < root->sectorsize);
fec577fb 875 if (root == extent_root) {
db94535d 876 pin_down_bytes(root, bytenr, num_bytes, 1);
fec577fb
CM
877 return 0;
878 }
db94535d 879 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
e20d96d6 880 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
fec577fb
CM
881 return ret ? ret : pending_ret;
882}
883
884/*
885 * walks the btree of allocated extents and find a hole of a given size.
886 * The key ins is changed to record the hole:
887 * ins->objectid == block start
62e2749e 888 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
889 * ins->offset == number of blocks
890 * Any available blocks before search_start are skipped.
891 */
e089f05c 892static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
db94535d
CM
893 *orig_root, u64 num_bytes, u64 empty_size,
894 u64 search_start, u64 search_end, u64 hint_byte,
f2654de4
CM
895 struct btrfs_key *ins, u64 exclude_start,
896 u64 exclude_nr, int data)
fec577fb 897{
5caf2a00 898 struct btrfs_path *path;
e2fa7227 899 struct btrfs_key key;
fec577fb
CM
900 int ret;
901 u64 hole_size = 0;
902 int slot = 0;
db94535d 903 u64 last_byte = 0;
be744175 904 u64 orig_search_start = search_start;
fec577fb 905 int start_found;
5f39d397 906 struct extent_buffer *l;
9f5fae2f 907 struct btrfs_root * root = orig_root->fs_info->extent_root;
f2458e1d 908 struct btrfs_fs_info *info = root->fs_info;
db94535d 909 u64 total_needed = num_bytes;
e20d96d6 910 int level;
be08c1b9 911 struct btrfs_block_group_cache *block_group;
be744175 912 int full_scan = 0;
fbdc762b 913 int wrapped = 0;
f84a8b36 914 u64 cached_start;
fec577fb 915
db94535d 916 WARN_ON(num_bytes < root->sectorsize);
b1a4d965
CM
917 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
918
5f39d397
CM
919 level = btrfs_header_level(root->node);
920
3e1ad54f 921 if (search_end == (u64)-1)
db94535d
CM
922 search_end = btrfs_super_total_bytes(&info->super_copy);
923 if (hint_byte) {
924 block_group = btrfs_lookup_block_group(info, hint_byte);
be744175 925 block_group = btrfs_find_block_group(root, block_group,
db94535d 926 hint_byte, data, 1);
be744175
CM
927 } else {
928 block_group = btrfs_find_block_group(root,
929 trans->block_group, 0,
de428b63 930 data, 1);
be744175
CM
931 }
932
6702ed49 933 total_needed += empty_size;
e011599b
CM
934 path = btrfs_alloc_path();
935
be744175 936check_failed:
f510cfec
CM
937 search_start = find_search_start(root, &block_group,
938 search_start, total_needed, data);
f84a8b36 939 cached_start = search_start;
5caf2a00 940 btrfs_init_path(path);
fec577fb
CM
941 ins->objectid = search_start;
942 ins->offset = 0;
fec577fb 943 start_found = 0;
2cc58cf2 944 path->reada = 2;
e37c9e69 945
5caf2a00 946 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
0f70abe2
CM
947 if (ret < 0)
948 goto error;
aa5d6bed 949
e37c9e69 950 if (path->slots[0] > 0) {
5caf2a00 951 path->slots[0]--;
e37c9e69
CM
952 }
953
5f39d397
CM
954 l = path->nodes[0];
955 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
956
e37c9e69
CM
957 /*
958 * a rare case, go back one key if we hit a block group item
959 * instead of an extent item
960 */
961 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
962 key.objectid + key.offset >= search_start) {
963 ins->objectid = key.objectid;
964 ins->offset = key.offset - 1;
965 btrfs_release_path(root, path);
966 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
967 if (ret < 0)
968 goto error;
969
970 if (path->slots[0] > 0) {
971 path->slots[0]--;
972 }
973 }
0579da42 974
fec577fb 975 while (1) {
5f39d397 976 l = path->nodes[0];
5caf2a00 977 slot = path->slots[0];
5f39d397 978 if (slot >= btrfs_header_nritems(l)) {
5caf2a00 979 ret = btrfs_next_leaf(root, path);
fec577fb
CM
980 if (ret == 0)
981 continue;
0f70abe2
CM
982 if (ret < 0)
983 goto error;
e19caa5f
CM
984
985 search_start = max(search_start,
986 block_group->key.objectid);
fec577fb
CM
987 if (!start_found) {
988 ins->objectid = search_start;
3e1ad54f 989 ins->offset = search_end - search_start;
fec577fb
CM
990 start_found = 1;
991 goto check_pending;
992 }
db94535d
CM
993 ins->objectid = last_byte > search_start ?
994 last_byte : search_start;
3e1ad54f 995 ins->offset = search_end - ins->objectid;
e19caa5f 996 BUG_ON(ins->objectid >= search_end);
fec577fb
CM
997 goto check_pending;
998 }
5f39d397 999 btrfs_item_key_to_cpu(l, &key, slot);
96b5179d 1000
db94535d 1001 if (key.objectid >= search_start && key.objectid > last_byte &&
e37c9e69 1002 start_found) {
db94535d
CM
1003 if (last_byte < search_start)
1004 last_byte = search_start;
1005 hole_size = key.objectid - last_byte;
1006 if (hole_size >= num_bytes) {
1007 ins->objectid = last_byte;
e37c9e69
CM
1008 ins->offset = hole_size;
1009 goto check_pending;
0579da42 1010 }
fec577fb 1011 }
96b5179d
CM
1012 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1013 if (!start_found) {
db94535d 1014 last_byte = key.objectid;
96b5179d
CM
1015 start_found = 1;
1016 }
e37c9e69 1017 goto next;
96b5179d
CM
1018 }
1019
e37c9e69 1020
0579da42 1021 start_found = 1;
db94535d 1022 last_byte = key.objectid + key.offset;
f510cfec 1023
db94535d 1024 if (!full_scan && last_byte >= block_group->key.objectid +
be744175
CM
1025 block_group->key.offset) {
1026 btrfs_release_path(root, path);
1027 search_start = block_group->key.objectid +
e19caa5f 1028 block_group->key.offset;
be744175
CM
1029 goto new_group;
1030 }
9078a3e1 1031next:
5caf2a00 1032 path->slots[0]++;
de428b63 1033 cond_resched();
fec577fb 1034 }
fec577fb
CM
1035check_pending:
1036 /* we have to make sure we didn't find an extent that has already
1037 * been allocated by the map tree or the original allocation
1038 */
5caf2a00 1039 btrfs_release_path(root, path);
fec577fb 1040 BUG_ON(ins->objectid < search_start);
e37c9e69 1041
db94535d 1042 if (ins->objectid + num_bytes >= search_end)
cf67582b
CM
1043 goto enospc;
1044
e19caa5f
CM
1045 if (!full_scan && ins->objectid + num_bytes >= block_group->
1046 key.objectid + block_group->key.offset) {
1047 search_start = block_group->key.objectid +
1048 block_group->key.offset;
1049 goto new_group;
1050 }
1a5bc167 1051 if (test_range_bit(&info->extent_ins, ins->objectid,
db94535d
CM
1052 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1053 search_start = ins->objectid + num_bytes;
1a5bc167
CM
1054 goto new_group;
1055 }
1056 if (test_range_bit(&info->pinned_extents, ins->objectid,
db94535d
CM
1057 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1058 search_start = ins->objectid + num_bytes;
1a5bc167 1059 goto new_group;
fec577fb 1060 }
db94535d 1061 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
f2654de4
CM
1062 ins->objectid < exclude_start + exclude_nr)) {
1063 search_start = exclude_start + exclude_nr;
1064 goto new_group;
1065 }
e37c9e69 1066 if (!data) {
5276aeda 1067 block_group = btrfs_lookup_block_group(info, ins->objectid);
26b8003f
CM
1068 if (block_group)
1069 trans->block_group = block_group;
f2458e1d 1070 }
db94535d 1071 ins->offset = num_bytes;
5caf2a00 1072 btrfs_free_path(path);
fec577fb 1073 return 0;
be744175
CM
1074
1075new_group:
db94535d 1076 if (search_start + num_bytes >= search_end) {
cf67582b 1077enospc:
be744175 1078 search_start = orig_search_start;
fbdc762b
CM
1079 if (full_scan) {
1080 ret = -ENOSPC;
1081 goto error;
1082 }
6702ed49
CM
1083 if (wrapped) {
1084 if (!full_scan)
1085 total_needed -= empty_size;
fbdc762b 1086 full_scan = 1;
6702ed49 1087 } else
fbdc762b 1088 wrapped = 1;
be744175 1089 }
5276aeda 1090 block_group = btrfs_lookup_block_group(info, search_start);
fbdc762b 1091 cond_resched();
be744175
CM
1092 if (!full_scan)
1093 block_group = btrfs_find_block_group(root, block_group,
de428b63 1094 search_start, data, 0);
be744175
CM
1095 goto check_failed;
1096
0f70abe2 1097error:
5caf2a00
CM
1098 btrfs_release_path(root, path);
1099 btrfs_free_path(path);
0f70abe2 1100 return ret;
fec577fb 1101}
fec577fb
CM
1102/*
1103 * finds a free extent and does all the dirty work required for allocation
1104 * returns the key for the extent through ins, and a tree buffer for
1105 * the first block of the extent through buf.
1106 *
1107 * returns 0 if everything worked, non-zero otherwise.
1108 */
4d775673
CM
1109int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1110 struct btrfs_root *root, u64 owner,
db94535d 1111 u64 num_bytes, u64 empty_size, u64 hint_byte,
be08c1b9 1112 u64 search_end, struct btrfs_key *ins, int data)
fec577fb
CM
1113{
1114 int ret;
1115 int pending_ret;
db94535d 1116 u64 super_used, root_used;
fbdc762b 1117 u64 search_start = 0;
1261ec42
CM
1118 struct btrfs_fs_info *info = root->fs_info;
1119 struct btrfs_root *extent_root = info->extent_root;
234b63a0 1120 struct btrfs_extent_item extent_item;
037e6390 1121
5f39d397
CM
1122 btrfs_set_stack_extent_refs(&extent_item, 1);
1123 btrfs_set_stack_extent_owner(&extent_item, owner);
fec577fb 1124
db94535d
CM
1125 WARN_ON(num_bytes < root->sectorsize);
1126 ret = find_free_extent(trans, root, num_bytes, empty_size,
1127 search_start, search_end, hint_byte, ins,
26b8003f
CM
1128 trans->alloc_exclude_start,
1129 trans->alloc_exclude_nr, data);
ccd467d6 1130 BUG_ON(ret);
f2654de4
CM
1131 if (ret)
1132 return ret;
fec577fb 1133
58176a96 1134 /* block accounting for super block */
db94535d
CM
1135 super_used = btrfs_super_bytes_used(&info->super_copy);
1136 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
26b8003f 1137
58176a96 1138 /* block accounting for root item */
db94535d
CM
1139 root_used = btrfs_root_used(&root->root_item);
1140 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
58176a96 1141
f510cfec
CM
1142 clear_extent_dirty(&root->fs_info->free_space_cache,
1143 ins->objectid, ins->objectid + ins->offset - 1,
1144 GFP_NOFS);
1145
26b8003f 1146 if (root == extent_root) {
1a5bc167
CM
1147 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1148 ins->objectid + ins->offset - 1,
1149 EXTENT_LOCKED, GFP_NOFS);
e19caa5f 1150 WARN_ON(data == 1);
26b8003f
CM
1151 goto update_block;
1152 }
1153
1154 WARN_ON(trans->alloc_exclude_nr);
1155 trans->alloc_exclude_start = ins->objectid;
1156 trans->alloc_exclude_nr = ins->offset;
e089f05c
CM
1157 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1158 sizeof(extent_item));
037e6390 1159
26b8003f
CM
1160 trans->alloc_exclude_start = 0;
1161 trans->alloc_exclude_nr = 0;
1162
ccd467d6 1163 BUG_ON(ret);
e089f05c 1164 finish_current_insert(trans, extent_root);
e20d96d6 1165 pending_ret = del_pending_extents(trans, extent_root);
f510cfec 1166
e37c9e69 1167 if (ret) {
037e6390 1168 return ret;
e37c9e69
CM
1169 }
1170 if (pending_ret) {
037e6390 1171 return pending_ret;
e37c9e69 1172 }
26b8003f
CM
1173
1174update_block:
1e2677e0
CM
1175 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1176 data);
fabb5681 1177 BUG_ON(ret);
037e6390 1178 return 0;
fec577fb
CM
1179}
1180
1181/*
1182 * helper function to allocate a block for a given tree
1183 * returns the tree buffer or NULL.
1184 */
5f39d397 1185struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
db94535d
CM
1186 struct btrfs_root *root,
1187 u32 blocksize, u64 hint,
5f39d397 1188 u64 empty_size)
fec577fb 1189{
e2fa7227 1190 struct btrfs_key ins;
fec577fb 1191 int ret;
5f39d397 1192 struct extent_buffer *buf;
fec577fb 1193
4d775673 1194 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
db94535d
CM
1195 blocksize, empty_size, hint,
1196 (u64)-1, &ins, 0);
fec577fb 1197 if (ret) {
54aa1f4d
CM
1198 BUG_ON(ret > 0);
1199 return ERR_PTR(ret);
fec577fb 1200 }
db94535d 1201 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
54aa1f4d 1202 if (!buf) {
db94535d 1203 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
54aa1f4d
CM
1204 return ERR_PTR(-ENOMEM);
1205 }
5f39d397
CM
1206 btrfs_set_buffer_uptodate(buf);
1207 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1208 buf->start + buf->len - 1, GFP_NOFS);
19c00ddc
CM
1209 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1210 buf->start, buf->start + buf->len - 1,
1211 EXTENT_CSUM, GFP_NOFS);
1212 buf->flags |= EXTENT_CSUM;
6b80053d 1213 btrfs_set_buffer_defrag(buf);
d3c2fdcf 1214 trans->blocks_used++;
fec577fb
CM
1215 return buf;
1216}
a28ec197 1217
6407bf6d 1218static int drop_leaf_ref(struct btrfs_trans_handle *trans,
5f39d397 1219 struct btrfs_root *root, struct extent_buffer *leaf)
6407bf6d 1220{
5f39d397 1221 struct btrfs_key key;
6407bf6d
CM
1222 struct btrfs_file_extent_item *fi;
1223 int i;
1224 int nritems;
1225 int ret;
1226
5f39d397
CM
1227 BUG_ON(!btrfs_is_leaf(leaf));
1228 nritems = btrfs_header_nritems(leaf);
6407bf6d 1229 for (i = 0; i < nritems; i++) {
db94535d 1230 u64 disk_bytenr;
5f39d397
CM
1231
1232 btrfs_item_key_to_cpu(leaf, &key, i);
1233 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d
CM
1234 continue;
1235 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5f39d397
CM
1236 if (btrfs_file_extent_type(leaf, fi) ==
1237 BTRFS_FILE_EXTENT_INLINE)
236454df 1238 continue;
6407bf6d
CM
1239 /*
1240 * FIXME make sure to insert a trans record that
1241 * repeats the snapshot del on crash
1242 */
db94535d
CM
1243 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1244 if (disk_bytenr == 0)
3a686375 1245 continue;
db94535d
CM
1246 ret = btrfs_free_extent(trans, root, disk_bytenr,
1247 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
6407bf6d
CM
1248 BUG_ON(ret);
1249 }
1250 return 0;
1251}
1252
e011599b 1253static void reada_walk_down(struct btrfs_root *root,
5f39d397 1254 struct extent_buffer *node)
e011599b
CM
1255{
1256 int i;
1257 u32 nritems;
db94535d 1258 u64 bytenr;
e011599b
CM
1259 int ret;
1260 u32 refs;
db94535d
CM
1261 int level;
1262 u32 blocksize;
e011599b 1263
5f39d397 1264 nritems = btrfs_header_nritems(node);
db94535d 1265 level = btrfs_header_level(node);
e011599b 1266 for (i = 0; i < nritems; i++) {
db94535d
CM
1267 bytenr = btrfs_node_blockptr(node, i);
1268 blocksize = btrfs_level_size(root, level - 1);
1269 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
e011599b
CM
1270 BUG_ON(ret);
1271 if (refs != 1)
1272 continue;
409eb95d 1273 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1274 ret = readahead_tree_block(root, bytenr, blocksize);
409eb95d
CM
1275 cond_resched();
1276 mutex_lock(&root->fs_info->fs_mutex);
e011599b
CM
1277 if (ret)
1278 break;
1279 }
1280}
1281
9aca1d51
CM
1282/*
1283 * helper function for drop_snapshot, this walks down the tree dropping ref
1284 * counts as it goes.
1285 */
e089f05c
CM
1286static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1287 *root, struct btrfs_path *path, int *level)
20524f02 1288{
5f39d397
CM
1289 struct extent_buffer *next;
1290 struct extent_buffer *cur;
db94535d
CM
1291 u64 bytenr;
1292 u32 blocksize;
20524f02
CM
1293 int ret;
1294 u32 refs;
1295
5caf2a00
CM
1296 WARN_ON(*level < 0);
1297 WARN_ON(*level >= BTRFS_MAX_LEVEL);
5f39d397 1298 ret = lookup_extent_ref(trans, root,
db94535d
CM
1299 path->nodes[*level]->start,
1300 path->nodes[*level]->len, &refs);
20524f02
CM
1301 BUG_ON(ret);
1302 if (refs > 1)
1303 goto out;
e011599b 1304
9aca1d51
CM
1305 /*
1306 * walk down to the last node level and free all the leaves
1307 */
6407bf6d 1308 while(*level >= 0) {
5caf2a00
CM
1309 WARN_ON(*level < 0);
1310 WARN_ON(*level >= BTRFS_MAX_LEVEL);
20524f02 1311 cur = path->nodes[*level];
e011599b
CM
1312
1313 if (*level > 0 && path->slots[*level] == 0)
5f39d397 1314 reada_walk_down(root, cur);
e011599b 1315
5f39d397 1316 if (btrfs_header_level(cur) != *level)
2c90e5d6 1317 WARN_ON(1);
e011599b 1318
7518a238 1319 if (path->slots[*level] >=
5f39d397 1320 btrfs_header_nritems(cur))
20524f02 1321 break;
6407bf6d
CM
1322 if (*level == 0) {
1323 ret = drop_leaf_ref(trans, root, cur);
1324 BUG_ON(ret);
1325 break;
1326 }
db94535d
CM
1327 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1328 blocksize = btrfs_level_size(root, *level - 1);
1329 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
6407bf6d
CM
1330 BUG_ON(ret);
1331 if (refs != 1) {
20524f02 1332 path->slots[*level]++;
db94535d
CM
1333 ret = btrfs_free_extent(trans, root, bytenr,
1334 blocksize, 1);
20524f02
CM
1335 BUG_ON(ret);
1336 continue;
1337 }
db94535d 1338 next = btrfs_find_tree_block(root, bytenr, blocksize);
5f39d397
CM
1339 if (!next || !btrfs_buffer_uptodate(next)) {
1340 free_extent_buffer(next);
e9d0b13b 1341 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1342 next = read_tree_block(root, bytenr, blocksize);
e9d0b13b
CM
1343 mutex_lock(&root->fs_info->fs_mutex);
1344
1345 /* we dropped the lock, check one more time */
db94535d
CM
1346 ret = lookup_extent_ref(trans, root, bytenr,
1347 blocksize, &refs);
e9d0b13b
CM
1348 BUG_ON(ret);
1349 if (refs != 1) {
1350 path->slots[*level]++;
5f39d397 1351 free_extent_buffer(next);
e9d0b13b 1352 ret = btrfs_free_extent(trans, root,
db94535d 1353 bytenr, blocksize, 1);
e9d0b13b
CM
1354 BUG_ON(ret);
1355 continue;
1356 }
1357 }
5caf2a00 1358 WARN_ON(*level <= 0);
83e15a28 1359 if (path->nodes[*level-1])
5f39d397 1360 free_extent_buffer(path->nodes[*level-1]);
20524f02 1361 path->nodes[*level-1] = next;
5f39d397 1362 *level = btrfs_header_level(next);
20524f02
CM
1363 path->slots[*level] = 0;
1364 }
1365out:
5caf2a00
CM
1366 WARN_ON(*level < 0);
1367 WARN_ON(*level >= BTRFS_MAX_LEVEL);
db94535d
CM
1368 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1369 path->nodes[*level]->len, 1);
5f39d397 1370 free_extent_buffer(path->nodes[*level]);
20524f02
CM
1371 path->nodes[*level] = NULL;
1372 *level += 1;
1373 BUG_ON(ret);
1374 return 0;
1375}
1376
9aca1d51
CM
1377/*
1378 * helper for dropping snapshots. This walks back up the tree in the path
1379 * to find the first node higher up where we haven't yet gone through
1380 * all the slots
1381 */
e089f05c
CM
1382static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1383 *root, struct btrfs_path *path, int *level)
20524f02
CM
1384{
1385 int i;
1386 int slot;
1387 int ret;
9f3a7427
CM
1388 struct btrfs_root_item *root_item = &root->root_item;
1389
234b63a0 1390 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
20524f02 1391 slot = path->slots[i];
5f39d397
CM
1392 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1393 struct extent_buffer *node;
1394 struct btrfs_disk_key disk_key;
1395 node = path->nodes[i];
20524f02
CM
1396 path->slots[i]++;
1397 *level = i;
9f3a7427 1398 WARN_ON(*level == 0);
5f39d397 1399 btrfs_node_key(node, &disk_key, path->slots[i]);
9f3a7427 1400 memcpy(&root_item->drop_progress,
5f39d397 1401 &disk_key, sizeof(disk_key));
9f3a7427 1402 root_item->drop_level = i;
20524f02
CM
1403 return 0;
1404 } else {
e089f05c 1405 ret = btrfs_free_extent(trans, root,
db94535d
CM
1406 path->nodes[*level]->start,
1407 path->nodes[*level]->len, 1);
6407bf6d 1408 BUG_ON(ret);
5f39d397 1409 free_extent_buffer(path->nodes[*level]);
83e15a28 1410 path->nodes[*level] = NULL;
20524f02 1411 *level = i + 1;
20524f02
CM
1412 }
1413 }
1414 return 1;
1415}
1416
9aca1d51
CM
1417/*
1418 * drop the reference count on the tree rooted at 'snap'. This traverses
1419 * the tree freeing any blocks that have a ref count of zero after being
1420 * decremented.
1421 */
e089f05c 1422int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
9f3a7427 1423 *root)
20524f02 1424{
3768f368 1425 int ret = 0;
9aca1d51 1426 int wret;
20524f02 1427 int level;
5caf2a00 1428 struct btrfs_path *path;
20524f02
CM
1429 int i;
1430 int orig_level;
9f3a7427 1431 struct btrfs_root_item *root_item = &root->root_item;
20524f02 1432
5caf2a00
CM
1433 path = btrfs_alloc_path();
1434 BUG_ON(!path);
20524f02 1435
5f39d397 1436 level = btrfs_header_level(root->node);
20524f02 1437 orig_level = level;
9f3a7427
CM
1438 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1439 path->nodes[level] = root->node;
f510cfec 1440 extent_buffer_get(root->node);
9f3a7427
CM
1441 path->slots[level] = 0;
1442 } else {
1443 struct btrfs_key key;
5f39d397
CM
1444 struct btrfs_disk_key found_key;
1445 struct extent_buffer *node;
6702ed49 1446
9f3a7427 1447 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6702ed49
CM
1448 level = root_item->drop_level;
1449 path->lowest_level = level;
9f3a7427 1450 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6702ed49 1451 if (wret < 0) {
9f3a7427
CM
1452 ret = wret;
1453 goto out;
1454 }
5f39d397
CM
1455 node = path->nodes[level];
1456 btrfs_node_key(node, &found_key, path->slots[level]);
1457 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1458 sizeof(found_key)));
9f3a7427 1459 }
20524f02 1460 while(1) {
5caf2a00 1461 wret = walk_down_tree(trans, root, path, &level);
9aca1d51 1462 if (wret > 0)
20524f02 1463 break;
9aca1d51
CM
1464 if (wret < 0)
1465 ret = wret;
1466
5caf2a00 1467 wret = walk_up_tree(trans, root, path, &level);
9aca1d51 1468 if (wret > 0)
20524f02 1469 break;
9aca1d51
CM
1470 if (wret < 0)
1471 ret = wret;
409eb95d 1472 ret = -EAGAIN;
409eb95d 1473 break;
20524f02 1474 }
83e15a28 1475 for (i = 0; i <= orig_level; i++) {
5caf2a00 1476 if (path->nodes[i]) {
5f39d397 1477 free_extent_buffer(path->nodes[i]);
0f82731f 1478 path->nodes[i] = NULL;
83e15a28 1479 }
20524f02 1480 }
9f3a7427 1481out:
5caf2a00 1482 btrfs_free_path(path);
9aca1d51 1483 return ret;
20524f02 1484}
9078a3e1 1485
96b5179d 1486int btrfs_free_block_groups(struct btrfs_fs_info *info)
9078a3e1 1487{
96b5179d
CM
1488 u64 start;
1489 u64 end;
b97f9203 1490 u64 ptr;
9078a3e1 1491 int ret;
9078a3e1 1492 while(1) {
96b5179d
CM
1493 ret = find_first_extent_bit(&info->block_group_cache, 0,
1494 &start, &end, (unsigned int)-1);
1495 if (ret)
9078a3e1 1496 break;
b97f9203
Y
1497 ret = get_state_private(&info->block_group_cache, start, &ptr);
1498 if (!ret)
1499 kfree((void *)(unsigned long)ptr);
96b5179d
CM
1500 clear_extent_bits(&info->block_group_cache, start,
1501 end, (unsigned int)-1, GFP_NOFS);
9078a3e1 1502 }
e37c9e69 1503 while(1) {
f510cfec
CM
1504 ret = find_first_extent_bit(&info->free_space_cache, 0,
1505 &start, &end, EXTENT_DIRTY);
1506 if (ret)
e37c9e69 1507 break;
f510cfec
CM
1508 clear_extent_dirty(&info->free_space_cache, start,
1509 end, GFP_NOFS);
e37c9e69 1510 }
be744175
CM
1511 return 0;
1512}
1513
9078a3e1
CM
1514int btrfs_read_block_groups(struct btrfs_root *root)
1515{
1516 struct btrfs_path *path;
1517 int ret;
1518 int err = 0;
96b5179d 1519 int bit;
9078a3e1 1520 struct btrfs_block_group_cache *cache;
be744175 1521 struct btrfs_fs_info *info = root->fs_info;
96b5179d 1522 struct extent_map_tree *block_group_cache;
9078a3e1
CM
1523 struct btrfs_key key;
1524 struct btrfs_key found_key;
5f39d397 1525 struct extent_buffer *leaf;
96b5179d
CM
1526
1527 block_group_cache = &info->block_group_cache;
9078a3e1 1528
be744175 1529 root = info->extent_root;
9078a3e1 1530 key.objectid = 0;
db94535d 1531 key.offset = BTRFS_BLOCK_GROUP_SIZE;
9078a3e1
CM
1532 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1533
1534 path = btrfs_alloc_path();
1535 if (!path)
1536 return -ENOMEM;
1537
1538 while(1) {
be744175 1539 ret = btrfs_search_slot(NULL, info->extent_root,
9078a3e1
CM
1540 &key, path, 0, 0);
1541 if (ret != 0) {
1542 err = ret;
1543 break;
1544 }
5f39d397
CM
1545 leaf = path->nodes[0];
1546 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
9078a3e1
CM
1547 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1548 if (!cache) {
1549 err = -1;
1550 break;
1551 }
3e1ad54f 1552
5f39d397
CM
1553 read_extent_buffer(leaf, &cache->item,
1554 btrfs_item_ptr_offset(leaf, path->slots[0]),
1555 sizeof(cache->item));
9078a3e1 1556 memcpy(&cache->key, &found_key, sizeof(found_key));
e37c9e69
CM
1557 cache->cached = 0;
1558
9078a3e1
CM
1559 key.objectid = found_key.objectid + found_key.offset;
1560 btrfs_release_path(root, path);
5f39d397 1561
f84a8b36
CM
1562 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1563 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1564 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1565 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
96b5179d 1566 bit = BLOCK_GROUP_DATA;
f84a8b36 1567 cache->data = BTRFS_BLOCK_GROUP_DATA;
96b5179d
CM
1568 } else {
1569 bit = BLOCK_GROUP_METADATA;
1570 cache->data = 0;
31f3c99b 1571 }
96b5179d
CM
1572
1573 /* use EXTENT_LOCKED to prevent merging */
1574 set_extent_bits(block_group_cache, found_key.objectid,
1575 found_key.objectid + found_key.offset - 1,
1576 bit | EXTENT_LOCKED, GFP_NOFS);
1577 set_state_private(block_group_cache, found_key.objectid,
ae2f5411 1578 (unsigned long)cache);
96b5179d 1579
9078a3e1 1580 if (key.objectid >=
db94535d 1581 btrfs_super_total_bytes(&info->super_copy))
9078a3e1
CM
1582 break;
1583 }
1584
1585 btrfs_free_path(path);
1586 return 0;
1587}