1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012 Alexander Block. All rights reserved.
6 #include <linux/bsearch.h>
8 #include <linux/file.h>
9 #include <linux/sort.h>
10 #include <linux/mount.h>
11 #include <linux/xattr.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/radix-tree.h>
14 #include <linux/vmalloc.h>
15 #include <linux/string.h>
16 #include <linux/compat.h>
17 #include <linux/crc32c.h>
23 #include "btrfs_inode.h"
24 #include "transaction.h"
25 #include "compression.h"
29 * Maximum number of references an extent can have in order for us to attempt to
30 * issue clone operations instead of write operations. This currently exists to
31 * avoid hitting limitations of the backreference walking code (taking a lot of
32 * time and using too much memory for extents with large number of references).
34 #define SEND_MAX_EXTENT_REFS 64
37 * A fs_path is a helper to dynamically build path names with unknown size.
38 * It reallocates the internal buffer on demand.
39 * It allows fast adding of path elements on the right side (normal path) and
40 * fast adding to the left side (reversed path). A reversed path can also be
41 * unreversed if needed.
50 unsigned short buf_len:15;
51 unsigned short reversed:1;
55 * Average path length does not exceed 200 bytes, we'll have
56 * better packing in the slab and higher chance to satisfy
57 * a allocation later during send.
62 #define FS_PATH_INLINE_SIZE \
63 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
66 /* reused for each extent */
68 struct btrfs_root *root;
75 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
76 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
79 struct file *send_filp;
85 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
86 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
88 struct btrfs_root *send_root;
89 struct btrfs_root *parent_root;
90 struct clone_root *clone_roots;
93 /* current state of the compare_tree call */
94 struct btrfs_path *left_path;
95 struct btrfs_path *right_path;
96 struct btrfs_key *cmp_key;
99 * infos of the currently processed inode. In case of deleted inodes,
100 * these are the values from the deleted inode.
105 int cur_inode_new_gen;
106 int cur_inode_deleted;
110 u64 cur_inode_last_extent;
111 u64 cur_inode_next_write_offset;
112 bool ignore_cur_inode;
116 struct list_head new_refs;
117 struct list_head deleted_refs;
119 struct radix_tree_root name_cache;
120 struct list_head name_cache_list;
123 struct file_ra_state ra;
128 * We process inodes by their increasing order, so if before an
129 * incremental send we reverse the parent/child relationship of
130 * directories such that a directory with a lower inode number was
131 * the parent of a directory with a higher inode number, and the one
132 * becoming the new parent got renamed too, we can't rename/move the
133 * directory with lower inode number when we finish processing it - we
134 * must process the directory with higher inode number first, then
135 * rename/move it and then rename/move the directory with lower inode
136 * number. Example follows.
138 * Tree state when the first send was performed:
150 * Tree state when the second (incremental) send is performed:
159 * The sequence of steps that lead to the second state was:
161 * mv /a/b/c/d /a/b/c2/d2
162 * mv /a/b/c /a/b/c2/d2/cc
164 * "c" has lower inode number, but we can't move it (2nd mv operation)
165 * before we move "d", which has higher inode number.
167 * So we just memorize which move/rename operations must be performed
168 * later when their respective parent is processed and moved/renamed.
171 /* Indexed by parent directory inode number. */
172 struct rb_root pending_dir_moves;
175 * Reverse index, indexed by the inode number of a directory that
176 * is waiting for the move/rename of its immediate parent before its
177 * own move/rename can be performed.
179 struct rb_root waiting_dir_moves;
182 * A directory that is going to be rm'ed might have a child directory
183 * which is in the pending directory moves index above. In this case,
184 * the directory can only be removed after the move/rename of its child
185 * is performed. Example:
205 * Sequence of steps that lead to the send snapshot:
206 * rm -f /a/b/c/foo.txt
208 * mv /a/b/c/x /a/b/YY
211 * When the child is processed, its move/rename is delayed until its
212 * parent is processed (as explained above), but all other operations
213 * like update utimes, chown, chgrp, etc, are performed and the paths
214 * that it uses for those operations must use the orphanized name of
215 * its parent (the directory we're going to rm later), so we need to
216 * memorize that name.
218 * Indexed by the inode number of the directory to be deleted.
220 struct rb_root orphan_dirs;
223 struct pending_dir_move {
225 struct list_head list;
229 struct list_head update_refs;
232 struct waiting_dir_move {
236 * There might be some directory that could not be removed because it
237 * was waiting for this directory inode to be moved first. Therefore
238 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
245 struct orphan_dir_info {
249 u64 last_dir_index_offset;
252 struct name_cache_entry {
253 struct list_head list;
255 * radix_tree has only 32bit entries but we need to handle 64bit inums.
256 * We use the lower 32bit of the 64bit inum to store it in the tree. If
257 * more then one inum would fall into the same entry, we use radix_list
258 * to store the additional entries. radix_list is also used to store
259 * entries where two entries have the same inum but different
262 struct list_head radix_list;
268 int need_later_update;
274 static void inconsistent_snapshot_error(struct send_ctx *sctx,
275 enum btrfs_compare_tree_result result,
278 const char *result_string;
281 case BTRFS_COMPARE_TREE_NEW:
282 result_string = "new";
284 case BTRFS_COMPARE_TREE_DELETED:
285 result_string = "deleted";
287 case BTRFS_COMPARE_TREE_CHANGED:
288 result_string = "updated";
290 case BTRFS_COMPARE_TREE_SAME:
292 result_string = "unchanged";
296 result_string = "unexpected";
299 btrfs_err(sctx->send_root->fs_info,
300 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
301 result_string, what, sctx->cmp_key->objectid,
302 sctx->send_root->root_key.objectid,
304 sctx->parent_root->root_key.objectid : 0));
307 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
309 static struct waiting_dir_move *
310 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
312 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
314 static int need_send_hole(struct send_ctx *sctx)
316 return (sctx->parent_root && !sctx->cur_inode_new &&
317 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
318 S_ISREG(sctx->cur_inode_mode));
321 static void fs_path_reset(struct fs_path *p)
324 p->start = p->buf + p->buf_len - 1;
334 static struct fs_path *fs_path_alloc(void)
338 p = kmalloc(sizeof(*p), GFP_KERNEL);
342 p->buf = p->inline_buf;
343 p->buf_len = FS_PATH_INLINE_SIZE;
348 static struct fs_path *fs_path_alloc_reversed(void)
360 static void fs_path_free(struct fs_path *p)
364 if (p->buf != p->inline_buf)
369 static int fs_path_len(struct fs_path *p)
371 return p->end - p->start;
374 static int fs_path_ensure_buf(struct fs_path *p, int len)
382 if (p->buf_len >= len)
385 if (len > PATH_MAX) {
390 path_len = p->end - p->start;
391 old_buf_len = p->buf_len;
394 * First time the inline_buf does not suffice
396 if (p->buf == p->inline_buf) {
397 tmp_buf = kmalloc(len, GFP_KERNEL);
399 memcpy(tmp_buf, p->buf, old_buf_len);
401 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
407 * The real size of the buffer is bigger, this will let the fast path
408 * happen most of the time
410 p->buf_len = ksize(p->buf);
413 tmp_buf = p->buf + old_buf_len - path_len - 1;
414 p->end = p->buf + p->buf_len - 1;
415 p->start = p->end - path_len;
416 memmove(p->start, tmp_buf, path_len + 1);
419 p->end = p->start + path_len;
424 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
430 new_len = p->end - p->start + name_len;
431 if (p->start != p->end)
433 ret = fs_path_ensure_buf(p, new_len);
438 if (p->start != p->end)
440 p->start -= name_len;
441 *prepared = p->start;
443 if (p->start != p->end)
454 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
459 ret = fs_path_prepare_for_add(p, name_len, &prepared);
462 memcpy(prepared, name, name_len);
468 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
473 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
476 memcpy(prepared, p2->start, p2->end - p2->start);
482 static int fs_path_add_from_extent_buffer(struct fs_path *p,
483 struct extent_buffer *eb,
484 unsigned long off, int len)
489 ret = fs_path_prepare_for_add(p, len, &prepared);
493 read_extent_buffer(eb, prepared, off, len);
499 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
503 p->reversed = from->reversed;
506 ret = fs_path_add_path(p, from);
512 static void fs_path_unreverse(struct fs_path *p)
521 len = p->end - p->start;
523 p->end = p->start + len;
524 memmove(p->start, tmp, len + 1);
528 static struct btrfs_path *alloc_path_for_send(void)
530 struct btrfs_path *path;
532 path = btrfs_alloc_path();
535 path->search_commit_root = 1;
536 path->skip_locking = 1;
537 path->need_commit_sem = 1;
541 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
547 ret = kernel_write(filp, buf + pos, len - pos, off);
548 /* TODO handle that correctly */
549 /*if (ret == -ERESTARTSYS) {
563 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
565 struct btrfs_tlv_header *hdr;
566 int total_len = sizeof(*hdr) + len;
567 int left = sctx->send_max_size - sctx->send_size;
569 if (unlikely(left < total_len))
572 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
573 hdr->tlv_type = cpu_to_le16(attr);
574 hdr->tlv_len = cpu_to_le16(len);
575 memcpy(hdr + 1, data, len);
576 sctx->send_size += total_len;
581 #define TLV_PUT_DEFINE_INT(bits) \
582 static int tlv_put_u##bits(struct send_ctx *sctx, \
583 u##bits attr, u##bits value) \
585 __le##bits __tmp = cpu_to_le##bits(value); \
586 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
589 TLV_PUT_DEFINE_INT(64)
591 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
592 const char *str, int len)
596 return tlv_put(sctx, attr, str, len);
599 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
602 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
605 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
606 struct extent_buffer *eb,
607 struct btrfs_timespec *ts)
609 struct btrfs_timespec bts;
610 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
611 return tlv_put(sctx, attr, &bts, sizeof(bts));
615 #define TLV_PUT(sctx, attrtype, data, attrlen) \
617 ret = tlv_put(sctx, attrtype, data, attrlen); \
619 goto tlv_put_failure; \
622 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
624 ret = tlv_put_u##bits(sctx, attrtype, value); \
626 goto tlv_put_failure; \
629 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
630 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
631 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
632 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
633 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
635 ret = tlv_put_string(sctx, attrtype, str, len); \
637 goto tlv_put_failure; \
639 #define TLV_PUT_PATH(sctx, attrtype, p) \
641 ret = tlv_put_string(sctx, attrtype, p->start, \
642 p->end - p->start); \
644 goto tlv_put_failure; \
646 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
648 ret = tlv_put_uuid(sctx, attrtype, uuid); \
650 goto tlv_put_failure; \
652 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
654 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
656 goto tlv_put_failure; \
659 static int send_header(struct send_ctx *sctx)
661 struct btrfs_stream_header hdr;
663 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
664 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
666 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
671 * For each command/item we want to send to userspace, we call this function.
673 static int begin_cmd(struct send_ctx *sctx, int cmd)
675 struct btrfs_cmd_header *hdr;
677 if (WARN_ON(!sctx->send_buf))
680 BUG_ON(sctx->send_size);
682 sctx->send_size += sizeof(*hdr);
683 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
684 hdr->cmd = cpu_to_le16(cmd);
689 static int send_cmd(struct send_ctx *sctx)
692 struct btrfs_cmd_header *hdr;
695 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
696 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
699 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
700 hdr->crc = cpu_to_le32(crc);
702 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
705 sctx->total_send_size += sctx->send_size;
706 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
713 * Sends a move instruction to user space
715 static int send_rename(struct send_ctx *sctx,
716 struct fs_path *from, struct fs_path *to)
718 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
721 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
723 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
727 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
728 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
730 ret = send_cmd(sctx);
738 * Sends a link instruction to user space
740 static int send_link(struct send_ctx *sctx,
741 struct fs_path *path, struct fs_path *lnk)
743 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
746 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
748 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
752 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
753 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
755 ret = send_cmd(sctx);
763 * Sends an unlink instruction to user space
765 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
767 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
770 btrfs_debug(fs_info, "send_unlink %s", path->start);
772 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
776 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
778 ret = send_cmd(sctx);
786 * Sends a rmdir instruction to user space
788 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
790 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
793 btrfs_debug(fs_info, "send_rmdir %s", path->start);
795 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
799 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
801 ret = send_cmd(sctx);
809 * Helper function to retrieve some fields from an inode item.
811 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
812 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
816 struct btrfs_inode_item *ii;
817 struct btrfs_key key;
820 key.type = BTRFS_INODE_ITEM_KEY;
822 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
829 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
830 struct btrfs_inode_item);
832 *size = btrfs_inode_size(path->nodes[0], ii);
834 *gen = btrfs_inode_generation(path->nodes[0], ii);
836 *mode = btrfs_inode_mode(path->nodes[0], ii);
838 *uid = btrfs_inode_uid(path->nodes[0], ii);
840 *gid = btrfs_inode_gid(path->nodes[0], ii);
842 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
847 static int get_inode_info(struct btrfs_root *root,
848 u64 ino, u64 *size, u64 *gen,
849 u64 *mode, u64 *uid, u64 *gid,
852 struct btrfs_path *path;
855 path = alloc_path_for_send();
858 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
860 btrfs_free_path(path);
864 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
869 * Helper function to iterate the entries in ONE btrfs_inode_ref or
870 * btrfs_inode_extref.
871 * The iterate callback may return a non zero value to stop iteration. This can
872 * be a negative value for error codes or 1 to simply stop it.
874 * path must point to the INODE_REF or INODE_EXTREF when called.
876 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
877 struct btrfs_key *found_key, int resolve,
878 iterate_inode_ref_t iterate, void *ctx)
880 struct extent_buffer *eb = path->nodes[0];
881 struct btrfs_item *item;
882 struct btrfs_inode_ref *iref;
883 struct btrfs_inode_extref *extref;
884 struct btrfs_path *tmp_path;
888 int slot = path->slots[0];
895 unsigned long name_off;
896 unsigned long elem_size;
899 p = fs_path_alloc_reversed();
903 tmp_path = alloc_path_for_send();
910 if (found_key->type == BTRFS_INODE_REF_KEY) {
911 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
912 struct btrfs_inode_ref);
913 item = btrfs_item_nr(slot);
914 total = btrfs_item_size(eb, item);
915 elem_size = sizeof(*iref);
917 ptr = btrfs_item_ptr_offset(eb, slot);
918 total = btrfs_item_size_nr(eb, slot);
919 elem_size = sizeof(*extref);
922 while (cur < total) {
925 if (found_key->type == BTRFS_INODE_REF_KEY) {
926 iref = (struct btrfs_inode_ref *)(ptr + cur);
927 name_len = btrfs_inode_ref_name_len(eb, iref);
928 name_off = (unsigned long)(iref + 1);
929 index = btrfs_inode_ref_index(eb, iref);
930 dir = found_key->offset;
932 extref = (struct btrfs_inode_extref *)(ptr + cur);
933 name_len = btrfs_inode_extref_name_len(eb, extref);
934 name_off = (unsigned long)&extref->name;
935 index = btrfs_inode_extref_index(eb, extref);
936 dir = btrfs_inode_extref_parent(eb, extref);
940 start = btrfs_ref_to_path(root, tmp_path, name_len,
944 ret = PTR_ERR(start);
947 if (start < p->buf) {
948 /* overflow , try again with larger buffer */
949 ret = fs_path_ensure_buf(p,
950 p->buf_len + p->buf - start);
953 start = btrfs_ref_to_path(root, tmp_path,
958 ret = PTR_ERR(start);
961 BUG_ON(start < p->buf);
965 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
971 cur += elem_size + name_len;
972 ret = iterate(num, dir, index, p, ctx);
979 btrfs_free_path(tmp_path);
984 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
985 const char *name, int name_len,
986 const char *data, int data_len,
990 * Helper function to iterate the entries in ONE btrfs_dir_item.
991 * The iterate callback may return a non zero value to stop iteration. This can
992 * be a negative value for error codes or 1 to simply stop it.
994 * path must point to the dir item when called.
996 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
997 iterate_dir_item_t iterate, void *ctx)
1000 struct extent_buffer *eb;
1001 struct btrfs_item *item;
1002 struct btrfs_dir_item *di;
1003 struct btrfs_key di_key;
1016 * Start with a small buffer (1 page). If later we end up needing more
1017 * space, which can happen for xattrs on a fs with a leaf size greater
1018 * then the page size, attempt to increase the buffer. Typically xattr
1022 buf = kmalloc(buf_len, GFP_KERNEL);
1028 eb = path->nodes[0];
1029 slot = path->slots[0];
1030 item = btrfs_item_nr(slot);
1031 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1034 total = btrfs_item_size(eb, item);
1037 while (cur < total) {
1038 name_len = btrfs_dir_name_len(eb, di);
1039 data_len = btrfs_dir_data_len(eb, di);
1040 type = btrfs_dir_type(eb, di);
1041 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1043 if (type == BTRFS_FT_XATTR) {
1044 if (name_len > XATTR_NAME_MAX) {
1045 ret = -ENAMETOOLONG;
1048 if (name_len + data_len >
1049 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1057 if (name_len + data_len > PATH_MAX) {
1058 ret = -ENAMETOOLONG;
1063 if (name_len + data_len > buf_len) {
1064 buf_len = name_len + data_len;
1065 if (is_vmalloc_addr(buf)) {
1069 char *tmp = krealloc(buf, buf_len,
1070 GFP_KERNEL | __GFP_NOWARN);
1077 buf = kvmalloc(buf_len, GFP_KERNEL);
1085 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1086 name_len + data_len);
1088 len = sizeof(*di) + name_len + data_len;
1089 di = (struct btrfs_dir_item *)((char *)di + len);
1092 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1093 data_len, type, ctx);
1109 static int __copy_first_ref(int num, u64 dir, int index,
1110 struct fs_path *p, void *ctx)
1113 struct fs_path *pt = ctx;
1115 ret = fs_path_copy(pt, p);
1119 /* we want the first only */
1124 * Retrieve the first path of an inode. If an inode has more then one
1125 * ref/hardlink, this is ignored.
1127 static int get_inode_path(struct btrfs_root *root,
1128 u64 ino, struct fs_path *path)
1131 struct btrfs_key key, found_key;
1132 struct btrfs_path *p;
1134 p = alloc_path_for_send();
1138 fs_path_reset(path);
1141 key.type = BTRFS_INODE_REF_KEY;
1144 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1151 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1152 if (found_key.objectid != ino ||
1153 (found_key.type != BTRFS_INODE_REF_KEY &&
1154 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1159 ret = iterate_inode_ref(root, p, &found_key, 1,
1160 __copy_first_ref, path);
1170 struct backref_ctx {
1171 struct send_ctx *sctx;
1173 struct btrfs_path *path;
1174 /* number of total found references */
1178 * used for clones found in send_root. clones found behind cur_objectid
1179 * and cur_offset are not considered as allowed clones.
1184 /* may be truncated in case it's the last extent in a file */
1187 /* data offset in the file extent item */
1190 /* Just to check for bugs in backref resolving */
1194 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1196 u64 root = (u64)(uintptr_t)key;
1197 struct clone_root *cr = (struct clone_root *)elt;
1199 if (root < cr->root->objectid)
1201 if (root > cr->root->objectid)
1206 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1208 struct clone_root *cr1 = (struct clone_root *)e1;
1209 struct clone_root *cr2 = (struct clone_root *)e2;
1211 if (cr1->root->objectid < cr2->root->objectid)
1213 if (cr1->root->objectid > cr2->root->objectid)
1219 * Called for every backref that is found for the current extent.
1220 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1222 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1224 struct backref_ctx *bctx = ctx_;
1225 struct clone_root *found;
1229 /* First check if the root is in the list of accepted clone sources */
1230 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1231 bctx->sctx->clone_roots_cnt,
1232 sizeof(struct clone_root),
1233 __clone_root_cmp_bsearch);
1237 if (found->root == bctx->sctx->send_root &&
1238 ino == bctx->cur_objectid &&
1239 offset == bctx->cur_offset) {
1240 bctx->found_itself = 1;
1244 * There are inodes that have extents that lie behind its i_size. Don't
1245 * accept clones from these extents.
1247 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1249 btrfs_release_path(bctx->path);
1253 if (offset + bctx->data_offset + bctx->extent_len > i_size)
1257 * Make sure we don't consider clones from send_root that are
1258 * behind the current inode/offset.
1260 if (found->root == bctx->sctx->send_root) {
1262 * TODO for the moment we don't accept clones from the inode
1263 * that is currently send. We may change this when
1264 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1267 if (ino >= bctx->cur_objectid)
1272 found->found_refs++;
1273 if (ino < found->ino) {
1275 found->offset = offset;
1276 } else if (found->ino == ino) {
1278 * same extent found more then once in the same file.
1280 if (found->offset > offset + bctx->extent_len)
1281 found->offset = offset;
1288 * Given an inode, offset and extent item, it finds a good clone for a clone
1289 * instruction. Returns -ENOENT when none could be found. The function makes
1290 * sure that the returned clone is usable at the point where sending is at the
1291 * moment. This means, that no clones are accepted which lie behind the current
1294 * path must point to the extent item when called.
1296 static int find_extent_clone(struct send_ctx *sctx,
1297 struct btrfs_path *path,
1298 u64 ino, u64 data_offset,
1300 struct clone_root **found)
1302 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1308 u64 extent_item_pos;
1310 struct btrfs_file_extent_item *fi;
1311 struct extent_buffer *eb = path->nodes[0];
1312 struct backref_ctx *backref_ctx = NULL;
1313 struct clone_root *cur_clone_root;
1314 struct btrfs_key found_key;
1315 struct btrfs_path *tmp_path;
1316 struct btrfs_extent_item *ei;
1320 tmp_path = alloc_path_for_send();
1324 /* We only use this path under the commit sem */
1325 tmp_path->need_commit_sem = 0;
1327 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
1333 backref_ctx->path = tmp_path;
1335 if (data_offset >= ino_size) {
1337 * There may be extents that lie behind the file's size.
1338 * I at least had this in combination with snapshotting while
1339 * writing large files.
1345 fi = btrfs_item_ptr(eb, path->slots[0],
1346 struct btrfs_file_extent_item);
1347 extent_type = btrfs_file_extent_type(eb, fi);
1348 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1352 compressed = btrfs_file_extent_compression(eb, fi);
1354 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1355 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1356 if (disk_byte == 0) {
1360 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1362 down_read(&fs_info->commit_root_sem);
1363 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1364 &found_key, &flags);
1365 up_read(&fs_info->commit_root_sem);
1369 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1374 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1375 struct btrfs_extent_item);
1377 * Backreference walking (iterate_extent_inodes() below) is currently
1378 * too expensive when an extent has a large number of references, both
1379 * in time spent and used memory. So for now just fallback to write
1380 * operations instead of clone operations when an extent has more than
1381 * a certain amount of references.
1383 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1387 btrfs_release_path(tmp_path);
1390 * Setup the clone roots.
1392 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1393 cur_clone_root = sctx->clone_roots + i;
1394 cur_clone_root->ino = (u64)-1;
1395 cur_clone_root->offset = 0;
1396 cur_clone_root->found_refs = 0;
1399 backref_ctx->sctx = sctx;
1400 backref_ctx->found = 0;
1401 backref_ctx->cur_objectid = ino;
1402 backref_ctx->cur_offset = data_offset;
1403 backref_ctx->found_itself = 0;
1404 backref_ctx->extent_len = num_bytes;
1406 * For non-compressed extents iterate_extent_inodes() gives us extent
1407 * offsets that already take into account the data offset, but not for
1408 * compressed extents, since the offset is logical and not relative to
1409 * the physical extent locations. We must take this into account to
1410 * avoid sending clone offsets that go beyond the source file's size,
1411 * which would result in the clone ioctl failing with -EINVAL on the
1414 if (compressed == BTRFS_COMPRESS_NONE)
1415 backref_ctx->data_offset = 0;
1417 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
1420 * The last extent of a file may be too large due to page alignment.
1421 * We need to adjust extent_len in this case so that the checks in
1422 * __iterate_backrefs work.
1424 if (data_offset + num_bytes >= ino_size)
1425 backref_ctx->extent_len = ino_size - data_offset;
1428 * Now collect all backrefs.
1430 if (compressed == BTRFS_COMPRESS_NONE)
1431 extent_item_pos = logical - found_key.objectid;
1433 extent_item_pos = 0;
1434 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1435 extent_item_pos, 1, __iterate_backrefs,
1436 backref_ctx, false);
1441 if (!backref_ctx->found_itself) {
1442 /* found a bug in backref code? */
1445 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1446 ino, data_offset, disk_byte, found_key.objectid);
1450 btrfs_debug(fs_info,
1451 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1452 data_offset, ino, num_bytes, logical);
1454 if (!backref_ctx->found)
1455 btrfs_debug(fs_info, "no clones found");
1457 cur_clone_root = NULL;
1458 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1459 if (sctx->clone_roots[i].found_refs) {
1460 if (!cur_clone_root)
1461 cur_clone_root = sctx->clone_roots + i;
1462 else if (sctx->clone_roots[i].root == sctx->send_root)
1463 /* prefer clones from send_root over others */
1464 cur_clone_root = sctx->clone_roots + i;
1469 if (cur_clone_root) {
1470 *found = cur_clone_root;
1477 btrfs_free_path(tmp_path);
1482 static int read_symlink(struct btrfs_root *root,
1484 struct fs_path *dest)
1487 struct btrfs_path *path;
1488 struct btrfs_key key;
1489 struct btrfs_file_extent_item *ei;
1495 path = alloc_path_for_send();
1500 key.type = BTRFS_EXTENT_DATA_KEY;
1502 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1507 * An empty symlink inode. Can happen in rare error paths when
1508 * creating a symlink (transaction committed before the inode
1509 * eviction handler removed the symlink inode items and a crash
1510 * happened in between or the subvol was snapshoted in between).
1511 * Print an informative message to dmesg/syslog so that the user
1512 * can delete the symlink.
1514 btrfs_err(root->fs_info,
1515 "Found empty symlink inode %llu at root %llu",
1516 ino, root->root_key.objectid);
1521 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1522 struct btrfs_file_extent_item);
1523 type = btrfs_file_extent_type(path->nodes[0], ei);
1524 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1525 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1526 BUG_ON(compression);
1528 off = btrfs_file_extent_inline_start(ei);
1529 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
1531 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1534 btrfs_free_path(path);
1539 * Helper function to generate a file name that is unique in the root of
1540 * send_root and parent_root. This is used to generate names for orphan inodes.
1542 static int gen_unique_name(struct send_ctx *sctx,
1544 struct fs_path *dest)
1547 struct btrfs_path *path;
1548 struct btrfs_dir_item *di;
1553 path = alloc_path_for_send();
1558 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1560 ASSERT(len < sizeof(tmp));
1562 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1563 path, BTRFS_FIRST_FREE_OBJECTID,
1564 tmp, strlen(tmp), 0);
1565 btrfs_release_path(path);
1571 /* not unique, try again */
1576 if (!sctx->parent_root) {
1582 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1583 path, BTRFS_FIRST_FREE_OBJECTID,
1584 tmp, strlen(tmp), 0);
1585 btrfs_release_path(path);
1591 /* not unique, try again */
1599 ret = fs_path_add(dest, tmp, strlen(tmp));
1602 btrfs_free_path(path);
1607 inode_state_no_change,
1608 inode_state_will_create,
1609 inode_state_did_create,
1610 inode_state_will_delete,
1611 inode_state_did_delete,
1614 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1622 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1624 if (ret < 0 && ret != -ENOENT)
1628 if (!sctx->parent_root) {
1629 right_ret = -ENOENT;
1631 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1632 NULL, NULL, NULL, NULL);
1633 if (ret < 0 && ret != -ENOENT)
1638 if (!left_ret && !right_ret) {
1639 if (left_gen == gen && right_gen == gen) {
1640 ret = inode_state_no_change;
1641 } else if (left_gen == gen) {
1642 if (ino < sctx->send_progress)
1643 ret = inode_state_did_create;
1645 ret = inode_state_will_create;
1646 } else if (right_gen == gen) {
1647 if (ino < sctx->send_progress)
1648 ret = inode_state_did_delete;
1650 ret = inode_state_will_delete;
1654 } else if (!left_ret) {
1655 if (left_gen == gen) {
1656 if (ino < sctx->send_progress)
1657 ret = inode_state_did_create;
1659 ret = inode_state_will_create;
1663 } else if (!right_ret) {
1664 if (right_gen == gen) {
1665 if (ino < sctx->send_progress)
1666 ret = inode_state_did_delete;
1668 ret = inode_state_will_delete;
1680 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1684 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1687 ret = get_cur_inode_state(sctx, ino, gen);
1691 if (ret == inode_state_no_change ||
1692 ret == inode_state_did_create ||
1693 ret == inode_state_will_delete)
1703 * Helper function to lookup a dir item in a dir.
1705 static int lookup_dir_item_inode(struct btrfs_root *root,
1706 u64 dir, const char *name, int name_len,
1711 struct btrfs_dir_item *di;
1712 struct btrfs_key key;
1713 struct btrfs_path *path;
1715 path = alloc_path_for_send();
1719 di = btrfs_lookup_dir_item(NULL, root, path,
1720 dir, name, name_len, 0);
1729 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1730 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1734 *found_inode = key.objectid;
1735 *found_type = btrfs_dir_type(path->nodes[0], di);
1738 btrfs_free_path(path);
1743 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1744 * generation of the parent dir and the name of the dir entry.
1746 static int get_first_ref(struct btrfs_root *root, u64 ino,
1747 u64 *dir, u64 *dir_gen, struct fs_path *name)
1750 struct btrfs_key key;
1751 struct btrfs_key found_key;
1752 struct btrfs_path *path;
1756 path = alloc_path_for_send();
1761 key.type = BTRFS_INODE_REF_KEY;
1764 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1768 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1770 if (ret || found_key.objectid != ino ||
1771 (found_key.type != BTRFS_INODE_REF_KEY &&
1772 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1777 if (found_key.type == BTRFS_INODE_REF_KEY) {
1778 struct btrfs_inode_ref *iref;
1779 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1780 struct btrfs_inode_ref);
1781 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1782 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1783 (unsigned long)(iref + 1),
1785 parent_dir = found_key.offset;
1787 struct btrfs_inode_extref *extref;
1788 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1789 struct btrfs_inode_extref);
1790 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1791 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1792 (unsigned long)&extref->name, len);
1793 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1797 btrfs_release_path(path);
1800 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1809 btrfs_free_path(path);
1813 static int is_first_ref(struct btrfs_root *root,
1815 const char *name, int name_len)
1818 struct fs_path *tmp_name;
1821 tmp_name = fs_path_alloc();
1825 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1829 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1834 ret = !memcmp(tmp_name->start, name, name_len);
1837 fs_path_free(tmp_name);
1842 * Used by process_recorded_refs to determine if a new ref would overwrite an
1843 * already existing ref. In case it detects an overwrite, it returns the
1844 * inode/gen in who_ino/who_gen.
1845 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1846 * to make sure later references to the overwritten inode are possible.
1847 * Orphanizing is however only required for the first ref of an inode.
1848 * process_recorded_refs does an additional is_first_ref check to see if
1849 * orphanizing is really required.
1851 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1852 const char *name, int name_len,
1853 u64 *who_ino, u64 *who_gen, u64 *who_mode)
1857 u64 other_inode = 0;
1860 if (!sctx->parent_root)
1863 ret = is_inode_existent(sctx, dir, dir_gen);
1868 * If we have a parent root we need to verify that the parent dir was
1869 * not deleted and then re-created, if it was then we have no overwrite
1870 * and we can just unlink this entry.
1872 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
1873 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1875 if (ret < 0 && ret != -ENOENT)
1885 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1886 &other_inode, &other_type);
1887 if (ret < 0 && ret != -ENOENT)
1895 * Check if the overwritten ref was already processed. If yes, the ref
1896 * was already unlinked/moved, so we can safely assume that we will not
1897 * overwrite anything at this point in time.
1899 if (other_inode > sctx->send_progress ||
1900 is_waiting_for_move(sctx, other_inode)) {
1901 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1902 who_gen, who_mode, NULL, NULL, NULL);
1907 *who_ino = other_inode;
1917 * Checks if the ref was overwritten by an already processed inode. This is
1918 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1919 * thus the orphan name needs be used.
1920 * process_recorded_refs also uses it to avoid unlinking of refs that were
1923 static int did_overwrite_ref(struct send_ctx *sctx,
1924 u64 dir, u64 dir_gen,
1925 u64 ino, u64 ino_gen,
1926 const char *name, int name_len)
1933 if (!sctx->parent_root)
1936 ret = is_inode_existent(sctx, dir, dir_gen);
1940 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1941 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1943 if (ret < 0 && ret != -ENOENT)
1953 /* check if the ref was overwritten by another ref */
1954 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1955 &ow_inode, &other_type);
1956 if (ret < 0 && ret != -ENOENT)
1959 /* was never and will never be overwritten */
1964 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1969 if (ow_inode == ino && gen == ino_gen) {
1975 * We know that it is or will be overwritten. Check this now.
1976 * The current inode being processed might have been the one that caused
1977 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1978 * the current inode being processed.
1980 if ((ow_inode < sctx->send_progress) ||
1981 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1982 gen == sctx->cur_inode_gen))
1992 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1993 * that got overwritten. This is used by process_recorded_refs to determine
1994 * if it has to use the path as returned by get_cur_path or the orphan name.
1996 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1999 struct fs_path *name = NULL;
2003 if (!sctx->parent_root)
2006 name = fs_path_alloc();
2010 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
2014 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2015 name->start, fs_path_len(name));
2023 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2024 * so we need to do some special handling in case we have clashes. This function
2025 * takes care of this with the help of name_cache_entry::radix_list.
2026 * In case of error, nce is kfreed.
2028 static int name_cache_insert(struct send_ctx *sctx,
2029 struct name_cache_entry *nce)
2032 struct list_head *nce_head;
2034 nce_head = radix_tree_lookup(&sctx->name_cache,
2035 (unsigned long)nce->ino);
2037 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
2042 INIT_LIST_HEAD(nce_head);
2044 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
2051 list_add_tail(&nce->radix_list, nce_head);
2052 list_add_tail(&nce->list, &sctx->name_cache_list);
2053 sctx->name_cache_size++;
2058 static void name_cache_delete(struct send_ctx *sctx,
2059 struct name_cache_entry *nce)
2061 struct list_head *nce_head;
2063 nce_head = radix_tree_lookup(&sctx->name_cache,
2064 (unsigned long)nce->ino);
2066 btrfs_err(sctx->send_root->fs_info,
2067 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2068 nce->ino, sctx->name_cache_size);
2071 list_del(&nce->radix_list);
2072 list_del(&nce->list);
2073 sctx->name_cache_size--;
2076 * We may not get to the final release of nce_head if the lookup fails
2078 if (nce_head && list_empty(nce_head)) {
2079 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2084 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2087 struct list_head *nce_head;
2088 struct name_cache_entry *cur;
2090 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2094 list_for_each_entry(cur, nce_head, radix_list) {
2095 if (cur->ino == ino && cur->gen == gen)
2102 * Removes the entry from the list and adds it back to the end. This marks the
2103 * entry as recently used so that name_cache_clean_unused does not remove it.
2105 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2107 list_del(&nce->list);
2108 list_add_tail(&nce->list, &sctx->name_cache_list);
2112 * Remove some entries from the beginning of name_cache_list.
2114 static void name_cache_clean_unused(struct send_ctx *sctx)
2116 struct name_cache_entry *nce;
2118 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2121 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2122 nce = list_entry(sctx->name_cache_list.next,
2123 struct name_cache_entry, list);
2124 name_cache_delete(sctx, nce);
2129 static void name_cache_free(struct send_ctx *sctx)
2131 struct name_cache_entry *nce;
2133 while (!list_empty(&sctx->name_cache_list)) {
2134 nce = list_entry(sctx->name_cache_list.next,
2135 struct name_cache_entry, list);
2136 name_cache_delete(sctx, nce);
2142 * Used by get_cur_path for each ref up to the root.
2143 * Returns 0 if it succeeded.
2144 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2145 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2146 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2147 * Returns <0 in case of error.
2149 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2153 struct fs_path *dest)
2157 struct name_cache_entry *nce = NULL;
2160 * First check if we already did a call to this function with the same
2161 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2162 * return the cached result.
2164 nce = name_cache_search(sctx, ino, gen);
2166 if (ino < sctx->send_progress && nce->need_later_update) {
2167 name_cache_delete(sctx, nce);
2171 name_cache_used(sctx, nce);
2172 *parent_ino = nce->parent_ino;
2173 *parent_gen = nce->parent_gen;
2174 ret = fs_path_add(dest, nce->name, nce->name_len);
2183 * If the inode is not existent yet, add the orphan name and return 1.
2184 * This should only happen for the parent dir that we determine in
2187 ret = is_inode_existent(sctx, ino, gen);
2192 ret = gen_unique_name(sctx, ino, gen, dest);
2200 * Depending on whether the inode was already processed or not, use
2201 * send_root or parent_root for ref lookup.
2203 if (ino < sctx->send_progress)
2204 ret = get_first_ref(sctx->send_root, ino,
2205 parent_ino, parent_gen, dest);
2207 ret = get_first_ref(sctx->parent_root, ino,
2208 parent_ino, parent_gen, dest);
2213 * Check if the ref was overwritten by an inode's ref that was processed
2214 * earlier. If yes, treat as orphan and return 1.
2216 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2217 dest->start, dest->end - dest->start);
2221 fs_path_reset(dest);
2222 ret = gen_unique_name(sctx, ino, gen, dest);
2230 * Store the result of the lookup in the name cache.
2232 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2240 nce->parent_ino = *parent_ino;
2241 nce->parent_gen = *parent_gen;
2242 nce->name_len = fs_path_len(dest);
2244 strcpy(nce->name, dest->start);
2246 if (ino < sctx->send_progress)
2247 nce->need_later_update = 0;
2249 nce->need_later_update = 1;
2251 nce_ret = name_cache_insert(sctx, nce);
2254 name_cache_clean_unused(sctx);
2261 * Magic happens here. This function returns the first ref to an inode as it
2262 * would look like while receiving the stream at this point in time.
2263 * We walk the path up to the root. For every inode in between, we check if it
2264 * was already processed/sent. If yes, we continue with the parent as found
2265 * in send_root. If not, we continue with the parent as found in parent_root.
2266 * If we encounter an inode that was deleted at this point in time, we use the
2267 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2268 * that were not created yet and overwritten inodes/refs.
2270 * When do we have have orphan inodes:
2271 * 1. When an inode is freshly created and thus no valid refs are available yet
2272 * 2. When a directory lost all it's refs (deleted) but still has dir items
2273 * inside which were not processed yet (pending for move/delete). If anyone
2274 * tried to get the path to the dir items, it would get a path inside that
2276 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2277 * of an unprocessed inode. If in that case the first ref would be
2278 * overwritten, the overwritten inode gets "orphanized". Later when we
2279 * process this overwritten inode, it is restored at a new place by moving
2282 * sctx->send_progress tells this function at which point in time receiving
2285 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2286 struct fs_path *dest)
2289 struct fs_path *name = NULL;
2290 u64 parent_inode = 0;
2294 name = fs_path_alloc();
2301 fs_path_reset(dest);
2303 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2304 struct waiting_dir_move *wdm;
2306 fs_path_reset(name);
2308 if (is_waiting_for_rm(sctx, ino, gen)) {
2309 ret = gen_unique_name(sctx, ino, gen, name);
2312 ret = fs_path_add_path(dest, name);
2316 wdm = get_waiting_dir_move(sctx, ino);
2317 if (wdm && wdm->orphanized) {
2318 ret = gen_unique_name(sctx, ino, gen, name);
2321 ret = get_first_ref(sctx->parent_root, ino,
2322 &parent_inode, &parent_gen, name);
2324 ret = __get_cur_name_and_parent(sctx, ino, gen,
2334 ret = fs_path_add_path(dest, name);
2345 fs_path_unreverse(dest);
2350 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2352 static int send_subvol_begin(struct send_ctx *sctx)
2355 struct btrfs_root *send_root = sctx->send_root;
2356 struct btrfs_root *parent_root = sctx->parent_root;
2357 struct btrfs_path *path;
2358 struct btrfs_key key;
2359 struct btrfs_root_ref *ref;
2360 struct extent_buffer *leaf;
2364 path = btrfs_alloc_path();
2368 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2370 btrfs_free_path(path);
2374 key.objectid = send_root->objectid;
2375 key.type = BTRFS_ROOT_BACKREF_KEY;
2378 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2387 leaf = path->nodes[0];
2388 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2389 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2390 key.objectid != send_root->objectid) {
2394 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2395 namelen = btrfs_root_ref_name_len(leaf, ref);
2396 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2397 btrfs_release_path(path);
2400 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2404 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2409 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2411 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2412 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2413 sctx->send_root->root_item.received_uuid);
2415 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2416 sctx->send_root->root_item.uuid);
2418 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2419 le64_to_cpu(sctx->send_root->root_item.ctransid));
2421 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2422 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2423 parent_root->root_item.received_uuid);
2425 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2426 parent_root->root_item.uuid);
2427 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2428 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2431 ret = send_cmd(sctx);
2435 btrfs_free_path(path);
2440 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2442 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2446 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
2448 p = fs_path_alloc();
2452 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2456 ret = get_cur_path(sctx, ino, gen, p);
2459 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2460 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2462 ret = send_cmd(sctx);
2470 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2472 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2476 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
2478 p = fs_path_alloc();
2482 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2486 ret = get_cur_path(sctx, ino, gen, p);
2489 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2490 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2492 ret = send_cmd(sctx);
2500 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2502 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2506 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2509 p = fs_path_alloc();
2513 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2517 ret = get_cur_path(sctx, ino, gen, p);
2520 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2521 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2522 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2524 ret = send_cmd(sctx);
2532 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2534 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2536 struct fs_path *p = NULL;
2537 struct btrfs_inode_item *ii;
2538 struct btrfs_path *path = NULL;
2539 struct extent_buffer *eb;
2540 struct btrfs_key key;
2543 btrfs_debug(fs_info, "send_utimes %llu", ino);
2545 p = fs_path_alloc();
2549 path = alloc_path_for_send();
2556 key.type = BTRFS_INODE_ITEM_KEY;
2558 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2564 eb = path->nodes[0];
2565 slot = path->slots[0];
2566 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2568 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2572 ret = get_cur_path(sctx, ino, gen, p);
2575 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2576 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2577 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2578 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2579 /* TODO Add otime support when the otime patches get into upstream */
2581 ret = send_cmd(sctx);
2586 btrfs_free_path(path);
2591 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2592 * a valid path yet because we did not process the refs yet. So, the inode
2593 * is created as orphan.
2595 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2597 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2605 btrfs_debug(fs_info, "send_create_inode %llu", ino);
2607 p = fs_path_alloc();
2611 if (ino != sctx->cur_ino) {
2612 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2617 gen = sctx->cur_inode_gen;
2618 mode = sctx->cur_inode_mode;
2619 rdev = sctx->cur_inode_rdev;
2622 if (S_ISREG(mode)) {
2623 cmd = BTRFS_SEND_C_MKFILE;
2624 } else if (S_ISDIR(mode)) {
2625 cmd = BTRFS_SEND_C_MKDIR;
2626 } else if (S_ISLNK(mode)) {
2627 cmd = BTRFS_SEND_C_SYMLINK;
2628 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2629 cmd = BTRFS_SEND_C_MKNOD;
2630 } else if (S_ISFIFO(mode)) {
2631 cmd = BTRFS_SEND_C_MKFIFO;
2632 } else if (S_ISSOCK(mode)) {
2633 cmd = BTRFS_SEND_C_MKSOCK;
2635 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2636 (int)(mode & S_IFMT));
2641 ret = begin_cmd(sctx, cmd);
2645 ret = gen_unique_name(sctx, ino, gen, p);
2649 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2650 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2652 if (S_ISLNK(mode)) {
2654 ret = read_symlink(sctx->send_root, ino, p);
2657 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2658 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2659 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2660 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2661 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2664 ret = send_cmd(sctx);
2676 * We need some special handling for inodes that get processed before the parent
2677 * directory got created. See process_recorded_refs for details.
2678 * This function does the check if we already created the dir out of order.
2680 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2683 struct btrfs_path *path = NULL;
2684 struct btrfs_key key;
2685 struct btrfs_key found_key;
2686 struct btrfs_key di_key;
2687 struct extent_buffer *eb;
2688 struct btrfs_dir_item *di;
2691 path = alloc_path_for_send();
2698 key.type = BTRFS_DIR_INDEX_KEY;
2700 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2705 eb = path->nodes[0];
2706 slot = path->slots[0];
2707 if (slot >= btrfs_header_nritems(eb)) {
2708 ret = btrfs_next_leaf(sctx->send_root, path);
2711 } else if (ret > 0) {
2718 btrfs_item_key_to_cpu(eb, &found_key, slot);
2719 if (found_key.objectid != key.objectid ||
2720 found_key.type != key.type) {
2725 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2726 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2728 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2729 di_key.objectid < sctx->send_progress) {
2738 btrfs_free_path(path);
2743 * Only creates the inode if it is:
2744 * 1. Not a directory
2745 * 2. Or a directory which was not created already due to out of order
2746 * directories. See did_create_dir and process_recorded_refs for details.
2748 static int send_create_inode_if_needed(struct send_ctx *sctx)
2752 if (S_ISDIR(sctx->cur_inode_mode)) {
2753 ret = did_create_dir(sctx, sctx->cur_ino);
2762 ret = send_create_inode(sctx, sctx->cur_ino);
2770 struct recorded_ref {
2771 struct list_head list;
2773 struct fs_path *full_path;
2779 static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2781 ref->full_path = path;
2782 ref->name = (char *)kbasename(ref->full_path->start);
2783 ref->name_len = ref->full_path->end - ref->name;
2787 * We need to process new refs before deleted refs, but compare_tree gives us
2788 * everything mixed. So we first record all refs and later process them.
2789 * This function is a helper to record one ref.
2791 static int __record_ref(struct list_head *head, u64 dir,
2792 u64 dir_gen, struct fs_path *path)
2794 struct recorded_ref *ref;
2796 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
2801 ref->dir_gen = dir_gen;
2802 set_ref_path(ref, path);
2803 list_add_tail(&ref->list, head);
2807 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2809 struct recorded_ref *new;
2811 new = kmalloc(sizeof(*ref), GFP_KERNEL);
2815 new->dir = ref->dir;
2816 new->dir_gen = ref->dir_gen;
2817 new->full_path = NULL;
2818 INIT_LIST_HEAD(&new->list);
2819 list_add_tail(&new->list, list);
2823 static void __free_recorded_refs(struct list_head *head)
2825 struct recorded_ref *cur;
2827 while (!list_empty(head)) {
2828 cur = list_entry(head->next, struct recorded_ref, list);
2829 fs_path_free(cur->full_path);
2830 list_del(&cur->list);
2835 static void free_recorded_refs(struct send_ctx *sctx)
2837 __free_recorded_refs(&sctx->new_refs);
2838 __free_recorded_refs(&sctx->deleted_refs);
2842 * Renames/moves a file/dir to its orphan name. Used when the first
2843 * ref of an unprocessed inode gets overwritten and for all non empty
2846 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2847 struct fs_path *path)
2850 struct fs_path *orphan;
2852 orphan = fs_path_alloc();
2856 ret = gen_unique_name(sctx, ino, gen, orphan);
2860 ret = send_rename(sctx, path, orphan);
2863 fs_path_free(orphan);
2867 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
2868 u64 dir_ino, u64 dir_gen)
2870 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2871 struct rb_node *parent = NULL;
2872 struct orphan_dir_info *entry, *odi;
2876 entry = rb_entry(parent, struct orphan_dir_info, node);
2877 if (dir_ino < entry->ino)
2879 else if (dir_ino > entry->ino)
2880 p = &(*p)->rb_right;
2881 else if (dir_gen < entry->gen)
2883 else if (dir_gen > entry->gen)
2884 p = &(*p)->rb_right;
2889 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2891 return ERR_PTR(-ENOMEM);
2894 odi->last_dir_index_offset = 0;
2896 rb_link_node(&odi->node, parent, p);
2897 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2901 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
2902 u64 dir_ino, u64 gen)
2904 struct rb_node *n = sctx->orphan_dirs.rb_node;
2905 struct orphan_dir_info *entry;
2908 entry = rb_entry(n, struct orphan_dir_info, node);
2909 if (dir_ino < entry->ino)
2911 else if (dir_ino > entry->ino)
2913 else if (gen < entry->gen)
2915 else if (gen > entry->gen)
2923 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
2925 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
2930 static void free_orphan_dir_info(struct send_ctx *sctx,
2931 struct orphan_dir_info *odi)
2935 rb_erase(&odi->node, &sctx->orphan_dirs);
2940 * Returns 1 if a directory can be removed at this point in time.
2941 * We check this by iterating all dir items and checking if the inode behind
2942 * the dir item was already processed.
2944 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2948 struct btrfs_root *root = sctx->parent_root;
2949 struct btrfs_path *path;
2950 struct btrfs_key key;
2951 struct btrfs_key found_key;
2952 struct btrfs_key loc;
2953 struct btrfs_dir_item *di;
2954 struct orphan_dir_info *odi = NULL;
2957 * Don't try to rmdir the top/root subvolume dir.
2959 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2962 path = alloc_path_for_send();
2967 key.type = BTRFS_DIR_INDEX_KEY;
2970 odi = get_orphan_dir_info(sctx, dir, dir_gen);
2972 key.offset = odi->last_dir_index_offset;
2974 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2979 struct waiting_dir_move *dm;
2981 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2982 ret = btrfs_next_leaf(root, path);
2989 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2991 if (found_key.objectid != key.objectid ||
2992 found_key.type != key.type)
2995 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2996 struct btrfs_dir_item);
2997 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2999 dm = get_waiting_dir_move(sctx, loc.objectid);
3001 odi = add_orphan_dir_info(sctx, dir, dir_gen);
3007 odi->last_dir_index_offset = found_key.offset;
3008 dm->rmdir_ino = dir;
3009 dm->rmdir_gen = dir_gen;
3014 if (loc.objectid > send_progress) {
3015 odi = add_orphan_dir_info(sctx, dir, dir_gen);
3021 odi->last_dir_index_offset = found_key.offset;
3028 free_orphan_dir_info(sctx, odi);
3033 btrfs_free_path(path);
3037 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3039 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3041 return entry != NULL;
3044 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3046 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3047 struct rb_node *parent = NULL;
3048 struct waiting_dir_move *entry, *dm;
3050 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
3056 dm->orphanized = orphanized;
3060 entry = rb_entry(parent, struct waiting_dir_move, node);
3061 if (ino < entry->ino) {
3063 } else if (ino > entry->ino) {
3064 p = &(*p)->rb_right;
3071 rb_link_node(&dm->node, parent, p);
3072 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3076 static struct waiting_dir_move *
3077 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3079 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3080 struct waiting_dir_move *entry;
3083 entry = rb_entry(n, struct waiting_dir_move, node);
3084 if (ino < entry->ino)
3086 else if (ino > entry->ino)
3094 static void free_waiting_dir_move(struct send_ctx *sctx,
3095 struct waiting_dir_move *dm)
3099 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3103 static int add_pending_dir_move(struct send_ctx *sctx,
3107 struct list_head *new_refs,
3108 struct list_head *deleted_refs,
3109 const bool is_orphan)
3111 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3112 struct rb_node *parent = NULL;
3113 struct pending_dir_move *entry = NULL, *pm;
3114 struct recorded_ref *cur;
3118 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3121 pm->parent_ino = parent_ino;
3124 INIT_LIST_HEAD(&pm->list);
3125 INIT_LIST_HEAD(&pm->update_refs);
3126 RB_CLEAR_NODE(&pm->node);
3130 entry = rb_entry(parent, struct pending_dir_move, node);
3131 if (parent_ino < entry->parent_ino) {
3133 } else if (parent_ino > entry->parent_ino) {
3134 p = &(*p)->rb_right;
3141 list_for_each_entry(cur, deleted_refs, list) {
3142 ret = dup_ref(cur, &pm->update_refs);
3146 list_for_each_entry(cur, new_refs, list) {
3147 ret = dup_ref(cur, &pm->update_refs);
3152 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3157 list_add_tail(&pm->list, &entry->list);
3159 rb_link_node(&pm->node, parent, p);
3160 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3165 __free_recorded_refs(&pm->update_refs);
3171 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3174 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3175 struct pending_dir_move *entry;
3178 entry = rb_entry(n, struct pending_dir_move, node);
3179 if (parent_ino < entry->parent_ino)
3181 else if (parent_ino > entry->parent_ino)
3189 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3190 u64 ino, u64 gen, u64 *ancestor_ino)
3193 u64 parent_inode = 0;
3195 u64 start_ino = ino;
3198 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3199 fs_path_reset(name);
3201 if (is_waiting_for_rm(sctx, ino, gen))
3203 if (is_waiting_for_move(sctx, ino)) {
3204 if (*ancestor_ino == 0)
3205 *ancestor_ino = ino;
3206 ret = get_first_ref(sctx->parent_root, ino,
3207 &parent_inode, &parent_gen, name);
3209 ret = __get_cur_name_and_parent(sctx, ino, gen,
3219 if (parent_inode == start_ino) {
3221 if (*ancestor_ino == 0)
3222 *ancestor_ino = ino;
3231 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3233 struct fs_path *from_path = NULL;
3234 struct fs_path *to_path = NULL;
3235 struct fs_path *name = NULL;
3236 u64 orig_progress = sctx->send_progress;
3237 struct recorded_ref *cur;
3238 u64 parent_ino, parent_gen;
3239 struct waiting_dir_move *dm = NULL;
3246 name = fs_path_alloc();
3247 from_path = fs_path_alloc();
3248 if (!name || !from_path) {
3253 dm = get_waiting_dir_move(sctx, pm->ino);
3255 rmdir_ino = dm->rmdir_ino;
3256 rmdir_gen = dm->rmdir_gen;
3257 is_orphan = dm->orphanized;
3258 free_waiting_dir_move(sctx, dm);
3261 ret = gen_unique_name(sctx, pm->ino,
3262 pm->gen, from_path);
3264 ret = get_first_ref(sctx->parent_root, pm->ino,
3265 &parent_ino, &parent_gen, name);
3268 ret = get_cur_path(sctx, parent_ino, parent_gen,
3272 ret = fs_path_add_path(from_path, name);
3277 sctx->send_progress = sctx->cur_ino + 1;
3278 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3282 LIST_HEAD(deleted_refs);
3283 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3284 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3285 &pm->update_refs, &deleted_refs,
3290 dm = get_waiting_dir_move(sctx, pm->ino);
3292 dm->rmdir_ino = rmdir_ino;
3293 dm->rmdir_gen = rmdir_gen;
3297 fs_path_reset(name);
3300 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3304 ret = send_rename(sctx, from_path, to_path);
3309 struct orphan_dir_info *odi;
3312 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
3314 /* already deleted */
3319 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
3325 name = fs_path_alloc();
3330 ret = get_cur_path(sctx, rmdir_ino, gen, name);
3333 ret = send_rmdir(sctx, name);
3339 ret = send_utimes(sctx, pm->ino, pm->gen);
3344 * After rename/move, need to update the utimes of both new parent(s)
3345 * and old parent(s).
3347 list_for_each_entry(cur, &pm->update_refs, list) {
3349 * The parent inode might have been deleted in the send snapshot
3351 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3352 NULL, NULL, NULL, NULL, NULL);
3353 if (ret == -ENOENT) {
3360 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3367 fs_path_free(from_path);
3368 fs_path_free(to_path);
3369 sctx->send_progress = orig_progress;
3374 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3376 if (!list_empty(&m->list))
3378 if (!RB_EMPTY_NODE(&m->node))
3379 rb_erase(&m->node, &sctx->pending_dir_moves);
3380 __free_recorded_refs(&m->update_refs);
3384 static void tail_append_pending_moves(struct send_ctx *sctx,
3385 struct pending_dir_move *moves,
3386 struct list_head *stack)
3388 if (list_empty(&moves->list)) {
3389 list_add_tail(&moves->list, stack);
3392 list_splice_init(&moves->list, &list);
3393 list_add_tail(&moves->list, stack);
3394 list_splice_tail(&list, stack);
3396 if (!RB_EMPTY_NODE(&moves->node)) {
3397 rb_erase(&moves->node, &sctx->pending_dir_moves);
3398 RB_CLEAR_NODE(&moves->node);
3402 static int apply_children_dir_moves(struct send_ctx *sctx)
3404 struct pending_dir_move *pm;
3405 struct list_head stack;
3406 u64 parent_ino = sctx->cur_ino;
3409 pm = get_pending_dir_moves(sctx, parent_ino);
3413 INIT_LIST_HEAD(&stack);
3414 tail_append_pending_moves(sctx, pm, &stack);
3416 while (!list_empty(&stack)) {
3417 pm = list_first_entry(&stack, struct pending_dir_move, list);
3418 parent_ino = pm->ino;
3419 ret = apply_dir_move(sctx, pm);
3420 free_pending_move(sctx, pm);
3423 pm = get_pending_dir_moves(sctx, parent_ino);
3425 tail_append_pending_moves(sctx, pm, &stack);
3430 while (!list_empty(&stack)) {
3431 pm = list_first_entry(&stack, struct pending_dir_move, list);
3432 free_pending_move(sctx, pm);
3438 * We might need to delay a directory rename even when no ancestor directory
3439 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3440 * renamed. This happens when we rename a directory to the old name (the name
3441 * in the parent root) of some other unrelated directory that got its rename
3442 * delayed due to some ancestor with higher number that got renamed.
3448 * |---- a/ (ino 257)
3449 * | |---- file (ino 260)
3451 * |---- b/ (ino 258)
3452 * |---- c/ (ino 259)
3456 * |---- a/ (ino 258)
3457 * |---- x/ (ino 259)
3458 * |---- y/ (ino 257)
3459 * |----- file (ino 260)
3461 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3462 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3463 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3466 * 1 - rename 259 from 'c' to 'x'
3467 * 2 - rename 257 from 'a' to 'x/y'
3468 * 3 - rename 258 from 'b' to 'a'
3470 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3471 * be done right away and < 0 on error.
3473 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3474 struct recorded_ref *parent_ref,
3475 const bool is_orphan)
3477 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3478 struct btrfs_path *path;
3479 struct btrfs_key key;
3480 struct btrfs_key di_key;
3481 struct btrfs_dir_item *di;
3485 struct waiting_dir_move *wdm;
3487 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3490 path = alloc_path_for_send();
3494 key.objectid = parent_ref->dir;
3495 key.type = BTRFS_DIR_ITEM_KEY;
3496 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3498 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3501 } else if (ret > 0) {
3506 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3507 parent_ref->name_len);
3513 * di_key.objectid has the number of the inode that has a dentry in the
3514 * parent directory with the same name that sctx->cur_ino is being
3515 * renamed to. We need to check if that inode is in the send root as
3516 * well and if it is currently marked as an inode with a pending rename,
3517 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3518 * that it happens after that other inode is renamed.
3520 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3521 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3526 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3527 &left_gen, NULL, NULL, NULL, NULL);
3530 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3531 &right_gen, NULL, NULL, NULL, NULL);
3538 /* Different inode, no need to delay the rename of sctx->cur_ino */
3539 if (right_gen != left_gen) {
3544 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3545 if (wdm && !wdm->orphanized) {
3546 ret = add_pending_dir_move(sctx,
3548 sctx->cur_inode_gen,
3551 &sctx->deleted_refs,
3557 btrfs_free_path(path);
3562 * Check if inode ino2, or any of its ancestors, is inode ino1.
3563 * Return 1 if true, 0 if false and < 0 on error.
3565 static int check_ino_in_path(struct btrfs_root *root,
3570 struct fs_path *fs_path)
3575 return ino1_gen == ino2_gen;
3577 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3582 fs_path_reset(fs_path);
3583 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3587 return parent_gen == ino1_gen;
3594 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3595 * possible path (in case ino2 is not a directory and has multiple hard links).
3596 * Return 1 if true, 0 if false and < 0 on error.
3598 static int is_ancestor(struct btrfs_root *root,
3602 struct fs_path *fs_path)
3604 bool free_fs_path = false;
3606 struct btrfs_path *path = NULL;
3607 struct btrfs_key key;
3610 fs_path = fs_path_alloc();
3613 free_fs_path = true;
3616 path = alloc_path_for_send();
3622 key.objectid = ino2;
3623 key.type = BTRFS_INODE_REF_KEY;
3626 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3631 struct extent_buffer *leaf = path->nodes[0];
3632 int slot = path->slots[0];
3636 if (slot >= btrfs_header_nritems(leaf)) {
3637 ret = btrfs_next_leaf(root, path);
3645 btrfs_item_key_to_cpu(leaf, &key, slot);
3646 if (key.objectid != ino2)
3648 if (key.type != BTRFS_INODE_REF_KEY &&
3649 key.type != BTRFS_INODE_EXTREF_KEY)
3652 item_size = btrfs_item_size_nr(leaf, slot);
3653 while (cur_offset < item_size) {
3657 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3659 struct btrfs_inode_extref *extref;
3661 ptr = btrfs_item_ptr_offset(leaf, slot);
3662 extref = (struct btrfs_inode_extref *)
3664 parent = btrfs_inode_extref_parent(leaf,
3666 cur_offset += sizeof(*extref);
3667 cur_offset += btrfs_inode_extref_name_len(leaf,
3670 parent = key.offset;
3671 cur_offset = item_size;
3674 ret = get_inode_info(root, parent, NULL, &parent_gen,
3675 NULL, NULL, NULL, NULL);
3678 ret = check_ino_in_path(root, ino1, ino1_gen,
3679 parent, parent_gen, fs_path);
3687 btrfs_free_path(path);
3689 fs_path_free(fs_path);
3693 static int wait_for_parent_move(struct send_ctx *sctx,
3694 struct recorded_ref *parent_ref,
3695 const bool is_orphan)
3698 u64 ino = parent_ref->dir;
3699 u64 ino_gen = parent_ref->dir_gen;
3700 u64 parent_ino_before, parent_ino_after;
3701 struct fs_path *path_before = NULL;
3702 struct fs_path *path_after = NULL;
3705 path_after = fs_path_alloc();
3706 path_before = fs_path_alloc();
3707 if (!path_after || !path_before) {
3713 * Our current directory inode may not yet be renamed/moved because some
3714 * ancestor (immediate or not) has to be renamed/moved first. So find if
3715 * such ancestor exists and make sure our own rename/move happens after
3716 * that ancestor is processed to avoid path build infinite loops (done
3717 * at get_cur_path()).
3719 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3720 u64 parent_ino_after_gen;
3722 if (is_waiting_for_move(sctx, ino)) {
3724 * If the current inode is an ancestor of ino in the
3725 * parent root, we need to delay the rename of the
3726 * current inode, otherwise don't delayed the rename
3727 * because we can end up with a circular dependency
3728 * of renames, resulting in some directories never
3729 * getting the respective rename operations issued in
3730 * the send stream or getting into infinite path build
3733 ret = is_ancestor(sctx->parent_root,
3734 sctx->cur_ino, sctx->cur_inode_gen,
3740 fs_path_reset(path_before);
3741 fs_path_reset(path_after);
3743 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3744 &parent_ino_after_gen, path_after);
3747 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3749 if (ret < 0 && ret != -ENOENT) {
3751 } else if (ret == -ENOENT) {
3756 len1 = fs_path_len(path_before);
3757 len2 = fs_path_len(path_after);
3758 if (ino > sctx->cur_ino &&
3759 (parent_ino_before != parent_ino_after || len1 != len2 ||
3760 memcmp(path_before->start, path_after->start, len1))) {
3763 ret = get_inode_info(sctx->parent_root, ino, NULL,
3764 &parent_ino_gen, NULL, NULL, NULL,
3768 if (ino_gen == parent_ino_gen) {
3773 ino = parent_ino_after;
3774 ino_gen = parent_ino_after_gen;
3778 fs_path_free(path_before);
3779 fs_path_free(path_after);
3782 ret = add_pending_dir_move(sctx,
3784 sctx->cur_inode_gen,
3787 &sctx->deleted_refs,
3796 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3799 struct fs_path *new_path;
3802 * Our reference's name member points to its full_path member string, so
3803 * we use here a new path.
3805 new_path = fs_path_alloc();
3809 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3811 fs_path_free(new_path);
3814 ret = fs_path_add(new_path, ref->name, ref->name_len);
3816 fs_path_free(new_path);
3820 fs_path_free(ref->full_path);
3821 set_ref_path(ref, new_path);
3827 * When processing the new references for an inode we may orphanize an existing
3828 * directory inode because its old name conflicts with one of the new references
3829 * of the current inode. Later, when processing another new reference of our
3830 * inode, we might need to orphanize another inode, but the path we have in the
3831 * reference reflects the pre-orphanization name of the directory we previously
3832 * orphanized. For example:
3834 * parent snapshot looks like:
3837 * |----- f1 (ino 257)
3838 * |----- f2 (ino 258)
3839 * |----- d1/ (ino 259)
3840 * |----- d2/ (ino 260)
3842 * send snapshot looks like:
3845 * |----- d1 (ino 258)
3846 * |----- f2/ (ino 259)
3847 * |----- f2_link/ (ino 260)
3848 * | |----- f1 (ino 257)
3850 * |----- d2 (ino 258)
3852 * When processing inode 257 we compute the name for inode 259 as "d1", and we
3853 * cache it in the name cache. Later when we start processing inode 258, when
3854 * collecting all its new references we set a full path of "d1/d2" for its new
3855 * reference with name "d2". When we start processing the new references we
3856 * start by processing the new reference with name "d1", and this results in
3857 * orphanizing inode 259, since its old reference causes a conflict. Then we
3858 * move on the next new reference, with name "d2", and we find out we must
3859 * orphanize inode 260, as its old reference conflicts with ours - but for the
3860 * orphanization we use a source path corresponding to the path we stored in the
3861 * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
3862 * receiver fail since the path component "d1/" no longer exists, it was renamed
3863 * to "o259-6-0/" when processing the previous new reference. So in this case we
3864 * must recompute the path in the new reference and use it for the new
3865 * orphanization operation.
3867 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3872 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3876 fs_path_reset(ref->full_path);
3877 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3881 ret = fs_path_add(ref->full_path, name, ref->name_len);
3885 /* Update the reference's base name pointer. */
3886 set_ref_path(ref, ref->full_path);
3893 * This does all the move/link/unlink/rmdir magic.
3895 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3897 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
3899 struct recorded_ref *cur;
3900 struct recorded_ref *cur2;
3901 struct list_head check_dirs;
3902 struct fs_path *valid_path = NULL;
3906 int did_overwrite = 0;
3908 u64 last_dir_ino_rm = 0;
3909 bool can_rename = true;
3910 bool orphanized_dir = false;
3911 bool orphanized_ancestor = false;
3913 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
3916 * This should never happen as the root dir always has the same ref
3917 * which is always '..'
3919 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3920 INIT_LIST_HEAD(&check_dirs);
3922 valid_path = fs_path_alloc();
3929 * First, check if the first ref of the current inode was overwritten
3930 * before. If yes, we know that the current inode was already orphanized
3931 * and thus use the orphan name. If not, we can use get_cur_path to
3932 * get the path of the first ref as it would like while receiving at
3933 * this point in time.
3934 * New inodes are always orphan at the beginning, so force to use the
3935 * orphan name in this case.
3936 * The first ref is stored in valid_path and will be updated if it
3937 * gets moved around.
3939 if (!sctx->cur_inode_new) {
3940 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3941 sctx->cur_inode_gen);
3947 if (sctx->cur_inode_new || did_overwrite) {
3948 ret = gen_unique_name(sctx, sctx->cur_ino,
3949 sctx->cur_inode_gen, valid_path);
3954 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3960 list_for_each_entry(cur, &sctx->new_refs, list) {
3962 * We may have refs where the parent directory does not exist
3963 * yet. This happens if the parent directories inum is higher
3964 * the the current inum. To handle this case, we create the
3965 * parent directory out of order. But we need to check if this
3966 * did already happen before due to other refs in the same dir.
3968 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3971 if (ret == inode_state_will_create) {
3974 * First check if any of the current inodes refs did
3975 * already create the dir.
3977 list_for_each_entry(cur2, &sctx->new_refs, list) {
3980 if (cur2->dir == cur->dir) {
3987 * If that did not happen, check if a previous inode
3988 * did already create the dir.
3991 ret = did_create_dir(sctx, cur->dir);
3995 ret = send_create_inode(sctx, cur->dir);
4002 * Check if this new ref would overwrite the first ref of
4003 * another unprocessed inode. If yes, orphanize the
4004 * overwritten inode. If we find an overwritten ref that is
4005 * not the first ref, simply unlink it.
4007 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4008 cur->name, cur->name_len,
4009 &ow_inode, &ow_gen, &ow_mode);
4013 ret = is_first_ref(sctx->parent_root,
4014 ow_inode, cur->dir, cur->name,
4019 struct name_cache_entry *nce;
4020 struct waiting_dir_move *wdm;
4022 if (orphanized_dir) {
4023 ret = refresh_ref_path(sctx, cur);
4028 ret = orphanize_inode(sctx, ow_inode, ow_gen,
4032 if (S_ISDIR(ow_mode))
4033 orphanized_dir = true;
4036 * If ow_inode has its rename operation delayed
4037 * make sure that its orphanized name is used in
4038 * the source path when performing its rename
4041 if (is_waiting_for_move(sctx, ow_inode)) {
4042 wdm = get_waiting_dir_move(sctx,
4045 wdm->orphanized = true;
4049 * Make sure we clear our orphanized inode's
4050 * name from the name cache. This is because the
4051 * inode ow_inode might be an ancestor of some
4052 * other inode that will be orphanized as well
4053 * later and has an inode number greater than
4054 * sctx->send_progress. We need to prevent
4055 * future name lookups from using the old name
4056 * and get instead the orphan name.
4058 nce = name_cache_search(sctx, ow_inode, ow_gen);
4060 name_cache_delete(sctx, nce);
4065 * ow_inode might currently be an ancestor of
4066 * cur_ino, therefore compute valid_path (the
4067 * current path of cur_ino) again because it
4068 * might contain the pre-orphanization name of
4069 * ow_inode, which is no longer valid.
4071 ret = is_ancestor(sctx->parent_root,
4073 sctx->cur_ino, NULL);
4075 orphanized_ancestor = true;
4076 fs_path_reset(valid_path);
4077 ret = get_cur_path(sctx, sctx->cur_ino,
4078 sctx->cur_inode_gen,
4085 * If we previously orphanized a directory that
4086 * collided with a new reference that we already
4087 * processed, recompute the current path because
4088 * that directory may be part of the path.
4090 if (orphanized_dir) {
4091 ret = refresh_ref_path(sctx, cur);
4095 ret = send_unlink(sctx, cur->full_path);
4101 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4102 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4111 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4113 ret = wait_for_parent_move(sctx, cur, is_orphan);
4123 * link/move the ref to the new place. If we have an orphan
4124 * inode, move it and update valid_path. If not, link or move
4125 * it depending on the inode mode.
4127 if (is_orphan && can_rename) {
4128 ret = send_rename(sctx, valid_path, cur->full_path);
4132 ret = fs_path_copy(valid_path, cur->full_path);
4135 } else if (can_rename) {
4136 if (S_ISDIR(sctx->cur_inode_mode)) {
4138 * Dirs can't be linked, so move it. For moved
4139 * dirs, we always have one new and one deleted
4140 * ref. The deleted ref is ignored later.
4142 ret = send_rename(sctx, valid_path,
4145 ret = fs_path_copy(valid_path,
4151 * We might have previously orphanized an inode
4152 * which is an ancestor of our current inode,
4153 * so our reference's full path, which was
4154 * computed before any such orphanizations, must
4157 if (orphanized_dir) {
4158 ret = update_ref_path(sctx, cur);
4162 ret = send_link(sctx, cur->full_path,
4168 ret = dup_ref(cur, &check_dirs);
4173 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4175 * Check if we can already rmdir the directory. If not,
4176 * orphanize it. For every dir item inside that gets deleted
4177 * later, we do this check again and rmdir it then if possible.
4178 * See the use of check_dirs for more details.
4180 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4185 ret = send_rmdir(sctx, valid_path);
4188 } else if (!is_orphan) {
4189 ret = orphanize_inode(sctx, sctx->cur_ino,
4190 sctx->cur_inode_gen, valid_path);
4196 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4197 ret = dup_ref(cur, &check_dirs);
4201 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4202 !list_empty(&sctx->deleted_refs)) {
4204 * We have a moved dir. Add the old parent to check_dirs
4206 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4208 ret = dup_ref(cur, &check_dirs);
4211 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4213 * We have a non dir inode. Go through all deleted refs and
4214 * unlink them if they were not already overwritten by other
4217 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4218 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4219 sctx->cur_ino, sctx->cur_inode_gen,
4220 cur->name, cur->name_len);
4225 * If we orphanized any ancestor before, we need
4226 * to recompute the full path for deleted names,
4227 * since any such path was computed before we
4228 * processed any references and orphanized any
4231 if (orphanized_ancestor) {
4232 ret = update_ref_path(sctx, cur);
4236 ret = send_unlink(sctx, cur->full_path);
4240 ret = dup_ref(cur, &check_dirs);
4245 * If the inode is still orphan, unlink the orphan. This may
4246 * happen when a previous inode did overwrite the first ref
4247 * of this inode and no new refs were added for the current
4248 * inode. Unlinking does not mean that the inode is deleted in
4249 * all cases. There may still be links to this inode in other
4253 ret = send_unlink(sctx, valid_path);
4260 * We did collect all parent dirs where cur_inode was once located. We
4261 * now go through all these dirs and check if they are pending for
4262 * deletion and if it's finally possible to perform the rmdir now.
4263 * We also update the inode stats of the parent dirs here.
4265 list_for_each_entry(cur, &check_dirs, list) {
4267 * In case we had refs into dirs that were not processed yet,
4268 * we don't need to do the utime and rmdir logic for these dirs.
4269 * The dir will be processed later.
4271 if (cur->dir > sctx->cur_ino)
4274 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4278 if (ret == inode_state_did_create ||
4279 ret == inode_state_no_change) {
4280 /* TODO delayed utimes */
4281 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
4284 } else if (ret == inode_state_did_delete &&
4285 cur->dir != last_dir_ino_rm) {
4286 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4291 ret = get_cur_path(sctx, cur->dir,
4292 cur->dir_gen, valid_path);
4295 ret = send_rmdir(sctx, valid_path);
4298 last_dir_ino_rm = cur->dir;
4306 __free_recorded_refs(&check_dirs);
4307 free_recorded_refs(sctx);
4308 fs_path_free(valid_path);
4312 static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4313 void *ctx, struct list_head *refs)
4316 struct send_ctx *sctx = ctx;
4320 p = fs_path_alloc();
4324 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
4329 ret = get_cur_path(sctx, dir, gen, p);
4332 ret = fs_path_add_path(p, name);
4336 ret = __record_ref(refs, dir, gen, p);
4344 static int __record_new_ref(int num, u64 dir, int index,
4345 struct fs_path *name,
4348 struct send_ctx *sctx = ctx;
4349 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
4353 static int __record_deleted_ref(int num, u64 dir, int index,
4354 struct fs_path *name,
4357 struct send_ctx *sctx = ctx;
4358 return record_ref(sctx->parent_root, dir, name, ctx,
4359 &sctx->deleted_refs);
4362 static int record_new_ref(struct send_ctx *sctx)
4366 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4367 sctx->cmp_key, 0, __record_new_ref, sctx);
4376 static int record_deleted_ref(struct send_ctx *sctx)
4380 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4381 sctx->cmp_key, 0, __record_deleted_ref, sctx);
4390 struct find_ref_ctx {
4393 struct btrfs_root *root;
4394 struct fs_path *name;
4398 static int __find_iref(int num, u64 dir, int index,
4399 struct fs_path *name,
4402 struct find_ref_ctx *ctx = ctx_;
4406 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4407 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
4409 * To avoid doing extra lookups we'll only do this if everything
4412 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4416 if (dir_gen != ctx->dir_gen)
4418 ctx->found_idx = num;
4424 static int find_iref(struct btrfs_root *root,
4425 struct btrfs_path *path,
4426 struct btrfs_key *key,
4427 u64 dir, u64 dir_gen, struct fs_path *name)
4430 struct find_ref_ctx ctx;
4434 ctx.dir_gen = dir_gen;
4438 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
4442 if (ctx.found_idx == -1)
4445 return ctx.found_idx;
4448 static int __record_changed_new_ref(int num, u64 dir, int index,
4449 struct fs_path *name,
4454 struct send_ctx *sctx = ctx;
4456 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4461 ret = find_iref(sctx->parent_root, sctx->right_path,
4462 sctx->cmp_key, dir, dir_gen, name);
4464 ret = __record_new_ref(num, dir, index, name, sctx);
4471 static int __record_changed_deleted_ref(int num, u64 dir, int index,
4472 struct fs_path *name,
4477 struct send_ctx *sctx = ctx;
4479 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4484 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
4485 dir, dir_gen, name);
4487 ret = __record_deleted_ref(num, dir, index, name, sctx);
4494 static int record_changed_ref(struct send_ctx *sctx)
4498 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4499 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4502 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4503 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4513 * Record and process all refs at once. Needed when an inode changes the
4514 * generation number, which means that it was deleted and recreated.
4516 static int process_all_refs(struct send_ctx *sctx,
4517 enum btrfs_compare_tree_result cmd)
4520 struct btrfs_root *root;
4521 struct btrfs_path *path;
4522 struct btrfs_key key;
4523 struct btrfs_key found_key;
4524 struct extent_buffer *eb;
4526 iterate_inode_ref_t cb;
4527 int pending_move = 0;
4529 path = alloc_path_for_send();
4533 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4534 root = sctx->send_root;
4535 cb = __record_new_ref;
4536 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4537 root = sctx->parent_root;
4538 cb = __record_deleted_ref;
4540 btrfs_err(sctx->send_root->fs_info,
4541 "Wrong command %d in process_all_refs", cmd);
4546 key.objectid = sctx->cmp_key->objectid;
4547 key.type = BTRFS_INODE_REF_KEY;
4549 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4554 eb = path->nodes[0];
4555 slot = path->slots[0];
4556 if (slot >= btrfs_header_nritems(eb)) {
4557 ret = btrfs_next_leaf(root, path);
4565 btrfs_item_key_to_cpu(eb, &found_key, slot);
4567 if (found_key.objectid != key.objectid ||
4568 (found_key.type != BTRFS_INODE_REF_KEY &&
4569 found_key.type != BTRFS_INODE_EXTREF_KEY))
4572 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4578 btrfs_release_path(path);
4581 * We don't actually care about pending_move as we are simply
4582 * re-creating this inode and will be rename'ing it into place once we
4583 * rename the parent directory.
4585 ret = process_recorded_refs(sctx, &pending_move);
4587 btrfs_free_path(path);
4591 static int send_set_xattr(struct send_ctx *sctx,
4592 struct fs_path *path,
4593 const char *name, int name_len,
4594 const char *data, int data_len)
4598 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4602 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4603 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4604 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4606 ret = send_cmd(sctx);
4613 static int send_remove_xattr(struct send_ctx *sctx,
4614 struct fs_path *path,
4615 const char *name, int name_len)
4619 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4623 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4624 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4626 ret = send_cmd(sctx);
4633 static int __process_new_xattr(int num, struct btrfs_key *di_key,
4634 const char *name, int name_len,
4635 const char *data, int data_len,
4639 struct send_ctx *sctx = ctx;
4641 struct posix_acl_xattr_header dummy_acl;
4643 /* Capabilities are emitted by finish_inode_if_needed */
4644 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4647 p = fs_path_alloc();
4652 * This hack is needed because empty acls are stored as zero byte
4653 * data in xattrs. Problem with that is, that receiving these zero byte
4654 * acls will fail later. To fix this, we send a dummy acl list that
4655 * only contains the version number and no entries.
4657 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4658 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4659 if (data_len == 0) {
4660 dummy_acl.a_version =
4661 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4662 data = (char *)&dummy_acl;
4663 data_len = sizeof(dummy_acl);
4667 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4671 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4678 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4679 const char *name, int name_len,
4680 const char *data, int data_len,
4684 struct send_ctx *sctx = ctx;
4687 p = fs_path_alloc();
4691 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4695 ret = send_remove_xattr(sctx, p, name, name_len);
4702 static int process_new_xattr(struct send_ctx *sctx)
4706 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4707 __process_new_xattr, sctx);
4712 static int process_deleted_xattr(struct send_ctx *sctx)
4714 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4715 __process_deleted_xattr, sctx);
4718 struct find_xattr_ctx {
4726 static int __find_xattr(int num, struct btrfs_key *di_key,
4727 const char *name, int name_len,
4728 const char *data, int data_len,
4729 u8 type, void *vctx)
4731 struct find_xattr_ctx *ctx = vctx;
4733 if (name_len == ctx->name_len &&
4734 strncmp(name, ctx->name, name_len) == 0) {
4735 ctx->found_idx = num;
4736 ctx->found_data_len = data_len;
4737 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4738 if (!ctx->found_data)
4745 static int find_xattr(struct btrfs_root *root,
4746 struct btrfs_path *path,
4747 struct btrfs_key *key,
4748 const char *name, int name_len,
4749 char **data, int *data_len)
4752 struct find_xattr_ctx ctx;
4755 ctx.name_len = name_len;
4757 ctx.found_data = NULL;
4758 ctx.found_data_len = 0;
4760 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
4764 if (ctx.found_idx == -1)
4767 *data = ctx.found_data;
4768 *data_len = ctx.found_data_len;
4770 kfree(ctx.found_data);
4772 return ctx.found_idx;
4776 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4777 const char *name, int name_len,
4778 const char *data, int data_len,
4782 struct send_ctx *sctx = ctx;
4783 char *found_data = NULL;
4784 int found_data_len = 0;
4786 ret = find_xattr(sctx->parent_root, sctx->right_path,
4787 sctx->cmp_key, name, name_len, &found_data,
4789 if (ret == -ENOENT) {
4790 ret = __process_new_xattr(num, di_key, name, name_len, data,
4791 data_len, type, ctx);
4792 } else if (ret >= 0) {
4793 if (data_len != found_data_len ||
4794 memcmp(data, found_data, data_len)) {
4795 ret = __process_new_xattr(num, di_key, name, name_len,
4796 data, data_len, type, ctx);
4806 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4807 const char *name, int name_len,
4808 const char *data, int data_len,
4812 struct send_ctx *sctx = ctx;
4814 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4815 name, name_len, NULL, NULL);
4817 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4818 data_len, type, ctx);
4825 static int process_changed_xattr(struct send_ctx *sctx)
4829 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4830 __process_changed_new_xattr, sctx);
4833 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4834 __process_changed_deleted_xattr, sctx);
4840 static int process_all_new_xattrs(struct send_ctx *sctx)
4843 struct btrfs_root *root;
4844 struct btrfs_path *path;
4845 struct btrfs_key key;
4846 struct btrfs_key found_key;
4847 struct extent_buffer *eb;
4850 path = alloc_path_for_send();
4854 root = sctx->send_root;
4856 key.objectid = sctx->cmp_key->objectid;
4857 key.type = BTRFS_XATTR_ITEM_KEY;
4859 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4864 eb = path->nodes[0];
4865 slot = path->slots[0];
4866 if (slot >= btrfs_header_nritems(eb)) {
4867 ret = btrfs_next_leaf(root, path);
4870 } else if (ret > 0) {
4877 btrfs_item_key_to_cpu(eb, &found_key, slot);
4878 if (found_key.objectid != key.objectid ||
4879 found_key.type != key.type) {
4884 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
4892 btrfs_free_path(path);
4896 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4898 struct btrfs_root *root = sctx->send_root;
4899 struct btrfs_fs_info *fs_info = root->fs_info;
4900 struct inode *inode;
4903 struct btrfs_key key;
4904 pgoff_t index = offset >> PAGE_SHIFT;
4906 unsigned pg_offset = offset & ~PAGE_MASK;
4909 key.objectid = sctx->cur_ino;
4910 key.type = BTRFS_INODE_ITEM_KEY;
4913 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4915 return PTR_ERR(inode);
4917 if (offset + len > i_size_read(inode)) {
4918 if (offset > i_size_read(inode))
4921 len = offset - i_size_read(inode);
4926 last_index = (offset + len - 1) >> PAGE_SHIFT;
4928 /* initial readahead */
4929 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4930 file_ra_state_init(&sctx->ra, inode->i_mapping);
4932 while (index <= last_index) {
4933 unsigned cur_len = min_t(unsigned, len,
4934 PAGE_SIZE - pg_offset);
4936 page = find_lock_page(inode->i_mapping, index);
4938 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4939 NULL, index, last_index + 1 - index);
4941 page = find_or_create_page(inode->i_mapping, index,
4949 if (PageReadahead(page)) {
4950 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4951 NULL, page, index, last_index + 1 - index);
4954 if (!PageUptodate(page)) {
4955 btrfs_readpage(NULL, page);
4957 if (!PageUptodate(page)) {
4966 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4981 * Read some bytes from the current inode/file and send a write command to
4984 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4986 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
4989 ssize_t num_read = 0;
4991 p = fs_path_alloc();
4995 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
4997 num_read = fill_read_buf(sctx, offset, len);
4998 if (num_read <= 0) {
5004 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5008 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5012 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5013 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5014 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
5016 ret = send_cmd(sctx);
5027 * Send a clone command to user space.
5029 static int send_clone(struct send_ctx *sctx,
5030 u64 offset, u32 len,
5031 struct clone_root *clone_root)
5037 btrfs_debug(sctx->send_root->fs_info,
5038 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
5039 offset, len, clone_root->root->objectid, clone_root->ino,
5040 clone_root->offset);
5042 p = fs_path_alloc();
5046 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5050 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5054 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5055 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5056 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5058 if (clone_root->root == sctx->send_root) {
5059 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
5060 &gen, NULL, NULL, NULL, NULL);
5063 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5065 ret = get_inode_path(clone_root->root, clone_root->ino, p);
5071 * If the parent we're using has a received_uuid set then use that as
5072 * our clone source as that is what we will look for when doing a
5075 * This covers the case that we create a snapshot off of a received
5076 * subvolume and then use that as the parent and try to receive on a
5079 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5080 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5081 clone_root->root->root_item.received_uuid);
5083 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5084 clone_root->root->root_item.uuid);
5085 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
5086 le64_to_cpu(clone_root->root->root_item.ctransid));
5087 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5088 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5089 clone_root->offset);
5091 ret = send_cmd(sctx);
5100 * Send an update extent command to user space.
5102 static int send_update_extent(struct send_ctx *sctx,
5103 u64 offset, u32 len)
5108 p = fs_path_alloc();
5112 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5116 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5120 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5121 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5122 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5124 ret = send_cmd(sctx);
5132 static int send_hole(struct send_ctx *sctx, u64 end)
5134 struct fs_path *p = NULL;
5135 u64 offset = sctx->cur_inode_last_extent;
5140 * A hole that starts at EOF or beyond it. Since we do not yet support
5141 * fallocate (for extent preallocation and hole punching), sending a
5142 * write of zeroes starting at EOF or beyond would later require issuing
5143 * a truncate operation which would undo the write and achieve nothing.
5145 if (offset >= sctx->cur_inode_size)
5149 * Don't go beyond the inode's i_size due to prealloc extents that start
5152 end = min_t(u64, end, sctx->cur_inode_size);
5154 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5155 return send_update_extent(sctx, offset, end - offset);
5157 p = fs_path_alloc();
5160 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5162 goto tlv_put_failure;
5163 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
5164 while (offset < end) {
5165 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
5167 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5170 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5171 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5172 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
5173 ret = send_cmd(sctx);
5178 sctx->cur_inode_next_write_offset = offset;
5184 static int send_extent_data(struct send_ctx *sctx,
5190 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5191 return send_update_extent(sctx, offset, len);
5193 while (sent < len) {
5194 u64 size = len - sent;
5197 if (size > BTRFS_SEND_READ_SIZE)
5198 size = BTRFS_SEND_READ_SIZE;
5199 ret = send_write(sctx, offset + sent, size);
5210 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5211 * found, call send_set_xattr function to emit it.
5213 * Return 0 if there isn't a capability, or when the capability was emitted
5214 * successfully, or < 0 if an error occurred.
5216 static int send_capabilities(struct send_ctx *sctx)
5218 struct fs_path *fspath = NULL;
5219 struct btrfs_path *path;
5220 struct btrfs_dir_item *di;
5221 struct extent_buffer *leaf;
5222 unsigned long data_ptr;
5227 path = alloc_path_for_send();
5231 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5232 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5234 /* There is no xattr for this inode */
5236 } else if (IS_ERR(di)) {
5241 leaf = path->nodes[0];
5242 buf_len = btrfs_dir_data_len(leaf, di);
5244 fspath = fs_path_alloc();
5245 buf = kmalloc(buf_len, GFP_KERNEL);
5246 if (!fspath || !buf) {
5251 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5255 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5256 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5258 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5259 strlen(XATTR_NAME_CAPS), buf, buf_len);
5262 fs_path_free(fspath);
5263 btrfs_free_path(path);
5267 static int clone_range(struct send_ctx *sctx,
5268 struct clone_root *clone_root,
5269 const u64 disk_byte,
5274 struct btrfs_path *path;
5275 struct btrfs_key key;
5279 * Prevent cloning from a zero offset with a length matching the sector
5280 * size because in some scenarios this will make the receiver fail.
5282 * For example, if in the source filesystem the extent at offset 0
5283 * has a length of sectorsize and it was written using direct IO, then
5284 * it can never be an inline extent (even if compression is enabled).
5285 * Then this extent can be cloned in the original filesystem to a non
5286 * zero file offset, but it may not be possible to clone in the
5287 * destination filesystem because it can be inlined due to compression
5288 * on the destination filesystem (as the receiver's write operations are
5289 * always done using buffered IO). The same happens when the original
5290 * filesystem does not have compression enabled but the destination
5293 if (clone_root->offset == 0 &&
5294 len == sctx->send_root->fs_info->sectorsize)
5295 return send_extent_data(sctx, offset, len);
5297 path = alloc_path_for_send();
5302 * We can't send a clone operation for the entire range if we find
5303 * extent items in the respective range in the source file that
5304 * refer to different extents or if we find holes.
5305 * So check for that and do a mix of clone and regular write/copy
5306 * operations if needed.
5310 * mkfs.btrfs -f /dev/sda
5311 * mount /dev/sda /mnt
5312 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5313 * cp --reflink=always /mnt/foo /mnt/bar
5314 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5315 * btrfs subvolume snapshot -r /mnt /mnt/snap
5317 * If when we send the snapshot and we are processing file bar (which
5318 * has a higher inode number than foo) we blindly send a clone operation
5319 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5320 * a file bar that matches the content of file foo - iow, doesn't match
5321 * the content from bar in the original filesystem.
5323 key.objectid = clone_root->ino;
5324 key.type = BTRFS_EXTENT_DATA_KEY;
5325 key.offset = clone_root->offset;
5326 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5329 if (ret > 0 && path->slots[0] > 0) {
5330 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5331 if (key.objectid == clone_root->ino &&
5332 key.type == BTRFS_EXTENT_DATA_KEY)
5337 struct extent_buffer *leaf = path->nodes[0];
5338 int slot = path->slots[0];
5339 struct btrfs_file_extent_item *ei;
5344 if (slot >= btrfs_header_nritems(leaf)) {
5345 ret = btrfs_next_leaf(clone_root->root, path);
5353 btrfs_item_key_to_cpu(leaf, &key, slot);
5356 * We might have an implicit trailing hole (NO_HOLES feature
5357 * enabled). We deal with it after leaving this loop.
5359 if (key.objectid != clone_root->ino ||
5360 key.type != BTRFS_EXTENT_DATA_KEY)
5363 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5364 type = btrfs_file_extent_type(leaf, ei);
5365 if (type == BTRFS_FILE_EXTENT_INLINE) {
5366 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
5367 ext_len = PAGE_ALIGN(ext_len);
5369 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5372 if (key.offset + ext_len <= clone_root->offset)
5375 if (key.offset > clone_root->offset) {
5376 /* Implicit hole, NO_HOLES feature enabled. */
5377 u64 hole_len = key.offset - clone_root->offset;
5381 ret = send_extent_data(sctx, offset, hole_len);
5389 clone_root->offset += hole_len;
5390 data_offset += hole_len;
5393 if (key.offset >= clone_root->offset + len)
5396 clone_len = min_t(u64, ext_len, len);
5398 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5399 btrfs_file_extent_offset(leaf, ei) == data_offset)
5400 ret = send_clone(sctx, offset, clone_len, clone_root);
5402 ret = send_extent_data(sctx, offset, clone_len);
5410 offset += clone_len;
5411 clone_root->offset += clone_len;
5412 data_offset += clone_len;
5418 ret = send_extent_data(sctx, offset, len);
5422 btrfs_free_path(path);
5426 static int send_write_or_clone(struct send_ctx *sctx,
5427 struct btrfs_path *path,
5428 struct btrfs_key *key,
5429 struct clone_root *clone_root)
5432 struct btrfs_file_extent_item *ei;
5433 u64 offset = key->offset;
5436 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
5438 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5439 struct btrfs_file_extent_item);
5440 type = btrfs_file_extent_type(path->nodes[0], ei);
5441 if (type == BTRFS_FILE_EXTENT_INLINE) {
5442 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
5444 * it is possible the inline item won't cover the whole page,
5445 * but there may be items after this page. Make
5446 * sure to send the whole thing
5448 len = PAGE_ALIGN(len);
5450 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
5453 if (offset >= sctx->cur_inode_size) {
5457 if (offset + len > sctx->cur_inode_size)
5458 len = sctx->cur_inode_size - offset;
5464 if (clone_root && IS_ALIGNED(offset + len, bs)) {
5468 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5469 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5470 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5473 ret = send_extent_data(sctx, offset, len);
5475 sctx->cur_inode_next_write_offset = offset + len;
5480 static int is_extent_unchanged(struct send_ctx *sctx,
5481 struct btrfs_path *left_path,
5482 struct btrfs_key *ekey)
5485 struct btrfs_key key;
5486 struct btrfs_path *path = NULL;
5487 struct extent_buffer *eb;
5489 struct btrfs_key found_key;
5490 struct btrfs_file_extent_item *ei;
5495 u64 left_offset_fixed;
5503 path = alloc_path_for_send();
5507 eb = left_path->nodes[0];
5508 slot = left_path->slots[0];
5509 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5510 left_type = btrfs_file_extent_type(eb, ei);
5512 if (left_type != BTRFS_FILE_EXTENT_REG) {
5516 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5517 left_len = btrfs_file_extent_num_bytes(eb, ei);
5518 left_offset = btrfs_file_extent_offset(eb, ei);
5519 left_gen = btrfs_file_extent_generation(eb, ei);
5522 * Following comments will refer to these graphics. L is the left
5523 * extents which we are checking at the moment. 1-8 are the right
5524 * extents that we iterate.
5527 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5530 * |--1--|-2b-|...(same as above)
5532 * Alternative situation. Happens on files where extents got split.
5534 * |-----------7-----------|-6-|
5536 * Alternative situation. Happens on files which got larger.
5539 * Nothing follows after 8.
5542 key.objectid = ekey->objectid;
5543 key.type = BTRFS_EXTENT_DATA_KEY;
5544 key.offset = ekey->offset;
5545 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5554 * Handle special case where the right side has no extents at all.
5556 eb = path->nodes[0];
5557 slot = path->slots[0];
5558 btrfs_item_key_to_cpu(eb, &found_key, slot);
5559 if (found_key.objectid != key.objectid ||
5560 found_key.type != key.type) {
5561 /* If we're a hole then just pretend nothing changed */
5562 ret = (left_disknr) ? 0 : 1;
5567 * We're now on 2a, 2b or 7.
5570 while (key.offset < ekey->offset + left_len) {
5571 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5572 right_type = btrfs_file_extent_type(eb, ei);
5573 if (right_type != BTRFS_FILE_EXTENT_REG &&
5574 right_type != BTRFS_FILE_EXTENT_INLINE) {
5579 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5580 right_len = btrfs_file_extent_ram_bytes(eb, ei);
5581 right_len = PAGE_ALIGN(right_len);
5583 right_len = btrfs_file_extent_num_bytes(eb, ei);
5587 * Are we at extent 8? If yes, we know the extent is changed.
5588 * This may only happen on the first iteration.
5590 if (found_key.offset + right_len <= ekey->offset) {
5591 /* If we're a hole just pretend nothing changed */
5592 ret = (left_disknr) ? 0 : 1;
5597 * We just wanted to see if when we have an inline extent, what
5598 * follows it is a regular extent (wanted to check the above
5599 * condition for inline extents too). This should normally not
5600 * happen but it's possible for example when we have an inline
5601 * compressed extent representing data with a size matching
5602 * the page size (currently the same as sector size).
5604 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5609 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5610 right_offset = btrfs_file_extent_offset(eb, ei);
5611 right_gen = btrfs_file_extent_generation(eb, ei);
5613 left_offset_fixed = left_offset;
5614 if (key.offset < ekey->offset) {
5615 /* Fix the right offset for 2a and 7. */
5616 right_offset += ekey->offset - key.offset;
5618 /* Fix the left offset for all behind 2a and 2b */
5619 left_offset_fixed += key.offset - ekey->offset;
5623 * Check if we have the same extent.
5625 if (left_disknr != right_disknr ||
5626 left_offset_fixed != right_offset ||
5627 left_gen != right_gen) {
5633 * Go to the next extent.
5635 ret = btrfs_next_item(sctx->parent_root, path);
5639 eb = path->nodes[0];
5640 slot = path->slots[0];
5641 btrfs_item_key_to_cpu(eb, &found_key, slot);
5643 if (ret || found_key.objectid != key.objectid ||
5644 found_key.type != key.type) {
5645 key.offset += right_len;
5648 if (found_key.offset != key.offset + right_len) {
5656 * We're now behind the left extent (treat as unchanged) or at the end
5657 * of the right side (treat as changed).
5659 if (key.offset >= ekey->offset + left_len)
5666 btrfs_free_path(path);
5670 static int get_last_extent(struct send_ctx *sctx, u64 offset)
5672 struct btrfs_path *path;
5673 struct btrfs_root *root = sctx->send_root;
5674 struct btrfs_file_extent_item *fi;
5675 struct btrfs_key key;
5680 path = alloc_path_for_send();
5684 sctx->cur_inode_last_extent = 0;
5686 key.objectid = sctx->cur_ino;
5687 key.type = BTRFS_EXTENT_DATA_KEY;
5688 key.offset = offset;
5689 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5693 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5694 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5697 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5698 struct btrfs_file_extent_item);
5699 type = btrfs_file_extent_type(path->nodes[0], fi);
5700 if (type == BTRFS_FILE_EXTENT_INLINE) {
5701 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
5702 extent_end = ALIGN(key.offset + size,
5703 sctx->send_root->fs_info->sectorsize);
5705 extent_end = key.offset +
5706 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5708 sctx->cur_inode_last_extent = extent_end;
5710 btrfs_free_path(path);
5714 static int range_is_hole_in_parent(struct send_ctx *sctx,
5718 struct btrfs_path *path;
5719 struct btrfs_key key;
5720 struct btrfs_root *root = sctx->parent_root;
5721 u64 search_start = start;
5724 path = alloc_path_for_send();
5728 key.objectid = sctx->cur_ino;
5729 key.type = BTRFS_EXTENT_DATA_KEY;
5730 key.offset = search_start;
5731 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5734 if (ret > 0 && path->slots[0] > 0)
5737 while (search_start < end) {
5738 struct extent_buffer *leaf = path->nodes[0];
5739 int slot = path->slots[0];
5740 struct btrfs_file_extent_item *fi;
5743 if (slot >= btrfs_header_nritems(leaf)) {
5744 ret = btrfs_next_leaf(root, path);
5752 btrfs_item_key_to_cpu(leaf, &key, slot);
5753 if (key.objectid < sctx->cur_ino ||
5754 key.type < BTRFS_EXTENT_DATA_KEY)
5756 if (key.objectid > sctx->cur_ino ||
5757 key.type > BTRFS_EXTENT_DATA_KEY ||
5761 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5762 if (btrfs_file_extent_type(leaf, fi) ==
5763 BTRFS_FILE_EXTENT_INLINE) {
5764 u64 size = btrfs_file_extent_ram_bytes(leaf, fi);
5766 extent_end = ALIGN(key.offset + size,
5767 root->fs_info->sectorsize);
5769 extent_end = key.offset +
5770 btrfs_file_extent_num_bytes(leaf, fi);
5772 if (extent_end <= start)
5774 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5775 search_start = extent_end;
5785 btrfs_free_path(path);
5789 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5790 struct btrfs_key *key)
5792 struct btrfs_file_extent_item *fi;
5797 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5800 if (sctx->cur_inode_last_extent == (u64)-1) {
5801 ret = get_last_extent(sctx, key->offset - 1);
5806 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5807 struct btrfs_file_extent_item);
5808 type = btrfs_file_extent_type(path->nodes[0], fi);
5809 if (type == BTRFS_FILE_EXTENT_INLINE) {
5810 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
5811 extent_end = ALIGN(key->offset + size,
5812 sctx->send_root->fs_info->sectorsize);
5814 extent_end = key->offset +
5815 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5818 if (path->slots[0] == 0 &&
5819 sctx->cur_inode_last_extent < key->offset) {
5821 * We might have skipped entire leafs that contained only
5822 * file extent items for our current inode. These leafs have
5823 * a generation number smaller (older) than the one in the
5824 * current leaf and the leaf our last extent came from, and
5825 * are located between these 2 leafs.
5827 ret = get_last_extent(sctx, key->offset - 1);
5832 if (sctx->cur_inode_last_extent < key->offset) {
5833 ret = range_is_hole_in_parent(sctx,
5834 sctx->cur_inode_last_extent,
5839 ret = send_hole(sctx, key->offset);
5843 sctx->cur_inode_last_extent = extent_end;
5847 static int process_extent(struct send_ctx *sctx,
5848 struct btrfs_path *path,
5849 struct btrfs_key *key)
5851 struct clone_root *found_clone = NULL;
5854 if (S_ISLNK(sctx->cur_inode_mode))
5857 if (sctx->parent_root && !sctx->cur_inode_new) {
5858 ret = is_extent_unchanged(sctx, path, key);
5866 struct btrfs_file_extent_item *ei;
5869 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5870 struct btrfs_file_extent_item);
5871 type = btrfs_file_extent_type(path->nodes[0], ei);
5872 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5873 type == BTRFS_FILE_EXTENT_REG) {
5875 * The send spec does not have a prealloc command yet,
5876 * so just leave a hole for prealloc'ed extents until
5877 * we have enough commands queued up to justify rev'ing
5880 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5885 /* Have a hole, just skip it. */
5886 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5893 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5894 sctx->cur_inode_size, &found_clone);
5895 if (ret != -ENOENT && ret < 0)
5898 ret = send_write_or_clone(sctx, path, key, found_clone);
5902 ret = maybe_send_hole(sctx, path, key);
5907 static int process_all_extents(struct send_ctx *sctx)
5910 struct btrfs_root *root;
5911 struct btrfs_path *path;
5912 struct btrfs_key key;
5913 struct btrfs_key found_key;
5914 struct extent_buffer *eb;
5917 root = sctx->send_root;
5918 path = alloc_path_for_send();
5922 key.objectid = sctx->cmp_key->objectid;
5923 key.type = BTRFS_EXTENT_DATA_KEY;
5925 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5930 eb = path->nodes[0];
5931 slot = path->slots[0];
5933 if (slot >= btrfs_header_nritems(eb)) {
5934 ret = btrfs_next_leaf(root, path);
5937 } else if (ret > 0) {
5944 btrfs_item_key_to_cpu(eb, &found_key, slot);
5946 if (found_key.objectid != key.objectid ||
5947 found_key.type != key.type) {
5952 ret = process_extent(sctx, path, &found_key);
5960 btrfs_free_path(path);
5964 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5966 int *refs_processed)
5970 if (sctx->cur_ino == 0)
5972 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
5973 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
5975 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5978 ret = process_recorded_refs(sctx, pending_move);
5982 *refs_processed = 1;
5987 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5998 int need_truncate = 1;
5999 int pending_move = 0;
6000 int refs_processed = 0;
6002 if (sctx->ignore_cur_inode)
6005 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6011 * We have processed the refs and thus need to advance send_progress.
6012 * Now, calls to get_cur_xxx will take the updated refs of the current
6013 * inode into account.
6015 * On the other hand, if our current inode is a directory and couldn't
6016 * be moved/renamed because its parent was renamed/moved too and it has
6017 * a higher inode number, we can only move/rename our current inode
6018 * after we moved/renamed its parent. Therefore in this case operate on
6019 * the old path (pre move/rename) of our current inode, and the
6020 * move/rename will be performed later.
6022 if (refs_processed && !pending_move)
6023 sctx->send_progress = sctx->cur_ino + 1;
6025 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6027 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6030 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
6031 &left_mode, &left_uid, &left_gid, NULL);
6035 if (!sctx->parent_root || sctx->cur_inode_new) {
6037 if (!S_ISLNK(sctx->cur_inode_mode))
6039 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6044 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
6045 &old_size, NULL, &right_mode, &right_uid,
6050 if (left_uid != right_uid || left_gid != right_gid)
6052 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6054 if ((old_size == sctx->cur_inode_size) ||
6055 (sctx->cur_inode_size > old_size &&
6056 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6060 if (S_ISREG(sctx->cur_inode_mode)) {
6061 if (need_send_hole(sctx)) {
6062 if (sctx->cur_inode_last_extent == (u64)-1 ||
6063 sctx->cur_inode_last_extent <
6064 sctx->cur_inode_size) {
6065 ret = get_last_extent(sctx, (u64)-1);
6069 if (sctx->cur_inode_last_extent <
6070 sctx->cur_inode_size) {
6071 ret = send_hole(sctx, sctx->cur_inode_size);
6076 if (need_truncate) {
6077 ret = send_truncate(sctx, sctx->cur_ino,
6078 sctx->cur_inode_gen,
6079 sctx->cur_inode_size);
6086 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6087 left_uid, left_gid);
6092 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6098 ret = send_capabilities(sctx);
6103 * If other directory inodes depended on our current directory
6104 * inode's move/rename, now do their move/rename operations.
6106 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6107 ret = apply_children_dir_moves(sctx);
6111 * Need to send that every time, no matter if it actually
6112 * changed between the two trees as we have done changes to
6113 * the inode before. If our inode is a directory and it's
6114 * waiting to be moved/renamed, we will send its utimes when
6115 * it's moved/renamed, therefore we don't need to do it here.
6117 sctx->send_progress = sctx->cur_ino + 1;
6118 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6127 struct parent_paths_ctx {
6128 struct list_head *refs;
6129 struct send_ctx *sctx;
6132 static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6135 struct parent_paths_ctx *ppctx = ctx;
6137 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6142 * Issue unlink operations for all paths of the current inode found in the
6145 static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6147 LIST_HEAD(deleted_refs);
6148 struct btrfs_path *path;
6149 struct btrfs_key key;
6150 struct parent_paths_ctx ctx;
6153 path = alloc_path_for_send();
6157 key.objectid = sctx->cur_ino;
6158 key.type = BTRFS_INODE_REF_KEY;
6160 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
6164 ctx.refs = &deleted_refs;
6168 struct extent_buffer *eb = path->nodes[0];
6169 int slot = path->slots[0];
6171 if (slot >= btrfs_header_nritems(eb)) {
6172 ret = btrfs_next_leaf(sctx->parent_root, path);
6180 btrfs_item_key_to_cpu(eb, &key, slot);
6181 if (key.objectid != sctx->cur_ino)
6183 if (key.type != BTRFS_INODE_REF_KEY &&
6184 key.type != BTRFS_INODE_EXTREF_KEY)
6187 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
6188 record_parent_ref, &ctx);
6195 while (!list_empty(&deleted_refs)) {
6196 struct recorded_ref *ref;
6198 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6199 ret = send_unlink(sctx, ref->full_path);
6202 fs_path_free(ref->full_path);
6203 list_del(&ref->list);
6208 btrfs_free_path(path);
6210 __free_recorded_refs(&deleted_refs);
6214 static int changed_inode(struct send_ctx *sctx,
6215 enum btrfs_compare_tree_result result)
6218 struct btrfs_key *key = sctx->cmp_key;
6219 struct btrfs_inode_item *left_ii = NULL;
6220 struct btrfs_inode_item *right_ii = NULL;
6224 sctx->cur_ino = key->objectid;
6225 sctx->cur_inode_new_gen = 0;
6226 sctx->cur_inode_last_extent = (u64)-1;
6227 sctx->cur_inode_next_write_offset = 0;
6228 sctx->ignore_cur_inode = false;
6231 * Set send_progress to current inode. This will tell all get_cur_xxx
6232 * functions that the current inode's refs are not updated yet. Later,
6233 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6235 sctx->send_progress = sctx->cur_ino;
6237 if (result == BTRFS_COMPARE_TREE_NEW ||
6238 result == BTRFS_COMPARE_TREE_CHANGED) {
6239 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6240 sctx->left_path->slots[0],
6241 struct btrfs_inode_item);
6242 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6245 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6246 sctx->right_path->slots[0],
6247 struct btrfs_inode_item);
6248 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6251 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6252 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6253 sctx->right_path->slots[0],
6254 struct btrfs_inode_item);
6256 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6260 * The cur_ino = root dir case is special here. We can't treat
6261 * the inode as deleted+reused because it would generate a
6262 * stream that tries to delete/mkdir the root dir.
6264 if (left_gen != right_gen &&
6265 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6266 sctx->cur_inode_new_gen = 1;
6270 * Normally we do not find inodes with a link count of zero (orphans)
6271 * because the most common case is to create a snapshot and use it
6272 * for a send operation. However other less common use cases involve
6273 * using a subvolume and send it after turning it to RO mode just
6274 * after deleting all hard links of a file while holding an open
6275 * file descriptor against it or turning a RO snapshot into RW mode,
6276 * keep an open file descriptor against a file, delete it and then
6277 * turn the snapshot back to RO mode before using it for a send
6278 * operation. So if we find such cases, ignore the inode and all its
6279 * items completely if it's a new inode, or if it's a changed inode
6280 * make sure all its previous paths (from the parent snapshot) are all
6281 * unlinked and all other the inode items are ignored.
6283 if (result == BTRFS_COMPARE_TREE_NEW ||
6284 result == BTRFS_COMPARE_TREE_CHANGED) {
6287 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6289 sctx->ignore_cur_inode = true;
6290 if (result == BTRFS_COMPARE_TREE_CHANGED)
6291 ret = btrfs_unlink_all_paths(sctx);
6296 if (result == BTRFS_COMPARE_TREE_NEW) {
6297 sctx->cur_inode_gen = left_gen;
6298 sctx->cur_inode_new = 1;
6299 sctx->cur_inode_deleted = 0;
6300 sctx->cur_inode_size = btrfs_inode_size(
6301 sctx->left_path->nodes[0], left_ii);
6302 sctx->cur_inode_mode = btrfs_inode_mode(
6303 sctx->left_path->nodes[0], left_ii);
6304 sctx->cur_inode_rdev = btrfs_inode_rdev(
6305 sctx->left_path->nodes[0], left_ii);
6306 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6307 ret = send_create_inode_if_needed(sctx);
6308 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6309 sctx->cur_inode_gen = right_gen;
6310 sctx->cur_inode_new = 0;
6311 sctx->cur_inode_deleted = 1;
6312 sctx->cur_inode_size = btrfs_inode_size(
6313 sctx->right_path->nodes[0], right_ii);
6314 sctx->cur_inode_mode = btrfs_inode_mode(
6315 sctx->right_path->nodes[0], right_ii);
6316 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
6318 * We need to do some special handling in case the inode was
6319 * reported as changed with a changed generation number. This
6320 * means that the original inode was deleted and new inode
6321 * reused the same inum. So we have to treat the old inode as
6322 * deleted and the new one as new.
6324 if (sctx->cur_inode_new_gen) {
6326 * First, process the inode as if it was deleted.
6328 sctx->cur_inode_gen = right_gen;
6329 sctx->cur_inode_new = 0;
6330 sctx->cur_inode_deleted = 1;
6331 sctx->cur_inode_size = btrfs_inode_size(
6332 sctx->right_path->nodes[0], right_ii);
6333 sctx->cur_inode_mode = btrfs_inode_mode(
6334 sctx->right_path->nodes[0], right_ii);
6335 ret = process_all_refs(sctx,
6336 BTRFS_COMPARE_TREE_DELETED);
6341 * Now process the inode as if it was new.
6343 sctx->cur_inode_gen = left_gen;
6344 sctx->cur_inode_new = 1;
6345 sctx->cur_inode_deleted = 0;
6346 sctx->cur_inode_size = btrfs_inode_size(
6347 sctx->left_path->nodes[0], left_ii);
6348 sctx->cur_inode_mode = btrfs_inode_mode(
6349 sctx->left_path->nodes[0], left_ii);
6350 sctx->cur_inode_rdev = btrfs_inode_rdev(
6351 sctx->left_path->nodes[0], left_ii);
6352 ret = send_create_inode_if_needed(sctx);
6356 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6360 * Advance send_progress now as we did not get into
6361 * process_recorded_refs_if_needed in the new_gen case.
6363 sctx->send_progress = sctx->cur_ino + 1;
6366 * Now process all extents and xattrs of the inode as if
6367 * they were all new.
6369 ret = process_all_extents(sctx);
6372 ret = process_all_new_xattrs(sctx);
6376 sctx->cur_inode_gen = left_gen;
6377 sctx->cur_inode_new = 0;
6378 sctx->cur_inode_new_gen = 0;
6379 sctx->cur_inode_deleted = 0;
6380 sctx->cur_inode_size = btrfs_inode_size(
6381 sctx->left_path->nodes[0], left_ii);
6382 sctx->cur_inode_mode = btrfs_inode_mode(
6383 sctx->left_path->nodes[0], left_ii);
6392 * We have to process new refs before deleted refs, but compare_trees gives us
6393 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6394 * first and later process them in process_recorded_refs.
6395 * For the cur_inode_new_gen case, we skip recording completely because
6396 * changed_inode did already initiate processing of refs. The reason for this is
6397 * that in this case, compare_tree actually compares the refs of 2 different
6398 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6399 * refs of the right tree as deleted and all refs of the left tree as new.
6401 static int changed_ref(struct send_ctx *sctx,
6402 enum btrfs_compare_tree_result result)
6406 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6407 inconsistent_snapshot_error(sctx, result, "reference");
6411 if (!sctx->cur_inode_new_gen &&
6412 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6413 if (result == BTRFS_COMPARE_TREE_NEW)
6414 ret = record_new_ref(sctx);
6415 else if (result == BTRFS_COMPARE_TREE_DELETED)
6416 ret = record_deleted_ref(sctx);
6417 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6418 ret = record_changed_ref(sctx);
6425 * Process new/deleted/changed xattrs. We skip processing in the
6426 * cur_inode_new_gen case because changed_inode did already initiate processing
6427 * of xattrs. The reason is the same as in changed_ref
6429 static int changed_xattr(struct send_ctx *sctx,
6430 enum btrfs_compare_tree_result result)
6434 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6435 inconsistent_snapshot_error(sctx, result, "xattr");
6439 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6440 if (result == BTRFS_COMPARE_TREE_NEW)
6441 ret = process_new_xattr(sctx);
6442 else if (result == BTRFS_COMPARE_TREE_DELETED)
6443 ret = process_deleted_xattr(sctx);
6444 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6445 ret = process_changed_xattr(sctx);
6452 * Process new/deleted/changed extents. We skip processing in the
6453 * cur_inode_new_gen case because changed_inode did already initiate processing
6454 * of extents. The reason is the same as in changed_ref
6456 static int changed_extent(struct send_ctx *sctx,
6457 enum btrfs_compare_tree_result result)
6462 * We have found an extent item that changed without the inode item
6463 * having changed. This can happen either after relocation (where the
6464 * disk_bytenr of an extent item is replaced at
6465 * relocation.c:replace_file_extents()) or after deduplication into a
6466 * file in both the parent and send snapshots (where an extent item can
6467 * get modified or replaced with a new one). Note that deduplication
6468 * updates the inode item, but it only changes the iversion (sequence
6469 * field in the inode item) of the inode, so if a file is deduplicated
6470 * the same amount of times in both the parent and send snapshots, its
6471 * iversion becames the same in both snapshots, whence the inode item is
6472 * the same on both snapshots.
6474 if (sctx->cur_ino != sctx->cmp_key->objectid)
6477 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6478 if (result != BTRFS_COMPARE_TREE_DELETED)
6479 ret = process_extent(sctx, sctx->left_path,
6486 static int dir_changed(struct send_ctx *sctx, u64 dir)
6488 u64 orig_gen, new_gen;
6491 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6496 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6501 return (orig_gen != new_gen) ? 1 : 0;
6504 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6505 struct btrfs_key *key)
6507 struct btrfs_inode_extref *extref;
6508 struct extent_buffer *leaf;
6509 u64 dirid = 0, last_dirid = 0;
6516 /* Easy case, just check this one dirid */
6517 if (key->type == BTRFS_INODE_REF_KEY) {
6518 dirid = key->offset;
6520 ret = dir_changed(sctx, dirid);
6524 leaf = path->nodes[0];
6525 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6526 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6527 while (cur_offset < item_size) {
6528 extref = (struct btrfs_inode_extref *)(ptr +
6530 dirid = btrfs_inode_extref_parent(leaf, extref);
6531 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6532 cur_offset += ref_name_len + sizeof(*extref);
6533 if (dirid == last_dirid)
6535 ret = dir_changed(sctx, dirid);
6545 * Updates compare related fields in sctx and simply forwards to the actual
6546 * changed_xxx functions.
6548 static int changed_cb(struct btrfs_path *left_path,
6549 struct btrfs_path *right_path,
6550 struct btrfs_key *key,
6551 enum btrfs_compare_tree_result result,
6555 struct send_ctx *sctx = ctx;
6557 if (result == BTRFS_COMPARE_TREE_SAME) {
6558 if (key->type == BTRFS_INODE_REF_KEY ||
6559 key->type == BTRFS_INODE_EXTREF_KEY) {
6560 ret = compare_refs(sctx, left_path, key);
6565 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6566 return maybe_send_hole(sctx, left_path, key);
6570 result = BTRFS_COMPARE_TREE_CHANGED;
6574 sctx->left_path = left_path;
6575 sctx->right_path = right_path;
6576 sctx->cmp_key = key;
6578 ret = finish_inode_if_needed(sctx, 0);
6582 /* Ignore non-FS objects */
6583 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6584 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6587 if (key->type == BTRFS_INODE_ITEM_KEY) {
6588 ret = changed_inode(sctx, result);
6589 } else if (!sctx->ignore_cur_inode) {
6590 if (key->type == BTRFS_INODE_REF_KEY ||
6591 key->type == BTRFS_INODE_EXTREF_KEY)
6592 ret = changed_ref(sctx, result);
6593 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6594 ret = changed_xattr(sctx, result);
6595 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6596 ret = changed_extent(sctx, result);
6603 static int full_send_tree(struct send_ctx *sctx)
6606 struct btrfs_root *send_root = sctx->send_root;
6607 struct btrfs_key key;
6608 struct btrfs_path *path;
6609 struct extent_buffer *eb;
6612 path = alloc_path_for_send();
6616 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6617 key.type = BTRFS_INODE_ITEM_KEY;
6620 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6627 eb = path->nodes[0];
6628 slot = path->slots[0];
6629 btrfs_item_key_to_cpu(eb, &key, slot);
6631 ret = changed_cb(path, NULL, &key,
6632 BTRFS_COMPARE_TREE_NEW, sctx);
6636 ret = btrfs_next_item(send_root, path);
6646 ret = finish_inode_if_needed(sctx, 1);
6649 btrfs_free_path(path);
6653 static int send_subvol(struct send_ctx *sctx)
6657 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6658 ret = send_header(sctx);
6663 ret = send_subvol_begin(sctx);
6667 if (sctx->parent_root) {
6668 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6672 ret = finish_inode_if_needed(sctx, 1);
6676 ret = full_send_tree(sctx);
6682 free_recorded_refs(sctx);
6687 * If orphan cleanup did remove any orphans from a root, it means the tree
6688 * was modified and therefore the commit root is not the same as the current
6689 * root anymore. This is a problem, because send uses the commit root and
6690 * therefore can see inode items that don't exist in the current root anymore,
6691 * and for example make calls to btrfs_iget, which will do tree lookups based
6692 * on the current root and not on the commit root. Those lookups will fail,
6693 * returning a -ESTALE error, and making send fail with that error. So make
6694 * sure a send does not see any orphans we have just removed, and that it will
6695 * see the same inodes regardless of whether a transaction commit happened
6696 * before it started (meaning that the commit root will be the same as the
6697 * current root) or not.
6699 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6702 struct btrfs_trans_handle *trans = NULL;
6705 if (sctx->parent_root &&
6706 sctx->parent_root->node != sctx->parent_root->commit_root)
6709 for (i = 0; i < sctx->clone_roots_cnt; i++)
6710 if (sctx->clone_roots[i].root->node !=
6711 sctx->clone_roots[i].root->commit_root)
6715 return btrfs_end_transaction(trans);
6720 /* Use any root, all fs roots will get their commit roots updated. */
6722 trans = btrfs_join_transaction(sctx->send_root);
6724 return PTR_ERR(trans);
6728 return btrfs_commit_transaction(trans);
6732 * Make sure any existing dellaloc is flushed for any root used by a send
6733 * operation so that we do not miss any data and we do not race with writeback
6734 * finishing and changing a tree while send is using the tree. This could
6735 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
6736 * a send operation then uses the subvolume.
6737 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
6739 static int flush_delalloc_roots(struct send_ctx *sctx)
6741 struct btrfs_root *root = sctx->parent_root;
6746 ret = btrfs_start_delalloc_snapshot(root);
6749 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
6752 for (i = 0; i < sctx->clone_roots_cnt; i++) {
6753 root = sctx->clone_roots[i].root;
6754 ret = btrfs_start_delalloc_snapshot(root);
6757 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
6763 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6765 spin_lock(&root->root_item_lock);
6766 root->send_in_progress--;
6768 * Not much left to do, we don't know why it's unbalanced and
6769 * can't blindly reset it to 0.
6771 if (root->send_in_progress < 0)
6772 btrfs_err(root->fs_info,
6773 "send_in_progress unbalanced %d root %llu",
6774 root->send_in_progress, root->root_key.objectid);
6775 spin_unlock(&root->root_item_lock);
6778 long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
6781 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6782 struct btrfs_fs_info *fs_info = send_root->fs_info;
6783 struct btrfs_root *clone_root;
6784 struct btrfs_key key;
6785 struct send_ctx *sctx = NULL;
6787 u64 *clone_sources_tmp = NULL;
6788 int clone_sources_to_rollback = 0;
6789 unsigned alloc_size;
6790 int sort_clone_roots = 0;
6793 if (!capable(CAP_SYS_ADMIN))
6797 * The subvolume must remain read-only during send, protect against
6798 * making it RW. This also protects against deletion.
6800 spin_lock(&send_root->root_item_lock);
6801 send_root->send_in_progress++;
6802 spin_unlock(&send_root->root_item_lock);
6805 * Userspace tools do the checks and warn the user if it's
6808 if (!btrfs_root_readonly(send_root)) {
6814 * Check that we don't overflow at later allocations, we request
6815 * clone_sources_count + 1 items, and compare to unsigned long inside
6818 if (arg->clone_sources_count >
6819 ULONG_MAX / sizeof(struct clone_root) - 1) {
6824 if (!access_ok(VERIFY_READ, arg->clone_sources,
6825 sizeof(*arg->clone_sources) *
6826 arg->clone_sources_count)) {
6831 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
6836 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
6842 INIT_LIST_HEAD(&sctx->new_refs);
6843 INIT_LIST_HEAD(&sctx->deleted_refs);
6844 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
6845 INIT_LIST_HEAD(&sctx->name_cache_list);
6847 sctx->flags = arg->flags;
6849 sctx->send_filp = fget(arg->send_fd);
6850 if (!sctx->send_filp) {
6855 sctx->send_root = send_root;
6857 * Unlikely but possible, if the subvolume is marked for deletion but
6858 * is slow to remove the directory entry, send can still be started
6860 if (btrfs_root_dead(sctx->send_root)) {
6865 sctx->clone_roots_cnt = arg->clone_sources_count;
6867 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
6868 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
6869 if (!sctx->send_buf) {
6874 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
6875 if (!sctx->read_buf) {
6880 sctx->pending_dir_moves = RB_ROOT;
6881 sctx->waiting_dir_moves = RB_ROOT;
6882 sctx->orphan_dirs = RB_ROOT;
6884 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6886 sctx->clone_roots = kvzalloc(alloc_size, GFP_KERNEL);
6887 if (!sctx->clone_roots) {
6892 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6894 if (arg->clone_sources_count) {
6895 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
6896 if (!clone_sources_tmp) {
6901 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
6908 for (i = 0; i < arg->clone_sources_count; i++) {
6909 key.objectid = clone_sources_tmp[i];
6910 key.type = BTRFS_ROOT_ITEM_KEY;
6911 key.offset = (u64)-1;
6913 index = srcu_read_lock(&fs_info->subvol_srcu);
6915 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
6916 if (IS_ERR(clone_root)) {
6917 srcu_read_unlock(&fs_info->subvol_srcu, index);
6918 ret = PTR_ERR(clone_root);
6921 spin_lock(&clone_root->root_item_lock);
6922 if (!btrfs_root_readonly(clone_root) ||
6923 btrfs_root_dead(clone_root)) {
6924 spin_unlock(&clone_root->root_item_lock);
6925 srcu_read_unlock(&fs_info->subvol_srcu, index);
6929 clone_root->send_in_progress++;
6930 spin_unlock(&clone_root->root_item_lock);
6931 srcu_read_unlock(&fs_info->subvol_srcu, index);
6933 sctx->clone_roots[i].root = clone_root;
6934 clone_sources_to_rollback = i + 1;
6936 kvfree(clone_sources_tmp);
6937 clone_sources_tmp = NULL;
6940 if (arg->parent_root) {
6941 key.objectid = arg->parent_root;
6942 key.type = BTRFS_ROOT_ITEM_KEY;
6943 key.offset = (u64)-1;
6945 index = srcu_read_lock(&fs_info->subvol_srcu);
6947 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
6948 if (IS_ERR(sctx->parent_root)) {
6949 srcu_read_unlock(&fs_info->subvol_srcu, index);
6950 ret = PTR_ERR(sctx->parent_root);
6954 spin_lock(&sctx->parent_root->root_item_lock);
6955 sctx->parent_root->send_in_progress++;
6956 if (!btrfs_root_readonly(sctx->parent_root) ||
6957 btrfs_root_dead(sctx->parent_root)) {
6958 spin_unlock(&sctx->parent_root->root_item_lock);
6959 srcu_read_unlock(&fs_info->subvol_srcu, index);
6963 spin_unlock(&sctx->parent_root->root_item_lock);
6965 srcu_read_unlock(&fs_info->subvol_srcu, index);
6969 * Clones from send_root are allowed, but only if the clone source
6970 * is behind the current send position. This is checked while searching
6971 * for possible clone sources.
6973 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6975 /* We do a bsearch later */
6976 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6977 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6979 sort_clone_roots = 1;
6981 ret = flush_delalloc_roots(sctx);
6985 ret = ensure_commit_roots_uptodate(sctx);
6989 current->journal_info = BTRFS_SEND_TRANS_STUB;
6990 ret = send_subvol(sctx);
6991 current->journal_info = NULL;
6995 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6996 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6999 ret = send_cmd(sctx);
7005 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7006 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7008 struct pending_dir_move *pm;
7010 n = rb_first(&sctx->pending_dir_moves);
7011 pm = rb_entry(n, struct pending_dir_move, node);
7012 while (!list_empty(&pm->list)) {
7013 struct pending_dir_move *pm2;
7015 pm2 = list_first_entry(&pm->list,
7016 struct pending_dir_move, list);
7017 free_pending_move(sctx, pm2);
7019 free_pending_move(sctx, pm);
7022 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7023 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7025 struct waiting_dir_move *dm;
7027 n = rb_first(&sctx->waiting_dir_moves);
7028 dm = rb_entry(n, struct waiting_dir_move, node);
7029 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7033 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7034 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7036 struct orphan_dir_info *odi;
7038 n = rb_first(&sctx->orphan_dirs);
7039 odi = rb_entry(n, struct orphan_dir_info, node);
7040 free_orphan_dir_info(sctx, odi);
7043 if (sort_clone_roots) {
7044 for (i = 0; i < sctx->clone_roots_cnt; i++)
7045 btrfs_root_dec_send_in_progress(
7046 sctx->clone_roots[i].root);
7048 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
7049 btrfs_root_dec_send_in_progress(
7050 sctx->clone_roots[i].root);
7052 btrfs_root_dec_send_in_progress(send_root);
7054 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
7055 btrfs_root_dec_send_in_progress(sctx->parent_root);
7057 kvfree(clone_sources_tmp);
7060 if (sctx->send_filp)
7061 fput(sctx->send_filp);
7063 kvfree(sctx->clone_roots);
7064 kvfree(sctx->send_buf);
7065 kvfree(sctx->read_buf);
7067 name_cache_free(sctx);