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.
56 unsigned short buf_len
:15;
57 unsigned short reversed
:1;
61 * Average path length does not exceed 200 bytes, we'll have
62 * better packing in the slab and higher chance to satisfy
63 * a allocation later during send.
68 #define FS_PATH_INLINE_SIZE \
69 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
72 /* reused for each extent */
74 struct btrfs_root
*root
;
81 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
82 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
85 struct file
*send_filp
;
91 u64 cmd_send_size
[BTRFS_SEND_C_MAX
+ 1];
92 u64 flags
; /* 'flags' member of btrfs_ioctl_send_args is u64 */
94 struct btrfs_root
*send_root
;
95 struct btrfs_root
*parent_root
;
96 struct clone_root
*clone_roots
;
99 /* current state of the compare_tree call */
100 struct btrfs_path
*left_path
;
101 struct btrfs_path
*right_path
;
102 struct btrfs_key
*cmp_key
;
105 * infos of the currently processed inode. In case of deleted inodes,
106 * these are the values from the deleted inode.
111 int cur_inode_new_gen
;
112 int cur_inode_deleted
;
116 u64 cur_inode_last_extent
;
120 struct list_head new_refs
;
121 struct list_head deleted_refs
;
123 struct radix_tree_root name_cache
;
124 struct list_head name_cache_list
;
127 struct file_ra_state ra
;
132 * We process inodes by their increasing order, so if before an
133 * incremental send we reverse the parent/child relationship of
134 * directories such that a directory with a lower inode number was
135 * the parent of a directory with a higher inode number, and the one
136 * becoming the new parent got renamed too, we can't rename/move the
137 * directory with lower inode number when we finish processing it - we
138 * must process the directory with higher inode number first, then
139 * rename/move it and then rename/move the directory with lower inode
140 * number. Example follows.
142 * Tree state when the first send was performed:
154 * Tree state when the second (incremental) send is performed:
163 * The sequence of steps that lead to the second state was:
165 * mv /a/b/c/d /a/b/c2/d2
166 * mv /a/b/c /a/b/c2/d2/cc
168 * "c" has lower inode number, but we can't move it (2nd mv operation)
169 * before we move "d", which has higher inode number.
171 * So we just memorize which move/rename operations must be performed
172 * later when their respective parent is processed and moved/renamed.
175 /* Indexed by parent directory inode number. */
176 struct rb_root pending_dir_moves
;
179 * Reverse index, indexed by the inode number of a directory that
180 * is waiting for the move/rename of its immediate parent before its
181 * own move/rename can be performed.
183 struct rb_root waiting_dir_moves
;
186 * A directory that is going to be rm'ed might have a child directory
187 * which is in the pending directory moves index above. In this case,
188 * the directory can only be removed after the move/rename of its child
189 * is performed. Example:
209 * Sequence of steps that lead to the send snapshot:
210 * rm -f /a/b/c/foo.txt
212 * mv /a/b/c/x /a/b/YY
215 * When the child is processed, its move/rename is delayed until its
216 * parent is processed (as explained above), but all other operations
217 * like update utimes, chown, chgrp, etc, are performed and the paths
218 * that it uses for those operations must use the orphanized name of
219 * its parent (the directory we're going to rm later), so we need to
220 * memorize that name.
222 * Indexed by the inode number of the directory to be deleted.
224 struct rb_root orphan_dirs
;
227 struct pending_dir_move
{
229 struct list_head list
;
233 struct list_head update_refs
;
236 struct waiting_dir_move
{
240 * There might be some directory that could not be removed because it
241 * was waiting for this directory inode to be moved first. Therefore
242 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
247 struct orphan_dir_info
{
253 struct name_cache_entry
{
254 struct list_head list
;
256 * radix_tree has only 32bit entries but we need to handle 64bit inums.
257 * We use the lower 32bit of the 64bit inum to store it in the tree. If
258 * more then one inum would fall into the same entry, we use radix_list
259 * to store the additional entries. radix_list is also used to store
260 * entries where two entries have the same inum but different
263 struct list_head radix_list
;
269 int need_later_update
;
274 static int is_waiting_for_move(struct send_ctx
*sctx
, u64 ino
);
276 static struct waiting_dir_move
*
277 get_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
);
279 static int is_waiting_for_rm(struct send_ctx
*sctx
, u64 dir_ino
);
281 static int need_send_hole(struct send_ctx
*sctx
)
283 return (sctx
->parent_root
&& !sctx
->cur_inode_new
&&
284 !sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
&&
285 S_ISREG(sctx
->cur_inode_mode
));
288 static void fs_path_reset(struct fs_path
*p
)
291 p
->start
= p
->buf
+ p
->buf_len
- 1;
301 static struct fs_path
*fs_path_alloc(void)
305 p
= kmalloc(sizeof(*p
), GFP_NOFS
);
309 p
->buf
= p
->inline_buf
;
310 p
->buf_len
= FS_PATH_INLINE_SIZE
;
315 static struct fs_path
*fs_path_alloc_reversed(void)
327 static void fs_path_free(struct fs_path
*p
)
331 if (p
->buf
!= p
->inline_buf
)
336 static int fs_path_len(struct fs_path
*p
)
338 return p
->end
- p
->start
;
341 static int fs_path_ensure_buf(struct fs_path
*p
, int len
)
349 if (p
->buf_len
>= len
)
352 path_len
= p
->end
- p
->start
;
353 old_buf_len
= p
->buf_len
;
356 * First time the inline_buf does not suffice
358 if (p
->buf
== p
->inline_buf
)
359 tmp_buf
= kmalloc(len
, GFP_NOFS
);
361 tmp_buf
= krealloc(p
->buf
, len
, GFP_NOFS
);
366 * The real size of the buffer is bigger, this will let the fast path
367 * happen most of the time
369 p
->buf_len
= ksize(p
->buf
);
372 tmp_buf
= p
->buf
+ old_buf_len
- path_len
- 1;
373 p
->end
= p
->buf
+ p
->buf_len
- 1;
374 p
->start
= p
->end
- path_len
;
375 memmove(p
->start
, tmp_buf
, path_len
+ 1);
378 p
->end
= p
->start
+ path_len
;
383 static int fs_path_prepare_for_add(struct fs_path
*p
, int name_len
,
389 new_len
= p
->end
- p
->start
+ name_len
;
390 if (p
->start
!= p
->end
)
392 ret
= fs_path_ensure_buf(p
, new_len
);
397 if (p
->start
!= p
->end
)
399 p
->start
-= name_len
;
400 *prepared
= p
->start
;
402 if (p
->start
!= p
->end
)
413 static int fs_path_add(struct fs_path
*p
, const char *name
, int name_len
)
418 ret
= fs_path_prepare_for_add(p
, name_len
, &prepared
);
421 memcpy(prepared
, name
, name_len
);
427 static int fs_path_add_path(struct fs_path
*p
, struct fs_path
*p2
)
432 ret
= fs_path_prepare_for_add(p
, p2
->end
- p2
->start
, &prepared
);
435 memcpy(prepared
, p2
->start
, p2
->end
- p2
->start
);
441 static int fs_path_add_from_extent_buffer(struct fs_path
*p
,
442 struct extent_buffer
*eb
,
443 unsigned long off
, int len
)
448 ret
= fs_path_prepare_for_add(p
, len
, &prepared
);
452 read_extent_buffer(eb
, prepared
, off
, len
);
458 static int fs_path_copy(struct fs_path
*p
, struct fs_path
*from
)
462 p
->reversed
= from
->reversed
;
465 ret
= fs_path_add_path(p
, from
);
471 static void fs_path_unreverse(struct fs_path
*p
)
480 len
= p
->end
- p
->start
;
482 p
->end
= p
->start
+ len
;
483 memmove(p
->start
, tmp
, len
+ 1);
487 static struct btrfs_path
*alloc_path_for_send(void)
489 struct btrfs_path
*path
;
491 path
= btrfs_alloc_path();
494 path
->search_commit_root
= 1;
495 path
->skip_locking
= 1;
496 path
->need_commit_sem
= 1;
500 static int write_buf(struct file
*filp
, const void *buf
, u32 len
, loff_t
*off
)
510 ret
= vfs_write(filp
, (char *)buf
+ pos
, len
- pos
, off
);
511 /* TODO handle that correctly */
512 /*if (ret == -ERESTARTSYS) {
531 static int tlv_put(struct send_ctx
*sctx
, u16 attr
, const void *data
, int len
)
533 struct btrfs_tlv_header
*hdr
;
534 int total_len
= sizeof(*hdr
) + len
;
535 int left
= sctx
->send_max_size
- sctx
->send_size
;
537 if (unlikely(left
< total_len
))
540 hdr
= (struct btrfs_tlv_header
*) (sctx
->send_buf
+ sctx
->send_size
);
541 hdr
->tlv_type
= cpu_to_le16(attr
);
542 hdr
->tlv_len
= cpu_to_le16(len
);
543 memcpy(hdr
+ 1, data
, len
);
544 sctx
->send_size
+= total_len
;
549 #define TLV_PUT_DEFINE_INT(bits) \
550 static int tlv_put_u##bits(struct send_ctx *sctx, \
551 u##bits attr, u##bits value) \
553 __le##bits __tmp = cpu_to_le##bits(value); \
554 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
557 TLV_PUT_DEFINE_INT(64)
559 static int tlv_put_string(struct send_ctx
*sctx
, u16 attr
,
560 const char *str
, int len
)
564 return tlv_put(sctx
, attr
, str
, len
);
567 static int tlv_put_uuid(struct send_ctx
*sctx
, u16 attr
,
570 return tlv_put(sctx
, attr
, uuid
, BTRFS_UUID_SIZE
);
573 static int tlv_put_btrfs_timespec(struct send_ctx
*sctx
, u16 attr
,
574 struct extent_buffer
*eb
,
575 struct btrfs_timespec
*ts
)
577 struct btrfs_timespec bts
;
578 read_extent_buffer(eb
, &bts
, (unsigned long)ts
, sizeof(bts
));
579 return tlv_put(sctx
, attr
, &bts
, sizeof(bts
));
583 #define TLV_PUT(sctx, attrtype, attrlen, data) \
585 ret = tlv_put(sctx, attrtype, attrlen, data); \
587 goto tlv_put_failure; \
590 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
592 ret = tlv_put_u##bits(sctx, attrtype, value); \
594 goto tlv_put_failure; \
597 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
598 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
599 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
600 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
601 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
603 ret = tlv_put_string(sctx, attrtype, str, len); \
605 goto tlv_put_failure; \
607 #define TLV_PUT_PATH(sctx, attrtype, p) \
609 ret = tlv_put_string(sctx, attrtype, p->start, \
610 p->end - p->start); \
612 goto tlv_put_failure; \
614 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
616 ret = tlv_put_uuid(sctx, attrtype, uuid); \
618 goto tlv_put_failure; \
620 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
622 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
624 goto tlv_put_failure; \
627 static int send_header(struct send_ctx
*sctx
)
629 struct btrfs_stream_header hdr
;
631 strcpy(hdr
.magic
, BTRFS_SEND_STREAM_MAGIC
);
632 hdr
.version
= cpu_to_le32(BTRFS_SEND_STREAM_VERSION
);
634 return write_buf(sctx
->send_filp
, &hdr
, sizeof(hdr
),
639 * For each command/item we want to send to userspace, we call this function.
641 static int begin_cmd(struct send_ctx
*sctx
, int cmd
)
643 struct btrfs_cmd_header
*hdr
;
645 if (WARN_ON(!sctx
->send_buf
))
648 BUG_ON(sctx
->send_size
);
650 sctx
->send_size
+= sizeof(*hdr
);
651 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
652 hdr
->cmd
= cpu_to_le16(cmd
);
657 static int send_cmd(struct send_ctx
*sctx
)
660 struct btrfs_cmd_header
*hdr
;
663 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
664 hdr
->len
= cpu_to_le32(sctx
->send_size
- sizeof(*hdr
));
667 crc
= btrfs_crc32c(0, (unsigned char *)sctx
->send_buf
, sctx
->send_size
);
668 hdr
->crc
= cpu_to_le32(crc
);
670 ret
= write_buf(sctx
->send_filp
, sctx
->send_buf
, sctx
->send_size
,
673 sctx
->total_send_size
+= sctx
->send_size
;
674 sctx
->cmd_send_size
[le16_to_cpu(hdr
->cmd
)] += sctx
->send_size
;
681 * Sends a move instruction to user space
683 static int send_rename(struct send_ctx
*sctx
,
684 struct fs_path
*from
, struct fs_path
*to
)
688 verbose_printk("btrfs: send_rename %s -> %s\n", from
->start
, to
->start
);
690 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RENAME
);
694 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, from
);
695 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_TO
, to
);
697 ret
= send_cmd(sctx
);
705 * Sends a link instruction to user space
707 static int send_link(struct send_ctx
*sctx
,
708 struct fs_path
*path
, struct fs_path
*lnk
)
712 verbose_printk("btrfs: send_link %s -> %s\n", path
->start
, lnk
->start
);
714 ret
= begin_cmd(sctx
, BTRFS_SEND_C_LINK
);
718 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
719 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, lnk
);
721 ret
= send_cmd(sctx
);
729 * Sends an unlink instruction to user space
731 static int send_unlink(struct send_ctx
*sctx
, struct fs_path
*path
)
735 verbose_printk("btrfs: send_unlink %s\n", path
->start
);
737 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UNLINK
);
741 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
743 ret
= send_cmd(sctx
);
751 * Sends a rmdir instruction to user space
753 static int send_rmdir(struct send_ctx
*sctx
, struct fs_path
*path
)
757 verbose_printk("btrfs: send_rmdir %s\n", path
->start
);
759 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RMDIR
);
763 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
765 ret
= send_cmd(sctx
);
773 * Helper function to retrieve some fields from an inode item.
775 static int __get_inode_info(struct btrfs_root
*root
, struct btrfs_path
*path
,
776 u64 ino
, u64
*size
, u64
*gen
, u64
*mode
, u64
*uid
,
780 struct btrfs_inode_item
*ii
;
781 struct btrfs_key key
;
784 key
.type
= BTRFS_INODE_ITEM_KEY
;
786 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
793 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
794 struct btrfs_inode_item
);
796 *size
= btrfs_inode_size(path
->nodes
[0], ii
);
798 *gen
= btrfs_inode_generation(path
->nodes
[0], ii
);
800 *mode
= btrfs_inode_mode(path
->nodes
[0], ii
);
802 *uid
= btrfs_inode_uid(path
->nodes
[0], ii
);
804 *gid
= btrfs_inode_gid(path
->nodes
[0], ii
);
806 *rdev
= btrfs_inode_rdev(path
->nodes
[0], ii
);
811 static int get_inode_info(struct btrfs_root
*root
,
812 u64 ino
, u64
*size
, u64
*gen
,
813 u64
*mode
, u64
*uid
, u64
*gid
,
816 struct btrfs_path
*path
;
819 path
= alloc_path_for_send();
822 ret
= __get_inode_info(root
, path
, ino
, size
, gen
, mode
, uid
, gid
,
824 btrfs_free_path(path
);
828 typedef int (*iterate_inode_ref_t
)(int num
, u64 dir
, int index
,
833 * Helper function to iterate the entries in ONE btrfs_inode_ref or
834 * btrfs_inode_extref.
835 * The iterate callback may return a non zero value to stop iteration. This can
836 * be a negative value for error codes or 1 to simply stop it.
838 * path must point to the INODE_REF or INODE_EXTREF when called.
840 static int iterate_inode_ref(struct btrfs_root
*root
, struct btrfs_path
*path
,
841 struct btrfs_key
*found_key
, int resolve
,
842 iterate_inode_ref_t iterate
, void *ctx
)
844 struct extent_buffer
*eb
= path
->nodes
[0];
845 struct btrfs_item
*item
;
846 struct btrfs_inode_ref
*iref
;
847 struct btrfs_inode_extref
*extref
;
848 struct btrfs_path
*tmp_path
;
852 int slot
= path
->slots
[0];
859 unsigned long name_off
;
860 unsigned long elem_size
;
863 p
= fs_path_alloc_reversed();
867 tmp_path
= alloc_path_for_send();
874 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
875 ptr
= (unsigned long)btrfs_item_ptr(eb
, slot
,
876 struct btrfs_inode_ref
);
877 item
= btrfs_item_nr(slot
);
878 total
= btrfs_item_size(eb
, item
);
879 elem_size
= sizeof(*iref
);
881 ptr
= btrfs_item_ptr_offset(eb
, slot
);
882 total
= btrfs_item_size_nr(eb
, slot
);
883 elem_size
= sizeof(*extref
);
886 while (cur
< total
) {
889 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
890 iref
= (struct btrfs_inode_ref
*)(ptr
+ cur
);
891 name_len
= btrfs_inode_ref_name_len(eb
, iref
);
892 name_off
= (unsigned long)(iref
+ 1);
893 index
= btrfs_inode_ref_index(eb
, iref
);
894 dir
= found_key
->offset
;
896 extref
= (struct btrfs_inode_extref
*)(ptr
+ cur
);
897 name_len
= btrfs_inode_extref_name_len(eb
, extref
);
898 name_off
= (unsigned long)&extref
->name
;
899 index
= btrfs_inode_extref_index(eb
, extref
);
900 dir
= btrfs_inode_extref_parent(eb
, extref
);
904 start
= btrfs_ref_to_path(root
, tmp_path
, name_len
,
908 ret
= PTR_ERR(start
);
911 if (start
< p
->buf
) {
912 /* overflow , try again with larger buffer */
913 ret
= fs_path_ensure_buf(p
,
914 p
->buf_len
+ p
->buf
- start
);
917 start
= btrfs_ref_to_path(root
, tmp_path
,
922 ret
= PTR_ERR(start
);
925 BUG_ON(start
< p
->buf
);
929 ret
= fs_path_add_from_extent_buffer(p
, eb
, name_off
,
935 cur
+= elem_size
+ name_len
;
936 ret
= iterate(num
, dir
, index
, p
, ctx
);
943 btrfs_free_path(tmp_path
);
948 typedef int (*iterate_dir_item_t
)(int num
, struct btrfs_key
*di_key
,
949 const char *name
, int name_len
,
950 const char *data
, int data_len
,
954 * Helper function to iterate the entries in ONE btrfs_dir_item.
955 * The iterate callback may return a non zero value to stop iteration. This can
956 * be a negative value for error codes or 1 to simply stop it.
958 * path must point to the dir item when called.
960 static int iterate_dir_item(struct btrfs_root
*root
, struct btrfs_path
*path
,
961 struct btrfs_key
*found_key
,
962 iterate_dir_item_t iterate
, void *ctx
)
965 struct extent_buffer
*eb
;
966 struct btrfs_item
*item
;
967 struct btrfs_dir_item
*di
;
968 struct btrfs_key di_key
;
970 const int buf_len
= PATH_MAX
;
980 buf
= kmalloc(buf_len
, GFP_NOFS
);
987 slot
= path
->slots
[0];
988 item
= btrfs_item_nr(slot
);
989 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
992 total
= btrfs_item_size(eb
, item
);
995 while (cur
< total
) {
996 name_len
= btrfs_dir_name_len(eb
, di
);
997 data_len
= btrfs_dir_data_len(eb
, di
);
998 type
= btrfs_dir_type(eb
, di
);
999 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
1004 if (name_len
+ data_len
> buf_len
) {
1005 ret
= -ENAMETOOLONG
;
1009 read_extent_buffer(eb
, buf
, (unsigned long)(di
+ 1),
1010 name_len
+ data_len
);
1012 len
= sizeof(*di
) + name_len
+ data_len
;
1013 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
1016 ret
= iterate(num
, &di_key
, buf
, name_len
, buf
+ name_len
,
1017 data_len
, type
, ctx
);
1033 static int __copy_first_ref(int num
, u64 dir
, int index
,
1034 struct fs_path
*p
, void *ctx
)
1037 struct fs_path
*pt
= ctx
;
1039 ret
= fs_path_copy(pt
, p
);
1043 /* we want the first only */
1048 * Retrieve the first path of an inode. If an inode has more then one
1049 * ref/hardlink, this is ignored.
1051 static int get_inode_path(struct btrfs_root
*root
,
1052 u64 ino
, struct fs_path
*path
)
1055 struct btrfs_key key
, found_key
;
1056 struct btrfs_path
*p
;
1058 p
= alloc_path_for_send();
1062 fs_path_reset(path
);
1065 key
.type
= BTRFS_INODE_REF_KEY
;
1068 ret
= btrfs_search_slot_for_read(root
, &key
, p
, 1, 0);
1075 btrfs_item_key_to_cpu(p
->nodes
[0], &found_key
, p
->slots
[0]);
1076 if (found_key
.objectid
!= ino
||
1077 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1078 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1083 ret
= iterate_inode_ref(root
, p
, &found_key
, 1,
1084 __copy_first_ref
, path
);
1094 struct backref_ctx
{
1095 struct send_ctx
*sctx
;
1097 struct btrfs_path
*path
;
1098 /* number of total found references */
1102 * used for clones found in send_root. clones found behind cur_objectid
1103 * and cur_offset are not considered as allowed clones.
1108 /* may be truncated in case it's the last extent in a file */
1111 /* Just to check for bugs in backref resolving */
1115 static int __clone_root_cmp_bsearch(const void *key
, const void *elt
)
1117 u64 root
= (u64
)(uintptr_t)key
;
1118 struct clone_root
*cr
= (struct clone_root
*)elt
;
1120 if (root
< cr
->root
->objectid
)
1122 if (root
> cr
->root
->objectid
)
1127 static int __clone_root_cmp_sort(const void *e1
, const void *e2
)
1129 struct clone_root
*cr1
= (struct clone_root
*)e1
;
1130 struct clone_root
*cr2
= (struct clone_root
*)e2
;
1132 if (cr1
->root
->objectid
< cr2
->root
->objectid
)
1134 if (cr1
->root
->objectid
> cr2
->root
->objectid
)
1140 * Called for every backref that is found for the current extent.
1141 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1143 static int __iterate_backrefs(u64 ino
, u64 offset
, u64 root
, void *ctx_
)
1145 struct backref_ctx
*bctx
= ctx_
;
1146 struct clone_root
*found
;
1150 /* First check if the root is in the list of accepted clone sources */
1151 found
= bsearch((void *)(uintptr_t)root
, bctx
->sctx
->clone_roots
,
1152 bctx
->sctx
->clone_roots_cnt
,
1153 sizeof(struct clone_root
),
1154 __clone_root_cmp_bsearch
);
1158 if (found
->root
== bctx
->sctx
->send_root
&&
1159 ino
== bctx
->cur_objectid
&&
1160 offset
== bctx
->cur_offset
) {
1161 bctx
->found_itself
= 1;
1165 * There are inodes that have extents that lie behind its i_size. Don't
1166 * accept clones from these extents.
1168 ret
= __get_inode_info(found
->root
, bctx
->path
, ino
, &i_size
, NULL
, NULL
,
1170 btrfs_release_path(bctx
->path
);
1174 if (offset
+ bctx
->extent_len
> i_size
)
1178 * Make sure we don't consider clones from send_root that are
1179 * behind the current inode/offset.
1181 if (found
->root
== bctx
->sctx
->send_root
) {
1183 * TODO for the moment we don't accept clones from the inode
1184 * that is currently send. We may change this when
1185 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1188 if (ino
>= bctx
->cur_objectid
)
1191 if (ino
> bctx
->cur_objectid
)
1193 if (offset
+ bctx
->extent_len
> bctx
->cur_offset
)
1199 found
->found_refs
++;
1200 if (ino
< found
->ino
) {
1202 found
->offset
= offset
;
1203 } else if (found
->ino
== ino
) {
1205 * same extent found more then once in the same file.
1207 if (found
->offset
> offset
+ bctx
->extent_len
)
1208 found
->offset
= offset
;
1215 * Given an inode, offset and extent item, it finds a good clone for a clone
1216 * instruction. Returns -ENOENT when none could be found. The function makes
1217 * sure that the returned clone is usable at the point where sending is at the
1218 * moment. This means, that no clones are accepted which lie behind the current
1221 * path must point to the extent item when called.
1223 static int find_extent_clone(struct send_ctx
*sctx
,
1224 struct btrfs_path
*path
,
1225 u64 ino
, u64 data_offset
,
1227 struct clone_root
**found
)
1234 u64 extent_item_pos
;
1236 struct btrfs_file_extent_item
*fi
;
1237 struct extent_buffer
*eb
= path
->nodes
[0];
1238 struct backref_ctx
*backref_ctx
= NULL
;
1239 struct clone_root
*cur_clone_root
;
1240 struct btrfs_key found_key
;
1241 struct btrfs_path
*tmp_path
;
1245 tmp_path
= alloc_path_for_send();
1249 /* We only use this path under the commit sem */
1250 tmp_path
->need_commit_sem
= 0;
1252 backref_ctx
= kmalloc(sizeof(*backref_ctx
), GFP_NOFS
);
1258 backref_ctx
->path
= tmp_path
;
1260 if (data_offset
>= ino_size
) {
1262 * There may be extents that lie behind the file's size.
1263 * I at least had this in combination with snapshotting while
1264 * writing large files.
1270 fi
= btrfs_item_ptr(eb
, path
->slots
[0],
1271 struct btrfs_file_extent_item
);
1272 extent_type
= btrfs_file_extent_type(eb
, fi
);
1273 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1277 compressed
= btrfs_file_extent_compression(eb
, fi
);
1279 num_bytes
= btrfs_file_extent_num_bytes(eb
, fi
);
1280 disk_byte
= btrfs_file_extent_disk_bytenr(eb
, fi
);
1281 if (disk_byte
== 0) {
1285 logical
= disk_byte
+ btrfs_file_extent_offset(eb
, fi
);
1287 down_read(&sctx
->send_root
->fs_info
->commit_root_sem
);
1288 ret
= extent_from_logical(sctx
->send_root
->fs_info
, disk_byte
, tmp_path
,
1289 &found_key
, &flags
);
1290 up_read(&sctx
->send_root
->fs_info
->commit_root_sem
);
1291 btrfs_release_path(tmp_path
);
1295 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1301 * Setup the clone roots.
1303 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1304 cur_clone_root
= sctx
->clone_roots
+ i
;
1305 cur_clone_root
->ino
= (u64
)-1;
1306 cur_clone_root
->offset
= 0;
1307 cur_clone_root
->found_refs
= 0;
1310 backref_ctx
->sctx
= sctx
;
1311 backref_ctx
->found
= 0;
1312 backref_ctx
->cur_objectid
= ino
;
1313 backref_ctx
->cur_offset
= data_offset
;
1314 backref_ctx
->found_itself
= 0;
1315 backref_ctx
->extent_len
= num_bytes
;
1318 * The last extent of a file may be too large due to page alignment.
1319 * We need to adjust extent_len in this case so that the checks in
1320 * __iterate_backrefs work.
1322 if (data_offset
+ num_bytes
>= ino_size
)
1323 backref_ctx
->extent_len
= ino_size
- data_offset
;
1326 * Now collect all backrefs.
1328 if (compressed
== BTRFS_COMPRESS_NONE
)
1329 extent_item_pos
= logical
- found_key
.objectid
;
1331 extent_item_pos
= 0;
1332 ret
= iterate_extent_inodes(sctx
->send_root
->fs_info
,
1333 found_key
.objectid
, extent_item_pos
, 1,
1334 __iterate_backrefs
, backref_ctx
);
1339 if (!backref_ctx
->found_itself
) {
1340 /* found a bug in backref code? */
1342 btrfs_err(sctx
->send_root
->fs_info
, "did not find backref in "
1343 "send_root. inode=%llu, offset=%llu, "
1344 "disk_byte=%llu found extent=%llu\n",
1345 ino
, data_offset
, disk_byte
, found_key
.objectid
);
1349 verbose_printk(KERN_DEBUG
"btrfs: find_extent_clone: data_offset=%llu, "
1351 "num_bytes=%llu, logical=%llu\n",
1352 data_offset
, ino
, num_bytes
, logical
);
1354 if (!backref_ctx
->found
)
1355 verbose_printk("btrfs: no clones found\n");
1357 cur_clone_root
= NULL
;
1358 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1359 if (sctx
->clone_roots
[i
].found_refs
) {
1360 if (!cur_clone_root
)
1361 cur_clone_root
= sctx
->clone_roots
+ i
;
1362 else if (sctx
->clone_roots
[i
].root
== sctx
->send_root
)
1363 /* prefer clones from send_root over others */
1364 cur_clone_root
= sctx
->clone_roots
+ i
;
1369 if (cur_clone_root
) {
1370 if (compressed
!= BTRFS_COMPRESS_NONE
) {
1372 * Offsets given by iterate_extent_inodes() are relative
1373 * to the start of the extent, we need to add logical
1374 * offset from the file extent item.
1375 * (See why at backref.c:check_extent_in_eb())
1377 cur_clone_root
->offset
+= btrfs_file_extent_offset(eb
,
1380 *found
= cur_clone_root
;
1387 btrfs_free_path(tmp_path
);
1392 static int read_symlink(struct btrfs_root
*root
,
1394 struct fs_path
*dest
)
1397 struct btrfs_path
*path
;
1398 struct btrfs_key key
;
1399 struct btrfs_file_extent_item
*ei
;
1405 path
= alloc_path_for_send();
1410 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1412 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1417 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1418 struct btrfs_file_extent_item
);
1419 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
1420 compression
= btrfs_file_extent_compression(path
->nodes
[0], ei
);
1421 BUG_ON(type
!= BTRFS_FILE_EXTENT_INLINE
);
1422 BUG_ON(compression
);
1424 off
= btrfs_file_extent_inline_start(ei
);
1425 len
= btrfs_file_extent_inline_len(path
->nodes
[0], path
->slots
[0], ei
);
1427 ret
= fs_path_add_from_extent_buffer(dest
, path
->nodes
[0], off
, len
);
1430 btrfs_free_path(path
);
1435 * Helper function to generate a file name that is unique in the root of
1436 * send_root and parent_root. This is used to generate names for orphan inodes.
1438 static int gen_unique_name(struct send_ctx
*sctx
,
1440 struct fs_path
*dest
)
1443 struct btrfs_path
*path
;
1444 struct btrfs_dir_item
*di
;
1449 path
= alloc_path_for_send();
1454 len
= snprintf(tmp
, sizeof(tmp
), "o%llu-%llu-%llu",
1456 ASSERT(len
< sizeof(tmp
));
1458 di
= btrfs_lookup_dir_item(NULL
, sctx
->send_root
,
1459 path
, BTRFS_FIRST_FREE_OBJECTID
,
1460 tmp
, strlen(tmp
), 0);
1461 btrfs_release_path(path
);
1467 /* not unique, try again */
1472 if (!sctx
->parent_root
) {
1478 di
= btrfs_lookup_dir_item(NULL
, sctx
->parent_root
,
1479 path
, BTRFS_FIRST_FREE_OBJECTID
,
1480 tmp
, strlen(tmp
), 0);
1481 btrfs_release_path(path
);
1487 /* not unique, try again */
1495 ret
= fs_path_add(dest
, tmp
, strlen(tmp
));
1498 btrfs_free_path(path
);
1503 inode_state_no_change
,
1504 inode_state_will_create
,
1505 inode_state_did_create
,
1506 inode_state_will_delete
,
1507 inode_state_did_delete
,
1510 static int get_cur_inode_state(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1518 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &left_gen
, NULL
, NULL
,
1520 if (ret
< 0 && ret
!= -ENOENT
)
1524 if (!sctx
->parent_root
) {
1525 right_ret
= -ENOENT
;
1527 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
, &right_gen
,
1528 NULL
, NULL
, NULL
, NULL
);
1529 if (ret
< 0 && ret
!= -ENOENT
)
1534 if (!left_ret
&& !right_ret
) {
1535 if (left_gen
== gen
&& right_gen
== gen
) {
1536 ret
= inode_state_no_change
;
1537 } else if (left_gen
== gen
) {
1538 if (ino
< sctx
->send_progress
)
1539 ret
= inode_state_did_create
;
1541 ret
= inode_state_will_create
;
1542 } else if (right_gen
== gen
) {
1543 if (ino
< sctx
->send_progress
)
1544 ret
= inode_state_did_delete
;
1546 ret
= inode_state_will_delete
;
1550 } else if (!left_ret
) {
1551 if (left_gen
== gen
) {
1552 if (ino
< sctx
->send_progress
)
1553 ret
= inode_state_did_create
;
1555 ret
= inode_state_will_create
;
1559 } else if (!right_ret
) {
1560 if (right_gen
== gen
) {
1561 if (ino
< sctx
->send_progress
)
1562 ret
= inode_state_did_delete
;
1564 ret
= inode_state_will_delete
;
1576 static int is_inode_existent(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1580 ret
= get_cur_inode_state(sctx
, ino
, gen
);
1584 if (ret
== inode_state_no_change
||
1585 ret
== inode_state_did_create
||
1586 ret
== inode_state_will_delete
)
1596 * Helper function to lookup a dir item in a dir.
1598 static int lookup_dir_item_inode(struct btrfs_root
*root
,
1599 u64 dir
, const char *name
, int name_len
,
1604 struct btrfs_dir_item
*di
;
1605 struct btrfs_key key
;
1606 struct btrfs_path
*path
;
1608 path
= alloc_path_for_send();
1612 di
= btrfs_lookup_dir_item(NULL
, root
, path
,
1613 dir
, name
, name_len
, 0);
1622 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1623 *found_inode
= key
.objectid
;
1624 *found_type
= btrfs_dir_type(path
->nodes
[0], di
);
1627 btrfs_free_path(path
);
1632 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1633 * generation of the parent dir and the name of the dir entry.
1635 static int get_first_ref(struct btrfs_root
*root
, u64 ino
,
1636 u64
*dir
, u64
*dir_gen
, struct fs_path
*name
)
1639 struct btrfs_key key
;
1640 struct btrfs_key found_key
;
1641 struct btrfs_path
*path
;
1645 path
= alloc_path_for_send();
1650 key
.type
= BTRFS_INODE_REF_KEY
;
1653 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
1657 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1659 if (ret
|| found_key
.objectid
!= ino
||
1660 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1661 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1666 if (key
.type
== BTRFS_INODE_REF_KEY
) {
1667 struct btrfs_inode_ref
*iref
;
1668 iref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1669 struct btrfs_inode_ref
);
1670 len
= btrfs_inode_ref_name_len(path
->nodes
[0], iref
);
1671 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1672 (unsigned long)(iref
+ 1),
1674 parent_dir
= found_key
.offset
;
1676 struct btrfs_inode_extref
*extref
;
1677 extref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1678 struct btrfs_inode_extref
);
1679 len
= btrfs_inode_extref_name_len(path
->nodes
[0], extref
);
1680 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1681 (unsigned long)&extref
->name
, len
);
1682 parent_dir
= btrfs_inode_extref_parent(path
->nodes
[0], extref
);
1686 btrfs_release_path(path
);
1688 ret
= get_inode_info(root
, parent_dir
, NULL
, dir_gen
, NULL
, NULL
,
1696 btrfs_free_path(path
);
1700 static int is_first_ref(struct btrfs_root
*root
,
1702 const char *name
, int name_len
)
1705 struct fs_path
*tmp_name
;
1709 tmp_name
= fs_path_alloc();
1713 ret
= get_first_ref(root
, ino
, &tmp_dir
, &tmp_dir_gen
, tmp_name
);
1717 if (dir
!= tmp_dir
|| name_len
!= fs_path_len(tmp_name
)) {
1722 ret
= !memcmp(tmp_name
->start
, name
, name_len
);
1725 fs_path_free(tmp_name
);
1730 * Used by process_recorded_refs to determine if a new ref would overwrite an
1731 * already existing ref. In case it detects an overwrite, it returns the
1732 * inode/gen in who_ino/who_gen.
1733 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1734 * to make sure later references to the overwritten inode are possible.
1735 * Orphanizing is however only required for the first ref of an inode.
1736 * process_recorded_refs does an additional is_first_ref check to see if
1737 * orphanizing is really required.
1739 static int will_overwrite_ref(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
1740 const char *name
, int name_len
,
1741 u64
*who_ino
, u64
*who_gen
)
1745 u64 other_inode
= 0;
1748 if (!sctx
->parent_root
)
1751 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1756 * If we have a parent root we need to verify that the parent dir was
1757 * not delted and then re-created, if it was then we have no overwrite
1758 * and we can just unlink this entry.
1760 if (sctx
->parent_root
) {
1761 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &gen
, NULL
,
1763 if (ret
< 0 && ret
!= -ENOENT
)
1773 ret
= lookup_dir_item_inode(sctx
->parent_root
, dir
, name
, name_len
,
1774 &other_inode
, &other_type
);
1775 if (ret
< 0 && ret
!= -ENOENT
)
1783 * Check if the overwritten ref was already processed. If yes, the ref
1784 * was already unlinked/moved, so we can safely assume that we will not
1785 * overwrite anything at this point in time.
1787 if (other_inode
> sctx
->send_progress
) {
1788 ret
= get_inode_info(sctx
->parent_root
, other_inode
, NULL
,
1789 who_gen
, NULL
, NULL
, NULL
, NULL
);
1794 *who_ino
= other_inode
;
1804 * Checks if the ref was overwritten by an already processed inode. This is
1805 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1806 * thus the orphan name needs be used.
1807 * process_recorded_refs also uses it to avoid unlinking of refs that were
1810 static int did_overwrite_ref(struct send_ctx
*sctx
,
1811 u64 dir
, u64 dir_gen
,
1812 u64 ino
, u64 ino_gen
,
1813 const char *name
, int name_len
)
1820 if (!sctx
->parent_root
)
1823 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1827 /* check if the ref was overwritten by another ref */
1828 ret
= lookup_dir_item_inode(sctx
->send_root
, dir
, name
, name_len
,
1829 &ow_inode
, &other_type
);
1830 if (ret
< 0 && ret
!= -ENOENT
)
1833 /* was never and will never be overwritten */
1838 ret
= get_inode_info(sctx
->send_root
, ow_inode
, NULL
, &gen
, NULL
, NULL
,
1843 if (ow_inode
== ino
&& gen
== ino_gen
) {
1848 /* we know that it is or will be overwritten. check this now */
1849 if (ow_inode
< sctx
->send_progress
)
1859 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1860 * that got overwritten. This is used by process_recorded_refs to determine
1861 * if it has to use the path as returned by get_cur_path or the orphan name.
1863 static int did_overwrite_first_ref(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1866 struct fs_path
*name
= NULL
;
1870 if (!sctx
->parent_root
)
1873 name
= fs_path_alloc();
1877 ret
= get_first_ref(sctx
->parent_root
, ino
, &dir
, &dir_gen
, name
);
1881 ret
= did_overwrite_ref(sctx
, dir
, dir_gen
, ino
, gen
,
1882 name
->start
, fs_path_len(name
));
1890 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1891 * so we need to do some special handling in case we have clashes. This function
1892 * takes care of this with the help of name_cache_entry::radix_list.
1893 * In case of error, nce is kfreed.
1895 static int name_cache_insert(struct send_ctx
*sctx
,
1896 struct name_cache_entry
*nce
)
1899 struct list_head
*nce_head
;
1901 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1902 (unsigned long)nce
->ino
);
1904 nce_head
= kmalloc(sizeof(*nce_head
), GFP_NOFS
);
1909 INIT_LIST_HEAD(nce_head
);
1911 ret
= radix_tree_insert(&sctx
->name_cache
, nce
->ino
, nce_head
);
1918 list_add_tail(&nce
->radix_list
, nce_head
);
1919 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
1920 sctx
->name_cache_size
++;
1925 static void name_cache_delete(struct send_ctx
*sctx
,
1926 struct name_cache_entry
*nce
)
1928 struct list_head
*nce_head
;
1930 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1931 (unsigned long)nce
->ino
);
1933 btrfs_err(sctx
->send_root
->fs_info
,
1934 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1935 nce
->ino
, sctx
->name_cache_size
);
1938 list_del(&nce
->radix_list
);
1939 list_del(&nce
->list
);
1940 sctx
->name_cache_size
--;
1943 * We may not get to the final release of nce_head if the lookup fails
1945 if (nce_head
&& list_empty(nce_head
)) {
1946 radix_tree_delete(&sctx
->name_cache
, (unsigned long)nce
->ino
);
1951 static struct name_cache_entry
*name_cache_search(struct send_ctx
*sctx
,
1954 struct list_head
*nce_head
;
1955 struct name_cache_entry
*cur
;
1957 nce_head
= radix_tree_lookup(&sctx
->name_cache
, (unsigned long)ino
);
1961 list_for_each_entry(cur
, nce_head
, radix_list
) {
1962 if (cur
->ino
== ino
&& cur
->gen
== gen
)
1969 * Removes the entry from the list and adds it back to the end. This marks the
1970 * entry as recently used so that name_cache_clean_unused does not remove it.
1972 static void name_cache_used(struct send_ctx
*sctx
, struct name_cache_entry
*nce
)
1974 list_del(&nce
->list
);
1975 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
1979 * Remove some entries from the beginning of name_cache_list.
1981 static void name_cache_clean_unused(struct send_ctx
*sctx
)
1983 struct name_cache_entry
*nce
;
1985 if (sctx
->name_cache_size
< SEND_CTX_NAME_CACHE_CLEAN_SIZE
)
1988 while (sctx
->name_cache_size
> SEND_CTX_MAX_NAME_CACHE_SIZE
) {
1989 nce
= list_entry(sctx
->name_cache_list
.next
,
1990 struct name_cache_entry
, list
);
1991 name_cache_delete(sctx
, nce
);
1996 static void name_cache_free(struct send_ctx
*sctx
)
1998 struct name_cache_entry
*nce
;
2000 while (!list_empty(&sctx
->name_cache_list
)) {
2001 nce
= list_entry(sctx
->name_cache_list
.next
,
2002 struct name_cache_entry
, list
);
2003 name_cache_delete(sctx
, nce
);
2009 * Used by get_cur_path for each ref up to the root.
2010 * Returns 0 if it succeeded.
2011 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2012 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2013 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2014 * Returns <0 in case of error.
2016 static int __get_cur_name_and_parent(struct send_ctx
*sctx
,
2020 struct fs_path
*dest
)
2024 struct btrfs_path
*path
= NULL
;
2025 struct name_cache_entry
*nce
= NULL
;
2028 * First check if we already did a call to this function with the same
2029 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2030 * return the cached result.
2032 nce
= name_cache_search(sctx
, ino
, gen
);
2034 if (ino
< sctx
->send_progress
&& nce
->need_later_update
) {
2035 name_cache_delete(sctx
, nce
);
2039 name_cache_used(sctx
, nce
);
2040 *parent_ino
= nce
->parent_ino
;
2041 *parent_gen
= nce
->parent_gen
;
2042 ret
= fs_path_add(dest
, nce
->name
, nce
->name_len
);
2050 path
= alloc_path_for_send();
2055 * If the inode is not existent yet, add the orphan name and return 1.
2056 * This should only happen for the parent dir that we determine in
2059 ret
= is_inode_existent(sctx
, ino
, gen
);
2064 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2072 * Depending on whether the inode was already processed or not, use
2073 * send_root or parent_root for ref lookup.
2075 if (ino
< sctx
->send_progress
)
2076 ret
= get_first_ref(sctx
->send_root
, ino
,
2077 parent_ino
, parent_gen
, dest
);
2079 ret
= get_first_ref(sctx
->parent_root
, ino
,
2080 parent_ino
, parent_gen
, dest
);
2085 * Check if the ref was overwritten by an inode's ref that was processed
2086 * earlier. If yes, treat as orphan and return 1.
2088 ret
= did_overwrite_ref(sctx
, *parent_ino
, *parent_gen
, ino
, gen
,
2089 dest
->start
, dest
->end
- dest
->start
);
2093 fs_path_reset(dest
);
2094 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2102 * Store the result of the lookup in the name cache.
2104 nce
= kmalloc(sizeof(*nce
) + fs_path_len(dest
) + 1, GFP_NOFS
);
2112 nce
->parent_ino
= *parent_ino
;
2113 nce
->parent_gen
= *parent_gen
;
2114 nce
->name_len
= fs_path_len(dest
);
2116 strcpy(nce
->name
, dest
->start
);
2118 if (ino
< sctx
->send_progress
)
2119 nce
->need_later_update
= 0;
2121 nce
->need_later_update
= 1;
2123 nce_ret
= name_cache_insert(sctx
, nce
);
2126 name_cache_clean_unused(sctx
);
2129 btrfs_free_path(path
);
2134 * Magic happens here. This function returns the first ref to an inode as it
2135 * would look like while receiving the stream at this point in time.
2136 * We walk the path up to the root. For every inode in between, we check if it
2137 * was already processed/sent. If yes, we continue with the parent as found
2138 * in send_root. If not, we continue with the parent as found in parent_root.
2139 * If we encounter an inode that was deleted at this point in time, we use the
2140 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2141 * that were not created yet and overwritten inodes/refs.
2143 * When do we have have orphan inodes:
2144 * 1. When an inode is freshly created and thus no valid refs are available yet
2145 * 2. When a directory lost all it's refs (deleted) but still has dir items
2146 * inside which were not processed yet (pending for move/delete). If anyone
2147 * tried to get the path to the dir items, it would get a path inside that
2149 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2150 * of an unprocessed inode. If in that case the first ref would be
2151 * overwritten, the overwritten inode gets "orphanized". Later when we
2152 * process this overwritten inode, it is restored at a new place by moving
2155 * sctx->send_progress tells this function at which point in time receiving
2158 static int get_cur_path(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2159 struct fs_path
*dest
)
2162 struct fs_path
*name
= NULL
;
2163 u64 parent_inode
= 0;
2167 name
= fs_path_alloc();
2174 fs_path_reset(dest
);
2176 while (!stop
&& ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
2177 fs_path_reset(name
);
2179 if (is_waiting_for_rm(sctx
, ino
)) {
2180 ret
= gen_unique_name(sctx
, ino
, gen
, name
);
2183 ret
= fs_path_add_path(dest
, name
);
2187 if (is_waiting_for_move(sctx
, ino
)) {
2188 ret
= get_first_ref(sctx
->parent_root
, ino
,
2189 &parent_inode
, &parent_gen
, name
);
2191 ret
= __get_cur_name_and_parent(sctx
, ino
, gen
,
2201 ret
= fs_path_add_path(dest
, name
);
2212 fs_path_unreverse(dest
);
2217 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2219 static int send_subvol_begin(struct send_ctx
*sctx
)
2222 struct btrfs_root
*send_root
= sctx
->send_root
;
2223 struct btrfs_root
*parent_root
= sctx
->parent_root
;
2224 struct btrfs_path
*path
;
2225 struct btrfs_key key
;
2226 struct btrfs_root_ref
*ref
;
2227 struct extent_buffer
*leaf
;
2231 path
= btrfs_alloc_path();
2235 name
= kmalloc(BTRFS_PATH_NAME_MAX
, GFP_NOFS
);
2237 btrfs_free_path(path
);
2241 key
.objectid
= send_root
->objectid
;
2242 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2245 ret
= btrfs_search_slot_for_read(send_root
->fs_info
->tree_root
,
2254 leaf
= path
->nodes
[0];
2255 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2256 if (key
.type
!= BTRFS_ROOT_BACKREF_KEY
||
2257 key
.objectid
!= send_root
->objectid
) {
2261 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
2262 namelen
= btrfs_root_ref_name_len(leaf
, ref
);
2263 read_extent_buffer(leaf
, name
, (unsigned long)(ref
+ 1), namelen
);
2264 btrfs_release_path(path
);
2267 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SNAPSHOT
);
2271 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SUBVOL
);
2276 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_PATH
, name
, namelen
);
2277 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_UUID
,
2278 sctx
->send_root
->root_item
.uuid
);
2279 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CTRANSID
,
2280 le64_to_cpu(sctx
->send_root
->root_item
.ctransid
));
2282 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
2283 sctx
->parent_root
->root_item
.uuid
);
2284 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
2285 le64_to_cpu(sctx
->parent_root
->root_item
.ctransid
));
2288 ret
= send_cmd(sctx
);
2292 btrfs_free_path(path
);
2297 static int send_truncate(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 size
)
2302 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino
, size
);
2304 p
= fs_path_alloc();
2308 ret
= begin_cmd(sctx
, BTRFS_SEND_C_TRUNCATE
);
2312 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2315 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2316 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, size
);
2318 ret
= send_cmd(sctx
);
2326 static int send_chmod(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 mode
)
2331 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino
, mode
);
2333 p
= fs_path_alloc();
2337 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHMOD
);
2341 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2344 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2345 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
& 07777);
2347 ret
= send_cmd(sctx
);
2355 static int send_chown(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 uid
, u64 gid
)
2360 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino
, uid
, gid
);
2362 p
= fs_path_alloc();
2366 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHOWN
);
2370 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2373 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2374 TLV_PUT_U64(sctx
, BTRFS_SEND_A_UID
, uid
);
2375 TLV_PUT_U64(sctx
, BTRFS_SEND_A_GID
, gid
);
2377 ret
= send_cmd(sctx
);
2385 static int send_utimes(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
2388 struct fs_path
*p
= NULL
;
2389 struct btrfs_inode_item
*ii
;
2390 struct btrfs_path
*path
= NULL
;
2391 struct extent_buffer
*eb
;
2392 struct btrfs_key key
;
2395 verbose_printk("btrfs: send_utimes %llu\n", ino
);
2397 p
= fs_path_alloc();
2401 path
= alloc_path_for_send();
2408 key
.type
= BTRFS_INODE_ITEM_KEY
;
2410 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2414 eb
= path
->nodes
[0];
2415 slot
= path
->slots
[0];
2416 ii
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
2418 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UTIMES
);
2422 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2425 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2426 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_ATIME
, eb
,
2427 btrfs_inode_atime(ii
));
2428 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_MTIME
, eb
,
2429 btrfs_inode_mtime(ii
));
2430 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_CTIME
, eb
,
2431 btrfs_inode_ctime(ii
));
2432 /* TODO Add otime support when the otime patches get into upstream */
2434 ret
= send_cmd(sctx
);
2439 btrfs_free_path(path
);
2444 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2445 * a valid path yet because we did not process the refs yet. So, the inode
2446 * is created as orphan.
2448 static int send_create_inode(struct send_ctx
*sctx
, u64 ino
)
2457 verbose_printk("btrfs: send_create_inode %llu\n", ino
);
2459 p
= fs_path_alloc();
2463 if (ino
!= sctx
->cur_ino
) {
2464 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &gen
, &mode
,
2469 gen
= sctx
->cur_inode_gen
;
2470 mode
= sctx
->cur_inode_mode
;
2471 rdev
= sctx
->cur_inode_rdev
;
2474 if (S_ISREG(mode
)) {
2475 cmd
= BTRFS_SEND_C_MKFILE
;
2476 } else if (S_ISDIR(mode
)) {
2477 cmd
= BTRFS_SEND_C_MKDIR
;
2478 } else if (S_ISLNK(mode
)) {
2479 cmd
= BTRFS_SEND_C_SYMLINK
;
2480 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
2481 cmd
= BTRFS_SEND_C_MKNOD
;
2482 } else if (S_ISFIFO(mode
)) {
2483 cmd
= BTRFS_SEND_C_MKFIFO
;
2484 } else if (S_ISSOCK(mode
)) {
2485 cmd
= BTRFS_SEND_C_MKSOCK
;
2487 printk(KERN_WARNING
"btrfs: unexpected inode type %o",
2488 (int)(mode
& S_IFMT
));
2493 ret
= begin_cmd(sctx
, cmd
);
2497 ret
= gen_unique_name(sctx
, ino
, gen
, p
);
2501 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2502 TLV_PUT_U64(sctx
, BTRFS_SEND_A_INO
, ino
);
2504 if (S_ISLNK(mode
)) {
2506 ret
= read_symlink(sctx
->send_root
, ino
, p
);
2509 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, p
);
2510 } else if (S_ISCHR(mode
) || S_ISBLK(mode
) ||
2511 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
2512 TLV_PUT_U64(sctx
, BTRFS_SEND_A_RDEV
, new_encode_dev(rdev
));
2513 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
);
2516 ret
= send_cmd(sctx
);
2528 * We need some special handling for inodes that get processed before the parent
2529 * directory got created. See process_recorded_refs for details.
2530 * This function does the check if we already created the dir out of order.
2532 static int did_create_dir(struct send_ctx
*sctx
, u64 dir
)
2535 struct btrfs_path
*path
= NULL
;
2536 struct btrfs_key key
;
2537 struct btrfs_key found_key
;
2538 struct btrfs_key di_key
;
2539 struct extent_buffer
*eb
;
2540 struct btrfs_dir_item
*di
;
2543 path
= alloc_path_for_send();
2550 key
.type
= BTRFS_DIR_INDEX_KEY
;
2552 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2557 eb
= path
->nodes
[0];
2558 slot
= path
->slots
[0];
2559 if (slot
>= btrfs_header_nritems(eb
)) {
2560 ret
= btrfs_next_leaf(sctx
->send_root
, path
);
2563 } else if (ret
> 0) {
2570 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
2571 if (found_key
.objectid
!= key
.objectid
||
2572 found_key
.type
!= key
.type
) {
2577 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
2578 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
2580 if (di_key
.type
!= BTRFS_ROOT_ITEM_KEY
&&
2581 di_key
.objectid
< sctx
->send_progress
) {
2590 btrfs_free_path(path
);
2595 * Only creates the inode if it is:
2596 * 1. Not a directory
2597 * 2. Or a directory which was not created already due to out of order
2598 * directories. See did_create_dir and process_recorded_refs for details.
2600 static int send_create_inode_if_needed(struct send_ctx
*sctx
)
2604 if (S_ISDIR(sctx
->cur_inode_mode
)) {
2605 ret
= did_create_dir(sctx
, sctx
->cur_ino
);
2614 ret
= send_create_inode(sctx
, sctx
->cur_ino
);
2622 struct recorded_ref
{
2623 struct list_head list
;
2626 struct fs_path
*full_path
;
2634 * We need to process new refs before deleted refs, but compare_tree gives us
2635 * everything mixed. So we first record all refs and later process them.
2636 * This function is a helper to record one ref.
2638 static int __record_ref(struct list_head
*head
, u64 dir
,
2639 u64 dir_gen
, struct fs_path
*path
)
2641 struct recorded_ref
*ref
;
2643 ref
= kmalloc(sizeof(*ref
), GFP_NOFS
);
2648 ref
->dir_gen
= dir_gen
;
2649 ref
->full_path
= path
;
2651 ref
->name
= (char *)kbasename(ref
->full_path
->start
);
2652 ref
->name_len
= ref
->full_path
->end
- ref
->name
;
2653 ref
->dir_path
= ref
->full_path
->start
;
2654 if (ref
->name
== ref
->full_path
->start
)
2655 ref
->dir_path_len
= 0;
2657 ref
->dir_path_len
= ref
->full_path
->end
-
2658 ref
->full_path
->start
- 1 - ref
->name_len
;
2660 list_add_tail(&ref
->list
, head
);
2664 static int dup_ref(struct recorded_ref
*ref
, struct list_head
*list
)
2666 struct recorded_ref
*new;
2668 new = kmalloc(sizeof(*ref
), GFP_NOFS
);
2672 new->dir
= ref
->dir
;
2673 new->dir_gen
= ref
->dir_gen
;
2674 new->full_path
= NULL
;
2675 INIT_LIST_HEAD(&new->list
);
2676 list_add_tail(&new->list
, list
);
2680 static void __free_recorded_refs(struct list_head
*head
)
2682 struct recorded_ref
*cur
;
2684 while (!list_empty(head
)) {
2685 cur
= list_entry(head
->next
, struct recorded_ref
, list
);
2686 fs_path_free(cur
->full_path
);
2687 list_del(&cur
->list
);
2692 static void free_recorded_refs(struct send_ctx
*sctx
)
2694 __free_recorded_refs(&sctx
->new_refs
);
2695 __free_recorded_refs(&sctx
->deleted_refs
);
2699 * Renames/moves a file/dir to its orphan name. Used when the first
2700 * ref of an unprocessed inode gets overwritten and for all non empty
2703 static int orphanize_inode(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2704 struct fs_path
*path
)
2707 struct fs_path
*orphan
;
2709 orphan
= fs_path_alloc();
2713 ret
= gen_unique_name(sctx
, ino
, gen
, orphan
);
2717 ret
= send_rename(sctx
, path
, orphan
);
2720 fs_path_free(orphan
);
2724 static struct orphan_dir_info
*
2725 add_orphan_dir_info(struct send_ctx
*sctx
, u64 dir_ino
)
2727 struct rb_node
**p
= &sctx
->orphan_dirs
.rb_node
;
2728 struct rb_node
*parent
= NULL
;
2729 struct orphan_dir_info
*entry
, *odi
;
2731 odi
= kmalloc(sizeof(*odi
), GFP_NOFS
);
2733 return ERR_PTR(-ENOMEM
);
2739 entry
= rb_entry(parent
, struct orphan_dir_info
, node
);
2740 if (dir_ino
< entry
->ino
) {
2742 } else if (dir_ino
> entry
->ino
) {
2743 p
= &(*p
)->rb_right
;
2750 rb_link_node(&odi
->node
, parent
, p
);
2751 rb_insert_color(&odi
->node
, &sctx
->orphan_dirs
);
2755 static struct orphan_dir_info
*
2756 get_orphan_dir_info(struct send_ctx
*sctx
, u64 dir_ino
)
2758 struct rb_node
*n
= sctx
->orphan_dirs
.rb_node
;
2759 struct orphan_dir_info
*entry
;
2762 entry
= rb_entry(n
, struct orphan_dir_info
, node
);
2763 if (dir_ino
< entry
->ino
)
2765 else if (dir_ino
> entry
->ino
)
2773 static int is_waiting_for_rm(struct send_ctx
*sctx
, u64 dir_ino
)
2775 struct orphan_dir_info
*odi
= get_orphan_dir_info(sctx
, dir_ino
);
2780 static void free_orphan_dir_info(struct send_ctx
*sctx
,
2781 struct orphan_dir_info
*odi
)
2785 rb_erase(&odi
->node
, &sctx
->orphan_dirs
);
2790 * Returns 1 if a directory can be removed at this point in time.
2791 * We check this by iterating all dir items and checking if the inode behind
2792 * the dir item was already processed.
2794 static int can_rmdir(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
2798 struct btrfs_root
*root
= sctx
->parent_root
;
2799 struct btrfs_path
*path
;
2800 struct btrfs_key key
;
2801 struct btrfs_key found_key
;
2802 struct btrfs_key loc
;
2803 struct btrfs_dir_item
*di
;
2806 * Don't try to rmdir the top/root subvolume dir.
2808 if (dir
== BTRFS_FIRST_FREE_OBJECTID
)
2811 path
= alloc_path_for_send();
2816 key
.type
= BTRFS_DIR_INDEX_KEY
;
2818 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2823 struct waiting_dir_move
*dm
;
2825 if (path
->slots
[0] >= btrfs_header_nritems(path
->nodes
[0])) {
2826 ret
= btrfs_next_leaf(root
, path
);
2833 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2835 if (found_key
.objectid
!= key
.objectid
||
2836 found_key
.type
!= key
.type
)
2839 di
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2840 struct btrfs_dir_item
);
2841 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &loc
);
2843 dm
= get_waiting_dir_move(sctx
, loc
.objectid
);
2845 struct orphan_dir_info
*odi
;
2847 odi
= add_orphan_dir_info(sctx
, dir
);
2853 dm
->rmdir_ino
= dir
;
2858 if (loc
.objectid
> send_progress
) {
2869 btrfs_free_path(path
);
2873 static int is_waiting_for_move(struct send_ctx
*sctx
, u64 ino
)
2875 struct waiting_dir_move
*entry
= get_waiting_dir_move(sctx
, ino
);
2877 return entry
!= NULL
;
2880 static int add_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
)
2882 struct rb_node
**p
= &sctx
->waiting_dir_moves
.rb_node
;
2883 struct rb_node
*parent
= NULL
;
2884 struct waiting_dir_move
*entry
, *dm
;
2886 dm
= kmalloc(sizeof(*dm
), GFP_NOFS
);
2894 entry
= rb_entry(parent
, struct waiting_dir_move
, node
);
2895 if (ino
< entry
->ino
) {
2897 } else if (ino
> entry
->ino
) {
2898 p
= &(*p
)->rb_right
;
2905 rb_link_node(&dm
->node
, parent
, p
);
2906 rb_insert_color(&dm
->node
, &sctx
->waiting_dir_moves
);
2910 static struct waiting_dir_move
*
2911 get_waiting_dir_move(struct send_ctx
*sctx
, u64 ino
)
2913 struct rb_node
*n
= sctx
->waiting_dir_moves
.rb_node
;
2914 struct waiting_dir_move
*entry
;
2917 entry
= rb_entry(n
, struct waiting_dir_move
, node
);
2918 if (ino
< entry
->ino
)
2920 else if (ino
> entry
->ino
)
2928 static void free_waiting_dir_move(struct send_ctx
*sctx
,
2929 struct waiting_dir_move
*dm
)
2933 rb_erase(&dm
->node
, &sctx
->waiting_dir_moves
);
2937 static int add_pending_dir_move(struct send_ctx
*sctx
,
2942 struct rb_node
**p
= &sctx
->pending_dir_moves
.rb_node
;
2943 struct rb_node
*parent
= NULL
;
2944 struct pending_dir_move
*entry
= NULL
, *pm
;
2945 struct recorded_ref
*cur
;
2949 pm
= kmalloc(sizeof(*pm
), GFP_NOFS
);
2952 pm
->parent_ino
= parent_ino
;
2955 INIT_LIST_HEAD(&pm
->list
);
2956 INIT_LIST_HEAD(&pm
->update_refs
);
2957 RB_CLEAR_NODE(&pm
->node
);
2961 entry
= rb_entry(parent
, struct pending_dir_move
, node
);
2962 if (parent_ino
< entry
->parent_ino
) {
2964 } else if (parent_ino
> entry
->parent_ino
) {
2965 p
= &(*p
)->rb_right
;
2972 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
2973 ret
= dup_ref(cur
, &pm
->update_refs
);
2977 list_for_each_entry(cur
, &sctx
->new_refs
, list
) {
2978 ret
= dup_ref(cur
, &pm
->update_refs
);
2983 ret
= add_waiting_dir_move(sctx
, pm
->ino
);
2988 list_add_tail(&pm
->list
, &entry
->list
);
2990 rb_link_node(&pm
->node
, parent
, p
);
2991 rb_insert_color(&pm
->node
, &sctx
->pending_dir_moves
);
2996 __free_recorded_refs(&pm
->update_refs
);
3002 static struct pending_dir_move
*get_pending_dir_moves(struct send_ctx
*sctx
,
3005 struct rb_node
*n
= sctx
->pending_dir_moves
.rb_node
;
3006 struct pending_dir_move
*entry
;
3009 entry
= rb_entry(n
, struct pending_dir_move
, node
);
3010 if (parent_ino
< entry
->parent_ino
)
3012 else if (parent_ino
> entry
->parent_ino
)
3020 static int apply_dir_move(struct send_ctx
*sctx
, struct pending_dir_move
*pm
)
3022 struct fs_path
*from_path
= NULL
;
3023 struct fs_path
*to_path
= NULL
;
3024 struct fs_path
*name
= NULL
;
3025 u64 orig_progress
= sctx
->send_progress
;
3026 struct recorded_ref
*cur
;
3027 u64 parent_ino
, parent_gen
;
3028 struct waiting_dir_move
*dm
= NULL
;
3032 name
= fs_path_alloc();
3033 from_path
= fs_path_alloc();
3034 if (!name
|| !from_path
) {
3039 dm
= get_waiting_dir_move(sctx
, pm
->ino
);
3041 rmdir_ino
= dm
->rmdir_ino
;
3042 free_waiting_dir_move(sctx
, dm
);
3044 ret
= get_first_ref(sctx
->parent_root
, pm
->ino
,
3045 &parent_ino
, &parent_gen
, name
);
3049 if (parent_ino
== sctx
->cur_ino
) {
3050 /* child only renamed, not moved */
3051 ASSERT(parent_gen
== sctx
->cur_inode_gen
);
3052 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
3056 ret
= fs_path_add_path(from_path
, name
);
3060 /* child moved and maybe renamed too */
3061 sctx
->send_progress
= pm
->ino
;
3062 ret
= get_cur_path(sctx
, pm
->ino
, pm
->gen
, from_path
);
3070 to_path
= fs_path_alloc();
3076 sctx
->send_progress
= sctx
->cur_ino
+ 1;
3077 ret
= get_cur_path(sctx
, pm
->ino
, pm
->gen
, to_path
);
3081 ret
= send_rename(sctx
, from_path
, to_path
);
3086 struct orphan_dir_info
*odi
;
3088 odi
= get_orphan_dir_info(sctx
, rmdir_ino
);
3090 /* already deleted */
3093 ret
= can_rmdir(sctx
, rmdir_ino
, odi
->gen
, sctx
->cur_ino
+ 1);
3099 name
= fs_path_alloc();
3104 ret
= get_cur_path(sctx
, rmdir_ino
, odi
->gen
, name
);
3107 ret
= send_rmdir(sctx
, name
);
3110 free_orphan_dir_info(sctx
, odi
);
3114 ret
= send_utimes(sctx
, pm
->ino
, pm
->gen
);
3119 * After rename/move, need to update the utimes of both new parent(s)
3120 * and old parent(s).
3122 list_for_each_entry(cur
, &pm
->update_refs
, list
) {
3123 if (cur
->dir
== rmdir_ino
)
3125 ret
= send_utimes(sctx
, cur
->dir
, cur
->dir_gen
);
3132 fs_path_free(from_path
);
3133 fs_path_free(to_path
);
3134 sctx
->send_progress
= orig_progress
;
3139 static void free_pending_move(struct send_ctx
*sctx
, struct pending_dir_move
*m
)
3141 if (!list_empty(&m
->list
))
3143 if (!RB_EMPTY_NODE(&m
->node
))
3144 rb_erase(&m
->node
, &sctx
->pending_dir_moves
);
3145 __free_recorded_refs(&m
->update_refs
);
3149 static void tail_append_pending_moves(struct pending_dir_move
*moves
,
3150 struct list_head
*stack
)
3152 if (list_empty(&moves
->list
)) {
3153 list_add_tail(&moves
->list
, stack
);
3156 list_splice_init(&moves
->list
, &list
);
3157 list_add_tail(&moves
->list
, stack
);
3158 list_splice_tail(&list
, stack
);
3162 static int apply_children_dir_moves(struct send_ctx
*sctx
)
3164 struct pending_dir_move
*pm
;
3165 struct list_head stack
;
3166 u64 parent_ino
= sctx
->cur_ino
;
3169 pm
= get_pending_dir_moves(sctx
, parent_ino
);
3173 INIT_LIST_HEAD(&stack
);
3174 tail_append_pending_moves(pm
, &stack
);
3176 while (!list_empty(&stack
)) {
3177 pm
= list_first_entry(&stack
, struct pending_dir_move
, list
);
3178 parent_ino
= pm
->ino
;
3179 ret
= apply_dir_move(sctx
, pm
);
3180 free_pending_move(sctx
, pm
);
3183 pm
= get_pending_dir_moves(sctx
, parent_ino
);
3185 tail_append_pending_moves(pm
, &stack
);
3190 while (!list_empty(&stack
)) {
3191 pm
= list_first_entry(&stack
, struct pending_dir_move
, list
);
3192 free_pending_move(sctx
, pm
);
3197 static int wait_for_parent_move(struct send_ctx
*sctx
,
3198 struct recorded_ref
*parent_ref
)
3201 u64 ino
= parent_ref
->dir
;
3202 u64 parent_ino_before
, parent_ino_after
;
3204 struct fs_path
*path_before
= NULL
;
3205 struct fs_path
*path_after
= NULL
;
3207 int register_upper_dirs
;
3210 if (is_waiting_for_move(sctx
, ino
))
3213 if (parent_ref
->dir
<= sctx
->cur_ino
)
3216 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
, &old_gen
,
3217 NULL
, NULL
, NULL
, NULL
);
3223 if (parent_ref
->dir_gen
!= old_gen
)
3226 path_before
= fs_path_alloc();
3230 ret
= get_first_ref(sctx
->parent_root
, ino
, &parent_ino_before
,
3232 if (ret
== -ENOENT
) {
3235 } else if (ret
< 0) {
3239 path_after
= fs_path_alloc();
3245 ret
= get_first_ref(sctx
->send_root
, ino
, &parent_ino_after
,
3247 if (ret
== -ENOENT
) {
3250 } else if (ret
< 0) {
3254 len1
= fs_path_len(path_before
);
3255 len2
= fs_path_len(path_after
);
3256 if (parent_ino_before
!= parent_ino_after
|| len1
!= len2
||
3257 memcmp(path_before
->start
, path_after
->start
, len1
)) {
3264 * Ok, our new most direct ancestor has a higher inode number but
3265 * wasn't moved/renamed. So maybe some of the new ancestors higher in
3266 * the hierarchy have an higher inode number too *and* were renamed
3267 * or moved - in this case we need to wait for the ancestor's rename
3268 * or move operation before we can do the move/rename for the current
3271 register_upper_dirs
= 0;
3272 ino
= parent_ino_after
;
3274 while ((ret
== 0 || register_upper_dirs
) && ino
> sctx
->cur_ino
) {
3277 fs_path_reset(path_before
);
3278 fs_path_reset(path_after
);
3280 ret
= get_first_ref(sctx
->send_root
, ino
, &parent_ino_after
,
3281 &parent_gen
, path_after
);
3284 ret
= get_first_ref(sctx
->parent_root
, ino
, &parent_ino_before
,
3286 if (ret
== -ENOENT
) {
3289 } else if (ret
< 0) {
3293 len1
= fs_path_len(path_before
);
3294 len2
= fs_path_len(path_after
);
3295 if (parent_ino_before
!= parent_ino_after
|| len1
!= len2
||
3296 memcmp(path_before
->start
, path_after
->start
, len1
)) {
3298 if (register_upper_dirs
) {
3301 register_upper_dirs
= 1;
3302 ino
= parent_ref
->dir
;
3303 gen
= parent_ref
->dir_gen
;
3306 } else if (register_upper_dirs
) {
3307 ret
= add_pending_dir_move(sctx
, ino
, gen
,
3309 if (ret
< 0 && ret
!= -EEXIST
)
3313 ino
= parent_ino_after
;
3318 fs_path_free(path_before
);
3319 fs_path_free(path_after
);
3325 * This does all the move/link/unlink/rmdir magic.
3327 static int process_recorded_refs(struct send_ctx
*sctx
, int *pending_move
)
3330 struct recorded_ref
*cur
;
3331 struct recorded_ref
*cur2
;
3332 struct list_head check_dirs
;
3333 struct fs_path
*valid_path
= NULL
;
3336 int did_overwrite
= 0;
3338 u64 last_dir_ino_rm
= 0;
3340 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx
->cur_ino
);
3343 * This should never happen as the root dir always has the same ref
3344 * which is always '..'
3346 BUG_ON(sctx
->cur_ino
<= BTRFS_FIRST_FREE_OBJECTID
);
3347 INIT_LIST_HEAD(&check_dirs
);
3349 valid_path
= fs_path_alloc();
3356 * First, check if the first ref of the current inode was overwritten
3357 * before. If yes, we know that the current inode was already orphanized
3358 * and thus use the orphan name. If not, we can use get_cur_path to
3359 * get the path of the first ref as it would like while receiving at
3360 * this point in time.
3361 * New inodes are always orphan at the beginning, so force to use the
3362 * orphan name in this case.
3363 * The first ref is stored in valid_path and will be updated if it
3364 * gets moved around.
3366 if (!sctx
->cur_inode_new
) {
3367 ret
= did_overwrite_first_ref(sctx
, sctx
->cur_ino
,
3368 sctx
->cur_inode_gen
);
3374 if (sctx
->cur_inode_new
|| did_overwrite
) {
3375 ret
= gen_unique_name(sctx
, sctx
->cur_ino
,
3376 sctx
->cur_inode_gen
, valid_path
);
3381 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
3387 list_for_each_entry(cur
, &sctx
->new_refs
, list
) {
3389 * We may have refs where the parent directory does not exist
3390 * yet. This happens if the parent directories inum is higher
3391 * the the current inum. To handle this case, we create the
3392 * parent directory out of order. But we need to check if this
3393 * did already happen before due to other refs in the same dir.
3395 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
3398 if (ret
== inode_state_will_create
) {
3401 * First check if any of the current inodes refs did
3402 * already create the dir.
3404 list_for_each_entry(cur2
, &sctx
->new_refs
, list
) {
3407 if (cur2
->dir
== cur
->dir
) {
3414 * If that did not happen, check if a previous inode
3415 * did already create the dir.
3418 ret
= did_create_dir(sctx
, cur
->dir
);
3422 ret
= send_create_inode(sctx
, cur
->dir
);
3429 * Check if this new ref would overwrite the first ref of
3430 * another unprocessed inode. If yes, orphanize the
3431 * overwritten inode. If we find an overwritten ref that is
3432 * not the first ref, simply unlink it.
3434 ret
= will_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
3435 cur
->name
, cur
->name_len
,
3436 &ow_inode
, &ow_gen
);
3440 ret
= is_first_ref(sctx
->parent_root
,
3441 ow_inode
, cur
->dir
, cur
->name
,
3446 ret
= orphanize_inode(sctx
, ow_inode
, ow_gen
,
3451 ret
= send_unlink(sctx
, cur
->full_path
);
3458 * link/move the ref to the new place. If we have an orphan
3459 * inode, move it and update valid_path. If not, link or move
3460 * it depending on the inode mode.
3463 ret
= send_rename(sctx
, valid_path
, cur
->full_path
);
3467 ret
= fs_path_copy(valid_path
, cur
->full_path
);
3471 if (S_ISDIR(sctx
->cur_inode_mode
)) {
3473 * Dirs can't be linked, so move it. For moved
3474 * dirs, we always have one new and one deleted
3475 * ref. The deleted ref is ignored later.
3477 ret
= wait_for_parent_move(sctx
, cur
);
3481 ret
= add_pending_dir_move(sctx
,
3483 sctx
->cur_inode_gen
,
3487 ret
= send_rename(sctx
, valid_path
,
3490 ret
= fs_path_copy(valid_path
,
3496 ret
= send_link(sctx
, cur
->full_path
,
3502 ret
= dup_ref(cur
, &check_dirs
);
3507 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->cur_inode_deleted
) {
3509 * Check if we can already rmdir the directory. If not,
3510 * orphanize it. For every dir item inside that gets deleted
3511 * later, we do this check again and rmdir it then if possible.
3512 * See the use of check_dirs for more details.
3514 ret
= can_rmdir(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
3519 ret
= send_rmdir(sctx
, valid_path
);
3522 } else if (!is_orphan
) {
3523 ret
= orphanize_inode(sctx
, sctx
->cur_ino
,
3524 sctx
->cur_inode_gen
, valid_path
);
3530 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
3531 ret
= dup_ref(cur
, &check_dirs
);
3535 } else if (S_ISDIR(sctx
->cur_inode_mode
) &&
3536 !list_empty(&sctx
->deleted_refs
)) {
3538 * We have a moved dir. Add the old parent to check_dirs
3540 cur
= list_entry(sctx
->deleted_refs
.next
, struct recorded_ref
,
3542 ret
= dup_ref(cur
, &check_dirs
);
3545 } else if (!S_ISDIR(sctx
->cur_inode_mode
)) {
3547 * We have a non dir inode. Go through all deleted refs and
3548 * unlink them if they were not already overwritten by other
3551 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
3552 ret
= did_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
3553 sctx
->cur_ino
, sctx
->cur_inode_gen
,
3554 cur
->name
, cur
->name_len
);
3558 ret
= send_unlink(sctx
, cur
->full_path
);
3562 ret
= dup_ref(cur
, &check_dirs
);
3567 * If the inode is still orphan, unlink the orphan. This may
3568 * happen when a previous inode did overwrite the first ref
3569 * of this inode and no new refs were added for the current
3570 * inode. Unlinking does not mean that the inode is deleted in
3571 * all cases. There may still be links to this inode in other
3575 ret
= send_unlink(sctx
, valid_path
);
3582 * We did collect all parent dirs where cur_inode was once located. We
3583 * now go through all these dirs and check if they are pending for
3584 * deletion and if it's finally possible to perform the rmdir now.
3585 * We also update the inode stats of the parent dirs here.
3587 list_for_each_entry(cur
, &check_dirs
, list
) {
3589 * In case we had refs into dirs that were not processed yet,
3590 * we don't need to do the utime and rmdir logic for these dirs.
3591 * The dir will be processed later.
3593 if (cur
->dir
> sctx
->cur_ino
)
3596 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
3600 if (ret
== inode_state_did_create
||
3601 ret
== inode_state_no_change
) {
3602 /* TODO delayed utimes */
3603 ret
= send_utimes(sctx
, cur
->dir
, cur
->dir_gen
);
3606 } else if (ret
== inode_state_did_delete
&&
3607 cur
->dir
!= last_dir_ino_rm
) {
3608 ret
= can_rmdir(sctx
, cur
->dir
, cur
->dir_gen
,
3613 ret
= get_cur_path(sctx
, cur
->dir
,
3614 cur
->dir_gen
, valid_path
);
3617 ret
= send_rmdir(sctx
, valid_path
);
3620 last_dir_ino_rm
= cur
->dir
;
3628 __free_recorded_refs(&check_dirs
);
3629 free_recorded_refs(sctx
);
3630 fs_path_free(valid_path
);
3634 static int record_ref(struct btrfs_root
*root
, int num
, u64 dir
, int index
,
3635 struct fs_path
*name
, void *ctx
, struct list_head
*refs
)
3638 struct send_ctx
*sctx
= ctx
;
3642 p
= fs_path_alloc();
3646 ret
= get_inode_info(root
, dir
, NULL
, &gen
, NULL
, NULL
,
3651 ret
= get_cur_path(sctx
, dir
, gen
, p
);
3654 ret
= fs_path_add_path(p
, name
);
3658 ret
= __record_ref(refs
, dir
, gen
, p
);
3666 static int __record_new_ref(int num
, u64 dir
, int index
,
3667 struct fs_path
*name
,
3670 struct send_ctx
*sctx
= ctx
;
3671 return record_ref(sctx
->send_root
, num
, dir
, index
, name
,
3672 ctx
, &sctx
->new_refs
);
3676 static int __record_deleted_ref(int num
, u64 dir
, int index
,
3677 struct fs_path
*name
,
3680 struct send_ctx
*sctx
= ctx
;
3681 return record_ref(sctx
->parent_root
, num
, dir
, index
, name
,
3682 ctx
, &sctx
->deleted_refs
);
3685 static int record_new_ref(struct send_ctx
*sctx
)
3689 ret
= iterate_inode_ref(sctx
->send_root
, sctx
->left_path
,
3690 sctx
->cmp_key
, 0, __record_new_ref
, sctx
);
3699 static int record_deleted_ref(struct send_ctx
*sctx
)
3703 ret
= iterate_inode_ref(sctx
->parent_root
, sctx
->right_path
,
3704 sctx
->cmp_key
, 0, __record_deleted_ref
, sctx
);
3713 struct find_ref_ctx
{
3716 struct btrfs_root
*root
;
3717 struct fs_path
*name
;
3721 static int __find_iref(int num
, u64 dir
, int index
,
3722 struct fs_path
*name
,
3725 struct find_ref_ctx
*ctx
= ctx_
;
3729 if (dir
== ctx
->dir
&& fs_path_len(name
) == fs_path_len(ctx
->name
) &&
3730 strncmp(name
->start
, ctx
->name
->start
, fs_path_len(name
)) == 0) {
3732 * To avoid doing extra lookups we'll only do this if everything
3735 ret
= get_inode_info(ctx
->root
, dir
, NULL
, &dir_gen
, NULL
,
3739 if (dir_gen
!= ctx
->dir_gen
)
3741 ctx
->found_idx
= num
;
3747 static int find_iref(struct btrfs_root
*root
,
3748 struct btrfs_path
*path
,
3749 struct btrfs_key
*key
,
3750 u64 dir
, u64 dir_gen
, struct fs_path
*name
)
3753 struct find_ref_ctx ctx
;
3757 ctx
.dir_gen
= dir_gen
;
3761 ret
= iterate_inode_ref(root
, path
, key
, 0, __find_iref
, &ctx
);
3765 if (ctx
.found_idx
== -1)
3768 return ctx
.found_idx
;
3771 static int __record_changed_new_ref(int num
, u64 dir
, int index
,
3772 struct fs_path
*name
,
3777 struct send_ctx
*sctx
= ctx
;
3779 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &dir_gen
, NULL
,
3784 ret
= find_iref(sctx
->parent_root
, sctx
->right_path
,
3785 sctx
->cmp_key
, dir
, dir_gen
, name
);
3787 ret
= __record_new_ref(num
, dir
, index
, name
, sctx
);
3794 static int __record_changed_deleted_ref(int num
, u64 dir
, int index
,
3795 struct fs_path
*name
,
3800 struct send_ctx
*sctx
= ctx
;
3802 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &dir_gen
, NULL
,
3807 ret
= find_iref(sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
3808 dir
, dir_gen
, name
);
3810 ret
= __record_deleted_ref(num
, dir
, index
, name
, sctx
);
3817 static int record_changed_ref(struct send_ctx
*sctx
)
3821 ret
= iterate_inode_ref(sctx
->send_root
, sctx
->left_path
,
3822 sctx
->cmp_key
, 0, __record_changed_new_ref
, sctx
);
3825 ret
= iterate_inode_ref(sctx
->parent_root
, sctx
->right_path
,
3826 sctx
->cmp_key
, 0, __record_changed_deleted_ref
, sctx
);
3836 * Record and process all refs at once. Needed when an inode changes the
3837 * generation number, which means that it was deleted and recreated.
3839 static int process_all_refs(struct send_ctx
*sctx
,
3840 enum btrfs_compare_tree_result cmd
)
3843 struct btrfs_root
*root
;
3844 struct btrfs_path
*path
;
3845 struct btrfs_key key
;
3846 struct btrfs_key found_key
;
3847 struct extent_buffer
*eb
;
3849 iterate_inode_ref_t cb
;
3850 int pending_move
= 0;
3852 path
= alloc_path_for_send();
3856 if (cmd
== BTRFS_COMPARE_TREE_NEW
) {
3857 root
= sctx
->send_root
;
3858 cb
= __record_new_ref
;
3859 } else if (cmd
== BTRFS_COMPARE_TREE_DELETED
) {
3860 root
= sctx
->parent_root
;
3861 cb
= __record_deleted_ref
;
3863 btrfs_err(sctx
->send_root
->fs_info
,
3864 "Wrong command %d in process_all_refs", cmd
);
3869 key
.objectid
= sctx
->cmp_key
->objectid
;
3870 key
.type
= BTRFS_INODE_REF_KEY
;
3872 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3877 eb
= path
->nodes
[0];
3878 slot
= path
->slots
[0];
3879 if (slot
>= btrfs_header_nritems(eb
)) {
3880 ret
= btrfs_next_leaf(root
, path
);
3888 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3890 if (found_key
.objectid
!= key
.objectid
||
3891 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
3892 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
))
3895 ret
= iterate_inode_ref(root
, path
, &found_key
, 0, cb
, sctx
);
3901 btrfs_release_path(path
);
3903 ret
= process_recorded_refs(sctx
, &pending_move
);
3904 /* Only applicable to an incremental send. */
3905 ASSERT(pending_move
== 0);
3908 btrfs_free_path(path
);
3912 static int send_set_xattr(struct send_ctx
*sctx
,
3913 struct fs_path
*path
,
3914 const char *name
, int name_len
,
3915 const char *data
, int data_len
)
3919 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SET_XATTR
);
3923 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
3924 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
3925 TLV_PUT(sctx
, BTRFS_SEND_A_XATTR_DATA
, data
, data_len
);
3927 ret
= send_cmd(sctx
);
3934 static int send_remove_xattr(struct send_ctx
*sctx
,
3935 struct fs_path
*path
,
3936 const char *name
, int name_len
)
3940 ret
= begin_cmd(sctx
, BTRFS_SEND_C_REMOVE_XATTR
);
3944 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
3945 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
3947 ret
= send_cmd(sctx
);
3954 static int __process_new_xattr(int num
, struct btrfs_key
*di_key
,
3955 const char *name
, int name_len
,
3956 const char *data
, int data_len
,
3960 struct send_ctx
*sctx
= ctx
;
3962 posix_acl_xattr_header dummy_acl
;
3964 p
= fs_path_alloc();
3969 * This hack is needed because empty acl's are stored as zero byte
3970 * data in xattrs. Problem with that is, that receiving these zero byte
3971 * acl's will fail later. To fix this, we send a dummy acl list that
3972 * only contains the version number and no entries.
3974 if (!strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
, name_len
) ||
3975 !strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
, name_len
)) {
3976 if (data_len
== 0) {
3977 dummy_acl
.a_version
=
3978 cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
3979 data
= (char *)&dummy_acl
;
3980 data_len
= sizeof(dummy_acl
);
3984 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3988 ret
= send_set_xattr(sctx
, p
, name
, name_len
, data
, data_len
);
3995 static int __process_deleted_xattr(int num
, struct btrfs_key
*di_key
,
3996 const char *name
, int name_len
,
3997 const char *data
, int data_len
,
4001 struct send_ctx
*sctx
= ctx
;
4004 p
= fs_path_alloc();
4008 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4012 ret
= send_remove_xattr(sctx
, p
, name
, name_len
);
4019 static int process_new_xattr(struct send_ctx
*sctx
)
4023 ret
= iterate_dir_item(sctx
->send_root
, sctx
->left_path
,
4024 sctx
->cmp_key
, __process_new_xattr
, sctx
);
4029 static int process_deleted_xattr(struct send_ctx
*sctx
)
4033 ret
= iterate_dir_item(sctx
->parent_root
, sctx
->right_path
,
4034 sctx
->cmp_key
, __process_deleted_xattr
, sctx
);
4039 struct find_xattr_ctx
{
4047 static int __find_xattr(int num
, struct btrfs_key
*di_key
,
4048 const char *name
, int name_len
,
4049 const char *data
, int data_len
,
4050 u8 type
, void *vctx
)
4052 struct find_xattr_ctx
*ctx
= vctx
;
4054 if (name_len
== ctx
->name_len
&&
4055 strncmp(name
, ctx
->name
, name_len
) == 0) {
4056 ctx
->found_idx
= num
;
4057 ctx
->found_data_len
= data_len
;
4058 ctx
->found_data
= kmemdup(data
, data_len
, GFP_NOFS
);
4059 if (!ctx
->found_data
)
4066 static int find_xattr(struct btrfs_root
*root
,
4067 struct btrfs_path
*path
,
4068 struct btrfs_key
*key
,
4069 const char *name
, int name_len
,
4070 char **data
, int *data_len
)
4073 struct find_xattr_ctx ctx
;
4076 ctx
.name_len
= name_len
;
4078 ctx
.found_data
= NULL
;
4079 ctx
.found_data_len
= 0;
4081 ret
= iterate_dir_item(root
, path
, key
, __find_xattr
, &ctx
);
4085 if (ctx
.found_idx
== -1)
4088 *data
= ctx
.found_data
;
4089 *data_len
= ctx
.found_data_len
;
4091 kfree(ctx
.found_data
);
4093 return ctx
.found_idx
;
4097 static int __process_changed_new_xattr(int num
, struct btrfs_key
*di_key
,
4098 const char *name
, int name_len
,
4099 const char *data
, int data_len
,
4103 struct send_ctx
*sctx
= ctx
;
4104 char *found_data
= NULL
;
4105 int found_data_len
= 0;
4107 ret
= find_xattr(sctx
->parent_root
, sctx
->right_path
,
4108 sctx
->cmp_key
, name
, name_len
, &found_data
,
4110 if (ret
== -ENOENT
) {
4111 ret
= __process_new_xattr(num
, di_key
, name
, name_len
, data
,
4112 data_len
, type
, ctx
);
4113 } else if (ret
>= 0) {
4114 if (data_len
!= found_data_len
||
4115 memcmp(data
, found_data
, data_len
)) {
4116 ret
= __process_new_xattr(num
, di_key
, name
, name_len
,
4117 data
, data_len
, type
, ctx
);
4127 static int __process_changed_deleted_xattr(int num
, struct btrfs_key
*di_key
,
4128 const char *name
, int name_len
,
4129 const char *data
, int data_len
,
4133 struct send_ctx
*sctx
= ctx
;
4135 ret
= find_xattr(sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
4136 name
, name_len
, NULL
, NULL
);
4138 ret
= __process_deleted_xattr(num
, di_key
, name
, name_len
, data
,
4139 data_len
, type
, ctx
);
4146 static int process_changed_xattr(struct send_ctx
*sctx
)
4150 ret
= iterate_dir_item(sctx
->send_root
, sctx
->left_path
,
4151 sctx
->cmp_key
, __process_changed_new_xattr
, sctx
);
4154 ret
= iterate_dir_item(sctx
->parent_root
, sctx
->right_path
,
4155 sctx
->cmp_key
, __process_changed_deleted_xattr
, sctx
);
4161 static int process_all_new_xattrs(struct send_ctx
*sctx
)
4164 struct btrfs_root
*root
;
4165 struct btrfs_path
*path
;
4166 struct btrfs_key key
;
4167 struct btrfs_key found_key
;
4168 struct extent_buffer
*eb
;
4171 path
= alloc_path_for_send();
4175 root
= sctx
->send_root
;
4177 key
.objectid
= sctx
->cmp_key
->objectid
;
4178 key
.type
= BTRFS_XATTR_ITEM_KEY
;
4180 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4185 eb
= path
->nodes
[0];
4186 slot
= path
->slots
[0];
4187 if (slot
>= btrfs_header_nritems(eb
)) {
4188 ret
= btrfs_next_leaf(root
, path
);
4191 } else if (ret
> 0) {
4198 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4199 if (found_key
.objectid
!= key
.objectid
||
4200 found_key
.type
!= key
.type
) {
4205 ret
= iterate_dir_item(root
, path
, &found_key
,
4206 __process_new_xattr
, sctx
);
4214 btrfs_free_path(path
);
4218 static ssize_t
fill_read_buf(struct send_ctx
*sctx
, u64 offset
, u32 len
)
4220 struct btrfs_root
*root
= sctx
->send_root
;
4221 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4222 struct inode
*inode
;
4225 struct btrfs_key key
;
4226 pgoff_t index
= offset
>> PAGE_CACHE_SHIFT
;
4228 unsigned pg_offset
= offset
& ~PAGE_CACHE_MASK
;
4231 key
.objectid
= sctx
->cur_ino
;
4232 key
.type
= BTRFS_INODE_ITEM_KEY
;
4235 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
4237 return PTR_ERR(inode
);
4239 if (offset
+ len
> i_size_read(inode
)) {
4240 if (offset
> i_size_read(inode
))
4243 len
= offset
- i_size_read(inode
);
4248 last_index
= (offset
+ len
- 1) >> PAGE_CACHE_SHIFT
;
4250 /* initial readahead */
4251 memset(&sctx
->ra
, 0, sizeof(struct file_ra_state
));
4252 file_ra_state_init(&sctx
->ra
, inode
->i_mapping
);
4253 btrfs_force_ra(inode
->i_mapping
, &sctx
->ra
, NULL
, index
,
4254 last_index
- index
+ 1);
4256 while (index
<= last_index
) {
4257 unsigned cur_len
= min_t(unsigned, len
,
4258 PAGE_CACHE_SIZE
- pg_offset
);
4259 page
= find_or_create_page(inode
->i_mapping
, index
, GFP_NOFS
);
4265 if (!PageUptodate(page
)) {
4266 btrfs_readpage(NULL
, page
);
4268 if (!PageUptodate(page
)) {
4270 page_cache_release(page
);
4277 memcpy(sctx
->read_buf
+ ret
, addr
+ pg_offset
, cur_len
);
4280 page_cache_release(page
);
4292 * Read some bytes from the current inode/file and send a write command to
4295 static int send_write(struct send_ctx
*sctx
, u64 offset
, u32 len
)
4299 ssize_t num_read
= 0;
4301 p
= fs_path_alloc();
4305 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset
, len
);
4307 num_read
= fill_read_buf(sctx
, offset
, len
);
4308 if (num_read
<= 0) {
4314 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
4318 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4322 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4323 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4324 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, num_read
);
4326 ret
= send_cmd(sctx
);
4337 * Send a clone command to user space.
4339 static int send_clone(struct send_ctx
*sctx
,
4340 u64 offset
, u32 len
,
4341 struct clone_root
*clone_root
)
4347 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4348 "clone_inode=%llu, clone_offset=%llu\n", offset
, len
,
4349 clone_root
->root
->objectid
, clone_root
->ino
,
4350 clone_root
->offset
);
4352 p
= fs_path_alloc();
4356 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CLONE
);
4360 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4364 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4365 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_LEN
, len
);
4366 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4368 if (clone_root
->root
== sctx
->send_root
) {
4369 ret
= get_inode_info(sctx
->send_root
, clone_root
->ino
, NULL
,
4370 &gen
, NULL
, NULL
, NULL
, NULL
);
4373 ret
= get_cur_path(sctx
, clone_root
->ino
, gen
, p
);
4375 ret
= get_inode_path(clone_root
->root
, clone_root
->ino
, p
);
4380 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
4381 clone_root
->root
->root_item
.uuid
);
4382 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
4383 le64_to_cpu(clone_root
->root
->root_item
.ctransid
));
4384 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_CLONE_PATH
, p
);
4385 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_OFFSET
,
4386 clone_root
->offset
);
4388 ret
= send_cmd(sctx
);
4397 * Send an update extent command to user space.
4399 static int send_update_extent(struct send_ctx
*sctx
,
4400 u64 offset
, u32 len
)
4405 p
= fs_path_alloc();
4409 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UPDATE_EXTENT
);
4413 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4417 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4418 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4419 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, len
);
4421 ret
= send_cmd(sctx
);
4429 static int send_hole(struct send_ctx
*sctx
, u64 end
)
4431 struct fs_path
*p
= NULL
;
4432 u64 offset
= sctx
->cur_inode_last_extent
;
4436 p
= fs_path_alloc();
4439 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
4441 goto tlv_put_failure
;
4442 memset(sctx
->read_buf
, 0, BTRFS_SEND_READ_SIZE
);
4443 while (offset
< end
) {
4444 len
= min_t(u64
, end
- offset
, BTRFS_SEND_READ_SIZE
);
4446 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
4449 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
4450 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
4451 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, len
);
4452 ret
= send_cmd(sctx
);
4462 static int send_write_or_clone(struct send_ctx
*sctx
,
4463 struct btrfs_path
*path
,
4464 struct btrfs_key
*key
,
4465 struct clone_root
*clone_root
)
4468 struct btrfs_file_extent_item
*ei
;
4469 u64 offset
= key
->offset
;
4474 u64 bs
= sctx
->send_root
->fs_info
->sb
->s_blocksize
;
4476 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4477 struct btrfs_file_extent_item
);
4478 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
4479 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4480 len
= btrfs_file_extent_inline_len(path
->nodes
[0],
4481 path
->slots
[0], ei
);
4483 * it is possible the inline item won't cover the whole page,
4484 * but there may be items after this page. Make
4485 * sure to send the whole thing
4487 len
= PAGE_CACHE_ALIGN(len
);
4489 len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
4492 if (offset
+ len
> sctx
->cur_inode_size
)
4493 len
= sctx
->cur_inode_size
- offset
;
4499 if (clone_root
&& IS_ALIGNED(offset
+ len
, bs
)) {
4500 ret
= send_clone(sctx
, offset
, len
, clone_root
);
4501 } else if (sctx
->flags
& BTRFS_SEND_FLAG_NO_FILE_DATA
) {
4502 ret
= send_update_extent(sctx
, offset
, len
);
4506 if (l
> BTRFS_SEND_READ_SIZE
)
4507 l
= BTRFS_SEND_READ_SIZE
;
4508 ret
= send_write(sctx
, pos
+ offset
, l
);
4521 static int is_extent_unchanged(struct send_ctx
*sctx
,
4522 struct btrfs_path
*left_path
,
4523 struct btrfs_key
*ekey
)
4526 struct btrfs_key key
;
4527 struct btrfs_path
*path
= NULL
;
4528 struct extent_buffer
*eb
;
4530 struct btrfs_key found_key
;
4531 struct btrfs_file_extent_item
*ei
;
4536 u64 left_offset_fixed
;
4544 path
= alloc_path_for_send();
4548 eb
= left_path
->nodes
[0];
4549 slot
= left_path
->slots
[0];
4550 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
4551 left_type
= btrfs_file_extent_type(eb
, ei
);
4553 if (left_type
!= BTRFS_FILE_EXTENT_REG
) {
4557 left_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
4558 left_len
= btrfs_file_extent_num_bytes(eb
, ei
);
4559 left_offset
= btrfs_file_extent_offset(eb
, ei
);
4560 left_gen
= btrfs_file_extent_generation(eb
, ei
);
4563 * Following comments will refer to these graphics. L is the left
4564 * extents which we are checking at the moment. 1-8 are the right
4565 * extents that we iterate.
4568 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4571 * |--1--|-2b-|...(same as above)
4573 * Alternative situation. Happens on files where extents got split.
4575 * |-----------7-----------|-6-|
4577 * Alternative situation. Happens on files which got larger.
4580 * Nothing follows after 8.
4583 key
.objectid
= ekey
->objectid
;
4584 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4585 key
.offset
= ekey
->offset
;
4586 ret
= btrfs_search_slot_for_read(sctx
->parent_root
, &key
, path
, 0, 0);
4595 * Handle special case where the right side has no extents at all.
4597 eb
= path
->nodes
[0];
4598 slot
= path
->slots
[0];
4599 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4600 if (found_key
.objectid
!= key
.objectid
||
4601 found_key
.type
!= key
.type
) {
4602 /* If we're a hole then just pretend nothing changed */
4603 ret
= (left_disknr
) ? 0 : 1;
4608 * We're now on 2a, 2b or 7.
4611 while (key
.offset
< ekey
->offset
+ left_len
) {
4612 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
4613 right_type
= btrfs_file_extent_type(eb
, ei
);
4614 if (right_type
!= BTRFS_FILE_EXTENT_REG
) {
4619 right_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
4620 right_len
= btrfs_file_extent_num_bytes(eb
, ei
);
4621 right_offset
= btrfs_file_extent_offset(eb
, ei
);
4622 right_gen
= btrfs_file_extent_generation(eb
, ei
);
4625 * Are we at extent 8? If yes, we know the extent is changed.
4626 * This may only happen on the first iteration.
4628 if (found_key
.offset
+ right_len
<= ekey
->offset
) {
4629 /* If we're a hole just pretend nothing changed */
4630 ret
= (left_disknr
) ? 0 : 1;
4634 left_offset_fixed
= left_offset
;
4635 if (key
.offset
< ekey
->offset
) {
4636 /* Fix the right offset for 2a and 7. */
4637 right_offset
+= ekey
->offset
- key
.offset
;
4639 /* Fix the left offset for all behind 2a and 2b */
4640 left_offset_fixed
+= key
.offset
- ekey
->offset
;
4644 * Check if we have the same extent.
4646 if (left_disknr
!= right_disknr
||
4647 left_offset_fixed
!= right_offset
||
4648 left_gen
!= right_gen
) {
4654 * Go to the next extent.
4656 ret
= btrfs_next_item(sctx
->parent_root
, path
);
4660 eb
= path
->nodes
[0];
4661 slot
= path
->slots
[0];
4662 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4664 if (ret
|| found_key
.objectid
!= key
.objectid
||
4665 found_key
.type
!= key
.type
) {
4666 key
.offset
+= right_len
;
4669 if (found_key
.offset
!= key
.offset
+ right_len
) {
4677 * We're now behind the left extent (treat as unchanged) or at the end
4678 * of the right side (treat as changed).
4680 if (key
.offset
>= ekey
->offset
+ left_len
)
4687 btrfs_free_path(path
);
4691 static int get_last_extent(struct send_ctx
*sctx
, u64 offset
)
4693 struct btrfs_path
*path
;
4694 struct btrfs_root
*root
= sctx
->send_root
;
4695 struct btrfs_file_extent_item
*fi
;
4696 struct btrfs_key key
;
4701 path
= alloc_path_for_send();
4705 sctx
->cur_inode_last_extent
= 0;
4707 key
.objectid
= sctx
->cur_ino
;
4708 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4709 key
.offset
= offset
;
4710 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 0, 1);
4714 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
4715 if (key
.objectid
!= sctx
->cur_ino
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
4718 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4719 struct btrfs_file_extent_item
);
4720 type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
4721 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4722 u64 size
= btrfs_file_extent_inline_len(path
->nodes
[0],
4723 path
->slots
[0], fi
);
4724 extent_end
= ALIGN(key
.offset
+ size
,
4725 sctx
->send_root
->sectorsize
);
4727 extent_end
= key
.offset
+
4728 btrfs_file_extent_num_bytes(path
->nodes
[0], fi
);
4730 sctx
->cur_inode_last_extent
= extent_end
;
4732 btrfs_free_path(path
);
4736 static int maybe_send_hole(struct send_ctx
*sctx
, struct btrfs_path
*path
,
4737 struct btrfs_key
*key
)
4739 struct btrfs_file_extent_item
*fi
;
4744 if (sctx
->cur_ino
!= key
->objectid
|| !need_send_hole(sctx
))
4747 if (sctx
->cur_inode_last_extent
== (u64
)-1) {
4748 ret
= get_last_extent(sctx
, key
->offset
- 1);
4753 fi
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4754 struct btrfs_file_extent_item
);
4755 type
= btrfs_file_extent_type(path
->nodes
[0], fi
);
4756 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
4757 u64 size
= btrfs_file_extent_inline_len(path
->nodes
[0],
4758 path
->slots
[0], fi
);
4759 extent_end
= ALIGN(key
->offset
+ size
,
4760 sctx
->send_root
->sectorsize
);
4762 extent_end
= key
->offset
+
4763 btrfs_file_extent_num_bytes(path
->nodes
[0], fi
);
4766 if (path
->slots
[0] == 0 &&
4767 sctx
->cur_inode_last_extent
< key
->offset
) {
4769 * We might have skipped entire leafs that contained only
4770 * file extent items for our current inode. These leafs have
4771 * a generation number smaller (older) than the one in the
4772 * current leaf and the leaf our last extent came from, and
4773 * are located between these 2 leafs.
4775 ret
= get_last_extent(sctx
, key
->offset
- 1);
4780 if (sctx
->cur_inode_last_extent
< key
->offset
)
4781 ret
= send_hole(sctx
, key
->offset
);
4782 sctx
->cur_inode_last_extent
= extent_end
;
4786 static int process_extent(struct send_ctx
*sctx
,
4787 struct btrfs_path
*path
,
4788 struct btrfs_key
*key
)
4790 struct clone_root
*found_clone
= NULL
;
4793 if (S_ISLNK(sctx
->cur_inode_mode
))
4796 if (sctx
->parent_root
&& !sctx
->cur_inode_new
) {
4797 ret
= is_extent_unchanged(sctx
, path
, key
);
4805 struct btrfs_file_extent_item
*ei
;
4808 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
4809 struct btrfs_file_extent_item
);
4810 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
4811 if (type
== BTRFS_FILE_EXTENT_PREALLOC
||
4812 type
== BTRFS_FILE_EXTENT_REG
) {
4814 * The send spec does not have a prealloc command yet,
4815 * so just leave a hole for prealloc'ed extents until
4816 * we have enough commands queued up to justify rev'ing
4819 if (type
== BTRFS_FILE_EXTENT_PREALLOC
) {
4824 /* Have a hole, just skip it. */
4825 if (btrfs_file_extent_disk_bytenr(path
->nodes
[0], ei
) == 0) {
4832 ret
= find_extent_clone(sctx
, path
, key
->objectid
, key
->offset
,
4833 sctx
->cur_inode_size
, &found_clone
);
4834 if (ret
!= -ENOENT
&& ret
< 0)
4837 ret
= send_write_or_clone(sctx
, path
, key
, found_clone
);
4841 ret
= maybe_send_hole(sctx
, path
, key
);
4846 static int process_all_extents(struct send_ctx
*sctx
)
4849 struct btrfs_root
*root
;
4850 struct btrfs_path
*path
;
4851 struct btrfs_key key
;
4852 struct btrfs_key found_key
;
4853 struct extent_buffer
*eb
;
4856 root
= sctx
->send_root
;
4857 path
= alloc_path_for_send();
4861 key
.objectid
= sctx
->cmp_key
->objectid
;
4862 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4864 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4869 eb
= path
->nodes
[0];
4870 slot
= path
->slots
[0];
4872 if (slot
>= btrfs_header_nritems(eb
)) {
4873 ret
= btrfs_next_leaf(root
, path
);
4876 } else if (ret
> 0) {
4883 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4885 if (found_key
.objectid
!= key
.objectid
||
4886 found_key
.type
!= key
.type
) {
4891 ret
= process_extent(sctx
, path
, &found_key
);
4899 btrfs_free_path(path
);
4903 static int process_recorded_refs_if_needed(struct send_ctx
*sctx
, int at_end
,
4905 int *refs_processed
)
4909 if (sctx
->cur_ino
== 0)
4911 if (!at_end
&& sctx
->cur_ino
== sctx
->cmp_key
->objectid
&&
4912 sctx
->cmp_key
->type
<= BTRFS_INODE_EXTREF_KEY
)
4914 if (list_empty(&sctx
->new_refs
) && list_empty(&sctx
->deleted_refs
))
4917 ret
= process_recorded_refs(sctx
, pending_move
);
4921 *refs_processed
= 1;
4926 static int finish_inode_if_needed(struct send_ctx
*sctx
, int at_end
)
4937 int pending_move
= 0;
4938 int refs_processed
= 0;
4940 ret
= process_recorded_refs_if_needed(sctx
, at_end
, &pending_move
,
4946 * We have processed the refs and thus need to advance send_progress.
4947 * Now, calls to get_cur_xxx will take the updated refs of the current
4948 * inode into account.
4950 * On the other hand, if our current inode is a directory and couldn't
4951 * be moved/renamed because its parent was renamed/moved too and it has
4952 * a higher inode number, we can only move/rename our current inode
4953 * after we moved/renamed its parent. Therefore in this case operate on
4954 * the old path (pre move/rename) of our current inode, and the
4955 * move/rename will be performed later.
4957 if (refs_processed
&& !pending_move
)
4958 sctx
->send_progress
= sctx
->cur_ino
+ 1;
4960 if (sctx
->cur_ino
== 0 || sctx
->cur_inode_deleted
)
4962 if (!at_end
&& sctx
->cmp_key
->objectid
== sctx
->cur_ino
)
4965 ret
= get_inode_info(sctx
->send_root
, sctx
->cur_ino
, NULL
, NULL
,
4966 &left_mode
, &left_uid
, &left_gid
, NULL
);
4970 if (!sctx
->parent_root
|| sctx
->cur_inode_new
) {
4972 if (!S_ISLNK(sctx
->cur_inode_mode
))
4975 ret
= get_inode_info(sctx
->parent_root
, sctx
->cur_ino
,
4976 NULL
, NULL
, &right_mode
, &right_uid
,
4981 if (left_uid
!= right_uid
|| left_gid
!= right_gid
)
4983 if (!S_ISLNK(sctx
->cur_inode_mode
) && left_mode
!= right_mode
)
4987 if (S_ISREG(sctx
->cur_inode_mode
)) {
4988 if (need_send_hole(sctx
)) {
4989 if (sctx
->cur_inode_last_extent
== (u64
)-1 ||
4990 sctx
->cur_inode_last_extent
<
4991 sctx
->cur_inode_size
) {
4992 ret
= get_last_extent(sctx
, (u64
)-1);
4996 if (sctx
->cur_inode_last_extent
<
4997 sctx
->cur_inode_size
) {
4998 ret
= send_hole(sctx
, sctx
->cur_inode_size
);
5003 ret
= send_truncate(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
5004 sctx
->cur_inode_size
);
5010 ret
= send_chown(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
5011 left_uid
, left_gid
);
5016 ret
= send_chmod(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
5023 * If other directory inodes depended on our current directory
5024 * inode's move/rename, now do their move/rename operations.
5026 if (!is_waiting_for_move(sctx
, sctx
->cur_ino
)) {
5027 ret
= apply_children_dir_moves(sctx
);
5031 * Need to send that every time, no matter if it actually
5032 * changed between the two trees as we have done changes to
5033 * the inode before. If our inode is a directory and it's
5034 * waiting to be moved/renamed, we will send its utimes when
5035 * it's moved/renamed, therefore we don't need to do it here.
5037 sctx
->send_progress
= sctx
->cur_ino
+ 1;
5038 ret
= send_utimes(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
);
5047 static int changed_inode(struct send_ctx
*sctx
,
5048 enum btrfs_compare_tree_result result
)
5051 struct btrfs_key
*key
= sctx
->cmp_key
;
5052 struct btrfs_inode_item
*left_ii
= NULL
;
5053 struct btrfs_inode_item
*right_ii
= NULL
;
5057 sctx
->cur_ino
= key
->objectid
;
5058 sctx
->cur_inode_new_gen
= 0;
5059 sctx
->cur_inode_last_extent
= (u64
)-1;
5062 * Set send_progress to current inode. This will tell all get_cur_xxx
5063 * functions that the current inode's refs are not updated yet. Later,
5064 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5066 sctx
->send_progress
= sctx
->cur_ino
;
5068 if (result
== BTRFS_COMPARE_TREE_NEW
||
5069 result
== BTRFS_COMPARE_TREE_CHANGED
) {
5070 left_ii
= btrfs_item_ptr(sctx
->left_path
->nodes
[0],
5071 sctx
->left_path
->slots
[0],
5072 struct btrfs_inode_item
);
5073 left_gen
= btrfs_inode_generation(sctx
->left_path
->nodes
[0],
5076 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
5077 sctx
->right_path
->slots
[0],
5078 struct btrfs_inode_item
);
5079 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
5082 if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
5083 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
5084 sctx
->right_path
->slots
[0],
5085 struct btrfs_inode_item
);
5087 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
5091 * The cur_ino = root dir case is special here. We can't treat
5092 * the inode as deleted+reused because it would generate a
5093 * stream that tries to delete/mkdir the root dir.
5095 if (left_gen
!= right_gen
&&
5096 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
5097 sctx
->cur_inode_new_gen
= 1;
5100 if (result
== BTRFS_COMPARE_TREE_NEW
) {
5101 sctx
->cur_inode_gen
= left_gen
;
5102 sctx
->cur_inode_new
= 1;
5103 sctx
->cur_inode_deleted
= 0;
5104 sctx
->cur_inode_size
= btrfs_inode_size(
5105 sctx
->left_path
->nodes
[0], left_ii
);
5106 sctx
->cur_inode_mode
= btrfs_inode_mode(
5107 sctx
->left_path
->nodes
[0], left_ii
);
5108 sctx
->cur_inode_rdev
= btrfs_inode_rdev(
5109 sctx
->left_path
->nodes
[0], left_ii
);
5110 if (sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
5111 ret
= send_create_inode_if_needed(sctx
);
5112 } else if (result
== BTRFS_COMPARE_TREE_DELETED
) {
5113 sctx
->cur_inode_gen
= right_gen
;
5114 sctx
->cur_inode_new
= 0;
5115 sctx
->cur_inode_deleted
= 1;
5116 sctx
->cur_inode_size
= btrfs_inode_size(
5117 sctx
->right_path
->nodes
[0], right_ii
);
5118 sctx
->cur_inode_mode
= btrfs_inode_mode(
5119 sctx
->right_path
->nodes
[0], right_ii
);
5120 } else if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
5122 * We need to do some special handling in case the inode was
5123 * reported as changed with a changed generation number. This
5124 * means that the original inode was deleted and new inode
5125 * reused the same inum. So we have to treat the old inode as
5126 * deleted and the new one as new.
5128 if (sctx
->cur_inode_new_gen
) {
5130 * First, process the inode as if it was deleted.
5132 sctx
->cur_inode_gen
= right_gen
;
5133 sctx
->cur_inode_new
= 0;
5134 sctx
->cur_inode_deleted
= 1;
5135 sctx
->cur_inode_size
= btrfs_inode_size(
5136 sctx
->right_path
->nodes
[0], right_ii
);
5137 sctx
->cur_inode_mode
= btrfs_inode_mode(
5138 sctx
->right_path
->nodes
[0], right_ii
);
5139 ret
= process_all_refs(sctx
,
5140 BTRFS_COMPARE_TREE_DELETED
);
5145 * Now process the inode as if it was new.
5147 sctx
->cur_inode_gen
= left_gen
;
5148 sctx
->cur_inode_new
= 1;
5149 sctx
->cur_inode_deleted
= 0;
5150 sctx
->cur_inode_size
= btrfs_inode_size(
5151 sctx
->left_path
->nodes
[0], left_ii
);
5152 sctx
->cur_inode_mode
= btrfs_inode_mode(
5153 sctx
->left_path
->nodes
[0], left_ii
);
5154 sctx
->cur_inode_rdev
= btrfs_inode_rdev(
5155 sctx
->left_path
->nodes
[0], left_ii
);
5156 ret
= send_create_inode_if_needed(sctx
);
5160 ret
= process_all_refs(sctx
, BTRFS_COMPARE_TREE_NEW
);
5164 * Advance send_progress now as we did not get into
5165 * process_recorded_refs_if_needed in the new_gen case.
5167 sctx
->send_progress
= sctx
->cur_ino
+ 1;
5170 * Now process all extents and xattrs of the inode as if
5171 * they were all new.
5173 ret
= process_all_extents(sctx
);
5176 ret
= process_all_new_xattrs(sctx
);
5180 sctx
->cur_inode_gen
= left_gen
;
5181 sctx
->cur_inode_new
= 0;
5182 sctx
->cur_inode_new_gen
= 0;
5183 sctx
->cur_inode_deleted
= 0;
5184 sctx
->cur_inode_size
= btrfs_inode_size(
5185 sctx
->left_path
->nodes
[0], left_ii
);
5186 sctx
->cur_inode_mode
= btrfs_inode_mode(
5187 sctx
->left_path
->nodes
[0], left_ii
);
5196 * We have to process new refs before deleted refs, but compare_trees gives us
5197 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5198 * first and later process them in process_recorded_refs.
5199 * For the cur_inode_new_gen case, we skip recording completely because
5200 * changed_inode did already initiate processing of refs. The reason for this is
5201 * that in this case, compare_tree actually compares the refs of 2 different
5202 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5203 * refs of the right tree as deleted and all refs of the left tree as new.
5205 static int changed_ref(struct send_ctx
*sctx
,
5206 enum btrfs_compare_tree_result result
)
5210 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
5212 if (!sctx
->cur_inode_new_gen
&&
5213 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
5214 if (result
== BTRFS_COMPARE_TREE_NEW
)
5215 ret
= record_new_ref(sctx
);
5216 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
5217 ret
= record_deleted_ref(sctx
);
5218 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
5219 ret
= record_changed_ref(sctx
);
5226 * Process new/deleted/changed xattrs. We skip processing in the
5227 * cur_inode_new_gen case because changed_inode did already initiate processing
5228 * of xattrs. The reason is the same as in changed_ref
5230 static int changed_xattr(struct send_ctx
*sctx
,
5231 enum btrfs_compare_tree_result result
)
5235 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
5237 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
5238 if (result
== BTRFS_COMPARE_TREE_NEW
)
5239 ret
= process_new_xattr(sctx
);
5240 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
5241 ret
= process_deleted_xattr(sctx
);
5242 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
5243 ret
= process_changed_xattr(sctx
);
5250 * Process new/deleted/changed extents. We skip processing in the
5251 * cur_inode_new_gen case because changed_inode did already initiate processing
5252 * of extents. The reason is the same as in changed_ref
5254 static int changed_extent(struct send_ctx
*sctx
,
5255 enum btrfs_compare_tree_result result
)
5259 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
5261 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
5262 if (result
!= BTRFS_COMPARE_TREE_DELETED
)
5263 ret
= process_extent(sctx
, sctx
->left_path
,
5270 static int dir_changed(struct send_ctx
*sctx
, u64 dir
)
5272 u64 orig_gen
, new_gen
;
5275 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &new_gen
, NULL
, NULL
,
5280 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &orig_gen
, NULL
,
5285 return (orig_gen
!= new_gen
) ? 1 : 0;
5288 static int compare_refs(struct send_ctx
*sctx
, struct btrfs_path
*path
,
5289 struct btrfs_key
*key
)
5291 struct btrfs_inode_extref
*extref
;
5292 struct extent_buffer
*leaf
;
5293 u64 dirid
= 0, last_dirid
= 0;
5300 /* Easy case, just check this one dirid */
5301 if (key
->type
== BTRFS_INODE_REF_KEY
) {
5302 dirid
= key
->offset
;
5304 ret
= dir_changed(sctx
, dirid
);
5308 leaf
= path
->nodes
[0];
5309 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
5310 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
5311 while (cur_offset
< item_size
) {
5312 extref
= (struct btrfs_inode_extref
*)(ptr
+
5314 dirid
= btrfs_inode_extref_parent(leaf
, extref
);
5315 ref_name_len
= btrfs_inode_extref_name_len(leaf
, extref
);
5316 cur_offset
+= ref_name_len
+ sizeof(*extref
);
5317 if (dirid
== last_dirid
)
5319 ret
= dir_changed(sctx
, dirid
);
5329 * Updates compare related fields in sctx and simply forwards to the actual
5330 * changed_xxx functions.
5332 static int changed_cb(struct btrfs_root
*left_root
,
5333 struct btrfs_root
*right_root
,
5334 struct btrfs_path
*left_path
,
5335 struct btrfs_path
*right_path
,
5336 struct btrfs_key
*key
,
5337 enum btrfs_compare_tree_result result
,
5341 struct send_ctx
*sctx
= ctx
;
5343 if (result
== BTRFS_COMPARE_TREE_SAME
) {
5344 if (key
->type
== BTRFS_INODE_REF_KEY
||
5345 key
->type
== BTRFS_INODE_EXTREF_KEY
) {
5346 ret
= compare_refs(sctx
, left_path
, key
);
5351 } else if (key
->type
== BTRFS_EXTENT_DATA_KEY
) {
5352 return maybe_send_hole(sctx
, left_path
, key
);
5356 result
= BTRFS_COMPARE_TREE_CHANGED
;
5360 sctx
->left_path
= left_path
;
5361 sctx
->right_path
= right_path
;
5362 sctx
->cmp_key
= key
;
5364 ret
= finish_inode_if_needed(sctx
, 0);
5368 /* Ignore non-FS objects */
5369 if (key
->objectid
== BTRFS_FREE_INO_OBJECTID
||
5370 key
->objectid
== BTRFS_FREE_SPACE_OBJECTID
)
5373 if (key
->type
== BTRFS_INODE_ITEM_KEY
)
5374 ret
= changed_inode(sctx
, result
);
5375 else if (key
->type
== BTRFS_INODE_REF_KEY
||
5376 key
->type
== BTRFS_INODE_EXTREF_KEY
)
5377 ret
= changed_ref(sctx
, result
);
5378 else if (key
->type
== BTRFS_XATTR_ITEM_KEY
)
5379 ret
= changed_xattr(sctx
, result
);
5380 else if (key
->type
== BTRFS_EXTENT_DATA_KEY
)
5381 ret
= changed_extent(sctx
, result
);
5387 static int full_send_tree(struct send_ctx
*sctx
)
5390 struct btrfs_root
*send_root
= sctx
->send_root
;
5391 struct btrfs_key key
;
5392 struct btrfs_key found_key
;
5393 struct btrfs_path
*path
;
5394 struct extent_buffer
*eb
;
5397 path
= alloc_path_for_send();
5401 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
5402 key
.type
= BTRFS_INODE_ITEM_KEY
;
5405 ret
= btrfs_search_slot_for_read(send_root
, &key
, path
, 1, 0);
5412 eb
= path
->nodes
[0];
5413 slot
= path
->slots
[0];
5414 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
5416 ret
= changed_cb(send_root
, NULL
, path
, NULL
,
5417 &found_key
, BTRFS_COMPARE_TREE_NEW
, sctx
);
5421 key
.objectid
= found_key
.objectid
;
5422 key
.type
= found_key
.type
;
5423 key
.offset
= found_key
.offset
+ 1;
5425 ret
= btrfs_next_item(send_root
, path
);
5435 ret
= finish_inode_if_needed(sctx
, 1);
5438 btrfs_free_path(path
);
5442 static int send_subvol(struct send_ctx
*sctx
)
5446 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_STREAM_HEADER
)) {
5447 ret
= send_header(sctx
);
5452 ret
= send_subvol_begin(sctx
);
5456 if (sctx
->parent_root
) {
5457 ret
= btrfs_compare_trees(sctx
->send_root
, sctx
->parent_root
,
5461 ret
= finish_inode_if_needed(sctx
, 1);
5465 ret
= full_send_tree(sctx
);
5471 free_recorded_refs(sctx
);
5475 static void btrfs_root_dec_send_in_progress(struct btrfs_root
* root
)
5477 spin_lock(&root
->root_item_lock
);
5478 root
->send_in_progress
--;
5480 * Not much left to do, we don't know why it's unbalanced and
5481 * can't blindly reset it to 0.
5483 if (root
->send_in_progress
< 0)
5484 btrfs_err(root
->fs_info
,
5485 "send_in_progres unbalanced %d root %llu\n",
5486 root
->send_in_progress
, root
->root_key
.objectid
);
5487 spin_unlock(&root
->root_item_lock
);
5490 long btrfs_ioctl_send(struct file
*mnt_file
, void __user
*arg_
)
5493 struct btrfs_root
*send_root
;
5494 struct btrfs_root
*clone_root
;
5495 struct btrfs_fs_info
*fs_info
;
5496 struct btrfs_ioctl_send_args
*arg
= NULL
;
5497 struct btrfs_key key
;
5498 struct send_ctx
*sctx
= NULL
;
5500 u64
*clone_sources_tmp
= NULL
;
5501 int clone_sources_to_rollback
= 0;
5502 int sort_clone_roots
= 0;
5505 if (!capable(CAP_SYS_ADMIN
))
5508 send_root
= BTRFS_I(file_inode(mnt_file
))->root
;
5509 fs_info
= send_root
->fs_info
;
5512 * The subvolume must remain read-only during send, protect against
5515 spin_lock(&send_root
->root_item_lock
);
5516 send_root
->send_in_progress
++;
5517 spin_unlock(&send_root
->root_item_lock
);
5520 * This is done when we lookup the root, it should already be complete
5521 * by the time we get here.
5523 WARN_ON(send_root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
);
5526 * Userspace tools do the checks and warn the user if it's
5529 if (!btrfs_root_readonly(send_root
)) {
5534 arg
= memdup_user(arg_
, sizeof(*arg
));
5541 if (!access_ok(VERIFY_READ
, arg
->clone_sources
,
5542 sizeof(*arg
->clone_sources
) *
5543 arg
->clone_sources_count
)) {
5548 if (arg
->flags
& ~BTRFS_SEND_FLAG_MASK
) {
5553 sctx
= kzalloc(sizeof(struct send_ctx
), GFP_NOFS
);
5559 INIT_LIST_HEAD(&sctx
->new_refs
);
5560 INIT_LIST_HEAD(&sctx
->deleted_refs
);
5561 INIT_RADIX_TREE(&sctx
->name_cache
, GFP_NOFS
);
5562 INIT_LIST_HEAD(&sctx
->name_cache_list
);
5564 sctx
->flags
= arg
->flags
;
5566 sctx
->send_filp
= fget(arg
->send_fd
);
5567 if (!sctx
->send_filp
) {
5572 sctx
->send_root
= send_root
;
5573 sctx
->clone_roots_cnt
= arg
->clone_sources_count
;
5575 sctx
->send_max_size
= BTRFS_SEND_BUF_SIZE
;
5576 sctx
->send_buf
= vmalloc(sctx
->send_max_size
);
5577 if (!sctx
->send_buf
) {
5582 sctx
->read_buf
= vmalloc(BTRFS_SEND_READ_SIZE
);
5583 if (!sctx
->read_buf
) {
5588 sctx
->pending_dir_moves
= RB_ROOT
;
5589 sctx
->waiting_dir_moves
= RB_ROOT
;
5590 sctx
->orphan_dirs
= RB_ROOT
;
5592 sctx
->clone_roots
= vzalloc(sizeof(struct clone_root
) *
5593 (arg
->clone_sources_count
+ 1));
5594 if (!sctx
->clone_roots
) {
5599 if (arg
->clone_sources_count
) {
5600 clone_sources_tmp
= vmalloc(arg
->clone_sources_count
*
5601 sizeof(*arg
->clone_sources
));
5602 if (!clone_sources_tmp
) {
5607 ret
= copy_from_user(clone_sources_tmp
, arg
->clone_sources
,
5608 arg
->clone_sources_count
*
5609 sizeof(*arg
->clone_sources
));
5615 for (i
= 0; i
< arg
->clone_sources_count
; i
++) {
5616 key
.objectid
= clone_sources_tmp
[i
];
5617 key
.type
= BTRFS_ROOT_ITEM_KEY
;
5618 key
.offset
= (u64
)-1;
5620 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
5622 clone_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
5623 if (IS_ERR(clone_root
)) {
5624 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5625 ret
= PTR_ERR(clone_root
);
5628 clone_sources_to_rollback
= i
+ 1;
5629 spin_lock(&clone_root
->root_item_lock
);
5630 clone_root
->send_in_progress
++;
5631 if (!btrfs_root_readonly(clone_root
)) {
5632 spin_unlock(&clone_root
->root_item_lock
);
5633 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5637 spin_unlock(&clone_root
->root_item_lock
);
5638 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5640 sctx
->clone_roots
[i
].root
= clone_root
;
5642 vfree(clone_sources_tmp
);
5643 clone_sources_tmp
= NULL
;
5646 if (arg
->parent_root
) {
5647 key
.objectid
= arg
->parent_root
;
5648 key
.type
= BTRFS_ROOT_ITEM_KEY
;
5649 key
.offset
= (u64
)-1;
5651 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
5653 sctx
->parent_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
5654 if (IS_ERR(sctx
->parent_root
)) {
5655 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5656 ret
= PTR_ERR(sctx
->parent_root
);
5660 spin_lock(&sctx
->parent_root
->root_item_lock
);
5661 sctx
->parent_root
->send_in_progress
++;
5662 if (!btrfs_root_readonly(sctx
->parent_root
)) {
5663 spin_unlock(&sctx
->parent_root
->root_item_lock
);
5664 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5668 spin_unlock(&sctx
->parent_root
->root_item_lock
);
5670 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5674 * Clones from send_root are allowed, but only if the clone source
5675 * is behind the current send position. This is checked while searching
5676 * for possible clone sources.
5678 sctx
->clone_roots
[sctx
->clone_roots_cnt
++].root
= sctx
->send_root
;
5680 /* We do a bsearch later */
5681 sort(sctx
->clone_roots
, sctx
->clone_roots_cnt
,
5682 sizeof(*sctx
->clone_roots
), __clone_root_cmp_sort
,
5684 sort_clone_roots
= 1;
5686 current
->journal_info
= (void *)BTRFS_SEND_TRANS_STUB
;
5687 ret
= send_subvol(sctx
);
5688 current
->journal_info
= NULL
;
5692 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_END_CMD
)) {
5693 ret
= begin_cmd(sctx
, BTRFS_SEND_C_END
);
5696 ret
= send_cmd(sctx
);
5702 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->pending_dir_moves
));
5703 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->pending_dir_moves
)) {
5705 struct pending_dir_move
*pm
;
5707 n
= rb_first(&sctx
->pending_dir_moves
);
5708 pm
= rb_entry(n
, struct pending_dir_move
, node
);
5709 while (!list_empty(&pm
->list
)) {
5710 struct pending_dir_move
*pm2
;
5712 pm2
= list_first_entry(&pm
->list
,
5713 struct pending_dir_move
, list
);
5714 free_pending_move(sctx
, pm2
);
5716 free_pending_move(sctx
, pm
);
5719 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
));
5720 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->waiting_dir_moves
)) {
5722 struct waiting_dir_move
*dm
;
5724 n
= rb_first(&sctx
->waiting_dir_moves
);
5725 dm
= rb_entry(n
, struct waiting_dir_move
, node
);
5726 rb_erase(&dm
->node
, &sctx
->waiting_dir_moves
);
5730 WARN_ON(sctx
&& !ret
&& !RB_EMPTY_ROOT(&sctx
->orphan_dirs
));
5731 while (sctx
&& !RB_EMPTY_ROOT(&sctx
->orphan_dirs
)) {
5733 struct orphan_dir_info
*odi
;
5735 n
= rb_first(&sctx
->orphan_dirs
);
5736 odi
= rb_entry(n
, struct orphan_dir_info
, node
);
5737 free_orphan_dir_info(sctx
, odi
);
5740 if (sort_clone_roots
) {
5741 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++)
5742 btrfs_root_dec_send_in_progress(
5743 sctx
->clone_roots
[i
].root
);
5745 for (i
= 0; sctx
&& i
< clone_sources_to_rollback
; i
++)
5746 btrfs_root_dec_send_in_progress(
5747 sctx
->clone_roots
[i
].root
);
5749 btrfs_root_dec_send_in_progress(send_root
);
5751 if (sctx
&& !IS_ERR_OR_NULL(sctx
->parent_root
))
5752 btrfs_root_dec_send_in_progress(sctx
->parent_root
);
5755 vfree(clone_sources_tmp
);
5758 if (sctx
->send_filp
)
5759 fput(sctx
->send_filp
);
5761 vfree(sctx
->clone_roots
);
5762 vfree(sctx
->send_buf
);
5763 vfree(sctx
->read_buf
);
5765 name_cache_free(sctx
);