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.
242 struct orphan_dir_info {
246 u64 last_dir_index_offset;
249 struct name_cache_entry {
250 struct list_head list;
252 * radix_tree has only 32bit entries but we need to handle 64bit inums.
253 * We use the lower 32bit of the 64bit inum to store it in the tree. If
254 * more then one inum would fall into the same entry, we use radix_list
255 * to store the additional entries. radix_list is also used to store
256 * entries where two entries have the same inum but different
259 struct list_head radix_list;
265 int need_later_update;
271 #define ADVANCE_ONLY_NEXT -1
273 enum btrfs_compare_tree_result {
274 BTRFS_COMPARE_TREE_NEW,
275 BTRFS_COMPARE_TREE_DELETED,
276 BTRFS_COMPARE_TREE_CHANGED,
277 BTRFS_COMPARE_TREE_SAME,
281 static void inconsistent_snapshot_error(struct send_ctx *sctx,
282 enum btrfs_compare_tree_result result,
285 const char *result_string;
288 case BTRFS_COMPARE_TREE_NEW:
289 result_string = "new";
291 case BTRFS_COMPARE_TREE_DELETED:
292 result_string = "deleted";
294 case BTRFS_COMPARE_TREE_CHANGED:
295 result_string = "updated";
297 case BTRFS_COMPARE_TREE_SAME:
299 result_string = "unchanged";
303 result_string = "unexpected";
306 btrfs_err(sctx->send_root->fs_info,
307 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
308 result_string, what, sctx->cmp_key->objectid,
309 sctx->send_root->root_key.objectid,
311 sctx->parent_root->root_key.objectid : 0));
314 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
316 static struct waiting_dir_move *
317 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
319 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
321 static int need_send_hole(struct send_ctx *sctx)
323 return (sctx->parent_root && !sctx->cur_inode_new &&
324 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
325 S_ISREG(sctx->cur_inode_mode));
328 static void fs_path_reset(struct fs_path *p)
331 p->start = p->buf + p->buf_len - 1;
341 static struct fs_path *fs_path_alloc(void)
345 p = kmalloc(sizeof(*p), GFP_KERNEL);
349 p->buf = p->inline_buf;
350 p->buf_len = FS_PATH_INLINE_SIZE;
355 static struct fs_path *fs_path_alloc_reversed(void)
367 static void fs_path_free(struct fs_path *p)
371 if (p->buf != p->inline_buf)
376 static int fs_path_len(struct fs_path *p)
378 return p->end - p->start;
381 static int fs_path_ensure_buf(struct fs_path *p, int len)
389 if (p->buf_len >= len)
392 if (len > PATH_MAX) {
397 path_len = p->end - p->start;
398 old_buf_len = p->buf_len;
401 * First time the inline_buf does not suffice
403 if (p->buf == p->inline_buf) {
404 tmp_buf = kmalloc(len, GFP_KERNEL);
406 memcpy(tmp_buf, p->buf, old_buf_len);
408 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
414 * The real size of the buffer is bigger, this will let the fast path
415 * happen most of the time
417 p->buf_len = ksize(p->buf);
420 tmp_buf = p->buf + old_buf_len - path_len - 1;
421 p->end = p->buf + p->buf_len - 1;
422 p->start = p->end - path_len;
423 memmove(p->start, tmp_buf, path_len + 1);
426 p->end = p->start + path_len;
431 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
437 new_len = p->end - p->start + name_len;
438 if (p->start != p->end)
440 ret = fs_path_ensure_buf(p, new_len);
445 if (p->start != p->end)
447 p->start -= name_len;
448 *prepared = p->start;
450 if (p->start != p->end)
461 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
466 ret = fs_path_prepare_for_add(p, name_len, &prepared);
469 memcpy(prepared, name, name_len);
475 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
480 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
483 memcpy(prepared, p2->start, p2->end - p2->start);
489 static int fs_path_add_from_extent_buffer(struct fs_path *p,
490 struct extent_buffer *eb,
491 unsigned long off, int len)
496 ret = fs_path_prepare_for_add(p, len, &prepared);
500 read_extent_buffer(eb, prepared, off, len);
506 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
510 p->reversed = from->reversed;
513 ret = fs_path_add_path(p, from);
519 static void fs_path_unreverse(struct fs_path *p)
528 len = p->end - p->start;
530 p->end = p->start + len;
531 memmove(p->start, tmp, len + 1);
535 static struct btrfs_path *alloc_path_for_send(void)
537 struct btrfs_path *path;
539 path = btrfs_alloc_path();
542 path->search_commit_root = 1;
543 path->skip_locking = 1;
544 path->need_commit_sem = 1;
548 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
554 ret = kernel_write(filp, buf + pos, len - pos, off);
555 /* TODO handle that correctly */
556 /*if (ret == -ERESTARTSYS) {
570 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
572 struct btrfs_tlv_header *hdr;
573 int total_len = sizeof(*hdr) + len;
574 int left = sctx->send_max_size - sctx->send_size;
576 if (unlikely(left < total_len))
579 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
580 hdr->tlv_type = cpu_to_le16(attr);
581 hdr->tlv_len = cpu_to_le16(len);
582 memcpy(hdr + 1, data, len);
583 sctx->send_size += total_len;
588 #define TLV_PUT_DEFINE_INT(bits) \
589 static int tlv_put_u##bits(struct send_ctx *sctx, \
590 u##bits attr, u##bits value) \
592 __le##bits __tmp = cpu_to_le##bits(value); \
593 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
596 TLV_PUT_DEFINE_INT(64)
598 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
599 const char *str, int len)
603 return tlv_put(sctx, attr, str, len);
606 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
609 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
612 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
613 struct extent_buffer *eb,
614 struct btrfs_timespec *ts)
616 struct btrfs_timespec bts;
617 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
618 return tlv_put(sctx, attr, &bts, sizeof(bts));
622 #define TLV_PUT(sctx, attrtype, data, attrlen) \
624 ret = tlv_put(sctx, attrtype, data, attrlen); \
626 goto tlv_put_failure; \
629 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
631 ret = tlv_put_u##bits(sctx, attrtype, value); \
633 goto tlv_put_failure; \
636 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
637 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
638 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
639 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
640 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
642 ret = tlv_put_string(sctx, attrtype, str, len); \
644 goto tlv_put_failure; \
646 #define TLV_PUT_PATH(sctx, attrtype, p) \
648 ret = tlv_put_string(sctx, attrtype, p->start, \
649 p->end - p->start); \
651 goto tlv_put_failure; \
653 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
655 ret = tlv_put_uuid(sctx, attrtype, uuid); \
657 goto tlv_put_failure; \
659 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
661 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
663 goto tlv_put_failure; \
666 static int send_header(struct send_ctx *sctx)
668 struct btrfs_stream_header hdr;
670 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
671 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
673 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
678 * For each command/item we want to send to userspace, we call this function.
680 static int begin_cmd(struct send_ctx *sctx, int cmd)
682 struct btrfs_cmd_header *hdr;
684 if (WARN_ON(!sctx->send_buf))
687 BUG_ON(sctx->send_size);
689 sctx->send_size += sizeof(*hdr);
690 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
691 hdr->cmd = cpu_to_le16(cmd);
696 static int send_cmd(struct send_ctx *sctx)
699 struct btrfs_cmd_header *hdr;
702 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
703 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
706 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
707 hdr->crc = cpu_to_le32(crc);
709 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
712 sctx->total_send_size += sctx->send_size;
713 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
720 * Sends a move instruction to user space
722 static int send_rename(struct send_ctx *sctx,
723 struct fs_path *from, struct fs_path *to)
725 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
728 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
730 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
734 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
735 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
737 ret = send_cmd(sctx);
745 * Sends a link instruction to user space
747 static int send_link(struct send_ctx *sctx,
748 struct fs_path *path, struct fs_path *lnk)
750 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
753 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
755 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
759 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
760 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
762 ret = send_cmd(sctx);
770 * Sends an unlink instruction to user space
772 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
774 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
777 btrfs_debug(fs_info, "send_unlink %s", path->start);
779 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
783 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
785 ret = send_cmd(sctx);
793 * Sends a rmdir instruction to user space
795 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
797 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
800 btrfs_debug(fs_info, "send_rmdir %s", path->start);
802 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
806 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
808 ret = send_cmd(sctx);
816 * Helper function to retrieve some fields from an inode item.
818 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
819 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
823 struct btrfs_inode_item *ii;
824 struct btrfs_key key;
827 key.type = BTRFS_INODE_ITEM_KEY;
829 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
836 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
837 struct btrfs_inode_item);
839 *size = btrfs_inode_size(path->nodes[0], ii);
841 *gen = btrfs_inode_generation(path->nodes[0], ii);
843 *mode = btrfs_inode_mode(path->nodes[0], ii);
845 *uid = btrfs_inode_uid(path->nodes[0], ii);
847 *gid = btrfs_inode_gid(path->nodes[0], ii);
849 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
854 static int get_inode_info(struct btrfs_root *root,
855 u64 ino, u64 *size, u64 *gen,
856 u64 *mode, u64 *uid, u64 *gid,
859 struct btrfs_path *path;
862 path = alloc_path_for_send();
865 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
867 btrfs_free_path(path);
871 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
876 * Helper function to iterate the entries in ONE btrfs_inode_ref or
877 * btrfs_inode_extref.
878 * The iterate callback may return a non zero value to stop iteration. This can
879 * be a negative value for error codes or 1 to simply stop it.
881 * path must point to the INODE_REF or INODE_EXTREF when called.
883 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
884 struct btrfs_key *found_key, int resolve,
885 iterate_inode_ref_t iterate, void *ctx)
887 struct extent_buffer *eb = path->nodes[0];
888 struct btrfs_item *item;
889 struct btrfs_inode_ref *iref;
890 struct btrfs_inode_extref *extref;
891 struct btrfs_path *tmp_path;
895 int slot = path->slots[0];
902 unsigned long name_off;
903 unsigned long elem_size;
906 p = fs_path_alloc_reversed();
910 tmp_path = alloc_path_for_send();
917 if (found_key->type == BTRFS_INODE_REF_KEY) {
918 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
919 struct btrfs_inode_ref);
920 item = btrfs_item_nr(slot);
921 total = btrfs_item_size(eb, item);
922 elem_size = sizeof(*iref);
924 ptr = btrfs_item_ptr_offset(eb, slot);
925 total = btrfs_item_size_nr(eb, slot);
926 elem_size = sizeof(*extref);
929 while (cur < total) {
932 if (found_key->type == BTRFS_INODE_REF_KEY) {
933 iref = (struct btrfs_inode_ref *)(ptr + cur);
934 name_len = btrfs_inode_ref_name_len(eb, iref);
935 name_off = (unsigned long)(iref + 1);
936 index = btrfs_inode_ref_index(eb, iref);
937 dir = found_key->offset;
939 extref = (struct btrfs_inode_extref *)(ptr + cur);
940 name_len = btrfs_inode_extref_name_len(eb, extref);
941 name_off = (unsigned long)&extref->name;
942 index = btrfs_inode_extref_index(eb, extref);
943 dir = btrfs_inode_extref_parent(eb, extref);
947 start = btrfs_ref_to_path(root, tmp_path, name_len,
951 ret = PTR_ERR(start);
954 if (start < p->buf) {
955 /* overflow , try again with larger buffer */
956 ret = fs_path_ensure_buf(p,
957 p->buf_len + p->buf - start);
960 start = btrfs_ref_to_path(root, tmp_path,
965 ret = PTR_ERR(start);
968 BUG_ON(start < p->buf);
972 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
978 cur += elem_size + name_len;
979 ret = iterate(num, dir, index, p, ctx);
986 btrfs_free_path(tmp_path);
991 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
992 const char *name, int name_len,
993 const char *data, int data_len,
997 * Helper function to iterate the entries in ONE btrfs_dir_item.
998 * The iterate callback may return a non zero value to stop iteration. This can
999 * be a negative value for error codes or 1 to simply stop it.
1001 * path must point to the dir item when called.
1003 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
1004 iterate_dir_item_t iterate, void *ctx)
1007 struct extent_buffer *eb;
1008 struct btrfs_item *item;
1009 struct btrfs_dir_item *di;
1010 struct btrfs_key di_key;
1023 * Start with a small buffer (1 page). If later we end up needing more
1024 * space, which can happen for xattrs on a fs with a leaf size greater
1025 * then the page size, attempt to increase the buffer. Typically xattr
1029 buf = kmalloc(buf_len, GFP_KERNEL);
1035 eb = path->nodes[0];
1036 slot = path->slots[0];
1037 item = btrfs_item_nr(slot);
1038 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1041 total = btrfs_item_size(eb, item);
1044 while (cur < total) {
1045 name_len = btrfs_dir_name_len(eb, di);
1046 data_len = btrfs_dir_data_len(eb, di);
1047 type = btrfs_dir_type(eb, di);
1048 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1050 if (type == BTRFS_FT_XATTR) {
1051 if (name_len > XATTR_NAME_MAX) {
1052 ret = -ENAMETOOLONG;
1055 if (name_len + data_len >
1056 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1064 if (name_len + data_len > PATH_MAX) {
1065 ret = -ENAMETOOLONG;
1070 if (name_len + data_len > buf_len) {
1071 buf_len = name_len + data_len;
1072 if (is_vmalloc_addr(buf)) {
1076 char *tmp = krealloc(buf, buf_len,
1077 GFP_KERNEL | __GFP_NOWARN);
1084 buf = kvmalloc(buf_len, GFP_KERNEL);
1092 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1093 name_len + data_len);
1095 len = sizeof(*di) + name_len + data_len;
1096 di = (struct btrfs_dir_item *)((char *)di + len);
1099 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1100 data_len, type, ctx);
1116 static int __copy_first_ref(int num, u64 dir, int index,
1117 struct fs_path *p, void *ctx)
1120 struct fs_path *pt = ctx;
1122 ret = fs_path_copy(pt, p);
1126 /* we want the first only */
1131 * Retrieve the first path of an inode. If an inode has more then one
1132 * ref/hardlink, this is ignored.
1134 static int get_inode_path(struct btrfs_root *root,
1135 u64 ino, struct fs_path *path)
1138 struct btrfs_key key, found_key;
1139 struct btrfs_path *p;
1141 p = alloc_path_for_send();
1145 fs_path_reset(path);
1148 key.type = BTRFS_INODE_REF_KEY;
1151 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1158 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1159 if (found_key.objectid != ino ||
1160 (found_key.type != BTRFS_INODE_REF_KEY &&
1161 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1166 ret = iterate_inode_ref(root, p, &found_key, 1,
1167 __copy_first_ref, path);
1177 struct backref_ctx {
1178 struct send_ctx *sctx;
1180 /* number of total found references */
1184 * used for clones found in send_root. clones found behind cur_objectid
1185 * and cur_offset are not considered as allowed clones.
1190 /* may be truncated in case it's the last extent in a file */
1193 /* data offset in the file extent item */
1196 /* Just to check for bugs in backref resolving */
1200 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1202 u64 root = (u64)(uintptr_t)key;
1203 struct clone_root *cr = (struct clone_root *)elt;
1205 if (root < cr->root->root_key.objectid)
1207 if (root > cr->root->root_key.objectid)
1212 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1214 struct clone_root *cr1 = (struct clone_root *)e1;
1215 struct clone_root *cr2 = (struct clone_root *)e2;
1217 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
1219 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
1225 * Called for every backref that is found for the current extent.
1226 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1228 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1230 struct backref_ctx *bctx = ctx_;
1231 struct clone_root *found;
1233 /* First check if the root is in the list of accepted clone sources */
1234 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1235 bctx->sctx->clone_roots_cnt,
1236 sizeof(struct clone_root),
1237 __clone_root_cmp_bsearch);
1241 if (found->root == bctx->sctx->send_root &&
1242 ino == bctx->cur_objectid &&
1243 offset == bctx->cur_offset) {
1244 bctx->found_itself = 1;
1248 * Make sure we don't consider clones from send_root that are
1249 * behind the current inode/offset.
1251 if (found->root == bctx->sctx->send_root) {
1253 * If the source inode was not yet processed we can't issue a
1254 * clone operation, as the source extent does not exist yet at
1255 * the destination of the stream.
1257 if (ino > bctx->cur_objectid)
1260 * We clone from the inode currently being sent as long as the
1261 * source extent is already processed, otherwise we could try
1262 * to clone from an extent that does not exist yet at the
1263 * destination of the stream.
1265 if (ino == bctx->cur_objectid &&
1266 offset + bctx->extent_len >
1267 bctx->sctx->cur_inode_next_write_offset)
1272 found->found_refs++;
1273 if (ino < found->ino) {
1275 found->offset = offset;
1276 } else if (found->ino == ino) {
1278 * same extent found more then once in the same file.
1280 if (found->offset > offset + bctx->extent_len)
1281 found->offset = offset;
1288 * Given an inode, offset and extent item, it finds a good clone for a clone
1289 * instruction. Returns -ENOENT when none could be found. The function makes
1290 * sure that the returned clone is usable at the point where sending is at the
1291 * moment. This means, that no clones are accepted which lie behind the current
1294 * path must point to the extent item when called.
1296 static int find_extent_clone(struct send_ctx *sctx,
1297 struct btrfs_path *path,
1298 u64 ino, u64 data_offset,
1300 struct clone_root **found)
1302 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1308 u64 extent_item_pos;
1310 struct btrfs_file_extent_item *fi;
1311 struct extent_buffer *eb = path->nodes[0];
1312 struct backref_ctx *backref_ctx = NULL;
1313 struct clone_root *cur_clone_root;
1314 struct btrfs_key found_key;
1315 struct btrfs_path *tmp_path;
1316 struct btrfs_extent_item *ei;
1320 tmp_path = alloc_path_for_send();
1324 /* We only use this path under the commit sem */
1325 tmp_path->need_commit_sem = 0;
1327 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
1333 if (data_offset >= ino_size) {
1335 * There may be extents that lie behind the file's size.
1336 * I at least had this in combination with snapshotting while
1337 * writing large files.
1343 fi = btrfs_item_ptr(eb, path->slots[0],
1344 struct btrfs_file_extent_item);
1345 extent_type = btrfs_file_extent_type(eb, fi);
1346 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1350 compressed = btrfs_file_extent_compression(eb, fi);
1352 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1353 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1354 if (disk_byte == 0) {
1358 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1360 down_read(&fs_info->commit_root_sem);
1361 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1362 &found_key, &flags);
1363 up_read(&fs_info->commit_root_sem);
1367 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1372 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1373 struct btrfs_extent_item);
1375 * Backreference walking (iterate_extent_inodes() below) is currently
1376 * too expensive when an extent has a large number of references, both
1377 * in time spent and used memory. So for now just fallback to write
1378 * operations instead of clone operations when an extent has more than
1379 * a certain amount of references.
1381 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1385 btrfs_release_path(tmp_path);
1388 * Setup the clone roots.
1390 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1391 cur_clone_root = sctx->clone_roots + i;
1392 cur_clone_root->ino = (u64)-1;
1393 cur_clone_root->offset = 0;
1394 cur_clone_root->found_refs = 0;
1397 backref_ctx->sctx = sctx;
1398 backref_ctx->found = 0;
1399 backref_ctx->cur_objectid = ino;
1400 backref_ctx->cur_offset = data_offset;
1401 backref_ctx->found_itself = 0;
1402 backref_ctx->extent_len = num_bytes;
1404 * For non-compressed extents iterate_extent_inodes() gives us extent
1405 * offsets that already take into account the data offset, but not for
1406 * compressed extents, since the offset is logical and not relative to
1407 * the physical extent locations. We must take this into account to
1408 * avoid sending clone offsets that go beyond the source file's size,
1409 * which would result in the clone ioctl failing with -EINVAL on the
1412 if (compressed == BTRFS_COMPRESS_NONE)
1413 backref_ctx->data_offset = 0;
1415 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
1418 * The last extent of a file may be too large due to page alignment.
1419 * We need to adjust extent_len in this case so that the checks in
1420 * __iterate_backrefs work.
1422 if (data_offset + num_bytes >= ino_size)
1423 backref_ctx->extent_len = ino_size - data_offset;
1426 * Now collect all backrefs.
1428 if (compressed == BTRFS_COMPRESS_NONE)
1429 extent_item_pos = logical - found_key.objectid;
1431 extent_item_pos = 0;
1432 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1433 extent_item_pos, 1, __iterate_backrefs,
1434 backref_ctx, false);
1439 if (!backref_ctx->found_itself) {
1440 /* found a bug in backref code? */
1443 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1444 ino, data_offset, disk_byte, found_key.objectid);
1448 btrfs_debug(fs_info,
1449 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1450 data_offset, ino, num_bytes, logical);
1452 if (!backref_ctx->found)
1453 btrfs_debug(fs_info, "no clones found");
1455 cur_clone_root = NULL;
1456 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1457 if (sctx->clone_roots[i].found_refs) {
1458 if (!cur_clone_root)
1459 cur_clone_root = sctx->clone_roots + i;
1460 else if (sctx->clone_roots[i].root == sctx->send_root)
1461 /* prefer clones from send_root over others */
1462 cur_clone_root = sctx->clone_roots + i;
1467 if (cur_clone_root) {
1468 *found = cur_clone_root;
1475 btrfs_free_path(tmp_path);
1480 static int read_symlink(struct btrfs_root *root,
1482 struct fs_path *dest)
1485 struct btrfs_path *path;
1486 struct btrfs_key key;
1487 struct btrfs_file_extent_item *ei;
1493 path = alloc_path_for_send();
1498 key.type = BTRFS_EXTENT_DATA_KEY;
1500 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1505 * An empty symlink inode. Can happen in rare error paths when
1506 * creating a symlink (transaction committed before the inode
1507 * eviction handler removed the symlink inode items and a crash
1508 * happened in between or the subvol was snapshoted in between).
1509 * Print an informative message to dmesg/syslog so that the user
1510 * can delete the symlink.
1512 btrfs_err(root->fs_info,
1513 "Found empty symlink inode %llu at root %llu",
1514 ino, root->root_key.objectid);
1519 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1520 struct btrfs_file_extent_item);
1521 type = btrfs_file_extent_type(path->nodes[0], ei);
1522 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1523 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1524 BUG_ON(compression);
1526 off = btrfs_file_extent_inline_start(ei);
1527 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
1529 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1532 btrfs_free_path(path);
1537 * Helper function to generate a file name that is unique in the root of
1538 * send_root and parent_root. This is used to generate names for orphan inodes.
1540 static int gen_unique_name(struct send_ctx *sctx,
1542 struct fs_path *dest)
1545 struct btrfs_path *path;
1546 struct btrfs_dir_item *di;
1551 path = alloc_path_for_send();
1556 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1558 ASSERT(len < sizeof(tmp));
1560 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1561 path, BTRFS_FIRST_FREE_OBJECTID,
1562 tmp, strlen(tmp), 0);
1563 btrfs_release_path(path);
1569 /* not unique, try again */
1574 if (!sctx->parent_root) {
1580 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1581 path, BTRFS_FIRST_FREE_OBJECTID,
1582 tmp, strlen(tmp), 0);
1583 btrfs_release_path(path);
1589 /* not unique, try again */
1597 ret = fs_path_add(dest, tmp, strlen(tmp));
1600 btrfs_free_path(path);
1605 inode_state_no_change,
1606 inode_state_will_create,
1607 inode_state_did_create,
1608 inode_state_will_delete,
1609 inode_state_did_delete,
1612 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1620 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1622 if (ret < 0 && ret != -ENOENT)
1626 if (!sctx->parent_root) {
1627 right_ret = -ENOENT;
1629 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1630 NULL, NULL, NULL, NULL);
1631 if (ret < 0 && ret != -ENOENT)
1636 if (!left_ret && !right_ret) {
1637 if (left_gen == gen && right_gen == gen) {
1638 ret = inode_state_no_change;
1639 } else if (left_gen == gen) {
1640 if (ino < sctx->send_progress)
1641 ret = inode_state_did_create;
1643 ret = inode_state_will_create;
1644 } else if (right_gen == gen) {
1645 if (ino < sctx->send_progress)
1646 ret = inode_state_did_delete;
1648 ret = inode_state_will_delete;
1652 } else if (!left_ret) {
1653 if (left_gen == gen) {
1654 if (ino < sctx->send_progress)
1655 ret = inode_state_did_create;
1657 ret = inode_state_will_create;
1661 } else if (!right_ret) {
1662 if (right_gen == gen) {
1663 if (ino < sctx->send_progress)
1664 ret = inode_state_did_delete;
1666 ret = inode_state_will_delete;
1678 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1682 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1685 ret = get_cur_inode_state(sctx, ino, gen);
1689 if (ret == inode_state_no_change ||
1690 ret == inode_state_did_create ||
1691 ret == inode_state_will_delete)
1701 * Helper function to lookup a dir item in a dir.
1703 static int lookup_dir_item_inode(struct btrfs_root *root,
1704 u64 dir, const char *name, int name_len,
1709 struct btrfs_dir_item *di;
1710 struct btrfs_key key;
1711 struct btrfs_path *path;
1713 path = alloc_path_for_send();
1717 di = btrfs_lookup_dir_item(NULL, root, path,
1718 dir, name, name_len, 0);
1719 if (IS_ERR_OR_NULL(di)) {
1720 ret = di ? PTR_ERR(di) : -ENOENT;
1723 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1724 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1728 *found_inode = key.objectid;
1729 *found_type = btrfs_dir_type(path->nodes[0], di);
1732 btrfs_free_path(path);
1737 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1738 * generation of the parent dir and the name of the dir entry.
1740 static int get_first_ref(struct btrfs_root *root, u64 ino,
1741 u64 *dir, u64 *dir_gen, struct fs_path *name)
1744 struct btrfs_key key;
1745 struct btrfs_key found_key;
1746 struct btrfs_path *path;
1750 path = alloc_path_for_send();
1755 key.type = BTRFS_INODE_REF_KEY;
1758 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1762 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1764 if (ret || found_key.objectid != ino ||
1765 (found_key.type != BTRFS_INODE_REF_KEY &&
1766 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1771 if (found_key.type == BTRFS_INODE_REF_KEY) {
1772 struct btrfs_inode_ref *iref;
1773 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1774 struct btrfs_inode_ref);
1775 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1776 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1777 (unsigned long)(iref + 1),
1779 parent_dir = found_key.offset;
1781 struct btrfs_inode_extref *extref;
1782 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1783 struct btrfs_inode_extref);
1784 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1785 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1786 (unsigned long)&extref->name, len);
1787 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1791 btrfs_release_path(path);
1794 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1803 btrfs_free_path(path);
1807 static int is_first_ref(struct btrfs_root *root,
1809 const char *name, int name_len)
1812 struct fs_path *tmp_name;
1815 tmp_name = fs_path_alloc();
1819 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1823 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1828 ret = !memcmp(tmp_name->start, name, name_len);
1831 fs_path_free(tmp_name);
1836 * Used by process_recorded_refs to determine if a new ref would overwrite an
1837 * already existing ref. In case it detects an overwrite, it returns the
1838 * inode/gen in who_ino/who_gen.
1839 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1840 * to make sure later references to the overwritten inode are possible.
1841 * Orphanizing is however only required for the first ref of an inode.
1842 * process_recorded_refs does an additional is_first_ref check to see if
1843 * orphanizing is really required.
1845 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1846 const char *name, int name_len,
1847 u64 *who_ino, u64 *who_gen, u64 *who_mode)
1851 u64 other_inode = 0;
1854 if (!sctx->parent_root)
1857 ret = is_inode_existent(sctx, dir, dir_gen);
1862 * If we have a parent root we need to verify that the parent dir was
1863 * not deleted and then re-created, if it was then we have no overwrite
1864 * and we can just unlink this entry.
1866 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
1867 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1869 if (ret < 0 && ret != -ENOENT)
1879 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1880 &other_inode, &other_type);
1881 if (ret < 0 && ret != -ENOENT)
1889 * Check if the overwritten ref was already processed. If yes, the ref
1890 * was already unlinked/moved, so we can safely assume that we will not
1891 * overwrite anything at this point in time.
1893 if (other_inode > sctx->send_progress ||
1894 is_waiting_for_move(sctx, other_inode)) {
1895 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1896 who_gen, who_mode, NULL, NULL, NULL);
1901 *who_ino = other_inode;
1911 * Checks if the ref was overwritten by an already processed inode. This is
1912 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1913 * thus the orphan name needs be used.
1914 * process_recorded_refs also uses it to avoid unlinking of refs that were
1917 static int did_overwrite_ref(struct send_ctx *sctx,
1918 u64 dir, u64 dir_gen,
1919 u64 ino, u64 ino_gen,
1920 const char *name, int name_len)
1927 if (!sctx->parent_root)
1930 ret = is_inode_existent(sctx, dir, dir_gen);
1934 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1935 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1937 if (ret < 0 && ret != -ENOENT)
1947 /* check if the ref was overwritten by another ref */
1948 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1949 &ow_inode, &other_type);
1950 if (ret < 0 && ret != -ENOENT)
1953 /* was never and will never be overwritten */
1958 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1963 if (ow_inode == ino && gen == ino_gen) {
1969 * We know that it is or will be overwritten. Check this now.
1970 * The current inode being processed might have been the one that caused
1971 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1972 * the current inode being processed.
1974 if ((ow_inode < sctx->send_progress) ||
1975 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1976 gen == sctx->cur_inode_gen))
1986 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1987 * that got overwritten. This is used by process_recorded_refs to determine
1988 * if it has to use the path as returned by get_cur_path or the orphan name.
1990 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1993 struct fs_path *name = NULL;
1997 if (!sctx->parent_root)
2000 name = fs_path_alloc();
2004 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
2008 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2009 name->start, fs_path_len(name));
2017 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2018 * so we need to do some special handling in case we have clashes. This function
2019 * takes care of this with the help of name_cache_entry::radix_list.
2020 * In case of error, nce is kfreed.
2022 static int name_cache_insert(struct send_ctx *sctx,
2023 struct name_cache_entry *nce)
2026 struct list_head *nce_head;
2028 nce_head = radix_tree_lookup(&sctx->name_cache,
2029 (unsigned long)nce->ino);
2031 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
2036 INIT_LIST_HEAD(nce_head);
2038 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
2045 list_add_tail(&nce->radix_list, nce_head);
2046 list_add_tail(&nce->list, &sctx->name_cache_list);
2047 sctx->name_cache_size++;
2052 static void name_cache_delete(struct send_ctx *sctx,
2053 struct name_cache_entry *nce)
2055 struct list_head *nce_head;
2057 nce_head = radix_tree_lookup(&sctx->name_cache,
2058 (unsigned long)nce->ino);
2060 btrfs_err(sctx->send_root->fs_info,
2061 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2062 nce->ino, sctx->name_cache_size);
2065 list_del(&nce->radix_list);
2066 list_del(&nce->list);
2067 sctx->name_cache_size--;
2070 * We may not get to the final release of nce_head if the lookup fails
2072 if (nce_head && list_empty(nce_head)) {
2073 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2078 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2081 struct list_head *nce_head;
2082 struct name_cache_entry *cur;
2084 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2088 list_for_each_entry(cur, nce_head, radix_list) {
2089 if (cur->ino == ino && cur->gen == gen)
2096 * Removes the entry from the list and adds it back to the end. This marks the
2097 * entry as recently used so that name_cache_clean_unused does not remove it.
2099 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2101 list_del(&nce->list);
2102 list_add_tail(&nce->list, &sctx->name_cache_list);
2106 * Remove some entries from the beginning of name_cache_list.
2108 static void name_cache_clean_unused(struct send_ctx *sctx)
2110 struct name_cache_entry *nce;
2112 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2115 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2116 nce = list_entry(sctx->name_cache_list.next,
2117 struct name_cache_entry, list);
2118 name_cache_delete(sctx, nce);
2123 static void name_cache_free(struct send_ctx *sctx)
2125 struct name_cache_entry *nce;
2127 while (!list_empty(&sctx->name_cache_list)) {
2128 nce = list_entry(sctx->name_cache_list.next,
2129 struct name_cache_entry, list);
2130 name_cache_delete(sctx, nce);
2136 * Used by get_cur_path for each ref up to the root.
2137 * Returns 0 if it succeeded.
2138 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2139 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2140 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2141 * Returns <0 in case of error.
2143 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2147 struct fs_path *dest)
2151 struct name_cache_entry *nce = NULL;
2154 * First check if we already did a call to this function with the same
2155 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2156 * return the cached result.
2158 nce = name_cache_search(sctx, ino, gen);
2160 if (ino < sctx->send_progress && nce->need_later_update) {
2161 name_cache_delete(sctx, nce);
2165 name_cache_used(sctx, nce);
2166 *parent_ino = nce->parent_ino;
2167 *parent_gen = nce->parent_gen;
2168 ret = fs_path_add(dest, nce->name, nce->name_len);
2177 * If the inode is not existent yet, add the orphan name and return 1.
2178 * This should only happen for the parent dir that we determine in
2181 ret = is_inode_existent(sctx, ino, gen);
2186 ret = gen_unique_name(sctx, ino, gen, dest);
2194 * Depending on whether the inode was already processed or not, use
2195 * send_root or parent_root for ref lookup.
2197 if (ino < sctx->send_progress)
2198 ret = get_first_ref(sctx->send_root, ino,
2199 parent_ino, parent_gen, dest);
2201 ret = get_first_ref(sctx->parent_root, ino,
2202 parent_ino, parent_gen, dest);
2207 * Check if the ref was overwritten by an inode's ref that was processed
2208 * earlier. If yes, treat as orphan and return 1.
2210 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2211 dest->start, dest->end - dest->start);
2215 fs_path_reset(dest);
2216 ret = gen_unique_name(sctx, ino, gen, dest);
2224 * Store the result of the lookup in the name cache.
2226 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2234 nce->parent_ino = *parent_ino;
2235 nce->parent_gen = *parent_gen;
2236 nce->name_len = fs_path_len(dest);
2238 strcpy(nce->name, dest->start);
2240 if (ino < sctx->send_progress)
2241 nce->need_later_update = 0;
2243 nce->need_later_update = 1;
2245 nce_ret = name_cache_insert(sctx, nce);
2248 name_cache_clean_unused(sctx);
2255 * Magic happens here. This function returns the first ref to an inode as it
2256 * would look like while receiving the stream at this point in time.
2257 * We walk the path up to the root. For every inode in between, we check if it
2258 * was already processed/sent. If yes, we continue with the parent as found
2259 * in send_root. If not, we continue with the parent as found in parent_root.
2260 * If we encounter an inode that was deleted at this point in time, we use the
2261 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2262 * that were not created yet and overwritten inodes/refs.
2264 * When do we have orphan inodes:
2265 * 1. When an inode is freshly created and thus no valid refs are available yet
2266 * 2. When a directory lost all it's refs (deleted) but still has dir items
2267 * inside which were not processed yet (pending for move/delete). If anyone
2268 * tried to get the path to the dir items, it would get a path inside that
2270 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2271 * of an unprocessed inode. If in that case the first ref would be
2272 * overwritten, the overwritten inode gets "orphanized". Later when we
2273 * process this overwritten inode, it is restored at a new place by moving
2276 * sctx->send_progress tells this function at which point in time receiving
2279 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2280 struct fs_path *dest)
2283 struct fs_path *name = NULL;
2284 u64 parent_inode = 0;
2288 name = fs_path_alloc();
2295 fs_path_reset(dest);
2297 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2298 struct waiting_dir_move *wdm;
2300 fs_path_reset(name);
2302 if (is_waiting_for_rm(sctx, ino)) {
2303 ret = gen_unique_name(sctx, ino, gen, name);
2306 ret = fs_path_add_path(dest, name);
2310 wdm = get_waiting_dir_move(sctx, ino);
2311 if (wdm && wdm->orphanized) {
2312 ret = gen_unique_name(sctx, ino, gen, name);
2315 ret = get_first_ref(sctx->parent_root, ino,
2316 &parent_inode, &parent_gen, name);
2318 ret = __get_cur_name_and_parent(sctx, ino, gen,
2328 ret = fs_path_add_path(dest, name);
2339 fs_path_unreverse(dest);
2344 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2346 static int send_subvol_begin(struct send_ctx *sctx)
2349 struct btrfs_root *send_root = sctx->send_root;
2350 struct btrfs_root *parent_root = sctx->parent_root;
2351 struct btrfs_path *path;
2352 struct btrfs_key key;
2353 struct btrfs_root_ref *ref;
2354 struct extent_buffer *leaf;
2358 path = btrfs_alloc_path();
2362 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2364 btrfs_free_path(path);
2368 key.objectid = send_root->root_key.objectid;
2369 key.type = BTRFS_ROOT_BACKREF_KEY;
2372 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2381 leaf = path->nodes[0];
2382 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2383 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2384 key.objectid != send_root->root_key.objectid) {
2388 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2389 namelen = btrfs_root_ref_name_len(leaf, ref);
2390 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2391 btrfs_release_path(path);
2394 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2398 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2403 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2405 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2406 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2407 sctx->send_root->root_item.received_uuid);
2409 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2410 sctx->send_root->root_item.uuid);
2412 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2413 le64_to_cpu(sctx->send_root->root_item.ctransid));
2415 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2416 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2417 parent_root->root_item.received_uuid);
2419 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2420 parent_root->root_item.uuid);
2421 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2422 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2425 ret = send_cmd(sctx);
2429 btrfs_free_path(path);
2434 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2436 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2440 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
2442 p = fs_path_alloc();
2446 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2450 ret = get_cur_path(sctx, ino, gen, p);
2453 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2454 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2456 ret = send_cmd(sctx);
2464 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2466 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2470 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
2472 p = fs_path_alloc();
2476 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2480 ret = get_cur_path(sctx, ino, gen, p);
2483 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2484 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2486 ret = send_cmd(sctx);
2494 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2496 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2500 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2503 p = fs_path_alloc();
2507 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2511 ret = get_cur_path(sctx, ino, gen, p);
2514 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2515 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2516 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2518 ret = send_cmd(sctx);
2526 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2528 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2530 struct fs_path *p = NULL;
2531 struct btrfs_inode_item *ii;
2532 struct btrfs_path *path = NULL;
2533 struct extent_buffer *eb;
2534 struct btrfs_key key;
2537 btrfs_debug(fs_info, "send_utimes %llu", ino);
2539 p = fs_path_alloc();
2543 path = alloc_path_for_send();
2550 key.type = BTRFS_INODE_ITEM_KEY;
2552 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2558 eb = path->nodes[0];
2559 slot = path->slots[0];
2560 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2562 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2566 ret = get_cur_path(sctx, ino, gen, p);
2569 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2570 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2571 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2572 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2573 /* TODO Add otime support when the otime patches get into upstream */
2575 ret = send_cmd(sctx);
2580 btrfs_free_path(path);
2585 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2586 * a valid path yet because we did not process the refs yet. So, the inode
2587 * is created as orphan.
2589 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2591 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2599 btrfs_debug(fs_info, "send_create_inode %llu", ino);
2601 p = fs_path_alloc();
2605 if (ino != sctx->cur_ino) {
2606 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2611 gen = sctx->cur_inode_gen;
2612 mode = sctx->cur_inode_mode;
2613 rdev = sctx->cur_inode_rdev;
2616 if (S_ISREG(mode)) {
2617 cmd = BTRFS_SEND_C_MKFILE;
2618 } else if (S_ISDIR(mode)) {
2619 cmd = BTRFS_SEND_C_MKDIR;
2620 } else if (S_ISLNK(mode)) {
2621 cmd = BTRFS_SEND_C_SYMLINK;
2622 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2623 cmd = BTRFS_SEND_C_MKNOD;
2624 } else if (S_ISFIFO(mode)) {
2625 cmd = BTRFS_SEND_C_MKFIFO;
2626 } else if (S_ISSOCK(mode)) {
2627 cmd = BTRFS_SEND_C_MKSOCK;
2629 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2630 (int)(mode & S_IFMT));
2635 ret = begin_cmd(sctx, cmd);
2639 ret = gen_unique_name(sctx, ino, gen, p);
2643 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2644 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2646 if (S_ISLNK(mode)) {
2648 ret = read_symlink(sctx->send_root, ino, p);
2651 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2652 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2653 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2654 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2655 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2658 ret = send_cmd(sctx);
2670 * We need some special handling for inodes that get processed before the parent
2671 * directory got created. See process_recorded_refs for details.
2672 * This function does the check if we already created the dir out of order.
2674 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2677 struct btrfs_path *path = NULL;
2678 struct btrfs_key key;
2679 struct btrfs_key found_key;
2680 struct btrfs_key di_key;
2681 struct extent_buffer *eb;
2682 struct btrfs_dir_item *di;
2685 path = alloc_path_for_send();
2692 key.type = BTRFS_DIR_INDEX_KEY;
2694 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2699 eb = path->nodes[0];
2700 slot = path->slots[0];
2701 if (slot >= btrfs_header_nritems(eb)) {
2702 ret = btrfs_next_leaf(sctx->send_root, path);
2705 } else if (ret > 0) {
2712 btrfs_item_key_to_cpu(eb, &found_key, slot);
2713 if (found_key.objectid != key.objectid ||
2714 found_key.type != key.type) {
2719 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2720 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2722 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2723 di_key.objectid < sctx->send_progress) {
2732 btrfs_free_path(path);
2737 * Only creates the inode if it is:
2738 * 1. Not a directory
2739 * 2. Or a directory which was not created already due to out of order
2740 * directories. See did_create_dir and process_recorded_refs for details.
2742 static int send_create_inode_if_needed(struct send_ctx *sctx)
2746 if (S_ISDIR(sctx->cur_inode_mode)) {
2747 ret = did_create_dir(sctx, sctx->cur_ino);
2756 ret = send_create_inode(sctx, sctx->cur_ino);
2764 struct recorded_ref {
2765 struct list_head list;
2767 struct fs_path *full_path;
2773 static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2775 ref->full_path = path;
2776 ref->name = (char *)kbasename(ref->full_path->start);
2777 ref->name_len = ref->full_path->end - ref->name;
2781 * We need to process new refs before deleted refs, but compare_tree gives us
2782 * everything mixed. So we first record all refs and later process them.
2783 * This function is a helper to record one ref.
2785 static int __record_ref(struct list_head *head, u64 dir,
2786 u64 dir_gen, struct fs_path *path)
2788 struct recorded_ref *ref;
2790 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
2795 ref->dir_gen = dir_gen;
2796 set_ref_path(ref, path);
2797 list_add_tail(&ref->list, head);
2801 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2803 struct recorded_ref *new;
2805 new = kmalloc(sizeof(*ref), GFP_KERNEL);
2809 new->dir = ref->dir;
2810 new->dir_gen = ref->dir_gen;
2811 new->full_path = NULL;
2812 INIT_LIST_HEAD(&new->list);
2813 list_add_tail(&new->list, list);
2817 static void __free_recorded_refs(struct list_head *head)
2819 struct recorded_ref *cur;
2821 while (!list_empty(head)) {
2822 cur = list_entry(head->next, struct recorded_ref, list);
2823 fs_path_free(cur->full_path);
2824 list_del(&cur->list);
2829 static void free_recorded_refs(struct send_ctx *sctx)
2831 __free_recorded_refs(&sctx->new_refs);
2832 __free_recorded_refs(&sctx->deleted_refs);
2836 * Renames/moves a file/dir to its orphan name. Used when the first
2837 * ref of an unprocessed inode gets overwritten and for all non empty
2840 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2841 struct fs_path *path)
2844 struct fs_path *orphan;
2846 orphan = fs_path_alloc();
2850 ret = gen_unique_name(sctx, ino, gen, orphan);
2854 ret = send_rename(sctx, path, orphan);
2857 fs_path_free(orphan);
2861 static struct orphan_dir_info *
2862 add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2864 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2865 struct rb_node *parent = NULL;
2866 struct orphan_dir_info *entry, *odi;
2870 entry = rb_entry(parent, struct orphan_dir_info, node);
2871 if (dir_ino < entry->ino) {
2873 } else if (dir_ino > entry->ino) {
2874 p = &(*p)->rb_right;
2880 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2882 return ERR_PTR(-ENOMEM);
2885 odi->last_dir_index_offset = 0;
2887 rb_link_node(&odi->node, parent, p);
2888 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2892 static struct orphan_dir_info *
2893 get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2895 struct rb_node *n = sctx->orphan_dirs.rb_node;
2896 struct orphan_dir_info *entry;
2899 entry = rb_entry(n, struct orphan_dir_info, node);
2900 if (dir_ino < entry->ino)
2902 else if (dir_ino > entry->ino)
2910 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2912 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2917 static void free_orphan_dir_info(struct send_ctx *sctx,
2918 struct orphan_dir_info *odi)
2922 rb_erase(&odi->node, &sctx->orphan_dirs);
2927 * Returns 1 if a directory can be removed at this point in time.
2928 * We check this by iterating all dir items and checking if the inode behind
2929 * the dir item was already processed.
2931 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2935 struct btrfs_root *root = sctx->parent_root;
2936 struct btrfs_path *path;
2937 struct btrfs_key key;
2938 struct btrfs_key found_key;
2939 struct btrfs_key loc;
2940 struct btrfs_dir_item *di;
2941 struct orphan_dir_info *odi = NULL;
2944 * Don't try to rmdir the top/root subvolume dir.
2946 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2949 path = alloc_path_for_send();
2954 key.type = BTRFS_DIR_INDEX_KEY;
2957 odi = get_orphan_dir_info(sctx, dir);
2959 key.offset = odi->last_dir_index_offset;
2961 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2966 struct waiting_dir_move *dm;
2968 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2969 ret = btrfs_next_leaf(root, path);
2976 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2978 if (found_key.objectid != key.objectid ||
2979 found_key.type != key.type)
2982 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2983 struct btrfs_dir_item);
2984 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2986 dm = get_waiting_dir_move(sctx, loc.objectid);
2988 odi = add_orphan_dir_info(sctx, dir);
2994 odi->last_dir_index_offset = found_key.offset;
2995 dm->rmdir_ino = dir;
3000 if (loc.objectid > send_progress) {
3001 odi = add_orphan_dir_info(sctx, dir);
3007 odi->last_dir_index_offset = found_key.offset;
3014 free_orphan_dir_info(sctx, odi);
3019 btrfs_free_path(path);
3023 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3025 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3027 return entry != NULL;
3030 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3032 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3033 struct rb_node *parent = NULL;
3034 struct waiting_dir_move *entry, *dm;
3036 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
3041 dm->orphanized = orphanized;
3045 entry = rb_entry(parent, struct waiting_dir_move, node);
3046 if (ino < entry->ino) {
3048 } else if (ino > entry->ino) {
3049 p = &(*p)->rb_right;
3056 rb_link_node(&dm->node, parent, p);
3057 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3061 static struct waiting_dir_move *
3062 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3064 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3065 struct waiting_dir_move *entry;
3068 entry = rb_entry(n, struct waiting_dir_move, node);
3069 if (ino < entry->ino)
3071 else if (ino > entry->ino)
3079 static void free_waiting_dir_move(struct send_ctx *sctx,
3080 struct waiting_dir_move *dm)
3084 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3088 static int add_pending_dir_move(struct send_ctx *sctx,
3092 struct list_head *new_refs,
3093 struct list_head *deleted_refs,
3094 const bool is_orphan)
3096 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3097 struct rb_node *parent = NULL;
3098 struct pending_dir_move *entry = NULL, *pm;
3099 struct recorded_ref *cur;
3103 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3106 pm->parent_ino = parent_ino;
3109 INIT_LIST_HEAD(&pm->list);
3110 INIT_LIST_HEAD(&pm->update_refs);
3111 RB_CLEAR_NODE(&pm->node);
3115 entry = rb_entry(parent, struct pending_dir_move, node);
3116 if (parent_ino < entry->parent_ino) {
3118 } else if (parent_ino > entry->parent_ino) {
3119 p = &(*p)->rb_right;
3126 list_for_each_entry(cur, deleted_refs, list) {
3127 ret = dup_ref(cur, &pm->update_refs);
3131 list_for_each_entry(cur, new_refs, list) {
3132 ret = dup_ref(cur, &pm->update_refs);
3137 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3142 list_add_tail(&pm->list, &entry->list);
3144 rb_link_node(&pm->node, parent, p);
3145 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3150 __free_recorded_refs(&pm->update_refs);
3156 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3159 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3160 struct pending_dir_move *entry;
3163 entry = rb_entry(n, struct pending_dir_move, node);
3164 if (parent_ino < entry->parent_ino)
3166 else if (parent_ino > entry->parent_ino)
3174 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3175 u64 ino, u64 gen, u64 *ancestor_ino)
3178 u64 parent_inode = 0;
3180 u64 start_ino = ino;
3183 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3184 fs_path_reset(name);
3186 if (is_waiting_for_rm(sctx, ino))
3188 if (is_waiting_for_move(sctx, ino)) {
3189 if (*ancestor_ino == 0)
3190 *ancestor_ino = ino;
3191 ret = get_first_ref(sctx->parent_root, ino,
3192 &parent_inode, &parent_gen, name);
3194 ret = __get_cur_name_and_parent(sctx, ino, gen,
3204 if (parent_inode == start_ino) {
3206 if (*ancestor_ino == 0)
3207 *ancestor_ino = ino;
3216 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3218 struct fs_path *from_path = NULL;
3219 struct fs_path *to_path = NULL;
3220 struct fs_path *name = NULL;
3221 u64 orig_progress = sctx->send_progress;
3222 struct recorded_ref *cur;
3223 u64 parent_ino, parent_gen;
3224 struct waiting_dir_move *dm = NULL;
3230 name = fs_path_alloc();
3231 from_path = fs_path_alloc();
3232 if (!name || !from_path) {
3237 dm = get_waiting_dir_move(sctx, pm->ino);
3239 rmdir_ino = dm->rmdir_ino;
3240 is_orphan = dm->orphanized;
3241 free_waiting_dir_move(sctx, dm);
3244 ret = gen_unique_name(sctx, pm->ino,
3245 pm->gen, from_path);
3247 ret = get_first_ref(sctx->parent_root, pm->ino,
3248 &parent_ino, &parent_gen, name);
3251 ret = get_cur_path(sctx, parent_ino, parent_gen,
3255 ret = fs_path_add_path(from_path, name);
3260 sctx->send_progress = sctx->cur_ino + 1;
3261 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3265 LIST_HEAD(deleted_refs);
3266 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3267 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3268 &pm->update_refs, &deleted_refs,
3273 dm = get_waiting_dir_move(sctx, pm->ino);
3275 dm->rmdir_ino = rmdir_ino;
3279 fs_path_reset(name);
3282 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3286 ret = send_rename(sctx, from_path, to_path);
3291 struct orphan_dir_info *odi;
3294 odi = get_orphan_dir_info(sctx, rmdir_ino);
3296 /* already deleted */
3301 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
3307 name = fs_path_alloc();
3312 ret = get_cur_path(sctx, rmdir_ino, gen, name);
3315 ret = send_rmdir(sctx, name);
3321 ret = send_utimes(sctx, pm->ino, pm->gen);
3326 * After rename/move, need to update the utimes of both new parent(s)
3327 * and old parent(s).
3329 list_for_each_entry(cur, &pm->update_refs, list) {
3331 * The parent inode might have been deleted in the send snapshot
3333 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3334 NULL, NULL, NULL, NULL, NULL);
3335 if (ret == -ENOENT) {
3342 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3349 fs_path_free(from_path);
3350 fs_path_free(to_path);
3351 sctx->send_progress = orig_progress;
3356 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3358 if (!list_empty(&m->list))
3360 if (!RB_EMPTY_NODE(&m->node))
3361 rb_erase(&m->node, &sctx->pending_dir_moves);
3362 __free_recorded_refs(&m->update_refs);
3366 static void tail_append_pending_moves(struct send_ctx *sctx,
3367 struct pending_dir_move *moves,
3368 struct list_head *stack)
3370 if (list_empty(&moves->list)) {
3371 list_add_tail(&moves->list, stack);
3374 list_splice_init(&moves->list, &list);
3375 list_add_tail(&moves->list, stack);
3376 list_splice_tail(&list, stack);
3378 if (!RB_EMPTY_NODE(&moves->node)) {
3379 rb_erase(&moves->node, &sctx->pending_dir_moves);
3380 RB_CLEAR_NODE(&moves->node);
3384 static int apply_children_dir_moves(struct send_ctx *sctx)
3386 struct pending_dir_move *pm;
3387 struct list_head stack;
3388 u64 parent_ino = sctx->cur_ino;
3391 pm = get_pending_dir_moves(sctx, parent_ino);
3395 INIT_LIST_HEAD(&stack);
3396 tail_append_pending_moves(sctx, pm, &stack);
3398 while (!list_empty(&stack)) {
3399 pm = list_first_entry(&stack, struct pending_dir_move, list);
3400 parent_ino = pm->ino;
3401 ret = apply_dir_move(sctx, pm);
3402 free_pending_move(sctx, pm);
3405 pm = get_pending_dir_moves(sctx, parent_ino);
3407 tail_append_pending_moves(sctx, pm, &stack);
3412 while (!list_empty(&stack)) {
3413 pm = list_first_entry(&stack, struct pending_dir_move, list);
3414 free_pending_move(sctx, pm);
3420 * We might need to delay a directory rename even when no ancestor directory
3421 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3422 * renamed. This happens when we rename a directory to the old name (the name
3423 * in the parent root) of some other unrelated directory that got its rename
3424 * delayed due to some ancestor with higher number that got renamed.
3430 * |---- a/ (ino 257)
3431 * | |---- file (ino 260)
3433 * |---- b/ (ino 258)
3434 * |---- c/ (ino 259)
3438 * |---- a/ (ino 258)
3439 * |---- x/ (ino 259)
3440 * |---- y/ (ino 257)
3441 * |----- file (ino 260)
3443 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3444 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3445 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3448 * 1 - rename 259 from 'c' to 'x'
3449 * 2 - rename 257 from 'a' to 'x/y'
3450 * 3 - rename 258 from 'b' to 'a'
3452 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3453 * be done right away and < 0 on error.
3455 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3456 struct recorded_ref *parent_ref,
3457 const bool is_orphan)
3459 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3460 struct btrfs_path *path;
3461 struct btrfs_key key;
3462 struct btrfs_key di_key;
3463 struct btrfs_dir_item *di;
3467 struct waiting_dir_move *wdm;
3469 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3472 path = alloc_path_for_send();
3476 key.objectid = parent_ref->dir;
3477 key.type = BTRFS_DIR_ITEM_KEY;
3478 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3480 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3483 } else if (ret > 0) {
3488 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3489 parent_ref->name_len);
3495 * di_key.objectid has the number of the inode that has a dentry in the
3496 * parent directory with the same name that sctx->cur_ino is being
3497 * renamed to. We need to check if that inode is in the send root as
3498 * well and if it is currently marked as an inode with a pending rename,
3499 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3500 * that it happens after that other inode is renamed.
3502 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3503 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3508 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3509 &left_gen, NULL, NULL, NULL, NULL);
3512 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3513 &right_gen, NULL, NULL, NULL, NULL);
3520 /* Different inode, no need to delay the rename of sctx->cur_ino */
3521 if (right_gen != left_gen) {
3526 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3527 if (wdm && !wdm->orphanized) {
3528 ret = add_pending_dir_move(sctx,
3530 sctx->cur_inode_gen,
3533 &sctx->deleted_refs,
3539 btrfs_free_path(path);
3544 * Check if inode ino2, or any of its ancestors, is inode ino1.
3545 * Return 1 if true, 0 if false and < 0 on error.
3547 static int check_ino_in_path(struct btrfs_root *root,
3552 struct fs_path *fs_path)
3557 return ino1_gen == ino2_gen;
3559 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3564 fs_path_reset(fs_path);
3565 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3569 return parent_gen == ino1_gen;
3576 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3577 * possible path (in case ino2 is not a directory and has multiple hard links).
3578 * Return 1 if true, 0 if false and < 0 on error.
3580 static int is_ancestor(struct btrfs_root *root,
3584 struct fs_path *fs_path)
3586 bool free_fs_path = false;
3588 struct btrfs_path *path = NULL;
3589 struct btrfs_key key;
3592 fs_path = fs_path_alloc();
3595 free_fs_path = true;
3598 path = alloc_path_for_send();
3604 key.objectid = ino2;
3605 key.type = BTRFS_INODE_REF_KEY;
3608 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3613 struct extent_buffer *leaf = path->nodes[0];
3614 int slot = path->slots[0];
3618 if (slot >= btrfs_header_nritems(leaf)) {
3619 ret = btrfs_next_leaf(root, path);
3627 btrfs_item_key_to_cpu(leaf, &key, slot);
3628 if (key.objectid != ino2)
3630 if (key.type != BTRFS_INODE_REF_KEY &&
3631 key.type != BTRFS_INODE_EXTREF_KEY)
3634 item_size = btrfs_item_size_nr(leaf, slot);
3635 while (cur_offset < item_size) {
3639 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3641 struct btrfs_inode_extref *extref;
3643 ptr = btrfs_item_ptr_offset(leaf, slot);
3644 extref = (struct btrfs_inode_extref *)
3646 parent = btrfs_inode_extref_parent(leaf,
3648 cur_offset += sizeof(*extref);
3649 cur_offset += btrfs_inode_extref_name_len(leaf,
3652 parent = key.offset;
3653 cur_offset = item_size;
3656 ret = get_inode_info(root, parent, NULL, &parent_gen,
3657 NULL, NULL, NULL, NULL);
3660 ret = check_ino_in_path(root, ino1, ino1_gen,
3661 parent, parent_gen, fs_path);
3669 btrfs_free_path(path);
3671 fs_path_free(fs_path);
3675 static int wait_for_parent_move(struct send_ctx *sctx,
3676 struct recorded_ref *parent_ref,
3677 const bool is_orphan)
3680 u64 ino = parent_ref->dir;
3681 u64 ino_gen = parent_ref->dir_gen;
3682 u64 parent_ino_before, parent_ino_after;
3683 struct fs_path *path_before = NULL;
3684 struct fs_path *path_after = NULL;
3687 path_after = fs_path_alloc();
3688 path_before = fs_path_alloc();
3689 if (!path_after || !path_before) {
3695 * Our current directory inode may not yet be renamed/moved because some
3696 * ancestor (immediate or not) has to be renamed/moved first. So find if
3697 * such ancestor exists and make sure our own rename/move happens after
3698 * that ancestor is processed to avoid path build infinite loops (done
3699 * at get_cur_path()).
3701 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3702 u64 parent_ino_after_gen;
3704 if (is_waiting_for_move(sctx, ino)) {
3706 * If the current inode is an ancestor of ino in the
3707 * parent root, we need to delay the rename of the
3708 * current inode, otherwise don't delayed the rename
3709 * because we can end up with a circular dependency
3710 * of renames, resulting in some directories never
3711 * getting the respective rename operations issued in
3712 * the send stream or getting into infinite path build
3715 ret = is_ancestor(sctx->parent_root,
3716 sctx->cur_ino, sctx->cur_inode_gen,
3722 fs_path_reset(path_before);
3723 fs_path_reset(path_after);
3725 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3726 &parent_ino_after_gen, path_after);
3729 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3731 if (ret < 0 && ret != -ENOENT) {
3733 } else if (ret == -ENOENT) {
3738 len1 = fs_path_len(path_before);
3739 len2 = fs_path_len(path_after);
3740 if (ino > sctx->cur_ino &&
3741 (parent_ino_before != parent_ino_after || len1 != len2 ||
3742 memcmp(path_before->start, path_after->start, len1))) {
3745 ret = get_inode_info(sctx->parent_root, ino, NULL,
3746 &parent_ino_gen, NULL, NULL, NULL,
3750 if (ino_gen == parent_ino_gen) {
3755 ino = parent_ino_after;
3756 ino_gen = parent_ino_after_gen;
3760 fs_path_free(path_before);
3761 fs_path_free(path_after);
3764 ret = add_pending_dir_move(sctx,
3766 sctx->cur_inode_gen,
3769 &sctx->deleted_refs,
3778 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3781 struct fs_path *new_path;
3784 * Our reference's name member points to its full_path member string, so
3785 * we use here a new path.
3787 new_path = fs_path_alloc();
3791 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3793 fs_path_free(new_path);
3796 ret = fs_path_add(new_path, ref->name, ref->name_len);
3798 fs_path_free(new_path);
3802 fs_path_free(ref->full_path);
3803 set_ref_path(ref, new_path);
3809 * This does all the move/link/unlink/rmdir magic.
3811 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3813 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
3815 struct recorded_ref *cur;
3816 struct recorded_ref *cur2;
3817 struct list_head check_dirs;
3818 struct fs_path *valid_path = NULL;
3822 int did_overwrite = 0;
3824 u64 last_dir_ino_rm = 0;
3825 bool can_rename = true;
3826 bool orphanized_dir = false;
3827 bool orphanized_ancestor = false;
3829 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
3832 * This should never happen as the root dir always has the same ref
3833 * which is always '..'
3835 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3836 INIT_LIST_HEAD(&check_dirs);
3838 valid_path = fs_path_alloc();
3845 * First, check if the first ref of the current inode was overwritten
3846 * before. If yes, we know that the current inode was already orphanized
3847 * and thus use the orphan name. If not, we can use get_cur_path to
3848 * get the path of the first ref as it would like while receiving at
3849 * this point in time.
3850 * New inodes are always orphan at the beginning, so force to use the
3851 * orphan name in this case.
3852 * The first ref is stored in valid_path and will be updated if it
3853 * gets moved around.
3855 if (!sctx->cur_inode_new) {
3856 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3857 sctx->cur_inode_gen);
3863 if (sctx->cur_inode_new || did_overwrite) {
3864 ret = gen_unique_name(sctx, sctx->cur_ino,
3865 sctx->cur_inode_gen, valid_path);
3870 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3876 list_for_each_entry(cur, &sctx->new_refs, list) {
3878 * We may have refs where the parent directory does not exist
3879 * yet. This happens if the parent directories inum is higher
3880 * than the current inum. To handle this case, we create the
3881 * parent directory out of order. But we need to check if this
3882 * did already happen before due to other refs in the same dir.
3884 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3887 if (ret == inode_state_will_create) {
3890 * First check if any of the current inodes refs did
3891 * already create the dir.
3893 list_for_each_entry(cur2, &sctx->new_refs, list) {
3896 if (cur2->dir == cur->dir) {
3903 * If that did not happen, check if a previous inode
3904 * did already create the dir.
3907 ret = did_create_dir(sctx, cur->dir);
3911 ret = send_create_inode(sctx, cur->dir);
3918 * Check if this new ref would overwrite the first ref of
3919 * another unprocessed inode. If yes, orphanize the
3920 * overwritten inode. If we find an overwritten ref that is
3921 * not the first ref, simply unlink it.
3923 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3924 cur->name, cur->name_len,
3925 &ow_inode, &ow_gen, &ow_mode);
3929 ret = is_first_ref(sctx->parent_root,
3930 ow_inode, cur->dir, cur->name,
3935 struct name_cache_entry *nce;
3936 struct waiting_dir_move *wdm;
3938 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3942 if (S_ISDIR(ow_mode))
3943 orphanized_dir = true;
3946 * If ow_inode has its rename operation delayed
3947 * make sure that its orphanized name is used in
3948 * the source path when performing its rename
3951 if (is_waiting_for_move(sctx, ow_inode)) {
3952 wdm = get_waiting_dir_move(sctx,
3955 wdm->orphanized = true;
3959 * Make sure we clear our orphanized inode's
3960 * name from the name cache. This is because the
3961 * inode ow_inode might be an ancestor of some
3962 * other inode that will be orphanized as well
3963 * later and has an inode number greater than
3964 * sctx->send_progress. We need to prevent
3965 * future name lookups from using the old name
3966 * and get instead the orphan name.
3968 nce = name_cache_search(sctx, ow_inode, ow_gen);
3970 name_cache_delete(sctx, nce);
3975 * ow_inode might currently be an ancestor of
3976 * cur_ino, therefore compute valid_path (the
3977 * current path of cur_ino) again because it
3978 * might contain the pre-orphanization name of
3979 * ow_inode, which is no longer valid.
3981 ret = is_ancestor(sctx->parent_root,
3983 sctx->cur_ino, NULL);
3985 orphanized_ancestor = true;
3986 fs_path_reset(valid_path);
3987 ret = get_cur_path(sctx, sctx->cur_ino,
3988 sctx->cur_inode_gen,
3994 ret = send_unlink(sctx, cur->full_path);
4000 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4001 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4010 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4012 ret = wait_for_parent_move(sctx, cur, is_orphan);
4022 * link/move the ref to the new place. If we have an orphan
4023 * inode, move it and update valid_path. If not, link or move
4024 * it depending on the inode mode.
4026 if (is_orphan && can_rename) {
4027 ret = send_rename(sctx, valid_path, cur->full_path);
4031 ret = fs_path_copy(valid_path, cur->full_path);
4034 } else if (can_rename) {
4035 if (S_ISDIR(sctx->cur_inode_mode)) {
4037 * Dirs can't be linked, so move it. For moved
4038 * dirs, we always have one new and one deleted
4039 * ref. The deleted ref is ignored later.
4041 ret = send_rename(sctx, valid_path,
4044 ret = fs_path_copy(valid_path,
4050 * We might have previously orphanized an inode
4051 * which is an ancestor of our current inode,
4052 * so our reference's full path, which was
4053 * computed before any such orphanizations, must
4056 if (orphanized_dir) {
4057 ret = update_ref_path(sctx, cur);
4061 ret = send_link(sctx, cur->full_path,
4067 ret = dup_ref(cur, &check_dirs);
4072 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4074 * Check if we can already rmdir the directory. If not,
4075 * orphanize it. For every dir item inside that gets deleted
4076 * later, we do this check again and rmdir it then if possible.
4077 * See the use of check_dirs for more details.
4079 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4084 ret = send_rmdir(sctx, valid_path);
4087 } else if (!is_orphan) {
4088 ret = orphanize_inode(sctx, sctx->cur_ino,
4089 sctx->cur_inode_gen, valid_path);
4095 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4096 ret = dup_ref(cur, &check_dirs);
4100 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4101 !list_empty(&sctx->deleted_refs)) {
4103 * We have a moved dir. Add the old parent to check_dirs
4105 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4107 ret = dup_ref(cur, &check_dirs);
4110 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4112 * We have a non dir inode. Go through all deleted refs and
4113 * unlink them if they were not already overwritten by other
4116 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4117 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4118 sctx->cur_ino, sctx->cur_inode_gen,
4119 cur->name, cur->name_len);
4124 * If we orphanized any ancestor before, we need
4125 * to recompute the full path for deleted names,
4126 * since any such path was computed before we
4127 * processed any references and orphanized any
4130 if (orphanized_ancestor) {
4131 ret = update_ref_path(sctx, cur);
4135 ret = send_unlink(sctx, cur->full_path);
4139 ret = dup_ref(cur, &check_dirs);
4144 * If the inode is still orphan, unlink the orphan. This may
4145 * happen when a previous inode did overwrite the first ref
4146 * of this inode and no new refs were added for the current
4147 * inode. Unlinking does not mean that the inode is deleted in
4148 * all cases. There may still be links to this inode in other
4152 ret = send_unlink(sctx, valid_path);
4159 * We did collect all parent dirs where cur_inode was once located. We
4160 * now go through all these dirs and check if they are pending for
4161 * deletion and if it's finally possible to perform the rmdir now.
4162 * We also update the inode stats of the parent dirs here.
4164 list_for_each_entry(cur, &check_dirs, list) {
4166 * In case we had refs into dirs that were not processed yet,
4167 * we don't need to do the utime and rmdir logic for these dirs.
4168 * The dir will be processed later.
4170 if (cur->dir > sctx->cur_ino)
4173 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4177 if (ret == inode_state_did_create ||
4178 ret == inode_state_no_change) {
4179 /* TODO delayed utimes */
4180 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
4183 } else if (ret == inode_state_did_delete &&
4184 cur->dir != last_dir_ino_rm) {
4185 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4190 ret = get_cur_path(sctx, cur->dir,
4191 cur->dir_gen, valid_path);
4194 ret = send_rmdir(sctx, valid_path);
4197 last_dir_ino_rm = cur->dir;
4205 __free_recorded_refs(&check_dirs);
4206 free_recorded_refs(sctx);
4207 fs_path_free(valid_path);
4211 static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4212 void *ctx, struct list_head *refs)
4215 struct send_ctx *sctx = ctx;
4219 p = fs_path_alloc();
4223 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
4228 ret = get_cur_path(sctx, dir, gen, p);
4231 ret = fs_path_add_path(p, name);
4235 ret = __record_ref(refs, dir, gen, p);
4243 static int __record_new_ref(int num, u64 dir, int index,
4244 struct fs_path *name,
4247 struct send_ctx *sctx = ctx;
4248 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
4252 static int __record_deleted_ref(int num, u64 dir, int index,
4253 struct fs_path *name,
4256 struct send_ctx *sctx = ctx;
4257 return record_ref(sctx->parent_root, dir, name, ctx,
4258 &sctx->deleted_refs);
4261 static int record_new_ref(struct send_ctx *sctx)
4265 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4266 sctx->cmp_key, 0, __record_new_ref, sctx);
4275 static int record_deleted_ref(struct send_ctx *sctx)
4279 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4280 sctx->cmp_key, 0, __record_deleted_ref, sctx);
4289 struct find_ref_ctx {
4292 struct btrfs_root *root;
4293 struct fs_path *name;
4297 static int __find_iref(int num, u64 dir, int index,
4298 struct fs_path *name,
4301 struct find_ref_ctx *ctx = ctx_;
4305 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4306 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
4308 * To avoid doing extra lookups we'll only do this if everything
4311 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4315 if (dir_gen != ctx->dir_gen)
4317 ctx->found_idx = num;
4323 static int find_iref(struct btrfs_root *root,
4324 struct btrfs_path *path,
4325 struct btrfs_key *key,
4326 u64 dir, u64 dir_gen, struct fs_path *name)
4329 struct find_ref_ctx ctx;
4333 ctx.dir_gen = dir_gen;
4337 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
4341 if (ctx.found_idx == -1)
4344 return ctx.found_idx;
4347 static int __record_changed_new_ref(int num, u64 dir, int index,
4348 struct fs_path *name,
4353 struct send_ctx *sctx = ctx;
4355 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4360 ret = find_iref(sctx->parent_root, sctx->right_path,
4361 sctx->cmp_key, dir, dir_gen, name);
4363 ret = __record_new_ref(num, dir, index, name, sctx);
4370 static int __record_changed_deleted_ref(int num, u64 dir, int index,
4371 struct fs_path *name,
4376 struct send_ctx *sctx = ctx;
4378 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4383 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
4384 dir, dir_gen, name);
4386 ret = __record_deleted_ref(num, dir, index, name, sctx);
4393 static int record_changed_ref(struct send_ctx *sctx)
4397 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4398 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4401 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4402 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4412 * Record and process all refs at once. Needed when an inode changes the
4413 * generation number, which means that it was deleted and recreated.
4415 static int process_all_refs(struct send_ctx *sctx,
4416 enum btrfs_compare_tree_result cmd)
4419 struct btrfs_root *root;
4420 struct btrfs_path *path;
4421 struct btrfs_key key;
4422 struct btrfs_key found_key;
4423 struct extent_buffer *eb;
4425 iterate_inode_ref_t cb;
4426 int pending_move = 0;
4428 path = alloc_path_for_send();
4432 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4433 root = sctx->send_root;
4434 cb = __record_new_ref;
4435 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4436 root = sctx->parent_root;
4437 cb = __record_deleted_ref;
4439 btrfs_err(sctx->send_root->fs_info,
4440 "Wrong command %d in process_all_refs", cmd);
4445 key.objectid = sctx->cmp_key->objectid;
4446 key.type = BTRFS_INODE_REF_KEY;
4448 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4453 eb = path->nodes[0];
4454 slot = path->slots[0];
4455 if (slot >= btrfs_header_nritems(eb)) {
4456 ret = btrfs_next_leaf(root, path);
4464 btrfs_item_key_to_cpu(eb, &found_key, slot);
4466 if (found_key.objectid != key.objectid ||
4467 (found_key.type != BTRFS_INODE_REF_KEY &&
4468 found_key.type != BTRFS_INODE_EXTREF_KEY))
4471 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4477 btrfs_release_path(path);
4480 * We don't actually care about pending_move as we are simply
4481 * re-creating this inode and will be rename'ing it into place once we
4482 * rename the parent directory.
4484 ret = process_recorded_refs(sctx, &pending_move);
4486 btrfs_free_path(path);
4490 static int send_set_xattr(struct send_ctx *sctx,
4491 struct fs_path *path,
4492 const char *name, int name_len,
4493 const char *data, int data_len)
4497 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4501 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4502 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4503 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4505 ret = send_cmd(sctx);
4512 static int send_remove_xattr(struct send_ctx *sctx,
4513 struct fs_path *path,
4514 const char *name, int name_len)
4518 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4522 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4523 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4525 ret = send_cmd(sctx);
4532 static int __process_new_xattr(int num, struct btrfs_key *di_key,
4533 const char *name, int name_len,
4534 const char *data, int data_len,
4538 struct send_ctx *sctx = ctx;
4540 struct posix_acl_xattr_header dummy_acl;
4542 /* Capabilities are emitted by finish_inode_if_needed */
4543 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4546 p = fs_path_alloc();
4551 * This hack is needed because empty acls are stored as zero byte
4552 * data in xattrs. Problem with that is, that receiving these zero byte
4553 * acls will fail later. To fix this, we send a dummy acl list that
4554 * only contains the version number and no entries.
4556 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4557 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4558 if (data_len == 0) {
4559 dummy_acl.a_version =
4560 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4561 data = (char *)&dummy_acl;
4562 data_len = sizeof(dummy_acl);
4566 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4570 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4577 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4578 const char *name, int name_len,
4579 const char *data, int data_len,
4583 struct send_ctx *sctx = ctx;
4586 p = fs_path_alloc();
4590 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4594 ret = send_remove_xattr(sctx, p, name, name_len);
4601 static int process_new_xattr(struct send_ctx *sctx)
4605 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4606 __process_new_xattr, sctx);
4611 static int process_deleted_xattr(struct send_ctx *sctx)
4613 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4614 __process_deleted_xattr, sctx);
4617 struct find_xattr_ctx {
4625 static int __find_xattr(int num, struct btrfs_key *di_key,
4626 const char *name, int name_len,
4627 const char *data, int data_len,
4628 u8 type, void *vctx)
4630 struct find_xattr_ctx *ctx = vctx;
4632 if (name_len == ctx->name_len &&
4633 strncmp(name, ctx->name, name_len) == 0) {
4634 ctx->found_idx = num;
4635 ctx->found_data_len = data_len;
4636 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4637 if (!ctx->found_data)
4644 static int find_xattr(struct btrfs_root *root,
4645 struct btrfs_path *path,
4646 struct btrfs_key *key,
4647 const char *name, int name_len,
4648 char **data, int *data_len)
4651 struct find_xattr_ctx ctx;
4654 ctx.name_len = name_len;
4656 ctx.found_data = NULL;
4657 ctx.found_data_len = 0;
4659 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
4663 if (ctx.found_idx == -1)
4666 *data = ctx.found_data;
4667 *data_len = ctx.found_data_len;
4669 kfree(ctx.found_data);
4671 return ctx.found_idx;
4675 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4676 const char *name, int name_len,
4677 const char *data, int data_len,
4681 struct send_ctx *sctx = ctx;
4682 char *found_data = NULL;
4683 int found_data_len = 0;
4685 ret = find_xattr(sctx->parent_root, sctx->right_path,
4686 sctx->cmp_key, name, name_len, &found_data,
4688 if (ret == -ENOENT) {
4689 ret = __process_new_xattr(num, di_key, name, name_len, data,
4690 data_len, type, ctx);
4691 } else if (ret >= 0) {
4692 if (data_len != found_data_len ||
4693 memcmp(data, found_data, data_len)) {
4694 ret = __process_new_xattr(num, di_key, name, name_len,
4695 data, data_len, type, ctx);
4705 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4706 const char *name, int name_len,
4707 const char *data, int data_len,
4711 struct send_ctx *sctx = ctx;
4713 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4714 name, name_len, NULL, NULL);
4716 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4717 data_len, type, ctx);
4724 static int process_changed_xattr(struct send_ctx *sctx)
4728 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4729 __process_changed_new_xattr, sctx);
4732 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4733 __process_changed_deleted_xattr, sctx);
4739 static int process_all_new_xattrs(struct send_ctx *sctx)
4742 struct btrfs_root *root;
4743 struct btrfs_path *path;
4744 struct btrfs_key key;
4745 struct btrfs_key found_key;
4746 struct extent_buffer *eb;
4749 path = alloc_path_for_send();
4753 root = sctx->send_root;
4755 key.objectid = sctx->cmp_key->objectid;
4756 key.type = BTRFS_XATTR_ITEM_KEY;
4758 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4763 eb = path->nodes[0];
4764 slot = path->slots[0];
4765 if (slot >= btrfs_header_nritems(eb)) {
4766 ret = btrfs_next_leaf(root, path);
4769 } else if (ret > 0) {
4776 btrfs_item_key_to_cpu(eb, &found_key, slot);
4777 if (found_key.objectid != key.objectid ||
4778 found_key.type != key.type) {
4783 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
4791 btrfs_free_path(path);
4795 static inline u64 max_send_read_size(const struct send_ctx *sctx)
4797 return sctx->send_max_size - SZ_16K;
4800 static int put_data_header(struct send_ctx *sctx, u32 len)
4802 struct btrfs_tlv_header *hdr;
4804 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
4806 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
4807 put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
4808 put_unaligned_le16(len, &hdr->tlv_len);
4809 sctx->send_size += sizeof(*hdr);
4813 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
4815 struct btrfs_root *root = sctx->send_root;
4816 struct btrfs_fs_info *fs_info = root->fs_info;
4817 struct inode *inode;
4820 pgoff_t index = offset >> PAGE_SHIFT;
4822 unsigned pg_offset = offset_in_page(offset);
4825 ret = put_data_header(sctx, len);
4829 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
4831 return PTR_ERR(inode);
4833 last_index = (offset + len - 1) >> PAGE_SHIFT;
4835 /* initial readahead */
4836 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4837 file_ra_state_init(&sctx->ra, inode->i_mapping);
4839 while (index <= last_index) {
4840 unsigned cur_len = min_t(unsigned, len,
4841 PAGE_SIZE - pg_offset);
4843 page = find_lock_page(inode->i_mapping, index);
4845 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4846 NULL, index, last_index + 1 - index);
4848 page = find_or_create_page(inode->i_mapping, index,
4856 if (PageReadahead(page)) {
4857 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4858 NULL, page, index, last_index + 1 - index);
4861 if (!PageUptodate(page)) {
4862 btrfs_readpage(NULL, page);
4864 if (!PageUptodate(page)) {
4873 memcpy(sctx->send_buf + sctx->send_size, addr + pg_offset,
4881 sctx->send_size += cur_len;
4888 * Read some bytes from the current inode/file and send a write command to
4891 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4893 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
4897 p = fs_path_alloc();
4901 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
4903 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4907 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4911 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4912 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4913 ret = put_file_data(sctx, offset, len);
4917 ret = send_cmd(sctx);
4926 * Send a clone command to user space.
4928 static int send_clone(struct send_ctx *sctx,
4929 u64 offset, u32 len,
4930 struct clone_root *clone_root)
4936 btrfs_debug(sctx->send_root->fs_info,
4937 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4938 offset, len, clone_root->root->root_key.objectid,
4939 clone_root->ino, clone_root->offset);
4941 p = fs_path_alloc();
4945 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4949 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4953 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4954 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4955 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4957 if (clone_root->root == sctx->send_root) {
4958 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
4959 &gen, NULL, NULL, NULL, NULL);
4962 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4964 ret = get_inode_path(clone_root->root, clone_root->ino, p);
4970 * If the parent we're using has a received_uuid set then use that as
4971 * our clone source as that is what we will look for when doing a
4974 * This covers the case that we create a snapshot off of a received
4975 * subvolume and then use that as the parent and try to receive on a
4978 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4979 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4980 clone_root->root->root_item.received_uuid);
4982 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4983 clone_root->root->root_item.uuid);
4984 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
4985 le64_to_cpu(clone_root->root->root_item.ctransid));
4986 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4987 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4988 clone_root->offset);
4990 ret = send_cmd(sctx);
4999 * Send an update extent command to user space.
5001 static int send_update_extent(struct send_ctx *sctx,
5002 u64 offset, u32 len)
5007 p = fs_path_alloc();
5011 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5015 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5019 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5020 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5021 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5023 ret = send_cmd(sctx);
5031 static int send_hole(struct send_ctx *sctx, u64 end)
5033 struct fs_path *p = NULL;
5034 u64 read_size = max_send_read_size(sctx);
5035 u64 offset = sctx->cur_inode_last_extent;
5039 * A hole that starts at EOF or beyond it. Since we do not yet support
5040 * fallocate (for extent preallocation and hole punching), sending a
5041 * write of zeroes starting at EOF or beyond would later require issuing
5042 * a truncate operation which would undo the write and achieve nothing.
5044 if (offset >= sctx->cur_inode_size)
5048 * Don't go beyond the inode's i_size due to prealloc extents that start
5051 end = min_t(u64, end, sctx->cur_inode_size);
5053 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5054 return send_update_extent(sctx, offset, end - offset);
5056 p = fs_path_alloc();
5059 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5061 goto tlv_put_failure;
5062 while (offset < end) {
5063 u64 len = min(end - offset, read_size);
5065 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5068 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5069 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5070 ret = put_data_header(sctx, len);
5073 memset(sctx->send_buf + sctx->send_size, 0, len);
5074 sctx->send_size += len;
5075 ret = send_cmd(sctx);
5080 sctx->cur_inode_next_write_offset = offset;
5086 static int send_extent_data(struct send_ctx *sctx,
5090 u64 read_size = max_send_read_size(sctx);
5093 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5094 return send_update_extent(sctx, offset, len);
5096 while (sent < len) {
5097 u64 size = min(len - sent, read_size);
5100 ret = send_write(sctx, offset + sent, size);
5109 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5110 * found, call send_set_xattr function to emit it.
5112 * Return 0 if there isn't a capability, or when the capability was emitted
5113 * successfully, or < 0 if an error occurred.
5115 static int send_capabilities(struct send_ctx *sctx)
5117 struct fs_path *fspath = NULL;
5118 struct btrfs_path *path;
5119 struct btrfs_dir_item *di;
5120 struct extent_buffer *leaf;
5121 unsigned long data_ptr;
5126 path = alloc_path_for_send();
5130 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5131 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5133 /* There is no xattr for this inode */
5135 } else if (IS_ERR(di)) {
5140 leaf = path->nodes[0];
5141 buf_len = btrfs_dir_data_len(leaf, di);
5143 fspath = fs_path_alloc();
5144 buf = kmalloc(buf_len, GFP_KERNEL);
5145 if (!fspath || !buf) {
5150 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5154 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5155 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5157 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5158 strlen(XATTR_NAME_CAPS), buf, buf_len);
5161 fs_path_free(fspath);
5162 btrfs_free_path(path);
5166 static int clone_range(struct send_ctx *sctx,
5167 struct clone_root *clone_root,
5168 const u64 disk_byte,
5173 struct btrfs_path *path;
5174 struct btrfs_key key;
5176 u64 clone_src_i_size = 0;
5179 * Prevent cloning from a zero offset with a length matching the sector
5180 * size because in some scenarios this will make the receiver fail.
5182 * For example, if in the source filesystem the extent at offset 0
5183 * has a length of sectorsize and it was written using direct IO, then
5184 * it can never be an inline extent (even if compression is enabled).
5185 * Then this extent can be cloned in the original filesystem to a non
5186 * zero file offset, but it may not be possible to clone in the
5187 * destination filesystem because it can be inlined due to compression
5188 * on the destination filesystem (as the receiver's write operations are
5189 * always done using buffered IO). The same happens when the original
5190 * filesystem does not have compression enabled but the destination
5193 if (clone_root->offset == 0 &&
5194 len == sctx->send_root->fs_info->sectorsize)
5195 return send_extent_data(sctx, offset, len);
5197 path = alloc_path_for_send();
5202 * There are inodes that have extents that lie behind its i_size. Don't
5203 * accept clones from these extents.
5205 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5206 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5207 btrfs_release_path(path);
5212 * We can't send a clone operation for the entire range if we find
5213 * extent items in the respective range in the source file that
5214 * refer to different extents or if we find holes.
5215 * So check for that and do a mix of clone and regular write/copy
5216 * operations if needed.
5220 * mkfs.btrfs -f /dev/sda
5221 * mount /dev/sda /mnt
5222 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5223 * cp --reflink=always /mnt/foo /mnt/bar
5224 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5225 * btrfs subvolume snapshot -r /mnt /mnt/snap
5227 * If when we send the snapshot and we are processing file bar (which
5228 * has a higher inode number than foo) we blindly send a clone operation
5229 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5230 * a file bar that matches the content of file foo - iow, doesn't match
5231 * the content from bar in the original filesystem.
5233 key.objectid = clone_root->ino;
5234 key.type = BTRFS_EXTENT_DATA_KEY;
5235 key.offset = clone_root->offset;
5236 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5239 if (ret > 0 && path->slots[0] > 0) {
5240 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5241 if (key.objectid == clone_root->ino &&
5242 key.type == BTRFS_EXTENT_DATA_KEY)
5247 struct extent_buffer *leaf = path->nodes[0];
5248 int slot = path->slots[0];
5249 struct btrfs_file_extent_item *ei;
5253 u64 clone_data_offset;
5255 if (slot >= btrfs_header_nritems(leaf)) {
5256 ret = btrfs_next_leaf(clone_root->root, path);
5264 btrfs_item_key_to_cpu(leaf, &key, slot);
5267 * We might have an implicit trailing hole (NO_HOLES feature
5268 * enabled). We deal with it after leaving this loop.
5270 if (key.objectid != clone_root->ino ||
5271 key.type != BTRFS_EXTENT_DATA_KEY)
5274 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5275 type = btrfs_file_extent_type(leaf, ei);
5276 if (type == BTRFS_FILE_EXTENT_INLINE) {
5277 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
5278 ext_len = PAGE_ALIGN(ext_len);
5280 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5283 if (key.offset + ext_len <= clone_root->offset)
5286 if (key.offset > clone_root->offset) {
5287 /* Implicit hole, NO_HOLES feature enabled. */
5288 u64 hole_len = key.offset - clone_root->offset;
5292 ret = send_extent_data(sctx, offset, hole_len);
5300 clone_root->offset += hole_len;
5301 data_offset += hole_len;
5304 if (key.offset >= clone_root->offset + len)
5307 if (key.offset >= clone_src_i_size)
5310 if (key.offset + ext_len > clone_src_i_size)
5311 ext_len = clone_src_i_size - key.offset;
5313 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5314 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5315 clone_root->offset = key.offset;
5316 if (clone_data_offset < data_offset &&
5317 clone_data_offset + ext_len > data_offset) {
5320 extent_offset = data_offset - clone_data_offset;
5321 ext_len -= extent_offset;
5322 clone_data_offset += extent_offset;
5323 clone_root->offset += extent_offset;
5327 clone_len = min_t(u64, ext_len, len);
5329 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5330 clone_data_offset == data_offset) {
5331 const u64 src_end = clone_root->offset + clone_len;
5332 const u64 sectorsize = SZ_64K;
5335 * We can't clone the last block, when its size is not
5336 * sector size aligned, into the middle of a file. If we
5337 * do so, the receiver will get a failure (-EINVAL) when
5338 * trying to clone or will silently corrupt the data in
5339 * the destination file if it's on a kernel without the
5340 * fix introduced by commit ac765f83f1397646
5341 * ("Btrfs: fix data corruption due to cloning of eof
5344 * So issue a clone of the aligned down range plus a
5345 * regular write for the eof block, if we hit that case.
5347 * Also, we use the maximum possible sector size, 64K,
5348 * because we don't know what's the sector size of the
5349 * filesystem that receives the stream, so we have to
5350 * assume the largest possible sector size.
5352 if (src_end == clone_src_i_size &&
5353 !IS_ALIGNED(src_end, sectorsize) &&
5354 offset + clone_len < sctx->cur_inode_size) {
5357 slen = ALIGN_DOWN(src_end - clone_root->offset,
5360 ret = send_clone(sctx, offset, slen,
5365 ret = send_extent_data(sctx, offset + slen,
5368 ret = send_clone(sctx, offset, clone_len,
5372 ret = send_extent_data(sctx, offset, clone_len);
5381 offset += clone_len;
5382 clone_root->offset += clone_len;
5383 data_offset += clone_len;
5389 ret = send_extent_data(sctx, offset, len);
5393 btrfs_free_path(path);
5397 static int send_write_or_clone(struct send_ctx *sctx,
5398 struct btrfs_path *path,
5399 struct btrfs_key *key,
5400 struct clone_root *clone_root)
5403 struct btrfs_file_extent_item *ei;
5404 u64 offset = key->offset;
5407 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
5409 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5410 struct btrfs_file_extent_item);
5411 type = btrfs_file_extent_type(path->nodes[0], ei);
5412 if (type == BTRFS_FILE_EXTENT_INLINE) {
5413 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
5415 * it is possible the inline item won't cover the whole page,
5416 * but there may be items after this page. Make
5417 * sure to send the whole thing
5419 len = PAGE_ALIGN(len);
5421 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
5424 if (offset >= sctx->cur_inode_size) {
5428 if (offset + len > sctx->cur_inode_size)
5429 len = sctx->cur_inode_size - offset;
5435 if (clone_root && IS_ALIGNED(offset + len, bs)) {
5439 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5440 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5441 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5444 ret = send_extent_data(sctx, offset, len);
5446 sctx->cur_inode_next_write_offset = offset + len;
5451 static int is_extent_unchanged(struct send_ctx *sctx,
5452 struct btrfs_path *left_path,
5453 struct btrfs_key *ekey)
5456 struct btrfs_key key;
5457 struct btrfs_path *path = NULL;
5458 struct extent_buffer *eb;
5460 struct btrfs_key found_key;
5461 struct btrfs_file_extent_item *ei;
5466 u64 left_offset_fixed;
5474 path = alloc_path_for_send();
5478 eb = left_path->nodes[0];
5479 slot = left_path->slots[0];
5480 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5481 left_type = btrfs_file_extent_type(eb, ei);
5483 if (left_type != BTRFS_FILE_EXTENT_REG) {
5487 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5488 left_len = btrfs_file_extent_num_bytes(eb, ei);
5489 left_offset = btrfs_file_extent_offset(eb, ei);
5490 left_gen = btrfs_file_extent_generation(eb, ei);
5493 * Following comments will refer to these graphics. L is the left
5494 * extents which we are checking at the moment. 1-8 are the right
5495 * extents that we iterate.
5498 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5501 * |--1--|-2b-|...(same as above)
5503 * Alternative situation. Happens on files where extents got split.
5505 * |-----------7-----------|-6-|
5507 * Alternative situation. Happens on files which got larger.
5510 * Nothing follows after 8.
5513 key.objectid = ekey->objectid;
5514 key.type = BTRFS_EXTENT_DATA_KEY;
5515 key.offset = ekey->offset;
5516 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5525 * Handle special case where the right side has no extents at all.
5527 eb = path->nodes[0];
5528 slot = path->slots[0];
5529 btrfs_item_key_to_cpu(eb, &found_key, slot);
5530 if (found_key.objectid != key.objectid ||
5531 found_key.type != key.type) {
5532 /* If we're a hole then just pretend nothing changed */
5533 ret = (left_disknr) ? 0 : 1;
5538 * We're now on 2a, 2b or 7.
5541 while (key.offset < ekey->offset + left_len) {
5542 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5543 right_type = btrfs_file_extent_type(eb, ei);
5544 if (right_type != BTRFS_FILE_EXTENT_REG &&
5545 right_type != BTRFS_FILE_EXTENT_INLINE) {
5550 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5551 right_len = btrfs_file_extent_ram_bytes(eb, ei);
5552 right_len = PAGE_ALIGN(right_len);
5554 right_len = btrfs_file_extent_num_bytes(eb, ei);
5558 * Are we at extent 8? If yes, we know the extent is changed.
5559 * This may only happen on the first iteration.
5561 if (found_key.offset + right_len <= ekey->offset) {
5562 /* If we're a hole just pretend nothing changed */
5563 ret = (left_disknr) ? 0 : 1;
5568 * We just wanted to see if when we have an inline extent, what
5569 * follows it is a regular extent (wanted to check the above
5570 * condition for inline extents too). This should normally not
5571 * happen but it's possible for example when we have an inline
5572 * compressed extent representing data with a size matching
5573 * the page size (currently the same as sector size).
5575 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5580 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5581 right_offset = btrfs_file_extent_offset(eb, ei);
5582 right_gen = btrfs_file_extent_generation(eb, ei);
5584 left_offset_fixed = left_offset;
5585 if (key.offset < ekey->offset) {
5586 /* Fix the right offset for 2a and 7. */
5587 right_offset += ekey->offset - key.offset;
5589 /* Fix the left offset for all behind 2a and 2b */
5590 left_offset_fixed += key.offset - ekey->offset;
5594 * Check if we have the same extent.
5596 if (left_disknr != right_disknr ||
5597 left_offset_fixed != right_offset ||
5598 left_gen != right_gen) {
5604 * Go to the next extent.
5606 ret = btrfs_next_item(sctx->parent_root, path);
5610 eb = path->nodes[0];
5611 slot = path->slots[0];
5612 btrfs_item_key_to_cpu(eb, &found_key, slot);
5614 if (ret || found_key.objectid != key.objectid ||
5615 found_key.type != key.type) {
5616 key.offset += right_len;
5619 if (found_key.offset != key.offset + right_len) {
5627 * We're now behind the left extent (treat as unchanged) or at the end
5628 * of the right side (treat as changed).
5630 if (key.offset >= ekey->offset + left_len)
5637 btrfs_free_path(path);
5641 static int get_last_extent(struct send_ctx *sctx, u64 offset)
5643 struct btrfs_path *path;
5644 struct btrfs_root *root = sctx->send_root;
5645 struct btrfs_key key;
5648 path = alloc_path_for_send();
5652 sctx->cur_inode_last_extent = 0;
5654 key.objectid = sctx->cur_ino;
5655 key.type = BTRFS_EXTENT_DATA_KEY;
5656 key.offset = offset;
5657 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5661 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5662 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5665 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
5667 btrfs_free_path(path);
5671 static int range_is_hole_in_parent(struct send_ctx *sctx,
5675 struct btrfs_path *path;
5676 struct btrfs_key key;
5677 struct btrfs_root *root = sctx->parent_root;
5678 u64 search_start = start;
5681 path = alloc_path_for_send();
5685 key.objectid = sctx->cur_ino;
5686 key.type = BTRFS_EXTENT_DATA_KEY;
5687 key.offset = search_start;
5688 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5691 if (ret > 0 && path->slots[0] > 0)
5694 while (search_start < end) {
5695 struct extent_buffer *leaf = path->nodes[0];
5696 int slot = path->slots[0];
5697 struct btrfs_file_extent_item *fi;
5700 if (slot >= btrfs_header_nritems(leaf)) {
5701 ret = btrfs_next_leaf(root, path);
5709 btrfs_item_key_to_cpu(leaf, &key, slot);
5710 if (key.objectid < sctx->cur_ino ||
5711 key.type < BTRFS_EXTENT_DATA_KEY)
5713 if (key.objectid > sctx->cur_ino ||
5714 key.type > BTRFS_EXTENT_DATA_KEY ||
5718 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5719 extent_end = btrfs_file_extent_end(path);
5720 if (extent_end <= start)
5722 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5723 search_start = extent_end;
5733 btrfs_free_path(path);
5737 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5738 struct btrfs_key *key)
5742 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5745 if (sctx->cur_inode_last_extent == (u64)-1) {
5746 ret = get_last_extent(sctx, key->offset - 1);
5751 if (path->slots[0] == 0 &&
5752 sctx->cur_inode_last_extent < key->offset) {
5754 * We might have skipped entire leafs that contained only
5755 * file extent items for our current inode. These leafs have
5756 * a generation number smaller (older) than the one in the
5757 * current leaf and the leaf our last extent came from, and
5758 * are located between these 2 leafs.
5760 ret = get_last_extent(sctx, key->offset - 1);
5765 if (sctx->cur_inode_last_extent < key->offset) {
5766 ret = range_is_hole_in_parent(sctx,
5767 sctx->cur_inode_last_extent,
5772 ret = send_hole(sctx, key->offset);
5776 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
5780 static int process_extent(struct send_ctx *sctx,
5781 struct btrfs_path *path,
5782 struct btrfs_key *key)
5784 struct clone_root *found_clone = NULL;
5787 if (S_ISLNK(sctx->cur_inode_mode))
5790 if (sctx->parent_root && !sctx->cur_inode_new) {
5791 ret = is_extent_unchanged(sctx, path, key);
5799 struct btrfs_file_extent_item *ei;
5802 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5803 struct btrfs_file_extent_item);
5804 type = btrfs_file_extent_type(path->nodes[0], ei);
5805 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5806 type == BTRFS_FILE_EXTENT_REG) {
5808 * The send spec does not have a prealloc command yet,
5809 * so just leave a hole for prealloc'ed extents until
5810 * we have enough commands queued up to justify rev'ing
5813 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5818 /* Have a hole, just skip it. */
5819 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5826 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5827 sctx->cur_inode_size, &found_clone);
5828 if (ret != -ENOENT && ret < 0)
5831 ret = send_write_or_clone(sctx, path, key, found_clone);
5835 ret = maybe_send_hole(sctx, path, key);
5840 static int process_all_extents(struct send_ctx *sctx)
5843 struct btrfs_root *root;
5844 struct btrfs_path *path;
5845 struct btrfs_key key;
5846 struct btrfs_key found_key;
5847 struct extent_buffer *eb;
5850 root = sctx->send_root;
5851 path = alloc_path_for_send();
5855 key.objectid = sctx->cmp_key->objectid;
5856 key.type = BTRFS_EXTENT_DATA_KEY;
5858 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5863 eb = path->nodes[0];
5864 slot = path->slots[0];
5866 if (slot >= btrfs_header_nritems(eb)) {
5867 ret = btrfs_next_leaf(root, path);
5870 } else if (ret > 0) {
5877 btrfs_item_key_to_cpu(eb, &found_key, slot);
5879 if (found_key.objectid != key.objectid ||
5880 found_key.type != key.type) {
5885 ret = process_extent(sctx, path, &found_key);
5893 btrfs_free_path(path);
5897 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5899 int *refs_processed)
5903 if (sctx->cur_ino == 0)
5905 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
5906 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
5908 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5911 ret = process_recorded_refs(sctx, pending_move);
5915 *refs_processed = 1;
5920 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5931 int need_truncate = 1;
5932 int pending_move = 0;
5933 int refs_processed = 0;
5935 if (sctx->ignore_cur_inode)
5938 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5944 * We have processed the refs and thus need to advance send_progress.
5945 * Now, calls to get_cur_xxx will take the updated refs of the current
5946 * inode into account.
5948 * On the other hand, if our current inode is a directory and couldn't
5949 * be moved/renamed because its parent was renamed/moved too and it has
5950 * a higher inode number, we can only move/rename our current inode
5951 * after we moved/renamed its parent. Therefore in this case operate on
5952 * the old path (pre move/rename) of our current inode, and the
5953 * move/rename will be performed later.
5955 if (refs_processed && !pending_move)
5956 sctx->send_progress = sctx->cur_ino + 1;
5958 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5960 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5963 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
5964 &left_mode, &left_uid, &left_gid, NULL);
5968 if (!sctx->parent_root || sctx->cur_inode_new) {
5970 if (!S_ISLNK(sctx->cur_inode_mode))
5972 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
5977 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5978 &old_size, NULL, &right_mode, &right_uid,
5983 if (left_uid != right_uid || left_gid != right_gid)
5985 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5987 if ((old_size == sctx->cur_inode_size) ||
5988 (sctx->cur_inode_size > old_size &&
5989 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
5993 if (S_ISREG(sctx->cur_inode_mode)) {
5994 if (need_send_hole(sctx)) {
5995 if (sctx->cur_inode_last_extent == (u64)-1 ||
5996 sctx->cur_inode_last_extent <
5997 sctx->cur_inode_size) {
5998 ret = get_last_extent(sctx, (u64)-1);
6002 if (sctx->cur_inode_last_extent <
6003 sctx->cur_inode_size) {
6004 ret = send_hole(sctx, sctx->cur_inode_size);
6009 if (need_truncate) {
6010 ret = send_truncate(sctx, sctx->cur_ino,
6011 sctx->cur_inode_gen,
6012 sctx->cur_inode_size);
6019 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6020 left_uid, left_gid);
6025 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6031 ret = send_capabilities(sctx);
6036 * If other directory inodes depended on our current directory
6037 * inode's move/rename, now do their move/rename operations.
6039 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6040 ret = apply_children_dir_moves(sctx);
6044 * Need to send that every time, no matter if it actually
6045 * changed between the two trees as we have done changes to
6046 * the inode before. If our inode is a directory and it's
6047 * waiting to be moved/renamed, we will send its utimes when
6048 * it's moved/renamed, therefore we don't need to do it here.
6050 sctx->send_progress = sctx->cur_ino + 1;
6051 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6060 struct parent_paths_ctx {
6061 struct list_head *refs;
6062 struct send_ctx *sctx;
6065 static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6068 struct parent_paths_ctx *ppctx = ctx;
6070 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6075 * Issue unlink operations for all paths of the current inode found in the
6078 static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6080 LIST_HEAD(deleted_refs);
6081 struct btrfs_path *path;
6082 struct btrfs_key key;
6083 struct parent_paths_ctx ctx;
6086 path = alloc_path_for_send();
6090 key.objectid = sctx->cur_ino;
6091 key.type = BTRFS_INODE_REF_KEY;
6093 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
6097 ctx.refs = &deleted_refs;
6101 struct extent_buffer *eb = path->nodes[0];
6102 int slot = path->slots[0];
6104 if (slot >= btrfs_header_nritems(eb)) {
6105 ret = btrfs_next_leaf(sctx->parent_root, path);
6113 btrfs_item_key_to_cpu(eb, &key, slot);
6114 if (key.objectid != sctx->cur_ino)
6116 if (key.type != BTRFS_INODE_REF_KEY &&
6117 key.type != BTRFS_INODE_EXTREF_KEY)
6120 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
6121 record_parent_ref, &ctx);
6128 while (!list_empty(&deleted_refs)) {
6129 struct recorded_ref *ref;
6131 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6132 ret = send_unlink(sctx, ref->full_path);
6135 fs_path_free(ref->full_path);
6136 list_del(&ref->list);
6141 btrfs_free_path(path);
6143 __free_recorded_refs(&deleted_refs);
6147 static int changed_inode(struct send_ctx *sctx,
6148 enum btrfs_compare_tree_result result)
6151 struct btrfs_key *key = sctx->cmp_key;
6152 struct btrfs_inode_item *left_ii = NULL;
6153 struct btrfs_inode_item *right_ii = NULL;
6157 sctx->cur_ino = key->objectid;
6158 sctx->cur_inode_new_gen = 0;
6159 sctx->cur_inode_last_extent = (u64)-1;
6160 sctx->cur_inode_next_write_offset = 0;
6161 sctx->ignore_cur_inode = false;
6164 * Set send_progress to current inode. This will tell all get_cur_xxx
6165 * functions that the current inode's refs are not updated yet. Later,
6166 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6168 sctx->send_progress = sctx->cur_ino;
6170 if (result == BTRFS_COMPARE_TREE_NEW ||
6171 result == BTRFS_COMPARE_TREE_CHANGED) {
6172 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6173 sctx->left_path->slots[0],
6174 struct btrfs_inode_item);
6175 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6178 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6179 sctx->right_path->slots[0],
6180 struct btrfs_inode_item);
6181 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6184 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6185 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6186 sctx->right_path->slots[0],
6187 struct btrfs_inode_item);
6189 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6193 * The cur_ino = root dir case is special here. We can't treat
6194 * the inode as deleted+reused because it would generate a
6195 * stream that tries to delete/mkdir the root dir.
6197 if (left_gen != right_gen &&
6198 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6199 sctx->cur_inode_new_gen = 1;
6203 * Normally we do not find inodes with a link count of zero (orphans)
6204 * because the most common case is to create a snapshot and use it
6205 * for a send operation. However other less common use cases involve
6206 * using a subvolume and send it after turning it to RO mode just
6207 * after deleting all hard links of a file while holding an open
6208 * file descriptor against it or turning a RO snapshot into RW mode,
6209 * keep an open file descriptor against a file, delete it and then
6210 * turn the snapshot back to RO mode before using it for a send
6211 * operation. So if we find such cases, ignore the inode and all its
6212 * items completely if it's a new inode, or if it's a changed inode
6213 * make sure all its previous paths (from the parent snapshot) are all
6214 * unlinked and all other the inode items are ignored.
6216 if (result == BTRFS_COMPARE_TREE_NEW ||
6217 result == BTRFS_COMPARE_TREE_CHANGED) {
6220 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6222 sctx->ignore_cur_inode = true;
6223 if (result == BTRFS_COMPARE_TREE_CHANGED)
6224 ret = btrfs_unlink_all_paths(sctx);
6229 if (result == BTRFS_COMPARE_TREE_NEW) {
6230 sctx->cur_inode_gen = left_gen;
6231 sctx->cur_inode_new = 1;
6232 sctx->cur_inode_deleted = 0;
6233 sctx->cur_inode_size = btrfs_inode_size(
6234 sctx->left_path->nodes[0], left_ii);
6235 sctx->cur_inode_mode = btrfs_inode_mode(
6236 sctx->left_path->nodes[0], left_ii);
6237 sctx->cur_inode_rdev = btrfs_inode_rdev(
6238 sctx->left_path->nodes[0], left_ii);
6239 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6240 ret = send_create_inode_if_needed(sctx);
6241 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6242 sctx->cur_inode_gen = right_gen;
6243 sctx->cur_inode_new = 0;
6244 sctx->cur_inode_deleted = 1;
6245 sctx->cur_inode_size = btrfs_inode_size(
6246 sctx->right_path->nodes[0], right_ii);
6247 sctx->cur_inode_mode = btrfs_inode_mode(
6248 sctx->right_path->nodes[0], right_ii);
6249 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
6251 * We need to do some special handling in case the inode was
6252 * reported as changed with a changed generation number. This
6253 * means that the original inode was deleted and new inode
6254 * reused the same inum. So we have to treat the old inode as
6255 * deleted and the new one as new.
6257 if (sctx->cur_inode_new_gen) {
6259 * First, process the inode as if it was deleted.
6261 sctx->cur_inode_gen = right_gen;
6262 sctx->cur_inode_new = 0;
6263 sctx->cur_inode_deleted = 1;
6264 sctx->cur_inode_size = btrfs_inode_size(
6265 sctx->right_path->nodes[0], right_ii);
6266 sctx->cur_inode_mode = btrfs_inode_mode(
6267 sctx->right_path->nodes[0], right_ii);
6268 ret = process_all_refs(sctx,
6269 BTRFS_COMPARE_TREE_DELETED);
6274 * Now process the inode as if it was new.
6276 sctx->cur_inode_gen = left_gen;
6277 sctx->cur_inode_new = 1;
6278 sctx->cur_inode_deleted = 0;
6279 sctx->cur_inode_size = btrfs_inode_size(
6280 sctx->left_path->nodes[0], left_ii);
6281 sctx->cur_inode_mode = btrfs_inode_mode(
6282 sctx->left_path->nodes[0], left_ii);
6283 sctx->cur_inode_rdev = btrfs_inode_rdev(
6284 sctx->left_path->nodes[0], left_ii);
6285 ret = send_create_inode_if_needed(sctx);
6289 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6293 * Advance send_progress now as we did not get into
6294 * process_recorded_refs_if_needed in the new_gen case.
6296 sctx->send_progress = sctx->cur_ino + 1;
6299 * Now process all extents and xattrs of the inode as if
6300 * they were all new.
6302 ret = process_all_extents(sctx);
6305 ret = process_all_new_xattrs(sctx);
6309 sctx->cur_inode_gen = left_gen;
6310 sctx->cur_inode_new = 0;
6311 sctx->cur_inode_new_gen = 0;
6312 sctx->cur_inode_deleted = 0;
6313 sctx->cur_inode_size = btrfs_inode_size(
6314 sctx->left_path->nodes[0], left_ii);
6315 sctx->cur_inode_mode = btrfs_inode_mode(
6316 sctx->left_path->nodes[0], left_ii);
6325 * We have to process new refs before deleted refs, but compare_trees gives us
6326 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6327 * first and later process them in process_recorded_refs.
6328 * For the cur_inode_new_gen case, we skip recording completely because
6329 * changed_inode did already initiate processing of refs. The reason for this is
6330 * that in this case, compare_tree actually compares the refs of 2 different
6331 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6332 * refs of the right tree as deleted and all refs of the left tree as new.
6334 static int changed_ref(struct send_ctx *sctx,
6335 enum btrfs_compare_tree_result result)
6339 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6340 inconsistent_snapshot_error(sctx, result, "reference");
6344 if (!sctx->cur_inode_new_gen &&
6345 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6346 if (result == BTRFS_COMPARE_TREE_NEW)
6347 ret = record_new_ref(sctx);
6348 else if (result == BTRFS_COMPARE_TREE_DELETED)
6349 ret = record_deleted_ref(sctx);
6350 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6351 ret = record_changed_ref(sctx);
6358 * Process new/deleted/changed xattrs. We skip processing in the
6359 * cur_inode_new_gen case because changed_inode did already initiate processing
6360 * of xattrs. The reason is the same as in changed_ref
6362 static int changed_xattr(struct send_ctx *sctx,
6363 enum btrfs_compare_tree_result result)
6367 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6368 inconsistent_snapshot_error(sctx, result, "xattr");
6372 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6373 if (result == BTRFS_COMPARE_TREE_NEW)
6374 ret = process_new_xattr(sctx);
6375 else if (result == BTRFS_COMPARE_TREE_DELETED)
6376 ret = process_deleted_xattr(sctx);
6377 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6378 ret = process_changed_xattr(sctx);
6385 * Process new/deleted/changed extents. We skip processing in the
6386 * cur_inode_new_gen case because changed_inode did already initiate processing
6387 * of extents. The reason is the same as in changed_ref
6389 static int changed_extent(struct send_ctx *sctx,
6390 enum btrfs_compare_tree_result result)
6395 * We have found an extent item that changed without the inode item
6396 * having changed. This can happen either after relocation (where the
6397 * disk_bytenr of an extent item is replaced at
6398 * relocation.c:replace_file_extents()) or after deduplication into a
6399 * file in both the parent and send snapshots (where an extent item can
6400 * get modified or replaced with a new one). Note that deduplication
6401 * updates the inode item, but it only changes the iversion (sequence
6402 * field in the inode item) of the inode, so if a file is deduplicated
6403 * the same amount of times in both the parent and send snapshots, its
6404 * iversion becames the same in both snapshots, whence the inode item is
6405 * the same on both snapshots.
6407 if (sctx->cur_ino != sctx->cmp_key->objectid)
6410 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6411 if (result != BTRFS_COMPARE_TREE_DELETED)
6412 ret = process_extent(sctx, sctx->left_path,
6419 static int dir_changed(struct send_ctx *sctx, u64 dir)
6421 u64 orig_gen, new_gen;
6424 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6429 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6434 return (orig_gen != new_gen) ? 1 : 0;
6437 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6438 struct btrfs_key *key)
6440 struct btrfs_inode_extref *extref;
6441 struct extent_buffer *leaf;
6442 u64 dirid = 0, last_dirid = 0;
6449 /* Easy case, just check this one dirid */
6450 if (key->type == BTRFS_INODE_REF_KEY) {
6451 dirid = key->offset;
6453 ret = dir_changed(sctx, dirid);
6457 leaf = path->nodes[0];
6458 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6459 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6460 while (cur_offset < item_size) {
6461 extref = (struct btrfs_inode_extref *)(ptr +
6463 dirid = btrfs_inode_extref_parent(leaf, extref);
6464 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6465 cur_offset += ref_name_len + sizeof(*extref);
6466 if (dirid == last_dirid)
6468 ret = dir_changed(sctx, dirid);
6478 * Updates compare related fields in sctx and simply forwards to the actual
6479 * changed_xxx functions.
6481 static int changed_cb(struct btrfs_path *left_path,
6482 struct btrfs_path *right_path,
6483 struct btrfs_key *key,
6484 enum btrfs_compare_tree_result result,
6488 struct send_ctx *sctx = ctx;
6490 if (result == BTRFS_COMPARE_TREE_SAME) {
6491 if (key->type == BTRFS_INODE_REF_KEY ||
6492 key->type == BTRFS_INODE_EXTREF_KEY) {
6493 ret = compare_refs(sctx, left_path, key);
6498 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6499 return maybe_send_hole(sctx, left_path, key);
6503 result = BTRFS_COMPARE_TREE_CHANGED;
6507 sctx->left_path = left_path;
6508 sctx->right_path = right_path;
6509 sctx->cmp_key = key;
6511 ret = finish_inode_if_needed(sctx, 0);
6515 /* Ignore non-FS objects */
6516 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6517 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6520 if (key->type == BTRFS_INODE_ITEM_KEY) {
6521 ret = changed_inode(sctx, result);
6522 } else if (!sctx->ignore_cur_inode) {
6523 if (key->type == BTRFS_INODE_REF_KEY ||
6524 key->type == BTRFS_INODE_EXTREF_KEY)
6525 ret = changed_ref(sctx, result);
6526 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6527 ret = changed_xattr(sctx, result);
6528 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6529 ret = changed_extent(sctx, result);
6536 static int full_send_tree(struct send_ctx *sctx)
6539 struct btrfs_root *send_root = sctx->send_root;
6540 struct btrfs_key key;
6541 struct btrfs_path *path;
6542 struct extent_buffer *eb;
6545 path = alloc_path_for_send();
6549 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6550 key.type = BTRFS_INODE_ITEM_KEY;
6553 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6560 eb = path->nodes[0];
6561 slot = path->slots[0];
6562 btrfs_item_key_to_cpu(eb, &key, slot);
6564 ret = changed_cb(path, NULL, &key,
6565 BTRFS_COMPARE_TREE_NEW, sctx);
6569 ret = btrfs_next_item(send_root, path);
6579 ret = finish_inode_if_needed(sctx, 1);
6582 btrfs_free_path(path);
6586 static int tree_move_down(struct btrfs_path *path, int *level)
6588 struct extent_buffer *eb;
6590 BUG_ON(*level == 0);
6591 eb = btrfs_read_node_slot(path->nodes[*level], path->slots[*level]);
6595 path->nodes[*level - 1] = eb;
6596 path->slots[*level - 1] = 0;
6601 static int tree_move_next_or_upnext(struct btrfs_path *path,
6602 int *level, int root_level)
6606 nritems = btrfs_header_nritems(path->nodes[*level]);
6608 path->slots[*level]++;
6610 while (path->slots[*level] >= nritems) {
6611 if (*level == root_level)
6615 path->slots[*level] = 0;
6616 free_extent_buffer(path->nodes[*level]);
6617 path->nodes[*level] = NULL;
6619 path->slots[*level]++;
6621 nritems = btrfs_header_nritems(path->nodes[*level]);
6628 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
6631 static int tree_advance(struct btrfs_path *path,
6632 int *level, int root_level,
6634 struct btrfs_key *key)
6638 if (*level == 0 || !allow_down) {
6639 ret = tree_move_next_or_upnext(path, level, root_level);
6641 ret = tree_move_down(path, level);
6645 btrfs_item_key_to_cpu(path->nodes[*level], key,
6646 path->slots[*level]);
6648 btrfs_node_key_to_cpu(path->nodes[*level], key,
6649 path->slots[*level]);
6654 static int tree_compare_item(struct btrfs_path *left_path,
6655 struct btrfs_path *right_path,
6660 unsigned long off1, off2;
6662 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
6663 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
6667 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
6668 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
6669 right_path->slots[0]);
6671 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
6673 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
6680 * This function compares two trees and calls the provided callback for
6681 * every changed/new/deleted item it finds.
6682 * If shared tree blocks are encountered, whole subtrees are skipped, making
6683 * the compare pretty fast on snapshotted subvolumes.
6685 * This currently works on commit roots only. As commit roots are read only,
6686 * we don't do any locking. The commit roots are protected with transactions.
6687 * Transactions are ended and rejoined when a commit is tried in between.
6689 * This function checks for modifications done to the trees while comparing.
6690 * If it detects a change, it aborts immediately.
6692 static int btrfs_compare_trees(struct btrfs_root *left_root,
6693 struct btrfs_root *right_root, void *ctx)
6695 struct btrfs_fs_info *fs_info = left_root->fs_info;
6698 struct btrfs_path *left_path = NULL;
6699 struct btrfs_path *right_path = NULL;
6700 struct btrfs_key left_key;
6701 struct btrfs_key right_key;
6702 char *tmp_buf = NULL;
6703 int left_root_level;
6704 int right_root_level;
6707 int left_end_reached;
6708 int right_end_reached;
6716 left_path = btrfs_alloc_path();
6721 right_path = btrfs_alloc_path();
6727 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
6733 left_path->search_commit_root = 1;
6734 left_path->skip_locking = 1;
6735 right_path->search_commit_root = 1;
6736 right_path->skip_locking = 1;
6739 * Strategy: Go to the first items of both trees. Then do
6741 * If both trees are at level 0
6742 * Compare keys of current items
6743 * If left < right treat left item as new, advance left tree
6745 * If left > right treat right item as deleted, advance right tree
6747 * If left == right do deep compare of items, treat as changed if
6748 * needed, advance both trees and repeat
6749 * If both trees are at the same level but not at level 0
6750 * Compare keys of current nodes/leafs
6751 * If left < right advance left tree and repeat
6752 * If left > right advance right tree and repeat
6753 * If left == right compare blockptrs of the next nodes/leafs
6754 * If they match advance both trees but stay at the same level
6756 * If they don't match advance both trees while allowing to go
6758 * If tree levels are different
6759 * Advance the tree that needs it and repeat
6761 * Advancing a tree means:
6762 * If we are at level 0, try to go to the next slot. If that's not
6763 * possible, go one level up and repeat. Stop when we found a level
6764 * where we could go to the next slot. We may at this point be on a
6767 * If we are not at level 0 and not on shared tree blocks, go one
6770 * If we are not at level 0 and on shared tree blocks, go one slot to
6771 * the right if possible or go up and right.
6774 down_read(&fs_info->commit_root_sem);
6775 left_level = btrfs_header_level(left_root->commit_root);
6776 left_root_level = left_level;
6777 left_path->nodes[left_level] =
6778 btrfs_clone_extent_buffer(left_root->commit_root);
6779 if (!left_path->nodes[left_level]) {
6780 up_read(&fs_info->commit_root_sem);
6785 right_level = btrfs_header_level(right_root->commit_root);
6786 right_root_level = right_level;
6787 right_path->nodes[right_level] =
6788 btrfs_clone_extent_buffer(right_root->commit_root);
6789 if (!right_path->nodes[right_level]) {
6790 up_read(&fs_info->commit_root_sem);
6794 up_read(&fs_info->commit_root_sem);
6796 if (left_level == 0)
6797 btrfs_item_key_to_cpu(left_path->nodes[left_level],
6798 &left_key, left_path->slots[left_level]);
6800 btrfs_node_key_to_cpu(left_path->nodes[left_level],
6801 &left_key, left_path->slots[left_level]);
6802 if (right_level == 0)
6803 btrfs_item_key_to_cpu(right_path->nodes[right_level],
6804 &right_key, right_path->slots[right_level]);
6806 btrfs_node_key_to_cpu(right_path->nodes[right_level],
6807 &right_key, right_path->slots[right_level]);
6809 left_end_reached = right_end_reached = 0;
6810 advance_left = advance_right = 0;
6814 if (advance_left && !left_end_reached) {
6815 ret = tree_advance(left_path, &left_level,
6817 advance_left != ADVANCE_ONLY_NEXT,
6820 left_end_reached = ADVANCE;
6825 if (advance_right && !right_end_reached) {
6826 ret = tree_advance(right_path, &right_level,
6828 advance_right != ADVANCE_ONLY_NEXT,
6831 right_end_reached = ADVANCE;
6837 if (left_end_reached && right_end_reached) {
6840 } else if (left_end_reached) {
6841 if (right_level == 0) {
6842 ret = changed_cb(left_path, right_path,
6844 BTRFS_COMPARE_TREE_DELETED,
6849 advance_right = ADVANCE;
6851 } else if (right_end_reached) {
6852 if (left_level == 0) {
6853 ret = changed_cb(left_path, right_path,
6855 BTRFS_COMPARE_TREE_NEW,
6860 advance_left = ADVANCE;
6864 if (left_level == 0 && right_level == 0) {
6865 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
6867 ret = changed_cb(left_path, right_path,
6869 BTRFS_COMPARE_TREE_NEW,
6873 advance_left = ADVANCE;
6874 } else if (cmp > 0) {
6875 ret = changed_cb(left_path, right_path,
6877 BTRFS_COMPARE_TREE_DELETED,
6881 advance_right = ADVANCE;
6883 enum btrfs_compare_tree_result result;
6885 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
6886 ret = tree_compare_item(left_path, right_path,
6889 result = BTRFS_COMPARE_TREE_CHANGED;
6891 result = BTRFS_COMPARE_TREE_SAME;
6892 ret = changed_cb(left_path, right_path,
6893 &left_key, result, ctx);
6896 advance_left = ADVANCE;
6897 advance_right = ADVANCE;
6899 } else if (left_level == right_level) {
6900 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
6902 advance_left = ADVANCE;
6903 } else if (cmp > 0) {
6904 advance_right = ADVANCE;
6906 left_blockptr = btrfs_node_blockptr(
6907 left_path->nodes[left_level],
6908 left_path->slots[left_level]);
6909 right_blockptr = btrfs_node_blockptr(
6910 right_path->nodes[right_level],
6911 right_path->slots[right_level]);
6912 left_gen = btrfs_node_ptr_generation(
6913 left_path->nodes[left_level],
6914 left_path->slots[left_level]);
6915 right_gen = btrfs_node_ptr_generation(
6916 right_path->nodes[right_level],
6917 right_path->slots[right_level]);
6918 if (left_blockptr == right_blockptr &&
6919 left_gen == right_gen) {
6921 * As we're on a shared block, don't
6922 * allow to go deeper.
6924 advance_left = ADVANCE_ONLY_NEXT;
6925 advance_right = ADVANCE_ONLY_NEXT;
6927 advance_left = ADVANCE;
6928 advance_right = ADVANCE;
6931 } else if (left_level < right_level) {
6932 advance_right = ADVANCE;
6934 advance_left = ADVANCE;
6939 btrfs_free_path(left_path);
6940 btrfs_free_path(right_path);
6945 static int send_subvol(struct send_ctx *sctx)
6949 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6950 ret = send_header(sctx);
6955 ret = send_subvol_begin(sctx);
6959 if (sctx->parent_root) {
6960 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
6963 ret = finish_inode_if_needed(sctx, 1);
6967 ret = full_send_tree(sctx);
6973 free_recorded_refs(sctx);
6978 * If orphan cleanup did remove any orphans from a root, it means the tree
6979 * was modified and therefore the commit root is not the same as the current
6980 * root anymore. This is a problem, because send uses the commit root and
6981 * therefore can see inode items that don't exist in the current root anymore,
6982 * and for example make calls to btrfs_iget, which will do tree lookups based
6983 * on the current root and not on the commit root. Those lookups will fail,
6984 * returning a -ESTALE error, and making send fail with that error. So make
6985 * sure a send does not see any orphans we have just removed, and that it will
6986 * see the same inodes regardless of whether a transaction commit happened
6987 * before it started (meaning that the commit root will be the same as the
6988 * current root) or not.
6990 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6993 struct btrfs_trans_handle *trans = NULL;
6996 if (sctx->parent_root &&
6997 sctx->parent_root->node != sctx->parent_root->commit_root)
7000 for (i = 0; i < sctx->clone_roots_cnt; i++)
7001 if (sctx->clone_roots[i].root->node !=
7002 sctx->clone_roots[i].root->commit_root)
7006 return btrfs_end_transaction(trans);
7011 /* Use any root, all fs roots will get their commit roots updated. */
7013 trans = btrfs_join_transaction(sctx->send_root);
7015 return PTR_ERR(trans);
7019 return btrfs_commit_transaction(trans);
7023 * Make sure any existing dellaloc is flushed for any root used by a send
7024 * operation so that we do not miss any data and we do not race with writeback
7025 * finishing and changing a tree while send is using the tree. This could
7026 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
7027 * a send operation then uses the subvolume.
7028 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
7030 static int flush_delalloc_roots(struct send_ctx *sctx)
7032 struct btrfs_root *root = sctx->parent_root;
7037 ret = btrfs_start_delalloc_snapshot(root);
7040 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7043 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7044 root = sctx->clone_roots[i].root;
7045 ret = btrfs_start_delalloc_snapshot(root);
7048 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7054 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7056 spin_lock(&root->root_item_lock);
7057 root->send_in_progress--;
7059 * Not much left to do, we don't know why it's unbalanced and
7060 * can't blindly reset it to 0.
7062 if (root->send_in_progress < 0)
7063 btrfs_err(root->fs_info,
7064 "send_in_progress unbalanced %d root %llu",
7065 root->send_in_progress, root->root_key.objectid);
7066 spin_unlock(&root->root_item_lock);
7069 static void dedupe_in_progress_warn(const struct btrfs_root *root)
7071 btrfs_warn_rl(root->fs_info,
7072 "cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7073 root->root_key.objectid, root->dedupe_in_progress);
7076 long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
7079 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
7080 struct btrfs_fs_info *fs_info = send_root->fs_info;
7081 struct btrfs_root *clone_root;
7082 struct send_ctx *sctx = NULL;
7084 u64 *clone_sources_tmp = NULL;
7085 int clone_sources_to_rollback = 0;
7086 unsigned alloc_size;
7087 int sort_clone_roots = 0;
7089 if (!capable(CAP_SYS_ADMIN))
7093 * The subvolume must remain read-only during send, protect against
7094 * making it RW. This also protects against deletion.
7096 spin_lock(&send_root->root_item_lock);
7097 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7098 dedupe_in_progress_warn(send_root);
7099 spin_unlock(&send_root->root_item_lock);
7102 send_root->send_in_progress++;
7103 spin_unlock(&send_root->root_item_lock);
7106 * Userspace tools do the checks and warn the user if it's
7109 if (!btrfs_root_readonly(send_root)) {
7115 * Check that we don't overflow at later allocations, we request
7116 * clone_sources_count + 1 items, and compare to unsigned long inside
7119 if (arg->clone_sources_count >
7120 ULONG_MAX / sizeof(struct clone_root) - 1) {
7125 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
7130 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
7136 INIT_LIST_HEAD(&sctx->new_refs);
7137 INIT_LIST_HEAD(&sctx->deleted_refs);
7138 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
7139 INIT_LIST_HEAD(&sctx->name_cache_list);
7141 sctx->flags = arg->flags;
7143 sctx->send_filp = fget(arg->send_fd);
7144 if (!sctx->send_filp) {
7149 sctx->send_root = send_root;
7151 * Unlikely but possible, if the subvolume is marked for deletion but
7152 * is slow to remove the directory entry, send can still be started
7154 if (btrfs_root_dead(sctx->send_root)) {
7159 sctx->clone_roots_cnt = arg->clone_sources_count;
7161 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
7162 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
7163 if (!sctx->send_buf) {
7168 sctx->pending_dir_moves = RB_ROOT;
7169 sctx->waiting_dir_moves = RB_ROOT;
7170 sctx->orphan_dirs = RB_ROOT;
7172 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
7174 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL);
7175 if (!sctx->clone_roots) {
7180 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
7182 if (arg->clone_sources_count) {
7183 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
7184 if (!clone_sources_tmp) {
7189 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
7196 for (i = 0; i < arg->clone_sources_count; i++) {
7197 clone_root = btrfs_get_fs_root(fs_info,
7198 clone_sources_tmp[i], true);
7199 if (IS_ERR(clone_root)) {
7200 ret = PTR_ERR(clone_root);
7203 spin_lock(&clone_root->root_item_lock);
7204 if (!btrfs_root_readonly(clone_root) ||
7205 btrfs_root_dead(clone_root)) {
7206 spin_unlock(&clone_root->root_item_lock);
7207 btrfs_put_root(clone_root);
7211 if (clone_root->dedupe_in_progress) {
7212 dedupe_in_progress_warn(clone_root);
7213 spin_unlock(&clone_root->root_item_lock);
7214 btrfs_put_root(clone_root);
7218 clone_root->send_in_progress++;
7219 spin_unlock(&clone_root->root_item_lock);
7221 sctx->clone_roots[i].root = clone_root;
7222 clone_sources_to_rollback = i + 1;
7224 kvfree(clone_sources_tmp);
7225 clone_sources_tmp = NULL;
7228 if (arg->parent_root) {
7229 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7231 if (IS_ERR(sctx->parent_root)) {
7232 ret = PTR_ERR(sctx->parent_root);
7236 spin_lock(&sctx->parent_root->root_item_lock);
7237 sctx->parent_root->send_in_progress++;
7238 if (!btrfs_root_readonly(sctx->parent_root) ||
7239 btrfs_root_dead(sctx->parent_root)) {
7240 spin_unlock(&sctx->parent_root->root_item_lock);
7244 if (sctx->parent_root->dedupe_in_progress) {
7245 dedupe_in_progress_warn(sctx->parent_root);
7246 spin_unlock(&sctx->parent_root->root_item_lock);
7250 spin_unlock(&sctx->parent_root->root_item_lock);
7254 * Clones from send_root are allowed, but only if the clone source
7255 * is behind the current send position. This is checked while searching
7256 * for possible clone sources.
7258 sctx->clone_roots[sctx->clone_roots_cnt++].root =
7259 btrfs_grab_root(sctx->send_root);
7261 /* We do a bsearch later */
7262 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7263 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7265 sort_clone_roots = 1;
7267 ret = flush_delalloc_roots(sctx);
7271 ret = ensure_commit_roots_uptodate(sctx);
7275 mutex_lock(&fs_info->balance_mutex);
7276 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
7277 mutex_unlock(&fs_info->balance_mutex);
7278 btrfs_warn_rl(fs_info,
7279 "cannot run send because a balance operation is in progress");
7283 fs_info->send_in_progress++;
7284 mutex_unlock(&fs_info->balance_mutex);
7286 current->journal_info = BTRFS_SEND_TRANS_STUB;
7287 ret = send_subvol(sctx);
7288 current->journal_info = NULL;
7289 mutex_lock(&fs_info->balance_mutex);
7290 fs_info->send_in_progress--;
7291 mutex_unlock(&fs_info->balance_mutex);
7295 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7296 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7299 ret = send_cmd(sctx);
7305 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7306 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7308 struct pending_dir_move *pm;
7310 n = rb_first(&sctx->pending_dir_moves);
7311 pm = rb_entry(n, struct pending_dir_move, node);
7312 while (!list_empty(&pm->list)) {
7313 struct pending_dir_move *pm2;
7315 pm2 = list_first_entry(&pm->list,
7316 struct pending_dir_move, list);
7317 free_pending_move(sctx, pm2);
7319 free_pending_move(sctx, pm);
7322 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7323 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7325 struct waiting_dir_move *dm;
7327 n = rb_first(&sctx->waiting_dir_moves);
7328 dm = rb_entry(n, struct waiting_dir_move, node);
7329 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7333 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7334 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7336 struct orphan_dir_info *odi;
7338 n = rb_first(&sctx->orphan_dirs);
7339 odi = rb_entry(n, struct orphan_dir_info, node);
7340 free_orphan_dir_info(sctx, odi);
7343 if (sort_clone_roots) {
7344 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7345 btrfs_root_dec_send_in_progress(
7346 sctx->clone_roots[i].root);
7347 btrfs_put_root(sctx->clone_roots[i].root);
7350 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
7351 btrfs_root_dec_send_in_progress(
7352 sctx->clone_roots[i].root);
7353 btrfs_put_root(sctx->clone_roots[i].root);
7356 btrfs_root_dec_send_in_progress(send_root);
7358 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
7359 btrfs_root_dec_send_in_progress(sctx->parent_root);
7360 btrfs_put_root(sctx->parent_root);
7363 kvfree(clone_sources_tmp);
7366 if (sctx->send_filp)
7367 fput(sctx->send_filp);
7369 kvfree(sctx->clone_roots);
7370 kvfree(sctx->send_buf);
7372 name_cache_free(sctx);