43 data_offset = btrfs_file_extent_offset(eb, fi);
44 data_len = btrfs_file_extent_num_bytes(eb, fi);
46 if (extent_item_pos < data_offset ||
47 extent_item_pos >= data_offset + data_len)
79 nritems = btrfs_header_nritems(eb);
81 btrfs_item_key_to_cpu(eb, &
key, slot);
85 extent_type = btrfs_file_extent_type(eb, fi);
89 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
90 if (disk_byte != wanted_disk_byte)
93 ret = check_extent_in_eb(&
key, eb, fi, extent_item_pos, eie);
184 u64 wanted_disk_byte,
185 const u64 *extent_item_pos)
208 if (path->
slots[0] >= btrfs_header_nritems(path->
nodes[0]))
213 slot = path->
slots[0];
215 btrfs_item_key_to_cpu(eb, &
key, slot);
222 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
224 if (disk_byte == wanted_disk_byte) {
226 if (extent_item_pos) {
227 ret = check_extent_in_eb(&
key, eb, fi,
238 if (!extent_item_pos) {
245 ret = btrfs_next_old_item(root, path, time_seq);
258 int search_commit_root,
261 struct ulist *parents,
262 const u64 *extent_item_pos)
270 int level = ref->
level;
288 if (root_level + 1 == level)
293 pr_debug(
"search slot in root %llu (level %d, ref count %d) returned "
294 "%d for key (%llu %u %llu)\n",
295 (
unsigned long long)ref->
root_id, level, ref->
count, ret,
313 ret = add_all_parents(root, path, parents, level, &ref->
key_for_search,
324 static int __resolve_indirect_refs(
struct btrfs_fs_info *fs_info,
325 int search_commit_root,
u64 time_seq,
327 const u64 *extent_item_pos)
334 struct ulist *parents;
352 err = __resolve_indirect_ref(fs_info, search_commit_root,
353 time_seq, ref, parents,
369 while ((node =
ulist_next(parents, &uiter))) {
375 memcpy(new_ref, ref,
sizeof(*ref));
379 list_add(&new_ref->
list, &ref->
list);
388 static inline int ref_for_same_block(
struct __prelim_ref *ref1,
429 if (btrfs_header_level(eb) == 0)
460 for (pos2 = pos1->
next, n2 = pos2->
next; pos2 != head;
461 pos2 = n2, n2 = pos2->
next) {
468 if (!ref_for_same_block(ref1, ref2))
504 btrfs_disk_key_to_cpu(&op_key, &extent_op->
key);
531 switch (node->
type) {
535 ref = btrfs_delayed_node_to_tree_ref(node);
536 ret = __add_prelim_ref(prefs, ref->
root, &op_key,
544 ref = btrfs_delayed_node_to_tree_ref(node);
545 ret = __add_prelim_ref(prefs, ref->
root,
NULL,
553 ref = btrfs_delayed_node_to_data_ref(node);
558 ret = __add_prelim_ref(prefs, ref->
root, &
key, 0, 0,
566 ref = btrfs_delayed_node_to_data_ref(node);
571 ret = __add_prelim_ref(prefs, ref->
root, &
key, 0,
590 int *info_level,
struct list_head *prefs)
605 leaf = path->
nodes[0];
606 slot = path->
slots[0];
608 item_size = btrfs_item_size_nr(leaf, slot);
609 BUG_ON(item_size <
sizeof(*ei));
612 flags = btrfs_extent_flags(leaf, ei);
614 ptr = (
unsigned long)(ei + 1);
615 end = (
unsigned long)ei + item_size;
621 *info_level = btrfs_tree_block_level(leaf, info);
634 type = btrfs_extent_inline_ref_type(leaf, iref);
635 offset = btrfs_extent_inline_ref_offset(leaf, iref);
639 ret = __add_prelim_ref(prefs, 0,
NULL,
640 *info_level + 1, offset,
648 count = btrfs_shared_data_ref_count(leaf, sdref);
649 ret = __add_prelim_ref(prefs, 0,
NULL, 0, offset,
654 ret = __add_prelim_ref(prefs, offset,
NULL,
664 count = btrfs_extent_data_ref_count(leaf, dref);
665 key.objectid = btrfs_extent_data_ref_objectid(leaf,
668 key.offset = btrfs_extent_data_ref_offset(leaf, dref);
669 root = btrfs_extent_data_ref_root(leaf, dref);
670 ret = __add_prelim_ref(prefs, root, &
key, 0, 0,
678 ptr += btrfs_extent_inline_ref_size(type);
698 ret = btrfs_next_item(extent_root, path);
706 slot = path->
slots[0];
707 leaf = path->
nodes[0];
708 btrfs_item_key_to_cpu(leaf, &
key, slot);
710 if (
key.objectid != bytenr)
719 ret = __add_prelim_ref(prefs, 0,
NULL,
720 info_level + 1,
key.offset,
729 count = btrfs_shared_data_ref_count(leaf, sdref);
730 ret = __add_prelim_ref(prefs, 0,
NULL, 0,
key.offset,
735 ret = __add_prelim_ref(prefs,
key.offset,
NULL,
746 count = btrfs_extent_data_ref_count(leaf, dref);
747 key.objectid = btrfs_extent_data_ref_objectid(leaf,
750 key.offset = btrfs_extent_data_ref_offset(leaf, dref);
751 root = btrfs_extent_data_ref_root(leaf, dref);
752 ret = __add_prelim_ref(prefs, root, &
key, 0, 0,
776 struct ulist *roots,
const u64 *extent_item_pos)
789 INIT_LIST_HEAD(&prefs);
790 INIT_LIST_HEAD(&prefs_delayed);
820 spin_lock(&delayed_refs->
lock);
825 spin_unlock(&delayed_refs->
lock);
835 btrfs_put_delayed_ref(&head->
node);
838 ret = __add_delayed_refs(head, time_seq,
842 spin_unlock(&delayed_refs->
lock);
846 spin_unlock(&delayed_refs->
lock);
849 if (path->
slots[0]) {
854 leaf = path->
nodes[0];
855 slot = path->
slots[0];
856 btrfs_item_key_to_cpu(leaf, &
key, slot);
857 if (
key.objectid == bytenr &&
859 ret = __add_inline_refs(fs_info, path, bytenr,
860 &info_level, &prefs);
863 ret = __add_keyed_refs(fs_info, path, bytenr,
871 list_splice_init(&prefs_delayed, &prefs);
873 ret = __add_missing_keys(fs_info, &prefs);
877 ret = __merge_refs(&prefs, 1);
881 ret = __resolve_indirect_refs(fs_info, search_commit_root, time_seq,
882 &prefs, extent_item_pos);
886 ret = __merge_refs(&prefs, 2);
890 while (!list_empty(&prefs)) {
910 ret = find_extent_in_eb(eb, bytenr,
911 *extent_item_pos, &eie);
918 if (!ret && extent_item_pos) {
935 while (!list_empty(&prefs)) {
940 while (!list_empty(&prefs_delayed)) {
950 static void free_leaf_list(
struct ulist *blocks)
962 for (; eie; eie = eie_next) {
963 eie_next = eie->
next;
983 const u64 *extent_item_pos)
997 ret = find_parent_nodes(trans, fs_info, bytenr,
998 time_seq, *leafs, tmp, extent_item_pos);
1001 if (ret < 0 && ret != -
ENOENT) {
1002 free_leaf_list(*leafs);
1024 u64 time_seq,
struct ulist **roots)
1042 ret = find_parent_nodes(trans, fs_info, bytenr,
1043 time_seq, tmp, *roots,
NULL);
1044 if (ret < 0 && ret != -
ENOENT) {
1060 static int __inode_info(
u64 inum,
u64 ioff,
u8 key_type,
1068 key.type = key_type;
1076 eb = path->
nodes[0];
1077 if (ret && path->
slots[0] >= btrfs_header_nritems(eb)) {
1081 eb = path->
nodes[0];
1084 btrfs_item_key_to_cpu(eb, found_key, path->
slots[0]);
1131 leaf = path->
nodes[0];
1132 slot = path->
slots[0];
1133 if (slot >= btrfs_header_nritems(leaf)) {
1152 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1161 if (found_key.
objectid != inode_objectid)
1169 *ret_extref = extref;
1171 *found_off = found_key.
offset;
1186 s64 bytes_left = ((
s64)size) - 1;
1192 if (bytes_left >= 0)
1193 dest[bytes_left] =
'\0';
1198 if (bytes_left >= 0)
1200 name_off, name_len);
1205 ret = inode_ref_info(parent, 0, fs_root, path, &found_key);
1211 next_inum = found_key.
offset;
1214 if (parent == next_inum)
1217 slot = path->
slots[0];
1218 eb = path->
nodes[0];
1228 name_len = btrfs_inode_ref_name_len(eb, iref);
1229 name_off = (
unsigned long)(iref + 1);
1233 if (bytes_left >= 0)
1234 dest[bytes_left] =
'/';
1241 return ERR_PTR(ret);
1243 return dest + bytes_left;
1267 btrfs_inode_ref_name_len(eb_in, iref),
1268 (
unsigned long)(iref + 1),
1269 eb_in, parent, dest, size);
1300 btrfs_item_key_to_cpu(path->
nodes[0], found_key, path->
slots[0]);
1304 pr_debug(
"logical %llu is not within any extent\n",
1305 (
unsigned long long)logical);
1309 eb = path->
nodes[0];
1310 item_size = btrfs_item_size_nr(eb, path->
slots[0]);
1311 BUG_ON(item_size <
sizeof(*ei));
1314 flags = btrfs_extent_flags(eb, ei);
1316 pr_debug(
"logical %llu is at position %llu within the extent (%llu "
1317 "EXTENT_ITEM %llu) flags %#llx size %u\n",
1318 (
unsigned long long)logical,
1319 (
unsigned long long)(logical - found_key->
objectid),
1320 (
unsigned long long)found_key->
objectid,
1321 (
unsigned long long)found_key->
offset,
1322 (
unsigned long long)flags, item_size);
1326 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1346 static int __get_extent_inline_ref(
unsigned long *ptr,
struct extent_buffer *eb,
1357 flags = btrfs_extent_flags(eb, ei);
1358 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1365 *ptr = (
unsigned long)*out_eiref;
1366 if ((
void *)*ptr >= (
void *)ei + item_size)
1370 end = (
unsigned long)ei + item_size;
1372 *out_type = btrfs_extent_inline_ref_type(eb, *out_eiref);
1374 *ptr += btrfs_extent_inline_ref_size(*out_type);
1398 if (*ptr == (
unsigned long)-1)
1402 ret = __get_extent_inline_ref(ptr, eb, ei, item_size,
1417 *out_root = btrfs_extent_inline_ref_offset(eb, eiref);
1418 *out_level = btrfs_tree_block_level(eb, info);
1421 *ptr = (
unsigned long)-1;
1427 u64 root,
u64 extent_item_objectid,
1433 for (eie = inode_list; eie; eie = eie->
next) {
1434 pr_debug(
"ref for %llu resolved, key (%llu EXTEND_DATA %llu), "
1435 "root %llu\n", extent_item_objectid,
1437 ret = iterate(eie->
inum, eie->
offset, root, ctx);
1439 pr_debug(
"stopping iteration for %llu due to ret=%d\n",
1440 extent_item_objectid, ret);
1454 u64 extent_item_objectid,
u64 extent_item_pos,
1455 int search_commit_root,
1466 struct seq_list tree_mod_seq_elem = {};
1470 pr_debug(
"resolving all inodes for extent %llu\n",
1471 extent_item_objectid);
1473 if (search_commit_root) {
1478 return PTR_ERR(trans);
1482 ret = btrfs_find_all_leafs(trans, fs_info, extent_item_objectid,
1483 tree_mod_seq_elem.
seq, &refs,
1489 while (!ret && (ref_node =
ulist_next(refs, &ref_uiter))) {
1491 tree_mod_seq_elem.
seq, &roots);
1495 while (!ret && (root_node =
ulist_next(roots, &root_uiter))) {
1496 pr_debug(
"root %llu references leaf %llu, data list "
1497 "%#llx\n", root_node->
val, ref_node->
val,
1498 (
long long)ref_node->
aux);
1502 extent_item_objectid,
1509 free_leaf_list(refs);
1512 if (!search_commit_root) {
1525 u64 extent_item_pos;
1534 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
1537 extent_item_pos = logical - found_key.
objectid;
1539 extent_item_pos, search_commit_root,
1548 static int iterate_inode_refs(
u64 inum,
struct btrfs_root *fs_root,
1566 ret = inode_ref_info(inum, parent ? parent+1 : 0, fs_root, path,
1571 ret = found ? 0 : -
ENOENT;
1576 parent = found_key.
offset;
1577 slot = path->
slots[0];
1578 eb = path->
nodes[0];
1585 item = btrfs_item_nr(eb, slot);
1588 for (cur = 0; cur < btrfs_item_size(eb, item); cur += len) {
1589 name_len = btrfs_inode_ref_name_len(eb, iref);
1591 pr_debug(
"following ref at offset %u for inode %llu in "
1593 (
unsigned long long)found_key.
objectid,
1594 (
unsigned long long)fs_root->
objectid);
1595 ret = iterate(parent, name_len,
1596 (
unsigned long)(iref + 1), eb, ctx);
1611 static int iterate_inode_extrefs(
u64 inum,
struct btrfs_root *fs_root,
1633 ret = found ? 0 : -
ENOENT;
1638 slot = path->
slots[0];
1639 eb = path->
nodes[0];
1647 leaf = path->
nodes[0];
1648 item_size = btrfs_item_size_nr(leaf, path->
slots[0]);
1652 while (cur_offset < item_size) {
1656 parent = btrfs_inode_extref_parent(eb, extref);
1657 name_len = btrfs_inode_extref_name_len(eb, extref);
1658 ret = iterate(parent, name_len,
1659 (
unsigned long)&extref->
name, eb, ctx);
1663 cur_offset += btrfs_inode_extref_name_len(leaf, extref);
1664 cur_offset +=
sizeof(*extref);
1677 static int iterate_irefs(
u64 inum,
struct btrfs_root *fs_root,
1684 ret = iterate_inode_refs(inum, fs_root, path, iterate, ctx);
1690 ret = iterate_inode_extrefs(inum, fs_root, path, iterate, ctx);
1691 if (ret == -
ENOENT && found_refs)
1701 static int inode_to_path(
u64 inum,
u32 name_len,
unsigned long name_off,
1707 int i = ipath->
fspath->elem_cnt;
1708 const int s_ptr =
sizeof(
char *);
1711 bytes_left = ipath->
fspath->bytes_left > s_ptr ?
1712 ipath->
fspath->bytes_left - s_ptr : 0;
1714 fspath_min = (
char *)ipath->
fspath->val + (i + 1) * s_ptr;
1716 name_off, eb, inum, fspath_min, bytes_left);
1718 return PTR_ERR(fspath);
1720 if (fspath > fspath_min) {
1722 ++ipath->
fspath->elem_cnt;
1723 ipath->
fspath->bytes_left = fspath - fspath_min;
1725 ++ipath->
fspath->elem_missed;
1727 ipath->
fspath->bytes_left = 0;
1746 inode_to_path, ipath);
1754 alloc_bytes =
max_t(
size_t, total_bytes,
sizeof(*data));
1759 if (total_bytes >=
sizeof(*data)) {
1760 data->
bytes_left = total_bytes -
sizeof(*data);
1761 data->bytes_missing = 0;
1787 return (
void *)fspath;