2 * Copyright (C) 2011 STRATO. 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.
23 #include "transaction.h"
24 #include "delayed-ref.h"
28 * this structure records all encountered refs on the way up to the root
31 struct list_head list
;
40 static int __add_prelim_ref(struct list_head
*head
, u64 root_id
,
41 struct btrfs_key
*key
, int level
, u64 parent
,
42 u64 wanted_disk_byte
, int count
)
44 struct __prelim_ref
*ref
;
46 /* in case we're adding delayed refs, we're holding the refs spinlock */
47 ref
= kmalloc(sizeof(*ref
), GFP_ATOMIC
);
51 ref
->root_id
= root_id
;
55 memset(&ref
->key
, 0, sizeof(ref
->key
));
60 ref
->wanted_disk_byte
= wanted_disk_byte
;
61 list_add_tail(&ref
->list
, head
);
66 static int add_all_parents(struct btrfs_root
*root
, struct btrfs_path
*path
,
67 struct ulist
*parents
,
68 struct extent_buffer
*eb
, int level
,
69 u64 wanted_objectid
, u64 wanted_disk_byte
)
73 struct btrfs_file_extent_item
*fi
;
78 ret
= ulist_add(parents
, eb
->start
, 0, GFP_NOFS
);
86 * if the current leaf is full with EXTENT_DATA items, we must
87 * check the next one if that holds a reference as well.
88 * ref->count cannot be used to skip this check.
89 * repeat this until we don't find any additional EXTENT_DATA items.
92 ret
= btrfs_next_leaf(root
, path
);
99 for (slot
= 0; slot
< btrfs_header_nritems(eb
); ++slot
) {
100 btrfs_item_key_to_cpu(eb
, &key
, slot
);
101 if (key
.objectid
!= wanted_objectid
||
102 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
104 fi
= btrfs_item_ptr(eb
, slot
,
105 struct btrfs_file_extent_item
);
106 disk_byte
= btrfs_file_extent_disk_bytenr(eb
, fi
);
107 if (disk_byte
== wanted_disk_byte
)
116 * resolve an indirect backref in the form (root_id, key, level)
117 * to a logical address
119 static int __resolve_indirect_ref(struct btrfs_fs_info
*fs_info
,
120 int search_commit_root
,
121 struct __prelim_ref
*ref
,
122 struct ulist
*parents
)
124 struct btrfs_path
*path
;
125 struct btrfs_root
*root
;
126 struct btrfs_key root_key
;
127 struct btrfs_key key
= {0};
128 struct extent_buffer
*eb
;
131 int level
= ref
->level
;
133 path
= btrfs_alloc_path();
136 path
->search_commit_root
= !!search_commit_root
;
138 root_key
.objectid
= ref
->root_id
;
139 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
140 root_key
.offset
= (u64
)-1;
141 root
= btrfs_read_fs_root_no_name(fs_info
, &root_key
);
148 root_level
= btrfs_header_level(root
->node
);
151 if (root_level
+ 1 == level
)
154 path
->lowest_level
= level
;
155 ret
= btrfs_search_slot(NULL
, root
, &ref
->key
, path
, 0, 0);
156 pr_debug("search slot in root %llu (level %d, ref count %d) returned "
157 "%d for key (%llu %u %llu)\n",
158 (unsigned long long)ref
->root_id
, level
, ref
->count
, ret
,
159 (unsigned long long)ref
->key
.objectid
, ref
->key
.type
,
160 (unsigned long long)ref
->key
.offset
);
164 eb
= path
->nodes
[level
];
172 if (ret
== 1 && path
->slots
[0] >= btrfs_header_nritems(eb
)) {
173 ret
= btrfs_next_leaf(root
, path
);
179 btrfs_item_key_to_cpu(eb
, &key
, path
->slots
[0]);
182 /* the last two parameters will only be used for level == 0 */
183 ret
= add_all_parents(root
, path
, parents
, eb
, level
, key
.objectid
,
184 ref
->wanted_disk_byte
);
186 btrfs_free_path(path
);
191 * resolve all indirect backrefs from the list
193 static int __resolve_indirect_refs(struct btrfs_fs_info
*fs_info
,
194 int search_commit_root
,
195 struct list_head
*head
)
199 struct __prelim_ref
*ref
;
200 struct __prelim_ref
*ref_safe
;
201 struct __prelim_ref
*new_ref
;
202 struct ulist
*parents
;
203 struct ulist_node
*node
;
204 struct ulist_iterator uiter
;
206 parents
= ulist_alloc(GFP_NOFS
);
211 * _safe allows us to insert directly after the current item without
212 * iterating over the newly inserted items.
213 * we're also allowed to re-assign ref during iteration.
215 list_for_each_entry_safe(ref
, ref_safe
, head
, list
) {
216 if (ref
->parent
) /* already direct */
220 err
= __resolve_indirect_ref(fs_info
, search_commit_root
,
228 /* we put the first parent into the ref at hand */
229 ULIST_ITER_INIT(&uiter
);
230 node
= ulist_next(parents
, &uiter
);
231 ref
->parent
= node
? node
->val
: 0;
233 /* additional parents require new refs being added here */
234 while ((node
= ulist_next(parents
, &uiter
))) {
235 new_ref
= kmalloc(sizeof(*new_ref
), GFP_NOFS
);
240 memcpy(new_ref
, ref
, sizeof(*ref
));
241 new_ref
->parent
= node
->val
;
242 list_add(&new_ref
->list
, &ref
->list
);
244 ulist_reinit(parents
);
252 * merge two lists of backrefs and adjust counts accordingly
254 * mode = 1: merge identical keys, if key is set
255 * mode = 2: merge identical parents
257 static int __merge_refs(struct list_head
*head
, int mode
)
259 struct list_head
*pos1
;
261 list_for_each(pos1
, head
) {
262 struct list_head
*n2
;
263 struct list_head
*pos2
;
264 struct __prelim_ref
*ref1
;
266 ref1
= list_entry(pos1
, struct __prelim_ref
, list
);
268 if (mode
== 1 && ref1
->key
.type
== 0)
270 for (pos2
= pos1
->next
, n2
= pos2
->next
; pos2
!= head
;
271 pos2
= n2
, n2
= pos2
->next
) {
272 struct __prelim_ref
*ref2
;
274 ref2
= list_entry(pos2
, struct __prelim_ref
, list
);
277 if (memcmp(&ref1
->key
, &ref2
->key
,
278 sizeof(ref1
->key
)) ||
279 ref1
->level
!= ref2
->level
||
280 ref1
->root_id
!= ref2
->root_id
)
282 ref1
->count
+= ref2
->count
;
284 if (ref1
->parent
!= ref2
->parent
)
286 ref1
->count
+= ref2
->count
;
288 list_del(&ref2
->list
);
297 * add all currently queued delayed refs from this head whose seq nr is
298 * smaller or equal that seq to the list
300 static int __add_delayed_refs(struct btrfs_delayed_ref_head
*head
, u64 seq
,
301 struct btrfs_key
*info_key
,
302 struct list_head
*prefs
)
304 struct btrfs_delayed_extent_op
*extent_op
= head
->extent_op
;
305 struct rb_node
*n
= &head
->node
.rb_node
;
309 if (extent_op
&& extent_op
->update_key
)
310 btrfs_disk_key_to_cpu(info_key
, &extent_op
->key
);
312 while ((n
= rb_prev(n
))) {
313 struct btrfs_delayed_ref_node
*node
;
314 node
= rb_entry(n
, struct btrfs_delayed_ref_node
,
316 if (node
->bytenr
!= head
->node
.bytenr
)
318 WARN_ON(node
->is_head
);
323 switch (node
->action
) {
324 case BTRFS_ADD_DELAYED_EXTENT
:
325 case BTRFS_UPDATE_DELAYED_HEAD
:
328 case BTRFS_ADD_DELAYED_REF
:
331 case BTRFS_DROP_DELAYED_REF
:
337 switch (node
->type
) {
338 case BTRFS_TREE_BLOCK_REF_KEY
: {
339 struct btrfs_delayed_tree_ref
*ref
;
341 ref
= btrfs_delayed_node_to_tree_ref(node
);
342 ret
= __add_prelim_ref(prefs
, ref
->root
, info_key
,
343 ref
->level
+ 1, 0, node
->bytenr
,
344 node
->ref_mod
* sgn
);
347 case BTRFS_SHARED_BLOCK_REF_KEY
: {
348 struct btrfs_delayed_tree_ref
*ref
;
350 ref
= btrfs_delayed_node_to_tree_ref(node
);
351 ret
= __add_prelim_ref(prefs
, ref
->root
, info_key
,
352 ref
->level
+ 1, ref
->parent
,
354 node
->ref_mod
* sgn
);
357 case BTRFS_EXTENT_DATA_REF_KEY
: {
358 struct btrfs_delayed_data_ref
*ref
;
359 struct btrfs_key key
;
361 ref
= btrfs_delayed_node_to_data_ref(node
);
363 key
.objectid
= ref
->objectid
;
364 key
.type
= BTRFS_EXTENT_DATA_KEY
;
365 key
.offset
= ref
->offset
;
366 ret
= __add_prelim_ref(prefs
, ref
->root
, &key
, 0, 0,
368 node
->ref_mod
* sgn
);
371 case BTRFS_SHARED_DATA_REF_KEY
: {
372 struct btrfs_delayed_data_ref
*ref
;
373 struct btrfs_key key
;
375 ref
= btrfs_delayed_node_to_data_ref(node
);
377 key
.objectid
= ref
->objectid
;
378 key
.type
= BTRFS_EXTENT_DATA_KEY
;
379 key
.offset
= ref
->offset
;
380 ret
= __add_prelim_ref(prefs
, ref
->root
, &key
, 0,
381 ref
->parent
, node
->bytenr
,
382 node
->ref_mod
* sgn
);
395 * add all inline backrefs for bytenr to the list
397 static int __add_inline_refs(struct btrfs_fs_info
*fs_info
,
398 struct btrfs_path
*path
, u64 bytenr
,
399 struct btrfs_key
*info_key
, int *info_level
,
400 struct list_head
*prefs
)
404 struct extent_buffer
*leaf
;
405 struct btrfs_key key
;
408 struct btrfs_extent_item
*ei
;
413 * enumerate all inline refs
415 leaf
= path
->nodes
[0];
416 slot
= path
->slots
[0];
418 item_size
= btrfs_item_size_nr(leaf
, slot
);
419 BUG_ON(item_size
< sizeof(*ei
));
421 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_item
);
422 flags
= btrfs_extent_flags(leaf
, ei
);
424 ptr
= (unsigned long)(ei
+ 1);
425 end
= (unsigned long)ei
+ item_size
;
427 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
428 struct btrfs_tree_block_info
*info
;
429 struct btrfs_disk_key disk_key
;
431 info
= (struct btrfs_tree_block_info
*)ptr
;
432 *info_level
= btrfs_tree_block_level(leaf
, info
);
433 btrfs_tree_block_key(leaf
, info
, &disk_key
);
434 btrfs_disk_key_to_cpu(info_key
, &disk_key
);
435 ptr
+= sizeof(struct btrfs_tree_block_info
);
438 BUG_ON(!(flags
& BTRFS_EXTENT_FLAG_DATA
));
442 struct btrfs_extent_inline_ref
*iref
;
446 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
447 type
= btrfs_extent_inline_ref_type(leaf
, iref
);
448 offset
= btrfs_extent_inline_ref_offset(leaf
, iref
);
451 case BTRFS_SHARED_BLOCK_REF_KEY
:
452 ret
= __add_prelim_ref(prefs
, 0, info_key
,
453 *info_level
+ 1, offset
,
456 case BTRFS_SHARED_DATA_REF_KEY
: {
457 struct btrfs_shared_data_ref
*sdref
;
460 sdref
= (struct btrfs_shared_data_ref
*)(iref
+ 1);
461 count
= btrfs_shared_data_ref_count(leaf
, sdref
);
462 ret
= __add_prelim_ref(prefs
, 0, NULL
, 0, offset
,
466 case BTRFS_TREE_BLOCK_REF_KEY
:
467 ret
= __add_prelim_ref(prefs
, offset
, info_key
,
468 *info_level
+ 1, 0, bytenr
, 1);
470 case BTRFS_EXTENT_DATA_REF_KEY
: {
471 struct btrfs_extent_data_ref
*dref
;
475 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
476 count
= btrfs_extent_data_ref_count(leaf
, dref
);
477 key
.objectid
= btrfs_extent_data_ref_objectid(leaf
,
479 key
.type
= BTRFS_EXTENT_DATA_KEY
;
480 key
.offset
= btrfs_extent_data_ref_offset(leaf
, dref
);
481 root
= btrfs_extent_data_ref_root(leaf
, dref
);
482 ret
= __add_prelim_ref(prefs
, root
, &key
, 0, 0, bytenr
,
490 ptr
+= btrfs_extent_inline_ref_size(type
);
497 * add all non-inline backrefs for bytenr to the list
499 static int __add_keyed_refs(struct btrfs_fs_info
*fs_info
,
500 struct btrfs_path
*path
, u64 bytenr
,
501 struct btrfs_key
*info_key
, int info_level
,
502 struct list_head
*prefs
)
504 struct btrfs_root
*extent_root
= fs_info
->extent_root
;
507 struct extent_buffer
*leaf
;
508 struct btrfs_key key
;
511 ret
= btrfs_next_item(extent_root
, path
);
519 slot
= path
->slots
[0];
520 leaf
= path
->nodes
[0];
521 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
523 if (key
.objectid
!= bytenr
)
525 if (key
.type
< BTRFS_TREE_BLOCK_REF_KEY
)
527 if (key
.type
> BTRFS_SHARED_DATA_REF_KEY
)
531 case BTRFS_SHARED_BLOCK_REF_KEY
:
532 ret
= __add_prelim_ref(prefs
, 0, info_key
,
533 info_level
+ 1, key
.offset
,
536 case BTRFS_SHARED_DATA_REF_KEY
: {
537 struct btrfs_shared_data_ref
*sdref
;
540 sdref
= btrfs_item_ptr(leaf
, slot
,
541 struct btrfs_shared_data_ref
);
542 count
= btrfs_shared_data_ref_count(leaf
, sdref
);
543 ret
= __add_prelim_ref(prefs
, 0, NULL
, 0, key
.offset
,
547 case BTRFS_TREE_BLOCK_REF_KEY
:
548 ret
= __add_prelim_ref(prefs
, key
.offset
, info_key
,
549 info_level
+ 1, 0, bytenr
, 1);
551 case BTRFS_EXTENT_DATA_REF_KEY
: {
552 struct btrfs_extent_data_ref
*dref
;
556 dref
= btrfs_item_ptr(leaf
, slot
,
557 struct btrfs_extent_data_ref
);
558 count
= btrfs_extent_data_ref_count(leaf
, dref
);
559 key
.objectid
= btrfs_extent_data_ref_objectid(leaf
,
561 key
.type
= BTRFS_EXTENT_DATA_KEY
;
562 key
.offset
= btrfs_extent_data_ref_offset(leaf
, dref
);
563 root
= btrfs_extent_data_ref_root(leaf
, dref
);
564 ret
= __add_prelim_ref(prefs
, root
, &key
, 0, 0,
578 * this adds all existing backrefs (inline backrefs, backrefs and delayed
579 * refs) for the given bytenr to the refs list, merges duplicates and resolves
580 * indirect refs to their parent bytenr.
581 * When roots are found, they're added to the roots list
583 * FIXME some caching might speed things up
585 static int find_parent_nodes(struct btrfs_trans_handle
*trans
,
586 struct btrfs_fs_info
*fs_info
, u64 bytenr
,
587 u64 seq
, struct ulist
*refs
, struct ulist
*roots
)
589 struct btrfs_key key
;
590 struct btrfs_path
*path
;
591 struct btrfs_key info_key
= { 0 };
592 struct btrfs_delayed_ref_root
*delayed_refs
= NULL
;
593 struct btrfs_delayed_ref_head
*head
;
596 int search_commit_root
= (trans
== BTRFS_BACKREF_SEARCH_COMMIT_ROOT
);
597 struct list_head prefs_delayed
;
598 struct list_head prefs
;
599 struct __prelim_ref
*ref
;
601 INIT_LIST_HEAD(&prefs
);
602 INIT_LIST_HEAD(&prefs_delayed
);
604 key
.objectid
= bytenr
;
605 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
606 key
.offset
= (u64
)-1;
608 path
= btrfs_alloc_path();
611 path
->search_commit_root
= !!search_commit_root
;
614 * grab both a lock on the path and a lock on the delayed ref head.
615 * We need both to get a consistent picture of how the refs look
616 * at a specified point in time
621 ret
= btrfs_search_slot(trans
, fs_info
->extent_root
, &key
, path
, 0, 0);
626 if (trans
!= BTRFS_BACKREF_SEARCH_COMMIT_ROOT
) {
628 * look if there are updates for this ref queued and lock the
631 delayed_refs
= &trans
->transaction
->delayed_refs
;
632 spin_lock(&delayed_refs
->lock
);
633 head
= btrfs_find_delayed_ref_head(trans
, bytenr
);
635 if (!mutex_trylock(&head
->mutex
)) {
636 atomic_inc(&head
->node
.refs
);
637 spin_unlock(&delayed_refs
->lock
);
639 btrfs_release_path(path
);
642 * Mutex was contended, block until it's
643 * released and try again
645 mutex_lock(&head
->mutex
);
646 mutex_unlock(&head
->mutex
);
647 btrfs_put_delayed_ref(&head
->node
);
650 ret
= __add_delayed_refs(head
, seq
, &info_key
,
653 spin_unlock(&delayed_refs
->lock
);
657 spin_unlock(&delayed_refs
->lock
);
660 if (path
->slots
[0]) {
661 struct extent_buffer
*leaf
;
665 leaf
= path
->nodes
[0];
666 slot
= path
->slots
[0];
667 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
668 if (key
.objectid
== bytenr
&&
669 key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
670 ret
= __add_inline_refs(fs_info
, path
, bytenr
,
671 &info_key
, &info_level
, &prefs
);
674 ret
= __add_keyed_refs(fs_info
, path
, bytenr
, &info_key
,
680 btrfs_release_path(path
);
683 * when adding the delayed refs above, the info_key might not have
684 * been known yet. Go over the list and replace the missing keys
686 list_for_each_entry(ref
, &prefs_delayed
, list
) {
687 if ((ref
->key
.offset
| ref
->key
.type
| ref
->key
.objectid
) == 0)
688 memcpy(&ref
->key
, &info_key
, sizeof(ref
->key
));
690 list_splice_init(&prefs_delayed
, &prefs
);
692 ret
= __merge_refs(&prefs
, 1);
696 ret
= __resolve_indirect_refs(fs_info
, search_commit_root
, &prefs
);
700 ret
= __merge_refs(&prefs
, 2);
704 while (!list_empty(&prefs
)) {
705 ref
= list_first_entry(&prefs
, struct __prelim_ref
, list
);
706 list_del(&ref
->list
);
709 if (ref
->count
&& ref
->root_id
&& ref
->parent
== 0) {
710 /* no parent == root of tree */
711 ret
= ulist_add(roots
, ref
->root_id
, 0, GFP_NOFS
);
714 if (ref
->count
&& ref
->parent
) {
715 ret
= ulist_add(refs
, ref
->parent
, 0, GFP_NOFS
);
723 mutex_unlock(&head
->mutex
);
724 btrfs_free_path(path
);
725 while (!list_empty(&prefs
)) {
726 ref
= list_first_entry(&prefs
, struct __prelim_ref
, list
);
727 list_del(&ref
->list
);
730 while (!list_empty(&prefs_delayed
)) {
731 ref
= list_first_entry(&prefs_delayed
, struct __prelim_ref
,
733 list_del(&ref
->list
);
741 * Finds all leafs with a reference to the specified combination of bytenr and
742 * offset. key_list_head will point to a list of corresponding keys (caller must
743 * free each list element). The leafs will be stored in the leafs ulist, which
744 * must be freed with ulist_free.
746 * returns 0 on success, <0 on error
748 static int btrfs_find_all_leafs(struct btrfs_trans_handle
*trans
,
749 struct btrfs_fs_info
*fs_info
, u64 bytenr
,
750 u64 num_bytes
, u64 seq
, struct ulist
**leafs
)
755 tmp
= ulist_alloc(GFP_NOFS
);
758 *leafs
= ulist_alloc(GFP_NOFS
);
764 ret
= find_parent_nodes(trans
, fs_info
, bytenr
, seq
, *leafs
, tmp
);
767 if (ret
< 0 && ret
!= -ENOENT
) {
776 * walk all backrefs for a given extent to find all roots that reference this
777 * extent. Walking a backref means finding all extents that reference this
778 * extent and in turn walk the backrefs of those, too. Naturally this is a
779 * recursive process, but here it is implemented in an iterative fashion: We
780 * find all referencing extents for the extent in question and put them on a
781 * list. In turn, we find all referencing extents for those, further appending
782 * to the list. The way we iterate the list allows adding more elements after
783 * the current while iterating. The process stops when we reach the end of the
784 * list. Found roots are added to the roots list.
786 * returns 0 on success, < 0 on error.
788 int btrfs_find_all_roots(struct btrfs_trans_handle
*trans
,
789 struct btrfs_fs_info
*fs_info
, u64 bytenr
,
790 u64 num_bytes
, u64 seq
, struct ulist
**roots
)
793 struct ulist_node
*node
= NULL
;
794 struct ulist_iterator uiter
;
797 tmp
= ulist_alloc(GFP_NOFS
);
800 *roots
= ulist_alloc(GFP_NOFS
);
806 ULIST_ITER_INIT(&uiter
);
808 ret
= find_parent_nodes(trans
, fs_info
, bytenr
, seq
,
810 if (ret
< 0 && ret
!= -ENOENT
) {
815 node
= ulist_next(tmp
, &uiter
);
826 static int __inode_info(u64 inum
, u64 ioff
, u8 key_type
,
827 struct btrfs_root
*fs_root
, struct btrfs_path
*path
,
828 struct btrfs_key
*found_key
)
831 struct btrfs_key key
;
832 struct extent_buffer
*eb
;
838 ret
= btrfs_search_slot(NULL
, fs_root
, &key
, path
, 0, 0);
843 if (ret
&& path
->slots
[0] >= btrfs_header_nritems(eb
)) {
844 ret
= btrfs_next_leaf(fs_root
, path
);
850 btrfs_item_key_to_cpu(eb
, found_key
, path
->slots
[0]);
851 if (found_key
->type
!= key
.type
|| found_key
->objectid
!= key
.objectid
)
858 * this makes the path point to (inum INODE_ITEM ioff)
860 int inode_item_info(u64 inum
, u64 ioff
, struct btrfs_root
*fs_root
,
861 struct btrfs_path
*path
)
863 struct btrfs_key key
;
864 return __inode_info(inum
, ioff
, BTRFS_INODE_ITEM_KEY
, fs_root
, path
,
868 static int inode_ref_info(u64 inum
, u64 ioff
, struct btrfs_root
*fs_root
,
869 struct btrfs_path
*path
,
870 struct btrfs_key
*found_key
)
872 return __inode_info(inum
, ioff
, BTRFS_INODE_REF_KEY
, fs_root
, path
,
877 * this iterates to turn a btrfs_inode_ref into a full filesystem path. elements
878 * of the path are separated by '/' and the path is guaranteed to be
879 * 0-terminated. the path is only given within the current file system.
880 * Therefore, it never starts with a '/'. the caller is responsible to provide
881 * "size" bytes in "dest". the dest buffer will be filled backwards. finally,
882 * the start point of the resulting string is returned. this pointer is within
884 * in case the path buffer would overflow, the pointer is decremented further
885 * as if output was written to the buffer, though no more output is actually
886 * generated. that way, the caller can determine how much space would be
887 * required for the path to fit into the buffer. in that case, the returned
888 * value will be smaller than dest. callers must check this!
890 static char *iref_to_path(struct btrfs_root
*fs_root
, struct btrfs_path
*path
,
891 struct btrfs_inode_ref
*iref
,
892 struct extent_buffer
*eb_in
, u64 parent
,
893 char *dest
, u32 size
)
899 s64 bytes_left
= size
- 1;
900 struct extent_buffer
*eb
= eb_in
;
901 struct btrfs_key found_key
;
902 int leave_spinning
= path
->leave_spinning
;
905 dest
[bytes_left
] = '\0';
907 path
->leave_spinning
= 1;
909 len
= btrfs_inode_ref_name_len(eb
, iref
);
912 read_extent_buffer(eb
, dest
+ bytes_left
,
913 (unsigned long)(iref
+ 1), len
);
915 btrfs_tree_read_unlock_blocking(eb
);
916 free_extent_buffer(eb
);
918 ret
= inode_ref_info(parent
, 0, fs_root
, path
, &found_key
);
923 next_inum
= found_key
.offset
;
925 /* regular exit ahead */
926 if (parent
== next_inum
)
929 slot
= path
->slots
[0];
931 /* make sure we can use eb after releasing the path */
933 atomic_inc(&eb
->refs
);
934 btrfs_tree_read_lock(eb
);
935 btrfs_set_lock_blocking_rw(eb
, BTRFS_READ_LOCK
);
937 btrfs_release_path(path
);
939 iref
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_ref
);
943 dest
[bytes_left
] = '/';
946 btrfs_release_path(path
);
947 path
->leave_spinning
= leave_spinning
;
952 return dest
+ bytes_left
;
956 * this makes the path point to (logical EXTENT_ITEM *)
957 * returns BTRFS_EXTENT_FLAG_DATA for data, BTRFS_EXTENT_FLAG_TREE_BLOCK for
958 * tree blocks and <0 on error.
960 int extent_from_logical(struct btrfs_fs_info
*fs_info
, u64 logical
,
961 struct btrfs_path
*path
, struct btrfs_key
*found_key
)
966 struct extent_buffer
*eb
;
967 struct btrfs_extent_item
*ei
;
968 struct btrfs_key key
;
970 key
.type
= BTRFS_EXTENT_ITEM_KEY
;
971 key
.objectid
= logical
;
972 key
.offset
= (u64
)-1;
974 ret
= btrfs_search_slot(NULL
, fs_info
->extent_root
, &key
, path
, 0, 0);
977 ret
= btrfs_previous_item(fs_info
->extent_root
, path
,
978 0, BTRFS_EXTENT_ITEM_KEY
);
982 btrfs_item_key_to_cpu(path
->nodes
[0], found_key
, path
->slots
[0]);
983 if (found_key
->type
!= BTRFS_EXTENT_ITEM_KEY
||
984 found_key
->objectid
> logical
||
985 found_key
->objectid
+ found_key
->offset
<= logical
) {
986 pr_debug("logical %llu is not within any extent\n",
987 (unsigned long long)logical
);
992 item_size
= btrfs_item_size_nr(eb
, path
->slots
[0]);
993 BUG_ON(item_size
< sizeof(*ei
));
995 ei
= btrfs_item_ptr(eb
, path
->slots
[0], struct btrfs_extent_item
);
996 flags
= btrfs_extent_flags(eb
, ei
);
998 pr_debug("logical %llu is at position %llu within the extent (%llu "
999 "EXTENT_ITEM %llu) flags %#llx size %u\n",
1000 (unsigned long long)logical
,
1001 (unsigned long long)(logical
- found_key
->objectid
),
1002 (unsigned long long)found_key
->objectid
,
1003 (unsigned long long)found_key
->offset
,
1004 (unsigned long long)flags
, item_size
);
1005 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
1006 return BTRFS_EXTENT_FLAG_TREE_BLOCK
;
1007 if (flags
& BTRFS_EXTENT_FLAG_DATA
)
1008 return BTRFS_EXTENT_FLAG_DATA
;
1014 * helper function to iterate extent inline refs. ptr must point to a 0 value
1015 * for the first call and may be modified. it is used to track state.
1016 * if more refs exist, 0 is returned and the next call to
1017 * __get_extent_inline_ref must pass the modified ptr parameter to get the
1018 * next ref. after the last ref was processed, 1 is returned.
1019 * returns <0 on error
1021 static int __get_extent_inline_ref(unsigned long *ptr
, struct extent_buffer
*eb
,
1022 struct btrfs_extent_item
*ei
, u32 item_size
,
1023 struct btrfs_extent_inline_ref
**out_eiref
,
1028 struct btrfs_tree_block_info
*info
;
1032 flags
= btrfs_extent_flags(eb
, ei
);
1033 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1034 info
= (struct btrfs_tree_block_info
*)(ei
+ 1);
1036 (struct btrfs_extent_inline_ref
*)(info
+ 1);
1038 *out_eiref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
1040 *ptr
= (unsigned long)*out_eiref
;
1041 if ((void *)*ptr
>= (void *)ei
+ item_size
)
1045 end
= (unsigned long)ei
+ item_size
;
1046 *out_eiref
= (struct btrfs_extent_inline_ref
*)*ptr
;
1047 *out_type
= btrfs_extent_inline_ref_type(eb
, *out_eiref
);
1049 *ptr
+= btrfs_extent_inline_ref_size(*out_type
);
1050 WARN_ON(*ptr
> end
);
1052 return 1; /* last */
1058 * reads the tree block backref for an extent. tree level and root are returned
1059 * through out_level and out_root. ptr must point to a 0 value for the first
1060 * call and may be modified (see __get_extent_inline_ref comment).
1061 * returns 0 if data was provided, 1 if there was no more data to provide or
1064 int tree_backref_for_extent(unsigned long *ptr
, struct extent_buffer
*eb
,
1065 struct btrfs_extent_item
*ei
, u32 item_size
,
1066 u64
*out_root
, u8
*out_level
)
1070 struct btrfs_tree_block_info
*info
;
1071 struct btrfs_extent_inline_ref
*eiref
;
1073 if (*ptr
== (unsigned long)-1)
1077 ret
= __get_extent_inline_ref(ptr
, eb
, ei
, item_size
,
1082 if (type
== BTRFS_TREE_BLOCK_REF_KEY
||
1083 type
== BTRFS_SHARED_BLOCK_REF_KEY
)
1090 /* we can treat both ref types equally here */
1091 info
= (struct btrfs_tree_block_info
*)(ei
+ 1);
1092 *out_root
= btrfs_extent_inline_ref_offset(eb
, eiref
);
1093 *out_level
= btrfs_tree_block_level(eb
, info
);
1096 *ptr
= (unsigned long)-1;
1101 static int iterate_leaf_refs(struct btrfs_fs_info
*fs_info
, u64 logical
,
1102 u64 orig_extent_item_objectid
,
1103 u64 extent_item_pos
, u64 root
,
1104 iterate_extent_inodes_t
*iterate
, void *ctx
)
1107 struct btrfs_key key
;
1108 struct btrfs_file_extent_item
*fi
;
1109 struct extent_buffer
*eb
;
1117 eb
= read_tree_block(fs_info
->tree_root
, logical
,
1118 fs_info
->tree_root
->leafsize
, 0);
1123 * from the shared data ref, we only have the leaf but we need
1124 * the key. thus, we must look into all items and see that we
1125 * find one (some) with a reference to our extent item.
1127 nritems
= btrfs_header_nritems(eb
);
1128 for (slot
= 0; slot
< nritems
; ++slot
) {
1129 btrfs_item_key_to_cpu(eb
, &key
, slot
);
1130 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
1132 fi
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
1133 extent_type
= btrfs_file_extent_type(eb
, fi
);
1134 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
)
1136 /* don't skip BTRFS_FILE_EXTENT_PREALLOC, we can handle that */
1137 disk_byte
= btrfs_file_extent_disk_bytenr(eb
, fi
);
1138 if (disk_byte
!= orig_extent_item_objectid
)
1141 data_offset
= btrfs_file_extent_offset(eb
, fi
);
1142 data_len
= btrfs_file_extent_num_bytes(eb
, fi
);
1144 if (extent_item_pos
< data_offset
||
1145 extent_item_pos
>= data_offset
+ data_len
)
1148 pr_debug("ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
1149 "root %llu\n", orig_extent_item_objectid
,
1150 key
.objectid
, key
.offset
, root
);
1151 ret
= iterate(key
.objectid
,
1152 key
.offset
+ (extent_item_pos
- data_offset
),
1155 pr_debug("stopping iteration because ret=%d\n", ret
);
1160 free_extent_buffer(eb
);
1166 * calls iterate() for every inode that references the extent identified by
1167 * the given parameters.
1168 * when the iterator function returns a non-zero value, iteration stops.
1170 int iterate_extent_inodes(struct btrfs_fs_info
*fs_info
,
1171 u64 extent_item_objectid
, u64 extent_item_pos
,
1172 int search_commit_root
,
1173 iterate_extent_inodes_t
*iterate
, void *ctx
)
1176 struct list_head data_refs
= LIST_HEAD_INIT(data_refs
);
1177 struct list_head shared_refs
= LIST_HEAD_INIT(shared_refs
);
1178 struct btrfs_trans_handle
*trans
;
1179 struct ulist
*refs
= NULL
;
1180 struct ulist
*roots
= NULL
;
1181 struct ulist_node
*ref_node
= NULL
;
1182 struct ulist_node
*root_node
= NULL
;
1183 struct seq_list seq_elem
;
1184 struct ulist_iterator ref_uiter
;
1185 struct ulist_iterator root_uiter
;
1186 struct btrfs_delayed_ref_root
*delayed_refs
= NULL
;
1188 pr_debug("resolving all inodes for extent %llu\n",
1189 extent_item_objectid
);
1191 if (search_commit_root
) {
1192 trans
= BTRFS_BACKREF_SEARCH_COMMIT_ROOT
;
1194 trans
= btrfs_join_transaction(fs_info
->extent_root
);
1196 return PTR_ERR(trans
);
1198 delayed_refs
= &trans
->transaction
->delayed_refs
;
1199 spin_lock(&delayed_refs
->lock
);
1200 btrfs_get_delayed_seq(delayed_refs
, &seq_elem
);
1201 spin_unlock(&delayed_refs
->lock
);
1204 ret
= btrfs_find_all_leafs(trans
, fs_info
, extent_item_objectid
,
1205 extent_item_pos
, seq_elem
.seq
,
1211 ULIST_ITER_INIT(&ref_uiter
);
1212 while (!ret
&& (ref_node
= ulist_next(refs
, &ref_uiter
))) {
1213 ret
= btrfs_find_all_roots(trans
, fs_info
, ref_node
->val
, -1,
1214 seq_elem
.seq
, &roots
);
1217 ULIST_ITER_INIT(&root_uiter
);
1218 while (!ret
&& (root_node
= ulist_next(roots
, &root_uiter
))) {
1219 pr_debug("root %llu references leaf %llu\n",
1220 root_node
->val
, ref_node
->val
);
1221 ret
= iterate_leaf_refs(fs_info
, ref_node
->val
,
1222 extent_item_objectid
,
1223 extent_item_pos
, root_node
->val
,
1231 if (!search_commit_root
) {
1232 btrfs_put_delayed_seq(delayed_refs
, &seq_elem
);
1233 btrfs_end_transaction(trans
, fs_info
->extent_root
);
1239 int iterate_inodes_from_logical(u64 logical
, struct btrfs_fs_info
*fs_info
,
1240 struct btrfs_path
*path
,
1241 iterate_extent_inodes_t
*iterate
, void *ctx
)
1244 u64 extent_item_pos
;
1245 struct btrfs_key found_key
;
1246 int search_commit_root
= path
->search_commit_root
;
1248 ret
= extent_from_logical(fs_info
, logical
, path
,
1250 btrfs_release_path(path
);
1251 if (ret
& BTRFS_EXTENT_FLAG_TREE_BLOCK
)
1256 extent_item_pos
= logical
- found_key
.objectid
;
1257 ret
= iterate_extent_inodes(fs_info
, found_key
.objectid
,
1258 extent_item_pos
, search_commit_root
,
1264 static int iterate_irefs(u64 inum
, struct btrfs_root
*fs_root
,
1265 struct btrfs_path
*path
,
1266 iterate_irefs_t
*iterate
, void *ctx
)
1275 struct extent_buffer
*eb
;
1276 struct btrfs_item
*item
;
1277 struct btrfs_inode_ref
*iref
;
1278 struct btrfs_key found_key
;
1281 path
->leave_spinning
= 1;
1282 ret
= inode_ref_info(inum
, parent
? parent
+1 : 0, fs_root
, path
,
1287 ret
= found
? 0 : -ENOENT
;
1292 parent
= found_key
.offset
;
1293 slot
= path
->slots
[0];
1294 eb
= path
->nodes
[0];
1295 /* make sure we can use eb after releasing the path */
1296 atomic_inc(&eb
->refs
);
1297 btrfs_tree_read_lock(eb
);
1298 btrfs_set_lock_blocking_rw(eb
, BTRFS_READ_LOCK
);
1299 btrfs_release_path(path
);
1301 item
= btrfs_item_nr(eb
, slot
);
1302 iref
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_ref
);
1304 for (cur
= 0; cur
< btrfs_item_size(eb
, item
); cur
+= len
) {
1305 name_len
= btrfs_inode_ref_name_len(eb
, iref
);
1306 /* path must be released before calling iterate()! */
1307 pr_debug("following ref at offset %u for inode %llu in "
1309 (unsigned long long)found_key
.objectid
,
1310 (unsigned long long)fs_root
->objectid
);
1311 ret
= iterate(parent
, iref
, eb
, ctx
);
1314 len
= sizeof(*iref
) + name_len
;
1315 iref
= (struct btrfs_inode_ref
*)((char *)iref
+ len
);
1317 btrfs_tree_read_unlock_blocking(eb
);
1318 free_extent_buffer(eb
);
1321 btrfs_release_path(path
);
1327 * returns 0 if the path could be dumped (probably truncated)
1328 * returns <0 in case of an error
1330 static int inode_to_path(u64 inum
, struct btrfs_inode_ref
*iref
,
1331 struct extent_buffer
*eb
, void *ctx
)
1333 struct inode_fs_paths
*ipath
= ctx
;
1336 int i
= ipath
->fspath
->elem_cnt
;
1337 const int s_ptr
= sizeof(char *);
1340 bytes_left
= ipath
->fspath
->bytes_left
> s_ptr
?
1341 ipath
->fspath
->bytes_left
- s_ptr
: 0;
1343 fspath_min
= (char *)ipath
->fspath
->val
+ (i
+ 1) * s_ptr
;
1344 fspath
= iref_to_path(ipath
->fs_root
, ipath
->btrfs_path
, iref
, eb
,
1345 inum
, fspath_min
, bytes_left
);
1347 return PTR_ERR(fspath
);
1349 if (fspath
> fspath_min
) {
1350 pr_debug("path resolved: %s\n", fspath
);
1351 ipath
->fspath
->val
[i
] = (u64
)(unsigned long)fspath
;
1352 ++ipath
->fspath
->elem_cnt
;
1353 ipath
->fspath
->bytes_left
= fspath
- fspath_min
;
1355 pr_debug("missed path, not enough space. missing bytes: %lu, "
1356 "constructed so far: %s\n",
1357 (unsigned long)(fspath_min
- fspath
), fspath_min
);
1358 ++ipath
->fspath
->elem_missed
;
1359 ipath
->fspath
->bytes_missing
+= fspath_min
- fspath
;
1360 ipath
->fspath
->bytes_left
= 0;
1367 * this dumps all file system paths to the inode into the ipath struct, provided
1368 * is has been created large enough. each path is zero-terminated and accessed
1369 * from ipath->fspath->val[i].
1370 * when it returns, there are ipath->fspath->elem_cnt number of paths available
1371 * in ipath->fspath->val[]. when the allocated space wasn't sufficient, the
1372 * number of missed paths in recored in ipath->fspath->elem_missed, otherwise,
1373 * it's zero. ipath->fspath->bytes_missing holds the number of bytes that would
1374 * have been needed to return all paths.
1376 int paths_from_inode(u64 inum
, struct inode_fs_paths
*ipath
)
1378 return iterate_irefs(inum
, ipath
->fs_root
, ipath
->btrfs_path
,
1379 inode_to_path
, ipath
);
1382 struct btrfs_data_container
*init_data_container(u32 total_bytes
)
1384 struct btrfs_data_container
*data
;
1387 alloc_bytes
= max_t(size_t, total_bytes
, sizeof(*data
));
1388 data
= kmalloc(alloc_bytes
, GFP_NOFS
);
1390 return ERR_PTR(-ENOMEM
);
1392 if (total_bytes
>= sizeof(*data
)) {
1393 data
->bytes_left
= total_bytes
- sizeof(*data
);
1394 data
->bytes_missing
= 0;
1396 data
->bytes_missing
= sizeof(*data
) - total_bytes
;
1397 data
->bytes_left
= 0;
1401 data
->elem_missed
= 0;
1407 * allocates space to return multiple file system paths for an inode.
1408 * total_bytes to allocate are passed, note that space usable for actual path
1409 * information will be total_bytes - sizeof(struct inode_fs_paths).
1410 * the returned pointer must be freed with free_ipath() in the end.
1412 struct inode_fs_paths
*init_ipath(s32 total_bytes
, struct btrfs_root
*fs_root
,
1413 struct btrfs_path
*path
)
1415 struct inode_fs_paths
*ifp
;
1416 struct btrfs_data_container
*fspath
;
1418 fspath
= init_data_container(total_bytes
);
1420 return (void *)fspath
;
1422 ifp
= kmalloc(sizeof(*ifp
), GFP_NOFS
);
1425 return ERR_PTR(-ENOMEM
);
1428 ifp
->btrfs_path
= path
;
1429 ifp
->fspath
= fspath
;
1430 ifp
->fs_root
= fs_root
;
1435 void free_ipath(struct inode_fs_paths
*ipath
)
1439 kfree(ipath
->fspath
);