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