Lines Matching refs:sctx
368 static void inconsistent_snapshot_error(struct send_ctx *sctx,
393 btrfs_err(sctx->send_root->fs_info,
395 result_string, what, sctx->cmp_key->objectid,
396 sctx->send_root->root_key.objectid,
397 (sctx->parent_root ?
398 sctx->parent_root->root_key.objectid : 0));
402 static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd)
404 switch (sctx->proto) {
412 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
415 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
417 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
419 static int need_send_hole(struct send_ctx *sctx)
421 return (sctx->parent_root && !sctx->cur_inode_new &&
422 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
423 S_ISREG(sctx->cur_inode_mode));
659 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
663 int left = sctx->send_max_size - sctx->send_size;
665 if (WARN_ON_ONCE(sctx->put_data))
671 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
675 sctx->send_size += total_len;
681 static int tlv_put_u##bits(struct send_ctx *sctx, \
685 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
692 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
697 return tlv_put(sctx, attr, str, len);
700 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
703 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
706 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
712 return tlv_put(sctx, attr, &bts, sizeof(bts));
716 #define TLV_PUT(sctx, attrtype, data, attrlen) \
718 ret = tlv_put(sctx, attrtype, data, attrlen); \
723 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
725 ret = tlv_put_u##bits(sctx, attrtype, value); \
730 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
731 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
732 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
733 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
734 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
736 ret = tlv_put_string(sctx, attrtype, str, len); \
740 #define TLV_PUT_PATH(sctx, attrtype, p) \
742 ret = tlv_put_string(sctx, attrtype, p->start, \
747 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
749 ret = tlv_put_uuid(sctx, attrtype, uuid); \
753 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
755 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
760 static int send_header(struct send_ctx *sctx)
765 hdr.version = cpu_to_le32(sctx->proto);
766 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
767 &sctx->send_off);
773 static int begin_cmd(struct send_ctx *sctx, int cmd)
777 if (WARN_ON(!sctx->send_buf))
780 BUG_ON(sctx->send_size);
782 sctx->send_size += sizeof(*hdr);
783 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
789 static int send_cmd(struct send_ctx *sctx)
795 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
796 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
799 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
802 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
803 &sctx->send_off);
805 sctx->send_size = 0;
806 sctx->put_data = false;
814 static int send_rename(struct send_ctx *sctx,
817 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
822 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
826 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
827 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
829 ret = send_cmd(sctx);
839 static int send_link(struct send_ctx *sctx,
842 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
847 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
851 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
852 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
854 ret = send_cmd(sctx);
864 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
866 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
871 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
875 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
877 ret = send_cmd(sctx);
887 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
889 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
894 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
898 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
900 ret = send_cmd(sctx);
1279 struct send_ctx *sctx;
1328 * Results are collected in sctx->clone_roots->ino/offset.
1337 clone_root = bsearch((void *)(uintptr_t)root_id, bctx->sctx->clone_roots,
1338 bctx->sctx->clone_roots_cnt,
1345 if (clone_root->root == bctx->sctx->send_root &&
1354 if (clone_root->root == bctx->sctx->send_root) {
1370 bctx->sctx->cur_inode_next_write_offset)
1403 struct send_ctx *sctx = bctx->sctx;
1404 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1409 if (btrfs_lru_cache_size(&sctx->backref_cache) == 0)
1423 if (fs_info->last_reloc_trans > sctx->backref_cache_last_reloc_trans) {
1424 btrfs_lru_cache_clear(&sctx->backref_cache);
1428 raw_entry = btrfs_lru_cache_lookup(&sctx->backref_cache, key, 0);
1443 struct send_ctx *sctx = bctx->sctx;
1444 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1468 root = bsearch((void *)(uintptr_t)root_id, sctx->clone_roots,
1469 sctx->clone_roots_cnt, sizeof(struct clone_root),
1493 ret = btrfs_lru_cache_store(&sctx->backref_cache, &new_entry->entry,
1507 if (btrfs_lru_cache_size(&sctx->backref_cache) == 1)
1508 sctx->backref_cache_last_reloc_trans = fs_info->last_reloc_trans;
1516 const struct send_ctx *sctx = bctx->sctx;
1531 if (refs == 1 && sctx->clone_roots_cnt == 1)
1569 static int find_extent_clone(struct send_ctx *sctx,
1575 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1614 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1615 cur_clone_root = sctx->clone_roots + i;
1622 backref_ctx.sctx = sctx;
1664 if (sctx->clone_roots_cnt == 1)
1673 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
1701 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1702 struct clone_root *clone_root = &sctx->clone_roots[i];
1807 static int gen_unique_name(struct send_ctx *sctx,
1831 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1845 if (!sctx->parent_root) {
1851 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1883 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen,
1893 ret = get_inode_info(sctx->send_root, ino, &info);
1901 if (!sctx->parent_root) {
1904 ret = get_inode_info(sctx->parent_root, ino, &info);
1917 if (ino < sctx->send_progress)
1922 if (ino < sctx->send_progress)
1931 if (ino < sctx->send_progress)
1940 if (ino < sctx->send_progress)
1955 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen,
1963 ret = get_cur_inode_state(sctx, ino, gen, send_gen, parent_gen);
2120 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2129 if (!sctx->parent_root)
2132 ret = is_inode_existent(sctx, dir, dir_gen, NULL, &parent_root_dir_gen);
2144 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID &&
2148 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
2160 if (other_inode > sctx->send_progress ||
2161 is_waiting_for_move(sctx, other_inode)) {
2162 ret = get_inode_info(sctx->parent_root, other_inode, &info);
2182 static int did_overwrite_ref(struct send_ctx *sctx,
2192 if (!sctx->parent_root)
2195 ret = is_inode_existent(sctx, dir, dir_gen, &send_root_dir_gen, NULL);
2207 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
2217 ret = get_inode_gen(sctx->send_root, ow_inode, &ow_gen);
2232 if (ow_inode < sctx->send_progress)
2235 if (ino != sctx->cur_ino && ow_inode == sctx->cur_ino) {
2237 ret = get_inode_gen(sctx->send_root, ow_inode, &ow_gen);
2241 if (ow_gen == sctx->cur_inode_gen)
2253 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
2260 if (!sctx->parent_root)
2267 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
2271 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2279 static inline struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2284 entry = btrfs_lru_cache_lookup(&sctx->name_cache, ino, gen);
2299 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2314 nce = name_cache_search(sctx, ino, gen);
2316 if (ino < sctx->send_progress && nce->need_later_update) {
2317 btrfs_lru_cache_remove(&sctx->name_cache, &nce->entry);
2335 ret = is_inode_existent(sctx, ino, gen, NULL, NULL);
2340 ret = gen_unique_name(sctx, ino, gen, dest);
2351 if (ino < sctx->send_progress)
2352 ret = get_first_ref(sctx->send_root, ino,
2355 ret = get_first_ref(sctx->parent_root, ino,
2364 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2370 ret = gen_unique_name(sctx, ino, gen, dest);
2394 if (ino < sctx->send_progress)
2399 nce_ret = btrfs_lru_cache_store(&sctx->name_cache, &nce->entry, GFP_KERNEL);
2431 * sctx->send_progress tells this function at which point in time receiving
2434 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2457 if (is_waiting_for_rm(sctx, ino, gen)) {
2458 ret = gen_unique_name(sctx, ino, gen, name);
2465 wdm = get_waiting_dir_move(sctx, ino);
2467 ret = gen_unique_name(sctx, ino, gen, name);
2470 ret = get_first_ref(sctx->parent_root, ino,
2473 ret = __get_cur_name_and_parent(sctx, ino, gen,
2501 static int send_subvol_begin(struct send_ctx *sctx)
2504 struct btrfs_root *send_root = sctx->send_root;
2505 struct btrfs_root *parent_root = sctx->parent_root;
2549 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2553 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2558 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2560 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2561 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2562 sctx->send_root->root_item.received_uuid);
2564 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2565 sctx->send_root->root_item.uuid);
2567 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2568 btrfs_root_ctransid(&sctx->send_root->root_item));
2571 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2574 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2576 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2577 btrfs_root_ctransid(&sctx->parent_root->root_item));
2580 ret = send_cmd(sctx);
2589 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2591 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2601 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2605 ret = get_cur_path(sctx, ino, gen, p);
2608 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2609 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2611 ret = send_cmd(sctx);
2619 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2621 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2631 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2635 ret = get_cur_path(sctx, ino, gen, p);
2638 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2639 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2641 ret = send_cmd(sctx);
2649 static int send_fileattr(struct send_ctx *sctx, u64 ino, u64 gen, u64 fileattr)
2651 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2655 if (sctx->proto < 2)
2664 ret = begin_cmd(sctx, BTRFS_SEND_C_FILEATTR);
2668 ret = get_cur_path(sctx, ino, gen, p);
2671 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2672 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILEATTR, fileattr);
2674 ret = send_cmd(sctx);
2682 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2684 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2695 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2699 ret = get_cur_path(sctx, ino, gen, p);
2702 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2703 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2704 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2706 ret = send_cmd(sctx);
2714 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2716 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2740 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2750 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2754 ret = get_cur_path(sctx, ino, gen, p);
2757 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2758 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2759 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2760 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2761 if (sctx->proto >= 2)
2762 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_OTIME, eb, &ii->otime);
2764 ret = send_cmd(sctx);
2783 static int cache_dir_utimes(struct send_ctx *sctx, u64 dir, u64 gen)
2788 entry = btrfs_lru_cache_lookup(&sctx->dir_utimes_cache, dir, gen);
2795 return send_utimes(sctx, dir, gen);
2800 ret = btrfs_lru_cache_store(&sctx->dir_utimes_cache, entry, GFP_KERNEL);
2804 return send_utimes(sctx, dir, gen);
2810 static int trim_dir_utimes_cache(struct send_ctx *sctx)
2812 while (btrfs_lru_cache_size(&sctx->dir_utimes_cache) >
2817 lru = btrfs_lru_cache_lru_entry(&sctx->dir_utimes_cache);
2820 ret = send_utimes(sctx, lru->key, lru->gen);
2824 btrfs_lru_cache_remove(&sctx->dir_utimes_cache, lru);
2835 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2837 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2852 if (ino != sctx->cur_ino) {
2853 ret = get_inode_info(sctx->send_root, ino, &info);
2860 gen = sctx->cur_inode_gen;
2861 mode = sctx->cur_inode_mode;
2862 rdev = sctx->cur_inode_rdev;
2878 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2884 ret = begin_cmd(sctx, cmd);
2888 ret = gen_unique_name(sctx, ino, gen, p);
2892 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2893 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2897 ret = read_symlink(sctx->send_root, ino, p);
2900 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2903 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2904 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2907 ret = send_cmd(sctx);
2918 static void cache_dir_created(struct send_ctx *sctx, u64 dir)
2930 ret = btrfs_lru_cache_store(&sctx->dir_created_cache, entry, GFP_KERNEL);
2940 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2950 if (btrfs_lru_cache_lookup(&sctx->dir_created_cache, dir, 0))
2961 btrfs_for_each_slot(sctx->send_root, &key, &found_key, path, iter_ret) {
2974 di_key.objectid < sctx->send_progress) {
2976 cache_dir_created(sctx, dir);
2994 static int send_create_inode_if_needed(struct send_ctx *sctx)
2998 if (S_ISDIR(sctx->cur_inode_mode)) {
2999 ret = did_create_dir(sctx, sctx->cur_ino);
3006 ret = send_create_inode(sctx, sctx->cur_ino);
3008 if (ret == 0 && S_ISDIR(sctx->cur_inode_mode))
3009 cache_dir_created(sctx, sctx->cur_ino);
3079 static void free_recorded_refs(struct send_ctx *sctx)
3081 __free_recorded_refs(&sctx->new_refs);
3082 __free_recorded_refs(&sctx->deleted_refs);
3090 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
3100 ret = gen_unique_name(sctx, ino, gen, orphan);
3104 ret = send_rename(sctx, path, orphan);
3111 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
3114 struct rb_node **p = &sctx->orphan_dirs.rb_node;
3142 rb_insert_color(&odi->node, &sctx->orphan_dirs);
3146 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
3149 struct rb_node *n = sctx->orphan_dirs.rb_node;
3168 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
3170 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
3175 static void free_orphan_dir_info(struct send_ctx *sctx,
3180 rb_erase(&odi->node, &sctx->orphan_dirs);
3189 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen)
3193 struct btrfs_root *root = sctx->parent_root;
3209 odi = get_orphan_dir_info(sctx, dir, dir_gen);
3210 if (odi && sctx->cur_ino < odi->dir_high_seq_ino)
3254 if (sctx->cur_ino < dir_high_seq_ino) {
3280 dm = get_waiting_dir_move(sctx, loc.objectid);
3288 if (loc.objectid > sctx->cur_ino) {
3297 free_orphan_dir_info(sctx, odi);
3308 odi = add_orphan_dir_info(sctx, dir, dir_gen);
3321 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3323 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3328 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3330 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3356 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3361 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3363 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3378 static void free_waiting_dir_move(struct send_ctx *sctx,
3383 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3387 static int add_pending_dir_move(struct send_ctx *sctx,
3395 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3436 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3444 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3455 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3458 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3473 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3485 if (is_waiting_for_rm(sctx, ino, gen))
3487 if (is_waiting_for_move(sctx, ino)) {
3490 ret = get_first_ref(sctx->parent_root, ino,
3493 ret = __get_cur_name_and_parent(sctx, ino, gen,
3515 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3520 u64 orig_progress = sctx->send_progress;
3537 dm = get_waiting_dir_move(sctx, pm->ino);
3542 free_waiting_dir_move(sctx, dm);
3545 ret = gen_unique_name(sctx, pm->ino,
3548 ret = get_first_ref(sctx->parent_root, pm->ino,
3552 ret = get_cur_path(sctx, parent_ino, parent_gen,
3561 sctx->send_progress = sctx->cur_ino + 1;
3562 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3568 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3574 dm = get_waiting_dir_move(sctx, pm->ino);
3584 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3588 ret = send_rename(sctx, from_path, to_path);
3596 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
3603 ret = can_rmdir(sctx, rmdir_ino, gen);
3614 ret = get_cur_path(sctx, rmdir_ino, gen, name);
3617 ret = send_rmdir(sctx, name);
3623 ret = cache_dir_utimes(sctx, pm->ino, pm->gen);
3635 ret = get_inode_info(sctx->send_root, cur->dir, NULL);
3643 ret = cache_dir_utimes(sctx, cur->dir, cur->dir_gen);
3652 sctx->send_progress = orig_progress;
3657 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3662 rb_erase(&m->node, &sctx->pending_dir_moves);
3667 static void tail_append_pending_moves(struct send_ctx *sctx,
3680 rb_erase(&moves->node, &sctx->pending_dir_moves);
3685 static int apply_children_dir_moves(struct send_ctx *sctx)
3689 u64 parent_ino = sctx->cur_ino;
3692 pm = get_pending_dir_moves(sctx, parent_ino);
3696 tail_append_pending_moves(sctx, pm, &stack);
3701 ret = apply_dir_move(sctx, pm);
3702 free_pending_move(sctx, pm);
3705 pm = get_pending_dir_moves(sctx, parent_ino);
3707 tail_append_pending_moves(sctx, pm, &stack);
3714 free_pending_move(sctx, pm);
3721 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3752 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3755 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3759 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3769 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3780 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3796 * parent directory with the same name that sctx->cur_ino is being
3799 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3808 ret = get_inode_gen(sctx->parent_root, di_key.objectid, &left_gen);
3811 ret = get_inode_gen(sctx->send_root, di_key.objectid, &right_gen);
3818 /* Different inode, no need to delay the rename of sctx->cur_ino */
3824 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3826 ret = add_pending_dir_move(sctx,
3827 sctx->cur_ino,
3828 sctx->cur_inode_gen,
3830 &sctx->new_refs,
3831 &sctx->deleted_refs,
3961 static int wait_for_parent_move(struct send_ctx *sctx,
3990 if (is_waiting_for_move(sctx, ino)) {
4001 ret = is_ancestor(sctx->parent_root,
4002 sctx->cur_ino, sctx->cur_inode_gen,
4011 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
4015 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
4026 if (ino > sctx->cur_ino &&
4031 ret = get_inode_gen(sctx->parent_root, ino, &parent_ino_gen);
4048 ret = add_pending_dir_move(sctx,
4049 sctx->cur_ino,
4050 sctx->cur_inode_gen,
4052 &sctx->new_refs,
4053 &sctx->deleted_refs,
4062 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
4075 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
4133 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
4143 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
4161 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
4163 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
4179 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
4185 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
4204 if (!sctx->cur_inode_new) {
4205 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
4206 sctx->cur_inode_gen);
4212 if (sctx->cur_inode_new || did_overwrite) {
4213 ret = gen_unique_name(sctx, sctx->cur_ino,
4214 sctx->cur_inode_gen, valid_path);
4219 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4263 list_for_each_entry(cur, &sctx->new_refs, list) {
4264 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
4276 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4282 ret = is_first_ref(sctx->parent_root,
4292 ret = refresh_ref_path(sctx, cur);
4297 ret = orphanize_inode(sctx, ow_inode, ow_gen,
4310 wdm = get_waiting_dir_move(sctx, ow_inode);
4320 * sctx->send_progress. We need to prevent
4324 nce = name_cache_search(sctx, ow_inode, ow_gen);
4326 btrfs_lru_cache_remove(&sctx->name_cache,
4336 ret = is_ancestor(sctx->parent_root,
4338 sctx->cur_ino, NULL);
4342 ret = get_cur_path(sctx, sctx->cur_ino,
4343 sctx->cur_inode_gen,
4356 ret = refresh_ref_path(sctx, cur);
4360 ret = send_unlink(sctx, cur->full_path);
4368 list_for_each_entry(cur, &sctx->new_refs, list) {
4376 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
4385 list_for_each_entry(cur2, &sctx->new_refs, list) {
4399 ret = did_create_dir(sctx, cur->dir);
4403 ret = send_create_inode(sctx, cur->dir);
4406 cache_dir_created(sctx, cur->dir);
4410 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4411 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4420 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4422 ret = wait_for_parent_move(sctx, cur, is_orphan);
4437 ret = send_rename(sctx, valid_path, cur->full_path);
4445 if (S_ISDIR(sctx->cur_inode_mode)) {
4451 ret = send_rename(sctx, valid_path,
4467 ret = update_ref_path(sctx, cur);
4471 ret = send_link(sctx, cur->full_path,
4482 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4489 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4493 ret = send_rmdir(sctx, valid_path);
4497 ret = orphanize_inode(sctx, sctx->cur_ino,
4498 sctx->cur_inode_gen, valid_path);
4504 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4509 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4510 !list_empty(&sctx->deleted_refs)) {
4514 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4519 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4525 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4526 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4527 sctx->cur_ino, sctx->cur_inode_gen,
4540 ret = update_ref_path(sctx, cur);
4544 ret = send_unlink(sctx, cur->full_path);
4561 ret = send_unlink(sctx, valid_path);
4579 if (cur->dir > sctx->cur_ino)
4582 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
4588 ret = cache_dir_utimes(sctx, cur->dir, cur->dir_gen);
4593 ret = can_rmdir(sctx, cur->dir, cur->dir_gen);
4597 ret = get_cur_path(sctx, cur->dir,
4601 ret = send_rmdir(sctx, valid_path);
4613 free_recorded_refs(sctx);
4653 struct send_ctx *sctx)
4671 ret = get_cur_path(sctx, dir, dir_gen, path);
4697 struct send_ctx *sctx = ctx;
4703 ret = get_inode_gen(sctx->send_root, dir, &dir_gen);
4710 node = rb_find(&data, &sctx->rbtree_deleted_refs, rbtree_ref_comp);
4715 ret = record_ref_in_tree(&sctx->rbtree_new_refs,
4716 &sctx->new_refs, name, dir, dir_gen,
4717 sctx);
4727 struct send_ctx *sctx = ctx;
4733 ret = get_inode_gen(sctx->parent_root, dir, &dir_gen);
4740 node = rb_find(&data, &sctx->rbtree_new_refs, rbtree_ref_comp);
4745 ret = record_ref_in_tree(&sctx->rbtree_deleted_refs,
4746 &sctx->deleted_refs, name, dir,
4747 dir_gen, sctx);
4753 static int record_new_ref(struct send_ctx *sctx)
4757 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4758 sctx->cmp_key, 0, record_new_ref_if_needed, sctx);
4767 static int record_deleted_ref(struct send_ctx *sctx)
4771 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4772 sctx->cmp_key, 0, record_deleted_ref_if_needed,
4773 sctx);
4782 static int record_changed_ref(struct send_ctx *sctx)
4786 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4787 sctx->cmp_key, 0, record_new_ref_if_needed, sctx);
4790 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4791 sctx->cmp_key, 0, record_deleted_ref_if_needed, sctx);
4804 static int process_all_refs(struct send_ctx *sctx,
4821 root = sctx->send_root;
4824 root = sctx->parent_root;
4827 btrfs_err(sctx->send_root->fs_info,
4833 key.objectid = sctx->cmp_key->objectid;
4842 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4858 ret = process_recorded_refs(sctx, &pending_move);
4864 static int send_set_xattr(struct send_ctx *sctx,
4871 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4875 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4876 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4877 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4879 ret = send_cmd(sctx);
4886 static int send_remove_xattr(struct send_ctx *sctx,
4892 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4896 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4897 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4899 ret = send_cmd(sctx);
4911 struct send_ctx *sctx = ctx;
4939 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4943 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4955 struct send_ctx *sctx = ctx;
4962 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4966 ret = send_remove_xattr(sctx, p, name, name_len);
4973 static int process_new_xattr(struct send_ctx *sctx)
4977 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4978 __process_new_xattr, sctx);
4983 static int process_deleted_xattr(struct send_ctx *sctx)
4985 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4986 __process_deleted_xattr, sctx);
5051 struct send_ctx *sctx = ctx;
5055 ret = find_xattr(sctx->parent_root, sctx->right_path,
5056 sctx->cmp_key, name, name_len, &found_data,
5081 struct send_ctx *sctx = ctx;
5083 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
5094 static int process_changed_xattr(struct send_ctx *sctx)
5098 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
5099 __process_changed_new_xattr, sctx);
5102 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
5103 __process_changed_deleted_xattr, sctx);
5109 static int process_all_new_xattrs(struct send_ctx *sctx)
5122 root = sctx->send_root;
5124 key.objectid = sctx->cmp_key->objectid;
5134 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
5146 static int send_verity(struct send_ctx *sctx, struct fs_path *path,
5151 ret = begin_cmd(sctx, BTRFS_SEND_C_ENABLE_VERITY);
5155 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
5156 TLV_PUT_U8(sctx, BTRFS_SEND_A_VERITY_ALGORITHM,
5158 TLV_PUT_U32(sctx, BTRFS_SEND_A_VERITY_BLOCK_SIZE,
5160 TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SALT_DATA, desc->salt,
5162 TLV_PUT(sctx, BTRFS_SEND_A_VERITY_SIG_DATA, desc->signature,
5165 ret = send_cmd(sctx);
5172 static int process_verity(struct send_ctx *sctx)
5175 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
5179 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, sctx->send_root);
5191 if (!sctx->verity_descriptor) {
5192 sctx->verity_descriptor = kvmalloc(FS_VERITY_MAX_DESCRIPTOR_SIZE,
5194 if (!sctx->verity_descriptor) {
5200 ret = btrfs_get_verity_descriptor(inode, sctx->verity_descriptor, ret);
5209 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5213 ret = send_verity(sctx, p, sctx->verity_descriptor);
5224 static inline u64 max_send_read_size(const struct send_ctx *sctx)
5226 return sctx->send_max_size - SZ_16K;
5229 static int put_data_header(struct send_ctx *sctx, u32 len)
5231 if (WARN_ON_ONCE(sctx->put_data))
5233 sctx->put_data = true;
5234 if (sctx->proto >= 2) {
5239 if (sctx->send_max_size - sctx->send_size < sizeof(__le16) + len)
5241 put_unaligned_le16(BTRFS_SEND_A_DATA, sctx->send_buf + sctx->send_size);
5242 sctx->send_size += sizeof(__le16);
5246 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
5248 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
5251 sctx->send_size += sizeof(*hdr);
5256 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
5258 struct btrfs_root *root = sctx->send_root;
5266 ret = put_data_header(sctx, len);
5276 page = find_lock_page(sctx->cur_inode->i_mapping, index);
5278 page_cache_sync_readahead(sctx->cur_inode->i_mapping,
5279 &sctx->ra, NULL, index,
5282 page = find_or_create_page(sctx->cur_inode->i_mapping,
5291 page_cache_async_readahead(sctx->cur_inode->i_mapping,
5292 &sctx->ra, NULL, page_folio(page),
5302 page_offset(page), sctx->cur_ino,
5303 sctx->send_root->root_key.objectid);
5310 memcpy_from_page(sctx->send_buf + sctx->send_size, page,
5317 sctx->send_size += cur_len;
5327 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
5329 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
5339 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5343 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5347 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5348 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5349 ret = put_file_data(sctx, offset, len);
5353 ret = send_cmd(sctx);
5364 static int send_clone(struct send_ctx *sctx,
5372 btrfs_debug(sctx->send_root->fs_info,
5381 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5385 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5389 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5390 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5391 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5393 if (clone_root->root == sctx->send_root) {
5394 ret = get_inode_gen(sctx->send_root, clone_root->ino, &gen);
5397 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5414 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5417 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5419 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
5421 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5422 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5425 ret = send_cmd(sctx);
5436 static int send_update_extent(struct send_ctx *sctx,
5446 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5450 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5454 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5455 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5456 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5458 ret = send_cmd(sctx);
5466 static int send_hole(struct send_ctx *sctx, u64 end)
5469 u64 read_size = max_send_read_size(sctx);
5470 u64 offset = sctx->cur_inode_last_extent;
5479 if (offset >= sctx->cur_inode_size)
5486 end = min_t(u64, end, sctx->cur_inode_size);
5488 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5489 return send_update_extent(sctx, offset, end - offset);
5494 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5500 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5503 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5504 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5505 ret = put_data_header(sctx, len);
5508 memset(sctx->send_buf + sctx->send_size, 0, len);
5509 sctx->send_size += len;
5510 ret = send_cmd(sctx);
5515 sctx->cur_inode_next_write_offset = offset;
5521 static int send_encoded_inline_extent(struct send_ctx *sctx,
5525 struct btrfs_root *root = sctx->send_root;
5536 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
5546 ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE);
5550 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5559 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath);
5560 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5561 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
5563 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, ram_bytes);
5564 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET, offset - key.offset);
5569 TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret);
5571 ret = put_data_header(sctx, inline_size);
5574 read_extent_buffer(leaf, sctx->send_buf + sctx->send_size,
5576 sctx->send_size += inline_size;
5578 ret = send_cmd(sctx);
5587 static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
5590 struct btrfs_root *root = sctx->send_root;
5603 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
5613 ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE);
5617 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5626 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath);
5627 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5628 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
5631 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN,
5633 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET,
5639 TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret);
5640 TLV_PUT_U32(sctx, BTRFS_SEND_A_ENCRYPTION, 0);
5642 ret = put_data_header(sctx, disk_num_bytes);
5651 data_offset = PAGE_ALIGN(sctx->send_size);
5652 if (data_offset > sctx->send_max_size ||
5653 sctx->send_max_size - data_offset < disk_num_bytes) {
5664 sctx->send_buf_pages +
5669 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
5670 hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr));
5672 crc = btrfs_crc32c(0, sctx->send_buf, sctx->send_size);
5673 crc = btrfs_crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes);
5676 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
5677 &sctx->send_off);
5679 ret = write_buf(sctx->send_filp, sctx->send_buf + data_offset,
5680 disk_num_bytes, &sctx->send_off);
5682 sctx->send_size = 0;
5683 sctx->put_data = false;
5692 static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path,
5698 u64 read_size = max_send_read_size(sctx);
5701 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5702 return send_update_extent(sctx, offset, len);
5706 if ((sctx->flags & BTRFS_SEND_FLAG_COMPRESSED) &&
5721 return send_encoded_inline_extent(sctx, path, offset,
5725 return send_encoded_extent(sctx, path, offset, len);
5729 if (sctx->cur_inode == NULL) {
5730 struct btrfs_root *root = sctx->send_root;
5732 sctx->cur_inode = btrfs_iget(root->fs_info->sb, sctx->cur_ino, root);
5733 if (IS_ERR(sctx->cur_inode)) {
5734 int err = PTR_ERR(sctx->cur_inode);
5736 sctx->cur_inode = NULL;
5739 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
5740 file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping);
5761 sctx->clean_page_cache = (sctx->cur_inode->i_mapping->nrpages == 0);
5762 sctx->page_cache_clear_start = round_down(offset, PAGE_SIZE);
5769 ret = send_write(sctx, offset + sent, size);
5775 if (sctx->clean_page_cache && PAGE_ALIGNED(end)) {
5799 truncate_inode_pages_range(&sctx->cur_inode->i_data,
5800 sctx->page_cache_clear_start,
5802 sctx->page_cache_clear_start = end;
5809 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5815 static int send_capabilities(struct send_ctx *sctx)
5830 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5850 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5857 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5866 static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path,
5892 len == sctx->send_root->fs_info->sectorsize)
5893 return send_extent_data(sctx, dst_path, offset, len);
5991 ret = send_extent_data(sctx, dst_path, offset,
6056 offset + clone_len < sctx->cur_inode_size) {
6062 ret = send_clone(sctx, offset, slen,
6067 ret = send_extent_data(sctx, dst_path,
6071 ret = send_clone(sctx, offset, clone_len,
6094 ret = send_extent_data(sctx, dst_path, offset,
6116 if (clone_root->root == sctx->send_root &&
6117 clone_root->ino == sctx->cur_ino &&
6118 clone_root->offset >= sctx->cur_inode_next_write_offset)
6127 ret = send_extent_data(sctx, dst_path, offset, len);
6135 static int send_write_or_clone(struct send_ctx *sctx,
6143 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
6145 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
6158 ret = clone_range(sctx, path, clone_root, disk_byte,
6161 ret = send_extent_data(sctx, path, offset, end - offset);
6163 sctx->cur_inode_next_write_offset = end;
6167 static int is_extent_unchanged(struct send_ctx *sctx,
6232 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
6322 ret = btrfs_next_item(sctx->parent_root, path);
6357 static int get_last_extent(struct send_ctx *sctx, u64 offset)
6360 struct btrfs_root *root = sctx->send_root;
6368 sctx->cur_inode_last_extent = 0;
6370 key.objectid = sctx->cur_ino;
6378 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
6381 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
6387 static int range_is_hole_in_parent(struct send_ctx *sctx,
6393 struct btrfs_root *root = sctx->parent_root;
6401 key.objectid = sctx->cur_ino;
6426 if (key.objectid < sctx->cur_ino ||
6429 if (key.objectid > sctx->cur_ino ||
6453 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
6458 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
6461 if (sctx->cur_inode_last_extent == (u64)-1) {
6462 ret = get_last_extent(sctx, key->offset - 1);
6468 sctx->cur_inode_last_extent < key->offset) {
6476 ret = get_last_extent(sctx, key->offset - 1);
6481 if (sctx->cur_inode_last_extent < key->offset) {
6482 ret = range_is_hole_in_parent(sctx,
6483 sctx->cur_inode_last_extent,
6488 ret = send_hole(sctx, key->offset);
6492 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
6496 static int process_extent(struct send_ctx *sctx,
6503 if (S_ISLNK(sctx->cur_inode_mode))
6506 if (sctx->parent_root && !sctx->cur_inode_new) {
6507 ret = is_extent_unchanged(sctx, path, key);
6542 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
6543 sctx->cur_inode_size, &found_clone);
6547 ret = send_write_or_clone(sctx, path, key, found_clone);
6551 ret = maybe_send_hole(sctx, path, key);
6556 static int process_all_extents(struct send_ctx *sctx)
6565 root = sctx->send_root;
6570 key.objectid = sctx->cmp_key->objectid;
6580 ret = process_extent(sctx, path, &found_key);
6592 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
6598 if (sctx->cur_ino == 0)
6600 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
6601 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
6603 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
6606 ret = process_recorded_refs(sctx, pending_move);
6615 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
6634 if (sctx->ignore_cur_inode)
6637 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6655 sctx->send_progress = sctx->cur_ino + 1;
6657 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6659 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6661 ret = get_inode_info(sctx->send_root, sctx->cur_ino, &info);
6669 if (!sctx->parent_root || sctx->cur_inode_new) {
6671 if (!S_ISLNK(sctx->cur_inode_mode))
6673 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6678 ret = get_inode_info(sctx->parent_root, sctx->cur_ino, &info);
6689 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6691 if (!S_ISLNK(sctx->cur_inode_mode) && left_fileattr != right_fileattr)
6693 if ((old_size == sctx->cur_inode_size) ||
6694 (sctx->cur_inode_size > old_size &&
6695 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6699 if (S_ISREG(sctx->cur_inode_mode)) {
6700 if (need_send_hole(sctx)) {
6701 if (sctx->cur_inode_last_extent == (u64)-1 ||
6702 sctx->cur_inode_last_extent <
6703 sctx->cur_inode_size) {
6704 ret = get_last_extent(sctx, (u64)-1);
6708 if (sctx->cur_inode_last_extent < sctx->cur_inode_size) {
6709 ret = range_is_hole_in_parent(sctx,
6710 sctx->cur_inode_last_extent,
6711 sctx->cur_inode_size);
6715 ret = send_hole(sctx, sctx->cur_inode_size);
6725 ret = send_truncate(sctx, sctx->cur_ino,
6726 sctx->cur_inode_gen,
6727 sctx->cur_inode_size);
6734 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6740 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6746 ret = send_fileattr(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6752 if (proto_cmd_ok(sctx, BTRFS_SEND_C_ENABLE_VERITY)
6753 && sctx->cur_inode_needs_verity) {
6754 ret = process_verity(sctx);
6759 ret = send_capabilities(sctx);
6767 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6768 ret = apply_children_dir_moves(sctx);
6778 sctx->send_progress = sctx->cur_ino + 1;
6786 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_size > 0)
6787 ret = cache_dir_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6789 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6797 ret = trim_dir_utimes_cache(sctx);
6802 static void close_current_inode(struct send_ctx *sctx)
6806 if (sctx->cur_inode == NULL)
6809 i_size = i_size_read(sctx->cur_inode);
6817 if (sctx->clean_page_cache && sctx->page_cache_clear_start < i_size)
6818 truncate_inode_pages_range(&sctx->cur_inode->i_data,
6819 sctx->page_cache_clear_start,
6822 iput(sctx->cur_inode);
6823 sctx->cur_inode = NULL;
6826 static int changed_inode(struct send_ctx *sctx,
6830 struct btrfs_key *key = sctx->cmp_key;
6836 close_current_inode(sctx);
6838 sctx->cur_ino = key->objectid;
6839 sctx->cur_inode_new_gen = false;
6840 sctx->cur_inode_last_extent = (u64)-1;
6841 sctx->cur_inode_next_write_offset = 0;
6842 sctx->ignore_cur_inode = false;
6849 sctx->send_progress = sctx->cur_ino;
6853 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6854 sctx->left_path->slots[0],
6856 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6859 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6860 sctx->right_path->slots[0],
6862 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6866 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6867 sctx->right_path->slots[0],
6870 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6879 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6880 sctx->cur_inode_new_gen = true;
6918 if (btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii) == 0) {
6919 sctx->ignore_cur_inode = true;
6922 sctx->cur_inode_gen = left_gen;
6923 sctx->cur_inode_new = true;
6924 sctx->cur_inode_deleted = false;
6925 sctx->cur_inode_size = btrfs_inode_size(
6926 sctx->left_path->nodes[0], left_ii);
6927 sctx->cur_inode_mode = btrfs_inode_mode(
6928 sctx->left_path->nodes[0], left_ii);
6929 sctx->cur_inode_rdev = btrfs_inode_rdev(
6930 sctx->left_path->nodes[0], left_ii);
6931 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6932 ret = send_create_inode_if_needed(sctx);
6934 sctx->cur_inode_gen = right_gen;
6935 sctx->cur_inode_new = false;
6936 sctx->cur_inode_deleted = true;
6937 sctx->cur_inode_size = btrfs_inode_size(
6938 sctx->right_path->nodes[0], right_ii);
6939 sctx->cur_inode_mode = btrfs_inode_mode(
6940 sctx->right_path->nodes[0], right_ii);
6944 new_nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6945 old_nlinks = btrfs_inode_nlink(sctx->right_path->nodes[0], right_ii);
6947 sctx->ignore_cur_inode = true;
6950 sctx->cur_inode_new_gen = 1;
6959 if (sctx->cur_inode_new_gen) {
6964 sctx->cur_inode_gen = right_gen;
6965 sctx->cur_inode_new = false;
6966 sctx->cur_inode_deleted = true;
6967 sctx->cur_inode_size = btrfs_inode_size(
6968 sctx->right_path->nodes[0], right_ii);
6969 sctx->cur_inode_mode = btrfs_inode_mode(
6970 sctx->right_path->nodes[0], right_ii);
6971 ret = process_all_refs(sctx,
6981 sctx->cur_inode_gen = left_gen;
6982 sctx->cur_inode_new = true;
6983 sctx->cur_inode_deleted = false;
6984 sctx->cur_inode_size = btrfs_inode_size(
6985 sctx->left_path->nodes[0],
6987 sctx->cur_inode_mode = btrfs_inode_mode(
6988 sctx->left_path->nodes[0],
6990 sctx->cur_inode_rdev = btrfs_inode_rdev(
6991 sctx->left_path->nodes[0],
6993 ret = send_create_inode_if_needed(sctx);
6997 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
7005 sctx->send_progress = sctx->cur_ino + 1;
7011 ret = process_all_extents(sctx);
7014 ret = process_all_new_xattrs(sctx);
7019 sctx->cur_inode_gen = left_gen;
7020 sctx->cur_inode_new = false;
7021 sctx->cur_inode_new_gen = false;
7022 sctx->cur_inode_deleted = false;
7023 sctx->cur_inode_size = btrfs_inode_size(
7024 sctx->left_path->nodes[0], left_ii);
7025 sctx->cur_inode_mode = btrfs_inode_mode(
7026 sctx->left_path->nodes[0], left_ii);
7044 static int changed_ref(struct send_ctx *sctx,
7049 if (sctx->cur_ino != sctx->cmp_key->objectid) {
7050 inconsistent_snapshot_error(sctx, result, "reference");
7054 if (!sctx->cur_inode_new_gen &&
7055 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
7057 ret = record_new_ref(sctx);
7059 ret = record_deleted_ref(sctx);
7061 ret = record_changed_ref(sctx);
7072 static int changed_xattr(struct send_ctx *sctx,
7077 if (sctx->cur_ino != sctx->cmp_key->objectid) {
7078 inconsistent_snapshot_error(sctx, result, "xattr");
7082 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
7084 ret = process_new_xattr(sctx);
7086 ret = process_deleted_xattr(sctx);
7088 ret = process_changed_xattr(sctx);
7099 static int changed_extent(struct send_ctx *sctx,
7117 if (sctx->cur_ino != sctx->cmp_key->objectid)
7120 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
7122 ret = process_extent(sctx, sctx->left_path,
7123 sctx->cmp_key);
7129 static int changed_verity(struct send_ctx *sctx, enum btrfs_compare_tree_result result)
7133 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
7135 sctx->cur_inode_needs_verity = true;
7140 static int dir_changed(struct send_ctx *sctx, u64 dir)
7145 ret = get_inode_gen(sctx->send_root, dir, &new_gen);
7149 ret = get_inode_gen(sctx->parent_root, dir, &orig_gen);
7156 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
7172 ret = dir_changed(sctx, dirid);
7187 ret = dir_changed(sctx, dirid);
7197 * Updates compare related fields in sctx and simply forwards to the actual
7204 struct send_ctx *sctx)
7233 lockdep_assert_not_held(&sctx->send_root->fs_info->commit_root_sem);
7255 ret = compare_refs(sctx, left_path, key);
7261 return maybe_send_hole(sctx, left_path, key);
7269 sctx->left_path = left_path;
7270 sctx->right_path = right_path;
7271 sctx->cmp_key = key;
7273 ret = finish_inode_if_needed(sctx, 0);
7283 ret = changed_inode(sctx, result);
7284 } else if (!sctx->ignore_cur_inode) {
7287 ret = changed_ref(sctx, result);
7289 ret = changed_xattr(sctx, result);
7291 ret = changed_extent(sctx, result);
7294 ret = changed_verity(sctx, result);
7301 static int search_key_again(const struct send_ctx *sctx,
7325 (root == sctx->parent_root ? "parent" : "send"),
7334 static int full_send_tree(struct send_ctx *sctx)
7337 struct btrfs_root *send_root = sctx->send_root;
7352 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7365 BTRFS_COMPARE_TREE_NEW, sctx);
7370 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
7371 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7384 ret = search_key_again(sctx, send_root, path, &key);
7401 ret = finish_inode_if_needed(sctx, 1);
7575 const struct send_ctx *sctx)
7580 lockdep_assert_held_read(&sctx->send_root->fs_info->commit_root_sem);
7592 ret = search_key_again(sctx, sctx->send_root, left_path, left_key);
7597 ret = search_key_again(sctx, sctx->parent_root, right_path, right_key);
7624 root_level = btrfs_header_level(sctx->send_root->commit_root);
7631 root_level = btrfs_header_level(sctx->parent_root->commit_root);
7655 struct btrfs_root *right_root, struct send_ctx *sctx)
7784 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7794 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
7798 sctx);
7801 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7836 sctx);
7849 sctx);
7865 sctx);
7871 sctx);
7884 &left_key, result, sctx);
7940 static int send_subvol(struct send_ctx *sctx)
7944 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
7945 ret = send_header(sctx);
7950 ret = send_subvol_begin(sctx);
7954 if (sctx->parent_root) {
7955 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
7958 ret = finish_inode_if_needed(sctx, 1);
7962 ret = full_send_tree(sctx);
7968 free_recorded_refs(sctx);
7985 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7991 if (sctx->parent_root &&
7992 sctx->parent_root->node != sctx->parent_root->commit_root)
7995 for (i = 0; i < sctx->clone_roots_cnt; i++)
7996 if (sctx->clone_roots[i].root->node !=
7997 sctx->clone_roots[i].root->commit_root)
8008 trans = btrfs_join_transaction(sctx->send_root);
8025 static int flush_delalloc_roots(struct send_ctx *sctx)
8027 struct btrfs_root *root = sctx->parent_root;
8038 for (i = 0; i < sctx->clone_roots_cnt; i++) {
8039 root = sctx->clone_roots[i].root;
8077 struct send_ctx *sctx = NULL;
8127 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
8128 if (!sctx) {
8133 INIT_LIST_HEAD(&sctx->new_refs);
8134 INIT_LIST_HEAD(&sctx->deleted_refs);
8136 btrfs_lru_cache_init(&sctx->name_cache, SEND_MAX_NAME_CACHE_SIZE);
8137 btrfs_lru_cache_init(&sctx->backref_cache, SEND_MAX_BACKREF_CACHE_SIZE);
8138 btrfs_lru_cache_init(&sctx->dir_created_cache,
8144 btrfs_lru_cache_init(&sctx->dir_utimes_cache, 0);
8146 sctx->pending_dir_moves = RB_ROOT;
8147 sctx->waiting_dir_moves = RB_ROOT;
8148 sctx->orphan_dirs = RB_ROOT;
8149 sctx->rbtree_new_refs = RB_ROOT;
8150 sctx->rbtree_deleted_refs = RB_ROOT;
8152 sctx->flags = arg->flags;
8160 sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION;
8162 sctx->proto = 1;
8164 if ((arg->flags & BTRFS_SEND_FLAG_COMPRESSED) && sctx->proto < 2) {
8169 sctx->send_filp = fget(arg->send_fd);
8170 if (!sctx->send_filp || !(sctx->send_filp->f_mode & FMODE_WRITE)) {
8175 sctx->send_root = send_root;
8180 if (btrfs_root_dead(sctx->send_root)) {
8185 sctx->clone_roots_cnt = arg->clone_sources_count;
8187 if (sctx->proto >= 2) {
8190 sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V2;
8191 sctx->send_buf = vmalloc(sctx->send_max_size);
8192 if (!sctx->send_buf) {
8196 send_buf_num_pages = sctx->send_max_size >> PAGE_SHIFT;
8197 sctx->send_buf_pages = kcalloc(send_buf_num_pages,
8198 sizeof(*sctx->send_buf_pages),
8200 if (!sctx->send_buf_pages) {
8205 sctx->send_buf_pages[i] =
8206 vmalloc_to_page(sctx->send_buf + (i << PAGE_SHIFT));
8209 sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V1;
8210 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
8212 if (!sctx->send_buf) {
8217 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
8220 if (!sctx->clone_roots) {
8267 sctx->clone_roots[i].root = clone_root;
8275 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
8277 if (IS_ERR(sctx->parent_root)) {
8278 ret = PTR_ERR(sctx->parent_root);
8282 spin_lock(&sctx->parent_root->root_item_lock);
8283 sctx->parent_root->send_in_progress++;
8284 if (!btrfs_root_readonly(sctx->parent_root) ||
8285 btrfs_root_dead(sctx->parent_root)) {
8286 spin_unlock(&sctx->parent_root->root_item_lock);
8290 if (sctx->parent_root->dedupe_in_progress) {
8291 dedupe_in_progress_warn(sctx->parent_root);
8292 spin_unlock(&sctx->parent_root->root_item_lock);
8296 spin_unlock(&sctx->parent_root->root_item_lock);
8304 sctx->clone_roots[sctx->clone_roots_cnt++].root =
8305 btrfs_grab_root(sctx->send_root);
8308 sort(sctx->clone_roots, sctx->clone_roots_cnt,
8309 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
8313 ret = flush_delalloc_roots(sctx);
8317 ret = ensure_commit_roots_uptodate(sctx);
8321 ret = send_subvol(sctx);
8325 btrfs_lru_cache_for_each_entry_safe(&sctx->dir_utimes_cache, entry, tmp) {
8326 ret = send_utimes(sctx, entry->key, entry->gen);
8329 btrfs_lru_cache_remove(&sctx->dir_utimes_cache, entry);
8332 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
8333 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
8336 ret = send_cmd(sctx);
8342 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
8343 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
8347 n = rb_first(&sctx->pending_dir_moves);
8354 free_pending_move(sctx, pm2);
8356 free_pending_move(sctx, pm);
8359 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
8360 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
8364 n = rb_first(&sctx->waiting_dir_moves);
8366 rb_erase(&dm->node, &sctx->waiting_dir_moves);
8370 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
8371 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
8375 n = rb_first(&sctx->orphan_dirs);
8377 free_orphan_dir_info(sctx, odi);
8381 for (i = 0; i < sctx->clone_roots_cnt; i++) {
8383 sctx->clone_roots[i].root);
8384 btrfs_put_root(sctx->clone_roots[i].root);
8387 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
8389 sctx->clone_roots[i].root);
8390 btrfs_put_root(sctx->clone_roots[i].root);
8395 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
8396 btrfs_root_dec_send_in_progress(sctx->parent_root);
8397 btrfs_put_root(sctx->parent_root);
8402 if (sctx) {
8403 if (sctx->send_filp)
8404 fput(sctx->send_filp);
8406 kvfree(sctx->clone_roots);
8407 kfree(sctx->send_buf_pages);
8408 kvfree(sctx->send_buf);
8409 kvfree(sctx->verity_descriptor);
8411 close_current_inode(sctx);
8413 btrfs_lru_cache_clear(&sctx->name_cache);
8414 btrfs_lru_cache_clear(&sctx->backref_cache);
8415 btrfs_lru_cache_clear(&sctx->dir_created_cache);
8416 btrfs_lru_cache_clear(&sctx->dir_utimes_cache);
8418 kfree(sctx);