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>
33 #include "btrfs_inode.h"
34 #include "transaction.h"
36 static int g_verbose = 0;
38 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
41 * A fs_path is a helper to dynamically build path names with unknown size.
42 * It reallocates the internal buffer on demand.
43 * It allows fast adding of path elements on the right side (normal path) and
44 * fast adding to the left side (reversed path). A reversed path can also be
45 * unreversed if needed.
63 #define FS_PATH_INLINE_SIZE \
64 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
67 /* reused for each extent */
69 struct btrfs_root *root;
76 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
77 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
80 struct file *send_filp;
86 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
90 struct btrfs_root *send_root;
91 struct btrfs_root *parent_root;
92 struct clone_root *clone_roots;
95 /* current state of the compare_tree call */
96 struct btrfs_path *left_path;
97 struct btrfs_path *right_path;
98 struct btrfs_key *cmp_key;
101 * infos of the currently processed inode. In case of deleted inodes,
102 * these are the values from the deleted inode.
107 int cur_inode_new_gen;
108 int cur_inode_deleted;
109 int cur_inode_first_ref_orphan;
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;
128 struct list_head use_list;
134 int need_later_update;
139 static void fs_path_reset(struct fs_path *p)
142 p->start = p->buf + p->buf_len - 1;
152 static struct fs_path *fs_path_alloc(struct send_ctx *sctx)
156 p = kmalloc(sizeof(*p), GFP_NOFS);
161 p->buf = p->inline_buf;
162 p->buf_len = FS_PATH_INLINE_SIZE;
167 static struct fs_path *fs_path_alloc_reversed(struct send_ctx *sctx)
171 p = fs_path_alloc(sctx);
179 static void fs_path_free(struct send_ctx *sctx, struct fs_path *p)
183 if (p->buf != p->inline_buf) {
192 static int fs_path_len(struct fs_path *p)
194 return p->end - p->start;
197 static int fs_path_ensure_buf(struct fs_path *p, int len)
205 if (p->buf_len >= len)
208 path_len = p->end - p->start;
209 old_buf_len = p->buf_len;
210 len = PAGE_ALIGN(len);
212 if (p->buf == p->inline_buf) {
213 tmp_buf = kmalloc(len, GFP_NOFS);
215 tmp_buf = vmalloc(len);
220 memcpy(tmp_buf, p->buf, p->buf_len);
224 if (p->virtual_mem) {
225 tmp_buf = vmalloc(len);
228 memcpy(tmp_buf, p->buf, p->buf_len);
231 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
233 tmp_buf = vmalloc(len);
236 memcpy(tmp_buf, p->buf, p->buf_len);
245 tmp_buf = p->buf + old_buf_len - path_len - 1;
246 p->end = p->buf + p->buf_len - 1;
247 p->start = p->end - path_len;
248 memmove(p->start, tmp_buf, path_len + 1);
251 p->end = p->start + path_len;
256 static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
261 new_len = p->end - p->start + name_len;
262 if (p->start != p->end)
264 ret = fs_path_ensure_buf(p, new_len);
269 if (p->start != p->end)
271 p->start -= name_len;
272 p->prepared = p->start;
274 if (p->start != p->end)
276 p->prepared = p->end;
285 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
289 ret = fs_path_prepare_for_add(p, name_len);
292 memcpy(p->prepared, name, name_len);
299 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
303 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
306 memcpy(p->prepared, p2->start, p2->end - p2->start);
313 static int fs_path_add_from_extent_buffer(struct fs_path *p,
314 struct extent_buffer *eb,
315 unsigned long off, int len)
319 ret = fs_path_prepare_for_add(p, len);
323 read_extent_buffer(eb, p->prepared, off, len);
330 static void fs_path_remove(struct fs_path *p)
333 while (p->start != p->end && *p->end != '/')
338 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
342 p->reversed = from->reversed;
345 ret = fs_path_add_path(p, from);
351 static void fs_path_unreverse(struct fs_path *p)
360 len = p->end - p->start;
362 p->end = p->start + len;
363 memmove(p->start, tmp, len + 1);
367 static struct btrfs_path *alloc_path_for_send(void)
369 struct btrfs_path *path;
371 path = btrfs_alloc_path();
374 path->search_commit_root = 1;
375 path->skip_locking = 1;
379 static int write_buf(struct send_ctx *sctx, const void *buf, u32 len)
389 ret = vfs_write(sctx->send_filp, (char *)buf + pos, len - pos,
391 /* TODO handle that correctly */
392 /*if (ret == -ERESTARTSYS) {
411 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
413 struct btrfs_tlv_header *hdr;
414 int total_len = sizeof(*hdr) + len;
415 int left = sctx->send_max_size - sctx->send_size;
417 if (unlikely(left < total_len))
420 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
421 hdr->tlv_type = cpu_to_le16(attr);
422 hdr->tlv_len = cpu_to_le16(len);
423 memcpy(hdr + 1, data, len);
424 sctx->send_size += total_len;
430 static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
432 return tlv_put(sctx, attr, &value, sizeof(value));
435 static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
437 __le16 tmp = cpu_to_le16(value);
438 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
441 static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
443 __le32 tmp = cpu_to_le32(value);
444 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
448 static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
450 __le64 tmp = cpu_to_le64(value);
451 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
454 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
455 const char *str, int len)
459 return tlv_put(sctx, attr, str, len);
462 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
465 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
469 static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
472 struct btrfs_timespec bts;
473 bts.sec = cpu_to_le64(ts->tv_sec);
474 bts.nsec = cpu_to_le32(ts->tv_nsec);
475 return tlv_put(sctx, attr, &bts, sizeof(bts));
479 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
480 struct extent_buffer *eb,
481 struct btrfs_timespec *ts)
483 struct btrfs_timespec bts;
484 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
485 return tlv_put(sctx, attr, &bts, sizeof(bts));
489 #define TLV_PUT(sctx, attrtype, attrlen, data) \
491 ret = tlv_put(sctx, attrtype, attrlen, data); \
493 goto tlv_put_failure; \
496 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
498 ret = tlv_put_u##bits(sctx, attrtype, value); \
500 goto tlv_put_failure; \
503 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
504 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
505 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
506 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
507 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
509 ret = tlv_put_string(sctx, attrtype, str, len); \
511 goto tlv_put_failure; \
513 #define TLV_PUT_PATH(sctx, attrtype, p) \
515 ret = tlv_put_string(sctx, attrtype, p->start, \
516 p->end - p->start); \
518 goto tlv_put_failure; \
520 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
522 ret = tlv_put_uuid(sctx, attrtype, uuid); \
524 goto tlv_put_failure; \
526 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
528 ret = tlv_put_timespec(sctx, attrtype, ts); \
530 goto tlv_put_failure; \
532 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
534 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
536 goto tlv_put_failure; \
539 static int send_header(struct send_ctx *sctx)
541 struct btrfs_stream_header hdr;
543 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
544 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
546 return write_buf(sctx, &hdr, sizeof(hdr));
550 * For each command/item we want to send to userspace, we call this function.
552 static int begin_cmd(struct send_ctx *sctx, int cmd)
554 struct btrfs_cmd_header *hdr;
556 if (!sctx->send_buf) {
561 BUG_ON(sctx->send_size);
563 sctx->send_size += sizeof(*hdr);
564 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
565 hdr->cmd = cpu_to_le16(cmd);
570 static int send_cmd(struct send_ctx *sctx)
573 struct btrfs_cmd_header *hdr;
576 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
577 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
580 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
581 hdr->crc = cpu_to_le32(crc);
583 ret = write_buf(sctx, sctx->send_buf, sctx->send_size);
585 sctx->total_send_size += sctx->send_size;
586 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
593 * Sends a move instruction to user space
595 static int send_rename(struct send_ctx *sctx,
596 struct fs_path *from, struct fs_path *to)
600 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
602 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
606 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
607 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
609 ret = send_cmd(sctx);
617 * Sends a link instruction to user space
619 static int send_link(struct send_ctx *sctx,
620 struct fs_path *path, struct fs_path *lnk)
624 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
626 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
630 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
631 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
633 ret = send_cmd(sctx);
641 * Sends an unlink instruction to user space
643 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
647 verbose_printk("btrfs: send_unlink %s\n", path->start);
649 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
653 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
655 ret = send_cmd(sctx);
663 * Sends a rmdir instruction to user space
665 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
669 verbose_printk("btrfs: send_rmdir %s\n", path->start);
671 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
675 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
677 ret = send_cmd(sctx);
685 * Helper function to retrieve some fields from an inode item.
687 static int get_inode_info(struct btrfs_root *root,
688 u64 ino, u64 *size, u64 *gen,
689 u64 *mode, u64 *uid, u64 *gid)
692 struct btrfs_inode_item *ii;
693 struct btrfs_key key;
694 struct btrfs_path *path;
696 path = alloc_path_for_send();
701 key.type = BTRFS_INODE_ITEM_KEY;
703 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
711 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
712 struct btrfs_inode_item);
714 *size = btrfs_inode_size(path->nodes[0], ii);
716 *gen = btrfs_inode_generation(path->nodes[0], ii);
718 *mode = btrfs_inode_mode(path->nodes[0], ii);
720 *uid = btrfs_inode_uid(path->nodes[0], ii);
722 *gid = btrfs_inode_gid(path->nodes[0], ii);
725 btrfs_free_path(path);
729 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
734 * Helper function to iterate the entries in ONE btrfs_inode_ref.
735 * The iterate callback may return a non zero value to stop iteration. This can
736 * be a negative value for error codes or 1 to simply stop it.
738 * path must point to the INODE_REF when called.
740 static int iterate_inode_ref(struct send_ctx *sctx,
741 struct btrfs_root *root, struct btrfs_path *path,
742 struct btrfs_key *found_key, int resolve,
743 iterate_inode_ref_t iterate, void *ctx)
745 struct extent_buffer *eb;
746 struct btrfs_item *item;
747 struct btrfs_inode_ref *iref;
748 struct btrfs_path *tmp_path;
760 p = fs_path_alloc_reversed(sctx);
764 tmp_path = alloc_path_for_send();
766 fs_path_free(sctx, p);
771 slot = path->slots[0];
772 item = btrfs_item_nr(eb, slot);
773 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
776 total = btrfs_item_size(eb, item);
779 while (cur < total) {
782 name_len = btrfs_inode_ref_name_len(eb, iref);
783 index = btrfs_inode_ref_index(eb, iref);
785 start = btrfs_iref_to_path(root, tmp_path, iref, eb,
786 found_key->offset, p->buf,
789 ret = PTR_ERR(start);
792 if (start < p->buf) {
793 /* overflow , try again with larger buffer */
794 ret = fs_path_ensure_buf(p,
795 p->buf_len + p->buf - start);
798 start = btrfs_iref_to_path(root, tmp_path, iref,
799 eb, found_key->offset, p->buf,
802 ret = PTR_ERR(start);
805 BUG_ON(start < p->buf);
809 ret = fs_path_add_from_extent_buffer(p, eb,
810 (unsigned long)(iref + 1), name_len);
816 len = sizeof(*iref) + name_len;
817 iref = (struct btrfs_inode_ref *)((char *)iref + len);
820 ret = iterate(num, found_key->offset, index, p, ctx);
828 btrfs_free_path(tmp_path);
829 fs_path_free(sctx, p);
833 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
834 const char *name, int name_len,
835 const char *data, int data_len,
839 * Helper function to iterate the entries in ONE btrfs_dir_item.
840 * The iterate callback may return a non zero value to stop iteration. This can
841 * be a negative value for error codes or 1 to simply stop it.
843 * path must point to the dir item when called.
845 static int iterate_dir_item(struct send_ctx *sctx,
846 struct btrfs_root *root, struct btrfs_path *path,
847 struct btrfs_key *found_key,
848 iterate_dir_item_t iterate, void *ctx)
851 struct extent_buffer *eb;
852 struct btrfs_item *item;
853 struct btrfs_dir_item *di;
854 struct btrfs_path *tmp_path = NULL;
855 struct btrfs_key di_key;
870 buf = kmalloc(buf_len, GFP_NOFS);
876 tmp_path = alloc_path_for_send();
883 slot = path->slots[0];
884 item = btrfs_item_nr(eb, slot);
885 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
888 total = btrfs_item_size(eb, item);
891 while (cur < total) {
892 name_len = btrfs_dir_name_len(eb, di);
893 data_len = btrfs_dir_data_len(eb, di);
894 type = btrfs_dir_type(eb, di);
895 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
897 if (name_len + data_len > buf_len) {
898 buf_len = PAGE_ALIGN(name_len + data_len);
900 buf2 = vmalloc(buf_len);
907 buf2 = krealloc(buf, buf_len, GFP_NOFS);
909 buf2 = vmalloc(buf_len);
923 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
924 name_len + data_len);
926 len = sizeof(*di) + name_len + data_len;
927 di = (struct btrfs_dir_item *)((char *)di + len);
930 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
931 data_len, type, ctx);
943 btrfs_free_path(tmp_path);
951 static int __copy_first_ref(int num, u64 dir, int index,
952 struct fs_path *p, void *ctx)
955 struct fs_path *pt = ctx;
957 ret = fs_path_copy(pt, p);
961 /* we want the first only */
966 * Retrieve the first path of an inode. If an inode has more then one
967 * ref/hardlink, this is ignored.
969 static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root,
970 u64 ino, struct fs_path *path)
973 struct btrfs_key key, found_key;
974 struct btrfs_path *p;
976 p = alloc_path_for_send();
983 key.type = BTRFS_INODE_REF_KEY;
986 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
993 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
994 if (found_key.objectid != ino ||
995 found_key.type != BTRFS_INODE_REF_KEY) {
1000 ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
1001 __copy_first_ref, path);
1011 struct backref_ctx {
1012 struct send_ctx *sctx;
1014 /* number of total found references */
1018 * used for clones found in send_root. clones found behind cur_objectid
1019 * and cur_offset are not considered as allowed clones.
1024 /* may be truncated in case it's the last extent in a file */
1027 /* Just to check for bugs in backref resolving */
1028 int found_in_send_root;
1031 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1033 u64 root = (u64)key;
1034 struct clone_root *cr = (struct clone_root *)elt;
1036 if (root < cr->root->objectid)
1038 if (root > cr->root->objectid)
1043 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1045 struct clone_root *cr1 = (struct clone_root *)e1;
1046 struct clone_root *cr2 = (struct clone_root *)e2;
1048 if (cr1->root->objectid < cr2->root->objectid)
1050 if (cr1->root->objectid > cr2->root->objectid)
1056 * Called for every backref that is found for the current extent.
1058 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1060 struct backref_ctx *bctx = ctx_;
1061 struct clone_root *found;
1065 /* First check if the root is in the list of accepted clone sources */
1066 found = bsearch((void *)root, bctx->sctx->clone_roots,
1067 bctx->sctx->clone_roots_cnt,
1068 sizeof(struct clone_root),
1069 __clone_root_cmp_bsearch);
1073 if (found->root == bctx->sctx->send_root &&
1074 ino == bctx->cur_objectid &&
1075 offset == bctx->cur_offset) {
1076 bctx->found_in_send_root = 1;
1080 * There are inodes that have extents that lie behind it's i_size. Don't
1081 * accept clones from these extents.
1083 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL);
1087 if (offset + bctx->extent_len > i_size)
1091 * Make sure we don't consider clones from send_root that are
1092 * behind the current inode/offset.
1094 if (found->root == bctx->sctx->send_root) {
1096 * TODO for the moment we don't accept clones from the inode
1097 * that is currently send. We may change this when
1098 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1101 if (ino >= bctx->cur_objectid)
1103 /*if (ino > ctx->cur_objectid)
1105 if (offset + ctx->extent_len > ctx->cur_offset)
1109 found->found_refs++;
1111 found->offset = offset;
1116 found->found_refs++;
1117 if (ino < found->ino) {
1119 found->offset = offset;
1120 } else if (found->ino == ino) {
1122 * same extent found more then once in the same file.
1124 if (found->offset > offset + bctx->extent_len)
1125 found->offset = offset;
1132 * path must point to the extent item when called.
1134 static int find_extent_clone(struct send_ctx *sctx,
1135 struct btrfs_path *path,
1136 u64 ino, u64 data_offset,
1138 struct clone_root **found)
1144 u64 extent_item_pos;
1145 struct btrfs_file_extent_item *fi;
1146 struct extent_buffer *eb = path->nodes[0];
1147 struct backref_ctx backref_ctx;
1148 struct clone_root *cur_clone_root;
1149 struct btrfs_key found_key;
1150 struct btrfs_path *tmp_path;
1153 tmp_path = alloc_path_for_send();
1157 if (data_offset >= ino_size) {
1159 * There may be extents that lie behind the file's size.
1160 * I at least had this in combination with snapshotting while
1161 * writing large files.
1167 fi = btrfs_item_ptr(eb, path->slots[0],
1168 struct btrfs_file_extent_item);
1169 extent_type = btrfs_file_extent_type(eb, fi);
1170 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1175 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1176 logical = btrfs_file_extent_disk_bytenr(eb, fi);
1181 logical += btrfs_file_extent_offset(eb, fi);
1183 ret = extent_from_logical(sctx->send_root->fs_info,
1184 logical, tmp_path, &found_key);
1185 btrfs_release_path(tmp_path);
1189 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1195 * Setup the clone roots.
1197 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1198 cur_clone_root = sctx->clone_roots + i;
1199 cur_clone_root->ino = (u64)-1;
1200 cur_clone_root->offset = 0;
1201 cur_clone_root->found_refs = 0;
1204 backref_ctx.sctx = sctx;
1205 backref_ctx.found = 0;
1206 backref_ctx.cur_objectid = ino;
1207 backref_ctx.cur_offset = data_offset;
1208 backref_ctx.found_in_send_root = 0;
1209 backref_ctx.extent_len = num_bytes;
1212 * The last extent of a file may be too large due to page alignment.
1213 * We need to adjust extent_len in this case so that the checks in
1214 * __iterate_backrefs work.
1216 if (data_offset + num_bytes >= ino_size)
1217 backref_ctx.extent_len = ino_size - data_offset;
1220 * Now collect all backrefs.
1222 extent_item_pos = logical - found_key.objectid;
1223 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1224 found_key.objectid, extent_item_pos, 1,
1225 __iterate_backrefs, &backref_ctx);
1229 if (!backref_ctx.found_in_send_root) {
1230 /* found a bug in backref code? */
1232 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1233 "send_root. inode=%llu, offset=%llu, "
1235 ino, data_offset, logical);
1239 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1241 "num_bytes=%llu, logical=%llu\n",
1242 data_offset, ino, num_bytes, logical);
1244 if (!backref_ctx.found)
1245 verbose_printk("btrfs: no clones found\n");
1247 cur_clone_root = NULL;
1248 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1249 if (sctx->clone_roots[i].found_refs) {
1250 if (!cur_clone_root)
1251 cur_clone_root = sctx->clone_roots + i;
1252 else if (sctx->clone_roots[i].root == sctx->send_root)
1253 /* prefer clones from send_root over others */
1254 cur_clone_root = sctx->clone_roots + i;
1260 if (cur_clone_root) {
1261 *found = cur_clone_root;
1268 btrfs_free_path(tmp_path);
1272 static int read_symlink(struct send_ctx *sctx,
1273 struct btrfs_root *root,
1275 struct fs_path *dest)
1278 struct btrfs_path *path;
1279 struct btrfs_key key;
1280 struct btrfs_file_extent_item *ei;
1286 path = alloc_path_for_send();
1291 key.type = BTRFS_EXTENT_DATA_KEY;
1293 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1298 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1299 struct btrfs_file_extent_item);
1300 type = btrfs_file_extent_type(path->nodes[0], ei);
1301 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1302 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1303 BUG_ON(compression);
1305 off = btrfs_file_extent_inline_start(ei);
1306 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1308 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1313 btrfs_free_path(path);
1318 * Helper function to generate a file name that is unique in the root of
1319 * send_root and parent_root. This is used to generate names for orphan inodes.
1321 static int gen_unique_name(struct send_ctx *sctx,
1323 struct fs_path *dest)
1326 struct btrfs_path *path;
1327 struct btrfs_dir_item *di;
1332 path = alloc_path_for_send();
1337 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1339 if (len >= sizeof(tmp)) {
1340 /* should really not happen */
1345 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1346 path, BTRFS_FIRST_FREE_OBJECTID,
1347 tmp, strlen(tmp), 0);
1348 btrfs_release_path(path);
1354 /* not unique, try again */
1359 if (!sctx->parent_root) {
1365 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1366 path, BTRFS_FIRST_FREE_OBJECTID,
1367 tmp, strlen(tmp), 0);
1368 btrfs_release_path(path);
1374 /* not unique, try again */
1382 ret = fs_path_add(dest, tmp, strlen(tmp));
1385 btrfs_free_path(path);
1390 inode_state_no_change,
1391 inode_state_will_create,
1392 inode_state_did_create,
1393 inode_state_will_delete,
1394 inode_state_did_delete,
1397 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1405 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1407 if (ret < 0 && ret != -ENOENT)
1411 if (!sctx->parent_root) {
1412 right_ret = -ENOENT;
1414 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1416 if (ret < 0 && ret != -ENOENT)
1421 if (!left_ret && !right_ret) {
1422 if (left_gen == gen && right_gen == gen)
1423 ret = inode_state_no_change;
1424 else if (left_gen == gen) {
1425 if (ino < sctx->send_progress)
1426 ret = inode_state_did_create;
1428 ret = inode_state_will_create;
1429 } else if (right_gen == gen) {
1430 if (ino < sctx->send_progress)
1431 ret = inode_state_did_delete;
1433 ret = inode_state_will_delete;
1437 } else if (!left_ret) {
1438 if (left_gen == gen) {
1439 if (ino < sctx->send_progress)
1440 ret = inode_state_did_create;
1442 ret = inode_state_will_create;
1446 } else if (!right_ret) {
1447 if (right_gen == gen) {
1448 if (ino < sctx->send_progress)
1449 ret = inode_state_did_delete;
1451 ret = inode_state_will_delete;
1463 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1467 ret = get_cur_inode_state(sctx, ino, gen);
1471 if (ret == inode_state_no_change ||
1472 ret == inode_state_did_create ||
1473 ret == inode_state_will_delete)
1483 * Helper function to lookup a dir item in a dir.
1485 static int lookup_dir_item_inode(struct btrfs_root *root,
1486 u64 dir, const char *name, int name_len,
1491 struct btrfs_dir_item *di;
1492 struct btrfs_key key;
1493 struct btrfs_path *path;
1495 path = alloc_path_for_send();
1499 di = btrfs_lookup_dir_item(NULL, root, path,
1500 dir, name, name_len, 0);
1509 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1510 *found_inode = key.objectid;
1511 *found_type = btrfs_dir_type(path->nodes[0], di);
1514 btrfs_free_path(path);
1518 static int get_first_ref(struct send_ctx *sctx,
1519 struct btrfs_root *root, u64 ino,
1520 u64 *dir, u64 *dir_gen, struct fs_path *name)
1523 struct btrfs_key key;
1524 struct btrfs_key found_key;
1525 struct btrfs_path *path;
1526 struct btrfs_inode_ref *iref;
1529 path = alloc_path_for_send();
1534 key.type = BTRFS_INODE_REF_KEY;
1537 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1541 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1543 if (ret || found_key.objectid != key.objectid ||
1544 found_key.type != key.type) {
1549 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1550 struct btrfs_inode_ref);
1551 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1552 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1553 (unsigned long)(iref + 1), len);
1556 btrfs_release_path(path);
1558 ret = get_inode_info(root, found_key.offset, NULL, dir_gen, NULL, NULL,
1563 *dir = found_key.offset;
1566 btrfs_free_path(path);
1570 static int is_first_ref(struct send_ctx *sctx,
1571 struct btrfs_root *root,
1573 const char *name, int name_len)
1576 struct fs_path *tmp_name;
1580 tmp_name = fs_path_alloc(sctx);
1584 ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1588 if (name_len != fs_path_len(tmp_name)) {
1593 ret = memcmp(tmp_name->start, name, name_len);
1600 fs_path_free(sctx, tmp_name);
1604 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1605 const char *name, int name_len,
1606 u64 *who_ino, u64 *who_gen)
1609 u64 other_inode = 0;
1612 if (!sctx->parent_root)
1615 ret = is_inode_existent(sctx, dir, dir_gen);
1619 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1620 &other_inode, &other_type);
1621 if (ret < 0 && ret != -ENOENT)
1628 if (other_inode > sctx->send_progress) {
1629 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1630 who_gen, NULL, NULL, NULL);
1635 *who_ino = other_inode;
1644 static int did_overwrite_ref(struct send_ctx *sctx,
1645 u64 dir, u64 dir_gen,
1646 u64 ino, u64 ino_gen,
1647 const char *name, int name_len)
1654 if (!sctx->parent_root)
1657 ret = is_inode_existent(sctx, dir, dir_gen);
1661 /* check if the ref was overwritten by another ref */
1662 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1663 &ow_inode, &other_type);
1664 if (ret < 0 && ret != -ENOENT)
1667 /* was never and will never be overwritten */
1672 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1677 if (ow_inode == ino && gen == ino_gen) {
1682 /* we know that it is or will be overwritten. check this now */
1683 if (ow_inode < sctx->send_progress)
1692 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1695 struct fs_path *name = NULL;
1699 if (!sctx->parent_root)
1702 name = fs_path_alloc(sctx);
1706 ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name);
1710 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1711 name->start, fs_path_len(name));
1716 fs_path_free(sctx, name);
1720 static int name_cache_insert(struct send_ctx *sctx,
1721 struct name_cache_entry *nce)
1724 struct name_cache_entry **ncea;
1726 ncea = radix_tree_lookup(&sctx->name_cache, nce->ino);
1735 ncea = kmalloc(sizeof(void *) * 2, GFP_NOFS);
1741 ret = radix_tree_insert(&sctx->name_cache, nce->ino, ncea);
1745 list_add_tail(&nce->list, &sctx->name_cache_list);
1746 sctx->name_cache_size++;
1751 static void name_cache_delete(struct send_ctx *sctx,
1752 struct name_cache_entry *nce)
1754 struct name_cache_entry **ncea;
1756 ncea = radix_tree_lookup(&sctx->name_cache, nce->ino);
1761 else if (ncea[1] == nce)
1766 if (!ncea[0] && !ncea[1]) {
1767 radix_tree_delete(&sctx->name_cache, nce->ino);
1771 list_del(&nce->list);
1773 sctx->name_cache_size--;
1776 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1779 struct name_cache_entry **ncea;
1781 ncea = radix_tree_lookup(&sctx->name_cache, ino);
1785 if (ncea[0] && ncea[0]->gen == gen)
1787 else if (ncea[1] && ncea[1]->gen == gen)
1792 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1794 list_del(&nce->list);
1795 list_add_tail(&nce->list, &sctx->name_cache_list);
1798 static void name_cache_clean_unused(struct send_ctx *sctx)
1800 struct name_cache_entry *nce;
1802 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1805 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1806 nce = list_entry(sctx->name_cache_list.next,
1807 struct name_cache_entry, list);
1808 name_cache_delete(sctx, nce);
1813 static void name_cache_free(struct send_ctx *sctx)
1815 struct name_cache_entry *nce;
1816 struct name_cache_entry *tmp;
1818 list_for_each_entry_safe(nce, tmp, &sctx->name_cache_list, list) {
1819 name_cache_delete(sctx, nce);
1823 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1827 struct fs_path *dest)
1831 struct btrfs_path *path = NULL;
1832 struct name_cache_entry *nce = NULL;
1834 nce = name_cache_search(sctx, ino, gen);
1836 if (ino < sctx->send_progress && nce->need_later_update) {
1837 name_cache_delete(sctx, nce);
1841 name_cache_used(sctx, nce);
1842 *parent_ino = nce->parent_ino;
1843 *parent_gen = nce->parent_gen;
1844 ret = fs_path_add(dest, nce->name, nce->name_len);
1852 path = alloc_path_for_send();
1856 ret = is_inode_existent(sctx, ino, gen);
1861 ret = gen_unique_name(sctx, ino, gen, dest);
1868 if (ino < sctx->send_progress)
1869 ret = get_first_ref(sctx, sctx->send_root, ino,
1870 parent_ino, parent_gen, dest);
1872 ret = get_first_ref(sctx, sctx->parent_root, ino,
1873 parent_ino, parent_gen, dest);
1877 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1878 dest->start, dest->end - dest->start);
1882 fs_path_reset(dest);
1883 ret = gen_unique_name(sctx, ino, gen, dest);
1890 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
1898 nce->parent_ino = *parent_ino;
1899 nce->parent_gen = *parent_gen;
1900 nce->name_len = fs_path_len(dest);
1902 strcpy(nce->name, dest->start);
1903 memset(&nce->use_list, 0, sizeof(nce->use_list));
1905 if (ino < sctx->send_progress)
1906 nce->need_later_update = 0;
1908 nce->need_later_update = 1;
1910 nce_ret = name_cache_insert(sctx, nce);
1913 name_cache_clean_unused(sctx);
1916 btrfs_free_path(path);
1921 * Magic happens here. This function returns the first ref to an inode as it
1922 * would look like while receiving the stream at this point in time.
1923 * We walk the path up to the root. For every inode in between, we check if it
1924 * was already processed/sent. If yes, we continue with the parent as found
1925 * in send_root. If not, we continue with the parent as found in parent_root.
1926 * If we encounter an inode that was deleted at this point in time, we use the
1927 * inodes "orphan" name instead of the real name and stop. Same with new inodes
1928 * that were not created yet and overwritten inodes/refs.
1930 * When do we have have orphan inodes:
1931 * 1. When an inode is freshly created and thus no valid refs are available yet
1932 * 2. When a directory lost all it's refs (deleted) but still has dir items
1933 * inside which were not processed yet (pending for move/delete). If anyone
1934 * tried to get the path to the dir items, it would get a path inside that
1936 * 3. When an inode is moved around or gets new links, it may overwrite the ref
1937 * of an unprocessed inode. If in that case the first ref would be
1938 * overwritten, the overwritten inode gets "orphanized". Later when we
1939 * process this overwritten inode, it is restored at a new place by moving
1942 * sctx->send_progress tells this function at which point in time receiving
1945 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
1946 struct fs_path *dest)
1949 struct fs_path *name = NULL;
1950 u64 parent_inode = 0;
1954 name = fs_path_alloc(sctx);
1961 fs_path_reset(dest);
1963 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
1964 fs_path_reset(name);
1966 ret = __get_cur_name_and_parent(sctx, ino, gen,
1967 &parent_inode, &parent_gen, name);
1973 ret = fs_path_add_path(dest, name);
1982 fs_path_free(sctx, name);
1984 fs_path_unreverse(dest);
1989 * Called for regular files when sending extents data. Opens a struct file
1990 * to read from the file.
1992 static int open_cur_inode_file(struct send_ctx *sctx)
1995 struct btrfs_key key;
1996 struct vfsmount *mnt;
1997 struct inode *inode;
1998 struct dentry *dentry;
2002 if (sctx->cur_inode_filp)
2005 key.objectid = sctx->cur_ino;
2006 key.type = BTRFS_INODE_ITEM_KEY;
2009 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2011 if (IS_ERR(inode)) {
2012 ret = PTR_ERR(inode);
2016 dentry = d_obtain_alias(inode);
2018 if (IS_ERR(dentry)) {
2019 ret = PTR_ERR(dentry);
2023 mnt = mntget(sctx->mnt);
2024 filp = dentry_open(dentry, mnt, O_RDONLY | O_LARGEFILE, current_cred());
2028 ret = PTR_ERR(filp);
2031 sctx->cur_inode_filp = filp;
2035 * no xxxput required here as every vfs op
2036 * does it by itself on failure
2042 * Closes the struct file that was created in open_cur_inode_file
2044 static int close_cur_inode_file(struct send_ctx *sctx)
2048 if (!sctx->cur_inode_filp)
2051 ret = filp_close(sctx->cur_inode_filp, NULL);
2052 sctx->cur_inode_filp = NULL;
2059 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2061 static int send_subvol_begin(struct send_ctx *sctx)
2064 struct btrfs_root *send_root = sctx->send_root;
2065 struct btrfs_root *parent_root = sctx->parent_root;
2066 struct btrfs_path *path;
2067 struct btrfs_key key;
2068 struct btrfs_root_ref *ref;
2069 struct extent_buffer *leaf;
2073 path = alloc_path_for_send();
2077 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2079 btrfs_free_path(path);
2083 key.objectid = send_root->objectid;
2084 key.type = BTRFS_ROOT_BACKREF_KEY;
2087 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2096 leaf = path->nodes[0];
2097 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2098 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2099 key.objectid != send_root->objectid) {
2103 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2104 namelen = btrfs_root_ref_name_len(leaf, ref);
2105 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2106 btrfs_release_path(path);
2112 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2116 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2121 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2122 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2123 sctx->send_root->root_item.uuid);
2124 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2125 sctx->send_root->root_item.ctransid);
2127 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2128 sctx->parent_root->root_item.uuid);
2129 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2130 sctx->parent_root->root_item.ctransid);
2133 ret = send_cmd(sctx);
2137 btrfs_free_path(path);
2142 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2147 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2149 p = fs_path_alloc(sctx);
2153 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2157 ret = get_cur_path(sctx, ino, gen, p);
2160 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2161 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2163 ret = send_cmd(sctx);
2167 fs_path_free(sctx, p);
2171 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2176 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2178 p = fs_path_alloc(sctx);
2182 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2186 ret = get_cur_path(sctx, ino, gen, p);
2189 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2190 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2192 ret = send_cmd(sctx);
2196 fs_path_free(sctx, p);
2200 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2205 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2207 p = fs_path_alloc(sctx);
2211 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2215 ret = get_cur_path(sctx, ino, gen, p);
2218 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2219 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2220 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2222 ret = send_cmd(sctx);
2226 fs_path_free(sctx, p);
2230 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2233 struct fs_path *p = NULL;
2234 struct btrfs_inode_item *ii;
2235 struct btrfs_path *path = NULL;
2236 struct extent_buffer *eb;
2237 struct btrfs_key key;
2240 verbose_printk("btrfs: send_utimes %llu\n", ino);
2242 p = fs_path_alloc(sctx);
2246 path = alloc_path_for_send();
2253 key.type = BTRFS_INODE_ITEM_KEY;
2255 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2259 eb = path->nodes[0];
2260 slot = path->slots[0];
2261 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2263 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2267 ret = get_cur_path(sctx, ino, gen, p);
2270 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2271 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2272 btrfs_inode_atime(ii));
2273 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2274 btrfs_inode_mtime(ii));
2275 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2276 btrfs_inode_ctime(ii));
2279 ret = send_cmd(sctx);
2283 fs_path_free(sctx, p);
2284 btrfs_free_path(path);
2289 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2290 * a valid path yet because we did not process the refs yet. So, the inode
2291 * is created as orphan.
2293 static int send_create_inode(struct send_ctx *sctx, struct btrfs_path *path,
2294 struct btrfs_key *key)
2297 struct extent_buffer *eb = path->nodes[0];
2298 struct btrfs_inode_item *ii;
2300 int slot = path->slots[0];
2304 verbose_printk("btrfs: send_create_inode %llu\n", sctx->cur_ino);
2306 p = fs_path_alloc(sctx);
2310 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2311 mode = btrfs_inode_mode(eb, ii);
2314 cmd = BTRFS_SEND_C_MKFILE;
2315 else if (S_ISDIR(mode))
2316 cmd = BTRFS_SEND_C_MKDIR;
2317 else if (S_ISLNK(mode))
2318 cmd = BTRFS_SEND_C_SYMLINK;
2319 else if (S_ISCHR(mode) || S_ISBLK(mode))
2320 cmd = BTRFS_SEND_C_MKNOD;
2321 else if (S_ISFIFO(mode))
2322 cmd = BTRFS_SEND_C_MKFIFO;
2323 else if (S_ISSOCK(mode))
2324 cmd = BTRFS_SEND_C_MKSOCK;
2326 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2327 (int)(mode & S_IFMT));
2332 ret = begin_cmd(sctx, cmd);
2336 ret = gen_unique_name(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
2340 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2341 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, sctx->cur_ino);
2343 if (S_ISLNK(mode)) {
2345 ret = read_symlink(sctx, sctx->send_root, sctx->cur_ino, p);
2348 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2349 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2350 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2351 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, btrfs_inode_rdev(eb, ii));
2354 ret = send_cmd(sctx);
2361 fs_path_free(sctx, p);
2365 struct recorded_ref {
2366 struct list_head list;
2369 struct fs_path *full_path;
2377 * We need to process new refs before deleted refs, but compare_tree gives us
2378 * everything mixed. So we first record all refs and later process them.
2379 * This function is a helper to record one ref.
2381 static int record_ref(struct list_head *head, u64 dir,
2382 u64 dir_gen, struct fs_path *path)
2384 struct recorded_ref *ref;
2387 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2392 ref->dir_gen = dir_gen;
2393 ref->full_path = path;
2395 tmp = strrchr(ref->full_path->start, '/');
2397 ref->name_len = ref->full_path->end - ref->full_path->start;
2398 ref->name = ref->full_path->start;
2399 ref->dir_path_len = 0;
2400 ref->dir_path = ref->full_path->start;
2403 ref->name_len = ref->full_path->end - tmp;
2405 ref->dir_path = ref->full_path->start;
2406 ref->dir_path_len = ref->full_path->end -
2407 ref->full_path->start - 1 - ref->name_len;
2410 list_add_tail(&ref->list, head);
2414 static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
2416 struct recorded_ref *cur;
2417 struct recorded_ref *tmp;
2419 list_for_each_entry_safe(cur, tmp, head, list) {
2420 fs_path_free(sctx, cur->full_path);
2423 INIT_LIST_HEAD(head);
2426 static void free_recorded_refs(struct send_ctx *sctx)
2428 __free_recorded_refs(sctx, &sctx->new_refs);
2429 __free_recorded_refs(sctx, &sctx->deleted_refs);
2433 * Renames/moves a file/dir to it's orphan name. Used when the first
2434 * ref of an unprocessed inode gets overwritten and for all non empty
2437 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2438 struct fs_path *path)
2441 struct fs_path *orphan;
2443 orphan = fs_path_alloc(sctx);
2447 ret = gen_unique_name(sctx, ino, gen, orphan);
2451 ret = send_rename(sctx, path, orphan);
2454 fs_path_free(sctx, orphan);
2459 * Returns 1 if a directory can be removed at this point in time.
2460 * We check this by iterating all dir items and checking if the inode behind
2461 * the dir item was already processed.
2463 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2466 struct btrfs_root *root = sctx->parent_root;
2467 struct btrfs_path *path;
2468 struct btrfs_key key;
2469 struct btrfs_key found_key;
2470 struct btrfs_key loc;
2471 struct btrfs_dir_item *di;
2473 path = alloc_path_for_send();
2478 key.type = BTRFS_DIR_INDEX_KEY;
2482 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2486 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2489 if (ret || found_key.objectid != key.objectid ||
2490 found_key.type != key.type) {
2494 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2495 struct btrfs_dir_item);
2496 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2498 if (loc.objectid > send_progress) {
2503 btrfs_release_path(path);
2504 key.offset = found_key.offset + 1;
2510 btrfs_free_path(path);
2514 struct finish_unordered_dir_ctx {
2515 struct send_ctx *sctx;
2516 struct fs_path *cur_path;
2517 struct fs_path *dir_path;
2523 int __finish_unordered_dir(int num, struct btrfs_key *di_key,
2524 const char *name, int name_len,
2525 const char *data, int data_len,
2529 struct finish_unordered_dir_ctx *fctx = ctx;
2530 struct send_ctx *sctx = fctx->sctx;
2535 if (di_key->objectid >= fctx->dir_ino)
2538 fs_path_reset(fctx->cur_path);
2540 ret = get_inode_info(sctx->send_root, di_key->objectid,
2541 NULL, &di_gen, &di_mode, NULL, NULL);
2545 ret = is_first_ref(sctx, sctx->send_root, di_key->objectid,
2546 fctx->dir_ino, name, name_len);
2551 ret = gen_unique_name(sctx, di_key->objectid, di_gen,
2554 ret = get_cur_path(sctx, di_key->objectid, di_gen,
2560 ret = fs_path_add(fctx->dir_path, name, name_len);
2564 if (!fctx->delete_pass) {
2565 if (S_ISDIR(di_mode)) {
2566 ret = send_rename(sctx, fctx->cur_path,
2569 ret = send_link(sctx, fctx->dir_path,
2572 fctx->need_delete = 1;
2574 } else if (!S_ISDIR(di_mode)) {
2575 ret = send_unlink(sctx, fctx->cur_path);
2580 fs_path_remove(fctx->dir_path);
2587 * Go through all dir items and see if we find refs which could not be created
2588 * in the past because the dir did not exist at that time.
2590 static int finish_outoforder_dir(struct send_ctx *sctx, u64 dir, u64 dir_gen)
2593 struct btrfs_path *path = NULL;
2594 struct btrfs_key key;
2595 struct btrfs_key found_key;
2596 struct extent_buffer *eb;
2597 struct finish_unordered_dir_ctx fctx;
2600 path = alloc_path_for_send();
2606 memset(&fctx, 0, sizeof(fctx));
2608 fctx.cur_path = fs_path_alloc(sctx);
2609 fctx.dir_path = fs_path_alloc(sctx);
2610 if (!fctx.cur_path || !fctx.dir_path) {
2616 ret = get_cur_path(sctx, dir, dir_gen, fctx.dir_path);
2621 * We do two passes. The first links in the new refs and the second
2622 * deletes orphans if required. Deletion of orphans is not required for
2623 * directory inodes, as we always have only one ref and use rename
2624 * instead of link for those.
2629 key.type = BTRFS_DIR_ITEM_KEY;
2632 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2636 eb = path->nodes[0];
2637 slot = path->slots[0];
2638 btrfs_item_key_to_cpu(eb, &found_key, slot);
2640 if (found_key.objectid != key.objectid ||
2641 found_key.type != key.type) {
2642 btrfs_release_path(path);
2646 ret = iterate_dir_item(sctx, sctx->send_root, path,
2647 &found_key, __finish_unordered_dir,
2652 key.offset = found_key.offset + 1;
2653 btrfs_release_path(path);
2656 if (!fctx.delete_pass && fctx.need_delete) {
2657 fctx.delete_pass = 1;
2662 btrfs_free_path(path);
2663 fs_path_free(sctx, fctx.cur_path);
2664 fs_path_free(sctx, fctx.dir_path);
2669 * This does all the move/link/unlink/rmdir magic.
2671 static int process_recorded_refs(struct send_ctx *sctx)
2674 struct recorded_ref *cur;
2675 struct ulist *check_dirs = NULL;
2676 struct ulist_iterator uit;
2677 struct ulist_node *un;
2678 struct fs_path *valid_path = NULL;
2681 int did_overwrite = 0;
2684 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2686 valid_path = fs_path_alloc(sctx);
2692 check_dirs = ulist_alloc(GFP_NOFS);
2699 * First, check if the first ref of the current inode was overwritten
2700 * before. If yes, we know that the current inode was already orphanized
2701 * and thus use the orphan name. If not, we can use get_cur_path to
2702 * get the path of the first ref as it would like while receiving at
2703 * this point in time.
2704 * New inodes are always orphan at the beginning, so force to use the
2705 * orphan name in this case.
2706 * The first ref is stored in valid_path and will be updated if it
2707 * gets moved around.
2709 if (!sctx->cur_inode_new) {
2710 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2711 sctx->cur_inode_gen);
2717 if (sctx->cur_inode_new || did_overwrite) {
2718 ret = gen_unique_name(sctx, sctx->cur_ino,
2719 sctx->cur_inode_gen, valid_path);
2724 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2730 list_for_each_entry(cur, &sctx->new_refs, list) {
2732 * Check if this new ref would overwrite the first ref of
2733 * another unprocessed inode. If yes, orphanize the
2734 * overwritten inode. If we find an overwritten ref that is
2735 * not the first ref, simply unlink it.
2737 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2738 cur->name, cur->name_len,
2739 &ow_inode, &ow_gen);
2743 ret = is_first_ref(sctx, sctx->parent_root,
2744 ow_inode, cur->dir, cur->name,
2749 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2754 ret = send_unlink(sctx, cur->full_path);
2761 * link/move the ref to the new place. If we have an orphan
2762 * inode, move it and update valid_path. If not, link or move
2763 * it depending on the inode mode.
2765 if (is_orphan && !sctx->cur_inode_first_ref_orphan) {
2766 ret = send_rename(sctx, valid_path, cur->full_path);
2770 ret = fs_path_copy(valid_path, cur->full_path);
2774 if (S_ISDIR(sctx->cur_inode_mode)) {
2776 * Dirs can't be linked, so move it. For moved
2777 * dirs, we always have one new and one deleted
2778 * ref. The deleted ref is ignored later.
2780 ret = send_rename(sctx, valid_path,
2784 ret = fs_path_copy(valid_path, cur->full_path);
2788 ret = send_link(sctx, cur->full_path,
2794 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2800 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2802 * Check if we can already rmdir the directory. If not,
2803 * orphanize it. For every dir item inside that gets deleted
2804 * later, we do this check again and rmdir it then if possible.
2805 * See the use of check_dirs for more details.
2807 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2811 ret = send_rmdir(sctx, valid_path);
2814 } else if (!is_orphan) {
2815 ret = orphanize_inode(sctx, sctx->cur_ino,
2816 sctx->cur_inode_gen, valid_path);
2822 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2823 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2828 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2830 * We have a non dir inode. Go through all deleted refs and
2831 * unlink them if they were not already overwritten by other
2834 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2835 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2836 sctx->cur_ino, sctx->cur_inode_gen,
2837 cur->name, cur->name_len);
2842 * In case the inode was moved to a directory
2843 * that was not created yet (see
2844 * __record_new_ref), we can not unlink the ref
2845 * as it will be needed later when the parent
2846 * directory is created, so that we can move in
2847 * the inode to the new dir.
2850 sctx->cur_inode_first_ref_orphan) {
2851 ret = orphanize_inode(sctx,
2853 sctx->cur_inode_gen,
2857 ret = gen_unique_name(sctx,
2859 sctx->cur_inode_gen,
2866 ret = send_unlink(sctx, cur->full_path);
2871 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2878 * If the inode is still orphan, unlink the orphan. This may
2879 * happen when a previous inode did overwrite the first ref
2880 * of this inode and no new refs were added for the current
2882 * We can however not delete the orphan in case the inode relies
2883 * in a directory that was not created yet (see
2886 if (is_orphan && !sctx->cur_inode_first_ref_orphan) {
2887 ret = send_unlink(sctx, valid_path);
2894 * We did collect all parent dirs where cur_inode was once located. We
2895 * now go through all these dirs and check if they are pending for
2896 * deletion and if it's finally possible to perform the rmdir now.
2897 * We also update the inode stats of the parent dirs here.
2899 ULIST_ITER_INIT(&uit);
2900 while ((un = ulist_next(check_dirs, &uit))) {
2901 if (un->val > sctx->cur_ino)
2904 ret = get_cur_inode_state(sctx, un->val, un->aux);
2908 if (ret == inode_state_did_create ||
2909 ret == inode_state_no_change) {
2910 /* TODO delayed utimes */
2911 ret = send_utimes(sctx, un->val, un->aux);
2914 } else if (ret == inode_state_did_delete) {
2915 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
2919 ret = get_cur_path(sctx, un->val, un->aux,
2923 ret = send_rmdir(sctx, valid_path);
2931 * Current inode is now at it's new position, so we must increase
2934 sctx->send_progress = sctx->cur_ino + 1;
2937 * We may have a directory here that has pending refs which could not
2938 * be created before (because the dir did not exist before, see
2939 * __record_new_ref). finish_outoforder_dir will link/move the pending
2942 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_new) {
2943 ret = finish_outoforder_dir(sctx, sctx->cur_ino,
2944 sctx->cur_inode_gen);
2952 free_recorded_refs(sctx);
2953 ulist_free(check_dirs);
2954 fs_path_free(sctx, valid_path);
2958 static int __record_new_ref(int num, u64 dir, int index,
2959 struct fs_path *name,
2963 struct send_ctx *sctx = ctx;
2967 p = fs_path_alloc(sctx);
2971 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
2977 * The parent may be non-existent at this point in time. This happens
2978 * if the ino of the parent dir is higher then the current ino. In this
2979 * case, we can not process this ref until the parent dir is finally
2980 * created. If we reach the parent dir later, process_recorded_refs
2981 * will go through all dir items and process the refs that could not be
2982 * processed before. In case this is the first ref, we set
2983 * cur_inode_first_ref_orphan to 1 to inform process_recorded_refs to
2984 * keep an orphan of the inode so that it later can be used for
2987 ret = is_inode_existent(sctx, dir, gen);
2991 ret = is_first_ref(sctx, sctx->send_root, sctx->cur_ino, dir,
2992 name->start, fs_path_len(name));
2996 sctx->cur_inode_first_ref_orphan = 1;
3001 ret = get_cur_path(sctx, dir, gen, p);
3004 ret = fs_path_add_path(p, name);
3008 ret = record_ref(&sctx->new_refs, dir, gen, p);
3012 fs_path_free(sctx, p);
3016 static int __record_deleted_ref(int num, u64 dir, int index,
3017 struct fs_path *name,
3021 struct send_ctx *sctx = ctx;
3025 p = fs_path_alloc(sctx);
3029 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3034 ret = get_cur_path(sctx, dir, gen, p);
3037 ret = fs_path_add_path(p, name);
3041 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3045 fs_path_free(sctx, p);
3049 static int record_new_ref(struct send_ctx *sctx)
3053 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3054 sctx->cmp_key, 0, __record_new_ref, sctx);
3063 static int record_deleted_ref(struct send_ctx *sctx)
3067 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3068 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3077 struct find_ref_ctx {
3079 struct fs_path *name;
3083 static int __find_iref(int num, u64 dir, int index,
3084 struct fs_path *name,
3087 struct find_ref_ctx *ctx = ctx_;
3089 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3090 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3091 ctx->found_idx = num;
3097 static int find_iref(struct send_ctx *sctx,
3098 struct btrfs_root *root,
3099 struct btrfs_path *path,
3100 struct btrfs_key *key,
3101 u64 dir, struct fs_path *name)
3104 struct find_ref_ctx ctx;
3110 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3114 if (ctx.found_idx == -1)
3117 return ctx.found_idx;
3120 static int __record_changed_new_ref(int num, u64 dir, int index,
3121 struct fs_path *name,
3125 struct send_ctx *sctx = ctx;
3127 ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
3128 sctx->cmp_key, dir, name);
3130 ret = __record_new_ref(num, dir, index, name, sctx);
3137 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3138 struct fs_path *name,
3142 struct send_ctx *sctx = ctx;
3144 ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3147 ret = __record_deleted_ref(num, dir, index, name, sctx);
3154 static int record_changed_ref(struct send_ctx *sctx)
3158 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3159 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3162 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3163 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3173 * Record and process all refs at once. Needed when an inode changes the
3174 * generation number, which means that it was deleted and recreated.
3176 static int process_all_refs(struct send_ctx *sctx,
3177 enum btrfs_compare_tree_result cmd)
3180 struct btrfs_root *root;
3181 struct btrfs_path *path;
3182 struct btrfs_key key;
3183 struct btrfs_key found_key;
3184 struct extent_buffer *eb;
3186 iterate_inode_ref_t cb;
3188 path = alloc_path_for_send();
3192 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3193 root = sctx->send_root;
3194 cb = __record_new_ref;
3195 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3196 root = sctx->parent_root;
3197 cb = __record_deleted_ref;
3202 key.objectid = sctx->cmp_key->objectid;
3203 key.type = BTRFS_INODE_REF_KEY;
3206 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3208 btrfs_release_path(path);
3212 btrfs_release_path(path);
3216 eb = path->nodes[0];
3217 slot = path->slots[0];
3218 btrfs_item_key_to_cpu(eb, &found_key, slot);
3220 if (found_key.objectid != key.objectid ||
3221 found_key.type != key.type) {
3222 btrfs_release_path(path);
3226 ret = iterate_inode_ref(sctx, sctx->parent_root, path,
3227 &found_key, 0, cb, sctx);
3228 btrfs_release_path(path);
3232 key.offset = found_key.offset + 1;
3235 ret = process_recorded_refs(sctx);
3238 btrfs_free_path(path);
3242 static int send_set_xattr(struct send_ctx *sctx,
3243 struct fs_path *path,
3244 const char *name, int name_len,
3245 const char *data, int data_len)
3249 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3253 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3254 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3255 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3257 ret = send_cmd(sctx);
3264 static int send_remove_xattr(struct send_ctx *sctx,
3265 struct fs_path *path,
3266 const char *name, int name_len)
3270 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3274 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3275 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3277 ret = send_cmd(sctx);
3284 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3285 const char *name, int name_len,
3286 const char *data, int data_len,
3290 struct send_ctx *sctx = ctx;
3292 posix_acl_xattr_header dummy_acl;
3294 p = fs_path_alloc(sctx);
3299 * This hack is needed because empty acl's are stored as zero byte
3300 * data in xattrs. Problem with that is, that receiving these zero byte
3301 * acl's will fail later. To fix this, we send a dummy acl list that
3302 * only contains the version number and no entries.
3304 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3305 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3306 if (data_len == 0) {
3307 dummy_acl.a_version =
3308 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3309 data = (char *)&dummy_acl;
3310 data_len = sizeof(dummy_acl);
3314 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3318 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3321 fs_path_free(sctx, p);
3325 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3326 const char *name, int name_len,
3327 const char *data, int data_len,
3331 struct send_ctx *sctx = ctx;
3334 p = fs_path_alloc(sctx);
3338 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3342 ret = send_remove_xattr(sctx, p, name, name_len);
3345 fs_path_free(sctx, p);
3349 static int process_new_xattr(struct send_ctx *sctx)
3353 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3354 sctx->cmp_key, __process_new_xattr, sctx);
3359 static int process_deleted_xattr(struct send_ctx *sctx)
3363 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3364 sctx->cmp_key, __process_deleted_xattr, sctx);
3369 struct find_xattr_ctx {
3377 static int __find_xattr(int num, struct btrfs_key *di_key,
3378 const char *name, int name_len,
3379 const char *data, int data_len,
3380 u8 type, void *vctx)
3382 struct find_xattr_ctx *ctx = vctx;
3384 if (name_len == ctx->name_len &&
3385 strncmp(name, ctx->name, name_len) == 0) {
3386 ctx->found_idx = num;
3387 ctx->found_data_len = data_len;
3388 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3389 if (!ctx->found_data)
3391 memcpy(ctx->found_data, data, data_len);
3397 static int find_xattr(struct send_ctx *sctx,
3398 struct btrfs_root *root,
3399 struct btrfs_path *path,
3400 struct btrfs_key *key,
3401 const char *name, int name_len,
3402 char **data, int *data_len)
3405 struct find_xattr_ctx ctx;
3408 ctx.name_len = name_len;
3410 ctx.found_data = NULL;
3411 ctx.found_data_len = 0;
3413 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3417 if (ctx.found_idx == -1)
3420 *data = ctx.found_data;
3421 *data_len = ctx.found_data_len;
3423 kfree(ctx.found_data);
3425 return ctx.found_idx;
3429 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3430 const char *name, int name_len,
3431 const char *data, int data_len,
3435 struct send_ctx *sctx = ctx;
3436 char *found_data = NULL;
3437 int found_data_len = 0;
3438 struct fs_path *p = NULL;
3440 ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
3441 sctx->cmp_key, name, name_len, &found_data,
3443 if (ret == -ENOENT) {
3444 ret = __process_new_xattr(num, di_key, name, name_len, data,
3445 data_len, type, ctx);
3446 } else if (ret >= 0) {
3447 if (data_len != found_data_len ||
3448 memcmp(data, found_data, data_len)) {
3449 ret = __process_new_xattr(num, di_key, name, name_len,
3450 data, data_len, type, ctx);
3457 fs_path_free(sctx, p);
3461 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3462 const char *name, int name_len,
3463 const char *data, int data_len,
3467 struct send_ctx *sctx = ctx;
3469 ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3470 name, name_len, NULL, NULL);
3472 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3473 data_len, type, ctx);
3480 static int process_changed_xattr(struct send_ctx *sctx)
3484 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3485 sctx->cmp_key, __process_changed_new_xattr, sctx);
3488 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3489 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3495 static int process_all_new_xattrs(struct send_ctx *sctx)
3498 struct btrfs_root *root;
3499 struct btrfs_path *path;
3500 struct btrfs_key key;
3501 struct btrfs_key found_key;
3502 struct extent_buffer *eb;
3505 path = alloc_path_for_send();
3509 root = sctx->send_root;
3511 key.objectid = sctx->cmp_key->objectid;
3512 key.type = BTRFS_XATTR_ITEM_KEY;
3515 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3523 eb = path->nodes[0];
3524 slot = path->slots[0];
3525 btrfs_item_key_to_cpu(eb, &found_key, slot);
3527 if (found_key.objectid != key.objectid ||
3528 found_key.type != key.type) {
3533 ret = iterate_dir_item(sctx, root, path, &found_key,
3534 __process_new_xattr, sctx);
3538 btrfs_release_path(path);
3539 key.offset = found_key.offset + 1;
3543 btrfs_free_path(path);
3548 * Read some bytes from the current inode/file and send a write command to
3551 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3555 loff_t pos = offset;
3557 mm_segment_t old_fs;
3559 p = fs_path_alloc(sctx);
3564 * vfs normally only accepts user space buffers for security reasons.
3565 * we only read from the file and also only provide the read_buf buffer
3566 * to vfs. As this buffer does not come from a user space call, it's
3567 * ok to temporary allow kernel space buffers.
3572 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3574 ret = open_cur_inode_file(sctx);
3578 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3585 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3589 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3593 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3594 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3595 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, readed);
3597 ret = send_cmd(sctx);
3601 fs_path_free(sctx, p);
3609 * Send a clone command to user space.
3611 static int send_clone(struct send_ctx *sctx,
3612 u64 offset, u32 len,
3613 struct clone_root *clone_root)
3616 struct btrfs_root *clone_root2 = clone_root->root;
3620 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3621 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3622 clone_root->root->objectid, clone_root->ino,
3623 clone_root->offset);
3625 p = fs_path_alloc(sctx);
3629 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3633 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3637 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3638 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3639 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3641 if (clone_root2 == sctx->send_root) {
3642 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3643 &gen, NULL, NULL, NULL);
3646 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3648 ret = get_inode_path(sctx, clone_root2, clone_root->ino, p);
3653 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3654 clone_root2->root_item.uuid);
3655 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3656 clone_root2->root_item.ctransid);
3657 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3658 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3659 clone_root->offset);
3661 ret = send_cmd(sctx);
3665 fs_path_free(sctx, p);
3669 static int send_write_or_clone(struct send_ctx *sctx,
3670 struct btrfs_path *path,
3671 struct btrfs_key *key,
3672 struct clone_root *clone_root)
3675 struct btrfs_file_extent_item *ei;
3676 u64 offset = key->offset;
3682 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3683 struct btrfs_file_extent_item);
3684 type = btrfs_file_extent_type(path->nodes[0], ei);
3685 if (type == BTRFS_FILE_EXTENT_INLINE)
3686 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3688 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3690 if (offset + len > sctx->cur_inode_size)
3691 len = sctx->cur_inode_size - offset;
3700 if (l > BTRFS_SEND_READ_SIZE)
3701 l = BTRFS_SEND_READ_SIZE;
3702 ret = send_write(sctx, pos + offset, l);
3711 ret = send_clone(sctx, offset, len, clone_root);
3718 static int is_extent_unchanged(struct send_ctx *sctx,
3719 struct btrfs_path *left_path,
3720 struct btrfs_key *ekey)
3723 struct btrfs_key key;
3724 struct btrfs_path *path = NULL;
3725 struct extent_buffer *eb;
3727 struct btrfs_key found_key;
3728 struct btrfs_file_extent_item *ei;
3733 u64 left_offset_fixed;
3739 path = alloc_path_for_send();
3743 eb = left_path->nodes[0];
3744 slot = left_path->slots[0];
3746 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3747 left_type = btrfs_file_extent_type(eb, ei);
3748 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3749 left_len = btrfs_file_extent_num_bytes(eb, ei);
3750 left_offset = btrfs_file_extent_offset(eb, ei);
3752 if (left_type != BTRFS_FILE_EXTENT_REG) {
3758 * Following comments will refer to these graphics. L is the left
3759 * extents which we are checking at the moment. 1-8 are the right
3760 * extents that we iterate.
3763 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3766 * |--1--|-2b-|...(same as above)
3768 * Alternative situation. Happens on files where extents got split.
3770 * |-----------7-----------|-6-|
3772 * Alternative situation. Happens on files which got larger.
3775 * Nothing follows after 8.
3778 key.objectid = ekey->objectid;
3779 key.type = BTRFS_EXTENT_DATA_KEY;
3780 key.offset = ekey->offset;
3781 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3790 * Handle special case where the right side has no extents at all.
3792 eb = path->nodes[0];
3793 slot = path->slots[0];
3794 btrfs_item_key_to_cpu(eb, &found_key, slot);
3795 if (found_key.objectid != key.objectid ||
3796 found_key.type != key.type) {
3802 * We're now on 2a, 2b or 7.
3805 while (key.offset < ekey->offset + left_len) {
3806 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3807 right_type = btrfs_file_extent_type(eb, ei);
3808 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3809 right_len = btrfs_file_extent_num_bytes(eb, ei);
3810 right_offset = btrfs_file_extent_offset(eb, ei);
3812 if (right_type != BTRFS_FILE_EXTENT_REG) {
3818 * Are we at extent 8? If yes, we know the extent is changed.
3819 * This may only happen on the first iteration.
3821 if (found_key.offset + right_len < ekey->offset) {
3826 left_offset_fixed = left_offset;
3827 if (key.offset < ekey->offset) {
3828 /* Fix the right offset for 2a and 7. */
3829 right_offset += ekey->offset - key.offset;
3831 /* Fix the left offset for all behind 2a and 2b */
3832 left_offset_fixed += key.offset - ekey->offset;
3836 * Check if we have the same extent.
3838 if (left_disknr + left_offset_fixed !=
3839 right_disknr + right_offset) {
3845 * Go to the next extent.
3847 ret = btrfs_next_item(sctx->parent_root, path);
3851 eb = path->nodes[0];
3852 slot = path->slots[0];
3853 btrfs_item_key_to_cpu(eb, &found_key, slot);
3855 if (ret || found_key.objectid != key.objectid ||
3856 found_key.type != key.type) {
3857 key.offset += right_len;
3860 if (found_key.offset != key.offset + right_len) {
3861 /* Should really not happen */
3870 * We're now behind the left extent (treat as unchanged) or at the end
3871 * of the right side (treat as changed).
3873 if (key.offset >= ekey->offset + left_len)
3880 btrfs_free_path(path);
3884 static int process_extent(struct send_ctx *sctx,
3885 struct btrfs_path *path,
3886 struct btrfs_key *key)
3889 struct clone_root *found_clone = NULL;
3891 if (S_ISLNK(sctx->cur_inode_mode))
3894 if (sctx->parent_root && !sctx->cur_inode_new) {
3895 ret = is_extent_unchanged(sctx, path, key);
3904 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3905 sctx->cur_inode_size, &found_clone);
3906 if (ret != -ENOENT && ret < 0)
3909 ret = send_write_or_clone(sctx, path, key, found_clone);
3915 static int process_all_extents(struct send_ctx *sctx)
3918 struct btrfs_root *root;
3919 struct btrfs_path *path;
3920 struct btrfs_key key;
3921 struct btrfs_key found_key;
3922 struct extent_buffer *eb;
3925 root = sctx->send_root;
3926 path = alloc_path_for_send();
3930 key.objectid = sctx->cmp_key->objectid;
3931 key.type = BTRFS_EXTENT_DATA_KEY;
3934 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3942 eb = path->nodes[0];
3943 slot = path->slots[0];
3944 btrfs_item_key_to_cpu(eb, &found_key, slot);
3946 if (found_key.objectid != key.objectid ||
3947 found_key.type != key.type) {
3952 ret = process_extent(sctx, path, &found_key);
3956 btrfs_release_path(path);
3957 key.offset = found_key.offset + 1;
3961 btrfs_free_path(path);
3965 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
3969 if (sctx->cur_ino == 0)
3971 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
3972 sctx->cmp_key->type <= BTRFS_INODE_REF_KEY)
3974 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
3977 ret = process_recorded_refs(sctx);
3983 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
3995 ret = process_recorded_refs_if_needed(sctx, at_end);
3999 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4001 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4004 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4005 &left_mode, &left_uid, &left_gid);
4009 if (!S_ISLNK(sctx->cur_inode_mode)) {
4010 if (!sctx->parent_root || sctx->cur_inode_new) {
4014 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4015 NULL, NULL, &right_mode, &right_uid,
4020 if (left_uid != right_uid || left_gid != right_gid)
4022 if (left_mode != right_mode)
4027 if (S_ISREG(sctx->cur_inode_mode)) {
4028 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4029 sctx->cur_inode_size);
4035 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4036 left_uid, left_gid);
4041 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4048 * Need to send that every time, no matter if it actually changed
4049 * between the two trees as we have done changes to the inode before.
4051 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4059 static int changed_inode(struct send_ctx *sctx,
4060 enum btrfs_compare_tree_result result)
4063 struct btrfs_key *key = sctx->cmp_key;
4064 struct btrfs_inode_item *left_ii = NULL;
4065 struct btrfs_inode_item *right_ii = NULL;
4069 ret = close_cur_inode_file(sctx);
4073 sctx->cur_ino = key->objectid;
4074 sctx->cur_inode_new_gen = 0;
4075 sctx->cur_inode_first_ref_orphan = 0;
4076 sctx->send_progress = sctx->cur_ino;
4078 if (result == BTRFS_COMPARE_TREE_NEW ||
4079 result == BTRFS_COMPARE_TREE_CHANGED) {
4080 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4081 sctx->left_path->slots[0],
4082 struct btrfs_inode_item);
4083 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4086 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4087 sctx->right_path->slots[0],
4088 struct btrfs_inode_item);
4089 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4092 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4093 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4094 sctx->right_path->slots[0],
4095 struct btrfs_inode_item);
4097 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4099 if (left_gen != right_gen)
4100 sctx->cur_inode_new_gen = 1;
4103 if (result == BTRFS_COMPARE_TREE_NEW) {
4104 sctx->cur_inode_gen = left_gen;
4105 sctx->cur_inode_new = 1;
4106 sctx->cur_inode_deleted = 0;
4107 sctx->cur_inode_size = btrfs_inode_size(
4108 sctx->left_path->nodes[0], left_ii);
4109 sctx->cur_inode_mode = btrfs_inode_mode(
4110 sctx->left_path->nodes[0], left_ii);
4111 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4112 ret = send_create_inode(sctx, sctx->left_path,
4114 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4115 sctx->cur_inode_gen = right_gen;
4116 sctx->cur_inode_new = 0;
4117 sctx->cur_inode_deleted = 1;
4118 sctx->cur_inode_size = btrfs_inode_size(
4119 sctx->right_path->nodes[0], right_ii);
4120 sctx->cur_inode_mode = btrfs_inode_mode(
4121 sctx->right_path->nodes[0], right_ii);
4122 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4123 if (sctx->cur_inode_new_gen) {
4124 sctx->cur_inode_gen = right_gen;
4125 sctx->cur_inode_new = 0;
4126 sctx->cur_inode_deleted = 1;
4127 sctx->cur_inode_size = btrfs_inode_size(
4128 sctx->right_path->nodes[0], right_ii);
4129 sctx->cur_inode_mode = btrfs_inode_mode(
4130 sctx->right_path->nodes[0], right_ii);
4131 ret = process_all_refs(sctx,
4132 BTRFS_COMPARE_TREE_DELETED);
4136 sctx->cur_inode_gen = left_gen;
4137 sctx->cur_inode_new = 1;
4138 sctx->cur_inode_deleted = 0;
4139 sctx->cur_inode_size = btrfs_inode_size(
4140 sctx->left_path->nodes[0], left_ii);
4141 sctx->cur_inode_mode = btrfs_inode_mode(
4142 sctx->left_path->nodes[0], left_ii);
4143 ret = send_create_inode(sctx, sctx->left_path,
4148 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4151 ret = process_all_extents(sctx);
4154 ret = process_all_new_xattrs(sctx);
4158 sctx->cur_inode_gen = left_gen;
4159 sctx->cur_inode_new = 0;
4160 sctx->cur_inode_new_gen = 0;
4161 sctx->cur_inode_deleted = 0;
4162 sctx->cur_inode_size = btrfs_inode_size(
4163 sctx->left_path->nodes[0], left_ii);
4164 sctx->cur_inode_mode = btrfs_inode_mode(
4165 sctx->left_path->nodes[0], left_ii);
4173 static int changed_ref(struct send_ctx *sctx,
4174 enum btrfs_compare_tree_result result)
4178 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4180 if (!sctx->cur_inode_new_gen &&
4181 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4182 if (result == BTRFS_COMPARE_TREE_NEW)
4183 ret = record_new_ref(sctx);
4184 else if (result == BTRFS_COMPARE_TREE_DELETED)
4185 ret = record_deleted_ref(sctx);
4186 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4187 ret = record_changed_ref(sctx);
4193 static int changed_xattr(struct send_ctx *sctx,
4194 enum btrfs_compare_tree_result result)
4198 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4200 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4201 if (result == BTRFS_COMPARE_TREE_NEW)
4202 ret = process_new_xattr(sctx);
4203 else if (result == BTRFS_COMPARE_TREE_DELETED)
4204 ret = process_deleted_xattr(sctx);
4205 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4206 ret = process_changed_xattr(sctx);
4212 static int changed_extent(struct send_ctx *sctx,
4213 enum btrfs_compare_tree_result result)
4217 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4219 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4220 if (result != BTRFS_COMPARE_TREE_DELETED)
4221 ret = process_extent(sctx, sctx->left_path,
4229 static int changed_cb(struct btrfs_root *left_root,
4230 struct btrfs_root *right_root,
4231 struct btrfs_path *left_path,
4232 struct btrfs_path *right_path,
4233 struct btrfs_key *key,
4234 enum btrfs_compare_tree_result result,
4238 struct send_ctx *sctx = ctx;
4240 sctx->left_path = left_path;
4241 sctx->right_path = right_path;
4242 sctx->cmp_key = key;
4244 ret = finish_inode_if_needed(sctx, 0);
4248 if (key->type == BTRFS_INODE_ITEM_KEY)
4249 ret = changed_inode(sctx, result);
4250 else if (key->type == BTRFS_INODE_REF_KEY)
4251 ret = changed_ref(sctx, result);
4252 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4253 ret = changed_xattr(sctx, result);
4254 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4255 ret = changed_extent(sctx, result);
4261 static int full_send_tree(struct send_ctx *sctx)
4264 struct btrfs_trans_handle *trans = NULL;
4265 struct btrfs_root *send_root = sctx->send_root;
4266 struct btrfs_key key;
4267 struct btrfs_key found_key;
4268 struct btrfs_path *path;
4269 struct extent_buffer *eb;
4274 path = alloc_path_for_send();
4278 spin_lock(&send_root->root_times_lock);
4279 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4280 spin_unlock(&send_root->root_times_lock);
4282 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4283 key.type = BTRFS_INODE_ITEM_KEY;
4288 * We need to make sure the transaction does not get committed
4289 * while we do anything on commit roots. Join a transaction to prevent
4292 trans = btrfs_join_transaction(send_root);
4293 if (IS_ERR(trans)) {
4294 ret = PTR_ERR(trans);
4300 * Make sure the tree has not changed
4302 spin_lock(&send_root->root_times_lock);
4303 ctransid = btrfs_root_ctransid(&send_root->root_item);
4304 spin_unlock(&send_root->root_times_lock);
4306 if (ctransid != start_ctransid) {
4307 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4308 "send was modified in between. This is "
4309 "probably a bug.\n");
4314 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4322 * When someone want to commit while we iterate, end the
4323 * joined transaction and rejoin.
4325 if (btrfs_should_end_transaction(trans, send_root)) {
4326 ret = btrfs_end_transaction(trans, send_root);
4330 btrfs_release_path(path);
4334 eb = path->nodes[0];
4335 slot = path->slots[0];
4336 btrfs_item_key_to_cpu(eb, &found_key, slot);
4338 ret = changed_cb(send_root, NULL, path, NULL,
4339 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4343 key.objectid = found_key.objectid;
4344 key.type = found_key.type;
4345 key.offset = found_key.offset + 1;
4347 ret = btrfs_next_item(send_root, path);
4357 ret = finish_inode_if_needed(sctx, 1);
4360 btrfs_free_path(path);
4363 ret = btrfs_end_transaction(trans, send_root);
4365 btrfs_end_transaction(trans, send_root);
4370 static int send_subvol(struct send_ctx *sctx)
4374 ret = send_header(sctx);
4378 ret = send_subvol_begin(sctx);
4382 if (sctx->parent_root) {
4383 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4387 ret = finish_inode_if_needed(sctx, 1);
4391 ret = full_send_tree(sctx);
4398 ret = close_cur_inode_file(sctx);
4400 close_cur_inode_file(sctx);
4402 free_recorded_refs(sctx);
4406 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4409 struct btrfs_root *send_root;
4410 struct btrfs_root *clone_root;
4411 struct btrfs_fs_info *fs_info;
4412 struct btrfs_ioctl_send_args *arg = NULL;
4413 struct btrfs_key key;
4414 struct file *filp = NULL;
4415 struct send_ctx *sctx = NULL;
4417 u64 *clone_sources_tmp = NULL;
4419 if (!capable(CAP_SYS_ADMIN))
4422 send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root;
4423 fs_info = send_root->fs_info;
4425 arg = memdup_user(arg_, sizeof(*arg));
4432 if (!access_ok(VERIFY_READ, arg->clone_sources,
4433 sizeof(*arg->clone_sources *
4434 arg->clone_sources_count))) {
4439 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4445 INIT_LIST_HEAD(&sctx->new_refs);
4446 INIT_LIST_HEAD(&sctx->deleted_refs);
4447 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4448 INIT_LIST_HEAD(&sctx->name_cache_list);
4450 sctx->send_filp = fget(arg->send_fd);
4451 if (IS_ERR(sctx->send_filp)) {
4452 ret = PTR_ERR(sctx->send_filp);
4456 sctx->mnt = mnt_file->f_path.mnt;
4458 sctx->send_root = send_root;
4459 sctx->clone_roots_cnt = arg->clone_sources_count;
4461 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4462 sctx->send_buf = vmalloc(sctx->send_max_size);
4463 if (!sctx->send_buf) {
4468 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4469 if (!sctx->read_buf) {
4474 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4475 (arg->clone_sources_count + 1));
4476 if (!sctx->clone_roots) {
4481 if (arg->clone_sources_count) {
4482 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4483 sizeof(*arg->clone_sources));
4484 if (!clone_sources_tmp) {
4489 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4490 arg->clone_sources_count *
4491 sizeof(*arg->clone_sources));
4497 for (i = 0; i < arg->clone_sources_count; i++) {
4498 key.objectid = clone_sources_tmp[i];
4499 key.type = BTRFS_ROOT_ITEM_KEY;
4500 key.offset = (u64)-1;
4501 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4506 if (IS_ERR(clone_root)) {
4507 ret = PTR_ERR(clone_root);
4510 sctx->clone_roots[i].root = clone_root;
4512 vfree(clone_sources_tmp);
4513 clone_sources_tmp = NULL;
4516 if (arg->parent_root) {
4517 key.objectid = arg->parent_root;
4518 key.type = BTRFS_ROOT_ITEM_KEY;
4519 key.offset = (u64)-1;
4520 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4521 if (!sctx->parent_root) {
4528 * Clones from send_root are allowed, but only if the clone source
4529 * is behind the current send position. This is checked while searching
4530 * for possible clone sources.
4532 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4534 /* We do a bsearch later */
4535 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4536 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4539 ret = send_subvol(sctx);
4543 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4546 ret = send_cmd(sctx);
4554 vfree(clone_sources_tmp);
4557 if (sctx->send_filp)
4558 fput(sctx->send_filp);
4560 vfree(sctx->clone_roots);
4561 vfree(sctx->send_buf);
4562 vfree(sctx->read_buf);
4564 name_cache_free(sctx);