24 #include <linux/xattr.h>
37 static int g_verbose = 0;
39 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
64 #define FS_PATH_INLINE_SIZE \
65 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
77 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
78 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
147 static void fs_path_reset(
struct fs_path *
p)
179 p = fs_path_alloc(sctx);
200 static int fs_path_len(
struct fs_path *p)
205 static int fs_path_ensure_buf(
struct fs_path *p,
int len)
253 tmp_buf = p->
buf + old_buf_len - path_len - 1;
272 ret = fs_path_ensure_buf(p, new_len);
293 static int fs_path_add(
struct fs_path *p,
const char *
name,
int name_len)
297 ret = fs_path_prepare_for_add(p, name_len);
307 static int fs_path_add_path(
struct fs_path *p,
struct fs_path *p2)
311 ret = fs_path_prepare_for_add(p, p2->
end - p2->
start);
321 static int fs_path_add_from_extent_buffer(
struct fs_path *p,
323 unsigned long off,
int len)
327 ret = fs_path_prepare_for_add(p, len);
339 static void fs_path_remove(
struct fs_path *p)
355 ret = fs_path_add_path(p, from);
361 static void fs_path_unreverse(
struct fs_path *p)
377 static struct btrfs_path *alloc_path_for_send(
void)
399 ret =
vfs_write(filp, (
char *)buf + pos, len - pos, off);
432 memcpy(hdr + 1, data, len);
441 return tlv_put(sctx, attr, &value,
sizeof(value));
444 static int tlv_put_u16(
struct send_ctx *sctx,
u16 attr,
u16 value)
447 return tlv_put(sctx, attr, &tmp,
sizeof(tmp));
450 static int tlv_put_u32(
struct send_ctx *sctx,
u16 attr,
u32 value)
453 return tlv_put(sctx, attr, &tmp,
sizeof(tmp));
457 static int tlv_put_u64(
struct send_ctx *sctx,
u16 attr,
u64 value)
460 return tlv_put(sctx, attr, &tmp,
sizeof(tmp));
463 static int tlv_put_string(
struct send_ctx *sctx,
u16 attr,
464 const char *
str,
int len)
468 return tlv_put(sctx, attr, str, len);
471 static int tlv_put_uuid(
struct send_ctx *sctx,
u16 attr,
478 static int tlv_put_timespec(
struct send_ctx *sctx,
u16 attr,
484 return tlv_put(sctx, attr, &bts,
sizeof(bts));
488 static int tlv_put_btrfs_timespec(
struct send_ctx *sctx,
u16 attr,
494 return tlv_put(sctx, attr, &bts,
sizeof(bts));
498 #define TLV_PUT(sctx, attrtype, attrlen, data) \
500 ret = tlv_put(sctx, attrtype, attrlen, data); \
502 goto tlv_put_failure; \
505 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
507 ret = tlv_put_u##bits(sctx, attrtype, value); \
509 goto tlv_put_failure; \
512 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
513 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
514 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
515 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
516 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
518 ret = tlv_put_string(sctx, attrtype, str, len); \
520 goto tlv_put_failure; \
522 #define TLV_PUT_PATH(sctx, attrtype, p) \
524 ret = tlv_put_string(sctx, attrtype, p->start, \
525 p->end - p->start); \
527 goto tlv_put_failure; \
529 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
531 ret = tlv_put_uuid(sctx, attrtype, uuid); \
533 goto tlv_put_failure; \
535 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
537 ret = tlv_put_timespec(sctx, attrtype, ts); \
539 goto tlv_put_failure; \
541 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
543 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
545 goto tlv_put_failure; \
548 static int send_header(
struct send_ctx *sctx)
562 static int begin_cmd(
struct send_ctx *sctx,
int cmd)
580 static int send_cmd(
struct send_ctx *sctx)
606 static int send_rename(
struct send_ctx *sctx,
620 ret = send_cmd(sctx);
630 static int send_link(
struct send_ctx *sctx,
644 ret = send_cmd(sctx);
666 ret = send_cmd(sctx);
688 ret = send_cmd(sctx);
698 static int get_inode_info(
struct btrfs_root *root,
708 path = alloc_path_for_send();
726 *size = btrfs_inode_size(path->
nodes[0], ii);
728 *gen = btrfs_inode_generation(path->
nodes[0], ii);
730 *mode = btrfs_inode_mode(path->
nodes[0], ii);
732 *uid = btrfs_inode_uid(path->
nodes[0], ii);
734 *gid = btrfs_inode_gid(path->
nodes[0], ii);
736 *rdev = btrfs_inode_rdev(path->
nodes[0], ii);
755 static int iterate_inode_ref(
struct send_ctx *sctx,
757 struct btrfs_key *found_key,
int resolve,
775 unsigned long name_off;
776 unsigned long elem_size;
779 p = fs_path_alloc_reversed(sctx);
783 tmp_path = alloc_path_for_send();
785 fs_path_free(sctx, p);
793 item = btrfs_item_nr(eb, slot);
794 total = btrfs_item_size(eb, item);
795 elem_size =
sizeof(*iref);
798 total = btrfs_item_size_nr(eb, slot);
799 elem_size =
sizeof(*extref);
802 while (cur < total) {
807 name_len = btrfs_inode_ref_name_len(eb, iref);
808 name_off = (
unsigned long)(iref + 1);
809 index = btrfs_inode_ref_index(eb, iref);
813 name_len = btrfs_inode_extref_name_len(eb, extref);
814 name_off = (
unsigned long)&extref->
name;
815 index = btrfs_inode_extref_index(eb, extref);
816 dir = btrfs_inode_extref_parent(eb, extref);
824 ret = PTR_ERR(start);
827 if (start < p->
buf) {
829 ret = fs_path_ensure_buf(p,
838 ret = PTR_ERR(start);
845 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
852 ret = iterate(num, dir, index, p, ctx);
860 fs_path_free(sctx, p);
876 static int iterate_dir_item(
struct send_ctx *sctx,
907 slot = path->
slots[0];
908 item = btrfs_item_nr(eb, slot);
912 total = btrfs_item_size(eb, item);
915 while (cur < total) {
916 name_len = btrfs_dir_name_len(eb, di);
917 data_len = btrfs_dir_data_len(eb, di);
918 type = btrfs_dir_type(eb, di);
919 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
921 if (name_len + data_len > buf_len) {
948 name_len + data_len);
950 len =
sizeof(*di) + name_len +
data_len;
954 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
955 data_len, type, ctx);
974 static int __copy_first_ref(
int num,
u64 dir,
int index,
980 ret = fs_path_copy(pt, p);
999 p = alloc_path_for_send();
1003 fs_path_reset(path);
1016 btrfs_item_key_to_cpu(p->
nodes[0], &found_key, p->
slots[0]);
1017 if (found_key.objectid != ino ||
1024 ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
1025 __copy_first_ref, path);
1055 static int __clone_root_cmp_bsearch(
const void *
key,
const void *elt)
1060 if (root < cr->root->objectid)
1062 if (root > cr->
root->objectid)
1067 static int __clone_root_cmp_sort(
const void *
e1,
const void *
e2)
1072 if (cr1->
root->objectid < cr2->
root->objectid)
1074 if (cr1->
root->objectid > cr2->
root->objectid)
1083 static int __iterate_backrefs(
u64 ino,
u64 offset,
u64 root,
void *ctx_)
1092 bctx->
sctx->clone_roots_cnt,
1094 __clone_root_cmp_bsearch);
1098 if (found->
root == bctx->
sctx->send_root &&
1120 if (found->
root == bctx->
sctx->send_root) {
1139 if (ino < found->ino) {
1142 }
else if (found->
ino == ino) {
1162 static int find_extent_clone(
struct send_ctx *sctx,
1173 u64 extent_item_pos;
1184 tmp_path = alloc_path_for_send();
1194 if (data_offset >= ino_size) {
1206 extent_type = btrfs_file_extent_type(eb, fi);
1211 compressed = btrfs_file_extent_compression(eb, fi);
1213 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1214 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1215 if (disk_byte == 0) {
1219 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1222 &found_key, &flags);
1237 cur_clone_root->
ino = (
u64)-1;
1238 cur_clone_root->
offset = 0;
1242 backref_ctx->
sctx = sctx;
1243 backref_ctx->
found = 0;
1254 if (data_offset + num_bytes >= ino_size)
1261 extent_item_pos = logical - found_key.objectid;
1263 extent_item_pos = 0;
1265 extent_item_pos = logical - found_key.objectid;
1267 found_key.objectid, extent_item_pos, 1,
1268 __iterate_backrefs, backref_ctx);
1277 "send_root. inode=%llu, offset=%llu, "
1278 "disk_byte=%llu found extent=%llu\n",
1279 ino, data_offset, disk_byte, found_key.objectid);
1285 "num_bytes=%llu, logical=%llu\n",
1286 data_offset, ino, num_bytes, logical);
1288 if (!backref_ctx->
found)
1291 cur_clone_root =
NULL;
1294 if (!cur_clone_root)
1303 if (cur_clone_root) {
1304 *found = cur_clone_root;
1316 static int read_symlink(
struct send_ctx *sctx,
1330 path = alloc_path_for_send();
1344 type = btrfs_file_extent_type(path->
nodes[0], ei);
1345 compression = btrfs_file_extent_compression(path->
nodes[0], ei);
1349 off = btrfs_file_extent_inline_start(ei);
1350 len = btrfs_file_extent_inline_len(path->
nodes[0], ei);
1352 ret = fs_path_add_from_extent_buffer(dest, path->
nodes[0], off, len);
1363 static int gen_unique_name(
struct send_ctx *sctx,
1374 path = alloc_path_for_send();
1379 len =
snprintf(tmp,
sizeof(tmp) - 1,
"o%llu-%llu-%llu",
1381 if (len >=
sizeof(tmp)) {
1424 ret = fs_path_add(dest, tmp,
strlen(tmp));
1439 static int get_cur_inode_state(
struct send_ctx *sctx,
u64 ino,
u64 gen)
1449 if (ret < 0 && ret != -
ENOENT)
1458 if (ret < 0 && ret != -
ENOENT)
1463 if (!left_ret && !right_ret) {
1464 if (left_gen == gen && right_gen == gen) {
1466 }
else if (left_gen == gen) {
1467 if (ino < sctx->send_progress)
1471 }
else if (right_gen == gen) {
1472 if (ino < sctx->send_progress)
1479 }
else if (!left_ret) {
1480 if (left_gen == gen) {
1481 if (ino < sctx->send_progress)
1488 }
else if (!right_ret) {
1489 if (right_gen == gen) {
1490 if (ino < sctx->send_progress)
1505 static int is_inode_existent(
struct send_ctx *sctx,
u64 ino,
u64 gen)
1509 ret = get_cur_inode_state(sctx, ino, gen);
1527 static int lookup_dir_item_inode(
struct btrfs_root *root,
1528 u64 dir,
const char *name,
int name_len,
1537 path = alloc_path_for_send();
1542 dir, name, name_len, 0);
1551 btrfs_dir_item_key_to_cpu(path->
nodes[0], di, &
key);
1552 *found_inode =
key.objectid;
1553 *found_type = btrfs_dir_type(path->
nodes[0], di);
1564 static int get_first_ref(
struct send_ctx *sctx,
1575 path = alloc_path_for_send();
1587 btrfs_item_key_to_cpu(path->
nodes[0], &found_key,
1589 if (ret || found_key.objectid != ino ||
1600 len = btrfs_inode_ref_name_len(path->
nodes[0], iref);
1601 ret = fs_path_add_from_extent_buffer(name, path->
nodes[0],
1602 (
unsigned long)(iref + 1),
1604 parent_dir = found_key.offset;
1609 len = btrfs_inode_extref_name_len(path->
nodes[0], extref);
1610 ret = fs_path_add_from_extent_buffer(name, path->
nodes[0],
1611 (
unsigned long)&extref->
name, len);
1612 parent_dir = btrfs_inode_extref_parent(path->
nodes[0], extref);
1618 ret = get_inode_info(root, parent_dir,
NULL, dir_gen,
NULL,
NULL,
1630 static int is_first_ref(
struct send_ctx *sctx,
1633 const char *name,
int name_len)
1640 tmp_name = fs_path_alloc(sctx);
1644 ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1648 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1656 fs_path_free(sctx, tmp_name);
1670 static int will_overwrite_ref(
struct send_ctx *sctx,
u64 dir,
u64 dir_gen,
1671 const char *name,
int name_len,
1672 u64 *who_ino,
u64 *who_gen)
1675 u64 other_inode = 0;
1681 ret = is_inode_existent(sctx, dir, dir_gen);
1685 ret = lookup_dir_item_inode(sctx->
parent_root, dir, name, name_len,
1686 &other_inode, &other_type);
1687 if (ret < 0 && ret != -
ENOENT)
1706 *who_ino = other_inode;
1722 static int did_overwrite_ref(
struct send_ctx *sctx,
1725 const char *name,
int name_len)
1735 ret = is_inode_existent(sctx, dir, dir_gen);
1740 ret = lookup_dir_item_inode(sctx->
send_root, dir, name, name_len,
1741 &ow_inode, &other_type);
1742 if (ret < 0 && ret != -
ENOENT)
1755 if (ow_inode == ino && gen == ino_gen) {
1761 if (ow_inode < sctx->send_progress)
1775 static int did_overwrite_first_ref(
struct send_ctx *sctx,
u64 ino,
u64 gen)
1785 name = fs_path_alloc(sctx);
1789 ret = get_first_ref(sctx, sctx->
parent_root, ino, &dir, &dir_gen, name);
1793 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1794 name->
start, fs_path_len(name));
1797 fs_path_free(sctx, name);
1807 static int name_cache_insert(
struct send_ctx *sctx,
1814 (
unsigned long)nce->
ino);
1819 INIT_LIST_HEAD(nce_head);
1835 static void name_cache_delete(
struct send_ctx *sctx,
1841 (
unsigned long)nce->
ino);
1848 if (list_empty(nce_head)) {
1865 if (cur->
ino == ino && cur->
gen == gen)
1884 static void name_cache_clean_unused(
struct send_ctx *sctx)
1894 name_cache_delete(sctx, nce);
1899 static void name_cache_free(
struct send_ctx *sctx)
1906 name_cache_delete(sctx, nce);
1919 static int __get_cur_name_and_parent(
struct send_ctx *sctx,
1935 nce = name_cache_search(sctx, ino, gen);
1938 name_cache_delete(sctx, nce);
1942 name_cache_used(sctx, nce);
1953 path = alloc_path_for_send();
1962 ret = is_inode_existent(sctx, ino, gen);
1967 ret = gen_unique_name(sctx, ino, gen, dest);
1978 if (ino < sctx->send_progress)
1979 ret = get_first_ref(sctx, sctx->
send_root, ino,
1980 parent_ino, parent_gen, dest);
1983 parent_ino, parent_gen, dest);
1991 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1996 fs_path_reset(dest);
1997 ret = gen_unique_name(sctx, ino, gen, dest);
2021 if (ino < sctx->send_progress)
2026 nce_ret = name_cache_insert(sctx, nce);
2029 name_cache_clean_unused(sctx);
2066 u64 parent_inode = 0;
2070 name = fs_path_alloc(sctx);
2077 fs_path_reset(dest);
2080 fs_path_reset(name);
2082 ret = __get_cur_name_and_parent(sctx, ino, gen,
2083 &parent_inode, &parent_gen, name);
2089 ret = fs_path_add_path(dest, name);
2098 fs_path_free(sctx, name);
2100 fs_path_unreverse(dest);
2108 static int open_cur_inode_file(
struct send_ctx *sctx)
2127 if (IS_ERR(inode)) {
2128 ret = PTR_ERR(inode);
2134 if (IS_ERR(dentry)) {
2135 ret = PTR_ERR(dentry);
2139 path.mnt = sctx->
mnt;
2140 path.dentry = dentry;
2145 ret = PTR_ERR(filp);
2161 static int close_cur_inode_file(
struct send_ctx *sctx)
2178 static int send_subvol_begin(
struct send_ctx *sctx)
2190 path = alloc_path_for_send();
2213 leaf = path->
nodes[0];
2214 btrfs_item_key_to_cpu(leaf, &
key, path->
slots[0]);
2221 namelen = btrfs_root_ref_name_len(leaf, ref);
2247 ret = send_cmd(sctx);
2261 verbose_printk(
"btrfs: send_truncate %llu size=%llu\n", ino, size);
2263 p = fs_path_alloc(sctx);
2271 ret = get_cur_path(sctx, ino, gen, p);
2277 ret = send_cmd(sctx);
2281 fs_path_free(sctx, p);
2292 p = fs_path_alloc(sctx);
2300 ret = get_cur_path(sctx, ino, gen, p);
2306 ret = send_cmd(sctx);
2310 fs_path_free(sctx, p);
2319 verbose_printk(
"btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2321 p = fs_path_alloc(sctx);
2329 ret = get_cur_path(sctx, ino, gen, p);
2336 ret = send_cmd(sctx);
2340 fs_path_free(sctx, p);
2356 p = fs_path_alloc(sctx);
2360 path = alloc_path_for_send();
2373 eb = path->
nodes[0];
2374 slot = path->
slots[0];
2381 ret = get_cur_path(sctx, ino, gen, p);
2386 btrfs_inode_atime(ii));
2388 btrfs_inode_mtime(ii));
2390 btrfs_inode_ctime(ii));
2393 ret = send_cmd(sctx);
2397 fs_path_free(sctx, p);
2407 static int send_create_inode(
struct send_ctx *sctx,
u64 ino)
2418 p = fs_path_alloc(sctx);
2446 ret = begin_cmd(sctx, cmd);
2450 ret = gen_unique_name(sctx, ino, gen, p);
2459 ret = read_symlink(sctx, sctx->
send_root, ino, p);
2469 ret = send_cmd(sctx);
2476 fs_path_free(sctx, p);
2485 static int did_create_dir(
struct send_ctx *sctx,
u64 dir)
2496 path = alloc_path_for_send();
2511 eb = path->
nodes[0];
2512 slot = path->
slots[0];
2513 btrfs_item_key_to_cpu(eb, &found_key, slot);
2515 if (ret || found_key.objectid !=
key.objectid ||
2516 found_key.type !=
key.type) {
2522 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2529 key.offset = found_key.offset + 1;
2544 static int send_create_inode_if_needed(
struct send_ctx *sctx)
2549 ret = did_create_dir(sctx, sctx->
cur_ino);
2558 ret = send_create_inode(sctx, sctx->
cur_ino);
2619 while (!list_empty(head)) {
2627 static void free_recorded_refs(
struct send_ctx *sctx)
2629 __free_recorded_refs(sctx, &sctx->
new_refs);
2638 static int orphanize_inode(
struct send_ctx *sctx,
u64 ino,
u64 gen,
2644 orphan = fs_path_alloc(sctx);
2648 ret = gen_unique_name(sctx, ino, gen, orphan);
2652 ret = send_rename(sctx, path, orphan);
2655 fs_path_free(sctx, orphan);
2664 static int can_rmdir(
struct send_ctx *sctx,
u64 dir,
u64 send_progress)
2680 path = alloc_path_for_send();
2693 btrfs_item_key_to_cpu(path->
nodes[0], &found_key,
2696 if (ret || found_key.objectid !=
key.objectid ||
2697 found_key.type !=
key.type) {
2703 btrfs_dir_item_key_to_cpu(path->
nodes[0], di, &
loc);
2705 if (
loc.objectid > send_progress) {
2711 key.offset = found_key.offset + 1;
2724 static int process_recorded_refs(
struct send_ctx *sctx)
2735 int did_overwrite = 0;
2746 valid_path = fs_path_alloc(sctx);
2770 ret = did_overwrite_first_ref(sctx, sctx->
cur_ino,
2778 ret = gen_unique_name(sctx, sctx->
cur_ino,
2798 ret = get_cur_inode_state(sctx, cur->
dir, cur->
dir_gen);
2810 if (cur2->
dir == cur->
dir) {
2821 ret = did_create_dir(sctx, cur->
dir);
2825 ret = send_create_inode(sctx, cur->
dir);
2837 ret = will_overwrite_ref(sctx, cur->
dir, cur->
dir_gen,
2839 &ow_inode, &ow_gen);
2844 ow_inode, cur->
dir, cur->
name,
2849 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2854 ret = send_unlink(sctx, cur->
full_path);
2866 ret = send_rename(sctx, valid_path, cur->
full_path);
2870 ret = fs_path_copy(valid_path, cur->
full_path);
2880 ret = send_rename(sctx, valid_path,
2884 ret = fs_path_copy(valid_path, cur->
full_path);
2911 ret = send_rmdir(sctx, valid_path);
2914 }
else if (!is_orphan) {
2915 ret = orphanize_inode(sctx, sctx->
cur_ino,
2946 ret = did_overwrite_ref(sctx, cur->
dir, cur->
dir_gen,
2952 ret = send_unlink(sctx, cur->
full_path);
2971 ret = send_unlink(sctx, valid_path);
2993 ret = get_cur_inode_state(sctx, un->
val, un->
aux);
3000 ret = send_utimes(sctx, un->
val, un->
aux);
3004 ret = can_rmdir(sctx, un->
val, sctx->
cur_ino);
3008 ret = get_cur_path(sctx, un->
val, un->
aux,
3012 ret = send_rmdir(sctx, valid_path);
3022 free_recorded_refs(sctx);
3024 fs_path_free(sctx, valid_path);
3028 static int __record_new_ref(
int num,
u64 dir,
int index,
3037 p = fs_path_alloc(sctx);
3046 ret = get_cur_path(sctx, dir, gen, p);
3049 ret = fs_path_add_path(p, name);
3053 ret = record_ref(&sctx->
new_refs, dir, gen, p);
3057 fs_path_free(sctx, p);
3061 static int __record_deleted_ref(
int num,
u64 dir,
int index,
3070 p = fs_path_alloc(sctx);
3079 ret = get_cur_path(sctx, dir, gen, p);
3082 ret = fs_path_add_path(p, name);
3090 fs_path_free(sctx, p);
3094 static int record_new_ref(
struct send_ctx *sctx)
3099 sctx->
cmp_key, 0, __record_new_ref, sctx);
3108 static int record_deleted_ref(
struct send_ctx *sctx)
3113 sctx->
cmp_key, 0, __record_deleted_ref, sctx);
3128 static int __find_iref(
int num,
u64 dir,
int index,
3134 if (dir == ctx->
dir && fs_path_len(name) == fs_path_len(ctx->
name) &&
3142 static int find_iref(
struct send_ctx *sctx,
3155 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3159 if (ctx.found_idx == -1)
3162 return ctx.found_idx;
3165 static int __record_changed_new_ref(
int num,
u64 dir,
int index,
3175 ret = __record_new_ref(num, dir, index, name, sctx);
3182 static int __record_changed_deleted_ref(
int num,
u64 dir,
int index,
3192 ret = __record_deleted_ref(num, dir, index, name, sctx);
3199 static int record_changed_ref(
struct send_ctx *sctx)
3204 sctx->
cmp_key, 0, __record_changed_new_ref, sctx);
3208 sctx->
cmp_key, 0, __record_changed_deleted_ref, sctx);
3221 static int process_all_refs(
struct send_ctx *sctx,
3233 path = alloc_path_for_send();
3239 cb = __record_new_ref;
3242 cb = __record_deleted_ref;
3257 eb = path->
nodes[0];
3258 slot = path->
slots[0];
3259 btrfs_item_key_to_cpu(eb, &found_key, slot);
3261 if (found_key.objectid != key.
objectid ||
3266 ret = iterate_inode_ref(sctx, root, path, &found_key, 0, cb,
3272 key.
offset = found_key.offset + 1;
3276 ret = process_recorded_refs(sctx);
3283 static int send_set_xattr(
struct send_ctx *sctx,
3285 const char *name,
int name_len,
3286 const char *data,
int data_len)
3298 ret = send_cmd(sctx);
3305 static int send_remove_xattr(
struct send_ctx *sctx,
3307 const char *name,
int name_len)
3318 ret = send_cmd(sctx);
3325 static int __process_new_xattr(
int num,
struct btrfs_key *di_key,
3326 const char *name,
int name_len,
3327 const char *data,
int data_len,
3335 p = fs_path_alloc(sctx);
3347 if (data_len == 0) {
3350 data = (
char *)&dummy_acl;
3351 data_len =
sizeof(dummy_acl);
3359 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3362 fs_path_free(sctx, p);
3366 static int __process_deleted_xattr(
int num,
struct btrfs_key *di_key,
3367 const char *name,
int name_len,
3368 const char *data,
int data_len,
3375 p = fs_path_alloc(sctx);
3383 ret = send_remove_xattr(sctx, p, name, name_len);
3386 fs_path_free(sctx, p);
3390 static int process_new_xattr(
struct send_ctx *sctx)
3395 sctx->
cmp_key, __process_new_xattr, sctx);
3400 static int process_deleted_xattr(
struct send_ctx *sctx)
3405 sctx->
cmp_key, __process_deleted_xattr, sctx);
3418 static int __find_xattr(
int num,
struct btrfs_key *di_key,
3419 const char *name,
int name_len,
3420 const char *data,
int data_len,
3421 u8 type,
void *vctx)
3438 static int find_xattr(
struct send_ctx *sctx,
3442 const char *name,
int name_len,
3443 char **data,
int *data_len)
3451 ctx.found_data =
NULL;
3452 ctx.found_data_len = 0;
3454 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3458 if (ctx.found_idx == -1)
3461 *data = ctx.found_data;
3462 *data_len = ctx.found_data_len;
3464 kfree(ctx.found_data);
3466 return ctx.found_idx;
3470 static int __process_changed_new_xattr(
int num,
struct btrfs_key *di_key,
3471 const char *name,
int name_len,
3472 const char *data,
int data_len,
3477 char *found_data =
NULL;
3478 int found_data_len = 0;
3482 sctx->
cmp_key, name, name_len, &found_data,
3485 ret = __process_new_xattr(num, di_key, name, name_len, data,
3486 data_len, type, ctx);
3487 }
else if (ret >= 0) {
3488 if (data_len != found_data_len ||
3489 memcmp(data, found_data, data_len)) {
3490 ret = __process_new_xattr(num, di_key, name, name_len,
3491 data, data_len, type, ctx);
3498 fs_path_free(sctx, p);
3502 static int __process_changed_deleted_xattr(
int num,
struct btrfs_key *di_key,
3503 const char *name,
int name_len,
3504 const char *data,
int data_len,
3513 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3514 data_len, type, ctx);
3521 static int process_changed_xattr(
struct send_ctx *sctx)
3526 sctx->
cmp_key, __process_changed_new_xattr, sctx);
3530 sctx->
cmp_key, __process_changed_deleted_xattr, sctx);
3536 static int process_all_new_xattrs(
struct send_ctx *sctx)
3546 path = alloc_path_for_send();
3564 eb = path->
nodes[0];
3565 slot = path->
slots[0];
3566 btrfs_item_key_to_cpu(eb, &found_key, slot);
3568 if (found_key.objectid != key.
objectid ||
3569 found_key.type != key.
type) {
3574 ret = iterate_dir_item(sctx, root, path, &found_key,
3575 __process_new_xattr, sctx);
3580 key.
offset = found_key.offset + 1;
3592 static int send_write(
struct send_ctx *sctx,
u64 offset,
u32 len)
3600 p = fs_path_alloc(sctx);
3613 verbose_printk(
"btrfs: send_write offset=%llu, len=%d\n", offset, len);
3615 ret = open_cur_inode_file(sctx);
3638 ret = send_cmd(sctx);
3642 fs_path_free(sctx, p);
3652 static int send_clone(
struct send_ctx *sctx,
3660 verbose_printk(
"btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3661 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3662 clone_root->
root->objectid, clone_root->
ino,
3665 p = fs_path_alloc(sctx);
3686 ret = get_cur_path(sctx, clone_root->
ino, gen, p);
3688 ret = get_inode_path(sctx, clone_root->
root,
3689 clone_root->
ino, p);
3695 clone_root->
root->root_item.uuid);
3697 clone_root->
root->root_item.ctransid);
3702 ret = send_cmd(sctx);
3706 fs_path_free(sctx, p);
3710 static int send_write_or_clone(
struct send_ctx *sctx,
3713 struct clone_root *clone_root)
3725 type = btrfs_file_extent_type(path->
nodes[0], ei);
3727 len = btrfs_file_extent_inline_len(path->
nodes[0], ei);
3735 len = btrfs_file_extent_num_bytes(path->
nodes[0], ei);
3750 ret = send_write(sctx, pos + offset, l);
3759 ret = send_clone(sctx, offset, len, clone_root);
3766 static int is_extent_unchanged(
struct send_ctx *sctx,
3781 u64 left_offset_fixed;
3789 path = alloc_path_for_send();
3793 eb = left_path->
nodes[0];
3794 slot = left_path->
slots[0];
3796 left_type = btrfs_file_extent_type(eb, ei);
3802 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3803 left_len = btrfs_file_extent_num_bytes(eb, ei);
3804 left_offset = btrfs_file_extent_offset(eb, ei);
3805 left_gen = btrfs_file_extent_generation(eb, ei);
3842 eb = path->
nodes[0];
3843 slot = path->
slots[0];
3844 btrfs_item_key_to_cpu(eb, &found_key, slot);
3845 if (found_key.objectid != key.
objectid ||
3846 found_key.type != key.
type) {
3857 right_type = btrfs_file_extent_type(eb, ei);
3858 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3859 right_len = btrfs_file_extent_num_bytes(eb, ei);
3860 right_offset = btrfs_file_extent_offset(eb, ei);
3861 right_gen = btrfs_file_extent_generation(eb, ei);
3872 if (found_key.offset + right_len <= ekey->offset) {
3877 left_offset_fixed = left_offset;
3889 if (left_disknr != right_disknr ||
3890 left_offset_fixed != right_offset ||
3891 left_gen != right_gen) {
3903 eb = path->
nodes[0];
3904 slot = path->
slots[0];
3905 btrfs_item_key_to_cpu(eb, &found_key, slot);
3907 if (ret || found_key.objectid != key.
objectid ||
3908 found_key.type != key.
type) {
3912 if (found_key.offset != key.
offset + right_len) {
3936 static int process_extent(
struct send_ctx *sctx,
3941 struct clone_root *found_clone =
NULL;
3947 ret = is_extent_unchanged(sctx, path, key);
3958 if (ret != -
ENOENT && ret < 0)
3961 ret = send_write_or_clone(sctx, path, key, found_clone);
3967 static int process_all_extents(
struct send_ctx *sctx)
3978 path = alloc_path_for_send();
3994 eb = path->
nodes[0];
3995 slot = path->
slots[0];
3996 btrfs_item_key_to_cpu(eb, &found_key, slot);
3998 if (found_key.objectid != key.
objectid ||
3999 found_key.type != key.
type) {
4004 ret = process_extent(sctx, path, &found_key);
4009 key.
offset = found_key.offset + 1;
4017 static int process_recorded_refs_if_needed(
struct send_ctx *sctx,
int at_end)
4029 ret = process_recorded_refs(sctx);
4044 static int finish_inode_if_needed(
struct send_ctx *sctx,
int at_end)
4056 ret = process_recorded_refs_if_needed(sctx, at_end);
4066 &left_mode, &left_uid, &left_gid,
NULL);
4076 NULL,
NULL, &right_mode, &right_uid,
4081 if (left_uid != right_uid || left_gid != right_gid)
4096 left_uid, left_gid);
4119 static int changed_inode(
struct send_ctx *sctx,
4129 ret = close_cur_inode_file(sctx);
4148 left_gen = btrfs_inode_generation(sctx->
left_path->nodes[0],
4154 right_gen = btrfs_inode_generation(sctx->
right_path->nodes[0],
4162 right_gen = btrfs_inode_generation(sctx->
right_path->nodes[0],
4170 if (left_gen != right_gen &&
4184 ret = send_create_inode_if_needed(sctx);
4212 ret = process_all_refs(sctx,
4227 ret = send_create_inode_if_needed(sctx);
4244 ret = process_all_extents(sctx);
4247 ret = process_all_new_xattrs(sctx);
4276 static int changed_ref(
struct send_ctx *sctx,
4286 ret = record_new_ref(sctx);
4288 ret = record_deleted_ref(sctx);
4290 ret = record_changed_ref(sctx);
4301 static int changed_xattr(
struct send_ctx *sctx,
4310 ret = process_new_xattr(sctx);
4312 ret = process_deleted_xattr(sctx);
4314 ret = process_changed_xattr(sctx);
4325 static int changed_extent(
struct send_ctx *sctx,
4334 ret = process_extent(sctx, sctx->
left_path,
4345 static int changed_cb(
struct btrfs_root *left_root,
4360 ret = finish_inode_if_needed(sctx, 0);
4370 ret = changed_inode(sctx, result);
4373 ret = changed_ref(sctx, result);
4375 ret = changed_xattr(sctx, result);
4377 ret = changed_extent(sctx, result);
4383 static int full_send_tree(
struct send_ctx *sctx)
4396 path = alloc_path_for_send();
4401 start_ctransid = btrfs_root_ctransid(&send_root->
root_item);
4415 if (IS_ERR(trans)) {
4416 ret = PTR_ERR(trans);
4426 ctransid = btrfs_root_ctransid(&send_root->
root_item);
4429 if (ctransid != start_ctransid) {
4431 "send was modified in between. This is "
4432 "probably a bug.\n");
4457 eb = path->
nodes[0];
4458 slot = path->
slots[0];
4459 btrfs_item_key_to_cpu(eb, &found_key, slot);
4461 ret = changed_cb(send_root,
NULL, path,
NULL,
4467 key.
type = found_key.type;
4468 key.
offset = found_key.offset + 1;
4470 ret = btrfs_next_item(send_root, path);
4480 ret = finish_inode_if_needed(sctx, 1);
4493 static int send_subvol(
struct send_ctx *sctx)
4497 ret = send_header(sctx);
4501 ret = send_subvol_begin(sctx);
4510 ret = finish_inode_if_needed(sctx, 1);
4514 ret = full_send_tree(sctx);
4521 ret = close_cur_inode_file(sctx);
4523 close_cur_inode_file(sctx);
4525 free_recorded_refs(sctx);
4540 u64 *clone_sources_tmp =
NULL;
4545 send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root;
4607 if (!clone_sources_tmp) {
4629 if (IS_ERR(clone_root)) {
4630 ret = PTR_ERR(clone_root);
4635 vfree(clone_sources_tmp);
4636 clone_sources_tmp =
NULL;
4659 sizeof(*sctx->
clone_roots), __clone_root_cmp_sort,
4662 ret = send_subvol(sctx);
4669 ret = send_cmd(sctx);
4677 vfree(clone_sources_tmp);
4687 name_cache_free(sctx);