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;
234 struct list_head update_refs;
237 struct waiting_dir_move {
241 * There might be some directory that could not be removed because it
242 * was waiting for this directory inode to be moved first. Therefore
243 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
248 struct orphan_dir_info {
254 struct name_cache_entry {
255 struct list_head list;
257 * radix_tree has only 32bit entries but we need to handle 64bit inums.
258 * We use the lower 32bit of the 64bit inum to store it in the tree. If
259 * more then one inum would fall into the same entry, we use radix_list
260 * to store the additional entries. radix_list is also used to store
261 * entries where two entries have the same inum but different
264 struct list_head radix_list;
270 int need_later_update;
275 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
277 static struct waiting_dir_move *
278 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
280 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
282 static int need_send_hole(struct send_ctx *sctx)
284 return (sctx->parent_root && !sctx->cur_inode_new &&
285 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
286 S_ISREG(sctx->cur_inode_mode));
289 static void fs_path_reset(struct fs_path *p)
292 p->start = p->buf + p->buf_len - 1;
302 static struct fs_path *fs_path_alloc(void)
306 p = kmalloc(sizeof(*p), GFP_NOFS);
310 p->buf = p->inline_buf;
311 p->buf_len = FS_PATH_INLINE_SIZE;
316 static struct fs_path *fs_path_alloc_reversed(void)
328 static void fs_path_free(struct fs_path *p)
332 if (p->buf != p->inline_buf)
337 static int fs_path_len(struct fs_path *p)
339 return p->end - p->start;
342 static int fs_path_ensure_buf(struct fs_path *p, int len)
350 if (p->buf_len >= len)
353 if (len > PATH_MAX) {
358 path_len = p->end - p->start;
359 old_buf_len = p->buf_len;
362 * First time the inline_buf does not suffice
364 if (p->buf == p->inline_buf) {
365 tmp_buf = kmalloc(len, GFP_NOFS);
367 memcpy(tmp_buf, p->buf, old_buf_len);
369 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
375 * The real size of the buffer is bigger, this will let the fast path
376 * happen most of the time
378 p->buf_len = ksize(p->buf);
381 tmp_buf = p->buf + old_buf_len - path_len - 1;
382 p->end = p->buf + p->buf_len - 1;
383 p->start = p->end - path_len;
384 memmove(p->start, tmp_buf, path_len + 1);
387 p->end = p->start + path_len;
392 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
398 new_len = p->end - p->start + name_len;
399 if (p->start != p->end)
401 ret = fs_path_ensure_buf(p, new_len);
406 if (p->start != p->end)
408 p->start -= name_len;
409 *prepared = p->start;
411 if (p->start != p->end)
422 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
427 ret = fs_path_prepare_for_add(p, name_len, &prepared);
430 memcpy(prepared, name, name_len);
436 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
441 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
444 memcpy(prepared, p2->start, p2->end - p2->start);
450 static int fs_path_add_from_extent_buffer(struct fs_path *p,
451 struct extent_buffer *eb,
452 unsigned long off, int len)
457 ret = fs_path_prepare_for_add(p, len, &prepared);
461 read_extent_buffer(eb, prepared, off, len);
467 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
471 p->reversed = from->reversed;
474 ret = fs_path_add_path(p, from);
480 static void fs_path_unreverse(struct fs_path *p)
489 len = p->end - p->start;
491 p->end = p->start + len;
492 memmove(p->start, tmp, len + 1);
496 static struct btrfs_path *alloc_path_for_send(void)
498 struct btrfs_path *path;
500 path = btrfs_alloc_path();
503 path->search_commit_root = 1;
504 path->skip_locking = 1;
505 path->need_commit_sem = 1;
509 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
519 ret = vfs_write(filp, (__force const char __user *)buf + pos,
521 /* TODO handle that correctly */
522 /*if (ret == -ERESTARTSYS) {
541 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
543 struct btrfs_tlv_header *hdr;
544 int total_len = sizeof(*hdr) + len;
545 int left = sctx->send_max_size - sctx->send_size;
547 if (unlikely(left < total_len))
550 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
551 hdr->tlv_type = cpu_to_le16(attr);
552 hdr->tlv_len = cpu_to_le16(len);
553 memcpy(hdr + 1, data, len);
554 sctx->send_size += total_len;
559 #define TLV_PUT_DEFINE_INT(bits) \
560 static int tlv_put_u##bits(struct send_ctx *sctx, \
561 u##bits attr, u##bits value) \
563 __le##bits __tmp = cpu_to_le##bits(value); \
564 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
567 TLV_PUT_DEFINE_INT(64)
569 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
570 const char *str, int len)
574 return tlv_put(sctx, attr, str, len);
577 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
580 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
583 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
584 struct extent_buffer *eb,
585 struct btrfs_timespec *ts)
587 struct btrfs_timespec bts;
588 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
589 return tlv_put(sctx, attr, &bts, sizeof(bts));
593 #define TLV_PUT(sctx, attrtype, attrlen, data) \
595 ret = tlv_put(sctx, attrtype, attrlen, data); \
597 goto tlv_put_failure; \
600 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
602 ret = tlv_put_u##bits(sctx, attrtype, value); \
604 goto tlv_put_failure; \
607 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
608 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
609 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
610 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
611 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
613 ret = tlv_put_string(sctx, attrtype, str, len); \
615 goto tlv_put_failure; \
617 #define TLV_PUT_PATH(sctx, attrtype, p) \
619 ret = tlv_put_string(sctx, attrtype, p->start, \
620 p->end - p->start); \
622 goto tlv_put_failure; \
624 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
626 ret = tlv_put_uuid(sctx, attrtype, uuid); \
628 goto tlv_put_failure; \
630 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
632 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
634 goto tlv_put_failure; \
637 static int send_header(struct send_ctx *sctx)
639 struct btrfs_stream_header hdr;
641 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
642 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
644 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
649 * For each command/item we want to send to userspace, we call this function.
651 static int begin_cmd(struct send_ctx *sctx, int cmd)
653 struct btrfs_cmd_header *hdr;
655 if (WARN_ON(!sctx->send_buf))
658 BUG_ON(sctx->send_size);
660 sctx->send_size += sizeof(*hdr);
661 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
662 hdr->cmd = cpu_to_le16(cmd);
667 static int send_cmd(struct send_ctx *sctx)
670 struct btrfs_cmd_header *hdr;
673 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
674 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
677 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
678 hdr->crc = cpu_to_le32(crc);
680 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
683 sctx->total_send_size += sctx->send_size;
684 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
691 * Sends a move instruction to user space
693 static int send_rename(struct send_ctx *sctx,
694 struct fs_path *from, struct fs_path *to)
698 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
700 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
704 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
705 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
707 ret = send_cmd(sctx);
715 * Sends a link instruction to user space
717 static int send_link(struct send_ctx *sctx,
718 struct fs_path *path, struct fs_path *lnk)
722 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
724 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
728 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
729 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
731 ret = send_cmd(sctx);
739 * Sends an unlink instruction to user space
741 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
745 verbose_printk("btrfs: send_unlink %s\n", path->start);
747 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
751 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
753 ret = send_cmd(sctx);
761 * Sends a rmdir instruction to user space
763 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
767 verbose_printk("btrfs: send_rmdir %s\n", path->start);
769 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
773 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
775 ret = send_cmd(sctx);
783 * Helper function to retrieve some fields from an inode item.
785 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
786 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
790 struct btrfs_inode_item *ii;
791 struct btrfs_key key;
794 key.type = BTRFS_INODE_ITEM_KEY;
796 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
803 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
804 struct btrfs_inode_item);
806 *size = btrfs_inode_size(path->nodes[0], ii);
808 *gen = btrfs_inode_generation(path->nodes[0], ii);
810 *mode = btrfs_inode_mode(path->nodes[0], ii);
812 *uid = btrfs_inode_uid(path->nodes[0], ii);
814 *gid = btrfs_inode_gid(path->nodes[0], ii);
816 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
821 static int get_inode_info(struct btrfs_root *root,
822 u64 ino, u64 *size, u64 *gen,
823 u64 *mode, u64 *uid, u64 *gid,
826 struct btrfs_path *path;
829 path = alloc_path_for_send();
832 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
834 btrfs_free_path(path);
838 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
843 * Helper function to iterate the entries in ONE btrfs_inode_ref or
844 * btrfs_inode_extref.
845 * The iterate callback may return a non zero value to stop iteration. This can
846 * be a negative value for error codes or 1 to simply stop it.
848 * path must point to the INODE_REF or INODE_EXTREF when called.
850 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
851 struct btrfs_key *found_key, int resolve,
852 iterate_inode_ref_t iterate, void *ctx)
854 struct extent_buffer *eb = path->nodes[0];
855 struct btrfs_item *item;
856 struct btrfs_inode_ref *iref;
857 struct btrfs_inode_extref *extref;
858 struct btrfs_path *tmp_path;
862 int slot = path->slots[0];
869 unsigned long name_off;
870 unsigned long elem_size;
873 p = fs_path_alloc_reversed();
877 tmp_path = alloc_path_for_send();
884 if (found_key->type == BTRFS_INODE_REF_KEY) {
885 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
886 struct btrfs_inode_ref);
887 item = btrfs_item_nr(slot);
888 total = btrfs_item_size(eb, item);
889 elem_size = sizeof(*iref);
891 ptr = btrfs_item_ptr_offset(eb, slot);
892 total = btrfs_item_size_nr(eb, slot);
893 elem_size = sizeof(*extref);
896 while (cur < total) {
899 if (found_key->type == BTRFS_INODE_REF_KEY) {
900 iref = (struct btrfs_inode_ref *)(ptr + cur);
901 name_len = btrfs_inode_ref_name_len(eb, iref);
902 name_off = (unsigned long)(iref + 1);
903 index = btrfs_inode_ref_index(eb, iref);
904 dir = found_key->offset;
906 extref = (struct btrfs_inode_extref *)(ptr + cur);
907 name_len = btrfs_inode_extref_name_len(eb, extref);
908 name_off = (unsigned long)&extref->name;
909 index = btrfs_inode_extref_index(eb, extref);
910 dir = btrfs_inode_extref_parent(eb, extref);
914 start = btrfs_ref_to_path(root, tmp_path, name_len,
918 ret = PTR_ERR(start);
921 if (start < p->buf) {
922 /* overflow , try again with larger buffer */
923 ret = fs_path_ensure_buf(p,
924 p->buf_len + p->buf - start);
927 start = btrfs_ref_to_path(root, tmp_path,
932 ret = PTR_ERR(start);
935 BUG_ON(start < p->buf);
939 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
945 cur += elem_size + name_len;
946 ret = iterate(num, dir, index, p, ctx);
953 btrfs_free_path(tmp_path);
958 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
959 const char *name, int name_len,
960 const char *data, int data_len,
964 * Helper function to iterate the entries in ONE btrfs_dir_item.
965 * The iterate callback may return a non zero value to stop iteration. This can
966 * be a negative value for error codes or 1 to simply stop it.
968 * path must point to the dir item when called.
970 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
971 struct btrfs_key *found_key,
972 iterate_dir_item_t iterate, void *ctx)
975 struct extent_buffer *eb;
976 struct btrfs_item *item;
977 struct btrfs_dir_item *di;
978 struct btrfs_key di_key;
991 * Start with a small buffer (1 page). If later we end up needing more
992 * space, which can happen for xattrs on a fs with a leaf size greater
993 * then the page size, attempt to increase the buffer. Typically xattr
997 buf = kmalloc(buf_len, GFP_NOFS);
1003 eb = path->nodes[0];
1004 slot = path->slots[0];
1005 item = btrfs_item_nr(slot);
1006 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1009 total = btrfs_item_size(eb, item);
1012 while (cur < total) {
1013 name_len = btrfs_dir_name_len(eb, di);
1014 data_len = btrfs_dir_data_len(eb, di);
1015 type = btrfs_dir_type(eb, di);
1016 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1018 if (type == BTRFS_FT_XATTR) {
1019 if (name_len > XATTR_NAME_MAX) {
1020 ret = -ENAMETOOLONG;
1023 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root)) {
1031 if (name_len + data_len > PATH_MAX) {
1032 ret = -ENAMETOOLONG;
1037 if (name_len + data_len > buf_len) {
1038 buf_len = name_len + data_len;
1039 if (is_vmalloc_addr(buf)) {
1043 char *tmp = krealloc(buf, buf_len,
1044 GFP_NOFS | __GFP_NOWARN);
1051 buf = vmalloc(buf_len);
1059 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1060 name_len + data_len);
1062 len = sizeof(*di) + name_len + data_len;
1063 di = (struct btrfs_dir_item *)((char *)di + len);
1066 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1067 data_len, type, ctx);
1083 static int __copy_first_ref(int num, u64 dir, int index,
1084 struct fs_path *p, void *ctx)
1087 struct fs_path *pt = ctx;
1089 ret = fs_path_copy(pt, p);
1093 /* we want the first only */
1098 * Retrieve the first path of an inode. If an inode has more then one
1099 * ref/hardlink, this is ignored.
1101 static int get_inode_path(struct btrfs_root *root,
1102 u64 ino, struct fs_path *path)
1105 struct btrfs_key key, found_key;
1106 struct btrfs_path *p;
1108 p = alloc_path_for_send();
1112 fs_path_reset(path);
1115 key.type = BTRFS_INODE_REF_KEY;
1118 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1125 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1126 if (found_key.objectid != ino ||
1127 (found_key.type != BTRFS_INODE_REF_KEY &&
1128 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1133 ret = iterate_inode_ref(root, p, &found_key, 1,
1134 __copy_first_ref, path);
1144 struct backref_ctx {
1145 struct send_ctx *sctx;
1147 struct btrfs_path *path;
1148 /* number of total found references */
1152 * used for clones found in send_root. clones found behind cur_objectid
1153 * and cur_offset are not considered as allowed clones.
1158 /* may be truncated in case it's the last extent in a file */
1161 /* Just to check for bugs in backref resolving */
1165 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1167 u64 root = (u64)(uintptr_t)key;
1168 struct clone_root *cr = (struct clone_root *)elt;
1170 if (root < cr->root->objectid)
1172 if (root > cr->root->objectid)
1177 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1179 struct clone_root *cr1 = (struct clone_root *)e1;
1180 struct clone_root *cr2 = (struct clone_root *)e2;
1182 if (cr1->root->objectid < cr2->root->objectid)
1184 if (cr1->root->objectid > cr2->root->objectid)
1190 * Called for every backref that is found for the current extent.
1191 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1193 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1195 struct backref_ctx *bctx = ctx_;
1196 struct clone_root *found;
1200 /* First check if the root is in the list of accepted clone sources */
1201 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1202 bctx->sctx->clone_roots_cnt,
1203 sizeof(struct clone_root),
1204 __clone_root_cmp_bsearch);
1208 if (found->root == bctx->sctx->send_root &&
1209 ino == bctx->cur_objectid &&
1210 offset == bctx->cur_offset) {
1211 bctx->found_itself = 1;
1215 * There are inodes that have extents that lie behind its i_size. Don't
1216 * accept clones from these extents.
1218 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1220 btrfs_release_path(bctx->path);
1224 if (offset + bctx->extent_len > i_size)
1228 * Make sure we don't consider clones from send_root that are
1229 * behind the current inode/offset.
1231 if (found->root == bctx->sctx->send_root) {
1233 * TODO for the moment we don't accept clones from the inode
1234 * that is currently send. We may change this when
1235 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1238 if (ino >= bctx->cur_objectid)
1241 if (ino > bctx->cur_objectid)
1243 if (offset + bctx->extent_len > bctx->cur_offset)
1249 found->found_refs++;
1250 if (ino < found->ino) {
1252 found->offset = offset;
1253 } else if (found->ino == ino) {
1255 * same extent found more then once in the same file.
1257 if (found->offset > offset + bctx->extent_len)
1258 found->offset = offset;
1265 * Given an inode, offset and extent item, it finds a good clone for a clone
1266 * instruction. Returns -ENOENT when none could be found. The function makes
1267 * sure that the returned clone is usable at the point where sending is at the
1268 * moment. This means, that no clones are accepted which lie behind the current
1271 * path must point to the extent item when called.
1273 static int find_extent_clone(struct send_ctx *sctx,
1274 struct btrfs_path *path,
1275 u64 ino, u64 data_offset,
1277 struct clone_root **found)
1284 u64 extent_item_pos;
1286 struct btrfs_file_extent_item *fi;
1287 struct extent_buffer *eb = path->nodes[0];
1288 struct backref_ctx *backref_ctx = NULL;
1289 struct clone_root *cur_clone_root;
1290 struct btrfs_key found_key;
1291 struct btrfs_path *tmp_path;
1295 tmp_path = alloc_path_for_send();
1299 /* We only use this path under the commit sem */
1300 tmp_path->need_commit_sem = 0;
1302 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1308 backref_ctx->path = tmp_path;
1310 if (data_offset >= ino_size) {
1312 * There may be extents that lie behind the file's size.
1313 * I at least had this in combination with snapshotting while
1314 * writing large files.
1320 fi = btrfs_item_ptr(eb, path->slots[0],
1321 struct btrfs_file_extent_item);
1322 extent_type = btrfs_file_extent_type(eb, fi);
1323 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1327 compressed = btrfs_file_extent_compression(eb, fi);
1329 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1330 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1331 if (disk_byte == 0) {
1335 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1337 down_read(&sctx->send_root->fs_info->commit_root_sem);
1338 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1339 &found_key, &flags);
1340 up_read(&sctx->send_root->fs_info->commit_root_sem);
1341 btrfs_release_path(tmp_path);
1345 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1351 * Setup the clone roots.
1353 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1354 cur_clone_root = sctx->clone_roots + i;
1355 cur_clone_root->ino = (u64)-1;
1356 cur_clone_root->offset = 0;
1357 cur_clone_root->found_refs = 0;
1360 backref_ctx->sctx = sctx;
1361 backref_ctx->found = 0;
1362 backref_ctx->cur_objectid = ino;
1363 backref_ctx->cur_offset = data_offset;
1364 backref_ctx->found_itself = 0;
1365 backref_ctx->extent_len = num_bytes;
1368 * The last extent of a file may be too large due to page alignment.
1369 * We need to adjust extent_len in this case so that the checks in
1370 * __iterate_backrefs work.
1372 if (data_offset + num_bytes >= ino_size)
1373 backref_ctx->extent_len = ino_size - data_offset;
1376 * Now collect all backrefs.
1378 if (compressed == BTRFS_COMPRESS_NONE)
1379 extent_item_pos = logical - found_key.objectid;
1381 extent_item_pos = 0;
1382 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1383 found_key.objectid, extent_item_pos, 1,
1384 __iterate_backrefs, backref_ctx);
1389 if (!backref_ctx->found_itself) {
1390 /* found a bug in backref code? */
1392 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
1393 "send_root. inode=%llu, offset=%llu, "
1394 "disk_byte=%llu found extent=%llu",
1395 ino, data_offset, disk_byte, found_key.objectid);
1399 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1401 "num_bytes=%llu, logical=%llu\n",
1402 data_offset, ino, num_bytes, logical);
1404 if (!backref_ctx->found)
1405 verbose_printk("btrfs: no clones found\n");
1407 cur_clone_root = NULL;
1408 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1409 if (sctx->clone_roots[i].found_refs) {
1410 if (!cur_clone_root)
1411 cur_clone_root = sctx->clone_roots + i;
1412 else if (sctx->clone_roots[i].root == sctx->send_root)
1413 /* prefer clones from send_root over others */
1414 cur_clone_root = sctx->clone_roots + i;
1419 if (cur_clone_root) {
1420 if (compressed != BTRFS_COMPRESS_NONE) {
1422 * Offsets given by iterate_extent_inodes() are relative
1423 * to the start of the extent, we need to add logical
1424 * offset from the file extent item.
1425 * (See why at backref.c:check_extent_in_eb())
1427 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1430 *found = cur_clone_root;
1437 btrfs_free_path(tmp_path);
1442 static int read_symlink(struct btrfs_root *root,
1444 struct fs_path *dest)
1447 struct btrfs_path *path;
1448 struct btrfs_key key;
1449 struct btrfs_file_extent_item *ei;
1455 path = alloc_path_for_send();
1460 key.type = BTRFS_EXTENT_DATA_KEY;
1462 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1467 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1468 struct btrfs_file_extent_item);
1469 type = btrfs_file_extent_type(path->nodes[0], ei);
1470 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1471 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1472 BUG_ON(compression);
1474 off = btrfs_file_extent_inline_start(ei);
1475 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
1477 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1480 btrfs_free_path(path);
1485 * Helper function to generate a file name that is unique in the root of
1486 * send_root and parent_root. This is used to generate names for orphan inodes.
1488 static int gen_unique_name(struct send_ctx *sctx,
1490 struct fs_path *dest)
1493 struct btrfs_path *path;
1494 struct btrfs_dir_item *di;
1499 path = alloc_path_for_send();
1504 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1506 ASSERT(len < sizeof(tmp));
1508 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1509 path, BTRFS_FIRST_FREE_OBJECTID,
1510 tmp, strlen(tmp), 0);
1511 btrfs_release_path(path);
1517 /* not unique, try again */
1522 if (!sctx->parent_root) {
1528 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1529 path, BTRFS_FIRST_FREE_OBJECTID,
1530 tmp, strlen(tmp), 0);
1531 btrfs_release_path(path);
1537 /* not unique, try again */
1545 ret = fs_path_add(dest, tmp, strlen(tmp));
1548 btrfs_free_path(path);
1553 inode_state_no_change,
1554 inode_state_will_create,
1555 inode_state_did_create,
1556 inode_state_will_delete,
1557 inode_state_did_delete,
1560 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1568 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1570 if (ret < 0 && ret != -ENOENT)
1574 if (!sctx->parent_root) {
1575 right_ret = -ENOENT;
1577 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1578 NULL, NULL, NULL, NULL);
1579 if (ret < 0 && ret != -ENOENT)
1584 if (!left_ret && !right_ret) {
1585 if (left_gen == gen && right_gen == gen) {
1586 ret = inode_state_no_change;
1587 } else if (left_gen == gen) {
1588 if (ino < sctx->send_progress)
1589 ret = inode_state_did_create;
1591 ret = inode_state_will_create;
1592 } else if (right_gen == gen) {
1593 if (ino < sctx->send_progress)
1594 ret = inode_state_did_delete;
1596 ret = inode_state_will_delete;
1600 } else if (!left_ret) {
1601 if (left_gen == gen) {
1602 if (ino < sctx->send_progress)
1603 ret = inode_state_did_create;
1605 ret = inode_state_will_create;
1609 } else if (!right_ret) {
1610 if (right_gen == gen) {
1611 if (ino < sctx->send_progress)
1612 ret = inode_state_did_delete;
1614 ret = inode_state_will_delete;
1626 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1630 ret = get_cur_inode_state(sctx, ino, gen);
1634 if (ret == inode_state_no_change ||
1635 ret == inode_state_did_create ||
1636 ret == inode_state_will_delete)
1646 * Helper function to lookup a dir item in a dir.
1648 static int lookup_dir_item_inode(struct btrfs_root *root,
1649 u64 dir, const char *name, int name_len,
1654 struct btrfs_dir_item *di;
1655 struct btrfs_key key;
1656 struct btrfs_path *path;
1658 path = alloc_path_for_send();
1662 di = btrfs_lookup_dir_item(NULL, root, path,
1663 dir, name, name_len, 0);
1672 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1673 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1677 *found_inode = key.objectid;
1678 *found_type = btrfs_dir_type(path->nodes[0], di);
1681 btrfs_free_path(path);
1686 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1687 * generation of the parent dir and the name of the dir entry.
1689 static int get_first_ref(struct btrfs_root *root, u64 ino,
1690 u64 *dir, u64 *dir_gen, struct fs_path *name)
1693 struct btrfs_key key;
1694 struct btrfs_key found_key;
1695 struct btrfs_path *path;
1699 path = alloc_path_for_send();
1704 key.type = BTRFS_INODE_REF_KEY;
1707 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1711 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1713 if (ret || found_key.objectid != ino ||
1714 (found_key.type != BTRFS_INODE_REF_KEY &&
1715 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1720 if (found_key.type == BTRFS_INODE_REF_KEY) {
1721 struct btrfs_inode_ref *iref;
1722 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1723 struct btrfs_inode_ref);
1724 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1725 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1726 (unsigned long)(iref + 1),
1728 parent_dir = found_key.offset;
1730 struct btrfs_inode_extref *extref;
1731 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1732 struct btrfs_inode_extref);
1733 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1734 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1735 (unsigned long)&extref->name, len);
1736 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1740 btrfs_release_path(path);
1743 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1752 btrfs_free_path(path);
1756 static int is_first_ref(struct btrfs_root *root,
1758 const char *name, int name_len)
1761 struct fs_path *tmp_name;
1764 tmp_name = fs_path_alloc();
1768 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1772 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1777 ret = !memcmp(tmp_name->start, name, name_len);
1780 fs_path_free(tmp_name);
1785 * Used by process_recorded_refs to determine if a new ref would overwrite an
1786 * already existing ref. In case it detects an overwrite, it returns the
1787 * inode/gen in who_ino/who_gen.
1788 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1789 * to make sure later references to the overwritten inode are possible.
1790 * Orphanizing is however only required for the first ref of an inode.
1791 * process_recorded_refs does an additional is_first_ref check to see if
1792 * orphanizing is really required.
1794 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1795 const char *name, int name_len,
1796 u64 *who_ino, u64 *who_gen)
1800 u64 other_inode = 0;
1803 if (!sctx->parent_root)
1806 ret = is_inode_existent(sctx, dir, dir_gen);
1811 * If we have a parent root we need to verify that the parent dir was
1812 * not delted and then re-created, if it was then we have no overwrite
1813 * and we can just unlink this entry.
1815 if (sctx->parent_root) {
1816 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1818 if (ret < 0 && ret != -ENOENT)
1828 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1829 &other_inode, &other_type);
1830 if (ret < 0 && ret != -ENOENT)
1838 * Check if the overwritten ref was already processed. If yes, the ref
1839 * was already unlinked/moved, so we can safely assume that we will not
1840 * overwrite anything at this point in time.
1842 if (other_inode > sctx->send_progress) {
1843 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1844 who_gen, NULL, NULL, NULL, NULL);
1849 *who_ino = other_inode;
1859 * Checks if the ref was overwritten by an already processed inode. This is
1860 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1861 * thus the orphan name needs be used.
1862 * process_recorded_refs also uses it to avoid unlinking of refs that were
1865 static int did_overwrite_ref(struct send_ctx *sctx,
1866 u64 dir, u64 dir_gen,
1867 u64 ino, u64 ino_gen,
1868 const char *name, int name_len)
1875 if (!sctx->parent_root)
1878 ret = is_inode_existent(sctx, dir, dir_gen);
1882 /* check if the ref was overwritten by another ref */
1883 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1884 &ow_inode, &other_type);
1885 if (ret < 0 && ret != -ENOENT)
1888 /* was never and will never be overwritten */
1893 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1898 if (ow_inode == ino && gen == ino_gen) {
1903 /* we know that it is or will be overwritten. check this now */
1904 if (ow_inode < sctx->send_progress)
1914 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1915 * that got overwritten. This is used by process_recorded_refs to determine
1916 * if it has to use the path as returned by get_cur_path or the orphan name.
1918 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1921 struct fs_path *name = NULL;
1925 if (!sctx->parent_root)
1928 name = fs_path_alloc();
1932 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1936 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1937 name->start, fs_path_len(name));
1945 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1946 * so we need to do some special handling in case we have clashes. This function
1947 * takes care of this with the help of name_cache_entry::radix_list.
1948 * In case of error, nce is kfreed.
1950 static int name_cache_insert(struct send_ctx *sctx,
1951 struct name_cache_entry *nce)
1954 struct list_head *nce_head;
1956 nce_head = radix_tree_lookup(&sctx->name_cache,
1957 (unsigned long)nce->ino);
1959 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1964 INIT_LIST_HEAD(nce_head);
1966 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1973 list_add_tail(&nce->radix_list, nce_head);
1974 list_add_tail(&nce->list, &sctx->name_cache_list);
1975 sctx->name_cache_size++;
1980 static void name_cache_delete(struct send_ctx *sctx,
1981 struct name_cache_entry *nce)
1983 struct list_head *nce_head;
1985 nce_head = radix_tree_lookup(&sctx->name_cache,
1986 (unsigned long)nce->ino);
1988 btrfs_err(sctx->send_root->fs_info,
1989 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1990 nce->ino, sctx->name_cache_size);
1993 list_del(&nce->radix_list);
1994 list_del(&nce->list);
1995 sctx->name_cache_size--;
1998 * We may not get to the final release of nce_head if the lookup fails
2000 if (nce_head && list_empty(nce_head)) {
2001 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2006 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2009 struct list_head *nce_head;
2010 struct name_cache_entry *cur;
2012 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2016 list_for_each_entry(cur, nce_head, radix_list) {
2017 if (cur->ino == ino && cur->gen == gen)
2024 * Removes the entry from the list and adds it back to the end. This marks the
2025 * entry as recently used so that name_cache_clean_unused does not remove it.
2027 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2029 list_del(&nce->list);
2030 list_add_tail(&nce->list, &sctx->name_cache_list);
2034 * Remove some entries from the beginning of name_cache_list.
2036 static void name_cache_clean_unused(struct send_ctx *sctx)
2038 struct name_cache_entry *nce;
2040 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2043 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2044 nce = list_entry(sctx->name_cache_list.next,
2045 struct name_cache_entry, list);
2046 name_cache_delete(sctx, nce);
2051 static void name_cache_free(struct send_ctx *sctx)
2053 struct name_cache_entry *nce;
2055 while (!list_empty(&sctx->name_cache_list)) {
2056 nce = list_entry(sctx->name_cache_list.next,
2057 struct name_cache_entry, list);
2058 name_cache_delete(sctx, nce);
2064 * Used by get_cur_path for each ref up to the root.
2065 * Returns 0 if it succeeded.
2066 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2067 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2068 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2069 * Returns <0 in case of error.
2071 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2075 struct fs_path *dest)
2079 struct name_cache_entry *nce = NULL;
2082 * First check if we already did a call to this function with the same
2083 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2084 * return the cached result.
2086 nce = name_cache_search(sctx, ino, gen);
2088 if (ino < sctx->send_progress && nce->need_later_update) {
2089 name_cache_delete(sctx, nce);
2093 name_cache_used(sctx, nce);
2094 *parent_ino = nce->parent_ino;
2095 *parent_gen = nce->parent_gen;
2096 ret = fs_path_add(dest, nce->name, nce->name_len);
2105 * If the inode is not existent yet, add the orphan name and return 1.
2106 * This should only happen for the parent dir that we determine in
2109 ret = is_inode_existent(sctx, ino, gen);
2114 ret = gen_unique_name(sctx, ino, gen, dest);
2122 * Depending on whether the inode was already processed or not, use
2123 * send_root or parent_root for ref lookup.
2125 if (ino < sctx->send_progress)
2126 ret = get_first_ref(sctx->send_root, ino,
2127 parent_ino, parent_gen, dest);
2129 ret = get_first_ref(sctx->parent_root, ino,
2130 parent_ino, parent_gen, dest);
2135 * Check if the ref was overwritten by an inode's ref that was processed
2136 * earlier. If yes, treat as orphan and return 1.
2138 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2139 dest->start, dest->end - dest->start);
2143 fs_path_reset(dest);
2144 ret = gen_unique_name(sctx, ino, gen, dest);
2152 * Store the result of the lookup in the name cache.
2154 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2162 nce->parent_ino = *parent_ino;
2163 nce->parent_gen = *parent_gen;
2164 nce->name_len = fs_path_len(dest);
2166 strcpy(nce->name, dest->start);
2168 if (ino < sctx->send_progress)
2169 nce->need_later_update = 0;
2171 nce->need_later_update = 1;
2173 nce_ret = name_cache_insert(sctx, nce);
2176 name_cache_clean_unused(sctx);
2183 * Magic happens here. This function returns the first ref to an inode as it
2184 * would look like while receiving the stream at this point in time.
2185 * We walk the path up to the root. For every inode in between, we check if it
2186 * was already processed/sent. If yes, we continue with the parent as found
2187 * in send_root. If not, we continue with the parent as found in parent_root.
2188 * If we encounter an inode that was deleted at this point in time, we use the
2189 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2190 * that were not created yet and overwritten inodes/refs.
2192 * When do we have have orphan inodes:
2193 * 1. When an inode is freshly created and thus no valid refs are available yet
2194 * 2. When a directory lost all it's refs (deleted) but still has dir items
2195 * inside which were not processed yet (pending for move/delete). If anyone
2196 * tried to get the path to the dir items, it would get a path inside that
2198 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2199 * of an unprocessed inode. If in that case the first ref would be
2200 * overwritten, the overwritten inode gets "orphanized". Later when we
2201 * process this overwritten inode, it is restored at a new place by moving
2204 * sctx->send_progress tells this function at which point in time receiving
2207 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2208 struct fs_path *dest)
2211 struct fs_path *name = NULL;
2212 u64 parent_inode = 0;
2216 name = fs_path_alloc();
2223 fs_path_reset(dest);
2225 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2226 fs_path_reset(name);
2228 if (is_waiting_for_rm(sctx, ino)) {
2229 ret = gen_unique_name(sctx, ino, gen, name);
2232 ret = fs_path_add_path(dest, name);
2236 if (is_waiting_for_move(sctx, ino)) {
2237 ret = get_first_ref(sctx->parent_root, ino,
2238 &parent_inode, &parent_gen, name);
2240 ret = __get_cur_name_and_parent(sctx, ino, gen,
2250 ret = fs_path_add_path(dest, name);
2261 fs_path_unreverse(dest);
2266 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2268 static int send_subvol_begin(struct send_ctx *sctx)
2271 struct btrfs_root *send_root = sctx->send_root;
2272 struct btrfs_root *parent_root = sctx->parent_root;
2273 struct btrfs_path *path;
2274 struct btrfs_key key;
2275 struct btrfs_root_ref *ref;
2276 struct extent_buffer *leaf;
2280 path = btrfs_alloc_path();
2284 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2286 btrfs_free_path(path);
2290 key.objectid = send_root->objectid;
2291 key.type = BTRFS_ROOT_BACKREF_KEY;
2294 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2303 leaf = path->nodes[0];
2304 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2305 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2306 key.objectid != send_root->objectid) {
2310 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2311 namelen = btrfs_root_ref_name_len(leaf, ref);
2312 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2313 btrfs_release_path(path);
2316 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2320 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2325 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2326 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2327 sctx->send_root->root_item.uuid);
2328 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2329 le64_to_cpu(sctx->send_root->root_item.ctransid));
2331 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2332 sctx->parent_root->root_item.uuid);
2333 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2334 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2337 ret = send_cmd(sctx);
2341 btrfs_free_path(path);
2346 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2351 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2353 p = fs_path_alloc();
2357 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2361 ret = get_cur_path(sctx, ino, gen, p);
2364 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2365 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2367 ret = send_cmd(sctx);
2375 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2380 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2382 p = fs_path_alloc();
2386 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2390 ret = get_cur_path(sctx, ino, gen, p);
2393 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2394 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2396 ret = send_cmd(sctx);
2404 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2409 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2411 p = fs_path_alloc();
2415 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2419 ret = get_cur_path(sctx, ino, gen, p);
2422 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2423 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2424 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2426 ret = send_cmd(sctx);
2434 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2437 struct fs_path *p = NULL;
2438 struct btrfs_inode_item *ii;
2439 struct btrfs_path *path = NULL;
2440 struct extent_buffer *eb;
2441 struct btrfs_key key;
2444 verbose_printk("btrfs: send_utimes %llu\n", ino);
2446 p = fs_path_alloc();
2450 path = alloc_path_for_send();
2457 key.type = BTRFS_INODE_ITEM_KEY;
2459 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2463 eb = path->nodes[0];
2464 slot = path->slots[0];
2465 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2467 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2471 ret = get_cur_path(sctx, ino, gen, p);
2474 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2475 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2476 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2477 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2478 /* TODO Add otime support when the otime patches get into upstream */
2480 ret = send_cmd(sctx);
2485 btrfs_free_path(path);
2490 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2491 * a valid path yet because we did not process the refs yet. So, the inode
2492 * is created as orphan.
2494 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2503 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2505 p = fs_path_alloc();
2509 if (ino != sctx->cur_ino) {
2510 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2515 gen = sctx->cur_inode_gen;
2516 mode = sctx->cur_inode_mode;
2517 rdev = sctx->cur_inode_rdev;
2520 if (S_ISREG(mode)) {
2521 cmd = BTRFS_SEND_C_MKFILE;
2522 } else if (S_ISDIR(mode)) {
2523 cmd = BTRFS_SEND_C_MKDIR;
2524 } else if (S_ISLNK(mode)) {
2525 cmd = BTRFS_SEND_C_SYMLINK;
2526 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2527 cmd = BTRFS_SEND_C_MKNOD;
2528 } else if (S_ISFIFO(mode)) {
2529 cmd = BTRFS_SEND_C_MKFIFO;
2530 } else if (S_ISSOCK(mode)) {
2531 cmd = BTRFS_SEND_C_MKSOCK;
2533 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2534 (int)(mode & S_IFMT));
2539 ret = begin_cmd(sctx, cmd);
2543 ret = gen_unique_name(sctx, ino, gen, p);
2547 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2548 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2550 if (S_ISLNK(mode)) {
2552 ret = read_symlink(sctx->send_root, ino, p);
2555 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2556 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2557 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2558 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2559 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2562 ret = send_cmd(sctx);
2574 * We need some special handling for inodes that get processed before the parent
2575 * directory got created. See process_recorded_refs for details.
2576 * This function does the check if we already created the dir out of order.
2578 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2581 struct btrfs_path *path = NULL;
2582 struct btrfs_key key;
2583 struct btrfs_key found_key;
2584 struct btrfs_key di_key;
2585 struct extent_buffer *eb;
2586 struct btrfs_dir_item *di;
2589 path = alloc_path_for_send();
2596 key.type = BTRFS_DIR_INDEX_KEY;
2598 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2603 eb = path->nodes[0];
2604 slot = path->slots[0];
2605 if (slot >= btrfs_header_nritems(eb)) {
2606 ret = btrfs_next_leaf(sctx->send_root, path);
2609 } else if (ret > 0) {
2616 btrfs_item_key_to_cpu(eb, &found_key, slot);
2617 if (found_key.objectid != key.objectid ||
2618 found_key.type != key.type) {
2623 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2624 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2626 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2627 di_key.objectid < sctx->send_progress) {
2636 btrfs_free_path(path);
2641 * Only creates the inode if it is:
2642 * 1. Not a directory
2643 * 2. Or a directory which was not created already due to out of order
2644 * directories. See did_create_dir and process_recorded_refs for details.
2646 static int send_create_inode_if_needed(struct send_ctx *sctx)
2650 if (S_ISDIR(sctx->cur_inode_mode)) {
2651 ret = did_create_dir(sctx, sctx->cur_ino);
2660 ret = send_create_inode(sctx, sctx->cur_ino);
2668 struct recorded_ref {
2669 struct list_head list;
2672 struct fs_path *full_path;
2680 * We need to process new refs before deleted refs, but compare_tree gives us
2681 * everything mixed. So we first record all refs and later process them.
2682 * This function is a helper to record one ref.
2684 static int __record_ref(struct list_head *head, u64 dir,
2685 u64 dir_gen, struct fs_path *path)
2687 struct recorded_ref *ref;
2689 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2694 ref->dir_gen = dir_gen;
2695 ref->full_path = path;
2697 ref->name = (char *)kbasename(ref->full_path->start);
2698 ref->name_len = ref->full_path->end - ref->name;
2699 ref->dir_path = ref->full_path->start;
2700 if (ref->name == ref->full_path->start)
2701 ref->dir_path_len = 0;
2703 ref->dir_path_len = ref->full_path->end -
2704 ref->full_path->start - 1 - ref->name_len;
2706 list_add_tail(&ref->list, head);
2710 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2712 struct recorded_ref *new;
2714 new = kmalloc(sizeof(*ref), GFP_NOFS);
2718 new->dir = ref->dir;
2719 new->dir_gen = ref->dir_gen;
2720 new->full_path = NULL;
2721 INIT_LIST_HEAD(&new->list);
2722 list_add_tail(&new->list, list);
2726 static void __free_recorded_refs(struct list_head *head)
2728 struct recorded_ref *cur;
2730 while (!list_empty(head)) {
2731 cur = list_entry(head->next, struct recorded_ref, list);
2732 fs_path_free(cur->full_path);
2733 list_del(&cur->list);
2738 static void free_recorded_refs(struct send_ctx *sctx)
2740 __free_recorded_refs(&sctx->new_refs);
2741 __free_recorded_refs(&sctx->deleted_refs);
2745 * Renames/moves a file/dir to its orphan name. Used when the first
2746 * ref of an unprocessed inode gets overwritten and for all non empty
2749 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2750 struct fs_path *path)
2753 struct fs_path *orphan;
2755 orphan = fs_path_alloc();
2759 ret = gen_unique_name(sctx, ino, gen, orphan);
2763 ret = send_rename(sctx, path, orphan);
2766 fs_path_free(orphan);
2770 static struct orphan_dir_info *
2771 add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2773 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2774 struct rb_node *parent = NULL;
2775 struct orphan_dir_info *entry, *odi;
2777 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2779 return ERR_PTR(-ENOMEM);
2785 entry = rb_entry(parent, struct orphan_dir_info, node);
2786 if (dir_ino < entry->ino) {
2788 } else if (dir_ino > entry->ino) {
2789 p = &(*p)->rb_right;
2796 rb_link_node(&odi->node, parent, p);
2797 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2801 static struct orphan_dir_info *
2802 get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2804 struct rb_node *n = sctx->orphan_dirs.rb_node;
2805 struct orphan_dir_info *entry;
2808 entry = rb_entry(n, struct orphan_dir_info, node);
2809 if (dir_ino < entry->ino)
2811 else if (dir_ino > entry->ino)
2819 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2821 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2826 static void free_orphan_dir_info(struct send_ctx *sctx,
2827 struct orphan_dir_info *odi)
2831 rb_erase(&odi->node, &sctx->orphan_dirs);
2836 * Returns 1 if a directory can be removed at this point in time.
2837 * We check this by iterating all dir items and checking if the inode behind
2838 * the dir item was already processed.
2840 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2844 struct btrfs_root *root = sctx->parent_root;
2845 struct btrfs_path *path;
2846 struct btrfs_key key;
2847 struct btrfs_key found_key;
2848 struct btrfs_key loc;
2849 struct btrfs_dir_item *di;
2852 * Don't try to rmdir the top/root subvolume dir.
2854 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2857 path = alloc_path_for_send();
2862 key.type = BTRFS_DIR_INDEX_KEY;
2864 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2869 struct waiting_dir_move *dm;
2871 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2872 ret = btrfs_next_leaf(root, path);
2879 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2881 if (found_key.objectid != key.objectid ||
2882 found_key.type != key.type)
2885 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2886 struct btrfs_dir_item);
2887 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2889 dm = get_waiting_dir_move(sctx, loc.objectid);
2891 struct orphan_dir_info *odi;
2893 odi = add_orphan_dir_info(sctx, dir);
2899 dm->rmdir_ino = dir;
2904 if (loc.objectid > send_progress) {
2915 btrfs_free_path(path);
2919 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2921 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
2923 return entry != NULL;
2926 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2928 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2929 struct rb_node *parent = NULL;
2930 struct waiting_dir_move *entry, *dm;
2932 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2940 entry = rb_entry(parent, struct waiting_dir_move, node);
2941 if (ino < entry->ino) {
2943 } else if (ino > entry->ino) {
2944 p = &(*p)->rb_right;
2951 rb_link_node(&dm->node, parent, p);
2952 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2956 static struct waiting_dir_move *
2957 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2959 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2960 struct waiting_dir_move *entry;
2963 entry = rb_entry(n, struct waiting_dir_move, node);
2964 if (ino < entry->ino)
2966 else if (ino > entry->ino)
2974 static void free_waiting_dir_move(struct send_ctx *sctx,
2975 struct waiting_dir_move *dm)
2979 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2983 static int add_pending_dir_move(struct send_ctx *sctx,
2987 struct list_head *new_refs,
2988 struct list_head *deleted_refs,
2989 const bool is_orphan)
2991 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2992 struct rb_node *parent = NULL;
2993 struct pending_dir_move *entry = NULL, *pm;
2994 struct recorded_ref *cur;
2998 pm = kmalloc(sizeof(*pm), GFP_NOFS);
3001 pm->parent_ino = parent_ino;
3004 pm->is_orphan = is_orphan;
3005 INIT_LIST_HEAD(&pm->list);
3006 INIT_LIST_HEAD(&pm->update_refs);
3007 RB_CLEAR_NODE(&pm->node);
3011 entry = rb_entry(parent, struct pending_dir_move, node);
3012 if (parent_ino < entry->parent_ino) {
3014 } else if (parent_ino > entry->parent_ino) {
3015 p = &(*p)->rb_right;
3022 list_for_each_entry(cur, deleted_refs, list) {
3023 ret = dup_ref(cur, &pm->update_refs);
3027 list_for_each_entry(cur, new_refs, list) {
3028 ret = dup_ref(cur, &pm->update_refs);
3033 ret = add_waiting_dir_move(sctx, pm->ino);
3038 list_add_tail(&pm->list, &entry->list);
3040 rb_link_node(&pm->node, parent, p);
3041 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3046 __free_recorded_refs(&pm->update_refs);
3052 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3055 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3056 struct pending_dir_move *entry;
3059 entry = rb_entry(n, struct pending_dir_move, node);
3060 if (parent_ino < entry->parent_ino)
3062 else if (parent_ino > entry->parent_ino)
3070 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3071 u64 ino, u64 gen, u64 *ancestor_ino)
3074 u64 parent_inode = 0;
3076 u64 start_ino = ino;
3079 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3080 fs_path_reset(name);
3082 if (is_waiting_for_rm(sctx, ino))
3084 if (is_waiting_for_move(sctx, ino)) {
3085 if (*ancestor_ino == 0)
3086 *ancestor_ino = ino;
3087 ret = get_first_ref(sctx->parent_root, ino,
3088 &parent_inode, &parent_gen, name);
3090 ret = __get_cur_name_and_parent(sctx, ino, gen,
3100 if (parent_inode == start_ino) {
3102 if (*ancestor_ino == 0)
3103 *ancestor_ino = ino;
3112 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3114 struct fs_path *from_path = NULL;
3115 struct fs_path *to_path = NULL;
3116 struct fs_path *name = NULL;
3117 u64 orig_progress = sctx->send_progress;
3118 struct recorded_ref *cur;
3119 u64 parent_ino, parent_gen;
3120 struct waiting_dir_move *dm = NULL;
3125 name = fs_path_alloc();
3126 from_path = fs_path_alloc();
3127 if (!name || !from_path) {
3132 dm = get_waiting_dir_move(sctx, pm->ino);
3134 rmdir_ino = dm->rmdir_ino;
3135 free_waiting_dir_move(sctx, dm);
3137 if (pm->is_orphan) {
3138 ret = gen_unique_name(sctx, pm->ino,
3139 pm->gen, from_path);
3141 ret = get_first_ref(sctx->parent_root, pm->ino,
3142 &parent_ino, &parent_gen, name);
3145 ret = get_cur_path(sctx, parent_ino, parent_gen,
3149 ret = fs_path_add_path(from_path, name);
3154 sctx->send_progress = sctx->cur_ino + 1;
3155 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3157 LIST_HEAD(deleted_refs);
3158 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3159 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3160 &pm->update_refs, &deleted_refs,
3165 dm = get_waiting_dir_move(sctx, pm->ino);
3167 dm->rmdir_ino = rmdir_ino;
3171 fs_path_reset(name);
3174 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3178 ret = send_rename(sctx, from_path, to_path);
3183 struct orphan_dir_info *odi;
3185 odi = get_orphan_dir_info(sctx, rmdir_ino);
3187 /* already deleted */
3190 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3196 name = fs_path_alloc();
3201 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3204 ret = send_rmdir(sctx, name);
3207 free_orphan_dir_info(sctx, odi);
3211 ret = send_utimes(sctx, pm->ino, pm->gen);
3216 * After rename/move, need to update the utimes of both new parent(s)
3217 * and old parent(s).
3219 list_for_each_entry(cur, &pm->update_refs, list) {
3220 if (cur->dir == rmdir_ino)
3222 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3229 fs_path_free(from_path);
3230 fs_path_free(to_path);
3231 sctx->send_progress = orig_progress;
3236 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3238 if (!list_empty(&m->list))
3240 if (!RB_EMPTY_NODE(&m->node))
3241 rb_erase(&m->node, &sctx->pending_dir_moves);
3242 __free_recorded_refs(&m->update_refs);
3246 static void tail_append_pending_moves(struct pending_dir_move *moves,
3247 struct list_head *stack)
3249 if (list_empty(&moves->list)) {
3250 list_add_tail(&moves->list, stack);
3253 list_splice_init(&moves->list, &list);
3254 list_add_tail(&moves->list, stack);
3255 list_splice_tail(&list, stack);
3259 static int apply_children_dir_moves(struct send_ctx *sctx)
3261 struct pending_dir_move *pm;
3262 struct list_head stack;
3263 u64 parent_ino = sctx->cur_ino;
3266 pm = get_pending_dir_moves(sctx, parent_ino);
3270 INIT_LIST_HEAD(&stack);
3271 tail_append_pending_moves(pm, &stack);
3273 while (!list_empty(&stack)) {
3274 pm = list_first_entry(&stack, struct pending_dir_move, list);
3275 parent_ino = pm->ino;
3276 ret = apply_dir_move(sctx, pm);
3277 free_pending_move(sctx, pm);
3280 pm = get_pending_dir_moves(sctx, parent_ino);
3282 tail_append_pending_moves(pm, &stack);
3287 while (!list_empty(&stack)) {
3288 pm = list_first_entry(&stack, struct pending_dir_move, list);
3289 free_pending_move(sctx, pm);
3295 * We might need to delay a directory rename even when no ancestor directory
3296 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3297 * renamed. This happens when we rename a directory to the old name (the name
3298 * in the parent root) of some other unrelated directory that got its rename
3299 * delayed due to some ancestor with higher number that got renamed.
3305 * |---- a/ (ino 257)
3306 * | |---- file (ino 260)
3308 * |---- b/ (ino 258)
3309 * |---- c/ (ino 259)
3313 * |---- a/ (ino 258)
3314 * |---- x/ (ino 259)
3315 * |---- y/ (ino 257)
3316 * |----- file (ino 260)
3318 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3319 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3320 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3323 * 1 - rename 259 from 'c' to 'x'
3324 * 2 - rename 257 from 'a' to 'x/y'
3325 * 3 - rename 258 from 'b' to 'a'
3327 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3328 * be done right away and < 0 on error.
3330 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3331 struct recorded_ref *parent_ref,
3332 const bool is_orphan)
3334 struct btrfs_path *path;
3335 struct btrfs_key key;
3336 struct btrfs_key di_key;
3337 struct btrfs_dir_item *di;
3342 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3345 path = alloc_path_for_send();
3349 key.objectid = parent_ref->dir;
3350 key.type = BTRFS_DIR_ITEM_KEY;
3351 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3353 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3356 } else if (ret > 0) {
3361 di = btrfs_match_dir_item_name(sctx->parent_root, path,
3362 parent_ref->name, parent_ref->name_len);
3368 * di_key.objectid has the number of the inode that has a dentry in the
3369 * parent directory with the same name that sctx->cur_ino is being
3370 * renamed to. We need to check if that inode is in the send root as
3371 * well and if it is currently marked as an inode with a pending rename,
3372 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3373 * that it happens after that other inode is renamed.
3375 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3376 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3381 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3382 &left_gen, NULL, NULL, NULL, NULL);
3385 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3386 &right_gen, NULL, NULL, NULL, NULL);
3393 /* Different inode, no need to delay the rename of sctx->cur_ino */
3394 if (right_gen != left_gen) {
3399 if (is_waiting_for_move(sctx, di_key.objectid)) {
3400 ret = add_pending_dir_move(sctx,
3402 sctx->cur_inode_gen,
3405 &sctx->deleted_refs,
3411 btrfs_free_path(path);
3415 static int wait_for_parent_move(struct send_ctx *sctx,
3416 struct recorded_ref *parent_ref)
3419 u64 ino = parent_ref->dir;
3420 u64 parent_ino_before, parent_ino_after;
3421 struct fs_path *path_before = NULL;
3422 struct fs_path *path_after = NULL;
3425 path_after = fs_path_alloc();
3426 path_before = fs_path_alloc();
3427 if (!path_after || !path_before) {
3433 * Our current directory inode may not yet be renamed/moved because some
3434 * ancestor (immediate or not) has to be renamed/moved first. So find if
3435 * such ancestor exists and make sure our own rename/move happens after
3436 * that ancestor is processed.
3438 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3439 if (is_waiting_for_move(sctx, ino)) {
3444 fs_path_reset(path_before);
3445 fs_path_reset(path_after);
3447 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3451 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3453 if (ret < 0 && ret != -ENOENT) {
3455 } else if (ret == -ENOENT) {
3460 len1 = fs_path_len(path_before);
3461 len2 = fs_path_len(path_after);
3462 if (ino > sctx->cur_ino &&
3463 (parent_ino_before != parent_ino_after || len1 != len2 ||
3464 memcmp(path_before->start, path_after->start, len1))) {
3468 ino = parent_ino_after;
3472 fs_path_free(path_before);
3473 fs_path_free(path_after);
3476 ret = add_pending_dir_move(sctx,
3478 sctx->cur_inode_gen,
3481 &sctx->deleted_refs,
3491 * This does all the move/link/unlink/rmdir magic.
3493 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3496 struct recorded_ref *cur;
3497 struct recorded_ref *cur2;
3498 struct list_head check_dirs;
3499 struct fs_path *valid_path = NULL;
3502 int did_overwrite = 0;
3504 u64 last_dir_ino_rm = 0;
3505 bool can_rename = true;
3507 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3510 * This should never happen as the root dir always has the same ref
3511 * which is always '..'
3513 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3514 INIT_LIST_HEAD(&check_dirs);
3516 valid_path = fs_path_alloc();
3523 * First, check if the first ref of the current inode was overwritten
3524 * before. If yes, we know that the current inode was already orphanized
3525 * and thus use the orphan name. If not, we can use get_cur_path to
3526 * get the path of the first ref as it would like while receiving at
3527 * this point in time.
3528 * New inodes are always orphan at the beginning, so force to use the
3529 * orphan name in this case.
3530 * The first ref is stored in valid_path and will be updated if it
3531 * gets moved around.
3533 if (!sctx->cur_inode_new) {
3534 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3535 sctx->cur_inode_gen);
3541 if (sctx->cur_inode_new || did_overwrite) {
3542 ret = gen_unique_name(sctx, sctx->cur_ino,
3543 sctx->cur_inode_gen, valid_path);
3548 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3554 list_for_each_entry(cur, &sctx->new_refs, list) {
3556 * We may have refs where the parent directory does not exist
3557 * yet. This happens if the parent directories inum is higher
3558 * the the current inum. To handle this case, we create the
3559 * parent directory out of order. But we need to check if this
3560 * did already happen before due to other refs in the same dir.
3562 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3565 if (ret == inode_state_will_create) {
3568 * First check if any of the current inodes refs did
3569 * already create the dir.
3571 list_for_each_entry(cur2, &sctx->new_refs, list) {
3574 if (cur2->dir == cur->dir) {
3581 * If that did not happen, check if a previous inode
3582 * did already create the dir.
3585 ret = did_create_dir(sctx, cur->dir);
3589 ret = send_create_inode(sctx, cur->dir);
3596 * Check if this new ref would overwrite the first ref of
3597 * another unprocessed inode. If yes, orphanize the
3598 * overwritten inode. If we find an overwritten ref that is
3599 * not the first ref, simply unlink it.
3601 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3602 cur->name, cur->name_len,
3603 &ow_inode, &ow_gen);
3607 ret = is_first_ref(sctx->parent_root,
3608 ow_inode, cur->dir, cur->name,
3613 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3618 ret = send_unlink(sctx, cur->full_path);
3624 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3625 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3635 * link/move the ref to the new place. If we have an orphan
3636 * inode, move it and update valid_path. If not, link or move
3637 * it depending on the inode mode.
3639 if (is_orphan && can_rename) {
3640 ret = send_rename(sctx, valid_path, cur->full_path);
3644 ret = fs_path_copy(valid_path, cur->full_path);
3647 } else if (can_rename) {
3648 if (S_ISDIR(sctx->cur_inode_mode)) {
3650 * Dirs can't be linked, so move it. For moved
3651 * dirs, we always have one new and one deleted
3652 * ref. The deleted ref is ignored later.
3654 ret = wait_for_parent_move(sctx, cur);
3660 ret = send_rename(sctx, valid_path,
3663 ret = fs_path_copy(valid_path,
3669 ret = send_link(sctx, cur->full_path,
3675 ret = dup_ref(cur, &check_dirs);
3680 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3682 * Check if we can already rmdir the directory. If not,
3683 * orphanize it. For every dir item inside that gets deleted
3684 * later, we do this check again and rmdir it then if possible.
3685 * See the use of check_dirs for more details.
3687 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3692 ret = send_rmdir(sctx, valid_path);
3695 } else if (!is_orphan) {
3696 ret = orphanize_inode(sctx, sctx->cur_ino,
3697 sctx->cur_inode_gen, valid_path);
3703 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3704 ret = dup_ref(cur, &check_dirs);
3708 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3709 !list_empty(&sctx->deleted_refs)) {
3711 * We have a moved dir. Add the old parent to check_dirs
3713 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3715 ret = dup_ref(cur, &check_dirs);
3718 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3720 * We have a non dir inode. Go through all deleted refs and
3721 * unlink them if they were not already overwritten by other
3724 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3725 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3726 sctx->cur_ino, sctx->cur_inode_gen,
3727 cur->name, cur->name_len);
3731 ret = send_unlink(sctx, cur->full_path);
3735 ret = dup_ref(cur, &check_dirs);
3740 * If the inode is still orphan, unlink the orphan. This may
3741 * happen when a previous inode did overwrite the first ref
3742 * of this inode and no new refs were added for the current
3743 * inode. Unlinking does not mean that the inode is deleted in
3744 * all cases. There may still be links to this inode in other
3748 ret = send_unlink(sctx, valid_path);
3755 * We did collect all parent dirs where cur_inode was once located. We
3756 * now go through all these dirs and check if they are pending for
3757 * deletion and if it's finally possible to perform the rmdir now.
3758 * We also update the inode stats of the parent dirs here.
3760 list_for_each_entry(cur, &check_dirs, list) {
3762 * In case we had refs into dirs that were not processed yet,
3763 * we don't need to do the utime and rmdir logic for these dirs.
3764 * The dir will be processed later.
3766 if (cur->dir > sctx->cur_ino)
3769 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3773 if (ret == inode_state_did_create ||
3774 ret == inode_state_no_change) {
3775 /* TODO delayed utimes */
3776 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3779 } else if (ret == inode_state_did_delete &&
3780 cur->dir != last_dir_ino_rm) {
3781 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3786 ret = get_cur_path(sctx, cur->dir,
3787 cur->dir_gen, valid_path);
3790 ret = send_rmdir(sctx, valid_path);
3793 last_dir_ino_rm = cur->dir;
3801 __free_recorded_refs(&check_dirs);
3802 free_recorded_refs(sctx);
3803 fs_path_free(valid_path);
3807 static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3808 struct fs_path *name, void *ctx, struct list_head *refs)
3811 struct send_ctx *sctx = ctx;
3815 p = fs_path_alloc();
3819 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
3824 ret = get_cur_path(sctx, dir, gen, p);
3827 ret = fs_path_add_path(p, name);
3831 ret = __record_ref(refs, dir, gen, p);
3839 static int __record_new_ref(int num, u64 dir, int index,
3840 struct fs_path *name,
3843 struct send_ctx *sctx = ctx;
3844 return record_ref(sctx->send_root, num, dir, index, name,
3845 ctx, &sctx->new_refs);
3849 static int __record_deleted_ref(int num, u64 dir, int index,
3850 struct fs_path *name,
3853 struct send_ctx *sctx = ctx;
3854 return record_ref(sctx->parent_root, num, dir, index, name,
3855 ctx, &sctx->deleted_refs);
3858 static int record_new_ref(struct send_ctx *sctx)
3862 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3863 sctx->cmp_key, 0, __record_new_ref, sctx);
3872 static int record_deleted_ref(struct send_ctx *sctx)
3876 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3877 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3886 struct find_ref_ctx {
3889 struct btrfs_root *root;
3890 struct fs_path *name;
3894 static int __find_iref(int num, u64 dir, int index,
3895 struct fs_path *name,
3898 struct find_ref_ctx *ctx = ctx_;
3902 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3903 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3905 * To avoid doing extra lookups we'll only do this if everything
3908 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3912 if (dir_gen != ctx->dir_gen)
3914 ctx->found_idx = num;
3920 static int find_iref(struct btrfs_root *root,
3921 struct btrfs_path *path,
3922 struct btrfs_key *key,
3923 u64 dir, u64 dir_gen, struct fs_path *name)
3926 struct find_ref_ctx ctx;
3930 ctx.dir_gen = dir_gen;
3934 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3938 if (ctx.found_idx == -1)
3941 return ctx.found_idx;
3944 static int __record_changed_new_ref(int num, u64 dir, int index,
3945 struct fs_path *name,
3950 struct send_ctx *sctx = ctx;
3952 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3957 ret = find_iref(sctx->parent_root, sctx->right_path,
3958 sctx->cmp_key, dir, dir_gen, name);
3960 ret = __record_new_ref(num, dir, index, name, sctx);
3967 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3968 struct fs_path *name,
3973 struct send_ctx *sctx = ctx;
3975 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3980 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3981 dir, dir_gen, name);
3983 ret = __record_deleted_ref(num, dir, index, name, sctx);
3990 static int record_changed_ref(struct send_ctx *sctx)
3994 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3995 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3998 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3999 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4009 * Record and process all refs at once. Needed when an inode changes the
4010 * generation number, which means that it was deleted and recreated.
4012 static int process_all_refs(struct send_ctx *sctx,
4013 enum btrfs_compare_tree_result cmd)
4016 struct btrfs_root *root;
4017 struct btrfs_path *path;
4018 struct btrfs_key key;
4019 struct btrfs_key found_key;
4020 struct extent_buffer *eb;
4022 iterate_inode_ref_t cb;
4023 int pending_move = 0;
4025 path = alloc_path_for_send();
4029 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4030 root = sctx->send_root;
4031 cb = __record_new_ref;
4032 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4033 root = sctx->parent_root;
4034 cb = __record_deleted_ref;
4036 btrfs_err(sctx->send_root->fs_info,
4037 "Wrong command %d in process_all_refs", cmd);
4042 key.objectid = sctx->cmp_key->objectid;
4043 key.type = BTRFS_INODE_REF_KEY;
4045 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4050 eb = path->nodes[0];
4051 slot = path->slots[0];
4052 if (slot >= btrfs_header_nritems(eb)) {
4053 ret = btrfs_next_leaf(root, path);
4061 btrfs_item_key_to_cpu(eb, &found_key, slot);
4063 if (found_key.objectid != key.objectid ||
4064 (found_key.type != BTRFS_INODE_REF_KEY &&
4065 found_key.type != BTRFS_INODE_EXTREF_KEY))
4068 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4074 btrfs_release_path(path);
4076 ret = process_recorded_refs(sctx, &pending_move);
4077 /* Only applicable to an incremental send. */
4078 ASSERT(pending_move == 0);
4081 btrfs_free_path(path);
4085 static int send_set_xattr(struct send_ctx *sctx,
4086 struct fs_path *path,
4087 const char *name, int name_len,
4088 const char *data, int data_len)
4092 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4096 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4097 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4098 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4100 ret = send_cmd(sctx);
4107 static int send_remove_xattr(struct send_ctx *sctx,
4108 struct fs_path *path,
4109 const char *name, int name_len)
4113 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4117 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4118 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4120 ret = send_cmd(sctx);
4127 static int __process_new_xattr(int num, struct btrfs_key *di_key,
4128 const char *name, int name_len,
4129 const char *data, int data_len,
4133 struct send_ctx *sctx = ctx;
4135 posix_acl_xattr_header dummy_acl;
4137 p = fs_path_alloc();
4142 * This hack is needed because empty acl's are stored as zero byte
4143 * data in xattrs. Problem with that is, that receiving these zero byte
4144 * acl's will fail later. To fix this, we send a dummy acl list that
4145 * only contains the version number and no entries.
4147 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4148 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4149 if (data_len == 0) {
4150 dummy_acl.a_version =
4151 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4152 data = (char *)&dummy_acl;
4153 data_len = sizeof(dummy_acl);
4157 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4161 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4168 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4169 const char *name, int name_len,
4170 const char *data, int data_len,
4174 struct send_ctx *sctx = ctx;
4177 p = fs_path_alloc();
4181 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4185 ret = send_remove_xattr(sctx, p, name, name_len);
4192 static int process_new_xattr(struct send_ctx *sctx)
4196 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4197 sctx->cmp_key, __process_new_xattr, sctx);
4202 static int process_deleted_xattr(struct send_ctx *sctx)
4206 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4207 sctx->cmp_key, __process_deleted_xattr, sctx);
4212 struct find_xattr_ctx {
4220 static int __find_xattr(int num, struct btrfs_key *di_key,
4221 const char *name, int name_len,
4222 const char *data, int data_len,
4223 u8 type, void *vctx)
4225 struct find_xattr_ctx *ctx = vctx;
4227 if (name_len == ctx->name_len &&
4228 strncmp(name, ctx->name, name_len) == 0) {
4229 ctx->found_idx = num;
4230 ctx->found_data_len = data_len;
4231 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
4232 if (!ctx->found_data)
4239 static int find_xattr(struct btrfs_root *root,
4240 struct btrfs_path *path,
4241 struct btrfs_key *key,
4242 const char *name, int name_len,
4243 char **data, int *data_len)
4246 struct find_xattr_ctx ctx;
4249 ctx.name_len = name_len;
4251 ctx.found_data = NULL;
4252 ctx.found_data_len = 0;
4254 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
4258 if (ctx.found_idx == -1)
4261 *data = ctx.found_data;
4262 *data_len = ctx.found_data_len;
4264 kfree(ctx.found_data);
4266 return ctx.found_idx;
4270 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4271 const char *name, int name_len,
4272 const char *data, int data_len,
4276 struct send_ctx *sctx = ctx;
4277 char *found_data = NULL;
4278 int found_data_len = 0;
4280 ret = find_xattr(sctx->parent_root, sctx->right_path,
4281 sctx->cmp_key, name, name_len, &found_data,
4283 if (ret == -ENOENT) {
4284 ret = __process_new_xattr(num, di_key, name, name_len, data,
4285 data_len, type, ctx);
4286 } else if (ret >= 0) {
4287 if (data_len != found_data_len ||
4288 memcmp(data, found_data, data_len)) {
4289 ret = __process_new_xattr(num, di_key, name, name_len,
4290 data, data_len, type, ctx);
4300 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4301 const char *name, int name_len,
4302 const char *data, int data_len,
4306 struct send_ctx *sctx = ctx;
4308 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4309 name, name_len, NULL, NULL);
4311 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4312 data_len, type, ctx);
4319 static int process_changed_xattr(struct send_ctx *sctx)
4323 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4324 sctx->cmp_key, __process_changed_new_xattr, sctx);
4327 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4328 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4334 static int process_all_new_xattrs(struct send_ctx *sctx)
4337 struct btrfs_root *root;
4338 struct btrfs_path *path;
4339 struct btrfs_key key;
4340 struct btrfs_key found_key;
4341 struct extent_buffer *eb;
4344 path = alloc_path_for_send();
4348 root = sctx->send_root;
4350 key.objectid = sctx->cmp_key->objectid;
4351 key.type = BTRFS_XATTR_ITEM_KEY;
4353 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4358 eb = path->nodes[0];
4359 slot = path->slots[0];
4360 if (slot >= btrfs_header_nritems(eb)) {
4361 ret = btrfs_next_leaf(root, path);
4364 } else if (ret > 0) {
4371 btrfs_item_key_to_cpu(eb, &found_key, slot);
4372 if (found_key.objectid != key.objectid ||
4373 found_key.type != key.type) {
4378 ret = iterate_dir_item(root, path, &found_key,
4379 __process_new_xattr, sctx);
4387 btrfs_free_path(path);
4391 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4393 struct btrfs_root *root = sctx->send_root;
4394 struct btrfs_fs_info *fs_info = root->fs_info;
4395 struct inode *inode;
4398 struct btrfs_key key;
4399 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4401 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4404 key.objectid = sctx->cur_ino;
4405 key.type = BTRFS_INODE_ITEM_KEY;
4408 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4410 return PTR_ERR(inode);
4412 if (offset + len > i_size_read(inode)) {
4413 if (offset > i_size_read(inode))
4416 len = offset - i_size_read(inode);
4421 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
4423 /* initial readahead */
4424 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4425 file_ra_state_init(&sctx->ra, inode->i_mapping);
4426 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4427 last_index - index + 1);
4429 while (index <= last_index) {
4430 unsigned cur_len = min_t(unsigned, len,
4431 PAGE_CACHE_SIZE - pg_offset);
4432 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4438 if (!PageUptodate(page)) {
4439 btrfs_readpage(NULL, page);
4441 if (!PageUptodate(page)) {
4443 page_cache_release(page);
4450 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4453 page_cache_release(page);
4465 * Read some bytes from the current inode/file and send a write command to
4468 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4472 ssize_t num_read = 0;
4474 p = fs_path_alloc();
4478 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4480 num_read = fill_read_buf(sctx, offset, len);
4481 if (num_read <= 0) {
4487 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4491 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4495 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4496 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4497 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
4499 ret = send_cmd(sctx);
4510 * Send a clone command to user space.
4512 static int send_clone(struct send_ctx *sctx,
4513 u64 offset, u32 len,
4514 struct clone_root *clone_root)
4520 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4521 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4522 clone_root->root->objectid, clone_root->ino,
4523 clone_root->offset);
4525 p = fs_path_alloc();
4529 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4533 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4537 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4538 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4539 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4541 if (clone_root->root == sctx->send_root) {
4542 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
4543 &gen, NULL, NULL, NULL, NULL);
4546 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4548 ret = get_inode_path(clone_root->root, clone_root->ino, p);
4553 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4554 clone_root->root->root_item.uuid);
4555 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
4556 le64_to_cpu(clone_root->root->root_item.ctransid));
4557 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4558 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4559 clone_root->offset);
4561 ret = send_cmd(sctx);
4570 * Send an update extent command to user space.
4572 static int send_update_extent(struct send_ctx *sctx,
4573 u64 offset, u32 len)
4578 p = fs_path_alloc();
4582 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4586 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4590 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4591 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4592 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4594 ret = send_cmd(sctx);
4602 static int send_hole(struct send_ctx *sctx, u64 end)
4604 struct fs_path *p = NULL;
4605 u64 offset = sctx->cur_inode_last_extent;
4609 p = fs_path_alloc();
4612 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4614 goto tlv_put_failure;
4615 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4616 while (offset < end) {
4617 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4619 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4622 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4623 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4624 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4625 ret = send_cmd(sctx);
4635 static int send_write_or_clone(struct send_ctx *sctx,
4636 struct btrfs_path *path,
4637 struct btrfs_key *key,
4638 struct clone_root *clone_root)
4641 struct btrfs_file_extent_item *ei;
4642 u64 offset = key->offset;
4647 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
4649 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4650 struct btrfs_file_extent_item);
4651 type = btrfs_file_extent_type(path->nodes[0], ei);
4652 if (type == BTRFS_FILE_EXTENT_INLINE) {
4653 len = btrfs_file_extent_inline_len(path->nodes[0],
4654 path->slots[0], ei);
4656 * it is possible the inline item won't cover the whole page,
4657 * but there may be items after this page. Make
4658 * sure to send the whole thing
4660 len = PAGE_CACHE_ALIGN(len);
4662 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
4665 if (offset + len > sctx->cur_inode_size)
4666 len = sctx->cur_inode_size - offset;
4672 if (clone_root && IS_ALIGNED(offset + len, bs)) {
4673 ret = send_clone(sctx, offset, len, clone_root);
4674 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4675 ret = send_update_extent(sctx, offset, len);
4679 if (l > BTRFS_SEND_READ_SIZE)
4680 l = BTRFS_SEND_READ_SIZE;
4681 ret = send_write(sctx, pos + offset, l);
4694 static int is_extent_unchanged(struct send_ctx *sctx,
4695 struct btrfs_path *left_path,
4696 struct btrfs_key *ekey)
4699 struct btrfs_key key;
4700 struct btrfs_path *path = NULL;
4701 struct extent_buffer *eb;
4703 struct btrfs_key found_key;
4704 struct btrfs_file_extent_item *ei;
4709 u64 left_offset_fixed;
4717 path = alloc_path_for_send();
4721 eb = left_path->nodes[0];
4722 slot = left_path->slots[0];
4723 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4724 left_type = btrfs_file_extent_type(eb, ei);
4726 if (left_type != BTRFS_FILE_EXTENT_REG) {
4730 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4731 left_len = btrfs_file_extent_num_bytes(eb, ei);
4732 left_offset = btrfs_file_extent_offset(eb, ei);
4733 left_gen = btrfs_file_extent_generation(eb, ei);
4736 * Following comments will refer to these graphics. L is the left
4737 * extents which we are checking at the moment. 1-8 are the right
4738 * extents that we iterate.
4741 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4744 * |--1--|-2b-|...(same as above)
4746 * Alternative situation. Happens on files where extents got split.
4748 * |-----------7-----------|-6-|
4750 * Alternative situation. Happens on files which got larger.
4753 * Nothing follows after 8.
4756 key.objectid = ekey->objectid;
4757 key.type = BTRFS_EXTENT_DATA_KEY;
4758 key.offset = ekey->offset;
4759 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4768 * Handle special case where the right side has no extents at all.
4770 eb = path->nodes[0];
4771 slot = path->slots[0];
4772 btrfs_item_key_to_cpu(eb, &found_key, slot);
4773 if (found_key.objectid != key.objectid ||
4774 found_key.type != key.type) {
4775 /* If we're a hole then just pretend nothing changed */
4776 ret = (left_disknr) ? 0 : 1;
4781 * We're now on 2a, 2b or 7.
4784 while (key.offset < ekey->offset + left_len) {
4785 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4786 right_type = btrfs_file_extent_type(eb, ei);
4787 if (right_type != BTRFS_FILE_EXTENT_REG) {
4792 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4793 right_len = btrfs_file_extent_num_bytes(eb, ei);
4794 right_offset = btrfs_file_extent_offset(eb, ei);
4795 right_gen = btrfs_file_extent_generation(eb, ei);
4798 * Are we at extent 8? If yes, we know the extent is changed.
4799 * This may only happen on the first iteration.
4801 if (found_key.offset + right_len <= ekey->offset) {
4802 /* If we're a hole just pretend nothing changed */
4803 ret = (left_disknr) ? 0 : 1;
4807 left_offset_fixed = left_offset;
4808 if (key.offset < ekey->offset) {
4809 /* Fix the right offset for 2a and 7. */
4810 right_offset += ekey->offset - key.offset;
4812 /* Fix the left offset for all behind 2a and 2b */
4813 left_offset_fixed += key.offset - ekey->offset;
4817 * Check if we have the same extent.
4819 if (left_disknr != right_disknr ||
4820 left_offset_fixed != right_offset ||
4821 left_gen != right_gen) {
4827 * Go to the next extent.
4829 ret = btrfs_next_item(sctx->parent_root, path);
4833 eb = path->nodes[0];
4834 slot = path->slots[0];
4835 btrfs_item_key_to_cpu(eb, &found_key, slot);
4837 if (ret || found_key.objectid != key.objectid ||
4838 found_key.type != key.type) {
4839 key.offset += right_len;
4842 if (found_key.offset != key.offset + right_len) {
4850 * We're now behind the left extent (treat as unchanged) or at the end
4851 * of the right side (treat as changed).
4853 if (key.offset >= ekey->offset + left_len)
4860 btrfs_free_path(path);
4864 static int get_last_extent(struct send_ctx *sctx, u64 offset)
4866 struct btrfs_path *path;
4867 struct btrfs_root *root = sctx->send_root;
4868 struct btrfs_file_extent_item *fi;
4869 struct btrfs_key key;
4874 path = alloc_path_for_send();
4878 sctx->cur_inode_last_extent = 0;
4880 key.objectid = sctx->cur_ino;
4881 key.type = BTRFS_EXTENT_DATA_KEY;
4882 key.offset = offset;
4883 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4887 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4888 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4891 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4892 struct btrfs_file_extent_item);
4893 type = btrfs_file_extent_type(path->nodes[0], fi);
4894 if (type == BTRFS_FILE_EXTENT_INLINE) {
4895 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4896 path->slots[0], fi);
4897 extent_end = ALIGN(key.offset + size,
4898 sctx->send_root->sectorsize);
4900 extent_end = key.offset +
4901 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4903 sctx->cur_inode_last_extent = extent_end;
4905 btrfs_free_path(path);
4909 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4910 struct btrfs_key *key)
4912 struct btrfs_file_extent_item *fi;
4917 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4920 if (sctx->cur_inode_last_extent == (u64)-1) {
4921 ret = get_last_extent(sctx, key->offset - 1);
4926 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4927 struct btrfs_file_extent_item);
4928 type = btrfs_file_extent_type(path->nodes[0], fi);
4929 if (type == BTRFS_FILE_EXTENT_INLINE) {
4930 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4931 path->slots[0], fi);
4932 extent_end = ALIGN(key->offset + size,
4933 sctx->send_root->sectorsize);
4935 extent_end = key->offset +
4936 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4939 if (path->slots[0] == 0 &&
4940 sctx->cur_inode_last_extent < key->offset) {
4942 * We might have skipped entire leafs that contained only
4943 * file extent items for our current inode. These leafs have
4944 * a generation number smaller (older) than the one in the
4945 * current leaf and the leaf our last extent came from, and
4946 * are located between these 2 leafs.
4948 ret = get_last_extent(sctx, key->offset - 1);
4953 if (sctx->cur_inode_last_extent < key->offset)
4954 ret = send_hole(sctx, key->offset);
4955 sctx->cur_inode_last_extent = extent_end;
4959 static int process_extent(struct send_ctx *sctx,
4960 struct btrfs_path *path,
4961 struct btrfs_key *key)
4963 struct clone_root *found_clone = NULL;
4966 if (S_ISLNK(sctx->cur_inode_mode))
4969 if (sctx->parent_root && !sctx->cur_inode_new) {
4970 ret = is_extent_unchanged(sctx, path, key);
4978 struct btrfs_file_extent_item *ei;
4981 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4982 struct btrfs_file_extent_item);
4983 type = btrfs_file_extent_type(path->nodes[0], ei);
4984 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4985 type == BTRFS_FILE_EXTENT_REG) {
4987 * The send spec does not have a prealloc command yet,
4988 * so just leave a hole for prealloc'ed extents until
4989 * we have enough commands queued up to justify rev'ing
4992 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4997 /* Have a hole, just skip it. */
4998 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5005 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5006 sctx->cur_inode_size, &found_clone);
5007 if (ret != -ENOENT && ret < 0)
5010 ret = send_write_or_clone(sctx, path, key, found_clone);
5014 ret = maybe_send_hole(sctx, path, key);
5019 static int process_all_extents(struct send_ctx *sctx)
5022 struct btrfs_root *root;
5023 struct btrfs_path *path;
5024 struct btrfs_key key;
5025 struct btrfs_key found_key;
5026 struct extent_buffer *eb;
5029 root = sctx->send_root;
5030 path = alloc_path_for_send();
5034 key.objectid = sctx->cmp_key->objectid;
5035 key.type = BTRFS_EXTENT_DATA_KEY;
5037 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5042 eb = path->nodes[0];
5043 slot = path->slots[0];
5045 if (slot >= btrfs_header_nritems(eb)) {
5046 ret = btrfs_next_leaf(root, path);
5049 } else if (ret > 0) {
5056 btrfs_item_key_to_cpu(eb, &found_key, slot);
5058 if (found_key.objectid != key.objectid ||
5059 found_key.type != key.type) {
5064 ret = process_extent(sctx, path, &found_key);
5072 btrfs_free_path(path);
5076 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5078 int *refs_processed)
5082 if (sctx->cur_ino == 0)
5084 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
5085 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
5087 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5090 ret = process_recorded_refs(sctx, pending_move);
5094 *refs_processed = 1;
5099 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5110 int pending_move = 0;
5111 int refs_processed = 0;
5113 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5119 * We have processed the refs and thus need to advance send_progress.
5120 * Now, calls to get_cur_xxx will take the updated refs of the current
5121 * inode into account.
5123 * On the other hand, if our current inode is a directory and couldn't
5124 * be moved/renamed because its parent was renamed/moved too and it has
5125 * a higher inode number, we can only move/rename our current inode
5126 * after we moved/renamed its parent. Therefore in this case operate on
5127 * the old path (pre move/rename) of our current inode, and the
5128 * move/rename will be performed later.
5130 if (refs_processed && !pending_move)
5131 sctx->send_progress = sctx->cur_ino + 1;
5133 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5135 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5138 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
5139 &left_mode, &left_uid, &left_gid, NULL);
5143 if (!sctx->parent_root || sctx->cur_inode_new) {
5145 if (!S_ISLNK(sctx->cur_inode_mode))
5148 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5149 NULL, NULL, &right_mode, &right_uid,
5154 if (left_uid != right_uid || left_gid != right_gid)
5156 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5160 if (S_ISREG(sctx->cur_inode_mode)) {
5161 if (need_send_hole(sctx)) {
5162 if (sctx->cur_inode_last_extent == (u64)-1 ||
5163 sctx->cur_inode_last_extent <
5164 sctx->cur_inode_size) {
5165 ret = get_last_extent(sctx, (u64)-1);
5169 if (sctx->cur_inode_last_extent <
5170 sctx->cur_inode_size) {
5171 ret = send_hole(sctx, sctx->cur_inode_size);
5176 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5177 sctx->cur_inode_size);
5183 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5184 left_uid, left_gid);
5189 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5196 * If other directory inodes depended on our current directory
5197 * inode's move/rename, now do their move/rename operations.
5199 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5200 ret = apply_children_dir_moves(sctx);
5204 * Need to send that every time, no matter if it actually
5205 * changed between the two trees as we have done changes to
5206 * the inode before. If our inode is a directory and it's
5207 * waiting to be moved/renamed, we will send its utimes when
5208 * it's moved/renamed, therefore we don't need to do it here.
5210 sctx->send_progress = sctx->cur_ino + 1;
5211 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5220 static int changed_inode(struct send_ctx *sctx,
5221 enum btrfs_compare_tree_result result)
5224 struct btrfs_key *key = sctx->cmp_key;
5225 struct btrfs_inode_item *left_ii = NULL;
5226 struct btrfs_inode_item *right_ii = NULL;
5230 sctx->cur_ino = key->objectid;
5231 sctx->cur_inode_new_gen = 0;
5232 sctx->cur_inode_last_extent = (u64)-1;
5235 * Set send_progress to current inode. This will tell all get_cur_xxx
5236 * functions that the current inode's refs are not updated yet. Later,
5237 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5239 sctx->send_progress = sctx->cur_ino;
5241 if (result == BTRFS_COMPARE_TREE_NEW ||
5242 result == BTRFS_COMPARE_TREE_CHANGED) {
5243 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5244 sctx->left_path->slots[0],
5245 struct btrfs_inode_item);
5246 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5249 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5250 sctx->right_path->slots[0],
5251 struct btrfs_inode_item);
5252 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5255 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5256 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5257 sctx->right_path->slots[0],
5258 struct btrfs_inode_item);
5260 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5264 * The cur_ino = root dir case is special here. We can't treat
5265 * the inode as deleted+reused because it would generate a
5266 * stream that tries to delete/mkdir the root dir.
5268 if (left_gen != right_gen &&
5269 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5270 sctx->cur_inode_new_gen = 1;
5273 if (result == BTRFS_COMPARE_TREE_NEW) {
5274 sctx->cur_inode_gen = left_gen;
5275 sctx->cur_inode_new = 1;
5276 sctx->cur_inode_deleted = 0;
5277 sctx->cur_inode_size = btrfs_inode_size(
5278 sctx->left_path->nodes[0], left_ii);
5279 sctx->cur_inode_mode = btrfs_inode_mode(
5280 sctx->left_path->nodes[0], left_ii);
5281 sctx->cur_inode_rdev = btrfs_inode_rdev(
5282 sctx->left_path->nodes[0], left_ii);
5283 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5284 ret = send_create_inode_if_needed(sctx);
5285 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5286 sctx->cur_inode_gen = right_gen;
5287 sctx->cur_inode_new = 0;
5288 sctx->cur_inode_deleted = 1;
5289 sctx->cur_inode_size = btrfs_inode_size(
5290 sctx->right_path->nodes[0], right_ii);
5291 sctx->cur_inode_mode = btrfs_inode_mode(
5292 sctx->right_path->nodes[0], right_ii);
5293 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
5295 * We need to do some special handling in case the inode was
5296 * reported as changed with a changed generation number. This
5297 * means that the original inode was deleted and new inode
5298 * reused the same inum. So we have to treat the old inode as
5299 * deleted and the new one as new.
5301 if (sctx->cur_inode_new_gen) {
5303 * First, process the inode as if it was deleted.
5305 sctx->cur_inode_gen = right_gen;
5306 sctx->cur_inode_new = 0;
5307 sctx->cur_inode_deleted = 1;
5308 sctx->cur_inode_size = btrfs_inode_size(
5309 sctx->right_path->nodes[0], right_ii);
5310 sctx->cur_inode_mode = btrfs_inode_mode(
5311 sctx->right_path->nodes[0], right_ii);
5312 ret = process_all_refs(sctx,
5313 BTRFS_COMPARE_TREE_DELETED);
5318 * Now process the inode as if it was new.
5320 sctx->cur_inode_gen = left_gen;
5321 sctx->cur_inode_new = 1;
5322 sctx->cur_inode_deleted = 0;
5323 sctx->cur_inode_size = btrfs_inode_size(
5324 sctx->left_path->nodes[0], left_ii);
5325 sctx->cur_inode_mode = btrfs_inode_mode(
5326 sctx->left_path->nodes[0], left_ii);
5327 sctx->cur_inode_rdev = btrfs_inode_rdev(
5328 sctx->left_path->nodes[0], left_ii);
5329 ret = send_create_inode_if_needed(sctx);
5333 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5337 * Advance send_progress now as we did not get into
5338 * process_recorded_refs_if_needed in the new_gen case.
5340 sctx->send_progress = sctx->cur_ino + 1;
5343 * Now process all extents and xattrs of the inode as if
5344 * they were all new.
5346 ret = process_all_extents(sctx);
5349 ret = process_all_new_xattrs(sctx);
5353 sctx->cur_inode_gen = left_gen;
5354 sctx->cur_inode_new = 0;
5355 sctx->cur_inode_new_gen = 0;
5356 sctx->cur_inode_deleted = 0;
5357 sctx->cur_inode_size = btrfs_inode_size(
5358 sctx->left_path->nodes[0], left_ii);
5359 sctx->cur_inode_mode = btrfs_inode_mode(
5360 sctx->left_path->nodes[0], left_ii);
5369 * We have to process new refs before deleted refs, but compare_trees gives us
5370 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5371 * first and later process them in process_recorded_refs.
5372 * For the cur_inode_new_gen case, we skip recording completely because
5373 * changed_inode did already initiate processing of refs. The reason for this is
5374 * that in this case, compare_tree actually compares the refs of 2 different
5375 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5376 * refs of the right tree as deleted and all refs of the left tree as new.
5378 static int changed_ref(struct send_ctx *sctx,
5379 enum btrfs_compare_tree_result result)
5383 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5385 if (!sctx->cur_inode_new_gen &&
5386 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5387 if (result == BTRFS_COMPARE_TREE_NEW)
5388 ret = record_new_ref(sctx);
5389 else if (result == BTRFS_COMPARE_TREE_DELETED)
5390 ret = record_deleted_ref(sctx);
5391 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5392 ret = record_changed_ref(sctx);
5399 * Process new/deleted/changed xattrs. We skip processing in the
5400 * cur_inode_new_gen case because changed_inode did already initiate processing
5401 * of xattrs. The reason is the same as in changed_ref
5403 static int changed_xattr(struct send_ctx *sctx,
5404 enum btrfs_compare_tree_result result)
5408 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5410 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5411 if (result == BTRFS_COMPARE_TREE_NEW)
5412 ret = process_new_xattr(sctx);
5413 else if (result == BTRFS_COMPARE_TREE_DELETED)
5414 ret = process_deleted_xattr(sctx);
5415 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5416 ret = process_changed_xattr(sctx);
5423 * Process new/deleted/changed extents. We skip processing in the
5424 * cur_inode_new_gen case because changed_inode did already initiate processing
5425 * of extents. The reason is the same as in changed_ref
5427 static int changed_extent(struct send_ctx *sctx,
5428 enum btrfs_compare_tree_result result)
5432 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5434 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5435 if (result != BTRFS_COMPARE_TREE_DELETED)
5436 ret = process_extent(sctx, sctx->left_path,
5443 static int dir_changed(struct send_ctx *sctx, u64 dir)
5445 u64 orig_gen, new_gen;
5448 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5453 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5458 return (orig_gen != new_gen) ? 1 : 0;
5461 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5462 struct btrfs_key *key)
5464 struct btrfs_inode_extref *extref;
5465 struct extent_buffer *leaf;
5466 u64 dirid = 0, last_dirid = 0;
5473 /* Easy case, just check this one dirid */
5474 if (key->type == BTRFS_INODE_REF_KEY) {
5475 dirid = key->offset;
5477 ret = dir_changed(sctx, dirid);
5481 leaf = path->nodes[0];
5482 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5483 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5484 while (cur_offset < item_size) {
5485 extref = (struct btrfs_inode_extref *)(ptr +
5487 dirid = btrfs_inode_extref_parent(leaf, extref);
5488 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5489 cur_offset += ref_name_len + sizeof(*extref);
5490 if (dirid == last_dirid)
5492 ret = dir_changed(sctx, dirid);
5502 * Updates compare related fields in sctx and simply forwards to the actual
5503 * changed_xxx functions.
5505 static int changed_cb(struct btrfs_root *left_root,
5506 struct btrfs_root *right_root,
5507 struct btrfs_path *left_path,
5508 struct btrfs_path *right_path,
5509 struct btrfs_key *key,
5510 enum btrfs_compare_tree_result result,
5514 struct send_ctx *sctx = ctx;
5516 if (result == BTRFS_COMPARE_TREE_SAME) {
5517 if (key->type == BTRFS_INODE_REF_KEY ||
5518 key->type == BTRFS_INODE_EXTREF_KEY) {
5519 ret = compare_refs(sctx, left_path, key);
5524 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5525 return maybe_send_hole(sctx, left_path, key);
5529 result = BTRFS_COMPARE_TREE_CHANGED;
5533 sctx->left_path = left_path;
5534 sctx->right_path = right_path;
5535 sctx->cmp_key = key;
5537 ret = finish_inode_if_needed(sctx, 0);
5541 /* Ignore non-FS objects */
5542 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5543 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5546 if (key->type == BTRFS_INODE_ITEM_KEY)
5547 ret = changed_inode(sctx, result);
5548 else if (key->type == BTRFS_INODE_REF_KEY ||
5549 key->type == BTRFS_INODE_EXTREF_KEY)
5550 ret = changed_ref(sctx, result);
5551 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5552 ret = changed_xattr(sctx, result);
5553 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5554 ret = changed_extent(sctx, result);
5560 static int full_send_tree(struct send_ctx *sctx)
5563 struct btrfs_root *send_root = sctx->send_root;
5564 struct btrfs_key key;
5565 struct btrfs_key found_key;
5566 struct btrfs_path *path;
5567 struct extent_buffer *eb;
5570 path = alloc_path_for_send();
5574 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5575 key.type = BTRFS_INODE_ITEM_KEY;
5578 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5585 eb = path->nodes[0];
5586 slot = path->slots[0];
5587 btrfs_item_key_to_cpu(eb, &found_key, slot);
5589 ret = changed_cb(send_root, NULL, path, NULL,
5590 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5594 key.objectid = found_key.objectid;
5595 key.type = found_key.type;
5596 key.offset = found_key.offset + 1;
5598 ret = btrfs_next_item(send_root, path);
5608 ret = finish_inode_if_needed(sctx, 1);
5611 btrfs_free_path(path);
5615 static int send_subvol(struct send_ctx *sctx)
5619 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5620 ret = send_header(sctx);
5625 ret = send_subvol_begin(sctx);
5629 if (sctx->parent_root) {
5630 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5634 ret = finish_inode_if_needed(sctx, 1);
5638 ret = full_send_tree(sctx);
5644 free_recorded_refs(sctx);
5649 * If orphan cleanup did remove any orphans from a root, it means the tree
5650 * was modified and therefore the commit root is not the same as the current
5651 * root anymore. This is a problem, because send uses the commit root and
5652 * therefore can see inode items that don't exist in the current root anymore,
5653 * and for example make calls to btrfs_iget, which will do tree lookups based
5654 * on the current root and not on the commit root. Those lookups will fail,
5655 * returning a -ESTALE error, and making send fail with that error. So make
5656 * sure a send does not see any orphans we have just removed, and that it will
5657 * see the same inodes regardless of whether a transaction commit happened
5658 * before it started (meaning that the commit root will be the same as the
5659 * current root) or not.
5661 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
5664 struct btrfs_trans_handle *trans = NULL;
5667 if (sctx->parent_root &&
5668 sctx->parent_root->node != sctx->parent_root->commit_root)
5671 for (i = 0; i < sctx->clone_roots_cnt; i++)
5672 if (sctx->clone_roots[i].root->node !=
5673 sctx->clone_roots[i].root->commit_root)
5677 return btrfs_end_transaction(trans, sctx->send_root);
5682 /* Use any root, all fs roots will get their commit roots updated. */
5684 trans = btrfs_join_transaction(sctx->send_root);
5686 return PTR_ERR(trans);
5690 return btrfs_commit_transaction(trans, sctx->send_root);
5693 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5695 spin_lock(&root->root_item_lock);
5696 root->send_in_progress--;
5698 * Not much left to do, we don't know why it's unbalanced and
5699 * can't blindly reset it to 0.
5701 if (root->send_in_progress < 0)
5702 btrfs_err(root->fs_info,
5703 "send_in_progres unbalanced %d root %llu",
5704 root->send_in_progress, root->root_key.objectid);
5705 spin_unlock(&root->root_item_lock);
5708 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5711 struct btrfs_root *send_root;
5712 struct btrfs_root *clone_root;
5713 struct btrfs_fs_info *fs_info;
5714 struct btrfs_ioctl_send_args *arg = NULL;
5715 struct btrfs_key key;
5716 struct send_ctx *sctx = NULL;
5718 u64 *clone_sources_tmp = NULL;
5719 int clone_sources_to_rollback = 0;
5720 int sort_clone_roots = 0;
5723 if (!capable(CAP_SYS_ADMIN))
5726 send_root = BTRFS_I(file_inode(mnt_file))->root;
5727 fs_info = send_root->fs_info;
5730 * The subvolume must remain read-only during send, protect against
5731 * making it RW. This also protects against deletion.
5733 spin_lock(&send_root->root_item_lock);
5734 send_root->send_in_progress++;
5735 spin_unlock(&send_root->root_item_lock);
5738 * This is done when we lookup the root, it should already be complete
5739 * by the time we get here.
5741 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5744 * Userspace tools do the checks and warn the user if it's
5747 if (!btrfs_root_readonly(send_root)) {
5752 arg = memdup_user(arg_, sizeof(*arg));
5759 if (!access_ok(VERIFY_READ, arg->clone_sources,
5760 sizeof(*arg->clone_sources) *
5761 arg->clone_sources_count)) {
5766 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
5771 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5777 INIT_LIST_HEAD(&sctx->new_refs);
5778 INIT_LIST_HEAD(&sctx->deleted_refs);
5779 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5780 INIT_LIST_HEAD(&sctx->name_cache_list);
5782 sctx->flags = arg->flags;
5784 sctx->send_filp = fget(arg->send_fd);
5785 if (!sctx->send_filp) {
5790 sctx->send_root = send_root;
5792 * Unlikely but possible, if the subvolume is marked for deletion but
5793 * is slow to remove the directory entry, send can still be started
5795 if (btrfs_root_dead(sctx->send_root)) {
5800 sctx->clone_roots_cnt = arg->clone_sources_count;
5802 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5803 sctx->send_buf = vmalloc(sctx->send_max_size);
5804 if (!sctx->send_buf) {
5809 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5810 if (!sctx->read_buf) {
5815 sctx->pending_dir_moves = RB_ROOT;
5816 sctx->waiting_dir_moves = RB_ROOT;
5817 sctx->orphan_dirs = RB_ROOT;
5819 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5820 (arg->clone_sources_count + 1));
5821 if (!sctx->clone_roots) {
5826 if (arg->clone_sources_count) {
5827 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5828 sizeof(*arg->clone_sources));
5829 if (!clone_sources_tmp) {
5834 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5835 arg->clone_sources_count *
5836 sizeof(*arg->clone_sources));
5842 for (i = 0; i < arg->clone_sources_count; i++) {
5843 key.objectid = clone_sources_tmp[i];
5844 key.type = BTRFS_ROOT_ITEM_KEY;
5845 key.offset = (u64)-1;
5847 index = srcu_read_lock(&fs_info->subvol_srcu);
5849 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
5850 if (IS_ERR(clone_root)) {
5851 srcu_read_unlock(&fs_info->subvol_srcu, index);
5852 ret = PTR_ERR(clone_root);
5855 clone_sources_to_rollback = i + 1;
5856 spin_lock(&clone_root->root_item_lock);
5857 clone_root->send_in_progress++;
5858 if (!btrfs_root_readonly(clone_root)) {
5859 spin_unlock(&clone_root->root_item_lock);
5860 srcu_read_unlock(&fs_info->subvol_srcu, index);
5864 spin_unlock(&clone_root->root_item_lock);
5865 srcu_read_unlock(&fs_info->subvol_srcu, index);
5867 sctx->clone_roots[i].root = clone_root;
5869 vfree(clone_sources_tmp);
5870 clone_sources_tmp = NULL;
5873 if (arg->parent_root) {
5874 key.objectid = arg->parent_root;
5875 key.type = BTRFS_ROOT_ITEM_KEY;
5876 key.offset = (u64)-1;
5878 index = srcu_read_lock(&fs_info->subvol_srcu);
5880 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
5881 if (IS_ERR(sctx->parent_root)) {
5882 srcu_read_unlock(&fs_info->subvol_srcu, index);
5883 ret = PTR_ERR(sctx->parent_root);
5887 spin_lock(&sctx->parent_root->root_item_lock);
5888 sctx->parent_root->send_in_progress++;
5889 if (!btrfs_root_readonly(sctx->parent_root) ||
5890 btrfs_root_dead(sctx->parent_root)) {
5891 spin_unlock(&sctx->parent_root->root_item_lock);
5892 srcu_read_unlock(&fs_info->subvol_srcu, index);
5896 spin_unlock(&sctx->parent_root->root_item_lock);
5898 srcu_read_unlock(&fs_info->subvol_srcu, index);
5902 * Clones from send_root are allowed, but only if the clone source
5903 * is behind the current send position. This is checked while searching
5904 * for possible clone sources.
5906 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5908 /* We do a bsearch later */
5909 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5910 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5912 sort_clone_roots = 1;
5914 ret = ensure_commit_roots_uptodate(sctx);
5918 current->journal_info = BTRFS_SEND_TRANS_STUB;
5919 ret = send_subvol(sctx);
5920 current->journal_info = NULL;
5924 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5925 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5928 ret = send_cmd(sctx);
5934 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5935 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5937 struct pending_dir_move *pm;
5939 n = rb_first(&sctx->pending_dir_moves);
5940 pm = rb_entry(n, struct pending_dir_move, node);
5941 while (!list_empty(&pm->list)) {
5942 struct pending_dir_move *pm2;
5944 pm2 = list_first_entry(&pm->list,
5945 struct pending_dir_move, list);
5946 free_pending_move(sctx, pm2);
5948 free_pending_move(sctx, pm);
5951 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5952 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5954 struct waiting_dir_move *dm;
5956 n = rb_first(&sctx->waiting_dir_moves);
5957 dm = rb_entry(n, struct waiting_dir_move, node);
5958 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5962 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5963 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5965 struct orphan_dir_info *odi;
5967 n = rb_first(&sctx->orphan_dirs);
5968 odi = rb_entry(n, struct orphan_dir_info, node);
5969 free_orphan_dir_info(sctx, odi);
5972 if (sort_clone_roots) {
5973 for (i = 0; i < sctx->clone_roots_cnt; i++)
5974 btrfs_root_dec_send_in_progress(
5975 sctx->clone_roots[i].root);
5977 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5978 btrfs_root_dec_send_in_progress(
5979 sctx->clone_roots[i].root);
5981 btrfs_root_dec_send_in_progress(send_root);
5983 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5984 btrfs_root_dec_send_in_progress(sctx->parent_root);
5987 vfree(clone_sources_tmp);
5990 if (sctx->send_filp)
5991 fput(sctx->send_filp);
5993 vfree(sctx->clone_roots);
5994 vfree(sctx->send_buf);
5995 vfree(sctx->read_buf);
5997 name_cache_free(sctx);