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