Btrfs: Fix typo in .. check (thanks Yan)
[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>
7bb86316 20#include <linux/crc32c.h>
74493f7a 21#include "hash.h"
fec577fb
CM
22#include "ctree.h"
23#include "disk-io.h"
24#include "print-tree.h"
e089f05c 25#include "transaction.h"
fec577fb 26
96b5179d
CM
27#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
28#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
29#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
30
e089f05c
CM
31static int finish_current_insert(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
e20d96d6
CM
33static int del_pending_extents(struct btrfs_trans_handle *trans, struct
34 btrfs_root *extent_root);
fec577fb 35
e37c9e69
CM
36static int cache_block_group(struct btrfs_root *root,
37 struct btrfs_block_group_cache *block_group)
38{
39 struct btrfs_path *path;
40 int ret;
41 struct btrfs_key key;
5f39d397 42 struct extent_buffer *leaf;
f510cfec 43 struct extent_map_tree *free_space_cache;
e37c9e69 44 int slot;
e37c9e69
CM
45 u64 last = 0;
46 u64 hole_size;
7d7d6068 47 u64 first_free;
e37c9e69
CM
48 int found = 0;
49
00f5c795
CM
50 if (!block_group)
51 return 0;
52
e37c9e69 53 root = root->fs_info->extent_root;
f510cfec 54 free_space_cache = &root->fs_info->free_space_cache;
e37c9e69
CM
55
56 if (block_group->cached)
57 return 0;
f510cfec 58
e37c9e69
CM
59 path = btrfs_alloc_path();
60 if (!path)
61 return -ENOMEM;
7d7d6068 62
2cc58cf2 63 path->reada = 2;
7d7d6068 64 first_free = block_group->key.objectid;
e37c9e69 65 key.objectid = block_group->key.objectid;
e37c9e69 66 key.offset = 0;
7d7d6068 67
e37c9e69
CM
68 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
69 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7d7d6068 70
e37c9e69
CM
71 if (ret < 0)
72 return ret;
7d7d6068 73
e37c9e69
CM
74 if (ret && path->slots[0] > 0)
75 path->slots[0]--;
7d7d6068 76
e37c9e69 77 while(1) {
5f39d397 78 leaf = path->nodes[0];
e37c9e69 79 slot = path->slots[0];
5f39d397 80 if (slot >= btrfs_header_nritems(leaf)) {
e37c9e69 81 ret = btrfs_next_leaf(root, path);
54aa1f4d
CM
82 if (ret < 0)
83 goto err;
de428b63 84 if (ret == 0) {
e37c9e69 85 continue;
de428b63 86 } else {
e37c9e69
CM
87 break;
88 }
89 }
7d7d6068 90
5f39d397 91 btrfs_item_key_to_cpu(leaf, &key, slot);
7d7d6068 92 if (key.objectid < block_group->key.objectid) {
7bb86316
CM
93 if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
94 key.objectid + key.offset > first_free)
7d7d6068
Y
95 first_free = key.objectid + key.offset;
96 goto next;
97 }
98
e37c9e69
CM
99 if (key.objectid >= block_group->key.objectid +
100 block_group->key.offset) {
e37c9e69
CM
101 break;
102 }
7d7d6068 103
e37c9e69
CM
104 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
105 if (!found) {
7d7d6068 106 last = first_free;
e37c9e69 107 found = 1;
e37c9e69 108 }
f510cfec
CM
109 if (key.objectid > last) {
110 hole_size = key.objectid - last;
111 set_extent_dirty(free_space_cache, last,
112 last + hole_size - 1,
113 GFP_NOFS);
7d7d6068
Y
114 }
115 last = key.objectid + key.offset;
e37c9e69 116 }
7d7d6068 117next:
e37c9e69
CM
118 path->slots[0]++;
119 }
120
7d7d6068
Y
121 if (!found)
122 last = first_free;
123 if (block_group->key.objectid +
124 block_group->key.offset > last) {
125 hole_size = block_group->key.objectid +
126 block_group->key.offset - last;
f510cfec
CM
127 set_extent_dirty(free_space_cache, last,
128 last + hole_size - 1, GFP_NOFS);
7d7d6068 129 }
e37c9e69 130 block_group->cached = 1;
54aa1f4d 131err:
e37c9e69
CM
132 btrfs_free_path(path);
133 return 0;
134}
135
5276aeda
CM
136struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
137 btrfs_fs_info *info,
db94535d 138 u64 bytenr)
be744175 139{
96b5179d
CM
140 struct extent_map_tree *block_group_cache;
141 struct btrfs_block_group_cache *block_group = NULL;
142 u64 ptr;
143 u64 start;
144 u64 end;
be744175
CM
145 int ret;
146
96b5179d
CM
147 block_group_cache = &info->block_group_cache;
148 ret = find_first_extent_bit(block_group_cache,
db94535d 149 bytenr, &start, &end,
96b5179d 150 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
be744175 151 if (ret) {
96b5179d 152 return NULL;
be744175 153 }
96b5179d
CM
154 ret = get_state_private(block_group_cache, start, &ptr);
155 if (ret)
156 return NULL;
157
ae2f5411 158 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
5cf66426 159 if (block_group->key.objectid <= bytenr && bytenr <
96b5179d
CM
160 block_group->key.objectid + block_group->key.offset)
161 return block_group;
be744175
CM
162 return NULL;
163}
e37c9e69
CM
164static u64 find_search_start(struct btrfs_root *root,
165 struct btrfs_block_group_cache **cache_ret,
5e5745dc
Y
166 u64 search_start, int num,
167 int data, int full_scan)
e37c9e69 168{
e37c9e69
CM
169 int ret;
170 struct btrfs_block_group_cache *cache = *cache_ret;
e19caa5f 171 u64 last;
f510cfec
CM
172 u64 start = 0;
173 u64 end = 0;
257d0ce3 174 u64 cache_miss = 0;
f84a8b36 175 int wrapped = 0;
e37c9e69 176
00f5c795 177 if (!cache) {
1a2b2ac7 178 goto out;
00f5c795 179 }
e37c9e69 180again:
54aa1f4d
CM
181 ret = cache_block_group(root, cache);
182 if (ret)
183 goto out;
f84a8b36 184
e19caa5f
CM
185 last = max(search_start, cache->key.objectid);
186
e37c9e69 187 while(1) {
f510cfec
CM
188 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
189 last, &start, &end, EXTENT_DIRTY);
e19caa5f 190 if (ret) {
257d0ce3
CM
191 if (!cache_miss)
192 cache_miss = last;
e19caa5f
CM
193 goto new_group;
194 }
f510cfec
CM
195
196 start = max(last, start);
197 last = end + 1;
257d0ce3
CM
198 if (last - start < num) {
199 if (last == cache->key.objectid + cache->key.offset)
200 cache_miss = start;
f510cfec 201 continue;
257d0ce3
CM
202 }
203 if (data != BTRFS_BLOCK_GROUP_MIXED &&
5cf66426 204 start + num > cache->key.objectid + cache->key.offset)
e37c9e69 205 goto new_group;
f510cfec 206 return start;
e37c9e69
CM
207 }
208out:
1a2b2ac7
CM
209 cache = btrfs_lookup_block_group(root->fs_info, search_start);
210 if (!cache) {
211 printk("Unable to find block group for %Lu\n",
212 search_start);
213 WARN_ON(1);
214 return search_start;
215 }
1a5bc167 216 return search_start;
e37c9e69
CM
217
218new_group:
e19caa5f 219 last = cache->key.objectid + cache->key.offset;
f84a8b36 220wrapped:
e19caa5f 221 cache = btrfs_lookup_block_group(root->fs_info, last);
e37c9e69 222 if (!cache) {
0e4de584 223no_cache:
f84a8b36
CM
224 if (!wrapped) {
225 wrapped = 1;
226 last = search_start;
227 data = BTRFS_BLOCK_GROUP_MIXED;
228 goto wrapped;
229 }
1a2b2ac7 230 goto out;
e37c9e69 231 }
257d0ce3
CM
232 if (cache_miss && !cache->cached) {
233 cache_block_group(root, cache);
234 last = cache_miss;
257d0ce3
CM
235 cache = btrfs_lookup_block_group(root->fs_info, last);
236 }
1a2b2ac7 237 cache = btrfs_find_block_group(root, cache, last, data, 0);
0e4de584
CM
238 if (!cache)
239 goto no_cache;
e37c9e69 240 *cache_ret = cache;
257d0ce3 241 cache_miss = 0;
e37c9e69
CM
242 goto again;
243}
244
84f54cfa
CM
245static u64 div_factor(u64 num, int factor)
246{
257d0ce3
CM
247 if (factor == 10)
248 return num;
84f54cfa
CM
249 num *= factor;
250 do_div(num, 10);
251 return num;
252}
253
31f3c99b
CM
254struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
255 struct btrfs_block_group_cache
be744175 256 *hint, u64 search_start,
de428b63 257 int data, int owner)
cd1bc465 258{
96b5179d
CM
259 struct btrfs_block_group_cache *cache;
260 struct extent_map_tree *block_group_cache;
31f3c99b 261 struct btrfs_block_group_cache *found_group = NULL;
cd1bc465
CM
262 struct btrfs_fs_info *info = root->fs_info;
263 u64 used;
31f3c99b
CM
264 u64 last = 0;
265 u64 hint_last;
96b5179d
CM
266 u64 start;
267 u64 end;
268 u64 free_check;
269 u64 ptr;
270 int bit;
cd1bc465 271 int ret;
31f3c99b 272 int full_search = 0;
de428b63 273 int factor = 8;
1e2677e0 274 int data_swap = 0;
de428b63 275
96b5179d
CM
276 block_group_cache = &info->block_group_cache;
277
de428b63 278 if (!owner)
f84a8b36 279 factor = 8;
be744175 280
257d0ce3 281 if (data == BTRFS_BLOCK_GROUP_MIXED) {
f84a8b36 282 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
257d0ce3
CM
283 factor = 10;
284 } else if (data)
96b5179d
CM
285 bit = BLOCK_GROUP_DATA;
286 else
287 bit = BLOCK_GROUP_METADATA;
be744175
CM
288
289 if (search_start) {
290 struct btrfs_block_group_cache *shint;
5276aeda 291 shint = btrfs_lookup_block_group(info, search_start);
f84a8b36
CM
292 if (shint && (shint->data == data ||
293 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
be744175 294 used = btrfs_block_group_used(&shint->item);
324ae4df
Y
295 if (used + shint->pinned <
296 div_factor(shint->key.offset, factor)) {
be744175
CM
297 return shint;
298 }
299 }
300 }
f84a8b36
CM
301 if (hint && (hint->data == data ||
302 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
31f3c99b 303 used = btrfs_block_group_used(&hint->item);
324ae4df
Y
304 if (used + hint->pinned <
305 div_factor(hint->key.offset, factor)) {
31f3c99b
CM
306 return hint;
307 }
e19caa5f 308 last = hint->key.objectid + hint->key.offset;
31f3c99b
CM
309 hint_last = last;
310 } else {
e37c9e69
CM
311 if (hint)
312 hint_last = max(hint->key.objectid, search_start);
313 else
314 hint_last = search_start;
315
316 last = hint_last;
31f3c99b 317 }
31f3c99b 318again:
cd1bc465 319 while(1) {
96b5179d
CM
320 ret = find_first_extent_bit(block_group_cache, last,
321 &start, &end, bit);
322 if (ret)
cd1bc465 323 break;
96b5179d
CM
324
325 ret = get_state_private(block_group_cache, start, &ptr);
326 if (ret)
327 break;
328
ae2f5411 329 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
330 last = cache->key.objectid + cache->key.offset;
331 used = btrfs_block_group_used(&cache->item);
332
333 if (full_search)
334 free_check = cache->key.offset;
335 else
336 free_check = div_factor(cache->key.offset, factor);
324ae4df 337 if (used + cache->pinned < free_check) {
96b5179d
CM
338 found_group = cache;
339 goto found;
cd1bc465 340 }
de428b63 341 cond_resched();
cd1bc465 342 }
31f3c99b 343 if (!full_search) {
be744175 344 last = search_start;
31f3c99b
CM
345 full_search = 1;
346 goto again;
347 }
1e2677e0 348 if (!data_swap) {
1e2677e0 349 data_swap = 1;
96b5179d 350 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1e2677e0
CM
351 last = search_start;
352 goto again;
353 }
be744175 354found:
31f3c99b 355 return found_group;
cd1bc465
CM
356}
357
7bb86316 358static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
74493f7a
CM
359 u64 owner, u64 owner_offset)
360{
361 u32 high_crc = ~(u32)0;
362 u32 low_crc = ~(u32)0;
363 __le64 lenum;
364
365 lenum = cpu_to_le64(root_objectid);
366 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
7bb86316
CM
367 lenum = cpu_to_le64(ref_generation);
368 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
74493f7a 369
7bb86316 370#if 0
74493f7a
CM
371 lenum = cpu_to_le64(owner);
372 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
74493f7a
CM
373 lenum = cpu_to_le64(owner_offset);
374 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
7bb86316 375#endif
74493f7a
CM
376 return ((u64)high_crc << 32) | (u64)low_crc;
377}
378
7bb86316
CM
379static int match_extent_ref(struct extent_buffer *leaf,
380 struct btrfs_extent_ref *disk_ref,
381 struct btrfs_extent_ref *cpu_ref)
382{
383 int ret;
384 int len;
385
386 if (cpu_ref->objectid)
387 len = sizeof(*cpu_ref);
388 else
389 len = 2 * sizeof(u64);
390 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
391 len);
392 return ret == 0;
393}
394
395static int lookup_extent_backref(struct btrfs_trans_handle *trans,
396 struct btrfs_root *root,
397 struct btrfs_path *path, u64 bytenr,
398 u64 root_objectid, u64 ref_generation,
399 u64 owner, u64 owner_offset, int del)
74493f7a
CM
400{
401 u64 hash;
402 struct btrfs_key key;
7bb86316 403 struct btrfs_key found_key;
74493f7a 404 struct btrfs_extent_ref ref;
7bb86316
CM
405 struct extent_buffer *leaf;
406 struct btrfs_extent_ref *disk_ref;
407 int ret;
408 int ret2;
409
410 btrfs_set_stack_ref_root(&ref, root_objectid);
411 btrfs_set_stack_ref_generation(&ref, ref_generation);
412 btrfs_set_stack_ref_objectid(&ref, owner);
413 btrfs_set_stack_ref_offset(&ref, owner_offset);
414
415 hash = hash_extent_ref(root_objectid, ref_generation, owner,
416 owner_offset);
417 key.offset = hash;
418 key.objectid = bytenr;
419 key.type = BTRFS_EXTENT_REF_KEY;
420
421 while (1) {
422 ret = btrfs_search_slot(trans, root, &key, path,
423 del ? -1 : 0, del);
424 if (ret < 0)
425 goto out;
426 leaf = path->nodes[0];
427 if (ret != 0) {
428 u32 nritems = btrfs_header_nritems(leaf);
429 if (path->slots[0] >= nritems) {
430 ret2 = btrfs_next_leaf(root, path);
431 if (ret2)
432 goto out;
433 leaf = path->nodes[0];
434 }
435 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
436 if (found_key.objectid != bytenr ||
437 found_key.type != BTRFS_EXTENT_REF_KEY)
438 goto out;
439 key.offset = found_key.offset;
440 if (del) {
441 btrfs_release_path(root, path);
442 continue;
443 }
444 }
445 disk_ref = btrfs_item_ptr(path->nodes[0],
446 path->slots[0],
447 struct btrfs_extent_ref);
448 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
449 ret = 0;
450 goto out;
451 }
452 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
453 key.offset = found_key.offset + 1;
454 btrfs_release_path(root, path);
455 }
456out:
457 return ret;
458}
459
d8d5f3e1
CM
460/*
461 * Back reference rules. Back refs have three main goals:
462 *
463 * 1) differentiate between all holders of references to an extent so that
464 * when a reference is dropped we can make sure it was a valid reference
465 * before freeing the extent.
466 *
467 * 2) Provide enough information to quickly find the holders of an extent
468 * if we notice a given block is corrupted or bad.
469 *
470 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
471 * maintenance. This is actually the same as #2, but with a slightly
472 * different use case.
473 *
474 * File extents can be referenced by:
475 *
476 * - multiple snapshots, subvolumes, or different generations in one subvol
477 * - different files inside a single subvolume (in theory, not implemented yet)
478 * - different offsets inside a file (bookend extents in file.c)
479 *
480 * The extent ref structure has fields for:
481 *
482 * - Objectid of the subvolume root
483 * - Generation number of the tree holding the reference
484 * - objectid of the file holding the reference
485 * - offset in the file corresponding to the key holding the reference
486 *
487 * When a file extent is allocated the fields are filled in:
488 * (root_key.objectid, trans->transid, inode objectid, offset in file)
489 *
490 * When a leaf is cow'd new references are added for every file extent found
491 * in the leaf. It looks the same as the create case, but trans->transid
492 * will be different when the block is cow'd.
493 *
494 * (root_key.objectid, trans->transid, inode objectid, offset in file)
495 *
496 * When a file extent is removed either during snapshot deletion or file
497 * truncation, the corresponding back reference is found
498 * by searching for:
499 *
500 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
501 * inode objectid, offset in file)
502 *
503 * Btree extents can be referenced by:
504 *
505 * - Different subvolumes
506 * - Different generations of the same subvolume
507 *
508 * Storing sufficient information for a full reverse mapping of a btree
509 * block would require storing the lowest key of the block in the backref,
510 * and it would require updating that lowest key either before write out or
511 * every time it changed. Instead, the objectid of the lowest key is stored
512 * along with the level of the tree block. This provides a hint
513 * about where in the btree the block can be found. Searches through the
514 * btree only need to look for a pointer to that block, so they stop one
515 * level higher than the level recorded in the backref.
516 *
517 * Some btrees do not do reference counting on their extents. These
518 * include the extent tree and the tree of tree roots. Backrefs for these
519 * trees always have a generation of zero.
520 *
521 * When a tree block is created, back references are inserted:
522 *
523 * (root->root_key.objectid, trans->transid or zero, lowest_key_objectid, level)
524 *
525 * When a tree block is cow'd in a reference counted root,
526 * new back references are added for all the blocks it points to.
527 * These are of the form (trans->transid will have increased since creation):
528 *
529 * (root->root_key.objectid, trans->transid, lowest_key_objectid, level)
530 *
531 * Because the lowest_key_objectid and the level are just hints
532 * they are not used when backrefs are deleted. When a backref is deleted:
533 *
534 * if backref was for a tree root:
535 * root_objectid = root->root_key.objectid
536 * else
537 * root_objectid = btrfs_header_owner(parent)
538 *
539 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
540 *
541 * Back Reference Key hashing:
542 *
543 * Back references have four fields, each 64 bits long. Unfortunately,
544 * This is hashed into a single 64 bit number and placed into the key offset.
545 * The key objectid corresponds to the first byte in the extent, and the
546 * key type is set to BTRFS_EXTENT_REF_KEY
547 */
7bb86316
CM
548int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
549 struct btrfs_root *root,
550 struct btrfs_path *path, u64 bytenr,
551 u64 root_objectid, u64 ref_generation,
552 u64 owner, u64 owner_offset)
553{
554 u64 hash;
555 struct btrfs_key key;
556 struct btrfs_extent_ref ref;
557 struct btrfs_extent_ref *disk_ref;
74493f7a
CM
558 int ret;
559
560 btrfs_set_stack_ref_root(&ref, root_objectid);
7bb86316 561 btrfs_set_stack_ref_generation(&ref, ref_generation);
74493f7a
CM
562 btrfs_set_stack_ref_objectid(&ref, owner);
563 btrfs_set_stack_ref_offset(&ref, owner_offset);
564
7bb86316
CM
565 hash = hash_extent_ref(root_objectid, ref_generation, owner,
566 owner_offset);
74493f7a
CM
567 key.offset = hash;
568 key.objectid = bytenr;
569 key.type = BTRFS_EXTENT_REF_KEY;
570
571 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
572 while (ret == -EEXIST) {
7bb86316
CM
573 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
574 struct btrfs_extent_ref);
575 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
576 goto out;
577 key.offset++;
578 btrfs_release_path(root, path);
579 ret = btrfs_insert_empty_item(trans, root, path, &key,
580 sizeof(ref));
74493f7a 581 }
7bb86316
CM
582 if (ret)
583 goto out;
584 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
585 struct btrfs_extent_ref);
586 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
587 sizeof(ref));
588 btrfs_mark_buffer_dirty(path->nodes[0]);
589out:
590 btrfs_release_path(root, path);
591 return ret;
74493f7a
CM
592}
593
b18c6685
CM
594int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
595 struct btrfs_root *root,
74493f7a 596 u64 bytenr, u64 num_bytes,
7bb86316 597 u64 root_objectid, u64 ref_generation,
74493f7a 598 u64 owner, u64 owner_offset)
02217ed2 599{
5caf2a00 600 struct btrfs_path *path;
02217ed2 601 int ret;
e2fa7227 602 struct btrfs_key key;
5f39d397 603 struct extent_buffer *l;
234b63a0 604 struct btrfs_extent_item *item;
cf27e1ee 605 u32 refs;
037e6390 606
db94535d 607 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 608 path = btrfs_alloc_path();
54aa1f4d
CM
609 if (!path)
610 return -ENOMEM;
26b8003f 611
db94535d 612 key.objectid = bytenr;
62e2749e 613 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 614 key.offset = num_bytes;
5caf2a00 615 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 616 0, 1);
54aa1f4d
CM
617 if (ret < 0)
618 return ret;
a429e513 619 if (ret != 0) {
a28ec197 620 BUG();
a429e513 621 }
02217ed2 622 BUG_ON(ret != 0);
5f39d397 623 l = path->nodes[0];
5caf2a00 624 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397
CM
625 refs = btrfs_extent_refs(l, item);
626 btrfs_set_extent_refs(l, item, refs + 1);
5caf2a00 627 btrfs_mark_buffer_dirty(path->nodes[0]);
a28ec197 628
5caf2a00 629 btrfs_release_path(root->fs_info->extent_root, path);
7bb86316
CM
630
631 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
632 path, bytenr, root_objectid,
633 ref_generation, owner, owner_offset);
634 BUG_ON(ret);
9f5fae2f 635 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 636 del_pending_extents(trans, root->fs_info->extent_root);
74493f7a
CM
637
638 btrfs_free_path(path);
02217ed2
CM
639 return 0;
640}
641
e9d0b13b
CM
642int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
643 struct btrfs_root *root)
644{
645 finish_current_insert(trans, root->fs_info->extent_root);
646 del_pending_extents(trans, root->fs_info->extent_root);
647 return 0;
648}
649
b18c6685 650static int lookup_extent_ref(struct btrfs_trans_handle *trans,
db94535d
CM
651 struct btrfs_root *root, u64 bytenr,
652 u64 num_bytes, u32 *refs)
a28ec197 653{
5caf2a00 654 struct btrfs_path *path;
a28ec197 655 int ret;
e2fa7227 656 struct btrfs_key key;
5f39d397 657 struct extent_buffer *l;
234b63a0 658 struct btrfs_extent_item *item;
5caf2a00 659
db94535d 660 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 661 path = btrfs_alloc_path();
db94535d
CM
662 key.objectid = bytenr;
663 key.offset = num_bytes;
62e2749e 664 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
5caf2a00 665 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 666 0, 0);
54aa1f4d
CM
667 if (ret < 0)
668 goto out;
5f39d397
CM
669 if (ret != 0) {
670 btrfs_print_leaf(root, path->nodes[0]);
db94535d 671 printk("failed to find block number %Lu\n", bytenr);
a28ec197 672 BUG();
5f39d397
CM
673 }
674 l = path->nodes[0];
5caf2a00 675 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397 676 *refs = btrfs_extent_refs(l, item);
54aa1f4d 677out:
5caf2a00 678 btrfs_free_path(path);
a28ec197
CM
679 return 0;
680}
681
c5739bba 682int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
7bb86316 683 struct btrfs_root *root, u64 owner_objectid)
c5739bba 684{
7bb86316
CM
685 u64 generation;
686 u64 key_objectid;
687 u64 level;
688 u32 nritems;
689 struct btrfs_disk_key disk_key;
690
691 level = btrfs_header_level(root->node);
692 generation = trans->transid;
693 nritems = btrfs_header_nritems(root->node);
694 if (nritems > 0) {
695 if (level == 0)
696 btrfs_item_key(root->node, &disk_key, 0);
697 else
698 btrfs_node_key(root->node, &disk_key, 0);
699 key_objectid = btrfs_disk_key_objectid(&disk_key);
700 } else {
701 key_objectid = 0;
702 }
db94535d 703 return btrfs_inc_extent_ref(trans, root, root->node->start,
7bb86316
CM
704 root->node->len, owner_objectid,
705 generation, 0, 0);
c5739bba
CM
706}
707
e089f05c 708int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 709 struct extent_buffer *buf)
02217ed2 710{
db94535d 711 u64 bytenr;
5f39d397
CM
712 u32 nritems;
713 struct btrfs_key key;
6407bf6d 714 struct btrfs_file_extent_item *fi;
02217ed2 715 int i;
db94535d 716 int level;
6407bf6d 717 int ret;
54aa1f4d 718 int faili;
a28ec197 719
3768f368 720 if (!root->ref_cows)
a28ec197 721 return 0;
5f39d397 722
db94535d 723 level = btrfs_header_level(buf);
5f39d397
CM
724 nritems = btrfs_header_nritems(buf);
725 for (i = 0; i < nritems; i++) {
db94535d
CM
726 if (level == 0) {
727 u64 disk_bytenr;
5f39d397
CM
728 btrfs_item_key_to_cpu(buf, &key, i);
729 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d 730 continue;
5f39d397 731 fi = btrfs_item_ptr(buf, i,
6407bf6d 732 struct btrfs_file_extent_item);
5f39d397 733 if (btrfs_file_extent_type(buf, fi) ==
236454df
CM
734 BTRFS_FILE_EXTENT_INLINE)
735 continue;
db94535d
CM
736 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
737 if (disk_bytenr == 0)
3a686375 738 continue;
db94535d 739 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
7bb86316
CM
740 btrfs_file_extent_disk_num_bytes(buf, fi),
741 root->root_key.objectid, trans->transid,
742 key.objectid, key.offset);
54aa1f4d
CM
743 if (ret) {
744 faili = i;
745 goto fail;
746 }
6407bf6d 747 } else {
db94535d
CM
748 bytenr = btrfs_node_blockptr(buf, i);
749 ret = btrfs_inc_extent_ref(trans, root, bytenr,
7bb86316
CM
750 btrfs_level_size(root, level - 1),
751 root->root_key.objectid,
752 trans->transid, 0, 0);
54aa1f4d
CM
753 if (ret) {
754 faili = i;
755 goto fail;
756 }
6407bf6d 757 }
02217ed2
CM
758 }
759 return 0;
54aa1f4d 760fail:
ccd467d6 761 WARN_ON(1);
7bb86316 762#if 0
54aa1f4d 763 for (i =0; i < faili; i++) {
db94535d
CM
764 if (level == 0) {
765 u64 disk_bytenr;
5f39d397
CM
766 btrfs_item_key_to_cpu(buf, &key, i);
767 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
54aa1f4d 768 continue;
5f39d397 769 fi = btrfs_item_ptr(buf, i,
54aa1f4d 770 struct btrfs_file_extent_item);
5f39d397 771 if (btrfs_file_extent_type(buf, fi) ==
54aa1f4d
CM
772 BTRFS_FILE_EXTENT_INLINE)
773 continue;
db94535d
CM
774 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
775 if (disk_bytenr == 0)
54aa1f4d 776 continue;
db94535d
CM
777 err = btrfs_free_extent(trans, root, disk_bytenr,
778 btrfs_file_extent_disk_num_bytes(buf,
5f39d397 779 fi), 0);
54aa1f4d
CM
780 BUG_ON(err);
781 } else {
db94535d
CM
782 bytenr = btrfs_node_blockptr(buf, i);
783 err = btrfs_free_extent(trans, root, bytenr,
784 btrfs_level_size(root, level - 1), 0);
54aa1f4d
CM
785 BUG_ON(err);
786 }
787 }
7bb86316 788#endif
54aa1f4d 789 return ret;
02217ed2
CM
790}
791
9078a3e1
CM
792static int write_one_cache_group(struct btrfs_trans_handle *trans,
793 struct btrfs_root *root,
794 struct btrfs_path *path,
795 struct btrfs_block_group_cache *cache)
796{
797 int ret;
798 int pending_ret;
799 struct btrfs_root *extent_root = root->fs_info->extent_root;
5f39d397
CM
800 unsigned long bi;
801 struct extent_buffer *leaf;
9078a3e1 802
9078a3e1 803 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
54aa1f4d
CM
804 if (ret < 0)
805 goto fail;
9078a3e1 806 BUG_ON(ret);
5f39d397
CM
807
808 leaf = path->nodes[0];
809 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
810 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
811 btrfs_mark_buffer_dirty(leaf);
9078a3e1 812 btrfs_release_path(extent_root, path);
54aa1f4d 813fail:
9078a3e1
CM
814 finish_current_insert(trans, extent_root);
815 pending_ret = del_pending_extents(trans, extent_root);
816 if (ret)
817 return ret;
818 if (pending_ret)
819 return pending_ret;
820 return 0;
821
822}
823
96b5179d
CM
824int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
825 struct btrfs_root *root)
9078a3e1 826{
96b5179d
CM
827 struct extent_map_tree *block_group_cache;
828 struct btrfs_block_group_cache *cache;
9078a3e1
CM
829 int ret;
830 int err = 0;
831 int werr = 0;
9078a3e1 832 struct btrfs_path *path;
96b5179d
CM
833 u64 last = 0;
834 u64 start;
835 u64 end;
836 u64 ptr;
9078a3e1 837
96b5179d 838 block_group_cache = &root->fs_info->block_group_cache;
9078a3e1
CM
839 path = btrfs_alloc_path();
840 if (!path)
841 return -ENOMEM;
842
843 while(1) {
96b5179d
CM
844 ret = find_first_extent_bit(block_group_cache, last,
845 &start, &end, BLOCK_GROUP_DIRTY);
846 if (ret)
9078a3e1 847 break;
54aa1f4d 848
96b5179d
CM
849 last = end + 1;
850 ret = get_state_private(block_group_cache, start, &ptr);
851 if (ret)
852 break;
853
ae2f5411 854 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
855 err = write_one_cache_group(trans, root,
856 path, cache);
857 /*
858 * if we fail to write the cache group, we want
859 * to keep it marked dirty in hopes that a later
860 * write will work
861 */
862 if (err) {
863 werr = err;
864 continue;
9078a3e1 865 }
96b5179d
CM
866 clear_extent_bits(block_group_cache, start, end,
867 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
868 }
869 btrfs_free_path(path);
870 return werr;
871}
872
873static int update_block_group(struct btrfs_trans_handle *trans,
874 struct btrfs_root *root,
db94535d
CM
875 u64 bytenr, u64 num_bytes, int alloc,
876 int mark_free, int data)
9078a3e1
CM
877{
878 struct btrfs_block_group_cache *cache;
879 struct btrfs_fs_info *info = root->fs_info;
db94535d 880 u64 total = num_bytes;
9078a3e1 881 u64 old_val;
db94535d 882 u64 byte_in_group;
96b5179d
CM
883 u64 start;
884 u64 end;
3e1ad54f 885
9078a3e1 886 while(total) {
db94535d 887 cache = btrfs_lookup_block_group(info, bytenr);
3e1ad54f 888 if (!cache) {
9078a3e1 889 return -1;
cd1bc465 890 }
db94535d
CM
891 byte_in_group = bytenr - cache->key.objectid;
892 WARN_ON(byte_in_group > cache->key.offset);
96b5179d
CM
893 start = cache->key.objectid;
894 end = start + cache->key.offset - 1;
895 set_extent_bits(&info->block_group_cache, start, end,
896 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
897
898 old_val = btrfs_block_group_used(&cache->item);
db94535d 899 num_bytes = min(total, cache->key.offset - byte_in_group);
cd1bc465 900 if (alloc) {
1e2677e0 901 if (cache->data != data &&
84f54cfa 902 old_val < (cache->key.offset >> 1)) {
96b5179d
CM
903 int bit_to_clear;
904 int bit_to_set;
96b5179d 905 cache->data = data;
1e2677e0 906 if (data) {
b97f9203
Y
907 bit_to_clear = BLOCK_GROUP_METADATA;
908 bit_to_set = BLOCK_GROUP_DATA;
f84a8b36
CM
909 cache->item.flags &=
910 ~BTRFS_BLOCK_GROUP_MIXED;
1e2677e0
CM
911 cache->item.flags |=
912 BTRFS_BLOCK_GROUP_DATA;
913 } else {
b97f9203
Y
914 bit_to_clear = BLOCK_GROUP_DATA;
915 bit_to_set = BLOCK_GROUP_METADATA;
f84a8b36
CM
916 cache->item.flags &=
917 ~BTRFS_BLOCK_GROUP_MIXED;
1e2677e0
CM
918 cache->item.flags &=
919 ~BTRFS_BLOCK_GROUP_DATA;
920 }
96b5179d
CM
921 clear_extent_bits(&info->block_group_cache,
922 start, end, bit_to_clear,
923 GFP_NOFS);
924 set_extent_bits(&info->block_group_cache,
925 start, end, bit_to_set,
926 GFP_NOFS);
f84a8b36
CM
927 } else if (cache->data != data &&
928 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
929 cache->data = BTRFS_BLOCK_GROUP_MIXED;
930 set_extent_bits(&info->block_group_cache,
931 start, end,
932 BLOCK_GROUP_DATA |
933 BLOCK_GROUP_METADATA,
934 GFP_NOFS);
1e2677e0 935 }
db94535d 936 old_val += num_bytes;
cd1bc465 937 } else {
db94535d 938 old_val -= num_bytes;
f510cfec
CM
939 if (mark_free) {
940 set_extent_dirty(&info->free_space_cache,
db94535d 941 bytenr, bytenr + num_bytes - 1,
f510cfec 942 GFP_NOFS);
e37c9e69 943 }
cd1bc465 944 }
9078a3e1 945 btrfs_set_block_group_used(&cache->item, old_val);
db94535d
CM
946 total -= num_bytes;
947 bytenr += num_bytes;
9078a3e1
CM
948 }
949 return 0;
950}
324ae4df
Y
951static int update_pinned_extents(struct btrfs_root *root,
952 u64 bytenr, u64 num, int pin)
953{
954 u64 len;
955 struct btrfs_block_group_cache *cache;
956 struct btrfs_fs_info *fs_info = root->fs_info;
957
958 if (pin) {
959 set_extent_dirty(&fs_info->pinned_extents,
960 bytenr, bytenr + num - 1, GFP_NOFS);
961 } else {
962 clear_extent_dirty(&fs_info->pinned_extents,
963 bytenr, bytenr + num - 1, GFP_NOFS);
964 }
965 while (num > 0) {
966 cache = btrfs_lookup_block_group(fs_info, bytenr);
967 WARN_ON(!cache);
968 len = min(num, cache->key.offset -
969 (bytenr - cache->key.objectid));
970 if (pin) {
971 cache->pinned += len;
972 fs_info->total_pinned += len;
973 } else {
974 cache->pinned -= len;
975 fs_info->total_pinned -= len;
976 }
977 bytenr += len;
978 num -= len;
979 }
980 return 0;
981}
9078a3e1 982
1a5bc167 983int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
ccd467d6 984{
ccd467d6 985 u64 last = 0;
1a5bc167
CM
986 u64 start;
987 u64 end;
988 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
ccd467d6 989 int ret;
ccd467d6
CM
990
991 while(1) {
1a5bc167
CM
992 ret = find_first_extent_bit(pinned_extents, last,
993 &start, &end, EXTENT_DIRTY);
994 if (ret)
ccd467d6 995 break;
1a5bc167
CM
996 set_extent_dirty(copy, start, end, GFP_NOFS);
997 last = end + 1;
ccd467d6
CM
998 }
999 return 0;
1000}
1001
1002int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1003 struct btrfs_root *root,
1a5bc167 1004 struct extent_map_tree *unpin)
a28ec197 1005{
1a5bc167
CM
1006 u64 start;
1007 u64 end;
a28ec197 1008 int ret;
f510cfec 1009 struct extent_map_tree *free_space_cache;
f510cfec 1010 free_space_cache = &root->fs_info->free_space_cache;
a28ec197
CM
1011
1012 while(1) {
1a5bc167
CM
1013 ret = find_first_extent_bit(unpin, 0, &start, &end,
1014 EXTENT_DIRTY);
1015 if (ret)
a28ec197 1016 break;
324ae4df 1017 update_pinned_extents(root, start, end + 1 - start, 0);
1a5bc167
CM
1018 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1019 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
a28ec197
CM
1020 }
1021 return 0;
1022}
1023
e089f05c
CM
1024static int finish_current_insert(struct btrfs_trans_handle *trans, struct
1025 btrfs_root *extent_root)
037e6390 1026{
7bb86316
CM
1027 u64 start;
1028 u64 end;
1029 struct btrfs_fs_info *info = extent_root->fs_info;
d8d5f3e1 1030 struct extent_buffer *eb;
7bb86316 1031 struct btrfs_path *path;
e2fa7227 1032 struct btrfs_key ins;
d8d5f3e1 1033 struct btrfs_disk_key first;
234b63a0 1034 struct btrfs_extent_item extent_item;
037e6390 1035 int ret;
d8d5f3e1 1036 int level;
1a5bc167 1037 int err = 0;
037e6390 1038
5f39d397 1039 btrfs_set_stack_extent_refs(&extent_item, 1);
62e2749e 1040 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
7bb86316 1041 path = btrfs_alloc_path();
037e6390 1042
26b8003f 1043 while(1) {
1a5bc167
CM
1044 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1045 &end, EXTENT_LOCKED);
1046 if (ret)
26b8003f
CM
1047 break;
1048
1a5bc167
CM
1049 ins.objectid = start;
1050 ins.offset = end + 1 - start;
1051 err = btrfs_insert_item(trans, extent_root, &ins,
1052 &extent_item, sizeof(extent_item));
1053 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1054 GFP_NOFS);
d8d5f3e1
CM
1055 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1056 level = btrfs_header_level(eb);
1057 if (level == 0) {
1058 btrfs_item_key(eb, &first, 0);
1059 } else {
1060 btrfs_node_key(eb, &first, 0);
1061 }
7bb86316
CM
1062 err = btrfs_insert_extent_backref(trans, extent_root, path,
1063 start, extent_root->root_key.objectid,
d8d5f3e1
CM
1064 0, btrfs_disk_key_objectid(&first),
1065 level);
7bb86316 1066 BUG_ON(err);
d8d5f3e1 1067 free_extent_buffer(eb);
037e6390 1068 }
7bb86316 1069 btrfs_free_path(path);
037e6390
CM
1070 return 0;
1071}
1072
db94535d
CM
1073static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1074 int pending)
e20d96d6 1075{
1a5bc167 1076 int err = 0;
5f39d397 1077 struct extent_buffer *buf;
8ef97622 1078
f4b9aa8d 1079 if (!pending) {
db94535d 1080 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
5f39d397
CM
1081 if (buf) {
1082 if (btrfs_buffer_uptodate(buf)) {
2c90e5d6
CM
1083 u64 transid =
1084 root->fs_info->running_transaction->transid;
5f39d397
CM
1085 if (btrfs_header_generation(buf) == transid) {
1086 free_extent_buffer(buf);
c549228f 1087 return 1;
2c90e5d6 1088 }
f4b9aa8d 1089 }
5f39d397 1090 free_extent_buffer(buf);
8ef97622 1091 }
324ae4df 1092 update_pinned_extents(root, bytenr, num_bytes, 1);
f4b9aa8d 1093 } else {
1a5bc167 1094 set_extent_bits(&root->fs_info->pending_del,
db94535d
CM
1095 bytenr, bytenr + num_bytes - 1,
1096 EXTENT_LOCKED, GFP_NOFS);
f4b9aa8d 1097 }
be744175 1098 BUG_ON(err < 0);
e20d96d6
CM
1099 return 0;
1100}
1101
fec577fb 1102/*
a28ec197 1103 * remove an extent from the root, returns 0 on success
fec577fb 1104 */
e089f05c 1105static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
7bb86316
CM
1106 *root, u64 bytenr, u64 num_bytes,
1107 u64 root_objectid, u64 ref_generation,
1108 u64 owner_objectid, u64 owner_offset, int pin,
e37c9e69 1109 int mark_free)
a28ec197 1110{
5caf2a00 1111 struct btrfs_path *path;
e2fa7227 1112 struct btrfs_key key;
1261ec42
CM
1113 struct btrfs_fs_info *info = root->fs_info;
1114 struct btrfs_root *extent_root = info->extent_root;
5f39d397 1115 struct extent_buffer *leaf;
a28ec197 1116 int ret;
234b63a0 1117 struct btrfs_extent_item *ei;
cf27e1ee 1118 u32 refs;
037e6390 1119
db94535d 1120 key.objectid = bytenr;
62e2749e 1121 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 1122 key.offset = num_bytes;
a28ec197 1123
5caf2a00 1124 path = btrfs_alloc_path();
54aa1f4d
CM
1125 if (!path)
1126 return -ENOMEM;
5f26f772 1127
7bb86316
CM
1128 if (ref_generation && owner_objectid == 0 && root_objectid == 3) {
1129//printk("drop backref root %Lu gen %Lu byte %Lu\n", root_objectid, ref_generation, bytenr );
1130 }
1131 ret = lookup_extent_backref(trans, extent_root, path,
1132 bytenr, root_objectid,
1133 ref_generation,
1134 owner_objectid, owner_offset, 1);
1135 if (ret == 0) {
1136 ret = btrfs_del_item(trans, extent_root, path);
1137 } else {
1138 btrfs_print_leaf(extent_root, path->nodes[0]);
1139 WARN_ON(1);
1140 printk("Unable to find ref byte nr %Lu root %Lu "
1141 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1142 root_objectid, ref_generation, owner_objectid,
1143 owner_offset);
1144 }
1145 btrfs_release_path(extent_root, path);
54aa1f4d
CM
1146 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1147 if (ret < 0)
1148 return ret;
1149 BUG_ON(ret);
5f39d397
CM
1150
1151 leaf = path->nodes[0];
1152 ei = btrfs_item_ptr(leaf, path->slots[0],
123abc88 1153 struct btrfs_extent_item);
5f39d397
CM
1154 refs = btrfs_extent_refs(leaf, ei);
1155 BUG_ON(refs == 0);
1156 refs -= 1;
1157 btrfs_set_extent_refs(leaf, ei, refs);
1158 btrfs_mark_buffer_dirty(leaf);
1159
cf27e1ee 1160 if (refs == 0) {
db94535d
CM
1161 u64 super_used;
1162 u64 root_used;
78fae27e
CM
1163
1164 if (pin) {
db94535d 1165 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
c549228f
Y
1166 if (ret > 0)
1167 mark_free = 1;
1168 BUG_ON(ret < 0);
78fae27e
CM
1169 }
1170
58176a96 1171 /* block accounting for super block */
db94535d
CM
1172 super_used = btrfs_super_bytes_used(&info->super_copy);
1173 btrfs_set_super_bytes_used(&info->super_copy,
1174 super_used - num_bytes);
58176a96
JB
1175
1176 /* block accounting for root item */
db94535d 1177 root_used = btrfs_root_used(&root->root_item);
5f39d397 1178 btrfs_set_root_used(&root->root_item,
db94535d 1179 root_used - num_bytes);
58176a96 1180
5caf2a00 1181 ret = btrfs_del_item(trans, extent_root, path);
54aa1f4d
CM
1182 if (ret) {
1183 return ret;
1184 }
db94535d 1185 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1e2677e0 1186 mark_free, 0);
9078a3e1 1187 BUG_ON(ret);
a28ec197 1188 }
5caf2a00 1189 btrfs_free_path(path);
e089f05c 1190 finish_current_insert(trans, extent_root);
a28ec197
CM
1191 return ret;
1192}
1193
a28ec197
CM
1194/*
1195 * find all the blocks marked as pending in the radix tree and remove
1196 * them from the extent map
1197 */
e089f05c
CM
1198static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1199 btrfs_root *extent_root)
a28ec197
CM
1200{
1201 int ret;
e20d96d6 1202 int err = 0;
1a5bc167
CM
1203 u64 start;
1204 u64 end;
1205 struct extent_map_tree *pending_del;
1206 struct extent_map_tree *pinned_extents;
8ef97622 1207
1a5bc167
CM
1208 pending_del = &extent_root->fs_info->pending_del;
1209 pinned_extents = &extent_root->fs_info->pinned_extents;
a28ec197
CM
1210
1211 while(1) {
1a5bc167
CM
1212 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1213 EXTENT_LOCKED);
1214 if (ret)
a28ec197 1215 break;
324ae4df 1216 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1a5bc167
CM
1217 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1218 GFP_NOFS);
1219 ret = __free_extent(trans, extent_root,
7bb86316
CM
1220 start, end + 1 - start,
1221 extent_root->root_key.objectid,
1222 0, 0, 0, 0, 0);
1a5bc167
CM
1223 if (ret)
1224 err = ret;
fec577fb 1225 }
e20d96d6 1226 return err;
fec577fb
CM
1227}
1228
1229/*
1230 * remove an extent from the root, returns 0 on success
1231 */
e089f05c 1232int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
7bb86316
CM
1233 *root, u64 bytenr, u64 num_bytes,
1234 u64 root_objectid, u64 ref_generation,
1235 u64 owner_objectid, u64 owner_offset, int pin)
fec577fb 1236{
9f5fae2f 1237 struct btrfs_root *extent_root = root->fs_info->extent_root;
fec577fb
CM
1238 int pending_ret;
1239 int ret;
a28ec197 1240
db94535d 1241 WARN_ON(num_bytes < root->sectorsize);
7bb86316
CM
1242 if (!root->ref_cows)
1243 ref_generation = 0;
1244
fec577fb 1245 if (root == extent_root) {
db94535d 1246 pin_down_bytes(root, bytenr, num_bytes, 1);
fec577fb
CM
1247 return 0;
1248 }
7bb86316
CM
1249 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1250 ref_generation, owner_objectid, owner_offset,
1251 pin, pin == 0);
e20d96d6 1252 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
fec577fb
CM
1253 return ret ? ret : pending_ret;
1254}
1255
87ee04eb
CM
1256static u64 stripe_align(struct btrfs_root *root, u64 val)
1257{
1258 u64 mask = ((u64)root->stripesize - 1);
1259 u64 ret = (val + mask) & ~mask;
1260 return ret;
1261}
1262
fec577fb
CM
1263/*
1264 * walks the btree of allocated extents and find a hole of a given size.
1265 * The key ins is changed to record the hole:
1266 * ins->objectid == block start
62e2749e 1267 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
1268 * ins->offset == number of blocks
1269 * Any available blocks before search_start are skipped.
1270 */
e089f05c 1271static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
db94535d
CM
1272 *orig_root, u64 num_bytes, u64 empty_size,
1273 u64 search_start, u64 search_end, u64 hint_byte,
f2654de4
CM
1274 struct btrfs_key *ins, u64 exclude_start,
1275 u64 exclude_nr, int data)
fec577fb 1276{
5caf2a00 1277 struct btrfs_path *path;
e2fa7227 1278 struct btrfs_key key;
fec577fb 1279 u64 hole_size = 0;
87ee04eb
CM
1280 u64 aligned;
1281 int ret;
fec577fb 1282 int slot = 0;
db94535d 1283 u64 last_byte = 0;
be744175 1284 u64 orig_search_start = search_start;
fec577fb 1285 int start_found;
5f39d397 1286 struct extent_buffer *l;
9f5fae2f 1287 struct btrfs_root * root = orig_root->fs_info->extent_root;
f2458e1d 1288 struct btrfs_fs_info *info = root->fs_info;
db94535d 1289 u64 total_needed = num_bytes;
e20d96d6 1290 int level;
be08c1b9 1291 struct btrfs_block_group_cache *block_group;
be744175 1292 int full_scan = 0;
fbdc762b 1293 int wrapped = 0;
f84a8b36 1294 u64 cached_start;
fec577fb 1295
db94535d 1296 WARN_ON(num_bytes < root->sectorsize);
b1a4d965
CM
1297 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1298
5f39d397
CM
1299 level = btrfs_header_level(root->node);
1300
015a739c 1301 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
257d0ce3
CM
1302 data = BTRFS_BLOCK_GROUP_MIXED;
1303 }
1304
3e1ad54f 1305 if (search_end == (u64)-1)
db94535d
CM
1306 search_end = btrfs_super_total_bytes(&info->super_copy);
1307 if (hint_byte) {
1308 block_group = btrfs_lookup_block_group(info, hint_byte);
1a2b2ac7
CM
1309 if (!block_group)
1310 hint_byte = search_start;
be744175 1311 block_group = btrfs_find_block_group(root, block_group,
db94535d 1312 hint_byte, data, 1);
be744175
CM
1313 } else {
1314 block_group = btrfs_find_block_group(root,
1a2b2ac7
CM
1315 trans->block_group,
1316 search_start, data, 1);
be744175
CM
1317 }
1318
6702ed49 1319 total_needed += empty_size;
e011599b 1320 path = btrfs_alloc_path();
be744175 1321check_failed:
70b043f0
CM
1322 if (!block_group) {
1323 block_group = btrfs_lookup_block_group(info, search_start);
1324 if (!block_group)
1325 block_group = btrfs_lookup_block_group(info,
1326 orig_search_start);
1327 }
5e5745dc
Y
1328 search_start = find_search_start(root, &block_group, search_start,
1329 total_needed, data, full_scan);
87ee04eb 1330 search_start = stripe_align(root, search_start);
f84a8b36 1331 cached_start = search_start;
5caf2a00 1332 btrfs_init_path(path);
fec577fb
CM
1333 ins->objectid = search_start;
1334 ins->offset = 0;
fec577fb 1335 start_found = 0;
2cc58cf2 1336 path->reada = 2;
e37c9e69 1337
5caf2a00 1338 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
0f70abe2
CM
1339 if (ret < 0)
1340 goto error;
aa5d6bed 1341
e37c9e69 1342 if (path->slots[0] > 0) {
5caf2a00 1343 path->slots[0]--;
e37c9e69
CM
1344 }
1345
5f39d397
CM
1346 l = path->nodes[0];
1347 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1348
e37c9e69 1349 /*
7bb86316 1350 * walk backwards to find the first extent item key
e37c9e69 1351 */
7bb86316
CM
1352 while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1353 if (path->slots[0] == 0) {
1354 ret = btrfs_prev_leaf(root, path);
1355 if (ret != 0) {
1356 ret = btrfs_search_slot(trans, root, ins,
1357 path, 0, 0);
1358 if (ret < 0)
1359 goto error;
1360 if (path->slots[0] > 0)
1361 path->slots[0]--;
1362 break;
1363 }
1364 } else {
e37c9e69
CM
1365 path->slots[0]--;
1366 }
7bb86316
CM
1367 l = path->nodes[0];
1368 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
e37c9e69 1369 }
fec577fb 1370 while (1) {
5f39d397 1371 l = path->nodes[0];
5caf2a00 1372 slot = path->slots[0];
5f39d397 1373 if (slot >= btrfs_header_nritems(l)) {
5caf2a00 1374 ret = btrfs_next_leaf(root, path);
fec577fb
CM
1375 if (ret == 0)
1376 continue;
0f70abe2
CM
1377 if (ret < 0)
1378 goto error;
e19caa5f
CM
1379
1380 search_start = max(search_start,
1381 block_group->key.objectid);
fec577fb 1382 if (!start_found) {
87ee04eb
CM
1383 aligned = stripe_align(root, search_start);
1384 ins->objectid = aligned;
1385 if (aligned >= search_end) {
1386 ret = -ENOSPC;
1387 goto error;
1388 }
1389 ins->offset = search_end - aligned;
fec577fb
CM
1390 start_found = 1;
1391 goto check_pending;
1392 }
87ee04eb
CM
1393 ins->objectid = stripe_align(root,
1394 last_byte > search_start ?
1395 last_byte : search_start);
1396 if (search_end <= ins->objectid) {
1397 ret = -ENOSPC;
1398 goto error;
1399 }
3e1ad54f 1400 ins->offset = search_end - ins->objectid;
e19caa5f 1401 BUG_ON(ins->objectid >= search_end);
fec577fb
CM
1402 goto check_pending;
1403 }
5f39d397 1404 btrfs_item_key_to_cpu(l, &key, slot);
96b5179d 1405
db94535d 1406 if (key.objectid >= search_start && key.objectid > last_byte &&
e37c9e69 1407 start_found) {
db94535d
CM
1408 if (last_byte < search_start)
1409 last_byte = search_start;
87ee04eb
CM
1410 aligned = stripe_align(root, last_byte);
1411 hole_size = key.objectid - aligned;
1412 if (key.objectid > aligned && hole_size >= num_bytes) {
1413 ins->objectid = aligned;
e37c9e69
CM
1414 ins->offset = hole_size;
1415 goto check_pending;
0579da42 1416 }
fec577fb 1417 }
96b5179d 1418 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
7bb86316
CM
1419 if (!start_found && btrfs_key_type(&key) ==
1420 BTRFS_BLOCK_GROUP_ITEM_KEY) {
db94535d 1421 last_byte = key.objectid;
96b5179d
CM
1422 start_found = 1;
1423 }
e37c9e69 1424 goto next;
96b5179d
CM
1425 }
1426
e37c9e69 1427
0579da42 1428 start_found = 1;
db94535d 1429 last_byte = key.objectid + key.offset;
f510cfec 1430
257d0ce3
CM
1431 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1432 last_byte >= block_group->key.objectid +
be744175
CM
1433 block_group->key.offset) {
1434 btrfs_release_path(root, path);
1435 search_start = block_group->key.objectid +
e19caa5f 1436 block_group->key.offset;
be744175
CM
1437 goto new_group;
1438 }
9078a3e1 1439next:
5caf2a00 1440 path->slots[0]++;
de428b63 1441 cond_resched();
fec577fb 1442 }
fec577fb
CM
1443check_pending:
1444 /* we have to make sure we didn't find an extent that has already
1445 * been allocated by the map tree or the original allocation
1446 */
5caf2a00 1447 btrfs_release_path(root, path);
fec577fb 1448 BUG_ON(ins->objectid < search_start);
e37c9e69 1449
db94535d 1450 if (ins->objectid + num_bytes >= search_end)
cf67582b 1451 goto enospc;
257d0ce3 1452 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
5cf66426 1453 ins->objectid + num_bytes > block_group->
e19caa5f
CM
1454 key.objectid + block_group->key.offset) {
1455 search_start = block_group->key.objectid +
1456 block_group->key.offset;
1457 goto new_group;
1458 }
1a5bc167 1459 if (test_range_bit(&info->extent_ins, ins->objectid,
db94535d
CM
1460 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1461 search_start = ins->objectid + num_bytes;
1a5bc167
CM
1462 goto new_group;
1463 }
1464 if (test_range_bit(&info->pinned_extents, ins->objectid,
db94535d
CM
1465 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1466 search_start = ins->objectid + num_bytes;
1a5bc167 1467 goto new_group;
fec577fb 1468 }
db94535d 1469 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
f2654de4
CM
1470 ins->objectid < exclude_start + exclude_nr)) {
1471 search_start = exclude_start + exclude_nr;
1472 goto new_group;
1473 }
e37c9e69 1474 if (!data) {
5276aeda 1475 block_group = btrfs_lookup_block_group(info, ins->objectid);
26b8003f
CM
1476 if (block_group)
1477 trans->block_group = block_group;
f2458e1d 1478 }
db94535d 1479 ins->offset = num_bytes;
5caf2a00 1480 btrfs_free_path(path);
fec577fb 1481 return 0;
be744175
CM
1482
1483new_group:
db94535d 1484 if (search_start + num_bytes >= search_end) {
cf67582b 1485enospc:
be744175 1486 search_start = orig_search_start;
fbdc762b
CM
1487 if (full_scan) {
1488 ret = -ENOSPC;
1489 goto error;
1490 }
6702ed49
CM
1491 if (wrapped) {
1492 if (!full_scan)
1493 total_needed -= empty_size;
fbdc762b 1494 full_scan = 1;
1a2b2ac7 1495 data = BTRFS_BLOCK_GROUP_MIXED;
6702ed49 1496 } else
fbdc762b 1497 wrapped = 1;
be744175 1498 }
5276aeda 1499 block_group = btrfs_lookup_block_group(info, search_start);
fbdc762b 1500 cond_resched();
1a2b2ac7
CM
1501 block_group = btrfs_find_block_group(root, block_group,
1502 search_start, data, 0);
be744175
CM
1503 goto check_failed;
1504
0f70abe2 1505error:
5caf2a00
CM
1506 btrfs_release_path(root, path);
1507 btrfs_free_path(path);
0f70abe2 1508 return ret;
fec577fb 1509}
fec577fb
CM
1510/*
1511 * finds a free extent and does all the dirty work required for allocation
1512 * returns the key for the extent through ins, and a tree buffer for
1513 * the first block of the extent through buf.
1514 *
1515 * returns 0 if everything worked, non-zero otherwise.
1516 */
4d775673 1517int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
7bb86316
CM
1518 struct btrfs_root *root,
1519 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1520 u64 owner, u64 owner_offset,
1521 u64 empty_size, u64 hint_byte,
be08c1b9 1522 u64 search_end, struct btrfs_key *ins, int data)
fec577fb
CM
1523{
1524 int ret;
1525 int pending_ret;
db94535d 1526 u64 super_used, root_used;
fbdc762b 1527 u64 search_start = 0;
1261ec42
CM
1528 struct btrfs_fs_info *info = root->fs_info;
1529 struct btrfs_root *extent_root = info->extent_root;
234b63a0 1530 struct btrfs_extent_item extent_item;
7bb86316 1531 struct btrfs_path *path;
037e6390 1532
5f39d397 1533 btrfs_set_stack_extent_refs(&extent_item, 1);
fec577fb 1534
db94535d
CM
1535 WARN_ON(num_bytes < root->sectorsize);
1536 ret = find_free_extent(trans, root, num_bytes, empty_size,
1537 search_start, search_end, hint_byte, ins,
26b8003f
CM
1538 trans->alloc_exclude_start,
1539 trans->alloc_exclude_nr, data);
ccd467d6 1540 BUG_ON(ret);
f2654de4
CM
1541 if (ret)
1542 return ret;
fec577fb 1543
58176a96 1544 /* block accounting for super block */
db94535d
CM
1545 super_used = btrfs_super_bytes_used(&info->super_copy);
1546 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
26b8003f 1547
58176a96 1548 /* block accounting for root item */
db94535d
CM
1549 root_used = btrfs_root_used(&root->root_item);
1550 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
58176a96 1551
f510cfec
CM
1552 clear_extent_dirty(&root->fs_info->free_space_cache,
1553 ins->objectid, ins->objectid + ins->offset - 1,
1554 GFP_NOFS);
1555
26b8003f 1556 if (root == extent_root) {
1a5bc167
CM
1557 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1558 ins->objectid + ins->offset - 1,
1559 EXTENT_LOCKED, GFP_NOFS);
e19caa5f 1560 WARN_ON(data == 1);
26b8003f
CM
1561 goto update_block;
1562 }
1563
1564 WARN_ON(trans->alloc_exclude_nr);
1565 trans->alloc_exclude_start = ins->objectid;
1566 trans->alloc_exclude_nr = ins->offset;
e089f05c
CM
1567 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1568 sizeof(extent_item));
037e6390 1569
26b8003f
CM
1570 trans->alloc_exclude_start = 0;
1571 trans->alloc_exclude_nr = 0;
7bb86316
CM
1572 BUG_ON(ret);
1573
1574 path = btrfs_alloc_path();
1575 BUG_ON(!path);
1576 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1577 ins->objectid, root_objectid,
1578 ref_generation, owner, owner_offset);
26b8003f 1579
ccd467d6 1580 BUG_ON(ret);
7bb86316 1581 btrfs_free_path(path);
e089f05c 1582 finish_current_insert(trans, extent_root);
e20d96d6 1583 pending_ret = del_pending_extents(trans, extent_root);
f510cfec 1584
e37c9e69 1585 if (ret) {
037e6390 1586 return ret;
e37c9e69
CM
1587 }
1588 if (pending_ret) {
037e6390 1589 return pending_ret;
e37c9e69 1590 }
26b8003f
CM
1591
1592update_block:
1e2677e0
CM
1593 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1594 data);
fabb5681 1595 BUG_ON(ret);
037e6390 1596 return 0;
fec577fb
CM
1597}
1598
1599/*
1600 * helper function to allocate a block for a given tree
1601 * returns the tree buffer or NULL.
1602 */
5f39d397 1603struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
db94535d 1604 struct btrfs_root *root,
7bb86316
CM
1605 u32 blocksize,
1606 u64 root_objectid, u64 hint,
1607 u64 empty_size)
1608{
1609 u64 ref_generation;
1610
1611 if (root->ref_cows)
1612 ref_generation = trans->transid;
1613 else
1614 ref_generation = 0;
1615
1616
1617 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1618 ref_generation, 0, 0, hint, empty_size);
1619}
1620
1621/*
1622 * helper function to allocate a block for a given tree
1623 * returns the tree buffer or NULL.
1624 */
1625struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1626 struct btrfs_root *root,
1627 u32 blocksize,
1628 u64 root_objectid,
1629 u64 ref_generation,
1630 u64 first_objectid,
1631 int level,
1632 u64 hint,
5f39d397 1633 u64 empty_size)
fec577fb 1634{
e2fa7227 1635 struct btrfs_key ins;
fec577fb 1636 int ret;
5f39d397 1637 struct extent_buffer *buf;
fec577fb 1638
7bb86316
CM
1639 ret = btrfs_alloc_extent(trans, root, blocksize,
1640 root_objectid, ref_generation,
1641 first_objectid, level, empty_size, hint,
db94535d 1642 (u64)-1, &ins, 0);
fec577fb 1643 if (ret) {
54aa1f4d
CM
1644 BUG_ON(ret > 0);
1645 return ERR_PTR(ret);
fec577fb 1646 }
db94535d 1647 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
54aa1f4d 1648 if (!buf) {
7bb86316
CM
1649 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1650 root->root_key.objectid, ref_generation,
1651 0, 0, 0);
54aa1f4d
CM
1652 return ERR_PTR(-ENOMEM);
1653 }
5f39d397
CM
1654 btrfs_set_buffer_uptodate(buf);
1655 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1656 buf->start + buf->len - 1, GFP_NOFS);
19c00ddc
CM
1657 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1658 buf->start, buf->start + buf->len - 1,
1659 EXTENT_CSUM, GFP_NOFS);
1660 buf->flags |= EXTENT_CSUM;
6b80053d 1661 btrfs_set_buffer_defrag(buf);
d3c2fdcf 1662 trans->blocks_used++;
fec577fb
CM
1663 return buf;
1664}
a28ec197 1665
6407bf6d 1666static int drop_leaf_ref(struct btrfs_trans_handle *trans,
5f39d397 1667 struct btrfs_root *root, struct extent_buffer *leaf)
6407bf6d 1668{
7bb86316
CM
1669 u64 leaf_owner;
1670 u64 leaf_generation;
5f39d397 1671 struct btrfs_key key;
6407bf6d
CM
1672 struct btrfs_file_extent_item *fi;
1673 int i;
1674 int nritems;
1675 int ret;
1676
5f39d397
CM
1677 BUG_ON(!btrfs_is_leaf(leaf));
1678 nritems = btrfs_header_nritems(leaf);
7bb86316
CM
1679 leaf_owner = btrfs_header_owner(leaf);
1680 leaf_generation = btrfs_header_generation(leaf);
1681
6407bf6d 1682 for (i = 0; i < nritems; i++) {
db94535d 1683 u64 disk_bytenr;
5f39d397
CM
1684
1685 btrfs_item_key_to_cpu(leaf, &key, i);
1686 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d
CM
1687 continue;
1688 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5f39d397
CM
1689 if (btrfs_file_extent_type(leaf, fi) ==
1690 BTRFS_FILE_EXTENT_INLINE)
236454df 1691 continue;
6407bf6d
CM
1692 /*
1693 * FIXME make sure to insert a trans record that
1694 * repeats the snapshot del on crash
1695 */
db94535d
CM
1696 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1697 if (disk_bytenr == 0)
3a686375 1698 continue;
db94535d 1699 ret = btrfs_free_extent(trans, root, disk_bytenr,
7bb86316
CM
1700 btrfs_file_extent_disk_num_bytes(leaf, fi),
1701 leaf_owner, leaf_generation,
1702 key.objectid, key.offset, 0);
6407bf6d
CM
1703 BUG_ON(ret);
1704 }
1705 return 0;
1706}
1707
e011599b 1708static void reada_walk_down(struct btrfs_root *root,
5f39d397 1709 struct extent_buffer *node)
e011599b
CM
1710{
1711 int i;
1712 u32 nritems;
db94535d 1713 u64 bytenr;
e011599b
CM
1714 int ret;
1715 u32 refs;
db94535d
CM
1716 int level;
1717 u32 blocksize;
e011599b 1718
5f39d397 1719 nritems = btrfs_header_nritems(node);
db94535d 1720 level = btrfs_header_level(node);
e011599b 1721 for (i = 0; i < nritems; i++) {
db94535d
CM
1722 bytenr = btrfs_node_blockptr(node, i);
1723 blocksize = btrfs_level_size(root, level - 1);
1724 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
e011599b
CM
1725 BUG_ON(ret);
1726 if (refs != 1)
1727 continue;
409eb95d 1728 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1729 ret = readahead_tree_block(root, bytenr, blocksize);
409eb95d
CM
1730 cond_resched();
1731 mutex_lock(&root->fs_info->fs_mutex);
e011599b
CM
1732 if (ret)
1733 break;
1734 }
1735}
1736
9aca1d51
CM
1737/*
1738 * helper function for drop_snapshot, this walks down the tree dropping ref
1739 * counts as it goes.
1740 */
e089f05c
CM
1741static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1742 *root, struct btrfs_path *path, int *level)
20524f02 1743{
7bb86316
CM
1744 u64 root_owner;
1745 u64 root_gen;
1746 u64 bytenr;
5f39d397
CM
1747 struct extent_buffer *next;
1748 struct extent_buffer *cur;
7bb86316 1749 struct extent_buffer *parent;
db94535d 1750 u32 blocksize;
20524f02
CM
1751 int ret;
1752 u32 refs;
1753
5caf2a00
CM
1754 WARN_ON(*level < 0);
1755 WARN_ON(*level >= BTRFS_MAX_LEVEL);
5f39d397 1756 ret = lookup_extent_ref(trans, root,
db94535d
CM
1757 path->nodes[*level]->start,
1758 path->nodes[*level]->len, &refs);
20524f02
CM
1759 BUG_ON(ret);
1760 if (refs > 1)
1761 goto out;
e011599b 1762
9aca1d51
CM
1763 /*
1764 * walk down to the last node level and free all the leaves
1765 */
6407bf6d 1766 while(*level >= 0) {
5caf2a00
CM
1767 WARN_ON(*level < 0);
1768 WARN_ON(*level >= BTRFS_MAX_LEVEL);
20524f02 1769 cur = path->nodes[*level];
e011599b
CM
1770
1771 if (*level > 0 && path->slots[*level] == 0)
5f39d397 1772 reada_walk_down(root, cur);
e011599b 1773
5f39d397 1774 if (btrfs_header_level(cur) != *level)
2c90e5d6 1775 WARN_ON(1);
e011599b 1776
7518a238 1777 if (path->slots[*level] >=
5f39d397 1778 btrfs_header_nritems(cur))
20524f02 1779 break;
6407bf6d
CM
1780 if (*level == 0) {
1781 ret = drop_leaf_ref(trans, root, cur);
1782 BUG_ON(ret);
1783 break;
1784 }
db94535d
CM
1785 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1786 blocksize = btrfs_level_size(root, *level - 1);
1787 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
6407bf6d
CM
1788 BUG_ON(ret);
1789 if (refs != 1) {
7bb86316
CM
1790 parent = path->nodes[*level];
1791 root_owner = btrfs_header_owner(parent);
1792 root_gen = btrfs_header_generation(parent);
20524f02 1793 path->slots[*level]++;
db94535d 1794 ret = btrfs_free_extent(trans, root, bytenr,
7bb86316
CM
1795 blocksize, root_owner,
1796 root_gen, 0, 0, 1);
20524f02
CM
1797 BUG_ON(ret);
1798 continue;
1799 }
db94535d 1800 next = btrfs_find_tree_block(root, bytenr, blocksize);
5f39d397
CM
1801 if (!next || !btrfs_buffer_uptodate(next)) {
1802 free_extent_buffer(next);
e9d0b13b 1803 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1804 next = read_tree_block(root, bytenr, blocksize);
e9d0b13b
CM
1805 mutex_lock(&root->fs_info->fs_mutex);
1806
1807 /* we dropped the lock, check one more time */
db94535d
CM
1808 ret = lookup_extent_ref(trans, root, bytenr,
1809 blocksize, &refs);
e9d0b13b
CM
1810 BUG_ON(ret);
1811 if (refs != 1) {
7bb86316
CM
1812 parent = path->nodes[*level];
1813 root_owner = btrfs_header_owner(parent);
1814 root_gen = btrfs_header_generation(parent);
1815
e9d0b13b 1816 path->slots[*level]++;
5f39d397 1817 free_extent_buffer(next);
7bb86316
CM
1818 ret = btrfs_free_extent(trans, root, bytenr,
1819 blocksize,
1820 root_owner,
1821 root_gen, 0, 0, 1);
e9d0b13b
CM
1822 BUG_ON(ret);
1823 continue;
1824 }
1825 }
5caf2a00 1826 WARN_ON(*level <= 0);
83e15a28 1827 if (path->nodes[*level-1])
5f39d397 1828 free_extent_buffer(path->nodes[*level-1]);
20524f02 1829 path->nodes[*level-1] = next;
5f39d397 1830 *level = btrfs_header_level(next);
20524f02
CM
1831 path->slots[*level] = 0;
1832 }
1833out:
5caf2a00
CM
1834 WARN_ON(*level < 0);
1835 WARN_ON(*level >= BTRFS_MAX_LEVEL);
7bb86316
CM
1836
1837 if (path->nodes[*level] == root->node) {
1838 root_owner = root->root_key.objectid;
1839 parent = path->nodes[*level];
1840 } else {
1841 parent = path->nodes[*level + 1];
1842 root_owner = btrfs_header_owner(parent);
1843 }
1844
1845 root_gen = btrfs_header_generation(parent);
db94535d 1846 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
7bb86316
CM
1847 path->nodes[*level]->len,
1848 root_owner, root_gen, 0, 0, 1);
5f39d397 1849 free_extent_buffer(path->nodes[*level]);
20524f02
CM
1850 path->nodes[*level] = NULL;
1851 *level += 1;
1852 BUG_ON(ret);
1853 return 0;
1854}
1855
9aca1d51
CM
1856/*
1857 * helper for dropping snapshots. This walks back up the tree in the path
1858 * to find the first node higher up where we haven't yet gone through
1859 * all the slots
1860 */
e089f05c
CM
1861static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1862 *root, struct btrfs_path *path, int *level)
20524f02 1863{
7bb86316
CM
1864 u64 root_owner;
1865 u64 root_gen;
1866 struct btrfs_root_item *root_item = &root->root_item;
20524f02
CM
1867 int i;
1868 int slot;
1869 int ret;
9f3a7427 1870
234b63a0 1871 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
20524f02 1872 slot = path->slots[i];
5f39d397
CM
1873 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1874 struct extent_buffer *node;
1875 struct btrfs_disk_key disk_key;
1876 node = path->nodes[i];
20524f02
CM
1877 path->slots[i]++;
1878 *level = i;
9f3a7427 1879 WARN_ON(*level == 0);
5f39d397 1880 btrfs_node_key(node, &disk_key, path->slots[i]);
9f3a7427 1881 memcpy(&root_item->drop_progress,
5f39d397 1882 &disk_key, sizeof(disk_key));
9f3a7427 1883 root_item->drop_level = i;
20524f02
CM
1884 return 0;
1885 } else {
7bb86316
CM
1886 if (path->nodes[*level] == root->node) {
1887 root_owner = root->root_key.objectid;
1888 root_gen =
1889 btrfs_header_generation(path->nodes[*level]);
1890 } else {
1891 struct extent_buffer *node;
1892 node = path->nodes[*level + 1];
1893 root_owner = btrfs_header_owner(node);
1894 root_gen = btrfs_header_generation(node);
1895 }
e089f05c 1896 ret = btrfs_free_extent(trans, root,
db94535d 1897 path->nodes[*level]->start,
7bb86316
CM
1898 path->nodes[*level]->len,
1899 root_owner, root_gen, 0, 0, 1);
6407bf6d 1900 BUG_ON(ret);
5f39d397 1901 free_extent_buffer(path->nodes[*level]);
83e15a28 1902 path->nodes[*level] = NULL;
20524f02 1903 *level = i + 1;
20524f02
CM
1904 }
1905 }
1906 return 1;
1907}
1908
9aca1d51
CM
1909/*
1910 * drop the reference count on the tree rooted at 'snap'. This traverses
1911 * the tree freeing any blocks that have a ref count of zero after being
1912 * decremented.
1913 */
e089f05c 1914int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
9f3a7427 1915 *root)
20524f02 1916{
3768f368 1917 int ret = 0;
9aca1d51 1918 int wret;
20524f02 1919 int level;
5caf2a00 1920 struct btrfs_path *path;
20524f02
CM
1921 int i;
1922 int orig_level;
9f3a7427 1923 struct btrfs_root_item *root_item = &root->root_item;
20524f02 1924
5caf2a00
CM
1925 path = btrfs_alloc_path();
1926 BUG_ON(!path);
20524f02 1927
5f39d397 1928 level = btrfs_header_level(root->node);
20524f02 1929 orig_level = level;
9f3a7427
CM
1930 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1931 path->nodes[level] = root->node;
f510cfec 1932 extent_buffer_get(root->node);
9f3a7427
CM
1933 path->slots[level] = 0;
1934 } else {
1935 struct btrfs_key key;
5f39d397
CM
1936 struct btrfs_disk_key found_key;
1937 struct extent_buffer *node;
6702ed49 1938
9f3a7427 1939 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6702ed49
CM
1940 level = root_item->drop_level;
1941 path->lowest_level = level;
9f3a7427 1942 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6702ed49 1943 if (wret < 0) {
9f3a7427
CM
1944 ret = wret;
1945 goto out;
1946 }
5f39d397
CM
1947 node = path->nodes[level];
1948 btrfs_node_key(node, &found_key, path->slots[level]);
1949 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1950 sizeof(found_key)));
9f3a7427 1951 }
20524f02 1952 while(1) {
5caf2a00 1953 wret = walk_down_tree(trans, root, path, &level);
9aca1d51 1954 if (wret > 0)
20524f02 1955 break;
9aca1d51
CM
1956 if (wret < 0)
1957 ret = wret;
1958
5caf2a00 1959 wret = walk_up_tree(trans, root, path, &level);
9aca1d51 1960 if (wret > 0)
20524f02 1961 break;
9aca1d51
CM
1962 if (wret < 0)
1963 ret = wret;
409eb95d 1964 ret = -EAGAIN;
409eb95d 1965 break;
20524f02 1966 }
83e15a28 1967 for (i = 0; i <= orig_level; i++) {
5caf2a00 1968 if (path->nodes[i]) {
5f39d397 1969 free_extent_buffer(path->nodes[i]);
0f82731f 1970 path->nodes[i] = NULL;
83e15a28 1971 }
20524f02 1972 }
9f3a7427 1973out:
5caf2a00 1974 btrfs_free_path(path);
9aca1d51 1975 return ret;
20524f02 1976}
9078a3e1 1977
96b5179d 1978int btrfs_free_block_groups(struct btrfs_fs_info *info)
9078a3e1 1979{
96b5179d
CM
1980 u64 start;
1981 u64 end;
b97f9203 1982 u64 ptr;
9078a3e1 1983 int ret;
9078a3e1 1984 while(1) {
96b5179d
CM
1985 ret = find_first_extent_bit(&info->block_group_cache, 0,
1986 &start, &end, (unsigned int)-1);
1987 if (ret)
9078a3e1 1988 break;
b97f9203
Y
1989 ret = get_state_private(&info->block_group_cache, start, &ptr);
1990 if (!ret)
1991 kfree((void *)(unsigned long)ptr);
96b5179d
CM
1992 clear_extent_bits(&info->block_group_cache, start,
1993 end, (unsigned int)-1, GFP_NOFS);
9078a3e1 1994 }
e37c9e69 1995 while(1) {
f510cfec
CM
1996 ret = find_first_extent_bit(&info->free_space_cache, 0,
1997 &start, &end, EXTENT_DIRTY);
1998 if (ret)
e37c9e69 1999 break;
f510cfec
CM
2000 clear_extent_dirty(&info->free_space_cache, start,
2001 end, GFP_NOFS);
e37c9e69 2002 }
be744175
CM
2003 return 0;
2004}
2005
9078a3e1
CM
2006int btrfs_read_block_groups(struct btrfs_root *root)
2007{
2008 struct btrfs_path *path;
2009 int ret;
2010 int err = 0;
96b5179d 2011 int bit;
9078a3e1 2012 struct btrfs_block_group_cache *cache;
be744175 2013 struct btrfs_fs_info *info = root->fs_info;
96b5179d 2014 struct extent_map_tree *block_group_cache;
9078a3e1
CM
2015 struct btrfs_key key;
2016 struct btrfs_key found_key;
5f39d397 2017 struct extent_buffer *leaf;
96b5179d
CM
2018
2019 block_group_cache = &info->block_group_cache;
9078a3e1 2020
be744175 2021 root = info->extent_root;
9078a3e1 2022 key.objectid = 0;
db94535d 2023 key.offset = BTRFS_BLOCK_GROUP_SIZE;
9078a3e1
CM
2024 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2025
2026 path = btrfs_alloc_path();
2027 if (!path)
2028 return -ENOMEM;
2029
2030 while(1) {
be744175 2031 ret = btrfs_search_slot(NULL, info->extent_root,
9078a3e1
CM
2032 &key, path, 0, 0);
2033 if (ret != 0) {
2034 err = ret;
2035 break;
2036 }
5f39d397
CM
2037 leaf = path->nodes[0];
2038 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
9078a3e1
CM
2039 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2040 if (!cache) {
2041 err = -1;
2042 break;
2043 }
3e1ad54f 2044
5f39d397
CM
2045 read_extent_buffer(leaf, &cache->item,
2046 btrfs_item_ptr_offset(leaf, path->slots[0]),
2047 sizeof(cache->item));
9078a3e1 2048 memcpy(&cache->key, &found_key, sizeof(found_key));
e37c9e69 2049 cache->cached = 0;
324ae4df 2050 cache->pinned = 0;
9078a3e1
CM
2051 key.objectid = found_key.objectid + found_key.offset;
2052 btrfs_release_path(root, path);
5f39d397 2053
f84a8b36
CM
2054 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2055 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2056 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2057 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
96b5179d 2058 bit = BLOCK_GROUP_DATA;
f84a8b36 2059 cache->data = BTRFS_BLOCK_GROUP_DATA;
96b5179d
CM
2060 } else {
2061 bit = BLOCK_GROUP_METADATA;
2062 cache->data = 0;
31f3c99b 2063 }
96b5179d
CM
2064
2065 /* use EXTENT_LOCKED to prevent merging */
2066 set_extent_bits(block_group_cache, found_key.objectid,
2067 found_key.objectid + found_key.offset - 1,
2068 bit | EXTENT_LOCKED, GFP_NOFS);
2069 set_state_private(block_group_cache, found_key.objectid,
ae2f5411 2070 (unsigned long)cache);
96b5179d 2071
9078a3e1 2072 if (key.objectid >=
db94535d 2073 btrfs_super_total_bytes(&info->super_copy))
9078a3e1
CM
2074 break;
2075 }
2076
2077 btrfs_free_path(path);
2078 return 0;
2079}