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];
88 u64 flags
; /* 'flags' member of btrfs_ioctl_send_args is u64 */
92 struct btrfs_root
*send_root
;
93 struct btrfs_root
*parent_root
;
94 struct clone_root
*clone_roots
;
97 /* current state of the compare_tree call */
98 struct btrfs_path
*left_path
;
99 struct btrfs_path
*right_path
;
100 struct btrfs_key
*cmp_key
;
103 * infos of the currently processed inode. In case of deleted inodes,
104 * these are the values from the deleted inode.
109 int cur_inode_new_gen
;
110 int cur_inode_deleted
;
116 struct list_head new_refs
;
117 struct list_head deleted_refs
;
119 struct radix_tree_root name_cache
;
120 struct list_head name_cache_list
;
123 struct file
*cur_inode_filp
;
127 struct name_cache_entry
{
128 struct list_head list
;
130 * radix_tree has only 32bit entries but we need to handle 64bit inums.
131 * We use the lower 32bit of the 64bit inum to store it in the tree. If
132 * more then one inum would fall into the same entry, we use radix_list
133 * to store the additional entries. radix_list is also used to store
134 * entries where two entries have the same inum but different
137 struct list_head radix_list
;
143 int need_later_update
;
148 static void fs_path_reset(struct fs_path
*p
)
151 p
->start
= p
->buf
+ p
->buf_len
- 1;
161 static struct fs_path
*fs_path_alloc(struct send_ctx
*sctx
)
165 p
= kmalloc(sizeof(*p
), GFP_NOFS
);
170 p
->buf
= p
->inline_buf
;
171 p
->buf_len
= FS_PATH_INLINE_SIZE
;
176 static struct fs_path
*fs_path_alloc_reversed(struct send_ctx
*sctx
)
180 p
= fs_path_alloc(sctx
);
188 static void fs_path_free(struct send_ctx
*sctx
, struct fs_path
*p
)
192 if (p
->buf
!= p
->inline_buf
) {
201 static int fs_path_len(struct fs_path
*p
)
203 return p
->end
- p
->start
;
206 static int fs_path_ensure_buf(struct fs_path
*p
, int len
)
214 if (p
->buf_len
>= len
)
217 path_len
= p
->end
- p
->start
;
218 old_buf_len
= p
->buf_len
;
219 len
= PAGE_ALIGN(len
);
221 if (p
->buf
== p
->inline_buf
) {
222 tmp_buf
= kmalloc(len
, GFP_NOFS
);
224 tmp_buf
= vmalloc(len
);
229 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
233 if (p
->virtual_mem
) {
234 tmp_buf
= vmalloc(len
);
237 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
240 tmp_buf
= krealloc(p
->buf
, len
, GFP_NOFS
);
242 tmp_buf
= vmalloc(len
);
245 memcpy(tmp_buf
, p
->buf
, p
->buf_len
);
254 tmp_buf
= p
->buf
+ old_buf_len
- path_len
- 1;
255 p
->end
= p
->buf
+ p
->buf_len
- 1;
256 p
->start
= p
->end
- path_len
;
257 memmove(p
->start
, tmp_buf
, path_len
+ 1);
260 p
->end
= p
->start
+ path_len
;
265 static int fs_path_prepare_for_add(struct fs_path
*p
, int name_len
)
270 new_len
= p
->end
- p
->start
+ name_len
;
271 if (p
->start
!= p
->end
)
273 ret
= fs_path_ensure_buf(p
, new_len
);
278 if (p
->start
!= p
->end
)
280 p
->start
-= name_len
;
281 p
->prepared
= p
->start
;
283 if (p
->start
!= p
->end
)
285 p
->prepared
= p
->end
;
294 static int fs_path_add(struct fs_path
*p
, const char *name
, int name_len
)
298 ret
= fs_path_prepare_for_add(p
, name_len
);
301 memcpy(p
->prepared
, name
, name_len
);
308 static int fs_path_add_path(struct fs_path
*p
, struct fs_path
*p2
)
312 ret
= fs_path_prepare_for_add(p
, p2
->end
- p2
->start
);
315 memcpy(p
->prepared
, p2
->start
, p2
->end
- p2
->start
);
322 static int fs_path_add_from_extent_buffer(struct fs_path
*p
,
323 struct extent_buffer
*eb
,
324 unsigned long off
, int len
)
328 ret
= fs_path_prepare_for_add(p
, len
);
332 read_extent_buffer(eb
, p
->prepared
, off
, len
);
340 static void fs_path_remove(struct fs_path
*p
)
343 while (p
->start
!= p
->end
&& *p
->end
!= '/')
349 static int fs_path_copy(struct fs_path
*p
, struct fs_path
*from
)
353 p
->reversed
= from
->reversed
;
356 ret
= fs_path_add_path(p
, from
);
362 static void fs_path_unreverse(struct fs_path
*p
)
371 len
= p
->end
- p
->start
;
373 p
->end
= p
->start
+ len
;
374 memmove(p
->start
, tmp
, len
+ 1);
378 static struct btrfs_path
*alloc_path_for_send(void)
380 struct btrfs_path
*path
;
382 path
= btrfs_alloc_path();
385 path
->search_commit_root
= 1;
386 path
->skip_locking
= 1;
390 static int write_buf(struct file
*filp
, const void *buf
, u32 len
, loff_t
*off
)
400 ret
= vfs_write(filp
, (char *)buf
+ pos
, len
- pos
, off
);
401 /* TODO handle that correctly */
402 /*if (ret == -ERESTARTSYS) {
421 static int tlv_put(struct send_ctx
*sctx
, u16 attr
, const void *data
, int len
)
423 struct btrfs_tlv_header
*hdr
;
424 int total_len
= sizeof(*hdr
) + len
;
425 int left
= sctx
->send_max_size
- sctx
->send_size
;
427 if (unlikely(left
< total_len
))
430 hdr
= (struct btrfs_tlv_header
*) (sctx
->send_buf
+ sctx
->send_size
);
431 hdr
->tlv_type
= cpu_to_le16(attr
);
432 hdr
->tlv_len
= cpu_to_le16(len
);
433 memcpy(hdr
+ 1, data
, len
);
434 sctx
->send_size
+= total_len
;
440 static int tlv_put_u8(struct send_ctx
*sctx
, u16 attr
, u8 value
)
442 return tlv_put(sctx
, attr
, &value
, sizeof(value
));
445 static int tlv_put_u16(struct send_ctx
*sctx
, u16 attr
, u16 value
)
447 __le16 tmp
= cpu_to_le16(value
);
448 return tlv_put(sctx
, attr
, &tmp
, sizeof(tmp
));
451 static int tlv_put_u32(struct send_ctx
*sctx
, u16 attr
, u32 value
)
453 __le32 tmp
= cpu_to_le32(value
);
454 return tlv_put(sctx
, attr
, &tmp
, sizeof(tmp
));
458 static int tlv_put_u64(struct send_ctx
*sctx
, u16 attr
, u64 value
)
460 __le64 tmp
= cpu_to_le64(value
);
461 return tlv_put(sctx
, attr
, &tmp
, sizeof(tmp
));
464 static int tlv_put_string(struct send_ctx
*sctx
, u16 attr
,
465 const char *str
, int len
)
469 return tlv_put(sctx
, attr
, str
, len
);
472 static int tlv_put_uuid(struct send_ctx
*sctx
, u16 attr
,
475 return tlv_put(sctx
, attr
, uuid
, BTRFS_UUID_SIZE
);
479 static int tlv_put_timespec(struct send_ctx
*sctx
, u16 attr
,
482 struct btrfs_timespec bts
;
483 bts
.sec
= cpu_to_le64(ts
->tv_sec
);
484 bts
.nsec
= cpu_to_le32(ts
->tv_nsec
);
485 return tlv_put(sctx
, attr
, &bts
, sizeof(bts
));
489 static int tlv_put_btrfs_timespec(struct send_ctx
*sctx
, u16 attr
,
490 struct extent_buffer
*eb
,
491 struct btrfs_timespec
*ts
)
493 struct btrfs_timespec bts
;
494 read_extent_buffer(eb
, &bts
, (unsigned long)ts
, sizeof(bts
));
495 return tlv_put(sctx
, attr
, &bts
, sizeof(bts
));
499 #define TLV_PUT(sctx, attrtype, attrlen, data) \
501 ret = tlv_put(sctx, attrtype, attrlen, data); \
503 goto tlv_put_failure; \
506 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
508 ret = tlv_put_u##bits(sctx, attrtype, value); \
510 goto tlv_put_failure; \
513 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
514 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
515 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
516 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
517 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
519 ret = tlv_put_string(sctx, attrtype, str, len); \
521 goto tlv_put_failure; \
523 #define TLV_PUT_PATH(sctx, attrtype, p) \
525 ret = tlv_put_string(sctx, attrtype, p->start, \
526 p->end - p->start); \
528 goto tlv_put_failure; \
530 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
532 ret = tlv_put_uuid(sctx, attrtype, uuid); \
534 goto tlv_put_failure; \
536 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
538 ret = tlv_put_timespec(sctx, attrtype, ts); \
540 goto tlv_put_failure; \
542 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
544 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
546 goto tlv_put_failure; \
549 static int send_header(struct send_ctx
*sctx
)
551 struct btrfs_stream_header hdr
;
553 strcpy(hdr
.magic
, BTRFS_SEND_STREAM_MAGIC
);
554 hdr
.version
= cpu_to_le32(BTRFS_SEND_STREAM_VERSION
);
556 return write_buf(sctx
->send_filp
, &hdr
, sizeof(hdr
),
561 * For each command/item we want to send to userspace, we call this function.
563 static int begin_cmd(struct send_ctx
*sctx
, int cmd
)
565 struct btrfs_cmd_header
*hdr
;
567 if (!sctx
->send_buf
) {
572 BUG_ON(sctx
->send_size
);
574 sctx
->send_size
+= sizeof(*hdr
);
575 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
576 hdr
->cmd
= cpu_to_le16(cmd
);
581 static int send_cmd(struct send_ctx
*sctx
)
584 struct btrfs_cmd_header
*hdr
;
587 hdr
= (struct btrfs_cmd_header
*)sctx
->send_buf
;
588 hdr
->len
= cpu_to_le32(sctx
->send_size
- sizeof(*hdr
));
591 crc
= crc32c(0, (unsigned char *)sctx
->send_buf
, sctx
->send_size
);
592 hdr
->crc
= cpu_to_le32(crc
);
594 ret
= write_buf(sctx
->send_filp
, sctx
->send_buf
, sctx
->send_size
,
597 sctx
->total_send_size
+= sctx
->send_size
;
598 sctx
->cmd_send_size
[le16_to_cpu(hdr
->cmd
)] += sctx
->send_size
;
605 * Sends a move instruction to user space
607 static int send_rename(struct send_ctx
*sctx
,
608 struct fs_path
*from
, struct fs_path
*to
)
612 verbose_printk("btrfs: send_rename %s -> %s\n", from
->start
, to
->start
);
614 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RENAME
);
618 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, from
);
619 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_TO
, to
);
621 ret
= send_cmd(sctx
);
629 * Sends a link instruction to user space
631 static int send_link(struct send_ctx
*sctx
,
632 struct fs_path
*path
, struct fs_path
*lnk
)
636 verbose_printk("btrfs: send_link %s -> %s\n", path
->start
, lnk
->start
);
638 ret
= begin_cmd(sctx
, BTRFS_SEND_C_LINK
);
642 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
643 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, lnk
);
645 ret
= send_cmd(sctx
);
653 * Sends an unlink instruction to user space
655 static int send_unlink(struct send_ctx
*sctx
, struct fs_path
*path
)
659 verbose_printk("btrfs: send_unlink %s\n", path
->start
);
661 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UNLINK
);
665 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
667 ret
= send_cmd(sctx
);
675 * Sends a rmdir instruction to user space
677 static int send_rmdir(struct send_ctx
*sctx
, struct fs_path
*path
)
681 verbose_printk("btrfs: send_rmdir %s\n", path
->start
);
683 ret
= begin_cmd(sctx
, BTRFS_SEND_C_RMDIR
);
687 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
689 ret
= send_cmd(sctx
);
697 * Helper function to retrieve some fields from an inode item.
699 static int get_inode_info(struct btrfs_root
*root
,
700 u64 ino
, u64
*size
, u64
*gen
,
701 u64
*mode
, u64
*uid
, u64
*gid
,
705 struct btrfs_inode_item
*ii
;
706 struct btrfs_key key
;
707 struct btrfs_path
*path
;
709 path
= alloc_path_for_send();
714 key
.type
= BTRFS_INODE_ITEM_KEY
;
716 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
724 ii
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
725 struct btrfs_inode_item
);
727 *size
= btrfs_inode_size(path
->nodes
[0], ii
);
729 *gen
= btrfs_inode_generation(path
->nodes
[0], ii
);
731 *mode
= btrfs_inode_mode(path
->nodes
[0], ii
);
733 *uid
= btrfs_inode_uid(path
->nodes
[0], ii
);
735 *gid
= btrfs_inode_gid(path
->nodes
[0], ii
);
737 *rdev
= btrfs_inode_rdev(path
->nodes
[0], ii
);
740 btrfs_free_path(path
);
744 typedef int (*iterate_inode_ref_t
)(int num
, u64 dir
, int index
,
749 * Helper function to iterate the entries in ONE btrfs_inode_ref or
750 * btrfs_inode_extref.
751 * The iterate callback may return a non zero value to stop iteration. This can
752 * be a negative value for error codes or 1 to simply stop it.
754 * path must point to the INODE_REF or INODE_EXTREF when called.
756 static int iterate_inode_ref(struct send_ctx
*sctx
,
757 struct btrfs_root
*root
, struct btrfs_path
*path
,
758 struct btrfs_key
*found_key
, int resolve
,
759 iterate_inode_ref_t iterate
, void *ctx
)
761 struct extent_buffer
*eb
= path
->nodes
[0];
762 struct btrfs_item
*item
;
763 struct btrfs_inode_ref
*iref
;
764 struct btrfs_inode_extref
*extref
;
765 struct btrfs_path
*tmp_path
;
769 int slot
= path
->slots
[0];
776 unsigned long name_off
;
777 unsigned long elem_size
;
780 p
= fs_path_alloc_reversed(sctx
);
784 tmp_path
= alloc_path_for_send();
786 fs_path_free(sctx
, p
);
791 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
792 ptr
= (unsigned long)btrfs_item_ptr(eb
, slot
,
793 struct btrfs_inode_ref
);
794 item
= btrfs_item_nr(eb
, slot
);
795 total
= btrfs_item_size(eb
, item
);
796 elem_size
= sizeof(*iref
);
798 ptr
= btrfs_item_ptr_offset(eb
, slot
);
799 total
= btrfs_item_size_nr(eb
, slot
);
800 elem_size
= sizeof(*extref
);
803 while (cur
< total
) {
806 if (found_key
->type
== BTRFS_INODE_REF_KEY
) {
807 iref
= (struct btrfs_inode_ref
*)(ptr
+ cur
);
808 name_len
= btrfs_inode_ref_name_len(eb
, iref
);
809 name_off
= (unsigned long)(iref
+ 1);
810 index
= btrfs_inode_ref_index(eb
, iref
);
811 dir
= found_key
->offset
;
813 extref
= (struct btrfs_inode_extref
*)(ptr
+ cur
);
814 name_len
= btrfs_inode_extref_name_len(eb
, extref
);
815 name_off
= (unsigned long)&extref
->name
;
816 index
= btrfs_inode_extref_index(eb
, extref
);
817 dir
= btrfs_inode_extref_parent(eb
, extref
);
821 start
= btrfs_ref_to_path(root
, tmp_path
, name_len
,
825 ret
= PTR_ERR(start
);
828 if (start
< p
->buf
) {
829 /* overflow , try again with larger buffer */
830 ret
= fs_path_ensure_buf(p
,
831 p
->buf_len
+ p
->buf
- start
);
834 start
= btrfs_ref_to_path(root
, tmp_path
,
839 ret
= PTR_ERR(start
);
842 BUG_ON(start
< p
->buf
);
846 ret
= fs_path_add_from_extent_buffer(p
, eb
, name_off
,
852 cur
+= elem_size
+ name_len
;
853 ret
= iterate(num
, dir
, index
, p
, ctx
);
860 btrfs_free_path(tmp_path
);
861 fs_path_free(sctx
, p
);
865 typedef int (*iterate_dir_item_t
)(int num
, struct btrfs_key
*di_key
,
866 const char *name
, int name_len
,
867 const char *data
, int data_len
,
871 * Helper function to iterate the entries in ONE btrfs_dir_item.
872 * The iterate callback may return a non zero value to stop iteration. This can
873 * be a negative value for error codes or 1 to simply stop it.
875 * path must point to the dir item when called.
877 static int iterate_dir_item(struct send_ctx
*sctx
,
878 struct btrfs_root
*root
, struct btrfs_path
*path
,
879 struct btrfs_key
*found_key
,
880 iterate_dir_item_t iterate
, void *ctx
)
883 struct extent_buffer
*eb
;
884 struct btrfs_item
*item
;
885 struct btrfs_dir_item
*di
;
886 struct btrfs_key di_key
;
901 buf
= kmalloc(buf_len
, GFP_NOFS
);
908 slot
= path
->slots
[0];
909 item
= btrfs_item_nr(eb
, slot
);
910 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
913 total
= btrfs_item_size(eb
, item
);
916 while (cur
< total
) {
917 name_len
= btrfs_dir_name_len(eb
, di
);
918 data_len
= btrfs_dir_data_len(eb
, di
);
919 type
= btrfs_dir_type(eb
, di
);
920 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
922 if (name_len
+ data_len
> buf_len
) {
923 buf_len
= PAGE_ALIGN(name_len
+ data_len
);
925 buf2
= vmalloc(buf_len
);
932 buf2
= krealloc(buf
, buf_len
, GFP_NOFS
);
934 buf2
= vmalloc(buf_len
);
948 read_extent_buffer(eb
, buf
, (unsigned long)(di
+ 1),
949 name_len
+ data_len
);
951 len
= sizeof(*di
) + name_len
+ data_len
;
952 di
= (struct btrfs_dir_item
*)((char *)di
+ len
);
955 ret
= iterate(num
, &di_key
, buf
, name_len
, buf
+ name_len
,
956 data_len
, type
, ctx
);
975 static int __copy_first_ref(int num
, u64 dir
, int index
,
976 struct fs_path
*p
, void *ctx
)
979 struct fs_path
*pt
= ctx
;
981 ret
= fs_path_copy(pt
, p
);
985 /* we want the first only */
990 * Retrieve the first path of an inode. If an inode has more then one
991 * ref/hardlink, this is ignored.
993 static int get_inode_path(struct send_ctx
*sctx
, struct btrfs_root
*root
,
994 u64 ino
, struct fs_path
*path
)
997 struct btrfs_key key
, found_key
;
998 struct btrfs_path
*p
;
1000 p
= alloc_path_for_send();
1004 fs_path_reset(path
);
1007 key
.type
= BTRFS_INODE_REF_KEY
;
1010 ret
= btrfs_search_slot_for_read(root
, &key
, p
, 1, 0);
1017 btrfs_item_key_to_cpu(p
->nodes
[0], &found_key
, p
->slots
[0]);
1018 if (found_key
.objectid
!= ino
||
1019 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1020 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1025 ret
= iterate_inode_ref(sctx
, root
, p
, &found_key
, 1,
1026 __copy_first_ref
, path
);
1036 struct backref_ctx
{
1037 struct send_ctx
*sctx
;
1039 /* number of total found references */
1043 * used for clones found in send_root. clones found behind cur_objectid
1044 * and cur_offset are not considered as allowed clones.
1049 /* may be truncated in case it's the last extent in a file */
1052 /* Just to check for bugs in backref resolving */
1056 static int __clone_root_cmp_bsearch(const void *key
, const void *elt
)
1058 u64 root
= (u64
)(uintptr_t)key
;
1059 struct clone_root
*cr
= (struct clone_root
*)elt
;
1061 if (root
< cr
->root
->objectid
)
1063 if (root
> cr
->root
->objectid
)
1068 static int __clone_root_cmp_sort(const void *e1
, const void *e2
)
1070 struct clone_root
*cr1
= (struct clone_root
*)e1
;
1071 struct clone_root
*cr2
= (struct clone_root
*)e2
;
1073 if (cr1
->root
->objectid
< cr2
->root
->objectid
)
1075 if (cr1
->root
->objectid
> cr2
->root
->objectid
)
1081 * Called for every backref that is found for the current extent.
1082 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1084 static int __iterate_backrefs(u64 ino
, u64 offset
, u64 root
, void *ctx_
)
1086 struct backref_ctx
*bctx
= ctx_
;
1087 struct clone_root
*found
;
1091 /* First check if the root is in the list of accepted clone sources */
1092 found
= bsearch((void *)(uintptr_t)root
, bctx
->sctx
->clone_roots
,
1093 bctx
->sctx
->clone_roots_cnt
,
1094 sizeof(struct clone_root
),
1095 __clone_root_cmp_bsearch
);
1099 if (found
->root
== bctx
->sctx
->send_root
&&
1100 ino
== bctx
->cur_objectid
&&
1101 offset
== bctx
->cur_offset
) {
1102 bctx
->found_itself
= 1;
1106 * There are inodes that have extents that lie behind its i_size. Don't
1107 * accept clones from these extents.
1109 ret
= get_inode_info(found
->root
, ino
, &i_size
, NULL
, NULL
, NULL
, NULL
,
1114 if (offset
+ bctx
->extent_len
> i_size
)
1118 * Make sure we don't consider clones from send_root that are
1119 * behind the current inode/offset.
1121 if (found
->root
== bctx
->sctx
->send_root
) {
1123 * TODO for the moment we don't accept clones from the inode
1124 * that is currently send. We may change this when
1125 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1128 if (ino
>= bctx
->cur_objectid
)
1131 if (ino
> bctx
->cur_objectid
)
1133 if (offset
+ bctx
->extent_len
> bctx
->cur_offset
)
1139 found
->found_refs
++;
1140 if (ino
< found
->ino
) {
1142 found
->offset
= offset
;
1143 } else if (found
->ino
== ino
) {
1145 * same extent found more then once in the same file.
1147 if (found
->offset
> offset
+ bctx
->extent_len
)
1148 found
->offset
= offset
;
1155 * Given an inode, offset and extent item, it finds a good clone for a clone
1156 * instruction. Returns -ENOENT when none could be found. The function makes
1157 * sure that the returned clone is usable at the point where sending is at the
1158 * moment. This means, that no clones are accepted which lie behind the current
1161 * path must point to the extent item when called.
1163 static int find_extent_clone(struct send_ctx
*sctx
,
1164 struct btrfs_path
*path
,
1165 u64 ino
, u64 data_offset
,
1167 struct clone_root
**found
)
1174 u64 extent_item_pos
;
1176 struct btrfs_file_extent_item
*fi
;
1177 struct extent_buffer
*eb
= path
->nodes
[0];
1178 struct backref_ctx
*backref_ctx
= NULL
;
1179 struct clone_root
*cur_clone_root
;
1180 struct btrfs_key found_key
;
1181 struct btrfs_path
*tmp_path
;
1185 tmp_path
= alloc_path_for_send();
1189 backref_ctx
= kmalloc(sizeof(*backref_ctx
), GFP_NOFS
);
1195 if (data_offset
>= ino_size
) {
1197 * There may be extents that lie behind the file's size.
1198 * I at least had this in combination with snapshotting while
1199 * writing large files.
1205 fi
= btrfs_item_ptr(eb
, path
->slots
[0],
1206 struct btrfs_file_extent_item
);
1207 extent_type
= btrfs_file_extent_type(eb
, fi
);
1208 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1212 compressed
= btrfs_file_extent_compression(eb
, fi
);
1214 num_bytes
= btrfs_file_extent_num_bytes(eb
, fi
);
1215 disk_byte
= btrfs_file_extent_disk_bytenr(eb
, fi
);
1216 if (disk_byte
== 0) {
1220 logical
= disk_byte
+ btrfs_file_extent_offset(eb
, fi
);
1222 ret
= extent_from_logical(sctx
->send_root
->fs_info
, disk_byte
, tmp_path
,
1223 &found_key
, &flags
);
1224 btrfs_release_path(tmp_path
);
1228 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
) {
1234 * Setup the clone roots.
1236 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1237 cur_clone_root
= sctx
->clone_roots
+ i
;
1238 cur_clone_root
->ino
= (u64
)-1;
1239 cur_clone_root
->offset
= 0;
1240 cur_clone_root
->found_refs
= 0;
1243 backref_ctx
->sctx
= sctx
;
1244 backref_ctx
->found
= 0;
1245 backref_ctx
->cur_objectid
= ino
;
1246 backref_ctx
->cur_offset
= data_offset
;
1247 backref_ctx
->found_itself
= 0;
1248 backref_ctx
->extent_len
= num_bytes
;
1251 * The last extent of a file may be too large due to page alignment.
1252 * We need to adjust extent_len in this case so that the checks in
1253 * __iterate_backrefs work.
1255 if (data_offset
+ num_bytes
>= ino_size
)
1256 backref_ctx
->extent_len
= ino_size
- data_offset
;
1259 * Now collect all backrefs.
1261 if (compressed
== BTRFS_COMPRESS_NONE
)
1262 extent_item_pos
= logical
- found_key
.objectid
;
1264 extent_item_pos
= 0;
1266 extent_item_pos
= logical
- found_key
.objectid
;
1267 ret
= iterate_extent_inodes(sctx
->send_root
->fs_info
,
1268 found_key
.objectid
, extent_item_pos
, 1,
1269 __iterate_backrefs
, backref_ctx
);
1274 if (!backref_ctx
->found_itself
) {
1275 /* found a bug in backref code? */
1277 printk(KERN_ERR
"btrfs: ERROR did not find backref in "
1278 "send_root. inode=%llu, offset=%llu, "
1279 "disk_byte=%llu found extent=%llu\n",
1280 ino
, data_offset
, disk_byte
, found_key
.objectid
);
1284 verbose_printk(KERN_DEBUG
"btrfs: find_extent_clone: data_offset=%llu, "
1286 "num_bytes=%llu, logical=%llu\n",
1287 data_offset
, ino
, num_bytes
, logical
);
1289 if (!backref_ctx
->found
)
1290 verbose_printk("btrfs: no clones found\n");
1292 cur_clone_root
= NULL
;
1293 for (i
= 0; i
< sctx
->clone_roots_cnt
; i
++) {
1294 if (sctx
->clone_roots
[i
].found_refs
) {
1295 if (!cur_clone_root
)
1296 cur_clone_root
= sctx
->clone_roots
+ i
;
1297 else if (sctx
->clone_roots
[i
].root
== sctx
->send_root
)
1298 /* prefer clones from send_root over others */
1299 cur_clone_root
= sctx
->clone_roots
+ i
;
1304 if (cur_clone_root
) {
1305 *found
= cur_clone_root
;
1312 btrfs_free_path(tmp_path
);
1317 static int read_symlink(struct send_ctx
*sctx
,
1318 struct btrfs_root
*root
,
1320 struct fs_path
*dest
)
1323 struct btrfs_path
*path
;
1324 struct btrfs_key key
;
1325 struct btrfs_file_extent_item
*ei
;
1331 path
= alloc_path_for_send();
1336 key
.type
= BTRFS_EXTENT_DATA_KEY
;
1338 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1343 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1344 struct btrfs_file_extent_item
);
1345 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
1346 compression
= btrfs_file_extent_compression(path
->nodes
[0], ei
);
1347 BUG_ON(type
!= BTRFS_FILE_EXTENT_INLINE
);
1348 BUG_ON(compression
);
1350 off
= btrfs_file_extent_inline_start(ei
);
1351 len
= btrfs_file_extent_inline_len(path
->nodes
[0], ei
);
1353 ret
= fs_path_add_from_extent_buffer(dest
, path
->nodes
[0], off
, len
);
1356 btrfs_free_path(path
);
1361 * Helper function to generate a file name that is unique in the root of
1362 * send_root and parent_root. This is used to generate names for orphan inodes.
1364 static int gen_unique_name(struct send_ctx
*sctx
,
1366 struct fs_path
*dest
)
1369 struct btrfs_path
*path
;
1370 struct btrfs_dir_item
*di
;
1375 path
= alloc_path_for_send();
1380 len
= snprintf(tmp
, sizeof(tmp
) - 1, "o%llu-%llu-%llu",
1382 if (len
>= sizeof(tmp
)) {
1383 /* should really not happen */
1388 di
= btrfs_lookup_dir_item(NULL
, sctx
->send_root
,
1389 path
, BTRFS_FIRST_FREE_OBJECTID
,
1390 tmp
, strlen(tmp
), 0);
1391 btrfs_release_path(path
);
1397 /* not unique, try again */
1402 if (!sctx
->parent_root
) {
1408 di
= btrfs_lookup_dir_item(NULL
, sctx
->parent_root
,
1409 path
, BTRFS_FIRST_FREE_OBJECTID
,
1410 tmp
, strlen(tmp
), 0);
1411 btrfs_release_path(path
);
1417 /* not unique, try again */
1425 ret
= fs_path_add(dest
, tmp
, strlen(tmp
));
1428 btrfs_free_path(path
);
1433 inode_state_no_change
,
1434 inode_state_will_create
,
1435 inode_state_did_create
,
1436 inode_state_will_delete
,
1437 inode_state_did_delete
,
1440 static int get_cur_inode_state(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1448 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &left_gen
, NULL
, NULL
,
1450 if (ret
< 0 && ret
!= -ENOENT
)
1454 if (!sctx
->parent_root
) {
1455 right_ret
= -ENOENT
;
1457 ret
= get_inode_info(sctx
->parent_root
, ino
, NULL
, &right_gen
,
1458 NULL
, NULL
, NULL
, NULL
);
1459 if (ret
< 0 && ret
!= -ENOENT
)
1464 if (!left_ret
&& !right_ret
) {
1465 if (left_gen
== gen
&& right_gen
== gen
) {
1466 ret
= inode_state_no_change
;
1467 } else if (left_gen
== gen
) {
1468 if (ino
< sctx
->send_progress
)
1469 ret
= inode_state_did_create
;
1471 ret
= inode_state_will_create
;
1472 } else if (right_gen
== gen
) {
1473 if (ino
< sctx
->send_progress
)
1474 ret
= inode_state_did_delete
;
1476 ret
= inode_state_will_delete
;
1480 } else if (!left_ret
) {
1481 if (left_gen
== gen
) {
1482 if (ino
< sctx
->send_progress
)
1483 ret
= inode_state_did_create
;
1485 ret
= inode_state_will_create
;
1489 } else if (!right_ret
) {
1490 if (right_gen
== gen
) {
1491 if (ino
< sctx
->send_progress
)
1492 ret
= inode_state_did_delete
;
1494 ret
= inode_state_will_delete
;
1506 static int is_inode_existent(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1510 ret
= get_cur_inode_state(sctx
, ino
, gen
);
1514 if (ret
== inode_state_no_change
||
1515 ret
== inode_state_did_create
||
1516 ret
== inode_state_will_delete
)
1526 * Helper function to lookup a dir item in a dir.
1528 static int lookup_dir_item_inode(struct btrfs_root
*root
,
1529 u64 dir
, const char *name
, int name_len
,
1534 struct btrfs_dir_item
*di
;
1535 struct btrfs_key key
;
1536 struct btrfs_path
*path
;
1538 path
= alloc_path_for_send();
1542 di
= btrfs_lookup_dir_item(NULL
, root
, path
,
1543 dir
, name
, name_len
, 0);
1552 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
1553 *found_inode
= key
.objectid
;
1554 *found_type
= btrfs_dir_type(path
->nodes
[0], di
);
1557 btrfs_free_path(path
);
1562 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1563 * generation of the parent dir and the name of the dir entry.
1565 static int get_first_ref(struct send_ctx
*sctx
,
1566 struct btrfs_root
*root
, u64 ino
,
1567 u64
*dir
, u64
*dir_gen
, struct fs_path
*name
)
1570 struct btrfs_key key
;
1571 struct btrfs_key found_key
;
1572 struct btrfs_path
*path
;
1576 path
= alloc_path_for_send();
1581 key
.type
= BTRFS_INODE_REF_KEY
;
1584 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
1588 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1590 if (ret
|| found_key
.objectid
!= ino
||
1591 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
1592 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
)) {
1597 if (key
.type
== BTRFS_INODE_REF_KEY
) {
1598 struct btrfs_inode_ref
*iref
;
1599 iref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1600 struct btrfs_inode_ref
);
1601 len
= btrfs_inode_ref_name_len(path
->nodes
[0], iref
);
1602 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1603 (unsigned long)(iref
+ 1),
1605 parent_dir
= found_key
.offset
;
1607 struct btrfs_inode_extref
*extref
;
1608 extref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1609 struct btrfs_inode_extref
);
1610 len
= btrfs_inode_extref_name_len(path
->nodes
[0], extref
);
1611 ret
= fs_path_add_from_extent_buffer(name
, path
->nodes
[0],
1612 (unsigned long)&extref
->name
, len
);
1613 parent_dir
= btrfs_inode_extref_parent(path
->nodes
[0], extref
);
1617 btrfs_release_path(path
);
1619 ret
= get_inode_info(root
, parent_dir
, NULL
, dir_gen
, NULL
, NULL
,
1627 btrfs_free_path(path
);
1631 static int is_first_ref(struct send_ctx
*sctx
,
1632 struct btrfs_root
*root
,
1634 const char *name
, int name_len
)
1637 struct fs_path
*tmp_name
;
1641 tmp_name
= fs_path_alloc(sctx
);
1645 ret
= get_first_ref(sctx
, root
, ino
, &tmp_dir
, &tmp_dir_gen
, tmp_name
);
1649 if (dir
!= tmp_dir
|| name_len
!= fs_path_len(tmp_name
)) {
1654 ret
= !memcmp(tmp_name
->start
, name
, name_len
);
1657 fs_path_free(sctx
, tmp_name
);
1662 * Used by process_recorded_refs to determine if a new ref would overwrite an
1663 * already existing ref. In case it detects an overwrite, it returns the
1664 * inode/gen in who_ino/who_gen.
1665 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1666 * to make sure later references to the overwritten inode are possible.
1667 * Orphanizing is however only required for the first ref of an inode.
1668 * process_recorded_refs does an additional is_first_ref check to see if
1669 * orphanizing is really required.
1671 static int will_overwrite_ref(struct send_ctx
*sctx
, u64 dir
, u64 dir_gen
,
1672 const char *name
, int name_len
,
1673 u64
*who_ino
, u64
*who_gen
)
1676 u64 other_inode
= 0;
1679 if (!sctx
->parent_root
)
1682 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1686 ret
= lookup_dir_item_inode(sctx
->parent_root
, dir
, name
, name_len
,
1687 &other_inode
, &other_type
);
1688 if (ret
< 0 && ret
!= -ENOENT
)
1696 * Check if the overwritten ref was already processed. If yes, the ref
1697 * was already unlinked/moved, so we can safely assume that we will not
1698 * overwrite anything at this point in time.
1700 if (other_inode
> sctx
->send_progress
) {
1701 ret
= get_inode_info(sctx
->parent_root
, other_inode
, NULL
,
1702 who_gen
, NULL
, NULL
, NULL
, NULL
);
1707 *who_ino
= other_inode
;
1717 * Checks if the ref was overwritten by an already processed inode. This is
1718 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1719 * thus the orphan name needs be used.
1720 * process_recorded_refs also uses it to avoid unlinking of refs that were
1723 static int did_overwrite_ref(struct send_ctx
*sctx
,
1724 u64 dir
, u64 dir_gen
,
1725 u64 ino
, u64 ino_gen
,
1726 const char *name
, int name_len
)
1733 if (!sctx
->parent_root
)
1736 ret
= is_inode_existent(sctx
, dir
, dir_gen
);
1740 /* check if the ref was overwritten by another ref */
1741 ret
= lookup_dir_item_inode(sctx
->send_root
, dir
, name
, name_len
,
1742 &ow_inode
, &other_type
);
1743 if (ret
< 0 && ret
!= -ENOENT
)
1746 /* was never and will never be overwritten */
1751 ret
= get_inode_info(sctx
->send_root
, ow_inode
, NULL
, &gen
, NULL
, NULL
,
1756 if (ow_inode
== ino
&& gen
== ino_gen
) {
1761 /* we know that it is or will be overwritten. check this now */
1762 if (ow_inode
< sctx
->send_progress
)
1772 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1773 * that got overwritten. This is used by process_recorded_refs to determine
1774 * if it has to use the path as returned by get_cur_path or the orphan name.
1776 static int did_overwrite_first_ref(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
1779 struct fs_path
*name
= NULL
;
1783 if (!sctx
->parent_root
)
1786 name
= fs_path_alloc(sctx
);
1790 ret
= get_first_ref(sctx
, sctx
->parent_root
, ino
, &dir
, &dir_gen
, name
);
1794 ret
= did_overwrite_ref(sctx
, dir
, dir_gen
, ino
, gen
,
1795 name
->start
, fs_path_len(name
));
1798 fs_path_free(sctx
, name
);
1803 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1804 * so we need to do some special handling in case we have clashes. This function
1805 * takes care of this with the help of name_cache_entry::radix_list.
1806 * In case of error, nce is kfreed.
1808 static int name_cache_insert(struct send_ctx
*sctx
,
1809 struct name_cache_entry
*nce
)
1812 struct list_head
*nce_head
;
1814 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1815 (unsigned long)nce
->ino
);
1817 nce_head
= kmalloc(sizeof(*nce_head
), GFP_NOFS
);
1822 INIT_LIST_HEAD(nce_head
);
1824 ret
= radix_tree_insert(&sctx
->name_cache
, nce
->ino
, nce_head
);
1831 list_add_tail(&nce
->radix_list
, nce_head
);
1832 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
1833 sctx
->name_cache_size
++;
1838 static void name_cache_delete(struct send_ctx
*sctx
,
1839 struct name_cache_entry
*nce
)
1841 struct list_head
*nce_head
;
1843 nce_head
= radix_tree_lookup(&sctx
->name_cache
,
1844 (unsigned long)nce
->ino
);
1847 list_del(&nce
->radix_list
);
1848 list_del(&nce
->list
);
1849 sctx
->name_cache_size
--;
1851 if (list_empty(nce_head
)) {
1852 radix_tree_delete(&sctx
->name_cache
, (unsigned long)nce
->ino
);
1857 static struct name_cache_entry
*name_cache_search(struct send_ctx
*sctx
,
1860 struct list_head
*nce_head
;
1861 struct name_cache_entry
*cur
;
1863 nce_head
= radix_tree_lookup(&sctx
->name_cache
, (unsigned long)ino
);
1867 list_for_each_entry(cur
, nce_head
, radix_list
) {
1868 if (cur
->ino
== ino
&& cur
->gen
== gen
)
1875 * Removes the entry from the list and adds it back to the end. This marks the
1876 * entry as recently used so that name_cache_clean_unused does not remove it.
1878 static void name_cache_used(struct send_ctx
*sctx
, struct name_cache_entry
*nce
)
1880 list_del(&nce
->list
);
1881 list_add_tail(&nce
->list
, &sctx
->name_cache_list
);
1885 * Remove some entries from the beginning of name_cache_list.
1887 static void name_cache_clean_unused(struct send_ctx
*sctx
)
1889 struct name_cache_entry
*nce
;
1891 if (sctx
->name_cache_size
< SEND_CTX_NAME_CACHE_CLEAN_SIZE
)
1894 while (sctx
->name_cache_size
> SEND_CTX_MAX_NAME_CACHE_SIZE
) {
1895 nce
= list_entry(sctx
->name_cache_list
.next
,
1896 struct name_cache_entry
, list
);
1897 name_cache_delete(sctx
, nce
);
1902 static void name_cache_free(struct send_ctx
*sctx
)
1904 struct name_cache_entry
*nce
;
1906 while (!list_empty(&sctx
->name_cache_list
)) {
1907 nce
= list_entry(sctx
->name_cache_list
.next
,
1908 struct name_cache_entry
, list
);
1909 name_cache_delete(sctx
, nce
);
1915 * Used by get_cur_path for each ref up to the root.
1916 * Returns 0 if it succeeded.
1917 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1918 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1919 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1920 * Returns <0 in case of error.
1922 static int __get_cur_name_and_parent(struct send_ctx
*sctx
,
1926 struct fs_path
*dest
)
1930 struct btrfs_path
*path
= NULL
;
1931 struct name_cache_entry
*nce
= NULL
;
1934 * First check if we already did a call to this function with the same
1935 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1936 * return the cached result.
1938 nce
= name_cache_search(sctx
, ino
, gen
);
1940 if (ino
< sctx
->send_progress
&& nce
->need_later_update
) {
1941 name_cache_delete(sctx
, nce
);
1945 name_cache_used(sctx
, nce
);
1946 *parent_ino
= nce
->parent_ino
;
1947 *parent_gen
= nce
->parent_gen
;
1948 ret
= fs_path_add(dest
, nce
->name
, nce
->name_len
);
1956 path
= alloc_path_for_send();
1961 * If the inode is not existent yet, add the orphan name and return 1.
1962 * This should only happen for the parent dir that we determine in
1965 ret
= is_inode_existent(sctx
, ino
, gen
);
1970 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
1978 * Depending on whether the inode was already processed or not, use
1979 * send_root or parent_root for ref lookup.
1981 if (ino
< sctx
->send_progress
)
1982 ret
= get_first_ref(sctx
, sctx
->send_root
, ino
,
1983 parent_ino
, parent_gen
, dest
);
1985 ret
= get_first_ref(sctx
, sctx
->parent_root
, ino
,
1986 parent_ino
, parent_gen
, dest
);
1991 * Check if the ref was overwritten by an inode's ref that was processed
1992 * earlier. If yes, treat as orphan and return 1.
1994 ret
= did_overwrite_ref(sctx
, *parent_ino
, *parent_gen
, ino
, gen
,
1995 dest
->start
, dest
->end
- dest
->start
);
1999 fs_path_reset(dest
);
2000 ret
= gen_unique_name(sctx
, ino
, gen
, dest
);
2008 * Store the result of the lookup in the name cache.
2010 nce
= kmalloc(sizeof(*nce
) + fs_path_len(dest
) + 1, GFP_NOFS
);
2018 nce
->parent_ino
= *parent_ino
;
2019 nce
->parent_gen
= *parent_gen
;
2020 nce
->name_len
= fs_path_len(dest
);
2022 strcpy(nce
->name
, dest
->start
);
2024 if (ino
< sctx
->send_progress
)
2025 nce
->need_later_update
= 0;
2027 nce
->need_later_update
= 1;
2029 nce_ret
= name_cache_insert(sctx
, nce
);
2032 name_cache_clean_unused(sctx
);
2035 btrfs_free_path(path
);
2040 * Magic happens here. This function returns the first ref to an inode as it
2041 * would look like while receiving the stream at this point in time.
2042 * We walk the path up to the root. For every inode in between, we check if it
2043 * was already processed/sent. If yes, we continue with the parent as found
2044 * in send_root. If not, we continue with the parent as found in parent_root.
2045 * If we encounter an inode that was deleted at this point in time, we use the
2046 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2047 * that were not created yet and overwritten inodes/refs.
2049 * When do we have have orphan inodes:
2050 * 1. When an inode is freshly created and thus no valid refs are available yet
2051 * 2. When a directory lost all it's refs (deleted) but still has dir items
2052 * inside which were not processed yet (pending for move/delete). If anyone
2053 * tried to get the path to the dir items, it would get a path inside that
2055 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2056 * of an unprocessed inode. If in that case the first ref would be
2057 * overwritten, the overwritten inode gets "orphanized". Later when we
2058 * process this overwritten inode, it is restored at a new place by moving
2061 * sctx->send_progress tells this function at which point in time receiving
2064 static int get_cur_path(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2065 struct fs_path
*dest
)
2068 struct fs_path
*name
= NULL
;
2069 u64 parent_inode
= 0;
2073 name
= fs_path_alloc(sctx
);
2080 fs_path_reset(dest
);
2082 while (!stop
&& ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
2083 fs_path_reset(name
);
2085 ret
= __get_cur_name_and_parent(sctx
, ino
, gen
,
2086 &parent_inode
, &parent_gen
, name
);
2092 ret
= fs_path_add_path(dest
, name
);
2101 fs_path_free(sctx
, name
);
2103 fs_path_unreverse(dest
);
2108 * Called for regular files when sending extents data. Opens a struct file
2109 * to read from the file.
2111 static int open_cur_inode_file(struct send_ctx
*sctx
)
2114 struct btrfs_key key
;
2116 struct inode
*inode
;
2117 struct dentry
*dentry
;
2121 if (sctx
->cur_inode_filp
)
2124 key
.objectid
= sctx
->cur_ino
;
2125 key
.type
= BTRFS_INODE_ITEM_KEY
;
2128 inode
= btrfs_iget(sctx
->send_root
->fs_info
->sb
, &key
, sctx
->send_root
,
2130 if (IS_ERR(inode
)) {
2131 ret
= PTR_ERR(inode
);
2135 dentry
= d_obtain_alias(inode
);
2137 if (IS_ERR(dentry
)) {
2138 ret
= PTR_ERR(dentry
);
2142 path
.mnt
= sctx
->mnt
;
2143 path
.dentry
= dentry
;
2144 filp
= dentry_open(&path
, O_RDONLY
| O_LARGEFILE
, current_cred());
2148 ret
= PTR_ERR(filp
);
2151 sctx
->cur_inode_filp
= filp
;
2155 * no xxxput required here as every vfs op
2156 * does it by itself on failure
2162 * Closes the struct file that was created in open_cur_inode_file
2164 static int close_cur_inode_file(struct send_ctx
*sctx
)
2168 if (!sctx
->cur_inode_filp
)
2171 ret
= filp_close(sctx
->cur_inode_filp
, NULL
);
2172 sctx
->cur_inode_filp
= NULL
;
2179 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2181 static int send_subvol_begin(struct send_ctx
*sctx
)
2184 struct btrfs_root
*send_root
= sctx
->send_root
;
2185 struct btrfs_root
*parent_root
= sctx
->parent_root
;
2186 struct btrfs_path
*path
;
2187 struct btrfs_key key
;
2188 struct btrfs_root_ref
*ref
;
2189 struct extent_buffer
*leaf
;
2193 path
= alloc_path_for_send();
2197 name
= kmalloc(BTRFS_PATH_NAME_MAX
, GFP_NOFS
);
2199 btrfs_free_path(path
);
2203 key
.objectid
= send_root
->objectid
;
2204 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
2207 ret
= btrfs_search_slot_for_read(send_root
->fs_info
->tree_root
,
2216 leaf
= path
->nodes
[0];
2217 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
2218 if (key
.type
!= BTRFS_ROOT_BACKREF_KEY
||
2219 key
.objectid
!= send_root
->objectid
) {
2223 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
2224 namelen
= btrfs_root_ref_name_len(leaf
, ref
);
2225 read_extent_buffer(leaf
, name
, (unsigned long)(ref
+ 1), namelen
);
2226 btrfs_release_path(path
);
2229 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SNAPSHOT
);
2233 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SUBVOL
);
2238 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_PATH
, name
, namelen
);
2239 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_UUID
,
2240 sctx
->send_root
->root_item
.uuid
);
2241 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CTRANSID
,
2242 sctx
->send_root
->root_item
.ctransid
);
2244 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
2245 sctx
->parent_root
->root_item
.uuid
);
2246 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
2247 sctx
->parent_root
->root_item
.ctransid
);
2250 ret
= send_cmd(sctx
);
2254 btrfs_free_path(path
);
2259 static int send_truncate(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 size
)
2264 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino
, size
);
2266 p
= fs_path_alloc(sctx
);
2270 ret
= begin_cmd(sctx
, BTRFS_SEND_C_TRUNCATE
);
2274 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2277 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2278 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, size
);
2280 ret
= send_cmd(sctx
);
2284 fs_path_free(sctx
, p
);
2288 static int send_chmod(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 mode
)
2293 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino
, mode
);
2295 p
= fs_path_alloc(sctx
);
2299 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHMOD
);
2303 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2306 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2307 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
& 07777);
2309 ret
= send_cmd(sctx
);
2313 fs_path_free(sctx
, p
);
2317 static int send_chown(struct send_ctx
*sctx
, u64 ino
, u64 gen
, u64 uid
, u64 gid
)
2322 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino
, uid
, gid
);
2324 p
= fs_path_alloc(sctx
);
2328 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CHOWN
);
2332 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2335 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2336 TLV_PUT_U64(sctx
, BTRFS_SEND_A_UID
, uid
);
2337 TLV_PUT_U64(sctx
, BTRFS_SEND_A_GID
, gid
);
2339 ret
= send_cmd(sctx
);
2343 fs_path_free(sctx
, p
);
2347 static int send_utimes(struct send_ctx
*sctx
, u64 ino
, u64 gen
)
2350 struct fs_path
*p
= NULL
;
2351 struct btrfs_inode_item
*ii
;
2352 struct btrfs_path
*path
= NULL
;
2353 struct extent_buffer
*eb
;
2354 struct btrfs_key key
;
2357 verbose_printk("btrfs: send_utimes %llu\n", ino
);
2359 p
= fs_path_alloc(sctx
);
2363 path
= alloc_path_for_send();
2370 key
.type
= BTRFS_INODE_ITEM_KEY
;
2372 ret
= btrfs_search_slot(NULL
, sctx
->send_root
, &key
, path
, 0, 0);
2376 eb
= path
->nodes
[0];
2377 slot
= path
->slots
[0];
2378 ii
= btrfs_item_ptr(eb
, slot
, struct btrfs_inode_item
);
2380 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UTIMES
);
2384 ret
= get_cur_path(sctx
, ino
, gen
, p
);
2387 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2388 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_ATIME
, eb
,
2389 btrfs_inode_atime(ii
));
2390 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_MTIME
, eb
,
2391 btrfs_inode_mtime(ii
));
2392 TLV_PUT_BTRFS_TIMESPEC(sctx
, BTRFS_SEND_A_CTIME
, eb
,
2393 btrfs_inode_ctime(ii
));
2394 /* TODO Add otime support when the otime patches get into upstream */
2396 ret
= send_cmd(sctx
);
2400 fs_path_free(sctx
, p
);
2401 btrfs_free_path(path
);
2406 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2407 * a valid path yet because we did not process the refs yet. So, the inode
2408 * is created as orphan.
2410 static int send_create_inode(struct send_ctx
*sctx
, u64 ino
)
2419 verbose_printk("btrfs: send_create_inode %llu\n", ino
);
2421 p
= fs_path_alloc(sctx
);
2425 ret
= get_inode_info(sctx
->send_root
, ino
, NULL
, &gen
, &mode
, NULL
,
2430 if (S_ISREG(mode
)) {
2431 cmd
= BTRFS_SEND_C_MKFILE
;
2432 } else if (S_ISDIR(mode
)) {
2433 cmd
= BTRFS_SEND_C_MKDIR
;
2434 } else if (S_ISLNK(mode
)) {
2435 cmd
= BTRFS_SEND_C_SYMLINK
;
2436 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
2437 cmd
= BTRFS_SEND_C_MKNOD
;
2438 } else if (S_ISFIFO(mode
)) {
2439 cmd
= BTRFS_SEND_C_MKFIFO
;
2440 } else if (S_ISSOCK(mode
)) {
2441 cmd
= BTRFS_SEND_C_MKSOCK
;
2443 printk(KERN_WARNING
"btrfs: unexpected inode type %o",
2444 (int)(mode
& S_IFMT
));
2449 ret
= begin_cmd(sctx
, cmd
);
2453 ret
= gen_unique_name(sctx
, ino
, gen
, p
);
2457 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
2458 TLV_PUT_U64(sctx
, BTRFS_SEND_A_INO
, ino
);
2460 if (S_ISLNK(mode
)) {
2462 ret
= read_symlink(sctx
, sctx
->send_root
, ino
, p
);
2465 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH_LINK
, p
);
2466 } else if (S_ISCHR(mode
) || S_ISBLK(mode
) ||
2467 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
2468 TLV_PUT_U64(sctx
, BTRFS_SEND_A_RDEV
, new_encode_dev(rdev
));
2469 TLV_PUT_U64(sctx
, BTRFS_SEND_A_MODE
, mode
);
2472 ret
= send_cmd(sctx
);
2479 fs_path_free(sctx
, p
);
2484 * We need some special handling for inodes that get processed before the parent
2485 * directory got created. See process_recorded_refs for details.
2486 * This function does the check if we already created the dir out of order.
2488 static int did_create_dir(struct send_ctx
*sctx
, u64 dir
)
2491 struct btrfs_path
*path
= NULL
;
2492 struct btrfs_key key
;
2493 struct btrfs_key found_key
;
2494 struct btrfs_key di_key
;
2495 struct extent_buffer
*eb
;
2496 struct btrfs_dir_item
*di
;
2499 path
= alloc_path_for_send();
2506 key
.type
= BTRFS_DIR_INDEX_KEY
;
2509 ret
= btrfs_search_slot_for_read(sctx
->send_root
, &key
, path
,
2514 eb
= path
->nodes
[0];
2515 slot
= path
->slots
[0];
2516 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
2518 if (ret
|| found_key
.objectid
!= key
.objectid
||
2519 found_key
.type
!= key
.type
) {
2524 di
= btrfs_item_ptr(eb
, slot
, struct btrfs_dir_item
);
2525 btrfs_dir_item_key_to_cpu(eb
, di
, &di_key
);
2527 if (di_key
.objectid
< sctx
->send_progress
) {
2532 key
.offset
= found_key
.offset
+ 1;
2533 btrfs_release_path(path
);
2537 btrfs_free_path(path
);
2542 * Only creates the inode if it is:
2543 * 1. Not a directory
2544 * 2. Or a directory which was not created already due to out of order
2545 * directories. See did_create_dir and process_recorded_refs for details.
2547 static int send_create_inode_if_needed(struct send_ctx
*sctx
)
2551 if (S_ISDIR(sctx
->cur_inode_mode
)) {
2552 ret
= did_create_dir(sctx
, sctx
->cur_ino
);
2561 ret
= send_create_inode(sctx
, sctx
->cur_ino
);
2569 struct recorded_ref
{
2570 struct list_head list
;
2573 struct fs_path
*full_path
;
2581 * We need to process new refs before deleted refs, but compare_tree gives us
2582 * everything mixed. So we first record all refs and later process them.
2583 * This function is a helper to record one ref.
2585 static int record_ref(struct list_head
*head
, u64 dir
,
2586 u64 dir_gen
, struct fs_path
*path
)
2588 struct recorded_ref
*ref
;
2591 ref
= kmalloc(sizeof(*ref
), GFP_NOFS
);
2596 ref
->dir_gen
= dir_gen
;
2597 ref
->full_path
= path
;
2599 tmp
= strrchr(ref
->full_path
->start
, '/');
2601 ref
->name_len
= ref
->full_path
->end
- ref
->full_path
->start
;
2602 ref
->name
= ref
->full_path
->start
;
2603 ref
->dir_path_len
= 0;
2604 ref
->dir_path
= ref
->full_path
->start
;
2607 ref
->name_len
= ref
->full_path
->end
- tmp
;
2609 ref
->dir_path
= ref
->full_path
->start
;
2610 ref
->dir_path_len
= ref
->full_path
->end
-
2611 ref
->full_path
->start
- 1 - ref
->name_len
;
2614 list_add_tail(&ref
->list
, head
);
2618 static void __free_recorded_refs(struct send_ctx
*sctx
, struct list_head
*head
)
2620 struct recorded_ref
*cur
;
2622 while (!list_empty(head
)) {
2623 cur
= list_entry(head
->next
, struct recorded_ref
, list
);
2624 fs_path_free(sctx
, cur
->full_path
);
2625 list_del(&cur
->list
);
2630 static void free_recorded_refs(struct send_ctx
*sctx
)
2632 __free_recorded_refs(sctx
, &sctx
->new_refs
);
2633 __free_recorded_refs(sctx
, &sctx
->deleted_refs
);
2637 * Renames/moves a file/dir to its orphan name. Used when the first
2638 * ref of an unprocessed inode gets overwritten and for all non empty
2641 static int orphanize_inode(struct send_ctx
*sctx
, u64 ino
, u64 gen
,
2642 struct fs_path
*path
)
2645 struct fs_path
*orphan
;
2647 orphan
= fs_path_alloc(sctx
);
2651 ret
= gen_unique_name(sctx
, ino
, gen
, orphan
);
2655 ret
= send_rename(sctx
, path
, orphan
);
2658 fs_path_free(sctx
, orphan
);
2663 * Returns 1 if a directory can be removed at this point in time.
2664 * We check this by iterating all dir items and checking if the inode behind
2665 * the dir item was already processed.
2667 static int can_rmdir(struct send_ctx
*sctx
, u64 dir
, u64 send_progress
)
2670 struct btrfs_root
*root
= sctx
->parent_root
;
2671 struct btrfs_path
*path
;
2672 struct btrfs_key key
;
2673 struct btrfs_key found_key
;
2674 struct btrfs_key loc
;
2675 struct btrfs_dir_item
*di
;
2678 * Don't try to rmdir the top/root subvolume dir.
2680 if (dir
== BTRFS_FIRST_FREE_OBJECTID
)
2683 path
= alloc_path_for_send();
2688 key
.type
= BTRFS_DIR_INDEX_KEY
;
2692 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
2696 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
2699 if (ret
|| found_key
.objectid
!= key
.objectid
||
2700 found_key
.type
!= key
.type
) {
2704 di
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2705 struct btrfs_dir_item
);
2706 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &loc
);
2708 if (loc
.objectid
> send_progress
) {
2713 btrfs_release_path(path
);
2714 key
.offset
= found_key
.offset
+ 1;
2720 btrfs_free_path(path
);
2725 * This does all the move/link/unlink/rmdir magic.
2727 static int process_recorded_refs(struct send_ctx
*sctx
)
2730 struct recorded_ref
*cur
;
2731 struct recorded_ref
*cur2
;
2732 struct ulist
*check_dirs
= NULL
;
2733 struct ulist_iterator uit
;
2734 struct ulist_node
*un
;
2735 struct fs_path
*valid_path
= NULL
;
2738 int did_overwrite
= 0;
2741 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx
->cur_ino
);
2744 * This should never happen as the root dir always has the same ref
2745 * which is always '..'
2747 BUG_ON(sctx
->cur_ino
<= BTRFS_FIRST_FREE_OBJECTID
);
2749 valid_path
= fs_path_alloc(sctx
);
2755 check_dirs
= ulist_alloc(GFP_NOFS
);
2762 * First, check if the first ref of the current inode was overwritten
2763 * before. If yes, we know that the current inode was already orphanized
2764 * and thus use the orphan name. If not, we can use get_cur_path to
2765 * get the path of the first ref as it would like while receiving at
2766 * this point in time.
2767 * New inodes are always orphan at the beginning, so force to use the
2768 * orphan name in this case.
2769 * The first ref is stored in valid_path and will be updated if it
2770 * gets moved around.
2772 if (!sctx
->cur_inode_new
) {
2773 ret
= did_overwrite_first_ref(sctx
, sctx
->cur_ino
,
2774 sctx
->cur_inode_gen
);
2780 if (sctx
->cur_inode_new
|| did_overwrite
) {
2781 ret
= gen_unique_name(sctx
, sctx
->cur_ino
,
2782 sctx
->cur_inode_gen
, valid_path
);
2787 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
2793 list_for_each_entry(cur
, &sctx
->new_refs
, list
) {
2795 * We may have refs where the parent directory does not exist
2796 * yet. This happens if the parent directories inum is higher
2797 * the the current inum. To handle this case, we create the
2798 * parent directory out of order. But we need to check if this
2799 * did already happen before due to other refs in the same dir.
2801 ret
= get_cur_inode_state(sctx
, cur
->dir
, cur
->dir_gen
);
2804 if (ret
== inode_state_will_create
) {
2807 * First check if any of the current inodes refs did
2808 * already create the dir.
2810 list_for_each_entry(cur2
, &sctx
->new_refs
, list
) {
2813 if (cur2
->dir
== cur
->dir
) {
2820 * If that did not happen, check if a previous inode
2821 * did already create the dir.
2824 ret
= did_create_dir(sctx
, cur
->dir
);
2828 ret
= send_create_inode(sctx
, cur
->dir
);
2835 * Check if this new ref would overwrite the first ref of
2836 * another unprocessed inode. If yes, orphanize the
2837 * overwritten inode. If we find an overwritten ref that is
2838 * not the first ref, simply unlink it.
2840 ret
= will_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
2841 cur
->name
, cur
->name_len
,
2842 &ow_inode
, &ow_gen
);
2846 ret
= is_first_ref(sctx
, sctx
->parent_root
,
2847 ow_inode
, cur
->dir
, cur
->name
,
2852 ret
= orphanize_inode(sctx
, ow_inode
, ow_gen
,
2857 ret
= send_unlink(sctx
, cur
->full_path
);
2864 * link/move the ref to the new place. If we have an orphan
2865 * inode, move it and update valid_path. If not, link or move
2866 * it depending on the inode mode.
2869 ret
= send_rename(sctx
, valid_path
, cur
->full_path
);
2873 ret
= fs_path_copy(valid_path
, cur
->full_path
);
2877 if (S_ISDIR(sctx
->cur_inode_mode
)) {
2879 * Dirs can't be linked, so move it. For moved
2880 * dirs, we always have one new and one deleted
2881 * ref. The deleted ref is ignored later.
2883 ret
= send_rename(sctx
, valid_path
,
2887 ret
= fs_path_copy(valid_path
, cur
->full_path
);
2891 ret
= send_link(sctx
, cur
->full_path
,
2897 ret
= ulist_add(check_dirs
, cur
->dir
, cur
->dir_gen
,
2903 if (S_ISDIR(sctx
->cur_inode_mode
) && sctx
->cur_inode_deleted
) {
2905 * Check if we can already rmdir the directory. If not,
2906 * orphanize it. For every dir item inside that gets deleted
2907 * later, we do this check again and rmdir it then if possible.
2908 * See the use of check_dirs for more details.
2910 ret
= can_rmdir(sctx
, sctx
->cur_ino
, sctx
->cur_ino
);
2914 ret
= send_rmdir(sctx
, valid_path
);
2917 } else if (!is_orphan
) {
2918 ret
= orphanize_inode(sctx
, sctx
->cur_ino
,
2919 sctx
->cur_inode_gen
, valid_path
);
2925 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
2926 ret
= ulist_add(check_dirs
, cur
->dir
, cur
->dir_gen
,
2931 } else if (S_ISDIR(sctx
->cur_inode_mode
) &&
2932 !list_empty(&sctx
->deleted_refs
)) {
2934 * We have a moved dir. Add the old parent to check_dirs
2936 cur
= list_entry(sctx
->deleted_refs
.next
, struct recorded_ref
,
2938 ret
= ulist_add(check_dirs
, cur
->dir
, cur
->dir_gen
,
2942 } else if (!S_ISDIR(sctx
->cur_inode_mode
)) {
2944 * We have a non dir inode. Go through all deleted refs and
2945 * unlink them if they were not already overwritten by other
2948 list_for_each_entry(cur
, &sctx
->deleted_refs
, list
) {
2949 ret
= did_overwrite_ref(sctx
, cur
->dir
, cur
->dir_gen
,
2950 sctx
->cur_ino
, sctx
->cur_inode_gen
,
2951 cur
->name
, cur
->name_len
);
2955 ret
= send_unlink(sctx
, cur
->full_path
);
2959 ret
= ulist_add(check_dirs
, cur
->dir
, cur
->dir_gen
,
2966 * If the inode is still orphan, unlink the orphan. This may
2967 * happen when a previous inode did overwrite the first ref
2968 * of this inode and no new refs were added for the current
2969 * inode. Unlinking does not mean that the inode is deleted in
2970 * all cases. There may still be links to this inode in other
2974 ret
= send_unlink(sctx
, valid_path
);
2981 * We did collect all parent dirs where cur_inode was once located. We
2982 * now go through all these dirs and check if they are pending for
2983 * deletion and if it's finally possible to perform the rmdir now.
2984 * We also update the inode stats of the parent dirs here.
2986 ULIST_ITER_INIT(&uit
);
2987 while ((un
= ulist_next(check_dirs
, &uit
))) {
2989 * In case we had refs into dirs that were not processed yet,
2990 * we don't need to do the utime and rmdir logic for these dirs.
2991 * The dir will be processed later.
2993 if (un
->val
> sctx
->cur_ino
)
2996 ret
= get_cur_inode_state(sctx
, un
->val
, un
->aux
);
3000 if (ret
== inode_state_did_create
||
3001 ret
== inode_state_no_change
) {
3002 /* TODO delayed utimes */
3003 ret
= send_utimes(sctx
, un
->val
, un
->aux
);
3006 } else if (ret
== inode_state_did_delete
) {
3007 ret
= can_rmdir(sctx
, un
->val
, sctx
->cur_ino
);
3011 ret
= get_cur_path(sctx
, un
->val
, un
->aux
,
3015 ret
= send_rmdir(sctx
, valid_path
);
3025 free_recorded_refs(sctx
);
3026 ulist_free(check_dirs
);
3027 fs_path_free(sctx
, valid_path
);
3031 static int __record_new_ref(int num
, u64 dir
, int index
,
3032 struct fs_path
*name
,
3036 struct send_ctx
*sctx
= ctx
;
3040 p
= fs_path_alloc(sctx
);
3044 ret
= get_inode_info(sctx
->send_root
, dir
, NULL
, &gen
, NULL
, NULL
,
3049 ret
= get_cur_path(sctx
, dir
, gen
, p
);
3052 ret
= fs_path_add_path(p
, name
);
3056 ret
= record_ref(&sctx
->new_refs
, dir
, gen
, p
);
3060 fs_path_free(sctx
, p
);
3064 static int __record_deleted_ref(int num
, u64 dir
, int index
,
3065 struct fs_path
*name
,
3069 struct send_ctx
*sctx
= ctx
;
3073 p
= fs_path_alloc(sctx
);
3077 ret
= get_inode_info(sctx
->parent_root
, dir
, NULL
, &gen
, NULL
, NULL
,
3082 ret
= get_cur_path(sctx
, dir
, gen
, p
);
3085 ret
= fs_path_add_path(p
, name
);
3089 ret
= record_ref(&sctx
->deleted_refs
, dir
, gen
, p
);
3093 fs_path_free(sctx
, p
);
3097 static int record_new_ref(struct send_ctx
*sctx
)
3101 ret
= iterate_inode_ref(sctx
, sctx
->send_root
, sctx
->left_path
,
3102 sctx
->cmp_key
, 0, __record_new_ref
, sctx
);
3111 static int record_deleted_ref(struct send_ctx
*sctx
)
3115 ret
= iterate_inode_ref(sctx
, sctx
->parent_root
, sctx
->right_path
,
3116 sctx
->cmp_key
, 0, __record_deleted_ref
, sctx
);
3125 struct find_ref_ctx
{
3127 struct fs_path
*name
;
3131 static int __find_iref(int num
, u64 dir
, int index
,
3132 struct fs_path
*name
,
3135 struct find_ref_ctx
*ctx
= ctx_
;
3137 if (dir
== ctx
->dir
&& fs_path_len(name
) == fs_path_len(ctx
->name
) &&
3138 strncmp(name
->start
, ctx
->name
->start
, fs_path_len(name
)) == 0) {
3139 ctx
->found_idx
= num
;
3145 static int find_iref(struct send_ctx
*sctx
,
3146 struct btrfs_root
*root
,
3147 struct btrfs_path
*path
,
3148 struct btrfs_key
*key
,
3149 u64 dir
, struct fs_path
*name
)
3152 struct find_ref_ctx ctx
;
3158 ret
= iterate_inode_ref(sctx
, root
, path
, key
, 0, __find_iref
, &ctx
);
3162 if (ctx
.found_idx
== -1)
3165 return ctx
.found_idx
;
3168 static int __record_changed_new_ref(int num
, u64 dir
, int index
,
3169 struct fs_path
*name
,
3173 struct send_ctx
*sctx
= ctx
;
3175 ret
= find_iref(sctx
, sctx
->parent_root
, sctx
->right_path
,
3176 sctx
->cmp_key
, dir
, name
);
3178 ret
= __record_new_ref(num
, dir
, index
, name
, sctx
);
3185 static int __record_changed_deleted_ref(int num
, u64 dir
, int index
,
3186 struct fs_path
*name
,
3190 struct send_ctx
*sctx
= ctx
;
3192 ret
= find_iref(sctx
, sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
3195 ret
= __record_deleted_ref(num
, dir
, index
, name
, sctx
);
3202 static int record_changed_ref(struct send_ctx
*sctx
)
3206 ret
= iterate_inode_ref(sctx
, sctx
->send_root
, sctx
->left_path
,
3207 sctx
->cmp_key
, 0, __record_changed_new_ref
, sctx
);
3210 ret
= iterate_inode_ref(sctx
, sctx
->parent_root
, sctx
->right_path
,
3211 sctx
->cmp_key
, 0, __record_changed_deleted_ref
, sctx
);
3221 * Record and process all refs at once. Needed when an inode changes the
3222 * generation number, which means that it was deleted and recreated.
3224 static int process_all_refs(struct send_ctx
*sctx
,
3225 enum btrfs_compare_tree_result cmd
)
3228 struct btrfs_root
*root
;
3229 struct btrfs_path
*path
;
3230 struct btrfs_key key
;
3231 struct btrfs_key found_key
;
3232 struct extent_buffer
*eb
;
3234 iterate_inode_ref_t cb
;
3236 path
= alloc_path_for_send();
3240 if (cmd
== BTRFS_COMPARE_TREE_NEW
) {
3241 root
= sctx
->send_root
;
3242 cb
= __record_new_ref
;
3243 } else if (cmd
== BTRFS_COMPARE_TREE_DELETED
) {
3244 root
= sctx
->parent_root
;
3245 cb
= __record_deleted_ref
;
3250 key
.objectid
= sctx
->cmp_key
->objectid
;
3251 key
.type
= BTRFS_INODE_REF_KEY
;
3254 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
3260 eb
= path
->nodes
[0];
3261 slot
= path
->slots
[0];
3262 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3264 if (found_key
.objectid
!= key
.objectid
||
3265 (found_key
.type
!= BTRFS_INODE_REF_KEY
&&
3266 found_key
.type
!= BTRFS_INODE_EXTREF_KEY
))
3269 ret
= iterate_inode_ref(sctx
, root
, path
, &found_key
, 0, cb
,
3271 btrfs_release_path(path
);
3275 key
.offset
= found_key
.offset
+ 1;
3277 btrfs_release_path(path
);
3279 ret
= process_recorded_refs(sctx
);
3282 btrfs_free_path(path
);
3286 static int send_set_xattr(struct send_ctx
*sctx
,
3287 struct fs_path
*path
,
3288 const char *name
, int name_len
,
3289 const char *data
, int data_len
)
3293 ret
= begin_cmd(sctx
, BTRFS_SEND_C_SET_XATTR
);
3297 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
3298 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
3299 TLV_PUT(sctx
, BTRFS_SEND_A_XATTR_DATA
, data
, data_len
);
3301 ret
= send_cmd(sctx
);
3308 static int send_remove_xattr(struct send_ctx
*sctx
,
3309 struct fs_path
*path
,
3310 const char *name
, int name_len
)
3314 ret
= begin_cmd(sctx
, BTRFS_SEND_C_REMOVE_XATTR
);
3318 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, path
);
3319 TLV_PUT_STRING(sctx
, BTRFS_SEND_A_XATTR_NAME
, name
, name_len
);
3321 ret
= send_cmd(sctx
);
3328 static int __process_new_xattr(int num
, struct btrfs_key
*di_key
,
3329 const char *name
, int name_len
,
3330 const char *data
, int data_len
,
3334 struct send_ctx
*sctx
= ctx
;
3336 posix_acl_xattr_header dummy_acl
;
3338 p
= fs_path_alloc(sctx
);
3343 * This hack is needed because empty acl's are stored as zero byte
3344 * data in xattrs. Problem with that is, that receiving these zero byte
3345 * acl's will fail later. To fix this, we send a dummy acl list that
3346 * only contains the version number and no entries.
3348 if (!strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
, name_len
) ||
3349 !strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
, name_len
)) {
3350 if (data_len
== 0) {
3351 dummy_acl
.a_version
=
3352 cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
3353 data
= (char *)&dummy_acl
;
3354 data_len
= sizeof(dummy_acl
);
3358 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3362 ret
= send_set_xattr(sctx
, p
, name
, name_len
, data
, data_len
);
3365 fs_path_free(sctx
, p
);
3369 static int __process_deleted_xattr(int num
, struct btrfs_key
*di_key
,
3370 const char *name
, int name_len
,
3371 const char *data
, int data_len
,
3375 struct send_ctx
*sctx
= ctx
;
3378 p
= fs_path_alloc(sctx
);
3382 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3386 ret
= send_remove_xattr(sctx
, p
, name
, name_len
);
3389 fs_path_free(sctx
, p
);
3393 static int process_new_xattr(struct send_ctx
*sctx
)
3397 ret
= iterate_dir_item(sctx
, sctx
->send_root
, sctx
->left_path
,
3398 sctx
->cmp_key
, __process_new_xattr
, sctx
);
3403 static int process_deleted_xattr(struct send_ctx
*sctx
)
3407 ret
= iterate_dir_item(sctx
, sctx
->parent_root
, sctx
->right_path
,
3408 sctx
->cmp_key
, __process_deleted_xattr
, sctx
);
3413 struct find_xattr_ctx
{
3421 static int __find_xattr(int num
, struct btrfs_key
*di_key
,
3422 const char *name
, int name_len
,
3423 const char *data
, int data_len
,
3424 u8 type
, void *vctx
)
3426 struct find_xattr_ctx
*ctx
= vctx
;
3428 if (name_len
== ctx
->name_len
&&
3429 strncmp(name
, ctx
->name
, name_len
) == 0) {
3430 ctx
->found_idx
= num
;
3431 ctx
->found_data_len
= data_len
;
3432 ctx
->found_data
= kmalloc(data_len
, GFP_NOFS
);
3433 if (!ctx
->found_data
)
3435 memcpy(ctx
->found_data
, data
, data_len
);
3441 static int find_xattr(struct send_ctx
*sctx
,
3442 struct btrfs_root
*root
,
3443 struct btrfs_path
*path
,
3444 struct btrfs_key
*key
,
3445 const char *name
, int name_len
,
3446 char **data
, int *data_len
)
3449 struct find_xattr_ctx ctx
;
3452 ctx
.name_len
= name_len
;
3454 ctx
.found_data
= NULL
;
3455 ctx
.found_data_len
= 0;
3457 ret
= iterate_dir_item(sctx
, root
, path
, key
, __find_xattr
, &ctx
);
3461 if (ctx
.found_idx
== -1)
3464 *data
= ctx
.found_data
;
3465 *data_len
= ctx
.found_data_len
;
3467 kfree(ctx
.found_data
);
3469 return ctx
.found_idx
;
3473 static int __process_changed_new_xattr(int num
, struct btrfs_key
*di_key
,
3474 const char *name
, int name_len
,
3475 const char *data
, int data_len
,
3479 struct send_ctx
*sctx
= ctx
;
3480 char *found_data
= NULL
;
3481 int found_data_len
= 0;
3483 ret
= find_xattr(sctx
, sctx
->parent_root
, sctx
->right_path
,
3484 sctx
->cmp_key
, name
, name_len
, &found_data
,
3486 if (ret
== -ENOENT
) {
3487 ret
= __process_new_xattr(num
, di_key
, name
, name_len
, data
,
3488 data_len
, type
, ctx
);
3489 } else if (ret
>= 0) {
3490 if (data_len
!= found_data_len
||
3491 memcmp(data
, found_data
, data_len
)) {
3492 ret
= __process_new_xattr(num
, di_key
, name
, name_len
,
3493 data
, data_len
, type
, ctx
);
3503 static int __process_changed_deleted_xattr(int num
, struct btrfs_key
*di_key
,
3504 const char *name
, int name_len
,
3505 const char *data
, int data_len
,
3509 struct send_ctx
*sctx
= ctx
;
3511 ret
= find_xattr(sctx
, sctx
->send_root
, sctx
->left_path
, sctx
->cmp_key
,
3512 name
, name_len
, NULL
, NULL
);
3514 ret
= __process_deleted_xattr(num
, di_key
, name
, name_len
, data
,
3515 data_len
, type
, ctx
);
3522 static int process_changed_xattr(struct send_ctx
*sctx
)
3526 ret
= iterate_dir_item(sctx
, sctx
->send_root
, sctx
->left_path
,
3527 sctx
->cmp_key
, __process_changed_new_xattr
, sctx
);
3530 ret
= iterate_dir_item(sctx
, sctx
->parent_root
, sctx
->right_path
,
3531 sctx
->cmp_key
, __process_changed_deleted_xattr
, sctx
);
3537 static int process_all_new_xattrs(struct send_ctx
*sctx
)
3540 struct btrfs_root
*root
;
3541 struct btrfs_path
*path
;
3542 struct btrfs_key key
;
3543 struct btrfs_key found_key
;
3544 struct extent_buffer
*eb
;
3547 path
= alloc_path_for_send();
3551 root
= sctx
->send_root
;
3553 key
.objectid
= sctx
->cmp_key
->objectid
;
3554 key
.type
= BTRFS_XATTR_ITEM_KEY
;
3557 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
3565 eb
= path
->nodes
[0];
3566 slot
= path
->slots
[0];
3567 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3569 if (found_key
.objectid
!= key
.objectid
||
3570 found_key
.type
!= key
.type
) {
3575 ret
= iterate_dir_item(sctx
, root
, path
, &found_key
,
3576 __process_new_xattr
, sctx
);
3580 btrfs_release_path(path
);
3581 key
.offset
= found_key
.offset
+ 1;
3585 btrfs_free_path(path
);
3590 * Read some bytes from the current inode/file and send a write command to
3593 static int send_write(struct send_ctx
*sctx
, u64 offset
, u32 len
)
3597 loff_t pos
= offset
;
3599 mm_segment_t old_fs
;
3601 p
= fs_path_alloc(sctx
);
3606 * vfs normally only accepts user space buffers for security reasons.
3607 * we only read from the file and also only provide the read_buf buffer
3608 * to vfs. As this buffer does not come from a user space call, it's
3609 * ok to temporary allow kernel space buffers.
3614 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset
, len
);
3616 ret
= open_cur_inode_file(sctx
);
3620 ret
= vfs_read(sctx
->cur_inode_filp
, sctx
->read_buf
, len
, &pos
);
3627 ret
= begin_cmd(sctx
, BTRFS_SEND_C_WRITE
);
3631 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3635 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
3636 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
3637 TLV_PUT(sctx
, BTRFS_SEND_A_DATA
, sctx
->read_buf
, num_read
);
3639 ret
= send_cmd(sctx
);
3643 fs_path_free(sctx
, p
);
3651 * Send a clone command to user space.
3653 static int send_clone(struct send_ctx
*sctx
,
3654 u64 offset
, u32 len
,
3655 struct clone_root
*clone_root
)
3661 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3662 "clone_inode=%llu, clone_offset=%llu\n", offset
, len
,
3663 clone_root
->root
->objectid
, clone_root
->ino
,
3664 clone_root
->offset
);
3666 p
= fs_path_alloc(sctx
);
3670 ret
= begin_cmd(sctx
, BTRFS_SEND_C_CLONE
);
3674 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3678 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
3679 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_LEN
, len
);
3680 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
3682 if (clone_root
->root
== sctx
->send_root
) {
3683 ret
= get_inode_info(sctx
->send_root
, clone_root
->ino
, NULL
,
3684 &gen
, NULL
, NULL
, NULL
, NULL
);
3687 ret
= get_cur_path(sctx
, clone_root
->ino
, gen
, p
);
3689 ret
= get_inode_path(sctx
, clone_root
->root
,
3690 clone_root
->ino
, p
);
3695 TLV_PUT_UUID(sctx
, BTRFS_SEND_A_CLONE_UUID
,
3696 clone_root
->root
->root_item
.uuid
);
3697 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_CTRANSID
,
3698 clone_root
->root
->root_item
.ctransid
);
3699 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_CLONE_PATH
, p
);
3700 TLV_PUT_U64(sctx
, BTRFS_SEND_A_CLONE_OFFSET
,
3701 clone_root
->offset
);
3703 ret
= send_cmd(sctx
);
3707 fs_path_free(sctx
, p
);
3712 * Send an update extent command to user space.
3714 static int send_update_extent(struct send_ctx
*sctx
,
3715 u64 offset
, u32 len
)
3720 p
= fs_path_alloc(sctx
);
3724 ret
= begin_cmd(sctx
, BTRFS_SEND_C_UPDATE_EXTENT
);
3728 ret
= get_cur_path(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
, p
);
3732 TLV_PUT_PATH(sctx
, BTRFS_SEND_A_PATH
, p
);
3733 TLV_PUT_U64(sctx
, BTRFS_SEND_A_FILE_OFFSET
, offset
);
3734 TLV_PUT_U64(sctx
, BTRFS_SEND_A_SIZE
, len
);
3736 ret
= send_cmd(sctx
);
3740 fs_path_free(sctx
, p
);
3744 static int send_write_or_clone(struct send_ctx
*sctx
,
3745 struct btrfs_path
*path
,
3746 struct btrfs_key
*key
,
3747 struct clone_root
*clone_root
)
3750 struct btrfs_file_extent_item
*ei
;
3751 u64 offset
= key
->offset
;
3757 ei
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
3758 struct btrfs_file_extent_item
);
3759 type
= btrfs_file_extent_type(path
->nodes
[0], ei
);
3760 if (type
== BTRFS_FILE_EXTENT_INLINE
) {
3761 len
= btrfs_file_extent_inline_len(path
->nodes
[0], ei
);
3763 * it is possible the inline item won't cover the whole page,
3764 * but there may be items after this page. Make
3765 * sure to send the whole thing
3767 len
= PAGE_CACHE_ALIGN(len
);
3769 len
= btrfs_file_extent_num_bytes(path
->nodes
[0], ei
);
3772 if (offset
+ len
> sctx
->cur_inode_size
)
3773 len
= sctx
->cur_inode_size
- offset
;
3780 ret
= send_clone(sctx
, offset
, len
, clone_root
);
3781 } else if (sctx
->flags
& BTRFS_SEND_FLAG_NO_FILE_DATA
) {
3782 ret
= send_update_extent(sctx
, offset
, len
);
3786 if (l
> BTRFS_SEND_READ_SIZE
)
3787 l
= BTRFS_SEND_READ_SIZE
;
3788 ret
= send_write(sctx
, pos
+ offset
, l
);
3801 static int is_extent_unchanged(struct send_ctx
*sctx
,
3802 struct btrfs_path
*left_path
,
3803 struct btrfs_key
*ekey
)
3806 struct btrfs_key key
;
3807 struct btrfs_path
*path
= NULL
;
3808 struct extent_buffer
*eb
;
3810 struct btrfs_key found_key
;
3811 struct btrfs_file_extent_item
*ei
;
3816 u64 left_offset_fixed
;
3824 path
= alloc_path_for_send();
3828 eb
= left_path
->nodes
[0];
3829 slot
= left_path
->slots
[0];
3830 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
3831 left_type
= btrfs_file_extent_type(eb
, ei
);
3833 if (left_type
!= BTRFS_FILE_EXTENT_REG
) {
3837 left_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
3838 left_len
= btrfs_file_extent_num_bytes(eb
, ei
);
3839 left_offset
= btrfs_file_extent_offset(eb
, ei
);
3840 left_gen
= btrfs_file_extent_generation(eb
, ei
);
3843 * Following comments will refer to these graphics. L is the left
3844 * extents which we are checking at the moment. 1-8 are the right
3845 * extents that we iterate.
3848 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3851 * |--1--|-2b-|...(same as above)
3853 * Alternative situation. Happens on files where extents got split.
3855 * |-----------7-----------|-6-|
3857 * Alternative situation. Happens on files which got larger.
3860 * Nothing follows after 8.
3863 key
.objectid
= ekey
->objectid
;
3864 key
.type
= BTRFS_EXTENT_DATA_KEY
;
3865 key
.offset
= ekey
->offset
;
3866 ret
= btrfs_search_slot_for_read(sctx
->parent_root
, &key
, path
, 0, 0);
3875 * Handle special case where the right side has no extents at all.
3877 eb
= path
->nodes
[0];
3878 slot
= path
->slots
[0];
3879 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3880 if (found_key
.objectid
!= key
.objectid
||
3881 found_key
.type
!= key
.type
) {
3887 * We're now on 2a, 2b or 7.
3890 while (key
.offset
< ekey
->offset
+ left_len
) {
3891 ei
= btrfs_item_ptr(eb
, slot
, struct btrfs_file_extent_item
);
3892 right_type
= btrfs_file_extent_type(eb
, ei
);
3893 right_disknr
= btrfs_file_extent_disk_bytenr(eb
, ei
);
3894 right_len
= btrfs_file_extent_num_bytes(eb
, ei
);
3895 right_offset
= btrfs_file_extent_offset(eb
, ei
);
3896 right_gen
= btrfs_file_extent_generation(eb
, ei
);
3898 if (right_type
!= BTRFS_FILE_EXTENT_REG
) {
3904 * Are we at extent 8? If yes, we know the extent is changed.
3905 * This may only happen on the first iteration.
3907 if (found_key
.offset
+ right_len
<= ekey
->offset
) {
3912 left_offset_fixed
= left_offset
;
3913 if (key
.offset
< ekey
->offset
) {
3914 /* Fix the right offset for 2a and 7. */
3915 right_offset
+= ekey
->offset
- key
.offset
;
3917 /* Fix the left offset for all behind 2a and 2b */
3918 left_offset_fixed
+= key
.offset
- ekey
->offset
;
3922 * Check if we have the same extent.
3924 if (left_disknr
!= right_disknr
||
3925 left_offset_fixed
!= right_offset
||
3926 left_gen
!= right_gen
) {
3932 * Go to the next extent.
3934 ret
= btrfs_next_item(sctx
->parent_root
, path
);
3938 eb
= path
->nodes
[0];
3939 slot
= path
->slots
[0];
3940 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
3942 if (ret
|| found_key
.objectid
!= key
.objectid
||
3943 found_key
.type
!= key
.type
) {
3944 key
.offset
+= right_len
;
3947 if (found_key
.offset
!= key
.offset
+ right_len
) {
3955 * We're now behind the left extent (treat as unchanged) or at the end
3956 * of the right side (treat as changed).
3958 if (key
.offset
>= ekey
->offset
+ left_len
)
3965 btrfs_free_path(path
);
3969 static int process_extent(struct send_ctx
*sctx
,
3970 struct btrfs_path
*path
,
3971 struct btrfs_key
*key
)
3974 struct clone_root
*found_clone
= NULL
;
3976 if (S_ISLNK(sctx
->cur_inode_mode
))
3979 if (sctx
->parent_root
&& !sctx
->cur_inode_new
) {
3980 ret
= is_extent_unchanged(sctx
, path
, key
);
3989 ret
= find_extent_clone(sctx
, path
, key
->objectid
, key
->offset
,
3990 sctx
->cur_inode_size
, &found_clone
);
3991 if (ret
!= -ENOENT
&& ret
< 0)
3994 ret
= send_write_or_clone(sctx
, path
, key
, found_clone
);
4000 static int process_all_extents(struct send_ctx
*sctx
)
4003 struct btrfs_root
*root
;
4004 struct btrfs_path
*path
;
4005 struct btrfs_key key
;
4006 struct btrfs_key found_key
;
4007 struct extent_buffer
*eb
;
4010 root
= sctx
->send_root
;
4011 path
= alloc_path_for_send();
4015 key
.objectid
= sctx
->cmp_key
->objectid
;
4016 key
.type
= BTRFS_EXTENT_DATA_KEY
;
4019 ret
= btrfs_search_slot_for_read(root
, &key
, path
, 1, 0);
4027 eb
= path
->nodes
[0];
4028 slot
= path
->slots
[0];
4029 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4031 if (found_key
.objectid
!= key
.objectid
||
4032 found_key
.type
!= key
.type
) {
4037 ret
= process_extent(sctx
, path
, &found_key
);
4041 btrfs_release_path(path
);
4042 key
.offset
= found_key
.offset
+ 1;
4046 btrfs_free_path(path
);
4050 static int process_recorded_refs_if_needed(struct send_ctx
*sctx
, int at_end
)
4054 if (sctx
->cur_ino
== 0)
4056 if (!at_end
&& sctx
->cur_ino
== sctx
->cmp_key
->objectid
&&
4057 sctx
->cmp_key
->type
<= BTRFS_INODE_EXTREF_KEY
)
4059 if (list_empty(&sctx
->new_refs
) && list_empty(&sctx
->deleted_refs
))
4062 ret
= process_recorded_refs(sctx
);
4067 * We have processed the refs and thus need to advance send_progress.
4068 * Now, calls to get_cur_xxx will take the updated refs of the current
4069 * inode into account.
4071 sctx
->send_progress
= sctx
->cur_ino
+ 1;
4077 static int finish_inode_if_needed(struct send_ctx
*sctx
, int at_end
)
4089 ret
= process_recorded_refs_if_needed(sctx
, at_end
);
4093 if (sctx
->cur_ino
== 0 || sctx
->cur_inode_deleted
)
4095 if (!at_end
&& sctx
->cmp_key
->objectid
== sctx
->cur_ino
)
4098 ret
= get_inode_info(sctx
->send_root
, sctx
->cur_ino
, NULL
, NULL
,
4099 &left_mode
, &left_uid
, &left_gid
, NULL
);
4103 if (!sctx
->parent_root
|| sctx
->cur_inode_new
) {
4105 if (!S_ISLNK(sctx
->cur_inode_mode
))
4108 ret
= get_inode_info(sctx
->parent_root
, sctx
->cur_ino
,
4109 NULL
, NULL
, &right_mode
, &right_uid
,
4114 if (left_uid
!= right_uid
|| left_gid
!= right_gid
)
4116 if (!S_ISLNK(sctx
->cur_inode_mode
) && left_mode
!= right_mode
)
4120 if (S_ISREG(sctx
->cur_inode_mode
)) {
4121 ret
= send_truncate(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4122 sctx
->cur_inode_size
);
4128 ret
= send_chown(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4129 left_uid
, left_gid
);
4134 ret
= send_chmod(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
,
4141 * Need to send that every time, no matter if it actually changed
4142 * between the two trees as we have done changes to the inode before.
4144 ret
= send_utimes(sctx
, sctx
->cur_ino
, sctx
->cur_inode_gen
);
4152 static int changed_inode(struct send_ctx
*sctx
,
4153 enum btrfs_compare_tree_result result
)
4156 struct btrfs_key
*key
= sctx
->cmp_key
;
4157 struct btrfs_inode_item
*left_ii
= NULL
;
4158 struct btrfs_inode_item
*right_ii
= NULL
;
4162 ret
= close_cur_inode_file(sctx
);
4166 sctx
->cur_ino
= key
->objectid
;
4167 sctx
->cur_inode_new_gen
= 0;
4170 * Set send_progress to current inode. This will tell all get_cur_xxx
4171 * functions that the current inode's refs are not updated yet. Later,
4172 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4174 sctx
->send_progress
= sctx
->cur_ino
;
4176 if (result
== BTRFS_COMPARE_TREE_NEW
||
4177 result
== BTRFS_COMPARE_TREE_CHANGED
) {
4178 left_ii
= btrfs_item_ptr(sctx
->left_path
->nodes
[0],
4179 sctx
->left_path
->slots
[0],
4180 struct btrfs_inode_item
);
4181 left_gen
= btrfs_inode_generation(sctx
->left_path
->nodes
[0],
4184 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
4185 sctx
->right_path
->slots
[0],
4186 struct btrfs_inode_item
);
4187 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
4190 if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
4191 right_ii
= btrfs_item_ptr(sctx
->right_path
->nodes
[0],
4192 sctx
->right_path
->slots
[0],
4193 struct btrfs_inode_item
);
4195 right_gen
= btrfs_inode_generation(sctx
->right_path
->nodes
[0],
4199 * The cur_ino = root dir case is special here. We can't treat
4200 * the inode as deleted+reused because it would generate a
4201 * stream that tries to delete/mkdir the root dir.
4203 if (left_gen
!= right_gen
&&
4204 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
4205 sctx
->cur_inode_new_gen
= 1;
4208 if (result
== BTRFS_COMPARE_TREE_NEW
) {
4209 sctx
->cur_inode_gen
= left_gen
;
4210 sctx
->cur_inode_new
= 1;
4211 sctx
->cur_inode_deleted
= 0;
4212 sctx
->cur_inode_size
= btrfs_inode_size(
4213 sctx
->left_path
->nodes
[0], left_ii
);
4214 sctx
->cur_inode_mode
= btrfs_inode_mode(
4215 sctx
->left_path
->nodes
[0], left_ii
);
4216 if (sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
)
4217 ret
= send_create_inode_if_needed(sctx
);
4218 } else if (result
== BTRFS_COMPARE_TREE_DELETED
) {
4219 sctx
->cur_inode_gen
= right_gen
;
4220 sctx
->cur_inode_new
= 0;
4221 sctx
->cur_inode_deleted
= 1;
4222 sctx
->cur_inode_size
= btrfs_inode_size(
4223 sctx
->right_path
->nodes
[0], right_ii
);
4224 sctx
->cur_inode_mode
= btrfs_inode_mode(
4225 sctx
->right_path
->nodes
[0], right_ii
);
4226 } else if (result
== BTRFS_COMPARE_TREE_CHANGED
) {
4228 * We need to do some special handling in case the inode was
4229 * reported as changed with a changed generation number. This
4230 * means that the original inode was deleted and new inode
4231 * reused the same inum. So we have to treat the old inode as
4232 * deleted and the new one as new.
4234 if (sctx
->cur_inode_new_gen
) {
4236 * First, process the inode as if it was deleted.
4238 sctx
->cur_inode_gen
= right_gen
;
4239 sctx
->cur_inode_new
= 0;
4240 sctx
->cur_inode_deleted
= 1;
4241 sctx
->cur_inode_size
= btrfs_inode_size(
4242 sctx
->right_path
->nodes
[0], right_ii
);
4243 sctx
->cur_inode_mode
= btrfs_inode_mode(
4244 sctx
->right_path
->nodes
[0], right_ii
);
4245 ret
= process_all_refs(sctx
,
4246 BTRFS_COMPARE_TREE_DELETED
);
4251 * Now process the inode as if it was new.
4253 sctx
->cur_inode_gen
= left_gen
;
4254 sctx
->cur_inode_new
= 1;
4255 sctx
->cur_inode_deleted
= 0;
4256 sctx
->cur_inode_size
= btrfs_inode_size(
4257 sctx
->left_path
->nodes
[0], left_ii
);
4258 sctx
->cur_inode_mode
= btrfs_inode_mode(
4259 sctx
->left_path
->nodes
[0], left_ii
);
4260 ret
= send_create_inode_if_needed(sctx
);
4264 ret
= process_all_refs(sctx
, BTRFS_COMPARE_TREE_NEW
);
4268 * Advance send_progress now as we did not get into
4269 * process_recorded_refs_if_needed in the new_gen case.
4271 sctx
->send_progress
= sctx
->cur_ino
+ 1;
4274 * Now process all extents and xattrs of the inode as if
4275 * they were all new.
4277 ret
= process_all_extents(sctx
);
4280 ret
= process_all_new_xattrs(sctx
);
4284 sctx
->cur_inode_gen
= left_gen
;
4285 sctx
->cur_inode_new
= 0;
4286 sctx
->cur_inode_new_gen
= 0;
4287 sctx
->cur_inode_deleted
= 0;
4288 sctx
->cur_inode_size
= btrfs_inode_size(
4289 sctx
->left_path
->nodes
[0], left_ii
);
4290 sctx
->cur_inode_mode
= btrfs_inode_mode(
4291 sctx
->left_path
->nodes
[0], left_ii
);
4300 * We have to process new refs before deleted refs, but compare_trees gives us
4301 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4302 * first and later process them in process_recorded_refs.
4303 * For the cur_inode_new_gen case, we skip recording completely because
4304 * changed_inode did already initiate processing of refs. The reason for this is
4305 * that in this case, compare_tree actually compares the refs of 2 different
4306 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4307 * refs of the right tree as deleted and all refs of the left tree as new.
4309 static int changed_ref(struct send_ctx
*sctx
,
4310 enum btrfs_compare_tree_result result
)
4314 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4316 if (!sctx
->cur_inode_new_gen
&&
4317 sctx
->cur_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
4318 if (result
== BTRFS_COMPARE_TREE_NEW
)
4319 ret
= record_new_ref(sctx
);
4320 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
4321 ret
= record_deleted_ref(sctx
);
4322 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
4323 ret
= record_changed_ref(sctx
);
4330 * Process new/deleted/changed xattrs. We skip processing in the
4331 * cur_inode_new_gen case because changed_inode did already initiate processing
4332 * of xattrs. The reason is the same as in changed_ref
4334 static int changed_xattr(struct send_ctx
*sctx
,
4335 enum btrfs_compare_tree_result result
)
4339 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4341 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
4342 if (result
== BTRFS_COMPARE_TREE_NEW
)
4343 ret
= process_new_xattr(sctx
);
4344 else if (result
== BTRFS_COMPARE_TREE_DELETED
)
4345 ret
= process_deleted_xattr(sctx
);
4346 else if (result
== BTRFS_COMPARE_TREE_CHANGED
)
4347 ret
= process_changed_xattr(sctx
);
4354 * Process new/deleted/changed extents. We skip processing in the
4355 * cur_inode_new_gen case because changed_inode did already initiate processing
4356 * of extents. The reason is the same as in changed_ref
4358 static int changed_extent(struct send_ctx
*sctx
,
4359 enum btrfs_compare_tree_result result
)
4363 BUG_ON(sctx
->cur_ino
!= sctx
->cmp_key
->objectid
);
4365 if (!sctx
->cur_inode_new_gen
&& !sctx
->cur_inode_deleted
) {
4366 if (result
!= BTRFS_COMPARE_TREE_DELETED
)
4367 ret
= process_extent(sctx
, sctx
->left_path
,
4375 * Updates compare related fields in sctx and simply forwards to the actual
4376 * changed_xxx functions.
4378 static int changed_cb(struct btrfs_root
*left_root
,
4379 struct btrfs_root
*right_root
,
4380 struct btrfs_path
*left_path
,
4381 struct btrfs_path
*right_path
,
4382 struct btrfs_key
*key
,
4383 enum btrfs_compare_tree_result result
,
4387 struct send_ctx
*sctx
= ctx
;
4389 sctx
->left_path
= left_path
;
4390 sctx
->right_path
= right_path
;
4391 sctx
->cmp_key
= key
;
4393 ret
= finish_inode_if_needed(sctx
, 0);
4397 /* Ignore non-FS objects */
4398 if (key
->objectid
== BTRFS_FREE_INO_OBJECTID
||
4399 key
->objectid
== BTRFS_FREE_SPACE_OBJECTID
)
4402 if (key
->type
== BTRFS_INODE_ITEM_KEY
)
4403 ret
= changed_inode(sctx
, result
);
4404 else if (key
->type
== BTRFS_INODE_REF_KEY
||
4405 key
->type
== BTRFS_INODE_EXTREF_KEY
)
4406 ret
= changed_ref(sctx
, result
);
4407 else if (key
->type
== BTRFS_XATTR_ITEM_KEY
)
4408 ret
= changed_xattr(sctx
, result
);
4409 else if (key
->type
== BTRFS_EXTENT_DATA_KEY
)
4410 ret
= changed_extent(sctx
, result
);
4416 static int full_send_tree(struct send_ctx
*sctx
)
4419 struct btrfs_trans_handle
*trans
= NULL
;
4420 struct btrfs_root
*send_root
= sctx
->send_root
;
4421 struct btrfs_key key
;
4422 struct btrfs_key found_key
;
4423 struct btrfs_path
*path
;
4424 struct extent_buffer
*eb
;
4429 path
= alloc_path_for_send();
4433 spin_lock(&send_root
->root_item_lock
);
4434 start_ctransid
= btrfs_root_ctransid(&send_root
->root_item
);
4435 spin_unlock(&send_root
->root_item_lock
);
4437 key
.objectid
= BTRFS_FIRST_FREE_OBJECTID
;
4438 key
.type
= BTRFS_INODE_ITEM_KEY
;
4443 * We need to make sure the transaction does not get committed
4444 * while we do anything on commit roots. Join a transaction to prevent
4447 trans
= btrfs_join_transaction(send_root
);
4448 if (IS_ERR(trans
)) {
4449 ret
= PTR_ERR(trans
);
4455 * Make sure the tree has not changed after re-joining. We detect this
4456 * by comparing start_ctransid and ctransid. They should always match.
4458 spin_lock(&send_root
->root_item_lock
);
4459 ctransid
= btrfs_root_ctransid(&send_root
->root_item
);
4460 spin_unlock(&send_root
->root_item_lock
);
4462 if (ctransid
!= start_ctransid
) {
4463 WARN(1, KERN_WARNING
"btrfs: the root that you're trying to "
4464 "send was modified in between. This is "
4465 "probably a bug.\n");
4470 ret
= btrfs_search_slot_for_read(send_root
, &key
, path
, 1, 0);
4478 * When someone want to commit while we iterate, end the
4479 * joined transaction and rejoin.
4481 if (btrfs_should_end_transaction(trans
, send_root
)) {
4482 ret
= btrfs_end_transaction(trans
, send_root
);
4486 btrfs_release_path(path
);
4490 eb
= path
->nodes
[0];
4491 slot
= path
->slots
[0];
4492 btrfs_item_key_to_cpu(eb
, &found_key
, slot
);
4494 ret
= changed_cb(send_root
, NULL
, path
, NULL
,
4495 &found_key
, BTRFS_COMPARE_TREE_NEW
, sctx
);
4499 key
.objectid
= found_key
.objectid
;
4500 key
.type
= found_key
.type
;
4501 key
.offset
= found_key
.offset
+ 1;
4503 ret
= btrfs_next_item(send_root
, path
);
4513 ret
= finish_inode_if_needed(sctx
, 1);
4516 btrfs_free_path(path
);
4519 ret
= btrfs_end_transaction(trans
, send_root
);
4521 btrfs_end_transaction(trans
, send_root
);
4526 static int send_subvol(struct send_ctx
*sctx
)
4530 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_STREAM_HEADER
)) {
4531 ret
= send_header(sctx
);
4536 ret
= send_subvol_begin(sctx
);
4540 if (sctx
->parent_root
) {
4541 ret
= btrfs_compare_trees(sctx
->send_root
, sctx
->parent_root
,
4545 ret
= finish_inode_if_needed(sctx
, 1);
4549 ret
= full_send_tree(sctx
);
4556 ret
= close_cur_inode_file(sctx
);
4558 close_cur_inode_file(sctx
);
4560 free_recorded_refs(sctx
);
4564 long btrfs_ioctl_send(struct file
*mnt_file
, void __user
*arg_
)
4567 struct btrfs_root
*send_root
;
4568 struct btrfs_root
*clone_root
;
4569 struct btrfs_fs_info
*fs_info
;
4570 struct btrfs_ioctl_send_args
*arg
= NULL
;
4571 struct btrfs_key key
;
4572 struct send_ctx
*sctx
= NULL
;
4574 u64
*clone_sources_tmp
= NULL
;
4576 if (!capable(CAP_SYS_ADMIN
))
4579 send_root
= BTRFS_I(file_inode(mnt_file
))->root
;
4580 fs_info
= send_root
->fs_info
;
4582 arg
= memdup_user(arg_
, sizeof(*arg
));
4589 if (!access_ok(VERIFY_READ
, arg
->clone_sources
,
4590 sizeof(*arg
->clone_sources
*
4591 arg
->clone_sources_count
))) {
4596 if (arg
->flags
& ~BTRFS_SEND_FLAG_MASK
) {
4601 sctx
= kzalloc(sizeof(struct send_ctx
), GFP_NOFS
);
4607 INIT_LIST_HEAD(&sctx
->new_refs
);
4608 INIT_LIST_HEAD(&sctx
->deleted_refs
);
4609 INIT_RADIX_TREE(&sctx
->name_cache
, GFP_NOFS
);
4610 INIT_LIST_HEAD(&sctx
->name_cache_list
);
4612 sctx
->flags
= arg
->flags
;
4614 sctx
->send_filp
= fget(arg
->send_fd
);
4615 if (!sctx
->send_filp
) {
4620 sctx
->mnt
= mnt_file
->f_path
.mnt
;
4622 sctx
->send_root
= send_root
;
4623 sctx
->clone_roots_cnt
= arg
->clone_sources_count
;
4625 sctx
->send_max_size
= BTRFS_SEND_BUF_SIZE
;
4626 sctx
->send_buf
= vmalloc(sctx
->send_max_size
);
4627 if (!sctx
->send_buf
) {
4632 sctx
->read_buf
= vmalloc(BTRFS_SEND_READ_SIZE
);
4633 if (!sctx
->read_buf
) {
4638 sctx
->clone_roots
= vzalloc(sizeof(struct clone_root
) *
4639 (arg
->clone_sources_count
+ 1));
4640 if (!sctx
->clone_roots
) {
4645 if (arg
->clone_sources_count
) {
4646 clone_sources_tmp
= vmalloc(arg
->clone_sources_count
*
4647 sizeof(*arg
->clone_sources
));
4648 if (!clone_sources_tmp
) {
4653 ret
= copy_from_user(clone_sources_tmp
, arg
->clone_sources
,
4654 arg
->clone_sources_count
*
4655 sizeof(*arg
->clone_sources
));
4661 for (i
= 0; i
< arg
->clone_sources_count
; i
++) {
4662 key
.objectid
= clone_sources_tmp
[i
];
4663 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4664 key
.offset
= (u64
)-1;
4665 clone_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
4670 if (IS_ERR(clone_root
)) {
4671 ret
= PTR_ERR(clone_root
);
4674 sctx
->clone_roots
[i
].root
= clone_root
;
4676 vfree(clone_sources_tmp
);
4677 clone_sources_tmp
= NULL
;
4680 if (arg
->parent_root
) {
4681 key
.objectid
= arg
->parent_root
;
4682 key
.type
= BTRFS_ROOT_ITEM_KEY
;
4683 key
.offset
= (u64
)-1;
4684 sctx
->parent_root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
4685 if (!sctx
->parent_root
) {
4692 * Clones from send_root are allowed, but only if the clone source
4693 * is behind the current send position. This is checked while searching
4694 * for possible clone sources.
4696 sctx
->clone_roots
[sctx
->clone_roots_cnt
++].root
= sctx
->send_root
;
4698 /* We do a bsearch later */
4699 sort(sctx
->clone_roots
, sctx
->clone_roots_cnt
,
4700 sizeof(*sctx
->clone_roots
), __clone_root_cmp_sort
,
4703 ret
= send_subvol(sctx
);
4707 if (!(sctx
->flags
& BTRFS_SEND_FLAG_OMIT_END_CMD
)) {
4708 ret
= begin_cmd(sctx
, BTRFS_SEND_C_END
);
4711 ret
= send_cmd(sctx
);
4718 vfree(clone_sources_tmp
);
4721 if (sctx
->send_filp
)
4722 fput(sctx
->send_filp
);
4724 vfree(sctx
->clone_roots
);
4725 vfree(sctx
->send_buf
);
4726 vfree(sctx
->read_buf
);
4728 name_cache_free(sctx
);