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/vmalloc.h>
28 #include <linux/string.h>
35 #include "btrfs_inode.h"
36 #include "transaction.h"
38 static int g_verbose = 0;
40 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
43 * A fs_path is a helper to dynamically build path names with unknown size.
44 * It reallocates the internal buffer on demand.
45 * It allows fast adding of path elements on the right side (normal path) and
46 * fast adding to the left side (reversed path). A reversed path can also be
47 * unreversed if needed.
57 unsigned int reversed:1;
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];
87 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
89 struct btrfs_root *send_root;
90 struct btrfs_root *parent_root;
91 struct clone_root *clone_roots;
94 /* current state of the compare_tree call */
95 struct btrfs_path *left_path;
96 struct btrfs_path *right_path;
97 struct btrfs_key *cmp_key;
100 * infos of the currently processed inode. In case of deleted inodes,
101 * these are the values from the deleted inode.
106 int cur_inode_new_gen;
107 int cur_inode_deleted;
110 u64 cur_inode_last_extent;
114 struct list_head new_refs;
115 struct list_head deleted_refs;
117 struct radix_tree_root name_cache;
118 struct list_head name_cache_list;
124 * We process inodes by their increasing order, so if before an
125 * incremental send we reverse the parent/child relationship of
126 * directories such that a directory with a lower inode number was
127 * the parent of a directory with a higher inode number, and the one
128 * becoming the new parent got renamed too, we can't rename/move the
129 * directory with lower inode number when we finish processing it - we
130 * must process the directory with higher inode number first, then
131 * rename/move it and then rename/move the directory with lower inode
132 * number. Example follows.
134 * Tree state when the first send was performed:
146 * Tree state when the second (incremental) send is performed:
155 * The sequence of steps that lead to the second state was:
157 * mv /a/b/c/d /a/b/c2/d2
158 * mv /a/b/c /a/b/c2/d2/cc
160 * "c" has lower inode number, but we can't move it (2nd mv operation)
161 * before we move "d", which has higher inode number.
163 * So we just memorize which move/rename operations must be performed
164 * later when their respective parent is processed and moved/renamed.
167 /* Indexed by parent directory inode number. */
168 struct rb_root pending_dir_moves;
171 * Reverse index, indexed by the inode number of a directory that
172 * is waiting for the move/rename of its immediate parent before its
173 * own move/rename can be performed.
175 struct rb_root waiting_dir_moves;
178 struct pending_dir_move {
180 struct list_head list;
184 struct list_head update_refs;
187 struct waiting_dir_move {
192 struct name_cache_entry {
193 struct list_head list;
195 * radix_tree has only 32bit entries but we need to handle 64bit inums.
196 * We use the lower 32bit of the 64bit inum to store it in the tree. If
197 * more then one inum would fall into the same entry, we use radix_list
198 * to store the additional entries. radix_list is also used to store
199 * entries where two entries have the same inum but different
202 struct list_head radix_list;
208 int need_later_update;
213 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
215 static int need_send_hole(struct send_ctx *sctx)
217 return (sctx->parent_root && !sctx->cur_inode_new &&
218 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
219 S_ISREG(sctx->cur_inode_mode));
222 static void fs_path_reset(struct fs_path *p)
225 p->start = p->buf + p->buf_len - 1;
235 static struct fs_path *fs_path_alloc(void)
239 p = kmalloc(sizeof(*p), GFP_NOFS);
243 p->buf = p->inline_buf;
244 p->buf_len = FS_PATH_INLINE_SIZE;
249 static struct fs_path *fs_path_alloc_reversed(void)
261 static void fs_path_free(struct fs_path *p)
265 if (p->buf != p->inline_buf) {
266 if (is_vmalloc_addr(p->buf))
274 static int fs_path_len(struct fs_path *p)
276 return p->end - p->start;
279 static int fs_path_ensure_buf(struct fs_path *p, int len)
287 if (p->buf_len >= len)
290 path_len = p->end - p->start;
291 old_buf_len = p->buf_len;
292 len = PAGE_ALIGN(len);
294 if (p->buf == p->inline_buf) {
295 tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN);
297 tmp_buf = vmalloc(len);
301 memcpy(tmp_buf, p->buf, p->buf_len);
305 if (is_vmalloc_addr(p->buf)) {
306 tmp_buf = vmalloc(len);
309 memcpy(tmp_buf, p->buf, p->buf_len);
312 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
314 tmp_buf = vmalloc(len);
317 memcpy(tmp_buf, p->buf, p->buf_len);
325 tmp_buf = p->buf + old_buf_len - path_len - 1;
326 p->end = p->buf + p->buf_len - 1;
327 p->start = p->end - path_len;
328 memmove(p->start, tmp_buf, path_len + 1);
331 p->end = p->start + path_len;
336 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
342 new_len = p->end - p->start + name_len;
343 if (p->start != p->end)
345 ret = fs_path_ensure_buf(p, new_len);
350 if (p->start != p->end)
352 p->start -= name_len;
353 *prepared = p->start;
355 if (p->start != p->end)
366 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
371 ret = fs_path_prepare_for_add(p, name_len, &prepared);
374 memcpy(prepared, name, name_len);
380 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
385 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
388 memcpy(prepared, p2->start, p2->end - p2->start);
394 static int fs_path_add_from_extent_buffer(struct fs_path *p,
395 struct extent_buffer *eb,
396 unsigned long off, int len)
401 ret = fs_path_prepare_for_add(p, len, &prepared);
405 read_extent_buffer(eb, prepared, off, len);
411 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
415 p->reversed = from->reversed;
418 ret = fs_path_add_path(p, from);
424 static void fs_path_unreverse(struct fs_path *p)
433 len = p->end - p->start;
435 p->end = p->start + len;
436 memmove(p->start, tmp, len + 1);
440 static struct btrfs_path *alloc_path_for_send(void)
442 struct btrfs_path *path;
444 path = btrfs_alloc_path();
447 path->search_commit_root = 1;
448 path->skip_locking = 1;
452 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
462 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
463 /* TODO handle that correctly */
464 /*if (ret == -ERESTARTSYS) {
483 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
485 struct btrfs_tlv_header *hdr;
486 int total_len = sizeof(*hdr) + len;
487 int left = sctx->send_max_size - sctx->send_size;
489 if (unlikely(left < total_len))
492 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
493 hdr->tlv_type = cpu_to_le16(attr);
494 hdr->tlv_len = cpu_to_le16(len);
495 memcpy(hdr + 1, data, len);
496 sctx->send_size += total_len;
501 #define TLV_PUT_DEFINE_INT(bits) \
502 static int tlv_put_u##bits(struct send_ctx *sctx, \
503 u##bits attr, u##bits value) \
505 __le##bits __tmp = cpu_to_le##bits(value); \
506 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
509 TLV_PUT_DEFINE_INT(64)
511 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
512 const char *str, int len)
516 return tlv_put(sctx, attr, str, len);
519 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
522 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
525 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
526 struct extent_buffer *eb,
527 struct btrfs_timespec *ts)
529 struct btrfs_timespec bts;
530 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
531 return tlv_put(sctx, attr, &bts, sizeof(bts));
535 #define TLV_PUT(sctx, attrtype, attrlen, data) \
537 ret = tlv_put(sctx, attrtype, attrlen, data); \
539 goto tlv_put_failure; \
542 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
544 ret = tlv_put_u##bits(sctx, attrtype, value); \
546 goto tlv_put_failure; \
549 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
550 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
551 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
552 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
553 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
555 ret = tlv_put_string(sctx, attrtype, str, len); \
557 goto tlv_put_failure; \
559 #define TLV_PUT_PATH(sctx, attrtype, p) \
561 ret = tlv_put_string(sctx, attrtype, p->start, \
562 p->end - p->start); \
564 goto tlv_put_failure; \
566 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
568 ret = tlv_put_uuid(sctx, attrtype, uuid); \
570 goto tlv_put_failure; \
572 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
574 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
576 goto tlv_put_failure; \
579 static int send_header(struct send_ctx *sctx)
581 struct btrfs_stream_header hdr;
583 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
584 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
586 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
591 * For each command/item we want to send to userspace, we call this function.
593 static int begin_cmd(struct send_ctx *sctx, int cmd)
595 struct btrfs_cmd_header *hdr;
597 if (WARN_ON(!sctx->send_buf))
600 BUG_ON(sctx->send_size);
602 sctx->send_size += sizeof(*hdr);
603 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
604 hdr->cmd = cpu_to_le16(cmd);
609 static int send_cmd(struct send_ctx *sctx)
612 struct btrfs_cmd_header *hdr;
615 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
616 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
619 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
620 hdr->crc = cpu_to_le32(crc);
622 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
625 sctx->total_send_size += sctx->send_size;
626 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
633 * Sends a move instruction to user space
635 static int send_rename(struct send_ctx *sctx,
636 struct fs_path *from, struct fs_path *to)
640 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
642 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
646 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
647 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
649 ret = send_cmd(sctx);
657 * Sends a link instruction to user space
659 static int send_link(struct send_ctx *sctx,
660 struct fs_path *path, struct fs_path *lnk)
664 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
666 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
670 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
671 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
673 ret = send_cmd(sctx);
681 * Sends an unlink instruction to user space
683 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
687 verbose_printk("btrfs: send_unlink %s\n", path->start);
689 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
693 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
695 ret = send_cmd(sctx);
703 * Sends a rmdir instruction to user space
705 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
709 verbose_printk("btrfs: send_rmdir %s\n", path->start);
711 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
715 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
717 ret = send_cmd(sctx);
725 * Helper function to retrieve some fields from an inode item.
727 static int get_inode_info(struct btrfs_root *root,
728 u64 ino, u64 *size, u64 *gen,
729 u64 *mode, u64 *uid, u64 *gid,
733 struct btrfs_inode_item *ii;
734 struct btrfs_key key;
735 struct btrfs_path *path;
737 path = alloc_path_for_send();
742 key.type = BTRFS_INODE_ITEM_KEY;
744 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
752 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
753 struct btrfs_inode_item);
755 *size = btrfs_inode_size(path->nodes[0], ii);
757 *gen = btrfs_inode_generation(path->nodes[0], ii);
759 *mode = btrfs_inode_mode(path->nodes[0], ii);
761 *uid = btrfs_inode_uid(path->nodes[0], ii);
763 *gid = btrfs_inode_gid(path->nodes[0], ii);
765 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
768 btrfs_free_path(path);
772 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
777 * Helper function to iterate the entries in ONE btrfs_inode_ref or
778 * btrfs_inode_extref.
779 * The iterate callback may return a non zero value to stop iteration. This can
780 * be a negative value for error codes or 1 to simply stop it.
782 * path must point to the INODE_REF or INODE_EXTREF when called.
784 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
785 struct btrfs_key *found_key, int resolve,
786 iterate_inode_ref_t iterate, void *ctx)
788 struct extent_buffer *eb = path->nodes[0];
789 struct btrfs_item *item;
790 struct btrfs_inode_ref *iref;
791 struct btrfs_inode_extref *extref;
792 struct btrfs_path *tmp_path;
796 int slot = path->slots[0];
803 unsigned long name_off;
804 unsigned long elem_size;
807 p = fs_path_alloc_reversed();
811 tmp_path = alloc_path_for_send();
818 if (found_key->type == BTRFS_INODE_REF_KEY) {
819 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
820 struct btrfs_inode_ref);
821 item = btrfs_item_nr(slot);
822 total = btrfs_item_size(eb, item);
823 elem_size = sizeof(*iref);
825 ptr = btrfs_item_ptr_offset(eb, slot);
826 total = btrfs_item_size_nr(eb, slot);
827 elem_size = sizeof(*extref);
830 while (cur < total) {
833 if (found_key->type == BTRFS_INODE_REF_KEY) {
834 iref = (struct btrfs_inode_ref *)(ptr + cur);
835 name_len = btrfs_inode_ref_name_len(eb, iref);
836 name_off = (unsigned long)(iref + 1);
837 index = btrfs_inode_ref_index(eb, iref);
838 dir = found_key->offset;
840 extref = (struct btrfs_inode_extref *)(ptr + cur);
841 name_len = btrfs_inode_extref_name_len(eb, extref);
842 name_off = (unsigned long)&extref->name;
843 index = btrfs_inode_extref_index(eb, extref);
844 dir = btrfs_inode_extref_parent(eb, extref);
848 start = btrfs_ref_to_path(root, tmp_path, name_len,
852 ret = PTR_ERR(start);
855 if (start < p->buf) {
856 /* overflow , try again with larger buffer */
857 ret = fs_path_ensure_buf(p,
858 p->buf_len + p->buf - start);
861 start = btrfs_ref_to_path(root, tmp_path,
866 ret = PTR_ERR(start);
869 BUG_ON(start < p->buf);
873 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
879 cur += elem_size + name_len;
880 ret = iterate(num, dir, index, p, ctx);
887 btrfs_free_path(tmp_path);
892 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
893 const char *name, int name_len,
894 const char *data, int data_len,
898 * Helper function to iterate the entries in ONE btrfs_dir_item.
899 * The iterate callback may return a non zero value to stop iteration. This can
900 * be a negative value for error codes or 1 to simply stop it.
902 * path must point to the dir item when called.
904 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
905 struct btrfs_key *found_key,
906 iterate_dir_item_t iterate, void *ctx)
909 struct extent_buffer *eb;
910 struct btrfs_item *item;
911 struct btrfs_dir_item *di;
912 struct btrfs_key di_key;
927 buf = kmalloc(buf_len, GFP_NOFS);
934 slot = path->slots[0];
935 item = btrfs_item_nr(slot);
936 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
939 total = btrfs_item_size(eb, item);
942 while (cur < total) {
943 name_len = btrfs_dir_name_len(eb, di);
944 data_len = btrfs_dir_data_len(eb, di);
945 type = btrfs_dir_type(eb, di);
946 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
948 if (name_len + data_len > buf_len) {
949 buf_len = PAGE_ALIGN(name_len + data_len);
951 buf2 = vmalloc(buf_len);
958 buf2 = krealloc(buf, buf_len, GFP_NOFS);
960 buf2 = vmalloc(buf_len);
974 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
975 name_len + data_len);
977 len = sizeof(*di) + name_len + data_len;
978 di = (struct btrfs_dir_item *)((char *)di + len);
981 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
982 data_len, type, ctx);
1001 static int __copy_first_ref(int num, u64 dir, int index,
1002 struct fs_path *p, void *ctx)
1005 struct fs_path *pt = ctx;
1007 ret = fs_path_copy(pt, p);
1011 /* we want the first only */
1016 * Retrieve the first path of an inode. If an inode has more then one
1017 * ref/hardlink, this is ignored.
1019 static int get_inode_path(struct btrfs_root *root,
1020 u64 ino, struct fs_path *path)
1023 struct btrfs_key key, found_key;
1024 struct btrfs_path *p;
1026 p = alloc_path_for_send();
1030 fs_path_reset(path);
1033 key.type = BTRFS_INODE_REF_KEY;
1036 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1043 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1044 if (found_key.objectid != ino ||
1045 (found_key.type != BTRFS_INODE_REF_KEY &&
1046 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1051 ret = iterate_inode_ref(root, p, &found_key, 1,
1052 __copy_first_ref, path);
1062 struct backref_ctx {
1063 struct send_ctx *sctx;
1065 /* number of total found references */
1069 * used for clones found in send_root. clones found behind cur_objectid
1070 * and cur_offset are not considered as allowed clones.
1075 /* may be truncated in case it's the last extent in a file */
1078 /* Just to check for bugs in backref resolving */
1082 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1084 u64 root = (u64)(uintptr_t)key;
1085 struct clone_root *cr = (struct clone_root *)elt;
1087 if (root < cr->root->objectid)
1089 if (root > cr->root->objectid)
1094 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1096 struct clone_root *cr1 = (struct clone_root *)e1;
1097 struct clone_root *cr2 = (struct clone_root *)e2;
1099 if (cr1->root->objectid < cr2->root->objectid)
1101 if (cr1->root->objectid > cr2->root->objectid)
1107 * Called for every backref that is found for the current extent.
1108 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1110 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1112 struct backref_ctx *bctx = ctx_;
1113 struct clone_root *found;
1117 /* First check if the root is in the list of accepted clone sources */
1118 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1119 bctx->sctx->clone_roots_cnt,
1120 sizeof(struct clone_root),
1121 __clone_root_cmp_bsearch);
1125 if (found->root == bctx->sctx->send_root &&
1126 ino == bctx->cur_objectid &&
1127 offset == bctx->cur_offset) {
1128 bctx->found_itself = 1;
1132 * There are inodes that have extents that lie behind its i_size. Don't
1133 * accept clones from these extents.
1135 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1140 if (offset + bctx->extent_len > i_size)
1144 * Make sure we don't consider clones from send_root that are
1145 * behind the current inode/offset.
1147 if (found->root == bctx->sctx->send_root) {
1149 * TODO for the moment we don't accept clones from the inode
1150 * that is currently send. We may change this when
1151 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1154 if (ino >= bctx->cur_objectid)
1157 if (ino > bctx->cur_objectid)
1159 if (offset + bctx->extent_len > bctx->cur_offset)
1165 found->found_refs++;
1166 if (ino < found->ino) {
1168 found->offset = offset;
1169 } else if (found->ino == ino) {
1171 * same extent found more then once in the same file.
1173 if (found->offset > offset + bctx->extent_len)
1174 found->offset = offset;
1181 * Given an inode, offset and extent item, it finds a good clone for a clone
1182 * instruction. Returns -ENOENT when none could be found. The function makes
1183 * sure that the returned clone is usable at the point where sending is at the
1184 * moment. This means, that no clones are accepted which lie behind the current
1187 * path must point to the extent item when called.
1189 static int find_extent_clone(struct send_ctx *sctx,
1190 struct btrfs_path *path,
1191 u64 ino, u64 data_offset,
1193 struct clone_root **found)
1200 u64 extent_item_pos;
1202 struct btrfs_file_extent_item *fi;
1203 struct extent_buffer *eb = path->nodes[0];
1204 struct backref_ctx *backref_ctx = NULL;
1205 struct clone_root *cur_clone_root;
1206 struct btrfs_key found_key;
1207 struct btrfs_path *tmp_path;
1211 tmp_path = alloc_path_for_send();
1215 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1221 if (data_offset >= ino_size) {
1223 * There may be extents that lie behind the file's size.
1224 * I at least had this in combination with snapshotting while
1225 * writing large files.
1231 fi = btrfs_item_ptr(eb, path->slots[0],
1232 struct btrfs_file_extent_item);
1233 extent_type = btrfs_file_extent_type(eb, fi);
1234 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1238 compressed = btrfs_file_extent_compression(eb, fi);
1240 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1241 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1242 if (disk_byte == 0) {
1246 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1248 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1249 &found_key, &flags);
1250 btrfs_release_path(tmp_path);
1254 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1260 * Setup the clone roots.
1262 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1263 cur_clone_root = sctx->clone_roots + i;
1264 cur_clone_root->ino = (u64)-1;
1265 cur_clone_root->offset = 0;
1266 cur_clone_root->found_refs = 0;
1269 backref_ctx->sctx = sctx;
1270 backref_ctx->found = 0;
1271 backref_ctx->cur_objectid = ino;
1272 backref_ctx->cur_offset = data_offset;
1273 backref_ctx->found_itself = 0;
1274 backref_ctx->extent_len = num_bytes;
1277 * The last extent of a file may be too large due to page alignment.
1278 * We need to adjust extent_len in this case so that the checks in
1279 * __iterate_backrefs work.
1281 if (data_offset + num_bytes >= ino_size)
1282 backref_ctx->extent_len = ino_size - data_offset;
1285 * Now collect all backrefs.
1287 if (compressed == BTRFS_COMPRESS_NONE)
1288 extent_item_pos = logical - found_key.objectid;
1290 extent_item_pos = 0;
1292 extent_item_pos = logical - found_key.objectid;
1293 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1294 found_key.objectid, extent_item_pos, 1,
1295 __iterate_backrefs, backref_ctx);
1300 if (!backref_ctx->found_itself) {
1301 /* found a bug in backref code? */
1303 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
1304 "send_root. inode=%llu, offset=%llu, "
1305 "disk_byte=%llu found extent=%llu\n",
1306 ino, data_offset, disk_byte, found_key.objectid);
1310 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1312 "num_bytes=%llu, logical=%llu\n",
1313 data_offset, ino, num_bytes, logical);
1315 if (!backref_ctx->found)
1316 verbose_printk("btrfs: no clones found\n");
1318 cur_clone_root = NULL;
1319 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1320 if (sctx->clone_roots[i].found_refs) {
1321 if (!cur_clone_root)
1322 cur_clone_root = sctx->clone_roots + i;
1323 else if (sctx->clone_roots[i].root == sctx->send_root)
1324 /* prefer clones from send_root over others */
1325 cur_clone_root = sctx->clone_roots + i;
1330 if (cur_clone_root) {
1331 if (compressed != BTRFS_COMPRESS_NONE) {
1333 * Offsets given by iterate_extent_inodes() are relative
1334 * to the start of the extent, we need to add logical
1335 * offset from the file extent item.
1336 * (See why at backref.c:check_extent_in_eb())
1338 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1341 *found = cur_clone_root;
1348 btrfs_free_path(tmp_path);
1353 static int read_symlink(struct btrfs_root *root,
1355 struct fs_path *dest)
1358 struct btrfs_path *path;
1359 struct btrfs_key key;
1360 struct btrfs_file_extent_item *ei;
1366 path = alloc_path_for_send();
1371 key.type = BTRFS_EXTENT_DATA_KEY;
1373 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1378 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1379 struct btrfs_file_extent_item);
1380 type = btrfs_file_extent_type(path->nodes[0], ei);
1381 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1382 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1383 BUG_ON(compression);
1385 off = btrfs_file_extent_inline_start(ei);
1386 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
1388 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1391 btrfs_free_path(path);
1396 * Helper function to generate a file name that is unique in the root of
1397 * send_root and parent_root. This is used to generate names for orphan inodes.
1399 static int gen_unique_name(struct send_ctx *sctx,
1401 struct fs_path *dest)
1404 struct btrfs_path *path;
1405 struct btrfs_dir_item *di;
1410 path = alloc_path_for_send();
1415 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1417 ASSERT(len < sizeof(tmp));
1419 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1420 path, BTRFS_FIRST_FREE_OBJECTID,
1421 tmp, strlen(tmp), 0);
1422 btrfs_release_path(path);
1428 /* not unique, try again */
1433 if (!sctx->parent_root) {
1439 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1440 path, BTRFS_FIRST_FREE_OBJECTID,
1441 tmp, strlen(tmp), 0);
1442 btrfs_release_path(path);
1448 /* not unique, try again */
1456 ret = fs_path_add(dest, tmp, strlen(tmp));
1459 btrfs_free_path(path);
1464 inode_state_no_change,
1465 inode_state_will_create,
1466 inode_state_did_create,
1467 inode_state_will_delete,
1468 inode_state_did_delete,
1471 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1479 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1481 if (ret < 0 && ret != -ENOENT)
1485 if (!sctx->parent_root) {
1486 right_ret = -ENOENT;
1488 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1489 NULL, NULL, NULL, NULL);
1490 if (ret < 0 && ret != -ENOENT)
1495 if (!left_ret && !right_ret) {
1496 if (left_gen == gen && right_gen == gen) {
1497 ret = inode_state_no_change;
1498 } else if (left_gen == gen) {
1499 if (ino < sctx->send_progress)
1500 ret = inode_state_did_create;
1502 ret = inode_state_will_create;
1503 } else if (right_gen == gen) {
1504 if (ino < sctx->send_progress)
1505 ret = inode_state_did_delete;
1507 ret = inode_state_will_delete;
1511 } else if (!left_ret) {
1512 if (left_gen == gen) {
1513 if (ino < sctx->send_progress)
1514 ret = inode_state_did_create;
1516 ret = inode_state_will_create;
1520 } else if (!right_ret) {
1521 if (right_gen == gen) {
1522 if (ino < sctx->send_progress)
1523 ret = inode_state_did_delete;
1525 ret = inode_state_will_delete;
1537 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1541 ret = get_cur_inode_state(sctx, ino, gen);
1545 if (ret == inode_state_no_change ||
1546 ret == inode_state_did_create ||
1547 ret == inode_state_will_delete)
1557 * Helper function to lookup a dir item in a dir.
1559 static int lookup_dir_item_inode(struct btrfs_root *root,
1560 u64 dir, const char *name, int name_len,
1565 struct btrfs_dir_item *di;
1566 struct btrfs_key key;
1567 struct btrfs_path *path;
1569 path = alloc_path_for_send();
1573 di = btrfs_lookup_dir_item(NULL, root, path,
1574 dir, name, name_len, 0);
1583 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1584 *found_inode = key.objectid;
1585 *found_type = btrfs_dir_type(path->nodes[0], di);
1588 btrfs_free_path(path);
1593 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1594 * generation of the parent dir and the name of the dir entry.
1596 static int get_first_ref(struct btrfs_root *root, u64 ino,
1597 u64 *dir, u64 *dir_gen, struct fs_path *name)
1600 struct btrfs_key key;
1601 struct btrfs_key found_key;
1602 struct btrfs_path *path;
1606 path = alloc_path_for_send();
1611 key.type = BTRFS_INODE_REF_KEY;
1614 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1618 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1620 if (ret || found_key.objectid != ino ||
1621 (found_key.type != BTRFS_INODE_REF_KEY &&
1622 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1627 if (key.type == BTRFS_INODE_REF_KEY) {
1628 struct btrfs_inode_ref *iref;
1629 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1630 struct btrfs_inode_ref);
1631 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1632 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1633 (unsigned long)(iref + 1),
1635 parent_dir = found_key.offset;
1637 struct btrfs_inode_extref *extref;
1638 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1639 struct btrfs_inode_extref);
1640 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1641 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1642 (unsigned long)&extref->name, len);
1643 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1647 btrfs_release_path(path);
1649 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
1657 btrfs_free_path(path);
1661 static int is_first_ref(struct btrfs_root *root,
1663 const char *name, int name_len)
1666 struct fs_path *tmp_name;
1670 tmp_name = fs_path_alloc();
1674 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1678 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1683 ret = !memcmp(tmp_name->start, name, name_len);
1686 fs_path_free(tmp_name);
1691 * Used by process_recorded_refs to determine if a new ref would overwrite an
1692 * already existing ref. In case it detects an overwrite, it returns the
1693 * inode/gen in who_ino/who_gen.
1694 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1695 * to make sure later references to the overwritten inode are possible.
1696 * Orphanizing is however only required for the first ref of an inode.
1697 * process_recorded_refs does an additional is_first_ref check to see if
1698 * orphanizing is really required.
1700 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1701 const char *name, int name_len,
1702 u64 *who_ino, u64 *who_gen)
1706 u64 other_inode = 0;
1709 if (!sctx->parent_root)
1712 ret = is_inode_existent(sctx, dir, dir_gen);
1717 * If we have a parent root we need to verify that the parent dir was
1718 * not delted and then re-created, if it was then we have no overwrite
1719 * and we can just unlink this entry.
1721 if (sctx->parent_root) {
1722 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1724 if (ret < 0 && ret != -ENOENT)
1734 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1735 &other_inode, &other_type);
1736 if (ret < 0 && ret != -ENOENT)
1744 * Check if the overwritten ref was already processed. If yes, the ref
1745 * was already unlinked/moved, so we can safely assume that we will not
1746 * overwrite anything at this point in time.
1748 if (other_inode > sctx->send_progress) {
1749 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1750 who_gen, NULL, NULL, NULL, NULL);
1755 *who_ino = other_inode;
1765 * Checks if the ref was overwritten by an already processed inode. This is
1766 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1767 * thus the orphan name needs be used.
1768 * process_recorded_refs also uses it to avoid unlinking of refs that were
1771 static int did_overwrite_ref(struct send_ctx *sctx,
1772 u64 dir, u64 dir_gen,
1773 u64 ino, u64 ino_gen,
1774 const char *name, int name_len)
1781 if (!sctx->parent_root)
1784 ret = is_inode_existent(sctx, dir, dir_gen);
1788 /* check if the ref was overwritten by another ref */
1789 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1790 &ow_inode, &other_type);
1791 if (ret < 0 && ret != -ENOENT)
1794 /* was never and will never be overwritten */
1799 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1804 if (ow_inode == ino && gen == ino_gen) {
1809 /* we know that it is or will be overwritten. check this now */
1810 if (ow_inode < sctx->send_progress)
1820 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1821 * that got overwritten. This is used by process_recorded_refs to determine
1822 * if it has to use the path as returned by get_cur_path or the orphan name.
1824 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1827 struct fs_path *name = NULL;
1831 if (!sctx->parent_root)
1834 name = fs_path_alloc();
1838 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1842 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1843 name->start, fs_path_len(name));
1851 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1852 * so we need to do some special handling in case we have clashes. This function
1853 * takes care of this with the help of name_cache_entry::radix_list.
1854 * In case of error, nce is kfreed.
1856 static int name_cache_insert(struct send_ctx *sctx,
1857 struct name_cache_entry *nce)
1860 struct list_head *nce_head;
1862 nce_head = radix_tree_lookup(&sctx->name_cache,
1863 (unsigned long)nce->ino);
1865 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1870 INIT_LIST_HEAD(nce_head);
1872 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1879 list_add_tail(&nce->radix_list, nce_head);
1880 list_add_tail(&nce->list, &sctx->name_cache_list);
1881 sctx->name_cache_size++;
1886 static void name_cache_delete(struct send_ctx *sctx,
1887 struct name_cache_entry *nce)
1889 struct list_head *nce_head;
1891 nce_head = radix_tree_lookup(&sctx->name_cache,
1892 (unsigned long)nce->ino);
1895 list_del(&nce->radix_list);
1896 list_del(&nce->list);
1897 sctx->name_cache_size--;
1899 if (list_empty(nce_head)) {
1900 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1905 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1908 struct list_head *nce_head;
1909 struct name_cache_entry *cur;
1911 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1915 list_for_each_entry(cur, nce_head, radix_list) {
1916 if (cur->ino == ino && cur->gen == gen)
1923 * Removes the entry from the list and adds it back to the end. This marks the
1924 * entry as recently used so that name_cache_clean_unused does not remove it.
1926 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1928 list_del(&nce->list);
1929 list_add_tail(&nce->list, &sctx->name_cache_list);
1933 * Remove some entries from the beginning of name_cache_list.
1935 static void name_cache_clean_unused(struct send_ctx *sctx)
1937 struct name_cache_entry *nce;
1939 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1942 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1943 nce = list_entry(sctx->name_cache_list.next,
1944 struct name_cache_entry, list);
1945 name_cache_delete(sctx, nce);
1950 static void name_cache_free(struct send_ctx *sctx)
1952 struct name_cache_entry *nce;
1954 while (!list_empty(&sctx->name_cache_list)) {
1955 nce = list_entry(sctx->name_cache_list.next,
1956 struct name_cache_entry, list);
1957 name_cache_delete(sctx, nce);
1963 * Used by get_cur_path for each ref up to the root.
1964 * Returns 0 if it succeeded.
1965 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1966 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1967 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1968 * Returns <0 in case of error.
1970 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1972 int skip_name_cache,
1975 struct fs_path *dest)
1979 struct btrfs_path *path = NULL;
1980 struct name_cache_entry *nce = NULL;
1982 if (skip_name_cache)
1985 * First check if we already did a call to this function with the same
1986 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1987 * return the cached result.
1989 nce = name_cache_search(sctx, ino, gen);
1991 if (ino < sctx->send_progress && nce->need_later_update) {
1992 name_cache_delete(sctx, nce);
1996 name_cache_used(sctx, nce);
1997 *parent_ino = nce->parent_ino;
1998 *parent_gen = nce->parent_gen;
1999 ret = fs_path_add(dest, nce->name, nce->name_len);
2007 path = alloc_path_for_send();
2012 * If the inode is not existent yet, add the orphan name and return 1.
2013 * This should only happen for the parent dir that we determine in
2016 ret = is_inode_existent(sctx, ino, gen);
2021 ret = gen_unique_name(sctx, ino, gen, dest);
2030 * Depending on whether the inode was already processed or not, use
2031 * send_root or parent_root for ref lookup.
2033 if (ino < sctx->send_progress && !skip_name_cache)
2034 ret = get_first_ref(sctx->send_root, ino,
2035 parent_ino, parent_gen, dest);
2037 ret = get_first_ref(sctx->parent_root, ino,
2038 parent_ino, parent_gen, dest);
2043 * Check if the ref was overwritten by an inode's ref that was processed
2044 * earlier. If yes, treat as orphan and return 1.
2046 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2047 dest->start, dest->end - dest->start);
2051 fs_path_reset(dest);
2052 ret = gen_unique_name(sctx, ino, gen, dest);
2057 if (skip_name_cache)
2062 * Store the result of the lookup in the name cache.
2064 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2072 nce->parent_ino = *parent_ino;
2073 nce->parent_gen = *parent_gen;
2074 nce->name_len = fs_path_len(dest);
2076 strcpy(nce->name, dest->start);
2078 if (ino < sctx->send_progress)
2079 nce->need_later_update = 0;
2081 nce->need_later_update = 1;
2083 nce_ret = name_cache_insert(sctx, nce);
2086 name_cache_clean_unused(sctx);
2089 btrfs_free_path(path);
2094 * Magic happens here. This function returns the first ref to an inode as it
2095 * would look like while receiving the stream at this point in time.
2096 * We walk the path up to the root. For every inode in between, we check if it
2097 * was already processed/sent. If yes, we continue with the parent as found
2098 * in send_root. If not, we continue with the parent as found in parent_root.
2099 * If we encounter an inode that was deleted at this point in time, we use the
2100 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2101 * that were not created yet and overwritten inodes/refs.
2103 * When do we have have orphan inodes:
2104 * 1. When an inode is freshly created and thus no valid refs are available yet
2105 * 2. When a directory lost all it's refs (deleted) but still has dir items
2106 * inside which were not processed yet (pending for move/delete). If anyone
2107 * tried to get the path to the dir items, it would get a path inside that
2109 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2110 * of an unprocessed inode. If in that case the first ref would be
2111 * overwritten, the overwritten inode gets "orphanized". Later when we
2112 * process this overwritten inode, it is restored at a new place by moving
2115 * sctx->send_progress tells this function at which point in time receiving
2118 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2119 struct fs_path *dest)
2122 struct fs_path *name = NULL;
2123 u64 parent_inode = 0;
2126 int skip_name_cache = 0;
2128 name = fs_path_alloc();
2134 if (is_waiting_for_move(sctx, ino))
2135 skip_name_cache = 1;
2138 fs_path_reset(dest);
2140 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2141 fs_path_reset(name);
2143 ret = __get_cur_name_and_parent(sctx, ino, gen, skip_name_cache,
2144 &parent_inode, &parent_gen, name);
2150 if (!skip_name_cache &&
2151 is_waiting_for_move(sctx, parent_inode))
2152 skip_name_cache = 1;
2154 ret = fs_path_add_path(dest, name);
2165 fs_path_unreverse(dest);
2170 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2172 static int send_subvol_begin(struct send_ctx *sctx)
2175 struct btrfs_root *send_root = sctx->send_root;
2176 struct btrfs_root *parent_root = sctx->parent_root;
2177 struct btrfs_path *path;
2178 struct btrfs_key key;
2179 struct btrfs_root_ref *ref;
2180 struct extent_buffer *leaf;
2184 path = btrfs_alloc_path();
2188 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2190 btrfs_free_path(path);
2194 key.objectid = send_root->objectid;
2195 key.type = BTRFS_ROOT_BACKREF_KEY;
2198 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2207 leaf = path->nodes[0];
2208 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2209 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2210 key.objectid != send_root->objectid) {
2214 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2215 namelen = btrfs_root_ref_name_len(leaf, ref);
2216 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2217 btrfs_release_path(path);
2220 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2224 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2229 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2230 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2231 sctx->send_root->root_item.uuid);
2232 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2233 le64_to_cpu(sctx->send_root->root_item.ctransid));
2235 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2236 sctx->parent_root->root_item.uuid);
2237 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2238 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2241 ret = send_cmd(sctx);
2245 btrfs_free_path(path);
2250 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2255 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2257 p = fs_path_alloc();
2261 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2265 ret = get_cur_path(sctx, ino, gen, p);
2268 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2269 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2271 ret = send_cmd(sctx);
2279 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2284 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2286 p = fs_path_alloc();
2290 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2294 ret = get_cur_path(sctx, ino, gen, p);
2297 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2298 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2300 ret = send_cmd(sctx);
2308 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2313 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2315 p = fs_path_alloc();
2319 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2323 ret = get_cur_path(sctx, ino, gen, p);
2326 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2327 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2328 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2330 ret = send_cmd(sctx);
2338 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2341 struct fs_path *p = NULL;
2342 struct btrfs_inode_item *ii;
2343 struct btrfs_path *path = NULL;
2344 struct extent_buffer *eb;
2345 struct btrfs_key key;
2348 verbose_printk("btrfs: send_utimes %llu\n", ino);
2350 p = fs_path_alloc();
2354 path = alloc_path_for_send();
2361 key.type = BTRFS_INODE_ITEM_KEY;
2363 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2367 eb = path->nodes[0];
2368 slot = path->slots[0];
2369 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2371 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2375 ret = get_cur_path(sctx, ino, gen, p);
2378 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2379 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2380 btrfs_inode_atime(ii));
2381 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2382 btrfs_inode_mtime(ii));
2383 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2384 btrfs_inode_ctime(ii));
2385 /* TODO Add otime support when the otime patches get into upstream */
2387 ret = send_cmd(sctx);
2392 btrfs_free_path(path);
2397 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2398 * a valid path yet because we did not process the refs yet. So, the inode
2399 * is created as orphan.
2401 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2410 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2412 p = fs_path_alloc();
2416 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2421 if (S_ISREG(mode)) {
2422 cmd = BTRFS_SEND_C_MKFILE;
2423 } else if (S_ISDIR(mode)) {
2424 cmd = BTRFS_SEND_C_MKDIR;
2425 } else if (S_ISLNK(mode)) {
2426 cmd = BTRFS_SEND_C_SYMLINK;
2427 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2428 cmd = BTRFS_SEND_C_MKNOD;
2429 } else if (S_ISFIFO(mode)) {
2430 cmd = BTRFS_SEND_C_MKFIFO;
2431 } else if (S_ISSOCK(mode)) {
2432 cmd = BTRFS_SEND_C_MKSOCK;
2434 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2435 (int)(mode & S_IFMT));
2440 ret = begin_cmd(sctx, cmd);
2444 ret = gen_unique_name(sctx, ino, gen, p);
2448 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2449 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2451 if (S_ISLNK(mode)) {
2453 ret = read_symlink(sctx->send_root, ino, p);
2456 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2457 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2458 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2459 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2460 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2463 ret = send_cmd(sctx);
2475 * We need some special handling for inodes that get processed before the parent
2476 * directory got created. See process_recorded_refs for details.
2477 * This function does the check if we already created the dir out of order.
2479 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2482 struct btrfs_path *path = NULL;
2483 struct btrfs_key key;
2484 struct btrfs_key found_key;
2485 struct btrfs_key di_key;
2486 struct extent_buffer *eb;
2487 struct btrfs_dir_item *di;
2490 path = alloc_path_for_send();
2497 key.type = BTRFS_DIR_INDEX_KEY;
2500 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2505 eb = path->nodes[0];
2506 slot = path->slots[0];
2507 btrfs_item_key_to_cpu(eb, &found_key, slot);
2509 if (ret || found_key.objectid != key.objectid ||
2510 found_key.type != key.type) {
2515 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2516 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2518 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2519 di_key.objectid < sctx->send_progress) {
2524 key.offset = found_key.offset + 1;
2525 btrfs_release_path(path);
2529 btrfs_free_path(path);
2534 * Only creates the inode if it is:
2535 * 1. Not a directory
2536 * 2. Or a directory which was not created already due to out of order
2537 * directories. See did_create_dir and process_recorded_refs for details.
2539 static int send_create_inode_if_needed(struct send_ctx *sctx)
2543 if (S_ISDIR(sctx->cur_inode_mode)) {
2544 ret = did_create_dir(sctx, sctx->cur_ino);
2553 ret = send_create_inode(sctx, sctx->cur_ino);
2561 struct recorded_ref {
2562 struct list_head list;
2565 struct fs_path *full_path;
2573 * We need to process new refs before deleted refs, but compare_tree gives us
2574 * everything mixed. So we first record all refs and later process them.
2575 * This function is a helper to record one ref.
2577 static int record_ref(struct list_head *head, u64 dir,
2578 u64 dir_gen, struct fs_path *path)
2580 struct recorded_ref *ref;
2582 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2587 ref->dir_gen = dir_gen;
2588 ref->full_path = path;
2590 ref->name = (char *)kbasename(ref->full_path->start);
2591 ref->name_len = ref->full_path->end - ref->name;
2592 ref->dir_path = ref->full_path->start;
2593 if (ref->name == ref->full_path->start)
2594 ref->dir_path_len = 0;
2596 ref->dir_path_len = ref->full_path->end -
2597 ref->full_path->start - 1 - ref->name_len;
2599 list_add_tail(&ref->list, head);
2603 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2605 struct recorded_ref *new;
2607 new = kmalloc(sizeof(*ref), GFP_NOFS);
2611 new->dir = ref->dir;
2612 new->dir_gen = ref->dir_gen;
2613 new->full_path = NULL;
2614 INIT_LIST_HEAD(&new->list);
2615 list_add_tail(&new->list, list);
2619 static void __free_recorded_refs(struct list_head *head)
2621 struct recorded_ref *cur;
2623 while (!list_empty(head)) {
2624 cur = list_entry(head->next, struct recorded_ref, list);
2625 fs_path_free(cur->full_path);
2626 list_del(&cur->list);
2631 static void free_recorded_refs(struct send_ctx *sctx)
2633 __free_recorded_refs(&sctx->new_refs);
2634 __free_recorded_refs(&sctx->deleted_refs);
2638 * Renames/moves a file/dir to its orphan name. Used when the first
2639 * ref of an unprocessed inode gets overwritten and for all non empty
2642 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2643 struct fs_path *path)
2646 struct fs_path *orphan;
2648 orphan = fs_path_alloc();
2652 ret = gen_unique_name(sctx, ino, gen, orphan);
2656 ret = send_rename(sctx, path, orphan);
2659 fs_path_free(orphan);
2664 * Returns 1 if a directory can be removed at this point in time.
2665 * We check this by iterating all dir items and checking if the inode behind
2666 * the dir item was already processed.
2668 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2671 struct btrfs_root *root = sctx->parent_root;
2672 struct btrfs_path *path;
2673 struct btrfs_key key;
2674 struct btrfs_key found_key;
2675 struct btrfs_key loc;
2676 struct btrfs_dir_item *di;
2679 * Don't try to rmdir the top/root subvolume dir.
2681 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2684 path = alloc_path_for_send();
2689 key.type = BTRFS_DIR_INDEX_KEY;
2693 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2697 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2700 if (ret || found_key.objectid != key.objectid ||
2701 found_key.type != key.type) {
2705 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2706 struct btrfs_dir_item);
2707 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2709 if (loc.objectid > send_progress) {
2714 btrfs_release_path(path);
2715 key.offset = found_key.offset + 1;
2721 btrfs_free_path(path);
2725 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2727 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2728 struct waiting_dir_move *entry;
2731 entry = rb_entry(n, struct waiting_dir_move, node);
2732 if (ino < entry->ino)
2734 else if (ino > entry->ino)
2742 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2744 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2745 struct rb_node *parent = NULL;
2746 struct waiting_dir_move *entry, *dm;
2748 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2755 entry = rb_entry(parent, struct waiting_dir_move, node);
2756 if (ino < entry->ino) {
2758 } else if (ino > entry->ino) {
2759 p = &(*p)->rb_right;
2766 rb_link_node(&dm->node, parent, p);
2767 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2771 static int del_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2773 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2774 struct waiting_dir_move *entry;
2777 entry = rb_entry(n, struct waiting_dir_move, node);
2778 if (ino < entry->ino) {
2780 } else if (ino > entry->ino) {
2783 rb_erase(&entry->node, &sctx->waiting_dir_moves);
2791 static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino)
2793 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2794 struct rb_node *parent = NULL;
2795 struct pending_dir_move *entry, *pm;
2796 struct recorded_ref *cur;
2800 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2803 pm->parent_ino = parent_ino;
2804 pm->ino = sctx->cur_ino;
2805 pm->gen = sctx->cur_inode_gen;
2806 INIT_LIST_HEAD(&pm->list);
2807 INIT_LIST_HEAD(&pm->update_refs);
2808 RB_CLEAR_NODE(&pm->node);
2812 entry = rb_entry(parent, struct pending_dir_move, node);
2813 if (parent_ino < entry->parent_ino) {
2815 } else if (parent_ino > entry->parent_ino) {
2816 p = &(*p)->rb_right;
2823 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2824 ret = dup_ref(cur, &pm->update_refs);
2828 list_for_each_entry(cur, &sctx->new_refs, list) {
2829 ret = dup_ref(cur, &pm->update_refs);
2834 ret = add_waiting_dir_move(sctx, pm->ino);
2839 list_add_tail(&pm->list, &entry->list);
2841 rb_link_node(&pm->node, parent, p);
2842 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
2847 __free_recorded_refs(&pm->update_refs);
2853 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
2856 struct rb_node *n = sctx->pending_dir_moves.rb_node;
2857 struct pending_dir_move *entry;
2860 entry = rb_entry(n, struct pending_dir_move, node);
2861 if (parent_ino < entry->parent_ino)
2863 else if (parent_ino > entry->parent_ino)
2871 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
2873 struct fs_path *from_path = NULL;
2874 struct fs_path *to_path = NULL;
2875 u64 orig_progress = sctx->send_progress;
2876 struct recorded_ref *cur;
2879 from_path = fs_path_alloc();
2883 sctx->send_progress = pm->ino;
2884 ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
2888 to_path = fs_path_alloc();
2894 sctx->send_progress = sctx->cur_ino + 1;
2895 ret = del_waiting_dir_move(sctx, pm->ino);
2898 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
2902 ret = send_rename(sctx, from_path, to_path);
2906 ret = send_utimes(sctx, pm->ino, pm->gen);
2911 * After rename/move, need to update the utimes of both new parent(s)
2912 * and old parent(s).
2914 list_for_each_entry(cur, &pm->update_refs, list) {
2915 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
2921 fs_path_free(from_path);
2922 fs_path_free(to_path);
2923 sctx->send_progress = orig_progress;
2928 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
2930 if (!list_empty(&m->list))
2932 if (!RB_EMPTY_NODE(&m->node))
2933 rb_erase(&m->node, &sctx->pending_dir_moves);
2934 __free_recorded_refs(&m->update_refs);
2938 static void tail_append_pending_moves(struct pending_dir_move *moves,
2939 struct list_head *stack)
2941 if (list_empty(&moves->list)) {
2942 list_add_tail(&moves->list, stack);
2945 list_splice_init(&moves->list, &list);
2946 list_add_tail(&moves->list, stack);
2947 list_splice_tail(&list, stack);
2951 static int apply_children_dir_moves(struct send_ctx *sctx)
2953 struct pending_dir_move *pm;
2954 struct list_head stack;
2955 u64 parent_ino = sctx->cur_ino;
2958 pm = get_pending_dir_moves(sctx, parent_ino);
2962 INIT_LIST_HEAD(&stack);
2963 tail_append_pending_moves(pm, &stack);
2965 while (!list_empty(&stack)) {
2966 pm = list_first_entry(&stack, struct pending_dir_move, list);
2967 parent_ino = pm->ino;
2968 ret = apply_dir_move(sctx, pm);
2969 free_pending_move(sctx, pm);
2972 pm = get_pending_dir_moves(sctx, parent_ino);
2974 tail_append_pending_moves(pm, &stack);
2979 while (!list_empty(&stack)) {
2980 pm = list_first_entry(&stack, struct pending_dir_move, list);
2981 free_pending_move(sctx, pm);
2986 static int wait_for_parent_move(struct send_ctx *sctx,
2987 struct recorded_ref *parent_ref)
2990 u64 ino = parent_ref->dir;
2991 u64 parent_ino_before, parent_ino_after;
2992 u64 new_gen, old_gen;
2993 struct fs_path *path_before = NULL;
2994 struct fs_path *path_after = NULL;
2997 if (parent_ref->dir <= sctx->cur_ino)
3000 if (is_waiting_for_move(sctx, ino))
3003 ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
3004 NULL, NULL, NULL, NULL);
3010 ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen,
3011 NULL, NULL, NULL, NULL);
3015 if (new_gen != old_gen)
3018 path_before = fs_path_alloc();
3022 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3024 if (ret == -ENOENT) {
3027 } else if (ret < 0) {
3031 path_after = fs_path_alloc();
3037 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3039 if (ret == -ENOENT) {
3042 } else if (ret < 0) {
3046 len1 = fs_path_len(path_before);
3047 len2 = fs_path_len(path_after);
3048 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3049 memcmp(path_before->start, path_after->start, len1)) {
3056 fs_path_free(path_before);
3057 fs_path_free(path_after);
3063 * This does all the move/link/unlink/rmdir magic.
3065 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3068 struct recorded_ref *cur;
3069 struct recorded_ref *cur2;
3070 struct list_head check_dirs;
3071 struct fs_path *valid_path = NULL;
3074 int did_overwrite = 0;
3077 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3080 * This should never happen as the root dir always has the same ref
3081 * which is always '..'
3083 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3084 INIT_LIST_HEAD(&check_dirs);
3086 valid_path = fs_path_alloc();
3093 * First, check if the first ref of the current inode was overwritten
3094 * before. If yes, we know that the current inode was already orphanized
3095 * and thus use the orphan name. If not, we can use get_cur_path to
3096 * get the path of the first ref as it would like while receiving at
3097 * this point in time.
3098 * New inodes are always orphan at the beginning, so force to use the
3099 * orphan name in this case.
3100 * The first ref is stored in valid_path and will be updated if it
3101 * gets moved around.
3103 if (!sctx->cur_inode_new) {
3104 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3105 sctx->cur_inode_gen);
3111 if (sctx->cur_inode_new || did_overwrite) {
3112 ret = gen_unique_name(sctx, sctx->cur_ino,
3113 sctx->cur_inode_gen, valid_path);
3118 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3124 list_for_each_entry(cur, &sctx->new_refs, list) {
3126 * We may have refs where the parent directory does not exist
3127 * yet. This happens if the parent directories inum is higher
3128 * the the current inum. To handle this case, we create the
3129 * parent directory out of order. But we need to check if this
3130 * did already happen before due to other refs in the same dir.
3132 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3135 if (ret == inode_state_will_create) {
3138 * First check if any of the current inodes refs did
3139 * already create the dir.
3141 list_for_each_entry(cur2, &sctx->new_refs, list) {
3144 if (cur2->dir == cur->dir) {
3151 * If that did not happen, check if a previous inode
3152 * did already create the dir.
3155 ret = did_create_dir(sctx, cur->dir);
3159 ret = send_create_inode(sctx, cur->dir);
3166 * Check if this new ref would overwrite the first ref of
3167 * another unprocessed inode. If yes, orphanize the
3168 * overwritten inode. If we find an overwritten ref that is
3169 * not the first ref, simply unlink it.
3171 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3172 cur->name, cur->name_len,
3173 &ow_inode, &ow_gen);
3177 ret = is_first_ref(sctx->parent_root,
3178 ow_inode, cur->dir, cur->name,
3183 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3188 ret = send_unlink(sctx, cur->full_path);
3195 * link/move the ref to the new place. If we have an orphan
3196 * inode, move it and update valid_path. If not, link or move
3197 * it depending on the inode mode.
3200 ret = send_rename(sctx, valid_path, cur->full_path);
3204 ret = fs_path_copy(valid_path, cur->full_path);
3208 if (S_ISDIR(sctx->cur_inode_mode)) {
3210 * Dirs can't be linked, so move it. For moved
3211 * dirs, we always have one new and one deleted
3212 * ref. The deleted ref is ignored later.
3214 ret = wait_for_parent_move(sctx, cur);
3218 ret = add_pending_dir_move(sctx,
3222 ret = send_rename(sctx, valid_path,
3225 ret = fs_path_copy(valid_path,
3231 ret = send_link(sctx, cur->full_path,
3237 ret = dup_ref(cur, &check_dirs);
3242 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3244 * Check if we can already rmdir the directory. If not,
3245 * orphanize it. For every dir item inside that gets deleted
3246 * later, we do this check again and rmdir it then if possible.
3247 * See the use of check_dirs for more details.
3249 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
3253 ret = send_rmdir(sctx, valid_path);
3256 } else if (!is_orphan) {
3257 ret = orphanize_inode(sctx, sctx->cur_ino,
3258 sctx->cur_inode_gen, valid_path);
3264 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3265 ret = dup_ref(cur, &check_dirs);
3269 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3270 !list_empty(&sctx->deleted_refs)) {
3272 * We have a moved dir. Add the old parent to check_dirs
3274 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3276 ret = dup_ref(cur, &check_dirs);
3279 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3281 * We have a non dir inode. Go through all deleted refs and
3282 * unlink them if they were not already overwritten by other
3285 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3286 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3287 sctx->cur_ino, sctx->cur_inode_gen,
3288 cur->name, cur->name_len);
3292 ret = send_unlink(sctx, cur->full_path);
3296 ret = dup_ref(cur, &check_dirs);
3301 * If the inode is still orphan, unlink the orphan. This may
3302 * happen when a previous inode did overwrite the first ref
3303 * of this inode and no new refs were added for the current
3304 * inode. Unlinking does not mean that the inode is deleted in
3305 * all cases. There may still be links to this inode in other
3309 ret = send_unlink(sctx, valid_path);
3316 * We did collect all parent dirs where cur_inode was once located. We
3317 * now go through all these dirs and check if they are pending for
3318 * deletion and if it's finally possible to perform the rmdir now.
3319 * We also update the inode stats of the parent dirs here.
3321 list_for_each_entry(cur, &check_dirs, list) {
3323 * In case we had refs into dirs that were not processed yet,
3324 * we don't need to do the utime and rmdir logic for these dirs.
3325 * The dir will be processed later.
3327 if (cur->dir > sctx->cur_ino)
3330 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3334 if (ret == inode_state_did_create ||
3335 ret == inode_state_no_change) {
3336 /* TODO delayed utimes */
3337 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3340 } else if (ret == inode_state_did_delete) {
3341 ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
3345 ret = get_cur_path(sctx, cur->dir,
3346 cur->dir_gen, valid_path);
3349 ret = send_rmdir(sctx, valid_path);
3359 __free_recorded_refs(&check_dirs);
3360 free_recorded_refs(sctx);
3361 fs_path_free(valid_path);
3365 static int __record_new_ref(int num, u64 dir, int index,
3366 struct fs_path *name,
3370 struct send_ctx *sctx = ctx;
3374 p = fs_path_alloc();
3378 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
3383 ret = get_cur_path(sctx, dir, gen, p);
3386 ret = fs_path_add_path(p, name);
3390 ret = record_ref(&sctx->new_refs, dir, gen, p);
3398 static int __record_deleted_ref(int num, u64 dir, int index,
3399 struct fs_path *name,
3403 struct send_ctx *sctx = ctx;
3407 p = fs_path_alloc();
3411 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3416 ret = get_cur_path(sctx, dir, gen, p);
3419 ret = fs_path_add_path(p, name);
3423 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3431 static int record_new_ref(struct send_ctx *sctx)
3435 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3436 sctx->cmp_key, 0, __record_new_ref, sctx);
3445 static int record_deleted_ref(struct send_ctx *sctx)
3449 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3450 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3459 struct find_ref_ctx {
3462 struct btrfs_root *root;
3463 struct fs_path *name;
3467 static int __find_iref(int num, u64 dir, int index,
3468 struct fs_path *name,
3471 struct find_ref_ctx *ctx = ctx_;
3475 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3476 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3478 * To avoid doing extra lookups we'll only do this if everything
3481 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3485 if (dir_gen != ctx->dir_gen)
3487 ctx->found_idx = num;
3493 static int find_iref(struct btrfs_root *root,
3494 struct btrfs_path *path,
3495 struct btrfs_key *key,
3496 u64 dir, u64 dir_gen, struct fs_path *name)
3499 struct find_ref_ctx ctx;
3503 ctx.dir_gen = dir_gen;
3507 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3511 if (ctx.found_idx == -1)
3514 return ctx.found_idx;
3517 static int __record_changed_new_ref(int num, u64 dir, int index,
3518 struct fs_path *name,
3523 struct send_ctx *sctx = ctx;
3525 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3530 ret = find_iref(sctx->parent_root, sctx->right_path,
3531 sctx->cmp_key, dir, dir_gen, name);
3533 ret = __record_new_ref(num, dir, index, name, sctx);
3540 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3541 struct fs_path *name,
3546 struct send_ctx *sctx = ctx;
3548 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3553 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3554 dir, dir_gen, name);
3556 ret = __record_deleted_ref(num, dir, index, name, sctx);
3563 static int record_changed_ref(struct send_ctx *sctx)
3567 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3568 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3571 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3572 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3582 * Record and process all refs at once. Needed when an inode changes the
3583 * generation number, which means that it was deleted and recreated.
3585 static int process_all_refs(struct send_ctx *sctx,
3586 enum btrfs_compare_tree_result cmd)
3589 struct btrfs_root *root;
3590 struct btrfs_path *path;
3591 struct btrfs_key key;
3592 struct btrfs_key found_key;
3593 struct extent_buffer *eb;
3595 iterate_inode_ref_t cb;
3596 int pending_move = 0;
3598 path = alloc_path_for_send();
3602 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3603 root = sctx->send_root;
3604 cb = __record_new_ref;
3605 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3606 root = sctx->parent_root;
3607 cb = __record_deleted_ref;
3612 key.objectid = sctx->cmp_key->objectid;
3613 key.type = BTRFS_INODE_REF_KEY;
3616 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3622 eb = path->nodes[0];
3623 slot = path->slots[0];
3624 btrfs_item_key_to_cpu(eb, &found_key, slot);
3626 if (found_key.objectid != key.objectid ||
3627 (found_key.type != BTRFS_INODE_REF_KEY &&
3628 found_key.type != BTRFS_INODE_EXTREF_KEY))
3631 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
3632 btrfs_release_path(path);
3636 key.offset = found_key.offset + 1;
3638 btrfs_release_path(path);
3640 ret = process_recorded_refs(sctx, &pending_move);
3641 /* Only applicable to an incremental send. */
3642 ASSERT(pending_move == 0);
3645 btrfs_free_path(path);
3649 static int send_set_xattr(struct send_ctx *sctx,
3650 struct fs_path *path,
3651 const char *name, int name_len,
3652 const char *data, int data_len)
3656 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3660 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3661 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3662 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3664 ret = send_cmd(sctx);
3671 static int send_remove_xattr(struct send_ctx *sctx,
3672 struct fs_path *path,
3673 const char *name, int name_len)
3677 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3681 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3682 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3684 ret = send_cmd(sctx);
3691 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3692 const char *name, int name_len,
3693 const char *data, int data_len,
3697 struct send_ctx *sctx = ctx;
3699 posix_acl_xattr_header dummy_acl;
3701 p = fs_path_alloc();
3706 * This hack is needed because empty acl's are stored as zero byte
3707 * data in xattrs. Problem with that is, that receiving these zero byte
3708 * acl's will fail later. To fix this, we send a dummy acl list that
3709 * only contains the version number and no entries.
3711 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3712 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3713 if (data_len == 0) {
3714 dummy_acl.a_version =
3715 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3716 data = (char *)&dummy_acl;
3717 data_len = sizeof(dummy_acl);
3721 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3725 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3732 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3733 const char *name, int name_len,
3734 const char *data, int data_len,
3738 struct send_ctx *sctx = ctx;
3741 p = fs_path_alloc();
3745 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3749 ret = send_remove_xattr(sctx, p, name, name_len);
3756 static int process_new_xattr(struct send_ctx *sctx)
3760 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3761 sctx->cmp_key, __process_new_xattr, sctx);
3766 static int process_deleted_xattr(struct send_ctx *sctx)
3770 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3771 sctx->cmp_key, __process_deleted_xattr, sctx);
3776 struct find_xattr_ctx {
3784 static int __find_xattr(int num, struct btrfs_key *di_key,
3785 const char *name, int name_len,
3786 const char *data, int data_len,
3787 u8 type, void *vctx)
3789 struct find_xattr_ctx *ctx = vctx;
3791 if (name_len == ctx->name_len &&
3792 strncmp(name, ctx->name, name_len) == 0) {
3793 ctx->found_idx = num;
3794 ctx->found_data_len = data_len;
3795 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
3796 if (!ctx->found_data)
3803 static int find_xattr(struct btrfs_root *root,
3804 struct btrfs_path *path,
3805 struct btrfs_key *key,
3806 const char *name, int name_len,
3807 char **data, int *data_len)
3810 struct find_xattr_ctx ctx;
3813 ctx.name_len = name_len;
3815 ctx.found_data = NULL;
3816 ctx.found_data_len = 0;
3818 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
3822 if (ctx.found_idx == -1)
3825 *data = ctx.found_data;
3826 *data_len = ctx.found_data_len;
3828 kfree(ctx.found_data);
3830 return ctx.found_idx;
3834 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3835 const char *name, int name_len,
3836 const char *data, int data_len,
3840 struct send_ctx *sctx = ctx;
3841 char *found_data = NULL;
3842 int found_data_len = 0;
3844 ret = find_xattr(sctx->parent_root, sctx->right_path,
3845 sctx->cmp_key, name, name_len, &found_data,
3847 if (ret == -ENOENT) {
3848 ret = __process_new_xattr(num, di_key, name, name_len, data,
3849 data_len, type, ctx);
3850 } else if (ret >= 0) {
3851 if (data_len != found_data_len ||
3852 memcmp(data, found_data, data_len)) {
3853 ret = __process_new_xattr(num, di_key, name, name_len,
3854 data, data_len, type, ctx);
3864 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3865 const char *name, int name_len,
3866 const char *data, int data_len,
3870 struct send_ctx *sctx = ctx;
3872 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3873 name, name_len, NULL, NULL);
3875 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3876 data_len, type, ctx);
3883 static int process_changed_xattr(struct send_ctx *sctx)
3887 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3888 sctx->cmp_key, __process_changed_new_xattr, sctx);
3891 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3892 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3898 static int process_all_new_xattrs(struct send_ctx *sctx)
3901 struct btrfs_root *root;
3902 struct btrfs_path *path;
3903 struct btrfs_key key;
3904 struct btrfs_key found_key;
3905 struct extent_buffer *eb;
3908 path = alloc_path_for_send();
3912 root = sctx->send_root;
3914 key.objectid = sctx->cmp_key->objectid;
3915 key.type = BTRFS_XATTR_ITEM_KEY;
3918 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3926 eb = path->nodes[0];
3927 slot = path->slots[0];
3928 btrfs_item_key_to_cpu(eb, &found_key, slot);
3930 if (found_key.objectid != key.objectid ||
3931 found_key.type != key.type) {
3936 ret = iterate_dir_item(root, path, &found_key,
3937 __process_new_xattr, sctx);
3941 btrfs_release_path(path);
3942 key.offset = found_key.offset + 1;
3946 btrfs_free_path(path);
3950 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
3952 struct btrfs_root *root = sctx->send_root;
3953 struct btrfs_fs_info *fs_info = root->fs_info;
3954 struct inode *inode;
3957 struct btrfs_key key;
3958 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
3960 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
3963 key.objectid = sctx->cur_ino;
3964 key.type = BTRFS_INODE_ITEM_KEY;
3967 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3969 return PTR_ERR(inode);
3971 if (offset + len > i_size_read(inode)) {
3972 if (offset > i_size_read(inode))
3975 len = offset - i_size_read(inode);
3980 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
3981 while (index <= last_index) {
3982 unsigned cur_len = min_t(unsigned, len,
3983 PAGE_CACHE_SIZE - pg_offset);
3984 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
3990 if (!PageUptodate(page)) {
3991 btrfs_readpage(NULL, page);
3993 if (!PageUptodate(page)) {
3995 page_cache_release(page);
4002 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4005 page_cache_release(page);
4017 * Read some bytes from the current inode/file and send a write command to
4020 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4024 ssize_t num_read = 0;
4026 p = fs_path_alloc();
4030 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4032 num_read = fill_read_buf(sctx, offset, len);
4033 if (num_read <= 0) {
4039 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4043 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4047 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4048 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4049 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
4051 ret = send_cmd(sctx);
4062 * Send a clone command to user space.
4064 static int send_clone(struct send_ctx *sctx,
4065 u64 offset, u32 len,
4066 struct clone_root *clone_root)
4072 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4073 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4074 clone_root->root->objectid, clone_root->ino,
4075 clone_root->offset);
4077 p = fs_path_alloc();
4081 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4085 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4089 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4090 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4091 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4093 if (clone_root->root == sctx->send_root) {
4094 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
4095 &gen, NULL, NULL, NULL, NULL);
4098 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4100 ret = get_inode_path(clone_root->root, clone_root->ino, p);
4105 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4106 clone_root->root->root_item.uuid);
4107 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
4108 le64_to_cpu(clone_root->root->root_item.ctransid));
4109 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4110 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4111 clone_root->offset);
4113 ret = send_cmd(sctx);
4122 * Send an update extent command to user space.
4124 static int send_update_extent(struct send_ctx *sctx,
4125 u64 offset, u32 len)
4130 p = fs_path_alloc();
4134 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4138 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4142 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4143 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4144 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4146 ret = send_cmd(sctx);
4154 static int send_hole(struct send_ctx *sctx, u64 end)
4156 struct fs_path *p = NULL;
4157 u64 offset = sctx->cur_inode_last_extent;
4161 p = fs_path_alloc();
4164 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4165 while (offset < end) {
4166 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4168 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4171 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4174 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4175 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4176 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4177 ret = send_cmd(sctx);
4187 static int send_write_or_clone(struct send_ctx *sctx,
4188 struct btrfs_path *path,
4189 struct btrfs_key *key,
4190 struct clone_root *clone_root)
4193 struct btrfs_file_extent_item *ei;
4194 u64 offset = key->offset;
4199 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
4201 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4202 struct btrfs_file_extent_item);
4203 type = btrfs_file_extent_type(path->nodes[0], ei);
4204 if (type == BTRFS_FILE_EXTENT_INLINE) {
4205 len = btrfs_file_extent_inline_len(path->nodes[0],
4206 path->slots[0], ei);
4208 * it is possible the inline item won't cover the whole page,
4209 * but there may be items after this page. Make
4210 * sure to send the whole thing
4212 len = PAGE_CACHE_ALIGN(len);
4214 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
4217 if (offset + len > sctx->cur_inode_size)
4218 len = sctx->cur_inode_size - offset;
4224 if (clone_root && IS_ALIGNED(offset + len, bs)) {
4225 ret = send_clone(sctx, offset, len, clone_root);
4226 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4227 ret = send_update_extent(sctx, offset, len);
4231 if (l > BTRFS_SEND_READ_SIZE)
4232 l = BTRFS_SEND_READ_SIZE;
4233 ret = send_write(sctx, pos + offset, l);
4246 static int is_extent_unchanged(struct send_ctx *sctx,
4247 struct btrfs_path *left_path,
4248 struct btrfs_key *ekey)
4251 struct btrfs_key key;
4252 struct btrfs_path *path = NULL;
4253 struct extent_buffer *eb;
4255 struct btrfs_key found_key;
4256 struct btrfs_file_extent_item *ei;
4261 u64 left_offset_fixed;
4269 path = alloc_path_for_send();
4273 eb = left_path->nodes[0];
4274 slot = left_path->slots[0];
4275 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4276 left_type = btrfs_file_extent_type(eb, ei);
4278 if (left_type != BTRFS_FILE_EXTENT_REG) {
4282 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4283 left_len = btrfs_file_extent_num_bytes(eb, ei);
4284 left_offset = btrfs_file_extent_offset(eb, ei);
4285 left_gen = btrfs_file_extent_generation(eb, ei);
4288 * Following comments will refer to these graphics. L is the left
4289 * extents which we are checking at the moment. 1-8 are the right
4290 * extents that we iterate.
4293 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4296 * |--1--|-2b-|...(same as above)
4298 * Alternative situation. Happens on files where extents got split.
4300 * |-----------7-----------|-6-|
4302 * Alternative situation. Happens on files which got larger.
4305 * Nothing follows after 8.
4308 key.objectid = ekey->objectid;
4309 key.type = BTRFS_EXTENT_DATA_KEY;
4310 key.offset = ekey->offset;
4311 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4320 * Handle special case where the right side has no extents at all.
4322 eb = path->nodes[0];
4323 slot = path->slots[0];
4324 btrfs_item_key_to_cpu(eb, &found_key, slot);
4325 if (found_key.objectid != key.objectid ||
4326 found_key.type != key.type) {
4327 /* If we're a hole then just pretend nothing changed */
4328 ret = (left_disknr) ? 0 : 1;
4333 * We're now on 2a, 2b or 7.
4336 while (key.offset < ekey->offset + left_len) {
4337 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4338 right_type = btrfs_file_extent_type(eb, ei);
4339 if (right_type != BTRFS_FILE_EXTENT_REG) {
4344 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4345 right_len = btrfs_file_extent_num_bytes(eb, ei);
4346 right_offset = btrfs_file_extent_offset(eb, ei);
4347 right_gen = btrfs_file_extent_generation(eb, ei);
4350 * Are we at extent 8? If yes, we know the extent is changed.
4351 * This may only happen on the first iteration.
4353 if (found_key.offset + right_len <= ekey->offset) {
4354 /* If we're a hole just pretend nothing changed */
4355 ret = (left_disknr) ? 0 : 1;
4359 left_offset_fixed = left_offset;
4360 if (key.offset < ekey->offset) {
4361 /* Fix the right offset for 2a and 7. */
4362 right_offset += ekey->offset - key.offset;
4364 /* Fix the left offset for all behind 2a and 2b */
4365 left_offset_fixed += key.offset - ekey->offset;
4369 * Check if we have the same extent.
4371 if (left_disknr != right_disknr ||
4372 left_offset_fixed != right_offset ||
4373 left_gen != right_gen) {
4379 * Go to the next extent.
4381 ret = btrfs_next_item(sctx->parent_root, path);
4385 eb = path->nodes[0];
4386 slot = path->slots[0];
4387 btrfs_item_key_to_cpu(eb, &found_key, slot);
4389 if (ret || found_key.objectid != key.objectid ||
4390 found_key.type != key.type) {
4391 key.offset += right_len;
4394 if (found_key.offset != key.offset + right_len) {
4402 * We're now behind the left extent (treat as unchanged) or at the end
4403 * of the right side (treat as changed).
4405 if (key.offset >= ekey->offset + left_len)
4412 btrfs_free_path(path);
4416 static int get_last_extent(struct send_ctx *sctx, u64 offset)
4418 struct btrfs_path *path;
4419 struct btrfs_root *root = sctx->send_root;
4420 struct btrfs_file_extent_item *fi;
4421 struct btrfs_key key;
4426 path = alloc_path_for_send();
4430 sctx->cur_inode_last_extent = 0;
4432 key.objectid = sctx->cur_ino;
4433 key.type = BTRFS_EXTENT_DATA_KEY;
4434 key.offset = offset;
4435 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4439 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4440 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4443 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4444 struct btrfs_file_extent_item);
4445 type = btrfs_file_extent_type(path->nodes[0], fi);
4446 if (type == BTRFS_FILE_EXTENT_INLINE) {
4447 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4448 path->slots[0], fi);
4449 extent_end = ALIGN(key.offset + size,
4450 sctx->send_root->sectorsize);
4452 extent_end = key.offset +
4453 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4455 sctx->cur_inode_last_extent = extent_end;
4457 btrfs_free_path(path);
4461 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4462 struct btrfs_key *key)
4464 struct btrfs_file_extent_item *fi;
4469 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4472 if (sctx->cur_inode_last_extent == (u64)-1) {
4473 ret = get_last_extent(sctx, key->offset - 1);
4478 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4479 struct btrfs_file_extent_item);
4480 type = btrfs_file_extent_type(path->nodes[0], fi);
4481 if (type == BTRFS_FILE_EXTENT_INLINE) {
4482 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4483 path->slots[0], fi);
4484 extent_end = ALIGN(key->offset + size,
4485 sctx->send_root->sectorsize);
4487 extent_end = key->offset +
4488 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4491 if (path->slots[0] == 0 &&
4492 sctx->cur_inode_last_extent < key->offset) {
4494 * We might have skipped entire leafs that contained only
4495 * file extent items for our current inode. These leafs have
4496 * a generation number smaller (older) than the one in the
4497 * current leaf and the leaf our last extent came from, and
4498 * are located between these 2 leafs.
4500 ret = get_last_extent(sctx, key->offset - 1);
4505 if (sctx->cur_inode_last_extent < key->offset)
4506 ret = send_hole(sctx, key->offset);
4507 sctx->cur_inode_last_extent = extent_end;
4511 static int process_extent(struct send_ctx *sctx,
4512 struct btrfs_path *path,
4513 struct btrfs_key *key)
4515 struct clone_root *found_clone = NULL;
4518 if (S_ISLNK(sctx->cur_inode_mode))
4521 if (sctx->parent_root && !sctx->cur_inode_new) {
4522 ret = is_extent_unchanged(sctx, path, key);
4530 struct btrfs_file_extent_item *ei;
4533 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4534 struct btrfs_file_extent_item);
4535 type = btrfs_file_extent_type(path->nodes[0], ei);
4536 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4537 type == BTRFS_FILE_EXTENT_REG) {
4539 * The send spec does not have a prealloc command yet,
4540 * so just leave a hole for prealloc'ed extents until
4541 * we have enough commands queued up to justify rev'ing
4544 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4549 /* Have a hole, just skip it. */
4550 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4557 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4558 sctx->cur_inode_size, &found_clone);
4559 if (ret != -ENOENT && ret < 0)
4562 ret = send_write_or_clone(sctx, path, key, found_clone);
4566 ret = maybe_send_hole(sctx, path, key);
4571 static int process_all_extents(struct send_ctx *sctx)
4574 struct btrfs_root *root;
4575 struct btrfs_path *path;
4576 struct btrfs_key key;
4577 struct btrfs_key found_key;
4578 struct extent_buffer *eb;
4581 root = sctx->send_root;
4582 path = alloc_path_for_send();
4586 key.objectid = sctx->cmp_key->objectid;
4587 key.type = BTRFS_EXTENT_DATA_KEY;
4589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4594 eb = path->nodes[0];
4595 slot = path->slots[0];
4597 if (slot >= btrfs_header_nritems(eb)) {
4598 ret = btrfs_next_leaf(root, path);
4601 } else if (ret > 0) {
4608 btrfs_item_key_to_cpu(eb, &found_key, slot);
4610 if (found_key.objectid != key.objectid ||
4611 found_key.type != key.type) {
4616 ret = process_extent(sctx, path, &found_key);
4624 btrfs_free_path(path);
4628 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4630 int *refs_processed)
4634 if (sctx->cur_ino == 0)
4636 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4637 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4639 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4642 ret = process_recorded_refs(sctx, pending_move);
4646 *refs_processed = 1;
4651 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4662 int pending_move = 0;
4663 int refs_processed = 0;
4665 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4671 * We have processed the refs and thus need to advance send_progress.
4672 * Now, calls to get_cur_xxx will take the updated refs of the current
4673 * inode into account.
4675 * On the other hand, if our current inode is a directory and couldn't
4676 * be moved/renamed because its parent was renamed/moved too and it has
4677 * a higher inode number, we can only move/rename our current inode
4678 * after we moved/renamed its parent. Therefore in this case operate on
4679 * the old path (pre move/rename) of our current inode, and the
4680 * move/rename will be performed later.
4682 if (refs_processed && !pending_move)
4683 sctx->send_progress = sctx->cur_ino + 1;
4685 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4687 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4690 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4691 &left_mode, &left_uid, &left_gid, NULL);
4695 if (!sctx->parent_root || sctx->cur_inode_new) {
4697 if (!S_ISLNK(sctx->cur_inode_mode))
4700 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4701 NULL, NULL, &right_mode, &right_uid,
4706 if (left_uid != right_uid || left_gid != right_gid)
4708 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4712 if (S_ISREG(sctx->cur_inode_mode)) {
4713 if (need_send_hole(sctx)) {
4714 if (sctx->cur_inode_last_extent == (u64)-1) {
4715 ret = get_last_extent(sctx, (u64)-1);
4719 if (sctx->cur_inode_last_extent <
4720 sctx->cur_inode_size) {
4721 ret = send_hole(sctx, sctx->cur_inode_size);
4726 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4727 sctx->cur_inode_size);
4733 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4734 left_uid, left_gid);
4739 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4746 * If other directory inodes depended on our current directory
4747 * inode's move/rename, now do their move/rename operations.
4749 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
4750 ret = apply_children_dir_moves(sctx);
4756 * Need to send that every time, no matter if it actually
4757 * changed between the two trees as we have done changes to
4760 sctx->send_progress = sctx->cur_ino + 1;
4761 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4769 static int changed_inode(struct send_ctx *sctx,
4770 enum btrfs_compare_tree_result result)
4773 struct btrfs_key *key = sctx->cmp_key;
4774 struct btrfs_inode_item *left_ii = NULL;
4775 struct btrfs_inode_item *right_ii = NULL;
4779 sctx->cur_ino = key->objectid;
4780 sctx->cur_inode_new_gen = 0;
4781 sctx->cur_inode_last_extent = (u64)-1;
4784 * Set send_progress to current inode. This will tell all get_cur_xxx
4785 * functions that the current inode's refs are not updated yet. Later,
4786 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4788 sctx->send_progress = sctx->cur_ino;
4790 if (result == BTRFS_COMPARE_TREE_NEW ||
4791 result == BTRFS_COMPARE_TREE_CHANGED) {
4792 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4793 sctx->left_path->slots[0],
4794 struct btrfs_inode_item);
4795 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4798 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4799 sctx->right_path->slots[0],
4800 struct btrfs_inode_item);
4801 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4804 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4805 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4806 sctx->right_path->slots[0],
4807 struct btrfs_inode_item);
4809 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4813 * The cur_ino = root dir case is special here. We can't treat
4814 * the inode as deleted+reused because it would generate a
4815 * stream that tries to delete/mkdir the root dir.
4817 if (left_gen != right_gen &&
4818 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4819 sctx->cur_inode_new_gen = 1;
4822 if (result == BTRFS_COMPARE_TREE_NEW) {
4823 sctx->cur_inode_gen = left_gen;
4824 sctx->cur_inode_new = 1;
4825 sctx->cur_inode_deleted = 0;
4826 sctx->cur_inode_size = btrfs_inode_size(
4827 sctx->left_path->nodes[0], left_ii);
4828 sctx->cur_inode_mode = btrfs_inode_mode(
4829 sctx->left_path->nodes[0], left_ii);
4830 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4831 ret = send_create_inode_if_needed(sctx);
4832 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4833 sctx->cur_inode_gen = right_gen;
4834 sctx->cur_inode_new = 0;
4835 sctx->cur_inode_deleted = 1;
4836 sctx->cur_inode_size = btrfs_inode_size(
4837 sctx->right_path->nodes[0], right_ii);
4838 sctx->cur_inode_mode = btrfs_inode_mode(
4839 sctx->right_path->nodes[0], right_ii);
4840 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4842 * We need to do some special handling in case the inode was
4843 * reported as changed with a changed generation number. This
4844 * means that the original inode was deleted and new inode
4845 * reused the same inum. So we have to treat the old inode as
4846 * deleted and the new one as new.
4848 if (sctx->cur_inode_new_gen) {
4850 * First, process the inode as if it was deleted.
4852 sctx->cur_inode_gen = right_gen;
4853 sctx->cur_inode_new = 0;
4854 sctx->cur_inode_deleted = 1;
4855 sctx->cur_inode_size = btrfs_inode_size(
4856 sctx->right_path->nodes[0], right_ii);
4857 sctx->cur_inode_mode = btrfs_inode_mode(
4858 sctx->right_path->nodes[0], right_ii);
4859 ret = process_all_refs(sctx,
4860 BTRFS_COMPARE_TREE_DELETED);
4865 * Now process the inode as if it was new.
4867 sctx->cur_inode_gen = left_gen;
4868 sctx->cur_inode_new = 1;
4869 sctx->cur_inode_deleted = 0;
4870 sctx->cur_inode_size = btrfs_inode_size(
4871 sctx->left_path->nodes[0], left_ii);
4872 sctx->cur_inode_mode = btrfs_inode_mode(
4873 sctx->left_path->nodes[0], left_ii);
4874 ret = send_create_inode_if_needed(sctx);
4878 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4882 * Advance send_progress now as we did not get into
4883 * process_recorded_refs_if_needed in the new_gen case.
4885 sctx->send_progress = sctx->cur_ino + 1;
4888 * Now process all extents and xattrs of the inode as if
4889 * they were all new.
4891 ret = process_all_extents(sctx);
4894 ret = process_all_new_xattrs(sctx);
4898 sctx->cur_inode_gen = left_gen;
4899 sctx->cur_inode_new = 0;
4900 sctx->cur_inode_new_gen = 0;
4901 sctx->cur_inode_deleted = 0;
4902 sctx->cur_inode_size = btrfs_inode_size(
4903 sctx->left_path->nodes[0], left_ii);
4904 sctx->cur_inode_mode = btrfs_inode_mode(
4905 sctx->left_path->nodes[0], left_ii);
4914 * We have to process new refs before deleted refs, but compare_trees gives us
4915 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4916 * first and later process them in process_recorded_refs.
4917 * For the cur_inode_new_gen case, we skip recording completely because
4918 * changed_inode did already initiate processing of refs. The reason for this is
4919 * that in this case, compare_tree actually compares the refs of 2 different
4920 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4921 * refs of the right tree as deleted and all refs of the left tree as new.
4923 static int changed_ref(struct send_ctx *sctx,
4924 enum btrfs_compare_tree_result result)
4928 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4930 if (!sctx->cur_inode_new_gen &&
4931 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4932 if (result == BTRFS_COMPARE_TREE_NEW)
4933 ret = record_new_ref(sctx);
4934 else if (result == BTRFS_COMPARE_TREE_DELETED)
4935 ret = record_deleted_ref(sctx);
4936 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4937 ret = record_changed_ref(sctx);
4944 * Process new/deleted/changed xattrs. We skip processing in the
4945 * cur_inode_new_gen case because changed_inode did already initiate processing
4946 * of xattrs. The reason is the same as in changed_ref
4948 static int changed_xattr(struct send_ctx *sctx,
4949 enum btrfs_compare_tree_result result)
4953 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4955 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4956 if (result == BTRFS_COMPARE_TREE_NEW)
4957 ret = process_new_xattr(sctx);
4958 else if (result == BTRFS_COMPARE_TREE_DELETED)
4959 ret = process_deleted_xattr(sctx);
4960 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4961 ret = process_changed_xattr(sctx);
4968 * Process new/deleted/changed extents. We skip processing in the
4969 * cur_inode_new_gen case because changed_inode did already initiate processing
4970 * of extents. The reason is the same as in changed_ref
4972 static int changed_extent(struct send_ctx *sctx,
4973 enum btrfs_compare_tree_result result)
4977 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4979 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4980 if (result != BTRFS_COMPARE_TREE_DELETED)
4981 ret = process_extent(sctx, sctx->left_path,
4988 static int dir_changed(struct send_ctx *sctx, u64 dir)
4990 u64 orig_gen, new_gen;
4993 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
4998 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5003 return (orig_gen != new_gen) ? 1 : 0;
5006 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5007 struct btrfs_key *key)
5009 struct btrfs_inode_extref *extref;
5010 struct extent_buffer *leaf;
5011 u64 dirid = 0, last_dirid = 0;
5018 /* Easy case, just check this one dirid */
5019 if (key->type == BTRFS_INODE_REF_KEY) {
5020 dirid = key->offset;
5022 ret = dir_changed(sctx, dirid);
5026 leaf = path->nodes[0];
5027 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5028 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5029 while (cur_offset < item_size) {
5030 extref = (struct btrfs_inode_extref *)(ptr +
5032 dirid = btrfs_inode_extref_parent(leaf, extref);
5033 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5034 cur_offset += ref_name_len + sizeof(*extref);
5035 if (dirid == last_dirid)
5037 ret = dir_changed(sctx, dirid);
5047 * Updates compare related fields in sctx and simply forwards to the actual
5048 * changed_xxx functions.
5050 static int changed_cb(struct btrfs_root *left_root,
5051 struct btrfs_root *right_root,
5052 struct btrfs_path *left_path,
5053 struct btrfs_path *right_path,
5054 struct btrfs_key *key,
5055 enum btrfs_compare_tree_result result,
5059 struct send_ctx *sctx = ctx;
5061 if (result == BTRFS_COMPARE_TREE_SAME) {
5062 if (key->type == BTRFS_INODE_REF_KEY ||
5063 key->type == BTRFS_INODE_EXTREF_KEY) {
5064 ret = compare_refs(sctx, left_path, key);
5069 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5070 return maybe_send_hole(sctx, left_path, key);
5074 result = BTRFS_COMPARE_TREE_CHANGED;
5078 sctx->left_path = left_path;
5079 sctx->right_path = right_path;
5080 sctx->cmp_key = key;
5082 ret = finish_inode_if_needed(sctx, 0);
5086 /* Ignore non-FS objects */
5087 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5088 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5091 if (key->type == BTRFS_INODE_ITEM_KEY)
5092 ret = changed_inode(sctx, result);
5093 else if (key->type == BTRFS_INODE_REF_KEY ||
5094 key->type == BTRFS_INODE_EXTREF_KEY)
5095 ret = changed_ref(sctx, result);
5096 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5097 ret = changed_xattr(sctx, result);
5098 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5099 ret = changed_extent(sctx, result);
5105 static int full_send_tree(struct send_ctx *sctx)
5108 struct btrfs_root *send_root = sctx->send_root;
5109 struct btrfs_key key;
5110 struct btrfs_key found_key;
5111 struct btrfs_path *path;
5112 struct extent_buffer *eb;
5117 path = alloc_path_for_send();
5121 spin_lock(&send_root->root_item_lock);
5122 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
5123 spin_unlock(&send_root->root_item_lock);
5125 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5126 key.type = BTRFS_INODE_ITEM_KEY;
5130 * Make sure the tree has not changed after re-joining. We detect this
5131 * by comparing start_ctransid and ctransid. They should always match.
5133 spin_lock(&send_root->root_item_lock);
5134 ctransid = btrfs_root_ctransid(&send_root->root_item);
5135 spin_unlock(&send_root->root_item_lock);
5137 if (ctransid != start_ctransid) {
5138 WARN(1, KERN_WARNING "BTRFS: the root that you're trying to "
5139 "send was modified in between. This is "
5140 "probably a bug.\n");
5145 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5152 eb = path->nodes[0];
5153 slot = path->slots[0];
5154 btrfs_item_key_to_cpu(eb, &found_key, slot);
5156 ret = changed_cb(send_root, NULL, path, NULL,
5157 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5161 key.objectid = found_key.objectid;
5162 key.type = found_key.type;
5163 key.offset = found_key.offset + 1;
5165 ret = btrfs_next_item(send_root, path);
5175 ret = finish_inode_if_needed(sctx, 1);
5178 btrfs_free_path(path);
5182 static int send_subvol(struct send_ctx *sctx)
5186 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5187 ret = send_header(sctx);
5192 ret = send_subvol_begin(sctx);
5196 if (sctx->parent_root) {
5197 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5201 ret = finish_inode_if_needed(sctx, 1);
5205 ret = full_send_tree(sctx);
5211 free_recorded_refs(sctx);
5215 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5217 spin_lock(&root->root_item_lock);
5218 root->send_in_progress--;
5220 * Not much left to do, we don't know why it's unbalanced and
5221 * can't blindly reset it to 0.
5223 if (root->send_in_progress < 0)
5224 btrfs_err(root->fs_info,
5225 "send_in_progres unbalanced %d root %llu\n",
5226 root->send_in_progress, root->root_key.objectid);
5227 spin_unlock(&root->root_item_lock);
5230 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5233 struct btrfs_root *send_root;
5234 struct btrfs_root *clone_root;
5235 struct btrfs_fs_info *fs_info;
5236 struct btrfs_ioctl_send_args *arg = NULL;
5237 struct btrfs_key key;
5238 struct send_ctx *sctx = NULL;
5240 u64 *clone_sources_tmp = NULL;
5241 int clone_sources_to_rollback = 0;
5242 int sort_clone_roots = 0;
5245 if (!capable(CAP_SYS_ADMIN))
5248 send_root = BTRFS_I(file_inode(mnt_file))->root;
5249 fs_info = send_root->fs_info;
5252 * The subvolume must remain read-only during send, protect against
5255 spin_lock(&send_root->root_item_lock);
5256 send_root->send_in_progress++;
5257 spin_unlock(&send_root->root_item_lock);
5260 * This is done when we lookup the root, it should already be complete
5261 * by the time we get here.
5263 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5266 * Userspace tools do the checks and warn the user if it's
5269 if (!btrfs_root_readonly(send_root)) {
5274 arg = memdup_user(arg_, sizeof(*arg));
5281 if (!access_ok(VERIFY_READ, arg->clone_sources,
5282 sizeof(*arg->clone_sources) *
5283 arg->clone_sources_count)) {
5288 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
5293 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5299 INIT_LIST_HEAD(&sctx->new_refs);
5300 INIT_LIST_HEAD(&sctx->deleted_refs);
5301 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5302 INIT_LIST_HEAD(&sctx->name_cache_list);
5304 sctx->flags = arg->flags;
5306 sctx->send_filp = fget(arg->send_fd);
5307 if (!sctx->send_filp) {
5312 sctx->send_root = send_root;
5313 sctx->clone_roots_cnt = arg->clone_sources_count;
5315 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5316 sctx->send_buf = vmalloc(sctx->send_max_size);
5317 if (!sctx->send_buf) {
5322 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5323 if (!sctx->read_buf) {
5328 sctx->pending_dir_moves = RB_ROOT;
5329 sctx->waiting_dir_moves = RB_ROOT;
5331 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5332 (arg->clone_sources_count + 1));
5333 if (!sctx->clone_roots) {
5338 if (arg->clone_sources_count) {
5339 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5340 sizeof(*arg->clone_sources));
5341 if (!clone_sources_tmp) {
5346 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5347 arg->clone_sources_count *
5348 sizeof(*arg->clone_sources));
5354 for (i = 0; i < arg->clone_sources_count; i++) {
5355 key.objectid = clone_sources_tmp[i];
5356 key.type = BTRFS_ROOT_ITEM_KEY;
5357 key.offset = (u64)-1;
5359 index = srcu_read_lock(&fs_info->subvol_srcu);
5361 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
5362 if (IS_ERR(clone_root)) {
5363 srcu_read_unlock(&fs_info->subvol_srcu, index);
5364 ret = PTR_ERR(clone_root);
5367 clone_sources_to_rollback = i + 1;
5368 spin_lock(&clone_root->root_item_lock);
5369 clone_root->send_in_progress++;
5370 if (!btrfs_root_readonly(clone_root)) {
5371 spin_unlock(&clone_root->root_item_lock);
5372 srcu_read_unlock(&fs_info->subvol_srcu, index);
5376 spin_unlock(&clone_root->root_item_lock);
5377 srcu_read_unlock(&fs_info->subvol_srcu, index);
5379 sctx->clone_roots[i].root = clone_root;
5381 vfree(clone_sources_tmp);
5382 clone_sources_tmp = NULL;
5385 if (arg->parent_root) {
5386 key.objectid = arg->parent_root;
5387 key.type = BTRFS_ROOT_ITEM_KEY;
5388 key.offset = (u64)-1;
5390 index = srcu_read_lock(&fs_info->subvol_srcu);
5392 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
5393 if (IS_ERR(sctx->parent_root)) {
5394 srcu_read_unlock(&fs_info->subvol_srcu, index);
5395 ret = PTR_ERR(sctx->parent_root);
5399 spin_lock(&sctx->parent_root->root_item_lock);
5400 sctx->parent_root->send_in_progress++;
5401 if (!btrfs_root_readonly(sctx->parent_root)) {
5402 spin_unlock(&sctx->parent_root->root_item_lock);
5403 srcu_read_unlock(&fs_info->subvol_srcu, index);
5407 spin_unlock(&sctx->parent_root->root_item_lock);
5409 srcu_read_unlock(&fs_info->subvol_srcu, index);
5413 * Clones from send_root are allowed, but only if the clone source
5414 * is behind the current send position. This is checked while searching
5415 * for possible clone sources.
5417 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5419 /* We do a bsearch later */
5420 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5421 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5423 sort_clone_roots = 1;
5425 ret = send_subvol(sctx);
5429 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5430 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5433 ret = send_cmd(sctx);
5439 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5440 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5442 struct pending_dir_move *pm;
5444 n = rb_first(&sctx->pending_dir_moves);
5445 pm = rb_entry(n, struct pending_dir_move, node);
5446 while (!list_empty(&pm->list)) {
5447 struct pending_dir_move *pm2;
5449 pm2 = list_first_entry(&pm->list,
5450 struct pending_dir_move, list);
5451 free_pending_move(sctx, pm2);
5453 free_pending_move(sctx, pm);
5456 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5457 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5459 struct waiting_dir_move *dm;
5461 n = rb_first(&sctx->waiting_dir_moves);
5462 dm = rb_entry(n, struct waiting_dir_move, node);
5463 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5467 if (sort_clone_roots) {
5468 for (i = 0; i < sctx->clone_roots_cnt; i++)
5469 btrfs_root_dec_send_in_progress(
5470 sctx->clone_roots[i].root);
5472 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5473 btrfs_root_dec_send_in_progress(
5474 sctx->clone_roots[i].root);
5476 btrfs_root_dec_send_in_progress(send_root);
5478 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5479 btrfs_root_dec_send_in_progress(sctx->parent_root);
5482 vfree(clone_sources_tmp);
5485 if (sctx->send_filp)
5486 fput(sctx->send_filp);
5488 vfree(sctx->clone_roots);
5489 vfree(sctx->send_buf);
5490 vfree(sctx->read_buf);
5492 name_cache_free(sctx);