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"
38 static int g_verbose
= 0;
40 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
43 * A fs_path is a helper to dynamically build path names with unknown size.
44 * It reallocates the internal buffer on demand.
45 * It allows fast adding of path elements on the right side (normal path) and
46 * fast adding to the left side (reversed path). A reversed path can also be
47 * unreversed if needed.
58 unsigned int reversed
:1;
59 unsigned int virtual_mem
:1;
65 #define FS_PATH_INLINE_SIZE \
66 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
69 /* reused for each extent */
71 struct btrfs_root
*root
;
78 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
79 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
82 struct file
*send_filp
;
88 u64 cmd_send_size
[BTRFS_SEND_C_MAX
+ 1];
89 u64 flags
; /* 'flags' member of btrfs_ioctl_send_args is u64 */
91 struct btrfs_root
*send_root
;
92 struct btrfs_root
*parent_root
;
93 struct clone_root
*clone_roots
;
96 /* current state of the compare_tree call */
97 struct btrfs_path
*left_path
;
98 struct btrfs_path
*right_path
;
99 struct btrfs_key
*cmp_key
;
102 * infos of the currently processed inode. In case of deleted inodes,
103 * these are the values from the deleted inode.
108 int cur_inode_new_gen
;
109 int cur_inode_deleted
;
112 u64 cur_inode_last_extent
;
116 struct list_head new_refs
;
117 struct list_head deleted_refs
;
119 struct radix_tree_root name_cache
;
120 struct list_head name_cache_list
;
126 * We process inodes by their increasing order, so if before an
127 * incremental send we reverse the parent/child relationship of
128 * directories such that a directory with a lower inode number was
129 * the parent of a directory with a higher inode number, and the one
130 * becoming the new parent got renamed too, we can't rename/move the
131 * directory with lower inode number when we finish processing it - we
132 * must process the directory with higher inode number first, then
133 * rename/move it and then rename/move the directory with lower inode
134 * number. Example follows.
136 * Tree state when the first send was performed:
148 * Tree state when the second (incremental) send is performed:
157 * The sequence of steps that lead to the second state was:
159 * mv /a/b/c/d /a/b/c2/d2
160 * mv /a/b/c /a/b/c2/d2/cc
162 * "c" has lower inode number, but we can't move it (2nd mv operation)
163 * before we move "d", which has higher inode number.
165 * So we just memorize which move/rename operations must be performed
166 * later when their respective parent is processed and moved/renamed.
169 /* Indexed by parent directory inode number. */
170 struct rb_root pending_dir_moves
;
173 * Reverse index, indexed by the inode number of a directory that
174 * is waiting for the move/rename of its immediate parent before its
175 * own move/rename can be performed.
177 struct rb_root waiting_dir_moves
;
180 struct pending_dir_move
{
182 struct list_head list
;
186 struct list_head update_refs
;
189 struct waiting_dir_move
{
194 struct name_cache_entry
{
195 struct list_head list
;
197 * radix_tree has only 32bit entries but we need to handle 64bit inums.
198 * We use the lower 32bit of the 64bit inum to store it in the tree. If
199 * more then one inum would fall into the same entry, we use radix_list
200 * to store the additional entries. radix_list is also used to store
201 * entries where two entries have the same inum but different
204 struct list_head radix_list
;
210 int need_later_update
;
215 static int is_waiting_for_move(struct send_ctx
*sctx
, u64 ino
);
217 static int need_send_hole(struct send_ctx
*sctx
)
219 return (sctx
->parent_root
&& !sctx
->cur_inode_new
&&
220 !sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
&&
221 S_ISREG(sctx
->cur_inode_mode
));
224 static void fs_path_reset(struct fs_path
*p
)
227 p
->start
= p
->buf
+ p
->buf_len
- 1;
237 static struct fs_path
*fs_path_alloc(void)
241 p
= kmalloc(sizeof(*p
), GFP_NOFS
);
246 p
->buf
= p
->inline_buf
;
247 p
->buf_len
= FS_PATH_INLINE_SIZE
;
252 static struct fs_path
*fs_path_alloc_reversed(void)
264 static void fs_path_free(struct fs_path
*p
)
268 if (p
->buf
!= p
->inline_buf
) {
277 static int fs_path_len(struct fs_path
*p
)
279 return p
->end
- p
->start
;
282 static int fs_path_ensure_buf(struct fs_path
*p
, int len
)
290 if (p
->buf_len
>= len
)
293 path_len
= p
->end
- p
->start
;
294 old_buf_len
= p
->buf_len
;
295 len
= PAGE_ALIGN(len
);
297 if (p
->buf
== p
->inline_buf
) {
298 tmp_buf
= kmalloc(len
, GFP_NOFS
| __GFP_NOWARN
);
300 tmp_buf
= vmalloc(len
);
305 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
309 if (p
->virtual_mem
) {
310 tmp_buf
= vmalloc(len
);
313 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
316 tmp_buf
= krealloc(p
->buf
, len
, GFP_NOFS
);
318 tmp_buf
= vmalloc(len
);
321 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
330 tmp_buf
= p
->buf
+ old_buf_len
- path_len
- 1;
331 p
->end
= p
->buf
+ p
->buf_len
- 1;
332 p
->start
= p
->end
- path_len
;
333 memmove(p
->start
, tmp_buf
, path_len
+ 1);
336 p
->end
= p
->start
+ path_len
;
341 static int fs_path_prepare_for_add(struct fs_path
*p
, int name_len
)
346 new_len
= p
->end
- p
->start
+ name_len
;
347 if (p
->start
!= p
->end
)
349 ret
= fs_path_ensure_buf(p
, new_len
);
354 if (p
->start
!= p
->end
)
356 p
->start
-= name_len
;
357 p
->prepared
= p
->start
;
359 if (p
->start
!= p
->end
)
361 p
->prepared
= p
->end
;
370 static int fs_path_add(struct fs_path
*p
, const char *name
, int name_len
)
374 ret
= fs_path_prepare_for_add(p
, name_len
);
377 memcpy(p
->prepared
, name
, name_len
);
384 static int fs_path_add_path(struct fs_path
*p
, struct fs_path
*p2
)
388 ret
= fs_path_prepare_for_add(p
, p2
->end
- p2
->start
);
391 memcpy(p
->prepared
, p2
->start
, p2
->end
- p2
->start
);
398 static int fs_path_add_from_extent_buffer(struct fs_path
*p
,
399 struct extent_buffer
*eb
,
400 unsigned long off
, int len
)
404 ret
= fs_path_prepare_for_add(p
, len
);
408 read_extent_buffer(eb
, p
->prepared
, off
, len
);
415 static int fs_path_copy(struct fs_path
*p
, struct fs_path
*from
)
419 p
->reversed
= from
->reversed
;
422 ret
= fs_path_add_path(p
, from
);
428 static void fs_path_unreverse(struct fs_path
*p
)
437 len
= p
->end
- p
->start
;
439 p
->end
= p
->start
+ len
;
440 memmove(p
->start
, tmp
, len
+ 1);
444 static struct btrfs_path
*alloc_path_for_send(void)
446 struct btrfs_path
*path
;
448 path
= btrfs_alloc_path();
451 path
->search_commit_root
= 1;
452 path
->skip_locking
= 1;
456 static int write_buf(struct file
*filp
, const void *buf
, u32 len
, loff_t
*off
)
466 ret
= vfs_write(filp
, (char *)buf
+ pos
, len
- pos
, off
);
467 /* TODO handle that correctly */
468 /*if (ret == -ERESTARTSYS) {
487 static int tlv_put(struct send_ctx
*sctx
, u16 attr
, const void *data
, int len
)
489 struct btrfs_tlv_header
*hdr
;
490 int total_len
= sizeof(*hdr
) + len
;
491 int left
= sctx
->send_max_size
- sctx
->send_size
;
493 if (unlikely(left
< total_len
))
496 hdr
= (struct btrfs_tlv_header
*) (sctx
->send_buf
+ sctx
->send_size
);
497 hdr
->tlv_type
= cpu_to_le16(attr
);
498 hdr
->tlv_len
= cpu_to_le16(len
);
499 memcpy(hdr
+ 1, data
, len
);
500 sctx
->send_size
+= total_len
;
505 #define TLV_PUT_DEFINE_INT(bits) \
506 static int tlv_put_u##bits(struct send_ctx *sctx, \
507 u##bits attr, u##bits value) \
509 __le##bits __tmp = cpu_to_le##bits(value); \
510 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
513 TLV_PUT_DEFINE_INT(64)
515 static int tlv_put_string(struct send_ctx
*sctx
, u16 attr
,
516 const char *str
, int len
)
520 return tlv_put(sctx
, attr
, str
, len
);
523 static int tlv_put_uuid(struct send_ctx
*sctx
, u16 attr
,
526 return tlv_put(sctx
, attr
, uuid
, BTRFS_UUID_SIZE
);
529 static int tlv_put_btrfs_timespec(struct send_ctx
*sctx
, u16 attr
,
530 struct extent_buffer
*eb
,
531 struct btrfs_timespec
*ts
)
533 struct btrfs_timespec bts
;
534 read_extent_buffer(eb
, &bts
, (unsigned long)ts
, sizeof(bts
));
535 return tlv_put(sctx
, attr
, &bts
, sizeof(bts
));
539 #define TLV_PUT(sctx, attrtype, attrlen, data) \
541 ret = tlv_put(sctx, attrtype, attrlen, data); \
543 goto tlv_put_failure; \
546 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
548 ret = tlv_put_u##bits(sctx, attrtype, value); \
550 goto tlv_put_failure; \
553 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
554 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
555 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
556 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
557 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
559 ret = tlv_put_string(sctx, attrtype, str, len); \
561 goto tlv_put_failure; \
563 #define TLV_PUT_PATH(sctx, attrtype, p) \
565 ret = tlv_put_string(sctx, attrtype, p->start, \
566 p->end - p->start); \
568 goto tlv_put_failure; \
570 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
572 ret = tlv_put_uuid(sctx, attrtype, uuid); \
574 goto tlv_put_failure; \
576 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
578 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
580 goto tlv_put_failure; \
583 static int send_header(struct send_ctx
*sctx
)
585 struct btrfs_stream_header hdr
;
587 strcpy(hdr
.magic
, BTRFS_SEND_STREAM_MAGIC
);
588 hdr
.version
= cpu_to_le32(BTRFS_SEND_STREAM_VERSION
);
590 return write_buf(sctx
->send_filp
, &hdr
, sizeof(hdr
),
595 * For each command/item we want to send to userspace, we call this function.
597 static int begin_cmd(struct send_ctx
*sctx
, int cmd
)
599 struct btrfs_cmd_header
*hdr
;
601 if (WARN_ON(!sctx
->send_buf
))
604 BUG_ON(sctx
->send_size
);
606 sctx
->send_size
+= sizeof(*hdr
);
607 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
608 hdr
->cmd
= cpu_to_le16(cmd
);
613 static int send_cmd(struct send_ctx
*sctx
)
616 struct btrfs_cmd_header
*hdr
;
619 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
620 hdr
->len
= cpu_to_le32(sctx
->send_size
- sizeof(*hdr
));
623 crc
= btrfs_crc32c(0, (unsigned char *)sctx
->send_buf
, sctx
->send_size
);
624 hdr
->crc
= cpu_to_le32(crc
);
626 ret
= write_buf(sctx
->send_filp
, sctx
->send_buf
, sctx
->send_size
,
629 sctx
->total_send_size
+= sctx
->send_size
;
630 sctx
->cmd_send_size
[le16_to_cpu(hdr
->cmd
)] += sctx
->send_size
;
637 * Sends a move instruction to user space
639 static int send_rename(struct send_ctx
*sctx
,
640 struct fs_path
*from
, struct fs_path
*to
)
644 verbose_printk("btrfs: send_rename %s -> %s\n", from
->start
, to
->start
);
646 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RENAME
);
650 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, from
);
651 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_TO
, to
);
653 ret
= send_cmd(sctx
);
661 * Sends a link instruction to user space
663 static int send_link(struct send_ctx
*sctx
,
664 struct fs_path
*path
, struct fs_path
*lnk
)
668 verbose_printk("btrfs: send_link %s -> %s\n", path
->start
, lnk
->start
);
670 ret
= begin_cmd(sctx
, BTRFS_SEND_C_LINK
);
674 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
675 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, lnk
);
677 ret
= send_cmd(sctx
);
685 * Sends an unlink instruction to user space
687 static int send_unlink(struct send_ctx
*sctx
, struct fs_path
*path
)
691 verbose_printk("btrfs: send_unlink %s\n", path
->start
);
693 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UNLINK
);
697 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
699 ret
= send_cmd(sctx
);
707 * Sends a rmdir instruction to user space
709 static int send_rmdir(struct send_ctx
*sctx
, struct fs_path
*path
)
713 verbose_printk("btrfs: send_rmdir %s\n", path
->start
);
715 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RMDIR
);
719 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
721 ret
= send_cmd(sctx
);
729 * Helper function to retrieve some fields from an inode item.
731 static int get_inode_info(struct btrfs_root
*root
,
732 u64 ino
, u64
*size
, u64
*gen
,
733 u64
*mode
, u64
*uid
, u64
*gid
,
737 struct btrfs_inode_item
*ii
;
738 struct btrfs_key key
;
739 struct btrfs_path
*path
;
741 path
= alloc_path_for_send();
746 key
.type
= BTRFS_INODE_ITEM_KEY
;
748 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
756 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
757 struct btrfs_inode_item
);
759 *size
= btrfs_inode_size(path
->nodes
[0], ii
);
761 *gen
= btrfs_inode_generation(path
->nodes
[0], ii
);
763 *mode
= btrfs_inode_mode(path
->nodes
[0], ii
);
765 *uid
= btrfs_inode_uid(path
->nodes
[0], ii
);
767 *gid
= btrfs_inode_gid(path
->nodes
[0], ii
);
769 *rdev
= btrfs_inode_rdev(path
->nodes
[0], ii
);
772 btrfs_free_path(path
);
776 typedef int (*iterate_inode_ref_t
)(int num
, u64 dir
, int index
,
781 * Helper function to iterate the entries in ONE btrfs_inode_ref or
782 * btrfs_inode_extref.
783 * The iterate callback may return a non zero value to stop iteration. This can
784 * be a negative value for error codes or 1 to simply stop it.
786 * path must point to the INODE_REF or INODE_EXTREF when called.
788 static int iterate_inode_ref(struct btrfs_root
*root
, struct btrfs_path
*path
,
789 struct btrfs_key
*found_key
, int resolve
,
790 iterate_inode_ref_t iterate
, void *ctx
)
792 struct extent_buffer
*eb
= path
->nodes
[0];
793 struct btrfs_item
*item
;
794 struct btrfs_inode_ref
*iref
;
795 struct btrfs_inode_extref
*extref
;
796 struct btrfs_path
*tmp_path
;
800 int slot
= path
->slots
[0];
807 unsigned long name_off
;
808 unsigned long elem_size
;
811 p
= fs_path_alloc_reversed();
815 tmp_path
= alloc_path_for_send();
822 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
823 ptr
= (unsigned long)btrfs_item_ptr(eb
, slot
,
824 struct btrfs_inode_ref
);
825 item
= btrfs_item_nr(slot
);
826 total
= btrfs_item_size(eb
, item
);
827 elem_size
= sizeof(*iref
);
829 ptr
= btrfs_item_ptr_offset(eb
, slot
);
830 total
= btrfs_item_size_nr(eb
, slot
);
831 elem_size
= sizeof(*extref
);
834 while (cur
< total
) {
837 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
838 iref
= (struct btrfs_inode_ref
*)(ptr
+ cur
);
839 name_len
= btrfs_inode_ref_name_len(eb
, iref
);
840 name_off
= (unsigned long)(iref
+ 1);
841 index
= btrfs_inode_ref_index(eb
, iref
);
842 dir
= found_key
->offset
;
844 extref
= (struct btrfs_inode_extref
*)(ptr
+ cur
);
845 name_len
= btrfs_inode_extref_name_len(eb
, extref
);
846 name_off
= (unsigned long)&extref
->name
;
847 index
= btrfs_inode_extref_index(eb
, extref
);
848 dir
= btrfs_inode_extref_parent(eb
, extref
);
852 start
= btrfs_ref_to_path(root
, tmp_path
, name_len
,
856 ret
= PTR_ERR(start
);
859 if (start
< p
->buf
) {
860 /* overflow , try again with larger buffer */
861 ret
= fs_path_ensure_buf(p
,
862 p
->buf_len
+ p
->buf
- start
);
865 start
= btrfs_ref_to_path(root
, tmp_path
,
870 ret
= PTR_ERR(start
);
873 BUG_ON(start
< p
->buf
);
877 ret
= fs_path_add_from_extent_buffer(p
, eb
, name_off
,
883 cur
+= elem_size
+ name_len
;
884 ret
= iterate(num
, dir
, index
, p
, ctx
);
891 btrfs_free_path(tmp_path
);
896 typedef int (*iterate_dir_item_t
)(int num
, struct btrfs_key
*di_key
,
897 const char *name
, int name_len
,
898 const char *data
, int data_len
,
902 * Helper function to iterate the entries in ONE btrfs_dir_item.
903 * The iterate callback may return a non zero value to stop iteration. This can
904 * be a negative value for error codes or 1 to simply stop it.
906 * path must point to the dir item when called.
908 static int iterate_dir_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
909 struct btrfs_key
*found_key
,
910 iterate_dir_item_t iterate
, void *ctx
)
913 struct extent_buffer
*eb
;
914 struct btrfs_item
*item
;
915 struct btrfs_dir_item
*di
;
916 struct btrfs_key di_key
;
931 buf
= kmalloc(buf_len
, GFP_NOFS
);
938 slot
= path
->slots
[0];
939 item
= btrfs_item_nr(slot
);
940 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
943 total
= btrfs_item_size(eb
, item
);
946 while (cur
< total
) {
947 name_len
= btrfs_dir_name_len(eb
, di
);
948 data_len
= btrfs_dir_data_len(eb
, di
);
949 type
= btrfs_dir_type(eb
, di
);
950 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
952 if (name_len
+ data_len
> buf_len
) {
953 buf_len
= PAGE_ALIGN(name_len
+ data_len
);
955 buf2
= vmalloc(buf_len
);
962 buf2
= krealloc(buf
, buf_len
, GFP_NOFS
);
964 buf2
= vmalloc(buf_len
);
978 read_extent_buffer(eb
, buf
, (unsigned long)(di
+ 1),
979 name_len
+ data_len
);
981 len
= sizeof(*di
) + name_len
+ data_len
;
982 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
985 ret
= iterate(num
, &di_key
, buf
, name_len
, buf
+ name_len
,
986 data_len
, type
, ctx
);
1005 static int __copy_first_ref(int num
, u64 dir
, int index
,
1006 struct fs_path
*p
, void *ctx
)
1009 struct fs_path
*pt
= ctx
;
1011 ret
= fs_path_copy(pt
, p
);
1015 /* we want the first only */
1020 * Retrieve the first path of an inode. If an inode has more then one
1021 * ref/hardlink, this is ignored.
1023 static int get_inode_path(struct btrfs_root
*root
,
1024 u64 ino
, struct fs_path
*path
)
1027 struct btrfs_key key
, found_key
;
1028 struct btrfs_path
*p
;
1030 p
= alloc_path_for_send();
1034 fs_path_reset(path
);
1037 key
.type
= BTRFS_INODE_REF_KEY
;
1040 ret
= btrfs_search_slot_for_read(root
, &key
, p
, 1, 0);
1047 btrfs_item_key_to_cpu(p
->nodes
[0], &found_key
, p
->slots
[0]);
1048 if (found_key
.objectid
!= ino
||
1049 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1050 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1055 ret
= iterate_inode_ref(root
, p
, &found_key
, 1,
1056 __copy_first_ref
, path
);
1066 struct backref_ctx
{
1067 struct send_ctx
*sctx
;
1069 /* number of total found references */
1073 * used for clones found in send_root. clones found behind cur_objectid
1074 * and cur_offset are not considered as allowed clones.
1079 /* may be truncated in case it's the last extent in a file */
1082 /* Just to check for bugs in backref resolving */
1086 static int __clone_root_cmp_bsearch(const void *key
, const void *elt
)
1088 u64 root
= (u64
)(uintptr_t)key
;
1089 struct clone_root
*cr
= (struct clone_root
*)elt
;
1091 if (root
< cr
->root
->objectid
)
1093 if (root
> cr
->root
->objectid
)
1098 static int __clone_root_cmp_sort(const void *e1
, const void *e2
)
1100 struct clone_root
*cr1
= (struct clone_root
*)e1
;
1101 struct clone_root
*cr2
= (struct clone_root
*)e2
;
1103 if (cr1
->root
->objectid
< cr2
->root
->objectid
)
1105 if (cr1
->root
->objectid
> cr2
->root
->objectid
)
1111 * Called for every backref that is found for the current extent.
1112 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1114 static int __iterate_backrefs(u64 ino
, u64 offset
, u64 root
, void *ctx_
)
1116 struct backref_ctx
*bctx
= ctx_
;
1117 struct clone_root
*found
;
1121 /* First check if the root is in the list of accepted clone sources */
1122 found
= bsearch((void *)(uintptr_t)root
, bctx
->sctx
->clone_roots
,
1123 bctx
->sctx
->clone_roots_cnt
,
1124 sizeof(struct clone_root
),
1125 __clone_root_cmp_bsearch
);
1129 if (found
->root
== bctx
->sctx
->send_root
&&
1130 ino
== bctx
->cur_objectid
&&
1131 offset
== bctx
->cur_offset
) {
1132 bctx
->found_itself
= 1;
1136 * There are inodes that have extents that lie behind its i_size. Don't
1137 * accept clones from these extents.
1139 ret
= get_inode_info(found
->root
, ino
, &i_size
, NULL
, NULL
, NULL
, NULL
,
1144 if (offset
+ bctx
->extent_len
> i_size
)
1148 * Make sure we don't consider clones from send_root that are
1149 * behind the current inode/offset.
1151 if (found
->root
== bctx
->sctx
->send_root
) {
1153 * TODO for the moment we don't accept clones from the inode
1154 * that is currently send. We may change this when
1155 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1158 if (ino
>= bctx
->cur_objectid
)
1161 if (ino
> bctx
->cur_objectid
)
1163 if (offset
+ bctx
->extent_len
> bctx
->cur_offset
)
1169 found
->found_refs
++;
1170 if (ino
< found
->ino
) {
1172 found
->offset
= offset
;
1173 } else if (found
->ino
== ino
) {
1175 * same extent found more then once in the same file.
1177 if (found
->offset
> offset
+ bctx
->extent_len
)
1178 found
->offset
= offset
;
1185 * Given an inode, offset and extent item, it finds a good clone for a clone
1186 * instruction. Returns -ENOENT when none could be found. The function makes
1187 * sure that the returned clone is usable at the point where sending is at the
1188 * moment. This means, that no clones are accepted which lie behind the current
1191 * path must point to the extent item when called.
1193 static int find_extent_clone(struct send_ctx
*sctx
,
1194 struct btrfs_path
*path
,
1195 u64 ino
, u64 data_offset
,
1197 struct clone_root
**found
)
1204 u64 extent_item_pos
;
1206 struct btrfs_file_extent_item
*fi
;
1207 struct extent_buffer
*eb
= path
->nodes
[0];
1208 struct backref_ctx
*backref_ctx
= NULL
;
1209 struct clone_root
*cur_clone_root
;
1210 struct btrfs_key found_key
;
1211 struct btrfs_path
*tmp_path
;
1215 tmp_path
= alloc_path_for_send();
1219 backref_ctx
= kmalloc(sizeof(*backref_ctx
), GFP_NOFS
);
1225 if (data_offset
>= ino_size
) {
1227 * There may be extents that lie behind the file's size.
1228 * I at least had this in combination with snapshotting while
1229 * writing large files.
1235 fi
= btrfs_item_ptr(eb
, path
->slots
[0],
1236 struct btrfs_file_extent_item
);
1237 extent_type
= btrfs_file_extent_type(eb
, fi
);
1238 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1242 compressed
= btrfs_file_extent_compression(eb
, fi
);
1244 num_bytes
= btrfs_file_extent_num_bytes(eb
, fi
);
1245 disk_byte
= btrfs_file_extent_disk_bytenr(eb
, fi
);
1246 if (disk_byte
== 0) {
1250 logical
= disk_byte
+ btrfs_file_extent_offset(eb
, fi
);
1252 ret
= extent_from_logical(sctx
->send_root
->fs_info
, disk_byte
, tmp_path
,
1253 &found_key
, &flags
);
1254 btrfs_release_path(tmp_path
);
1258 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1264 * Setup the clone roots.
1266 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1267 cur_clone_root
= sctx
->clone_roots
+ i
;
1268 cur_clone_root
->ino
= (u64
)-1;
1269 cur_clone_root
->offset
= 0;
1270 cur_clone_root
->found_refs
= 0;
1273 backref_ctx
->sctx
= sctx
;
1274 backref_ctx
->found
= 0;
1275 backref_ctx
->cur_objectid
= ino
;
1276 backref_ctx
->cur_offset
= data_offset
;
1277 backref_ctx
->found_itself
= 0;
1278 backref_ctx
->extent_len
= num_bytes
;
1281 * The last extent of a file may be too large due to page alignment.
1282 * We need to adjust extent_len in this case so that the checks in
1283 * __iterate_backrefs work.
1285 if (data_offset
+ num_bytes
>= ino_size
)
1286 backref_ctx
->extent_len
= ino_size
- data_offset
;
1289 * Now collect all backrefs.
1291 if (compressed
== BTRFS_COMPRESS_NONE
)
1292 extent_item_pos
= logical
- found_key
.objectid
;
1294 extent_item_pos
= 0;
1296 extent_item_pos
= logical
- found_key
.objectid
;
1297 ret
= iterate_extent_inodes(sctx
->send_root
->fs_info
,
1298 found_key
.objectid
, extent_item_pos
, 1,
1299 __iterate_backrefs
, backref_ctx
);
1304 if (!backref_ctx
->found_itself
) {
1305 /* found a bug in backref code? */
1307 btrfs_err(sctx
->send_root
->fs_info
, "did not find backref in "
1308 "send_root. inode=%llu, offset=%llu, "
1309 "disk_byte=%llu found extent=%llu\n",
1310 ino
, data_offset
, disk_byte
, found_key
.objectid
);
1314 verbose_printk(KERN_DEBUG
"btrfs: find_extent_clone: data_offset=%llu, "
1316 "num_bytes=%llu, logical=%llu\n",
1317 data_offset
, ino
, num_bytes
, logical
);
1319 if (!backref_ctx
->found
)
1320 verbose_printk("btrfs: no clones found\n");
1322 cur_clone_root
= NULL
;
1323 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1324 if (sctx
->clone_roots
[i
].found_refs
) {
1325 if (!cur_clone_root
)
1326 cur_clone_root
= sctx
->clone_roots
+ i
;
1327 else if (sctx
->clone_roots
[i
].root
== sctx
->send_root
)
1328 /* prefer clones from send_root over others */
1329 cur_clone_root
= sctx
->clone_roots
+ i
;
1334 if (cur_clone_root
) {
1335 if (compressed
!= BTRFS_COMPRESS_NONE
) {
1337 * Offsets given by iterate_extent_inodes() are relative
1338 * to the start of the extent, we need to add logical
1339 * offset from the file extent item.
1340 * (See why at backref.c:check_extent_in_eb())
1342 cur_clone_root
->offset
+= btrfs_file_extent_offset(eb
,
1345 *found
= cur_clone_root
;
1352 btrfs_free_path(tmp_path
);
1357 static int read_symlink(struct btrfs_root
*root
,
1359 struct fs_path
*dest
)
1362 struct btrfs_path
*path
;
1363 struct btrfs_key key
;
1364 struct btrfs_file_extent_item
*ei
;
1370 path
= alloc_path_for_send();
1375 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1377 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1382 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1383 struct btrfs_file_extent_item
);
1384 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
1385 compression
= btrfs_file_extent_compression(path
->nodes
[0], ei
);
1386 BUG_ON(type
!= BTRFS_FILE_EXTENT_INLINE
);
1387 BUG_ON(compression
);
1389 off
= btrfs_file_extent_inline_start(ei
);
1390 len
= btrfs_file_extent_inline_len(path
->nodes
[0], path
->slots
[0], ei
);
1392 ret
= fs_path_add_from_extent_buffer(dest
, path
->nodes
[0], off
, len
);
1395 btrfs_free_path(path
);
1400 * Helper function to generate a file name that is unique in the root of
1401 * send_root and parent_root. This is used to generate names for orphan inodes.
1403 static int gen_unique_name(struct send_ctx
*sctx
,
1405 struct fs_path
*dest
)
1408 struct btrfs_path
*path
;
1409 struct btrfs_dir_item
*di
;
1414 path
= alloc_path_for_send();
1419 len
= snprintf(tmp
, sizeof(tmp
), "o%llu-%llu-%llu",
1421 if (len
>= sizeof(tmp
)) {
1422 /* should really not happen */
1427 di
= btrfs_lookup_dir_item(NULL
, sctx
->send_root
,
1428 path
, BTRFS_FIRST_FREE_OBJECTID
,
1429 tmp
, strlen(tmp
), 0);
1430 btrfs_release_path(path
);
1436 /* not unique, try again */
1441 if (!sctx
->parent_root
) {
1447 di
= btrfs_lookup_dir_item(NULL
, sctx
->parent_root
,
1448 path
, BTRFS_FIRST_FREE_OBJECTID
,
1449 tmp
, strlen(tmp
), 0);
1450 btrfs_release_path(path
);
1456 /* not unique, try again */
1464 ret
= fs_path_add(dest
, tmp
, strlen(tmp
));
1467 btrfs_free_path(path
);
1472 inode_state_no_change
,
1473 inode_state_will_create
,
1474 inode_state_did_create
,
1475 inode_state_will_delete
,
1476 inode_state_did_delete
,
1479 static int get_cur_inode_state(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1487 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &left_gen
, NULL
, NULL
,
1489 if (ret
< 0 && ret
!= -ENOENT
)
1493 if (!sctx
->parent_root
) {
1494 right_ret
= -ENOENT
;
1496 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
, &right_gen
,
1497 NULL
, NULL
, NULL
, NULL
);
1498 if (ret
< 0 && ret
!= -ENOENT
)
1503 if (!left_ret
&& !right_ret
) {
1504 if (left_gen
== gen
&& right_gen
== gen
) {
1505 ret
= inode_state_no_change
;
1506 } else if (left_gen
== gen
) {
1507 if (ino
< sctx
->send_progress
)
1508 ret
= inode_state_did_create
;
1510 ret
= inode_state_will_create
;
1511 } else if (right_gen
== gen
) {
1512 if (ino
< sctx
->send_progress
)
1513 ret
= inode_state_did_delete
;
1515 ret
= inode_state_will_delete
;
1519 } else if (!left_ret
) {
1520 if (left_gen
== gen
) {
1521 if (ino
< sctx
->send_progress
)
1522 ret
= inode_state_did_create
;
1524 ret
= inode_state_will_create
;
1528 } else if (!right_ret
) {
1529 if (right_gen
== gen
) {
1530 if (ino
< sctx
->send_progress
)
1531 ret
= inode_state_did_delete
;
1533 ret
= inode_state_will_delete
;
1545 static int is_inode_existent(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1549 ret
= get_cur_inode_state(sctx
, ino
, gen
);
1553 if (ret
== inode_state_no_change
||
1554 ret
== inode_state_did_create
||
1555 ret
== inode_state_will_delete
)
1565 * Helper function to lookup a dir item in a dir.
1567 static int lookup_dir_item_inode(struct btrfs_root
*root
,
1568 u64 dir
, const char *name
, int name_len
,
1573 struct btrfs_dir_item
*di
;
1574 struct btrfs_key key
;
1575 struct btrfs_path
*path
;
1577 path
= alloc_path_for_send();
1581 di
= btrfs_lookup_dir_item(NULL
, root
, path
,
1582 dir
, name
, name_len
, 0);
1591 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1592 *found_inode
= key
.objectid
;
1593 *found_type
= btrfs_dir_type(path
->nodes
[0], di
);
1596 btrfs_free_path(path
);
1601 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1602 * generation of the parent dir and the name of the dir entry.
1604 static int get_first_ref(struct btrfs_root
*root
, u64 ino
,
1605 u64
*dir
, u64
*dir_gen
, struct fs_path
*name
)
1608 struct btrfs_key key
;
1609 struct btrfs_key found_key
;
1610 struct btrfs_path
*path
;
1614 path
= alloc_path_for_send();
1619 key
.type
= BTRFS_INODE_REF_KEY
;
1622 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
1626 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1628 if (ret
|| found_key
.objectid
!= ino
||
1629 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1630 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1635 if (key
.type
== BTRFS_INODE_REF_KEY
) {
1636 struct btrfs_inode_ref
*iref
;
1637 iref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1638 struct btrfs_inode_ref
);
1639 len
= btrfs_inode_ref_name_len(path
->nodes
[0], iref
);
1640 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1641 (unsigned long)(iref
+ 1),
1643 parent_dir
= found_key
.offset
;
1645 struct btrfs_inode_extref
*extref
;
1646 extref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1647 struct btrfs_inode_extref
);
1648 len
= btrfs_inode_extref_name_len(path
->nodes
[0], extref
);
1649 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1650 (unsigned long)&extref
->name
, len
);
1651 parent_dir
= btrfs_inode_extref_parent(path
->nodes
[0], extref
);
1655 btrfs_release_path(path
);
1657 ret
= get_inode_info(root
, parent_dir
, NULL
, dir_gen
, NULL
, NULL
,
1665 btrfs_free_path(path
);
1669 static int is_first_ref(struct btrfs_root
*root
,
1671 const char *name
, int name_len
)
1674 struct fs_path
*tmp_name
;
1678 tmp_name
= fs_path_alloc();
1682 ret
= get_first_ref(root
, ino
, &tmp_dir
, &tmp_dir_gen
, tmp_name
);
1686 if (dir
!= tmp_dir
|| name_len
!= fs_path_len(tmp_name
)) {
1691 ret
= !memcmp(tmp_name
->start
, name
, name_len
);
1694 fs_path_free(tmp_name
);
1699 * Used by process_recorded_refs to determine if a new ref would overwrite an
1700 * already existing ref. In case it detects an overwrite, it returns the
1701 * inode/gen in who_ino/who_gen.
1702 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1703 * to make sure later references to the overwritten inode are possible.
1704 * Orphanizing is however only required for the first ref of an inode.
1705 * process_recorded_refs does an additional is_first_ref check to see if
1706 * orphanizing is really required.
1708 static int will_overwrite_ref(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
1709 const char *name
, int name_len
,
1710 u64
*who_ino
, u64
*who_gen
)
1714 u64 other_inode
= 0;
1717 if (!sctx
->parent_root
)
1720 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1725 * If we have a parent root we need to verify that the parent dir was
1726 * not delted and then re-created, if it was then we have no overwrite
1727 * and we can just unlink this entry.
1729 if (sctx
->parent_root
) {
1730 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &gen
, NULL
,
1732 if (ret
< 0 && ret
!= -ENOENT
)
1742 ret
= lookup_dir_item_inode(sctx
->parent_root
, dir
, name
, name_len
,
1743 &other_inode
, &other_type
);
1744 if (ret
< 0 && ret
!= -ENOENT
)
1752 * Check if the overwritten ref was already processed. If yes, the ref
1753 * was already unlinked/moved, so we can safely assume that we will not
1754 * overwrite anything at this point in time.
1756 if (other_inode
> sctx
->send_progress
) {
1757 ret
= get_inode_info(sctx
->parent_root
, other_inode
, NULL
,
1758 who_gen
, NULL
, NULL
, NULL
, NULL
);
1763 *who_ino
= other_inode
;
1773 * Checks if the ref was overwritten by an already processed inode. This is
1774 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1775 * thus the orphan name needs be used.
1776 * process_recorded_refs also uses it to avoid unlinking of refs that were
1779 static int did_overwrite_ref(struct send_ctx
*sctx
,
1780 u64 dir
, u64 dir_gen
,
1781 u64 ino
, u64 ino_gen
,
1782 const char *name
, int name_len
)
1789 if (!sctx
->parent_root
)
1792 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1796 /* check if the ref was overwritten by another ref */
1797 ret
= lookup_dir_item_inode(sctx
->send_root
, dir
, name
, name_len
,
1798 &ow_inode
, &other_type
);
1799 if (ret
< 0 && ret
!= -ENOENT
)
1802 /* was never and will never be overwritten */
1807 ret
= get_inode_info(sctx
->send_root
, ow_inode
, NULL
, &gen
, NULL
, NULL
,
1812 if (ow_inode
== ino
&& gen
== ino_gen
) {
1817 /* we know that it is or will be overwritten. check this now */
1818 if (ow_inode
< sctx
->send_progress
)
1828 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1829 * that got overwritten. This is used by process_recorded_refs to determine
1830 * if it has to use the path as returned by get_cur_path or the orphan name.
1832 static int did_overwrite_first_ref(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1835 struct fs_path
*name
= NULL
;
1839 if (!sctx
->parent_root
)
1842 name
= fs_path_alloc();
1846 ret
= get_first_ref(sctx
->parent_root
, ino
, &dir
, &dir_gen
, name
);
1850 ret
= did_overwrite_ref(sctx
, dir
, dir_gen
, ino
, gen
,
1851 name
->start
, fs_path_len(name
));
1859 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1860 * so we need to do some special handling in case we have clashes. This function
1861 * takes care of this with the help of name_cache_entry::radix_list.
1862 * In case of error, nce is kfreed.
1864 static int name_cache_insert(struct send_ctx
*sctx
,
1865 struct name_cache_entry
*nce
)
1868 struct list_head
*nce_head
;
1870 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1871 (unsigned long)nce
->ino
);
1873 nce_head
= kmalloc(sizeof(*nce_head
), GFP_NOFS
);
1878 INIT_LIST_HEAD(nce_head
);
1880 ret
= radix_tree_insert(&sctx
->name_cache
, nce
->ino
, nce_head
);
1887 list_add_tail(&nce
->radix_list
, nce_head
);
1888 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
1889 sctx
->name_cache_size
++;
1894 static void name_cache_delete(struct send_ctx
*sctx
,
1895 struct name_cache_entry
*nce
)
1897 struct list_head
*nce_head
;
1899 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1900 (unsigned long)nce
->ino
);
1903 list_del(&nce
->radix_list
);
1904 list_del(&nce
->list
);
1905 sctx
->name_cache_size
--;
1907 if (list_empty(nce_head
)) {
1908 radix_tree_delete(&sctx
->name_cache
, (unsigned long)nce
->ino
);
1913 static struct name_cache_entry
*name_cache_search(struct send_ctx
*sctx
,
1916 struct list_head
*nce_head
;
1917 struct name_cache_entry
*cur
;
1919 nce_head
= radix_tree_lookup(&sctx
->name_cache
, (unsigned long)ino
);
1923 list_for_each_entry(cur
, nce_head
, radix_list
) {
1924 if (cur
->ino
== ino
&& cur
->gen
== gen
)
1931 * Removes the entry from the list and adds it back to the end. This marks the
1932 * entry as recently used so that name_cache_clean_unused does not remove it.
1934 static void name_cache_used(struct send_ctx
*sctx
, struct name_cache_entry
*nce
)
1936 list_del(&nce
->list
);
1937 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
1941 * Remove some entries from the beginning of name_cache_list.
1943 static void name_cache_clean_unused(struct send_ctx
*sctx
)
1945 struct name_cache_entry
*nce
;
1947 if (sctx
->name_cache_size
< SEND_CTX_NAME_CACHE_CLEAN_SIZE
)
1950 while (sctx
->name_cache_size
> SEND_CTX_MAX_NAME_CACHE_SIZE
) {
1951 nce
= list_entry(sctx
->name_cache_list
.next
,
1952 struct name_cache_entry
, list
);
1953 name_cache_delete(sctx
, nce
);
1958 static void name_cache_free(struct send_ctx
*sctx
)
1960 struct name_cache_entry
*nce
;
1962 while (!list_empty(&sctx
->name_cache_list
)) {
1963 nce
= list_entry(sctx
->name_cache_list
.next
,
1964 struct name_cache_entry
, list
);
1965 name_cache_delete(sctx
, nce
);
1971 * Used by get_cur_path for each ref up to the root.
1972 * Returns 0 if it succeeded.
1973 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1974 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1975 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1976 * Returns <0 in case of error.
1978 static int __get_cur_name_and_parent(struct send_ctx
*sctx
,
1980 int skip_name_cache
,
1983 struct fs_path
*dest
)
1987 struct btrfs_path
*path
= NULL
;
1988 struct name_cache_entry
*nce
= NULL
;
1990 if (skip_name_cache
)
1993 * First check if we already did a call to this function with the same
1994 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1995 * return the cached result.
1997 nce
= name_cache_search(sctx
, ino
, gen
);
1999 if (ino
< sctx
->send_progress
&& nce
->need_later_update
) {
2000 name_cache_delete(sctx
, nce
);
2004 name_cache_used(sctx
, nce
);
2005 *parent_ino
= nce
->parent_ino
;
2006 *parent_gen
= nce
->parent_gen
;
2007 ret
= fs_path_add(dest
, nce
->name
, nce
->name_len
);
2015 path
= alloc_path_for_send();
2020 * If the inode is not existent yet, add the orphan name and return 1.
2021 * This should only happen for the parent dir that we determine in
2024 ret
= is_inode_existent(sctx
, ino
, gen
);
2029 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2038 * Depending on whether the inode was already processed or not, use
2039 * send_root or parent_root for ref lookup.
2041 if (ino
< sctx
->send_progress
&& !skip_name_cache
)
2042 ret
= get_first_ref(sctx
->send_root
, ino
,
2043 parent_ino
, parent_gen
, dest
);
2045 ret
= get_first_ref(sctx
->parent_root
, ino
,
2046 parent_ino
, parent_gen
, dest
);
2051 * Check if the ref was overwritten by an inode's ref that was processed
2052 * earlier. If yes, treat as orphan and return 1.
2054 ret
= did_overwrite_ref(sctx
, *parent_ino
, *parent_gen
, ino
, gen
,
2055 dest
->start
, dest
->end
- dest
->start
);
2059 fs_path_reset(dest
);
2060 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2065 if (skip_name_cache
)
2070 * Store the result of the lookup in the name cache.
2072 nce
= kmalloc(sizeof(*nce
) + fs_path_len(dest
) + 1, GFP_NOFS
);
2080 nce
->parent_ino
= *parent_ino
;
2081 nce
->parent_gen
= *parent_gen
;
2082 nce
->name_len
= fs_path_len(dest
);
2084 strcpy(nce
->name
, dest
->start
);
2086 if (ino
< sctx
->send_progress
)
2087 nce
->need_later_update
= 0;
2089 nce
->need_later_update
= 1;
2091 nce_ret
= name_cache_insert(sctx
, nce
);
2094 name_cache_clean_unused(sctx
);
2097 btrfs_free_path(path
);
2102 * Magic happens here. This function returns the first ref to an inode as it
2103 * would look like while receiving the stream at this point in time.
2104 * We walk the path up to the root. For every inode in between, we check if it
2105 * was already processed/sent. If yes, we continue with the parent as found
2106 * in send_root. If not, we continue with the parent as found in parent_root.
2107 * If we encounter an inode that was deleted at this point in time, we use the
2108 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2109 * that were not created yet and overwritten inodes/refs.
2111 * When do we have have orphan inodes:
2112 * 1. When an inode is freshly created and thus no valid refs are available yet
2113 * 2. When a directory lost all it's refs (deleted) but still has dir items
2114 * inside which were not processed yet (pending for move/delete). If anyone
2115 * tried to get the path to the dir items, it would get a path inside that
2117 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2118 * of an unprocessed inode. If in that case the first ref would be
2119 * overwritten, the overwritten inode gets "orphanized". Later when we
2120 * process this overwritten inode, it is restored at a new place by moving
2123 * sctx->send_progress tells this function at which point in time receiving
2126 static int get_cur_path(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2127 struct fs_path
*dest
)
2130 struct fs_path
*name
= NULL
;
2131 u64 parent_inode
= 0;
2134 u64 start_ino
= ino
;
2135 u64 start_gen
= gen
;
2136 int skip_name_cache
= 0;
2138 name
= fs_path_alloc();
2144 if (is_waiting_for_move(sctx
, ino
))
2145 skip_name_cache
= 1;
2149 fs_path_reset(dest
);
2151 while (!stop
&& ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
2152 fs_path_reset(name
);
2154 ret
= __get_cur_name_and_parent(sctx
, ino
, gen
, skip_name_cache
,
2155 &parent_inode
, &parent_gen
, name
);
2161 if (!skip_name_cache
&&
2162 is_waiting_for_move(sctx
, parent_inode
)) {
2166 skip_name_cache
= 1;
2170 ret
= fs_path_add_path(dest
, name
);
2181 fs_path_unreverse(dest
);
2186 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2188 static int send_subvol_begin(struct send_ctx
*sctx
)
2191 struct btrfs_root
*send_root
= sctx
->send_root
;
2192 struct btrfs_root
*parent_root
= sctx
->parent_root
;
2193 struct btrfs_path
*path
;
2194 struct btrfs_key key
;
2195 struct btrfs_root_ref
*ref
;
2196 struct extent_buffer
*leaf
;
2200 path
= btrfs_alloc_path();
2204 name
= kmalloc(BTRFS_PATH_NAME_MAX
, GFP_NOFS
);
2206 btrfs_free_path(path
);
2210 key
.objectid
= send_root
->objectid
;
2211 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2214 ret
= btrfs_search_slot_for_read(send_root
->fs_info
->tree_root
,
2223 leaf
= path
->nodes
[0];
2224 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2225 if (key
.type
!= BTRFS_ROOT_BACKREF_KEY
||
2226 key
.objectid
!= send_root
->objectid
) {
2230 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
2231 namelen
= btrfs_root_ref_name_len(leaf
, ref
);
2232 read_extent_buffer(leaf
, name
, (unsigned long)(ref
+ 1), namelen
);
2233 btrfs_release_path(path
);
2236 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SNAPSHOT
);
2240 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SUBVOL
);
2245 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_PATH
, name
, namelen
);
2246 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_UUID
,
2247 sctx
->send_root
->root_item
.uuid
);
2248 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CTRANSID
,
2249 le64_to_cpu(sctx
->send_root
->root_item
.ctransid
));
2251 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
2252 sctx
->parent_root
->root_item
.uuid
);
2253 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
2254 le64_to_cpu(sctx
->parent_root
->root_item
.ctransid
));
2257 ret
= send_cmd(sctx
);
2261 btrfs_free_path(path
);
2266 static int send_truncate(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 size
)
2271 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino
, size
);
2273 p
= fs_path_alloc();
2277 ret
= begin_cmd(sctx
, BTRFS_SEND_C_TRUNCATE
);
2281 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2284 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2285 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, size
);
2287 ret
= send_cmd(sctx
);
2295 static int send_chmod(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 mode
)
2300 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino
, mode
);
2302 p
= fs_path_alloc();
2306 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHMOD
);
2310 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2313 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2314 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
& 07777);
2316 ret
= send_cmd(sctx
);
2324 static int send_chown(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 uid
, u64 gid
)
2329 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino
, uid
, gid
);
2331 p
= fs_path_alloc();
2335 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHOWN
);
2339 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2342 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2343 TLV_PUT_U64(sctx
, BTRFS_SEND_A_UID
, uid
);
2344 TLV_PUT_U64(sctx
, BTRFS_SEND_A_GID
, gid
);
2346 ret
= send_cmd(sctx
);
2354 static int send_utimes(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
2357 struct fs_path
*p
= NULL
;
2358 struct btrfs_inode_item
*ii
;
2359 struct btrfs_path
*path
= NULL
;
2360 struct extent_buffer
*eb
;
2361 struct btrfs_key key
;
2364 verbose_printk("btrfs: send_utimes %llu\n", ino
);
2366 p
= fs_path_alloc();
2370 path
= alloc_path_for_send();
2377 key
.type
= BTRFS_INODE_ITEM_KEY
;
2379 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2383 eb
= path
->nodes
[0];
2384 slot
= path
->slots
[0];
2385 ii
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
2387 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UTIMES
);
2391 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2394 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2395 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_ATIME
, eb
,
2396 btrfs_inode_atime(ii
));
2397 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_MTIME
, eb
,
2398 btrfs_inode_mtime(ii
));
2399 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_CTIME
, eb
,
2400 btrfs_inode_ctime(ii
));
2401 /* TODO Add otime support when the otime patches get into upstream */
2403 ret
= send_cmd(sctx
);
2408 btrfs_free_path(path
);
2413 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2414 * a valid path yet because we did not process the refs yet. So, the inode
2415 * is created as orphan.
2417 static int send_create_inode(struct send_ctx
*sctx
, u64 ino
)
2426 verbose_printk("btrfs: send_create_inode %llu\n", ino
);
2428 p
= fs_path_alloc();
2432 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &gen
, &mode
, NULL
,
2437 if (S_ISREG(mode
)) {
2438 cmd
= BTRFS_SEND_C_MKFILE
;
2439 } else if (S_ISDIR(mode
)) {
2440 cmd
= BTRFS_SEND_C_MKDIR
;
2441 } else if (S_ISLNK(mode
)) {
2442 cmd
= BTRFS_SEND_C_SYMLINK
;
2443 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
2444 cmd
= BTRFS_SEND_C_MKNOD
;
2445 } else if (S_ISFIFO(mode
)) {
2446 cmd
= BTRFS_SEND_C_MKFIFO
;
2447 } else if (S_ISSOCK(mode
)) {
2448 cmd
= BTRFS_SEND_C_MKSOCK
;
2450 printk(KERN_WARNING
"btrfs: unexpected inode type %o",
2451 (int)(mode
& S_IFMT
));
2456 ret
= begin_cmd(sctx
, cmd
);
2460 ret
= gen_unique_name(sctx
, ino
, gen
, p
);
2464 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2465 TLV_PUT_U64(sctx
, BTRFS_SEND_A_INO
, ino
);
2467 if (S_ISLNK(mode
)) {
2469 ret
= read_symlink(sctx
->send_root
, ino
, p
);
2472 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, p
);
2473 } else if (S_ISCHR(mode
) || S_ISBLK(mode
) ||
2474 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
2475 TLV_PUT_U64(sctx
, BTRFS_SEND_A_RDEV
, new_encode_dev(rdev
));
2476 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
);
2479 ret
= send_cmd(sctx
);
2491 * We need some special handling for inodes that get processed before the parent
2492 * directory got created. See process_recorded_refs for details.
2493 * This function does the check if we already created the dir out of order.
2495 static int did_create_dir(struct send_ctx
*sctx
, u64 dir
)
2498 struct btrfs_path
*path
= NULL
;
2499 struct btrfs_key key
;
2500 struct btrfs_key found_key
;
2501 struct btrfs_key di_key
;
2502 struct extent_buffer
*eb
;
2503 struct btrfs_dir_item
*di
;
2506 path
= alloc_path_for_send();
2513 key
.type
= BTRFS_DIR_INDEX_KEY
;
2516 ret
= btrfs_search_slot_for_read(sctx
->send_root
, &key
, path
,
2521 eb
= path
->nodes
[0];
2522 slot
= path
->slots
[0];
2523 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
2525 if (ret
|| found_key
.objectid
!= key
.objectid
||
2526 found_key
.type
!= key
.type
) {
2531 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
2532 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
2534 if (di_key
.type
!= BTRFS_ROOT_ITEM_KEY
&&
2535 di_key
.objectid
< sctx
->send_progress
) {
2540 key
.offset
= found_key
.offset
+ 1;
2541 btrfs_release_path(path
);
2545 btrfs_free_path(path
);
2550 * Only creates the inode if it is:
2551 * 1. Not a directory
2552 * 2. Or a directory which was not created already due to out of order
2553 * directories. See did_create_dir and process_recorded_refs for details.
2555 static int send_create_inode_if_needed(struct send_ctx
*sctx
)
2559 if (S_ISDIR(sctx
->cur_inode_mode
)) {
2560 ret
= did_create_dir(sctx
, sctx
->cur_ino
);
2569 ret
= send_create_inode(sctx
, sctx
->cur_ino
);
2577 struct recorded_ref
{
2578 struct list_head list
;
2581 struct fs_path
*full_path
;
2589 * We need to process new refs before deleted refs, but compare_tree gives us
2590 * everything mixed. So we first record all refs and later process them.
2591 * This function is a helper to record one ref.
2593 static int record_ref(struct list_head
*head
, u64 dir
,
2594 u64 dir_gen
, struct fs_path
*path
)
2596 struct recorded_ref
*ref
;
2598 ref
= kmalloc(sizeof(*ref
), GFP_NOFS
);
2603 ref
->dir_gen
= dir_gen
;
2604 ref
->full_path
= path
;
2606 ref
->name
= (char *)kbasename(ref
->full_path
->start
);
2607 ref
->name_len
= ref
->full_path
->end
- ref
->name
;
2608 ref
->dir_path
= ref
->full_path
->start
;
2609 if (ref
->name
== ref
->full_path
->start
)
2610 ref
->dir_path_len
= 0;
2612 ref
->dir_path_len
= ref
->full_path
->end
-
2613 ref
->full_path
->start
- 1 - ref
->name_len
;
2615 list_add_tail(&ref
->list
, head
);
2619 static int dup_ref(struct recorded_ref
*ref
, struct list_head
*list
)
2621 struct recorded_ref
*new;
2623 new = kmalloc(sizeof(*ref
), GFP_NOFS
);
2627 new->dir
= ref
->dir
;
2628 new->dir_gen
= ref
->dir_gen
;
2629 new->full_path
= NULL
;
2630 INIT_LIST_HEAD(&new->list
);
2631 list_add_tail(&new->list
, list
);
2635 static void __free_recorded_refs(struct list_head
*head
)
2637 struct recorded_ref
*cur
;
2639 while (!list_empty(head
)) {
2640 cur
= list_entry(head
->next
, struct recorded_ref
, list
);
2641 fs_path_free(cur
->full_path
);
2642 list_del(&cur
->list
);
2647 static void free_recorded_refs(struct send_ctx
*sctx
)
2649 __free_recorded_refs(&sctx
->new_refs
);
2650 __free_recorded_refs(&sctx
->deleted_refs
);
2654 * Renames/moves a file/dir to its orphan name. Used when the first
2655 * ref of an unprocessed inode gets overwritten and for all non empty
2658 static int orphanize_inode(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2659 struct fs_path
*path
)
2662 struct fs_path
*orphan
;
2664 orphan
= fs_path_alloc();
2668 ret
= gen_unique_name(sctx
, ino
, gen
, orphan
);
2672 ret
= send_rename(sctx
, path
, orphan
);
2675 fs_path_free(orphan
);
2680 * Returns 1 if a directory can be removed at this point in time.
2681 * We check this by iterating all dir items and checking if the inode behind
2682 * the dir item was already processed.
2684 static int can_rmdir(struct send_ctx
*sctx
, u64 dir
, u64 send_progress
)
2687 struct btrfs_root
*root
= sctx
->parent_root
;
2688 struct btrfs_path
*path
;
2689 struct btrfs_key key
;
2690 struct btrfs_key found_key
;
2691 struct btrfs_key loc
;
2692 struct btrfs_dir_item
*di
;
2695 * Don't try to rmdir the top/root subvolume dir.
2697 if (dir
== BTRFS_FIRST_FREE_OBJECTID
)
2700 path
= alloc_path_for_send();
2705 key
.type
= BTRFS_DIR_INDEX_KEY
;
2709 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
2713 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2716 if (ret
|| found_key
.objectid
!= key
.objectid
||
2717 found_key
.type
!= key
.type
) {
2721 di
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2722 struct btrfs_dir_item
);
2723 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &loc
);
2725 if (loc
.objectid
> send_progress
) {
2730 btrfs_release_path(path
);
2731 key
.offset
= found_key
.offset
+ 1;
2737 btrfs_free_path(path
);
2741 static int is_waiting_for_move(struct send_ctx
*sctx
, u64 ino
)
2743 struct rb_node
*n
= sctx
->waiting_dir_moves
.rb_node
;
2744 struct waiting_dir_move
*entry
;
2747 entry
= rb_entry(n
, struct waiting_dir_move
, node
);
2748 if (ino
< entry
->ino
)
2750 else if (ino
> entry
->ino
)
2758 static int add_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
)
2760 struct rb_node
**p
= &sctx
->waiting_dir_moves
.rb_node
;
2761 struct rb_node
*parent
= NULL
;
2762 struct waiting_dir_move
*entry
, *dm
;
2764 dm
= kmalloc(sizeof(*dm
), GFP_NOFS
);
2771 entry
= rb_entry(parent
, struct waiting_dir_move
, node
);
2772 if (ino
< entry
->ino
) {
2774 } else if (ino
> entry
->ino
) {
2775 p
= &(*p
)->rb_right
;
2782 rb_link_node(&dm
->node
, parent
, p
);
2783 rb_insert_color(&dm
->node
, &sctx
->waiting_dir_moves
);
2787 static int del_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
)
2789 struct rb_node
*n
= sctx
->waiting_dir_moves
.rb_node
;
2790 struct waiting_dir_move
*entry
;
2793 entry
= rb_entry(n
, struct waiting_dir_move
, node
);
2794 if (ino
< entry
->ino
) {
2796 } else if (ino
> entry
->ino
) {
2799 rb_erase(&entry
->node
, &sctx
->waiting_dir_moves
);
2807 static int add_pending_dir_move(struct send_ctx
*sctx
, u64 parent_ino
)
2809 struct rb_node
**p
= &sctx
->pending_dir_moves
.rb_node
;
2810 struct rb_node
*parent
= NULL
;
2811 struct pending_dir_move
*entry
, *pm
;
2812 struct recorded_ref
*cur
;
2816 pm
= kmalloc(sizeof(*pm
), GFP_NOFS
);
2819 pm
->parent_ino
= parent_ino
;
2820 pm
->ino
= sctx
->cur_ino
;
2821 pm
->gen
= sctx
->cur_inode_gen
;
2822 INIT_LIST_HEAD(&pm
->list
);
2823 INIT_LIST_HEAD(&pm
->update_refs
);
2824 RB_CLEAR_NODE(&pm
->node
);
2828 entry
= rb_entry(parent
, struct pending_dir_move
, node
);
2829 if (parent_ino
< entry
->parent_ino
) {
2831 } else if (parent_ino
> entry
->parent_ino
) {
2832 p
= &(*p
)->rb_right
;
2839 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
2840 ret
= dup_ref(cur
, &pm
->update_refs
);
2844 list_for_each_entry(cur
, &sctx
->new_refs
, list
) {
2845 ret
= dup_ref(cur
, &pm
->update_refs
);
2850 ret
= add_waiting_dir_move(sctx
, pm
->ino
);
2855 list_add_tail(&pm
->list
, &entry
->list
);
2857 rb_link_node(&pm
->node
, parent
, p
);
2858 rb_insert_color(&pm
->node
, &sctx
->pending_dir_moves
);
2863 __free_recorded_refs(&pm
->update_refs
);
2869 static struct pending_dir_move
*get_pending_dir_moves(struct send_ctx
*sctx
,
2872 struct rb_node
*n
= sctx
->pending_dir_moves
.rb_node
;
2873 struct pending_dir_move
*entry
;
2876 entry
= rb_entry(n
, struct pending_dir_move
, node
);
2877 if (parent_ino
< entry
->parent_ino
)
2879 else if (parent_ino
> entry
->parent_ino
)
2887 static int apply_dir_move(struct send_ctx
*sctx
, struct pending_dir_move
*pm
)
2889 struct fs_path
*from_path
= NULL
;
2890 struct fs_path
*to_path
= NULL
;
2891 u64 orig_progress
= sctx
->send_progress
;
2892 struct recorded_ref
*cur
;
2895 from_path
= fs_path_alloc();
2899 sctx
->send_progress
= pm
->ino
;
2900 ret
= get_cur_path(sctx
, pm
->ino
, pm
->gen
, from_path
);
2904 to_path
= fs_path_alloc();
2910 sctx
->send_progress
= sctx
->cur_ino
+ 1;
2911 ret
= del_waiting_dir_move(sctx
, pm
->ino
);
2914 ret
= get_cur_path(sctx
, pm
->ino
, pm
->gen
, to_path
);
2918 ret
= send_rename(sctx
, from_path
, to_path
);
2922 ret
= send_utimes(sctx
, pm
->ino
, pm
->gen
);
2927 * After rename/move, need to update the utimes of both new parent(s)
2928 * and old parent(s).
2930 list_for_each_entry(cur
, &pm
->update_refs
, list
) {
2931 ret
= send_utimes(sctx
, cur
->dir
, cur
->dir_gen
);
2937 fs_path_free(from_path
);
2938 fs_path_free(to_path
);
2939 sctx
->send_progress
= orig_progress
;
2944 static void free_pending_move(struct send_ctx
*sctx
, struct pending_dir_move
*m
)
2946 if (!list_empty(&m
->list
))
2948 if (!RB_EMPTY_NODE(&m
->node
))
2949 rb_erase(&m
->node
, &sctx
->pending_dir_moves
);
2950 __free_recorded_refs(&m
->update_refs
);
2954 static void tail_append_pending_moves(struct pending_dir_move
*moves
,
2955 struct list_head
*stack
)
2957 if (list_empty(&moves
->list
)) {
2958 list_add_tail(&moves
->list
, stack
);
2961 list_splice_init(&moves
->list
, &list
);
2962 list_add_tail(&moves
->list
, stack
);
2963 list_splice_tail(&list
, stack
);
2967 static int apply_children_dir_moves(struct send_ctx
*sctx
)
2969 struct pending_dir_move
*pm
;
2970 struct list_head stack
;
2971 u64 parent_ino
= sctx
->cur_ino
;
2974 pm
= get_pending_dir_moves(sctx
, parent_ino
);
2978 INIT_LIST_HEAD(&stack
);
2979 tail_append_pending_moves(pm
, &stack
);
2981 while (!list_empty(&stack
)) {
2982 pm
= list_first_entry(&stack
, struct pending_dir_move
, list
);
2983 parent_ino
= pm
->ino
;
2984 ret
= apply_dir_move(sctx
, pm
);
2985 free_pending_move(sctx
, pm
);
2988 pm
= get_pending_dir_moves(sctx
, parent_ino
);
2990 tail_append_pending_moves(pm
, &stack
);
2995 while (!list_empty(&stack
)) {
2996 pm
= list_first_entry(&stack
, struct pending_dir_move
, list
);
2997 free_pending_move(sctx
, pm
);
3002 static int wait_for_parent_move(struct send_ctx
*sctx
,
3003 struct recorded_ref
*parent_ref
)
3006 u64 ino
= parent_ref
->dir
;
3007 u64 parent_ino_before
, parent_ino_after
;
3008 u64 new_gen
, old_gen
;
3009 struct fs_path
*path_before
= NULL
;
3010 struct fs_path
*path_after
= NULL
;
3013 if (parent_ref
->dir
<= sctx
->cur_ino
)
3016 if (is_waiting_for_move(sctx
, ino
))
3019 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
, &old_gen
,
3020 NULL
, NULL
, NULL
, NULL
);
3026 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &new_gen
,
3027 NULL
, NULL
, NULL
, NULL
);
3031 if (new_gen
!= old_gen
)
3034 path_before
= fs_path_alloc();
3038 ret
= get_first_ref(sctx
->parent_root
, ino
, &parent_ino_before
,
3040 if (ret
== -ENOENT
) {
3043 } else if (ret
< 0) {
3047 path_after
= fs_path_alloc();
3053 ret
= get_first_ref(sctx
->send_root
, ino
, &parent_ino_after
,
3055 if (ret
== -ENOENT
) {
3058 } else if (ret
< 0) {
3062 len1
= fs_path_len(path_before
);
3063 len2
= fs_path_len(path_after
);
3064 if ((parent_ino_before
!= parent_ino_after
) && (len1
!= len2
||
3065 memcmp(path_before
->start
, path_after
->start
, len1
))) {
3072 fs_path_free(path_before
);
3073 fs_path_free(path_after
);
3079 * This does all the move/link/unlink/rmdir magic.
3081 static int process_recorded_refs(struct send_ctx
*sctx
, int *pending_move
)
3084 struct recorded_ref
*cur
;
3085 struct recorded_ref
*cur2
;
3086 struct list_head check_dirs
;
3087 struct fs_path
*valid_path
= NULL
;
3090 int did_overwrite
= 0;
3093 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx
->cur_ino
);
3096 * This should never happen as the root dir always has the same ref
3097 * which is always '..'
3099 BUG_ON(sctx
->cur_ino
<= BTRFS_FIRST_FREE_OBJECTID
);
3100 INIT_LIST_HEAD(&check_dirs
);
3102 valid_path
= fs_path_alloc();
3109 * First, check if the first ref of the current inode was overwritten
3110 * before. If yes, we know that the current inode was already orphanized
3111 * and thus use the orphan name. If not, we can use get_cur_path to
3112 * get the path of the first ref as it would like while receiving at
3113 * this point in time.
3114 * New inodes are always orphan at the beginning, so force to use the
3115 * orphan name in this case.
3116 * The first ref is stored in valid_path and will be updated if it
3117 * gets moved around.
3119 if (!sctx
->cur_inode_new
) {
3120 ret
= did_overwrite_first_ref(sctx
, sctx
->cur_ino
,
3121 sctx
->cur_inode_gen
);
3127 if (sctx
->cur_inode_new
|| did_overwrite
) {
3128 ret
= gen_unique_name(sctx
, sctx
->cur_ino
,
3129 sctx
->cur_inode_gen
, valid_path
);
3134 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
3140 list_for_each_entry(cur
, &sctx
->new_refs
, list
) {
3142 * We may have refs where the parent directory does not exist
3143 * yet. This happens if the parent directories inum is higher
3144 * the the current inum. To handle this case, we create the
3145 * parent directory out of order. But we need to check if this
3146 * did already happen before due to other refs in the same dir.
3148 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
3151 if (ret
== inode_state_will_create
) {
3154 * First check if any of the current inodes refs did
3155 * already create the dir.
3157 list_for_each_entry(cur2
, &sctx
->new_refs
, list
) {
3160 if (cur2
->dir
== cur
->dir
) {
3167 * If that did not happen, check if a previous inode
3168 * did already create the dir.
3171 ret
= did_create_dir(sctx
, cur
->dir
);
3175 ret
= send_create_inode(sctx
, cur
->dir
);
3182 * Check if this new ref would overwrite the first ref of
3183 * another unprocessed inode. If yes, orphanize the
3184 * overwritten inode. If we find an overwritten ref that is
3185 * not the first ref, simply unlink it.
3187 ret
= will_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
3188 cur
->name
, cur
->name_len
,
3189 &ow_inode
, &ow_gen
);
3193 ret
= is_first_ref(sctx
->parent_root
,
3194 ow_inode
, cur
->dir
, cur
->name
,
3199 ret
= orphanize_inode(sctx
, ow_inode
, ow_gen
,
3204 ret
= send_unlink(sctx
, cur
->full_path
);
3211 * link/move the ref to the new place. If we have an orphan
3212 * inode, move it and update valid_path. If not, link or move
3213 * it depending on the inode mode.
3216 ret
= send_rename(sctx
, valid_path
, cur
->full_path
);
3220 ret
= fs_path_copy(valid_path
, cur
->full_path
);
3224 if (S_ISDIR(sctx
->cur_inode_mode
)) {
3226 * Dirs can't be linked, so move it. For moved
3227 * dirs, we always have one new and one deleted
3228 * ref. The deleted ref is ignored later.
3230 ret
= wait_for_parent_move(sctx
, cur
);
3234 ret
= add_pending_dir_move(sctx
,
3238 ret
= send_rename(sctx
, valid_path
,
3241 ret
= fs_path_copy(valid_path
,
3247 ret
= send_link(sctx
, cur
->full_path
,
3253 ret
= dup_ref(cur
, &check_dirs
);
3258 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->cur_inode_deleted
) {
3260 * Check if we can already rmdir the directory. If not,
3261 * orphanize it. For every dir item inside that gets deleted
3262 * later, we do this check again and rmdir it then if possible.
3263 * See the use of check_dirs for more details.
3265 ret
= can_rmdir(sctx
, sctx
->cur_ino
, sctx
->cur_ino
);
3269 ret
= send_rmdir(sctx
, valid_path
);
3272 } else if (!is_orphan
) {
3273 ret
= orphanize_inode(sctx
, sctx
->cur_ino
,
3274 sctx
->cur_inode_gen
, valid_path
);
3280 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
3281 ret
= dup_ref(cur
, &check_dirs
);
3285 } else if (S_ISDIR(sctx
->cur_inode_mode
) &&
3286 !list_empty(&sctx
->deleted_refs
)) {
3288 * We have a moved dir. Add the old parent to check_dirs
3290 cur
= list_entry(sctx
->deleted_refs
.next
, struct recorded_ref
,
3292 ret
= dup_ref(cur
, &check_dirs
);
3295 } else if (!S_ISDIR(sctx
->cur_inode_mode
)) {
3297 * We have a non dir inode. Go through all deleted refs and
3298 * unlink them if they were not already overwritten by other
3301 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
3302 ret
= did_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
3303 sctx
->cur_ino
, sctx
->cur_inode_gen
,
3304 cur
->name
, cur
->name_len
);
3308 ret
= send_unlink(sctx
, cur
->full_path
);
3312 ret
= dup_ref(cur
, &check_dirs
);
3317 * If the inode is still orphan, unlink the orphan. This may
3318 * happen when a previous inode did overwrite the first ref
3319 * of this inode and no new refs were added for the current
3320 * inode. Unlinking does not mean that the inode is deleted in
3321 * all cases. There may still be links to this inode in other
3325 ret
= send_unlink(sctx
, valid_path
);
3332 * We did collect all parent dirs where cur_inode was once located. We
3333 * now go through all these dirs and check if they are pending for
3334 * deletion and if it's finally possible to perform the rmdir now.
3335 * We also update the inode stats of the parent dirs here.
3337 list_for_each_entry(cur
, &check_dirs
, list
) {
3339 * In case we had refs into dirs that were not processed yet,
3340 * we don't need to do the utime and rmdir logic for these dirs.
3341 * The dir will be processed later.
3343 if (cur
->dir
> sctx
->cur_ino
)
3346 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
3350 if (ret
== inode_state_did_create
||
3351 ret
== inode_state_no_change
) {
3352 /* TODO delayed utimes */
3353 ret
= send_utimes(sctx
, cur
->dir
, cur
->dir_gen
);
3356 } else if (ret
== inode_state_did_delete
) {
3357 ret
= can_rmdir(sctx
, cur
->dir
, sctx
->cur_ino
);
3361 ret
= get_cur_path(sctx
, cur
->dir
,
3362 cur
->dir_gen
, valid_path
);
3365 ret
= send_rmdir(sctx
, valid_path
);
3375 __free_recorded_refs(&check_dirs
);
3376 free_recorded_refs(sctx
);
3377 fs_path_free(valid_path
);
3381 static int __record_new_ref(int num
, u64 dir
, int index
,
3382 struct fs_path
*name
,
3386 struct send_ctx
*sctx
= ctx
;
3390 p
= fs_path_alloc();
3394 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &gen
, NULL
, NULL
,
3399 ret
= get_cur_path(sctx
, dir
, gen
, p
);
3402 ret
= fs_path_add_path(p
, name
);
3406 ret
= record_ref(&sctx
->new_refs
, dir
, gen
, p
);
3414 static int __record_deleted_ref(int num
, u64 dir
, int index
,
3415 struct fs_path
*name
,
3419 struct send_ctx
*sctx
= ctx
;
3423 p
= fs_path_alloc();
3427 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &gen
, NULL
, NULL
,
3432 ret
= get_cur_path(sctx
, dir
, gen
, p
);
3435 ret
= fs_path_add_path(p
, name
);
3439 ret
= record_ref(&sctx
->deleted_refs
, dir
, gen
, p
);
3447 static int record_new_ref(struct send_ctx
*sctx
)
3451 ret
= iterate_inode_ref(sctx
->send_root
, sctx
->left_path
,
3452 sctx
->cmp_key
, 0, __record_new_ref
, sctx
);
3461 static int record_deleted_ref(struct send_ctx
*sctx
)
3465 ret
= iterate_inode_ref(sctx
->parent_root
, sctx
->right_path
,
3466 sctx
->cmp_key
, 0, __record_deleted_ref
, sctx
);
3475 struct find_ref_ctx
{
3478 struct btrfs_root
*root
;
3479 struct fs_path
*name
;
3483 static int __find_iref(int num
, u64 dir
, int index
,
3484 struct fs_path
*name
,
3487 struct find_ref_ctx
*ctx
= ctx_
;
3491 if (dir
== ctx
->dir
&& fs_path_len(name
) == fs_path_len(ctx
->name
) &&
3492 strncmp(name
->start
, ctx
->name
->start
, fs_path_len(name
)) == 0) {
3494 * To avoid doing extra lookups we'll only do this if everything
3497 ret
= get_inode_info(ctx
->root
, dir
, NULL
, &dir_gen
, NULL
,
3501 if (dir_gen
!= ctx
->dir_gen
)
3503 ctx
->found_idx
= num
;
3509 static int find_iref(struct btrfs_root
*root
,
3510 struct btrfs_path
*path
,
3511 struct btrfs_key
*key
,
3512 u64 dir
, u64 dir_gen
, struct fs_path
*name
)
3515 struct find_ref_ctx ctx
;
3519 ctx
.dir_gen
= dir_gen
;
3523 ret
= iterate_inode_ref(root
, path
, key
, 0, __find_iref
, &ctx
);
3527 if (ctx
.found_idx
== -1)
3530 return ctx
.found_idx
;
3533 static int __record_changed_new_ref(int num
, u64 dir
, int index
,
3534 struct fs_path
*name
,
3539 struct send_ctx
*sctx
= ctx
;
3541 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &dir_gen
, NULL
,
3546 ret
= find_iref(sctx
->parent_root
, sctx
->right_path
,
3547 sctx
->cmp_key
, dir
, dir_gen
, name
);
3549 ret
= __record_new_ref(num
, dir
, index
, name
, sctx
);
3556 static int __record_changed_deleted_ref(int num
, u64 dir
, int index
,
3557 struct fs_path
*name
,
3562 struct send_ctx
*sctx
= ctx
;
3564 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &dir_gen
, NULL
,
3569 ret
= find_iref(sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
3570 dir
, dir_gen
, name
);
3572 ret
= __record_deleted_ref(num
, dir
, index
, name
, sctx
);
3579 static int record_changed_ref(struct send_ctx
*sctx
)
3583 ret
= iterate_inode_ref(sctx
->send_root
, sctx
->left_path
,
3584 sctx
->cmp_key
, 0, __record_changed_new_ref
, sctx
);
3587 ret
= iterate_inode_ref(sctx
->parent_root
, sctx
->right_path
,
3588 sctx
->cmp_key
, 0, __record_changed_deleted_ref
, sctx
);
3598 * Record and process all refs at once. Needed when an inode changes the
3599 * generation number, which means that it was deleted and recreated.
3601 static int process_all_refs(struct send_ctx
*sctx
,
3602 enum btrfs_compare_tree_result cmd
)
3605 struct btrfs_root
*root
;
3606 struct btrfs_path
*path
;
3607 struct btrfs_key key
;
3608 struct btrfs_key found_key
;
3609 struct extent_buffer
*eb
;
3611 iterate_inode_ref_t cb
;
3612 int pending_move
= 0;
3614 path
= alloc_path_for_send();
3618 if (cmd
== BTRFS_COMPARE_TREE_NEW
) {
3619 root
= sctx
->send_root
;
3620 cb
= __record_new_ref
;
3621 } else if (cmd
== BTRFS_COMPARE_TREE_DELETED
) {
3622 root
= sctx
->parent_root
;
3623 cb
= __record_deleted_ref
;
3628 key
.objectid
= sctx
->cmp_key
->objectid
;
3629 key
.type
= BTRFS_INODE_REF_KEY
;
3632 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
3638 eb
= path
->nodes
[0];
3639 slot
= path
->slots
[0];
3640 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3642 if (found_key
.objectid
!= key
.objectid
||
3643 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
3644 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
))
3647 ret
= iterate_inode_ref(root
, path
, &found_key
, 0, cb
, sctx
);
3648 btrfs_release_path(path
);
3652 key
.offset
= found_key
.offset
+ 1;
3654 btrfs_release_path(path
);
3656 ret
= process_recorded_refs(sctx
, &pending_move
);
3657 /* Only applicable to an incremental send. */
3658 ASSERT(pending_move
== 0);
3661 btrfs_free_path(path
);
3665 static int send_set_xattr(struct send_ctx
*sctx
,
3666 struct fs_path
*path
,
3667 const char *name
, int name_len
,
3668 const char *data
, int data_len
)
3672 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SET_XATTR
);
3676 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
3677 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
3678 TLV_PUT(sctx
, BTRFS_SEND_A_XATTR_DATA
, data
, data_len
);
3680 ret
= send_cmd(sctx
);
3687 static int send_remove_xattr(struct send_ctx
*sctx
,
3688 struct fs_path
*path
,
3689 const char *name
, int name_len
)
3693 ret
= begin_cmd(sctx
, BTRFS_SEND_C_REMOVE_XATTR
);
3697 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
3698 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
3700 ret
= send_cmd(sctx
);
3707 static int __process_new_xattr(int num
, struct btrfs_key
*di_key
,
3708 const char *name
, int name_len
,
3709 const char *data
, int data_len
,
3713 struct send_ctx
*sctx
= ctx
;
3715 posix_acl_xattr_header dummy_acl
;
3717 p
= fs_path_alloc();
3722 * This hack is needed because empty acl's are stored as zero byte
3723 * data in xattrs. Problem with that is, that receiving these zero byte
3724 * acl's will fail later. To fix this, we send a dummy acl list that
3725 * only contains the version number and no entries.
3727 if (!strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
, name_len
) ||
3728 !strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
, name_len
)) {
3729 if (data_len
== 0) {
3730 dummy_acl
.a_version
=
3731 cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
3732 data
= (char *)&dummy_acl
;
3733 data_len
= sizeof(dummy_acl
);
3737 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3741 ret
= send_set_xattr(sctx
, p
, name
, name_len
, data
, data_len
);
3748 static int __process_deleted_xattr(int num
, struct btrfs_key
*di_key
,
3749 const char *name
, int name_len
,
3750 const char *data
, int data_len
,
3754 struct send_ctx
*sctx
= ctx
;
3757 p
= fs_path_alloc();
3761 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3765 ret
= send_remove_xattr(sctx
, p
, name
, name_len
);
3772 static int process_new_xattr(struct send_ctx
*sctx
)
3776 ret
= iterate_dir_item(sctx
->send_root
, sctx
->left_path
,
3777 sctx
->cmp_key
, __process_new_xattr
, sctx
);
3782 static int process_deleted_xattr(struct send_ctx
*sctx
)
3786 ret
= iterate_dir_item(sctx
->parent_root
, sctx
->right_path
,
3787 sctx
->cmp_key
, __process_deleted_xattr
, sctx
);
3792 struct find_xattr_ctx
{
3800 static int __find_xattr(int num
, struct btrfs_key
*di_key
,
3801 const char *name
, int name_len
,
3802 const char *data
, int data_len
,
3803 u8 type
, void *vctx
)
3805 struct find_xattr_ctx
*ctx
= vctx
;
3807 if (name_len
== ctx
->name_len
&&
3808 strncmp(name
, ctx
->name
, name_len
) == 0) {
3809 ctx
->found_idx
= num
;
3810 ctx
->found_data_len
= data_len
;
3811 ctx
->found_data
= kmemdup(data
, data_len
, GFP_NOFS
);
3812 if (!ctx
->found_data
)
3819 static int find_xattr(struct btrfs_root
*root
,
3820 struct btrfs_path
*path
,
3821 struct btrfs_key
*key
,
3822 const char *name
, int name_len
,
3823 char **data
, int *data_len
)
3826 struct find_xattr_ctx ctx
;
3829 ctx
.name_len
= name_len
;
3831 ctx
.found_data
= NULL
;
3832 ctx
.found_data_len
= 0;
3834 ret
= iterate_dir_item(root
, path
, key
, __find_xattr
, &ctx
);
3838 if (ctx
.found_idx
== -1)
3841 *data
= ctx
.found_data
;
3842 *data_len
= ctx
.found_data_len
;
3844 kfree(ctx
.found_data
);
3846 return ctx
.found_idx
;
3850 static int __process_changed_new_xattr(int num
, struct btrfs_key
*di_key
,
3851 const char *name
, int name_len
,
3852 const char *data
, int data_len
,
3856 struct send_ctx
*sctx
= ctx
;
3857 char *found_data
= NULL
;
3858 int found_data_len
= 0;
3860 ret
= find_xattr(sctx
->parent_root
, sctx
->right_path
,
3861 sctx
->cmp_key
, name
, name_len
, &found_data
,
3863 if (ret
== -ENOENT
) {
3864 ret
= __process_new_xattr(num
, di_key
, name
, name_len
, data
,
3865 data_len
, type
, ctx
);
3866 } else if (ret
>= 0) {
3867 if (data_len
!= found_data_len
||
3868 memcmp(data
, found_data
, data_len
)) {
3869 ret
= __process_new_xattr(num
, di_key
, name
, name_len
,
3870 data
, data_len
, type
, ctx
);
3880 static int __process_changed_deleted_xattr(int num
, struct btrfs_key
*di_key
,
3881 const char *name
, int name_len
,
3882 const char *data
, int data_len
,
3886 struct send_ctx
*sctx
= ctx
;
3888 ret
= find_xattr(sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
3889 name
, name_len
, NULL
, NULL
);
3891 ret
= __process_deleted_xattr(num
, di_key
, name
, name_len
, data
,
3892 data_len
, type
, ctx
);
3899 static int process_changed_xattr(struct send_ctx
*sctx
)
3903 ret
= iterate_dir_item(sctx
->send_root
, sctx
->left_path
,
3904 sctx
->cmp_key
, __process_changed_new_xattr
, sctx
);
3907 ret
= iterate_dir_item(sctx
->parent_root
, sctx
->right_path
,
3908 sctx
->cmp_key
, __process_changed_deleted_xattr
, sctx
);
3914 static int process_all_new_xattrs(struct send_ctx
*sctx
)
3917 struct btrfs_root
*root
;
3918 struct btrfs_path
*path
;
3919 struct btrfs_key key
;
3920 struct btrfs_key found_key
;
3921 struct extent_buffer
*eb
;
3924 path
= alloc_path_for_send();
3928 root
= sctx
->send_root
;
3930 key
.objectid
= sctx
->cmp_key
->objectid
;
3931 key
.type
= BTRFS_XATTR_ITEM_KEY
;
3934 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
3942 eb
= path
->nodes
[0];
3943 slot
= path
->slots
[0];
3944 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3946 if (found_key
.objectid
!= key
.objectid
||
3947 found_key
.type
!= key
.type
) {
3952 ret
= iterate_dir_item(root
, path
, &found_key
,
3953 __process_new_xattr
, sctx
);
3957 btrfs_release_path(path
);
3958 key
.offset
= found_key
.offset
+ 1;
3962 btrfs_free_path(path
);
3966 static ssize_t
fill_read_buf(struct send_ctx
*sctx
, u64 offset
, u32 len
)
3968 struct btrfs_root
*root
= sctx
->send_root
;
3969 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3970 struct inode
*inode
;
3973 struct btrfs_key key
;
3974 pgoff_t index
= offset
>> PAGE_CACHE_SHIFT
;
3976 unsigned pg_offset
= offset
& ~PAGE_CACHE_MASK
;
3979 key
.objectid
= sctx
->cur_ino
;
3980 key
.type
= BTRFS_INODE_ITEM_KEY
;
3983 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
3985 return PTR_ERR(inode
);
3987 if (offset
+ len
> i_size_read(inode
)) {
3988 if (offset
> i_size_read(inode
))
3991 len
= offset
- i_size_read(inode
);
3996 last_index
= (offset
+ len
- 1) >> PAGE_CACHE_SHIFT
;
3997 while (index
<= last_index
) {
3998 unsigned cur_len
= min_t(unsigned, len
,
3999 PAGE_CACHE_SIZE
- pg_offset
);
4000 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
4006 if (!PageUptodate(page
)) {
4007 btrfs_readpage(NULL
, page
);
4009 if (!PageUptodate(page
)) {
4011 page_cache_release(page
);
4018 memcpy(sctx
->read_buf
+ ret
, addr
+ pg_offset
, cur_len
);
4021 page_cache_release(page
);
4033 * Read some bytes from the current inode/file and send a write command to
4036 static int send_write(struct send_ctx
*sctx
, u64 offset
, u32 len
)
4040 ssize_t num_read
= 0;
4042 p
= fs_path_alloc();
4046 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset
, len
);
4048 num_read
= fill_read_buf(sctx
, offset
, len
);
4049 if (num_read
<= 0) {
4055 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
4059 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4063 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4064 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4065 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, num_read
);
4067 ret
= send_cmd(sctx
);
4078 * Send a clone command to user space.
4080 static int send_clone(struct send_ctx
*sctx
,
4081 u64 offset
, u32 len
,
4082 struct clone_root
*clone_root
)
4088 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4089 "clone_inode=%llu, clone_offset=%llu\n", offset
, len
,
4090 clone_root
->root
->objectid
, clone_root
->ino
,
4091 clone_root
->offset
);
4093 p
= fs_path_alloc();
4097 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CLONE
);
4101 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4105 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4106 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_LEN
, len
);
4107 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4109 if (clone_root
->root
== sctx
->send_root
) {
4110 ret
= get_inode_info(sctx
->send_root
, clone_root
->ino
, NULL
,
4111 &gen
, NULL
, NULL
, NULL
, NULL
);
4114 ret
= get_cur_path(sctx
, clone_root
->ino
, gen
, p
);
4116 ret
= get_inode_path(clone_root
->root
, clone_root
->ino
, p
);
4121 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
4122 clone_root
->root
->root_item
.uuid
);
4123 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
4124 le64_to_cpu(clone_root
->root
->root_item
.ctransid
));
4125 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_CLONE_PATH
, p
);
4126 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_OFFSET
,
4127 clone_root
->offset
);
4129 ret
= send_cmd(sctx
);
4138 * Send an update extent command to user space.
4140 static int send_update_extent(struct send_ctx
*sctx
,
4141 u64 offset
, u32 len
)
4146 p
= fs_path_alloc();
4150 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UPDATE_EXTENT
);
4154 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4158 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4159 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4160 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, len
);
4162 ret
= send_cmd(sctx
);
4170 static int send_hole(struct send_ctx
*sctx
, u64 end
)
4172 struct fs_path
*p
= NULL
;
4173 u64 offset
= sctx
->cur_inode_last_extent
;
4177 p
= fs_path_alloc();
4180 memset(sctx
->read_buf
, 0, BTRFS_SEND_READ_SIZE
);
4181 while (offset
< end
) {
4182 len
= min_t(u64
, end
- offset
, BTRFS_SEND_READ_SIZE
);
4184 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
4187 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4190 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4191 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4192 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, len
);
4193 ret
= send_cmd(sctx
);
4203 static int send_write_or_clone(struct send_ctx
*sctx
,
4204 struct btrfs_path
*path
,
4205 struct btrfs_key
*key
,
4206 struct clone_root
*clone_root
)
4209 struct btrfs_file_extent_item
*ei
;
4210 u64 offset
= key
->offset
;
4215 u64 bs
= sctx
->send_root
->fs_info
->sb
->s_blocksize
;
4217 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4218 struct btrfs_file_extent_item
);
4219 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
4220 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4221 len
= btrfs_file_extent_inline_len(path
->nodes
[0],
4222 path
->slots
[0], ei
);
4224 * it is possible the inline item won't cover the whole page,
4225 * but there may be items after this page. Make
4226 * sure to send the whole thing
4228 len
= PAGE_CACHE_ALIGN(len
);
4230 len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
4233 if (offset
+ len
> sctx
->cur_inode_size
)
4234 len
= sctx
->cur_inode_size
- offset
;
4240 if (clone_root
&& IS_ALIGNED(offset
+ len
, bs
)) {
4241 ret
= send_clone(sctx
, offset
, len
, clone_root
);
4242 } else if (sctx
->flags
& BTRFS_SEND_FLAG_NO_FILE_DATA
) {
4243 ret
= send_update_extent(sctx
, offset
, len
);
4247 if (l
> BTRFS_SEND_READ_SIZE
)
4248 l
= BTRFS_SEND_READ_SIZE
;
4249 ret
= send_write(sctx
, pos
+ offset
, l
);
4262 static int is_extent_unchanged(struct send_ctx
*sctx
,
4263 struct btrfs_path
*left_path
,
4264 struct btrfs_key
*ekey
)
4267 struct btrfs_key key
;
4268 struct btrfs_path
*path
= NULL
;
4269 struct extent_buffer
*eb
;
4271 struct btrfs_key found_key
;
4272 struct btrfs_file_extent_item
*ei
;
4277 u64 left_offset_fixed
;
4285 path
= alloc_path_for_send();
4289 eb
= left_path
->nodes
[0];
4290 slot
= left_path
->slots
[0];
4291 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
4292 left_type
= btrfs_file_extent_type(eb
, ei
);
4294 if (left_type
!= BTRFS_FILE_EXTENT_REG
) {
4298 left_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
4299 left_len
= btrfs_file_extent_num_bytes(eb
, ei
);
4300 left_offset
= btrfs_file_extent_offset(eb
, ei
);
4301 left_gen
= btrfs_file_extent_generation(eb
, ei
);
4304 * Following comments will refer to these graphics. L is the left
4305 * extents which we are checking at the moment. 1-8 are the right
4306 * extents that we iterate.
4309 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4312 * |--1--|-2b-|...(same as above)
4314 * Alternative situation. Happens on files where extents got split.
4316 * |-----------7-----------|-6-|
4318 * Alternative situation. Happens on files which got larger.
4321 * Nothing follows after 8.
4324 key
.objectid
= ekey
->objectid
;
4325 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4326 key
.offset
= ekey
->offset
;
4327 ret
= btrfs_search_slot_for_read(sctx
->parent_root
, &key
, path
, 0, 0);
4336 * Handle special case where the right side has no extents at all.
4338 eb
= path
->nodes
[0];
4339 slot
= path
->slots
[0];
4340 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4341 if (found_key
.objectid
!= key
.objectid
||
4342 found_key
.type
!= key
.type
) {
4343 /* If we're a hole then just pretend nothing changed */
4344 ret
= (left_disknr
) ? 0 : 1;
4349 * We're now on 2a, 2b or 7.
4352 while (key
.offset
< ekey
->offset
+ left_len
) {
4353 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
4354 right_type
= btrfs_file_extent_type(eb
, ei
);
4355 if (right_type
!= BTRFS_FILE_EXTENT_REG
) {
4360 right_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
4361 right_len
= btrfs_file_extent_num_bytes(eb
, ei
);
4362 right_offset
= btrfs_file_extent_offset(eb
, ei
);
4363 right_gen
= btrfs_file_extent_generation(eb
, ei
);
4366 * Are we at extent 8? If yes, we know the extent is changed.
4367 * This may only happen on the first iteration.
4369 if (found_key
.offset
+ right_len
<= ekey
->offset
) {
4370 /* If we're a hole just pretend nothing changed */
4371 ret
= (left_disknr
) ? 0 : 1;
4375 left_offset_fixed
= left_offset
;
4376 if (key
.offset
< ekey
->offset
) {
4377 /* Fix the right offset for 2a and 7. */
4378 right_offset
+= ekey
->offset
- key
.offset
;
4380 /* Fix the left offset for all behind 2a and 2b */
4381 left_offset_fixed
+= key
.offset
- ekey
->offset
;
4385 * Check if we have the same extent.
4387 if (left_disknr
!= right_disknr
||
4388 left_offset_fixed
!= right_offset
||
4389 left_gen
!= right_gen
) {
4395 * Go to the next extent.
4397 ret
= btrfs_next_item(sctx
->parent_root
, path
);
4401 eb
= path
->nodes
[0];
4402 slot
= path
->slots
[0];
4403 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4405 if (ret
|| found_key
.objectid
!= key
.objectid
||
4406 found_key
.type
!= key
.type
) {
4407 key
.offset
+= right_len
;
4410 if (found_key
.offset
!= key
.offset
+ right_len
) {
4418 * We're now behind the left extent (treat as unchanged) or at the end
4419 * of the right side (treat as changed).
4421 if (key
.offset
>= ekey
->offset
+ left_len
)
4428 btrfs_free_path(path
);
4432 static int get_last_extent(struct send_ctx
*sctx
, u64 offset
)
4434 struct btrfs_path
*path
;
4435 struct btrfs_root
*root
= sctx
->send_root
;
4436 struct btrfs_file_extent_item
*fi
;
4437 struct btrfs_key key
;
4442 path
= alloc_path_for_send();
4446 sctx
->cur_inode_last_extent
= 0;
4448 key
.objectid
= sctx
->cur_ino
;
4449 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4450 key
.offset
= offset
;
4451 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 0, 1);
4455 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
4456 if (key
.objectid
!= sctx
->cur_ino
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
4459 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4460 struct btrfs_file_extent_item
);
4461 type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
4462 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4463 u64 size
= btrfs_file_extent_inline_len(path
->nodes
[0],
4464 path
->slots
[0], fi
);
4465 extent_end
= ALIGN(key
.offset
+ size
,
4466 sctx
->send_root
->sectorsize
);
4468 extent_end
= key
.offset
+
4469 btrfs_file_extent_num_bytes(path
->nodes
[0], fi
);
4471 sctx
->cur_inode_last_extent
= extent_end
;
4473 btrfs_free_path(path
);
4477 static int maybe_send_hole(struct send_ctx
*sctx
, struct btrfs_path
*path
,
4478 struct btrfs_key
*key
)
4480 struct btrfs_file_extent_item
*fi
;
4485 if (sctx
->cur_ino
!= key
->objectid
|| !need_send_hole(sctx
))
4488 if (sctx
->cur_inode_last_extent
== (u64
)-1) {
4489 ret
= get_last_extent(sctx
, key
->offset
- 1);
4494 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4495 struct btrfs_file_extent_item
);
4496 type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
4497 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4498 u64 size
= btrfs_file_extent_inline_len(path
->nodes
[0],
4499 path
->slots
[0], fi
);
4500 extent_end
= ALIGN(key
->offset
+ size
,
4501 sctx
->send_root
->sectorsize
);
4503 extent_end
= key
->offset
+
4504 btrfs_file_extent_num_bytes(path
->nodes
[0], fi
);
4507 if (path
->slots
[0] == 0 &&
4508 sctx
->cur_inode_last_extent
< key
->offset
) {
4510 * We might have skipped entire leafs that contained only
4511 * file extent items for our current inode. These leafs have
4512 * a generation number smaller (older) than the one in the
4513 * current leaf and the leaf our last extent came from, and
4514 * are located between these 2 leafs.
4516 ret
= get_last_extent(sctx
, key
->offset
- 1);
4521 if (sctx
->cur_inode_last_extent
< key
->offset
)
4522 ret
= send_hole(sctx
, key
->offset
);
4523 sctx
->cur_inode_last_extent
= extent_end
;
4527 static int process_extent(struct send_ctx
*sctx
,
4528 struct btrfs_path
*path
,
4529 struct btrfs_key
*key
)
4531 struct clone_root
*found_clone
= NULL
;
4534 if (S_ISLNK(sctx
->cur_inode_mode
))
4537 if (sctx
->parent_root
&& !sctx
->cur_inode_new
) {
4538 ret
= is_extent_unchanged(sctx
, path
, key
);
4546 struct btrfs_file_extent_item
*ei
;
4549 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4550 struct btrfs_file_extent_item
);
4551 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
4552 if (type
== BTRFS_FILE_EXTENT_PREALLOC
||
4553 type
== BTRFS_FILE_EXTENT_REG
) {
4555 * The send spec does not have a prealloc command yet,
4556 * so just leave a hole for prealloc'ed extents until
4557 * we have enough commands queued up to justify rev'ing
4560 if (type
== BTRFS_FILE_EXTENT_PREALLOC
) {
4565 /* Have a hole, just skip it. */
4566 if (btrfs_file_extent_disk_bytenr(path
->nodes
[0], ei
) == 0) {
4573 ret
= find_extent_clone(sctx
, path
, key
->objectid
, key
->offset
,
4574 sctx
->cur_inode_size
, &found_clone
);
4575 if (ret
!= -ENOENT
&& ret
< 0)
4578 ret
= send_write_or_clone(sctx
, path
, key
, found_clone
);
4582 ret
= maybe_send_hole(sctx
, path
, key
);
4587 static int process_all_extents(struct send_ctx
*sctx
)
4590 struct btrfs_root
*root
;
4591 struct btrfs_path
*path
;
4592 struct btrfs_key key
;
4593 struct btrfs_key found_key
;
4594 struct extent_buffer
*eb
;
4597 root
= sctx
->send_root
;
4598 path
= alloc_path_for_send();
4602 key
.objectid
= sctx
->cmp_key
->objectid
;
4603 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4605 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4610 eb
= path
->nodes
[0];
4611 slot
= path
->slots
[0];
4613 if (slot
>= btrfs_header_nritems(eb
)) {
4614 ret
= btrfs_next_leaf(root
, path
);
4617 } else if (ret
> 0) {
4624 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4626 if (found_key
.objectid
!= key
.objectid
||
4627 found_key
.type
!= key
.type
) {
4632 ret
= process_extent(sctx
, path
, &found_key
);
4640 btrfs_free_path(path
);
4644 static int process_recorded_refs_if_needed(struct send_ctx
*sctx
, int at_end
,
4646 int *refs_processed
)
4650 if (sctx
->cur_ino
== 0)
4652 if (!at_end
&& sctx
->cur_ino
== sctx
->cmp_key
->objectid
&&
4653 sctx
->cmp_key
->type
<= BTRFS_INODE_EXTREF_KEY
)
4655 if (list_empty(&sctx
->new_refs
) && list_empty(&sctx
->deleted_refs
))
4658 ret
= process_recorded_refs(sctx
, pending_move
);
4662 *refs_processed
= 1;
4667 static int finish_inode_if_needed(struct send_ctx
*sctx
, int at_end
)
4678 int pending_move
= 0;
4679 int refs_processed
= 0;
4681 ret
= process_recorded_refs_if_needed(sctx
, at_end
, &pending_move
,
4687 * We have processed the refs and thus need to advance send_progress.
4688 * Now, calls to get_cur_xxx will take the updated refs of the current
4689 * inode into account.
4691 * On the other hand, if our current inode is a directory and couldn't
4692 * be moved/renamed because its parent was renamed/moved too and it has
4693 * a higher inode number, we can only move/rename our current inode
4694 * after we moved/renamed its parent. Therefore in this case operate on
4695 * the old path (pre move/rename) of our current inode, and the
4696 * move/rename will be performed later.
4698 if (refs_processed
&& !pending_move
)
4699 sctx
->send_progress
= sctx
->cur_ino
+ 1;
4701 if (sctx
->cur_ino
== 0 || sctx
->cur_inode_deleted
)
4703 if (!at_end
&& sctx
->cmp_key
->objectid
== sctx
->cur_ino
)
4706 ret
= get_inode_info(sctx
->send_root
, sctx
->cur_ino
, NULL
, NULL
,
4707 &left_mode
, &left_uid
, &left_gid
, NULL
);
4711 if (!sctx
->parent_root
|| sctx
->cur_inode_new
) {
4713 if (!S_ISLNK(sctx
->cur_inode_mode
))
4716 ret
= get_inode_info(sctx
->parent_root
, sctx
->cur_ino
,
4717 NULL
, NULL
, &right_mode
, &right_uid
,
4722 if (left_uid
!= right_uid
|| left_gid
!= right_gid
)
4724 if (!S_ISLNK(sctx
->cur_inode_mode
) && left_mode
!= right_mode
)
4728 if (S_ISREG(sctx
->cur_inode_mode
)) {
4729 if (need_send_hole(sctx
)) {
4730 if (sctx
->cur_inode_last_extent
== (u64
)-1) {
4731 ret
= get_last_extent(sctx
, (u64
)-1);
4735 if (sctx
->cur_inode_last_extent
<
4736 sctx
->cur_inode_size
) {
4737 ret
= send_hole(sctx
, sctx
->cur_inode_size
);
4742 ret
= send_truncate(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4743 sctx
->cur_inode_size
);
4749 ret
= send_chown(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4750 left_uid
, left_gid
);
4755 ret
= send_chmod(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4762 * If other directory inodes depended on our current directory
4763 * inode's move/rename, now do their move/rename operations.
4765 if (!is_waiting_for_move(sctx
, sctx
->cur_ino
)) {
4766 ret
= apply_children_dir_moves(sctx
);
4772 * Need to send that every time, no matter if it actually
4773 * changed between the two trees as we have done changes to
4776 sctx
->send_progress
= sctx
->cur_ino
+ 1;
4777 ret
= send_utimes(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
);
4785 static int changed_inode(struct send_ctx
*sctx
,
4786 enum btrfs_compare_tree_result result
)
4789 struct btrfs_key
*key
= sctx
->cmp_key
;
4790 struct btrfs_inode_item
*left_ii
= NULL
;
4791 struct btrfs_inode_item
*right_ii
= NULL
;
4795 sctx
->cur_ino
= key
->objectid
;
4796 sctx
->cur_inode_new_gen
= 0;
4797 sctx
->cur_inode_last_extent
= (u64
)-1;
4800 * Set send_progress to current inode. This will tell all get_cur_xxx
4801 * functions that the current inode's refs are not updated yet. Later,
4802 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4804 sctx
->send_progress
= sctx
->cur_ino
;
4806 if (result
== BTRFS_COMPARE_TREE_NEW
||
4807 result
== BTRFS_COMPARE_TREE_CHANGED
) {
4808 left_ii
= btrfs_item_ptr(sctx
->left_path
->nodes
[0],
4809 sctx
->left_path
->slots
[0],
4810 struct btrfs_inode_item
);
4811 left_gen
= btrfs_inode_generation(sctx
->left_path
->nodes
[0],
4814 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
4815 sctx
->right_path
->slots
[0],
4816 struct btrfs_inode_item
);
4817 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
4820 if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
4821 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
4822 sctx
->right_path
->slots
[0],
4823 struct btrfs_inode_item
);
4825 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
4829 * The cur_ino = root dir case is special here. We can't treat
4830 * the inode as deleted+reused because it would generate a
4831 * stream that tries to delete/mkdir the root dir.
4833 if (left_gen
!= right_gen
&&
4834 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
4835 sctx
->cur_inode_new_gen
= 1;
4838 if (result
== BTRFS_COMPARE_TREE_NEW
) {
4839 sctx
->cur_inode_gen
= left_gen
;
4840 sctx
->cur_inode_new
= 1;
4841 sctx
->cur_inode_deleted
= 0;
4842 sctx
->cur_inode_size
= btrfs_inode_size(
4843 sctx
->left_path
->nodes
[0], left_ii
);
4844 sctx
->cur_inode_mode
= btrfs_inode_mode(
4845 sctx
->left_path
->nodes
[0], left_ii
);
4846 if (sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
4847 ret
= send_create_inode_if_needed(sctx
);
4848 } else if (result
== BTRFS_COMPARE_TREE_DELETED
) {
4849 sctx
->cur_inode_gen
= right_gen
;
4850 sctx
->cur_inode_new
= 0;
4851 sctx
->cur_inode_deleted
= 1;
4852 sctx
->cur_inode_size
= btrfs_inode_size(
4853 sctx
->right_path
->nodes
[0], right_ii
);
4854 sctx
->cur_inode_mode
= btrfs_inode_mode(
4855 sctx
->right_path
->nodes
[0], right_ii
);
4856 } else if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
4858 * We need to do some special handling in case the inode was
4859 * reported as changed with a changed generation number. This
4860 * means that the original inode was deleted and new inode
4861 * reused the same inum. So we have to treat the old inode as
4862 * deleted and the new one as new.
4864 if (sctx
->cur_inode_new_gen
) {
4866 * First, process the inode as if it was deleted.
4868 sctx
->cur_inode_gen
= right_gen
;
4869 sctx
->cur_inode_new
= 0;
4870 sctx
->cur_inode_deleted
= 1;
4871 sctx
->cur_inode_size
= btrfs_inode_size(
4872 sctx
->right_path
->nodes
[0], right_ii
);
4873 sctx
->cur_inode_mode
= btrfs_inode_mode(
4874 sctx
->right_path
->nodes
[0], right_ii
);
4875 ret
= process_all_refs(sctx
,
4876 BTRFS_COMPARE_TREE_DELETED
);
4881 * Now process the inode as if it was new.
4883 sctx
->cur_inode_gen
= left_gen
;
4884 sctx
->cur_inode_new
= 1;
4885 sctx
->cur_inode_deleted
= 0;
4886 sctx
->cur_inode_size
= btrfs_inode_size(
4887 sctx
->left_path
->nodes
[0], left_ii
);
4888 sctx
->cur_inode_mode
= btrfs_inode_mode(
4889 sctx
->left_path
->nodes
[0], left_ii
);
4890 ret
= send_create_inode_if_needed(sctx
);
4894 ret
= process_all_refs(sctx
, BTRFS_COMPARE_TREE_NEW
);
4898 * Advance send_progress now as we did not get into
4899 * process_recorded_refs_if_needed in the new_gen case.
4901 sctx
->send_progress
= sctx
->cur_ino
+ 1;
4904 * Now process all extents and xattrs of the inode as if
4905 * they were all new.
4907 ret
= process_all_extents(sctx
);
4910 ret
= process_all_new_xattrs(sctx
);
4914 sctx
->cur_inode_gen
= left_gen
;
4915 sctx
->cur_inode_new
= 0;
4916 sctx
->cur_inode_new_gen
= 0;
4917 sctx
->cur_inode_deleted
= 0;
4918 sctx
->cur_inode_size
= btrfs_inode_size(
4919 sctx
->left_path
->nodes
[0], left_ii
);
4920 sctx
->cur_inode_mode
= btrfs_inode_mode(
4921 sctx
->left_path
->nodes
[0], left_ii
);
4930 * We have to process new refs before deleted refs, but compare_trees gives us
4931 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4932 * first and later process them in process_recorded_refs.
4933 * For the cur_inode_new_gen case, we skip recording completely because
4934 * changed_inode did already initiate processing of refs. The reason for this is
4935 * that in this case, compare_tree actually compares the refs of 2 different
4936 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4937 * refs of the right tree as deleted and all refs of the left tree as new.
4939 static int changed_ref(struct send_ctx
*sctx
,
4940 enum btrfs_compare_tree_result result
)
4944 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4946 if (!sctx
->cur_inode_new_gen
&&
4947 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
4948 if (result
== BTRFS_COMPARE_TREE_NEW
)
4949 ret
= record_new_ref(sctx
);
4950 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
4951 ret
= record_deleted_ref(sctx
);
4952 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
4953 ret
= record_changed_ref(sctx
);
4960 * Process new/deleted/changed xattrs. We skip processing in the
4961 * cur_inode_new_gen case because changed_inode did already initiate processing
4962 * of xattrs. The reason is the same as in changed_ref
4964 static int changed_xattr(struct send_ctx
*sctx
,
4965 enum btrfs_compare_tree_result result
)
4969 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4971 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
4972 if (result
== BTRFS_COMPARE_TREE_NEW
)
4973 ret
= process_new_xattr(sctx
);
4974 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
4975 ret
= process_deleted_xattr(sctx
);
4976 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
4977 ret
= process_changed_xattr(sctx
);
4984 * Process new/deleted/changed extents. We skip processing in the
4985 * cur_inode_new_gen case because changed_inode did already initiate processing
4986 * of extents. The reason is the same as in changed_ref
4988 static int changed_extent(struct send_ctx
*sctx
,
4989 enum btrfs_compare_tree_result result
)
4993 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4995 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
4996 if (result
!= BTRFS_COMPARE_TREE_DELETED
)
4997 ret
= process_extent(sctx
, sctx
->left_path
,
5004 static int dir_changed(struct send_ctx
*sctx
, u64 dir
)
5006 u64 orig_gen
, new_gen
;
5009 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &new_gen
, NULL
, NULL
,
5014 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &orig_gen
, NULL
,
5019 return (orig_gen
!= new_gen
) ? 1 : 0;
5022 static int compare_refs(struct send_ctx
*sctx
, struct btrfs_path
*path
,
5023 struct btrfs_key
*key
)
5025 struct btrfs_inode_extref
*extref
;
5026 struct extent_buffer
*leaf
;
5027 u64 dirid
= 0, last_dirid
= 0;
5034 /* Easy case, just check this one dirid */
5035 if (key
->type
== BTRFS_INODE_REF_KEY
) {
5036 dirid
= key
->offset
;
5038 ret
= dir_changed(sctx
, dirid
);
5042 leaf
= path
->nodes
[0];
5043 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
5044 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
5045 while (cur_offset
< item_size
) {
5046 extref
= (struct btrfs_inode_extref
*)(ptr
+
5048 dirid
= btrfs_inode_extref_parent(leaf
, extref
);
5049 ref_name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
5050 cur_offset
+= ref_name_len
+ sizeof(*extref
);
5051 if (dirid
== last_dirid
)
5053 ret
= dir_changed(sctx
, dirid
);
5063 * Updates compare related fields in sctx and simply forwards to the actual
5064 * changed_xxx functions.
5066 static int changed_cb(struct btrfs_root
*left_root
,
5067 struct btrfs_root
*right_root
,
5068 struct btrfs_path
*left_path
,
5069 struct btrfs_path
*right_path
,
5070 struct btrfs_key
*key
,
5071 enum btrfs_compare_tree_result result
,
5075 struct send_ctx
*sctx
= ctx
;
5077 if (result
== BTRFS_COMPARE_TREE_SAME
) {
5078 if (key
->type
== BTRFS_INODE_REF_KEY
||
5079 key
->type
== BTRFS_INODE_EXTREF_KEY
) {
5080 ret
= compare_refs(sctx
, left_path
, key
);
5085 } else if (key
->type
== BTRFS_EXTENT_DATA_KEY
) {
5086 return maybe_send_hole(sctx
, left_path
, key
);
5090 result
= BTRFS_COMPARE_TREE_CHANGED
;
5094 sctx
->left_path
= left_path
;
5095 sctx
->right_path
= right_path
;
5096 sctx
->cmp_key
= key
;
5098 ret
= finish_inode_if_needed(sctx
, 0);
5102 /* Ignore non-FS objects */
5103 if (key
->objectid
== BTRFS_FREE_INO_OBJECTID
||
5104 key
->objectid
== BTRFS_FREE_SPACE_OBJECTID
)
5107 if (key
->type
== BTRFS_INODE_ITEM_KEY
)
5108 ret
= changed_inode(sctx
, result
);
5109 else if (key
->type
== BTRFS_INODE_REF_KEY
||
5110 key
->type
== BTRFS_INODE_EXTREF_KEY
)
5111 ret
= changed_ref(sctx
, result
);
5112 else if (key
->type
== BTRFS_XATTR_ITEM_KEY
)
5113 ret
= changed_xattr(sctx
, result
);
5114 else if (key
->type
== BTRFS_EXTENT_DATA_KEY
)
5115 ret
= changed_extent(sctx
, result
);
5121 static int full_send_tree(struct send_ctx
*sctx
)
5124 struct btrfs_root
*send_root
= sctx
->send_root
;
5125 struct btrfs_key key
;
5126 struct btrfs_key found_key
;
5127 struct btrfs_path
*path
;
5128 struct extent_buffer
*eb
;
5133 path
= alloc_path_for_send();
5137 spin_lock(&send_root
->root_item_lock
);
5138 start_ctransid
= btrfs_root_ctransid(&send_root
->root_item
);
5139 spin_unlock(&send_root
->root_item_lock
);
5141 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
5142 key
.type
= BTRFS_INODE_ITEM_KEY
;
5146 * Make sure the tree has not changed after re-joining. We detect this
5147 * by comparing start_ctransid and ctransid. They should always match.
5149 spin_lock(&send_root
->root_item_lock
);
5150 ctransid
= btrfs_root_ctransid(&send_root
->root_item
);
5151 spin_unlock(&send_root
->root_item_lock
);
5153 if (ctransid
!= start_ctransid
) {
5154 WARN(1, KERN_WARNING
"BTRFS: the root that you're trying to "
5155 "send was modified in between. This is "
5156 "probably a bug.\n");
5161 ret
= btrfs_search_slot_for_read(send_root
, &key
, path
, 1, 0);
5168 eb
= path
->nodes
[0];
5169 slot
= path
->slots
[0];
5170 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5172 ret
= changed_cb(send_root
, NULL
, path
, NULL
,
5173 &found_key
, BTRFS_COMPARE_TREE_NEW
, sctx
);
5177 key
.objectid
= found_key
.objectid
;
5178 key
.type
= found_key
.type
;
5179 key
.offset
= found_key
.offset
+ 1;
5181 ret
= btrfs_next_item(send_root
, path
);
5191 ret
= finish_inode_if_needed(sctx
, 1);
5194 btrfs_free_path(path
);
5198 static int send_subvol(struct send_ctx
*sctx
)
5202 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_STREAM_HEADER
)) {
5203 ret
= send_header(sctx
);
5208 ret
= send_subvol_begin(sctx
);
5212 if (sctx
->parent_root
) {
5213 ret
= btrfs_compare_trees(sctx
->send_root
, sctx
->parent_root
,
5217 ret
= finish_inode_if_needed(sctx
, 1);
5221 ret
= full_send_tree(sctx
);
5227 free_recorded_refs(sctx
);
5231 static void btrfs_root_dec_send_in_progress(struct btrfs_root
* root
)
5233 spin_lock(&root
->root_item_lock
);
5234 root
->send_in_progress
--;
5236 * Not much left to do, we don't know why it's unbalanced and
5237 * can't blindly reset it to 0.
5239 if (root
->send_in_progress
< 0)
5240 btrfs_err(root
->fs_info
,
5241 "send_in_progres unbalanced %d root %llu\n",
5242 root
->send_in_progress
, root
->root_key
.objectid
);
5243 spin_unlock(&root
->root_item_lock
);
5246 long btrfs_ioctl_send(struct file
*mnt_file
, void __user
*arg_
)
5249 struct btrfs_root
*send_root
;
5250 struct btrfs_root
*clone_root
;
5251 struct btrfs_fs_info
*fs_info
;
5252 struct btrfs_ioctl_send_args
*arg
= NULL
;
5253 struct btrfs_key key
;
5254 struct send_ctx
*sctx
= NULL
;
5256 u64
*clone_sources_tmp
= NULL
;
5257 int clone_sources_to_rollback
= 0;
5258 int sort_clone_roots
= 0;
5261 if (!capable(CAP_SYS_ADMIN
))
5264 send_root
= BTRFS_I(file_inode(mnt_file
))->root
;
5265 fs_info
= send_root
->fs_info
;
5268 * The subvolume must remain read-only during send, protect against
5271 spin_lock(&send_root
->root_item_lock
);
5272 send_root
->send_in_progress
++;
5273 spin_unlock(&send_root
->root_item_lock
);
5276 * This is done when we lookup the root, it should already be complete
5277 * by the time we get here.
5279 WARN_ON(send_root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
);
5282 * Userspace tools do the checks and warn the user if it's
5285 if (!btrfs_root_readonly(send_root
)) {
5290 arg
= memdup_user(arg_
, sizeof(*arg
));
5297 if (!access_ok(VERIFY_READ
, arg
->clone_sources
,
5298 sizeof(*arg
->clone_sources
) *
5299 arg
->clone_sources_count
)) {
5304 if (arg
->flags
& ~BTRFS_SEND_FLAG_MASK
) {
5309 sctx
= kzalloc(sizeof(struct send_ctx
), GFP_NOFS
);
5315 INIT_LIST_HEAD(&sctx
->new_refs
);
5316 INIT_LIST_HEAD(&sctx
->deleted_refs
);
5317 INIT_RADIX_TREE(&sctx
->name_cache
, GFP_NOFS
);
5318 INIT_LIST_HEAD(&sctx
->name_cache_list
);
5320 sctx
->flags
= arg
->flags
;
5322 sctx
->send_filp
= fget(arg
->send_fd
);
5323 if (!sctx
->send_filp
) {
5328 sctx
->send_root
= send_root
;
5329 sctx
->clone_roots_cnt
= arg
->clone_sources_count
;
5331 sctx
->send_max_size
= BTRFS_SEND_BUF_SIZE
;
5332 sctx
->send_buf
= vmalloc(sctx
->send_max_size
);
5333 if (!sctx
->send_buf
) {
5338 sctx
->read_buf
= vmalloc(BTRFS_SEND_READ_SIZE
);
5339 if (!sctx
->read_buf
) {
5344 sctx
->pending_dir_moves
= RB_ROOT
;
5345 sctx
->waiting_dir_moves
= RB_ROOT
;
5347 sctx
->clone_roots
= vzalloc(sizeof(struct clone_root
) *
5348 (arg
->clone_sources_count
+ 1));
5349 if (!sctx
->clone_roots
) {
5354 if (arg
->clone_sources_count
) {
5355 clone_sources_tmp
= vmalloc(arg
->clone_sources_count
*
5356 sizeof(*arg
->clone_sources
));
5357 if (!clone_sources_tmp
) {
5362 ret
= copy_from_user(clone_sources_tmp
, arg
->clone_sources
,
5363 arg
->clone_sources_count
*
5364 sizeof(*arg
->clone_sources
));
5370 for (i
= 0; i
< arg
->clone_sources_count
; i
++) {
5371 key
.objectid
= clone_sources_tmp
[i
];
5372 key
.type
= BTRFS_ROOT_ITEM_KEY
;
5373 key
.offset
= (u64
)-1;
5375 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
5377 clone_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
5378 if (IS_ERR(clone_root
)) {
5379 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5380 ret
= PTR_ERR(clone_root
);
5383 clone_sources_to_rollback
= i
+ 1;
5384 spin_lock(&clone_root
->root_item_lock
);
5385 clone_root
->send_in_progress
++;
5386 if (!btrfs_root_readonly(clone_root
)) {
5387 spin_unlock(&clone_root
->root_item_lock
);
5388 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5392 spin_unlock(&clone_root
->root_item_lock
);
5393 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5395 sctx
->clone_roots
[i
].root
= clone_root
;
5397 vfree(clone_sources_tmp
);
5398 clone_sources_tmp
= NULL
;
5401 if (arg
->parent_root
) {
5402 key
.objectid
= arg
->parent_root
;
5403 key
.type
= BTRFS_ROOT_ITEM_KEY
;
5404 key
.offset
= (u64
)-1;
5406 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
5408 sctx
->parent_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
5409 if (IS_ERR(sctx
->parent_root
)) {
5410 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5411 ret
= PTR_ERR(sctx
->parent_root
);
5415 spin_lock(&sctx
->parent_root
->root_item_lock
);
5416 sctx
->parent_root
->send_in_progress
++;
5417 if (!btrfs_root_readonly(sctx
->parent_root
)) {
5418 spin_unlock(&sctx
->parent_root
->root_item_lock
);
5419 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5423 spin_unlock(&sctx
->parent_root
->root_item_lock
);
5425 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5429 * Clones from send_root are allowed, but only if the clone source
5430 * is behind the current send position. This is checked while searching
5431 * for possible clone sources.
5433 sctx
->clone_roots
[sctx
->clone_roots_cnt
++].root
= sctx
->send_root
;
5435 /* We do a bsearch later */
5436 sort(sctx
->clone_roots
, sctx
->clone_roots_cnt
,
5437 sizeof(*sctx
->clone_roots
), __clone_root_cmp_sort
,
5439 sort_clone_roots
= 1;
5441 ret
= send_subvol(sctx
);
5445 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_END_CMD
)) {
5446 ret
= begin_cmd(sctx
, BTRFS_SEND_C_END
);
5449 ret
= send_cmd(sctx
);
5455 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->pending_dir_moves
));
5456 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->pending_dir_moves
)) {
5458 struct pending_dir_move
*pm
;
5460 n
= rb_first(&sctx
->pending_dir_moves
);
5461 pm
= rb_entry(n
, struct pending_dir_move
, node
);
5462 while (!list_empty(&pm
->list
)) {
5463 struct pending_dir_move
*pm2
;
5465 pm2
= list_first_entry(&pm
->list
,
5466 struct pending_dir_move
, list
);
5467 free_pending_move(sctx
, pm2
);
5469 free_pending_move(sctx
, pm
);
5472 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
));
5473 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
)) {
5475 struct waiting_dir_move
*dm
;
5477 n
= rb_first(&sctx
->waiting_dir_moves
);
5478 dm
= rb_entry(n
, struct waiting_dir_move
, node
);
5479 rb_erase(&dm
->node
, &sctx
->waiting_dir_moves
);
5483 if (sort_clone_roots
) {
5484 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++)
5485 btrfs_root_dec_send_in_progress(
5486 sctx
->clone_roots
[i
].root
);
5488 for (i
= 0; sctx
&& i
< clone_sources_to_rollback
; i
++)
5489 btrfs_root_dec_send_in_progress(
5490 sctx
->clone_roots
[i
].root
);
5492 btrfs_root_dec_send_in_progress(send_root
);
5494 if (sctx
&& !IS_ERR_OR_NULL(sctx
->parent_root
))
5495 btrfs_root_dec_send_in_progress(sctx
->parent_root
);
5498 vfree(clone_sources_tmp
);
5501 if (sctx
->send_filp
)
5502 fput(sctx
->send_filp
);
5504 vfree(sctx
->clone_roots
);
5505 vfree(sctx
->send_buf
);
5506 vfree(sctx
->read_buf
);
5508 name_cache_free(sctx
);