Lines Matching refs:sctx
282 static void inconsistent_snapshot_error(struct send_ctx *sctx, in inconsistent_snapshot_error() argument
307 btrfs_err(sctx->send_root->fs_info, in inconsistent_snapshot_error()
309 result_string, what, sctx->cmp_key->objectid, in inconsistent_snapshot_error()
310 sctx->send_root->root_key.objectid, in inconsistent_snapshot_error()
311 (sctx->parent_root ? in inconsistent_snapshot_error()
312 sctx->parent_root->root_key.objectid : 0)); in inconsistent_snapshot_error()
315 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
318 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
320 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
322 static int need_send_hole(struct send_ctx *sctx) in need_send_hole() argument
324 return (sctx->parent_root && !sctx->cur_inode_new && in need_send_hole()
325 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && in need_send_hole()
326 S_ISREG(sctx->cur_inode_mode)); in need_send_hole()
571 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) in tlv_put() argument
575 int left = sctx->send_max_size - sctx->send_size; in tlv_put()
580 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); in tlv_put()
584 sctx->send_size += total_len; in tlv_put()
590 static int tlv_put_u##bits(struct send_ctx *sctx, \
594 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
599 static int tlv_put_string(struct send_ctx *sctx, u16 attr, in tlv_put_string() argument
604 return tlv_put(sctx, attr, str, len); in tlv_put_string()
607 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, in tlv_put_uuid() argument
610 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); in tlv_put_uuid()
613 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, in tlv_put_btrfs_timespec() argument
619 return tlv_put(sctx, attr, &bts, sizeof(bts)); in tlv_put_btrfs_timespec()
623 #define TLV_PUT(sctx, attrtype, data, attrlen) \ argument
625 ret = tlv_put(sctx, attrtype, data, attrlen); \
630 #define TLV_PUT_INT(sctx, attrtype, bits, value) \ argument
632 ret = tlv_put_u##bits(sctx, attrtype, value); \
637 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) argument
638 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) argument
639 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) argument
640 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) argument
641 #define TLV_PUT_STRING(sctx, attrtype, str, len) \ argument
643 ret = tlv_put_string(sctx, attrtype, str, len); \
647 #define TLV_PUT_PATH(sctx, attrtype, p) \ argument
649 ret = tlv_put_string(sctx, attrtype, p->start, \
654 #define TLV_PUT_UUID(sctx, attrtype, uuid) \ argument
656 ret = tlv_put_uuid(sctx, attrtype, uuid); \
660 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ argument
662 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
667 static int send_header(struct send_ctx *sctx) in send_header() argument
674 return write_buf(sctx->send_filp, &hdr, sizeof(hdr), in send_header()
675 &sctx->send_off); in send_header()
681 static int begin_cmd(struct send_ctx *sctx, int cmd) in begin_cmd() argument
685 if (WARN_ON(!sctx->send_buf)) in begin_cmd()
688 BUG_ON(sctx->send_size); in begin_cmd()
690 sctx->send_size += sizeof(*hdr); in begin_cmd()
691 hdr = (struct btrfs_cmd_header *)sctx->send_buf; in begin_cmd()
697 static int send_cmd(struct send_ctx *sctx) in send_cmd() argument
703 hdr = (struct btrfs_cmd_header *)sctx->send_buf; in send_cmd()
704 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len); in send_cmd()
707 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); in send_cmd()
710 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, in send_cmd()
711 &sctx->send_off); in send_cmd()
713 sctx->total_send_size += sctx->send_size; in send_cmd()
714 sctx->cmd_send_size[get_unaligned_le16(&hdr->cmd)] += sctx->send_size; in send_cmd()
715 sctx->send_size = 0; in send_cmd()
723 static int send_rename(struct send_ctx *sctx, in send_rename() argument
726 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_rename()
731 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); in send_rename()
735 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); in send_rename()
736 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); in send_rename()
738 ret = send_cmd(sctx); in send_rename()
748 static int send_link(struct send_ctx *sctx, in send_link() argument
751 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_link()
756 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); in send_link()
760 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_link()
761 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); in send_link()
763 ret = send_cmd(sctx); in send_link()
773 static int send_unlink(struct send_ctx *sctx, struct fs_path *path) in send_unlink() argument
775 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_unlink()
780 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); in send_unlink()
784 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_unlink()
786 ret = send_cmd(sctx); in send_unlink()
796 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) in send_rmdir() argument
798 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_rmdir()
803 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); in send_rmdir()
807 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_rmdir()
809 ret = send_cmd(sctx); in send_rmdir()
1179 struct send_ctx *sctx; member
1232 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, in __iterate_backrefs()
1233 bctx->sctx->clone_roots_cnt, in __iterate_backrefs()
1239 if (found->root == bctx->sctx->send_root && in __iterate_backrefs()
1249 if (found->root == bctx->sctx->send_root) { in __iterate_backrefs()
1265 bctx->sctx->cur_inode_next_write_offset) in __iterate_backrefs()
1294 static int find_extent_clone(struct send_ctx *sctx, in find_extent_clone() argument
1300 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in find_extent_clone()
1382 for (i = 0; i < sctx->clone_roots_cnt; i++) { in find_extent_clone()
1383 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1389 backref_ctx.sctx = sctx; in find_extent_clone()
1435 for (i = 0; i < sctx->clone_roots_cnt; i++) { in find_extent_clone()
1436 if (sctx->clone_roots[i].found_refs) { in find_extent_clone()
1438 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1439 else if (sctx->clone_roots[i].root == sctx->send_root) in find_extent_clone()
1441 cur_clone_root = sctx->clone_roots + i; in find_extent_clone()
1518 static int gen_unique_name(struct send_ctx *sctx, in gen_unique_name() argument
1538 di = btrfs_lookup_dir_item(NULL, sctx->send_root, in gen_unique_name()
1552 if (!sctx->parent_root) { in gen_unique_name()
1558 di = btrfs_lookup_dir_item(NULL, sctx->parent_root, in gen_unique_name()
1590 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) in get_cur_inode_state() argument
1598 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL, in get_cur_inode_state()
1604 if (!sctx->parent_root) { in get_cur_inode_state()
1607 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, in get_cur_inode_state()
1618 if (ino < sctx->send_progress) in get_cur_inode_state()
1623 if (ino < sctx->send_progress) in get_cur_inode_state()
1632 if (ino < sctx->send_progress) in get_cur_inode_state()
1641 if (ino < sctx->send_progress) in get_cur_inode_state()
1656 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) in is_inode_existent() argument
1663 ret = get_cur_inode_state(sctx, ino, gen); in is_inode_existent()
1823 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, in will_overwrite_ref() argument
1832 if (!sctx->parent_root) in will_overwrite_ref()
1835 ret = is_inode_existent(sctx, dir, dir_gen); in will_overwrite_ref()
1844 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) { in will_overwrite_ref()
1845 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, in will_overwrite_ref()
1857 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, in will_overwrite_ref()
1871 if (other_inode > sctx->send_progress || in will_overwrite_ref()
1872 is_waiting_for_move(sctx, other_inode)) { in will_overwrite_ref()
1873 ret = get_inode_info(sctx->parent_root, other_inode, NULL, in will_overwrite_ref()
1895 static int did_overwrite_ref(struct send_ctx *sctx, in did_overwrite_ref() argument
1905 if (!sctx->parent_root) in did_overwrite_ref()
1908 ret = is_inode_existent(sctx, dir, dir_gen); in did_overwrite_ref()
1913 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, in did_overwrite_ref()
1926 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, in did_overwrite_ref()
1936 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL, in did_overwrite_ref()
1952 if ((ow_inode < sctx->send_progress) || in did_overwrite_ref()
1953 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino && in did_overwrite_ref()
1954 gen == sctx->cur_inode_gen)) in did_overwrite_ref()
1968 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) in did_overwrite_first_ref() argument
1975 if (!sctx->parent_root) in did_overwrite_first_ref()
1982 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); in did_overwrite_first_ref()
1986 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, in did_overwrite_first_ref()
2000 static int name_cache_insert(struct send_ctx *sctx, in name_cache_insert() argument
2006 nce_head = radix_tree_lookup(&sctx->name_cache, in name_cache_insert()
2016 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); in name_cache_insert()
2024 list_add_tail(&nce->list, &sctx->name_cache_list); in name_cache_insert()
2025 sctx->name_cache_size++; in name_cache_insert()
2030 static void name_cache_delete(struct send_ctx *sctx, in name_cache_delete() argument
2035 nce_head = radix_tree_lookup(&sctx->name_cache, in name_cache_delete()
2038 btrfs_err(sctx->send_root->fs_info, in name_cache_delete()
2040 nce->ino, sctx->name_cache_size); in name_cache_delete()
2045 sctx->name_cache_size--; in name_cache_delete()
2051 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); in name_cache_delete()
2056 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, in name_cache_search() argument
2062 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); in name_cache_search()
2076 static void name_cache_clean_unused(struct send_ctx *sctx) in name_cache_clean_unused() argument
2080 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) in name_cache_clean_unused()
2083 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { in name_cache_clean_unused()
2084 nce = list_entry(sctx->name_cache_list.next, in name_cache_clean_unused()
2086 name_cache_delete(sctx, nce); in name_cache_clean_unused()
2091 static void name_cache_free(struct send_ctx *sctx) in name_cache_free() argument
2095 while (!list_empty(&sctx->name_cache_list)) { in name_cache_free()
2096 nce = list_entry(sctx->name_cache_list.next, in name_cache_free()
2098 name_cache_delete(sctx, nce); in name_cache_free()
2111 static int __get_cur_name_and_parent(struct send_ctx *sctx, in __get_cur_name_and_parent() argument
2126 nce = name_cache_search(sctx, ino, gen); in __get_cur_name_and_parent()
2128 if (ino < sctx->send_progress && nce->need_later_update) { in __get_cur_name_and_parent()
2129 name_cache_delete(sctx, nce); in __get_cur_name_and_parent()
2138 list_move_tail(&nce->list, &sctx->name_cache_list); in __get_cur_name_and_parent()
2155 ret = is_inode_existent(sctx, ino, gen); in __get_cur_name_and_parent()
2160 ret = gen_unique_name(sctx, ino, gen, dest); in __get_cur_name_and_parent()
2171 if (ino < sctx->send_progress) in __get_cur_name_and_parent()
2172 ret = get_first_ref(sctx->send_root, ino, in __get_cur_name_and_parent()
2175 ret = get_first_ref(sctx->parent_root, ino, in __get_cur_name_and_parent()
2184 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, in __get_cur_name_and_parent()
2190 ret = gen_unique_name(sctx, ino, gen, dest); in __get_cur_name_and_parent()
2214 if (ino < sctx->send_progress) in __get_cur_name_and_parent()
2219 nce_ret = name_cache_insert(sctx, nce); in __get_cur_name_and_parent()
2222 name_cache_clean_unused(sctx); in __get_cur_name_and_parent()
2253 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, in get_cur_path() argument
2276 if (is_waiting_for_rm(sctx, ino, gen)) { in get_cur_path()
2277 ret = gen_unique_name(sctx, ino, gen, name); in get_cur_path()
2284 wdm = get_waiting_dir_move(sctx, ino); in get_cur_path()
2286 ret = gen_unique_name(sctx, ino, gen, name); in get_cur_path()
2289 ret = get_first_ref(sctx->parent_root, ino, in get_cur_path()
2292 ret = __get_cur_name_and_parent(sctx, ino, gen, in get_cur_path()
2320 static int send_subvol_begin(struct send_ctx *sctx) in send_subvol_begin() argument
2323 struct btrfs_root *send_root = sctx->send_root; in send_subvol_begin()
2324 struct btrfs_root *parent_root = sctx->parent_root; in send_subvol_begin()
2368 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); in send_subvol_begin()
2372 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); in send_subvol_begin()
2377 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); in send_subvol_begin()
2379 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid)) in send_subvol_begin()
2380 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, in send_subvol_begin()
2381 sctx->send_root->root_item.received_uuid); in send_subvol_begin()
2383 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, in send_subvol_begin()
2384 sctx->send_root->root_item.uuid); in send_subvol_begin()
2386 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, in send_subvol_begin()
2387 btrfs_root_ctransid(&sctx->send_root->root_item)); in send_subvol_begin()
2390 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_subvol_begin()
2393 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_subvol_begin()
2395 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, in send_subvol_begin()
2396 btrfs_root_ctransid(&sctx->parent_root->root_item)); in send_subvol_begin()
2399 ret = send_cmd(sctx); in send_subvol_begin()
2408 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) in send_truncate() argument
2410 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_truncate()
2420 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); in send_truncate()
2424 ret = get_cur_path(sctx, ino, gen, p); in send_truncate()
2427 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_truncate()
2428 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); in send_truncate()
2430 ret = send_cmd(sctx); in send_truncate()
2438 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) in send_chmod() argument
2440 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_chmod()
2450 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); in send_chmod()
2454 ret = get_cur_path(sctx, ino, gen, p); in send_chmod()
2457 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_chmod()
2458 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); in send_chmod()
2460 ret = send_cmd(sctx); in send_chmod()
2468 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) in send_chown() argument
2470 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_chown()
2481 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); in send_chown()
2485 ret = get_cur_path(sctx, ino, gen, p); in send_chown()
2488 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_chown()
2489 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); in send_chown()
2490 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); in send_chown()
2492 ret = send_cmd(sctx); in send_chown()
2500 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) in send_utimes() argument
2502 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_utimes()
2526 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); in send_utimes()
2536 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); in send_utimes()
2540 ret = get_cur_path(sctx, ino, gen, p); in send_utimes()
2543 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_utimes()
2544 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime); in send_utimes()
2545 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime); in send_utimes()
2546 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime); in send_utimes()
2549 ret = send_cmd(sctx); in send_utimes()
2563 static int send_create_inode(struct send_ctx *sctx, u64 ino) in send_create_inode() argument
2565 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_create_inode()
2579 if (ino != sctx->cur_ino) { in send_create_inode()
2580 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, in send_create_inode()
2585 gen = sctx->cur_inode_gen; in send_create_inode()
2586 mode = sctx->cur_inode_mode; in send_create_inode()
2587 rdev = sctx->cur_inode_rdev; in send_create_inode()
2603 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o", in send_create_inode()
2609 ret = begin_cmd(sctx, cmd); in send_create_inode()
2613 ret = gen_unique_name(sctx, ino, gen, p); in send_create_inode()
2617 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_create_inode()
2618 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); in send_create_inode()
2622 ret = read_symlink(sctx->send_root, ino, p); in send_create_inode()
2625 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); in send_create_inode()
2628 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); in send_create_inode()
2629 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); in send_create_inode()
2632 ret = send_cmd(sctx); in send_create_inode()
2648 static int did_create_dir(struct send_ctx *sctx, u64 dir) in did_create_dir() argument
2668 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); in did_create_dir()
2676 ret = btrfs_next_leaf(sctx->send_root, path); in did_create_dir()
2697 di_key.objectid < sctx->send_progress) { in did_create_dir()
2716 static int send_create_inode_if_needed(struct send_ctx *sctx) in send_create_inode_if_needed() argument
2720 if (S_ISDIR(sctx->cur_inode_mode)) { in send_create_inode_if_needed()
2721 ret = did_create_dir(sctx, sctx->cur_ino); in send_create_inode_if_needed()
2730 ret = send_create_inode(sctx, sctx->cur_ino); in send_create_inode_if_needed()
2803 static void free_recorded_refs(struct send_ctx *sctx) in free_recorded_refs() argument
2805 __free_recorded_refs(&sctx->new_refs); in free_recorded_refs()
2806 __free_recorded_refs(&sctx->deleted_refs); in free_recorded_refs()
2814 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, in orphanize_inode() argument
2824 ret = gen_unique_name(sctx, ino, gen, orphan); in orphanize_inode()
2828 ret = send_rename(sctx, path, orphan); in orphanize_inode()
2835 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx, in add_orphan_dir_info() argument
2838 struct rb_node **p = &sctx->orphan_dirs.rb_node; in add_orphan_dir_info()
2865 rb_insert_color(&odi->node, &sctx->orphan_dirs); in add_orphan_dir_info()
2869 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx, in get_orphan_dir_info() argument
2872 struct rb_node *n = sctx->orphan_dirs.rb_node; in get_orphan_dir_info()
2891 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen) in is_waiting_for_rm() argument
2893 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen); in is_waiting_for_rm()
2898 static void free_orphan_dir_info(struct send_ctx *sctx, in free_orphan_dir_info() argument
2903 rb_erase(&odi->node, &sctx->orphan_dirs); in free_orphan_dir_info()
2912 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen, in can_rmdir() argument
2916 struct btrfs_root *root = sctx->parent_root; in can_rmdir()
2938 odi = get_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
2967 dm = get_waiting_dir_move(sctx, loc.objectid); in can_rmdir()
2969 odi = add_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
2983 odi = add_orphan_dir_info(sctx, dir, dir_gen); in can_rmdir()
2996 free_orphan_dir_info(sctx, odi); in can_rmdir()
3005 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) in is_waiting_for_move() argument
3007 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino); in is_waiting_for_move()
3012 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized) in add_waiting_dir_move() argument
3014 struct rb_node **p = &sctx->waiting_dir_moves.rb_node; in add_waiting_dir_move()
3040 rb_insert_color(&dm->node, &sctx->waiting_dir_moves); in add_waiting_dir_move()
3045 get_waiting_dir_move(struct send_ctx *sctx, u64 ino) in get_waiting_dir_move() argument
3047 struct rb_node *n = sctx->waiting_dir_moves.rb_node; in get_waiting_dir_move()
3062 static void free_waiting_dir_move(struct send_ctx *sctx, in free_waiting_dir_move() argument
3067 rb_erase(&dm->node, &sctx->waiting_dir_moves); in free_waiting_dir_move()
3071 static int add_pending_dir_move(struct send_ctx *sctx, in add_pending_dir_move() argument
3079 struct rb_node **p = &sctx->pending_dir_moves.rb_node; in add_pending_dir_move()
3120 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan); in add_pending_dir_move()
3128 rb_insert_color(&pm->node, &sctx->pending_dir_moves); in add_pending_dir_move()
3139 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx, in get_pending_dir_moves() argument
3142 struct rb_node *n = sctx->pending_dir_moves.rb_node; in get_pending_dir_moves()
3157 static int path_loop(struct send_ctx *sctx, struct fs_path *name, in path_loop() argument
3169 if (is_waiting_for_rm(sctx, ino, gen)) in path_loop()
3171 if (is_waiting_for_move(sctx, ino)) { in path_loop()
3174 ret = get_first_ref(sctx->parent_root, ino, in path_loop()
3177 ret = __get_cur_name_and_parent(sctx, ino, gen, in path_loop()
3199 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm) in apply_dir_move() argument
3204 u64 orig_progress = sctx->send_progress; in apply_dir_move()
3221 dm = get_waiting_dir_move(sctx, pm->ino); in apply_dir_move()
3226 free_waiting_dir_move(sctx, dm); in apply_dir_move()
3229 ret = gen_unique_name(sctx, pm->ino, in apply_dir_move()
3232 ret = get_first_ref(sctx->parent_root, pm->ino, in apply_dir_move()
3236 ret = get_cur_path(sctx, parent_ino, parent_gen, in apply_dir_move()
3245 sctx->send_progress = sctx->cur_ino + 1; in apply_dir_move()
3246 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor); in apply_dir_move()
3252 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor, in apply_dir_move()
3258 dm = get_waiting_dir_move(sctx, pm->ino); in apply_dir_move()
3268 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path); in apply_dir_move()
3272 ret = send_rename(sctx, from_path, to_path); in apply_dir_move()
3280 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen); in apply_dir_move()
3287 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino); in apply_dir_move()
3298 ret = get_cur_path(sctx, rmdir_ino, gen, name); in apply_dir_move()
3301 ret = send_rmdir(sctx, name); in apply_dir_move()
3307 ret = send_utimes(sctx, pm->ino, pm->gen); in apply_dir_move()
3319 ret = get_inode_info(sctx->send_root, cur->dir, NULL, in apply_dir_move()
3328 ret = send_utimes(sctx, cur->dir, cur->dir_gen); in apply_dir_move()
3337 sctx->send_progress = orig_progress; in apply_dir_move()
3342 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m) in free_pending_move() argument
3347 rb_erase(&m->node, &sctx->pending_dir_moves); in free_pending_move()
3352 static void tail_append_pending_moves(struct send_ctx *sctx, in tail_append_pending_moves() argument
3365 rb_erase(&moves->node, &sctx->pending_dir_moves); in tail_append_pending_moves()
3370 static int apply_children_dir_moves(struct send_ctx *sctx) in apply_children_dir_moves() argument
3374 u64 parent_ino = sctx->cur_ino; in apply_children_dir_moves()
3377 pm = get_pending_dir_moves(sctx, parent_ino); in apply_children_dir_moves()
3382 tail_append_pending_moves(sctx, pm, &stack); in apply_children_dir_moves()
3387 ret = apply_dir_move(sctx, pm); in apply_children_dir_moves()
3388 free_pending_move(sctx, pm); in apply_children_dir_moves()
3391 pm = get_pending_dir_moves(sctx, parent_ino); in apply_children_dir_moves()
3393 tail_append_pending_moves(sctx, pm, &stack); in apply_children_dir_moves()
3400 free_pending_move(sctx, pm); in apply_children_dir_moves()
3441 static int wait_for_dest_dir_move(struct send_ctx *sctx, in wait_for_dest_dir_move() argument
3445 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info; in wait_for_dest_dir_move()
3455 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) in wait_for_dest_dir_move()
3466 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); in wait_for_dest_dir_move()
3494 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL, in wait_for_dest_dir_move()
3498 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL, in wait_for_dest_dir_move()
3512 wdm = get_waiting_dir_move(sctx, di_key.objectid); in wait_for_dest_dir_move()
3514 ret = add_pending_dir_move(sctx, in wait_for_dest_dir_move()
3515 sctx->cur_ino, in wait_for_dest_dir_move()
3516 sctx->cur_inode_gen, in wait_for_dest_dir_move()
3518 &sctx->new_refs, in wait_for_dest_dir_move()
3519 &sctx->deleted_refs, in wait_for_dest_dir_move()
3661 static int wait_for_parent_move(struct send_ctx *sctx, in wait_for_parent_move() argument
3690 if (is_waiting_for_move(sctx, ino)) { in wait_for_parent_move()
3701 ret = is_ancestor(sctx->parent_root, in wait_for_parent_move()
3702 sctx->cur_ino, sctx->cur_inode_gen, in wait_for_parent_move()
3711 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after, in wait_for_parent_move()
3715 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before, in wait_for_parent_move()
3726 if (ino > sctx->cur_ino && in wait_for_parent_move()
3731 ret = get_inode_info(sctx->parent_root, ino, NULL, in wait_for_parent_move()
3750 ret = add_pending_dir_move(sctx, in wait_for_parent_move()
3751 sctx->cur_ino, in wait_for_parent_move()
3752 sctx->cur_inode_gen, in wait_for_parent_move()
3754 &sctx->new_refs, in wait_for_parent_move()
3755 &sctx->deleted_refs, in wait_for_parent_move()
3764 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) in update_ref_path() argument
3777 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path); in update_ref_path()
3835 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref) in refresh_ref_path() argument
3845 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path); in refresh_ref_path()
3863 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move) in process_recorded_refs() argument
3865 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in process_recorded_refs()
3881 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino); in process_recorded_refs()
3887 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); in process_recorded_refs()
3907 if (!sctx->cur_inode_new) { in process_recorded_refs()
3908 ret = did_overwrite_first_ref(sctx, sctx->cur_ino, in process_recorded_refs()
3909 sctx->cur_inode_gen); in process_recorded_refs()
3915 if (sctx->cur_inode_new || did_overwrite) { in process_recorded_refs()
3916 ret = gen_unique_name(sctx, sctx->cur_ino, in process_recorded_refs()
3917 sctx->cur_inode_gen, valid_path); in process_recorded_refs()
3922 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
3966 list_for_each_entry(cur, &sctx->new_refs, list) { in process_recorded_refs()
3967 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
3979 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
3985 ret = is_first_ref(sctx->parent_root, in process_recorded_refs()
3995 ret = refresh_ref_path(sctx, cur); in process_recorded_refs()
4000 ret = orphanize_inode(sctx, ow_inode, ow_gen, in process_recorded_refs()
4013 if (is_waiting_for_move(sctx, ow_inode)) { in process_recorded_refs()
4014 wdm = get_waiting_dir_move(sctx, in process_recorded_refs()
4030 nce = name_cache_search(sctx, ow_inode, ow_gen); in process_recorded_refs()
4032 name_cache_delete(sctx, nce); in process_recorded_refs()
4043 ret = is_ancestor(sctx->parent_root, in process_recorded_refs()
4045 sctx->cur_ino, NULL); in process_recorded_refs()
4049 ret = get_cur_path(sctx, sctx->cur_ino, in process_recorded_refs()
4050 sctx->cur_inode_gen, in process_recorded_refs()
4063 ret = refresh_ref_path(sctx, cur); in process_recorded_refs()
4067 ret = send_unlink(sctx, cur->full_path); in process_recorded_refs()
4075 list_for_each_entry(cur, &sctx->new_refs, list) { in process_recorded_refs()
4083 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4092 list_for_each_entry(cur2, &sctx->new_refs, list) { in process_recorded_refs()
4106 ret = did_create_dir(sctx, cur->dir); in process_recorded_refs()
4110 ret = send_create_inode(sctx, cur->dir); in process_recorded_refs()
4116 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) { in process_recorded_refs()
4117 ret = wait_for_dest_dir_move(sctx, cur, is_orphan); in process_recorded_refs()
4126 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root && in process_recorded_refs()
4128 ret = wait_for_parent_move(sctx, cur, is_orphan); in process_recorded_refs()
4143 ret = send_rename(sctx, valid_path, cur->full_path); in process_recorded_refs()
4151 if (S_ISDIR(sctx->cur_inode_mode)) { in process_recorded_refs()
4157 ret = send_rename(sctx, valid_path, in process_recorded_refs()
4173 ret = update_ref_path(sctx, cur); in process_recorded_refs()
4177 ret = send_link(sctx, cur->full_path, in process_recorded_refs()
4188 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { in process_recorded_refs()
4195 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
4196 sctx->cur_ino); in process_recorded_refs()
4200 ret = send_rmdir(sctx, valid_path); in process_recorded_refs()
4204 ret = orphanize_inode(sctx, sctx->cur_ino, in process_recorded_refs()
4205 sctx->cur_inode_gen, valid_path); in process_recorded_refs()
4211 list_for_each_entry(cur, &sctx->deleted_refs, list) { in process_recorded_refs()
4216 } else if (S_ISDIR(sctx->cur_inode_mode) && in process_recorded_refs()
4217 !list_empty(&sctx->deleted_refs)) { in process_recorded_refs()
4221 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, in process_recorded_refs()
4226 } else if (!S_ISDIR(sctx->cur_inode_mode)) { in process_recorded_refs()
4232 list_for_each_entry(cur, &sctx->deleted_refs, list) { in process_recorded_refs()
4233 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
4234 sctx->cur_ino, sctx->cur_inode_gen, in process_recorded_refs()
4247 ret = update_ref_path(sctx, cur); in process_recorded_refs()
4251 ret = send_unlink(sctx, cur->full_path); in process_recorded_refs()
4268 ret = send_unlink(sctx, valid_path); in process_recorded_refs()
4286 if (cur->dir > sctx->cur_ino) in process_recorded_refs()
4289 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4296 ret = send_utimes(sctx, cur->dir, cur->dir_gen); in process_recorded_refs()
4301 ret = can_rmdir(sctx, cur->dir, cur->dir_gen, in process_recorded_refs()
4302 sctx->cur_ino); in process_recorded_refs()
4306 ret = get_cur_path(sctx, cur->dir, in process_recorded_refs()
4310 ret = send_rmdir(sctx, valid_path); in process_recorded_refs()
4322 free_recorded_refs(sctx); in process_recorded_refs()
4331 struct send_ctx *sctx = ctx; in record_ref() local
4344 ret = get_cur_path(sctx, dir, gen, p); in record_ref()
4363 struct send_ctx *sctx = ctx; in __record_new_ref() local
4364 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs); in __record_new_ref()
4372 struct send_ctx *sctx = ctx; in __record_deleted_ref() local
4373 return record_ref(sctx->parent_root, dir, name, ctx, in __record_deleted_ref()
4374 &sctx->deleted_refs); in __record_deleted_ref()
4377 static int record_new_ref(struct send_ctx *sctx) in record_new_ref() argument
4381 ret = iterate_inode_ref(sctx->send_root, sctx->left_path, in record_new_ref()
4382 sctx->cmp_key, 0, __record_new_ref, sctx); in record_new_ref()
4391 static int record_deleted_ref(struct send_ctx *sctx) in record_deleted_ref() argument
4395 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, in record_deleted_ref()
4396 sctx->cmp_key, 0, __record_deleted_ref, sctx); in record_deleted_ref()
4469 struct send_ctx *sctx = ctx; in __record_changed_new_ref() local
4471 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL, in __record_changed_new_ref()
4476 ret = find_iref(sctx->parent_root, sctx->right_path, in __record_changed_new_ref()
4477 sctx->cmp_key, dir, dir_gen, name); in __record_changed_new_ref()
4479 ret = __record_new_ref(num, dir, index, name, sctx); in __record_changed_new_ref()
4492 struct send_ctx *sctx = ctx; in __record_changed_deleted_ref() local
4494 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL, in __record_changed_deleted_ref()
4499 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key, in __record_changed_deleted_ref()
4502 ret = __record_deleted_ref(num, dir, index, name, sctx); in __record_changed_deleted_ref()
4509 static int record_changed_ref(struct send_ctx *sctx) in record_changed_ref() argument
4513 ret = iterate_inode_ref(sctx->send_root, sctx->left_path, in record_changed_ref()
4514 sctx->cmp_key, 0, __record_changed_new_ref, sctx); in record_changed_ref()
4517 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, in record_changed_ref()
4518 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx); in record_changed_ref()
4531 static int process_all_refs(struct send_ctx *sctx, in process_all_refs() argument
4549 root = sctx->send_root; in process_all_refs()
4552 root = sctx->parent_root; in process_all_refs()
4555 btrfs_err(sctx->send_root->fs_info, in process_all_refs()
4561 key.objectid = sctx->cmp_key->objectid; in process_all_refs()
4587 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx); in process_all_refs()
4600 ret = process_recorded_refs(sctx, &pending_move); in process_all_refs()
4606 static int send_set_xattr(struct send_ctx *sctx, in send_set_xattr() argument
4613 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); in send_set_xattr()
4617 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_set_xattr()
4618 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); in send_set_xattr()
4619 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); in send_set_xattr()
4621 ret = send_cmd(sctx); in send_set_xattr()
4628 static int send_remove_xattr(struct send_ctx *sctx, in send_remove_xattr() argument
4634 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); in send_remove_xattr()
4638 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); in send_remove_xattr()
4639 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); in send_remove_xattr()
4641 ret = send_cmd(sctx); in send_remove_xattr()
4654 struct send_ctx *sctx = ctx; in __process_new_xattr() local
4682 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in __process_new_xattr()
4686 ret = send_set_xattr(sctx, p, name, name_len, data, data_len); in __process_new_xattr()
4699 struct send_ctx *sctx = ctx; in __process_deleted_xattr() local
4706 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in __process_deleted_xattr()
4710 ret = send_remove_xattr(sctx, p, name, name_len); in __process_deleted_xattr()
4717 static int process_new_xattr(struct send_ctx *sctx) in process_new_xattr() argument
4721 ret = iterate_dir_item(sctx->send_root, sctx->left_path, in process_new_xattr()
4722 __process_new_xattr, sctx); in process_new_xattr()
4727 static int process_deleted_xattr(struct send_ctx *sctx) in process_deleted_xattr() argument
4729 return iterate_dir_item(sctx->parent_root, sctx->right_path, in process_deleted_xattr()
4730 __process_deleted_xattr, sctx); in process_deleted_xattr()
4797 struct send_ctx *sctx = ctx; in __process_changed_new_xattr() local
4801 ret = find_xattr(sctx->parent_root, sctx->right_path, in __process_changed_new_xattr()
4802 sctx->cmp_key, name, name_len, &found_data, in __process_changed_new_xattr()
4827 struct send_ctx *sctx = ctx; in __process_changed_deleted_xattr() local
4829 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key, in __process_changed_deleted_xattr()
4840 static int process_changed_xattr(struct send_ctx *sctx) in process_changed_xattr() argument
4844 ret = iterate_dir_item(sctx->send_root, sctx->left_path, in process_changed_xattr()
4845 __process_changed_new_xattr, sctx); in process_changed_xattr()
4848 ret = iterate_dir_item(sctx->parent_root, sctx->right_path, in process_changed_xattr()
4849 __process_changed_deleted_xattr, sctx); in process_changed_xattr()
4855 static int process_all_new_xattrs(struct send_ctx *sctx) in process_all_new_xattrs() argument
4869 root = sctx->send_root; in process_all_new_xattrs()
4871 key.objectid = sctx->cmp_key->objectid; in process_all_new_xattrs()
4899 ret = iterate_dir_item(root, path, __process_new_xattr, sctx); in process_all_new_xattrs()
4911 static inline u64 max_send_read_size(const struct send_ctx *sctx) in max_send_read_size() argument
4913 return sctx->send_max_size - SZ_16K; in max_send_read_size()
4916 static int put_data_header(struct send_ctx *sctx, u32 len) in put_data_header() argument
4920 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len) in put_data_header()
4922 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size); in put_data_header()
4925 sctx->send_size += sizeof(*hdr); in put_data_header()
4929 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len) in put_file_data() argument
4931 struct btrfs_root *root = sctx->send_root; in put_file_data()
4940 ret = put_data_header(sctx, len); in put_file_data()
4944 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root); in put_file_data()
4951 memset(&sctx->ra, 0, sizeof(struct file_ra_state)); in put_file_data()
4952 file_ra_state_init(&sctx->ra, inode->i_mapping); in put_file_data()
4960 page_cache_sync_readahead(inode->i_mapping, &sctx->ra, in put_file_data()
4972 page_cache_async_readahead(inode->i_mapping, &sctx->ra, in put_file_data()
4987 memcpy_from_page(sctx->send_buf + sctx->send_size, page, in put_file_data()
4994 sctx->send_size += cur_len; in put_file_data()
5004 static int send_write(struct send_ctx *sctx, u64 offset, u32 len) in send_write() argument
5006 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info; in send_write()
5016 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); in send_write()
5020 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_write()
5024 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_write()
5025 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_write()
5026 ret = put_file_data(sctx, offset, len); in send_write()
5030 ret = send_cmd(sctx); in send_write()
5041 static int send_clone(struct send_ctx *sctx, in send_clone() argument
5049 btrfs_debug(sctx->send_root->fs_info, in send_clone()
5058 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); in send_clone()
5062 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_clone()
5066 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_clone()
5067 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); in send_clone()
5068 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_clone()
5070 if (clone_root->root == sctx->send_root) { in send_clone()
5071 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL, in send_clone()
5075 ret = get_cur_path(sctx, clone_root->ino, gen, p); in send_clone()
5092 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_clone()
5095 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, in send_clone()
5097 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, in send_clone()
5099 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); in send_clone()
5100 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, in send_clone()
5103 ret = send_cmd(sctx); in send_clone()
5114 static int send_update_extent(struct send_ctx *sctx, in send_update_extent() argument
5124 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT); in send_update_extent()
5128 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_update_extent()
5132 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_update_extent()
5133 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_update_extent()
5134 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); in send_update_extent()
5136 ret = send_cmd(sctx); in send_update_extent()
5144 static int send_hole(struct send_ctx *sctx, u64 end) in send_hole() argument
5147 u64 read_size = max_send_read_size(sctx); in send_hole()
5148 u64 offset = sctx->cur_inode_last_extent; in send_hole()
5157 if (offset >= sctx->cur_inode_size) in send_hole()
5164 end = min_t(u64, end, sctx->cur_inode_size); in send_hole()
5166 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) in send_hole()
5167 return send_update_extent(sctx, offset, end - offset); in send_hole()
5172 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); in send_hole()
5178 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); in send_hole()
5181 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); in send_hole()
5182 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); in send_hole()
5183 ret = put_data_header(sctx, len); in send_hole()
5186 memset(sctx->send_buf + sctx->send_size, 0, len); in send_hole()
5187 sctx->send_size += len; in send_hole()
5188 ret = send_cmd(sctx); in send_hole()
5193 sctx->cur_inode_next_write_offset = offset; in send_hole()
5199 static int send_extent_data(struct send_ctx *sctx, in send_extent_data() argument
5203 u64 read_size = max_send_read_size(sctx); in send_extent_data()
5206 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) in send_extent_data()
5207 return send_update_extent(sctx, offset, len); in send_extent_data()
5213 ret = send_write(sctx, offset + sent, size); in send_extent_data()
5228 static int send_capabilities(struct send_ctx *sctx) in send_capabilities() argument
5243 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino, in send_capabilities()
5263 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath); in send_capabilities()
5270 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS, in send_capabilities()
5279 static int clone_range(struct send_ctx *sctx, in clone_range() argument
5307 len == sctx->send_root->fs_info->sectorsize) in clone_range()
5308 return send_extent_data(sctx, offset, len); in clone_range()
5405 ret = send_extent_data(sctx, offset, hole_len); in clone_range()
5467 offset + clone_len < sctx->cur_inode_size) { in clone_range()
5473 ret = send_clone(sctx, offset, slen, in clone_range()
5478 ret = send_extent_data(sctx, offset + slen, in clone_range()
5481 ret = send_clone(sctx, offset, clone_len, in clone_range()
5485 ret = send_extent_data(sctx, offset, clone_len); in clone_range()
5506 if (clone_root->root == sctx->send_root && in clone_range()
5507 clone_root->ino == sctx->cur_ino && in clone_range()
5508 clone_root->offset >= sctx->cur_inode_next_write_offset) in clone_range()
5517 ret = send_extent_data(sctx, offset, len); in clone_range()
5525 static int send_write_or_clone(struct send_ctx *sctx, in send_write_or_clone() argument
5533 u64 bs = sctx->send_root->fs_info->sb->s_blocksize; in send_write_or_clone()
5535 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size); in send_write_or_clone()
5548 ret = clone_range(sctx, clone_root, disk_byte, data_offset, in send_write_or_clone()
5551 ret = send_extent_data(sctx, offset, end - offset); in send_write_or_clone()
5553 sctx->cur_inode_next_write_offset = end; in send_write_or_clone()
5557 static int is_extent_unchanged(struct send_ctx *sctx, in is_extent_unchanged() argument
5622 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); in is_extent_unchanged()
5712 ret = btrfs_next_item(sctx->parent_root, path); in is_extent_unchanged()
5747 static int get_last_extent(struct send_ctx *sctx, u64 offset) in get_last_extent() argument
5750 struct btrfs_root *root = sctx->send_root; in get_last_extent()
5758 sctx->cur_inode_last_extent = 0; in get_last_extent()
5760 key.objectid = sctx->cur_ino; in get_last_extent()
5768 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY) in get_last_extent()
5771 sctx->cur_inode_last_extent = btrfs_file_extent_end(path); in get_last_extent()
5777 static int range_is_hole_in_parent(struct send_ctx *sctx, in range_is_hole_in_parent() argument
5783 struct btrfs_root *root = sctx->parent_root; in range_is_hole_in_parent()
5791 key.objectid = sctx->cur_ino; in range_is_hole_in_parent()
5816 if (key.objectid < sctx->cur_ino || in range_is_hole_in_parent()
5819 if (key.objectid > sctx->cur_ino || in range_is_hole_in_parent()
5843 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path, in maybe_send_hole() argument
5848 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx)) in maybe_send_hole()
5851 if (sctx->cur_inode_last_extent == (u64)-1) { in maybe_send_hole()
5852 ret = get_last_extent(sctx, key->offset - 1); in maybe_send_hole()
5858 sctx->cur_inode_last_extent < key->offset) { in maybe_send_hole()
5866 ret = get_last_extent(sctx, key->offset - 1); in maybe_send_hole()
5871 if (sctx->cur_inode_last_extent < key->offset) { in maybe_send_hole()
5872 ret = range_is_hole_in_parent(sctx, in maybe_send_hole()
5873 sctx->cur_inode_last_extent, in maybe_send_hole()
5878 ret = send_hole(sctx, key->offset); in maybe_send_hole()
5882 sctx->cur_inode_last_extent = btrfs_file_extent_end(path); in maybe_send_hole()
5886 static int process_extent(struct send_ctx *sctx, in process_extent() argument
5893 if (S_ISLNK(sctx->cur_inode_mode)) in process_extent()
5896 if (sctx->parent_root && !sctx->cur_inode_new) { in process_extent()
5897 ret = is_extent_unchanged(sctx, path, key); in process_extent()
5932 ret = find_extent_clone(sctx, path, key->objectid, key->offset, in process_extent()
5933 sctx->cur_inode_size, &found_clone); in process_extent()
5937 ret = send_write_or_clone(sctx, path, key, found_clone); in process_extent()
5941 ret = maybe_send_hole(sctx, path, key); in process_extent()
5946 static int process_all_extents(struct send_ctx *sctx) in process_all_extents() argument
5956 root = sctx->send_root; in process_all_extents()
5961 key.objectid = sctx->cmp_key->objectid; in process_all_extents()
5991 ret = process_extent(sctx, path, &found_key); in process_all_extents()
6003 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end, in process_recorded_refs_if_needed() argument
6009 if (sctx->cur_ino == 0) in process_recorded_refs_if_needed()
6011 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && in process_recorded_refs_if_needed()
6012 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY) in process_recorded_refs_if_needed()
6014 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) in process_recorded_refs_if_needed()
6017 ret = process_recorded_refs(sctx, pending_move); in process_recorded_refs_if_needed()
6026 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) in finish_inode_if_needed() argument
6041 if (sctx->ignore_cur_inode) in finish_inode_if_needed()
6044 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move, in finish_inode_if_needed()
6062 sctx->send_progress = sctx->cur_ino + 1; in finish_inode_if_needed()
6064 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) in finish_inode_if_needed()
6066 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) in finish_inode_if_needed()
6069 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL, in finish_inode_if_needed()
6074 if (!sctx->parent_root || sctx->cur_inode_new) { in finish_inode_if_needed()
6076 if (!S_ISLNK(sctx->cur_inode_mode)) in finish_inode_if_needed()
6078 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size) in finish_inode_if_needed()
6083 ret = get_inode_info(sctx->parent_root, sctx->cur_ino, in finish_inode_if_needed()
6091 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode) in finish_inode_if_needed()
6093 if ((old_size == sctx->cur_inode_size) || in finish_inode_if_needed()
6094 (sctx->cur_inode_size > old_size && in finish_inode_if_needed()
6095 sctx->cur_inode_next_write_offset == sctx->cur_inode_size)) in finish_inode_if_needed()
6099 if (S_ISREG(sctx->cur_inode_mode)) { in finish_inode_if_needed()
6100 if (need_send_hole(sctx)) { in finish_inode_if_needed()
6101 if (sctx->cur_inode_last_extent == (u64)-1 || in finish_inode_if_needed()
6102 sctx->cur_inode_last_extent < in finish_inode_if_needed()
6103 sctx->cur_inode_size) { in finish_inode_if_needed()
6104 ret = get_last_extent(sctx, (u64)-1); in finish_inode_if_needed()
6108 if (sctx->cur_inode_last_extent < in finish_inode_if_needed()
6109 sctx->cur_inode_size) { in finish_inode_if_needed()
6110 ret = send_hole(sctx, sctx->cur_inode_size); in finish_inode_if_needed()
6116 ret = send_truncate(sctx, sctx->cur_ino, in finish_inode_if_needed()
6117 sctx->cur_inode_gen, in finish_inode_if_needed()
6118 sctx->cur_inode_size); in finish_inode_if_needed()
6125 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, in finish_inode_if_needed()
6131 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, in finish_inode_if_needed()
6137 ret = send_capabilities(sctx); in finish_inode_if_needed()
6145 if (!is_waiting_for_move(sctx, sctx->cur_ino)) { in finish_inode_if_needed()
6146 ret = apply_children_dir_moves(sctx); in finish_inode_if_needed()
6156 sctx->send_progress = sctx->cur_ino + 1; in finish_inode_if_needed()
6157 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); in finish_inode_if_needed()
6168 struct send_ctx *sctx; member
6176 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx, in record_parent_ref()
6184 static int btrfs_unlink_all_paths(struct send_ctx *sctx) in btrfs_unlink_all_paths() argument
6196 key.objectid = sctx->cur_ino; in btrfs_unlink_all_paths()
6199 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0); in btrfs_unlink_all_paths()
6204 ctx.sctx = sctx; in btrfs_unlink_all_paths()
6211 ret = btrfs_next_leaf(sctx->parent_root, path); in btrfs_unlink_all_paths()
6220 if (key.objectid != sctx->cur_ino) in btrfs_unlink_all_paths()
6226 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1, in btrfs_unlink_all_paths()
6238 ret = send_unlink(sctx, ref->full_path); in btrfs_unlink_all_paths()
6253 static int changed_inode(struct send_ctx *sctx, in changed_inode() argument
6257 struct btrfs_key *key = sctx->cmp_key; in changed_inode()
6263 sctx->cur_ino = key->objectid; in changed_inode()
6264 sctx->cur_inode_new_gen = 0; in changed_inode()
6265 sctx->cur_inode_last_extent = (u64)-1; in changed_inode()
6266 sctx->cur_inode_next_write_offset = 0; in changed_inode()
6267 sctx->ignore_cur_inode = false; in changed_inode()
6274 sctx->send_progress = sctx->cur_ino; in changed_inode()
6278 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], in changed_inode()
6279 sctx->left_path->slots[0], in changed_inode()
6281 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], in changed_inode()
6284 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], in changed_inode()
6285 sctx->right_path->slots[0], in changed_inode()
6287 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], in changed_inode()
6291 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], in changed_inode()
6292 sctx->right_path->slots[0], in changed_inode()
6295 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], in changed_inode()
6304 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) in changed_inode()
6305 sctx->cur_inode_new_gen = 1; in changed_inode()
6326 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii); in changed_inode()
6328 sctx->ignore_cur_inode = true; in changed_inode()
6330 ret = btrfs_unlink_all_paths(sctx); in changed_inode()
6336 sctx->cur_inode_gen = left_gen; in changed_inode()
6337 sctx->cur_inode_new = 1; in changed_inode()
6338 sctx->cur_inode_deleted = 0; in changed_inode()
6339 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6340 sctx->left_path->nodes[0], left_ii); in changed_inode()
6341 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6342 sctx->left_path->nodes[0], left_ii); in changed_inode()
6343 sctx->cur_inode_rdev = btrfs_inode_rdev( in changed_inode()
6344 sctx->left_path->nodes[0], left_ii); in changed_inode()
6345 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) in changed_inode()
6346 ret = send_create_inode_if_needed(sctx); in changed_inode()
6348 sctx->cur_inode_gen = right_gen; in changed_inode()
6349 sctx->cur_inode_new = 0; in changed_inode()
6350 sctx->cur_inode_deleted = 1; in changed_inode()
6351 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6352 sctx->right_path->nodes[0], right_ii); in changed_inode()
6353 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6354 sctx->right_path->nodes[0], right_ii); in changed_inode()
6363 if (sctx->cur_inode_new_gen) { in changed_inode()
6367 sctx->cur_inode_gen = right_gen; in changed_inode()
6368 sctx->cur_inode_new = 0; in changed_inode()
6369 sctx->cur_inode_deleted = 1; in changed_inode()
6370 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6371 sctx->right_path->nodes[0], right_ii); in changed_inode()
6372 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6373 sctx->right_path->nodes[0], right_ii); in changed_inode()
6374 ret = process_all_refs(sctx, in changed_inode()
6382 sctx->cur_inode_gen = left_gen; in changed_inode()
6383 sctx->cur_inode_new = 1; in changed_inode()
6384 sctx->cur_inode_deleted = 0; in changed_inode()
6385 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6386 sctx->left_path->nodes[0], left_ii); in changed_inode()
6387 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6388 sctx->left_path->nodes[0], left_ii); in changed_inode()
6389 sctx->cur_inode_rdev = btrfs_inode_rdev( in changed_inode()
6390 sctx->left_path->nodes[0], left_ii); in changed_inode()
6391 ret = send_create_inode_if_needed(sctx); in changed_inode()
6395 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); in changed_inode()
6402 sctx->send_progress = sctx->cur_ino + 1; in changed_inode()
6408 ret = process_all_extents(sctx); in changed_inode()
6411 ret = process_all_new_xattrs(sctx); in changed_inode()
6415 sctx->cur_inode_gen = left_gen; in changed_inode()
6416 sctx->cur_inode_new = 0; in changed_inode()
6417 sctx->cur_inode_new_gen = 0; in changed_inode()
6418 sctx->cur_inode_deleted = 0; in changed_inode()
6419 sctx->cur_inode_size = btrfs_inode_size( in changed_inode()
6420 sctx->left_path->nodes[0], left_ii); in changed_inode()
6421 sctx->cur_inode_mode = btrfs_inode_mode( in changed_inode()
6422 sctx->left_path->nodes[0], left_ii); in changed_inode()
6440 static int changed_ref(struct send_ctx *sctx, in changed_ref() argument
6445 if (sctx->cur_ino != sctx->cmp_key->objectid) { in changed_ref()
6446 inconsistent_snapshot_error(sctx, result, "reference"); in changed_ref()
6450 if (!sctx->cur_inode_new_gen && in changed_ref()
6451 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { in changed_ref()
6453 ret = record_new_ref(sctx); in changed_ref()
6455 ret = record_deleted_ref(sctx); in changed_ref()
6457 ret = record_changed_ref(sctx); in changed_ref()
6468 static int changed_xattr(struct send_ctx *sctx, in changed_xattr() argument
6473 if (sctx->cur_ino != sctx->cmp_key->objectid) { in changed_xattr()
6474 inconsistent_snapshot_error(sctx, result, "xattr"); in changed_xattr()
6478 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { in changed_xattr()
6480 ret = process_new_xattr(sctx); in changed_xattr()
6482 ret = process_deleted_xattr(sctx); in changed_xattr()
6484 ret = process_changed_xattr(sctx); in changed_xattr()
6495 static int changed_extent(struct send_ctx *sctx, in changed_extent() argument
6513 if (sctx->cur_ino != sctx->cmp_key->objectid) in changed_extent()
6516 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { in changed_extent()
6518 ret = process_extent(sctx, sctx->left_path, in changed_extent()
6519 sctx->cmp_key); in changed_extent()
6525 static int dir_changed(struct send_ctx *sctx, u64 dir) in dir_changed() argument
6530 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL, in dir_changed()
6535 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL, in dir_changed()
6543 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path, in compare_refs() argument
6559 ret = dir_changed(sctx, dirid); in compare_refs()
6574 ret = dir_changed(sctx, dirid); in compare_refs()
6591 struct send_ctx *sctx) in changed_cb() argument
6598 ret = compare_refs(sctx, left_path, key); in changed_cb()
6604 return maybe_send_hole(sctx, left_path, key); in changed_cb()
6612 sctx->left_path = left_path; in changed_cb()
6613 sctx->right_path = right_path; in changed_cb()
6614 sctx->cmp_key = key; in changed_cb()
6616 ret = finish_inode_if_needed(sctx, 0); in changed_cb()
6626 ret = changed_inode(sctx, result); in changed_cb()
6627 } else if (!sctx->ignore_cur_inode) { in changed_cb()
6630 ret = changed_ref(sctx, result); in changed_cb()
6632 ret = changed_xattr(sctx, result); in changed_cb()
6634 ret = changed_extent(sctx, result); in changed_cb()
6641 static int full_send_tree(struct send_ctx *sctx) in full_send_tree() argument
6644 struct btrfs_root *send_root = sctx->send_root; in full_send_tree()
6671 BTRFS_COMPARE_TREE_NEW, sctx); in full_send_tree()
6685 ret = finish_inode_if_needed(sctx, 1); in full_send_tree()
6820 struct btrfs_root *right_root, struct send_ctx *sctx) in btrfs_compare_trees() argument
6981 sctx); in btrfs_compare_trees()
6992 sctx); in btrfs_compare_trees()
7006 sctx); in btrfs_compare_trees()
7014 sctx); in btrfs_compare_trees()
7029 &left_key, result, sctx); in btrfs_compare_trees()
7081 static int send_subvol(struct send_ctx *sctx) in send_subvol() argument
7085 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) { in send_subvol()
7086 ret = send_header(sctx); in send_subvol()
7091 ret = send_subvol_begin(sctx); in send_subvol()
7095 if (sctx->parent_root) { in send_subvol()
7096 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx); in send_subvol()
7099 ret = finish_inode_if_needed(sctx, 1); in send_subvol()
7103 ret = full_send_tree(sctx); in send_subvol()
7109 free_recorded_refs(sctx); in send_subvol()
7126 static int ensure_commit_roots_uptodate(struct send_ctx *sctx) in ensure_commit_roots_uptodate() argument
7132 if (sctx->parent_root && in ensure_commit_roots_uptodate()
7133 sctx->parent_root->node != sctx->parent_root->commit_root) in ensure_commit_roots_uptodate()
7136 for (i = 0; i < sctx->clone_roots_cnt; i++) in ensure_commit_roots_uptodate()
7137 if (sctx->clone_roots[i].root->node != in ensure_commit_roots_uptodate()
7138 sctx->clone_roots[i].root->commit_root) in ensure_commit_roots_uptodate()
7149 trans = btrfs_join_transaction(sctx->send_root); in ensure_commit_roots_uptodate()
7166 static int flush_delalloc_roots(struct send_ctx *sctx) in flush_delalloc_roots() argument
7168 struct btrfs_root *root = sctx->parent_root; in flush_delalloc_roots()
7179 for (i = 0; i < sctx->clone_roots_cnt; i++) { in flush_delalloc_roots()
7180 root = sctx->clone_roots[i].root; in flush_delalloc_roots()
7218 struct send_ctx *sctx = NULL; in btrfs_ioctl_send() local
7266 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL); in btrfs_ioctl_send()
7267 if (!sctx) { in btrfs_ioctl_send()
7272 INIT_LIST_HEAD(&sctx->new_refs); in btrfs_ioctl_send()
7273 INIT_LIST_HEAD(&sctx->deleted_refs); in btrfs_ioctl_send()
7274 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL); in btrfs_ioctl_send()
7275 INIT_LIST_HEAD(&sctx->name_cache_list); in btrfs_ioctl_send()
7277 sctx->flags = arg->flags; in btrfs_ioctl_send()
7279 sctx->send_filp = fget(arg->send_fd); in btrfs_ioctl_send()
7280 if (!sctx->send_filp) { in btrfs_ioctl_send()
7285 sctx->send_root = send_root; in btrfs_ioctl_send()
7290 if (btrfs_root_dead(sctx->send_root)) { in btrfs_ioctl_send()
7295 sctx->clone_roots_cnt = arg->clone_sources_count; in btrfs_ioctl_send()
7297 sctx->send_max_size = BTRFS_SEND_BUF_SIZE; in btrfs_ioctl_send()
7298 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL); in btrfs_ioctl_send()
7299 if (!sctx->send_buf) { in btrfs_ioctl_send()
7304 sctx->pending_dir_moves = RB_ROOT; in btrfs_ioctl_send()
7305 sctx->waiting_dir_moves = RB_ROOT; in btrfs_ioctl_send()
7306 sctx->orphan_dirs = RB_ROOT; in btrfs_ioctl_send()
7308 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots), in btrfs_ioctl_send()
7311 if (!sctx->clone_roots) { in btrfs_ioctl_send()
7358 sctx->clone_roots[i].root = clone_root; in btrfs_ioctl_send()
7366 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root, in btrfs_ioctl_send()
7368 if (IS_ERR(sctx->parent_root)) { in btrfs_ioctl_send()
7369 ret = PTR_ERR(sctx->parent_root); in btrfs_ioctl_send()
7373 spin_lock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
7374 sctx->parent_root->send_in_progress++; in btrfs_ioctl_send()
7375 if (!btrfs_root_readonly(sctx->parent_root) || in btrfs_ioctl_send()
7376 btrfs_root_dead(sctx->parent_root)) { in btrfs_ioctl_send()
7377 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
7381 if (sctx->parent_root->dedupe_in_progress) { in btrfs_ioctl_send()
7382 dedupe_in_progress_warn(sctx->parent_root); in btrfs_ioctl_send()
7383 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
7387 spin_unlock(&sctx->parent_root->root_item_lock); in btrfs_ioctl_send()
7395 sctx->clone_roots[sctx->clone_roots_cnt++].root = in btrfs_ioctl_send()
7396 btrfs_grab_root(sctx->send_root); in btrfs_ioctl_send()
7399 sort(sctx->clone_roots, sctx->clone_roots_cnt, in btrfs_ioctl_send()
7400 sizeof(*sctx->clone_roots), __clone_root_cmp_sort, in btrfs_ioctl_send()
7404 ret = flush_delalloc_roots(sctx); in btrfs_ioctl_send()
7408 ret = ensure_commit_roots_uptodate(sctx); in btrfs_ioctl_send()
7423 ret = send_subvol(sctx); in btrfs_ioctl_send()
7430 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) { in btrfs_ioctl_send()
7431 ret = begin_cmd(sctx, BTRFS_SEND_C_END); in btrfs_ioctl_send()
7434 ret = send_cmd(sctx); in btrfs_ioctl_send()
7440 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)); in btrfs_ioctl_send()
7441 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) { in btrfs_ioctl_send()
7445 n = rb_first(&sctx->pending_dir_moves); in btrfs_ioctl_send()
7452 free_pending_move(sctx, pm2); in btrfs_ioctl_send()
7454 free_pending_move(sctx, pm); in btrfs_ioctl_send()
7457 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)); in btrfs_ioctl_send()
7458 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) { in btrfs_ioctl_send()
7462 n = rb_first(&sctx->waiting_dir_moves); in btrfs_ioctl_send()
7464 rb_erase(&dm->node, &sctx->waiting_dir_moves); in btrfs_ioctl_send()
7468 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs)); in btrfs_ioctl_send()
7469 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) { in btrfs_ioctl_send()
7473 n = rb_first(&sctx->orphan_dirs); in btrfs_ioctl_send()
7475 free_orphan_dir_info(sctx, odi); in btrfs_ioctl_send()
7479 for (i = 0; i < sctx->clone_roots_cnt; i++) { in btrfs_ioctl_send()
7481 sctx->clone_roots[i].root); in btrfs_ioctl_send()
7482 btrfs_put_root(sctx->clone_roots[i].root); in btrfs_ioctl_send()
7485 for (i = 0; sctx && i < clone_sources_to_rollback; i++) { in btrfs_ioctl_send()
7487 sctx->clone_roots[i].root); in btrfs_ioctl_send()
7488 btrfs_put_root(sctx->clone_roots[i].root); in btrfs_ioctl_send()
7493 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) { in btrfs_ioctl_send()
7494 btrfs_root_dec_send_in_progress(sctx->parent_root); in btrfs_ioctl_send()
7495 btrfs_put_root(sctx->parent_root); in btrfs_ioctl_send()
7500 if (sctx) { in btrfs_ioctl_send()
7501 if (sctx->send_filp) in btrfs_ioctl_send()
7502 fput(sctx->send_filp); in btrfs_ioctl_send()
7504 kvfree(sctx->clone_roots); in btrfs_ioctl_send()
7505 kvfree(sctx->send_buf); in btrfs_ioctl_send()
7507 name_cache_free(sctx); in btrfs_ioctl_send()
7509 kfree(sctx); in btrfs_ioctl_send()