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, (__force const char __user *)buf + pos,
520 /* TODO handle that correctly */
521 /*if (ret == -ERESTARTSYS) {
540 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
542 struct btrfs_tlv_header *hdr;
543 int total_len = sizeof(*hdr) + len;
544 int left = sctx->send_max_size - sctx->send_size;
546 if (unlikely(left < total_len))
549 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
550 hdr->tlv_type = cpu_to_le16(attr);
551 hdr->tlv_len = cpu_to_le16(len);
552 memcpy(hdr + 1, data, len);
553 sctx->send_size += total_len;
558 #define TLV_PUT_DEFINE_INT(bits) \
559 static int tlv_put_u##bits(struct send_ctx *sctx, \
560 u##bits attr, u##bits value) \
562 __le##bits __tmp = cpu_to_le##bits(value); \
563 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
566 TLV_PUT_DEFINE_INT(64)
568 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
569 const char *str, int len)
573 return tlv_put(sctx, attr, str, len);
576 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
579 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
582 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
583 struct extent_buffer *eb,
584 struct btrfs_timespec *ts)
586 struct btrfs_timespec bts;
587 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
588 return tlv_put(sctx, attr, &bts, sizeof(bts));
592 #define TLV_PUT(sctx, attrtype, attrlen, data) \
594 ret = tlv_put(sctx, attrtype, attrlen, data); \
596 goto tlv_put_failure; \
599 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
601 ret = tlv_put_u##bits(sctx, attrtype, value); \
603 goto tlv_put_failure; \
606 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
607 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
608 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
609 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
610 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
612 ret = tlv_put_string(sctx, attrtype, str, len); \
614 goto tlv_put_failure; \
616 #define TLV_PUT_PATH(sctx, attrtype, p) \
618 ret = tlv_put_string(sctx, attrtype, p->start, \
619 p->end - p->start); \
621 goto tlv_put_failure; \
623 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
625 ret = tlv_put_uuid(sctx, attrtype, uuid); \
627 goto tlv_put_failure; \
629 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
631 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
633 goto tlv_put_failure; \
636 static int send_header(struct send_ctx *sctx)
638 struct btrfs_stream_header hdr;
640 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
641 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
643 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
648 * For each command/item we want to send to userspace, we call this function.
650 static int begin_cmd(struct send_ctx *sctx, int cmd)
652 struct btrfs_cmd_header *hdr;
654 if (WARN_ON(!sctx->send_buf))
657 BUG_ON(sctx->send_size);
659 sctx->send_size += sizeof(*hdr);
660 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
661 hdr->cmd = cpu_to_le16(cmd);
666 static int send_cmd(struct send_ctx *sctx)
669 struct btrfs_cmd_header *hdr;
672 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
673 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
676 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
677 hdr->crc = cpu_to_le32(crc);
679 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
682 sctx->total_send_size += sctx->send_size;
683 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
690 * Sends a move instruction to user space
692 static int send_rename(struct send_ctx *sctx,
693 struct fs_path *from, struct fs_path *to)
697 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
699 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
703 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
704 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
706 ret = send_cmd(sctx);
714 * Sends a link instruction to user space
716 static int send_link(struct send_ctx *sctx,
717 struct fs_path *path, struct fs_path *lnk)
721 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
723 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
727 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
728 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
730 ret = send_cmd(sctx);
738 * Sends an unlink instruction to user space
740 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
744 verbose_printk("btrfs: send_unlink %s\n", path->start);
746 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
750 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
752 ret = send_cmd(sctx);
760 * Sends a rmdir instruction to user space
762 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
766 verbose_printk("btrfs: send_rmdir %s\n", path->start);
768 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
772 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
774 ret = send_cmd(sctx);
782 * Helper function to retrieve some fields from an inode item.
784 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
785 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
789 struct btrfs_inode_item *ii;
790 struct btrfs_key key;
793 key.type = BTRFS_INODE_ITEM_KEY;
795 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
802 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
803 struct btrfs_inode_item);
805 *size = btrfs_inode_size(path->nodes[0], ii);
807 *gen = btrfs_inode_generation(path->nodes[0], ii);
809 *mode = btrfs_inode_mode(path->nodes[0], ii);
811 *uid = btrfs_inode_uid(path->nodes[0], ii);
813 *gid = btrfs_inode_gid(path->nodes[0], ii);
815 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
820 static int get_inode_info(struct btrfs_root *root,
821 u64 ino, u64 *size, u64 *gen,
822 u64 *mode, u64 *uid, u64 *gid,
825 struct btrfs_path *path;
828 path = alloc_path_for_send();
831 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
833 btrfs_free_path(path);
837 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
842 * Helper function to iterate the entries in ONE btrfs_inode_ref or
843 * btrfs_inode_extref.
844 * The iterate callback may return a non zero value to stop iteration. This can
845 * be a negative value for error codes or 1 to simply stop it.
847 * path must point to the INODE_REF or INODE_EXTREF when called.
849 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
850 struct btrfs_key *found_key, int resolve,
851 iterate_inode_ref_t iterate, void *ctx)
853 struct extent_buffer *eb = path->nodes[0];
854 struct btrfs_item *item;
855 struct btrfs_inode_ref *iref;
856 struct btrfs_inode_extref *extref;
857 struct btrfs_path *tmp_path;
861 int slot = path->slots[0];
868 unsigned long name_off;
869 unsigned long elem_size;
872 p = fs_path_alloc_reversed();
876 tmp_path = alloc_path_for_send();
883 if (found_key->type == BTRFS_INODE_REF_KEY) {
884 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
885 struct btrfs_inode_ref);
886 item = btrfs_item_nr(slot);
887 total = btrfs_item_size(eb, item);
888 elem_size = sizeof(*iref);
890 ptr = btrfs_item_ptr_offset(eb, slot);
891 total = btrfs_item_size_nr(eb, slot);
892 elem_size = sizeof(*extref);
895 while (cur < total) {
898 if (found_key->type == BTRFS_INODE_REF_KEY) {
899 iref = (struct btrfs_inode_ref *)(ptr + cur);
900 name_len = btrfs_inode_ref_name_len(eb, iref);
901 name_off = (unsigned long)(iref + 1);
902 index = btrfs_inode_ref_index(eb, iref);
903 dir = found_key->offset;
905 extref = (struct btrfs_inode_extref *)(ptr + cur);
906 name_len = btrfs_inode_extref_name_len(eb, extref);
907 name_off = (unsigned long)&extref->name;
908 index = btrfs_inode_extref_index(eb, extref);
909 dir = btrfs_inode_extref_parent(eb, extref);
913 start = btrfs_ref_to_path(root, tmp_path, name_len,
917 ret = PTR_ERR(start);
920 if (start < p->buf) {
921 /* overflow , try again with larger buffer */
922 ret = fs_path_ensure_buf(p,
923 p->buf_len + p->buf - start);
926 start = btrfs_ref_to_path(root, tmp_path,
931 ret = PTR_ERR(start);
934 BUG_ON(start < p->buf);
938 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
944 cur += elem_size + name_len;
945 ret = iterate(num, dir, index, p, ctx);
952 btrfs_free_path(tmp_path);
957 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
958 const char *name, int name_len,
959 const char *data, int data_len,
963 * Helper function to iterate the entries in ONE btrfs_dir_item.
964 * The iterate callback may return a non zero value to stop iteration. This can
965 * be a negative value for error codes or 1 to simply stop it.
967 * path must point to the dir item when called.
969 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
970 struct btrfs_key *found_key,
971 iterate_dir_item_t iterate, void *ctx)
974 struct extent_buffer *eb;
975 struct btrfs_item *item;
976 struct btrfs_dir_item *di;
977 struct btrfs_key di_key;
989 if (found_key->type == BTRFS_XATTR_ITEM_KEY)
990 buf_len = BTRFS_MAX_XATTR_SIZE(root);
994 buf = kmalloc(buf_len, GFP_NOFS);
1000 eb = path->nodes[0];
1001 slot = path->slots[0];
1002 item = btrfs_item_nr(slot);
1003 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1006 total = btrfs_item_size(eb, item);
1009 while (cur < total) {
1010 name_len = btrfs_dir_name_len(eb, di);
1011 data_len = btrfs_dir_data_len(eb, di);
1012 type = btrfs_dir_type(eb, di);
1013 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1015 if (type == BTRFS_FT_XATTR) {
1016 if (name_len > XATTR_NAME_MAX) {
1017 ret = -ENAMETOOLONG;
1020 if (name_len + data_len > buf_len) {
1028 if (name_len + data_len > buf_len) {
1029 ret = -ENAMETOOLONG;
1034 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1035 name_len + data_len);
1037 len = sizeof(*di) + name_len + data_len;
1038 di = (struct btrfs_dir_item *)((char *)di + len);
1041 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1042 data_len, type, ctx);
1058 static int __copy_first_ref(int num, u64 dir, int index,
1059 struct fs_path *p, void *ctx)
1062 struct fs_path *pt = ctx;
1064 ret = fs_path_copy(pt, p);
1068 /* we want the first only */
1073 * Retrieve the first path of an inode. If an inode has more then one
1074 * ref/hardlink, this is ignored.
1076 static int get_inode_path(struct btrfs_root *root,
1077 u64 ino, struct fs_path *path)
1080 struct btrfs_key key, found_key;
1081 struct btrfs_path *p;
1083 p = alloc_path_for_send();
1087 fs_path_reset(path);
1090 key.type = BTRFS_INODE_REF_KEY;
1093 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1100 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1101 if (found_key.objectid != ino ||
1102 (found_key.type != BTRFS_INODE_REF_KEY &&
1103 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1108 ret = iterate_inode_ref(root, p, &found_key, 1,
1109 __copy_first_ref, path);
1119 struct backref_ctx {
1120 struct send_ctx *sctx;
1122 struct btrfs_path *path;
1123 /* number of total found references */
1127 * used for clones found in send_root. clones found behind cur_objectid
1128 * and cur_offset are not considered as allowed clones.
1133 /* may be truncated in case it's the last extent in a file */
1136 /* Just to check for bugs in backref resolving */
1140 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1142 u64 root = (u64)(uintptr_t)key;
1143 struct clone_root *cr = (struct clone_root *)elt;
1145 if (root < cr->root->objectid)
1147 if (root > cr->root->objectid)
1152 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1154 struct clone_root *cr1 = (struct clone_root *)e1;
1155 struct clone_root *cr2 = (struct clone_root *)e2;
1157 if (cr1->root->objectid < cr2->root->objectid)
1159 if (cr1->root->objectid > cr2->root->objectid)
1165 * Called for every backref that is found for the current extent.
1166 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1168 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1170 struct backref_ctx *bctx = ctx_;
1171 struct clone_root *found;
1175 /* First check if the root is in the list of accepted clone sources */
1176 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1177 bctx->sctx->clone_roots_cnt,
1178 sizeof(struct clone_root),
1179 __clone_root_cmp_bsearch);
1183 if (found->root == bctx->sctx->send_root &&
1184 ino == bctx->cur_objectid &&
1185 offset == bctx->cur_offset) {
1186 bctx->found_itself = 1;
1190 * There are inodes that have extents that lie behind its i_size. Don't
1191 * accept clones from these extents.
1193 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1195 btrfs_release_path(bctx->path);
1199 if (offset + bctx->extent_len > i_size)
1203 * Make sure we don't consider clones from send_root that are
1204 * behind the current inode/offset.
1206 if (found->root == bctx->sctx->send_root) {
1208 * TODO for the moment we don't accept clones from the inode
1209 * that is currently send. We may change this when
1210 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1213 if (ino >= bctx->cur_objectid)
1216 if (ino > bctx->cur_objectid)
1218 if (offset + bctx->extent_len > bctx->cur_offset)
1224 found->found_refs++;
1225 if (ino < found->ino) {
1227 found->offset = offset;
1228 } else if (found->ino == ino) {
1230 * same extent found more then once in the same file.
1232 if (found->offset > offset + bctx->extent_len)
1233 found->offset = offset;
1240 * Given an inode, offset and extent item, it finds a good clone for a clone
1241 * instruction. Returns -ENOENT when none could be found. The function makes
1242 * sure that the returned clone is usable at the point where sending is at the
1243 * moment. This means, that no clones are accepted which lie behind the current
1246 * path must point to the extent item when called.
1248 static int find_extent_clone(struct send_ctx *sctx,
1249 struct btrfs_path *path,
1250 u64 ino, u64 data_offset,
1252 struct clone_root **found)
1259 u64 extent_item_pos;
1261 struct btrfs_file_extent_item *fi;
1262 struct extent_buffer *eb = path->nodes[0];
1263 struct backref_ctx *backref_ctx = NULL;
1264 struct clone_root *cur_clone_root;
1265 struct btrfs_key found_key;
1266 struct btrfs_path *tmp_path;
1270 tmp_path = alloc_path_for_send();
1274 /* We only use this path under the commit sem */
1275 tmp_path->need_commit_sem = 0;
1277 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1283 backref_ctx->path = tmp_path;
1285 if (data_offset >= ino_size) {
1287 * There may be extents that lie behind the file's size.
1288 * I at least had this in combination with snapshotting while
1289 * writing large files.
1295 fi = btrfs_item_ptr(eb, path->slots[0],
1296 struct btrfs_file_extent_item);
1297 extent_type = btrfs_file_extent_type(eb, fi);
1298 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1302 compressed = btrfs_file_extent_compression(eb, fi);
1304 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1305 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1306 if (disk_byte == 0) {
1310 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1312 down_read(&sctx->send_root->fs_info->commit_root_sem);
1313 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1314 &found_key, &flags);
1315 up_read(&sctx->send_root->fs_info->commit_root_sem);
1316 btrfs_release_path(tmp_path);
1320 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1326 * Setup the clone roots.
1328 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1329 cur_clone_root = sctx->clone_roots + i;
1330 cur_clone_root->ino = (u64)-1;
1331 cur_clone_root->offset = 0;
1332 cur_clone_root->found_refs = 0;
1335 backref_ctx->sctx = sctx;
1336 backref_ctx->found = 0;
1337 backref_ctx->cur_objectid = ino;
1338 backref_ctx->cur_offset = data_offset;
1339 backref_ctx->found_itself = 0;
1340 backref_ctx->extent_len = num_bytes;
1343 * The last extent of a file may be too large due to page alignment.
1344 * We need to adjust extent_len in this case so that the checks in
1345 * __iterate_backrefs work.
1347 if (data_offset + num_bytes >= ino_size)
1348 backref_ctx->extent_len = ino_size - data_offset;
1351 * Now collect all backrefs.
1353 if (compressed == BTRFS_COMPRESS_NONE)
1354 extent_item_pos = logical - found_key.objectid;
1356 extent_item_pos = 0;
1357 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1358 found_key.objectid, extent_item_pos, 1,
1359 __iterate_backrefs, backref_ctx);
1364 if (!backref_ctx->found_itself) {
1365 /* found a bug in backref code? */
1367 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
1368 "send_root. inode=%llu, offset=%llu, "
1369 "disk_byte=%llu found extent=%llu",
1370 ino, data_offset, disk_byte, found_key.objectid);
1374 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1376 "num_bytes=%llu, logical=%llu\n",
1377 data_offset, ino, num_bytes, logical);
1379 if (!backref_ctx->found)
1380 verbose_printk("btrfs: no clones found\n");
1382 cur_clone_root = NULL;
1383 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1384 if (sctx->clone_roots[i].found_refs) {
1385 if (!cur_clone_root)
1386 cur_clone_root = sctx->clone_roots + i;
1387 else if (sctx->clone_roots[i].root == sctx->send_root)
1388 /* prefer clones from send_root over others */
1389 cur_clone_root = sctx->clone_roots + i;
1394 if (cur_clone_root) {
1395 if (compressed != BTRFS_COMPRESS_NONE) {
1397 * Offsets given by iterate_extent_inodes() are relative
1398 * to the start of the extent, we need to add logical
1399 * offset from the file extent item.
1400 * (See why at backref.c:check_extent_in_eb())
1402 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1405 *found = cur_clone_root;
1412 btrfs_free_path(tmp_path);
1417 static int read_symlink(struct btrfs_root *root,
1419 struct fs_path *dest)
1422 struct btrfs_path *path;
1423 struct btrfs_key key;
1424 struct btrfs_file_extent_item *ei;
1430 path = alloc_path_for_send();
1435 key.type = BTRFS_EXTENT_DATA_KEY;
1437 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1442 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1443 struct btrfs_file_extent_item);
1444 type = btrfs_file_extent_type(path->nodes[0], ei);
1445 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1446 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1447 BUG_ON(compression);
1449 off = btrfs_file_extent_inline_start(ei);
1450 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
1452 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1455 btrfs_free_path(path);
1460 * Helper function to generate a file name that is unique in the root of
1461 * send_root and parent_root. This is used to generate names for orphan inodes.
1463 static int gen_unique_name(struct send_ctx *sctx,
1465 struct fs_path *dest)
1468 struct btrfs_path *path;
1469 struct btrfs_dir_item *di;
1474 path = alloc_path_for_send();
1479 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1481 ASSERT(len < sizeof(tmp));
1483 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1484 path, BTRFS_FIRST_FREE_OBJECTID,
1485 tmp, strlen(tmp), 0);
1486 btrfs_release_path(path);
1492 /* not unique, try again */
1497 if (!sctx->parent_root) {
1503 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1504 path, BTRFS_FIRST_FREE_OBJECTID,
1505 tmp, strlen(tmp), 0);
1506 btrfs_release_path(path);
1512 /* not unique, try again */
1520 ret = fs_path_add(dest, tmp, strlen(tmp));
1523 btrfs_free_path(path);
1528 inode_state_no_change,
1529 inode_state_will_create,
1530 inode_state_did_create,
1531 inode_state_will_delete,
1532 inode_state_did_delete,
1535 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1543 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1545 if (ret < 0 && ret != -ENOENT)
1549 if (!sctx->parent_root) {
1550 right_ret = -ENOENT;
1552 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1553 NULL, NULL, NULL, NULL);
1554 if (ret < 0 && ret != -ENOENT)
1559 if (!left_ret && !right_ret) {
1560 if (left_gen == gen && right_gen == gen) {
1561 ret = inode_state_no_change;
1562 } else if (left_gen == gen) {
1563 if (ino < sctx->send_progress)
1564 ret = inode_state_did_create;
1566 ret = inode_state_will_create;
1567 } else if (right_gen == gen) {
1568 if (ino < sctx->send_progress)
1569 ret = inode_state_did_delete;
1571 ret = inode_state_will_delete;
1575 } else if (!left_ret) {
1576 if (left_gen == gen) {
1577 if (ino < sctx->send_progress)
1578 ret = inode_state_did_create;
1580 ret = inode_state_will_create;
1584 } else if (!right_ret) {
1585 if (right_gen == gen) {
1586 if (ino < sctx->send_progress)
1587 ret = inode_state_did_delete;
1589 ret = inode_state_will_delete;
1601 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1605 ret = get_cur_inode_state(sctx, ino, gen);
1609 if (ret == inode_state_no_change ||
1610 ret == inode_state_did_create ||
1611 ret == inode_state_will_delete)
1621 * Helper function to lookup a dir item in a dir.
1623 static int lookup_dir_item_inode(struct btrfs_root *root,
1624 u64 dir, const char *name, int name_len,
1629 struct btrfs_dir_item *di;
1630 struct btrfs_key key;
1631 struct btrfs_path *path;
1633 path = alloc_path_for_send();
1637 di = btrfs_lookup_dir_item(NULL, root, path,
1638 dir, name, name_len, 0);
1647 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1648 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1652 *found_inode = key.objectid;
1653 *found_type = btrfs_dir_type(path->nodes[0], di);
1656 btrfs_free_path(path);
1661 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1662 * generation of the parent dir and the name of the dir entry.
1664 static int get_first_ref(struct btrfs_root *root, u64 ino,
1665 u64 *dir, u64 *dir_gen, struct fs_path *name)
1668 struct btrfs_key key;
1669 struct btrfs_key found_key;
1670 struct btrfs_path *path;
1674 path = alloc_path_for_send();
1679 key.type = BTRFS_INODE_REF_KEY;
1682 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1686 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1688 if (ret || found_key.objectid != ino ||
1689 (found_key.type != BTRFS_INODE_REF_KEY &&
1690 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1695 if (found_key.type == BTRFS_INODE_REF_KEY) {
1696 struct btrfs_inode_ref *iref;
1697 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1698 struct btrfs_inode_ref);
1699 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1700 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1701 (unsigned long)(iref + 1),
1703 parent_dir = found_key.offset;
1705 struct btrfs_inode_extref *extref;
1706 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1707 struct btrfs_inode_extref);
1708 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1709 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1710 (unsigned long)&extref->name, len);
1711 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1715 btrfs_release_path(path);
1718 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1727 btrfs_free_path(path);
1731 static int is_first_ref(struct btrfs_root *root,
1733 const char *name, int name_len)
1736 struct fs_path *tmp_name;
1739 tmp_name = fs_path_alloc();
1743 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1747 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1752 ret = !memcmp(tmp_name->start, name, name_len);
1755 fs_path_free(tmp_name);
1760 * Used by process_recorded_refs to determine if a new ref would overwrite an
1761 * already existing ref. In case it detects an overwrite, it returns the
1762 * inode/gen in who_ino/who_gen.
1763 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1764 * to make sure later references to the overwritten inode are possible.
1765 * Orphanizing is however only required for the first ref of an inode.
1766 * process_recorded_refs does an additional is_first_ref check to see if
1767 * orphanizing is really required.
1769 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1770 const char *name, int name_len,
1771 u64 *who_ino, u64 *who_gen)
1775 u64 other_inode = 0;
1778 if (!sctx->parent_root)
1781 ret = is_inode_existent(sctx, dir, dir_gen);
1786 * If we have a parent root we need to verify that the parent dir was
1787 * not delted and then re-created, if it was then we have no overwrite
1788 * and we can just unlink this entry.
1790 if (sctx->parent_root) {
1791 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1793 if (ret < 0 && ret != -ENOENT)
1803 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1804 &other_inode, &other_type);
1805 if (ret < 0 && ret != -ENOENT)
1813 * Check if the overwritten ref was already processed. If yes, the ref
1814 * was already unlinked/moved, so we can safely assume that we will not
1815 * overwrite anything at this point in time.
1817 if (other_inode > sctx->send_progress) {
1818 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1819 who_gen, NULL, NULL, NULL, NULL);
1824 *who_ino = other_inode;
1834 * Checks if the ref was overwritten by an already processed inode. This is
1835 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1836 * thus the orphan name needs be used.
1837 * process_recorded_refs also uses it to avoid unlinking of refs that were
1840 static int did_overwrite_ref(struct send_ctx *sctx,
1841 u64 dir, u64 dir_gen,
1842 u64 ino, u64 ino_gen,
1843 const char *name, int name_len)
1850 if (!sctx->parent_root)
1853 ret = is_inode_existent(sctx, dir, dir_gen);
1857 /* check if the ref was overwritten by another ref */
1858 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1859 &ow_inode, &other_type);
1860 if (ret < 0 && ret != -ENOENT)
1863 /* was never and will never be overwritten */
1868 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1873 if (ow_inode == ino && gen == ino_gen) {
1878 /* we know that it is or will be overwritten. check this now */
1879 if (ow_inode < sctx->send_progress)
1889 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1890 * that got overwritten. This is used by process_recorded_refs to determine
1891 * if it has to use the path as returned by get_cur_path or the orphan name.
1893 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1896 struct fs_path *name = NULL;
1900 if (!sctx->parent_root)
1903 name = fs_path_alloc();
1907 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1911 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1912 name->start, fs_path_len(name));
1920 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1921 * so we need to do some special handling in case we have clashes. This function
1922 * takes care of this with the help of name_cache_entry::radix_list.
1923 * In case of error, nce is kfreed.
1925 static int name_cache_insert(struct send_ctx *sctx,
1926 struct name_cache_entry *nce)
1929 struct list_head *nce_head;
1931 nce_head = radix_tree_lookup(&sctx->name_cache,
1932 (unsigned long)nce->ino);
1934 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1939 INIT_LIST_HEAD(nce_head);
1941 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1948 list_add_tail(&nce->radix_list, nce_head);
1949 list_add_tail(&nce->list, &sctx->name_cache_list);
1950 sctx->name_cache_size++;
1955 static void name_cache_delete(struct send_ctx *sctx,
1956 struct name_cache_entry *nce)
1958 struct list_head *nce_head;
1960 nce_head = radix_tree_lookup(&sctx->name_cache,
1961 (unsigned long)nce->ino);
1963 btrfs_err(sctx->send_root->fs_info,
1964 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1965 nce->ino, sctx->name_cache_size);
1968 list_del(&nce->radix_list);
1969 list_del(&nce->list);
1970 sctx->name_cache_size--;
1973 * We may not get to the final release of nce_head if the lookup fails
1975 if (nce_head && list_empty(nce_head)) {
1976 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1981 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1984 struct list_head *nce_head;
1985 struct name_cache_entry *cur;
1987 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1991 list_for_each_entry(cur, nce_head, radix_list) {
1992 if (cur->ino == ino && cur->gen == gen)
1999 * Removes the entry from the list and adds it back to the end. This marks the
2000 * entry as recently used so that name_cache_clean_unused does not remove it.
2002 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2004 list_del(&nce->list);
2005 list_add_tail(&nce->list, &sctx->name_cache_list);
2009 * Remove some entries from the beginning of name_cache_list.
2011 static void name_cache_clean_unused(struct send_ctx *sctx)
2013 struct name_cache_entry *nce;
2015 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2018 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2019 nce = list_entry(sctx->name_cache_list.next,
2020 struct name_cache_entry, list);
2021 name_cache_delete(sctx, nce);
2026 static void name_cache_free(struct send_ctx *sctx)
2028 struct name_cache_entry *nce;
2030 while (!list_empty(&sctx->name_cache_list)) {
2031 nce = list_entry(sctx->name_cache_list.next,
2032 struct name_cache_entry, list);
2033 name_cache_delete(sctx, nce);
2039 * Used by get_cur_path for each ref up to the root.
2040 * Returns 0 if it succeeded.
2041 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2042 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2043 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2044 * Returns <0 in case of error.
2046 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2050 struct fs_path *dest)
2054 struct name_cache_entry *nce = NULL;
2057 * First check if we already did a call to this function with the same
2058 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2059 * return the cached result.
2061 nce = name_cache_search(sctx, ino, gen);
2063 if (ino < sctx->send_progress && nce->need_later_update) {
2064 name_cache_delete(sctx, nce);
2068 name_cache_used(sctx, nce);
2069 *parent_ino = nce->parent_ino;
2070 *parent_gen = nce->parent_gen;
2071 ret = fs_path_add(dest, nce->name, nce->name_len);
2080 * If the inode is not existent yet, add the orphan name and return 1.
2081 * This should only happen for the parent dir that we determine in
2084 ret = is_inode_existent(sctx, ino, gen);
2089 ret = gen_unique_name(sctx, ino, gen, dest);
2097 * Depending on whether the inode was already processed or not, use
2098 * send_root or parent_root for ref lookup.
2100 if (ino < sctx->send_progress)
2101 ret = get_first_ref(sctx->send_root, ino,
2102 parent_ino, parent_gen, dest);
2104 ret = get_first_ref(sctx->parent_root, ino,
2105 parent_ino, parent_gen, dest);
2110 * Check if the ref was overwritten by an inode's ref that was processed
2111 * earlier. If yes, treat as orphan and return 1.
2113 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2114 dest->start, dest->end - dest->start);
2118 fs_path_reset(dest);
2119 ret = gen_unique_name(sctx, ino, gen, dest);
2127 * Store the result of the lookup in the name cache.
2129 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2137 nce->parent_ino = *parent_ino;
2138 nce->parent_gen = *parent_gen;
2139 nce->name_len = fs_path_len(dest);
2141 strcpy(nce->name, dest->start);
2143 if (ino < sctx->send_progress)
2144 nce->need_later_update = 0;
2146 nce->need_later_update = 1;
2148 nce_ret = name_cache_insert(sctx, nce);
2151 name_cache_clean_unused(sctx);
2158 * Magic happens here. This function returns the first ref to an inode as it
2159 * would look like while receiving the stream at this point in time.
2160 * We walk the path up to the root. For every inode in between, we check if it
2161 * was already processed/sent. If yes, we continue with the parent as found
2162 * in send_root. If not, we continue with the parent as found in parent_root.
2163 * If we encounter an inode that was deleted at this point in time, we use the
2164 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2165 * that were not created yet and overwritten inodes/refs.
2167 * When do we have have orphan inodes:
2168 * 1. When an inode is freshly created and thus no valid refs are available yet
2169 * 2. When a directory lost all it's refs (deleted) but still has dir items
2170 * inside which were not processed yet (pending for move/delete). If anyone
2171 * tried to get the path to the dir items, it would get a path inside that
2173 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2174 * of an unprocessed inode. If in that case the first ref would be
2175 * overwritten, the overwritten inode gets "orphanized". Later when we
2176 * process this overwritten inode, it is restored at a new place by moving
2179 * sctx->send_progress tells this function at which point in time receiving
2182 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2183 struct fs_path *dest)
2186 struct fs_path *name = NULL;
2187 u64 parent_inode = 0;
2191 name = fs_path_alloc();
2198 fs_path_reset(dest);
2200 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2201 fs_path_reset(name);
2203 if (is_waiting_for_rm(sctx, ino)) {
2204 ret = gen_unique_name(sctx, ino, gen, name);
2207 ret = fs_path_add_path(dest, name);
2211 if (is_waiting_for_move(sctx, ino)) {
2212 ret = get_first_ref(sctx->parent_root, ino,
2213 &parent_inode, &parent_gen, name);
2215 ret = __get_cur_name_and_parent(sctx, ino, gen,
2225 ret = fs_path_add_path(dest, name);
2236 fs_path_unreverse(dest);
2241 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2243 static int send_subvol_begin(struct send_ctx *sctx)
2246 struct btrfs_root *send_root = sctx->send_root;
2247 struct btrfs_root *parent_root = sctx->parent_root;
2248 struct btrfs_path *path;
2249 struct btrfs_key key;
2250 struct btrfs_root_ref *ref;
2251 struct extent_buffer *leaf;
2255 path = btrfs_alloc_path();
2259 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2261 btrfs_free_path(path);
2265 key.objectid = send_root->objectid;
2266 key.type = BTRFS_ROOT_BACKREF_KEY;
2269 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2278 leaf = path->nodes[0];
2279 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2280 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2281 key.objectid != send_root->objectid) {
2285 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2286 namelen = btrfs_root_ref_name_len(leaf, ref);
2287 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2288 btrfs_release_path(path);
2291 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2295 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2300 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2301 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2302 sctx->send_root->root_item.uuid);
2303 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2304 le64_to_cpu(sctx->send_root->root_item.ctransid));
2306 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2307 sctx->parent_root->root_item.uuid);
2308 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2309 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2312 ret = send_cmd(sctx);
2316 btrfs_free_path(path);
2321 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2326 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2328 p = fs_path_alloc();
2332 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2336 ret = get_cur_path(sctx, ino, gen, p);
2339 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2340 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2342 ret = send_cmd(sctx);
2350 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2355 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2357 p = fs_path_alloc();
2361 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2365 ret = get_cur_path(sctx, ino, gen, p);
2368 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2369 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2371 ret = send_cmd(sctx);
2379 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2384 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2386 p = fs_path_alloc();
2390 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2394 ret = get_cur_path(sctx, ino, gen, p);
2397 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2398 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2399 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2401 ret = send_cmd(sctx);
2409 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2412 struct fs_path *p = NULL;
2413 struct btrfs_inode_item *ii;
2414 struct btrfs_path *path = NULL;
2415 struct extent_buffer *eb;
2416 struct btrfs_key key;
2419 verbose_printk("btrfs: send_utimes %llu\n", ino);
2421 p = fs_path_alloc();
2425 path = alloc_path_for_send();
2432 key.type = BTRFS_INODE_ITEM_KEY;
2434 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2438 eb = path->nodes[0];
2439 slot = path->slots[0];
2440 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2442 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2446 ret = get_cur_path(sctx, ino, gen, p);
2449 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2450 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2451 btrfs_inode_atime(ii));
2452 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2453 btrfs_inode_mtime(ii));
2454 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2455 btrfs_inode_ctime(ii));
2456 /* TODO Add otime support when the otime patches get into upstream */
2458 ret = send_cmd(sctx);
2463 btrfs_free_path(path);
2468 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2469 * a valid path yet because we did not process the refs yet. So, the inode
2470 * is created as orphan.
2472 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2481 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2483 p = fs_path_alloc();
2487 if (ino != sctx->cur_ino) {
2488 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2493 gen = sctx->cur_inode_gen;
2494 mode = sctx->cur_inode_mode;
2495 rdev = sctx->cur_inode_rdev;
2498 if (S_ISREG(mode)) {
2499 cmd = BTRFS_SEND_C_MKFILE;
2500 } else if (S_ISDIR(mode)) {
2501 cmd = BTRFS_SEND_C_MKDIR;
2502 } else if (S_ISLNK(mode)) {
2503 cmd = BTRFS_SEND_C_SYMLINK;
2504 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2505 cmd = BTRFS_SEND_C_MKNOD;
2506 } else if (S_ISFIFO(mode)) {
2507 cmd = BTRFS_SEND_C_MKFIFO;
2508 } else if (S_ISSOCK(mode)) {
2509 cmd = BTRFS_SEND_C_MKSOCK;
2511 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2512 (int)(mode & S_IFMT));
2517 ret = begin_cmd(sctx, cmd);
2521 ret = gen_unique_name(sctx, ino, gen, p);
2525 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2526 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2528 if (S_ISLNK(mode)) {
2530 ret = read_symlink(sctx->send_root, ino, p);
2533 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2534 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2535 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2536 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2537 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2540 ret = send_cmd(sctx);
2552 * We need some special handling for inodes that get processed before the parent
2553 * directory got created. See process_recorded_refs for details.
2554 * This function does the check if we already created the dir out of order.
2556 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2559 struct btrfs_path *path = NULL;
2560 struct btrfs_key key;
2561 struct btrfs_key found_key;
2562 struct btrfs_key di_key;
2563 struct extent_buffer *eb;
2564 struct btrfs_dir_item *di;
2567 path = alloc_path_for_send();
2574 key.type = BTRFS_DIR_INDEX_KEY;
2576 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2581 eb = path->nodes[0];
2582 slot = path->slots[0];
2583 if (slot >= btrfs_header_nritems(eb)) {
2584 ret = btrfs_next_leaf(sctx->send_root, path);
2587 } else if (ret > 0) {
2594 btrfs_item_key_to_cpu(eb, &found_key, slot);
2595 if (found_key.objectid != key.objectid ||
2596 found_key.type != key.type) {
2601 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2602 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2604 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2605 di_key.objectid < sctx->send_progress) {
2614 btrfs_free_path(path);
2619 * Only creates the inode if it is:
2620 * 1. Not a directory
2621 * 2. Or a directory which was not created already due to out of order
2622 * directories. See did_create_dir and process_recorded_refs for details.
2624 static int send_create_inode_if_needed(struct send_ctx *sctx)
2628 if (S_ISDIR(sctx->cur_inode_mode)) {
2629 ret = did_create_dir(sctx, sctx->cur_ino);
2638 ret = send_create_inode(sctx, sctx->cur_ino);
2646 struct recorded_ref {
2647 struct list_head list;
2650 struct fs_path *full_path;
2658 * We need to process new refs before deleted refs, but compare_tree gives us
2659 * everything mixed. So we first record all refs and later process them.
2660 * This function is a helper to record one ref.
2662 static int __record_ref(struct list_head *head, u64 dir,
2663 u64 dir_gen, struct fs_path *path)
2665 struct recorded_ref *ref;
2667 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2672 ref->dir_gen = dir_gen;
2673 ref->full_path = path;
2675 ref->name = (char *)kbasename(ref->full_path->start);
2676 ref->name_len = ref->full_path->end - ref->name;
2677 ref->dir_path = ref->full_path->start;
2678 if (ref->name == ref->full_path->start)
2679 ref->dir_path_len = 0;
2681 ref->dir_path_len = ref->full_path->end -
2682 ref->full_path->start - 1 - ref->name_len;
2684 list_add_tail(&ref->list, head);
2688 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2690 struct recorded_ref *new;
2692 new = kmalloc(sizeof(*ref), GFP_NOFS);
2696 new->dir = ref->dir;
2697 new->dir_gen = ref->dir_gen;
2698 new->full_path = NULL;
2699 INIT_LIST_HEAD(&new->list);
2700 list_add_tail(&new->list, list);
2704 static void __free_recorded_refs(struct list_head *head)
2706 struct recorded_ref *cur;
2708 while (!list_empty(head)) {
2709 cur = list_entry(head->next, struct recorded_ref, list);
2710 fs_path_free(cur->full_path);
2711 list_del(&cur->list);
2716 static void free_recorded_refs(struct send_ctx *sctx)
2718 __free_recorded_refs(&sctx->new_refs);
2719 __free_recorded_refs(&sctx->deleted_refs);
2723 * Renames/moves a file/dir to its orphan name. Used when the first
2724 * ref of an unprocessed inode gets overwritten and for all non empty
2727 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2728 struct fs_path *path)
2731 struct fs_path *orphan;
2733 orphan = fs_path_alloc();
2737 ret = gen_unique_name(sctx, ino, gen, orphan);
2741 ret = send_rename(sctx, path, orphan);
2744 fs_path_free(orphan);
2748 static struct orphan_dir_info *
2749 add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2751 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2752 struct rb_node *parent = NULL;
2753 struct orphan_dir_info *entry, *odi;
2755 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2757 return ERR_PTR(-ENOMEM);
2763 entry = rb_entry(parent, struct orphan_dir_info, node);
2764 if (dir_ino < entry->ino) {
2766 } else if (dir_ino > entry->ino) {
2767 p = &(*p)->rb_right;
2774 rb_link_node(&odi->node, parent, p);
2775 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2779 static struct orphan_dir_info *
2780 get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2782 struct rb_node *n = sctx->orphan_dirs.rb_node;
2783 struct orphan_dir_info *entry;
2786 entry = rb_entry(n, struct orphan_dir_info, node);
2787 if (dir_ino < entry->ino)
2789 else if (dir_ino > entry->ino)
2797 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2799 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2804 static void free_orphan_dir_info(struct send_ctx *sctx,
2805 struct orphan_dir_info *odi)
2809 rb_erase(&odi->node, &sctx->orphan_dirs);
2814 * Returns 1 if a directory can be removed at this point in time.
2815 * We check this by iterating all dir items and checking if the inode behind
2816 * the dir item was already processed.
2818 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2822 struct btrfs_root *root = sctx->parent_root;
2823 struct btrfs_path *path;
2824 struct btrfs_key key;
2825 struct btrfs_key found_key;
2826 struct btrfs_key loc;
2827 struct btrfs_dir_item *di;
2830 * Don't try to rmdir the top/root subvolume dir.
2832 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2835 path = alloc_path_for_send();
2840 key.type = BTRFS_DIR_INDEX_KEY;
2842 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2847 struct waiting_dir_move *dm;
2849 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2850 ret = btrfs_next_leaf(root, path);
2857 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2859 if (found_key.objectid != key.objectid ||
2860 found_key.type != key.type)
2863 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2864 struct btrfs_dir_item);
2865 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2867 dm = get_waiting_dir_move(sctx, loc.objectid);
2869 struct orphan_dir_info *odi;
2871 odi = add_orphan_dir_info(sctx, dir);
2877 dm->rmdir_ino = dir;
2882 if (loc.objectid > send_progress) {
2893 btrfs_free_path(path);
2897 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2899 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
2901 return entry != NULL;
2904 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2906 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2907 struct rb_node *parent = NULL;
2908 struct waiting_dir_move *entry, *dm;
2910 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2918 entry = rb_entry(parent, struct waiting_dir_move, node);
2919 if (ino < entry->ino) {
2921 } else if (ino > entry->ino) {
2922 p = &(*p)->rb_right;
2929 rb_link_node(&dm->node, parent, p);
2930 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2934 static struct waiting_dir_move *
2935 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2937 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2938 struct waiting_dir_move *entry;
2941 entry = rb_entry(n, struct waiting_dir_move, node);
2942 if (ino < entry->ino)
2944 else if (ino > entry->ino)
2952 static void free_waiting_dir_move(struct send_ctx *sctx,
2953 struct waiting_dir_move *dm)
2957 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2961 static int add_pending_dir_move(struct send_ctx *sctx,
2965 struct list_head *new_refs,
2966 struct list_head *deleted_refs)
2968 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2969 struct rb_node *parent = NULL;
2970 struct pending_dir_move *entry = NULL, *pm;
2971 struct recorded_ref *cur;
2975 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2978 pm->parent_ino = parent_ino;
2981 INIT_LIST_HEAD(&pm->list);
2982 INIT_LIST_HEAD(&pm->update_refs);
2983 RB_CLEAR_NODE(&pm->node);
2987 entry = rb_entry(parent, struct pending_dir_move, node);
2988 if (parent_ino < entry->parent_ino) {
2990 } else if (parent_ino > entry->parent_ino) {
2991 p = &(*p)->rb_right;
2998 list_for_each_entry(cur, deleted_refs, list) {
2999 ret = dup_ref(cur, &pm->update_refs);
3003 list_for_each_entry(cur, new_refs, list) {
3004 ret = dup_ref(cur, &pm->update_refs);
3009 ret = add_waiting_dir_move(sctx, pm->ino);
3014 list_add_tail(&pm->list, &entry->list);
3016 rb_link_node(&pm->node, parent, p);
3017 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3022 __free_recorded_refs(&pm->update_refs);
3028 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3031 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3032 struct pending_dir_move *entry;
3035 entry = rb_entry(n, struct pending_dir_move, node);
3036 if (parent_ino < entry->parent_ino)
3038 else if (parent_ino > entry->parent_ino)
3046 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3047 u64 ino, u64 gen, u64 *ancestor_ino)
3050 u64 parent_inode = 0;
3052 u64 start_ino = ino;
3055 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3056 fs_path_reset(name);
3058 if (is_waiting_for_rm(sctx, ino))
3060 if (is_waiting_for_move(sctx, ino)) {
3061 if (*ancestor_ino == 0)
3062 *ancestor_ino = ino;
3063 ret = get_first_ref(sctx->parent_root, ino,
3064 &parent_inode, &parent_gen, name);
3066 ret = __get_cur_name_and_parent(sctx, ino, gen,
3076 if (parent_inode == start_ino) {
3078 if (*ancestor_ino == 0)
3079 *ancestor_ino = ino;
3088 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3090 struct fs_path *from_path = NULL;
3091 struct fs_path *to_path = NULL;
3092 struct fs_path *name = NULL;
3093 u64 orig_progress = sctx->send_progress;
3094 struct recorded_ref *cur;
3095 u64 parent_ino, parent_gen;
3096 struct waiting_dir_move *dm = NULL;
3101 name = fs_path_alloc();
3102 from_path = fs_path_alloc();
3103 if (!name || !from_path) {
3108 dm = get_waiting_dir_move(sctx, pm->ino);
3110 rmdir_ino = dm->rmdir_ino;
3111 free_waiting_dir_move(sctx, dm);
3113 ret = get_first_ref(sctx->parent_root, pm->ino,
3114 &parent_ino, &parent_gen, name);
3118 ret = get_cur_path(sctx, parent_ino, parent_gen,
3122 ret = fs_path_add_path(from_path, name);
3126 sctx->send_progress = sctx->cur_ino + 1;
3127 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3129 LIST_HEAD(deleted_refs);
3130 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3131 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3132 &pm->update_refs, &deleted_refs);
3136 dm = get_waiting_dir_move(sctx, pm->ino);
3138 dm->rmdir_ino = rmdir_ino;
3142 fs_path_reset(name);
3145 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3149 ret = send_rename(sctx, from_path, to_path);
3154 struct orphan_dir_info *odi;
3156 odi = get_orphan_dir_info(sctx, rmdir_ino);
3158 /* already deleted */
3161 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3167 name = fs_path_alloc();
3172 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3175 ret = send_rmdir(sctx, name);
3178 free_orphan_dir_info(sctx, odi);
3182 ret = send_utimes(sctx, pm->ino, pm->gen);
3187 * After rename/move, need to update the utimes of both new parent(s)
3188 * and old parent(s).
3190 list_for_each_entry(cur, &pm->update_refs, list) {
3191 if (cur->dir == rmdir_ino)
3193 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3200 fs_path_free(from_path);
3201 fs_path_free(to_path);
3202 sctx->send_progress = orig_progress;
3207 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3209 if (!list_empty(&m->list))
3211 if (!RB_EMPTY_NODE(&m->node))
3212 rb_erase(&m->node, &sctx->pending_dir_moves);
3213 __free_recorded_refs(&m->update_refs);
3217 static void tail_append_pending_moves(struct pending_dir_move *moves,
3218 struct list_head *stack)
3220 if (list_empty(&moves->list)) {
3221 list_add_tail(&moves->list, stack);
3224 list_splice_init(&moves->list, &list);
3225 list_add_tail(&moves->list, stack);
3226 list_splice_tail(&list, stack);
3230 static int apply_children_dir_moves(struct send_ctx *sctx)
3232 struct pending_dir_move *pm;
3233 struct list_head stack;
3234 u64 parent_ino = sctx->cur_ino;
3237 pm = get_pending_dir_moves(sctx, parent_ino);
3241 INIT_LIST_HEAD(&stack);
3242 tail_append_pending_moves(pm, &stack);
3244 while (!list_empty(&stack)) {
3245 pm = list_first_entry(&stack, struct pending_dir_move, list);
3246 parent_ino = pm->ino;
3247 ret = apply_dir_move(sctx, pm);
3248 free_pending_move(sctx, pm);
3251 pm = get_pending_dir_moves(sctx, parent_ino);
3253 tail_append_pending_moves(pm, &stack);
3258 while (!list_empty(&stack)) {
3259 pm = list_first_entry(&stack, struct pending_dir_move, list);
3260 free_pending_move(sctx, pm);
3265 static int wait_for_parent_move(struct send_ctx *sctx,
3266 struct recorded_ref *parent_ref)
3269 u64 ino = parent_ref->dir;
3270 u64 parent_ino_before, parent_ino_after;
3271 struct fs_path *path_before = NULL;
3272 struct fs_path *path_after = NULL;
3275 path_after = fs_path_alloc();
3276 path_before = fs_path_alloc();
3277 if (!path_after || !path_before) {
3283 * Our current directory inode may not yet be renamed/moved because some
3284 * ancestor (immediate or not) has to be renamed/moved first. So find if
3285 * such ancestor exists and make sure our own rename/move happens after
3286 * that ancestor is processed.
3288 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3289 if (is_waiting_for_move(sctx, ino)) {
3294 fs_path_reset(path_before);
3295 fs_path_reset(path_after);
3297 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3301 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3303 if (ret < 0 && ret != -ENOENT) {
3305 } else if (ret == -ENOENT) {
3310 len1 = fs_path_len(path_before);
3311 len2 = fs_path_len(path_after);
3312 if (ino > sctx->cur_ino &&
3313 (parent_ino_before != parent_ino_after || len1 != len2 ||
3314 memcmp(path_before->start, path_after->start, len1))) {
3318 ino = parent_ino_after;
3322 fs_path_free(path_before);
3323 fs_path_free(path_after);
3326 ret = add_pending_dir_move(sctx,
3328 sctx->cur_inode_gen,
3331 &sctx->deleted_refs);
3340 * This does all the move/link/unlink/rmdir magic.
3342 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3345 struct recorded_ref *cur;
3346 struct recorded_ref *cur2;
3347 struct list_head check_dirs;
3348 struct fs_path *valid_path = NULL;
3351 int did_overwrite = 0;
3353 u64 last_dir_ino_rm = 0;
3355 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3358 * This should never happen as the root dir always has the same ref
3359 * which is always '..'
3361 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3362 INIT_LIST_HEAD(&check_dirs);
3364 valid_path = fs_path_alloc();
3371 * First, check if the first ref of the current inode was overwritten
3372 * before. If yes, we know that the current inode was already orphanized
3373 * and thus use the orphan name. If not, we can use get_cur_path to
3374 * get the path of the first ref as it would like while receiving at
3375 * this point in time.
3376 * New inodes are always orphan at the beginning, so force to use the
3377 * orphan name in this case.
3378 * The first ref is stored in valid_path and will be updated if it
3379 * gets moved around.
3381 if (!sctx->cur_inode_new) {
3382 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3383 sctx->cur_inode_gen);
3389 if (sctx->cur_inode_new || did_overwrite) {
3390 ret = gen_unique_name(sctx, sctx->cur_ino,
3391 sctx->cur_inode_gen, valid_path);
3396 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3402 list_for_each_entry(cur, &sctx->new_refs, list) {
3404 * We may have refs where the parent directory does not exist
3405 * yet. This happens if the parent directories inum is higher
3406 * the the current inum. To handle this case, we create the
3407 * parent directory out of order. But we need to check if this
3408 * did already happen before due to other refs in the same dir.
3410 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3413 if (ret == inode_state_will_create) {
3416 * First check if any of the current inodes refs did
3417 * already create the dir.
3419 list_for_each_entry(cur2, &sctx->new_refs, list) {
3422 if (cur2->dir == cur->dir) {
3429 * If that did not happen, check if a previous inode
3430 * did already create the dir.
3433 ret = did_create_dir(sctx, cur->dir);
3437 ret = send_create_inode(sctx, cur->dir);
3444 * Check if this new ref would overwrite the first ref of
3445 * another unprocessed inode. If yes, orphanize the
3446 * overwritten inode. If we find an overwritten ref that is
3447 * not the first ref, simply unlink it.
3449 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3450 cur->name, cur->name_len,
3451 &ow_inode, &ow_gen);
3455 ret = is_first_ref(sctx->parent_root,
3456 ow_inode, cur->dir, cur->name,
3461 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3466 ret = send_unlink(sctx, cur->full_path);
3473 * link/move the ref to the new place. If we have an orphan
3474 * inode, move it and update valid_path. If not, link or move
3475 * it depending on the inode mode.
3478 ret = send_rename(sctx, valid_path, cur->full_path);
3482 ret = fs_path_copy(valid_path, cur->full_path);
3486 if (S_ISDIR(sctx->cur_inode_mode)) {
3488 * Dirs can't be linked, so move it. For moved
3489 * dirs, we always have one new and one deleted
3490 * ref. The deleted ref is ignored later.
3492 ret = wait_for_parent_move(sctx, cur);
3498 ret = send_rename(sctx, valid_path,
3501 ret = fs_path_copy(valid_path,
3507 ret = send_link(sctx, cur->full_path,
3513 ret = dup_ref(cur, &check_dirs);
3518 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3520 * Check if we can already rmdir the directory. If not,
3521 * orphanize it. For every dir item inside that gets deleted
3522 * later, we do this check again and rmdir it then if possible.
3523 * See the use of check_dirs for more details.
3525 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3530 ret = send_rmdir(sctx, valid_path);
3533 } else if (!is_orphan) {
3534 ret = orphanize_inode(sctx, sctx->cur_ino,
3535 sctx->cur_inode_gen, valid_path);
3541 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3542 ret = dup_ref(cur, &check_dirs);
3546 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3547 !list_empty(&sctx->deleted_refs)) {
3549 * We have a moved dir. Add the old parent to check_dirs
3551 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3553 ret = dup_ref(cur, &check_dirs);
3556 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3558 * We have a non dir inode. Go through all deleted refs and
3559 * unlink them if they were not already overwritten by other
3562 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3563 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3564 sctx->cur_ino, sctx->cur_inode_gen,
3565 cur->name, cur->name_len);
3569 ret = send_unlink(sctx, cur->full_path);
3573 ret = dup_ref(cur, &check_dirs);
3578 * If the inode is still orphan, unlink the orphan. This may
3579 * happen when a previous inode did overwrite the first ref
3580 * of this inode and no new refs were added for the current
3581 * inode. Unlinking does not mean that the inode is deleted in
3582 * all cases. There may still be links to this inode in other
3586 ret = send_unlink(sctx, valid_path);
3593 * We did collect all parent dirs where cur_inode was once located. We
3594 * now go through all these dirs and check if they are pending for
3595 * deletion and if it's finally possible to perform the rmdir now.
3596 * We also update the inode stats of the parent dirs here.
3598 list_for_each_entry(cur, &check_dirs, list) {
3600 * In case we had refs into dirs that were not processed yet,
3601 * we don't need to do the utime and rmdir logic for these dirs.
3602 * The dir will be processed later.
3604 if (cur->dir > sctx->cur_ino)
3607 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3611 if (ret == inode_state_did_create ||
3612 ret == inode_state_no_change) {
3613 /* TODO delayed utimes */
3614 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3617 } else if (ret == inode_state_did_delete &&
3618 cur->dir != last_dir_ino_rm) {
3619 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3624 ret = get_cur_path(sctx, cur->dir,
3625 cur->dir_gen, valid_path);
3628 ret = send_rmdir(sctx, valid_path);
3631 last_dir_ino_rm = cur->dir;
3639 __free_recorded_refs(&check_dirs);
3640 free_recorded_refs(sctx);
3641 fs_path_free(valid_path);
3645 static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3646 struct fs_path *name, void *ctx, struct list_head *refs)
3649 struct send_ctx *sctx = ctx;
3653 p = fs_path_alloc();
3657 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
3662 ret = get_cur_path(sctx, dir, gen, p);
3665 ret = fs_path_add_path(p, name);
3669 ret = __record_ref(refs, dir, gen, p);
3677 static int __record_new_ref(int num, u64 dir, int index,
3678 struct fs_path *name,
3681 struct send_ctx *sctx = ctx;
3682 return record_ref(sctx->send_root, num, dir, index, name,
3683 ctx, &sctx->new_refs);
3687 static int __record_deleted_ref(int num, u64 dir, int index,
3688 struct fs_path *name,
3691 struct send_ctx *sctx = ctx;
3692 return record_ref(sctx->parent_root, num, dir, index, name,
3693 ctx, &sctx->deleted_refs);
3696 static int record_new_ref(struct send_ctx *sctx)
3700 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3701 sctx->cmp_key, 0, __record_new_ref, sctx);
3710 static int record_deleted_ref(struct send_ctx *sctx)
3714 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3715 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3724 struct find_ref_ctx {
3727 struct btrfs_root *root;
3728 struct fs_path *name;
3732 static int __find_iref(int num, u64 dir, int index,
3733 struct fs_path *name,
3736 struct find_ref_ctx *ctx = ctx_;
3740 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3741 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3743 * To avoid doing extra lookups we'll only do this if everything
3746 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3750 if (dir_gen != ctx->dir_gen)
3752 ctx->found_idx = num;
3758 static int find_iref(struct btrfs_root *root,
3759 struct btrfs_path *path,
3760 struct btrfs_key *key,
3761 u64 dir, u64 dir_gen, struct fs_path *name)
3764 struct find_ref_ctx ctx;
3768 ctx.dir_gen = dir_gen;
3772 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3776 if (ctx.found_idx == -1)
3779 return ctx.found_idx;
3782 static int __record_changed_new_ref(int num, u64 dir, int index,
3783 struct fs_path *name,
3788 struct send_ctx *sctx = ctx;
3790 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3795 ret = find_iref(sctx->parent_root, sctx->right_path,
3796 sctx->cmp_key, dir, dir_gen, name);
3798 ret = __record_new_ref(num, dir, index, name, sctx);
3805 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3806 struct fs_path *name,
3811 struct send_ctx *sctx = ctx;
3813 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3818 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3819 dir, dir_gen, name);
3821 ret = __record_deleted_ref(num, dir, index, name, sctx);
3828 static int record_changed_ref(struct send_ctx *sctx)
3832 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3833 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3836 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3837 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3847 * Record and process all refs at once. Needed when an inode changes the
3848 * generation number, which means that it was deleted and recreated.
3850 static int process_all_refs(struct send_ctx *sctx,
3851 enum btrfs_compare_tree_result cmd)
3854 struct btrfs_root *root;
3855 struct btrfs_path *path;
3856 struct btrfs_key key;
3857 struct btrfs_key found_key;
3858 struct extent_buffer *eb;
3860 iterate_inode_ref_t cb;
3861 int pending_move = 0;
3863 path = alloc_path_for_send();
3867 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3868 root = sctx->send_root;
3869 cb = __record_new_ref;
3870 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3871 root = sctx->parent_root;
3872 cb = __record_deleted_ref;
3874 btrfs_err(sctx->send_root->fs_info,
3875 "Wrong command %d in process_all_refs", cmd);
3880 key.objectid = sctx->cmp_key->objectid;
3881 key.type = BTRFS_INODE_REF_KEY;
3883 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3888 eb = path->nodes[0];
3889 slot = path->slots[0];
3890 if (slot >= btrfs_header_nritems(eb)) {
3891 ret = btrfs_next_leaf(root, path);
3899 btrfs_item_key_to_cpu(eb, &found_key, slot);
3901 if (found_key.objectid != key.objectid ||
3902 (found_key.type != BTRFS_INODE_REF_KEY &&
3903 found_key.type != BTRFS_INODE_EXTREF_KEY))
3906 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
3912 btrfs_release_path(path);
3914 ret = process_recorded_refs(sctx, &pending_move);
3915 /* Only applicable to an incremental send. */
3916 ASSERT(pending_move == 0);
3919 btrfs_free_path(path);
3923 static int send_set_xattr(struct send_ctx *sctx,
3924 struct fs_path *path,
3925 const char *name, int name_len,
3926 const char *data, int data_len)
3930 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3934 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3935 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3936 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3938 ret = send_cmd(sctx);
3945 static int send_remove_xattr(struct send_ctx *sctx,
3946 struct fs_path *path,
3947 const char *name, int name_len)
3951 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3955 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3956 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3958 ret = send_cmd(sctx);
3965 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3966 const char *name, int name_len,
3967 const char *data, int data_len,
3971 struct send_ctx *sctx = ctx;
3973 posix_acl_xattr_header dummy_acl;
3975 p = fs_path_alloc();
3980 * This hack is needed because empty acl's are stored as zero byte
3981 * data in xattrs. Problem with that is, that receiving these zero byte
3982 * acl's will fail later. To fix this, we send a dummy acl list that
3983 * only contains the version number and no entries.
3985 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3986 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3987 if (data_len == 0) {
3988 dummy_acl.a_version =
3989 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3990 data = (char *)&dummy_acl;
3991 data_len = sizeof(dummy_acl);
3995 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3999 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4006 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4007 const char *name, int name_len,
4008 const char *data, int data_len,
4012 struct send_ctx *sctx = ctx;
4015 p = fs_path_alloc();
4019 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4023 ret = send_remove_xattr(sctx, p, name, name_len);
4030 static int process_new_xattr(struct send_ctx *sctx)
4034 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4035 sctx->cmp_key, __process_new_xattr, sctx);
4040 static int process_deleted_xattr(struct send_ctx *sctx)
4044 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4045 sctx->cmp_key, __process_deleted_xattr, sctx);
4050 struct find_xattr_ctx {
4058 static int __find_xattr(int num, struct btrfs_key *di_key,
4059 const char *name, int name_len,
4060 const char *data, int data_len,
4061 u8 type, void *vctx)
4063 struct find_xattr_ctx *ctx = vctx;
4065 if (name_len == ctx->name_len &&
4066 strncmp(name, ctx->name, name_len) == 0) {
4067 ctx->found_idx = num;
4068 ctx->found_data_len = data_len;
4069 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
4070 if (!ctx->found_data)
4077 static int find_xattr(struct btrfs_root *root,
4078 struct btrfs_path *path,
4079 struct btrfs_key *key,
4080 const char *name, int name_len,
4081 char **data, int *data_len)
4084 struct find_xattr_ctx ctx;
4087 ctx.name_len = name_len;
4089 ctx.found_data = NULL;
4090 ctx.found_data_len = 0;
4092 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
4096 if (ctx.found_idx == -1)
4099 *data = ctx.found_data;
4100 *data_len = ctx.found_data_len;
4102 kfree(ctx.found_data);
4104 return ctx.found_idx;
4108 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4109 const char *name, int name_len,
4110 const char *data, int data_len,
4114 struct send_ctx *sctx = ctx;
4115 char *found_data = NULL;
4116 int found_data_len = 0;
4118 ret = find_xattr(sctx->parent_root, sctx->right_path,
4119 sctx->cmp_key, name, name_len, &found_data,
4121 if (ret == -ENOENT) {
4122 ret = __process_new_xattr(num, di_key, name, name_len, data,
4123 data_len, type, ctx);
4124 } else if (ret >= 0) {
4125 if (data_len != found_data_len ||
4126 memcmp(data, found_data, data_len)) {
4127 ret = __process_new_xattr(num, di_key, name, name_len,
4128 data, data_len, type, ctx);
4138 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4139 const char *name, int name_len,
4140 const char *data, int data_len,
4144 struct send_ctx *sctx = ctx;
4146 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4147 name, name_len, NULL, NULL);
4149 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4150 data_len, type, ctx);
4157 static int process_changed_xattr(struct send_ctx *sctx)
4161 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4162 sctx->cmp_key, __process_changed_new_xattr, sctx);
4165 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4166 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4172 static int process_all_new_xattrs(struct send_ctx *sctx)
4175 struct btrfs_root *root;
4176 struct btrfs_path *path;
4177 struct btrfs_key key;
4178 struct btrfs_key found_key;
4179 struct extent_buffer *eb;
4182 path = alloc_path_for_send();
4186 root = sctx->send_root;
4188 key.objectid = sctx->cmp_key->objectid;
4189 key.type = BTRFS_XATTR_ITEM_KEY;
4191 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4196 eb = path->nodes[0];
4197 slot = path->slots[0];
4198 if (slot >= btrfs_header_nritems(eb)) {
4199 ret = btrfs_next_leaf(root, path);
4202 } else if (ret > 0) {
4209 btrfs_item_key_to_cpu(eb, &found_key, slot);
4210 if (found_key.objectid != key.objectid ||
4211 found_key.type != key.type) {
4216 ret = iterate_dir_item(root, path, &found_key,
4217 __process_new_xattr, sctx);
4225 btrfs_free_path(path);
4229 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4231 struct btrfs_root *root = sctx->send_root;
4232 struct btrfs_fs_info *fs_info = root->fs_info;
4233 struct inode *inode;
4236 struct btrfs_key key;
4237 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4239 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4242 key.objectid = sctx->cur_ino;
4243 key.type = BTRFS_INODE_ITEM_KEY;
4246 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4248 return PTR_ERR(inode);
4250 if (offset + len > i_size_read(inode)) {
4251 if (offset > i_size_read(inode))
4254 len = offset - i_size_read(inode);
4259 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
4261 /* initial readahead */
4262 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4263 file_ra_state_init(&sctx->ra, inode->i_mapping);
4264 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4265 last_index - index + 1);
4267 while (index <= last_index) {
4268 unsigned cur_len = min_t(unsigned, len,
4269 PAGE_CACHE_SIZE - pg_offset);
4270 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4276 if (!PageUptodate(page)) {
4277 btrfs_readpage(NULL, page);
4279 if (!PageUptodate(page)) {
4281 page_cache_release(page);
4288 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4291 page_cache_release(page);
4303 * Read some bytes from the current inode/file and send a write command to
4306 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4310 ssize_t num_read = 0;
4312 p = fs_path_alloc();
4316 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4318 num_read = fill_read_buf(sctx, offset, len);
4319 if (num_read <= 0) {
4325 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4329 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4333 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4334 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4335 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
4337 ret = send_cmd(sctx);
4348 * Send a clone command to user space.
4350 static int send_clone(struct send_ctx *sctx,
4351 u64 offset, u32 len,
4352 struct clone_root *clone_root)
4358 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4359 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4360 clone_root->root->objectid, clone_root->ino,
4361 clone_root->offset);
4363 p = fs_path_alloc();
4367 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4371 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4375 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4376 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4377 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4379 if (clone_root->root == sctx->send_root) {
4380 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
4381 &gen, NULL, NULL, NULL, NULL);
4384 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4386 ret = get_inode_path(clone_root->root, clone_root->ino, p);
4391 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4392 clone_root->root->root_item.uuid);
4393 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
4394 le64_to_cpu(clone_root->root->root_item.ctransid));
4395 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4396 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4397 clone_root->offset);
4399 ret = send_cmd(sctx);
4408 * Send an update extent command to user space.
4410 static int send_update_extent(struct send_ctx *sctx,
4411 u64 offset, u32 len)
4416 p = fs_path_alloc();
4420 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4424 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4428 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4429 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4430 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4432 ret = send_cmd(sctx);
4440 static int send_hole(struct send_ctx *sctx, u64 end)
4442 struct fs_path *p = NULL;
4443 u64 offset = sctx->cur_inode_last_extent;
4447 p = fs_path_alloc();
4450 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4452 goto tlv_put_failure;
4453 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4454 while (offset < end) {
4455 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4457 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4460 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4461 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4462 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4463 ret = send_cmd(sctx);
4473 static int send_write_or_clone(struct send_ctx *sctx,
4474 struct btrfs_path *path,
4475 struct btrfs_key *key,
4476 struct clone_root *clone_root)
4479 struct btrfs_file_extent_item *ei;
4480 u64 offset = key->offset;
4485 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
4487 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4488 struct btrfs_file_extent_item);
4489 type = btrfs_file_extent_type(path->nodes[0], ei);
4490 if (type == BTRFS_FILE_EXTENT_INLINE) {
4491 len = btrfs_file_extent_inline_len(path->nodes[0],
4492 path->slots[0], ei);
4494 * it is possible the inline item won't cover the whole page,
4495 * but there may be items after this page. Make
4496 * sure to send the whole thing
4498 len = PAGE_CACHE_ALIGN(len);
4500 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
4503 if (offset + len > sctx->cur_inode_size)
4504 len = sctx->cur_inode_size - offset;
4510 if (clone_root && IS_ALIGNED(offset + len, bs)) {
4511 ret = send_clone(sctx, offset, len, clone_root);
4512 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4513 ret = send_update_extent(sctx, offset, len);
4517 if (l > BTRFS_SEND_READ_SIZE)
4518 l = BTRFS_SEND_READ_SIZE;
4519 ret = send_write(sctx, pos + offset, l);
4532 static int is_extent_unchanged(struct send_ctx *sctx,
4533 struct btrfs_path *left_path,
4534 struct btrfs_key *ekey)
4537 struct btrfs_key key;
4538 struct btrfs_path *path = NULL;
4539 struct extent_buffer *eb;
4541 struct btrfs_key found_key;
4542 struct btrfs_file_extent_item *ei;
4547 u64 left_offset_fixed;
4555 path = alloc_path_for_send();
4559 eb = left_path->nodes[0];
4560 slot = left_path->slots[0];
4561 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4562 left_type = btrfs_file_extent_type(eb, ei);
4564 if (left_type != BTRFS_FILE_EXTENT_REG) {
4568 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4569 left_len = btrfs_file_extent_num_bytes(eb, ei);
4570 left_offset = btrfs_file_extent_offset(eb, ei);
4571 left_gen = btrfs_file_extent_generation(eb, ei);
4574 * Following comments will refer to these graphics. L is the left
4575 * extents which we are checking at the moment. 1-8 are the right
4576 * extents that we iterate.
4579 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4582 * |--1--|-2b-|...(same as above)
4584 * Alternative situation. Happens on files where extents got split.
4586 * |-----------7-----------|-6-|
4588 * Alternative situation. Happens on files which got larger.
4591 * Nothing follows after 8.
4594 key.objectid = ekey->objectid;
4595 key.type = BTRFS_EXTENT_DATA_KEY;
4596 key.offset = ekey->offset;
4597 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4606 * Handle special case where the right side has no extents at all.
4608 eb = path->nodes[0];
4609 slot = path->slots[0];
4610 btrfs_item_key_to_cpu(eb, &found_key, slot);
4611 if (found_key.objectid != key.objectid ||
4612 found_key.type != key.type) {
4613 /* If we're a hole then just pretend nothing changed */
4614 ret = (left_disknr) ? 0 : 1;
4619 * We're now on 2a, 2b or 7.
4622 while (key.offset < ekey->offset + left_len) {
4623 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4624 right_type = btrfs_file_extent_type(eb, ei);
4625 if (right_type != BTRFS_FILE_EXTENT_REG) {
4630 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4631 right_len = btrfs_file_extent_num_bytes(eb, ei);
4632 right_offset = btrfs_file_extent_offset(eb, ei);
4633 right_gen = btrfs_file_extent_generation(eb, ei);
4636 * Are we at extent 8? If yes, we know the extent is changed.
4637 * This may only happen on the first iteration.
4639 if (found_key.offset + right_len <= ekey->offset) {
4640 /* If we're a hole just pretend nothing changed */
4641 ret = (left_disknr) ? 0 : 1;
4645 left_offset_fixed = left_offset;
4646 if (key.offset < ekey->offset) {
4647 /* Fix the right offset for 2a and 7. */
4648 right_offset += ekey->offset - key.offset;
4650 /* Fix the left offset for all behind 2a and 2b */
4651 left_offset_fixed += key.offset - ekey->offset;
4655 * Check if we have the same extent.
4657 if (left_disknr != right_disknr ||
4658 left_offset_fixed != right_offset ||
4659 left_gen != right_gen) {
4665 * Go to the next extent.
4667 ret = btrfs_next_item(sctx->parent_root, path);
4671 eb = path->nodes[0];
4672 slot = path->slots[0];
4673 btrfs_item_key_to_cpu(eb, &found_key, slot);
4675 if (ret || found_key.objectid != key.objectid ||
4676 found_key.type != key.type) {
4677 key.offset += right_len;
4680 if (found_key.offset != key.offset + right_len) {
4688 * We're now behind the left extent (treat as unchanged) or at the end
4689 * of the right side (treat as changed).
4691 if (key.offset >= ekey->offset + left_len)
4698 btrfs_free_path(path);
4702 static int get_last_extent(struct send_ctx *sctx, u64 offset)
4704 struct btrfs_path *path;
4705 struct btrfs_root *root = sctx->send_root;
4706 struct btrfs_file_extent_item *fi;
4707 struct btrfs_key key;
4712 path = alloc_path_for_send();
4716 sctx->cur_inode_last_extent = 0;
4718 key.objectid = sctx->cur_ino;
4719 key.type = BTRFS_EXTENT_DATA_KEY;
4720 key.offset = offset;
4721 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4725 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4726 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4729 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4730 struct btrfs_file_extent_item);
4731 type = btrfs_file_extent_type(path->nodes[0], fi);
4732 if (type == BTRFS_FILE_EXTENT_INLINE) {
4733 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4734 path->slots[0], fi);
4735 extent_end = ALIGN(key.offset + size,
4736 sctx->send_root->sectorsize);
4738 extent_end = key.offset +
4739 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4741 sctx->cur_inode_last_extent = extent_end;
4743 btrfs_free_path(path);
4747 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4748 struct btrfs_key *key)
4750 struct btrfs_file_extent_item *fi;
4755 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4758 if (sctx->cur_inode_last_extent == (u64)-1) {
4759 ret = get_last_extent(sctx, key->offset - 1);
4764 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4765 struct btrfs_file_extent_item);
4766 type = btrfs_file_extent_type(path->nodes[0], fi);
4767 if (type == BTRFS_FILE_EXTENT_INLINE) {
4768 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4769 path->slots[0], fi);
4770 extent_end = ALIGN(key->offset + size,
4771 sctx->send_root->sectorsize);
4773 extent_end = key->offset +
4774 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4777 if (path->slots[0] == 0 &&
4778 sctx->cur_inode_last_extent < key->offset) {
4780 * We might have skipped entire leafs that contained only
4781 * file extent items for our current inode. These leafs have
4782 * a generation number smaller (older) than the one in the
4783 * current leaf and the leaf our last extent came from, and
4784 * are located between these 2 leafs.
4786 ret = get_last_extent(sctx, key->offset - 1);
4791 if (sctx->cur_inode_last_extent < key->offset)
4792 ret = send_hole(sctx, key->offset);
4793 sctx->cur_inode_last_extent = extent_end;
4797 static int process_extent(struct send_ctx *sctx,
4798 struct btrfs_path *path,
4799 struct btrfs_key *key)
4801 struct clone_root *found_clone = NULL;
4804 if (S_ISLNK(sctx->cur_inode_mode))
4807 if (sctx->parent_root && !sctx->cur_inode_new) {
4808 ret = is_extent_unchanged(sctx, path, key);
4816 struct btrfs_file_extent_item *ei;
4819 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4820 struct btrfs_file_extent_item);
4821 type = btrfs_file_extent_type(path->nodes[0], ei);
4822 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4823 type == BTRFS_FILE_EXTENT_REG) {
4825 * The send spec does not have a prealloc command yet,
4826 * so just leave a hole for prealloc'ed extents until
4827 * we have enough commands queued up to justify rev'ing
4830 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4835 /* Have a hole, just skip it. */
4836 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4843 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4844 sctx->cur_inode_size, &found_clone);
4845 if (ret != -ENOENT && ret < 0)
4848 ret = send_write_or_clone(sctx, path, key, found_clone);
4852 ret = maybe_send_hole(sctx, path, key);
4857 static int process_all_extents(struct send_ctx *sctx)
4860 struct btrfs_root *root;
4861 struct btrfs_path *path;
4862 struct btrfs_key key;
4863 struct btrfs_key found_key;
4864 struct extent_buffer *eb;
4867 root = sctx->send_root;
4868 path = alloc_path_for_send();
4872 key.objectid = sctx->cmp_key->objectid;
4873 key.type = BTRFS_EXTENT_DATA_KEY;
4875 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4880 eb = path->nodes[0];
4881 slot = path->slots[0];
4883 if (slot >= btrfs_header_nritems(eb)) {
4884 ret = btrfs_next_leaf(root, path);
4887 } else if (ret > 0) {
4894 btrfs_item_key_to_cpu(eb, &found_key, slot);
4896 if (found_key.objectid != key.objectid ||
4897 found_key.type != key.type) {
4902 ret = process_extent(sctx, path, &found_key);
4910 btrfs_free_path(path);
4914 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4916 int *refs_processed)
4920 if (sctx->cur_ino == 0)
4922 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4923 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4925 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4928 ret = process_recorded_refs(sctx, pending_move);
4932 *refs_processed = 1;
4937 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4948 int pending_move = 0;
4949 int refs_processed = 0;
4951 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4957 * We have processed the refs and thus need to advance send_progress.
4958 * Now, calls to get_cur_xxx will take the updated refs of the current
4959 * inode into account.
4961 * On the other hand, if our current inode is a directory and couldn't
4962 * be moved/renamed because its parent was renamed/moved too and it has
4963 * a higher inode number, we can only move/rename our current inode
4964 * after we moved/renamed its parent. Therefore in this case operate on
4965 * the old path (pre move/rename) of our current inode, and the
4966 * move/rename will be performed later.
4968 if (refs_processed && !pending_move)
4969 sctx->send_progress = sctx->cur_ino + 1;
4971 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4973 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4976 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4977 &left_mode, &left_uid, &left_gid, NULL);
4981 if (!sctx->parent_root || sctx->cur_inode_new) {
4983 if (!S_ISLNK(sctx->cur_inode_mode))
4986 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4987 NULL, NULL, &right_mode, &right_uid,
4992 if (left_uid != right_uid || left_gid != right_gid)
4994 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4998 if (S_ISREG(sctx->cur_inode_mode)) {
4999 if (need_send_hole(sctx)) {
5000 if (sctx->cur_inode_last_extent == (u64)-1 ||
5001 sctx->cur_inode_last_extent <
5002 sctx->cur_inode_size) {
5003 ret = get_last_extent(sctx, (u64)-1);
5007 if (sctx->cur_inode_last_extent <
5008 sctx->cur_inode_size) {
5009 ret = send_hole(sctx, sctx->cur_inode_size);
5014 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5015 sctx->cur_inode_size);
5021 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5022 left_uid, left_gid);
5027 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5034 * If other directory inodes depended on our current directory
5035 * inode's move/rename, now do their move/rename operations.
5037 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5038 ret = apply_children_dir_moves(sctx);
5042 * Need to send that every time, no matter if it actually
5043 * changed between the two trees as we have done changes to
5044 * the inode before. If our inode is a directory and it's
5045 * waiting to be moved/renamed, we will send its utimes when
5046 * it's moved/renamed, therefore we don't need to do it here.
5048 sctx->send_progress = sctx->cur_ino + 1;
5049 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5058 static int changed_inode(struct send_ctx *sctx,
5059 enum btrfs_compare_tree_result result)
5062 struct btrfs_key *key = sctx->cmp_key;
5063 struct btrfs_inode_item *left_ii = NULL;
5064 struct btrfs_inode_item *right_ii = NULL;
5068 sctx->cur_ino = key->objectid;
5069 sctx->cur_inode_new_gen = 0;
5070 sctx->cur_inode_last_extent = (u64)-1;
5073 * Set send_progress to current inode. This will tell all get_cur_xxx
5074 * functions that the current inode's refs are not updated yet. Later,
5075 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5077 sctx->send_progress = sctx->cur_ino;
5079 if (result == BTRFS_COMPARE_TREE_NEW ||
5080 result == BTRFS_COMPARE_TREE_CHANGED) {
5081 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5082 sctx->left_path->slots[0],
5083 struct btrfs_inode_item);
5084 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5087 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5088 sctx->right_path->slots[0],
5089 struct btrfs_inode_item);
5090 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5093 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5094 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5095 sctx->right_path->slots[0],
5096 struct btrfs_inode_item);
5098 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5102 * The cur_ino = root dir case is special here. We can't treat
5103 * the inode as deleted+reused because it would generate a
5104 * stream that tries to delete/mkdir the root dir.
5106 if (left_gen != right_gen &&
5107 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5108 sctx->cur_inode_new_gen = 1;
5111 if (result == BTRFS_COMPARE_TREE_NEW) {
5112 sctx->cur_inode_gen = left_gen;
5113 sctx->cur_inode_new = 1;
5114 sctx->cur_inode_deleted = 0;
5115 sctx->cur_inode_size = btrfs_inode_size(
5116 sctx->left_path->nodes[0], left_ii);
5117 sctx->cur_inode_mode = btrfs_inode_mode(
5118 sctx->left_path->nodes[0], left_ii);
5119 sctx->cur_inode_rdev = btrfs_inode_rdev(
5120 sctx->left_path->nodes[0], left_ii);
5121 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5122 ret = send_create_inode_if_needed(sctx);
5123 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5124 sctx->cur_inode_gen = right_gen;
5125 sctx->cur_inode_new = 0;
5126 sctx->cur_inode_deleted = 1;
5127 sctx->cur_inode_size = btrfs_inode_size(
5128 sctx->right_path->nodes[0], right_ii);
5129 sctx->cur_inode_mode = btrfs_inode_mode(
5130 sctx->right_path->nodes[0], right_ii);
5131 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
5133 * We need to do some special handling in case the inode was
5134 * reported as changed with a changed generation number. This
5135 * means that the original inode was deleted and new inode
5136 * reused the same inum. So we have to treat the old inode as
5137 * deleted and the new one as new.
5139 if (sctx->cur_inode_new_gen) {
5141 * First, process the inode as if it was deleted.
5143 sctx->cur_inode_gen = right_gen;
5144 sctx->cur_inode_new = 0;
5145 sctx->cur_inode_deleted = 1;
5146 sctx->cur_inode_size = btrfs_inode_size(
5147 sctx->right_path->nodes[0], right_ii);
5148 sctx->cur_inode_mode = btrfs_inode_mode(
5149 sctx->right_path->nodes[0], right_ii);
5150 ret = process_all_refs(sctx,
5151 BTRFS_COMPARE_TREE_DELETED);
5156 * Now process the inode as if it was new.
5158 sctx->cur_inode_gen = left_gen;
5159 sctx->cur_inode_new = 1;
5160 sctx->cur_inode_deleted = 0;
5161 sctx->cur_inode_size = btrfs_inode_size(
5162 sctx->left_path->nodes[0], left_ii);
5163 sctx->cur_inode_mode = btrfs_inode_mode(
5164 sctx->left_path->nodes[0], left_ii);
5165 sctx->cur_inode_rdev = btrfs_inode_rdev(
5166 sctx->left_path->nodes[0], left_ii);
5167 ret = send_create_inode_if_needed(sctx);
5171 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5175 * Advance send_progress now as we did not get into
5176 * process_recorded_refs_if_needed in the new_gen case.
5178 sctx->send_progress = sctx->cur_ino + 1;
5181 * Now process all extents and xattrs of the inode as if
5182 * they were all new.
5184 ret = process_all_extents(sctx);
5187 ret = process_all_new_xattrs(sctx);
5191 sctx->cur_inode_gen = left_gen;
5192 sctx->cur_inode_new = 0;
5193 sctx->cur_inode_new_gen = 0;
5194 sctx->cur_inode_deleted = 0;
5195 sctx->cur_inode_size = btrfs_inode_size(
5196 sctx->left_path->nodes[0], left_ii);
5197 sctx->cur_inode_mode = btrfs_inode_mode(
5198 sctx->left_path->nodes[0], left_ii);
5207 * We have to process new refs before deleted refs, but compare_trees gives us
5208 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5209 * first and later process them in process_recorded_refs.
5210 * For the cur_inode_new_gen case, we skip recording completely because
5211 * changed_inode did already initiate processing of refs. The reason for this is
5212 * that in this case, compare_tree actually compares the refs of 2 different
5213 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5214 * refs of the right tree as deleted and all refs of the left tree as new.
5216 static int changed_ref(struct send_ctx *sctx,
5217 enum btrfs_compare_tree_result result)
5221 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5223 if (!sctx->cur_inode_new_gen &&
5224 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5225 if (result == BTRFS_COMPARE_TREE_NEW)
5226 ret = record_new_ref(sctx);
5227 else if (result == BTRFS_COMPARE_TREE_DELETED)
5228 ret = record_deleted_ref(sctx);
5229 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5230 ret = record_changed_ref(sctx);
5237 * Process new/deleted/changed xattrs. We skip processing in the
5238 * cur_inode_new_gen case because changed_inode did already initiate processing
5239 * of xattrs. The reason is the same as in changed_ref
5241 static int changed_xattr(struct send_ctx *sctx,
5242 enum btrfs_compare_tree_result result)
5246 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5248 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5249 if (result == BTRFS_COMPARE_TREE_NEW)
5250 ret = process_new_xattr(sctx);
5251 else if (result == BTRFS_COMPARE_TREE_DELETED)
5252 ret = process_deleted_xattr(sctx);
5253 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5254 ret = process_changed_xattr(sctx);
5261 * Process new/deleted/changed extents. We skip processing in the
5262 * cur_inode_new_gen case because changed_inode did already initiate processing
5263 * of extents. The reason is the same as in changed_ref
5265 static int changed_extent(struct send_ctx *sctx,
5266 enum btrfs_compare_tree_result result)
5270 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5272 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5273 if (result != BTRFS_COMPARE_TREE_DELETED)
5274 ret = process_extent(sctx, sctx->left_path,
5281 static int dir_changed(struct send_ctx *sctx, u64 dir)
5283 u64 orig_gen, new_gen;
5286 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5291 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5296 return (orig_gen != new_gen) ? 1 : 0;
5299 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5300 struct btrfs_key *key)
5302 struct btrfs_inode_extref *extref;
5303 struct extent_buffer *leaf;
5304 u64 dirid = 0, last_dirid = 0;
5311 /* Easy case, just check this one dirid */
5312 if (key->type == BTRFS_INODE_REF_KEY) {
5313 dirid = key->offset;
5315 ret = dir_changed(sctx, dirid);
5319 leaf = path->nodes[0];
5320 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5321 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5322 while (cur_offset < item_size) {
5323 extref = (struct btrfs_inode_extref *)(ptr +
5325 dirid = btrfs_inode_extref_parent(leaf, extref);
5326 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5327 cur_offset += ref_name_len + sizeof(*extref);
5328 if (dirid == last_dirid)
5330 ret = dir_changed(sctx, dirid);
5340 * Updates compare related fields in sctx and simply forwards to the actual
5341 * changed_xxx functions.
5343 static int changed_cb(struct btrfs_root *left_root,
5344 struct btrfs_root *right_root,
5345 struct btrfs_path *left_path,
5346 struct btrfs_path *right_path,
5347 struct btrfs_key *key,
5348 enum btrfs_compare_tree_result result,
5352 struct send_ctx *sctx = ctx;
5354 if (result == BTRFS_COMPARE_TREE_SAME) {
5355 if (key->type == BTRFS_INODE_REF_KEY ||
5356 key->type == BTRFS_INODE_EXTREF_KEY) {
5357 ret = compare_refs(sctx, left_path, key);
5362 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5363 return maybe_send_hole(sctx, left_path, key);
5367 result = BTRFS_COMPARE_TREE_CHANGED;
5371 sctx->left_path = left_path;
5372 sctx->right_path = right_path;
5373 sctx->cmp_key = key;
5375 ret = finish_inode_if_needed(sctx, 0);
5379 /* Ignore non-FS objects */
5380 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5381 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5384 if (key->type == BTRFS_INODE_ITEM_KEY)
5385 ret = changed_inode(sctx, result);
5386 else if (key->type == BTRFS_INODE_REF_KEY ||
5387 key->type == BTRFS_INODE_EXTREF_KEY)
5388 ret = changed_ref(sctx, result);
5389 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5390 ret = changed_xattr(sctx, result);
5391 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5392 ret = changed_extent(sctx, result);
5398 static int full_send_tree(struct send_ctx *sctx)
5401 struct btrfs_root *send_root = sctx->send_root;
5402 struct btrfs_key key;
5403 struct btrfs_key found_key;
5404 struct btrfs_path *path;
5405 struct extent_buffer *eb;
5408 path = alloc_path_for_send();
5412 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5413 key.type = BTRFS_INODE_ITEM_KEY;
5416 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5423 eb = path->nodes[0];
5424 slot = path->slots[0];
5425 btrfs_item_key_to_cpu(eb, &found_key, slot);
5427 ret = changed_cb(send_root, NULL, path, NULL,
5428 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5432 key.objectid = found_key.objectid;
5433 key.type = found_key.type;
5434 key.offset = found_key.offset + 1;
5436 ret = btrfs_next_item(send_root, path);
5446 ret = finish_inode_if_needed(sctx, 1);
5449 btrfs_free_path(path);
5453 static int send_subvol(struct send_ctx *sctx)
5457 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5458 ret = send_header(sctx);
5463 ret = send_subvol_begin(sctx);
5467 if (sctx->parent_root) {
5468 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5472 ret = finish_inode_if_needed(sctx, 1);
5476 ret = full_send_tree(sctx);
5482 free_recorded_refs(sctx);
5486 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5488 spin_lock(&root->root_item_lock);
5489 root->send_in_progress--;
5491 * Not much left to do, we don't know why it's unbalanced and
5492 * can't blindly reset it to 0.
5494 if (root->send_in_progress < 0)
5495 btrfs_err(root->fs_info,
5496 "send_in_progres unbalanced %d root %llu",
5497 root->send_in_progress, root->root_key.objectid);
5498 spin_unlock(&root->root_item_lock);
5501 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5504 struct btrfs_root *send_root;
5505 struct btrfs_root *clone_root;
5506 struct btrfs_fs_info *fs_info;
5507 struct btrfs_ioctl_send_args *arg = NULL;
5508 struct btrfs_key key;
5509 struct send_ctx *sctx = NULL;
5511 u64 *clone_sources_tmp = NULL;
5512 int clone_sources_to_rollback = 0;
5513 int sort_clone_roots = 0;
5516 if (!capable(CAP_SYS_ADMIN))
5519 send_root = BTRFS_I(file_inode(mnt_file))->root;
5520 fs_info = send_root->fs_info;
5523 * The subvolume must remain read-only during send, protect against
5524 * making it RW. This also protects against deletion.
5526 spin_lock(&send_root->root_item_lock);
5527 send_root->send_in_progress++;
5528 spin_unlock(&send_root->root_item_lock);
5531 * This is done when we lookup the root, it should already be complete
5532 * by the time we get here.
5534 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5537 * Userspace tools do the checks and warn the user if it's
5540 if (!btrfs_root_readonly(send_root)) {
5545 arg = memdup_user(arg_, sizeof(*arg));
5552 if (!access_ok(VERIFY_READ, arg->clone_sources,
5553 sizeof(*arg->clone_sources) *
5554 arg->clone_sources_count)) {
5559 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
5564 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5570 INIT_LIST_HEAD(&sctx->new_refs);
5571 INIT_LIST_HEAD(&sctx->deleted_refs);
5572 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5573 INIT_LIST_HEAD(&sctx->name_cache_list);
5575 sctx->flags = arg->flags;
5577 sctx->send_filp = fget(arg->send_fd);
5578 if (!sctx->send_filp) {
5583 sctx->send_root = send_root;
5585 * Unlikely but possible, if the subvolume is marked for deletion but
5586 * is slow to remove the directory entry, send can still be started
5588 if (btrfs_root_dead(sctx->send_root)) {
5593 sctx->clone_roots_cnt = arg->clone_sources_count;
5595 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5596 sctx->send_buf = vmalloc(sctx->send_max_size);
5597 if (!sctx->send_buf) {
5602 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5603 if (!sctx->read_buf) {
5608 sctx->pending_dir_moves = RB_ROOT;
5609 sctx->waiting_dir_moves = RB_ROOT;
5610 sctx->orphan_dirs = RB_ROOT;
5612 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5613 (arg->clone_sources_count + 1));
5614 if (!sctx->clone_roots) {
5619 if (arg->clone_sources_count) {
5620 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5621 sizeof(*arg->clone_sources));
5622 if (!clone_sources_tmp) {
5627 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5628 arg->clone_sources_count *
5629 sizeof(*arg->clone_sources));
5635 for (i = 0; i < arg->clone_sources_count; i++) {
5636 key.objectid = clone_sources_tmp[i];
5637 key.type = BTRFS_ROOT_ITEM_KEY;
5638 key.offset = (u64)-1;
5640 index = srcu_read_lock(&fs_info->subvol_srcu);
5642 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
5643 if (IS_ERR(clone_root)) {
5644 srcu_read_unlock(&fs_info->subvol_srcu, index);
5645 ret = PTR_ERR(clone_root);
5648 clone_sources_to_rollback = i + 1;
5649 spin_lock(&clone_root->root_item_lock);
5650 clone_root->send_in_progress++;
5651 if (!btrfs_root_readonly(clone_root)) {
5652 spin_unlock(&clone_root->root_item_lock);
5653 srcu_read_unlock(&fs_info->subvol_srcu, index);
5657 spin_unlock(&clone_root->root_item_lock);
5658 srcu_read_unlock(&fs_info->subvol_srcu, index);
5660 sctx->clone_roots[i].root = clone_root;
5662 vfree(clone_sources_tmp);
5663 clone_sources_tmp = NULL;
5666 if (arg->parent_root) {
5667 key.objectid = arg->parent_root;
5668 key.type = BTRFS_ROOT_ITEM_KEY;
5669 key.offset = (u64)-1;
5671 index = srcu_read_lock(&fs_info->subvol_srcu);
5673 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
5674 if (IS_ERR(sctx->parent_root)) {
5675 srcu_read_unlock(&fs_info->subvol_srcu, index);
5676 ret = PTR_ERR(sctx->parent_root);
5680 spin_lock(&sctx->parent_root->root_item_lock);
5681 sctx->parent_root->send_in_progress++;
5682 if (!btrfs_root_readonly(sctx->parent_root) ||
5683 btrfs_root_dead(sctx->parent_root)) {
5684 spin_unlock(&sctx->parent_root->root_item_lock);
5685 srcu_read_unlock(&fs_info->subvol_srcu, index);
5689 spin_unlock(&sctx->parent_root->root_item_lock);
5691 srcu_read_unlock(&fs_info->subvol_srcu, index);
5695 * Clones from send_root are allowed, but only if the clone source
5696 * is behind the current send position. This is checked while searching
5697 * for possible clone sources.
5699 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5701 /* We do a bsearch later */
5702 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5703 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5705 sort_clone_roots = 1;
5707 current->journal_info = (void *)BTRFS_SEND_TRANS_STUB;
5708 ret = send_subvol(sctx);
5709 current->journal_info = NULL;
5713 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5714 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5717 ret = send_cmd(sctx);
5723 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5724 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5726 struct pending_dir_move *pm;
5728 n = rb_first(&sctx->pending_dir_moves);
5729 pm = rb_entry(n, struct pending_dir_move, node);
5730 while (!list_empty(&pm->list)) {
5731 struct pending_dir_move *pm2;
5733 pm2 = list_first_entry(&pm->list,
5734 struct pending_dir_move, list);
5735 free_pending_move(sctx, pm2);
5737 free_pending_move(sctx, pm);
5740 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5741 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5743 struct waiting_dir_move *dm;
5745 n = rb_first(&sctx->waiting_dir_moves);
5746 dm = rb_entry(n, struct waiting_dir_move, node);
5747 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5751 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5752 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5754 struct orphan_dir_info *odi;
5756 n = rb_first(&sctx->orphan_dirs);
5757 odi = rb_entry(n, struct orphan_dir_info, node);
5758 free_orphan_dir_info(sctx, odi);
5761 if (sort_clone_roots) {
5762 for (i = 0; i < sctx->clone_roots_cnt; i++)
5763 btrfs_root_dec_send_in_progress(
5764 sctx->clone_roots[i].root);
5766 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5767 btrfs_root_dec_send_in_progress(
5768 sctx->clone_roots[i].root);
5770 btrfs_root_dec_send_in_progress(send_root);
5772 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5773 btrfs_root_dec_send_in_progress(sctx->parent_root);
5776 vfree(clone_sources_tmp);
5779 if (sctx->send_filp)
5780 fput(sctx->send_filp);
5782 vfree(sctx->clone_roots);
5783 vfree(sctx->send_buf);
5784 vfree(sctx->read_buf);
5786 name_cache_free(sctx);