Lines Matching refs:sctx

282 static void inconsistent_snapshot_error(struct send_ctx *sctx,  in inconsistent_snapshot_error()  argument
307 btrfs_err(sctx->send_root->fs_info, in inconsistent_snapshot_error()
309 result_string, what, sctx->cmp_key->objectid, in inconsistent_snapshot_error()
310 sctx->send_root->root_key.objectid, in inconsistent_snapshot_error()
311 (sctx->parent_root ? in inconsistent_snapshot_error()
312 sctx->parent_root->root_key.objectid : 0)); in inconsistent_snapshot_error()
315 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
318 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
320 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
322 static int need_send_hole(struct send_ctx *sctx) in need_send_hole() argument
324 return (sctx->parent_root && !sctx->cur_inode_new && in need_send_hole()
325 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && in need_send_hole()
326 S_ISREG(sctx->cur_inode_mode)); in need_send_hole()
571 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) in tlv_put() argument
575 int left = sctx->send_max_size - sctx->send_size; in tlv_put()
580 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); in tlv_put()
584 sctx->send_size += total_len; in tlv_put()
590 static int tlv_put_u##bits(struct send_ctx *sctx, \
594 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
599 static int tlv_put_string(struct send_ctx *sctx, u16 attr, in tlv_put_string() argument
604 return tlv_put(sctx, attr, str, len); in tlv_put_string()
607 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, in tlv_put_uuid() argument
610 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); in tlv_put_uuid()
613 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, in tlv_put_btrfs_timespec() argument
619 return tlv_put(sctx, attr, &bts, sizeof(bts)); in tlv_put_btrfs_timespec()
623 #define TLV_PUT(sctx, attrtype, data, attrlen) \ argument
625 ret = tlv_put(sctx, attrtype, data, attrlen); \
630 #define TLV_PUT_INT(sctx, attrtype, bits, value) \ argument
632 ret = tlv_put_u##bits(sctx, attrtype, value); \
637 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) argument
638 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) argument
639 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) argument
640 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) argument
641 #define TLV_PUT_STRING(sctx, attrtype, str, len) \ argument
643 ret = tlv_put_string(sctx, attrtype, str, len); \
647 #define TLV_PUT_PATH(sctx, attrtype, p) \ argument
649 ret = tlv_put_string(sctx, attrtype, p->start, \
654 #define TLV_PUT_UUID(sctx, attrtype, uuid) \ argument
656 ret = tlv_put_uuid(sctx, attrtype, uuid); \
660 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ argument
662 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
667 static int send_header(struct send_ctx *sctx) in send_header() argument
674 return write_buf(sctx->send_filp, &hdr, sizeof(hdr), in send_header()
675 &sctx->send_off); in send_header()
681 static int begin_cmd(struct send_ctx *sctx, int cmd) in begin_cmd() argument
685 if (WARN_ON(!sctx->send_buf)) in begin_cmd()
688 BUG_ON(sctx->send_size); in begin_cmd()
690 sctx->send_size += sizeof(*hdr); in begin_cmd()
691 hdr = (struct btrfs_cmd_header *)sctx->send_buf; in begin_cmd()
697 static int send_cmd(struct send_ctx *sctx) in send_cmd() argument
703 hdr = (struct btrfs_cmd_header *)sctx->send_buf; in send_cmd()
704 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len); in send_cmd()
707 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); in send_cmd()
710 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, in send_cmd()
711 &sctx->send_off); in send_cmd()
713 sctx->total_send_size += sctx->send_size; in send_cmd()
714 sctx->cmd_send_size[get_unaligned_le16(&hdr->cmd)] += sctx->send_size; in send_cmd()
715 sctx->send_size = 0; in send_cmd()
723 static int send_rename(struct send_ctx *sctx, in send_rename() argument
726 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_rename()
731 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); in send_rename()
735 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); in send_rename()
736 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); in send_rename()
738 ret = send_cmd(sctx); in send_rename()
748 static int send_link(struct send_ctx *sctx, in send_link() argument
751 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_link()
756 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); in send_link()
760 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_link()
761 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); in send_link()
763 ret = send_cmd(sctx); in send_link()
773 static int send_unlink(struct send_ctx *sctx, struct fs_path *path) in send_unlink() argument
775 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_unlink()
780 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); in send_unlink()
784 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_unlink()
786 ret = send_cmd(sctx); in send_unlink()
796 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) in send_rmdir() argument
798 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_rmdir()
803 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); in send_rmdir()
807 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_rmdir()
809 ret = send_cmd(sctx); in send_rmdir()
1179 struct send_ctx *sctx; member
1235 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, in __iterate_backrefs()
1236 bctx->sctx->clone_roots_cnt, in __iterate_backrefs()
1242 if (found->root == bctx->sctx->send_root && in __iterate_backrefs()
1252 if (found->root == bctx->sctx->send_root) { in __iterate_backrefs()
1268 bctx->sctx->cur_inode_next_write_offset) in __iterate_backrefs()
1297 static int find_extent_clone(struct send_ctx *sctx, in find_extent_clone() argument
1303 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in find_extent_clone()
1391 for (i = 0; i < sctx->clone_roots_cnt; i++) { in find_extent_clone()
1392 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1398 backref_ctx->sctx = sctx; in find_extent_clone()
1457 for (i = 0; i < sctx->clone_roots_cnt; i++) { in find_extent_clone()
1458 if (sctx->clone_roots[i].found_refs) { in find_extent_clone()
1460 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1461 else if (sctx->clone_roots[i].root == sctx->send_root) in find_extent_clone()
1463 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1541 static int gen_unique_name(struct send_ctx *sctx, in gen_unique_name() argument
1561 di = btrfs_lookup_dir_item(NULL, sctx->send_root, in gen_unique_name()
1575 if (!sctx->parent_root) { in gen_unique_name()
1581 di = btrfs_lookup_dir_item(NULL, sctx->parent_root, in gen_unique_name()
1613 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) in get_cur_inode_state() argument
1621 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL, in get_cur_inode_state()
1627 if (!sctx->parent_root) { in get_cur_inode_state()
1630 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, in get_cur_inode_state()
1641 if (ino < sctx->send_progress) in get_cur_inode_state()
1646 if (ino < sctx->send_progress) in get_cur_inode_state()
1655 if (ino < sctx->send_progress) in get_cur_inode_state()
1664 if (ino < sctx->send_progress) in get_cur_inode_state()
1679 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) in is_inode_existent() argument
1686 ret = get_cur_inode_state(sctx, ino, gen); in is_inode_existent()
1846 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, in will_overwrite_ref() argument
1855 if (!sctx->parent_root) in will_overwrite_ref()
1858 ret = is_inode_existent(sctx, dir, dir_gen); in will_overwrite_ref()
1867 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) { in will_overwrite_ref()
1868 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, in will_overwrite_ref()
1880 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, in will_overwrite_ref()
1894 if (other_inode > sctx->send_progress || in will_overwrite_ref()
1895 is_waiting_for_move(sctx, other_inode)) { in will_overwrite_ref()
1896 ret = get_inode_info(sctx->parent_root, other_inode, NULL, in will_overwrite_ref()
1918 static int did_overwrite_ref(struct send_ctx *sctx, in did_overwrite_ref() argument
1928 if (!sctx->parent_root) in did_overwrite_ref()
1931 ret = is_inode_existent(sctx, dir, dir_gen); in did_overwrite_ref()
1936 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, in did_overwrite_ref()
1949 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, in did_overwrite_ref()
1959 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL, in did_overwrite_ref()
1975 if ((ow_inode < sctx->send_progress) || in did_overwrite_ref()
1976 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino && in did_overwrite_ref()
1977 gen == sctx->cur_inode_gen)) in did_overwrite_ref()
1991 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) in did_overwrite_first_ref() argument
1998 if (!sctx->parent_root) in did_overwrite_first_ref()
2005 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); in did_overwrite_first_ref()
2009 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, in did_overwrite_first_ref()
2023 static int name_cache_insert(struct send_ctx *sctx, in name_cache_insert() argument
2029 nce_head = radix_tree_lookup(&sctx->name_cache, in name_cache_insert()
2039 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); in name_cache_insert()
2047 list_add_tail(&nce->list, &sctx->name_cache_list); in name_cache_insert()
2048 sctx->name_cache_size++; in name_cache_insert()
2053 static void name_cache_delete(struct send_ctx *sctx, in name_cache_delete() argument
2058 nce_head = radix_tree_lookup(&sctx->name_cache, in name_cache_delete()
2061 btrfs_err(sctx->send_root->fs_info, in name_cache_delete()
2063 nce->ino, sctx->name_cache_size); in name_cache_delete()
2068 sctx->name_cache_size--; in name_cache_delete()
2074 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); in name_cache_delete()
2079 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, in name_cache_search() argument
2085 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); in name_cache_search()
2100 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce) in name_cache_used() argument
2103 list_add_tail(&nce->list, &sctx->name_cache_list); in name_cache_used()
2109 static void name_cache_clean_unused(struct send_ctx *sctx) in name_cache_clean_unused() argument
2113 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) in name_cache_clean_unused()
2116 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { in name_cache_clean_unused()
2117 nce = list_entry(sctx->name_cache_list.next, in name_cache_clean_unused()
2119 name_cache_delete(sctx, nce); in name_cache_clean_unused()
2124 static void name_cache_free(struct send_ctx *sctx) in name_cache_free() argument
2128 while (!list_empty(&sctx->name_cache_list)) { in name_cache_free()
2129 nce = list_entry(sctx->name_cache_list.next, in name_cache_free()
2131 name_cache_delete(sctx, nce); in name_cache_free()
2144 static int __get_cur_name_and_parent(struct send_ctx *sctx, in __get_cur_name_and_parent() argument
2159 nce = name_cache_search(sctx, ino, gen); in __get_cur_name_and_parent()
2161 if (ino < sctx->send_progress && nce->need_later_update) { in __get_cur_name_and_parent()
2162 name_cache_delete(sctx, nce); in __get_cur_name_and_parent()
2166 name_cache_used(sctx, nce); in __get_cur_name_and_parent()
2182 ret = is_inode_existent(sctx, ino, gen); in __get_cur_name_and_parent()
2187 ret = gen_unique_name(sctx, ino, gen, dest); in __get_cur_name_and_parent()
2198 if (ino < sctx->send_progress) in __get_cur_name_and_parent()
2199 ret = get_first_ref(sctx->send_root, ino, in __get_cur_name_and_parent()
2202 ret = get_first_ref(sctx->parent_root, ino, in __get_cur_name_and_parent()
2211 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, in __get_cur_name_and_parent()
2217 ret = gen_unique_name(sctx, ino, gen, dest); in __get_cur_name_and_parent()
2241 if (ino < sctx->send_progress) in __get_cur_name_and_parent()
2246 nce_ret = name_cache_insert(sctx, nce); in __get_cur_name_and_parent()
2249 name_cache_clean_unused(sctx); in __get_cur_name_and_parent()
2280 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, in get_cur_path() argument
2303 if (is_waiting_for_rm(sctx, ino, gen)) { in get_cur_path()
2304 ret = gen_unique_name(sctx, ino, gen, name); in get_cur_path()
2311 wdm = get_waiting_dir_move(sctx, ino); in get_cur_path()
2313 ret = gen_unique_name(sctx, ino, gen, name); in get_cur_path()
2316 ret = get_first_ref(sctx->parent_root, ino, in get_cur_path()
2319 ret = __get_cur_name_and_parent(sctx, ino, gen, in get_cur_path()
2347 static int send_subvol_begin(struct send_ctx *sctx) in send_subvol_begin() argument
2350 struct btrfs_root *send_root = sctx->send_root; in send_subvol_begin()
2351 struct btrfs_root *parent_root = sctx->parent_root; in send_subvol_begin()
2395 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); in send_subvol_begin()
2399 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); in send_subvol_begin()
2404 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); in send_subvol_begin()
2406 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid)) in send_subvol_begin()
2407 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, in send_subvol_begin()
2408 sctx->send_root->root_item.received_uuid); in send_subvol_begin()
2410 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, in send_subvol_begin()
2411 sctx->send_root->root_item.uuid); in send_subvol_begin()
2413 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, in send_subvol_begin()
2414 le64_to_cpu(sctx->send_root->root_item.ctransid)); in send_subvol_begin()
2417 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_subvol_begin()
2420 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_subvol_begin()
2422 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, in send_subvol_begin()
2423 le64_to_cpu(sctx->parent_root->root_item.ctransid)); in send_subvol_begin()
2426 ret = send_cmd(sctx); in send_subvol_begin()
2435 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) in send_truncate() argument
2437 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_truncate()
2447 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); in send_truncate()
2451 ret = get_cur_path(sctx, ino, gen, p); in send_truncate()
2454 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_truncate()
2455 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); in send_truncate()
2457 ret = send_cmd(sctx); in send_truncate()
2465 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) in send_chmod() argument
2467 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_chmod()
2477 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); in send_chmod()
2481 ret = get_cur_path(sctx, ino, gen, p); in send_chmod()
2484 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_chmod()
2485 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); in send_chmod()
2487 ret = send_cmd(sctx); in send_chmod()
2495 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) in send_chown() argument
2497 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_chown()
2508 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); in send_chown()
2512 ret = get_cur_path(sctx, ino, gen, p); in send_chown()
2515 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_chown()
2516 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); in send_chown()
2517 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); in send_chown()
2519 ret = send_cmd(sctx); in send_chown()
2527 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) in send_utimes() argument
2529 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_utimes()
2553 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); in send_utimes()
2563 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); in send_utimes()
2567 ret = get_cur_path(sctx, ino, gen, p); in send_utimes()
2570 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_utimes()
2571 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime); in send_utimes()
2572 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime); in send_utimes()
2573 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime); in send_utimes()
2576 ret = send_cmd(sctx); in send_utimes()
2590 static int send_create_inode(struct send_ctx *sctx, u64 ino) in send_create_inode() argument
2592 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_create_inode()
2606 if (ino != sctx->cur_ino) { in send_create_inode()
2607 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, in send_create_inode()
2612 gen = sctx->cur_inode_gen; in send_create_inode()
2613 mode = sctx->cur_inode_mode; in send_create_inode()
2614 rdev = sctx->cur_inode_rdev; in send_create_inode()
2630 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o", in send_create_inode()
2636 ret = begin_cmd(sctx, cmd); in send_create_inode()
2640 ret = gen_unique_name(sctx, ino, gen, p); in send_create_inode()
2644 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_create_inode()
2645 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); in send_create_inode()
2649 ret = read_symlink(sctx->send_root, ino, p); in send_create_inode()
2652 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); in send_create_inode()
2655 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); in send_create_inode()
2656 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); in send_create_inode()
2659 ret = send_cmd(sctx); in send_create_inode()
2675 static int did_create_dir(struct send_ctx *sctx, u64 dir) in did_create_dir() argument
2695 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); in did_create_dir()
2703 ret = btrfs_next_leaf(sctx->send_root, path); in did_create_dir()
2724 di_key.objectid < sctx->send_progress) { in did_create_dir()
2743 static int send_create_inode_if_needed(struct send_ctx *sctx) in send_create_inode_if_needed() argument
2747 if (S_ISDIR(sctx->cur_inode_mode)) { in send_create_inode_if_needed()
2748 ret = did_create_dir(sctx, sctx->cur_ino); in send_create_inode_if_needed()
2757 ret = send_create_inode(sctx, sctx->cur_ino); in send_create_inode_if_needed()
2830 static void free_recorded_refs(struct send_ctx *sctx) in free_recorded_refs() argument
2832 __free_recorded_refs(&sctx->new_refs); in free_recorded_refs()
2833 __free_recorded_refs(&sctx->deleted_refs); in free_recorded_refs()
2841 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, in orphanize_inode() argument
2851 ret = gen_unique_name(sctx, ino, gen, orphan); in orphanize_inode()
2855 ret = send_rename(sctx, path, orphan); in orphanize_inode()
2862 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx, in add_orphan_dir_info() argument
2865 struct rb_node **p = &sctx->orphan_dirs.rb_node; in add_orphan_dir_info()
2892 rb_insert_color(&odi->node, &sctx->orphan_dirs); in add_orphan_dir_info()
2896 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx, in get_orphan_dir_info() argument
2899 struct rb_node *n = sctx->orphan_dirs.rb_node; in get_orphan_dir_info()
2918 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen) in is_waiting_for_rm() argument
2920 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen); in is_waiting_for_rm()
2925 static void free_orphan_dir_info(struct send_ctx *sctx, in free_orphan_dir_info() argument
2930 rb_erase(&odi->node, &sctx->orphan_dirs); in free_orphan_dir_info()
2939 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen, in can_rmdir() argument
2943 struct btrfs_root *root = sctx->parent_root; in can_rmdir()
2965 odi = get_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
2994 dm = get_waiting_dir_move(sctx, loc.objectid); in can_rmdir()
2996 odi = add_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
3010 odi = add_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
3023 free_orphan_dir_info(sctx, odi); in can_rmdir()
3032 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) in is_waiting_for_move() argument
3034 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino); in is_waiting_for_move()
3039 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized) in add_waiting_dir_move() argument
3041 struct rb_node **p = &sctx->waiting_dir_moves.rb_node; in add_waiting_dir_move()
3067 rb_insert_color(&dm->node, &sctx->waiting_dir_moves); in add_waiting_dir_move()
3072 get_waiting_dir_move(struct send_ctx *sctx, u64 ino) in get_waiting_dir_move() argument
3074 struct rb_node *n = sctx->waiting_dir_moves.rb_node; in get_waiting_dir_move()
3089 static void free_waiting_dir_move(struct send_ctx *sctx, in free_waiting_dir_move() argument
3094 rb_erase(&dm->node, &sctx->waiting_dir_moves); in free_waiting_dir_move()
3098 static int add_pending_dir_move(struct send_ctx *sctx, in add_pending_dir_move() argument
3106 struct rb_node **p = &sctx->pending_dir_moves.rb_node; in add_pending_dir_move()
3147 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan); in add_pending_dir_move()
3155 rb_insert_color(&pm->node, &sctx->pending_dir_moves); in add_pending_dir_move()
3166 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx, in get_pending_dir_moves() argument
3169 struct rb_node *n = sctx->pending_dir_moves.rb_node; in get_pending_dir_moves()
3184 static int path_loop(struct send_ctx *sctx, struct fs_path *name, in path_loop() argument
3196 if (is_waiting_for_rm(sctx, ino, gen)) in path_loop()
3198 if (is_waiting_for_move(sctx, ino)) { in path_loop()
3201 ret = get_first_ref(sctx->parent_root, ino, in path_loop()
3204 ret = __get_cur_name_and_parent(sctx, ino, gen, in path_loop()
3226 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm) in apply_dir_move() argument
3231 u64 orig_progress = sctx->send_progress; in apply_dir_move()
3248 dm = get_waiting_dir_move(sctx, pm->ino); in apply_dir_move()
3253 free_waiting_dir_move(sctx, dm); in apply_dir_move()
3256 ret = gen_unique_name(sctx, pm->ino, in apply_dir_move()
3259 ret = get_first_ref(sctx->parent_root, pm->ino, in apply_dir_move()
3263 ret = get_cur_path(sctx, parent_ino, parent_gen, in apply_dir_move()
3272 sctx->send_progress = sctx->cur_ino + 1; in apply_dir_move()
3273 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor); in apply_dir_move()
3279 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor, in apply_dir_move()
3285 dm = get_waiting_dir_move(sctx, pm->ino); in apply_dir_move()
3295 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path); in apply_dir_move()
3299 ret = send_rename(sctx, from_path, to_path); in apply_dir_move()
3307 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen); in apply_dir_move()
3314 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino); in apply_dir_move()
3325 ret = get_cur_path(sctx, rmdir_ino, gen, name); in apply_dir_move()
3328 ret = send_rmdir(sctx, name); in apply_dir_move()
3334 ret = send_utimes(sctx, pm->ino, pm->gen); in apply_dir_move()
3346 ret = get_inode_info(sctx->send_root, cur->dir, NULL, in apply_dir_move()
3355 ret = send_utimes(sctx, cur->dir, cur->dir_gen); in apply_dir_move()
3364 sctx->send_progress = orig_progress; in apply_dir_move()
3369 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m) in free_pending_move() argument
3374 rb_erase(&m->node, &sctx->pending_dir_moves); in free_pending_move()
3379 static void tail_append_pending_moves(struct send_ctx *sctx, in tail_append_pending_moves() argument
3392 rb_erase(&moves->node, &sctx->pending_dir_moves); in tail_append_pending_moves()
3397 static int apply_children_dir_moves(struct send_ctx *sctx) in apply_children_dir_moves() argument
3401 u64 parent_ino = sctx->cur_ino; in apply_children_dir_moves()
3404 pm = get_pending_dir_moves(sctx, parent_ino); in apply_children_dir_moves()
3409 tail_append_pending_moves(sctx, pm, &stack); in apply_children_dir_moves()
3414 ret = apply_dir_move(sctx, pm); in apply_children_dir_moves()
3415 free_pending_move(sctx, pm); in apply_children_dir_moves()
3418 pm = get_pending_dir_moves(sctx, parent_ino); in apply_children_dir_moves()
3420 tail_append_pending_moves(sctx, pm, &stack); in apply_children_dir_moves()
3427 free_pending_move(sctx, pm); in apply_children_dir_moves()
3468 static int wait_for_dest_dir_move(struct send_ctx *sctx, in wait_for_dest_dir_move() argument
3472 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info; in wait_for_dest_dir_move()
3482 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) in wait_for_dest_dir_move()
3493 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); in wait_for_dest_dir_move()
3521 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL, in wait_for_dest_dir_move()
3525 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL, in wait_for_dest_dir_move()
3539 wdm = get_waiting_dir_move(sctx, di_key.objectid); in wait_for_dest_dir_move()
3541 ret = add_pending_dir_move(sctx, in wait_for_dest_dir_move()
3542 sctx->cur_ino, in wait_for_dest_dir_move()
3543 sctx->cur_inode_gen, in wait_for_dest_dir_move()
3545 &sctx->new_refs, in wait_for_dest_dir_move()
3546 &sctx->deleted_refs, in wait_for_dest_dir_move()
3688 static int wait_for_parent_move(struct send_ctx *sctx, in wait_for_parent_move() argument
3717 if (is_waiting_for_move(sctx, ino)) { in wait_for_parent_move()
3728 ret = is_ancestor(sctx->parent_root, in wait_for_parent_move()
3729 sctx->cur_ino, sctx->cur_inode_gen, in wait_for_parent_move()
3738 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after, in wait_for_parent_move()
3742 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before, in wait_for_parent_move()
3753 if (ino > sctx->cur_ino && in wait_for_parent_move()
3758 ret = get_inode_info(sctx->parent_root, ino, NULL, in wait_for_parent_move()
3777 ret = add_pending_dir_move(sctx, in wait_for_parent_move()
3778 sctx->cur_ino, in wait_for_parent_move()
3779 sctx->cur_inode_gen, in wait_for_parent_move()
3781 &sctx->new_refs, in wait_for_parent_move()
3782 &sctx->deleted_refs, in wait_for_parent_move()
3791 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) in update_ref_path() argument
3804 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path); in update_ref_path()
3862 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) in refresh_ref_path() argument
3872 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path); in refresh_ref_path()
3890 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move) in process_recorded_refs() argument
3892 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in process_recorded_refs()
3908 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino); in process_recorded_refs()
3914 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); in process_recorded_refs()
3934 if (!sctx->cur_inode_new) { in process_recorded_refs()
3935 ret = did_overwrite_first_ref(sctx, sctx->cur_ino, in process_recorded_refs()
3936 sctx->cur_inode_gen); in process_recorded_refs()
3942 if (sctx->cur_inode_new || did_overwrite) { in process_recorded_refs()
3943 ret = gen_unique_name(sctx, sctx->cur_ino, in process_recorded_refs()
3944 sctx->cur_inode_gen, valid_path); in process_recorded_refs()
3949 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
3993 list_for_each_entry(cur, &sctx->new_refs, list) { in process_recorded_refs()
3994 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4006 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
4012 ret = is_first_ref(sctx->parent_root, in process_recorded_refs()
4022 ret = refresh_ref_path(sctx, cur); in process_recorded_refs()
4027 ret = orphanize_inode(sctx, ow_inode, ow_gen, in process_recorded_refs()
4040 if (is_waiting_for_move(sctx, ow_inode)) { in process_recorded_refs()
4041 wdm = get_waiting_dir_move(sctx, in process_recorded_refs()
4057 nce = name_cache_search(sctx, ow_inode, ow_gen); in process_recorded_refs()
4059 name_cache_delete(sctx, nce); in process_recorded_refs()
4070 ret = is_ancestor(sctx->parent_root, in process_recorded_refs()
4072 sctx->cur_ino, NULL); in process_recorded_refs()
4076 ret = get_cur_path(sctx, sctx->cur_ino, in process_recorded_refs()
4077 sctx->cur_inode_gen, in process_recorded_refs()
4090 ret = refresh_ref_path(sctx, cur); in process_recorded_refs()
4094 ret = send_unlink(sctx, cur->full_path); in process_recorded_refs()
4102 list_for_each_entry(cur, &sctx->new_refs, list) { in process_recorded_refs()
4110 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4119 list_for_each_entry(cur2, &sctx->new_refs, list) { in process_recorded_refs()
4133 ret = did_create_dir(sctx, cur->dir); in process_recorded_refs()
4137 ret = send_create_inode(sctx, cur->dir); in process_recorded_refs()
4143 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) { in process_recorded_refs()
4144 ret = wait_for_dest_dir_move(sctx, cur, is_orphan); in process_recorded_refs()
4153 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root && in process_recorded_refs()
4155 ret = wait_for_parent_move(sctx, cur, is_orphan); in process_recorded_refs()
4170 ret = send_rename(sctx, valid_path, cur->full_path); in process_recorded_refs()
4178 if (S_ISDIR(sctx->cur_inode_mode)) { in process_recorded_refs()
4184 ret = send_rename(sctx, valid_path, in process_recorded_refs()
4200 ret = update_ref_path(sctx, cur); in process_recorded_refs()
4204 ret = send_link(sctx, cur->full_path, in process_recorded_refs()
4215 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { in process_recorded_refs()
4222 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
4223 sctx->cur_ino); in process_recorded_refs()
4227 ret = send_rmdir(sctx, valid_path); in process_recorded_refs()
4231 ret = orphanize_inode(sctx, sctx->cur_ino, in process_recorded_refs()
4232 sctx->cur_inode_gen, valid_path); in process_recorded_refs()
4238 list_for_each_entry(cur, &sctx->deleted_refs, list) { in process_recorded_refs()
4243 } else if (S_ISDIR(sctx->cur_inode_mode) && in process_recorded_refs()
4244 !list_empty(&sctx->deleted_refs)) { in process_recorded_refs()
4248 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, in process_recorded_refs()
4253 } else if (!S_ISDIR(sctx->cur_inode_mode)) { in process_recorded_refs()
4259 list_for_each_entry(cur, &sctx->deleted_refs, list) { in process_recorded_refs()
4260 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
4261 sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
4274 ret = update_ref_path(sctx, cur); in process_recorded_refs()
4278 ret = send_unlink(sctx, cur->full_path); in process_recorded_refs()
4295 ret = send_unlink(sctx, valid_path); in process_recorded_refs()
4313 if (cur->dir > sctx->cur_ino) in process_recorded_refs()
4316 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4323 ret = send_utimes(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4328 ret = can_rmdir(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
4329 sctx->cur_ino); in process_recorded_refs()
4333 ret = get_cur_path(sctx, cur->dir, in process_recorded_refs()
4337 ret = send_rmdir(sctx, valid_path); in process_recorded_refs()
4349 free_recorded_refs(sctx); in process_recorded_refs()
4358 struct send_ctx *sctx = ctx; in record_ref() local
4371 ret = get_cur_path(sctx, dir, gen, p); in record_ref()
4390 struct send_ctx *sctx = ctx; in __record_new_ref() local
4391 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs); in __record_new_ref()
4399 struct send_ctx *sctx = ctx; in __record_deleted_ref() local
4400 return record_ref(sctx->parent_root, dir, name, ctx, in __record_deleted_ref()
4401 &sctx->deleted_refs); in __record_deleted_ref()
4404 static int record_new_ref(struct send_ctx *sctx) in record_new_ref() argument
4408 ret = iterate_inode_ref(sctx->send_root, sctx->left_path, in record_new_ref()
4409 sctx->cmp_key, 0, __record_new_ref, sctx); in record_new_ref()
4418 static int record_deleted_ref(struct send_ctx *sctx) in record_deleted_ref() argument
4422 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, in record_deleted_ref()
4423 sctx->cmp_key, 0, __record_deleted_ref, sctx); in record_deleted_ref()
4496 struct send_ctx *sctx = ctx; in __record_changed_new_ref() local
4498 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL, in __record_changed_new_ref()
4503 ret = find_iref(sctx->parent_root, sctx->right_path, in __record_changed_new_ref()
4504 sctx->cmp_key, dir, dir_gen, name); in __record_changed_new_ref()
4506 ret = __record_new_ref(num, dir, index, name, sctx); in __record_changed_new_ref()
4519 struct send_ctx *sctx = ctx; in __record_changed_deleted_ref() local
4521 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL, in __record_changed_deleted_ref()
4526 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key, in __record_changed_deleted_ref()
4529 ret = __record_deleted_ref(num, dir, index, name, sctx); in __record_changed_deleted_ref()
4536 static int record_changed_ref(struct send_ctx *sctx) in record_changed_ref() argument
4540 ret = iterate_inode_ref(sctx->send_root, sctx->left_path, in record_changed_ref()
4541 sctx->cmp_key, 0, __record_changed_new_ref, sctx); in record_changed_ref()
4544 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, in record_changed_ref()
4545 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx); in record_changed_ref()
4558 static int process_all_refs(struct send_ctx *sctx, in process_all_refs() argument
4576 root = sctx->send_root; in process_all_refs()
4579 root = sctx->parent_root; in process_all_refs()
4582 btrfs_err(sctx->send_root->fs_info, in process_all_refs()
4588 key.objectid = sctx->cmp_key->objectid; in process_all_refs()
4614 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx); in process_all_refs()
4627 ret = process_recorded_refs(sctx, &pending_move); in process_all_refs()
4633 static int send_set_xattr(struct send_ctx *sctx, in send_set_xattr() argument
4640 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); in send_set_xattr()
4644 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_set_xattr()
4645 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); in send_set_xattr()
4646 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); in send_set_xattr()
4648 ret = send_cmd(sctx); in send_set_xattr()
4655 static int send_remove_xattr(struct send_ctx *sctx, in send_remove_xattr() argument
4661 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); in send_remove_xattr()
4665 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_remove_xattr()
4666 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); in send_remove_xattr()
4668 ret = send_cmd(sctx); in send_remove_xattr()
4681 struct send_ctx *sctx = ctx; in __process_new_xattr() local
4709 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in __process_new_xattr()
4713 ret = send_set_xattr(sctx, p, name, name_len, data, data_len); in __process_new_xattr()
4726 struct send_ctx *sctx = ctx; in __process_deleted_xattr() local
4733 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in __process_deleted_xattr()
4737 ret = send_remove_xattr(sctx, p, name, name_len); in __process_deleted_xattr()
4744 static int process_new_xattr(struct send_ctx *sctx) in process_new_xattr() argument
4748 ret = iterate_dir_item(sctx->send_root, sctx->left_path, in process_new_xattr()
4749 __process_new_xattr, sctx); in process_new_xattr()
4754 static int process_deleted_xattr(struct send_ctx *sctx) in process_deleted_xattr() argument
4756 return iterate_dir_item(sctx->parent_root, sctx->right_path, in process_deleted_xattr()
4757 __process_deleted_xattr, sctx); in process_deleted_xattr()
4824 struct send_ctx *sctx = ctx; in __process_changed_new_xattr() local
4828 ret = find_xattr(sctx->parent_root, sctx->right_path, in __process_changed_new_xattr()
4829 sctx->cmp_key, name, name_len, &found_data, in __process_changed_new_xattr()
4854 struct send_ctx *sctx = ctx; in __process_changed_deleted_xattr() local
4856 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key, in __process_changed_deleted_xattr()
4867 static int process_changed_xattr(struct send_ctx *sctx) in process_changed_xattr() argument
4871 ret = iterate_dir_item(sctx->send_root, sctx->left_path, in process_changed_xattr()
4872 __process_changed_new_xattr, sctx); in process_changed_xattr()
4875 ret = iterate_dir_item(sctx->parent_root, sctx->right_path, in process_changed_xattr()
4876 __process_changed_deleted_xattr, sctx); in process_changed_xattr()
4882 static int process_all_new_xattrs(struct send_ctx *sctx) in process_all_new_xattrs() argument
4896 root = sctx->send_root; in process_all_new_xattrs()
4898 key.objectid = sctx->cmp_key->objectid; in process_all_new_xattrs()
4926 ret = iterate_dir_item(root, path, __process_new_xattr, sctx); in process_all_new_xattrs()
4938 static inline u64 max_send_read_size(const struct send_ctx *sctx) in max_send_read_size() argument
4940 return sctx->send_max_size - SZ_16K; in max_send_read_size()
4943 static int put_data_header(struct send_ctx *sctx, u32 len) in put_data_header() argument
4947 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len) in put_data_header()
4949 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size); in put_data_header()
4952 sctx->send_size += sizeof(*hdr); in put_data_header()
4956 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len) in put_file_data() argument
4958 struct btrfs_root *root = sctx->send_root; in put_file_data()
4968 ret = put_data_header(sctx, len); in put_file_data()
4972 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root); in put_file_data()
4979 memset(&sctx->ra, 0, sizeof(struct file_ra_state)); in put_file_data()
4980 file_ra_state_init(&sctx->ra, inode->i_mapping); in put_file_data()
4988 page_cache_sync_readahead(inode->i_mapping, &sctx->ra, in put_file_data()
5000 page_cache_async_readahead(inode->i_mapping, &sctx->ra, in put_file_data()
5011 page_offset(page), sctx->cur_ino, in put_file_data()
5012 sctx->send_root->root_key.objectid); in put_file_data()
5020 memcpy(sctx->send_buf + sctx->send_size, addr + pg_offset, in put_file_data()
5028 sctx->send_size += cur_len; in put_file_data()
5038 static int send_write(struct send_ctx *sctx, u64 offset, u32 len) in send_write() argument
5040 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_write()
5050 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); in send_write()
5054 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_write()
5058 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_write()
5059 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_write()
5060 ret = put_file_data(sctx, offset, len); in send_write()
5064 ret = send_cmd(sctx); in send_write()
5075 static int send_clone(struct send_ctx *sctx, in send_clone() argument
5083 btrfs_debug(sctx->send_root->fs_info, in send_clone()
5092 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); in send_clone()
5096 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_clone()
5100 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_clone()
5101 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); in send_clone()
5102 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_clone()
5104 if (clone_root->root == sctx->send_root) { in send_clone()
5105 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL, in send_clone()
5109 ret = get_cur_path(sctx, clone_root->ino, gen, p); in send_clone()
5126 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_clone()
5129 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_clone()
5131 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, in send_clone()
5133 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); in send_clone()
5134 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, in send_clone()
5137 ret = send_cmd(sctx); in send_clone()
5148 static int send_update_extent(struct send_ctx *sctx, in send_update_extent() argument
5158 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT); in send_update_extent()
5162 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_update_extent()
5166 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_update_extent()
5167 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_update_extent()
5168 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); in send_update_extent()
5170 ret = send_cmd(sctx); in send_update_extent()
5178 static int send_hole(struct send_ctx *sctx, u64 end) in send_hole() argument
5181 u64 read_size = max_send_read_size(sctx); in send_hole()
5182 u64 offset = sctx->cur_inode_last_extent; in send_hole()
5191 if (offset >= sctx->cur_inode_size) in send_hole()
5198 end = min_t(u64, end, sctx->cur_inode_size); in send_hole()
5200 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) in send_hole()
5201 return send_update_extent(sctx, offset, end - offset); in send_hole()
5206 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_hole()
5212 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); in send_hole()
5215 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_hole()
5216 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_hole()
5217 ret = put_data_header(sctx, len); in send_hole()
5220 memset(sctx->send_buf + sctx->send_size, 0, len); in send_hole()
5221 sctx->send_size += len; in send_hole()
5222 ret = send_cmd(sctx); in send_hole()
5227 sctx->cur_inode_next_write_offset = offset; in send_hole()
5233 static int send_extent_data(struct send_ctx *sctx, in send_extent_data() argument
5237 u64 read_size = max_send_read_size(sctx); in send_extent_data()
5240 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) in send_extent_data()
5241 return send_update_extent(sctx, offset, len); in send_extent_data()
5247 ret = send_write(sctx, offset + sent, size); in send_extent_data()
5262 static int send_capabilities(struct send_ctx *sctx) in send_capabilities() argument
5277 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino, in send_capabilities()
5297 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); in send_capabilities()
5304 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS, in send_capabilities()
5313 static int clone_range(struct send_ctx *sctx, in clone_range() argument
5341 len == sctx->send_root->fs_info->sectorsize) in clone_range()
5342 return send_extent_data(sctx, offset, len); in clone_range()
5440 ret = send_extent_data(sctx, offset, hole_len); in clone_range()
5504 offset + clone_len < sctx->cur_inode_size) { in clone_range()
5510 ret = send_clone(sctx, offset, slen, in clone_range()
5515 ret = send_extent_data(sctx, offset + slen, in clone_range()
5518 ret = send_clone(sctx, offset, clone_len, in clone_range()
5541 ret = send_extent_data(sctx, offset, clone_len); in clone_range()
5562 if (clone_root->root == sctx->send_root && in clone_range()
5563 clone_root->ino == sctx->cur_ino && in clone_range()
5564 clone_root->offset >= sctx->cur_inode_next_write_offset) in clone_range()
5573 ret = send_extent_data(sctx, offset, len); in clone_range()
5581 static int send_write_or_clone(struct send_ctx *sctx, in send_write_or_clone() argument
5589 u64 bs = sctx->send_root->fs_info->sb->s_blocksize; in send_write_or_clone()
5591 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size); in send_write_or_clone()
5604 ret = clone_range(sctx, clone_root, disk_byte, data_offset, in send_write_or_clone()
5607 ret = send_extent_data(sctx, offset, end - offset); in send_write_or_clone()
5609 sctx->cur_inode_next_write_offset = end; in send_write_or_clone()
5613 static int is_extent_unchanged(struct send_ctx *sctx, in is_extent_unchanged() argument
5678 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); in is_extent_unchanged()
5768 ret = btrfs_next_item(sctx->parent_root, path); in is_extent_unchanged()
5803 static int get_last_extent(struct send_ctx *sctx, u64 offset) in get_last_extent() argument
5806 struct btrfs_root *root = sctx->send_root; in get_last_extent()
5814 sctx->cur_inode_last_extent = 0; in get_last_extent()
5816 key.objectid = sctx->cur_ino; in get_last_extent()
5824 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY) in get_last_extent()
5827 sctx->cur_inode_last_extent = btrfs_file_extent_end(path); in get_last_extent()
5833 static int range_is_hole_in_parent(struct send_ctx *sctx, in range_is_hole_in_parent() argument
5839 struct btrfs_root *root = sctx->parent_root; in range_is_hole_in_parent()
5847 key.objectid = sctx->cur_ino; in range_is_hole_in_parent()
5872 if (key.objectid < sctx->cur_ino || in range_is_hole_in_parent()
5875 if (key.objectid > sctx->cur_ino || in range_is_hole_in_parent()
5899 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path, in maybe_send_hole() argument
5904 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx)) in maybe_send_hole()
5907 if (sctx->cur_inode_last_extent == (u64)-1) { in maybe_send_hole()
5908 ret = get_last_extent(sctx, key->offset - 1); in maybe_send_hole()
5914 sctx->cur_inode_last_extent < key->offset) { in maybe_send_hole()
5922 ret = get_last_extent(sctx, key->offset - 1); in maybe_send_hole()
5927 if (sctx->cur_inode_last_extent < key->offset) { in maybe_send_hole()
5928 ret = range_is_hole_in_parent(sctx, in maybe_send_hole()
5929 sctx->cur_inode_last_extent, in maybe_send_hole()
5934 ret = send_hole(sctx, key->offset); in maybe_send_hole()
5938 sctx->cur_inode_last_extent = btrfs_file_extent_end(path); in maybe_send_hole()
5942 static int process_extent(struct send_ctx *sctx, in process_extent() argument
5949 if (S_ISLNK(sctx->cur_inode_mode)) in process_extent()
5952 if (sctx->parent_root && !sctx->cur_inode_new) { in process_extent()
5953 ret = is_extent_unchanged(sctx, path, key); in process_extent()
5988 ret = find_extent_clone(sctx, path, key->objectid, key->offset, in process_extent()
5989 sctx->cur_inode_size, &found_clone); in process_extent()
5993 ret = send_write_or_clone(sctx, path, key, found_clone); in process_extent()
5997 ret = maybe_send_hole(sctx, path, key); in process_extent()
6002 static int process_all_extents(struct send_ctx *sctx) in process_all_extents() argument
6012 root = sctx->send_root; in process_all_extents()
6017 key.objectid = sctx->cmp_key->objectid; in process_all_extents()
6047 ret = process_extent(sctx, path, &found_key); in process_all_extents()
6059 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end, in process_recorded_refs_if_needed() argument
6065 if (sctx->cur_ino == 0) in process_recorded_refs_if_needed()
6067 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && in process_recorded_refs_if_needed()
6068 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY) in process_recorded_refs_if_needed()
6070 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) in process_recorded_refs_if_needed()
6073 ret = process_recorded_refs(sctx, pending_move); in process_recorded_refs_if_needed()
6082 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) in finish_inode_if_needed() argument
6097 if (sctx->ignore_cur_inode) in finish_inode_if_needed()
6100 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move, in finish_inode_if_needed()
6118 sctx->send_progress = sctx->cur_ino + 1; in finish_inode_if_needed()
6120 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) in finish_inode_if_needed()
6122 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) in finish_inode_if_needed()
6125 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL, in finish_inode_if_needed()
6130 if (!sctx->parent_root || sctx->cur_inode_new) { in finish_inode_if_needed()
6132 if (!S_ISLNK(sctx->cur_inode_mode)) in finish_inode_if_needed()
6134 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size) in finish_inode_if_needed()
6139 ret = get_inode_info(sctx->parent_root, sctx->cur_ino, in finish_inode_if_needed()
6147 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode) in finish_inode_if_needed()
6149 if ((old_size == sctx->cur_inode_size) || in finish_inode_if_needed()
6150 (sctx->cur_inode_size > old_size && in finish_inode_if_needed()
6151 sctx->cur_inode_next_write_offset == sctx->cur_inode_size)) in finish_inode_if_needed()
6155 if (S_ISREG(sctx->cur_inode_mode)) { in finish_inode_if_needed()
6156 if (need_send_hole(sctx)) { in finish_inode_if_needed()
6157 if (sctx->cur_inode_last_extent == (u64)-1 || in finish_inode_if_needed()
6158 sctx->cur_inode_last_extent < in finish_inode_if_needed()
6159 sctx->cur_inode_size) { in finish_inode_if_needed()
6160 ret = get_last_extent(sctx, (u64)-1); in finish_inode_if_needed()
6164 if (sctx->cur_inode_last_extent < in finish_inode_if_needed()
6165 sctx->cur_inode_size) { in finish_inode_if_needed()
6166 ret = send_hole(sctx, sctx->cur_inode_size); in finish_inode_if_needed()
6172 ret = send_truncate(sctx, sctx->cur_ino, in finish_inode_if_needed()
6173 sctx->cur_inode_gen, in finish_inode_if_needed()
6174 sctx->cur_inode_size); in finish_inode_if_needed()
6181 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, in finish_inode_if_needed()
6187 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, in finish_inode_if_needed()
6193 ret = send_capabilities(sctx); in finish_inode_if_needed()
6201 if (!is_waiting_for_move(sctx, sctx->cur_ino)) { in finish_inode_if_needed()
6202 ret = apply_children_dir_moves(sctx); in finish_inode_if_needed()
6212 sctx->send_progress = sctx->cur_ino + 1; in finish_inode_if_needed()
6213 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); in finish_inode_if_needed()
6224 struct send_ctx *sctx; member
6232 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx, in record_parent_ref()
6240 static int btrfs_unlink_all_paths(struct send_ctx *sctx) in btrfs_unlink_all_paths() argument
6252 key.objectid = sctx->cur_ino; in btrfs_unlink_all_paths()
6255 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); in btrfs_unlink_all_paths()
6260 ctx.sctx = sctx; in btrfs_unlink_all_paths()
6267 ret = btrfs_next_leaf(sctx->parent_root, path); in btrfs_unlink_all_paths()
6276 if (key.objectid != sctx->cur_ino) in btrfs_unlink_all_paths()
6282 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1, in btrfs_unlink_all_paths()
6294 ret = send_unlink(sctx, ref->full_path); in btrfs_unlink_all_paths()
6309 static int changed_inode(struct send_ctx *sctx, in changed_inode() argument
6313 struct btrfs_key *key = sctx->cmp_key; in changed_inode()
6319 sctx->cur_ino = key->objectid; in changed_inode()
6320 sctx->cur_inode_new_gen = 0; in changed_inode()
6321 sctx->cur_inode_last_extent = (u64)-1; in changed_inode()
6322 sctx->cur_inode_next_write_offset = 0; in changed_inode()
6323 sctx->ignore_cur_inode = false; in changed_inode()
6330 sctx->send_progress = sctx->cur_ino; in changed_inode()
6334 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], in changed_inode()
6335 sctx->left_path->slots[0], in changed_inode()
6337 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], in changed_inode()
6340 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], in changed_inode()
6341 sctx->right_path->slots[0], in changed_inode()
6343 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], in changed_inode()
6347 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], in changed_inode()
6348 sctx->right_path->slots[0], in changed_inode()
6351 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], in changed_inode()
6360 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) in changed_inode()
6361 sctx->cur_inode_new_gen = 1; in changed_inode()
6382 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii); in changed_inode()
6384 sctx->ignore_cur_inode = true; in changed_inode()
6386 ret = btrfs_unlink_all_paths(sctx); in changed_inode()
6392 sctx->cur_inode_gen = left_gen; in changed_inode()
6393 sctx->cur_inode_new = 1; in changed_inode()
6394 sctx->cur_inode_deleted = 0; in changed_inode()
6395 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6396 sctx->left_path->nodes[0], left_ii); in changed_inode()
6397 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6398 sctx->left_path->nodes[0], left_ii); in changed_inode()
6399 sctx->cur_inode_rdev = btrfs_inode_rdev( in changed_inode()
6400 sctx->left_path->nodes[0], left_ii); in changed_inode()
6401 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) in changed_inode()
6402 ret = send_create_inode_if_needed(sctx); in changed_inode()
6404 sctx->cur_inode_gen = right_gen; in changed_inode()
6405 sctx->cur_inode_new = 0; in changed_inode()
6406 sctx->cur_inode_deleted = 1; in changed_inode()
6407 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6408 sctx->right_path->nodes[0], right_ii); in changed_inode()
6409 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6410 sctx->right_path->nodes[0], right_ii); in changed_inode()
6419 if (sctx->cur_inode_new_gen) { in changed_inode()
6423 sctx->cur_inode_gen = right_gen; in changed_inode()
6424 sctx->cur_inode_new = 0; in changed_inode()
6425 sctx->cur_inode_deleted = 1; in changed_inode()
6426 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6427 sctx->right_path->nodes[0], right_ii); in changed_inode()
6428 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6429 sctx->right_path->nodes[0], right_ii); in changed_inode()
6430 ret = process_all_refs(sctx, in changed_inode()
6438 sctx->cur_inode_gen = left_gen; in changed_inode()
6439 sctx->cur_inode_new = 1; in changed_inode()
6440 sctx->cur_inode_deleted = 0; in changed_inode()
6441 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6442 sctx->left_path->nodes[0], left_ii); in changed_inode()
6443 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6444 sctx->left_path->nodes[0], left_ii); in changed_inode()
6445 sctx->cur_inode_rdev = btrfs_inode_rdev( in changed_inode()
6446 sctx->left_path->nodes[0], left_ii); in changed_inode()
6447 ret = send_create_inode_if_needed(sctx); in changed_inode()
6451 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); in changed_inode()
6458 sctx->send_progress = sctx->cur_ino + 1; in changed_inode()
6464 ret = process_all_extents(sctx); in changed_inode()
6467 ret = process_all_new_xattrs(sctx); in changed_inode()
6471 sctx->cur_inode_gen = left_gen; in changed_inode()
6472 sctx->cur_inode_new = 0; in changed_inode()
6473 sctx->cur_inode_new_gen = 0; in changed_inode()
6474 sctx->cur_inode_deleted = 0; in changed_inode()
6475 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6476 sctx->left_path->nodes[0], left_ii); in changed_inode()
6477 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6478 sctx->left_path->nodes[0], left_ii); in changed_inode()
6496 static int changed_ref(struct send_ctx *sctx, in changed_ref() argument
6501 if (sctx->cur_ino != sctx->cmp_key->objectid) { in changed_ref()
6502 inconsistent_snapshot_error(sctx, result, "reference"); in changed_ref()
6506 if (!sctx->cur_inode_new_gen && in changed_ref()
6507 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { in changed_ref()
6509 ret = record_new_ref(sctx); in changed_ref()
6511 ret = record_deleted_ref(sctx); in changed_ref()
6513 ret = record_changed_ref(sctx); in changed_ref()
6524 static int changed_xattr(struct send_ctx *sctx, in changed_xattr() argument
6529 if (sctx->cur_ino != sctx->cmp_key->objectid) { in changed_xattr()
6530 inconsistent_snapshot_error(sctx, result, "xattr"); in changed_xattr()
6534 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { in changed_xattr()
6536 ret = process_new_xattr(sctx); in changed_xattr()
6538 ret = process_deleted_xattr(sctx); in changed_xattr()
6540 ret = process_changed_xattr(sctx); in changed_xattr()
6551 static int changed_extent(struct send_ctx *sctx, in changed_extent() argument
6569 if (sctx->cur_ino != sctx->cmp_key->objectid) in changed_extent()
6572 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { in changed_extent()
6574 ret = process_extent(sctx, sctx->left_path, in changed_extent()
6575 sctx->cmp_key); in changed_extent()
6581 static int dir_changed(struct send_ctx *sctx, u64 dir) in dir_changed() argument
6586 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL, in dir_changed()
6591 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL, in dir_changed()
6599 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path, in compare_refs() argument
6615 ret = dir_changed(sctx, dirid); in compare_refs()
6630 ret = dir_changed(sctx, dirid); in compare_refs()
6650 struct send_ctx *sctx = ctx; in changed_cb() local
6655 ret = compare_refs(sctx, left_path, key); in changed_cb()
6661 return maybe_send_hole(sctx, left_path, key); in changed_cb()
6669 sctx->left_path = left_path; in changed_cb()
6670 sctx->right_path = right_path; in changed_cb()
6671 sctx->cmp_key = key; in changed_cb()
6673 ret = finish_inode_if_needed(sctx, 0); in changed_cb()
6683 ret = changed_inode(sctx, result); in changed_cb()
6684 } else if (!sctx->ignore_cur_inode) { in changed_cb()
6687 ret = changed_ref(sctx, result); in changed_cb()
6689 ret = changed_xattr(sctx, result); in changed_cb()
6691 ret = changed_extent(sctx, result); in changed_cb()
6698 static int full_send_tree(struct send_ctx *sctx) in full_send_tree() argument
6701 struct btrfs_root *send_root = sctx->send_root; in full_send_tree()
6727 BTRFS_COMPARE_TREE_NEW, sctx); in full_send_tree()
6741 ret = finish_inode_if_needed(sctx, 1); in full_send_tree()
7107 static int send_subvol(struct send_ctx *sctx) in send_subvol() argument
7111 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) { in send_subvol()
7112 ret = send_header(sctx); in send_subvol()
7117 ret = send_subvol_begin(sctx); in send_subvol()
7121 if (sctx->parent_root) { in send_subvol()
7122 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx); in send_subvol()
7125 ret = finish_inode_if_needed(sctx, 1); in send_subvol()
7129 ret = full_send_tree(sctx); in send_subvol()
7135 free_recorded_refs(sctx); in send_subvol()
7152 static int ensure_commit_roots_uptodate(struct send_ctx *sctx) in ensure_commit_roots_uptodate() argument
7158 if (sctx->parent_root && in ensure_commit_roots_uptodate()
7159 sctx->parent_root->node != sctx->parent_root->commit_root) in ensure_commit_roots_uptodate()
7162 for (i = 0; i < sctx->clone_roots_cnt; i++) in ensure_commit_roots_uptodate()
7163 if (sctx->clone_roots[i].root->node != in ensure_commit_roots_uptodate()
7164 sctx->clone_roots[i].root->commit_root) in ensure_commit_roots_uptodate()
7175 trans = btrfs_join_transaction(sctx->send_root); in ensure_commit_roots_uptodate()
7192 static int flush_delalloc_roots(struct send_ctx *sctx) in flush_delalloc_roots() argument
7194 struct btrfs_root *root = sctx->parent_root; in flush_delalloc_roots()
7205 for (i = 0; i < sctx->clone_roots_cnt; i++) { in flush_delalloc_roots()
7206 root = sctx->clone_roots[i].root; in flush_delalloc_roots()
7244 struct send_ctx *sctx = NULL; in btrfs_ioctl_send() local
7292 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL); in btrfs_ioctl_send()
7293 if (!sctx) { in btrfs_ioctl_send()
7298 INIT_LIST_HEAD(&sctx->new_refs); in btrfs_ioctl_send()
7299 INIT_LIST_HEAD(&sctx->deleted_refs); in btrfs_ioctl_send()
7300 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL); in btrfs_ioctl_send()
7301 INIT_LIST_HEAD(&sctx->name_cache_list); in btrfs_ioctl_send()
7303 sctx->flags = arg->flags; in btrfs_ioctl_send()
7305 sctx->send_filp = fget(arg->send_fd); in btrfs_ioctl_send()
7306 if (!sctx->send_filp) { in btrfs_ioctl_send()
7311 sctx->send_root = send_root; in btrfs_ioctl_send()
7316 if (btrfs_root_dead(sctx->send_root)) { in btrfs_ioctl_send()
7321 sctx->clone_roots_cnt = arg->clone_sources_count; in btrfs_ioctl_send()
7323 sctx->send_max_size = BTRFS_SEND_BUF_SIZE; in btrfs_ioctl_send()
7324 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL); in btrfs_ioctl_send()
7325 if (!sctx->send_buf) { in btrfs_ioctl_send()
7330 sctx->pending_dir_moves = RB_ROOT; in btrfs_ioctl_send()
7331 sctx->waiting_dir_moves = RB_ROOT; in btrfs_ioctl_send()
7332 sctx->orphan_dirs = RB_ROOT; in btrfs_ioctl_send()
7334 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots), in btrfs_ioctl_send()
7337 if (!sctx->clone_roots) { in btrfs_ioctl_send()
7384 sctx->clone_roots[i].root = clone_root; in btrfs_ioctl_send()
7392 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root, in btrfs_ioctl_send()
7394 if (IS_ERR(sctx->parent_root)) { in btrfs_ioctl_send()
7395 ret = PTR_ERR(sctx->parent_root); in btrfs_ioctl_send()
7399 spin_lock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
7400 sctx->parent_root->send_in_progress++; in btrfs_ioctl_send()
7401 if (!btrfs_root_readonly(sctx->parent_root) || in btrfs_ioctl_send()
7402 btrfs_root_dead(sctx->parent_root)) { in btrfs_ioctl_send()
7403 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
7407 if (sctx->parent_root->dedupe_in_progress) { in btrfs_ioctl_send()
7408 dedupe_in_progress_warn(sctx->parent_root); in btrfs_ioctl_send()
7409 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
7413 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
7421 sctx->clone_roots[sctx->clone_roots_cnt++].root = in btrfs_ioctl_send()
7422 btrfs_grab_root(sctx->send_root); in btrfs_ioctl_send()
7425 sort(sctx->clone_roots, sctx->clone_roots_cnt, in btrfs_ioctl_send()
7426 sizeof(*sctx->clone_roots), __clone_root_cmp_sort, in btrfs_ioctl_send()
7430 ret = flush_delalloc_roots(sctx); in btrfs_ioctl_send()
7434 ret = ensure_commit_roots_uptodate(sctx); in btrfs_ioctl_send()
7450 ret = send_subvol(sctx); in btrfs_ioctl_send()
7458 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) { in btrfs_ioctl_send()
7459 ret = begin_cmd(sctx, BTRFS_SEND_C_END); in btrfs_ioctl_send()
7462 ret = send_cmd(sctx); in btrfs_ioctl_send()
7468 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)); in btrfs_ioctl_send()
7469 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) { in btrfs_ioctl_send()
7473 n = rb_first(&sctx->pending_dir_moves); in btrfs_ioctl_send()
7480 free_pending_move(sctx, pm2); in btrfs_ioctl_send()
7482 free_pending_move(sctx, pm); in btrfs_ioctl_send()
7485 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)); in btrfs_ioctl_send()
7486 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) { in btrfs_ioctl_send()
7490 n = rb_first(&sctx->waiting_dir_moves); in btrfs_ioctl_send()
7492 rb_erase(&dm->node, &sctx->waiting_dir_moves); in btrfs_ioctl_send()
7496 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs)); in btrfs_ioctl_send()
7497 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) { in btrfs_ioctl_send()
7501 n = rb_first(&sctx->orphan_dirs); in btrfs_ioctl_send()
7503 free_orphan_dir_info(sctx, odi); in btrfs_ioctl_send()
7507 for (i = 0; i < sctx->clone_roots_cnt; i++) { in btrfs_ioctl_send()
7509 sctx->clone_roots[i].root); in btrfs_ioctl_send()
7510 btrfs_put_root(sctx->clone_roots[i].root); in btrfs_ioctl_send()
7513 for (i = 0; sctx && i < clone_sources_to_rollback; i++) { in btrfs_ioctl_send()
7515 sctx->clone_roots[i].root); in btrfs_ioctl_send()
7516 btrfs_put_root(sctx->clone_roots[i].root); in btrfs_ioctl_send()
7521 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) { in btrfs_ioctl_send()
7522 btrfs_root_dec_send_in_progress(sctx->parent_root); in btrfs_ioctl_send()
7523 btrfs_put_root(sctx->parent_root); in btrfs_ioctl_send()
7528 if (sctx) { in btrfs_ioctl_send()
7529 if (sctx->send_filp) in btrfs_ioctl_send()
7530 fput(sctx->send_filp); in btrfs_ioctl_send()
7532 kvfree(sctx->clone_roots); in btrfs_ioctl_send()
7533 kvfree(sctx->send_buf); in btrfs_ioctl_send()
7535 name_cache_free(sctx); in btrfs_ioctl_send()
7537 kfree(sctx); in btrfs_ioctl_send()