2 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/bsearch.h>
21 #include <linux/file.h>
22 #include <linux/sort.h>
23 #include <linux/mount.h>
24 #include <linux/xattr.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/radix-tree.h>
27 #include <linux/crc32c.h>
28 #include <linux/vmalloc.h>
34 #include "btrfs_inode.h"
35 #include "transaction.h"
37 static int g_verbose = 0;
39 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
42 * A fs_path is a helper to dynamically build path names with unknown size.
43 * It reallocates the internal buffer on demand.
44 * It allows fast adding of path elements on the right side (normal path) and
45 * fast adding to the left side (reversed path). A reversed path can also be
46 * unreversed if needed.
64 #define FS_PATH_INLINE_SIZE \
65 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
68 /* reused for each extent */
70 struct btrfs_root *root;
77 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
78 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
81 struct file *send_filp;
87 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
91 struct btrfs_root *send_root;
92 struct btrfs_root *parent_root;
93 struct clone_root *clone_roots;
96 /* current state of the compare_tree call */
97 struct btrfs_path *left_path;
98 struct btrfs_path *right_path;
99 struct btrfs_key *cmp_key;
102 * infos of the currently processed inode. In case of deleted inodes,
103 * these are the values from the deleted inode.
108 int cur_inode_new_gen;
109 int cur_inode_deleted;
115 struct list_head new_refs;
116 struct list_head deleted_refs;
118 struct radix_tree_root name_cache;
119 struct list_head name_cache_list;
122 struct file *cur_inode_filp;
126 struct name_cache_entry {
127 struct list_head list;
129 * radix_tree has only 32bit entries but we need to handle 64bit inums.
130 * We use the lower 32bit of the 64bit inum to store it in the tree. If
131 * more then one inum would fall into the same entry, we use radix_list
132 * to store the additional entries. radix_list is also used to store
133 * entries where two entries have the same inum but different
136 struct list_head radix_list;
142 int need_later_update;
147 static void fs_path_reset(struct fs_path *p)
150 p->start = p->buf + p->buf_len - 1;
160 static struct fs_path *fs_path_alloc(struct send_ctx *sctx)
164 p = kmalloc(sizeof(*p), GFP_NOFS);
169 p->buf = p->inline_buf;
170 p->buf_len = FS_PATH_INLINE_SIZE;
175 static struct fs_path *fs_path_alloc_reversed(struct send_ctx *sctx)
179 p = fs_path_alloc(sctx);
187 static void fs_path_free(struct send_ctx *sctx, struct fs_path *p)
191 if (p->buf != p->inline_buf) {
200 static int fs_path_len(struct fs_path *p)
202 return p->end - p->start;
205 static int fs_path_ensure_buf(struct fs_path *p, int len)
213 if (p->buf_len >= len)
216 path_len = p->end - p->start;
217 old_buf_len = p->buf_len;
218 len = PAGE_ALIGN(len);
220 if (p->buf == p->inline_buf) {
221 tmp_buf = kmalloc(len, GFP_NOFS);
223 tmp_buf = vmalloc(len);
228 memcpy(tmp_buf, p->buf, p->buf_len);
232 if (p->virtual_mem) {
233 tmp_buf = vmalloc(len);
236 memcpy(tmp_buf, p->buf, p->buf_len);
239 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
241 tmp_buf = vmalloc(len);
244 memcpy(tmp_buf, p->buf, p->buf_len);
253 tmp_buf = p->buf + old_buf_len - path_len - 1;
254 p->end = p->buf + p->buf_len - 1;
255 p->start = p->end - path_len;
256 memmove(p->start, tmp_buf, path_len + 1);
259 p->end = p->start + path_len;
264 static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
269 new_len = p->end - p->start + name_len;
270 if (p->start != p->end)
272 ret = fs_path_ensure_buf(p, new_len);
277 if (p->start != p->end)
279 p->start -= name_len;
280 p->prepared = p->start;
282 if (p->start != p->end)
284 p->prepared = p->end;
293 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
297 ret = fs_path_prepare_for_add(p, name_len);
300 memcpy(p->prepared, name, name_len);
307 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
311 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
314 memcpy(p->prepared, p2->start, p2->end - p2->start);
321 static int fs_path_add_from_extent_buffer(struct fs_path *p,
322 struct extent_buffer *eb,
323 unsigned long off, int len)
327 ret = fs_path_prepare_for_add(p, len);
331 read_extent_buffer(eb, p->prepared, off, len);
339 static void fs_path_remove(struct fs_path *p)
342 while (p->start != p->end && *p->end != '/')
348 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
352 p->reversed = from->reversed;
355 ret = fs_path_add_path(p, from);
361 static void fs_path_unreverse(struct fs_path *p)
370 len = p->end - p->start;
372 p->end = p->start + len;
373 memmove(p->start, tmp, len + 1);
377 static struct btrfs_path *alloc_path_for_send(void)
379 struct btrfs_path *path;
381 path = btrfs_alloc_path();
384 path->search_commit_root = 1;
385 path->skip_locking = 1;
389 static int write_buf(struct send_ctx *sctx, const void *buf, u32 len)
399 ret = vfs_write(sctx->send_filp, (char *)buf + pos, len - pos,
401 /* TODO handle that correctly */
402 /*if (ret == -ERESTARTSYS) {
421 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
423 struct btrfs_tlv_header *hdr;
424 int total_len = sizeof(*hdr) + len;
425 int left = sctx->send_max_size - sctx->send_size;
427 if (unlikely(left < total_len))
430 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
431 hdr->tlv_type = cpu_to_le16(attr);
432 hdr->tlv_len = cpu_to_le16(len);
433 memcpy(hdr + 1, data, len);
434 sctx->send_size += total_len;
440 static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
442 return tlv_put(sctx, attr, &value, sizeof(value));
445 static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
447 __le16 tmp = cpu_to_le16(value);
448 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
451 static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
453 __le32 tmp = cpu_to_le32(value);
454 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
458 static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
460 __le64 tmp = cpu_to_le64(value);
461 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
464 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
465 const char *str, int len)
469 return tlv_put(sctx, attr, str, len);
472 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
475 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
479 static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
482 struct btrfs_timespec bts;
483 bts.sec = cpu_to_le64(ts->tv_sec);
484 bts.nsec = cpu_to_le32(ts->tv_nsec);
485 return tlv_put(sctx, attr, &bts, sizeof(bts));
489 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
490 struct extent_buffer *eb,
491 struct btrfs_timespec *ts)
493 struct btrfs_timespec bts;
494 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
495 return tlv_put(sctx, attr, &bts, sizeof(bts));
499 #define TLV_PUT(sctx, attrtype, attrlen, data) \
501 ret = tlv_put(sctx, attrtype, attrlen, data); \
503 goto tlv_put_failure; \
506 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
508 ret = tlv_put_u##bits(sctx, attrtype, value); \
510 goto tlv_put_failure; \
513 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
514 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
515 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
516 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
517 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
519 ret = tlv_put_string(sctx, attrtype, str, len); \
521 goto tlv_put_failure; \
523 #define TLV_PUT_PATH(sctx, attrtype, p) \
525 ret = tlv_put_string(sctx, attrtype, p->start, \
526 p->end - p->start); \
528 goto tlv_put_failure; \
530 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
532 ret = tlv_put_uuid(sctx, attrtype, uuid); \
534 goto tlv_put_failure; \
536 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
538 ret = tlv_put_timespec(sctx, attrtype, ts); \
540 goto tlv_put_failure; \
542 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
544 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
546 goto tlv_put_failure; \
549 static int send_header(struct send_ctx *sctx)
551 struct btrfs_stream_header hdr;
553 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
554 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
556 return write_buf(sctx, &hdr, sizeof(hdr));
560 * For each command/item we want to send to userspace, we call this function.
562 static int begin_cmd(struct send_ctx *sctx, int cmd)
564 struct btrfs_cmd_header *hdr;
566 if (!sctx->send_buf) {
571 BUG_ON(sctx->send_size);
573 sctx->send_size += sizeof(*hdr);
574 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
575 hdr->cmd = cpu_to_le16(cmd);
580 static int send_cmd(struct send_ctx *sctx)
583 struct btrfs_cmd_header *hdr;
586 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
587 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
590 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
591 hdr->crc = cpu_to_le32(crc);
593 ret = write_buf(sctx, sctx->send_buf, sctx->send_size);
595 sctx->total_send_size += sctx->send_size;
596 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
603 * Sends a move instruction to user space
605 static int send_rename(struct send_ctx *sctx,
606 struct fs_path *from, struct fs_path *to)
610 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
612 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
616 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
617 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
619 ret = send_cmd(sctx);
627 * Sends a link instruction to user space
629 static int send_link(struct send_ctx *sctx,
630 struct fs_path *path, struct fs_path *lnk)
634 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
636 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
640 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
641 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
643 ret = send_cmd(sctx);
651 * Sends an unlink instruction to user space
653 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
657 verbose_printk("btrfs: send_unlink %s\n", path->start);
659 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
663 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
665 ret = send_cmd(sctx);
673 * Sends a rmdir instruction to user space
675 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
679 verbose_printk("btrfs: send_rmdir %s\n", path->start);
681 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
685 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
687 ret = send_cmd(sctx);
695 * Helper function to retrieve some fields from an inode item.
697 static int get_inode_info(struct btrfs_root *root,
698 u64 ino, u64 *size, u64 *gen,
699 u64 *mode, u64 *uid, u64 *gid,
703 struct btrfs_inode_item *ii;
704 struct btrfs_key key;
705 struct btrfs_path *path;
707 path = alloc_path_for_send();
712 key.type = BTRFS_INODE_ITEM_KEY;
714 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
722 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
723 struct btrfs_inode_item);
725 *size = btrfs_inode_size(path->nodes[0], ii);
727 *gen = btrfs_inode_generation(path->nodes[0], ii);
729 *mode = btrfs_inode_mode(path->nodes[0], ii);
731 *uid = btrfs_inode_uid(path->nodes[0], ii);
733 *gid = btrfs_inode_gid(path->nodes[0], ii);
735 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
738 btrfs_free_path(path);
742 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
747 * Helper function to iterate the entries in ONE btrfs_inode_ref.
748 * The iterate callback may return a non zero value to stop iteration. This can
749 * be a negative value for error codes or 1 to simply stop it.
751 * path must point to the INODE_REF when called.
753 static int iterate_inode_ref(struct send_ctx *sctx,
754 struct btrfs_root *root, struct btrfs_path *path,
755 struct btrfs_key *found_key, int resolve,
756 iterate_inode_ref_t iterate, void *ctx)
758 struct extent_buffer *eb;
759 struct btrfs_item *item;
760 struct btrfs_inode_ref *iref;
761 struct btrfs_path *tmp_path;
773 p = fs_path_alloc_reversed(sctx);
777 tmp_path = alloc_path_for_send();
779 fs_path_free(sctx, p);
784 slot = path->slots[0];
785 item = btrfs_item_nr(eb, slot);
786 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
789 total = btrfs_item_size(eb, item);
792 while (cur < total) {
795 name_len = btrfs_inode_ref_name_len(eb, iref);
796 index = btrfs_inode_ref_index(eb, iref);
798 start = btrfs_iref_to_path(root, tmp_path, iref, eb,
799 found_key->offset, p->buf,
802 ret = PTR_ERR(start);
805 if (start < p->buf) {
806 /* overflow , try again with larger buffer */
807 ret = fs_path_ensure_buf(p,
808 p->buf_len + p->buf - start);
811 start = btrfs_iref_to_path(root, tmp_path, iref,
812 eb, found_key->offset, p->buf,
815 ret = PTR_ERR(start);
818 BUG_ON(start < p->buf);
822 ret = fs_path_add_from_extent_buffer(p, eb,
823 (unsigned long)(iref + 1), name_len);
829 len = sizeof(*iref) + name_len;
830 iref = (struct btrfs_inode_ref *)((char *)iref + len);
833 ret = iterate(num, found_key->offset, index, p, ctx);
841 btrfs_free_path(tmp_path);
842 fs_path_free(sctx, p);
846 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
847 const char *name, int name_len,
848 const char *data, int data_len,
852 * Helper function to iterate the entries in ONE btrfs_dir_item.
853 * The iterate callback may return a non zero value to stop iteration. This can
854 * be a negative value for error codes or 1 to simply stop it.
856 * path must point to the dir item when called.
858 static int iterate_dir_item(struct send_ctx *sctx,
859 struct btrfs_root *root, struct btrfs_path *path,
860 struct btrfs_key *found_key,
861 iterate_dir_item_t iterate, void *ctx)
864 struct extent_buffer *eb;
865 struct btrfs_item *item;
866 struct btrfs_dir_item *di;
867 struct btrfs_path *tmp_path = NULL;
868 struct btrfs_key di_key;
883 buf = kmalloc(buf_len, GFP_NOFS);
889 tmp_path = alloc_path_for_send();
896 slot = path->slots[0];
897 item = btrfs_item_nr(eb, slot);
898 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
901 total = btrfs_item_size(eb, item);
904 while (cur < total) {
905 name_len = btrfs_dir_name_len(eb, di);
906 data_len = btrfs_dir_data_len(eb, di);
907 type = btrfs_dir_type(eb, di);
908 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
910 if (name_len + data_len > buf_len) {
911 buf_len = PAGE_ALIGN(name_len + data_len);
913 buf2 = vmalloc(buf_len);
920 buf2 = krealloc(buf, buf_len, GFP_NOFS);
922 buf2 = vmalloc(buf_len);
936 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
937 name_len + data_len);
939 len = sizeof(*di) + name_len + data_len;
940 di = (struct btrfs_dir_item *)((char *)di + len);
943 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
944 data_len, type, ctx);
956 btrfs_free_path(tmp_path);
964 static int __copy_first_ref(int num, u64 dir, int index,
965 struct fs_path *p, void *ctx)
968 struct fs_path *pt = ctx;
970 ret = fs_path_copy(pt, p);
974 /* we want the first only */
979 * Retrieve the first path of an inode. If an inode has more then one
980 * ref/hardlink, this is ignored.
982 static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root,
983 u64 ino, struct fs_path *path)
986 struct btrfs_key key, found_key;
987 struct btrfs_path *p;
989 p = alloc_path_for_send();
996 key.type = BTRFS_INODE_REF_KEY;
999 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1006 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1007 if (found_key.objectid != ino ||
1008 found_key.type != BTRFS_INODE_REF_KEY) {
1013 ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
1014 __copy_first_ref, path);
1024 struct backref_ctx {
1025 struct send_ctx *sctx;
1027 /* number of total found references */
1031 * used for clones found in send_root. clones found behind cur_objectid
1032 * and cur_offset are not considered as allowed clones.
1037 /* may be truncated in case it's the last extent in a file */
1040 /* Just to check for bugs in backref resolving */
1044 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1046 u64 root = (u64)key;
1047 struct clone_root *cr = (struct clone_root *)elt;
1049 if (root < cr->root->objectid)
1051 if (root > cr->root->objectid)
1056 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1058 struct clone_root *cr1 = (struct clone_root *)e1;
1059 struct clone_root *cr2 = (struct clone_root *)e2;
1061 if (cr1->root->objectid < cr2->root->objectid)
1063 if (cr1->root->objectid > cr2->root->objectid)
1069 * Called for every backref that is found for the current extent.
1070 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1072 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1074 struct backref_ctx *bctx = ctx_;
1075 struct clone_root *found;
1079 /* First check if the root is in the list of accepted clone sources */
1080 found = bsearch((void *)root, bctx->sctx->clone_roots,
1081 bctx->sctx->clone_roots_cnt,
1082 sizeof(struct clone_root),
1083 __clone_root_cmp_bsearch);
1087 if (found->root == bctx->sctx->send_root &&
1088 ino == bctx->cur_objectid &&
1089 offset == bctx->cur_offset) {
1090 bctx->found_itself = 1;
1094 * There are inodes that have extents that lie behind its i_size. Don't
1095 * accept clones from these extents.
1097 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1102 if (offset + bctx->extent_len > i_size)
1106 * Make sure we don't consider clones from send_root that are
1107 * behind the current inode/offset.
1109 if (found->root == bctx->sctx->send_root) {
1111 * TODO for the moment we don't accept clones from the inode
1112 * that is currently send. We may change this when
1113 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1116 if (ino >= bctx->cur_objectid)
1118 /*if (ino > ctx->cur_objectid)
1120 if (offset + ctx->extent_len > ctx->cur_offset)
1125 found->found_refs++;
1126 if (ino < found->ino) {
1128 found->offset = offset;
1129 } else if (found->ino == ino) {
1131 * same extent found more then once in the same file.
1133 if (found->offset > offset + bctx->extent_len)
1134 found->offset = offset;
1141 * Given an inode, offset and extent item, it finds a good clone for a clone
1142 * instruction. Returns -ENOENT when none could be found. The function makes
1143 * sure that the returned clone is usable at the point where sending is at the
1144 * moment. This means, that no clones are accepted which lie behind the current
1147 * path must point to the extent item when called.
1149 static int find_extent_clone(struct send_ctx *sctx,
1150 struct btrfs_path *path,
1151 u64 ino, u64 data_offset,
1153 struct clone_root **found)
1159 u64 extent_item_pos;
1160 struct btrfs_file_extent_item *fi;
1161 struct extent_buffer *eb = path->nodes[0];
1162 struct backref_ctx *backref_ctx = NULL;
1163 struct clone_root *cur_clone_root;
1164 struct btrfs_key found_key;
1165 struct btrfs_path *tmp_path;
1168 tmp_path = alloc_path_for_send();
1172 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1178 if (data_offset >= ino_size) {
1180 * There may be extents that lie behind the file's size.
1181 * I at least had this in combination with snapshotting while
1182 * writing large files.
1188 fi = btrfs_item_ptr(eb, path->slots[0],
1189 struct btrfs_file_extent_item);
1190 extent_type = btrfs_file_extent_type(eb, fi);
1191 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1196 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1197 logical = btrfs_file_extent_disk_bytenr(eb, fi);
1202 logical += btrfs_file_extent_offset(eb, fi);
1204 ret = extent_from_logical(sctx->send_root->fs_info,
1205 logical, tmp_path, &found_key);
1206 btrfs_release_path(tmp_path);
1210 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1216 * Setup the clone roots.
1218 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1219 cur_clone_root = sctx->clone_roots + i;
1220 cur_clone_root->ino = (u64)-1;
1221 cur_clone_root->offset = 0;
1222 cur_clone_root->found_refs = 0;
1225 backref_ctx->sctx = sctx;
1226 backref_ctx->found = 0;
1227 backref_ctx->cur_objectid = ino;
1228 backref_ctx->cur_offset = data_offset;
1229 backref_ctx->found_itself = 0;
1230 backref_ctx->extent_len = num_bytes;
1233 * The last extent of a file may be too large due to page alignment.
1234 * We need to adjust extent_len in this case so that the checks in
1235 * __iterate_backrefs work.
1237 if (data_offset + num_bytes >= ino_size)
1238 backref_ctx->extent_len = ino_size - data_offset;
1241 * Now collect all backrefs.
1243 extent_item_pos = logical - found_key.objectid;
1244 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1245 found_key.objectid, extent_item_pos, 1,
1246 __iterate_backrefs, backref_ctx);
1250 if (!backref_ctx->found_itself) {
1251 /* found a bug in backref code? */
1253 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1254 "send_root. inode=%llu, offset=%llu, "
1256 ino, data_offset, logical);
1260 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1262 "num_bytes=%llu, logical=%llu\n",
1263 data_offset, ino, num_bytes, logical);
1265 if (!backref_ctx->found)
1266 verbose_printk("btrfs: no clones found\n");
1268 cur_clone_root = NULL;
1269 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1270 if (sctx->clone_roots[i].found_refs) {
1271 if (!cur_clone_root)
1272 cur_clone_root = sctx->clone_roots + i;
1273 else if (sctx->clone_roots[i].root == sctx->send_root)
1274 /* prefer clones from send_root over others */
1275 cur_clone_root = sctx->clone_roots + i;
1280 if (cur_clone_root) {
1281 *found = cur_clone_root;
1288 btrfs_free_path(tmp_path);
1293 static int read_symlink(struct send_ctx *sctx,
1294 struct btrfs_root *root,
1296 struct fs_path *dest)
1299 struct btrfs_path *path;
1300 struct btrfs_key key;
1301 struct btrfs_file_extent_item *ei;
1307 path = alloc_path_for_send();
1312 key.type = BTRFS_EXTENT_DATA_KEY;
1314 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1319 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1320 struct btrfs_file_extent_item);
1321 type = btrfs_file_extent_type(path->nodes[0], ei);
1322 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1323 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1324 BUG_ON(compression);
1326 off = btrfs_file_extent_inline_start(ei);
1327 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1329 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1334 btrfs_free_path(path);
1339 * Helper function to generate a file name that is unique in the root of
1340 * send_root and parent_root. This is used to generate names for orphan inodes.
1342 static int gen_unique_name(struct send_ctx *sctx,
1344 struct fs_path *dest)
1347 struct btrfs_path *path;
1348 struct btrfs_dir_item *di;
1353 path = alloc_path_for_send();
1358 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1360 if (len >= sizeof(tmp)) {
1361 /* should really not happen */
1366 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1367 path, BTRFS_FIRST_FREE_OBJECTID,
1368 tmp, strlen(tmp), 0);
1369 btrfs_release_path(path);
1375 /* not unique, try again */
1380 if (!sctx->parent_root) {
1386 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1387 path, BTRFS_FIRST_FREE_OBJECTID,
1388 tmp, strlen(tmp), 0);
1389 btrfs_release_path(path);
1395 /* not unique, try again */
1403 ret = fs_path_add(dest, tmp, strlen(tmp));
1406 btrfs_free_path(path);
1411 inode_state_no_change,
1412 inode_state_will_create,
1413 inode_state_did_create,
1414 inode_state_will_delete,
1415 inode_state_did_delete,
1418 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1426 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1428 if (ret < 0 && ret != -ENOENT)
1432 if (!sctx->parent_root) {
1433 right_ret = -ENOENT;
1435 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1436 NULL, NULL, NULL, NULL);
1437 if (ret < 0 && ret != -ENOENT)
1442 if (!left_ret && !right_ret) {
1443 if (left_gen == gen && right_gen == gen)
1444 ret = inode_state_no_change;
1445 else if (left_gen == gen) {
1446 if (ino < sctx->send_progress)
1447 ret = inode_state_did_create;
1449 ret = inode_state_will_create;
1450 } else if (right_gen == gen) {
1451 if (ino < sctx->send_progress)
1452 ret = inode_state_did_delete;
1454 ret = inode_state_will_delete;
1458 } else if (!left_ret) {
1459 if (left_gen == gen) {
1460 if (ino < sctx->send_progress)
1461 ret = inode_state_did_create;
1463 ret = inode_state_will_create;
1467 } else if (!right_ret) {
1468 if (right_gen == gen) {
1469 if (ino < sctx->send_progress)
1470 ret = inode_state_did_delete;
1472 ret = inode_state_will_delete;
1484 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1488 ret = get_cur_inode_state(sctx, ino, gen);
1492 if (ret == inode_state_no_change ||
1493 ret == inode_state_did_create ||
1494 ret == inode_state_will_delete)
1504 * Helper function to lookup a dir item in a dir.
1506 static int lookup_dir_item_inode(struct btrfs_root *root,
1507 u64 dir, const char *name, int name_len,
1512 struct btrfs_dir_item *di;
1513 struct btrfs_key key;
1514 struct btrfs_path *path;
1516 path = alloc_path_for_send();
1520 di = btrfs_lookup_dir_item(NULL, root, path,
1521 dir, name, name_len, 0);
1530 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1531 *found_inode = key.objectid;
1532 *found_type = btrfs_dir_type(path->nodes[0], di);
1535 btrfs_free_path(path);
1540 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1541 * generation of the parent dir and the name of the dir entry.
1543 static int get_first_ref(struct send_ctx *sctx,
1544 struct btrfs_root *root, u64 ino,
1545 u64 *dir, u64 *dir_gen, struct fs_path *name)
1548 struct btrfs_key key;
1549 struct btrfs_key found_key;
1550 struct btrfs_path *path;
1551 struct btrfs_inode_ref *iref;
1554 path = alloc_path_for_send();
1559 key.type = BTRFS_INODE_REF_KEY;
1562 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1566 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1568 if (ret || found_key.objectid != key.objectid ||
1569 found_key.type != key.type) {
1574 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1575 struct btrfs_inode_ref);
1576 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1577 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1578 (unsigned long)(iref + 1), len);
1581 btrfs_release_path(path);
1583 ret = get_inode_info(root, found_key.offset, NULL, dir_gen, NULL, NULL,
1588 *dir = found_key.offset;
1591 btrfs_free_path(path);
1595 static int is_first_ref(struct send_ctx *sctx,
1596 struct btrfs_root *root,
1598 const char *name, int name_len)
1601 struct fs_path *tmp_name;
1605 tmp_name = fs_path_alloc(sctx);
1609 ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1613 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1618 ret = memcmp(tmp_name->start, name, name_len);
1625 fs_path_free(sctx, tmp_name);
1630 * Used by process_recorded_refs to determine if a new ref would overwrite an
1631 * already existing ref. In case it detects an overwrite, it returns the
1632 * inode/gen in who_ino/who_gen.
1633 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1634 * to make sure later references to the overwritten inode are possible.
1635 * Orphanizing is however only required for the first ref of an inode.
1636 * process_recorded_refs does an additional is_first_ref check to see if
1637 * orphanizing is really required.
1639 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1640 const char *name, int name_len,
1641 u64 *who_ino, u64 *who_gen)
1644 u64 other_inode = 0;
1647 if (!sctx->parent_root)
1650 ret = is_inode_existent(sctx, dir, dir_gen);
1654 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1655 &other_inode, &other_type);
1656 if (ret < 0 && ret != -ENOENT)
1664 * Check if the overwritten ref was already processed. If yes, the ref
1665 * was already unlinked/moved, so we can safely assume that we will not
1666 * overwrite anything at this point in time.
1668 if (other_inode > sctx->send_progress) {
1669 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1670 who_gen, NULL, NULL, NULL, NULL);
1675 *who_ino = other_inode;
1685 * Checks if the ref was overwritten by an already processed inode. This is
1686 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1687 * thus the orphan name needs be used.
1688 * process_recorded_refs also uses it to avoid unlinking of refs that were
1691 static int did_overwrite_ref(struct send_ctx *sctx,
1692 u64 dir, u64 dir_gen,
1693 u64 ino, u64 ino_gen,
1694 const char *name, int name_len)
1701 if (!sctx->parent_root)
1704 ret = is_inode_existent(sctx, dir, dir_gen);
1708 /* check if the ref was overwritten by another ref */
1709 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1710 &ow_inode, &other_type);
1711 if (ret < 0 && ret != -ENOENT)
1714 /* was never and will never be overwritten */
1719 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1724 if (ow_inode == ino && gen == ino_gen) {
1729 /* we know that it is or will be overwritten. check this now */
1730 if (ow_inode < sctx->send_progress)
1740 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1741 * that got overwritten. This is used by process_recorded_refs to determine
1742 * if it has to use the path as returned by get_cur_path or the orphan name.
1744 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1747 struct fs_path *name = NULL;
1751 if (!sctx->parent_root)
1754 name = fs_path_alloc(sctx);
1758 ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name);
1762 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1763 name->start, fs_path_len(name));
1768 fs_path_free(sctx, name);
1773 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1774 * so we need to do some special handling in case we have clashes. This function
1775 * takes care of this with the help of name_cache_entry::radix_list.
1777 static int name_cache_insert(struct send_ctx *sctx,
1778 struct name_cache_entry *nce)
1781 struct list_head *nce_head;
1783 nce_head = radix_tree_lookup(&sctx->name_cache,
1784 (unsigned long)nce->ino);
1786 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1789 INIT_LIST_HEAD(nce_head);
1791 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1795 list_add_tail(&nce->radix_list, nce_head);
1796 list_add_tail(&nce->list, &sctx->name_cache_list);
1797 sctx->name_cache_size++;
1802 static void name_cache_delete(struct send_ctx *sctx,
1803 struct name_cache_entry *nce)
1805 struct list_head *nce_head;
1807 nce_head = radix_tree_lookup(&sctx->name_cache,
1808 (unsigned long)nce->ino);
1811 list_del(&nce->radix_list);
1812 list_del(&nce->list);
1813 sctx->name_cache_size--;
1815 if (list_empty(nce_head)) {
1816 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1821 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1824 struct list_head *nce_head;
1825 struct name_cache_entry *cur;
1827 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1831 list_for_each_entry(cur, nce_head, radix_list) {
1832 if (cur->ino == ino && cur->gen == gen)
1839 * Removes the entry from the list and adds it back to the end. This marks the
1840 * entry as recently used so that name_cache_clean_unused does not remove it.
1842 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1844 list_del(&nce->list);
1845 list_add_tail(&nce->list, &sctx->name_cache_list);
1849 * Remove some entries from the beginning of name_cache_list.
1851 static void name_cache_clean_unused(struct send_ctx *sctx)
1853 struct name_cache_entry *nce;
1855 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1858 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1859 nce = list_entry(sctx->name_cache_list.next,
1860 struct name_cache_entry, list);
1861 name_cache_delete(sctx, nce);
1866 static void name_cache_free(struct send_ctx *sctx)
1868 struct name_cache_entry *nce;
1869 struct name_cache_entry *tmp;
1871 list_for_each_entry_safe(nce, tmp, &sctx->name_cache_list, list) {
1872 name_cache_delete(sctx, nce);
1878 * Used by get_cur_path for each ref up to the root.
1879 * Returns 0 if it succeeded.
1880 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1881 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1882 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1883 * Returns <0 in case of error.
1885 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1889 struct fs_path *dest)
1893 struct btrfs_path *path = NULL;
1894 struct name_cache_entry *nce = NULL;
1897 * First check if we already did a call to this function with the same
1898 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1899 * return the cached result.
1901 nce = name_cache_search(sctx, ino, gen);
1903 if (ino < sctx->send_progress && nce->need_later_update) {
1904 name_cache_delete(sctx, nce);
1908 name_cache_used(sctx, nce);
1909 *parent_ino = nce->parent_ino;
1910 *parent_gen = nce->parent_gen;
1911 ret = fs_path_add(dest, nce->name, nce->name_len);
1919 path = alloc_path_for_send();
1924 * If the inode is not existent yet, add the orphan name and return 1.
1925 * This should only happen for the parent dir that we determine in
1928 ret = is_inode_existent(sctx, ino, gen);
1933 ret = gen_unique_name(sctx, ino, gen, dest);
1941 * Depending on whether the inode was already processed or not, use
1942 * send_root or parent_root for ref lookup.
1944 if (ino < sctx->send_progress)
1945 ret = get_first_ref(sctx, sctx->send_root, ino,
1946 parent_ino, parent_gen, dest);
1948 ret = get_first_ref(sctx, sctx->parent_root, ino,
1949 parent_ino, parent_gen, dest);
1954 * Check if the ref was overwritten by an inode's ref that was processed
1955 * earlier. If yes, treat as orphan and return 1.
1957 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1958 dest->start, dest->end - dest->start);
1962 fs_path_reset(dest);
1963 ret = gen_unique_name(sctx, ino, gen, dest);
1971 * Store the result of the lookup in the name cache.
1973 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
1981 nce->parent_ino = *parent_ino;
1982 nce->parent_gen = *parent_gen;
1983 nce->name_len = fs_path_len(dest);
1985 strcpy(nce->name, dest->start);
1987 if (ino < sctx->send_progress)
1988 nce->need_later_update = 0;
1990 nce->need_later_update = 1;
1992 nce_ret = name_cache_insert(sctx, nce);
1995 name_cache_clean_unused(sctx);
1998 btrfs_free_path(path);
2003 * Magic happens here. This function returns the first ref to an inode as it
2004 * would look like while receiving the stream at this point in time.
2005 * We walk the path up to the root. For every inode in between, we check if it
2006 * was already processed/sent. If yes, we continue with the parent as found
2007 * in send_root. If not, we continue with the parent as found in parent_root.
2008 * If we encounter an inode that was deleted at this point in time, we use the
2009 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2010 * that were not created yet and overwritten inodes/refs.
2012 * When do we have have orphan inodes:
2013 * 1. When an inode is freshly created and thus no valid refs are available yet
2014 * 2. When a directory lost all it's refs (deleted) but still has dir items
2015 * inside which were not processed yet (pending for move/delete). If anyone
2016 * tried to get the path to the dir items, it would get a path inside that
2018 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2019 * of an unprocessed inode. If in that case the first ref would be
2020 * overwritten, the overwritten inode gets "orphanized". Later when we
2021 * process this overwritten inode, it is restored at a new place by moving
2024 * sctx->send_progress tells this function at which point in time receiving
2027 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2028 struct fs_path *dest)
2031 struct fs_path *name = NULL;
2032 u64 parent_inode = 0;
2036 name = fs_path_alloc(sctx);
2043 fs_path_reset(dest);
2045 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2046 fs_path_reset(name);
2048 ret = __get_cur_name_and_parent(sctx, ino, gen,
2049 &parent_inode, &parent_gen, name);
2055 ret = fs_path_add_path(dest, name);
2064 fs_path_free(sctx, name);
2066 fs_path_unreverse(dest);
2071 * Called for regular files when sending extents data. Opens a struct file
2072 * to read from the file.
2074 static int open_cur_inode_file(struct send_ctx *sctx)
2077 struct btrfs_key key;
2079 struct inode *inode;
2080 struct dentry *dentry;
2084 if (sctx->cur_inode_filp)
2087 key.objectid = sctx->cur_ino;
2088 key.type = BTRFS_INODE_ITEM_KEY;
2091 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2093 if (IS_ERR(inode)) {
2094 ret = PTR_ERR(inode);
2098 dentry = d_obtain_alias(inode);
2100 if (IS_ERR(dentry)) {
2101 ret = PTR_ERR(dentry);
2105 path.mnt = sctx->mnt;
2106 path.dentry = dentry;
2107 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2111 ret = PTR_ERR(filp);
2114 sctx->cur_inode_filp = filp;
2118 * no xxxput required here as every vfs op
2119 * does it by itself on failure
2125 * Closes the struct file that was created in open_cur_inode_file
2127 static int close_cur_inode_file(struct send_ctx *sctx)
2131 if (!sctx->cur_inode_filp)
2134 ret = filp_close(sctx->cur_inode_filp, NULL);
2135 sctx->cur_inode_filp = NULL;
2142 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2144 static int send_subvol_begin(struct send_ctx *sctx)
2147 struct btrfs_root *send_root = sctx->send_root;
2148 struct btrfs_root *parent_root = sctx->parent_root;
2149 struct btrfs_path *path;
2150 struct btrfs_key key;
2151 struct btrfs_root_ref *ref;
2152 struct extent_buffer *leaf;
2156 path = alloc_path_for_send();
2160 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2162 btrfs_free_path(path);
2166 key.objectid = send_root->objectid;
2167 key.type = BTRFS_ROOT_BACKREF_KEY;
2170 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2179 leaf = path->nodes[0];
2180 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2181 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2182 key.objectid != send_root->objectid) {
2186 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2187 namelen = btrfs_root_ref_name_len(leaf, ref);
2188 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2189 btrfs_release_path(path);
2195 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2199 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2204 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2205 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2206 sctx->send_root->root_item.uuid);
2207 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2208 sctx->send_root->root_item.ctransid);
2210 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2211 sctx->parent_root->root_item.uuid);
2212 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2213 sctx->parent_root->root_item.ctransid);
2216 ret = send_cmd(sctx);
2220 btrfs_free_path(path);
2225 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2230 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2232 p = fs_path_alloc(sctx);
2236 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2240 ret = get_cur_path(sctx, ino, gen, p);
2243 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2244 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2246 ret = send_cmd(sctx);
2250 fs_path_free(sctx, p);
2254 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2259 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2261 p = fs_path_alloc(sctx);
2265 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2269 ret = get_cur_path(sctx, ino, gen, p);
2272 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2273 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2275 ret = send_cmd(sctx);
2279 fs_path_free(sctx, p);
2283 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2288 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2290 p = fs_path_alloc(sctx);
2294 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2298 ret = get_cur_path(sctx, ino, gen, p);
2301 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2302 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2303 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2305 ret = send_cmd(sctx);
2309 fs_path_free(sctx, p);
2313 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2316 struct fs_path *p = NULL;
2317 struct btrfs_inode_item *ii;
2318 struct btrfs_path *path = NULL;
2319 struct extent_buffer *eb;
2320 struct btrfs_key key;
2323 verbose_printk("btrfs: send_utimes %llu\n", ino);
2325 p = fs_path_alloc(sctx);
2329 path = alloc_path_for_send();
2336 key.type = BTRFS_INODE_ITEM_KEY;
2338 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2342 eb = path->nodes[0];
2343 slot = path->slots[0];
2344 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2346 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2350 ret = get_cur_path(sctx, ino, gen, p);
2353 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2354 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2355 btrfs_inode_atime(ii));
2356 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2357 btrfs_inode_mtime(ii));
2358 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2359 btrfs_inode_ctime(ii));
2360 /* TODO Add otime support when the otime patches get into upstream */
2362 ret = send_cmd(sctx);
2366 fs_path_free(sctx, p);
2367 btrfs_free_path(path);
2372 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2373 * a valid path yet because we did not process the refs yet. So, the inode
2374 * is created as orphan.
2376 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2385 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2387 p = fs_path_alloc(sctx);
2391 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2397 cmd = BTRFS_SEND_C_MKFILE;
2398 else if (S_ISDIR(mode))
2399 cmd = BTRFS_SEND_C_MKDIR;
2400 else if (S_ISLNK(mode))
2401 cmd = BTRFS_SEND_C_SYMLINK;
2402 else if (S_ISCHR(mode) || S_ISBLK(mode))
2403 cmd = BTRFS_SEND_C_MKNOD;
2404 else if (S_ISFIFO(mode))
2405 cmd = BTRFS_SEND_C_MKFIFO;
2406 else if (S_ISSOCK(mode))
2407 cmd = BTRFS_SEND_C_MKSOCK;
2409 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2410 (int)(mode & S_IFMT));
2415 ret = begin_cmd(sctx, cmd);
2419 ret = gen_unique_name(sctx, ino, gen, p);
2423 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2424 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2426 if (S_ISLNK(mode)) {
2428 ret = read_symlink(sctx, sctx->send_root, ino, p);
2431 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2432 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2433 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2434 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, rdev);
2437 ret = send_cmd(sctx);
2444 fs_path_free(sctx, p);
2449 * We need some special handling for inodes that get processed before the parent
2450 * directory got created. See process_recorded_refs for details.
2451 * This function does the check if we already created the dir out of order.
2453 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2456 struct btrfs_path *path = NULL;
2457 struct btrfs_key key;
2458 struct btrfs_key found_key;
2459 struct btrfs_key di_key;
2460 struct extent_buffer *eb;
2461 struct btrfs_dir_item *di;
2464 path = alloc_path_for_send();
2471 key.type = BTRFS_DIR_INDEX_KEY;
2474 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2479 eb = path->nodes[0];
2480 slot = path->slots[0];
2481 btrfs_item_key_to_cpu(eb, &found_key, slot);
2483 if (ret || found_key.objectid != key.objectid ||
2484 found_key.type != key.type) {
2489 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2490 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2492 if (di_key.objectid < sctx->send_progress) {
2497 key.offset = found_key.offset + 1;
2498 btrfs_release_path(path);
2502 btrfs_free_path(path);
2507 * Only creates the inode if it is:
2508 * 1. Not a directory
2509 * 2. Or a directory which was not created already due to out of order
2510 * directories. See did_create_dir and process_recorded_refs for details.
2512 static int send_create_inode_if_needed(struct send_ctx *sctx)
2516 if (S_ISDIR(sctx->cur_inode_mode)) {
2517 ret = did_create_dir(sctx, sctx->cur_ino);
2526 ret = send_create_inode(sctx, sctx->cur_ino);
2534 struct recorded_ref {
2535 struct list_head list;
2538 struct fs_path *full_path;
2546 * We need to process new refs before deleted refs, but compare_tree gives us
2547 * everything mixed. So we first record all refs and later process them.
2548 * This function is a helper to record one ref.
2550 static int record_ref(struct list_head *head, u64 dir,
2551 u64 dir_gen, struct fs_path *path)
2553 struct recorded_ref *ref;
2556 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2561 ref->dir_gen = dir_gen;
2562 ref->full_path = path;
2564 tmp = strrchr(ref->full_path->start, '/');
2566 ref->name_len = ref->full_path->end - ref->full_path->start;
2567 ref->name = ref->full_path->start;
2568 ref->dir_path_len = 0;
2569 ref->dir_path = ref->full_path->start;
2572 ref->name_len = ref->full_path->end - tmp;
2574 ref->dir_path = ref->full_path->start;
2575 ref->dir_path_len = ref->full_path->end -
2576 ref->full_path->start - 1 - ref->name_len;
2579 list_add_tail(&ref->list, head);
2583 static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
2585 struct recorded_ref *cur;
2586 struct recorded_ref *tmp;
2588 list_for_each_entry_safe(cur, tmp, head, list) {
2589 fs_path_free(sctx, cur->full_path);
2592 INIT_LIST_HEAD(head);
2595 static void free_recorded_refs(struct send_ctx *sctx)
2597 __free_recorded_refs(sctx, &sctx->new_refs);
2598 __free_recorded_refs(sctx, &sctx->deleted_refs);
2602 * Renames/moves a file/dir to its orphan name. Used when the first
2603 * ref of an unprocessed inode gets overwritten and for all non empty
2606 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2607 struct fs_path *path)
2610 struct fs_path *orphan;
2612 orphan = fs_path_alloc(sctx);
2616 ret = gen_unique_name(sctx, ino, gen, orphan);
2620 ret = send_rename(sctx, path, orphan);
2623 fs_path_free(sctx, orphan);
2628 * Returns 1 if a directory can be removed at this point in time.
2629 * We check this by iterating all dir items and checking if the inode behind
2630 * the dir item was already processed.
2632 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2635 struct btrfs_root *root = sctx->parent_root;
2636 struct btrfs_path *path;
2637 struct btrfs_key key;
2638 struct btrfs_key found_key;
2639 struct btrfs_key loc;
2640 struct btrfs_dir_item *di;
2642 path = alloc_path_for_send();
2647 key.type = BTRFS_DIR_INDEX_KEY;
2651 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2655 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2658 if (ret || found_key.objectid != key.objectid ||
2659 found_key.type != key.type) {
2663 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2664 struct btrfs_dir_item);
2665 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2667 if (loc.objectid > send_progress) {
2672 btrfs_release_path(path);
2673 key.offset = found_key.offset + 1;
2679 btrfs_free_path(path);
2684 * This does all the move/link/unlink/rmdir magic.
2686 static int process_recorded_refs(struct send_ctx *sctx)
2689 struct recorded_ref *cur;
2690 struct recorded_ref *cur2;
2691 struct ulist *check_dirs = NULL;
2692 struct ulist_iterator uit;
2693 struct ulist_node *un;
2694 struct fs_path *valid_path = NULL;
2697 int did_overwrite = 0;
2700 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2702 valid_path = fs_path_alloc(sctx);
2708 check_dirs = ulist_alloc(GFP_NOFS);
2715 * First, check if the first ref of the current inode was overwritten
2716 * before. If yes, we know that the current inode was already orphanized
2717 * and thus use the orphan name. If not, we can use get_cur_path to
2718 * get the path of the first ref as it would like while receiving at
2719 * this point in time.
2720 * New inodes are always orphan at the beginning, so force to use the
2721 * orphan name in this case.
2722 * The first ref is stored in valid_path and will be updated if it
2723 * gets moved around.
2725 if (!sctx->cur_inode_new) {
2726 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2727 sctx->cur_inode_gen);
2733 if (sctx->cur_inode_new || did_overwrite) {
2734 ret = gen_unique_name(sctx, sctx->cur_ino,
2735 sctx->cur_inode_gen, valid_path);
2740 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2746 list_for_each_entry(cur, &sctx->new_refs, list) {
2748 * We may have refs where the parent directory does not exist
2749 * yet. This happens if the parent directories inum is higher
2750 * the the current inum. To handle this case, we create the
2751 * parent directory out of order. But we need to check if this
2752 * did already happen before due to other refs in the same dir.
2754 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2757 if (ret == inode_state_will_create) {
2760 * First check if any of the current inodes refs did
2761 * already create the dir.
2763 list_for_each_entry(cur2, &sctx->new_refs, list) {
2766 if (cur2->dir == cur->dir) {
2773 * If that did not happen, check if a previous inode
2774 * did already create the dir.
2777 ret = did_create_dir(sctx, cur->dir);
2781 ret = send_create_inode(sctx, cur->dir);
2788 * Check if this new ref would overwrite the first ref of
2789 * another unprocessed inode. If yes, orphanize the
2790 * overwritten inode. If we find an overwritten ref that is
2791 * not the first ref, simply unlink it.
2793 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2794 cur->name, cur->name_len,
2795 &ow_inode, &ow_gen);
2799 ret = is_first_ref(sctx, sctx->parent_root,
2800 ow_inode, cur->dir, cur->name,
2805 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2810 ret = send_unlink(sctx, cur->full_path);
2817 * link/move the ref to the new place. If we have an orphan
2818 * inode, move it and update valid_path. If not, link or move
2819 * it depending on the inode mode.
2822 ret = send_rename(sctx, valid_path, cur->full_path);
2826 ret = fs_path_copy(valid_path, cur->full_path);
2830 if (S_ISDIR(sctx->cur_inode_mode)) {
2832 * Dirs can't be linked, so move it. For moved
2833 * dirs, we always have one new and one deleted
2834 * ref. The deleted ref is ignored later.
2836 ret = send_rename(sctx, valid_path,
2840 ret = fs_path_copy(valid_path, cur->full_path);
2844 ret = send_link(sctx, cur->full_path,
2850 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2856 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2858 * Check if we can already rmdir the directory. If not,
2859 * orphanize it. For every dir item inside that gets deleted
2860 * later, we do this check again and rmdir it then if possible.
2861 * See the use of check_dirs for more details.
2863 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2867 ret = send_rmdir(sctx, valid_path);
2870 } else if (!is_orphan) {
2871 ret = orphanize_inode(sctx, sctx->cur_ino,
2872 sctx->cur_inode_gen, valid_path);
2878 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2879 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2884 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2885 !list_empty(&sctx->deleted_refs)) {
2887 * We have a moved dir. Add the old parent to check_dirs
2889 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2891 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2895 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2897 * We have a non dir inode. Go through all deleted refs and
2898 * unlink them if they were not already overwritten by other
2901 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2902 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2903 sctx->cur_ino, sctx->cur_inode_gen,
2904 cur->name, cur->name_len);
2908 ret = send_unlink(sctx, cur->full_path);
2912 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2919 * If the inode is still orphan, unlink the orphan. This may
2920 * happen when a previous inode did overwrite the first ref
2921 * of this inode and no new refs were added for the current
2922 * inode. Unlinking does not mean that the inode is deleted in
2923 * all cases. There may still be links to this inode in other
2927 ret = send_unlink(sctx, valid_path);
2934 * We did collect all parent dirs where cur_inode was once located. We
2935 * now go through all these dirs and check if they are pending for
2936 * deletion and if it's finally possible to perform the rmdir now.
2937 * We also update the inode stats of the parent dirs here.
2939 ULIST_ITER_INIT(&uit);
2940 while ((un = ulist_next(check_dirs, &uit))) {
2942 * In case we had refs into dirs that were not processed yet,
2943 * we don't need to do the utime and rmdir logic for these dirs.
2944 * The dir will be processed later.
2946 if (un->val > sctx->cur_ino)
2949 ret = get_cur_inode_state(sctx, un->val, un->aux);
2953 if (ret == inode_state_did_create ||
2954 ret == inode_state_no_change) {
2955 /* TODO delayed utimes */
2956 ret = send_utimes(sctx, un->val, un->aux);
2959 } else if (ret == inode_state_did_delete) {
2960 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
2964 ret = get_cur_path(sctx, un->val, un->aux,
2968 ret = send_rmdir(sctx, valid_path);
2978 free_recorded_refs(sctx);
2979 ulist_free(check_dirs);
2980 fs_path_free(sctx, valid_path);
2984 static int __record_new_ref(int num, u64 dir, int index,
2985 struct fs_path *name,
2989 struct send_ctx *sctx = ctx;
2993 p = fs_path_alloc(sctx);
2997 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
3002 ret = get_cur_path(sctx, dir, gen, p);
3005 ret = fs_path_add_path(p, name);
3009 ret = record_ref(&sctx->new_refs, dir, gen, p);
3013 fs_path_free(sctx, p);
3017 static int __record_deleted_ref(int num, u64 dir, int index,
3018 struct fs_path *name,
3022 struct send_ctx *sctx = ctx;
3026 p = fs_path_alloc(sctx);
3030 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3035 ret = get_cur_path(sctx, dir, gen, p);
3038 ret = fs_path_add_path(p, name);
3042 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3046 fs_path_free(sctx, p);
3050 static int record_new_ref(struct send_ctx *sctx)
3054 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3055 sctx->cmp_key, 0, __record_new_ref, sctx);
3064 static int record_deleted_ref(struct send_ctx *sctx)
3068 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3069 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3078 struct find_ref_ctx {
3080 struct fs_path *name;
3084 static int __find_iref(int num, u64 dir, int index,
3085 struct fs_path *name,
3088 struct find_ref_ctx *ctx = ctx_;
3090 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3091 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3092 ctx->found_idx = num;
3098 static int find_iref(struct send_ctx *sctx,
3099 struct btrfs_root *root,
3100 struct btrfs_path *path,
3101 struct btrfs_key *key,
3102 u64 dir, struct fs_path *name)
3105 struct find_ref_ctx ctx;
3111 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3115 if (ctx.found_idx == -1)
3118 return ctx.found_idx;
3121 static int __record_changed_new_ref(int num, u64 dir, int index,
3122 struct fs_path *name,
3126 struct send_ctx *sctx = ctx;
3128 ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
3129 sctx->cmp_key, dir, name);
3131 ret = __record_new_ref(num, dir, index, name, sctx);
3138 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3139 struct fs_path *name,
3143 struct send_ctx *sctx = ctx;
3145 ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3148 ret = __record_deleted_ref(num, dir, index, name, sctx);
3155 static int record_changed_ref(struct send_ctx *sctx)
3159 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3160 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3163 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3164 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3174 * Record and process all refs at once. Needed when an inode changes the
3175 * generation number, which means that it was deleted and recreated.
3177 static int process_all_refs(struct send_ctx *sctx,
3178 enum btrfs_compare_tree_result cmd)
3181 struct btrfs_root *root;
3182 struct btrfs_path *path;
3183 struct btrfs_key key;
3184 struct btrfs_key found_key;
3185 struct extent_buffer *eb;
3187 iterate_inode_ref_t cb;
3189 path = alloc_path_for_send();
3193 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3194 root = sctx->send_root;
3195 cb = __record_new_ref;
3196 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3197 root = sctx->parent_root;
3198 cb = __record_deleted_ref;
3203 key.objectid = sctx->cmp_key->objectid;
3204 key.type = BTRFS_INODE_REF_KEY;
3207 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3209 btrfs_release_path(path);
3213 btrfs_release_path(path);
3217 eb = path->nodes[0];
3218 slot = path->slots[0];
3219 btrfs_item_key_to_cpu(eb, &found_key, slot);
3221 if (found_key.objectid != key.objectid ||
3222 found_key.type != key.type) {
3223 btrfs_release_path(path);
3227 ret = iterate_inode_ref(sctx, sctx->parent_root, path,
3228 &found_key, 0, cb, sctx);
3229 btrfs_release_path(path);
3233 key.offset = found_key.offset + 1;
3236 ret = process_recorded_refs(sctx);
3239 btrfs_free_path(path);
3243 static int send_set_xattr(struct send_ctx *sctx,
3244 struct fs_path *path,
3245 const char *name, int name_len,
3246 const char *data, int data_len)
3250 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3254 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3255 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3256 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3258 ret = send_cmd(sctx);
3265 static int send_remove_xattr(struct send_ctx *sctx,
3266 struct fs_path *path,
3267 const char *name, int name_len)
3271 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3275 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3276 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3278 ret = send_cmd(sctx);
3285 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3286 const char *name, int name_len,
3287 const char *data, int data_len,
3291 struct send_ctx *sctx = ctx;
3293 posix_acl_xattr_header dummy_acl;
3295 p = fs_path_alloc(sctx);
3300 * This hack is needed because empty acl's are stored as zero byte
3301 * data in xattrs. Problem with that is, that receiving these zero byte
3302 * acl's will fail later. To fix this, we send a dummy acl list that
3303 * only contains the version number and no entries.
3305 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3306 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3307 if (data_len == 0) {
3308 dummy_acl.a_version =
3309 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3310 data = (char *)&dummy_acl;
3311 data_len = sizeof(dummy_acl);
3315 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3319 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3322 fs_path_free(sctx, p);
3326 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3327 const char *name, int name_len,
3328 const char *data, int data_len,
3332 struct send_ctx *sctx = ctx;
3335 p = fs_path_alloc(sctx);
3339 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3343 ret = send_remove_xattr(sctx, p, name, name_len);
3346 fs_path_free(sctx, p);
3350 static int process_new_xattr(struct send_ctx *sctx)
3354 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3355 sctx->cmp_key, __process_new_xattr, sctx);
3360 static int process_deleted_xattr(struct send_ctx *sctx)
3364 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3365 sctx->cmp_key, __process_deleted_xattr, sctx);
3370 struct find_xattr_ctx {
3378 static int __find_xattr(int num, struct btrfs_key *di_key,
3379 const char *name, int name_len,
3380 const char *data, int data_len,
3381 u8 type, void *vctx)
3383 struct find_xattr_ctx *ctx = vctx;
3385 if (name_len == ctx->name_len &&
3386 strncmp(name, ctx->name, name_len) == 0) {
3387 ctx->found_idx = num;
3388 ctx->found_data_len = data_len;
3389 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3390 if (!ctx->found_data)
3392 memcpy(ctx->found_data, data, data_len);
3398 static int find_xattr(struct send_ctx *sctx,
3399 struct btrfs_root *root,
3400 struct btrfs_path *path,
3401 struct btrfs_key *key,
3402 const char *name, int name_len,
3403 char **data, int *data_len)
3406 struct find_xattr_ctx ctx;
3409 ctx.name_len = name_len;
3411 ctx.found_data = NULL;
3412 ctx.found_data_len = 0;
3414 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3418 if (ctx.found_idx == -1)
3421 *data = ctx.found_data;
3422 *data_len = ctx.found_data_len;
3424 kfree(ctx.found_data);
3426 return ctx.found_idx;
3430 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3431 const char *name, int name_len,
3432 const char *data, int data_len,
3436 struct send_ctx *sctx = ctx;
3437 char *found_data = NULL;
3438 int found_data_len = 0;
3439 struct fs_path *p = NULL;
3441 ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
3442 sctx->cmp_key, name, name_len, &found_data,
3444 if (ret == -ENOENT) {
3445 ret = __process_new_xattr(num, di_key, name, name_len, data,
3446 data_len, type, ctx);
3447 } else if (ret >= 0) {
3448 if (data_len != found_data_len ||
3449 memcmp(data, found_data, data_len)) {
3450 ret = __process_new_xattr(num, di_key, name, name_len,
3451 data, data_len, type, ctx);
3458 fs_path_free(sctx, p);
3462 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3463 const char *name, int name_len,
3464 const char *data, int data_len,
3468 struct send_ctx *sctx = ctx;
3470 ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3471 name, name_len, NULL, NULL);
3473 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3474 data_len, type, ctx);
3481 static int process_changed_xattr(struct send_ctx *sctx)
3485 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3486 sctx->cmp_key, __process_changed_new_xattr, sctx);
3489 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3490 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3496 static int process_all_new_xattrs(struct send_ctx *sctx)
3499 struct btrfs_root *root;
3500 struct btrfs_path *path;
3501 struct btrfs_key key;
3502 struct btrfs_key found_key;
3503 struct extent_buffer *eb;
3506 path = alloc_path_for_send();
3510 root = sctx->send_root;
3512 key.objectid = sctx->cmp_key->objectid;
3513 key.type = BTRFS_XATTR_ITEM_KEY;
3516 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3524 eb = path->nodes[0];
3525 slot = path->slots[0];
3526 btrfs_item_key_to_cpu(eb, &found_key, slot);
3528 if (found_key.objectid != key.objectid ||
3529 found_key.type != key.type) {
3534 ret = iterate_dir_item(sctx, root, path, &found_key,
3535 __process_new_xattr, sctx);
3539 btrfs_release_path(path);
3540 key.offset = found_key.offset + 1;
3544 btrfs_free_path(path);
3549 * Read some bytes from the current inode/file and send a write command to
3552 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3556 loff_t pos = offset;
3558 mm_segment_t old_fs;
3560 p = fs_path_alloc(sctx);
3565 * vfs normally only accepts user space buffers for security reasons.
3566 * we only read from the file and also only provide the read_buf buffer
3567 * to vfs. As this buffer does not come from a user space call, it's
3568 * ok to temporary allow kernel space buffers.
3573 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3575 ret = open_cur_inode_file(sctx);
3579 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3586 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3590 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3594 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3595 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3596 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, readed);
3598 ret = send_cmd(sctx);
3602 fs_path_free(sctx, p);
3610 * Send a clone command to user space.
3612 static int send_clone(struct send_ctx *sctx,
3613 u64 offset, u32 len,
3614 struct clone_root *clone_root)
3617 struct btrfs_root *clone_root2 = clone_root->root;
3621 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3622 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3623 clone_root->root->objectid, clone_root->ino,
3624 clone_root->offset);
3626 p = fs_path_alloc(sctx);
3630 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3634 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3638 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3639 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3640 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3642 if (clone_root2 == sctx->send_root) {
3643 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3644 &gen, NULL, NULL, NULL, NULL);
3647 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3649 ret = get_inode_path(sctx, clone_root2, clone_root->ino, p);
3654 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3655 clone_root2->root_item.uuid);
3656 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3657 clone_root2->root_item.ctransid);
3658 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3659 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3660 clone_root->offset);
3662 ret = send_cmd(sctx);
3666 fs_path_free(sctx, p);
3670 static int send_write_or_clone(struct send_ctx *sctx,
3671 struct btrfs_path *path,
3672 struct btrfs_key *key,
3673 struct clone_root *clone_root)
3676 struct btrfs_file_extent_item *ei;
3677 u64 offset = key->offset;
3683 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3684 struct btrfs_file_extent_item);
3685 type = btrfs_file_extent_type(path->nodes[0], ei);
3686 if (type == BTRFS_FILE_EXTENT_INLINE)
3687 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3689 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3691 if (offset + len > sctx->cur_inode_size)
3692 len = sctx->cur_inode_size - offset;
3701 if (l > BTRFS_SEND_READ_SIZE)
3702 l = BTRFS_SEND_READ_SIZE;
3703 ret = send_write(sctx, pos + offset, l);
3712 ret = send_clone(sctx, offset, len, clone_root);
3719 static int is_extent_unchanged(struct send_ctx *sctx,
3720 struct btrfs_path *left_path,
3721 struct btrfs_key *ekey)
3724 struct btrfs_key key;
3725 struct btrfs_path *path = NULL;
3726 struct extent_buffer *eb;
3728 struct btrfs_key found_key;
3729 struct btrfs_file_extent_item *ei;
3734 u64 left_offset_fixed;
3740 path = alloc_path_for_send();
3744 eb = left_path->nodes[0];
3745 slot = left_path->slots[0];
3747 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3748 left_type = btrfs_file_extent_type(eb, ei);
3749 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3750 left_len = btrfs_file_extent_num_bytes(eb, ei);
3751 left_offset = btrfs_file_extent_offset(eb, ei);
3753 if (left_type != BTRFS_FILE_EXTENT_REG) {
3759 * Following comments will refer to these graphics. L is the left
3760 * extents which we are checking at the moment. 1-8 are the right
3761 * extents that we iterate.
3764 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3767 * |--1--|-2b-|...(same as above)
3769 * Alternative situation. Happens on files where extents got split.
3771 * |-----------7-----------|-6-|
3773 * Alternative situation. Happens on files which got larger.
3776 * Nothing follows after 8.
3779 key.objectid = ekey->objectid;
3780 key.type = BTRFS_EXTENT_DATA_KEY;
3781 key.offset = ekey->offset;
3782 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3791 * Handle special case where the right side has no extents at all.
3793 eb = path->nodes[0];
3794 slot = path->slots[0];
3795 btrfs_item_key_to_cpu(eb, &found_key, slot);
3796 if (found_key.objectid != key.objectid ||
3797 found_key.type != key.type) {
3803 * We're now on 2a, 2b or 7.
3806 while (key.offset < ekey->offset + left_len) {
3807 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3808 right_type = btrfs_file_extent_type(eb, ei);
3809 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3810 right_len = btrfs_file_extent_num_bytes(eb, ei);
3811 right_offset = btrfs_file_extent_offset(eb, ei);
3813 if (right_type != BTRFS_FILE_EXTENT_REG) {
3819 * Are we at extent 8? If yes, we know the extent is changed.
3820 * This may only happen on the first iteration.
3822 if (found_key.offset + right_len < ekey->offset) {
3827 left_offset_fixed = left_offset;
3828 if (key.offset < ekey->offset) {
3829 /* Fix the right offset for 2a and 7. */
3830 right_offset += ekey->offset - key.offset;
3832 /* Fix the left offset for all behind 2a and 2b */
3833 left_offset_fixed += key.offset - ekey->offset;
3837 * Check if we have the same extent.
3839 if (left_disknr + left_offset_fixed !=
3840 right_disknr + right_offset) {
3846 * Go to the next extent.
3848 ret = btrfs_next_item(sctx->parent_root, path);
3852 eb = path->nodes[0];
3853 slot = path->slots[0];
3854 btrfs_item_key_to_cpu(eb, &found_key, slot);
3856 if (ret || found_key.objectid != key.objectid ||
3857 found_key.type != key.type) {
3858 key.offset += right_len;
3861 if (found_key.offset != key.offset + right_len) {
3862 /* Should really not happen */
3871 * We're now behind the left extent (treat as unchanged) or at the end
3872 * of the right side (treat as changed).
3874 if (key.offset >= ekey->offset + left_len)
3881 btrfs_free_path(path);
3885 static int process_extent(struct send_ctx *sctx,
3886 struct btrfs_path *path,
3887 struct btrfs_key *key)
3890 struct clone_root *found_clone = NULL;
3892 if (S_ISLNK(sctx->cur_inode_mode))
3895 if (sctx->parent_root && !sctx->cur_inode_new) {
3896 ret = is_extent_unchanged(sctx, path, key);
3905 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3906 sctx->cur_inode_size, &found_clone);
3907 if (ret != -ENOENT && ret < 0)
3910 ret = send_write_or_clone(sctx, path, key, found_clone);
3916 static int process_all_extents(struct send_ctx *sctx)
3919 struct btrfs_root *root;
3920 struct btrfs_path *path;
3921 struct btrfs_key key;
3922 struct btrfs_key found_key;
3923 struct extent_buffer *eb;
3926 root = sctx->send_root;
3927 path = alloc_path_for_send();
3931 key.objectid = sctx->cmp_key->objectid;
3932 key.type = BTRFS_EXTENT_DATA_KEY;
3935 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3943 eb = path->nodes[0];
3944 slot = path->slots[0];
3945 btrfs_item_key_to_cpu(eb, &found_key, slot);
3947 if (found_key.objectid != key.objectid ||
3948 found_key.type != key.type) {
3953 ret = process_extent(sctx, path, &found_key);
3957 btrfs_release_path(path);
3958 key.offset = found_key.offset + 1;
3962 btrfs_free_path(path);
3966 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
3970 if (sctx->cur_ino == 0)
3972 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
3973 sctx->cmp_key->type <= BTRFS_INODE_REF_KEY)
3975 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
3978 ret = process_recorded_refs(sctx);
3983 * We have processed the refs and thus need to advance send_progress.
3984 * Now, calls to get_cur_xxx will take the updated refs of the current
3985 * inode into account.
3987 sctx->send_progress = sctx->cur_ino + 1;
3993 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4005 ret = process_recorded_refs_if_needed(sctx, at_end);
4009 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4011 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4014 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4015 &left_mode, &left_uid, &left_gid, NULL);
4019 if (!S_ISLNK(sctx->cur_inode_mode)) {
4020 if (!sctx->parent_root || sctx->cur_inode_new) {
4024 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4025 NULL, NULL, &right_mode, &right_uid,
4030 if (left_uid != right_uid || left_gid != right_gid)
4032 if (left_mode != right_mode)
4037 if (S_ISREG(sctx->cur_inode_mode)) {
4038 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4039 sctx->cur_inode_size);
4045 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4046 left_uid, left_gid);
4051 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4058 * Need to send that every time, no matter if it actually changed
4059 * between the two trees as we have done changes to the inode before.
4061 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4069 static int changed_inode(struct send_ctx *sctx,
4070 enum btrfs_compare_tree_result result)
4073 struct btrfs_key *key = sctx->cmp_key;
4074 struct btrfs_inode_item *left_ii = NULL;
4075 struct btrfs_inode_item *right_ii = NULL;
4079 ret = close_cur_inode_file(sctx);
4083 sctx->cur_ino = key->objectid;
4084 sctx->cur_inode_new_gen = 0;
4087 * Set send_progress to current inode. This will tell all get_cur_xxx
4088 * functions that the current inode's refs are not updated yet. Later,
4089 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4091 sctx->send_progress = sctx->cur_ino;
4093 if (result == BTRFS_COMPARE_TREE_NEW ||
4094 result == BTRFS_COMPARE_TREE_CHANGED) {
4095 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4096 sctx->left_path->slots[0],
4097 struct btrfs_inode_item);
4098 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4101 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4102 sctx->right_path->slots[0],
4103 struct btrfs_inode_item);
4104 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4107 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4108 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4109 sctx->right_path->slots[0],
4110 struct btrfs_inode_item);
4112 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4114 if (left_gen != right_gen)
4115 sctx->cur_inode_new_gen = 1;
4118 if (result == BTRFS_COMPARE_TREE_NEW) {
4119 sctx->cur_inode_gen = left_gen;
4120 sctx->cur_inode_new = 1;
4121 sctx->cur_inode_deleted = 0;
4122 sctx->cur_inode_size = btrfs_inode_size(
4123 sctx->left_path->nodes[0], left_ii);
4124 sctx->cur_inode_mode = btrfs_inode_mode(
4125 sctx->left_path->nodes[0], left_ii);
4126 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4127 ret = send_create_inode_if_needed(sctx);
4128 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4129 sctx->cur_inode_gen = right_gen;
4130 sctx->cur_inode_new = 0;
4131 sctx->cur_inode_deleted = 1;
4132 sctx->cur_inode_size = btrfs_inode_size(
4133 sctx->right_path->nodes[0], right_ii);
4134 sctx->cur_inode_mode = btrfs_inode_mode(
4135 sctx->right_path->nodes[0], right_ii);
4136 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4138 * We need to do some special handling in case the inode was
4139 * reported as changed with a changed generation number. This
4140 * means that the original inode was deleted and new inode
4141 * reused the same inum. So we have to treat the old inode as
4142 * deleted and the new one as new.
4144 if (sctx->cur_inode_new_gen) {
4146 * First, process the inode as if it was deleted.
4148 sctx->cur_inode_gen = right_gen;
4149 sctx->cur_inode_new = 0;
4150 sctx->cur_inode_deleted = 1;
4151 sctx->cur_inode_size = btrfs_inode_size(
4152 sctx->right_path->nodes[0], right_ii);
4153 sctx->cur_inode_mode = btrfs_inode_mode(
4154 sctx->right_path->nodes[0], right_ii);
4155 ret = process_all_refs(sctx,
4156 BTRFS_COMPARE_TREE_DELETED);
4161 * Now process the inode as if it was new.
4163 sctx->cur_inode_gen = left_gen;
4164 sctx->cur_inode_new = 1;
4165 sctx->cur_inode_deleted = 0;
4166 sctx->cur_inode_size = btrfs_inode_size(
4167 sctx->left_path->nodes[0], left_ii);
4168 sctx->cur_inode_mode = btrfs_inode_mode(
4169 sctx->left_path->nodes[0], left_ii);
4170 ret = send_create_inode_if_needed(sctx);
4174 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4178 * Advance send_progress now as we did not get into
4179 * process_recorded_refs_if_needed in the new_gen case.
4181 sctx->send_progress = sctx->cur_ino + 1;
4184 * Now process all extents and xattrs of the inode as if
4185 * they were all new.
4187 ret = process_all_extents(sctx);
4190 ret = process_all_new_xattrs(sctx);
4194 sctx->cur_inode_gen = left_gen;
4195 sctx->cur_inode_new = 0;
4196 sctx->cur_inode_new_gen = 0;
4197 sctx->cur_inode_deleted = 0;
4198 sctx->cur_inode_size = btrfs_inode_size(
4199 sctx->left_path->nodes[0], left_ii);
4200 sctx->cur_inode_mode = btrfs_inode_mode(
4201 sctx->left_path->nodes[0], left_ii);
4210 * We have to process new refs before deleted refs, but compare_trees gives us
4211 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4212 * first and later process them in process_recorded_refs.
4213 * For the cur_inode_new_gen case, we skip recording completely because
4214 * changed_inode did already initiate processing of refs. The reason for this is
4215 * that in this case, compare_tree actually compares the refs of 2 different
4216 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4217 * refs of the right tree as deleted and all refs of the left tree as new.
4219 static int changed_ref(struct send_ctx *sctx,
4220 enum btrfs_compare_tree_result result)
4224 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4226 if (!sctx->cur_inode_new_gen &&
4227 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4228 if (result == BTRFS_COMPARE_TREE_NEW)
4229 ret = record_new_ref(sctx);
4230 else if (result == BTRFS_COMPARE_TREE_DELETED)
4231 ret = record_deleted_ref(sctx);
4232 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4233 ret = record_changed_ref(sctx);
4240 * Process new/deleted/changed xattrs. We skip processing in the
4241 * cur_inode_new_gen case because changed_inode did already initiate processing
4242 * of xattrs. The reason is the same as in changed_ref
4244 static int changed_xattr(struct send_ctx *sctx,
4245 enum btrfs_compare_tree_result result)
4249 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4251 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4252 if (result == BTRFS_COMPARE_TREE_NEW)
4253 ret = process_new_xattr(sctx);
4254 else if (result == BTRFS_COMPARE_TREE_DELETED)
4255 ret = process_deleted_xattr(sctx);
4256 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4257 ret = process_changed_xattr(sctx);
4264 * Process new/deleted/changed extents. We skip processing in the
4265 * cur_inode_new_gen case because changed_inode did already initiate processing
4266 * of extents. The reason is the same as in changed_ref
4268 static int changed_extent(struct send_ctx *sctx,
4269 enum btrfs_compare_tree_result result)
4273 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4275 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4276 if (result != BTRFS_COMPARE_TREE_DELETED)
4277 ret = process_extent(sctx, sctx->left_path,
4285 * Updates compare related fields in sctx and simply forwards to the actual
4286 * changed_xxx functions.
4288 static int changed_cb(struct btrfs_root *left_root,
4289 struct btrfs_root *right_root,
4290 struct btrfs_path *left_path,
4291 struct btrfs_path *right_path,
4292 struct btrfs_key *key,
4293 enum btrfs_compare_tree_result result,
4297 struct send_ctx *sctx = ctx;
4299 sctx->left_path = left_path;
4300 sctx->right_path = right_path;
4301 sctx->cmp_key = key;
4303 ret = finish_inode_if_needed(sctx, 0);
4307 if (key->type == BTRFS_INODE_ITEM_KEY)
4308 ret = changed_inode(sctx, result);
4309 else if (key->type == BTRFS_INODE_REF_KEY)
4310 ret = changed_ref(sctx, result);
4311 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4312 ret = changed_xattr(sctx, result);
4313 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4314 ret = changed_extent(sctx, result);
4320 static int full_send_tree(struct send_ctx *sctx)
4323 struct btrfs_trans_handle *trans = NULL;
4324 struct btrfs_root *send_root = sctx->send_root;
4325 struct btrfs_key key;
4326 struct btrfs_key found_key;
4327 struct btrfs_path *path;
4328 struct extent_buffer *eb;
4333 path = alloc_path_for_send();
4337 spin_lock(&send_root->root_times_lock);
4338 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4339 spin_unlock(&send_root->root_times_lock);
4341 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4342 key.type = BTRFS_INODE_ITEM_KEY;
4347 * We need to make sure the transaction does not get committed
4348 * while we do anything on commit roots. Join a transaction to prevent
4351 trans = btrfs_join_transaction(send_root);
4352 if (IS_ERR(trans)) {
4353 ret = PTR_ERR(trans);
4359 * Make sure the tree has not changed after re-joining. We detect this
4360 * by comparing start_ctransid and ctransid. They should always match.
4362 spin_lock(&send_root->root_times_lock);
4363 ctransid = btrfs_root_ctransid(&send_root->root_item);
4364 spin_unlock(&send_root->root_times_lock);
4366 if (ctransid != start_ctransid) {
4367 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4368 "send was modified in between. This is "
4369 "probably a bug.\n");
4374 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4382 * When someone want to commit while we iterate, end the
4383 * joined transaction and rejoin.
4385 if (btrfs_should_end_transaction(trans, send_root)) {
4386 ret = btrfs_end_transaction(trans, send_root);
4390 btrfs_release_path(path);
4394 eb = path->nodes[0];
4395 slot = path->slots[0];
4396 btrfs_item_key_to_cpu(eb, &found_key, slot);
4398 ret = changed_cb(send_root, NULL, path, NULL,
4399 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4403 key.objectid = found_key.objectid;
4404 key.type = found_key.type;
4405 key.offset = found_key.offset + 1;
4407 ret = btrfs_next_item(send_root, path);
4417 ret = finish_inode_if_needed(sctx, 1);
4420 btrfs_free_path(path);
4423 ret = btrfs_end_transaction(trans, send_root);
4425 btrfs_end_transaction(trans, send_root);
4430 static int send_subvol(struct send_ctx *sctx)
4434 ret = send_header(sctx);
4438 ret = send_subvol_begin(sctx);
4442 if (sctx->parent_root) {
4443 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4447 ret = finish_inode_if_needed(sctx, 1);
4451 ret = full_send_tree(sctx);
4458 ret = close_cur_inode_file(sctx);
4460 close_cur_inode_file(sctx);
4462 free_recorded_refs(sctx);
4466 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4469 struct btrfs_root *send_root;
4470 struct btrfs_root *clone_root;
4471 struct btrfs_fs_info *fs_info;
4472 struct btrfs_ioctl_send_args *arg = NULL;
4473 struct btrfs_key key;
4474 struct file *filp = NULL;
4475 struct send_ctx *sctx = NULL;
4477 u64 *clone_sources_tmp = NULL;
4479 if (!capable(CAP_SYS_ADMIN))
4482 send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root;
4483 fs_info = send_root->fs_info;
4485 arg = memdup_user(arg_, sizeof(*arg));
4492 if (!access_ok(VERIFY_READ, arg->clone_sources,
4493 sizeof(*arg->clone_sources *
4494 arg->clone_sources_count))) {
4499 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4505 INIT_LIST_HEAD(&sctx->new_refs);
4506 INIT_LIST_HEAD(&sctx->deleted_refs);
4507 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4508 INIT_LIST_HEAD(&sctx->name_cache_list);
4510 sctx->send_filp = fget(arg->send_fd);
4511 if (IS_ERR(sctx->send_filp)) {
4512 ret = PTR_ERR(sctx->send_filp);
4516 sctx->mnt = mnt_file->f_path.mnt;
4518 sctx->send_root = send_root;
4519 sctx->clone_roots_cnt = arg->clone_sources_count;
4521 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4522 sctx->send_buf = vmalloc(sctx->send_max_size);
4523 if (!sctx->send_buf) {
4528 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4529 if (!sctx->read_buf) {
4534 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4535 (arg->clone_sources_count + 1));
4536 if (!sctx->clone_roots) {
4541 if (arg->clone_sources_count) {
4542 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4543 sizeof(*arg->clone_sources));
4544 if (!clone_sources_tmp) {
4549 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4550 arg->clone_sources_count *
4551 sizeof(*arg->clone_sources));
4557 for (i = 0; i < arg->clone_sources_count; i++) {
4558 key.objectid = clone_sources_tmp[i];
4559 key.type = BTRFS_ROOT_ITEM_KEY;
4560 key.offset = (u64)-1;
4561 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4566 if (IS_ERR(clone_root)) {
4567 ret = PTR_ERR(clone_root);
4570 sctx->clone_roots[i].root = clone_root;
4572 vfree(clone_sources_tmp);
4573 clone_sources_tmp = NULL;
4576 if (arg->parent_root) {
4577 key.objectid = arg->parent_root;
4578 key.type = BTRFS_ROOT_ITEM_KEY;
4579 key.offset = (u64)-1;
4580 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4581 if (!sctx->parent_root) {
4588 * Clones from send_root are allowed, but only if the clone source
4589 * is behind the current send position. This is checked while searching
4590 * for possible clone sources.
4592 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4594 /* We do a bsearch later */
4595 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4596 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4599 ret = send_subvol(sctx);
4603 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4606 ret = send_cmd(sctx);
4614 vfree(clone_sources_tmp);
4617 if (sctx->send_filp)
4618 fput(sctx->send_filp);
4620 vfree(sctx->clone_roots);
4621 vfree(sctx->send_buf);
4622 vfree(sctx->read_buf);
4624 name_cache_free(sctx);