Merge branch 'for-chris' of git://github.com/idryomov/btrfs-progs
[btrfs-progs-unstable/devel.git] / extent-tree.c
blob544ab2ff701e6844b23161b81922a26a4184c047
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "print-tree.h"
26 #include "transaction.h"
27 #include "crc32c.h"
28 #include "volumes.h"
30 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
36 #define PENDING_EXTENT_INSERT 0
37 #define PENDING_EXTENT_DELETE 1
38 #define PENDING_BACKREF_UPDATE 2
40 struct pending_extent_op {
41 int type;
42 u64 bytenr;
43 u64 num_bytes;
44 u64 flags;
45 struct btrfs_disk_key key;
46 int level;
49 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
50 struct btrfs_root *root,
51 u64 root_objectid, u64 generation,
52 u64 flags, struct btrfs_disk_key *key,
53 int level, struct btrfs_key *ins);
54 static int __free_extent(struct btrfs_trans_handle *trans,
55 struct btrfs_root *root,
56 u64 bytenr, u64 num_bytes, u64 parent,
57 u64 root_objectid, u64 owner_objectid,
58 u64 owner_offset, int refs_to_drop);
59 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
60 btrfs_root *extent_root);
61 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
62 btrfs_root *extent_root);
64 static int remove_sb_from_cache(struct btrfs_root *root,
65 struct btrfs_block_group_cache *cache)
67 u64 bytenr;
68 u64 *logical;
69 int stripe_len;
70 int i, nr, ret;
71 struct extent_io_tree *free_space_cache;
73 free_space_cache = &root->fs_info->free_space_cache;
74 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
75 bytenr = btrfs_sb_offset(i);
76 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
77 cache->key.objectid, bytenr, 0,
78 &logical, &nr, &stripe_len);
79 BUG_ON(ret);
80 while (nr--) {
81 clear_extent_dirty(free_space_cache, logical[nr],
82 logical[nr] + stripe_len - 1, GFP_NOFS);
84 kfree(logical);
86 return 0;
89 static int cache_block_group(struct btrfs_root *root,
90 struct btrfs_block_group_cache *block_group)
92 struct btrfs_path *path;
93 int ret;
94 struct btrfs_key key;
95 struct extent_buffer *leaf;
96 struct extent_io_tree *free_space_cache;
97 int slot;
98 u64 last;
99 u64 hole_size;
101 if (!block_group)
102 return 0;
104 root = root->fs_info->extent_root;
105 free_space_cache = &root->fs_info->free_space_cache;
107 if (block_group->cached)
108 return 0;
110 path = btrfs_alloc_path();
111 if (!path)
112 return -ENOMEM;
114 path->reada = 2;
115 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
116 key.objectid = last;
117 key.offset = 0;
118 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
120 if (ret < 0)
121 goto err;
123 while(1) {
124 leaf = path->nodes[0];
125 slot = path->slots[0];
126 if (slot >= btrfs_header_nritems(leaf)) {
127 ret = btrfs_next_leaf(root, path);
128 if (ret < 0)
129 goto err;
130 if (ret == 0) {
131 continue;
132 } else {
133 break;
136 btrfs_item_key_to_cpu(leaf, &key, slot);
137 if (key.objectid < block_group->key.objectid) {
138 goto next;
140 if (key.objectid >= block_group->key.objectid +
141 block_group->key.offset) {
142 break;
145 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
146 if (key.objectid > last) {
147 hole_size = key.objectid - last;
148 set_extent_dirty(free_space_cache, last,
149 last + hole_size - 1,
150 GFP_NOFS);
152 last = key.objectid + key.offset;
154 next:
155 path->slots[0]++;
158 if (block_group->key.objectid +
159 block_group->key.offset > last) {
160 hole_size = block_group->key.objectid +
161 block_group->key.offset - last;
162 set_extent_dirty(free_space_cache, last,
163 last + hole_size - 1, GFP_NOFS);
165 remove_sb_from_cache(root, block_group);
166 block_group->cached = 1;
167 err:
168 btrfs_free_path(path);
169 return 0;
172 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
173 btrfs_fs_info *info,
174 u64 bytenr)
176 struct extent_io_tree *block_group_cache;
177 struct btrfs_block_group_cache *block_group = NULL;
178 u64 ptr;
179 u64 start;
180 u64 end;
181 int ret;
183 bytenr = max_t(u64, bytenr,
184 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
185 block_group_cache = &info->block_group_cache;
186 ret = find_first_extent_bit(block_group_cache,
187 bytenr, &start, &end,
188 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
189 BLOCK_GROUP_SYSTEM);
190 if (ret) {
191 return NULL;
193 ret = get_state_private(block_group_cache, start, &ptr);
194 if (ret)
195 return NULL;
197 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
198 return block_group;
201 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
202 btrfs_fs_info *info,
203 u64 bytenr)
205 struct extent_io_tree *block_group_cache;
206 struct btrfs_block_group_cache *block_group = NULL;
207 u64 ptr;
208 u64 start;
209 u64 end;
210 int ret;
212 block_group_cache = &info->block_group_cache;
213 ret = find_first_extent_bit(block_group_cache,
214 bytenr, &start, &end,
215 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
216 BLOCK_GROUP_SYSTEM);
217 if (ret) {
218 return NULL;
220 ret = get_state_private(block_group_cache, start, &ptr);
221 if (ret)
222 return NULL;
224 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
225 if (block_group->key.objectid <= bytenr && bytenr <
226 block_group->key.objectid + block_group->key.offset)
227 return block_group;
228 return NULL;
231 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
233 return (cache->flags & bits) == bits;
236 static int noinline find_search_start(struct btrfs_root *root,
237 struct btrfs_block_group_cache **cache_ret,
238 u64 *start_ret, int num, int data)
240 int ret;
241 struct btrfs_block_group_cache *cache = *cache_ret;
242 u64 last;
243 u64 start = 0;
244 u64 end = 0;
245 u64 search_start = *start_ret;
246 int wrapped = 0;
248 if (!cache) {
249 goto out;
251 again:
252 ret = cache_block_group(root, cache);
253 if (ret)
254 goto out;
256 last = max(search_start, cache->key.objectid);
257 if (cache->ro || !block_group_bits(cache, data)) {
258 goto new_group;
261 while(1) {
262 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
263 last, &start, &end, EXTENT_DIRTY);
264 if (ret) {
265 goto new_group;
268 start = max(last, start);
269 last = end + 1;
270 if (last - start < num) {
271 continue;
273 if (start + num > cache->key.objectid + cache->key.offset) {
274 goto new_group;
276 *start_ret = start;
277 return 0;
279 out:
280 cache = btrfs_lookup_block_group(root->fs_info, search_start);
281 if (!cache) {
282 printk("Unable to find block group for %llu\n",
283 (unsigned long long)search_start);
284 WARN_ON(1);
286 return -ENOSPC;
288 new_group:
289 last = cache->key.objectid + cache->key.offset;
290 wrapped:
291 cache = btrfs_lookup_first_block_group(root->fs_info, last);
292 if (!cache) {
293 no_cache:
294 if (!wrapped) {
295 wrapped = 1;
296 last = search_start;
297 goto wrapped;
299 goto out;
301 cache = btrfs_find_block_group(root, cache, last, data, 0);
302 cache = btrfs_find_block_group(root, cache, last, data, 0);
303 if (!cache)
304 goto no_cache;
306 *cache_ret = cache;
307 goto again;
310 static u64 div_factor(u64 num, int factor)
312 if (factor == 10)
313 return num;
314 num *= factor;
315 num /= 10;
316 return num;
319 static int block_group_state_bits(u64 flags)
321 int bits = 0;
322 if (flags & BTRFS_BLOCK_GROUP_DATA)
323 bits |= BLOCK_GROUP_DATA;
324 if (flags & BTRFS_BLOCK_GROUP_METADATA)
325 bits |= BLOCK_GROUP_METADATA;
326 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
327 bits |= BLOCK_GROUP_SYSTEM;
328 return bits;
331 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
332 struct btrfs_block_group_cache
333 *hint, u64 search_start,
334 int data, int owner)
336 struct btrfs_block_group_cache *cache;
337 struct extent_io_tree *block_group_cache;
338 struct btrfs_block_group_cache *found_group = NULL;
339 struct btrfs_fs_info *info = root->fs_info;
340 u64 used;
341 u64 last = 0;
342 u64 hint_last;
343 u64 start;
344 u64 end;
345 u64 free_check;
346 u64 ptr;
347 int bit;
348 int ret;
349 int full_search = 0;
350 int factor = 10;
352 block_group_cache = &info->block_group_cache;
354 if (!owner)
355 factor = 10;
357 bit = block_group_state_bits(data);
359 if (search_start) {
360 struct btrfs_block_group_cache *shint;
361 shint = btrfs_lookup_block_group(info, search_start);
362 if (shint && !shint->ro && block_group_bits(shint, data)) {
363 used = btrfs_block_group_used(&shint->item);
364 if (used + shint->pinned <
365 div_factor(shint->key.offset, factor)) {
366 return shint;
370 if (hint && !hint->ro && block_group_bits(hint, data)) {
371 used = btrfs_block_group_used(&hint->item);
372 if (used + hint->pinned <
373 div_factor(hint->key.offset, factor)) {
374 return hint;
376 last = hint->key.objectid + hint->key.offset;
377 hint_last = last;
378 } else {
379 if (hint)
380 hint_last = max(hint->key.objectid, search_start);
381 else
382 hint_last = search_start;
384 last = hint_last;
386 again:
387 while(1) {
388 ret = find_first_extent_bit(block_group_cache, last,
389 &start, &end, bit);
390 if (ret)
391 break;
393 ret = get_state_private(block_group_cache, start, &ptr);
394 if (ret)
395 break;
397 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
398 last = cache->key.objectid + cache->key.offset;
399 used = btrfs_block_group_used(&cache->item);
401 if (!cache->ro && block_group_bits(cache, data)) {
402 if (full_search)
403 free_check = cache->key.offset;
404 else
405 free_check = div_factor(cache->key.offset,
406 factor);
408 if (used + cache->pinned < free_check) {
409 found_group = cache;
410 goto found;
413 cond_resched();
415 if (!full_search) {
416 last = search_start;
417 full_search = 1;
418 goto again;
420 found:
421 return found_group;
425 * Back reference rules. Back refs have three main goals:
427 * 1) differentiate between all holders of references to an extent so that
428 * when a reference is dropped we can make sure it was a valid reference
429 * before freeing the extent.
431 * 2) Provide enough information to quickly find the holders of an extent
432 * if we notice a given block is corrupted or bad.
434 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
435 * maintenance. This is actually the same as #2, but with a slightly
436 * different use case.
438 * There are two kinds of back refs. The implicit back refs is optimized
439 * for pointers in non-shared tree blocks. For a given pointer in a block,
440 * back refs of this kind provide information about the block's owner tree
441 * and the pointer's key. These information allow us to find the block by
442 * b-tree searching. The full back refs is for pointers in tree blocks not
443 * referenced by their owner trees. The location of tree block is recorded
444 * in the back refs. Actually the full back refs is generic, and can be
445 * used in all cases the implicit back refs is used. The major shortcoming
446 * of the full back refs is its overhead. Every time a tree block gets
447 * COWed, we have to update back refs entry for all pointers in it.
449 * For a newly allocated tree block, we use implicit back refs for
450 * pointers in it. This means most tree related operations only involve
451 * implicit back refs. For a tree block created in old transaction, the
452 * only way to drop a reference to it is COW it. So we can detect the
453 * event that tree block loses its owner tree's reference and do the
454 * back refs conversion.
456 * When a tree block is COW'd through a tree, there are four cases:
458 * The reference count of the block is one and the tree is the block's
459 * owner tree. Nothing to do in this case.
461 * The reference count of the block is one and the tree is not the
462 * block's owner tree. In this case, full back refs is used for pointers
463 * in the block. Remove these full back refs, add implicit back refs for
464 * every pointers in the new block.
466 * The reference count of the block is greater than one and the tree is
467 * the block's owner tree. In this case, implicit back refs is used for
468 * pointers in the block. Add full back refs for every pointers in the
469 * block, increase lower level extents' reference counts. The original
470 * implicit back refs are entailed to the new block.
472 * The reference count of the block is greater than one and the tree is
473 * not the block's owner tree. Add implicit back refs for every pointer in
474 * the new block, increase lower level extents' reference count.
476 * Back Reference Key composing:
478 * The key objectid corresponds to the first byte in the extent,
479 * The key type is used to differentiate between types of back refs.
480 * There are different meanings of the key offset for different types
481 * of back refs.
483 * File extents can be referenced by:
485 * - multiple snapshots, subvolumes, or different generations in one subvol
486 * - different files inside a single subvolume
487 * - different offsets inside a file (bookend extents in file.c)
489 * The extent ref structure for the implicit back refs has fields for:
491 * - Objectid of the subvolume root
492 * - objectid of the file holding the reference
493 * - original offset in the file
494 * - how many bookend extents
496 * The key offset for the implicit back refs is hash of the first
497 * three fields.
499 * The extent ref structure for the full back refs has field for:
501 * - number of pointers in the tree leaf
503 * The key offset for the implicit back refs is the first byte of
504 * the tree leaf
506 * When a file extent is allocated, The implicit back refs is used.
507 * the fields are filled in:
509 * (root_key.objectid, inode objectid, offset in file, 1)
511 * When a file extent is removed file truncation, we find the
512 * corresponding implicit back refs and check the following fields:
514 * (btrfs_header_owner(leaf), inode objectid, offset in file)
516 * Btree extents can be referenced by:
518 * - Different subvolumes
520 * Both the implicit back refs and the full back refs for tree blocks
521 * only consist of key. The key offset for the implicit back refs is
522 * objectid of block's owner tree. The key offset for the full back refs
523 * is the first byte of parent block.
525 * When implicit back refs is used, information about the lowest key and
526 * level of the tree block are required. These information are stored in
527 * tree block info structure.
530 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
531 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root,
533 struct btrfs_path *path,
534 u64 owner, u32 extra_size)
536 struct btrfs_extent_item *item;
537 struct btrfs_extent_item_v0 *ei0;
538 struct btrfs_extent_ref_v0 *ref0;
539 struct btrfs_tree_block_info *bi;
540 struct extent_buffer *leaf;
541 struct btrfs_key key;
542 struct btrfs_key found_key;
543 u32 new_size = sizeof(*item);
544 u64 refs;
545 int ret;
547 leaf = path->nodes[0];
548 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
550 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
551 ei0 = btrfs_item_ptr(leaf, path->slots[0],
552 struct btrfs_extent_item_v0);
553 refs = btrfs_extent_refs_v0(leaf, ei0);
555 if (owner == (u64)-1) {
556 while (1) {
557 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
558 ret = btrfs_next_leaf(root, path);
559 if (ret < 0)
560 return ret;
561 BUG_ON(ret > 0);
562 leaf = path->nodes[0];
564 btrfs_item_key_to_cpu(leaf, &found_key,
565 path->slots[0]);
566 BUG_ON(key.objectid != found_key.objectid);
567 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
568 path->slots[0]++;
569 continue;
571 ref0 = btrfs_item_ptr(leaf, path->slots[0],
572 struct btrfs_extent_ref_v0);
573 owner = btrfs_ref_objectid_v0(leaf, ref0);
574 break;
577 btrfs_release_path(root, path);
579 if (owner < BTRFS_FIRST_FREE_OBJECTID)
580 new_size += sizeof(*bi);
582 new_size -= sizeof(*ei0);
583 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
584 if (ret < 0)
585 return ret;
586 BUG_ON(ret);
588 ret = btrfs_extend_item(trans, root, path, new_size);
589 BUG_ON(ret);
591 leaf = path->nodes[0];
592 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
593 btrfs_set_extent_refs(leaf, item, refs);
594 /* FIXME: get real generation */
595 btrfs_set_extent_generation(leaf, item, 0);
596 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
597 btrfs_set_extent_flags(leaf, item,
598 BTRFS_EXTENT_FLAG_TREE_BLOCK |
599 BTRFS_BLOCK_FLAG_FULL_BACKREF);
600 bi = (struct btrfs_tree_block_info *)(item + 1);
601 /* FIXME: get first key of the block */
602 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
603 btrfs_set_tree_block_level(leaf, bi, (int)owner);
604 } else {
605 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
607 btrfs_mark_buffer_dirty(leaf);
608 return 0;
610 #endif
612 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
614 u32 high_crc = ~(u32)0;
615 u32 low_crc = ~(u32)0;
616 __le64 lenum;
618 lenum = cpu_to_le64(root_objectid);
619 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
620 lenum = cpu_to_le64(owner);
621 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
622 lenum = cpu_to_le64(offset);
623 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
625 return ((u64)high_crc << 31) ^ (u64)low_crc;
628 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
629 struct btrfs_extent_data_ref *ref)
631 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
632 btrfs_extent_data_ref_objectid(leaf, ref),
633 btrfs_extent_data_ref_offset(leaf, ref));
636 static int match_extent_data_ref(struct extent_buffer *leaf,
637 struct btrfs_extent_data_ref *ref,
638 u64 root_objectid, u64 owner, u64 offset)
640 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
641 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
642 btrfs_extent_data_ref_offset(leaf, ref) != offset)
643 return 0;
644 return 1;
647 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
648 struct btrfs_root *root,
649 struct btrfs_path *path,
650 u64 bytenr, u64 parent,
651 u64 root_objectid,
652 u64 owner, u64 offset)
654 struct btrfs_key key;
655 struct btrfs_extent_data_ref *ref;
656 struct extent_buffer *leaf;
657 u32 nritems;
658 int ret;
659 int recow;
660 int err = -ENOENT;
662 key.objectid = bytenr;
663 if (parent) {
664 key.type = BTRFS_SHARED_DATA_REF_KEY;
665 key.offset = parent;
666 } else {
667 key.type = BTRFS_EXTENT_DATA_REF_KEY;
668 key.offset = hash_extent_data_ref(root_objectid,
669 owner, offset);
671 again:
672 recow = 0;
673 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
674 if (ret < 0) {
675 err = ret;
676 goto fail;
679 if (parent) {
680 if (!ret)
681 return 0;
682 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
683 key.type = BTRFS_EXTENT_REF_V0_KEY;
684 btrfs_release_path(root, path);
685 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
686 if (ret < 0) {
687 err = ret;
688 goto fail;
690 if (!ret)
691 return 0;
692 #endif
693 goto fail;
696 leaf = path->nodes[0];
697 nritems = btrfs_header_nritems(leaf);
698 while (1) {
699 if (path->slots[0] >= nritems) {
700 ret = btrfs_next_leaf(root, path);
701 if (ret < 0)
702 err = ret;
703 if (ret)
704 goto fail;
706 leaf = path->nodes[0];
707 nritems = btrfs_header_nritems(leaf);
708 recow = 1;
711 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
712 if (key.objectid != bytenr ||
713 key.type != BTRFS_EXTENT_DATA_REF_KEY)
714 goto fail;
716 ref = btrfs_item_ptr(leaf, path->slots[0],
717 struct btrfs_extent_data_ref);
719 if (match_extent_data_ref(leaf, ref, root_objectid,
720 owner, offset)) {
721 if (recow) {
722 btrfs_release_path(root, path);
723 goto again;
725 err = 0;
726 break;
728 path->slots[0]++;
730 fail:
731 return err;
734 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
735 struct btrfs_root *root,
736 struct btrfs_path *path,
737 u64 bytenr, u64 parent,
738 u64 root_objectid, u64 owner,
739 u64 offset, int refs_to_add)
741 struct btrfs_key key;
742 struct extent_buffer *leaf;
743 u32 size;
744 u32 num_refs;
745 int ret;
747 key.objectid = bytenr;
748 if (parent) {
749 key.type = BTRFS_SHARED_DATA_REF_KEY;
750 key.offset = parent;
751 size = sizeof(struct btrfs_shared_data_ref);
752 } else {
753 key.type = BTRFS_EXTENT_DATA_REF_KEY;
754 key.offset = hash_extent_data_ref(root_objectid,
755 owner, offset);
756 size = sizeof(struct btrfs_extent_data_ref);
759 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
760 if (ret && ret != -EEXIST)
761 goto fail;
763 leaf = path->nodes[0];
764 if (parent) {
765 struct btrfs_shared_data_ref *ref;
766 ref = btrfs_item_ptr(leaf, path->slots[0],
767 struct btrfs_shared_data_ref);
768 if (ret == 0) {
769 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
770 } else {
771 num_refs = btrfs_shared_data_ref_count(leaf, ref);
772 num_refs += refs_to_add;
773 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
775 } else {
776 struct btrfs_extent_data_ref *ref;
777 while (ret == -EEXIST) {
778 ref = btrfs_item_ptr(leaf, path->slots[0],
779 struct btrfs_extent_data_ref);
780 if (match_extent_data_ref(leaf, ref, root_objectid,
781 owner, offset))
782 break;
783 btrfs_release_path(root, path);
785 key.offset++;
786 ret = btrfs_insert_empty_item(trans, root, path, &key,
787 size);
788 if (ret && ret != -EEXIST)
789 goto fail;
791 leaf = path->nodes[0];
793 ref = btrfs_item_ptr(leaf, path->slots[0],
794 struct btrfs_extent_data_ref);
795 if (ret == 0) {
796 btrfs_set_extent_data_ref_root(leaf, ref,
797 root_objectid);
798 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
799 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
800 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
801 } else {
802 num_refs = btrfs_extent_data_ref_count(leaf, ref);
803 num_refs += refs_to_add;
804 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
807 btrfs_mark_buffer_dirty(leaf);
808 ret = 0;
809 fail:
810 btrfs_release_path(root, path);
811 return ret;
814 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
815 struct btrfs_root *root,
816 struct btrfs_path *path,
817 int refs_to_drop)
819 struct btrfs_key key;
820 struct btrfs_extent_data_ref *ref1 = NULL;
821 struct btrfs_shared_data_ref *ref2 = NULL;
822 struct extent_buffer *leaf;
823 u32 num_refs = 0;
824 int ret = 0;
826 leaf = path->nodes[0];
827 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
829 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
830 ref1 = btrfs_item_ptr(leaf, path->slots[0],
831 struct btrfs_extent_data_ref);
832 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
833 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
834 ref2 = btrfs_item_ptr(leaf, path->slots[0],
835 struct btrfs_shared_data_ref);
836 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
837 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
838 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
839 struct btrfs_extent_ref_v0 *ref0;
840 ref0 = btrfs_item_ptr(leaf, path->slots[0],
841 struct btrfs_extent_ref_v0);
842 num_refs = btrfs_ref_count_v0(leaf, ref0);
843 #endif
844 } else {
845 BUG();
848 BUG_ON(num_refs < refs_to_drop);
849 num_refs -= refs_to_drop;
851 if (num_refs == 0) {
852 ret = btrfs_del_item(trans, root, path);
853 } else {
854 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
855 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
856 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
857 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
858 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
859 else {
860 struct btrfs_extent_ref_v0 *ref0;
861 ref0 = btrfs_item_ptr(leaf, path->slots[0],
862 struct btrfs_extent_ref_v0);
863 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
865 #endif
866 btrfs_mark_buffer_dirty(leaf);
868 return ret;
871 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
872 struct btrfs_path *path,
873 struct btrfs_extent_inline_ref *iref)
875 struct btrfs_key key;
876 struct extent_buffer *leaf;
877 struct btrfs_extent_data_ref *ref1;
878 struct btrfs_shared_data_ref *ref2;
879 u32 num_refs = 0;
881 leaf = path->nodes[0];
882 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
883 if (iref) {
884 if (btrfs_extent_inline_ref_type(leaf, iref) ==
885 BTRFS_EXTENT_DATA_REF_KEY) {
886 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
887 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
888 } else {
889 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
890 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
892 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
893 ref1 = btrfs_item_ptr(leaf, path->slots[0],
894 struct btrfs_extent_data_ref);
895 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
896 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
897 ref2 = btrfs_item_ptr(leaf, path->slots[0],
898 struct btrfs_shared_data_ref);
899 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
900 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
901 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
902 struct btrfs_extent_ref_v0 *ref0;
903 ref0 = btrfs_item_ptr(leaf, path->slots[0],
904 struct btrfs_extent_ref_v0);
905 num_refs = btrfs_ref_count_v0(leaf, ref0);
906 #endif
907 } else {
908 BUG();
910 return num_refs;
913 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
914 struct btrfs_root *root,
915 struct btrfs_path *path,
916 u64 bytenr, u64 parent,
917 u64 root_objectid)
919 struct btrfs_key key;
920 int ret;
922 key.objectid = bytenr;
923 if (parent) {
924 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
925 key.offset = parent;
926 } else {
927 key.type = BTRFS_TREE_BLOCK_REF_KEY;
928 key.offset = root_objectid;
931 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
932 if (ret > 0)
933 ret = -ENOENT;
934 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
935 if (ret == -ENOENT && parent) {
936 btrfs_release_path(root, path);
937 key.type = BTRFS_EXTENT_REF_V0_KEY;
938 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
939 if (ret > 0)
940 ret = -ENOENT;
942 #endif
943 return ret;
946 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
947 struct btrfs_root *root,
948 struct btrfs_path *path,
949 u64 bytenr, u64 parent,
950 u64 root_objectid)
952 struct btrfs_key key;
953 int ret;
955 key.objectid = bytenr;
956 if (parent) {
957 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
958 key.offset = parent;
959 } else {
960 key.type = BTRFS_TREE_BLOCK_REF_KEY;
961 key.offset = root_objectid;
964 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
966 btrfs_release_path(root, path);
967 return ret;
970 static inline int extent_ref_type(u64 parent, u64 owner)
972 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
973 if (parent > 0)
974 return BTRFS_SHARED_BLOCK_REF_KEY;
975 else
976 return BTRFS_TREE_BLOCK_REF_KEY;
977 } else {
978 if (parent > 0)
979 return BTRFS_SHARED_DATA_REF_KEY;
980 else
981 return BTRFS_EXTENT_DATA_REF_KEY;
985 static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
988 int level;
989 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
990 if (!path->nodes[level])
991 break;
992 if (path->slots[level] + 1 >=
993 btrfs_header_nritems(path->nodes[level]))
994 continue;
995 if (level == 0)
996 btrfs_item_key_to_cpu(path->nodes[level], key,
997 path->slots[level] + 1);
998 else
999 btrfs_node_key_to_cpu(path->nodes[level], key,
1000 path->slots[level] + 1);
1001 return 0;
1003 return 1;
1006 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1007 struct btrfs_root *root,
1008 struct btrfs_path *path,
1009 struct btrfs_extent_inline_ref **ref_ret,
1010 u64 bytenr, u64 num_bytes,
1011 u64 parent, u64 root_objectid,
1012 u64 owner, u64 offset, int insert)
1014 struct btrfs_key key;
1015 struct extent_buffer *leaf;
1016 struct btrfs_extent_item *ei;
1017 struct btrfs_extent_inline_ref *iref;
1018 u64 flags;
1019 u32 item_size;
1020 unsigned long ptr;
1021 unsigned long end;
1022 int extra_size;
1023 int type;
1024 int want;
1025 int ret;
1026 int err = 0;
1028 key.objectid = bytenr;
1029 key.type = BTRFS_EXTENT_ITEM_KEY;
1030 key.offset = num_bytes;
1032 want = extent_ref_type(parent, owner);
1033 if (insert)
1034 extra_size = btrfs_extent_inline_ref_size(want);
1035 else
1036 extra_size = -1;
1037 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1038 if (ret < 0) {
1039 err = ret;
1040 goto out;
1042 if (ret) {
1043 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1044 return -ENOENT;
1047 BUG_ON(ret);
1049 leaf = path->nodes[0];
1050 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1051 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1052 if (item_size < sizeof(*ei)) {
1053 if (!insert) {
1054 err = -ENOENT;
1055 goto out;
1057 ret = convert_extent_item_v0(trans, root, path, owner,
1058 extra_size);
1059 if (ret < 0) {
1060 err = ret;
1061 goto out;
1063 leaf = path->nodes[0];
1064 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1066 #endif
1067 if (item_size < sizeof(*ei)) {
1068 printf("Size is %u, needs to be %u, slot %d\n",
1069 (unsigned)item_size,
1070 (unsigned)sizeof(*ei), path->slots[0]);
1071 btrfs_print_leaf(root, leaf);
1072 return -EINVAL;
1074 BUG_ON(item_size < sizeof(*ei));
1076 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1077 flags = btrfs_extent_flags(leaf, ei);
1079 ptr = (unsigned long)(ei + 1);
1080 end = (unsigned long)ei + item_size;
1082 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1083 ptr += sizeof(struct btrfs_tree_block_info);
1084 BUG_ON(ptr > end);
1085 } else {
1086 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1087 return -EIO;
1091 err = -ENOENT;
1092 while (1) {
1093 if (ptr >= end) {
1094 WARN_ON(ptr > end);
1095 break;
1097 iref = (struct btrfs_extent_inline_ref *)ptr;
1098 type = btrfs_extent_inline_ref_type(leaf, iref);
1099 if (want < type)
1100 break;
1101 if (want > type) {
1102 ptr += btrfs_extent_inline_ref_size(type);
1103 continue;
1106 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1107 struct btrfs_extent_data_ref *dref;
1108 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1109 if (match_extent_data_ref(leaf, dref, root_objectid,
1110 owner, offset)) {
1111 err = 0;
1112 break;
1114 if (hash_extent_data_ref_item(leaf, dref) <
1115 hash_extent_data_ref(root_objectid, owner, offset))
1116 break;
1117 } else {
1118 u64 ref_offset;
1119 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1120 if (parent > 0) {
1121 if (parent == ref_offset) {
1122 err = 0;
1123 break;
1125 if (ref_offset < parent)
1126 break;
1127 } else {
1128 if (root_objectid == ref_offset) {
1129 err = 0;
1130 break;
1132 if (ref_offset < root_objectid)
1133 break;
1136 ptr += btrfs_extent_inline_ref_size(type);
1138 if (err == -ENOENT && insert) {
1139 if (item_size + extra_size >=
1140 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1141 err = -EAGAIN;
1142 goto out;
1145 * To add new inline back ref, we have to make sure
1146 * there is no corresponding back ref item.
1147 * For simplicity, we just do not add new inline back
1148 * ref if there is any back ref item.
1150 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1151 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1152 err = -EAGAIN;
1153 goto out;
1156 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1157 out:
1158 return err;
1161 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1162 struct btrfs_root *root,
1163 struct btrfs_path *path,
1164 struct btrfs_extent_inline_ref *iref,
1165 u64 parent, u64 root_objectid,
1166 u64 owner, u64 offset, int refs_to_add)
1168 struct extent_buffer *leaf;
1169 struct btrfs_extent_item *ei;
1170 unsigned long ptr;
1171 unsigned long end;
1172 unsigned long item_offset;
1173 u64 refs;
1174 int size;
1175 int type;
1176 int ret;
1178 leaf = path->nodes[0];
1179 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1180 item_offset = (unsigned long)iref - (unsigned long)ei;
1182 type = extent_ref_type(parent, owner);
1183 size = btrfs_extent_inline_ref_size(type);
1185 ret = btrfs_extend_item(trans, root, path, size);
1186 BUG_ON(ret);
1188 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1189 refs = btrfs_extent_refs(leaf, ei);
1190 refs += refs_to_add;
1191 btrfs_set_extent_refs(leaf, ei, refs);
1193 ptr = (unsigned long)ei + item_offset;
1194 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1195 if (ptr < end - size)
1196 memmove_extent_buffer(leaf, ptr + size, ptr,
1197 end - size - ptr);
1199 iref = (struct btrfs_extent_inline_ref *)ptr;
1200 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1201 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1202 struct btrfs_extent_data_ref *dref;
1203 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1204 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1205 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1206 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1207 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1208 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1209 struct btrfs_shared_data_ref *sref;
1210 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1211 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1212 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1213 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1214 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1215 } else {
1216 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1218 btrfs_mark_buffer_dirty(leaf);
1219 return 0;
1222 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1223 struct btrfs_root *root,
1224 struct btrfs_path *path,
1225 struct btrfs_extent_inline_ref **ref_ret,
1226 u64 bytenr, u64 num_bytes, u64 parent,
1227 u64 root_objectid, u64 owner, u64 offset)
1229 int ret;
1231 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1232 bytenr, num_bytes, parent,
1233 root_objectid, owner, offset, 0);
1234 if (ret != -ENOENT)
1235 return ret;
1237 btrfs_release_path(root, path);
1238 *ref_ret = NULL;
1240 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1241 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1242 root_objectid);
1243 } else {
1244 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1245 root_objectid, owner, offset);
1247 return ret;
1250 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1251 struct btrfs_root *root,
1252 struct btrfs_path *path,
1253 struct btrfs_extent_inline_ref *iref,
1254 int refs_to_mod)
1256 struct extent_buffer *leaf;
1257 struct btrfs_extent_item *ei;
1258 struct btrfs_extent_data_ref *dref = NULL;
1259 struct btrfs_shared_data_ref *sref = NULL;
1260 unsigned long ptr;
1261 unsigned long end;
1262 u32 item_size;
1263 int size;
1264 int type;
1265 int ret;
1266 u64 refs;
1268 leaf = path->nodes[0];
1269 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1270 refs = btrfs_extent_refs(leaf, ei);
1271 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1272 refs += refs_to_mod;
1273 btrfs_set_extent_refs(leaf, ei, refs);
1275 type = btrfs_extent_inline_ref_type(leaf, iref);
1277 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1278 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1279 refs = btrfs_extent_data_ref_count(leaf, dref);
1280 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1281 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1282 refs = btrfs_shared_data_ref_count(leaf, sref);
1283 } else {
1284 refs = 1;
1285 BUG_ON(refs_to_mod != -1);
1288 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1289 refs += refs_to_mod;
1291 if (refs > 0) {
1292 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1293 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1294 else
1295 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1296 } else {
1297 size = btrfs_extent_inline_ref_size(type);
1298 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1299 ptr = (unsigned long)iref;
1300 end = (unsigned long)ei + item_size;
1301 if (ptr + size < end)
1302 memmove_extent_buffer(leaf, ptr, ptr + size,
1303 end - ptr - size);
1304 item_size -= size;
1305 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1306 BUG_ON(ret);
1308 btrfs_mark_buffer_dirty(leaf);
1309 return 0;
1312 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1313 struct btrfs_root *root,
1314 struct btrfs_path *path,
1315 u64 bytenr, u64 num_bytes, u64 parent,
1316 u64 root_objectid, u64 owner,
1317 u64 offset, int refs_to_add)
1319 struct btrfs_extent_inline_ref *iref;
1320 int ret;
1322 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1323 bytenr, num_bytes, parent,
1324 root_objectid, owner, offset, 1);
1325 if (ret == 0) {
1326 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1327 ret = update_inline_extent_backref(trans, root, path, iref,
1328 refs_to_add);
1329 } else if (ret == -ENOENT) {
1330 ret = setup_inline_extent_backref(trans, root, path, iref,
1331 parent, root_objectid,
1332 owner, offset, refs_to_add);
1334 return ret;
1337 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1338 struct btrfs_root *root,
1339 struct btrfs_path *path,
1340 u64 bytenr, u64 parent, u64 root_objectid,
1341 u64 owner, u64 offset, int refs_to_add)
1343 int ret;
1345 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1346 ret = insert_extent_data_ref(trans, root, path, bytenr,
1347 parent, root_objectid,
1348 owner, offset, refs_to_add);
1349 } else {
1350 BUG_ON(refs_to_add != 1);
1351 ret = insert_tree_block_ref(trans, root, path, bytenr,
1352 parent, root_objectid);
1354 return ret;
1357 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1358 struct btrfs_root *root,
1359 struct btrfs_path *path,
1360 struct btrfs_extent_inline_ref *iref,
1361 int refs_to_drop, int is_data)
1363 int ret;
1365 BUG_ON(!is_data && refs_to_drop != 1);
1366 if (iref) {
1367 ret = update_inline_extent_backref(trans, root, path, iref,
1368 -refs_to_drop);
1369 } else if (is_data) {
1370 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1371 } else {
1372 ret = btrfs_del_item(trans, root, path);
1374 return ret;
1377 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1378 struct btrfs_root *root,
1379 u64 bytenr, u64 num_bytes, u64 parent,
1380 u64 root_objectid, u64 owner, u64 offset)
1382 struct btrfs_path *path;
1383 struct extent_buffer *leaf;
1384 struct btrfs_extent_item *item;
1385 u64 refs;
1386 int ret;
1387 int err = 0;
1389 path = btrfs_alloc_path();
1390 if (!path)
1391 return -ENOMEM;
1393 path->reada = 1;
1394 path->leave_spinning = 1;
1396 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1397 path, bytenr, num_bytes, parent,
1398 root_objectid, owner, offset, 1);
1399 if (ret == 0)
1400 goto out;
1402 if (ret != -EAGAIN) {
1403 err = ret;
1404 goto out;
1407 leaf = path->nodes[0];
1408 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1409 refs = btrfs_extent_refs(leaf, item);
1410 btrfs_set_extent_refs(leaf, item, refs + 1);
1412 btrfs_mark_buffer_dirty(leaf);
1413 btrfs_release_path(root->fs_info->extent_root, path);
1415 path->reada = 1;
1416 path->leave_spinning = 1;
1418 /* now insert the actual backref */
1419 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1420 path, bytenr, parent, root_objectid,
1421 owner, offset, 1);
1422 if (ret)
1423 err = ret;
1424 out:
1425 btrfs_free_path(path);
1426 finish_current_insert(trans, root->fs_info->extent_root);
1427 del_pending_extents(trans, root->fs_info->extent_root);
1428 BUG_ON(err);
1429 return err;
1432 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1433 struct btrfs_root *root)
1435 finish_current_insert(trans, root->fs_info->extent_root);
1436 del_pending_extents(trans, root->fs_info->extent_root);
1437 return 0;
1440 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1441 struct btrfs_root *root, u64 bytenr,
1442 u64 num_bytes, u64 *refs, u64 *flags)
1444 struct btrfs_path *path;
1445 int ret;
1446 struct btrfs_key key;
1447 struct extent_buffer *l;
1448 struct btrfs_extent_item *item;
1449 u32 item_size;
1450 u64 num_refs;
1451 u64 extent_flags;
1453 WARN_ON(num_bytes < root->sectorsize);
1454 path = btrfs_alloc_path();
1455 path->reada = 1;
1456 key.objectid = bytenr;
1457 key.offset = num_bytes;
1458 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1459 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1460 0, 0);
1461 if (ret < 0)
1462 goto out;
1463 if (ret != 0) {
1464 ret = -EIO;
1465 goto out;
1468 l = path->nodes[0];
1469 item_size = btrfs_item_size_nr(l, path->slots[0]);
1470 if (item_size >= sizeof(*item)) {
1471 item = btrfs_item_ptr(l, path->slots[0],
1472 struct btrfs_extent_item);
1473 num_refs = btrfs_extent_refs(l, item);
1474 extent_flags = btrfs_extent_flags(l, item);
1475 } else {
1476 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1477 struct btrfs_extent_item_v0 *ei0;
1478 BUG_ON(item_size != sizeof(*ei0));
1479 ei0 = btrfs_item_ptr(l, path->slots[0],
1480 struct btrfs_extent_item_v0);
1481 num_refs = btrfs_extent_refs_v0(l, ei0);
1482 /* FIXME: this isn't correct for data */
1483 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1484 #else
1485 BUG();
1486 #endif
1488 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1489 if (refs)
1490 *refs = num_refs;
1491 if (flags)
1492 *flags = extent_flags;
1493 out:
1494 btrfs_free_path(path);
1495 return 0;
1498 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1499 struct btrfs_root *root,
1500 u64 bytenr, u64 num_bytes, u64 flags)
1502 struct btrfs_path *path;
1503 int ret;
1504 struct btrfs_key key;
1505 struct extent_buffer *l;
1506 struct btrfs_extent_item *item;
1507 u32 item_size;
1509 WARN_ON(num_bytes < root->sectorsize);
1510 path = btrfs_alloc_path();
1511 path->reada = 1;
1512 key.objectid = bytenr;
1513 key.offset = num_bytes;
1514 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1515 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1516 0, 0);
1517 if (ret < 0)
1518 goto out;
1519 if (ret != 0) {
1520 btrfs_print_leaf(root, path->nodes[0]);
1521 printk("failed to find block number %Lu\n",
1522 (unsigned long long)bytenr);
1523 BUG();
1525 l = path->nodes[0];
1526 item_size = btrfs_item_size_nr(l, path->slots[0]);
1527 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1528 if (item_size < sizeof(*item)) {
1529 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1530 path, (u64)-1, 0);
1531 if (ret < 0)
1532 goto out;
1534 l = path->nodes[0];
1535 item_size = btrfs_item_size_nr(l, path->slots[0]);
1537 #endif
1538 BUG_ON(item_size < sizeof(*item));
1539 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1540 flags |= btrfs_extent_flags(l, item);
1541 btrfs_set_extent_flags(l, item, flags);
1542 out:
1543 btrfs_free_path(path);
1544 finish_current_insert(trans, root->fs_info->extent_root);
1545 del_pending_extents(trans, root->fs_info->extent_root);
1546 return ret;
1549 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1550 struct btrfs_root *root,
1551 struct extent_buffer *buf,
1552 int record_parent, int inc)
1554 u64 bytenr;
1555 u64 num_bytes;
1556 u64 parent;
1557 u64 ref_root;
1558 u32 nritems;
1559 struct btrfs_key key;
1560 struct btrfs_file_extent_item *fi;
1561 int i;
1562 int level;
1563 int ret = 0;
1564 int (*process_func)(struct btrfs_trans_handle *trans,
1565 struct btrfs_root *root,
1566 u64, u64, u64, u64, u64, u64);
1568 ref_root = btrfs_header_owner(buf);
1569 nritems = btrfs_header_nritems(buf);
1570 level = btrfs_header_level(buf);
1572 if (!root->ref_cows && level == 0)
1573 return 0;
1575 if (inc)
1576 process_func = btrfs_inc_extent_ref;
1577 else
1578 process_func = btrfs_free_extent;
1580 if (record_parent)
1581 parent = buf->start;
1582 else
1583 parent = 0;
1585 for (i = 0; i < nritems; i++) {
1586 cond_resched();
1587 if (level == 0) {
1588 btrfs_item_key_to_cpu(buf, &key, i);
1589 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1590 continue;
1591 fi = btrfs_item_ptr(buf, i,
1592 struct btrfs_file_extent_item);
1593 if (btrfs_file_extent_type(buf, fi) ==
1594 BTRFS_FILE_EXTENT_INLINE)
1595 continue;
1596 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1597 if (bytenr == 0)
1598 continue;
1600 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1601 key.offset -= btrfs_file_extent_offset(buf, fi);
1602 ret = process_func(trans, root, bytenr, num_bytes,
1603 parent, ref_root, key.objectid,
1604 key.offset);
1605 if (ret) {
1606 WARN_ON(1);
1607 goto fail;
1609 } else {
1610 bytenr = btrfs_node_blockptr(buf, i);
1611 num_bytes = btrfs_level_size(root, level - 1);
1612 ret = process_func(trans, root, bytenr, num_bytes,
1613 parent, ref_root, level - 1, 0);
1614 if (ret) {
1615 WARN_ON(1);
1616 goto fail;
1620 return 0;
1621 fail:
1622 WARN_ON(1);
1623 return ret;
1626 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1627 struct extent_buffer *buf, int record_parent)
1629 return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1632 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1633 struct extent_buffer *buf, int record_parent)
1635 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1638 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1639 struct btrfs_root *root,
1640 struct btrfs_path *path,
1641 struct btrfs_block_group_cache *cache)
1643 int ret;
1644 int pending_ret;
1645 struct btrfs_root *extent_root = root->fs_info->extent_root;
1646 unsigned long bi;
1647 struct extent_buffer *leaf;
1649 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1650 if (ret < 0)
1651 goto fail;
1652 BUG_ON(ret);
1654 leaf = path->nodes[0];
1655 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1656 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1657 btrfs_mark_buffer_dirty(leaf);
1658 btrfs_release_path(extent_root, path);
1659 fail:
1660 finish_current_insert(trans, extent_root);
1661 pending_ret = del_pending_extents(trans, extent_root);
1662 if (ret)
1663 return ret;
1664 if (pending_ret)
1665 return pending_ret;
1666 return 0;
1670 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1671 struct btrfs_root *root)
1673 struct extent_io_tree *block_group_cache;
1674 struct btrfs_block_group_cache *cache;
1675 int ret;
1676 struct btrfs_path *path;
1677 u64 last = 0;
1678 u64 start;
1679 u64 end;
1680 u64 ptr;
1682 block_group_cache = &root->fs_info->block_group_cache;
1683 path = btrfs_alloc_path();
1684 if (!path)
1685 return -ENOMEM;
1687 while(1) {
1688 ret = find_first_extent_bit(block_group_cache, last,
1689 &start, &end, BLOCK_GROUP_DIRTY);
1690 if (ret) {
1691 if (last == 0)
1692 break;
1693 last = 0;
1694 continue;
1697 last = end + 1;
1698 ret = get_state_private(block_group_cache, start, &ptr);
1699 BUG_ON(ret);
1701 clear_extent_bits(block_group_cache, start, end,
1702 BLOCK_GROUP_DIRTY, GFP_NOFS);
1704 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1705 ret = write_one_cache_group(trans, root, path, cache);
1706 BUG_ON(ret);
1708 btrfs_free_path(path);
1709 return 0;
1712 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1713 u64 flags)
1715 struct list_head *head = &info->space_info;
1716 struct list_head *cur;
1717 struct btrfs_space_info *found;
1718 list_for_each(cur, head) {
1719 found = list_entry(cur, struct btrfs_space_info, list);
1720 if (found->flags & flags)
1721 return found;
1723 return NULL;
1727 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1728 u64 total_bytes, u64 bytes_used,
1729 struct btrfs_space_info **space_info)
1731 struct btrfs_space_info *found;
1733 found = __find_space_info(info, flags);
1734 if (found) {
1735 found->total_bytes += total_bytes;
1736 found->bytes_used += bytes_used;
1737 if (found->total_bytes < found->bytes_used) {
1738 fprintf(stderr, "warning, bad space info total_bytes "
1739 "%llu used %llu\n",
1740 (unsigned long long)found->total_bytes,
1741 (unsigned long long)found->bytes_used);
1743 *space_info = found;
1744 return 0;
1746 found = kmalloc(sizeof(*found), GFP_NOFS);
1747 if (!found)
1748 return -ENOMEM;
1750 list_add(&found->list, &info->space_info);
1751 found->flags = flags;
1752 found->total_bytes = total_bytes;
1753 found->bytes_used = bytes_used;
1754 found->bytes_pinned = 0;
1755 found->full = 0;
1756 *space_info = found;
1757 return 0;
1761 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1763 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1764 BTRFS_BLOCK_GROUP_RAID1 |
1765 BTRFS_BLOCK_GROUP_RAID10 |
1766 BTRFS_BLOCK_GROUP_DUP);
1767 if (extra_flags) {
1768 if (flags & BTRFS_BLOCK_GROUP_DATA)
1769 fs_info->avail_data_alloc_bits |= extra_flags;
1770 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1771 fs_info->avail_metadata_alloc_bits |= extra_flags;
1772 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1773 fs_info->avail_system_alloc_bits |= extra_flags;
1777 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1778 struct btrfs_root *extent_root, u64 alloc_bytes,
1779 u64 flags)
1781 struct btrfs_space_info *space_info;
1782 u64 thresh;
1783 u64 start;
1784 u64 num_bytes;
1785 int ret;
1787 space_info = __find_space_info(extent_root->fs_info, flags);
1788 if (!space_info) {
1789 ret = update_space_info(extent_root->fs_info, flags,
1790 0, 0, &space_info);
1791 BUG_ON(ret);
1793 BUG_ON(!space_info);
1795 if (space_info->full)
1796 return 0;
1798 thresh = div_factor(space_info->total_bytes, 7);
1799 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1800 thresh)
1801 return 0;
1803 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1804 space_info->flags);
1805 if (ret == -ENOSPC) {
1806 space_info->full = 1;
1807 return 0;
1810 BUG_ON(ret);
1812 ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1813 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1814 BUG_ON(ret);
1815 return 0;
1818 static int update_block_group(struct btrfs_trans_handle *trans,
1819 struct btrfs_root *root,
1820 u64 bytenr, u64 num_bytes, int alloc,
1821 int mark_free)
1823 struct btrfs_block_group_cache *cache;
1824 struct btrfs_fs_info *info = root->fs_info;
1825 u64 total = num_bytes;
1826 u64 old_val;
1827 u64 byte_in_group;
1828 u64 start;
1829 u64 end;
1831 /* block accounting for super block */
1832 old_val = btrfs_super_bytes_used(&info->super_copy);
1833 if (alloc)
1834 old_val += num_bytes;
1835 else
1836 old_val -= num_bytes;
1837 btrfs_set_super_bytes_used(&info->super_copy, old_val);
1839 /* block accounting for root item */
1840 old_val = btrfs_root_used(&root->root_item);
1841 if (alloc)
1842 old_val += num_bytes;
1843 else
1844 old_val -= num_bytes;
1845 btrfs_set_root_used(&root->root_item, old_val);
1847 while(total) {
1848 cache = btrfs_lookup_block_group(info, bytenr);
1849 if (!cache) {
1850 return -1;
1852 byte_in_group = bytenr - cache->key.objectid;
1853 WARN_ON(byte_in_group > cache->key.offset);
1854 start = cache->key.objectid;
1855 end = start + cache->key.offset - 1;
1856 set_extent_bits(&info->block_group_cache, start, end,
1857 BLOCK_GROUP_DIRTY, GFP_NOFS);
1859 old_val = btrfs_block_group_used(&cache->item);
1860 num_bytes = min(total, cache->key.offset - byte_in_group);
1862 if (alloc) {
1863 old_val += num_bytes;
1864 cache->space_info->bytes_used += num_bytes;
1865 } else {
1866 old_val -= num_bytes;
1867 cache->space_info->bytes_used -= num_bytes;
1868 if (mark_free) {
1869 set_extent_dirty(&info->free_space_cache,
1870 bytenr, bytenr + num_bytes - 1,
1871 GFP_NOFS);
1874 btrfs_set_block_group_used(&cache->item, old_val);
1875 total -= num_bytes;
1876 bytenr += num_bytes;
1878 return 0;
1881 static int update_pinned_extents(struct btrfs_root *root,
1882 u64 bytenr, u64 num, int pin)
1884 u64 len;
1885 struct btrfs_block_group_cache *cache;
1886 struct btrfs_fs_info *fs_info = root->fs_info;
1888 if (pin) {
1889 set_extent_dirty(&fs_info->pinned_extents,
1890 bytenr, bytenr + num - 1, GFP_NOFS);
1891 } else {
1892 clear_extent_dirty(&fs_info->pinned_extents,
1893 bytenr, bytenr + num - 1, GFP_NOFS);
1895 while (num > 0) {
1896 cache = btrfs_lookup_block_group(fs_info, bytenr);
1897 WARN_ON(!cache);
1898 len = min(num, cache->key.offset -
1899 (bytenr - cache->key.objectid));
1900 if (pin) {
1901 cache->pinned += len;
1902 cache->space_info->bytes_pinned += len;
1903 fs_info->total_pinned += len;
1904 } else {
1905 cache->pinned -= len;
1906 cache->space_info->bytes_pinned -= len;
1907 fs_info->total_pinned -= len;
1909 bytenr += len;
1910 num -= len;
1912 return 0;
1915 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1917 u64 last = 0;
1918 u64 start;
1919 u64 end;
1920 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1921 int ret;
1923 while(1) {
1924 ret = find_first_extent_bit(pinned_extents, last,
1925 &start, &end, EXTENT_DIRTY);
1926 if (ret)
1927 break;
1928 set_extent_dirty(copy, start, end, GFP_NOFS);
1929 last = end + 1;
1931 return 0;
1934 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1935 struct btrfs_root *root,
1936 struct extent_io_tree *unpin)
1938 u64 start;
1939 u64 end;
1940 int ret;
1941 struct extent_io_tree *free_space_cache;
1942 free_space_cache = &root->fs_info->free_space_cache;
1944 while(1) {
1945 ret = find_first_extent_bit(unpin, 0, &start, &end,
1946 EXTENT_DIRTY);
1947 if (ret)
1948 break;
1949 update_pinned_extents(root, start, end + 1 - start, 0);
1950 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1951 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1953 return 0;
1956 static int finish_current_insert(struct btrfs_trans_handle *trans,
1957 struct btrfs_root *extent_root)
1959 u64 start;
1960 u64 end;
1961 u64 priv;
1962 struct btrfs_fs_info *info = extent_root->fs_info;
1963 struct btrfs_path *path;
1964 struct pending_extent_op *extent_op;
1965 struct btrfs_key key;
1966 int ret;
1968 path = btrfs_alloc_path();
1970 while(1) {
1971 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1972 &end, EXTENT_LOCKED);
1973 if (ret)
1974 break;
1976 ret = get_state_private(&info->extent_ins, start, &priv);
1977 BUG_ON(ret);
1978 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1980 if (extent_op->type == PENDING_EXTENT_INSERT) {
1981 key.objectid = start;
1982 key.offset = end + 1 - start;
1983 key.type = BTRFS_EXTENT_ITEM_KEY;
1984 ret = alloc_reserved_tree_block(trans, extent_root,
1985 extent_root->root_key.objectid,
1986 trans->transid,
1987 extent_op->flags,
1988 &extent_op->key,
1989 extent_op->level, &key);
1990 } else {
1991 BUG_ON(1);
1994 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1995 GFP_NOFS);
1996 kfree(extent_op);
1998 btrfs_free_path(path);
1999 return 0;
2002 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2003 struct btrfs_root *root,
2004 u64 bytenr, u64 num_bytes, int is_data)
2006 int err = 0;
2007 struct extent_buffer *buf;
2009 if (is_data)
2010 goto pinit;
2012 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2013 if (!buf)
2014 goto pinit;
2016 /* we can reuse a block if it hasn't been written
2017 * and it is from this transaction. We can't
2018 * reuse anything from the tree log root because
2019 * it has tiny sub-transactions.
2021 if (btrfs_buffer_uptodate(buf, 0)) {
2022 u64 header_owner = btrfs_header_owner(buf);
2023 u64 header_transid = btrfs_header_generation(buf);
2024 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2025 header_transid == trans->transid &&
2026 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2027 clean_tree_block(NULL, root, buf);
2028 free_extent_buffer(buf);
2029 return 1;
2032 free_extent_buffer(buf);
2033 pinit:
2034 update_pinned_extents(root, bytenr, num_bytes, 1);
2036 BUG_ON(err < 0);
2037 return 0;
2040 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2041 u64 bytenr, u64 num_bytes)
2043 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2047 * remove an extent from the root, returns 0 on success
2049 static int __free_extent(struct btrfs_trans_handle *trans,
2050 struct btrfs_root *root,
2051 u64 bytenr, u64 num_bytes, u64 parent,
2052 u64 root_objectid, u64 owner_objectid,
2053 u64 owner_offset, int refs_to_drop)
2056 struct btrfs_key key;
2057 struct btrfs_path *path;
2058 struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2059 struct btrfs_root *extent_root = root->fs_info->extent_root;
2060 struct extent_buffer *leaf;
2061 struct btrfs_extent_item *ei;
2062 struct btrfs_extent_inline_ref *iref;
2063 int ret;
2064 int is_data;
2065 int extent_slot = 0;
2066 int found_extent = 0;
2067 int num_to_del = 1;
2068 u32 item_size;
2069 u64 refs;
2071 path = btrfs_alloc_path();
2072 if (!path)
2073 return -ENOMEM;
2075 path->reada = 1;
2076 path->leave_spinning = 1;
2078 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2079 BUG_ON(!is_data && refs_to_drop != 1);
2081 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2082 bytenr, num_bytes, parent,
2083 root_objectid, owner_objectid,
2084 owner_offset);
2085 if (ret == 0) {
2086 extent_slot = path->slots[0];
2087 while (extent_slot >= 0) {
2088 btrfs_item_key_to_cpu(path->nodes[0], &key,
2089 extent_slot);
2090 if (key.objectid != bytenr)
2091 break;
2092 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2093 key.offset == num_bytes) {
2094 found_extent = 1;
2095 break;
2097 if (path->slots[0] - extent_slot > 5)
2098 break;
2099 extent_slot--;
2101 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2102 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2103 if (found_extent && item_size < sizeof(*ei))
2104 found_extent = 0;
2105 #endif
2106 if (!found_extent) {
2107 BUG_ON(iref);
2108 ret = remove_extent_backref(trans, extent_root, path,
2109 NULL, refs_to_drop,
2110 is_data);
2111 BUG_ON(ret);
2112 btrfs_release_path(extent_root, path);
2113 path->leave_spinning = 1;
2115 key.objectid = bytenr;
2116 key.type = BTRFS_EXTENT_ITEM_KEY;
2117 key.offset = num_bytes;
2119 ret = btrfs_search_slot(trans, extent_root,
2120 &key, path, -1, 1);
2121 if (ret) {
2122 printk(KERN_ERR "umm, got %d back from search"
2123 ", was looking for %llu\n", ret,
2124 (unsigned long long)bytenr);
2125 btrfs_print_leaf(extent_root, path->nodes[0]);
2127 BUG_ON(ret);
2128 extent_slot = path->slots[0];
2130 } else {
2131 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2132 "parent %llu root %llu owner %llu offset %llu\n",
2133 (unsigned long long)bytenr,
2134 (unsigned long long)parent,
2135 (unsigned long long)root_objectid,
2136 (unsigned long long)owner_objectid,
2137 (unsigned long long)owner_offset);
2138 ret = -EIO;
2139 goto fail;
2142 leaf = path->nodes[0];
2143 item_size = btrfs_item_size_nr(leaf, extent_slot);
2144 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2145 if (item_size < sizeof(*ei)) {
2146 BUG_ON(found_extent || extent_slot != path->slots[0]);
2147 ret = convert_extent_item_v0(trans, extent_root, path,
2148 owner_objectid, 0);
2149 BUG_ON(ret < 0);
2151 btrfs_release_path(extent_root, path);
2152 path->leave_spinning = 1;
2154 key.objectid = bytenr;
2155 key.type = BTRFS_EXTENT_ITEM_KEY;
2156 key.offset = num_bytes;
2158 ret = btrfs_search_slot(trans, extent_root, &key, path,
2159 -1, 1);
2160 if (ret) {
2161 printk(KERN_ERR "umm, got %d back from search"
2162 ", was looking for %llu\n", ret,
2163 (unsigned long long)bytenr);
2164 btrfs_print_leaf(extent_root, path->nodes[0]);
2166 BUG_ON(ret);
2167 extent_slot = path->slots[0];
2168 leaf = path->nodes[0];
2169 item_size = btrfs_item_size_nr(leaf, extent_slot);
2171 #endif
2172 BUG_ON(item_size < sizeof(*ei));
2173 ei = btrfs_item_ptr(leaf, extent_slot,
2174 struct btrfs_extent_item);
2175 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2176 struct btrfs_tree_block_info *bi;
2177 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2178 bi = (struct btrfs_tree_block_info *)(ei + 1);
2179 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2182 refs = btrfs_extent_refs(leaf, ei);
2183 BUG_ON(refs < refs_to_drop);
2184 refs -= refs_to_drop;
2186 if (refs > 0) {
2188 * In the case of inline back ref, reference count will
2189 * be updated by remove_extent_backref
2191 if (iref) {
2192 BUG_ON(!found_extent);
2193 } else {
2194 btrfs_set_extent_refs(leaf, ei, refs);
2195 btrfs_mark_buffer_dirty(leaf);
2197 if (found_extent) {
2198 ret = remove_extent_backref(trans, extent_root, path,
2199 iref, refs_to_drop,
2200 is_data);
2201 BUG_ON(ret);
2203 } else {
2204 int mark_free = 0;
2205 int pin = 1;
2207 if (found_extent) {
2208 BUG_ON(is_data && refs_to_drop !=
2209 extent_data_ref_count(root, path, iref));
2210 if (iref) {
2211 BUG_ON(path->slots[0] != extent_slot);
2212 } else {
2213 BUG_ON(path->slots[0] != extent_slot + 1);
2214 path->slots[0] = extent_slot;
2215 num_to_del = 2;
2219 if (ops && ops->free_extent) {
2220 ret = ops->free_extent(root, bytenr, num_bytes);
2221 if (ret > 0) {
2222 pin = 0;
2223 mark_free = 0;
2227 if (pin) {
2228 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2229 is_data);
2230 if (ret > 0)
2231 mark_free = 1;
2232 BUG_ON(ret < 0);
2235 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2236 num_to_del);
2237 BUG_ON(ret);
2238 btrfs_release_path(extent_root, path);
2240 if (is_data) {
2241 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2242 BUG_ON(ret);
2245 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2246 mark_free);
2247 BUG_ON(ret);
2249 fail:
2250 btrfs_free_path(path);
2251 finish_current_insert(trans, extent_root);
2252 return ret;
2256 * find all the blocks marked as pending in the radix tree and remove
2257 * them from the extent map
2259 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2260 btrfs_root *extent_root)
2262 int ret;
2263 int err = 0;
2264 u64 start;
2265 u64 end;
2266 u64 priv;
2267 struct extent_io_tree *pending_del;
2268 struct extent_io_tree *extent_ins;
2269 struct pending_extent_op *extent_op;
2271 extent_ins = &extent_root->fs_info->extent_ins;
2272 pending_del = &extent_root->fs_info->pending_del;
2274 while(1) {
2275 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2276 EXTENT_LOCKED);
2277 if (ret)
2278 break;
2280 ret = get_state_private(pending_del, start, &priv);
2281 BUG_ON(ret);
2282 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2284 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2285 GFP_NOFS);
2287 if (!test_range_bit(extent_ins, start, end,
2288 EXTENT_LOCKED, 0)) {
2289 ret = __free_extent(trans, extent_root,
2290 start, end + 1 - start, 0,
2291 extent_root->root_key.objectid,
2292 extent_op->level, 0, 1);
2293 kfree(extent_op);
2294 } else {
2295 kfree(extent_op);
2296 ret = get_state_private(extent_ins, start, &priv);
2297 BUG_ON(ret);
2298 extent_op = (struct pending_extent_op *)
2299 (unsigned long)priv;
2301 clear_extent_bits(extent_ins, start, end,
2302 EXTENT_LOCKED, GFP_NOFS);
2304 if (extent_op->type == PENDING_BACKREF_UPDATE)
2305 BUG_ON(1);
2307 kfree(extent_op);
2309 if (ret)
2310 err = ret;
2312 return err;
2316 * remove an extent from the root, returns 0 on success
2319 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2320 struct btrfs_root *root,
2321 u64 bytenr, u64 num_bytes, u64 parent,
2322 u64 root_objectid, u64 owner, u64 offset)
2324 struct btrfs_root *extent_root = root->fs_info->extent_root;
2325 int pending_ret;
2326 int ret;
2328 WARN_ON(num_bytes < root->sectorsize);
2329 if (root == extent_root) {
2330 struct pending_extent_op *extent_op;
2332 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2333 BUG_ON(!extent_op);
2335 extent_op->type = PENDING_EXTENT_DELETE;
2336 extent_op->bytenr = bytenr;
2337 extent_op->num_bytes = num_bytes;
2338 extent_op->level = (int)owner;
2340 set_extent_bits(&root->fs_info->pending_del,
2341 bytenr, bytenr + num_bytes - 1,
2342 EXTENT_LOCKED, GFP_NOFS);
2343 set_state_private(&root->fs_info->pending_del,
2344 bytenr, (unsigned long)extent_op);
2345 return 0;
2347 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2348 root_objectid, owner, offset, 1);
2349 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2350 return ret ? ret : pending_ret;
2353 static u64 stripe_align(struct btrfs_root *root, u64 val)
2355 u64 mask = ((u64)root->stripesize - 1);
2356 u64 ret = (val + mask) & ~mask;
2357 return ret;
2361 * walks the btree of allocated extents and find a hole of a given size.
2362 * The key ins is changed to record the hole:
2363 * ins->objectid == block start
2364 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2365 * ins->offset == number of blocks
2366 * Any available blocks before search_start are skipped.
2368 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2369 struct btrfs_root *orig_root,
2370 u64 num_bytes, u64 empty_size,
2371 u64 search_start, u64 search_end,
2372 u64 hint_byte, struct btrfs_key *ins,
2373 u64 exclude_start, u64 exclude_nr,
2374 int data)
2376 int ret;
2377 u64 orig_search_start = search_start;
2378 struct btrfs_root * root = orig_root->fs_info->extent_root;
2379 struct btrfs_fs_info *info = root->fs_info;
2380 u64 total_needed = num_bytes;
2381 struct btrfs_block_group_cache *block_group;
2382 int full_scan = 0;
2383 int wrapped = 0;
2385 WARN_ON(num_bytes < root->sectorsize);
2386 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2388 if (hint_byte) {
2389 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2390 if (!block_group)
2391 hint_byte = search_start;
2392 block_group = btrfs_find_block_group(root, block_group,
2393 hint_byte, data, 1);
2394 } else {
2395 block_group = btrfs_find_block_group(root,
2396 trans->block_group,
2397 search_start, data, 1);
2400 total_needed += empty_size;
2402 check_failed:
2403 if (!block_group) {
2404 block_group = btrfs_lookup_first_block_group(info,
2405 search_start);
2406 if (!block_group)
2407 block_group = btrfs_lookup_first_block_group(info,
2408 orig_search_start);
2410 ret = find_search_start(root, &block_group, &search_start,
2411 total_needed, data);
2412 if (ret)
2413 goto error;
2415 search_start = stripe_align(root, search_start);
2416 ins->objectid = search_start;
2417 ins->offset = num_bytes;
2419 if (ins->objectid + num_bytes >
2420 block_group->key.objectid + block_group->key.offset) {
2421 search_start = block_group->key.objectid +
2422 block_group->key.offset;
2423 goto new_group;
2426 if (test_range_bit(&info->extent_ins, ins->objectid,
2427 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2428 search_start = ins->objectid + num_bytes;
2429 goto new_group;
2432 if (test_range_bit(&info->pinned_extents, ins->objectid,
2433 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2434 search_start = ins->objectid + num_bytes;
2435 goto new_group;
2438 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2439 ins->objectid < exclude_start + exclude_nr)) {
2440 search_start = exclude_start + exclude_nr;
2441 goto new_group;
2444 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2445 block_group = btrfs_lookup_block_group(info, ins->objectid);
2446 if (block_group)
2447 trans->block_group = block_group;
2449 ins->offset = num_bytes;
2450 return 0;
2452 new_group:
2453 block_group = btrfs_lookup_first_block_group(info, search_start);
2454 if (!block_group) {
2455 search_start = orig_search_start;
2456 if (full_scan) {
2457 ret = -ENOSPC;
2458 goto error;
2460 if (wrapped) {
2461 if (!full_scan)
2462 total_needed -= empty_size;
2463 full_scan = 1;
2464 } else
2465 wrapped = 1;
2467 cond_resched();
2468 block_group = btrfs_find_block_group(root, block_group,
2469 search_start, data, 0);
2470 goto check_failed;
2472 error:
2473 return ret;
2476 static int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2477 struct btrfs_root *root,
2478 u64 num_bytes, u64 empty_size,
2479 u64 hint_byte, u64 search_end,
2480 struct btrfs_key *ins, int data)
2482 int ret;
2483 u64 search_start = 0;
2484 u64 alloc_profile;
2485 struct btrfs_fs_info *info = root->fs_info;
2487 if (info->extent_ops) {
2488 struct btrfs_extent_ops *ops = info->extent_ops;
2489 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2490 BUG_ON(ret);
2491 goto found;
2494 if (data) {
2495 alloc_profile = info->avail_data_alloc_bits &
2496 info->data_alloc_profile;
2497 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2498 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2499 info->system_allocs >= 0) {
2500 alloc_profile = info->avail_system_alloc_bits &
2501 info->system_alloc_profile;
2502 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2503 } else {
2504 alloc_profile = info->avail_metadata_alloc_bits &
2505 info->metadata_alloc_profile;
2506 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2509 if (root->ref_cows) {
2510 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2511 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2512 num_bytes,
2513 BTRFS_BLOCK_GROUP_METADATA);
2514 BUG_ON(ret);
2516 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2517 num_bytes + 2 * 1024 * 1024, data);
2518 BUG_ON(ret);
2521 WARN_ON(num_bytes < root->sectorsize);
2522 ret = find_free_extent(trans, root, num_bytes, empty_size,
2523 search_start, search_end, hint_byte, ins,
2524 trans->alloc_exclude_start,
2525 trans->alloc_exclude_nr, data);
2526 BUG_ON(ret);
2527 found:
2528 clear_extent_dirty(&root->fs_info->free_space_cache,
2529 ins->objectid, ins->objectid + ins->offset - 1,
2530 GFP_NOFS);
2531 return ret;
2534 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2535 struct btrfs_root *root,
2536 u64 root_objectid, u64 generation,
2537 u64 flags, struct btrfs_disk_key *key,
2538 int level, struct btrfs_key *ins)
2540 int ret;
2541 struct btrfs_fs_info *fs_info = root->fs_info;
2542 struct btrfs_extent_item *extent_item;
2543 struct btrfs_tree_block_info *block_info;
2544 struct btrfs_extent_inline_ref *iref;
2545 struct btrfs_path *path;
2546 struct extent_buffer *leaf;
2547 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
2549 path = btrfs_alloc_path();
2550 BUG_ON(!path);
2552 path->leave_spinning = 1;
2553 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2554 ins, size);
2555 BUG_ON(ret);
2557 leaf = path->nodes[0];
2558 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2559 struct btrfs_extent_item);
2560 btrfs_set_extent_refs(leaf, extent_item, 1);
2561 btrfs_set_extent_generation(leaf, extent_item, generation);
2562 btrfs_set_extent_flags(leaf, extent_item,
2563 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2564 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2566 btrfs_set_tree_block_key(leaf, block_info, key);
2567 btrfs_set_tree_block_level(leaf, block_info, level);
2569 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2570 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2571 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2573 btrfs_mark_buffer_dirty(leaf);
2574 btrfs_free_path(path);
2576 ret = update_block_group(trans, root, ins->objectid, ins->offset,
2577 1, 0);
2578 if (ret) {
2579 printk(KERN_ERR "btrfs update block group failed for %llu "
2580 "%llu\n", (unsigned long long)ins->objectid,
2581 (unsigned long long)ins->offset);
2582 BUG();
2584 return ret;
2587 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2588 struct btrfs_root *root, u64 num_bytes,
2589 u64 root_objectid, u64 generation,
2590 u64 flags, struct btrfs_disk_key *key,
2591 int level, u64 empty_size, u64 hint_byte,
2592 u64 search_end, struct btrfs_key *ins)
2594 int ret;
2595 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2596 hint_byte, search_end, ins, 0);
2597 BUG_ON(ret);
2599 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2600 struct pending_extent_op *extent_op;
2602 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2603 BUG_ON(!extent_op);
2605 extent_op->type = PENDING_EXTENT_INSERT;
2606 extent_op->bytenr = ins->objectid;
2607 extent_op->num_bytes = ins->offset;
2608 extent_op->level = level;
2609 extent_op->flags = flags;
2610 memcpy(&extent_op->key, key, sizeof(*key));
2612 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2613 ins->objectid + ins->offset - 1,
2614 EXTENT_LOCKED, GFP_NOFS);
2615 set_state_private(&root->fs_info->extent_ins,
2616 ins->objectid, (unsigned long)extent_op);
2617 } else {
2618 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2619 generation, flags,
2620 key, level, ins);
2621 finish_current_insert(trans, root->fs_info->extent_root);
2622 del_pending_extents(trans, root->fs_info->extent_root);
2624 return ret;
2628 * helper function to allocate a block for a given tree
2629 * returns the tree buffer or NULL.
2631 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2632 struct btrfs_root *root,
2633 u32 blocksize, u64 root_objectid,
2634 struct btrfs_disk_key *key, int level,
2635 u64 hint, u64 empty_size)
2637 struct btrfs_key ins;
2638 int ret;
2639 struct extent_buffer *buf;
2641 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2642 trans->transid, 0, key, level,
2643 empty_size, hint, (u64)-1, &ins);
2644 if (ret) {
2645 BUG_ON(ret > 0);
2646 return ERR_PTR(ret);
2649 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2650 if (!buf) {
2651 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2652 0, root->root_key.objectid, level, 0);
2653 BUG_ON(1);
2654 return ERR_PTR(-ENOMEM);
2656 btrfs_set_buffer_uptodate(buf);
2657 trans->blocks_used++;
2659 return buf;
2662 #if 0
2664 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2665 struct btrfs_root *root,
2666 struct extent_buffer *leaf)
2668 u64 leaf_owner;
2669 u64 leaf_generation;
2670 struct btrfs_key key;
2671 struct btrfs_file_extent_item *fi;
2672 int i;
2673 int nritems;
2674 int ret;
2676 BUG_ON(!btrfs_is_leaf(leaf));
2677 nritems = btrfs_header_nritems(leaf);
2678 leaf_owner = btrfs_header_owner(leaf);
2679 leaf_generation = btrfs_header_generation(leaf);
2681 for (i = 0; i < nritems; i++) {
2682 u64 disk_bytenr;
2684 btrfs_item_key_to_cpu(leaf, &key, i);
2685 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2686 continue;
2687 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2688 if (btrfs_file_extent_type(leaf, fi) ==
2689 BTRFS_FILE_EXTENT_INLINE)
2690 continue;
2692 * FIXME make sure to insert a trans record that
2693 * repeats the snapshot del on crash
2695 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2696 if (disk_bytenr == 0)
2697 continue;
2698 ret = btrfs_free_extent(trans, root, disk_bytenr,
2699 btrfs_file_extent_disk_num_bytes(leaf, fi),
2700 leaf->start, leaf_owner, leaf_generation,
2701 key.objectid, 0);
2702 BUG_ON(ret);
2704 return 0;
2707 static void noinline reada_walk_down(struct btrfs_root *root,
2708 struct extent_buffer *node,
2709 int slot)
2711 u64 bytenr;
2712 u64 last = 0;
2713 u32 nritems;
2714 u32 refs;
2715 u32 blocksize;
2716 int ret;
2717 int i;
2718 int level;
2719 int skipped = 0;
2721 nritems = btrfs_header_nritems(node);
2722 level = btrfs_header_level(node);
2723 if (level)
2724 return;
2726 for (i = slot; i < nritems && skipped < 32; i++) {
2727 bytenr = btrfs_node_blockptr(node, i);
2728 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2729 (last > bytenr && last - bytenr > 32 * 1024))) {
2730 skipped++;
2731 continue;
2733 blocksize = btrfs_level_size(root, level - 1);
2734 if (i != slot) {
2735 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2736 blocksize, &refs);
2737 BUG_ON(ret);
2738 if (refs != 1) {
2739 skipped++;
2740 continue;
2743 mutex_unlock(&root->fs_info->fs_mutex);
2744 ret = readahead_tree_block(root, bytenr, blocksize,
2745 btrfs_node_ptr_generation(node, i));
2746 last = bytenr + blocksize;
2747 cond_resched();
2748 mutex_lock(&root->fs_info->fs_mutex);
2749 if (ret)
2750 break;
2755 * helper function for drop_snapshot, this walks down the tree dropping ref
2756 * counts as it goes.
2758 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2759 struct btrfs_root *root,
2760 struct btrfs_path *path, int *level)
2762 u64 root_owner;
2763 u64 root_gen;
2764 u64 bytenr;
2765 u64 ptr_gen;
2766 struct extent_buffer *next;
2767 struct extent_buffer *cur;
2768 struct extent_buffer *parent;
2769 u32 blocksize;
2770 int ret;
2771 u32 refs;
2773 WARN_ON(*level < 0);
2774 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2775 ret = btrfs_lookup_extent_ref(trans, root,
2776 path->nodes[*level]->start,
2777 path->nodes[*level]->len, &refs);
2778 BUG_ON(ret);
2779 if (refs > 1)
2780 goto out;
2783 * walk down to the last node level and free all the leaves
2785 while(*level >= 0) {
2786 WARN_ON(*level < 0);
2787 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2788 cur = path->nodes[*level];
2790 if (btrfs_header_level(cur) != *level)
2791 WARN_ON(1);
2793 if (path->slots[*level] >=
2794 btrfs_header_nritems(cur))
2795 break;
2796 if (*level == 0) {
2797 ret = drop_leaf_ref(trans, root, cur);
2798 BUG_ON(ret);
2799 break;
2801 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2802 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2803 blocksize = btrfs_level_size(root, *level - 1);
2804 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2805 &refs);
2806 BUG_ON(ret);
2807 if (refs != 1) {
2808 parent = path->nodes[*level];
2809 root_owner = btrfs_header_owner(parent);
2810 root_gen = btrfs_header_generation(parent);
2811 path->slots[*level]++;
2812 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2813 parent->start, root_owner,
2814 root_gen, *level - 1, 1);
2815 BUG_ON(ret);
2816 continue;
2818 next = btrfs_find_tree_block(root, bytenr, blocksize);
2819 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2820 free_extent_buffer(next);
2821 reada_walk_down(root, cur, path->slots[*level]);
2822 mutex_unlock(&root->fs_info->fs_mutex);
2823 next = read_tree_block(root, bytenr, blocksize,
2824 ptr_gen);
2825 mutex_lock(&root->fs_info->fs_mutex);
2827 WARN_ON(*level <= 0);
2828 if (path->nodes[*level-1])
2829 free_extent_buffer(path->nodes[*level-1]);
2830 path->nodes[*level-1] = next;
2831 *level = btrfs_header_level(next);
2832 path->slots[*level] = 0;
2834 out:
2835 WARN_ON(*level < 0);
2836 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2838 if (path->nodes[*level] == root->node) {
2839 root_owner = root->root_key.objectid;
2840 parent = path->nodes[*level];
2841 } else {
2842 parent = path->nodes[*level + 1];
2843 root_owner = btrfs_header_owner(parent);
2846 root_gen = btrfs_header_generation(parent);
2847 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2848 path->nodes[*level]->len, parent->start,
2849 root_owner, root_gen, *level, 1);
2850 free_extent_buffer(path->nodes[*level]);
2851 path->nodes[*level] = NULL;
2852 *level += 1;
2853 BUG_ON(ret);
2854 return 0;
2858 * helper for dropping snapshots. This walks back up the tree in the path
2859 * to find the first node higher up where we haven't yet gone through
2860 * all the slots
2862 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2863 struct btrfs_root *root,
2864 struct btrfs_path *path, int *level)
2866 u64 root_owner;
2867 u64 root_gen;
2868 struct btrfs_root_item *root_item = &root->root_item;
2869 int i;
2870 int slot;
2871 int ret;
2873 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2874 slot = path->slots[i];
2875 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2876 struct extent_buffer *node;
2877 struct btrfs_disk_key disk_key;
2878 node = path->nodes[i];
2879 path->slots[i]++;
2880 *level = i;
2881 WARN_ON(*level == 0);
2882 btrfs_node_key(node, &disk_key, path->slots[i]);
2883 memcpy(&root_item->drop_progress,
2884 &disk_key, sizeof(disk_key));
2885 root_item->drop_level = i;
2886 return 0;
2887 } else {
2888 struct extent_buffer *parent;
2889 if (path->nodes[*level] == root->node)
2890 parent = path->nodes[*level];
2891 else
2892 parent = path->nodes[*level + 1];
2894 root_owner = btrfs_header_owner(parent);
2895 root_gen = btrfs_header_generation(parent);
2896 ret = btrfs_free_extent(trans, root,
2897 path->nodes[*level]->start,
2898 path->nodes[*level]->len,
2899 parent->start, root_owner,
2900 root_gen, *level, 1);
2901 BUG_ON(ret);
2902 free_extent_buffer(path->nodes[*level]);
2903 path->nodes[*level] = NULL;
2904 *level = i + 1;
2907 return 1;
2911 * drop the reference count on the tree rooted at 'snap'. This traverses
2912 * the tree freeing any blocks that have a ref count of zero after being
2913 * decremented.
2915 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2916 *root)
2918 int ret = 0;
2919 int wret;
2920 int level;
2921 struct btrfs_path *path;
2922 int i;
2923 int orig_level;
2924 struct btrfs_root_item *root_item = &root->root_item;
2926 path = btrfs_alloc_path();
2927 BUG_ON(!path);
2929 level = btrfs_header_level(root->node);
2930 orig_level = level;
2931 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2932 path->nodes[level] = root->node;
2933 extent_buffer_get(root->node);
2934 path->slots[level] = 0;
2935 } else {
2936 struct btrfs_key key;
2937 struct btrfs_disk_key found_key;
2938 struct extent_buffer *node;
2940 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2941 level = root_item->drop_level;
2942 path->lowest_level = level;
2943 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2944 if (wret < 0) {
2945 ret = wret;
2946 goto out;
2948 node = path->nodes[level];
2949 btrfs_node_key(node, &found_key, path->slots[level]);
2950 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2951 sizeof(found_key)));
2953 while(1) {
2954 wret = walk_down_tree(trans, root, path, &level);
2955 if (wret < 0)
2956 ret = wret;
2957 if (wret != 0)
2958 break;
2960 wret = walk_up_tree(trans, root, path, &level);
2961 if (wret < 0)
2962 ret = wret;
2963 if (wret != 0)
2964 break;
2966 ret = -EAGAIN;
2967 break;
2970 for (i = 0; i <= orig_level; i++) {
2971 if (path->nodes[i]) {
2972 free_extent_buffer(path->nodes[i]);
2973 path->nodes[i] = NULL;
2976 out:
2977 btrfs_free_path(path);
2978 return ret;
2981 #endif
2983 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2985 u64 start;
2986 u64 end;
2987 u64 ptr;
2988 int ret;
2989 while(1) {
2990 ret = find_first_extent_bit(&info->block_group_cache, 0,
2991 &start, &end, (unsigned int)-1);
2992 if (ret)
2993 break;
2994 ret = get_state_private(&info->block_group_cache, start, &ptr);
2995 if (!ret)
2996 kfree((void *)(unsigned long)ptr);
2997 clear_extent_bits(&info->block_group_cache, start,
2998 end, (unsigned int)-1, GFP_NOFS);
3000 while(1) {
3001 ret = find_first_extent_bit(&info->free_space_cache, 0,
3002 &start, &end, EXTENT_DIRTY);
3003 if (ret)
3004 break;
3005 clear_extent_dirty(&info->free_space_cache, start,
3006 end, GFP_NOFS);
3008 return 0;
3011 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3012 struct btrfs_key *key)
3014 int ret;
3015 struct btrfs_key found_key;
3016 struct extent_buffer *leaf;
3017 int slot;
3019 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3020 if (ret < 0)
3021 return ret;
3022 while(1) {
3023 slot = path->slots[0];
3024 leaf = path->nodes[0];
3025 if (slot >= btrfs_header_nritems(leaf)) {
3026 ret = btrfs_next_leaf(root, path);
3027 if (ret == 0)
3028 continue;
3029 if (ret < 0)
3030 goto error;
3031 break;
3033 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3035 if (found_key.objectid >= key->objectid &&
3036 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3037 return 0;
3038 path->slots[0]++;
3040 ret = -ENOENT;
3041 error:
3042 return ret;
3045 int btrfs_read_block_groups(struct btrfs_root *root)
3047 struct btrfs_path *path;
3048 int ret;
3049 int bit;
3050 struct btrfs_block_group_cache *cache;
3051 struct btrfs_fs_info *info = root->fs_info;
3052 struct btrfs_space_info *space_info;
3053 struct extent_io_tree *block_group_cache;
3054 struct btrfs_key key;
3055 struct btrfs_key found_key;
3056 struct extent_buffer *leaf;
3058 block_group_cache = &info->block_group_cache;
3060 root = info->extent_root;
3061 key.objectid = 0;
3062 key.offset = 0;
3063 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3064 path = btrfs_alloc_path();
3065 if (!path)
3066 return -ENOMEM;
3068 while(1) {
3069 ret = find_first_block_group(root, path, &key);
3070 if (ret > 0) {
3071 ret = 0;
3072 goto error;
3074 if (ret != 0) {
3075 goto error;
3077 leaf = path->nodes[0];
3078 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3079 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3080 if (!cache) {
3081 ret = -ENOMEM;
3082 break;
3085 read_extent_buffer(leaf, &cache->item,
3086 btrfs_item_ptr_offset(leaf, path->slots[0]),
3087 sizeof(cache->item));
3088 memcpy(&cache->key, &found_key, sizeof(found_key));
3089 cache->cached = 0;
3090 cache->pinned = 0;
3091 key.objectid = found_key.objectid + found_key.offset;
3092 btrfs_release_path(root, path);
3093 cache->flags = btrfs_block_group_flags(&cache->item);
3094 bit = 0;
3095 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3096 bit = BLOCK_GROUP_DATA;
3097 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3098 bit = BLOCK_GROUP_SYSTEM;
3099 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3100 bit = BLOCK_GROUP_METADATA;
3102 set_avail_alloc_bits(info, cache->flags);
3103 if (btrfs_chunk_readonly(root, cache->key.objectid))
3104 cache->ro = 1;
3106 ret = update_space_info(info, cache->flags, found_key.offset,
3107 btrfs_block_group_used(&cache->item),
3108 &space_info);
3109 BUG_ON(ret);
3110 cache->space_info = space_info;
3112 /* use EXTENT_LOCKED to prevent merging */
3113 set_extent_bits(block_group_cache, found_key.objectid,
3114 found_key.objectid + found_key.offset - 1,
3115 bit | EXTENT_LOCKED, GFP_NOFS);
3116 set_state_private(block_group_cache, found_key.objectid,
3117 (unsigned long)cache);
3119 ret = 0;
3120 error:
3121 btrfs_free_path(path);
3122 return ret;
3125 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3126 struct btrfs_root *root, u64 bytes_used,
3127 u64 type, u64 chunk_objectid, u64 chunk_offset,
3128 u64 size)
3130 int ret;
3131 int bit = 0;
3132 struct btrfs_root *extent_root;
3133 struct btrfs_block_group_cache *cache;
3134 struct extent_io_tree *block_group_cache;
3136 extent_root = root->fs_info->extent_root;
3137 block_group_cache = &root->fs_info->block_group_cache;
3139 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3140 BUG_ON(!cache);
3141 cache->key.objectid = chunk_offset;
3142 cache->key.offset = size;
3144 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3145 btrfs_set_block_group_used(&cache->item, bytes_used);
3146 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3147 cache->flags = type;
3148 btrfs_set_block_group_flags(&cache->item, type);
3150 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3151 &cache->space_info);
3152 BUG_ON(ret);
3154 bit = block_group_state_bits(type);
3155 set_extent_bits(block_group_cache, chunk_offset,
3156 chunk_offset + size - 1,
3157 bit | EXTENT_LOCKED, GFP_NOFS);
3159 set_state_private(block_group_cache, chunk_offset,
3160 (unsigned long)cache);
3161 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3162 sizeof(cache->item));
3163 BUG_ON(ret);
3165 finish_current_insert(trans, extent_root);
3166 ret = del_pending_extents(trans, extent_root);
3167 BUG_ON(ret);
3168 set_avail_alloc_bits(extent_root->fs_info, type);
3169 return 0;
3173 * This is for converter use only.
3175 * In that case, we don't know where are free blocks located.
3176 * Therefore all block group cache entries must be setup properly
3177 * before doing any block allocation.
3179 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3180 struct btrfs_root *root)
3182 u64 total_bytes;
3183 u64 cur_start;
3184 u64 group_type;
3185 u64 group_size;
3186 u64 group_align;
3187 u64 total_data = 0;
3188 u64 total_metadata = 0;
3189 u64 chunk_objectid;
3190 int ret;
3191 int bit;
3192 struct btrfs_root *extent_root;
3193 struct btrfs_block_group_cache *cache;
3194 struct extent_io_tree *block_group_cache;
3196 extent_root = root->fs_info->extent_root;
3197 block_group_cache = &root->fs_info->block_group_cache;
3198 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3199 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
3200 group_align = 64 * root->sectorsize;
3202 cur_start = 0;
3203 while (cur_start < total_bytes) {
3204 group_size = total_bytes / 12;
3205 group_size = min_t(u64, group_size, total_bytes - cur_start);
3206 if (cur_start == 0) {
3207 bit = BLOCK_GROUP_SYSTEM;
3208 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3209 group_size /= 4;
3210 group_size &= ~(group_align - 1);
3211 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3212 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3213 } else {
3214 group_size &= ~(group_align - 1);
3215 if (total_data >= total_metadata * 2) {
3216 group_type = BTRFS_BLOCK_GROUP_METADATA;
3217 group_size = min_t(u64, group_size,
3218 1ULL * 1024 * 1024 * 1024);
3219 total_metadata += group_size;
3220 } else {
3221 group_type = BTRFS_BLOCK_GROUP_DATA;
3222 group_size = min_t(u64, group_size,
3223 5ULL * 1024 * 1024 * 1024);
3224 total_data += group_size;
3226 if ((total_bytes - cur_start) * 4 < group_size * 5)
3227 group_size = total_bytes - cur_start;
3230 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3231 BUG_ON(!cache);
3233 cache->key.objectid = cur_start;
3234 cache->key.offset = group_size;
3235 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3237 btrfs_set_block_group_used(&cache->item, 0);
3238 btrfs_set_block_group_chunk_objectid(&cache->item,
3239 chunk_objectid);
3240 btrfs_set_block_group_flags(&cache->item, group_type);
3242 cache->flags = group_type;
3244 ret = update_space_info(root->fs_info, group_type, group_size,
3245 0, &cache->space_info);
3246 BUG_ON(ret);
3247 set_avail_alloc_bits(extent_root->fs_info, group_type);
3249 set_extent_bits(block_group_cache, cur_start,
3250 cur_start + group_size - 1,
3251 bit | EXTENT_LOCKED, GFP_NOFS);
3252 set_state_private(block_group_cache, cur_start,
3253 (unsigned long)cache);
3254 cur_start += group_size;
3256 /* then insert all the items */
3257 cur_start = 0;
3258 while(cur_start < total_bytes) {
3259 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3260 BUG_ON(!cache);
3262 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3263 sizeof(cache->item));
3264 BUG_ON(ret);
3266 finish_current_insert(trans, extent_root);
3267 ret = del_pending_extents(trans, extent_root);
3268 BUG_ON(ret);
3270 cur_start = cache->key.objectid + cache->key.offset;
3272 return 0;
3275 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *root,
3277 u64 bytenr, u64 num_bytes, int alloc,
3278 int mark_free)
3280 return update_block_group(trans, root, bytenr, num_bytes,
3281 alloc, mark_free);
3284 static int btrfs_count_extents_in_block_group(struct btrfs_root *root,
3285 struct btrfs_path *path, u64 start,
3286 u64 len,
3287 u64 *total)
3289 struct btrfs_key key;
3290 struct extent_buffer *leaf;
3291 u64 bytes_used = 0;
3292 int ret;
3293 int slot;
3295 key.offset = 0;
3296 key.objectid = start;
3297 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3298 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
3299 &key, path, 0, 0);
3300 if (ret < 0)
3301 return ret;
3302 while(1) {
3303 leaf = path->nodes[0];
3304 slot = path->slots[0];
3305 if (slot >= btrfs_header_nritems(leaf)) {
3306 ret = btrfs_next_leaf(root, path);
3307 if (ret < 0)
3308 return ret;
3309 if (ret > 0)
3310 break;
3311 leaf = path->nodes[0];
3312 slot = path->slots[0];
3314 btrfs_item_key_to_cpu(leaf, &key, slot);
3315 if (key.objectid > start + len)
3316 break;
3317 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3318 bytes_used += key.offset;
3319 path->slots[0]++;
3321 *total = bytes_used;
3322 btrfs_release_path(root, path);
3323 return 0;
3326 int btrfs_check_block_accounting(struct btrfs_root *root)
3328 int ret;
3329 u64 start = 0;
3330 u64 bytes_used = 0;
3331 struct btrfs_path path;
3332 struct btrfs_block_group_cache *cache;
3333 struct btrfs_fs_info *fs_info = root->fs_info;
3335 btrfs_init_path(&path);
3337 while(1) {
3338 cache = btrfs_lookup_block_group(fs_info, start);
3339 if (!cache)
3340 break;
3342 ret = btrfs_count_extents_in_block_group(root, &path,
3343 cache->key.objectid,
3344 cache->key.offset,
3345 &bytes_used);
3347 if (ret == 0) {
3348 u64 on_disk = btrfs_block_group_used(&cache->item);
3349 if (on_disk != bytes_used) {
3350 fprintf(stderr, "bad block group accounting found %llu "
3351 "expected %llu block group %llu\n",
3352 (unsigned long long)bytes_used,
3353 (unsigned long long)on_disk,
3354 (unsigned long long)cache->key.objectid);
3357 start = cache->key.objectid + cache->key.offset;
3359 cache->space_info->bytes_used = 0;
3361 return 0;
3365 * Fixup block accounting. The initial block accounting created by
3366 * make_block_groups isn't accuracy in this case.
3368 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3369 struct btrfs_root *root)
3371 int ret;
3372 int slot;
3373 u64 start = 0;
3374 u64 bytes_used = 0;
3375 struct btrfs_path path;
3376 struct btrfs_key key;
3377 struct extent_buffer *leaf;
3378 struct btrfs_block_group_cache *cache;
3379 struct btrfs_fs_info *fs_info = root->fs_info;
3381 while(1) {
3382 cache = btrfs_lookup_block_group(fs_info, start);
3383 if (!cache)
3384 break;
3385 start = cache->key.objectid + cache->key.offset;
3386 btrfs_set_block_group_used(&cache->item, 0);
3387 cache->space_info->bytes_used = 0;
3390 btrfs_init_path(&path);
3391 key.offset = 0;
3392 key.objectid = 0;
3393 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3394 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3395 &key, &path, 0, 0);
3396 if (ret < 0)
3397 return ret;
3398 while(1) {
3399 leaf = path.nodes[0];
3400 slot = path.slots[0];
3401 if (slot >= btrfs_header_nritems(leaf)) {
3402 ret = btrfs_next_leaf(root, &path);
3403 if (ret < 0)
3404 return ret;
3405 if (ret > 0)
3406 break;
3407 leaf = path.nodes[0];
3408 slot = path.slots[0];
3410 btrfs_item_key_to_cpu(leaf, &key, slot);
3411 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3412 bytes_used += key.offset;
3413 ret = btrfs_update_block_group(trans, root,
3414 key.objectid, key.offset, 1, 0);
3415 BUG_ON(ret);
3417 path.slots[0]++;
3419 btrfs_set_super_bytes_used(&root->fs_info->super_copy, bytes_used);
3420 btrfs_release_path(root, &path);
3421 return 0;