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.
56 unsigned short buf_len:15;
57 unsigned short reversed:1;
61 * Average path length does not exceed 200 bytes, we'll have
62 * better packing in the slab and higher chance to satisfy
63 * a allocation later during send.
68 #define FS_PATH_INLINE_SIZE \
69 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
72 /* reused for each extent */
74 struct btrfs_root *root;
81 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
82 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
85 struct file *send_filp;
91 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
92 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
94 struct btrfs_root *send_root;
95 struct btrfs_root *parent_root;
96 struct clone_root *clone_roots;
99 /* current state of the compare_tree call */
100 struct btrfs_path *left_path;
101 struct btrfs_path *right_path;
102 struct btrfs_key *cmp_key;
105 * infos of the currently processed inode. In case of deleted inodes,
106 * these are the values from the deleted inode.
111 int cur_inode_new_gen;
112 int cur_inode_deleted;
116 u64 cur_inode_last_extent;
120 struct list_head new_refs;
121 struct list_head deleted_refs;
123 struct radix_tree_root name_cache;
124 struct list_head name_cache_list;
127 struct file_ra_state ra;
132 * We process inodes by their increasing order, so if before an
133 * incremental send we reverse the parent/child relationship of
134 * directories such that a directory with a lower inode number was
135 * the parent of a directory with a higher inode number, and the one
136 * becoming the new parent got renamed too, we can't rename/move the
137 * directory with lower inode number when we finish processing it - we
138 * must process the directory with higher inode number first, then
139 * rename/move it and then rename/move the directory with lower inode
140 * number. Example follows.
142 * Tree state when the first send was performed:
154 * Tree state when the second (incremental) send is performed:
163 * The sequence of steps that lead to the second state was:
165 * mv /a/b/c/d /a/b/c2/d2
166 * mv /a/b/c /a/b/c2/d2/cc
168 * "c" has lower inode number, but we can't move it (2nd mv operation)
169 * before we move "d", which has higher inode number.
171 * So we just memorize which move/rename operations must be performed
172 * later when their respective parent is processed and moved/renamed.
175 /* Indexed by parent directory inode number. */
176 struct rb_root pending_dir_moves;
179 * Reverse index, indexed by the inode number of a directory that
180 * is waiting for the move/rename of its immediate parent before its
181 * own move/rename can be performed.
183 struct rb_root waiting_dir_moves;
186 * A directory that is going to be rm'ed might have a child directory
187 * which is in the pending directory moves index above. In this case,
188 * the directory can only be removed after the move/rename of its child
189 * is performed. Example:
209 * Sequence of steps that lead to the send snapshot:
210 * rm -f /a/b/c/foo.txt
212 * mv /a/b/c/x /a/b/YY
215 * When the child is processed, its move/rename is delayed until its
216 * parent is processed (as explained above), but all other operations
217 * like update utimes, chown, chgrp, etc, are performed and the paths
218 * that it uses for those operations must use the orphanized name of
219 * its parent (the directory we're going to rm later), so we need to
220 * memorize that name.
222 * Indexed by the inode number of the directory to be deleted.
224 struct rb_root orphan_dirs;
227 struct pending_dir_move {
229 struct list_head list;
233 struct list_head update_refs;
236 struct waiting_dir_move {
240 * There might be some directory that could not be removed because it
241 * was waiting for this directory inode to be moved first. Therefore
242 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
247 struct orphan_dir_info {
253 struct name_cache_entry {
254 struct list_head list;
256 * radix_tree has only 32bit entries but we need to handle 64bit inums.
257 * We use the lower 32bit of the 64bit inum to store it in the tree. If
258 * more then one inum would fall into the same entry, we use radix_list
259 * to store the additional entries. radix_list is also used to store
260 * entries where two entries have the same inum but different
263 struct list_head radix_list;
269 int need_later_update;
274 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
276 static struct waiting_dir_move *
277 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
279 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
281 static int need_send_hole(struct send_ctx *sctx)
283 return (sctx->parent_root && !sctx->cur_inode_new &&
284 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
285 S_ISREG(sctx->cur_inode_mode));
288 static void fs_path_reset(struct fs_path *p)
291 p->start = p->buf + p->buf_len - 1;
301 static struct fs_path *fs_path_alloc(void)
305 p = kmalloc(sizeof(*p), GFP_NOFS);
309 p->buf = p->inline_buf;
310 p->buf_len = FS_PATH_INLINE_SIZE;
315 static struct fs_path *fs_path_alloc_reversed(void)
327 static void fs_path_free(struct fs_path *p)
331 if (p->buf != p->inline_buf)
336 static int fs_path_len(struct fs_path *p)
338 return p->end - p->start;
341 static int fs_path_ensure_buf(struct fs_path *p, int len)
349 if (p->buf_len >= len)
352 if (len > PATH_MAX) {
357 path_len = p->end - p->start;
358 old_buf_len = p->buf_len;
361 * First time the inline_buf does not suffice
363 if (p->buf == p->inline_buf) {
364 tmp_buf = kmalloc(len, GFP_NOFS);
366 memcpy(tmp_buf, p->buf, old_buf_len);
368 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
374 * The real size of the buffer is bigger, this will let the fast path
375 * happen most of the time
377 p->buf_len = ksize(p->buf);
380 tmp_buf = p->buf + old_buf_len - path_len - 1;
381 p->end = p->buf + p->buf_len - 1;
382 p->start = p->end - path_len;
383 memmove(p->start, tmp_buf, path_len + 1);
386 p->end = p->start + path_len;
391 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
397 new_len = p->end - p->start + name_len;
398 if (p->start != p->end)
400 ret = fs_path_ensure_buf(p, new_len);
405 if (p->start != p->end)
407 p->start -= name_len;
408 *prepared = p->start;
410 if (p->start != p->end)
421 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
426 ret = fs_path_prepare_for_add(p, name_len, &prepared);
429 memcpy(prepared, name, name_len);
435 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
440 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
443 memcpy(prepared, p2->start, p2->end - p2->start);
449 static int fs_path_add_from_extent_buffer(struct fs_path *p,
450 struct extent_buffer *eb,
451 unsigned long off, int len)
456 ret = fs_path_prepare_for_add(p, len, &prepared);
460 read_extent_buffer(eb, prepared, off, len);
466 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
470 p->reversed = from->reversed;
473 ret = fs_path_add_path(p, from);
479 static void fs_path_unreverse(struct fs_path *p)
488 len = p->end - p->start;
490 p->end = p->start + len;
491 memmove(p->start, tmp, len + 1);
495 static struct btrfs_path *alloc_path_for_send(void)
497 struct btrfs_path *path;
499 path = btrfs_alloc_path();
502 path->search_commit_root = 1;
503 path->skip_locking = 1;
504 path->need_commit_sem = 1;
508 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
518 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
519 /* TODO handle that correctly */
520 /*if (ret == -ERESTARTSYS) {
539 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
541 struct btrfs_tlv_header *hdr;
542 int total_len = sizeof(*hdr) + len;
543 int left = sctx->send_max_size - sctx->send_size;
545 if (unlikely(left < total_len))
548 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
549 hdr->tlv_type = cpu_to_le16(attr);
550 hdr->tlv_len = cpu_to_le16(len);
551 memcpy(hdr + 1, data, len);
552 sctx->send_size += total_len;
557 #define TLV_PUT_DEFINE_INT(bits) \
558 static int tlv_put_u##bits(struct send_ctx *sctx, \
559 u##bits attr, u##bits value) \
561 __le##bits __tmp = cpu_to_le##bits(value); \
562 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
565 TLV_PUT_DEFINE_INT(64)
567 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
568 const char *str, int len)
572 return tlv_put(sctx, attr, str, len);
575 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
578 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
581 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
582 struct extent_buffer *eb,
583 struct btrfs_timespec *ts)
585 struct btrfs_timespec bts;
586 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
587 return tlv_put(sctx, attr, &bts, sizeof(bts));
591 #define TLV_PUT(sctx, attrtype, attrlen, data) \
593 ret = tlv_put(sctx, attrtype, attrlen, data); \
595 goto tlv_put_failure; \
598 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
600 ret = tlv_put_u##bits(sctx, attrtype, value); \
602 goto tlv_put_failure; \
605 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
606 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
607 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
608 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
609 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
611 ret = tlv_put_string(sctx, attrtype, str, len); \
613 goto tlv_put_failure; \
615 #define TLV_PUT_PATH(sctx, attrtype, p) \
617 ret = tlv_put_string(sctx, attrtype, p->start, \
618 p->end - p->start); \
620 goto tlv_put_failure; \
622 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
624 ret = tlv_put_uuid(sctx, attrtype, uuid); \
626 goto tlv_put_failure; \
628 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
630 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
632 goto tlv_put_failure; \
635 static int send_header(struct send_ctx *sctx)
637 struct btrfs_stream_header hdr;
639 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
640 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
642 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
647 * For each command/item we want to send to userspace, we call this function.
649 static int begin_cmd(struct send_ctx *sctx, int cmd)
651 struct btrfs_cmd_header *hdr;
653 if (WARN_ON(!sctx->send_buf))
656 BUG_ON(sctx->send_size);
658 sctx->send_size += sizeof(*hdr);
659 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
660 hdr->cmd = cpu_to_le16(cmd);
665 static int send_cmd(struct send_ctx *sctx)
668 struct btrfs_cmd_header *hdr;
671 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
672 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
675 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
676 hdr->crc = cpu_to_le32(crc);
678 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
681 sctx->total_send_size += sctx->send_size;
682 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
689 * Sends a move instruction to user space
691 static int send_rename(struct send_ctx *sctx,
692 struct fs_path *from, struct fs_path *to)
696 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
698 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
702 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
703 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
705 ret = send_cmd(sctx);
713 * Sends a link instruction to user space
715 static int send_link(struct send_ctx *sctx,
716 struct fs_path *path, struct fs_path *lnk)
720 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
722 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
726 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
727 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
729 ret = send_cmd(sctx);
737 * Sends an unlink instruction to user space
739 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
743 verbose_printk("btrfs: send_unlink %s\n", path->start);
745 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
749 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
751 ret = send_cmd(sctx);
759 * Sends a rmdir instruction to user space
761 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
765 verbose_printk("btrfs: send_rmdir %s\n", path->start);
767 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
771 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
773 ret = send_cmd(sctx);
781 * Helper function to retrieve some fields from an inode item.
783 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
784 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
788 struct btrfs_inode_item *ii;
789 struct btrfs_key key;
792 key.type = BTRFS_INODE_ITEM_KEY;
794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
801 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
802 struct btrfs_inode_item);
804 *size = btrfs_inode_size(path->nodes[0], ii);
806 *gen = btrfs_inode_generation(path->nodes[0], ii);
808 *mode = btrfs_inode_mode(path->nodes[0], ii);
810 *uid = btrfs_inode_uid(path->nodes[0], ii);
812 *gid = btrfs_inode_gid(path->nodes[0], ii);
814 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
819 static int get_inode_info(struct btrfs_root *root,
820 u64 ino, u64 *size, u64 *gen,
821 u64 *mode, u64 *uid, u64 *gid,
824 struct btrfs_path *path;
827 path = alloc_path_for_send();
830 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
832 btrfs_free_path(path);
836 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
841 * Helper function to iterate the entries in ONE btrfs_inode_ref or
842 * btrfs_inode_extref.
843 * The iterate callback may return a non zero value to stop iteration. This can
844 * be a negative value for error codes or 1 to simply stop it.
846 * path must point to the INODE_REF or INODE_EXTREF when called.
848 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
849 struct btrfs_key *found_key, int resolve,
850 iterate_inode_ref_t iterate, void *ctx)
852 struct extent_buffer *eb = path->nodes[0];
853 struct btrfs_item *item;
854 struct btrfs_inode_ref *iref;
855 struct btrfs_inode_extref *extref;
856 struct btrfs_path *tmp_path;
860 int slot = path->slots[0];
867 unsigned long name_off;
868 unsigned long elem_size;
871 p = fs_path_alloc_reversed();
875 tmp_path = alloc_path_for_send();
882 if (found_key->type == BTRFS_INODE_REF_KEY) {
883 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
884 struct btrfs_inode_ref);
885 item = btrfs_item_nr(slot);
886 total = btrfs_item_size(eb, item);
887 elem_size = sizeof(*iref);
889 ptr = btrfs_item_ptr_offset(eb, slot);
890 total = btrfs_item_size_nr(eb, slot);
891 elem_size = sizeof(*extref);
894 while (cur < total) {
897 if (found_key->type == BTRFS_INODE_REF_KEY) {
898 iref = (struct btrfs_inode_ref *)(ptr + cur);
899 name_len = btrfs_inode_ref_name_len(eb, iref);
900 name_off = (unsigned long)(iref + 1);
901 index = btrfs_inode_ref_index(eb, iref);
902 dir = found_key->offset;
904 extref = (struct btrfs_inode_extref *)(ptr + cur);
905 name_len = btrfs_inode_extref_name_len(eb, extref);
906 name_off = (unsigned long)&extref->name;
907 index = btrfs_inode_extref_index(eb, extref);
908 dir = btrfs_inode_extref_parent(eb, extref);
912 start = btrfs_ref_to_path(root, tmp_path, name_len,
916 ret = PTR_ERR(start);
919 if (start < p->buf) {
920 /* overflow , try again with larger buffer */
921 ret = fs_path_ensure_buf(p,
922 p->buf_len + p->buf - start);
925 start = btrfs_ref_to_path(root, tmp_path,
930 ret = PTR_ERR(start);
933 BUG_ON(start < p->buf);
937 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
943 cur += elem_size + name_len;
944 ret = iterate(num, dir, index, p, ctx);
951 btrfs_free_path(tmp_path);
956 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
957 const char *name, int name_len,
958 const char *data, int data_len,
962 * Helper function to iterate the entries in ONE btrfs_dir_item.
963 * The iterate callback may return a non zero value to stop iteration. This can
964 * be a negative value for error codes or 1 to simply stop it.
966 * path must point to the dir item when called.
968 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
969 struct btrfs_key *found_key,
970 iterate_dir_item_t iterate, void *ctx)
973 struct extent_buffer *eb;
974 struct btrfs_item *item;
975 struct btrfs_dir_item *di;
976 struct btrfs_key di_key;
978 const int buf_len = PATH_MAX;
988 buf = kmalloc(buf_len, GFP_NOFS);
995 slot = path->slots[0];
996 item = btrfs_item_nr(slot);
997 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1000 total = btrfs_item_size(eb, item);
1003 while (cur < total) {
1004 name_len = btrfs_dir_name_len(eb, di);
1005 data_len = btrfs_dir_data_len(eb, di);
1006 type = btrfs_dir_type(eb, di);
1007 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1012 if (name_len + data_len > buf_len) {
1013 ret = -ENAMETOOLONG;
1017 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1018 name_len + data_len);
1020 len = sizeof(*di) + name_len + data_len;
1021 di = (struct btrfs_dir_item *)((char *)di + len);
1024 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1025 data_len, type, ctx);
1041 static int __copy_first_ref(int num, u64 dir, int index,
1042 struct fs_path *p, void *ctx)
1045 struct fs_path *pt = ctx;
1047 ret = fs_path_copy(pt, p);
1051 /* we want the first only */
1056 * Retrieve the first path of an inode. If an inode has more then one
1057 * ref/hardlink, this is ignored.
1059 static int get_inode_path(struct btrfs_root *root,
1060 u64 ino, struct fs_path *path)
1063 struct btrfs_key key, found_key;
1064 struct btrfs_path *p;
1066 p = alloc_path_for_send();
1070 fs_path_reset(path);
1073 key.type = BTRFS_INODE_REF_KEY;
1076 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1083 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1084 if (found_key.objectid != ino ||
1085 (found_key.type != BTRFS_INODE_REF_KEY &&
1086 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1091 ret = iterate_inode_ref(root, p, &found_key, 1,
1092 __copy_first_ref, path);
1102 struct backref_ctx {
1103 struct send_ctx *sctx;
1105 struct btrfs_path *path;
1106 /* number of total found references */
1110 * used for clones found in send_root. clones found behind cur_objectid
1111 * and cur_offset are not considered as allowed clones.
1116 /* may be truncated in case it's the last extent in a file */
1119 /* Just to check for bugs in backref resolving */
1123 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1125 u64 root = (u64)(uintptr_t)key;
1126 struct clone_root *cr = (struct clone_root *)elt;
1128 if (root < cr->root->objectid)
1130 if (root > cr->root->objectid)
1135 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1137 struct clone_root *cr1 = (struct clone_root *)e1;
1138 struct clone_root *cr2 = (struct clone_root *)e2;
1140 if (cr1->root->objectid < cr2->root->objectid)
1142 if (cr1->root->objectid > cr2->root->objectid)
1148 * Called for every backref that is found for the current extent.
1149 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1151 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1153 struct backref_ctx *bctx = ctx_;
1154 struct clone_root *found;
1158 /* First check if the root is in the list of accepted clone sources */
1159 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1160 bctx->sctx->clone_roots_cnt,
1161 sizeof(struct clone_root),
1162 __clone_root_cmp_bsearch);
1166 if (found->root == bctx->sctx->send_root &&
1167 ino == bctx->cur_objectid &&
1168 offset == bctx->cur_offset) {
1169 bctx->found_itself = 1;
1173 * There are inodes that have extents that lie behind its i_size. Don't
1174 * accept clones from these extents.
1176 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1178 btrfs_release_path(bctx->path);
1182 if (offset + bctx->extent_len > i_size)
1186 * Make sure we don't consider clones from send_root that are
1187 * behind the current inode/offset.
1189 if (found->root == bctx->sctx->send_root) {
1191 * TODO for the moment we don't accept clones from the inode
1192 * that is currently send. We may change this when
1193 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1196 if (ino >= bctx->cur_objectid)
1199 if (ino > bctx->cur_objectid)
1201 if (offset + bctx->extent_len > bctx->cur_offset)
1207 found->found_refs++;
1208 if (ino < found->ino) {
1210 found->offset = offset;
1211 } else if (found->ino == ino) {
1213 * same extent found more then once in the same file.
1215 if (found->offset > offset + bctx->extent_len)
1216 found->offset = offset;
1223 * Given an inode, offset and extent item, it finds a good clone for a clone
1224 * instruction. Returns -ENOENT when none could be found. The function makes
1225 * sure that the returned clone is usable at the point where sending is at the
1226 * moment. This means, that no clones are accepted which lie behind the current
1229 * path must point to the extent item when called.
1231 static int find_extent_clone(struct send_ctx *sctx,
1232 struct btrfs_path *path,
1233 u64 ino, u64 data_offset,
1235 struct clone_root **found)
1242 u64 extent_item_pos;
1244 struct btrfs_file_extent_item *fi;
1245 struct extent_buffer *eb = path->nodes[0];
1246 struct backref_ctx *backref_ctx = NULL;
1247 struct clone_root *cur_clone_root;
1248 struct btrfs_key found_key;
1249 struct btrfs_path *tmp_path;
1253 tmp_path = alloc_path_for_send();
1257 /* We only use this path under the commit sem */
1258 tmp_path->need_commit_sem = 0;
1260 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1266 backref_ctx->path = tmp_path;
1268 if (data_offset >= ino_size) {
1270 * There may be extents that lie behind the file's size.
1271 * I at least had this in combination with snapshotting while
1272 * writing large files.
1278 fi = btrfs_item_ptr(eb, path->slots[0],
1279 struct btrfs_file_extent_item);
1280 extent_type = btrfs_file_extent_type(eb, fi);
1281 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1285 compressed = btrfs_file_extent_compression(eb, fi);
1287 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1288 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1289 if (disk_byte == 0) {
1293 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1295 down_read(&sctx->send_root->fs_info->commit_root_sem);
1296 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1297 &found_key, &flags);
1298 up_read(&sctx->send_root->fs_info->commit_root_sem);
1299 btrfs_release_path(tmp_path);
1303 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1309 * Setup the clone roots.
1311 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1312 cur_clone_root = sctx->clone_roots + i;
1313 cur_clone_root->ino = (u64)-1;
1314 cur_clone_root->offset = 0;
1315 cur_clone_root->found_refs = 0;
1318 backref_ctx->sctx = sctx;
1319 backref_ctx->found = 0;
1320 backref_ctx->cur_objectid = ino;
1321 backref_ctx->cur_offset = data_offset;
1322 backref_ctx->found_itself = 0;
1323 backref_ctx->extent_len = num_bytes;
1326 * The last extent of a file may be too large due to page alignment.
1327 * We need to adjust extent_len in this case so that the checks in
1328 * __iterate_backrefs work.
1330 if (data_offset + num_bytes >= ino_size)
1331 backref_ctx->extent_len = ino_size - data_offset;
1334 * Now collect all backrefs.
1336 if (compressed == BTRFS_COMPRESS_NONE)
1337 extent_item_pos = logical - found_key.objectid;
1339 extent_item_pos = 0;
1340 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1341 found_key.objectid, extent_item_pos, 1,
1342 __iterate_backrefs, backref_ctx);
1347 if (!backref_ctx->found_itself) {
1348 /* found a bug in backref code? */
1350 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
1351 "send_root. inode=%llu, offset=%llu, "
1352 "disk_byte=%llu found extent=%llu\n",
1353 ino, data_offset, disk_byte, found_key.objectid);
1357 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1359 "num_bytes=%llu, logical=%llu\n",
1360 data_offset, ino, num_bytes, logical);
1362 if (!backref_ctx->found)
1363 verbose_printk("btrfs: no clones found\n");
1365 cur_clone_root = NULL;
1366 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1367 if (sctx->clone_roots[i].found_refs) {
1368 if (!cur_clone_root)
1369 cur_clone_root = sctx->clone_roots + i;
1370 else if (sctx->clone_roots[i].root == sctx->send_root)
1371 /* prefer clones from send_root over others */
1372 cur_clone_root = sctx->clone_roots + i;
1377 if (cur_clone_root) {
1378 if (compressed != BTRFS_COMPRESS_NONE) {
1380 * Offsets given by iterate_extent_inodes() are relative
1381 * to the start of the extent, we need to add logical
1382 * offset from the file extent item.
1383 * (See why at backref.c:check_extent_in_eb())
1385 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1388 *found = cur_clone_root;
1395 btrfs_free_path(tmp_path);
1400 static int read_symlink(struct btrfs_root *root,
1402 struct fs_path *dest)
1405 struct btrfs_path *path;
1406 struct btrfs_key key;
1407 struct btrfs_file_extent_item *ei;
1413 path = alloc_path_for_send();
1418 key.type = BTRFS_EXTENT_DATA_KEY;
1420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1425 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1426 struct btrfs_file_extent_item);
1427 type = btrfs_file_extent_type(path->nodes[0], ei);
1428 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1429 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1430 BUG_ON(compression);
1432 off = btrfs_file_extent_inline_start(ei);
1433 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
1435 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1438 btrfs_free_path(path);
1443 * Helper function to generate a file name that is unique in the root of
1444 * send_root and parent_root. This is used to generate names for orphan inodes.
1446 static int gen_unique_name(struct send_ctx *sctx,
1448 struct fs_path *dest)
1451 struct btrfs_path *path;
1452 struct btrfs_dir_item *di;
1457 path = alloc_path_for_send();
1462 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1464 ASSERT(len < sizeof(tmp));
1466 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1467 path, BTRFS_FIRST_FREE_OBJECTID,
1468 tmp, strlen(tmp), 0);
1469 btrfs_release_path(path);
1475 /* not unique, try again */
1480 if (!sctx->parent_root) {
1486 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1487 path, BTRFS_FIRST_FREE_OBJECTID,
1488 tmp, strlen(tmp), 0);
1489 btrfs_release_path(path);
1495 /* not unique, try again */
1503 ret = fs_path_add(dest, tmp, strlen(tmp));
1506 btrfs_free_path(path);
1511 inode_state_no_change,
1512 inode_state_will_create,
1513 inode_state_did_create,
1514 inode_state_will_delete,
1515 inode_state_did_delete,
1518 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1526 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1528 if (ret < 0 && ret != -ENOENT)
1532 if (!sctx->parent_root) {
1533 right_ret = -ENOENT;
1535 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1536 NULL, NULL, NULL, NULL);
1537 if (ret < 0 && ret != -ENOENT)
1542 if (!left_ret && !right_ret) {
1543 if (left_gen == gen && right_gen == gen) {
1544 ret = inode_state_no_change;
1545 } else if (left_gen == gen) {
1546 if (ino < sctx->send_progress)
1547 ret = inode_state_did_create;
1549 ret = inode_state_will_create;
1550 } else if (right_gen == gen) {
1551 if (ino < sctx->send_progress)
1552 ret = inode_state_did_delete;
1554 ret = inode_state_will_delete;
1558 } else if (!left_ret) {
1559 if (left_gen == gen) {
1560 if (ino < sctx->send_progress)
1561 ret = inode_state_did_create;
1563 ret = inode_state_will_create;
1567 } else if (!right_ret) {
1568 if (right_gen == gen) {
1569 if (ino < sctx->send_progress)
1570 ret = inode_state_did_delete;
1572 ret = inode_state_will_delete;
1584 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1588 ret = get_cur_inode_state(sctx, ino, gen);
1592 if (ret == inode_state_no_change ||
1593 ret == inode_state_did_create ||
1594 ret == inode_state_will_delete)
1604 * Helper function to lookup a dir item in a dir.
1606 static int lookup_dir_item_inode(struct btrfs_root *root,
1607 u64 dir, const char *name, int name_len,
1612 struct btrfs_dir_item *di;
1613 struct btrfs_key key;
1614 struct btrfs_path *path;
1616 path = alloc_path_for_send();
1620 di = btrfs_lookup_dir_item(NULL, root, path,
1621 dir, name, name_len, 0);
1630 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1631 *found_inode = key.objectid;
1632 *found_type = btrfs_dir_type(path->nodes[0], di);
1635 btrfs_free_path(path);
1640 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1641 * generation of the parent dir and the name of the dir entry.
1643 static int get_first_ref(struct btrfs_root *root, u64 ino,
1644 u64 *dir, u64 *dir_gen, struct fs_path *name)
1647 struct btrfs_key key;
1648 struct btrfs_key found_key;
1649 struct btrfs_path *path;
1653 path = alloc_path_for_send();
1658 key.type = BTRFS_INODE_REF_KEY;
1661 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1665 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1667 if (ret || found_key.objectid != ino ||
1668 (found_key.type != BTRFS_INODE_REF_KEY &&
1669 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1674 if (found_key.type == BTRFS_INODE_REF_KEY) {
1675 struct btrfs_inode_ref *iref;
1676 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1677 struct btrfs_inode_ref);
1678 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1679 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1680 (unsigned long)(iref + 1),
1682 parent_dir = found_key.offset;
1684 struct btrfs_inode_extref *extref;
1685 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1686 struct btrfs_inode_extref);
1687 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1688 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1689 (unsigned long)&extref->name, len);
1690 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1694 btrfs_release_path(path);
1696 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
1704 btrfs_free_path(path);
1708 static int is_first_ref(struct btrfs_root *root,
1710 const char *name, int name_len)
1713 struct fs_path *tmp_name;
1717 tmp_name = fs_path_alloc();
1721 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1725 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1730 ret = !memcmp(tmp_name->start, name, name_len);
1733 fs_path_free(tmp_name);
1738 * Used by process_recorded_refs to determine if a new ref would overwrite an
1739 * already existing ref. In case it detects an overwrite, it returns the
1740 * inode/gen in who_ino/who_gen.
1741 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1742 * to make sure later references to the overwritten inode are possible.
1743 * Orphanizing is however only required for the first ref of an inode.
1744 * process_recorded_refs does an additional is_first_ref check to see if
1745 * orphanizing is really required.
1747 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1748 const char *name, int name_len,
1749 u64 *who_ino, u64 *who_gen)
1753 u64 other_inode = 0;
1756 if (!sctx->parent_root)
1759 ret = is_inode_existent(sctx, dir, dir_gen);
1764 * If we have a parent root we need to verify that the parent dir was
1765 * not delted and then re-created, if it was then we have no overwrite
1766 * and we can just unlink this entry.
1768 if (sctx->parent_root) {
1769 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1771 if (ret < 0 && ret != -ENOENT)
1781 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1782 &other_inode, &other_type);
1783 if (ret < 0 && ret != -ENOENT)
1791 * Check if the overwritten ref was already processed. If yes, the ref
1792 * was already unlinked/moved, so we can safely assume that we will not
1793 * overwrite anything at this point in time.
1795 if (other_inode > sctx->send_progress) {
1796 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1797 who_gen, NULL, NULL, NULL, NULL);
1802 *who_ino = other_inode;
1812 * Checks if the ref was overwritten by an already processed inode. This is
1813 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1814 * thus the orphan name needs be used.
1815 * process_recorded_refs also uses it to avoid unlinking of refs that were
1818 static int did_overwrite_ref(struct send_ctx *sctx,
1819 u64 dir, u64 dir_gen,
1820 u64 ino, u64 ino_gen,
1821 const char *name, int name_len)
1828 if (!sctx->parent_root)
1831 ret = is_inode_existent(sctx, dir, dir_gen);
1835 /* check if the ref was overwritten by another ref */
1836 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1837 &ow_inode, &other_type);
1838 if (ret < 0 && ret != -ENOENT)
1841 /* was never and will never be overwritten */
1846 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1851 if (ow_inode == ino && gen == ino_gen) {
1856 /* we know that it is or will be overwritten. check this now */
1857 if (ow_inode < sctx->send_progress)
1867 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1868 * that got overwritten. This is used by process_recorded_refs to determine
1869 * if it has to use the path as returned by get_cur_path or the orphan name.
1871 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1874 struct fs_path *name = NULL;
1878 if (!sctx->parent_root)
1881 name = fs_path_alloc();
1885 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1889 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1890 name->start, fs_path_len(name));
1898 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1899 * so we need to do some special handling in case we have clashes. This function
1900 * takes care of this with the help of name_cache_entry::radix_list.
1901 * In case of error, nce is kfreed.
1903 static int name_cache_insert(struct send_ctx *sctx,
1904 struct name_cache_entry *nce)
1907 struct list_head *nce_head;
1909 nce_head = radix_tree_lookup(&sctx->name_cache,
1910 (unsigned long)nce->ino);
1912 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1917 INIT_LIST_HEAD(nce_head);
1919 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1926 list_add_tail(&nce->radix_list, nce_head);
1927 list_add_tail(&nce->list, &sctx->name_cache_list);
1928 sctx->name_cache_size++;
1933 static void name_cache_delete(struct send_ctx *sctx,
1934 struct name_cache_entry *nce)
1936 struct list_head *nce_head;
1938 nce_head = radix_tree_lookup(&sctx->name_cache,
1939 (unsigned long)nce->ino);
1941 btrfs_err(sctx->send_root->fs_info,
1942 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1943 nce->ino, sctx->name_cache_size);
1946 list_del(&nce->radix_list);
1947 list_del(&nce->list);
1948 sctx->name_cache_size--;
1951 * We may not get to the final release of nce_head if the lookup fails
1953 if (nce_head && list_empty(nce_head)) {
1954 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1959 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1962 struct list_head *nce_head;
1963 struct name_cache_entry *cur;
1965 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1969 list_for_each_entry(cur, nce_head, radix_list) {
1970 if (cur->ino == ino && cur->gen == gen)
1977 * Removes the entry from the list and adds it back to the end. This marks the
1978 * entry as recently used so that name_cache_clean_unused does not remove it.
1980 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1982 list_del(&nce->list);
1983 list_add_tail(&nce->list, &sctx->name_cache_list);
1987 * Remove some entries from the beginning of name_cache_list.
1989 static void name_cache_clean_unused(struct send_ctx *sctx)
1991 struct name_cache_entry *nce;
1993 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1996 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1997 nce = list_entry(sctx->name_cache_list.next,
1998 struct name_cache_entry, list);
1999 name_cache_delete(sctx, nce);
2004 static void name_cache_free(struct send_ctx *sctx)
2006 struct name_cache_entry *nce;
2008 while (!list_empty(&sctx->name_cache_list)) {
2009 nce = list_entry(sctx->name_cache_list.next,
2010 struct name_cache_entry, list);
2011 name_cache_delete(sctx, nce);
2017 * Used by get_cur_path for each ref up to the root.
2018 * Returns 0 if it succeeded.
2019 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2020 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2021 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2022 * Returns <0 in case of error.
2024 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2028 struct fs_path *dest)
2032 struct btrfs_path *path = NULL;
2033 struct name_cache_entry *nce = NULL;
2036 * First check if we already did a call to this function with the same
2037 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2038 * return the cached result.
2040 nce = name_cache_search(sctx, ino, gen);
2042 if (ino < sctx->send_progress && nce->need_later_update) {
2043 name_cache_delete(sctx, nce);
2047 name_cache_used(sctx, nce);
2048 *parent_ino = nce->parent_ino;
2049 *parent_gen = nce->parent_gen;
2050 ret = fs_path_add(dest, nce->name, nce->name_len);
2058 path = alloc_path_for_send();
2063 * If the inode is not existent yet, add the orphan name and return 1.
2064 * This should only happen for the parent dir that we determine in
2067 ret = is_inode_existent(sctx, ino, gen);
2072 ret = gen_unique_name(sctx, ino, gen, dest);
2080 * Depending on whether the inode was already processed or not, use
2081 * send_root or parent_root for ref lookup.
2083 if (ino < sctx->send_progress)
2084 ret = get_first_ref(sctx->send_root, ino,
2085 parent_ino, parent_gen, dest);
2087 ret = get_first_ref(sctx->parent_root, ino,
2088 parent_ino, parent_gen, dest);
2093 * Check if the ref was overwritten by an inode's ref that was processed
2094 * earlier. If yes, treat as orphan and return 1.
2096 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2097 dest->start, dest->end - dest->start);
2101 fs_path_reset(dest);
2102 ret = gen_unique_name(sctx, ino, gen, dest);
2110 * Store the result of the lookup in the name cache.
2112 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2120 nce->parent_ino = *parent_ino;
2121 nce->parent_gen = *parent_gen;
2122 nce->name_len = fs_path_len(dest);
2124 strcpy(nce->name, dest->start);
2126 if (ino < sctx->send_progress)
2127 nce->need_later_update = 0;
2129 nce->need_later_update = 1;
2131 nce_ret = name_cache_insert(sctx, nce);
2134 name_cache_clean_unused(sctx);
2137 btrfs_free_path(path);
2142 * Magic happens here. This function returns the first ref to an inode as it
2143 * would look like while receiving the stream at this point in time.
2144 * We walk the path up to the root. For every inode in between, we check if it
2145 * was already processed/sent. If yes, we continue with the parent as found
2146 * in send_root. If not, we continue with the parent as found in parent_root.
2147 * If we encounter an inode that was deleted at this point in time, we use the
2148 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2149 * that were not created yet and overwritten inodes/refs.
2151 * When do we have have orphan inodes:
2152 * 1. When an inode is freshly created and thus no valid refs are available yet
2153 * 2. When a directory lost all it's refs (deleted) but still has dir items
2154 * inside which were not processed yet (pending for move/delete). If anyone
2155 * tried to get the path to the dir items, it would get a path inside that
2157 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2158 * of an unprocessed inode. If in that case the first ref would be
2159 * overwritten, the overwritten inode gets "orphanized". Later when we
2160 * process this overwritten inode, it is restored at a new place by moving
2163 * sctx->send_progress tells this function at which point in time receiving
2166 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2167 struct fs_path *dest)
2170 struct fs_path *name = NULL;
2171 u64 parent_inode = 0;
2175 name = fs_path_alloc();
2182 fs_path_reset(dest);
2184 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2185 fs_path_reset(name);
2187 if (is_waiting_for_rm(sctx, ino)) {
2188 ret = gen_unique_name(sctx, ino, gen, name);
2191 ret = fs_path_add_path(dest, name);
2195 if (is_waiting_for_move(sctx, ino)) {
2196 ret = get_first_ref(sctx->parent_root, ino,
2197 &parent_inode, &parent_gen, name);
2199 ret = __get_cur_name_and_parent(sctx, ino, gen,
2209 ret = fs_path_add_path(dest, name);
2220 fs_path_unreverse(dest);
2225 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2227 static int send_subvol_begin(struct send_ctx *sctx)
2230 struct btrfs_root *send_root = sctx->send_root;
2231 struct btrfs_root *parent_root = sctx->parent_root;
2232 struct btrfs_path *path;
2233 struct btrfs_key key;
2234 struct btrfs_root_ref *ref;
2235 struct extent_buffer *leaf;
2239 path = btrfs_alloc_path();
2243 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2245 btrfs_free_path(path);
2249 key.objectid = send_root->objectid;
2250 key.type = BTRFS_ROOT_BACKREF_KEY;
2253 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2262 leaf = path->nodes[0];
2263 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2264 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2265 key.objectid != send_root->objectid) {
2269 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2270 namelen = btrfs_root_ref_name_len(leaf, ref);
2271 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2272 btrfs_release_path(path);
2275 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2279 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2284 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2285 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2286 sctx->send_root->root_item.uuid);
2287 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2288 le64_to_cpu(sctx->send_root->root_item.ctransid));
2290 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2291 sctx->parent_root->root_item.uuid);
2292 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2293 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2296 ret = send_cmd(sctx);
2300 btrfs_free_path(path);
2305 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2310 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2312 p = fs_path_alloc();
2316 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2320 ret = get_cur_path(sctx, ino, gen, p);
2323 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2324 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2326 ret = send_cmd(sctx);
2334 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2339 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2341 p = fs_path_alloc();
2345 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2349 ret = get_cur_path(sctx, ino, gen, p);
2352 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2353 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2355 ret = send_cmd(sctx);
2363 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2368 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2370 p = fs_path_alloc();
2374 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2378 ret = get_cur_path(sctx, ino, gen, p);
2381 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2382 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2383 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2385 ret = send_cmd(sctx);
2393 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2396 struct fs_path *p = NULL;
2397 struct btrfs_inode_item *ii;
2398 struct btrfs_path *path = NULL;
2399 struct extent_buffer *eb;
2400 struct btrfs_key key;
2403 verbose_printk("btrfs: send_utimes %llu\n", ino);
2405 p = fs_path_alloc();
2409 path = alloc_path_for_send();
2416 key.type = BTRFS_INODE_ITEM_KEY;
2418 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2422 eb = path->nodes[0];
2423 slot = path->slots[0];
2424 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2426 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2430 ret = get_cur_path(sctx, ino, gen, p);
2433 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2434 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2435 btrfs_inode_atime(ii));
2436 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2437 btrfs_inode_mtime(ii));
2438 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2439 btrfs_inode_ctime(ii));
2440 /* TODO Add otime support when the otime patches get into upstream */
2442 ret = send_cmd(sctx);
2447 btrfs_free_path(path);
2452 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2453 * a valid path yet because we did not process the refs yet. So, the inode
2454 * is created as orphan.
2456 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2465 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2467 p = fs_path_alloc();
2471 if (ino != sctx->cur_ino) {
2472 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2477 gen = sctx->cur_inode_gen;
2478 mode = sctx->cur_inode_mode;
2479 rdev = sctx->cur_inode_rdev;
2482 if (S_ISREG(mode)) {
2483 cmd = BTRFS_SEND_C_MKFILE;
2484 } else if (S_ISDIR(mode)) {
2485 cmd = BTRFS_SEND_C_MKDIR;
2486 } else if (S_ISLNK(mode)) {
2487 cmd = BTRFS_SEND_C_SYMLINK;
2488 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2489 cmd = BTRFS_SEND_C_MKNOD;
2490 } else if (S_ISFIFO(mode)) {
2491 cmd = BTRFS_SEND_C_MKFIFO;
2492 } else if (S_ISSOCK(mode)) {
2493 cmd = BTRFS_SEND_C_MKSOCK;
2495 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2496 (int)(mode & S_IFMT));
2501 ret = begin_cmd(sctx, cmd);
2505 ret = gen_unique_name(sctx, ino, gen, p);
2509 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2510 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2512 if (S_ISLNK(mode)) {
2514 ret = read_symlink(sctx->send_root, ino, p);
2517 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2518 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2519 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2520 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2521 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2524 ret = send_cmd(sctx);
2536 * We need some special handling for inodes that get processed before the parent
2537 * directory got created. See process_recorded_refs for details.
2538 * This function does the check if we already created the dir out of order.
2540 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2543 struct btrfs_path *path = NULL;
2544 struct btrfs_key key;
2545 struct btrfs_key found_key;
2546 struct btrfs_key di_key;
2547 struct extent_buffer *eb;
2548 struct btrfs_dir_item *di;
2551 path = alloc_path_for_send();
2558 key.type = BTRFS_DIR_INDEX_KEY;
2560 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2565 eb = path->nodes[0];
2566 slot = path->slots[0];
2567 if (slot >= btrfs_header_nritems(eb)) {
2568 ret = btrfs_next_leaf(sctx->send_root, path);
2571 } else if (ret > 0) {
2578 btrfs_item_key_to_cpu(eb, &found_key, slot);
2579 if (found_key.objectid != key.objectid ||
2580 found_key.type != key.type) {
2585 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2586 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2588 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2589 di_key.objectid < sctx->send_progress) {
2598 btrfs_free_path(path);
2603 * Only creates the inode if it is:
2604 * 1. Not a directory
2605 * 2. Or a directory which was not created already due to out of order
2606 * directories. See did_create_dir and process_recorded_refs for details.
2608 static int send_create_inode_if_needed(struct send_ctx *sctx)
2612 if (S_ISDIR(sctx->cur_inode_mode)) {
2613 ret = did_create_dir(sctx, sctx->cur_ino);
2622 ret = send_create_inode(sctx, sctx->cur_ino);
2630 struct recorded_ref {
2631 struct list_head list;
2634 struct fs_path *full_path;
2642 * We need to process new refs before deleted refs, but compare_tree gives us
2643 * everything mixed. So we first record all refs and later process them.
2644 * This function is a helper to record one ref.
2646 static int __record_ref(struct list_head *head, u64 dir,
2647 u64 dir_gen, struct fs_path *path)
2649 struct recorded_ref *ref;
2651 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2656 ref->dir_gen = dir_gen;
2657 ref->full_path = path;
2659 ref->name = (char *)kbasename(ref->full_path->start);
2660 ref->name_len = ref->full_path->end - ref->name;
2661 ref->dir_path = ref->full_path->start;
2662 if (ref->name == ref->full_path->start)
2663 ref->dir_path_len = 0;
2665 ref->dir_path_len = ref->full_path->end -
2666 ref->full_path->start - 1 - ref->name_len;
2668 list_add_tail(&ref->list, head);
2672 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2674 struct recorded_ref *new;
2676 new = kmalloc(sizeof(*ref), GFP_NOFS);
2680 new->dir = ref->dir;
2681 new->dir_gen = ref->dir_gen;
2682 new->full_path = NULL;
2683 INIT_LIST_HEAD(&new->list);
2684 list_add_tail(&new->list, list);
2688 static void __free_recorded_refs(struct list_head *head)
2690 struct recorded_ref *cur;
2692 while (!list_empty(head)) {
2693 cur = list_entry(head->next, struct recorded_ref, list);
2694 fs_path_free(cur->full_path);
2695 list_del(&cur->list);
2700 static void free_recorded_refs(struct send_ctx *sctx)
2702 __free_recorded_refs(&sctx->new_refs);
2703 __free_recorded_refs(&sctx->deleted_refs);
2707 * Renames/moves a file/dir to its orphan name. Used when the first
2708 * ref of an unprocessed inode gets overwritten and for all non empty
2711 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2712 struct fs_path *path)
2715 struct fs_path *orphan;
2717 orphan = fs_path_alloc();
2721 ret = gen_unique_name(sctx, ino, gen, orphan);
2725 ret = send_rename(sctx, path, orphan);
2728 fs_path_free(orphan);
2732 static struct orphan_dir_info *
2733 add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2735 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2736 struct rb_node *parent = NULL;
2737 struct orphan_dir_info *entry, *odi;
2739 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2741 return ERR_PTR(-ENOMEM);
2747 entry = rb_entry(parent, struct orphan_dir_info, node);
2748 if (dir_ino < entry->ino) {
2750 } else if (dir_ino > entry->ino) {
2751 p = &(*p)->rb_right;
2758 rb_link_node(&odi->node, parent, p);
2759 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2763 static struct orphan_dir_info *
2764 get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2766 struct rb_node *n = sctx->orphan_dirs.rb_node;
2767 struct orphan_dir_info *entry;
2770 entry = rb_entry(n, struct orphan_dir_info, node);
2771 if (dir_ino < entry->ino)
2773 else if (dir_ino > entry->ino)
2781 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2783 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2788 static void free_orphan_dir_info(struct send_ctx *sctx,
2789 struct orphan_dir_info *odi)
2793 rb_erase(&odi->node, &sctx->orphan_dirs);
2798 * Returns 1 if a directory can be removed at this point in time.
2799 * We check this by iterating all dir items and checking if the inode behind
2800 * the dir item was already processed.
2802 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2806 struct btrfs_root *root = sctx->parent_root;
2807 struct btrfs_path *path;
2808 struct btrfs_key key;
2809 struct btrfs_key found_key;
2810 struct btrfs_key loc;
2811 struct btrfs_dir_item *di;
2814 * Don't try to rmdir the top/root subvolume dir.
2816 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2819 path = alloc_path_for_send();
2824 key.type = BTRFS_DIR_INDEX_KEY;
2826 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2831 struct waiting_dir_move *dm;
2833 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2834 ret = btrfs_next_leaf(root, path);
2841 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2843 if (found_key.objectid != key.objectid ||
2844 found_key.type != key.type)
2847 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2848 struct btrfs_dir_item);
2849 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2851 dm = get_waiting_dir_move(sctx, loc.objectid);
2853 struct orphan_dir_info *odi;
2855 odi = add_orphan_dir_info(sctx, dir);
2861 dm->rmdir_ino = dir;
2866 if (loc.objectid > send_progress) {
2877 btrfs_free_path(path);
2881 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2883 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
2885 return entry != NULL;
2888 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2890 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2891 struct rb_node *parent = NULL;
2892 struct waiting_dir_move *entry, *dm;
2894 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2902 entry = rb_entry(parent, struct waiting_dir_move, node);
2903 if (ino < entry->ino) {
2905 } else if (ino > entry->ino) {
2906 p = &(*p)->rb_right;
2913 rb_link_node(&dm->node, parent, p);
2914 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2918 static struct waiting_dir_move *
2919 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2921 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2922 struct waiting_dir_move *entry;
2925 entry = rb_entry(n, struct waiting_dir_move, node);
2926 if (ino < entry->ino)
2928 else if (ino > entry->ino)
2936 static void free_waiting_dir_move(struct send_ctx *sctx,
2937 struct waiting_dir_move *dm)
2941 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2945 static int add_pending_dir_move(struct send_ctx *sctx,
2950 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2951 struct rb_node *parent = NULL;
2952 struct pending_dir_move *entry = NULL, *pm;
2953 struct recorded_ref *cur;
2957 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2960 pm->parent_ino = parent_ino;
2963 INIT_LIST_HEAD(&pm->list);
2964 INIT_LIST_HEAD(&pm->update_refs);
2965 RB_CLEAR_NODE(&pm->node);
2969 entry = rb_entry(parent, struct pending_dir_move, node);
2970 if (parent_ino < entry->parent_ino) {
2972 } else if (parent_ino > entry->parent_ino) {
2973 p = &(*p)->rb_right;
2980 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2981 ret = dup_ref(cur, &pm->update_refs);
2985 list_for_each_entry(cur, &sctx->new_refs, list) {
2986 ret = dup_ref(cur, &pm->update_refs);
2991 ret = add_waiting_dir_move(sctx, pm->ino);
2996 list_add_tail(&pm->list, &entry->list);
2998 rb_link_node(&pm->node, parent, p);
2999 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3004 __free_recorded_refs(&pm->update_refs);
3010 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3013 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3014 struct pending_dir_move *entry;
3017 entry = rb_entry(n, struct pending_dir_move, node);
3018 if (parent_ino < entry->parent_ino)
3020 else if (parent_ino > entry->parent_ino)
3028 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3030 struct fs_path *from_path = NULL;
3031 struct fs_path *to_path = NULL;
3032 struct fs_path *name = NULL;
3033 u64 orig_progress = sctx->send_progress;
3034 struct recorded_ref *cur;
3035 u64 parent_ino, parent_gen;
3036 struct waiting_dir_move *dm = NULL;
3040 name = fs_path_alloc();
3041 from_path = fs_path_alloc();
3042 if (!name || !from_path) {
3047 dm = get_waiting_dir_move(sctx, pm->ino);
3049 rmdir_ino = dm->rmdir_ino;
3050 free_waiting_dir_move(sctx, dm);
3052 ret = get_first_ref(sctx->parent_root, pm->ino,
3053 &parent_ino, &parent_gen, name);
3057 if (parent_ino == sctx->cur_ino) {
3058 /* child only renamed, not moved */
3059 ASSERT(parent_gen == sctx->cur_inode_gen);
3060 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3064 ret = fs_path_add_path(from_path, name);
3068 /* child moved and maybe renamed too */
3069 sctx->send_progress = pm->ino;
3070 ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
3078 to_path = fs_path_alloc();
3084 sctx->send_progress = sctx->cur_ino + 1;
3085 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3089 ret = send_rename(sctx, from_path, to_path);
3094 struct orphan_dir_info *odi;
3096 odi = get_orphan_dir_info(sctx, rmdir_ino);
3098 /* already deleted */
3101 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3107 name = fs_path_alloc();
3112 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3115 ret = send_rmdir(sctx, name);
3118 free_orphan_dir_info(sctx, odi);
3122 ret = send_utimes(sctx, pm->ino, pm->gen);
3127 * After rename/move, need to update the utimes of both new parent(s)
3128 * and old parent(s).
3130 list_for_each_entry(cur, &pm->update_refs, list) {
3131 if (cur->dir == rmdir_ino)
3133 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3140 fs_path_free(from_path);
3141 fs_path_free(to_path);
3142 sctx->send_progress = orig_progress;
3147 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3149 if (!list_empty(&m->list))
3151 if (!RB_EMPTY_NODE(&m->node))
3152 rb_erase(&m->node, &sctx->pending_dir_moves);
3153 __free_recorded_refs(&m->update_refs);
3157 static void tail_append_pending_moves(struct pending_dir_move *moves,
3158 struct list_head *stack)
3160 if (list_empty(&moves->list)) {
3161 list_add_tail(&moves->list, stack);
3164 list_splice_init(&moves->list, &list);
3165 list_add_tail(&moves->list, stack);
3166 list_splice_tail(&list, stack);
3170 static int apply_children_dir_moves(struct send_ctx *sctx)
3172 struct pending_dir_move *pm;
3173 struct list_head stack;
3174 u64 parent_ino = sctx->cur_ino;
3177 pm = get_pending_dir_moves(sctx, parent_ino);
3181 INIT_LIST_HEAD(&stack);
3182 tail_append_pending_moves(pm, &stack);
3184 while (!list_empty(&stack)) {
3185 pm = list_first_entry(&stack, struct pending_dir_move, list);
3186 parent_ino = pm->ino;
3187 ret = apply_dir_move(sctx, pm);
3188 free_pending_move(sctx, pm);
3191 pm = get_pending_dir_moves(sctx, parent_ino);
3193 tail_append_pending_moves(pm, &stack);
3198 while (!list_empty(&stack)) {
3199 pm = list_first_entry(&stack, struct pending_dir_move, list);
3200 free_pending_move(sctx, pm);
3205 static int wait_for_parent_move(struct send_ctx *sctx,
3206 struct recorded_ref *parent_ref)
3209 u64 ino = parent_ref->dir;
3210 u64 parent_ino_before, parent_ino_after;
3212 struct fs_path *path_before = NULL;
3213 struct fs_path *path_after = NULL;
3215 int register_upper_dirs;
3218 if (is_waiting_for_move(sctx, ino))
3221 if (parent_ref->dir <= sctx->cur_ino)
3224 ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
3225 NULL, NULL, NULL, NULL);
3231 if (parent_ref->dir_gen != old_gen)
3234 path_before = fs_path_alloc();
3238 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3240 if (ret == -ENOENT) {
3243 } else if (ret < 0) {
3247 path_after = fs_path_alloc();
3253 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3255 if (ret == -ENOENT) {
3258 } else if (ret < 0) {
3262 len1 = fs_path_len(path_before);
3263 len2 = fs_path_len(path_after);
3264 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3265 memcmp(path_before->start, path_after->start, len1)) {
3272 * Ok, our new most direct ancestor has a higher inode number but
3273 * wasn't moved/renamed. So maybe some of the new ancestors higher in
3274 * the hierarchy have an higher inode number too *and* were renamed
3275 * or moved - in this case we need to wait for the ancestor's rename
3276 * or move operation before we can do the move/rename for the current
3279 register_upper_dirs = 0;
3280 ino = parent_ino_after;
3282 while ((ret == 0 || register_upper_dirs) && ino > sctx->cur_ino) {
3285 fs_path_reset(path_before);
3286 fs_path_reset(path_after);
3288 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3289 &parent_gen, path_after);
3292 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3294 if (ret == -ENOENT) {
3297 } else if (ret < 0) {
3301 len1 = fs_path_len(path_before);
3302 len2 = fs_path_len(path_after);
3303 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3304 memcmp(path_before->start, path_after->start, len1)) {
3306 if (register_upper_dirs) {
3309 register_upper_dirs = 1;
3310 ino = parent_ref->dir;
3311 gen = parent_ref->dir_gen;
3314 } else if (register_upper_dirs) {
3315 ret = add_pending_dir_move(sctx, ino, gen,
3317 if (ret < 0 && ret != -EEXIST)
3321 ino = parent_ino_after;
3326 fs_path_free(path_before);
3327 fs_path_free(path_after);
3333 * This does all the move/link/unlink/rmdir magic.
3335 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3338 struct recorded_ref *cur;
3339 struct recorded_ref *cur2;
3340 struct list_head check_dirs;
3341 struct fs_path *valid_path = NULL;
3344 int did_overwrite = 0;
3346 u64 last_dir_ino_rm = 0;
3348 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3351 * This should never happen as the root dir always has the same ref
3352 * which is always '..'
3354 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3355 INIT_LIST_HEAD(&check_dirs);
3357 valid_path = fs_path_alloc();
3364 * First, check if the first ref of the current inode was overwritten
3365 * before. If yes, we know that the current inode was already orphanized
3366 * and thus use the orphan name. If not, we can use get_cur_path to
3367 * get the path of the first ref as it would like while receiving at
3368 * this point in time.
3369 * New inodes are always orphan at the beginning, so force to use the
3370 * orphan name in this case.
3371 * The first ref is stored in valid_path and will be updated if it
3372 * gets moved around.
3374 if (!sctx->cur_inode_new) {
3375 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3376 sctx->cur_inode_gen);
3382 if (sctx->cur_inode_new || did_overwrite) {
3383 ret = gen_unique_name(sctx, sctx->cur_ino,
3384 sctx->cur_inode_gen, valid_path);
3389 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3395 list_for_each_entry(cur, &sctx->new_refs, list) {
3397 * We may have refs where the parent directory does not exist
3398 * yet. This happens if the parent directories inum is higher
3399 * the the current inum. To handle this case, we create the
3400 * parent directory out of order. But we need to check if this
3401 * did already happen before due to other refs in the same dir.
3403 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3406 if (ret == inode_state_will_create) {
3409 * First check if any of the current inodes refs did
3410 * already create the dir.
3412 list_for_each_entry(cur2, &sctx->new_refs, list) {
3415 if (cur2->dir == cur->dir) {
3422 * If that did not happen, check if a previous inode
3423 * did already create the dir.
3426 ret = did_create_dir(sctx, cur->dir);
3430 ret = send_create_inode(sctx, cur->dir);
3437 * Check if this new ref would overwrite the first ref of
3438 * another unprocessed inode. If yes, orphanize the
3439 * overwritten inode. If we find an overwritten ref that is
3440 * not the first ref, simply unlink it.
3442 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3443 cur->name, cur->name_len,
3444 &ow_inode, &ow_gen);
3448 ret = is_first_ref(sctx->parent_root,
3449 ow_inode, cur->dir, cur->name,
3454 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3459 ret = send_unlink(sctx, cur->full_path);
3466 * link/move the ref to the new place. If we have an orphan
3467 * inode, move it and update valid_path. If not, link or move
3468 * it depending on the inode mode.
3471 ret = send_rename(sctx, valid_path, cur->full_path);
3475 ret = fs_path_copy(valid_path, cur->full_path);
3479 if (S_ISDIR(sctx->cur_inode_mode)) {
3481 * Dirs can't be linked, so move it. For moved
3482 * dirs, we always have one new and one deleted
3483 * ref. The deleted ref is ignored later.
3485 ret = wait_for_parent_move(sctx, cur);
3489 ret = add_pending_dir_move(sctx,
3491 sctx->cur_inode_gen,
3495 ret = send_rename(sctx, valid_path,
3498 ret = fs_path_copy(valid_path,
3504 ret = send_link(sctx, cur->full_path,
3510 ret = dup_ref(cur, &check_dirs);
3515 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3517 * Check if we can already rmdir the directory. If not,
3518 * orphanize it. For every dir item inside that gets deleted
3519 * later, we do this check again and rmdir it then if possible.
3520 * See the use of check_dirs for more details.
3522 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3527 ret = send_rmdir(sctx, valid_path);
3530 } else if (!is_orphan) {
3531 ret = orphanize_inode(sctx, sctx->cur_ino,
3532 sctx->cur_inode_gen, valid_path);
3538 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3539 ret = dup_ref(cur, &check_dirs);
3543 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3544 !list_empty(&sctx->deleted_refs)) {
3546 * We have a moved dir. Add the old parent to check_dirs
3548 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3550 ret = dup_ref(cur, &check_dirs);
3553 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3555 * We have a non dir inode. Go through all deleted refs and
3556 * unlink them if they were not already overwritten by other
3559 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3560 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3561 sctx->cur_ino, sctx->cur_inode_gen,
3562 cur->name, cur->name_len);
3566 ret = send_unlink(sctx, cur->full_path);
3570 ret = dup_ref(cur, &check_dirs);
3575 * If the inode is still orphan, unlink the orphan. This may
3576 * happen when a previous inode did overwrite the first ref
3577 * of this inode and no new refs were added for the current
3578 * inode. Unlinking does not mean that the inode is deleted in
3579 * all cases. There may still be links to this inode in other
3583 ret = send_unlink(sctx, valid_path);
3590 * We did collect all parent dirs where cur_inode was once located. We
3591 * now go through all these dirs and check if they are pending for
3592 * deletion and if it's finally possible to perform the rmdir now.
3593 * We also update the inode stats of the parent dirs here.
3595 list_for_each_entry(cur, &check_dirs, list) {
3597 * In case we had refs into dirs that were not processed yet,
3598 * we don't need to do the utime and rmdir logic for these dirs.
3599 * The dir will be processed later.
3601 if (cur->dir > sctx->cur_ino)
3604 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3608 if (ret == inode_state_did_create ||
3609 ret == inode_state_no_change) {
3610 /* TODO delayed utimes */
3611 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3614 } else if (ret == inode_state_did_delete &&
3615 cur->dir != last_dir_ino_rm) {
3616 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3621 ret = get_cur_path(sctx, cur->dir,
3622 cur->dir_gen, valid_path);
3625 ret = send_rmdir(sctx, valid_path);
3628 last_dir_ino_rm = cur->dir;
3636 __free_recorded_refs(&check_dirs);
3637 free_recorded_refs(sctx);
3638 fs_path_free(valid_path);
3642 static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3643 struct fs_path *name, void *ctx, struct list_head *refs)
3646 struct send_ctx *sctx = ctx;
3650 p = fs_path_alloc();
3654 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
3659 ret = get_cur_path(sctx, dir, gen, p);
3662 ret = fs_path_add_path(p, name);
3666 ret = __record_ref(refs, dir, gen, p);
3674 static int __record_new_ref(int num, u64 dir, int index,
3675 struct fs_path *name,
3678 struct send_ctx *sctx = ctx;
3679 return record_ref(sctx->send_root, num, dir, index, name,
3680 ctx, &sctx->new_refs);
3684 static int __record_deleted_ref(int num, u64 dir, int index,
3685 struct fs_path *name,
3688 struct send_ctx *sctx = ctx;
3689 return record_ref(sctx->parent_root, num, dir, index, name,
3690 ctx, &sctx->deleted_refs);
3693 static int record_new_ref(struct send_ctx *sctx)
3697 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3698 sctx->cmp_key, 0, __record_new_ref, sctx);
3707 static int record_deleted_ref(struct send_ctx *sctx)
3711 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3712 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3721 struct find_ref_ctx {
3724 struct btrfs_root *root;
3725 struct fs_path *name;
3729 static int __find_iref(int num, u64 dir, int index,
3730 struct fs_path *name,
3733 struct find_ref_ctx *ctx = ctx_;
3737 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3738 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3740 * To avoid doing extra lookups we'll only do this if everything
3743 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3747 if (dir_gen != ctx->dir_gen)
3749 ctx->found_idx = num;
3755 static int find_iref(struct btrfs_root *root,
3756 struct btrfs_path *path,
3757 struct btrfs_key *key,
3758 u64 dir, u64 dir_gen, struct fs_path *name)
3761 struct find_ref_ctx ctx;
3765 ctx.dir_gen = dir_gen;
3769 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3773 if (ctx.found_idx == -1)
3776 return ctx.found_idx;
3779 static int __record_changed_new_ref(int num, u64 dir, int index,
3780 struct fs_path *name,
3785 struct send_ctx *sctx = ctx;
3787 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3792 ret = find_iref(sctx->parent_root, sctx->right_path,
3793 sctx->cmp_key, dir, dir_gen, name);
3795 ret = __record_new_ref(num, dir, index, name, sctx);
3802 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3803 struct fs_path *name,
3808 struct send_ctx *sctx = ctx;
3810 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3815 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3816 dir, dir_gen, name);
3818 ret = __record_deleted_ref(num, dir, index, name, sctx);
3825 static int record_changed_ref(struct send_ctx *sctx)
3829 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3830 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3833 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3834 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3844 * Record and process all refs at once. Needed when an inode changes the
3845 * generation number, which means that it was deleted and recreated.
3847 static int process_all_refs(struct send_ctx *sctx,
3848 enum btrfs_compare_tree_result cmd)
3851 struct btrfs_root *root;
3852 struct btrfs_path *path;
3853 struct btrfs_key key;
3854 struct btrfs_key found_key;
3855 struct extent_buffer *eb;
3857 iterate_inode_ref_t cb;
3858 int pending_move = 0;
3860 path = alloc_path_for_send();
3864 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3865 root = sctx->send_root;
3866 cb = __record_new_ref;
3867 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3868 root = sctx->parent_root;
3869 cb = __record_deleted_ref;
3871 btrfs_err(sctx->send_root->fs_info,
3872 "Wrong command %d in process_all_refs", cmd);
3877 key.objectid = sctx->cmp_key->objectid;
3878 key.type = BTRFS_INODE_REF_KEY;
3880 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3885 eb = path->nodes[0];
3886 slot = path->slots[0];
3887 if (slot >= btrfs_header_nritems(eb)) {
3888 ret = btrfs_next_leaf(root, path);
3896 btrfs_item_key_to_cpu(eb, &found_key, slot);
3898 if (found_key.objectid != key.objectid ||
3899 (found_key.type != BTRFS_INODE_REF_KEY &&
3900 found_key.type != BTRFS_INODE_EXTREF_KEY))
3903 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
3909 btrfs_release_path(path);
3911 ret = process_recorded_refs(sctx, &pending_move);
3912 /* Only applicable to an incremental send. */
3913 ASSERT(pending_move == 0);
3916 btrfs_free_path(path);
3920 static int send_set_xattr(struct send_ctx *sctx,
3921 struct fs_path *path,
3922 const char *name, int name_len,
3923 const char *data, int data_len)
3927 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3931 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3932 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3933 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3935 ret = send_cmd(sctx);
3942 static int send_remove_xattr(struct send_ctx *sctx,
3943 struct fs_path *path,
3944 const char *name, int name_len)
3948 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3952 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3953 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3955 ret = send_cmd(sctx);
3962 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3963 const char *name, int name_len,
3964 const char *data, int data_len,
3968 struct send_ctx *sctx = ctx;
3970 posix_acl_xattr_header dummy_acl;
3972 p = fs_path_alloc();
3977 * This hack is needed because empty acl's are stored as zero byte
3978 * data in xattrs. Problem with that is, that receiving these zero byte
3979 * acl's will fail later. To fix this, we send a dummy acl list that
3980 * only contains the version number and no entries.
3982 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3983 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3984 if (data_len == 0) {
3985 dummy_acl.a_version =
3986 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3987 data = (char *)&dummy_acl;
3988 data_len = sizeof(dummy_acl);
3992 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3996 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4003 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4004 const char *name, int name_len,
4005 const char *data, int data_len,
4009 struct send_ctx *sctx = ctx;
4012 p = fs_path_alloc();
4016 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4020 ret = send_remove_xattr(sctx, p, name, name_len);
4027 static int process_new_xattr(struct send_ctx *sctx)
4031 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4032 sctx->cmp_key, __process_new_xattr, sctx);
4037 static int process_deleted_xattr(struct send_ctx *sctx)
4041 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4042 sctx->cmp_key, __process_deleted_xattr, sctx);
4047 struct find_xattr_ctx {
4055 static int __find_xattr(int num, struct btrfs_key *di_key,
4056 const char *name, int name_len,
4057 const char *data, int data_len,
4058 u8 type, void *vctx)
4060 struct find_xattr_ctx *ctx = vctx;
4062 if (name_len == ctx->name_len &&
4063 strncmp(name, ctx->name, name_len) == 0) {
4064 ctx->found_idx = num;
4065 ctx->found_data_len = data_len;
4066 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
4067 if (!ctx->found_data)
4074 static int find_xattr(struct btrfs_root *root,
4075 struct btrfs_path *path,
4076 struct btrfs_key *key,
4077 const char *name, int name_len,
4078 char **data, int *data_len)
4081 struct find_xattr_ctx ctx;
4084 ctx.name_len = name_len;
4086 ctx.found_data = NULL;
4087 ctx.found_data_len = 0;
4089 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
4093 if (ctx.found_idx == -1)
4096 *data = ctx.found_data;
4097 *data_len = ctx.found_data_len;
4099 kfree(ctx.found_data);
4101 return ctx.found_idx;
4105 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4106 const char *name, int name_len,
4107 const char *data, int data_len,
4111 struct send_ctx *sctx = ctx;
4112 char *found_data = NULL;
4113 int found_data_len = 0;
4115 ret = find_xattr(sctx->parent_root, sctx->right_path,
4116 sctx->cmp_key, name, name_len, &found_data,
4118 if (ret == -ENOENT) {
4119 ret = __process_new_xattr(num, di_key, name, name_len, data,
4120 data_len, type, ctx);
4121 } else if (ret >= 0) {
4122 if (data_len != found_data_len ||
4123 memcmp(data, found_data, data_len)) {
4124 ret = __process_new_xattr(num, di_key, name, name_len,
4125 data, data_len, type, ctx);
4135 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4136 const char *name, int name_len,
4137 const char *data, int data_len,
4141 struct send_ctx *sctx = ctx;
4143 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4144 name, name_len, NULL, NULL);
4146 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4147 data_len, type, ctx);
4154 static int process_changed_xattr(struct send_ctx *sctx)
4158 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4159 sctx->cmp_key, __process_changed_new_xattr, sctx);
4162 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4163 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4169 static int process_all_new_xattrs(struct send_ctx *sctx)
4172 struct btrfs_root *root;
4173 struct btrfs_path *path;
4174 struct btrfs_key key;
4175 struct btrfs_key found_key;
4176 struct extent_buffer *eb;
4179 path = alloc_path_for_send();
4183 root = sctx->send_root;
4185 key.objectid = sctx->cmp_key->objectid;
4186 key.type = BTRFS_XATTR_ITEM_KEY;
4188 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4193 eb = path->nodes[0];
4194 slot = path->slots[0];
4195 if (slot >= btrfs_header_nritems(eb)) {
4196 ret = btrfs_next_leaf(root, path);
4199 } else if (ret > 0) {
4206 btrfs_item_key_to_cpu(eb, &found_key, slot);
4207 if (found_key.objectid != key.objectid ||
4208 found_key.type != key.type) {
4213 ret = iterate_dir_item(root, path, &found_key,
4214 __process_new_xattr, sctx);
4222 btrfs_free_path(path);
4226 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4228 struct btrfs_root *root = sctx->send_root;
4229 struct btrfs_fs_info *fs_info = root->fs_info;
4230 struct inode *inode;
4233 struct btrfs_key key;
4234 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4236 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4239 key.objectid = sctx->cur_ino;
4240 key.type = BTRFS_INODE_ITEM_KEY;
4243 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4245 return PTR_ERR(inode);
4247 if (offset + len > i_size_read(inode)) {
4248 if (offset > i_size_read(inode))
4251 len = offset - i_size_read(inode);
4256 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
4258 /* initial readahead */
4259 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4260 file_ra_state_init(&sctx->ra, inode->i_mapping);
4261 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4262 last_index - index + 1);
4264 while (index <= last_index) {
4265 unsigned cur_len = min_t(unsigned, len,
4266 PAGE_CACHE_SIZE - pg_offset);
4267 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4273 if (!PageUptodate(page)) {
4274 btrfs_readpage(NULL, page);
4276 if (!PageUptodate(page)) {
4278 page_cache_release(page);
4285 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4288 page_cache_release(page);
4300 * Read some bytes from the current inode/file and send a write command to
4303 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4307 ssize_t num_read = 0;
4309 p = fs_path_alloc();
4313 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4315 num_read = fill_read_buf(sctx, offset, len);
4316 if (num_read <= 0) {
4322 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4326 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4330 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4331 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4332 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
4334 ret = send_cmd(sctx);
4345 * Send a clone command to user space.
4347 static int send_clone(struct send_ctx *sctx,
4348 u64 offset, u32 len,
4349 struct clone_root *clone_root)
4355 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4356 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4357 clone_root->root->objectid, clone_root->ino,
4358 clone_root->offset);
4360 p = fs_path_alloc();
4364 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4368 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4372 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4373 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4374 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4376 if (clone_root->root == sctx->send_root) {
4377 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
4378 &gen, NULL, NULL, NULL, NULL);
4381 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4383 ret = get_inode_path(clone_root->root, clone_root->ino, p);
4388 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4389 clone_root->root->root_item.uuid);
4390 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
4391 le64_to_cpu(clone_root->root->root_item.ctransid));
4392 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4393 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4394 clone_root->offset);
4396 ret = send_cmd(sctx);
4405 * Send an update extent command to user space.
4407 static int send_update_extent(struct send_ctx *sctx,
4408 u64 offset, u32 len)
4413 p = fs_path_alloc();
4417 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4421 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4425 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4426 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4427 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4429 ret = send_cmd(sctx);
4437 static int send_hole(struct send_ctx *sctx, u64 end)
4439 struct fs_path *p = NULL;
4440 u64 offset = sctx->cur_inode_last_extent;
4444 p = fs_path_alloc();
4447 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4449 goto tlv_put_failure;
4450 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4451 while (offset < end) {
4452 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4454 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4457 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4458 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4459 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4460 ret = send_cmd(sctx);
4470 static int send_write_or_clone(struct send_ctx *sctx,
4471 struct btrfs_path *path,
4472 struct btrfs_key *key,
4473 struct clone_root *clone_root)
4476 struct btrfs_file_extent_item *ei;
4477 u64 offset = key->offset;
4482 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
4484 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4485 struct btrfs_file_extent_item);
4486 type = btrfs_file_extent_type(path->nodes[0], ei);
4487 if (type == BTRFS_FILE_EXTENT_INLINE) {
4488 len = btrfs_file_extent_inline_len(path->nodes[0],
4489 path->slots[0], ei);
4491 * it is possible the inline item won't cover the whole page,
4492 * but there may be items after this page. Make
4493 * sure to send the whole thing
4495 len = PAGE_CACHE_ALIGN(len);
4497 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
4500 if (offset + len > sctx->cur_inode_size)
4501 len = sctx->cur_inode_size - offset;
4507 if (clone_root && IS_ALIGNED(offset + len, bs)) {
4508 ret = send_clone(sctx, offset, len, clone_root);
4509 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4510 ret = send_update_extent(sctx, offset, len);
4514 if (l > BTRFS_SEND_READ_SIZE)
4515 l = BTRFS_SEND_READ_SIZE;
4516 ret = send_write(sctx, pos + offset, l);
4529 static int is_extent_unchanged(struct send_ctx *sctx,
4530 struct btrfs_path *left_path,
4531 struct btrfs_key *ekey)
4534 struct btrfs_key key;
4535 struct btrfs_path *path = NULL;
4536 struct extent_buffer *eb;
4538 struct btrfs_key found_key;
4539 struct btrfs_file_extent_item *ei;
4544 u64 left_offset_fixed;
4552 path = alloc_path_for_send();
4556 eb = left_path->nodes[0];
4557 slot = left_path->slots[0];
4558 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4559 left_type = btrfs_file_extent_type(eb, ei);
4561 if (left_type != BTRFS_FILE_EXTENT_REG) {
4565 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4566 left_len = btrfs_file_extent_num_bytes(eb, ei);
4567 left_offset = btrfs_file_extent_offset(eb, ei);
4568 left_gen = btrfs_file_extent_generation(eb, ei);
4571 * Following comments will refer to these graphics. L is the left
4572 * extents which we are checking at the moment. 1-8 are the right
4573 * extents that we iterate.
4576 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4579 * |--1--|-2b-|...(same as above)
4581 * Alternative situation. Happens on files where extents got split.
4583 * |-----------7-----------|-6-|
4585 * Alternative situation. Happens on files which got larger.
4588 * Nothing follows after 8.
4591 key.objectid = ekey->objectid;
4592 key.type = BTRFS_EXTENT_DATA_KEY;
4593 key.offset = ekey->offset;
4594 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4603 * Handle special case where the right side has no extents at all.
4605 eb = path->nodes[0];
4606 slot = path->slots[0];
4607 btrfs_item_key_to_cpu(eb, &found_key, slot);
4608 if (found_key.objectid != key.objectid ||
4609 found_key.type != key.type) {
4610 /* If we're a hole then just pretend nothing changed */
4611 ret = (left_disknr) ? 0 : 1;
4616 * We're now on 2a, 2b or 7.
4619 while (key.offset < ekey->offset + left_len) {
4620 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4621 right_type = btrfs_file_extent_type(eb, ei);
4622 if (right_type != BTRFS_FILE_EXTENT_REG) {
4627 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4628 right_len = btrfs_file_extent_num_bytes(eb, ei);
4629 right_offset = btrfs_file_extent_offset(eb, ei);
4630 right_gen = btrfs_file_extent_generation(eb, ei);
4633 * Are we at extent 8? If yes, we know the extent is changed.
4634 * This may only happen on the first iteration.
4636 if (found_key.offset + right_len <= ekey->offset) {
4637 /* If we're a hole just pretend nothing changed */
4638 ret = (left_disknr) ? 0 : 1;
4642 left_offset_fixed = left_offset;
4643 if (key.offset < ekey->offset) {
4644 /* Fix the right offset for 2a and 7. */
4645 right_offset += ekey->offset - key.offset;
4647 /* Fix the left offset for all behind 2a and 2b */
4648 left_offset_fixed += key.offset - ekey->offset;
4652 * Check if we have the same extent.
4654 if (left_disknr != right_disknr ||
4655 left_offset_fixed != right_offset ||
4656 left_gen != right_gen) {
4662 * Go to the next extent.
4664 ret = btrfs_next_item(sctx->parent_root, path);
4668 eb = path->nodes[0];
4669 slot = path->slots[0];
4670 btrfs_item_key_to_cpu(eb, &found_key, slot);
4672 if (ret || found_key.objectid != key.objectid ||
4673 found_key.type != key.type) {
4674 key.offset += right_len;
4677 if (found_key.offset != key.offset + right_len) {
4685 * We're now behind the left extent (treat as unchanged) or at the end
4686 * of the right side (treat as changed).
4688 if (key.offset >= ekey->offset + left_len)
4695 btrfs_free_path(path);
4699 static int get_last_extent(struct send_ctx *sctx, u64 offset)
4701 struct btrfs_path *path;
4702 struct btrfs_root *root = sctx->send_root;
4703 struct btrfs_file_extent_item *fi;
4704 struct btrfs_key key;
4709 path = alloc_path_for_send();
4713 sctx->cur_inode_last_extent = 0;
4715 key.objectid = sctx->cur_ino;
4716 key.type = BTRFS_EXTENT_DATA_KEY;
4717 key.offset = offset;
4718 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4722 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4723 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4726 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4727 struct btrfs_file_extent_item);
4728 type = btrfs_file_extent_type(path->nodes[0], fi);
4729 if (type == BTRFS_FILE_EXTENT_INLINE) {
4730 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4731 path->slots[0], fi);
4732 extent_end = ALIGN(key.offset + size,
4733 sctx->send_root->sectorsize);
4735 extent_end = key.offset +
4736 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4738 sctx->cur_inode_last_extent = extent_end;
4740 btrfs_free_path(path);
4744 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4745 struct btrfs_key *key)
4747 struct btrfs_file_extent_item *fi;
4752 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4755 if (sctx->cur_inode_last_extent == (u64)-1) {
4756 ret = get_last_extent(sctx, key->offset - 1);
4761 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4762 struct btrfs_file_extent_item);
4763 type = btrfs_file_extent_type(path->nodes[0], fi);
4764 if (type == BTRFS_FILE_EXTENT_INLINE) {
4765 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4766 path->slots[0], fi);
4767 extent_end = ALIGN(key->offset + size,
4768 sctx->send_root->sectorsize);
4770 extent_end = key->offset +
4771 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4774 if (path->slots[0] == 0 &&
4775 sctx->cur_inode_last_extent < key->offset) {
4777 * We might have skipped entire leafs that contained only
4778 * file extent items for our current inode. These leafs have
4779 * a generation number smaller (older) than the one in the
4780 * current leaf and the leaf our last extent came from, and
4781 * are located between these 2 leafs.
4783 ret = get_last_extent(sctx, key->offset - 1);
4788 if (sctx->cur_inode_last_extent < key->offset)
4789 ret = send_hole(sctx, key->offset);
4790 sctx->cur_inode_last_extent = extent_end;
4794 static int process_extent(struct send_ctx *sctx,
4795 struct btrfs_path *path,
4796 struct btrfs_key *key)
4798 struct clone_root *found_clone = NULL;
4801 if (S_ISLNK(sctx->cur_inode_mode))
4804 if (sctx->parent_root && !sctx->cur_inode_new) {
4805 ret = is_extent_unchanged(sctx, path, key);
4813 struct btrfs_file_extent_item *ei;
4816 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4817 struct btrfs_file_extent_item);
4818 type = btrfs_file_extent_type(path->nodes[0], ei);
4819 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4820 type == BTRFS_FILE_EXTENT_REG) {
4822 * The send spec does not have a prealloc command yet,
4823 * so just leave a hole for prealloc'ed extents until
4824 * we have enough commands queued up to justify rev'ing
4827 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4832 /* Have a hole, just skip it. */
4833 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4840 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4841 sctx->cur_inode_size, &found_clone);
4842 if (ret != -ENOENT && ret < 0)
4845 ret = send_write_or_clone(sctx, path, key, found_clone);
4849 ret = maybe_send_hole(sctx, path, key);
4854 static int process_all_extents(struct send_ctx *sctx)
4857 struct btrfs_root *root;
4858 struct btrfs_path *path;
4859 struct btrfs_key key;
4860 struct btrfs_key found_key;
4861 struct extent_buffer *eb;
4864 root = sctx->send_root;
4865 path = alloc_path_for_send();
4869 key.objectid = sctx->cmp_key->objectid;
4870 key.type = BTRFS_EXTENT_DATA_KEY;
4872 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4877 eb = path->nodes[0];
4878 slot = path->slots[0];
4880 if (slot >= btrfs_header_nritems(eb)) {
4881 ret = btrfs_next_leaf(root, path);
4884 } else if (ret > 0) {
4891 btrfs_item_key_to_cpu(eb, &found_key, slot);
4893 if (found_key.objectid != key.objectid ||
4894 found_key.type != key.type) {
4899 ret = process_extent(sctx, path, &found_key);
4907 btrfs_free_path(path);
4911 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4913 int *refs_processed)
4917 if (sctx->cur_ino == 0)
4919 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4920 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4922 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4925 ret = process_recorded_refs(sctx, pending_move);
4929 *refs_processed = 1;
4934 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4945 int pending_move = 0;
4946 int refs_processed = 0;
4948 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4954 * We have processed the refs and thus need to advance send_progress.
4955 * Now, calls to get_cur_xxx will take the updated refs of the current
4956 * inode into account.
4958 * On the other hand, if our current inode is a directory and couldn't
4959 * be moved/renamed because its parent was renamed/moved too and it has
4960 * a higher inode number, we can only move/rename our current inode
4961 * after we moved/renamed its parent. Therefore in this case operate on
4962 * the old path (pre move/rename) of our current inode, and the
4963 * move/rename will be performed later.
4965 if (refs_processed && !pending_move)
4966 sctx->send_progress = sctx->cur_ino + 1;
4968 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4970 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4973 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4974 &left_mode, &left_uid, &left_gid, NULL);
4978 if (!sctx->parent_root || sctx->cur_inode_new) {
4980 if (!S_ISLNK(sctx->cur_inode_mode))
4983 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4984 NULL, NULL, &right_mode, &right_uid,
4989 if (left_uid != right_uid || left_gid != right_gid)
4991 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4995 if (S_ISREG(sctx->cur_inode_mode)) {
4996 if (need_send_hole(sctx)) {
4997 if (sctx->cur_inode_last_extent == (u64)-1 ||
4998 sctx->cur_inode_last_extent <
4999 sctx->cur_inode_size) {
5000 ret = get_last_extent(sctx, (u64)-1);
5004 if (sctx->cur_inode_last_extent <
5005 sctx->cur_inode_size) {
5006 ret = send_hole(sctx, sctx->cur_inode_size);
5011 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5012 sctx->cur_inode_size);
5018 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5019 left_uid, left_gid);
5024 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5031 * If other directory inodes depended on our current directory
5032 * inode's move/rename, now do their move/rename operations.
5034 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5035 ret = apply_children_dir_moves(sctx);
5039 * Need to send that every time, no matter if it actually
5040 * changed between the two trees as we have done changes to
5041 * the inode before. If our inode is a directory and it's
5042 * waiting to be moved/renamed, we will send its utimes when
5043 * it's moved/renamed, therefore we don't need to do it here.
5045 sctx->send_progress = sctx->cur_ino + 1;
5046 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5055 static int changed_inode(struct send_ctx *sctx,
5056 enum btrfs_compare_tree_result result)
5059 struct btrfs_key *key = sctx->cmp_key;
5060 struct btrfs_inode_item *left_ii = NULL;
5061 struct btrfs_inode_item *right_ii = NULL;
5065 sctx->cur_ino = key->objectid;
5066 sctx->cur_inode_new_gen = 0;
5067 sctx->cur_inode_last_extent = (u64)-1;
5070 * Set send_progress to current inode. This will tell all get_cur_xxx
5071 * functions that the current inode's refs are not updated yet. Later,
5072 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5074 sctx->send_progress = sctx->cur_ino;
5076 if (result == BTRFS_COMPARE_TREE_NEW ||
5077 result == BTRFS_COMPARE_TREE_CHANGED) {
5078 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5079 sctx->left_path->slots[0],
5080 struct btrfs_inode_item);
5081 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5084 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5085 sctx->right_path->slots[0],
5086 struct btrfs_inode_item);
5087 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5090 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5091 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5092 sctx->right_path->slots[0],
5093 struct btrfs_inode_item);
5095 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5099 * The cur_ino = root dir case is special here. We can't treat
5100 * the inode as deleted+reused because it would generate a
5101 * stream that tries to delete/mkdir the root dir.
5103 if (left_gen != right_gen &&
5104 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5105 sctx->cur_inode_new_gen = 1;
5108 if (result == BTRFS_COMPARE_TREE_NEW) {
5109 sctx->cur_inode_gen = left_gen;
5110 sctx->cur_inode_new = 1;
5111 sctx->cur_inode_deleted = 0;
5112 sctx->cur_inode_size = btrfs_inode_size(
5113 sctx->left_path->nodes[0], left_ii);
5114 sctx->cur_inode_mode = btrfs_inode_mode(
5115 sctx->left_path->nodes[0], left_ii);
5116 sctx->cur_inode_rdev = btrfs_inode_rdev(
5117 sctx->left_path->nodes[0], left_ii);
5118 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5119 ret = send_create_inode_if_needed(sctx);
5120 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5121 sctx->cur_inode_gen = right_gen;
5122 sctx->cur_inode_new = 0;
5123 sctx->cur_inode_deleted = 1;
5124 sctx->cur_inode_size = btrfs_inode_size(
5125 sctx->right_path->nodes[0], right_ii);
5126 sctx->cur_inode_mode = btrfs_inode_mode(
5127 sctx->right_path->nodes[0], right_ii);
5128 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
5130 * We need to do some special handling in case the inode was
5131 * reported as changed with a changed generation number. This
5132 * means that the original inode was deleted and new inode
5133 * reused the same inum. So we have to treat the old inode as
5134 * deleted and the new one as new.
5136 if (sctx->cur_inode_new_gen) {
5138 * First, process the inode as if it was deleted.
5140 sctx->cur_inode_gen = right_gen;
5141 sctx->cur_inode_new = 0;
5142 sctx->cur_inode_deleted = 1;
5143 sctx->cur_inode_size = btrfs_inode_size(
5144 sctx->right_path->nodes[0], right_ii);
5145 sctx->cur_inode_mode = btrfs_inode_mode(
5146 sctx->right_path->nodes[0], right_ii);
5147 ret = process_all_refs(sctx,
5148 BTRFS_COMPARE_TREE_DELETED);
5153 * Now process the inode as if it was new.
5155 sctx->cur_inode_gen = left_gen;
5156 sctx->cur_inode_new = 1;
5157 sctx->cur_inode_deleted = 0;
5158 sctx->cur_inode_size = btrfs_inode_size(
5159 sctx->left_path->nodes[0], left_ii);
5160 sctx->cur_inode_mode = btrfs_inode_mode(
5161 sctx->left_path->nodes[0], left_ii);
5162 sctx->cur_inode_rdev = btrfs_inode_rdev(
5163 sctx->left_path->nodes[0], left_ii);
5164 ret = send_create_inode_if_needed(sctx);
5168 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5172 * Advance send_progress now as we did not get into
5173 * process_recorded_refs_if_needed in the new_gen case.
5175 sctx->send_progress = sctx->cur_ino + 1;
5178 * Now process all extents and xattrs of the inode as if
5179 * they were all new.
5181 ret = process_all_extents(sctx);
5184 ret = process_all_new_xattrs(sctx);
5188 sctx->cur_inode_gen = left_gen;
5189 sctx->cur_inode_new = 0;
5190 sctx->cur_inode_new_gen = 0;
5191 sctx->cur_inode_deleted = 0;
5192 sctx->cur_inode_size = btrfs_inode_size(
5193 sctx->left_path->nodes[0], left_ii);
5194 sctx->cur_inode_mode = btrfs_inode_mode(
5195 sctx->left_path->nodes[0], left_ii);
5204 * We have to process new refs before deleted refs, but compare_trees gives us
5205 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5206 * first and later process them in process_recorded_refs.
5207 * For the cur_inode_new_gen case, we skip recording completely because
5208 * changed_inode did already initiate processing of refs. The reason for this is
5209 * that in this case, compare_tree actually compares the refs of 2 different
5210 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5211 * refs of the right tree as deleted and all refs of the left tree as new.
5213 static int changed_ref(struct send_ctx *sctx,
5214 enum btrfs_compare_tree_result result)
5218 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5220 if (!sctx->cur_inode_new_gen &&
5221 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5222 if (result == BTRFS_COMPARE_TREE_NEW)
5223 ret = record_new_ref(sctx);
5224 else if (result == BTRFS_COMPARE_TREE_DELETED)
5225 ret = record_deleted_ref(sctx);
5226 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5227 ret = record_changed_ref(sctx);
5234 * Process new/deleted/changed xattrs. We skip processing in the
5235 * cur_inode_new_gen case because changed_inode did already initiate processing
5236 * of xattrs. The reason is the same as in changed_ref
5238 static int changed_xattr(struct send_ctx *sctx,
5239 enum btrfs_compare_tree_result result)
5243 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5245 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5246 if (result == BTRFS_COMPARE_TREE_NEW)
5247 ret = process_new_xattr(sctx);
5248 else if (result == BTRFS_COMPARE_TREE_DELETED)
5249 ret = process_deleted_xattr(sctx);
5250 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5251 ret = process_changed_xattr(sctx);
5258 * Process new/deleted/changed extents. We skip processing in the
5259 * cur_inode_new_gen case because changed_inode did already initiate processing
5260 * of extents. The reason is the same as in changed_ref
5262 static int changed_extent(struct send_ctx *sctx,
5263 enum btrfs_compare_tree_result result)
5267 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5269 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5270 if (result != BTRFS_COMPARE_TREE_DELETED)
5271 ret = process_extent(sctx, sctx->left_path,
5278 static int dir_changed(struct send_ctx *sctx, u64 dir)
5280 u64 orig_gen, new_gen;
5283 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5288 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5293 return (orig_gen != new_gen) ? 1 : 0;
5296 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5297 struct btrfs_key *key)
5299 struct btrfs_inode_extref *extref;
5300 struct extent_buffer *leaf;
5301 u64 dirid = 0, last_dirid = 0;
5308 /* Easy case, just check this one dirid */
5309 if (key->type == BTRFS_INODE_REF_KEY) {
5310 dirid = key->offset;
5312 ret = dir_changed(sctx, dirid);
5316 leaf = path->nodes[0];
5317 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5318 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5319 while (cur_offset < item_size) {
5320 extref = (struct btrfs_inode_extref *)(ptr +
5322 dirid = btrfs_inode_extref_parent(leaf, extref);
5323 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5324 cur_offset += ref_name_len + sizeof(*extref);
5325 if (dirid == last_dirid)
5327 ret = dir_changed(sctx, dirid);
5337 * Updates compare related fields in sctx and simply forwards to the actual
5338 * changed_xxx functions.
5340 static int changed_cb(struct btrfs_root *left_root,
5341 struct btrfs_root *right_root,
5342 struct btrfs_path *left_path,
5343 struct btrfs_path *right_path,
5344 struct btrfs_key *key,
5345 enum btrfs_compare_tree_result result,
5349 struct send_ctx *sctx = ctx;
5351 if (result == BTRFS_COMPARE_TREE_SAME) {
5352 if (key->type == BTRFS_INODE_REF_KEY ||
5353 key->type == BTRFS_INODE_EXTREF_KEY) {
5354 ret = compare_refs(sctx, left_path, key);
5359 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5360 return maybe_send_hole(sctx, left_path, key);
5364 result = BTRFS_COMPARE_TREE_CHANGED;
5368 sctx->left_path = left_path;
5369 sctx->right_path = right_path;
5370 sctx->cmp_key = key;
5372 ret = finish_inode_if_needed(sctx, 0);
5376 /* Ignore non-FS objects */
5377 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5378 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5381 if (key->type == BTRFS_INODE_ITEM_KEY)
5382 ret = changed_inode(sctx, result);
5383 else if (key->type == BTRFS_INODE_REF_KEY ||
5384 key->type == BTRFS_INODE_EXTREF_KEY)
5385 ret = changed_ref(sctx, result);
5386 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5387 ret = changed_xattr(sctx, result);
5388 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5389 ret = changed_extent(sctx, result);
5395 static int full_send_tree(struct send_ctx *sctx)
5398 struct btrfs_root *send_root = sctx->send_root;
5399 struct btrfs_key key;
5400 struct btrfs_key found_key;
5401 struct btrfs_path *path;
5402 struct extent_buffer *eb;
5405 path = alloc_path_for_send();
5409 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5410 key.type = BTRFS_INODE_ITEM_KEY;
5413 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5420 eb = path->nodes[0];
5421 slot = path->slots[0];
5422 btrfs_item_key_to_cpu(eb, &found_key, slot);
5424 ret = changed_cb(send_root, NULL, path, NULL,
5425 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5429 key.objectid = found_key.objectid;
5430 key.type = found_key.type;
5431 key.offset = found_key.offset + 1;
5433 ret = btrfs_next_item(send_root, path);
5443 ret = finish_inode_if_needed(sctx, 1);
5446 btrfs_free_path(path);
5450 static int send_subvol(struct send_ctx *sctx)
5454 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5455 ret = send_header(sctx);
5460 ret = send_subvol_begin(sctx);
5464 if (sctx->parent_root) {
5465 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5469 ret = finish_inode_if_needed(sctx, 1);
5473 ret = full_send_tree(sctx);
5479 free_recorded_refs(sctx);
5483 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5485 spin_lock(&root->root_item_lock);
5486 root->send_in_progress--;
5488 * Not much left to do, we don't know why it's unbalanced and
5489 * can't blindly reset it to 0.
5491 if (root->send_in_progress < 0)
5492 btrfs_err(root->fs_info,
5493 "send_in_progres unbalanced %d root %llu\n",
5494 root->send_in_progress, root->root_key.objectid);
5495 spin_unlock(&root->root_item_lock);
5498 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5501 struct btrfs_root *send_root;
5502 struct btrfs_root *clone_root;
5503 struct btrfs_fs_info *fs_info;
5504 struct btrfs_ioctl_send_args *arg = NULL;
5505 struct btrfs_key key;
5506 struct send_ctx *sctx = NULL;
5508 u64 *clone_sources_tmp = NULL;
5509 int clone_sources_to_rollback = 0;
5510 int sort_clone_roots = 0;
5513 if (!capable(CAP_SYS_ADMIN))
5516 send_root = BTRFS_I(file_inode(mnt_file))->root;
5517 fs_info = send_root->fs_info;
5520 * The subvolume must remain read-only during send, protect against
5523 spin_lock(&send_root->root_item_lock);
5524 send_root->send_in_progress++;
5525 spin_unlock(&send_root->root_item_lock);
5528 * This is done when we lookup the root, it should already be complete
5529 * by the time we get here.
5531 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5534 * Userspace tools do the checks and warn the user if it's
5537 if (!btrfs_root_readonly(send_root)) {
5542 arg = memdup_user(arg_, sizeof(*arg));
5549 if (!access_ok(VERIFY_READ, arg->clone_sources,
5550 sizeof(*arg->clone_sources) *
5551 arg->clone_sources_count)) {
5556 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
5561 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5567 INIT_LIST_HEAD(&sctx->new_refs);
5568 INIT_LIST_HEAD(&sctx->deleted_refs);
5569 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5570 INIT_LIST_HEAD(&sctx->name_cache_list);
5572 sctx->flags = arg->flags;
5574 sctx->send_filp = fget(arg->send_fd);
5575 if (!sctx->send_filp) {
5580 sctx->send_root = send_root;
5581 sctx->clone_roots_cnt = arg->clone_sources_count;
5583 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5584 sctx->send_buf = vmalloc(sctx->send_max_size);
5585 if (!sctx->send_buf) {
5590 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5591 if (!sctx->read_buf) {
5596 sctx->pending_dir_moves = RB_ROOT;
5597 sctx->waiting_dir_moves = RB_ROOT;
5598 sctx->orphan_dirs = RB_ROOT;
5600 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5601 (arg->clone_sources_count + 1));
5602 if (!sctx->clone_roots) {
5607 if (arg->clone_sources_count) {
5608 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5609 sizeof(*arg->clone_sources));
5610 if (!clone_sources_tmp) {
5615 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5616 arg->clone_sources_count *
5617 sizeof(*arg->clone_sources));
5623 for (i = 0; i < arg->clone_sources_count; i++) {
5624 key.objectid = clone_sources_tmp[i];
5625 key.type = BTRFS_ROOT_ITEM_KEY;
5626 key.offset = (u64)-1;
5628 index = srcu_read_lock(&fs_info->subvol_srcu);
5630 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
5631 if (IS_ERR(clone_root)) {
5632 srcu_read_unlock(&fs_info->subvol_srcu, index);
5633 ret = PTR_ERR(clone_root);
5636 clone_sources_to_rollback = i + 1;
5637 spin_lock(&clone_root->root_item_lock);
5638 clone_root->send_in_progress++;
5639 if (!btrfs_root_readonly(clone_root)) {
5640 spin_unlock(&clone_root->root_item_lock);
5641 srcu_read_unlock(&fs_info->subvol_srcu, index);
5645 spin_unlock(&clone_root->root_item_lock);
5646 srcu_read_unlock(&fs_info->subvol_srcu, index);
5648 sctx->clone_roots[i].root = clone_root;
5650 vfree(clone_sources_tmp);
5651 clone_sources_tmp = NULL;
5654 if (arg->parent_root) {
5655 key.objectid = arg->parent_root;
5656 key.type = BTRFS_ROOT_ITEM_KEY;
5657 key.offset = (u64)-1;
5659 index = srcu_read_lock(&fs_info->subvol_srcu);
5661 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
5662 if (IS_ERR(sctx->parent_root)) {
5663 srcu_read_unlock(&fs_info->subvol_srcu, index);
5664 ret = PTR_ERR(sctx->parent_root);
5668 spin_lock(&sctx->parent_root->root_item_lock);
5669 sctx->parent_root->send_in_progress++;
5670 if (!btrfs_root_readonly(sctx->parent_root)) {
5671 spin_unlock(&sctx->parent_root->root_item_lock);
5672 srcu_read_unlock(&fs_info->subvol_srcu, index);
5676 spin_unlock(&sctx->parent_root->root_item_lock);
5678 srcu_read_unlock(&fs_info->subvol_srcu, index);
5682 * Clones from send_root are allowed, but only if the clone source
5683 * is behind the current send position. This is checked while searching
5684 * for possible clone sources.
5686 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5688 /* We do a bsearch later */
5689 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5690 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5692 sort_clone_roots = 1;
5694 current->journal_info = (void *)BTRFS_SEND_TRANS_STUB;
5695 ret = send_subvol(sctx);
5696 current->journal_info = NULL;
5700 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5701 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5704 ret = send_cmd(sctx);
5710 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5711 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5713 struct pending_dir_move *pm;
5715 n = rb_first(&sctx->pending_dir_moves);
5716 pm = rb_entry(n, struct pending_dir_move, node);
5717 while (!list_empty(&pm->list)) {
5718 struct pending_dir_move *pm2;
5720 pm2 = list_first_entry(&pm->list,
5721 struct pending_dir_move, list);
5722 free_pending_move(sctx, pm2);
5724 free_pending_move(sctx, pm);
5727 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5728 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5730 struct waiting_dir_move *dm;
5732 n = rb_first(&sctx->waiting_dir_moves);
5733 dm = rb_entry(n, struct waiting_dir_move, node);
5734 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5738 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5739 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5741 struct orphan_dir_info *odi;
5743 n = rb_first(&sctx->orphan_dirs);
5744 odi = rb_entry(n, struct orphan_dir_info, node);
5745 free_orphan_dir_info(sctx, odi);
5748 if (sort_clone_roots) {
5749 for (i = 0; i < sctx->clone_roots_cnt; i++)
5750 btrfs_root_dec_send_in_progress(
5751 sctx->clone_roots[i].root);
5753 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5754 btrfs_root_dec_send_in_progress(
5755 sctx->clone_roots[i].root);
5757 btrfs_root_dec_send_in_progress(send_root);
5759 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5760 btrfs_root_dec_send_in_progress(sctx->parent_root);
5763 vfree(clone_sources_tmp);
5766 if (sctx->send_filp)
5767 fput(sctx->send_filp);
5769 vfree(sctx->clone_roots);
5770 vfree(sctx->send_buf);
5771 vfree(sctx->read_buf);
5773 name_cache_free(sctx);