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;
126 * We process inodes by their increasing order, so if before an
127 * incremental send we reverse the parent/child relationship of
128 * directories such that a directory with a lower inode number was
129 * the parent of a directory with a higher inode number, and the one
130 * becoming the new parent got renamed too, we can't rename/move the
131 * directory with lower inode number when we finish processing it - we
132 * must process the directory with higher inode number first, then
133 * rename/move it and then rename/move the directory with lower inode
134 * number. Example follows.
136 * Tree state when the first send was performed:
148 * Tree state when the second (incremental) send is performed:
157 * The sequence of steps that lead to the second state was:
159 * mv /a/b/c/d /a/b/c2/d2
160 * mv /a/b/c /a/b/c2/d2/cc
162 * "c" has lower inode number, but we can't move it (2nd mv operation)
163 * before we move "d", which has higher inode number.
165 * So we just memorize which move/rename operations must be performed
166 * later when their respective parent is processed and moved/renamed.
169 /* Indexed by parent directory inode number. */
170 struct rb_root pending_dir_moves;
173 * Reverse index, indexed by the inode number of a directory that
174 * is waiting for the move/rename of its immediate parent before its
175 * own move/rename can be performed.
177 struct rb_root waiting_dir_moves;
180 * A directory that is going to be rm'ed might have a child directory
181 * which is in the pending directory moves index above. In this case,
182 * the directory can only be removed after the move/rename of its child
183 * is performed. Example:
203 * Sequence of steps that lead to the send snapshot:
204 * rm -f /a/b/c/foo.txt
206 * mv /a/b/c/x /a/b/YY
209 * When the child is processed, its move/rename is delayed until its
210 * parent is processed (as explained above), but all other operations
211 * like update utimes, chown, chgrp, etc, are performed and the paths
212 * that it uses for those operations must use the orphanized name of
213 * its parent (the directory we're going to rm later), so we need to
214 * memorize that name.
216 * Indexed by the inode number of the directory to be deleted.
218 struct rb_root orphan_dirs;
221 struct pending_dir_move {
223 struct list_head list;
227 struct list_head update_refs;
230 struct waiting_dir_move {
234 * There might be some directory that could not be removed because it
235 * was waiting for this directory inode to be moved first. Therefore
236 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
243 struct orphan_dir_info {
247 u64 last_dir_index_offset;
250 struct name_cache_entry {
251 struct list_head list;
253 * radix_tree has only 32bit entries but we need to handle 64bit inums.
254 * We use the lower 32bit of the 64bit inum to store it in the tree. If
255 * more then one inum would fall into the same entry, we use radix_list
256 * to store the additional entries. radix_list is also used to store
257 * entries where two entries have the same inum but different
260 struct list_head radix_list;
266 int need_later_update;
272 #define ADVANCE_ONLY_NEXT -1
274 enum btrfs_compare_tree_result {
275 BTRFS_COMPARE_TREE_NEW,
276 BTRFS_COMPARE_TREE_DELETED,
277 BTRFS_COMPARE_TREE_CHANGED,
278 BTRFS_COMPARE_TREE_SAME,
282 static void inconsistent_snapshot_error(struct send_ctx *sctx,
283 enum btrfs_compare_tree_result result,
286 const char *result_string;
289 case BTRFS_COMPARE_TREE_NEW:
290 result_string = "new";
292 case BTRFS_COMPARE_TREE_DELETED:
293 result_string = "deleted";
295 case BTRFS_COMPARE_TREE_CHANGED:
296 result_string = "updated";
298 case BTRFS_COMPARE_TREE_SAME:
300 result_string = "unchanged";
304 result_string = "unexpected";
307 btrfs_err(sctx->send_root->fs_info,
308 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
309 result_string, what, sctx->cmp_key->objectid,
310 sctx->send_root->root_key.objectid,
312 sctx->parent_root->root_key.objectid : 0));
315 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
317 static struct waiting_dir_move *
318 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
320 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
322 static int need_send_hole(struct send_ctx *sctx)
324 return (sctx->parent_root && !sctx->cur_inode_new &&
325 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
326 S_ISREG(sctx->cur_inode_mode));
329 static void fs_path_reset(struct fs_path *p)
332 p->start = p->buf + p->buf_len - 1;
342 static struct fs_path *fs_path_alloc(void)
346 p = kmalloc(sizeof(*p), GFP_KERNEL);
350 p->buf = p->inline_buf;
351 p->buf_len = FS_PATH_INLINE_SIZE;
356 static struct fs_path *fs_path_alloc_reversed(void)
368 static void fs_path_free(struct fs_path *p)
372 if (p->buf != p->inline_buf)
377 static int fs_path_len(struct fs_path *p)
379 return p->end - p->start;
382 static int fs_path_ensure_buf(struct fs_path *p, int len)
390 if (p->buf_len >= len)
393 if (len > PATH_MAX) {
398 path_len = p->end - p->start;
399 old_buf_len = p->buf_len;
402 * First time the inline_buf does not suffice
404 if (p->buf == p->inline_buf) {
405 tmp_buf = kmalloc(len, GFP_KERNEL);
407 memcpy(tmp_buf, p->buf, old_buf_len);
409 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
415 * The real size of the buffer is bigger, this will let the fast path
416 * happen most of the time
418 p->buf_len = ksize(p->buf);
421 tmp_buf = p->buf + old_buf_len - path_len - 1;
422 p->end = p->buf + p->buf_len - 1;
423 p->start = p->end - path_len;
424 memmove(p->start, tmp_buf, path_len + 1);
427 p->end = p->start + path_len;
432 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
438 new_len = p->end - p->start + name_len;
439 if (p->start != p->end)
441 ret = fs_path_ensure_buf(p, new_len);
446 if (p->start != p->end)
448 p->start -= name_len;
449 *prepared = p->start;
451 if (p->start != p->end)
462 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
467 ret = fs_path_prepare_for_add(p, name_len, &prepared);
470 memcpy(prepared, name, name_len);
476 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
481 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
484 memcpy(prepared, p2->start, p2->end - p2->start);
490 static int fs_path_add_from_extent_buffer(struct fs_path *p,
491 struct extent_buffer *eb,
492 unsigned long off, int len)
497 ret = fs_path_prepare_for_add(p, len, &prepared);
501 read_extent_buffer(eb, prepared, off, len);
507 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
511 p->reversed = from->reversed;
514 ret = fs_path_add_path(p, from);
520 static void fs_path_unreverse(struct fs_path *p)
529 len = p->end - p->start;
531 p->end = p->start + len;
532 memmove(p->start, tmp, len + 1);
536 static struct btrfs_path *alloc_path_for_send(void)
538 struct btrfs_path *path;
540 path = btrfs_alloc_path();
543 path->search_commit_root = 1;
544 path->skip_locking = 1;
545 path->need_commit_sem = 1;
549 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
555 ret = kernel_write(filp, buf + pos, len - pos, off);
556 /* TODO handle that correctly */
557 /*if (ret == -ERESTARTSYS) {
571 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
573 struct btrfs_tlv_header *hdr;
574 int total_len = sizeof(*hdr) + len;
575 int left = sctx->send_max_size - sctx->send_size;
577 if (unlikely(left < total_len))
580 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
581 put_unaligned_le16(attr, &hdr->tlv_type);
582 put_unaligned_le16(len, &hdr->tlv_len);
583 memcpy(hdr + 1, data, len);
584 sctx->send_size += total_len;
589 #define TLV_PUT_DEFINE_INT(bits) \
590 static int tlv_put_u##bits(struct send_ctx *sctx, \
591 u##bits attr, u##bits value) \
593 __le##bits __tmp = cpu_to_le##bits(value); \
594 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
597 TLV_PUT_DEFINE_INT(64)
599 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
600 const char *str, int len)
604 return tlv_put(sctx, attr, str, len);
607 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
610 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
613 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
614 struct extent_buffer *eb,
615 struct btrfs_timespec *ts)
617 struct btrfs_timespec bts;
618 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
619 return tlv_put(sctx, attr, &bts, sizeof(bts));
623 #define TLV_PUT(sctx, attrtype, data, attrlen) \
625 ret = tlv_put(sctx, attrtype, data, attrlen); \
627 goto tlv_put_failure; \
630 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
632 ret = tlv_put_u##bits(sctx, attrtype, value); \
634 goto tlv_put_failure; \
637 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
638 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
639 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
640 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
641 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
643 ret = tlv_put_string(sctx, attrtype, str, len); \
645 goto tlv_put_failure; \
647 #define TLV_PUT_PATH(sctx, attrtype, p) \
649 ret = tlv_put_string(sctx, attrtype, p->start, \
650 p->end - p->start); \
652 goto tlv_put_failure; \
654 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
656 ret = tlv_put_uuid(sctx, attrtype, uuid); \
658 goto tlv_put_failure; \
660 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
662 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
664 goto tlv_put_failure; \
667 static int send_header(struct send_ctx *sctx)
669 struct btrfs_stream_header hdr;
671 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
672 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
674 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
679 * For each command/item we want to send to userspace, we call this function.
681 static int begin_cmd(struct send_ctx *sctx, int cmd)
683 struct btrfs_cmd_header *hdr;
685 if (WARN_ON(!sctx->send_buf))
688 BUG_ON(sctx->send_size);
690 sctx->send_size += sizeof(*hdr);
691 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
692 put_unaligned_le16(cmd, &hdr->cmd);
697 static int send_cmd(struct send_ctx *sctx)
700 struct btrfs_cmd_header *hdr;
703 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
704 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
705 put_unaligned_le32(0, &hdr->crc);
707 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
708 put_unaligned_le32(crc, &hdr->crc);
710 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
713 sctx->total_send_size += sctx->send_size;
714 sctx->cmd_send_size[get_unaligned_le16(&hdr->cmd)] += sctx->send_size;
721 * Sends a move instruction to user space
723 static int send_rename(struct send_ctx *sctx,
724 struct fs_path *from, struct fs_path *to)
726 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
729 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
731 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
735 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
736 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
738 ret = send_cmd(sctx);
746 * Sends a link instruction to user space
748 static int send_link(struct send_ctx *sctx,
749 struct fs_path *path, struct fs_path *lnk)
751 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
754 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
756 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
760 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
761 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
763 ret = send_cmd(sctx);
771 * Sends an unlink instruction to user space
773 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
775 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
778 btrfs_debug(fs_info, "send_unlink %s", path->start);
780 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
784 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
786 ret = send_cmd(sctx);
794 * Sends a rmdir instruction to user space
796 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
798 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
801 btrfs_debug(fs_info, "send_rmdir %s", path->start);
803 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
807 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
809 ret = send_cmd(sctx);
817 * Helper function to retrieve some fields from an inode item.
819 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
820 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
824 struct btrfs_inode_item *ii;
825 struct btrfs_key key;
828 key.type = BTRFS_INODE_ITEM_KEY;
830 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
837 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
838 struct btrfs_inode_item);
840 *size = btrfs_inode_size(path->nodes[0], ii);
842 *gen = btrfs_inode_generation(path->nodes[0], ii);
844 *mode = btrfs_inode_mode(path->nodes[0], ii);
846 *uid = btrfs_inode_uid(path->nodes[0], ii);
848 *gid = btrfs_inode_gid(path->nodes[0], ii);
850 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
855 static int get_inode_info(struct btrfs_root *root,
856 u64 ino, u64 *size, u64 *gen,
857 u64 *mode, u64 *uid, u64 *gid,
860 struct btrfs_path *path;
863 path = alloc_path_for_send();
866 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
868 btrfs_free_path(path);
872 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
877 * Helper function to iterate the entries in ONE btrfs_inode_ref or
878 * btrfs_inode_extref.
879 * The iterate callback may return a non zero value to stop iteration. This can
880 * be a negative value for error codes or 1 to simply stop it.
882 * path must point to the INODE_REF or INODE_EXTREF when called.
884 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
885 struct btrfs_key *found_key, int resolve,
886 iterate_inode_ref_t iterate, void *ctx)
888 struct extent_buffer *eb = path->nodes[0];
889 struct btrfs_item *item;
890 struct btrfs_inode_ref *iref;
891 struct btrfs_inode_extref *extref;
892 struct btrfs_path *tmp_path;
896 int slot = path->slots[0];
903 unsigned long name_off;
904 unsigned long elem_size;
907 p = fs_path_alloc_reversed();
911 tmp_path = alloc_path_for_send();
918 if (found_key->type == BTRFS_INODE_REF_KEY) {
919 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
920 struct btrfs_inode_ref);
921 item = btrfs_item_nr(slot);
922 total = btrfs_item_size(eb, item);
923 elem_size = sizeof(*iref);
925 ptr = btrfs_item_ptr_offset(eb, slot);
926 total = btrfs_item_size_nr(eb, slot);
927 elem_size = sizeof(*extref);
930 while (cur < total) {
933 if (found_key->type == BTRFS_INODE_REF_KEY) {
934 iref = (struct btrfs_inode_ref *)(ptr + cur);
935 name_len = btrfs_inode_ref_name_len(eb, iref);
936 name_off = (unsigned long)(iref + 1);
937 index = btrfs_inode_ref_index(eb, iref);
938 dir = found_key->offset;
940 extref = (struct btrfs_inode_extref *)(ptr + cur);
941 name_len = btrfs_inode_extref_name_len(eb, extref);
942 name_off = (unsigned long)&extref->name;
943 index = btrfs_inode_extref_index(eb, extref);
944 dir = btrfs_inode_extref_parent(eb, extref);
948 start = btrfs_ref_to_path(root, tmp_path, name_len,
952 ret = PTR_ERR(start);
955 if (start < p->buf) {
956 /* overflow , try again with larger buffer */
957 ret = fs_path_ensure_buf(p,
958 p->buf_len + p->buf - start);
961 start = btrfs_ref_to_path(root, tmp_path,
966 ret = PTR_ERR(start);
969 BUG_ON(start < p->buf);
973 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
979 cur += elem_size + name_len;
980 ret = iterate(num, dir, index, p, ctx);
987 btrfs_free_path(tmp_path);
992 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
993 const char *name, int name_len,
994 const char *data, int data_len,
998 * Helper function to iterate the entries in ONE btrfs_dir_item.
999 * The iterate callback may return a non zero value to stop iteration. This can
1000 * be a negative value for error codes or 1 to simply stop it.
1002 * path must point to the dir item when called.
1004 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
1005 iterate_dir_item_t iterate, void *ctx)
1008 struct extent_buffer *eb;
1009 struct btrfs_item *item;
1010 struct btrfs_dir_item *di;
1011 struct btrfs_key di_key;
1024 * Start with a small buffer (1 page). If later we end up needing more
1025 * space, which can happen for xattrs on a fs with a leaf size greater
1026 * then the page size, attempt to increase the buffer. Typically xattr
1030 buf = kmalloc(buf_len, GFP_KERNEL);
1036 eb = path->nodes[0];
1037 slot = path->slots[0];
1038 item = btrfs_item_nr(slot);
1039 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1042 total = btrfs_item_size(eb, item);
1045 while (cur < total) {
1046 name_len = btrfs_dir_name_len(eb, di);
1047 data_len = btrfs_dir_data_len(eb, di);
1048 type = btrfs_dir_type(eb, di);
1049 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1051 if (type == BTRFS_FT_XATTR) {
1052 if (name_len > XATTR_NAME_MAX) {
1053 ret = -ENAMETOOLONG;
1056 if (name_len + data_len >
1057 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1065 if (name_len + data_len > PATH_MAX) {
1066 ret = -ENAMETOOLONG;
1071 if (name_len + data_len > buf_len) {
1072 buf_len = name_len + data_len;
1073 if (is_vmalloc_addr(buf)) {
1077 char *tmp = krealloc(buf, buf_len,
1078 GFP_KERNEL | __GFP_NOWARN);
1085 buf = kvmalloc(buf_len, GFP_KERNEL);
1093 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1094 name_len + data_len);
1096 len = sizeof(*di) + name_len + data_len;
1097 di = (struct btrfs_dir_item *)((char *)di + len);
1100 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1101 data_len, type, ctx);
1117 static int __copy_first_ref(int num, u64 dir, int index,
1118 struct fs_path *p, void *ctx)
1121 struct fs_path *pt = ctx;
1123 ret = fs_path_copy(pt, p);
1127 /* we want the first only */
1132 * Retrieve the first path of an inode. If an inode has more then one
1133 * ref/hardlink, this is ignored.
1135 static int get_inode_path(struct btrfs_root *root,
1136 u64 ino, struct fs_path *path)
1139 struct btrfs_key key, found_key;
1140 struct btrfs_path *p;
1142 p = alloc_path_for_send();
1146 fs_path_reset(path);
1149 key.type = BTRFS_INODE_REF_KEY;
1152 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1159 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1160 if (found_key.objectid != ino ||
1161 (found_key.type != BTRFS_INODE_REF_KEY &&
1162 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1167 ret = iterate_inode_ref(root, p, &found_key, 1,
1168 __copy_first_ref, path);
1178 struct backref_ctx {
1179 struct send_ctx *sctx;
1181 /* number of total found references */
1185 * used for clones found in send_root. clones found behind cur_objectid
1186 * and cur_offset are not considered as allowed clones.
1191 /* may be truncated in case it's the last extent in a file */
1194 /* data offset in the file extent item */
1197 /* Just to check for bugs in backref resolving */
1201 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1203 u64 root = (u64)(uintptr_t)key;
1204 struct clone_root *cr = (struct clone_root *)elt;
1206 if (root < cr->root->root_key.objectid)
1208 if (root > cr->root->root_key.objectid)
1213 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1215 struct clone_root *cr1 = (struct clone_root *)e1;
1216 struct clone_root *cr2 = (struct clone_root *)e2;
1218 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
1220 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
1226 * Called for every backref that is found for the current extent.
1227 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1229 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1231 struct backref_ctx *bctx = ctx_;
1232 struct clone_root *found;
1234 /* First check if the root is in the list of accepted clone sources */
1235 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1236 bctx->sctx->clone_roots_cnt,
1237 sizeof(struct clone_root),
1238 __clone_root_cmp_bsearch);
1242 if (found->root == bctx->sctx->send_root &&
1243 ino == bctx->cur_objectid &&
1244 offset == bctx->cur_offset) {
1245 bctx->found_itself = 1;
1249 * Make sure we don't consider clones from send_root that are
1250 * behind the current inode/offset.
1252 if (found->root == bctx->sctx->send_root) {
1254 * If the source inode was not yet processed we can't issue a
1255 * clone operation, as the source extent does not exist yet at
1256 * the destination of the stream.
1258 if (ino > bctx->cur_objectid)
1261 * We clone from the inode currently being sent as long as the
1262 * source extent is already processed, otherwise we could try
1263 * to clone from an extent that does not exist yet at the
1264 * destination of the stream.
1266 if (ino == bctx->cur_objectid &&
1267 offset + bctx->extent_len >
1268 bctx->sctx->cur_inode_next_write_offset)
1273 found->found_refs++;
1274 if (ino < found->ino) {
1276 found->offset = offset;
1277 } else if (found->ino == ino) {
1279 * same extent found more then once in the same file.
1281 if (found->offset > offset + bctx->extent_len)
1282 found->offset = offset;
1289 * Given an inode, offset and extent item, it finds a good clone for a clone
1290 * instruction. Returns -ENOENT when none could be found. The function makes
1291 * sure that the returned clone is usable at the point where sending is at the
1292 * moment. This means, that no clones are accepted which lie behind the current
1295 * path must point to the extent item when called.
1297 static int find_extent_clone(struct send_ctx *sctx,
1298 struct btrfs_path *path,
1299 u64 ino, u64 data_offset,
1301 struct clone_root **found)
1303 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1309 u64 extent_item_pos;
1311 struct btrfs_file_extent_item *fi;
1312 struct extent_buffer *eb = path->nodes[0];
1313 struct backref_ctx *backref_ctx = NULL;
1314 struct clone_root *cur_clone_root;
1315 struct btrfs_key found_key;
1316 struct btrfs_path *tmp_path;
1317 struct btrfs_extent_item *ei;
1321 tmp_path = alloc_path_for_send();
1325 /* We only use this path under the commit sem */
1326 tmp_path->need_commit_sem = 0;
1328 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
1334 if (data_offset >= ino_size) {
1336 * There may be extents that lie behind the file's size.
1337 * I at least had this in combination with snapshotting while
1338 * writing large files.
1344 fi = btrfs_item_ptr(eb, path->slots[0],
1345 struct btrfs_file_extent_item);
1346 extent_type = btrfs_file_extent_type(eb, fi);
1347 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1351 compressed = btrfs_file_extent_compression(eb, fi);
1353 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1354 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1355 if (disk_byte == 0) {
1359 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1361 down_read(&fs_info->commit_root_sem);
1362 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1363 &found_key, &flags);
1364 up_read(&fs_info->commit_root_sem);
1368 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1373 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1374 struct btrfs_extent_item);
1376 * Backreference walking (iterate_extent_inodes() below) is currently
1377 * too expensive when an extent has a large number of references, both
1378 * in time spent and used memory. So for now just fallback to write
1379 * operations instead of clone operations when an extent has more than
1380 * a certain amount of references.
1382 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1386 btrfs_release_path(tmp_path);
1389 * Setup the clone roots.
1391 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1392 cur_clone_root = sctx->clone_roots + i;
1393 cur_clone_root->ino = (u64)-1;
1394 cur_clone_root->offset = 0;
1395 cur_clone_root->found_refs = 0;
1398 backref_ctx->sctx = sctx;
1399 backref_ctx->found = 0;
1400 backref_ctx->cur_objectid = ino;
1401 backref_ctx->cur_offset = data_offset;
1402 backref_ctx->found_itself = 0;
1403 backref_ctx->extent_len = num_bytes;
1405 * For non-compressed extents iterate_extent_inodes() gives us extent
1406 * offsets that already take into account the data offset, but not for
1407 * compressed extents, since the offset is logical and not relative to
1408 * the physical extent locations. We must take this into account to
1409 * avoid sending clone offsets that go beyond the source file's size,
1410 * which would result in the clone ioctl failing with -EINVAL on the
1413 if (compressed == BTRFS_COMPRESS_NONE)
1414 backref_ctx->data_offset = 0;
1416 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
1419 * The last extent of a file may be too large due to page alignment.
1420 * We need to adjust extent_len in this case so that the checks in
1421 * __iterate_backrefs work.
1423 if (data_offset + num_bytes >= ino_size)
1424 backref_ctx->extent_len = ino_size - data_offset;
1427 * Now collect all backrefs.
1429 if (compressed == BTRFS_COMPRESS_NONE)
1430 extent_item_pos = logical - found_key.objectid;
1432 extent_item_pos = 0;
1433 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1434 extent_item_pos, 1, __iterate_backrefs,
1435 backref_ctx, false);
1440 if (!backref_ctx->found_itself) {
1441 /* found a bug in backref code? */
1444 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1445 ino, data_offset, disk_byte, found_key.objectid);
1449 btrfs_debug(fs_info,
1450 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1451 data_offset, ino, num_bytes, logical);
1453 if (!backref_ctx->found)
1454 btrfs_debug(fs_info, "no clones found");
1456 cur_clone_root = NULL;
1457 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1458 if (sctx->clone_roots[i].found_refs) {
1459 if (!cur_clone_root)
1460 cur_clone_root = sctx->clone_roots + i;
1461 else if (sctx->clone_roots[i].root == sctx->send_root)
1462 /* prefer clones from send_root over others */
1463 cur_clone_root = sctx->clone_roots + i;
1468 if (cur_clone_root) {
1469 *found = cur_clone_root;
1476 btrfs_free_path(tmp_path);
1481 static int read_symlink(struct btrfs_root *root,
1483 struct fs_path *dest)
1486 struct btrfs_path *path;
1487 struct btrfs_key key;
1488 struct btrfs_file_extent_item *ei;
1494 path = alloc_path_for_send();
1499 key.type = BTRFS_EXTENT_DATA_KEY;
1501 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1506 * An empty symlink inode. Can happen in rare error paths when
1507 * creating a symlink (transaction committed before the inode
1508 * eviction handler removed the symlink inode items and a crash
1509 * happened in between or the subvol was snapshoted in between).
1510 * Print an informative message to dmesg/syslog so that the user
1511 * can delete the symlink.
1513 btrfs_err(root->fs_info,
1514 "Found empty symlink inode %llu at root %llu",
1515 ino, root->root_key.objectid);
1520 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1521 struct btrfs_file_extent_item);
1522 type = btrfs_file_extent_type(path->nodes[0], ei);
1523 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1524 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1525 BUG_ON(compression);
1527 off = btrfs_file_extent_inline_start(ei);
1528 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
1530 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1533 btrfs_free_path(path);
1538 * Helper function to generate a file name that is unique in the root of
1539 * send_root and parent_root. This is used to generate names for orphan inodes.
1541 static int gen_unique_name(struct send_ctx *sctx,
1543 struct fs_path *dest)
1546 struct btrfs_path *path;
1547 struct btrfs_dir_item *di;
1552 path = alloc_path_for_send();
1557 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1559 ASSERT(len < sizeof(tmp));
1561 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1562 path, BTRFS_FIRST_FREE_OBJECTID,
1563 tmp, strlen(tmp), 0);
1564 btrfs_release_path(path);
1570 /* not unique, try again */
1575 if (!sctx->parent_root) {
1581 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1582 path, BTRFS_FIRST_FREE_OBJECTID,
1583 tmp, strlen(tmp), 0);
1584 btrfs_release_path(path);
1590 /* not unique, try again */
1598 ret = fs_path_add(dest, tmp, strlen(tmp));
1601 btrfs_free_path(path);
1606 inode_state_no_change,
1607 inode_state_will_create,
1608 inode_state_did_create,
1609 inode_state_will_delete,
1610 inode_state_did_delete,
1613 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1621 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1623 if (ret < 0 && ret != -ENOENT)
1627 if (!sctx->parent_root) {
1628 right_ret = -ENOENT;
1630 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1631 NULL, NULL, NULL, NULL);
1632 if (ret < 0 && ret != -ENOENT)
1637 if (!left_ret && !right_ret) {
1638 if (left_gen == gen && right_gen == gen) {
1639 ret = inode_state_no_change;
1640 } else if (left_gen == gen) {
1641 if (ino < sctx->send_progress)
1642 ret = inode_state_did_create;
1644 ret = inode_state_will_create;
1645 } else if (right_gen == gen) {
1646 if (ino < sctx->send_progress)
1647 ret = inode_state_did_delete;
1649 ret = inode_state_will_delete;
1653 } else if (!left_ret) {
1654 if (left_gen == gen) {
1655 if (ino < sctx->send_progress)
1656 ret = inode_state_did_create;
1658 ret = inode_state_will_create;
1662 } else if (!right_ret) {
1663 if (right_gen == gen) {
1664 if (ino < sctx->send_progress)
1665 ret = inode_state_did_delete;
1667 ret = inode_state_will_delete;
1679 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1683 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1686 ret = get_cur_inode_state(sctx, ino, gen);
1690 if (ret == inode_state_no_change ||
1691 ret == inode_state_did_create ||
1692 ret == inode_state_will_delete)
1702 * Helper function to lookup a dir item in a dir.
1704 static int lookup_dir_item_inode(struct btrfs_root *root,
1705 u64 dir, const char *name, int name_len,
1710 struct btrfs_dir_item *di;
1711 struct btrfs_key key;
1712 struct btrfs_path *path;
1714 path = alloc_path_for_send();
1718 di = btrfs_lookup_dir_item(NULL, root, path,
1719 dir, name, name_len, 0);
1720 if (IS_ERR_OR_NULL(di)) {
1721 ret = di ? PTR_ERR(di) : -ENOENT;
1724 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1725 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1729 *found_inode = key.objectid;
1730 *found_type = btrfs_dir_type(path->nodes[0], di);
1733 btrfs_free_path(path);
1738 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1739 * generation of the parent dir and the name of the dir entry.
1741 static int get_first_ref(struct btrfs_root *root, u64 ino,
1742 u64 *dir, u64 *dir_gen, struct fs_path *name)
1745 struct btrfs_key key;
1746 struct btrfs_key found_key;
1747 struct btrfs_path *path;
1751 path = alloc_path_for_send();
1756 key.type = BTRFS_INODE_REF_KEY;
1759 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1763 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1765 if (ret || found_key.objectid != ino ||
1766 (found_key.type != BTRFS_INODE_REF_KEY &&
1767 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1772 if (found_key.type == BTRFS_INODE_REF_KEY) {
1773 struct btrfs_inode_ref *iref;
1774 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1775 struct btrfs_inode_ref);
1776 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1777 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1778 (unsigned long)(iref + 1),
1780 parent_dir = found_key.offset;
1782 struct btrfs_inode_extref *extref;
1783 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1784 struct btrfs_inode_extref);
1785 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1786 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1787 (unsigned long)&extref->name, len);
1788 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1792 btrfs_release_path(path);
1795 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1804 btrfs_free_path(path);
1808 static int is_first_ref(struct btrfs_root *root,
1810 const char *name, int name_len)
1813 struct fs_path *tmp_name;
1816 tmp_name = fs_path_alloc();
1820 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1824 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1829 ret = !memcmp(tmp_name->start, name, name_len);
1832 fs_path_free(tmp_name);
1837 * Used by process_recorded_refs to determine if a new ref would overwrite an
1838 * already existing ref. In case it detects an overwrite, it returns the
1839 * inode/gen in who_ino/who_gen.
1840 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1841 * to make sure later references to the overwritten inode are possible.
1842 * Orphanizing is however only required for the first ref of an inode.
1843 * process_recorded_refs does an additional is_first_ref check to see if
1844 * orphanizing is really required.
1846 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1847 const char *name, int name_len,
1848 u64 *who_ino, u64 *who_gen, u64 *who_mode)
1852 u64 other_inode = 0;
1855 if (!sctx->parent_root)
1858 ret = is_inode_existent(sctx, dir, dir_gen);
1863 * If we have a parent root we need to verify that the parent dir was
1864 * not deleted and then re-created, if it was then we have no overwrite
1865 * and we can just unlink this entry.
1867 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
1868 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1870 if (ret < 0 && ret != -ENOENT)
1880 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1881 &other_inode, &other_type);
1882 if (ret < 0 && ret != -ENOENT)
1890 * Check if the overwritten ref was already processed. If yes, the ref
1891 * was already unlinked/moved, so we can safely assume that we will not
1892 * overwrite anything at this point in time.
1894 if (other_inode > sctx->send_progress ||
1895 is_waiting_for_move(sctx, other_inode)) {
1896 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1897 who_gen, who_mode, NULL, NULL, NULL);
1902 *who_ino = other_inode;
1912 * Checks if the ref was overwritten by an already processed inode. This is
1913 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1914 * thus the orphan name needs be used.
1915 * process_recorded_refs also uses it to avoid unlinking of refs that were
1918 static int did_overwrite_ref(struct send_ctx *sctx,
1919 u64 dir, u64 dir_gen,
1920 u64 ino, u64 ino_gen,
1921 const char *name, int name_len)
1928 if (!sctx->parent_root)
1931 ret = is_inode_existent(sctx, dir, dir_gen);
1935 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1936 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1938 if (ret < 0 && ret != -ENOENT)
1948 /* check if the ref was overwritten by another ref */
1949 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1950 &ow_inode, &other_type);
1951 if (ret < 0 && ret != -ENOENT)
1954 /* was never and will never be overwritten */
1959 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1964 if (ow_inode == ino && gen == ino_gen) {
1970 * We know that it is or will be overwritten. Check this now.
1971 * The current inode being processed might have been the one that caused
1972 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1973 * the current inode being processed.
1975 if ((ow_inode < sctx->send_progress) ||
1976 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1977 gen == sctx->cur_inode_gen))
1987 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1988 * that got overwritten. This is used by process_recorded_refs to determine
1989 * if it has to use the path as returned by get_cur_path or the orphan name.
1991 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1994 struct fs_path *name = NULL;
1998 if (!sctx->parent_root)
2001 name = fs_path_alloc();
2005 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
2009 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2010 name->start, fs_path_len(name));
2018 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2019 * so we need to do some special handling in case we have clashes. This function
2020 * takes care of this with the help of name_cache_entry::radix_list.
2021 * In case of error, nce is kfreed.
2023 static int name_cache_insert(struct send_ctx *sctx,
2024 struct name_cache_entry *nce)
2027 struct list_head *nce_head;
2029 nce_head = radix_tree_lookup(&sctx->name_cache,
2030 (unsigned long)nce->ino);
2032 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
2037 INIT_LIST_HEAD(nce_head);
2039 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
2046 list_add_tail(&nce->radix_list, nce_head);
2047 list_add_tail(&nce->list, &sctx->name_cache_list);
2048 sctx->name_cache_size++;
2053 static void name_cache_delete(struct send_ctx *sctx,
2054 struct name_cache_entry *nce)
2056 struct list_head *nce_head;
2058 nce_head = radix_tree_lookup(&sctx->name_cache,
2059 (unsigned long)nce->ino);
2061 btrfs_err(sctx->send_root->fs_info,
2062 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2063 nce->ino, sctx->name_cache_size);
2066 list_del(&nce->radix_list);
2067 list_del(&nce->list);
2068 sctx->name_cache_size--;
2071 * We may not get to the final release of nce_head if the lookup fails
2073 if (nce_head && list_empty(nce_head)) {
2074 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2079 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2082 struct list_head *nce_head;
2083 struct name_cache_entry *cur;
2085 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2089 list_for_each_entry(cur, nce_head, radix_list) {
2090 if (cur->ino == ino && cur->gen == gen)
2097 * Removes the entry from the list and adds it back to the end. This marks the
2098 * entry as recently used so that name_cache_clean_unused does not remove it.
2100 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2102 list_del(&nce->list);
2103 list_add_tail(&nce->list, &sctx->name_cache_list);
2107 * Remove some entries from the beginning of name_cache_list.
2109 static void name_cache_clean_unused(struct send_ctx *sctx)
2111 struct name_cache_entry *nce;
2113 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2116 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2117 nce = list_entry(sctx->name_cache_list.next,
2118 struct name_cache_entry, list);
2119 name_cache_delete(sctx, nce);
2124 static void name_cache_free(struct send_ctx *sctx)
2126 struct name_cache_entry *nce;
2128 while (!list_empty(&sctx->name_cache_list)) {
2129 nce = list_entry(sctx->name_cache_list.next,
2130 struct name_cache_entry, list);
2131 name_cache_delete(sctx, nce);
2137 * Used by get_cur_path for each ref up to the root.
2138 * Returns 0 if it succeeded.
2139 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2140 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2141 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2142 * Returns <0 in case of error.
2144 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2148 struct fs_path *dest)
2152 struct name_cache_entry *nce = NULL;
2155 * First check if we already did a call to this function with the same
2156 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2157 * return the cached result.
2159 nce = name_cache_search(sctx, ino, gen);
2161 if (ino < sctx->send_progress && nce->need_later_update) {
2162 name_cache_delete(sctx, nce);
2166 name_cache_used(sctx, nce);
2167 *parent_ino = nce->parent_ino;
2168 *parent_gen = nce->parent_gen;
2169 ret = fs_path_add(dest, nce->name, nce->name_len);
2178 * If the inode is not existent yet, add the orphan name and return 1.
2179 * This should only happen for the parent dir that we determine in
2182 ret = is_inode_existent(sctx, ino, gen);
2187 ret = gen_unique_name(sctx, ino, gen, dest);
2195 * Depending on whether the inode was already processed or not, use
2196 * send_root or parent_root for ref lookup.
2198 if (ino < sctx->send_progress)
2199 ret = get_first_ref(sctx->send_root, ino,
2200 parent_ino, parent_gen, dest);
2202 ret = get_first_ref(sctx->parent_root, ino,
2203 parent_ino, parent_gen, dest);
2208 * Check if the ref was overwritten by an inode's ref that was processed
2209 * earlier. If yes, treat as orphan and return 1.
2211 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2212 dest->start, dest->end - dest->start);
2216 fs_path_reset(dest);
2217 ret = gen_unique_name(sctx, ino, gen, dest);
2225 * Store the result of the lookup in the name cache.
2227 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2235 nce->parent_ino = *parent_ino;
2236 nce->parent_gen = *parent_gen;
2237 nce->name_len = fs_path_len(dest);
2239 strcpy(nce->name, dest->start);
2241 if (ino < sctx->send_progress)
2242 nce->need_later_update = 0;
2244 nce->need_later_update = 1;
2246 nce_ret = name_cache_insert(sctx, nce);
2249 name_cache_clean_unused(sctx);
2256 * Magic happens here. This function returns the first ref to an inode as it
2257 * would look like while receiving the stream at this point in time.
2258 * We walk the path up to the root. For every inode in between, we check if it
2259 * was already processed/sent. If yes, we continue with the parent as found
2260 * in send_root. If not, we continue with the parent as found in parent_root.
2261 * If we encounter an inode that was deleted at this point in time, we use the
2262 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2263 * that were not created yet and overwritten inodes/refs.
2265 * When do we have orphan inodes:
2266 * 1. When an inode is freshly created and thus no valid refs are available yet
2267 * 2. When a directory lost all it's refs (deleted) but still has dir items
2268 * inside which were not processed yet (pending for move/delete). If anyone
2269 * tried to get the path to the dir items, it would get a path inside that
2271 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2272 * of an unprocessed inode. If in that case the first ref would be
2273 * overwritten, the overwritten inode gets "orphanized". Later when we
2274 * process this overwritten inode, it is restored at a new place by moving
2277 * sctx->send_progress tells this function at which point in time receiving
2280 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2281 struct fs_path *dest)
2284 struct fs_path *name = NULL;
2285 u64 parent_inode = 0;
2289 name = fs_path_alloc();
2296 fs_path_reset(dest);
2298 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2299 struct waiting_dir_move *wdm;
2301 fs_path_reset(name);
2303 if (is_waiting_for_rm(sctx, ino, gen)) {
2304 ret = gen_unique_name(sctx, ino, gen, name);
2307 ret = fs_path_add_path(dest, name);
2311 wdm = get_waiting_dir_move(sctx, ino);
2312 if (wdm && wdm->orphanized) {
2313 ret = gen_unique_name(sctx, ino, gen, name);
2316 ret = get_first_ref(sctx->parent_root, ino,
2317 &parent_inode, &parent_gen, name);
2319 ret = __get_cur_name_and_parent(sctx, ino, gen,
2329 ret = fs_path_add_path(dest, name);
2340 fs_path_unreverse(dest);
2345 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2347 static int send_subvol_begin(struct send_ctx *sctx)
2350 struct btrfs_root *send_root = sctx->send_root;
2351 struct btrfs_root *parent_root = sctx->parent_root;
2352 struct btrfs_path *path;
2353 struct btrfs_key key;
2354 struct btrfs_root_ref *ref;
2355 struct extent_buffer *leaf;
2359 path = btrfs_alloc_path();
2363 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2365 btrfs_free_path(path);
2369 key.objectid = send_root->root_key.objectid;
2370 key.type = BTRFS_ROOT_BACKREF_KEY;
2373 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2382 leaf = path->nodes[0];
2383 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2384 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2385 key.objectid != send_root->root_key.objectid) {
2389 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2390 namelen = btrfs_root_ref_name_len(leaf, ref);
2391 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2392 btrfs_release_path(path);
2395 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2399 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2404 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2406 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2407 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2408 sctx->send_root->root_item.received_uuid);
2410 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2411 sctx->send_root->root_item.uuid);
2413 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2414 btrfs_root_ctransid(&sctx->send_root->root_item));
2416 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2417 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2418 parent_root->root_item.received_uuid);
2420 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2421 parent_root->root_item.uuid);
2422 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2423 btrfs_root_ctransid(&sctx->parent_root->root_item));
2426 ret = send_cmd(sctx);
2430 btrfs_free_path(path);
2435 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2437 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2441 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
2443 p = fs_path_alloc();
2447 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2451 ret = get_cur_path(sctx, ino, gen, p);
2454 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2455 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2457 ret = send_cmd(sctx);
2465 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2467 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2471 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
2473 p = fs_path_alloc();
2477 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2481 ret = get_cur_path(sctx, ino, gen, p);
2484 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2485 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2487 ret = send_cmd(sctx);
2495 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2497 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2501 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2504 p = fs_path_alloc();
2508 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2512 ret = get_cur_path(sctx, ino, gen, p);
2515 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2516 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2517 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2519 ret = send_cmd(sctx);
2527 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2529 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2531 struct fs_path *p = NULL;
2532 struct btrfs_inode_item *ii;
2533 struct btrfs_path *path = NULL;
2534 struct extent_buffer *eb;
2535 struct btrfs_key key;
2538 btrfs_debug(fs_info, "send_utimes %llu", ino);
2540 p = fs_path_alloc();
2544 path = alloc_path_for_send();
2551 key.type = BTRFS_INODE_ITEM_KEY;
2553 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2559 eb = path->nodes[0];
2560 slot = path->slots[0];
2561 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2563 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2567 ret = get_cur_path(sctx, ino, gen, p);
2570 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2571 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2572 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2573 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2574 /* TODO Add otime support when the otime patches get into upstream */
2576 ret = send_cmd(sctx);
2581 btrfs_free_path(path);
2586 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2587 * a valid path yet because we did not process the refs yet. So, the inode
2588 * is created as orphan.
2590 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2592 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2600 btrfs_debug(fs_info, "send_create_inode %llu", ino);
2602 p = fs_path_alloc();
2606 if (ino != sctx->cur_ino) {
2607 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2612 gen = sctx->cur_inode_gen;
2613 mode = sctx->cur_inode_mode;
2614 rdev = sctx->cur_inode_rdev;
2617 if (S_ISREG(mode)) {
2618 cmd = BTRFS_SEND_C_MKFILE;
2619 } else if (S_ISDIR(mode)) {
2620 cmd = BTRFS_SEND_C_MKDIR;
2621 } else if (S_ISLNK(mode)) {
2622 cmd = BTRFS_SEND_C_SYMLINK;
2623 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2624 cmd = BTRFS_SEND_C_MKNOD;
2625 } else if (S_ISFIFO(mode)) {
2626 cmd = BTRFS_SEND_C_MKFIFO;
2627 } else if (S_ISSOCK(mode)) {
2628 cmd = BTRFS_SEND_C_MKSOCK;
2630 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2631 (int)(mode & S_IFMT));
2636 ret = begin_cmd(sctx, cmd);
2640 ret = gen_unique_name(sctx, ino, gen, p);
2644 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2645 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2647 if (S_ISLNK(mode)) {
2649 ret = read_symlink(sctx->send_root, ino, p);
2652 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2653 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2654 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2655 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2656 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2659 ret = send_cmd(sctx);
2671 * We need some special handling for inodes that get processed before the parent
2672 * directory got created. See process_recorded_refs for details.
2673 * This function does the check if we already created the dir out of order.
2675 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2678 struct btrfs_path *path = NULL;
2679 struct btrfs_key key;
2680 struct btrfs_key found_key;
2681 struct btrfs_key di_key;
2682 struct extent_buffer *eb;
2683 struct btrfs_dir_item *di;
2686 path = alloc_path_for_send();
2693 key.type = BTRFS_DIR_INDEX_KEY;
2695 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2700 eb = path->nodes[0];
2701 slot = path->slots[0];
2702 if (slot >= btrfs_header_nritems(eb)) {
2703 ret = btrfs_next_leaf(sctx->send_root, path);
2706 } else if (ret > 0) {
2713 btrfs_item_key_to_cpu(eb, &found_key, slot);
2714 if (found_key.objectid != key.objectid ||
2715 found_key.type != key.type) {
2720 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2721 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2723 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2724 di_key.objectid < sctx->send_progress) {
2733 btrfs_free_path(path);
2738 * Only creates the inode if it is:
2739 * 1. Not a directory
2740 * 2. Or a directory which was not created already due to out of order
2741 * directories. See did_create_dir and process_recorded_refs for details.
2743 static int send_create_inode_if_needed(struct send_ctx *sctx)
2747 if (S_ISDIR(sctx->cur_inode_mode)) {
2748 ret = did_create_dir(sctx, sctx->cur_ino);
2757 ret = send_create_inode(sctx, sctx->cur_ino);
2765 struct recorded_ref {
2766 struct list_head list;
2768 struct fs_path *full_path;
2774 static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2776 ref->full_path = path;
2777 ref->name = (char *)kbasename(ref->full_path->start);
2778 ref->name_len = ref->full_path->end - ref->name;
2782 * We need to process new refs before deleted refs, but compare_tree gives us
2783 * everything mixed. So we first record all refs and later process them.
2784 * This function is a helper to record one ref.
2786 static int __record_ref(struct list_head *head, u64 dir,
2787 u64 dir_gen, struct fs_path *path)
2789 struct recorded_ref *ref;
2791 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
2796 ref->dir_gen = dir_gen;
2797 set_ref_path(ref, path);
2798 list_add_tail(&ref->list, head);
2802 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2804 struct recorded_ref *new;
2806 new = kmalloc(sizeof(*ref), GFP_KERNEL);
2810 new->dir = ref->dir;
2811 new->dir_gen = ref->dir_gen;
2812 new->full_path = NULL;
2813 INIT_LIST_HEAD(&new->list);
2814 list_add_tail(&new->list, list);
2818 static void __free_recorded_refs(struct list_head *head)
2820 struct recorded_ref *cur;
2822 while (!list_empty(head)) {
2823 cur = list_entry(head->next, struct recorded_ref, list);
2824 fs_path_free(cur->full_path);
2825 list_del(&cur->list);
2830 static void free_recorded_refs(struct send_ctx *sctx)
2832 __free_recorded_refs(&sctx->new_refs);
2833 __free_recorded_refs(&sctx->deleted_refs);
2837 * Renames/moves a file/dir to its orphan name. Used when the first
2838 * ref of an unprocessed inode gets overwritten and for all non empty
2841 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2842 struct fs_path *path)
2845 struct fs_path *orphan;
2847 orphan = fs_path_alloc();
2851 ret = gen_unique_name(sctx, ino, gen, orphan);
2855 ret = send_rename(sctx, path, orphan);
2858 fs_path_free(orphan);
2862 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
2863 u64 dir_ino, u64 dir_gen)
2865 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2866 struct rb_node *parent = NULL;
2867 struct orphan_dir_info *entry, *odi;
2871 entry = rb_entry(parent, struct orphan_dir_info, node);
2872 if (dir_ino < entry->ino)
2874 else if (dir_ino > entry->ino)
2875 p = &(*p)->rb_right;
2876 else if (dir_gen < entry->gen)
2878 else if (dir_gen > entry->gen)
2879 p = &(*p)->rb_right;
2884 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2886 return ERR_PTR(-ENOMEM);
2889 odi->last_dir_index_offset = 0;
2891 rb_link_node(&odi->node, parent, p);
2892 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2896 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
2897 u64 dir_ino, u64 gen)
2899 struct rb_node *n = sctx->orphan_dirs.rb_node;
2900 struct orphan_dir_info *entry;
2903 entry = rb_entry(n, struct orphan_dir_info, node);
2904 if (dir_ino < entry->ino)
2906 else if (dir_ino > entry->ino)
2908 else if (gen < entry->gen)
2910 else if (gen > entry->gen)
2918 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
2920 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
2925 static void free_orphan_dir_info(struct send_ctx *sctx,
2926 struct orphan_dir_info *odi)
2930 rb_erase(&odi->node, &sctx->orphan_dirs);
2935 * Returns 1 if a directory can be removed at this point in time.
2936 * We check this by iterating all dir items and checking if the inode behind
2937 * the dir item was already processed.
2939 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2943 struct btrfs_root *root = sctx->parent_root;
2944 struct btrfs_path *path;
2945 struct btrfs_key key;
2946 struct btrfs_key found_key;
2947 struct btrfs_key loc;
2948 struct btrfs_dir_item *di;
2949 struct orphan_dir_info *odi = NULL;
2952 * Don't try to rmdir the top/root subvolume dir.
2954 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2957 path = alloc_path_for_send();
2962 key.type = BTRFS_DIR_INDEX_KEY;
2965 odi = get_orphan_dir_info(sctx, dir, dir_gen);
2967 key.offset = odi->last_dir_index_offset;
2969 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2974 struct waiting_dir_move *dm;
2976 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2977 ret = btrfs_next_leaf(root, path);
2984 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2986 if (found_key.objectid != key.objectid ||
2987 found_key.type != key.type)
2990 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2991 struct btrfs_dir_item);
2992 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2994 dm = get_waiting_dir_move(sctx, loc.objectid);
2996 odi = add_orphan_dir_info(sctx, dir, dir_gen);
3002 odi->last_dir_index_offset = found_key.offset;
3003 dm->rmdir_ino = dir;
3004 dm->rmdir_gen = dir_gen;
3009 if (loc.objectid > send_progress) {
3010 odi = add_orphan_dir_info(sctx, dir, dir_gen);
3016 odi->last_dir_index_offset = found_key.offset;
3023 free_orphan_dir_info(sctx, odi);
3028 btrfs_free_path(path);
3032 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3034 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3036 return entry != NULL;
3039 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3041 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3042 struct rb_node *parent = NULL;
3043 struct waiting_dir_move *entry, *dm;
3045 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
3051 dm->orphanized = orphanized;
3055 entry = rb_entry(parent, struct waiting_dir_move, node);
3056 if (ino < entry->ino) {
3058 } else if (ino > entry->ino) {
3059 p = &(*p)->rb_right;
3066 rb_link_node(&dm->node, parent, p);
3067 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3071 static struct waiting_dir_move *
3072 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3074 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3075 struct waiting_dir_move *entry;
3078 entry = rb_entry(n, struct waiting_dir_move, node);
3079 if (ino < entry->ino)
3081 else if (ino > entry->ino)
3089 static void free_waiting_dir_move(struct send_ctx *sctx,
3090 struct waiting_dir_move *dm)
3094 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3098 static int add_pending_dir_move(struct send_ctx *sctx,
3102 struct list_head *new_refs,
3103 struct list_head *deleted_refs,
3104 const bool is_orphan)
3106 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3107 struct rb_node *parent = NULL;
3108 struct pending_dir_move *entry = NULL, *pm;
3109 struct recorded_ref *cur;
3113 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3116 pm->parent_ino = parent_ino;
3119 INIT_LIST_HEAD(&pm->list);
3120 INIT_LIST_HEAD(&pm->update_refs);
3121 RB_CLEAR_NODE(&pm->node);
3125 entry = rb_entry(parent, struct pending_dir_move, node);
3126 if (parent_ino < entry->parent_ino) {
3128 } else if (parent_ino > entry->parent_ino) {
3129 p = &(*p)->rb_right;
3136 list_for_each_entry(cur, deleted_refs, list) {
3137 ret = dup_ref(cur, &pm->update_refs);
3141 list_for_each_entry(cur, new_refs, list) {
3142 ret = dup_ref(cur, &pm->update_refs);
3147 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3152 list_add_tail(&pm->list, &entry->list);
3154 rb_link_node(&pm->node, parent, p);
3155 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3160 __free_recorded_refs(&pm->update_refs);
3166 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3169 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3170 struct pending_dir_move *entry;
3173 entry = rb_entry(n, struct pending_dir_move, node);
3174 if (parent_ino < entry->parent_ino)
3176 else if (parent_ino > entry->parent_ino)
3184 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3185 u64 ino, u64 gen, u64 *ancestor_ino)
3188 u64 parent_inode = 0;
3190 u64 start_ino = ino;
3193 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3194 fs_path_reset(name);
3196 if (is_waiting_for_rm(sctx, ino, gen))
3198 if (is_waiting_for_move(sctx, ino)) {
3199 if (*ancestor_ino == 0)
3200 *ancestor_ino = ino;
3201 ret = get_first_ref(sctx->parent_root, ino,
3202 &parent_inode, &parent_gen, name);
3204 ret = __get_cur_name_and_parent(sctx, ino, gen,
3214 if (parent_inode == start_ino) {
3216 if (*ancestor_ino == 0)
3217 *ancestor_ino = ino;
3226 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3228 struct fs_path *from_path = NULL;
3229 struct fs_path *to_path = NULL;
3230 struct fs_path *name = NULL;
3231 u64 orig_progress = sctx->send_progress;
3232 struct recorded_ref *cur;
3233 u64 parent_ino, parent_gen;
3234 struct waiting_dir_move *dm = NULL;
3241 name = fs_path_alloc();
3242 from_path = fs_path_alloc();
3243 if (!name || !from_path) {
3248 dm = get_waiting_dir_move(sctx, pm->ino);
3250 rmdir_ino = dm->rmdir_ino;
3251 rmdir_gen = dm->rmdir_gen;
3252 is_orphan = dm->orphanized;
3253 free_waiting_dir_move(sctx, dm);
3256 ret = gen_unique_name(sctx, pm->ino,
3257 pm->gen, from_path);
3259 ret = get_first_ref(sctx->parent_root, pm->ino,
3260 &parent_ino, &parent_gen, name);
3263 ret = get_cur_path(sctx, parent_ino, parent_gen,
3267 ret = fs_path_add_path(from_path, name);
3272 sctx->send_progress = sctx->cur_ino + 1;
3273 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3277 LIST_HEAD(deleted_refs);
3278 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3279 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3280 &pm->update_refs, &deleted_refs,
3285 dm = get_waiting_dir_move(sctx, pm->ino);
3287 dm->rmdir_ino = rmdir_ino;
3288 dm->rmdir_gen = rmdir_gen;
3292 fs_path_reset(name);
3295 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3299 ret = send_rename(sctx, from_path, to_path);
3304 struct orphan_dir_info *odi;
3307 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
3309 /* already deleted */
3314 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
3320 name = fs_path_alloc();
3325 ret = get_cur_path(sctx, rmdir_ino, gen, name);
3328 ret = send_rmdir(sctx, name);
3334 ret = send_utimes(sctx, pm->ino, pm->gen);
3339 * After rename/move, need to update the utimes of both new parent(s)
3340 * and old parent(s).
3342 list_for_each_entry(cur, &pm->update_refs, list) {
3344 * The parent inode might have been deleted in the send snapshot
3346 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3347 NULL, NULL, NULL, NULL, NULL);
3348 if (ret == -ENOENT) {
3355 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3362 fs_path_free(from_path);
3363 fs_path_free(to_path);
3364 sctx->send_progress = orig_progress;
3369 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3371 if (!list_empty(&m->list))
3373 if (!RB_EMPTY_NODE(&m->node))
3374 rb_erase(&m->node, &sctx->pending_dir_moves);
3375 __free_recorded_refs(&m->update_refs);
3379 static void tail_append_pending_moves(struct send_ctx *sctx,
3380 struct pending_dir_move *moves,
3381 struct list_head *stack)
3383 if (list_empty(&moves->list)) {
3384 list_add_tail(&moves->list, stack);
3387 list_splice_init(&moves->list, &list);
3388 list_add_tail(&moves->list, stack);
3389 list_splice_tail(&list, stack);
3391 if (!RB_EMPTY_NODE(&moves->node)) {
3392 rb_erase(&moves->node, &sctx->pending_dir_moves);
3393 RB_CLEAR_NODE(&moves->node);
3397 static int apply_children_dir_moves(struct send_ctx *sctx)
3399 struct pending_dir_move *pm;
3400 struct list_head stack;
3401 u64 parent_ino = sctx->cur_ino;
3404 pm = get_pending_dir_moves(sctx, parent_ino);
3408 INIT_LIST_HEAD(&stack);
3409 tail_append_pending_moves(sctx, pm, &stack);
3411 while (!list_empty(&stack)) {
3412 pm = list_first_entry(&stack, struct pending_dir_move, list);
3413 parent_ino = pm->ino;
3414 ret = apply_dir_move(sctx, pm);
3415 free_pending_move(sctx, pm);
3418 pm = get_pending_dir_moves(sctx, parent_ino);
3420 tail_append_pending_moves(sctx, pm, &stack);
3425 while (!list_empty(&stack)) {
3426 pm = list_first_entry(&stack, struct pending_dir_move, list);
3427 free_pending_move(sctx, pm);
3433 * We might need to delay a directory rename even when no ancestor directory
3434 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3435 * renamed. This happens when we rename a directory to the old name (the name
3436 * in the parent root) of some other unrelated directory that got its rename
3437 * delayed due to some ancestor with higher number that got renamed.
3443 * |---- a/ (ino 257)
3444 * | |---- file (ino 260)
3446 * |---- b/ (ino 258)
3447 * |---- c/ (ino 259)
3451 * |---- a/ (ino 258)
3452 * |---- x/ (ino 259)
3453 * |---- y/ (ino 257)
3454 * |----- file (ino 260)
3456 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3457 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3458 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3461 * 1 - rename 259 from 'c' to 'x'
3462 * 2 - rename 257 from 'a' to 'x/y'
3463 * 3 - rename 258 from 'b' to 'a'
3465 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3466 * be done right away and < 0 on error.
3468 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3469 struct recorded_ref *parent_ref,
3470 const bool is_orphan)
3472 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3473 struct btrfs_path *path;
3474 struct btrfs_key key;
3475 struct btrfs_key di_key;
3476 struct btrfs_dir_item *di;
3480 struct waiting_dir_move *wdm;
3482 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3485 path = alloc_path_for_send();
3489 key.objectid = parent_ref->dir;
3490 key.type = BTRFS_DIR_ITEM_KEY;
3491 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3493 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3496 } else if (ret > 0) {
3501 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3502 parent_ref->name_len);
3508 * di_key.objectid has the number of the inode that has a dentry in the
3509 * parent directory with the same name that sctx->cur_ino is being
3510 * renamed to. We need to check if that inode is in the send root as
3511 * well and if it is currently marked as an inode with a pending rename,
3512 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3513 * that it happens after that other inode is renamed.
3515 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3516 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3521 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3522 &left_gen, NULL, NULL, NULL, NULL);
3525 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3526 &right_gen, NULL, NULL, NULL, NULL);
3533 /* Different inode, no need to delay the rename of sctx->cur_ino */
3534 if (right_gen != left_gen) {
3539 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3540 if (wdm && !wdm->orphanized) {
3541 ret = add_pending_dir_move(sctx,
3543 sctx->cur_inode_gen,
3546 &sctx->deleted_refs,
3552 btrfs_free_path(path);
3557 * Check if inode ino2, or any of its ancestors, is inode ino1.
3558 * Return 1 if true, 0 if false and < 0 on error.
3560 static int check_ino_in_path(struct btrfs_root *root,
3565 struct fs_path *fs_path)
3570 return ino1_gen == ino2_gen;
3572 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3577 fs_path_reset(fs_path);
3578 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3582 return parent_gen == ino1_gen;
3589 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3590 * possible path (in case ino2 is not a directory and has multiple hard links).
3591 * Return 1 if true, 0 if false and < 0 on error.
3593 static int is_ancestor(struct btrfs_root *root,
3597 struct fs_path *fs_path)
3599 bool free_fs_path = false;
3601 struct btrfs_path *path = NULL;
3602 struct btrfs_key key;
3605 fs_path = fs_path_alloc();
3608 free_fs_path = true;
3611 path = alloc_path_for_send();
3617 key.objectid = ino2;
3618 key.type = BTRFS_INODE_REF_KEY;
3621 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3626 struct extent_buffer *leaf = path->nodes[0];
3627 int slot = path->slots[0];
3631 if (slot >= btrfs_header_nritems(leaf)) {
3632 ret = btrfs_next_leaf(root, path);
3640 btrfs_item_key_to_cpu(leaf, &key, slot);
3641 if (key.objectid != ino2)
3643 if (key.type != BTRFS_INODE_REF_KEY &&
3644 key.type != BTRFS_INODE_EXTREF_KEY)
3647 item_size = btrfs_item_size_nr(leaf, slot);
3648 while (cur_offset < item_size) {
3652 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3654 struct btrfs_inode_extref *extref;
3656 ptr = btrfs_item_ptr_offset(leaf, slot);
3657 extref = (struct btrfs_inode_extref *)
3659 parent = btrfs_inode_extref_parent(leaf,
3661 cur_offset += sizeof(*extref);
3662 cur_offset += btrfs_inode_extref_name_len(leaf,
3665 parent = key.offset;
3666 cur_offset = item_size;
3669 ret = get_inode_info(root, parent, NULL, &parent_gen,
3670 NULL, NULL, NULL, NULL);
3673 ret = check_ino_in_path(root, ino1, ino1_gen,
3674 parent, parent_gen, fs_path);
3682 btrfs_free_path(path);
3684 fs_path_free(fs_path);
3688 static int wait_for_parent_move(struct send_ctx *sctx,
3689 struct recorded_ref *parent_ref,
3690 const bool is_orphan)
3693 u64 ino = parent_ref->dir;
3694 u64 ino_gen = parent_ref->dir_gen;
3695 u64 parent_ino_before, parent_ino_after;
3696 struct fs_path *path_before = NULL;
3697 struct fs_path *path_after = NULL;
3700 path_after = fs_path_alloc();
3701 path_before = fs_path_alloc();
3702 if (!path_after || !path_before) {
3708 * Our current directory inode may not yet be renamed/moved because some
3709 * ancestor (immediate or not) has to be renamed/moved first. So find if
3710 * such ancestor exists and make sure our own rename/move happens after
3711 * that ancestor is processed to avoid path build infinite loops (done
3712 * at get_cur_path()).
3714 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3715 u64 parent_ino_after_gen;
3717 if (is_waiting_for_move(sctx, ino)) {
3719 * If the current inode is an ancestor of ino in the
3720 * parent root, we need to delay the rename of the
3721 * current inode, otherwise don't delayed the rename
3722 * because we can end up with a circular dependency
3723 * of renames, resulting in some directories never
3724 * getting the respective rename operations issued in
3725 * the send stream or getting into infinite path build
3728 ret = is_ancestor(sctx->parent_root,
3729 sctx->cur_ino, sctx->cur_inode_gen,
3735 fs_path_reset(path_before);
3736 fs_path_reset(path_after);
3738 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3739 &parent_ino_after_gen, path_after);
3742 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3744 if (ret < 0 && ret != -ENOENT) {
3746 } else if (ret == -ENOENT) {
3751 len1 = fs_path_len(path_before);
3752 len2 = fs_path_len(path_after);
3753 if (ino > sctx->cur_ino &&
3754 (parent_ino_before != parent_ino_after || len1 != len2 ||
3755 memcmp(path_before->start, path_after->start, len1))) {
3758 ret = get_inode_info(sctx->parent_root, ino, NULL,
3759 &parent_ino_gen, NULL, NULL, NULL,
3763 if (ino_gen == parent_ino_gen) {
3768 ino = parent_ino_after;
3769 ino_gen = parent_ino_after_gen;
3773 fs_path_free(path_before);
3774 fs_path_free(path_after);
3777 ret = add_pending_dir_move(sctx,
3779 sctx->cur_inode_gen,
3782 &sctx->deleted_refs,
3791 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3794 struct fs_path *new_path;
3797 * Our reference's name member points to its full_path member string, so
3798 * we use here a new path.
3800 new_path = fs_path_alloc();
3804 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3806 fs_path_free(new_path);
3809 ret = fs_path_add(new_path, ref->name, ref->name_len);
3811 fs_path_free(new_path);
3815 fs_path_free(ref->full_path);
3816 set_ref_path(ref, new_path);
3822 * When processing the new references for an inode we may orphanize an existing
3823 * directory inode because its old name conflicts with one of the new references
3824 * of the current inode. Later, when processing another new reference of our
3825 * inode, we might need to orphanize another inode, but the path we have in the
3826 * reference reflects the pre-orphanization name of the directory we previously
3827 * orphanized. For example:
3829 * parent snapshot looks like:
3832 * |----- f1 (ino 257)
3833 * |----- f2 (ino 258)
3834 * |----- d1/ (ino 259)
3835 * |----- d2/ (ino 260)
3837 * send snapshot looks like:
3840 * |----- d1 (ino 258)
3841 * |----- f2/ (ino 259)
3842 * |----- f2_link/ (ino 260)
3843 * | |----- f1 (ino 257)
3845 * |----- d2 (ino 258)
3847 * When processing inode 257 we compute the name for inode 259 as "d1", and we
3848 * cache it in the name cache. Later when we start processing inode 258, when
3849 * collecting all its new references we set a full path of "d1/d2" for its new
3850 * reference with name "d2". When we start processing the new references we
3851 * start by processing the new reference with name "d1", and this results in
3852 * orphanizing inode 259, since its old reference causes a conflict. Then we
3853 * move on the next new reference, with name "d2", and we find out we must
3854 * orphanize inode 260, as its old reference conflicts with ours - but for the
3855 * orphanization we use a source path corresponding to the path we stored in the
3856 * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
3857 * receiver fail since the path component "d1/" no longer exists, it was renamed
3858 * to "o259-6-0/" when processing the previous new reference. So in this case we
3859 * must recompute the path in the new reference and use it for the new
3860 * orphanization operation.
3862 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3867 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3871 fs_path_reset(ref->full_path);
3872 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3876 ret = fs_path_add(ref->full_path, name, ref->name_len);
3880 /* Update the reference's base name pointer. */
3881 set_ref_path(ref, ref->full_path);
3888 * This does all the move/link/unlink/rmdir magic.
3890 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3892 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
3894 struct recorded_ref *cur;
3895 struct recorded_ref *cur2;
3896 struct list_head check_dirs;
3897 struct fs_path *valid_path = NULL;
3901 int did_overwrite = 0;
3903 u64 last_dir_ino_rm = 0;
3904 bool can_rename = true;
3905 bool orphanized_dir = false;
3906 bool orphanized_ancestor = false;
3908 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
3911 * This should never happen as the root dir always has the same ref
3912 * which is always '..'
3914 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3915 INIT_LIST_HEAD(&check_dirs);
3917 valid_path = fs_path_alloc();
3924 * First, check if the first ref of the current inode was overwritten
3925 * before. If yes, we know that the current inode was already orphanized
3926 * and thus use the orphan name. If not, we can use get_cur_path to
3927 * get the path of the first ref as it would like while receiving at
3928 * this point in time.
3929 * New inodes are always orphan at the beginning, so force to use the
3930 * orphan name in this case.
3931 * The first ref is stored in valid_path and will be updated if it
3932 * gets moved around.
3934 if (!sctx->cur_inode_new) {
3935 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3936 sctx->cur_inode_gen);
3942 if (sctx->cur_inode_new || did_overwrite) {
3943 ret = gen_unique_name(sctx, sctx->cur_ino,
3944 sctx->cur_inode_gen, valid_path);
3949 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3956 * Before doing any rename and link operations, do a first pass on the
3957 * new references to orphanize any unprocessed inodes that may have a
3958 * reference that conflicts with one of the new references of the current
3959 * inode. This needs to happen first because a new reference may conflict
3960 * with the old reference of a parent directory, so we must make sure
3961 * that the path used for link and rename commands don't use an
3962 * orphanized name when an ancestor was not yet orphanized.
3969 * |----- testdir/ (ino 259)
3970 * | |----- a (ino 257)
3972 * |----- b (ino 258)
3977 * |----- testdir_2/ (ino 259)
3978 * | |----- a (ino 260)
3980 * |----- testdir (ino 257)
3981 * |----- b (ino 257)
3982 * |----- b2 (ino 258)
3984 * Processing the new reference for inode 257 with name "b" may happen
3985 * before processing the new reference with name "testdir". If so, we
3986 * must make sure that by the time we send a link command to create the
3987 * hard link "b", inode 259 was already orphanized, since the generated
3988 * path in "valid_path" already contains the orphanized name for 259.
3989 * We are processing inode 257, so only later when processing 259 we do
3990 * the rename operation to change its temporary (orphanized) name to
3993 list_for_each_entry(cur, &sctx->new_refs, list) {
3994 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3997 if (ret == inode_state_will_create)
4001 * Check if this new ref would overwrite the first ref of another
4002 * unprocessed inode. If yes, orphanize the overwritten inode.
4003 * If we find an overwritten ref that is not the first ref,
4006 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4007 cur->name, cur->name_len,
4008 &ow_inode, &ow_gen, &ow_mode);
4012 ret = is_first_ref(sctx->parent_root,
4013 ow_inode, cur->dir, cur->name,
4018 struct name_cache_entry *nce;
4019 struct waiting_dir_move *wdm;
4021 if (orphanized_dir) {
4022 ret = refresh_ref_path(sctx, cur);
4027 ret = orphanize_inode(sctx, ow_inode, ow_gen,
4031 if (S_ISDIR(ow_mode))
4032 orphanized_dir = true;
4035 * If ow_inode has its rename operation delayed
4036 * make sure that its orphanized name is used in
4037 * the source path when performing its rename
4040 if (is_waiting_for_move(sctx, ow_inode)) {
4041 wdm = get_waiting_dir_move(sctx,
4044 wdm->orphanized = true;
4048 * Make sure we clear our orphanized inode's
4049 * name from the name cache. This is because the
4050 * inode ow_inode might be an ancestor of some
4051 * other inode that will be orphanized as well
4052 * later and has an inode number greater than
4053 * sctx->send_progress. We need to prevent
4054 * future name lookups from using the old name
4055 * and get instead the orphan name.
4057 nce = name_cache_search(sctx, ow_inode, ow_gen);
4059 name_cache_delete(sctx, nce);
4064 * ow_inode might currently be an ancestor of
4065 * cur_ino, therefore compute valid_path (the
4066 * current path of cur_ino) again because it
4067 * might contain the pre-orphanization name of
4068 * ow_inode, which is no longer valid.
4070 ret = is_ancestor(sctx->parent_root,
4072 sctx->cur_ino, NULL);
4074 orphanized_ancestor = true;
4075 fs_path_reset(valid_path);
4076 ret = get_cur_path(sctx, sctx->cur_ino,
4077 sctx->cur_inode_gen,
4083 ret = send_unlink(sctx, cur->full_path);
4091 list_for_each_entry(cur, &sctx->new_refs, list) {
4093 * We may have refs where the parent directory does not exist
4094 * yet. This happens if the parent directories inum is higher
4095 * than the current inum. To handle this case, we create the
4096 * parent directory out of order. But we need to check if this
4097 * did already happen before due to other refs in the same dir.
4099 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4102 if (ret == inode_state_will_create) {
4105 * First check if any of the current inodes refs did
4106 * already create the dir.
4108 list_for_each_entry(cur2, &sctx->new_refs, list) {
4111 if (cur2->dir == cur->dir) {
4118 * If that did not happen, check if a previous inode
4119 * did already create the dir.
4122 ret = did_create_dir(sctx, cur->dir);
4126 ret = send_create_inode(sctx, cur->dir);
4132 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4133 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4142 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4144 ret = wait_for_parent_move(sctx, cur, is_orphan);
4154 * link/move the ref to the new place. If we have an orphan
4155 * inode, move it and update valid_path. If not, link or move
4156 * it depending on the inode mode.
4158 if (is_orphan && can_rename) {
4159 ret = send_rename(sctx, valid_path, cur->full_path);
4163 ret = fs_path_copy(valid_path, cur->full_path);
4166 } else if (can_rename) {
4167 if (S_ISDIR(sctx->cur_inode_mode)) {
4169 * Dirs can't be linked, so move it. For moved
4170 * dirs, we always have one new and one deleted
4171 * ref. The deleted ref is ignored later.
4173 ret = send_rename(sctx, valid_path,
4176 ret = fs_path_copy(valid_path,
4182 * We might have previously orphanized an inode
4183 * which is an ancestor of our current inode,
4184 * so our reference's full path, which was
4185 * computed before any such orphanizations, must
4188 if (orphanized_dir) {
4189 ret = update_ref_path(sctx, cur);
4193 ret = send_link(sctx, cur->full_path,
4199 ret = dup_ref(cur, &check_dirs);
4204 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4206 * Check if we can already rmdir the directory. If not,
4207 * orphanize it. For every dir item inside that gets deleted
4208 * later, we do this check again and rmdir it then if possible.
4209 * See the use of check_dirs for more details.
4211 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4216 ret = send_rmdir(sctx, valid_path);
4219 } else if (!is_orphan) {
4220 ret = orphanize_inode(sctx, sctx->cur_ino,
4221 sctx->cur_inode_gen, valid_path);
4227 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4228 ret = dup_ref(cur, &check_dirs);
4232 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4233 !list_empty(&sctx->deleted_refs)) {
4235 * We have a moved dir. Add the old parent to check_dirs
4237 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4239 ret = dup_ref(cur, &check_dirs);
4242 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4244 * We have a non dir inode. Go through all deleted refs and
4245 * unlink them if they were not already overwritten by other
4248 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4249 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4250 sctx->cur_ino, sctx->cur_inode_gen,
4251 cur->name, cur->name_len);
4256 * If we orphanized any ancestor before, we need
4257 * to recompute the full path for deleted names,
4258 * since any such path was computed before we
4259 * processed any references and orphanized any
4262 if (orphanized_ancestor) {
4263 ret = update_ref_path(sctx, cur);
4267 ret = send_unlink(sctx, cur->full_path);
4271 ret = dup_ref(cur, &check_dirs);
4276 * If the inode is still orphan, unlink the orphan. This may
4277 * happen when a previous inode did overwrite the first ref
4278 * of this inode and no new refs were added for the current
4279 * inode. Unlinking does not mean that the inode is deleted in
4280 * all cases. There may still be links to this inode in other
4284 ret = send_unlink(sctx, valid_path);
4291 * We did collect all parent dirs where cur_inode was once located. We
4292 * now go through all these dirs and check if they are pending for
4293 * deletion and if it's finally possible to perform the rmdir now.
4294 * We also update the inode stats of the parent dirs here.
4296 list_for_each_entry(cur, &check_dirs, list) {
4298 * In case we had refs into dirs that were not processed yet,
4299 * we don't need to do the utime and rmdir logic for these dirs.
4300 * The dir will be processed later.
4302 if (cur->dir > sctx->cur_ino)
4305 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4309 if (ret == inode_state_did_create ||
4310 ret == inode_state_no_change) {
4311 /* TODO delayed utimes */
4312 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
4315 } else if (ret == inode_state_did_delete &&
4316 cur->dir != last_dir_ino_rm) {
4317 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4322 ret = get_cur_path(sctx, cur->dir,
4323 cur->dir_gen, valid_path);
4326 ret = send_rmdir(sctx, valid_path);
4329 last_dir_ino_rm = cur->dir;
4337 __free_recorded_refs(&check_dirs);
4338 free_recorded_refs(sctx);
4339 fs_path_free(valid_path);
4343 static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4344 void *ctx, struct list_head *refs)
4347 struct send_ctx *sctx = ctx;
4351 p = fs_path_alloc();
4355 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
4360 ret = get_cur_path(sctx, dir, gen, p);
4363 ret = fs_path_add_path(p, name);
4367 ret = __record_ref(refs, dir, gen, p);
4375 static int __record_new_ref(int num, u64 dir, int index,
4376 struct fs_path *name,
4379 struct send_ctx *sctx = ctx;
4380 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
4384 static int __record_deleted_ref(int num, u64 dir, int index,
4385 struct fs_path *name,
4388 struct send_ctx *sctx = ctx;
4389 return record_ref(sctx->parent_root, dir, name, ctx,
4390 &sctx->deleted_refs);
4393 static int record_new_ref(struct send_ctx *sctx)
4397 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4398 sctx->cmp_key, 0, __record_new_ref, sctx);
4407 static int record_deleted_ref(struct send_ctx *sctx)
4411 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4412 sctx->cmp_key, 0, __record_deleted_ref, sctx);
4421 struct find_ref_ctx {
4424 struct btrfs_root *root;
4425 struct fs_path *name;
4429 static int __find_iref(int num, u64 dir, int index,
4430 struct fs_path *name,
4433 struct find_ref_ctx *ctx = ctx_;
4437 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4438 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
4440 * To avoid doing extra lookups we'll only do this if everything
4443 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4447 if (dir_gen != ctx->dir_gen)
4449 ctx->found_idx = num;
4455 static int find_iref(struct btrfs_root *root,
4456 struct btrfs_path *path,
4457 struct btrfs_key *key,
4458 u64 dir, u64 dir_gen, struct fs_path *name)
4461 struct find_ref_ctx ctx;
4465 ctx.dir_gen = dir_gen;
4469 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
4473 if (ctx.found_idx == -1)
4476 return ctx.found_idx;
4479 static int __record_changed_new_ref(int num, u64 dir, int index,
4480 struct fs_path *name,
4485 struct send_ctx *sctx = ctx;
4487 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4492 ret = find_iref(sctx->parent_root, sctx->right_path,
4493 sctx->cmp_key, dir, dir_gen, name);
4495 ret = __record_new_ref(num, dir, index, name, sctx);
4502 static int __record_changed_deleted_ref(int num, u64 dir, int index,
4503 struct fs_path *name,
4508 struct send_ctx *sctx = ctx;
4510 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4515 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
4516 dir, dir_gen, name);
4518 ret = __record_deleted_ref(num, dir, index, name, sctx);
4525 static int record_changed_ref(struct send_ctx *sctx)
4529 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4530 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4533 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4534 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4544 * Record and process all refs at once. Needed when an inode changes the
4545 * generation number, which means that it was deleted and recreated.
4547 static int process_all_refs(struct send_ctx *sctx,
4548 enum btrfs_compare_tree_result cmd)
4551 struct btrfs_root *root;
4552 struct btrfs_path *path;
4553 struct btrfs_key key;
4554 struct btrfs_key found_key;
4555 struct extent_buffer *eb;
4557 iterate_inode_ref_t cb;
4558 int pending_move = 0;
4560 path = alloc_path_for_send();
4564 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4565 root = sctx->send_root;
4566 cb = __record_new_ref;
4567 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4568 root = sctx->parent_root;
4569 cb = __record_deleted_ref;
4571 btrfs_err(sctx->send_root->fs_info,
4572 "Wrong command %d in process_all_refs", cmd);
4577 key.objectid = sctx->cmp_key->objectid;
4578 key.type = BTRFS_INODE_REF_KEY;
4580 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4585 eb = path->nodes[0];
4586 slot = path->slots[0];
4587 if (slot >= btrfs_header_nritems(eb)) {
4588 ret = btrfs_next_leaf(root, path);
4596 btrfs_item_key_to_cpu(eb, &found_key, slot);
4598 if (found_key.objectid != key.objectid ||
4599 (found_key.type != BTRFS_INODE_REF_KEY &&
4600 found_key.type != BTRFS_INODE_EXTREF_KEY))
4603 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4609 btrfs_release_path(path);
4612 * We don't actually care about pending_move as we are simply
4613 * re-creating this inode and will be rename'ing it into place once we
4614 * rename the parent directory.
4616 ret = process_recorded_refs(sctx, &pending_move);
4618 btrfs_free_path(path);
4622 static int send_set_xattr(struct send_ctx *sctx,
4623 struct fs_path *path,
4624 const char *name, int name_len,
4625 const char *data, int data_len)
4629 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4633 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4634 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4635 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4637 ret = send_cmd(sctx);
4644 static int send_remove_xattr(struct send_ctx *sctx,
4645 struct fs_path *path,
4646 const char *name, int name_len)
4650 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4654 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4655 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4657 ret = send_cmd(sctx);
4664 static int __process_new_xattr(int num, struct btrfs_key *di_key,
4665 const char *name, int name_len,
4666 const char *data, int data_len,
4670 struct send_ctx *sctx = ctx;
4672 struct posix_acl_xattr_header dummy_acl;
4674 /* Capabilities are emitted by finish_inode_if_needed */
4675 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4678 p = fs_path_alloc();
4683 * This hack is needed because empty acls are stored as zero byte
4684 * data in xattrs. Problem with that is, that receiving these zero byte
4685 * acls will fail later. To fix this, we send a dummy acl list that
4686 * only contains the version number and no entries.
4688 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4689 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4690 if (data_len == 0) {
4691 dummy_acl.a_version =
4692 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4693 data = (char *)&dummy_acl;
4694 data_len = sizeof(dummy_acl);
4698 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4702 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4709 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4710 const char *name, int name_len,
4711 const char *data, int data_len,
4715 struct send_ctx *sctx = ctx;
4718 p = fs_path_alloc();
4722 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4726 ret = send_remove_xattr(sctx, p, name, name_len);
4733 static int process_new_xattr(struct send_ctx *sctx)
4737 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4738 __process_new_xattr, sctx);
4743 static int process_deleted_xattr(struct send_ctx *sctx)
4745 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4746 __process_deleted_xattr, sctx);
4749 struct find_xattr_ctx {
4757 static int __find_xattr(int num, struct btrfs_key *di_key,
4758 const char *name, int name_len,
4759 const char *data, int data_len,
4760 u8 type, void *vctx)
4762 struct find_xattr_ctx *ctx = vctx;
4764 if (name_len == ctx->name_len &&
4765 strncmp(name, ctx->name, name_len) == 0) {
4766 ctx->found_idx = num;
4767 ctx->found_data_len = data_len;
4768 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4769 if (!ctx->found_data)
4776 static int find_xattr(struct btrfs_root *root,
4777 struct btrfs_path *path,
4778 struct btrfs_key *key,
4779 const char *name, int name_len,
4780 char **data, int *data_len)
4783 struct find_xattr_ctx ctx;
4786 ctx.name_len = name_len;
4788 ctx.found_data = NULL;
4789 ctx.found_data_len = 0;
4791 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
4795 if (ctx.found_idx == -1)
4798 *data = ctx.found_data;
4799 *data_len = ctx.found_data_len;
4801 kfree(ctx.found_data);
4803 return ctx.found_idx;
4807 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4808 const char *name, int name_len,
4809 const char *data, int data_len,
4813 struct send_ctx *sctx = ctx;
4814 char *found_data = NULL;
4815 int found_data_len = 0;
4817 ret = find_xattr(sctx->parent_root, sctx->right_path,
4818 sctx->cmp_key, name, name_len, &found_data,
4820 if (ret == -ENOENT) {
4821 ret = __process_new_xattr(num, di_key, name, name_len, data,
4822 data_len, type, ctx);
4823 } else if (ret >= 0) {
4824 if (data_len != found_data_len ||
4825 memcmp(data, found_data, data_len)) {
4826 ret = __process_new_xattr(num, di_key, name, name_len,
4827 data, data_len, type, ctx);
4837 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4838 const char *name, int name_len,
4839 const char *data, int data_len,
4843 struct send_ctx *sctx = ctx;
4845 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4846 name, name_len, NULL, NULL);
4848 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4849 data_len, type, ctx);
4856 static int process_changed_xattr(struct send_ctx *sctx)
4860 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4861 __process_changed_new_xattr, sctx);
4864 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4865 __process_changed_deleted_xattr, sctx);
4871 static int process_all_new_xattrs(struct send_ctx *sctx)
4874 struct btrfs_root *root;
4875 struct btrfs_path *path;
4876 struct btrfs_key key;
4877 struct btrfs_key found_key;
4878 struct extent_buffer *eb;
4881 path = alloc_path_for_send();
4885 root = sctx->send_root;
4887 key.objectid = sctx->cmp_key->objectid;
4888 key.type = BTRFS_XATTR_ITEM_KEY;
4890 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4895 eb = path->nodes[0];
4896 slot = path->slots[0];
4897 if (slot >= btrfs_header_nritems(eb)) {
4898 ret = btrfs_next_leaf(root, path);
4901 } else if (ret > 0) {
4908 btrfs_item_key_to_cpu(eb, &found_key, slot);
4909 if (found_key.objectid != key.objectid ||
4910 found_key.type != key.type) {
4915 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
4923 btrfs_free_path(path);
4927 static inline u64 max_send_read_size(const struct send_ctx *sctx)
4929 return sctx->send_max_size - SZ_16K;
4932 static int put_data_header(struct send_ctx *sctx, u32 len)
4934 struct btrfs_tlv_header *hdr;
4936 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
4938 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
4939 put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
4940 put_unaligned_le16(len, &hdr->tlv_len);
4941 sctx->send_size += sizeof(*hdr);
4945 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
4947 struct btrfs_root *root = sctx->send_root;
4948 struct btrfs_fs_info *fs_info = root->fs_info;
4949 struct inode *inode;
4952 pgoff_t index = offset >> PAGE_SHIFT;
4954 unsigned pg_offset = offset_in_page(offset);
4957 ret = put_data_header(sctx, len);
4961 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
4963 return PTR_ERR(inode);
4965 last_index = (offset + len - 1) >> PAGE_SHIFT;
4967 /* initial readahead */
4968 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4969 file_ra_state_init(&sctx->ra, inode->i_mapping);
4971 while (index <= last_index) {
4972 unsigned cur_len = min_t(unsigned, len,
4973 PAGE_SIZE - pg_offset);
4975 page = find_lock_page(inode->i_mapping, index);
4977 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4978 NULL, index, last_index + 1 - index);
4980 page = find_or_create_page(inode->i_mapping, index,
4988 if (PageReadahead(page)) {
4989 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4990 NULL, page, index, last_index + 1 - index);
4993 if (!PageUptodate(page)) {
4994 btrfs_readpage(NULL, page);
4996 if (!PageUptodate(page)) {
5005 memcpy(sctx->send_buf + sctx->send_size, addr + pg_offset,
5013 sctx->send_size += cur_len;
5020 * Read some bytes from the current inode/file and send a write command to
5023 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
5025 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
5029 p = fs_path_alloc();
5033 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
5035 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5039 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5043 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5044 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5045 ret = put_file_data(sctx, offset, len);
5049 ret = send_cmd(sctx);
5058 * Send a clone command to user space.
5060 static int send_clone(struct send_ctx *sctx,
5061 u64 offset, u32 len,
5062 struct clone_root *clone_root)
5068 btrfs_debug(sctx->send_root->fs_info,
5069 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
5070 offset, len, clone_root->root->root_key.objectid,
5071 clone_root->ino, clone_root->offset);
5073 p = fs_path_alloc();
5077 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5081 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5085 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5086 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5087 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5089 if (clone_root->root == sctx->send_root) {
5090 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
5091 &gen, NULL, NULL, NULL, NULL);
5094 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5096 ret = get_inode_path(clone_root->root, clone_root->ino, p);
5102 * If the parent we're using has a received_uuid set then use that as
5103 * our clone source as that is what we will look for when doing a
5106 * This covers the case that we create a snapshot off of a received
5107 * subvolume and then use that as the parent and try to receive on a
5110 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5111 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5112 clone_root->root->root_item.received_uuid);
5114 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5115 clone_root->root->root_item.uuid);
5116 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
5117 btrfs_root_ctransid(&clone_root->root->root_item));
5118 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5119 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5120 clone_root->offset);
5122 ret = send_cmd(sctx);
5131 * Send an update extent command to user space.
5133 static int send_update_extent(struct send_ctx *sctx,
5134 u64 offset, u32 len)
5139 p = fs_path_alloc();
5143 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5147 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5151 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5152 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5153 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5155 ret = send_cmd(sctx);
5163 static int send_hole(struct send_ctx *sctx, u64 end)
5165 struct fs_path *p = NULL;
5166 u64 read_size = max_send_read_size(sctx);
5167 u64 offset = sctx->cur_inode_last_extent;
5171 * A hole that starts at EOF or beyond it. Since we do not yet support
5172 * fallocate (for extent preallocation and hole punching), sending a
5173 * write of zeroes starting at EOF or beyond would later require issuing
5174 * a truncate operation which would undo the write and achieve nothing.
5176 if (offset >= sctx->cur_inode_size)
5180 * Don't go beyond the inode's i_size due to prealloc extents that start
5183 end = min_t(u64, end, sctx->cur_inode_size);
5185 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5186 return send_update_extent(sctx, offset, end - offset);
5188 p = fs_path_alloc();
5191 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5193 goto tlv_put_failure;
5194 while (offset < end) {
5195 u64 len = min(end - offset, read_size);
5197 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5200 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5201 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5202 ret = put_data_header(sctx, len);
5205 memset(sctx->send_buf + sctx->send_size, 0, len);
5206 sctx->send_size += len;
5207 ret = send_cmd(sctx);
5212 sctx->cur_inode_next_write_offset = offset;
5218 static int send_extent_data(struct send_ctx *sctx,
5222 u64 read_size = max_send_read_size(sctx);
5225 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5226 return send_update_extent(sctx, offset, len);
5228 while (sent < len) {
5229 u64 size = min(len - sent, read_size);
5232 ret = send_write(sctx, offset + sent, size);
5241 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5242 * found, call send_set_xattr function to emit it.
5244 * Return 0 if there isn't a capability, or when the capability was emitted
5245 * successfully, or < 0 if an error occurred.
5247 static int send_capabilities(struct send_ctx *sctx)
5249 struct fs_path *fspath = NULL;
5250 struct btrfs_path *path;
5251 struct btrfs_dir_item *di;
5252 struct extent_buffer *leaf;
5253 unsigned long data_ptr;
5258 path = alloc_path_for_send();
5262 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5263 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5265 /* There is no xattr for this inode */
5267 } else if (IS_ERR(di)) {
5272 leaf = path->nodes[0];
5273 buf_len = btrfs_dir_data_len(leaf, di);
5275 fspath = fs_path_alloc();
5276 buf = kmalloc(buf_len, GFP_KERNEL);
5277 if (!fspath || !buf) {
5282 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5286 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5287 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5289 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5290 strlen(XATTR_NAME_CAPS), buf, buf_len);
5293 fs_path_free(fspath);
5294 btrfs_free_path(path);
5298 static int clone_range(struct send_ctx *sctx,
5299 struct clone_root *clone_root,
5300 const u64 disk_byte,
5305 struct btrfs_path *path;
5306 struct btrfs_key key;
5308 u64 clone_src_i_size = 0;
5311 * Prevent cloning from a zero offset with a length matching the sector
5312 * size because in some scenarios this will make the receiver fail.
5314 * For example, if in the source filesystem the extent at offset 0
5315 * has a length of sectorsize and it was written using direct IO, then
5316 * it can never be an inline extent (even if compression is enabled).
5317 * Then this extent can be cloned in the original filesystem to a non
5318 * zero file offset, but it may not be possible to clone in the
5319 * destination filesystem because it can be inlined due to compression
5320 * on the destination filesystem (as the receiver's write operations are
5321 * always done using buffered IO). The same happens when the original
5322 * filesystem does not have compression enabled but the destination
5325 if (clone_root->offset == 0 &&
5326 len == sctx->send_root->fs_info->sectorsize)
5327 return send_extent_data(sctx, offset, len);
5329 path = alloc_path_for_send();
5334 * There are inodes that have extents that lie behind its i_size. Don't
5335 * accept clones from these extents.
5337 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5338 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5339 btrfs_release_path(path);
5344 * We can't send a clone operation for the entire range if we find
5345 * extent items in the respective range in the source file that
5346 * refer to different extents or if we find holes.
5347 * So check for that and do a mix of clone and regular write/copy
5348 * operations if needed.
5352 * mkfs.btrfs -f /dev/sda
5353 * mount /dev/sda /mnt
5354 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5355 * cp --reflink=always /mnt/foo /mnt/bar
5356 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5357 * btrfs subvolume snapshot -r /mnt /mnt/snap
5359 * If when we send the snapshot and we are processing file bar (which
5360 * has a higher inode number than foo) we blindly send a clone operation
5361 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5362 * a file bar that matches the content of file foo - iow, doesn't match
5363 * the content from bar in the original filesystem.
5365 key.objectid = clone_root->ino;
5366 key.type = BTRFS_EXTENT_DATA_KEY;
5367 key.offset = clone_root->offset;
5368 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5371 if (ret > 0 && path->slots[0] > 0) {
5372 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5373 if (key.objectid == clone_root->ino &&
5374 key.type == BTRFS_EXTENT_DATA_KEY)
5379 struct extent_buffer *leaf = path->nodes[0];
5380 int slot = path->slots[0];
5381 struct btrfs_file_extent_item *ei;
5385 u64 clone_data_offset;
5387 if (slot >= btrfs_header_nritems(leaf)) {
5388 ret = btrfs_next_leaf(clone_root->root, path);
5396 btrfs_item_key_to_cpu(leaf, &key, slot);
5399 * We might have an implicit trailing hole (NO_HOLES feature
5400 * enabled). We deal with it after leaving this loop.
5402 if (key.objectid != clone_root->ino ||
5403 key.type != BTRFS_EXTENT_DATA_KEY)
5406 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5407 type = btrfs_file_extent_type(leaf, ei);
5408 if (type == BTRFS_FILE_EXTENT_INLINE) {
5409 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
5410 ext_len = PAGE_ALIGN(ext_len);
5412 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5415 if (key.offset + ext_len <= clone_root->offset)
5418 if (key.offset > clone_root->offset) {
5419 /* Implicit hole, NO_HOLES feature enabled. */
5420 u64 hole_len = key.offset - clone_root->offset;
5424 ret = send_extent_data(sctx, offset, hole_len);
5432 clone_root->offset += hole_len;
5433 data_offset += hole_len;
5436 if (key.offset >= clone_root->offset + len)
5439 if (key.offset >= clone_src_i_size)
5442 if (key.offset + ext_len > clone_src_i_size)
5443 ext_len = clone_src_i_size - key.offset;
5445 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5446 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5447 clone_root->offset = key.offset;
5448 if (clone_data_offset < data_offset &&
5449 clone_data_offset + ext_len > data_offset) {
5452 extent_offset = data_offset - clone_data_offset;
5453 ext_len -= extent_offset;
5454 clone_data_offset += extent_offset;
5455 clone_root->offset += extent_offset;
5459 clone_len = min_t(u64, ext_len, len);
5461 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5462 clone_data_offset == data_offset) {
5463 const u64 src_end = clone_root->offset + clone_len;
5464 const u64 sectorsize = SZ_64K;
5467 * We can't clone the last block, when its size is not
5468 * sector size aligned, into the middle of a file. If we
5469 * do so, the receiver will get a failure (-EINVAL) when
5470 * trying to clone or will silently corrupt the data in
5471 * the destination file if it's on a kernel without the
5472 * fix introduced by commit ac765f83f1397646
5473 * ("Btrfs: fix data corruption due to cloning of eof
5476 * So issue a clone of the aligned down range plus a
5477 * regular write for the eof block, if we hit that case.
5479 * Also, we use the maximum possible sector size, 64K,
5480 * because we don't know what's the sector size of the
5481 * filesystem that receives the stream, so we have to
5482 * assume the largest possible sector size.
5484 if (src_end == clone_src_i_size &&
5485 !IS_ALIGNED(src_end, sectorsize) &&
5486 offset + clone_len < sctx->cur_inode_size) {
5489 slen = ALIGN_DOWN(src_end - clone_root->offset,
5492 ret = send_clone(sctx, offset, slen,
5497 ret = send_extent_data(sctx, offset + slen,
5500 ret = send_clone(sctx, offset, clone_len,
5504 ret = send_extent_data(sctx, offset, clone_len);
5513 offset += clone_len;
5514 clone_root->offset += clone_len;
5517 * If we are cloning from the file we are currently processing,
5518 * and using the send root as the clone root, we must stop once
5519 * the current clone offset reaches the current eof of the file
5520 * at the receiver, otherwise we would issue an invalid clone
5521 * operation (source range going beyond eof) and cause the
5522 * receiver to fail. So if we reach the current eof, bail out
5523 * and fallback to a regular write.
5525 if (clone_root->root == sctx->send_root &&
5526 clone_root->ino == sctx->cur_ino &&
5527 clone_root->offset >= sctx->cur_inode_next_write_offset)
5530 data_offset += clone_len;
5536 ret = send_extent_data(sctx, offset, len);
5540 btrfs_free_path(path);
5544 static int send_write_or_clone(struct send_ctx *sctx,
5545 struct btrfs_path *path,
5546 struct btrfs_key *key,
5547 struct clone_root *clone_root)
5550 u64 offset = key->offset;
5552 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
5554 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
5558 if (clone_root && IS_ALIGNED(end, bs)) {
5559 struct btrfs_file_extent_item *ei;
5563 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5564 struct btrfs_file_extent_item);
5565 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5566 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5567 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5568 offset, end - offset);
5570 ret = send_extent_data(sctx, offset, end - offset);
5572 sctx->cur_inode_next_write_offset = end;
5576 static int is_extent_unchanged(struct send_ctx *sctx,
5577 struct btrfs_path *left_path,
5578 struct btrfs_key *ekey)
5581 struct btrfs_key key;
5582 struct btrfs_path *path = NULL;
5583 struct extent_buffer *eb;
5585 struct btrfs_key found_key;
5586 struct btrfs_file_extent_item *ei;
5591 u64 left_offset_fixed;
5599 path = alloc_path_for_send();
5603 eb = left_path->nodes[0];
5604 slot = left_path->slots[0];
5605 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5606 left_type = btrfs_file_extent_type(eb, ei);
5608 if (left_type != BTRFS_FILE_EXTENT_REG) {
5612 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5613 left_len = btrfs_file_extent_num_bytes(eb, ei);
5614 left_offset = btrfs_file_extent_offset(eb, ei);
5615 left_gen = btrfs_file_extent_generation(eb, ei);
5618 * Following comments will refer to these graphics. L is the left
5619 * extents which we are checking at the moment. 1-8 are the right
5620 * extents that we iterate.
5623 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5626 * |--1--|-2b-|...(same as above)
5628 * Alternative situation. Happens on files where extents got split.
5630 * |-----------7-----------|-6-|
5632 * Alternative situation. Happens on files which got larger.
5635 * Nothing follows after 8.
5638 key.objectid = ekey->objectid;
5639 key.type = BTRFS_EXTENT_DATA_KEY;
5640 key.offset = ekey->offset;
5641 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5650 * Handle special case where the right side has no extents at all.
5652 eb = path->nodes[0];
5653 slot = path->slots[0];
5654 btrfs_item_key_to_cpu(eb, &found_key, slot);
5655 if (found_key.objectid != key.objectid ||
5656 found_key.type != key.type) {
5657 /* If we're a hole then just pretend nothing changed */
5658 ret = (left_disknr) ? 0 : 1;
5663 * We're now on 2a, 2b or 7.
5666 while (key.offset < ekey->offset + left_len) {
5667 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5668 right_type = btrfs_file_extent_type(eb, ei);
5669 if (right_type != BTRFS_FILE_EXTENT_REG &&
5670 right_type != BTRFS_FILE_EXTENT_INLINE) {
5675 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5676 right_len = btrfs_file_extent_ram_bytes(eb, ei);
5677 right_len = PAGE_ALIGN(right_len);
5679 right_len = btrfs_file_extent_num_bytes(eb, ei);
5683 * Are we at extent 8? If yes, we know the extent is changed.
5684 * This may only happen on the first iteration.
5686 if (found_key.offset + right_len <= ekey->offset) {
5687 /* If we're a hole just pretend nothing changed */
5688 ret = (left_disknr) ? 0 : 1;
5693 * We just wanted to see if when we have an inline extent, what
5694 * follows it is a regular extent (wanted to check the above
5695 * condition for inline extents too). This should normally not
5696 * happen but it's possible for example when we have an inline
5697 * compressed extent representing data with a size matching
5698 * the page size (currently the same as sector size).
5700 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5705 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5706 right_offset = btrfs_file_extent_offset(eb, ei);
5707 right_gen = btrfs_file_extent_generation(eb, ei);
5709 left_offset_fixed = left_offset;
5710 if (key.offset < ekey->offset) {
5711 /* Fix the right offset for 2a and 7. */
5712 right_offset += ekey->offset - key.offset;
5714 /* Fix the left offset for all behind 2a and 2b */
5715 left_offset_fixed += key.offset - ekey->offset;
5719 * Check if we have the same extent.
5721 if (left_disknr != right_disknr ||
5722 left_offset_fixed != right_offset ||
5723 left_gen != right_gen) {
5729 * Go to the next extent.
5731 ret = btrfs_next_item(sctx->parent_root, path);
5735 eb = path->nodes[0];
5736 slot = path->slots[0];
5737 btrfs_item_key_to_cpu(eb, &found_key, slot);
5739 if (ret || found_key.objectid != key.objectid ||
5740 found_key.type != key.type) {
5741 key.offset += right_len;
5744 if (found_key.offset != key.offset + right_len) {
5752 * We're now behind the left extent (treat as unchanged) or at the end
5753 * of the right side (treat as changed).
5755 if (key.offset >= ekey->offset + left_len)
5762 btrfs_free_path(path);
5766 static int get_last_extent(struct send_ctx *sctx, u64 offset)
5768 struct btrfs_path *path;
5769 struct btrfs_root *root = sctx->send_root;
5770 struct btrfs_key key;
5773 path = alloc_path_for_send();
5777 sctx->cur_inode_last_extent = 0;
5779 key.objectid = sctx->cur_ino;
5780 key.type = BTRFS_EXTENT_DATA_KEY;
5781 key.offset = offset;
5782 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5786 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5787 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5790 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
5792 btrfs_free_path(path);
5796 static int range_is_hole_in_parent(struct send_ctx *sctx,
5800 struct btrfs_path *path;
5801 struct btrfs_key key;
5802 struct btrfs_root *root = sctx->parent_root;
5803 u64 search_start = start;
5806 path = alloc_path_for_send();
5810 key.objectid = sctx->cur_ino;
5811 key.type = BTRFS_EXTENT_DATA_KEY;
5812 key.offset = search_start;
5813 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5816 if (ret > 0 && path->slots[0] > 0)
5819 while (search_start < end) {
5820 struct extent_buffer *leaf = path->nodes[0];
5821 int slot = path->slots[0];
5822 struct btrfs_file_extent_item *fi;
5825 if (slot >= btrfs_header_nritems(leaf)) {
5826 ret = btrfs_next_leaf(root, path);
5834 btrfs_item_key_to_cpu(leaf, &key, slot);
5835 if (key.objectid < sctx->cur_ino ||
5836 key.type < BTRFS_EXTENT_DATA_KEY)
5838 if (key.objectid > sctx->cur_ino ||
5839 key.type > BTRFS_EXTENT_DATA_KEY ||
5843 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5844 extent_end = btrfs_file_extent_end(path);
5845 if (extent_end <= start)
5847 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5848 search_start = extent_end;
5858 btrfs_free_path(path);
5862 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5863 struct btrfs_key *key)
5867 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5870 if (sctx->cur_inode_last_extent == (u64)-1) {
5871 ret = get_last_extent(sctx, key->offset - 1);
5876 if (path->slots[0] == 0 &&
5877 sctx->cur_inode_last_extent < key->offset) {
5879 * We might have skipped entire leafs that contained only
5880 * file extent items for our current inode. These leafs have
5881 * a generation number smaller (older) than the one in the
5882 * current leaf and the leaf our last extent came from, and
5883 * are located between these 2 leafs.
5885 ret = get_last_extent(sctx, key->offset - 1);
5890 if (sctx->cur_inode_last_extent < key->offset) {
5891 ret = range_is_hole_in_parent(sctx,
5892 sctx->cur_inode_last_extent,
5897 ret = send_hole(sctx, key->offset);
5901 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
5905 static int process_extent(struct send_ctx *sctx,
5906 struct btrfs_path *path,
5907 struct btrfs_key *key)
5909 struct clone_root *found_clone = NULL;
5912 if (S_ISLNK(sctx->cur_inode_mode))
5915 if (sctx->parent_root && !sctx->cur_inode_new) {
5916 ret = is_extent_unchanged(sctx, path, key);
5924 struct btrfs_file_extent_item *ei;
5927 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5928 struct btrfs_file_extent_item);
5929 type = btrfs_file_extent_type(path->nodes[0], ei);
5930 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5931 type == BTRFS_FILE_EXTENT_REG) {
5933 * The send spec does not have a prealloc command yet,
5934 * so just leave a hole for prealloc'ed extents until
5935 * we have enough commands queued up to justify rev'ing
5938 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5943 /* Have a hole, just skip it. */
5944 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5951 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5952 sctx->cur_inode_size, &found_clone);
5953 if (ret != -ENOENT && ret < 0)
5956 ret = send_write_or_clone(sctx, path, key, found_clone);
5960 ret = maybe_send_hole(sctx, path, key);
5965 static int process_all_extents(struct send_ctx *sctx)
5968 struct btrfs_root *root;
5969 struct btrfs_path *path;
5970 struct btrfs_key key;
5971 struct btrfs_key found_key;
5972 struct extent_buffer *eb;
5975 root = sctx->send_root;
5976 path = alloc_path_for_send();
5980 key.objectid = sctx->cmp_key->objectid;
5981 key.type = BTRFS_EXTENT_DATA_KEY;
5983 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5988 eb = path->nodes[0];
5989 slot = path->slots[0];
5991 if (slot >= btrfs_header_nritems(eb)) {
5992 ret = btrfs_next_leaf(root, path);
5995 } else if (ret > 0) {
6002 btrfs_item_key_to_cpu(eb, &found_key, slot);
6004 if (found_key.objectid != key.objectid ||
6005 found_key.type != key.type) {
6010 ret = process_extent(sctx, path, &found_key);
6018 btrfs_free_path(path);
6022 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
6024 int *refs_processed)
6028 if (sctx->cur_ino == 0)
6030 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
6031 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
6033 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
6036 ret = process_recorded_refs(sctx, pending_move);
6040 *refs_processed = 1;
6045 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
6056 int need_truncate = 1;
6057 int pending_move = 0;
6058 int refs_processed = 0;
6060 if (sctx->ignore_cur_inode)
6063 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6069 * We have processed the refs and thus need to advance send_progress.
6070 * Now, calls to get_cur_xxx will take the updated refs of the current
6071 * inode into account.
6073 * On the other hand, if our current inode is a directory and couldn't
6074 * be moved/renamed because its parent was renamed/moved too and it has
6075 * a higher inode number, we can only move/rename our current inode
6076 * after we moved/renamed its parent. Therefore in this case operate on
6077 * the old path (pre move/rename) of our current inode, and the
6078 * move/rename will be performed later.
6080 if (refs_processed && !pending_move)
6081 sctx->send_progress = sctx->cur_ino + 1;
6083 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6085 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6088 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
6089 &left_mode, &left_uid, &left_gid, NULL);
6093 if (!sctx->parent_root || sctx->cur_inode_new) {
6095 if (!S_ISLNK(sctx->cur_inode_mode))
6097 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6102 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
6103 &old_size, NULL, &right_mode, &right_uid,
6108 if (left_uid != right_uid || left_gid != right_gid)
6110 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6112 if ((old_size == sctx->cur_inode_size) ||
6113 (sctx->cur_inode_size > old_size &&
6114 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6118 if (S_ISREG(sctx->cur_inode_mode)) {
6119 if (need_send_hole(sctx)) {
6120 if (sctx->cur_inode_last_extent == (u64)-1 ||
6121 sctx->cur_inode_last_extent <
6122 sctx->cur_inode_size) {
6123 ret = get_last_extent(sctx, (u64)-1);
6127 if (sctx->cur_inode_last_extent <
6128 sctx->cur_inode_size) {
6129 ret = send_hole(sctx, sctx->cur_inode_size);
6134 if (need_truncate) {
6135 ret = send_truncate(sctx, sctx->cur_ino,
6136 sctx->cur_inode_gen,
6137 sctx->cur_inode_size);
6144 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6145 left_uid, left_gid);
6150 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6156 ret = send_capabilities(sctx);
6161 * If other directory inodes depended on our current directory
6162 * inode's move/rename, now do their move/rename operations.
6164 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6165 ret = apply_children_dir_moves(sctx);
6169 * Need to send that every time, no matter if it actually
6170 * changed between the two trees as we have done changes to
6171 * the inode before. If our inode is a directory and it's
6172 * waiting to be moved/renamed, we will send its utimes when
6173 * it's moved/renamed, therefore we don't need to do it here.
6175 sctx->send_progress = sctx->cur_ino + 1;
6176 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6185 struct parent_paths_ctx {
6186 struct list_head *refs;
6187 struct send_ctx *sctx;
6190 static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6193 struct parent_paths_ctx *ppctx = ctx;
6195 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6200 * Issue unlink operations for all paths of the current inode found in the
6203 static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6205 LIST_HEAD(deleted_refs);
6206 struct btrfs_path *path;
6207 struct btrfs_key key;
6208 struct parent_paths_ctx ctx;
6211 path = alloc_path_for_send();
6215 key.objectid = sctx->cur_ino;
6216 key.type = BTRFS_INODE_REF_KEY;
6218 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
6222 ctx.refs = &deleted_refs;
6226 struct extent_buffer *eb = path->nodes[0];
6227 int slot = path->slots[0];
6229 if (slot >= btrfs_header_nritems(eb)) {
6230 ret = btrfs_next_leaf(sctx->parent_root, path);
6238 btrfs_item_key_to_cpu(eb, &key, slot);
6239 if (key.objectid != sctx->cur_ino)
6241 if (key.type != BTRFS_INODE_REF_KEY &&
6242 key.type != BTRFS_INODE_EXTREF_KEY)
6245 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
6246 record_parent_ref, &ctx);
6253 while (!list_empty(&deleted_refs)) {
6254 struct recorded_ref *ref;
6256 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6257 ret = send_unlink(sctx, ref->full_path);
6260 fs_path_free(ref->full_path);
6261 list_del(&ref->list);
6266 btrfs_free_path(path);
6268 __free_recorded_refs(&deleted_refs);
6272 static int changed_inode(struct send_ctx *sctx,
6273 enum btrfs_compare_tree_result result)
6276 struct btrfs_key *key = sctx->cmp_key;
6277 struct btrfs_inode_item *left_ii = NULL;
6278 struct btrfs_inode_item *right_ii = NULL;
6282 sctx->cur_ino = key->objectid;
6283 sctx->cur_inode_new_gen = 0;
6284 sctx->cur_inode_last_extent = (u64)-1;
6285 sctx->cur_inode_next_write_offset = 0;
6286 sctx->ignore_cur_inode = false;
6289 * Set send_progress to current inode. This will tell all get_cur_xxx
6290 * functions that the current inode's refs are not updated yet. Later,
6291 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6293 sctx->send_progress = sctx->cur_ino;
6295 if (result == BTRFS_COMPARE_TREE_NEW ||
6296 result == BTRFS_COMPARE_TREE_CHANGED) {
6297 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6298 sctx->left_path->slots[0],
6299 struct btrfs_inode_item);
6300 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6303 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6304 sctx->right_path->slots[0],
6305 struct btrfs_inode_item);
6306 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6309 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6310 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6311 sctx->right_path->slots[0],
6312 struct btrfs_inode_item);
6314 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6318 * The cur_ino = root dir case is special here. We can't treat
6319 * the inode as deleted+reused because it would generate a
6320 * stream that tries to delete/mkdir the root dir.
6322 if (left_gen != right_gen &&
6323 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6324 sctx->cur_inode_new_gen = 1;
6328 * Normally we do not find inodes with a link count of zero (orphans)
6329 * because the most common case is to create a snapshot and use it
6330 * for a send operation. However other less common use cases involve
6331 * using a subvolume and send it after turning it to RO mode just
6332 * after deleting all hard links of a file while holding an open
6333 * file descriptor against it or turning a RO snapshot into RW mode,
6334 * keep an open file descriptor against a file, delete it and then
6335 * turn the snapshot back to RO mode before using it for a send
6336 * operation. So if we find such cases, ignore the inode and all its
6337 * items completely if it's a new inode, or if it's a changed inode
6338 * make sure all its previous paths (from the parent snapshot) are all
6339 * unlinked and all other the inode items are ignored.
6341 if (result == BTRFS_COMPARE_TREE_NEW ||
6342 result == BTRFS_COMPARE_TREE_CHANGED) {
6345 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6347 sctx->ignore_cur_inode = true;
6348 if (result == BTRFS_COMPARE_TREE_CHANGED)
6349 ret = btrfs_unlink_all_paths(sctx);
6354 if (result == BTRFS_COMPARE_TREE_NEW) {
6355 sctx->cur_inode_gen = left_gen;
6356 sctx->cur_inode_new = 1;
6357 sctx->cur_inode_deleted = 0;
6358 sctx->cur_inode_size = btrfs_inode_size(
6359 sctx->left_path->nodes[0], left_ii);
6360 sctx->cur_inode_mode = btrfs_inode_mode(
6361 sctx->left_path->nodes[0], left_ii);
6362 sctx->cur_inode_rdev = btrfs_inode_rdev(
6363 sctx->left_path->nodes[0], left_ii);
6364 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6365 ret = send_create_inode_if_needed(sctx);
6366 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6367 sctx->cur_inode_gen = right_gen;
6368 sctx->cur_inode_new = 0;
6369 sctx->cur_inode_deleted = 1;
6370 sctx->cur_inode_size = btrfs_inode_size(
6371 sctx->right_path->nodes[0], right_ii);
6372 sctx->cur_inode_mode = btrfs_inode_mode(
6373 sctx->right_path->nodes[0], right_ii);
6374 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
6376 * We need to do some special handling in case the inode was
6377 * reported as changed with a changed generation number. This
6378 * means that the original inode was deleted and new inode
6379 * reused the same inum. So we have to treat the old inode as
6380 * deleted and the new one as new.
6382 if (sctx->cur_inode_new_gen) {
6384 * First, process the inode as if it was deleted.
6386 sctx->cur_inode_gen = right_gen;
6387 sctx->cur_inode_new = 0;
6388 sctx->cur_inode_deleted = 1;
6389 sctx->cur_inode_size = btrfs_inode_size(
6390 sctx->right_path->nodes[0], right_ii);
6391 sctx->cur_inode_mode = btrfs_inode_mode(
6392 sctx->right_path->nodes[0], right_ii);
6393 ret = process_all_refs(sctx,
6394 BTRFS_COMPARE_TREE_DELETED);
6399 * Now process the inode as if it was new.
6401 sctx->cur_inode_gen = left_gen;
6402 sctx->cur_inode_new = 1;
6403 sctx->cur_inode_deleted = 0;
6404 sctx->cur_inode_size = btrfs_inode_size(
6405 sctx->left_path->nodes[0], left_ii);
6406 sctx->cur_inode_mode = btrfs_inode_mode(
6407 sctx->left_path->nodes[0], left_ii);
6408 sctx->cur_inode_rdev = btrfs_inode_rdev(
6409 sctx->left_path->nodes[0], left_ii);
6410 ret = send_create_inode_if_needed(sctx);
6414 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6418 * Advance send_progress now as we did not get into
6419 * process_recorded_refs_if_needed in the new_gen case.
6421 sctx->send_progress = sctx->cur_ino + 1;
6424 * Now process all extents and xattrs of the inode as if
6425 * they were all new.
6427 ret = process_all_extents(sctx);
6430 ret = process_all_new_xattrs(sctx);
6434 sctx->cur_inode_gen = left_gen;
6435 sctx->cur_inode_new = 0;
6436 sctx->cur_inode_new_gen = 0;
6437 sctx->cur_inode_deleted = 0;
6438 sctx->cur_inode_size = btrfs_inode_size(
6439 sctx->left_path->nodes[0], left_ii);
6440 sctx->cur_inode_mode = btrfs_inode_mode(
6441 sctx->left_path->nodes[0], left_ii);
6450 * We have to process new refs before deleted refs, but compare_trees gives us
6451 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6452 * first and later process them in process_recorded_refs.
6453 * For the cur_inode_new_gen case, we skip recording completely because
6454 * changed_inode did already initiate processing of refs. The reason for this is
6455 * that in this case, compare_tree actually compares the refs of 2 different
6456 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6457 * refs of the right tree as deleted and all refs of the left tree as new.
6459 static int changed_ref(struct send_ctx *sctx,
6460 enum btrfs_compare_tree_result result)
6464 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6465 inconsistent_snapshot_error(sctx, result, "reference");
6469 if (!sctx->cur_inode_new_gen &&
6470 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6471 if (result == BTRFS_COMPARE_TREE_NEW)
6472 ret = record_new_ref(sctx);
6473 else if (result == BTRFS_COMPARE_TREE_DELETED)
6474 ret = record_deleted_ref(sctx);
6475 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6476 ret = record_changed_ref(sctx);
6483 * Process new/deleted/changed xattrs. We skip processing in the
6484 * cur_inode_new_gen case because changed_inode did already initiate processing
6485 * of xattrs. The reason is the same as in changed_ref
6487 static int changed_xattr(struct send_ctx *sctx,
6488 enum btrfs_compare_tree_result result)
6492 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6493 inconsistent_snapshot_error(sctx, result, "xattr");
6497 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6498 if (result == BTRFS_COMPARE_TREE_NEW)
6499 ret = process_new_xattr(sctx);
6500 else if (result == BTRFS_COMPARE_TREE_DELETED)
6501 ret = process_deleted_xattr(sctx);
6502 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6503 ret = process_changed_xattr(sctx);
6510 * Process new/deleted/changed extents. We skip processing in the
6511 * cur_inode_new_gen case because changed_inode did already initiate processing
6512 * of extents. The reason is the same as in changed_ref
6514 static int changed_extent(struct send_ctx *sctx,
6515 enum btrfs_compare_tree_result result)
6520 * We have found an extent item that changed without the inode item
6521 * having changed. This can happen either after relocation (where the
6522 * disk_bytenr of an extent item is replaced at
6523 * relocation.c:replace_file_extents()) or after deduplication into a
6524 * file in both the parent and send snapshots (where an extent item can
6525 * get modified or replaced with a new one). Note that deduplication
6526 * updates the inode item, but it only changes the iversion (sequence
6527 * field in the inode item) of the inode, so if a file is deduplicated
6528 * the same amount of times in both the parent and send snapshots, its
6529 * iversion becames the same in both snapshots, whence the inode item is
6530 * the same on both snapshots.
6532 if (sctx->cur_ino != sctx->cmp_key->objectid)
6535 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6536 if (result != BTRFS_COMPARE_TREE_DELETED)
6537 ret = process_extent(sctx, sctx->left_path,
6544 static int dir_changed(struct send_ctx *sctx, u64 dir)
6546 u64 orig_gen, new_gen;
6549 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6554 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6559 return (orig_gen != new_gen) ? 1 : 0;
6562 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6563 struct btrfs_key *key)
6565 struct btrfs_inode_extref *extref;
6566 struct extent_buffer *leaf;
6567 u64 dirid = 0, last_dirid = 0;
6574 /* Easy case, just check this one dirid */
6575 if (key->type == BTRFS_INODE_REF_KEY) {
6576 dirid = key->offset;
6578 ret = dir_changed(sctx, dirid);
6582 leaf = path->nodes[0];
6583 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6584 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6585 while (cur_offset < item_size) {
6586 extref = (struct btrfs_inode_extref *)(ptr +
6588 dirid = btrfs_inode_extref_parent(leaf, extref);
6589 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6590 cur_offset += ref_name_len + sizeof(*extref);
6591 if (dirid == last_dirid)
6593 ret = dir_changed(sctx, dirid);
6603 * Updates compare related fields in sctx and simply forwards to the actual
6604 * changed_xxx functions.
6606 static int changed_cb(struct btrfs_path *left_path,
6607 struct btrfs_path *right_path,
6608 struct btrfs_key *key,
6609 enum btrfs_compare_tree_result result,
6613 struct send_ctx *sctx = ctx;
6615 if (result == BTRFS_COMPARE_TREE_SAME) {
6616 if (key->type == BTRFS_INODE_REF_KEY ||
6617 key->type == BTRFS_INODE_EXTREF_KEY) {
6618 ret = compare_refs(sctx, left_path, key);
6623 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6624 return maybe_send_hole(sctx, left_path, key);
6628 result = BTRFS_COMPARE_TREE_CHANGED;
6632 sctx->left_path = left_path;
6633 sctx->right_path = right_path;
6634 sctx->cmp_key = key;
6636 ret = finish_inode_if_needed(sctx, 0);
6640 /* Ignore non-FS objects */
6641 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6642 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6645 if (key->type == BTRFS_INODE_ITEM_KEY) {
6646 ret = changed_inode(sctx, result);
6647 } else if (!sctx->ignore_cur_inode) {
6648 if (key->type == BTRFS_INODE_REF_KEY ||
6649 key->type == BTRFS_INODE_EXTREF_KEY)
6650 ret = changed_ref(sctx, result);
6651 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6652 ret = changed_xattr(sctx, result);
6653 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6654 ret = changed_extent(sctx, result);
6661 static int full_send_tree(struct send_ctx *sctx)
6664 struct btrfs_root *send_root = sctx->send_root;
6665 struct btrfs_key key;
6666 struct btrfs_path *path;
6667 struct extent_buffer *eb;
6670 path = alloc_path_for_send();
6674 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6675 key.type = BTRFS_INODE_ITEM_KEY;
6678 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6685 eb = path->nodes[0];
6686 slot = path->slots[0];
6687 btrfs_item_key_to_cpu(eb, &key, slot);
6689 ret = changed_cb(path, NULL, &key,
6690 BTRFS_COMPARE_TREE_NEW, sctx);
6694 ret = btrfs_next_item(send_root, path);
6704 ret = finish_inode_if_needed(sctx, 1);
6707 btrfs_free_path(path);
6711 static int tree_move_down(struct btrfs_path *path, int *level)
6713 struct extent_buffer *eb;
6715 BUG_ON(*level == 0);
6716 eb = btrfs_read_node_slot(path->nodes[*level], path->slots[*level]);
6720 path->nodes[*level - 1] = eb;
6721 path->slots[*level - 1] = 0;
6726 static int tree_move_next_or_upnext(struct btrfs_path *path,
6727 int *level, int root_level)
6731 nritems = btrfs_header_nritems(path->nodes[*level]);
6733 path->slots[*level]++;
6735 while (path->slots[*level] >= nritems) {
6736 if (*level == root_level)
6740 path->slots[*level] = 0;
6741 free_extent_buffer(path->nodes[*level]);
6742 path->nodes[*level] = NULL;
6744 path->slots[*level]++;
6746 nritems = btrfs_header_nritems(path->nodes[*level]);
6753 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
6756 static int tree_advance(struct btrfs_path *path,
6757 int *level, int root_level,
6759 struct btrfs_key *key)
6763 if (*level == 0 || !allow_down) {
6764 ret = tree_move_next_or_upnext(path, level, root_level);
6766 ret = tree_move_down(path, level);
6770 btrfs_item_key_to_cpu(path->nodes[*level], key,
6771 path->slots[*level]);
6773 btrfs_node_key_to_cpu(path->nodes[*level], key,
6774 path->slots[*level]);
6779 static int tree_compare_item(struct btrfs_path *left_path,
6780 struct btrfs_path *right_path,
6785 unsigned long off1, off2;
6787 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
6788 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
6792 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
6793 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
6794 right_path->slots[0]);
6796 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
6798 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
6805 * This function compares two trees and calls the provided callback for
6806 * every changed/new/deleted item it finds.
6807 * If shared tree blocks are encountered, whole subtrees are skipped, making
6808 * the compare pretty fast on snapshotted subvolumes.
6810 * This currently works on commit roots only. As commit roots are read only,
6811 * we don't do any locking. The commit roots are protected with transactions.
6812 * Transactions are ended and rejoined when a commit is tried in between.
6814 * This function checks for modifications done to the trees while comparing.
6815 * If it detects a change, it aborts immediately.
6817 static int btrfs_compare_trees(struct btrfs_root *left_root,
6818 struct btrfs_root *right_root, void *ctx)
6820 struct btrfs_fs_info *fs_info = left_root->fs_info;
6823 struct btrfs_path *left_path = NULL;
6824 struct btrfs_path *right_path = NULL;
6825 struct btrfs_key left_key;
6826 struct btrfs_key right_key;
6827 char *tmp_buf = NULL;
6828 int left_root_level;
6829 int right_root_level;
6832 int left_end_reached;
6833 int right_end_reached;
6841 left_path = btrfs_alloc_path();
6846 right_path = btrfs_alloc_path();
6852 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
6858 left_path->search_commit_root = 1;
6859 left_path->skip_locking = 1;
6860 right_path->search_commit_root = 1;
6861 right_path->skip_locking = 1;
6864 * Strategy: Go to the first items of both trees. Then do
6866 * If both trees are at level 0
6867 * Compare keys of current items
6868 * If left < right treat left item as new, advance left tree
6870 * If left > right treat right item as deleted, advance right tree
6872 * If left == right do deep compare of items, treat as changed if
6873 * needed, advance both trees and repeat
6874 * If both trees are at the same level but not at level 0
6875 * Compare keys of current nodes/leafs
6876 * If left < right advance left tree and repeat
6877 * If left > right advance right tree and repeat
6878 * If left == right compare blockptrs of the next nodes/leafs
6879 * If they match advance both trees but stay at the same level
6881 * If they don't match advance both trees while allowing to go
6883 * If tree levels are different
6884 * Advance the tree that needs it and repeat
6886 * Advancing a tree means:
6887 * If we are at level 0, try to go to the next slot. If that's not
6888 * possible, go one level up and repeat. Stop when we found a level
6889 * where we could go to the next slot. We may at this point be on a
6892 * If we are not at level 0 and not on shared tree blocks, go one
6895 * If we are not at level 0 and on shared tree blocks, go one slot to
6896 * the right if possible or go up and right.
6899 down_read(&fs_info->commit_root_sem);
6900 left_level = btrfs_header_level(left_root->commit_root);
6901 left_root_level = left_level;
6902 left_path->nodes[left_level] =
6903 btrfs_clone_extent_buffer(left_root->commit_root);
6904 if (!left_path->nodes[left_level]) {
6905 up_read(&fs_info->commit_root_sem);
6910 right_level = btrfs_header_level(right_root->commit_root);
6911 right_root_level = right_level;
6912 right_path->nodes[right_level] =
6913 btrfs_clone_extent_buffer(right_root->commit_root);
6914 if (!right_path->nodes[right_level]) {
6915 up_read(&fs_info->commit_root_sem);
6919 up_read(&fs_info->commit_root_sem);
6921 if (left_level == 0)
6922 btrfs_item_key_to_cpu(left_path->nodes[left_level],
6923 &left_key, left_path->slots[left_level]);
6925 btrfs_node_key_to_cpu(left_path->nodes[left_level],
6926 &left_key, left_path->slots[left_level]);
6927 if (right_level == 0)
6928 btrfs_item_key_to_cpu(right_path->nodes[right_level],
6929 &right_key, right_path->slots[right_level]);
6931 btrfs_node_key_to_cpu(right_path->nodes[right_level],
6932 &right_key, right_path->slots[right_level]);
6934 left_end_reached = right_end_reached = 0;
6935 advance_left = advance_right = 0;
6939 if (advance_left && !left_end_reached) {
6940 ret = tree_advance(left_path, &left_level,
6942 advance_left != ADVANCE_ONLY_NEXT,
6945 left_end_reached = ADVANCE;
6950 if (advance_right && !right_end_reached) {
6951 ret = tree_advance(right_path, &right_level,
6953 advance_right != ADVANCE_ONLY_NEXT,
6956 right_end_reached = ADVANCE;
6962 if (left_end_reached && right_end_reached) {
6965 } else if (left_end_reached) {
6966 if (right_level == 0) {
6967 ret = changed_cb(left_path, right_path,
6969 BTRFS_COMPARE_TREE_DELETED,
6974 advance_right = ADVANCE;
6976 } else if (right_end_reached) {
6977 if (left_level == 0) {
6978 ret = changed_cb(left_path, right_path,
6980 BTRFS_COMPARE_TREE_NEW,
6985 advance_left = ADVANCE;
6989 if (left_level == 0 && right_level == 0) {
6990 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
6992 ret = changed_cb(left_path, right_path,
6994 BTRFS_COMPARE_TREE_NEW,
6998 advance_left = ADVANCE;
6999 } else if (cmp > 0) {
7000 ret = changed_cb(left_path, right_path,
7002 BTRFS_COMPARE_TREE_DELETED,
7006 advance_right = ADVANCE;
7008 enum btrfs_compare_tree_result result;
7010 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
7011 ret = tree_compare_item(left_path, right_path,
7014 result = BTRFS_COMPARE_TREE_CHANGED;
7016 result = BTRFS_COMPARE_TREE_SAME;
7017 ret = changed_cb(left_path, right_path,
7018 &left_key, result, ctx);
7021 advance_left = ADVANCE;
7022 advance_right = ADVANCE;
7024 } else if (left_level == right_level) {
7025 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7027 advance_left = ADVANCE;
7028 } else if (cmp > 0) {
7029 advance_right = ADVANCE;
7031 left_blockptr = btrfs_node_blockptr(
7032 left_path->nodes[left_level],
7033 left_path->slots[left_level]);
7034 right_blockptr = btrfs_node_blockptr(
7035 right_path->nodes[right_level],
7036 right_path->slots[right_level]);
7037 left_gen = btrfs_node_ptr_generation(
7038 left_path->nodes[left_level],
7039 left_path->slots[left_level]);
7040 right_gen = btrfs_node_ptr_generation(
7041 right_path->nodes[right_level],
7042 right_path->slots[right_level]);
7043 if (left_blockptr == right_blockptr &&
7044 left_gen == right_gen) {
7046 * As we're on a shared block, don't
7047 * allow to go deeper.
7049 advance_left = ADVANCE_ONLY_NEXT;
7050 advance_right = ADVANCE_ONLY_NEXT;
7052 advance_left = ADVANCE;
7053 advance_right = ADVANCE;
7056 } else if (left_level < right_level) {
7057 advance_right = ADVANCE;
7059 advance_left = ADVANCE;
7064 btrfs_free_path(left_path);
7065 btrfs_free_path(right_path);
7070 static int send_subvol(struct send_ctx *sctx)
7074 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
7075 ret = send_header(sctx);
7080 ret = send_subvol_begin(sctx);
7084 if (sctx->parent_root) {
7085 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
7088 ret = finish_inode_if_needed(sctx, 1);
7092 ret = full_send_tree(sctx);
7098 free_recorded_refs(sctx);
7103 * If orphan cleanup did remove any orphans from a root, it means the tree
7104 * was modified and therefore the commit root is not the same as the current
7105 * root anymore. This is a problem, because send uses the commit root and
7106 * therefore can see inode items that don't exist in the current root anymore,
7107 * and for example make calls to btrfs_iget, which will do tree lookups based
7108 * on the current root and not on the commit root. Those lookups will fail,
7109 * returning a -ESTALE error, and making send fail with that error. So make
7110 * sure a send does not see any orphans we have just removed, and that it will
7111 * see the same inodes regardless of whether a transaction commit happened
7112 * before it started (meaning that the commit root will be the same as the
7113 * current root) or not.
7115 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7118 struct btrfs_trans_handle *trans = NULL;
7121 if (sctx->parent_root &&
7122 sctx->parent_root->node != sctx->parent_root->commit_root)
7125 for (i = 0; i < sctx->clone_roots_cnt; i++)
7126 if (sctx->clone_roots[i].root->node !=
7127 sctx->clone_roots[i].root->commit_root)
7131 return btrfs_end_transaction(trans);
7136 /* Use any root, all fs roots will get their commit roots updated. */
7138 trans = btrfs_join_transaction(sctx->send_root);
7140 return PTR_ERR(trans);
7144 return btrfs_commit_transaction(trans);
7148 * Make sure any existing dellaloc is flushed for any root used by a send
7149 * operation so that we do not miss any data and we do not race with writeback
7150 * finishing and changing a tree while send is using the tree. This could
7151 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
7152 * a send operation then uses the subvolume.
7153 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
7155 static int flush_delalloc_roots(struct send_ctx *sctx)
7157 struct btrfs_root *root = sctx->parent_root;
7162 ret = btrfs_start_delalloc_snapshot(root);
7165 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7168 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7169 root = sctx->clone_roots[i].root;
7170 ret = btrfs_start_delalloc_snapshot(root);
7173 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7179 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7181 spin_lock(&root->root_item_lock);
7182 root->send_in_progress--;
7184 * Not much left to do, we don't know why it's unbalanced and
7185 * can't blindly reset it to 0.
7187 if (root->send_in_progress < 0)
7188 btrfs_err(root->fs_info,
7189 "send_in_progress unbalanced %d root %llu",
7190 root->send_in_progress, root->root_key.objectid);
7191 spin_unlock(&root->root_item_lock);
7194 static void dedupe_in_progress_warn(const struct btrfs_root *root)
7196 btrfs_warn_rl(root->fs_info,
7197 "cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7198 root->root_key.objectid, root->dedupe_in_progress);
7201 long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
7204 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
7205 struct btrfs_fs_info *fs_info = send_root->fs_info;
7206 struct btrfs_root *clone_root;
7207 struct send_ctx *sctx = NULL;
7209 u64 *clone_sources_tmp = NULL;
7210 int clone_sources_to_rollback = 0;
7212 int sort_clone_roots = 0;
7214 if (!capable(CAP_SYS_ADMIN))
7218 * The subvolume must remain read-only during send, protect against
7219 * making it RW. This also protects against deletion.
7221 spin_lock(&send_root->root_item_lock);
7222 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7223 dedupe_in_progress_warn(send_root);
7224 spin_unlock(&send_root->root_item_lock);
7227 send_root->send_in_progress++;
7228 spin_unlock(&send_root->root_item_lock);
7231 * Userspace tools do the checks and warn the user if it's
7234 if (!btrfs_root_readonly(send_root)) {
7240 * Check that we don't overflow at later allocations, we request
7241 * clone_sources_count + 1 items, and compare to unsigned long inside
7244 if (arg->clone_sources_count >
7245 ULONG_MAX / sizeof(struct clone_root) - 1) {
7250 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
7255 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
7261 INIT_LIST_HEAD(&sctx->new_refs);
7262 INIT_LIST_HEAD(&sctx->deleted_refs);
7263 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
7264 INIT_LIST_HEAD(&sctx->name_cache_list);
7266 sctx->flags = arg->flags;
7268 sctx->send_filp = fget(arg->send_fd);
7269 if (!sctx->send_filp) {
7274 sctx->send_root = send_root;
7276 * Unlikely but possible, if the subvolume is marked for deletion but
7277 * is slow to remove the directory entry, send can still be started
7279 if (btrfs_root_dead(sctx->send_root)) {
7284 sctx->clone_roots_cnt = arg->clone_sources_count;
7286 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
7287 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
7288 if (!sctx->send_buf) {
7293 sctx->pending_dir_moves = RB_ROOT;
7294 sctx->waiting_dir_moves = RB_ROOT;
7295 sctx->orphan_dirs = RB_ROOT;
7297 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
7298 arg->clone_sources_count + 1,
7300 if (!sctx->clone_roots) {
7305 alloc_size = array_size(sizeof(*arg->clone_sources),
7306 arg->clone_sources_count);
7308 if (arg->clone_sources_count) {
7309 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
7310 if (!clone_sources_tmp) {
7315 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
7322 for (i = 0; i < arg->clone_sources_count; i++) {
7323 clone_root = btrfs_get_fs_root(fs_info,
7324 clone_sources_tmp[i], true);
7325 if (IS_ERR(clone_root)) {
7326 ret = PTR_ERR(clone_root);
7329 spin_lock(&clone_root->root_item_lock);
7330 if (!btrfs_root_readonly(clone_root) ||
7331 btrfs_root_dead(clone_root)) {
7332 spin_unlock(&clone_root->root_item_lock);
7333 btrfs_put_root(clone_root);
7337 if (clone_root->dedupe_in_progress) {
7338 dedupe_in_progress_warn(clone_root);
7339 spin_unlock(&clone_root->root_item_lock);
7340 btrfs_put_root(clone_root);
7344 clone_root->send_in_progress++;
7345 spin_unlock(&clone_root->root_item_lock);
7347 sctx->clone_roots[i].root = clone_root;
7348 clone_sources_to_rollback = i + 1;
7350 kvfree(clone_sources_tmp);
7351 clone_sources_tmp = NULL;
7354 if (arg->parent_root) {
7355 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7357 if (IS_ERR(sctx->parent_root)) {
7358 ret = PTR_ERR(sctx->parent_root);
7362 spin_lock(&sctx->parent_root->root_item_lock);
7363 sctx->parent_root->send_in_progress++;
7364 if (!btrfs_root_readonly(sctx->parent_root) ||
7365 btrfs_root_dead(sctx->parent_root)) {
7366 spin_unlock(&sctx->parent_root->root_item_lock);
7370 if (sctx->parent_root->dedupe_in_progress) {
7371 dedupe_in_progress_warn(sctx->parent_root);
7372 spin_unlock(&sctx->parent_root->root_item_lock);
7376 spin_unlock(&sctx->parent_root->root_item_lock);
7380 * Clones from send_root are allowed, but only if the clone source
7381 * is behind the current send position. This is checked while searching
7382 * for possible clone sources.
7384 sctx->clone_roots[sctx->clone_roots_cnt++].root =
7385 btrfs_grab_root(sctx->send_root);
7387 /* We do a bsearch later */
7388 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7389 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7391 sort_clone_roots = 1;
7393 ret = flush_delalloc_roots(sctx);
7397 ret = ensure_commit_roots_uptodate(sctx);
7401 mutex_lock(&fs_info->balance_mutex);
7402 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
7403 mutex_unlock(&fs_info->balance_mutex);
7404 btrfs_warn_rl(fs_info,
7405 "cannot run send because a balance operation is in progress");
7409 fs_info->send_in_progress++;
7410 mutex_unlock(&fs_info->balance_mutex);
7412 current->journal_info = BTRFS_SEND_TRANS_STUB;
7413 ret = send_subvol(sctx);
7414 current->journal_info = NULL;
7415 mutex_lock(&fs_info->balance_mutex);
7416 fs_info->send_in_progress--;
7417 mutex_unlock(&fs_info->balance_mutex);
7421 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7422 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7425 ret = send_cmd(sctx);
7431 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7432 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7434 struct pending_dir_move *pm;
7436 n = rb_first(&sctx->pending_dir_moves);
7437 pm = rb_entry(n, struct pending_dir_move, node);
7438 while (!list_empty(&pm->list)) {
7439 struct pending_dir_move *pm2;
7441 pm2 = list_first_entry(&pm->list,
7442 struct pending_dir_move, list);
7443 free_pending_move(sctx, pm2);
7445 free_pending_move(sctx, pm);
7448 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7449 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7451 struct waiting_dir_move *dm;
7453 n = rb_first(&sctx->waiting_dir_moves);
7454 dm = rb_entry(n, struct waiting_dir_move, node);
7455 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7459 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7460 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7462 struct orphan_dir_info *odi;
7464 n = rb_first(&sctx->orphan_dirs);
7465 odi = rb_entry(n, struct orphan_dir_info, node);
7466 free_orphan_dir_info(sctx, odi);
7469 if (sort_clone_roots) {
7470 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7471 btrfs_root_dec_send_in_progress(
7472 sctx->clone_roots[i].root);
7473 btrfs_put_root(sctx->clone_roots[i].root);
7476 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
7477 btrfs_root_dec_send_in_progress(
7478 sctx->clone_roots[i].root);
7479 btrfs_put_root(sctx->clone_roots[i].root);
7482 btrfs_root_dec_send_in_progress(send_root);
7484 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
7485 btrfs_root_dec_send_in_progress(sctx->parent_root);
7486 btrfs_put_root(sctx->parent_root);
7489 kvfree(clone_sources_tmp);
7492 if (sctx->send_filp)
7493 fput(sctx->send_filp);
7495 kvfree(sctx->clone_roots);
7496 kvfree(sctx->send_buf);
7498 name_cache_free(sctx);