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