Lines Matching refs:sctx
312 static void inconsistent_snapshot_error(struct send_ctx *sctx, in inconsistent_snapshot_error() argument
337 btrfs_err(sctx->send_root->fs_info, in inconsistent_snapshot_error()
339 result_string, what, sctx->cmp_key->objectid, in inconsistent_snapshot_error()
340 sctx->send_root->root_key.objectid, in inconsistent_snapshot_error()
341 (sctx->parent_root ? in inconsistent_snapshot_error()
342 sctx->parent_root->root_key.objectid : 0)); in inconsistent_snapshot_error()
346 static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd) in proto_cmd_ok() argument
348 switch (sctx->proto) { in proto_cmd_ok()
356 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
359 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
361 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
363 static int need_send_hole(struct send_ctx *sctx) in need_send_hole() argument
365 return (sctx->parent_root && !sctx->cur_inode_new && in need_send_hole()
366 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && in need_send_hole()
367 S_ISREG(sctx->cur_inode_mode)); in need_send_hole()
602 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) in tlv_put() argument
606 int left = sctx->send_max_size - sctx->send_size; in tlv_put()
608 if (WARN_ON_ONCE(sctx->put_data)) in tlv_put()
614 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); in tlv_put()
618 sctx->send_size += total_len; in tlv_put()
624 static int tlv_put_u##bits(struct send_ctx *sctx, \
628 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
635 static int tlv_put_string(struct send_ctx *sctx, u16 attr, in tlv_put_string() argument
640 return tlv_put(sctx, attr, str, len); in tlv_put_string()
643 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, in tlv_put_uuid() argument
646 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); in tlv_put_uuid()
649 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, in tlv_put_btrfs_timespec() argument
655 return tlv_put(sctx, attr, &bts, sizeof(bts)); in tlv_put_btrfs_timespec()
659 #define TLV_PUT(sctx, attrtype, data, attrlen) \ argument
661 ret = tlv_put(sctx, attrtype, data, attrlen); \
666 #define TLV_PUT_INT(sctx, attrtype, bits, value) \ argument
668 ret = tlv_put_u##bits(sctx, attrtype, value); \
673 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) argument
674 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) argument
675 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) argument
676 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) argument
677 #define TLV_PUT_STRING(sctx, attrtype, str, len) \ argument
679 ret = tlv_put_string(sctx, attrtype, str, len); \
683 #define TLV_PUT_PATH(sctx, attrtype, p) \ argument
685 ret = tlv_put_string(sctx, attrtype, p->start, \
690 #define TLV_PUT_UUID(sctx, attrtype, uuid) \ argument
692 ret = tlv_put_uuid(sctx, attrtype, uuid); \
696 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ argument
698 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
703 static int send_header(struct send_ctx *sctx) in send_header() argument
708 hdr.version = cpu_to_le32(sctx->proto); in send_header()
709 return write_buf(sctx->send_filp, &hdr, sizeof(hdr), in send_header()
710 &sctx->send_off); in send_header()
716 static int begin_cmd(struct send_ctx *sctx, int cmd) in begin_cmd() argument
720 if (WARN_ON(!sctx->send_buf)) in begin_cmd()
723 BUG_ON(sctx->send_size); in begin_cmd()
725 sctx->send_size += sizeof(*hdr); in begin_cmd()
726 hdr = (struct btrfs_cmd_header *)sctx->send_buf; in begin_cmd()
732 static int send_cmd(struct send_ctx *sctx) in send_cmd() argument
738 hdr = (struct btrfs_cmd_header *)sctx->send_buf; in send_cmd()
739 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len); in send_cmd()
742 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); in send_cmd()
745 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, in send_cmd()
746 &sctx->send_off); in send_cmd()
748 sctx->send_size = 0; in send_cmd()
749 sctx->put_data = false; in send_cmd()
757 static int send_rename(struct send_ctx *sctx, in send_rename() argument
760 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_rename()
765 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); in send_rename()
769 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); in send_rename()
770 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); in send_rename()
772 ret = send_cmd(sctx); in send_rename()
782 static int send_link(struct send_ctx *sctx, in send_link() argument
785 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_link()
790 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); in send_link()
794 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_link()
795 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); in send_link()
797 ret = send_cmd(sctx); in send_link()
807 static int send_unlink(struct send_ctx *sctx, struct fs_path *path) in send_unlink() argument
809 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_unlink()
814 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); in send_unlink()
818 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_unlink()
820 ret = send_cmd(sctx); in send_unlink()
830 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) in send_rmdir() argument
832 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_rmdir()
837 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); in send_rmdir()
841 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_rmdir()
843 ret = send_cmd(sctx); in send_rmdir()
1224 struct send_ctx *sctx; member
1277 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, in __iterate_backrefs()
1278 bctx->sctx->clone_roots_cnt, in __iterate_backrefs()
1284 if (found->root == bctx->sctx->send_root && in __iterate_backrefs()
1294 if (found->root == bctx->sctx->send_root) { in __iterate_backrefs()
1310 bctx->sctx->cur_inode_next_write_offset) in __iterate_backrefs()
1339 static int find_extent_clone(struct send_ctx *sctx, in find_extent_clone() argument
1345 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in find_extent_clone()
1427 for (i = 0; i < sctx->clone_roots_cnt; i++) { in find_extent_clone()
1428 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1434 backref_ctx.sctx = sctx; in find_extent_clone()
1464 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) { in find_extent_clone()
1500 for (i = 0; i < sctx->clone_roots_cnt; i++) { in find_extent_clone()
1501 if (sctx->clone_roots[i].found_refs) { in find_extent_clone()
1503 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1504 else if (sctx->clone_roots[i].root == sctx->send_root) in find_extent_clone()
1506 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1583 static int gen_unique_name(struct send_ctx *sctx, in gen_unique_name() argument
1603 di = btrfs_lookup_dir_item(NULL, sctx->send_root, in gen_unique_name()
1617 if (!sctx->parent_root) { in gen_unique_name()
1623 di = btrfs_lookup_dir_item(NULL, sctx->parent_root, in gen_unique_name()
1655 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) in get_cur_inode_state() argument
1664 ret = get_inode_info(sctx->send_root, ino, &info); in get_cur_inode_state()
1670 if (!sctx->parent_root) { in get_cur_inode_state()
1673 ret = get_inode_info(sctx->parent_root, ino, &info); in get_cur_inode_state()
1684 if (ino < sctx->send_progress) in get_cur_inode_state()
1689 if (ino < sctx->send_progress) in get_cur_inode_state()
1698 if (ino < sctx->send_progress) in get_cur_inode_state()
1707 if (ino < sctx->send_progress) in get_cur_inode_state()
1722 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) in is_inode_existent() argument
1729 ret = get_cur_inode_state(sctx, ino, gen); in is_inode_existent()
1886 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, in will_overwrite_ref() argument
1895 if (!sctx->parent_root) in will_overwrite_ref()
1898 ret = is_inode_existent(sctx, dir, dir_gen); in will_overwrite_ref()
1907 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) { in will_overwrite_ref()
1908 ret = get_inode_gen(sctx->parent_root, dir, &gen); in will_overwrite_ref()
1919 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, in will_overwrite_ref()
1933 if (other_inode > sctx->send_progress || in will_overwrite_ref()
1934 is_waiting_for_move(sctx, other_inode)) { in will_overwrite_ref()
1935 ret = get_inode_info(sctx->parent_root, other_inode, &info); in will_overwrite_ref()
1958 static int did_overwrite_ref(struct send_ctx *sctx, in did_overwrite_ref() argument
1967 if (!sctx->parent_root) in did_overwrite_ref()
1970 ret = is_inode_existent(sctx, dir, dir_gen); in did_overwrite_ref()
1975 ret = get_inode_gen(sctx->send_root, dir, &gen); in did_overwrite_ref()
1987 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, in did_overwrite_ref()
1997 ret = get_inode_gen(sctx->send_root, ow_inode, &gen); in did_overwrite_ref()
2012 if ((ow_inode < sctx->send_progress) || in did_overwrite_ref()
2013 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino && in did_overwrite_ref()
2014 gen == sctx->cur_inode_gen)) in did_overwrite_ref()
2028 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) in did_overwrite_first_ref() argument
2035 if (!sctx->parent_root) in did_overwrite_first_ref()
2042 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); in did_overwrite_first_ref()
2046 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, in did_overwrite_first_ref()
2060 static int name_cache_insert(struct send_ctx *sctx, in name_cache_insert() argument
2066 nce_head = radix_tree_lookup(&sctx->name_cache, in name_cache_insert()
2076 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); in name_cache_insert()
2084 list_add_tail(&nce->list, &sctx->name_cache_list); in name_cache_insert()
2085 sctx->name_cache_size++; in name_cache_insert()
2090 static void name_cache_delete(struct send_ctx *sctx, in name_cache_delete() argument
2095 nce_head = radix_tree_lookup(&sctx->name_cache, in name_cache_delete()
2098 btrfs_err(sctx->send_root->fs_info, in name_cache_delete()
2100 nce->ino, sctx->name_cache_size); in name_cache_delete()
2105 sctx->name_cache_size--; in name_cache_delete()
2111 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); in name_cache_delete()
2116 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, in name_cache_search() argument
2122 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); in name_cache_search()
2136 static void name_cache_clean_unused(struct send_ctx *sctx) in name_cache_clean_unused() argument
2140 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) in name_cache_clean_unused()
2143 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { in name_cache_clean_unused()
2144 nce = list_entry(sctx->name_cache_list.next, in name_cache_clean_unused()
2146 name_cache_delete(sctx, nce); in name_cache_clean_unused()
2151 static void name_cache_free(struct send_ctx *sctx) in name_cache_free() argument
2155 while (!list_empty(&sctx->name_cache_list)) { in name_cache_free()
2156 nce = list_entry(sctx->name_cache_list.next, in name_cache_free()
2158 name_cache_delete(sctx, nce); in name_cache_free()
2171 static int __get_cur_name_and_parent(struct send_ctx *sctx, in __get_cur_name_and_parent() argument
2186 nce = name_cache_search(sctx, ino, gen); in __get_cur_name_and_parent()
2188 if (ino < sctx->send_progress && nce->need_later_update) { in __get_cur_name_and_parent()
2189 name_cache_delete(sctx, nce); in __get_cur_name_and_parent()
2198 list_move_tail(&nce->list, &sctx->name_cache_list); in __get_cur_name_and_parent()
2215 ret = is_inode_existent(sctx, ino, gen); in __get_cur_name_and_parent()
2220 ret = gen_unique_name(sctx, ino, gen, dest); in __get_cur_name_and_parent()
2231 if (ino < sctx->send_progress) in __get_cur_name_and_parent()
2232 ret = get_first_ref(sctx->send_root, ino, in __get_cur_name_and_parent()
2235 ret = get_first_ref(sctx->parent_root, ino, in __get_cur_name_and_parent()
2244 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, in __get_cur_name_and_parent()
2250 ret = gen_unique_name(sctx, ino, gen, dest); in __get_cur_name_and_parent()
2274 if (ino < sctx->send_progress) in __get_cur_name_and_parent()
2279 nce_ret = name_cache_insert(sctx, nce); in __get_cur_name_and_parent()
2282 name_cache_clean_unused(sctx); in __get_cur_name_and_parent()
2313 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, in get_cur_path() argument
2336 if (is_waiting_for_rm(sctx, ino, gen)) { in get_cur_path()
2337 ret = gen_unique_name(sctx, ino, gen, name); in get_cur_path()
2344 wdm = get_waiting_dir_move(sctx, ino); in get_cur_path()
2346 ret = gen_unique_name(sctx, ino, gen, name); in get_cur_path()
2349 ret = get_first_ref(sctx->parent_root, ino, in get_cur_path()
2352 ret = __get_cur_name_and_parent(sctx, ino, gen, in get_cur_path()
2380 static int send_subvol_begin(struct send_ctx *sctx) in send_subvol_begin() argument
2383 struct btrfs_root *send_root = sctx->send_root; in send_subvol_begin()
2384 struct btrfs_root *parent_root = sctx->parent_root; in send_subvol_begin()
2428 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); in send_subvol_begin()
2432 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); in send_subvol_begin()
2437 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); in send_subvol_begin()
2439 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid)) in send_subvol_begin()
2440 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, in send_subvol_begin()
2441 sctx->send_root->root_item.received_uuid); in send_subvol_begin()
2443 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, in send_subvol_begin()
2444 sctx->send_root->root_item.uuid); in send_subvol_begin()
2446 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, in send_subvol_begin()
2447 btrfs_root_ctransid(&sctx->send_root->root_item)); in send_subvol_begin()
2450 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_subvol_begin()
2453 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_subvol_begin()
2455 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, in send_subvol_begin()
2456 btrfs_root_ctransid(&sctx->parent_root->root_item)); in send_subvol_begin()
2459 ret = send_cmd(sctx); in send_subvol_begin()
2468 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) in send_truncate() argument
2470 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_truncate()
2480 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); in send_truncate()
2484 ret = get_cur_path(sctx, ino, gen, p); in send_truncate()
2487 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_truncate()
2488 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); in send_truncate()
2490 ret = send_cmd(sctx); in send_truncate()
2498 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) in send_chmod() argument
2500 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_chmod()
2510 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); in send_chmod()
2514 ret = get_cur_path(sctx, ino, gen, p); in send_chmod()
2517 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_chmod()
2518 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); in send_chmod()
2520 ret = send_cmd(sctx); in send_chmod()
2528 static int send_fileattr(struct send_ctx *sctx, u64 ino, u64 gen, u64 fileattr) in send_fileattr() argument
2530 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_fileattr()
2534 if (sctx->proto < 2) in send_fileattr()
2543 ret = begin_cmd(sctx, BTRFS_SEND_C_FILEATTR); in send_fileattr()
2547 ret = get_cur_path(sctx, ino, gen, p); in send_fileattr()
2550 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_fileattr()
2551 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILEATTR, fileattr); in send_fileattr()
2553 ret = send_cmd(sctx); in send_fileattr()
2561 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) in send_chown() argument
2563 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_chown()
2574 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); in send_chown()
2578 ret = get_cur_path(sctx, ino, gen, p); in send_chown()
2581 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_chown()
2582 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); in send_chown()
2583 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); in send_chown()
2585 ret = send_cmd(sctx); in send_chown()
2593 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) in send_utimes() argument
2595 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_utimes()
2619 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); in send_utimes()
2629 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); in send_utimes()
2633 ret = get_cur_path(sctx, ino, gen, p); in send_utimes()
2636 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_utimes()
2637 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime); in send_utimes()
2638 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime); in send_utimes()
2639 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime); in send_utimes()
2640 if (sctx->proto >= 2) in send_utimes()
2641 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_OTIME, eb, &ii->otime); in send_utimes()
2643 ret = send_cmd(sctx); in send_utimes()
2657 static int send_create_inode(struct send_ctx *sctx, u64 ino) in send_create_inode() argument
2659 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_create_inode()
2674 if (ino != sctx->cur_ino) { in send_create_inode()
2675 ret = get_inode_info(sctx->send_root, ino, &info); in send_create_inode()
2682 gen = sctx->cur_inode_gen; in send_create_inode()
2683 mode = sctx->cur_inode_mode; in send_create_inode()
2684 rdev = sctx->cur_inode_rdev; in send_create_inode()
2700 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o", in send_create_inode()
2706 ret = begin_cmd(sctx, cmd); in send_create_inode()
2710 ret = gen_unique_name(sctx, ino, gen, p); in send_create_inode()
2714 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_create_inode()
2715 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); in send_create_inode()
2719 ret = read_symlink(sctx->send_root, ino, p); in send_create_inode()
2722 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); in send_create_inode()
2725 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); in send_create_inode()
2726 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); in send_create_inode()
2729 ret = send_cmd(sctx); in send_create_inode()
2745 static int did_create_dir(struct send_ctx *sctx, u64 dir) in did_create_dir() argument
2763 btrfs_for_each_slot(sctx->send_root, &key, &found_key, path, iter_ret) { in did_create_dir()
2776 di_key.objectid < sctx->send_progress) { in did_create_dir()
2795 static int send_create_inode_if_needed(struct send_ctx *sctx) in send_create_inode_if_needed() argument
2799 if (S_ISDIR(sctx->cur_inode_mode)) { in send_create_inode_if_needed()
2800 ret = did_create_dir(sctx, sctx->cur_ino); in send_create_inode_if_needed()
2807 return send_create_inode(sctx, sctx->cur_ino); in send_create_inode_if_needed()
2875 static void free_recorded_refs(struct send_ctx *sctx) in free_recorded_refs() argument
2877 __free_recorded_refs(&sctx->new_refs); in free_recorded_refs()
2878 __free_recorded_refs(&sctx->deleted_refs); in free_recorded_refs()
2886 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, in orphanize_inode() argument
2896 ret = gen_unique_name(sctx, ino, gen, orphan); in orphanize_inode()
2900 ret = send_rename(sctx, path, orphan); in orphanize_inode()
2907 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx, in add_orphan_dir_info() argument
2910 struct rb_node **p = &sctx->orphan_dirs.rb_node; in add_orphan_dir_info()
2937 rb_insert_color(&odi->node, &sctx->orphan_dirs); in add_orphan_dir_info()
2941 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx, in get_orphan_dir_info() argument
2944 struct rb_node *n = sctx->orphan_dirs.rb_node; in get_orphan_dir_info()
2963 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen) in is_waiting_for_rm() argument
2965 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen); in is_waiting_for_rm()
2970 static void free_orphan_dir_info(struct send_ctx *sctx, in free_orphan_dir_info() argument
2975 rb_erase(&odi->node, &sctx->orphan_dirs); in free_orphan_dir_info()
2984 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen, in can_rmdir() argument
2989 struct btrfs_root *root = sctx->parent_root; in can_rmdir()
3011 odi = get_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
3026 dm = get_waiting_dir_move(sctx, loc.objectid); in can_rmdir()
3028 odi = add_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
3042 odi = add_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
3057 free_orphan_dir_info(sctx, odi); in can_rmdir()
3066 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) in is_waiting_for_move() argument
3068 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino); in is_waiting_for_move()
3073 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized) in add_waiting_dir_move() argument
3075 struct rb_node **p = &sctx->waiting_dir_moves.rb_node; in add_waiting_dir_move()
3101 rb_insert_color(&dm->node, &sctx->waiting_dir_moves); in add_waiting_dir_move()
3106 get_waiting_dir_move(struct send_ctx *sctx, u64 ino) in get_waiting_dir_move() argument
3108 struct rb_node *n = sctx->waiting_dir_moves.rb_node; in get_waiting_dir_move()
3123 static void free_waiting_dir_move(struct send_ctx *sctx, in free_waiting_dir_move() argument
3128 rb_erase(&dm->node, &sctx->waiting_dir_moves); in free_waiting_dir_move()
3132 static int add_pending_dir_move(struct send_ctx *sctx, in add_pending_dir_move() argument
3140 struct rb_node **p = &sctx->pending_dir_moves.rb_node; in add_pending_dir_move()
3181 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan); in add_pending_dir_move()
3189 rb_insert_color(&pm->node, &sctx->pending_dir_moves); in add_pending_dir_move()
3200 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx, in get_pending_dir_moves() argument
3203 struct rb_node *n = sctx->pending_dir_moves.rb_node; in get_pending_dir_moves()
3218 static int path_loop(struct send_ctx *sctx, struct fs_path *name, in path_loop() argument
3230 if (is_waiting_for_rm(sctx, ino, gen)) in path_loop()
3232 if (is_waiting_for_move(sctx, ino)) { in path_loop()
3235 ret = get_first_ref(sctx->parent_root, ino, in path_loop()
3238 ret = __get_cur_name_and_parent(sctx, ino, gen, in path_loop()
3260 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm) in apply_dir_move() argument
3265 u64 orig_progress = sctx->send_progress; in apply_dir_move()
3282 dm = get_waiting_dir_move(sctx, pm->ino); in apply_dir_move()
3287 free_waiting_dir_move(sctx, dm); in apply_dir_move()
3290 ret = gen_unique_name(sctx, pm->ino, in apply_dir_move()
3293 ret = get_first_ref(sctx->parent_root, pm->ino, in apply_dir_move()
3297 ret = get_cur_path(sctx, parent_ino, parent_gen, in apply_dir_move()
3306 sctx->send_progress = sctx->cur_ino + 1; in apply_dir_move()
3307 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor); in apply_dir_move()
3313 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor, in apply_dir_move()
3319 dm = get_waiting_dir_move(sctx, pm->ino); in apply_dir_move()
3329 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path); in apply_dir_move()
3333 ret = send_rename(sctx, from_path, to_path); in apply_dir_move()
3341 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen); in apply_dir_move()
3348 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino); in apply_dir_move()
3359 ret = get_cur_path(sctx, rmdir_ino, gen, name); in apply_dir_move()
3362 ret = send_rmdir(sctx, name); in apply_dir_move()
3368 ret = send_utimes(sctx, pm->ino, pm->gen); in apply_dir_move()
3380 ret = get_inode_info(sctx->send_root, cur->dir, NULL); in apply_dir_move()
3388 ret = send_utimes(sctx, cur->dir, cur->dir_gen); in apply_dir_move()
3397 sctx->send_progress = orig_progress; in apply_dir_move()
3402 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m) in free_pending_move() argument
3407 rb_erase(&m->node, &sctx->pending_dir_moves); in free_pending_move()
3412 static void tail_append_pending_moves(struct send_ctx *sctx, in tail_append_pending_moves() argument
3425 rb_erase(&moves->node, &sctx->pending_dir_moves); in tail_append_pending_moves()
3430 static int apply_children_dir_moves(struct send_ctx *sctx) in apply_children_dir_moves() argument
3434 u64 parent_ino = sctx->cur_ino; in apply_children_dir_moves()
3437 pm = get_pending_dir_moves(sctx, parent_ino); in apply_children_dir_moves()
3442 tail_append_pending_moves(sctx, pm, &stack); in apply_children_dir_moves()
3447 ret = apply_dir_move(sctx, pm); in apply_children_dir_moves()
3448 free_pending_move(sctx, pm); in apply_children_dir_moves()
3451 pm = get_pending_dir_moves(sctx, parent_ino); in apply_children_dir_moves()
3453 tail_append_pending_moves(sctx, pm, &stack); in apply_children_dir_moves()
3460 free_pending_move(sctx, pm); in apply_children_dir_moves()
3501 static int wait_for_dest_dir_move(struct send_ctx *sctx, in wait_for_dest_dir_move() argument
3505 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info; in wait_for_dest_dir_move()
3515 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) in wait_for_dest_dir_move()
3526 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); in wait_for_dest_dir_move()
3554 ret = get_inode_gen(sctx->parent_root, di_key.objectid, &left_gen); in wait_for_dest_dir_move()
3557 ret = get_inode_gen(sctx->send_root, di_key.objectid, &right_gen); in wait_for_dest_dir_move()
3570 wdm = get_waiting_dir_move(sctx, di_key.objectid); in wait_for_dest_dir_move()
3572 ret = add_pending_dir_move(sctx, in wait_for_dest_dir_move()
3573 sctx->cur_ino, in wait_for_dest_dir_move()
3574 sctx->cur_inode_gen, in wait_for_dest_dir_move()
3576 &sctx->new_refs, in wait_for_dest_dir_move()
3577 &sctx->deleted_refs, in wait_for_dest_dir_move()
3707 static int wait_for_parent_move(struct send_ctx *sctx, in wait_for_parent_move() argument
3736 if (is_waiting_for_move(sctx, ino)) { in wait_for_parent_move()
3747 ret = is_ancestor(sctx->parent_root, in wait_for_parent_move()
3748 sctx->cur_ino, sctx->cur_inode_gen, in wait_for_parent_move()
3757 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after, in wait_for_parent_move()
3761 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before, in wait_for_parent_move()
3772 if (ino > sctx->cur_ino && in wait_for_parent_move()
3777 ret = get_inode_gen(sctx->parent_root, ino, &parent_ino_gen); in wait_for_parent_move()
3794 ret = add_pending_dir_move(sctx, in wait_for_parent_move()
3795 sctx->cur_ino, in wait_for_parent_move()
3796 sctx->cur_inode_gen, in wait_for_parent_move()
3798 &sctx->new_refs, in wait_for_parent_move()
3799 &sctx->deleted_refs, in wait_for_parent_move()
3808 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) in update_ref_path() argument
3821 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path); in update_ref_path()
3879 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) in refresh_ref_path() argument
3889 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path); in refresh_ref_path()
3907 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move) in process_recorded_refs() argument
3909 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in process_recorded_refs()
3925 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino); in process_recorded_refs()
3931 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); in process_recorded_refs()
3951 if (!sctx->cur_inode_new) { in process_recorded_refs()
3952 ret = did_overwrite_first_ref(sctx, sctx->cur_ino, in process_recorded_refs()
3953 sctx->cur_inode_gen); in process_recorded_refs()
3959 if (sctx->cur_inode_new || did_overwrite) { in process_recorded_refs()
3960 ret = gen_unique_name(sctx, sctx->cur_ino, in process_recorded_refs()
3961 sctx->cur_inode_gen, valid_path); in process_recorded_refs()
3966 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
4010 list_for_each_entry(cur, &sctx->new_refs, list) { in process_recorded_refs()
4011 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4023 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
4029 ret = is_first_ref(sctx->parent_root, in process_recorded_refs()
4039 ret = refresh_ref_path(sctx, cur); in process_recorded_refs()
4044 ret = orphanize_inode(sctx, ow_inode, ow_gen, in process_recorded_refs()
4057 if (is_waiting_for_move(sctx, ow_inode)) { in process_recorded_refs()
4058 wdm = get_waiting_dir_move(sctx, in process_recorded_refs()
4074 nce = name_cache_search(sctx, ow_inode, ow_gen); in process_recorded_refs()
4076 name_cache_delete(sctx, nce); in process_recorded_refs()
4087 ret = is_ancestor(sctx->parent_root, in process_recorded_refs()
4089 sctx->cur_ino, NULL); in process_recorded_refs()
4093 ret = get_cur_path(sctx, sctx->cur_ino, in process_recorded_refs()
4094 sctx->cur_inode_gen, in process_recorded_refs()
4107 ret = refresh_ref_path(sctx, cur); in process_recorded_refs()
4111 ret = send_unlink(sctx, cur->full_path); in process_recorded_refs()
4119 list_for_each_entry(cur, &sctx->new_refs, list) { in process_recorded_refs()
4127 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4136 list_for_each_entry(cur2, &sctx->new_refs, list) { in process_recorded_refs()
4150 ret = did_create_dir(sctx, cur->dir); in process_recorded_refs()
4154 ret = send_create_inode(sctx, cur->dir); in process_recorded_refs()
4160 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) { in process_recorded_refs()
4161 ret = wait_for_dest_dir_move(sctx, cur, is_orphan); in process_recorded_refs()
4170 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root && in process_recorded_refs()
4172 ret = wait_for_parent_move(sctx, cur, is_orphan); in process_recorded_refs()
4187 ret = send_rename(sctx, valid_path, cur->full_path); in process_recorded_refs()
4195 if (S_ISDIR(sctx->cur_inode_mode)) { in process_recorded_refs()
4201 ret = send_rename(sctx, valid_path, in process_recorded_refs()
4217 ret = update_ref_path(sctx, cur); in process_recorded_refs()
4221 ret = send_link(sctx, cur->full_path, in process_recorded_refs()
4232 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { in process_recorded_refs()
4239 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
4240 sctx->cur_ino); in process_recorded_refs()
4244 ret = send_rmdir(sctx, valid_path); in process_recorded_refs()
4248 ret = orphanize_inode(sctx, sctx->cur_ino, in process_recorded_refs()
4249 sctx->cur_inode_gen, valid_path); in process_recorded_refs()
4255 list_for_each_entry(cur, &sctx->deleted_refs, list) { in process_recorded_refs()
4260 } else if (S_ISDIR(sctx->cur_inode_mode) && in process_recorded_refs()
4261 !list_empty(&sctx->deleted_refs)) { in process_recorded_refs()
4265 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, in process_recorded_refs()
4270 } else if (!S_ISDIR(sctx->cur_inode_mode)) { in process_recorded_refs()
4276 list_for_each_entry(cur, &sctx->deleted_refs, list) { in process_recorded_refs()
4277 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
4278 sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
4291 ret = update_ref_path(sctx, cur); in process_recorded_refs()
4295 ret = send_unlink(sctx, cur->full_path); in process_recorded_refs()
4312 ret = send_unlink(sctx, valid_path); in process_recorded_refs()
4330 if (cur->dir > sctx->cur_ino) in process_recorded_refs()
4333 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4340 ret = send_utimes(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4345 ret = can_rmdir(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
4346 sctx->cur_ino); in process_recorded_refs()
4350 ret = get_cur_path(sctx, cur->dir, in process_recorded_refs()
4354 ret = send_rmdir(sctx, valid_path); in process_recorded_refs()
4366 free_recorded_refs(sctx); in process_recorded_refs()
4406 struct send_ctx *sctx) in record_ref_in_tree() argument
4424 ret = get_cur_path(sctx, dir, dir_gen, path); in record_ref_in_tree()
4450 struct send_ctx *sctx = ctx; in record_new_ref_if_needed() local
4456 ret = get_inode_gen(sctx->send_root, dir, &dir_gen); in record_new_ref_if_needed()
4463 node = rb_find(&data, &sctx->rbtree_deleted_refs, rbtree_ref_comp); in record_new_ref_if_needed()
4468 ret = record_ref_in_tree(&sctx->rbtree_new_refs, in record_new_ref_if_needed()
4469 &sctx->new_refs, name, dir, dir_gen, in record_new_ref_if_needed()
4470 sctx); in record_new_ref_if_needed()
4480 struct send_ctx *sctx = ctx; in record_deleted_ref_if_needed() local
4486 ret = get_inode_gen(sctx->parent_root, dir, &dir_gen); in record_deleted_ref_if_needed()
4493 node = rb_find(&data, &sctx->rbtree_new_refs, rbtree_ref_comp); in record_deleted_ref_if_needed()
4498 ret = record_ref_in_tree(&sctx->rbtree_deleted_refs, in record_deleted_ref_if_needed()
4499 &sctx->deleted_refs, name, dir, in record_deleted_ref_if_needed()
4500 dir_gen, sctx); in record_deleted_ref_if_needed()
4506 static int record_new_ref(struct send_ctx *sctx) in record_new_ref() argument
4510 ret = iterate_inode_ref(sctx->send_root, sctx->left_path, in record_new_ref()
4511 sctx->cmp_key, 0, record_new_ref_if_needed, sctx); in record_new_ref()
4520 static int record_deleted_ref(struct send_ctx *sctx) in record_deleted_ref() argument
4524 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, in record_deleted_ref()
4525 sctx->cmp_key, 0, record_deleted_ref_if_needed, in record_deleted_ref()
4526 sctx); in record_deleted_ref()
4535 static int record_changed_ref(struct send_ctx *sctx) in record_changed_ref() argument
4539 ret = iterate_inode_ref(sctx->send_root, sctx->left_path, in record_changed_ref()
4540 sctx->cmp_key, 0, record_new_ref_if_needed, sctx); in record_changed_ref()
4543 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, in record_changed_ref()
4544 sctx->cmp_key, 0, record_deleted_ref_if_needed, sctx); in record_changed_ref()
4557 static int process_all_refs(struct send_ctx *sctx, in process_all_refs() argument
4574 root = sctx->send_root; in process_all_refs()
4577 root = sctx->parent_root; in process_all_refs()
4580 btrfs_err(sctx->send_root->fs_info, in process_all_refs()
4586 key.objectid = sctx->cmp_key->objectid; in process_all_refs()
4595 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx); in process_all_refs()
4611 ret = process_recorded_refs(sctx, &pending_move); in process_all_refs()
4617 static int send_set_xattr(struct send_ctx *sctx, in send_set_xattr() argument
4624 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); in send_set_xattr()
4628 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_set_xattr()
4629 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); in send_set_xattr()
4630 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); in send_set_xattr()
4632 ret = send_cmd(sctx); in send_set_xattr()
4639 static int send_remove_xattr(struct send_ctx *sctx, in send_remove_xattr() argument
4645 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); in send_remove_xattr()
4649 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_remove_xattr()
4650 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); in send_remove_xattr()
4652 ret = send_cmd(sctx); in send_remove_xattr()
4664 struct send_ctx *sctx = ctx; in __process_new_xattr() local
4692 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in __process_new_xattr()
4696 ret = send_set_xattr(sctx, p, name, name_len, data, data_len); in __process_new_xattr()
4708 struct send_ctx *sctx = ctx; in __process_deleted_xattr() local
4715 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in __process_deleted_xattr()
4719 ret = send_remove_xattr(sctx, p, name, name_len); in __process_deleted_xattr()
4726 static int process_new_xattr(struct send_ctx *sctx) in process_new_xattr() argument
4730 ret = iterate_dir_item(sctx->send_root, sctx->left_path, in process_new_xattr()
4731 __process_new_xattr, sctx); in process_new_xattr()
4736 static int process_deleted_xattr(struct send_ctx *sctx) in process_deleted_xattr() argument
4738 return iterate_dir_item(sctx->parent_root, sctx->right_path, in process_deleted_xattr()
4739 __process_deleted_xattr, sctx); in process_deleted_xattr()
4804 struct send_ctx *sctx = ctx; in __process_changed_new_xattr() local
4808 ret = find_xattr(sctx->parent_root, sctx->right_path, in __process_changed_new_xattr()
4809 sctx->cmp_key, name, name_len, &found_data, in __process_changed_new_xattr()
4834 struct send_ctx *sctx = ctx; in __process_changed_deleted_xattr() local
4836 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key, in __process_changed_deleted_xattr()
4847 static int process_changed_xattr(struct send_ctx *sctx) in process_changed_xattr() argument
4851 ret = iterate_dir_item(sctx->send_root, sctx->left_path, in process_changed_xattr()
4852 __process_changed_new_xattr, sctx); in process_changed_xattr()
4855 ret = iterate_dir_item(sctx->parent_root, sctx->right_path, in process_changed_xattr()
4856 __process_changed_deleted_xattr, sctx); in process_changed_xattr()
4862 static int process_all_new_xattrs(struct send_ctx *sctx) in process_all_new_xattrs() argument
4875 root = sctx->send_root; in process_all_new_xattrs()
4877 key.objectid = sctx->cmp_key->objectid; in process_all_new_xattrs()
4887 ret = iterate_dir_item(root, path, __process_new_xattr, sctx); in process_all_new_xattrs()
4899 static int send_verity(struct send_ctx *sctx, struct fs_path *path, in send_verity() argument
4904 ret = begin_cmd(sctx, BTRFS_SEND_C_ENABLE_VERITY); in send_verity()
4908 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_verity()
4909 TLV_PUT_U8(sctx, BTRFS_SEND_A_VERITY_ALGORITHM, in send_verity()
4911 TLV_PUT_U32(sctx, BTRFS_SEND_A_VERITY_BLOCK_SIZE, in send_verity()
4913 TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SALT_DATA, desc->salt, in send_verity()
4915 TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SIG_DATA, desc->signature, in send_verity()
4918 ret = send_cmd(sctx); in send_verity()
4925 static int process_verity(struct send_ctx *sctx) in process_verity() argument
4928 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in process_verity()
4932 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, sctx->send_root); in process_verity()
4944 if (!sctx->verity_descriptor) { in process_verity()
4945 sctx->verity_descriptor = kvmalloc(FS_VERITY_MAX_DESCRIPTOR_SIZE, in process_verity()
4947 if (!sctx->verity_descriptor) { in process_verity()
4953 ret = btrfs_get_verity_descriptor(inode, sctx->verity_descriptor, ret); in process_verity()
4962 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in process_verity()
4966 ret = send_verity(sctx, p, sctx->verity_descriptor); in process_verity()
4977 static inline u64 max_send_read_size(const struct send_ctx *sctx) in max_send_read_size() argument
4979 return sctx->send_max_size - SZ_16K; in max_send_read_size()
4982 static int put_data_header(struct send_ctx *sctx, u32 len) in put_data_header() argument
4984 if (WARN_ON_ONCE(sctx->put_data)) in put_data_header()
4986 sctx->put_data = true; in put_data_header()
4987 if (sctx->proto >= 2) { in put_data_header()
4992 if (sctx->send_max_size - sctx->send_size < sizeof(__le16) + len) in put_data_header()
4994 put_unaligned_le16(BTRFS_SEND_A_DATA, sctx->send_buf + sctx->send_size); in put_data_header()
4995 sctx->send_size += sizeof(__le16); in put_data_header()
4999 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len) in put_data_header()
5001 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size); in put_data_header()
5004 sctx->send_size += sizeof(*hdr); in put_data_header()
5009 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len) in put_file_data() argument
5011 struct btrfs_root *root = sctx->send_root; in put_file_data()
5019 ret = put_data_header(sctx, len); in put_file_data()
5029 page = find_lock_page(sctx->cur_inode->i_mapping, index); in put_file_data()
5031 page_cache_sync_readahead(sctx->cur_inode->i_mapping, in put_file_data()
5032 &sctx->ra, NULL, index, in put_file_data()
5035 page = find_or_create_page(sctx->cur_inode->i_mapping, in put_file_data()
5044 page_cache_async_readahead(sctx->cur_inode->i_mapping, in put_file_data()
5045 &sctx->ra, NULL, page_folio(page), in put_file_data()
5055 page_offset(page), sctx->cur_ino, in put_file_data()
5056 sctx->send_root->root_key.objectid); in put_file_data()
5063 memcpy_from_page(sctx->send_buf + sctx->send_size, page, in put_file_data()
5070 sctx->send_size += cur_len; in put_file_data()
5080 static int send_write(struct send_ctx *sctx, u64 offset, u32 len) in send_write() argument
5082 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_write()
5092 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); in send_write()
5096 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_write()
5100 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_write()
5101 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_write()
5102 ret = put_file_data(sctx, offset, len); in send_write()
5106 ret = send_cmd(sctx); in send_write()
5117 static int send_clone(struct send_ctx *sctx, in send_clone() argument
5125 btrfs_debug(sctx->send_root->fs_info, in send_clone()
5134 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); in send_clone()
5138 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_clone()
5142 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_clone()
5143 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); in send_clone()
5144 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_clone()
5146 if (clone_root->root == sctx->send_root) { in send_clone()
5147 ret = get_inode_gen(sctx->send_root, clone_root->ino, &gen); in send_clone()
5150 ret = get_cur_path(sctx, clone_root->ino, gen, p); in send_clone()
5167 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_clone()
5170 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_clone()
5172 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, in send_clone()
5174 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); in send_clone()
5175 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, in send_clone()
5178 ret = send_cmd(sctx); in send_clone()
5189 static int send_update_extent(struct send_ctx *sctx, in send_update_extent() argument
5199 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT); in send_update_extent()
5203 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_update_extent()
5207 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_update_extent()
5208 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_update_extent()
5209 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); in send_update_extent()
5211 ret = send_cmd(sctx); in send_update_extent()
5219 static int send_hole(struct send_ctx *sctx, u64 end) in send_hole() argument
5222 u64 read_size = max_send_read_size(sctx); in send_hole()
5223 u64 offset = sctx->cur_inode_last_extent; in send_hole()
5232 if (offset >= sctx->cur_inode_size) in send_hole()
5239 end = min_t(u64, end, sctx->cur_inode_size); in send_hole()
5241 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) in send_hole()
5242 return send_update_extent(sctx, offset, end - offset); in send_hole()
5247 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_hole()
5253 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); in send_hole()
5256 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_hole()
5257 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_hole()
5258 ret = put_data_header(sctx, len); in send_hole()
5261 memset(sctx->send_buf + sctx->send_size, 0, len); in send_hole()
5262 sctx->send_size += len; in send_hole()
5263 ret = send_cmd(sctx); in send_hole()
5268 sctx->cur_inode_next_write_offset = offset; in send_hole()
5274 static int send_encoded_inline_extent(struct send_ctx *sctx, in send_encoded_inline_extent() argument
5278 struct btrfs_root *root = sctx->send_root; in send_encoded_inline_extent()
5289 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root); in send_encoded_inline_extent()
5299 ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE); in send_encoded_inline_extent()
5303 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); in send_encoded_inline_extent()
5312 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath); in send_encoded_inline_extent()
5313 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_encoded_inline_extent()
5314 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN, in send_encoded_inline_extent()
5316 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, ram_bytes); in send_encoded_inline_extent()
5317 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET, offset - key.offset); in send_encoded_inline_extent()
5322 TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret); in send_encoded_inline_extent()
5324 ret = put_data_header(sctx, inline_size); in send_encoded_inline_extent()
5327 read_extent_buffer(leaf, sctx->send_buf + sctx->send_size, in send_encoded_inline_extent()
5329 sctx->send_size += inline_size; in send_encoded_inline_extent()
5331 ret = send_cmd(sctx); in send_encoded_inline_extent()
5340 static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path, in send_encoded_extent() argument
5343 struct btrfs_root *root = sctx->send_root; in send_encoded_extent()
5356 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root); in send_encoded_extent()
5366 ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE); in send_encoded_extent()
5370 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); in send_encoded_extent()
5379 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath); in send_encoded_extent()
5380 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_encoded_extent()
5381 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN, in send_encoded_extent()
5384 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, in send_encoded_extent()
5386 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET, in send_encoded_extent()
5392 TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret); in send_encoded_extent()
5393 TLV_PUT_U32(sctx, BTRFS_SEND_A_ENCRYPTION, 0); in send_encoded_extent()
5395 ret = put_data_header(sctx, disk_num_bytes); in send_encoded_extent()
5404 data_offset = ALIGN(sctx->send_size, PAGE_SIZE); in send_encoded_extent()
5405 if (data_offset > sctx->send_max_size || in send_encoded_extent()
5406 sctx->send_max_size - data_offset < disk_num_bytes) { in send_encoded_extent()
5417 sctx->send_buf_pages + in send_encoded_extent()
5422 hdr = (struct btrfs_cmd_header *)sctx->send_buf; in send_encoded_extent()
5423 hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr)); in send_encoded_extent()
5425 crc = btrfs_crc32c(0, sctx->send_buf, sctx->send_size); in send_encoded_extent()
5426 crc = btrfs_crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes); in send_encoded_extent()
5429 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, in send_encoded_extent()
5430 &sctx->send_off); in send_encoded_extent()
5432 ret = write_buf(sctx->send_filp, sctx->send_buf + data_offset, in send_encoded_extent()
5433 disk_num_bytes, &sctx->send_off); in send_encoded_extent()
5435 sctx->send_size = 0; in send_encoded_extent()
5436 sctx->put_data = false; in send_encoded_extent()
5445 static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path, in send_extent_data() argument
5451 u64 read_size = max_send_read_size(sctx); in send_extent_data()
5454 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) in send_extent_data()
5455 return send_update_extent(sctx, offset, len); in send_extent_data()
5459 if ((sctx->flags & BTRFS_SEND_FLAG_COMPRESSED) && in send_extent_data()
5474 return send_encoded_inline_extent(sctx, path, offset, in send_extent_data()
5478 return send_encoded_extent(sctx, path, offset, len); in send_extent_data()
5482 if (sctx->cur_inode == NULL) { in send_extent_data()
5483 struct btrfs_root *root = sctx->send_root; in send_extent_data()
5485 sctx->cur_inode = btrfs_iget(root->fs_info->sb, sctx->cur_ino, root); in send_extent_data()
5486 if (IS_ERR(sctx->cur_inode)) { in send_extent_data()
5487 int err = PTR_ERR(sctx->cur_inode); in send_extent_data()
5489 sctx->cur_inode = NULL; in send_extent_data()
5492 memset(&sctx->ra, 0, sizeof(struct file_ra_state)); in send_extent_data()
5493 file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping); in send_extent_data()
5514 sctx->clean_page_cache = (sctx->cur_inode->i_mapping->nrpages == 0); in send_extent_data()
5515 sctx->page_cache_clear_start = round_down(offset, PAGE_SIZE); in send_extent_data()
5522 ret = send_write(sctx, offset + sent, size); in send_extent_data()
5528 if (sctx->clean_page_cache && IS_ALIGNED(end, PAGE_SIZE)) { in send_extent_data()
5552 truncate_inode_pages_range(&sctx->cur_inode->i_data, in send_extent_data()
5553 sctx->page_cache_clear_start, in send_extent_data()
5555 sctx->page_cache_clear_start = end; in send_extent_data()
5568 static int send_capabilities(struct send_ctx *sctx) in send_capabilities() argument
5583 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino, in send_capabilities()
5603 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); in send_capabilities()
5610 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS, in send_capabilities()
5619 static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path, in clone_range() argument
5645 len == sctx->send_root->fs_info->sectorsize) in clone_range()
5646 return send_extent_data(sctx, dst_path, offset, len); in clone_range()
5744 ret = send_extent_data(sctx, dst_path, offset, in clone_range()
5809 offset + clone_len < sctx->cur_inode_size) { in clone_range()
5815 ret = send_clone(sctx, offset, slen, in clone_range()
5820 ret = send_extent_data(sctx, dst_path, in clone_range()
5824 ret = send_clone(sctx, offset, clone_len, in clone_range()
5847 ret = send_extent_data(sctx, dst_path, offset, in clone_range()
5869 if (clone_root->root == sctx->send_root && in clone_range()
5870 clone_root->ino == sctx->cur_ino && in clone_range()
5871 clone_root->offset >= sctx->cur_inode_next_write_offset) in clone_range()
5880 ret = send_extent_data(sctx, dst_path, offset, len); in clone_range()
5888 static int send_write_or_clone(struct send_ctx *sctx, in send_write_or_clone() argument
5896 u64 bs = sctx->send_root->fs_info->sb->s_blocksize; in send_write_or_clone()
5898 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size); in send_write_or_clone()
5911 ret = clone_range(sctx, path, clone_root, disk_byte, in send_write_or_clone()
5914 ret = send_extent_data(sctx, path, offset, end - offset); in send_write_or_clone()
5916 sctx->cur_inode_next_write_offset = end; in send_write_or_clone()
5920 static int is_extent_unchanged(struct send_ctx *sctx, in is_extent_unchanged() argument
5985 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); in is_extent_unchanged()
6075 ret = btrfs_next_item(sctx->parent_root, path); in is_extent_unchanged()
6110 static int get_last_extent(struct send_ctx *sctx, u64 offset) in get_last_extent() argument
6113 struct btrfs_root *root = sctx->send_root; in get_last_extent()
6121 sctx->cur_inode_last_extent = 0; in get_last_extent()
6123 key.objectid = sctx->cur_ino; in get_last_extent()
6131 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY) in get_last_extent()
6134 sctx->cur_inode_last_extent = btrfs_file_extent_end(path); in get_last_extent()
6140 static int range_is_hole_in_parent(struct send_ctx *sctx, in range_is_hole_in_parent() argument
6146 struct btrfs_root *root = sctx->parent_root; in range_is_hole_in_parent()
6154 key.objectid = sctx->cur_ino; in range_is_hole_in_parent()
6179 if (key.objectid < sctx->cur_ino || in range_is_hole_in_parent()
6182 if (key.objectid > sctx->cur_ino || in range_is_hole_in_parent()
6206 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path, in maybe_send_hole() argument
6211 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx)) in maybe_send_hole()
6214 if (sctx->cur_inode_last_extent == (u64)-1) { in maybe_send_hole()
6215 ret = get_last_extent(sctx, key->offset - 1); in maybe_send_hole()
6221 sctx->cur_inode_last_extent < key->offset) { in maybe_send_hole()
6229 ret = get_last_extent(sctx, key->offset - 1); in maybe_send_hole()
6234 if (sctx->cur_inode_last_extent < key->offset) { in maybe_send_hole()
6235 ret = range_is_hole_in_parent(sctx, in maybe_send_hole()
6236 sctx->cur_inode_last_extent, in maybe_send_hole()
6241 ret = send_hole(sctx, key->offset); in maybe_send_hole()
6245 sctx->cur_inode_last_extent = btrfs_file_extent_end(path); in maybe_send_hole()
6249 static int process_extent(struct send_ctx *sctx, in process_extent() argument
6256 if (S_ISLNK(sctx->cur_inode_mode)) in process_extent()
6259 if (sctx->parent_root && !sctx->cur_inode_new) { in process_extent()
6260 ret = is_extent_unchanged(sctx, path, key); in process_extent()
6295 ret = find_extent_clone(sctx, path, key->objectid, key->offset, in process_extent()
6296 sctx->cur_inode_size, &found_clone); in process_extent()
6300 ret = send_write_or_clone(sctx, path, key, found_clone); in process_extent()
6304 ret = maybe_send_hole(sctx, path, key); in process_extent()
6309 static int process_all_extents(struct send_ctx *sctx) in process_all_extents() argument
6318 root = sctx->send_root; in process_all_extents()
6323 key.objectid = sctx->cmp_key->objectid; in process_all_extents()
6333 ret = process_extent(sctx, path, &found_key); in process_all_extents()
6345 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end, in process_recorded_refs_if_needed() argument
6351 if (sctx->cur_ino == 0) in process_recorded_refs_if_needed()
6353 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && in process_recorded_refs_if_needed()
6354 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY) in process_recorded_refs_if_needed()
6356 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) in process_recorded_refs_if_needed()
6359 ret = process_recorded_refs(sctx, pending_move); in process_recorded_refs_if_needed()
6368 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) in finish_inode_if_needed() argument
6387 if (sctx->ignore_cur_inode) in finish_inode_if_needed()
6390 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move, in finish_inode_if_needed()
6408 sctx->send_progress = sctx->cur_ino + 1; in finish_inode_if_needed()
6410 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) in finish_inode_if_needed()
6412 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) in finish_inode_if_needed()
6414 ret = get_inode_info(sctx->send_root, sctx->cur_ino, &info); in finish_inode_if_needed()
6422 if (!sctx->parent_root || sctx->cur_inode_new) { in finish_inode_if_needed()
6424 if (!S_ISLNK(sctx->cur_inode_mode)) in finish_inode_if_needed()
6426 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size) in finish_inode_if_needed()
6431 ret = get_inode_info(sctx->parent_root, sctx->cur_ino, &info); in finish_inode_if_needed()
6442 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode) in finish_inode_if_needed()
6444 if (!S_ISLNK(sctx->cur_inode_mode) && left_fileattr != right_fileattr) in finish_inode_if_needed()
6446 if ((old_size == sctx->cur_inode_size) || in finish_inode_if_needed()
6447 (sctx->cur_inode_size > old_size && in finish_inode_if_needed()
6448 sctx->cur_inode_next_write_offset == sctx->cur_inode_size)) in finish_inode_if_needed()
6452 if (S_ISREG(sctx->cur_inode_mode)) { in finish_inode_if_needed()
6453 if (need_send_hole(sctx)) { in finish_inode_if_needed()
6454 if (sctx->cur_inode_last_extent == (u64)-1 || in finish_inode_if_needed()
6455 sctx->cur_inode_last_extent < in finish_inode_if_needed()
6456 sctx->cur_inode_size) { in finish_inode_if_needed()
6457 ret = get_last_extent(sctx, (u64)-1); in finish_inode_if_needed()
6461 if (sctx->cur_inode_last_extent < in finish_inode_if_needed()
6462 sctx->cur_inode_size) { in finish_inode_if_needed()
6463 ret = send_hole(sctx, sctx->cur_inode_size); in finish_inode_if_needed()
6469 ret = send_truncate(sctx, sctx->cur_ino, in finish_inode_if_needed()
6470 sctx->cur_inode_gen, in finish_inode_if_needed()
6471 sctx->cur_inode_size); in finish_inode_if_needed()
6478 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, in finish_inode_if_needed()
6484 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, in finish_inode_if_needed()
6490 ret = send_fileattr(sctx, sctx->cur_ino, sctx->cur_inode_gen, in finish_inode_if_needed()
6496 if (proto_cmd_ok(sctx, BTRFS_SEND_C_ENABLE_VERITY) in finish_inode_if_needed()
6497 && sctx->cur_inode_needs_verity) { in finish_inode_if_needed()
6498 ret = process_verity(sctx); in finish_inode_if_needed()
6503 ret = send_capabilities(sctx); in finish_inode_if_needed()
6511 if (!is_waiting_for_move(sctx, sctx->cur_ino)) { in finish_inode_if_needed()
6512 ret = apply_children_dir_moves(sctx); in finish_inode_if_needed()
6522 sctx->send_progress = sctx->cur_ino + 1; in finish_inode_if_needed()
6523 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); in finish_inode_if_needed()
6532 static void close_current_inode(struct send_ctx *sctx) in close_current_inode() argument
6536 if (sctx->cur_inode == NULL) in close_current_inode()
6539 i_size = i_size_read(sctx->cur_inode); in close_current_inode()
6547 if (sctx->clean_page_cache && sctx->page_cache_clear_start < i_size) in close_current_inode()
6548 truncate_inode_pages_range(&sctx->cur_inode->i_data, in close_current_inode()
6549 sctx->page_cache_clear_start, in close_current_inode()
6552 iput(sctx->cur_inode); in close_current_inode()
6553 sctx->cur_inode = NULL; in close_current_inode()
6556 static int changed_inode(struct send_ctx *sctx, in changed_inode() argument
6560 struct btrfs_key *key = sctx->cmp_key; in changed_inode()
6566 close_current_inode(sctx); in changed_inode()
6568 sctx->cur_ino = key->objectid; in changed_inode()
6569 sctx->cur_inode_new_gen = false; in changed_inode()
6570 sctx->cur_inode_last_extent = (u64)-1; in changed_inode()
6571 sctx->cur_inode_next_write_offset = 0; in changed_inode()
6572 sctx->ignore_cur_inode = false; in changed_inode()
6579 sctx->send_progress = sctx->cur_ino; in changed_inode()
6583 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], in changed_inode()
6584 sctx->left_path->slots[0], in changed_inode()
6586 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], in changed_inode()
6589 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], in changed_inode()
6590 sctx->right_path->slots[0], in changed_inode()
6592 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], in changed_inode()
6596 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], in changed_inode()
6597 sctx->right_path->slots[0], in changed_inode()
6600 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], in changed_inode()
6609 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) in changed_inode()
6610 sctx->cur_inode_new_gen = true; in changed_inode()
6648 if (btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii) == 0) { in changed_inode()
6649 sctx->ignore_cur_inode = true; in changed_inode()
6652 sctx->cur_inode_gen = left_gen; in changed_inode()
6653 sctx->cur_inode_new = true; in changed_inode()
6654 sctx->cur_inode_deleted = false; in changed_inode()
6655 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6656 sctx->left_path->nodes[0], left_ii); in changed_inode()
6657 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6658 sctx->left_path->nodes[0], left_ii); in changed_inode()
6659 sctx->cur_inode_rdev = btrfs_inode_rdev( in changed_inode()
6660 sctx->left_path->nodes[0], left_ii); in changed_inode()
6661 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) in changed_inode()
6662 ret = send_create_inode_if_needed(sctx); in changed_inode()
6664 sctx->cur_inode_gen = right_gen; in changed_inode()
6665 sctx->cur_inode_new = false; in changed_inode()
6666 sctx->cur_inode_deleted = true; in changed_inode()
6667 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6668 sctx->right_path->nodes[0], right_ii); in changed_inode()
6669 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6670 sctx->right_path->nodes[0], right_ii); in changed_inode()
6674 new_nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii); in changed_inode()
6675 old_nlinks = btrfs_inode_nlink(sctx->right_path->nodes[0], right_ii); in changed_inode()
6677 sctx->ignore_cur_inode = true; in changed_inode()
6680 sctx->cur_inode_new_gen = 1; in changed_inode()
6689 if (sctx->cur_inode_new_gen) { in changed_inode()
6694 sctx->cur_inode_gen = right_gen; in changed_inode()
6695 sctx->cur_inode_new = false; in changed_inode()
6696 sctx->cur_inode_deleted = true; in changed_inode()
6697 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6698 sctx->right_path->nodes[0], right_ii); in changed_inode()
6699 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6700 sctx->right_path->nodes[0], right_ii); in changed_inode()
6701 ret = process_all_refs(sctx, in changed_inode()
6711 sctx->cur_inode_gen = left_gen; in changed_inode()
6712 sctx->cur_inode_new = true; in changed_inode()
6713 sctx->cur_inode_deleted = false; in changed_inode()
6714 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6715 sctx->left_path->nodes[0], in changed_inode()
6717 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6718 sctx->left_path->nodes[0], in changed_inode()
6720 sctx->cur_inode_rdev = btrfs_inode_rdev( in changed_inode()
6721 sctx->left_path->nodes[0], in changed_inode()
6723 ret = send_create_inode_if_needed(sctx); in changed_inode()
6727 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); in changed_inode()
6735 sctx->send_progress = sctx->cur_ino + 1; in changed_inode()
6741 ret = process_all_extents(sctx); in changed_inode()
6744 ret = process_all_new_xattrs(sctx); in changed_inode()
6749 sctx->cur_inode_gen = left_gen; in changed_inode()
6750 sctx->cur_inode_new = false; in changed_inode()
6751 sctx->cur_inode_new_gen = false; in changed_inode()
6752 sctx->cur_inode_deleted = false; in changed_inode()
6753 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6754 sctx->left_path->nodes[0], left_ii); in changed_inode()
6755 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6756 sctx->left_path->nodes[0], left_ii); in changed_inode()
6774 static int changed_ref(struct send_ctx *sctx, in changed_ref() argument
6779 if (sctx->cur_ino != sctx->cmp_key->objectid) { in changed_ref()
6780 inconsistent_snapshot_error(sctx, result, "reference"); in changed_ref()
6784 if (!sctx->cur_inode_new_gen && in changed_ref()
6785 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { in changed_ref()
6787 ret = record_new_ref(sctx); in changed_ref()
6789 ret = record_deleted_ref(sctx); in changed_ref()
6791 ret = record_changed_ref(sctx); in changed_ref()
6802 static int changed_xattr(struct send_ctx *sctx, in changed_xattr() argument
6807 if (sctx->cur_ino != sctx->cmp_key->objectid) { in changed_xattr()
6808 inconsistent_snapshot_error(sctx, result, "xattr"); in changed_xattr()
6812 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { in changed_xattr()
6814 ret = process_new_xattr(sctx); in changed_xattr()
6816 ret = process_deleted_xattr(sctx); in changed_xattr()
6818 ret = process_changed_xattr(sctx); in changed_xattr()
6829 static int changed_extent(struct send_ctx *sctx, in changed_extent() argument
6847 if (sctx->cur_ino != sctx->cmp_key->objectid) in changed_extent()
6850 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { in changed_extent()
6852 ret = process_extent(sctx, sctx->left_path, in changed_extent()
6853 sctx->cmp_key); in changed_extent()
6859 static int changed_verity(struct send_ctx *sctx, enum btrfs_compare_tree_result result) in changed_verity() argument
6863 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { in changed_verity()
6865 sctx->cur_inode_needs_verity = true; in changed_verity()
6870 static int dir_changed(struct send_ctx *sctx, u64 dir) in dir_changed() argument
6875 ret = get_inode_gen(sctx->send_root, dir, &new_gen); in dir_changed()
6879 ret = get_inode_gen(sctx->parent_root, dir, &orig_gen); in dir_changed()
6886 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path, in compare_refs() argument
6902 ret = dir_changed(sctx, dirid); in compare_refs()
6917 ret = dir_changed(sctx, dirid); in compare_refs()
6934 struct send_ctx *sctx) in changed_cb() argument
6963 lockdep_assert_not_held(&sctx->send_root->fs_info->commit_root_sem); in changed_cb()
6985 ret = compare_refs(sctx, left_path, key); in changed_cb()
6991 return maybe_send_hole(sctx, left_path, key); in changed_cb()
6999 sctx->left_path = left_path; in changed_cb()
7000 sctx->right_path = right_path; in changed_cb()
7001 sctx->cmp_key = key; in changed_cb()
7003 ret = finish_inode_if_needed(sctx, 0); in changed_cb()
7013 ret = changed_inode(sctx, result); in changed_cb()
7014 } else if (!sctx->ignore_cur_inode) { in changed_cb()
7017 ret = changed_ref(sctx, result); in changed_cb()
7019 ret = changed_xattr(sctx, result); in changed_cb()
7021 ret = changed_extent(sctx, result); in changed_cb()
7024 ret = changed_verity(sctx, result); in changed_cb()
7031 static int search_key_again(const struct send_ctx *sctx, in search_key_again() argument
7055 (root == sctx->parent_root ? "parent" : "send"), in search_key_again()
7064 static int full_send_tree(struct send_ctx *sctx) in full_send_tree() argument
7067 struct btrfs_root *send_root = sctx->send_root; in full_send_tree()
7082 sctx->last_reloc_trans = fs_info->last_reloc_trans; in full_send_tree()
7095 BTRFS_COMPARE_TREE_NEW, sctx); in full_send_tree()
7100 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) { in full_send_tree()
7101 sctx->last_reloc_trans = fs_info->last_reloc_trans; in full_send_tree()
7114 ret = search_key_again(sctx, send_root, path, &key); in full_send_tree()
7131 ret = finish_inode_if_needed(sctx, 1); in full_send_tree()
7305 const struct send_ctx *sctx) in restart_after_relocation() argument
7310 lockdep_assert_held_read(&sctx->send_root->fs_info->commit_root_sem); in restart_after_relocation()
7322 ret = search_key_again(sctx, sctx->send_root, left_path, left_key); in restart_after_relocation()
7327 ret = search_key_again(sctx, sctx->parent_root, right_path, right_key); in restart_after_relocation()
7354 root_level = btrfs_header_level(sctx->send_root->commit_root); in restart_after_relocation()
7361 root_level = btrfs_header_level(sctx->parent_root->commit_root); in restart_after_relocation()
7385 struct btrfs_root *right_root, struct send_ctx *sctx) in btrfs_compare_trees() argument
7514 sctx->last_reloc_trans = fs_info->last_reloc_trans; in btrfs_compare_trees()
7524 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) { in btrfs_compare_trees()
7528 sctx); in btrfs_compare_trees()
7531 sctx->last_reloc_trans = fs_info->last_reloc_trans; in btrfs_compare_trees()
7566 sctx); in btrfs_compare_trees()
7579 sctx); in btrfs_compare_trees()
7595 sctx); in btrfs_compare_trees()
7601 sctx); in btrfs_compare_trees()
7614 &left_key, result, sctx); in btrfs_compare_trees()
7670 static int send_subvol(struct send_ctx *sctx) in send_subvol() argument
7674 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) { in send_subvol()
7675 ret = send_header(sctx); in send_subvol()
7680 ret = send_subvol_begin(sctx); in send_subvol()
7684 if (sctx->parent_root) { in send_subvol()
7685 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx); in send_subvol()
7688 ret = finish_inode_if_needed(sctx, 1); in send_subvol()
7692 ret = full_send_tree(sctx); in send_subvol()
7698 free_recorded_refs(sctx); in send_subvol()
7715 static int ensure_commit_roots_uptodate(struct send_ctx *sctx) in ensure_commit_roots_uptodate() argument
7721 if (sctx->parent_root && in ensure_commit_roots_uptodate()
7722 sctx->parent_root->node != sctx->parent_root->commit_root) in ensure_commit_roots_uptodate()
7725 for (i = 0; i < sctx->clone_roots_cnt; i++) in ensure_commit_roots_uptodate()
7726 if (sctx->clone_roots[i].root->node != in ensure_commit_roots_uptodate()
7727 sctx->clone_roots[i].root->commit_root) in ensure_commit_roots_uptodate()
7738 trans = btrfs_join_transaction(sctx->send_root); in ensure_commit_roots_uptodate()
7755 static int flush_delalloc_roots(struct send_ctx *sctx) in flush_delalloc_roots() argument
7757 struct btrfs_root *root = sctx->parent_root; in flush_delalloc_roots()
7768 for (i = 0; i < sctx->clone_roots_cnt; i++) { in flush_delalloc_roots()
7769 root = sctx->clone_roots[i].root; in flush_delalloc_roots()
7807 struct send_ctx *sctx = NULL; in btrfs_ioctl_send() local
7855 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL); in btrfs_ioctl_send()
7856 if (!sctx) { in btrfs_ioctl_send()
7861 INIT_LIST_HEAD(&sctx->new_refs); in btrfs_ioctl_send()
7862 INIT_LIST_HEAD(&sctx->deleted_refs); in btrfs_ioctl_send()
7863 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL); in btrfs_ioctl_send()
7864 INIT_LIST_HEAD(&sctx->name_cache_list); in btrfs_ioctl_send()
7866 sctx->flags = arg->flags; in btrfs_ioctl_send()
7874 sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION; in btrfs_ioctl_send()
7876 sctx->proto = 1; in btrfs_ioctl_send()
7878 if ((arg->flags & BTRFS_SEND_FLAG_COMPRESSED) && sctx->proto < 2) { in btrfs_ioctl_send()
7883 sctx->send_filp = fget(arg->send_fd); in btrfs_ioctl_send()
7884 if (!sctx->send_filp) { in btrfs_ioctl_send()
7889 sctx->send_root = send_root; in btrfs_ioctl_send()
7894 if (btrfs_root_dead(sctx->send_root)) { in btrfs_ioctl_send()
7899 sctx->clone_roots_cnt = arg->clone_sources_count; in btrfs_ioctl_send()
7901 if (sctx->proto >= 2) { in btrfs_ioctl_send()
7904 sctx->send_max_size = ALIGN(SZ_16K + BTRFS_MAX_COMPRESSED, PAGE_SIZE); in btrfs_ioctl_send()
7905 sctx->send_buf = vmalloc(sctx->send_max_size); in btrfs_ioctl_send()
7906 if (!sctx->send_buf) { in btrfs_ioctl_send()
7910 send_buf_num_pages = sctx->send_max_size >> PAGE_SHIFT; in btrfs_ioctl_send()
7911 sctx->send_buf_pages = kcalloc(send_buf_num_pages, in btrfs_ioctl_send()
7912 sizeof(*sctx->send_buf_pages), in btrfs_ioctl_send()
7914 if (!sctx->send_buf_pages) { in btrfs_ioctl_send()
7919 sctx->send_buf_pages[i] = in btrfs_ioctl_send()
7920 vmalloc_to_page(sctx->send_buf + (i << PAGE_SHIFT)); in btrfs_ioctl_send()
7923 sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V1; in btrfs_ioctl_send()
7924 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL); in btrfs_ioctl_send()
7926 if (!sctx->send_buf) { in btrfs_ioctl_send()
7931 sctx->pending_dir_moves = RB_ROOT; in btrfs_ioctl_send()
7932 sctx->waiting_dir_moves = RB_ROOT; in btrfs_ioctl_send()
7933 sctx->orphan_dirs = RB_ROOT; in btrfs_ioctl_send()
7934 sctx->rbtree_new_refs = RB_ROOT; in btrfs_ioctl_send()
7935 sctx->rbtree_deleted_refs = RB_ROOT; in btrfs_ioctl_send()
7937 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots), in btrfs_ioctl_send()
7940 if (!sctx->clone_roots) { in btrfs_ioctl_send()
7987 sctx->clone_roots[i].root = clone_root; in btrfs_ioctl_send()
7995 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root, in btrfs_ioctl_send()
7997 if (IS_ERR(sctx->parent_root)) { in btrfs_ioctl_send()
7998 ret = PTR_ERR(sctx->parent_root); in btrfs_ioctl_send()
8002 spin_lock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
8003 sctx->parent_root->send_in_progress++; in btrfs_ioctl_send()
8004 if (!btrfs_root_readonly(sctx->parent_root) || in btrfs_ioctl_send()
8005 btrfs_root_dead(sctx->parent_root)) { in btrfs_ioctl_send()
8006 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
8010 if (sctx->parent_root->dedupe_in_progress) { in btrfs_ioctl_send()
8011 dedupe_in_progress_warn(sctx->parent_root); in btrfs_ioctl_send()
8012 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
8016 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
8024 sctx->clone_roots[sctx->clone_roots_cnt++].root = in btrfs_ioctl_send()
8025 btrfs_grab_root(sctx->send_root); in btrfs_ioctl_send()
8028 sort(sctx->clone_roots, sctx->clone_roots_cnt, in btrfs_ioctl_send()
8029 sizeof(*sctx->clone_roots), __clone_root_cmp_sort, in btrfs_ioctl_send()
8033 ret = flush_delalloc_roots(sctx); in btrfs_ioctl_send()
8037 ret = ensure_commit_roots_uptodate(sctx); in btrfs_ioctl_send()
8041 ret = send_subvol(sctx); in btrfs_ioctl_send()
8045 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) { in btrfs_ioctl_send()
8046 ret = begin_cmd(sctx, BTRFS_SEND_C_END); in btrfs_ioctl_send()
8049 ret = send_cmd(sctx); in btrfs_ioctl_send()
8055 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)); in btrfs_ioctl_send()
8056 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) { in btrfs_ioctl_send()
8060 n = rb_first(&sctx->pending_dir_moves); in btrfs_ioctl_send()
8067 free_pending_move(sctx, pm2); in btrfs_ioctl_send()
8069 free_pending_move(sctx, pm); in btrfs_ioctl_send()
8072 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)); in btrfs_ioctl_send()
8073 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) { in btrfs_ioctl_send()
8077 n = rb_first(&sctx->waiting_dir_moves); in btrfs_ioctl_send()
8079 rb_erase(&dm->node, &sctx->waiting_dir_moves); in btrfs_ioctl_send()
8083 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs)); in btrfs_ioctl_send()
8084 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) { in btrfs_ioctl_send()
8088 n = rb_first(&sctx->orphan_dirs); in btrfs_ioctl_send()
8090 free_orphan_dir_info(sctx, odi); in btrfs_ioctl_send()
8094 for (i = 0; i < sctx->clone_roots_cnt; i++) { in btrfs_ioctl_send()
8096 sctx->clone_roots[i].root); in btrfs_ioctl_send()
8097 btrfs_put_root(sctx->clone_roots[i].root); in btrfs_ioctl_send()
8100 for (i = 0; sctx && i < clone_sources_to_rollback; i++) { in btrfs_ioctl_send()
8102 sctx->clone_roots[i].root); in btrfs_ioctl_send()
8103 btrfs_put_root(sctx->clone_roots[i].root); in btrfs_ioctl_send()
8108 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) { in btrfs_ioctl_send()
8109 btrfs_root_dec_send_in_progress(sctx->parent_root); in btrfs_ioctl_send()
8110 btrfs_put_root(sctx->parent_root); in btrfs_ioctl_send()
8115 if (sctx) { in btrfs_ioctl_send()
8116 if (sctx->send_filp) in btrfs_ioctl_send()
8117 fput(sctx->send_filp); in btrfs_ioctl_send()
8119 kvfree(sctx->clone_roots); in btrfs_ioctl_send()
8120 kfree(sctx->send_buf_pages); in btrfs_ioctl_send()
8121 kvfree(sctx->send_buf); in btrfs_ioctl_send()
8122 kvfree(sctx->verity_descriptor); in btrfs_ioctl_send()
8124 name_cache_free(sctx); in btrfs_ioctl_send()
8126 close_current_inode(sctx); in btrfs_ioctl_send()
8128 kfree(sctx); in btrfs_ioctl_send()