2 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/bsearch.h>
21 #include <linux/file.h>
22 #include <linux/sort.h>
23 #include <linux/mount.h>
24 #include <linux/xattr.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/radix-tree.h>
27 #include <linux/crc32c.h>
28 #include <linux/vmalloc.h>
34 #include "btrfs_inode.h"
35 #include "transaction.h"
37 static int g_verbose = 0;
39 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
42 * A fs_path is a helper to dynamically build path names with unknown size.
43 * It reallocates the internal buffer on demand.
44 * It allows fast adding of path elements on the right side (normal path) and
45 * fast adding to the left side (reversed path). A reversed path can also be
46 * unreversed if needed.
64 #define FS_PATH_INLINE_SIZE \
65 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
68 /* reused for each extent */
70 struct btrfs_root *root;
77 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
78 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
81 struct file *send_filp;
87 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
91 struct btrfs_root *send_root;
92 struct btrfs_root *parent_root;
93 struct clone_root *clone_roots;
96 /* current state of the compare_tree call */
97 struct btrfs_path *left_path;
98 struct btrfs_path *right_path;
99 struct btrfs_key *cmp_key;
102 * infos of the currently processed inode. In case of deleted inodes,
103 * these are the values from the deleted inode.
108 int cur_inode_new_gen;
109 int cur_inode_deleted;
115 struct list_head new_refs;
116 struct list_head deleted_refs;
118 struct radix_tree_root name_cache;
119 struct list_head name_cache_list;
122 struct file *cur_inode_filp;
126 struct name_cache_entry {
127 struct list_head list;
129 * radix_tree has only 32bit entries but we need to handle 64bit inums.
130 * We use the lower 32bit of the 64bit inum to store it in the tree. If
131 * more then one inum would fall into the same entry, we use radix_list
132 * to store the additional entries. radix_list is also used to store
133 * entries where two entries have the same inum but different
136 struct list_head radix_list;
142 int need_later_update;
147 static void fs_path_reset(struct fs_path *p)
150 p->start = p->buf + p->buf_len - 1;
160 static struct fs_path *fs_path_alloc(struct send_ctx *sctx)
164 p = kmalloc(sizeof(*p), GFP_NOFS);
169 p->buf = p->inline_buf;
170 p->buf_len = FS_PATH_INLINE_SIZE;
175 static struct fs_path *fs_path_alloc_reversed(struct send_ctx *sctx)
179 p = fs_path_alloc(sctx);
187 static void fs_path_free(struct send_ctx *sctx, struct fs_path *p)
191 if (p->buf != p->inline_buf) {
200 static int fs_path_len(struct fs_path *p)
202 return p->end - p->start;
205 static int fs_path_ensure_buf(struct fs_path *p, int len)
213 if (p->buf_len >= len)
216 path_len = p->end - p->start;
217 old_buf_len = p->buf_len;
218 len = PAGE_ALIGN(len);
220 if (p->buf == p->inline_buf) {
221 tmp_buf = kmalloc(len, GFP_NOFS);
223 tmp_buf = vmalloc(len);
228 memcpy(tmp_buf, p->buf, p->buf_len);
232 if (p->virtual_mem) {
233 tmp_buf = vmalloc(len);
236 memcpy(tmp_buf, p->buf, p->buf_len);
239 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
241 tmp_buf = vmalloc(len);
244 memcpy(tmp_buf, p->buf, p->buf_len);
253 tmp_buf = p->buf + old_buf_len - path_len - 1;
254 p->end = p->buf + p->buf_len - 1;
255 p->start = p->end - path_len;
256 memmove(p->start, tmp_buf, path_len + 1);
259 p->end = p->start + path_len;
264 static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
269 new_len = p->end - p->start + name_len;
270 if (p->start != p->end)
272 ret = fs_path_ensure_buf(p, new_len);
277 if (p->start != p->end)
279 p->start -= name_len;
280 p->prepared = p->start;
282 if (p->start != p->end)
284 p->prepared = p->end;
293 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
297 ret = fs_path_prepare_for_add(p, name_len);
300 memcpy(p->prepared, name, name_len);
307 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
311 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
314 memcpy(p->prepared, p2->start, p2->end - p2->start);
321 static int fs_path_add_from_extent_buffer(struct fs_path *p,
322 struct extent_buffer *eb,
323 unsigned long off, int len)
327 ret = fs_path_prepare_for_add(p, len);
331 read_extent_buffer(eb, p->prepared, off, len);
339 static void fs_path_remove(struct fs_path *p)
342 while (p->start != p->end && *p->end != '/')
348 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
352 p->reversed = from->reversed;
355 ret = fs_path_add_path(p, from);
361 static void fs_path_unreverse(struct fs_path *p)
370 len = p->end - p->start;
372 p->end = p->start + len;
373 memmove(p->start, tmp, len + 1);
377 static struct btrfs_path *alloc_path_for_send(void)
379 struct btrfs_path *path;
381 path = btrfs_alloc_path();
384 path->search_commit_root = 1;
385 path->skip_locking = 1;
389 static int write_buf(struct send_ctx *sctx, const void *buf, u32 len)
399 ret = vfs_write(sctx->send_filp, (char *)buf + pos, len - pos,
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, &hdr, sizeof(hdr));
560 * For each command/item we want to send to userspace, we call this function.
562 static int begin_cmd(struct send_ctx *sctx, int cmd)
564 struct btrfs_cmd_header *hdr;
566 if (!sctx->send_buf) {
571 BUG_ON(sctx->send_size);
573 sctx->send_size += sizeof(*hdr);
574 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
575 hdr->cmd = cpu_to_le16(cmd);
580 static int send_cmd(struct send_ctx *sctx)
583 struct btrfs_cmd_header *hdr;
586 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
587 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
590 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
591 hdr->crc = cpu_to_le32(crc);
593 ret = write_buf(sctx, sctx->send_buf, sctx->send_size);
595 sctx->total_send_size += sctx->send_size;
596 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
603 * Sends a move instruction to user space
605 static int send_rename(struct send_ctx *sctx,
606 struct fs_path *from, struct fs_path *to)
610 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
612 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
616 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
617 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
619 ret = send_cmd(sctx);
627 * Sends a link instruction to user space
629 static int send_link(struct send_ctx *sctx,
630 struct fs_path *path, struct fs_path *lnk)
634 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
636 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
640 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
641 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
643 ret = send_cmd(sctx);
651 * Sends an unlink instruction to user space
653 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
657 verbose_printk("btrfs: send_unlink %s\n", path->start);
659 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
663 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
665 ret = send_cmd(sctx);
673 * Sends a rmdir instruction to user space
675 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
679 verbose_printk("btrfs: send_rmdir %s\n", path->start);
681 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
685 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
687 ret = send_cmd(sctx);
695 * Helper function to retrieve some fields from an inode item.
697 static int get_inode_info(struct btrfs_root *root,
698 u64 ino, u64 *size, u64 *gen,
699 u64 *mode, u64 *uid, u64 *gid,
703 struct btrfs_inode_item *ii;
704 struct btrfs_key key;
705 struct btrfs_path *path;
707 path = alloc_path_for_send();
712 key.type = BTRFS_INODE_ITEM_KEY;
714 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
722 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
723 struct btrfs_inode_item);
725 *size = btrfs_inode_size(path->nodes[0], ii);
727 *gen = btrfs_inode_generation(path->nodes[0], ii);
729 *mode = btrfs_inode_mode(path->nodes[0], ii);
731 *uid = btrfs_inode_uid(path->nodes[0], ii);
733 *gid = btrfs_inode_gid(path->nodes[0], ii);
735 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
738 btrfs_free_path(path);
742 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
747 * Helper function to iterate the entries in ONE btrfs_inode_ref.
748 * The iterate callback may return a non zero value to stop iteration. This can
749 * be a negative value for error codes or 1 to simply stop it.
751 * path must point to the INODE_REF when called.
753 static int iterate_inode_ref(struct send_ctx *sctx,
754 struct btrfs_root *root, struct btrfs_path *path,
755 struct btrfs_key *found_key, int resolve,
756 iterate_inode_ref_t iterate, void *ctx)
758 struct extent_buffer *eb;
759 struct btrfs_item *item;
760 struct btrfs_inode_ref *iref;
761 struct btrfs_path *tmp_path;
773 p = fs_path_alloc_reversed(sctx);
777 tmp_path = alloc_path_for_send();
779 fs_path_free(sctx, p);
784 slot = path->slots[0];
785 item = btrfs_item_nr(eb, slot);
786 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
789 total = btrfs_item_size(eb, item);
792 while (cur < total) {
795 name_len = btrfs_inode_ref_name_len(eb, iref);
796 index = btrfs_inode_ref_index(eb, iref);
798 start = btrfs_iref_to_path(root, tmp_path, iref, eb,
799 found_key->offset, p->buf,
802 ret = PTR_ERR(start);
805 if (start < p->buf) {
806 /* overflow , try again with larger buffer */
807 ret = fs_path_ensure_buf(p,
808 p->buf_len + p->buf - start);
811 start = btrfs_iref_to_path(root, tmp_path, iref,
812 eb, found_key->offset, p->buf,
815 ret = PTR_ERR(start);
818 BUG_ON(start < p->buf);
822 ret = fs_path_add_from_extent_buffer(p, eb,
823 (unsigned long)(iref + 1), name_len);
829 len = sizeof(*iref) + name_len;
830 iref = (struct btrfs_inode_ref *)((char *)iref + len);
833 ret = iterate(num, found_key->offset, index, p, ctx);
841 btrfs_free_path(tmp_path);
842 fs_path_free(sctx, p);
846 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
847 const char *name, int name_len,
848 const char *data, int data_len,
852 * Helper function to iterate the entries in ONE btrfs_dir_item.
853 * The iterate callback may return a non zero value to stop iteration. This can
854 * be a negative value for error codes or 1 to simply stop it.
856 * path must point to the dir item when called.
858 static int iterate_dir_item(struct send_ctx *sctx,
859 struct btrfs_root *root, struct btrfs_path *path,
860 struct btrfs_key *found_key,
861 iterate_dir_item_t iterate, void *ctx)
864 struct extent_buffer *eb;
865 struct btrfs_item *item;
866 struct btrfs_dir_item *di;
867 struct btrfs_key di_key;
882 buf = kmalloc(buf_len, GFP_NOFS);
889 slot = path->slots[0];
890 item = btrfs_item_nr(eb, slot);
891 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
894 total = btrfs_item_size(eb, item);
897 while (cur < total) {
898 name_len = btrfs_dir_name_len(eb, di);
899 data_len = btrfs_dir_data_len(eb, di);
900 type = btrfs_dir_type(eb, di);
901 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
903 if (name_len + data_len > buf_len) {
904 buf_len = PAGE_ALIGN(name_len + data_len);
906 buf2 = vmalloc(buf_len);
913 buf2 = krealloc(buf, buf_len, GFP_NOFS);
915 buf2 = vmalloc(buf_len);
929 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
930 name_len + data_len);
932 len = sizeof(*di) + name_len + data_len;
933 di = (struct btrfs_dir_item *)((char *)di + len);
936 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
937 data_len, type, ctx);
956 static int __copy_first_ref(int num, u64 dir, int index,
957 struct fs_path *p, void *ctx)
960 struct fs_path *pt = ctx;
962 ret = fs_path_copy(pt, p);
966 /* we want the first only */
971 * Retrieve the first path of an inode. If an inode has more then one
972 * ref/hardlink, this is ignored.
974 static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root,
975 u64 ino, struct fs_path *path)
978 struct btrfs_key key, found_key;
979 struct btrfs_path *p;
981 p = alloc_path_for_send();
988 key.type = BTRFS_INODE_REF_KEY;
991 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
998 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
999 if (found_key.objectid != ino ||
1000 found_key.type != BTRFS_INODE_REF_KEY) {
1005 ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
1006 __copy_first_ref, path);
1016 struct backref_ctx {
1017 struct send_ctx *sctx;
1019 /* number of total found references */
1023 * used for clones found in send_root. clones found behind cur_objectid
1024 * and cur_offset are not considered as allowed clones.
1029 /* may be truncated in case it's the last extent in a file */
1032 /* Just to check for bugs in backref resolving */
1036 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1038 u64 root = (u64)(uintptr_t)key;
1039 struct clone_root *cr = (struct clone_root *)elt;
1041 if (root < cr->root->objectid)
1043 if (root > cr->root->objectid)
1048 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1050 struct clone_root *cr1 = (struct clone_root *)e1;
1051 struct clone_root *cr2 = (struct clone_root *)e2;
1053 if (cr1->root->objectid < cr2->root->objectid)
1055 if (cr1->root->objectid > cr2->root->objectid)
1061 * Called for every backref that is found for the current extent.
1062 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1064 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1066 struct backref_ctx *bctx = ctx_;
1067 struct clone_root *found;
1071 /* First check if the root is in the list of accepted clone sources */
1072 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1073 bctx->sctx->clone_roots_cnt,
1074 sizeof(struct clone_root),
1075 __clone_root_cmp_bsearch);
1079 if (found->root == bctx->sctx->send_root &&
1080 ino == bctx->cur_objectid &&
1081 offset == bctx->cur_offset) {
1082 bctx->found_itself = 1;
1086 * There are inodes that have extents that lie behind its i_size. Don't
1087 * accept clones from these extents.
1089 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1094 if (offset + bctx->extent_len > i_size)
1098 * Make sure we don't consider clones from send_root that are
1099 * behind the current inode/offset.
1101 if (found->root == bctx->sctx->send_root) {
1103 * TODO for the moment we don't accept clones from the inode
1104 * that is currently send. We may change this when
1105 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1108 if (ino >= bctx->cur_objectid)
1111 if (ino > bctx->cur_objectid)
1113 if (offset + bctx->extent_len > bctx->cur_offset)
1119 found->found_refs++;
1120 if (ino < found->ino) {
1122 found->offset = offset;
1123 } else if (found->ino == ino) {
1125 * same extent found more then once in the same file.
1127 if (found->offset > offset + bctx->extent_len)
1128 found->offset = offset;
1135 * Given an inode, offset and extent item, it finds a good clone for a clone
1136 * instruction. Returns -ENOENT when none could be found. The function makes
1137 * sure that the returned clone is usable at the point where sending is at the
1138 * moment. This means, that no clones are accepted which lie behind the current
1141 * path must point to the extent item when called.
1143 static int find_extent_clone(struct send_ctx *sctx,
1144 struct btrfs_path *path,
1145 u64 ino, u64 data_offset,
1147 struct clone_root **found)
1154 u64 extent_item_pos;
1155 struct btrfs_file_extent_item *fi;
1156 struct extent_buffer *eb = path->nodes[0];
1157 struct backref_ctx *backref_ctx = NULL;
1158 struct clone_root *cur_clone_root;
1159 struct btrfs_key found_key;
1160 struct btrfs_path *tmp_path;
1164 tmp_path = alloc_path_for_send();
1168 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1174 if (data_offset >= ino_size) {
1176 * There may be extents that lie behind the file's size.
1177 * I at least had this in combination with snapshotting while
1178 * writing large files.
1184 fi = btrfs_item_ptr(eb, path->slots[0],
1185 struct btrfs_file_extent_item);
1186 extent_type = btrfs_file_extent_type(eb, fi);
1187 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1191 compressed = btrfs_file_extent_compression(eb, fi);
1193 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1194 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1195 if (disk_byte == 0) {
1199 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1201 ret = extent_from_logical(sctx->send_root->fs_info,
1202 disk_byte, tmp_path, &found_key);
1203 btrfs_release_path(tmp_path);
1207 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1213 * Setup the clone roots.
1215 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1216 cur_clone_root = sctx->clone_roots + i;
1217 cur_clone_root->ino = (u64)-1;
1218 cur_clone_root->offset = 0;
1219 cur_clone_root->found_refs = 0;
1222 backref_ctx->sctx = sctx;
1223 backref_ctx->found = 0;
1224 backref_ctx->cur_objectid = ino;
1225 backref_ctx->cur_offset = data_offset;
1226 backref_ctx->found_itself = 0;
1227 backref_ctx->extent_len = num_bytes;
1230 * The last extent of a file may be too large due to page alignment.
1231 * We need to adjust extent_len in this case so that the checks in
1232 * __iterate_backrefs work.
1234 if (data_offset + num_bytes >= ino_size)
1235 backref_ctx->extent_len = ino_size - data_offset;
1238 * Now collect all backrefs.
1240 if (compressed == BTRFS_COMPRESS_NONE)
1241 extent_item_pos = logical - found_key.objectid;
1243 extent_item_pos = 0;
1245 extent_item_pos = logical - found_key.objectid;
1246 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1247 found_key.objectid, extent_item_pos, 1,
1248 __iterate_backrefs, backref_ctx);
1253 if (!backref_ctx->found_itself) {
1254 /* found a bug in backref code? */
1256 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1257 "send_root. inode=%llu, offset=%llu, "
1258 "disk_byte=%llu found extent=%llu\n",
1259 ino, data_offset, disk_byte, found_key.objectid);
1263 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1265 "num_bytes=%llu, logical=%llu\n",
1266 data_offset, ino, num_bytes, logical);
1268 if (!backref_ctx->found)
1269 verbose_printk("btrfs: no clones found\n");
1271 cur_clone_root = NULL;
1272 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1273 if (sctx->clone_roots[i].found_refs) {
1274 if (!cur_clone_root)
1275 cur_clone_root = sctx->clone_roots + i;
1276 else if (sctx->clone_roots[i].root == sctx->send_root)
1277 /* prefer clones from send_root over others */
1278 cur_clone_root = sctx->clone_roots + i;
1283 if (cur_clone_root) {
1284 *found = cur_clone_root;
1291 btrfs_free_path(tmp_path);
1296 static int read_symlink(struct send_ctx *sctx,
1297 struct btrfs_root *root,
1299 struct fs_path *dest)
1302 struct btrfs_path *path;
1303 struct btrfs_key key;
1304 struct btrfs_file_extent_item *ei;
1310 path = alloc_path_for_send();
1315 key.type = BTRFS_EXTENT_DATA_KEY;
1317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1322 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1323 struct btrfs_file_extent_item);
1324 type = btrfs_file_extent_type(path->nodes[0], ei);
1325 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1326 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1327 BUG_ON(compression);
1329 off = btrfs_file_extent_inline_start(ei);
1330 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1332 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1335 btrfs_free_path(path);
1340 * Helper function to generate a file name that is unique in the root of
1341 * send_root and parent_root. This is used to generate names for orphan inodes.
1343 static int gen_unique_name(struct send_ctx *sctx,
1345 struct fs_path *dest)
1348 struct btrfs_path *path;
1349 struct btrfs_dir_item *di;
1354 path = alloc_path_for_send();
1359 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1361 if (len >= sizeof(tmp)) {
1362 /* should really not happen */
1367 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1368 path, BTRFS_FIRST_FREE_OBJECTID,
1369 tmp, strlen(tmp), 0);
1370 btrfs_release_path(path);
1376 /* not unique, try again */
1381 if (!sctx->parent_root) {
1387 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1388 path, BTRFS_FIRST_FREE_OBJECTID,
1389 tmp, strlen(tmp), 0);
1390 btrfs_release_path(path);
1396 /* not unique, try again */
1404 ret = fs_path_add(dest, tmp, strlen(tmp));
1407 btrfs_free_path(path);
1412 inode_state_no_change,
1413 inode_state_will_create,
1414 inode_state_did_create,
1415 inode_state_will_delete,
1416 inode_state_did_delete,
1419 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1427 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1429 if (ret < 0 && ret != -ENOENT)
1433 if (!sctx->parent_root) {
1434 right_ret = -ENOENT;
1436 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1437 NULL, NULL, NULL, NULL);
1438 if (ret < 0 && ret != -ENOENT)
1443 if (!left_ret && !right_ret) {
1444 if (left_gen == gen && right_gen == gen) {
1445 ret = inode_state_no_change;
1446 } else if (left_gen == gen) {
1447 if (ino < sctx->send_progress)
1448 ret = inode_state_did_create;
1450 ret = inode_state_will_create;
1451 } else if (right_gen == gen) {
1452 if (ino < sctx->send_progress)
1453 ret = inode_state_did_delete;
1455 ret = inode_state_will_delete;
1459 } else if (!left_ret) {
1460 if (left_gen == gen) {
1461 if (ino < sctx->send_progress)
1462 ret = inode_state_did_create;
1464 ret = inode_state_will_create;
1468 } else if (!right_ret) {
1469 if (right_gen == gen) {
1470 if (ino < sctx->send_progress)
1471 ret = inode_state_did_delete;
1473 ret = inode_state_will_delete;
1485 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1489 ret = get_cur_inode_state(sctx, ino, gen);
1493 if (ret == inode_state_no_change ||
1494 ret == inode_state_did_create ||
1495 ret == inode_state_will_delete)
1505 * Helper function to lookup a dir item in a dir.
1507 static int lookup_dir_item_inode(struct btrfs_root *root,
1508 u64 dir, const char *name, int name_len,
1513 struct btrfs_dir_item *di;
1514 struct btrfs_key key;
1515 struct btrfs_path *path;
1517 path = alloc_path_for_send();
1521 di = btrfs_lookup_dir_item(NULL, root, path,
1522 dir, name, name_len, 0);
1531 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1532 *found_inode = key.objectid;
1533 *found_type = btrfs_dir_type(path->nodes[0], di);
1536 btrfs_free_path(path);
1541 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1542 * generation of the parent dir and the name of the dir entry.
1544 static int get_first_ref(struct send_ctx *sctx,
1545 struct btrfs_root *root, u64 ino,
1546 u64 *dir, u64 *dir_gen, struct fs_path *name)
1549 struct btrfs_key key;
1550 struct btrfs_key found_key;
1551 struct btrfs_path *path;
1552 struct btrfs_inode_ref *iref;
1555 path = alloc_path_for_send();
1560 key.type = BTRFS_INODE_REF_KEY;
1563 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1567 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1569 if (ret || found_key.objectid != key.objectid ||
1570 found_key.type != key.type) {
1575 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1576 struct btrfs_inode_ref);
1577 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1578 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1579 (unsigned long)(iref + 1), len);
1582 btrfs_release_path(path);
1584 ret = get_inode_info(root, found_key.offset, NULL, dir_gen, NULL, NULL,
1589 *dir = found_key.offset;
1592 btrfs_free_path(path);
1596 static int is_first_ref(struct send_ctx *sctx,
1597 struct btrfs_root *root,
1599 const char *name, int name_len)
1602 struct fs_path *tmp_name;
1606 tmp_name = fs_path_alloc(sctx);
1610 ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1614 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1619 ret = !memcmp(tmp_name->start, name, name_len);
1622 fs_path_free(sctx, tmp_name);
1627 * Used by process_recorded_refs to determine if a new ref would overwrite an
1628 * already existing ref. In case it detects an overwrite, it returns the
1629 * inode/gen in who_ino/who_gen.
1630 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1631 * to make sure later references to the overwritten inode are possible.
1632 * Orphanizing is however only required for the first ref of an inode.
1633 * process_recorded_refs does an additional is_first_ref check to see if
1634 * orphanizing is really required.
1636 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1637 const char *name, int name_len,
1638 u64 *who_ino, u64 *who_gen)
1641 u64 other_inode = 0;
1644 if (!sctx->parent_root)
1647 ret = is_inode_existent(sctx, dir, dir_gen);
1651 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1652 &other_inode, &other_type);
1653 if (ret < 0 && ret != -ENOENT)
1661 * Check if the overwritten ref was already processed. If yes, the ref
1662 * was already unlinked/moved, so we can safely assume that we will not
1663 * overwrite anything at this point in time.
1665 if (other_inode > sctx->send_progress) {
1666 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1667 who_gen, NULL, NULL, NULL, NULL);
1672 *who_ino = other_inode;
1682 * Checks if the ref was overwritten by an already processed inode. This is
1683 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1684 * thus the orphan name needs be used.
1685 * process_recorded_refs also uses it to avoid unlinking of refs that were
1688 static int did_overwrite_ref(struct send_ctx *sctx,
1689 u64 dir, u64 dir_gen,
1690 u64 ino, u64 ino_gen,
1691 const char *name, int name_len)
1698 if (!sctx->parent_root)
1701 ret = is_inode_existent(sctx, dir, dir_gen);
1705 /* check if the ref was overwritten by another ref */
1706 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1707 &ow_inode, &other_type);
1708 if (ret < 0 && ret != -ENOENT)
1711 /* was never and will never be overwritten */
1716 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1721 if (ow_inode == ino && gen == ino_gen) {
1726 /* we know that it is or will be overwritten. check this now */
1727 if (ow_inode < sctx->send_progress)
1737 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1738 * that got overwritten. This is used by process_recorded_refs to determine
1739 * if it has to use the path as returned by get_cur_path or the orphan name.
1741 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1744 struct fs_path *name = NULL;
1748 if (!sctx->parent_root)
1751 name = fs_path_alloc(sctx);
1755 ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name);
1759 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1760 name->start, fs_path_len(name));
1763 fs_path_free(sctx, name);
1768 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1769 * so we need to do some special handling in case we have clashes. This function
1770 * takes care of this with the help of name_cache_entry::radix_list.
1771 * In case of error, nce is kfreed.
1773 static int name_cache_insert(struct send_ctx *sctx,
1774 struct name_cache_entry *nce)
1777 struct list_head *nce_head;
1779 nce_head = radix_tree_lookup(&sctx->name_cache,
1780 (unsigned long)nce->ino);
1782 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1785 INIT_LIST_HEAD(nce_head);
1787 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1794 list_add_tail(&nce->radix_list, nce_head);
1795 list_add_tail(&nce->list, &sctx->name_cache_list);
1796 sctx->name_cache_size++;
1801 static void name_cache_delete(struct send_ctx *sctx,
1802 struct name_cache_entry *nce)
1804 struct list_head *nce_head;
1806 nce_head = radix_tree_lookup(&sctx->name_cache,
1807 (unsigned long)nce->ino);
1810 list_del(&nce->radix_list);
1811 list_del(&nce->list);
1812 sctx->name_cache_size--;
1814 if (list_empty(nce_head)) {
1815 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1820 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1823 struct list_head *nce_head;
1824 struct name_cache_entry *cur;
1826 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1830 list_for_each_entry(cur, nce_head, radix_list) {
1831 if (cur->ino == ino && cur->gen == gen)
1838 * Removes the entry from the list and adds it back to the end. This marks the
1839 * entry as recently used so that name_cache_clean_unused does not remove it.
1841 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1843 list_del(&nce->list);
1844 list_add_tail(&nce->list, &sctx->name_cache_list);
1848 * Remove some entries from the beginning of name_cache_list.
1850 static void name_cache_clean_unused(struct send_ctx *sctx)
1852 struct name_cache_entry *nce;
1854 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1857 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1858 nce = list_entry(sctx->name_cache_list.next,
1859 struct name_cache_entry, list);
1860 name_cache_delete(sctx, nce);
1865 static void name_cache_free(struct send_ctx *sctx)
1867 struct name_cache_entry *nce;
1869 while (!list_empty(&sctx->name_cache_list)) {
1870 nce = list_entry(sctx->name_cache_list.next,
1871 struct name_cache_entry, list);
1872 name_cache_delete(sctx, nce);
1878 * Used by get_cur_path for each ref up to the root.
1879 * Returns 0 if it succeeded.
1880 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1881 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1882 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1883 * Returns <0 in case of error.
1885 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1889 struct fs_path *dest)
1893 struct btrfs_path *path = NULL;
1894 struct name_cache_entry *nce = NULL;
1897 * First check if we already did a call to this function with the same
1898 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1899 * return the cached result.
1901 nce = name_cache_search(sctx, ino, gen);
1903 if (ino < sctx->send_progress && nce->need_later_update) {
1904 name_cache_delete(sctx, nce);
1908 name_cache_used(sctx, nce);
1909 *parent_ino = nce->parent_ino;
1910 *parent_gen = nce->parent_gen;
1911 ret = fs_path_add(dest, nce->name, nce->name_len);
1919 path = alloc_path_for_send();
1924 * If the inode is not existent yet, add the orphan name and return 1.
1925 * This should only happen for the parent dir that we determine in
1928 ret = is_inode_existent(sctx, ino, gen);
1933 ret = gen_unique_name(sctx, ino, gen, dest);
1941 * Depending on whether the inode was already processed or not, use
1942 * send_root or parent_root for ref lookup.
1944 if (ino < sctx->send_progress)
1945 ret = get_first_ref(sctx, sctx->send_root, ino,
1946 parent_ino, parent_gen, dest);
1948 ret = get_first_ref(sctx, sctx->parent_root, ino,
1949 parent_ino, parent_gen, dest);
1954 * Check if the ref was overwritten by an inode's ref that was processed
1955 * earlier. If yes, treat as orphan and return 1.
1957 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1958 dest->start, dest->end - dest->start);
1962 fs_path_reset(dest);
1963 ret = gen_unique_name(sctx, ino, gen, dest);
1971 * Store the result of the lookup in the name cache.
1973 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
1981 nce->parent_ino = *parent_ino;
1982 nce->parent_gen = *parent_gen;
1983 nce->name_len = fs_path_len(dest);
1985 strcpy(nce->name, dest->start);
1987 if (ino < sctx->send_progress)
1988 nce->need_later_update = 0;
1990 nce->need_later_update = 1;
1992 nce_ret = name_cache_insert(sctx, nce);
1995 name_cache_clean_unused(sctx);
1998 btrfs_free_path(path);
2003 * Magic happens here. This function returns the first ref to an inode as it
2004 * would look like while receiving the stream at this point in time.
2005 * We walk the path up to the root. For every inode in between, we check if it
2006 * was already processed/sent. If yes, we continue with the parent as found
2007 * in send_root. If not, we continue with the parent as found in parent_root.
2008 * If we encounter an inode that was deleted at this point in time, we use the
2009 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2010 * that were not created yet and overwritten inodes/refs.
2012 * When do we have have orphan inodes:
2013 * 1. When an inode is freshly created and thus no valid refs are available yet
2014 * 2. When a directory lost all it's refs (deleted) but still has dir items
2015 * inside which were not processed yet (pending for move/delete). If anyone
2016 * tried to get the path to the dir items, it would get a path inside that
2018 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2019 * of an unprocessed inode. If in that case the first ref would be
2020 * overwritten, the overwritten inode gets "orphanized". Later when we
2021 * process this overwritten inode, it is restored at a new place by moving
2024 * sctx->send_progress tells this function at which point in time receiving
2027 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2028 struct fs_path *dest)
2031 struct fs_path *name = NULL;
2032 u64 parent_inode = 0;
2036 name = fs_path_alloc(sctx);
2043 fs_path_reset(dest);
2045 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2046 fs_path_reset(name);
2048 ret = __get_cur_name_and_parent(sctx, ino, gen,
2049 &parent_inode, &parent_gen, name);
2055 ret = fs_path_add_path(dest, name);
2064 fs_path_free(sctx, name);
2066 fs_path_unreverse(dest);
2071 * Called for regular files when sending extents data. Opens a struct file
2072 * to read from the file.
2074 static int open_cur_inode_file(struct send_ctx *sctx)
2077 struct btrfs_key key;
2079 struct inode *inode;
2080 struct dentry *dentry;
2084 if (sctx->cur_inode_filp)
2087 key.objectid = sctx->cur_ino;
2088 key.type = BTRFS_INODE_ITEM_KEY;
2091 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2093 if (IS_ERR(inode)) {
2094 ret = PTR_ERR(inode);
2098 dentry = d_obtain_alias(inode);
2100 if (IS_ERR(dentry)) {
2101 ret = PTR_ERR(dentry);
2105 path.mnt = sctx->mnt;
2106 path.dentry = dentry;
2107 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2111 ret = PTR_ERR(filp);
2114 sctx->cur_inode_filp = filp;
2118 * no xxxput required here as every vfs op
2119 * does it by itself on failure
2125 * Closes the struct file that was created in open_cur_inode_file
2127 static int close_cur_inode_file(struct send_ctx *sctx)
2131 if (!sctx->cur_inode_filp)
2134 ret = filp_close(sctx->cur_inode_filp, NULL);
2135 sctx->cur_inode_filp = NULL;
2142 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2144 static int send_subvol_begin(struct send_ctx *sctx)
2147 struct btrfs_root *send_root = sctx->send_root;
2148 struct btrfs_root *parent_root = sctx->parent_root;
2149 struct btrfs_path *path;
2150 struct btrfs_key key;
2151 struct btrfs_root_ref *ref;
2152 struct extent_buffer *leaf;
2156 path = alloc_path_for_send();
2160 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2162 btrfs_free_path(path);
2166 key.objectid = send_root->objectid;
2167 key.type = BTRFS_ROOT_BACKREF_KEY;
2170 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2179 leaf = path->nodes[0];
2180 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2181 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2182 key.objectid != send_root->objectid) {
2186 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2187 namelen = btrfs_root_ref_name_len(leaf, ref);
2188 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2189 btrfs_release_path(path);
2192 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2196 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2201 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2202 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2203 sctx->send_root->root_item.uuid);
2204 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2205 sctx->send_root->root_item.ctransid);
2207 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2208 sctx->parent_root->root_item.uuid);
2209 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2210 sctx->parent_root->root_item.ctransid);
2213 ret = send_cmd(sctx);
2217 btrfs_free_path(path);
2222 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2227 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2229 p = fs_path_alloc(sctx);
2233 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2237 ret = get_cur_path(sctx, ino, gen, p);
2240 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2241 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2243 ret = send_cmd(sctx);
2247 fs_path_free(sctx, p);
2251 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2256 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2258 p = fs_path_alloc(sctx);
2262 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2266 ret = get_cur_path(sctx, ino, gen, p);
2269 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2270 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2272 ret = send_cmd(sctx);
2276 fs_path_free(sctx, p);
2280 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2285 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2287 p = fs_path_alloc(sctx);
2291 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2295 ret = get_cur_path(sctx, ino, gen, p);
2298 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2299 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2300 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2302 ret = send_cmd(sctx);
2306 fs_path_free(sctx, p);
2310 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2313 struct fs_path *p = NULL;
2314 struct btrfs_inode_item *ii;
2315 struct btrfs_path *path = NULL;
2316 struct extent_buffer *eb;
2317 struct btrfs_key key;
2320 verbose_printk("btrfs: send_utimes %llu\n", ino);
2322 p = fs_path_alloc(sctx);
2326 path = alloc_path_for_send();
2333 key.type = BTRFS_INODE_ITEM_KEY;
2335 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2339 eb = path->nodes[0];
2340 slot = path->slots[0];
2341 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2343 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2347 ret = get_cur_path(sctx, ino, gen, p);
2350 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2351 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2352 btrfs_inode_atime(ii));
2353 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2354 btrfs_inode_mtime(ii));
2355 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2356 btrfs_inode_ctime(ii));
2357 /* TODO Add otime support when the otime patches get into upstream */
2359 ret = send_cmd(sctx);
2363 fs_path_free(sctx, p);
2364 btrfs_free_path(path);
2369 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2370 * a valid path yet because we did not process the refs yet. So, the inode
2371 * is created as orphan.
2373 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2382 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2384 p = fs_path_alloc(sctx);
2388 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2393 if (S_ISREG(mode)) {
2394 cmd = BTRFS_SEND_C_MKFILE;
2395 } else if (S_ISDIR(mode)) {
2396 cmd = BTRFS_SEND_C_MKDIR;
2397 } else if (S_ISLNK(mode)) {
2398 cmd = BTRFS_SEND_C_SYMLINK;
2399 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2400 cmd = BTRFS_SEND_C_MKNOD;
2401 } else if (S_ISFIFO(mode)) {
2402 cmd = BTRFS_SEND_C_MKFIFO;
2403 } else if (S_ISSOCK(mode)) {
2404 cmd = BTRFS_SEND_C_MKSOCK;
2406 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2407 (int)(mode & S_IFMT));
2412 ret = begin_cmd(sctx, cmd);
2416 ret = gen_unique_name(sctx, ino, gen, p);
2420 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2421 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2423 if (S_ISLNK(mode)) {
2425 ret = read_symlink(sctx, sctx->send_root, ino, p);
2428 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2429 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2430 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2431 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, rdev);
2434 ret = send_cmd(sctx);
2441 fs_path_free(sctx, p);
2446 * We need some special handling for inodes that get processed before the parent
2447 * directory got created. See process_recorded_refs for details.
2448 * This function does the check if we already created the dir out of order.
2450 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2453 struct btrfs_path *path = NULL;
2454 struct btrfs_key key;
2455 struct btrfs_key found_key;
2456 struct btrfs_key di_key;
2457 struct extent_buffer *eb;
2458 struct btrfs_dir_item *di;
2461 path = alloc_path_for_send();
2468 key.type = BTRFS_DIR_INDEX_KEY;
2471 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2476 eb = path->nodes[0];
2477 slot = path->slots[0];
2478 btrfs_item_key_to_cpu(eb, &found_key, slot);
2480 if (ret || found_key.objectid != key.objectid ||
2481 found_key.type != key.type) {
2486 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2487 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2489 if (di_key.objectid < sctx->send_progress) {
2494 key.offset = found_key.offset + 1;
2495 btrfs_release_path(path);
2499 btrfs_free_path(path);
2504 * Only creates the inode if it is:
2505 * 1. Not a directory
2506 * 2. Or a directory which was not created already due to out of order
2507 * directories. See did_create_dir and process_recorded_refs for details.
2509 static int send_create_inode_if_needed(struct send_ctx *sctx)
2513 if (S_ISDIR(sctx->cur_inode_mode)) {
2514 ret = did_create_dir(sctx, sctx->cur_ino);
2523 ret = send_create_inode(sctx, sctx->cur_ino);
2531 struct recorded_ref {
2532 struct list_head list;
2535 struct fs_path *full_path;
2543 * We need to process new refs before deleted refs, but compare_tree gives us
2544 * everything mixed. So we first record all refs and later process them.
2545 * This function is a helper to record one ref.
2547 static int record_ref(struct list_head *head, u64 dir,
2548 u64 dir_gen, struct fs_path *path)
2550 struct recorded_ref *ref;
2553 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2558 ref->dir_gen = dir_gen;
2559 ref->full_path = path;
2561 tmp = strrchr(ref->full_path->start, '/');
2563 ref->name_len = ref->full_path->end - ref->full_path->start;
2564 ref->name = ref->full_path->start;
2565 ref->dir_path_len = 0;
2566 ref->dir_path = ref->full_path->start;
2569 ref->name_len = ref->full_path->end - tmp;
2571 ref->dir_path = ref->full_path->start;
2572 ref->dir_path_len = ref->full_path->end -
2573 ref->full_path->start - 1 - ref->name_len;
2576 list_add_tail(&ref->list, head);
2580 static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
2582 struct recorded_ref *cur;
2584 while (!list_empty(head)) {
2585 cur = list_entry(head->next, struct recorded_ref, list);
2586 fs_path_free(sctx, cur->full_path);
2587 list_del(&cur->list);
2592 static void free_recorded_refs(struct send_ctx *sctx)
2594 __free_recorded_refs(sctx, &sctx->new_refs);
2595 __free_recorded_refs(sctx, &sctx->deleted_refs);
2599 * Renames/moves a file/dir to its orphan name. Used when the first
2600 * ref of an unprocessed inode gets overwritten and for all non empty
2603 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2604 struct fs_path *path)
2607 struct fs_path *orphan;
2609 orphan = fs_path_alloc(sctx);
2613 ret = gen_unique_name(sctx, ino, gen, orphan);
2617 ret = send_rename(sctx, path, orphan);
2620 fs_path_free(sctx, orphan);
2625 * Returns 1 if a directory can be removed at this point in time.
2626 * We check this by iterating all dir items and checking if the inode behind
2627 * the dir item was already processed.
2629 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2632 struct btrfs_root *root = sctx->parent_root;
2633 struct btrfs_path *path;
2634 struct btrfs_key key;
2635 struct btrfs_key found_key;
2636 struct btrfs_key loc;
2637 struct btrfs_dir_item *di;
2640 * Don't try to rmdir the top/root subvolume dir.
2642 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2645 path = alloc_path_for_send();
2650 key.type = BTRFS_DIR_INDEX_KEY;
2654 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2658 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2661 if (ret || found_key.objectid != key.objectid ||
2662 found_key.type != key.type) {
2666 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2667 struct btrfs_dir_item);
2668 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2670 if (loc.objectid > send_progress) {
2675 btrfs_release_path(path);
2676 key.offset = found_key.offset + 1;
2682 btrfs_free_path(path);
2687 * This does all the move/link/unlink/rmdir magic.
2689 static int process_recorded_refs(struct send_ctx *sctx)
2692 struct recorded_ref *cur;
2693 struct recorded_ref *cur2;
2694 struct ulist *check_dirs = NULL;
2695 struct ulist_iterator uit;
2696 struct ulist_node *un;
2697 struct fs_path *valid_path = NULL;
2700 int did_overwrite = 0;
2703 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2706 * This should never happen as the root dir always has the same ref
2707 * which is always '..'
2709 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
2711 valid_path = fs_path_alloc(sctx);
2717 check_dirs = ulist_alloc(GFP_NOFS);
2724 * First, check if the first ref of the current inode was overwritten
2725 * before. If yes, we know that the current inode was already orphanized
2726 * and thus use the orphan name. If not, we can use get_cur_path to
2727 * get the path of the first ref as it would like while receiving at
2728 * this point in time.
2729 * New inodes are always orphan at the beginning, so force to use the
2730 * orphan name in this case.
2731 * The first ref is stored in valid_path and will be updated if it
2732 * gets moved around.
2734 if (!sctx->cur_inode_new) {
2735 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2736 sctx->cur_inode_gen);
2742 if (sctx->cur_inode_new || did_overwrite) {
2743 ret = gen_unique_name(sctx, sctx->cur_ino,
2744 sctx->cur_inode_gen, valid_path);
2749 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2755 list_for_each_entry(cur, &sctx->new_refs, list) {
2757 * We may have refs where the parent directory does not exist
2758 * yet. This happens if the parent directories inum is higher
2759 * the the current inum. To handle this case, we create the
2760 * parent directory out of order. But we need to check if this
2761 * did already happen before due to other refs in the same dir.
2763 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2766 if (ret == inode_state_will_create) {
2769 * First check if any of the current inodes refs did
2770 * already create the dir.
2772 list_for_each_entry(cur2, &sctx->new_refs, list) {
2775 if (cur2->dir == cur->dir) {
2782 * If that did not happen, check if a previous inode
2783 * did already create the dir.
2786 ret = did_create_dir(sctx, cur->dir);
2790 ret = send_create_inode(sctx, cur->dir);
2797 * Check if this new ref would overwrite the first ref of
2798 * another unprocessed inode. If yes, orphanize the
2799 * overwritten inode. If we find an overwritten ref that is
2800 * not the first ref, simply unlink it.
2802 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2803 cur->name, cur->name_len,
2804 &ow_inode, &ow_gen);
2808 ret = is_first_ref(sctx, sctx->parent_root,
2809 ow_inode, cur->dir, cur->name,
2814 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2819 ret = send_unlink(sctx, cur->full_path);
2826 * link/move the ref to the new place. If we have an orphan
2827 * inode, move it and update valid_path. If not, link or move
2828 * it depending on the inode mode.
2831 ret = send_rename(sctx, valid_path, cur->full_path);
2835 ret = fs_path_copy(valid_path, cur->full_path);
2839 if (S_ISDIR(sctx->cur_inode_mode)) {
2841 * Dirs can't be linked, so move it. For moved
2842 * dirs, we always have one new and one deleted
2843 * ref. The deleted ref is ignored later.
2845 ret = send_rename(sctx, valid_path,
2849 ret = fs_path_copy(valid_path, cur->full_path);
2853 ret = send_link(sctx, cur->full_path,
2859 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2865 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2867 * Check if we can already rmdir the directory. If not,
2868 * orphanize it. For every dir item inside that gets deleted
2869 * later, we do this check again and rmdir it then if possible.
2870 * See the use of check_dirs for more details.
2872 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2876 ret = send_rmdir(sctx, valid_path);
2879 } else if (!is_orphan) {
2880 ret = orphanize_inode(sctx, sctx->cur_ino,
2881 sctx->cur_inode_gen, valid_path);
2887 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2888 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2893 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2894 !list_empty(&sctx->deleted_refs)) {
2896 * We have a moved dir. Add the old parent to check_dirs
2898 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2900 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2904 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2906 * We have a non dir inode. Go through all deleted refs and
2907 * unlink them if they were not already overwritten by other
2910 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2911 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2912 sctx->cur_ino, sctx->cur_inode_gen,
2913 cur->name, cur->name_len);
2917 ret = send_unlink(sctx, cur->full_path);
2921 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2928 * If the inode is still orphan, unlink the orphan. This may
2929 * happen when a previous inode did overwrite the first ref
2930 * of this inode and no new refs were added for the current
2931 * inode. Unlinking does not mean that the inode is deleted in
2932 * all cases. There may still be links to this inode in other
2936 ret = send_unlink(sctx, valid_path);
2943 * We did collect all parent dirs where cur_inode was once located. We
2944 * now go through all these dirs and check if they are pending for
2945 * deletion and if it's finally possible to perform the rmdir now.
2946 * We also update the inode stats of the parent dirs here.
2948 ULIST_ITER_INIT(&uit);
2949 while ((un = ulist_next(check_dirs, &uit))) {
2951 * In case we had refs into dirs that were not processed yet,
2952 * we don't need to do the utime and rmdir logic for these dirs.
2953 * The dir will be processed later.
2955 if (un->val > sctx->cur_ino)
2958 ret = get_cur_inode_state(sctx, un->val, un->aux);
2962 if (ret == inode_state_did_create ||
2963 ret == inode_state_no_change) {
2964 /* TODO delayed utimes */
2965 ret = send_utimes(sctx, un->val, un->aux);
2968 } else if (ret == inode_state_did_delete) {
2969 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
2973 ret = get_cur_path(sctx, un->val, un->aux,
2977 ret = send_rmdir(sctx, valid_path);
2987 free_recorded_refs(sctx);
2988 ulist_free(check_dirs);
2989 fs_path_free(sctx, valid_path);
2993 static int __record_new_ref(int num, u64 dir, int index,
2994 struct fs_path *name,
2998 struct send_ctx *sctx = ctx;
3002 p = fs_path_alloc(sctx);
3006 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
3011 ret = get_cur_path(sctx, dir, gen, p);
3014 ret = fs_path_add_path(p, name);
3018 ret = record_ref(&sctx->new_refs, dir, gen, p);
3022 fs_path_free(sctx, p);
3026 static int __record_deleted_ref(int num, u64 dir, int index,
3027 struct fs_path *name,
3031 struct send_ctx *sctx = ctx;
3035 p = fs_path_alloc(sctx);
3039 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3044 ret = get_cur_path(sctx, dir, gen, p);
3047 ret = fs_path_add_path(p, name);
3051 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3055 fs_path_free(sctx, p);
3059 static int record_new_ref(struct send_ctx *sctx)
3063 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3064 sctx->cmp_key, 0, __record_new_ref, sctx);
3073 static int record_deleted_ref(struct send_ctx *sctx)
3077 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3078 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3087 struct find_ref_ctx {
3089 struct fs_path *name;
3093 static int __find_iref(int num, u64 dir, int index,
3094 struct fs_path *name,
3097 struct find_ref_ctx *ctx = ctx_;
3099 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3100 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3101 ctx->found_idx = num;
3107 static int find_iref(struct send_ctx *sctx,
3108 struct btrfs_root *root,
3109 struct btrfs_path *path,
3110 struct btrfs_key *key,
3111 u64 dir, struct fs_path *name)
3114 struct find_ref_ctx ctx;
3120 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3124 if (ctx.found_idx == -1)
3127 return ctx.found_idx;
3130 static int __record_changed_new_ref(int num, u64 dir, int index,
3131 struct fs_path *name,
3135 struct send_ctx *sctx = ctx;
3137 ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
3138 sctx->cmp_key, dir, name);
3140 ret = __record_new_ref(num, dir, index, name, sctx);
3147 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3148 struct fs_path *name,
3152 struct send_ctx *sctx = ctx;
3154 ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3157 ret = __record_deleted_ref(num, dir, index, name, sctx);
3164 static int record_changed_ref(struct send_ctx *sctx)
3168 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3169 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3172 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3173 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3183 * Record and process all refs at once. Needed when an inode changes the
3184 * generation number, which means that it was deleted and recreated.
3186 static int process_all_refs(struct send_ctx *sctx,
3187 enum btrfs_compare_tree_result cmd)
3190 struct btrfs_root *root;
3191 struct btrfs_path *path;
3192 struct btrfs_key key;
3193 struct btrfs_key found_key;
3194 struct extent_buffer *eb;
3196 iterate_inode_ref_t cb;
3198 path = alloc_path_for_send();
3202 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3203 root = sctx->send_root;
3204 cb = __record_new_ref;
3205 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3206 root = sctx->parent_root;
3207 cb = __record_deleted_ref;
3212 key.objectid = sctx->cmp_key->objectid;
3213 key.type = BTRFS_INODE_REF_KEY;
3216 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3222 eb = path->nodes[0];
3223 slot = path->slots[0];
3224 btrfs_item_key_to_cpu(eb, &found_key, slot);
3226 if (found_key.objectid != key.objectid ||
3227 found_key.type != key.type)
3230 ret = iterate_inode_ref(sctx, root, path, &found_key, 0, cb,
3232 btrfs_release_path(path);
3236 key.offset = found_key.offset + 1;
3238 btrfs_release_path(path);
3240 ret = process_recorded_refs(sctx);
3243 btrfs_free_path(path);
3247 static int send_set_xattr(struct send_ctx *sctx,
3248 struct fs_path *path,
3249 const char *name, int name_len,
3250 const char *data, int data_len)
3254 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3258 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3259 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3260 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3262 ret = send_cmd(sctx);
3269 static int send_remove_xattr(struct send_ctx *sctx,
3270 struct fs_path *path,
3271 const char *name, int name_len)
3275 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3279 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3280 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3282 ret = send_cmd(sctx);
3289 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3290 const char *name, int name_len,
3291 const char *data, int data_len,
3295 struct send_ctx *sctx = ctx;
3297 posix_acl_xattr_header dummy_acl;
3299 p = fs_path_alloc(sctx);
3304 * This hack is needed because empty acl's are stored as zero byte
3305 * data in xattrs. Problem with that is, that receiving these zero byte
3306 * acl's will fail later. To fix this, we send a dummy acl list that
3307 * only contains the version number and no entries.
3309 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3310 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3311 if (data_len == 0) {
3312 dummy_acl.a_version =
3313 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3314 data = (char *)&dummy_acl;
3315 data_len = sizeof(dummy_acl);
3319 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3323 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3326 fs_path_free(sctx, p);
3330 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3331 const char *name, int name_len,
3332 const char *data, int data_len,
3336 struct send_ctx *sctx = ctx;
3339 p = fs_path_alloc(sctx);
3343 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3347 ret = send_remove_xattr(sctx, p, name, name_len);
3350 fs_path_free(sctx, p);
3354 static int process_new_xattr(struct send_ctx *sctx)
3358 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3359 sctx->cmp_key, __process_new_xattr, sctx);
3364 static int process_deleted_xattr(struct send_ctx *sctx)
3368 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3369 sctx->cmp_key, __process_deleted_xattr, sctx);
3374 struct find_xattr_ctx {
3382 static int __find_xattr(int num, struct btrfs_key *di_key,
3383 const char *name, int name_len,
3384 const char *data, int data_len,
3385 u8 type, void *vctx)
3387 struct find_xattr_ctx *ctx = vctx;
3389 if (name_len == ctx->name_len &&
3390 strncmp(name, ctx->name, name_len) == 0) {
3391 ctx->found_idx = num;
3392 ctx->found_data_len = data_len;
3393 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3394 if (!ctx->found_data)
3396 memcpy(ctx->found_data, data, data_len);
3402 static int find_xattr(struct send_ctx *sctx,
3403 struct btrfs_root *root,
3404 struct btrfs_path *path,
3405 struct btrfs_key *key,
3406 const char *name, int name_len,
3407 char **data, int *data_len)
3410 struct find_xattr_ctx ctx;
3413 ctx.name_len = name_len;
3415 ctx.found_data = NULL;
3416 ctx.found_data_len = 0;
3418 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3422 if (ctx.found_idx == -1)
3425 *data = ctx.found_data;
3426 *data_len = ctx.found_data_len;
3428 kfree(ctx.found_data);
3430 return ctx.found_idx;
3434 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3435 const char *name, int name_len,
3436 const char *data, int data_len,
3440 struct send_ctx *sctx = ctx;
3441 char *found_data = NULL;
3442 int found_data_len = 0;
3443 struct fs_path *p = NULL;
3445 ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
3446 sctx->cmp_key, name, name_len, &found_data,
3448 if (ret == -ENOENT) {
3449 ret = __process_new_xattr(num, di_key, name, name_len, data,
3450 data_len, type, ctx);
3451 } else if (ret >= 0) {
3452 if (data_len != found_data_len ||
3453 memcmp(data, found_data, data_len)) {
3454 ret = __process_new_xattr(num, di_key, name, name_len,
3455 data, data_len, type, ctx);
3462 fs_path_free(sctx, p);
3466 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3467 const char *name, int name_len,
3468 const char *data, int data_len,
3472 struct send_ctx *sctx = ctx;
3474 ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3475 name, name_len, NULL, NULL);
3477 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3478 data_len, type, ctx);
3485 static int process_changed_xattr(struct send_ctx *sctx)
3489 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3490 sctx->cmp_key, __process_changed_new_xattr, sctx);
3493 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3494 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3500 static int process_all_new_xattrs(struct send_ctx *sctx)
3503 struct btrfs_root *root;
3504 struct btrfs_path *path;
3505 struct btrfs_key key;
3506 struct btrfs_key found_key;
3507 struct extent_buffer *eb;
3510 path = alloc_path_for_send();
3514 root = sctx->send_root;
3516 key.objectid = sctx->cmp_key->objectid;
3517 key.type = BTRFS_XATTR_ITEM_KEY;
3520 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3528 eb = path->nodes[0];
3529 slot = path->slots[0];
3530 btrfs_item_key_to_cpu(eb, &found_key, slot);
3532 if (found_key.objectid != key.objectid ||
3533 found_key.type != key.type) {
3538 ret = iterate_dir_item(sctx, root, path, &found_key,
3539 __process_new_xattr, sctx);
3543 btrfs_release_path(path);
3544 key.offset = found_key.offset + 1;
3548 btrfs_free_path(path);
3553 * Read some bytes from the current inode/file and send a write command to
3556 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3560 loff_t pos = offset;
3562 mm_segment_t old_fs;
3564 p = fs_path_alloc(sctx);
3569 * vfs normally only accepts user space buffers for security reasons.
3570 * we only read from the file and also only provide the read_buf buffer
3571 * to vfs. As this buffer does not come from a user space call, it's
3572 * ok to temporary allow kernel space buffers.
3577 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3579 ret = open_cur_inode_file(sctx);
3583 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3590 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3594 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3598 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3599 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3600 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
3602 ret = send_cmd(sctx);
3606 fs_path_free(sctx, p);
3614 * Send a clone command to user space.
3616 static int send_clone(struct send_ctx *sctx,
3617 u64 offset, u32 len,
3618 struct clone_root *clone_root)
3624 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3625 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3626 clone_root->root->objectid, clone_root->ino,
3627 clone_root->offset);
3629 p = fs_path_alloc(sctx);
3633 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3637 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3641 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3642 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3643 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3645 if (clone_root->root == sctx->send_root) {
3646 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3647 &gen, NULL, NULL, NULL, NULL);
3650 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3652 ret = get_inode_path(sctx, clone_root->root,
3653 clone_root->ino, p);
3658 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3659 clone_root->root->root_item.uuid);
3660 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3661 clone_root->root->root_item.ctransid);
3662 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3663 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3664 clone_root->offset);
3666 ret = send_cmd(sctx);
3670 fs_path_free(sctx, p);
3674 static int send_write_or_clone(struct send_ctx *sctx,
3675 struct btrfs_path *path,
3676 struct btrfs_key *key,
3677 struct clone_root *clone_root)
3680 struct btrfs_file_extent_item *ei;
3681 u64 offset = key->offset;
3687 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3688 struct btrfs_file_extent_item);
3689 type = btrfs_file_extent_type(path->nodes[0], ei);
3690 if (type == BTRFS_FILE_EXTENT_INLINE) {
3691 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3693 * it is possible the inline item won't cover the whole page,
3694 * but there may be items after this page. Make
3695 * sure to send the whole thing
3697 len = PAGE_CACHE_ALIGN(len);
3699 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3702 if (offset + len > sctx->cur_inode_size)
3703 len = sctx->cur_inode_size - offset;
3712 if (l > BTRFS_SEND_READ_SIZE)
3713 l = BTRFS_SEND_READ_SIZE;
3714 ret = send_write(sctx, pos + offset, l);
3723 ret = send_clone(sctx, offset, len, clone_root);
3730 static int is_extent_unchanged(struct send_ctx *sctx,
3731 struct btrfs_path *left_path,
3732 struct btrfs_key *ekey)
3735 struct btrfs_key key;
3736 struct btrfs_path *path = NULL;
3737 struct extent_buffer *eb;
3739 struct btrfs_key found_key;
3740 struct btrfs_file_extent_item *ei;
3745 u64 left_offset_fixed;
3753 path = alloc_path_for_send();
3757 eb = left_path->nodes[0];
3758 slot = left_path->slots[0];
3759 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3760 left_type = btrfs_file_extent_type(eb, ei);
3762 if (left_type != BTRFS_FILE_EXTENT_REG) {
3766 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3767 left_len = btrfs_file_extent_num_bytes(eb, ei);
3768 left_offset = btrfs_file_extent_offset(eb, ei);
3769 left_gen = btrfs_file_extent_generation(eb, ei);
3772 * Following comments will refer to these graphics. L is the left
3773 * extents which we are checking at the moment. 1-8 are the right
3774 * extents that we iterate.
3777 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3780 * |--1--|-2b-|...(same as above)
3782 * Alternative situation. Happens on files where extents got split.
3784 * |-----------7-----------|-6-|
3786 * Alternative situation. Happens on files which got larger.
3789 * Nothing follows after 8.
3792 key.objectid = ekey->objectid;
3793 key.type = BTRFS_EXTENT_DATA_KEY;
3794 key.offset = ekey->offset;
3795 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3804 * Handle special case where the right side has no extents at all.
3806 eb = path->nodes[0];
3807 slot = path->slots[0];
3808 btrfs_item_key_to_cpu(eb, &found_key, slot);
3809 if (found_key.objectid != key.objectid ||
3810 found_key.type != key.type) {
3816 * We're now on 2a, 2b or 7.
3819 while (key.offset < ekey->offset + left_len) {
3820 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3821 right_type = btrfs_file_extent_type(eb, ei);
3822 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3823 right_len = btrfs_file_extent_num_bytes(eb, ei);
3824 right_offset = btrfs_file_extent_offset(eb, ei);
3825 right_gen = btrfs_file_extent_generation(eb, ei);
3827 if (right_type != BTRFS_FILE_EXTENT_REG) {
3833 * Are we at extent 8? If yes, we know the extent is changed.
3834 * This may only happen on the first iteration.
3836 if (found_key.offset + right_len <= ekey->offset) {
3841 left_offset_fixed = left_offset;
3842 if (key.offset < ekey->offset) {
3843 /* Fix the right offset for 2a and 7. */
3844 right_offset += ekey->offset - key.offset;
3846 /* Fix the left offset for all behind 2a and 2b */
3847 left_offset_fixed += key.offset - ekey->offset;
3851 * Check if we have the same extent.
3853 if (left_disknr != right_disknr ||
3854 left_offset_fixed != right_offset ||
3855 left_gen != right_gen) {
3861 * Go to the next extent.
3863 ret = btrfs_next_item(sctx->parent_root, path);
3867 eb = path->nodes[0];
3868 slot = path->slots[0];
3869 btrfs_item_key_to_cpu(eb, &found_key, slot);
3871 if (ret || found_key.objectid != key.objectid ||
3872 found_key.type != key.type) {
3873 key.offset += right_len;
3876 if (found_key.offset != key.offset + right_len) {
3877 /* Should really not happen */
3886 * We're now behind the left extent (treat as unchanged) or at the end
3887 * of the right side (treat as changed).
3889 if (key.offset >= ekey->offset + left_len)
3896 btrfs_free_path(path);
3900 static int process_extent(struct send_ctx *sctx,
3901 struct btrfs_path *path,
3902 struct btrfs_key *key)
3905 struct clone_root *found_clone = NULL;
3907 if (S_ISLNK(sctx->cur_inode_mode))
3910 if (sctx->parent_root && !sctx->cur_inode_new) {
3911 ret = is_extent_unchanged(sctx, path, key);
3920 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3921 sctx->cur_inode_size, &found_clone);
3922 if (ret != -ENOENT && ret < 0)
3925 ret = send_write_or_clone(sctx, path, key, found_clone);
3931 static int process_all_extents(struct send_ctx *sctx)
3934 struct btrfs_root *root;
3935 struct btrfs_path *path;
3936 struct btrfs_key key;
3937 struct btrfs_key found_key;
3938 struct extent_buffer *eb;
3941 root = sctx->send_root;
3942 path = alloc_path_for_send();
3946 key.objectid = sctx->cmp_key->objectid;
3947 key.type = BTRFS_EXTENT_DATA_KEY;
3950 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3958 eb = path->nodes[0];
3959 slot = path->slots[0];
3960 btrfs_item_key_to_cpu(eb, &found_key, slot);
3962 if (found_key.objectid != key.objectid ||
3963 found_key.type != key.type) {
3968 ret = process_extent(sctx, path, &found_key);
3972 btrfs_release_path(path);
3973 key.offset = found_key.offset + 1;
3977 btrfs_free_path(path);
3981 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
3985 if (sctx->cur_ino == 0)
3987 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
3988 sctx->cmp_key->type <= BTRFS_INODE_REF_KEY)
3990 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
3993 ret = process_recorded_refs(sctx);
3998 * We have processed the refs and thus need to advance send_progress.
3999 * Now, calls to get_cur_xxx will take the updated refs of the current
4000 * inode into account.
4002 sctx->send_progress = sctx->cur_ino + 1;
4008 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4020 ret = process_recorded_refs_if_needed(sctx, at_end);
4024 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4026 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4029 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4030 &left_mode, &left_uid, &left_gid, NULL);
4034 if (!S_ISLNK(sctx->cur_inode_mode)) {
4035 if (!sctx->parent_root || sctx->cur_inode_new) {
4039 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4040 NULL, NULL, &right_mode, &right_uid,
4045 if (left_uid != right_uid || left_gid != right_gid)
4047 if (left_mode != right_mode)
4052 if (S_ISREG(sctx->cur_inode_mode)) {
4053 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4054 sctx->cur_inode_size);
4060 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4061 left_uid, left_gid);
4066 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4073 * Need to send that every time, no matter if it actually changed
4074 * between the two trees as we have done changes to the inode before.
4076 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4084 static int changed_inode(struct send_ctx *sctx,
4085 enum btrfs_compare_tree_result result)
4088 struct btrfs_key *key = sctx->cmp_key;
4089 struct btrfs_inode_item *left_ii = NULL;
4090 struct btrfs_inode_item *right_ii = NULL;
4094 ret = close_cur_inode_file(sctx);
4098 sctx->cur_ino = key->objectid;
4099 sctx->cur_inode_new_gen = 0;
4102 * Set send_progress to current inode. This will tell all get_cur_xxx
4103 * functions that the current inode's refs are not updated yet. Later,
4104 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4106 sctx->send_progress = sctx->cur_ino;
4108 if (result == BTRFS_COMPARE_TREE_NEW ||
4109 result == BTRFS_COMPARE_TREE_CHANGED) {
4110 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4111 sctx->left_path->slots[0],
4112 struct btrfs_inode_item);
4113 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4116 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4117 sctx->right_path->slots[0],
4118 struct btrfs_inode_item);
4119 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4122 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4123 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4124 sctx->right_path->slots[0],
4125 struct btrfs_inode_item);
4127 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4131 * The cur_ino = root dir case is special here. We can't treat
4132 * the inode as deleted+reused because it would generate a
4133 * stream that tries to delete/mkdir the root dir.
4135 if (left_gen != right_gen &&
4136 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4137 sctx->cur_inode_new_gen = 1;
4140 if (result == BTRFS_COMPARE_TREE_NEW) {
4141 sctx->cur_inode_gen = left_gen;
4142 sctx->cur_inode_new = 1;
4143 sctx->cur_inode_deleted = 0;
4144 sctx->cur_inode_size = btrfs_inode_size(
4145 sctx->left_path->nodes[0], left_ii);
4146 sctx->cur_inode_mode = btrfs_inode_mode(
4147 sctx->left_path->nodes[0], left_ii);
4148 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4149 ret = send_create_inode_if_needed(sctx);
4150 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4151 sctx->cur_inode_gen = right_gen;
4152 sctx->cur_inode_new = 0;
4153 sctx->cur_inode_deleted = 1;
4154 sctx->cur_inode_size = btrfs_inode_size(
4155 sctx->right_path->nodes[0], right_ii);
4156 sctx->cur_inode_mode = btrfs_inode_mode(
4157 sctx->right_path->nodes[0], right_ii);
4158 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4160 * We need to do some special handling in case the inode was
4161 * reported as changed with a changed generation number. This
4162 * means that the original inode was deleted and new inode
4163 * reused the same inum. So we have to treat the old inode as
4164 * deleted and the new one as new.
4166 if (sctx->cur_inode_new_gen) {
4168 * First, process the inode as if it was deleted.
4170 sctx->cur_inode_gen = right_gen;
4171 sctx->cur_inode_new = 0;
4172 sctx->cur_inode_deleted = 1;
4173 sctx->cur_inode_size = btrfs_inode_size(
4174 sctx->right_path->nodes[0], right_ii);
4175 sctx->cur_inode_mode = btrfs_inode_mode(
4176 sctx->right_path->nodes[0], right_ii);
4177 ret = process_all_refs(sctx,
4178 BTRFS_COMPARE_TREE_DELETED);
4183 * Now process the inode as if it was new.
4185 sctx->cur_inode_gen = left_gen;
4186 sctx->cur_inode_new = 1;
4187 sctx->cur_inode_deleted = 0;
4188 sctx->cur_inode_size = btrfs_inode_size(
4189 sctx->left_path->nodes[0], left_ii);
4190 sctx->cur_inode_mode = btrfs_inode_mode(
4191 sctx->left_path->nodes[0], left_ii);
4192 ret = send_create_inode_if_needed(sctx);
4196 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4200 * Advance send_progress now as we did not get into
4201 * process_recorded_refs_if_needed in the new_gen case.
4203 sctx->send_progress = sctx->cur_ino + 1;
4206 * Now process all extents and xattrs of the inode as if
4207 * they were all new.
4209 ret = process_all_extents(sctx);
4212 ret = process_all_new_xattrs(sctx);
4216 sctx->cur_inode_gen = left_gen;
4217 sctx->cur_inode_new = 0;
4218 sctx->cur_inode_new_gen = 0;
4219 sctx->cur_inode_deleted = 0;
4220 sctx->cur_inode_size = btrfs_inode_size(
4221 sctx->left_path->nodes[0], left_ii);
4222 sctx->cur_inode_mode = btrfs_inode_mode(
4223 sctx->left_path->nodes[0], left_ii);
4232 * We have to process new refs before deleted refs, but compare_trees gives us
4233 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4234 * first and later process them in process_recorded_refs.
4235 * For the cur_inode_new_gen case, we skip recording completely because
4236 * changed_inode did already initiate processing of refs. The reason for this is
4237 * that in this case, compare_tree actually compares the refs of 2 different
4238 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4239 * refs of the right tree as deleted and all refs of the left tree as new.
4241 static int changed_ref(struct send_ctx *sctx,
4242 enum btrfs_compare_tree_result result)
4246 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4248 if (!sctx->cur_inode_new_gen &&
4249 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4250 if (result == BTRFS_COMPARE_TREE_NEW)
4251 ret = record_new_ref(sctx);
4252 else if (result == BTRFS_COMPARE_TREE_DELETED)
4253 ret = record_deleted_ref(sctx);
4254 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4255 ret = record_changed_ref(sctx);
4262 * Process new/deleted/changed xattrs. We skip processing in the
4263 * cur_inode_new_gen case because changed_inode did already initiate processing
4264 * of xattrs. The reason is the same as in changed_ref
4266 static int changed_xattr(struct send_ctx *sctx,
4267 enum btrfs_compare_tree_result result)
4271 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4273 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4274 if (result == BTRFS_COMPARE_TREE_NEW)
4275 ret = process_new_xattr(sctx);
4276 else if (result == BTRFS_COMPARE_TREE_DELETED)
4277 ret = process_deleted_xattr(sctx);
4278 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4279 ret = process_changed_xattr(sctx);
4286 * Process new/deleted/changed extents. We skip processing in the
4287 * cur_inode_new_gen case because changed_inode did already initiate processing
4288 * of extents. The reason is the same as in changed_ref
4290 static int changed_extent(struct send_ctx *sctx,
4291 enum btrfs_compare_tree_result result)
4295 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4297 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4298 if (result != BTRFS_COMPARE_TREE_DELETED)
4299 ret = process_extent(sctx, sctx->left_path,
4307 * Updates compare related fields in sctx and simply forwards to the actual
4308 * changed_xxx functions.
4310 static int changed_cb(struct btrfs_root *left_root,
4311 struct btrfs_root *right_root,
4312 struct btrfs_path *left_path,
4313 struct btrfs_path *right_path,
4314 struct btrfs_key *key,
4315 enum btrfs_compare_tree_result result,
4319 struct send_ctx *sctx = ctx;
4321 sctx->left_path = left_path;
4322 sctx->right_path = right_path;
4323 sctx->cmp_key = key;
4325 ret = finish_inode_if_needed(sctx, 0);
4329 /* Ignore non-FS objects */
4330 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4331 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4334 if (key->type == BTRFS_INODE_ITEM_KEY)
4335 ret = changed_inode(sctx, result);
4336 else if (key->type == BTRFS_INODE_REF_KEY)
4337 ret = changed_ref(sctx, result);
4338 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4339 ret = changed_xattr(sctx, result);
4340 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4341 ret = changed_extent(sctx, result);
4347 static int full_send_tree(struct send_ctx *sctx)
4350 struct btrfs_trans_handle *trans = NULL;
4351 struct btrfs_root *send_root = sctx->send_root;
4352 struct btrfs_key key;
4353 struct btrfs_key found_key;
4354 struct btrfs_path *path;
4355 struct extent_buffer *eb;
4360 path = alloc_path_for_send();
4364 spin_lock(&send_root->root_times_lock);
4365 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4366 spin_unlock(&send_root->root_times_lock);
4368 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4369 key.type = BTRFS_INODE_ITEM_KEY;
4374 * We need to make sure the transaction does not get committed
4375 * while we do anything on commit roots. Join a transaction to prevent
4378 trans = btrfs_join_transaction(send_root);
4379 if (IS_ERR(trans)) {
4380 ret = PTR_ERR(trans);
4386 * Make sure the tree has not changed after re-joining. We detect this
4387 * by comparing start_ctransid and ctransid. They should always match.
4389 spin_lock(&send_root->root_times_lock);
4390 ctransid = btrfs_root_ctransid(&send_root->root_item);
4391 spin_unlock(&send_root->root_times_lock);
4393 if (ctransid != start_ctransid) {
4394 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4395 "send was modified in between. This is "
4396 "probably a bug.\n");
4401 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4409 * When someone want to commit while we iterate, end the
4410 * joined transaction and rejoin.
4412 if (btrfs_should_end_transaction(trans, send_root)) {
4413 ret = btrfs_end_transaction(trans, send_root);
4417 btrfs_release_path(path);
4421 eb = path->nodes[0];
4422 slot = path->slots[0];
4423 btrfs_item_key_to_cpu(eb, &found_key, slot);
4425 ret = changed_cb(send_root, NULL, path, NULL,
4426 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4430 key.objectid = found_key.objectid;
4431 key.type = found_key.type;
4432 key.offset = found_key.offset + 1;
4434 ret = btrfs_next_item(send_root, path);
4444 ret = finish_inode_if_needed(sctx, 1);
4447 btrfs_free_path(path);
4450 ret = btrfs_end_transaction(trans, send_root);
4452 btrfs_end_transaction(trans, send_root);
4457 static int send_subvol(struct send_ctx *sctx)
4461 ret = send_header(sctx);
4465 ret = send_subvol_begin(sctx);
4469 if (sctx->parent_root) {
4470 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4474 ret = finish_inode_if_needed(sctx, 1);
4478 ret = full_send_tree(sctx);
4485 ret = close_cur_inode_file(sctx);
4487 close_cur_inode_file(sctx);
4489 free_recorded_refs(sctx);
4493 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4496 struct btrfs_root *send_root;
4497 struct btrfs_root *clone_root;
4498 struct btrfs_fs_info *fs_info;
4499 struct btrfs_ioctl_send_args *arg = NULL;
4500 struct btrfs_key key;
4501 struct file *filp = NULL;
4502 struct send_ctx *sctx = NULL;
4504 u64 *clone_sources_tmp = NULL;
4506 if (!capable(CAP_SYS_ADMIN))
4509 send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root;
4510 fs_info = send_root->fs_info;
4512 arg = memdup_user(arg_, sizeof(*arg));
4519 if (!access_ok(VERIFY_READ, arg->clone_sources,
4520 sizeof(*arg->clone_sources *
4521 arg->clone_sources_count))) {
4526 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4532 INIT_LIST_HEAD(&sctx->new_refs);
4533 INIT_LIST_HEAD(&sctx->deleted_refs);
4534 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4535 INIT_LIST_HEAD(&sctx->name_cache_list);
4537 sctx->send_filp = fget(arg->send_fd);
4538 if (IS_ERR(sctx->send_filp)) {
4539 ret = PTR_ERR(sctx->send_filp);
4543 sctx->mnt = mnt_file->f_path.mnt;
4545 sctx->send_root = send_root;
4546 sctx->clone_roots_cnt = arg->clone_sources_count;
4548 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4549 sctx->send_buf = vmalloc(sctx->send_max_size);
4550 if (!sctx->send_buf) {
4555 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4556 if (!sctx->read_buf) {
4561 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4562 (arg->clone_sources_count + 1));
4563 if (!sctx->clone_roots) {
4568 if (arg->clone_sources_count) {
4569 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4570 sizeof(*arg->clone_sources));
4571 if (!clone_sources_tmp) {
4576 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4577 arg->clone_sources_count *
4578 sizeof(*arg->clone_sources));
4584 for (i = 0; i < arg->clone_sources_count; i++) {
4585 key.objectid = clone_sources_tmp[i];
4586 key.type = BTRFS_ROOT_ITEM_KEY;
4587 key.offset = (u64)-1;
4588 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4593 if (IS_ERR(clone_root)) {
4594 ret = PTR_ERR(clone_root);
4597 sctx->clone_roots[i].root = clone_root;
4599 vfree(clone_sources_tmp);
4600 clone_sources_tmp = NULL;
4603 if (arg->parent_root) {
4604 key.objectid = arg->parent_root;
4605 key.type = BTRFS_ROOT_ITEM_KEY;
4606 key.offset = (u64)-1;
4607 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4608 if (!sctx->parent_root) {
4615 * Clones from send_root are allowed, but only if the clone source
4616 * is behind the current send position. This is checked while searching
4617 * for possible clone sources.
4619 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4621 /* We do a bsearch later */
4622 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4623 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4626 ret = send_subvol(sctx);
4630 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4633 ret = send_cmd(sctx);
4641 vfree(clone_sources_tmp);
4644 if (sctx->send_filp)
4645 fput(sctx->send_filp);
4647 vfree(sctx->clone_roots);
4648 vfree(sctx->send_buf);
4649 vfree(sctx->read_buf);
4651 name_cache_free(sctx);