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