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/vmalloc.h>
14 #include <linux/string.h>
15 #include <linux/compat.h>
16 #include <linux/crc32c.h>
22 #include "btrfs_inode.h"
23 #include "transaction.h"
24 #include "compression.h"
26 #include "print-tree.h"
29 * Maximum number of references an extent can have in order for us to attempt to
30 * issue clone operations instead of write operations. This currently exists to
31 * avoid hitting limitations of the backreference walking code (taking a lot of
32 * time and using too much memory for extents with large number of references).
34 #define SEND_MAX_EXTENT_REFS 64
37 * A fs_path is a helper to dynamically build path names with unknown size.
38 * It reallocates the internal buffer on demand.
39 * It allows fast adding of path elements on the right side (normal path) and
40 * fast adding to the left side (reversed path). A reversed path can also be
41 * unreversed if needed.
50 unsigned short buf_len:15;
51 unsigned short reversed:1;
55 * Average path length does not exceed 200 bytes, we'll have
56 * better packing in the slab and higher chance to satisfy
57 * a allocation later during send.
62 #define FS_PATH_INLINE_SIZE \
63 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
66 /* reused for each extent */
68 struct btrfs_root *root;
75 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
76 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
79 struct file *send_filp;
85 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
86 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
87 /* Protocol version compatibility requested */
90 struct btrfs_root *send_root;
91 struct btrfs_root *parent_root;
92 struct clone_root *clone_roots;
95 /* current state of the compare_tree call */
96 struct btrfs_path *left_path;
97 struct btrfs_path *right_path;
98 struct btrfs_key *cmp_key;
101 * Keep track of the generation of the last transaction that was used
102 * for relocating a block group. This is periodically checked in order
103 * to detect if a relocation happened since the last check, so that we
104 * don't operate on stale extent buffers for nodes (level >= 1) or on
105 * stale disk_bytenr values of file extent items.
107 u64 last_reloc_trans;
110 * infos of the currently processed inode. In case of deleted inodes,
111 * these are the values from the deleted inode.
116 int cur_inode_new_gen;
117 int cur_inode_deleted;
121 u64 cur_inode_last_extent;
122 u64 cur_inode_next_write_offset;
123 bool ignore_cur_inode;
127 struct list_head new_refs;
128 struct list_head deleted_refs;
130 struct xarray name_cache;
131 struct list_head name_cache_list;
135 * The inode we are currently processing. It's not NULL only when we
136 * need to issue write commands for data extents from this inode.
138 struct inode *cur_inode;
139 struct file_ra_state ra;
140 u64 page_cache_clear_start;
141 bool clean_page_cache;
144 * We process inodes by their increasing order, so if before an
145 * incremental send we reverse the parent/child relationship of
146 * directories such that a directory with a lower inode number was
147 * the parent of a directory with a higher inode number, and the one
148 * becoming the new parent got renamed too, we can't rename/move the
149 * directory with lower inode number when we finish processing it - we
150 * must process the directory with higher inode number first, then
151 * rename/move it and then rename/move the directory with lower inode
152 * number. Example follows.
154 * Tree state when the first send was performed:
166 * Tree state when the second (incremental) send is performed:
175 * The sequence of steps that lead to the second state was:
177 * mv /a/b/c/d /a/b/c2/d2
178 * mv /a/b/c /a/b/c2/d2/cc
180 * "c" has lower inode number, but we can't move it (2nd mv operation)
181 * before we move "d", which has higher inode number.
183 * So we just memorize which move/rename operations must be performed
184 * later when their respective parent is processed and moved/renamed.
187 /* Indexed by parent directory inode number. */
188 struct rb_root pending_dir_moves;
191 * Reverse index, indexed by the inode number of a directory that
192 * is waiting for the move/rename of its immediate parent before its
193 * own move/rename can be performed.
195 struct rb_root waiting_dir_moves;
198 * A directory that is going to be rm'ed might have a child directory
199 * which is in the pending directory moves index above. In this case,
200 * the directory can only be removed after the move/rename of its child
201 * is performed. Example:
221 * Sequence of steps that lead to the send snapshot:
222 * rm -f /a/b/c/foo.txt
224 * mv /a/b/c/x /a/b/YY
227 * When the child is processed, its move/rename is delayed until its
228 * parent is processed (as explained above), but all other operations
229 * like update utimes, chown, chgrp, etc, are performed and the paths
230 * that it uses for those operations must use the orphanized name of
231 * its parent (the directory we're going to rm later), so we need to
232 * memorize that name.
234 * Indexed by the inode number of the directory to be deleted.
236 struct rb_root orphan_dirs;
239 struct pending_dir_move {
241 struct list_head list;
245 struct list_head update_refs;
248 struct waiting_dir_move {
252 * There might be some directory that could not be removed because it
253 * was waiting for this directory inode to be moved first. Therefore
254 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
261 struct orphan_dir_info {
265 u64 last_dir_index_offset;
268 struct name_cache_entry {
269 struct list_head list;
271 * On 32bit kernels, xarray has only 32bit indices, but we need to
272 * handle 64bit inums. We use the lower 32bit of the 64bit inum to store
273 * it in the tree. If more than one inum would fall into the same entry,
274 * we use inum_aliases to store the additional entries. inum_aliases is
275 * also used to store entries with the same inum but different generations.
277 struct list_head inum_aliases;
283 int need_later_update;
289 #define ADVANCE_ONLY_NEXT -1
291 enum btrfs_compare_tree_result {
292 BTRFS_COMPARE_TREE_NEW,
293 BTRFS_COMPARE_TREE_DELETED,
294 BTRFS_COMPARE_TREE_CHANGED,
295 BTRFS_COMPARE_TREE_SAME,
299 static void inconsistent_snapshot_error(struct send_ctx *sctx,
300 enum btrfs_compare_tree_result result,
303 const char *result_string;
306 case BTRFS_COMPARE_TREE_NEW:
307 result_string = "new";
309 case BTRFS_COMPARE_TREE_DELETED:
310 result_string = "deleted";
312 case BTRFS_COMPARE_TREE_CHANGED:
313 result_string = "updated";
315 case BTRFS_COMPARE_TREE_SAME:
317 result_string = "unchanged";
321 result_string = "unexpected";
324 btrfs_err(sctx->send_root->fs_info,
325 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
326 result_string, what, sctx->cmp_key->objectid,
327 sctx->send_root->root_key.objectid,
329 sctx->parent_root->root_key.objectid : 0));
333 static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd)
335 switch (sctx->proto) {
336 case 1: return cmd < __BTRFS_SEND_C_MAX_V1;
337 case 2: return cmd < __BTRFS_SEND_C_MAX_V2;
338 default: return false;
342 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
344 static struct waiting_dir_move *
345 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
347 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
349 static int need_send_hole(struct send_ctx *sctx)
351 return (sctx->parent_root && !sctx->cur_inode_new &&
352 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
353 S_ISREG(sctx->cur_inode_mode));
356 static void fs_path_reset(struct fs_path *p)
359 p->start = p->buf + p->buf_len - 1;
369 static struct fs_path *fs_path_alloc(void)
373 p = kmalloc(sizeof(*p), GFP_KERNEL);
377 p->buf = p->inline_buf;
378 p->buf_len = FS_PATH_INLINE_SIZE;
383 static struct fs_path *fs_path_alloc_reversed(void)
395 static void fs_path_free(struct fs_path *p)
399 if (p->buf != p->inline_buf)
404 static int fs_path_len(struct fs_path *p)
406 return p->end - p->start;
409 static int fs_path_ensure_buf(struct fs_path *p, int len)
417 if (p->buf_len >= len)
420 if (len > PATH_MAX) {
425 path_len = p->end - p->start;
426 old_buf_len = p->buf_len;
429 * First time the inline_buf does not suffice
431 if (p->buf == p->inline_buf) {
432 tmp_buf = kmalloc(len, GFP_KERNEL);
434 memcpy(tmp_buf, p->buf, old_buf_len);
436 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
442 * The real size of the buffer is bigger, this will let the fast path
443 * happen most of the time
445 p->buf_len = ksize(p->buf);
448 tmp_buf = p->buf + old_buf_len - path_len - 1;
449 p->end = p->buf + p->buf_len - 1;
450 p->start = p->end - path_len;
451 memmove(p->start, tmp_buf, path_len + 1);
454 p->end = p->start + path_len;
459 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
465 new_len = p->end - p->start + name_len;
466 if (p->start != p->end)
468 ret = fs_path_ensure_buf(p, new_len);
473 if (p->start != p->end)
475 p->start -= name_len;
476 *prepared = p->start;
478 if (p->start != p->end)
489 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
494 ret = fs_path_prepare_for_add(p, name_len, &prepared);
497 memcpy(prepared, name, name_len);
503 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
508 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
511 memcpy(prepared, p2->start, p2->end - p2->start);
517 static int fs_path_add_from_extent_buffer(struct fs_path *p,
518 struct extent_buffer *eb,
519 unsigned long off, int len)
524 ret = fs_path_prepare_for_add(p, len, &prepared);
528 read_extent_buffer(eb, prepared, off, len);
534 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
536 p->reversed = from->reversed;
539 return fs_path_add_path(p, from);
542 static void fs_path_unreverse(struct fs_path *p)
551 len = p->end - p->start;
553 p->end = p->start + len;
554 memmove(p->start, tmp, len + 1);
558 static struct btrfs_path *alloc_path_for_send(void)
560 struct btrfs_path *path;
562 path = btrfs_alloc_path();
565 path->search_commit_root = 1;
566 path->skip_locking = 1;
567 path->need_commit_sem = 1;
571 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
577 ret = kernel_write(filp, buf + pos, len - pos, off);
578 /* TODO handle that correctly */
579 /*if (ret == -ERESTARTSYS) {
593 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
595 struct btrfs_tlv_header *hdr;
596 int total_len = sizeof(*hdr) + len;
597 int left = sctx->send_max_size - sctx->send_size;
599 if (unlikely(left < total_len))
602 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
603 put_unaligned_le16(attr, &hdr->tlv_type);
604 put_unaligned_le16(len, &hdr->tlv_len);
605 memcpy(hdr + 1, data, len);
606 sctx->send_size += total_len;
611 #define TLV_PUT_DEFINE_INT(bits) \
612 static int tlv_put_u##bits(struct send_ctx *sctx, \
613 u##bits attr, u##bits value) \
615 __le##bits __tmp = cpu_to_le##bits(value); \
616 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
619 TLV_PUT_DEFINE_INT(64)
621 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
622 const char *str, int len)
626 return tlv_put(sctx, attr, str, len);
629 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
632 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
635 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
636 struct extent_buffer *eb,
637 struct btrfs_timespec *ts)
639 struct btrfs_timespec bts;
640 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
641 return tlv_put(sctx, attr, &bts, sizeof(bts));
645 #define TLV_PUT(sctx, attrtype, data, attrlen) \
647 ret = tlv_put(sctx, attrtype, data, attrlen); \
649 goto tlv_put_failure; \
652 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
654 ret = tlv_put_u##bits(sctx, attrtype, value); \
656 goto tlv_put_failure; \
659 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
660 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
661 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
662 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
663 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
665 ret = tlv_put_string(sctx, attrtype, str, len); \
667 goto tlv_put_failure; \
669 #define TLV_PUT_PATH(sctx, attrtype, p) \
671 ret = tlv_put_string(sctx, attrtype, p->start, \
672 p->end - p->start); \
674 goto tlv_put_failure; \
676 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
678 ret = tlv_put_uuid(sctx, attrtype, uuid); \
680 goto tlv_put_failure; \
682 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
684 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
686 goto tlv_put_failure; \
689 static int send_header(struct send_ctx *sctx)
691 struct btrfs_stream_header hdr;
693 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
694 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
696 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
701 * For each command/item we want to send to userspace, we call this function.
703 static int begin_cmd(struct send_ctx *sctx, int cmd)
705 struct btrfs_cmd_header *hdr;
707 if (WARN_ON(!sctx->send_buf))
710 BUG_ON(sctx->send_size);
712 sctx->send_size += sizeof(*hdr);
713 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
714 put_unaligned_le16(cmd, &hdr->cmd);
719 static int send_cmd(struct send_ctx *sctx)
722 struct btrfs_cmd_header *hdr;
725 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
726 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
727 put_unaligned_le32(0, &hdr->crc);
729 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
730 put_unaligned_le32(crc, &hdr->crc);
732 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
735 sctx->total_send_size += sctx->send_size;
736 sctx->cmd_send_size[get_unaligned_le16(&hdr->cmd)] += sctx->send_size;
743 * Sends a move instruction to user space
745 static int send_rename(struct send_ctx *sctx,
746 struct fs_path *from, struct fs_path *to)
748 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
751 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
753 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
757 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
758 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
760 ret = send_cmd(sctx);
768 * Sends a link instruction to user space
770 static int send_link(struct send_ctx *sctx,
771 struct fs_path *path, struct fs_path *lnk)
773 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
776 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
778 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
782 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
783 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
785 ret = send_cmd(sctx);
793 * Sends an unlink instruction to user space
795 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
797 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
800 btrfs_debug(fs_info, "send_unlink %s", path->start);
802 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
806 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
808 ret = send_cmd(sctx);
816 * Sends a rmdir instruction to user space
818 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
820 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
823 btrfs_debug(fs_info, "send_rmdir %s", path->start);
825 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
829 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
831 ret = send_cmd(sctx);
839 * Helper function to retrieve some fields from an inode item.
841 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
842 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
846 struct btrfs_inode_item *ii;
847 struct btrfs_key key;
850 key.type = BTRFS_INODE_ITEM_KEY;
852 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
859 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
860 struct btrfs_inode_item);
862 *size = btrfs_inode_size(path->nodes[0], ii);
864 *gen = btrfs_inode_generation(path->nodes[0], ii);
866 *mode = btrfs_inode_mode(path->nodes[0], ii);
868 *uid = btrfs_inode_uid(path->nodes[0], ii);
870 *gid = btrfs_inode_gid(path->nodes[0], ii);
872 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
877 static int get_inode_info(struct btrfs_root *root,
878 u64 ino, u64 *size, u64 *gen,
879 u64 *mode, u64 *uid, u64 *gid,
882 struct btrfs_path *path;
885 path = alloc_path_for_send();
888 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
890 btrfs_free_path(path);
894 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
899 * Helper function to iterate the entries in ONE btrfs_inode_ref or
900 * btrfs_inode_extref.
901 * The iterate callback may return a non zero value to stop iteration. This can
902 * be a negative value for error codes or 1 to simply stop it.
904 * path must point to the INODE_REF or INODE_EXTREF when called.
906 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
907 struct btrfs_key *found_key, int resolve,
908 iterate_inode_ref_t iterate, void *ctx)
910 struct extent_buffer *eb = path->nodes[0];
911 struct btrfs_inode_ref *iref;
912 struct btrfs_inode_extref *extref;
913 struct btrfs_path *tmp_path;
917 int slot = path->slots[0];
924 unsigned long name_off;
925 unsigned long elem_size;
928 p = fs_path_alloc_reversed();
932 tmp_path = alloc_path_for_send();
939 if (found_key->type == BTRFS_INODE_REF_KEY) {
940 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
941 struct btrfs_inode_ref);
942 total = btrfs_item_size(eb, slot);
943 elem_size = sizeof(*iref);
945 ptr = btrfs_item_ptr_offset(eb, slot);
946 total = btrfs_item_size(eb, slot);
947 elem_size = sizeof(*extref);
950 while (cur < total) {
953 if (found_key->type == BTRFS_INODE_REF_KEY) {
954 iref = (struct btrfs_inode_ref *)(ptr + cur);
955 name_len = btrfs_inode_ref_name_len(eb, iref);
956 name_off = (unsigned long)(iref + 1);
957 index = btrfs_inode_ref_index(eb, iref);
958 dir = found_key->offset;
960 extref = (struct btrfs_inode_extref *)(ptr + cur);
961 name_len = btrfs_inode_extref_name_len(eb, extref);
962 name_off = (unsigned long)&extref->name;
963 index = btrfs_inode_extref_index(eb, extref);
964 dir = btrfs_inode_extref_parent(eb, extref);
968 start = btrfs_ref_to_path(root, tmp_path, name_len,
972 ret = PTR_ERR(start);
975 if (start < p->buf) {
976 /* overflow , try again with larger buffer */
977 ret = fs_path_ensure_buf(p,
978 p->buf_len + p->buf - start);
981 start = btrfs_ref_to_path(root, tmp_path,
986 ret = PTR_ERR(start);
989 BUG_ON(start < p->buf);
993 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
999 cur += elem_size + name_len;
1000 ret = iterate(num, dir, index, p, ctx);
1007 btrfs_free_path(tmp_path);
1012 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
1013 const char *name, int name_len,
1014 const char *data, int data_len,
1018 * Helper function to iterate the entries in ONE btrfs_dir_item.
1019 * The iterate callback may return a non zero value to stop iteration. This can
1020 * be a negative value for error codes or 1 to simply stop it.
1022 * path must point to the dir item when called.
1024 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
1025 iterate_dir_item_t iterate, void *ctx)
1028 struct extent_buffer *eb;
1029 struct btrfs_dir_item *di;
1030 struct btrfs_key di_key;
1042 * Start with a small buffer (1 page). If later we end up needing more
1043 * space, which can happen for xattrs on a fs with a leaf size greater
1044 * then the page size, attempt to increase the buffer. Typically xattr
1048 buf = kmalloc(buf_len, GFP_KERNEL);
1054 eb = path->nodes[0];
1055 slot = path->slots[0];
1056 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1059 total = btrfs_item_size(eb, slot);
1062 while (cur < total) {
1063 name_len = btrfs_dir_name_len(eb, di);
1064 data_len = btrfs_dir_data_len(eb, di);
1065 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1067 if (btrfs_dir_type(eb, di) == BTRFS_FT_XATTR) {
1068 if (name_len > XATTR_NAME_MAX) {
1069 ret = -ENAMETOOLONG;
1072 if (name_len + data_len >
1073 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1081 if (name_len + data_len > PATH_MAX) {
1082 ret = -ENAMETOOLONG;
1087 if (name_len + data_len > buf_len) {
1088 buf_len = name_len + data_len;
1089 if (is_vmalloc_addr(buf)) {
1093 char *tmp = krealloc(buf, buf_len,
1094 GFP_KERNEL | __GFP_NOWARN);
1101 buf = kvmalloc(buf_len, GFP_KERNEL);
1109 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1110 name_len + data_len);
1112 len = sizeof(*di) + name_len + data_len;
1113 di = (struct btrfs_dir_item *)((char *)di + len);
1116 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1133 static int __copy_first_ref(int num, u64 dir, int index,
1134 struct fs_path *p, void *ctx)
1137 struct fs_path *pt = ctx;
1139 ret = fs_path_copy(pt, p);
1143 /* we want the first only */
1148 * Retrieve the first path of an inode. If an inode has more then one
1149 * ref/hardlink, this is ignored.
1151 static int get_inode_path(struct btrfs_root *root,
1152 u64 ino, struct fs_path *path)
1155 struct btrfs_key key, found_key;
1156 struct btrfs_path *p;
1158 p = alloc_path_for_send();
1162 fs_path_reset(path);
1165 key.type = BTRFS_INODE_REF_KEY;
1168 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1175 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1176 if (found_key.objectid != ino ||
1177 (found_key.type != BTRFS_INODE_REF_KEY &&
1178 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1183 ret = iterate_inode_ref(root, p, &found_key, 1,
1184 __copy_first_ref, path);
1194 struct backref_ctx {
1195 struct send_ctx *sctx;
1197 /* number of total found references */
1201 * used for clones found in send_root. clones found behind cur_objectid
1202 * and cur_offset are not considered as allowed clones.
1207 /* may be truncated in case it's the last extent in a file */
1210 /* Just to check for bugs in backref resolving */
1214 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1216 u64 root = (u64)(uintptr_t)key;
1217 const struct clone_root *cr = elt;
1219 if (root < cr->root->root_key.objectid)
1221 if (root > cr->root->root_key.objectid)
1226 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1228 const struct clone_root *cr1 = e1;
1229 const struct clone_root *cr2 = e2;
1231 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
1233 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
1239 * Called for every backref that is found for the current extent.
1240 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1242 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1244 struct backref_ctx *bctx = ctx_;
1245 struct clone_root *found;
1247 /* First check if the root is in the list of accepted clone sources */
1248 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1249 bctx->sctx->clone_roots_cnt,
1250 sizeof(struct clone_root),
1251 __clone_root_cmp_bsearch);
1255 if (found->root == bctx->sctx->send_root &&
1256 ino == bctx->cur_objectid &&
1257 offset == bctx->cur_offset) {
1258 bctx->found_itself = 1;
1262 * Make sure we don't consider clones from send_root that are
1263 * behind the current inode/offset.
1265 if (found->root == bctx->sctx->send_root) {
1267 * If the source inode was not yet processed we can't issue a
1268 * clone operation, as the source extent does not exist yet at
1269 * the destination of the stream.
1271 if (ino > bctx->cur_objectid)
1274 * We clone from the inode currently being sent as long as the
1275 * source extent is already processed, otherwise we could try
1276 * to clone from an extent that does not exist yet at the
1277 * destination of the stream.
1279 if (ino == bctx->cur_objectid &&
1280 offset + bctx->extent_len >
1281 bctx->sctx->cur_inode_next_write_offset)
1286 found->found_refs++;
1287 if (ino < found->ino) {
1289 found->offset = offset;
1290 } else if (found->ino == ino) {
1292 * same extent found more then once in the same file.
1294 if (found->offset > offset + bctx->extent_len)
1295 found->offset = offset;
1302 * Given an inode, offset and extent item, it finds a good clone for a clone
1303 * instruction. Returns -ENOENT when none could be found. The function makes
1304 * sure that the returned clone is usable at the point where sending is at the
1305 * moment. This means, that no clones are accepted which lie behind the current
1308 * path must point to the extent item when called.
1310 static int find_extent_clone(struct send_ctx *sctx,
1311 struct btrfs_path *path,
1312 u64 ino, u64 data_offset,
1314 struct clone_root **found)
1316 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1322 u64 extent_item_pos;
1324 struct btrfs_file_extent_item *fi;
1325 struct extent_buffer *eb = path->nodes[0];
1326 struct backref_ctx backref_ctx = {0};
1327 struct clone_root *cur_clone_root;
1328 struct btrfs_key found_key;
1329 struct btrfs_path *tmp_path;
1330 struct btrfs_extent_item *ei;
1334 tmp_path = alloc_path_for_send();
1338 /* We only use this path under the commit sem */
1339 tmp_path->need_commit_sem = 0;
1341 if (data_offset >= ino_size) {
1343 * There may be extents that lie behind the file's size.
1344 * I at least had this in combination with snapshotting while
1345 * writing large files.
1351 fi = btrfs_item_ptr(eb, path->slots[0],
1352 struct btrfs_file_extent_item);
1353 extent_type = btrfs_file_extent_type(eb, fi);
1354 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1358 compressed = btrfs_file_extent_compression(eb, fi);
1360 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1361 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1362 if (disk_byte == 0) {
1366 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1368 down_read(&fs_info->commit_root_sem);
1369 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1370 &found_key, &flags);
1371 up_read(&fs_info->commit_root_sem);
1375 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1380 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1381 struct btrfs_extent_item);
1383 * Backreference walking (iterate_extent_inodes() below) is currently
1384 * too expensive when an extent has a large number of references, both
1385 * in time spent and used memory. So for now just fallback to write
1386 * operations instead of clone operations when an extent has more than
1387 * a certain amount of references.
1389 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1393 btrfs_release_path(tmp_path);
1396 * Setup the clone roots.
1398 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1399 cur_clone_root = sctx->clone_roots + i;
1400 cur_clone_root->ino = (u64)-1;
1401 cur_clone_root->offset = 0;
1402 cur_clone_root->found_refs = 0;
1405 backref_ctx.sctx = sctx;
1406 backref_ctx.found = 0;
1407 backref_ctx.cur_objectid = ino;
1408 backref_ctx.cur_offset = data_offset;
1409 backref_ctx.found_itself = 0;
1410 backref_ctx.extent_len = num_bytes;
1413 * The last extent of a file may be too large due to page alignment.
1414 * We need to adjust extent_len in this case so that the checks in
1415 * __iterate_backrefs work.
1417 if (data_offset + num_bytes >= ino_size)
1418 backref_ctx.extent_len = ino_size - data_offset;
1421 * Now collect all backrefs.
1423 if (compressed == BTRFS_COMPRESS_NONE)
1424 extent_item_pos = logical - found_key.objectid;
1426 extent_item_pos = 0;
1427 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1428 extent_item_pos, 1, __iterate_backrefs,
1429 &backref_ctx, false);
1434 down_read(&fs_info->commit_root_sem);
1435 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
1437 * A transaction commit for a transaction in which block group
1438 * relocation was done just happened.
1439 * The disk_bytenr of the file extent item we processed is
1440 * possibly stale, referring to the extent's location before
1441 * relocation. So act as if we haven't found any clone sources
1442 * and fallback to write commands, which will read the correct
1443 * data from the new extent location. Otherwise we will fail
1444 * below because we haven't found our own back reference or we
1445 * could be getting incorrect sources in case the old extent
1446 * was already reallocated after the relocation.
1448 up_read(&fs_info->commit_root_sem);
1452 up_read(&fs_info->commit_root_sem);
1454 if (!backref_ctx.found_itself) {
1455 /* found a bug in backref code? */
1458 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1459 ino, data_offset, disk_byte, found_key.objectid);
1463 btrfs_debug(fs_info,
1464 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1465 data_offset, ino, num_bytes, logical);
1467 if (!backref_ctx.found)
1468 btrfs_debug(fs_info, "no clones found");
1470 cur_clone_root = NULL;
1471 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1472 if (sctx->clone_roots[i].found_refs) {
1473 if (!cur_clone_root)
1474 cur_clone_root = sctx->clone_roots + i;
1475 else if (sctx->clone_roots[i].root == sctx->send_root)
1476 /* prefer clones from send_root over others */
1477 cur_clone_root = sctx->clone_roots + i;
1482 if (cur_clone_root) {
1483 *found = cur_clone_root;
1490 btrfs_free_path(tmp_path);
1494 static int read_symlink(struct btrfs_root *root,
1496 struct fs_path *dest)
1499 struct btrfs_path *path;
1500 struct btrfs_key key;
1501 struct btrfs_file_extent_item *ei;
1507 path = alloc_path_for_send();
1512 key.type = BTRFS_EXTENT_DATA_KEY;
1514 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1519 * An empty symlink inode. Can happen in rare error paths when
1520 * creating a symlink (transaction committed before the inode
1521 * eviction handler removed the symlink inode items and a crash
1522 * happened in between or the subvol was snapshoted in between).
1523 * Print an informative message to dmesg/syslog so that the user
1524 * can delete the symlink.
1526 btrfs_err(root->fs_info,
1527 "Found empty symlink inode %llu at root %llu",
1528 ino, root->root_key.objectid);
1533 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1534 struct btrfs_file_extent_item);
1535 type = btrfs_file_extent_type(path->nodes[0], ei);
1536 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1537 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1538 BUG_ON(compression);
1540 off = btrfs_file_extent_inline_start(ei);
1541 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
1543 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1546 btrfs_free_path(path);
1551 * Helper function to generate a file name that is unique in the root of
1552 * send_root and parent_root. This is used to generate names for orphan inodes.
1554 static int gen_unique_name(struct send_ctx *sctx,
1556 struct fs_path *dest)
1559 struct btrfs_path *path;
1560 struct btrfs_dir_item *di;
1565 path = alloc_path_for_send();
1570 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1572 ASSERT(len < sizeof(tmp));
1574 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1575 path, BTRFS_FIRST_FREE_OBJECTID,
1576 tmp, strlen(tmp), 0);
1577 btrfs_release_path(path);
1583 /* not unique, try again */
1588 if (!sctx->parent_root) {
1594 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1595 path, BTRFS_FIRST_FREE_OBJECTID,
1596 tmp, strlen(tmp), 0);
1597 btrfs_release_path(path);
1603 /* not unique, try again */
1611 ret = fs_path_add(dest, tmp, strlen(tmp));
1614 btrfs_free_path(path);
1619 inode_state_no_change,
1620 inode_state_will_create,
1621 inode_state_did_create,
1622 inode_state_will_delete,
1623 inode_state_did_delete,
1626 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1634 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1636 if (ret < 0 && ret != -ENOENT)
1640 if (!sctx->parent_root) {
1641 right_ret = -ENOENT;
1643 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1644 NULL, NULL, NULL, NULL);
1645 if (ret < 0 && ret != -ENOENT)
1650 if (!left_ret && !right_ret) {
1651 if (left_gen == gen && right_gen == gen) {
1652 ret = inode_state_no_change;
1653 } else if (left_gen == gen) {
1654 if (ino < sctx->send_progress)
1655 ret = inode_state_did_create;
1657 ret = inode_state_will_create;
1658 } else if (right_gen == gen) {
1659 if (ino < sctx->send_progress)
1660 ret = inode_state_did_delete;
1662 ret = inode_state_will_delete;
1666 } else if (!left_ret) {
1667 if (left_gen == gen) {
1668 if (ino < sctx->send_progress)
1669 ret = inode_state_did_create;
1671 ret = inode_state_will_create;
1675 } else if (!right_ret) {
1676 if (right_gen == gen) {
1677 if (ino < sctx->send_progress)
1678 ret = inode_state_did_delete;
1680 ret = inode_state_will_delete;
1692 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1696 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1699 ret = get_cur_inode_state(sctx, ino, gen);
1703 if (ret == inode_state_no_change ||
1704 ret == inode_state_did_create ||
1705 ret == inode_state_will_delete)
1715 * Helper function to lookup a dir item in a dir.
1717 static int lookup_dir_item_inode(struct btrfs_root *root,
1718 u64 dir, const char *name, int name_len,
1722 struct btrfs_dir_item *di;
1723 struct btrfs_key key;
1724 struct btrfs_path *path;
1726 path = alloc_path_for_send();
1730 di = btrfs_lookup_dir_item(NULL, root, path,
1731 dir, name, name_len, 0);
1732 if (IS_ERR_OR_NULL(di)) {
1733 ret = di ? PTR_ERR(di) : -ENOENT;
1736 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1737 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1741 *found_inode = key.objectid;
1744 btrfs_free_path(path);
1749 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1750 * generation of the parent dir and the name of the dir entry.
1752 static int get_first_ref(struct btrfs_root *root, u64 ino,
1753 u64 *dir, u64 *dir_gen, struct fs_path *name)
1756 struct btrfs_key key;
1757 struct btrfs_key found_key;
1758 struct btrfs_path *path;
1762 path = alloc_path_for_send();
1767 key.type = BTRFS_INODE_REF_KEY;
1770 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1774 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1776 if (ret || found_key.objectid != ino ||
1777 (found_key.type != BTRFS_INODE_REF_KEY &&
1778 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1783 if (found_key.type == BTRFS_INODE_REF_KEY) {
1784 struct btrfs_inode_ref *iref;
1785 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1786 struct btrfs_inode_ref);
1787 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1788 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1789 (unsigned long)(iref + 1),
1791 parent_dir = found_key.offset;
1793 struct btrfs_inode_extref *extref;
1794 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1795 struct btrfs_inode_extref);
1796 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1797 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1798 (unsigned long)&extref->name, len);
1799 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1803 btrfs_release_path(path);
1806 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1815 btrfs_free_path(path);
1819 static int is_first_ref(struct btrfs_root *root,
1821 const char *name, int name_len)
1824 struct fs_path *tmp_name;
1827 tmp_name = fs_path_alloc();
1831 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1835 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1840 ret = !memcmp(tmp_name->start, name, name_len);
1843 fs_path_free(tmp_name);
1848 * Used by process_recorded_refs to determine if a new ref would overwrite an
1849 * already existing ref. In case it detects an overwrite, it returns the
1850 * inode/gen in who_ino/who_gen.
1851 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1852 * to make sure later references to the overwritten inode are possible.
1853 * Orphanizing is however only required for the first ref of an inode.
1854 * process_recorded_refs does an additional is_first_ref check to see if
1855 * orphanizing is really required.
1857 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1858 const char *name, int name_len,
1859 u64 *who_ino, u64 *who_gen, u64 *who_mode)
1863 u64 other_inode = 0;
1865 if (!sctx->parent_root)
1868 ret = is_inode_existent(sctx, dir, dir_gen);
1873 * If we have a parent root we need to verify that the parent dir was
1874 * not deleted and then re-created, if it was then we have no overwrite
1875 * and we can just unlink this entry.
1877 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
1878 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1880 if (ret < 0 && ret != -ENOENT)
1890 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1892 if (ret < 0 && ret != -ENOENT)
1900 * Check if the overwritten ref was already processed. If yes, the ref
1901 * was already unlinked/moved, so we can safely assume that we will not
1902 * overwrite anything at this point in time.
1904 if (other_inode > sctx->send_progress ||
1905 is_waiting_for_move(sctx, other_inode)) {
1906 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1907 who_gen, who_mode, NULL, NULL, NULL);
1912 *who_ino = other_inode;
1922 * Checks if the ref was overwritten by an already processed inode. This is
1923 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1924 * thus the orphan name needs be used.
1925 * process_recorded_refs also uses it to avoid unlinking of refs that were
1928 static int did_overwrite_ref(struct send_ctx *sctx,
1929 u64 dir, u64 dir_gen,
1930 u64 ino, u64 ino_gen,
1931 const char *name, int name_len)
1937 if (!sctx->parent_root)
1940 ret = is_inode_existent(sctx, dir, dir_gen);
1944 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1945 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1947 if (ret < 0 && ret != -ENOENT)
1957 /* check if the ref was overwritten by another ref */
1958 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1960 if (ret < 0 && ret != -ENOENT)
1963 /* was never and will never be overwritten */
1968 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1973 if (ow_inode == ino && gen == ino_gen) {
1979 * We know that it is or will be overwritten. Check this now.
1980 * The current inode being processed might have been the one that caused
1981 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1982 * the current inode being processed.
1984 if ((ow_inode < sctx->send_progress) ||
1985 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1986 gen == sctx->cur_inode_gen))
1996 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1997 * that got overwritten. This is used by process_recorded_refs to determine
1998 * if it has to use the path as returned by get_cur_path or the orphan name.
2000 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
2003 struct fs_path *name = NULL;
2007 if (!sctx->parent_root)
2010 name = fs_path_alloc();
2014 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
2018 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2019 name->start, fs_path_len(name));
2027 * Insert a name cache entry. On 32bit kernels the xarray index is 32bit,
2028 * so we need to do some special handling in case we have clashes. This function
2029 * takes care of this with the help of name_cache_entry::inum_aliases.
2030 * In case of error, nce is kfreed.
2032 static int name_cache_insert(struct send_ctx *sctx,
2033 struct name_cache_entry *nce)
2036 struct list_head *nce_head;
2038 nce_head = xa_load(&sctx->name_cache, (unsigned long)nce->ino);
2040 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
2045 INIT_LIST_HEAD(nce_head);
2047 ret = xa_insert(&sctx->name_cache, nce->ino, nce_head, GFP_KERNEL);
2054 list_add_tail(&nce->inum_aliases, nce_head);
2055 list_add_tail(&nce->list, &sctx->name_cache_list);
2056 sctx->name_cache_size++;
2061 static void name_cache_delete(struct send_ctx *sctx,
2062 struct name_cache_entry *nce)
2064 struct list_head *nce_head;
2066 nce_head = xa_load(&sctx->name_cache, (unsigned long)nce->ino);
2068 btrfs_err(sctx->send_root->fs_info,
2069 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2070 nce->ino, sctx->name_cache_size);
2073 list_del(&nce->inum_aliases);
2074 list_del(&nce->list);
2075 sctx->name_cache_size--;
2078 * We may not get to the final release of nce_head if the lookup fails
2080 if (nce_head && list_empty(nce_head)) {
2081 xa_erase(&sctx->name_cache, (unsigned long)nce->ino);
2086 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2089 struct list_head *nce_head;
2090 struct name_cache_entry *cur;
2092 nce_head = xa_load(&sctx->name_cache, (unsigned long)ino);
2096 list_for_each_entry(cur, nce_head, inum_aliases) {
2097 if (cur->ino == ino && cur->gen == gen)
2104 * Remove some entries from the beginning of name_cache_list.
2106 static void name_cache_clean_unused(struct send_ctx *sctx)
2108 struct name_cache_entry *nce;
2110 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2113 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2114 nce = list_entry(sctx->name_cache_list.next,
2115 struct name_cache_entry, list);
2116 name_cache_delete(sctx, nce);
2121 static void name_cache_free(struct send_ctx *sctx)
2123 struct name_cache_entry *nce;
2125 while (!list_empty(&sctx->name_cache_list)) {
2126 nce = list_entry(sctx->name_cache_list.next,
2127 struct name_cache_entry, list);
2128 name_cache_delete(sctx, nce);
2134 * Used by get_cur_path for each ref up to the root.
2135 * Returns 0 if it succeeded.
2136 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2137 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2138 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2139 * Returns <0 in case of error.
2141 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2145 struct fs_path *dest)
2149 struct name_cache_entry *nce = NULL;
2152 * First check if we already did a call to this function with the same
2153 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2154 * return the cached result.
2156 nce = name_cache_search(sctx, ino, gen);
2158 if (ino < sctx->send_progress && nce->need_later_update) {
2159 name_cache_delete(sctx, nce);
2164 * Removes the entry from the list and adds it back to
2165 * the end. This marks the entry as recently used so
2166 * that name_cache_clean_unused does not remove it.
2168 list_move_tail(&nce->list, &sctx->name_cache_list);
2170 *parent_ino = nce->parent_ino;
2171 *parent_gen = nce->parent_gen;
2172 ret = fs_path_add(dest, nce->name, nce->name_len);
2181 * If the inode is not existent yet, add the orphan name and return 1.
2182 * This should only happen for the parent dir that we determine in
2185 ret = is_inode_existent(sctx, ino, gen);
2190 ret = gen_unique_name(sctx, ino, gen, dest);
2198 * Depending on whether the inode was already processed or not, use
2199 * send_root or parent_root for ref lookup.
2201 if (ino < sctx->send_progress)
2202 ret = get_first_ref(sctx->send_root, ino,
2203 parent_ino, parent_gen, dest);
2205 ret = get_first_ref(sctx->parent_root, ino,
2206 parent_ino, parent_gen, dest);
2211 * Check if the ref was overwritten by an inode's ref that was processed
2212 * earlier. If yes, treat as orphan and return 1.
2214 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2215 dest->start, dest->end - dest->start);
2219 fs_path_reset(dest);
2220 ret = gen_unique_name(sctx, ino, gen, dest);
2228 * Store the result of the lookup in the name cache.
2230 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2238 nce->parent_ino = *parent_ino;
2239 nce->parent_gen = *parent_gen;
2240 nce->name_len = fs_path_len(dest);
2242 strcpy(nce->name, dest->start);
2244 if (ino < sctx->send_progress)
2245 nce->need_later_update = 0;
2247 nce->need_later_update = 1;
2249 nce_ret = name_cache_insert(sctx, nce);
2252 name_cache_clean_unused(sctx);
2259 * Magic happens here. This function returns the first ref to an inode as it
2260 * would look like while receiving the stream at this point in time.
2261 * We walk the path up to the root. For every inode in between, we check if it
2262 * was already processed/sent. If yes, we continue with the parent as found
2263 * in send_root. If not, we continue with the parent as found in parent_root.
2264 * If we encounter an inode that was deleted at this point in time, we use the
2265 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2266 * that were not created yet and overwritten inodes/refs.
2268 * When do we have orphan inodes:
2269 * 1. When an inode is freshly created and thus no valid refs are available yet
2270 * 2. When a directory lost all it's refs (deleted) but still has dir items
2271 * inside which were not processed yet (pending for move/delete). If anyone
2272 * tried to get the path to the dir items, it would get a path inside that
2274 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2275 * of an unprocessed inode. If in that case the first ref would be
2276 * overwritten, the overwritten inode gets "orphanized". Later when we
2277 * process this overwritten inode, it is restored at a new place by moving
2280 * sctx->send_progress tells this function at which point in time receiving
2283 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2284 struct fs_path *dest)
2287 struct fs_path *name = NULL;
2288 u64 parent_inode = 0;
2292 name = fs_path_alloc();
2299 fs_path_reset(dest);
2301 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2302 struct waiting_dir_move *wdm;
2304 fs_path_reset(name);
2306 if (is_waiting_for_rm(sctx, ino, gen)) {
2307 ret = gen_unique_name(sctx, ino, gen, name);
2310 ret = fs_path_add_path(dest, name);
2314 wdm = get_waiting_dir_move(sctx, ino);
2315 if (wdm && wdm->orphanized) {
2316 ret = gen_unique_name(sctx, ino, gen, name);
2319 ret = get_first_ref(sctx->parent_root, ino,
2320 &parent_inode, &parent_gen, name);
2322 ret = __get_cur_name_and_parent(sctx, ino, gen,
2332 ret = fs_path_add_path(dest, name);
2343 fs_path_unreverse(dest);
2348 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2350 static int send_subvol_begin(struct send_ctx *sctx)
2353 struct btrfs_root *send_root = sctx->send_root;
2354 struct btrfs_root *parent_root = sctx->parent_root;
2355 struct btrfs_path *path;
2356 struct btrfs_key key;
2357 struct btrfs_root_ref *ref;
2358 struct extent_buffer *leaf;
2362 path = btrfs_alloc_path();
2366 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2368 btrfs_free_path(path);
2372 key.objectid = send_root->root_key.objectid;
2373 key.type = BTRFS_ROOT_BACKREF_KEY;
2376 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2385 leaf = path->nodes[0];
2386 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2387 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2388 key.objectid != send_root->root_key.objectid) {
2392 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2393 namelen = btrfs_root_ref_name_len(leaf, ref);
2394 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2395 btrfs_release_path(path);
2398 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2402 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2407 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2409 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2410 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2411 sctx->send_root->root_item.received_uuid);
2413 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2414 sctx->send_root->root_item.uuid);
2416 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2417 btrfs_root_ctransid(&sctx->send_root->root_item));
2419 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2420 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2421 parent_root->root_item.received_uuid);
2423 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2424 parent_root->root_item.uuid);
2425 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2426 btrfs_root_ctransid(&sctx->parent_root->root_item));
2429 ret = send_cmd(sctx);
2433 btrfs_free_path(path);
2438 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2440 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2444 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
2446 p = fs_path_alloc();
2450 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2454 ret = get_cur_path(sctx, ino, gen, p);
2457 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2458 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2460 ret = send_cmd(sctx);
2468 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2470 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2474 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
2476 p = fs_path_alloc();
2480 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2484 ret = get_cur_path(sctx, ino, gen, p);
2487 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2488 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2490 ret = send_cmd(sctx);
2498 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2500 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2504 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2507 p = fs_path_alloc();
2511 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2515 ret = get_cur_path(sctx, ino, gen, p);
2518 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2519 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2520 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2522 ret = send_cmd(sctx);
2530 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2532 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2534 struct fs_path *p = NULL;
2535 struct btrfs_inode_item *ii;
2536 struct btrfs_path *path = NULL;
2537 struct extent_buffer *eb;
2538 struct btrfs_key key;
2541 btrfs_debug(fs_info, "send_utimes %llu", ino);
2543 p = fs_path_alloc();
2547 path = alloc_path_for_send();
2554 key.type = BTRFS_INODE_ITEM_KEY;
2556 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2562 eb = path->nodes[0];
2563 slot = path->slots[0];
2564 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2566 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2570 ret = get_cur_path(sctx, ino, gen, p);
2573 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2574 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2575 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2576 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2577 /* TODO Add otime support when the otime patches get into upstream */
2579 ret = send_cmd(sctx);
2584 btrfs_free_path(path);
2589 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2590 * a valid path yet because we did not process the refs yet. So, the inode
2591 * is created as orphan.
2593 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2595 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2603 btrfs_debug(fs_info, "send_create_inode %llu", ino);
2605 p = fs_path_alloc();
2609 if (ino != sctx->cur_ino) {
2610 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2615 gen = sctx->cur_inode_gen;
2616 mode = sctx->cur_inode_mode;
2617 rdev = sctx->cur_inode_rdev;
2620 if (S_ISREG(mode)) {
2621 cmd = BTRFS_SEND_C_MKFILE;
2622 } else if (S_ISDIR(mode)) {
2623 cmd = BTRFS_SEND_C_MKDIR;
2624 } else if (S_ISLNK(mode)) {
2625 cmd = BTRFS_SEND_C_SYMLINK;
2626 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2627 cmd = BTRFS_SEND_C_MKNOD;
2628 } else if (S_ISFIFO(mode)) {
2629 cmd = BTRFS_SEND_C_MKFIFO;
2630 } else if (S_ISSOCK(mode)) {
2631 cmd = BTRFS_SEND_C_MKSOCK;
2633 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2634 (int)(mode & S_IFMT));
2639 ret = begin_cmd(sctx, cmd);
2643 ret = gen_unique_name(sctx, ino, gen, p);
2647 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2648 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2650 if (S_ISLNK(mode)) {
2652 ret = read_symlink(sctx->send_root, ino, p);
2655 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2656 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2657 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2658 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2659 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2662 ret = send_cmd(sctx);
2674 * We need some special handling for inodes that get processed before the parent
2675 * directory got created. See process_recorded_refs for details.
2676 * This function does the check if we already created the dir out of order.
2678 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2682 struct btrfs_path *path = NULL;
2683 struct btrfs_key key;
2684 struct btrfs_key found_key;
2685 struct btrfs_key di_key;
2686 struct btrfs_dir_item *di;
2688 path = alloc_path_for_send();
2693 key.type = BTRFS_DIR_INDEX_KEY;
2696 btrfs_for_each_slot(sctx->send_root, &key, &found_key, path, iter_ret) {
2697 struct extent_buffer *eb = path->nodes[0];
2699 if (found_key.objectid != key.objectid ||
2700 found_key.type != key.type) {
2705 di = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dir_item);
2706 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2708 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2709 di_key.objectid < sctx->send_progress) {
2714 /* Catch error found during iteration */
2718 btrfs_free_path(path);
2723 * Only creates the inode if it is:
2724 * 1. Not a directory
2725 * 2. Or a directory which was not created already due to out of order
2726 * directories. See did_create_dir and process_recorded_refs for details.
2728 static int send_create_inode_if_needed(struct send_ctx *sctx)
2732 if (S_ISDIR(sctx->cur_inode_mode)) {
2733 ret = did_create_dir(sctx, sctx->cur_ino);
2740 return send_create_inode(sctx, sctx->cur_ino);
2743 struct recorded_ref {
2744 struct list_head list;
2746 struct fs_path *full_path;
2752 static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2754 ref->full_path = path;
2755 ref->name = (char *)kbasename(ref->full_path->start);
2756 ref->name_len = ref->full_path->end - ref->name;
2760 * We need to process new refs before deleted refs, but compare_tree gives us
2761 * everything mixed. So we first record all refs and later process them.
2762 * This function is a helper to record one ref.
2764 static int __record_ref(struct list_head *head, u64 dir,
2765 u64 dir_gen, struct fs_path *path)
2767 struct recorded_ref *ref;
2769 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
2774 ref->dir_gen = dir_gen;
2775 set_ref_path(ref, path);
2776 list_add_tail(&ref->list, head);
2780 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2782 struct recorded_ref *new;
2784 new = kmalloc(sizeof(*ref), GFP_KERNEL);
2788 new->dir = ref->dir;
2789 new->dir_gen = ref->dir_gen;
2790 new->full_path = NULL;
2791 INIT_LIST_HEAD(&new->list);
2792 list_add_tail(&new->list, list);
2796 static void __free_recorded_refs(struct list_head *head)
2798 struct recorded_ref *cur;
2800 while (!list_empty(head)) {
2801 cur = list_entry(head->next, struct recorded_ref, list);
2802 fs_path_free(cur->full_path);
2803 list_del(&cur->list);
2808 static void free_recorded_refs(struct send_ctx *sctx)
2810 __free_recorded_refs(&sctx->new_refs);
2811 __free_recorded_refs(&sctx->deleted_refs);
2815 * Renames/moves a file/dir to its orphan name. Used when the first
2816 * ref of an unprocessed inode gets overwritten and for all non empty
2819 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2820 struct fs_path *path)
2823 struct fs_path *orphan;
2825 orphan = fs_path_alloc();
2829 ret = gen_unique_name(sctx, ino, gen, orphan);
2833 ret = send_rename(sctx, path, orphan);
2836 fs_path_free(orphan);
2840 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
2841 u64 dir_ino, u64 dir_gen)
2843 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2844 struct rb_node *parent = NULL;
2845 struct orphan_dir_info *entry, *odi;
2849 entry = rb_entry(parent, struct orphan_dir_info, node);
2850 if (dir_ino < entry->ino)
2852 else if (dir_ino > entry->ino)
2853 p = &(*p)->rb_right;
2854 else if (dir_gen < entry->gen)
2856 else if (dir_gen > entry->gen)
2857 p = &(*p)->rb_right;
2862 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2864 return ERR_PTR(-ENOMEM);
2867 odi->last_dir_index_offset = 0;
2869 rb_link_node(&odi->node, parent, p);
2870 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2874 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
2875 u64 dir_ino, u64 gen)
2877 struct rb_node *n = sctx->orphan_dirs.rb_node;
2878 struct orphan_dir_info *entry;
2881 entry = rb_entry(n, struct orphan_dir_info, node);
2882 if (dir_ino < entry->ino)
2884 else if (dir_ino > entry->ino)
2886 else if (gen < entry->gen)
2888 else if (gen > entry->gen)
2896 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
2898 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
2903 static void free_orphan_dir_info(struct send_ctx *sctx,
2904 struct orphan_dir_info *odi)
2908 rb_erase(&odi->node, &sctx->orphan_dirs);
2913 * Returns 1 if a directory can be removed at this point in time.
2914 * We check this by iterating all dir items and checking if the inode behind
2915 * the dir item was already processed.
2917 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2922 struct btrfs_root *root = sctx->parent_root;
2923 struct btrfs_path *path;
2924 struct btrfs_key key;
2925 struct btrfs_key found_key;
2926 struct btrfs_key loc;
2927 struct btrfs_dir_item *di;
2928 struct orphan_dir_info *odi = NULL;
2931 * Don't try to rmdir the top/root subvolume dir.
2933 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2936 path = alloc_path_for_send();
2941 key.type = BTRFS_DIR_INDEX_KEY;
2944 odi = get_orphan_dir_info(sctx, dir, dir_gen);
2946 key.offset = odi->last_dir_index_offset;
2948 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
2949 struct waiting_dir_move *dm;
2951 if (found_key.objectid != key.objectid ||
2952 found_key.type != key.type)
2955 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2956 struct btrfs_dir_item);
2957 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2959 dm = get_waiting_dir_move(sctx, loc.objectid);
2961 odi = add_orphan_dir_info(sctx, dir, dir_gen);
2967 odi->last_dir_index_offset = found_key.offset;
2968 dm->rmdir_ino = dir;
2969 dm->rmdir_gen = dir_gen;
2974 if (loc.objectid > send_progress) {
2975 odi = add_orphan_dir_info(sctx, dir, dir_gen);
2981 odi->last_dir_index_offset = found_key.offset;
2990 free_orphan_dir_info(sctx, odi);
2995 btrfs_free_path(path);
2999 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3001 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3003 return entry != NULL;
3006 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3008 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3009 struct rb_node *parent = NULL;
3010 struct waiting_dir_move *entry, *dm;
3012 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
3018 dm->orphanized = orphanized;
3022 entry = rb_entry(parent, struct waiting_dir_move, node);
3023 if (ino < entry->ino) {
3025 } else if (ino > entry->ino) {
3026 p = &(*p)->rb_right;
3033 rb_link_node(&dm->node, parent, p);
3034 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3038 static struct waiting_dir_move *
3039 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3041 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3042 struct waiting_dir_move *entry;
3045 entry = rb_entry(n, struct waiting_dir_move, node);
3046 if (ino < entry->ino)
3048 else if (ino > entry->ino)
3056 static void free_waiting_dir_move(struct send_ctx *sctx,
3057 struct waiting_dir_move *dm)
3061 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3065 static int add_pending_dir_move(struct send_ctx *sctx,
3069 struct list_head *new_refs,
3070 struct list_head *deleted_refs,
3071 const bool is_orphan)
3073 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3074 struct rb_node *parent = NULL;
3075 struct pending_dir_move *entry = NULL, *pm;
3076 struct recorded_ref *cur;
3080 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3083 pm->parent_ino = parent_ino;
3086 INIT_LIST_HEAD(&pm->list);
3087 INIT_LIST_HEAD(&pm->update_refs);
3088 RB_CLEAR_NODE(&pm->node);
3092 entry = rb_entry(parent, struct pending_dir_move, node);
3093 if (parent_ino < entry->parent_ino) {
3095 } else if (parent_ino > entry->parent_ino) {
3096 p = &(*p)->rb_right;
3103 list_for_each_entry(cur, deleted_refs, list) {
3104 ret = dup_ref(cur, &pm->update_refs);
3108 list_for_each_entry(cur, new_refs, list) {
3109 ret = dup_ref(cur, &pm->update_refs);
3114 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3119 list_add_tail(&pm->list, &entry->list);
3121 rb_link_node(&pm->node, parent, p);
3122 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3127 __free_recorded_refs(&pm->update_refs);
3133 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3136 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3137 struct pending_dir_move *entry;
3140 entry = rb_entry(n, struct pending_dir_move, node);
3141 if (parent_ino < entry->parent_ino)
3143 else if (parent_ino > entry->parent_ino)
3151 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3152 u64 ino, u64 gen, u64 *ancestor_ino)
3155 u64 parent_inode = 0;
3157 u64 start_ino = ino;
3160 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3161 fs_path_reset(name);
3163 if (is_waiting_for_rm(sctx, ino, gen))
3165 if (is_waiting_for_move(sctx, ino)) {
3166 if (*ancestor_ino == 0)
3167 *ancestor_ino = ino;
3168 ret = get_first_ref(sctx->parent_root, ino,
3169 &parent_inode, &parent_gen, name);
3171 ret = __get_cur_name_and_parent(sctx, ino, gen,
3181 if (parent_inode == start_ino) {
3183 if (*ancestor_ino == 0)
3184 *ancestor_ino = ino;
3193 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3195 struct fs_path *from_path = NULL;
3196 struct fs_path *to_path = NULL;
3197 struct fs_path *name = NULL;
3198 u64 orig_progress = sctx->send_progress;
3199 struct recorded_ref *cur;
3200 u64 parent_ino, parent_gen;
3201 struct waiting_dir_move *dm = NULL;
3208 name = fs_path_alloc();
3209 from_path = fs_path_alloc();
3210 if (!name || !from_path) {
3215 dm = get_waiting_dir_move(sctx, pm->ino);
3217 rmdir_ino = dm->rmdir_ino;
3218 rmdir_gen = dm->rmdir_gen;
3219 is_orphan = dm->orphanized;
3220 free_waiting_dir_move(sctx, dm);
3223 ret = gen_unique_name(sctx, pm->ino,
3224 pm->gen, from_path);
3226 ret = get_first_ref(sctx->parent_root, pm->ino,
3227 &parent_ino, &parent_gen, name);
3230 ret = get_cur_path(sctx, parent_ino, parent_gen,
3234 ret = fs_path_add_path(from_path, name);
3239 sctx->send_progress = sctx->cur_ino + 1;
3240 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3244 LIST_HEAD(deleted_refs);
3245 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3246 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3247 &pm->update_refs, &deleted_refs,
3252 dm = get_waiting_dir_move(sctx, pm->ino);
3254 dm->rmdir_ino = rmdir_ino;
3255 dm->rmdir_gen = rmdir_gen;
3259 fs_path_reset(name);
3262 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3266 ret = send_rename(sctx, from_path, to_path);
3271 struct orphan_dir_info *odi;
3274 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
3276 /* already deleted */
3281 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
3287 name = fs_path_alloc();
3292 ret = get_cur_path(sctx, rmdir_ino, gen, name);
3295 ret = send_rmdir(sctx, name);
3301 ret = send_utimes(sctx, pm->ino, pm->gen);
3306 * After rename/move, need to update the utimes of both new parent(s)
3307 * and old parent(s).
3309 list_for_each_entry(cur, &pm->update_refs, list) {
3311 * The parent inode might have been deleted in the send snapshot
3313 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3314 NULL, NULL, NULL, NULL, NULL);
3315 if (ret == -ENOENT) {
3322 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3329 fs_path_free(from_path);
3330 fs_path_free(to_path);
3331 sctx->send_progress = orig_progress;
3336 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3338 if (!list_empty(&m->list))
3340 if (!RB_EMPTY_NODE(&m->node))
3341 rb_erase(&m->node, &sctx->pending_dir_moves);
3342 __free_recorded_refs(&m->update_refs);
3346 static void tail_append_pending_moves(struct send_ctx *sctx,
3347 struct pending_dir_move *moves,
3348 struct list_head *stack)
3350 if (list_empty(&moves->list)) {
3351 list_add_tail(&moves->list, stack);
3354 list_splice_init(&moves->list, &list);
3355 list_add_tail(&moves->list, stack);
3356 list_splice_tail(&list, stack);
3358 if (!RB_EMPTY_NODE(&moves->node)) {
3359 rb_erase(&moves->node, &sctx->pending_dir_moves);
3360 RB_CLEAR_NODE(&moves->node);
3364 static int apply_children_dir_moves(struct send_ctx *sctx)
3366 struct pending_dir_move *pm;
3367 struct list_head stack;
3368 u64 parent_ino = sctx->cur_ino;
3371 pm = get_pending_dir_moves(sctx, parent_ino);
3375 INIT_LIST_HEAD(&stack);
3376 tail_append_pending_moves(sctx, pm, &stack);
3378 while (!list_empty(&stack)) {
3379 pm = list_first_entry(&stack, struct pending_dir_move, list);
3380 parent_ino = pm->ino;
3381 ret = apply_dir_move(sctx, pm);
3382 free_pending_move(sctx, pm);
3385 pm = get_pending_dir_moves(sctx, parent_ino);
3387 tail_append_pending_moves(sctx, pm, &stack);
3392 while (!list_empty(&stack)) {
3393 pm = list_first_entry(&stack, struct pending_dir_move, list);
3394 free_pending_move(sctx, pm);
3400 * We might need to delay a directory rename even when no ancestor directory
3401 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3402 * renamed. This happens when we rename a directory to the old name (the name
3403 * in the parent root) of some other unrelated directory that got its rename
3404 * delayed due to some ancestor with higher number that got renamed.
3410 * |---- a/ (ino 257)
3411 * | |---- file (ino 260)
3413 * |---- b/ (ino 258)
3414 * |---- c/ (ino 259)
3418 * |---- a/ (ino 258)
3419 * |---- x/ (ino 259)
3420 * |---- y/ (ino 257)
3421 * |----- file (ino 260)
3423 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3424 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3425 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3428 * 1 - rename 259 from 'c' to 'x'
3429 * 2 - rename 257 from 'a' to 'x/y'
3430 * 3 - rename 258 from 'b' to 'a'
3432 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3433 * be done right away and < 0 on error.
3435 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3436 struct recorded_ref *parent_ref,
3437 const bool is_orphan)
3439 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3440 struct btrfs_path *path;
3441 struct btrfs_key key;
3442 struct btrfs_key di_key;
3443 struct btrfs_dir_item *di;
3447 struct waiting_dir_move *wdm;
3449 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3452 path = alloc_path_for_send();
3456 key.objectid = parent_ref->dir;
3457 key.type = BTRFS_DIR_ITEM_KEY;
3458 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3460 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3463 } else if (ret > 0) {
3468 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3469 parent_ref->name_len);
3475 * di_key.objectid has the number of the inode that has a dentry in the
3476 * parent directory with the same name that sctx->cur_ino is being
3477 * renamed to. We need to check if that inode is in the send root as
3478 * well and if it is currently marked as an inode with a pending rename,
3479 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3480 * that it happens after that other inode is renamed.
3482 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3483 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3488 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3489 &left_gen, NULL, NULL, NULL, NULL);
3492 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3493 &right_gen, NULL, NULL, NULL, NULL);
3500 /* Different inode, no need to delay the rename of sctx->cur_ino */
3501 if (right_gen != left_gen) {
3506 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3507 if (wdm && !wdm->orphanized) {
3508 ret = add_pending_dir_move(sctx,
3510 sctx->cur_inode_gen,
3513 &sctx->deleted_refs,
3519 btrfs_free_path(path);
3524 * Check if inode ino2, or any of its ancestors, is inode ino1.
3525 * Return 1 if true, 0 if false and < 0 on error.
3527 static int check_ino_in_path(struct btrfs_root *root,
3532 struct fs_path *fs_path)
3537 return ino1_gen == ino2_gen;
3539 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3544 fs_path_reset(fs_path);
3545 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3549 return parent_gen == ino1_gen;
3556 * Check if inode ino1 is an ancestor of inode ino2 in the given root for any
3557 * possible path (in case ino2 is not a directory and has multiple hard links).
3558 * Return 1 if true, 0 if false and < 0 on error.
3560 static int is_ancestor(struct btrfs_root *root,
3564 struct fs_path *fs_path)
3566 bool free_fs_path = false;
3569 struct btrfs_path *path = NULL;
3570 struct btrfs_key key;
3573 fs_path = fs_path_alloc();
3576 free_fs_path = true;
3579 path = alloc_path_for_send();
3585 key.objectid = ino2;
3586 key.type = BTRFS_INODE_REF_KEY;
3589 btrfs_for_each_slot(root, &key, &key, path, iter_ret) {
3590 struct extent_buffer *leaf = path->nodes[0];
3591 int slot = path->slots[0];
3595 if (key.objectid != ino2)
3597 if (key.type != BTRFS_INODE_REF_KEY &&
3598 key.type != BTRFS_INODE_EXTREF_KEY)
3601 item_size = btrfs_item_size(leaf, slot);
3602 while (cur_offset < item_size) {
3606 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3608 struct btrfs_inode_extref *extref;
3610 ptr = btrfs_item_ptr_offset(leaf, slot);
3611 extref = (struct btrfs_inode_extref *)
3613 parent = btrfs_inode_extref_parent(leaf,
3615 cur_offset += sizeof(*extref);
3616 cur_offset += btrfs_inode_extref_name_len(leaf,
3619 parent = key.offset;
3620 cur_offset = item_size;
3623 ret = get_inode_info(root, parent, NULL, &parent_gen,
3624 NULL, NULL, NULL, NULL);
3627 ret = check_ino_in_path(root, ino1, ino1_gen,
3628 parent, parent_gen, fs_path);
3638 btrfs_free_path(path);
3640 fs_path_free(fs_path);
3644 static int wait_for_parent_move(struct send_ctx *sctx,
3645 struct recorded_ref *parent_ref,
3646 const bool is_orphan)
3649 u64 ino = parent_ref->dir;
3650 u64 ino_gen = parent_ref->dir_gen;
3651 u64 parent_ino_before, parent_ino_after;
3652 struct fs_path *path_before = NULL;
3653 struct fs_path *path_after = NULL;
3656 path_after = fs_path_alloc();
3657 path_before = fs_path_alloc();
3658 if (!path_after || !path_before) {
3664 * Our current directory inode may not yet be renamed/moved because some
3665 * ancestor (immediate or not) has to be renamed/moved first. So find if
3666 * such ancestor exists and make sure our own rename/move happens after
3667 * that ancestor is processed to avoid path build infinite loops (done
3668 * at get_cur_path()).
3670 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3671 u64 parent_ino_after_gen;
3673 if (is_waiting_for_move(sctx, ino)) {
3675 * If the current inode is an ancestor of ino in the
3676 * parent root, we need to delay the rename of the
3677 * current inode, otherwise don't delayed the rename
3678 * because we can end up with a circular dependency
3679 * of renames, resulting in some directories never
3680 * getting the respective rename operations issued in
3681 * the send stream or getting into infinite path build
3684 ret = is_ancestor(sctx->parent_root,
3685 sctx->cur_ino, sctx->cur_inode_gen,
3691 fs_path_reset(path_before);
3692 fs_path_reset(path_after);
3694 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3695 &parent_ino_after_gen, path_after);
3698 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3700 if (ret < 0 && ret != -ENOENT) {
3702 } else if (ret == -ENOENT) {
3707 len1 = fs_path_len(path_before);
3708 len2 = fs_path_len(path_after);
3709 if (ino > sctx->cur_ino &&
3710 (parent_ino_before != parent_ino_after || len1 != len2 ||
3711 memcmp(path_before->start, path_after->start, len1))) {
3714 ret = get_inode_info(sctx->parent_root, ino, NULL,
3715 &parent_ino_gen, NULL, NULL, NULL,
3719 if (ino_gen == parent_ino_gen) {
3724 ino = parent_ino_after;
3725 ino_gen = parent_ino_after_gen;
3729 fs_path_free(path_before);
3730 fs_path_free(path_after);
3733 ret = add_pending_dir_move(sctx,
3735 sctx->cur_inode_gen,
3738 &sctx->deleted_refs,
3747 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3750 struct fs_path *new_path;
3753 * Our reference's name member points to its full_path member string, so
3754 * we use here a new path.
3756 new_path = fs_path_alloc();
3760 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3762 fs_path_free(new_path);
3765 ret = fs_path_add(new_path, ref->name, ref->name_len);
3767 fs_path_free(new_path);
3771 fs_path_free(ref->full_path);
3772 set_ref_path(ref, new_path);
3778 * When processing the new references for an inode we may orphanize an existing
3779 * directory inode because its old name conflicts with one of the new references
3780 * of the current inode. Later, when processing another new reference of our
3781 * inode, we might need to orphanize another inode, but the path we have in the
3782 * reference reflects the pre-orphanization name of the directory we previously
3783 * orphanized. For example:
3785 * parent snapshot looks like:
3788 * |----- f1 (ino 257)
3789 * |----- f2 (ino 258)
3790 * |----- d1/ (ino 259)
3791 * |----- d2/ (ino 260)
3793 * send snapshot looks like:
3796 * |----- d1 (ino 258)
3797 * |----- f2/ (ino 259)
3798 * |----- f2_link/ (ino 260)
3799 * | |----- f1 (ino 257)
3801 * |----- d2 (ino 258)
3803 * When processing inode 257 we compute the name for inode 259 as "d1", and we
3804 * cache it in the name cache. Later when we start processing inode 258, when
3805 * collecting all its new references we set a full path of "d1/d2" for its new
3806 * reference with name "d2". When we start processing the new references we
3807 * start by processing the new reference with name "d1", and this results in
3808 * orphanizing inode 259, since its old reference causes a conflict. Then we
3809 * move on the next new reference, with name "d2", and we find out we must
3810 * orphanize inode 260, as its old reference conflicts with ours - but for the
3811 * orphanization we use a source path corresponding to the path we stored in the
3812 * new reference, which is "d1/d2" and not "o259-6-0/d2" - this makes the
3813 * receiver fail since the path component "d1/" no longer exists, it was renamed
3814 * to "o259-6-0/" when processing the previous new reference. So in this case we
3815 * must recompute the path in the new reference and use it for the new
3816 * orphanization operation.
3818 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3823 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3827 fs_path_reset(ref->full_path);
3828 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3832 ret = fs_path_add(ref->full_path, name, ref->name_len);
3836 /* Update the reference's base name pointer. */
3837 set_ref_path(ref, ref->full_path);
3844 * This does all the move/link/unlink/rmdir magic.
3846 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3848 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
3850 struct recorded_ref *cur;
3851 struct recorded_ref *cur2;
3852 struct list_head check_dirs;
3853 struct fs_path *valid_path = NULL;
3857 int did_overwrite = 0;
3859 u64 last_dir_ino_rm = 0;
3860 bool can_rename = true;
3861 bool orphanized_dir = false;
3862 bool orphanized_ancestor = false;
3864 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
3867 * This should never happen as the root dir always has the same ref
3868 * which is always '..'
3870 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3871 INIT_LIST_HEAD(&check_dirs);
3873 valid_path = fs_path_alloc();
3880 * First, check if the first ref of the current inode was overwritten
3881 * before. If yes, we know that the current inode was already orphanized
3882 * and thus use the orphan name. If not, we can use get_cur_path to
3883 * get the path of the first ref as it would like while receiving at
3884 * this point in time.
3885 * New inodes are always orphan at the beginning, so force to use the
3886 * orphan name in this case.
3887 * The first ref is stored in valid_path and will be updated if it
3888 * gets moved around.
3890 if (!sctx->cur_inode_new) {
3891 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3892 sctx->cur_inode_gen);
3898 if (sctx->cur_inode_new || did_overwrite) {
3899 ret = gen_unique_name(sctx, sctx->cur_ino,
3900 sctx->cur_inode_gen, valid_path);
3905 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3912 * Before doing any rename and link operations, do a first pass on the
3913 * new references to orphanize any unprocessed inodes that may have a
3914 * reference that conflicts with one of the new references of the current
3915 * inode. This needs to happen first because a new reference may conflict
3916 * with the old reference of a parent directory, so we must make sure
3917 * that the path used for link and rename commands don't use an
3918 * orphanized name when an ancestor was not yet orphanized.
3925 * |----- testdir/ (ino 259)
3926 * | |----- a (ino 257)
3928 * |----- b (ino 258)
3933 * |----- testdir_2/ (ino 259)
3934 * | |----- a (ino 260)
3936 * |----- testdir (ino 257)
3937 * |----- b (ino 257)
3938 * |----- b2 (ino 258)
3940 * Processing the new reference for inode 257 with name "b" may happen
3941 * before processing the new reference with name "testdir". If so, we
3942 * must make sure that by the time we send a link command to create the
3943 * hard link "b", inode 259 was already orphanized, since the generated
3944 * path in "valid_path" already contains the orphanized name for 259.
3945 * We are processing inode 257, so only later when processing 259 we do
3946 * the rename operation to change its temporary (orphanized) name to
3949 list_for_each_entry(cur, &sctx->new_refs, list) {
3950 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3953 if (ret == inode_state_will_create)
3957 * Check if this new ref would overwrite the first ref of another
3958 * unprocessed inode. If yes, orphanize the overwritten inode.
3959 * If we find an overwritten ref that is not the first ref,
3962 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3963 cur->name, cur->name_len,
3964 &ow_inode, &ow_gen, &ow_mode);
3968 ret = is_first_ref(sctx->parent_root,
3969 ow_inode, cur->dir, cur->name,
3974 struct name_cache_entry *nce;
3975 struct waiting_dir_move *wdm;
3977 if (orphanized_dir) {
3978 ret = refresh_ref_path(sctx, cur);
3983 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3987 if (S_ISDIR(ow_mode))
3988 orphanized_dir = true;
3991 * If ow_inode has its rename operation delayed
3992 * make sure that its orphanized name is used in
3993 * the source path when performing its rename
3996 if (is_waiting_for_move(sctx, ow_inode)) {
3997 wdm = get_waiting_dir_move(sctx,
4000 wdm->orphanized = true;
4004 * Make sure we clear our orphanized inode's
4005 * name from the name cache. This is because the
4006 * inode ow_inode might be an ancestor of some
4007 * other inode that will be orphanized as well
4008 * later and has an inode number greater than
4009 * sctx->send_progress. We need to prevent
4010 * future name lookups from using the old name
4011 * and get instead the orphan name.
4013 nce = name_cache_search(sctx, ow_inode, ow_gen);
4015 name_cache_delete(sctx, nce);
4020 * ow_inode might currently be an ancestor of
4021 * cur_ino, therefore compute valid_path (the
4022 * current path of cur_ino) again because it
4023 * might contain the pre-orphanization name of
4024 * ow_inode, which is no longer valid.
4026 ret = is_ancestor(sctx->parent_root,
4028 sctx->cur_ino, NULL);
4030 orphanized_ancestor = true;
4031 fs_path_reset(valid_path);
4032 ret = get_cur_path(sctx, sctx->cur_ino,
4033 sctx->cur_inode_gen,
4040 * If we previously orphanized a directory that
4041 * collided with a new reference that we already
4042 * processed, recompute the current path because
4043 * that directory may be part of the path.
4045 if (orphanized_dir) {
4046 ret = refresh_ref_path(sctx, cur);
4050 ret = send_unlink(sctx, cur->full_path);
4058 list_for_each_entry(cur, &sctx->new_refs, list) {
4060 * We may have refs where the parent directory does not exist
4061 * yet. This happens if the parent directories inum is higher
4062 * than the current inum. To handle this case, we create the
4063 * parent directory out of order. But we need to check if this
4064 * did already happen before due to other refs in the same dir.
4066 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4069 if (ret == inode_state_will_create) {
4072 * First check if any of the current inodes refs did
4073 * already create the dir.
4075 list_for_each_entry(cur2, &sctx->new_refs, list) {
4078 if (cur2->dir == cur->dir) {
4085 * If that did not happen, check if a previous inode
4086 * did already create the dir.
4089 ret = did_create_dir(sctx, cur->dir);
4093 ret = send_create_inode(sctx, cur->dir);
4099 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4100 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4109 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4111 ret = wait_for_parent_move(sctx, cur, is_orphan);
4121 * link/move the ref to the new place. If we have an orphan
4122 * inode, move it and update valid_path. If not, link or move
4123 * it depending on the inode mode.
4125 if (is_orphan && can_rename) {
4126 ret = send_rename(sctx, valid_path, cur->full_path);
4130 ret = fs_path_copy(valid_path, cur->full_path);
4133 } else if (can_rename) {
4134 if (S_ISDIR(sctx->cur_inode_mode)) {
4136 * Dirs can't be linked, so move it. For moved
4137 * dirs, we always have one new and one deleted
4138 * ref. The deleted ref is ignored later.
4140 ret = send_rename(sctx, valid_path,
4143 ret = fs_path_copy(valid_path,
4149 * We might have previously orphanized an inode
4150 * which is an ancestor of our current inode,
4151 * so our reference's full path, which was
4152 * computed before any such orphanizations, must
4155 if (orphanized_dir) {
4156 ret = update_ref_path(sctx, cur);
4160 ret = send_link(sctx, cur->full_path,
4166 ret = dup_ref(cur, &check_dirs);
4171 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4173 * Check if we can already rmdir the directory. If not,
4174 * orphanize it. For every dir item inside that gets deleted
4175 * later, we do this check again and rmdir it then if possible.
4176 * See the use of check_dirs for more details.
4178 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4183 ret = send_rmdir(sctx, valid_path);
4186 } else if (!is_orphan) {
4187 ret = orphanize_inode(sctx, sctx->cur_ino,
4188 sctx->cur_inode_gen, valid_path);
4194 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4195 ret = dup_ref(cur, &check_dirs);
4199 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4200 !list_empty(&sctx->deleted_refs)) {
4202 * We have a moved dir. Add the old parent to check_dirs
4204 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4206 ret = dup_ref(cur, &check_dirs);
4209 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4211 * We have a non dir inode. Go through all deleted refs and
4212 * unlink them if they were not already overwritten by other
4215 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4216 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4217 sctx->cur_ino, sctx->cur_inode_gen,
4218 cur->name, cur->name_len);
4223 * If we orphanized any ancestor before, we need
4224 * to recompute the full path for deleted names,
4225 * since any such path was computed before we
4226 * processed any references and orphanized any
4229 if (orphanized_ancestor) {
4230 ret = update_ref_path(sctx, cur);
4234 ret = send_unlink(sctx, cur->full_path);
4238 ret = dup_ref(cur, &check_dirs);
4243 * If the inode is still orphan, unlink the orphan. This may
4244 * happen when a previous inode did overwrite the first ref
4245 * of this inode and no new refs were added for the current
4246 * inode. Unlinking does not mean that the inode is deleted in
4247 * all cases. There may still be links to this inode in other
4251 ret = send_unlink(sctx, valid_path);
4258 * We did collect all parent dirs where cur_inode was once located. We
4259 * now go through all these dirs and check if they are pending for
4260 * deletion and if it's finally possible to perform the rmdir now.
4261 * We also update the inode stats of the parent dirs here.
4263 list_for_each_entry(cur, &check_dirs, list) {
4265 * In case we had refs into dirs that were not processed yet,
4266 * we don't need to do the utime and rmdir logic for these dirs.
4267 * The dir will be processed later.
4269 if (cur->dir > sctx->cur_ino)
4272 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4276 if (ret == inode_state_did_create ||
4277 ret == inode_state_no_change) {
4278 /* TODO delayed utimes */
4279 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
4282 } else if (ret == inode_state_did_delete &&
4283 cur->dir != last_dir_ino_rm) {
4284 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4289 ret = get_cur_path(sctx, cur->dir,
4290 cur->dir_gen, valid_path);
4293 ret = send_rmdir(sctx, valid_path);
4296 last_dir_ino_rm = cur->dir;
4304 __free_recorded_refs(&check_dirs);
4305 free_recorded_refs(sctx);
4306 fs_path_free(valid_path);
4310 static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4311 void *ctx, struct list_head *refs)
4314 struct send_ctx *sctx = ctx;
4318 p = fs_path_alloc();
4322 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
4327 ret = get_cur_path(sctx, dir, gen, p);
4330 ret = fs_path_add_path(p, name);
4334 ret = __record_ref(refs, dir, gen, p);
4342 static int __record_new_ref(int num, u64 dir, int index,
4343 struct fs_path *name,
4346 struct send_ctx *sctx = ctx;
4347 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
4351 static int __record_deleted_ref(int num, u64 dir, int index,
4352 struct fs_path *name,
4355 struct send_ctx *sctx = ctx;
4356 return record_ref(sctx->parent_root, dir, name, ctx,
4357 &sctx->deleted_refs);
4360 static int record_new_ref(struct send_ctx *sctx)
4364 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4365 sctx->cmp_key, 0, __record_new_ref, sctx);
4374 static int record_deleted_ref(struct send_ctx *sctx)
4378 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4379 sctx->cmp_key, 0, __record_deleted_ref, sctx);
4388 struct find_ref_ctx {
4391 struct btrfs_root *root;
4392 struct fs_path *name;
4396 static int __find_iref(int num, u64 dir, int index,
4397 struct fs_path *name,
4400 struct find_ref_ctx *ctx = ctx_;
4404 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4405 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
4407 * To avoid doing extra lookups we'll only do this if everything
4410 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4414 if (dir_gen != ctx->dir_gen)
4416 ctx->found_idx = num;
4422 static int find_iref(struct btrfs_root *root,
4423 struct btrfs_path *path,
4424 struct btrfs_key *key,
4425 u64 dir, u64 dir_gen, struct fs_path *name)
4428 struct find_ref_ctx ctx;
4432 ctx.dir_gen = dir_gen;
4436 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
4440 if (ctx.found_idx == -1)
4443 return ctx.found_idx;
4446 static int __record_changed_new_ref(int num, u64 dir, int index,
4447 struct fs_path *name,
4452 struct send_ctx *sctx = ctx;
4454 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4459 ret = find_iref(sctx->parent_root, sctx->right_path,
4460 sctx->cmp_key, dir, dir_gen, name);
4462 ret = __record_new_ref(num, dir, index, name, sctx);
4469 static int __record_changed_deleted_ref(int num, u64 dir, int index,
4470 struct fs_path *name,
4475 struct send_ctx *sctx = ctx;
4477 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4482 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
4483 dir, dir_gen, name);
4485 ret = __record_deleted_ref(num, dir, index, name, sctx);
4492 static int record_changed_ref(struct send_ctx *sctx)
4496 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4497 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4500 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4501 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4511 * Record and process all refs at once. Needed when an inode changes the
4512 * generation number, which means that it was deleted and recreated.
4514 static int process_all_refs(struct send_ctx *sctx,
4515 enum btrfs_compare_tree_result cmd)
4519 struct btrfs_root *root;
4520 struct btrfs_path *path;
4521 struct btrfs_key key;
4522 struct btrfs_key found_key;
4523 iterate_inode_ref_t cb;
4524 int pending_move = 0;
4526 path = alloc_path_for_send();
4530 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4531 root = sctx->send_root;
4532 cb = __record_new_ref;
4533 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4534 root = sctx->parent_root;
4535 cb = __record_deleted_ref;
4537 btrfs_err(sctx->send_root->fs_info,
4538 "Wrong command %d in process_all_refs", cmd);
4543 key.objectid = sctx->cmp_key->objectid;
4544 key.type = BTRFS_INODE_REF_KEY;
4546 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
4547 if (found_key.objectid != key.objectid ||
4548 (found_key.type != BTRFS_INODE_REF_KEY &&
4549 found_key.type != BTRFS_INODE_EXTREF_KEY))
4552 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4556 /* Catch error found during iteration */
4561 btrfs_release_path(path);
4564 * We don't actually care about pending_move as we are simply
4565 * re-creating this inode and will be rename'ing it into place once we
4566 * rename the parent directory.
4568 ret = process_recorded_refs(sctx, &pending_move);
4570 btrfs_free_path(path);
4574 static int send_set_xattr(struct send_ctx *sctx,
4575 struct fs_path *path,
4576 const char *name, int name_len,
4577 const char *data, int data_len)
4581 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4585 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4586 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4587 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4589 ret = send_cmd(sctx);
4596 static int send_remove_xattr(struct send_ctx *sctx,
4597 struct fs_path *path,
4598 const char *name, int name_len)
4602 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4606 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4607 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4609 ret = send_cmd(sctx);
4616 static int __process_new_xattr(int num, struct btrfs_key *di_key,
4617 const char *name, int name_len, const char *data,
4618 int data_len, void *ctx)
4621 struct send_ctx *sctx = ctx;
4623 struct posix_acl_xattr_header dummy_acl;
4625 /* Capabilities are emitted by finish_inode_if_needed */
4626 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4629 p = fs_path_alloc();
4634 * This hack is needed because empty acls are stored as zero byte
4635 * data in xattrs. Problem with that is, that receiving these zero byte
4636 * acls will fail later. To fix this, we send a dummy acl list that
4637 * only contains the version number and no entries.
4639 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4640 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4641 if (data_len == 0) {
4642 dummy_acl.a_version =
4643 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4644 data = (char *)&dummy_acl;
4645 data_len = sizeof(dummy_acl);
4649 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4653 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4660 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4661 const char *name, int name_len,
4662 const char *data, int data_len, void *ctx)
4665 struct send_ctx *sctx = ctx;
4668 p = fs_path_alloc();
4672 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4676 ret = send_remove_xattr(sctx, p, name, name_len);
4683 static int process_new_xattr(struct send_ctx *sctx)
4687 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4688 __process_new_xattr, sctx);
4693 static int process_deleted_xattr(struct send_ctx *sctx)
4695 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4696 __process_deleted_xattr, sctx);
4699 struct find_xattr_ctx {
4707 static int __find_xattr(int num, struct btrfs_key *di_key, const char *name,
4708 int name_len, const char *data, int data_len, void *vctx)
4710 struct find_xattr_ctx *ctx = vctx;
4712 if (name_len == ctx->name_len &&
4713 strncmp(name, ctx->name, name_len) == 0) {
4714 ctx->found_idx = num;
4715 ctx->found_data_len = data_len;
4716 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4717 if (!ctx->found_data)
4724 static int find_xattr(struct btrfs_root *root,
4725 struct btrfs_path *path,
4726 struct btrfs_key *key,
4727 const char *name, int name_len,
4728 char **data, int *data_len)
4731 struct find_xattr_ctx ctx;
4734 ctx.name_len = name_len;
4736 ctx.found_data = NULL;
4737 ctx.found_data_len = 0;
4739 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
4743 if (ctx.found_idx == -1)
4746 *data = ctx.found_data;
4747 *data_len = ctx.found_data_len;
4749 kfree(ctx.found_data);
4751 return ctx.found_idx;
4755 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4756 const char *name, int name_len,
4757 const char *data, int data_len,
4761 struct send_ctx *sctx = ctx;
4762 char *found_data = NULL;
4763 int found_data_len = 0;
4765 ret = find_xattr(sctx->parent_root, sctx->right_path,
4766 sctx->cmp_key, name, name_len, &found_data,
4768 if (ret == -ENOENT) {
4769 ret = __process_new_xattr(num, di_key, name, name_len, data,
4771 } else if (ret >= 0) {
4772 if (data_len != found_data_len ||
4773 memcmp(data, found_data, data_len)) {
4774 ret = __process_new_xattr(num, di_key, name, name_len,
4775 data, data_len, ctx);
4785 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4786 const char *name, int name_len,
4787 const char *data, int data_len,
4791 struct send_ctx *sctx = ctx;
4793 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4794 name, name_len, NULL, NULL);
4796 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4804 static int process_changed_xattr(struct send_ctx *sctx)
4808 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4809 __process_changed_new_xattr, sctx);
4812 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4813 __process_changed_deleted_xattr, sctx);
4819 static int process_all_new_xattrs(struct send_ctx *sctx)
4823 struct btrfs_root *root;
4824 struct btrfs_path *path;
4825 struct btrfs_key key;
4826 struct btrfs_key found_key;
4828 path = alloc_path_for_send();
4832 root = sctx->send_root;
4834 key.objectid = sctx->cmp_key->objectid;
4835 key.type = BTRFS_XATTR_ITEM_KEY;
4837 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
4838 if (found_key.objectid != key.objectid ||
4839 found_key.type != key.type) {
4844 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
4848 /* Catch error found during iteration */
4852 btrfs_free_path(path);
4856 static inline u64 max_send_read_size(const struct send_ctx *sctx)
4858 return sctx->send_max_size - SZ_16K;
4861 static int put_data_header(struct send_ctx *sctx, u32 len)
4863 struct btrfs_tlv_header *hdr;
4865 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
4867 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
4868 put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
4869 put_unaligned_le16(len, &hdr->tlv_len);
4870 sctx->send_size += sizeof(*hdr);
4874 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
4876 struct btrfs_root *root = sctx->send_root;
4877 struct btrfs_fs_info *fs_info = root->fs_info;
4879 pgoff_t index = offset >> PAGE_SHIFT;
4881 unsigned pg_offset = offset_in_page(offset);
4884 ret = put_data_header(sctx, len);
4888 last_index = (offset + len - 1) >> PAGE_SHIFT;
4890 while (index <= last_index) {
4891 unsigned cur_len = min_t(unsigned, len,
4892 PAGE_SIZE - pg_offset);
4894 page = find_lock_page(sctx->cur_inode->i_mapping, index);
4896 page_cache_sync_readahead(sctx->cur_inode->i_mapping,
4897 &sctx->ra, NULL, index,
4898 last_index + 1 - index);
4900 page = find_or_create_page(sctx->cur_inode->i_mapping,
4908 if (PageReadahead(page))
4909 page_cache_async_readahead(sctx->cur_inode->i_mapping,
4910 &sctx->ra, NULL, page_folio(page),
4911 index, last_index + 1 - index);
4913 if (!PageUptodate(page)) {
4914 btrfs_read_folio(NULL, page_folio(page));
4916 if (!PageUptodate(page)) {
4919 "send: IO error at offset %llu for inode %llu root %llu",
4920 page_offset(page), sctx->cur_ino,
4921 sctx->send_root->root_key.objectid);
4928 memcpy_from_page(sctx->send_buf + sctx->send_size, page,
4929 pg_offset, cur_len);
4935 sctx->send_size += cur_len;
4942 * Read some bytes from the current inode/file and send a write command to
4945 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4947 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
4951 p = fs_path_alloc();
4955 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
4957 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4961 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4965 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4966 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4967 ret = put_file_data(sctx, offset, len);
4971 ret = send_cmd(sctx);
4980 * Send a clone command to user space.
4982 static int send_clone(struct send_ctx *sctx,
4983 u64 offset, u32 len,
4984 struct clone_root *clone_root)
4990 btrfs_debug(sctx->send_root->fs_info,
4991 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4992 offset, len, clone_root->root->root_key.objectid,
4993 clone_root->ino, clone_root->offset);
4995 p = fs_path_alloc();
4999 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5003 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5007 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5008 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5009 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5011 if (clone_root->root == sctx->send_root) {
5012 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
5013 &gen, NULL, NULL, NULL, NULL);
5016 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5018 ret = get_inode_path(clone_root->root, clone_root->ino, p);
5024 * If the parent we're using has a received_uuid set then use that as
5025 * our clone source as that is what we will look for when doing a
5028 * This covers the case that we create a snapshot off of a received
5029 * subvolume and then use that as the parent and try to receive on a
5032 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5033 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5034 clone_root->root->root_item.received_uuid);
5036 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5037 clone_root->root->root_item.uuid);
5038 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
5039 btrfs_root_ctransid(&clone_root->root->root_item));
5040 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5041 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5042 clone_root->offset);
5044 ret = send_cmd(sctx);
5053 * Send an update extent command to user space.
5055 static int send_update_extent(struct send_ctx *sctx,
5056 u64 offset, u32 len)
5061 p = fs_path_alloc();
5065 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5069 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5073 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5074 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5075 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5077 ret = send_cmd(sctx);
5085 static int send_hole(struct send_ctx *sctx, u64 end)
5087 struct fs_path *p = NULL;
5088 u64 read_size = max_send_read_size(sctx);
5089 u64 offset = sctx->cur_inode_last_extent;
5093 * A hole that starts at EOF or beyond it. Since we do not yet support
5094 * fallocate (for extent preallocation and hole punching), sending a
5095 * write of zeroes starting at EOF or beyond would later require issuing
5096 * a truncate operation which would undo the write and achieve nothing.
5098 if (offset >= sctx->cur_inode_size)
5102 * Don't go beyond the inode's i_size due to prealloc extents that start
5105 end = min_t(u64, end, sctx->cur_inode_size);
5107 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5108 return send_update_extent(sctx, offset, end - offset);
5110 p = fs_path_alloc();
5113 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5115 goto tlv_put_failure;
5116 while (offset < end) {
5117 u64 len = min(end - offset, read_size);
5119 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5122 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5123 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5124 ret = put_data_header(sctx, len);
5127 memset(sctx->send_buf + sctx->send_size, 0, len);
5128 sctx->send_size += len;
5129 ret = send_cmd(sctx);
5134 sctx->cur_inode_next_write_offset = offset;
5140 static int send_extent_data(struct send_ctx *sctx,
5144 const u64 end = offset + len;
5145 u64 read_size = max_send_read_size(sctx);
5148 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5149 return send_update_extent(sctx, offset, len);
5151 if (sctx->cur_inode == NULL) {
5152 struct btrfs_root *root = sctx->send_root;
5154 sctx->cur_inode = btrfs_iget(root->fs_info->sb, sctx->cur_ino, root);
5155 if (IS_ERR(sctx->cur_inode)) {
5156 int err = PTR_ERR(sctx->cur_inode);
5158 sctx->cur_inode = NULL;
5161 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
5162 file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping);
5165 * It's very likely there are no pages from this inode in the page
5166 * cache, so after reading extents and sending their data, we clean
5167 * the page cache to avoid trashing the page cache (adding pressure
5168 * to the page cache and forcing eviction of other data more useful
5169 * for applications).
5171 * We decide if we should clean the page cache simply by checking
5172 * if the inode's mapping nrpages is 0 when we first open it, and
5173 * not by using something like filemap_range_has_page() before
5174 * reading an extent because when we ask the readahead code to
5175 * read a given file range, it may (and almost always does) read
5176 * pages from beyond that range (see the documentation for
5177 * page_cache_sync_readahead()), so it would not be reliable,
5178 * because after reading the first extent future calls to
5179 * filemap_range_has_page() would return true because the readahead
5180 * on the previous extent resulted in reading pages of the current
5183 sctx->clean_page_cache = (sctx->cur_inode->i_mapping->nrpages == 0);
5184 sctx->page_cache_clear_start = round_down(offset, PAGE_SIZE);
5187 while (sent < len) {
5188 u64 size = min(len - sent, read_size);
5191 ret = send_write(sctx, offset + sent, size);
5197 if (sctx->clean_page_cache && IS_ALIGNED(end, PAGE_SIZE)) {
5199 * Always operate only on ranges that are a multiple of the page
5200 * size. This is not only to prevent zeroing parts of a page in
5201 * the case of subpage sector size, but also to guarantee we evict
5202 * pages, as passing a range that is smaller than page size does
5203 * not evict the respective page (only zeroes part of its content).
5205 * Always start from the end offset of the last range cleared.
5206 * This is because the readahead code may (and very often does)
5207 * reads pages beyond the range we request for readahead. So if
5208 * we have an extent layout like this:
5210 * [ extent A ] [ extent B ] [ extent C ]
5212 * When we ask page_cache_sync_readahead() to read extent A, it
5213 * may also trigger reads for pages of extent B. If we are doing
5214 * an incremental send and extent B has not changed between the
5215 * parent and send snapshots, some or all of its pages may end
5216 * up being read and placed in the page cache. So when truncating
5217 * the page cache we always start from the end offset of the
5218 * previously processed extent up to the end of the current
5221 truncate_inode_pages_range(&sctx->cur_inode->i_data,
5222 sctx->page_cache_clear_start,
5224 sctx->page_cache_clear_start = end;
5231 * Search for a capability xattr related to sctx->cur_ino. If the capability is
5232 * found, call send_set_xattr function to emit it.
5234 * Return 0 if there isn't a capability, or when the capability was emitted
5235 * successfully, or < 0 if an error occurred.
5237 static int send_capabilities(struct send_ctx *sctx)
5239 struct fs_path *fspath = NULL;
5240 struct btrfs_path *path;
5241 struct btrfs_dir_item *di;
5242 struct extent_buffer *leaf;
5243 unsigned long data_ptr;
5248 path = alloc_path_for_send();
5252 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5253 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5255 /* There is no xattr for this inode */
5257 } else if (IS_ERR(di)) {
5262 leaf = path->nodes[0];
5263 buf_len = btrfs_dir_data_len(leaf, di);
5265 fspath = fs_path_alloc();
5266 buf = kmalloc(buf_len, GFP_KERNEL);
5267 if (!fspath || !buf) {
5272 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5276 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5277 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5279 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5280 strlen(XATTR_NAME_CAPS), buf, buf_len);
5283 fs_path_free(fspath);
5284 btrfs_free_path(path);
5288 static int clone_range(struct send_ctx *sctx,
5289 struct clone_root *clone_root,
5290 const u64 disk_byte,
5295 struct btrfs_path *path;
5296 struct btrfs_key key;
5298 u64 clone_src_i_size = 0;
5301 * Prevent cloning from a zero offset with a length matching the sector
5302 * size because in some scenarios this will make the receiver fail.
5304 * For example, if in the source filesystem the extent at offset 0
5305 * has a length of sectorsize and it was written using direct IO, then
5306 * it can never be an inline extent (even if compression is enabled).
5307 * Then this extent can be cloned in the original filesystem to a non
5308 * zero file offset, but it may not be possible to clone in the
5309 * destination filesystem because it can be inlined due to compression
5310 * on the destination filesystem (as the receiver's write operations are
5311 * always done using buffered IO). The same happens when the original
5312 * filesystem does not have compression enabled but the destination
5315 if (clone_root->offset == 0 &&
5316 len == sctx->send_root->fs_info->sectorsize)
5317 return send_extent_data(sctx, offset, len);
5319 path = alloc_path_for_send();
5324 * There are inodes that have extents that lie behind its i_size. Don't
5325 * accept clones from these extents.
5327 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5328 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL);
5329 btrfs_release_path(path);
5334 * We can't send a clone operation for the entire range if we find
5335 * extent items in the respective range in the source file that
5336 * refer to different extents or if we find holes.
5337 * So check for that and do a mix of clone and regular write/copy
5338 * operations if needed.
5342 * mkfs.btrfs -f /dev/sda
5343 * mount /dev/sda /mnt
5344 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5345 * cp --reflink=always /mnt/foo /mnt/bar
5346 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5347 * btrfs subvolume snapshot -r /mnt /mnt/snap
5349 * If when we send the snapshot and we are processing file bar (which
5350 * has a higher inode number than foo) we blindly send a clone operation
5351 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5352 * a file bar that matches the content of file foo - iow, doesn't match
5353 * the content from bar in the original filesystem.
5355 key.objectid = clone_root->ino;
5356 key.type = BTRFS_EXTENT_DATA_KEY;
5357 key.offset = clone_root->offset;
5358 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5361 if (ret > 0 && path->slots[0] > 0) {
5362 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5363 if (key.objectid == clone_root->ino &&
5364 key.type == BTRFS_EXTENT_DATA_KEY)
5369 struct extent_buffer *leaf = path->nodes[0];
5370 int slot = path->slots[0];
5371 struct btrfs_file_extent_item *ei;
5375 u64 clone_data_offset;
5377 if (slot >= btrfs_header_nritems(leaf)) {
5378 ret = btrfs_next_leaf(clone_root->root, path);
5386 btrfs_item_key_to_cpu(leaf, &key, slot);
5389 * We might have an implicit trailing hole (NO_HOLES feature
5390 * enabled). We deal with it after leaving this loop.
5392 if (key.objectid != clone_root->ino ||
5393 key.type != BTRFS_EXTENT_DATA_KEY)
5396 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5397 type = btrfs_file_extent_type(leaf, ei);
5398 if (type == BTRFS_FILE_EXTENT_INLINE) {
5399 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
5400 ext_len = PAGE_ALIGN(ext_len);
5402 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5405 if (key.offset + ext_len <= clone_root->offset)
5408 if (key.offset > clone_root->offset) {
5409 /* Implicit hole, NO_HOLES feature enabled. */
5410 u64 hole_len = key.offset - clone_root->offset;
5414 ret = send_extent_data(sctx, offset, hole_len);
5422 clone_root->offset += hole_len;
5423 data_offset += hole_len;
5426 if (key.offset >= clone_root->offset + len)
5429 if (key.offset >= clone_src_i_size)
5432 if (key.offset + ext_len > clone_src_i_size)
5433 ext_len = clone_src_i_size - key.offset;
5435 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5436 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5437 clone_root->offset = key.offset;
5438 if (clone_data_offset < data_offset &&
5439 clone_data_offset + ext_len > data_offset) {
5442 extent_offset = data_offset - clone_data_offset;
5443 ext_len -= extent_offset;
5444 clone_data_offset += extent_offset;
5445 clone_root->offset += extent_offset;
5449 clone_len = min_t(u64, ext_len, len);
5451 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5452 clone_data_offset == data_offset) {
5453 const u64 src_end = clone_root->offset + clone_len;
5454 const u64 sectorsize = SZ_64K;
5457 * We can't clone the last block, when its size is not
5458 * sector size aligned, into the middle of a file. If we
5459 * do so, the receiver will get a failure (-EINVAL) when
5460 * trying to clone or will silently corrupt the data in
5461 * the destination file if it's on a kernel without the
5462 * fix introduced by commit ac765f83f1397646
5463 * ("Btrfs: fix data corruption due to cloning of eof
5466 * So issue a clone of the aligned down range plus a
5467 * regular write for the eof block, if we hit that case.
5469 * Also, we use the maximum possible sector size, 64K,
5470 * because we don't know what's the sector size of the
5471 * filesystem that receives the stream, so we have to
5472 * assume the largest possible sector size.
5474 if (src_end == clone_src_i_size &&
5475 !IS_ALIGNED(src_end, sectorsize) &&
5476 offset + clone_len < sctx->cur_inode_size) {
5479 slen = ALIGN_DOWN(src_end - clone_root->offset,
5482 ret = send_clone(sctx, offset, slen,
5487 ret = send_extent_data(sctx, offset + slen,
5490 ret = send_clone(sctx, offset, clone_len,
5494 ret = send_extent_data(sctx, offset, clone_len);
5503 offset += clone_len;
5504 clone_root->offset += clone_len;
5507 * If we are cloning from the file we are currently processing,
5508 * and using the send root as the clone root, we must stop once
5509 * the current clone offset reaches the current eof of the file
5510 * at the receiver, otherwise we would issue an invalid clone
5511 * operation (source range going beyond eof) and cause the
5512 * receiver to fail. So if we reach the current eof, bail out
5513 * and fallback to a regular write.
5515 if (clone_root->root == sctx->send_root &&
5516 clone_root->ino == sctx->cur_ino &&
5517 clone_root->offset >= sctx->cur_inode_next_write_offset)
5520 data_offset += clone_len;
5526 ret = send_extent_data(sctx, offset, len);
5530 btrfs_free_path(path);
5534 static int send_write_or_clone(struct send_ctx *sctx,
5535 struct btrfs_path *path,
5536 struct btrfs_key *key,
5537 struct clone_root *clone_root)
5540 u64 offset = key->offset;
5542 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
5544 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
5548 if (clone_root && IS_ALIGNED(end, bs)) {
5549 struct btrfs_file_extent_item *ei;
5553 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5554 struct btrfs_file_extent_item);
5555 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5556 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5557 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5558 offset, end - offset);
5560 ret = send_extent_data(sctx, offset, end - offset);
5562 sctx->cur_inode_next_write_offset = end;
5566 static int is_extent_unchanged(struct send_ctx *sctx,
5567 struct btrfs_path *left_path,
5568 struct btrfs_key *ekey)
5571 struct btrfs_key key;
5572 struct btrfs_path *path = NULL;
5573 struct extent_buffer *eb;
5575 struct btrfs_key found_key;
5576 struct btrfs_file_extent_item *ei;
5581 u64 left_offset_fixed;
5589 path = alloc_path_for_send();
5593 eb = left_path->nodes[0];
5594 slot = left_path->slots[0];
5595 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5596 left_type = btrfs_file_extent_type(eb, ei);
5598 if (left_type != BTRFS_FILE_EXTENT_REG) {
5602 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5603 left_len = btrfs_file_extent_num_bytes(eb, ei);
5604 left_offset = btrfs_file_extent_offset(eb, ei);
5605 left_gen = btrfs_file_extent_generation(eb, ei);
5608 * Following comments will refer to these graphics. L is the left
5609 * extents which we are checking at the moment. 1-8 are the right
5610 * extents that we iterate.
5613 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5616 * |--1--|-2b-|...(same as above)
5618 * Alternative situation. Happens on files where extents got split.
5620 * |-----------7-----------|-6-|
5622 * Alternative situation. Happens on files which got larger.
5625 * Nothing follows after 8.
5628 key.objectid = ekey->objectid;
5629 key.type = BTRFS_EXTENT_DATA_KEY;
5630 key.offset = ekey->offset;
5631 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5640 * Handle special case where the right side has no extents at all.
5642 eb = path->nodes[0];
5643 slot = path->slots[0];
5644 btrfs_item_key_to_cpu(eb, &found_key, slot);
5645 if (found_key.objectid != key.objectid ||
5646 found_key.type != key.type) {
5647 /* If we're a hole then just pretend nothing changed */
5648 ret = (left_disknr) ? 0 : 1;
5653 * We're now on 2a, 2b or 7.
5656 while (key.offset < ekey->offset + left_len) {
5657 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5658 right_type = btrfs_file_extent_type(eb, ei);
5659 if (right_type != BTRFS_FILE_EXTENT_REG &&
5660 right_type != BTRFS_FILE_EXTENT_INLINE) {
5665 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5666 right_len = btrfs_file_extent_ram_bytes(eb, ei);
5667 right_len = PAGE_ALIGN(right_len);
5669 right_len = btrfs_file_extent_num_bytes(eb, ei);
5673 * Are we at extent 8? If yes, we know the extent is changed.
5674 * This may only happen on the first iteration.
5676 if (found_key.offset + right_len <= ekey->offset) {
5677 /* If we're a hole just pretend nothing changed */
5678 ret = (left_disknr) ? 0 : 1;
5683 * We just wanted to see if when we have an inline extent, what
5684 * follows it is a regular extent (wanted to check the above
5685 * condition for inline extents too). This should normally not
5686 * happen but it's possible for example when we have an inline
5687 * compressed extent representing data with a size matching
5688 * the page size (currently the same as sector size).
5690 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5695 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5696 right_offset = btrfs_file_extent_offset(eb, ei);
5697 right_gen = btrfs_file_extent_generation(eb, ei);
5699 left_offset_fixed = left_offset;
5700 if (key.offset < ekey->offset) {
5701 /* Fix the right offset for 2a and 7. */
5702 right_offset += ekey->offset - key.offset;
5704 /* Fix the left offset for all behind 2a and 2b */
5705 left_offset_fixed += key.offset - ekey->offset;
5709 * Check if we have the same extent.
5711 if (left_disknr != right_disknr ||
5712 left_offset_fixed != right_offset ||
5713 left_gen != right_gen) {
5719 * Go to the next extent.
5721 ret = btrfs_next_item(sctx->parent_root, path);
5725 eb = path->nodes[0];
5726 slot = path->slots[0];
5727 btrfs_item_key_to_cpu(eb, &found_key, slot);
5729 if (ret || found_key.objectid != key.objectid ||
5730 found_key.type != key.type) {
5731 key.offset += right_len;
5734 if (found_key.offset != key.offset + right_len) {
5742 * We're now behind the left extent (treat as unchanged) or at the end
5743 * of the right side (treat as changed).
5745 if (key.offset >= ekey->offset + left_len)
5752 btrfs_free_path(path);
5756 static int get_last_extent(struct send_ctx *sctx, u64 offset)
5758 struct btrfs_path *path;
5759 struct btrfs_root *root = sctx->send_root;
5760 struct btrfs_key key;
5763 path = alloc_path_for_send();
5767 sctx->cur_inode_last_extent = 0;
5769 key.objectid = sctx->cur_ino;
5770 key.type = BTRFS_EXTENT_DATA_KEY;
5771 key.offset = offset;
5772 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5776 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5777 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5780 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
5782 btrfs_free_path(path);
5786 static int range_is_hole_in_parent(struct send_ctx *sctx,
5790 struct btrfs_path *path;
5791 struct btrfs_key key;
5792 struct btrfs_root *root = sctx->parent_root;
5793 u64 search_start = start;
5796 path = alloc_path_for_send();
5800 key.objectid = sctx->cur_ino;
5801 key.type = BTRFS_EXTENT_DATA_KEY;
5802 key.offset = search_start;
5803 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5806 if (ret > 0 && path->slots[0] > 0)
5809 while (search_start < end) {
5810 struct extent_buffer *leaf = path->nodes[0];
5811 int slot = path->slots[0];
5812 struct btrfs_file_extent_item *fi;
5815 if (slot >= btrfs_header_nritems(leaf)) {
5816 ret = btrfs_next_leaf(root, path);
5824 btrfs_item_key_to_cpu(leaf, &key, slot);
5825 if (key.objectid < sctx->cur_ino ||
5826 key.type < BTRFS_EXTENT_DATA_KEY)
5828 if (key.objectid > sctx->cur_ino ||
5829 key.type > BTRFS_EXTENT_DATA_KEY ||
5833 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5834 extent_end = btrfs_file_extent_end(path);
5835 if (extent_end <= start)
5837 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5838 search_start = extent_end;
5848 btrfs_free_path(path);
5852 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5853 struct btrfs_key *key)
5857 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5860 if (sctx->cur_inode_last_extent == (u64)-1) {
5861 ret = get_last_extent(sctx, key->offset - 1);
5866 if (path->slots[0] == 0 &&
5867 sctx->cur_inode_last_extent < key->offset) {
5869 * We might have skipped entire leafs that contained only
5870 * file extent items for our current inode. These leafs have
5871 * a generation number smaller (older) than the one in the
5872 * current leaf and the leaf our last extent came from, and
5873 * are located between these 2 leafs.
5875 ret = get_last_extent(sctx, key->offset - 1);
5880 if (sctx->cur_inode_last_extent < key->offset) {
5881 ret = range_is_hole_in_parent(sctx,
5882 sctx->cur_inode_last_extent,
5887 ret = send_hole(sctx, key->offset);
5891 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
5895 static int process_extent(struct send_ctx *sctx,
5896 struct btrfs_path *path,
5897 struct btrfs_key *key)
5899 struct clone_root *found_clone = NULL;
5902 if (S_ISLNK(sctx->cur_inode_mode))
5905 if (sctx->parent_root && !sctx->cur_inode_new) {
5906 ret = is_extent_unchanged(sctx, path, key);
5914 struct btrfs_file_extent_item *ei;
5917 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5918 struct btrfs_file_extent_item);
5919 type = btrfs_file_extent_type(path->nodes[0], ei);
5920 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5921 type == BTRFS_FILE_EXTENT_REG) {
5923 * The send spec does not have a prealloc command yet,
5924 * so just leave a hole for prealloc'ed extents until
5925 * we have enough commands queued up to justify rev'ing
5928 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5933 /* Have a hole, just skip it. */
5934 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5941 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5942 sctx->cur_inode_size, &found_clone);
5943 if (ret != -ENOENT && ret < 0)
5946 ret = send_write_or_clone(sctx, path, key, found_clone);
5950 ret = maybe_send_hole(sctx, path, key);
5955 static int process_all_extents(struct send_ctx *sctx)
5959 struct btrfs_root *root;
5960 struct btrfs_path *path;
5961 struct btrfs_key key;
5962 struct btrfs_key found_key;
5964 root = sctx->send_root;
5965 path = alloc_path_for_send();
5969 key.objectid = sctx->cmp_key->objectid;
5970 key.type = BTRFS_EXTENT_DATA_KEY;
5972 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
5973 if (found_key.objectid != key.objectid ||
5974 found_key.type != key.type) {
5979 ret = process_extent(sctx, path, &found_key);
5983 /* Catch error found during iteration */
5987 btrfs_free_path(path);
5991 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5993 int *refs_processed)
5997 if (sctx->cur_ino == 0)
5999 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
6000 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
6002 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
6005 ret = process_recorded_refs(sctx, pending_move);
6009 *refs_processed = 1;
6014 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
6025 int need_truncate = 1;
6026 int pending_move = 0;
6027 int refs_processed = 0;
6029 if (sctx->ignore_cur_inode)
6032 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6038 * We have processed the refs and thus need to advance send_progress.
6039 * Now, calls to get_cur_xxx will take the updated refs of the current
6040 * inode into account.
6042 * On the other hand, if our current inode is a directory and couldn't
6043 * be moved/renamed because its parent was renamed/moved too and it has
6044 * a higher inode number, we can only move/rename our current inode
6045 * after we moved/renamed its parent. Therefore in this case operate on
6046 * the old path (pre move/rename) of our current inode, and the
6047 * move/rename will be performed later.
6049 if (refs_processed && !pending_move)
6050 sctx->send_progress = sctx->cur_ino + 1;
6052 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6054 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6057 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
6058 &left_mode, &left_uid, &left_gid, NULL);
6062 if (!sctx->parent_root || sctx->cur_inode_new) {
6064 if (!S_ISLNK(sctx->cur_inode_mode))
6066 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6071 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
6072 &old_size, NULL, &right_mode, &right_uid,
6077 if (left_uid != right_uid || left_gid != right_gid)
6079 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6081 if ((old_size == sctx->cur_inode_size) ||
6082 (sctx->cur_inode_size > old_size &&
6083 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6087 if (S_ISREG(sctx->cur_inode_mode)) {
6088 if (need_send_hole(sctx)) {
6089 if (sctx->cur_inode_last_extent == (u64)-1 ||
6090 sctx->cur_inode_last_extent <
6091 sctx->cur_inode_size) {
6092 ret = get_last_extent(sctx, (u64)-1);
6096 if (sctx->cur_inode_last_extent <
6097 sctx->cur_inode_size) {
6098 ret = send_hole(sctx, sctx->cur_inode_size);
6103 if (need_truncate) {
6104 ret = send_truncate(sctx, sctx->cur_ino,
6105 sctx->cur_inode_gen,
6106 sctx->cur_inode_size);
6113 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6114 left_uid, left_gid);
6119 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6125 ret = send_capabilities(sctx);
6130 * If other directory inodes depended on our current directory
6131 * inode's move/rename, now do their move/rename operations.
6133 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6134 ret = apply_children_dir_moves(sctx);
6138 * Need to send that every time, no matter if it actually
6139 * changed between the two trees as we have done changes to
6140 * the inode before. If our inode is a directory and it's
6141 * waiting to be moved/renamed, we will send its utimes when
6142 * it's moved/renamed, therefore we don't need to do it here.
6144 sctx->send_progress = sctx->cur_ino + 1;
6145 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6154 struct parent_paths_ctx {
6155 struct list_head *refs;
6156 struct send_ctx *sctx;
6159 static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6162 struct parent_paths_ctx *ppctx = ctx;
6164 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
6169 * Issue unlink operations for all paths of the current inode found in the
6172 static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6174 LIST_HEAD(deleted_refs);
6175 struct btrfs_path *path;
6176 struct btrfs_root *root = sctx->parent_root;
6177 struct btrfs_key key;
6178 struct btrfs_key found_key;
6179 struct parent_paths_ctx ctx;
6183 path = alloc_path_for_send();
6187 key.objectid = sctx->cur_ino;
6188 key.type = BTRFS_INODE_REF_KEY;
6191 ctx.refs = &deleted_refs;
6194 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
6195 if (found_key.objectid != key.objectid)
6197 if (found_key.type != key.type &&
6198 found_key.type != BTRFS_INODE_EXTREF_KEY)
6201 ret = iterate_inode_ref(root, path, &found_key, 1,
6202 record_parent_ref, &ctx);
6206 /* Catch error found during iteration */
6212 while (!list_empty(&deleted_refs)) {
6213 struct recorded_ref *ref;
6215 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6216 ret = send_unlink(sctx, ref->full_path);
6219 fs_path_free(ref->full_path);
6220 list_del(&ref->list);
6225 btrfs_free_path(path);
6227 __free_recorded_refs(&deleted_refs);
6231 static void close_current_inode(struct send_ctx *sctx)
6235 if (sctx->cur_inode == NULL)
6238 i_size = i_size_read(sctx->cur_inode);
6241 * If we are doing an incremental send, we may have extents between the
6242 * last processed extent and the i_size that have not been processed
6243 * because they haven't changed but we may have read some of their pages
6244 * through readahead, see the comments at send_extent_data().
6246 if (sctx->clean_page_cache && sctx->page_cache_clear_start < i_size)
6247 truncate_inode_pages_range(&sctx->cur_inode->i_data,
6248 sctx->page_cache_clear_start,
6249 round_up(i_size, PAGE_SIZE) - 1);
6251 iput(sctx->cur_inode);
6252 sctx->cur_inode = NULL;
6255 static int changed_inode(struct send_ctx *sctx,
6256 enum btrfs_compare_tree_result result)
6259 struct btrfs_key *key = sctx->cmp_key;
6260 struct btrfs_inode_item *left_ii = NULL;
6261 struct btrfs_inode_item *right_ii = NULL;
6265 close_current_inode(sctx);
6267 sctx->cur_ino = key->objectid;
6268 sctx->cur_inode_new_gen = 0;
6269 sctx->cur_inode_last_extent = (u64)-1;
6270 sctx->cur_inode_next_write_offset = 0;
6271 sctx->ignore_cur_inode = false;
6274 * Set send_progress to current inode. This will tell all get_cur_xxx
6275 * functions that the current inode's refs are not updated yet. Later,
6276 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6278 sctx->send_progress = sctx->cur_ino;
6280 if (result == BTRFS_COMPARE_TREE_NEW ||
6281 result == BTRFS_COMPARE_TREE_CHANGED) {
6282 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6283 sctx->left_path->slots[0],
6284 struct btrfs_inode_item);
6285 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6288 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6289 sctx->right_path->slots[0],
6290 struct btrfs_inode_item);
6291 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6294 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6295 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6296 sctx->right_path->slots[0],
6297 struct btrfs_inode_item);
6299 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6303 * The cur_ino = root dir case is special here. We can't treat
6304 * the inode as deleted+reused because it would generate a
6305 * stream that tries to delete/mkdir the root dir.
6307 if (left_gen != right_gen &&
6308 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6309 sctx->cur_inode_new_gen = 1;
6313 * Normally we do not find inodes with a link count of zero (orphans)
6314 * because the most common case is to create a snapshot and use it
6315 * for a send operation. However other less common use cases involve
6316 * using a subvolume and send it after turning it to RO mode just
6317 * after deleting all hard links of a file while holding an open
6318 * file descriptor against it or turning a RO snapshot into RW mode,
6319 * keep an open file descriptor against a file, delete it and then
6320 * turn the snapshot back to RO mode before using it for a send
6321 * operation. So if we find such cases, ignore the inode and all its
6322 * items completely if it's a new inode, or if it's a changed inode
6323 * make sure all its previous paths (from the parent snapshot) are all
6324 * unlinked and all other the inode items are ignored.
6326 if (result == BTRFS_COMPARE_TREE_NEW ||
6327 result == BTRFS_COMPARE_TREE_CHANGED) {
6330 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6332 sctx->ignore_cur_inode = true;
6333 if (result == BTRFS_COMPARE_TREE_CHANGED)
6334 ret = btrfs_unlink_all_paths(sctx);
6339 if (result == BTRFS_COMPARE_TREE_NEW) {
6340 sctx->cur_inode_gen = left_gen;
6341 sctx->cur_inode_new = 1;
6342 sctx->cur_inode_deleted = 0;
6343 sctx->cur_inode_size = btrfs_inode_size(
6344 sctx->left_path->nodes[0], left_ii);
6345 sctx->cur_inode_mode = btrfs_inode_mode(
6346 sctx->left_path->nodes[0], left_ii);
6347 sctx->cur_inode_rdev = btrfs_inode_rdev(
6348 sctx->left_path->nodes[0], left_ii);
6349 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6350 ret = send_create_inode_if_needed(sctx);
6351 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6352 sctx->cur_inode_gen = right_gen;
6353 sctx->cur_inode_new = 0;
6354 sctx->cur_inode_deleted = 1;
6355 sctx->cur_inode_size = btrfs_inode_size(
6356 sctx->right_path->nodes[0], right_ii);
6357 sctx->cur_inode_mode = btrfs_inode_mode(
6358 sctx->right_path->nodes[0], right_ii);
6359 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
6361 * We need to do some special handling in case the inode was
6362 * reported as changed with a changed generation number. This
6363 * means that the original inode was deleted and new inode
6364 * reused the same inum. So we have to treat the old inode as
6365 * deleted and the new one as new.
6367 if (sctx->cur_inode_new_gen) {
6369 * First, process the inode as if it was deleted.
6371 sctx->cur_inode_gen = right_gen;
6372 sctx->cur_inode_new = 0;
6373 sctx->cur_inode_deleted = 1;
6374 sctx->cur_inode_size = btrfs_inode_size(
6375 sctx->right_path->nodes[0], right_ii);
6376 sctx->cur_inode_mode = btrfs_inode_mode(
6377 sctx->right_path->nodes[0], right_ii);
6378 ret = process_all_refs(sctx,
6379 BTRFS_COMPARE_TREE_DELETED);
6384 * Now process the inode as if it was new.
6386 sctx->cur_inode_gen = left_gen;
6387 sctx->cur_inode_new = 1;
6388 sctx->cur_inode_deleted = 0;
6389 sctx->cur_inode_size = btrfs_inode_size(
6390 sctx->left_path->nodes[0], left_ii);
6391 sctx->cur_inode_mode = btrfs_inode_mode(
6392 sctx->left_path->nodes[0], left_ii);
6393 sctx->cur_inode_rdev = btrfs_inode_rdev(
6394 sctx->left_path->nodes[0], left_ii);
6395 ret = send_create_inode_if_needed(sctx);
6399 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6403 * Advance send_progress now as we did not get into
6404 * process_recorded_refs_if_needed in the new_gen case.
6406 sctx->send_progress = sctx->cur_ino + 1;
6409 * Now process all extents and xattrs of the inode as if
6410 * they were all new.
6412 ret = process_all_extents(sctx);
6415 ret = process_all_new_xattrs(sctx);
6419 sctx->cur_inode_gen = left_gen;
6420 sctx->cur_inode_new = 0;
6421 sctx->cur_inode_new_gen = 0;
6422 sctx->cur_inode_deleted = 0;
6423 sctx->cur_inode_size = btrfs_inode_size(
6424 sctx->left_path->nodes[0], left_ii);
6425 sctx->cur_inode_mode = btrfs_inode_mode(
6426 sctx->left_path->nodes[0], left_ii);
6435 * We have to process new refs before deleted refs, but compare_trees gives us
6436 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6437 * first and later process them in process_recorded_refs.
6438 * For the cur_inode_new_gen case, we skip recording completely because
6439 * changed_inode did already initiate processing of refs. The reason for this is
6440 * that in this case, compare_tree actually compares the refs of 2 different
6441 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6442 * refs of the right tree as deleted and all refs of the left tree as new.
6444 static int changed_ref(struct send_ctx *sctx,
6445 enum btrfs_compare_tree_result result)
6449 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6450 inconsistent_snapshot_error(sctx, result, "reference");
6454 if (!sctx->cur_inode_new_gen &&
6455 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6456 if (result == BTRFS_COMPARE_TREE_NEW)
6457 ret = record_new_ref(sctx);
6458 else if (result == BTRFS_COMPARE_TREE_DELETED)
6459 ret = record_deleted_ref(sctx);
6460 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6461 ret = record_changed_ref(sctx);
6468 * Process new/deleted/changed xattrs. We skip processing in the
6469 * cur_inode_new_gen case because changed_inode did already initiate processing
6470 * of xattrs. The reason is the same as in changed_ref
6472 static int changed_xattr(struct send_ctx *sctx,
6473 enum btrfs_compare_tree_result result)
6477 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6478 inconsistent_snapshot_error(sctx, result, "xattr");
6482 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6483 if (result == BTRFS_COMPARE_TREE_NEW)
6484 ret = process_new_xattr(sctx);
6485 else if (result == BTRFS_COMPARE_TREE_DELETED)
6486 ret = process_deleted_xattr(sctx);
6487 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6488 ret = process_changed_xattr(sctx);
6495 * Process new/deleted/changed extents. We skip processing in the
6496 * cur_inode_new_gen case because changed_inode did already initiate processing
6497 * of extents. The reason is the same as in changed_ref
6499 static int changed_extent(struct send_ctx *sctx,
6500 enum btrfs_compare_tree_result result)
6505 * We have found an extent item that changed without the inode item
6506 * having changed. This can happen either after relocation (where the
6507 * disk_bytenr of an extent item is replaced at
6508 * relocation.c:replace_file_extents()) or after deduplication into a
6509 * file in both the parent and send snapshots (where an extent item can
6510 * get modified or replaced with a new one). Note that deduplication
6511 * updates the inode item, but it only changes the iversion (sequence
6512 * field in the inode item) of the inode, so if a file is deduplicated
6513 * the same amount of times in both the parent and send snapshots, its
6514 * iversion becomes the same in both snapshots, whence the inode item is
6515 * the same on both snapshots.
6517 if (sctx->cur_ino != sctx->cmp_key->objectid)
6520 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6521 if (result != BTRFS_COMPARE_TREE_DELETED)
6522 ret = process_extent(sctx, sctx->left_path,
6529 static int dir_changed(struct send_ctx *sctx, u64 dir)
6531 u64 orig_gen, new_gen;
6534 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6539 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6544 return (orig_gen != new_gen) ? 1 : 0;
6547 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6548 struct btrfs_key *key)
6550 struct btrfs_inode_extref *extref;
6551 struct extent_buffer *leaf;
6552 u64 dirid = 0, last_dirid = 0;
6559 /* Easy case, just check this one dirid */
6560 if (key->type == BTRFS_INODE_REF_KEY) {
6561 dirid = key->offset;
6563 ret = dir_changed(sctx, dirid);
6567 leaf = path->nodes[0];
6568 item_size = btrfs_item_size(leaf, path->slots[0]);
6569 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6570 while (cur_offset < item_size) {
6571 extref = (struct btrfs_inode_extref *)(ptr +
6573 dirid = btrfs_inode_extref_parent(leaf, extref);
6574 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6575 cur_offset += ref_name_len + sizeof(*extref);
6576 if (dirid == last_dirid)
6578 ret = dir_changed(sctx, dirid);
6588 * Updates compare related fields in sctx and simply forwards to the actual
6589 * changed_xxx functions.
6591 static int changed_cb(struct btrfs_path *left_path,
6592 struct btrfs_path *right_path,
6593 struct btrfs_key *key,
6594 enum btrfs_compare_tree_result result,
6595 struct send_ctx *sctx)
6600 * We can not hold the commit root semaphore here. This is because in
6601 * the case of sending and receiving to the same filesystem, using a
6602 * pipe, could result in a deadlock:
6604 * 1) The task running send blocks on the pipe because it's full;
6606 * 2) The task running receive, which is the only consumer of the pipe,
6607 * is waiting for a transaction commit (for example due to a space
6608 * reservation when doing a write or triggering a transaction commit
6609 * when creating a subvolume);
6611 * 3) The transaction is waiting to write lock the commit root semaphore,
6612 * but can not acquire it since it's being held at 1).
6614 * Down this call chain we write to the pipe through kernel_write().
6615 * The same type of problem can also happen when sending to a file that
6616 * is stored in the same filesystem - when reserving space for a write
6617 * into the file, we can trigger a transaction commit.
6619 * Our caller has supplied us with clones of leaves from the send and
6620 * parent roots, so we're safe here from a concurrent relocation and
6621 * further reallocation of metadata extents while we are here. Below we
6622 * also assert that the leaves are clones.
6624 lockdep_assert_not_held(&sctx->send_root->fs_info->commit_root_sem);
6627 * We always have a send root, so left_path is never NULL. We will not
6628 * have a leaf when we have reached the end of the send root but have
6629 * not yet reached the end of the parent root.
6631 if (left_path->nodes[0])
6632 ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
6633 &left_path->nodes[0]->bflags));
6635 * When doing a full send we don't have a parent root, so right_path is
6636 * NULL. When doing an incremental send, we may have reached the end of
6637 * the parent root already, so we don't have a leaf at right_path.
6639 if (right_path && right_path->nodes[0])
6640 ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
6641 &right_path->nodes[0]->bflags));
6643 if (result == BTRFS_COMPARE_TREE_SAME) {
6644 if (key->type == BTRFS_INODE_REF_KEY ||
6645 key->type == BTRFS_INODE_EXTREF_KEY) {
6646 ret = compare_refs(sctx, left_path, key);
6651 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6652 return maybe_send_hole(sctx, left_path, key);
6656 result = BTRFS_COMPARE_TREE_CHANGED;
6660 sctx->left_path = left_path;
6661 sctx->right_path = right_path;
6662 sctx->cmp_key = key;
6664 ret = finish_inode_if_needed(sctx, 0);
6668 /* Ignore non-FS objects */
6669 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6670 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6673 if (key->type == BTRFS_INODE_ITEM_KEY) {
6674 ret = changed_inode(sctx, result);
6675 } else if (!sctx->ignore_cur_inode) {
6676 if (key->type == BTRFS_INODE_REF_KEY ||
6677 key->type == BTRFS_INODE_EXTREF_KEY)
6678 ret = changed_ref(sctx, result);
6679 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6680 ret = changed_xattr(sctx, result);
6681 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6682 ret = changed_extent(sctx, result);
6689 static int search_key_again(const struct send_ctx *sctx,
6690 struct btrfs_root *root,
6691 struct btrfs_path *path,
6692 const struct btrfs_key *key)
6696 if (!path->need_commit_sem)
6697 lockdep_assert_held_read(&root->fs_info->commit_root_sem);
6700 * Roots used for send operations are readonly and no one can add,
6701 * update or remove keys from them, so we should be able to find our
6702 * key again. The only exception is deduplication, which can operate on
6703 * readonly roots and add, update or remove keys to/from them - but at
6704 * the moment we don't allow it to run in parallel with send.
6706 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6709 btrfs_print_tree(path->nodes[path->lowest_level], false);
6710 btrfs_err(root->fs_info,
6711 "send: key (%llu %u %llu) not found in %s root %llu, lowest_level %d, slot %d",
6712 key->objectid, key->type, key->offset,
6713 (root == sctx->parent_root ? "parent" : "send"),
6714 root->root_key.objectid, path->lowest_level,
6715 path->slots[path->lowest_level]);
6722 static int full_send_tree(struct send_ctx *sctx)
6725 struct btrfs_root *send_root = sctx->send_root;
6726 struct btrfs_key key;
6727 struct btrfs_fs_info *fs_info = send_root->fs_info;
6728 struct btrfs_path *path;
6730 path = alloc_path_for_send();
6733 path->reada = READA_FORWARD_ALWAYS;
6735 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6736 key.type = BTRFS_INODE_ITEM_KEY;
6739 down_read(&fs_info->commit_root_sem);
6740 sctx->last_reloc_trans = fs_info->last_reloc_trans;
6741 up_read(&fs_info->commit_root_sem);
6743 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6750 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
6752 ret = changed_cb(path, NULL, &key,
6753 BTRFS_COMPARE_TREE_NEW, sctx);
6757 down_read(&fs_info->commit_root_sem);
6758 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
6759 sctx->last_reloc_trans = fs_info->last_reloc_trans;
6760 up_read(&fs_info->commit_root_sem);
6762 * A transaction used for relocating a block group was
6763 * committed or is about to finish its commit. Release
6764 * our path (leaf) and restart the search, so that we
6765 * avoid operating on any file extent items that are
6766 * stale, with a disk_bytenr that reflects a pre
6767 * relocation value. This way we avoid as much as
6768 * possible to fallback to regular writes when checking
6769 * if we can clone file ranges.
6771 btrfs_release_path(path);
6772 ret = search_key_again(sctx, send_root, path, &key);
6776 up_read(&fs_info->commit_root_sem);
6779 ret = btrfs_next_item(send_root, path);
6789 ret = finish_inode_if_needed(sctx, 1);
6792 btrfs_free_path(path);
6796 static int replace_node_with_clone(struct btrfs_path *path, int level)
6798 struct extent_buffer *clone;
6800 clone = btrfs_clone_extent_buffer(path->nodes[level]);
6804 free_extent_buffer(path->nodes[level]);
6805 path->nodes[level] = clone;
6810 static int tree_move_down(struct btrfs_path *path, int *level, u64 reada_min_gen)
6812 struct extent_buffer *eb;
6813 struct extent_buffer *parent = path->nodes[*level];
6814 int slot = path->slots[*level];
6815 const int nritems = btrfs_header_nritems(parent);
6819 lockdep_assert_held_read(&parent->fs_info->commit_root_sem);
6821 BUG_ON(*level == 0);
6822 eb = btrfs_read_node_slot(parent, slot);
6827 * Trigger readahead for the next leaves we will process, so that it is
6828 * very likely that when we need them they are already in memory and we
6829 * will not block on disk IO. For nodes we only do readahead for one,
6830 * since the time window between processing nodes is typically larger.
6832 reada_max = (*level == 1 ? SZ_128K : eb->fs_info->nodesize);
6834 for (slot++; slot < nritems && reada_done < reada_max; slot++) {
6835 if (btrfs_node_ptr_generation(parent, slot) > reada_min_gen) {
6836 btrfs_readahead_node_child(parent, slot);
6837 reada_done += eb->fs_info->nodesize;
6841 path->nodes[*level - 1] = eb;
6842 path->slots[*level - 1] = 0;
6846 return replace_node_with_clone(path, 0);
6851 static int tree_move_next_or_upnext(struct btrfs_path *path,
6852 int *level, int root_level)
6856 nritems = btrfs_header_nritems(path->nodes[*level]);
6858 path->slots[*level]++;
6860 while (path->slots[*level] >= nritems) {
6861 if (*level == root_level) {
6862 path->slots[*level] = nritems - 1;
6867 path->slots[*level] = 0;
6868 free_extent_buffer(path->nodes[*level]);
6869 path->nodes[*level] = NULL;
6871 path->slots[*level]++;
6873 nritems = btrfs_header_nritems(path->nodes[*level]);
6880 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
6883 static int tree_advance(struct btrfs_path *path,
6884 int *level, int root_level,
6886 struct btrfs_key *key,
6891 if (*level == 0 || !allow_down) {
6892 ret = tree_move_next_or_upnext(path, level, root_level);
6894 ret = tree_move_down(path, level, reada_min_gen);
6898 * Even if we have reached the end of a tree, ret is -1, update the key
6899 * anyway, so that in case we need to restart due to a block group
6900 * relocation, we can assert that the last key of the root node still
6901 * exists in the tree.
6904 btrfs_item_key_to_cpu(path->nodes[*level], key,
6905 path->slots[*level]);
6907 btrfs_node_key_to_cpu(path->nodes[*level], key,
6908 path->slots[*level]);
6913 static int tree_compare_item(struct btrfs_path *left_path,
6914 struct btrfs_path *right_path,
6919 unsigned long off1, off2;
6921 len1 = btrfs_item_size(left_path->nodes[0], left_path->slots[0]);
6922 len2 = btrfs_item_size(right_path->nodes[0], right_path->slots[0]);
6926 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
6927 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
6928 right_path->slots[0]);
6930 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
6932 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
6939 * A transaction used for relocating a block group was committed or is about to
6940 * finish its commit. Release our paths and restart the search, so that we are
6941 * not using stale extent buffers:
6943 * 1) For levels > 0, we are only holding references of extent buffers, without
6944 * any locks on them, which does not prevent them from having been relocated
6945 * and reallocated after the last time we released the commit root semaphore.
6946 * The exception are the root nodes, for which we always have a clone, see
6947 * the comment at btrfs_compare_trees();
6949 * 2) For leaves, level 0, we are holding copies (clones) of extent buffers, so
6950 * we are safe from the concurrent relocation and reallocation. However they
6951 * can have file extent items with a pre relocation disk_bytenr value, so we
6952 * restart the start from the current commit roots and clone the new leaves so
6953 * that we get the post relocation disk_bytenr values. Not doing so, could
6954 * make us clone the wrong data in case there are new extents using the old
6955 * disk_bytenr that happen to be shared.
6957 static int restart_after_relocation(struct btrfs_path *left_path,
6958 struct btrfs_path *right_path,
6959 const struct btrfs_key *left_key,
6960 const struct btrfs_key *right_key,
6963 const struct send_ctx *sctx)
6968 lockdep_assert_held_read(&sctx->send_root->fs_info->commit_root_sem);
6970 btrfs_release_path(left_path);
6971 btrfs_release_path(right_path);
6974 * Since keys can not be added or removed to/from our roots because they
6975 * are readonly and we do not allow deduplication to run in parallel
6976 * (which can add, remove or change keys), the layout of the trees should
6979 left_path->lowest_level = left_level;
6980 ret = search_key_again(sctx, sctx->send_root, left_path, left_key);
6984 right_path->lowest_level = right_level;
6985 ret = search_key_again(sctx, sctx->parent_root, right_path, right_key);
6990 * If the lowest level nodes are leaves, clone them so that they can be
6991 * safely used by changed_cb() while not under the protection of the
6992 * commit root semaphore, even if relocation and reallocation happens in
6995 if (left_level == 0) {
6996 ret = replace_node_with_clone(left_path, 0);
7001 if (right_level == 0) {
7002 ret = replace_node_with_clone(right_path, 0);
7008 * Now clone the root nodes (unless they happen to be the leaves we have
7009 * already cloned). This is to protect against concurrent snapshotting of
7010 * the send and parent roots (see the comment at btrfs_compare_trees()).
7012 root_level = btrfs_header_level(sctx->send_root->commit_root);
7013 if (root_level > 0) {
7014 ret = replace_node_with_clone(left_path, root_level);
7019 root_level = btrfs_header_level(sctx->parent_root->commit_root);
7020 if (root_level > 0) {
7021 ret = replace_node_with_clone(right_path, root_level);
7030 * This function compares two trees and calls the provided callback for
7031 * every changed/new/deleted item it finds.
7032 * If shared tree blocks are encountered, whole subtrees are skipped, making
7033 * the compare pretty fast on snapshotted subvolumes.
7035 * This currently works on commit roots only. As commit roots are read only,
7036 * we don't do any locking. The commit roots are protected with transactions.
7037 * Transactions are ended and rejoined when a commit is tried in between.
7039 * This function checks for modifications done to the trees while comparing.
7040 * If it detects a change, it aborts immediately.
7042 static int btrfs_compare_trees(struct btrfs_root *left_root,
7043 struct btrfs_root *right_root, struct send_ctx *sctx)
7045 struct btrfs_fs_info *fs_info = left_root->fs_info;
7048 struct btrfs_path *left_path = NULL;
7049 struct btrfs_path *right_path = NULL;
7050 struct btrfs_key left_key;
7051 struct btrfs_key right_key;
7052 char *tmp_buf = NULL;
7053 int left_root_level;
7054 int right_root_level;
7057 int left_end_reached = 0;
7058 int right_end_reached = 0;
7059 int advance_left = 0;
7060 int advance_right = 0;
7067 left_path = btrfs_alloc_path();
7072 right_path = btrfs_alloc_path();
7078 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
7084 left_path->search_commit_root = 1;
7085 left_path->skip_locking = 1;
7086 right_path->search_commit_root = 1;
7087 right_path->skip_locking = 1;
7090 * Strategy: Go to the first items of both trees. Then do
7092 * If both trees are at level 0
7093 * Compare keys of current items
7094 * If left < right treat left item as new, advance left tree
7096 * If left > right treat right item as deleted, advance right tree
7098 * If left == right do deep compare of items, treat as changed if
7099 * needed, advance both trees and repeat
7100 * If both trees are at the same level but not at level 0
7101 * Compare keys of current nodes/leafs
7102 * If left < right advance left tree and repeat
7103 * If left > right advance right tree and repeat
7104 * If left == right compare blockptrs of the next nodes/leafs
7105 * If they match advance both trees but stay at the same level
7107 * If they don't match advance both trees while allowing to go
7109 * If tree levels are different
7110 * Advance the tree that needs it and repeat
7112 * Advancing a tree means:
7113 * If we are at level 0, try to go to the next slot. If that's not
7114 * possible, go one level up and repeat. Stop when we found a level
7115 * where we could go to the next slot. We may at this point be on a
7118 * If we are not at level 0 and not on shared tree blocks, go one
7121 * If we are not at level 0 and on shared tree blocks, go one slot to
7122 * the right if possible or go up and right.
7125 down_read(&fs_info->commit_root_sem);
7126 left_level = btrfs_header_level(left_root->commit_root);
7127 left_root_level = left_level;
7129 * We clone the root node of the send and parent roots to prevent races
7130 * with snapshot creation of these roots. Snapshot creation COWs the
7131 * root node of a tree, so after the transaction is committed the old
7132 * extent can be reallocated while this send operation is still ongoing.
7133 * So we clone them, under the commit root semaphore, to be race free.
7135 left_path->nodes[left_level] =
7136 btrfs_clone_extent_buffer(left_root->commit_root);
7137 if (!left_path->nodes[left_level]) {
7142 right_level = btrfs_header_level(right_root->commit_root);
7143 right_root_level = right_level;
7144 right_path->nodes[right_level] =
7145 btrfs_clone_extent_buffer(right_root->commit_root);
7146 if (!right_path->nodes[right_level]) {
7151 * Our right root is the parent root, while the left root is the "send"
7152 * root. We know that all new nodes/leaves in the left root must have
7153 * a generation greater than the right root's generation, so we trigger
7154 * readahead for those nodes and leaves of the left root, as we know we
7155 * will need to read them at some point.
7157 reada_min_gen = btrfs_header_generation(right_root->commit_root);
7159 if (left_level == 0)
7160 btrfs_item_key_to_cpu(left_path->nodes[left_level],
7161 &left_key, left_path->slots[left_level]);
7163 btrfs_node_key_to_cpu(left_path->nodes[left_level],
7164 &left_key, left_path->slots[left_level]);
7165 if (right_level == 0)
7166 btrfs_item_key_to_cpu(right_path->nodes[right_level],
7167 &right_key, right_path->slots[right_level]);
7169 btrfs_node_key_to_cpu(right_path->nodes[right_level],
7170 &right_key, right_path->slots[right_level]);
7172 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7175 if (need_resched() ||
7176 rwsem_is_contended(&fs_info->commit_root_sem)) {
7177 up_read(&fs_info->commit_root_sem);
7179 down_read(&fs_info->commit_root_sem);
7182 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
7183 ret = restart_after_relocation(left_path, right_path,
7184 &left_key, &right_key,
7185 left_level, right_level,
7189 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7192 if (advance_left && !left_end_reached) {
7193 ret = tree_advance(left_path, &left_level,
7195 advance_left != ADVANCE_ONLY_NEXT,
7196 &left_key, reada_min_gen);
7198 left_end_reached = ADVANCE;
7203 if (advance_right && !right_end_reached) {
7204 ret = tree_advance(right_path, &right_level,
7206 advance_right != ADVANCE_ONLY_NEXT,
7207 &right_key, reada_min_gen);
7209 right_end_reached = ADVANCE;
7215 if (left_end_reached && right_end_reached) {
7218 } else if (left_end_reached) {
7219 if (right_level == 0) {
7220 up_read(&fs_info->commit_root_sem);
7221 ret = changed_cb(left_path, right_path,
7223 BTRFS_COMPARE_TREE_DELETED,
7227 down_read(&fs_info->commit_root_sem);
7229 advance_right = ADVANCE;
7231 } else if (right_end_reached) {
7232 if (left_level == 0) {
7233 up_read(&fs_info->commit_root_sem);
7234 ret = changed_cb(left_path, right_path,
7236 BTRFS_COMPARE_TREE_NEW,
7240 down_read(&fs_info->commit_root_sem);
7242 advance_left = ADVANCE;
7246 if (left_level == 0 && right_level == 0) {
7247 up_read(&fs_info->commit_root_sem);
7248 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7250 ret = changed_cb(left_path, right_path,
7252 BTRFS_COMPARE_TREE_NEW,
7254 advance_left = ADVANCE;
7255 } else if (cmp > 0) {
7256 ret = changed_cb(left_path, right_path,
7258 BTRFS_COMPARE_TREE_DELETED,
7260 advance_right = ADVANCE;
7262 enum btrfs_compare_tree_result result;
7264 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
7265 ret = tree_compare_item(left_path, right_path,
7268 result = BTRFS_COMPARE_TREE_CHANGED;
7270 result = BTRFS_COMPARE_TREE_SAME;
7271 ret = changed_cb(left_path, right_path,
7272 &left_key, result, sctx);
7273 advance_left = ADVANCE;
7274 advance_right = ADVANCE;
7279 down_read(&fs_info->commit_root_sem);
7280 } else if (left_level == right_level) {
7281 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7283 advance_left = ADVANCE;
7284 } else if (cmp > 0) {
7285 advance_right = ADVANCE;
7287 left_blockptr = btrfs_node_blockptr(
7288 left_path->nodes[left_level],
7289 left_path->slots[left_level]);
7290 right_blockptr = btrfs_node_blockptr(
7291 right_path->nodes[right_level],
7292 right_path->slots[right_level]);
7293 left_gen = btrfs_node_ptr_generation(
7294 left_path->nodes[left_level],
7295 left_path->slots[left_level]);
7296 right_gen = btrfs_node_ptr_generation(
7297 right_path->nodes[right_level],
7298 right_path->slots[right_level]);
7299 if (left_blockptr == right_blockptr &&
7300 left_gen == right_gen) {
7302 * As we're on a shared block, don't
7303 * allow to go deeper.
7305 advance_left = ADVANCE_ONLY_NEXT;
7306 advance_right = ADVANCE_ONLY_NEXT;
7308 advance_left = ADVANCE;
7309 advance_right = ADVANCE;
7312 } else if (left_level < right_level) {
7313 advance_right = ADVANCE;
7315 advance_left = ADVANCE;
7320 up_read(&fs_info->commit_root_sem);
7322 btrfs_free_path(left_path);
7323 btrfs_free_path(right_path);
7328 static int send_subvol(struct send_ctx *sctx)
7332 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
7333 ret = send_header(sctx);
7338 ret = send_subvol_begin(sctx);
7342 if (sctx->parent_root) {
7343 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
7346 ret = finish_inode_if_needed(sctx, 1);
7350 ret = full_send_tree(sctx);
7356 free_recorded_refs(sctx);
7361 * If orphan cleanup did remove any orphans from a root, it means the tree
7362 * was modified and therefore the commit root is not the same as the current
7363 * root anymore. This is a problem, because send uses the commit root and
7364 * therefore can see inode items that don't exist in the current root anymore,
7365 * and for example make calls to btrfs_iget, which will do tree lookups based
7366 * on the current root and not on the commit root. Those lookups will fail,
7367 * returning a -ESTALE error, and making send fail with that error. So make
7368 * sure a send does not see any orphans we have just removed, and that it will
7369 * see the same inodes regardless of whether a transaction commit happened
7370 * before it started (meaning that the commit root will be the same as the
7371 * current root) or not.
7373 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7376 struct btrfs_trans_handle *trans = NULL;
7379 if (sctx->parent_root &&
7380 sctx->parent_root->node != sctx->parent_root->commit_root)
7383 for (i = 0; i < sctx->clone_roots_cnt; i++)
7384 if (sctx->clone_roots[i].root->node !=
7385 sctx->clone_roots[i].root->commit_root)
7389 return btrfs_end_transaction(trans);
7394 /* Use any root, all fs roots will get their commit roots updated. */
7396 trans = btrfs_join_transaction(sctx->send_root);
7398 return PTR_ERR(trans);
7402 return btrfs_commit_transaction(trans);
7406 * Make sure any existing dellaloc is flushed for any root used by a send
7407 * operation so that we do not miss any data and we do not race with writeback
7408 * finishing and changing a tree while send is using the tree. This could
7409 * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and
7410 * a send operation then uses the subvolume.
7411 * After flushing delalloc ensure_commit_roots_uptodate() must be called.
7413 static int flush_delalloc_roots(struct send_ctx *sctx)
7415 struct btrfs_root *root = sctx->parent_root;
7420 ret = btrfs_start_delalloc_snapshot(root, false);
7423 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7426 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7427 root = sctx->clone_roots[i].root;
7428 ret = btrfs_start_delalloc_snapshot(root, false);
7431 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7437 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7439 spin_lock(&root->root_item_lock);
7440 root->send_in_progress--;
7442 * Not much left to do, we don't know why it's unbalanced and
7443 * can't blindly reset it to 0.
7445 if (root->send_in_progress < 0)
7446 btrfs_err(root->fs_info,
7447 "send_in_progress unbalanced %d root %llu",
7448 root->send_in_progress, root->root_key.objectid);
7449 spin_unlock(&root->root_item_lock);
7452 static void dedupe_in_progress_warn(const struct btrfs_root *root)
7454 btrfs_warn_rl(root->fs_info,
7455 "cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7456 root->root_key.objectid, root->dedupe_in_progress);
7459 long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg)
7462 struct btrfs_root *send_root = BTRFS_I(inode)->root;
7463 struct btrfs_fs_info *fs_info = send_root->fs_info;
7464 struct btrfs_root *clone_root;
7465 struct send_ctx *sctx = NULL;
7467 u64 *clone_sources_tmp = NULL;
7468 int clone_sources_to_rollback = 0;
7470 int sort_clone_roots = 0;
7472 if (!capable(CAP_SYS_ADMIN))
7476 * The subvolume must remain read-only during send, protect against
7477 * making it RW. This also protects against deletion.
7479 spin_lock(&send_root->root_item_lock);
7480 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7481 dedupe_in_progress_warn(send_root);
7482 spin_unlock(&send_root->root_item_lock);
7485 send_root->send_in_progress++;
7486 spin_unlock(&send_root->root_item_lock);
7489 * Userspace tools do the checks and warn the user if it's
7492 if (!btrfs_root_readonly(send_root)) {
7498 * Check that we don't overflow at later allocations, we request
7499 * clone_sources_count + 1 items, and compare to unsigned long inside
7502 if (arg->clone_sources_count >
7503 ULONG_MAX / sizeof(struct clone_root) - 1) {
7508 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
7513 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
7519 INIT_LIST_HEAD(&sctx->new_refs);
7520 INIT_LIST_HEAD(&sctx->deleted_refs);
7521 xa_init_flags(&sctx->name_cache, GFP_KERNEL);
7522 INIT_LIST_HEAD(&sctx->name_cache_list);
7524 sctx->flags = arg->flags;
7526 if (arg->flags & BTRFS_SEND_FLAG_VERSION) {
7527 if (arg->version > BTRFS_SEND_STREAM_VERSION) {
7531 /* Zero means "use the highest version" */
7532 sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION;
7537 sctx->send_filp = fget(arg->send_fd);
7538 if (!sctx->send_filp) {
7543 sctx->send_root = send_root;
7545 * Unlikely but possible, if the subvolume is marked for deletion but
7546 * is slow to remove the directory entry, send can still be started
7548 if (btrfs_root_dead(sctx->send_root)) {
7553 sctx->clone_roots_cnt = arg->clone_sources_count;
7555 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
7556 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
7557 if (!sctx->send_buf) {
7562 sctx->pending_dir_moves = RB_ROOT;
7563 sctx->waiting_dir_moves = RB_ROOT;
7564 sctx->orphan_dirs = RB_ROOT;
7566 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
7567 arg->clone_sources_count + 1,
7569 if (!sctx->clone_roots) {
7574 alloc_size = array_size(sizeof(*arg->clone_sources),
7575 arg->clone_sources_count);
7577 if (arg->clone_sources_count) {
7578 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
7579 if (!clone_sources_tmp) {
7584 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
7591 for (i = 0; i < arg->clone_sources_count; i++) {
7592 clone_root = btrfs_get_fs_root(fs_info,
7593 clone_sources_tmp[i], true);
7594 if (IS_ERR(clone_root)) {
7595 ret = PTR_ERR(clone_root);
7598 spin_lock(&clone_root->root_item_lock);
7599 if (!btrfs_root_readonly(clone_root) ||
7600 btrfs_root_dead(clone_root)) {
7601 spin_unlock(&clone_root->root_item_lock);
7602 btrfs_put_root(clone_root);
7606 if (clone_root->dedupe_in_progress) {
7607 dedupe_in_progress_warn(clone_root);
7608 spin_unlock(&clone_root->root_item_lock);
7609 btrfs_put_root(clone_root);
7613 clone_root->send_in_progress++;
7614 spin_unlock(&clone_root->root_item_lock);
7616 sctx->clone_roots[i].root = clone_root;
7617 clone_sources_to_rollback = i + 1;
7619 kvfree(clone_sources_tmp);
7620 clone_sources_tmp = NULL;
7623 if (arg->parent_root) {
7624 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7626 if (IS_ERR(sctx->parent_root)) {
7627 ret = PTR_ERR(sctx->parent_root);
7631 spin_lock(&sctx->parent_root->root_item_lock);
7632 sctx->parent_root->send_in_progress++;
7633 if (!btrfs_root_readonly(sctx->parent_root) ||
7634 btrfs_root_dead(sctx->parent_root)) {
7635 spin_unlock(&sctx->parent_root->root_item_lock);
7639 if (sctx->parent_root->dedupe_in_progress) {
7640 dedupe_in_progress_warn(sctx->parent_root);
7641 spin_unlock(&sctx->parent_root->root_item_lock);
7645 spin_unlock(&sctx->parent_root->root_item_lock);
7649 * Clones from send_root are allowed, but only if the clone source
7650 * is behind the current send position. This is checked while searching
7651 * for possible clone sources.
7653 sctx->clone_roots[sctx->clone_roots_cnt++].root =
7654 btrfs_grab_root(sctx->send_root);
7656 /* We do a bsearch later */
7657 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7658 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7660 sort_clone_roots = 1;
7662 ret = flush_delalloc_roots(sctx);
7666 ret = ensure_commit_roots_uptodate(sctx);
7670 ret = send_subvol(sctx);
7674 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7675 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7678 ret = send_cmd(sctx);
7684 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7685 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7687 struct pending_dir_move *pm;
7689 n = rb_first(&sctx->pending_dir_moves);
7690 pm = rb_entry(n, struct pending_dir_move, node);
7691 while (!list_empty(&pm->list)) {
7692 struct pending_dir_move *pm2;
7694 pm2 = list_first_entry(&pm->list,
7695 struct pending_dir_move, list);
7696 free_pending_move(sctx, pm2);
7698 free_pending_move(sctx, pm);
7701 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7702 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7704 struct waiting_dir_move *dm;
7706 n = rb_first(&sctx->waiting_dir_moves);
7707 dm = rb_entry(n, struct waiting_dir_move, node);
7708 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7712 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
7713 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
7715 struct orphan_dir_info *odi;
7717 n = rb_first(&sctx->orphan_dirs);
7718 odi = rb_entry(n, struct orphan_dir_info, node);
7719 free_orphan_dir_info(sctx, odi);
7722 if (sort_clone_roots) {
7723 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7724 btrfs_root_dec_send_in_progress(
7725 sctx->clone_roots[i].root);
7726 btrfs_put_root(sctx->clone_roots[i].root);
7729 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
7730 btrfs_root_dec_send_in_progress(
7731 sctx->clone_roots[i].root);
7732 btrfs_put_root(sctx->clone_roots[i].root);
7735 btrfs_root_dec_send_in_progress(send_root);
7737 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
7738 btrfs_root_dec_send_in_progress(sctx->parent_root);
7739 btrfs_put_root(sctx->parent_root);
7742 kvfree(clone_sources_tmp);
7745 if (sctx->send_filp)
7746 fput(sctx->send_filp);
7748 kvfree(sctx->clone_roots);
7749 kvfree(sctx->send_buf);
7751 name_cache_free(sctx);
7753 close_current_inode(sctx);