2 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/bsearch.h>
21 #include <linux/file.h>
22 #include <linux/sort.h>
23 #include <linux/mount.h>
24 #include <linux/xattr.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/radix-tree.h>
27 #include <linux/vmalloc.h>
28 #include <linux/string.h>
35 #include "btrfs_inode.h"
36 #include "transaction.h"
39 static int g_verbose
= 0;
41 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
44 * A fs_path is a helper to dynamically build path names with unknown size.
45 * It reallocates the internal buffer on demand.
46 * It allows fast adding of path elements on the right side (normal path) and
47 * fast adding to the left side (reversed path). A reversed path can also be
48 * unreversed if needed.
57 unsigned short buf_len
:15;
58 unsigned short reversed
:1;
62 * Average path length does not exceed 200 bytes, we'll have
63 * better packing in the slab and higher chance to satisfy
64 * a allocation later during send.
69 #define FS_PATH_INLINE_SIZE \
70 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
73 /* reused for each extent */
75 struct btrfs_root
*root
;
82 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
83 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
86 struct file
*send_filp
;
92 u64 cmd_send_size
[BTRFS_SEND_C_MAX
+ 1];
93 u64 flags
; /* 'flags' member of btrfs_ioctl_send_args is u64 */
95 struct btrfs_root
*send_root
;
96 struct btrfs_root
*parent_root
;
97 struct clone_root
*clone_roots
;
100 /* current state of the compare_tree call */
101 struct btrfs_path
*left_path
;
102 struct btrfs_path
*right_path
;
103 struct btrfs_key
*cmp_key
;
106 * infos of the currently processed inode. In case of deleted inodes,
107 * these are the values from the deleted inode.
112 int cur_inode_new_gen
;
113 int cur_inode_deleted
;
117 u64 cur_inode_last_extent
;
121 struct list_head new_refs
;
122 struct list_head deleted_refs
;
124 struct radix_tree_root name_cache
;
125 struct list_head name_cache_list
;
128 struct file_ra_state ra
;
133 * We process inodes by their increasing order, so if before an
134 * incremental send we reverse the parent/child relationship of
135 * directories such that a directory with a lower inode number was
136 * the parent of a directory with a higher inode number, and the one
137 * becoming the new parent got renamed too, we can't rename/move the
138 * directory with lower inode number when we finish processing it - we
139 * must process the directory with higher inode number first, then
140 * rename/move it and then rename/move the directory with lower inode
141 * number. Example follows.
143 * Tree state when the first send was performed:
155 * Tree state when the second (incremental) send is performed:
164 * The sequence of steps that lead to the second state was:
166 * mv /a/b/c/d /a/b/c2/d2
167 * mv /a/b/c /a/b/c2/d2/cc
169 * "c" has lower inode number, but we can't move it (2nd mv operation)
170 * before we move "d", which has higher inode number.
172 * So we just memorize which move/rename operations must be performed
173 * later when their respective parent is processed and moved/renamed.
176 /* Indexed by parent directory inode number. */
177 struct rb_root pending_dir_moves
;
180 * Reverse index, indexed by the inode number of a directory that
181 * is waiting for the move/rename of its immediate parent before its
182 * own move/rename can be performed.
184 struct rb_root waiting_dir_moves
;
187 * A directory that is going to be rm'ed might have a child directory
188 * which is in the pending directory moves index above. In this case,
189 * the directory can only be removed after the move/rename of its child
190 * is performed. Example:
210 * Sequence of steps that lead to the send snapshot:
211 * rm -f /a/b/c/foo.txt
213 * mv /a/b/c/x /a/b/YY
216 * When the child is processed, its move/rename is delayed until its
217 * parent is processed (as explained above), but all other operations
218 * like update utimes, chown, chgrp, etc, are performed and the paths
219 * that it uses for those operations must use the orphanized name of
220 * its parent (the directory we're going to rm later), so we need to
221 * memorize that name.
223 * Indexed by the inode number of the directory to be deleted.
225 struct rb_root orphan_dirs
;
228 struct pending_dir_move
{
230 struct list_head list
;
235 struct list_head update_refs
;
238 struct waiting_dir_move
{
242 * There might be some directory that could not be removed because it
243 * was waiting for this directory inode to be moved first. Therefore
244 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
250 struct orphan_dir_info
{
256 struct name_cache_entry
{
257 struct list_head list
;
259 * radix_tree has only 32bit entries but we need to handle 64bit inums.
260 * We use the lower 32bit of the 64bit inum to store it in the tree. If
261 * more then one inum would fall into the same entry, we use radix_list
262 * to store the additional entries. radix_list is also used to store
263 * entries where two entries have the same inum but different
266 struct list_head radix_list
;
272 int need_later_update
;
277 static int is_waiting_for_move(struct send_ctx
*sctx
, u64 ino
);
279 static struct waiting_dir_move
*
280 get_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
);
282 static int is_waiting_for_rm(struct send_ctx
*sctx
, u64 dir_ino
);
284 static int need_send_hole(struct send_ctx
*sctx
)
286 return (sctx
->parent_root
&& !sctx
->cur_inode_new
&&
287 !sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
&&
288 S_ISREG(sctx
->cur_inode_mode
));
291 static void fs_path_reset(struct fs_path
*p
)
294 p
->start
= p
->buf
+ p
->buf_len
- 1;
304 static struct fs_path
*fs_path_alloc(void)
308 p
= kmalloc(sizeof(*p
), GFP_NOFS
);
312 p
->buf
= p
->inline_buf
;
313 p
->buf_len
= FS_PATH_INLINE_SIZE
;
318 static struct fs_path
*fs_path_alloc_reversed(void)
330 static void fs_path_free(struct fs_path
*p
)
334 if (p
->buf
!= p
->inline_buf
)
339 static int fs_path_len(struct fs_path
*p
)
341 return p
->end
- p
->start
;
344 static int fs_path_ensure_buf(struct fs_path
*p
, int len
)
352 if (p
->buf_len
>= len
)
355 if (len
> PATH_MAX
) {
360 path_len
= p
->end
- p
->start
;
361 old_buf_len
= p
->buf_len
;
364 * First time the inline_buf does not suffice
366 if (p
->buf
== p
->inline_buf
) {
367 tmp_buf
= kmalloc(len
, GFP_NOFS
);
369 memcpy(tmp_buf
, p
->buf
, old_buf_len
);
371 tmp_buf
= krealloc(p
->buf
, len
, GFP_NOFS
);
377 * The real size of the buffer is bigger, this will let the fast path
378 * happen most of the time
380 p
->buf_len
= ksize(p
->buf
);
383 tmp_buf
= p
->buf
+ old_buf_len
- path_len
- 1;
384 p
->end
= p
->buf
+ p
->buf_len
- 1;
385 p
->start
= p
->end
- path_len
;
386 memmove(p
->start
, tmp_buf
, path_len
+ 1);
389 p
->end
= p
->start
+ path_len
;
394 static int fs_path_prepare_for_add(struct fs_path
*p
, int name_len
,
400 new_len
= p
->end
- p
->start
+ name_len
;
401 if (p
->start
!= p
->end
)
403 ret
= fs_path_ensure_buf(p
, new_len
);
408 if (p
->start
!= p
->end
)
410 p
->start
-= name_len
;
411 *prepared
= p
->start
;
413 if (p
->start
!= p
->end
)
424 static int fs_path_add(struct fs_path
*p
, const char *name
, int name_len
)
429 ret
= fs_path_prepare_for_add(p
, name_len
, &prepared
);
432 memcpy(prepared
, name
, name_len
);
438 static int fs_path_add_path(struct fs_path
*p
, struct fs_path
*p2
)
443 ret
= fs_path_prepare_for_add(p
, p2
->end
- p2
->start
, &prepared
);
446 memcpy(prepared
, p2
->start
, p2
->end
- p2
->start
);
452 static int fs_path_add_from_extent_buffer(struct fs_path
*p
,
453 struct extent_buffer
*eb
,
454 unsigned long off
, int len
)
459 ret
= fs_path_prepare_for_add(p
, len
, &prepared
);
463 read_extent_buffer(eb
, prepared
, off
, len
);
469 static int fs_path_copy(struct fs_path
*p
, struct fs_path
*from
)
473 p
->reversed
= from
->reversed
;
476 ret
= fs_path_add_path(p
, from
);
482 static void fs_path_unreverse(struct fs_path
*p
)
491 len
= p
->end
- p
->start
;
493 p
->end
= p
->start
+ len
;
494 memmove(p
->start
, tmp
, len
+ 1);
498 static struct btrfs_path
*alloc_path_for_send(void)
500 struct btrfs_path
*path
;
502 path
= btrfs_alloc_path();
505 path
->search_commit_root
= 1;
506 path
->skip_locking
= 1;
507 path
->need_commit_sem
= 1;
511 static int write_buf(struct file
*filp
, const void *buf
, u32 len
, loff_t
*off
)
521 ret
= vfs_write(filp
, (__force
const char __user
*)buf
+ pos
,
523 /* TODO handle that correctly */
524 /*if (ret == -ERESTARTSYS) {
543 static int tlv_put(struct send_ctx
*sctx
, u16 attr
, const void *data
, int len
)
545 struct btrfs_tlv_header
*hdr
;
546 int total_len
= sizeof(*hdr
) + len
;
547 int left
= sctx
->send_max_size
- sctx
->send_size
;
549 if (unlikely(left
< total_len
))
552 hdr
= (struct btrfs_tlv_header
*) (sctx
->send_buf
+ sctx
->send_size
);
553 hdr
->tlv_type
= cpu_to_le16(attr
);
554 hdr
->tlv_len
= cpu_to_le16(len
);
555 memcpy(hdr
+ 1, data
, len
);
556 sctx
->send_size
+= total_len
;
561 #define TLV_PUT_DEFINE_INT(bits) \
562 static int tlv_put_u##bits(struct send_ctx *sctx, \
563 u##bits attr, u##bits value) \
565 __le##bits __tmp = cpu_to_le##bits(value); \
566 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
569 TLV_PUT_DEFINE_INT(64)
571 static int tlv_put_string(struct send_ctx
*sctx
, u16 attr
,
572 const char *str
, int len
)
576 return tlv_put(sctx
, attr
, str
, len
);
579 static int tlv_put_uuid(struct send_ctx
*sctx
, u16 attr
,
582 return tlv_put(sctx
, attr
, uuid
, BTRFS_UUID_SIZE
);
585 static int tlv_put_btrfs_timespec(struct send_ctx
*sctx
, u16 attr
,
586 struct extent_buffer
*eb
,
587 struct btrfs_timespec
*ts
)
589 struct btrfs_timespec bts
;
590 read_extent_buffer(eb
, &bts
, (unsigned long)ts
, sizeof(bts
));
591 return tlv_put(sctx
, attr
, &bts
, sizeof(bts
));
595 #define TLV_PUT(sctx, attrtype, attrlen, data) \
597 ret = tlv_put(sctx, attrtype, attrlen, data); \
599 goto tlv_put_failure; \
602 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
604 ret = tlv_put_u##bits(sctx, attrtype, value); \
606 goto tlv_put_failure; \
609 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
610 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
611 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
612 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
613 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
615 ret = tlv_put_string(sctx, attrtype, str, len); \
617 goto tlv_put_failure; \
619 #define TLV_PUT_PATH(sctx, attrtype, p) \
621 ret = tlv_put_string(sctx, attrtype, p->start, \
622 p->end - p->start); \
624 goto tlv_put_failure; \
626 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
628 ret = tlv_put_uuid(sctx, attrtype, uuid); \
630 goto tlv_put_failure; \
632 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
634 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
636 goto tlv_put_failure; \
639 static int send_header(struct send_ctx
*sctx
)
641 struct btrfs_stream_header hdr
;
643 strcpy(hdr
.magic
, BTRFS_SEND_STREAM_MAGIC
);
644 hdr
.version
= cpu_to_le32(BTRFS_SEND_STREAM_VERSION
);
646 return write_buf(sctx
->send_filp
, &hdr
, sizeof(hdr
),
651 * For each command/item we want to send to userspace, we call this function.
653 static int begin_cmd(struct send_ctx
*sctx
, int cmd
)
655 struct btrfs_cmd_header
*hdr
;
657 if (WARN_ON(!sctx
->send_buf
))
660 BUG_ON(sctx
->send_size
);
662 sctx
->send_size
+= sizeof(*hdr
);
663 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
664 hdr
->cmd
= cpu_to_le16(cmd
);
669 static int send_cmd(struct send_ctx
*sctx
)
672 struct btrfs_cmd_header
*hdr
;
675 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
676 hdr
->len
= cpu_to_le32(sctx
->send_size
- sizeof(*hdr
));
679 crc
= btrfs_crc32c(0, (unsigned char *)sctx
->send_buf
, sctx
->send_size
);
680 hdr
->crc
= cpu_to_le32(crc
);
682 ret
= write_buf(sctx
->send_filp
, sctx
->send_buf
, sctx
->send_size
,
685 sctx
->total_send_size
+= sctx
->send_size
;
686 sctx
->cmd_send_size
[le16_to_cpu(hdr
->cmd
)] += sctx
->send_size
;
693 * Sends a move instruction to user space
695 static int send_rename(struct send_ctx
*sctx
,
696 struct fs_path
*from
, struct fs_path
*to
)
700 verbose_printk("btrfs: send_rename %s -> %s\n", from
->start
, to
->start
);
702 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RENAME
);
706 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, from
);
707 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_TO
, to
);
709 ret
= send_cmd(sctx
);
717 * Sends a link instruction to user space
719 static int send_link(struct send_ctx
*sctx
,
720 struct fs_path
*path
, struct fs_path
*lnk
)
724 verbose_printk("btrfs: send_link %s -> %s\n", path
->start
, lnk
->start
);
726 ret
= begin_cmd(sctx
, BTRFS_SEND_C_LINK
);
730 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
731 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, lnk
);
733 ret
= send_cmd(sctx
);
741 * Sends an unlink instruction to user space
743 static int send_unlink(struct send_ctx
*sctx
, struct fs_path
*path
)
747 verbose_printk("btrfs: send_unlink %s\n", path
->start
);
749 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UNLINK
);
753 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
755 ret
= send_cmd(sctx
);
763 * Sends a rmdir instruction to user space
765 static int send_rmdir(struct send_ctx
*sctx
, struct fs_path
*path
)
769 verbose_printk("btrfs: send_rmdir %s\n", path
->start
);
771 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RMDIR
);
775 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
777 ret
= send_cmd(sctx
);
785 * Helper function to retrieve some fields from an inode item.
787 static int __get_inode_info(struct btrfs_root
*root
, struct btrfs_path
*path
,
788 u64 ino
, u64
*size
, u64
*gen
, u64
*mode
, u64
*uid
,
792 struct btrfs_inode_item
*ii
;
793 struct btrfs_key key
;
796 key
.type
= BTRFS_INODE_ITEM_KEY
;
798 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
805 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
806 struct btrfs_inode_item
);
808 *size
= btrfs_inode_size(path
->nodes
[0], ii
);
810 *gen
= btrfs_inode_generation(path
->nodes
[0], ii
);
812 *mode
= btrfs_inode_mode(path
->nodes
[0], ii
);
814 *uid
= btrfs_inode_uid(path
->nodes
[0], ii
);
816 *gid
= btrfs_inode_gid(path
->nodes
[0], ii
);
818 *rdev
= btrfs_inode_rdev(path
->nodes
[0], ii
);
823 static int get_inode_info(struct btrfs_root
*root
,
824 u64 ino
, u64
*size
, u64
*gen
,
825 u64
*mode
, u64
*uid
, u64
*gid
,
828 struct btrfs_path
*path
;
831 path
= alloc_path_for_send();
834 ret
= __get_inode_info(root
, path
, ino
, size
, gen
, mode
, uid
, gid
,
836 btrfs_free_path(path
);
840 typedef int (*iterate_inode_ref_t
)(int num
, u64 dir
, int index
,
845 * Helper function to iterate the entries in ONE btrfs_inode_ref or
846 * btrfs_inode_extref.
847 * The iterate callback may return a non zero value to stop iteration. This can
848 * be a negative value for error codes or 1 to simply stop it.
850 * path must point to the INODE_REF or INODE_EXTREF when called.
852 static int iterate_inode_ref(struct btrfs_root
*root
, struct btrfs_path
*path
,
853 struct btrfs_key
*found_key
, int resolve
,
854 iterate_inode_ref_t iterate
, void *ctx
)
856 struct extent_buffer
*eb
= path
->nodes
[0];
857 struct btrfs_item
*item
;
858 struct btrfs_inode_ref
*iref
;
859 struct btrfs_inode_extref
*extref
;
860 struct btrfs_path
*tmp_path
;
864 int slot
= path
->slots
[0];
871 unsigned long name_off
;
872 unsigned long elem_size
;
875 p
= fs_path_alloc_reversed();
879 tmp_path
= alloc_path_for_send();
886 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
887 ptr
= (unsigned long)btrfs_item_ptr(eb
, slot
,
888 struct btrfs_inode_ref
);
889 item
= btrfs_item_nr(slot
);
890 total
= btrfs_item_size(eb
, item
);
891 elem_size
= sizeof(*iref
);
893 ptr
= btrfs_item_ptr_offset(eb
, slot
);
894 total
= btrfs_item_size_nr(eb
, slot
);
895 elem_size
= sizeof(*extref
);
898 while (cur
< total
) {
901 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
902 iref
= (struct btrfs_inode_ref
*)(ptr
+ cur
);
903 name_len
= btrfs_inode_ref_name_len(eb
, iref
);
904 name_off
= (unsigned long)(iref
+ 1);
905 index
= btrfs_inode_ref_index(eb
, iref
);
906 dir
= found_key
->offset
;
908 extref
= (struct btrfs_inode_extref
*)(ptr
+ cur
);
909 name_len
= btrfs_inode_extref_name_len(eb
, extref
);
910 name_off
= (unsigned long)&extref
->name
;
911 index
= btrfs_inode_extref_index(eb
, extref
);
912 dir
= btrfs_inode_extref_parent(eb
, extref
);
916 start
= btrfs_ref_to_path(root
, tmp_path
, name_len
,
920 ret
= PTR_ERR(start
);
923 if (start
< p
->buf
) {
924 /* overflow , try again with larger buffer */
925 ret
= fs_path_ensure_buf(p
,
926 p
->buf_len
+ p
->buf
- start
);
929 start
= btrfs_ref_to_path(root
, tmp_path
,
934 ret
= PTR_ERR(start
);
937 BUG_ON(start
< p
->buf
);
941 ret
= fs_path_add_from_extent_buffer(p
, eb
, name_off
,
947 cur
+= elem_size
+ name_len
;
948 ret
= iterate(num
, dir
, index
, p
, ctx
);
955 btrfs_free_path(tmp_path
);
960 typedef int (*iterate_dir_item_t
)(int num
, struct btrfs_key
*di_key
,
961 const char *name
, int name_len
,
962 const char *data
, int data_len
,
966 * Helper function to iterate the entries in ONE btrfs_dir_item.
967 * The iterate callback may return a non zero value to stop iteration. This can
968 * be a negative value for error codes or 1 to simply stop it.
970 * path must point to the dir item when called.
972 static int iterate_dir_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
973 struct btrfs_key
*found_key
,
974 iterate_dir_item_t iterate
, void *ctx
)
977 struct extent_buffer
*eb
;
978 struct btrfs_item
*item
;
979 struct btrfs_dir_item
*di
;
980 struct btrfs_key di_key
;
993 * Start with a small buffer (1 page). If later we end up needing more
994 * space, which can happen for xattrs on a fs with a leaf size greater
995 * then the page size, attempt to increase the buffer. Typically xattr
999 buf
= kmalloc(buf_len
, GFP_NOFS
);
1005 eb
= path
->nodes
[0];
1006 slot
= path
->slots
[0];
1007 item
= btrfs_item_nr(slot
);
1008 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
1011 total
= btrfs_item_size(eb
, item
);
1014 while (cur
< total
) {
1015 name_len
= btrfs_dir_name_len(eb
, di
);
1016 data_len
= btrfs_dir_data_len(eb
, di
);
1017 type
= btrfs_dir_type(eb
, di
);
1018 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
1020 if (type
== BTRFS_FT_XATTR
) {
1021 if (name_len
> XATTR_NAME_MAX
) {
1022 ret
= -ENAMETOOLONG
;
1025 if (name_len
+ data_len
> BTRFS_MAX_XATTR_SIZE(root
)) {
1033 if (name_len
+ data_len
> PATH_MAX
) {
1034 ret
= -ENAMETOOLONG
;
1039 if (name_len
+ data_len
> buf_len
) {
1040 buf_len
= name_len
+ data_len
;
1041 if (is_vmalloc_addr(buf
)) {
1045 char *tmp
= krealloc(buf
, buf_len
,
1046 GFP_NOFS
| __GFP_NOWARN
);
1053 buf
= vmalloc(buf_len
);
1061 read_extent_buffer(eb
, buf
, (unsigned long)(di
+ 1),
1062 name_len
+ data_len
);
1064 len
= sizeof(*di
) + name_len
+ data_len
;
1065 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
1068 ret
= iterate(num
, &di_key
, buf
, name_len
, buf
+ name_len
,
1069 data_len
, type
, ctx
);
1085 static int __copy_first_ref(int num
, u64 dir
, int index
,
1086 struct fs_path
*p
, void *ctx
)
1089 struct fs_path
*pt
= ctx
;
1091 ret
= fs_path_copy(pt
, p
);
1095 /* we want the first only */
1100 * Retrieve the first path of an inode. If an inode has more then one
1101 * ref/hardlink, this is ignored.
1103 static int get_inode_path(struct btrfs_root
*root
,
1104 u64 ino
, struct fs_path
*path
)
1107 struct btrfs_key key
, found_key
;
1108 struct btrfs_path
*p
;
1110 p
= alloc_path_for_send();
1114 fs_path_reset(path
);
1117 key
.type
= BTRFS_INODE_REF_KEY
;
1120 ret
= btrfs_search_slot_for_read(root
, &key
, p
, 1, 0);
1127 btrfs_item_key_to_cpu(p
->nodes
[0], &found_key
, p
->slots
[0]);
1128 if (found_key
.objectid
!= ino
||
1129 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1130 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1135 ret
= iterate_inode_ref(root
, p
, &found_key
, 1,
1136 __copy_first_ref
, path
);
1146 struct backref_ctx
{
1147 struct send_ctx
*sctx
;
1149 struct btrfs_path
*path
;
1150 /* number of total found references */
1154 * used for clones found in send_root. clones found behind cur_objectid
1155 * and cur_offset are not considered as allowed clones.
1160 /* may be truncated in case it's the last extent in a file */
1163 /* data offset in the file extent item */
1166 /* Just to check for bugs in backref resolving */
1170 static int __clone_root_cmp_bsearch(const void *key
, const void *elt
)
1172 u64 root
= (u64
)(uintptr_t)key
;
1173 struct clone_root
*cr
= (struct clone_root
*)elt
;
1175 if (root
< cr
->root
->objectid
)
1177 if (root
> cr
->root
->objectid
)
1182 static int __clone_root_cmp_sort(const void *e1
, const void *e2
)
1184 struct clone_root
*cr1
= (struct clone_root
*)e1
;
1185 struct clone_root
*cr2
= (struct clone_root
*)e2
;
1187 if (cr1
->root
->objectid
< cr2
->root
->objectid
)
1189 if (cr1
->root
->objectid
> cr2
->root
->objectid
)
1195 * Called for every backref that is found for the current extent.
1196 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1198 static int __iterate_backrefs(u64 ino
, u64 offset
, u64 root
, void *ctx_
)
1200 struct backref_ctx
*bctx
= ctx_
;
1201 struct clone_root
*found
;
1205 /* First check if the root is in the list of accepted clone sources */
1206 found
= bsearch((void *)(uintptr_t)root
, bctx
->sctx
->clone_roots
,
1207 bctx
->sctx
->clone_roots_cnt
,
1208 sizeof(struct clone_root
),
1209 __clone_root_cmp_bsearch
);
1213 if (found
->root
== bctx
->sctx
->send_root
&&
1214 ino
== bctx
->cur_objectid
&&
1215 offset
== bctx
->cur_offset
) {
1216 bctx
->found_itself
= 1;
1220 * There are inodes that have extents that lie behind its i_size. Don't
1221 * accept clones from these extents.
1223 ret
= __get_inode_info(found
->root
, bctx
->path
, ino
, &i_size
, NULL
, NULL
,
1225 btrfs_release_path(bctx
->path
);
1229 if (offset
+ bctx
->data_offset
+ bctx
->extent_len
> i_size
)
1233 * Make sure we don't consider clones from send_root that are
1234 * behind the current inode/offset.
1236 if (found
->root
== bctx
->sctx
->send_root
) {
1238 * TODO for the moment we don't accept clones from the inode
1239 * that is currently send. We may change this when
1240 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1243 if (ino
>= bctx
->cur_objectid
)
1246 if (ino
> bctx
->cur_objectid
)
1248 if (offset
+ bctx
->extent_len
> bctx
->cur_offset
)
1254 found
->found_refs
++;
1255 if (ino
< found
->ino
) {
1257 found
->offset
= offset
;
1258 } else if (found
->ino
== ino
) {
1260 * same extent found more then once in the same file.
1262 if (found
->offset
> offset
+ bctx
->extent_len
)
1263 found
->offset
= offset
;
1270 * Given an inode, offset and extent item, it finds a good clone for a clone
1271 * instruction. Returns -ENOENT when none could be found. The function makes
1272 * sure that the returned clone is usable at the point where sending is at the
1273 * moment. This means, that no clones are accepted which lie behind the current
1276 * path must point to the extent item when called.
1278 static int find_extent_clone(struct send_ctx
*sctx
,
1279 struct btrfs_path
*path
,
1280 u64 ino
, u64 data_offset
,
1282 struct clone_root
**found
)
1289 u64 extent_item_pos
;
1291 struct btrfs_file_extent_item
*fi
;
1292 struct extent_buffer
*eb
= path
->nodes
[0];
1293 struct backref_ctx
*backref_ctx
= NULL
;
1294 struct clone_root
*cur_clone_root
;
1295 struct btrfs_key found_key
;
1296 struct btrfs_path
*tmp_path
;
1300 tmp_path
= alloc_path_for_send();
1304 /* We only use this path under the commit sem */
1305 tmp_path
->need_commit_sem
= 0;
1307 backref_ctx
= kmalloc(sizeof(*backref_ctx
), GFP_NOFS
);
1313 backref_ctx
->path
= tmp_path
;
1315 if (data_offset
>= ino_size
) {
1317 * There may be extents that lie behind the file's size.
1318 * I at least had this in combination with snapshotting while
1319 * writing large files.
1325 fi
= btrfs_item_ptr(eb
, path
->slots
[0],
1326 struct btrfs_file_extent_item
);
1327 extent_type
= btrfs_file_extent_type(eb
, fi
);
1328 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1332 compressed
= btrfs_file_extent_compression(eb
, fi
);
1334 num_bytes
= btrfs_file_extent_num_bytes(eb
, fi
);
1335 disk_byte
= btrfs_file_extent_disk_bytenr(eb
, fi
);
1336 if (disk_byte
== 0) {
1340 logical
= disk_byte
+ btrfs_file_extent_offset(eb
, fi
);
1342 down_read(&sctx
->send_root
->fs_info
->commit_root_sem
);
1343 ret
= extent_from_logical(sctx
->send_root
->fs_info
, disk_byte
, tmp_path
,
1344 &found_key
, &flags
);
1345 up_read(&sctx
->send_root
->fs_info
->commit_root_sem
);
1346 btrfs_release_path(tmp_path
);
1350 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1356 * Setup the clone roots.
1358 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1359 cur_clone_root
= sctx
->clone_roots
+ i
;
1360 cur_clone_root
->ino
= (u64
)-1;
1361 cur_clone_root
->offset
= 0;
1362 cur_clone_root
->found_refs
= 0;
1365 backref_ctx
->sctx
= sctx
;
1366 backref_ctx
->found
= 0;
1367 backref_ctx
->cur_objectid
= ino
;
1368 backref_ctx
->cur_offset
= data_offset
;
1369 backref_ctx
->found_itself
= 0;
1370 backref_ctx
->extent_len
= num_bytes
;
1372 * For non-compressed extents iterate_extent_inodes() gives us extent
1373 * offsets that already take into account the data offset, but not for
1374 * compressed extents, since the offset is logical and not relative to
1375 * the physical extent locations. We must take this into account to
1376 * avoid sending clone offsets that go beyond the source file's size,
1377 * which would result in the clone ioctl failing with -EINVAL on the
1380 if (compressed
== BTRFS_COMPRESS_NONE
)
1381 backref_ctx
->data_offset
= 0;
1383 backref_ctx
->data_offset
= btrfs_file_extent_offset(eb
, fi
);
1386 * The last extent of a file may be too large due to page alignment.
1387 * We need to adjust extent_len in this case so that the checks in
1388 * __iterate_backrefs work.
1390 if (data_offset
+ num_bytes
>= ino_size
)
1391 backref_ctx
->extent_len
= ino_size
- data_offset
;
1394 * Now collect all backrefs.
1396 if (compressed
== BTRFS_COMPRESS_NONE
)
1397 extent_item_pos
= logical
- found_key
.objectid
;
1399 extent_item_pos
= 0;
1400 ret
= iterate_extent_inodes(sctx
->send_root
->fs_info
,
1401 found_key
.objectid
, extent_item_pos
, 1,
1402 __iterate_backrefs
, backref_ctx
);
1407 if (!backref_ctx
->found_itself
) {
1408 /* found a bug in backref code? */
1410 btrfs_err(sctx
->send_root
->fs_info
, "did not find backref in "
1411 "send_root. inode=%llu, offset=%llu, "
1412 "disk_byte=%llu found extent=%llu",
1413 ino
, data_offset
, disk_byte
, found_key
.objectid
);
1417 verbose_printk(KERN_DEBUG
"btrfs: find_extent_clone: data_offset=%llu, "
1419 "num_bytes=%llu, logical=%llu\n",
1420 data_offset
, ino
, num_bytes
, logical
);
1422 if (!backref_ctx
->found
)
1423 verbose_printk("btrfs: no clones found\n");
1425 cur_clone_root
= NULL
;
1426 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1427 if (sctx
->clone_roots
[i
].found_refs
) {
1428 if (!cur_clone_root
)
1429 cur_clone_root
= sctx
->clone_roots
+ i
;
1430 else if (sctx
->clone_roots
[i
].root
== sctx
->send_root
)
1431 /* prefer clones from send_root over others */
1432 cur_clone_root
= sctx
->clone_roots
+ i
;
1437 if (cur_clone_root
) {
1438 *found
= cur_clone_root
;
1445 btrfs_free_path(tmp_path
);
1450 static int read_symlink(struct btrfs_root
*root
,
1452 struct fs_path
*dest
)
1455 struct btrfs_path
*path
;
1456 struct btrfs_key key
;
1457 struct btrfs_file_extent_item
*ei
;
1463 path
= alloc_path_for_send();
1468 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1470 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1475 * An empty symlink inode. Can happen in rare error paths when
1476 * creating a symlink (transaction committed before the inode
1477 * eviction handler removed the symlink inode items and a crash
1478 * happened in between or the subvol was snapshoted in between).
1479 * Print an informative message to dmesg/syslog so that the user
1480 * can delete the symlink.
1482 btrfs_err(root
->fs_info
,
1483 "Found empty symlink inode %llu at root %llu",
1484 ino
, root
->root_key
.objectid
);
1489 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1490 struct btrfs_file_extent_item
);
1491 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
1492 compression
= btrfs_file_extent_compression(path
->nodes
[0], ei
);
1493 BUG_ON(type
!= BTRFS_FILE_EXTENT_INLINE
);
1494 BUG_ON(compression
);
1496 off
= btrfs_file_extent_inline_start(ei
);
1497 len
= btrfs_file_extent_inline_len(path
->nodes
[0], path
->slots
[0], ei
);
1499 ret
= fs_path_add_from_extent_buffer(dest
, path
->nodes
[0], off
, len
);
1502 btrfs_free_path(path
);
1507 * Helper function to generate a file name that is unique in the root of
1508 * send_root and parent_root. This is used to generate names for orphan inodes.
1510 static int gen_unique_name(struct send_ctx
*sctx
,
1512 struct fs_path
*dest
)
1515 struct btrfs_path
*path
;
1516 struct btrfs_dir_item
*di
;
1521 path
= alloc_path_for_send();
1526 len
= snprintf(tmp
, sizeof(tmp
), "o%llu-%llu-%llu",
1528 ASSERT(len
< sizeof(tmp
));
1530 di
= btrfs_lookup_dir_item(NULL
, sctx
->send_root
,
1531 path
, BTRFS_FIRST_FREE_OBJECTID
,
1532 tmp
, strlen(tmp
), 0);
1533 btrfs_release_path(path
);
1539 /* not unique, try again */
1544 if (!sctx
->parent_root
) {
1550 di
= btrfs_lookup_dir_item(NULL
, sctx
->parent_root
,
1551 path
, BTRFS_FIRST_FREE_OBJECTID
,
1552 tmp
, strlen(tmp
), 0);
1553 btrfs_release_path(path
);
1559 /* not unique, try again */
1567 ret
= fs_path_add(dest
, tmp
, strlen(tmp
));
1570 btrfs_free_path(path
);
1575 inode_state_no_change
,
1576 inode_state_will_create
,
1577 inode_state_did_create
,
1578 inode_state_will_delete
,
1579 inode_state_did_delete
,
1582 static int get_cur_inode_state(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1590 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &left_gen
, NULL
, NULL
,
1592 if (ret
< 0 && ret
!= -ENOENT
)
1596 if (!sctx
->parent_root
) {
1597 right_ret
= -ENOENT
;
1599 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
, &right_gen
,
1600 NULL
, NULL
, NULL
, NULL
);
1601 if (ret
< 0 && ret
!= -ENOENT
)
1606 if (!left_ret
&& !right_ret
) {
1607 if (left_gen
== gen
&& right_gen
== gen
) {
1608 ret
= inode_state_no_change
;
1609 } else if (left_gen
== gen
) {
1610 if (ino
< sctx
->send_progress
)
1611 ret
= inode_state_did_create
;
1613 ret
= inode_state_will_create
;
1614 } else if (right_gen
== gen
) {
1615 if (ino
< sctx
->send_progress
)
1616 ret
= inode_state_did_delete
;
1618 ret
= inode_state_will_delete
;
1622 } else if (!left_ret
) {
1623 if (left_gen
== gen
) {
1624 if (ino
< sctx
->send_progress
)
1625 ret
= inode_state_did_create
;
1627 ret
= inode_state_will_create
;
1631 } else if (!right_ret
) {
1632 if (right_gen
== gen
) {
1633 if (ino
< sctx
->send_progress
)
1634 ret
= inode_state_did_delete
;
1636 ret
= inode_state_will_delete
;
1648 static int is_inode_existent(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1652 if (ino
== BTRFS_FIRST_FREE_OBJECTID
)
1655 ret
= get_cur_inode_state(sctx
, ino
, gen
);
1659 if (ret
== inode_state_no_change
||
1660 ret
== inode_state_did_create
||
1661 ret
== inode_state_will_delete
)
1671 * Helper function to lookup a dir item in a dir.
1673 static int lookup_dir_item_inode(struct btrfs_root
*root
,
1674 u64 dir
, const char *name
, int name_len
,
1679 struct btrfs_dir_item
*di
;
1680 struct btrfs_key key
;
1681 struct btrfs_path
*path
;
1683 path
= alloc_path_for_send();
1687 di
= btrfs_lookup_dir_item(NULL
, root
, path
,
1688 dir
, name
, name_len
, 0);
1697 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1698 if (key
.type
== BTRFS_ROOT_ITEM_KEY
) {
1702 *found_inode
= key
.objectid
;
1703 *found_type
= btrfs_dir_type(path
->nodes
[0], di
);
1706 btrfs_free_path(path
);
1711 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1712 * generation of the parent dir and the name of the dir entry.
1714 static int get_first_ref(struct btrfs_root
*root
, u64 ino
,
1715 u64
*dir
, u64
*dir_gen
, struct fs_path
*name
)
1718 struct btrfs_key key
;
1719 struct btrfs_key found_key
;
1720 struct btrfs_path
*path
;
1724 path
= alloc_path_for_send();
1729 key
.type
= BTRFS_INODE_REF_KEY
;
1732 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
1736 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1738 if (ret
|| found_key
.objectid
!= ino
||
1739 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1740 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1745 if (found_key
.type
== BTRFS_INODE_REF_KEY
) {
1746 struct btrfs_inode_ref
*iref
;
1747 iref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1748 struct btrfs_inode_ref
);
1749 len
= btrfs_inode_ref_name_len(path
->nodes
[0], iref
);
1750 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1751 (unsigned long)(iref
+ 1),
1753 parent_dir
= found_key
.offset
;
1755 struct btrfs_inode_extref
*extref
;
1756 extref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1757 struct btrfs_inode_extref
);
1758 len
= btrfs_inode_extref_name_len(path
->nodes
[0], extref
);
1759 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1760 (unsigned long)&extref
->name
, len
);
1761 parent_dir
= btrfs_inode_extref_parent(path
->nodes
[0], extref
);
1765 btrfs_release_path(path
);
1768 ret
= get_inode_info(root
, parent_dir
, NULL
, dir_gen
, NULL
,
1777 btrfs_free_path(path
);
1781 static int is_first_ref(struct btrfs_root
*root
,
1783 const char *name
, int name_len
)
1786 struct fs_path
*tmp_name
;
1789 tmp_name
= fs_path_alloc();
1793 ret
= get_first_ref(root
, ino
, &tmp_dir
, NULL
, tmp_name
);
1797 if (dir
!= tmp_dir
|| name_len
!= fs_path_len(tmp_name
)) {
1802 ret
= !memcmp(tmp_name
->start
, name
, name_len
);
1805 fs_path_free(tmp_name
);
1810 * Used by process_recorded_refs to determine if a new ref would overwrite an
1811 * already existing ref. In case it detects an overwrite, it returns the
1812 * inode/gen in who_ino/who_gen.
1813 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1814 * to make sure later references to the overwritten inode are possible.
1815 * Orphanizing is however only required for the first ref of an inode.
1816 * process_recorded_refs does an additional is_first_ref check to see if
1817 * orphanizing is really required.
1819 static int will_overwrite_ref(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
1820 const char *name
, int name_len
,
1821 u64
*who_ino
, u64
*who_gen
)
1825 u64 other_inode
= 0;
1828 if (!sctx
->parent_root
)
1831 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1836 * If we have a parent root we need to verify that the parent dir was
1837 * not delted and then re-created, if it was then we have no overwrite
1838 * and we can just unlink this entry.
1840 if (sctx
->parent_root
&& dir
!= BTRFS_FIRST_FREE_OBJECTID
) {
1841 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &gen
, NULL
,
1843 if (ret
< 0 && ret
!= -ENOENT
)
1853 ret
= lookup_dir_item_inode(sctx
->parent_root
, dir
, name
, name_len
,
1854 &other_inode
, &other_type
);
1855 if (ret
< 0 && ret
!= -ENOENT
)
1863 * Check if the overwritten ref was already processed. If yes, the ref
1864 * was already unlinked/moved, so we can safely assume that we will not
1865 * overwrite anything at this point in time.
1867 if (other_inode
> sctx
->send_progress
) {
1868 ret
= get_inode_info(sctx
->parent_root
, other_inode
, NULL
,
1869 who_gen
, NULL
, NULL
, NULL
, NULL
);
1874 *who_ino
= other_inode
;
1884 * Checks if the ref was overwritten by an already processed inode. This is
1885 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1886 * thus the orphan name needs be used.
1887 * process_recorded_refs also uses it to avoid unlinking of refs that were
1890 static int did_overwrite_ref(struct send_ctx
*sctx
,
1891 u64 dir
, u64 dir_gen
,
1892 u64 ino
, u64 ino_gen
,
1893 const char *name
, int name_len
)
1900 if (!sctx
->parent_root
)
1903 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1907 /* check if the ref was overwritten by another ref */
1908 ret
= lookup_dir_item_inode(sctx
->send_root
, dir
, name
, name_len
,
1909 &ow_inode
, &other_type
);
1910 if (ret
< 0 && ret
!= -ENOENT
)
1913 /* was never and will never be overwritten */
1918 ret
= get_inode_info(sctx
->send_root
, ow_inode
, NULL
, &gen
, NULL
, NULL
,
1923 if (ow_inode
== ino
&& gen
== ino_gen
) {
1929 * We know that it is or will be overwritten. Check this now.
1930 * The current inode being processed might have been the one that caused
1931 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1932 * the current inode being processed.
1934 if ((ow_inode
< sctx
->send_progress
) ||
1935 (ino
!= sctx
->cur_ino
&& ow_inode
== sctx
->cur_ino
&&
1936 gen
== sctx
->cur_inode_gen
))
1946 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1947 * that got overwritten. This is used by process_recorded_refs to determine
1948 * if it has to use the path as returned by get_cur_path or the orphan name.
1950 static int did_overwrite_first_ref(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1953 struct fs_path
*name
= NULL
;
1957 if (!sctx
->parent_root
)
1960 name
= fs_path_alloc();
1964 ret
= get_first_ref(sctx
->parent_root
, ino
, &dir
, &dir_gen
, name
);
1968 ret
= did_overwrite_ref(sctx
, dir
, dir_gen
, ino
, gen
,
1969 name
->start
, fs_path_len(name
));
1977 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1978 * so we need to do some special handling in case we have clashes. This function
1979 * takes care of this with the help of name_cache_entry::radix_list.
1980 * In case of error, nce is kfreed.
1982 static int name_cache_insert(struct send_ctx
*sctx
,
1983 struct name_cache_entry
*nce
)
1986 struct list_head
*nce_head
;
1988 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1989 (unsigned long)nce
->ino
);
1991 nce_head
= kmalloc(sizeof(*nce_head
), GFP_NOFS
);
1996 INIT_LIST_HEAD(nce_head
);
1998 ret
= radix_tree_insert(&sctx
->name_cache
, nce
->ino
, nce_head
);
2005 list_add_tail(&nce
->radix_list
, nce_head
);
2006 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
2007 sctx
->name_cache_size
++;
2012 static void name_cache_delete(struct send_ctx
*sctx
,
2013 struct name_cache_entry
*nce
)
2015 struct list_head
*nce_head
;
2017 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
2018 (unsigned long)nce
->ino
);
2020 btrfs_err(sctx
->send_root
->fs_info
,
2021 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2022 nce
->ino
, sctx
->name_cache_size
);
2025 list_del(&nce
->radix_list
);
2026 list_del(&nce
->list
);
2027 sctx
->name_cache_size
--;
2030 * We may not get to the final release of nce_head if the lookup fails
2032 if (nce_head
&& list_empty(nce_head
)) {
2033 radix_tree_delete(&sctx
->name_cache
, (unsigned long)nce
->ino
);
2038 static struct name_cache_entry
*name_cache_search(struct send_ctx
*sctx
,
2041 struct list_head
*nce_head
;
2042 struct name_cache_entry
*cur
;
2044 nce_head
= radix_tree_lookup(&sctx
->name_cache
, (unsigned long)ino
);
2048 list_for_each_entry(cur
, nce_head
, radix_list
) {
2049 if (cur
->ino
== ino
&& cur
->gen
== gen
)
2056 * Removes the entry from the list and adds it back to the end. This marks the
2057 * entry as recently used so that name_cache_clean_unused does not remove it.
2059 static void name_cache_used(struct send_ctx
*sctx
, struct name_cache_entry
*nce
)
2061 list_del(&nce
->list
);
2062 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
2066 * Remove some entries from the beginning of name_cache_list.
2068 static void name_cache_clean_unused(struct send_ctx
*sctx
)
2070 struct name_cache_entry
*nce
;
2072 if (sctx
->name_cache_size
< SEND_CTX_NAME_CACHE_CLEAN_SIZE
)
2075 while (sctx
->name_cache_size
> SEND_CTX_MAX_NAME_CACHE_SIZE
) {
2076 nce
= list_entry(sctx
->name_cache_list
.next
,
2077 struct name_cache_entry
, list
);
2078 name_cache_delete(sctx
, nce
);
2083 static void name_cache_free(struct send_ctx
*sctx
)
2085 struct name_cache_entry
*nce
;
2087 while (!list_empty(&sctx
->name_cache_list
)) {
2088 nce
= list_entry(sctx
->name_cache_list
.next
,
2089 struct name_cache_entry
, list
);
2090 name_cache_delete(sctx
, nce
);
2096 * Used by get_cur_path for each ref up to the root.
2097 * Returns 0 if it succeeded.
2098 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2099 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2100 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2101 * Returns <0 in case of error.
2103 static int __get_cur_name_and_parent(struct send_ctx
*sctx
,
2107 struct fs_path
*dest
)
2111 struct name_cache_entry
*nce
= NULL
;
2114 * First check if we already did a call to this function with the same
2115 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2116 * return the cached result.
2118 nce
= name_cache_search(sctx
, ino
, gen
);
2120 if (ino
< sctx
->send_progress
&& nce
->need_later_update
) {
2121 name_cache_delete(sctx
, nce
);
2125 name_cache_used(sctx
, nce
);
2126 *parent_ino
= nce
->parent_ino
;
2127 *parent_gen
= nce
->parent_gen
;
2128 ret
= fs_path_add(dest
, nce
->name
, nce
->name_len
);
2137 * If the inode is not existent yet, add the orphan name and return 1.
2138 * This should only happen for the parent dir that we determine in
2141 ret
= is_inode_existent(sctx
, ino
, gen
);
2146 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2154 * Depending on whether the inode was already processed or not, use
2155 * send_root or parent_root for ref lookup.
2157 if (ino
< sctx
->send_progress
)
2158 ret
= get_first_ref(sctx
->send_root
, ino
,
2159 parent_ino
, parent_gen
, dest
);
2161 ret
= get_first_ref(sctx
->parent_root
, ino
,
2162 parent_ino
, parent_gen
, dest
);
2167 * Check if the ref was overwritten by an inode's ref that was processed
2168 * earlier. If yes, treat as orphan and return 1.
2170 ret
= did_overwrite_ref(sctx
, *parent_ino
, *parent_gen
, ino
, gen
,
2171 dest
->start
, dest
->end
- dest
->start
);
2175 fs_path_reset(dest
);
2176 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2184 * Store the result of the lookup in the name cache.
2186 nce
= kmalloc(sizeof(*nce
) + fs_path_len(dest
) + 1, GFP_NOFS
);
2194 nce
->parent_ino
= *parent_ino
;
2195 nce
->parent_gen
= *parent_gen
;
2196 nce
->name_len
= fs_path_len(dest
);
2198 strcpy(nce
->name
, dest
->start
);
2200 if (ino
< sctx
->send_progress
)
2201 nce
->need_later_update
= 0;
2203 nce
->need_later_update
= 1;
2205 nce_ret
= name_cache_insert(sctx
, nce
);
2208 name_cache_clean_unused(sctx
);
2215 * Magic happens here. This function returns the first ref to an inode as it
2216 * would look like while receiving the stream at this point in time.
2217 * We walk the path up to the root. For every inode in between, we check if it
2218 * was already processed/sent. If yes, we continue with the parent as found
2219 * in send_root. If not, we continue with the parent as found in parent_root.
2220 * If we encounter an inode that was deleted at this point in time, we use the
2221 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2222 * that were not created yet and overwritten inodes/refs.
2224 * When do we have have orphan inodes:
2225 * 1. When an inode is freshly created and thus no valid refs are available yet
2226 * 2. When a directory lost all it's refs (deleted) but still has dir items
2227 * inside which were not processed yet (pending for move/delete). If anyone
2228 * tried to get the path to the dir items, it would get a path inside that
2230 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2231 * of an unprocessed inode. If in that case the first ref would be
2232 * overwritten, the overwritten inode gets "orphanized". Later when we
2233 * process this overwritten inode, it is restored at a new place by moving
2236 * sctx->send_progress tells this function at which point in time receiving
2239 static int get_cur_path(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2240 struct fs_path
*dest
)
2243 struct fs_path
*name
= NULL
;
2244 u64 parent_inode
= 0;
2248 name
= fs_path_alloc();
2255 fs_path_reset(dest
);
2257 while (!stop
&& ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
2258 struct waiting_dir_move
*wdm
;
2260 fs_path_reset(name
);
2262 if (is_waiting_for_rm(sctx
, ino
)) {
2263 ret
= gen_unique_name(sctx
, ino
, gen
, name
);
2266 ret
= fs_path_add_path(dest
, name
);
2270 wdm
= get_waiting_dir_move(sctx
, ino
);
2271 if (wdm
&& wdm
->orphanized
) {
2272 ret
= gen_unique_name(sctx
, ino
, gen
, name
);
2275 ret
= get_first_ref(sctx
->parent_root
, ino
,
2276 &parent_inode
, &parent_gen
, name
);
2278 ret
= __get_cur_name_and_parent(sctx
, ino
, gen
,
2288 ret
= fs_path_add_path(dest
, name
);
2299 fs_path_unreverse(dest
);
2304 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2306 static int send_subvol_begin(struct send_ctx
*sctx
)
2309 struct btrfs_root
*send_root
= sctx
->send_root
;
2310 struct btrfs_root
*parent_root
= sctx
->parent_root
;
2311 struct btrfs_path
*path
;
2312 struct btrfs_key key
;
2313 struct btrfs_root_ref
*ref
;
2314 struct extent_buffer
*leaf
;
2318 path
= btrfs_alloc_path();
2322 name
= kmalloc(BTRFS_PATH_NAME_MAX
, GFP_NOFS
);
2324 btrfs_free_path(path
);
2328 key
.objectid
= send_root
->objectid
;
2329 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2332 ret
= btrfs_search_slot_for_read(send_root
->fs_info
->tree_root
,
2341 leaf
= path
->nodes
[0];
2342 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2343 if (key
.type
!= BTRFS_ROOT_BACKREF_KEY
||
2344 key
.objectid
!= send_root
->objectid
) {
2348 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
2349 namelen
= btrfs_root_ref_name_len(leaf
, ref
);
2350 read_extent_buffer(leaf
, name
, (unsigned long)(ref
+ 1), namelen
);
2351 btrfs_release_path(path
);
2354 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SNAPSHOT
);
2358 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SUBVOL
);
2363 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_PATH
, name
, namelen
);
2365 if (!btrfs_is_empty_uuid(sctx
->send_root
->root_item
.received_uuid
))
2366 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_UUID
,
2367 sctx
->send_root
->root_item
.received_uuid
);
2369 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_UUID
,
2370 sctx
->send_root
->root_item
.uuid
);
2372 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CTRANSID
,
2373 le64_to_cpu(sctx
->send_root
->root_item
.ctransid
));
2375 if (!btrfs_is_empty_uuid(parent_root
->root_item
.received_uuid
))
2376 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
2377 parent_root
->root_item
.received_uuid
);
2379 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
2380 parent_root
->root_item
.uuid
);
2381 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
2382 le64_to_cpu(sctx
->parent_root
->root_item
.ctransid
));
2385 ret
= send_cmd(sctx
);
2389 btrfs_free_path(path
);
2394 static int send_truncate(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 size
)
2399 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino
, size
);
2401 p
= fs_path_alloc();
2405 ret
= begin_cmd(sctx
, BTRFS_SEND_C_TRUNCATE
);
2409 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2412 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2413 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, size
);
2415 ret
= send_cmd(sctx
);
2423 static int send_chmod(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 mode
)
2428 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino
, mode
);
2430 p
= fs_path_alloc();
2434 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHMOD
);
2438 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2441 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2442 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
& 07777);
2444 ret
= send_cmd(sctx
);
2452 static int send_chown(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 uid
, u64 gid
)
2457 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino
, uid
, gid
);
2459 p
= fs_path_alloc();
2463 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHOWN
);
2467 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2470 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2471 TLV_PUT_U64(sctx
, BTRFS_SEND_A_UID
, uid
);
2472 TLV_PUT_U64(sctx
, BTRFS_SEND_A_GID
, gid
);
2474 ret
= send_cmd(sctx
);
2482 static int send_utimes(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
2485 struct fs_path
*p
= NULL
;
2486 struct btrfs_inode_item
*ii
;
2487 struct btrfs_path
*path
= NULL
;
2488 struct extent_buffer
*eb
;
2489 struct btrfs_key key
;
2492 verbose_printk("btrfs: send_utimes %llu\n", ino
);
2494 p
= fs_path_alloc();
2498 path
= alloc_path_for_send();
2505 key
.type
= BTRFS_INODE_ITEM_KEY
;
2507 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2511 eb
= path
->nodes
[0];
2512 slot
= path
->slots
[0];
2513 ii
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
2515 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UTIMES
);
2519 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2522 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2523 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_ATIME
, eb
, &ii
->atime
);
2524 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_MTIME
, eb
, &ii
->mtime
);
2525 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_CTIME
, eb
, &ii
->ctime
);
2526 /* TODO Add otime support when the otime patches get into upstream */
2528 ret
= send_cmd(sctx
);
2533 btrfs_free_path(path
);
2538 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2539 * a valid path yet because we did not process the refs yet. So, the inode
2540 * is created as orphan.
2542 static int send_create_inode(struct send_ctx
*sctx
, u64 ino
)
2551 verbose_printk("btrfs: send_create_inode %llu\n", ino
);
2553 p
= fs_path_alloc();
2557 if (ino
!= sctx
->cur_ino
) {
2558 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &gen
, &mode
,
2563 gen
= sctx
->cur_inode_gen
;
2564 mode
= sctx
->cur_inode_mode
;
2565 rdev
= sctx
->cur_inode_rdev
;
2568 if (S_ISREG(mode
)) {
2569 cmd
= BTRFS_SEND_C_MKFILE
;
2570 } else if (S_ISDIR(mode
)) {
2571 cmd
= BTRFS_SEND_C_MKDIR
;
2572 } else if (S_ISLNK(mode
)) {
2573 cmd
= BTRFS_SEND_C_SYMLINK
;
2574 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
2575 cmd
= BTRFS_SEND_C_MKNOD
;
2576 } else if (S_ISFIFO(mode
)) {
2577 cmd
= BTRFS_SEND_C_MKFIFO
;
2578 } else if (S_ISSOCK(mode
)) {
2579 cmd
= BTRFS_SEND_C_MKSOCK
;
2581 btrfs_warn(sctx
->send_root
->fs_info
, "unexpected inode type %o",
2582 (int)(mode
& S_IFMT
));
2587 ret
= begin_cmd(sctx
, cmd
);
2591 ret
= gen_unique_name(sctx
, ino
, gen
, p
);
2595 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2596 TLV_PUT_U64(sctx
, BTRFS_SEND_A_INO
, ino
);
2598 if (S_ISLNK(mode
)) {
2600 ret
= read_symlink(sctx
->send_root
, ino
, p
);
2603 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, p
);
2604 } else if (S_ISCHR(mode
) || S_ISBLK(mode
) ||
2605 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
2606 TLV_PUT_U64(sctx
, BTRFS_SEND_A_RDEV
, new_encode_dev(rdev
));
2607 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
);
2610 ret
= send_cmd(sctx
);
2622 * We need some special handling for inodes that get processed before the parent
2623 * directory got created. See process_recorded_refs for details.
2624 * This function does the check if we already created the dir out of order.
2626 static int did_create_dir(struct send_ctx
*sctx
, u64 dir
)
2629 struct btrfs_path
*path
= NULL
;
2630 struct btrfs_key key
;
2631 struct btrfs_key found_key
;
2632 struct btrfs_key di_key
;
2633 struct extent_buffer
*eb
;
2634 struct btrfs_dir_item
*di
;
2637 path
= alloc_path_for_send();
2644 key
.type
= BTRFS_DIR_INDEX_KEY
;
2646 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2651 eb
= path
->nodes
[0];
2652 slot
= path
->slots
[0];
2653 if (slot
>= btrfs_header_nritems(eb
)) {
2654 ret
= btrfs_next_leaf(sctx
->send_root
, path
);
2657 } else if (ret
> 0) {
2664 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
2665 if (found_key
.objectid
!= key
.objectid
||
2666 found_key
.type
!= key
.type
) {
2671 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
2672 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
2674 if (di_key
.type
!= BTRFS_ROOT_ITEM_KEY
&&
2675 di_key
.objectid
< sctx
->send_progress
) {
2684 btrfs_free_path(path
);
2689 * Only creates the inode if it is:
2690 * 1. Not a directory
2691 * 2. Or a directory which was not created already due to out of order
2692 * directories. See did_create_dir and process_recorded_refs for details.
2694 static int send_create_inode_if_needed(struct send_ctx
*sctx
)
2698 if (S_ISDIR(sctx
->cur_inode_mode
)) {
2699 ret
= did_create_dir(sctx
, sctx
->cur_ino
);
2708 ret
= send_create_inode(sctx
, sctx
->cur_ino
);
2716 struct recorded_ref
{
2717 struct list_head list
;
2720 struct fs_path
*full_path
;
2728 * We need to process new refs before deleted refs, but compare_tree gives us
2729 * everything mixed. So we first record all refs and later process them.
2730 * This function is a helper to record one ref.
2732 static int __record_ref(struct list_head
*head
, u64 dir
,
2733 u64 dir_gen
, struct fs_path
*path
)
2735 struct recorded_ref
*ref
;
2737 ref
= kmalloc(sizeof(*ref
), GFP_NOFS
);
2742 ref
->dir_gen
= dir_gen
;
2743 ref
->full_path
= path
;
2745 ref
->name
= (char *)kbasename(ref
->full_path
->start
);
2746 ref
->name_len
= ref
->full_path
->end
- ref
->name
;
2747 ref
->dir_path
= ref
->full_path
->start
;
2748 if (ref
->name
== ref
->full_path
->start
)
2749 ref
->dir_path_len
= 0;
2751 ref
->dir_path_len
= ref
->full_path
->end
-
2752 ref
->full_path
->start
- 1 - ref
->name_len
;
2754 list_add_tail(&ref
->list
, head
);
2758 static int dup_ref(struct recorded_ref
*ref
, struct list_head
*list
)
2760 struct recorded_ref
*new;
2762 new = kmalloc(sizeof(*ref
), GFP_NOFS
);
2766 new->dir
= ref
->dir
;
2767 new->dir_gen
= ref
->dir_gen
;
2768 new->full_path
= NULL
;
2769 INIT_LIST_HEAD(&new->list
);
2770 list_add_tail(&new->list
, list
);
2774 static void __free_recorded_refs(struct list_head
*head
)
2776 struct recorded_ref
*cur
;
2778 while (!list_empty(head
)) {
2779 cur
= list_entry(head
->next
, struct recorded_ref
, list
);
2780 fs_path_free(cur
->full_path
);
2781 list_del(&cur
->list
);
2786 static void free_recorded_refs(struct send_ctx
*sctx
)
2788 __free_recorded_refs(&sctx
->new_refs
);
2789 __free_recorded_refs(&sctx
->deleted_refs
);
2793 * Renames/moves a file/dir to its orphan name. Used when the first
2794 * ref of an unprocessed inode gets overwritten and for all non empty
2797 static int orphanize_inode(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2798 struct fs_path
*path
)
2801 struct fs_path
*orphan
;
2803 orphan
= fs_path_alloc();
2807 ret
= gen_unique_name(sctx
, ino
, gen
, orphan
);
2811 ret
= send_rename(sctx
, path
, orphan
);
2814 fs_path_free(orphan
);
2818 static struct orphan_dir_info
*
2819 add_orphan_dir_info(struct send_ctx
*sctx
, u64 dir_ino
)
2821 struct rb_node
**p
= &sctx
->orphan_dirs
.rb_node
;
2822 struct rb_node
*parent
= NULL
;
2823 struct orphan_dir_info
*entry
, *odi
;
2825 odi
= kmalloc(sizeof(*odi
), GFP_NOFS
);
2827 return ERR_PTR(-ENOMEM
);
2833 entry
= rb_entry(parent
, struct orphan_dir_info
, node
);
2834 if (dir_ino
< entry
->ino
) {
2836 } else if (dir_ino
> entry
->ino
) {
2837 p
= &(*p
)->rb_right
;
2844 rb_link_node(&odi
->node
, parent
, p
);
2845 rb_insert_color(&odi
->node
, &sctx
->orphan_dirs
);
2849 static struct orphan_dir_info
*
2850 get_orphan_dir_info(struct send_ctx
*sctx
, u64 dir_ino
)
2852 struct rb_node
*n
= sctx
->orphan_dirs
.rb_node
;
2853 struct orphan_dir_info
*entry
;
2856 entry
= rb_entry(n
, struct orphan_dir_info
, node
);
2857 if (dir_ino
< entry
->ino
)
2859 else if (dir_ino
> entry
->ino
)
2867 static int is_waiting_for_rm(struct send_ctx
*sctx
, u64 dir_ino
)
2869 struct orphan_dir_info
*odi
= get_orphan_dir_info(sctx
, dir_ino
);
2874 static void free_orphan_dir_info(struct send_ctx
*sctx
,
2875 struct orphan_dir_info
*odi
)
2879 rb_erase(&odi
->node
, &sctx
->orphan_dirs
);
2884 * Returns 1 if a directory can be removed at this point in time.
2885 * We check this by iterating all dir items and checking if the inode behind
2886 * the dir item was already processed.
2888 static int can_rmdir(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
2892 struct btrfs_root
*root
= sctx
->parent_root
;
2893 struct btrfs_path
*path
;
2894 struct btrfs_key key
;
2895 struct btrfs_key found_key
;
2896 struct btrfs_key loc
;
2897 struct btrfs_dir_item
*di
;
2900 * Don't try to rmdir the top/root subvolume dir.
2902 if (dir
== BTRFS_FIRST_FREE_OBJECTID
)
2905 path
= alloc_path_for_send();
2910 key
.type
= BTRFS_DIR_INDEX_KEY
;
2912 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2917 struct waiting_dir_move
*dm
;
2919 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
2920 ret
= btrfs_next_leaf(root
, path
);
2927 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2929 if (found_key
.objectid
!= key
.objectid
||
2930 found_key
.type
!= key
.type
)
2933 di
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2934 struct btrfs_dir_item
);
2935 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &loc
);
2937 dm
= get_waiting_dir_move(sctx
, loc
.objectid
);
2939 struct orphan_dir_info
*odi
;
2941 odi
= add_orphan_dir_info(sctx
, dir
);
2947 dm
->rmdir_ino
= dir
;
2952 if (loc
.objectid
> send_progress
) {
2963 btrfs_free_path(path
);
2967 static int is_waiting_for_move(struct send_ctx
*sctx
, u64 ino
)
2969 struct waiting_dir_move
*entry
= get_waiting_dir_move(sctx
, ino
);
2971 return entry
!= NULL
;
2974 static int add_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
, bool orphanized
)
2976 struct rb_node
**p
= &sctx
->waiting_dir_moves
.rb_node
;
2977 struct rb_node
*parent
= NULL
;
2978 struct waiting_dir_move
*entry
, *dm
;
2980 dm
= kmalloc(sizeof(*dm
), GFP_NOFS
);
2985 dm
->orphanized
= orphanized
;
2989 entry
= rb_entry(parent
, struct waiting_dir_move
, node
);
2990 if (ino
< entry
->ino
) {
2992 } else if (ino
> entry
->ino
) {
2993 p
= &(*p
)->rb_right
;
3000 rb_link_node(&dm
->node
, parent
, p
);
3001 rb_insert_color(&dm
->node
, &sctx
->waiting_dir_moves
);
3005 static struct waiting_dir_move
*
3006 get_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
)
3008 struct rb_node
*n
= sctx
->waiting_dir_moves
.rb_node
;
3009 struct waiting_dir_move
*entry
;
3012 entry
= rb_entry(n
, struct waiting_dir_move
, node
);
3013 if (ino
< entry
->ino
)
3015 else if (ino
> entry
->ino
)
3023 static void free_waiting_dir_move(struct send_ctx
*sctx
,
3024 struct waiting_dir_move
*dm
)
3028 rb_erase(&dm
->node
, &sctx
->waiting_dir_moves
);
3032 static int add_pending_dir_move(struct send_ctx
*sctx
,
3036 struct list_head
*new_refs
,
3037 struct list_head
*deleted_refs
,
3038 const bool is_orphan
)
3040 struct rb_node
**p
= &sctx
->pending_dir_moves
.rb_node
;
3041 struct rb_node
*parent
= NULL
;
3042 struct pending_dir_move
*entry
= NULL
, *pm
;
3043 struct recorded_ref
*cur
;
3047 pm
= kmalloc(sizeof(*pm
), GFP_NOFS
);
3050 pm
->parent_ino
= parent_ino
;
3053 pm
->is_orphan
= is_orphan
;
3054 INIT_LIST_HEAD(&pm
->list
);
3055 INIT_LIST_HEAD(&pm
->update_refs
);
3056 RB_CLEAR_NODE(&pm
->node
);
3060 entry
= rb_entry(parent
, struct pending_dir_move
, node
);
3061 if (parent_ino
< entry
->parent_ino
) {
3063 } else if (parent_ino
> entry
->parent_ino
) {
3064 p
= &(*p
)->rb_right
;
3071 list_for_each_entry(cur
, deleted_refs
, list
) {
3072 ret
= dup_ref(cur
, &pm
->update_refs
);
3076 list_for_each_entry(cur
, new_refs
, list
) {
3077 ret
= dup_ref(cur
, &pm
->update_refs
);
3082 ret
= add_waiting_dir_move(sctx
, pm
->ino
, is_orphan
);
3087 list_add_tail(&pm
->list
, &entry
->list
);
3089 rb_link_node(&pm
->node
, parent
, p
);
3090 rb_insert_color(&pm
->node
, &sctx
->pending_dir_moves
);
3095 __free_recorded_refs(&pm
->update_refs
);
3101 static struct pending_dir_move
*get_pending_dir_moves(struct send_ctx
*sctx
,
3104 struct rb_node
*n
= sctx
->pending_dir_moves
.rb_node
;
3105 struct pending_dir_move
*entry
;
3108 entry
= rb_entry(n
, struct pending_dir_move
, node
);
3109 if (parent_ino
< entry
->parent_ino
)
3111 else if (parent_ino
> entry
->parent_ino
)
3119 static int apply_dir_move(struct send_ctx
*sctx
, struct pending_dir_move
*pm
)
3121 struct fs_path
*from_path
= NULL
;
3122 struct fs_path
*to_path
= NULL
;
3123 struct fs_path
*name
= NULL
;
3124 u64 orig_progress
= sctx
->send_progress
;
3125 struct recorded_ref
*cur
;
3126 u64 parent_ino
, parent_gen
;
3127 struct waiting_dir_move
*dm
= NULL
;
3131 name
= fs_path_alloc();
3132 from_path
= fs_path_alloc();
3133 if (!name
|| !from_path
) {
3138 dm
= get_waiting_dir_move(sctx
, pm
->ino
);
3140 rmdir_ino
= dm
->rmdir_ino
;
3141 free_waiting_dir_move(sctx
, dm
);
3143 if (pm
->is_orphan
) {
3144 ret
= gen_unique_name(sctx
, pm
->ino
,
3145 pm
->gen
, from_path
);
3147 ret
= get_first_ref(sctx
->parent_root
, pm
->ino
,
3148 &parent_ino
, &parent_gen
, name
);
3151 ret
= get_cur_path(sctx
, parent_ino
, parent_gen
,
3155 ret
= fs_path_add_path(from_path
, name
);
3160 sctx
->send_progress
= sctx
->cur_ino
+ 1;
3161 fs_path_reset(name
);
3164 ret
= get_cur_path(sctx
, pm
->ino
, pm
->gen
, to_path
);
3168 ret
= send_rename(sctx
, from_path
, to_path
);
3173 struct orphan_dir_info
*odi
;
3175 odi
= get_orphan_dir_info(sctx
, rmdir_ino
);
3177 /* already deleted */
3180 ret
= can_rmdir(sctx
, rmdir_ino
, odi
->gen
, sctx
->cur_ino
+ 1);
3186 name
= fs_path_alloc();
3191 ret
= get_cur_path(sctx
, rmdir_ino
, odi
->gen
, name
);
3194 ret
= send_rmdir(sctx
, name
);
3197 free_orphan_dir_info(sctx
, odi
);
3201 ret
= send_utimes(sctx
, pm
->ino
, pm
->gen
);
3206 * After rename/move, need to update the utimes of both new parent(s)
3207 * and old parent(s).
3209 list_for_each_entry(cur
, &pm
->update_refs
, list
) {
3210 if (cur
->dir
== rmdir_ino
)
3212 ret
= send_utimes(sctx
, cur
->dir
, cur
->dir_gen
);
3219 fs_path_free(from_path
);
3220 fs_path_free(to_path
);
3221 sctx
->send_progress
= orig_progress
;
3226 static void free_pending_move(struct send_ctx
*sctx
, struct pending_dir_move
*m
)
3228 if (!list_empty(&m
->list
))
3230 if (!RB_EMPTY_NODE(&m
->node
))
3231 rb_erase(&m
->node
, &sctx
->pending_dir_moves
);
3232 __free_recorded_refs(&m
->update_refs
);
3236 static void tail_append_pending_moves(struct send_ctx
*sctx
,
3237 struct pending_dir_move
*moves
,
3238 struct list_head
*stack
)
3240 if (list_empty(&moves
->list
)) {
3241 list_add_tail(&moves
->list
, stack
);
3244 list_splice_init(&moves
->list
, &list
);
3245 list_add_tail(&moves
->list
, stack
);
3246 list_splice_tail(&list
, stack
);
3248 if (!RB_EMPTY_NODE(&moves
->node
)) {
3249 rb_erase(&moves
->node
, &sctx
->pending_dir_moves
);
3250 RB_CLEAR_NODE(&moves
->node
);
3254 static int apply_children_dir_moves(struct send_ctx
*sctx
)
3256 struct pending_dir_move
*pm
;
3257 struct list_head stack
;
3258 u64 parent_ino
= sctx
->cur_ino
;
3261 pm
= get_pending_dir_moves(sctx
, parent_ino
);
3265 INIT_LIST_HEAD(&stack
);
3266 tail_append_pending_moves(sctx
, pm
, &stack
);
3268 while (!list_empty(&stack
)) {
3269 pm
= list_first_entry(&stack
, struct pending_dir_move
, list
);
3270 parent_ino
= pm
->ino
;
3271 ret
= apply_dir_move(sctx
, pm
);
3272 free_pending_move(sctx
, pm
);
3275 pm
= get_pending_dir_moves(sctx
, parent_ino
);
3277 tail_append_pending_moves(sctx
, pm
, &stack
);
3282 while (!list_empty(&stack
)) {
3283 pm
= list_first_entry(&stack
, struct pending_dir_move
, list
);
3284 free_pending_move(sctx
, pm
);
3290 * We might need to delay a directory rename even when no ancestor directory
3291 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3292 * renamed. This happens when we rename a directory to the old name (the name
3293 * in the parent root) of some other unrelated directory that got its rename
3294 * delayed due to some ancestor with higher number that got renamed.
3300 * |---- a/ (ino 257)
3301 * | |---- file (ino 260)
3303 * |---- b/ (ino 258)
3304 * |---- c/ (ino 259)
3308 * |---- a/ (ino 258)
3309 * |---- x/ (ino 259)
3310 * |---- y/ (ino 257)
3311 * |----- file (ino 260)
3313 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3314 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3315 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3318 * 1 - rename 259 from 'c' to 'x'
3319 * 2 - rename 257 from 'a' to 'x/y'
3320 * 3 - rename 258 from 'b' to 'a'
3322 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3323 * be done right away and < 0 on error.
3325 static int wait_for_dest_dir_move(struct send_ctx
*sctx
,
3326 struct recorded_ref
*parent_ref
,
3327 const bool is_orphan
)
3329 struct btrfs_path
*path
;
3330 struct btrfs_key key
;
3331 struct btrfs_key di_key
;
3332 struct btrfs_dir_item
*di
;
3337 if (RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
))
3340 path
= alloc_path_for_send();
3344 key
.objectid
= parent_ref
->dir
;
3345 key
.type
= BTRFS_DIR_ITEM_KEY
;
3346 key
.offset
= btrfs_name_hash(parent_ref
->name
, parent_ref
->name_len
);
3348 ret
= btrfs_search_slot(NULL
, sctx
->parent_root
, &key
, path
, 0, 0);
3351 } else if (ret
> 0) {
3356 di
= btrfs_match_dir_item_name(sctx
->parent_root
, path
,
3357 parent_ref
->name
, parent_ref
->name_len
);
3363 * di_key.objectid has the number of the inode that has a dentry in the
3364 * parent directory with the same name that sctx->cur_ino is being
3365 * renamed to. We need to check if that inode is in the send root as
3366 * well and if it is currently marked as an inode with a pending rename,
3367 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3368 * that it happens after that other inode is renamed.
3370 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &di_key
);
3371 if (di_key
.type
!= BTRFS_INODE_ITEM_KEY
) {
3376 ret
= get_inode_info(sctx
->parent_root
, di_key
.objectid
, NULL
,
3377 &left_gen
, NULL
, NULL
, NULL
, NULL
);
3380 ret
= get_inode_info(sctx
->send_root
, di_key
.objectid
, NULL
,
3381 &right_gen
, NULL
, NULL
, NULL
, NULL
);
3388 /* Different inode, no need to delay the rename of sctx->cur_ino */
3389 if (right_gen
!= left_gen
) {
3394 if (is_waiting_for_move(sctx
, di_key
.objectid
)) {
3395 ret
= add_pending_dir_move(sctx
,
3397 sctx
->cur_inode_gen
,
3400 &sctx
->deleted_refs
,
3406 btrfs_free_path(path
);
3411 * Check if ino ino1 is an ancestor of inode ino2 in the given root.
3412 * Return 1 if true, 0 if false and < 0 on error.
3414 static int is_ancestor(struct btrfs_root
*root
,
3418 struct fs_path
*fs_path
)
3422 while (ino
> BTRFS_FIRST_FREE_OBJECTID
) {
3427 fs_path_reset(fs_path
);
3428 ret
= get_first_ref(root
, ino
, &parent
, &parent_gen
, fs_path
);
3430 if (ret
== -ENOENT
&& ino
== ino2
)
3435 return parent_gen
== ino1_gen
? 1 : 0;
3441 static int wait_for_parent_move(struct send_ctx
*sctx
,
3442 struct recorded_ref
*parent_ref
,
3443 const bool is_orphan
)
3446 u64 ino
= parent_ref
->dir
;
3447 u64 parent_ino_before
, parent_ino_after
;
3448 struct fs_path
*path_before
= NULL
;
3449 struct fs_path
*path_after
= NULL
;
3452 path_after
= fs_path_alloc();
3453 path_before
= fs_path_alloc();
3454 if (!path_after
|| !path_before
) {
3460 * Our current directory inode may not yet be renamed/moved because some
3461 * ancestor (immediate or not) has to be renamed/moved first. So find if
3462 * such ancestor exists and make sure our own rename/move happens after
3463 * that ancestor is processed to avoid path build infinite loops (done
3464 * at get_cur_path()).
3466 while (ino
> BTRFS_FIRST_FREE_OBJECTID
) {
3467 if (is_waiting_for_move(sctx
, ino
)) {
3469 * If the current inode is an ancestor of ino in the
3470 * parent root, we need to delay the rename of the
3471 * current inode, otherwise don't delayed the rename
3472 * because we can end up with a circular dependency
3473 * of renames, resulting in some directories never
3474 * getting the respective rename operations issued in
3475 * the send stream or getting into infinite path build
3478 ret
= is_ancestor(sctx
->parent_root
,
3479 sctx
->cur_ino
, sctx
->cur_inode_gen
,
3484 fs_path_reset(path_before
);
3485 fs_path_reset(path_after
);
3487 ret
= get_first_ref(sctx
->send_root
, ino
, &parent_ino_after
,
3491 ret
= get_first_ref(sctx
->parent_root
, ino
, &parent_ino_before
,
3493 if (ret
< 0 && ret
!= -ENOENT
) {
3495 } else if (ret
== -ENOENT
) {
3500 len1
= fs_path_len(path_before
);
3501 len2
= fs_path_len(path_after
);
3502 if (ino
> sctx
->cur_ino
&&
3503 (parent_ino_before
!= parent_ino_after
|| len1
!= len2
||
3504 memcmp(path_before
->start
, path_after
->start
, len1
))) {
3508 ino
= parent_ino_after
;
3512 fs_path_free(path_before
);
3513 fs_path_free(path_after
);
3516 ret
= add_pending_dir_move(sctx
,
3518 sctx
->cur_inode_gen
,
3521 &sctx
->deleted_refs
,
3531 * This does all the move/link/unlink/rmdir magic.
3533 static int process_recorded_refs(struct send_ctx
*sctx
, int *pending_move
)
3536 struct recorded_ref
*cur
;
3537 struct recorded_ref
*cur2
;
3538 struct list_head check_dirs
;
3539 struct fs_path
*valid_path
= NULL
;
3542 int did_overwrite
= 0;
3544 u64 last_dir_ino_rm
= 0;
3545 bool can_rename
= true;
3547 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx
->cur_ino
);
3550 * This should never happen as the root dir always has the same ref
3551 * which is always '..'
3553 BUG_ON(sctx
->cur_ino
<= BTRFS_FIRST_FREE_OBJECTID
);
3554 INIT_LIST_HEAD(&check_dirs
);
3556 valid_path
= fs_path_alloc();
3563 * First, check if the first ref of the current inode was overwritten
3564 * before. If yes, we know that the current inode was already orphanized
3565 * and thus use the orphan name. If not, we can use get_cur_path to
3566 * get the path of the first ref as it would like while receiving at
3567 * this point in time.
3568 * New inodes are always orphan at the beginning, so force to use the
3569 * orphan name in this case.
3570 * The first ref is stored in valid_path and will be updated if it
3571 * gets moved around.
3573 if (!sctx
->cur_inode_new
) {
3574 ret
= did_overwrite_first_ref(sctx
, sctx
->cur_ino
,
3575 sctx
->cur_inode_gen
);
3581 if (sctx
->cur_inode_new
|| did_overwrite
) {
3582 ret
= gen_unique_name(sctx
, sctx
->cur_ino
,
3583 sctx
->cur_inode_gen
, valid_path
);
3588 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
3594 list_for_each_entry(cur
, &sctx
->new_refs
, list
) {
3596 * We may have refs where the parent directory does not exist
3597 * yet. This happens if the parent directories inum is higher
3598 * the the current inum. To handle this case, we create the
3599 * parent directory out of order. But we need to check if this
3600 * did already happen before due to other refs in the same dir.
3602 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
3605 if (ret
== inode_state_will_create
) {
3608 * First check if any of the current inodes refs did
3609 * already create the dir.
3611 list_for_each_entry(cur2
, &sctx
->new_refs
, list
) {
3614 if (cur2
->dir
== cur
->dir
) {
3621 * If that did not happen, check if a previous inode
3622 * did already create the dir.
3625 ret
= did_create_dir(sctx
, cur
->dir
);
3629 ret
= send_create_inode(sctx
, cur
->dir
);
3636 * Check if this new ref would overwrite the first ref of
3637 * another unprocessed inode. If yes, orphanize the
3638 * overwritten inode. If we find an overwritten ref that is
3639 * not the first ref, simply unlink it.
3641 ret
= will_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
3642 cur
->name
, cur
->name_len
,
3643 &ow_inode
, &ow_gen
);
3647 ret
= is_first_ref(sctx
->parent_root
,
3648 ow_inode
, cur
->dir
, cur
->name
,
3653 struct name_cache_entry
*nce
;
3655 ret
= orphanize_inode(sctx
, ow_inode
, ow_gen
,
3660 * Make sure we clear our orphanized inode's
3661 * name from the name cache. This is because the
3662 * inode ow_inode might be an ancestor of some
3663 * other inode that will be orphanized as well
3664 * later and has an inode number greater than
3665 * sctx->send_progress. We need to prevent
3666 * future name lookups from using the old name
3667 * and get instead the orphan name.
3669 nce
= name_cache_search(sctx
, ow_inode
, ow_gen
);
3671 name_cache_delete(sctx
, nce
);
3675 ret
= send_unlink(sctx
, cur
->full_path
);
3681 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->parent_root
) {
3682 ret
= wait_for_dest_dir_move(sctx
, cur
, is_orphan
);
3691 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->parent_root
&&
3693 ret
= wait_for_parent_move(sctx
, cur
, is_orphan
);
3703 * link/move the ref to the new place. If we have an orphan
3704 * inode, move it and update valid_path. If not, link or move
3705 * it depending on the inode mode.
3707 if (is_orphan
&& can_rename
) {
3708 ret
= send_rename(sctx
, valid_path
, cur
->full_path
);
3712 ret
= fs_path_copy(valid_path
, cur
->full_path
);
3715 } else if (can_rename
) {
3716 if (S_ISDIR(sctx
->cur_inode_mode
)) {
3718 * Dirs can't be linked, so move it. For moved
3719 * dirs, we always have one new and one deleted
3720 * ref. The deleted ref is ignored later.
3722 ret
= send_rename(sctx
, valid_path
,
3725 ret
= fs_path_copy(valid_path
,
3730 ret
= send_link(sctx
, cur
->full_path
,
3736 ret
= dup_ref(cur
, &check_dirs
);
3741 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->cur_inode_deleted
) {
3743 * Check if we can already rmdir the directory. If not,
3744 * orphanize it. For every dir item inside that gets deleted
3745 * later, we do this check again and rmdir it then if possible.
3746 * See the use of check_dirs for more details.
3748 ret
= can_rmdir(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
3753 ret
= send_rmdir(sctx
, valid_path
);
3756 } else if (!is_orphan
) {
3757 ret
= orphanize_inode(sctx
, sctx
->cur_ino
,
3758 sctx
->cur_inode_gen
, valid_path
);
3764 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
3765 ret
= dup_ref(cur
, &check_dirs
);
3769 } else if (S_ISDIR(sctx
->cur_inode_mode
) &&
3770 !list_empty(&sctx
->deleted_refs
)) {
3772 * We have a moved dir. Add the old parent to check_dirs
3774 cur
= list_entry(sctx
->deleted_refs
.next
, struct recorded_ref
,
3776 ret
= dup_ref(cur
, &check_dirs
);
3779 } else if (!S_ISDIR(sctx
->cur_inode_mode
)) {
3781 * We have a non dir inode. Go through all deleted refs and
3782 * unlink them if they were not already overwritten by other
3785 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
3786 ret
= did_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
3787 sctx
->cur_ino
, sctx
->cur_inode_gen
,
3788 cur
->name
, cur
->name_len
);
3792 ret
= send_unlink(sctx
, cur
->full_path
);
3796 ret
= dup_ref(cur
, &check_dirs
);
3801 * If the inode is still orphan, unlink the orphan. This may
3802 * happen when a previous inode did overwrite the first ref
3803 * of this inode and no new refs were added for the current
3804 * inode. Unlinking does not mean that the inode is deleted in
3805 * all cases. There may still be links to this inode in other
3809 ret
= send_unlink(sctx
, valid_path
);
3816 * We did collect all parent dirs where cur_inode was once located. We
3817 * now go through all these dirs and check if they are pending for
3818 * deletion and if it's finally possible to perform the rmdir now.
3819 * We also update the inode stats of the parent dirs here.
3821 list_for_each_entry(cur
, &check_dirs
, list
) {
3823 * In case we had refs into dirs that were not processed yet,
3824 * we don't need to do the utime and rmdir logic for these dirs.
3825 * The dir will be processed later.
3827 if (cur
->dir
> sctx
->cur_ino
)
3830 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
3834 if (ret
== inode_state_did_create
||
3835 ret
== inode_state_no_change
) {
3836 /* TODO delayed utimes */
3837 ret
= send_utimes(sctx
, cur
->dir
, cur
->dir_gen
);
3840 } else if (ret
== inode_state_did_delete
&&
3841 cur
->dir
!= last_dir_ino_rm
) {
3842 ret
= can_rmdir(sctx
, cur
->dir
, cur
->dir_gen
,
3847 ret
= get_cur_path(sctx
, cur
->dir
,
3848 cur
->dir_gen
, valid_path
);
3851 ret
= send_rmdir(sctx
, valid_path
);
3854 last_dir_ino_rm
= cur
->dir
;
3862 __free_recorded_refs(&check_dirs
);
3863 free_recorded_refs(sctx
);
3864 fs_path_free(valid_path
);
3868 static int record_ref(struct btrfs_root
*root
, int num
, u64 dir
, int index
,
3869 struct fs_path
*name
, void *ctx
, struct list_head
*refs
)
3872 struct send_ctx
*sctx
= ctx
;
3876 p
= fs_path_alloc();
3880 ret
= get_inode_info(root
, dir
, NULL
, &gen
, NULL
, NULL
,
3885 ret
= get_cur_path(sctx
, dir
, gen
, p
);
3888 ret
= fs_path_add_path(p
, name
);
3892 ret
= __record_ref(refs
, dir
, gen
, p
);
3900 static int __record_new_ref(int num
, u64 dir
, int index
,
3901 struct fs_path
*name
,
3904 struct send_ctx
*sctx
= ctx
;
3905 return record_ref(sctx
->send_root
, num
, dir
, index
, name
,
3906 ctx
, &sctx
->new_refs
);
3910 static int __record_deleted_ref(int num
, u64 dir
, int index
,
3911 struct fs_path
*name
,
3914 struct send_ctx
*sctx
= ctx
;
3915 return record_ref(sctx
->parent_root
, num
, dir
, index
, name
,
3916 ctx
, &sctx
->deleted_refs
);
3919 static int record_new_ref(struct send_ctx
*sctx
)
3923 ret
= iterate_inode_ref(sctx
->send_root
, sctx
->left_path
,
3924 sctx
->cmp_key
, 0, __record_new_ref
, sctx
);
3933 static int record_deleted_ref(struct send_ctx
*sctx
)
3937 ret
= iterate_inode_ref(sctx
->parent_root
, sctx
->right_path
,
3938 sctx
->cmp_key
, 0, __record_deleted_ref
, sctx
);
3947 struct find_ref_ctx
{
3950 struct btrfs_root
*root
;
3951 struct fs_path
*name
;
3955 static int __find_iref(int num
, u64 dir
, int index
,
3956 struct fs_path
*name
,
3959 struct find_ref_ctx
*ctx
= ctx_
;
3963 if (dir
== ctx
->dir
&& fs_path_len(name
) == fs_path_len(ctx
->name
) &&
3964 strncmp(name
->start
, ctx
->name
->start
, fs_path_len(name
)) == 0) {
3966 * To avoid doing extra lookups we'll only do this if everything
3969 ret
= get_inode_info(ctx
->root
, dir
, NULL
, &dir_gen
, NULL
,
3973 if (dir_gen
!= ctx
->dir_gen
)
3975 ctx
->found_idx
= num
;
3981 static int find_iref(struct btrfs_root
*root
,
3982 struct btrfs_path
*path
,
3983 struct btrfs_key
*key
,
3984 u64 dir
, u64 dir_gen
, struct fs_path
*name
)
3987 struct find_ref_ctx ctx
;
3991 ctx
.dir_gen
= dir_gen
;
3995 ret
= iterate_inode_ref(root
, path
, key
, 0, __find_iref
, &ctx
);
3999 if (ctx
.found_idx
== -1)
4002 return ctx
.found_idx
;
4005 static int __record_changed_new_ref(int num
, u64 dir
, int index
,
4006 struct fs_path
*name
,
4011 struct send_ctx
*sctx
= ctx
;
4013 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &dir_gen
, NULL
,
4018 ret
= find_iref(sctx
->parent_root
, sctx
->right_path
,
4019 sctx
->cmp_key
, dir
, dir_gen
, name
);
4021 ret
= __record_new_ref(num
, dir
, index
, name
, sctx
);
4028 static int __record_changed_deleted_ref(int num
, u64 dir
, int index
,
4029 struct fs_path
*name
,
4034 struct send_ctx
*sctx
= ctx
;
4036 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &dir_gen
, NULL
,
4041 ret
= find_iref(sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
4042 dir
, dir_gen
, name
);
4044 ret
= __record_deleted_ref(num
, dir
, index
, name
, sctx
);
4051 static int record_changed_ref(struct send_ctx
*sctx
)
4055 ret
= iterate_inode_ref(sctx
->send_root
, sctx
->left_path
,
4056 sctx
->cmp_key
, 0, __record_changed_new_ref
, sctx
);
4059 ret
= iterate_inode_ref(sctx
->parent_root
, sctx
->right_path
,
4060 sctx
->cmp_key
, 0, __record_changed_deleted_ref
, sctx
);
4070 * Record and process all refs at once. Needed when an inode changes the
4071 * generation number, which means that it was deleted and recreated.
4073 static int process_all_refs(struct send_ctx
*sctx
,
4074 enum btrfs_compare_tree_result cmd
)
4077 struct btrfs_root
*root
;
4078 struct btrfs_path
*path
;
4079 struct btrfs_key key
;
4080 struct btrfs_key found_key
;
4081 struct extent_buffer
*eb
;
4083 iterate_inode_ref_t cb
;
4084 int pending_move
= 0;
4086 path
= alloc_path_for_send();
4090 if (cmd
== BTRFS_COMPARE_TREE_NEW
) {
4091 root
= sctx
->send_root
;
4092 cb
= __record_new_ref
;
4093 } else if (cmd
== BTRFS_COMPARE_TREE_DELETED
) {
4094 root
= sctx
->parent_root
;
4095 cb
= __record_deleted_ref
;
4097 btrfs_err(sctx
->send_root
->fs_info
,
4098 "Wrong command %d in process_all_refs", cmd
);
4103 key
.objectid
= sctx
->cmp_key
->objectid
;
4104 key
.type
= BTRFS_INODE_REF_KEY
;
4106 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4111 eb
= path
->nodes
[0];
4112 slot
= path
->slots
[0];
4113 if (slot
>= btrfs_header_nritems(eb
)) {
4114 ret
= btrfs_next_leaf(root
, path
);
4122 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4124 if (found_key
.objectid
!= key
.objectid
||
4125 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
4126 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
))
4129 ret
= iterate_inode_ref(root
, path
, &found_key
, 0, cb
, sctx
);
4135 btrfs_release_path(path
);
4137 ret
= process_recorded_refs(sctx
, &pending_move
);
4138 /* Only applicable to an incremental send. */
4139 ASSERT(pending_move
== 0);
4142 btrfs_free_path(path
);
4146 static int send_set_xattr(struct send_ctx
*sctx
,
4147 struct fs_path
*path
,
4148 const char *name
, int name_len
,
4149 const char *data
, int data_len
)
4153 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SET_XATTR
);
4157 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
4158 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
4159 TLV_PUT(sctx
, BTRFS_SEND_A_XATTR_DATA
, data
, data_len
);
4161 ret
= send_cmd(sctx
);
4168 static int send_remove_xattr(struct send_ctx
*sctx
,
4169 struct fs_path
*path
,
4170 const char *name
, int name_len
)
4174 ret
= begin_cmd(sctx
, BTRFS_SEND_C_REMOVE_XATTR
);
4178 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
4179 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
4181 ret
= send_cmd(sctx
);
4188 static int __process_new_xattr(int num
, struct btrfs_key
*di_key
,
4189 const char *name
, int name_len
,
4190 const char *data
, int data_len
,
4194 struct send_ctx
*sctx
= ctx
;
4196 posix_acl_xattr_header dummy_acl
;
4198 /* Capabilities are emitted by finish_inode_if_needed */
4199 if (!strncmp(name
, XATTR_NAME_CAPS
, name_len
))
4202 p
= fs_path_alloc();
4207 * This hack is needed because empty acl's are stored as zero byte
4208 * data in xattrs. Problem with that is, that receiving these zero byte
4209 * acl's will fail later. To fix this, we send a dummy acl list that
4210 * only contains the version number and no entries.
4212 if (!strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
, name_len
) ||
4213 !strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
, name_len
)) {
4214 if (data_len
== 0) {
4215 dummy_acl
.a_version
=
4216 cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
4217 data
= (char *)&dummy_acl
;
4218 data_len
= sizeof(dummy_acl
);
4222 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4226 ret
= send_set_xattr(sctx
, p
, name
, name_len
, data
, data_len
);
4233 static int __process_deleted_xattr(int num
, struct btrfs_key
*di_key
,
4234 const char *name
, int name_len
,
4235 const char *data
, int data_len
,
4239 struct send_ctx
*sctx
= ctx
;
4242 p
= fs_path_alloc();
4246 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4250 ret
= send_remove_xattr(sctx
, p
, name
, name_len
);
4257 static int process_new_xattr(struct send_ctx
*sctx
)
4261 ret
= iterate_dir_item(sctx
->send_root
, sctx
->left_path
,
4262 sctx
->cmp_key
, __process_new_xattr
, sctx
);
4267 static int process_deleted_xattr(struct send_ctx
*sctx
)
4271 ret
= iterate_dir_item(sctx
->parent_root
, sctx
->right_path
,
4272 sctx
->cmp_key
, __process_deleted_xattr
, sctx
);
4277 struct find_xattr_ctx
{
4285 static int __find_xattr(int num
, struct btrfs_key
*di_key
,
4286 const char *name
, int name_len
,
4287 const char *data
, int data_len
,
4288 u8 type
, void *vctx
)
4290 struct find_xattr_ctx
*ctx
= vctx
;
4292 if (name_len
== ctx
->name_len
&&
4293 strncmp(name
, ctx
->name
, name_len
) == 0) {
4294 ctx
->found_idx
= num
;
4295 ctx
->found_data_len
= data_len
;
4296 ctx
->found_data
= kmemdup(data
, data_len
, GFP_NOFS
);
4297 if (!ctx
->found_data
)
4304 static int find_xattr(struct btrfs_root
*root
,
4305 struct btrfs_path
*path
,
4306 struct btrfs_key
*key
,
4307 const char *name
, int name_len
,
4308 char **data
, int *data_len
)
4311 struct find_xattr_ctx ctx
;
4314 ctx
.name_len
= name_len
;
4316 ctx
.found_data
= NULL
;
4317 ctx
.found_data_len
= 0;
4319 ret
= iterate_dir_item(root
, path
, key
, __find_xattr
, &ctx
);
4323 if (ctx
.found_idx
== -1)
4326 *data
= ctx
.found_data
;
4327 *data_len
= ctx
.found_data_len
;
4329 kfree(ctx
.found_data
);
4331 return ctx
.found_idx
;
4335 static int __process_changed_new_xattr(int num
, struct btrfs_key
*di_key
,
4336 const char *name
, int name_len
,
4337 const char *data
, int data_len
,
4341 struct send_ctx
*sctx
= ctx
;
4342 char *found_data
= NULL
;
4343 int found_data_len
= 0;
4345 ret
= find_xattr(sctx
->parent_root
, sctx
->right_path
,
4346 sctx
->cmp_key
, name
, name_len
, &found_data
,
4348 if (ret
== -ENOENT
) {
4349 ret
= __process_new_xattr(num
, di_key
, name
, name_len
, data
,
4350 data_len
, type
, ctx
);
4351 } else if (ret
>= 0) {
4352 if (data_len
!= found_data_len
||
4353 memcmp(data
, found_data
, data_len
)) {
4354 ret
= __process_new_xattr(num
, di_key
, name
, name_len
,
4355 data
, data_len
, type
, ctx
);
4365 static int __process_changed_deleted_xattr(int num
, struct btrfs_key
*di_key
,
4366 const char *name
, int name_len
,
4367 const char *data
, int data_len
,
4371 struct send_ctx
*sctx
= ctx
;
4373 ret
= find_xattr(sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
4374 name
, name_len
, NULL
, NULL
);
4376 ret
= __process_deleted_xattr(num
, di_key
, name
, name_len
, data
,
4377 data_len
, type
, ctx
);
4384 static int process_changed_xattr(struct send_ctx
*sctx
)
4388 ret
= iterate_dir_item(sctx
->send_root
, sctx
->left_path
,
4389 sctx
->cmp_key
, __process_changed_new_xattr
, sctx
);
4392 ret
= iterate_dir_item(sctx
->parent_root
, sctx
->right_path
,
4393 sctx
->cmp_key
, __process_changed_deleted_xattr
, sctx
);
4399 static int process_all_new_xattrs(struct send_ctx
*sctx
)
4402 struct btrfs_root
*root
;
4403 struct btrfs_path
*path
;
4404 struct btrfs_key key
;
4405 struct btrfs_key found_key
;
4406 struct extent_buffer
*eb
;
4409 path
= alloc_path_for_send();
4413 root
= sctx
->send_root
;
4415 key
.objectid
= sctx
->cmp_key
->objectid
;
4416 key
.type
= BTRFS_XATTR_ITEM_KEY
;
4418 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4423 eb
= path
->nodes
[0];
4424 slot
= path
->slots
[0];
4425 if (slot
>= btrfs_header_nritems(eb
)) {
4426 ret
= btrfs_next_leaf(root
, path
);
4429 } else if (ret
> 0) {
4436 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4437 if (found_key
.objectid
!= key
.objectid
||
4438 found_key
.type
!= key
.type
) {
4443 ret
= iterate_dir_item(root
, path
, &found_key
,
4444 __process_new_xattr
, sctx
);
4452 btrfs_free_path(path
);
4456 static ssize_t
fill_read_buf(struct send_ctx
*sctx
, u64 offset
, u32 len
)
4458 struct btrfs_root
*root
= sctx
->send_root
;
4459 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4460 struct inode
*inode
;
4463 struct btrfs_key key
;
4464 pgoff_t index
= offset
>> PAGE_CACHE_SHIFT
;
4466 unsigned pg_offset
= offset
& ~PAGE_CACHE_MASK
;
4469 key
.objectid
= sctx
->cur_ino
;
4470 key
.type
= BTRFS_INODE_ITEM_KEY
;
4473 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
4475 return PTR_ERR(inode
);
4477 if (offset
+ len
> i_size_read(inode
)) {
4478 if (offset
> i_size_read(inode
))
4481 len
= offset
- i_size_read(inode
);
4486 last_index
= (offset
+ len
- 1) >> PAGE_CACHE_SHIFT
;
4488 /* initial readahead */
4489 memset(&sctx
->ra
, 0, sizeof(struct file_ra_state
));
4490 file_ra_state_init(&sctx
->ra
, inode
->i_mapping
);
4491 btrfs_force_ra(inode
->i_mapping
, &sctx
->ra
, NULL
, index
,
4492 last_index
- index
+ 1);
4494 while (index
<= last_index
) {
4495 unsigned cur_len
= min_t(unsigned, len
,
4496 PAGE_CACHE_SIZE
- pg_offset
);
4497 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
4503 if (!PageUptodate(page
)) {
4504 btrfs_readpage(NULL
, page
);
4506 if (!PageUptodate(page
)) {
4508 page_cache_release(page
);
4515 memcpy(sctx
->read_buf
+ ret
, addr
+ pg_offset
, cur_len
);
4518 page_cache_release(page
);
4530 * Read some bytes from the current inode/file and send a write command to
4533 static int send_write(struct send_ctx
*sctx
, u64 offset
, u32 len
)
4537 ssize_t num_read
= 0;
4539 p
= fs_path_alloc();
4543 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset
, len
);
4545 num_read
= fill_read_buf(sctx
, offset
, len
);
4546 if (num_read
<= 0) {
4552 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
4556 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4560 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4561 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4562 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, num_read
);
4564 ret
= send_cmd(sctx
);
4575 * Send a clone command to user space.
4577 static int send_clone(struct send_ctx
*sctx
,
4578 u64 offset
, u32 len
,
4579 struct clone_root
*clone_root
)
4585 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4586 "clone_inode=%llu, clone_offset=%llu\n", offset
, len
,
4587 clone_root
->root
->objectid
, clone_root
->ino
,
4588 clone_root
->offset
);
4590 p
= fs_path_alloc();
4594 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CLONE
);
4598 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4602 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4603 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_LEN
, len
);
4604 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4606 if (clone_root
->root
== sctx
->send_root
) {
4607 ret
= get_inode_info(sctx
->send_root
, clone_root
->ino
, NULL
,
4608 &gen
, NULL
, NULL
, NULL
, NULL
);
4611 ret
= get_cur_path(sctx
, clone_root
->ino
, gen
, p
);
4613 ret
= get_inode_path(clone_root
->root
, clone_root
->ino
, p
);
4619 * If the parent we're using has a received_uuid set then use that as
4620 * our clone source as that is what we will look for when doing a
4623 * This covers the case that we create a snapshot off of a received
4624 * subvolume and then use that as the parent and try to receive on a
4627 if (!btrfs_is_empty_uuid(clone_root
->root
->root_item
.received_uuid
))
4628 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
4629 clone_root
->root
->root_item
.received_uuid
);
4631 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
4632 clone_root
->root
->root_item
.uuid
);
4633 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
4634 le64_to_cpu(clone_root
->root
->root_item
.ctransid
));
4635 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_CLONE_PATH
, p
);
4636 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_OFFSET
,
4637 clone_root
->offset
);
4639 ret
= send_cmd(sctx
);
4648 * Send an update extent command to user space.
4650 static int send_update_extent(struct send_ctx
*sctx
,
4651 u64 offset
, u32 len
)
4656 p
= fs_path_alloc();
4660 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UPDATE_EXTENT
);
4664 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4668 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4669 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4670 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, len
);
4672 ret
= send_cmd(sctx
);
4680 static int send_hole(struct send_ctx
*sctx
, u64 end
)
4682 struct fs_path
*p
= NULL
;
4683 u64 offset
= sctx
->cur_inode_last_extent
;
4687 if (sctx
->flags
& BTRFS_SEND_FLAG_NO_FILE_DATA
)
4688 return send_update_extent(sctx
, offset
, end
- offset
);
4690 p
= fs_path_alloc();
4693 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4695 goto tlv_put_failure
;
4696 memset(sctx
->read_buf
, 0, BTRFS_SEND_READ_SIZE
);
4697 while (offset
< end
) {
4698 len
= min_t(u64
, end
- offset
, BTRFS_SEND_READ_SIZE
);
4700 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
4703 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4704 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4705 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, len
);
4706 ret
= send_cmd(sctx
);
4716 static int send_extent_data(struct send_ctx
*sctx
,
4722 if (sctx
->flags
& BTRFS_SEND_FLAG_NO_FILE_DATA
)
4723 return send_update_extent(sctx
, offset
, len
);
4725 while (sent
< len
) {
4726 u64 size
= len
- sent
;
4729 if (size
> BTRFS_SEND_READ_SIZE
)
4730 size
= BTRFS_SEND_READ_SIZE
;
4731 ret
= send_write(sctx
, offset
+ sent
, size
);
4742 * Search for a capability xattr related to sctx->cur_ino. If the capability is
4743 * found, call send_set_xattr function to emit it.
4745 * Return 0 if there isn't a capability, or when the capability was emitted
4746 * successfully, or < 0 if an error occurred.
4748 static int send_capabilities(struct send_ctx
*sctx
)
4750 struct fs_path
*fspath
= NULL
;
4751 struct btrfs_path
*path
;
4752 struct btrfs_dir_item
*di
;
4753 struct extent_buffer
*leaf
;
4754 unsigned long data_ptr
;
4759 path
= alloc_path_for_send();
4763 di
= btrfs_lookup_xattr(NULL
, sctx
->send_root
, path
, sctx
->cur_ino
,
4764 XATTR_NAME_CAPS
, strlen(XATTR_NAME_CAPS
), 0);
4766 /* There is no xattr for this inode */
4768 } else if (IS_ERR(di
)) {
4773 leaf
= path
->nodes
[0];
4774 buf_len
= btrfs_dir_data_len(leaf
, di
);
4776 fspath
= fs_path_alloc();
4777 buf
= kmalloc(buf_len
, GFP_KERNEL
);
4778 if (!fspath
|| !buf
) {
4783 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, fspath
);
4787 data_ptr
= (unsigned long)(di
+ 1) + btrfs_dir_name_len(leaf
, di
);
4788 read_extent_buffer(leaf
, buf
, data_ptr
, buf_len
);
4790 ret
= send_set_xattr(sctx
, fspath
, XATTR_NAME_CAPS
,
4791 strlen(XATTR_NAME_CAPS
), buf
, buf_len
);
4794 fs_path_free(fspath
);
4795 btrfs_free_path(path
);
4799 static int clone_range(struct send_ctx
*sctx
,
4800 struct clone_root
*clone_root
,
4801 const u64 disk_byte
,
4806 struct btrfs_path
*path
;
4807 struct btrfs_key key
;
4810 path
= alloc_path_for_send();
4815 * We can't send a clone operation for the entire range if we find
4816 * extent items in the respective range in the source file that
4817 * refer to different extents or if we find holes.
4818 * So check for that and do a mix of clone and regular write/copy
4819 * operations if needed.
4823 * mkfs.btrfs -f /dev/sda
4824 * mount /dev/sda /mnt
4825 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
4826 * cp --reflink=always /mnt/foo /mnt/bar
4827 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
4828 * btrfs subvolume snapshot -r /mnt /mnt/snap
4830 * If when we send the snapshot and we are processing file bar (which
4831 * has a higher inode number than foo) we blindly send a clone operation
4832 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
4833 * a file bar that matches the content of file foo - iow, doesn't match
4834 * the content from bar in the original filesystem.
4836 key
.objectid
= clone_root
->ino
;
4837 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4838 key
.offset
= clone_root
->offset
;
4839 ret
= btrfs_search_slot(NULL
, clone_root
->root
, &key
, path
, 0, 0);
4842 if (ret
> 0 && path
->slots
[0] > 0) {
4843 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0] - 1);
4844 if (key
.objectid
== clone_root
->ino
&&
4845 key
.type
== BTRFS_EXTENT_DATA_KEY
)
4850 struct extent_buffer
*leaf
= path
->nodes
[0];
4851 int slot
= path
->slots
[0];
4852 struct btrfs_file_extent_item
*ei
;
4857 if (slot
>= btrfs_header_nritems(leaf
)) {
4858 ret
= btrfs_next_leaf(clone_root
->root
, path
);
4866 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
4869 * We might have an implicit trailing hole (NO_HOLES feature
4870 * enabled). We deal with it after leaving this loop.
4872 if (key
.objectid
!= clone_root
->ino
||
4873 key
.type
!= BTRFS_EXTENT_DATA_KEY
)
4876 ei
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
4877 type
= btrfs_file_extent_type(leaf
, ei
);
4878 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4879 ext_len
= btrfs_file_extent_inline_len(leaf
, slot
, ei
);
4880 ext_len
= PAGE_CACHE_ALIGN(ext_len
);
4882 ext_len
= btrfs_file_extent_num_bytes(leaf
, ei
);
4885 if (key
.offset
+ ext_len
<= clone_root
->offset
)
4888 if (key
.offset
> clone_root
->offset
) {
4889 /* Implicit hole, NO_HOLES feature enabled. */
4890 u64 hole_len
= key
.offset
- clone_root
->offset
;
4894 ret
= send_extent_data(sctx
, offset
, hole_len
);
4902 clone_root
->offset
+= hole_len
;
4903 data_offset
+= hole_len
;
4906 if (key
.offset
>= clone_root
->offset
+ len
)
4909 clone_len
= min_t(u64
, ext_len
, len
);
4911 if (btrfs_file_extent_disk_bytenr(leaf
, ei
) == disk_byte
&&
4912 btrfs_file_extent_offset(leaf
, ei
) == data_offset
)
4913 ret
= send_clone(sctx
, offset
, clone_len
, clone_root
);
4915 ret
= send_extent_data(sctx
, offset
, clone_len
);
4923 offset
+= clone_len
;
4924 clone_root
->offset
+= clone_len
;
4925 data_offset
+= clone_len
;
4931 ret
= send_extent_data(sctx
, offset
, len
);
4935 btrfs_free_path(path
);
4939 static int send_write_or_clone(struct send_ctx
*sctx
,
4940 struct btrfs_path
*path
,
4941 struct btrfs_key
*key
,
4942 struct clone_root
*clone_root
)
4945 struct btrfs_file_extent_item
*ei
;
4946 u64 offset
= key
->offset
;
4949 u64 bs
= sctx
->send_root
->fs_info
->sb
->s_blocksize
;
4951 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4952 struct btrfs_file_extent_item
);
4953 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
4954 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4955 len
= btrfs_file_extent_inline_len(path
->nodes
[0],
4956 path
->slots
[0], ei
);
4958 * it is possible the inline item won't cover the whole page,
4959 * but there may be items after this page. Make
4960 * sure to send the whole thing
4962 len
= PAGE_CACHE_ALIGN(len
);
4964 len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
4967 if (offset
+ len
> sctx
->cur_inode_size
)
4968 len
= sctx
->cur_inode_size
- offset
;
4974 if (clone_root
&& IS_ALIGNED(offset
+ len
, bs
)) {
4978 disk_byte
= btrfs_file_extent_disk_bytenr(path
->nodes
[0], ei
);
4979 data_offset
= btrfs_file_extent_offset(path
->nodes
[0], ei
);
4980 ret
= clone_range(sctx
, clone_root
, disk_byte
, data_offset
,
4983 ret
= send_extent_data(sctx
, offset
, len
);
4989 static int is_extent_unchanged(struct send_ctx
*sctx
,
4990 struct btrfs_path
*left_path
,
4991 struct btrfs_key
*ekey
)
4994 struct btrfs_key key
;
4995 struct btrfs_path
*path
= NULL
;
4996 struct extent_buffer
*eb
;
4998 struct btrfs_key found_key
;
4999 struct btrfs_file_extent_item
*ei
;
5004 u64 left_offset_fixed
;
5012 path
= alloc_path_for_send();
5016 eb
= left_path
->nodes
[0];
5017 slot
= left_path
->slots
[0];
5018 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
5019 left_type
= btrfs_file_extent_type(eb
, ei
);
5021 if (left_type
!= BTRFS_FILE_EXTENT_REG
) {
5025 left_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
5026 left_len
= btrfs_file_extent_num_bytes(eb
, ei
);
5027 left_offset
= btrfs_file_extent_offset(eb
, ei
);
5028 left_gen
= btrfs_file_extent_generation(eb
, ei
);
5031 * Following comments will refer to these graphics. L is the left
5032 * extents which we are checking at the moment. 1-8 are the right
5033 * extents that we iterate.
5036 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5039 * |--1--|-2b-|...(same as above)
5041 * Alternative situation. Happens on files where extents got split.
5043 * |-----------7-----------|-6-|
5045 * Alternative situation. Happens on files which got larger.
5048 * Nothing follows after 8.
5051 key
.objectid
= ekey
->objectid
;
5052 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5053 key
.offset
= ekey
->offset
;
5054 ret
= btrfs_search_slot_for_read(sctx
->parent_root
, &key
, path
, 0, 0);
5063 * Handle special case where the right side has no extents at all.
5065 eb
= path
->nodes
[0];
5066 slot
= path
->slots
[0];
5067 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5068 if (found_key
.objectid
!= key
.objectid
||
5069 found_key
.type
!= key
.type
) {
5070 /* If we're a hole then just pretend nothing changed */
5071 ret
= (left_disknr
) ? 0 : 1;
5076 * We're now on 2a, 2b or 7.
5079 while (key
.offset
< ekey
->offset
+ left_len
) {
5080 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
5081 right_type
= btrfs_file_extent_type(eb
, ei
);
5082 if (right_type
!= BTRFS_FILE_EXTENT_REG
&&
5083 right_type
!= BTRFS_FILE_EXTENT_INLINE
) {
5088 if (right_type
== BTRFS_FILE_EXTENT_INLINE
) {
5089 right_len
= btrfs_file_extent_inline_len(eb
, slot
, ei
);
5090 right_len
= PAGE_ALIGN(right_len
);
5092 right_len
= btrfs_file_extent_num_bytes(eb
, ei
);
5096 * Are we at extent 8? If yes, we know the extent is changed.
5097 * This may only happen on the first iteration.
5099 if (found_key
.offset
+ right_len
<= ekey
->offset
) {
5100 /* If we're a hole just pretend nothing changed */
5101 ret
= (left_disknr
) ? 0 : 1;
5106 * We just wanted to see if when we have an inline extent, what
5107 * follows it is a regular extent (wanted to check the above
5108 * condition for inline extents too). This should normally not
5109 * happen but it's possible for example when we have an inline
5110 * compressed extent representing data with a size matching
5111 * the page size (currently the same as sector size).
5113 if (right_type
== BTRFS_FILE_EXTENT_INLINE
) {
5118 right_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
5119 right_offset
= btrfs_file_extent_offset(eb
, ei
);
5120 right_gen
= btrfs_file_extent_generation(eb
, ei
);
5122 left_offset_fixed
= left_offset
;
5123 if (key
.offset
< ekey
->offset
) {
5124 /* Fix the right offset for 2a and 7. */
5125 right_offset
+= ekey
->offset
- key
.offset
;
5127 /* Fix the left offset for all behind 2a and 2b */
5128 left_offset_fixed
+= key
.offset
- ekey
->offset
;
5132 * Check if we have the same extent.
5134 if (left_disknr
!= right_disknr
||
5135 left_offset_fixed
!= right_offset
||
5136 left_gen
!= right_gen
) {
5142 * Go to the next extent.
5144 ret
= btrfs_next_item(sctx
->parent_root
, path
);
5148 eb
= path
->nodes
[0];
5149 slot
= path
->slots
[0];
5150 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5152 if (ret
|| found_key
.objectid
!= key
.objectid
||
5153 found_key
.type
!= key
.type
) {
5154 key
.offset
+= right_len
;
5157 if (found_key
.offset
!= key
.offset
+ right_len
) {
5165 * We're now behind the left extent (treat as unchanged) or at the end
5166 * of the right side (treat as changed).
5168 if (key
.offset
>= ekey
->offset
+ left_len
)
5175 btrfs_free_path(path
);
5179 static int get_last_extent(struct send_ctx
*sctx
, u64 offset
)
5181 struct btrfs_path
*path
;
5182 struct btrfs_root
*root
= sctx
->send_root
;
5183 struct btrfs_file_extent_item
*fi
;
5184 struct btrfs_key key
;
5189 path
= alloc_path_for_send();
5193 sctx
->cur_inode_last_extent
= 0;
5195 key
.objectid
= sctx
->cur_ino
;
5196 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5197 key
.offset
= offset
;
5198 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 0, 1);
5202 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
5203 if (key
.objectid
!= sctx
->cur_ino
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
5206 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5207 struct btrfs_file_extent_item
);
5208 type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
5209 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
5210 u64 size
= btrfs_file_extent_inline_len(path
->nodes
[0],
5211 path
->slots
[0], fi
);
5212 extent_end
= ALIGN(key
.offset
+ size
,
5213 sctx
->send_root
->sectorsize
);
5215 extent_end
= key
.offset
+
5216 btrfs_file_extent_num_bytes(path
->nodes
[0], fi
);
5218 sctx
->cur_inode_last_extent
= extent_end
;
5220 btrfs_free_path(path
);
5224 static int maybe_send_hole(struct send_ctx
*sctx
, struct btrfs_path
*path
,
5225 struct btrfs_key
*key
)
5227 struct btrfs_file_extent_item
*fi
;
5232 if (sctx
->cur_ino
!= key
->objectid
|| !need_send_hole(sctx
))
5235 if (sctx
->cur_inode_last_extent
== (u64
)-1) {
5236 ret
= get_last_extent(sctx
, key
->offset
- 1);
5241 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5242 struct btrfs_file_extent_item
);
5243 type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
5244 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
5245 u64 size
= btrfs_file_extent_inline_len(path
->nodes
[0],
5246 path
->slots
[0], fi
);
5247 extent_end
= ALIGN(key
->offset
+ size
,
5248 sctx
->send_root
->sectorsize
);
5250 extent_end
= key
->offset
+
5251 btrfs_file_extent_num_bytes(path
->nodes
[0], fi
);
5254 if (path
->slots
[0] == 0 &&
5255 sctx
->cur_inode_last_extent
< key
->offset
) {
5257 * We might have skipped entire leafs that contained only
5258 * file extent items for our current inode. These leafs have
5259 * a generation number smaller (older) than the one in the
5260 * current leaf and the leaf our last extent came from, and
5261 * are located between these 2 leafs.
5263 ret
= get_last_extent(sctx
, key
->offset
- 1);
5268 if (sctx
->cur_inode_last_extent
< key
->offset
)
5269 ret
= send_hole(sctx
, key
->offset
);
5270 sctx
->cur_inode_last_extent
= extent_end
;
5274 static int process_extent(struct send_ctx
*sctx
,
5275 struct btrfs_path
*path
,
5276 struct btrfs_key
*key
)
5278 struct clone_root
*found_clone
= NULL
;
5281 if (S_ISLNK(sctx
->cur_inode_mode
))
5284 if (sctx
->parent_root
&& !sctx
->cur_inode_new
) {
5285 ret
= is_extent_unchanged(sctx
, path
, key
);
5293 struct btrfs_file_extent_item
*ei
;
5296 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5297 struct btrfs_file_extent_item
);
5298 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
5299 if (type
== BTRFS_FILE_EXTENT_PREALLOC
||
5300 type
== BTRFS_FILE_EXTENT_REG
) {
5302 * The send spec does not have a prealloc command yet,
5303 * so just leave a hole for prealloc'ed extents until
5304 * we have enough commands queued up to justify rev'ing
5307 if (type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5312 /* Have a hole, just skip it. */
5313 if (btrfs_file_extent_disk_bytenr(path
->nodes
[0], ei
) == 0) {
5320 ret
= find_extent_clone(sctx
, path
, key
->objectid
, key
->offset
,
5321 sctx
->cur_inode_size
, &found_clone
);
5322 if (ret
!= -ENOENT
&& ret
< 0)
5325 ret
= send_write_or_clone(sctx
, path
, key
, found_clone
);
5329 ret
= maybe_send_hole(sctx
, path
, key
);
5334 static int process_all_extents(struct send_ctx
*sctx
)
5337 struct btrfs_root
*root
;
5338 struct btrfs_path
*path
;
5339 struct btrfs_key key
;
5340 struct btrfs_key found_key
;
5341 struct extent_buffer
*eb
;
5344 root
= sctx
->send_root
;
5345 path
= alloc_path_for_send();
5349 key
.objectid
= sctx
->cmp_key
->objectid
;
5350 key
.type
= BTRFS_EXTENT_DATA_KEY
;
5352 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5357 eb
= path
->nodes
[0];
5358 slot
= path
->slots
[0];
5360 if (slot
>= btrfs_header_nritems(eb
)) {
5361 ret
= btrfs_next_leaf(root
, path
);
5364 } else if (ret
> 0) {
5371 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5373 if (found_key
.objectid
!= key
.objectid
||
5374 found_key
.type
!= key
.type
) {
5379 ret
= process_extent(sctx
, path
, &found_key
);
5387 btrfs_free_path(path
);
5391 static int process_recorded_refs_if_needed(struct send_ctx
*sctx
, int at_end
,
5393 int *refs_processed
)
5397 if (sctx
->cur_ino
== 0)
5399 if (!at_end
&& sctx
->cur_ino
== sctx
->cmp_key
->objectid
&&
5400 sctx
->cmp_key
->type
<= BTRFS_INODE_EXTREF_KEY
)
5402 if (list_empty(&sctx
->new_refs
) && list_empty(&sctx
->deleted_refs
))
5405 ret
= process_recorded_refs(sctx
, pending_move
);
5409 *refs_processed
= 1;
5414 static int finish_inode_if_needed(struct send_ctx
*sctx
, int at_end
)
5425 int pending_move
= 0;
5426 int refs_processed
= 0;
5428 ret
= process_recorded_refs_if_needed(sctx
, at_end
, &pending_move
,
5434 * We have processed the refs and thus need to advance send_progress.
5435 * Now, calls to get_cur_xxx will take the updated refs of the current
5436 * inode into account.
5438 * On the other hand, if our current inode is a directory and couldn't
5439 * be moved/renamed because its parent was renamed/moved too and it has
5440 * a higher inode number, we can only move/rename our current inode
5441 * after we moved/renamed its parent. Therefore in this case operate on
5442 * the old path (pre move/rename) of our current inode, and the
5443 * move/rename will be performed later.
5445 if (refs_processed
&& !pending_move
)
5446 sctx
->send_progress
= sctx
->cur_ino
+ 1;
5448 if (sctx
->cur_ino
== 0 || sctx
->cur_inode_deleted
)
5450 if (!at_end
&& sctx
->cmp_key
->objectid
== sctx
->cur_ino
)
5453 ret
= get_inode_info(sctx
->send_root
, sctx
->cur_ino
, NULL
, NULL
,
5454 &left_mode
, &left_uid
, &left_gid
, NULL
);
5458 if (!sctx
->parent_root
|| sctx
->cur_inode_new
) {
5460 if (!S_ISLNK(sctx
->cur_inode_mode
))
5463 ret
= get_inode_info(sctx
->parent_root
, sctx
->cur_ino
,
5464 NULL
, NULL
, &right_mode
, &right_uid
,
5469 if (left_uid
!= right_uid
|| left_gid
!= right_gid
)
5471 if (!S_ISLNK(sctx
->cur_inode_mode
) && left_mode
!= right_mode
)
5475 if (S_ISREG(sctx
->cur_inode_mode
)) {
5476 if (need_send_hole(sctx
)) {
5477 if (sctx
->cur_inode_last_extent
== (u64
)-1 ||
5478 sctx
->cur_inode_last_extent
<
5479 sctx
->cur_inode_size
) {
5480 ret
= get_last_extent(sctx
, (u64
)-1);
5484 if (sctx
->cur_inode_last_extent
<
5485 sctx
->cur_inode_size
) {
5486 ret
= send_hole(sctx
, sctx
->cur_inode_size
);
5491 ret
= send_truncate(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
5492 sctx
->cur_inode_size
);
5498 ret
= send_chown(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
5499 left_uid
, left_gid
);
5504 ret
= send_chmod(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
5510 ret
= send_capabilities(sctx
);
5515 * If other directory inodes depended on our current directory
5516 * inode's move/rename, now do their move/rename operations.
5518 if (!is_waiting_for_move(sctx
, sctx
->cur_ino
)) {
5519 ret
= apply_children_dir_moves(sctx
);
5523 * Need to send that every time, no matter if it actually
5524 * changed between the two trees as we have done changes to
5525 * the inode before. If our inode is a directory and it's
5526 * waiting to be moved/renamed, we will send its utimes when
5527 * it's moved/renamed, therefore we don't need to do it here.
5529 sctx
->send_progress
= sctx
->cur_ino
+ 1;
5530 ret
= send_utimes(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
);
5539 static int changed_inode(struct send_ctx
*sctx
,
5540 enum btrfs_compare_tree_result result
)
5543 struct btrfs_key
*key
= sctx
->cmp_key
;
5544 struct btrfs_inode_item
*left_ii
= NULL
;
5545 struct btrfs_inode_item
*right_ii
= NULL
;
5549 sctx
->cur_ino
= key
->objectid
;
5550 sctx
->cur_inode_new_gen
= 0;
5551 sctx
->cur_inode_last_extent
= (u64
)-1;
5554 * Set send_progress to current inode. This will tell all get_cur_xxx
5555 * functions that the current inode's refs are not updated yet. Later,
5556 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5558 sctx
->send_progress
= sctx
->cur_ino
;
5560 if (result
== BTRFS_COMPARE_TREE_NEW
||
5561 result
== BTRFS_COMPARE_TREE_CHANGED
) {
5562 left_ii
= btrfs_item_ptr(sctx
->left_path
->nodes
[0],
5563 sctx
->left_path
->slots
[0],
5564 struct btrfs_inode_item
);
5565 left_gen
= btrfs_inode_generation(sctx
->left_path
->nodes
[0],
5568 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
5569 sctx
->right_path
->slots
[0],
5570 struct btrfs_inode_item
);
5571 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
5574 if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
5575 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
5576 sctx
->right_path
->slots
[0],
5577 struct btrfs_inode_item
);
5579 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
5583 * The cur_ino = root dir case is special here. We can't treat
5584 * the inode as deleted+reused because it would generate a
5585 * stream that tries to delete/mkdir the root dir.
5587 if (left_gen
!= right_gen
&&
5588 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
5589 sctx
->cur_inode_new_gen
= 1;
5592 if (result
== BTRFS_COMPARE_TREE_NEW
) {
5593 sctx
->cur_inode_gen
= left_gen
;
5594 sctx
->cur_inode_new
= 1;
5595 sctx
->cur_inode_deleted
= 0;
5596 sctx
->cur_inode_size
= btrfs_inode_size(
5597 sctx
->left_path
->nodes
[0], left_ii
);
5598 sctx
->cur_inode_mode
= btrfs_inode_mode(
5599 sctx
->left_path
->nodes
[0], left_ii
);
5600 sctx
->cur_inode_rdev
= btrfs_inode_rdev(
5601 sctx
->left_path
->nodes
[0], left_ii
);
5602 if (sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
5603 ret
= send_create_inode_if_needed(sctx
);
5604 } else if (result
== BTRFS_COMPARE_TREE_DELETED
) {
5605 sctx
->cur_inode_gen
= right_gen
;
5606 sctx
->cur_inode_new
= 0;
5607 sctx
->cur_inode_deleted
= 1;
5608 sctx
->cur_inode_size
= btrfs_inode_size(
5609 sctx
->right_path
->nodes
[0], right_ii
);
5610 sctx
->cur_inode_mode
= btrfs_inode_mode(
5611 sctx
->right_path
->nodes
[0], right_ii
);
5612 } else if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
5614 * We need to do some special handling in case the inode was
5615 * reported as changed with a changed generation number. This
5616 * means that the original inode was deleted and new inode
5617 * reused the same inum. So we have to treat the old inode as
5618 * deleted and the new one as new.
5620 if (sctx
->cur_inode_new_gen
) {
5622 * First, process the inode as if it was deleted.
5624 sctx
->cur_inode_gen
= right_gen
;
5625 sctx
->cur_inode_new
= 0;
5626 sctx
->cur_inode_deleted
= 1;
5627 sctx
->cur_inode_size
= btrfs_inode_size(
5628 sctx
->right_path
->nodes
[0], right_ii
);
5629 sctx
->cur_inode_mode
= btrfs_inode_mode(
5630 sctx
->right_path
->nodes
[0], right_ii
);
5631 ret
= process_all_refs(sctx
,
5632 BTRFS_COMPARE_TREE_DELETED
);
5637 * Now process the inode as if it was new.
5639 sctx
->cur_inode_gen
= left_gen
;
5640 sctx
->cur_inode_new
= 1;
5641 sctx
->cur_inode_deleted
= 0;
5642 sctx
->cur_inode_size
= btrfs_inode_size(
5643 sctx
->left_path
->nodes
[0], left_ii
);
5644 sctx
->cur_inode_mode
= btrfs_inode_mode(
5645 sctx
->left_path
->nodes
[0], left_ii
);
5646 sctx
->cur_inode_rdev
= btrfs_inode_rdev(
5647 sctx
->left_path
->nodes
[0], left_ii
);
5648 ret
= send_create_inode_if_needed(sctx
);
5652 ret
= process_all_refs(sctx
, BTRFS_COMPARE_TREE_NEW
);
5656 * Advance send_progress now as we did not get into
5657 * process_recorded_refs_if_needed in the new_gen case.
5659 sctx
->send_progress
= sctx
->cur_ino
+ 1;
5662 * Now process all extents and xattrs of the inode as if
5663 * they were all new.
5665 ret
= process_all_extents(sctx
);
5668 ret
= process_all_new_xattrs(sctx
);
5672 sctx
->cur_inode_gen
= left_gen
;
5673 sctx
->cur_inode_new
= 0;
5674 sctx
->cur_inode_new_gen
= 0;
5675 sctx
->cur_inode_deleted
= 0;
5676 sctx
->cur_inode_size
= btrfs_inode_size(
5677 sctx
->left_path
->nodes
[0], left_ii
);
5678 sctx
->cur_inode_mode
= btrfs_inode_mode(
5679 sctx
->left_path
->nodes
[0], left_ii
);
5688 * We have to process new refs before deleted refs, but compare_trees gives us
5689 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5690 * first and later process them in process_recorded_refs.
5691 * For the cur_inode_new_gen case, we skip recording completely because
5692 * changed_inode did already initiate processing of refs. The reason for this is
5693 * that in this case, compare_tree actually compares the refs of 2 different
5694 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5695 * refs of the right tree as deleted and all refs of the left tree as new.
5697 static int changed_ref(struct send_ctx
*sctx
,
5698 enum btrfs_compare_tree_result result
)
5702 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
5704 if (!sctx
->cur_inode_new_gen
&&
5705 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
5706 if (result
== BTRFS_COMPARE_TREE_NEW
)
5707 ret
= record_new_ref(sctx
);
5708 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
5709 ret
= record_deleted_ref(sctx
);
5710 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
5711 ret
= record_changed_ref(sctx
);
5718 * Process new/deleted/changed xattrs. We skip processing in the
5719 * cur_inode_new_gen case because changed_inode did already initiate processing
5720 * of xattrs. The reason is the same as in changed_ref
5722 static int changed_xattr(struct send_ctx
*sctx
,
5723 enum btrfs_compare_tree_result result
)
5727 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
5729 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
5730 if (result
== BTRFS_COMPARE_TREE_NEW
)
5731 ret
= process_new_xattr(sctx
);
5732 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
5733 ret
= process_deleted_xattr(sctx
);
5734 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
5735 ret
= process_changed_xattr(sctx
);
5742 * Process new/deleted/changed extents. We skip processing in the
5743 * cur_inode_new_gen case because changed_inode did already initiate processing
5744 * of extents. The reason is the same as in changed_ref
5746 static int changed_extent(struct send_ctx
*sctx
,
5747 enum btrfs_compare_tree_result result
)
5751 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
5753 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
5754 if (result
!= BTRFS_COMPARE_TREE_DELETED
)
5755 ret
= process_extent(sctx
, sctx
->left_path
,
5762 static int dir_changed(struct send_ctx
*sctx
, u64 dir
)
5764 u64 orig_gen
, new_gen
;
5767 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &new_gen
, NULL
, NULL
,
5772 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &orig_gen
, NULL
,
5777 return (orig_gen
!= new_gen
) ? 1 : 0;
5780 static int compare_refs(struct send_ctx
*sctx
, struct btrfs_path
*path
,
5781 struct btrfs_key
*key
)
5783 struct btrfs_inode_extref
*extref
;
5784 struct extent_buffer
*leaf
;
5785 u64 dirid
= 0, last_dirid
= 0;
5792 /* Easy case, just check this one dirid */
5793 if (key
->type
== BTRFS_INODE_REF_KEY
) {
5794 dirid
= key
->offset
;
5796 ret
= dir_changed(sctx
, dirid
);
5800 leaf
= path
->nodes
[0];
5801 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
5802 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
5803 while (cur_offset
< item_size
) {
5804 extref
= (struct btrfs_inode_extref
*)(ptr
+
5806 dirid
= btrfs_inode_extref_parent(leaf
, extref
);
5807 ref_name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
5808 cur_offset
+= ref_name_len
+ sizeof(*extref
);
5809 if (dirid
== last_dirid
)
5811 ret
= dir_changed(sctx
, dirid
);
5821 * Updates compare related fields in sctx and simply forwards to the actual
5822 * changed_xxx functions.
5824 static int changed_cb(struct btrfs_root
*left_root
,
5825 struct btrfs_root
*right_root
,
5826 struct btrfs_path
*left_path
,
5827 struct btrfs_path
*right_path
,
5828 struct btrfs_key
*key
,
5829 enum btrfs_compare_tree_result result
,
5833 struct send_ctx
*sctx
= ctx
;
5835 if (result
== BTRFS_COMPARE_TREE_SAME
) {
5836 if (key
->type
== BTRFS_INODE_REF_KEY
||
5837 key
->type
== BTRFS_INODE_EXTREF_KEY
) {
5838 ret
= compare_refs(sctx
, left_path
, key
);
5843 } else if (key
->type
== BTRFS_EXTENT_DATA_KEY
) {
5844 return maybe_send_hole(sctx
, left_path
, key
);
5848 result
= BTRFS_COMPARE_TREE_CHANGED
;
5852 sctx
->left_path
= left_path
;
5853 sctx
->right_path
= right_path
;
5854 sctx
->cmp_key
= key
;
5856 ret
= finish_inode_if_needed(sctx
, 0);
5860 /* Ignore non-FS objects */
5861 if (key
->objectid
== BTRFS_FREE_INO_OBJECTID
||
5862 key
->objectid
== BTRFS_FREE_SPACE_OBJECTID
)
5865 if (key
->type
== BTRFS_INODE_ITEM_KEY
)
5866 ret
= changed_inode(sctx
, result
);
5867 else if (key
->type
== BTRFS_INODE_REF_KEY
||
5868 key
->type
== BTRFS_INODE_EXTREF_KEY
)
5869 ret
= changed_ref(sctx
, result
);
5870 else if (key
->type
== BTRFS_XATTR_ITEM_KEY
)
5871 ret
= changed_xattr(sctx
, result
);
5872 else if (key
->type
== BTRFS_EXTENT_DATA_KEY
)
5873 ret
= changed_extent(sctx
, result
);
5879 static int full_send_tree(struct send_ctx
*sctx
)
5882 struct btrfs_root
*send_root
= sctx
->send_root
;
5883 struct btrfs_key key
;
5884 struct btrfs_key found_key
;
5885 struct btrfs_path
*path
;
5886 struct extent_buffer
*eb
;
5889 path
= alloc_path_for_send();
5893 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
5894 key
.type
= BTRFS_INODE_ITEM_KEY
;
5897 ret
= btrfs_search_slot_for_read(send_root
, &key
, path
, 1, 0);
5904 eb
= path
->nodes
[0];
5905 slot
= path
->slots
[0];
5906 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5908 ret
= changed_cb(send_root
, NULL
, path
, NULL
,
5909 &found_key
, BTRFS_COMPARE_TREE_NEW
, sctx
);
5913 key
.objectid
= found_key
.objectid
;
5914 key
.type
= found_key
.type
;
5915 key
.offset
= found_key
.offset
+ 1;
5917 ret
= btrfs_next_item(send_root
, path
);
5927 ret
= finish_inode_if_needed(sctx
, 1);
5930 btrfs_free_path(path
);
5934 static int send_subvol(struct send_ctx
*sctx
)
5938 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_STREAM_HEADER
)) {
5939 ret
= send_header(sctx
);
5944 ret
= send_subvol_begin(sctx
);
5948 if (sctx
->parent_root
) {
5949 ret
= btrfs_compare_trees(sctx
->send_root
, sctx
->parent_root
,
5953 ret
= finish_inode_if_needed(sctx
, 1);
5957 ret
= full_send_tree(sctx
);
5963 free_recorded_refs(sctx
);
5968 * If orphan cleanup did remove any orphans from a root, it means the tree
5969 * was modified and therefore the commit root is not the same as the current
5970 * root anymore. This is a problem, because send uses the commit root and
5971 * therefore can see inode items that don't exist in the current root anymore,
5972 * and for example make calls to btrfs_iget, which will do tree lookups based
5973 * on the current root and not on the commit root. Those lookups will fail,
5974 * returning a -ESTALE error, and making send fail with that error. So make
5975 * sure a send does not see any orphans we have just removed, and that it will
5976 * see the same inodes regardless of whether a transaction commit happened
5977 * before it started (meaning that the commit root will be the same as the
5978 * current root) or not.
5980 static int ensure_commit_roots_uptodate(struct send_ctx
*sctx
)
5983 struct btrfs_trans_handle
*trans
= NULL
;
5986 if (sctx
->parent_root
&&
5987 sctx
->parent_root
->node
!= sctx
->parent_root
->commit_root
)
5990 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++)
5991 if (sctx
->clone_roots
[i
].root
->node
!=
5992 sctx
->clone_roots
[i
].root
->commit_root
)
5996 return btrfs_end_transaction(trans
, sctx
->send_root
);
6001 /* Use any root, all fs roots will get their commit roots updated. */
6003 trans
= btrfs_join_transaction(sctx
->send_root
);
6005 return PTR_ERR(trans
);
6009 return btrfs_commit_transaction(trans
, sctx
->send_root
);
6012 static void btrfs_root_dec_send_in_progress(struct btrfs_root
* root
)
6014 spin_lock(&root
->root_item_lock
);
6015 root
->send_in_progress
--;
6017 * Not much left to do, we don't know why it's unbalanced and
6018 * can't blindly reset it to 0.
6020 if (root
->send_in_progress
< 0)
6021 btrfs_err(root
->fs_info
,
6022 "send_in_progres unbalanced %d root %llu",
6023 root
->send_in_progress
, root
->root_key
.objectid
);
6024 spin_unlock(&root
->root_item_lock
);
6027 long btrfs_ioctl_send(struct file
*mnt_file
, void __user
*arg_
)
6030 struct btrfs_root
*send_root
;
6031 struct btrfs_root
*clone_root
;
6032 struct btrfs_fs_info
*fs_info
;
6033 struct btrfs_ioctl_send_args
*arg
= NULL
;
6034 struct btrfs_key key
;
6035 struct send_ctx
*sctx
= NULL
;
6037 u64
*clone_sources_tmp
= NULL
;
6038 int clone_sources_to_rollback
= 0;
6039 int sort_clone_roots
= 0;
6042 if (!capable(CAP_SYS_ADMIN
))
6045 send_root
= BTRFS_I(file_inode(mnt_file
))->root
;
6046 fs_info
= send_root
->fs_info
;
6049 * The subvolume must remain read-only during send, protect against
6050 * making it RW. This also protects against deletion.
6052 spin_lock(&send_root
->root_item_lock
);
6053 send_root
->send_in_progress
++;
6054 spin_unlock(&send_root
->root_item_lock
);
6057 * This is done when we lookup the root, it should already be complete
6058 * by the time we get here.
6060 WARN_ON(send_root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
);
6063 * Userspace tools do the checks and warn the user if it's
6066 if (!btrfs_root_readonly(send_root
)) {
6071 arg
= memdup_user(arg_
, sizeof(*arg
));
6078 if (!access_ok(VERIFY_READ
, arg
->clone_sources
,
6079 sizeof(*arg
->clone_sources
) *
6080 arg
->clone_sources_count
)) {
6085 if (arg
->flags
& ~BTRFS_SEND_FLAG_MASK
) {
6090 sctx
= kzalloc(sizeof(struct send_ctx
), GFP_NOFS
);
6096 INIT_LIST_HEAD(&sctx
->new_refs
);
6097 INIT_LIST_HEAD(&sctx
->deleted_refs
);
6098 INIT_RADIX_TREE(&sctx
->name_cache
, GFP_NOFS
);
6099 INIT_LIST_HEAD(&sctx
->name_cache_list
);
6101 sctx
->flags
= arg
->flags
;
6103 sctx
->send_filp
= fget(arg
->send_fd
);
6104 if (!sctx
->send_filp
) {
6109 sctx
->send_root
= send_root
;
6111 * Unlikely but possible, if the subvolume is marked for deletion but
6112 * is slow to remove the directory entry, send can still be started
6114 if (btrfs_root_dead(sctx
->send_root
)) {
6119 sctx
->clone_roots_cnt
= arg
->clone_sources_count
;
6121 sctx
->send_max_size
= BTRFS_SEND_BUF_SIZE
;
6122 sctx
->send_buf
= vmalloc(sctx
->send_max_size
);
6123 if (!sctx
->send_buf
) {
6128 sctx
->read_buf
= vmalloc(BTRFS_SEND_READ_SIZE
);
6129 if (!sctx
->read_buf
) {
6134 sctx
->pending_dir_moves
= RB_ROOT
;
6135 sctx
->waiting_dir_moves
= RB_ROOT
;
6136 sctx
->orphan_dirs
= RB_ROOT
;
6138 sctx
->clone_roots
= vzalloc(sizeof(struct clone_root
) *
6139 (arg
->clone_sources_count
+ 1));
6140 if (!sctx
->clone_roots
) {
6145 if (arg
->clone_sources_count
) {
6146 clone_sources_tmp
= vmalloc(arg
->clone_sources_count
*
6147 sizeof(*arg
->clone_sources
));
6148 if (!clone_sources_tmp
) {
6153 ret
= copy_from_user(clone_sources_tmp
, arg
->clone_sources
,
6154 arg
->clone_sources_count
*
6155 sizeof(*arg
->clone_sources
));
6161 for (i
= 0; i
< arg
->clone_sources_count
; i
++) {
6162 key
.objectid
= clone_sources_tmp
[i
];
6163 key
.type
= BTRFS_ROOT_ITEM_KEY
;
6164 key
.offset
= (u64
)-1;
6166 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
6168 clone_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
6169 if (IS_ERR(clone_root
)) {
6170 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6171 ret
= PTR_ERR(clone_root
);
6174 spin_lock(&clone_root
->root_item_lock
);
6175 if (!btrfs_root_readonly(clone_root
) ||
6176 btrfs_root_dead(clone_root
)) {
6177 spin_unlock(&clone_root
->root_item_lock
);
6178 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6182 clone_root
->send_in_progress
++;
6183 spin_unlock(&clone_root
->root_item_lock
);
6184 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6186 sctx
->clone_roots
[i
].root
= clone_root
;
6187 clone_sources_to_rollback
= i
+ 1;
6189 vfree(clone_sources_tmp
);
6190 clone_sources_tmp
= NULL
;
6193 if (arg
->parent_root
) {
6194 key
.objectid
= arg
->parent_root
;
6195 key
.type
= BTRFS_ROOT_ITEM_KEY
;
6196 key
.offset
= (u64
)-1;
6198 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
6200 sctx
->parent_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
6201 if (IS_ERR(sctx
->parent_root
)) {
6202 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6203 ret
= PTR_ERR(sctx
->parent_root
);
6207 spin_lock(&sctx
->parent_root
->root_item_lock
);
6208 sctx
->parent_root
->send_in_progress
++;
6209 if (!btrfs_root_readonly(sctx
->parent_root
) ||
6210 btrfs_root_dead(sctx
->parent_root
)) {
6211 spin_unlock(&sctx
->parent_root
->root_item_lock
);
6212 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6216 spin_unlock(&sctx
->parent_root
->root_item_lock
);
6218 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
6222 * Clones from send_root are allowed, but only if the clone source
6223 * is behind the current send position. This is checked while searching
6224 * for possible clone sources.
6226 sctx
->clone_roots
[sctx
->clone_roots_cnt
++].root
= sctx
->send_root
;
6228 /* We do a bsearch later */
6229 sort(sctx
->clone_roots
, sctx
->clone_roots_cnt
,
6230 sizeof(*sctx
->clone_roots
), __clone_root_cmp_sort
,
6232 sort_clone_roots
= 1;
6234 ret
= ensure_commit_roots_uptodate(sctx
);
6238 current
->journal_info
= BTRFS_SEND_TRANS_STUB
;
6239 ret
= send_subvol(sctx
);
6240 current
->journal_info
= NULL
;
6244 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_END_CMD
)) {
6245 ret
= begin_cmd(sctx
, BTRFS_SEND_C_END
);
6248 ret
= send_cmd(sctx
);
6254 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->pending_dir_moves
));
6255 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->pending_dir_moves
)) {
6257 struct pending_dir_move
*pm
;
6259 n
= rb_first(&sctx
->pending_dir_moves
);
6260 pm
= rb_entry(n
, struct pending_dir_move
, node
);
6261 while (!list_empty(&pm
->list
)) {
6262 struct pending_dir_move
*pm2
;
6264 pm2
= list_first_entry(&pm
->list
,
6265 struct pending_dir_move
, list
);
6266 free_pending_move(sctx
, pm2
);
6268 free_pending_move(sctx
, pm
);
6271 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
));
6272 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
)) {
6274 struct waiting_dir_move
*dm
;
6276 n
= rb_first(&sctx
->waiting_dir_moves
);
6277 dm
= rb_entry(n
, struct waiting_dir_move
, node
);
6278 rb_erase(&dm
->node
, &sctx
->waiting_dir_moves
);
6282 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->orphan_dirs
));
6283 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->orphan_dirs
)) {
6285 struct orphan_dir_info
*odi
;
6287 n
= rb_first(&sctx
->orphan_dirs
);
6288 odi
= rb_entry(n
, struct orphan_dir_info
, node
);
6289 free_orphan_dir_info(sctx
, odi
);
6292 if (sort_clone_roots
) {
6293 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++)
6294 btrfs_root_dec_send_in_progress(
6295 sctx
->clone_roots
[i
].root
);
6297 for (i
= 0; sctx
&& i
< clone_sources_to_rollback
; i
++)
6298 btrfs_root_dec_send_in_progress(
6299 sctx
->clone_roots
[i
].root
);
6301 btrfs_root_dec_send_in_progress(send_root
);
6303 if (sctx
&& !IS_ERR_OR_NULL(sctx
->parent_root
))
6304 btrfs_root_dec_send_in_progress(sctx
->parent_root
);
6307 vfree(clone_sources_tmp
);
6310 if (sctx
->send_filp
)
6311 fput(sctx
->send_filp
);
6313 vfree(sctx
->clone_roots
);
6314 vfree(sctx
->send_buf
);
6315 vfree(sctx
->read_buf
);
6317 name_cache_free(sctx
);