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