2 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/bsearch.h>
21 #include <linux/file.h>
22 #include <linux/sort.h>
23 #include <linux/mount.h>
24 #include <linux/xattr.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/radix-tree.h>
27 #include <linux/crc32c.h>
28 #include <linux/vmalloc.h>
34 #include "btrfs_inode.h"
35 #include "transaction.h"
37 static int g_verbose = 0;
39 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
42 * A fs_path is a helper to dynamically build path names with unknown size.
43 * It reallocates the internal buffer on demand.
44 * It allows fast adding of path elements on the right side (normal path) and
45 * fast adding to the left side (reversed path). A reversed path can also be
46 * unreversed if needed.
64 #define FS_PATH_INLINE_SIZE \
65 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
68 /* reused for each extent */
70 struct btrfs_root *root;
77 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
78 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
81 struct file *send_filp;
87 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
91 struct btrfs_root *send_root;
92 struct btrfs_root *parent_root;
93 struct clone_root *clone_roots;
96 /* current state of the compare_tree call */
97 struct btrfs_path *left_path;
98 struct btrfs_path *right_path;
99 struct btrfs_key *cmp_key;
102 * infos of the currently processed inode. In case of deleted inodes,
103 * these are the values from the deleted inode.
108 int cur_inode_new_gen;
109 int cur_inode_deleted;
115 struct list_head new_refs;
116 struct list_head deleted_refs;
118 struct radix_tree_root name_cache;
119 struct list_head name_cache_list;
122 struct file *cur_inode_filp;
126 struct name_cache_entry {
127 struct list_head list;
129 * radix_tree has only 32bit entries but we need to handle 64bit inums.
130 * We use the lower 32bit of the 64bit inum to store it in the tree. If
131 * more then one inum would fall into the same entry, we use radix_list
132 * to store the additional entries. radix_list is also used to store
133 * entries where two entries have the same inum but different
136 struct list_head radix_list;
142 int need_later_update;
147 static void fs_path_reset(struct fs_path *p)
150 p->start = p->buf + p->buf_len - 1;
160 static struct fs_path *fs_path_alloc(struct send_ctx *sctx)
164 p = kmalloc(sizeof(*p), GFP_NOFS);
169 p->buf = p->inline_buf;
170 p->buf_len = FS_PATH_INLINE_SIZE;
175 static struct fs_path *fs_path_alloc_reversed(struct send_ctx *sctx)
179 p = fs_path_alloc(sctx);
187 static void fs_path_free(struct send_ctx *sctx, struct fs_path *p)
191 if (p->buf != p->inline_buf) {
200 static int fs_path_len(struct fs_path *p)
202 return p->end - p->start;
205 static int fs_path_ensure_buf(struct fs_path *p, int len)
213 if (p->buf_len >= len)
216 path_len = p->end - p->start;
217 old_buf_len = p->buf_len;
218 len = PAGE_ALIGN(len);
220 if (p->buf == p->inline_buf) {
221 tmp_buf = kmalloc(len, GFP_NOFS);
223 tmp_buf = vmalloc(len);
228 memcpy(tmp_buf, p->buf, p->buf_len);
232 if (p->virtual_mem) {
233 tmp_buf = vmalloc(len);
236 memcpy(tmp_buf, p->buf, p->buf_len);
239 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
241 tmp_buf = vmalloc(len);
244 memcpy(tmp_buf, p->buf, p->buf_len);
253 tmp_buf = p->buf + old_buf_len - path_len - 1;
254 p->end = p->buf + p->buf_len - 1;
255 p->start = p->end - path_len;
256 memmove(p->start, tmp_buf, path_len + 1);
259 p->end = p->start + path_len;
264 static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
269 new_len = p->end - p->start + name_len;
270 if (p->start != p->end)
272 ret = fs_path_ensure_buf(p, new_len);
277 if (p->start != p->end)
279 p->start -= name_len;
280 p->prepared = p->start;
282 if (p->start != p->end)
284 p->prepared = p->end;
293 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
297 ret = fs_path_prepare_for_add(p, name_len);
300 memcpy(p->prepared, name, name_len);
307 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
311 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
314 memcpy(p->prepared, p2->start, p2->end - p2->start);
321 static int fs_path_add_from_extent_buffer(struct fs_path *p,
322 struct extent_buffer *eb,
323 unsigned long off, int len)
327 ret = fs_path_prepare_for_add(p, len);
331 read_extent_buffer(eb, p->prepared, off, len);
339 static void fs_path_remove(struct fs_path *p)
342 while (p->start != p->end && *p->end != '/')
348 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
352 p->reversed = from->reversed;
355 ret = fs_path_add_path(p, from);
361 static void fs_path_unreverse(struct fs_path *p)
370 len = p->end - p->start;
372 p->end = p->start + len;
373 memmove(p->start, tmp, len + 1);
377 static struct btrfs_path *alloc_path_for_send(void)
379 struct btrfs_path *path;
381 path = btrfs_alloc_path();
384 path->search_commit_root = 1;
385 path->skip_locking = 1;
389 int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
399 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
400 /* TODO handle that correctly */
401 /*if (ret == -ERESTARTSYS) {
420 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
422 struct btrfs_tlv_header *hdr;
423 int total_len = sizeof(*hdr) + len;
424 int left = sctx->send_max_size - sctx->send_size;
426 if (unlikely(left < total_len))
429 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
430 hdr->tlv_type = cpu_to_le16(attr);
431 hdr->tlv_len = cpu_to_le16(len);
432 memcpy(hdr + 1, data, len);
433 sctx->send_size += total_len;
439 static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
441 return tlv_put(sctx, attr, &value, sizeof(value));
444 static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
446 __le16 tmp = cpu_to_le16(value);
447 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
450 static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
452 __le32 tmp = cpu_to_le32(value);
453 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
457 static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
459 __le64 tmp = cpu_to_le64(value);
460 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
463 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
464 const char *str, int len)
468 return tlv_put(sctx, attr, str, len);
471 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
474 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
478 static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
481 struct btrfs_timespec bts;
482 bts.sec = cpu_to_le64(ts->tv_sec);
483 bts.nsec = cpu_to_le32(ts->tv_nsec);
484 return tlv_put(sctx, attr, &bts, sizeof(bts));
488 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
489 struct extent_buffer *eb,
490 struct btrfs_timespec *ts)
492 struct btrfs_timespec bts;
493 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
494 return tlv_put(sctx, attr, &bts, sizeof(bts));
498 #define TLV_PUT(sctx, attrtype, attrlen, data) \
500 ret = tlv_put(sctx, attrtype, attrlen, data); \
502 goto tlv_put_failure; \
505 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
507 ret = tlv_put_u##bits(sctx, attrtype, value); \
509 goto tlv_put_failure; \
512 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
513 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
514 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
515 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
516 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
518 ret = tlv_put_string(sctx, attrtype, str, len); \
520 goto tlv_put_failure; \
522 #define TLV_PUT_PATH(sctx, attrtype, p) \
524 ret = tlv_put_string(sctx, attrtype, p->start, \
525 p->end - p->start); \
527 goto tlv_put_failure; \
529 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
531 ret = tlv_put_uuid(sctx, attrtype, uuid); \
533 goto tlv_put_failure; \
535 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
537 ret = tlv_put_timespec(sctx, attrtype, ts); \
539 goto tlv_put_failure; \
541 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
543 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
545 goto tlv_put_failure; \
548 static int send_header(struct send_ctx *sctx)
550 struct btrfs_stream_header hdr;
552 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
553 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
555 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
560 * For each command/item we want to send to userspace, we call this function.
562 static int begin_cmd(struct send_ctx *sctx, int cmd)
564 struct btrfs_cmd_header *hdr;
566 if (!sctx->send_buf) {
571 BUG_ON(sctx->send_size);
573 sctx->send_size += sizeof(*hdr);
574 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
575 hdr->cmd = cpu_to_le16(cmd);
580 static int send_cmd(struct send_ctx *sctx)
583 struct btrfs_cmd_header *hdr;
586 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
587 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
590 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
591 hdr->crc = cpu_to_le32(crc);
593 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
596 sctx->total_send_size += sctx->send_size;
597 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
604 * Sends a move instruction to user space
606 static int send_rename(struct send_ctx *sctx,
607 struct fs_path *from, struct fs_path *to)
611 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
613 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
617 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
618 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
620 ret = send_cmd(sctx);
628 * Sends a link instruction to user space
630 static int send_link(struct send_ctx *sctx,
631 struct fs_path *path, struct fs_path *lnk)
635 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
637 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
641 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
642 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
644 ret = send_cmd(sctx);
652 * Sends an unlink instruction to user space
654 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
658 verbose_printk("btrfs: send_unlink %s\n", path->start);
660 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
664 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
666 ret = send_cmd(sctx);
674 * Sends a rmdir instruction to user space
676 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
680 verbose_printk("btrfs: send_rmdir %s\n", path->start);
682 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
686 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
688 ret = send_cmd(sctx);
696 * Helper function to retrieve some fields from an inode item.
698 static int get_inode_info(struct btrfs_root *root,
699 u64 ino, u64 *size, u64 *gen,
700 u64 *mode, u64 *uid, u64 *gid,
704 struct btrfs_inode_item *ii;
705 struct btrfs_key key;
706 struct btrfs_path *path;
708 path = alloc_path_for_send();
713 key.type = BTRFS_INODE_ITEM_KEY;
715 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
723 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
724 struct btrfs_inode_item);
726 *size = btrfs_inode_size(path->nodes[0], ii);
728 *gen = btrfs_inode_generation(path->nodes[0], ii);
730 *mode = btrfs_inode_mode(path->nodes[0], ii);
732 *uid = btrfs_inode_uid(path->nodes[0], ii);
734 *gid = btrfs_inode_gid(path->nodes[0], ii);
736 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
739 btrfs_free_path(path);
743 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
748 * Helper function to iterate the entries in ONE btrfs_inode_ref or
749 * btrfs_inode_extref.
750 * The iterate callback may return a non zero value to stop iteration. This can
751 * be a negative value for error codes or 1 to simply stop it.
753 * path must point to the INODE_REF or INODE_EXTREF when called.
755 static int iterate_inode_ref(struct send_ctx *sctx,
756 struct btrfs_root *root, struct btrfs_path *path,
757 struct btrfs_key *found_key, int resolve,
758 iterate_inode_ref_t iterate, void *ctx)
760 struct extent_buffer *eb = path->nodes[0];
761 struct btrfs_item *item;
762 struct btrfs_inode_ref *iref;
763 struct btrfs_inode_extref *extref;
764 struct btrfs_path *tmp_path;
768 int slot = path->slots[0];
775 unsigned long name_off;
776 unsigned long elem_size;
779 p = fs_path_alloc_reversed(sctx);
783 tmp_path = alloc_path_for_send();
785 fs_path_free(sctx, p);
790 if (found_key->type == BTRFS_INODE_REF_KEY) {
791 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
792 struct btrfs_inode_ref);
793 item = btrfs_item_nr(eb, slot);
794 total = btrfs_item_size(eb, item);
795 elem_size = sizeof(*iref);
797 ptr = btrfs_item_ptr_offset(eb, slot);
798 total = btrfs_item_size_nr(eb, slot);
799 elem_size = sizeof(*extref);
802 while (cur < total) {
805 if (found_key->type == BTRFS_INODE_REF_KEY) {
806 iref = (struct btrfs_inode_ref *)(ptr + cur);
807 name_len = btrfs_inode_ref_name_len(eb, iref);
808 name_off = (unsigned long)(iref + 1);
809 index = btrfs_inode_ref_index(eb, iref);
810 dir = found_key->offset;
812 extref = (struct btrfs_inode_extref *)(ptr + cur);
813 name_len = btrfs_inode_extref_name_len(eb, extref);
814 name_off = (unsigned long)&extref->name;
815 index = btrfs_inode_extref_index(eb, extref);
816 dir = btrfs_inode_extref_parent(eb, extref);
820 start = btrfs_ref_to_path(root, tmp_path, name_len,
824 ret = PTR_ERR(start);
827 if (start < p->buf) {
828 /* overflow , try again with larger buffer */
829 ret = fs_path_ensure_buf(p,
830 p->buf_len + p->buf - start);
833 start = btrfs_ref_to_path(root, tmp_path,
838 ret = PTR_ERR(start);
841 BUG_ON(start < p->buf);
845 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
851 cur += elem_size + name_len;
852 ret = iterate(num, dir, index, p, ctx);
859 btrfs_free_path(tmp_path);
860 fs_path_free(sctx, p);
864 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
865 const char *name, int name_len,
866 const char *data, int data_len,
870 * Helper function to iterate the entries in ONE btrfs_dir_item.
871 * The iterate callback may return a non zero value to stop iteration. This can
872 * be a negative value for error codes or 1 to simply stop it.
874 * path must point to the dir item when called.
876 static int iterate_dir_item(struct send_ctx *sctx,
877 struct btrfs_root *root, struct btrfs_path *path,
878 struct btrfs_key *found_key,
879 iterate_dir_item_t iterate, void *ctx)
882 struct extent_buffer *eb;
883 struct btrfs_item *item;
884 struct btrfs_dir_item *di;
885 struct btrfs_key di_key;
900 buf = kmalloc(buf_len, GFP_NOFS);
907 slot = path->slots[0];
908 item = btrfs_item_nr(eb, slot);
909 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
912 total = btrfs_item_size(eb, item);
915 while (cur < total) {
916 name_len = btrfs_dir_name_len(eb, di);
917 data_len = btrfs_dir_data_len(eb, di);
918 type = btrfs_dir_type(eb, di);
919 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
921 if (name_len + data_len > buf_len) {
922 buf_len = PAGE_ALIGN(name_len + data_len);
924 buf2 = vmalloc(buf_len);
931 buf2 = krealloc(buf, buf_len, GFP_NOFS);
933 buf2 = vmalloc(buf_len);
947 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
948 name_len + data_len);
950 len = sizeof(*di) + name_len + data_len;
951 di = (struct btrfs_dir_item *)((char *)di + len);
954 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
955 data_len, type, ctx);
974 static int __copy_first_ref(int num, u64 dir, int index,
975 struct fs_path *p, void *ctx)
978 struct fs_path *pt = ctx;
980 ret = fs_path_copy(pt, p);
984 /* we want the first only */
989 * Retrieve the first path of an inode. If an inode has more then one
990 * ref/hardlink, this is ignored.
992 static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root,
993 u64 ino, struct fs_path *path)
996 struct btrfs_key key, found_key;
997 struct btrfs_path *p;
999 p = alloc_path_for_send();
1003 fs_path_reset(path);
1006 key.type = BTRFS_INODE_REF_KEY;
1009 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1016 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1017 if (found_key.objectid != ino ||
1018 (found_key.type != BTRFS_INODE_REF_KEY &&
1019 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1024 ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
1025 __copy_first_ref, path);
1035 struct backref_ctx {
1036 struct send_ctx *sctx;
1038 /* number of total found references */
1042 * used for clones found in send_root. clones found behind cur_objectid
1043 * and cur_offset are not considered as allowed clones.
1048 /* may be truncated in case it's the last extent in a file */
1051 /* Just to check for bugs in backref resolving */
1055 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1057 u64 root = (u64)(uintptr_t)key;
1058 struct clone_root *cr = (struct clone_root *)elt;
1060 if (root < cr->root->objectid)
1062 if (root > cr->root->objectid)
1067 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1069 struct clone_root *cr1 = (struct clone_root *)e1;
1070 struct clone_root *cr2 = (struct clone_root *)e2;
1072 if (cr1->root->objectid < cr2->root->objectid)
1074 if (cr1->root->objectid > cr2->root->objectid)
1080 * Called for every backref that is found for the current extent.
1081 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1083 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1085 struct backref_ctx *bctx = ctx_;
1086 struct clone_root *found;
1090 /* First check if the root is in the list of accepted clone sources */
1091 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1092 bctx->sctx->clone_roots_cnt,
1093 sizeof(struct clone_root),
1094 __clone_root_cmp_bsearch);
1098 if (found->root == bctx->sctx->send_root &&
1099 ino == bctx->cur_objectid &&
1100 offset == bctx->cur_offset) {
1101 bctx->found_itself = 1;
1105 * There are inodes that have extents that lie behind its i_size. Don't
1106 * accept clones from these extents.
1108 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1113 if (offset + bctx->extent_len > i_size)
1117 * Make sure we don't consider clones from send_root that are
1118 * behind the current inode/offset.
1120 if (found->root == bctx->sctx->send_root) {
1122 * TODO for the moment we don't accept clones from the inode
1123 * that is currently send. We may change this when
1124 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1127 if (ino >= bctx->cur_objectid)
1130 if (ino > bctx->cur_objectid)
1132 if (offset + bctx->extent_len > bctx->cur_offset)
1138 found->found_refs++;
1139 if (ino < found->ino) {
1141 found->offset = offset;
1142 } else if (found->ino == ino) {
1144 * same extent found more then once in the same file.
1146 if (found->offset > offset + bctx->extent_len)
1147 found->offset = offset;
1154 * Given an inode, offset and extent item, it finds a good clone for a clone
1155 * instruction. Returns -ENOENT when none could be found. The function makes
1156 * sure that the returned clone is usable at the point where sending is at the
1157 * moment. This means, that no clones are accepted which lie behind the current
1160 * path must point to the extent item when called.
1162 static int find_extent_clone(struct send_ctx *sctx,
1163 struct btrfs_path *path,
1164 u64 ino, u64 data_offset,
1166 struct clone_root **found)
1173 u64 extent_item_pos;
1175 struct btrfs_file_extent_item *fi;
1176 struct extent_buffer *eb = path->nodes[0];
1177 struct backref_ctx *backref_ctx = NULL;
1178 struct clone_root *cur_clone_root;
1179 struct btrfs_key found_key;
1180 struct btrfs_path *tmp_path;
1184 tmp_path = alloc_path_for_send();
1188 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1194 if (data_offset >= ino_size) {
1196 * There may be extents that lie behind the file's size.
1197 * I at least had this in combination with snapshotting while
1198 * writing large files.
1204 fi = btrfs_item_ptr(eb, path->slots[0],
1205 struct btrfs_file_extent_item);
1206 extent_type = btrfs_file_extent_type(eb, fi);
1207 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1211 compressed = btrfs_file_extent_compression(eb, fi);
1213 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1214 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1215 if (disk_byte == 0) {
1219 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1221 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1222 &found_key, &flags);
1223 btrfs_release_path(tmp_path);
1227 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1233 * Setup the clone roots.
1235 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1236 cur_clone_root = sctx->clone_roots + i;
1237 cur_clone_root->ino = (u64)-1;
1238 cur_clone_root->offset = 0;
1239 cur_clone_root->found_refs = 0;
1242 backref_ctx->sctx = sctx;
1243 backref_ctx->found = 0;
1244 backref_ctx->cur_objectid = ino;
1245 backref_ctx->cur_offset = data_offset;
1246 backref_ctx->found_itself = 0;
1247 backref_ctx->extent_len = num_bytes;
1250 * The last extent of a file may be too large due to page alignment.
1251 * We need to adjust extent_len in this case so that the checks in
1252 * __iterate_backrefs work.
1254 if (data_offset + num_bytes >= ino_size)
1255 backref_ctx->extent_len = ino_size - data_offset;
1258 * Now collect all backrefs.
1260 if (compressed == BTRFS_COMPRESS_NONE)
1261 extent_item_pos = logical - found_key.objectid;
1263 extent_item_pos = 0;
1265 extent_item_pos = logical - found_key.objectid;
1266 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1267 found_key.objectid, extent_item_pos, 1,
1268 __iterate_backrefs, backref_ctx);
1273 if (!backref_ctx->found_itself) {
1274 /* found a bug in backref code? */
1276 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1277 "send_root. inode=%llu, offset=%llu, "
1278 "disk_byte=%llu found extent=%llu\n",
1279 ino, data_offset, disk_byte, found_key.objectid);
1283 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1285 "num_bytes=%llu, logical=%llu\n",
1286 data_offset, ino, num_bytes, logical);
1288 if (!backref_ctx->found)
1289 verbose_printk("btrfs: no clones found\n");
1291 cur_clone_root = NULL;
1292 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1293 if (sctx->clone_roots[i].found_refs) {
1294 if (!cur_clone_root)
1295 cur_clone_root = sctx->clone_roots + i;
1296 else if (sctx->clone_roots[i].root == sctx->send_root)
1297 /* prefer clones from send_root over others */
1298 cur_clone_root = sctx->clone_roots + i;
1303 if (cur_clone_root) {
1304 *found = cur_clone_root;
1311 btrfs_free_path(tmp_path);
1316 static int read_symlink(struct send_ctx *sctx,
1317 struct btrfs_root *root,
1319 struct fs_path *dest)
1322 struct btrfs_path *path;
1323 struct btrfs_key key;
1324 struct btrfs_file_extent_item *ei;
1330 path = alloc_path_for_send();
1335 key.type = BTRFS_EXTENT_DATA_KEY;
1337 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1342 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1343 struct btrfs_file_extent_item);
1344 type = btrfs_file_extent_type(path->nodes[0], ei);
1345 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1346 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1347 BUG_ON(compression);
1349 off = btrfs_file_extent_inline_start(ei);
1350 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1352 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1355 btrfs_free_path(path);
1360 * Helper function to generate a file name that is unique in the root of
1361 * send_root and parent_root. This is used to generate names for orphan inodes.
1363 static int gen_unique_name(struct send_ctx *sctx,
1365 struct fs_path *dest)
1368 struct btrfs_path *path;
1369 struct btrfs_dir_item *di;
1374 path = alloc_path_for_send();
1379 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1381 if (len >= sizeof(tmp)) {
1382 /* should really not happen */
1387 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1388 path, BTRFS_FIRST_FREE_OBJECTID,
1389 tmp, strlen(tmp), 0);
1390 btrfs_release_path(path);
1396 /* not unique, try again */
1401 if (!sctx->parent_root) {
1407 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1408 path, BTRFS_FIRST_FREE_OBJECTID,
1409 tmp, strlen(tmp), 0);
1410 btrfs_release_path(path);
1416 /* not unique, try again */
1424 ret = fs_path_add(dest, tmp, strlen(tmp));
1427 btrfs_free_path(path);
1432 inode_state_no_change,
1433 inode_state_will_create,
1434 inode_state_did_create,
1435 inode_state_will_delete,
1436 inode_state_did_delete,
1439 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1447 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1449 if (ret < 0 && ret != -ENOENT)
1453 if (!sctx->parent_root) {
1454 right_ret = -ENOENT;
1456 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1457 NULL, NULL, NULL, NULL);
1458 if (ret < 0 && ret != -ENOENT)
1463 if (!left_ret && !right_ret) {
1464 if (left_gen == gen && right_gen == gen) {
1465 ret = inode_state_no_change;
1466 } else if (left_gen == gen) {
1467 if (ino < sctx->send_progress)
1468 ret = inode_state_did_create;
1470 ret = inode_state_will_create;
1471 } else if (right_gen == gen) {
1472 if (ino < sctx->send_progress)
1473 ret = inode_state_did_delete;
1475 ret = inode_state_will_delete;
1479 } else if (!left_ret) {
1480 if (left_gen == gen) {
1481 if (ino < sctx->send_progress)
1482 ret = inode_state_did_create;
1484 ret = inode_state_will_create;
1488 } else if (!right_ret) {
1489 if (right_gen == gen) {
1490 if (ino < sctx->send_progress)
1491 ret = inode_state_did_delete;
1493 ret = inode_state_will_delete;
1505 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1509 ret = get_cur_inode_state(sctx, ino, gen);
1513 if (ret == inode_state_no_change ||
1514 ret == inode_state_did_create ||
1515 ret == inode_state_will_delete)
1525 * Helper function to lookup a dir item in a dir.
1527 static int lookup_dir_item_inode(struct btrfs_root *root,
1528 u64 dir, const char *name, int name_len,
1533 struct btrfs_dir_item *di;
1534 struct btrfs_key key;
1535 struct btrfs_path *path;
1537 path = alloc_path_for_send();
1541 di = btrfs_lookup_dir_item(NULL, root, path,
1542 dir, name, name_len, 0);
1551 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1552 *found_inode = key.objectid;
1553 *found_type = btrfs_dir_type(path->nodes[0], di);
1556 btrfs_free_path(path);
1561 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1562 * generation of the parent dir and the name of the dir entry.
1564 static int get_first_ref(struct send_ctx *sctx,
1565 struct btrfs_root *root, u64 ino,
1566 u64 *dir, u64 *dir_gen, struct fs_path *name)
1569 struct btrfs_key key;
1570 struct btrfs_key found_key;
1571 struct btrfs_path *path;
1575 path = alloc_path_for_send();
1580 key.type = BTRFS_INODE_REF_KEY;
1583 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1587 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1589 if (ret || found_key.objectid != ino ||
1590 (found_key.type != BTRFS_INODE_REF_KEY &&
1591 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1596 if (key.type == BTRFS_INODE_REF_KEY) {
1597 struct btrfs_inode_ref *iref;
1598 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1599 struct btrfs_inode_ref);
1600 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1601 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1602 (unsigned long)(iref + 1),
1604 parent_dir = found_key.offset;
1606 struct btrfs_inode_extref *extref;
1607 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1608 struct btrfs_inode_extref);
1609 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1610 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1611 (unsigned long)&extref->name, len);
1612 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1616 btrfs_release_path(path);
1618 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
1626 btrfs_free_path(path);
1630 static int is_first_ref(struct send_ctx *sctx,
1631 struct btrfs_root *root,
1633 const char *name, int name_len)
1636 struct fs_path *tmp_name;
1640 tmp_name = fs_path_alloc(sctx);
1644 ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1648 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1653 ret = !memcmp(tmp_name->start, name, name_len);
1656 fs_path_free(sctx, tmp_name);
1661 * Used by process_recorded_refs to determine if a new ref would overwrite an
1662 * already existing ref. In case it detects an overwrite, it returns the
1663 * inode/gen in who_ino/who_gen.
1664 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1665 * to make sure later references to the overwritten inode are possible.
1666 * Orphanizing is however only required for the first ref of an inode.
1667 * process_recorded_refs does an additional is_first_ref check to see if
1668 * orphanizing is really required.
1670 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1671 const char *name, int name_len,
1672 u64 *who_ino, u64 *who_gen)
1675 u64 other_inode = 0;
1678 if (!sctx->parent_root)
1681 ret = is_inode_existent(sctx, dir, dir_gen);
1685 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1686 &other_inode, &other_type);
1687 if (ret < 0 && ret != -ENOENT)
1695 * Check if the overwritten ref was already processed. If yes, the ref
1696 * was already unlinked/moved, so we can safely assume that we will not
1697 * overwrite anything at this point in time.
1699 if (other_inode > sctx->send_progress) {
1700 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1701 who_gen, NULL, NULL, NULL, NULL);
1706 *who_ino = other_inode;
1716 * Checks if the ref was overwritten by an already processed inode. This is
1717 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1718 * thus the orphan name needs be used.
1719 * process_recorded_refs also uses it to avoid unlinking of refs that were
1722 static int did_overwrite_ref(struct send_ctx *sctx,
1723 u64 dir, u64 dir_gen,
1724 u64 ino, u64 ino_gen,
1725 const char *name, int name_len)
1732 if (!sctx->parent_root)
1735 ret = is_inode_existent(sctx, dir, dir_gen);
1739 /* check if the ref was overwritten by another ref */
1740 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1741 &ow_inode, &other_type);
1742 if (ret < 0 && ret != -ENOENT)
1745 /* was never and will never be overwritten */
1750 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1755 if (ow_inode == ino && gen == ino_gen) {
1760 /* we know that it is or will be overwritten. check this now */
1761 if (ow_inode < sctx->send_progress)
1771 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1772 * that got overwritten. This is used by process_recorded_refs to determine
1773 * if it has to use the path as returned by get_cur_path or the orphan name.
1775 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1778 struct fs_path *name = NULL;
1782 if (!sctx->parent_root)
1785 name = fs_path_alloc(sctx);
1789 ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name);
1793 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1794 name->start, fs_path_len(name));
1797 fs_path_free(sctx, name);
1802 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1803 * so we need to do some special handling in case we have clashes. This function
1804 * takes care of this with the help of name_cache_entry::radix_list.
1805 * In case of error, nce is kfreed.
1807 static int name_cache_insert(struct send_ctx *sctx,
1808 struct name_cache_entry *nce)
1811 struct list_head *nce_head;
1813 nce_head = radix_tree_lookup(&sctx->name_cache,
1814 (unsigned long)nce->ino);
1816 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1819 INIT_LIST_HEAD(nce_head);
1821 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1828 list_add_tail(&nce->radix_list, nce_head);
1829 list_add_tail(&nce->list, &sctx->name_cache_list);
1830 sctx->name_cache_size++;
1835 static void name_cache_delete(struct send_ctx *sctx,
1836 struct name_cache_entry *nce)
1838 struct list_head *nce_head;
1840 nce_head = radix_tree_lookup(&sctx->name_cache,
1841 (unsigned long)nce->ino);
1844 list_del(&nce->radix_list);
1845 list_del(&nce->list);
1846 sctx->name_cache_size--;
1848 if (list_empty(nce_head)) {
1849 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1854 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1857 struct list_head *nce_head;
1858 struct name_cache_entry *cur;
1860 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1864 list_for_each_entry(cur, nce_head, radix_list) {
1865 if (cur->ino == ino && cur->gen == gen)
1872 * Removes the entry from the list and adds it back to the end. This marks the
1873 * entry as recently used so that name_cache_clean_unused does not remove it.
1875 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1877 list_del(&nce->list);
1878 list_add_tail(&nce->list, &sctx->name_cache_list);
1882 * Remove some entries from the beginning of name_cache_list.
1884 static void name_cache_clean_unused(struct send_ctx *sctx)
1886 struct name_cache_entry *nce;
1888 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1891 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1892 nce = list_entry(sctx->name_cache_list.next,
1893 struct name_cache_entry, list);
1894 name_cache_delete(sctx, nce);
1899 static void name_cache_free(struct send_ctx *sctx)
1901 struct name_cache_entry *nce;
1903 while (!list_empty(&sctx->name_cache_list)) {
1904 nce = list_entry(sctx->name_cache_list.next,
1905 struct name_cache_entry, list);
1906 name_cache_delete(sctx, nce);
1912 * Used by get_cur_path for each ref up to the root.
1913 * Returns 0 if it succeeded.
1914 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1915 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1916 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1917 * Returns <0 in case of error.
1919 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1923 struct fs_path *dest)
1927 struct btrfs_path *path = NULL;
1928 struct name_cache_entry *nce = NULL;
1931 * First check if we already did a call to this function with the same
1932 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1933 * return the cached result.
1935 nce = name_cache_search(sctx, ino, gen);
1937 if (ino < sctx->send_progress && nce->need_later_update) {
1938 name_cache_delete(sctx, nce);
1942 name_cache_used(sctx, nce);
1943 *parent_ino = nce->parent_ino;
1944 *parent_gen = nce->parent_gen;
1945 ret = fs_path_add(dest, nce->name, nce->name_len);
1953 path = alloc_path_for_send();
1958 * If the inode is not existent yet, add the orphan name and return 1.
1959 * This should only happen for the parent dir that we determine in
1962 ret = is_inode_existent(sctx, ino, gen);
1967 ret = gen_unique_name(sctx, ino, gen, dest);
1975 * Depending on whether the inode was already processed or not, use
1976 * send_root or parent_root for ref lookup.
1978 if (ino < sctx->send_progress)
1979 ret = get_first_ref(sctx, sctx->send_root, ino,
1980 parent_ino, parent_gen, dest);
1982 ret = get_first_ref(sctx, sctx->parent_root, ino,
1983 parent_ino, parent_gen, dest);
1988 * Check if the ref was overwritten by an inode's ref that was processed
1989 * earlier. If yes, treat as orphan and return 1.
1991 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1992 dest->start, dest->end - dest->start);
1996 fs_path_reset(dest);
1997 ret = gen_unique_name(sctx, ino, gen, dest);
2005 * Store the result of the lookup in the name cache.
2007 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2015 nce->parent_ino = *parent_ino;
2016 nce->parent_gen = *parent_gen;
2017 nce->name_len = fs_path_len(dest);
2019 strcpy(nce->name, dest->start);
2021 if (ino < sctx->send_progress)
2022 nce->need_later_update = 0;
2024 nce->need_later_update = 1;
2026 nce_ret = name_cache_insert(sctx, nce);
2029 name_cache_clean_unused(sctx);
2032 btrfs_free_path(path);
2037 * Magic happens here. This function returns the first ref to an inode as it
2038 * would look like while receiving the stream at this point in time.
2039 * We walk the path up to the root. For every inode in between, we check if it
2040 * was already processed/sent. If yes, we continue with the parent as found
2041 * in send_root. If not, we continue with the parent as found in parent_root.
2042 * If we encounter an inode that was deleted at this point in time, we use the
2043 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2044 * that were not created yet and overwritten inodes/refs.
2046 * When do we have have orphan inodes:
2047 * 1. When an inode is freshly created and thus no valid refs are available yet
2048 * 2. When a directory lost all it's refs (deleted) but still has dir items
2049 * inside which were not processed yet (pending for move/delete). If anyone
2050 * tried to get the path to the dir items, it would get a path inside that
2052 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2053 * of an unprocessed inode. If in that case the first ref would be
2054 * overwritten, the overwritten inode gets "orphanized". Later when we
2055 * process this overwritten inode, it is restored at a new place by moving
2058 * sctx->send_progress tells this function at which point in time receiving
2061 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2062 struct fs_path *dest)
2065 struct fs_path *name = NULL;
2066 u64 parent_inode = 0;
2070 name = fs_path_alloc(sctx);
2077 fs_path_reset(dest);
2079 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2080 fs_path_reset(name);
2082 ret = __get_cur_name_and_parent(sctx, ino, gen,
2083 &parent_inode, &parent_gen, name);
2089 ret = fs_path_add_path(dest, name);
2098 fs_path_free(sctx, name);
2100 fs_path_unreverse(dest);
2105 * Called for regular files when sending extents data. Opens a struct file
2106 * to read from the file.
2108 static int open_cur_inode_file(struct send_ctx *sctx)
2111 struct btrfs_key key;
2113 struct inode *inode;
2114 struct dentry *dentry;
2118 if (sctx->cur_inode_filp)
2121 key.objectid = sctx->cur_ino;
2122 key.type = BTRFS_INODE_ITEM_KEY;
2125 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2127 if (IS_ERR(inode)) {
2128 ret = PTR_ERR(inode);
2132 dentry = d_obtain_alias(inode);
2134 if (IS_ERR(dentry)) {
2135 ret = PTR_ERR(dentry);
2139 path.mnt = sctx->mnt;
2140 path.dentry = dentry;
2141 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2145 ret = PTR_ERR(filp);
2148 sctx->cur_inode_filp = filp;
2152 * no xxxput required here as every vfs op
2153 * does it by itself on failure
2159 * Closes the struct file that was created in open_cur_inode_file
2161 static int close_cur_inode_file(struct send_ctx *sctx)
2165 if (!sctx->cur_inode_filp)
2168 ret = filp_close(sctx->cur_inode_filp, NULL);
2169 sctx->cur_inode_filp = NULL;
2176 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2178 static int send_subvol_begin(struct send_ctx *sctx)
2181 struct btrfs_root *send_root = sctx->send_root;
2182 struct btrfs_root *parent_root = sctx->parent_root;
2183 struct btrfs_path *path;
2184 struct btrfs_key key;
2185 struct btrfs_root_ref *ref;
2186 struct extent_buffer *leaf;
2190 path = alloc_path_for_send();
2194 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2196 btrfs_free_path(path);
2200 key.objectid = send_root->objectid;
2201 key.type = BTRFS_ROOT_BACKREF_KEY;
2204 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2213 leaf = path->nodes[0];
2214 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2215 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2216 key.objectid != send_root->objectid) {
2220 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2221 namelen = btrfs_root_ref_name_len(leaf, ref);
2222 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2223 btrfs_release_path(path);
2226 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2230 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2235 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2236 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2237 sctx->send_root->root_item.uuid);
2238 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2239 sctx->send_root->root_item.ctransid);
2241 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2242 sctx->parent_root->root_item.uuid);
2243 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2244 sctx->parent_root->root_item.ctransid);
2247 ret = send_cmd(sctx);
2251 btrfs_free_path(path);
2256 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2261 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2263 p = fs_path_alloc(sctx);
2267 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2271 ret = get_cur_path(sctx, ino, gen, p);
2274 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2275 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2277 ret = send_cmd(sctx);
2281 fs_path_free(sctx, p);
2285 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2290 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2292 p = fs_path_alloc(sctx);
2296 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2300 ret = get_cur_path(sctx, ino, gen, p);
2303 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2304 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2306 ret = send_cmd(sctx);
2310 fs_path_free(sctx, p);
2314 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2319 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2321 p = fs_path_alloc(sctx);
2325 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2329 ret = get_cur_path(sctx, ino, gen, p);
2332 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2333 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2334 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2336 ret = send_cmd(sctx);
2340 fs_path_free(sctx, p);
2344 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2347 struct fs_path *p = NULL;
2348 struct btrfs_inode_item *ii;
2349 struct btrfs_path *path = NULL;
2350 struct extent_buffer *eb;
2351 struct btrfs_key key;
2354 verbose_printk("btrfs: send_utimes %llu\n", ino);
2356 p = fs_path_alloc(sctx);
2360 path = alloc_path_for_send();
2367 key.type = BTRFS_INODE_ITEM_KEY;
2369 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2373 eb = path->nodes[0];
2374 slot = path->slots[0];
2375 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2377 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2381 ret = get_cur_path(sctx, ino, gen, p);
2384 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2385 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2386 btrfs_inode_atime(ii));
2387 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2388 btrfs_inode_mtime(ii));
2389 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2390 btrfs_inode_ctime(ii));
2391 /* TODO Add otime support when the otime patches get into upstream */
2393 ret = send_cmd(sctx);
2397 fs_path_free(sctx, p);
2398 btrfs_free_path(path);
2403 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2404 * a valid path yet because we did not process the refs yet. So, the inode
2405 * is created as orphan.
2407 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2416 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2418 p = fs_path_alloc(sctx);
2422 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2427 if (S_ISREG(mode)) {
2428 cmd = BTRFS_SEND_C_MKFILE;
2429 } else if (S_ISDIR(mode)) {
2430 cmd = BTRFS_SEND_C_MKDIR;
2431 } else if (S_ISLNK(mode)) {
2432 cmd = BTRFS_SEND_C_SYMLINK;
2433 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2434 cmd = BTRFS_SEND_C_MKNOD;
2435 } else if (S_ISFIFO(mode)) {
2436 cmd = BTRFS_SEND_C_MKFIFO;
2437 } else if (S_ISSOCK(mode)) {
2438 cmd = BTRFS_SEND_C_MKSOCK;
2440 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2441 (int)(mode & S_IFMT));
2446 ret = begin_cmd(sctx, cmd);
2450 ret = gen_unique_name(sctx, ino, gen, p);
2454 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2455 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2457 if (S_ISLNK(mode)) {
2459 ret = read_symlink(sctx, sctx->send_root, ino, p);
2462 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2463 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2464 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2465 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2466 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2469 ret = send_cmd(sctx);
2476 fs_path_free(sctx, p);
2481 * We need some special handling for inodes that get processed before the parent
2482 * directory got created. See process_recorded_refs for details.
2483 * This function does the check if we already created the dir out of order.
2485 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2488 struct btrfs_path *path = NULL;
2489 struct btrfs_key key;
2490 struct btrfs_key found_key;
2491 struct btrfs_key di_key;
2492 struct extent_buffer *eb;
2493 struct btrfs_dir_item *di;
2496 path = alloc_path_for_send();
2503 key.type = BTRFS_DIR_INDEX_KEY;
2506 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2511 eb = path->nodes[0];
2512 slot = path->slots[0];
2513 btrfs_item_key_to_cpu(eb, &found_key, slot);
2515 if (ret || found_key.objectid != key.objectid ||
2516 found_key.type != key.type) {
2521 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2522 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2524 if (di_key.objectid < sctx->send_progress) {
2529 key.offset = found_key.offset + 1;
2530 btrfs_release_path(path);
2534 btrfs_free_path(path);
2539 * Only creates the inode if it is:
2540 * 1. Not a directory
2541 * 2. Or a directory which was not created already due to out of order
2542 * directories. See did_create_dir and process_recorded_refs for details.
2544 static int send_create_inode_if_needed(struct send_ctx *sctx)
2548 if (S_ISDIR(sctx->cur_inode_mode)) {
2549 ret = did_create_dir(sctx, sctx->cur_ino);
2558 ret = send_create_inode(sctx, sctx->cur_ino);
2566 struct recorded_ref {
2567 struct list_head list;
2570 struct fs_path *full_path;
2578 * We need to process new refs before deleted refs, but compare_tree gives us
2579 * everything mixed. So we first record all refs and later process them.
2580 * This function is a helper to record one ref.
2582 static int record_ref(struct list_head *head, u64 dir,
2583 u64 dir_gen, struct fs_path *path)
2585 struct recorded_ref *ref;
2588 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2593 ref->dir_gen = dir_gen;
2594 ref->full_path = path;
2596 tmp = strrchr(ref->full_path->start, '/');
2598 ref->name_len = ref->full_path->end - ref->full_path->start;
2599 ref->name = ref->full_path->start;
2600 ref->dir_path_len = 0;
2601 ref->dir_path = ref->full_path->start;
2604 ref->name_len = ref->full_path->end - tmp;
2606 ref->dir_path = ref->full_path->start;
2607 ref->dir_path_len = ref->full_path->end -
2608 ref->full_path->start - 1 - ref->name_len;
2611 list_add_tail(&ref->list, head);
2615 static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
2617 struct recorded_ref *cur;
2619 while (!list_empty(head)) {
2620 cur = list_entry(head->next, struct recorded_ref, list);
2621 fs_path_free(sctx, cur->full_path);
2622 list_del(&cur->list);
2627 static void free_recorded_refs(struct send_ctx *sctx)
2629 __free_recorded_refs(sctx, &sctx->new_refs);
2630 __free_recorded_refs(sctx, &sctx->deleted_refs);
2634 * Renames/moves a file/dir to its orphan name. Used when the first
2635 * ref of an unprocessed inode gets overwritten and for all non empty
2638 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2639 struct fs_path *path)
2642 struct fs_path *orphan;
2644 orphan = fs_path_alloc(sctx);
2648 ret = gen_unique_name(sctx, ino, gen, orphan);
2652 ret = send_rename(sctx, path, orphan);
2655 fs_path_free(sctx, orphan);
2660 * Returns 1 if a directory can be removed at this point in time.
2661 * We check this by iterating all dir items and checking if the inode behind
2662 * the dir item was already processed.
2664 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2667 struct btrfs_root *root = sctx->parent_root;
2668 struct btrfs_path *path;
2669 struct btrfs_key key;
2670 struct btrfs_key found_key;
2671 struct btrfs_key loc;
2672 struct btrfs_dir_item *di;
2675 * Don't try to rmdir the top/root subvolume dir.
2677 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2680 path = alloc_path_for_send();
2685 key.type = BTRFS_DIR_INDEX_KEY;
2689 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2693 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2696 if (ret || found_key.objectid != key.objectid ||
2697 found_key.type != key.type) {
2701 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2702 struct btrfs_dir_item);
2703 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2705 if (loc.objectid > send_progress) {
2710 btrfs_release_path(path);
2711 key.offset = found_key.offset + 1;
2717 btrfs_free_path(path);
2722 * This does all the move/link/unlink/rmdir magic.
2724 static int process_recorded_refs(struct send_ctx *sctx)
2727 struct recorded_ref *cur;
2728 struct recorded_ref *cur2;
2729 struct ulist *check_dirs = NULL;
2730 struct ulist_iterator uit;
2731 struct ulist_node *un;
2732 struct fs_path *valid_path = NULL;
2735 int did_overwrite = 0;
2738 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2741 * This should never happen as the root dir always has the same ref
2742 * which is always '..'
2744 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
2746 valid_path = fs_path_alloc(sctx);
2752 check_dirs = ulist_alloc(GFP_NOFS);
2759 * First, check if the first ref of the current inode was overwritten
2760 * before. If yes, we know that the current inode was already orphanized
2761 * and thus use the orphan name. If not, we can use get_cur_path to
2762 * get the path of the first ref as it would like while receiving at
2763 * this point in time.
2764 * New inodes are always orphan at the beginning, so force to use the
2765 * orphan name in this case.
2766 * The first ref is stored in valid_path and will be updated if it
2767 * gets moved around.
2769 if (!sctx->cur_inode_new) {
2770 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2771 sctx->cur_inode_gen);
2777 if (sctx->cur_inode_new || did_overwrite) {
2778 ret = gen_unique_name(sctx, sctx->cur_ino,
2779 sctx->cur_inode_gen, valid_path);
2784 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2790 list_for_each_entry(cur, &sctx->new_refs, list) {
2792 * We may have refs where the parent directory does not exist
2793 * yet. This happens if the parent directories inum is higher
2794 * the the current inum. To handle this case, we create the
2795 * parent directory out of order. But we need to check if this
2796 * did already happen before due to other refs in the same dir.
2798 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2801 if (ret == inode_state_will_create) {
2804 * First check if any of the current inodes refs did
2805 * already create the dir.
2807 list_for_each_entry(cur2, &sctx->new_refs, list) {
2810 if (cur2->dir == cur->dir) {
2817 * If that did not happen, check if a previous inode
2818 * did already create the dir.
2821 ret = did_create_dir(sctx, cur->dir);
2825 ret = send_create_inode(sctx, cur->dir);
2832 * Check if this new ref would overwrite the first ref of
2833 * another unprocessed inode. If yes, orphanize the
2834 * overwritten inode. If we find an overwritten ref that is
2835 * not the first ref, simply unlink it.
2837 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2838 cur->name, cur->name_len,
2839 &ow_inode, &ow_gen);
2843 ret = is_first_ref(sctx, sctx->parent_root,
2844 ow_inode, cur->dir, cur->name,
2849 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2854 ret = send_unlink(sctx, cur->full_path);
2861 * link/move the ref to the new place. If we have an orphan
2862 * inode, move it and update valid_path. If not, link or move
2863 * it depending on the inode mode.
2866 ret = send_rename(sctx, valid_path, cur->full_path);
2870 ret = fs_path_copy(valid_path, cur->full_path);
2874 if (S_ISDIR(sctx->cur_inode_mode)) {
2876 * Dirs can't be linked, so move it. For moved
2877 * dirs, we always have one new and one deleted
2878 * ref. The deleted ref is ignored later.
2880 ret = send_rename(sctx, valid_path,
2884 ret = fs_path_copy(valid_path, cur->full_path);
2888 ret = send_link(sctx, cur->full_path,
2894 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2900 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2902 * Check if we can already rmdir the directory. If not,
2903 * orphanize it. For every dir item inside that gets deleted
2904 * later, we do this check again and rmdir it then if possible.
2905 * See the use of check_dirs for more details.
2907 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2911 ret = send_rmdir(sctx, valid_path);
2914 } else if (!is_orphan) {
2915 ret = orphanize_inode(sctx, sctx->cur_ino,
2916 sctx->cur_inode_gen, valid_path);
2922 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2923 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2928 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2929 !list_empty(&sctx->deleted_refs)) {
2931 * We have a moved dir. Add the old parent to check_dirs
2933 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2935 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2939 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2941 * We have a non dir inode. Go through all deleted refs and
2942 * unlink them if they were not already overwritten by other
2945 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2946 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2947 sctx->cur_ino, sctx->cur_inode_gen,
2948 cur->name, cur->name_len);
2952 ret = send_unlink(sctx, cur->full_path);
2956 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2963 * If the inode is still orphan, unlink the orphan. This may
2964 * happen when a previous inode did overwrite the first ref
2965 * of this inode and no new refs were added for the current
2966 * inode. Unlinking does not mean that the inode is deleted in
2967 * all cases. There may still be links to this inode in other
2971 ret = send_unlink(sctx, valid_path);
2978 * We did collect all parent dirs where cur_inode was once located. We
2979 * now go through all these dirs and check if they are pending for
2980 * deletion and if it's finally possible to perform the rmdir now.
2981 * We also update the inode stats of the parent dirs here.
2983 ULIST_ITER_INIT(&uit);
2984 while ((un = ulist_next(check_dirs, &uit))) {
2986 * In case we had refs into dirs that were not processed yet,
2987 * we don't need to do the utime and rmdir logic for these dirs.
2988 * The dir will be processed later.
2990 if (un->val > sctx->cur_ino)
2993 ret = get_cur_inode_state(sctx, un->val, un->aux);
2997 if (ret == inode_state_did_create ||
2998 ret == inode_state_no_change) {
2999 /* TODO delayed utimes */
3000 ret = send_utimes(sctx, un->val, un->aux);
3003 } else if (ret == inode_state_did_delete) {
3004 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
3008 ret = get_cur_path(sctx, un->val, un->aux,
3012 ret = send_rmdir(sctx, valid_path);
3022 free_recorded_refs(sctx);
3023 ulist_free(check_dirs);
3024 fs_path_free(sctx, valid_path);
3028 static int __record_new_ref(int num, u64 dir, int index,
3029 struct fs_path *name,
3033 struct send_ctx *sctx = ctx;
3037 p = fs_path_alloc(sctx);
3041 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
3046 ret = get_cur_path(sctx, dir, gen, p);
3049 ret = fs_path_add_path(p, name);
3053 ret = record_ref(&sctx->new_refs, dir, gen, p);
3057 fs_path_free(sctx, p);
3061 static int __record_deleted_ref(int num, u64 dir, int index,
3062 struct fs_path *name,
3066 struct send_ctx *sctx = ctx;
3070 p = fs_path_alloc(sctx);
3074 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3079 ret = get_cur_path(sctx, dir, gen, p);
3082 ret = fs_path_add_path(p, name);
3086 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3090 fs_path_free(sctx, p);
3094 static int record_new_ref(struct send_ctx *sctx)
3098 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3099 sctx->cmp_key, 0, __record_new_ref, sctx);
3108 static int record_deleted_ref(struct send_ctx *sctx)
3112 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3113 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3122 struct find_ref_ctx {
3124 struct fs_path *name;
3128 static int __find_iref(int num, u64 dir, int index,
3129 struct fs_path *name,
3132 struct find_ref_ctx *ctx = ctx_;
3134 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3135 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3136 ctx->found_idx = num;
3142 static int find_iref(struct send_ctx *sctx,
3143 struct btrfs_root *root,
3144 struct btrfs_path *path,
3145 struct btrfs_key *key,
3146 u64 dir, struct fs_path *name)
3149 struct find_ref_ctx ctx;
3155 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3159 if (ctx.found_idx == -1)
3162 return ctx.found_idx;
3165 static int __record_changed_new_ref(int num, u64 dir, int index,
3166 struct fs_path *name,
3170 struct send_ctx *sctx = ctx;
3172 ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
3173 sctx->cmp_key, dir, name);
3175 ret = __record_new_ref(num, dir, index, name, sctx);
3182 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3183 struct fs_path *name,
3187 struct send_ctx *sctx = ctx;
3189 ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3192 ret = __record_deleted_ref(num, dir, index, name, sctx);
3199 static int record_changed_ref(struct send_ctx *sctx)
3203 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3204 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3207 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3208 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3218 * Record and process all refs at once. Needed when an inode changes the
3219 * generation number, which means that it was deleted and recreated.
3221 static int process_all_refs(struct send_ctx *sctx,
3222 enum btrfs_compare_tree_result cmd)
3225 struct btrfs_root *root;
3226 struct btrfs_path *path;
3227 struct btrfs_key key;
3228 struct btrfs_key found_key;
3229 struct extent_buffer *eb;
3231 iterate_inode_ref_t cb;
3233 path = alloc_path_for_send();
3237 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3238 root = sctx->send_root;
3239 cb = __record_new_ref;
3240 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3241 root = sctx->parent_root;
3242 cb = __record_deleted_ref;
3247 key.objectid = sctx->cmp_key->objectid;
3248 key.type = BTRFS_INODE_REF_KEY;
3251 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3257 eb = path->nodes[0];
3258 slot = path->slots[0];
3259 btrfs_item_key_to_cpu(eb, &found_key, slot);
3261 if (found_key.objectid != key.objectid ||
3262 (found_key.type != BTRFS_INODE_REF_KEY &&
3263 found_key.type != BTRFS_INODE_EXTREF_KEY))
3266 ret = iterate_inode_ref(sctx, root, path, &found_key, 0, cb,
3268 btrfs_release_path(path);
3272 key.offset = found_key.offset + 1;
3274 btrfs_release_path(path);
3276 ret = process_recorded_refs(sctx);
3279 btrfs_free_path(path);
3283 static int send_set_xattr(struct send_ctx *sctx,
3284 struct fs_path *path,
3285 const char *name, int name_len,
3286 const char *data, int data_len)
3290 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3294 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3295 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3296 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3298 ret = send_cmd(sctx);
3305 static int send_remove_xattr(struct send_ctx *sctx,
3306 struct fs_path *path,
3307 const char *name, int name_len)
3311 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3315 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3316 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3318 ret = send_cmd(sctx);
3325 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3326 const char *name, int name_len,
3327 const char *data, int data_len,
3331 struct send_ctx *sctx = ctx;
3333 posix_acl_xattr_header dummy_acl;
3335 p = fs_path_alloc(sctx);
3340 * This hack is needed because empty acl's are stored as zero byte
3341 * data in xattrs. Problem with that is, that receiving these zero byte
3342 * acl's will fail later. To fix this, we send a dummy acl list that
3343 * only contains the version number and no entries.
3345 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3346 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3347 if (data_len == 0) {
3348 dummy_acl.a_version =
3349 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3350 data = (char *)&dummy_acl;
3351 data_len = sizeof(dummy_acl);
3355 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3359 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3362 fs_path_free(sctx, p);
3366 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3367 const char *name, int name_len,
3368 const char *data, int data_len,
3372 struct send_ctx *sctx = ctx;
3375 p = fs_path_alloc(sctx);
3379 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3383 ret = send_remove_xattr(sctx, p, name, name_len);
3386 fs_path_free(sctx, p);
3390 static int process_new_xattr(struct send_ctx *sctx)
3394 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3395 sctx->cmp_key, __process_new_xattr, sctx);
3400 static int process_deleted_xattr(struct send_ctx *sctx)
3404 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3405 sctx->cmp_key, __process_deleted_xattr, sctx);
3410 struct find_xattr_ctx {
3418 static int __find_xattr(int num, struct btrfs_key *di_key,
3419 const char *name, int name_len,
3420 const char *data, int data_len,
3421 u8 type, void *vctx)
3423 struct find_xattr_ctx *ctx = vctx;
3425 if (name_len == ctx->name_len &&
3426 strncmp(name, ctx->name, name_len) == 0) {
3427 ctx->found_idx = num;
3428 ctx->found_data_len = data_len;
3429 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3430 if (!ctx->found_data)
3432 memcpy(ctx->found_data, data, data_len);
3438 static int find_xattr(struct send_ctx *sctx,
3439 struct btrfs_root *root,
3440 struct btrfs_path *path,
3441 struct btrfs_key *key,
3442 const char *name, int name_len,
3443 char **data, int *data_len)
3446 struct find_xattr_ctx ctx;
3449 ctx.name_len = name_len;
3451 ctx.found_data = NULL;
3452 ctx.found_data_len = 0;
3454 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3458 if (ctx.found_idx == -1)
3461 *data = ctx.found_data;
3462 *data_len = ctx.found_data_len;
3464 kfree(ctx.found_data);
3466 return ctx.found_idx;
3470 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3471 const char *name, int name_len,
3472 const char *data, int data_len,
3476 struct send_ctx *sctx = ctx;
3477 char *found_data = NULL;
3478 int found_data_len = 0;
3479 struct fs_path *p = NULL;
3481 ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
3482 sctx->cmp_key, name, name_len, &found_data,
3484 if (ret == -ENOENT) {
3485 ret = __process_new_xattr(num, di_key, name, name_len, data,
3486 data_len, type, ctx);
3487 } else if (ret >= 0) {
3488 if (data_len != found_data_len ||
3489 memcmp(data, found_data, data_len)) {
3490 ret = __process_new_xattr(num, di_key, name, name_len,
3491 data, data_len, type, ctx);
3498 fs_path_free(sctx, p);
3502 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3503 const char *name, int name_len,
3504 const char *data, int data_len,
3508 struct send_ctx *sctx = ctx;
3510 ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3511 name, name_len, NULL, NULL);
3513 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3514 data_len, type, ctx);
3521 static int process_changed_xattr(struct send_ctx *sctx)
3525 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3526 sctx->cmp_key, __process_changed_new_xattr, sctx);
3529 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3530 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3536 static int process_all_new_xattrs(struct send_ctx *sctx)
3539 struct btrfs_root *root;
3540 struct btrfs_path *path;
3541 struct btrfs_key key;
3542 struct btrfs_key found_key;
3543 struct extent_buffer *eb;
3546 path = alloc_path_for_send();
3550 root = sctx->send_root;
3552 key.objectid = sctx->cmp_key->objectid;
3553 key.type = BTRFS_XATTR_ITEM_KEY;
3556 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3564 eb = path->nodes[0];
3565 slot = path->slots[0];
3566 btrfs_item_key_to_cpu(eb, &found_key, slot);
3568 if (found_key.objectid != key.objectid ||
3569 found_key.type != key.type) {
3574 ret = iterate_dir_item(sctx, root, path, &found_key,
3575 __process_new_xattr, sctx);
3579 btrfs_release_path(path);
3580 key.offset = found_key.offset + 1;
3584 btrfs_free_path(path);
3589 * Read some bytes from the current inode/file and send a write command to
3592 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3596 loff_t pos = offset;
3598 mm_segment_t old_fs;
3600 p = fs_path_alloc(sctx);
3605 * vfs normally only accepts user space buffers for security reasons.
3606 * we only read from the file and also only provide the read_buf buffer
3607 * to vfs. As this buffer does not come from a user space call, it's
3608 * ok to temporary allow kernel space buffers.
3613 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3615 ret = open_cur_inode_file(sctx);
3619 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3626 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3630 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3634 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3635 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3636 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
3638 ret = send_cmd(sctx);
3642 fs_path_free(sctx, p);
3650 * Send a clone command to user space.
3652 static int send_clone(struct send_ctx *sctx,
3653 u64 offset, u32 len,
3654 struct clone_root *clone_root)
3660 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3661 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3662 clone_root->root->objectid, clone_root->ino,
3663 clone_root->offset);
3665 p = fs_path_alloc(sctx);
3669 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3673 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3677 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3678 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3679 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3681 if (clone_root->root == sctx->send_root) {
3682 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3683 &gen, NULL, NULL, NULL, NULL);
3686 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3688 ret = get_inode_path(sctx, clone_root->root,
3689 clone_root->ino, p);
3694 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3695 clone_root->root->root_item.uuid);
3696 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3697 clone_root->root->root_item.ctransid);
3698 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3699 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3700 clone_root->offset);
3702 ret = send_cmd(sctx);
3706 fs_path_free(sctx, p);
3710 static int send_write_or_clone(struct send_ctx *sctx,
3711 struct btrfs_path *path,
3712 struct btrfs_key *key,
3713 struct clone_root *clone_root)
3716 struct btrfs_file_extent_item *ei;
3717 u64 offset = key->offset;
3723 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3724 struct btrfs_file_extent_item);
3725 type = btrfs_file_extent_type(path->nodes[0], ei);
3726 if (type == BTRFS_FILE_EXTENT_INLINE) {
3727 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3729 * it is possible the inline item won't cover the whole page,
3730 * but there may be items after this page. Make
3731 * sure to send the whole thing
3733 len = PAGE_CACHE_ALIGN(len);
3735 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3738 if (offset + len > sctx->cur_inode_size)
3739 len = sctx->cur_inode_size - offset;
3748 if (l > BTRFS_SEND_READ_SIZE)
3749 l = BTRFS_SEND_READ_SIZE;
3750 ret = send_write(sctx, pos + offset, l);
3759 ret = send_clone(sctx, offset, len, clone_root);
3766 static int is_extent_unchanged(struct send_ctx *sctx,
3767 struct btrfs_path *left_path,
3768 struct btrfs_key *ekey)
3771 struct btrfs_key key;
3772 struct btrfs_path *path = NULL;
3773 struct extent_buffer *eb;
3775 struct btrfs_key found_key;
3776 struct btrfs_file_extent_item *ei;
3781 u64 left_offset_fixed;
3789 path = alloc_path_for_send();
3793 eb = left_path->nodes[0];
3794 slot = left_path->slots[0];
3795 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3796 left_type = btrfs_file_extent_type(eb, ei);
3798 if (left_type != BTRFS_FILE_EXTENT_REG) {
3802 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3803 left_len = btrfs_file_extent_num_bytes(eb, ei);
3804 left_offset = btrfs_file_extent_offset(eb, ei);
3805 left_gen = btrfs_file_extent_generation(eb, ei);
3808 * Following comments will refer to these graphics. L is the left
3809 * extents which we are checking at the moment. 1-8 are the right
3810 * extents that we iterate.
3813 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3816 * |--1--|-2b-|...(same as above)
3818 * Alternative situation. Happens on files where extents got split.
3820 * |-----------7-----------|-6-|
3822 * Alternative situation. Happens on files which got larger.
3825 * Nothing follows after 8.
3828 key.objectid = ekey->objectid;
3829 key.type = BTRFS_EXTENT_DATA_KEY;
3830 key.offset = ekey->offset;
3831 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3840 * Handle special case where the right side has no extents at all.
3842 eb = path->nodes[0];
3843 slot = path->slots[0];
3844 btrfs_item_key_to_cpu(eb, &found_key, slot);
3845 if (found_key.objectid != key.objectid ||
3846 found_key.type != key.type) {
3852 * We're now on 2a, 2b or 7.
3855 while (key.offset < ekey->offset + left_len) {
3856 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3857 right_type = btrfs_file_extent_type(eb, ei);
3858 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3859 right_len = btrfs_file_extent_num_bytes(eb, ei);
3860 right_offset = btrfs_file_extent_offset(eb, ei);
3861 right_gen = btrfs_file_extent_generation(eb, ei);
3863 if (right_type != BTRFS_FILE_EXTENT_REG) {
3869 * Are we at extent 8? If yes, we know the extent is changed.
3870 * This may only happen on the first iteration.
3872 if (found_key.offset + right_len <= ekey->offset) {
3877 left_offset_fixed = left_offset;
3878 if (key.offset < ekey->offset) {
3879 /* Fix the right offset for 2a and 7. */
3880 right_offset += ekey->offset - key.offset;
3882 /* Fix the left offset for all behind 2a and 2b */
3883 left_offset_fixed += key.offset - ekey->offset;
3887 * Check if we have the same extent.
3889 if (left_disknr != right_disknr ||
3890 left_offset_fixed != right_offset ||
3891 left_gen != right_gen) {
3897 * Go to the next extent.
3899 ret = btrfs_next_item(sctx->parent_root, path);
3903 eb = path->nodes[0];
3904 slot = path->slots[0];
3905 btrfs_item_key_to_cpu(eb, &found_key, slot);
3907 if (ret || found_key.objectid != key.objectid ||
3908 found_key.type != key.type) {
3909 key.offset += right_len;
3912 if (found_key.offset != key.offset + right_len) {
3913 /* Should really not happen */
3922 * We're now behind the left extent (treat as unchanged) or at the end
3923 * of the right side (treat as changed).
3925 if (key.offset >= ekey->offset + left_len)
3932 btrfs_free_path(path);
3936 static int process_extent(struct send_ctx *sctx,
3937 struct btrfs_path *path,
3938 struct btrfs_key *key)
3941 struct clone_root *found_clone = NULL;
3943 if (S_ISLNK(sctx->cur_inode_mode))
3946 if (sctx->parent_root && !sctx->cur_inode_new) {
3947 ret = is_extent_unchanged(sctx, path, key);
3956 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3957 sctx->cur_inode_size, &found_clone);
3958 if (ret != -ENOENT && ret < 0)
3961 ret = send_write_or_clone(sctx, path, key, found_clone);
3967 static int process_all_extents(struct send_ctx *sctx)
3970 struct btrfs_root *root;
3971 struct btrfs_path *path;
3972 struct btrfs_key key;
3973 struct btrfs_key found_key;
3974 struct extent_buffer *eb;
3977 root = sctx->send_root;
3978 path = alloc_path_for_send();
3982 key.objectid = sctx->cmp_key->objectid;
3983 key.type = BTRFS_EXTENT_DATA_KEY;
3986 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3994 eb = path->nodes[0];
3995 slot = path->slots[0];
3996 btrfs_item_key_to_cpu(eb, &found_key, slot);
3998 if (found_key.objectid != key.objectid ||
3999 found_key.type != key.type) {
4004 ret = process_extent(sctx, path, &found_key);
4008 btrfs_release_path(path);
4009 key.offset = found_key.offset + 1;
4013 btrfs_free_path(path);
4017 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
4021 if (sctx->cur_ino == 0)
4023 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4024 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4026 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4029 ret = process_recorded_refs(sctx);
4034 * We have processed the refs and thus need to advance send_progress.
4035 * Now, calls to get_cur_xxx will take the updated refs of the current
4036 * inode into account.
4038 sctx->send_progress = sctx->cur_ino + 1;
4044 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4056 ret = process_recorded_refs_if_needed(sctx, at_end);
4060 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4062 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4065 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4066 &left_mode, &left_uid, &left_gid, NULL);
4070 if (!sctx->parent_root || sctx->cur_inode_new) {
4072 if (!S_ISLNK(sctx->cur_inode_mode))
4075 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4076 NULL, NULL, &right_mode, &right_uid,
4081 if (left_uid != right_uid || left_gid != right_gid)
4083 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4087 if (S_ISREG(sctx->cur_inode_mode)) {
4088 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4089 sctx->cur_inode_size);
4095 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4096 left_uid, left_gid);
4101 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4108 * Need to send that every time, no matter if it actually changed
4109 * between the two trees as we have done changes to the inode before.
4111 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4119 static int changed_inode(struct send_ctx *sctx,
4120 enum btrfs_compare_tree_result result)
4123 struct btrfs_key *key = sctx->cmp_key;
4124 struct btrfs_inode_item *left_ii = NULL;
4125 struct btrfs_inode_item *right_ii = NULL;
4129 ret = close_cur_inode_file(sctx);
4133 sctx->cur_ino = key->objectid;
4134 sctx->cur_inode_new_gen = 0;
4137 * Set send_progress to current inode. This will tell all get_cur_xxx
4138 * functions that the current inode's refs are not updated yet. Later,
4139 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4141 sctx->send_progress = sctx->cur_ino;
4143 if (result == BTRFS_COMPARE_TREE_NEW ||
4144 result == BTRFS_COMPARE_TREE_CHANGED) {
4145 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4146 sctx->left_path->slots[0],
4147 struct btrfs_inode_item);
4148 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4151 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4152 sctx->right_path->slots[0],
4153 struct btrfs_inode_item);
4154 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4157 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4158 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4159 sctx->right_path->slots[0],
4160 struct btrfs_inode_item);
4162 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4166 * The cur_ino = root dir case is special here. We can't treat
4167 * the inode as deleted+reused because it would generate a
4168 * stream that tries to delete/mkdir the root dir.
4170 if (left_gen != right_gen &&
4171 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4172 sctx->cur_inode_new_gen = 1;
4175 if (result == BTRFS_COMPARE_TREE_NEW) {
4176 sctx->cur_inode_gen = left_gen;
4177 sctx->cur_inode_new = 1;
4178 sctx->cur_inode_deleted = 0;
4179 sctx->cur_inode_size = btrfs_inode_size(
4180 sctx->left_path->nodes[0], left_ii);
4181 sctx->cur_inode_mode = btrfs_inode_mode(
4182 sctx->left_path->nodes[0], left_ii);
4183 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4184 ret = send_create_inode_if_needed(sctx);
4185 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4186 sctx->cur_inode_gen = right_gen;
4187 sctx->cur_inode_new = 0;
4188 sctx->cur_inode_deleted = 1;
4189 sctx->cur_inode_size = btrfs_inode_size(
4190 sctx->right_path->nodes[0], right_ii);
4191 sctx->cur_inode_mode = btrfs_inode_mode(
4192 sctx->right_path->nodes[0], right_ii);
4193 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4195 * We need to do some special handling in case the inode was
4196 * reported as changed with a changed generation number. This
4197 * means that the original inode was deleted and new inode
4198 * reused the same inum. So we have to treat the old inode as
4199 * deleted and the new one as new.
4201 if (sctx->cur_inode_new_gen) {
4203 * First, process the inode as if it was deleted.
4205 sctx->cur_inode_gen = right_gen;
4206 sctx->cur_inode_new = 0;
4207 sctx->cur_inode_deleted = 1;
4208 sctx->cur_inode_size = btrfs_inode_size(
4209 sctx->right_path->nodes[0], right_ii);
4210 sctx->cur_inode_mode = btrfs_inode_mode(
4211 sctx->right_path->nodes[0], right_ii);
4212 ret = process_all_refs(sctx,
4213 BTRFS_COMPARE_TREE_DELETED);
4218 * Now process the inode as if it was new.
4220 sctx->cur_inode_gen = left_gen;
4221 sctx->cur_inode_new = 1;
4222 sctx->cur_inode_deleted = 0;
4223 sctx->cur_inode_size = btrfs_inode_size(
4224 sctx->left_path->nodes[0], left_ii);
4225 sctx->cur_inode_mode = btrfs_inode_mode(
4226 sctx->left_path->nodes[0], left_ii);
4227 ret = send_create_inode_if_needed(sctx);
4231 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4235 * Advance send_progress now as we did not get into
4236 * process_recorded_refs_if_needed in the new_gen case.
4238 sctx->send_progress = sctx->cur_ino + 1;
4241 * Now process all extents and xattrs of the inode as if
4242 * they were all new.
4244 ret = process_all_extents(sctx);
4247 ret = process_all_new_xattrs(sctx);
4251 sctx->cur_inode_gen = left_gen;
4252 sctx->cur_inode_new = 0;
4253 sctx->cur_inode_new_gen = 0;
4254 sctx->cur_inode_deleted = 0;
4255 sctx->cur_inode_size = btrfs_inode_size(
4256 sctx->left_path->nodes[0], left_ii);
4257 sctx->cur_inode_mode = btrfs_inode_mode(
4258 sctx->left_path->nodes[0], left_ii);
4267 * We have to process new refs before deleted refs, but compare_trees gives us
4268 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4269 * first and later process them in process_recorded_refs.
4270 * For the cur_inode_new_gen case, we skip recording completely because
4271 * changed_inode did already initiate processing of refs. The reason for this is
4272 * that in this case, compare_tree actually compares the refs of 2 different
4273 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4274 * refs of the right tree as deleted and all refs of the left tree as new.
4276 static int changed_ref(struct send_ctx *sctx,
4277 enum btrfs_compare_tree_result result)
4281 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4283 if (!sctx->cur_inode_new_gen &&
4284 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4285 if (result == BTRFS_COMPARE_TREE_NEW)
4286 ret = record_new_ref(sctx);
4287 else if (result == BTRFS_COMPARE_TREE_DELETED)
4288 ret = record_deleted_ref(sctx);
4289 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4290 ret = record_changed_ref(sctx);
4297 * Process new/deleted/changed xattrs. We skip processing in the
4298 * cur_inode_new_gen case because changed_inode did already initiate processing
4299 * of xattrs. The reason is the same as in changed_ref
4301 static int changed_xattr(struct send_ctx *sctx,
4302 enum btrfs_compare_tree_result result)
4306 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4308 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4309 if (result == BTRFS_COMPARE_TREE_NEW)
4310 ret = process_new_xattr(sctx);
4311 else if (result == BTRFS_COMPARE_TREE_DELETED)
4312 ret = process_deleted_xattr(sctx);
4313 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4314 ret = process_changed_xattr(sctx);
4321 * Process new/deleted/changed extents. We skip processing in the
4322 * cur_inode_new_gen case because changed_inode did already initiate processing
4323 * of extents. The reason is the same as in changed_ref
4325 static int changed_extent(struct send_ctx *sctx,
4326 enum btrfs_compare_tree_result result)
4330 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4332 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4333 if (result != BTRFS_COMPARE_TREE_DELETED)
4334 ret = process_extent(sctx, sctx->left_path,
4342 * Updates compare related fields in sctx and simply forwards to the actual
4343 * changed_xxx functions.
4345 static int changed_cb(struct btrfs_root *left_root,
4346 struct btrfs_root *right_root,
4347 struct btrfs_path *left_path,
4348 struct btrfs_path *right_path,
4349 struct btrfs_key *key,
4350 enum btrfs_compare_tree_result result,
4354 struct send_ctx *sctx = ctx;
4356 sctx->left_path = left_path;
4357 sctx->right_path = right_path;
4358 sctx->cmp_key = key;
4360 ret = finish_inode_if_needed(sctx, 0);
4364 /* Ignore non-FS objects */
4365 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4366 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4369 if (key->type == BTRFS_INODE_ITEM_KEY)
4370 ret = changed_inode(sctx, result);
4371 else if (key->type == BTRFS_INODE_REF_KEY ||
4372 key->type == BTRFS_INODE_EXTREF_KEY)
4373 ret = changed_ref(sctx, result);
4374 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4375 ret = changed_xattr(sctx, result);
4376 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4377 ret = changed_extent(sctx, result);
4383 static int full_send_tree(struct send_ctx *sctx)
4386 struct btrfs_trans_handle *trans = NULL;
4387 struct btrfs_root *send_root = sctx->send_root;
4388 struct btrfs_key key;
4389 struct btrfs_key found_key;
4390 struct btrfs_path *path;
4391 struct extent_buffer *eb;
4396 path = alloc_path_for_send();
4400 spin_lock(&send_root->root_times_lock);
4401 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4402 spin_unlock(&send_root->root_times_lock);
4404 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4405 key.type = BTRFS_INODE_ITEM_KEY;
4410 * We need to make sure the transaction does not get committed
4411 * while we do anything on commit roots. Join a transaction to prevent
4414 trans = btrfs_join_transaction(send_root);
4415 if (IS_ERR(trans)) {
4416 ret = PTR_ERR(trans);
4422 * Make sure the tree has not changed after re-joining. We detect this
4423 * by comparing start_ctransid and ctransid. They should always match.
4425 spin_lock(&send_root->root_times_lock);
4426 ctransid = btrfs_root_ctransid(&send_root->root_item);
4427 spin_unlock(&send_root->root_times_lock);
4429 if (ctransid != start_ctransid) {
4430 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4431 "send was modified in between. This is "
4432 "probably a bug.\n");
4437 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4445 * When someone want to commit while we iterate, end the
4446 * joined transaction and rejoin.
4448 if (btrfs_should_end_transaction(trans, send_root)) {
4449 ret = btrfs_end_transaction(trans, send_root);
4453 btrfs_release_path(path);
4457 eb = path->nodes[0];
4458 slot = path->slots[0];
4459 btrfs_item_key_to_cpu(eb, &found_key, slot);
4461 ret = changed_cb(send_root, NULL, path, NULL,
4462 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4466 key.objectid = found_key.objectid;
4467 key.type = found_key.type;
4468 key.offset = found_key.offset + 1;
4470 ret = btrfs_next_item(send_root, path);
4480 ret = finish_inode_if_needed(sctx, 1);
4483 btrfs_free_path(path);
4486 ret = btrfs_end_transaction(trans, send_root);
4488 btrfs_end_transaction(trans, send_root);
4493 static int send_subvol(struct send_ctx *sctx)
4497 ret = send_header(sctx);
4501 ret = send_subvol_begin(sctx);
4505 if (sctx->parent_root) {
4506 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4510 ret = finish_inode_if_needed(sctx, 1);
4514 ret = full_send_tree(sctx);
4521 ret = close_cur_inode_file(sctx);
4523 close_cur_inode_file(sctx);
4525 free_recorded_refs(sctx);
4529 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4532 struct btrfs_root *send_root;
4533 struct btrfs_root *clone_root;
4534 struct btrfs_fs_info *fs_info;
4535 struct btrfs_ioctl_send_args *arg = NULL;
4536 struct btrfs_key key;
4537 struct file *filp = NULL;
4538 struct send_ctx *sctx = NULL;
4540 u64 *clone_sources_tmp = NULL;
4542 if (!capable(CAP_SYS_ADMIN))
4545 send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root;
4546 fs_info = send_root->fs_info;
4548 arg = memdup_user(arg_, sizeof(*arg));
4555 if (!access_ok(VERIFY_READ, arg->clone_sources,
4556 sizeof(*arg->clone_sources *
4557 arg->clone_sources_count))) {
4562 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4568 INIT_LIST_HEAD(&sctx->new_refs);
4569 INIT_LIST_HEAD(&sctx->deleted_refs);
4570 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4571 INIT_LIST_HEAD(&sctx->name_cache_list);
4573 sctx->send_filp = fget(arg->send_fd);
4574 if (IS_ERR(sctx->send_filp)) {
4575 ret = PTR_ERR(sctx->send_filp);
4579 sctx->mnt = mnt_file->f_path.mnt;
4581 sctx->send_root = send_root;
4582 sctx->clone_roots_cnt = arg->clone_sources_count;
4584 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4585 sctx->send_buf = vmalloc(sctx->send_max_size);
4586 if (!sctx->send_buf) {
4591 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4592 if (!sctx->read_buf) {
4597 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4598 (arg->clone_sources_count + 1));
4599 if (!sctx->clone_roots) {
4604 if (arg->clone_sources_count) {
4605 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4606 sizeof(*arg->clone_sources));
4607 if (!clone_sources_tmp) {
4612 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4613 arg->clone_sources_count *
4614 sizeof(*arg->clone_sources));
4620 for (i = 0; i < arg->clone_sources_count; i++) {
4621 key.objectid = clone_sources_tmp[i];
4622 key.type = BTRFS_ROOT_ITEM_KEY;
4623 key.offset = (u64)-1;
4624 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4629 if (IS_ERR(clone_root)) {
4630 ret = PTR_ERR(clone_root);
4633 sctx->clone_roots[i].root = clone_root;
4635 vfree(clone_sources_tmp);
4636 clone_sources_tmp = NULL;
4639 if (arg->parent_root) {
4640 key.objectid = arg->parent_root;
4641 key.type = BTRFS_ROOT_ITEM_KEY;
4642 key.offset = (u64)-1;
4643 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4644 if (!sctx->parent_root) {
4651 * Clones from send_root are allowed, but only if the clone source
4652 * is behind the current send position. This is checked while searching
4653 * for possible clone sources.
4655 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4657 /* We do a bsearch later */
4658 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4659 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4662 ret = send_subvol(sctx);
4666 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4669 ret = send_cmd(sctx);
4677 vfree(clone_sources_tmp);
4680 if (sctx->send_filp)
4681 fput(sctx->send_filp);
4683 vfree(sctx->clone_roots);
4684 vfree(sctx->send_buf);
4685 vfree(sctx->read_buf);
4687 name_cache_free(sctx);