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;
110 int cur_inode_first_ref_orphan;
116 struct list_head new_refs;
117 struct list_head deleted_refs;
119 struct radix_tree_root name_cache;
120 struct list_head name_cache_list;
123 struct file *cur_inode_filp;
127 struct name_cache_entry {
128 struct list_head list;
129 struct list_head use_list;
135 int need_later_update;
140 static void fs_path_reset(struct fs_path *p)
143 p->start = p->buf + p->buf_len - 1;
153 static struct fs_path *fs_path_alloc(struct send_ctx *sctx)
157 p = kmalloc(sizeof(*p), GFP_NOFS);
162 p->buf = p->inline_buf;
163 p->buf_len = FS_PATH_INLINE_SIZE;
168 static struct fs_path *fs_path_alloc_reversed(struct send_ctx *sctx)
172 p = fs_path_alloc(sctx);
180 static void fs_path_free(struct send_ctx *sctx, struct fs_path *p)
184 if (p->buf != p->inline_buf) {
193 static int fs_path_len(struct fs_path *p)
195 return p->end - p->start;
198 static int fs_path_ensure_buf(struct fs_path *p, int len)
206 if (p->buf_len >= len)
209 path_len = p->end - p->start;
210 old_buf_len = p->buf_len;
211 len = PAGE_ALIGN(len);
213 if (p->buf == p->inline_buf) {
214 tmp_buf = kmalloc(len, GFP_NOFS);
216 tmp_buf = vmalloc(len);
221 memcpy(tmp_buf, p->buf, p->buf_len);
225 if (p->virtual_mem) {
226 tmp_buf = vmalloc(len);
229 memcpy(tmp_buf, p->buf, p->buf_len);
232 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
234 tmp_buf = vmalloc(len);
237 memcpy(tmp_buf, p->buf, p->buf_len);
246 tmp_buf = p->buf + old_buf_len - path_len - 1;
247 p->end = p->buf + p->buf_len - 1;
248 p->start = p->end - path_len;
249 memmove(p->start, tmp_buf, path_len + 1);
252 p->end = p->start + path_len;
257 static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
262 new_len = p->end - p->start + name_len;
263 if (p->start != p->end)
265 ret = fs_path_ensure_buf(p, new_len);
270 if (p->start != p->end)
272 p->start -= name_len;
273 p->prepared = p->start;
275 if (p->start != p->end)
277 p->prepared = p->end;
286 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
290 ret = fs_path_prepare_for_add(p, name_len);
293 memcpy(p->prepared, name, name_len);
300 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
304 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
307 memcpy(p->prepared, p2->start, p2->end - p2->start);
314 static int fs_path_add_from_extent_buffer(struct fs_path *p,
315 struct extent_buffer *eb,
316 unsigned long off, int len)
320 ret = fs_path_prepare_for_add(p, len);
324 read_extent_buffer(eb, p->prepared, off, len);
331 static void fs_path_remove(struct fs_path *p)
334 while (p->start != p->end && *p->end != '/')
339 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
343 p->reversed = from->reversed;
346 ret = fs_path_add_path(p, from);
352 static void fs_path_unreverse(struct fs_path *p)
361 len = p->end - p->start;
363 p->end = p->start + len;
364 memmove(p->start, tmp, len + 1);
368 static struct btrfs_path *alloc_path_for_send(void)
370 struct btrfs_path *path;
372 path = btrfs_alloc_path();
375 path->search_commit_root = 1;
376 path->skip_locking = 1;
380 static int write_buf(struct send_ctx *sctx, const void *buf, u32 len)
390 ret = vfs_write(sctx->send_filp, (char *)buf + pos, len - pos,
392 /* TODO handle that correctly */
393 /*if (ret == -ERESTARTSYS) {
412 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
414 struct btrfs_tlv_header *hdr;
415 int total_len = sizeof(*hdr) + len;
416 int left = sctx->send_max_size - sctx->send_size;
418 if (unlikely(left < total_len))
421 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
422 hdr->tlv_type = cpu_to_le16(attr);
423 hdr->tlv_len = cpu_to_le16(len);
424 memcpy(hdr + 1, data, len);
425 sctx->send_size += total_len;
431 static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
433 return tlv_put(sctx, attr, &value, sizeof(value));
436 static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
438 __le16 tmp = cpu_to_le16(value);
439 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
442 static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
444 __le32 tmp = cpu_to_le32(value);
445 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
449 static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
451 __le64 tmp = cpu_to_le64(value);
452 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
455 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
456 const char *str, int len)
460 return tlv_put(sctx, attr, str, len);
463 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
466 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
470 static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
473 struct btrfs_timespec bts;
474 bts.sec = cpu_to_le64(ts->tv_sec);
475 bts.nsec = cpu_to_le32(ts->tv_nsec);
476 return tlv_put(sctx, attr, &bts, sizeof(bts));
480 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
481 struct extent_buffer *eb,
482 struct btrfs_timespec *ts)
484 struct btrfs_timespec bts;
485 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
486 return tlv_put(sctx, attr, &bts, sizeof(bts));
490 #define TLV_PUT(sctx, attrtype, attrlen, data) \
492 ret = tlv_put(sctx, attrtype, attrlen, data); \
494 goto tlv_put_failure; \
497 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
499 ret = tlv_put_u##bits(sctx, attrtype, value); \
501 goto tlv_put_failure; \
504 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
505 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
506 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
507 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
508 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
510 ret = tlv_put_string(sctx, attrtype, str, len); \
512 goto tlv_put_failure; \
514 #define TLV_PUT_PATH(sctx, attrtype, p) \
516 ret = tlv_put_string(sctx, attrtype, p->start, \
517 p->end - p->start); \
519 goto tlv_put_failure; \
521 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
523 ret = tlv_put_uuid(sctx, attrtype, uuid); \
525 goto tlv_put_failure; \
527 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
529 ret = tlv_put_timespec(sctx, attrtype, ts); \
531 goto tlv_put_failure; \
533 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
535 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
537 goto tlv_put_failure; \
540 static int send_header(struct send_ctx *sctx)
542 struct btrfs_stream_header hdr;
544 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
545 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
547 return write_buf(sctx, &hdr, sizeof(hdr));
551 * For each command/item we want to send to userspace, we call this function.
553 static int begin_cmd(struct send_ctx *sctx, int cmd)
555 struct btrfs_cmd_header *hdr;
557 if (!sctx->send_buf) {
562 BUG_ON(sctx->send_size);
564 sctx->send_size += sizeof(*hdr);
565 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
566 hdr->cmd = cpu_to_le16(cmd);
571 static int send_cmd(struct send_ctx *sctx)
574 struct btrfs_cmd_header *hdr;
577 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
578 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
581 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
582 hdr->crc = cpu_to_le32(crc);
584 ret = write_buf(sctx, sctx->send_buf, sctx->send_size);
586 sctx->total_send_size += sctx->send_size;
587 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
594 * Sends a move instruction to user space
596 static int send_rename(struct send_ctx *sctx,
597 struct fs_path *from, struct fs_path *to)
601 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
603 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
607 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
608 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
610 ret = send_cmd(sctx);
618 * Sends a link instruction to user space
620 static int send_link(struct send_ctx *sctx,
621 struct fs_path *path, struct fs_path *lnk)
625 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
627 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
631 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
632 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
634 ret = send_cmd(sctx);
642 * Sends an unlink instruction to user space
644 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
648 verbose_printk("btrfs: send_unlink %s\n", path->start);
650 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
654 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
656 ret = send_cmd(sctx);
664 * Sends a rmdir instruction to user space
666 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
670 verbose_printk("btrfs: send_rmdir %s\n", path->start);
672 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
676 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
678 ret = send_cmd(sctx);
686 * Helper function to retrieve some fields from an inode item.
688 static int get_inode_info(struct btrfs_root *root,
689 u64 ino, u64 *size, u64 *gen,
690 u64 *mode, u64 *uid, u64 *gid)
693 struct btrfs_inode_item *ii;
694 struct btrfs_key key;
695 struct btrfs_path *path;
697 path = alloc_path_for_send();
702 key.type = BTRFS_INODE_ITEM_KEY;
704 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
712 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
713 struct btrfs_inode_item);
715 *size = btrfs_inode_size(path->nodes[0], ii);
717 *gen = btrfs_inode_generation(path->nodes[0], ii);
719 *mode = btrfs_inode_mode(path->nodes[0], ii);
721 *uid = btrfs_inode_uid(path->nodes[0], ii);
723 *gid = btrfs_inode_gid(path->nodes[0], ii);
726 btrfs_free_path(path);
730 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
735 * Helper function to iterate the entries in ONE btrfs_inode_ref.
736 * The iterate callback may return a non zero value to stop iteration. This can
737 * be a negative value for error codes or 1 to simply stop it.
739 * path must point to the INODE_REF when called.
741 static int iterate_inode_ref(struct send_ctx *sctx,
742 struct btrfs_root *root, struct btrfs_path *path,
743 struct btrfs_key *found_key, int resolve,
744 iterate_inode_ref_t iterate, void *ctx)
746 struct extent_buffer *eb;
747 struct btrfs_item *item;
748 struct btrfs_inode_ref *iref;
749 struct btrfs_path *tmp_path;
761 p = fs_path_alloc_reversed(sctx);
765 tmp_path = alloc_path_for_send();
767 fs_path_free(sctx, p);
772 slot = path->slots[0];
773 item = btrfs_item_nr(eb, slot);
774 iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
777 total = btrfs_item_size(eb, item);
780 while (cur < total) {
783 name_len = btrfs_inode_ref_name_len(eb, iref);
784 index = btrfs_inode_ref_index(eb, iref);
786 start = btrfs_iref_to_path(root, tmp_path, iref, eb,
787 found_key->offset, p->buf,
790 ret = PTR_ERR(start);
793 if (start < p->buf) {
794 /* overflow , try again with larger buffer */
795 ret = fs_path_ensure_buf(p,
796 p->buf_len + p->buf - start);
799 start = btrfs_iref_to_path(root, tmp_path, iref,
800 eb, found_key->offset, p->buf,
803 ret = PTR_ERR(start);
806 BUG_ON(start < p->buf);
810 ret = fs_path_add_from_extent_buffer(p, eb,
811 (unsigned long)(iref + 1), name_len);
817 len = sizeof(*iref) + name_len;
818 iref = (struct btrfs_inode_ref *)((char *)iref + len);
821 ret = iterate(num, found_key->offset, index, p, ctx);
829 btrfs_free_path(tmp_path);
830 fs_path_free(sctx, p);
834 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
835 const char *name, int name_len,
836 const char *data, int data_len,
840 * Helper function to iterate the entries in ONE btrfs_dir_item.
841 * The iterate callback may return a non zero value to stop iteration. This can
842 * be a negative value for error codes or 1 to simply stop it.
844 * path must point to the dir item when called.
846 static int iterate_dir_item(struct send_ctx *sctx,
847 struct btrfs_root *root, struct btrfs_path *path,
848 struct btrfs_key *found_key,
849 iterate_dir_item_t iterate, void *ctx)
852 struct extent_buffer *eb;
853 struct btrfs_item *item;
854 struct btrfs_dir_item *di;
855 struct btrfs_path *tmp_path = NULL;
856 struct btrfs_key di_key;
871 buf = kmalloc(buf_len, GFP_NOFS);
877 tmp_path = alloc_path_for_send();
884 slot = path->slots[0];
885 item = btrfs_item_nr(eb, slot);
886 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
889 total = btrfs_item_size(eb, item);
892 while (cur < total) {
893 name_len = btrfs_dir_name_len(eb, di);
894 data_len = btrfs_dir_data_len(eb, di);
895 type = btrfs_dir_type(eb, di);
896 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
898 if (name_len + data_len > buf_len) {
899 buf_len = PAGE_ALIGN(name_len + data_len);
901 buf2 = vmalloc(buf_len);
908 buf2 = krealloc(buf, buf_len, GFP_NOFS);
910 buf2 = vmalloc(buf_len);
924 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
925 name_len + data_len);
927 len = sizeof(*di) + name_len + data_len;
928 di = (struct btrfs_dir_item *)((char *)di + len);
931 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
932 data_len, type, ctx);
944 btrfs_free_path(tmp_path);
952 static int __copy_first_ref(int num, u64 dir, int index,
953 struct fs_path *p, void *ctx)
956 struct fs_path *pt = ctx;
958 ret = fs_path_copy(pt, p);
962 /* we want the first only */
967 * Retrieve the first path of an inode. If an inode has more then one
968 * ref/hardlink, this is ignored.
970 static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root,
971 u64 ino, struct fs_path *path)
974 struct btrfs_key key, found_key;
975 struct btrfs_path *p;
977 p = alloc_path_for_send();
984 key.type = BTRFS_INODE_REF_KEY;
987 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
994 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
995 if (found_key.objectid != ino ||
996 found_key.type != BTRFS_INODE_REF_KEY) {
1001 ret = iterate_inode_ref(sctx, root, p, &found_key, 1,
1002 __copy_first_ref, path);
1012 struct backref_ctx {
1013 struct send_ctx *sctx;
1015 /* number of total found references */
1019 * used for clones found in send_root. clones found behind cur_objectid
1020 * and cur_offset are not considered as allowed clones.
1025 /* may be truncated in case it's the last extent in a file */
1028 /* Just to check for bugs in backref resolving */
1029 int found_in_send_root;
1032 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1034 u64 root = (u64)key;
1035 struct clone_root *cr = (struct clone_root *)elt;
1037 if (root < cr->root->objectid)
1039 if (root > cr->root->objectid)
1044 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1046 struct clone_root *cr1 = (struct clone_root *)e1;
1047 struct clone_root *cr2 = (struct clone_root *)e2;
1049 if (cr1->root->objectid < cr2->root->objectid)
1051 if (cr1->root->objectid > cr2->root->objectid)
1057 * Called for every backref that is found for the current extent.
1059 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1061 struct backref_ctx *bctx = ctx_;
1062 struct clone_root *found;
1066 /* First check if the root is in the list of accepted clone sources */
1067 found = bsearch((void *)root, bctx->sctx->clone_roots,
1068 bctx->sctx->clone_roots_cnt,
1069 sizeof(struct clone_root),
1070 __clone_root_cmp_bsearch);
1074 if (found->root == bctx->sctx->send_root &&
1075 ino == bctx->cur_objectid &&
1076 offset == bctx->cur_offset) {
1077 bctx->found_in_send_root = 1;
1081 * There are inodes that have extents that lie behind it's i_size. Don't
1082 * accept clones from these extents.
1084 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL);
1088 if (offset + bctx->extent_len > i_size)
1092 * Make sure we don't consider clones from send_root that are
1093 * behind the current inode/offset.
1095 if (found->root == bctx->sctx->send_root) {
1097 * TODO for the moment we don't accept clones from the inode
1098 * that is currently send. We may change this when
1099 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1102 if (ino >= bctx->cur_objectid)
1104 /*if (ino > ctx->cur_objectid)
1106 if (offset + ctx->extent_len > ctx->cur_offset)
1110 found->found_refs++;
1112 found->offset = offset;
1117 found->found_refs++;
1118 if (ino < found->ino) {
1120 found->offset = offset;
1121 } else if (found->ino == ino) {
1123 * same extent found more then once in the same file.
1125 if (found->offset > offset + bctx->extent_len)
1126 found->offset = offset;
1133 * path must point to the extent item when called.
1135 static int find_extent_clone(struct send_ctx *sctx,
1136 struct btrfs_path *path,
1137 u64 ino, u64 data_offset,
1139 struct clone_root **found)
1145 u64 extent_item_pos;
1146 struct btrfs_file_extent_item *fi;
1147 struct extent_buffer *eb = path->nodes[0];
1148 struct backref_ctx backref_ctx;
1149 struct clone_root *cur_clone_root;
1150 struct btrfs_key found_key;
1151 struct btrfs_path *tmp_path;
1154 tmp_path = alloc_path_for_send();
1158 if (data_offset >= ino_size) {
1160 * There may be extents that lie behind the file's size.
1161 * I at least had this in combination with snapshotting while
1162 * writing large files.
1168 fi = btrfs_item_ptr(eb, path->slots[0],
1169 struct btrfs_file_extent_item);
1170 extent_type = btrfs_file_extent_type(eb, fi);
1171 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1176 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1177 logical = btrfs_file_extent_disk_bytenr(eb, fi);
1182 logical += btrfs_file_extent_offset(eb, fi);
1184 ret = extent_from_logical(sctx->send_root->fs_info,
1185 logical, tmp_path, &found_key);
1186 btrfs_release_path(tmp_path);
1190 if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1196 * Setup the clone roots.
1198 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1199 cur_clone_root = sctx->clone_roots + i;
1200 cur_clone_root->ino = (u64)-1;
1201 cur_clone_root->offset = 0;
1202 cur_clone_root->found_refs = 0;
1205 backref_ctx.sctx = sctx;
1206 backref_ctx.found = 0;
1207 backref_ctx.cur_objectid = ino;
1208 backref_ctx.cur_offset = data_offset;
1209 backref_ctx.found_in_send_root = 0;
1210 backref_ctx.extent_len = num_bytes;
1213 * The last extent of a file may be too large due to page alignment.
1214 * We need to adjust extent_len in this case so that the checks in
1215 * __iterate_backrefs work.
1217 if (data_offset + num_bytes >= ino_size)
1218 backref_ctx.extent_len = ino_size - data_offset;
1221 * Now collect all backrefs.
1223 extent_item_pos = logical - found_key.objectid;
1224 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1225 found_key.objectid, extent_item_pos, 1,
1226 __iterate_backrefs, &backref_ctx);
1230 if (!backref_ctx.found_in_send_root) {
1231 /* found a bug in backref code? */
1233 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1234 "send_root. inode=%llu, offset=%llu, "
1236 ino, data_offset, logical);
1240 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1242 "num_bytes=%llu, logical=%llu\n",
1243 data_offset, ino, num_bytes, logical);
1245 if (!backref_ctx.found)
1246 verbose_printk("btrfs: no clones found\n");
1248 cur_clone_root = NULL;
1249 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1250 if (sctx->clone_roots[i].found_refs) {
1251 if (!cur_clone_root)
1252 cur_clone_root = sctx->clone_roots + i;
1253 else if (sctx->clone_roots[i].root == sctx->send_root)
1254 /* prefer clones from send_root over others */
1255 cur_clone_root = sctx->clone_roots + i;
1261 if (cur_clone_root) {
1262 *found = cur_clone_root;
1269 btrfs_free_path(tmp_path);
1273 static int read_symlink(struct send_ctx *sctx,
1274 struct btrfs_root *root,
1276 struct fs_path *dest)
1279 struct btrfs_path *path;
1280 struct btrfs_key key;
1281 struct btrfs_file_extent_item *ei;
1287 path = alloc_path_for_send();
1292 key.type = BTRFS_EXTENT_DATA_KEY;
1294 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1299 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1300 struct btrfs_file_extent_item);
1301 type = btrfs_file_extent_type(path->nodes[0], ei);
1302 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1303 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1304 BUG_ON(compression);
1306 off = btrfs_file_extent_inline_start(ei);
1307 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1309 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1314 btrfs_free_path(path);
1319 * Helper function to generate a file name that is unique in the root of
1320 * send_root and parent_root. This is used to generate names for orphan inodes.
1322 static int gen_unique_name(struct send_ctx *sctx,
1324 struct fs_path *dest)
1327 struct btrfs_path *path;
1328 struct btrfs_dir_item *di;
1333 path = alloc_path_for_send();
1338 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1340 if (len >= sizeof(tmp)) {
1341 /* should really not happen */
1346 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1347 path, BTRFS_FIRST_FREE_OBJECTID,
1348 tmp, strlen(tmp), 0);
1349 btrfs_release_path(path);
1355 /* not unique, try again */
1360 if (!sctx->parent_root) {
1366 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1367 path, BTRFS_FIRST_FREE_OBJECTID,
1368 tmp, strlen(tmp), 0);
1369 btrfs_release_path(path);
1375 /* not unique, try again */
1383 ret = fs_path_add(dest, tmp, strlen(tmp));
1386 btrfs_free_path(path);
1391 inode_state_no_change,
1392 inode_state_will_create,
1393 inode_state_did_create,
1394 inode_state_will_delete,
1395 inode_state_did_delete,
1398 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1406 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1408 if (ret < 0 && ret != -ENOENT)
1412 if (!sctx->parent_root) {
1413 right_ret = -ENOENT;
1415 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1417 if (ret < 0 && ret != -ENOENT)
1422 if (!left_ret && !right_ret) {
1423 if (left_gen == gen && right_gen == gen)
1424 ret = inode_state_no_change;
1425 else if (left_gen == gen) {
1426 if (ino < sctx->send_progress)
1427 ret = inode_state_did_create;
1429 ret = inode_state_will_create;
1430 } else if (right_gen == gen) {
1431 if (ino < sctx->send_progress)
1432 ret = inode_state_did_delete;
1434 ret = inode_state_will_delete;
1438 } else if (!left_ret) {
1439 if (left_gen == gen) {
1440 if (ino < sctx->send_progress)
1441 ret = inode_state_did_create;
1443 ret = inode_state_will_create;
1447 } else if (!right_ret) {
1448 if (right_gen == gen) {
1449 if (ino < sctx->send_progress)
1450 ret = inode_state_did_delete;
1452 ret = inode_state_will_delete;
1464 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1468 ret = get_cur_inode_state(sctx, ino, gen);
1472 if (ret == inode_state_no_change ||
1473 ret == inode_state_did_create ||
1474 ret == inode_state_will_delete)
1484 * Helper function to lookup a dir item in a dir.
1486 static int lookup_dir_item_inode(struct btrfs_root *root,
1487 u64 dir, const char *name, int name_len,
1492 struct btrfs_dir_item *di;
1493 struct btrfs_key key;
1494 struct btrfs_path *path;
1496 path = alloc_path_for_send();
1500 di = btrfs_lookup_dir_item(NULL, root, path,
1501 dir, name, name_len, 0);
1510 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1511 *found_inode = key.objectid;
1512 *found_type = btrfs_dir_type(path->nodes[0], di);
1515 btrfs_free_path(path);
1519 static int get_first_ref(struct send_ctx *sctx,
1520 struct btrfs_root *root, u64 ino,
1521 u64 *dir, u64 *dir_gen, struct fs_path *name)
1524 struct btrfs_key key;
1525 struct btrfs_key found_key;
1526 struct btrfs_path *path;
1527 struct btrfs_inode_ref *iref;
1530 path = alloc_path_for_send();
1535 key.type = BTRFS_INODE_REF_KEY;
1538 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1542 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1544 if (ret || found_key.objectid != key.objectid ||
1545 found_key.type != key.type) {
1550 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1551 struct btrfs_inode_ref);
1552 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1553 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1554 (unsigned long)(iref + 1), len);
1557 btrfs_release_path(path);
1559 ret = get_inode_info(root, found_key.offset, NULL, dir_gen, NULL, NULL,
1564 *dir = found_key.offset;
1567 btrfs_free_path(path);
1571 static int is_first_ref(struct send_ctx *sctx,
1572 struct btrfs_root *root,
1574 const char *name, int name_len)
1577 struct fs_path *tmp_name;
1581 tmp_name = fs_path_alloc(sctx);
1585 ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1589 if (name_len != fs_path_len(tmp_name)) {
1594 ret = memcmp(tmp_name->start, name, name_len);
1601 fs_path_free(sctx, tmp_name);
1605 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1606 const char *name, int name_len,
1607 u64 *who_ino, u64 *who_gen)
1610 u64 other_inode = 0;
1613 if (!sctx->parent_root)
1616 ret = is_inode_existent(sctx, dir, dir_gen);
1620 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1621 &other_inode, &other_type);
1622 if (ret < 0 && ret != -ENOENT)
1629 if (other_inode > sctx->send_progress) {
1630 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1631 who_gen, NULL, NULL, NULL);
1636 *who_ino = other_inode;
1645 static int did_overwrite_ref(struct send_ctx *sctx,
1646 u64 dir, u64 dir_gen,
1647 u64 ino, u64 ino_gen,
1648 const char *name, int name_len)
1655 if (!sctx->parent_root)
1658 ret = is_inode_existent(sctx, dir, dir_gen);
1662 /* check if the ref was overwritten by another ref */
1663 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1664 &ow_inode, &other_type);
1665 if (ret < 0 && ret != -ENOENT)
1668 /* was never and will never be overwritten */
1673 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1678 if (ow_inode == ino && gen == ino_gen) {
1683 /* we know that it is or will be overwritten. check this now */
1684 if (ow_inode < sctx->send_progress)
1693 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1696 struct fs_path *name = NULL;
1700 if (!sctx->parent_root)
1703 name = fs_path_alloc(sctx);
1707 ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name);
1711 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1712 name->start, fs_path_len(name));
1717 fs_path_free(sctx, name);
1721 static int name_cache_insert(struct send_ctx *sctx,
1722 struct name_cache_entry *nce)
1725 struct name_cache_entry **ncea;
1727 ncea = radix_tree_lookup(&sctx->name_cache, nce->ino);
1736 ncea = kmalloc(sizeof(void *) * 2, GFP_NOFS);
1742 ret = radix_tree_insert(&sctx->name_cache, nce->ino, ncea);
1746 list_add_tail(&nce->list, &sctx->name_cache_list);
1747 sctx->name_cache_size++;
1752 static void name_cache_delete(struct send_ctx *sctx,
1753 struct name_cache_entry *nce)
1755 struct name_cache_entry **ncea;
1757 ncea = radix_tree_lookup(&sctx->name_cache, nce->ino);
1762 else if (ncea[1] == nce)
1767 if (!ncea[0] && !ncea[1]) {
1768 radix_tree_delete(&sctx->name_cache, nce->ino);
1772 list_del(&nce->list);
1774 sctx->name_cache_size--;
1777 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1780 struct name_cache_entry **ncea;
1782 ncea = radix_tree_lookup(&sctx->name_cache, ino);
1786 if (ncea[0] && ncea[0]->gen == gen)
1788 else if (ncea[1] && ncea[1]->gen == gen)
1793 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1795 list_del(&nce->list);
1796 list_add_tail(&nce->list, &sctx->name_cache_list);
1799 static void name_cache_clean_unused(struct send_ctx *sctx)
1801 struct name_cache_entry *nce;
1803 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1806 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1807 nce = list_entry(sctx->name_cache_list.next,
1808 struct name_cache_entry, list);
1809 name_cache_delete(sctx, nce);
1814 static void name_cache_free(struct send_ctx *sctx)
1816 struct name_cache_entry *nce;
1817 struct name_cache_entry *tmp;
1819 list_for_each_entry_safe(nce, tmp, &sctx->name_cache_list, list) {
1820 name_cache_delete(sctx, nce);
1824 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1828 struct fs_path *dest)
1832 struct btrfs_path *path = NULL;
1833 struct name_cache_entry *nce = NULL;
1835 nce = name_cache_search(sctx, ino, gen);
1837 if (ino < sctx->send_progress && nce->need_later_update) {
1838 name_cache_delete(sctx, nce);
1842 name_cache_used(sctx, nce);
1843 *parent_ino = nce->parent_ino;
1844 *parent_gen = nce->parent_gen;
1845 ret = fs_path_add(dest, nce->name, nce->name_len);
1853 path = alloc_path_for_send();
1857 ret = is_inode_existent(sctx, ino, gen);
1862 ret = gen_unique_name(sctx, ino, gen, dest);
1869 if (ino < sctx->send_progress)
1870 ret = get_first_ref(sctx, sctx->send_root, ino,
1871 parent_ino, parent_gen, dest);
1873 ret = get_first_ref(sctx, sctx->parent_root, ino,
1874 parent_ino, parent_gen, dest);
1878 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
1879 dest->start, dest->end - dest->start);
1883 fs_path_reset(dest);
1884 ret = gen_unique_name(sctx, ino, gen, dest);
1891 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
1899 nce->parent_ino = *parent_ino;
1900 nce->parent_gen = *parent_gen;
1901 nce->name_len = fs_path_len(dest);
1903 strcpy(nce->name, dest->start);
1904 memset(&nce->use_list, 0, sizeof(nce->use_list));
1906 if (ino < sctx->send_progress)
1907 nce->need_later_update = 0;
1909 nce->need_later_update = 1;
1911 nce_ret = name_cache_insert(sctx, nce);
1914 name_cache_clean_unused(sctx);
1917 btrfs_free_path(path);
1922 * Magic happens here. This function returns the first ref to an inode as it
1923 * would look like while receiving the stream at this point in time.
1924 * We walk the path up to the root. For every inode in between, we check if it
1925 * was already processed/sent. If yes, we continue with the parent as found
1926 * in send_root. If not, we continue with the parent as found in parent_root.
1927 * If we encounter an inode that was deleted at this point in time, we use the
1928 * inodes "orphan" name instead of the real name and stop. Same with new inodes
1929 * that were not created yet and overwritten inodes/refs.
1931 * When do we have have orphan inodes:
1932 * 1. When an inode is freshly created and thus no valid refs are available yet
1933 * 2. When a directory lost all it's refs (deleted) but still has dir items
1934 * inside which were not processed yet (pending for move/delete). If anyone
1935 * tried to get the path to the dir items, it would get a path inside that
1937 * 3. When an inode is moved around or gets new links, it may overwrite the ref
1938 * of an unprocessed inode. If in that case the first ref would be
1939 * overwritten, the overwritten inode gets "orphanized". Later when we
1940 * process this overwritten inode, it is restored at a new place by moving
1943 * sctx->send_progress tells this function at which point in time receiving
1946 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
1947 struct fs_path *dest)
1950 struct fs_path *name = NULL;
1951 u64 parent_inode = 0;
1955 name = fs_path_alloc(sctx);
1962 fs_path_reset(dest);
1964 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
1965 fs_path_reset(name);
1967 ret = __get_cur_name_and_parent(sctx, ino, gen,
1968 &parent_inode, &parent_gen, name);
1974 ret = fs_path_add_path(dest, name);
1983 fs_path_free(sctx, name);
1985 fs_path_unreverse(dest);
1990 * Called for regular files when sending extents data. Opens a struct file
1991 * to read from the file.
1993 static int open_cur_inode_file(struct send_ctx *sctx)
1996 struct btrfs_key key;
1998 struct inode *inode;
1999 struct dentry *dentry;
2003 if (sctx->cur_inode_filp)
2006 key.objectid = sctx->cur_ino;
2007 key.type = BTRFS_INODE_ITEM_KEY;
2010 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2012 if (IS_ERR(inode)) {
2013 ret = PTR_ERR(inode);
2017 dentry = d_obtain_alias(inode);
2019 if (IS_ERR(dentry)) {
2020 ret = PTR_ERR(dentry);
2024 path.mnt = sctx->mnt;
2025 path.dentry = dentry;
2026 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2030 ret = PTR_ERR(filp);
2033 sctx->cur_inode_filp = filp;
2037 * no xxxput required here as every vfs op
2038 * does it by itself on failure
2044 * Closes the struct file that was created in open_cur_inode_file
2046 static int close_cur_inode_file(struct send_ctx *sctx)
2050 if (!sctx->cur_inode_filp)
2053 ret = filp_close(sctx->cur_inode_filp, NULL);
2054 sctx->cur_inode_filp = NULL;
2061 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2063 static int send_subvol_begin(struct send_ctx *sctx)
2066 struct btrfs_root *send_root = sctx->send_root;
2067 struct btrfs_root *parent_root = sctx->parent_root;
2068 struct btrfs_path *path;
2069 struct btrfs_key key;
2070 struct btrfs_root_ref *ref;
2071 struct extent_buffer *leaf;
2075 path = alloc_path_for_send();
2079 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2081 btrfs_free_path(path);
2085 key.objectid = send_root->objectid;
2086 key.type = BTRFS_ROOT_BACKREF_KEY;
2089 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2098 leaf = path->nodes[0];
2099 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2100 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2101 key.objectid != send_root->objectid) {
2105 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2106 namelen = btrfs_root_ref_name_len(leaf, ref);
2107 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2108 btrfs_release_path(path);
2114 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2118 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2123 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2124 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2125 sctx->send_root->root_item.uuid);
2126 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2127 sctx->send_root->root_item.ctransid);
2129 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2130 sctx->parent_root->root_item.uuid);
2131 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2132 sctx->parent_root->root_item.ctransid);
2135 ret = send_cmd(sctx);
2139 btrfs_free_path(path);
2144 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2149 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2151 p = fs_path_alloc(sctx);
2155 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2159 ret = get_cur_path(sctx, ino, gen, p);
2162 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2163 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2165 ret = send_cmd(sctx);
2169 fs_path_free(sctx, p);
2173 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2178 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2180 p = fs_path_alloc(sctx);
2184 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2188 ret = get_cur_path(sctx, ino, gen, p);
2191 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2192 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2194 ret = send_cmd(sctx);
2198 fs_path_free(sctx, p);
2202 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2207 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2209 p = fs_path_alloc(sctx);
2213 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2217 ret = get_cur_path(sctx, ino, gen, p);
2220 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2221 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2222 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2224 ret = send_cmd(sctx);
2228 fs_path_free(sctx, p);
2232 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2235 struct fs_path *p = NULL;
2236 struct btrfs_inode_item *ii;
2237 struct btrfs_path *path = NULL;
2238 struct extent_buffer *eb;
2239 struct btrfs_key key;
2242 verbose_printk("btrfs: send_utimes %llu\n", ino);
2244 p = fs_path_alloc(sctx);
2248 path = alloc_path_for_send();
2255 key.type = BTRFS_INODE_ITEM_KEY;
2257 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2261 eb = path->nodes[0];
2262 slot = path->slots[0];
2263 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2265 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2269 ret = get_cur_path(sctx, ino, gen, p);
2272 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2273 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2274 btrfs_inode_atime(ii));
2275 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2276 btrfs_inode_mtime(ii));
2277 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2278 btrfs_inode_ctime(ii));
2281 ret = send_cmd(sctx);
2285 fs_path_free(sctx, p);
2286 btrfs_free_path(path);
2291 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2292 * a valid path yet because we did not process the refs yet. So, the inode
2293 * is created as orphan.
2295 static int send_create_inode(struct send_ctx *sctx, struct btrfs_path *path,
2296 struct btrfs_key *key)
2299 struct extent_buffer *eb = path->nodes[0];
2300 struct btrfs_inode_item *ii;
2302 int slot = path->slots[0];
2306 verbose_printk("btrfs: send_create_inode %llu\n", sctx->cur_ino);
2308 p = fs_path_alloc(sctx);
2312 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2313 mode = btrfs_inode_mode(eb, ii);
2316 cmd = BTRFS_SEND_C_MKFILE;
2317 else if (S_ISDIR(mode))
2318 cmd = BTRFS_SEND_C_MKDIR;
2319 else if (S_ISLNK(mode))
2320 cmd = BTRFS_SEND_C_SYMLINK;
2321 else if (S_ISCHR(mode) || S_ISBLK(mode))
2322 cmd = BTRFS_SEND_C_MKNOD;
2323 else if (S_ISFIFO(mode))
2324 cmd = BTRFS_SEND_C_MKFIFO;
2325 else if (S_ISSOCK(mode))
2326 cmd = BTRFS_SEND_C_MKSOCK;
2328 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2329 (int)(mode & S_IFMT));
2334 ret = begin_cmd(sctx, cmd);
2338 ret = gen_unique_name(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
2342 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2343 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, sctx->cur_ino);
2345 if (S_ISLNK(mode)) {
2347 ret = read_symlink(sctx, sctx->send_root, sctx->cur_ino, p);
2350 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2351 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2352 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2353 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, btrfs_inode_rdev(eb, ii));
2356 ret = send_cmd(sctx);
2363 fs_path_free(sctx, p);
2367 struct recorded_ref {
2368 struct list_head list;
2371 struct fs_path *full_path;
2379 * We need to process new refs before deleted refs, but compare_tree gives us
2380 * everything mixed. So we first record all refs and later process them.
2381 * This function is a helper to record one ref.
2383 static int record_ref(struct list_head *head, u64 dir,
2384 u64 dir_gen, struct fs_path *path)
2386 struct recorded_ref *ref;
2389 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2394 ref->dir_gen = dir_gen;
2395 ref->full_path = path;
2397 tmp = strrchr(ref->full_path->start, '/');
2399 ref->name_len = ref->full_path->end - ref->full_path->start;
2400 ref->name = ref->full_path->start;
2401 ref->dir_path_len = 0;
2402 ref->dir_path = ref->full_path->start;
2405 ref->name_len = ref->full_path->end - tmp;
2407 ref->dir_path = ref->full_path->start;
2408 ref->dir_path_len = ref->full_path->end -
2409 ref->full_path->start - 1 - ref->name_len;
2412 list_add_tail(&ref->list, head);
2416 static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head)
2418 struct recorded_ref *cur;
2419 struct recorded_ref *tmp;
2421 list_for_each_entry_safe(cur, tmp, head, list) {
2422 fs_path_free(sctx, cur->full_path);
2425 INIT_LIST_HEAD(head);
2428 static void free_recorded_refs(struct send_ctx *sctx)
2430 __free_recorded_refs(sctx, &sctx->new_refs);
2431 __free_recorded_refs(sctx, &sctx->deleted_refs);
2435 * Renames/moves a file/dir to it's orphan name. Used when the first
2436 * ref of an unprocessed inode gets overwritten and for all non empty
2439 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2440 struct fs_path *path)
2443 struct fs_path *orphan;
2445 orphan = fs_path_alloc(sctx);
2449 ret = gen_unique_name(sctx, ino, gen, orphan);
2453 ret = send_rename(sctx, path, orphan);
2456 fs_path_free(sctx, orphan);
2461 * Returns 1 if a directory can be removed at this point in time.
2462 * We check this by iterating all dir items and checking if the inode behind
2463 * the dir item was already processed.
2465 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2468 struct btrfs_root *root = sctx->parent_root;
2469 struct btrfs_path *path;
2470 struct btrfs_key key;
2471 struct btrfs_key found_key;
2472 struct btrfs_key loc;
2473 struct btrfs_dir_item *di;
2475 path = alloc_path_for_send();
2480 key.type = BTRFS_DIR_INDEX_KEY;
2484 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2488 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2491 if (ret || found_key.objectid != key.objectid ||
2492 found_key.type != key.type) {
2496 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2497 struct btrfs_dir_item);
2498 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2500 if (loc.objectid > send_progress) {
2505 btrfs_release_path(path);
2506 key.offset = found_key.offset + 1;
2512 btrfs_free_path(path);
2516 struct finish_unordered_dir_ctx {
2517 struct send_ctx *sctx;
2518 struct fs_path *cur_path;
2519 struct fs_path *dir_path;
2525 int __finish_unordered_dir(int num, struct btrfs_key *di_key,
2526 const char *name, int name_len,
2527 const char *data, int data_len,
2531 struct finish_unordered_dir_ctx *fctx = ctx;
2532 struct send_ctx *sctx = fctx->sctx;
2537 if (di_key->objectid >= fctx->dir_ino)
2540 fs_path_reset(fctx->cur_path);
2542 ret = get_inode_info(sctx->send_root, di_key->objectid,
2543 NULL, &di_gen, &di_mode, NULL, NULL);
2547 ret = is_first_ref(sctx, sctx->send_root, di_key->objectid,
2548 fctx->dir_ino, name, name_len);
2553 ret = gen_unique_name(sctx, di_key->objectid, di_gen,
2556 ret = get_cur_path(sctx, di_key->objectid, di_gen,
2562 ret = fs_path_add(fctx->dir_path, name, name_len);
2566 if (!fctx->delete_pass) {
2567 if (S_ISDIR(di_mode)) {
2568 ret = send_rename(sctx, fctx->cur_path,
2571 ret = send_link(sctx, fctx->dir_path,
2574 fctx->need_delete = 1;
2576 } else if (!S_ISDIR(di_mode)) {
2577 ret = send_unlink(sctx, fctx->cur_path);
2582 fs_path_remove(fctx->dir_path);
2589 * Go through all dir items and see if we find refs which could not be created
2590 * in the past because the dir did not exist at that time.
2592 static int finish_outoforder_dir(struct send_ctx *sctx, u64 dir, u64 dir_gen)
2595 struct btrfs_path *path = NULL;
2596 struct btrfs_key key;
2597 struct btrfs_key found_key;
2598 struct extent_buffer *eb;
2599 struct finish_unordered_dir_ctx fctx;
2602 path = alloc_path_for_send();
2608 memset(&fctx, 0, sizeof(fctx));
2610 fctx.cur_path = fs_path_alloc(sctx);
2611 fctx.dir_path = fs_path_alloc(sctx);
2612 if (!fctx.cur_path || !fctx.dir_path) {
2618 ret = get_cur_path(sctx, dir, dir_gen, fctx.dir_path);
2623 * We do two passes. The first links in the new refs and the second
2624 * deletes orphans if required. Deletion of orphans is not required for
2625 * directory inodes, as we always have only one ref and use rename
2626 * instead of link for those.
2631 key.type = BTRFS_DIR_ITEM_KEY;
2634 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2638 eb = path->nodes[0];
2639 slot = path->slots[0];
2640 btrfs_item_key_to_cpu(eb, &found_key, slot);
2642 if (found_key.objectid != key.objectid ||
2643 found_key.type != key.type) {
2644 btrfs_release_path(path);
2648 ret = iterate_dir_item(sctx, sctx->send_root, path,
2649 &found_key, __finish_unordered_dir,
2654 key.offset = found_key.offset + 1;
2655 btrfs_release_path(path);
2658 if (!fctx.delete_pass && fctx.need_delete) {
2659 fctx.delete_pass = 1;
2664 btrfs_free_path(path);
2665 fs_path_free(sctx, fctx.cur_path);
2666 fs_path_free(sctx, fctx.dir_path);
2671 * This does all the move/link/unlink/rmdir magic.
2673 static int process_recorded_refs(struct send_ctx *sctx)
2676 struct recorded_ref *cur;
2677 struct ulist *check_dirs = NULL;
2678 struct ulist_iterator uit;
2679 struct ulist_node *un;
2680 struct fs_path *valid_path = NULL;
2683 int did_overwrite = 0;
2686 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2688 valid_path = fs_path_alloc(sctx);
2694 check_dirs = ulist_alloc(GFP_NOFS);
2701 * First, check if the first ref of the current inode was overwritten
2702 * before. If yes, we know that the current inode was already orphanized
2703 * and thus use the orphan name. If not, we can use get_cur_path to
2704 * get the path of the first ref as it would like while receiving at
2705 * this point in time.
2706 * New inodes are always orphan at the beginning, so force to use the
2707 * orphan name in this case.
2708 * The first ref is stored in valid_path and will be updated if it
2709 * gets moved around.
2711 if (!sctx->cur_inode_new) {
2712 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2713 sctx->cur_inode_gen);
2719 if (sctx->cur_inode_new || did_overwrite) {
2720 ret = gen_unique_name(sctx, sctx->cur_ino,
2721 sctx->cur_inode_gen, valid_path);
2726 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2732 list_for_each_entry(cur, &sctx->new_refs, list) {
2734 * Check if this new ref would overwrite the first ref of
2735 * another unprocessed inode. If yes, orphanize the
2736 * overwritten inode. If we find an overwritten ref that is
2737 * not the first ref, simply unlink it.
2739 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2740 cur->name, cur->name_len,
2741 &ow_inode, &ow_gen);
2745 ret = is_first_ref(sctx, sctx->parent_root,
2746 ow_inode, cur->dir, cur->name,
2751 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2756 ret = send_unlink(sctx, cur->full_path);
2763 * link/move the ref to the new place. If we have an orphan
2764 * inode, move it and update valid_path. If not, link or move
2765 * it depending on the inode mode.
2767 if (is_orphan && !sctx->cur_inode_first_ref_orphan) {
2768 ret = send_rename(sctx, valid_path, cur->full_path);
2772 ret = fs_path_copy(valid_path, cur->full_path);
2776 if (S_ISDIR(sctx->cur_inode_mode)) {
2778 * Dirs can't be linked, so move it. For moved
2779 * dirs, we always have one new and one deleted
2780 * ref. The deleted ref is ignored later.
2782 ret = send_rename(sctx, valid_path,
2786 ret = fs_path_copy(valid_path, cur->full_path);
2790 ret = send_link(sctx, cur->full_path,
2796 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2802 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2804 * Check if we can already rmdir the directory. If not,
2805 * orphanize it. For every dir item inside that gets deleted
2806 * later, we do this check again and rmdir it then if possible.
2807 * See the use of check_dirs for more details.
2809 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2813 ret = send_rmdir(sctx, valid_path);
2816 } else if (!is_orphan) {
2817 ret = orphanize_inode(sctx, sctx->cur_ino,
2818 sctx->cur_inode_gen, valid_path);
2824 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2825 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2830 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2832 * We have a non dir inode. Go through all deleted refs and
2833 * unlink them if they were not already overwritten by other
2836 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2837 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2838 sctx->cur_ino, sctx->cur_inode_gen,
2839 cur->name, cur->name_len);
2844 * In case the inode was moved to a directory
2845 * that was not created yet (see
2846 * __record_new_ref), we can not unlink the ref
2847 * as it will be needed later when the parent
2848 * directory is created, so that we can move in
2849 * the inode to the new dir.
2852 sctx->cur_inode_first_ref_orphan) {
2853 ret = orphanize_inode(sctx,
2855 sctx->cur_inode_gen,
2859 ret = gen_unique_name(sctx,
2861 sctx->cur_inode_gen,
2868 ret = send_unlink(sctx, cur->full_path);
2873 ret = ulist_add(check_dirs, cur->dir, cur->dir_gen,
2880 * If the inode is still orphan, unlink the orphan. This may
2881 * happen when a previous inode did overwrite the first ref
2882 * of this inode and no new refs were added for the current
2884 * We can however not delete the orphan in case the inode relies
2885 * in a directory that was not created yet (see
2888 if (is_orphan && !sctx->cur_inode_first_ref_orphan) {
2889 ret = send_unlink(sctx, valid_path);
2896 * We did collect all parent dirs where cur_inode was once located. We
2897 * now go through all these dirs and check if they are pending for
2898 * deletion and if it's finally possible to perform the rmdir now.
2899 * We also update the inode stats of the parent dirs here.
2901 ULIST_ITER_INIT(&uit);
2902 while ((un = ulist_next(check_dirs, &uit))) {
2903 if (un->val > sctx->cur_ino)
2906 ret = get_cur_inode_state(sctx, un->val, un->aux);
2910 if (ret == inode_state_did_create ||
2911 ret == inode_state_no_change) {
2912 /* TODO delayed utimes */
2913 ret = send_utimes(sctx, un->val, un->aux);
2916 } else if (ret == inode_state_did_delete) {
2917 ret = can_rmdir(sctx, un->val, sctx->cur_ino);
2921 ret = get_cur_path(sctx, un->val, un->aux,
2925 ret = send_rmdir(sctx, valid_path);
2933 * Current inode is now at it's new position, so we must increase
2936 sctx->send_progress = sctx->cur_ino + 1;
2939 * We may have a directory here that has pending refs which could not
2940 * be created before (because the dir did not exist before, see
2941 * __record_new_ref). finish_outoforder_dir will link/move the pending
2944 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_new) {
2945 ret = finish_outoforder_dir(sctx, sctx->cur_ino,
2946 sctx->cur_inode_gen);
2954 free_recorded_refs(sctx);
2955 ulist_free(check_dirs);
2956 fs_path_free(sctx, valid_path);
2960 static int __record_new_ref(int num, u64 dir, int index,
2961 struct fs_path *name,
2965 struct send_ctx *sctx = ctx;
2969 p = fs_path_alloc(sctx);
2973 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
2979 * The parent may be non-existent at this point in time. This happens
2980 * if the ino of the parent dir is higher then the current ino. In this
2981 * case, we can not process this ref until the parent dir is finally
2982 * created. If we reach the parent dir later, process_recorded_refs
2983 * will go through all dir items and process the refs that could not be
2984 * processed before. In case this is the first ref, we set
2985 * cur_inode_first_ref_orphan to 1 to inform process_recorded_refs to
2986 * keep an orphan of the inode so that it later can be used for
2989 ret = is_inode_existent(sctx, dir, gen);
2993 ret = is_first_ref(sctx, sctx->send_root, sctx->cur_ino, dir,
2994 name->start, fs_path_len(name));
2998 sctx->cur_inode_first_ref_orphan = 1;
3003 ret = get_cur_path(sctx, dir, gen, p);
3006 ret = fs_path_add_path(p, name);
3010 ret = record_ref(&sctx->new_refs, dir, gen, p);
3014 fs_path_free(sctx, p);
3018 static int __record_deleted_ref(int num, u64 dir, int index,
3019 struct fs_path *name,
3023 struct send_ctx *sctx = ctx;
3027 p = fs_path_alloc(sctx);
3031 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3036 ret = get_cur_path(sctx, dir, gen, p);
3039 ret = fs_path_add_path(p, name);
3043 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3047 fs_path_free(sctx, p);
3051 static int record_new_ref(struct send_ctx *sctx)
3055 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3056 sctx->cmp_key, 0, __record_new_ref, sctx);
3065 static int record_deleted_ref(struct send_ctx *sctx)
3069 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3070 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3079 struct find_ref_ctx {
3081 struct fs_path *name;
3085 static int __find_iref(int num, u64 dir, int index,
3086 struct fs_path *name,
3089 struct find_ref_ctx *ctx = ctx_;
3091 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3092 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3093 ctx->found_idx = num;
3099 static int find_iref(struct send_ctx *sctx,
3100 struct btrfs_root *root,
3101 struct btrfs_path *path,
3102 struct btrfs_key *key,
3103 u64 dir, struct fs_path *name)
3106 struct find_ref_ctx ctx;
3112 ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx);
3116 if (ctx.found_idx == -1)
3119 return ctx.found_idx;
3122 static int __record_changed_new_ref(int num, u64 dir, int index,
3123 struct fs_path *name,
3127 struct send_ctx *sctx = ctx;
3129 ret = find_iref(sctx, sctx->parent_root, sctx->right_path,
3130 sctx->cmp_key, dir, name);
3132 ret = __record_new_ref(num, dir, index, name, sctx);
3139 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3140 struct fs_path *name,
3144 struct send_ctx *sctx = ctx;
3146 ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3149 ret = __record_deleted_ref(num, dir, index, name, sctx);
3156 static int record_changed_ref(struct send_ctx *sctx)
3160 ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path,
3161 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3164 ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path,
3165 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3175 * Record and process all refs at once. Needed when an inode changes the
3176 * generation number, which means that it was deleted and recreated.
3178 static int process_all_refs(struct send_ctx *sctx,
3179 enum btrfs_compare_tree_result cmd)
3182 struct btrfs_root *root;
3183 struct btrfs_path *path;
3184 struct btrfs_key key;
3185 struct btrfs_key found_key;
3186 struct extent_buffer *eb;
3188 iterate_inode_ref_t cb;
3190 path = alloc_path_for_send();
3194 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3195 root = sctx->send_root;
3196 cb = __record_new_ref;
3197 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3198 root = sctx->parent_root;
3199 cb = __record_deleted_ref;
3204 key.objectid = sctx->cmp_key->objectid;
3205 key.type = BTRFS_INODE_REF_KEY;
3208 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3210 btrfs_release_path(path);
3214 btrfs_release_path(path);
3218 eb = path->nodes[0];
3219 slot = path->slots[0];
3220 btrfs_item_key_to_cpu(eb, &found_key, slot);
3222 if (found_key.objectid != key.objectid ||
3223 found_key.type != key.type) {
3224 btrfs_release_path(path);
3228 ret = iterate_inode_ref(sctx, sctx->parent_root, path,
3229 &found_key, 0, cb, sctx);
3230 btrfs_release_path(path);
3234 key.offset = found_key.offset + 1;
3237 ret = process_recorded_refs(sctx);
3240 btrfs_free_path(path);
3244 static int send_set_xattr(struct send_ctx *sctx,
3245 struct fs_path *path,
3246 const char *name, int name_len,
3247 const char *data, int data_len)
3251 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3255 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3256 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3257 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3259 ret = send_cmd(sctx);
3266 static int send_remove_xattr(struct send_ctx *sctx,
3267 struct fs_path *path,
3268 const char *name, int name_len)
3272 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3276 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3277 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3279 ret = send_cmd(sctx);
3286 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3287 const char *name, int name_len,
3288 const char *data, int data_len,
3292 struct send_ctx *sctx = ctx;
3294 posix_acl_xattr_header dummy_acl;
3296 p = fs_path_alloc(sctx);
3301 * This hack is needed because empty acl's are stored as zero byte
3302 * data in xattrs. Problem with that is, that receiving these zero byte
3303 * acl's will fail later. To fix this, we send a dummy acl list that
3304 * only contains the version number and no entries.
3306 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3307 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3308 if (data_len == 0) {
3309 dummy_acl.a_version =
3310 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3311 data = (char *)&dummy_acl;
3312 data_len = sizeof(dummy_acl);
3316 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3320 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3323 fs_path_free(sctx, p);
3327 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3328 const char *name, int name_len,
3329 const char *data, int data_len,
3333 struct send_ctx *sctx = ctx;
3336 p = fs_path_alloc(sctx);
3340 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3344 ret = send_remove_xattr(sctx, p, name, name_len);
3347 fs_path_free(sctx, p);
3351 static int process_new_xattr(struct send_ctx *sctx)
3355 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3356 sctx->cmp_key, __process_new_xattr, sctx);
3361 static int process_deleted_xattr(struct send_ctx *sctx)
3365 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3366 sctx->cmp_key, __process_deleted_xattr, sctx);
3371 struct find_xattr_ctx {
3379 static int __find_xattr(int num, struct btrfs_key *di_key,
3380 const char *name, int name_len,
3381 const char *data, int data_len,
3382 u8 type, void *vctx)
3384 struct find_xattr_ctx *ctx = vctx;
3386 if (name_len == ctx->name_len &&
3387 strncmp(name, ctx->name, name_len) == 0) {
3388 ctx->found_idx = num;
3389 ctx->found_data_len = data_len;
3390 ctx->found_data = kmalloc(data_len, GFP_NOFS);
3391 if (!ctx->found_data)
3393 memcpy(ctx->found_data, data, data_len);
3399 static int find_xattr(struct send_ctx *sctx,
3400 struct btrfs_root *root,
3401 struct btrfs_path *path,
3402 struct btrfs_key *key,
3403 const char *name, int name_len,
3404 char **data, int *data_len)
3407 struct find_xattr_ctx ctx;
3410 ctx.name_len = name_len;
3412 ctx.found_data = NULL;
3413 ctx.found_data_len = 0;
3415 ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx);
3419 if (ctx.found_idx == -1)
3422 *data = ctx.found_data;
3423 *data_len = ctx.found_data_len;
3425 kfree(ctx.found_data);
3427 return ctx.found_idx;
3431 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3432 const char *name, int name_len,
3433 const char *data, int data_len,
3437 struct send_ctx *sctx = ctx;
3438 char *found_data = NULL;
3439 int found_data_len = 0;
3440 struct fs_path *p = NULL;
3442 ret = find_xattr(sctx, sctx->parent_root, sctx->right_path,
3443 sctx->cmp_key, name, name_len, &found_data,
3445 if (ret == -ENOENT) {
3446 ret = __process_new_xattr(num, di_key, name, name_len, data,
3447 data_len, type, ctx);
3448 } else if (ret >= 0) {
3449 if (data_len != found_data_len ||
3450 memcmp(data, found_data, data_len)) {
3451 ret = __process_new_xattr(num, di_key, name, name_len,
3452 data, data_len, type, ctx);
3459 fs_path_free(sctx, p);
3463 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3464 const char *name, int name_len,
3465 const char *data, int data_len,
3469 struct send_ctx *sctx = ctx;
3471 ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key,
3472 name, name_len, NULL, NULL);
3474 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3475 data_len, type, ctx);
3482 static int process_changed_xattr(struct send_ctx *sctx)
3486 ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path,
3487 sctx->cmp_key, __process_changed_new_xattr, sctx);
3490 ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path,
3491 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3497 static int process_all_new_xattrs(struct send_ctx *sctx)
3500 struct btrfs_root *root;
3501 struct btrfs_path *path;
3502 struct btrfs_key key;
3503 struct btrfs_key found_key;
3504 struct extent_buffer *eb;
3507 path = alloc_path_for_send();
3511 root = sctx->send_root;
3513 key.objectid = sctx->cmp_key->objectid;
3514 key.type = BTRFS_XATTR_ITEM_KEY;
3517 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3525 eb = path->nodes[0];
3526 slot = path->slots[0];
3527 btrfs_item_key_to_cpu(eb, &found_key, slot);
3529 if (found_key.objectid != key.objectid ||
3530 found_key.type != key.type) {
3535 ret = iterate_dir_item(sctx, root, path, &found_key,
3536 __process_new_xattr, sctx);
3540 btrfs_release_path(path);
3541 key.offset = found_key.offset + 1;
3545 btrfs_free_path(path);
3550 * Read some bytes from the current inode/file and send a write command to
3553 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3557 loff_t pos = offset;
3559 mm_segment_t old_fs;
3561 p = fs_path_alloc(sctx);
3566 * vfs normally only accepts user space buffers for security reasons.
3567 * we only read from the file and also only provide the read_buf buffer
3568 * to vfs. As this buffer does not come from a user space call, it's
3569 * ok to temporary allow kernel space buffers.
3574 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3576 ret = open_cur_inode_file(sctx);
3580 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3587 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3591 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3595 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3596 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3597 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, readed);
3599 ret = send_cmd(sctx);
3603 fs_path_free(sctx, p);
3611 * Send a clone command to user space.
3613 static int send_clone(struct send_ctx *sctx,
3614 u64 offset, u32 len,
3615 struct clone_root *clone_root)
3618 struct btrfs_root *clone_root2 = clone_root->root;
3622 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3623 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3624 clone_root->root->objectid, clone_root->ino,
3625 clone_root->offset);
3627 p = fs_path_alloc(sctx);
3631 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3635 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3639 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3640 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3641 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3643 if (clone_root2 == sctx->send_root) {
3644 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3645 &gen, NULL, NULL, NULL);
3648 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3650 ret = get_inode_path(sctx, clone_root2, clone_root->ino, p);
3655 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3656 clone_root2->root_item.uuid);
3657 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3658 clone_root2->root_item.ctransid);
3659 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3660 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3661 clone_root->offset);
3663 ret = send_cmd(sctx);
3667 fs_path_free(sctx, p);
3671 static int send_write_or_clone(struct send_ctx *sctx,
3672 struct btrfs_path *path,
3673 struct btrfs_key *key,
3674 struct clone_root *clone_root)
3677 struct btrfs_file_extent_item *ei;
3678 u64 offset = key->offset;
3684 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3685 struct btrfs_file_extent_item);
3686 type = btrfs_file_extent_type(path->nodes[0], ei);
3687 if (type == BTRFS_FILE_EXTENT_INLINE)
3688 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3690 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3692 if (offset + len > sctx->cur_inode_size)
3693 len = sctx->cur_inode_size - offset;
3702 if (l > BTRFS_SEND_READ_SIZE)
3703 l = BTRFS_SEND_READ_SIZE;
3704 ret = send_write(sctx, pos + offset, l);
3713 ret = send_clone(sctx, offset, len, clone_root);
3720 static int is_extent_unchanged(struct send_ctx *sctx,
3721 struct btrfs_path *left_path,
3722 struct btrfs_key *ekey)
3725 struct btrfs_key key;
3726 struct btrfs_path *path = NULL;
3727 struct extent_buffer *eb;
3729 struct btrfs_key found_key;
3730 struct btrfs_file_extent_item *ei;
3735 u64 left_offset_fixed;
3741 path = alloc_path_for_send();
3745 eb = left_path->nodes[0];
3746 slot = left_path->slots[0];
3748 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3749 left_type = btrfs_file_extent_type(eb, ei);
3750 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3751 left_len = btrfs_file_extent_num_bytes(eb, ei);
3752 left_offset = btrfs_file_extent_offset(eb, ei);
3754 if (left_type != BTRFS_FILE_EXTENT_REG) {
3760 * Following comments will refer to these graphics. L is the left
3761 * extents which we are checking at the moment. 1-8 are the right
3762 * extents that we iterate.
3765 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3768 * |--1--|-2b-|...(same as above)
3770 * Alternative situation. Happens on files where extents got split.
3772 * |-----------7-----------|-6-|
3774 * Alternative situation. Happens on files which got larger.
3777 * Nothing follows after 8.
3780 key.objectid = ekey->objectid;
3781 key.type = BTRFS_EXTENT_DATA_KEY;
3782 key.offset = ekey->offset;
3783 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3792 * Handle special case where the right side has no extents at all.
3794 eb = path->nodes[0];
3795 slot = path->slots[0];
3796 btrfs_item_key_to_cpu(eb, &found_key, slot);
3797 if (found_key.objectid != key.objectid ||
3798 found_key.type != key.type) {
3804 * We're now on 2a, 2b or 7.
3807 while (key.offset < ekey->offset + left_len) {
3808 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3809 right_type = btrfs_file_extent_type(eb, ei);
3810 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3811 right_len = btrfs_file_extent_num_bytes(eb, ei);
3812 right_offset = btrfs_file_extent_offset(eb, ei);
3814 if (right_type != BTRFS_FILE_EXTENT_REG) {
3820 * Are we at extent 8? If yes, we know the extent is changed.
3821 * This may only happen on the first iteration.
3823 if (found_key.offset + right_len < ekey->offset) {
3828 left_offset_fixed = left_offset;
3829 if (key.offset < ekey->offset) {
3830 /* Fix the right offset for 2a and 7. */
3831 right_offset += ekey->offset - key.offset;
3833 /* Fix the left offset for all behind 2a and 2b */
3834 left_offset_fixed += key.offset - ekey->offset;
3838 * Check if we have the same extent.
3840 if (left_disknr + left_offset_fixed !=
3841 right_disknr + right_offset) {
3847 * Go to the next extent.
3849 ret = btrfs_next_item(sctx->parent_root, path);
3853 eb = path->nodes[0];
3854 slot = path->slots[0];
3855 btrfs_item_key_to_cpu(eb, &found_key, slot);
3857 if (ret || found_key.objectid != key.objectid ||
3858 found_key.type != key.type) {
3859 key.offset += right_len;
3862 if (found_key.offset != key.offset + right_len) {
3863 /* Should really not happen */
3872 * We're now behind the left extent (treat as unchanged) or at the end
3873 * of the right side (treat as changed).
3875 if (key.offset >= ekey->offset + left_len)
3882 btrfs_free_path(path);
3886 static int process_extent(struct send_ctx *sctx,
3887 struct btrfs_path *path,
3888 struct btrfs_key *key)
3891 struct clone_root *found_clone = NULL;
3893 if (S_ISLNK(sctx->cur_inode_mode))
3896 if (sctx->parent_root && !sctx->cur_inode_new) {
3897 ret = is_extent_unchanged(sctx, path, key);
3906 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
3907 sctx->cur_inode_size, &found_clone);
3908 if (ret != -ENOENT && ret < 0)
3911 ret = send_write_or_clone(sctx, path, key, found_clone);
3917 static int process_all_extents(struct send_ctx *sctx)
3920 struct btrfs_root *root;
3921 struct btrfs_path *path;
3922 struct btrfs_key key;
3923 struct btrfs_key found_key;
3924 struct extent_buffer *eb;
3927 root = sctx->send_root;
3928 path = alloc_path_for_send();
3932 key.objectid = sctx->cmp_key->objectid;
3933 key.type = BTRFS_EXTENT_DATA_KEY;
3936 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3944 eb = path->nodes[0];
3945 slot = path->slots[0];
3946 btrfs_item_key_to_cpu(eb, &found_key, slot);
3948 if (found_key.objectid != key.objectid ||
3949 found_key.type != key.type) {
3954 ret = process_extent(sctx, path, &found_key);
3958 btrfs_release_path(path);
3959 key.offset = found_key.offset + 1;
3963 btrfs_free_path(path);
3967 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
3971 if (sctx->cur_ino == 0)
3973 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
3974 sctx->cmp_key->type <= BTRFS_INODE_REF_KEY)
3976 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
3979 ret = process_recorded_refs(sctx);
3985 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
3997 ret = process_recorded_refs_if_needed(sctx, at_end);
4001 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4003 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4006 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4007 &left_mode, &left_uid, &left_gid);
4011 if (!S_ISLNK(sctx->cur_inode_mode)) {
4012 if (!sctx->parent_root || sctx->cur_inode_new) {
4016 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4017 NULL, NULL, &right_mode, &right_uid,
4022 if (left_uid != right_uid || left_gid != right_gid)
4024 if (left_mode != right_mode)
4029 if (S_ISREG(sctx->cur_inode_mode)) {
4030 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4031 sctx->cur_inode_size);
4037 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4038 left_uid, left_gid);
4043 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4050 * Need to send that every time, no matter if it actually changed
4051 * between the two trees as we have done changes to the inode before.
4053 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4061 static int changed_inode(struct send_ctx *sctx,
4062 enum btrfs_compare_tree_result result)
4065 struct btrfs_key *key = sctx->cmp_key;
4066 struct btrfs_inode_item *left_ii = NULL;
4067 struct btrfs_inode_item *right_ii = NULL;
4071 ret = close_cur_inode_file(sctx);
4075 sctx->cur_ino = key->objectid;
4076 sctx->cur_inode_new_gen = 0;
4077 sctx->cur_inode_first_ref_orphan = 0;
4078 sctx->send_progress = sctx->cur_ino;
4080 if (result == BTRFS_COMPARE_TREE_NEW ||
4081 result == BTRFS_COMPARE_TREE_CHANGED) {
4082 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4083 sctx->left_path->slots[0],
4084 struct btrfs_inode_item);
4085 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4088 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4089 sctx->right_path->slots[0],
4090 struct btrfs_inode_item);
4091 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4094 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4095 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4096 sctx->right_path->slots[0],
4097 struct btrfs_inode_item);
4099 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4101 if (left_gen != right_gen)
4102 sctx->cur_inode_new_gen = 1;
4105 if (result == BTRFS_COMPARE_TREE_NEW) {
4106 sctx->cur_inode_gen = left_gen;
4107 sctx->cur_inode_new = 1;
4108 sctx->cur_inode_deleted = 0;
4109 sctx->cur_inode_size = btrfs_inode_size(
4110 sctx->left_path->nodes[0], left_ii);
4111 sctx->cur_inode_mode = btrfs_inode_mode(
4112 sctx->left_path->nodes[0], left_ii);
4113 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4114 ret = send_create_inode(sctx, sctx->left_path,
4116 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4117 sctx->cur_inode_gen = right_gen;
4118 sctx->cur_inode_new = 0;
4119 sctx->cur_inode_deleted = 1;
4120 sctx->cur_inode_size = btrfs_inode_size(
4121 sctx->right_path->nodes[0], right_ii);
4122 sctx->cur_inode_mode = btrfs_inode_mode(
4123 sctx->right_path->nodes[0], right_ii);
4124 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4125 if (sctx->cur_inode_new_gen) {
4126 sctx->cur_inode_gen = right_gen;
4127 sctx->cur_inode_new = 0;
4128 sctx->cur_inode_deleted = 1;
4129 sctx->cur_inode_size = btrfs_inode_size(
4130 sctx->right_path->nodes[0], right_ii);
4131 sctx->cur_inode_mode = btrfs_inode_mode(
4132 sctx->right_path->nodes[0], right_ii);
4133 ret = process_all_refs(sctx,
4134 BTRFS_COMPARE_TREE_DELETED);
4138 sctx->cur_inode_gen = left_gen;
4139 sctx->cur_inode_new = 1;
4140 sctx->cur_inode_deleted = 0;
4141 sctx->cur_inode_size = btrfs_inode_size(
4142 sctx->left_path->nodes[0], left_ii);
4143 sctx->cur_inode_mode = btrfs_inode_mode(
4144 sctx->left_path->nodes[0], left_ii);
4145 ret = send_create_inode(sctx, sctx->left_path,
4150 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4153 ret = process_all_extents(sctx);
4156 ret = process_all_new_xattrs(sctx);
4160 sctx->cur_inode_gen = left_gen;
4161 sctx->cur_inode_new = 0;
4162 sctx->cur_inode_new_gen = 0;
4163 sctx->cur_inode_deleted = 0;
4164 sctx->cur_inode_size = btrfs_inode_size(
4165 sctx->left_path->nodes[0], left_ii);
4166 sctx->cur_inode_mode = btrfs_inode_mode(
4167 sctx->left_path->nodes[0], left_ii);
4175 static int changed_ref(struct send_ctx *sctx,
4176 enum btrfs_compare_tree_result result)
4180 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4182 if (!sctx->cur_inode_new_gen &&
4183 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4184 if (result == BTRFS_COMPARE_TREE_NEW)
4185 ret = record_new_ref(sctx);
4186 else if (result == BTRFS_COMPARE_TREE_DELETED)
4187 ret = record_deleted_ref(sctx);
4188 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4189 ret = record_changed_ref(sctx);
4195 static int changed_xattr(struct send_ctx *sctx,
4196 enum btrfs_compare_tree_result result)
4200 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4202 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4203 if (result == BTRFS_COMPARE_TREE_NEW)
4204 ret = process_new_xattr(sctx);
4205 else if (result == BTRFS_COMPARE_TREE_DELETED)
4206 ret = process_deleted_xattr(sctx);
4207 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4208 ret = process_changed_xattr(sctx);
4214 static int changed_extent(struct send_ctx *sctx,
4215 enum btrfs_compare_tree_result result)
4219 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4221 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4222 if (result != BTRFS_COMPARE_TREE_DELETED)
4223 ret = process_extent(sctx, sctx->left_path,
4231 static int changed_cb(struct btrfs_root *left_root,
4232 struct btrfs_root *right_root,
4233 struct btrfs_path *left_path,
4234 struct btrfs_path *right_path,
4235 struct btrfs_key *key,
4236 enum btrfs_compare_tree_result result,
4240 struct send_ctx *sctx = ctx;
4242 sctx->left_path = left_path;
4243 sctx->right_path = right_path;
4244 sctx->cmp_key = key;
4246 ret = finish_inode_if_needed(sctx, 0);
4250 if (key->type == BTRFS_INODE_ITEM_KEY)
4251 ret = changed_inode(sctx, result);
4252 else if (key->type == BTRFS_INODE_REF_KEY)
4253 ret = changed_ref(sctx, result);
4254 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4255 ret = changed_xattr(sctx, result);
4256 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4257 ret = changed_extent(sctx, result);
4263 static int full_send_tree(struct send_ctx *sctx)
4266 struct btrfs_trans_handle *trans = NULL;
4267 struct btrfs_root *send_root = sctx->send_root;
4268 struct btrfs_key key;
4269 struct btrfs_key found_key;
4270 struct btrfs_path *path;
4271 struct extent_buffer *eb;
4276 path = alloc_path_for_send();
4280 spin_lock(&send_root->root_times_lock);
4281 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4282 spin_unlock(&send_root->root_times_lock);
4284 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4285 key.type = BTRFS_INODE_ITEM_KEY;
4290 * We need to make sure the transaction does not get committed
4291 * while we do anything on commit roots. Join a transaction to prevent
4294 trans = btrfs_join_transaction(send_root);
4295 if (IS_ERR(trans)) {
4296 ret = PTR_ERR(trans);
4302 * Make sure the tree has not changed
4304 spin_lock(&send_root->root_times_lock);
4305 ctransid = btrfs_root_ctransid(&send_root->root_item);
4306 spin_unlock(&send_root->root_times_lock);
4308 if (ctransid != start_ctransid) {
4309 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4310 "send was modified in between. This is "
4311 "probably a bug.\n");
4316 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4324 * When someone want to commit while we iterate, end the
4325 * joined transaction and rejoin.
4327 if (btrfs_should_end_transaction(trans, send_root)) {
4328 ret = btrfs_end_transaction(trans, send_root);
4332 btrfs_release_path(path);
4336 eb = path->nodes[0];
4337 slot = path->slots[0];
4338 btrfs_item_key_to_cpu(eb, &found_key, slot);
4340 ret = changed_cb(send_root, NULL, path, NULL,
4341 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4345 key.objectid = found_key.objectid;
4346 key.type = found_key.type;
4347 key.offset = found_key.offset + 1;
4349 ret = btrfs_next_item(send_root, path);
4359 ret = finish_inode_if_needed(sctx, 1);
4362 btrfs_free_path(path);
4365 ret = btrfs_end_transaction(trans, send_root);
4367 btrfs_end_transaction(trans, send_root);
4372 static int send_subvol(struct send_ctx *sctx)
4376 ret = send_header(sctx);
4380 ret = send_subvol_begin(sctx);
4384 if (sctx->parent_root) {
4385 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4389 ret = finish_inode_if_needed(sctx, 1);
4393 ret = full_send_tree(sctx);
4400 ret = close_cur_inode_file(sctx);
4402 close_cur_inode_file(sctx);
4404 free_recorded_refs(sctx);
4408 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4411 struct btrfs_root *send_root;
4412 struct btrfs_root *clone_root;
4413 struct btrfs_fs_info *fs_info;
4414 struct btrfs_ioctl_send_args *arg = NULL;
4415 struct btrfs_key key;
4416 struct file *filp = NULL;
4417 struct send_ctx *sctx = NULL;
4419 u64 *clone_sources_tmp = NULL;
4421 if (!capable(CAP_SYS_ADMIN))
4424 send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root;
4425 fs_info = send_root->fs_info;
4427 arg = memdup_user(arg_, sizeof(*arg));
4434 if (!access_ok(VERIFY_READ, arg->clone_sources,
4435 sizeof(*arg->clone_sources *
4436 arg->clone_sources_count))) {
4441 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4447 INIT_LIST_HEAD(&sctx->new_refs);
4448 INIT_LIST_HEAD(&sctx->deleted_refs);
4449 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4450 INIT_LIST_HEAD(&sctx->name_cache_list);
4452 sctx->send_filp = fget(arg->send_fd);
4453 if (IS_ERR(sctx->send_filp)) {
4454 ret = PTR_ERR(sctx->send_filp);
4458 sctx->mnt = mnt_file->f_path.mnt;
4460 sctx->send_root = send_root;
4461 sctx->clone_roots_cnt = arg->clone_sources_count;
4463 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4464 sctx->send_buf = vmalloc(sctx->send_max_size);
4465 if (!sctx->send_buf) {
4470 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4471 if (!sctx->read_buf) {
4476 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4477 (arg->clone_sources_count + 1));
4478 if (!sctx->clone_roots) {
4483 if (arg->clone_sources_count) {
4484 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4485 sizeof(*arg->clone_sources));
4486 if (!clone_sources_tmp) {
4491 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4492 arg->clone_sources_count *
4493 sizeof(*arg->clone_sources));
4499 for (i = 0; i < arg->clone_sources_count; i++) {
4500 key.objectid = clone_sources_tmp[i];
4501 key.type = BTRFS_ROOT_ITEM_KEY;
4502 key.offset = (u64)-1;
4503 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4508 if (IS_ERR(clone_root)) {
4509 ret = PTR_ERR(clone_root);
4512 sctx->clone_roots[i].root = clone_root;
4514 vfree(clone_sources_tmp);
4515 clone_sources_tmp = NULL;
4518 if (arg->parent_root) {
4519 key.objectid = arg->parent_root;
4520 key.type = BTRFS_ROOT_ITEM_KEY;
4521 key.offset = (u64)-1;
4522 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4523 if (!sctx->parent_root) {
4530 * Clones from send_root are allowed, but only if the clone source
4531 * is behind the current send position. This is checked while searching
4532 * for possible clone sources.
4534 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4536 /* We do a bsearch later */
4537 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4538 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4541 ret = send_subvol(sctx);
4545 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4548 ret = send_cmd(sctx);
4556 vfree(clone_sources_tmp);
4559 if (sctx->send_filp)
4560 fput(sctx->send_filp);
4562 vfree(sctx->clone_roots);
4563 vfree(sctx->send_buf);
4564 vfree(sctx->read_buf);
4566 name_cache_free(sctx);