send.c (88ffb665c894b1929b30b09e05506ff359d9fb89) send.c (f73853c7168aef0e071c160979af05a148691e61)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 */
5
6#include <linux/bsearch.h>
7#include <linux/fs.h>
8#include <linux/file.h>

--- 1267 unchanged lines hidden (view full) ---

1276 * used for clones found in send_root. clones found behind cur_objectid
1277 * and cur_offset are not considered as allowed clones.
1278 */
1279 u64 cur_objectid;
1280 u64 cur_offset;
1281
1282 /* may be truncated in case it's the last extent in a file */
1283 u64 extent_len;
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 */
5
6#include <linux/bsearch.h>
7#include <linux/fs.h>
8#include <linux/file.h>

--- 1267 unchanged lines hidden (view full) ---

1276 * used for clones found in send_root. clones found behind cur_objectid
1277 * and cur_offset are not considered as allowed clones.
1278 */
1279 u64 cur_objectid;
1280 u64 cur_offset;
1281
1282 /* may be truncated in case it's the last extent in a file */
1283 u64 extent_len;
1284
1285 /* The bytenr the file extent item we are processing refers to. */
1286 u64 bytenr;
1284};
1285
1286static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1287{
1288 u64 root = (u64)(uintptr_t)key;
1289 const struct clone_root *cr = elt;
1290
1291 if (root < cr->root->root_key.objectid)

--- 221 unchanged lines hidden (view full) ---

1513 * to take any lock here.
1514 */
1515 if (sctx->backref_cache.size == 0)
1516 sctx->backref_cache.last_reloc_trans = fs_info->last_reloc_trans;
1517
1518 sctx->backref_cache.size++;
1519}
1520
1287};
1288
1289static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1290{
1291 u64 root = (u64)(uintptr_t)key;
1292 const struct clone_root *cr = elt;
1293
1294 if (root < cr->root->root_key.objectid)

--- 221 unchanged lines hidden (view full) ---

1516 * to take any lock here.
1517 */
1518 if (sctx->backref_cache.size == 0)
1519 sctx->backref_cache.last_reloc_trans = fs_info->last_reloc_trans;
1520
1521 sctx->backref_cache.size++;
1522}
1523
1524static int check_extent_item(u64 bytenr, const struct btrfs_extent_item *ei,
1525 const struct extent_buffer *leaf, void *ctx)
1526{
1527 const u64 refs = btrfs_extent_refs(leaf, ei);
1528 const struct backref_ctx *bctx = ctx;
1529 const struct send_ctx *sctx = bctx->sctx;
1530
1531 if (bytenr == bctx->bytenr) {
1532 const u64 flags = btrfs_extent_flags(leaf, ei);
1533
1534 if (WARN_ON(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
1535 return -EUCLEAN;
1536
1537 /*
1538 * If we have only one reference and only the send root as a
1539 * clone source - meaning no clone roots were given in the
1540 * struct btrfs_ioctl_send_args passed to the send ioctl - then
1541 * it's our reference and there's no point in doing backref
1542 * walking which is expensive, so exit early.
1543 */
1544 if (refs == 1 && sctx->clone_roots_cnt == 1)
1545 return -ENOENT;
1546 }
1547
1548 /*
1549 * Backreference walking (iterate_extent_inodes() below) is currently
1550 * too expensive when an extent has a large number of references, both
1551 * in time spent and used memory. So for now just fallback to write
1552 * operations instead of clone operations when an extent has more than
1553 * a certain amount of references.
1554 */
1555 if (refs > SEND_MAX_EXTENT_REFS)
1556 return -ENOENT;
1557
1558 return 0;
1559}
1560
1521/*
1522 * Given an inode, offset and extent item, it finds a good clone for a clone
1523 * instruction. Returns -ENOENT when none could be found. The function makes
1524 * sure that the returned clone is usable at the point where sending is at the
1525 * moment. This means, that no clones are accepted which lie behind the current
1526 * inode+offset.
1527 *
1528 * path must point to the extent item when called.

--- 5 unchanged lines hidden (view full) ---

1534 struct clone_root **found)
1535{
1536 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1537 int ret;
1538 int extent_type;
1539 u64 logical;
1540 u64 disk_byte;
1541 u64 num_bytes;
1561/*
1562 * Given an inode, offset and extent item, it finds a good clone for a clone
1563 * instruction. Returns -ENOENT when none could be found. The function makes
1564 * sure that the returned clone is usable at the point where sending is at the
1565 * moment. This means, that no clones are accepted which lie behind the current
1566 * inode+offset.
1567 *
1568 * path must point to the extent item when called.

--- 5 unchanged lines hidden (view full) ---

1574 struct clone_root **found)
1575{
1576 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1577 int ret;
1578 int extent_type;
1579 u64 logical;
1580 u64 disk_byte;
1581 u64 num_bytes;
1542 u64 extent_refs;
1543 u64 flags = 0;
1544 struct btrfs_file_extent_item *fi;
1545 struct extent_buffer *eb = path->nodes[0];
1546 struct backref_ctx backref_ctx = { 0 };
1547 struct btrfs_backref_walk_ctx backref_walk_ctx = { 0 };
1548 struct clone_root *cur_clone_root;
1582 struct btrfs_file_extent_item *fi;
1583 struct extent_buffer *eb = path->nodes[0];
1584 struct backref_ctx backref_ctx = { 0 };
1585 struct btrfs_backref_walk_ctx backref_walk_ctx = { 0 };
1586 struct clone_root *cur_clone_root;
1549 struct btrfs_key found_key;
1550 struct btrfs_path *tmp_path;
1551 struct btrfs_extent_item *ei;
1552 int compressed;
1553 u32 i;
1554
1555 /*
1556 * With fallocate we can get prealloc extents beyond the inode's i_size,
1557 * so we don't do anything here because clone operations can not clone
1558 * to a range beyond i_size without increasing the i_size of the
1559 * destination inode.

--- 9 unchanged lines hidden (view full) ---

1569 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1570 if (disk_byte == 0)
1571 return -ENOENT;
1572
1573 compressed = btrfs_file_extent_compression(eb, fi);
1574 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1575 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1576
1587 int compressed;
1588 u32 i;
1589
1590 /*
1591 * With fallocate we can get prealloc extents beyond the inode's i_size,
1592 * so we don't do anything here because clone operations can not clone
1593 * to a range beyond i_size without increasing the i_size of the
1594 * destination inode.

--- 9 unchanged lines hidden (view full) ---

1604 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1605 if (disk_byte == 0)
1606 return -ENOENT;
1607
1608 compressed = btrfs_file_extent_compression(eb, fi);
1609 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1610 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1611
1577 tmp_path = alloc_path_for_send();
1578 if (!tmp_path)
1579 return -ENOMEM;
1580
1581 /* We only use this path under the commit sem */
1582 tmp_path->need_commit_sem = 0;
1583
1584 down_read(&fs_info->commit_root_sem);
1585 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1586 &found_key, &flags);
1587 up_read(&fs_info->commit_root_sem);
1588
1589 if (ret < 0)
1590 goto out;
1591 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1592 ret = -EIO;
1593 goto out;
1594 }
1595
1596 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1597 struct btrfs_extent_item);
1598 extent_refs = btrfs_extent_refs(tmp_path->nodes[0], ei);
1599 /*
1612 /*
1600 * Backreference walking (iterate_extent_inodes() below) is currently
1601 * too expensive when an extent has a large number of references, both
1602 * in time spent and used memory. So for now just fallback to write
1603 * operations instead of clone operations when an extent has more than
1604 * a certain amount of references.
1605 *
1606 * Also, if we have only one reference and only the send root as a clone
1607 * source - meaning no clone roots were given in the struct
1608 * btrfs_ioctl_send_args passed to the send ioctl - then it's our
1609 * reference and there's no point in doing backref walking which is
1610 * expensive, so exit early.
1611 */
1612 if ((extent_refs == 1 && sctx->clone_roots_cnt == 1) ||
1613 extent_refs > SEND_MAX_EXTENT_REFS) {
1614 ret = -ENOENT;
1615 goto out;
1616 }
1617 btrfs_release_path(tmp_path);
1618
1619 /*
1620 * Setup the clone roots.
1621 */
1622 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1623 cur_clone_root = sctx->clone_roots + i;
1624 cur_clone_root->ino = (u64)-1;
1625 cur_clone_root->offset = 0;
1626 cur_clone_root->num_bytes = 0;
1627 cur_clone_root->found_ref = false;
1628 }
1629
1630 backref_ctx.sctx = sctx;
1631 backref_ctx.cur_objectid = ino;
1632 backref_ctx.cur_offset = data_offset;
1613 * Setup the clone roots.
1614 */
1615 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1616 cur_clone_root = sctx->clone_roots + i;
1617 cur_clone_root->ino = (u64)-1;
1618 cur_clone_root->offset = 0;
1619 cur_clone_root->num_bytes = 0;
1620 cur_clone_root->found_ref = false;
1621 }
1622
1623 backref_ctx.sctx = sctx;
1624 backref_ctx.cur_objectid = ino;
1625 backref_ctx.cur_offset = data_offset;
1626 backref_ctx.bytenr = disk_byte;
1633
1634 /*
1635 * The last extent of a file may be too large due to page alignment.
1636 * We need to adjust extent_len in this case so that the checks in
1637 * iterate_backrefs() work.
1638 */
1639 if (data_offset + num_bytes >= ino_size)
1640 backref_ctx.extent_len = ino_size - data_offset;
1641 else
1642 backref_ctx.extent_len = num_bytes;
1643
1644 /*
1645 * Now collect all backrefs.
1646 */
1627
1628 /*
1629 * The last extent of a file may be too large due to page alignment.
1630 * We need to adjust extent_len in this case so that the checks in
1631 * iterate_backrefs() work.
1632 */
1633 if (data_offset + num_bytes >= ino_size)
1634 backref_ctx.extent_len = ino_size - data_offset;
1635 else
1636 backref_ctx.extent_len = num_bytes;
1637
1638 /*
1639 * Now collect all backrefs.
1640 */
1647 backref_walk_ctx.bytenr = found_key.objectid;
1641 backref_walk_ctx.bytenr = disk_byte;
1648 if (compressed == BTRFS_COMPRESS_NONE)
1642 if (compressed == BTRFS_COMPRESS_NONE)
1649 backref_walk_ctx.extent_item_pos = logical - found_key.objectid;
1643 backref_walk_ctx.extent_item_pos = btrfs_file_extent_offset(eb, fi);
1650 backref_walk_ctx.fs_info = fs_info;
1651 backref_walk_ctx.cache_lookup = lookup_backref_cache;
1652 backref_walk_ctx.cache_store = store_backref_cache;
1653 backref_walk_ctx.indirect_ref_iterator = iterate_backrefs;
1644 backref_walk_ctx.fs_info = fs_info;
1645 backref_walk_ctx.cache_lookup = lookup_backref_cache;
1646 backref_walk_ctx.cache_store = store_backref_cache;
1647 backref_walk_ctx.indirect_ref_iterator = iterate_backrefs;
1648 backref_walk_ctx.check_extent_item = check_extent_item;
1654 backref_walk_ctx.user_ctx = &backref_ctx;
1655
1656 ret = iterate_extent_inodes(&backref_walk_ctx, true, iterate_backrefs,
1657 &backref_ctx);
1658 if (ret < 0)
1649 backref_walk_ctx.user_ctx = &backref_ctx;
1650
1651 ret = iterate_extent_inodes(&backref_walk_ctx, true, iterate_backrefs,
1652 &backref_ctx);
1653 if (ret < 0)
1659 goto out;
1654 return ret;
1660
1661 down_read(&fs_info->commit_root_sem);
1662 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
1663 /*
1664 * A transaction commit for a transaction in which block group
1665 * relocation was done just happened.
1666 * The disk_bytenr of the file extent item we processed is
1667 * possibly stale, referring to the extent's location before
1668 * relocation. So act as if we haven't found any clone sources
1669 * and fallback to write commands, which will read the correct
1670 * data from the new extent location. Otherwise we will fail
1671 * below because we haven't found our own back reference or we
1672 * could be getting incorrect sources in case the old extent
1673 * was already reallocated after the relocation.
1674 */
1675 up_read(&fs_info->commit_root_sem);
1655
1656 down_read(&fs_info->commit_root_sem);
1657 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
1658 /*
1659 * A transaction commit for a transaction in which block group
1660 * relocation was done just happened.
1661 * The disk_bytenr of the file extent item we processed is
1662 * possibly stale, referring to the extent's location before
1663 * relocation. So act as if we haven't found any clone sources
1664 * and fallback to write commands, which will read the correct
1665 * data from the new extent location. Otherwise we will fail
1666 * below because we haven't found our own back reference or we
1667 * could be getting incorrect sources in case the old extent
1668 * was already reallocated after the relocation.
1669 */
1670 up_read(&fs_info->commit_root_sem);
1676 ret = -ENOENT;
1677 goto out;
1671 return -ENOENT;
1678 }
1679 up_read(&fs_info->commit_root_sem);
1680
1681 btrfs_debug(fs_info,
1682 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1683 data_offset, ino, num_bytes, logical);
1684
1685 if (!backref_ctx.found) {
1686 btrfs_debug(fs_info, "no clones found");
1672 }
1673 up_read(&fs_info->commit_root_sem);
1674
1675 btrfs_debug(fs_info,
1676 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1677 data_offset, ino, num_bytes, logical);
1678
1679 if (!backref_ctx.found) {
1680 btrfs_debug(fs_info, "no clones found");
1687 ret = -ENOENT;
1688 goto out;
1681 return -ENOENT;
1689 }
1690
1691 cur_clone_root = NULL;
1692 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1693 struct clone_root *clone_root = &sctx->clone_roots[i];
1694
1695 if (!clone_root->found_ref)
1696 continue;

--- 18 unchanged lines hidden (view full) ---

1715
1716 if (cur_clone_root) {
1717 *found = cur_clone_root;
1718 ret = 0;
1719 } else {
1720 ret = -ENOENT;
1721 }
1722
1682 }
1683
1684 cur_clone_root = NULL;
1685 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1686 struct clone_root *clone_root = &sctx->clone_roots[i];
1687
1688 if (!clone_root->found_ref)
1689 continue;

--- 18 unchanged lines hidden (view full) ---

1708
1709 if (cur_clone_root) {
1710 *found = cur_clone_root;
1711 ret = 0;
1712 } else {
1713 ret = -ENOENT;
1714 }
1715
1723out:
1724 btrfs_free_path(tmp_path);
1725 return ret;
1726}
1727
1728static int read_symlink(struct btrfs_root *root,
1729 u64 ino,
1730 struct fs_path *dest)
1731{
1732 int ret;

--- 6614 unchanged lines hidden ---
1716 return ret;
1717}
1718
1719static int read_symlink(struct btrfs_root *root,
1720 u64 ino,
1721 struct fs_path *dest)
1722{
1723 int ret;

--- 6614 unchanged lines hidden ---