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/crc32c.h>
28 #include <linux/vmalloc.h>
34 #include "btrfs_inode.h"
35 #include "transaction.h"
37 static int g_verbose
= 0;
39 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
42 * A fs_path is a helper to dynamically build path names with unknown size.
43 * It reallocates the internal buffer on demand.
44 * It allows fast adding of path elements on the right side (normal path) and
45 * fast adding to the left side (reversed path). A reversed path can also be
46 * unreversed if needed.
64 #define FS_PATH_INLINE_SIZE \
65 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
68 /* reused for each extent */
70 struct btrfs_root
*root
;
77 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
78 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
81 struct file
*send_filp
;
87 u64 cmd_send_size
[BTRFS_SEND_C_MAX
+ 1];
91 struct btrfs_root
*send_root
;
92 struct btrfs_root
*parent_root
;
93 struct clone_root
*clone_roots
;
96 /* current state of the compare_tree call */
97 struct btrfs_path
*left_path
;
98 struct btrfs_path
*right_path
;
99 struct btrfs_key
*cmp_key
;
102 * infos of the currently processed inode. In case of deleted inodes,
103 * these are the values from the deleted inode.
108 int cur_inode_new_gen
;
109 int cur_inode_deleted
;
115 struct list_head new_refs
;
116 struct list_head deleted_refs
;
118 struct radix_tree_root name_cache
;
119 struct list_head name_cache_list
;
122 struct file
*cur_inode_filp
;
126 struct name_cache_entry
{
127 struct list_head list
;
129 * radix_tree has only 32bit entries but we need to handle 64bit inums.
130 * We use the lower 32bit of the 64bit inum to store it in the tree. If
131 * more then one inum would fall into the same entry, we use radix_list
132 * to store the additional entries. radix_list is also used to store
133 * entries where two entries have the same inum but different
136 struct list_head radix_list
;
142 int need_later_update
;
147 static void fs_path_reset(struct fs_path
*p
)
150 p
->start
= p
->buf
+ p
->buf_len
- 1;
160 static struct fs_path
*fs_path_alloc(struct send_ctx
*sctx
)
164 p
= kmalloc(sizeof(*p
), GFP_NOFS
);
169 p
->buf
= p
->inline_buf
;
170 p
->buf_len
= FS_PATH_INLINE_SIZE
;
175 static struct fs_path
*fs_path_alloc_reversed(struct send_ctx
*sctx
)
179 p
= fs_path_alloc(sctx
);
187 static void fs_path_free(struct send_ctx
*sctx
, struct fs_path
*p
)
191 if (p
->buf
!= p
->inline_buf
) {
200 static int fs_path_len(struct fs_path
*p
)
202 return p
->end
- p
->start
;
205 static int fs_path_ensure_buf(struct fs_path
*p
, int len
)
213 if (p
->buf_len
>= len
)
216 path_len
= p
->end
- p
->start
;
217 old_buf_len
= p
->buf_len
;
218 len
= PAGE_ALIGN(len
);
220 if (p
->buf
== p
->inline_buf
) {
221 tmp_buf
= kmalloc(len
, GFP_NOFS
);
223 tmp_buf
= vmalloc(len
);
228 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
232 if (p
->virtual_mem
) {
233 tmp_buf
= vmalloc(len
);
236 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
239 tmp_buf
= krealloc(p
->buf
, len
, GFP_NOFS
);
241 tmp_buf
= vmalloc(len
);
244 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
253 tmp_buf
= p
->buf
+ old_buf_len
- path_len
- 1;
254 p
->end
= p
->buf
+ p
->buf_len
- 1;
255 p
->start
= p
->end
- path_len
;
256 memmove(p
->start
, tmp_buf
, path_len
+ 1);
259 p
->end
= p
->start
+ path_len
;
264 static int fs_path_prepare_for_add(struct fs_path
*p
, int name_len
)
269 new_len
= p
->end
- p
->start
+ name_len
;
270 if (p
->start
!= p
->end
)
272 ret
= fs_path_ensure_buf(p
, new_len
);
277 if (p
->start
!= p
->end
)
279 p
->start
-= name_len
;
280 p
->prepared
= p
->start
;
282 if (p
->start
!= p
->end
)
284 p
->prepared
= p
->end
;
293 static int fs_path_add(struct fs_path
*p
, const char *name
, int name_len
)
297 ret
= fs_path_prepare_for_add(p
, name_len
);
300 memcpy(p
->prepared
, name
, name_len
);
307 static int fs_path_add_path(struct fs_path
*p
, struct fs_path
*p2
)
311 ret
= fs_path_prepare_for_add(p
, p2
->end
- p2
->start
);
314 memcpy(p
->prepared
, p2
->start
, p2
->end
- p2
->start
);
321 static int fs_path_add_from_extent_buffer(struct fs_path
*p
,
322 struct extent_buffer
*eb
,
323 unsigned long off
, int len
)
327 ret
= fs_path_prepare_for_add(p
, len
);
331 read_extent_buffer(eb
, p
->prepared
, off
, len
);
339 static void fs_path_remove(struct fs_path
*p
)
342 while (p
->start
!= p
->end
&& *p
->end
!= '/')
348 static int fs_path_copy(struct fs_path
*p
, struct fs_path
*from
)
352 p
->reversed
= from
->reversed
;
355 ret
= fs_path_add_path(p
, from
);
361 static void fs_path_unreverse(struct fs_path
*p
)
370 len
= p
->end
- p
->start
;
372 p
->end
= p
->start
+ len
;
373 memmove(p
->start
, tmp
, len
+ 1);
377 static struct btrfs_path
*alloc_path_for_send(void)
379 struct btrfs_path
*path
;
381 path
= btrfs_alloc_path();
384 path
->search_commit_root
= 1;
385 path
->skip_locking
= 1;
389 int write_buf(struct file
*filp
, const void *buf
, u32 len
, loff_t
*off
)
399 ret
= vfs_write(filp
, (char *)buf
+ pos
, len
- pos
, off
);
400 /* TODO handle that correctly */
401 /*if (ret == -ERESTARTSYS) {
420 static int tlv_put(struct send_ctx
*sctx
, u16 attr
, const void *data
, int len
)
422 struct btrfs_tlv_header
*hdr
;
423 int total_len
= sizeof(*hdr
) + len
;
424 int left
= sctx
->send_max_size
- sctx
->send_size
;
426 if (unlikely(left
< total_len
))
429 hdr
= (struct btrfs_tlv_header
*) (sctx
->send_buf
+ sctx
->send_size
);
430 hdr
->tlv_type
= cpu_to_le16(attr
);
431 hdr
->tlv_len
= cpu_to_le16(len
);
432 memcpy(hdr
+ 1, data
, len
);
433 sctx
->send_size
+= total_len
;
439 static int tlv_put_u8(struct send_ctx
*sctx
, u16 attr
, u8 value
)
441 return tlv_put(sctx
, attr
, &value
, sizeof(value
));
444 static int tlv_put_u16(struct send_ctx
*sctx
, u16 attr
, u16 value
)
446 __le16 tmp
= cpu_to_le16(value
);
447 return tlv_put(sctx
, attr
, &tmp
, sizeof(tmp
));
450 static int tlv_put_u32(struct send_ctx
*sctx
, u16 attr
, u32 value
)
452 __le32 tmp
= cpu_to_le32(value
);
453 return tlv_put(sctx
, attr
, &tmp
, sizeof(tmp
));
457 static int tlv_put_u64(struct send_ctx
*sctx
, u16 attr
, u64 value
)
459 __le64 tmp
= cpu_to_le64(value
);
460 return tlv_put(sctx
, attr
, &tmp
, sizeof(tmp
));
463 static int tlv_put_string(struct send_ctx
*sctx
, u16 attr
,
464 const char *str
, int len
)
468 return tlv_put(sctx
, attr
, str
, len
);
471 static int tlv_put_uuid(struct send_ctx
*sctx
, u16 attr
,
474 return tlv_put(sctx
, attr
, uuid
, BTRFS_UUID_SIZE
);
478 static int tlv_put_timespec(struct send_ctx
*sctx
, u16 attr
,
481 struct btrfs_timespec bts
;
482 bts
.sec
= cpu_to_le64(ts
->tv_sec
);
483 bts
.nsec
= cpu_to_le32(ts
->tv_nsec
);
484 return tlv_put(sctx
, attr
, &bts
, sizeof(bts
));
488 static int tlv_put_btrfs_timespec(struct send_ctx
*sctx
, u16 attr
,
489 struct extent_buffer
*eb
,
490 struct btrfs_timespec
*ts
)
492 struct btrfs_timespec bts
;
493 read_extent_buffer(eb
, &bts
, (unsigned long)ts
, sizeof(bts
));
494 return tlv_put(sctx
, attr
, &bts
, sizeof(bts
));
498 #define TLV_PUT(sctx, attrtype, attrlen, data) \
500 ret = tlv_put(sctx, attrtype, attrlen, data); \
502 goto tlv_put_failure; \
505 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
507 ret = tlv_put_u##bits(sctx, attrtype, value); \
509 goto tlv_put_failure; \
512 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
513 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
514 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
515 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
516 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
518 ret = tlv_put_string(sctx, attrtype, str, len); \
520 goto tlv_put_failure; \
522 #define TLV_PUT_PATH(sctx, attrtype, p) \
524 ret = tlv_put_string(sctx, attrtype, p->start, \
525 p->end - p->start); \
527 goto tlv_put_failure; \
529 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
531 ret = tlv_put_uuid(sctx, attrtype, uuid); \
533 goto tlv_put_failure; \
535 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
537 ret = tlv_put_timespec(sctx, attrtype, ts); \
539 goto tlv_put_failure; \
541 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
543 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
545 goto tlv_put_failure; \
548 static int send_header(struct send_ctx
*sctx
)
550 struct btrfs_stream_header hdr
;
552 strcpy(hdr
.magic
, BTRFS_SEND_STREAM_MAGIC
);
553 hdr
.version
= cpu_to_le32(BTRFS_SEND_STREAM_VERSION
);
555 return write_buf(sctx
->send_filp
, &hdr
, sizeof(hdr
),
560 * For each command/item we want to send to userspace, we call this function.
562 static int begin_cmd(struct send_ctx
*sctx
, int cmd
)
564 struct btrfs_cmd_header
*hdr
;
566 if (!sctx
->send_buf
) {
571 BUG_ON(sctx
->send_size
);
573 sctx
->send_size
+= sizeof(*hdr
);
574 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
575 hdr
->cmd
= cpu_to_le16(cmd
);
580 static int send_cmd(struct send_ctx
*sctx
)
583 struct btrfs_cmd_header
*hdr
;
586 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
587 hdr
->len
= cpu_to_le32(sctx
->send_size
- sizeof(*hdr
));
590 crc
= crc32c(0, (unsigned char *)sctx
->send_buf
, sctx
->send_size
);
591 hdr
->crc
= cpu_to_le32(crc
);
593 ret
= write_buf(sctx
->send_filp
, sctx
->send_buf
, sctx
->send_size
,
596 sctx
->total_send_size
+= sctx
->send_size
;
597 sctx
->cmd_send_size
[le16_to_cpu(hdr
->cmd
)] += sctx
->send_size
;
604 * Sends a move instruction to user space
606 static int send_rename(struct send_ctx
*sctx
,
607 struct fs_path
*from
, struct fs_path
*to
)
611 verbose_printk("btrfs: send_rename %s -> %s\n", from
->start
, to
->start
);
613 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RENAME
);
617 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, from
);
618 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_TO
, to
);
620 ret
= send_cmd(sctx
);
628 * Sends a link instruction to user space
630 static int send_link(struct send_ctx
*sctx
,
631 struct fs_path
*path
, struct fs_path
*lnk
)
635 verbose_printk("btrfs: send_link %s -> %s\n", path
->start
, lnk
->start
);
637 ret
= begin_cmd(sctx
, BTRFS_SEND_C_LINK
);
641 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
642 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, lnk
);
644 ret
= send_cmd(sctx
);
652 * Sends an unlink instruction to user space
654 static int send_unlink(struct send_ctx
*sctx
, struct fs_path
*path
)
658 verbose_printk("btrfs: send_unlink %s\n", path
->start
);
660 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UNLINK
);
664 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
666 ret
= send_cmd(sctx
);
674 * Sends a rmdir instruction to user space
676 static int send_rmdir(struct send_ctx
*sctx
, struct fs_path
*path
)
680 verbose_printk("btrfs: send_rmdir %s\n", path
->start
);
682 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RMDIR
);
686 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
688 ret
= send_cmd(sctx
);
696 * Helper function to retrieve some fields from an inode item.
698 static int get_inode_info(struct btrfs_root
*root
,
699 u64 ino
, u64
*size
, u64
*gen
,
700 u64
*mode
, u64
*uid
, u64
*gid
,
704 struct btrfs_inode_item
*ii
;
705 struct btrfs_key key
;
706 struct btrfs_path
*path
;
708 path
= alloc_path_for_send();
713 key
.type
= BTRFS_INODE_ITEM_KEY
;
715 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
723 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
724 struct btrfs_inode_item
);
726 *size
= btrfs_inode_size(path
->nodes
[0], ii
);
728 *gen
= btrfs_inode_generation(path
->nodes
[0], ii
);
730 *mode
= btrfs_inode_mode(path
->nodes
[0], ii
);
732 *uid
= btrfs_inode_uid(path
->nodes
[0], ii
);
734 *gid
= btrfs_inode_gid(path
->nodes
[0], ii
);
736 *rdev
= btrfs_inode_rdev(path
->nodes
[0], ii
);
739 btrfs_free_path(path
);
743 typedef int (*iterate_inode_ref_t
)(int num
, u64 dir
, int index
,
748 * Helper function to iterate the entries in ONE btrfs_inode_ref.
749 * The iterate callback may return a non zero value to stop iteration. This can
750 * be a negative value for error codes or 1 to simply stop it.
752 * path must point to the INODE_REF when called.
754 static int iterate_inode_ref(struct send_ctx
*sctx
,
755 struct btrfs_root
*root
, struct btrfs_path
*path
,
756 struct btrfs_key
*found_key
, int resolve
,
757 iterate_inode_ref_t iterate
, void *ctx
)
759 struct extent_buffer
*eb
;
760 struct btrfs_item
*item
;
761 struct btrfs_inode_ref
*iref
;
762 struct btrfs_path
*tmp_path
;
774 p
= fs_path_alloc_reversed(sctx
);
778 tmp_path
= alloc_path_for_send();
780 fs_path_free(sctx
, p
);
785 slot
= path
->slots
[0];
786 item
= btrfs_item_nr(eb
, slot
);
787 iref
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_ref
);
790 total
= btrfs_item_size(eb
, item
);
793 while (cur
< total
) {
796 name_len
= btrfs_inode_ref_name_len(eb
, iref
);
797 index
= btrfs_inode_ref_index(eb
, iref
);
799 start
= btrfs_iref_to_path(root
, tmp_path
, iref
, eb
,
800 found_key
->offset
, p
->buf
,
803 ret
= PTR_ERR(start
);
806 if (start
< p
->buf
) {
807 /* overflow , try again with larger buffer */
808 ret
= fs_path_ensure_buf(p
,
809 p
->buf_len
+ p
->buf
- start
);
812 start
= btrfs_iref_to_path(root
, tmp_path
, iref
,
813 eb
, found_key
->offset
, p
->buf
,
816 ret
= PTR_ERR(start
);
819 BUG_ON(start
< p
->buf
);
823 ret
= fs_path_add_from_extent_buffer(p
, eb
,
824 (unsigned long)(iref
+ 1), name_len
);
830 len
= sizeof(*iref
) + name_len
;
831 iref
= (struct btrfs_inode_ref
*)((char *)iref
+ len
);
834 ret
= iterate(num
, found_key
->offset
, index
, p
, ctx
);
842 btrfs_free_path(tmp_path
);
843 fs_path_free(sctx
, p
);
847 typedef int (*iterate_dir_item_t
)(int num
, struct btrfs_key
*di_key
,
848 const char *name
, int name_len
,
849 const char *data
, int data_len
,
853 * Helper function to iterate the entries in ONE btrfs_dir_item.
854 * The iterate callback may return a non zero value to stop iteration. This can
855 * be a negative value for error codes or 1 to simply stop it.
857 * path must point to the dir item when called.
859 static int iterate_dir_item(struct send_ctx
*sctx
,
860 struct btrfs_root
*root
, struct btrfs_path
*path
,
861 struct btrfs_key
*found_key
,
862 iterate_dir_item_t iterate
, void *ctx
)
865 struct extent_buffer
*eb
;
866 struct btrfs_item
*item
;
867 struct btrfs_dir_item
*di
;
868 struct btrfs_key di_key
;
883 buf
= kmalloc(buf_len
, GFP_NOFS
);
890 slot
= path
->slots
[0];
891 item
= btrfs_item_nr(eb
, slot
);
892 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
895 total
= btrfs_item_size(eb
, item
);
898 while (cur
< total
) {
899 name_len
= btrfs_dir_name_len(eb
, di
);
900 data_len
= btrfs_dir_data_len(eb
, di
);
901 type
= btrfs_dir_type(eb
, di
);
902 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
904 if (name_len
+ data_len
> buf_len
) {
905 buf_len
= PAGE_ALIGN(name_len
+ data_len
);
907 buf2
= vmalloc(buf_len
);
914 buf2
= krealloc(buf
, buf_len
, GFP_NOFS
);
916 buf2
= vmalloc(buf_len
);
930 read_extent_buffer(eb
, buf
, (unsigned long)(di
+ 1),
931 name_len
+ data_len
);
933 len
= sizeof(*di
) + name_len
+ data_len
;
934 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
937 ret
= iterate(num
, &di_key
, buf
, name_len
, buf
+ name_len
,
938 data_len
, type
, ctx
);
957 static int __copy_first_ref(int num
, u64 dir
, int index
,
958 struct fs_path
*p
, void *ctx
)
961 struct fs_path
*pt
= ctx
;
963 ret
= fs_path_copy(pt
, p
);
967 /* we want the first only */
972 * Retrieve the first path of an inode. If an inode has more then one
973 * ref/hardlink, this is ignored.
975 static int get_inode_path(struct send_ctx
*sctx
, struct btrfs_root
*root
,
976 u64 ino
, struct fs_path
*path
)
979 struct btrfs_key key
, found_key
;
980 struct btrfs_path
*p
;
982 p
= alloc_path_for_send();
989 key
.type
= BTRFS_INODE_REF_KEY
;
992 ret
= btrfs_search_slot_for_read(root
, &key
, p
, 1, 0);
999 btrfs_item_key_to_cpu(p
->nodes
[0], &found_key
, p
->slots
[0]);
1000 if (found_key
.objectid
!= ino
||
1001 found_key
.type
!= BTRFS_INODE_REF_KEY
) {
1006 ret
= iterate_inode_ref(sctx
, root
, p
, &found_key
, 1,
1007 __copy_first_ref
, path
);
1017 struct backref_ctx
{
1018 struct send_ctx
*sctx
;
1020 /* number of total found references */
1024 * used for clones found in send_root. clones found behind cur_objectid
1025 * and cur_offset are not considered as allowed clones.
1030 /* may be truncated in case it's the last extent in a file */
1033 /* Just to check for bugs in backref resolving */
1037 static int __clone_root_cmp_bsearch(const void *key
, const void *elt
)
1039 u64 root
= (u64
)(uintptr_t)key
;
1040 struct clone_root
*cr
= (struct clone_root
*)elt
;
1042 if (root
< cr
->root
->objectid
)
1044 if (root
> cr
->root
->objectid
)
1049 static int __clone_root_cmp_sort(const void *e1
, const void *e2
)
1051 struct clone_root
*cr1
= (struct clone_root
*)e1
;
1052 struct clone_root
*cr2
= (struct clone_root
*)e2
;
1054 if (cr1
->root
->objectid
< cr2
->root
->objectid
)
1056 if (cr1
->root
->objectid
> cr2
->root
->objectid
)
1062 * Called for every backref that is found for the current extent.
1063 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1065 static int __iterate_backrefs(u64 ino
, u64 offset
, u64 root
, void *ctx_
)
1067 struct backref_ctx
*bctx
= ctx_
;
1068 struct clone_root
*found
;
1072 /* First check if the root is in the list of accepted clone sources */
1073 found
= bsearch((void *)(uintptr_t)root
, bctx
->sctx
->clone_roots
,
1074 bctx
->sctx
->clone_roots_cnt
,
1075 sizeof(struct clone_root
),
1076 __clone_root_cmp_bsearch
);
1080 if (found
->root
== bctx
->sctx
->send_root
&&
1081 ino
== bctx
->cur_objectid
&&
1082 offset
== bctx
->cur_offset
) {
1083 bctx
->found_itself
= 1;
1087 * There are inodes that have extents that lie behind its i_size. Don't
1088 * accept clones from these extents.
1090 ret
= get_inode_info(found
->root
, ino
, &i_size
, NULL
, NULL
, NULL
, NULL
,
1095 if (offset
+ bctx
->extent_len
> i_size
)
1099 * Make sure we don't consider clones from send_root that are
1100 * behind the current inode/offset.
1102 if (found
->root
== bctx
->sctx
->send_root
) {
1104 * TODO for the moment we don't accept clones from the inode
1105 * that is currently send. We may change this when
1106 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1109 if (ino
>= bctx
->cur_objectid
)
1112 if (ino
> bctx
->cur_objectid
)
1114 if (offset
+ bctx
->extent_len
> bctx
->cur_offset
)
1120 found
->found_refs
++;
1121 if (ino
< found
->ino
) {
1123 found
->offset
= offset
;
1124 } else if (found
->ino
== ino
) {
1126 * same extent found more then once in the same file.
1128 if (found
->offset
> offset
+ bctx
->extent_len
)
1129 found
->offset
= offset
;
1136 * Given an inode, offset and extent item, it finds a good clone for a clone
1137 * instruction. Returns -ENOENT when none could be found. The function makes
1138 * sure that the returned clone is usable at the point where sending is at the
1139 * moment. This means, that no clones are accepted which lie behind the current
1142 * path must point to the extent item when called.
1144 static int find_extent_clone(struct send_ctx
*sctx
,
1145 struct btrfs_path
*path
,
1146 u64 ino
, u64 data_offset
,
1148 struct clone_root
**found
)
1155 u64 extent_item_pos
;
1157 struct btrfs_file_extent_item
*fi
;
1158 struct extent_buffer
*eb
= path
->nodes
[0];
1159 struct backref_ctx
*backref_ctx
= NULL
;
1160 struct clone_root
*cur_clone_root
;
1161 struct btrfs_key found_key
;
1162 struct btrfs_path
*tmp_path
;
1166 tmp_path
= alloc_path_for_send();
1170 backref_ctx
= kmalloc(sizeof(*backref_ctx
), GFP_NOFS
);
1176 if (data_offset
>= ino_size
) {
1178 * There may be extents that lie behind the file's size.
1179 * I at least had this in combination with snapshotting while
1180 * writing large files.
1186 fi
= btrfs_item_ptr(eb
, path
->slots
[0],
1187 struct btrfs_file_extent_item
);
1188 extent_type
= btrfs_file_extent_type(eb
, fi
);
1189 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1193 compressed
= btrfs_file_extent_compression(eb
, fi
);
1195 num_bytes
= btrfs_file_extent_num_bytes(eb
, fi
);
1196 disk_byte
= btrfs_file_extent_disk_bytenr(eb
, fi
);
1197 if (disk_byte
== 0) {
1201 logical
= disk_byte
+ btrfs_file_extent_offset(eb
, fi
);
1203 ret
= extent_from_logical(sctx
->send_root
->fs_info
, disk_byte
, tmp_path
,
1204 &found_key
, &flags
);
1205 btrfs_release_path(tmp_path
);
1209 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1215 * Setup the clone roots.
1217 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1218 cur_clone_root
= sctx
->clone_roots
+ i
;
1219 cur_clone_root
->ino
= (u64
)-1;
1220 cur_clone_root
->offset
= 0;
1221 cur_clone_root
->found_refs
= 0;
1224 backref_ctx
->sctx
= sctx
;
1225 backref_ctx
->found
= 0;
1226 backref_ctx
->cur_objectid
= ino
;
1227 backref_ctx
->cur_offset
= data_offset
;
1228 backref_ctx
->found_itself
= 0;
1229 backref_ctx
->extent_len
= num_bytes
;
1232 * The last extent of a file may be too large due to page alignment.
1233 * We need to adjust extent_len in this case so that the checks in
1234 * __iterate_backrefs work.
1236 if (data_offset
+ num_bytes
>= ino_size
)
1237 backref_ctx
->extent_len
= ino_size
- data_offset
;
1240 * Now collect all backrefs.
1242 if (compressed
== BTRFS_COMPRESS_NONE
)
1243 extent_item_pos
= logical
- found_key
.objectid
;
1245 extent_item_pos
= 0;
1247 extent_item_pos
= logical
- found_key
.objectid
;
1248 ret
= iterate_extent_inodes(sctx
->send_root
->fs_info
,
1249 found_key
.objectid
, extent_item_pos
, 1,
1250 __iterate_backrefs
, backref_ctx
);
1255 if (!backref_ctx
->found_itself
) {
1256 /* found a bug in backref code? */
1258 printk(KERN_ERR
"btrfs: ERROR did not find backref in "
1259 "send_root. inode=%llu, offset=%llu, "
1260 "disk_byte=%llu found extent=%llu\n",
1261 ino
, data_offset
, disk_byte
, found_key
.objectid
);
1265 verbose_printk(KERN_DEBUG
"btrfs: find_extent_clone: data_offset=%llu, "
1267 "num_bytes=%llu, logical=%llu\n",
1268 data_offset
, ino
, num_bytes
, logical
);
1270 if (!backref_ctx
->found
)
1271 verbose_printk("btrfs: no clones found\n");
1273 cur_clone_root
= NULL
;
1274 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1275 if (sctx
->clone_roots
[i
].found_refs
) {
1276 if (!cur_clone_root
)
1277 cur_clone_root
= sctx
->clone_roots
+ i
;
1278 else if (sctx
->clone_roots
[i
].root
== sctx
->send_root
)
1279 /* prefer clones from send_root over others */
1280 cur_clone_root
= sctx
->clone_roots
+ i
;
1285 if (cur_clone_root
) {
1286 *found
= cur_clone_root
;
1293 btrfs_free_path(tmp_path
);
1298 static int read_symlink(struct send_ctx
*sctx
,
1299 struct btrfs_root
*root
,
1301 struct fs_path
*dest
)
1304 struct btrfs_path
*path
;
1305 struct btrfs_key key
;
1306 struct btrfs_file_extent_item
*ei
;
1312 path
= alloc_path_for_send();
1317 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1319 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1324 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1325 struct btrfs_file_extent_item
);
1326 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
1327 compression
= btrfs_file_extent_compression(path
->nodes
[0], ei
);
1328 BUG_ON(type
!= BTRFS_FILE_EXTENT_INLINE
);
1329 BUG_ON(compression
);
1331 off
= btrfs_file_extent_inline_start(ei
);
1332 len
= btrfs_file_extent_inline_len(path
->nodes
[0], ei
);
1334 ret
= fs_path_add_from_extent_buffer(dest
, path
->nodes
[0], off
, len
);
1337 btrfs_free_path(path
);
1342 * Helper function to generate a file name that is unique in the root of
1343 * send_root and parent_root. This is used to generate names for orphan inodes.
1345 static int gen_unique_name(struct send_ctx
*sctx
,
1347 struct fs_path
*dest
)
1350 struct btrfs_path
*path
;
1351 struct btrfs_dir_item
*di
;
1356 path
= alloc_path_for_send();
1361 len
= snprintf(tmp
, sizeof(tmp
) - 1, "o%llu-%llu-%llu",
1363 if (len
>= sizeof(tmp
)) {
1364 /* should really not happen */
1369 di
= btrfs_lookup_dir_item(NULL
, sctx
->send_root
,
1370 path
, BTRFS_FIRST_FREE_OBJECTID
,
1371 tmp
, strlen(tmp
), 0);
1372 btrfs_release_path(path
);
1378 /* not unique, try again */
1383 if (!sctx
->parent_root
) {
1389 di
= btrfs_lookup_dir_item(NULL
, sctx
->parent_root
,
1390 path
, BTRFS_FIRST_FREE_OBJECTID
,
1391 tmp
, strlen(tmp
), 0);
1392 btrfs_release_path(path
);
1398 /* not unique, try again */
1406 ret
= fs_path_add(dest
, tmp
, strlen(tmp
));
1409 btrfs_free_path(path
);
1414 inode_state_no_change
,
1415 inode_state_will_create
,
1416 inode_state_did_create
,
1417 inode_state_will_delete
,
1418 inode_state_did_delete
,
1421 static int get_cur_inode_state(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1429 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &left_gen
, NULL
, NULL
,
1431 if (ret
< 0 && ret
!= -ENOENT
)
1435 if (!sctx
->parent_root
) {
1436 right_ret
= -ENOENT
;
1438 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
, &right_gen
,
1439 NULL
, NULL
, NULL
, NULL
);
1440 if (ret
< 0 && ret
!= -ENOENT
)
1445 if (!left_ret
&& !right_ret
) {
1446 if (left_gen
== gen
&& right_gen
== gen
) {
1447 ret
= inode_state_no_change
;
1448 } else if (left_gen
== gen
) {
1449 if (ino
< sctx
->send_progress
)
1450 ret
= inode_state_did_create
;
1452 ret
= inode_state_will_create
;
1453 } else if (right_gen
== gen
) {
1454 if (ino
< sctx
->send_progress
)
1455 ret
= inode_state_did_delete
;
1457 ret
= inode_state_will_delete
;
1461 } else if (!left_ret
) {
1462 if (left_gen
== gen
) {
1463 if (ino
< sctx
->send_progress
)
1464 ret
= inode_state_did_create
;
1466 ret
= inode_state_will_create
;
1470 } else if (!right_ret
) {
1471 if (right_gen
== gen
) {
1472 if (ino
< sctx
->send_progress
)
1473 ret
= inode_state_did_delete
;
1475 ret
= inode_state_will_delete
;
1487 static int is_inode_existent(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1491 ret
= get_cur_inode_state(sctx
, ino
, gen
);
1495 if (ret
== inode_state_no_change
||
1496 ret
== inode_state_did_create
||
1497 ret
== inode_state_will_delete
)
1507 * Helper function to lookup a dir item in a dir.
1509 static int lookup_dir_item_inode(struct btrfs_root
*root
,
1510 u64 dir
, const char *name
, int name_len
,
1515 struct btrfs_dir_item
*di
;
1516 struct btrfs_key key
;
1517 struct btrfs_path
*path
;
1519 path
= alloc_path_for_send();
1523 di
= btrfs_lookup_dir_item(NULL
, root
, path
,
1524 dir
, name
, name_len
, 0);
1533 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1534 *found_inode
= key
.objectid
;
1535 *found_type
= btrfs_dir_type(path
->nodes
[0], di
);
1538 btrfs_free_path(path
);
1543 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1544 * generation of the parent dir and the name of the dir entry.
1546 static int get_first_ref(struct send_ctx
*sctx
,
1547 struct btrfs_root
*root
, u64 ino
,
1548 u64
*dir
, u64
*dir_gen
, struct fs_path
*name
)
1551 struct btrfs_key key
;
1552 struct btrfs_key found_key
;
1553 struct btrfs_path
*path
;
1554 struct btrfs_inode_ref
*iref
;
1557 path
= alloc_path_for_send();
1562 key
.type
= BTRFS_INODE_REF_KEY
;
1565 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
1569 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1571 if (ret
|| found_key
.objectid
!= key
.objectid
||
1572 found_key
.type
!= key
.type
) {
1577 iref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1578 struct btrfs_inode_ref
);
1579 len
= btrfs_inode_ref_name_len(path
->nodes
[0], iref
);
1580 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1581 (unsigned long)(iref
+ 1), len
);
1584 btrfs_release_path(path
);
1586 ret
= get_inode_info(root
, found_key
.offset
, NULL
, dir_gen
, NULL
, NULL
,
1591 *dir
= found_key
.offset
;
1594 btrfs_free_path(path
);
1598 static int is_first_ref(struct send_ctx
*sctx
,
1599 struct btrfs_root
*root
,
1601 const char *name
, int name_len
)
1604 struct fs_path
*tmp_name
;
1608 tmp_name
= fs_path_alloc(sctx
);
1612 ret
= get_first_ref(sctx
, root
, ino
, &tmp_dir
, &tmp_dir_gen
, tmp_name
);
1616 if (dir
!= tmp_dir
|| name_len
!= fs_path_len(tmp_name
)) {
1621 ret
= !memcmp(tmp_name
->start
, name
, name_len
);
1624 fs_path_free(sctx
, tmp_name
);
1629 * Used by process_recorded_refs to determine if a new ref would overwrite an
1630 * already existing ref. In case it detects an overwrite, it returns the
1631 * inode/gen in who_ino/who_gen.
1632 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1633 * to make sure later references to the overwritten inode are possible.
1634 * Orphanizing is however only required for the first ref of an inode.
1635 * process_recorded_refs does an additional is_first_ref check to see if
1636 * orphanizing is really required.
1638 static int will_overwrite_ref(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
1639 const char *name
, int name_len
,
1640 u64
*who_ino
, u64
*who_gen
)
1643 u64 other_inode
= 0;
1646 if (!sctx
->parent_root
)
1649 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1653 ret
= lookup_dir_item_inode(sctx
->parent_root
, dir
, name
, name_len
,
1654 &other_inode
, &other_type
);
1655 if (ret
< 0 && ret
!= -ENOENT
)
1663 * Check if the overwritten ref was already processed. If yes, the ref
1664 * was already unlinked/moved, so we can safely assume that we will not
1665 * overwrite anything at this point in time.
1667 if (other_inode
> sctx
->send_progress
) {
1668 ret
= get_inode_info(sctx
->parent_root
, other_inode
, NULL
,
1669 who_gen
, NULL
, NULL
, NULL
, NULL
);
1674 *who_ino
= other_inode
;
1684 * Checks if the ref was overwritten by an already processed inode. This is
1685 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1686 * thus the orphan name needs be used.
1687 * process_recorded_refs also uses it to avoid unlinking of refs that were
1690 static int did_overwrite_ref(struct send_ctx
*sctx
,
1691 u64 dir
, u64 dir_gen
,
1692 u64 ino
, u64 ino_gen
,
1693 const char *name
, int name_len
)
1700 if (!sctx
->parent_root
)
1703 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1707 /* check if the ref was overwritten by another ref */
1708 ret
= lookup_dir_item_inode(sctx
->send_root
, dir
, name
, name_len
,
1709 &ow_inode
, &other_type
);
1710 if (ret
< 0 && ret
!= -ENOENT
)
1713 /* was never and will never be overwritten */
1718 ret
= get_inode_info(sctx
->send_root
, ow_inode
, NULL
, &gen
, NULL
, NULL
,
1723 if (ow_inode
== ino
&& gen
== ino_gen
) {
1728 /* we know that it is or will be overwritten. check this now */
1729 if (ow_inode
< sctx
->send_progress
)
1739 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1740 * that got overwritten. This is used by process_recorded_refs to determine
1741 * if it has to use the path as returned by get_cur_path or the orphan name.
1743 static int did_overwrite_first_ref(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1746 struct fs_path
*name
= NULL
;
1750 if (!sctx
->parent_root
)
1753 name
= fs_path_alloc(sctx
);
1757 ret
= get_first_ref(sctx
, sctx
->parent_root
, ino
, &dir
, &dir_gen
, name
);
1761 ret
= did_overwrite_ref(sctx
, dir
, dir_gen
, ino
, gen
,
1762 name
->start
, fs_path_len(name
));
1765 fs_path_free(sctx
, name
);
1770 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1771 * so we need to do some special handling in case we have clashes. This function
1772 * takes care of this with the help of name_cache_entry::radix_list.
1773 * In case of error, nce is kfreed.
1775 static int name_cache_insert(struct send_ctx
*sctx
,
1776 struct name_cache_entry
*nce
)
1779 struct list_head
*nce_head
;
1781 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1782 (unsigned long)nce
->ino
);
1784 nce_head
= kmalloc(sizeof(*nce_head
), GFP_NOFS
);
1787 INIT_LIST_HEAD(nce_head
);
1789 ret
= radix_tree_insert(&sctx
->name_cache
, nce
->ino
, nce_head
);
1796 list_add_tail(&nce
->radix_list
, nce_head
);
1797 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
1798 sctx
->name_cache_size
++;
1803 static void name_cache_delete(struct send_ctx
*sctx
,
1804 struct name_cache_entry
*nce
)
1806 struct list_head
*nce_head
;
1808 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1809 (unsigned long)nce
->ino
);
1812 list_del(&nce
->radix_list
);
1813 list_del(&nce
->list
);
1814 sctx
->name_cache_size
--;
1816 if (list_empty(nce_head
)) {
1817 radix_tree_delete(&sctx
->name_cache
, (unsigned long)nce
->ino
);
1822 static struct name_cache_entry
*name_cache_search(struct send_ctx
*sctx
,
1825 struct list_head
*nce_head
;
1826 struct name_cache_entry
*cur
;
1828 nce_head
= radix_tree_lookup(&sctx
->name_cache
, (unsigned long)ino
);
1832 list_for_each_entry(cur
, nce_head
, radix_list
) {
1833 if (cur
->ino
== ino
&& cur
->gen
== gen
)
1840 * Removes the entry from the list and adds it back to the end. This marks the
1841 * entry as recently used so that name_cache_clean_unused does not remove it.
1843 static void name_cache_used(struct send_ctx
*sctx
, struct name_cache_entry
*nce
)
1845 list_del(&nce
->list
);
1846 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
1850 * Remove some entries from the beginning of name_cache_list.
1852 static void name_cache_clean_unused(struct send_ctx
*sctx
)
1854 struct name_cache_entry
*nce
;
1856 if (sctx
->name_cache_size
< SEND_CTX_NAME_CACHE_CLEAN_SIZE
)
1859 while (sctx
->name_cache_size
> SEND_CTX_MAX_NAME_CACHE_SIZE
) {
1860 nce
= list_entry(sctx
->name_cache_list
.next
,
1861 struct name_cache_entry
, list
);
1862 name_cache_delete(sctx
, nce
);
1867 static void name_cache_free(struct send_ctx
*sctx
)
1869 struct name_cache_entry
*nce
;
1871 while (!list_empty(&sctx
->name_cache_list
)) {
1872 nce
= list_entry(sctx
->name_cache_list
.next
,
1873 struct name_cache_entry
, list
);
1874 name_cache_delete(sctx
, nce
);
1880 * Used by get_cur_path for each ref up to the root.
1881 * Returns 0 if it succeeded.
1882 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1883 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1884 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1885 * Returns <0 in case of error.
1887 static int __get_cur_name_and_parent(struct send_ctx
*sctx
,
1891 struct fs_path
*dest
)
1895 struct btrfs_path
*path
= NULL
;
1896 struct name_cache_entry
*nce
= NULL
;
1899 * First check if we already did a call to this function with the same
1900 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1901 * return the cached result.
1903 nce
= name_cache_search(sctx
, ino
, gen
);
1905 if (ino
< sctx
->send_progress
&& nce
->need_later_update
) {
1906 name_cache_delete(sctx
, nce
);
1910 name_cache_used(sctx
, nce
);
1911 *parent_ino
= nce
->parent_ino
;
1912 *parent_gen
= nce
->parent_gen
;
1913 ret
= fs_path_add(dest
, nce
->name
, nce
->name_len
);
1921 path
= alloc_path_for_send();
1926 * If the inode is not existent yet, add the orphan name and return 1.
1927 * This should only happen for the parent dir that we determine in
1930 ret
= is_inode_existent(sctx
, ino
, gen
);
1935 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
1943 * Depending on whether the inode was already processed or not, use
1944 * send_root or parent_root for ref lookup.
1946 if (ino
< sctx
->send_progress
)
1947 ret
= get_first_ref(sctx
, sctx
->send_root
, ino
,
1948 parent_ino
, parent_gen
, dest
);
1950 ret
= get_first_ref(sctx
, sctx
->parent_root
, ino
,
1951 parent_ino
, parent_gen
, dest
);
1956 * Check if the ref was overwritten by an inode's ref that was processed
1957 * earlier. If yes, treat as orphan and return 1.
1959 ret
= did_overwrite_ref(sctx
, *parent_ino
, *parent_gen
, ino
, gen
,
1960 dest
->start
, dest
->end
- dest
->start
);
1964 fs_path_reset(dest
);
1965 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
1973 * Store the result of the lookup in the name cache.
1975 nce
= kmalloc(sizeof(*nce
) + fs_path_len(dest
) + 1, GFP_NOFS
);
1983 nce
->parent_ino
= *parent_ino
;
1984 nce
->parent_gen
= *parent_gen
;
1985 nce
->name_len
= fs_path_len(dest
);
1987 strcpy(nce
->name
, dest
->start
);
1989 if (ino
< sctx
->send_progress
)
1990 nce
->need_later_update
= 0;
1992 nce
->need_later_update
= 1;
1994 nce_ret
= name_cache_insert(sctx
, nce
);
1997 name_cache_clean_unused(sctx
);
2000 btrfs_free_path(path
);
2005 * Magic happens here. This function returns the first ref to an inode as it
2006 * would look like while receiving the stream at this point in time.
2007 * We walk the path up to the root. For every inode in between, we check if it
2008 * was already processed/sent. If yes, we continue with the parent as found
2009 * in send_root. If not, we continue with the parent as found in parent_root.
2010 * If we encounter an inode that was deleted at this point in time, we use the
2011 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2012 * that were not created yet and overwritten inodes/refs.
2014 * When do we have have orphan inodes:
2015 * 1. When an inode is freshly created and thus no valid refs are available yet
2016 * 2. When a directory lost all it's refs (deleted) but still has dir items
2017 * inside which were not processed yet (pending for move/delete). If anyone
2018 * tried to get the path to the dir items, it would get a path inside that
2020 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2021 * of an unprocessed inode. If in that case the first ref would be
2022 * overwritten, the overwritten inode gets "orphanized". Later when we
2023 * process this overwritten inode, it is restored at a new place by moving
2026 * sctx->send_progress tells this function at which point in time receiving
2029 static int get_cur_path(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2030 struct fs_path
*dest
)
2033 struct fs_path
*name
= NULL
;
2034 u64 parent_inode
= 0;
2038 name
= fs_path_alloc(sctx
);
2045 fs_path_reset(dest
);
2047 while (!stop
&& ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
2048 fs_path_reset(name
);
2050 ret
= __get_cur_name_and_parent(sctx
, ino
, gen
,
2051 &parent_inode
, &parent_gen
, name
);
2057 ret
= fs_path_add_path(dest
, name
);
2066 fs_path_free(sctx
, name
);
2068 fs_path_unreverse(dest
);
2073 * Called for regular files when sending extents data. Opens a struct file
2074 * to read from the file.
2076 static int open_cur_inode_file(struct send_ctx
*sctx
)
2079 struct btrfs_key key
;
2081 struct inode
*inode
;
2082 struct dentry
*dentry
;
2086 if (sctx
->cur_inode_filp
)
2089 key
.objectid
= sctx
->cur_ino
;
2090 key
.type
= BTRFS_INODE_ITEM_KEY
;
2093 inode
= btrfs_iget(sctx
->send_root
->fs_info
->sb
, &key
, sctx
->send_root
,
2095 if (IS_ERR(inode
)) {
2096 ret
= PTR_ERR(inode
);
2100 dentry
= d_obtain_alias(inode
);
2102 if (IS_ERR(dentry
)) {
2103 ret
= PTR_ERR(dentry
);
2107 path
.mnt
= sctx
->mnt
;
2108 path
.dentry
= dentry
;
2109 filp
= dentry_open(&path
, O_RDONLY
| O_LARGEFILE
, current_cred());
2113 ret
= PTR_ERR(filp
);
2116 sctx
->cur_inode_filp
= filp
;
2120 * no xxxput required here as every vfs op
2121 * does it by itself on failure
2127 * Closes the struct file that was created in open_cur_inode_file
2129 static int close_cur_inode_file(struct send_ctx
*sctx
)
2133 if (!sctx
->cur_inode_filp
)
2136 ret
= filp_close(sctx
->cur_inode_filp
, NULL
);
2137 sctx
->cur_inode_filp
= NULL
;
2144 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2146 static int send_subvol_begin(struct send_ctx
*sctx
)
2149 struct btrfs_root
*send_root
= sctx
->send_root
;
2150 struct btrfs_root
*parent_root
= sctx
->parent_root
;
2151 struct btrfs_path
*path
;
2152 struct btrfs_key key
;
2153 struct btrfs_root_ref
*ref
;
2154 struct extent_buffer
*leaf
;
2158 path
= alloc_path_for_send();
2162 name
= kmalloc(BTRFS_PATH_NAME_MAX
, GFP_NOFS
);
2164 btrfs_free_path(path
);
2168 key
.objectid
= send_root
->objectid
;
2169 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2172 ret
= btrfs_search_slot_for_read(send_root
->fs_info
->tree_root
,
2181 leaf
= path
->nodes
[0];
2182 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2183 if (key
.type
!= BTRFS_ROOT_BACKREF_KEY
||
2184 key
.objectid
!= send_root
->objectid
) {
2188 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
2189 namelen
= btrfs_root_ref_name_len(leaf
, ref
);
2190 read_extent_buffer(leaf
, name
, (unsigned long)(ref
+ 1), namelen
);
2191 btrfs_release_path(path
);
2194 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SNAPSHOT
);
2198 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SUBVOL
);
2203 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_PATH
, name
, namelen
);
2204 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_UUID
,
2205 sctx
->send_root
->root_item
.uuid
);
2206 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CTRANSID
,
2207 sctx
->send_root
->root_item
.ctransid
);
2209 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
2210 sctx
->parent_root
->root_item
.uuid
);
2211 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
2212 sctx
->parent_root
->root_item
.ctransid
);
2215 ret
= send_cmd(sctx
);
2219 btrfs_free_path(path
);
2224 static int send_truncate(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 size
)
2229 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino
, size
);
2231 p
= fs_path_alloc(sctx
);
2235 ret
= begin_cmd(sctx
, BTRFS_SEND_C_TRUNCATE
);
2239 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2242 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2243 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, size
);
2245 ret
= send_cmd(sctx
);
2249 fs_path_free(sctx
, p
);
2253 static int send_chmod(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 mode
)
2258 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino
, mode
);
2260 p
= fs_path_alloc(sctx
);
2264 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHMOD
);
2268 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2271 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2272 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
& 07777);
2274 ret
= send_cmd(sctx
);
2278 fs_path_free(sctx
, p
);
2282 static int send_chown(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 uid
, u64 gid
)
2287 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino
, uid
, gid
);
2289 p
= fs_path_alloc(sctx
);
2293 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHOWN
);
2297 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2300 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2301 TLV_PUT_U64(sctx
, BTRFS_SEND_A_UID
, uid
);
2302 TLV_PUT_U64(sctx
, BTRFS_SEND_A_GID
, gid
);
2304 ret
= send_cmd(sctx
);
2308 fs_path_free(sctx
, p
);
2312 static int send_utimes(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
2315 struct fs_path
*p
= NULL
;
2316 struct btrfs_inode_item
*ii
;
2317 struct btrfs_path
*path
= NULL
;
2318 struct extent_buffer
*eb
;
2319 struct btrfs_key key
;
2322 verbose_printk("btrfs: send_utimes %llu\n", ino
);
2324 p
= fs_path_alloc(sctx
);
2328 path
= alloc_path_for_send();
2335 key
.type
= BTRFS_INODE_ITEM_KEY
;
2337 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2341 eb
= path
->nodes
[0];
2342 slot
= path
->slots
[0];
2343 ii
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
2345 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UTIMES
);
2349 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2352 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2353 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_ATIME
, eb
,
2354 btrfs_inode_atime(ii
));
2355 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_MTIME
, eb
,
2356 btrfs_inode_mtime(ii
));
2357 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_CTIME
, eb
,
2358 btrfs_inode_ctime(ii
));
2359 /* TODO Add otime support when the otime patches get into upstream */
2361 ret
= send_cmd(sctx
);
2365 fs_path_free(sctx
, p
);
2366 btrfs_free_path(path
);
2371 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2372 * a valid path yet because we did not process the refs yet. So, the inode
2373 * is created as orphan.
2375 static int send_create_inode(struct send_ctx
*sctx
, u64 ino
)
2384 verbose_printk("btrfs: send_create_inode %llu\n", ino
);
2386 p
= fs_path_alloc(sctx
);
2390 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &gen
, &mode
, NULL
,
2395 if (S_ISREG(mode
)) {
2396 cmd
= BTRFS_SEND_C_MKFILE
;
2397 } else if (S_ISDIR(mode
)) {
2398 cmd
= BTRFS_SEND_C_MKDIR
;
2399 } else if (S_ISLNK(mode
)) {
2400 cmd
= BTRFS_SEND_C_SYMLINK
;
2401 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
2402 cmd
= BTRFS_SEND_C_MKNOD
;
2403 } else if (S_ISFIFO(mode
)) {
2404 cmd
= BTRFS_SEND_C_MKFIFO
;
2405 } else if (S_ISSOCK(mode
)) {
2406 cmd
= BTRFS_SEND_C_MKSOCK
;
2408 printk(KERN_WARNING
"btrfs: unexpected inode type %o",
2409 (int)(mode
& S_IFMT
));
2414 ret
= begin_cmd(sctx
, cmd
);
2418 ret
= gen_unique_name(sctx
, ino
, gen
, p
);
2422 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2423 TLV_PUT_U64(sctx
, BTRFS_SEND_A_INO
, ino
);
2425 if (S_ISLNK(mode
)) {
2427 ret
= read_symlink(sctx
, sctx
->send_root
, ino
, p
);
2430 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, p
);
2431 } else if (S_ISCHR(mode
) || S_ISBLK(mode
) ||
2432 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
2433 TLV_PUT_U64(sctx
, BTRFS_SEND_A_RDEV
, rdev
);
2436 ret
= send_cmd(sctx
);
2443 fs_path_free(sctx
, p
);
2448 * We need some special handling for inodes that get processed before the parent
2449 * directory got created. See process_recorded_refs for details.
2450 * This function does the check if we already created the dir out of order.
2452 static int did_create_dir(struct send_ctx
*sctx
, u64 dir
)
2455 struct btrfs_path
*path
= NULL
;
2456 struct btrfs_key key
;
2457 struct btrfs_key found_key
;
2458 struct btrfs_key di_key
;
2459 struct extent_buffer
*eb
;
2460 struct btrfs_dir_item
*di
;
2463 path
= alloc_path_for_send();
2470 key
.type
= BTRFS_DIR_INDEX_KEY
;
2473 ret
= btrfs_search_slot_for_read(sctx
->send_root
, &key
, path
,
2478 eb
= path
->nodes
[0];
2479 slot
= path
->slots
[0];
2480 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
2482 if (ret
|| found_key
.objectid
!= key
.objectid
||
2483 found_key
.type
!= key
.type
) {
2488 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
2489 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
2491 if (di_key
.objectid
< sctx
->send_progress
) {
2496 key
.offset
= found_key
.offset
+ 1;
2497 btrfs_release_path(path
);
2501 btrfs_free_path(path
);
2506 * Only creates the inode if it is:
2507 * 1. Not a directory
2508 * 2. Or a directory which was not created already due to out of order
2509 * directories. See did_create_dir and process_recorded_refs for details.
2511 static int send_create_inode_if_needed(struct send_ctx
*sctx
)
2515 if (S_ISDIR(sctx
->cur_inode_mode
)) {
2516 ret
= did_create_dir(sctx
, sctx
->cur_ino
);
2525 ret
= send_create_inode(sctx
, sctx
->cur_ino
);
2533 struct recorded_ref
{
2534 struct list_head list
;
2537 struct fs_path
*full_path
;
2545 * We need to process new refs before deleted refs, but compare_tree gives us
2546 * everything mixed. So we first record all refs and later process them.
2547 * This function is a helper to record one ref.
2549 static int record_ref(struct list_head
*head
, u64 dir
,
2550 u64 dir_gen
, struct fs_path
*path
)
2552 struct recorded_ref
*ref
;
2555 ref
= kmalloc(sizeof(*ref
), GFP_NOFS
);
2560 ref
->dir_gen
= dir_gen
;
2561 ref
->full_path
= path
;
2563 tmp
= strrchr(ref
->full_path
->start
, '/');
2565 ref
->name_len
= ref
->full_path
->end
- ref
->full_path
->start
;
2566 ref
->name
= ref
->full_path
->start
;
2567 ref
->dir_path_len
= 0;
2568 ref
->dir_path
= ref
->full_path
->start
;
2571 ref
->name_len
= ref
->full_path
->end
- tmp
;
2573 ref
->dir_path
= ref
->full_path
->start
;
2574 ref
->dir_path_len
= ref
->full_path
->end
-
2575 ref
->full_path
->start
- 1 - ref
->name_len
;
2578 list_add_tail(&ref
->list
, head
);
2582 static void __free_recorded_refs(struct send_ctx
*sctx
, struct list_head
*head
)
2584 struct recorded_ref
*cur
;
2586 while (!list_empty(head
)) {
2587 cur
= list_entry(head
->next
, struct recorded_ref
, list
);
2588 fs_path_free(sctx
, cur
->full_path
);
2589 list_del(&cur
->list
);
2594 static void free_recorded_refs(struct send_ctx
*sctx
)
2596 __free_recorded_refs(sctx
, &sctx
->new_refs
);
2597 __free_recorded_refs(sctx
, &sctx
->deleted_refs
);
2601 * Renames/moves a file/dir to its orphan name. Used when the first
2602 * ref of an unprocessed inode gets overwritten and for all non empty
2605 static int orphanize_inode(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2606 struct fs_path
*path
)
2609 struct fs_path
*orphan
;
2611 orphan
= fs_path_alloc(sctx
);
2615 ret
= gen_unique_name(sctx
, ino
, gen
, orphan
);
2619 ret
= send_rename(sctx
, path
, orphan
);
2622 fs_path_free(sctx
, orphan
);
2627 * Returns 1 if a directory can be removed at this point in time.
2628 * We check this by iterating all dir items and checking if the inode behind
2629 * the dir item was already processed.
2631 static int can_rmdir(struct send_ctx
*sctx
, u64 dir
, u64 send_progress
)
2634 struct btrfs_root
*root
= sctx
->parent_root
;
2635 struct btrfs_path
*path
;
2636 struct btrfs_key key
;
2637 struct btrfs_key found_key
;
2638 struct btrfs_key loc
;
2639 struct btrfs_dir_item
*di
;
2642 * Don't try to rmdir the top/root subvolume dir.
2644 if (dir
== BTRFS_FIRST_FREE_OBJECTID
)
2647 path
= alloc_path_for_send();
2652 key
.type
= BTRFS_DIR_INDEX_KEY
;
2656 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
2660 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2663 if (ret
|| found_key
.objectid
!= key
.objectid
||
2664 found_key
.type
!= key
.type
) {
2668 di
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2669 struct btrfs_dir_item
);
2670 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &loc
);
2672 if (loc
.objectid
> send_progress
) {
2677 btrfs_release_path(path
);
2678 key
.offset
= found_key
.offset
+ 1;
2684 btrfs_free_path(path
);
2689 * This does all the move/link/unlink/rmdir magic.
2691 static int process_recorded_refs(struct send_ctx
*sctx
)
2694 struct recorded_ref
*cur
;
2695 struct recorded_ref
*cur2
;
2696 struct ulist
*check_dirs
= NULL
;
2697 struct ulist_iterator uit
;
2698 struct ulist_node
*un
;
2699 struct fs_path
*valid_path
= NULL
;
2702 int did_overwrite
= 0;
2705 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx
->cur_ino
);
2708 * This should never happen as the root dir always has the same ref
2709 * which is always '..'
2711 BUG_ON(sctx
->cur_ino
<= BTRFS_FIRST_FREE_OBJECTID
);
2713 valid_path
= fs_path_alloc(sctx
);
2719 check_dirs
= ulist_alloc(GFP_NOFS
);
2726 * First, check if the first ref of the current inode was overwritten
2727 * before. If yes, we know that the current inode was already orphanized
2728 * and thus use the orphan name. If not, we can use get_cur_path to
2729 * get the path of the first ref as it would like while receiving at
2730 * this point in time.
2731 * New inodes are always orphan at the beginning, so force to use the
2732 * orphan name in this case.
2733 * The first ref is stored in valid_path and will be updated if it
2734 * gets moved around.
2736 if (!sctx
->cur_inode_new
) {
2737 ret
= did_overwrite_first_ref(sctx
, sctx
->cur_ino
,
2738 sctx
->cur_inode_gen
);
2744 if (sctx
->cur_inode_new
|| did_overwrite
) {
2745 ret
= gen_unique_name(sctx
, sctx
->cur_ino
,
2746 sctx
->cur_inode_gen
, valid_path
);
2751 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
2757 list_for_each_entry(cur
, &sctx
->new_refs
, list
) {
2759 * We may have refs where the parent directory does not exist
2760 * yet. This happens if the parent directories inum is higher
2761 * the the current inum. To handle this case, we create the
2762 * parent directory out of order. But we need to check if this
2763 * did already happen before due to other refs in the same dir.
2765 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
2768 if (ret
== inode_state_will_create
) {
2771 * First check if any of the current inodes refs did
2772 * already create the dir.
2774 list_for_each_entry(cur2
, &sctx
->new_refs
, list
) {
2777 if (cur2
->dir
== cur
->dir
) {
2784 * If that did not happen, check if a previous inode
2785 * did already create the dir.
2788 ret
= did_create_dir(sctx
, cur
->dir
);
2792 ret
= send_create_inode(sctx
, cur
->dir
);
2799 * Check if this new ref would overwrite the first ref of
2800 * another unprocessed inode. If yes, orphanize the
2801 * overwritten inode. If we find an overwritten ref that is
2802 * not the first ref, simply unlink it.
2804 ret
= will_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
2805 cur
->name
, cur
->name_len
,
2806 &ow_inode
, &ow_gen
);
2810 ret
= is_first_ref(sctx
, sctx
->parent_root
,
2811 ow_inode
, cur
->dir
, cur
->name
,
2816 ret
= orphanize_inode(sctx
, ow_inode
, ow_gen
,
2821 ret
= send_unlink(sctx
, cur
->full_path
);
2828 * link/move the ref to the new place. If we have an orphan
2829 * inode, move it and update valid_path. If not, link or move
2830 * it depending on the inode mode.
2833 ret
= send_rename(sctx
, valid_path
, cur
->full_path
);
2837 ret
= fs_path_copy(valid_path
, cur
->full_path
);
2841 if (S_ISDIR(sctx
->cur_inode_mode
)) {
2843 * Dirs can't be linked, so move it. For moved
2844 * dirs, we always have one new and one deleted
2845 * ref. The deleted ref is ignored later.
2847 ret
= send_rename(sctx
, valid_path
,
2851 ret
= fs_path_copy(valid_path
, cur
->full_path
);
2855 ret
= send_link(sctx
, cur
->full_path
,
2861 ret
= ulist_add(check_dirs
, cur
->dir
, cur
->dir_gen
,
2867 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->cur_inode_deleted
) {
2869 * Check if we can already rmdir the directory. If not,
2870 * orphanize it. For every dir item inside that gets deleted
2871 * later, we do this check again and rmdir it then if possible.
2872 * See the use of check_dirs for more details.
2874 ret
= can_rmdir(sctx
, sctx
->cur_ino
, sctx
->cur_ino
);
2878 ret
= send_rmdir(sctx
, valid_path
);
2881 } else if (!is_orphan
) {
2882 ret
= orphanize_inode(sctx
, sctx
->cur_ino
,
2883 sctx
->cur_inode_gen
, valid_path
);
2889 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
2890 ret
= ulist_add(check_dirs
, cur
->dir
, cur
->dir_gen
,
2895 } else if (S_ISDIR(sctx
->cur_inode_mode
) &&
2896 !list_empty(&sctx
->deleted_refs
)) {
2898 * We have a moved dir. Add the old parent to check_dirs
2900 cur
= list_entry(sctx
->deleted_refs
.next
, struct recorded_ref
,
2902 ret
= ulist_add(check_dirs
, cur
->dir
, cur
->dir_gen
,
2906 } else if (!S_ISDIR(sctx
->cur_inode_mode
)) {
2908 * We have a non dir inode. Go through all deleted refs and
2909 * unlink them if they were not already overwritten by other
2912 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
2913 ret
= did_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
2914 sctx
->cur_ino
, sctx
->cur_inode_gen
,
2915 cur
->name
, cur
->name_len
);
2919 ret
= send_unlink(sctx
, cur
->full_path
);
2923 ret
= ulist_add(check_dirs
, cur
->dir
, cur
->dir_gen
,
2930 * If the inode is still orphan, unlink the orphan. This may
2931 * happen when a previous inode did overwrite the first ref
2932 * of this inode and no new refs were added for the current
2933 * inode. Unlinking does not mean that the inode is deleted in
2934 * all cases. There may still be links to this inode in other
2938 ret
= send_unlink(sctx
, valid_path
);
2945 * We did collect all parent dirs where cur_inode was once located. We
2946 * now go through all these dirs and check if they are pending for
2947 * deletion and if it's finally possible to perform the rmdir now.
2948 * We also update the inode stats of the parent dirs here.
2950 ULIST_ITER_INIT(&uit
);
2951 while ((un
= ulist_next(check_dirs
, &uit
))) {
2953 * In case we had refs into dirs that were not processed yet,
2954 * we don't need to do the utime and rmdir logic for these dirs.
2955 * The dir will be processed later.
2957 if (un
->val
> sctx
->cur_ino
)
2960 ret
= get_cur_inode_state(sctx
, un
->val
, un
->aux
);
2964 if (ret
== inode_state_did_create
||
2965 ret
== inode_state_no_change
) {
2966 /* TODO delayed utimes */
2967 ret
= send_utimes(sctx
, un
->val
, un
->aux
);
2970 } else if (ret
== inode_state_did_delete
) {
2971 ret
= can_rmdir(sctx
, un
->val
, sctx
->cur_ino
);
2975 ret
= get_cur_path(sctx
, un
->val
, un
->aux
,
2979 ret
= send_rmdir(sctx
, valid_path
);
2989 free_recorded_refs(sctx
);
2990 ulist_free(check_dirs
);
2991 fs_path_free(sctx
, valid_path
);
2995 static int __record_new_ref(int num
, u64 dir
, int index
,
2996 struct fs_path
*name
,
3000 struct send_ctx
*sctx
= ctx
;
3004 p
= fs_path_alloc(sctx
);
3008 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &gen
, NULL
, NULL
,
3013 ret
= get_cur_path(sctx
, dir
, gen
, p
);
3016 ret
= fs_path_add_path(p
, name
);
3020 ret
= record_ref(&sctx
->new_refs
, dir
, gen
, p
);
3024 fs_path_free(sctx
, p
);
3028 static int __record_deleted_ref(int num
, u64 dir
, int index
,
3029 struct fs_path
*name
,
3033 struct send_ctx
*sctx
= ctx
;
3037 p
= fs_path_alloc(sctx
);
3041 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &gen
, NULL
, NULL
,
3046 ret
= get_cur_path(sctx
, dir
, gen
, p
);
3049 ret
= fs_path_add_path(p
, name
);
3053 ret
= record_ref(&sctx
->deleted_refs
, dir
, gen
, p
);
3057 fs_path_free(sctx
, p
);
3061 static int record_new_ref(struct send_ctx
*sctx
)
3065 ret
= iterate_inode_ref(sctx
, sctx
->send_root
, sctx
->left_path
,
3066 sctx
->cmp_key
, 0, __record_new_ref
, sctx
);
3075 static int record_deleted_ref(struct send_ctx
*sctx
)
3079 ret
= iterate_inode_ref(sctx
, sctx
->parent_root
, sctx
->right_path
,
3080 sctx
->cmp_key
, 0, __record_deleted_ref
, sctx
);
3089 struct find_ref_ctx
{
3091 struct fs_path
*name
;
3095 static int __find_iref(int num
, u64 dir
, int index
,
3096 struct fs_path
*name
,
3099 struct find_ref_ctx
*ctx
= ctx_
;
3101 if (dir
== ctx
->dir
&& fs_path_len(name
) == fs_path_len(ctx
->name
) &&
3102 strncmp(name
->start
, ctx
->name
->start
, fs_path_len(name
)) == 0) {
3103 ctx
->found_idx
= num
;
3109 static int find_iref(struct send_ctx
*sctx
,
3110 struct btrfs_root
*root
,
3111 struct btrfs_path
*path
,
3112 struct btrfs_key
*key
,
3113 u64 dir
, struct fs_path
*name
)
3116 struct find_ref_ctx ctx
;
3122 ret
= iterate_inode_ref(sctx
, root
, path
, key
, 0, __find_iref
, &ctx
);
3126 if (ctx
.found_idx
== -1)
3129 return ctx
.found_idx
;
3132 static int __record_changed_new_ref(int num
, u64 dir
, int index
,
3133 struct fs_path
*name
,
3137 struct send_ctx
*sctx
= ctx
;
3139 ret
= find_iref(sctx
, sctx
->parent_root
, sctx
->right_path
,
3140 sctx
->cmp_key
, dir
, name
);
3142 ret
= __record_new_ref(num
, dir
, index
, name
, sctx
);
3149 static int __record_changed_deleted_ref(int num
, u64 dir
, int index
,
3150 struct fs_path
*name
,
3154 struct send_ctx
*sctx
= ctx
;
3156 ret
= find_iref(sctx
, sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
3159 ret
= __record_deleted_ref(num
, dir
, index
, name
, sctx
);
3166 static int record_changed_ref(struct send_ctx
*sctx
)
3170 ret
= iterate_inode_ref(sctx
, sctx
->send_root
, sctx
->left_path
,
3171 sctx
->cmp_key
, 0, __record_changed_new_ref
, sctx
);
3174 ret
= iterate_inode_ref(sctx
, sctx
->parent_root
, sctx
->right_path
,
3175 sctx
->cmp_key
, 0, __record_changed_deleted_ref
, sctx
);
3185 * Record and process all refs at once. Needed when an inode changes the
3186 * generation number, which means that it was deleted and recreated.
3188 static int process_all_refs(struct send_ctx
*sctx
,
3189 enum btrfs_compare_tree_result cmd
)
3192 struct btrfs_root
*root
;
3193 struct btrfs_path
*path
;
3194 struct btrfs_key key
;
3195 struct btrfs_key found_key
;
3196 struct extent_buffer
*eb
;
3198 iterate_inode_ref_t cb
;
3200 path
= alloc_path_for_send();
3204 if (cmd
== BTRFS_COMPARE_TREE_NEW
) {
3205 root
= sctx
->send_root
;
3206 cb
= __record_new_ref
;
3207 } else if (cmd
== BTRFS_COMPARE_TREE_DELETED
) {
3208 root
= sctx
->parent_root
;
3209 cb
= __record_deleted_ref
;
3214 key
.objectid
= sctx
->cmp_key
->objectid
;
3215 key
.type
= BTRFS_INODE_REF_KEY
;
3218 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
3224 eb
= path
->nodes
[0];
3225 slot
= path
->slots
[0];
3226 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3228 if (found_key
.objectid
!= key
.objectid
||
3229 found_key
.type
!= key
.type
)
3232 ret
= iterate_inode_ref(sctx
, root
, path
, &found_key
, 0, cb
,
3234 btrfs_release_path(path
);
3238 key
.offset
= found_key
.offset
+ 1;
3240 btrfs_release_path(path
);
3242 ret
= process_recorded_refs(sctx
);
3245 btrfs_free_path(path
);
3249 static int send_set_xattr(struct send_ctx
*sctx
,
3250 struct fs_path
*path
,
3251 const char *name
, int name_len
,
3252 const char *data
, int data_len
)
3256 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SET_XATTR
);
3260 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
3261 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
3262 TLV_PUT(sctx
, BTRFS_SEND_A_XATTR_DATA
, data
, data_len
);
3264 ret
= send_cmd(sctx
);
3271 static int send_remove_xattr(struct send_ctx
*sctx
,
3272 struct fs_path
*path
,
3273 const char *name
, int name_len
)
3277 ret
= begin_cmd(sctx
, BTRFS_SEND_C_REMOVE_XATTR
);
3281 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
3282 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
3284 ret
= send_cmd(sctx
);
3291 static int __process_new_xattr(int num
, struct btrfs_key
*di_key
,
3292 const char *name
, int name_len
,
3293 const char *data
, int data_len
,
3297 struct send_ctx
*sctx
= ctx
;
3299 posix_acl_xattr_header dummy_acl
;
3301 p
= fs_path_alloc(sctx
);
3306 * This hack is needed because empty acl's are stored as zero byte
3307 * data in xattrs. Problem with that is, that receiving these zero byte
3308 * acl's will fail later. To fix this, we send a dummy acl list that
3309 * only contains the version number and no entries.
3311 if (!strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
, name_len
) ||
3312 !strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
, name_len
)) {
3313 if (data_len
== 0) {
3314 dummy_acl
.a_version
=
3315 cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
3316 data
= (char *)&dummy_acl
;
3317 data_len
= sizeof(dummy_acl
);
3321 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3325 ret
= send_set_xattr(sctx
, p
, name
, name_len
, data
, data_len
);
3328 fs_path_free(sctx
, p
);
3332 static int __process_deleted_xattr(int num
, struct btrfs_key
*di_key
,
3333 const char *name
, int name_len
,
3334 const char *data
, int data_len
,
3338 struct send_ctx
*sctx
= ctx
;
3341 p
= fs_path_alloc(sctx
);
3345 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3349 ret
= send_remove_xattr(sctx
, p
, name
, name_len
);
3352 fs_path_free(sctx
, p
);
3356 static int process_new_xattr(struct send_ctx
*sctx
)
3360 ret
= iterate_dir_item(sctx
, sctx
->send_root
, sctx
->left_path
,
3361 sctx
->cmp_key
, __process_new_xattr
, sctx
);
3366 static int process_deleted_xattr(struct send_ctx
*sctx
)
3370 ret
= iterate_dir_item(sctx
, sctx
->parent_root
, sctx
->right_path
,
3371 sctx
->cmp_key
, __process_deleted_xattr
, sctx
);
3376 struct find_xattr_ctx
{
3384 static int __find_xattr(int num
, struct btrfs_key
*di_key
,
3385 const char *name
, int name_len
,
3386 const char *data
, int data_len
,
3387 u8 type
, void *vctx
)
3389 struct find_xattr_ctx
*ctx
= vctx
;
3391 if (name_len
== ctx
->name_len
&&
3392 strncmp(name
, ctx
->name
, name_len
) == 0) {
3393 ctx
->found_idx
= num
;
3394 ctx
->found_data_len
= data_len
;
3395 ctx
->found_data
= kmalloc(data_len
, GFP_NOFS
);
3396 if (!ctx
->found_data
)
3398 memcpy(ctx
->found_data
, data
, data_len
);
3404 static int find_xattr(struct send_ctx
*sctx
,
3405 struct btrfs_root
*root
,
3406 struct btrfs_path
*path
,
3407 struct btrfs_key
*key
,
3408 const char *name
, int name_len
,
3409 char **data
, int *data_len
)
3412 struct find_xattr_ctx ctx
;
3415 ctx
.name_len
= name_len
;
3417 ctx
.found_data
= NULL
;
3418 ctx
.found_data_len
= 0;
3420 ret
= iterate_dir_item(sctx
, root
, path
, key
, __find_xattr
, &ctx
);
3424 if (ctx
.found_idx
== -1)
3427 *data
= ctx
.found_data
;
3428 *data_len
= ctx
.found_data_len
;
3430 kfree(ctx
.found_data
);
3432 return ctx
.found_idx
;
3436 static int __process_changed_new_xattr(int num
, struct btrfs_key
*di_key
,
3437 const char *name
, int name_len
,
3438 const char *data
, int data_len
,
3442 struct send_ctx
*sctx
= ctx
;
3443 char *found_data
= NULL
;
3444 int found_data_len
= 0;
3445 struct fs_path
*p
= NULL
;
3447 ret
= find_xattr(sctx
, sctx
->parent_root
, sctx
->right_path
,
3448 sctx
->cmp_key
, name
, name_len
, &found_data
,
3450 if (ret
== -ENOENT
) {
3451 ret
= __process_new_xattr(num
, di_key
, name
, name_len
, data
,
3452 data_len
, type
, ctx
);
3453 } else if (ret
>= 0) {
3454 if (data_len
!= found_data_len
||
3455 memcmp(data
, found_data
, data_len
)) {
3456 ret
= __process_new_xattr(num
, di_key
, name
, name_len
,
3457 data
, data_len
, type
, ctx
);
3464 fs_path_free(sctx
, p
);
3468 static int __process_changed_deleted_xattr(int num
, struct btrfs_key
*di_key
,
3469 const char *name
, int name_len
,
3470 const char *data
, int data_len
,
3474 struct send_ctx
*sctx
= ctx
;
3476 ret
= find_xattr(sctx
, sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
3477 name
, name_len
, NULL
, NULL
);
3479 ret
= __process_deleted_xattr(num
, di_key
, name
, name_len
, data
,
3480 data_len
, type
, ctx
);
3487 static int process_changed_xattr(struct send_ctx
*sctx
)
3491 ret
= iterate_dir_item(sctx
, sctx
->send_root
, sctx
->left_path
,
3492 sctx
->cmp_key
, __process_changed_new_xattr
, sctx
);
3495 ret
= iterate_dir_item(sctx
, sctx
->parent_root
, sctx
->right_path
,
3496 sctx
->cmp_key
, __process_changed_deleted_xattr
, sctx
);
3502 static int process_all_new_xattrs(struct send_ctx
*sctx
)
3505 struct btrfs_root
*root
;
3506 struct btrfs_path
*path
;
3507 struct btrfs_key key
;
3508 struct btrfs_key found_key
;
3509 struct extent_buffer
*eb
;
3512 path
= alloc_path_for_send();
3516 root
= sctx
->send_root
;
3518 key
.objectid
= sctx
->cmp_key
->objectid
;
3519 key
.type
= BTRFS_XATTR_ITEM_KEY
;
3522 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
3530 eb
= path
->nodes
[0];
3531 slot
= path
->slots
[0];
3532 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3534 if (found_key
.objectid
!= key
.objectid
||
3535 found_key
.type
!= key
.type
) {
3540 ret
= iterate_dir_item(sctx
, root
, path
, &found_key
,
3541 __process_new_xattr
, sctx
);
3545 btrfs_release_path(path
);
3546 key
.offset
= found_key
.offset
+ 1;
3550 btrfs_free_path(path
);
3555 * Read some bytes from the current inode/file and send a write command to
3558 static int send_write(struct send_ctx
*sctx
, u64 offset
, u32 len
)
3562 loff_t pos
= offset
;
3564 mm_segment_t old_fs
;
3566 p
= fs_path_alloc(sctx
);
3571 * vfs normally only accepts user space buffers for security reasons.
3572 * we only read from the file and also only provide the read_buf buffer
3573 * to vfs. As this buffer does not come from a user space call, it's
3574 * ok to temporary allow kernel space buffers.
3579 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset
, len
);
3581 ret
= open_cur_inode_file(sctx
);
3585 ret
= vfs_read(sctx
->cur_inode_filp
, sctx
->read_buf
, len
, &pos
);
3592 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
3596 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3600 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
3601 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
3602 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, num_read
);
3604 ret
= send_cmd(sctx
);
3608 fs_path_free(sctx
, p
);
3616 * Send a clone command to user space.
3618 static int send_clone(struct send_ctx
*sctx
,
3619 u64 offset
, u32 len
,
3620 struct clone_root
*clone_root
)
3626 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3627 "clone_inode=%llu, clone_offset=%llu\n", offset
, len
,
3628 clone_root
->root
->objectid
, clone_root
->ino
,
3629 clone_root
->offset
);
3631 p
= fs_path_alloc(sctx
);
3635 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CLONE
);
3639 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3643 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
3644 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_LEN
, len
);
3645 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
3647 if (clone_root
->root
== sctx
->send_root
) {
3648 ret
= get_inode_info(sctx
->send_root
, clone_root
->ino
, NULL
,
3649 &gen
, NULL
, NULL
, NULL
, NULL
);
3652 ret
= get_cur_path(sctx
, clone_root
->ino
, gen
, p
);
3654 ret
= get_inode_path(sctx
, clone_root
->root
,
3655 clone_root
->ino
, p
);
3660 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
3661 clone_root
->root
->root_item
.uuid
);
3662 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
3663 clone_root
->root
->root_item
.ctransid
);
3664 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_CLONE_PATH
, p
);
3665 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_OFFSET
,
3666 clone_root
->offset
);
3668 ret
= send_cmd(sctx
);
3672 fs_path_free(sctx
, p
);
3676 static int send_write_or_clone(struct send_ctx
*sctx
,
3677 struct btrfs_path
*path
,
3678 struct btrfs_key
*key
,
3679 struct clone_root
*clone_root
)
3682 struct btrfs_file_extent_item
*ei
;
3683 u64 offset
= key
->offset
;
3689 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3690 struct btrfs_file_extent_item
);
3691 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
3692 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3693 len
= btrfs_file_extent_inline_len(path
->nodes
[0], ei
);
3695 * it is possible the inline item won't cover the whole page,
3696 * but there may be items after this page. Make
3697 * sure to send the whole thing
3699 len
= PAGE_CACHE_ALIGN(len
);
3701 len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
3704 if (offset
+ len
> sctx
->cur_inode_size
)
3705 len
= sctx
->cur_inode_size
- offset
;
3714 if (l
> BTRFS_SEND_READ_SIZE
)
3715 l
= BTRFS_SEND_READ_SIZE
;
3716 ret
= send_write(sctx
, pos
+ offset
, l
);
3725 ret
= send_clone(sctx
, offset
, len
, clone_root
);
3732 static int is_extent_unchanged(struct send_ctx
*sctx
,
3733 struct btrfs_path
*left_path
,
3734 struct btrfs_key
*ekey
)
3737 struct btrfs_key key
;
3738 struct btrfs_path
*path
= NULL
;
3739 struct extent_buffer
*eb
;
3741 struct btrfs_key found_key
;
3742 struct btrfs_file_extent_item
*ei
;
3747 u64 left_offset_fixed
;
3755 path
= alloc_path_for_send();
3759 eb
= left_path
->nodes
[0];
3760 slot
= left_path
->slots
[0];
3761 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
3762 left_type
= btrfs_file_extent_type(eb
, ei
);
3764 if (left_type
!= BTRFS_FILE_EXTENT_REG
) {
3768 left_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
3769 left_len
= btrfs_file_extent_num_bytes(eb
, ei
);
3770 left_offset
= btrfs_file_extent_offset(eb
, ei
);
3771 left_gen
= btrfs_file_extent_generation(eb
, ei
);
3774 * Following comments will refer to these graphics. L is the left
3775 * extents which we are checking at the moment. 1-8 are the right
3776 * extents that we iterate.
3779 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3782 * |--1--|-2b-|...(same as above)
3784 * Alternative situation. Happens on files where extents got split.
3786 * |-----------7-----------|-6-|
3788 * Alternative situation. Happens on files which got larger.
3791 * Nothing follows after 8.
3794 key
.objectid
= ekey
->objectid
;
3795 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3796 key
.offset
= ekey
->offset
;
3797 ret
= btrfs_search_slot_for_read(sctx
->parent_root
, &key
, path
, 0, 0);
3806 * Handle special case where the right side has no extents at all.
3808 eb
= path
->nodes
[0];
3809 slot
= path
->slots
[0];
3810 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3811 if (found_key
.objectid
!= key
.objectid
||
3812 found_key
.type
!= key
.type
) {
3818 * We're now on 2a, 2b or 7.
3821 while (key
.offset
< ekey
->offset
+ left_len
) {
3822 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
3823 right_type
= btrfs_file_extent_type(eb
, ei
);
3824 right_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
3825 right_len
= btrfs_file_extent_num_bytes(eb
, ei
);
3826 right_offset
= btrfs_file_extent_offset(eb
, ei
);
3827 right_gen
= btrfs_file_extent_generation(eb
, ei
);
3829 if (right_type
!= BTRFS_FILE_EXTENT_REG
) {
3835 * Are we at extent 8? If yes, we know the extent is changed.
3836 * This may only happen on the first iteration.
3838 if (found_key
.offset
+ right_len
<= ekey
->offset
) {
3843 left_offset_fixed
= left_offset
;
3844 if (key
.offset
< ekey
->offset
) {
3845 /* Fix the right offset for 2a and 7. */
3846 right_offset
+= ekey
->offset
- key
.offset
;
3848 /* Fix the left offset for all behind 2a and 2b */
3849 left_offset_fixed
+= key
.offset
- ekey
->offset
;
3853 * Check if we have the same extent.
3855 if (left_disknr
!= right_disknr
||
3856 left_offset_fixed
!= right_offset
||
3857 left_gen
!= right_gen
) {
3863 * Go to the next extent.
3865 ret
= btrfs_next_item(sctx
->parent_root
, path
);
3869 eb
= path
->nodes
[0];
3870 slot
= path
->slots
[0];
3871 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3873 if (ret
|| found_key
.objectid
!= key
.objectid
||
3874 found_key
.type
!= key
.type
) {
3875 key
.offset
+= right_len
;
3878 if (found_key
.offset
!= key
.offset
+ right_len
) {
3879 /* Should really not happen */
3888 * We're now behind the left extent (treat as unchanged) or at the end
3889 * of the right side (treat as changed).
3891 if (key
.offset
>= ekey
->offset
+ left_len
)
3898 btrfs_free_path(path
);
3902 static int process_extent(struct send_ctx
*sctx
,
3903 struct btrfs_path
*path
,
3904 struct btrfs_key
*key
)
3907 struct clone_root
*found_clone
= NULL
;
3909 if (S_ISLNK(sctx
->cur_inode_mode
))
3912 if (sctx
->parent_root
&& !sctx
->cur_inode_new
) {
3913 ret
= is_extent_unchanged(sctx
, path
, key
);
3922 ret
= find_extent_clone(sctx
, path
, key
->objectid
, key
->offset
,
3923 sctx
->cur_inode_size
, &found_clone
);
3924 if (ret
!= -ENOENT
&& ret
< 0)
3927 ret
= send_write_or_clone(sctx
, path
, key
, found_clone
);
3933 static int process_all_extents(struct send_ctx
*sctx
)
3936 struct btrfs_root
*root
;
3937 struct btrfs_path
*path
;
3938 struct btrfs_key key
;
3939 struct btrfs_key found_key
;
3940 struct extent_buffer
*eb
;
3943 root
= sctx
->send_root
;
3944 path
= alloc_path_for_send();
3948 key
.objectid
= sctx
->cmp_key
->objectid
;
3949 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3952 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
3960 eb
= path
->nodes
[0];
3961 slot
= path
->slots
[0];
3962 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3964 if (found_key
.objectid
!= key
.objectid
||
3965 found_key
.type
!= key
.type
) {
3970 ret
= process_extent(sctx
, path
, &found_key
);
3974 btrfs_release_path(path
);
3975 key
.offset
= found_key
.offset
+ 1;
3979 btrfs_free_path(path
);
3983 static int process_recorded_refs_if_needed(struct send_ctx
*sctx
, int at_end
)
3987 if (sctx
->cur_ino
== 0)
3989 if (!at_end
&& sctx
->cur_ino
== sctx
->cmp_key
->objectid
&&
3990 sctx
->cmp_key
->type
<= BTRFS_INODE_REF_KEY
)
3992 if (list_empty(&sctx
->new_refs
) && list_empty(&sctx
->deleted_refs
))
3995 ret
= process_recorded_refs(sctx
);
4000 * We have processed the refs and thus need to advance send_progress.
4001 * Now, calls to get_cur_xxx will take the updated refs of the current
4002 * inode into account.
4004 sctx
->send_progress
= sctx
->cur_ino
+ 1;
4010 static int finish_inode_if_needed(struct send_ctx
*sctx
, int at_end
)
4022 ret
= process_recorded_refs_if_needed(sctx
, at_end
);
4026 if (sctx
->cur_ino
== 0 || sctx
->cur_inode_deleted
)
4028 if (!at_end
&& sctx
->cmp_key
->objectid
== sctx
->cur_ino
)
4031 ret
= get_inode_info(sctx
->send_root
, sctx
->cur_ino
, NULL
, NULL
,
4032 &left_mode
, &left_uid
, &left_gid
, NULL
);
4036 if (!S_ISLNK(sctx
->cur_inode_mode
)) {
4037 if (!sctx
->parent_root
|| sctx
->cur_inode_new
) {
4041 ret
= get_inode_info(sctx
->parent_root
, sctx
->cur_ino
,
4042 NULL
, NULL
, &right_mode
, &right_uid
,
4047 if (left_uid
!= right_uid
|| left_gid
!= right_gid
)
4049 if (left_mode
!= right_mode
)
4054 if (S_ISREG(sctx
->cur_inode_mode
)) {
4055 ret
= send_truncate(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4056 sctx
->cur_inode_size
);
4062 ret
= send_chown(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4063 left_uid
, left_gid
);
4068 ret
= send_chmod(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4075 * Need to send that every time, no matter if it actually changed
4076 * between the two trees as we have done changes to the inode before.
4078 ret
= send_utimes(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
);
4086 static int changed_inode(struct send_ctx
*sctx
,
4087 enum btrfs_compare_tree_result result
)
4090 struct btrfs_key
*key
= sctx
->cmp_key
;
4091 struct btrfs_inode_item
*left_ii
= NULL
;
4092 struct btrfs_inode_item
*right_ii
= NULL
;
4096 ret
= close_cur_inode_file(sctx
);
4100 sctx
->cur_ino
= key
->objectid
;
4101 sctx
->cur_inode_new_gen
= 0;
4104 * Set send_progress to current inode. This will tell all get_cur_xxx
4105 * functions that the current inode's refs are not updated yet. Later,
4106 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4108 sctx
->send_progress
= sctx
->cur_ino
;
4110 if (result
== BTRFS_COMPARE_TREE_NEW
||
4111 result
== BTRFS_COMPARE_TREE_CHANGED
) {
4112 left_ii
= btrfs_item_ptr(sctx
->left_path
->nodes
[0],
4113 sctx
->left_path
->slots
[0],
4114 struct btrfs_inode_item
);
4115 left_gen
= btrfs_inode_generation(sctx
->left_path
->nodes
[0],
4118 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
4119 sctx
->right_path
->slots
[0],
4120 struct btrfs_inode_item
);
4121 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
4124 if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
4125 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
4126 sctx
->right_path
->slots
[0],
4127 struct btrfs_inode_item
);
4129 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
4133 * The cur_ino = root dir case is special here. We can't treat
4134 * the inode as deleted+reused because it would generate a
4135 * stream that tries to delete/mkdir the root dir.
4137 if (left_gen
!= right_gen
&&
4138 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
4139 sctx
->cur_inode_new_gen
= 1;
4142 if (result
== BTRFS_COMPARE_TREE_NEW
) {
4143 sctx
->cur_inode_gen
= left_gen
;
4144 sctx
->cur_inode_new
= 1;
4145 sctx
->cur_inode_deleted
= 0;
4146 sctx
->cur_inode_size
= btrfs_inode_size(
4147 sctx
->left_path
->nodes
[0], left_ii
);
4148 sctx
->cur_inode_mode
= btrfs_inode_mode(
4149 sctx
->left_path
->nodes
[0], left_ii
);
4150 if (sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
4151 ret
= send_create_inode_if_needed(sctx
);
4152 } else if (result
== BTRFS_COMPARE_TREE_DELETED
) {
4153 sctx
->cur_inode_gen
= right_gen
;
4154 sctx
->cur_inode_new
= 0;
4155 sctx
->cur_inode_deleted
= 1;
4156 sctx
->cur_inode_size
= btrfs_inode_size(
4157 sctx
->right_path
->nodes
[0], right_ii
);
4158 sctx
->cur_inode_mode
= btrfs_inode_mode(
4159 sctx
->right_path
->nodes
[0], right_ii
);
4160 } else if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
4162 * We need to do some special handling in case the inode was
4163 * reported as changed with a changed generation number. This
4164 * means that the original inode was deleted and new inode
4165 * reused the same inum. So we have to treat the old inode as
4166 * deleted and the new one as new.
4168 if (sctx
->cur_inode_new_gen
) {
4170 * First, process the inode as if it was deleted.
4172 sctx
->cur_inode_gen
= right_gen
;
4173 sctx
->cur_inode_new
= 0;
4174 sctx
->cur_inode_deleted
= 1;
4175 sctx
->cur_inode_size
= btrfs_inode_size(
4176 sctx
->right_path
->nodes
[0], right_ii
);
4177 sctx
->cur_inode_mode
= btrfs_inode_mode(
4178 sctx
->right_path
->nodes
[0], right_ii
);
4179 ret
= process_all_refs(sctx
,
4180 BTRFS_COMPARE_TREE_DELETED
);
4185 * Now process the inode as if it was new.
4187 sctx
->cur_inode_gen
= left_gen
;
4188 sctx
->cur_inode_new
= 1;
4189 sctx
->cur_inode_deleted
= 0;
4190 sctx
->cur_inode_size
= btrfs_inode_size(
4191 sctx
->left_path
->nodes
[0], left_ii
);
4192 sctx
->cur_inode_mode
= btrfs_inode_mode(
4193 sctx
->left_path
->nodes
[0], left_ii
);
4194 ret
= send_create_inode_if_needed(sctx
);
4198 ret
= process_all_refs(sctx
, BTRFS_COMPARE_TREE_NEW
);
4202 * Advance send_progress now as we did not get into
4203 * process_recorded_refs_if_needed in the new_gen case.
4205 sctx
->send_progress
= sctx
->cur_ino
+ 1;
4208 * Now process all extents and xattrs of the inode as if
4209 * they were all new.
4211 ret
= process_all_extents(sctx
);
4214 ret
= process_all_new_xattrs(sctx
);
4218 sctx
->cur_inode_gen
= left_gen
;
4219 sctx
->cur_inode_new
= 0;
4220 sctx
->cur_inode_new_gen
= 0;
4221 sctx
->cur_inode_deleted
= 0;
4222 sctx
->cur_inode_size
= btrfs_inode_size(
4223 sctx
->left_path
->nodes
[0], left_ii
);
4224 sctx
->cur_inode_mode
= btrfs_inode_mode(
4225 sctx
->left_path
->nodes
[0], left_ii
);
4234 * We have to process new refs before deleted refs, but compare_trees gives us
4235 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4236 * first and later process them in process_recorded_refs.
4237 * For the cur_inode_new_gen case, we skip recording completely because
4238 * changed_inode did already initiate processing of refs. The reason for this is
4239 * that in this case, compare_tree actually compares the refs of 2 different
4240 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4241 * refs of the right tree as deleted and all refs of the left tree as new.
4243 static int changed_ref(struct send_ctx
*sctx
,
4244 enum btrfs_compare_tree_result result
)
4248 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4250 if (!sctx
->cur_inode_new_gen
&&
4251 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
4252 if (result
== BTRFS_COMPARE_TREE_NEW
)
4253 ret
= record_new_ref(sctx
);
4254 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
4255 ret
= record_deleted_ref(sctx
);
4256 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
4257 ret
= record_changed_ref(sctx
);
4264 * Process new/deleted/changed xattrs. We skip processing in the
4265 * cur_inode_new_gen case because changed_inode did already initiate processing
4266 * of xattrs. The reason is the same as in changed_ref
4268 static int changed_xattr(struct send_ctx
*sctx
,
4269 enum btrfs_compare_tree_result result
)
4273 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4275 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
4276 if (result
== BTRFS_COMPARE_TREE_NEW
)
4277 ret
= process_new_xattr(sctx
);
4278 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
4279 ret
= process_deleted_xattr(sctx
);
4280 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
4281 ret
= process_changed_xattr(sctx
);
4288 * Process new/deleted/changed extents. We skip processing in the
4289 * cur_inode_new_gen case because changed_inode did already initiate processing
4290 * of extents. The reason is the same as in changed_ref
4292 static int changed_extent(struct send_ctx
*sctx
,
4293 enum btrfs_compare_tree_result result
)
4297 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4299 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
4300 if (result
!= BTRFS_COMPARE_TREE_DELETED
)
4301 ret
= process_extent(sctx
, sctx
->left_path
,
4309 * Updates compare related fields in sctx and simply forwards to the actual
4310 * changed_xxx functions.
4312 static int changed_cb(struct btrfs_root
*left_root
,
4313 struct btrfs_root
*right_root
,
4314 struct btrfs_path
*left_path
,
4315 struct btrfs_path
*right_path
,
4316 struct btrfs_key
*key
,
4317 enum btrfs_compare_tree_result result
,
4321 struct send_ctx
*sctx
= ctx
;
4323 sctx
->left_path
= left_path
;
4324 sctx
->right_path
= right_path
;
4325 sctx
->cmp_key
= key
;
4327 ret
= finish_inode_if_needed(sctx
, 0);
4331 /* Ignore non-FS objects */
4332 if (key
->objectid
== BTRFS_FREE_INO_OBJECTID
||
4333 key
->objectid
== BTRFS_FREE_SPACE_OBJECTID
)
4336 if (key
->type
== BTRFS_INODE_ITEM_KEY
)
4337 ret
= changed_inode(sctx
, result
);
4338 else if (key
->type
== BTRFS_INODE_REF_KEY
)
4339 ret
= changed_ref(sctx
, result
);
4340 else if (key
->type
== BTRFS_XATTR_ITEM_KEY
)
4341 ret
= changed_xattr(sctx
, result
);
4342 else if (key
->type
== BTRFS_EXTENT_DATA_KEY
)
4343 ret
= changed_extent(sctx
, result
);
4349 static int full_send_tree(struct send_ctx
*sctx
)
4352 struct btrfs_trans_handle
*trans
= NULL
;
4353 struct btrfs_root
*send_root
= sctx
->send_root
;
4354 struct btrfs_key key
;
4355 struct btrfs_key found_key
;
4356 struct btrfs_path
*path
;
4357 struct extent_buffer
*eb
;
4362 path
= alloc_path_for_send();
4366 spin_lock(&send_root
->root_times_lock
);
4367 start_ctransid
= btrfs_root_ctransid(&send_root
->root_item
);
4368 spin_unlock(&send_root
->root_times_lock
);
4370 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
4371 key
.type
= BTRFS_INODE_ITEM_KEY
;
4376 * We need to make sure the transaction does not get committed
4377 * while we do anything on commit roots. Join a transaction to prevent
4380 trans
= btrfs_join_transaction(send_root
);
4381 if (IS_ERR(trans
)) {
4382 ret
= PTR_ERR(trans
);
4388 * Make sure the tree has not changed after re-joining. We detect this
4389 * by comparing start_ctransid and ctransid. They should always match.
4391 spin_lock(&send_root
->root_times_lock
);
4392 ctransid
= btrfs_root_ctransid(&send_root
->root_item
);
4393 spin_unlock(&send_root
->root_times_lock
);
4395 if (ctransid
!= start_ctransid
) {
4396 WARN(1, KERN_WARNING
"btrfs: the root that you're trying to "
4397 "send was modified in between. This is "
4398 "probably a bug.\n");
4403 ret
= btrfs_search_slot_for_read(send_root
, &key
, path
, 1, 0);
4411 * When someone want to commit while we iterate, end the
4412 * joined transaction and rejoin.
4414 if (btrfs_should_end_transaction(trans
, send_root
)) {
4415 ret
= btrfs_end_transaction(trans
, send_root
);
4419 btrfs_release_path(path
);
4423 eb
= path
->nodes
[0];
4424 slot
= path
->slots
[0];
4425 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4427 ret
= changed_cb(send_root
, NULL
, path
, NULL
,
4428 &found_key
, BTRFS_COMPARE_TREE_NEW
, sctx
);
4432 key
.objectid
= found_key
.objectid
;
4433 key
.type
= found_key
.type
;
4434 key
.offset
= found_key
.offset
+ 1;
4436 ret
= btrfs_next_item(send_root
, path
);
4446 ret
= finish_inode_if_needed(sctx
, 1);
4449 btrfs_free_path(path
);
4452 ret
= btrfs_end_transaction(trans
, send_root
);
4454 btrfs_end_transaction(trans
, send_root
);
4459 static int send_subvol(struct send_ctx
*sctx
)
4463 ret
= send_header(sctx
);
4467 ret
= send_subvol_begin(sctx
);
4471 if (sctx
->parent_root
) {
4472 ret
= btrfs_compare_trees(sctx
->send_root
, sctx
->parent_root
,
4476 ret
= finish_inode_if_needed(sctx
, 1);
4480 ret
= full_send_tree(sctx
);
4487 ret
= close_cur_inode_file(sctx
);
4489 close_cur_inode_file(sctx
);
4491 free_recorded_refs(sctx
);
4495 long btrfs_ioctl_send(struct file
*mnt_file
, void __user
*arg_
)
4498 struct btrfs_root
*send_root
;
4499 struct btrfs_root
*clone_root
;
4500 struct btrfs_fs_info
*fs_info
;
4501 struct btrfs_ioctl_send_args
*arg
= NULL
;
4502 struct btrfs_key key
;
4503 struct file
*filp
= NULL
;
4504 struct send_ctx
*sctx
= NULL
;
4506 u64
*clone_sources_tmp
= NULL
;
4508 if (!capable(CAP_SYS_ADMIN
))
4511 send_root
= BTRFS_I(fdentry(mnt_file
)->d_inode
)->root
;
4512 fs_info
= send_root
->fs_info
;
4514 arg
= memdup_user(arg_
, sizeof(*arg
));
4521 if (!access_ok(VERIFY_READ
, arg
->clone_sources
,
4522 sizeof(*arg
->clone_sources
*
4523 arg
->clone_sources_count
))) {
4528 sctx
= kzalloc(sizeof(struct send_ctx
), GFP_NOFS
);
4534 INIT_LIST_HEAD(&sctx
->new_refs
);
4535 INIT_LIST_HEAD(&sctx
->deleted_refs
);
4536 INIT_RADIX_TREE(&sctx
->name_cache
, GFP_NOFS
);
4537 INIT_LIST_HEAD(&sctx
->name_cache_list
);
4539 sctx
->send_filp
= fget(arg
->send_fd
);
4540 if (IS_ERR(sctx
->send_filp
)) {
4541 ret
= PTR_ERR(sctx
->send_filp
);
4545 sctx
->mnt
= mnt_file
->f_path
.mnt
;
4547 sctx
->send_root
= send_root
;
4548 sctx
->clone_roots_cnt
= arg
->clone_sources_count
;
4550 sctx
->send_max_size
= BTRFS_SEND_BUF_SIZE
;
4551 sctx
->send_buf
= vmalloc(sctx
->send_max_size
);
4552 if (!sctx
->send_buf
) {
4557 sctx
->read_buf
= vmalloc(BTRFS_SEND_READ_SIZE
);
4558 if (!sctx
->read_buf
) {
4563 sctx
->clone_roots
= vzalloc(sizeof(struct clone_root
) *
4564 (arg
->clone_sources_count
+ 1));
4565 if (!sctx
->clone_roots
) {
4570 if (arg
->clone_sources_count
) {
4571 clone_sources_tmp
= vmalloc(arg
->clone_sources_count
*
4572 sizeof(*arg
->clone_sources
));
4573 if (!clone_sources_tmp
) {
4578 ret
= copy_from_user(clone_sources_tmp
, arg
->clone_sources
,
4579 arg
->clone_sources_count
*
4580 sizeof(*arg
->clone_sources
));
4586 for (i
= 0; i
< arg
->clone_sources_count
; i
++) {
4587 key
.objectid
= clone_sources_tmp
[i
];
4588 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4589 key
.offset
= (u64
)-1;
4590 clone_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
4595 if (IS_ERR(clone_root
)) {
4596 ret
= PTR_ERR(clone_root
);
4599 sctx
->clone_roots
[i
].root
= clone_root
;
4601 vfree(clone_sources_tmp
);
4602 clone_sources_tmp
= NULL
;
4605 if (arg
->parent_root
) {
4606 key
.objectid
= arg
->parent_root
;
4607 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4608 key
.offset
= (u64
)-1;
4609 sctx
->parent_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
4610 if (!sctx
->parent_root
) {
4617 * Clones from send_root are allowed, but only if the clone source
4618 * is behind the current send position. This is checked while searching
4619 * for possible clone sources.
4621 sctx
->clone_roots
[sctx
->clone_roots_cnt
++].root
= sctx
->send_root
;
4623 /* We do a bsearch later */
4624 sort(sctx
->clone_roots
, sctx
->clone_roots_cnt
,
4625 sizeof(*sctx
->clone_roots
), __clone_root_cmp_sort
,
4628 ret
= send_subvol(sctx
);
4632 ret
= begin_cmd(sctx
, BTRFS_SEND_C_END
);
4635 ret
= send_cmd(sctx
);
4643 vfree(clone_sources_tmp
);
4646 if (sctx
->send_filp
)
4647 fput(sctx
->send_filp
);
4649 vfree(sctx
->clone_roots
);
4650 vfree(sctx
->send_buf
);
4651 vfree(sctx
->read_buf
);
4653 name_cache_free(sctx
);