2 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/bsearch.h>
21 #include <linux/file.h>
22 #include <linux/sort.h>
23 #include <linux/mount.h>
24 #include <linux/xattr.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/radix-tree.h>
27 #include <linux/vmalloc.h>
28 #include <linux/string.h>
35 #include "btrfs_inode.h"
36 #include "transaction.h"
37 #include "compression.h"
41 * Maximum number of references an extent can have in order for us to attempt to
42 * issue clone operations instead of write operations. This currently exists to
43 * avoid hitting limitations of the backreference walking code (taking a lot of
44 * time and using too much memory for extents with large number of references).
46 #define SEND_MAX_EXTENT_REFS 64
49 * A fs_path is a helper to dynamically build path names with unknown size.
50 * It reallocates the internal buffer on demand.
51 * It allows fast adding of path elements on the right side (normal path) and
52 * fast adding to the left side (reversed path). A reversed path can also be
53 * unreversed if needed.
62 unsigned short buf_len:15;
63 unsigned short reversed:1;
67 * Average path length does not exceed 200 bytes, we'll have
68 * better packing in the slab and higher chance to satisfy
69 * a allocation later during send.
74 #define FS_PATH_INLINE_SIZE \
75 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
78 /* reused for each extent */
80 struct btrfs_root *root;
87 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
88 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
91 struct file *send_filp;
97 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
98 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
100 struct btrfs_root *send_root;
101 struct btrfs_root *parent_root;
102 struct clone_root *clone_roots;
105 /* current state of the compare_tree call */
106 struct btrfs_path *left_path;
107 struct btrfs_path *right_path;
108 struct btrfs_key *cmp_key;
111 * infos of the currently processed inode. In case of deleted inodes,
112 * these are the values from the deleted inode.
117 int cur_inode_new_gen;
118 int cur_inode_deleted;
122 u64 cur_inode_last_extent;
126 struct list_head new_refs;
127 struct list_head deleted_refs;
129 struct radix_tree_root name_cache;
130 struct list_head name_cache_list;
133 struct file_ra_state ra;
138 * We process inodes by their increasing order, so if before an
139 * incremental send we reverse the parent/child relationship of
140 * directories such that a directory with a lower inode number was
141 * the parent of a directory with a higher inode number, and the one
142 * becoming the new parent got renamed too, we can't rename/move the
143 * directory with lower inode number when we finish processing it - we
144 * must process the directory with higher inode number first, then
145 * rename/move it and then rename/move the directory with lower inode
146 * number. Example follows.
148 * Tree state when the first send was performed:
160 * Tree state when the second (incremental) send is performed:
169 * The sequence of steps that lead to the second state was:
171 * mv /a/b/c/d /a/b/c2/d2
172 * mv /a/b/c /a/b/c2/d2/cc
174 * "c" has lower inode number, but we can't move it (2nd mv operation)
175 * before we move "d", which has higher inode number.
177 * So we just memorize which move/rename operations must be performed
178 * later when their respective parent is processed and moved/renamed.
181 /* Indexed by parent directory inode number. */
182 struct rb_root pending_dir_moves;
185 * Reverse index, indexed by the inode number of a directory that
186 * is waiting for the move/rename of its immediate parent before its
187 * own move/rename can be performed.
189 struct rb_root waiting_dir_moves;
192 * A directory that is going to be rm'ed might have a child directory
193 * which is in the pending directory moves index above. In this case,
194 * the directory can only be removed after the move/rename of its child
195 * is performed. Example:
215 * Sequence of steps that lead to the send snapshot:
216 * rm -f /a/b/c/foo.txt
218 * mv /a/b/c/x /a/b/YY
221 * When the child is processed, its move/rename is delayed until its
222 * parent is processed (as explained above), but all other operations
223 * like update utimes, chown, chgrp, etc, are performed and the paths
224 * that it uses for those operations must use the orphanized name of
225 * its parent (the directory we're going to rm later), so we need to
226 * memorize that name.
228 * Indexed by the inode number of the directory to be deleted.
230 struct rb_root orphan_dirs;
233 struct pending_dir_move {
235 struct list_head list;
239 struct list_head update_refs;
242 struct waiting_dir_move {
246 * There might be some directory that could not be removed because it
247 * was waiting for this directory inode to be moved first. Therefore
248 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
254 struct orphan_dir_info {
260 struct name_cache_entry {
261 struct list_head list;
263 * radix_tree has only 32bit entries but we need to handle 64bit inums.
264 * We use the lower 32bit of the 64bit inum to store it in the tree. If
265 * more then one inum would fall into the same entry, we use radix_list
266 * to store the additional entries. radix_list is also used to store
267 * entries where two entries have the same inum but different
270 struct list_head radix_list;
276 int need_later_update;
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, attrlen, data) \
624 ret = tlv_put(sctx, attrtype, attrlen, data); \
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 struct btrfs_key *found_key,
1005 iterate_dir_item_t iterate, void *ctx)
1008 struct extent_buffer *eb;
1009 struct btrfs_item *item;
1010 struct btrfs_dir_item *di;
1011 struct btrfs_key di_key;
1024 * Start with a small buffer (1 page). If later we end up needing more
1025 * space, which can happen for xattrs on a fs with a leaf size greater
1026 * then the page size, attempt to increase the buffer. Typically xattr
1030 buf = kmalloc(buf_len, GFP_KERNEL);
1036 eb = path->nodes[0];
1037 slot = path->slots[0];
1038 item = btrfs_item_nr(slot);
1039 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1042 total = btrfs_item_size(eb, item);
1045 while (cur < total) {
1046 name_len = btrfs_dir_name_len(eb, di);
1047 data_len = btrfs_dir_data_len(eb, di);
1048 type = btrfs_dir_type(eb, di);
1049 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1051 if (type == BTRFS_FT_XATTR) {
1052 if (name_len > XATTR_NAME_MAX) {
1053 ret = -ENAMETOOLONG;
1056 if (name_len + data_len >
1057 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1065 if (name_len + data_len > PATH_MAX) {
1066 ret = -ENAMETOOLONG;
1071 ret = btrfs_is_name_len_valid(eb, path->slots[0],
1072 (unsigned long)(di + 1), name_len + data_len);
1077 if (name_len + data_len > buf_len) {
1078 buf_len = name_len + data_len;
1079 if (is_vmalloc_addr(buf)) {
1083 char *tmp = krealloc(buf, buf_len,
1084 GFP_KERNEL | __GFP_NOWARN);
1091 buf = kvmalloc(buf_len, GFP_KERNEL);
1099 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1100 name_len + data_len);
1102 len = sizeof(*di) + name_len + data_len;
1103 di = (struct btrfs_dir_item *)((char *)di + len);
1106 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1107 data_len, type, ctx);
1123 static int __copy_first_ref(int num, u64 dir, int index,
1124 struct fs_path *p, void *ctx)
1127 struct fs_path *pt = ctx;
1129 ret = fs_path_copy(pt, p);
1133 /* we want the first only */
1138 * Retrieve the first path of an inode. If an inode has more then one
1139 * ref/hardlink, this is ignored.
1141 static int get_inode_path(struct btrfs_root *root,
1142 u64 ino, struct fs_path *path)
1145 struct btrfs_key key, found_key;
1146 struct btrfs_path *p;
1148 p = alloc_path_for_send();
1152 fs_path_reset(path);
1155 key.type = BTRFS_INODE_REF_KEY;
1158 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1165 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1166 if (found_key.objectid != ino ||
1167 (found_key.type != BTRFS_INODE_REF_KEY &&
1168 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1173 ret = iterate_inode_ref(root, p, &found_key, 1,
1174 __copy_first_ref, path);
1184 struct backref_ctx {
1185 struct send_ctx *sctx;
1187 struct btrfs_path *path;
1188 /* number of total found references */
1192 * used for clones found in send_root. clones found behind cur_objectid
1193 * and cur_offset are not considered as allowed clones.
1198 /* may be truncated in case it's the last extent in a file */
1201 /* data offset in the file extent item */
1204 /* Just to check for bugs in backref resolving */
1208 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1210 u64 root = (u64)(uintptr_t)key;
1211 struct clone_root *cr = (struct clone_root *)elt;
1213 if (root < cr->root->objectid)
1215 if (root > cr->root->objectid)
1220 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1222 struct clone_root *cr1 = (struct clone_root *)e1;
1223 struct clone_root *cr2 = (struct clone_root *)e2;
1225 if (cr1->root->objectid < cr2->root->objectid)
1227 if (cr1->root->objectid > cr2->root->objectid)
1233 * Called for every backref that is found for the current extent.
1234 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1236 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1238 struct backref_ctx *bctx = ctx_;
1239 struct clone_root *found;
1243 /* First check if the root is in the list of accepted clone sources */
1244 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1245 bctx->sctx->clone_roots_cnt,
1246 sizeof(struct clone_root),
1247 __clone_root_cmp_bsearch);
1251 if (found->root == bctx->sctx->send_root &&
1252 ino == bctx->cur_objectid &&
1253 offset == bctx->cur_offset) {
1254 bctx->found_itself = 1;
1258 * There are inodes that have extents that lie behind its i_size. Don't
1259 * accept clones from these extents.
1261 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1263 btrfs_release_path(bctx->path);
1267 if (offset + bctx->data_offset + bctx->extent_len > i_size)
1271 * Make sure we don't consider clones from send_root that are
1272 * behind the current inode/offset.
1274 if (found->root == bctx->sctx->send_root) {
1276 * TODO for the moment we don't accept clones from the inode
1277 * that is currently send. We may change this when
1278 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1281 if (ino >= bctx->cur_objectid)
1284 if (ino > bctx->cur_objectid)
1286 if (offset + bctx->extent_len > bctx->cur_offset)
1292 found->found_refs++;
1293 if (ino < found->ino) {
1295 found->offset = offset;
1296 } else if (found->ino == ino) {
1298 * same extent found more then once in the same file.
1300 if (found->offset > offset + bctx->extent_len)
1301 found->offset = offset;
1308 * Given an inode, offset and extent item, it finds a good clone for a clone
1309 * instruction. Returns -ENOENT when none could be found. The function makes
1310 * sure that the returned clone is usable at the point where sending is at the
1311 * moment. This means, that no clones are accepted which lie behind the current
1314 * path must point to the extent item when called.
1316 static int find_extent_clone(struct send_ctx *sctx,
1317 struct btrfs_path *path,
1318 u64 ino, u64 data_offset,
1320 struct clone_root **found)
1322 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1328 u64 extent_item_pos;
1330 struct btrfs_file_extent_item *fi;
1331 struct extent_buffer *eb = path->nodes[0];
1332 struct backref_ctx *backref_ctx = NULL;
1333 struct clone_root *cur_clone_root;
1334 struct btrfs_key found_key;
1335 struct btrfs_path *tmp_path;
1336 struct btrfs_extent_item *ei;
1340 tmp_path = alloc_path_for_send();
1344 /* We only use this path under the commit sem */
1345 tmp_path->need_commit_sem = 0;
1347 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
1353 backref_ctx->path = tmp_path;
1355 if (data_offset >= ino_size) {
1357 * There may be extents that lie behind the file's size.
1358 * I at least had this in combination with snapshotting while
1359 * writing large files.
1365 fi = btrfs_item_ptr(eb, path->slots[0],
1366 struct btrfs_file_extent_item);
1367 extent_type = btrfs_file_extent_type(eb, fi);
1368 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1372 compressed = btrfs_file_extent_compression(eb, fi);
1374 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1375 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1376 if (disk_byte == 0) {
1380 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1382 down_read(&fs_info->commit_root_sem);
1383 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1384 &found_key, &flags);
1385 up_read(&fs_info->commit_root_sem);
1389 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1394 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1395 struct btrfs_extent_item);
1397 * Backreference walking (iterate_extent_inodes() below) is currently
1398 * too expensive when an extent has a large number of references, both
1399 * in time spent and used memory. So for now just fallback to write
1400 * operations instead of clone operations when an extent has more than
1401 * a certain amount of references.
1403 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1407 btrfs_release_path(tmp_path);
1410 * Setup the clone roots.
1412 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1413 cur_clone_root = sctx->clone_roots + i;
1414 cur_clone_root->ino = (u64)-1;
1415 cur_clone_root->offset = 0;
1416 cur_clone_root->found_refs = 0;
1419 backref_ctx->sctx = sctx;
1420 backref_ctx->found = 0;
1421 backref_ctx->cur_objectid = ino;
1422 backref_ctx->cur_offset = data_offset;
1423 backref_ctx->found_itself = 0;
1424 backref_ctx->extent_len = num_bytes;
1426 * For non-compressed extents iterate_extent_inodes() gives us extent
1427 * offsets that already take into account the data offset, but not for
1428 * compressed extents, since the offset is logical and not relative to
1429 * the physical extent locations. We must take this into account to
1430 * avoid sending clone offsets that go beyond the source file's size,
1431 * which would result in the clone ioctl failing with -EINVAL on the
1434 if (compressed == BTRFS_COMPRESS_NONE)
1435 backref_ctx->data_offset = 0;
1437 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
1440 * The last extent of a file may be too large due to page alignment.
1441 * We need to adjust extent_len in this case so that the checks in
1442 * __iterate_backrefs work.
1444 if (data_offset + num_bytes >= ino_size)
1445 backref_ctx->extent_len = ino_size - data_offset;
1448 * Now collect all backrefs.
1450 if (compressed == BTRFS_COMPRESS_NONE)
1451 extent_item_pos = logical - found_key.objectid;
1453 extent_item_pos = 0;
1454 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1455 extent_item_pos, 1, __iterate_backrefs,
1461 if (!backref_ctx->found_itself) {
1462 /* found a bug in backref code? */
1465 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1466 ino, data_offset, disk_byte, found_key.objectid);
1470 btrfs_debug(fs_info,
1471 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1472 data_offset, ino, num_bytes, logical);
1474 if (!backref_ctx->found)
1475 btrfs_debug(fs_info, "no clones found");
1477 cur_clone_root = NULL;
1478 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1479 if (sctx->clone_roots[i].found_refs) {
1480 if (!cur_clone_root)
1481 cur_clone_root = sctx->clone_roots + i;
1482 else if (sctx->clone_roots[i].root == sctx->send_root)
1483 /* prefer clones from send_root over others */
1484 cur_clone_root = sctx->clone_roots + i;
1489 if (cur_clone_root) {
1490 *found = cur_clone_root;
1497 btrfs_free_path(tmp_path);
1502 static int read_symlink(struct btrfs_root *root,
1504 struct fs_path *dest)
1507 struct btrfs_path *path;
1508 struct btrfs_key key;
1509 struct btrfs_file_extent_item *ei;
1515 path = alloc_path_for_send();
1520 key.type = BTRFS_EXTENT_DATA_KEY;
1522 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1527 * An empty symlink inode. Can happen in rare error paths when
1528 * creating a symlink (transaction committed before the inode
1529 * eviction handler removed the symlink inode items and a crash
1530 * happened in between or the subvol was snapshoted in between).
1531 * Print an informative message to dmesg/syslog so that the user
1532 * can delete the symlink.
1534 btrfs_err(root->fs_info,
1535 "Found empty symlink inode %llu at root %llu",
1536 ino, root->root_key.objectid);
1541 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1542 struct btrfs_file_extent_item);
1543 type = btrfs_file_extent_type(path->nodes[0], ei);
1544 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1545 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1546 BUG_ON(compression);
1548 off = btrfs_file_extent_inline_start(ei);
1549 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
1551 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1554 btrfs_free_path(path);
1559 * Helper function to generate a file name that is unique in the root of
1560 * send_root and parent_root. This is used to generate names for orphan inodes.
1562 static int gen_unique_name(struct send_ctx *sctx,
1564 struct fs_path *dest)
1567 struct btrfs_path *path;
1568 struct btrfs_dir_item *di;
1573 path = alloc_path_for_send();
1578 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1580 ASSERT(len < sizeof(tmp));
1582 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1583 path, BTRFS_FIRST_FREE_OBJECTID,
1584 tmp, strlen(tmp), 0);
1585 btrfs_release_path(path);
1591 /* not unique, try again */
1596 if (!sctx->parent_root) {
1602 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1603 path, BTRFS_FIRST_FREE_OBJECTID,
1604 tmp, strlen(tmp), 0);
1605 btrfs_release_path(path);
1611 /* not unique, try again */
1619 ret = fs_path_add(dest, tmp, strlen(tmp));
1622 btrfs_free_path(path);
1627 inode_state_no_change,
1628 inode_state_will_create,
1629 inode_state_did_create,
1630 inode_state_will_delete,
1631 inode_state_did_delete,
1634 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1642 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1644 if (ret < 0 && ret != -ENOENT)
1648 if (!sctx->parent_root) {
1649 right_ret = -ENOENT;
1651 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1652 NULL, NULL, NULL, NULL);
1653 if (ret < 0 && ret != -ENOENT)
1658 if (!left_ret && !right_ret) {
1659 if (left_gen == gen && right_gen == gen) {
1660 ret = inode_state_no_change;
1661 } else if (left_gen == gen) {
1662 if (ino < sctx->send_progress)
1663 ret = inode_state_did_create;
1665 ret = inode_state_will_create;
1666 } else if (right_gen == gen) {
1667 if (ino < sctx->send_progress)
1668 ret = inode_state_did_delete;
1670 ret = inode_state_will_delete;
1674 } else if (!left_ret) {
1675 if (left_gen == gen) {
1676 if (ino < sctx->send_progress)
1677 ret = inode_state_did_create;
1679 ret = inode_state_will_create;
1683 } else if (!right_ret) {
1684 if (right_gen == gen) {
1685 if (ino < sctx->send_progress)
1686 ret = inode_state_did_delete;
1688 ret = inode_state_will_delete;
1700 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1704 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1707 ret = get_cur_inode_state(sctx, ino, gen);
1711 if (ret == inode_state_no_change ||
1712 ret == inode_state_did_create ||
1713 ret == inode_state_will_delete)
1723 * Helper function to lookup a dir item in a dir.
1725 static int lookup_dir_item_inode(struct btrfs_root *root,
1726 u64 dir, const char *name, int name_len,
1731 struct btrfs_dir_item *di;
1732 struct btrfs_key key;
1733 struct btrfs_path *path;
1735 path = alloc_path_for_send();
1739 di = btrfs_lookup_dir_item(NULL, root, path,
1740 dir, name, name_len, 0);
1749 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1750 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1754 *found_inode = key.objectid;
1755 *found_type = btrfs_dir_type(path->nodes[0], di);
1758 btrfs_free_path(path);
1763 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1764 * generation of the parent dir and the name of the dir entry.
1766 static int get_first_ref(struct btrfs_root *root, u64 ino,
1767 u64 *dir, u64 *dir_gen, struct fs_path *name)
1770 struct btrfs_key key;
1771 struct btrfs_key found_key;
1772 struct btrfs_path *path;
1776 path = alloc_path_for_send();
1781 key.type = BTRFS_INODE_REF_KEY;
1784 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1788 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1790 if (ret || found_key.objectid != ino ||
1791 (found_key.type != BTRFS_INODE_REF_KEY &&
1792 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1797 if (found_key.type == BTRFS_INODE_REF_KEY) {
1798 struct btrfs_inode_ref *iref;
1799 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1800 struct btrfs_inode_ref);
1801 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1802 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1803 (unsigned long)(iref + 1),
1805 parent_dir = found_key.offset;
1807 struct btrfs_inode_extref *extref;
1808 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1809 struct btrfs_inode_extref);
1810 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1811 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1812 (unsigned long)&extref->name, len);
1813 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1817 btrfs_release_path(path);
1820 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1829 btrfs_free_path(path);
1833 static int is_first_ref(struct btrfs_root *root,
1835 const char *name, int name_len)
1838 struct fs_path *tmp_name;
1841 tmp_name = fs_path_alloc();
1845 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1849 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1854 ret = !memcmp(tmp_name->start, name, name_len);
1857 fs_path_free(tmp_name);
1862 * Used by process_recorded_refs to determine if a new ref would overwrite an
1863 * already existing ref. In case it detects an overwrite, it returns the
1864 * inode/gen in who_ino/who_gen.
1865 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1866 * to make sure later references to the overwritten inode are possible.
1867 * Orphanizing is however only required for the first ref of an inode.
1868 * process_recorded_refs does an additional is_first_ref check to see if
1869 * orphanizing is really required.
1871 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1872 const char *name, int name_len,
1873 u64 *who_ino, u64 *who_gen, u64 *who_mode)
1877 u64 other_inode = 0;
1880 if (!sctx->parent_root)
1883 ret = is_inode_existent(sctx, dir, dir_gen);
1888 * If we have a parent root we need to verify that the parent dir was
1889 * not deleted and then re-created, if it was then we have no overwrite
1890 * and we can just unlink this entry.
1892 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
1893 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1895 if (ret < 0 && ret != -ENOENT)
1905 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1906 &other_inode, &other_type);
1907 if (ret < 0 && ret != -ENOENT)
1915 * Check if the overwritten ref was already processed. If yes, the ref
1916 * was already unlinked/moved, so we can safely assume that we will not
1917 * overwrite anything at this point in time.
1919 if (other_inode > sctx->send_progress ||
1920 is_waiting_for_move(sctx, other_inode)) {
1921 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1922 who_gen, who_mode, NULL, NULL, NULL);
1927 *who_ino = other_inode;
1937 * Checks if the ref was overwritten by an already processed inode. This is
1938 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1939 * thus the orphan name needs be used.
1940 * process_recorded_refs also uses it to avoid unlinking of refs that were
1943 static int did_overwrite_ref(struct send_ctx *sctx,
1944 u64 dir, u64 dir_gen,
1945 u64 ino, u64 ino_gen,
1946 const char *name, int name_len)
1953 if (!sctx->parent_root)
1956 ret = is_inode_existent(sctx, dir, dir_gen);
1960 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1961 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1963 if (ret < 0 && ret != -ENOENT)
1973 /* check if the ref was overwritten by another ref */
1974 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1975 &ow_inode, &other_type);
1976 if (ret < 0 && ret != -ENOENT)
1979 /* was never and will never be overwritten */
1984 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1989 if (ow_inode == ino && gen == ino_gen) {
1995 * We know that it is or will be overwritten. Check this now.
1996 * The current inode being processed might have been the one that caused
1997 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1998 * the current inode being processed.
2000 if ((ow_inode < sctx->send_progress) ||
2001 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
2002 gen == sctx->cur_inode_gen))
2012 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
2013 * that got overwritten. This is used by process_recorded_refs to determine
2014 * if it has to use the path as returned by get_cur_path or the orphan name.
2016 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
2019 struct fs_path *name = NULL;
2023 if (!sctx->parent_root)
2026 name = fs_path_alloc();
2030 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
2034 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2035 name->start, fs_path_len(name));
2043 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2044 * so we need to do some special handling in case we have clashes. This function
2045 * takes care of this with the help of name_cache_entry::radix_list.
2046 * In case of error, nce is kfreed.
2048 static int name_cache_insert(struct send_ctx *sctx,
2049 struct name_cache_entry *nce)
2052 struct list_head *nce_head;
2054 nce_head = radix_tree_lookup(&sctx->name_cache,
2055 (unsigned long)nce->ino);
2057 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
2062 INIT_LIST_HEAD(nce_head);
2064 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
2071 list_add_tail(&nce->radix_list, nce_head);
2072 list_add_tail(&nce->list, &sctx->name_cache_list);
2073 sctx->name_cache_size++;
2078 static void name_cache_delete(struct send_ctx *sctx,
2079 struct name_cache_entry *nce)
2081 struct list_head *nce_head;
2083 nce_head = radix_tree_lookup(&sctx->name_cache,
2084 (unsigned long)nce->ino);
2086 btrfs_err(sctx->send_root->fs_info,
2087 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2088 nce->ino, sctx->name_cache_size);
2091 list_del(&nce->radix_list);
2092 list_del(&nce->list);
2093 sctx->name_cache_size--;
2096 * We may not get to the final release of nce_head if the lookup fails
2098 if (nce_head && list_empty(nce_head)) {
2099 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2104 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2107 struct list_head *nce_head;
2108 struct name_cache_entry *cur;
2110 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2114 list_for_each_entry(cur, nce_head, radix_list) {
2115 if (cur->ino == ino && cur->gen == gen)
2122 * Removes the entry from the list and adds it back to the end. This marks the
2123 * entry as recently used so that name_cache_clean_unused does not remove it.
2125 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2127 list_del(&nce->list);
2128 list_add_tail(&nce->list, &sctx->name_cache_list);
2132 * Remove some entries from the beginning of name_cache_list.
2134 static void name_cache_clean_unused(struct send_ctx *sctx)
2136 struct name_cache_entry *nce;
2138 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2141 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2142 nce = list_entry(sctx->name_cache_list.next,
2143 struct name_cache_entry, list);
2144 name_cache_delete(sctx, nce);
2149 static void name_cache_free(struct send_ctx *sctx)
2151 struct name_cache_entry *nce;
2153 while (!list_empty(&sctx->name_cache_list)) {
2154 nce = list_entry(sctx->name_cache_list.next,
2155 struct name_cache_entry, list);
2156 name_cache_delete(sctx, nce);
2162 * Used by get_cur_path for each ref up to the root.
2163 * Returns 0 if it succeeded.
2164 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2165 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2166 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2167 * Returns <0 in case of error.
2169 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2173 struct fs_path *dest)
2177 struct name_cache_entry *nce = NULL;
2180 * First check if we already did a call to this function with the same
2181 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2182 * return the cached result.
2184 nce = name_cache_search(sctx, ino, gen);
2186 if (ino < sctx->send_progress && nce->need_later_update) {
2187 name_cache_delete(sctx, nce);
2191 name_cache_used(sctx, nce);
2192 *parent_ino = nce->parent_ino;
2193 *parent_gen = nce->parent_gen;
2194 ret = fs_path_add(dest, nce->name, nce->name_len);
2203 * If the inode is not existent yet, add the orphan name and return 1.
2204 * This should only happen for the parent dir that we determine in
2207 ret = is_inode_existent(sctx, ino, gen);
2212 ret = gen_unique_name(sctx, ino, gen, dest);
2220 * Depending on whether the inode was already processed or not, use
2221 * send_root or parent_root for ref lookup.
2223 if (ino < sctx->send_progress)
2224 ret = get_first_ref(sctx->send_root, ino,
2225 parent_ino, parent_gen, dest);
2227 ret = get_first_ref(sctx->parent_root, ino,
2228 parent_ino, parent_gen, dest);
2233 * Check if the ref was overwritten by an inode's ref that was processed
2234 * earlier. If yes, treat as orphan and return 1.
2236 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2237 dest->start, dest->end - dest->start);
2241 fs_path_reset(dest);
2242 ret = gen_unique_name(sctx, ino, gen, dest);
2250 * Store the result of the lookup in the name cache.
2252 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2260 nce->parent_ino = *parent_ino;
2261 nce->parent_gen = *parent_gen;
2262 nce->name_len = fs_path_len(dest);
2264 strcpy(nce->name, dest->start);
2266 if (ino < sctx->send_progress)
2267 nce->need_later_update = 0;
2269 nce->need_later_update = 1;
2271 nce_ret = name_cache_insert(sctx, nce);
2274 name_cache_clean_unused(sctx);
2281 * Magic happens here. This function returns the first ref to an inode as it
2282 * would look like while receiving the stream at this point in time.
2283 * We walk the path up to the root. For every inode in between, we check if it
2284 * was already processed/sent. If yes, we continue with the parent as found
2285 * in send_root. If not, we continue with the parent as found in parent_root.
2286 * If we encounter an inode that was deleted at this point in time, we use the
2287 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2288 * that were not created yet and overwritten inodes/refs.
2290 * When do we have have orphan inodes:
2291 * 1. When an inode is freshly created and thus no valid refs are available yet
2292 * 2. When a directory lost all it's refs (deleted) but still has dir items
2293 * inside which were not processed yet (pending for move/delete). If anyone
2294 * tried to get the path to the dir items, it would get a path inside that
2296 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2297 * of an unprocessed inode. If in that case the first ref would be
2298 * overwritten, the overwritten inode gets "orphanized". Later when we
2299 * process this overwritten inode, it is restored at a new place by moving
2302 * sctx->send_progress tells this function at which point in time receiving
2305 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2306 struct fs_path *dest)
2309 struct fs_path *name = NULL;
2310 u64 parent_inode = 0;
2314 name = fs_path_alloc();
2321 fs_path_reset(dest);
2323 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2324 struct waiting_dir_move *wdm;
2326 fs_path_reset(name);
2328 if (is_waiting_for_rm(sctx, ino)) {
2329 ret = gen_unique_name(sctx, ino, gen, name);
2332 ret = fs_path_add_path(dest, name);
2336 wdm = get_waiting_dir_move(sctx, ino);
2337 if (wdm && wdm->orphanized) {
2338 ret = gen_unique_name(sctx, ino, gen, name);
2341 ret = get_first_ref(sctx->parent_root, ino,
2342 &parent_inode, &parent_gen, name);
2344 ret = __get_cur_name_and_parent(sctx, ino, gen,
2354 ret = fs_path_add_path(dest, name);
2365 fs_path_unreverse(dest);
2370 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2372 static int send_subvol_begin(struct send_ctx *sctx)
2375 struct btrfs_root *send_root = sctx->send_root;
2376 struct btrfs_root *parent_root = sctx->parent_root;
2377 struct btrfs_path *path;
2378 struct btrfs_key key;
2379 struct btrfs_root_ref *ref;
2380 struct extent_buffer *leaf;
2384 path = btrfs_alloc_path();
2388 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2390 btrfs_free_path(path);
2394 key.objectid = send_root->objectid;
2395 key.type = BTRFS_ROOT_BACKREF_KEY;
2398 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2407 leaf = path->nodes[0];
2408 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2409 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2410 key.objectid != send_root->objectid) {
2414 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2415 namelen = btrfs_root_ref_name_len(leaf, ref);
2416 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2417 btrfs_release_path(path);
2420 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2424 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2429 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2431 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2432 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2433 sctx->send_root->root_item.received_uuid);
2435 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2436 sctx->send_root->root_item.uuid);
2438 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2439 le64_to_cpu(sctx->send_root->root_item.ctransid));
2441 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2442 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2443 parent_root->root_item.received_uuid);
2445 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2446 parent_root->root_item.uuid);
2447 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2448 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2451 ret = send_cmd(sctx);
2455 btrfs_free_path(path);
2460 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2462 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2466 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
2468 p = fs_path_alloc();
2472 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2476 ret = get_cur_path(sctx, ino, gen, p);
2479 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2480 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2482 ret = send_cmd(sctx);
2490 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2492 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2496 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
2498 p = fs_path_alloc();
2502 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2506 ret = get_cur_path(sctx, ino, gen, p);
2509 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2510 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2512 ret = send_cmd(sctx);
2520 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2522 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2526 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2529 p = fs_path_alloc();
2533 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2537 ret = get_cur_path(sctx, ino, gen, p);
2540 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2541 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2542 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2544 ret = send_cmd(sctx);
2552 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2554 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2556 struct fs_path *p = NULL;
2557 struct btrfs_inode_item *ii;
2558 struct btrfs_path *path = NULL;
2559 struct extent_buffer *eb;
2560 struct btrfs_key key;
2563 btrfs_debug(fs_info, "send_utimes %llu", ino);
2565 p = fs_path_alloc();
2569 path = alloc_path_for_send();
2576 key.type = BTRFS_INODE_ITEM_KEY;
2578 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2584 eb = path->nodes[0];
2585 slot = path->slots[0];
2586 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2588 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2592 ret = get_cur_path(sctx, ino, gen, p);
2595 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2596 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2597 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2598 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2599 /* TODO Add otime support when the otime patches get into upstream */
2601 ret = send_cmd(sctx);
2606 btrfs_free_path(path);
2611 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2612 * a valid path yet because we did not process the refs yet. So, the inode
2613 * is created as orphan.
2615 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2617 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2625 btrfs_debug(fs_info, "send_create_inode %llu", ino);
2627 p = fs_path_alloc();
2631 if (ino != sctx->cur_ino) {
2632 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2637 gen = sctx->cur_inode_gen;
2638 mode = sctx->cur_inode_mode;
2639 rdev = sctx->cur_inode_rdev;
2642 if (S_ISREG(mode)) {
2643 cmd = BTRFS_SEND_C_MKFILE;
2644 } else if (S_ISDIR(mode)) {
2645 cmd = BTRFS_SEND_C_MKDIR;
2646 } else if (S_ISLNK(mode)) {
2647 cmd = BTRFS_SEND_C_SYMLINK;
2648 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2649 cmd = BTRFS_SEND_C_MKNOD;
2650 } else if (S_ISFIFO(mode)) {
2651 cmd = BTRFS_SEND_C_MKFIFO;
2652 } else if (S_ISSOCK(mode)) {
2653 cmd = BTRFS_SEND_C_MKSOCK;
2655 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2656 (int)(mode & S_IFMT));
2661 ret = begin_cmd(sctx, cmd);
2665 ret = gen_unique_name(sctx, ino, gen, p);
2669 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2670 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2672 if (S_ISLNK(mode)) {
2674 ret = read_symlink(sctx->send_root, ino, p);
2677 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2678 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2679 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2680 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2681 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2684 ret = send_cmd(sctx);
2696 * We need some special handling for inodes that get processed before the parent
2697 * directory got created. See process_recorded_refs for details.
2698 * This function does the check if we already created the dir out of order.
2700 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2703 struct btrfs_path *path = NULL;
2704 struct btrfs_key key;
2705 struct btrfs_key found_key;
2706 struct btrfs_key di_key;
2707 struct extent_buffer *eb;
2708 struct btrfs_dir_item *di;
2711 path = alloc_path_for_send();
2718 key.type = BTRFS_DIR_INDEX_KEY;
2720 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2725 eb = path->nodes[0];
2726 slot = path->slots[0];
2727 if (slot >= btrfs_header_nritems(eb)) {
2728 ret = btrfs_next_leaf(sctx->send_root, path);
2731 } else if (ret > 0) {
2738 btrfs_item_key_to_cpu(eb, &found_key, slot);
2739 if (found_key.objectid != key.objectid ||
2740 found_key.type != key.type) {
2745 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2746 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2748 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2749 di_key.objectid < sctx->send_progress) {
2758 btrfs_free_path(path);
2763 * Only creates the inode if it is:
2764 * 1. Not a directory
2765 * 2. Or a directory which was not created already due to out of order
2766 * directories. See did_create_dir and process_recorded_refs for details.
2768 static int send_create_inode_if_needed(struct send_ctx *sctx)
2772 if (S_ISDIR(sctx->cur_inode_mode)) {
2773 ret = did_create_dir(sctx, sctx->cur_ino);
2782 ret = send_create_inode(sctx, sctx->cur_ino);
2790 struct recorded_ref {
2791 struct list_head list;
2793 struct fs_path *full_path;
2799 static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2801 ref->full_path = path;
2802 ref->name = (char *)kbasename(ref->full_path->start);
2803 ref->name_len = ref->full_path->end - ref->name;
2807 * We need to process new refs before deleted refs, but compare_tree gives us
2808 * everything mixed. So we first record all refs and later process them.
2809 * This function is a helper to record one ref.
2811 static int __record_ref(struct list_head *head, u64 dir,
2812 u64 dir_gen, struct fs_path *path)
2814 struct recorded_ref *ref;
2816 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
2821 ref->dir_gen = dir_gen;
2822 set_ref_path(ref, path);
2823 list_add_tail(&ref->list, head);
2827 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2829 struct recorded_ref *new;
2831 new = kmalloc(sizeof(*ref), GFP_KERNEL);
2835 new->dir = ref->dir;
2836 new->dir_gen = ref->dir_gen;
2837 new->full_path = NULL;
2838 INIT_LIST_HEAD(&new->list);
2839 list_add_tail(&new->list, list);
2843 static void __free_recorded_refs(struct list_head *head)
2845 struct recorded_ref *cur;
2847 while (!list_empty(head)) {
2848 cur = list_entry(head->next, struct recorded_ref, list);
2849 fs_path_free(cur->full_path);
2850 list_del(&cur->list);
2855 static void free_recorded_refs(struct send_ctx *sctx)
2857 __free_recorded_refs(&sctx->new_refs);
2858 __free_recorded_refs(&sctx->deleted_refs);
2862 * Renames/moves a file/dir to its orphan name. Used when the first
2863 * ref of an unprocessed inode gets overwritten and for all non empty
2866 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2867 struct fs_path *path)
2870 struct fs_path *orphan;
2872 orphan = fs_path_alloc();
2876 ret = gen_unique_name(sctx, ino, gen, orphan);
2880 ret = send_rename(sctx, path, orphan);
2883 fs_path_free(orphan);
2887 static struct orphan_dir_info *
2888 add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2890 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2891 struct rb_node *parent = NULL;
2892 struct orphan_dir_info *entry, *odi;
2894 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2896 return ERR_PTR(-ENOMEM);
2902 entry = rb_entry(parent, struct orphan_dir_info, node);
2903 if (dir_ino < entry->ino) {
2905 } else if (dir_ino > entry->ino) {
2906 p = &(*p)->rb_right;
2913 rb_link_node(&odi->node, parent, p);
2914 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2918 static struct orphan_dir_info *
2919 get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2921 struct rb_node *n = sctx->orphan_dirs.rb_node;
2922 struct orphan_dir_info *entry;
2925 entry = rb_entry(n, struct orphan_dir_info, node);
2926 if (dir_ino < entry->ino)
2928 else if (dir_ino > entry->ino)
2936 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2938 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2943 static void free_orphan_dir_info(struct send_ctx *sctx,
2944 struct orphan_dir_info *odi)
2948 rb_erase(&odi->node, &sctx->orphan_dirs);
2953 * Returns 1 if a directory can be removed at this point in time.
2954 * We check this by iterating all dir items and checking if the inode behind
2955 * the dir item was already processed.
2957 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2961 struct btrfs_root *root = sctx->parent_root;
2962 struct btrfs_path *path;
2963 struct btrfs_key key;
2964 struct btrfs_key found_key;
2965 struct btrfs_key loc;
2966 struct btrfs_dir_item *di;
2969 * Don't try to rmdir the top/root subvolume dir.
2971 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2974 path = alloc_path_for_send();
2979 key.type = BTRFS_DIR_INDEX_KEY;
2981 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2986 struct waiting_dir_move *dm;
2988 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2989 ret = btrfs_next_leaf(root, path);
2996 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2998 if (found_key.objectid != key.objectid ||
2999 found_key.type != key.type)
3002 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
3003 struct btrfs_dir_item);
3004 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
3006 dm = get_waiting_dir_move(sctx, loc.objectid);
3008 struct orphan_dir_info *odi;
3010 odi = add_orphan_dir_info(sctx, dir);
3016 dm->rmdir_ino = dir;
3021 if (loc.objectid > send_progress) {
3022 struct orphan_dir_info *odi;
3024 odi = get_orphan_dir_info(sctx, dir);
3025 free_orphan_dir_info(sctx, odi);
3036 btrfs_free_path(path);
3040 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3042 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3044 return entry != NULL;
3047 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3049 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3050 struct rb_node *parent = NULL;
3051 struct waiting_dir_move *entry, *dm;
3053 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
3058 dm->orphanized = orphanized;
3062 entry = rb_entry(parent, struct waiting_dir_move, node);
3063 if (ino < entry->ino) {
3065 } else if (ino > entry->ino) {
3066 p = &(*p)->rb_right;
3073 rb_link_node(&dm->node, parent, p);
3074 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3078 static struct waiting_dir_move *
3079 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3081 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3082 struct waiting_dir_move *entry;
3085 entry = rb_entry(n, struct waiting_dir_move, node);
3086 if (ino < entry->ino)
3088 else if (ino > entry->ino)
3096 static void free_waiting_dir_move(struct send_ctx *sctx,
3097 struct waiting_dir_move *dm)
3101 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3105 static int add_pending_dir_move(struct send_ctx *sctx,
3109 struct list_head *new_refs,
3110 struct list_head *deleted_refs,
3111 const bool is_orphan)
3113 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3114 struct rb_node *parent = NULL;
3115 struct pending_dir_move *entry = NULL, *pm;
3116 struct recorded_ref *cur;
3120 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3123 pm->parent_ino = parent_ino;
3126 INIT_LIST_HEAD(&pm->list);
3127 INIT_LIST_HEAD(&pm->update_refs);
3128 RB_CLEAR_NODE(&pm->node);
3132 entry = rb_entry(parent, struct pending_dir_move, node);
3133 if (parent_ino < entry->parent_ino) {
3135 } else if (parent_ino > entry->parent_ino) {
3136 p = &(*p)->rb_right;
3143 list_for_each_entry(cur, deleted_refs, list) {
3144 ret = dup_ref(cur, &pm->update_refs);
3148 list_for_each_entry(cur, new_refs, list) {
3149 ret = dup_ref(cur, &pm->update_refs);
3154 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3159 list_add_tail(&pm->list, &entry->list);
3161 rb_link_node(&pm->node, parent, p);
3162 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3167 __free_recorded_refs(&pm->update_refs);
3173 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3176 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3177 struct pending_dir_move *entry;
3180 entry = rb_entry(n, struct pending_dir_move, node);
3181 if (parent_ino < entry->parent_ino)
3183 else if (parent_ino > entry->parent_ino)
3191 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3192 u64 ino, u64 gen, u64 *ancestor_ino)
3195 u64 parent_inode = 0;
3197 u64 start_ino = ino;
3200 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3201 fs_path_reset(name);
3203 if (is_waiting_for_rm(sctx, ino))
3205 if (is_waiting_for_move(sctx, ino)) {
3206 if (*ancestor_ino == 0)
3207 *ancestor_ino = ino;
3208 ret = get_first_ref(sctx->parent_root, ino,
3209 &parent_inode, &parent_gen, name);
3211 ret = __get_cur_name_and_parent(sctx, ino, gen,
3221 if (parent_inode == start_ino) {
3223 if (*ancestor_ino == 0)
3224 *ancestor_ino = ino;
3233 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3235 struct fs_path *from_path = NULL;
3236 struct fs_path *to_path = NULL;
3237 struct fs_path *name = NULL;
3238 u64 orig_progress = sctx->send_progress;
3239 struct recorded_ref *cur;
3240 u64 parent_ino, parent_gen;
3241 struct waiting_dir_move *dm = NULL;
3247 name = fs_path_alloc();
3248 from_path = fs_path_alloc();
3249 if (!name || !from_path) {
3254 dm = get_waiting_dir_move(sctx, pm->ino);
3256 rmdir_ino = dm->rmdir_ino;
3257 is_orphan = dm->orphanized;
3258 free_waiting_dir_move(sctx, dm);
3261 ret = gen_unique_name(sctx, pm->ino,
3262 pm->gen, from_path);
3264 ret = get_first_ref(sctx->parent_root, pm->ino,
3265 &parent_ino, &parent_gen, name);
3268 ret = get_cur_path(sctx, parent_ino, parent_gen,
3272 ret = fs_path_add_path(from_path, name);
3277 sctx->send_progress = sctx->cur_ino + 1;
3278 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3282 LIST_HEAD(deleted_refs);
3283 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3284 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3285 &pm->update_refs, &deleted_refs,
3290 dm = get_waiting_dir_move(sctx, pm->ino);
3292 dm->rmdir_ino = rmdir_ino;
3296 fs_path_reset(name);
3299 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3303 ret = send_rename(sctx, from_path, to_path);
3308 struct orphan_dir_info *odi;
3310 odi = get_orphan_dir_info(sctx, rmdir_ino);
3312 /* already deleted */
3315 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino);
3321 name = fs_path_alloc();
3326 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3329 ret = send_rmdir(sctx, name);
3332 free_orphan_dir_info(sctx, odi);
3336 ret = send_utimes(sctx, pm->ino, pm->gen);
3341 * After rename/move, need to update the utimes of both new parent(s)
3342 * and old parent(s).
3344 list_for_each_entry(cur, &pm->update_refs, list) {
3346 * The parent inode might have been deleted in the send snapshot
3348 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3349 NULL, NULL, NULL, NULL, NULL);
3350 if (ret == -ENOENT) {
3357 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3364 fs_path_free(from_path);
3365 fs_path_free(to_path);
3366 sctx->send_progress = orig_progress;
3371 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3373 if (!list_empty(&m->list))
3375 if (!RB_EMPTY_NODE(&m->node))
3376 rb_erase(&m->node, &sctx->pending_dir_moves);
3377 __free_recorded_refs(&m->update_refs);
3381 static void tail_append_pending_moves(struct send_ctx *sctx,
3382 struct pending_dir_move *moves,
3383 struct list_head *stack)
3385 if (list_empty(&moves->list)) {
3386 list_add_tail(&moves->list, stack);
3389 list_splice_init(&moves->list, &list);
3390 list_add_tail(&moves->list, stack);
3391 list_splice_tail(&list, stack);
3393 if (!RB_EMPTY_NODE(&moves->node)) {
3394 rb_erase(&moves->node, &sctx->pending_dir_moves);
3395 RB_CLEAR_NODE(&moves->node);
3399 static int apply_children_dir_moves(struct send_ctx *sctx)
3401 struct pending_dir_move *pm;
3402 struct list_head stack;
3403 u64 parent_ino = sctx->cur_ino;
3406 pm = get_pending_dir_moves(sctx, parent_ino);
3410 INIT_LIST_HEAD(&stack);
3411 tail_append_pending_moves(sctx, pm, &stack);
3413 while (!list_empty(&stack)) {
3414 pm = list_first_entry(&stack, struct pending_dir_move, list);
3415 parent_ino = pm->ino;
3416 ret = apply_dir_move(sctx, pm);
3417 free_pending_move(sctx, pm);
3420 pm = get_pending_dir_moves(sctx, parent_ino);
3422 tail_append_pending_moves(sctx, pm, &stack);
3427 while (!list_empty(&stack)) {
3428 pm = list_first_entry(&stack, struct pending_dir_move, list);
3429 free_pending_move(sctx, pm);
3435 * We might need to delay a directory rename even when no ancestor directory
3436 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3437 * renamed. This happens when we rename a directory to the old name (the name
3438 * in the parent root) of some other unrelated directory that got its rename
3439 * delayed due to some ancestor with higher number that got renamed.
3445 * |---- a/ (ino 257)
3446 * | |---- file (ino 260)
3448 * |---- b/ (ino 258)
3449 * |---- c/ (ino 259)
3453 * |---- a/ (ino 258)
3454 * |---- x/ (ino 259)
3455 * |---- y/ (ino 257)
3456 * |----- file (ino 260)
3458 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3459 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3460 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3463 * 1 - rename 259 from 'c' to 'x'
3464 * 2 - rename 257 from 'a' to 'x/y'
3465 * 3 - rename 258 from 'b' to 'a'
3467 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3468 * be done right away and < 0 on error.
3470 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3471 struct recorded_ref *parent_ref,
3472 const bool is_orphan)
3474 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3475 struct btrfs_path *path;
3476 struct btrfs_key key;
3477 struct btrfs_key di_key;
3478 struct btrfs_dir_item *di;
3482 struct waiting_dir_move *wdm;
3484 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3487 path = alloc_path_for_send();
3491 key.objectid = parent_ref->dir;
3492 key.type = BTRFS_DIR_ITEM_KEY;
3493 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3495 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3498 } else if (ret > 0) {
3503 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3504 parent_ref->name_len);
3510 * di_key.objectid has the number of the inode that has a dentry in the
3511 * parent directory with the same name that sctx->cur_ino is being
3512 * renamed to. We need to check if that inode is in the send root as
3513 * well and if it is currently marked as an inode with a pending rename,
3514 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3515 * that it happens after that other inode is renamed.
3517 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3518 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3523 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3524 &left_gen, NULL, NULL, NULL, NULL);
3527 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3528 &right_gen, NULL, NULL, NULL, NULL);
3535 /* Different inode, no need to delay the rename of sctx->cur_ino */
3536 if (right_gen != left_gen) {
3541 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3542 if (wdm && !wdm->orphanized) {
3543 ret = add_pending_dir_move(sctx,
3545 sctx->cur_inode_gen,
3548 &sctx->deleted_refs,
3554 btrfs_free_path(path);
3559 * Check if inode ino2, or any of its ancestors, is inode ino1.
3560 * Return 1 if true, 0 if false and < 0 on error.
3562 static int check_ino_in_path(struct btrfs_root *root,
3567 struct fs_path *fs_path)
3572 return ino1_gen == ino2_gen;
3574 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3579 fs_path_reset(fs_path);
3580 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3584 return parent_gen == ino1_gen;
3591 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3592 * possible path (in case ino2 is not a directory and has multiple hard links).
3593 * Return 1 if true, 0 if false and < 0 on error.
3595 static int is_ancestor(struct btrfs_root *root,
3599 struct fs_path *fs_path)
3601 bool free_fs_path = false;
3603 struct btrfs_path *path = NULL;
3604 struct btrfs_key key;
3607 fs_path = fs_path_alloc();
3610 free_fs_path = true;
3613 path = alloc_path_for_send();
3619 key.objectid = ino2;
3620 key.type = BTRFS_INODE_REF_KEY;
3623 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3628 struct extent_buffer *leaf = path->nodes[0];
3629 int slot = path->slots[0];
3633 if (slot >= btrfs_header_nritems(leaf)) {
3634 ret = btrfs_next_leaf(root, path);
3642 btrfs_item_key_to_cpu(leaf, &key, slot);
3643 if (key.objectid != ino2)
3645 if (key.type != BTRFS_INODE_REF_KEY &&
3646 key.type != BTRFS_INODE_EXTREF_KEY)
3649 item_size = btrfs_item_size_nr(leaf, slot);
3650 while (cur_offset < item_size) {
3654 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3656 struct btrfs_inode_extref *extref;
3658 ptr = btrfs_item_ptr_offset(leaf, slot);
3659 extref = (struct btrfs_inode_extref *)
3661 parent = btrfs_inode_extref_parent(leaf,
3663 cur_offset += sizeof(*extref);
3664 cur_offset += btrfs_inode_extref_name_len(leaf,
3667 parent = key.offset;
3668 cur_offset = item_size;
3671 ret = get_inode_info(root, parent, NULL, &parent_gen,
3672 NULL, NULL, NULL, NULL);
3675 ret = check_ino_in_path(root, ino1, ino1_gen,
3676 parent, parent_gen, fs_path);
3684 btrfs_free_path(path);
3686 fs_path_free(fs_path);
3690 static int wait_for_parent_move(struct send_ctx *sctx,
3691 struct recorded_ref *parent_ref,
3692 const bool is_orphan)
3695 u64 ino = parent_ref->dir;
3696 u64 ino_gen = parent_ref->dir_gen;
3697 u64 parent_ino_before, parent_ino_after;
3698 struct fs_path *path_before = NULL;
3699 struct fs_path *path_after = NULL;
3702 path_after = fs_path_alloc();
3703 path_before = fs_path_alloc();
3704 if (!path_after || !path_before) {
3710 * Our current directory inode may not yet be renamed/moved because some
3711 * ancestor (immediate or not) has to be renamed/moved first. So find if
3712 * such ancestor exists and make sure our own rename/move happens after
3713 * that ancestor is processed to avoid path build infinite loops (done
3714 * at get_cur_path()).
3716 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3717 u64 parent_ino_after_gen;
3719 if (is_waiting_for_move(sctx, ino)) {
3721 * If the current inode is an ancestor of ino in the
3722 * parent root, we need to delay the rename of the
3723 * current inode, otherwise don't delayed the rename
3724 * because we can end up with a circular dependency
3725 * of renames, resulting in some directories never
3726 * getting the respective rename operations issued in
3727 * the send stream or getting into infinite path build
3730 ret = is_ancestor(sctx->parent_root,
3731 sctx->cur_ino, sctx->cur_inode_gen,
3737 fs_path_reset(path_before);
3738 fs_path_reset(path_after);
3740 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3741 &parent_ino_after_gen, path_after);
3744 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3746 if (ret < 0 && ret != -ENOENT) {
3748 } else if (ret == -ENOENT) {
3753 len1 = fs_path_len(path_before);
3754 len2 = fs_path_len(path_after);
3755 if (ino > sctx->cur_ino &&
3756 (parent_ino_before != parent_ino_after || len1 != len2 ||
3757 memcmp(path_before->start, path_after->start, len1))) {
3760 ret = get_inode_info(sctx->parent_root, ino, NULL,
3761 &parent_ino_gen, NULL, NULL, NULL,
3765 if (ino_gen == parent_ino_gen) {
3770 ino = parent_ino_after;
3771 ino_gen = parent_ino_after_gen;
3775 fs_path_free(path_before);
3776 fs_path_free(path_after);
3779 ret = add_pending_dir_move(sctx,
3781 sctx->cur_inode_gen,
3784 &sctx->deleted_refs,
3793 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3796 struct fs_path *new_path;
3799 * Our reference's name member points to its full_path member string, so
3800 * we use here a new path.
3802 new_path = fs_path_alloc();
3806 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3808 fs_path_free(new_path);
3811 ret = fs_path_add(new_path, ref->name, ref->name_len);
3813 fs_path_free(new_path);
3817 fs_path_free(ref->full_path);
3818 set_ref_path(ref, new_path);
3824 * When processing the new references for an inode we may orphanize an existing
3825 * directory inode because its old name conflicts with one of the new references
3826 * of the current inode. Later, when processing another new reference of our
3827 * inode, we might need to orphanize another inode, but the path we have in the
3828 * reference reflects the pre-orphanization name of the directory we previously
3829 * orphanized. For example:
3831 * parent snapshot looks like:
3834 * |----- f1 (ino 257)
3835 * |----- f2 (ino 258)
3836 * |----- d1/ (ino 259)
3837 * |----- d2/ (ino 260)
3839 * send snapshot looks like:
3842 * |----- d1 (ino 258)
3843 * |----- f2/ (ino 259)
3844 * |----- f2_link/ (ino 260)
3845 * | |----- f1 (ino 257)
3847 * |----- d2 (ino 258)
3849 * When processing inode 257 we compute the name for inode 259 as "d1", and we
3850 * cache it in the name cache. Later when we start processing inode 258, when
3851 * collecting all its new references we set a full path of "d1/d2" for its new
3852 * reference with name "d2". When we start processing the new references we
3853 * start by processing the new reference with name "d1", and this results in
3854 * orphanizing inode 259, since its old reference causes a conflict. Then we
3855 * move on the next new reference, with name "d2", and we find out we must
3856 * orphanize inode 260, as its old reference conflicts with ours - but for the
3857 * orphanization we use a source path corresponding to the path we stored in the
3858 * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
3859 * receiver fail since the path component "d1/" no longer exists, it was renamed
3860 * to "o259-6-0/" when processing the previous new reference. So in this case we
3861 * must recompute the path in the new reference and use it for the new
3862 * orphanization operation.
3864 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3869 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3873 fs_path_reset(ref->full_path);
3874 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3878 ret = fs_path_add(ref->full_path, name, ref->name_len);
3882 /* Update the reference's base name pointer. */
3883 set_ref_path(ref, ref->full_path);
3890 * This does all the move/link/unlink/rmdir magic.
3892 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3894 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
3896 struct recorded_ref *cur;
3897 struct recorded_ref *cur2;
3898 struct list_head check_dirs;
3899 struct fs_path *valid_path = NULL;
3903 int did_overwrite = 0;
3905 u64 last_dir_ino_rm = 0;
3906 bool can_rename = true;
3907 bool orphanized_dir = false;
3908 bool orphanized_ancestor = false;
3910 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
3913 * This should never happen as the root dir always has the same ref
3914 * which is always '..'
3916 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3917 INIT_LIST_HEAD(&check_dirs);
3919 valid_path = fs_path_alloc();
3926 * First, check if the first ref of the current inode was overwritten
3927 * before. If yes, we know that the current inode was already orphanized
3928 * and thus use the orphan name. If not, we can use get_cur_path to
3929 * get the path of the first ref as it would like while receiving at
3930 * this point in time.
3931 * New inodes are always orphan at the beginning, so force to use the
3932 * orphan name in this case.
3933 * The first ref is stored in valid_path and will be updated if it
3934 * gets moved around.
3936 if (!sctx->cur_inode_new) {
3937 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3938 sctx->cur_inode_gen);
3944 if (sctx->cur_inode_new || did_overwrite) {
3945 ret = gen_unique_name(sctx, sctx->cur_ino,
3946 sctx->cur_inode_gen, valid_path);
3951 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3957 list_for_each_entry(cur, &sctx->new_refs, list) {
3959 * We may have refs where the parent directory does not exist
3960 * yet. This happens if the parent directories inum is higher
3961 * the the current inum. To handle this case, we create the
3962 * parent directory out of order. But we need to check if this
3963 * did already happen before due to other refs in the same dir.
3965 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3968 if (ret == inode_state_will_create) {
3971 * First check if any of the current inodes refs did
3972 * already create the dir.
3974 list_for_each_entry(cur2, &sctx->new_refs, list) {
3977 if (cur2->dir == cur->dir) {
3984 * If that did not happen, check if a previous inode
3985 * did already create the dir.
3988 ret = did_create_dir(sctx, cur->dir);
3992 ret = send_create_inode(sctx, cur->dir);
3999 * Check if this new ref would overwrite the first ref of
4000 * another unprocessed inode. If yes, orphanize the
4001 * overwritten inode. If we find an overwritten ref that is
4002 * not the first ref, simply unlink it.
4004 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4005 cur->name, cur->name_len,
4006 &ow_inode, &ow_gen, &ow_mode);
4010 ret = is_first_ref(sctx->parent_root,
4011 ow_inode, cur->dir, cur->name,
4016 struct name_cache_entry *nce;
4017 struct waiting_dir_move *wdm;
4019 if (orphanized_dir) {
4020 ret = refresh_ref_path(sctx, cur);
4025 ret = orphanize_inode(sctx, ow_inode, ow_gen,
4029 if (S_ISDIR(ow_mode))
4030 orphanized_dir = true;
4033 * If ow_inode has its rename operation delayed
4034 * make sure that its orphanized name is used in
4035 * the source path when performing its rename
4038 if (is_waiting_for_move(sctx, ow_inode)) {
4039 wdm = get_waiting_dir_move(sctx,
4042 wdm->orphanized = true;
4046 * Make sure we clear our orphanized inode's
4047 * name from the name cache. This is because the
4048 * inode ow_inode might be an ancestor of some
4049 * other inode that will be orphanized as well
4050 * later and has an inode number greater than
4051 * sctx->send_progress. We need to prevent
4052 * future name lookups from using the old name
4053 * and get instead the orphan name.
4055 nce = name_cache_search(sctx, ow_inode, ow_gen);
4057 name_cache_delete(sctx, nce);
4062 * ow_inode might currently be an ancestor of
4063 * cur_ino, therefore compute valid_path (the
4064 * current path of cur_ino) again because it
4065 * might contain the pre-orphanization name of
4066 * ow_inode, which is no longer valid.
4068 ret = is_ancestor(sctx->parent_root,
4070 sctx->cur_ino, NULL);
4072 orphanized_ancestor = true;
4073 fs_path_reset(valid_path);
4074 ret = get_cur_path(sctx, sctx->cur_ino,
4075 sctx->cur_inode_gen,
4082 * If we previously orphanized a directory that
4083 * collided with a new reference that we already
4084 * processed, recompute the current path because
4085 * that directory may be part of the path.
4087 if (orphanized_dir) {
4088 ret = refresh_ref_path(sctx, cur);
4092 ret = send_unlink(sctx, cur->full_path);
4098 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4099 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4108 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4110 ret = wait_for_parent_move(sctx, cur, is_orphan);
4120 * link/move the ref to the new place. If we have an orphan
4121 * inode, move it and update valid_path. If not, link or move
4122 * it depending on the inode mode.
4124 if (is_orphan && can_rename) {
4125 ret = send_rename(sctx, valid_path, cur->full_path);
4129 ret = fs_path_copy(valid_path, cur->full_path);
4132 } else if (can_rename) {
4133 if (S_ISDIR(sctx->cur_inode_mode)) {
4135 * Dirs can't be linked, so move it. For moved
4136 * dirs, we always have one new and one deleted
4137 * ref. The deleted ref is ignored later.
4139 ret = send_rename(sctx, valid_path,
4142 ret = fs_path_copy(valid_path,
4148 * We might have previously orphanized an inode
4149 * which is an ancestor of our current inode,
4150 * so our reference's full path, which was
4151 * computed before any such orphanizations, must
4154 if (orphanized_dir) {
4155 ret = update_ref_path(sctx, cur);
4159 ret = send_link(sctx, cur->full_path,
4165 ret = dup_ref(cur, &check_dirs);
4170 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4172 * Check if we can already rmdir the directory. If not,
4173 * orphanize it. For every dir item inside that gets deleted
4174 * later, we do this check again and rmdir it then if possible.
4175 * See the use of check_dirs for more details.
4177 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4182 ret = send_rmdir(sctx, valid_path);
4185 } else if (!is_orphan) {
4186 ret = orphanize_inode(sctx, sctx->cur_ino,
4187 sctx->cur_inode_gen, valid_path);
4193 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4194 ret = dup_ref(cur, &check_dirs);
4198 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4199 !list_empty(&sctx->deleted_refs)) {
4201 * We have a moved dir. Add the old parent to check_dirs
4203 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4205 ret = dup_ref(cur, &check_dirs);
4208 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4210 * We have a non dir inode. Go through all deleted refs and
4211 * unlink them if they were not already overwritten by other
4214 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4215 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4216 sctx->cur_ino, sctx->cur_inode_gen,
4217 cur->name, cur->name_len);
4222 * If we orphanized any ancestor before, we need
4223 * to recompute the full path for deleted names,
4224 * since any such path was computed before we
4225 * processed any references and orphanized any
4228 if (orphanized_ancestor) {
4229 ret = update_ref_path(sctx, cur);
4233 ret = send_unlink(sctx, cur->full_path);
4237 ret = dup_ref(cur, &check_dirs);
4242 * If the inode is still orphan, unlink the orphan. This may
4243 * happen when a previous inode did overwrite the first ref
4244 * of this inode and no new refs were added for the current
4245 * inode. Unlinking does not mean that the inode is deleted in
4246 * all cases. There may still be links to this inode in other
4250 ret = send_unlink(sctx, valid_path);
4257 * We did collect all parent dirs where cur_inode was once located. We
4258 * now go through all these dirs and check if they are pending for
4259 * deletion and if it's finally possible to perform the rmdir now.
4260 * We also update the inode stats of the parent dirs here.
4262 list_for_each_entry(cur, &check_dirs, list) {
4264 * In case we had refs into dirs that were not processed yet,
4265 * we don't need to do the utime and rmdir logic for these dirs.
4266 * The dir will be processed later.
4268 if (cur->dir > sctx->cur_ino)
4271 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4275 if (ret == inode_state_did_create ||
4276 ret == inode_state_no_change) {
4277 /* TODO delayed utimes */
4278 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
4281 } else if (ret == inode_state_did_delete &&
4282 cur->dir != last_dir_ino_rm) {
4283 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4288 ret = get_cur_path(sctx, cur->dir,
4289 cur->dir_gen, valid_path);
4292 ret = send_rmdir(sctx, valid_path);
4295 last_dir_ino_rm = cur->dir;
4303 __free_recorded_refs(&check_dirs);
4304 free_recorded_refs(sctx);
4305 fs_path_free(valid_path);
4309 static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
4310 struct fs_path *name, void *ctx, struct list_head *refs)
4313 struct send_ctx *sctx = ctx;
4317 p = fs_path_alloc();
4321 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
4326 ret = get_cur_path(sctx, dir, gen, p);
4329 ret = fs_path_add_path(p, name);
4333 ret = __record_ref(refs, dir, gen, p);
4341 static int __record_new_ref(int num, u64 dir, int index,
4342 struct fs_path *name,
4345 struct send_ctx *sctx = ctx;
4346 return record_ref(sctx->send_root, num, dir, index, name,
4347 ctx, &sctx->new_refs);
4351 static int __record_deleted_ref(int num, u64 dir, int index,
4352 struct fs_path *name,
4355 struct send_ctx *sctx = ctx;
4356 return record_ref(sctx->parent_root, num, dir, index, name,
4357 ctx, &sctx->deleted_refs);
4360 static int record_new_ref(struct send_ctx *sctx)
4364 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4365 sctx->cmp_key, 0, __record_new_ref, sctx);
4374 static int record_deleted_ref(struct send_ctx *sctx)
4378 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4379 sctx->cmp_key, 0, __record_deleted_ref, sctx);
4388 struct find_ref_ctx {
4391 struct btrfs_root *root;
4392 struct fs_path *name;
4396 static int __find_iref(int num, u64 dir, int index,
4397 struct fs_path *name,
4400 struct find_ref_ctx *ctx = ctx_;
4404 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4405 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
4407 * To avoid doing extra lookups we'll only do this if everything
4410 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4414 if (dir_gen != ctx->dir_gen)
4416 ctx->found_idx = num;
4422 static int find_iref(struct btrfs_root *root,
4423 struct btrfs_path *path,
4424 struct btrfs_key *key,
4425 u64 dir, u64 dir_gen, struct fs_path *name)
4428 struct find_ref_ctx ctx;
4432 ctx.dir_gen = dir_gen;
4436 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
4440 if (ctx.found_idx == -1)
4443 return ctx.found_idx;
4446 static int __record_changed_new_ref(int num, u64 dir, int index,
4447 struct fs_path *name,
4452 struct send_ctx *sctx = ctx;
4454 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4459 ret = find_iref(sctx->parent_root, sctx->right_path,
4460 sctx->cmp_key, dir, dir_gen, name);
4462 ret = __record_new_ref(num, dir, index, name, sctx);
4469 static int __record_changed_deleted_ref(int num, u64 dir, int index,
4470 struct fs_path *name,
4475 struct send_ctx *sctx = ctx;
4477 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4482 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
4483 dir, dir_gen, name);
4485 ret = __record_deleted_ref(num, dir, index, name, sctx);
4492 static int record_changed_ref(struct send_ctx *sctx)
4496 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4497 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4500 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4501 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4511 * Record and process all refs at once. Needed when an inode changes the
4512 * generation number, which means that it was deleted and recreated.
4514 static int process_all_refs(struct send_ctx *sctx,
4515 enum btrfs_compare_tree_result cmd)
4518 struct btrfs_root *root;
4519 struct btrfs_path *path;
4520 struct btrfs_key key;
4521 struct btrfs_key found_key;
4522 struct extent_buffer *eb;
4524 iterate_inode_ref_t cb;
4525 int pending_move = 0;
4527 path = alloc_path_for_send();
4531 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4532 root = sctx->send_root;
4533 cb = __record_new_ref;
4534 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4535 root = sctx->parent_root;
4536 cb = __record_deleted_ref;
4538 btrfs_err(sctx->send_root->fs_info,
4539 "Wrong command %d in process_all_refs", cmd);
4544 key.objectid = sctx->cmp_key->objectid;
4545 key.type = BTRFS_INODE_REF_KEY;
4547 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4552 eb = path->nodes[0];
4553 slot = path->slots[0];
4554 if (slot >= btrfs_header_nritems(eb)) {
4555 ret = btrfs_next_leaf(root, path);
4563 btrfs_item_key_to_cpu(eb, &found_key, slot);
4565 if (found_key.objectid != key.objectid ||
4566 (found_key.type != BTRFS_INODE_REF_KEY &&
4567 found_key.type != BTRFS_INODE_EXTREF_KEY))
4570 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4576 btrfs_release_path(path);
4579 * We don't actually care about pending_move as we are simply
4580 * re-creating this inode and will be rename'ing it into place once we
4581 * rename the parent directory.
4583 ret = process_recorded_refs(sctx, &pending_move);
4585 btrfs_free_path(path);
4589 static int send_set_xattr(struct send_ctx *sctx,
4590 struct fs_path *path,
4591 const char *name, int name_len,
4592 const char *data, int data_len)
4596 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4600 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4601 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4602 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4604 ret = send_cmd(sctx);
4611 static int send_remove_xattr(struct send_ctx *sctx,
4612 struct fs_path *path,
4613 const char *name, int name_len)
4617 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4621 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4622 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4624 ret = send_cmd(sctx);
4631 static int __process_new_xattr(int num, struct btrfs_key *di_key,
4632 const char *name, int name_len,
4633 const char *data, int data_len,
4637 struct send_ctx *sctx = ctx;
4639 struct posix_acl_xattr_header dummy_acl;
4641 /* Capabilities are emitted by finish_inode_if_needed */
4642 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4645 p = fs_path_alloc();
4650 * This hack is needed because empty acls are stored as zero byte
4651 * data in xattrs. Problem with that is, that receiving these zero byte
4652 * acls will fail later. To fix this, we send a dummy acl list that
4653 * only contains the version number and no entries.
4655 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4656 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4657 if (data_len == 0) {
4658 dummy_acl.a_version =
4659 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4660 data = (char *)&dummy_acl;
4661 data_len = sizeof(dummy_acl);
4665 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4669 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4676 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4677 const char *name, int name_len,
4678 const char *data, int data_len,
4682 struct send_ctx *sctx = ctx;
4685 p = fs_path_alloc();
4689 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4693 ret = send_remove_xattr(sctx, p, name, name_len);
4700 static int process_new_xattr(struct send_ctx *sctx)
4704 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4705 sctx->cmp_key, __process_new_xattr, sctx);
4710 static int process_deleted_xattr(struct send_ctx *sctx)
4712 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4713 sctx->cmp_key, __process_deleted_xattr, sctx);
4716 struct find_xattr_ctx {
4724 static int __find_xattr(int num, struct btrfs_key *di_key,
4725 const char *name, int name_len,
4726 const char *data, int data_len,
4727 u8 type, void *vctx)
4729 struct find_xattr_ctx *ctx = vctx;
4731 if (name_len == ctx->name_len &&
4732 strncmp(name, ctx->name, name_len) == 0) {
4733 ctx->found_idx = num;
4734 ctx->found_data_len = data_len;
4735 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4736 if (!ctx->found_data)
4743 static int find_xattr(struct btrfs_root *root,
4744 struct btrfs_path *path,
4745 struct btrfs_key *key,
4746 const char *name, int name_len,
4747 char **data, int *data_len)
4750 struct find_xattr_ctx ctx;
4753 ctx.name_len = name_len;
4755 ctx.found_data = NULL;
4756 ctx.found_data_len = 0;
4758 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
4762 if (ctx.found_idx == -1)
4765 *data = ctx.found_data;
4766 *data_len = ctx.found_data_len;
4768 kfree(ctx.found_data);
4770 return ctx.found_idx;
4774 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4775 const char *name, int name_len,
4776 const char *data, int data_len,
4780 struct send_ctx *sctx = ctx;
4781 char *found_data = NULL;
4782 int found_data_len = 0;
4784 ret = find_xattr(sctx->parent_root, sctx->right_path,
4785 sctx->cmp_key, name, name_len, &found_data,
4787 if (ret == -ENOENT) {
4788 ret = __process_new_xattr(num, di_key, name, name_len, data,
4789 data_len, type, ctx);
4790 } else if (ret >= 0) {
4791 if (data_len != found_data_len ||
4792 memcmp(data, found_data, data_len)) {
4793 ret = __process_new_xattr(num, di_key, name, name_len,
4794 data, data_len, type, ctx);
4804 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4805 const char *name, int name_len,
4806 const char *data, int data_len,
4810 struct send_ctx *sctx = ctx;
4812 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4813 name, name_len, NULL, NULL);
4815 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4816 data_len, type, ctx);
4823 static int process_changed_xattr(struct send_ctx *sctx)
4827 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4828 sctx->cmp_key, __process_changed_new_xattr, sctx);
4831 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4832 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4838 static int process_all_new_xattrs(struct send_ctx *sctx)
4841 struct btrfs_root *root;
4842 struct btrfs_path *path;
4843 struct btrfs_key key;
4844 struct btrfs_key found_key;
4845 struct extent_buffer *eb;
4848 path = alloc_path_for_send();
4852 root = sctx->send_root;
4854 key.objectid = sctx->cmp_key->objectid;
4855 key.type = BTRFS_XATTR_ITEM_KEY;
4857 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4862 eb = path->nodes[0];
4863 slot = path->slots[0];
4864 if (slot >= btrfs_header_nritems(eb)) {
4865 ret = btrfs_next_leaf(root, path);
4868 } else if (ret > 0) {
4875 btrfs_item_key_to_cpu(eb, &found_key, slot);
4876 if (found_key.objectid != key.objectid ||
4877 found_key.type != key.type) {
4882 ret = iterate_dir_item(root, path, &found_key,
4883 __process_new_xattr, sctx);
4891 btrfs_free_path(path);
4895 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4897 struct btrfs_root *root = sctx->send_root;
4898 struct btrfs_fs_info *fs_info = root->fs_info;
4899 struct inode *inode;
4902 struct btrfs_key key;
4903 pgoff_t index = offset >> PAGE_SHIFT;
4905 unsigned pg_offset = offset & ~PAGE_MASK;
4908 key.objectid = sctx->cur_ino;
4909 key.type = BTRFS_INODE_ITEM_KEY;
4912 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4914 return PTR_ERR(inode);
4916 if (offset + len > i_size_read(inode)) {
4917 if (offset > i_size_read(inode))
4920 len = offset - i_size_read(inode);
4925 last_index = (offset + len - 1) >> PAGE_SHIFT;
4927 /* initial readahead */
4928 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4929 file_ra_state_init(&sctx->ra, inode->i_mapping);
4930 page_cache_sync_readahead(inode->i_mapping, &sctx->ra, NULL, index,
4931 last_index - index + 1);
4933 while (index <= last_index) {
4934 unsigned cur_len = min_t(unsigned, len,
4935 PAGE_SIZE - pg_offset);
4936 page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
4942 if (!PageUptodate(page)) {
4943 btrfs_readpage(NULL, page);
4945 if (!PageUptodate(page)) {
4948 "send: IO error at offset %llu for inode %llu root %llu",
4949 page_offset(page), sctx->cur_ino,
4950 sctx->send_root->root_key.objectid);
4958 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4973 * Read some bytes from the current inode/file and send a write command to
4976 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4978 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
4981 ssize_t num_read = 0;
4983 p = fs_path_alloc();
4987 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
4989 num_read = fill_read_buf(sctx, offset, len);
4990 if (num_read <= 0) {
4996 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5000 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5004 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5005 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5006 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
5008 ret = send_cmd(sctx);
5019 * Send a clone command to user space.
5021 static int send_clone(struct send_ctx *sctx,
5022 u64 offset, u32 len,
5023 struct clone_root *clone_root)
5029 btrfs_debug(sctx->send_root->fs_info,
5030 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
5031 offset, len, clone_root->root->objectid, clone_root->ino,
5032 clone_root->offset);
5034 p = fs_path_alloc();
5038 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5042 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5046 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5047 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5048 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5050 if (clone_root->root == sctx->send_root) {
5051 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
5052 &gen, NULL, NULL, NULL, NULL);
5055 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5057 ret = get_inode_path(clone_root->root, clone_root->ino, p);
5063 * If the parent we're using has a received_uuid set then use that as
5064 * our clone source as that is what we will look for when doing a
5067 * This covers the case that we create a snapshot off of a received
5068 * subvolume and then use that as the parent and try to receive on a
5071 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5072 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5073 clone_root->root->root_item.received_uuid);
5075 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5076 clone_root->root->root_item.uuid);
5077 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
5078 le64_to_cpu(clone_root->root->root_item.ctransid));
5079 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5080 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5081 clone_root->offset);
5083 ret = send_cmd(sctx);
5092 * Send an update extent command to user space.
5094 static int send_update_extent(struct send_ctx *sctx,
5095 u64 offset, u32 len)
5100 p = fs_path_alloc();
5104 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5108 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5112 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5113 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5114 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5116 ret = send_cmd(sctx);
5124 static int send_hole(struct send_ctx *sctx, u64 end)
5126 struct fs_path *p = NULL;
5127 u64 offset = sctx->cur_inode_last_extent;
5132 * Don't go beyond the inode's i_size due to prealloc extents that start
5135 end = min_t(u64, end, sctx->cur_inode_size);
5137 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5138 return send_update_extent(sctx, offset, end - offset);
5140 p = fs_path_alloc();
5143 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5145 goto tlv_put_failure;
5146 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
5147 while (offset < end) {
5148 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
5150 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5153 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5154 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5155 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
5156 ret = send_cmd(sctx);
5166 static int send_extent_data(struct send_ctx *sctx,
5172 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5173 return send_update_extent(sctx, offset, len);
5175 while (sent < len) {
5176 u64 size = len - sent;
5179 if (size > BTRFS_SEND_READ_SIZE)
5180 size = BTRFS_SEND_READ_SIZE;
5181 ret = send_write(sctx, offset + sent, size);
5192 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5193 * found, call send_set_xattr function to emit it.
5195 * Return 0 if there isn't a capability, or when the capability was emitted
5196 * successfully, or < 0 if an error occurred.
5198 static int send_capabilities(struct send_ctx *sctx)
5200 struct fs_path *fspath = NULL;
5201 struct btrfs_path *path;
5202 struct btrfs_dir_item *di;
5203 struct extent_buffer *leaf;
5204 unsigned long data_ptr;
5209 path = alloc_path_for_send();
5213 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5214 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5216 /* There is no xattr for this inode */
5218 } else if (IS_ERR(di)) {
5223 leaf = path->nodes[0];
5224 buf_len = btrfs_dir_data_len(leaf, di);
5226 fspath = fs_path_alloc();
5227 buf = kmalloc(buf_len, GFP_KERNEL);
5228 if (!fspath || !buf) {
5233 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5237 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5238 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5240 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5241 strlen(XATTR_NAME_CAPS), buf, buf_len);
5244 fs_path_free(fspath);
5245 btrfs_free_path(path);
5249 static int clone_range(struct send_ctx *sctx,
5250 struct clone_root *clone_root,
5251 const u64 disk_byte,
5256 struct btrfs_path *path;
5257 struct btrfs_key key;
5261 * Prevent cloning from a zero offset with a length matching the sector
5262 * size because in some scenarios this will make the receiver fail.
5264 * For example, if in the source filesystem the extent at offset 0
5265 * has a length of sectorsize and it was written using direct IO, then
5266 * it can never be an inline extent (even if compression is enabled).
5267 * Then this extent can be cloned in the original filesystem to a non
5268 * zero file offset, but it may not be possible to clone in the
5269 * destination filesystem because it can be inlined due to compression
5270 * on the destination filesystem (as the receiver's write operations are
5271 * always done using buffered IO). The same happens when the original
5272 * filesystem does not have compression enabled but the destination
5275 if (clone_root->offset == 0 &&
5276 len == sctx->send_root->fs_info->sectorsize)
5277 return send_extent_data(sctx, offset, len);
5279 path = alloc_path_for_send();
5284 * We can't send a clone operation for the entire range if we find
5285 * extent items in the respective range in the source file that
5286 * refer to different extents or if we find holes.
5287 * So check for that and do a mix of clone and regular write/copy
5288 * operations if needed.
5292 * mkfs.btrfs -f /dev/sda
5293 * mount /dev/sda /mnt
5294 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5295 * cp --reflink=always /mnt/foo /mnt/bar
5296 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5297 * btrfs subvolume snapshot -r /mnt /mnt/snap
5299 * If when we send the snapshot and we are processing file bar (which
5300 * has a higher inode number than foo) we blindly send a clone operation
5301 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5302 * a file bar that matches the content of file foo - iow, doesn't match
5303 * the content from bar in the original filesystem.
5305 key.objectid = clone_root->ino;
5306 key.type = BTRFS_EXTENT_DATA_KEY;
5307 key.offset = clone_root->offset;
5308 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5311 if (ret > 0 && path->slots[0] > 0) {
5312 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5313 if (key.objectid == clone_root->ino &&
5314 key.type == BTRFS_EXTENT_DATA_KEY)
5319 struct extent_buffer *leaf = path->nodes[0];
5320 int slot = path->slots[0];
5321 struct btrfs_file_extent_item *ei;
5326 if (slot >= btrfs_header_nritems(leaf)) {
5327 ret = btrfs_next_leaf(clone_root->root, path);
5335 btrfs_item_key_to_cpu(leaf, &key, slot);
5338 * We might have an implicit trailing hole (NO_HOLES feature
5339 * enabled). We deal with it after leaving this loop.
5341 if (key.objectid != clone_root->ino ||
5342 key.type != BTRFS_EXTENT_DATA_KEY)
5345 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5346 type = btrfs_file_extent_type(leaf, ei);
5347 if (type == BTRFS_FILE_EXTENT_INLINE) {
5348 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
5349 ext_len = PAGE_ALIGN(ext_len);
5351 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5354 if (key.offset + ext_len <= clone_root->offset)
5357 if (key.offset > clone_root->offset) {
5358 /* Implicit hole, NO_HOLES feature enabled. */
5359 u64 hole_len = key.offset - clone_root->offset;
5363 ret = send_extent_data(sctx, offset, hole_len);
5371 clone_root->offset += hole_len;
5372 data_offset += hole_len;
5375 if (key.offset >= clone_root->offset + len)
5378 clone_len = min_t(u64, ext_len, len);
5380 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5381 btrfs_file_extent_offset(leaf, ei) == data_offset)
5382 ret = send_clone(sctx, offset, clone_len, clone_root);
5384 ret = send_extent_data(sctx, offset, clone_len);
5392 offset += clone_len;
5393 clone_root->offset += clone_len;
5394 data_offset += clone_len;
5400 ret = send_extent_data(sctx, offset, len);
5404 btrfs_free_path(path);
5408 static int send_write_or_clone(struct send_ctx *sctx,
5409 struct btrfs_path *path,
5410 struct btrfs_key *key,
5411 struct clone_root *clone_root)
5414 struct btrfs_file_extent_item *ei;
5415 u64 offset = key->offset;
5418 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
5420 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5421 struct btrfs_file_extent_item);
5422 type = btrfs_file_extent_type(path->nodes[0], ei);
5423 if (type == BTRFS_FILE_EXTENT_INLINE) {
5424 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
5426 * it is possible the inline item won't cover the whole page,
5427 * but there may be items after this page. Make
5428 * sure to send the whole thing
5430 len = PAGE_ALIGN(len);
5432 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
5435 if (offset + len > sctx->cur_inode_size)
5436 len = sctx->cur_inode_size - offset;
5442 if (clone_root && IS_ALIGNED(offset + len, bs)) {
5446 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5447 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5448 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5451 ret = send_extent_data(sctx, offset, len);
5457 static int is_extent_unchanged(struct send_ctx *sctx,
5458 struct btrfs_path *left_path,
5459 struct btrfs_key *ekey)
5462 struct btrfs_key key;
5463 struct btrfs_path *path = NULL;
5464 struct extent_buffer *eb;
5466 struct btrfs_key found_key;
5467 struct btrfs_file_extent_item *ei;
5472 u64 left_offset_fixed;
5480 path = alloc_path_for_send();
5484 eb = left_path->nodes[0];
5485 slot = left_path->slots[0];
5486 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5487 left_type = btrfs_file_extent_type(eb, ei);
5489 if (left_type != BTRFS_FILE_EXTENT_REG) {
5493 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5494 left_len = btrfs_file_extent_num_bytes(eb, ei);
5495 left_offset = btrfs_file_extent_offset(eb, ei);
5496 left_gen = btrfs_file_extent_generation(eb, ei);
5499 * Following comments will refer to these graphics. L is the left
5500 * extents which we are checking at the moment. 1-8 are the right
5501 * extents that we iterate.
5504 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5507 * |--1--|-2b-|...(same as above)
5509 * Alternative situation. Happens on files where extents got split.
5511 * |-----------7-----------|-6-|
5513 * Alternative situation. Happens on files which got larger.
5516 * Nothing follows after 8.
5519 key.objectid = ekey->objectid;
5520 key.type = BTRFS_EXTENT_DATA_KEY;
5521 key.offset = ekey->offset;
5522 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5531 * Handle special case where the right side has no extents at all.
5533 eb = path->nodes[0];
5534 slot = path->slots[0];
5535 btrfs_item_key_to_cpu(eb, &found_key, slot);
5536 if (found_key.objectid != key.objectid ||
5537 found_key.type != key.type) {
5538 /* If we're a hole then just pretend nothing changed */
5539 ret = (left_disknr) ? 0 : 1;
5544 * We're now on 2a, 2b or 7.
5547 while (key.offset < ekey->offset + left_len) {
5548 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5549 right_type = btrfs_file_extent_type(eb, ei);
5550 if (right_type != BTRFS_FILE_EXTENT_REG &&
5551 right_type != BTRFS_FILE_EXTENT_INLINE) {
5556 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5557 right_len = btrfs_file_extent_ram_bytes(eb, ei);
5558 right_len = PAGE_ALIGN(right_len);
5560 right_len = btrfs_file_extent_num_bytes(eb, ei);
5564 * Are we at extent 8? If yes, we know the extent is changed.
5565 * This may only happen on the first iteration.
5567 if (found_key.offset + right_len <= ekey->offset) {
5568 /* If we're a hole just pretend nothing changed */
5569 ret = (left_disknr) ? 0 : 1;
5574 * We just wanted to see if when we have an inline extent, what
5575 * follows it is a regular extent (wanted to check the above
5576 * condition for inline extents too). This should normally not
5577 * happen but it's possible for example when we have an inline
5578 * compressed extent representing data with a size matching
5579 * the page size (currently the same as sector size).
5581 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5586 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5587 right_offset = btrfs_file_extent_offset(eb, ei);
5588 right_gen = btrfs_file_extent_generation(eb, ei);
5590 left_offset_fixed = left_offset;
5591 if (key.offset < ekey->offset) {
5592 /* Fix the right offset for 2a and 7. */
5593 right_offset += ekey->offset - key.offset;
5595 /* Fix the left offset for all behind 2a and 2b */
5596 left_offset_fixed += key.offset - ekey->offset;
5600 * Check if we have the same extent.
5602 if (left_disknr != right_disknr ||
5603 left_offset_fixed != right_offset ||
5604 left_gen != right_gen) {
5610 * Go to the next extent.
5612 ret = btrfs_next_item(sctx->parent_root, path);
5616 eb = path->nodes[0];
5617 slot = path->slots[0];
5618 btrfs_item_key_to_cpu(eb, &found_key, slot);
5620 if (ret || found_key.objectid != key.objectid ||
5621 found_key.type != key.type) {
5622 key.offset += right_len;
5625 if (found_key.offset != key.offset + right_len) {
5633 * We're now behind the left extent (treat as unchanged) or at the end
5634 * of the right side (treat as changed).
5636 if (key.offset >= ekey->offset + left_len)
5643 btrfs_free_path(path);
5647 static int get_last_extent(struct send_ctx *sctx, u64 offset)
5649 struct btrfs_path *path;
5650 struct btrfs_root *root = sctx->send_root;
5651 struct btrfs_file_extent_item *fi;
5652 struct btrfs_key key;
5657 path = alloc_path_for_send();
5661 sctx->cur_inode_last_extent = 0;
5663 key.objectid = sctx->cur_ino;
5664 key.type = BTRFS_EXTENT_DATA_KEY;
5665 key.offset = offset;
5666 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5670 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5671 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5674 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5675 struct btrfs_file_extent_item);
5676 type = btrfs_file_extent_type(path->nodes[0], fi);
5677 if (type == BTRFS_FILE_EXTENT_INLINE) {
5678 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
5679 extent_end = ALIGN(key.offset + size,
5680 sctx->send_root->fs_info->sectorsize);
5682 extent_end = key.offset +
5683 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5685 sctx->cur_inode_last_extent = extent_end;
5687 btrfs_free_path(path);
5691 static int range_is_hole_in_parent(struct send_ctx *sctx,
5695 struct btrfs_path *path;
5696 struct btrfs_key key;
5697 struct btrfs_root *root = sctx->parent_root;
5698 u64 search_start = start;
5701 path = alloc_path_for_send();
5705 key.objectid = sctx->cur_ino;
5706 key.type = BTRFS_EXTENT_DATA_KEY;
5707 key.offset = search_start;
5708 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5711 if (ret > 0 && path->slots[0] > 0)
5714 while (search_start < end) {
5715 struct extent_buffer *leaf = path->nodes[0];
5716 int slot = path->slots[0];
5717 struct btrfs_file_extent_item *fi;
5720 if (slot >= btrfs_header_nritems(leaf)) {
5721 ret = btrfs_next_leaf(root, path);
5729 btrfs_item_key_to_cpu(leaf, &key, slot);
5730 if (key.objectid < sctx->cur_ino ||
5731 key.type < BTRFS_EXTENT_DATA_KEY)
5733 if (key.objectid > sctx->cur_ino ||
5734 key.type > BTRFS_EXTENT_DATA_KEY ||
5738 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5739 if (btrfs_file_extent_type(leaf, fi) ==
5740 BTRFS_FILE_EXTENT_INLINE) {
5741 u64 size = btrfs_file_extent_ram_bytes(leaf, fi);
5743 extent_end = ALIGN(key.offset + size,
5744 root->fs_info->sectorsize);
5746 extent_end = key.offset +
5747 btrfs_file_extent_num_bytes(leaf, fi);
5749 if (extent_end <= start)
5751 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5752 search_start = extent_end;
5762 btrfs_free_path(path);
5766 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5767 struct btrfs_key *key)
5769 struct btrfs_file_extent_item *fi;
5774 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5777 if (sctx->cur_inode_last_extent == (u64)-1) {
5778 ret = get_last_extent(sctx, key->offset - 1);
5783 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5784 struct btrfs_file_extent_item);
5785 type = btrfs_file_extent_type(path->nodes[0], fi);
5786 if (type == BTRFS_FILE_EXTENT_INLINE) {
5787 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
5788 extent_end = ALIGN(key->offset + size,
5789 sctx->send_root->fs_info->sectorsize);
5791 extent_end = key->offset +
5792 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5795 if (path->slots[0] == 0 &&
5796 sctx->cur_inode_last_extent < key->offset) {
5798 * We might have skipped entire leafs that contained only
5799 * file extent items for our current inode. These leafs have
5800 * a generation number smaller (older) than the one in the
5801 * current leaf and the leaf our last extent came from, and
5802 * are located between these 2 leafs.
5804 ret = get_last_extent(sctx, key->offset - 1);
5809 if (sctx->cur_inode_last_extent < key->offset) {
5810 ret = range_is_hole_in_parent(sctx,
5811 sctx->cur_inode_last_extent,
5816 ret = send_hole(sctx, key->offset);
5820 sctx->cur_inode_last_extent = extent_end;
5824 static int process_extent(struct send_ctx *sctx,
5825 struct btrfs_path *path,
5826 struct btrfs_key *key)
5828 struct clone_root *found_clone = NULL;
5831 if (S_ISLNK(sctx->cur_inode_mode))
5834 if (sctx->parent_root && !sctx->cur_inode_new) {
5835 ret = is_extent_unchanged(sctx, path, key);
5843 struct btrfs_file_extent_item *ei;
5846 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5847 struct btrfs_file_extent_item);
5848 type = btrfs_file_extent_type(path->nodes[0], ei);
5849 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5850 type == BTRFS_FILE_EXTENT_REG) {
5852 * The send spec does not have a prealloc command yet,
5853 * so just leave a hole for prealloc'ed extents until
5854 * we have enough commands queued up to justify rev'ing
5857 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5862 /* Have a hole, just skip it. */
5863 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5870 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5871 sctx->cur_inode_size, &found_clone);
5872 if (ret != -ENOENT && ret < 0)
5875 ret = send_write_or_clone(sctx, path, key, found_clone);
5879 ret = maybe_send_hole(sctx, path, key);
5884 static int process_all_extents(struct send_ctx *sctx)
5887 struct btrfs_root *root;
5888 struct btrfs_path *path;
5889 struct btrfs_key key;
5890 struct btrfs_key found_key;
5891 struct extent_buffer *eb;
5894 root = sctx->send_root;
5895 path = alloc_path_for_send();
5899 key.objectid = sctx->cmp_key->objectid;
5900 key.type = BTRFS_EXTENT_DATA_KEY;
5902 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5907 eb = path->nodes[0];
5908 slot = path->slots[0];
5910 if (slot >= btrfs_header_nritems(eb)) {
5911 ret = btrfs_next_leaf(root, path);
5914 } else if (ret > 0) {
5921 btrfs_item_key_to_cpu(eb, &found_key, slot);
5923 if (found_key.objectid != key.objectid ||
5924 found_key.type != key.type) {
5929 ret = process_extent(sctx, path, &found_key);
5937 btrfs_free_path(path);
5941 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5943 int *refs_processed)
5947 if (sctx->cur_ino == 0)
5949 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
5950 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
5952 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5955 ret = process_recorded_refs(sctx, pending_move);
5959 *refs_processed = 1;
5964 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5975 int pending_move = 0;
5976 int refs_processed = 0;
5978 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5984 * We have processed the refs and thus need to advance send_progress.
5985 * Now, calls to get_cur_xxx will take the updated refs of the current
5986 * inode into account.
5988 * On the other hand, if our current inode is a directory and couldn't
5989 * be moved/renamed because its parent was renamed/moved too and it has
5990 * a higher inode number, we can only move/rename our current inode
5991 * after we moved/renamed its parent. Therefore in this case operate on
5992 * the old path (pre move/rename) of our current inode, and the
5993 * move/rename will be performed later.
5995 if (refs_processed && !pending_move)
5996 sctx->send_progress = sctx->cur_ino + 1;
5998 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6000 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6003 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
6004 &left_mode, &left_uid, &left_gid, NULL);
6008 if (!sctx->parent_root || sctx->cur_inode_new) {
6010 if (!S_ISLNK(sctx->cur_inode_mode))
6013 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
6014 NULL, NULL, &right_mode, &right_uid,
6019 if (left_uid != right_uid || left_gid != right_gid)
6021 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6025 if (S_ISREG(sctx->cur_inode_mode)) {
6026 if (need_send_hole(sctx)) {
6027 if (sctx->cur_inode_last_extent == (u64)-1 ||
6028 sctx->cur_inode_last_extent <
6029 sctx->cur_inode_size) {
6030 ret = get_last_extent(sctx, (u64)-1);
6034 if (sctx->cur_inode_last_extent <
6035 sctx->cur_inode_size) {
6036 ret = send_hole(sctx, sctx->cur_inode_size);
6041 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6042 sctx->cur_inode_size);
6048 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6049 left_uid, left_gid);
6054 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6060 ret = send_capabilities(sctx);
6065 * If other directory inodes depended on our current directory
6066 * inode's move/rename, now do their move/rename operations.
6068 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6069 ret = apply_children_dir_moves(sctx);
6073 * Need to send that every time, no matter if it actually
6074 * changed between the two trees as we have done changes to
6075 * the inode before. If our inode is a directory and it's
6076 * waiting to be moved/renamed, we will send its utimes when
6077 * it's moved/renamed, therefore we don't need to do it here.
6079 sctx->send_progress = sctx->cur_ino + 1;
6080 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6089 static int changed_inode(struct send_ctx *sctx,
6090 enum btrfs_compare_tree_result result)
6093 struct btrfs_key *key = sctx->cmp_key;
6094 struct btrfs_inode_item *left_ii = NULL;
6095 struct btrfs_inode_item *right_ii = NULL;
6099 sctx->cur_ino = key->objectid;
6100 sctx->cur_inode_new_gen = 0;
6101 sctx->cur_inode_last_extent = (u64)-1;
6104 * Set send_progress to current inode. This will tell all get_cur_xxx
6105 * functions that the current inode's refs are not updated yet. Later,
6106 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6108 sctx->send_progress = sctx->cur_ino;
6110 if (result == BTRFS_COMPARE_TREE_NEW ||
6111 result == BTRFS_COMPARE_TREE_CHANGED) {
6112 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6113 sctx->left_path->slots[0],
6114 struct btrfs_inode_item);
6115 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6118 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6119 sctx->right_path->slots[0],
6120 struct btrfs_inode_item);
6121 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6124 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6125 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6126 sctx->right_path->slots[0],
6127 struct btrfs_inode_item);
6129 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6133 * The cur_ino = root dir case is special here. We can't treat
6134 * the inode as deleted+reused because it would generate a
6135 * stream that tries to delete/mkdir the root dir.
6137 if (left_gen != right_gen &&
6138 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6139 sctx->cur_inode_new_gen = 1;
6142 if (result == BTRFS_COMPARE_TREE_NEW) {
6143 sctx->cur_inode_gen = left_gen;
6144 sctx->cur_inode_new = 1;
6145 sctx->cur_inode_deleted = 0;
6146 sctx->cur_inode_size = btrfs_inode_size(
6147 sctx->left_path->nodes[0], left_ii);
6148 sctx->cur_inode_mode = btrfs_inode_mode(
6149 sctx->left_path->nodes[0], left_ii);
6150 sctx->cur_inode_rdev = btrfs_inode_rdev(
6151 sctx->left_path->nodes[0], left_ii);
6152 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6153 ret = send_create_inode_if_needed(sctx);
6154 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6155 sctx->cur_inode_gen = right_gen;
6156 sctx->cur_inode_new = 0;
6157 sctx->cur_inode_deleted = 1;
6158 sctx->cur_inode_size = btrfs_inode_size(
6159 sctx->right_path->nodes[0], right_ii);
6160 sctx->cur_inode_mode = btrfs_inode_mode(
6161 sctx->right_path->nodes[0], right_ii);
6162 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
6164 * We need to do some special handling in case the inode was
6165 * reported as changed with a changed generation number. This
6166 * means that the original inode was deleted and new inode
6167 * reused the same inum. So we have to treat the old inode as
6168 * deleted and the new one as new.
6170 if (sctx->cur_inode_new_gen) {
6172 * First, process the inode as if it was deleted.
6174 sctx->cur_inode_gen = right_gen;
6175 sctx->cur_inode_new = 0;
6176 sctx->cur_inode_deleted = 1;
6177 sctx->cur_inode_size = btrfs_inode_size(
6178 sctx->right_path->nodes[0], right_ii);
6179 sctx->cur_inode_mode = btrfs_inode_mode(
6180 sctx->right_path->nodes[0], right_ii);
6181 ret = process_all_refs(sctx,
6182 BTRFS_COMPARE_TREE_DELETED);
6187 * Now process the inode as if it was new.
6189 sctx->cur_inode_gen = left_gen;
6190 sctx->cur_inode_new = 1;
6191 sctx->cur_inode_deleted = 0;
6192 sctx->cur_inode_size = btrfs_inode_size(
6193 sctx->left_path->nodes[0], left_ii);
6194 sctx->cur_inode_mode = btrfs_inode_mode(
6195 sctx->left_path->nodes[0], left_ii);
6196 sctx->cur_inode_rdev = btrfs_inode_rdev(
6197 sctx->left_path->nodes[0], left_ii);
6198 ret = send_create_inode_if_needed(sctx);
6202 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6206 * Advance send_progress now as we did not get into
6207 * process_recorded_refs_if_needed in the new_gen case.
6209 sctx->send_progress = sctx->cur_ino + 1;
6212 * Now process all extents and xattrs of the inode as if
6213 * they were all new.
6215 ret = process_all_extents(sctx);
6218 ret = process_all_new_xattrs(sctx);
6222 sctx->cur_inode_gen = left_gen;
6223 sctx->cur_inode_new = 0;
6224 sctx->cur_inode_new_gen = 0;
6225 sctx->cur_inode_deleted = 0;
6226 sctx->cur_inode_size = btrfs_inode_size(
6227 sctx->left_path->nodes[0], left_ii);
6228 sctx->cur_inode_mode = btrfs_inode_mode(
6229 sctx->left_path->nodes[0], left_ii);
6238 * We have to process new refs before deleted refs, but compare_trees gives us
6239 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6240 * first and later process them in process_recorded_refs.
6241 * For the cur_inode_new_gen case, we skip recording completely because
6242 * changed_inode did already initiate processing of refs. The reason for this is
6243 * that in this case, compare_tree actually compares the refs of 2 different
6244 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6245 * refs of the right tree as deleted and all refs of the left tree as new.
6247 static int changed_ref(struct send_ctx *sctx,
6248 enum btrfs_compare_tree_result result)
6252 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6253 inconsistent_snapshot_error(sctx, result, "reference");
6257 if (!sctx->cur_inode_new_gen &&
6258 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6259 if (result == BTRFS_COMPARE_TREE_NEW)
6260 ret = record_new_ref(sctx);
6261 else if (result == BTRFS_COMPARE_TREE_DELETED)
6262 ret = record_deleted_ref(sctx);
6263 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6264 ret = record_changed_ref(sctx);
6271 * Process new/deleted/changed xattrs. We skip processing in the
6272 * cur_inode_new_gen case because changed_inode did already initiate processing
6273 * of xattrs. The reason is the same as in changed_ref
6275 static int changed_xattr(struct send_ctx *sctx,
6276 enum btrfs_compare_tree_result result)
6280 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6281 inconsistent_snapshot_error(sctx, result, "xattr");
6285 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6286 if (result == BTRFS_COMPARE_TREE_NEW)
6287 ret = process_new_xattr(sctx);
6288 else if (result == BTRFS_COMPARE_TREE_DELETED)
6289 ret = process_deleted_xattr(sctx);
6290 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6291 ret = process_changed_xattr(sctx);
6298 * Process new/deleted/changed extents. We skip processing in the
6299 * cur_inode_new_gen case because changed_inode did already initiate processing
6300 * of extents. The reason is the same as in changed_ref
6302 static int changed_extent(struct send_ctx *sctx,
6303 enum btrfs_compare_tree_result result)
6308 * We have found an extent item that changed without the inode item
6309 * having changed. This can happen either after relocation (where the
6310 * disk_bytenr of an extent item is replaced at
6311 * relocation.c:replace_file_extents()) or after deduplication into a
6312 * file in both the parent and send snapshots (where an extent item can
6313 * get modified or replaced with a new one). Note that deduplication
6314 * updates the inode item, but it only changes the iversion (sequence
6315 * field in the inode item) of the inode, so if a file is deduplicated
6316 * the same amount of times in both the parent and send snapshots, its
6317 * iversion becames the same in both snapshots, whence the inode item is
6318 * the same on both snapshots.
6320 if (sctx->cur_ino != sctx->cmp_key->objectid)
6323 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6324 if (result != BTRFS_COMPARE_TREE_DELETED)
6325 ret = process_extent(sctx, sctx->left_path,
6332 static int dir_changed(struct send_ctx *sctx, u64 dir)
6334 u64 orig_gen, new_gen;
6337 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6342 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6347 return (orig_gen != new_gen) ? 1 : 0;
6350 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6351 struct btrfs_key *key)
6353 struct btrfs_inode_extref *extref;
6354 struct extent_buffer *leaf;
6355 u64 dirid = 0, last_dirid = 0;
6362 /* Easy case, just check this one dirid */
6363 if (key->type == BTRFS_INODE_REF_KEY) {
6364 dirid = key->offset;
6366 ret = dir_changed(sctx, dirid);
6370 leaf = path->nodes[0];
6371 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6372 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6373 while (cur_offset < item_size) {
6374 extref = (struct btrfs_inode_extref *)(ptr +
6376 dirid = btrfs_inode_extref_parent(leaf, extref);
6377 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6378 cur_offset += ref_name_len + sizeof(*extref);
6379 if (dirid == last_dirid)
6381 ret = dir_changed(sctx, dirid);
6391 * Updates compare related fields in sctx and simply forwards to the actual
6392 * changed_xxx functions.
6394 static int changed_cb(struct btrfs_root *left_root,
6395 struct btrfs_root *right_root,
6396 struct btrfs_path *left_path,
6397 struct btrfs_path *right_path,
6398 struct btrfs_key *key,
6399 enum btrfs_compare_tree_result result,
6403 struct send_ctx *sctx = ctx;
6405 if (result == BTRFS_COMPARE_TREE_SAME) {
6406 if (key->type == BTRFS_INODE_REF_KEY ||
6407 key->type == BTRFS_INODE_EXTREF_KEY) {
6408 ret = compare_refs(sctx, left_path, key);
6413 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6414 return maybe_send_hole(sctx, left_path, key);
6418 result = BTRFS_COMPARE_TREE_CHANGED;
6422 sctx->left_path = left_path;
6423 sctx->right_path = right_path;
6424 sctx->cmp_key = key;
6426 ret = finish_inode_if_needed(sctx, 0);
6430 /* Ignore non-FS objects */
6431 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6432 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6435 if (key->type == BTRFS_INODE_ITEM_KEY)
6436 ret = changed_inode(sctx, result);
6437 else if (key->type == BTRFS_INODE_REF_KEY ||
6438 key->type == BTRFS_INODE_EXTREF_KEY)
6439 ret = changed_ref(sctx, result);
6440 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6441 ret = changed_xattr(sctx, result);
6442 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6443 ret = changed_extent(sctx, result);
6449 static int full_send_tree(struct send_ctx *sctx)
6452 struct btrfs_root *send_root = sctx->send_root;
6453 struct btrfs_key key;
6454 struct btrfs_key found_key;
6455 struct btrfs_path *path;
6456 struct extent_buffer *eb;
6459 path = alloc_path_for_send();
6463 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6464 key.type = BTRFS_INODE_ITEM_KEY;
6467 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6474 eb = path->nodes[0];
6475 slot = path->slots[0];
6476 btrfs_item_key_to_cpu(eb, &found_key, slot);
6478 ret = changed_cb(send_root, NULL, path, NULL,
6479 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
6483 key.objectid = found_key.objectid;
6484 key.type = found_key.type;
6485 key.offset = found_key.offset + 1;
6487 ret = btrfs_next_item(send_root, path);
6497 ret = finish_inode_if_needed(sctx, 1);
6500 btrfs_free_path(path);
6504 static int send_subvol(struct send_ctx *sctx)
6508 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6509 ret = send_header(sctx);
6514 ret = send_subvol_begin(sctx);
6518 if (sctx->parent_root) {
6519 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6523 ret = finish_inode_if_needed(sctx, 1);
6527 ret = full_send_tree(sctx);
6533 free_recorded_refs(sctx);
6538 * If orphan cleanup did remove any orphans from a root, it means the tree
6539 * was modified and therefore the commit root is not the same as the current
6540 * root anymore. This is a problem, because send uses the commit root and
6541 * therefore can see inode items that don't exist in the current root anymore,
6542 * and for example make calls to btrfs_iget, which will do tree lookups based
6543 * on the current root and not on the commit root. Those lookups will fail,
6544 * returning a -ESTALE error, and making send fail with that error. So make
6545 * sure a send does not see any orphans we have just removed, and that it will
6546 * see the same inodes regardless of whether a transaction commit happened
6547 * before it started (meaning that the commit root will be the same as the
6548 * current root) or not.
6550 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6553 struct btrfs_trans_handle *trans = NULL;
6556 if (sctx->parent_root &&
6557 sctx->parent_root->node != sctx->parent_root->commit_root)
6560 for (i = 0; i < sctx->clone_roots_cnt; i++)
6561 if (sctx->clone_roots[i].root->node !=
6562 sctx->clone_roots[i].root->commit_root)
6566 return btrfs_end_transaction(trans);
6571 /* Use any root, all fs roots will get their commit roots updated. */
6573 trans = btrfs_join_transaction(sctx->send_root);
6575 return PTR_ERR(trans);
6579 return btrfs_commit_transaction(trans);
6582 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6584 spin_lock(&root->root_item_lock);
6585 root->send_in_progress--;
6587 * Not much left to do, we don't know why it's unbalanced and
6588 * can't blindly reset it to 0.
6590 if (root->send_in_progress < 0)
6591 btrfs_err(root->fs_info,
6592 "send_in_progres unbalanced %d root %llu",
6593 root->send_in_progress, root->root_key.objectid);
6594 spin_unlock(&root->root_item_lock);
6597 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
6600 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6601 struct btrfs_fs_info *fs_info = send_root->fs_info;
6602 struct btrfs_root *clone_root;
6603 struct btrfs_ioctl_send_args *arg = NULL;
6604 struct btrfs_key key;
6605 struct send_ctx *sctx = NULL;
6607 u64 *clone_sources_tmp = NULL;
6608 int clone_sources_to_rollback = 0;
6609 unsigned alloc_size;
6610 int sort_clone_roots = 0;
6613 if (!capable(CAP_SYS_ADMIN))
6617 * The subvolume must remain read-only during send, protect against
6618 * making it RW. This also protects against deletion.
6620 spin_lock(&send_root->root_item_lock);
6621 send_root->send_in_progress++;
6622 spin_unlock(&send_root->root_item_lock);
6625 * This is done when we lookup the root, it should already be complete
6626 * by the time we get here.
6628 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6631 * Userspace tools do the checks and warn the user if it's
6634 if (!btrfs_root_readonly(send_root)) {
6639 arg = memdup_user(arg_, sizeof(*arg));
6647 * Check that we don't overflow at later allocations, we request
6648 * clone_sources_count + 1 items, and compare to unsigned long inside
6649 * access_ok. Also set an upper limit for allocation size so this can't
6650 * easily exhaust memory. Max number of clone sources is about 200K.
6652 if (arg->clone_sources_count > SZ_8M / sizeof(struct clone_root)) {
6657 if (!access_ok(VERIFY_READ, arg->clone_sources,
6658 sizeof(*arg->clone_sources) *
6659 arg->clone_sources_count)) {
6664 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
6669 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
6675 INIT_LIST_HEAD(&sctx->new_refs);
6676 INIT_LIST_HEAD(&sctx->deleted_refs);
6677 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
6678 INIT_LIST_HEAD(&sctx->name_cache_list);
6680 sctx->flags = arg->flags;
6682 sctx->send_filp = fget(arg->send_fd);
6683 if (!sctx->send_filp) {
6688 sctx->send_root = send_root;
6690 * Unlikely but possible, if the subvolume is marked for deletion but
6691 * is slow to remove the directory entry, send can still be started
6693 if (btrfs_root_dead(sctx->send_root)) {
6698 sctx->clone_roots_cnt = arg->clone_sources_count;
6700 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
6701 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
6702 if (!sctx->send_buf) {
6707 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
6708 if (!sctx->read_buf) {
6713 sctx->pending_dir_moves = RB_ROOT;
6714 sctx->waiting_dir_moves = RB_ROOT;
6715 sctx->orphan_dirs = RB_ROOT;
6717 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6719 sctx->clone_roots = kvzalloc(alloc_size, GFP_KERNEL);
6720 if (!sctx->clone_roots) {
6725 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6727 if (arg->clone_sources_count) {
6728 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
6729 if (!clone_sources_tmp) {
6734 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
6741 for (i = 0; i < arg->clone_sources_count; i++) {
6742 key.objectid = clone_sources_tmp[i];
6743 key.type = BTRFS_ROOT_ITEM_KEY;
6744 key.offset = (u64)-1;
6746 index = srcu_read_lock(&fs_info->subvol_srcu);
6748 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
6749 if (IS_ERR(clone_root)) {
6750 srcu_read_unlock(&fs_info->subvol_srcu, index);
6751 ret = PTR_ERR(clone_root);
6754 spin_lock(&clone_root->root_item_lock);
6755 if (!btrfs_root_readonly(clone_root) ||
6756 btrfs_root_dead(clone_root)) {
6757 spin_unlock(&clone_root->root_item_lock);
6758 srcu_read_unlock(&fs_info->subvol_srcu, index);
6762 clone_root->send_in_progress++;
6763 spin_unlock(&clone_root->root_item_lock);
6764 srcu_read_unlock(&fs_info->subvol_srcu, index);
6766 sctx->clone_roots[i].root = clone_root;
6767 clone_sources_to_rollback = i + 1;
6769 kvfree(clone_sources_tmp);
6770 clone_sources_tmp = NULL;
6773 if (arg->parent_root) {
6774 key.objectid = arg->parent_root;
6775 key.type = BTRFS_ROOT_ITEM_KEY;
6776 key.offset = (u64)-1;
6778 index = srcu_read_lock(&fs_info->subvol_srcu);
6780 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
6781 if (IS_ERR(sctx->parent_root)) {
6782 srcu_read_unlock(&fs_info->subvol_srcu, index);
6783 ret = PTR_ERR(sctx->parent_root);
6787 spin_lock(&sctx->parent_root->root_item_lock);
6788 sctx->parent_root->send_in_progress++;
6789 if (!btrfs_root_readonly(sctx->parent_root) ||
6790 btrfs_root_dead(sctx->parent_root)) {
6791 spin_unlock(&sctx->parent_root->root_item_lock);
6792 srcu_read_unlock(&fs_info->subvol_srcu, index);
6796 spin_unlock(&sctx->parent_root->root_item_lock);
6798 srcu_read_unlock(&fs_info->subvol_srcu, index);
6802 * Clones from send_root are allowed, but only if the clone source
6803 * is behind the current send position. This is checked while searching
6804 * for possible clone sources.
6806 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6808 /* We do a bsearch later */
6809 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6810 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6812 sort_clone_roots = 1;
6814 ret = ensure_commit_roots_uptodate(sctx);
6818 current->journal_info = BTRFS_SEND_TRANS_STUB;
6819 ret = send_subvol(sctx);
6820 current->journal_info = NULL;
6824 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6825 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6828 ret = send_cmd(sctx);
6834 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6835 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6837 struct pending_dir_move *pm;
6839 n = rb_first(&sctx->pending_dir_moves);
6840 pm = rb_entry(n, struct pending_dir_move, node);
6841 while (!list_empty(&pm->list)) {
6842 struct pending_dir_move *pm2;
6844 pm2 = list_first_entry(&pm->list,
6845 struct pending_dir_move, list);
6846 free_pending_move(sctx, pm2);
6848 free_pending_move(sctx, pm);
6851 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6852 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6854 struct waiting_dir_move *dm;
6856 n = rb_first(&sctx->waiting_dir_moves);
6857 dm = rb_entry(n, struct waiting_dir_move, node);
6858 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6862 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6863 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6865 struct orphan_dir_info *odi;
6867 n = rb_first(&sctx->orphan_dirs);
6868 odi = rb_entry(n, struct orphan_dir_info, node);
6869 free_orphan_dir_info(sctx, odi);
6872 if (sort_clone_roots) {
6873 for (i = 0; i < sctx->clone_roots_cnt; i++)
6874 btrfs_root_dec_send_in_progress(
6875 sctx->clone_roots[i].root);
6877 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6878 btrfs_root_dec_send_in_progress(
6879 sctx->clone_roots[i].root);
6881 btrfs_root_dec_send_in_progress(send_root);
6883 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6884 btrfs_root_dec_send_in_progress(sctx->parent_root);
6887 kvfree(clone_sources_tmp);
6890 if (sctx->send_filp)
6891 fput(sctx->send_filp);
6893 kvfree(sctx->clone_roots);
6894 kvfree(sctx->send_buf);
6895 kvfree(sctx->read_buf);
6897 name_cache_free(sctx);