1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2012 Alexander Block. All rights reserved.
6 #include <linux/bsearch.h>
8 #include <linux/file.h>
9 #include <linux/sort.h>
10 #include <linux/mount.h>
11 #include <linux/xattr.h>
12 #include <linux/posix_acl_xattr.h>
13 #include <linux/radix-tree.h>
14 #include <linux/vmalloc.h>
15 #include <linux/string.h>
16 #include <linux/compat.h>
17 #include <linux/crc32c.h>
23 #include "btrfs_inode.h"
24 #include "transaction.h"
25 #include "compression.h"
28 * A fs_path is a helper to dynamically build path names with unknown size.
29 * It reallocates the internal buffer on demand.
30 * It allows fast adding of path elements on the right side (normal path) and
31 * fast adding to the left side (reversed path). A reversed path can also be
32 * unreversed if needed.
41 unsigned short buf_len
:15;
42 unsigned short reversed
:1;
46 * Average path length does not exceed 200 bytes, we'll have
47 * better packing in the slab and higher chance to satisfy
48 * a allocation later during send.
53 #define FS_PATH_INLINE_SIZE \
54 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
57 /* reused for each extent */
59 struct btrfs_root
*root
;
66 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
67 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
70 struct file
*send_filp
;
76 u64 cmd_send_size
[BTRFS_SEND_C_MAX
+ 1];
77 u64 flags
; /* 'flags' member of btrfs_ioctl_send_args is u64 */
79 struct btrfs_root
*send_root
;
80 struct btrfs_root
*parent_root
;
81 struct clone_root
*clone_roots
;
84 /* current state of the compare_tree call */
85 struct btrfs_path
*left_path
;
86 struct btrfs_path
*right_path
;
87 struct btrfs_key
*cmp_key
;
90 * infos of the currently processed inode. In case of deleted inodes,
91 * these are the values from the deleted inode.
96 int cur_inode_new_gen
;
97 int cur_inode_deleted
;
101 u64 cur_inode_last_extent
;
102 u64 cur_inode_next_write_offset
;
103 bool ignore_cur_inode
;
107 struct list_head new_refs
;
108 struct list_head deleted_refs
;
110 struct radix_tree_root name_cache
;
111 struct list_head name_cache_list
;
114 struct file_ra_state ra
;
119 * We process inodes by their increasing order, so if before an
120 * incremental send we reverse the parent/child relationship of
121 * directories such that a directory with a lower inode number was
122 * the parent of a directory with a higher inode number, and the one
123 * becoming the new parent got renamed too, we can't rename/move the
124 * directory with lower inode number when we finish processing it - we
125 * must process the directory with higher inode number first, then
126 * rename/move it and then rename/move the directory with lower inode
127 * number. Example follows.
129 * Tree state when the first send was performed:
141 * Tree state when the second (incremental) send is performed:
150 * The sequence of steps that lead to the second state was:
152 * mv /a/b/c/d /a/b/c2/d2
153 * mv /a/b/c /a/b/c2/d2/cc
155 * "c" has lower inode number, but we can't move it (2nd mv operation)
156 * before we move "d", which has higher inode number.
158 * So we just memorize which move/rename operations must be performed
159 * later when their respective parent is processed and moved/renamed.
162 /* Indexed by parent directory inode number. */
163 struct rb_root pending_dir_moves
;
166 * Reverse index, indexed by the inode number of a directory that
167 * is waiting for the move/rename of its immediate parent before its
168 * own move/rename can be performed.
170 struct rb_root waiting_dir_moves
;
173 * A directory that is going to be rm'ed might have a child directory
174 * which is in the pending directory moves index above. In this case,
175 * the directory can only be removed after the move/rename of its child
176 * is performed. Example:
196 * Sequence of steps that lead to the send snapshot:
197 * rm -f /a/b/c/foo.txt
199 * mv /a/b/c/x /a/b/YY
202 * When the child is processed, its move/rename is delayed until its
203 * parent is processed (as explained above), but all other operations
204 * like update utimes, chown, chgrp, etc, are performed and the paths
205 * that it uses for those operations must use the orphanized name of
206 * its parent (the directory we're going to rm later), so we need to
207 * memorize that name.
209 * Indexed by the inode number of the directory to be deleted.
211 struct rb_root orphan_dirs
;
214 struct pending_dir_move
{
216 struct list_head list
;
220 struct list_head update_refs
;
223 struct waiting_dir_move
{
227 * There might be some directory that could not be removed because it
228 * was waiting for this directory inode to be moved first. Therefore
229 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
235 struct orphan_dir_info
{
239 u64 last_dir_index_offset
;
242 struct name_cache_entry
{
243 struct list_head list
;
245 * radix_tree has only 32bit entries but we need to handle 64bit inums.
246 * We use the lower 32bit of the 64bit inum to store it in the tree. If
247 * more then one inum would fall into the same entry, we use radix_list
248 * to store the additional entries. radix_list is also used to store
249 * entries where two entries have the same inum but different
252 struct list_head radix_list
;
258 int need_later_update
;
264 static void inconsistent_snapshot_error(struct send_ctx
*sctx
,
265 enum btrfs_compare_tree_result result
,
268 const char *result_string
;
271 case BTRFS_COMPARE_TREE_NEW
:
272 result_string
= "new";
274 case BTRFS_COMPARE_TREE_DELETED
:
275 result_string
= "deleted";
277 case BTRFS_COMPARE_TREE_CHANGED
:
278 result_string
= "updated";
280 case BTRFS_COMPARE_TREE_SAME
:
282 result_string
= "unchanged";
286 result_string
= "unexpected";
289 btrfs_err(sctx
->send_root
->fs_info
,
290 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
291 result_string
, what
, sctx
->cmp_key
->objectid
,
292 sctx
->send_root
->root_key
.objectid
,
294 sctx
->parent_root
->root_key
.objectid
: 0));
297 static int is_waiting_for_move(struct send_ctx
*sctx
, u64 ino
);
299 static struct waiting_dir_move
*
300 get_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
);
302 static int is_waiting_for_rm(struct send_ctx
*sctx
, u64 dir_ino
);
304 static int need_send_hole(struct send_ctx
*sctx
)
306 return (sctx
->parent_root
&& !sctx
->cur_inode_new
&&
307 !sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
&&
308 S_ISREG(sctx
->cur_inode_mode
));
311 static void fs_path_reset(struct fs_path
*p
)
314 p
->start
= p
->buf
+ p
->buf_len
- 1;
324 static struct fs_path
*fs_path_alloc(void)
328 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
332 p
->buf
= p
->inline_buf
;
333 p
->buf_len
= FS_PATH_INLINE_SIZE
;
338 static struct fs_path
*fs_path_alloc_reversed(void)
350 static void fs_path_free(struct fs_path
*p
)
354 if (p
->buf
!= p
->inline_buf
)
359 static int fs_path_len(struct fs_path
*p
)
361 return p
->end
- p
->start
;
364 static int fs_path_ensure_buf(struct fs_path
*p
, int len
)
372 if (p
->buf_len
>= len
)
375 if (len
> PATH_MAX
) {
380 path_len
= p
->end
- p
->start
;
381 old_buf_len
= p
->buf_len
;
384 * First time the inline_buf does not suffice
386 if (p
->buf
== p
->inline_buf
) {
387 tmp_buf
= kmalloc(len
, GFP_KERNEL
);
389 memcpy(tmp_buf
, p
->buf
, old_buf_len
);
391 tmp_buf
= krealloc(p
->buf
, len
, GFP_KERNEL
);
397 * The real size of the buffer is bigger, this will let the fast path
398 * happen most of the time
400 p
->buf_len
= ksize(p
->buf
);
403 tmp_buf
= p
->buf
+ old_buf_len
- path_len
- 1;
404 p
->end
= p
->buf
+ p
->buf_len
- 1;
405 p
->start
= p
->end
- path_len
;
406 memmove(p
->start
, tmp_buf
, path_len
+ 1);
409 p
->end
= p
->start
+ path_len
;
414 static int fs_path_prepare_for_add(struct fs_path
*p
, int name_len
,
420 new_len
= p
->end
- p
->start
+ name_len
;
421 if (p
->start
!= p
->end
)
423 ret
= fs_path_ensure_buf(p
, new_len
);
428 if (p
->start
!= p
->end
)
430 p
->start
-= name_len
;
431 *prepared
= p
->start
;
433 if (p
->start
!= p
->end
)
444 static int fs_path_add(struct fs_path
*p
, const char *name
, int name_len
)
449 ret
= fs_path_prepare_for_add(p
, name_len
, &prepared
);
452 memcpy(prepared
, name
, name_len
);
458 static int fs_path_add_path(struct fs_path
*p
, struct fs_path
*p2
)
463 ret
= fs_path_prepare_for_add(p
, p2
->end
- p2
->start
, &prepared
);
466 memcpy(prepared
, p2
->start
, p2
->end
- p2
->start
);
472 static int fs_path_add_from_extent_buffer(struct fs_path
*p
,
473 struct extent_buffer
*eb
,
474 unsigned long off
, int len
)
479 ret
= fs_path_prepare_for_add(p
, len
, &prepared
);
483 read_extent_buffer(eb
, prepared
, off
, len
);
489 static int fs_path_copy(struct fs_path
*p
, struct fs_path
*from
)
493 p
->reversed
= from
->reversed
;
496 ret
= fs_path_add_path(p
, from
);
502 static void fs_path_unreverse(struct fs_path
*p
)
511 len
= p
->end
- p
->start
;
513 p
->end
= p
->start
+ len
;
514 memmove(p
->start
, tmp
, len
+ 1);
518 static struct btrfs_path
*alloc_path_for_send(void)
520 struct btrfs_path
*path
;
522 path
= btrfs_alloc_path();
525 path
->search_commit_root
= 1;
526 path
->skip_locking
= 1;
527 path
->need_commit_sem
= 1;
531 static int write_buf(struct file
*filp
, const void *buf
, u32 len
, loff_t
*off
)
537 ret
= kernel_write(filp
, buf
+ pos
, len
- pos
, off
);
538 /* TODO handle that correctly */
539 /*if (ret == -ERESTARTSYS) {
553 static int tlv_put(struct send_ctx
*sctx
, u16 attr
, const void *data
, int len
)
555 struct btrfs_tlv_header
*hdr
;
556 int total_len
= sizeof(*hdr
) + len
;
557 int left
= sctx
->send_max_size
- sctx
->send_size
;
559 if (unlikely(left
< total_len
))
562 hdr
= (struct btrfs_tlv_header
*) (sctx
->send_buf
+ sctx
->send_size
);
563 hdr
->tlv_type
= cpu_to_le16(attr
);
564 hdr
->tlv_len
= cpu_to_le16(len
);
565 memcpy(hdr
+ 1, data
, len
);
566 sctx
->send_size
+= total_len
;
571 #define TLV_PUT_DEFINE_INT(bits) \
572 static int tlv_put_u##bits(struct send_ctx *sctx, \
573 u##bits attr, u##bits value) \
575 __le##bits __tmp = cpu_to_le##bits(value); \
576 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
579 TLV_PUT_DEFINE_INT(64)
581 static int tlv_put_string(struct send_ctx
*sctx
, u16 attr
,
582 const char *str
, int len
)
586 return tlv_put(sctx
, attr
, str
, len
);
589 static int tlv_put_uuid(struct send_ctx
*sctx
, u16 attr
,
592 return tlv_put(sctx
, attr
, uuid
, BTRFS_UUID_SIZE
);
595 static int tlv_put_btrfs_timespec(struct send_ctx
*sctx
, u16 attr
,
596 struct extent_buffer
*eb
,
597 struct btrfs_timespec
*ts
)
599 struct btrfs_timespec bts
;
600 read_extent_buffer(eb
, &bts
, (unsigned long)ts
, sizeof(bts
));
601 return tlv_put(sctx
, attr
, &bts
, sizeof(bts
));
605 #define TLV_PUT(sctx, attrtype, data, attrlen) \
607 ret = tlv_put(sctx, attrtype, data, attrlen); \
609 goto tlv_put_failure; \
612 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
614 ret = tlv_put_u##bits(sctx, attrtype, value); \
616 goto tlv_put_failure; \
619 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
620 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
621 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
622 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
623 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
625 ret = tlv_put_string(sctx, attrtype, str, len); \
627 goto tlv_put_failure; \
629 #define TLV_PUT_PATH(sctx, attrtype, p) \
631 ret = tlv_put_string(sctx, attrtype, p->start, \
632 p->end - p->start); \
634 goto tlv_put_failure; \
636 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
638 ret = tlv_put_uuid(sctx, attrtype, uuid); \
640 goto tlv_put_failure; \
642 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
644 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
646 goto tlv_put_failure; \
649 static int send_header(struct send_ctx
*sctx
)
651 struct btrfs_stream_header hdr
;
653 strcpy(hdr
.magic
, BTRFS_SEND_STREAM_MAGIC
);
654 hdr
.version
= cpu_to_le32(BTRFS_SEND_STREAM_VERSION
);
656 return write_buf(sctx
->send_filp
, &hdr
, sizeof(hdr
),
661 * For each command/item we want to send to userspace, we call this function.
663 static int begin_cmd(struct send_ctx
*sctx
, int cmd
)
665 struct btrfs_cmd_header
*hdr
;
667 if (WARN_ON(!sctx
->send_buf
))
670 BUG_ON(sctx
->send_size
);
672 sctx
->send_size
+= sizeof(*hdr
);
673 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
674 hdr
->cmd
= cpu_to_le16(cmd
);
679 static int send_cmd(struct send_ctx
*sctx
)
682 struct btrfs_cmd_header
*hdr
;
685 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
686 hdr
->len
= cpu_to_le32(sctx
->send_size
- sizeof(*hdr
));
689 crc
= crc32c(0, (unsigned char *)sctx
->send_buf
, sctx
->send_size
);
690 hdr
->crc
= cpu_to_le32(crc
);
692 ret
= write_buf(sctx
->send_filp
, sctx
->send_buf
, sctx
->send_size
,
695 sctx
->total_send_size
+= sctx
->send_size
;
696 sctx
->cmd_send_size
[le16_to_cpu(hdr
->cmd
)] += sctx
->send_size
;
703 * Sends a move instruction to user space
705 static int send_rename(struct send_ctx
*sctx
,
706 struct fs_path
*from
, struct fs_path
*to
)
708 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
711 btrfs_debug(fs_info
, "send_rename %s -> %s", from
->start
, to
->start
);
713 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RENAME
);
717 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, from
);
718 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_TO
, to
);
720 ret
= send_cmd(sctx
);
728 * Sends a link instruction to user space
730 static int send_link(struct send_ctx
*sctx
,
731 struct fs_path
*path
, struct fs_path
*lnk
)
733 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
736 btrfs_debug(fs_info
, "send_link %s -> %s", path
->start
, lnk
->start
);
738 ret
= begin_cmd(sctx
, BTRFS_SEND_C_LINK
);
742 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
743 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, lnk
);
745 ret
= send_cmd(sctx
);
753 * Sends an unlink instruction to user space
755 static int send_unlink(struct send_ctx
*sctx
, struct fs_path
*path
)
757 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
760 btrfs_debug(fs_info
, "send_unlink %s", path
->start
);
762 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UNLINK
);
766 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
768 ret
= send_cmd(sctx
);
776 * Sends a rmdir instruction to user space
778 static int send_rmdir(struct send_ctx
*sctx
, struct fs_path
*path
)
780 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
783 btrfs_debug(fs_info
, "send_rmdir %s", path
->start
);
785 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RMDIR
);
789 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
791 ret
= send_cmd(sctx
);
799 * Helper function to retrieve some fields from an inode item.
801 static int __get_inode_info(struct btrfs_root
*root
, struct btrfs_path
*path
,
802 u64 ino
, u64
*size
, u64
*gen
, u64
*mode
, u64
*uid
,
806 struct btrfs_inode_item
*ii
;
807 struct btrfs_key key
;
810 key
.type
= BTRFS_INODE_ITEM_KEY
;
812 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
819 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
820 struct btrfs_inode_item
);
822 *size
= btrfs_inode_size(path
->nodes
[0], ii
);
824 *gen
= btrfs_inode_generation(path
->nodes
[0], ii
);
826 *mode
= btrfs_inode_mode(path
->nodes
[0], ii
);
828 *uid
= btrfs_inode_uid(path
->nodes
[0], ii
);
830 *gid
= btrfs_inode_gid(path
->nodes
[0], ii
);
832 *rdev
= btrfs_inode_rdev(path
->nodes
[0], ii
);
837 static int get_inode_info(struct btrfs_root
*root
,
838 u64 ino
, u64
*size
, u64
*gen
,
839 u64
*mode
, u64
*uid
, u64
*gid
,
842 struct btrfs_path
*path
;
845 path
= alloc_path_for_send();
848 ret
= __get_inode_info(root
, path
, ino
, size
, gen
, mode
, uid
, gid
,
850 btrfs_free_path(path
);
854 typedef int (*iterate_inode_ref_t
)(int num
, u64 dir
, int index
,
859 * Helper function to iterate the entries in ONE btrfs_inode_ref or
860 * btrfs_inode_extref.
861 * The iterate callback may return a non zero value to stop iteration. This can
862 * be a negative value for error codes or 1 to simply stop it.
864 * path must point to the INODE_REF or INODE_EXTREF when called.
866 static int iterate_inode_ref(struct btrfs_root
*root
, struct btrfs_path
*path
,
867 struct btrfs_key
*found_key
, int resolve
,
868 iterate_inode_ref_t iterate
, void *ctx
)
870 struct extent_buffer
*eb
= path
->nodes
[0];
871 struct btrfs_item
*item
;
872 struct btrfs_inode_ref
*iref
;
873 struct btrfs_inode_extref
*extref
;
874 struct btrfs_path
*tmp_path
;
878 int slot
= path
->slots
[0];
885 unsigned long name_off
;
886 unsigned long elem_size
;
889 p
= fs_path_alloc_reversed();
893 tmp_path
= alloc_path_for_send();
900 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
901 ptr
= (unsigned long)btrfs_item_ptr(eb
, slot
,
902 struct btrfs_inode_ref
);
903 item
= btrfs_item_nr(slot
);
904 total
= btrfs_item_size(eb
, item
);
905 elem_size
= sizeof(*iref
);
907 ptr
= btrfs_item_ptr_offset(eb
, slot
);
908 total
= btrfs_item_size_nr(eb
, slot
);
909 elem_size
= sizeof(*extref
);
912 while (cur
< total
) {
915 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
916 iref
= (struct btrfs_inode_ref
*)(ptr
+ cur
);
917 name_len
= btrfs_inode_ref_name_len(eb
, iref
);
918 name_off
= (unsigned long)(iref
+ 1);
919 index
= btrfs_inode_ref_index(eb
, iref
);
920 dir
= found_key
->offset
;
922 extref
= (struct btrfs_inode_extref
*)(ptr
+ cur
);
923 name_len
= btrfs_inode_extref_name_len(eb
, extref
);
924 name_off
= (unsigned long)&extref
->name
;
925 index
= btrfs_inode_extref_index(eb
, extref
);
926 dir
= btrfs_inode_extref_parent(eb
, extref
);
930 start
= btrfs_ref_to_path(root
, tmp_path
, name_len
,
934 ret
= PTR_ERR(start
);
937 if (start
< p
->buf
) {
938 /* overflow , try again with larger buffer */
939 ret
= fs_path_ensure_buf(p
,
940 p
->buf_len
+ p
->buf
- start
);
943 start
= btrfs_ref_to_path(root
, tmp_path
,
948 ret
= PTR_ERR(start
);
951 BUG_ON(start
< p
->buf
);
955 ret
= fs_path_add_from_extent_buffer(p
, eb
, name_off
,
961 cur
+= elem_size
+ name_len
;
962 ret
= iterate(num
, dir
, index
, p
, ctx
);
969 btrfs_free_path(tmp_path
);
974 typedef int (*iterate_dir_item_t
)(int num
, struct btrfs_key
*di_key
,
975 const char *name
, int name_len
,
976 const char *data
, int data_len
,
980 * Helper function to iterate the entries in ONE btrfs_dir_item.
981 * The iterate callback may return a non zero value to stop iteration. This can
982 * be a negative value for error codes or 1 to simply stop it.
984 * path must point to the dir item when called.
986 static int iterate_dir_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
987 iterate_dir_item_t iterate
, void *ctx
)
990 struct extent_buffer
*eb
;
991 struct btrfs_item
*item
;
992 struct btrfs_dir_item
*di
;
993 struct btrfs_key di_key
;
1006 * Start with a small buffer (1 page). If later we end up needing more
1007 * space, which can happen for xattrs on a fs with a leaf size greater
1008 * then the page size, attempt to increase the buffer. Typically xattr
1012 buf
= kmalloc(buf_len
, GFP_KERNEL
);
1018 eb
= path
->nodes
[0];
1019 slot
= path
->slots
[0];
1020 item
= btrfs_item_nr(slot
);
1021 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
1024 total
= btrfs_item_size(eb
, item
);
1027 while (cur
< total
) {
1028 name_len
= btrfs_dir_name_len(eb
, di
);
1029 data_len
= btrfs_dir_data_len(eb
, di
);
1030 type
= btrfs_dir_type(eb
, di
);
1031 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
1033 if (type
== BTRFS_FT_XATTR
) {
1034 if (name_len
> XATTR_NAME_MAX
) {
1035 ret
= -ENAMETOOLONG
;
1038 if (name_len
+ data_len
>
1039 BTRFS_MAX_XATTR_SIZE(root
->fs_info
)) {
1047 if (name_len
+ data_len
> PATH_MAX
) {
1048 ret
= -ENAMETOOLONG
;
1053 if (name_len
+ data_len
> buf_len
) {
1054 buf_len
= name_len
+ data_len
;
1055 if (is_vmalloc_addr(buf
)) {
1059 char *tmp
= krealloc(buf
, buf_len
,
1060 GFP_KERNEL
| __GFP_NOWARN
);
1067 buf
= kvmalloc(buf_len
, GFP_KERNEL
);
1075 read_extent_buffer(eb
, buf
, (unsigned long)(di
+ 1),
1076 name_len
+ data_len
);
1078 len
= sizeof(*di
) + name_len
+ data_len
;
1079 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
1082 ret
= iterate(num
, &di_key
, buf
, name_len
, buf
+ name_len
,
1083 data_len
, type
, ctx
);
1099 static int __copy_first_ref(int num
, u64 dir
, int index
,
1100 struct fs_path
*p
, void *ctx
)
1103 struct fs_path
*pt
= ctx
;
1105 ret
= fs_path_copy(pt
, p
);
1109 /* we want the first only */
1114 * Retrieve the first path of an inode. If an inode has more then one
1115 * ref/hardlink, this is ignored.
1117 static int get_inode_path(struct btrfs_root
*root
,
1118 u64 ino
, struct fs_path
*path
)
1121 struct btrfs_key key
, found_key
;
1122 struct btrfs_path
*p
;
1124 p
= alloc_path_for_send();
1128 fs_path_reset(path
);
1131 key
.type
= BTRFS_INODE_REF_KEY
;
1134 ret
= btrfs_search_slot_for_read(root
, &key
, p
, 1, 0);
1141 btrfs_item_key_to_cpu(p
->nodes
[0], &found_key
, p
->slots
[0]);
1142 if (found_key
.objectid
!= ino
||
1143 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1144 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1149 ret
= iterate_inode_ref(root
, p
, &found_key
, 1,
1150 __copy_first_ref
, path
);
1160 struct backref_ctx
{
1161 struct send_ctx
*sctx
;
1163 struct btrfs_path
*path
;
1164 /* number of total found references */
1168 * used for clones found in send_root. clones found behind cur_objectid
1169 * and cur_offset are not considered as allowed clones.
1174 /* may be truncated in case it's the last extent in a file */
1177 /* data offset in the file extent item */
1180 /* Just to check for bugs in backref resolving */
1184 static int __clone_root_cmp_bsearch(const void *key
, const void *elt
)
1186 u64 root
= (u64
)(uintptr_t)key
;
1187 struct clone_root
*cr
= (struct clone_root
*)elt
;
1189 if (root
< cr
->root
->root_key
.objectid
)
1191 if (root
> cr
->root
->root_key
.objectid
)
1196 static int __clone_root_cmp_sort(const void *e1
, const void *e2
)
1198 struct clone_root
*cr1
= (struct clone_root
*)e1
;
1199 struct clone_root
*cr2
= (struct clone_root
*)e2
;
1201 if (cr1
->root
->root_key
.objectid
< cr2
->root
->root_key
.objectid
)
1203 if (cr1
->root
->root_key
.objectid
> cr2
->root
->root_key
.objectid
)
1209 * Called for every backref that is found for the current extent.
1210 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1212 static int __iterate_backrefs(u64 ino
, u64 offset
, u64 root
, void *ctx_
)
1214 struct backref_ctx
*bctx
= ctx_
;
1215 struct clone_root
*found
;
1219 /* First check if the root is in the list of accepted clone sources */
1220 found
= bsearch((void *)(uintptr_t)root
, bctx
->sctx
->clone_roots
,
1221 bctx
->sctx
->clone_roots_cnt
,
1222 sizeof(struct clone_root
),
1223 __clone_root_cmp_bsearch
);
1227 if (found
->root
== bctx
->sctx
->send_root
&&
1228 ino
== bctx
->cur_objectid
&&
1229 offset
== bctx
->cur_offset
) {
1230 bctx
->found_itself
= 1;
1234 * There are inodes that have extents that lie behind its i_size. Don't
1235 * accept clones from these extents.
1237 ret
= __get_inode_info(found
->root
, bctx
->path
, ino
, &i_size
, NULL
, NULL
,
1239 btrfs_release_path(bctx
->path
);
1243 if (offset
+ bctx
->data_offset
+ bctx
->extent_len
> i_size
)
1247 * Make sure we don't consider clones from send_root that are
1248 * behind the current inode/offset.
1250 if (found
->root
== bctx
->sctx
->send_root
) {
1252 * TODO for the moment we don't accept clones from the inode
1253 * that is currently send. We may change this when
1254 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1257 if (ino
>= bctx
->cur_objectid
)
1262 found
->found_refs
++;
1263 if (ino
< found
->ino
) {
1265 found
->offset
= offset
;
1266 } else if (found
->ino
== ino
) {
1268 * same extent found more then once in the same file.
1270 if (found
->offset
> offset
+ bctx
->extent_len
)
1271 found
->offset
= offset
;
1278 * Given an inode, offset and extent item, it finds a good clone for a clone
1279 * instruction. Returns -ENOENT when none could be found. The function makes
1280 * sure that the returned clone is usable at the point where sending is at the
1281 * moment. This means, that no clones are accepted which lie behind the current
1284 * path must point to the extent item when called.
1286 static int find_extent_clone(struct send_ctx
*sctx
,
1287 struct btrfs_path
*path
,
1288 u64 ino
, u64 data_offset
,
1290 struct clone_root
**found
)
1292 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
1298 u64 extent_item_pos
;
1300 struct btrfs_file_extent_item
*fi
;
1301 struct extent_buffer
*eb
= path
->nodes
[0];
1302 struct backref_ctx
*backref_ctx
= NULL
;
1303 struct clone_root
*cur_clone_root
;
1304 struct btrfs_key found_key
;
1305 struct btrfs_path
*tmp_path
;
1309 tmp_path
= alloc_path_for_send();
1313 /* We only use this path under the commit sem */
1314 tmp_path
->need_commit_sem
= 0;
1316 backref_ctx
= kmalloc(sizeof(*backref_ctx
), GFP_KERNEL
);
1322 backref_ctx
->path
= tmp_path
;
1324 if (data_offset
>= ino_size
) {
1326 * There may be extents that lie behind the file's size.
1327 * I at least had this in combination with snapshotting while
1328 * writing large files.
1334 fi
= btrfs_item_ptr(eb
, path
->slots
[0],
1335 struct btrfs_file_extent_item
);
1336 extent_type
= btrfs_file_extent_type(eb
, fi
);
1337 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1341 compressed
= btrfs_file_extent_compression(eb
, fi
);
1343 num_bytes
= btrfs_file_extent_num_bytes(eb
, fi
);
1344 disk_byte
= btrfs_file_extent_disk_bytenr(eb
, fi
);
1345 if (disk_byte
== 0) {
1349 logical
= disk_byte
+ btrfs_file_extent_offset(eb
, fi
);
1351 down_read(&fs_info
->commit_root_sem
);
1352 ret
= extent_from_logical(fs_info
, disk_byte
, tmp_path
,
1353 &found_key
, &flags
);
1354 up_read(&fs_info
->commit_root_sem
);
1355 btrfs_release_path(tmp_path
);
1359 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1365 * Setup the clone roots.
1367 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1368 cur_clone_root
= sctx
->clone_roots
+ i
;
1369 cur_clone_root
->ino
= (u64
)-1;
1370 cur_clone_root
->offset
= 0;
1371 cur_clone_root
->found_refs
= 0;
1374 backref_ctx
->sctx
= sctx
;
1375 backref_ctx
->found
= 0;
1376 backref_ctx
->cur_objectid
= ino
;
1377 backref_ctx
->cur_offset
= data_offset
;
1378 backref_ctx
->found_itself
= 0;
1379 backref_ctx
->extent_len
= num_bytes
;
1381 * For non-compressed extents iterate_extent_inodes() gives us extent
1382 * offsets that already take into account the data offset, but not for
1383 * compressed extents, since the offset is logical and not relative to
1384 * the physical extent locations. We must take this into account to
1385 * avoid sending clone offsets that go beyond the source file's size,
1386 * which would result in the clone ioctl failing with -EINVAL on the
1389 if (compressed
== BTRFS_COMPRESS_NONE
)
1390 backref_ctx
->data_offset
= 0;
1392 backref_ctx
->data_offset
= btrfs_file_extent_offset(eb
, fi
);
1395 * The last extent of a file may be too large due to page alignment.
1396 * We need to adjust extent_len in this case so that the checks in
1397 * __iterate_backrefs work.
1399 if (data_offset
+ num_bytes
>= ino_size
)
1400 backref_ctx
->extent_len
= ino_size
- data_offset
;
1403 * Now collect all backrefs.
1405 if (compressed
== BTRFS_COMPRESS_NONE
)
1406 extent_item_pos
= logical
- found_key
.objectid
;
1408 extent_item_pos
= 0;
1409 ret
= iterate_extent_inodes(fs_info
, found_key
.objectid
,
1410 extent_item_pos
, 1, __iterate_backrefs
,
1411 backref_ctx
, false);
1416 if (!backref_ctx
->found_itself
) {
1417 /* found a bug in backref code? */
1420 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1421 ino
, data_offset
, disk_byte
, found_key
.objectid
);
1425 btrfs_debug(fs_info
,
1426 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1427 data_offset
, ino
, num_bytes
, logical
);
1429 if (!backref_ctx
->found
)
1430 btrfs_debug(fs_info
, "no clones found");
1432 cur_clone_root
= NULL
;
1433 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1434 if (sctx
->clone_roots
[i
].found_refs
) {
1435 if (!cur_clone_root
)
1436 cur_clone_root
= sctx
->clone_roots
+ i
;
1437 else if (sctx
->clone_roots
[i
].root
== sctx
->send_root
)
1438 /* prefer clones from send_root over others */
1439 cur_clone_root
= sctx
->clone_roots
+ i
;
1444 if (cur_clone_root
) {
1445 *found
= cur_clone_root
;
1452 btrfs_free_path(tmp_path
);
1457 static int read_symlink(struct btrfs_root
*root
,
1459 struct fs_path
*dest
)
1462 struct btrfs_path
*path
;
1463 struct btrfs_key key
;
1464 struct btrfs_file_extent_item
*ei
;
1470 path
= alloc_path_for_send();
1475 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1477 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1482 * An empty symlink inode. Can happen in rare error paths when
1483 * creating a symlink (transaction committed before the inode
1484 * eviction handler removed the symlink inode items and a crash
1485 * happened in between or the subvol was snapshoted in between).
1486 * Print an informative message to dmesg/syslog so that the user
1487 * can delete the symlink.
1489 btrfs_err(root
->fs_info
,
1490 "Found empty symlink inode %llu at root %llu",
1491 ino
, root
->root_key
.objectid
);
1496 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1497 struct btrfs_file_extent_item
);
1498 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
1499 compression
= btrfs_file_extent_compression(path
->nodes
[0], ei
);
1500 BUG_ON(type
!= BTRFS_FILE_EXTENT_INLINE
);
1501 BUG_ON(compression
);
1503 off
= btrfs_file_extent_inline_start(ei
);
1504 len
= btrfs_file_extent_ram_bytes(path
->nodes
[0], ei
);
1506 ret
= fs_path_add_from_extent_buffer(dest
, path
->nodes
[0], off
, len
);
1509 btrfs_free_path(path
);
1514 * Helper function to generate a file name that is unique in the root of
1515 * send_root and parent_root. This is used to generate names for orphan inodes.
1517 static int gen_unique_name(struct send_ctx
*sctx
,
1519 struct fs_path
*dest
)
1522 struct btrfs_path
*path
;
1523 struct btrfs_dir_item
*di
;
1528 path
= alloc_path_for_send();
1533 len
= snprintf(tmp
, sizeof(tmp
), "o%llu-%llu-%llu",
1535 ASSERT(len
< sizeof(tmp
));
1537 di
= btrfs_lookup_dir_item(NULL
, sctx
->send_root
,
1538 path
, BTRFS_FIRST_FREE_OBJECTID
,
1539 tmp
, strlen(tmp
), 0);
1540 btrfs_release_path(path
);
1546 /* not unique, try again */
1551 if (!sctx
->parent_root
) {
1557 di
= btrfs_lookup_dir_item(NULL
, sctx
->parent_root
,
1558 path
, BTRFS_FIRST_FREE_OBJECTID
,
1559 tmp
, strlen(tmp
), 0);
1560 btrfs_release_path(path
);
1566 /* not unique, try again */
1574 ret
= fs_path_add(dest
, tmp
, strlen(tmp
));
1577 btrfs_free_path(path
);
1582 inode_state_no_change
,
1583 inode_state_will_create
,
1584 inode_state_did_create
,
1585 inode_state_will_delete
,
1586 inode_state_did_delete
,
1589 static int get_cur_inode_state(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1597 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &left_gen
, NULL
, NULL
,
1599 if (ret
< 0 && ret
!= -ENOENT
)
1603 if (!sctx
->parent_root
) {
1604 right_ret
= -ENOENT
;
1606 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
, &right_gen
,
1607 NULL
, NULL
, NULL
, NULL
);
1608 if (ret
< 0 && ret
!= -ENOENT
)
1613 if (!left_ret
&& !right_ret
) {
1614 if (left_gen
== gen
&& right_gen
== gen
) {
1615 ret
= inode_state_no_change
;
1616 } else if (left_gen
== gen
) {
1617 if (ino
< sctx
->send_progress
)
1618 ret
= inode_state_did_create
;
1620 ret
= inode_state_will_create
;
1621 } else if (right_gen
== gen
) {
1622 if (ino
< sctx
->send_progress
)
1623 ret
= inode_state_did_delete
;
1625 ret
= inode_state_will_delete
;
1629 } else if (!left_ret
) {
1630 if (left_gen
== gen
) {
1631 if (ino
< sctx
->send_progress
)
1632 ret
= inode_state_did_create
;
1634 ret
= inode_state_will_create
;
1638 } else if (!right_ret
) {
1639 if (right_gen
== gen
) {
1640 if (ino
< sctx
->send_progress
)
1641 ret
= inode_state_did_delete
;
1643 ret
= inode_state_will_delete
;
1655 static int is_inode_existent(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1659 if (ino
== BTRFS_FIRST_FREE_OBJECTID
)
1662 ret
= get_cur_inode_state(sctx
, ino
, gen
);
1666 if (ret
== inode_state_no_change
||
1667 ret
== inode_state_did_create
||
1668 ret
== inode_state_will_delete
)
1678 * Helper function to lookup a dir item in a dir.
1680 static int lookup_dir_item_inode(struct btrfs_root
*root
,
1681 u64 dir
, const char *name
, int name_len
,
1686 struct btrfs_dir_item
*di
;
1687 struct btrfs_key key
;
1688 struct btrfs_path
*path
;
1690 path
= alloc_path_for_send();
1694 di
= btrfs_lookup_dir_item(NULL
, root
, path
,
1695 dir
, name
, name_len
, 0);
1696 if (IS_ERR_OR_NULL(di
)) {
1697 ret
= di
? PTR_ERR(di
) : -ENOENT
;
1700 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1701 if (key
.type
== BTRFS_ROOT_ITEM_KEY
) {
1705 *found_inode
= key
.objectid
;
1706 *found_type
= btrfs_dir_type(path
->nodes
[0], di
);
1709 btrfs_free_path(path
);
1714 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1715 * generation of the parent dir and the name of the dir entry.
1717 static int get_first_ref(struct btrfs_root
*root
, u64 ino
,
1718 u64
*dir
, u64
*dir_gen
, struct fs_path
*name
)
1721 struct btrfs_key key
;
1722 struct btrfs_key found_key
;
1723 struct btrfs_path
*path
;
1727 path
= alloc_path_for_send();
1732 key
.type
= BTRFS_INODE_REF_KEY
;
1735 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
1739 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1741 if (ret
|| found_key
.objectid
!= ino
||
1742 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1743 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1748 if (found_key
.type
== BTRFS_INODE_REF_KEY
) {
1749 struct btrfs_inode_ref
*iref
;
1750 iref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1751 struct btrfs_inode_ref
);
1752 len
= btrfs_inode_ref_name_len(path
->nodes
[0], iref
);
1753 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1754 (unsigned long)(iref
+ 1),
1756 parent_dir
= found_key
.offset
;
1758 struct btrfs_inode_extref
*extref
;
1759 extref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1760 struct btrfs_inode_extref
);
1761 len
= btrfs_inode_extref_name_len(path
->nodes
[0], extref
);
1762 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1763 (unsigned long)&extref
->name
, len
);
1764 parent_dir
= btrfs_inode_extref_parent(path
->nodes
[0], extref
);
1768 btrfs_release_path(path
);
1771 ret
= get_inode_info(root
, parent_dir
, NULL
, dir_gen
, NULL
,
1780 btrfs_free_path(path
);
1784 static int is_first_ref(struct btrfs_root
*root
,
1786 const char *name
, int name_len
)
1789 struct fs_path
*tmp_name
;
1792 tmp_name
= fs_path_alloc();
1796 ret
= get_first_ref(root
, ino
, &tmp_dir
, NULL
, tmp_name
);
1800 if (dir
!= tmp_dir
|| name_len
!= fs_path_len(tmp_name
)) {
1805 ret
= !memcmp(tmp_name
->start
, name
, name_len
);
1808 fs_path_free(tmp_name
);
1813 * Used by process_recorded_refs to determine if a new ref would overwrite an
1814 * already existing ref. In case it detects an overwrite, it returns the
1815 * inode/gen in who_ino/who_gen.
1816 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1817 * to make sure later references to the overwritten inode are possible.
1818 * Orphanizing is however only required for the first ref of an inode.
1819 * process_recorded_refs does an additional is_first_ref check to see if
1820 * orphanizing is really required.
1822 static int will_overwrite_ref(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
1823 const char *name
, int name_len
,
1824 u64
*who_ino
, u64
*who_gen
, u64
*who_mode
)
1828 u64 other_inode
= 0;
1831 if (!sctx
->parent_root
)
1834 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1839 * If we have a parent root we need to verify that the parent dir was
1840 * not deleted and then re-created, if it was then we have no overwrite
1841 * and we can just unlink this entry.
1843 if (sctx
->parent_root
&& dir
!= BTRFS_FIRST_FREE_OBJECTID
) {
1844 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &gen
, NULL
,
1846 if (ret
< 0 && ret
!= -ENOENT
)
1856 ret
= lookup_dir_item_inode(sctx
->parent_root
, dir
, name
, name_len
,
1857 &other_inode
, &other_type
);
1858 if (ret
< 0 && ret
!= -ENOENT
)
1866 * Check if the overwritten ref was already processed. If yes, the ref
1867 * was already unlinked/moved, so we can safely assume that we will not
1868 * overwrite anything at this point in time.
1870 if (other_inode
> sctx
->send_progress
||
1871 is_waiting_for_move(sctx
, other_inode
)) {
1872 ret
= get_inode_info(sctx
->parent_root
, other_inode
, NULL
,
1873 who_gen
, who_mode
, NULL
, NULL
, NULL
);
1878 *who_ino
= other_inode
;
1888 * Checks if the ref was overwritten by an already processed inode. This is
1889 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1890 * thus the orphan name needs be used.
1891 * process_recorded_refs also uses it to avoid unlinking of refs that were
1894 static int did_overwrite_ref(struct send_ctx
*sctx
,
1895 u64 dir
, u64 dir_gen
,
1896 u64 ino
, u64 ino_gen
,
1897 const char *name
, int name_len
)
1904 if (!sctx
->parent_root
)
1907 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1911 if (dir
!= BTRFS_FIRST_FREE_OBJECTID
) {
1912 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &gen
, NULL
,
1914 if (ret
< 0 && ret
!= -ENOENT
)
1924 /* check if the ref was overwritten by another ref */
1925 ret
= lookup_dir_item_inode(sctx
->send_root
, dir
, name
, name_len
,
1926 &ow_inode
, &other_type
);
1927 if (ret
< 0 && ret
!= -ENOENT
)
1930 /* was never and will never be overwritten */
1935 ret
= get_inode_info(sctx
->send_root
, ow_inode
, NULL
, &gen
, NULL
, NULL
,
1940 if (ow_inode
== ino
&& gen
== ino_gen
) {
1946 * We know that it is or will be overwritten. Check this now.
1947 * The current inode being processed might have been the one that caused
1948 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1949 * the current inode being processed.
1951 if ((ow_inode
< sctx
->send_progress
) ||
1952 (ino
!= sctx
->cur_ino
&& ow_inode
== sctx
->cur_ino
&&
1953 gen
== sctx
->cur_inode_gen
))
1963 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1964 * that got overwritten. This is used by process_recorded_refs to determine
1965 * if it has to use the path as returned by get_cur_path or the orphan name.
1967 static int did_overwrite_first_ref(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1970 struct fs_path
*name
= NULL
;
1974 if (!sctx
->parent_root
)
1977 name
= fs_path_alloc();
1981 ret
= get_first_ref(sctx
->parent_root
, ino
, &dir
, &dir_gen
, name
);
1985 ret
= did_overwrite_ref(sctx
, dir
, dir_gen
, ino
, gen
,
1986 name
->start
, fs_path_len(name
));
1994 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1995 * so we need to do some special handling in case we have clashes. This function
1996 * takes care of this with the help of name_cache_entry::radix_list.
1997 * In case of error, nce is kfreed.
1999 static int name_cache_insert(struct send_ctx
*sctx
,
2000 struct name_cache_entry
*nce
)
2003 struct list_head
*nce_head
;
2005 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
2006 (unsigned long)nce
->ino
);
2008 nce_head
= kmalloc(sizeof(*nce_head
), GFP_KERNEL
);
2013 INIT_LIST_HEAD(nce_head
);
2015 ret
= radix_tree_insert(&sctx
->name_cache
, nce
->ino
, nce_head
);
2022 list_add_tail(&nce
->radix_list
, nce_head
);
2023 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
2024 sctx
->name_cache_size
++;
2029 static void name_cache_delete(struct send_ctx
*sctx
,
2030 struct name_cache_entry
*nce
)
2032 struct list_head
*nce_head
;
2034 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
2035 (unsigned long)nce
->ino
);
2037 btrfs_err(sctx
->send_root
->fs_info
,
2038 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2039 nce
->ino
, sctx
->name_cache_size
);
2042 list_del(&nce
->radix_list
);
2043 list_del(&nce
->list
);
2044 sctx
->name_cache_size
--;
2047 * We may not get to the final release of nce_head if the lookup fails
2049 if (nce_head
&& list_empty(nce_head
)) {
2050 radix_tree_delete(&sctx
->name_cache
, (unsigned long)nce
->ino
);
2055 static struct name_cache_entry
*name_cache_search(struct send_ctx
*sctx
,
2058 struct list_head
*nce_head
;
2059 struct name_cache_entry
*cur
;
2061 nce_head
= radix_tree_lookup(&sctx
->name_cache
, (unsigned long)ino
);
2065 list_for_each_entry(cur
, nce_head
, radix_list
) {
2066 if (cur
->ino
== ino
&& cur
->gen
== gen
)
2073 * Removes the entry from the list and adds it back to the end. This marks the
2074 * entry as recently used so that name_cache_clean_unused does not remove it.
2076 static void name_cache_used(struct send_ctx
*sctx
, struct name_cache_entry
*nce
)
2078 list_del(&nce
->list
);
2079 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
2083 * Remove some entries from the beginning of name_cache_list.
2085 static void name_cache_clean_unused(struct send_ctx
*sctx
)
2087 struct name_cache_entry
*nce
;
2089 if (sctx
->name_cache_size
< SEND_CTX_NAME_CACHE_CLEAN_SIZE
)
2092 while (sctx
->name_cache_size
> SEND_CTX_MAX_NAME_CACHE_SIZE
) {
2093 nce
= list_entry(sctx
->name_cache_list
.next
,
2094 struct name_cache_entry
, list
);
2095 name_cache_delete(sctx
, nce
);
2100 static void name_cache_free(struct send_ctx
*sctx
)
2102 struct name_cache_entry
*nce
;
2104 while (!list_empty(&sctx
->name_cache_list
)) {
2105 nce
= list_entry(sctx
->name_cache_list
.next
,
2106 struct name_cache_entry
, list
);
2107 name_cache_delete(sctx
, nce
);
2113 * Used by get_cur_path for each ref up to the root.
2114 * Returns 0 if it succeeded.
2115 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2116 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2117 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2118 * Returns <0 in case of error.
2120 static int __get_cur_name_and_parent(struct send_ctx
*sctx
,
2124 struct fs_path
*dest
)
2128 struct name_cache_entry
*nce
= NULL
;
2131 * First check if we already did a call to this function with the same
2132 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2133 * return the cached result.
2135 nce
= name_cache_search(sctx
, ino
, gen
);
2137 if (ino
< sctx
->send_progress
&& nce
->need_later_update
) {
2138 name_cache_delete(sctx
, nce
);
2142 name_cache_used(sctx
, nce
);
2143 *parent_ino
= nce
->parent_ino
;
2144 *parent_gen
= nce
->parent_gen
;
2145 ret
= fs_path_add(dest
, nce
->name
, nce
->name_len
);
2154 * If the inode is not existent yet, add the orphan name and return 1.
2155 * This should only happen for the parent dir that we determine in
2158 ret
= is_inode_existent(sctx
, ino
, gen
);
2163 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2171 * Depending on whether the inode was already processed or not, use
2172 * send_root or parent_root for ref lookup.
2174 if (ino
< sctx
->send_progress
)
2175 ret
= get_first_ref(sctx
->send_root
, ino
,
2176 parent_ino
, parent_gen
, dest
);
2178 ret
= get_first_ref(sctx
->parent_root
, ino
,
2179 parent_ino
, parent_gen
, dest
);
2184 * Check if the ref was overwritten by an inode's ref that was processed
2185 * earlier. If yes, treat as orphan and return 1.
2187 ret
= did_overwrite_ref(sctx
, *parent_ino
, *parent_gen
, ino
, gen
,
2188 dest
->start
, dest
->end
- dest
->start
);
2192 fs_path_reset(dest
);
2193 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2201 * Store the result of the lookup in the name cache.
2203 nce
= kmalloc(sizeof(*nce
) + fs_path_len(dest
) + 1, GFP_KERNEL
);
2211 nce
->parent_ino
= *parent_ino
;
2212 nce
->parent_gen
= *parent_gen
;
2213 nce
->name_len
= fs_path_len(dest
);
2215 strcpy(nce
->name
, dest
->start
);
2217 if (ino
< sctx
->send_progress
)
2218 nce
->need_later_update
= 0;
2220 nce
->need_later_update
= 1;
2222 nce_ret
= name_cache_insert(sctx
, nce
);
2225 name_cache_clean_unused(sctx
);
2232 * Magic happens here. This function returns the first ref to an inode as it
2233 * would look like while receiving the stream at this point in time.
2234 * We walk the path up to the root. For every inode in between, we check if it
2235 * was already processed/sent. If yes, we continue with the parent as found
2236 * in send_root. If not, we continue with the parent as found in parent_root.
2237 * If we encounter an inode that was deleted at this point in time, we use the
2238 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2239 * that were not created yet and overwritten inodes/refs.
2241 * When do we have have orphan inodes:
2242 * 1. When an inode is freshly created and thus no valid refs are available yet
2243 * 2. When a directory lost all it's refs (deleted) but still has dir items
2244 * inside which were not processed yet (pending for move/delete). If anyone
2245 * tried to get the path to the dir items, it would get a path inside that
2247 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2248 * of an unprocessed inode. If in that case the first ref would be
2249 * overwritten, the overwritten inode gets "orphanized". Later when we
2250 * process this overwritten inode, it is restored at a new place by moving
2253 * sctx->send_progress tells this function at which point in time receiving
2256 static int get_cur_path(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2257 struct fs_path
*dest
)
2260 struct fs_path
*name
= NULL
;
2261 u64 parent_inode
= 0;
2265 name
= fs_path_alloc();
2272 fs_path_reset(dest
);
2274 while (!stop
&& ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
2275 struct waiting_dir_move
*wdm
;
2277 fs_path_reset(name
);
2279 if (is_waiting_for_rm(sctx
, ino
)) {
2280 ret
= gen_unique_name(sctx
, ino
, gen
, name
);
2283 ret
= fs_path_add_path(dest
, name
);
2287 wdm
= get_waiting_dir_move(sctx
, ino
);
2288 if (wdm
&& wdm
->orphanized
) {
2289 ret
= gen_unique_name(sctx
, ino
, gen
, name
);
2292 ret
= get_first_ref(sctx
->parent_root
, ino
,
2293 &parent_inode
, &parent_gen
, name
);
2295 ret
= __get_cur_name_and_parent(sctx
, ino
, gen
,
2305 ret
= fs_path_add_path(dest
, name
);
2316 fs_path_unreverse(dest
);
2321 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2323 static int send_subvol_begin(struct send_ctx
*sctx
)
2326 struct btrfs_root
*send_root
= sctx
->send_root
;
2327 struct btrfs_root
*parent_root
= sctx
->parent_root
;
2328 struct btrfs_path
*path
;
2329 struct btrfs_key key
;
2330 struct btrfs_root_ref
*ref
;
2331 struct extent_buffer
*leaf
;
2335 path
= btrfs_alloc_path();
2339 name
= kmalloc(BTRFS_PATH_NAME_MAX
, GFP_KERNEL
);
2341 btrfs_free_path(path
);
2345 key
.objectid
= send_root
->root_key
.objectid
;
2346 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2349 ret
= btrfs_search_slot_for_read(send_root
->fs_info
->tree_root
,
2358 leaf
= path
->nodes
[0];
2359 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2360 if (key
.type
!= BTRFS_ROOT_BACKREF_KEY
||
2361 key
.objectid
!= send_root
->root_key
.objectid
) {
2365 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
2366 namelen
= btrfs_root_ref_name_len(leaf
, ref
);
2367 read_extent_buffer(leaf
, name
, (unsigned long)(ref
+ 1), namelen
);
2368 btrfs_release_path(path
);
2371 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SNAPSHOT
);
2375 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SUBVOL
);
2380 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_PATH
, name
, namelen
);
2382 if (!btrfs_is_empty_uuid(sctx
->send_root
->root_item
.received_uuid
))
2383 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_UUID
,
2384 sctx
->send_root
->root_item
.received_uuid
);
2386 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_UUID
,
2387 sctx
->send_root
->root_item
.uuid
);
2389 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CTRANSID
,
2390 le64_to_cpu(sctx
->send_root
->root_item
.ctransid
));
2392 if (!btrfs_is_empty_uuid(parent_root
->root_item
.received_uuid
))
2393 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
2394 parent_root
->root_item
.received_uuid
);
2396 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
2397 parent_root
->root_item
.uuid
);
2398 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
2399 le64_to_cpu(sctx
->parent_root
->root_item
.ctransid
));
2402 ret
= send_cmd(sctx
);
2406 btrfs_free_path(path
);
2411 static int send_truncate(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 size
)
2413 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
2417 btrfs_debug(fs_info
, "send_truncate %llu size=%llu", ino
, size
);
2419 p
= fs_path_alloc();
2423 ret
= begin_cmd(sctx
, BTRFS_SEND_C_TRUNCATE
);
2427 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2430 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2431 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, size
);
2433 ret
= send_cmd(sctx
);
2441 static int send_chmod(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 mode
)
2443 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
2447 btrfs_debug(fs_info
, "send_chmod %llu mode=%llu", ino
, mode
);
2449 p
= fs_path_alloc();
2453 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHMOD
);
2457 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2460 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2461 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
& 07777);
2463 ret
= send_cmd(sctx
);
2471 static int send_chown(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 uid
, u64 gid
)
2473 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
2477 btrfs_debug(fs_info
, "send_chown %llu uid=%llu, gid=%llu",
2480 p
= fs_path_alloc();
2484 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHOWN
);
2488 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2491 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2492 TLV_PUT_U64(sctx
, BTRFS_SEND_A_UID
, uid
);
2493 TLV_PUT_U64(sctx
, BTRFS_SEND_A_GID
, gid
);
2495 ret
= send_cmd(sctx
);
2503 static int send_utimes(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
2505 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
2507 struct fs_path
*p
= NULL
;
2508 struct btrfs_inode_item
*ii
;
2509 struct btrfs_path
*path
= NULL
;
2510 struct extent_buffer
*eb
;
2511 struct btrfs_key key
;
2514 btrfs_debug(fs_info
, "send_utimes %llu", ino
);
2516 p
= fs_path_alloc();
2520 path
= alloc_path_for_send();
2527 key
.type
= BTRFS_INODE_ITEM_KEY
;
2529 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2535 eb
= path
->nodes
[0];
2536 slot
= path
->slots
[0];
2537 ii
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
2539 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UTIMES
);
2543 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2546 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2547 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_ATIME
, eb
, &ii
->atime
);
2548 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_MTIME
, eb
, &ii
->mtime
);
2549 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_CTIME
, eb
, &ii
->ctime
);
2550 /* TODO Add otime support when the otime patches get into upstream */
2552 ret
= send_cmd(sctx
);
2557 btrfs_free_path(path
);
2562 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2563 * a valid path yet because we did not process the refs yet. So, the inode
2564 * is created as orphan.
2566 static int send_create_inode(struct send_ctx
*sctx
, u64 ino
)
2568 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
2576 btrfs_debug(fs_info
, "send_create_inode %llu", ino
);
2578 p
= fs_path_alloc();
2582 if (ino
!= sctx
->cur_ino
) {
2583 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &gen
, &mode
,
2588 gen
= sctx
->cur_inode_gen
;
2589 mode
= sctx
->cur_inode_mode
;
2590 rdev
= sctx
->cur_inode_rdev
;
2593 if (S_ISREG(mode
)) {
2594 cmd
= BTRFS_SEND_C_MKFILE
;
2595 } else if (S_ISDIR(mode
)) {
2596 cmd
= BTRFS_SEND_C_MKDIR
;
2597 } else if (S_ISLNK(mode
)) {
2598 cmd
= BTRFS_SEND_C_SYMLINK
;
2599 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
2600 cmd
= BTRFS_SEND_C_MKNOD
;
2601 } else if (S_ISFIFO(mode
)) {
2602 cmd
= BTRFS_SEND_C_MKFIFO
;
2603 } else if (S_ISSOCK(mode
)) {
2604 cmd
= BTRFS_SEND_C_MKSOCK
;
2606 btrfs_warn(sctx
->send_root
->fs_info
, "unexpected inode type %o",
2607 (int)(mode
& S_IFMT
));
2612 ret
= begin_cmd(sctx
, cmd
);
2616 ret
= gen_unique_name(sctx
, ino
, gen
, p
);
2620 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2621 TLV_PUT_U64(sctx
, BTRFS_SEND_A_INO
, ino
);
2623 if (S_ISLNK(mode
)) {
2625 ret
= read_symlink(sctx
->send_root
, ino
, p
);
2628 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, p
);
2629 } else if (S_ISCHR(mode
) || S_ISBLK(mode
) ||
2630 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
2631 TLV_PUT_U64(sctx
, BTRFS_SEND_A_RDEV
, new_encode_dev(rdev
));
2632 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
);
2635 ret
= send_cmd(sctx
);
2647 * We need some special handling for inodes that get processed before the parent
2648 * directory got created. See process_recorded_refs for details.
2649 * This function does the check if we already created the dir out of order.
2651 static int did_create_dir(struct send_ctx
*sctx
, u64 dir
)
2654 struct btrfs_path
*path
= NULL
;
2655 struct btrfs_key key
;
2656 struct btrfs_key found_key
;
2657 struct btrfs_key di_key
;
2658 struct extent_buffer
*eb
;
2659 struct btrfs_dir_item
*di
;
2662 path
= alloc_path_for_send();
2669 key
.type
= BTRFS_DIR_INDEX_KEY
;
2671 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2676 eb
= path
->nodes
[0];
2677 slot
= path
->slots
[0];
2678 if (slot
>= btrfs_header_nritems(eb
)) {
2679 ret
= btrfs_next_leaf(sctx
->send_root
, path
);
2682 } else if (ret
> 0) {
2689 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
2690 if (found_key
.objectid
!= key
.objectid
||
2691 found_key
.type
!= key
.type
) {
2696 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
2697 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
2699 if (di_key
.type
!= BTRFS_ROOT_ITEM_KEY
&&
2700 di_key
.objectid
< sctx
->send_progress
) {
2709 btrfs_free_path(path
);
2714 * Only creates the inode if it is:
2715 * 1. Not a directory
2716 * 2. Or a directory which was not created already due to out of order
2717 * directories. See did_create_dir and process_recorded_refs for details.
2719 static int send_create_inode_if_needed(struct send_ctx
*sctx
)
2723 if (S_ISDIR(sctx
->cur_inode_mode
)) {
2724 ret
= did_create_dir(sctx
, sctx
->cur_ino
);
2733 ret
= send_create_inode(sctx
, sctx
->cur_ino
);
2741 struct recorded_ref
{
2742 struct list_head list
;
2744 struct fs_path
*full_path
;
2750 static void set_ref_path(struct recorded_ref
*ref
, struct fs_path
*path
)
2752 ref
->full_path
= path
;
2753 ref
->name
= (char *)kbasename(ref
->full_path
->start
);
2754 ref
->name_len
= ref
->full_path
->end
- ref
->name
;
2758 * We need to process new refs before deleted refs, but compare_tree gives us
2759 * everything mixed. So we first record all refs and later process them.
2760 * This function is a helper to record one ref.
2762 static int __record_ref(struct list_head
*head
, u64 dir
,
2763 u64 dir_gen
, struct fs_path
*path
)
2765 struct recorded_ref
*ref
;
2767 ref
= kmalloc(sizeof(*ref
), GFP_KERNEL
);
2772 ref
->dir_gen
= dir_gen
;
2773 set_ref_path(ref
, path
);
2774 list_add_tail(&ref
->list
, head
);
2778 static int dup_ref(struct recorded_ref
*ref
, struct list_head
*list
)
2780 struct recorded_ref
*new;
2782 new = kmalloc(sizeof(*ref
), GFP_KERNEL
);
2786 new->dir
= ref
->dir
;
2787 new->dir_gen
= ref
->dir_gen
;
2788 new->full_path
= NULL
;
2789 INIT_LIST_HEAD(&new->list
);
2790 list_add_tail(&new->list
, list
);
2794 static void __free_recorded_refs(struct list_head
*head
)
2796 struct recorded_ref
*cur
;
2798 while (!list_empty(head
)) {
2799 cur
= list_entry(head
->next
, struct recorded_ref
, list
);
2800 fs_path_free(cur
->full_path
);
2801 list_del(&cur
->list
);
2806 static void free_recorded_refs(struct send_ctx
*sctx
)
2808 __free_recorded_refs(&sctx
->new_refs
);
2809 __free_recorded_refs(&sctx
->deleted_refs
);
2813 * Renames/moves a file/dir to its orphan name. Used when the first
2814 * ref of an unprocessed inode gets overwritten and for all non empty
2817 static int orphanize_inode(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2818 struct fs_path
*path
)
2821 struct fs_path
*orphan
;
2823 orphan
= fs_path_alloc();
2827 ret
= gen_unique_name(sctx
, ino
, gen
, orphan
);
2831 ret
= send_rename(sctx
, path
, orphan
);
2834 fs_path_free(orphan
);
2838 static struct orphan_dir_info
*
2839 add_orphan_dir_info(struct send_ctx
*sctx
, u64 dir_ino
)
2841 struct rb_node
**p
= &sctx
->orphan_dirs
.rb_node
;
2842 struct rb_node
*parent
= NULL
;
2843 struct orphan_dir_info
*entry
, *odi
;
2847 entry
= rb_entry(parent
, struct orphan_dir_info
, node
);
2848 if (dir_ino
< entry
->ino
) {
2850 } else if (dir_ino
> entry
->ino
) {
2851 p
= &(*p
)->rb_right
;
2857 odi
= kmalloc(sizeof(*odi
), GFP_KERNEL
);
2859 return ERR_PTR(-ENOMEM
);
2862 odi
->last_dir_index_offset
= 0;
2864 rb_link_node(&odi
->node
, parent
, p
);
2865 rb_insert_color(&odi
->node
, &sctx
->orphan_dirs
);
2869 static struct orphan_dir_info
*
2870 get_orphan_dir_info(struct send_ctx
*sctx
, u64 dir_ino
)
2872 struct rb_node
*n
= sctx
->orphan_dirs
.rb_node
;
2873 struct orphan_dir_info
*entry
;
2876 entry
= rb_entry(n
, struct orphan_dir_info
, node
);
2877 if (dir_ino
< entry
->ino
)
2879 else if (dir_ino
> entry
->ino
)
2887 static int is_waiting_for_rm(struct send_ctx
*sctx
, u64 dir_ino
)
2889 struct orphan_dir_info
*odi
= get_orphan_dir_info(sctx
, dir_ino
);
2894 static void free_orphan_dir_info(struct send_ctx
*sctx
,
2895 struct orphan_dir_info
*odi
)
2899 rb_erase(&odi
->node
, &sctx
->orphan_dirs
);
2904 * Returns 1 if a directory can be removed at this point in time.
2905 * We check this by iterating all dir items and checking if the inode behind
2906 * the dir item was already processed.
2908 static int can_rmdir(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
2912 struct btrfs_root
*root
= sctx
->parent_root
;
2913 struct btrfs_path
*path
;
2914 struct btrfs_key key
;
2915 struct btrfs_key found_key
;
2916 struct btrfs_key loc
;
2917 struct btrfs_dir_item
*di
;
2918 struct orphan_dir_info
*odi
= NULL
;
2921 * Don't try to rmdir the top/root subvolume dir.
2923 if (dir
== BTRFS_FIRST_FREE_OBJECTID
)
2926 path
= alloc_path_for_send();
2931 key
.type
= BTRFS_DIR_INDEX_KEY
;
2934 odi
= get_orphan_dir_info(sctx
, dir
);
2936 key
.offset
= odi
->last_dir_index_offset
;
2938 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2943 struct waiting_dir_move
*dm
;
2945 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
2946 ret
= btrfs_next_leaf(root
, path
);
2953 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2955 if (found_key
.objectid
!= key
.objectid
||
2956 found_key
.type
!= key
.type
)
2959 di
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2960 struct btrfs_dir_item
);
2961 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &loc
);
2963 dm
= get_waiting_dir_move(sctx
, loc
.objectid
);
2965 odi
= add_orphan_dir_info(sctx
, dir
);
2971 odi
->last_dir_index_offset
= found_key
.offset
;
2972 dm
->rmdir_ino
= dir
;
2977 if (loc
.objectid
> send_progress
) {
2978 odi
= add_orphan_dir_info(sctx
, dir
);
2984 odi
->last_dir_index_offset
= found_key
.offset
;
2991 free_orphan_dir_info(sctx
, odi
);
2996 btrfs_free_path(path
);
3000 static int is_waiting_for_move(struct send_ctx
*sctx
, u64 ino
)
3002 struct waiting_dir_move
*entry
= get_waiting_dir_move(sctx
, ino
);
3004 return entry
!= NULL
;
3007 static int add_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
, bool orphanized
)
3009 struct rb_node
**p
= &sctx
->waiting_dir_moves
.rb_node
;
3010 struct rb_node
*parent
= NULL
;
3011 struct waiting_dir_move
*entry
, *dm
;
3013 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
))
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
;
3207 name
= fs_path_alloc();
3208 from_path
= fs_path_alloc();
3209 if (!name
|| !from_path
) {
3214 dm
= get_waiting_dir_move(sctx
, pm
->ino
);
3216 rmdir_ino
= dm
->rmdir_ino
;
3217 is_orphan
= dm
->orphanized
;
3218 free_waiting_dir_move(sctx
, dm
);
3221 ret
= gen_unique_name(sctx
, pm
->ino
,
3222 pm
->gen
, from_path
);
3224 ret
= get_first_ref(sctx
->parent_root
, pm
->ino
,
3225 &parent_ino
, &parent_gen
, name
);
3228 ret
= get_cur_path(sctx
, parent_ino
, parent_gen
,
3232 ret
= fs_path_add_path(from_path
, name
);
3237 sctx
->send_progress
= sctx
->cur_ino
+ 1;
3238 ret
= path_loop(sctx
, name
, pm
->ino
, pm
->gen
, &ancestor
);
3242 LIST_HEAD(deleted_refs
);
3243 ASSERT(ancestor
> BTRFS_FIRST_FREE_OBJECTID
);
3244 ret
= add_pending_dir_move(sctx
, pm
->ino
, pm
->gen
, ancestor
,
3245 &pm
->update_refs
, &deleted_refs
,
3250 dm
= get_waiting_dir_move(sctx
, pm
->ino
);
3252 dm
->rmdir_ino
= rmdir_ino
;
3256 fs_path_reset(name
);
3259 ret
= get_cur_path(sctx
, pm
->ino
, pm
->gen
, to_path
);
3263 ret
= send_rename(sctx
, from_path
, to_path
);
3268 struct orphan_dir_info
*odi
;
3271 odi
= get_orphan_dir_info(sctx
, rmdir_ino
);
3273 /* already deleted */
3278 ret
= can_rmdir(sctx
, rmdir_ino
, gen
, sctx
->cur_ino
);
3284 name
= fs_path_alloc();
3289 ret
= get_cur_path(sctx
, rmdir_ino
, gen
, name
);
3292 ret
= send_rmdir(sctx
, name
);
3298 ret
= send_utimes(sctx
, pm
->ino
, pm
->gen
);
3303 * After rename/move, need to update the utimes of both new parent(s)
3304 * and old parent(s).
3306 list_for_each_entry(cur
, &pm
->update_refs
, list
) {
3308 * The parent inode might have been deleted in the send snapshot
3310 ret
= get_inode_info(sctx
->send_root
, cur
->dir
, NULL
,
3311 NULL
, NULL
, NULL
, NULL
, NULL
);
3312 if (ret
== -ENOENT
) {
3319 ret
= send_utimes(sctx
, cur
->dir
, cur
->dir_gen
);
3326 fs_path_free(from_path
);
3327 fs_path_free(to_path
);
3328 sctx
->send_progress
= orig_progress
;
3333 static void free_pending_move(struct send_ctx
*sctx
, struct pending_dir_move
*m
)
3335 if (!list_empty(&m
->list
))
3337 if (!RB_EMPTY_NODE(&m
->node
))
3338 rb_erase(&m
->node
, &sctx
->pending_dir_moves
);
3339 __free_recorded_refs(&m
->update_refs
);
3343 static void tail_append_pending_moves(struct send_ctx
*sctx
,
3344 struct pending_dir_move
*moves
,
3345 struct list_head
*stack
)
3347 if (list_empty(&moves
->list
)) {
3348 list_add_tail(&moves
->list
, stack
);
3351 list_splice_init(&moves
->list
, &list
);
3352 list_add_tail(&moves
->list
, stack
);
3353 list_splice_tail(&list
, stack
);
3355 if (!RB_EMPTY_NODE(&moves
->node
)) {
3356 rb_erase(&moves
->node
, &sctx
->pending_dir_moves
);
3357 RB_CLEAR_NODE(&moves
->node
);
3361 static int apply_children_dir_moves(struct send_ctx
*sctx
)
3363 struct pending_dir_move
*pm
;
3364 struct list_head stack
;
3365 u64 parent_ino
= sctx
->cur_ino
;
3368 pm
= get_pending_dir_moves(sctx
, parent_ino
);
3372 INIT_LIST_HEAD(&stack
);
3373 tail_append_pending_moves(sctx
, pm
, &stack
);
3375 while (!list_empty(&stack
)) {
3376 pm
= list_first_entry(&stack
, struct pending_dir_move
, list
);
3377 parent_ino
= pm
->ino
;
3378 ret
= apply_dir_move(sctx
, pm
);
3379 free_pending_move(sctx
, pm
);
3382 pm
= get_pending_dir_moves(sctx
, parent_ino
);
3384 tail_append_pending_moves(sctx
, pm
, &stack
);
3389 while (!list_empty(&stack
)) {
3390 pm
= list_first_entry(&stack
, struct pending_dir_move
, list
);
3391 free_pending_move(sctx
, pm
);
3397 * We might need to delay a directory rename even when no ancestor directory
3398 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3399 * renamed. This happens when we rename a directory to the old name (the name
3400 * in the parent root) of some other unrelated directory that got its rename
3401 * delayed due to some ancestor with higher number that got renamed.
3407 * |---- a/ (ino 257)
3408 * | |---- file (ino 260)
3410 * |---- b/ (ino 258)
3411 * |---- c/ (ino 259)
3415 * |---- a/ (ino 258)
3416 * |---- x/ (ino 259)
3417 * |---- y/ (ino 257)
3418 * |----- file (ino 260)
3420 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3421 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3422 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3425 * 1 - rename 259 from 'c' to 'x'
3426 * 2 - rename 257 from 'a' to 'x/y'
3427 * 3 - rename 258 from 'b' to 'a'
3429 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3430 * be done right away and < 0 on error.
3432 static int wait_for_dest_dir_move(struct send_ctx
*sctx
,
3433 struct recorded_ref
*parent_ref
,
3434 const bool is_orphan
)
3436 struct btrfs_fs_info
*fs_info
= sctx
->parent_root
->fs_info
;
3437 struct btrfs_path
*path
;
3438 struct btrfs_key key
;
3439 struct btrfs_key di_key
;
3440 struct btrfs_dir_item
*di
;
3444 struct waiting_dir_move
*wdm
;
3446 if (RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
))
3449 path
= alloc_path_for_send();
3453 key
.objectid
= parent_ref
->dir
;
3454 key
.type
= BTRFS_DIR_ITEM_KEY
;
3455 key
.offset
= btrfs_name_hash(parent_ref
->name
, parent_ref
->name_len
);
3457 ret
= btrfs_search_slot(NULL
, sctx
->parent_root
, &key
, path
, 0, 0);
3460 } else if (ret
> 0) {
3465 di
= btrfs_match_dir_item_name(fs_info
, path
, parent_ref
->name
,
3466 parent_ref
->name_len
);
3472 * di_key.objectid has the number of the inode that has a dentry in the
3473 * parent directory with the same name that sctx->cur_ino is being
3474 * renamed to. We need to check if that inode is in the send root as
3475 * well and if it is currently marked as an inode with a pending rename,
3476 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3477 * that it happens after that other inode is renamed.
3479 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &di_key
);
3480 if (di_key
.type
!= BTRFS_INODE_ITEM_KEY
) {
3485 ret
= get_inode_info(sctx
->parent_root
, di_key
.objectid
, NULL
,
3486 &left_gen
, NULL
, NULL
, NULL
, NULL
);
3489 ret
= get_inode_info(sctx
->send_root
, di_key
.objectid
, NULL
,
3490 &right_gen
, NULL
, NULL
, NULL
, NULL
);
3497 /* Different inode, no need to delay the rename of sctx->cur_ino */
3498 if (right_gen
!= left_gen
) {
3503 wdm
= get_waiting_dir_move(sctx
, di_key
.objectid
);
3504 if (wdm
&& !wdm
->orphanized
) {
3505 ret
= add_pending_dir_move(sctx
,
3507 sctx
->cur_inode_gen
,
3510 &sctx
->deleted_refs
,
3516 btrfs_free_path(path
);
3521 * Check if inode ino2, or any of its ancestors, is inode ino1.
3522 * Return 1 if true, 0 if false and < 0 on error.
3524 static int check_ino_in_path(struct btrfs_root
*root
,
3529 struct fs_path
*fs_path
)
3534 return ino1_gen
== ino2_gen
;
3536 while (ino
> BTRFS_FIRST_FREE_OBJECTID
) {
3541 fs_path_reset(fs_path
);
3542 ret
= get_first_ref(root
, ino
, &parent
, &parent_gen
, fs_path
);
3546 return parent_gen
== ino1_gen
;
3553 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3554 * possible path (in case ino2 is not a directory and has multiple hard links).
3555 * Return 1 if true, 0 if false and < 0 on error.
3557 static int is_ancestor(struct btrfs_root
*root
,
3561 struct fs_path
*fs_path
)
3563 bool free_fs_path
= false;
3565 struct btrfs_path
*path
= NULL
;
3566 struct btrfs_key key
;
3569 fs_path
= fs_path_alloc();
3572 free_fs_path
= true;
3575 path
= alloc_path_for_send();
3581 key
.objectid
= ino2
;
3582 key
.type
= BTRFS_INODE_REF_KEY
;
3585 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3590 struct extent_buffer
*leaf
= path
->nodes
[0];
3591 int slot
= path
->slots
[0];
3595 if (slot
>= btrfs_header_nritems(leaf
)) {
3596 ret
= btrfs_next_leaf(root
, path
);
3604 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
3605 if (key
.objectid
!= ino2
)
3607 if (key
.type
!= BTRFS_INODE_REF_KEY
&&
3608 key
.type
!= BTRFS_INODE_EXTREF_KEY
)
3611 item_size
= btrfs_item_size_nr(leaf
, slot
);
3612 while (cur_offset
< item_size
) {
3616 if (key
.type
== BTRFS_INODE_EXTREF_KEY
) {
3618 struct btrfs_inode_extref
*extref
;
3620 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
3621 extref
= (struct btrfs_inode_extref
*)
3623 parent
= btrfs_inode_extref_parent(leaf
,
3625 cur_offset
+= sizeof(*extref
);
3626 cur_offset
+= btrfs_inode_extref_name_len(leaf
,
3629 parent
= key
.offset
;
3630 cur_offset
= item_size
;
3633 ret
= get_inode_info(root
, parent
, NULL
, &parent_gen
,
3634 NULL
, NULL
, NULL
, NULL
);
3637 ret
= check_ino_in_path(root
, ino1
, ino1_gen
,
3638 parent
, parent_gen
, fs_path
);
3646 btrfs_free_path(path
);
3648 fs_path_free(fs_path
);
3652 static int wait_for_parent_move(struct send_ctx
*sctx
,
3653 struct recorded_ref
*parent_ref
,
3654 const bool is_orphan
)
3657 u64 ino
= parent_ref
->dir
;
3658 u64 ino_gen
= parent_ref
->dir_gen
;
3659 u64 parent_ino_before
, parent_ino_after
;
3660 struct fs_path
*path_before
= NULL
;
3661 struct fs_path
*path_after
= NULL
;
3664 path_after
= fs_path_alloc();
3665 path_before
= fs_path_alloc();
3666 if (!path_after
|| !path_before
) {
3672 * Our current directory inode may not yet be renamed/moved because some
3673 * ancestor (immediate or not) has to be renamed/moved first. So find if
3674 * such ancestor exists and make sure our own rename/move happens after
3675 * that ancestor is processed to avoid path build infinite loops (done
3676 * at get_cur_path()).
3678 while (ino
> BTRFS_FIRST_FREE_OBJECTID
) {
3679 u64 parent_ino_after_gen
;
3681 if (is_waiting_for_move(sctx
, ino
)) {
3683 * If the current inode is an ancestor of ino in the
3684 * parent root, we need to delay the rename of the
3685 * current inode, otherwise don't delayed the rename
3686 * because we can end up with a circular dependency
3687 * of renames, resulting in some directories never
3688 * getting the respective rename operations issued in
3689 * the send stream or getting into infinite path build
3692 ret
= is_ancestor(sctx
->parent_root
,
3693 sctx
->cur_ino
, sctx
->cur_inode_gen
,
3699 fs_path_reset(path_before
);
3700 fs_path_reset(path_after
);
3702 ret
= get_first_ref(sctx
->send_root
, ino
, &parent_ino_after
,
3703 &parent_ino_after_gen
, path_after
);
3706 ret
= get_first_ref(sctx
->parent_root
, ino
, &parent_ino_before
,
3708 if (ret
< 0 && ret
!= -ENOENT
) {
3710 } else if (ret
== -ENOENT
) {
3715 len1
= fs_path_len(path_before
);
3716 len2
= fs_path_len(path_after
);
3717 if (ino
> sctx
->cur_ino
&&
3718 (parent_ino_before
!= parent_ino_after
|| len1
!= len2
||
3719 memcmp(path_before
->start
, path_after
->start
, len1
))) {
3722 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
,
3723 &parent_ino_gen
, NULL
, NULL
, NULL
,
3727 if (ino_gen
== parent_ino_gen
) {
3732 ino
= parent_ino_after
;
3733 ino_gen
= parent_ino_after_gen
;
3737 fs_path_free(path_before
);
3738 fs_path_free(path_after
);
3741 ret
= add_pending_dir_move(sctx
,
3743 sctx
->cur_inode_gen
,
3746 &sctx
->deleted_refs
,
3755 static int update_ref_path(struct send_ctx
*sctx
, struct recorded_ref
*ref
)
3758 struct fs_path
*new_path
;
3761 * Our reference's name member points to its full_path member string, so
3762 * we use here a new path.
3764 new_path
= fs_path_alloc();
3768 ret
= get_cur_path(sctx
, ref
->dir
, ref
->dir_gen
, new_path
);
3770 fs_path_free(new_path
);
3773 ret
= fs_path_add(new_path
, ref
->name
, ref
->name_len
);
3775 fs_path_free(new_path
);
3779 fs_path_free(ref
->full_path
);
3780 set_ref_path(ref
, new_path
);
3786 * This does all the move/link/unlink/rmdir magic.
3788 static int process_recorded_refs(struct send_ctx
*sctx
, int *pending_move
)
3790 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
3792 struct recorded_ref
*cur
;
3793 struct recorded_ref
*cur2
;
3794 struct list_head check_dirs
;
3795 struct fs_path
*valid_path
= NULL
;
3799 int did_overwrite
= 0;
3801 u64 last_dir_ino_rm
= 0;
3802 bool can_rename
= true;
3803 bool orphanized_dir
= false;
3804 bool orphanized_ancestor
= false;
3806 btrfs_debug(fs_info
, "process_recorded_refs %llu", sctx
->cur_ino
);
3809 * This should never happen as the root dir always has the same ref
3810 * which is always '..'
3812 BUG_ON(sctx
->cur_ino
<= BTRFS_FIRST_FREE_OBJECTID
);
3813 INIT_LIST_HEAD(&check_dirs
);
3815 valid_path
= fs_path_alloc();
3822 * First, check if the first ref of the current inode was overwritten
3823 * before. If yes, we know that the current inode was already orphanized
3824 * and thus use the orphan name. If not, we can use get_cur_path to
3825 * get the path of the first ref as it would like while receiving at
3826 * this point in time.
3827 * New inodes are always orphan at the beginning, so force to use the
3828 * orphan name in this case.
3829 * The first ref is stored in valid_path and will be updated if it
3830 * gets moved around.
3832 if (!sctx
->cur_inode_new
) {
3833 ret
= did_overwrite_first_ref(sctx
, sctx
->cur_ino
,
3834 sctx
->cur_inode_gen
);
3840 if (sctx
->cur_inode_new
|| did_overwrite
) {
3841 ret
= gen_unique_name(sctx
, sctx
->cur_ino
,
3842 sctx
->cur_inode_gen
, valid_path
);
3847 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
3853 list_for_each_entry(cur
, &sctx
->new_refs
, list
) {
3855 * We may have refs where the parent directory does not exist
3856 * yet. This happens if the parent directories inum is higher
3857 * the the current inum. To handle this case, we create the
3858 * parent directory out of order. But we need to check if this
3859 * did already happen before due to other refs in the same dir.
3861 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
3864 if (ret
== inode_state_will_create
) {
3867 * First check if any of the current inodes refs did
3868 * already create the dir.
3870 list_for_each_entry(cur2
, &sctx
->new_refs
, list
) {
3873 if (cur2
->dir
== cur
->dir
) {
3880 * If that did not happen, check if a previous inode
3881 * did already create the dir.
3884 ret
= did_create_dir(sctx
, cur
->dir
);
3888 ret
= send_create_inode(sctx
, cur
->dir
);
3895 * Check if this new ref would overwrite the first ref of
3896 * another unprocessed inode. If yes, orphanize the
3897 * overwritten inode. If we find an overwritten ref that is
3898 * not the first ref, simply unlink it.
3900 ret
= will_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
3901 cur
->name
, cur
->name_len
,
3902 &ow_inode
, &ow_gen
, &ow_mode
);
3906 ret
= is_first_ref(sctx
->parent_root
,
3907 ow_inode
, cur
->dir
, cur
->name
,
3912 struct name_cache_entry
*nce
;
3913 struct waiting_dir_move
*wdm
;
3915 ret
= orphanize_inode(sctx
, ow_inode
, ow_gen
,
3919 if (S_ISDIR(ow_mode
))
3920 orphanized_dir
= true;
3923 * If ow_inode has its rename operation delayed
3924 * make sure that its orphanized name is used in
3925 * the source path when performing its rename
3928 if (is_waiting_for_move(sctx
, ow_inode
)) {
3929 wdm
= get_waiting_dir_move(sctx
,
3932 wdm
->orphanized
= true;
3936 * Make sure we clear our orphanized inode's
3937 * name from the name cache. This is because the
3938 * inode ow_inode might be an ancestor of some
3939 * other inode that will be orphanized as well
3940 * later and has an inode number greater than
3941 * sctx->send_progress. We need to prevent
3942 * future name lookups from using the old name
3943 * and get instead the orphan name.
3945 nce
= name_cache_search(sctx
, ow_inode
, ow_gen
);
3947 name_cache_delete(sctx
, nce
);
3952 * ow_inode might currently be an ancestor of
3953 * cur_ino, therefore compute valid_path (the
3954 * current path of cur_ino) again because it
3955 * might contain the pre-orphanization name of
3956 * ow_inode, which is no longer valid.
3958 ret
= is_ancestor(sctx
->parent_root
,
3960 sctx
->cur_ino
, NULL
);
3962 orphanized_ancestor
= true;
3963 fs_path_reset(valid_path
);
3964 ret
= get_cur_path(sctx
, sctx
->cur_ino
,
3965 sctx
->cur_inode_gen
,
3971 ret
= send_unlink(sctx
, cur
->full_path
);
3977 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->parent_root
) {
3978 ret
= wait_for_dest_dir_move(sctx
, cur
, is_orphan
);
3987 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->parent_root
&&
3989 ret
= wait_for_parent_move(sctx
, cur
, is_orphan
);
3999 * link/move the ref to the new place. If we have an orphan
4000 * inode, move it and update valid_path. If not, link or move
4001 * it depending on the inode mode.
4003 if (is_orphan
&& can_rename
) {
4004 ret
= send_rename(sctx
, valid_path
, cur
->full_path
);
4008 ret
= fs_path_copy(valid_path
, cur
->full_path
);
4011 } else if (can_rename
) {
4012 if (S_ISDIR(sctx
->cur_inode_mode
)) {
4014 * Dirs can't be linked, so move it. For moved
4015 * dirs, we always have one new and one deleted
4016 * ref. The deleted ref is ignored later.
4018 ret
= send_rename(sctx
, valid_path
,
4021 ret
= fs_path_copy(valid_path
,
4027 * We might have previously orphanized an inode
4028 * which is an ancestor of our current inode,
4029 * so our reference's full path, which was
4030 * computed before any such orphanizations, must
4033 if (orphanized_dir
) {
4034 ret
= update_ref_path(sctx
, cur
);
4038 ret
= send_link(sctx
, cur
->full_path
,
4044 ret
= dup_ref(cur
, &check_dirs
);
4049 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->cur_inode_deleted
) {
4051 * Check if we can already rmdir the directory. If not,
4052 * orphanize it. For every dir item inside that gets deleted
4053 * later, we do this check again and rmdir it then if possible.
4054 * See the use of check_dirs for more details.
4056 ret
= can_rmdir(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4061 ret
= send_rmdir(sctx
, valid_path
);
4064 } else if (!is_orphan
) {
4065 ret
= orphanize_inode(sctx
, sctx
->cur_ino
,
4066 sctx
->cur_inode_gen
, valid_path
);
4072 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
4073 ret
= dup_ref(cur
, &check_dirs
);
4077 } else if (S_ISDIR(sctx
->cur_inode_mode
) &&
4078 !list_empty(&sctx
->deleted_refs
)) {
4080 * We have a moved dir. Add the old parent to check_dirs
4082 cur
= list_entry(sctx
->deleted_refs
.next
, struct recorded_ref
,
4084 ret
= dup_ref(cur
, &check_dirs
);
4087 } else if (!S_ISDIR(sctx
->cur_inode_mode
)) {
4089 * We have a non dir inode. Go through all deleted refs and
4090 * unlink them if they were not already overwritten by other
4093 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
4094 ret
= did_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
4095 sctx
->cur_ino
, sctx
->cur_inode_gen
,
4096 cur
->name
, cur
->name_len
);
4101 * If we orphanized any ancestor before, we need
4102 * to recompute the full path for deleted names,
4103 * since any such path was computed before we
4104 * processed any references and orphanized any
4107 if (orphanized_ancestor
) {
4108 ret
= update_ref_path(sctx
, cur
);
4112 ret
= send_unlink(sctx
, cur
->full_path
);
4116 ret
= dup_ref(cur
, &check_dirs
);
4121 * If the inode is still orphan, unlink the orphan. This may
4122 * happen when a previous inode did overwrite the first ref
4123 * of this inode and no new refs were added for the current
4124 * inode. Unlinking does not mean that the inode is deleted in
4125 * all cases. There may still be links to this inode in other
4129 ret
= send_unlink(sctx
, valid_path
);
4136 * We did collect all parent dirs where cur_inode was once located. We
4137 * now go through all these dirs and check if they are pending for
4138 * deletion and if it's finally possible to perform the rmdir now.
4139 * We also update the inode stats of the parent dirs here.
4141 list_for_each_entry(cur
, &check_dirs
, list
) {
4143 * In case we had refs into dirs that were not processed yet,
4144 * we don't need to do the utime and rmdir logic for these dirs.
4145 * The dir will be processed later.
4147 if (cur
->dir
> sctx
->cur_ino
)
4150 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
4154 if (ret
== inode_state_did_create
||
4155 ret
== inode_state_no_change
) {
4156 /* TODO delayed utimes */
4157 ret
= send_utimes(sctx
, cur
->dir
, cur
->dir_gen
);
4160 } else if (ret
== inode_state_did_delete
&&
4161 cur
->dir
!= last_dir_ino_rm
) {
4162 ret
= can_rmdir(sctx
, cur
->dir
, cur
->dir_gen
,
4167 ret
= get_cur_path(sctx
, cur
->dir
,
4168 cur
->dir_gen
, valid_path
);
4171 ret
= send_rmdir(sctx
, valid_path
);
4174 last_dir_ino_rm
= cur
->dir
;
4182 __free_recorded_refs(&check_dirs
);
4183 free_recorded_refs(sctx
);
4184 fs_path_free(valid_path
);
4188 static int record_ref(struct btrfs_root
*root
, u64 dir
, struct fs_path
*name
,
4189 void *ctx
, struct list_head
*refs
)
4192 struct send_ctx
*sctx
= ctx
;
4196 p
= fs_path_alloc();
4200 ret
= get_inode_info(root
, dir
, NULL
, &gen
, NULL
, NULL
,
4205 ret
= get_cur_path(sctx
, dir
, gen
, p
);
4208 ret
= fs_path_add_path(p
, name
);
4212 ret
= __record_ref(refs
, dir
, gen
, p
);
4220 static int __record_new_ref(int num
, u64 dir
, int index
,
4221 struct fs_path
*name
,
4224 struct send_ctx
*sctx
= ctx
;
4225 return record_ref(sctx
->send_root
, dir
, name
, ctx
, &sctx
->new_refs
);
4229 static int __record_deleted_ref(int num
, u64 dir
, int index
,
4230 struct fs_path
*name
,
4233 struct send_ctx
*sctx
= ctx
;
4234 return record_ref(sctx
->parent_root
, dir
, name
, ctx
,
4235 &sctx
->deleted_refs
);
4238 static int record_new_ref(struct send_ctx
*sctx
)
4242 ret
= iterate_inode_ref(sctx
->send_root
, sctx
->left_path
,
4243 sctx
->cmp_key
, 0, __record_new_ref
, sctx
);
4252 static int record_deleted_ref(struct send_ctx
*sctx
)
4256 ret
= iterate_inode_ref(sctx
->parent_root
, sctx
->right_path
,
4257 sctx
->cmp_key
, 0, __record_deleted_ref
, sctx
);
4266 struct find_ref_ctx
{
4269 struct btrfs_root
*root
;
4270 struct fs_path
*name
;
4274 static int __find_iref(int num
, u64 dir
, int index
,
4275 struct fs_path
*name
,
4278 struct find_ref_ctx
*ctx
= ctx_
;
4282 if (dir
== ctx
->dir
&& fs_path_len(name
) == fs_path_len(ctx
->name
) &&
4283 strncmp(name
->start
, ctx
->name
->start
, fs_path_len(name
)) == 0) {
4285 * To avoid doing extra lookups we'll only do this if everything
4288 ret
= get_inode_info(ctx
->root
, dir
, NULL
, &dir_gen
, NULL
,
4292 if (dir_gen
!= ctx
->dir_gen
)
4294 ctx
->found_idx
= num
;
4300 static int find_iref(struct btrfs_root
*root
,
4301 struct btrfs_path
*path
,
4302 struct btrfs_key
*key
,
4303 u64 dir
, u64 dir_gen
, struct fs_path
*name
)
4306 struct find_ref_ctx ctx
;
4310 ctx
.dir_gen
= dir_gen
;
4314 ret
= iterate_inode_ref(root
, path
, key
, 0, __find_iref
, &ctx
);
4318 if (ctx
.found_idx
== -1)
4321 return ctx
.found_idx
;
4324 static int __record_changed_new_ref(int num
, u64 dir
, int index
,
4325 struct fs_path
*name
,
4330 struct send_ctx
*sctx
= ctx
;
4332 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &dir_gen
, NULL
,
4337 ret
= find_iref(sctx
->parent_root
, sctx
->right_path
,
4338 sctx
->cmp_key
, dir
, dir_gen
, name
);
4340 ret
= __record_new_ref(num
, dir
, index
, name
, sctx
);
4347 static int __record_changed_deleted_ref(int num
, u64 dir
, int index
,
4348 struct fs_path
*name
,
4353 struct send_ctx
*sctx
= ctx
;
4355 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &dir_gen
, NULL
,
4360 ret
= find_iref(sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
4361 dir
, dir_gen
, name
);
4363 ret
= __record_deleted_ref(num
, dir
, index
, name
, sctx
);
4370 static int record_changed_ref(struct send_ctx
*sctx
)
4374 ret
= iterate_inode_ref(sctx
->send_root
, sctx
->left_path
,
4375 sctx
->cmp_key
, 0, __record_changed_new_ref
, sctx
);
4378 ret
= iterate_inode_ref(sctx
->parent_root
, sctx
->right_path
,
4379 sctx
->cmp_key
, 0, __record_changed_deleted_ref
, sctx
);
4389 * Record and process all refs at once. Needed when an inode changes the
4390 * generation number, which means that it was deleted and recreated.
4392 static int process_all_refs(struct send_ctx
*sctx
,
4393 enum btrfs_compare_tree_result cmd
)
4396 struct btrfs_root
*root
;
4397 struct btrfs_path
*path
;
4398 struct btrfs_key key
;
4399 struct btrfs_key found_key
;
4400 struct extent_buffer
*eb
;
4402 iterate_inode_ref_t cb
;
4403 int pending_move
= 0;
4405 path
= alloc_path_for_send();
4409 if (cmd
== BTRFS_COMPARE_TREE_NEW
) {
4410 root
= sctx
->send_root
;
4411 cb
= __record_new_ref
;
4412 } else if (cmd
== BTRFS_COMPARE_TREE_DELETED
) {
4413 root
= sctx
->parent_root
;
4414 cb
= __record_deleted_ref
;
4416 btrfs_err(sctx
->send_root
->fs_info
,
4417 "Wrong command %d in process_all_refs", cmd
);
4422 key
.objectid
= sctx
->cmp_key
->objectid
;
4423 key
.type
= BTRFS_INODE_REF_KEY
;
4425 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4430 eb
= path
->nodes
[0];
4431 slot
= path
->slots
[0];
4432 if (slot
>= btrfs_header_nritems(eb
)) {
4433 ret
= btrfs_next_leaf(root
, path
);
4441 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4443 if (found_key
.objectid
!= key
.objectid
||
4444 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
4445 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
))
4448 ret
= iterate_inode_ref(root
, path
, &found_key
, 0, cb
, sctx
);
4454 btrfs_release_path(path
);
4457 * We don't actually care about pending_move as we are simply
4458 * re-creating this inode and will be rename'ing it into place once we
4459 * rename the parent directory.
4461 ret
= process_recorded_refs(sctx
, &pending_move
);
4463 btrfs_free_path(path
);
4467 static int send_set_xattr(struct send_ctx
*sctx
,
4468 struct fs_path
*path
,
4469 const char *name
, int name_len
,
4470 const char *data
, int data_len
)
4474 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SET_XATTR
);
4478 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
4479 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
4480 TLV_PUT(sctx
, BTRFS_SEND_A_XATTR_DATA
, data
, data_len
);
4482 ret
= send_cmd(sctx
);
4489 static int send_remove_xattr(struct send_ctx
*sctx
,
4490 struct fs_path
*path
,
4491 const char *name
, int name_len
)
4495 ret
= begin_cmd(sctx
, BTRFS_SEND_C_REMOVE_XATTR
);
4499 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
4500 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
4502 ret
= send_cmd(sctx
);
4509 static int __process_new_xattr(int num
, struct btrfs_key
*di_key
,
4510 const char *name
, int name_len
,
4511 const char *data
, int data_len
,
4515 struct send_ctx
*sctx
= ctx
;
4517 struct posix_acl_xattr_header dummy_acl
;
4519 p
= fs_path_alloc();
4524 * This hack is needed because empty acls are stored as zero byte
4525 * data in xattrs. Problem with that is, that receiving these zero byte
4526 * acls will fail later. To fix this, we send a dummy acl list that
4527 * only contains the version number and no entries.
4529 if (!strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
, name_len
) ||
4530 !strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
, name_len
)) {
4531 if (data_len
== 0) {
4532 dummy_acl
.a_version
=
4533 cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
4534 data
= (char *)&dummy_acl
;
4535 data_len
= sizeof(dummy_acl
);
4539 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4543 ret
= send_set_xattr(sctx
, p
, name
, name_len
, data
, data_len
);
4550 static int __process_deleted_xattr(int num
, struct btrfs_key
*di_key
,
4551 const char *name
, int name_len
,
4552 const char *data
, int data_len
,
4556 struct send_ctx
*sctx
= ctx
;
4559 p
= fs_path_alloc();
4563 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4567 ret
= send_remove_xattr(sctx
, p
, name
, name_len
);
4574 static int process_new_xattr(struct send_ctx
*sctx
)
4578 ret
= iterate_dir_item(sctx
->send_root
, sctx
->left_path
,
4579 __process_new_xattr
, sctx
);
4584 static int process_deleted_xattr(struct send_ctx
*sctx
)
4586 return iterate_dir_item(sctx
->parent_root
, sctx
->right_path
,
4587 __process_deleted_xattr
, sctx
);
4590 struct find_xattr_ctx
{
4598 static int __find_xattr(int num
, struct btrfs_key
*di_key
,
4599 const char *name
, int name_len
,
4600 const char *data
, int data_len
,
4601 u8 type
, void *vctx
)
4603 struct find_xattr_ctx
*ctx
= vctx
;
4605 if (name_len
== ctx
->name_len
&&
4606 strncmp(name
, ctx
->name
, name_len
) == 0) {
4607 ctx
->found_idx
= num
;
4608 ctx
->found_data_len
= data_len
;
4609 ctx
->found_data
= kmemdup(data
, data_len
, GFP_KERNEL
);
4610 if (!ctx
->found_data
)
4617 static int find_xattr(struct btrfs_root
*root
,
4618 struct btrfs_path
*path
,
4619 struct btrfs_key
*key
,
4620 const char *name
, int name_len
,
4621 char **data
, int *data_len
)
4624 struct find_xattr_ctx ctx
;
4627 ctx
.name_len
= name_len
;
4629 ctx
.found_data
= NULL
;
4630 ctx
.found_data_len
= 0;
4632 ret
= iterate_dir_item(root
, path
, __find_xattr
, &ctx
);
4636 if (ctx
.found_idx
== -1)
4639 *data
= ctx
.found_data
;
4640 *data_len
= ctx
.found_data_len
;
4642 kfree(ctx
.found_data
);
4644 return ctx
.found_idx
;
4648 static int __process_changed_new_xattr(int num
, struct btrfs_key
*di_key
,
4649 const char *name
, int name_len
,
4650 const char *data
, int data_len
,
4654 struct send_ctx
*sctx
= ctx
;
4655 char *found_data
= NULL
;
4656 int found_data_len
= 0;
4658 ret
= find_xattr(sctx
->parent_root
, sctx
->right_path
,
4659 sctx
->cmp_key
, name
, name_len
, &found_data
,
4661 if (ret
== -ENOENT
) {
4662 ret
= __process_new_xattr(num
, di_key
, name
, name_len
, data
,
4663 data_len
, type
, ctx
);
4664 } else if (ret
>= 0) {
4665 if (data_len
!= found_data_len
||
4666 memcmp(data
, found_data
, data_len
)) {
4667 ret
= __process_new_xattr(num
, di_key
, name
, name_len
,
4668 data
, data_len
, type
, ctx
);
4678 static int __process_changed_deleted_xattr(int num
, struct btrfs_key
*di_key
,
4679 const char *name
, int name_len
,
4680 const char *data
, int data_len
,
4684 struct send_ctx
*sctx
= ctx
;
4686 ret
= find_xattr(sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
4687 name
, name_len
, NULL
, NULL
);
4689 ret
= __process_deleted_xattr(num
, di_key
, name
, name_len
, data
,
4690 data_len
, type
, ctx
);
4697 static int process_changed_xattr(struct send_ctx
*sctx
)
4701 ret
= iterate_dir_item(sctx
->send_root
, sctx
->left_path
,
4702 __process_changed_new_xattr
, sctx
);
4705 ret
= iterate_dir_item(sctx
->parent_root
, sctx
->right_path
,
4706 __process_changed_deleted_xattr
, sctx
);
4712 static int process_all_new_xattrs(struct send_ctx
*sctx
)
4715 struct btrfs_root
*root
;
4716 struct btrfs_path
*path
;
4717 struct btrfs_key key
;
4718 struct btrfs_key found_key
;
4719 struct extent_buffer
*eb
;
4722 path
= alloc_path_for_send();
4726 root
= sctx
->send_root
;
4728 key
.objectid
= sctx
->cmp_key
->objectid
;
4729 key
.type
= BTRFS_XATTR_ITEM_KEY
;
4731 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4736 eb
= path
->nodes
[0];
4737 slot
= path
->slots
[0];
4738 if (slot
>= btrfs_header_nritems(eb
)) {
4739 ret
= btrfs_next_leaf(root
, path
);
4742 } else if (ret
> 0) {
4749 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4750 if (found_key
.objectid
!= key
.objectid
||
4751 found_key
.type
!= key
.type
) {
4756 ret
= iterate_dir_item(root
, path
, __process_new_xattr
, sctx
);
4764 btrfs_free_path(path
);
4768 static ssize_t
fill_read_buf(struct send_ctx
*sctx
, u64 offset
, u32 len
)
4770 struct btrfs_root
*root
= sctx
->send_root
;
4771 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4772 struct inode
*inode
;
4775 struct btrfs_key key
;
4776 pgoff_t index
= offset
>> PAGE_SHIFT
;
4778 unsigned pg_offset
= offset
& ~PAGE_MASK
;
4781 key
.objectid
= sctx
->cur_ino
;
4782 key
.type
= BTRFS_INODE_ITEM_KEY
;
4785 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
4787 return PTR_ERR(inode
);
4789 if (offset
+ len
> i_size_read(inode
)) {
4790 if (offset
> i_size_read(inode
))
4793 len
= offset
- i_size_read(inode
);
4798 last_index
= (offset
+ len
- 1) >> PAGE_SHIFT
;
4800 /* initial readahead */
4801 memset(&sctx
->ra
, 0, sizeof(struct file_ra_state
));
4802 file_ra_state_init(&sctx
->ra
, inode
->i_mapping
);
4804 while (index
<= last_index
) {
4805 unsigned cur_len
= min_t(unsigned, len
,
4806 PAGE_SIZE
- pg_offset
);
4808 page
= find_lock_page(inode
->i_mapping
, index
);
4810 page_cache_sync_readahead(inode
->i_mapping
, &sctx
->ra
,
4811 NULL
, index
, last_index
+ 1 - index
);
4813 page
= find_or_create_page(inode
->i_mapping
, index
,
4821 if (PageReadahead(page
)) {
4822 page_cache_async_readahead(inode
->i_mapping
, &sctx
->ra
,
4823 NULL
, page
, index
, last_index
+ 1 - index
);
4826 if (!PageUptodate(page
)) {
4827 btrfs_readpage(NULL
, page
);
4829 if (!PageUptodate(page
)) {
4838 memcpy(sctx
->read_buf
+ ret
, addr
+ pg_offset
, cur_len
);
4853 * Read some bytes from the current inode/file and send a write command to
4856 static int send_write(struct send_ctx
*sctx
, u64 offset
, u32 len
)
4858 struct btrfs_fs_info
*fs_info
= sctx
->send_root
->fs_info
;
4861 ssize_t num_read
= 0;
4863 p
= fs_path_alloc();
4867 btrfs_debug(fs_info
, "send_write offset=%llu, len=%d", offset
, len
);
4869 num_read
= fill_read_buf(sctx
, offset
, len
);
4870 if (num_read
<= 0) {
4876 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
4880 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4884 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4885 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4886 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, num_read
);
4888 ret
= send_cmd(sctx
);
4899 * Send a clone command to user space.
4901 static int send_clone(struct send_ctx
*sctx
,
4902 u64 offset
, u32 len
,
4903 struct clone_root
*clone_root
)
4909 btrfs_debug(sctx
->send_root
->fs_info
,
4910 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4911 offset
, len
, clone_root
->root
->root_key
.objectid
,
4912 clone_root
->ino
, clone_root
->offset
);
4914 p
= fs_path_alloc();
4918 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CLONE
);
4922 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4926 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4927 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_LEN
, len
);
4928 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4930 if (clone_root
->root
== sctx
->send_root
) {
4931 ret
= get_inode_info(sctx
->send_root
, clone_root
->ino
, NULL
,
4932 &gen
, NULL
, NULL
, NULL
, NULL
);
4935 ret
= get_cur_path(sctx
, clone_root
->ino
, gen
, p
);
4937 ret
= get_inode_path(clone_root
->root
, clone_root
->ino
, p
);
4943 * If the parent we're using has a received_uuid set then use that as
4944 * our clone source as that is what we will look for when doing a
4947 * This covers the case that we create a snapshot off of a received
4948 * subvolume and then use that as the parent and try to receive on a
4951 if (!btrfs_is_empty_uuid(clone_root
->root
->root_item
.received_uuid
))
4952 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
4953 clone_root
->root
->root_item
.received_uuid
);
4955 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
4956 clone_root
->root
->root_item
.uuid
);
4957 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
4958 le64_to_cpu(clone_root
->root
->root_item
.ctransid
));
4959 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_CLONE_PATH
, p
);
4960 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_OFFSET
,
4961 clone_root
->offset
);
4963 ret
= send_cmd(sctx
);
4972 * Send an update extent command to user space.
4974 static int send_update_extent(struct send_ctx
*sctx
,
4975 u64 offset
, u32 len
)
4980 p
= fs_path_alloc();
4984 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UPDATE_EXTENT
);
4988 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4992 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4993 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4994 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, len
);
4996 ret
= send_cmd(sctx
);
5004 static int send_hole(struct send_ctx
*sctx
, u64 end
)
5006 struct fs_path
*p
= NULL
;
5007 u64 offset
= sctx
->cur_inode_last_extent
;
5012 * A hole that starts at EOF or beyond it. Since we do not yet support
5013 * fallocate (for extent preallocation and hole punching), sending a
5014 * write of zeroes starting at EOF or beyond would later require issuing
5015 * a truncate operation which would undo the write and achieve nothing.
5017 if (offset
>= sctx
->cur_inode_size
)
5020 if (sctx
->flags
& BTRFS_SEND_FLAG_NO_FILE_DATA
)
5021 return send_update_extent(sctx
, offset
, end
- offset
);
5023 p
= fs_path_alloc();
5026 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
5028 goto tlv_put_failure
;
5029 memset(sctx
->read_buf
, 0, BTRFS_SEND_READ_SIZE
);
5030 while (offset
< end
) {
5031 len
= min_t(u64
, end
- offset
, BTRFS_SEND_READ_SIZE
);
5033 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
5036 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
5037 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
5038 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, len
);
5039 ret
= send_cmd(sctx
);
5044 sctx
->cur_inode_next_write_offset
= offset
;
5050 static int send_extent_data(struct send_ctx
*sctx
,
5056 if (sctx
->flags
& BTRFS_SEND_FLAG_NO_FILE_DATA
)
5057 return send_update_extent(sctx
, offset
, len
);
5059 while (sent
< len
) {
5060 u64 size
= len
- sent
;
5063 if (size
> BTRFS_SEND_READ_SIZE
)
5064 size
= BTRFS_SEND_READ_SIZE
;
5065 ret
= send_write(sctx
, offset
+ sent
, size
);
5075 static int clone_range(struct send_ctx
*sctx
,
5076 struct clone_root
*clone_root
,
5077 const u64 disk_byte
,
5082 struct btrfs_path
*path
;
5083 struct btrfs_key key
;
5087 * Prevent cloning from a zero offset with a length matching the sector
5088 * size because in some scenarios this will make the receiver fail.
5090 * For example, if in the source filesystem the extent at offset 0
5091 * has a length of sectorsize and it was written using direct IO, then
5092 * it can never be an inline extent (even if compression is enabled).
5093 * Then this extent can be cloned in the original filesystem to a non
5094 * zero file offset, but it may not be possible to clone in the
5095 * destination filesystem because it can be inlined due to compression
5096 * on the destination filesystem (as the receiver's write operations are
5097 * always done using buffered IO). The same happens when the original
5098 * filesystem does not have compression enabled but the destination
5101 if (clone_root
->offset
== 0 &&
5102 len
== sctx
->send_root
->fs_info
->sectorsize
)
5103 return send_extent_data(sctx
, offset
, len
);
5105 path
= alloc_path_for_send();
5110 * We can't send a clone operation for the entire range if we find
5111 * extent items in the respective range in the source file that
5112 * refer to different extents or if we find holes.
5113 * So check for that and do a mix of clone and regular write/copy
5114 * operations if needed.
5118 * mkfs.btrfs -f /dev/sda
5119 * mount /dev/sda /mnt
5120 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5121 * cp --reflink=always /mnt/foo /mnt/bar
5122 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5123 * btrfs subvolume snapshot -r /mnt /mnt/snap
5125 * If when we send the snapshot and we are processing file bar (which
5126 * has a higher inode number than foo) we blindly send a clone operation
5127 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5128 * a file bar that matches the content of file foo - iow, doesn't match
5129 * the content from bar in the original filesystem.
5131 key
.objectid
= clone_root
->ino
;
5132 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5133 key
.offset
= clone_root
->offset
;
5134 ret
= btrfs_search_slot(NULL
, clone_root
->root
, &key
, path
, 0, 0);
5137 if (ret
> 0 && path
->slots
[0] > 0) {
5138 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0] - 1);
5139 if (key
.objectid
== clone_root
->ino
&&
5140 key
.type
== BTRFS_EXTENT_DATA_KEY
)
5145 struct extent_buffer
*leaf
= path
->nodes
[0];
5146 int slot
= path
->slots
[0];
5147 struct btrfs_file_extent_item
*ei
;
5152 if (slot
>= btrfs_header_nritems(leaf
)) {
5153 ret
= btrfs_next_leaf(clone_root
->root
, path
);
5161 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5164 * We might have an implicit trailing hole (NO_HOLES feature
5165 * enabled). We deal with it after leaving this loop.
5167 if (key
.objectid
!= clone_root
->ino
||
5168 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5171 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5172 type
= btrfs_file_extent_type(leaf
, ei
);
5173 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
5174 ext_len
= btrfs_file_extent_ram_bytes(leaf
, ei
);
5175 ext_len
= PAGE_ALIGN(ext_len
);
5177 ext_len
= btrfs_file_extent_num_bytes(leaf
, ei
);
5180 if (key
.offset
+ ext_len
<= clone_root
->offset
)
5183 if (key
.offset
> clone_root
->offset
) {
5184 /* Implicit hole, NO_HOLES feature enabled. */
5185 u64 hole_len
= key
.offset
- clone_root
->offset
;
5189 ret
= send_extent_data(sctx
, offset
, hole_len
);
5197 clone_root
->offset
+= hole_len
;
5198 data_offset
+= hole_len
;
5201 if (key
.offset
>= clone_root
->offset
+ len
)
5204 clone_len
= min_t(u64
, ext_len
, len
);
5206 if (btrfs_file_extent_disk_bytenr(leaf
, ei
) == disk_byte
&&
5207 btrfs_file_extent_offset(leaf
, ei
) == data_offset
)
5208 ret
= send_clone(sctx
, offset
, clone_len
, clone_root
);
5210 ret
= send_extent_data(sctx
, offset
, clone_len
);
5218 offset
+= clone_len
;
5219 clone_root
->offset
+= clone_len
;
5220 data_offset
+= clone_len
;
5226 ret
= send_extent_data(sctx
, offset
, len
);
5230 btrfs_free_path(path
);
5234 static int send_write_or_clone(struct send_ctx
*sctx
,
5235 struct btrfs_path
*path
,
5236 struct btrfs_key
*key
,
5237 struct clone_root
*clone_root
)
5240 struct btrfs_file_extent_item
*ei
;
5241 u64 offset
= key
->offset
;
5244 u64 bs
= sctx
->send_root
->fs_info
->sb
->s_blocksize
;
5246 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5247 struct btrfs_file_extent_item
);
5248 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
5249 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
5250 len
= btrfs_file_extent_ram_bytes(path
->nodes
[0], ei
);
5252 * it is possible the inline item won't cover the whole page,
5253 * but there may be items after this page. Make
5254 * sure to send the whole thing
5256 len
= PAGE_ALIGN(len
);
5258 len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
5261 if (offset
>= sctx
->cur_inode_size
) {
5265 if (offset
+ len
> sctx
->cur_inode_size
)
5266 len
= sctx
->cur_inode_size
- offset
;
5272 if (clone_root
&& IS_ALIGNED(offset
+ len
, bs
)) {
5276 disk_byte
= btrfs_file_extent_disk_bytenr(path
->nodes
[0], ei
);
5277 data_offset
= btrfs_file_extent_offset(path
->nodes
[0], ei
);
5278 ret
= clone_range(sctx
, clone_root
, disk_byte
, data_offset
,
5281 ret
= send_extent_data(sctx
, offset
, len
);
5283 sctx
->cur_inode_next_write_offset
= offset
+ len
;
5288 static int is_extent_unchanged(struct send_ctx
*sctx
,
5289 struct btrfs_path
*left_path
,
5290 struct btrfs_key
*ekey
)
5293 struct btrfs_key key
;
5294 struct btrfs_path
*path
= NULL
;
5295 struct extent_buffer
*eb
;
5297 struct btrfs_key found_key
;
5298 struct btrfs_file_extent_item
*ei
;
5303 u64 left_offset_fixed
;
5311 path
= alloc_path_for_send();
5315 eb
= left_path
->nodes
[0];
5316 slot
= left_path
->slots
[0];
5317 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
5318 left_type
= btrfs_file_extent_type(eb
, ei
);
5320 if (left_type
!= BTRFS_FILE_EXTENT_REG
) {
5324 left_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
5325 left_len
= btrfs_file_extent_num_bytes(eb
, ei
);
5326 left_offset
= btrfs_file_extent_offset(eb
, ei
);
5327 left_gen
= btrfs_file_extent_generation(eb
, ei
);
5330 * Following comments will refer to these graphics. L is the left
5331 * extents which we are checking at the moment. 1-8 are the right
5332 * extents that we iterate.
5335 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5338 * |--1--|-2b-|...(same as above)
5340 * Alternative situation. Happens on files where extents got split.
5342 * |-----------7-----------|-6-|
5344 * Alternative situation. Happens on files which got larger.
5347 * Nothing follows after 8.
5350 key
.objectid
= ekey
->objectid
;
5351 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5352 key
.offset
= ekey
->offset
;
5353 ret
= btrfs_search_slot_for_read(sctx
->parent_root
, &key
, path
, 0, 0);
5362 * Handle special case where the right side has no extents at all.
5364 eb
= path
->nodes
[0];
5365 slot
= path
->slots
[0];
5366 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5367 if (found_key
.objectid
!= key
.objectid
||
5368 found_key
.type
!= key
.type
) {
5369 /* If we're a hole then just pretend nothing changed */
5370 ret
= (left_disknr
) ? 0 : 1;
5375 * We're now on 2a, 2b or 7.
5378 while (key
.offset
< ekey
->offset
+ left_len
) {
5379 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
5380 right_type
= btrfs_file_extent_type(eb
, ei
);
5381 if (right_type
!= BTRFS_FILE_EXTENT_REG
&&
5382 right_type
!= BTRFS_FILE_EXTENT_INLINE
) {
5387 if (right_type
== BTRFS_FILE_EXTENT_INLINE
) {
5388 right_len
= btrfs_file_extent_ram_bytes(eb
, ei
);
5389 right_len
= PAGE_ALIGN(right_len
);
5391 right_len
= btrfs_file_extent_num_bytes(eb
, ei
);
5395 * Are we at extent 8? If yes, we know the extent is changed.
5396 * This may only happen on the first iteration.
5398 if (found_key
.offset
+ right_len
<= ekey
->offset
) {
5399 /* If we're a hole just pretend nothing changed */
5400 ret
= (left_disknr
) ? 0 : 1;
5405 * We just wanted to see if when we have an inline extent, what
5406 * follows it is a regular extent (wanted to check the above
5407 * condition for inline extents too). This should normally not
5408 * happen but it's possible for example when we have an inline
5409 * compressed extent representing data with a size matching
5410 * the page size (currently the same as sector size).
5412 if (right_type
== BTRFS_FILE_EXTENT_INLINE
) {
5417 right_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
5418 right_offset
= btrfs_file_extent_offset(eb
, ei
);
5419 right_gen
= btrfs_file_extent_generation(eb
, ei
);
5421 left_offset_fixed
= left_offset
;
5422 if (key
.offset
< ekey
->offset
) {
5423 /* Fix the right offset for 2a and 7. */
5424 right_offset
+= ekey
->offset
- key
.offset
;
5426 /* Fix the left offset for all behind 2a and 2b */
5427 left_offset_fixed
+= key
.offset
- ekey
->offset
;
5431 * Check if we have the same extent.
5433 if (left_disknr
!= right_disknr
||
5434 left_offset_fixed
!= right_offset
||
5435 left_gen
!= right_gen
) {
5441 * Go to the next extent.
5443 ret
= btrfs_next_item(sctx
->parent_root
, path
);
5447 eb
= path
->nodes
[0];
5448 slot
= path
->slots
[0];
5449 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5451 if (ret
|| found_key
.objectid
!= key
.objectid
||
5452 found_key
.type
!= key
.type
) {
5453 key
.offset
+= right_len
;
5456 if (found_key
.offset
!= key
.offset
+ right_len
) {
5464 * We're now behind the left extent (treat as unchanged) or at the end
5465 * of the right side (treat as changed).
5467 if (key
.offset
>= ekey
->offset
+ left_len
)
5474 btrfs_free_path(path
);
5478 static int get_last_extent(struct send_ctx
*sctx
, u64 offset
)
5480 struct btrfs_path
*path
;
5481 struct btrfs_root
*root
= sctx
->send_root
;
5482 struct btrfs_file_extent_item
*fi
;
5483 struct btrfs_key key
;
5488 path
= alloc_path_for_send();
5492 sctx
->cur_inode_last_extent
= 0;
5494 key
.objectid
= sctx
->cur_ino
;
5495 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5496 key
.offset
= offset
;
5497 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 0, 1);
5501 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
5502 if (key
.objectid
!= sctx
->cur_ino
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5505 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5506 struct btrfs_file_extent_item
);
5507 type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
5508 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
5509 u64 size
= btrfs_file_extent_ram_bytes(path
->nodes
[0], fi
);
5510 extent_end
= ALIGN(key
.offset
+ size
,
5511 sctx
->send_root
->fs_info
->sectorsize
);
5513 extent_end
= key
.offset
+
5514 btrfs_file_extent_num_bytes(path
->nodes
[0], fi
);
5516 sctx
->cur_inode_last_extent
= extent_end
;
5518 btrfs_free_path(path
);
5522 static int range_is_hole_in_parent(struct send_ctx
*sctx
,
5526 struct btrfs_path
*path
;
5527 struct btrfs_key key
;
5528 struct btrfs_root
*root
= sctx
->parent_root
;
5529 u64 search_start
= start
;
5532 path
= alloc_path_for_send();
5536 key
.objectid
= sctx
->cur_ino
;
5537 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5538 key
.offset
= search_start
;
5539 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5542 if (ret
> 0 && path
->slots
[0] > 0)
5545 while (search_start
< end
) {
5546 struct extent_buffer
*leaf
= path
->nodes
[0];
5547 int slot
= path
->slots
[0];
5548 struct btrfs_file_extent_item
*fi
;
5551 if (slot
>= btrfs_header_nritems(leaf
)) {
5552 ret
= btrfs_next_leaf(root
, path
);
5560 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
5561 if (key
.objectid
< sctx
->cur_ino
||
5562 key
.type
< BTRFS_EXTENT_DATA_KEY
)
5564 if (key
.objectid
> sctx
->cur_ino
||
5565 key
.type
> BTRFS_EXTENT_DATA_KEY
||
5569 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
5570 if (btrfs_file_extent_type(leaf
, fi
) ==
5571 BTRFS_FILE_EXTENT_INLINE
) {
5572 u64 size
= btrfs_file_extent_ram_bytes(leaf
, fi
);
5574 extent_end
= ALIGN(key
.offset
+ size
,
5575 root
->fs_info
->sectorsize
);
5577 extent_end
= key
.offset
+
5578 btrfs_file_extent_num_bytes(leaf
, fi
);
5580 if (extent_end
<= start
)
5582 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) == 0) {
5583 search_start
= extent_end
;
5593 btrfs_free_path(path
);
5597 static int maybe_send_hole(struct send_ctx
*sctx
, struct btrfs_path
*path
,
5598 struct btrfs_key
*key
)
5600 struct btrfs_file_extent_item
*fi
;
5605 if (sctx
->cur_ino
!= key
->objectid
|| !need_send_hole(sctx
))
5608 if (sctx
->cur_inode_last_extent
== (u64
)-1) {
5609 ret
= get_last_extent(sctx
, key
->offset
- 1);
5614 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5615 struct btrfs_file_extent_item
);
5616 type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
5617 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
5618 u64 size
= btrfs_file_extent_ram_bytes(path
->nodes
[0], fi
);
5619 extent_end
= ALIGN(key
->offset
+ size
,
5620 sctx
->send_root
->fs_info
->sectorsize
);
5622 extent_end
= key
->offset
+
5623 btrfs_file_extent_num_bytes(path
->nodes
[0], fi
);
5626 if (path
->slots
[0] == 0 &&
5627 sctx
->cur_inode_last_extent
< key
->offset
) {
5629 * We might have skipped entire leafs that contained only
5630 * file extent items for our current inode. These leafs have
5631 * a generation number smaller (older) than the one in the
5632 * current leaf and the leaf our last extent came from, and
5633 * are located between these 2 leafs.
5635 ret
= get_last_extent(sctx
, key
->offset
- 1);
5640 if (sctx
->cur_inode_last_extent
< key
->offset
) {
5641 ret
= range_is_hole_in_parent(sctx
,
5642 sctx
->cur_inode_last_extent
,
5647 ret
= send_hole(sctx
, key
->offset
);
5651 sctx
->cur_inode_last_extent
= extent_end
;
5655 static int process_extent(struct send_ctx
*sctx
,
5656 struct btrfs_path
*path
,
5657 struct btrfs_key
*key
)
5659 struct clone_root
*found_clone
= NULL
;
5662 if (S_ISLNK(sctx
->cur_inode_mode
))
5665 if (sctx
->parent_root
&& !sctx
->cur_inode_new
) {
5666 ret
= is_extent_unchanged(sctx
, path
, key
);
5674 struct btrfs_file_extent_item
*ei
;
5677 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5678 struct btrfs_file_extent_item
);
5679 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
5680 if (type
== BTRFS_FILE_EXTENT_PREALLOC
||
5681 type
== BTRFS_FILE_EXTENT_REG
) {
5683 * The send spec does not have a prealloc command yet,
5684 * so just leave a hole for prealloc'ed extents until
5685 * we have enough commands queued up to justify rev'ing
5688 if (type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5693 /* Have a hole, just skip it. */
5694 if (btrfs_file_extent_disk_bytenr(path
->nodes
[0], ei
) == 0) {
5701 ret
= find_extent_clone(sctx
, path
, key
->objectid
, key
->offset
,
5702 sctx
->cur_inode_size
, &found_clone
);
5703 if (ret
!= -ENOENT
&& ret
< 0)
5706 ret
= send_write_or_clone(sctx
, path
, key
, found_clone
);
5710 ret
= maybe_send_hole(sctx
, path
, key
);
5715 static int process_all_extents(struct send_ctx
*sctx
)
5718 struct btrfs_root
*root
;
5719 struct btrfs_path
*path
;
5720 struct btrfs_key key
;
5721 struct btrfs_key found_key
;
5722 struct extent_buffer
*eb
;
5725 root
= sctx
->send_root
;
5726 path
= alloc_path_for_send();
5730 key
.objectid
= sctx
->cmp_key
->objectid
;
5731 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5733 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5738 eb
= path
->nodes
[0];
5739 slot
= path
->slots
[0];
5741 if (slot
>= btrfs_header_nritems(eb
)) {
5742 ret
= btrfs_next_leaf(root
, path
);
5745 } else if (ret
> 0) {
5752 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5754 if (found_key
.objectid
!= key
.objectid
||
5755 found_key
.type
!= key
.type
) {
5760 ret
= process_extent(sctx
, path
, &found_key
);
5768 btrfs_free_path(path
);
5772 static int process_recorded_refs_if_needed(struct send_ctx
*sctx
, int at_end
,
5774 int *refs_processed
)
5778 if (sctx
->cur_ino
== 0)
5780 if (!at_end
&& sctx
->cur_ino
== sctx
->cmp_key
->objectid
&&
5781 sctx
->cmp_key
->type
<= BTRFS_INODE_EXTREF_KEY
)
5783 if (list_empty(&sctx
->new_refs
) && list_empty(&sctx
->deleted_refs
))
5786 ret
= process_recorded_refs(sctx
, pending_move
);
5790 *refs_processed
= 1;
5795 static int finish_inode_if_needed(struct send_ctx
*sctx
, int at_end
)
5806 int need_truncate
= 1;
5807 int pending_move
= 0;
5808 int refs_processed
= 0;
5810 if (sctx
->ignore_cur_inode
)
5813 ret
= process_recorded_refs_if_needed(sctx
, at_end
, &pending_move
,
5819 * We have processed the refs and thus need to advance send_progress.
5820 * Now, calls to get_cur_xxx will take the updated refs of the current
5821 * inode into account.
5823 * On the other hand, if our current inode is a directory and couldn't
5824 * be moved/renamed because its parent was renamed/moved too and it has
5825 * a higher inode number, we can only move/rename our current inode
5826 * after we moved/renamed its parent. Therefore in this case operate on
5827 * the old path (pre move/rename) of our current inode, and the
5828 * move/rename will be performed later.
5830 if (refs_processed
&& !pending_move
)
5831 sctx
->send_progress
= sctx
->cur_ino
+ 1;
5833 if (sctx
->cur_ino
== 0 || sctx
->cur_inode_deleted
)
5835 if (!at_end
&& sctx
->cmp_key
->objectid
== sctx
->cur_ino
)
5838 ret
= get_inode_info(sctx
->send_root
, sctx
->cur_ino
, NULL
, NULL
,
5839 &left_mode
, &left_uid
, &left_gid
, NULL
);
5843 if (!sctx
->parent_root
|| sctx
->cur_inode_new
) {
5845 if (!S_ISLNK(sctx
->cur_inode_mode
))
5847 if (sctx
->cur_inode_next_write_offset
== sctx
->cur_inode_size
)
5852 ret
= get_inode_info(sctx
->parent_root
, sctx
->cur_ino
,
5853 &old_size
, NULL
, &right_mode
, &right_uid
,
5858 if (left_uid
!= right_uid
|| left_gid
!= right_gid
)
5860 if (!S_ISLNK(sctx
->cur_inode_mode
) && left_mode
!= right_mode
)
5862 if ((old_size
== sctx
->cur_inode_size
) ||
5863 (sctx
->cur_inode_size
> old_size
&&
5864 sctx
->cur_inode_next_write_offset
== sctx
->cur_inode_size
))
5868 if (S_ISREG(sctx
->cur_inode_mode
)) {
5869 if (need_send_hole(sctx
)) {
5870 if (sctx
->cur_inode_last_extent
== (u64
)-1 ||
5871 sctx
->cur_inode_last_extent
<
5872 sctx
->cur_inode_size
) {
5873 ret
= get_last_extent(sctx
, (u64
)-1);
5877 if (sctx
->cur_inode_last_extent
<
5878 sctx
->cur_inode_size
) {
5879 ret
= send_hole(sctx
, sctx
->cur_inode_size
);
5884 if (need_truncate
) {
5885 ret
= send_truncate(sctx
, sctx
->cur_ino
,
5886 sctx
->cur_inode_gen
,
5887 sctx
->cur_inode_size
);
5894 ret
= send_chown(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
5895 left_uid
, left_gid
);
5900 ret
= send_chmod(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
5907 * If other directory inodes depended on our current directory
5908 * inode's move/rename, now do their move/rename operations.
5910 if (!is_waiting_for_move(sctx
, sctx
->cur_ino
)) {
5911 ret
= apply_children_dir_moves(sctx
);
5915 * Need to send that every time, no matter if it actually
5916 * changed between the two trees as we have done changes to
5917 * the inode before. If our inode is a directory and it's
5918 * waiting to be moved/renamed, we will send its utimes when
5919 * it's moved/renamed, therefore we don't need to do it here.
5921 sctx
->send_progress
= sctx
->cur_ino
+ 1;
5922 ret
= send_utimes(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
);
5931 struct parent_paths_ctx
{
5932 struct list_head
*refs
;
5933 struct send_ctx
*sctx
;
5936 static int record_parent_ref(int num
, u64 dir
, int index
, struct fs_path
*name
,
5939 struct parent_paths_ctx
*ppctx
= ctx
;
5941 return record_ref(ppctx
->sctx
->parent_root
, dir
, name
, ppctx
->sctx
,
5946 * Issue unlink operations for all paths of the current inode found in the
5949 static int btrfs_unlink_all_paths(struct send_ctx
*sctx
)
5951 LIST_HEAD(deleted_refs
);
5952 struct btrfs_path
*path
;
5953 struct btrfs_key key
;
5954 struct parent_paths_ctx ctx
;
5957 path
= alloc_path_for_send();
5961 key
.objectid
= sctx
->cur_ino
;
5962 key
.type
= BTRFS_INODE_REF_KEY
;
5964 ret
= btrfs_search_slot(NULL
, sctx
->parent_root
, &key
, path
, 0, 0);
5968 ctx
.refs
= &deleted_refs
;
5972 struct extent_buffer
*eb
= path
->nodes
[0];
5973 int slot
= path
->slots
[0];
5975 if (slot
>= btrfs_header_nritems(eb
)) {
5976 ret
= btrfs_next_leaf(sctx
->parent_root
, path
);
5984 btrfs_item_key_to_cpu(eb
, &key
, slot
);
5985 if (key
.objectid
!= sctx
->cur_ino
)
5987 if (key
.type
!= BTRFS_INODE_REF_KEY
&&
5988 key
.type
!= BTRFS_INODE_EXTREF_KEY
)
5991 ret
= iterate_inode_ref(sctx
->parent_root
, path
, &key
, 1,
5992 record_parent_ref
, &ctx
);
5999 while (!list_empty(&deleted_refs
)) {
6000 struct recorded_ref
*ref
;
6002 ref
= list_first_entry(&deleted_refs
, struct recorded_ref
, list
);
6003 ret
= send_unlink(sctx
, ref
->full_path
);
6006 fs_path_free(ref
->full_path
);
6007 list_del(&ref
->list
);
6012 btrfs_free_path(path
);
6014 __free_recorded_refs(&deleted_refs
);
6018 static int changed_inode(struct send_ctx
*sctx
,
6019 enum btrfs_compare_tree_result result
)
6022 struct btrfs_key
*key
= sctx
->cmp_key
;
6023 struct btrfs_inode_item
*left_ii
= NULL
;
6024 struct btrfs_inode_item
*right_ii
= NULL
;
6028 sctx
->cur_ino
= key
->objectid
;
6029 sctx
->cur_inode_new_gen
= 0;
6030 sctx
->cur_inode_last_extent
= (u64
)-1;
6031 sctx
->cur_inode_next_write_offset
= 0;
6032 sctx
->ignore_cur_inode
= false;
6035 * Set send_progress to current inode. This will tell all get_cur_xxx
6036 * functions that the current inode's refs are not updated yet. Later,
6037 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6039 sctx
->send_progress
= sctx
->cur_ino
;
6041 if (result
== BTRFS_COMPARE_TREE_NEW
||
6042 result
== BTRFS_COMPARE_TREE_CHANGED
) {
6043 left_ii
= btrfs_item_ptr(sctx
->left_path
->nodes
[0],
6044 sctx
->left_path
->slots
[0],
6045 struct btrfs_inode_item
);
6046 left_gen
= btrfs_inode_generation(sctx
->left_path
->nodes
[0],
6049 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
6050 sctx
->right_path
->slots
[0],
6051 struct btrfs_inode_item
);
6052 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
6055 if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
6056 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
6057 sctx
->right_path
->slots
[0],
6058 struct btrfs_inode_item
);
6060 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
6064 * The cur_ino = root dir case is special here. We can't treat
6065 * the inode as deleted+reused because it would generate a
6066 * stream that tries to delete/mkdir the root dir.
6068 if (left_gen
!= right_gen
&&
6069 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
6070 sctx
->cur_inode_new_gen
= 1;
6074 * Normally we do not find inodes with a link count of zero (orphans)
6075 * because the most common case is to create a snapshot and use it
6076 * for a send operation. However other less common use cases involve
6077 * using a subvolume and send it after turning it to RO mode just
6078 * after deleting all hard links of a file while holding an open
6079 * file descriptor against it or turning a RO snapshot into RW mode,
6080 * keep an open file descriptor against a file, delete it and then
6081 * turn the snapshot back to RO mode before using it for a send
6082 * operation. So if we find such cases, ignore the inode and all its
6083 * items completely if it's a new inode, or if it's a changed inode
6084 * make sure all its previous paths (from the parent snapshot) are all
6085 * unlinked and all other the inode items are ignored.
6087 if (result
== BTRFS_COMPARE_TREE_NEW
||
6088 result
== BTRFS_COMPARE_TREE_CHANGED
) {
6091 nlinks
= btrfs_inode_nlink(sctx
->left_path
->nodes
[0], left_ii
);
6093 sctx
->ignore_cur_inode
= true;
6094 if (result
== BTRFS_COMPARE_TREE_CHANGED
)
6095 ret
= btrfs_unlink_all_paths(sctx
);
6100 if (result
== BTRFS_COMPARE_TREE_NEW
) {
6101 sctx
->cur_inode_gen
= left_gen
;
6102 sctx
->cur_inode_new
= 1;
6103 sctx
->cur_inode_deleted
= 0;
6104 sctx
->cur_inode_size
= btrfs_inode_size(
6105 sctx
->left_path
->nodes
[0], left_ii
);
6106 sctx
->cur_inode_mode
= btrfs_inode_mode(
6107 sctx
->left_path
->nodes
[0], left_ii
);
6108 sctx
->cur_inode_rdev
= btrfs_inode_rdev(
6109 sctx
->left_path
->nodes
[0], left_ii
);
6110 if (sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
6111 ret
= send_create_inode_if_needed(sctx
);
6112 } else if (result
== BTRFS_COMPARE_TREE_DELETED
) {
6113 sctx
->cur_inode_gen
= right_gen
;
6114 sctx
->cur_inode_new
= 0;
6115 sctx
->cur_inode_deleted
= 1;
6116 sctx
->cur_inode_size
= btrfs_inode_size(
6117 sctx
->right_path
->nodes
[0], right_ii
);
6118 sctx
->cur_inode_mode
= btrfs_inode_mode(
6119 sctx
->right_path
->nodes
[0], right_ii
);
6120 } else if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
6122 * We need to do some special handling in case the inode was
6123 * reported as changed with a changed generation number. This
6124 * means that the original inode was deleted and new inode
6125 * reused the same inum. So we have to treat the old inode as
6126 * deleted and the new one as new.
6128 if (sctx
->cur_inode_new_gen
) {
6130 * First, process the inode as if it was deleted.
6132 sctx
->cur_inode_gen
= right_gen
;
6133 sctx
->cur_inode_new
= 0;
6134 sctx
->cur_inode_deleted
= 1;
6135 sctx
->cur_inode_size
= btrfs_inode_size(
6136 sctx
->right_path
->nodes
[0], right_ii
);
6137 sctx
->cur_inode_mode
= btrfs_inode_mode(
6138 sctx
->right_path
->nodes
[0], right_ii
);
6139 ret
= process_all_refs(sctx
,
6140 BTRFS_COMPARE_TREE_DELETED
);
6145 * Now process the inode as if it was new.
6147 sctx
->cur_inode_gen
= left_gen
;
6148 sctx
->cur_inode_new
= 1;
6149 sctx
->cur_inode_deleted
= 0;
6150 sctx
->cur_inode_size
= btrfs_inode_size(
6151 sctx
->left_path
->nodes
[0], left_ii
);
6152 sctx
->cur_inode_mode
= btrfs_inode_mode(
6153 sctx
->left_path
->nodes
[0], left_ii
);
6154 sctx
->cur_inode_rdev
= btrfs_inode_rdev(
6155 sctx
->left_path
->nodes
[0], left_ii
);
6156 ret
= send_create_inode_if_needed(sctx
);
6160 ret
= process_all_refs(sctx
, BTRFS_COMPARE_TREE_NEW
);
6164 * Advance send_progress now as we did not get into
6165 * process_recorded_refs_if_needed in the new_gen case.
6167 sctx
->send_progress
= sctx
->cur_ino
+ 1;
6170 * Now process all extents and xattrs of the inode as if
6171 * they were all new.
6173 ret
= process_all_extents(sctx
);
6176 ret
= process_all_new_xattrs(sctx
);
6180 sctx
->cur_inode_gen
= left_gen
;
6181 sctx
->cur_inode_new
= 0;
6182 sctx
->cur_inode_new_gen
= 0;
6183 sctx
->cur_inode_deleted
= 0;
6184 sctx
->cur_inode_size
= btrfs_inode_size(
6185 sctx
->left_path
->nodes
[0], left_ii
);
6186 sctx
->cur_inode_mode
= btrfs_inode_mode(
6187 sctx
->left_path
->nodes
[0], left_ii
);
6196 * We have to process new refs before deleted refs, but compare_trees gives us
6197 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6198 * first and later process them in process_recorded_refs.
6199 * For the cur_inode_new_gen case, we skip recording completely because
6200 * changed_inode did already initiate processing of refs. The reason for this is
6201 * that in this case, compare_tree actually compares the refs of 2 different
6202 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6203 * refs of the right tree as deleted and all refs of the left tree as new.
6205 static int changed_ref(struct send_ctx
*sctx
,
6206 enum btrfs_compare_tree_result result
)
6210 if (sctx
->cur_ino
!= sctx
->cmp_key
->objectid
) {
6211 inconsistent_snapshot_error(sctx
, result
, "reference");
6215 if (!sctx
->cur_inode_new_gen
&&
6216 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
6217 if (result
== BTRFS_COMPARE_TREE_NEW
)
6218 ret
= record_new_ref(sctx
);
6219 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
6220 ret
= record_deleted_ref(sctx
);
6221 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
6222 ret
= record_changed_ref(sctx
);
6229 * Process new/deleted/changed xattrs. We skip processing in the
6230 * cur_inode_new_gen case because changed_inode did already initiate processing
6231 * of xattrs. The reason is the same as in changed_ref
6233 static int changed_xattr(struct send_ctx
*sctx
,
6234 enum btrfs_compare_tree_result result
)
6238 if (sctx
->cur_ino
!= sctx
->cmp_key
->objectid
) {
6239 inconsistent_snapshot_error(sctx
, result
, "xattr");
6243 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
6244 if (result
== BTRFS_COMPARE_TREE_NEW
)
6245 ret
= process_new_xattr(sctx
);
6246 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
6247 ret
= process_deleted_xattr(sctx
);
6248 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
6249 ret
= process_changed_xattr(sctx
);
6256 * Process new/deleted/changed extents. We skip processing in the
6257 * cur_inode_new_gen case because changed_inode did already initiate processing
6258 * of extents. The reason is the same as in changed_ref
6260 static int changed_extent(struct send_ctx
*sctx
,
6261 enum btrfs_compare_tree_result result
)
6265 if (sctx
->cur_ino
!= sctx
->cmp_key
->objectid
) {
6267 if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
6268 struct extent_buffer
*leaf_l
;
6269 struct extent_buffer
*leaf_r
;
6270 struct btrfs_file_extent_item
*ei_l
;
6271 struct btrfs_file_extent_item
*ei_r
;
6273 leaf_l
= sctx
->left_path
->nodes
[0];
6274 leaf_r
= sctx
->right_path
->nodes
[0];
6275 ei_l
= btrfs_item_ptr(leaf_l
,
6276 sctx
->left_path
->slots
[0],
6277 struct btrfs_file_extent_item
);
6278 ei_r
= btrfs_item_ptr(leaf_r
,
6279 sctx
->right_path
->slots
[0],
6280 struct btrfs_file_extent_item
);
6283 * We may have found an extent item that has changed
6284 * only its disk_bytenr field and the corresponding
6285 * inode item was not updated. This case happens due to
6286 * very specific timings during relocation when a leaf
6287 * that contains file extent items is COWed while
6288 * relocation is ongoing and its in the stage where it
6289 * updates data pointers. So when this happens we can
6290 * safely ignore it since we know it's the same extent,
6291 * but just at different logical and physical locations
6292 * (when an extent is fully replaced with a new one, we
6293 * know the generation number must have changed too,
6294 * since snapshot creation implies committing the current
6295 * transaction, and the inode item must have been updated
6297 * This replacement of the disk_bytenr happens at
6298 * relocation.c:replace_file_extents() through
6299 * relocation.c:btrfs_reloc_cow_block().
6301 if (btrfs_file_extent_generation(leaf_l
, ei_l
) ==
6302 btrfs_file_extent_generation(leaf_r
, ei_r
) &&
6303 btrfs_file_extent_ram_bytes(leaf_l
, ei_l
) ==
6304 btrfs_file_extent_ram_bytes(leaf_r
, ei_r
) &&
6305 btrfs_file_extent_compression(leaf_l
, ei_l
) ==
6306 btrfs_file_extent_compression(leaf_r
, ei_r
) &&
6307 btrfs_file_extent_encryption(leaf_l
, ei_l
) ==
6308 btrfs_file_extent_encryption(leaf_r
, ei_r
) &&
6309 btrfs_file_extent_other_encoding(leaf_l
, ei_l
) ==
6310 btrfs_file_extent_other_encoding(leaf_r
, ei_r
) &&
6311 btrfs_file_extent_type(leaf_l
, ei_l
) ==
6312 btrfs_file_extent_type(leaf_r
, ei_r
) &&
6313 btrfs_file_extent_disk_bytenr(leaf_l
, ei_l
) !=
6314 btrfs_file_extent_disk_bytenr(leaf_r
, ei_r
) &&
6315 btrfs_file_extent_disk_num_bytes(leaf_l
, ei_l
) ==
6316 btrfs_file_extent_disk_num_bytes(leaf_r
, ei_r
) &&
6317 btrfs_file_extent_offset(leaf_l
, ei_l
) ==
6318 btrfs_file_extent_offset(leaf_r
, ei_r
) &&
6319 btrfs_file_extent_num_bytes(leaf_l
, ei_l
) ==
6320 btrfs_file_extent_num_bytes(leaf_r
, ei_r
))
6324 inconsistent_snapshot_error(sctx
, result
, "extent");
6328 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
6329 if (result
!= BTRFS_COMPARE_TREE_DELETED
)
6330 ret
= process_extent(sctx
, sctx
->left_path
,
6337 static int dir_changed(struct send_ctx
*sctx
, u64 dir
)
6339 u64 orig_gen
, new_gen
;
6342 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &new_gen
, NULL
, NULL
,
6347 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &orig_gen
, NULL
,
6352 return (orig_gen
!= new_gen
) ? 1 : 0;
6355 static int compare_refs(struct send_ctx
*sctx
, struct btrfs_path
*path
,
6356 struct btrfs_key
*key
)
6358 struct btrfs_inode_extref
*extref
;
6359 struct extent_buffer
*leaf
;
6360 u64 dirid
= 0, last_dirid
= 0;
6367 /* Easy case, just check this one dirid */
6368 if (key
->type
== BTRFS_INODE_REF_KEY
) {
6369 dirid
= key
->offset
;
6371 ret
= dir_changed(sctx
, dirid
);
6375 leaf
= path
->nodes
[0];
6376 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
6377 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
6378 while (cur_offset
< item_size
) {
6379 extref
= (struct btrfs_inode_extref
*)(ptr
+
6381 dirid
= btrfs_inode_extref_parent(leaf
, extref
);
6382 ref_name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
6383 cur_offset
+= ref_name_len
+ sizeof(*extref
);
6384 if (dirid
== last_dirid
)
6386 ret
= dir_changed(sctx
, dirid
);
6396 * Updates compare related fields in sctx and simply forwards to the actual
6397 * changed_xxx functions.
6399 static int changed_cb(struct btrfs_path
*left_path
,
6400 struct btrfs_path
*right_path
,
6401 struct btrfs_key
*key
,
6402 enum btrfs_compare_tree_result result
,
6406 struct send_ctx
*sctx
= ctx
;
6408 if (result
== BTRFS_COMPARE_TREE_SAME
) {
6409 if (key
->type
== BTRFS_INODE_REF_KEY
||
6410 key
->type
== BTRFS_INODE_EXTREF_KEY
) {
6411 ret
= compare_refs(sctx
, left_path
, key
);
6416 } else if (key
->type
== BTRFS_EXTENT_DATA_KEY
) {
6417 return maybe_send_hole(sctx
, left_path
, key
);
6421 result
= BTRFS_COMPARE_TREE_CHANGED
;
6425 sctx
->left_path
= left_path
;
6426 sctx
->right_path
= right_path
;
6427 sctx
->cmp_key
= key
;
6429 ret
= finish_inode_if_needed(sctx
, 0);
6433 /* Ignore non-FS objects */
6434 if (key
->objectid
== BTRFS_FREE_INO_OBJECTID
||
6435 key
->objectid
== BTRFS_FREE_SPACE_OBJECTID
)
6438 if (key
->type
== BTRFS_INODE_ITEM_KEY
) {
6439 ret
= changed_inode(sctx
, result
);
6440 } else if (!sctx
->ignore_cur_inode
) {
6441 if (key
->type
== BTRFS_INODE_REF_KEY
||
6442 key
->type
== BTRFS_INODE_EXTREF_KEY
)
6443 ret
= changed_ref(sctx
, result
);
6444 else if (key
->type
== BTRFS_XATTR_ITEM_KEY
)
6445 ret
= changed_xattr(sctx
, result
);
6446 else if (key
->type
== BTRFS_EXTENT_DATA_KEY
)
6447 ret
= changed_extent(sctx
, result
);
6454 static int full_send_tree(struct send_ctx
*sctx
)
6457 struct btrfs_root
*send_root
= sctx
->send_root
;
6458 struct btrfs_key key
;
6459 struct btrfs_path
*path
;
6460 struct extent_buffer
*eb
;
6463 path
= alloc_path_for_send();
6467 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
6468 key
.type
= BTRFS_INODE_ITEM_KEY
;
6471 ret
= btrfs_search_slot_for_read(send_root
, &key
, path
, 1, 0);
6478 eb
= path
->nodes
[0];
6479 slot
= path
->slots
[0];
6480 btrfs_item_key_to_cpu(eb
, &key
, slot
);
6482 ret
= changed_cb(path
, NULL
, &key
,
6483 BTRFS_COMPARE_TREE_NEW
, sctx
);
6487 ret
= btrfs_next_item(send_root
, path
);
6497 ret
= finish_inode_if_needed(sctx
, 1);
6500 btrfs_free_path(path
);
6504 static int send_subvol(struct send_ctx
*sctx
)
6508 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_STREAM_HEADER
)) {
6509 ret
= send_header(sctx
);
6514 ret
= send_subvol_begin(sctx
);
6518 if (sctx
->parent_root
) {
6519 ret
= btrfs_compare_trees(sctx
->send_root
, sctx
->parent_root
,
6523 ret
= finish_inode_if_needed(sctx
, 1);
6527 ret
= full_send_tree(sctx
);
6533 free_recorded_refs(sctx
);
6538 * If orphan cleanup did remove any orphans from a root, it means the tree
6539 * was modified and therefore the commit root is not the same as the current
6540 * root anymore. This is a problem, because send uses the commit root and
6541 * therefore can see inode items that don't exist in the current root anymore,
6542 * and for example make calls to btrfs_iget, which will do tree lookups based
6543 * on the current root and not on the commit root. Those lookups will fail,
6544 * returning a -ESTALE error, and making send fail with that error. So make
6545 * sure a send does not see any orphans we have just removed, and that it will
6546 * see the same inodes regardless of whether a transaction commit happened
6547 * before it started (meaning that the commit root will be the same as the
6548 * current root) or not.
6550 static int ensure_commit_roots_uptodate(struct send_ctx
*sctx
)
6553 struct btrfs_trans_handle
*trans
= NULL
;
6556 if (sctx
->parent_root
&&
6557 sctx
->parent_root
->node
!= sctx
->parent_root
->commit_root
)
6560 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++)
6561 if (sctx
->clone_roots
[i
].root
->node
!=
6562 sctx
->clone_roots
[i
].root
->commit_root
)
6566 return btrfs_end_transaction(trans
);
6571 /* Use any root, all fs roots will get their commit roots updated. */
6573 trans
= btrfs_join_transaction(sctx
->send_root
);
6575 return PTR_ERR(trans
);
6579 return btrfs_commit_transaction(trans
);
6582 static void btrfs_root_dec_send_in_progress(struct btrfs_root
* root
)
6584 spin_lock(&root
->root_item_lock
);
6585 root
->send_in_progress
--;
6587 * Not much left to do, we don't know why it's unbalanced and
6588 * can't blindly reset it to 0.
6590 if (root
->send_in_progress
< 0)
6591 btrfs_err(root
->fs_info
,
6592 "send_in_progress unbalanced %d root %llu",
6593 root
->send_in_progress
, root
->root_key
.objectid
);
6594 spin_unlock(&root
->root_item_lock
);
6597 long btrfs_ioctl_send(struct file
*mnt_file
, struct btrfs_ioctl_send_args
*arg
)
6600 struct btrfs_root
*send_root
= BTRFS_I(file_inode(mnt_file
))->root
;
6601 struct btrfs_fs_info
*fs_info
= send_root
->fs_info
;
6602 struct btrfs_root
*clone_root
;
6603 struct btrfs_key key
;
6604 struct send_ctx
*sctx
= NULL
;
6606 u64
*clone_sources_tmp
= NULL
;
6607 int clone_sources_to_rollback
= 0;
6608 unsigned alloc_size
;
6609 int sort_clone_roots
= 0;
6612 if (!capable(CAP_SYS_ADMIN
))
6616 * The subvolume must remain read-only during send, protect against
6617 * making it RW. This also protects against deletion.
6619 spin_lock(&send_root
->root_item_lock
);
6620 send_root
->send_in_progress
++;
6621 spin_unlock(&send_root
->root_item_lock
);
6624 * This is done when we lookup the root, it should already be complete
6625 * by the time we get here.
6627 WARN_ON(send_root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
);
6630 * Userspace tools do the checks and warn the user if it's
6633 if (!btrfs_root_readonly(send_root
)) {
6639 * Check that we don't overflow at later allocations, we request
6640 * clone_sources_count + 1 items, and compare to unsigned long inside
6643 if (arg
->clone_sources_count
>
6644 ULONG_MAX
/ sizeof(struct clone_root
) - 1) {
6649 if (!access_ok(VERIFY_READ
, arg
->clone_sources
,
6650 sizeof(*arg
->clone_sources
) *
6651 arg
->clone_sources_count
)) {
6656 if (arg
->flags
& ~BTRFS_SEND_FLAG_MASK
) {
6661 sctx
= kzalloc(sizeof(struct send_ctx
), GFP_KERNEL
);
6667 INIT_LIST_HEAD(&sctx
->new_refs
);
6668 INIT_LIST_HEAD(&sctx
->deleted_refs
);
6669 INIT_RADIX_TREE(&sctx
->name_cache
, GFP_KERNEL
);
6670 INIT_LIST_HEAD(&sctx
->name_cache_list
);
6672 sctx
->flags
= arg
->flags
;
6674 sctx
->send_filp
= fget(arg
->send_fd
);
6675 if (!sctx
->send_filp
) {
6680 sctx
->send_root
= send_root
;
6682 * Unlikely but possible, if the subvolume is marked for deletion but
6683 * is slow to remove the directory entry, send can still be started
6685 if (btrfs_root_dead(sctx
->send_root
)) {
6690 sctx
->clone_roots_cnt
= arg
->clone_sources_count
;
6692 sctx
->send_max_size
= BTRFS_SEND_BUF_SIZE
;
6693 sctx
->send_buf
= kvmalloc(sctx
->send_max_size
, GFP_KERNEL
);
6694 if (!sctx
->send_buf
) {
6699 sctx
->read_buf
= kvmalloc(BTRFS_SEND_READ_SIZE
, GFP_KERNEL
);
6700 if (!sctx
->read_buf
) {
6705 sctx
->pending_dir_moves
= RB_ROOT
;
6706 sctx
->waiting_dir_moves
= RB_ROOT
;
6707 sctx
->orphan_dirs
= RB_ROOT
;
6709 alloc_size
= sizeof(struct clone_root
) * (arg
->clone_sources_count
+ 1);
6711 sctx
->clone_roots
= kzalloc(alloc_size
, GFP_KERNEL
);
6712 if (!sctx
->clone_roots
) {
6717 alloc_size
= arg
->clone_sources_count
* sizeof(*arg
->clone_sources
);
6719 if (arg
->clone_sources_count
) {
6720 clone_sources_tmp
= kvmalloc(alloc_size
, GFP_KERNEL
);
6721 if (!clone_sources_tmp
) {
6726 ret
= copy_from_user(clone_sources_tmp
, arg
->clone_sources
,
6733 for (i
= 0; i
< arg
->clone_sources_count
; i
++) {
6734 key
.objectid
= clone_sources_tmp
[i
];
6735 key
.type
= BTRFS_ROOT_ITEM_KEY
;
6736 key
.offset
= (u64
)-1;
6738 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
6740 clone_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
6741 if (IS_ERR(clone_root
)) {
6742 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6743 ret
= PTR_ERR(clone_root
);
6746 spin_lock(&clone_root
->root_item_lock
);
6747 if (!btrfs_root_readonly(clone_root
) ||
6748 btrfs_root_dead(clone_root
)) {
6749 spin_unlock(&clone_root
->root_item_lock
);
6750 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6754 clone_root
->send_in_progress
++;
6755 spin_unlock(&clone_root
->root_item_lock
);
6756 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6758 sctx
->clone_roots
[i
].root
= clone_root
;
6759 clone_sources_to_rollback
= i
+ 1;
6761 kvfree(clone_sources_tmp
);
6762 clone_sources_tmp
= NULL
;
6765 if (arg
->parent_root
) {
6766 key
.objectid
= arg
->parent_root
;
6767 key
.type
= BTRFS_ROOT_ITEM_KEY
;
6768 key
.offset
= (u64
)-1;
6770 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
6772 sctx
->parent_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
6773 if (IS_ERR(sctx
->parent_root
)) {
6774 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6775 ret
= PTR_ERR(sctx
->parent_root
);
6779 spin_lock(&sctx
->parent_root
->root_item_lock
);
6780 sctx
->parent_root
->send_in_progress
++;
6781 if (!btrfs_root_readonly(sctx
->parent_root
) ||
6782 btrfs_root_dead(sctx
->parent_root
)) {
6783 spin_unlock(&sctx
->parent_root
->root_item_lock
);
6784 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6788 spin_unlock(&sctx
->parent_root
->root_item_lock
);
6790 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6794 * Clones from send_root are allowed, but only if the clone source
6795 * is behind the current send position. This is checked while searching
6796 * for possible clone sources.
6798 sctx
->clone_roots
[sctx
->clone_roots_cnt
++].root
= sctx
->send_root
;
6800 /* We do a bsearch later */
6801 sort(sctx
->clone_roots
, sctx
->clone_roots_cnt
,
6802 sizeof(*sctx
->clone_roots
), __clone_root_cmp_sort
,
6804 sort_clone_roots
= 1;
6806 ret
= ensure_commit_roots_uptodate(sctx
);
6810 current
->journal_info
= BTRFS_SEND_TRANS_STUB
;
6811 ret
= send_subvol(sctx
);
6812 current
->journal_info
= NULL
;
6816 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_END_CMD
)) {
6817 ret
= begin_cmd(sctx
, BTRFS_SEND_C_END
);
6820 ret
= send_cmd(sctx
);
6826 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->pending_dir_moves
));
6827 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->pending_dir_moves
)) {
6829 struct pending_dir_move
*pm
;
6831 n
= rb_first(&sctx
->pending_dir_moves
);
6832 pm
= rb_entry(n
, struct pending_dir_move
, node
);
6833 while (!list_empty(&pm
->list
)) {
6834 struct pending_dir_move
*pm2
;
6836 pm2
= list_first_entry(&pm
->list
,
6837 struct pending_dir_move
, list
);
6838 free_pending_move(sctx
, pm2
);
6840 free_pending_move(sctx
, pm
);
6843 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
));
6844 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
)) {
6846 struct waiting_dir_move
*dm
;
6848 n
= rb_first(&sctx
->waiting_dir_moves
);
6849 dm
= rb_entry(n
, struct waiting_dir_move
, node
);
6850 rb_erase(&dm
->node
, &sctx
->waiting_dir_moves
);
6854 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->orphan_dirs
));
6855 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->orphan_dirs
)) {
6857 struct orphan_dir_info
*odi
;
6859 n
= rb_first(&sctx
->orphan_dirs
);
6860 odi
= rb_entry(n
, struct orphan_dir_info
, node
);
6861 free_orphan_dir_info(sctx
, odi
);
6864 if (sort_clone_roots
) {
6865 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++)
6866 btrfs_root_dec_send_in_progress(
6867 sctx
->clone_roots
[i
].root
);
6869 for (i
= 0; sctx
&& i
< clone_sources_to_rollback
; i
++)
6870 btrfs_root_dec_send_in_progress(
6871 sctx
->clone_roots
[i
].root
);
6873 btrfs_root_dec_send_in_progress(send_root
);
6875 if (sctx
&& !IS_ERR_OR_NULL(sctx
->parent_root
))
6876 btrfs_root_dec_send_in_progress(sctx
->parent_root
);
6878 kvfree(clone_sources_tmp
);
6881 if (sctx
->send_filp
)
6882 fput(sctx
->send_filp
);
6884 kvfree(sctx
->clone_roots
);
6885 kvfree(sctx
->send_buf
);
6886 kvfree(sctx
->read_buf
);
6888 name_cache_free(sctx
);