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>
29 #include <linux/string.h>
35 #include "btrfs_inode.h"
36 #include "transaction.h"
38 static int g_verbose = 0;
40 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
43 * A fs_path is a helper to dynamically build path names with unknown size.
44 * It reallocates the internal buffer on demand.
45 * It allows fast adding of path elements on the right side (normal path) and
46 * fast adding to the left side (reversed path). A reversed path can also be
47 * unreversed if needed.
58 unsigned int reversed:1;
59 unsigned int virtual_mem:1;
65 #define FS_PATH_INLINE_SIZE \
66 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
69 /* reused for each extent */
71 struct btrfs_root *root;
78 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
79 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
82 struct file *send_filp;
88 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
89 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
93 struct btrfs_root *send_root;
94 struct btrfs_root *parent_root;
95 struct clone_root *clone_roots;
98 /* current state of the compare_tree call */
99 struct btrfs_path *left_path;
100 struct btrfs_path *right_path;
101 struct btrfs_key *cmp_key;
104 * infos of the currently processed inode. In case of deleted inodes,
105 * these are the values from the deleted inode.
110 int cur_inode_new_gen;
111 int cur_inode_deleted;
117 struct list_head new_refs;
118 struct list_head deleted_refs;
120 struct radix_tree_root name_cache;
121 struct list_head name_cache_list;
124 struct file *cur_inode_filp;
128 struct name_cache_entry {
129 struct list_head list;
131 * radix_tree has only 32bit entries but we need to handle 64bit inums.
132 * We use the lower 32bit of the 64bit inum to store it in the tree. If
133 * more then one inum would fall into the same entry, we use radix_list
134 * to store the additional entries. radix_list is also used to store
135 * entries where two entries have the same inum but different
138 struct list_head radix_list;
144 int need_later_update;
149 static void fs_path_reset(struct fs_path *p)
152 p->start = p->buf + p->buf_len - 1;
162 static struct fs_path *fs_path_alloc(void)
166 p = kmalloc(sizeof(*p), GFP_NOFS);
171 p->buf = p->inline_buf;
172 p->buf_len = FS_PATH_INLINE_SIZE;
177 static struct fs_path *fs_path_alloc_reversed(void)
189 static void fs_path_free(struct fs_path *p)
193 if (p->buf != p->inline_buf) {
202 static int fs_path_len(struct fs_path *p)
204 return p->end - p->start;
207 static int fs_path_ensure_buf(struct fs_path *p, int len)
215 if (p->buf_len >= len)
218 path_len = p->end - p->start;
219 old_buf_len = p->buf_len;
220 len = PAGE_ALIGN(len);
222 if (p->buf == p->inline_buf) {
223 tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN);
225 tmp_buf = vmalloc(len);
230 memcpy(tmp_buf, p->buf, p->buf_len);
234 if (p->virtual_mem) {
235 tmp_buf = vmalloc(len);
238 memcpy(tmp_buf, p->buf, p->buf_len);
241 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
243 tmp_buf = vmalloc(len);
246 memcpy(tmp_buf, p->buf, p->buf_len);
255 tmp_buf = p->buf + old_buf_len - path_len - 1;
256 p->end = p->buf + p->buf_len - 1;
257 p->start = p->end - path_len;
258 memmove(p->start, tmp_buf, path_len + 1);
261 p->end = p->start + path_len;
266 static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
271 new_len = p->end - p->start + name_len;
272 if (p->start != p->end)
274 ret = fs_path_ensure_buf(p, new_len);
279 if (p->start != p->end)
281 p->start -= name_len;
282 p->prepared = p->start;
284 if (p->start != p->end)
286 p->prepared = p->end;
295 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
299 ret = fs_path_prepare_for_add(p, name_len);
302 memcpy(p->prepared, name, name_len);
309 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
313 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
316 memcpy(p->prepared, p2->start, p2->end - p2->start);
323 static int fs_path_add_from_extent_buffer(struct fs_path *p,
324 struct extent_buffer *eb,
325 unsigned long off, int len)
329 ret = fs_path_prepare_for_add(p, len);
333 read_extent_buffer(eb, p->prepared, off, len);
341 static void fs_path_remove(struct fs_path *p)
344 while (p->start != p->end && *p->end != '/')
350 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
354 p->reversed = from->reversed;
357 ret = fs_path_add_path(p, from);
363 static void fs_path_unreverse(struct fs_path *p)
372 len = p->end - p->start;
374 p->end = p->start + len;
375 memmove(p->start, tmp, len + 1);
379 static struct btrfs_path *alloc_path_for_send(void)
381 struct btrfs_path *path;
383 path = btrfs_alloc_path();
386 path->search_commit_root = 1;
387 path->skip_locking = 1;
391 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
401 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
402 /* TODO handle that correctly */
403 /*if (ret == -ERESTARTSYS) {
422 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
424 struct btrfs_tlv_header *hdr;
425 int total_len = sizeof(*hdr) + len;
426 int left = sctx->send_max_size - sctx->send_size;
428 if (unlikely(left < total_len))
431 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
432 hdr->tlv_type = cpu_to_le16(attr);
433 hdr->tlv_len = cpu_to_le16(len);
434 memcpy(hdr + 1, data, len);
435 sctx->send_size += total_len;
441 static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
443 return tlv_put(sctx, attr, &value, sizeof(value));
446 static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
448 __le16 tmp = cpu_to_le16(value);
449 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
452 static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
454 __le32 tmp = cpu_to_le32(value);
455 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
459 static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
461 __le64 tmp = cpu_to_le64(value);
462 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
465 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
466 const char *str, int len)
470 return tlv_put(sctx, attr, str, len);
473 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
476 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
480 static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
483 struct btrfs_timespec bts;
484 bts.sec = cpu_to_le64(ts->tv_sec);
485 bts.nsec = cpu_to_le32(ts->tv_nsec);
486 return tlv_put(sctx, attr, &bts, sizeof(bts));
490 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
491 struct extent_buffer *eb,
492 struct btrfs_timespec *ts)
494 struct btrfs_timespec bts;
495 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
496 return tlv_put(sctx, attr, &bts, sizeof(bts));
500 #define TLV_PUT(sctx, attrtype, attrlen, data) \
502 ret = tlv_put(sctx, attrtype, attrlen, data); \
504 goto tlv_put_failure; \
507 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
509 ret = tlv_put_u##bits(sctx, attrtype, value); \
511 goto tlv_put_failure; \
514 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
515 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
516 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
517 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
518 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
520 ret = tlv_put_string(sctx, attrtype, str, len); \
522 goto tlv_put_failure; \
524 #define TLV_PUT_PATH(sctx, attrtype, p) \
526 ret = tlv_put_string(sctx, attrtype, p->start, \
527 p->end - p->start); \
529 goto tlv_put_failure; \
531 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
533 ret = tlv_put_uuid(sctx, attrtype, uuid); \
535 goto tlv_put_failure; \
537 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
539 ret = tlv_put_timespec(sctx, attrtype, ts); \
541 goto tlv_put_failure; \
543 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
545 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
547 goto tlv_put_failure; \
550 static int send_header(struct send_ctx *sctx)
552 struct btrfs_stream_header hdr;
554 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
555 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
557 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
562 * For each command/item we want to send to userspace, we call this function.
564 static int begin_cmd(struct send_ctx *sctx, int cmd)
566 struct btrfs_cmd_header *hdr;
568 if (!sctx->send_buf) {
573 BUG_ON(sctx->send_size);
575 sctx->send_size += sizeof(*hdr);
576 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
577 hdr->cmd = cpu_to_le16(cmd);
582 static int send_cmd(struct send_ctx *sctx)
585 struct btrfs_cmd_header *hdr;
588 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
589 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
592 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
593 hdr->crc = cpu_to_le32(crc);
595 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
598 sctx->total_send_size += sctx->send_size;
599 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
606 * Sends a move instruction to user space
608 static int send_rename(struct send_ctx *sctx,
609 struct fs_path *from, struct fs_path *to)
613 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
615 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
619 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
620 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
622 ret = send_cmd(sctx);
630 * Sends a link instruction to user space
632 static int send_link(struct send_ctx *sctx,
633 struct fs_path *path, struct fs_path *lnk)
637 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
639 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
643 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
644 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
646 ret = send_cmd(sctx);
654 * Sends an unlink instruction to user space
656 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
660 verbose_printk("btrfs: send_unlink %s\n", path->start);
662 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
666 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
668 ret = send_cmd(sctx);
676 * Sends a rmdir instruction to user space
678 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
682 verbose_printk("btrfs: send_rmdir %s\n", path->start);
684 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
688 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
690 ret = send_cmd(sctx);
698 * Helper function to retrieve some fields from an inode item.
700 static int get_inode_info(struct btrfs_root *root,
701 u64 ino, u64 *size, u64 *gen,
702 u64 *mode, u64 *uid, u64 *gid,
706 struct btrfs_inode_item *ii;
707 struct btrfs_key key;
708 struct btrfs_path *path;
710 path = alloc_path_for_send();
715 key.type = BTRFS_INODE_ITEM_KEY;
717 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
725 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
726 struct btrfs_inode_item);
728 *size = btrfs_inode_size(path->nodes[0], ii);
730 *gen = btrfs_inode_generation(path->nodes[0], ii);
732 *mode = btrfs_inode_mode(path->nodes[0], ii);
734 *uid = btrfs_inode_uid(path->nodes[0], ii);
736 *gid = btrfs_inode_gid(path->nodes[0], ii);
738 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
741 btrfs_free_path(path);
745 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
750 * Helper function to iterate the entries in ONE btrfs_inode_ref or
751 * btrfs_inode_extref.
752 * The iterate callback may return a non zero value to stop iteration. This can
753 * be a negative value for error codes or 1 to simply stop it.
755 * path must point to the INODE_REF or INODE_EXTREF when called.
757 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
758 struct btrfs_key *found_key, int resolve,
759 iterate_inode_ref_t iterate, void *ctx)
761 struct extent_buffer *eb = path->nodes[0];
762 struct btrfs_item *item;
763 struct btrfs_inode_ref *iref;
764 struct btrfs_inode_extref *extref;
765 struct btrfs_path *tmp_path;
769 int slot = path->slots[0];
776 unsigned long name_off;
777 unsigned long elem_size;
780 p = fs_path_alloc_reversed();
784 tmp_path = alloc_path_for_send();
791 if (found_key->type == BTRFS_INODE_REF_KEY) {
792 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
793 struct btrfs_inode_ref);
794 item = btrfs_item_nr(eb, slot);
795 total = btrfs_item_size(eb, item);
796 elem_size = sizeof(*iref);
798 ptr = btrfs_item_ptr_offset(eb, slot);
799 total = btrfs_item_size_nr(eb, slot);
800 elem_size = sizeof(*extref);
803 while (cur < total) {
806 if (found_key->type == BTRFS_INODE_REF_KEY) {
807 iref = (struct btrfs_inode_ref *)(ptr + cur);
808 name_len = btrfs_inode_ref_name_len(eb, iref);
809 name_off = (unsigned long)(iref + 1);
810 index = btrfs_inode_ref_index(eb, iref);
811 dir = found_key->offset;
813 extref = (struct btrfs_inode_extref *)(ptr + cur);
814 name_len = btrfs_inode_extref_name_len(eb, extref);
815 name_off = (unsigned long)&extref->name;
816 index = btrfs_inode_extref_index(eb, extref);
817 dir = btrfs_inode_extref_parent(eb, extref);
821 start = btrfs_ref_to_path(root, tmp_path, name_len,
825 ret = PTR_ERR(start);
828 if (start < p->buf) {
829 /* overflow , try again with larger buffer */
830 ret = fs_path_ensure_buf(p,
831 p->buf_len + p->buf - start);
834 start = btrfs_ref_to_path(root, tmp_path,
839 ret = PTR_ERR(start);
842 BUG_ON(start < p->buf);
846 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
852 cur += elem_size + name_len;
853 ret = iterate(num, dir, index, p, ctx);
860 btrfs_free_path(tmp_path);
865 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
866 const char *name, int name_len,
867 const char *data, int data_len,
871 * Helper function to iterate the entries in ONE btrfs_dir_item.
872 * The iterate callback may return a non zero value to stop iteration. This can
873 * be a negative value for error codes or 1 to simply stop it.
875 * path must point to the dir item when called.
877 static int iterate_dir_item(struct 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 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(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 btrfs_root *root,
1318 struct fs_path *dest)
1321 struct btrfs_path *path;
1322 struct btrfs_key key;
1323 struct btrfs_file_extent_item *ei;
1329 path = alloc_path_for_send();
1334 key.type = BTRFS_EXTENT_DATA_KEY;
1336 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1341 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1342 struct btrfs_file_extent_item);
1343 type = btrfs_file_extent_type(path->nodes[0], ei);
1344 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1345 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1346 BUG_ON(compression);
1348 off = btrfs_file_extent_inline_start(ei);
1349 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1351 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1354 btrfs_free_path(path);
1359 * Helper function to generate a file name that is unique in the root of
1360 * send_root and parent_root. This is used to generate names for orphan inodes.
1362 static int gen_unique_name(struct send_ctx *sctx,
1364 struct fs_path *dest)
1367 struct btrfs_path *path;
1368 struct btrfs_dir_item *di;
1373 path = alloc_path_for_send();
1378 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1380 if (len >= sizeof(tmp)) {
1381 /* should really not happen */
1386 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1387 path, BTRFS_FIRST_FREE_OBJECTID,
1388 tmp, strlen(tmp), 0);
1389 btrfs_release_path(path);
1395 /* not unique, try again */
1400 if (!sctx->parent_root) {
1406 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1407 path, BTRFS_FIRST_FREE_OBJECTID,
1408 tmp, strlen(tmp), 0);
1409 btrfs_release_path(path);
1415 /* not unique, try again */
1423 ret = fs_path_add(dest, tmp, strlen(tmp));
1426 btrfs_free_path(path);
1431 inode_state_no_change,
1432 inode_state_will_create,
1433 inode_state_did_create,
1434 inode_state_will_delete,
1435 inode_state_did_delete,
1438 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1446 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1448 if (ret < 0 && ret != -ENOENT)
1452 if (!sctx->parent_root) {
1453 right_ret = -ENOENT;
1455 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1456 NULL, NULL, NULL, NULL);
1457 if (ret < 0 && ret != -ENOENT)
1462 if (!left_ret && !right_ret) {
1463 if (left_gen == gen && right_gen == gen) {
1464 ret = inode_state_no_change;
1465 } else if (left_gen == gen) {
1466 if (ino < sctx->send_progress)
1467 ret = inode_state_did_create;
1469 ret = inode_state_will_create;
1470 } else if (right_gen == gen) {
1471 if (ino < sctx->send_progress)
1472 ret = inode_state_did_delete;
1474 ret = inode_state_will_delete;
1478 } else if (!left_ret) {
1479 if (left_gen == gen) {
1480 if (ino < sctx->send_progress)
1481 ret = inode_state_did_create;
1483 ret = inode_state_will_create;
1487 } else if (!right_ret) {
1488 if (right_gen == gen) {
1489 if (ino < sctx->send_progress)
1490 ret = inode_state_did_delete;
1492 ret = inode_state_will_delete;
1504 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1508 ret = get_cur_inode_state(sctx, ino, gen);
1512 if (ret == inode_state_no_change ||
1513 ret == inode_state_did_create ||
1514 ret == inode_state_will_delete)
1524 * Helper function to lookup a dir item in a dir.
1526 static int lookup_dir_item_inode(struct btrfs_root *root,
1527 u64 dir, const char *name, int name_len,
1532 struct btrfs_dir_item *di;
1533 struct btrfs_key key;
1534 struct btrfs_path *path;
1536 path = alloc_path_for_send();
1540 di = btrfs_lookup_dir_item(NULL, root, path,
1541 dir, name, name_len, 0);
1550 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1551 *found_inode = key.objectid;
1552 *found_type = btrfs_dir_type(path->nodes[0], di);
1555 btrfs_free_path(path);
1560 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1561 * generation of the parent dir and the name of the dir entry.
1563 static int get_first_ref(struct btrfs_root *root, u64 ino,
1564 u64 *dir, u64 *dir_gen, struct fs_path *name)
1567 struct btrfs_key key;
1568 struct btrfs_key found_key;
1569 struct btrfs_path *path;
1573 path = alloc_path_for_send();
1578 key.type = BTRFS_INODE_REF_KEY;
1581 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1585 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1587 if (ret || found_key.objectid != ino ||
1588 (found_key.type != BTRFS_INODE_REF_KEY &&
1589 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1594 if (key.type == BTRFS_INODE_REF_KEY) {
1595 struct btrfs_inode_ref *iref;
1596 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1597 struct btrfs_inode_ref);
1598 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1599 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1600 (unsigned long)(iref + 1),
1602 parent_dir = found_key.offset;
1604 struct btrfs_inode_extref *extref;
1605 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1606 struct btrfs_inode_extref);
1607 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1608 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1609 (unsigned long)&extref->name, len);
1610 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1614 btrfs_release_path(path);
1616 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
1624 btrfs_free_path(path);
1628 static int is_first_ref(struct btrfs_root *root,
1630 const char *name, int name_len)
1633 struct fs_path *tmp_name;
1637 tmp_name = fs_path_alloc();
1641 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1645 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1650 ret = !memcmp(tmp_name->start, name, name_len);
1653 fs_path_free(tmp_name);
1658 * Used by process_recorded_refs to determine if a new ref would overwrite an
1659 * already existing ref. In case it detects an overwrite, it returns the
1660 * inode/gen in who_ino/who_gen.
1661 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1662 * to make sure later references to the overwritten inode are possible.
1663 * Orphanizing is however only required for the first ref of an inode.
1664 * process_recorded_refs does an additional is_first_ref check to see if
1665 * orphanizing is really required.
1667 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1668 const char *name, int name_len,
1669 u64 *who_ino, u64 *who_gen)
1673 u64 other_inode = 0;
1676 if (!sctx->parent_root)
1679 ret = is_inode_existent(sctx, dir, dir_gen);
1684 * If we have a parent root we need to verify that the parent dir was
1685 * not delted and then re-created, if it was then we have no overwrite
1686 * and we can just unlink this entry.
1688 if (sctx->parent_root) {
1689 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1691 if (ret < 0 && ret != -ENOENT)
1701 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1702 &other_inode, &other_type);
1703 if (ret < 0 && ret != -ENOENT)
1711 * Check if the overwritten ref was already processed. If yes, the ref
1712 * was already unlinked/moved, so we can safely assume that we will not
1713 * overwrite anything at this point in time.
1715 if (other_inode > sctx->send_progress) {
1716 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1717 who_gen, NULL, NULL, NULL, NULL);
1722 *who_ino = other_inode;
1732 * Checks if the ref was overwritten by an already processed inode. This is
1733 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1734 * thus the orphan name needs be used.
1735 * process_recorded_refs also uses it to avoid unlinking of refs that were
1738 static int did_overwrite_ref(struct send_ctx *sctx,
1739 u64 dir, u64 dir_gen,
1740 u64 ino, u64 ino_gen,
1741 const char *name, int name_len)
1748 if (!sctx->parent_root)
1751 ret = is_inode_existent(sctx, dir, dir_gen);
1755 /* check if the ref was overwritten by another ref */
1756 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1757 &ow_inode, &other_type);
1758 if (ret < 0 && ret != -ENOENT)
1761 /* was never and will never be overwritten */
1766 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1771 if (ow_inode == ino && gen == ino_gen) {
1776 /* we know that it is or will be overwritten. check this now */
1777 if (ow_inode < sctx->send_progress)
1787 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1788 * that got overwritten. This is used by process_recorded_refs to determine
1789 * if it has to use the path as returned by get_cur_path or the orphan name.
1791 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1794 struct fs_path *name = NULL;
1798 if (!sctx->parent_root)
1801 name = fs_path_alloc();
1805 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1809 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1810 name->start, fs_path_len(name));
1818 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1819 * so we need to do some special handling in case we have clashes. This function
1820 * takes care of this with the help of name_cache_entry::radix_list.
1821 * In case of error, nce is kfreed.
1823 static int name_cache_insert(struct send_ctx *sctx,
1824 struct name_cache_entry *nce)
1827 struct list_head *nce_head;
1829 nce_head = radix_tree_lookup(&sctx->name_cache,
1830 (unsigned long)nce->ino);
1832 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1837 INIT_LIST_HEAD(nce_head);
1839 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1846 list_add_tail(&nce->radix_list, nce_head);
1847 list_add_tail(&nce->list, &sctx->name_cache_list);
1848 sctx->name_cache_size++;
1853 static void name_cache_delete(struct send_ctx *sctx,
1854 struct name_cache_entry *nce)
1856 struct list_head *nce_head;
1858 nce_head = radix_tree_lookup(&sctx->name_cache,
1859 (unsigned long)nce->ino);
1862 list_del(&nce->radix_list);
1863 list_del(&nce->list);
1864 sctx->name_cache_size--;
1866 if (list_empty(nce_head)) {
1867 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1872 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1875 struct list_head *nce_head;
1876 struct name_cache_entry *cur;
1878 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1882 list_for_each_entry(cur, nce_head, radix_list) {
1883 if (cur->ino == ino && cur->gen == gen)
1890 * Removes the entry from the list and adds it back to the end. This marks the
1891 * entry as recently used so that name_cache_clean_unused does not remove it.
1893 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1895 list_del(&nce->list);
1896 list_add_tail(&nce->list, &sctx->name_cache_list);
1900 * Remove some entries from the beginning of name_cache_list.
1902 static void name_cache_clean_unused(struct send_ctx *sctx)
1904 struct name_cache_entry *nce;
1906 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1909 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1910 nce = list_entry(sctx->name_cache_list.next,
1911 struct name_cache_entry, list);
1912 name_cache_delete(sctx, nce);
1917 static void name_cache_free(struct send_ctx *sctx)
1919 struct name_cache_entry *nce;
1921 while (!list_empty(&sctx->name_cache_list)) {
1922 nce = list_entry(sctx->name_cache_list.next,
1923 struct name_cache_entry, list);
1924 name_cache_delete(sctx, nce);
1930 * Used by get_cur_path for each ref up to the root.
1931 * Returns 0 if it succeeded.
1932 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1933 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1934 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1935 * Returns <0 in case of error.
1937 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1941 struct fs_path *dest)
1945 struct btrfs_path *path = NULL;
1946 struct name_cache_entry *nce = NULL;
1949 * First check if we already did a call to this function with the same
1950 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1951 * return the cached result.
1953 nce = name_cache_search(sctx, ino, gen);
1955 if (ino < sctx->send_progress && nce->need_later_update) {
1956 name_cache_delete(sctx, nce);
1960 name_cache_used(sctx, nce);
1961 *parent_ino = nce->parent_ino;
1962 *parent_gen = nce->parent_gen;
1963 ret = fs_path_add(dest, nce->name, nce->name_len);
1971 path = alloc_path_for_send();
1976 * If the inode is not existent yet, add the orphan name and return 1.
1977 * This should only happen for the parent dir that we determine in
1980 ret = is_inode_existent(sctx, ino, gen);
1985 ret = gen_unique_name(sctx, ino, gen, dest);
1993 * Depending on whether the inode was already processed or not, use
1994 * send_root or parent_root for ref lookup.
1996 if (ino < sctx->send_progress)
1997 ret = get_first_ref(sctx->send_root, ino,
1998 parent_ino, parent_gen, dest);
2000 ret = get_first_ref(sctx->parent_root, ino,
2001 parent_ino, parent_gen, dest);
2006 * Check if the ref was overwritten by an inode's ref that was processed
2007 * earlier. If yes, treat as orphan and return 1.
2009 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2010 dest->start, dest->end - dest->start);
2014 fs_path_reset(dest);
2015 ret = gen_unique_name(sctx, ino, gen, dest);
2023 * Store the result of the lookup in the name cache.
2025 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2033 nce->parent_ino = *parent_ino;
2034 nce->parent_gen = *parent_gen;
2035 nce->name_len = fs_path_len(dest);
2037 strcpy(nce->name, dest->start);
2039 if (ino < sctx->send_progress)
2040 nce->need_later_update = 0;
2042 nce->need_later_update = 1;
2044 nce_ret = name_cache_insert(sctx, nce);
2047 name_cache_clean_unused(sctx);
2050 btrfs_free_path(path);
2055 * Magic happens here. This function returns the first ref to an inode as it
2056 * would look like while receiving the stream at this point in time.
2057 * We walk the path up to the root. For every inode in between, we check if it
2058 * was already processed/sent. If yes, we continue with the parent as found
2059 * in send_root. If not, we continue with the parent as found in parent_root.
2060 * If we encounter an inode that was deleted at this point in time, we use the
2061 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2062 * that were not created yet and overwritten inodes/refs.
2064 * When do we have have orphan inodes:
2065 * 1. When an inode is freshly created and thus no valid refs are available yet
2066 * 2. When a directory lost all it's refs (deleted) but still has dir items
2067 * inside which were not processed yet (pending for move/delete). If anyone
2068 * tried to get the path to the dir items, it would get a path inside that
2070 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2071 * of an unprocessed inode. If in that case the first ref would be
2072 * overwritten, the overwritten inode gets "orphanized". Later when we
2073 * process this overwritten inode, it is restored at a new place by moving
2076 * sctx->send_progress tells this function at which point in time receiving
2079 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2080 struct fs_path *dest)
2083 struct fs_path *name = NULL;
2084 u64 parent_inode = 0;
2088 name = fs_path_alloc();
2095 fs_path_reset(dest);
2097 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2098 fs_path_reset(name);
2100 ret = __get_cur_name_and_parent(sctx, ino, gen,
2101 &parent_inode, &parent_gen, name);
2107 ret = fs_path_add_path(dest, name);
2118 fs_path_unreverse(dest);
2123 * Called for regular files when sending extents data. Opens a struct file
2124 * to read from the file.
2126 static int open_cur_inode_file(struct send_ctx *sctx)
2129 struct btrfs_key key;
2131 struct inode *inode;
2132 struct dentry *dentry;
2136 if (sctx->cur_inode_filp)
2139 key.objectid = sctx->cur_ino;
2140 key.type = BTRFS_INODE_ITEM_KEY;
2143 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2145 if (IS_ERR(inode)) {
2146 ret = PTR_ERR(inode);
2150 dentry = d_obtain_alias(inode);
2152 if (IS_ERR(dentry)) {
2153 ret = PTR_ERR(dentry);
2157 path.mnt = sctx->mnt;
2158 path.dentry = dentry;
2159 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2163 ret = PTR_ERR(filp);
2166 sctx->cur_inode_filp = filp;
2170 * no xxxput required here as every vfs op
2171 * does it by itself on failure
2177 * Closes the struct file that was created in open_cur_inode_file
2179 static int close_cur_inode_file(struct send_ctx *sctx)
2183 if (!sctx->cur_inode_filp)
2186 ret = filp_close(sctx->cur_inode_filp, NULL);
2187 sctx->cur_inode_filp = NULL;
2194 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2196 static int send_subvol_begin(struct send_ctx *sctx)
2199 struct btrfs_root *send_root = sctx->send_root;
2200 struct btrfs_root *parent_root = sctx->parent_root;
2201 struct btrfs_path *path;
2202 struct btrfs_key key;
2203 struct btrfs_root_ref *ref;
2204 struct extent_buffer *leaf;
2208 path = alloc_path_for_send();
2212 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2214 btrfs_free_path(path);
2218 key.objectid = send_root->objectid;
2219 key.type = BTRFS_ROOT_BACKREF_KEY;
2222 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2231 leaf = path->nodes[0];
2232 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2233 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2234 key.objectid != send_root->objectid) {
2238 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2239 namelen = btrfs_root_ref_name_len(leaf, ref);
2240 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2241 btrfs_release_path(path);
2244 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2248 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2253 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2254 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2255 sctx->send_root->root_item.uuid);
2256 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2257 sctx->send_root->root_item.ctransid);
2259 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2260 sctx->parent_root->root_item.uuid);
2261 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2262 sctx->parent_root->root_item.ctransid);
2265 ret = send_cmd(sctx);
2269 btrfs_free_path(path);
2274 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2279 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2281 p = fs_path_alloc();
2285 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2289 ret = get_cur_path(sctx, ino, gen, p);
2292 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2293 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2295 ret = send_cmd(sctx);
2303 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2308 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2310 p = fs_path_alloc();
2314 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2318 ret = get_cur_path(sctx, ino, gen, p);
2321 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2322 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2324 ret = send_cmd(sctx);
2332 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2337 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2339 p = fs_path_alloc();
2343 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2347 ret = get_cur_path(sctx, ino, gen, p);
2350 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2351 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2352 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2354 ret = send_cmd(sctx);
2362 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2365 struct fs_path *p = NULL;
2366 struct btrfs_inode_item *ii;
2367 struct btrfs_path *path = NULL;
2368 struct extent_buffer *eb;
2369 struct btrfs_key key;
2372 verbose_printk("btrfs: send_utimes %llu\n", ino);
2374 p = fs_path_alloc();
2378 path = alloc_path_for_send();
2385 key.type = BTRFS_INODE_ITEM_KEY;
2387 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2391 eb = path->nodes[0];
2392 slot = path->slots[0];
2393 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2395 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2399 ret = get_cur_path(sctx, ino, gen, p);
2402 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2403 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2404 btrfs_inode_atime(ii));
2405 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2406 btrfs_inode_mtime(ii));
2407 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2408 btrfs_inode_ctime(ii));
2409 /* TODO Add otime support when the otime patches get into upstream */
2411 ret = send_cmd(sctx);
2416 btrfs_free_path(path);
2421 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2422 * a valid path yet because we did not process the refs yet. So, the inode
2423 * is created as orphan.
2425 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2434 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2436 p = fs_path_alloc();
2440 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2445 if (S_ISREG(mode)) {
2446 cmd = BTRFS_SEND_C_MKFILE;
2447 } else if (S_ISDIR(mode)) {
2448 cmd = BTRFS_SEND_C_MKDIR;
2449 } else if (S_ISLNK(mode)) {
2450 cmd = BTRFS_SEND_C_SYMLINK;
2451 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2452 cmd = BTRFS_SEND_C_MKNOD;
2453 } else if (S_ISFIFO(mode)) {
2454 cmd = BTRFS_SEND_C_MKFIFO;
2455 } else if (S_ISSOCK(mode)) {
2456 cmd = BTRFS_SEND_C_MKSOCK;
2458 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2459 (int)(mode & S_IFMT));
2464 ret = begin_cmd(sctx, cmd);
2468 ret = gen_unique_name(sctx, ino, gen, p);
2472 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2473 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2475 if (S_ISLNK(mode)) {
2477 ret = read_symlink(sctx->send_root, ino, p);
2480 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2481 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2482 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2483 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2484 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2487 ret = send_cmd(sctx);
2499 * We need some special handling for inodes that get processed before the parent
2500 * directory got created. See process_recorded_refs for details.
2501 * This function does the check if we already created the dir out of order.
2503 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2506 struct btrfs_path *path = NULL;
2507 struct btrfs_key key;
2508 struct btrfs_key found_key;
2509 struct btrfs_key di_key;
2510 struct extent_buffer *eb;
2511 struct btrfs_dir_item *di;
2514 path = alloc_path_for_send();
2521 key.type = BTRFS_DIR_INDEX_KEY;
2524 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2529 eb = path->nodes[0];
2530 slot = path->slots[0];
2531 btrfs_item_key_to_cpu(eb, &found_key, slot);
2533 if (ret || found_key.objectid != key.objectid ||
2534 found_key.type != key.type) {
2539 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2540 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2542 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2543 di_key.objectid < sctx->send_progress) {
2548 key.offset = found_key.offset + 1;
2549 btrfs_release_path(path);
2553 btrfs_free_path(path);
2558 * Only creates the inode if it is:
2559 * 1. Not a directory
2560 * 2. Or a directory which was not created already due to out of order
2561 * directories. See did_create_dir and process_recorded_refs for details.
2563 static int send_create_inode_if_needed(struct send_ctx *sctx)
2567 if (S_ISDIR(sctx->cur_inode_mode)) {
2568 ret = did_create_dir(sctx, sctx->cur_ino);
2577 ret = send_create_inode(sctx, sctx->cur_ino);
2585 struct recorded_ref {
2586 struct list_head list;
2589 struct fs_path *full_path;
2597 * We need to process new refs before deleted refs, but compare_tree gives us
2598 * everything mixed. So we first record all refs and later process them.
2599 * This function is a helper to record one ref.
2601 static int record_ref(struct list_head *head, u64 dir,
2602 u64 dir_gen, struct fs_path *path)
2604 struct recorded_ref *ref;
2606 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2611 ref->dir_gen = dir_gen;
2612 ref->full_path = path;
2614 ref->name = (char *)kbasename(ref->full_path->start);
2615 ref->name_len = ref->full_path->end - ref->name;
2616 ref->dir_path = ref->full_path->start;
2617 if (ref->name == ref->full_path->start)
2618 ref->dir_path_len = 0;
2620 ref->dir_path_len = ref->full_path->end -
2621 ref->full_path->start - 1 - ref->name_len;
2623 list_add_tail(&ref->list, head);
2627 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2629 struct recorded_ref *new;
2631 new = kmalloc(sizeof(*ref), GFP_NOFS);
2635 new->dir = ref->dir;
2636 new->dir_gen = ref->dir_gen;
2637 new->full_path = NULL;
2638 INIT_LIST_HEAD(&new->list);
2639 list_add_tail(&new->list, list);
2643 static void __free_recorded_refs(struct list_head *head)
2645 struct recorded_ref *cur;
2647 while (!list_empty(head)) {
2648 cur = list_entry(head->next, struct recorded_ref, list);
2649 fs_path_free(cur->full_path);
2650 list_del(&cur->list);
2655 static void free_recorded_refs(struct send_ctx *sctx)
2657 __free_recorded_refs(&sctx->new_refs);
2658 __free_recorded_refs(&sctx->deleted_refs);
2662 * Renames/moves a file/dir to its orphan name. Used when the first
2663 * ref of an unprocessed inode gets overwritten and for all non empty
2666 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2667 struct fs_path *path)
2670 struct fs_path *orphan;
2672 orphan = fs_path_alloc();
2676 ret = gen_unique_name(sctx, ino, gen, orphan);
2680 ret = send_rename(sctx, path, orphan);
2683 fs_path_free(orphan);
2688 * Returns 1 if a directory can be removed at this point in time.
2689 * We check this by iterating all dir items and checking if the inode behind
2690 * the dir item was already processed.
2692 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2695 struct btrfs_root *root = sctx->parent_root;
2696 struct btrfs_path *path;
2697 struct btrfs_key key;
2698 struct btrfs_key found_key;
2699 struct btrfs_key loc;
2700 struct btrfs_dir_item *di;
2703 * Don't try to rmdir the top/root subvolume dir.
2705 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2708 path = alloc_path_for_send();
2713 key.type = BTRFS_DIR_INDEX_KEY;
2717 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2721 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2724 if (ret || found_key.objectid != key.objectid ||
2725 found_key.type != key.type) {
2729 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2730 struct btrfs_dir_item);
2731 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2733 if (loc.objectid > send_progress) {
2738 btrfs_release_path(path);
2739 key.offset = found_key.offset + 1;
2745 btrfs_free_path(path);
2750 * This does all the move/link/unlink/rmdir magic.
2752 static int process_recorded_refs(struct send_ctx *sctx)
2755 struct recorded_ref *cur;
2756 struct recorded_ref *cur2;
2757 struct list_head check_dirs;
2758 struct fs_path *valid_path = NULL;
2761 int did_overwrite = 0;
2764 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2767 * This should never happen as the root dir always has the same ref
2768 * which is always '..'
2770 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
2771 INIT_LIST_HEAD(&check_dirs);
2773 valid_path = fs_path_alloc();
2780 * First, check if the first ref of the current inode was overwritten
2781 * before. If yes, we know that the current inode was already orphanized
2782 * and thus use the orphan name. If not, we can use get_cur_path to
2783 * get the path of the first ref as it would like while receiving at
2784 * this point in time.
2785 * New inodes are always orphan at the beginning, so force to use the
2786 * orphan name in this case.
2787 * The first ref is stored in valid_path and will be updated if it
2788 * gets moved around.
2790 if (!sctx->cur_inode_new) {
2791 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2792 sctx->cur_inode_gen);
2798 if (sctx->cur_inode_new || did_overwrite) {
2799 ret = gen_unique_name(sctx, sctx->cur_ino,
2800 sctx->cur_inode_gen, valid_path);
2805 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2811 list_for_each_entry(cur, &sctx->new_refs, list) {
2813 * We may have refs where the parent directory does not exist
2814 * yet. This happens if the parent directories inum is higher
2815 * the the current inum. To handle this case, we create the
2816 * parent directory out of order. But we need to check if this
2817 * did already happen before due to other refs in the same dir.
2819 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2822 if (ret == inode_state_will_create) {
2825 * First check if any of the current inodes refs did
2826 * already create the dir.
2828 list_for_each_entry(cur2, &sctx->new_refs, list) {
2831 if (cur2->dir == cur->dir) {
2838 * If that did not happen, check if a previous inode
2839 * did already create the dir.
2842 ret = did_create_dir(sctx, cur->dir);
2846 ret = send_create_inode(sctx, cur->dir);
2853 * Check if this new ref would overwrite the first ref of
2854 * another unprocessed inode. If yes, orphanize the
2855 * overwritten inode. If we find an overwritten ref that is
2856 * not the first ref, simply unlink it.
2858 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2859 cur->name, cur->name_len,
2860 &ow_inode, &ow_gen);
2864 ret = is_first_ref(sctx->parent_root,
2865 ow_inode, cur->dir, cur->name,
2870 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2875 ret = send_unlink(sctx, cur->full_path);
2882 * link/move the ref to the new place. If we have an orphan
2883 * inode, move it and update valid_path. If not, link or move
2884 * it depending on the inode mode.
2887 ret = send_rename(sctx, valid_path, cur->full_path);
2891 ret = fs_path_copy(valid_path, cur->full_path);
2895 if (S_ISDIR(sctx->cur_inode_mode)) {
2897 * Dirs can't be linked, so move it. For moved
2898 * dirs, we always have one new and one deleted
2899 * ref. The deleted ref is ignored later.
2901 ret = send_rename(sctx, valid_path,
2905 ret = fs_path_copy(valid_path, cur->full_path);
2909 ret = send_link(sctx, cur->full_path,
2915 ret = dup_ref(cur, &check_dirs);
2920 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2922 * Check if we can already rmdir the directory. If not,
2923 * orphanize it. For every dir item inside that gets deleted
2924 * later, we do this check again and rmdir it then if possible.
2925 * See the use of check_dirs for more details.
2927 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2931 ret = send_rmdir(sctx, valid_path);
2934 } else if (!is_orphan) {
2935 ret = orphanize_inode(sctx, sctx->cur_ino,
2936 sctx->cur_inode_gen, valid_path);
2942 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2943 ret = dup_ref(cur, &check_dirs);
2947 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2948 !list_empty(&sctx->deleted_refs)) {
2950 * We have a moved dir. Add the old parent to check_dirs
2952 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2954 ret = dup_ref(cur, &check_dirs);
2957 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2959 * We have a non dir inode. Go through all deleted refs and
2960 * unlink them if they were not already overwritten by other
2963 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2964 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2965 sctx->cur_ino, sctx->cur_inode_gen,
2966 cur->name, cur->name_len);
2970 ret = send_unlink(sctx, cur->full_path);
2974 ret = dup_ref(cur, &check_dirs);
2979 * If the inode is still orphan, unlink the orphan. This may
2980 * happen when a previous inode did overwrite the first ref
2981 * of this inode and no new refs were added for the current
2982 * inode. Unlinking does not mean that the inode is deleted in
2983 * all cases. There may still be links to this inode in other
2987 ret = send_unlink(sctx, valid_path);
2994 * We did collect all parent dirs where cur_inode was once located. We
2995 * now go through all these dirs and check if they are pending for
2996 * deletion and if it's finally possible to perform the rmdir now.
2997 * We also update the inode stats of the parent dirs here.
2999 list_for_each_entry(cur, &check_dirs, list) {
3001 * In case we had refs into dirs that were not processed yet,
3002 * we don't need to do the utime and rmdir logic for these dirs.
3003 * The dir will be processed later.
3005 if (cur->dir > sctx->cur_ino)
3008 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3012 if (ret == inode_state_did_create ||
3013 ret == inode_state_no_change) {
3014 /* TODO delayed utimes */
3015 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3018 } else if (ret == inode_state_did_delete) {
3019 ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
3023 ret = get_cur_path(sctx, cur->dir,
3024 cur->dir_gen, valid_path);
3027 ret = send_rmdir(sctx, valid_path);
3037 __free_recorded_refs(&check_dirs);
3038 free_recorded_refs(sctx);
3039 fs_path_free(valid_path);
3043 static int __record_new_ref(int num, u64 dir, int index,
3044 struct fs_path *name,
3048 struct send_ctx *sctx = ctx;
3052 p = fs_path_alloc();
3056 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
3061 ret = get_cur_path(sctx, dir, gen, p);
3064 ret = fs_path_add_path(p, name);
3068 ret = record_ref(&sctx->new_refs, dir, gen, p);
3076 static int __record_deleted_ref(int num, u64 dir, int index,
3077 struct fs_path *name,
3081 struct send_ctx *sctx = ctx;
3085 p = fs_path_alloc();
3089 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3094 ret = get_cur_path(sctx, dir, gen, p);
3097 ret = fs_path_add_path(p, name);
3101 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3109 static int record_new_ref(struct send_ctx *sctx)
3113 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3114 sctx->cmp_key, 0, __record_new_ref, sctx);
3123 static int record_deleted_ref(struct send_ctx *sctx)
3127 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3128 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3137 struct find_ref_ctx {
3140 struct btrfs_root *root;
3141 struct fs_path *name;
3145 static int __find_iref(int num, u64 dir, int index,
3146 struct fs_path *name,
3149 struct find_ref_ctx *ctx = ctx_;
3153 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3154 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3156 * To avoid doing extra lookups we'll only do this if everything
3159 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3163 if (dir_gen != ctx->dir_gen)
3165 ctx->found_idx = num;
3171 static int find_iref(struct btrfs_root *root,
3172 struct btrfs_path *path,
3173 struct btrfs_key *key,
3174 u64 dir, u64 dir_gen, struct fs_path *name)
3177 struct find_ref_ctx ctx;
3181 ctx.dir_gen = dir_gen;
3185 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3189 if (ctx.found_idx == -1)
3192 return ctx.found_idx;
3195 static int __record_changed_new_ref(int num, u64 dir, int index,
3196 struct fs_path *name,
3201 struct send_ctx *sctx = ctx;
3203 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3208 ret = find_iref(sctx->parent_root, sctx->right_path,
3209 sctx->cmp_key, dir, dir_gen, name);
3211 ret = __record_new_ref(num, dir, index, name, sctx);
3218 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3219 struct fs_path *name,
3224 struct send_ctx *sctx = ctx;
3226 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3231 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3232 dir, dir_gen, name);
3234 ret = __record_deleted_ref(num, dir, index, name, sctx);
3241 static int record_changed_ref(struct send_ctx *sctx)
3245 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3246 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3249 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3250 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3260 * Record and process all refs at once. Needed when an inode changes the
3261 * generation number, which means that it was deleted and recreated.
3263 static int process_all_refs(struct send_ctx *sctx,
3264 enum btrfs_compare_tree_result cmd)
3267 struct btrfs_root *root;
3268 struct btrfs_path *path;
3269 struct btrfs_key key;
3270 struct btrfs_key found_key;
3271 struct extent_buffer *eb;
3273 iterate_inode_ref_t cb;
3275 path = alloc_path_for_send();
3279 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3280 root = sctx->send_root;
3281 cb = __record_new_ref;
3282 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3283 root = sctx->parent_root;
3284 cb = __record_deleted_ref;
3289 key.objectid = sctx->cmp_key->objectid;
3290 key.type = BTRFS_INODE_REF_KEY;
3293 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3299 eb = path->nodes[0];
3300 slot = path->slots[0];
3301 btrfs_item_key_to_cpu(eb, &found_key, slot);
3303 if (found_key.objectid != key.objectid ||
3304 (found_key.type != BTRFS_INODE_REF_KEY &&
3305 found_key.type != BTRFS_INODE_EXTREF_KEY))
3308 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
3309 btrfs_release_path(path);
3313 key.offset = found_key.offset + 1;
3315 btrfs_release_path(path);
3317 ret = process_recorded_refs(sctx);
3320 btrfs_free_path(path);
3324 static int send_set_xattr(struct send_ctx *sctx,
3325 struct fs_path *path,
3326 const char *name, int name_len,
3327 const char *data, int data_len)
3331 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3335 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3336 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3337 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3339 ret = send_cmd(sctx);
3346 static int send_remove_xattr(struct send_ctx *sctx,
3347 struct fs_path *path,
3348 const char *name, int name_len)
3352 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3356 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3357 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3359 ret = send_cmd(sctx);
3366 static int __process_new_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;
3374 posix_acl_xattr_header dummy_acl;
3376 p = fs_path_alloc();
3381 * This hack is needed because empty acl's are stored as zero byte
3382 * data in xattrs. Problem with that is, that receiving these zero byte
3383 * acl's will fail later. To fix this, we send a dummy acl list that
3384 * only contains the version number and no entries.
3386 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3387 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3388 if (data_len == 0) {
3389 dummy_acl.a_version =
3390 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3391 data = (char *)&dummy_acl;
3392 data_len = sizeof(dummy_acl);
3396 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3400 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3407 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3408 const char *name, int name_len,
3409 const char *data, int data_len,
3413 struct send_ctx *sctx = ctx;
3416 p = fs_path_alloc();
3420 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3424 ret = send_remove_xattr(sctx, p, name, name_len);
3431 static int process_new_xattr(struct send_ctx *sctx)
3435 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3436 sctx->cmp_key, __process_new_xattr, sctx);
3441 static int process_deleted_xattr(struct send_ctx *sctx)
3445 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3446 sctx->cmp_key, __process_deleted_xattr, sctx);
3451 struct find_xattr_ctx {
3459 static int __find_xattr(int num, struct btrfs_key *di_key,
3460 const char *name, int name_len,
3461 const char *data, int data_len,
3462 u8 type, void *vctx)
3464 struct find_xattr_ctx *ctx = vctx;
3466 if (name_len == ctx->name_len &&
3467 strncmp(name, ctx->name, name_len) == 0) {
3468 ctx->found_idx = num;
3469 ctx->found_data_len = data_len;
3470 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
3471 if (!ctx->found_data)
3478 static int find_xattr(struct btrfs_root *root,
3479 struct btrfs_path *path,
3480 struct btrfs_key *key,
3481 const char *name, int name_len,
3482 char **data, int *data_len)
3485 struct find_xattr_ctx ctx;
3488 ctx.name_len = name_len;
3490 ctx.found_data = NULL;
3491 ctx.found_data_len = 0;
3493 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
3497 if (ctx.found_idx == -1)
3500 *data = ctx.found_data;
3501 *data_len = ctx.found_data_len;
3503 kfree(ctx.found_data);
3505 return ctx.found_idx;
3509 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3510 const char *name, int name_len,
3511 const char *data, int data_len,
3515 struct send_ctx *sctx = ctx;
3516 char *found_data = NULL;
3517 int found_data_len = 0;
3519 ret = find_xattr(sctx->parent_root, sctx->right_path,
3520 sctx->cmp_key, name, name_len, &found_data,
3522 if (ret == -ENOENT) {
3523 ret = __process_new_xattr(num, di_key, name, name_len, data,
3524 data_len, type, ctx);
3525 } else if (ret >= 0) {
3526 if (data_len != found_data_len ||
3527 memcmp(data, found_data, data_len)) {
3528 ret = __process_new_xattr(num, di_key, name, name_len,
3529 data, data_len, type, ctx);
3539 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3540 const char *name, int name_len,
3541 const char *data, int data_len,
3545 struct send_ctx *sctx = ctx;
3547 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3548 name, name_len, NULL, NULL);
3550 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3551 data_len, type, ctx);
3558 static int process_changed_xattr(struct send_ctx *sctx)
3562 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3563 sctx->cmp_key, __process_changed_new_xattr, sctx);
3566 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3567 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3573 static int process_all_new_xattrs(struct send_ctx *sctx)
3576 struct btrfs_root *root;
3577 struct btrfs_path *path;
3578 struct btrfs_key key;
3579 struct btrfs_key found_key;
3580 struct extent_buffer *eb;
3583 path = alloc_path_for_send();
3587 root = sctx->send_root;
3589 key.objectid = sctx->cmp_key->objectid;
3590 key.type = BTRFS_XATTR_ITEM_KEY;
3593 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3601 eb = path->nodes[0];
3602 slot = path->slots[0];
3603 btrfs_item_key_to_cpu(eb, &found_key, slot);
3605 if (found_key.objectid != key.objectid ||
3606 found_key.type != key.type) {
3611 ret = iterate_dir_item(root, path, &found_key,
3612 __process_new_xattr, sctx);
3616 btrfs_release_path(path);
3617 key.offset = found_key.offset + 1;
3621 btrfs_free_path(path);
3626 * Read some bytes from the current inode/file and send a write command to
3629 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3633 loff_t pos = offset;
3635 mm_segment_t old_fs;
3637 p = fs_path_alloc();
3642 * vfs normally only accepts user space buffers for security reasons.
3643 * we only read from the file and also only provide the read_buf buffer
3644 * to vfs. As this buffer does not come from a user space call, it's
3645 * ok to temporary allow kernel space buffers.
3650 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3652 ret = open_cur_inode_file(sctx);
3656 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3663 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3667 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3671 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3672 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3673 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
3675 ret = send_cmd(sctx);
3687 * Send a clone command to user space.
3689 static int send_clone(struct send_ctx *sctx,
3690 u64 offset, u32 len,
3691 struct clone_root *clone_root)
3697 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3698 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3699 clone_root->root->objectid, clone_root->ino,
3700 clone_root->offset);
3702 p = fs_path_alloc();
3706 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3710 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3714 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3715 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3716 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3718 if (clone_root->root == sctx->send_root) {
3719 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3720 &gen, NULL, NULL, NULL, NULL);
3723 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3725 ret = get_inode_path(clone_root->root, clone_root->ino, p);
3730 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3731 clone_root->root->root_item.uuid);
3732 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3733 clone_root->root->root_item.ctransid);
3734 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3735 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3736 clone_root->offset);
3738 ret = send_cmd(sctx);
3747 * Send an update extent command to user space.
3749 static int send_update_extent(struct send_ctx *sctx,
3750 u64 offset, u32 len)
3755 p = fs_path_alloc();
3759 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
3763 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3767 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3768 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3769 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
3771 ret = send_cmd(sctx);
3779 static int send_write_or_clone(struct send_ctx *sctx,
3780 struct btrfs_path *path,
3781 struct btrfs_key *key,
3782 struct clone_root *clone_root)
3785 struct btrfs_file_extent_item *ei;
3786 u64 offset = key->offset;
3792 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3793 struct btrfs_file_extent_item);
3794 type = btrfs_file_extent_type(path->nodes[0], ei);
3795 if (type == BTRFS_FILE_EXTENT_INLINE) {
3796 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3798 * it is possible the inline item won't cover the whole page,
3799 * but there may be items after this page. Make
3800 * sure to send the whole thing
3802 len = PAGE_CACHE_ALIGN(len);
3804 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3807 if (offset + len > sctx->cur_inode_size)
3808 len = sctx->cur_inode_size - offset;
3815 ret = send_clone(sctx, offset, len, clone_root);
3816 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
3817 ret = send_update_extent(sctx, offset, len);
3821 if (l > BTRFS_SEND_READ_SIZE)
3822 l = BTRFS_SEND_READ_SIZE;
3823 ret = send_write(sctx, pos + offset, l);
3836 static int is_extent_unchanged(struct send_ctx *sctx,
3837 struct btrfs_path *left_path,
3838 struct btrfs_key *ekey)
3841 struct btrfs_key key;
3842 struct btrfs_path *path = NULL;
3843 struct extent_buffer *eb;
3845 struct btrfs_key found_key;
3846 struct btrfs_file_extent_item *ei;
3851 u64 left_offset_fixed;
3859 path = alloc_path_for_send();
3863 eb = left_path->nodes[0];
3864 slot = left_path->slots[0];
3865 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3866 left_type = btrfs_file_extent_type(eb, ei);
3868 if (left_type != BTRFS_FILE_EXTENT_REG) {
3872 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3873 left_len = btrfs_file_extent_num_bytes(eb, ei);
3874 left_offset = btrfs_file_extent_offset(eb, ei);
3875 left_gen = btrfs_file_extent_generation(eb, ei);
3878 * Following comments will refer to these graphics. L is the left
3879 * extents which we are checking at the moment. 1-8 are the right
3880 * extents that we iterate.
3883 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3886 * |--1--|-2b-|...(same as above)
3888 * Alternative situation. Happens on files where extents got split.
3890 * |-----------7-----------|-6-|
3892 * Alternative situation. Happens on files which got larger.
3895 * Nothing follows after 8.
3898 key.objectid = ekey->objectid;
3899 key.type = BTRFS_EXTENT_DATA_KEY;
3900 key.offset = ekey->offset;
3901 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3910 * Handle special case where the right side has no extents at all.
3912 eb = path->nodes[0];
3913 slot = path->slots[0];
3914 btrfs_item_key_to_cpu(eb, &found_key, slot);
3915 if (found_key.objectid != key.objectid ||
3916 found_key.type != key.type) {
3917 /* If we're a hole then just pretend nothing changed */
3918 ret = (left_disknr) ? 0 : 1;
3923 * We're now on 2a, 2b or 7.
3926 while (key.offset < ekey->offset + left_len) {
3927 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3928 right_type = btrfs_file_extent_type(eb, ei);
3929 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3930 right_len = btrfs_file_extent_num_bytes(eb, ei);
3931 right_offset = btrfs_file_extent_offset(eb, ei);
3932 right_gen = btrfs_file_extent_generation(eb, ei);
3934 if (right_type != BTRFS_FILE_EXTENT_REG) {
3940 * Are we at extent 8? If yes, we know the extent is changed.
3941 * This may only happen on the first iteration.
3943 if (found_key.offset + right_len <= ekey->offset) {
3944 /* If we're a hole just pretend nothing changed */
3945 ret = (left_disknr) ? 0 : 1;
3949 left_offset_fixed = left_offset;
3950 if (key.offset < ekey->offset) {
3951 /* Fix the right offset for 2a and 7. */
3952 right_offset += ekey->offset - key.offset;
3954 /* Fix the left offset for all behind 2a and 2b */
3955 left_offset_fixed += key.offset - ekey->offset;
3959 * Check if we have the same extent.
3961 if (left_disknr != right_disknr ||
3962 left_offset_fixed != right_offset ||
3963 left_gen != right_gen) {
3969 * Go to the next extent.
3971 ret = btrfs_next_item(sctx->parent_root, path);
3975 eb = path->nodes[0];
3976 slot = path->slots[0];
3977 btrfs_item_key_to_cpu(eb, &found_key, slot);
3979 if (ret || found_key.objectid != key.objectid ||
3980 found_key.type != key.type) {
3981 key.offset += right_len;
3984 if (found_key.offset != key.offset + right_len) {
3992 * We're now behind the left extent (treat as unchanged) or at the end
3993 * of the right side (treat as changed).
3995 if (key.offset >= ekey->offset + left_len)
4002 btrfs_free_path(path);
4006 static int process_extent(struct send_ctx *sctx,
4007 struct btrfs_path *path,
4008 struct btrfs_key *key)
4010 struct clone_root *found_clone = NULL;
4013 if (S_ISLNK(sctx->cur_inode_mode))
4016 if (sctx->parent_root && !sctx->cur_inode_new) {
4017 ret = is_extent_unchanged(sctx, path, key);
4025 struct btrfs_file_extent_item *ei;
4028 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4029 struct btrfs_file_extent_item);
4030 type = btrfs_file_extent_type(path->nodes[0], ei);
4031 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4032 type == BTRFS_FILE_EXTENT_REG) {
4034 * The send spec does not have a prealloc command yet,
4035 * so just leave a hole for prealloc'ed extents until
4036 * we have enough commands queued up to justify rev'ing
4039 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4044 /* Have a hole, just skip it. */
4045 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4052 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4053 sctx->cur_inode_size, &found_clone);
4054 if (ret != -ENOENT && ret < 0)
4057 ret = send_write_or_clone(sctx, path, key, found_clone);
4063 static int process_all_extents(struct send_ctx *sctx)
4066 struct btrfs_root *root;
4067 struct btrfs_path *path;
4068 struct btrfs_key key;
4069 struct btrfs_key found_key;
4070 struct extent_buffer *eb;
4073 root = sctx->send_root;
4074 path = alloc_path_for_send();
4078 key.objectid = sctx->cmp_key->objectid;
4079 key.type = BTRFS_EXTENT_DATA_KEY;
4082 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
4090 eb = path->nodes[0];
4091 slot = path->slots[0];
4092 btrfs_item_key_to_cpu(eb, &found_key, slot);
4094 if (found_key.objectid != key.objectid ||
4095 found_key.type != key.type) {
4100 ret = process_extent(sctx, path, &found_key);
4104 btrfs_release_path(path);
4105 key.offset = found_key.offset + 1;
4109 btrfs_free_path(path);
4113 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
4117 if (sctx->cur_ino == 0)
4119 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4120 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4122 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4125 ret = process_recorded_refs(sctx);
4130 * We have processed the refs and thus need to advance send_progress.
4131 * Now, calls to get_cur_xxx will take the updated refs of the current
4132 * inode into account.
4134 sctx->send_progress = sctx->cur_ino + 1;
4140 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4152 ret = process_recorded_refs_if_needed(sctx, at_end);
4156 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4158 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4161 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4162 &left_mode, &left_uid, &left_gid, NULL);
4166 if (!sctx->parent_root || sctx->cur_inode_new) {
4168 if (!S_ISLNK(sctx->cur_inode_mode))
4171 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4172 NULL, NULL, &right_mode, &right_uid,
4177 if (left_uid != right_uid || left_gid != right_gid)
4179 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4183 if (S_ISREG(sctx->cur_inode_mode)) {
4184 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4185 sctx->cur_inode_size);
4191 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4192 left_uid, left_gid);
4197 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4204 * Need to send that every time, no matter if it actually changed
4205 * between the two trees as we have done changes to the inode before.
4207 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4215 static int changed_inode(struct send_ctx *sctx,
4216 enum btrfs_compare_tree_result result)
4219 struct btrfs_key *key = sctx->cmp_key;
4220 struct btrfs_inode_item *left_ii = NULL;
4221 struct btrfs_inode_item *right_ii = NULL;
4225 ret = close_cur_inode_file(sctx);
4229 sctx->cur_ino = key->objectid;
4230 sctx->cur_inode_new_gen = 0;
4233 * Set send_progress to current inode. This will tell all get_cur_xxx
4234 * functions that the current inode's refs are not updated yet. Later,
4235 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4237 sctx->send_progress = sctx->cur_ino;
4239 if (result == BTRFS_COMPARE_TREE_NEW ||
4240 result == BTRFS_COMPARE_TREE_CHANGED) {
4241 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4242 sctx->left_path->slots[0],
4243 struct btrfs_inode_item);
4244 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4247 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4248 sctx->right_path->slots[0],
4249 struct btrfs_inode_item);
4250 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4253 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4254 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4255 sctx->right_path->slots[0],
4256 struct btrfs_inode_item);
4258 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4262 * The cur_ino = root dir case is special here. We can't treat
4263 * the inode as deleted+reused because it would generate a
4264 * stream that tries to delete/mkdir the root dir.
4266 if (left_gen != right_gen &&
4267 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4268 sctx->cur_inode_new_gen = 1;
4271 if (result == BTRFS_COMPARE_TREE_NEW) {
4272 sctx->cur_inode_gen = left_gen;
4273 sctx->cur_inode_new = 1;
4274 sctx->cur_inode_deleted = 0;
4275 sctx->cur_inode_size = btrfs_inode_size(
4276 sctx->left_path->nodes[0], left_ii);
4277 sctx->cur_inode_mode = btrfs_inode_mode(
4278 sctx->left_path->nodes[0], left_ii);
4279 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4280 ret = send_create_inode_if_needed(sctx);
4281 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4282 sctx->cur_inode_gen = right_gen;
4283 sctx->cur_inode_new = 0;
4284 sctx->cur_inode_deleted = 1;
4285 sctx->cur_inode_size = btrfs_inode_size(
4286 sctx->right_path->nodes[0], right_ii);
4287 sctx->cur_inode_mode = btrfs_inode_mode(
4288 sctx->right_path->nodes[0], right_ii);
4289 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4291 * We need to do some special handling in case the inode was
4292 * reported as changed with a changed generation number. This
4293 * means that the original inode was deleted and new inode
4294 * reused the same inum. So we have to treat the old inode as
4295 * deleted and the new one as new.
4297 if (sctx->cur_inode_new_gen) {
4299 * First, process the inode as if it was deleted.
4301 sctx->cur_inode_gen = right_gen;
4302 sctx->cur_inode_new = 0;
4303 sctx->cur_inode_deleted = 1;
4304 sctx->cur_inode_size = btrfs_inode_size(
4305 sctx->right_path->nodes[0], right_ii);
4306 sctx->cur_inode_mode = btrfs_inode_mode(
4307 sctx->right_path->nodes[0], right_ii);
4308 ret = process_all_refs(sctx,
4309 BTRFS_COMPARE_TREE_DELETED);
4314 * Now process the inode as if it was new.
4316 sctx->cur_inode_gen = left_gen;
4317 sctx->cur_inode_new = 1;
4318 sctx->cur_inode_deleted = 0;
4319 sctx->cur_inode_size = btrfs_inode_size(
4320 sctx->left_path->nodes[0], left_ii);
4321 sctx->cur_inode_mode = btrfs_inode_mode(
4322 sctx->left_path->nodes[0], left_ii);
4323 ret = send_create_inode_if_needed(sctx);
4327 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4331 * Advance send_progress now as we did not get into
4332 * process_recorded_refs_if_needed in the new_gen case.
4334 sctx->send_progress = sctx->cur_ino + 1;
4337 * Now process all extents and xattrs of the inode as if
4338 * they were all new.
4340 ret = process_all_extents(sctx);
4343 ret = process_all_new_xattrs(sctx);
4347 sctx->cur_inode_gen = left_gen;
4348 sctx->cur_inode_new = 0;
4349 sctx->cur_inode_new_gen = 0;
4350 sctx->cur_inode_deleted = 0;
4351 sctx->cur_inode_size = btrfs_inode_size(
4352 sctx->left_path->nodes[0], left_ii);
4353 sctx->cur_inode_mode = btrfs_inode_mode(
4354 sctx->left_path->nodes[0], left_ii);
4363 * We have to process new refs before deleted refs, but compare_trees gives us
4364 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4365 * first and later process them in process_recorded_refs.
4366 * For the cur_inode_new_gen case, we skip recording completely because
4367 * changed_inode did already initiate processing of refs. The reason for this is
4368 * that in this case, compare_tree actually compares the refs of 2 different
4369 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4370 * refs of the right tree as deleted and all refs of the left tree as new.
4372 static int changed_ref(struct send_ctx *sctx,
4373 enum btrfs_compare_tree_result result)
4377 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4379 if (!sctx->cur_inode_new_gen &&
4380 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4381 if (result == BTRFS_COMPARE_TREE_NEW)
4382 ret = record_new_ref(sctx);
4383 else if (result == BTRFS_COMPARE_TREE_DELETED)
4384 ret = record_deleted_ref(sctx);
4385 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4386 ret = record_changed_ref(sctx);
4393 * Process new/deleted/changed xattrs. We skip processing in the
4394 * cur_inode_new_gen case because changed_inode did already initiate processing
4395 * of xattrs. The reason is the same as in changed_ref
4397 static int changed_xattr(struct send_ctx *sctx,
4398 enum btrfs_compare_tree_result result)
4402 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4404 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4405 if (result == BTRFS_COMPARE_TREE_NEW)
4406 ret = process_new_xattr(sctx);
4407 else if (result == BTRFS_COMPARE_TREE_DELETED)
4408 ret = process_deleted_xattr(sctx);
4409 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4410 ret = process_changed_xattr(sctx);
4417 * Process new/deleted/changed extents. We skip processing in the
4418 * cur_inode_new_gen case because changed_inode did already initiate processing
4419 * of extents. The reason is the same as in changed_ref
4421 static int changed_extent(struct send_ctx *sctx,
4422 enum btrfs_compare_tree_result result)
4426 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4428 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4429 if (result != BTRFS_COMPARE_TREE_DELETED)
4430 ret = process_extent(sctx, sctx->left_path,
4437 static int dir_changed(struct send_ctx *sctx, u64 dir)
4439 u64 orig_gen, new_gen;
4442 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
4447 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
4452 return (orig_gen != new_gen) ? 1 : 0;
4455 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
4456 struct btrfs_key *key)
4458 struct btrfs_inode_extref *extref;
4459 struct extent_buffer *leaf;
4460 u64 dirid = 0, last_dirid = 0;
4467 /* Easy case, just check this one dirid */
4468 if (key->type == BTRFS_INODE_REF_KEY) {
4469 dirid = key->offset;
4471 ret = dir_changed(sctx, dirid);
4475 leaf = path->nodes[0];
4476 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4477 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4478 while (cur_offset < item_size) {
4479 extref = (struct btrfs_inode_extref *)(ptr +
4481 dirid = btrfs_inode_extref_parent(leaf, extref);
4482 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
4483 cur_offset += ref_name_len + sizeof(*extref);
4484 if (dirid == last_dirid)
4486 ret = dir_changed(sctx, dirid);
4496 * Updates compare related fields in sctx and simply forwards to the actual
4497 * changed_xxx functions.
4499 static int changed_cb(struct btrfs_root *left_root,
4500 struct btrfs_root *right_root,
4501 struct btrfs_path *left_path,
4502 struct btrfs_path *right_path,
4503 struct btrfs_key *key,
4504 enum btrfs_compare_tree_result result,
4508 struct send_ctx *sctx = ctx;
4510 if (result == BTRFS_COMPARE_TREE_SAME) {
4511 if (key->type != BTRFS_INODE_REF_KEY &&
4512 key->type != BTRFS_INODE_EXTREF_KEY)
4514 ret = compare_refs(sctx, left_path, key);
4519 result = BTRFS_COMPARE_TREE_CHANGED;
4523 sctx->left_path = left_path;
4524 sctx->right_path = right_path;
4525 sctx->cmp_key = key;
4527 ret = finish_inode_if_needed(sctx, 0);
4531 /* Ignore non-FS objects */
4532 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4533 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4536 if (key->type == BTRFS_INODE_ITEM_KEY)
4537 ret = changed_inode(sctx, result);
4538 else if (key->type == BTRFS_INODE_REF_KEY ||
4539 key->type == BTRFS_INODE_EXTREF_KEY)
4540 ret = changed_ref(sctx, result);
4541 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4542 ret = changed_xattr(sctx, result);
4543 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4544 ret = changed_extent(sctx, result);
4550 static int full_send_tree(struct send_ctx *sctx)
4553 struct btrfs_trans_handle *trans = NULL;
4554 struct btrfs_root *send_root = sctx->send_root;
4555 struct btrfs_key key;
4556 struct btrfs_key found_key;
4557 struct btrfs_path *path;
4558 struct extent_buffer *eb;
4563 path = alloc_path_for_send();
4567 spin_lock(&send_root->root_item_lock);
4568 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4569 spin_unlock(&send_root->root_item_lock);
4571 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4572 key.type = BTRFS_INODE_ITEM_KEY;
4577 * We need to make sure the transaction does not get committed
4578 * while we do anything on commit roots. Join a transaction to prevent
4581 trans = btrfs_join_transaction(send_root);
4582 if (IS_ERR(trans)) {
4583 ret = PTR_ERR(trans);
4589 * Make sure the tree has not changed after re-joining. We detect this
4590 * by comparing start_ctransid and ctransid. They should always match.
4592 spin_lock(&send_root->root_item_lock);
4593 ctransid = btrfs_root_ctransid(&send_root->root_item);
4594 spin_unlock(&send_root->root_item_lock);
4596 if (ctransid != start_ctransid) {
4597 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4598 "send was modified in between. This is "
4599 "probably a bug.\n");
4604 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4612 * When someone want to commit while we iterate, end the
4613 * joined transaction and rejoin.
4615 if (btrfs_should_end_transaction(trans, send_root)) {
4616 ret = btrfs_end_transaction(trans, send_root);
4620 btrfs_release_path(path);
4624 eb = path->nodes[0];
4625 slot = path->slots[0];
4626 btrfs_item_key_to_cpu(eb, &found_key, slot);
4628 ret = changed_cb(send_root, NULL, path, NULL,
4629 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4633 key.objectid = found_key.objectid;
4634 key.type = found_key.type;
4635 key.offset = found_key.offset + 1;
4637 ret = btrfs_next_item(send_root, path);
4647 ret = finish_inode_if_needed(sctx, 1);
4650 btrfs_free_path(path);
4653 ret = btrfs_end_transaction(trans, send_root);
4655 btrfs_end_transaction(trans, send_root);
4660 static int send_subvol(struct send_ctx *sctx)
4664 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
4665 ret = send_header(sctx);
4670 ret = send_subvol_begin(sctx);
4674 if (sctx->parent_root) {
4675 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4679 ret = finish_inode_if_needed(sctx, 1);
4683 ret = full_send_tree(sctx);
4690 ret = close_cur_inode_file(sctx);
4692 close_cur_inode_file(sctx);
4694 free_recorded_refs(sctx);
4698 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4701 struct btrfs_root *send_root;
4702 struct btrfs_root *clone_root;
4703 struct btrfs_fs_info *fs_info;
4704 struct btrfs_ioctl_send_args *arg = NULL;
4705 struct btrfs_key key;
4706 struct send_ctx *sctx = NULL;
4708 u64 *clone_sources_tmp = NULL;
4710 if (!capable(CAP_SYS_ADMIN))
4713 send_root = BTRFS_I(file_inode(mnt_file))->root;
4714 fs_info = send_root->fs_info;
4717 * This is done when we lookup the root, it should already be complete
4718 * by the time we get here.
4720 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
4723 * If we just created this root we need to make sure that the orphan
4724 * cleanup has been done and committed since we search the commit root,
4725 * so check its commit root transid with our otransid and if they match
4726 * commit the transaction to make sure everything is updated.
4728 down_read(&send_root->fs_info->extent_commit_sem);
4729 if (btrfs_header_generation(send_root->commit_root) ==
4730 btrfs_root_otransid(&send_root->root_item)) {
4731 struct btrfs_trans_handle *trans;
4733 up_read(&send_root->fs_info->extent_commit_sem);
4735 trans = btrfs_attach_transaction_barrier(send_root);
4736 if (IS_ERR(trans)) {
4737 if (PTR_ERR(trans) != -ENOENT) {
4738 ret = PTR_ERR(trans);
4741 /* ENOENT means theres no transaction */
4743 ret = btrfs_commit_transaction(trans, send_root);
4748 up_read(&send_root->fs_info->extent_commit_sem);
4751 arg = memdup_user(arg_, sizeof(*arg));
4758 if (!access_ok(VERIFY_READ, arg->clone_sources,
4759 sizeof(*arg->clone_sources *
4760 arg->clone_sources_count))) {
4765 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
4770 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4776 INIT_LIST_HEAD(&sctx->new_refs);
4777 INIT_LIST_HEAD(&sctx->deleted_refs);
4778 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4779 INIT_LIST_HEAD(&sctx->name_cache_list);
4781 sctx->flags = arg->flags;
4783 sctx->send_filp = fget(arg->send_fd);
4784 if (!sctx->send_filp) {
4789 sctx->mnt = mnt_file->f_path.mnt;
4791 sctx->send_root = send_root;
4792 sctx->clone_roots_cnt = arg->clone_sources_count;
4794 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4795 sctx->send_buf = vmalloc(sctx->send_max_size);
4796 if (!sctx->send_buf) {
4801 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4802 if (!sctx->read_buf) {
4807 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4808 (arg->clone_sources_count + 1));
4809 if (!sctx->clone_roots) {
4814 if (arg->clone_sources_count) {
4815 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4816 sizeof(*arg->clone_sources));
4817 if (!clone_sources_tmp) {
4822 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4823 arg->clone_sources_count *
4824 sizeof(*arg->clone_sources));
4830 for (i = 0; i < arg->clone_sources_count; i++) {
4831 key.objectid = clone_sources_tmp[i];
4832 key.type = BTRFS_ROOT_ITEM_KEY;
4833 key.offset = (u64)-1;
4834 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4835 if (IS_ERR(clone_root)) {
4836 ret = PTR_ERR(clone_root);
4839 sctx->clone_roots[i].root = clone_root;
4841 vfree(clone_sources_tmp);
4842 clone_sources_tmp = NULL;
4845 if (arg->parent_root) {
4846 key.objectid = arg->parent_root;
4847 key.type = BTRFS_ROOT_ITEM_KEY;
4848 key.offset = (u64)-1;
4849 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4850 if (IS_ERR(sctx->parent_root)) {
4851 ret = PTR_ERR(sctx->parent_root);
4857 * Clones from send_root are allowed, but only if the clone source
4858 * is behind the current send position. This is checked while searching
4859 * for possible clone sources.
4861 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4863 /* We do a bsearch later */
4864 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4865 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4868 ret = send_subvol(sctx);
4872 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
4873 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4876 ret = send_cmd(sctx);
4883 vfree(clone_sources_tmp);
4886 if (sctx->send_filp)
4887 fput(sctx->send_filp);
4889 vfree(sctx->clone_roots);
4890 vfree(sctx->send_buf);
4891 vfree(sctx->read_buf);
4893 name_cache_free(sctx);