1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012 Alexander Block. All rights reserved.
6 #include <linux/bsearch.h>
8 #include <linux/file.h>
9 #include <linux/sort.h>
10 #include <linux/mount.h>
11 #include <linux/xattr.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/radix-tree.h>
14 #include <linux/vmalloc.h>
15 #include <linux/string.h>
16 #include <linux/compat.h>
17 #include <linux/crc32c.h>
23 #include "btrfs_inode.h"
24 #include "transaction.h"
25 #include "compression.h"
27 #include "print-tree.h"
30 * Maximum number of references an extent can have in order for us to attempt to
31 * issue clone operations instead of write operations. This currently exists to
32 * avoid hitting limitations of the backreference walking code (taking a lot of
33 * time and using too much memory for extents with large number of references).
35 #define SEND_MAX_EXTENT_REFS 64
38 * A fs_path is a helper to dynamically build path names with unknown size.
39 * It reallocates the internal buffer on demand.
40 * It allows fast adding of path elements on the right side (normal path) and
41 * fast adding to the left side (reversed path). A reversed path can also be
42 * unreversed if needed.
51 unsigned short buf_len:15;
52 unsigned short reversed:1;
56 * Average path length does not exceed 200 bytes, we'll have
57 * better packing in the slab and higher chance to satisfy
58 * a allocation later during send.
63 #define FS_PATH_INLINE_SIZE \
64 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
67 /* reused for each extent */
69 struct btrfs_root *root;
76 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
77 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
80 struct file *send_filp;
86 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
87 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
88 /* Protocol version compatibility requested */
91 struct btrfs_root *send_root;
92 struct btrfs_root *parent_root;
93 struct clone_root *clone_roots;
96 /* current state of the compare_tree call */
97 struct btrfs_path *left_path;
98 struct btrfs_path *right_path;
99 struct btrfs_key *cmp_key;
102 * Keep track of the generation of the last transaction that was used
103 * for relocating a block group. This is periodically checked in order
104 * to detect if a relocation happened since the last check, so that we
105 * don't operate on stale extent buffers for nodes (level >= 1) or on
106 * stale disk_bytenr values of file extent items.
108 u64 last_reloc_trans;
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;
123 u64 cur_inode_next_write_offset;
124 bool ignore_cur_inode;
128 struct list_head new_refs;
129 struct list_head deleted_refs;
131 struct radix_tree_root name_cache;
132 struct list_head name_cache_list;
136 * The inode we are currently processing. It's not NULL only when we
137 * need to issue write commands for data extents from this inode.
139 struct inode *cur_inode;
140 struct file_ra_state ra;
141 u64 page_cache_clear_start;
142 bool clean_page_cache;
145 * We process inodes by their increasing order, so if before an
146 * incremental send we reverse the parent/child relationship of
147 * directories such that a directory with a lower inode number was
148 * the parent of a directory with a higher inode number, and the one
149 * becoming the new parent got renamed too, we can't rename/move the
150 * directory with lower inode number when we finish processing it - we
151 * must process the directory with higher inode number first, then
152 * rename/move it and then rename/move the directory with lower inode
153 * number. Example follows.
155 * Tree state when the first send was performed:
167 * Tree state when the second (incremental) send is performed:
176 * The sequence of steps that lead to the second state was:
178 * mv /a/b/c/d /a/b/c2/d2
179 * mv /a/b/c /a/b/c2/d2/cc
181 * "c" has lower inode number, but we can't move it (2nd mv operation)
182 * before we move "d", which has higher inode number.
184 * So we just memorize which move/rename operations must be performed
185 * later when their respective parent is processed and moved/renamed.
188 /* Indexed by parent directory inode number. */
189 struct rb_root pending_dir_moves;
192 * Reverse index, indexed by the inode number of a directory that
193 * is waiting for the move/rename of its immediate parent before its
194 * own move/rename can be performed.
196 struct rb_root waiting_dir_moves;
199 * A directory that is going to be rm'ed might have a child directory
200 * which is in the pending directory moves index above. In this case,
201 * the directory can only be removed after the move/rename of its child
202 * is performed. Example:
222 * Sequence of steps that lead to the send snapshot:
223 * rm -f /a/b/c/foo.txt
225 * mv /a/b/c/x /a/b/YY
228 * When the child is processed, its move/rename is delayed until its
229 * parent is processed (as explained above), but all other operations
230 * like update utimes, chown, chgrp, etc, are performed and the paths
231 * that it uses for those operations must use the orphanized name of
232 * its parent (the directory we're going to rm later), so we need to
233 * memorize that name.
235 * Indexed by the inode number of the directory to be deleted.
237 struct rb_root orphan_dirs;
240 struct pending_dir_move {
242 struct list_head list;
246 struct list_head update_refs;
249 struct waiting_dir_move {
253 * There might be some directory that could not be removed because it
254 * was waiting for this directory inode to be moved first. Therefore
255 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
262 struct orphan_dir_info {
266 u64 last_dir_index_offset;
269 struct name_cache_entry {
270 struct list_head list;
272 * radix_tree has only 32bit entries but we need to handle 64bit inums.
273 * We use the lower 32bit of the 64bit inum to store it in the tree. If
274 * more then one inum would fall into the same entry, we use radix_list
275 * to store the additional entries. radix_list is also used to store
276 * entries where two entries have the same inum but different
279 struct list_head radix_list;
285 int need_later_update;
291 #define ADVANCE_ONLY_NEXT -1
293 enum btrfs_compare_tree_result {
294 BTRFS_COMPARE_TREE_NEW,
295 BTRFS_COMPARE_TREE_DELETED,
296 BTRFS_COMPARE_TREE_CHANGED,
297 BTRFS_COMPARE_TREE_SAME,
301 static void inconsistent_snapshot_error(struct send_ctx *sctx,
302 enum btrfs_compare_tree_result result,
305 const char *result_string;
308 case BTRFS_COMPARE_TREE_NEW:
309 result_string = "new";
311 case BTRFS_COMPARE_TREE_DELETED:
312 result_string = "deleted";
314 case BTRFS_COMPARE_TREE_CHANGED:
315 result_string = "updated";
317 case BTRFS_COMPARE_TREE_SAME:
319 result_string = "unchanged";
323 result_string = "unexpected";
326 btrfs_err(sctx->send_root->fs_info,
327 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
328 result_string, what, sctx->cmp_key->objectid,
329 sctx->send_root->root_key.objectid,
331 sctx->parent_root->root_key.objectid : 0));
335 static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd)
337 switch (sctx->proto) {
338 case 1: return cmd < __BTRFS_SEND_C_MAX_V1;
339 case 2: return cmd < __BTRFS_SEND_C_MAX_V2;
340 default: return false;
344 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
346 static struct waiting_dir_move *
347 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
349 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
351 static int need_send_hole(struct send_ctx *sctx)
353 return (sctx->parent_root && !sctx->cur_inode_new &&
354 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
355 S_ISREG(sctx->cur_inode_mode));
358 static void fs_path_reset(struct fs_path *p)
361 p->start = p->buf + p->buf_len - 1;
371 static struct fs_path *fs_path_alloc(void)
375 p = kmalloc(sizeof(*p), GFP_KERNEL);
379 p->buf = p->inline_buf;
380 p->buf_len = FS_PATH_INLINE_SIZE;
385 static struct fs_path *fs_path_alloc_reversed(void)
397 static void fs_path_free(struct fs_path *p)
401 if (p->buf != p->inline_buf)
406 static int fs_path_len(struct fs_path *p)
408 return p->end - p->start;
411 static int fs_path_ensure_buf(struct fs_path *p, int len)
419 if (p->buf_len >= len)
422 if (len > PATH_MAX) {
427 path_len = p->end - p->start;
428 old_buf_len = p->buf_len;
431 * First time the inline_buf does not suffice
433 if (p->buf == p->inline_buf) {
434 tmp_buf = kmalloc(len, GFP_KERNEL);
436 memcpy(tmp_buf, p->buf, old_buf_len);
438 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
444 * The real size of the buffer is bigger, this will let the fast path
445 * happen most of the time
447 p->buf_len = ksize(p->buf);
450 tmp_buf = p->buf + old_buf_len - path_len - 1;
451 p->end = p->buf + p->buf_len - 1;
452 p->start = p->end - path_len;
453 memmove(p->start, tmp_buf, path_len + 1);
456 p->end = p->start + path_len;
461 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
467 new_len = p->end - p->start + name_len;
468 if (p->start != p->end)
470 ret = fs_path_ensure_buf(p, new_len);
475 if (p->start != p->end)
477 p->start -= name_len;
478 *prepared = p->start;
480 if (p->start != p->end)
491 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
496 ret = fs_path_prepare_for_add(p, name_len, &prepared);
499 memcpy(prepared, name, name_len);
505 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
510 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
513 memcpy(prepared, p2->start, p2->end - p2->start);
519 static int fs_path_add_from_extent_buffer(struct fs_path *p,
520 struct extent_buffer *eb,
521 unsigned long off, int len)
526 ret = fs_path_prepare_for_add(p, len, &prepared);
530 read_extent_buffer(eb, prepared, off, len);
536 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
538 p->reversed = from->reversed;
541 return fs_path_add_path(p, from);
544 static void fs_path_unreverse(struct fs_path *p)
553 len = p->end - p->start;
555 p->end = p->start + len;
556 memmove(p->start, tmp, len + 1);
560 static struct btrfs_path *alloc_path_for_send(void)
562 struct btrfs_path *path;
564 path = btrfs_alloc_path();
567 path->search_commit_root = 1;
568 path->skip_locking = 1;
569 path->need_commit_sem = 1;
573 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
579 ret = kernel_write(filp, buf + pos, len - pos, off);
580 /* TODO handle that correctly */
581 /*if (ret == -ERESTARTSYS) {
595 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
597 struct btrfs_tlv_header *hdr;
598 int total_len = sizeof(*hdr) + len;
599 int left = sctx->send_max_size - sctx->send_size;
601 if (unlikely(left < total_len))
604 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
605 put_unaligned_le16(attr, &hdr->tlv_type);
606 put_unaligned_le16(len, &hdr->tlv_len);
607 memcpy(hdr + 1, data, len);
608 sctx->send_size += total_len;
613 #define TLV_PUT_DEFINE_INT(bits) \
614 static int tlv_put_u##bits(struct send_ctx *sctx, \
615 u##bits attr, u##bits value) \
617 __le##bits __tmp = cpu_to_le##bits(value); \
618 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
621 TLV_PUT_DEFINE_INT(64)
623 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
624 const char *str, int len)
628 return tlv_put(sctx, attr, str, len);
631 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
634 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
637 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
638 struct extent_buffer *eb,
639 struct btrfs_timespec *ts)
641 struct btrfs_timespec bts;
642 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
643 return tlv_put(sctx, attr, &bts, sizeof(bts));
647 #define TLV_PUT(sctx, attrtype, data, attrlen) \
649 ret = tlv_put(sctx, attrtype, data, attrlen); \
651 goto tlv_put_failure; \
654 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
656 ret = tlv_put_u##bits(sctx, attrtype, value); \
658 goto tlv_put_failure; \
661 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
662 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
663 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
664 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
665 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
667 ret = tlv_put_string(sctx, attrtype, str, len); \
669 goto tlv_put_failure; \
671 #define TLV_PUT_PATH(sctx, attrtype, p) \
673 ret = tlv_put_string(sctx, attrtype, p->start, \
674 p->end - p->start); \
676 goto tlv_put_failure; \
678 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
680 ret = tlv_put_uuid(sctx, attrtype, uuid); \
682 goto tlv_put_failure; \
684 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
686 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
688 goto tlv_put_failure; \
691 static int send_header(struct send_ctx *sctx)
693 struct btrfs_stream_header hdr;
695 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
696 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
698 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
703 * For each command/item we want to send to userspace, we call this function.
705 static int begin_cmd(struct send_ctx *sctx, int cmd)
707 struct btrfs_cmd_header *hdr;
709 if (WARN_ON(!sctx->send_buf))
712 BUG_ON(sctx->send_size);
714 sctx->send_size += sizeof(*hdr);
715 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
716 put_unaligned_le16(cmd, &hdr->cmd);
721 static int send_cmd(struct send_ctx *sctx)
724 struct btrfs_cmd_header *hdr;
727 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
728 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
729 put_unaligned_le32(0, &hdr->crc);
731 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
732 put_unaligned_le32(crc, &hdr->crc);
734 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
737 sctx->total_send_size += sctx->send_size;
738 sctx->cmd_send_size[get_unaligned_le16(&hdr->cmd)] += sctx->send_size;
745 * Sends a move instruction to user space
747 static int send_rename(struct send_ctx *sctx,
748 struct fs_path *from, struct fs_path *to)
750 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
753 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
755 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
759 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
760 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
762 ret = send_cmd(sctx);
770 * Sends a link instruction to user space
772 static int send_link(struct send_ctx *sctx,
773 struct fs_path *path, struct fs_path *lnk)
775 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
778 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
780 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
784 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
785 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
787 ret = send_cmd(sctx);
795 * Sends an unlink instruction to user space
797 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
799 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
802 btrfs_debug(fs_info, "send_unlink %s", path->start);
804 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
808 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
810 ret = send_cmd(sctx);
818 * Sends a rmdir instruction to user space
820 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
822 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
825 btrfs_debug(fs_info, "send_rmdir %s", path->start);
827 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
831 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
833 ret = send_cmd(sctx);
841 * Helper function to retrieve some fields from an inode item.
843 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
844 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
848 struct btrfs_inode_item *ii;
849 struct btrfs_key key;
852 key.type = BTRFS_INODE_ITEM_KEY;
854 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
861 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
862 struct btrfs_inode_item);
864 *size = btrfs_inode_size(path->nodes[0], ii);
866 *gen = btrfs_inode_generation(path->nodes[0], ii);
868 *mode = btrfs_inode_mode(path->nodes[0], ii);
870 *uid = btrfs_inode_uid(path->nodes[0], ii);
872 *gid = btrfs_inode_gid(path->nodes[0], ii);
874 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
879 static int get_inode_info(struct btrfs_root *root,
880 u64 ino, u64 *size, u64 *gen,
881 u64 *mode, u64 *uid, u64 *gid,
884 struct btrfs_path *path;
887 path = alloc_path_for_send();
890 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
892 btrfs_free_path(path);
896 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
901 * Helper function to iterate the entries in ONE btrfs_inode_ref or
902 * btrfs_inode_extref.
903 * The iterate callback may return a non zero value to stop iteration. This can
904 * be a negative value for error codes or 1 to simply stop it.
906 * path must point to the INODE_REF or INODE_EXTREF when called.
908 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
909 struct btrfs_key *found_key, int resolve,
910 iterate_inode_ref_t iterate, void *ctx)
912 struct extent_buffer *eb = path->nodes[0];
913 struct btrfs_inode_ref *iref;
914 struct btrfs_inode_extref *extref;
915 struct btrfs_path *tmp_path;
919 int slot = path->slots[0];
926 unsigned long name_off;
927 unsigned long elem_size;
930 p = fs_path_alloc_reversed();
934 tmp_path = alloc_path_for_send();
941 if (found_key->type == BTRFS_INODE_REF_KEY) {
942 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
943 struct btrfs_inode_ref);
944 total = btrfs_item_size(eb, slot);
945 elem_size = sizeof(*iref);
947 ptr = btrfs_item_ptr_offset(eb, slot);
948 total = btrfs_item_size(eb, slot);
949 elem_size = sizeof(*extref);
952 while (cur < total) {
955 if (found_key->type == BTRFS_INODE_REF_KEY) {
956 iref = (struct btrfs_inode_ref *)(ptr + cur);
957 name_len = btrfs_inode_ref_name_len(eb, iref);
958 name_off = (unsigned long)(iref + 1);
959 index = btrfs_inode_ref_index(eb, iref);
960 dir = found_key->offset;
962 extref = (struct btrfs_inode_extref *)(ptr + cur);
963 name_len = btrfs_inode_extref_name_len(eb, extref);
964 name_off = (unsigned long)&extref->name;
965 index = btrfs_inode_extref_index(eb, extref);
966 dir = btrfs_inode_extref_parent(eb, extref);
970 start = btrfs_ref_to_path(root, tmp_path, name_len,
974 ret = PTR_ERR(start);
977 if (start < p->buf) {
978 /* overflow , try again with larger buffer */
979 ret = fs_path_ensure_buf(p,
980 p->buf_len + p->buf - start);
983 start = btrfs_ref_to_path(root, tmp_path,
988 ret = PTR_ERR(start);
991 BUG_ON(start < p->buf);
995 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
1001 cur += elem_size + name_len;
1002 ret = iterate(num, dir, index, p, ctx);
1009 btrfs_free_path(tmp_path);
1014 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
1015 const char *name, int name_len,
1016 const char *data, int data_len,
1020 * Helper function to iterate the entries in ONE btrfs_dir_item.
1021 * The iterate callback may return a non zero value to stop iteration. This can
1022 * be a negative value for error codes or 1 to simply stop it.
1024 * path must point to the dir item when called.
1026 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
1027 iterate_dir_item_t iterate, void *ctx)
1030 struct extent_buffer *eb;
1031 struct btrfs_dir_item *di;
1032 struct btrfs_key di_key;
1044 * Start with a small buffer (1 page). If later we end up needing more
1045 * space, which can happen for xattrs on a fs with a leaf size greater
1046 * then the page size, attempt to increase the buffer. Typically xattr
1050 buf = kmalloc(buf_len, GFP_KERNEL);
1056 eb = path->nodes[0];
1057 slot = path->slots[0];
1058 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1061 total = btrfs_item_size(eb, slot);
1064 while (cur < total) {
1065 name_len = btrfs_dir_name_len(eb, di);
1066 data_len = btrfs_dir_data_len(eb, di);
1067 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1069 if (btrfs_dir_type(eb, di) == BTRFS_FT_XATTR) {
1070 if (name_len > XATTR_NAME_MAX) {
1071 ret = -ENAMETOOLONG;
1074 if (name_len + data_len >
1075 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1083 if (name_len + data_len > PATH_MAX) {
1084 ret = -ENAMETOOLONG;
1089 if (name_len + data_len > buf_len) {
1090 buf_len = name_len + data_len;
1091 if (is_vmalloc_addr(buf)) {
1095 char *tmp = krealloc(buf, buf_len,
1096 GFP_KERNEL | __GFP_NOWARN);
1103 buf = kvmalloc(buf_len, GFP_KERNEL);
1111 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1112 name_len + data_len);
1114 len = sizeof(*di) + name_len + data_len;
1115 di = (struct btrfs_dir_item *)((char *)di + len);
1118 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1135 static int __copy_first_ref(int num, u64 dir, int index,
1136 struct fs_path *p, void *ctx)
1139 struct fs_path *pt = ctx;
1141 ret = fs_path_copy(pt, p);
1145 /* we want the first only */
1150 * Retrieve the first path of an inode. If an inode has more then one
1151 * ref/hardlink, this is ignored.
1153 static int get_inode_path(struct btrfs_root *root,
1154 u64 ino, struct fs_path *path)
1157 struct btrfs_key key, found_key;
1158 struct btrfs_path *p;
1160 p = alloc_path_for_send();
1164 fs_path_reset(path);
1167 key.type = BTRFS_INODE_REF_KEY;
1170 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1177 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1178 if (found_key.objectid != ino ||
1179 (found_key.type != BTRFS_INODE_REF_KEY &&
1180 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1185 ret = iterate_inode_ref(root, p, &found_key, 1,
1186 __copy_first_ref, path);
1196 struct backref_ctx {
1197 struct send_ctx *sctx;
1199 /* number of total found references */
1203 * used for clones found in send_root. clones found behind cur_objectid
1204 * and cur_offset are not considered as allowed clones.
1209 /* may be truncated in case it's the last extent in a file */
1212 /* Just to check for bugs in backref resolving */
1216 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1218 u64 root = (u64)(uintptr_t)key;
1219 const struct clone_root *cr = elt;
1221 if (root < cr->root->root_key.objectid)
1223 if (root > cr->root->root_key.objectid)
1228 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1230 const struct clone_root *cr1 = e1;
1231 const struct clone_root *cr2 = e2;
1233 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
1235 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
1241 * Called for every backref that is found for the current extent.
1242 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1244 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1246 struct backref_ctx *bctx = ctx_;
1247 struct clone_root *found;
1249 /* First check if the root is in the list of accepted clone sources */
1250 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1251 bctx->sctx->clone_roots_cnt,
1252 sizeof(struct clone_root),
1253 __clone_root_cmp_bsearch);
1257 if (found->root == bctx->sctx->send_root &&
1258 ino == bctx->cur_objectid &&
1259 offset == bctx->cur_offset) {
1260 bctx->found_itself = 1;
1264 * Make sure we don't consider clones from send_root that are
1265 * behind the current inode/offset.
1267 if (found->root == bctx->sctx->send_root) {
1269 * If the source inode was not yet processed we can't issue a
1270 * clone operation, as the source extent does not exist yet at
1271 * the destination of the stream.
1273 if (ino > bctx->cur_objectid)
1276 * We clone from the inode currently being sent as long as the
1277 * source extent is already processed, otherwise we could try
1278 * to clone from an extent that does not exist yet at the
1279 * destination of the stream.
1281 if (ino == bctx->cur_objectid &&
1282 offset + bctx->extent_len >
1283 bctx->sctx->cur_inode_next_write_offset)
1288 found->found_refs++;
1289 if (ino < found->ino) {
1291 found->offset = offset;
1292 } else if (found->ino == ino) {
1294 * same extent found more then once in the same file.
1296 if (found->offset > offset + bctx->extent_len)
1297 found->offset = offset;
1304 * Given an inode, offset and extent item, it finds a good clone for a clone
1305 * instruction. Returns -ENOENT when none could be found. The function makes
1306 * sure that the returned clone is usable at the point where sending is at the
1307 * moment. This means, that no clones are accepted which lie behind the current
1310 * path must point to the extent item when called.
1312 static int find_extent_clone(struct send_ctx *sctx,
1313 struct btrfs_path *path,
1314 u64 ino, u64 data_offset,
1316 struct clone_root **found)
1318 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1324 u64 extent_item_pos;
1326 struct btrfs_file_extent_item *fi;
1327 struct extent_buffer *eb = path->nodes[0];
1328 struct backref_ctx backref_ctx = {0};
1329 struct clone_root *cur_clone_root;
1330 struct btrfs_key found_key;
1331 struct btrfs_path *tmp_path;
1332 struct btrfs_extent_item *ei;
1336 tmp_path = alloc_path_for_send();
1340 /* We only use this path under the commit sem */
1341 tmp_path->need_commit_sem = 0;
1343 if (data_offset >= ino_size) {
1345 * There may be extents that lie behind the file's size.
1346 * I at least had this in combination with snapshotting while
1347 * writing large files.
1353 fi = btrfs_item_ptr(eb, path->slots[0],
1354 struct btrfs_file_extent_item);
1355 extent_type = btrfs_file_extent_type(eb, fi);
1356 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1360 compressed = btrfs_file_extent_compression(eb, fi);
1362 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1363 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1364 if (disk_byte == 0) {
1368 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1370 down_read(&fs_info->commit_root_sem);
1371 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1372 &found_key, &flags);
1373 up_read(&fs_info->commit_root_sem);
1377 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1382 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1383 struct btrfs_extent_item);
1385 * Backreference walking (iterate_extent_inodes() below) is currently
1386 * too expensive when an extent has a large number of references, both
1387 * in time spent and used memory. So for now just fallback to write
1388 * operations instead of clone operations when an extent has more than
1389 * a certain amount of references.
1391 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1395 btrfs_release_path(tmp_path);
1398 * Setup the clone roots.
1400 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1401 cur_clone_root = sctx->clone_roots + i;
1402 cur_clone_root->ino = (u64)-1;
1403 cur_clone_root->offset = 0;
1404 cur_clone_root->found_refs = 0;
1407 backref_ctx.sctx = sctx;
1408 backref_ctx.found = 0;
1409 backref_ctx.cur_objectid = ino;
1410 backref_ctx.cur_offset = data_offset;
1411 backref_ctx.found_itself = 0;
1412 backref_ctx.extent_len = num_bytes;
1415 * The last extent of a file may be too large due to page alignment.
1416 * We need to adjust extent_len in this case so that the checks in
1417 * __iterate_backrefs work.
1419 if (data_offset + num_bytes >= ino_size)
1420 backref_ctx.extent_len = ino_size - data_offset;
1423 * Now collect all backrefs.
1425 if (compressed == BTRFS_COMPRESS_NONE)
1426 extent_item_pos = logical - found_key.objectid;
1428 extent_item_pos = 0;
1429 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1430 extent_item_pos, 1, __iterate_backrefs,
1431 &backref_ctx, false);
1436 down_read(&fs_info->commit_root_sem);
1437 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
1439 * A transaction commit for a transaction in which block group
1440 * relocation was done just happened.
1441 * The disk_bytenr of the file extent item we processed is
1442 * possibly stale, referring to the extent's location before
1443 * relocation. So act as if we haven't found any clone sources
1444 * and fallback to write commands, which will read the correct
1445 * data from the new extent location. Otherwise we will fail
1446 * below because we haven't found our own back reference or we
1447 * could be getting incorrect sources in case the old extent
1448 * was already reallocated after the relocation.
1450 up_read(&fs_info->commit_root_sem);
1454 up_read(&fs_info->commit_root_sem);
1456 if (!backref_ctx.found_itself) {
1457 /* found a bug in backref code? */
1460 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1461 ino, data_offset, disk_byte, found_key.objectid);
1465 btrfs_debug(fs_info,
1466 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1467 data_offset, ino, num_bytes, logical);
1469 if (!backref_ctx.found)
1470 btrfs_debug(fs_info, "no clones found");
1472 cur_clone_root = NULL;
1473 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1474 if (sctx->clone_roots[i].found_refs) {
1475 if (!cur_clone_root)
1476 cur_clone_root = sctx->clone_roots + i;
1477 else if (sctx->clone_roots[i].root == sctx->send_root)
1478 /* prefer clones from send_root over others */
1479 cur_clone_root = sctx->clone_roots + i;
1484 if (cur_clone_root) {
1485 *found = cur_clone_root;
1492 btrfs_free_path(tmp_path);
1496 static int read_symlink(struct btrfs_root *root,
1498 struct fs_path *dest)
1501 struct btrfs_path *path;
1502 struct btrfs_key key;
1503 struct btrfs_file_extent_item *ei;
1509 path = alloc_path_for_send();
1514 key.type = BTRFS_EXTENT_DATA_KEY;
1516 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1521 * An empty symlink inode. Can happen in rare error paths when
1522 * creating a symlink (transaction committed before the inode
1523 * eviction handler removed the symlink inode items and a crash
1524 * happened in between or the subvol was snapshoted in between).
1525 * Print an informative message to dmesg/syslog so that the user
1526 * can delete the symlink.
1528 btrfs_err(root->fs_info,
1529 "Found empty symlink inode %llu at root %llu",
1530 ino, root->root_key.objectid);
1535 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1536 struct btrfs_file_extent_item);
1537 type = btrfs_file_extent_type(path->nodes[0], ei);
1538 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1539 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1540 BUG_ON(compression);
1542 off = btrfs_file_extent_inline_start(ei);
1543 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
1545 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1548 btrfs_free_path(path);
1553 * Helper function to generate a file name that is unique in the root of
1554 * send_root and parent_root. This is used to generate names for orphan inodes.
1556 static int gen_unique_name(struct send_ctx *sctx,
1558 struct fs_path *dest)
1561 struct btrfs_path *path;
1562 struct btrfs_dir_item *di;
1567 path = alloc_path_for_send();
1572 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1574 ASSERT(len < sizeof(tmp));
1576 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1577 path, BTRFS_FIRST_FREE_OBJECTID,
1578 tmp, strlen(tmp), 0);
1579 btrfs_release_path(path);
1585 /* not unique, try again */
1590 if (!sctx->parent_root) {
1596 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1597 path, BTRFS_FIRST_FREE_OBJECTID,
1598 tmp, strlen(tmp), 0);
1599 btrfs_release_path(path);
1605 /* not unique, try again */
1613 ret = fs_path_add(dest, tmp, strlen(tmp));
1616 btrfs_free_path(path);
1621 inode_state_no_change,
1622 inode_state_will_create,
1623 inode_state_did_create,
1624 inode_state_will_delete,
1625 inode_state_did_delete,
1628 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1636 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1638 if (ret < 0 && ret != -ENOENT)
1642 if (!sctx->parent_root) {
1643 right_ret = -ENOENT;
1645 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1646 NULL, NULL, NULL, NULL);
1647 if (ret < 0 && ret != -ENOENT)
1652 if (!left_ret && !right_ret) {
1653 if (left_gen == gen && right_gen == gen) {
1654 ret = inode_state_no_change;
1655 } else if (left_gen == gen) {
1656 if (ino < sctx->send_progress)
1657 ret = inode_state_did_create;
1659 ret = inode_state_will_create;
1660 } else if (right_gen == gen) {
1661 if (ino < sctx->send_progress)
1662 ret = inode_state_did_delete;
1664 ret = inode_state_will_delete;
1668 } else if (!left_ret) {
1669 if (left_gen == gen) {
1670 if (ino < sctx->send_progress)
1671 ret = inode_state_did_create;
1673 ret = inode_state_will_create;
1677 } else if (!right_ret) {
1678 if (right_gen == gen) {
1679 if (ino < sctx->send_progress)
1680 ret = inode_state_did_delete;
1682 ret = inode_state_will_delete;
1694 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1698 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1701 ret = get_cur_inode_state(sctx, ino, gen);
1705 if (ret == inode_state_no_change ||
1706 ret == inode_state_did_create ||
1707 ret == inode_state_will_delete)
1717 * Helper function to lookup a dir item in a dir.
1719 static int lookup_dir_item_inode(struct btrfs_root *root,
1720 u64 dir, const char *name, int name_len,
1724 struct btrfs_dir_item *di;
1725 struct btrfs_key key;
1726 struct btrfs_path *path;
1728 path = alloc_path_for_send();
1732 di = btrfs_lookup_dir_item(NULL, root, path,
1733 dir, name, name_len, 0);
1734 if (IS_ERR_OR_NULL(di)) {
1735 ret = di ? PTR_ERR(di) : -ENOENT;
1738 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1739 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1743 *found_inode = key.objectid;
1746 btrfs_free_path(path);
1751 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1752 * generation of the parent dir and the name of the dir entry.
1754 static int get_first_ref(struct btrfs_root *root, u64 ino,
1755 u64 *dir, u64 *dir_gen, struct fs_path *name)
1758 struct btrfs_key key;
1759 struct btrfs_key found_key;
1760 struct btrfs_path *path;
1764 path = alloc_path_for_send();
1769 key.type = BTRFS_INODE_REF_KEY;
1772 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1776 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1778 if (ret || found_key.objectid != ino ||
1779 (found_key.type != BTRFS_INODE_REF_KEY &&
1780 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1785 if (found_key.type == BTRFS_INODE_REF_KEY) {
1786 struct btrfs_inode_ref *iref;
1787 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1788 struct btrfs_inode_ref);
1789 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1790 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1791 (unsigned long)(iref + 1),
1793 parent_dir = found_key.offset;
1795 struct btrfs_inode_extref *extref;
1796 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1797 struct btrfs_inode_extref);
1798 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1799 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1800 (unsigned long)&extref->name, len);
1801 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1805 btrfs_release_path(path);
1808 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1817 btrfs_free_path(path);
1821 static int is_first_ref(struct btrfs_root *root,
1823 const char *name, int name_len)
1826 struct fs_path *tmp_name;
1829 tmp_name = fs_path_alloc();
1833 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1837 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1842 ret = !memcmp(tmp_name->start, name, name_len);
1845 fs_path_free(tmp_name);
1850 * Used by process_recorded_refs to determine if a new ref would overwrite an
1851 * already existing ref. In case it detects an overwrite, it returns the
1852 * inode/gen in who_ino/who_gen.
1853 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1854 * to make sure later references to the overwritten inode are possible.
1855 * Orphanizing is however only required for the first ref of an inode.
1856 * process_recorded_refs does an additional is_first_ref check to see if
1857 * orphanizing is really required.
1859 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1860 const char *name, int name_len,
1861 u64 *who_ino, u64 *who_gen, u64 *who_mode)
1865 u64 other_inode = 0;
1867 if (!sctx->parent_root)
1870 ret = is_inode_existent(sctx, dir, dir_gen);
1875 * If we have a parent root we need to verify that the parent dir was
1876 * not deleted and then re-created, if it was then we have no overwrite
1877 * and we can just unlink this entry.
1879 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
1880 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1882 if (ret < 0 && ret != -ENOENT)
1892 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1894 if (ret < 0 && ret != -ENOENT)
1902 * Check if the overwritten ref was already processed. If yes, the ref
1903 * was already unlinked/moved, so we can safely assume that we will not
1904 * overwrite anything at this point in time.
1906 if (other_inode > sctx->send_progress ||
1907 is_waiting_for_move(sctx, other_inode)) {
1908 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1909 who_gen, who_mode, NULL, NULL, NULL);
1914 *who_ino = other_inode;
1924 * Checks if the ref was overwritten by an already processed inode. This is
1925 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1926 * thus the orphan name needs be used.
1927 * process_recorded_refs also uses it to avoid unlinking of refs that were
1930 static int did_overwrite_ref(struct send_ctx *sctx,
1931 u64 dir, u64 dir_gen,
1932 u64 ino, u64 ino_gen,
1933 const char *name, int name_len)
1939 if (!sctx->parent_root)
1942 ret = is_inode_existent(sctx, dir, dir_gen);
1946 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1947 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1949 if (ret < 0 && ret != -ENOENT)
1959 /* check if the ref was overwritten by another ref */
1960 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1962 if (ret < 0 && ret != -ENOENT)
1965 /* was never and will never be overwritten */
1970 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1975 if (ow_inode == ino && gen == ino_gen) {
1981 * We know that it is or will be overwritten. Check this now.
1982 * The current inode being processed might have been the one that caused
1983 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1984 * the current inode being processed.
1986 if ((ow_inode < sctx->send_progress) ||
1987 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1988 gen == sctx->cur_inode_gen))
1998 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1999 * that got overwritten. This is used by process_recorded_refs to determine
2000 * if it has to use the path as returned by get_cur_path or the orphan name.
2002 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
2005 struct fs_path *name = NULL;
2009 if (!sctx->parent_root)
2012 name = fs_path_alloc();
2016 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
2020 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2021 name->start, fs_path_len(name));
2029 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
2030 * so we need to do some special handling in case we have clashes. This function
2031 * takes care of this with the help of name_cache_entry::radix_list.
2032 * In case of error, nce is kfreed.
2034 static int name_cache_insert(struct send_ctx *sctx,
2035 struct name_cache_entry *nce)
2038 struct list_head *nce_head;
2040 nce_head = radix_tree_lookup(&sctx->name_cache,
2041 (unsigned long)nce->ino);
2043 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
2048 INIT_LIST_HEAD(nce_head);
2050 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
2057 list_add_tail(&nce->radix_list, nce_head);
2058 list_add_tail(&nce->list, &sctx->name_cache_list);
2059 sctx->name_cache_size++;
2064 static void name_cache_delete(struct send_ctx *sctx,
2065 struct name_cache_entry *nce)
2067 struct list_head *nce_head;
2069 nce_head = radix_tree_lookup(&sctx->name_cache,
2070 (unsigned long)nce->ino);
2072 btrfs_err(sctx->send_root->fs_info,
2073 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2074 nce->ino, sctx->name_cache_size);
2077 list_del(&nce->radix_list);
2078 list_del(&nce->list);
2079 sctx->name_cache_size--;
2082 * We may not get to the final release of nce_head if the lookup fails
2084 if (nce_head && list_empty(nce_head)) {
2085 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2090 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2093 struct list_head *nce_head;
2094 struct name_cache_entry *cur;
2096 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2100 list_for_each_entry(cur, nce_head, radix_list) {
2101 if (cur->ino == ino && cur->gen == gen)
2108 * Remove some entries from the beginning of name_cache_list.
2110 static void name_cache_clean_unused(struct send_ctx *sctx)
2112 struct name_cache_entry *nce;
2114 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2117 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2118 nce = list_entry(sctx->name_cache_list.next,
2119 struct name_cache_entry, list);
2120 name_cache_delete(sctx, nce);
2125 static void name_cache_free(struct send_ctx *sctx)
2127 struct name_cache_entry *nce;
2129 while (!list_empty(&sctx->name_cache_list)) {
2130 nce = list_entry(sctx->name_cache_list.next,
2131 struct name_cache_entry, list);
2132 name_cache_delete(sctx, nce);
2138 * Used by get_cur_path for each ref up to the root.
2139 * Returns 0 if it succeeded.
2140 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2141 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2142 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2143 * Returns <0 in case of error.
2145 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2149 struct fs_path *dest)
2153 struct name_cache_entry *nce = NULL;
2156 * First check if we already did a call to this function with the same
2157 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2158 * return the cached result.
2160 nce = name_cache_search(sctx, ino, gen);
2162 if (ino < sctx->send_progress && nce->need_later_update) {
2163 name_cache_delete(sctx, nce);
2168 * Removes the entry from the list and adds it back to
2169 * the end. This marks the entry as recently used so
2170 * that name_cache_clean_unused does not remove it.
2172 list_move_tail(&nce->list, &sctx->name_cache_list);
2174 *parent_ino = nce->parent_ino;
2175 *parent_gen = nce->parent_gen;
2176 ret = fs_path_add(dest, nce->name, nce->name_len);
2185 * If the inode is not existent yet, add the orphan name and return 1.
2186 * This should only happen for the parent dir that we determine in
2189 ret = is_inode_existent(sctx, ino, gen);
2194 ret = gen_unique_name(sctx, ino, gen, dest);
2202 * Depending on whether the inode was already processed or not, use
2203 * send_root or parent_root for ref lookup.
2205 if (ino < sctx->send_progress)
2206 ret = get_first_ref(sctx->send_root, ino,
2207 parent_ino, parent_gen, dest);
2209 ret = get_first_ref(sctx->parent_root, ino,
2210 parent_ino, parent_gen, dest);
2215 * Check if the ref was overwritten by an inode's ref that was processed
2216 * earlier. If yes, treat as orphan and return 1.
2218 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2219 dest->start, dest->end - dest->start);
2223 fs_path_reset(dest);
2224 ret = gen_unique_name(sctx, ino, gen, dest);
2232 * Store the result of the lookup in the name cache.
2234 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2242 nce->parent_ino = *parent_ino;
2243 nce->parent_gen = *parent_gen;
2244 nce->name_len = fs_path_len(dest);
2246 strcpy(nce->name, dest->start);
2248 if (ino < sctx->send_progress)
2249 nce->need_later_update = 0;
2251 nce->need_later_update = 1;
2253 nce_ret = name_cache_insert(sctx, nce);
2256 name_cache_clean_unused(sctx);
2263 * Magic happens here. This function returns the first ref to an inode as it
2264 * would look like while receiving the stream at this point in time.
2265 * We walk the path up to the root. For every inode in between, we check if it
2266 * was already processed/sent. If yes, we continue with the parent as found
2267 * in send_root. If not, we continue with the parent as found in parent_root.
2268 * If we encounter an inode that was deleted at this point in time, we use the
2269 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2270 * that were not created yet and overwritten inodes/refs.
2272 * When do we have orphan inodes:
2273 * 1. When an inode is freshly created and thus no valid refs are available yet
2274 * 2. When a directory lost all it's refs (deleted) but still has dir items
2275 * inside which were not processed yet (pending for move/delete). If anyone
2276 * tried to get the path to the dir items, it would get a path inside that
2278 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2279 * of an unprocessed inode. If in that case the first ref would be
2280 * overwritten, the overwritten inode gets "orphanized". Later when we
2281 * process this overwritten inode, it is restored at a new place by moving
2284 * sctx->send_progress tells this function at which point in time receiving
2287 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2288 struct fs_path *dest)
2291 struct fs_path *name = NULL;
2292 u64 parent_inode = 0;
2296 name = fs_path_alloc();
2303 fs_path_reset(dest);
2305 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2306 struct waiting_dir_move *wdm;
2308 fs_path_reset(name);
2310 if (is_waiting_for_rm(sctx, ino, gen)) {
2311 ret = gen_unique_name(sctx, ino, gen, name);
2314 ret = fs_path_add_path(dest, name);
2318 wdm = get_waiting_dir_move(sctx, ino);
2319 if (wdm && wdm->orphanized) {
2320 ret = gen_unique_name(sctx, ino, gen, name);
2323 ret = get_first_ref(sctx->parent_root, ino,
2324 &parent_inode, &parent_gen, name);
2326 ret = __get_cur_name_and_parent(sctx, ino, gen,
2336 ret = fs_path_add_path(dest, name);
2347 fs_path_unreverse(dest);
2352 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2354 static int send_subvol_begin(struct send_ctx *sctx)
2357 struct btrfs_root *send_root = sctx->send_root;
2358 struct btrfs_root *parent_root = sctx->parent_root;
2359 struct btrfs_path *path;
2360 struct btrfs_key key;
2361 struct btrfs_root_ref *ref;
2362 struct extent_buffer *leaf;
2366 path = btrfs_alloc_path();
2370 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2372 btrfs_free_path(path);
2376 key.objectid = send_root->root_key.objectid;
2377 key.type = BTRFS_ROOT_BACKREF_KEY;
2380 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2389 leaf = path->nodes[0];
2390 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2391 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2392 key.objectid != send_root->root_key.objectid) {
2396 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2397 namelen = btrfs_root_ref_name_len(leaf, ref);
2398 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2399 btrfs_release_path(path);
2402 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2406 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2411 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2413 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2414 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2415 sctx->send_root->root_item.received_uuid);
2417 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2418 sctx->send_root->root_item.uuid);
2420 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2421 btrfs_root_ctransid(&sctx->send_root->root_item));
2423 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2424 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2425 parent_root->root_item.received_uuid);
2427 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2428 parent_root->root_item.uuid);
2429 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2430 btrfs_root_ctransid(&sctx->parent_root->root_item));
2433 ret = send_cmd(sctx);
2437 btrfs_free_path(path);
2442 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2444 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2448 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
2450 p = fs_path_alloc();
2454 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2458 ret = get_cur_path(sctx, ino, gen, p);
2461 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2462 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2464 ret = send_cmd(sctx);
2472 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2474 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2478 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
2480 p = fs_path_alloc();
2484 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2488 ret = get_cur_path(sctx, ino, gen, p);
2491 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2492 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2494 ret = send_cmd(sctx);
2502 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2504 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2508 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2511 p = fs_path_alloc();
2515 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2519 ret = get_cur_path(sctx, ino, gen, p);
2522 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2523 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2524 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2526 ret = send_cmd(sctx);
2534 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2536 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2538 struct fs_path *p = NULL;
2539 struct btrfs_inode_item *ii;
2540 struct btrfs_path *path = NULL;
2541 struct extent_buffer *eb;
2542 struct btrfs_key key;
2545 btrfs_debug(fs_info, "send_utimes %llu", ino);
2547 p = fs_path_alloc();
2551 path = alloc_path_for_send();
2558 key.type = BTRFS_INODE_ITEM_KEY;
2560 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2566 eb = path->nodes[0];
2567 slot = path->slots[0];
2568 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2570 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2574 ret = get_cur_path(sctx, ino, gen, p);
2577 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2578 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2579 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2580 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2581 /* TODO Add otime support when the otime patches get into upstream */
2583 ret = send_cmd(sctx);
2588 btrfs_free_path(path);
2593 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2594 * a valid path yet because we did not process the refs yet. So, the inode
2595 * is created as orphan.
2597 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2599 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2607 btrfs_debug(fs_info, "send_create_inode %llu", ino);
2609 p = fs_path_alloc();
2613 if (ino != sctx->cur_ino) {
2614 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2619 gen = sctx->cur_inode_gen;
2620 mode = sctx->cur_inode_mode;
2621 rdev = sctx->cur_inode_rdev;
2624 if (S_ISREG(mode)) {
2625 cmd = BTRFS_SEND_C_MKFILE;
2626 } else if (S_ISDIR(mode)) {
2627 cmd = BTRFS_SEND_C_MKDIR;
2628 } else if (S_ISLNK(mode)) {
2629 cmd = BTRFS_SEND_C_SYMLINK;
2630 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2631 cmd = BTRFS_SEND_C_MKNOD;
2632 } else if (S_ISFIFO(mode)) {
2633 cmd = BTRFS_SEND_C_MKFIFO;
2634 } else if (S_ISSOCK(mode)) {
2635 cmd = BTRFS_SEND_C_MKSOCK;
2637 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2638 (int)(mode & S_IFMT));
2643 ret = begin_cmd(sctx, cmd);
2647 ret = gen_unique_name(sctx, ino, gen, p);
2651 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2652 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2654 if (S_ISLNK(mode)) {
2656 ret = read_symlink(sctx->send_root, ino, p);
2659 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2660 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2661 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2662 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2663 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2666 ret = send_cmd(sctx);
2678 * We need some special handling for inodes that get processed before the parent
2679 * directory got created. See process_recorded_refs for details.
2680 * This function does the check if we already created the dir out of order.
2682 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2686 struct btrfs_path *path = NULL;
2687 struct btrfs_key key;
2688 struct btrfs_key found_key;
2689 struct btrfs_key di_key;
2690 struct btrfs_dir_item *di;
2692 path = alloc_path_for_send();
2697 key.type = BTRFS_DIR_INDEX_KEY;
2700 btrfs_for_each_slot(sctx->send_root, &key, &found_key, path, iter_ret) {
2701 struct extent_buffer *eb = path->nodes[0];
2703 if (found_key.objectid != key.objectid ||
2704 found_key.type != key.type) {
2709 di = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dir_item);
2710 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2712 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2713 di_key.objectid < sctx->send_progress) {
2718 /* Catch error found during iteration */
2722 btrfs_free_path(path);
2727 * Only creates the inode if it is:
2728 * 1. Not a directory
2729 * 2. Or a directory which was not created already due to out of order
2730 * directories. See did_create_dir and process_recorded_refs for details.
2732 static int send_create_inode_if_needed(struct send_ctx *sctx)
2736 if (S_ISDIR(sctx->cur_inode_mode)) {
2737 ret = did_create_dir(sctx, sctx->cur_ino);
2744 return send_create_inode(sctx, sctx->cur_ino);
2747 struct recorded_ref {
2748 struct list_head list;
2750 struct fs_path *full_path;
2756 static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2758 ref->full_path = path;
2759 ref->name = (char *)kbasename(ref->full_path->start);
2760 ref->name_len = ref->full_path->end - ref->name;
2764 * We need to process new refs before deleted refs, but compare_tree gives us
2765 * everything mixed. So we first record all refs and later process them.
2766 * This function is a helper to record one ref.
2768 static int __record_ref(struct list_head *head, u64 dir,
2769 u64 dir_gen, struct fs_path *path)
2771 struct recorded_ref *ref;
2773 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
2778 ref->dir_gen = dir_gen;
2779 set_ref_path(ref, path);
2780 list_add_tail(&ref->list, head);
2784 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2786 struct recorded_ref *new;
2788 new = kmalloc(sizeof(*ref), GFP_KERNEL);
2792 new->dir = ref->dir;
2793 new->dir_gen = ref->dir_gen;
2794 new->full_path = NULL;
2795 INIT_LIST_HEAD(&new->list);
2796 list_add_tail(&new->list, list);
2800 static void __free_recorded_refs(struct list_head *head)
2802 struct recorded_ref *cur;
2804 while (!list_empty(head)) {
2805 cur = list_entry(head->next, struct recorded_ref, list);
2806 fs_path_free(cur->full_path);
2807 list_del(&cur->list);
2812 static void free_recorded_refs(struct send_ctx *sctx)
2814 __free_recorded_refs(&sctx->new_refs);
2815 __free_recorded_refs(&sctx->deleted_refs);
2819 * Renames/moves a file/dir to its orphan name. Used when the first
2820 * ref of an unprocessed inode gets overwritten and for all non empty
2823 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2824 struct fs_path *path)
2827 struct fs_path *orphan;
2829 orphan = fs_path_alloc();
2833 ret = gen_unique_name(sctx, ino, gen, orphan);
2837 ret = send_rename(sctx, path, orphan);
2840 fs_path_free(orphan);
2844 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
2845 u64 dir_ino, u64 dir_gen)
2847 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2848 struct rb_node *parent = NULL;
2849 struct orphan_dir_info *entry, *odi;
2853 entry = rb_entry(parent, struct orphan_dir_info, node);
2854 if (dir_ino < entry->ino)
2856 else if (dir_ino > entry->ino)
2857 p = &(*p)->rb_right;
2858 else if (dir_gen < entry->gen)
2860 else if (dir_gen > entry->gen)
2861 p = &(*p)->rb_right;
2866 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2868 return ERR_PTR(-ENOMEM);
2871 odi->last_dir_index_offset = 0;
2873 rb_link_node(&odi->node, parent, p);
2874 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2878 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
2879 u64 dir_ino, u64 gen)
2881 struct rb_node *n = sctx->orphan_dirs.rb_node;
2882 struct orphan_dir_info *entry;
2885 entry = rb_entry(n, struct orphan_dir_info, node);
2886 if (dir_ino < entry->ino)
2888 else if (dir_ino > entry->ino)
2890 else if (gen < entry->gen)
2892 else if (gen > entry->gen)
2900 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
2902 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
2907 static void free_orphan_dir_info(struct send_ctx *sctx,
2908 struct orphan_dir_info *odi)
2912 rb_erase(&odi->node, &sctx->orphan_dirs);
2917 * Returns 1 if a directory can be removed at this point in time.
2918 * We check this by iterating all dir items and checking if the inode behind
2919 * the dir item was already processed.
2921 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2926 struct btrfs_root *root = sctx->parent_root;
2927 struct btrfs_path *path;
2928 struct btrfs_key key;
2929 struct btrfs_key found_key;
2930 struct btrfs_key loc;
2931 struct btrfs_dir_item *di;
2932 struct orphan_dir_info *odi = NULL;
2935 * Don't try to rmdir the top/root subvolume dir.
2937 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2940 path = alloc_path_for_send();
2945 key.type = BTRFS_DIR_INDEX_KEY;
2948 odi = get_orphan_dir_info(sctx, dir, dir_gen);
2950 key.offset = odi->last_dir_index_offset;
2952 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
2953 struct waiting_dir_move *dm;
2955 if (found_key.objectid != key.objectid ||
2956 found_key.type != key.type)
2959 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2960 struct btrfs_dir_item);
2961 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2963 dm = get_waiting_dir_move(sctx, loc.objectid);
2965 odi = add_orphan_dir_info(sctx, dir, dir_gen);
2971 odi->last_dir_index_offset = found_key.offset;
2972 dm->rmdir_ino = dir;
2973 dm->rmdir_gen = dir_gen;
2978 if (loc.objectid > send_progress) {
2979 odi = add_orphan_dir_info(sctx, dir, dir_gen);
2985 odi->last_dir_index_offset = found_key.offset;
2994 free_orphan_dir_info(sctx, odi);
2999 btrfs_free_path(path);
3003 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3005 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3007 return entry != NULL;
3010 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3012 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3013 struct rb_node *parent = NULL;
3014 struct waiting_dir_move *entry, *dm;
3016 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
3022 dm->orphanized = orphanized;
3026 entry = rb_entry(parent, struct waiting_dir_move, node);
3027 if (ino < entry->ino) {
3029 } else if (ino > entry->ino) {
3030 p = &(*p)->rb_right;
3037 rb_link_node(&dm->node, parent, p);
3038 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3042 static struct waiting_dir_move *
3043 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3045 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3046 struct waiting_dir_move *entry;
3049 entry = rb_entry(n, struct waiting_dir_move, node);
3050 if (ino < entry->ino)
3052 else if (ino > entry->ino)
3060 static void free_waiting_dir_move(struct send_ctx *sctx,
3061 struct waiting_dir_move *dm)
3065 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3069 static int add_pending_dir_move(struct send_ctx *sctx,
3073 struct list_head *new_refs,
3074 struct list_head *deleted_refs,
3075 const bool is_orphan)
3077 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3078 struct rb_node *parent = NULL;
3079 struct pending_dir_move *entry = NULL, *pm;
3080 struct recorded_ref *cur;
3084 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3087 pm->parent_ino = parent_ino;
3090 INIT_LIST_HEAD(&pm->list);
3091 INIT_LIST_HEAD(&pm->update_refs);
3092 RB_CLEAR_NODE(&pm->node);
3096 entry = rb_entry(parent, struct pending_dir_move, node);
3097 if (parent_ino < entry->parent_ino) {
3099 } else if (parent_ino > entry->parent_ino) {
3100 p = &(*p)->rb_right;
3107 list_for_each_entry(cur, deleted_refs, list) {
3108 ret = dup_ref(cur, &pm->update_refs);
3112 list_for_each_entry(cur, new_refs, list) {
3113 ret = dup_ref(cur, &pm->update_refs);
3118 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3123 list_add_tail(&pm->list, &entry->list);
3125 rb_link_node(&pm->node, parent, p);
3126 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3131 __free_recorded_refs(&pm->update_refs);
3137 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3140 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3141 struct pending_dir_move *entry;
3144 entry = rb_entry(n, struct pending_dir_move, node);
3145 if (parent_ino < entry->parent_ino)
3147 else if (parent_ino > entry->parent_ino)
3155 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3156 u64 ino, u64 gen, u64 *ancestor_ino)
3159 u64 parent_inode = 0;
3161 u64 start_ino = ino;
3164 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3165 fs_path_reset(name);
3167 if (is_waiting_for_rm(sctx, ino, gen))
3169 if (is_waiting_for_move(sctx, ino)) {
3170 if (*ancestor_ino == 0)
3171 *ancestor_ino = ino;
3172 ret = get_first_ref(sctx->parent_root, ino,
3173 &parent_inode, &parent_gen, name);
3175 ret = __get_cur_name_and_parent(sctx, ino, gen,
3185 if (parent_inode == start_ino) {
3187 if (*ancestor_ino == 0)
3188 *ancestor_ino = ino;
3197 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3199 struct fs_path *from_path = NULL;
3200 struct fs_path *to_path = NULL;
3201 struct fs_path *name = NULL;
3202 u64 orig_progress = sctx->send_progress;
3203 struct recorded_ref *cur;
3204 u64 parent_ino, parent_gen;
3205 struct waiting_dir_move *dm = NULL;
3212 name = fs_path_alloc();
3213 from_path = fs_path_alloc();
3214 if (!name || !from_path) {
3219 dm = get_waiting_dir_move(sctx, pm->ino);
3221 rmdir_ino = dm->rmdir_ino;
3222 rmdir_gen = dm->rmdir_gen;
3223 is_orphan = dm->orphanized;
3224 free_waiting_dir_move(sctx, dm);
3227 ret = gen_unique_name(sctx, pm->ino,
3228 pm->gen, from_path);
3230 ret = get_first_ref(sctx->parent_root, pm->ino,
3231 &parent_ino, &parent_gen, name);
3234 ret = get_cur_path(sctx, parent_ino, parent_gen,
3238 ret = fs_path_add_path(from_path, name);
3243 sctx->send_progress = sctx->cur_ino + 1;
3244 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3248 LIST_HEAD(deleted_refs);
3249 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3250 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3251 &pm->update_refs, &deleted_refs,
3256 dm = get_waiting_dir_move(sctx, pm->ino);
3258 dm->rmdir_ino = rmdir_ino;
3259 dm->rmdir_gen = rmdir_gen;
3263 fs_path_reset(name);
3266 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3270 ret = send_rename(sctx, from_path, to_path);
3275 struct orphan_dir_info *odi;
3278 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
3280 /* already deleted */
3285 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
3291 name = fs_path_alloc();
3296 ret = get_cur_path(sctx, rmdir_ino, gen, name);
3299 ret = send_rmdir(sctx, name);
3305 ret = send_utimes(sctx, pm->ino, pm->gen);
3310 * After rename/move, need to update the utimes of both new parent(s)
3311 * and old parent(s).
3313 list_for_each_entry(cur, &pm->update_refs, list) {
3315 * The parent inode might have been deleted in the send snapshot
3317 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3318 NULL, NULL, NULL, NULL, NULL);
3319 if (ret == -ENOENT) {
3326 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3333 fs_path_free(from_path);
3334 fs_path_free(to_path);
3335 sctx->send_progress = orig_progress;
3340 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3342 if (!list_empty(&m->list))
3344 if (!RB_EMPTY_NODE(&m->node))
3345 rb_erase(&m->node, &sctx->pending_dir_moves);
3346 __free_recorded_refs(&m->update_refs);
3350 static void tail_append_pending_moves(struct send_ctx *sctx,
3351 struct pending_dir_move *moves,
3352 struct list_head *stack)
3354 if (list_empty(&moves->list)) {
3355 list_add_tail(&moves->list, stack);
3358 list_splice_init(&moves->list, &list);
3359 list_add_tail(&moves->list, stack);
3360 list_splice_tail(&list, stack);
3362 if (!RB_EMPTY_NODE(&moves->node)) {
3363 rb_erase(&moves->node, &sctx->pending_dir_moves);
3364 RB_CLEAR_NODE(&moves->node);
3368 static int apply_children_dir_moves(struct send_ctx *sctx)
3370 struct pending_dir_move *pm;
3371 struct list_head stack;
3372 u64 parent_ino = sctx->cur_ino;
3375 pm = get_pending_dir_moves(sctx, parent_ino);
3379 INIT_LIST_HEAD(&stack);
3380 tail_append_pending_moves(sctx, pm, &stack);
3382 while (!list_empty(&stack)) {
3383 pm = list_first_entry(&stack, struct pending_dir_move, list);
3384 parent_ino = pm->ino;
3385 ret = apply_dir_move(sctx, pm);
3386 free_pending_move(sctx, pm);
3389 pm = get_pending_dir_moves(sctx, parent_ino);
3391 tail_append_pending_moves(sctx, pm, &stack);
3396 while (!list_empty(&stack)) {
3397 pm = list_first_entry(&stack, struct pending_dir_move, list);
3398 free_pending_move(sctx, pm);
3404 * We might need to delay a directory rename even when no ancestor directory
3405 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3406 * renamed. This happens when we rename a directory to the old name (the name
3407 * in the parent root) of some other unrelated directory that got its rename
3408 * delayed due to some ancestor with higher number that got renamed.
3414 * |---- a/ (ino 257)
3415 * | |---- file (ino 260)
3417 * |---- b/ (ino 258)
3418 * |---- c/ (ino 259)
3422 * |---- a/ (ino 258)
3423 * |---- x/ (ino 259)
3424 * |---- y/ (ino 257)
3425 * |----- file (ino 260)
3427 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3428 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3429 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3432 * 1 - rename 259 from 'c' to 'x'
3433 * 2 - rename 257 from 'a' to 'x/y'
3434 * 3 - rename 258 from 'b' to 'a'
3436 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3437 * be done right away and < 0 on error.
3439 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3440 struct recorded_ref *parent_ref,
3441 const bool is_orphan)
3443 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3444 struct btrfs_path *path;
3445 struct btrfs_key key;
3446 struct btrfs_key di_key;
3447 struct btrfs_dir_item *di;
3451 struct waiting_dir_move *wdm;
3453 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3456 path = alloc_path_for_send();
3460 key.objectid = parent_ref->dir;
3461 key.type = BTRFS_DIR_ITEM_KEY;
3462 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3464 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3467 } else if (ret > 0) {
3472 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3473 parent_ref->name_len);
3479 * di_key.objectid has the number of the inode that has a dentry in the
3480 * parent directory with the same name that sctx->cur_ino is being
3481 * renamed to. We need to check if that inode is in the send root as
3482 * well and if it is currently marked as an inode with a pending rename,
3483 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3484 * that it happens after that other inode is renamed.
3486 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3487 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3492 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3493 &left_gen, NULL, NULL, NULL, NULL);
3496 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3497 &right_gen, NULL, NULL, NULL, NULL);
3504 /* Different inode, no need to delay the rename of sctx->cur_ino */
3505 if (right_gen != left_gen) {
3510 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3511 if (wdm && !wdm->orphanized) {
3512 ret = add_pending_dir_move(sctx,
3514 sctx->cur_inode_gen,
3517 &sctx->deleted_refs,
3523 btrfs_free_path(path);
3528 * Check if inode ino2, or any of its ancestors, is inode ino1.
3529 * Return 1 if true, 0 if false and < 0 on error.
3531 static int check_ino_in_path(struct btrfs_root *root,
3536 struct fs_path *fs_path)
3541 return ino1_gen == ino2_gen;
3543 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3548 fs_path_reset(fs_path);
3549 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3553 return parent_gen == ino1_gen;
3560 * Check if inode ino1 is an ancestor of inode ino2 in the given root for any
3561 * possible path (in case ino2 is not a directory and has multiple hard links).
3562 * Return 1 if true, 0 if false and < 0 on error.
3564 static int is_ancestor(struct btrfs_root *root,
3568 struct fs_path *fs_path)
3570 bool free_fs_path = false;
3573 struct btrfs_path *path = NULL;
3574 struct btrfs_key key;
3577 fs_path = fs_path_alloc();
3580 free_fs_path = true;
3583 path = alloc_path_for_send();
3589 key.objectid = ino2;
3590 key.type = BTRFS_INODE_REF_KEY;
3593 btrfs_for_each_slot(root, &key, &key, path, iter_ret) {
3594 struct extent_buffer *leaf = path->nodes[0];
3595 int slot = path->slots[0];
3599 if (key.objectid != ino2)
3601 if (key.type != BTRFS_INODE_REF_KEY &&
3602 key.type != BTRFS_INODE_EXTREF_KEY)
3605 item_size = btrfs_item_size(leaf, slot);
3606 while (cur_offset < item_size) {
3610 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3612 struct btrfs_inode_extref *extref;
3614 ptr = btrfs_item_ptr_offset(leaf, slot);
3615 extref = (struct btrfs_inode_extref *)
3617 parent = btrfs_inode_extref_parent(leaf,
3619 cur_offset += sizeof(*extref);
3620 cur_offset += btrfs_inode_extref_name_len(leaf,
3623 parent = key.offset;
3624 cur_offset = item_size;
3627 ret = get_inode_info(root, parent, NULL, &parent_gen,
3628 NULL, NULL, NULL, NULL);
3631 ret = check_ino_in_path(root, ino1, ino1_gen,
3632 parent, parent_gen, fs_path);
3642 btrfs_free_path(path);
3644 fs_path_free(fs_path);
3648 static int wait_for_parent_move(struct send_ctx *sctx,
3649 struct recorded_ref *parent_ref,
3650 const bool is_orphan)
3653 u64 ino = parent_ref->dir;
3654 u64 ino_gen = parent_ref->dir_gen;
3655 u64 parent_ino_before, parent_ino_after;
3656 struct fs_path *path_before = NULL;
3657 struct fs_path *path_after = NULL;
3660 path_after = fs_path_alloc();
3661 path_before = fs_path_alloc();
3662 if (!path_after || !path_before) {
3668 * Our current directory inode may not yet be renamed/moved because some
3669 * ancestor (immediate or not) has to be renamed/moved first. So find if
3670 * such ancestor exists and make sure our own rename/move happens after
3671 * that ancestor is processed to avoid path build infinite loops (done
3672 * at get_cur_path()).
3674 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3675 u64 parent_ino_after_gen;
3677 if (is_waiting_for_move(sctx, ino)) {
3679 * If the current inode is an ancestor of ino in the
3680 * parent root, we need to delay the rename of the
3681 * current inode, otherwise don't delayed the rename
3682 * because we can end up with a circular dependency
3683 * of renames, resulting in some directories never
3684 * getting the respective rename operations issued in
3685 * the send stream or getting into infinite path build
3688 ret = is_ancestor(sctx->parent_root,
3689 sctx->cur_ino, sctx->cur_inode_gen,
3695 fs_path_reset(path_before);
3696 fs_path_reset(path_after);
3698 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3699 &parent_ino_after_gen, path_after);
3702 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3704 if (ret < 0 && ret != -ENOENT) {
3706 } else if (ret == -ENOENT) {
3711 len1 = fs_path_len(path_before);
3712 len2 = fs_path_len(path_after);
3713 if (ino > sctx->cur_ino &&
3714 (parent_ino_before != parent_ino_after || len1 != len2 ||
3715 memcmp(path_before->start, path_after->start, len1))) {
3718 ret = get_inode_info(sctx->parent_root, ino, NULL,
3719 &parent_ino_gen, NULL, NULL, NULL,
3723 if (ino_gen == parent_ino_gen) {
3728 ino = parent_ino_after;
3729 ino_gen = parent_ino_after_gen;
3733 fs_path_free(path_before);
3734 fs_path_free(path_after);
3737 ret = add_pending_dir_move(sctx,
3739 sctx->cur_inode_gen,
3742 &sctx->deleted_refs,
3751 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3754 struct fs_path *new_path;
3757 * Our reference's name member points to its full_path member string, so
3758 * we use here a new path.
3760 new_path = fs_path_alloc();
3764 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3766 fs_path_free(new_path);
3769 ret = fs_path_add(new_path, ref->name, ref->name_len);
3771 fs_path_free(new_path);
3775 fs_path_free(ref->full_path);
3776 set_ref_path(ref, new_path);
3782 * When processing the new references for an inode we may orphanize an existing
3783 * directory inode because its old name conflicts with one of the new references
3784 * of the current inode. Later, when processing another new reference of our
3785 * inode, we might need to orphanize another inode, but the path we have in the
3786 * reference reflects the pre-orphanization name of the directory we previously
3787 * orphanized. For example:
3789 * parent snapshot looks like:
3792 * |----- f1 (ino 257)
3793 * |----- f2 (ino 258)
3794 * |----- d1/ (ino 259)
3795 * |----- d2/ (ino 260)
3797 * send snapshot looks like:
3800 * |----- d1 (ino 258)
3801 * |----- f2/ (ino 259)
3802 * |----- f2_link/ (ino 260)
3803 * | |----- f1 (ino 257)
3805 * |----- d2 (ino 258)
3807 * When processing inode 257 we compute the name for inode 259 as "d1", and we
3808 * cache it in the name cache. Later when we start processing inode 258, when
3809 * collecting all its new references we set a full path of "d1/d2" for its new
3810 * reference with name "d2". When we start processing the new references we
3811 * start by processing the new reference with name "d1", and this results in
3812 * orphanizing inode 259, since its old reference causes a conflict. Then we
3813 * move on the next new reference, with name "d2", and we find out we must
3814 * orphanize inode 260, as its old reference conflicts with ours - but for the
3815 * orphanization we use a source path corresponding to the path we stored in the
3816 * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
3817 * receiver fail since the path component "d1/" no longer exists, it was renamed
3818 * to "o259-6-0/" when processing the previous new reference. So in this case we
3819 * must recompute the path in the new reference and use it for the new
3820 * orphanization operation.
3822 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3827 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3831 fs_path_reset(ref->full_path);
3832 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3836 ret = fs_path_add(ref->full_path, name, ref->name_len);
3840 /* Update the reference's base name pointer. */
3841 set_ref_path(ref, ref->full_path);
3848 * This does all the move/link/unlink/rmdir magic.
3850 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3852 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
3854 struct recorded_ref *cur;
3855 struct recorded_ref *cur2;
3856 struct list_head check_dirs;
3857 struct fs_path *valid_path = NULL;
3861 int did_overwrite = 0;
3863 u64 last_dir_ino_rm = 0;
3864 bool can_rename = true;
3865 bool orphanized_dir = false;
3866 bool orphanized_ancestor = false;
3868 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
3871 * This should never happen as the root dir always has the same ref
3872 * which is always '..'
3874 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3875 INIT_LIST_HEAD(&check_dirs);
3877 valid_path = fs_path_alloc();
3884 * First, check if the first ref of the current inode was overwritten
3885 * before. If yes, we know that the current inode was already orphanized
3886 * and thus use the orphan name. If not, we can use get_cur_path to
3887 * get the path of the first ref as it would like while receiving at
3888 * this point in time.
3889 * New inodes are always orphan at the beginning, so force to use the
3890 * orphan name in this case.
3891 * The first ref is stored in valid_path and will be updated if it
3892 * gets moved around.
3894 if (!sctx->cur_inode_new) {
3895 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3896 sctx->cur_inode_gen);
3902 if (sctx->cur_inode_new || did_overwrite) {
3903 ret = gen_unique_name(sctx, sctx->cur_ino,
3904 sctx->cur_inode_gen, valid_path);
3909 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3916 * Before doing any rename and link operations, do a first pass on the
3917 * new references to orphanize any unprocessed inodes that may have a
3918 * reference that conflicts with one of the new references of the current
3919 * inode. This needs to happen first because a new reference may conflict
3920 * with the old reference of a parent directory, so we must make sure
3921 * that the path used for link and rename commands don't use an
3922 * orphanized name when an ancestor was not yet orphanized.
3929 * |----- testdir/ (ino 259)
3930 * | |----- a (ino 257)
3932 * |----- b (ino 258)
3937 * |----- testdir_2/ (ino 259)
3938 * | |----- a (ino 260)
3940 * |----- testdir (ino 257)
3941 * |----- b (ino 257)
3942 * |----- b2 (ino 258)
3944 * Processing the new reference for inode 257 with name "b" may happen
3945 * before processing the new reference with name "testdir". If so, we
3946 * must make sure that by the time we send a link command to create the
3947 * hard link "b", inode 259 was already orphanized, since the generated
3948 * path in "valid_path" already contains the orphanized name for 259.
3949 * We are processing inode 257, so only later when processing 259 we do
3950 * the rename operation to change its temporary (orphanized) name to
3953 list_for_each_entry(cur, &sctx->new_refs, list) {
3954 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3957 if (ret == inode_state_will_create)
3961 * Check if this new ref would overwrite the first ref of another
3962 * unprocessed inode. If yes, orphanize the overwritten inode.
3963 * If we find an overwritten ref that is not the first ref,
3966 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3967 cur->name, cur->name_len,
3968 &ow_inode, &ow_gen, &ow_mode);
3972 ret = is_first_ref(sctx->parent_root,
3973 ow_inode, cur->dir, cur->name,
3978 struct name_cache_entry *nce;
3979 struct waiting_dir_move *wdm;
3981 if (orphanized_dir) {
3982 ret = refresh_ref_path(sctx, cur);
3987 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3991 if (S_ISDIR(ow_mode))
3992 orphanized_dir = true;
3995 * If ow_inode has its rename operation delayed
3996 * make sure that its orphanized name is used in
3997 * the source path when performing its rename
4000 if (is_waiting_for_move(sctx, ow_inode)) {
4001 wdm = get_waiting_dir_move(sctx,
4004 wdm->orphanized = true;
4008 * Make sure we clear our orphanized inode's
4009 * name from the name cache. This is because the
4010 * inode ow_inode might be an ancestor of some
4011 * other inode that will be orphanized as well
4012 * later and has an inode number greater than
4013 * sctx->send_progress. We need to prevent
4014 * future name lookups from using the old name
4015 * and get instead the orphan name.
4017 nce = name_cache_search(sctx, ow_inode, ow_gen);
4019 name_cache_delete(sctx, nce);
4024 * ow_inode might currently be an ancestor of
4025 * cur_ino, therefore compute valid_path (the
4026 * current path of cur_ino) again because it
4027 * might contain the pre-orphanization name of
4028 * ow_inode, which is no longer valid.
4030 ret = is_ancestor(sctx->parent_root,
4032 sctx->cur_ino, NULL);
4034 orphanized_ancestor = true;
4035 fs_path_reset(valid_path);
4036 ret = get_cur_path(sctx, sctx->cur_ino,
4037 sctx->cur_inode_gen,
4044 * If we previously orphanized a directory that
4045 * collided with a new reference that we already
4046 * processed, recompute the current path because
4047 * that directory may be part of the path.
4049 if (orphanized_dir) {
4050 ret = refresh_ref_path(sctx, cur);
4054 ret = send_unlink(sctx, cur->full_path);
4062 list_for_each_entry(cur, &sctx->new_refs, list) {
4064 * We may have refs where the parent directory does not exist
4065 * yet. This happens if the parent directories inum is higher
4066 * than the current inum. To handle this case, we create the
4067 * parent directory out of order. But we need to check if this
4068 * did already happen before due to other refs in the same dir.
4070 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4073 if (ret == inode_state_will_create) {
4076 * First check if any of the current inodes refs did
4077 * already create the dir.
4079 list_for_each_entry(cur2, &sctx->new_refs, list) {
4082 if (cur2->dir == cur->dir) {
4089 * If that did not happen, check if a previous inode
4090 * did already create the dir.
4093 ret = did_create_dir(sctx, cur->dir);
4097 ret = send_create_inode(sctx, cur->dir);
4103 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4104 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4113 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4115 ret = wait_for_parent_move(sctx, cur, is_orphan);
4125 * link/move the ref to the new place. If we have an orphan
4126 * inode, move it and update valid_path. If not, link or move
4127 * it depending on the inode mode.
4129 if (is_orphan && can_rename) {
4130 ret = send_rename(sctx, valid_path, cur->full_path);
4134 ret = fs_path_copy(valid_path, cur->full_path);
4137 } else if (can_rename) {
4138 if (S_ISDIR(sctx->cur_inode_mode)) {
4140 * Dirs can't be linked, so move it. For moved
4141 * dirs, we always have one new and one deleted
4142 * ref. The deleted ref is ignored later.
4144 ret = send_rename(sctx, valid_path,
4147 ret = fs_path_copy(valid_path,
4153 * We might have previously orphanized an inode
4154 * which is an ancestor of our current inode,
4155 * so our reference's full path, which was
4156 * computed before any such orphanizations, must
4159 if (orphanized_dir) {
4160 ret = update_ref_path(sctx, cur);
4164 ret = send_link(sctx, cur->full_path,
4170 ret = dup_ref(cur, &check_dirs);
4175 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4177 * Check if we can already rmdir the directory. If not,
4178 * orphanize it. For every dir item inside that gets deleted
4179 * later, we do this check again and rmdir it then if possible.
4180 * See the use of check_dirs for more details.
4182 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4187 ret = send_rmdir(sctx, valid_path);
4190 } else if (!is_orphan) {
4191 ret = orphanize_inode(sctx, sctx->cur_ino,
4192 sctx->cur_inode_gen, valid_path);
4198 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4199 ret = dup_ref(cur, &check_dirs);
4203 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4204 !list_empty(&sctx->deleted_refs)) {
4206 * We have a moved dir. Add the old parent to check_dirs
4208 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4210 ret = dup_ref(cur, &check_dirs);
4213 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4215 * We have a non dir inode. Go through all deleted refs and
4216 * unlink them if they were not already overwritten by other
4219 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4220 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4221 sctx->cur_ino, sctx->cur_inode_gen,
4222 cur->name, cur->name_len);
4227 * If we orphanized any ancestor before, we need
4228 * to recompute the full path for deleted names,
4229 * since any such path was computed before we
4230 * processed any references and orphanized any
4233 if (orphanized_ancestor) {
4234 ret = update_ref_path(sctx, cur);
4238 ret = send_unlink(sctx, cur->full_path);
4242 ret = dup_ref(cur, &check_dirs);
4247 * If the inode is still orphan, unlink the orphan. This may
4248 * happen when a previous inode did overwrite the first ref
4249 * of this inode and no new refs were added for the current
4250 * inode. Unlinking does not mean that the inode is deleted in
4251 * all cases. There may still be links to this inode in other
4255 ret = send_unlink(sctx, valid_path);
4262 * We did collect all parent dirs where cur_inode was once located. We
4263 * now go through all these dirs and check if they are pending for
4264 * deletion and if it's finally possible to perform the rmdir now.
4265 * We also update the inode stats of the parent dirs here.
4267 list_for_each_entry(cur, &check_dirs, list) {
4269 * In case we had refs into dirs that were not processed yet,
4270 * we don't need to do the utime and rmdir logic for these dirs.
4271 * The dir will be processed later.
4273 if (cur->dir > sctx->cur_ino)
4276 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4280 if (ret == inode_state_did_create ||
4281 ret == inode_state_no_change) {
4282 /* TODO delayed utimes */
4283 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
4286 } else if (ret == inode_state_did_delete &&
4287 cur->dir != last_dir_ino_rm) {
4288 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4293 ret = get_cur_path(sctx, cur->dir,
4294 cur->dir_gen, valid_path);
4297 ret = send_rmdir(sctx, valid_path);
4300 last_dir_ino_rm = cur->dir;
4308 __free_recorded_refs(&check_dirs);
4309 free_recorded_refs(sctx);
4310 fs_path_free(valid_path);
4314 static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4315 void *ctx, struct list_head *refs)
4318 struct send_ctx *sctx = ctx;
4322 p = fs_path_alloc();
4326 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
4331 ret = get_cur_path(sctx, dir, gen, p);
4334 ret = fs_path_add_path(p, name);
4338 ret = __record_ref(refs, dir, gen, p);
4346 static int __record_new_ref(int num, u64 dir, int index,
4347 struct fs_path *name,
4350 struct send_ctx *sctx = ctx;
4351 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
4355 static int __record_deleted_ref(int num, u64 dir, int index,
4356 struct fs_path *name,
4359 struct send_ctx *sctx = ctx;
4360 return record_ref(sctx->parent_root, dir, name, ctx,
4361 &sctx->deleted_refs);
4364 static int record_new_ref(struct send_ctx *sctx)
4368 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4369 sctx->cmp_key, 0, __record_new_ref, sctx);
4378 static int record_deleted_ref(struct send_ctx *sctx)
4382 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4383 sctx->cmp_key, 0, __record_deleted_ref, sctx);
4392 struct find_ref_ctx {
4395 struct btrfs_root *root;
4396 struct fs_path *name;
4400 static int __find_iref(int num, u64 dir, int index,
4401 struct fs_path *name,
4404 struct find_ref_ctx *ctx = ctx_;
4408 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4409 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
4411 * To avoid doing extra lookups we'll only do this if everything
4414 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4418 if (dir_gen != ctx->dir_gen)
4420 ctx->found_idx = num;
4426 static int find_iref(struct btrfs_root *root,
4427 struct btrfs_path *path,
4428 struct btrfs_key *key,
4429 u64 dir, u64 dir_gen, struct fs_path *name)
4432 struct find_ref_ctx ctx;
4436 ctx.dir_gen = dir_gen;
4440 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
4444 if (ctx.found_idx == -1)
4447 return ctx.found_idx;
4450 static int __record_changed_new_ref(int num, u64 dir, int index,
4451 struct fs_path *name,
4456 struct send_ctx *sctx = ctx;
4458 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4463 ret = find_iref(sctx->parent_root, sctx->right_path,
4464 sctx->cmp_key, dir, dir_gen, name);
4466 ret = __record_new_ref(num, dir, index, name, sctx);
4473 static int __record_changed_deleted_ref(int num, u64 dir, int index,
4474 struct fs_path *name,
4479 struct send_ctx *sctx = ctx;
4481 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4486 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
4487 dir, dir_gen, name);
4489 ret = __record_deleted_ref(num, dir, index, name, sctx);
4496 static int record_changed_ref(struct send_ctx *sctx)
4500 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4501 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4504 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4505 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4515 * Record and process all refs at once. Needed when an inode changes the
4516 * generation number, which means that it was deleted and recreated.
4518 static int process_all_refs(struct send_ctx *sctx,
4519 enum btrfs_compare_tree_result cmd)
4523 struct btrfs_root *root;
4524 struct btrfs_path *path;
4525 struct btrfs_key key;
4526 struct btrfs_key found_key;
4527 iterate_inode_ref_t cb;
4528 int pending_move = 0;
4530 path = alloc_path_for_send();
4534 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4535 root = sctx->send_root;
4536 cb = __record_new_ref;
4537 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4538 root = sctx->parent_root;
4539 cb = __record_deleted_ref;
4541 btrfs_err(sctx->send_root->fs_info,
4542 "Wrong command %d in process_all_refs", cmd);
4547 key.objectid = sctx->cmp_key->objectid;
4548 key.type = BTRFS_INODE_REF_KEY;
4550 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
4551 if (found_key.objectid != key.objectid ||
4552 (found_key.type != BTRFS_INODE_REF_KEY &&
4553 found_key.type != BTRFS_INODE_EXTREF_KEY))
4556 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4560 /* Catch error found during iteration */
4565 btrfs_release_path(path);
4568 * We don't actually care about pending_move as we are simply
4569 * re-creating this inode and will be rename'ing it into place once we
4570 * rename the parent directory.
4572 ret = process_recorded_refs(sctx, &pending_move);
4574 btrfs_free_path(path);
4578 static int send_set_xattr(struct send_ctx *sctx,
4579 struct fs_path *path,
4580 const char *name, int name_len,
4581 const char *data, int data_len)
4585 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4589 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4590 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4591 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4593 ret = send_cmd(sctx);
4600 static int send_remove_xattr(struct send_ctx *sctx,
4601 struct fs_path *path,
4602 const char *name, int name_len)
4606 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4610 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4611 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4613 ret = send_cmd(sctx);
4620 static int __process_new_xattr(int num, struct btrfs_key *di_key,
4621 const char *name, int name_len, const char *data,
4622 int data_len, void *ctx)
4625 struct send_ctx *sctx = ctx;
4627 struct posix_acl_xattr_header dummy_acl;
4629 /* Capabilities are emitted by finish_inode_if_needed */
4630 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4633 p = fs_path_alloc();
4638 * This hack is needed because empty acls are stored as zero byte
4639 * data in xattrs. Problem with that is, that receiving these zero byte
4640 * acls will fail later. To fix this, we send a dummy acl list that
4641 * only contains the version number and no entries.
4643 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4644 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4645 if (data_len == 0) {
4646 dummy_acl.a_version =
4647 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4648 data = (char *)&dummy_acl;
4649 data_len = sizeof(dummy_acl);
4653 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4657 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4664 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4665 const char *name, int name_len,
4666 const char *data, int data_len, void *ctx)
4669 struct send_ctx *sctx = ctx;
4672 p = fs_path_alloc();
4676 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4680 ret = send_remove_xattr(sctx, p, name, name_len);
4687 static int process_new_xattr(struct send_ctx *sctx)
4691 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4692 __process_new_xattr, sctx);
4697 static int process_deleted_xattr(struct send_ctx *sctx)
4699 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4700 __process_deleted_xattr, sctx);
4703 struct find_xattr_ctx {
4711 static int __find_xattr(int num, struct btrfs_key *di_key, const char *name,
4712 int name_len, const char *data, int data_len, void *vctx)
4714 struct find_xattr_ctx *ctx = vctx;
4716 if (name_len == ctx->name_len &&
4717 strncmp(name, ctx->name, name_len) == 0) {
4718 ctx->found_idx = num;
4719 ctx->found_data_len = data_len;
4720 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4721 if (!ctx->found_data)
4728 static int find_xattr(struct btrfs_root *root,
4729 struct btrfs_path *path,
4730 struct btrfs_key *key,
4731 const char *name, int name_len,
4732 char **data, int *data_len)
4735 struct find_xattr_ctx ctx;
4738 ctx.name_len = name_len;
4740 ctx.found_data = NULL;
4741 ctx.found_data_len = 0;
4743 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
4747 if (ctx.found_idx == -1)
4750 *data = ctx.found_data;
4751 *data_len = ctx.found_data_len;
4753 kfree(ctx.found_data);
4755 return ctx.found_idx;
4759 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4760 const char *name, int name_len,
4761 const char *data, int data_len,
4765 struct send_ctx *sctx = ctx;
4766 char *found_data = NULL;
4767 int found_data_len = 0;
4769 ret = find_xattr(sctx->parent_root, sctx->right_path,
4770 sctx->cmp_key, name, name_len, &found_data,
4772 if (ret == -ENOENT) {
4773 ret = __process_new_xattr(num, di_key, name, name_len, data,
4775 } else if (ret >= 0) {
4776 if (data_len != found_data_len ||
4777 memcmp(data, found_data, data_len)) {
4778 ret = __process_new_xattr(num, di_key, name, name_len,
4779 data, data_len, ctx);
4789 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4790 const char *name, int name_len,
4791 const char *data, int data_len,
4795 struct send_ctx *sctx = ctx;
4797 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4798 name, name_len, NULL, NULL);
4800 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4808 static int process_changed_xattr(struct send_ctx *sctx)
4812 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4813 __process_changed_new_xattr, sctx);
4816 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4817 __process_changed_deleted_xattr, sctx);
4823 static int process_all_new_xattrs(struct send_ctx *sctx)
4827 struct btrfs_root *root;
4828 struct btrfs_path *path;
4829 struct btrfs_key key;
4830 struct btrfs_key found_key;
4832 path = alloc_path_for_send();
4836 root = sctx->send_root;
4838 key.objectid = sctx->cmp_key->objectid;
4839 key.type = BTRFS_XATTR_ITEM_KEY;
4841 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
4842 if (found_key.objectid != key.objectid ||
4843 found_key.type != key.type) {
4848 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
4852 /* Catch error found during iteration */
4856 btrfs_free_path(path);
4860 static inline u64 max_send_read_size(const struct send_ctx *sctx)
4862 return sctx->send_max_size - SZ_16K;
4865 static int put_data_header(struct send_ctx *sctx, u32 len)
4867 struct btrfs_tlv_header *hdr;
4869 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
4871 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
4872 put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
4873 put_unaligned_le16(len, &hdr->tlv_len);
4874 sctx->send_size += sizeof(*hdr);
4878 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
4880 struct btrfs_root *root = sctx->send_root;
4881 struct btrfs_fs_info *fs_info = root->fs_info;
4883 pgoff_t index = offset >> PAGE_SHIFT;
4885 unsigned pg_offset = offset_in_page(offset);
4888 ret = put_data_header(sctx, len);
4892 last_index = (offset + len - 1) >> PAGE_SHIFT;
4894 while (index <= last_index) {
4895 unsigned cur_len = min_t(unsigned, len,
4896 PAGE_SIZE - pg_offset);
4898 page = find_lock_page(sctx->cur_inode->i_mapping, index);
4900 page_cache_sync_readahead(sctx->cur_inode->i_mapping,
4901 &sctx->ra, NULL, index,
4902 last_index + 1 - index);
4904 page = find_or_create_page(sctx->cur_inode->i_mapping,
4912 if (PageReadahead(page))
4913 page_cache_async_readahead(sctx->cur_inode->i_mapping,
4914 &sctx->ra, NULL, page_folio(page),
4915 index, last_index + 1 - index);
4917 if (!PageUptodate(page)) {
4918 btrfs_read_folio(NULL, page_folio(page));
4920 if (!PageUptodate(page)) {
4923 "send: IO error at offset %llu for inode %llu root %llu",
4924 page_offset(page), sctx->cur_ino,
4925 sctx->send_root->root_key.objectid);
4932 memcpy_from_page(sctx->send_buf + sctx->send_size, page,
4933 pg_offset, cur_len);
4939 sctx->send_size += cur_len;
4946 * Read some bytes from the current inode/file and send a write command to
4949 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4951 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
4955 p = fs_path_alloc();
4959 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
4961 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4965 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4969 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4970 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4971 ret = put_file_data(sctx, offset, len);
4975 ret = send_cmd(sctx);
4984 * Send a clone command to user space.
4986 static int send_clone(struct send_ctx *sctx,
4987 u64 offset, u32 len,
4988 struct clone_root *clone_root)
4994 btrfs_debug(sctx->send_root->fs_info,
4995 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4996 offset, len, clone_root->root->root_key.objectid,
4997 clone_root->ino, clone_root->offset);
4999 p = fs_path_alloc();
5003 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5007 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5011 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5012 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5013 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5015 if (clone_root->root == sctx->send_root) {
5016 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
5017 &gen, NULL, NULL, NULL, NULL);
5020 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5022 ret = get_inode_path(clone_root->root, clone_root->ino, p);
5028 * If the parent we're using has a received_uuid set then use that as
5029 * our clone source as that is what we will look for when doing a
5032 * This covers the case that we create a snapshot off of a received
5033 * subvolume and then use that as the parent and try to receive on a
5036 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5037 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5038 clone_root->root->root_item.received_uuid);
5040 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5041 clone_root->root->root_item.uuid);
5042 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
5043 btrfs_root_ctransid(&clone_root->root->root_item));
5044 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5045 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5046 clone_root->offset);
5048 ret = send_cmd(sctx);
5057 * Send an update extent command to user space.
5059 static int send_update_extent(struct send_ctx *sctx,
5060 u64 offset, u32 len)
5065 p = fs_path_alloc();
5069 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5073 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5077 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5078 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5079 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5081 ret = send_cmd(sctx);
5089 static int send_hole(struct send_ctx *sctx, u64 end)
5091 struct fs_path *p = NULL;
5092 u64 read_size = max_send_read_size(sctx);
5093 u64 offset = sctx->cur_inode_last_extent;
5097 * A hole that starts at EOF or beyond it. Since we do not yet support
5098 * fallocate (for extent preallocation and hole punching), sending a
5099 * write of zeroes starting at EOF or beyond would later require issuing
5100 * a truncate operation which would undo the write and achieve nothing.
5102 if (offset >= sctx->cur_inode_size)
5106 * Don't go beyond the inode's i_size due to prealloc extents that start
5109 end = min_t(u64, end, sctx->cur_inode_size);
5111 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5112 return send_update_extent(sctx, offset, end - offset);
5114 p = fs_path_alloc();
5117 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5119 goto tlv_put_failure;
5120 while (offset < end) {
5121 u64 len = min(end - offset, read_size);
5123 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5126 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5127 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5128 ret = put_data_header(sctx, len);
5131 memset(sctx->send_buf + sctx->send_size, 0, len);
5132 sctx->send_size += len;
5133 ret = send_cmd(sctx);
5138 sctx->cur_inode_next_write_offset = offset;
5144 static int send_extent_data(struct send_ctx *sctx,
5148 const u64 end = offset + len;
5149 u64 read_size = max_send_read_size(sctx);
5152 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5153 return send_update_extent(sctx, offset, len);
5155 if (sctx->cur_inode == NULL) {
5156 struct btrfs_root *root = sctx->send_root;
5158 sctx->cur_inode = btrfs_iget(root->fs_info->sb, sctx->cur_ino, root);
5159 if (IS_ERR(sctx->cur_inode)) {
5160 int err = PTR_ERR(sctx->cur_inode);
5162 sctx->cur_inode = NULL;
5165 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
5166 file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping);
5169 * It's very likely there are no pages from this inode in the page
5170 * cache, so after reading extents and sending their data, we clean
5171 * the page cache to avoid trashing the page cache (adding pressure
5172 * to the page cache and forcing eviction of other data more useful
5173 * for applications).
5175 * We decide if we should clean the page cache simply by checking
5176 * if the inode's mapping nrpages is 0 when we first open it, and
5177 * not by using something like filemap_range_has_page() before
5178 * reading an extent because when we ask the readahead code to
5179 * read a given file range, it may (and almost always does) read
5180 * pages from beyond that range (see the documentation for
5181 * page_cache_sync_readahead()), so it would not be reliable,
5182 * because after reading the first extent future calls to
5183 * filemap_range_has_page() would return true because the readahead
5184 * on the previous extent resulted in reading pages of the current
5187 sctx->clean_page_cache = (sctx->cur_inode->i_mapping->nrpages == 0);
5188 sctx->page_cache_clear_start = round_down(offset, PAGE_SIZE);
5191 while (sent < len) {
5192 u64 size = min(len - sent, read_size);
5195 ret = send_write(sctx, offset + sent, size);
5201 if (sctx->clean_page_cache && IS_ALIGNED(end, PAGE_SIZE)) {
5203 * Always operate only on ranges that are a multiple of the page
5204 * size. This is not only to prevent zeroing parts of a page in
5205 * the case of subpage sector size, but also to guarantee we evict
5206 * pages, as passing a range that is smaller than page size does
5207 * not evict the respective page (only zeroes part of its content).
5209 * Always start from the end offset of the last range cleared.
5210 * This is because the readahead code may (and very often does)
5211 * reads pages beyond the range we request for readahead. So if
5212 * we have an extent layout like this:
5214 * [ extent A ] [ extent B ] [ extent C ]
5216 * When we ask page_cache_sync_readahead() to read extent A, it
5217 * may also trigger reads for pages of extent B. If we are doing
5218 * an incremental send and extent B has not changed between the
5219 * parent and send snapshots, some or all of its pages may end
5220 * up being read and placed in the page cache. So when truncating
5221 * the page cache we always start from the end offset of the
5222 * previously processed extent up to the end of the current
5225 truncate_inode_pages_range(&sctx->cur_inode->i_data,
5226 sctx->page_cache_clear_start,
5228 sctx->page_cache_clear_start = end;
5235 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5236 * found, call send_set_xattr function to emit it.
5238 * Return 0 if there isn't a capability, or when the capability was emitted
5239 * successfully, or < 0 if an error occurred.
5241 static int send_capabilities(struct send_ctx *sctx)
5243 struct fs_path *fspath = NULL;
5244 struct btrfs_path *path;
5245 struct btrfs_dir_item *di;
5246 struct extent_buffer *leaf;
5247 unsigned long data_ptr;
5252 path = alloc_path_for_send();
5256 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5257 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5259 /* There is no xattr for this inode */
5261 } else if (IS_ERR(di)) {
5266 leaf = path->nodes[0];
5267 buf_len = btrfs_dir_data_len(leaf, di);
5269 fspath = fs_path_alloc();
5270 buf = kmalloc(buf_len, GFP_KERNEL);
5271 if (!fspath || !buf) {
5276 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5280 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5281 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5283 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5284 strlen(XATTR_NAME_CAPS), buf, buf_len);
5287 fs_path_free(fspath);
5288 btrfs_free_path(path);
5292 static int clone_range(struct send_ctx *sctx,
5293 struct clone_root *clone_root,
5294 const u64 disk_byte,
5299 struct btrfs_path *path;
5300 struct btrfs_key key;
5302 u64 clone_src_i_size = 0;
5305 * Prevent cloning from a zero offset with a length matching the sector
5306 * size because in some scenarios this will make the receiver fail.
5308 * For example, if in the source filesystem the extent at offset 0
5309 * has a length of sectorsize and it was written using direct IO, then
5310 * it can never be an inline extent (even if compression is enabled).
5311 * Then this extent can be cloned in the original filesystem to a non
5312 * zero file offset, but it may not be possible to clone in the
5313 * destination filesystem because it can be inlined due to compression
5314 * on the destination filesystem (as the receiver's write operations are
5315 * always done using buffered IO). The same happens when the original
5316 * filesystem does not have compression enabled but the destination
5319 if (clone_root->offset == 0 &&
5320 len == sctx->send_root->fs_info->sectorsize)
5321 return send_extent_data(sctx, offset, len);
5323 path = alloc_path_for_send();
5328 * There are inodes that have extents that lie behind its i_size. Don't
5329 * accept clones from these extents.
5331 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5332 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5333 btrfs_release_path(path);
5338 * We can't send a clone operation for the entire range if we find
5339 * extent items in the respective range in the source file that
5340 * refer to different extents or if we find holes.
5341 * So check for that and do a mix of clone and regular write/copy
5342 * operations if needed.
5346 * mkfs.btrfs -f /dev/sda
5347 * mount /dev/sda /mnt
5348 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5349 * cp --reflink=always /mnt/foo /mnt/bar
5350 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5351 * btrfs subvolume snapshot -r /mnt /mnt/snap
5353 * If when we send the snapshot and we are processing file bar (which
5354 * has a higher inode number than foo) we blindly send a clone operation
5355 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5356 * a file bar that matches the content of file foo - iow, doesn't match
5357 * the content from bar in the original filesystem.
5359 key.objectid = clone_root->ino;
5360 key.type = BTRFS_EXTENT_DATA_KEY;
5361 key.offset = clone_root->offset;
5362 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5365 if (ret > 0 && path->slots[0] > 0) {
5366 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5367 if (key.objectid == clone_root->ino &&
5368 key.type == BTRFS_EXTENT_DATA_KEY)
5373 struct extent_buffer *leaf = path->nodes[0];
5374 int slot = path->slots[0];
5375 struct btrfs_file_extent_item *ei;
5379 u64 clone_data_offset;
5381 if (slot >= btrfs_header_nritems(leaf)) {
5382 ret = btrfs_next_leaf(clone_root->root, path);
5390 btrfs_item_key_to_cpu(leaf, &key, slot);
5393 * We might have an implicit trailing hole (NO_HOLES feature
5394 * enabled). We deal with it after leaving this loop.
5396 if (key.objectid != clone_root->ino ||
5397 key.type != BTRFS_EXTENT_DATA_KEY)
5400 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5401 type = btrfs_file_extent_type(leaf, ei);
5402 if (type == BTRFS_FILE_EXTENT_INLINE) {
5403 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
5404 ext_len = PAGE_ALIGN(ext_len);
5406 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5409 if (key.offset + ext_len <= clone_root->offset)
5412 if (key.offset > clone_root->offset) {
5413 /* Implicit hole, NO_HOLES feature enabled. */
5414 u64 hole_len = key.offset - clone_root->offset;
5418 ret = send_extent_data(sctx, offset, hole_len);
5426 clone_root->offset += hole_len;
5427 data_offset += hole_len;
5430 if (key.offset >= clone_root->offset + len)
5433 if (key.offset >= clone_src_i_size)
5436 if (key.offset + ext_len > clone_src_i_size)
5437 ext_len = clone_src_i_size - key.offset;
5439 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5440 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5441 clone_root->offset = key.offset;
5442 if (clone_data_offset < data_offset &&
5443 clone_data_offset + ext_len > data_offset) {
5446 extent_offset = data_offset - clone_data_offset;
5447 ext_len -= extent_offset;
5448 clone_data_offset += extent_offset;
5449 clone_root->offset += extent_offset;
5453 clone_len = min_t(u64, ext_len, len);
5455 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5456 clone_data_offset == data_offset) {
5457 const u64 src_end = clone_root->offset + clone_len;
5458 const u64 sectorsize = SZ_64K;
5461 * We can't clone the last block, when its size is not
5462 * sector size aligned, into the middle of a file. If we
5463 * do so, the receiver will get a failure (-EINVAL) when
5464 * trying to clone or will silently corrupt the data in
5465 * the destination file if it's on a kernel without the
5466 * fix introduced by commit ac765f83f1397646
5467 * ("Btrfs: fix data corruption due to cloning of eof
5470 * So issue a clone of the aligned down range plus a
5471 * regular write for the eof block, if we hit that case.
5473 * Also, we use the maximum possible sector size, 64K,
5474 * because we don't know what's the sector size of the
5475 * filesystem that receives the stream, so we have to
5476 * assume the largest possible sector size.
5478 if (src_end == clone_src_i_size &&
5479 !IS_ALIGNED(src_end, sectorsize) &&
5480 offset + clone_len < sctx->cur_inode_size) {
5483 slen = ALIGN_DOWN(src_end - clone_root->offset,
5486 ret = send_clone(sctx, offset, slen,
5491 ret = send_extent_data(sctx, offset + slen,
5494 ret = send_clone(sctx, offset, clone_len,
5498 ret = send_extent_data(sctx, offset, clone_len);
5507 offset += clone_len;
5508 clone_root->offset += clone_len;
5511 * If we are cloning from the file we are currently processing,
5512 * and using the send root as the clone root, we must stop once
5513 * the current clone offset reaches the current eof of the file
5514 * at the receiver, otherwise we would issue an invalid clone
5515 * operation (source range going beyond eof) and cause the
5516 * receiver to fail. So if we reach the current eof, bail out
5517 * and fallback to a regular write.
5519 if (clone_root->root == sctx->send_root &&
5520 clone_root->ino == sctx->cur_ino &&
5521 clone_root->offset >= sctx->cur_inode_next_write_offset)
5524 data_offset += clone_len;
5530 ret = send_extent_data(sctx, offset, len);
5534 btrfs_free_path(path);
5538 static int send_write_or_clone(struct send_ctx *sctx,
5539 struct btrfs_path *path,
5540 struct btrfs_key *key,
5541 struct clone_root *clone_root)
5544 u64 offset = key->offset;
5546 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
5548 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
5552 if (clone_root && IS_ALIGNED(end, bs)) {
5553 struct btrfs_file_extent_item *ei;
5557 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5558 struct btrfs_file_extent_item);
5559 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5560 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5561 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5562 offset, end - offset);
5564 ret = send_extent_data(sctx, offset, end - offset);
5566 sctx->cur_inode_next_write_offset = end;
5570 static int is_extent_unchanged(struct send_ctx *sctx,
5571 struct btrfs_path *left_path,
5572 struct btrfs_key *ekey)
5575 struct btrfs_key key;
5576 struct btrfs_path *path = NULL;
5577 struct extent_buffer *eb;
5579 struct btrfs_key found_key;
5580 struct btrfs_file_extent_item *ei;
5585 u64 left_offset_fixed;
5593 path = alloc_path_for_send();
5597 eb = left_path->nodes[0];
5598 slot = left_path->slots[0];
5599 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5600 left_type = btrfs_file_extent_type(eb, ei);
5602 if (left_type != BTRFS_FILE_EXTENT_REG) {
5606 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5607 left_len = btrfs_file_extent_num_bytes(eb, ei);
5608 left_offset = btrfs_file_extent_offset(eb, ei);
5609 left_gen = btrfs_file_extent_generation(eb, ei);
5612 * Following comments will refer to these graphics. L is the left
5613 * extents which we are checking at the moment. 1-8 are the right
5614 * extents that we iterate.
5617 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5620 * |--1--|-2b-|...(same as above)
5622 * Alternative situation. Happens on files where extents got split.
5624 * |-----------7-----------|-6-|
5626 * Alternative situation. Happens on files which got larger.
5629 * Nothing follows after 8.
5632 key.objectid = ekey->objectid;
5633 key.type = BTRFS_EXTENT_DATA_KEY;
5634 key.offset = ekey->offset;
5635 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5644 * Handle special case where the right side has no extents at all.
5646 eb = path->nodes[0];
5647 slot = path->slots[0];
5648 btrfs_item_key_to_cpu(eb, &found_key, slot);
5649 if (found_key.objectid != key.objectid ||
5650 found_key.type != key.type) {
5651 /* If we're a hole then just pretend nothing changed */
5652 ret = (left_disknr) ? 0 : 1;
5657 * We're now on 2a, 2b or 7.
5660 while (key.offset < ekey->offset + left_len) {
5661 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5662 right_type = btrfs_file_extent_type(eb, ei);
5663 if (right_type != BTRFS_FILE_EXTENT_REG &&
5664 right_type != BTRFS_FILE_EXTENT_INLINE) {
5669 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5670 right_len = btrfs_file_extent_ram_bytes(eb, ei);
5671 right_len = PAGE_ALIGN(right_len);
5673 right_len = btrfs_file_extent_num_bytes(eb, ei);
5677 * Are we at extent 8? If yes, we know the extent is changed.
5678 * This may only happen on the first iteration.
5680 if (found_key.offset + right_len <= ekey->offset) {
5681 /* If we're a hole just pretend nothing changed */
5682 ret = (left_disknr) ? 0 : 1;
5687 * We just wanted to see if when we have an inline extent, what
5688 * follows it is a regular extent (wanted to check the above
5689 * condition for inline extents too). This should normally not
5690 * happen but it's possible for example when we have an inline
5691 * compressed extent representing data with a size matching
5692 * the page size (currently the same as sector size).
5694 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5699 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5700 right_offset = btrfs_file_extent_offset(eb, ei);
5701 right_gen = btrfs_file_extent_generation(eb, ei);
5703 left_offset_fixed = left_offset;
5704 if (key.offset < ekey->offset) {
5705 /* Fix the right offset for 2a and 7. */
5706 right_offset += ekey->offset - key.offset;
5708 /* Fix the left offset for all behind 2a and 2b */
5709 left_offset_fixed += key.offset - ekey->offset;
5713 * Check if we have the same extent.
5715 if (left_disknr != right_disknr ||
5716 left_offset_fixed != right_offset ||
5717 left_gen != right_gen) {
5723 * Go to the next extent.
5725 ret = btrfs_next_item(sctx->parent_root, path);
5729 eb = path->nodes[0];
5730 slot = path->slots[0];
5731 btrfs_item_key_to_cpu(eb, &found_key, slot);
5733 if (ret || found_key.objectid != key.objectid ||
5734 found_key.type != key.type) {
5735 key.offset += right_len;
5738 if (found_key.offset != key.offset + right_len) {
5746 * We're now behind the left extent (treat as unchanged) or at the end
5747 * of the right side (treat as changed).
5749 if (key.offset >= ekey->offset + left_len)
5756 btrfs_free_path(path);
5760 static int get_last_extent(struct send_ctx *sctx, u64 offset)
5762 struct btrfs_path *path;
5763 struct btrfs_root *root = sctx->send_root;
5764 struct btrfs_key key;
5767 path = alloc_path_for_send();
5771 sctx->cur_inode_last_extent = 0;
5773 key.objectid = sctx->cur_ino;
5774 key.type = BTRFS_EXTENT_DATA_KEY;
5775 key.offset = offset;
5776 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5780 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5781 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5784 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
5786 btrfs_free_path(path);
5790 static int range_is_hole_in_parent(struct send_ctx *sctx,
5794 struct btrfs_path *path;
5795 struct btrfs_key key;
5796 struct btrfs_root *root = sctx->parent_root;
5797 u64 search_start = start;
5800 path = alloc_path_for_send();
5804 key.objectid = sctx->cur_ino;
5805 key.type = BTRFS_EXTENT_DATA_KEY;
5806 key.offset = search_start;
5807 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5810 if (ret > 0 && path->slots[0] > 0)
5813 while (search_start < end) {
5814 struct extent_buffer *leaf = path->nodes[0];
5815 int slot = path->slots[0];
5816 struct btrfs_file_extent_item *fi;
5819 if (slot >= btrfs_header_nritems(leaf)) {
5820 ret = btrfs_next_leaf(root, path);
5828 btrfs_item_key_to_cpu(leaf, &key, slot);
5829 if (key.objectid < sctx->cur_ino ||
5830 key.type < BTRFS_EXTENT_DATA_KEY)
5832 if (key.objectid > sctx->cur_ino ||
5833 key.type > BTRFS_EXTENT_DATA_KEY ||
5837 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5838 extent_end = btrfs_file_extent_end(path);
5839 if (extent_end <= start)
5841 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5842 search_start = extent_end;
5852 btrfs_free_path(path);
5856 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5857 struct btrfs_key *key)
5861 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5864 if (sctx->cur_inode_last_extent == (u64)-1) {
5865 ret = get_last_extent(sctx, key->offset - 1);
5870 if (path->slots[0] == 0 &&
5871 sctx->cur_inode_last_extent < key->offset) {
5873 * We might have skipped entire leafs that contained only
5874 * file extent items for our current inode. These leafs have
5875 * a generation number smaller (older) than the one in the
5876 * current leaf and the leaf our last extent came from, and
5877 * are located between these 2 leafs.
5879 ret = get_last_extent(sctx, key->offset - 1);
5884 if (sctx->cur_inode_last_extent < key->offset) {
5885 ret = range_is_hole_in_parent(sctx,
5886 sctx->cur_inode_last_extent,
5891 ret = send_hole(sctx, key->offset);
5895 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
5899 static int process_extent(struct send_ctx *sctx,
5900 struct btrfs_path *path,
5901 struct btrfs_key *key)
5903 struct clone_root *found_clone = NULL;
5906 if (S_ISLNK(sctx->cur_inode_mode))
5909 if (sctx->parent_root && !sctx->cur_inode_new) {
5910 ret = is_extent_unchanged(sctx, path, key);
5918 struct btrfs_file_extent_item *ei;
5921 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5922 struct btrfs_file_extent_item);
5923 type = btrfs_file_extent_type(path->nodes[0], ei);
5924 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5925 type == BTRFS_FILE_EXTENT_REG) {
5927 * The send spec does not have a prealloc command yet,
5928 * so just leave a hole for prealloc'ed extents until
5929 * we have enough commands queued up to justify rev'ing
5932 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5937 /* Have a hole, just skip it. */
5938 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5945 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5946 sctx->cur_inode_size, &found_clone);
5947 if (ret != -ENOENT && ret < 0)
5950 ret = send_write_or_clone(sctx, path, key, found_clone);
5954 ret = maybe_send_hole(sctx, path, key);
5959 static int process_all_extents(struct send_ctx *sctx)
5963 struct btrfs_root *root;
5964 struct btrfs_path *path;
5965 struct btrfs_key key;
5966 struct btrfs_key found_key;
5968 root = sctx->send_root;
5969 path = alloc_path_for_send();
5973 key.objectid = sctx->cmp_key->objectid;
5974 key.type = BTRFS_EXTENT_DATA_KEY;
5976 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
5977 if (found_key.objectid != key.objectid ||
5978 found_key.type != key.type) {
5983 ret = process_extent(sctx, path, &found_key);
5987 /* Catch error found during iteration */
5991 btrfs_free_path(path);
5995 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5997 int *refs_processed)
6001 if (sctx->cur_ino == 0)
6003 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
6004 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
6006 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
6009 ret = process_recorded_refs(sctx, pending_move);
6013 *refs_processed = 1;
6018 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
6029 int need_truncate = 1;
6030 int pending_move = 0;
6031 int refs_processed = 0;
6033 if (sctx->ignore_cur_inode)
6036 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6042 * We have processed the refs and thus need to advance send_progress.
6043 * Now, calls to get_cur_xxx will take the updated refs of the current
6044 * inode into account.
6046 * On the other hand, if our current inode is a directory and couldn't
6047 * be moved/renamed because its parent was renamed/moved too and it has
6048 * a higher inode number, we can only move/rename our current inode
6049 * after we moved/renamed its parent. Therefore in this case operate on
6050 * the old path (pre move/rename) of our current inode, and the
6051 * move/rename will be performed later.
6053 if (refs_processed && !pending_move)
6054 sctx->send_progress = sctx->cur_ino + 1;
6056 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6058 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6061 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
6062 &left_mode, &left_uid, &left_gid, NULL);
6066 if (!sctx->parent_root || sctx->cur_inode_new) {
6068 if (!S_ISLNK(sctx->cur_inode_mode))
6070 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6075 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
6076 &old_size, NULL, &right_mode, &right_uid,
6081 if (left_uid != right_uid || left_gid != right_gid)
6083 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6085 if ((old_size == sctx->cur_inode_size) ||
6086 (sctx->cur_inode_size > old_size &&
6087 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6091 if (S_ISREG(sctx->cur_inode_mode)) {
6092 if (need_send_hole(sctx)) {
6093 if (sctx->cur_inode_last_extent == (u64)-1 ||
6094 sctx->cur_inode_last_extent <
6095 sctx->cur_inode_size) {
6096 ret = get_last_extent(sctx, (u64)-1);
6100 if (sctx->cur_inode_last_extent <
6101 sctx->cur_inode_size) {
6102 ret = send_hole(sctx, sctx->cur_inode_size);
6107 if (need_truncate) {
6108 ret = send_truncate(sctx, sctx->cur_ino,
6109 sctx->cur_inode_gen,
6110 sctx->cur_inode_size);
6117 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6118 left_uid, left_gid);
6123 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6129 ret = send_capabilities(sctx);
6134 * If other directory inodes depended on our current directory
6135 * inode's move/rename, now do their move/rename operations.
6137 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6138 ret = apply_children_dir_moves(sctx);
6142 * Need to send that every time, no matter if it actually
6143 * changed between the two trees as we have done changes to
6144 * the inode before. If our inode is a directory and it's
6145 * waiting to be moved/renamed, we will send its utimes when
6146 * it's moved/renamed, therefore we don't need to do it here.
6148 sctx->send_progress = sctx->cur_ino + 1;
6149 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6158 struct parent_paths_ctx {
6159 struct list_head *refs;
6160 struct send_ctx *sctx;
6163 static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6166 struct parent_paths_ctx *ppctx = ctx;
6168 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6173 * Issue unlink operations for all paths of the current inode found in the
6176 static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6178 LIST_HEAD(deleted_refs);
6179 struct btrfs_path *path;
6180 struct btrfs_root *root = sctx->parent_root;
6181 struct btrfs_key key;
6182 struct btrfs_key found_key;
6183 struct parent_paths_ctx ctx;
6187 path = alloc_path_for_send();
6191 key.objectid = sctx->cur_ino;
6192 key.type = BTRFS_INODE_REF_KEY;
6195 ctx.refs = &deleted_refs;
6198 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
6199 if (found_key.objectid != key.objectid)
6201 if (found_key.type != key.type &&
6202 found_key.type != BTRFS_INODE_EXTREF_KEY)
6205 ret = iterate_inode_ref(root, path, &found_key, 1,
6206 record_parent_ref, &ctx);
6210 /* Catch error found during iteration */
6216 while (!list_empty(&deleted_refs)) {
6217 struct recorded_ref *ref;
6219 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6220 ret = send_unlink(sctx, ref->full_path);
6223 fs_path_free(ref->full_path);
6224 list_del(&ref->list);
6229 btrfs_free_path(path);
6231 __free_recorded_refs(&deleted_refs);
6235 static void close_current_inode(struct send_ctx *sctx)
6239 if (sctx->cur_inode == NULL)
6242 i_size = i_size_read(sctx->cur_inode);
6245 * If we are doing an incremental send, we may have extents between the
6246 * last processed extent and the i_size that have not been processed
6247 * because they haven't changed but we may have read some of their pages
6248 * through readahead, see the comments at send_extent_data().
6250 if (sctx->clean_page_cache && sctx->page_cache_clear_start < i_size)
6251 truncate_inode_pages_range(&sctx->cur_inode->i_data,
6252 sctx->page_cache_clear_start,
6253 round_up(i_size, PAGE_SIZE) - 1);
6255 iput(sctx->cur_inode);
6256 sctx->cur_inode = NULL;
6259 static int changed_inode(struct send_ctx *sctx,
6260 enum btrfs_compare_tree_result result)
6263 struct btrfs_key *key = sctx->cmp_key;
6264 struct btrfs_inode_item *left_ii = NULL;
6265 struct btrfs_inode_item *right_ii = NULL;
6269 close_current_inode(sctx);
6271 sctx->cur_ino = key->objectid;
6272 sctx->cur_inode_new_gen = 0;
6273 sctx->cur_inode_last_extent = (u64)-1;
6274 sctx->cur_inode_next_write_offset = 0;
6275 sctx->ignore_cur_inode = false;
6278 * Set send_progress to current inode. This will tell all get_cur_xxx
6279 * functions that the current inode's refs are not updated yet. Later,
6280 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6282 sctx->send_progress = sctx->cur_ino;
6284 if (result == BTRFS_COMPARE_TREE_NEW ||
6285 result == BTRFS_COMPARE_TREE_CHANGED) {
6286 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6287 sctx->left_path->slots[0],
6288 struct btrfs_inode_item);
6289 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6292 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6293 sctx->right_path->slots[0],
6294 struct btrfs_inode_item);
6295 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6298 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6299 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6300 sctx->right_path->slots[0],
6301 struct btrfs_inode_item);
6303 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6307 * The cur_ino = root dir case is special here. We can't treat
6308 * the inode as deleted+reused because it would generate a
6309 * stream that tries to delete/mkdir the root dir.
6311 if (left_gen != right_gen &&
6312 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6313 sctx->cur_inode_new_gen = 1;
6317 * Normally we do not find inodes with a link count of zero (orphans)
6318 * because the most common case is to create a snapshot and use it
6319 * for a send operation. However other less common use cases involve
6320 * using a subvolume and send it after turning it to RO mode just
6321 * after deleting all hard links of a file while holding an open
6322 * file descriptor against it or turning a RO snapshot into RW mode,
6323 * keep an open file descriptor against a file, delete it and then
6324 * turn the snapshot back to RO mode before using it for a send
6325 * operation. So if we find such cases, ignore the inode and all its
6326 * items completely if it's a new inode, or if it's a changed inode
6327 * make sure all its previous paths (from the parent snapshot) are all
6328 * unlinked and all other the inode items are ignored.
6330 if (result == BTRFS_COMPARE_TREE_NEW ||
6331 result == BTRFS_COMPARE_TREE_CHANGED) {
6334 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6336 sctx->ignore_cur_inode = true;
6337 if (result == BTRFS_COMPARE_TREE_CHANGED)
6338 ret = btrfs_unlink_all_paths(sctx);
6343 if (result == BTRFS_COMPARE_TREE_NEW) {
6344 sctx->cur_inode_gen = left_gen;
6345 sctx->cur_inode_new = 1;
6346 sctx->cur_inode_deleted = 0;
6347 sctx->cur_inode_size = btrfs_inode_size(
6348 sctx->left_path->nodes[0], left_ii);
6349 sctx->cur_inode_mode = btrfs_inode_mode(
6350 sctx->left_path->nodes[0], left_ii);
6351 sctx->cur_inode_rdev = btrfs_inode_rdev(
6352 sctx->left_path->nodes[0], left_ii);
6353 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6354 ret = send_create_inode_if_needed(sctx);
6355 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6356 sctx->cur_inode_gen = right_gen;
6357 sctx->cur_inode_new = 0;
6358 sctx->cur_inode_deleted = 1;
6359 sctx->cur_inode_size = btrfs_inode_size(
6360 sctx->right_path->nodes[0], right_ii);
6361 sctx->cur_inode_mode = btrfs_inode_mode(
6362 sctx->right_path->nodes[0], right_ii);
6363 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
6365 * We need to do some special handling in case the inode was
6366 * reported as changed with a changed generation number. This
6367 * means that the original inode was deleted and new inode
6368 * reused the same inum. So we have to treat the old inode as
6369 * deleted and the new one as new.
6371 if (sctx->cur_inode_new_gen) {
6373 * First, process the inode as if it was deleted.
6375 sctx->cur_inode_gen = right_gen;
6376 sctx->cur_inode_new = 0;
6377 sctx->cur_inode_deleted = 1;
6378 sctx->cur_inode_size = btrfs_inode_size(
6379 sctx->right_path->nodes[0], right_ii);
6380 sctx->cur_inode_mode = btrfs_inode_mode(
6381 sctx->right_path->nodes[0], right_ii);
6382 ret = process_all_refs(sctx,
6383 BTRFS_COMPARE_TREE_DELETED);
6388 * Now process the inode as if it was new.
6390 sctx->cur_inode_gen = left_gen;
6391 sctx->cur_inode_new = 1;
6392 sctx->cur_inode_deleted = 0;
6393 sctx->cur_inode_size = btrfs_inode_size(
6394 sctx->left_path->nodes[0], left_ii);
6395 sctx->cur_inode_mode = btrfs_inode_mode(
6396 sctx->left_path->nodes[0], left_ii);
6397 sctx->cur_inode_rdev = btrfs_inode_rdev(
6398 sctx->left_path->nodes[0], left_ii);
6399 ret = send_create_inode_if_needed(sctx);
6403 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6407 * Advance send_progress now as we did not get into
6408 * process_recorded_refs_if_needed in the new_gen case.
6410 sctx->send_progress = sctx->cur_ino + 1;
6413 * Now process all extents and xattrs of the inode as if
6414 * they were all new.
6416 ret = process_all_extents(sctx);
6419 ret = process_all_new_xattrs(sctx);
6423 sctx->cur_inode_gen = left_gen;
6424 sctx->cur_inode_new = 0;
6425 sctx->cur_inode_new_gen = 0;
6426 sctx->cur_inode_deleted = 0;
6427 sctx->cur_inode_size = btrfs_inode_size(
6428 sctx->left_path->nodes[0], left_ii);
6429 sctx->cur_inode_mode = btrfs_inode_mode(
6430 sctx->left_path->nodes[0], left_ii);
6439 * We have to process new refs before deleted refs, but compare_trees gives us
6440 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6441 * first and later process them in process_recorded_refs.
6442 * For the cur_inode_new_gen case, we skip recording completely because
6443 * changed_inode did already initiate processing of refs. The reason for this is
6444 * that in this case, compare_tree actually compares the refs of 2 different
6445 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6446 * refs of the right tree as deleted and all refs of the left tree as new.
6448 static int changed_ref(struct send_ctx *sctx,
6449 enum btrfs_compare_tree_result result)
6453 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6454 inconsistent_snapshot_error(sctx, result, "reference");
6458 if (!sctx->cur_inode_new_gen &&
6459 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6460 if (result == BTRFS_COMPARE_TREE_NEW)
6461 ret = record_new_ref(sctx);
6462 else if (result == BTRFS_COMPARE_TREE_DELETED)
6463 ret = record_deleted_ref(sctx);
6464 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6465 ret = record_changed_ref(sctx);
6472 * Process new/deleted/changed xattrs. We skip processing in the
6473 * cur_inode_new_gen case because changed_inode did already initiate processing
6474 * of xattrs. The reason is the same as in changed_ref
6476 static int changed_xattr(struct send_ctx *sctx,
6477 enum btrfs_compare_tree_result result)
6481 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6482 inconsistent_snapshot_error(sctx, result, "xattr");
6486 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6487 if (result == BTRFS_COMPARE_TREE_NEW)
6488 ret = process_new_xattr(sctx);
6489 else if (result == BTRFS_COMPARE_TREE_DELETED)
6490 ret = process_deleted_xattr(sctx);
6491 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6492 ret = process_changed_xattr(sctx);
6499 * Process new/deleted/changed extents. We skip processing in the
6500 * cur_inode_new_gen case because changed_inode did already initiate processing
6501 * of extents. The reason is the same as in changed_ref
6503 static int changed_extent(struct send_ctx *sctx,
6504 enum btrfs_compare_tree_result result)
6509 * We have found an extent item that changed without the inode item
6510 * having changed. This can happen either after relocation (where the
6511 * disk_bytenr of an extent item is replaced at
6512 * relocation.c:replace_file_extents()) or after deduplication into a
6513 * file in both the parent and send snapshots (where an extent item can
6514 * get modified or replaced with a new one). Note that deduplication
6515 * updates the inode item, but it only changes the iversion (sequence
6516 * field in the inode item) of the inode, so if a file is deduplicated
6517 * the same amount of times in both the parent and send snapshots, its
6518 * iversion becomes the same in both snapshots, whence the inode item is
6519 * the same on both snapshots.
6521 if (sctx->cur_ino != sctx->cmp_key->objectid)
6524 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6525 if (result != BTRFS_COMPARE_TREE_DELETED)
6526 ret = process_extent(sctx, sctx->left_path,
6533 static int dir_changed(struct send_ctx *sctx, u64 dir)
6535 u64 orig_gen, new_gen;
6538 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6543 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6548 return (orig_gen != new_gen) ? 1 : 0;
6551 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6552 struct btrfs_key *key)
6554 struct btrfs_inode_extref *extref;
6555 struct extent_buffer *leaf;
6556 u64 dirid = 0, last_dirid = 0;
6563 /* Easy case, just check this one dirid */
6564 if (key->type == BTRFS_INODE_REF_KEY) {
6565 dirid = key->offset;
6567 ret = dir_changed(sctx, dirid);
6571 leaf = path->nodes[0];
6572 item_size = btrfs_item_size(leaf, path->slots[0]);
6573 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6574 while (cur_offset < item_size) {
6575 extref = (struct btrfs_inode_extref *)(ptr +
6577 dirid = btrfs_inode_extref_parent(leaf, extref);
6578 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6579 cur_offset += ref_name_len + sizeof(*extref);
6580 if (dirid == last_dirid)
6582 ret = dir_changed(sctx, dirid);
6592 * Updates compare related fields in sctx and simply forwards to the actual
6593 * changed_xxx functions.
6595 static int changed_cb(struct btrfs_path *left_path,
6596 struct btrfs_path *right_path,
6597 struct btrfs_key *key,
6598 enum btrfs_compare_tree_result result,
6599 struct send_ctx *sctx)
6604 * We can not hold the commit root semaphore here. This is because in
6605 * the case of sending and receiving to the same filesystem, using a
6606 * pipe, could result in a deadlock:
6608 * 1) The task running send blocks on the pipe because it's full;
6610 * 2) The task running receive, which is the only consumer of the pipe,
6611 * is waiting for a transaction commit (for example due to a space
6612 * reservation when doing a write or triggering a transaction commit
6613 * when creating a subvolume);
6615 * 3) The transaction is waiting to write lock the commit root semaphore,
6616 * but can not acquire it since it's being held at 1).
6618 * Down this call chain we write to the pipe through kernel_write().
6619 * The same type of problem can also happen when sending to a file that
6620 * is stored in the same filesystem - when reserving space for a write
6621 * into the file, we can trigger a transaction commit.
6623 * Our caller has supplied us with clones of leaves from the send and
6624 * parent roots, so we're safe here from a concurrent relocation and
6625 * further reallocation of metadata extents while we are here. Below we
6626 * also assert that the leaves are clones.
6628 lockdep_assert_not_held(&sctx->send_root->fs_info->commit_root_sem);
6631 * We always have a send root, so left_path is never NULL. We will not
6632 * have a leaf when we have reached the end of the send root but have
6633 * not yet reached the end of the parent root.
6635 if (left_path->nodes[0])
6636 ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
6637 &left_path->nodes[0]->bflags));
6639 * When doing a full send we don't have a parent root, so right_path is
6640 * NULL. When doing an incremental send, we may have reached the end of
6641 * the parent root already, so we don't have a leaf at right_path.
6643 if (right_path && right_path->nodes[0])
6644 ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
6645 &right_path->nodes[0]->bflags));
6647 if (result == BTRFS_COMPARE_TREE_SAME) {
6648 if (key->type == BTRFS_INODE_REF_KEY ||
6649 key->type == BTRFS_INODE_EXTREF_KEY) {
6650 ret = compare_refs(sctx, left_path, key);
6655 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6656 return maybe_send_hole(sctx, left_path, key);
6660 result = BTRFS_COMPARE_TREE_CHANGED;
6664 sctx->left_path = left_path;
6665 sctx->right_path = right_path;
6666 sctx->cmp_key = key;
6668 ret = finish_inode_if_needed(sctx, 0);
6672 /* Ignore non-FS objects */
6673 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6674 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6677 if (key->type == BTRFS_INODE_ITEM_KEY) {
6678 ret = changed_inode(sctx, result);
6679 } else if (!sctx->ignore_cur_inode) {
6680 if (key->type == BTRFS_INODE_REF_KEY ||
6681 key->type == BTRFS_INODE_EXTREF_KEY)
6682 ret = changed_ref(sctx, result);
6683 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6684 ret = changed_xattr(sctx, result);
6685 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6686 ret = changed_extent(sctx, result);
6693 static int search_key_again(const struct send_ctx *sctx,
6694 struct btrfs_root *root,
6695 struct btrfs_path *path,
6696 const struct btrfs_key *key)
6700 if (!path->need_commit_sem)
6701 lockdep_assert_held_read(&root->fs_info->commit_root_sem);
6704 * Roots used for send operations are readonly and no one can add,
6705 * update or remove keys from them, so we should be able to find our
6706 * key again. The only exception is deduplication, which can operate on
6707 * readonly roots and add, update or remove keys to/from them - but at
6708 * the moment we don't allow it to run in parallel with send.
6710 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6713 btrfs_print_tree(path->nodes[path->lowest_level], false);
6714 btrfs_err(root->fs_info,
6715 "send: key (%llu %u %llu) not found in %s root %llu, lowest_level %d, slot %d",
6716 key->objectid, key->type, key->offset,
6717 (root == sctx->parent_root ? "parent" : "send"),
6718 root->root_key.objectid, path->lowest_level,
6719 path->slots[path->lowest_level]);
6726 static int full_send_tree(struct send_ctx *sctx)
6729 struct btrfs_root *send_root = sctx->send_root;
6730 struct btrfs_key key;
6731 struct btrfs_fs_info *fs_info = send_root->fs_info;
6732 struct btrfs_path *path;
6734 path = alloc_path_for_send();
6737 path->reada = READA_FORWARD_ALWAYS;
6739 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6740 key.type = BTRFS_INODE_ITEM_KEY;
6743 down_read(&fs_info->commit_root_sem);
6744 sctx->last_reloc_trans = fs_info->last_reloc_trans;
6745 up_read(&fs_info->commit_root_sem);
6747 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6754 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
6756 ret = changed_cb(path, NULL, &key,
6757 BTRFS_COMPARE_TREE_NEW, sctx);
6761 down_read(&fs_info->commit_root_sem);
6762 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
6763 sctx->last_reloc_trans = fs_info->last_reloc_trans;
6764 up_read(&fs_info->commit_root_sem);
6766 * A transaction used for relocating a block group was
6767 * committed or is about to finish its commit. Release
6768 * our path (leaf) and restart the search, so that we
6769 * avoid operating on any file extent items that are
6770 * stale, with a disk_bytenr that reflects a pre
6771 * relocation value. This way we avoid as much as
6772 * possible to fallback to regular writes when checking
6773 * if we can clone file ranges.
6775 btrfs_release_path(path);
6776 ret = search_key_again(sctx, send_root, path, &key);
6780 up_read(&fs_info->commit_root_sem);
6783 ret = btrfs_next_item(send_root, path);
6793 ret = finish_inode_if_needed(sctx, 1);
6796 btrfs_free_path(path);
6800 static int replace_node_with_clone(struct btrfs_path *path, int level)
6802 struct extent_buffer *clone;
6804 clone = btrfs_clone_extent_buffer(path->nodes[level]);
6808 free_extent_buffer(path->nodes[level]);
6809 path->nodes[level] = clone;
6814 static int tree_move_down(struct btrfs_path *path, int *level, u64 reada_min_gen)
6816 struct extent_buffer *eb;
6817 struct extent_buffer *parent = path->nodes[*level];
6818 int slot = path->slots[*level];
6819 const int nritems = btrfs_header_nritems(parent);
6823 lockdep_assert_held_read(&parent->fs_info->commit_root_sem);
6825 BUG_ON(*level == 0);
6826 eb = btrfs_read_node_slot(parent, slot);
6831 * Trigger readahead for the next leaves we will process, so that it is
6832 * very likely that when we need them they are already in memory and we
6833 * will not block on disk IO. For nodes we only do readahead for one,
6834 * since the time window between processing nodes is typically larger.
6836 reada_max = (*level == 1 ? SZ_128K : eb->fs_info->nodesize);
6838 for (slot++; slot < nritems && reada_done < reada_max; slot++) {
6839 if (btrfs_node_ptr_generation(parent, slot) > reada_min_gen) {
6840 btrfs_readahead_node_child(parent, slot);
6841 reada_done += eb->fs_info->nodesize;
6845 path->nodes[*level - 1] = eb;
6846 path->slots[*level - 1] = 0;
6850 return replace_node_with_clone(path, 0);
6855 static int tree_move_next_or_upnext(struct btrfs_path *path,
6856 int *level, int root_level)
6860 nritems = btrfs_header_nritems(path->nodes[*level]);
6862 path->slots[*level]++;
6864 while (path->slots[*level] >= nritems) {
6865 if (*level == root_level) {
6866 path->slots[*level] = nritems - 1;
6871 path->slots[*level] = 0;
6872 free_extent_buffer(path->nodes[*level]);
6873 path->nodes[*level] = NULL;
6875 path->slots[*level]++;
6877 nritems = btrfs_header_nritems(path->nodes[*level]);
6884 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
6887 static int tree_advance(struct btrfs_path *path,
6888 int *level, int root_level,
6890 struct btrfs_key *key,
6895 if (*level == 0 || !allow_down) {
6896 ret = tree_move_next_or_upnext(path, level, root_level);
6898 ret = tree_move_down(path, level, reada_min_gen);
6902 * Even if we have reached the end of a tree, ret is -1, update the key
6903 * anyway, so that in case we need to restart due to a block group
6904 * relocation, we can assert that the last key of the root node still
6905 * exists in the tree.
6908 btrfs_item_key_to_cpu(path->nodes[*level], key,
6909 path->slots[*level]);
6911 btrfs_node_key_to_cpu(path->nodes[*level], key,
6912 path->slots[*level]);
6917 static int tree_compare_item(struct btrfs_path *left_path,
6918 struct btrfs_path *right_path,
6923 unsigned long off1, off2;
6925 len1 = btrfs_item_size(left_path->nodes[0], left_path->slots[0]);
6926 len2 = btrfs_item_size(right_path->nodes[0], right_path->slots[0]);
6930 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
6931 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
6932 right_path->slots[0]);
6934 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
6936 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
6943 * A transaction used for relocating a block group was committed or is about to
6944 * finish its commit. Release our paths and restart the search, so that we are
6945 * not using stale extent buffers:
6947 * 1) For levels > 0, we are only holding references of extent buffers, without
6948 * any locks on them, which does not prevent them from having been relocated
6949 * and reallocated after the last time we released the commit root semaphore.
6950 * The exception are the root nodes, for which we always have a clone, see
6951 * the comment at btrfs_compare_trees();
6953 * 2) For leaves, level 0, we are holding copies (clones) of extent buffers, so
6954 * we are safe from the concurrent relocation and reallocation. However they
6955 * can have file extent items with a pre relocation disk_bytenr value, so we
6956 * restart the start from the current commit roots and clone the new leaves so
6957 * that we get the post relocation disk_bytenr values. Not doing so, could
6958 * make us clone the wrong data in case there are new extents using the old
6959 * disk_bytenr that happen to be shared.
6961 static int restart_after_relocation(struct btrfs_path *left_path,
6962 struct btrfs_path *right_path,
6963 const struct btrfs_key *left_key,
6964 const struct btrfs_key *right_key,
6967 const struct send_ctx *sctx)
6972 lockdep_assert_held_read(&sctx->send_root->fs_info->commit_root_sem);
6974 btrfs_release_path(left_path);
6975 btrfs_release_path(right_path);
6978 * Since keys can not be added or removed to/from our roots because they
6979 * are readonly and we do not allow deduplication to run in parallel
6980 * (which can add, remove or change keys), the layout of the trees should
6983 left_path->lowest_level = left_level;
6984 ret = search_key_again(sctx, sctx->send_root, left_path, left_key);
6988 right_path->lowest_level = right_level;
6989 ret = search_key_again(sctx, sctx->parent_root, right_path, right_key);
6994 * If the lowest level nodes are leaves, clone them so that they can be
6995 * safely used by changed_cb() while not under the protection of the
6996 * commit root semaphore, even if relocation and reallocation happens in
6999 if (left_level == 0) {
7000 ret = replace_node_with_clone(left_path, 0);
7005 if (right_level == 0) {
7006 ret = replace_node_with_clone(right_path, 0);
7012 * Now clone the root nodes (unless they happen to be the leaves we have
7013 * already cloned). This is to protect against concurrent snapshotting of
7014 * the send and parent roots (see the comment at btrfs_compare_trees()).
7016 root_level = btrfs_header_level(sctx->send_root->commit_root);
7017 if (root_level > 0) {
7018 ret = replace_node_with_clone(left_path, root_level);
7023 root_level = btrfs_header_level(sctx->parent_root->commit_root);
7024 if (root_level > 0) {
7025 ret = replace_node_with_clone(right_path, root_level);
7034 * This function compares two trees and calls the provided callback for
7035 * every changed/new/deleted item it finds.
7036 * If shared tree blocks are encountered, whole subtrees are skipped, making
7037 * the compare pretty fast on snapshotted subvolumes.
7039 * This currently works on commit roots only. As commit roots are read only,
7040 * we don't do any locking. The commit roots are protected with transactions.
7041 * Transactions are ended and rejoined when a commit is tried in between.
7043 * This function checks for modifications done to the trees while comparing.
7044 * If it detects a change, it aborts immediately.
7046 static int btrfs_compare_trees(struct btrfs_root *left_root,
7047 struct btrfs_root *right_root, struct send_ctx *sctx)
7049 struct btrfs_fs_info *fs_info = left_root->fs_info;
7052 struct btrfs_path *left_path = NULL;
7053 struct btrfs_path *right_path = NULL;
7054 struct btrfs_key left_key;
7055 struct btrfs_key right_key;
7056 char *tmp_buf = NULL;
7057 int left_root_level;
7058 int right_root_level;
7061 int left_end_reached = 0;
7062 int right_end_reached = 0;
7063 int advance_left = 0;
7064 int advance_right = 0;
7071 left_path = btrfs_alloc_path();
7076 right_path = btrfs_alloc_path();
7082 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
7088 left_path->search_commit_root = 1;
7089 left_path->skip_locking = 1;
7090 right_path->search_commit_root = 1;
7091 right_path->skip_locking = 1;
7094 * Strategy: Go to the first items of both trees. Then do
7096 * If both trees are at level 0
7097 * Compare keys of current items
7098 * If left < right treat left item as new, advance left tree
7100 * If left > right treat right item as deleted, advance right tree
7102 * If left == right do deep compare of items, treat as changed if
7103 * needed, advance both trees and repeat
7104 * If both trees are at the same level but not at level 0
7105 * Compare keys of current nodes/leafs
7106 * If left < right advance left tree and repeat
7107 * If left > right advance right tree and repeat
7108 * If left == right compare blockptrs of the next nodes/leafs
7109 * If they match advance both trees but stay at the same level
7111 * If they don't match advance both trees while allowing to go
7113 * If tree levels are different
7114 * Advance the tree that needs it and repeat
7116 * Advancing a tree means:
7117 * If we are at level 0, try to go to the next slot. If that's not
7118 * possible, go one level up and repeat. Stop when we found a level
7119 * where we could go to the next slot. We may at this point be on a
7122 * If we are not at level 0 and not on shared tree blocks, go one
7125 * If we are not at level 0 and on shared tree blocks, go one slot to
7126 * the right if possible or go up and right.
7129 down_read(&fs_info->commit_root_sem);
7130 left_level = btrfs_header_level(left_root->commit_root);
7131 left_root_level = left_level;
7133 * We clone the root node of the send and parent roots to prevent races
7134 * with snapshot creation of these roots. Snapshot creation COWs the
7135 * root node of a tree, so after the transaction is committed the old
7136 * extent can be reallocated while this send operation is still ongoing.
7137 * So we clone them, under the commit root semaphore, to be race free.
7139 left_path->nodes[left_level] =
7140 btrfs_clone_extent_buffer(left_root->commit_root);
7141 if (!left_path->nodes[left_level]) {
7146 right_level = btrfs_header_level(right_root->commit_root);
7147 right_root_level = right_level;
7148 right_path->nodes[right_level] =
7149 btrfs_clone_extent_buffer(right_root->commit_root);
7150 if (!right_path->nodes[right_level]) {
7155 * Our right root is the parent root, while the left root is the "send"
7156 * root. We know that all new nodes/leaves in the left root must have
7157 * a generation greater than the right root's generation, so we trigger
7158 * readahead for those nodes and leaves of the left root, as we know we
7159 * will need to read them at some point.
7161 reada_min_gen = btrfs_header_generation(right_root->commit_root);
7163 if (left_level == 0)
7164 btrfs_item_key_to_cpu(left_path->nodes[left_level],
7165 &left_key, left_path->slots[left_level]);
7167 btrfs_node_key_to_cpu(left_path->nodes[left_level],
7168 &left_key, left_path->slots[left_level]);
7169 if (right_level == 0)
7170 btrfs_item_key_to_cpu(right_path->nodes[right_level],
7171 &right_key, right_path->slots[right_level]);
7173 btrfs_node_key_to_cpu(right_path->nodes[right_level],
7174 &right_key, right_path->slots[right_level]);
7176 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7179 if (need_resched() ||
7180 rwsem_is_contended(&fs_info->commit_root_sem)) {
7181 up_read(&fs_info->commit_root_sem);
7183 down_read(&fs_info->commit_root_sem);
7186 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
7187 ret = restart_after_relocation(left_path, right_path,
7188 &left_key, &right_key,
7189 left_level, right_level,
7193 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7196 if (advance_left && !left_end_reached) {
7197 ret = tree_advance(left_path, &left_level,
7199 advance_left != ADVANCE_ONLY_NEXT,
7200 &left_key, reada_min_gen);
7202 left_end_reached = ADVANCE;
7207 if (advance_right && !right_end_reached) {
7208 ret = tree_advance(right_path, &right_level,
7210 advance_right != ADVANCE_ONLY_NEXT,
7211 &right_key, reada_min_gen);
7213 right_end_reached = ADVANCE;
7219 if (left_end_reached && right_end_reached) {
7222 } else if (left_end_reached) {
7223 if (right_level == 0) {
7224 up_read(&fs_info->commit_root_sem);
7225 ret = changed_cb(left_path, right_path,
7227 BTRFS_COMPARE_TREE_DELETED,
7231 down_read(&fs_info->commit_root_sem);
7233 advance_right = ADVANCE;
7235 } else if (right_end_reached) {
7236 if (left_level == 0) {
7237 up_read(&fs_info->commit_root_sem);
7238 ret = changed_cb(left_path, right_path,
7240 BTRFS_COMPARE_TREE_NEW,
7244 down_read(&fs_info->commit_root_sem);
7246 advance_left = ADVANCE;
7250 if (left_level == 0 && right_level == 0) {
7251 up_read(&fs_info->commit_root_sem);
7252 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7254 ret = changed_cb(left_path, right_path,
7256 BTRFS_COMPARE_TREE_NEW,
7258 advance_left = ADVANCE;
7259 } else if (cmp > 0) {
7260 ret = changed_cb(left_path, right_path,
7262 BTRFS_COMPARE_TREE_DELETED,
7264 advance_right = ADVANCE;
7266 enum btrfs_compare_tree_result result;
7268 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
7269 ret = tree_compare_item(left_path, right_path,
7272 result = BTRFS_COMPARE_TREE_CHANGED;
7274 result = BTRFS_COMPARE_TREE_SAME;
7275 ret = changed_cb(left_path, right_path,
7276 &left_key, result, sctx);
7277 advance_left = ADVANCE;
7278 advance_right = ADVANCE;
7283 down_read(&fs_info->commit_root_sem);
7284 } else if (left_level == right_level) {
7285 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7287 advance_left = ADVANCE;
7288 } else if (cmp > 0) {
7289 advance_right = ADVANCE;
7291 left_blockptr = btrfs_node_blockptr(
7292 left_path->nodes[left_level],
7293 left_path->slots[left_level]);
7294 right_blockptr = btrfs_node_blockptr(
7295 right_path->nodes[right_level],
7296 right_path->slots[right_level]);
7297 left_gen = btrfs_node_ptr_generation(
7298 left_path->nodes[left_level],
7299 left_path->slots[left_level]);
7300 right_gen = btrfs_node_ptr_generation(
7301 right_path->nodes[right_level],
7302 right_path->slots[right_level]);
7303 if (left_blockptr == right_blockptr &&
7304 left_gen == right_gen) {
7306 * As we're on a shared block, don't
7307 * allow to go deeper.
7309 advance_left = ADVANCE_ONLY_NEXT;
7310 advance_right = ADVANCE_ONLY_NEXT;
7312 advance_left = ADVANCE;
7313 advance_right = ADVANCE;
7316 } else if (left_level < right_level) {
7317 advance_right = ADVANCE;
7319 advance_left = ADVANCE;
7324 up_read(&fs_info->commit_root_sem);
7326 btrfs_free_path(left_path);
7327 btrfs_free_path(right_path);
7332 static int send_subvol(struct send_ctx *sctx)
7336 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
7337 ret = send_header(sctx);
7342 ret = send_subvol_begin(sctx);
7346 if (sctx->parent_root) {
7347 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
7350 ret = finish_inode_if_needed(sctx, 1);
7354 ret = full_send_tree(sctx);
7360 free_recorded_refs(sctx);
7365 * If orphan cleanup did remove any orphans from a root, it means the tree
7366 * was modified and therefore the commit root is not the same as the current
7367 * root anymore. This is a problem, because send uses the commit root and
7368 * therefore can see inode items that don't exist in the current root anymore,
7369 * and for example make calls to btrfs_iget, which will do tree lookups based
7370 * on the current root and not on the commit root. Those lookups will fail,
7371 * returning a -ESTALE error, and making send fail with that error. So make
7372 * sure a send does not see any orphans we have just removed, and that it will
7373 * see the same inodes regardless of whether a transaction commit happened
7374 * before it started (meaning that the commit root will be the same as the
7375 * current root) or not.
7377 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7380 struct btrfs_trans_handle *trans = NULL;
7383 if (sctx->parent_root &&
7384 sctx->parent_root->node != sctx->parent_root->commit_root)
7387 for (i = 0; i < sctx->clone_roots_cnt; i++)
7388 if (sctx->clone_roots[i].root->node !=
7389 sctx->clone_roots[i].root->commit_root)
7393 return btrfs_end_transaction(trans);
7398 /* Use any root, all fs roots will get their commit roots updated. */
7400 trans = btrfs_join_transaction(sctx->send_root);
7402 return PTR_ERR(trans);
7406 return btrfs_commit_transaction(trans);
7410 * Make sure any existing dellaloc is flushed for any root used by a send
7411 * operation so that we do not miss any data and we do not race with writeback
7412 * finishing and changing a tree while send is using the tree. This could
7413 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
7414 * a send operation then uses the subvolume.
7415 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
7417 static int flush_delalloc_roots(struct send_ctx *sctx)
7419 struct btrfs_root *root = sctx->parent_root;
7424 ret = btrfs_start_delalloc_snapshot(root, false);
7427 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7430 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7431 root = sctx->clone_roots[i].root;
7432 ret = btrfs_start_delalloc_snapshot(root, false);
7435 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7441 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7443 spin_lock(&root->root_item_lock);
7444 root->send_in_progress--;
7446 * Not much left to do, we don't know why it's unbalanced and
7447 * can't blindly reset it to 0.
7449 if (root->send_in_progress < 0)
7450 btrfs_err(root->fs_info,
7451 "send_in_progress unbalanced %d root %llu",
7452 root->send_in_progress, root->root_key.objectid);
7453 spin_unlock(&root->root_item_lock);
7456 static void dedupe_in_progress_warn(const struct btrfs_root *root)
7458 btrfs_warn_rl(root->fs_info,
7459 "cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7460 root->root_key.objectid, root->dedupe_in_progress);
7463 long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg)
7466 struct btrfs_root *send_root = BTRFS_I(inode)->root;
7467 struct btrfs_fs_info *fs_info = send_root->fs_info;
7468 struct btrfs_root *clone_root;
7469 struct send_ctx *sctx = NULL;
7471 u64 *clone_sources_tmp = NULL;
7472 int clone_sources_to_rollback = 0;
7474 int sort_clone_roots = 0;
7476 if (!capable(CAP_SYS_ADMIN))
7480 * The subvolume must remain read-only during send, protect against
7481 * making it RW. This also protects against deletion.
7483 spin_lock(&send_root->root_item_lock);
7484 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7485 dedupe_in_progress_warn(send_root);
7486 spin_unlock(&send_root->root_item_lock);
7489 send_root->send_in_progress++;
7490 spin_unlock(&send_root->root_item_lock);
7493 * Userspace tools do the checks and warn the user if it's
7496 if (!btrfs_root_readonly(send_root)) {
7502 * Check that we don't overflow at later allocations, we request
7503 * clone_sources_count + 1 items, and compare to unsigned long inside
7506 if (arg->clone_sources_count >
7507 ULONG_MAX / sizeof(struct clone_root) - 1) {
7512 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
7517 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
7523 INIT_LIST_HEAD(&sctx->new_refs);
7524 INIT_LIST_HEAD(&sctx->deleted_refs);
7525 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
7526 INIT_LIST_HEAD(&sctx->name_cache_list);
7528 sctx->flags = arg->flags;
7530 if (arg->flags & BTRFS_SEND_FLAG_VERSION) {
7531 if (arg->version > BTRFS_SEND_STREAM_VERSION) {
7535 /* Zero means "use the highest version" */
7536 sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION;
7541 sctx->send_filp = fget(arg->send_fd);
7542 if (!sctx->send_filp) {
7547 sctx->send_root = send_root;
7549 * Unlikely but possible, if the subvolume is marked for deletion but
7550 * is slow to remove the directory entry, send can still be started
7552 if (btrfs_root_dead(sctx->send_root)) {
7557 sctx->clone_roots_cnt = arg->clone_sources_count;
7559 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
7560 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
7561 if (!sctx->send_buf) {
7566 sctx->pending_dir_moves = RB_ROOT;
7567 sctx->waiting_dir_moves = RB_ROOT;
7568 sctx->orphan_dirs = RB_ROOT;
7570 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
7571 arg->clone_sources_count + 1,
7573 if (!sctx->clone_roots) {
7578 alloc_size = array_size(sizeof(*arg->clone_sources),
7579 arg->clone_sources_count);
7581 if (arg->clone_sources_count) {
7582 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
7583 if (!clone_sources_tmp) {
7588 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
7595 for (i = 0; i < arg->clone_sources_count; i++) {
7596 clone_root = btrfs_get_fs_root(fs_info,
7597 clone_sources_tmp[i], true);
7598 if (IS_ERR(clone_root)) {
7599 ret = PTR_ERR(clone_root);
7602 spin_lock(&clone_root->root_item_lock);
7603 if (!btrfs_root_readonly(clone_root) ||
7604 btrfs_root_dead(clone_root)) {
7605 spin_unlock(&clone_root->root_item_lock);
7606 btrfs_put_root(clone_root);
7610 if (clone_root->dedupe_in_progress) {
7611 dedupe_in_progress_warn(clone_root);
7612 spin_unlock(&clone_root->root_item_lock);
7613 btrfs_put_root(clone_root);
7617 clone_root->send_in_progress++;
7618 spin_unlock(&clone_root->root_item_lock);
7620 sctx->clone_roots[i].root = clone_root;
7621 clone_sources_to_rollback = i + 1;
7623 kvfree(clone_sources_tmp);
7624 clone_sources_tmp = NULL;
7627 if (arg->parent_root) {
7628 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7630 if (IS_ERR(sctx->parent_root)) {
7631 ret = PTR_ERR(sctx->parent_root);
7635 spin_lock(&sctx->parent_root->root_item_lock);
7636 sctx->parent_root->send_in_progress++;
7637 if (!btrfs_root_readonly(sctx->parent_root) ||
7638 btrfs_root_dead(sctx->parent_root)) {
7639 spin_unlock(&sctx->parent_root->root_item_lock);
7643 if (sctx->parent_root->dedupe_in_progress) {
7644 dedupe_in_progress_warn(sctx->parent_root);
7645 spin_unlock(&sctx->parent_root->root_item_lock);
7649 spin_unlock(&sctx->parent_root->root_item_lock);
7653 * Clones from send_root are allowed, but only if the clone source
7654 * is behind the current send position. This is checked while searching
7655 * for possible clone sources.
7657 sctx->clone_roots[sctx->clone_roots_cnt++].root =
7658 btrfs_grab_root(sctx->send_root);
7660 /* We do a bsearch later */
7661 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7662 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7664 sort_clone_roots = 1;
7666 ret = flush_delalloc_roots(sctx);
7670 ret = ensure_commit_roots_uptodate(sctx);
7674 ret = send_subvol(sctx);
7678 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7679 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7682 ret = send_cmd(sctx);
7688 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7689 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7691 struct pending_dir_move *pm;
7693 n = rb_first(&sctx->pending_dir_moves);
7694 pm = rb_entry(n, struct pending_dir_move, node);
7695 while (!list_empty(&pm->list)) {
7696 struct pending_dir_move *pm2;
7698 pm2 = list_first_entry(&pm->list,
7699 struct pending_dir_move, list);
7700 free_pending_move(sctx, pm2);
7702 free_pending_move(sctx, pm);
7705 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7706 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7708 struct waiting_dir_move *dm;
7710 n = rb_first(&sctx->waiting_dir_moves);
7711 dm = rb_entry(n, struct waiting_dir_move, node);
7712 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7716 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7717 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7719 struct orphan_dir_info *odi;
7721 n = rb_first(&sctx->orphan_dirs);
7722 odi = rb_entry(n, struct orphan_dir_info, node);
7723 free_orphan_dir_info(sctx, odi);
7726 if (sort_clone_roots) {
7727 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7728 btrfs_root_dec_send_in_progress(
7729 sctx->clone_roots[i].root);
7730 btrfs_put_root(sctx->clone_roots[i].root);
7733 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
7734 btrfs_root_dec_send_in_progress(
7735 sctx->clone_roots[i].root);
7736 btrfs_put_root(sctx->clone_roots[i].root);
7739 btrfs_root_dec_send_in_progress(send_root);
7741 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
7742 btrfs_root_dec_send_in_progress(sctx->parent_root);
7743 btrfs_put_root(sctx->parent_root);
7746 kvfree(clone_sources_tmp);
7749 if (sctx->send_filp)
7750 fput(sctx->send_filp);
7752 kvfree(sctx->clone_roots);
7753 kvfree(sctx->send_buf);
7755 name_cache_free(sctx);
7757 close_current_inode(sctx);