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