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);
1821 INIT_LIST_HEAD(nce_head);
1823 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1830 list_add_tail(&nce->radix_list, nce_head);
1831 list_add_tail(&nce->list, &sctx->name_cache_list);
1832 sctx->name_cache_size++;
1837 static void name_cache_delete(struct send_ctx *sctx,
1838 struct name_cache_entry *nce)
1840 struct list_head *nce_head;
1842 nce_head = radix_tree_lookup(&sctx->name_cache,
1843 (unsigned long)nce->ino);
1846 list_del(&nce->radix_list);
1847 list_del(&nce->list);
1848 sctx->name_cache_size--;
1850 if (list_empty(nce_head)) {
1851 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1856 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1859 struct list_head *nce_head;
1860 struct name_cache_entry *cur;
1862 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1866 list_for_each_entry(cur, nce_head, radix_list) {
1867 if (cur->ino == ino && cur->gen == gen)
1874 * Removes the entry from the list and adds it back to the end. This marks the
1875 * entry as recently used so that name_cache_clean_unused does not remove it.
1877 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1879 list_del(&nce->list);
1880 list_add_tail(&nce->list, &sctx->name_cache_list);
1884 * Remove some entries from the beginning of name_cache_list.
1886 static void name_cache_clean_unused(struct send_ctx *sctx)
1888 struct name_cache_entry *nce;
1890 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1893 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1894 nce = list_entry(sctx->name_cache_list.next,
1895 struct name_cache_entry, list);
1896 name_cache_delete(sctx, nce);
1901 static void name_cache_free(struct send_ctx *sctx)
1903 struct name_cache_entry *nce;
1905 while (!list_empty(&sctx->name_cache_list)) {
1906 nce = list_entry(sctx->name_cache_list.next,
1907 struct name_cache_entry, list);
1908 name_cache_delete(sctx, nce);
1914 * Used by get_cur_path for each ref up to the root.
1915 * Returns 0 if it succeeded.
1916 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1917 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1918 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1919 * Returns <0 in case of error.
1921 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1925 struct fs_path *dest)
1929 struct btrfs_path *path = NULL;
1930 struct name_cache_entry *nce = NULL;
1933 * First check if we already did a call to this function with the same
1934 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1935 * return the cached result.
1937 nce = name_cache_search(sctx, ino, gen);
1939 if (ino < sctx->send_progress && nce->need_later_update) {
1940 name_cache_delete(sctx, nce);
1944 name_cache_used(sctx, nce);
1945 *parent_ino = nce->parent_ino;
1946 *parent_gen = nce->parent_gen;
1947 ret = fs_path_add(dest, nce->name, nce->name_len);
1955 path = alloc_path_for_send();
1960 * If the inode is not existent yet, add the orphan name and return 1.
1961 * This should only happen for the parent dir that we determine in
1964 ret = is_inode_existent(sctx, ino, gen);
1969 ret = gen_unique_name(sctx, ino, gen, dest);
1977 * Depending on whether the inode was already processed or not, use
1978 * send_root or parent_root for ref lookup.
1980 if (ino < sctx->send_progress)
1981 ret = get_first_ref(sctx, sctx->send_root, ino,
1982 parent_ino, parent_gen, dest);
1984 ret = get_first_ref(sctx, sctx->parent_root, ino,
1985 parent_ino, parent_gen, dest);
1990 * Check if the ref was overwritten by an inode's ref that was processed
1991 * earlier. If yes, treat as orphan and return 1.
1993 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1994 dest->start, dest->end - dest->start);
1998 fs_path_reset(dest);
1999 ret = gen_unique_name(sctx, ino, gen, dest);
2007 * Store the result of the lookup in the name cache.
2009 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2017 nce->parent_ino = *parent_ino;
2018 nce->parent_gen = *parent_gen;
2019 nce->name_len = fs_path_len(dest);
2021 strcpy(nce->name, dest->start);
2023 if (ino < sctx->send_progress)
2024 nce->need_later_update = 0;
2026 nce->need_later_update = 1;
2028 nce_ret = name_cache_insert(sctx, nce);
2031 name_cache_clean_unused(sctx);
2034 btrfs_free_path(path);
2039 * Magic happens here. This function returns the first ref to an inode as it
2040 * would look like while receiving the stream at this point in time.
2041 * We walk the path up to the root. For every inode in between, we check if it
2042 * was already processed/sent. If yes, we continue with the parent as found
2043 * in send_root. If not, we continue with the parent as found in parent_root.
2044 * If we encounter an inode that was deleted at this point in time, we use the
2045 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2046 * that were not created yet and overwritten inodes/refs.
2048 * When do we have have orphan inodes:
2049 * 1. When an inode is freshly created and thus no valid refs are available yet
2050 * 2. When a directory lost all it's refs (deleted) but still has dir items
2051 * inside which were not processed yet (pending for move/delete). If anyone
2052 * tried to get the path to the dir items, it would get a path inside that
2054 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2055 * of an unprocessed inode. If in that case the first ref would be
2056 * overwritten, the overwritten inode gets "orphanized". Later when we
2057 * process this overwritten inode, it is restored at a new place by moving
2060 * sctx->send_progress tells this function at which point in time receiving
2063 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2064 struct fs_path *dest)
2067 struct fs_path *name = NULL;
2068 u64 parent_inode = 0;
2072 name = fs_path_alloc(sctx);
2079 fs_path_reset(dest);
2081 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2082 fs_path_reset(name);
2084 ret = __get_cur_name_and_parent(sctx, ino, gen,
2085 &parent_inode, &parent_gen, name);
2091 ret = fs_path_add_path(dest, name);
2100 fs_path_free(sctx, name);
2102 fs_path_unreverse(dest);
2107 * Called for regular files when sending extents data. Opens a struct file
2108 * to read from the file.
2110 static int open_cur_inode_file(struct send_ctx *sctx)
2113 struct btrfs_key key;
2115 struct inode *inode;
2116 struct dentry *dentry;
2120 if (sctx->cur_inode_filp)
2123 key.objectid = sctx->cur_ino;
2124 key.type = BTRFS_INODE_ITEM_KEY;
2127 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2129 if (IS_ERR(inode)) {
2130 ret = PTR_ERR(inode);
2134 dentry = d_obtain_alias(inode);
2136 if (IS_ERR(dentry)) {
2137 ret = PTR_ERR(dentry);
2141 path.mnt = sctx->mnt;
2142 path.dentry = dentry;
2143 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2147 ret = PTR_ERR(filp);
2150 sctx->cur_inode_filp = filp;
2154 * no xxxput required here as every vfs op
2155 * does it by itself on failure
2161 * Closes the struct file that was created in open_cur_inode_file
2163 static int close_cur_inode_file(struct send_ctx *sctx)
2167 if (!sctx->cur_inode_filp)
2170 ret = filp_close(sctx->cur_inode_filp, NULL);
2171 sctx->cur_inode_filp = NULL;
2178 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2180 static int send_subvol_begin(struct send_ctx *sctx)
2183 struct btrfs_root *send_root = sctx->send_root;
2184 struct btrfs_root *parent_root = sctx->parent_root;
2185 struct btrfs_path *path;
2186 struct btrfs_key key;
2187 struct btrfs_root_ref *ref;
2188 struct extent_buffer *leaf;
2192 path = alloc_path_for_send();
2196 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2198 btrfs_free_path(path);
2202 key.objectid = send_root->objectid;
2203 key.type = BTRFS_ROOT_BACKREF_KEY;
2206 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2215 leaf = path->nodes[0];
2216 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2217 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2218 key.objectid != send_root->objectid) {
2222 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2223 namelen = btrfs_root_ref_name_len(leaf, ref);
2224 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2225 btrfs_release_path(path);
2228 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2232 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2237 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2238 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2239 sctx->send_root->root_item.uuid);
2240 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2241 sctx->send_root->root_item.ctransid);
2243 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2244 sctx->parent_root->root_item.uuid);
2245 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2246 sctx->parent_root->root_item.ctransid);
2249 ret = send_cmd(sctx);
2253 btrfs_free_path(path);
2258 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2263 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2265 p = fs_path_alloc(sctx);
2269 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2273 ret = get_cur_path(sctx, ino, gen, p);
2276 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2277 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2279 ret = send_cmd(sctx);
2283 fs_path_free(sctx, p);
2287 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2292 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2294 p = fs_path_alloc(sctx);
2298 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2302 ret = get_cur_path(sctx, ino, gen, p);
2305 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2306 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2308 ret = send_cmd(sctx);
2312 fs_path_free(sctx, p);
2316 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2321 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2323 p = fs_path_alloc(sctx);
2327 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2331 ret = get_cur_path(sctx, ino, gen, p);
2334 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2335 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2336 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2338 ret = send_cmd(sctx);
2342 fs_path_free(sctx, p);
2346 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2349 struct fs_path *p = NULL;
2350 struct btrfs_inode_item *ii;
2351 struct btrfs_path *path = NULL;
2352 struct extent_buffer *eb;
2353 struct btrfs_key key;
2356 verbose_printk("btrfs: send_utimes %llu\n", ino);
2358 p = fs_path_alloc(sctx);
2362 path = alloc_path_for_send();
2369 key.type = BTRFS_INODE_ITEM_KEY;
2371 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2375 eb = path->nodes[0];
2376 slot = path->slots[0];
2377 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2379 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2383 ret = get_cur_path(sctx, ino, gen, p);
2386 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2387 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2388 btrfs_inode_atime(ii));
2389 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2390 btrfs_inode_mtime(ii));
2391 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2392 btrfs_inode_ctime(ii));
2393 /* TODO Add otime support when the otime patches get into upstream */
2395 ret = send_cmd(sctx);
2399 fs_path_free(sctx, p);
2400 btrfs_free_path(path);
2405 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2406 * a valid path yet because we did not process the refs yet. So, the inode
2407 * is created as orphan.
2409 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2418 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2420 p = fs_path_alloc(sctx);
2424 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2429 if (S_ISREG(mode)) {
2430 cmd = BTRFS_SEND_C_MKFILE;
2431 } else if (S_ISDIR(mode)) {
2432 cmd = BTRFS_SEND_C_MKDIR;
2433 } else if (S_ISLNK(mode)) {
2434 cmd = BTRFS_SEND_C_SYMLINK;
2435 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2436 cmd = BTRFS_SEND_C_MKNOD;
2437 } else if (S_ISFIFO(mode)) {
2438 cmd = BTRFS_SEND_C_MKFIFO;
2439 } else if (S_ISSOCK(mode)) {
2440 cmd = BTRFS_SEND_C_MKSOCK;
2442 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2443 (int)(mode & S_IFMT));
2448 ret = begin_cmd(sctx, cmd);
2452 ret = gen_unique_name(sctx, ino, gen, p);
2456 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2457 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2459 if (S_ISLNK(mode)) {
2461 ret = read_symlink(sctx, sctx->send_root, ino, p);
2464 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2465 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2466 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2467 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2468 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2471 ret = send_cmd(sctx);
2478 fs_path_free(sctx, p);
2483 * We need some special handling for inodes that get processed before the parent
2484 * directory got created. See process_recorded_refs for details.
2485 * This function does the check if we already created the dir out of order.
2487 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2490 struct btrfs_path *path = NULL;
2491 struct btrfs_key key;
2492 struct btrfs_key found_key;
2493 struct btrfs_key di_key;
2494 struct extent_buffer *eb;
2495 struct btrfs_dir_item *di;
2498 path = alloc_path_for_send();
2505 key.type = BTRFS_DIR_INDEX_KEY;
2508 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2513 eb = path->nodes[0];
2514 slot = path->slots[0];
2515 btrfs_item_key_to_cpu(eb, &found_key, slot);
2517 if (ret || found_key.objectid != key.objectid ||
2518 found_key.type != key.type) {
2523 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2524 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2526 if (di_key.objectid < sctx->send_progress) {
2531 key.offset = found_key.offset + 1;
2532 btrfs_release_path(path);
2536 btrfs_free_path(path);
2541 * Only creates the inode if it is:
2542 * 1. Not a directory
2543 * 2. Or a directory which was not created already due to out of order
2544 * directories. See did_create_dir and process_recorded_refs for details.
2546 static int send_create_inode_if_needed(struct send_ctx *sctx)
2550 if (S_ISDIR(sctx->cur_inode_mode)) {
2551 ret = did_create_dir(sctx, sctx->cur_ino);
2560 ret = send_create_inode(sctx, sctx->cur_ino);
2568 struct recorded_ref {
2569 struct list_head list;
2572 struct fs_path *full_path;
2580 * We need to process new refs before deleted refs, but compare_tree gives us
2581 * everything mixed. So we first record all refs and later process them.
2582 * This function is a helper to record one ref.
2584 static int record_ref(struct list_head *head, u64 dir,
2585 u64 dir_gen, struct fs_path *path)
2587 struct recorded_ref *ref;
2590 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2595 ref->dir_gen = dir_gen;
2596 ref->full_path = path;
2598 tmp = strrchr(ref->full_path->start, '/');
2600 ref->name_len = ref->full_path->end - ref->full_path->start;
2601 ref->name = ref->full_path->start;
2602 ref->dir_path_len = 0;
2603 ref->dir_path = ref->full_path->start;
2606 ref->name_len = ref->full_path->end - tmp;
2608 ref->dir_path = ref->full_path->start;
2609 ref->dir_path_len = ref->full_path->end -
2610 ref->full_path->start - 1 - ref->name_len;
2613 list_add_tail(&ref->list, head);
2617 static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
2619 struct recorded_ref *cur;
2621 while (!list_empty(head)) {
2622 cur = list_entry(head->next, struct recorded_ref, list);
2623 fs_path_free(sctx, cur->full_path);
2624 list_del(&cur->list);
2629 static void free_recorded_refs(struct send_ctx *sctx)
2631 __free_recorded_refs(sctx, &sctx->new_refs);
2632 __free_recorded_refs(sctx, &sctx->deleted_refs);
2636 * Renames/moves a file/dir to its orphan name. Used when the first
2637 * ref of an unprocessed inode gets overwritten and for all non empty
2640 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2641 struct fs_path *path)
2644 struct fs_path *orphan;
2646 orphan = fs_path_alloc(sctx);
2650 ret = gen_unique_name(sctx, ino, gen, orphan);
2654 ret = send_rename(sctx, path, orphan);
2657 fs_path_free(sctx, orphan);
2662 * Returns 1 if a directory can be removed at this point in time.
2663 * We check this by iterating all dir items and checking if the inode behind
2664 * the dir item was already processed.
2666 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2669 struct btrfs_root *root = sctx->parent_root;
2670 struct btrfs_path *path;
2671 struct btrfs_key key;
2672 struct btrfs_key found_key;
2673 struct btrfs_key loc;
2674 struct btrfs_dir_item *di;
2677 * Don't try to rmdir the top/root subvolume dir.
2679 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2682 path = alloc_path_for_send();
2687 key.type = BTRFS_DIR_INDEX_KEY;
2691 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2695 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2698 if (ret || found_key.objectid != key.objectid ||
2699 found_key.type != key.type) {
2703 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2704 struct btrfs_dir_item);
2705 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2707 if (loc.objectid > send_progress) {
2712 btrfs_release_path(path);
2713 key.offset = found_key.offset + 1;
2719 btrfs_free_path(path);
2724 * This does all the move/link/unlink/rmdir magic.
2726 static int process_recorded_refs(struct send_ctx *sctx)
2729 struct recorded_ref *cur;
2730 struct recorded_ref *cur2;
2731 struct ulist *check_dirs = NULL;
2732 struct ulist_iterator uit;
2733 struct ulist_node *un;
2734 struct fs_path *valid_path = NULL;
2737 int did_overwrite = 0;
2740 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2743 * This should never happen as the root dir always has the same ref
2744 * which is always '..'
2746 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
2748 valid_path = fs_path_alloc(sctx);
2754 check_dirs = ulist_alloc(GFP_NOFS);
2761 * First, check if the first ref of the current inode was overwritten
2762 * before. If yes, we know that the current inode was already orphanized
2763 * and thus use the orphan name. If not, we can use get_cur_path to
2764 * get the path of the first ref as it would like while receiving at
2765 * this point in time.
2766 * New inodes are always orphan at the beginning, so force to use the
2767 * orphan name in this case.
2768 * The first ref is stored in valid_path and will be updated if it
2769 * gets moved around.
2771 if (!sctx->cur_inode_new) {
2772 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2773 sctx->cur_inode_gen);
2779 if (sctx->cur_inode_new || did_overwrite) {
2780 ret = gen_unique_name(sctx, sctx->cur_ino,
2781 sctx->cur_inode_gen, valid_path);
2786 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2792 list_for_each_entry(cur, &sctx->new_refs, list) {
2794 * We may have refs where the parent directory does not exist
2795 * yet. This happens if the parent directories inum is higher
2796 * the the current inum. To handle this case, we create the
2797 * parent directory out of order. But we need to check if this
2798 * did already happen before due to other refs in the same dir.
2800 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2803 if (ret == inode_state_will_create) {
2806 * First check if any of the current inodes refs did
2807 * already create the dir.
2809 list_for_each_entry(cur2, &sctx->new_refs, list) {
2812 if (cur2->dir == cur->dir) {
2819 * If that did not happen, check if a previous inode
2820 * did already create the dir.
2823 ret = did_create_dir(sctx, cur->dir);
2827 ret = send_create_inode(sctx, cur->dir);
2834 * Check if this new ref would overwrite the first ref of
2835 * another unprocessed inode. If yes, orphanize the
2836 * overwritten inode. If we find an overwritten ref that is
2837 * not the first ref, simply unlink it.
2839 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2840 cur->name, cur->name_len,
2841 &ow_inode, &ow_gen);
2845 ret = is_first_ref(sctx, sctx->parent_root,
2846 ow_inode, cur->dir, cur->name,
2851 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2856 ret = send_unlink(sctx, cur->full_path);
2863 * link/move the ref to the new place. If we have an orphan
2864 * inode, move it and update valid_path. If not, link or move
2865 * it depending on the inode mode.
2868 ret = send_rename(sctx, valid_path, cur->full_path);
2872 ret = fs_path_copy(valid_path, cur->full_path);
2876 if (S_ISDIR(sctx->cur_inode_mode)) {
2878 * Dirs can't be linked, so move it. For moved
2879 * dirs, we always have one new and one deleted
2880 * ref. The deleted ref is ignored later.
2882 ret = send_rename(sctx, valid_path,
2886 ret = fs_path_copy(valid_path, cur->full_path);
2890 ret = send_link(sctx, cur->full_path,
2896 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2902 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2904 * Check if we can already rmdir the directory. If not,
2905 * orphanize it. For every dir item inside that gets deleted
2906 * later, we do this check again and rmdir it then if possible.
2907 * See the use of check_dirs for more details.
2909 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2913 ret = send_rmdir(sctx, valid_path);
2916 } else if (!is_orphan) {
2917 ret = orphanize_inode(sctx, sctx->cur_ino,
2918 sctx->cur_inode_gen, valid_path);
2924 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2925 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2930 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2931 !list_empty(&sctx->deleted_refs)) {
2933 * We have a moved dir. Add the old parent to check_dirs
2935 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2937 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2941 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2943 * We have a non dir inode. Go through all deleted refs and
2944 * unlink them if they were not already overwritten by other
2947 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2948 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2949 sctx->cur_ino, sctx->cur_inode_gen,
2950 cur->name, cur->name_len);
2954 ret = send_unlink(sctx, cur->full_path);
2958 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2965 * If the inode is still orphan, unlink the orphan. This may
2966 * happen when a previous inode did overwrite the first ref
2967 * of this inode and no new refs were added for the current
2968 * inode. Unlinking does not mean that the inode is deleted in
2969 * all cases. There may still be links to this inode in other
2973 ret = send_unlink(sctx, valid_path);
2980 * We did collect all parent dirs where cur_inode was once located. We
2981 * now go through all these dirs and check if they are pending for
2982 * deletion and if it's finally possible to perform the rmdir now.
2983 * We also update the inode stats of the parent dirs here.
2985 ULIST_ITER_INIT(&uit);
2986 while ((un = ulist_next(check_dirs, &uit))) {
2988 * In case we had refs into dirs that were not processed yet,
2989 * we don't need to do the utime and rmdir logic for these dirs.
2990 * The dir will be processed later.
2992 if (un->val > sctx->cur_ino)
2995 ret = get_cur_inode_state(sctx, un->val, un->aux);
2999 if (ret == inode_state_did_create ||
3000 ret == inode_state_no_change) {
3001 /* TODO delayed utimes */
3002 ret = send_utimes(sctx, un->val, un->aux);
3005 } else if (ret == inode_state_did_delete) {
3006 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
3010 ret = get_cur_path(sctx, un->val, un->aux,
3014 ret = send_rmdir(sctx, valid_path);
3024 free_recorded_refs(sctx);
3025 ulist_free(check_dirs);
3026 fs_path_free(sctx, valid_path);
3030 static int __record_new_ref(int num, u64 dir, int index,
3031 struct fs_path *name,
3035 struct send_ctx *sctx = ctx;
3039 p = fs_path_alloc(sctx);
3043 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
3048 ret = get_cur_path(sctx, dir, gen, p);
3051 ret = fs_path_add_path(p, name);
3055 ret = record_ref(&sctx->new_refs, dir, gen, p);
3059 fs_path_free(sctx, p);
3063 static int __record_deleted_ref(int num, u64 dir, int index,
3064 struct fs_path *name,
3068 struct send_ctx *sctx = ctx;
3072 p = fs_path_alloc(sctx);
3076 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3081 ret = get_cur_path(sctx, dir, gen, p);
3084 ret = fs_path_add_path(p, name);
3088 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3092 fs_path_free(sctx, p);
3096 static int record_new_ref(struct send_ctx *sctx)
3100 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3101 sctx->cmp_key, 0, __record_new_ref, sctx);
3110 static int record_deleted_ref(struct send_ctx *sctx)
3114 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3115 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3124 struct find_ref_ctx {
3126 struct fs_path *name;
3130 static int __find_iref(int num, u64 dir, int index,
3131 struct fs_path *name,
3134 struct find_ref_ctx *ctx = ctx_;
3136 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3137 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3138 ctx->found_idx = num;
3144 static int find_iref(struct send_ctx *sctx,
3145 struct btrfs_root *root,
3146 struct btrfs_path *path,
3147 struct btrfs_key *key,
3148 u64 dir, struct fs_path *name)
3151 struct find_ref_ctx ctx;
3157 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3161 if (ctx.found_idx == -1)
3164 return ctx.found_idx;
3167 static int __record_changed_new_ref(int num, u64 dir, int index,
3168 struct fs_path *name,
3172 struct send_ctx *sctx = ctx;
3174 ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
3175 sctx->cmp_key, dir, name);
3177 ret = __record_new_ref(num, dir, index, name, sctx);
3184 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3185 struct fs_path *name,
3189 struct send_ctx *sctx = ctx;
3191 ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3194 ret = __record_deleted_ref(num, dir, index, name, sctx);
3201 static int record_changed_ref(struct send_ctx *sctx)
3205 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3206 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3209 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3210 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3220 * Record and process all refs at once. Needed when an inode changes the
3221 * generation number, which means that it was deleted and recreated.
3223 static int process_all_refs(struct send_ctx *sctx,
3224 enum btrfs_compare_tree_result cmd)
3227 struct btrfs_root *root;
3228 struct btrfs_path *path;
3229 struct btrfs_key key;
3230 struct btrfs_key found_key;
3231 struct extent_buffer *eb;
3233 iterate_inode_ref_t cb;
3235 path = alloc_path_for_send();
3239 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3240 root = sctx->send_root;
3241 cb = __record_new_ref;
3242 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3243 root = sctx->parent_root;
3244 cb = __record_deleted_ref;
3249 key.objectid = sctx->cmp_key->objectid;
3250 key.type = BTRFS_INODE_REF_KEY;
3253 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3259 eb = path->nodes[0];
3260 slot = path->slots[0];
3261 btrfs_item_key_to_cpu(eb, &found_key, slot);
3263 if (found_key.objectid != key.objectid ||
3264 (found_key.type != BTRFS_INODE_REF_KEY &&
3265 found_key.type != BTRFS_INODE_EXTREF_KEY))
3268 ret = iterate_inode_ref(sctx, root, path, &found_key, 0, cb,
3270 btrfs_release_path(path);
3274 key.offset = found_key.offset + 1;
3276 btrfs_release_path(path);
3278 ret = process_recorded_refs(sctx);
3281 btrfs_free_path(path);
3285 static int send_set_xattr(struct send_ctx *sctx,
3286 struct fs_path *path,
3287 const char *name, int name_len,
3288 const char *data, int data_len)
3292 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3296 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3297 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3298 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3300 ret = send_cmd(sctx);
3307 static int send_remove_xattr(struct send_ctx *sctx,
3308 struct fs_path *path,
3309 const char *name, int name_len)
3313 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3317 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3318 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3320 ret = send_cmd(sctx);
3327 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3328 const char *name, int name_len,
3329 const char *data, int data_len,
3333 struct send_ctx *sctx = ctx;
3335 posix_acl_xattr_header dummy_acl;
3337 p = fs_path_alloc(sctx);
3342 * This hack is needed because empty acl's are stored as zero byte
3343 * data in xattrs. Problem with that is, that receiving these zero byte
3344 * acl's will fail later. To fix this, we send a dummy acl list that
3345 * only contains the version number and no entries.
3347 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3348 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3349 if (data_len == 0) {
3350 dummy_acl.a_version =
3351 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3352 data = (char *)&dummy_acl;
3353 data_len = sizeof(dummy_acl);
3357 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3361 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3364 fs_path_free(sctx, p);
3368 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3369 const char *name, int name_len,
3370 const char *data, int data_len,
3374 struct send_ctx *sctx = ctx;
3377 p = fs_path_alloc(sctx);
3381 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3385 ret = send_remove_xattr(sctx, p, name, name_len);
3388 fs_path_free(sctx, p);
3392 static int process_new_xattr(struct send_ctx *sctx)
3396 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3397 sctx->cmp_key, __process_new_xattr, sctx);
3402 static int process_deleted_xattr(struct send_ctx *sctx)
3406 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3407 sctx->cmp_key, __process_deleted_xattr, sctx);
3412 struct find_xattr_ctx {
3420 static int __find_xattr(int num, struct btrfs_key *di_key,
3421 const char *name, int name_len,
3422 const char *data, int data_len,
3423 u8 type, void *vctx)
3425 struct find_xattr_ctx *ctx = vctx;
3427 if (name_len == ctx->name_len &&
3428 strncmp(name, ctx->name, name_len) == 0) {
3429 ctx->found_idx = num;
3430 ctx->found_data_len = data_len;
3431 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3432 if (!ctx->found_data)
3434 memcpy(ctx->found_data, data, data_len);
3440 static int find_xattr(struct send_ctx *sctx,
3441 struct btrfs_root *root,
3442 struct btrfs_path *path,
3443 struct btrfs_key *key,
3444 const char *name, int name_len,
3445 char **data, int *data_len)
3448 struct find_xattr_ctx ctx;
3451 ctx.name_len = name_len;
3453 ctx.found_data = NULL;
3454 ctx.found_data_len = 0;
3456 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3460 if (ctx.found_idx == -1)
3463 *data = ctx.found_data;
3464 *data_len = ctx.found_data_len;
3466 kfree(ctx.found_data);
3468 return ctx.found_idx;
3472 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3473 const char *name, int name_len,
3474 const char *data, int data_len,
3478 struct send_ctx *sctx = ctx;
3479 char *found_data = NULL;
3480 int found_data_len = 0;
3481 struct fs_path *p = NULL;
3483 ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
3484 sctx->cmp_key, name, name_len, &found_data,
3486 if (ret == -ENOENT) {
3487 ret = __process_new_xattr(num, di_key, name, name_len, data,
3488 data_len, type, ctx);
3489 } else if (ret >= 0) {
3490 if (data_len != found_data_len ||
3491 memcmp(data, found_data, data_len)) {
3492 ret = __process_new_xattr(num, di_key, name, name_len,
3493 data, data_len, type, ctx);
3500 fs_path_free(sctx, p);
3504 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3505 const char *name, int name_len,
3506 const char *data, int data_len,
3510 struct send_ctx *sctx = ctx;
3512 ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3513 name, name_len, NULL, NULL);
3515 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3516 data_len, type, ctx);
3523 static int process_changed_xattr(struct send_ctx *sctx)
3527 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3528 sctx->cmp_key, __process_changed_new_xattr, sctx);
3531 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3532 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3538 static int process_all_new_xattrs(struct send_ctx *sctx)
3541 struct btrfs_root *root;
3542 struct btrfs_path *path;
3543 struct btrfs_key key;
3544 struct btrfs_key found_key;
3545 struct extent_buffer *eb;
3548 path = alloc_path_for_send();
3552 root = sctx->send_root;
3554 key.objectid = sctx->cmp_key->objectid;
3555 key.type = BTRFS_XATTR_ITEM_KEY;
3558 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3566 eb = path->nodes[0];
3567 slot = path->slots[0];
3568 btrfs_item_key_to_cpu(eb, &found_key, slot);
3570 if (found_key.objectid != key.objectid ||
3571 found_key.type != key.type) {
3576 ret = iterate_dir_item(sctx, root, path, &found_key,
3577 __process_new_xattr, sctx);
3581 btrfs_release_path(path);
3582 key.offset = found_key.offset + 1;
3586 btrfs_free_path(path);
3591 * Read some bytes from the current inode/file and send a write command to
3594 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3598 loff_t pos = offset;
3600 mm_segment_t old_fs;
3602 p = fs_path_alloc(sctx);
3607 * vfs normally only accepts user space buffers for security reasons.
3608 * we only read from the file and also only provide the read_buf buffer
3609 * to vfs. As this buffer does not come from a user space call, it's
3610 * ok to temporary allow kernel space buffers.
3615 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3617 ret = open_cur_inode_file(sctx);
3621 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3628 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3632 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3636 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3637 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3638 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
3640 ret = send_cmd(sctx);
3644 fs_path_free(sctx, p);
3652 * Send a clone command to user space.
3654 static int send_clone(struct send_ctx *sctx,
3655 u64 offset, u32 len,
3656 struct clone_root *clone_root)
3662 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3663 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3664 clone_root->root->objectid, clone_root->ino,
3665 clone_root->offset);
3667 p = fs_path_alloc(sctx);
3671 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3675 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3679 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3680 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3681 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3683 if (clone_root->root == sctx->send_root) {
3684 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3685 &gen, NULL, NULL, NULL, NULL);
3688 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3690 ret = get_inode_path(sctx, clone_root->root,
3691 clone_root->ino, p);
3696 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3697 clone_root->root->root_item.uuid);
3698 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3699 clone_root->root->root_item.ctransid);
3700 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3701 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3702 clone_root->offset);
3704 ret = send_cmd(sctx);
3708 fs_path_free(sctx, p);
3712 static int send_write_or_clone(struct send_ctx *sctx,
3713 struct btrfs_path *path,
3714 struct btrfs_key *key,
3715 struct clone_root *clone_root)
3718 struct btrfs_file_extent_item *ei;
3719 u64 offset = key->offset;
3725 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3726 struct btrfs_file_extent_item);
3727 type = btrfs_file_extent_type(path->nodes[0], ei);
3728 if (type == BTRFS_FILE_EXTENT_INLINE) {
3729 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3731 * it is possible the inline item won't cover the whole page,
3732 * but there may be items after this page. Make
3733 * sure to send the whole thing
3735 len = PAGE_CACHE_ALIGN(len);
3737 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3740 if (offset + len > sctx->cur_inode_size)
3741 len = sctx->cur_inode_size - offset;
3750 if (l > BTRFS_SEND_READ_SIZE)
3751 l = BTRFS_SEND_READ_SIZE;
3752 ret = send_write(sctx, pos + offset, l);
3761 ret = send_clone(sctx, offset, len, clone_root);
3768 static int is_extent_unchanged(struct send_ctx *sctx,
3769 struct btrfs_path *left_path,
3770 struct btrfs_key *ekey)
3773 struct btrfs_key key;
3774 struct btrfs_path *path = NULL;
3775 struct extent_buffer *eb;
3777 struct btrfs_key found_key;
3778 struct btrfs_file_extent_item *ei;
3783 u64 left_offset_fixed;
3791 path = alloc_path_for_send();
3795 eb = left_path->nodes[0];
3796 slot = left_path->slots[0];
3797 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3798 left_type = btrfs_file_extent_type(eb, ei);
3800 if (left_type != BTRFS_FILE_EXTENT_REG) {
3804 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3805 left_len = btrfs_file_extent_num_bytes(eb, ei);
3806 left_offset = btrfs_file_extent_offset(eb, ei);
3807 left_gen = btrfs_file_extent_generation(eb, ei);
3810 * Following comments will refer to these graphics. L is the left
3811 * extents which we are checking at the moment. 1-8 are the right
3812 * extents that we iterate.
3815 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3818 * |--1--|-2b-|...(same as above)
3820 * Alternative situation. Happens on files where extents got split.
3822 * |-----------7-----------|-6-|
3824 * Alternative situation. Happens on files which got larger.
3827 * Nothing follows after 8.
3830 key.objectid = ekey->objectid;
3831 key.type = BTRFS_EXTENT_DATA_KEY;
3832 key.offset = ekey->offset;
3833 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3842 * Handle special case where the right side has no extents at all.
3844 eb = path->nodes[0];
3845 slot = path->slots[0];
3846 btrfs_item_key_to_cpu(eb, &found_key, slot);
3847 if (found_key.objectid != key.objectid ||
3848 found_key.type != key.type) {
3854 * We're now on 2a, 2b or 7.
3857 while (key.offset < ekey->offset + left_len) {
3858 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3859 right_type = btrfs_file_extent_type(eb, ei);
3860 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3861 right_len = btrfs_file_extent_num_bytes(eb, ei);
3862 right_offset = btrfs_file_extent_offset(eb, ei);
3863 right_gen = btrfs_file_extent_generation(eb, ei);
3865 if (right_type != BTRFS_FILE_EXTENT_REG) {
3871 * Are we at extent 8? If yes, we know the extent is changed.
3872 * This may only happen on the first iteration.
3874 if (found_key.offset + right_len <= ekey->offset) {
3879 left_offset_fixed = left_offset;
3880 if (key.offset < ekey->offset) {
3881 /* Fix the right offset for 2a and 7. */
3882 right_offset += ekey->offset - key.offset;
3884 /* Fix the left offset for all behind 2a and 2b */
3885 left_offset_fixed += key.offset - ekey->offset;
3889 * Check if we have the same extent.
3891 if (left_disknr != right_disknr ||
3892 left_offset_fixed != right_offset ||
3893 left_gen != right_gen) {
3899 * Go to the next extent.
3901 ret = btrfs_next_item(sctx->parent_root, path);
3905 eb = path->nodes[0];
3906 slot = path->slots[0];
3907 btrfs_item_key_to_cpu(eb, &found_key, slot);
3909 if (ret || found_key.objectid != key.objectid ||
3910 found_key.type != key.type) {
3911 key.offset += right_len;
3914 if (found_key.offset != key.offset + right_len) {
3915 /* Should really not happen */
3924 * We're now behind the left extent (treat as unchanged) or at the end
3925 * of the right side (treat as changed).
3927 if (key.offset >= ekey->offset + left_len)
3934 btrfs_free_path(path);
3938 static int process_extent(struct send_ctx *sctx,
3939 struct btrfs_path *path,
3940 struct btrfs_key *key)
3943 struct clone_root *found_clone = NULL;
3945 if (S_ISLNK(sctx->cur_inode_mode))
3948 if (sctx->parent_root && !sctx->cur_inode_new) {
3949 ret = is_extent_unchanged(sctx, path, key);
3958 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3959 sctx->cur_inode_size, &found_clone);
3960 if (ret != -ENOENT && ret < 0)
3963 ret = send_write_or_clone(sctx, path, key, found_clone);
3969 static int process_all_extents(struct send_ctx *sctx)
3972 struct btrfs_root *root;
3973 struct btrfs_path *path;
3974 struct btrfs_key key;
3975 struct btrfs_key found_key;
3976 struct extent_buffer *eb;
3979 root = sctx->send_root;
3980 path = alloc_path_for_send();
3984 key.objectid = sctx->cmp_key->objectid;
3985 key.type = BTRFS_EXTENT_DATA_KEY;
3988 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3996 eb = path->nodes[0];
3997 slot = path->slots[0];
3998 btrfs_item_key_to_cpu(eb, &found_key, slot);
4000 if (found_key.objectid != key.objectid ||
4001 found_key.type != key.type) {
4006 ret = process_extent(sctx, path, &found_key);
4010 btrfs_release_path(path);
4011 key.offset = found_key.offset + 1;
4015 btrfs_free_path(path);
4019 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
4023 if (sctx->cur_ino == 0)
4025 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4026 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4028 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4031 ret = process_recorded_refs(sctx);
4036 * We have processed the refs and thus need to advance send_progress.
4037 * Now, calls to get_cur_xxx will take the updated refs of the current
4038 * inode into account.
4040 sctx->send_progress = sctx->cur_ino + 1;
4046 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4058 ret = process_recorded_refs_if_needed(sctx, at_end);
4062 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4064 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4067 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4068 &left_mode, &left_uid, &left_gid, NULL);
4072 if (!sctx->parent_root || sctx->cur_inode_new) {
4074 if (!S_ISLNK(sctx->cur_inode_mode))
4077 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4078 NULL, NULL, &right_mode, &right_uid,
4083 if (left_uid != right_uid || left_gid != right_gid)
4085 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4089 if (S_ISREG(sctx->cur_inode_mode)) {
4090 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4091 sctx->cur_inode_size);
4097 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4098 left_uid, left_gid);
4103 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4110 * Need to send that every time, no matter if it actually changed
4111 * between the two trees as we have done changes to the inode before.
4113 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4121 static int changed_inode(struct send_ctx *sctx,
4122 enum btrfs_compare_tree_result result)
4125 struct btrfs_key *key = sctx->cmp_key;
4126 struct btrfs_inode_item *left_ii = NULL;
4127 struct btrfs_inode_item *right_ii = NULL;
4131 ret = close_cur_inode_file(sctx);
4135 sctx->cur_ino = key->objectid;
4136 sctx->cur_inode_new_gen = 0;
4139 * Set send_progress to current inode. This will tell all get_cur_xxx
4140 * functions that the current inode's refs are not updated yet. Later,
4141 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4143 sctx->send_progress = sctx->cur_ino;
4145 if (result == BTRFS_COMPARE_TREE_NEW ||
4146 result == BTRFS_COMPARE_TREE_CHANGED) {
4147 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4148 sctx->left_path->slots[0],
4149 struct btrfs_inode_item);
4150 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4153 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4154 sctx->right_path->slots[0],
4155 struct btrfs_inode_item);
4156 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4159 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4160 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4161 sctx->right_path->slots[0],
4162 struct btrfs_inode_item);
4164 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4168 * The cur_ino = root dir case is special here. We can't treat
4169 * the inode as deleted+reused because it would generate a
4170 * stream that tries to delete/mkdir the root dir.
4172 if (left_gen != right_gen &&
4173 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4174 sctx->cur_inode_new_gen = 1;
4177 if (result == BTRFS_COMPARE_TREE_NEW) {
4178 sctx->cur_inode_gen = left_gen;
4179 sctx->cur_inode_new = 1;
4180 sctx->cur_inode_deleted = 0;
4181 sctx->cur_inode_size = btrfs_inode_size(
4182 sctx->left_path->nodes[0], left_ii);
4183 sctx->cur_inode_mode = btrfs_inode_mode(
4184 sctx->left_path->nodes[0], left_ii);
4185 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4186 ret = send_create_inode_if_needed(sctx);
4187 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4188 sctx->cur_inode_gen = right_gen;
4189 sctx->cur_inode_new = 0;
4190 sctx->cur_inode_deleted = 1;
4191 sctx->cur_inode_size = btrfs_inode_size(
4192 sctx->right_path->nodes[0], right_ii);
4193 sctx->cur_inode_mode = btrfs_inode_mode(
4194 sctx->right_path->nodes[0], right_ii);
4195 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4197 * We need to do some special handling in case the inode was
4198 * reported as changed with a changed generation number. This
4199 * means that the original inode was deleted and new inode
4200 * reused the same inum. So we have to treat the old inode as
4201 * deleted and the new one as new.
4203 if (sctx->cur_inode_new_gen) {
4205 * First, process the inode as if it was deleted.
4207 sctx->cur_inode_gen = right_gen;
4208 sctx->cur_inode_new = 0;
4209 sctx->cur_inode_deleted = 1;
4210 sctx->cur_inode_size = btrfs_inode_size(
4211 sctx->right_path->nodes[0], right_ii);
4212 sctx->cur_inode_mode = btrfs_inode_mode(
4213 sctx->right_path->nodes[0], right_ii);
4214 ret = process_all_refs(sctx,
4215 BTRFS_COMPARE_TREE_DELETED);
4220 * Now process the inode as if it was new.
4222 sctx->cur_inode_gen = left_gen;
4223 sctx->cur_inode_new = 1;
4224 sctx->cur_inode_deleted = 0;
4225 sctx->cur_inode_size = btrfs_inode_size(
4226 sctx->left_path->nodes[0], left_ii);
4227 sctx->cur_inode_mode = btrfs_inode_mode(
4228 sctx->left_path->nodes[0], left_ii);
4229 ret = send_create_inode_if_needed(sctx);
4233 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4237 * Advance send_progress now as we did not get into
4238 * process_recorded_refs_if_needed in the new_gen case.
4240 sctx->send_progress = sctx->cur_ino + 1;
4243 * Now process all extents and xattrs of the inode as if
4244 * they were all new.
4246 ret = process_all_extents(sctx);
4249 ret = process_all_new_xattrs(sctx);
4253 sctx->cur_inode_gen = left_gen;
4254 sctx->cur_inode_new = 0;
4255 sctx->cur_inode_new_gen = 0;
4256 sctx->cur_inode_deleted = 0;
4257 sctx->cur_inode_size = btrfs_inode_size(
4258 sctx->left_path->nodes[0], left_ii);
4259 sctx->cur_inode_mode = btrfs_inode_mode(
4260 sctx->left_path->nodes[0], left_ii);
4269 * We have to process new refs before deleted refs, but compare_trees gives us
4270 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4271 * first and later process them in process_recorded_refs.
4272 * For the cur_inode_new_gen case, we skip recording completely because
4273 * changed_inode did already initiate processing of refs. The reason for this is
4274 * that in this case, compare_tree actually compares the refs of 2 different
4275 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4276 * refs of the right tree as deleted and all refs of the left tree as new.
4278 static int changed_ref(struct send_ctx *sctx,
4279 enum btrfs_compare_tree_result result)
4283 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4285 if (!sctx->cur_inode_new_gen &&
4286 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4287 if (result == BTRFS_COMPARE_TREE_NEW)
4288 ret = record_new_ref(sctx);
4289 else if (result == BTRFS_COMPARE_TREE_DELETED)
4290 ret = record_deleted_ref(sctx);
4291 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4292 ret = record_changed_ref(sctx);
4299 * Process new/deleted/changed xattrs. We skip processing in the
4300 * cur_inode_new_gen case because changed_inode did already initiate processing
4301 * of xattrs. The reason is the same as in changed_ref
4303 static int changed_xattr(struct send_ctx *sctx,
4304 enum btrfs_compare_tree_result result)
4308 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4310 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4311 if (result == BTRFS_COMPARE_TREE_NEW)
4312 ret = process_new_xattr(sctx);
4313 else if (result == BTRFS_COMPARE_TREE_DELETED)
4314 ret = process_deleted_xattr(sctx);
4315 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4316 ret = process_changed_xattr(sctx);
4323 * Process new/deleted/changed extents. We skip processing in the
4324 * cur_inode_new_gen case because changed_inode did already initiate processing
4325 * of extents. The reason is the same as in changed_ref
4327 static int changed_extent(struct send_ctx *sctx,
4328 enum btrfs_compare_tree_result result)
4332 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4334 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4335 if (result != BTRFS_COMPARE_TREE_DELETED)
4336 ret = process_extent(sctx, sctx->left_path,
4344 * Updates compare related fields in sctx and simply forwards to the actual
4345 * changed_xxx functions.
4347 static int changed_cb(struct btrfs_root *left_root,
4348 struct btrfs_root *right_root,
4349 struct btrfs_path *left_path,
4350 struct btrfs_path *right_path,
4351 struct btrfs_key *key,
4352 enum btrfs_compare_tree_result result,
4356 struct send_ctx *sctx = ctx;
4358 sctx->left_path = left_path;
4359 sctx->right_path = right_path;
4360 sctx->cmp_key = key;
4362 ret = finish_inode_if_needed(sctx, 0);
4366 /* Ignore non-FS objects */
4367 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4368 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4371 if (key->type == BTRFS_INODE_ITEM_KEY)
4372 ret = changed_inode(sctx, result);
4373 else if (key->type == BTRFS_INODE_REF_KEY ||
4374 key->type == BTRFS_INODE_EXTREF_KEY)
4375 ret = changed_ref(sctx, result);
4376 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4377 ret = changed_xattr(sctx, result);
4378 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4379 ret = changed_extent(sctx, result);
4385 static int full_send_tree(struct send_ctx *sctx)
4388 struct btrfs_trans_handle *trans = NULL;
4389 struct btrfs_root *send_root = sctx->send_root;
4390 struct btrfs_key key;
4391 struct btrfs_key found_key;
4392 struct btrfs_path *path;
4393 struct extent_buffer *eb;
4398 path = alloc_path_for_send();
4402 spin_lock(&send_root->root_item_lock);
4403 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4404 spin_unlock(&send_root->root_item_lock);
4406 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4407 key.type = BTRFS_INODE_ITEM_KEY;
4412 * We need to make sure the transaction does not get committed
4413 * while we do anything on commit roots. Join a transaction to prevent
4416 trans = btrfs_join_transaction(send_root);
4417 if (IS_ERR(trans)) {
4418 ret = PTR_ERR(trans);
4424 * Make sure the tree has not changed after re-joining. We detect this
4425 * by comparing start_ctransid and ctransid. They should always match.
4427 spin_lock(&send_root->root_item_lock);
4428 ctransid = btrfs_root_ctransid(&send_root->root_item);
4429 spin_unlock(&send_root->root_item_lock);
4431 if (ctransid != start_ctransid) {
4432 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4433 "send was modified in between. This is "
4434 "probably a bug.\n");
4439 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4447 * When someone want to commit while we iterate, end the
4448 * joined transaction and rejoin.
4450 if (btrfs_should_end_transaction(trans, send_root)) {
4451 ret = btrfs_end_transaction(trans, send_root);
4455 btrfs_release_path(path);
4459 eb = path->nodes[0];
4460 slot = path->slots[0];
4461 btrfs_item_key_to_cpu(eb, &found_key, slot);
4463 ret = changed_cb(send_root, NULL, path, NULL,
4464 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4468 key.objectid = found_key.objectid;
4469 key.type = found_key.type;
4470 key.offset = found_key.offset + 1;
4472 ret = btrfs_next_item(send_root, path);
4482 ret = finish_inode_if_needed(sctx, 1);
4485 btrfs_free_path(path);
4488 ret = btrfs_end_transaction(trans, send_root);
4490 btrfs_end_transaction(trans, send_root);
4495 static int send_subvol(struct send_ctx *sctx)
4499 ret = send_header(sctx);
4503 ret = send_subvol_begin(sctx);
4507 if (sctx->parent_root) {
4508 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4512 ret = finish_inode_if_needed(sctx, 1);
4516 ret = full_send_tree(sctx);
4523 ret = close_cur_inode_file(sctx);
4525 close_cur_inode_file(sctx);
4527 free_recorded_refs(sctx);
4531 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4534 struct btrfs_root *send_root;
4535 struct btrfs_root *clone_root;
4536 struct btrfs_fs_info *fs_info;
4537 struct btrfs_ioctl_send_args *arg = NULL;
4538 struct btrfs_key key;
4539 struct file *filp = NULL;
4540 struct send_ctx *sctx = NULL;
4542 u64 *clone_sources_tmp = NULL;
4544 if (!capable(CAP_SYS_ADMIN))
4547 send_root = BTRFS_I(file_inode(mnt_file))->root;
4548 fs_info = send_root->fs_info;
4550 arg = memdup_user(arg_, sizeof(*arg));
4557 if (!access_ok(VERIFY_READ, arg->clone_sources,
4558 sizeof(*arg->clone_sources *
4559 arg->clone_sources_count))) {
4564 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4570 INIT_LIST_HEAD(&sctx->new_refs);
4571 INIT_LIST_HEAD(&sctx->deleted_refs);
4572 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4573 INIT_LIST_HEAD(&sctx->name_cache_list);
4575 sctx->send_filp = fget(arg->send_fd);
4576 if (IS_ERR(sctx->send_filp)) {
4577 ret = PTR_ERR(sctx->send_filp);
4581 sctx->mnt = mnt_file->f_path.mnt;
4583 sctx->send_root = send_root;
4584 sctx->clone_roots_cnt = arg->clone_sources_count;
4586 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4587 sctx->send_buf = vmalloc(sctx->send_max_size);
4588 if (!sctx->send_buf) {
4593 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4594 if (!sctx->read_buf) {
4599 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4600 (arg->clone_sources_count + 1));
4601 if (!sctx->clone_roots) {
4606 if (arg->clone_sources_count) {
4607 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4608 sizeof(*arg->clone_sources));
4609 if (!clone_sources_tmp) {
4614 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4615 arg->clone_sources_count *
4616 sizeof(*arg->clone_sources));
4622 for (i = 0; i < arg->clone_sources_count; i++) {
4623 key.objectid = clone_sources_tmp[i];
4624 key.type = BTRFS_ROOT_ITEM_KEY;
4625 key.offset = (u64)-1;
4626 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4631 if (IS_ERR(clone_root)) {
4632 ret = PTR_ERR(clone_root);
4635 sctx->clone_roots[i].root = clone_root;
4637 vfree(clone_sources_tmp);
4638 clone_sources_tmp = NULL;
4641 if (arg->parent_root) {
4642 key.objectid = arg->parent_root;
4643 key.type = BTRFS_ROOT_ITEM_KEY;
4644 key.offset = (u64)-1;
4645 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4646 if (!sctx->parent_root) {
4653 * Clones from send_root are allowed, but only if the clone source
4654 * is behind the current send position. This is checked while searching
4655 * for possible clone sources.
4657 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4659 /* We do a bsearch later */
4660 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4661 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4664 ret = send_subvol(sctx);
4668 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4671 ret = send_cmd(sctx);
4679 vfree(clone_sources_tmp);
4682 if (sctx->send_filp)
4683 fput(sctx->send_filp);
4685 vfree(sctx->clone_roots);
4686 vfree(sctx->send_buf);
4687 vfree(sctx->read_buf);
4689 name_cache_free(sctx);