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;
115 u64 cur_inode_last_extent;
119 struct list_head new_refs;
120 struct list_head deleted_refs;
122 struct radix_tree_root name_cache;
123 struct list_head name_cache_list;
129 * We process inodes by their increasing order, so if before an
130 * incremental send we reverse the parent/child relationship of
131 * directories such that a directory with a lower inode number was
132 * the parent of a directory with a higher inode number, and the one
133 * becoming the new parent got renamed too, we can't rename/move the
134 * directory with lower inode number when we finish processing it - we
135 * must process the directory with higher inode number first, then
136 * rename/move it and then rename/move the directory with lower inode
137 * number. Example follows.
139 * Tree state when the first send was performed:
151 * Tree state when the second (incremental) send is performed:
160 * The sequence of steps that lead to the second state was:
162 * mv /a/b/c/d /a/b/c2/d2
163 * mv /a/b/c /a/b/c2/d2/cc
165 * "c" has lower inode number, but we can't move it (2nd mv operation)
166 * before we move "d", which has higher inode number.
168 * So we just memorize which move/rename operations must be performed
169 * later when their respective parent is processed and moved/renamed.
172 /* Indexed by parent directory inode number. */
173 struct rb_root pending_dir_moves;
176 * Reverse index, indexed by the inode number of a directory that
177 * is waiting for the move/rename of its immediate parent before its
178 * own move/rename can be performed.
180 struct rb_root waiting_dir_moves;
183 * A directory that is going to be rm'ed might have a child directory
184 * which is in the pending directory moves index above. In this case,
185 * the directory can only be removed after the move/rename of its child
186 * is performed. Example:
206 * Sequence of steps that lead to the send snapshot:
207 * rm -f /a/b/c/foo.txt
209 * mv /a/b/c/x /a/b/YY
212 * When the child is processed, its move/rename is delayed until its
213 * parent is processed (as explained above), but all other operations
214 * like update utimes, chown, chgrp, etc, are performed and the paths
215 * that it uses for those operations must use the orphanized name of
216 * its parent (the directory we're going to rm later), so we need to
217 * memorize that name.
219 * Indexed by the inode number of the directory to be deleted.
221 struct rb_root orphan_dirs;
224 struct pending_dir_move {
226 struct list_head list;
230 struct list_head update_refs;
233 struct waiting_dir_move {
237 * There might be some directory that could not be removed because it
238 * was waiting for this directory inode to be moved first. Therefore
239 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
244 struct orphan_dir_info {
250 struct name_cache_entry {
251 struct list_head list;
253 * radix_tree has only 32bit entries but we need to handle 64bit inums.
254 * We use the lower 32bit of the 64bit inum to store it in the tree. If
255 * more then one inum would fall into the same entry, we use radix_list
256 * to store the additional entries. radix_list is also used to store
257 * entries where two entries have the same inum but different
260 struct list_head radix_list;
266 int need_later_update;
271 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
273 static struct waiting_dir_move *
274 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
276 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
278 static int need_send_hole(struct send_ctx *sctx)
280 return (sctx->parent_root && !sctx->cur_inode_new &&
281 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
282 S_ISREG(sctx->cur_inode_mode));
285 static void fs_path_reset(struct fs_path *p)
288 p->start = p->buf + p->buf_len - 1;
298 static struct fs_path *fs_path_alloc(void)
302 p = kmalloc(sizeof(*p), GFP_NOFS);
306 p->buf = p->inline_buf;
307 p->buf_len = FS_PATH_INLINE_SIZE;
312 static struct fs_path *fs_path_alloc_reversed(void)
324 static void fs_path_free(struct fs_path *p)
328 if (p->buf != p->inline_buf)
333 static int fs_path_len(struct fs_path *p)
335 return p->end - p->start;
338 static int fs_path_ensure_buf(struct fs_path *p, int len)
346 if (p->buf_len >= len)
350 * First time the inline_buf does not suffice
352 if (p->buf == p->inline_buf) {
353 p->buf = kmalloc(len, GFP_NOFS);
357 * The real size of the buffer is bigger, this will let the
358 * fast path happen most of the time
360 p->buf_len = ksize(p->buf);
364 tmp = krealloc(p->buf, len, GFP_NOFS);
368 p->buf_len = ksize(p->buf);
371 path_len = p->end - p->start;
372 old_buf_len = p->buf_len;
375 tmp_buf = p->buf + old_buf_len - path_len - 1;
376 p->end = p->buf + p->buf_len - 1;
377 p->start = p->end - path_len;
378 memmove(p->start, tmp_buf, path_len + 1);
381 p->end = p->start + path_len;
386 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
392 new_len = p->end - p->start + name_len;
393 if (p->start != p->end)
395 ret = fs_path_ensure_buf(p, new_len);
400 if (p->start != p->end)
402 p->start -= name_len;
403 *prepared = p->start;
405 if (p->start != p->end)
416 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
421 ret = fs_path_prepare_for_add(p, name_len, &prepared);
424 memcpy(prepared, name, name_len);
430 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
435 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
438 memcpy(prepared, p2->start, p2->end - p2->start);
444 static int fs_path_add_from_extent_buffer(struct fs_path *p,
445 struct extent_buffer *eb,
446 unsigned long off, int len)
451 ret = fs_path_prepare_for_add(p, len, &prepared);
455 read_extent_buffer(eb, prepared, off, len);
461 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
465 p->reversed = from->reversed;
468 ret = fs_path_add_path(p, from);
474 static void fs_path_unreverse(struct fs_path *p)
483 len = p->end - p->start;
485 p->end = p->start + len;
486 memmove(p->start, tmp, len + 1);
490 static struct btrfs_path *alloc_path_for_send(void)
492 struct btrfs_path *path;
494 path = btrfs_alloc_path();
497 path->search_commit_root = 1;
498 path->skip_locking = 1;
502 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
512 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
513 /* TODO handle that correctly */
514 /*if (ret == -ERESTARTSYS) {
533 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
535 struct btrfs_tlv_header *hdr;
536 int total_len = sizeof(*hdr) + len;
537 int left = sctx->send_max_size - sctx->send_size;
539 if (unlikely(left < total_len))
542 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
543 hdr->tlv_type = cpu_to_le16(attr);
544 hdr->tlv_len = cpu_to_le16(len);
545 memcpy(hdr + 1, data, len);
546 sctx->send_size += total_len;
551 #define TLV_PUT_DEFINE_INT(bits) \
552 static int tlv_put_u##bits(struct send_ctx *sctx, \
553 u##bits attr, u##bits value) \
555 __le##bits __tmp = cpu_to_le##bits(value); \
556 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
559 TLV_PUT_DEFINE_INT(64)
561 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
562 const char *str, int len)
566 return tlv_put(sctx, attr, str, len);
569 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
572 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
575 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
576 struct extent_buffer *eb,
577 struct btrfs_timespec *ts)
579 struct btrfs_timespec bts;
580 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
581 return tlv_put(sctx, attr, &bts, sizeof(bts));
585 #define TLV_PUT(sctx, attrtype, attrlen, data) \
587 ret = tlv_put(sctx, attrtype, attrlen, data); \
589 goto tlv_put_failure; \
592 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
594 ret = tlv_put_u##bits(sctx, attrtype, value); \
596 goto tlv_put_failure; \
599 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
600 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
601 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
602 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
603 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
605 ret = tlv_put_string(sctx, attrtype, str, len); \
607 goto tlv_put_failure; \
609 #define TLV_PUT_PATH(sctx, attrtype, p) \
611 ret = tlv_put_string(sctx, attrtype, p->start, \
612 p->end - p->start); \
614 goto tlv_put_failure; \
616 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
618 ret = tlv_put_uuid(sctx, attrtype, uuid); \
620 goto tlv_put_failure; \
622 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
624 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
626 goto tlv_put_failure; \
629 static int send_header(struct send_ctx *sctx)
631 struct btrfs_stream_header hdr;
633 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
634 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
636 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
641 * For each command/item we want to send to userspace, we call this function.
643 static int begin_cmd(struct send_ctx *sctx, int cmd)
645 struct btrfs_cmd_header *hdr;
647 if (WARN_ON(!sctx->send_buf))
650 BUG_ON(sctx->send_size);
652 sctx->send_size += sizeof(*hdr);
653 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
654 hdr->cmd = cpu_to_le16(cmd);
659 static int send_cmd(struct send_ctx *sctx)
662 struct btrfs_cmd_header *hdr;
665 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
666 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
669 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
670 hdr->crc = cpu_to_le32(crc);
672 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
675 sctx->total_send_size += sctx->send_size;
676 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
683 * Sends a move instruction to user space
685 static int send_rename(struct send_ctx *sctx,
686 struct fs_path *from, struct fs_path *to)
690 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
692 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
696 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
697 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
699 ret = send_cmd(sctx);
707 * Sends a link instruction to user space
709 static int send_link(struct send_ctx *sctx,
710 struct fs_path *path, struct fs_path *lnk)
714 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
716 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
720 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
721 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
723 ret = send_cmd(sctx);
731 * Sends an unlink instruction to user space
733 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
737 verbose_printk("btrfs: send_unlink %s\n", path->start);
739 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
743 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
745 ret = send_cmd(sctx);
753 * Sends a rmdir instruction to user space
755 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
759 verbose_printk("btrfs: send_rmdir %s\n", path->start);
761 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
765 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
767 ret = send_cmd(sctx);
775 * Helper function to retrieve some fields from an inode item.
777 static int get_inode_info(struct btrfs_root *root,
778 u64 ino, u64 *size, u64 *gen,
779 u64 *mode, u64 *uid, u64 *gid,
783 struct btrfs_inode_item *ii;
784 struct btrfs_key key;
785 struct btrfs_path *path;
787 path = alloc_path_for_send();
792 key.type = BTRFS_INODE_ITEM_KEY;
794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
802 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
803 struct btrfs_inode_item);
805 *size = btrfs_inode_size(path->nodes[0], ii);
807 *gen = btrfs_inode_generation(path->nodes[0], ii);
809 *mode = btrfs_inode_mode(path->nodes[0], ii);
811 *uid = btrfs_inode_uid(path->nodes[0], ii);
813 *gid = btrfs_inode_gid(path->nodes[0], ii);
815 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
818 btrfs_free_path(path);
822 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
827 * Helper function to iterate the entries in ONE btrfs_inode_ref or
828 * btrfs_inode_extref.
829 * The iterate callback may return a non zero value to stop iteration. This can
830 * be a negative value for error codes or 1 to simply stop it.
832 * path must point to the INODE_REF or INODE_EXTREF when called.
834 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
835 struct btrfs_key *found_key, int resolve,
836 iterate_inode_ref_t iterate, void *ctx)
838 struct extent_buffer *eb = path->nodes[0];
839 struct btrfs_item *item;
840 struct btrfs_inode_ref *iref;
841 struct btrfs_inode_extref *extref;
842 struct btrfs_path *tmp_path;
846 int slot = path->slots[0];
853 unsigned long name_off;
854 unsigned long elem_size;
857 p = fs_path_alloc_reversed();
861 tmp_path = alloc_path_for_send();
868 if (found_key->type == BTRFS_INODE_REF_KEY) {
869 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
870 struct btrfs_inode_ref);
871 item = btrfs_item_nr(slot);
872 total = btrfs_item_size(eb, item);
873 elem_size = sizeof(*iref);
875 ptr = btrfs_item_ptr_offset(eb, slot);
876 total = btrfs_item_size_nr(eb, slot);
877 elem_size = sizeof(*extref);
880 while (cur < total) {
883 if (found_key->type == BTRFS_INODE_REF_KEY) {
884 iref = (struct btrfs_inode_ref *)(ptr + cur);
885 name_len = btrfs_inode_ref_name_len(eb, iref);
886 name_off = (unsigned long)(iref + 1);
887 index = btrfs_inode_ref_index(eb, iref);
888 dir = found_key->offset;
890 extref = (struct btrfs_inode_extref *)(ptr + cur);
891 name_len = btrfs_inode_extref_name_len(eb, extref);
892 name_off = (unsigned long)&extref->name;
893 index = btrfs_inode_extref_index(eb, extref);
894 dir = btrfs_inode_extref_parent(eb, extref);
898 start = btrfs_ref_to_path(root, tmp_path, name_len,
902 ret = PTR_ERR(start);
905 if (start < p->buf) {
906 /* overflow , try again with larger buffer */
907 ret = fs_path_ensure_buf(p,
908 p->buf_len + p->buf - start);
911 start = btrfs_ref_to_path(root, tmp_path,
916 ret = PTR_ERR(start);
919 BUG_ON(start < p->buf);
923 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
929 cur += elem_size + name_len;
930 ret = iterate(num, dir, index, p, ctx);
937 btrfs_free_path(tmp_path);
942 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
943 const char *name, int name_len,
944 const char *data, int data_len,
948 * Helper function to iterate the entries in ONE btrfs_dir_item.
949 * The iterate callback may return a non zero value to stop iteration. This can
950 * be a negative value for error codes or 1 to simply stop it.
952 * path must point to the dir item when called.
954 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
955 struct btrfs_key *found_key,
956 iterate_dir_item_t iterate, void *ctx)
959 struct extent_buffer *eb;
960 struct btrfs_item *item;
961 struct btrfs_dir_item *di;
962 struct btrfs_key di_key;
964 const int buf_len = PATH_MAX;
974 buf = kmalloc(buf_len, GFP_NOFS);
981 slot = path->slots[0];
982 item = btrfs_item_nr(slot);
983 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
986 total = btrfs_item_size(eb, item);
989 while (cur < total) {
990 name_len = btrfs_dir_name_len(eb, di);
991 data_len = btrfs_dir_data_len(eb, di);
992 type = btrfs_dir_type(eb, di);
993 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
998 if (name_len + data_len > buf_len) {
1003 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1004 name_len + data_len);
1006 len = sizeof(*di) + name_len + data_len;
1007 di = (struct btrfs_dir_item *)((char *)di + len);
1010 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1011 data_len, type, ctx);
1027 static int __copy_first_ref(int num, u64 dir, int index,
1028 struct fs_path *p, void *ctx)
1031 struct fs_path *pt = ctx;
1033 ret = fs_path_copy(pt, p);
1037 /* we want the first only */
1042 * Retrieve the first path of an inode. If an inode has more then one
1043 * ref/hardlink, this is ignored.
1045 static int get_inode_path(struct btrfs_root *root,
1046 u64 ino, struct fs_path *path)
1049 struct btrfs_key key, found_key;
1050 struct btrfs_path *p;
1052 p = alloc_path_for_send();
1056 fs_path_reset(path);
1059 key.type = BTRFS_INODE_REF_KEY;
1062 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1069 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1070 if (found_key.objectid != ino ||
1071 (found_key.type != BTRFS_INODE_REF_KEY &&
1072 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1077 ret = iterate_inode_ref(root, p, &found_key, 1,
1078 __copy_first_ref, path);
1088 struct backref_ctx {
1089 struct send_ctx *sctx;
1091 /* number of total found references */
1095 * used for clones found in send_root. clones found behind cur_objectid
1096 * and cur_offset are not considered as allowed clones.
1101 /* may be truncated in case it's the last extent in a file */
1104 /* Just to check for bugs in backref resolving */
1108 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1110 u64 root = (u64)(uintptr_t)key;
1111 struct clone_root *cr = (struct clone_root *)elt;
1113 if (root < cr->root->objectid)
1115 if (root > cr->root->objectid)
1120 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1122 struct clone_root *cr1 = (struct clone_root *)e1;
1123 struct clone_root *cr2 = (struct clone_root *)e2;
1125 if (cr1->root->objectid < cr2->root->objectid)
1127 if (cr1->root->objectid > cr2->root->objectid)
1133 * Called for every backref that is found for the current extent.
1134 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1136 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1138 struct backref_ctx *bctx = ctx_;
1139 struct clone_root *found;
1143 /* First check if the root is in the list of accepted clone sources */
1144 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1145 bctx->sctx->clone_roots_cnt,
1146 sizeof(struct clone_root),
1147 __clone_root_cmp_bsearch);
1151 if (found->root == bctx->sctx->send_root &&
1152 ino == bctx->cur_objectid &&
1153 offset == bctx->cur_offset) {
1154 bctx->found_itself = 1;
1158 * There are inodes that have extents that lie behind its i_size. Don't
1159 * accept clones from these extents.
1161 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1166 if (offset + bctx->extent_len > i_size)
1170 * Make sure we don't consider clones from send_root that are
1171 * behind the current inode/offset.
1173 if (found->root == bctx->sctx->send_root) {
1175 * TODO for the moment we don't accept clones from the inode
1176 * that is currently send. We may change this when
1177 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1180 if (ino >= bctx->cur_objectid)
1183 if (ino > bctx->cur_objectid)
1185 if (offset + bctx->extent_len > bctx->cur_offset)
1191 found->found_refs++;
1192 if (ino < found->ino) {
1194 found->offset = offset;
1195 } else if (found->ino == ino) {
1197 * same extent found more then once in the same file.
1199 if (found->offset > offset + bctx->extent_len)
1200 found->offset = offset;
1207 * Given an inode, offset and extent item, it finds a good clone for a clone
1208 * instruction. Returns -ENOENT when none could be found. The function makes
1209 * sure that the returned clone is usable at the point where sending is at the
1210 * moment. This means, that no clones are accepted which lie behind the current
1213 * path must point to the extent item when called.
1215 static int find_extent_clone(struct send_ctx *sctx,
1216 struct btrfs_path *path,
1217 u64 ino, u64 data_offset,
1219 struct clone_root **found)
1226 u64 extent_item_pos;
1228 struct btrfs_file_extent_item *fi;
1229 struct extent_buffer *eb = path->nodes[0];
1230 struct backref_ctx *backref_ctx = NULL;
1231 struct clone_root *cur_clone_root;
1232 struct btrfs_key found_key;
1233 struct btrfs_path *tmp_path;
1237 tmp_path = alloc_path_for_send();
1241 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1247 if (data_offset >= ino_size) {
1249 * There may be extents that lie behind the file's size.
1250 * I at least had this in combination with snapshotting while
1251 * writing large files.
1257 fi = btrfs_item_ptr(eb, path->slots[0],
1258 struct btrfs_file_extent_item);
1259 extent_type = btrfs_file_extent_type(eb, fi);
1260 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1264 compressed = btrfs_file_extent_compression(eb, fi);
1266 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1267 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1268 if (disk_byte == 0) {
1272 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1274 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1275 &found_key, &flags);
1276 btrfs_release_path(tmp_path);
1280 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1286 * Setup the clone roots.
1288 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1289 cur_clone_root = sctx->clone_roots + i;
1290 cur_clone_root->ino = (u64)-1;
1291 cur_clone_root->offset = 0;
1292 cur_clone_root->found_refs = 0;
1295 backref_ctx->sctx = sctx;
1296 backref_ctx->found = 0;
1297 backref_ctx->cur_objectid = ino;
1298 backref_ctx->cur_offset = data_offset;
1299 backref_ctx->found_itself = 0;
1300 backref_ctx->extent_len = num_bytes;
1303 * The last extent of a file may be too large due to page alignment.
1304 * We need to adjust extent_len in this case so that the checks in
1305 * __iterate_backrefs work.
1307 if (data_offset + num_bytes >= ino_size)
1308 backref_ctx->extent_len = ino_size - data_offset;
1311 * Now collect all backrefs.
1313 if (compressed == BTRFS_COMPRESS_NONE)
1314 extent_item_pos = logical - found_key.objectid;
1316 extent_item_pos = 0;
1317 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1318 found_key.objectid, extent_item_pos, 1,
1319 __iterate_backrefs, backref_ctx);
1324 if (!backref_ctx->found_itself) {
1325 /* found a bug in backref code? */
1327 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
1328 "send_root. inode=%llu, offset=%llu, "
1329 "disk_byte=%llu found extent=%llu\n",
1330 ino, data_offset, disk_byte, found_key.objectid);
1334 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1336 "num_bytes=%llu, logical=%llu\n",
1337 data_offset, ino, num_bytes, logical);
1339 if (!backref_ctx->found)
1340 verbose_printk("btrfs: no clones found\n");
1342 cur_clone_root = NULL;
1343 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1344 if (sctx->clone_roots[i].found_refs) {
1345 if (!cur_clone_root)
1346 cur_clone_root = sctx->clone_roots + i;
1347 else if (sctx->clone_roots[i].root == sctx->send_root)
1348 /* prefer clones from send_root over others */
1349 cur_clone_root = sctx->clone_roots + i;
1354 if (cur_clone_root) {
1355 if (compressed != BTRFS_COMPRESS_NONE) {
1357 * Offsets given by iterate_extent_inodes() are relative
1358 * to the start of the extent, we need to add logical
1359 * offset from the file extent item.
1360 * (See why at backref.c:check_extent_in_eb())
1362 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1365 *found = cur_clone_root;
1372 btrfs_free_path(tmp_path);
1377 static int read_symlink(struct btrfs_root *root,
1379 struct fs_path *dest)
1382 struct btrfs_path *path;
1383 struct btrfs_key key;
1384 struct btrfs_file_extent_item *ei;
1390 path = alloc_path_for_send();
1395 key.type = BTRFS_EXTENT_DATA_KEY;
1397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1402 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1403 struct btrfs_file_extent_item);
1404 type = btrfs_file_extent_type(path->nodes[0], ei);
1405 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1406 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1407 BUG_ON(compression);
1409 off = btrfs_file_extent_inline_start(ei);
1410 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
1412 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1415 btrfs_free_path(path);
1420 * Helper function to generate a file name that is unique in the root of
1421 * send_root and parent_root. This is used to generate names for orphan inodes.
1423 static int gen_unique_name(struct send_ctx *sctx,
1425 struct fs_path *dest)
1428 struct btrfs_path *path;
1429 struct btrfs_dir_item *di;
1434 path = alloc_path_for_send();
1439 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1441 ASSERT(len < sizeof(tmp));
1443 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1444 path, BTRFS_FIRST_FREE_OBJECTID,
1445 tmp, strlen(tmp), 0);
1446 btrfs_release_path(path);
1452 /* not unique, try again */
1457 if (!sctx->parent_root) {
1463 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1464 path, BTRFS_FIRST_FREE_OBJECTID,
1465 tmp, strlen(tmp), 0);
1466 btrfs_release_path(path);
1472 /* not unique, try again */
1480 ret = fs_path_add(dest, tmp, strlen(tmp));
1483 btrfs_free_path(path);
1488 inode_state_no_change,
1489 inode_state_will_create,
1490 inode_state_did_create,
1491 inode_state_will_delete,
1492 inode_state_did_delete,
1495 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1503 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1505 if (ret < 0 && ret != -ENOENT)
1509 if (!sctx->parent_root) {
1510 right_ret = -ENOENT;
1512 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1513 NULL, NULL, NULL, NULL);
1514 if (ret < 0 && ret != -ENOENT)
1519 if (!left_ret && !right_ret) {
1520 if (left_gen == gen && right_gen == gen) {
1521 ret = inode_state_no_change;
1522 } else if (left_gen == gen) {
1523 if (ino < sctx->send_progress)
1524 ret = inode_state_did_create;
1526 ret = inode_state_will_create;
1527 } else if (right_gen == gen) {
1528 if (ino < sctx->send_progress)
1529 ret = inode_state_did_delete;
1531 ret = inode_state_will_delete;
1535 } else if (!left_ret) {
1536 if (left_gen == gen) {
1537 if (ino < sctx->send_progress)
1538 ret = inode_state_did_create;
1540 ret = inode_state_will_create;
1544 } else if (!right_ret) {
1545 if (right_gen == gen) {
1546 if (ino < sctx->send_progress)
1547 ret = inode_state_did_delete;
1549 ret = inode_state_will_delete;
1561 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1565 ret = get_cur_inode_state(sctx, ino, gen);
1569 if (ret == inode_state_no_change ||
1570 ret == inode_state_did_create ||
1571 ret == inode_state_will_delete)
1581 * Helper function to lookup a dir item in a dir.
1583 static int lookup_dir_item_inode(struct btrfs_root *root,
1584 u64 dir, const char *name, int name_len,
1589 struct btrfs_dir_item *di;
1590 struct btrfs_key key;
1591 struct btrfs_path *path;
1593 path = alloc_path_for_send();
1597 di = btrfs_lookup_dir_item(NULL, root, path,
1598 dir, name, name_len, 0);
1607 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1608 *found_inode = key.objectid;
1609 *found_type = btrfs_dir_type(path->nodes[0], di);
1612 btrfs_free_path(path);
1617 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1618 * generation of the parent dir and the name of the dir entry.
1620 static int get_first_ref(struct btrfs_root *root, u64 ino,
1621 u64 *dir, u64 *dir_gen, struct fs_path *name)
1624 struct btrfs_key key;
1625 struct btrfs_key found_key;
1626 struct btrfs_path *path;
1630 path = alloc_path_for_send();
1635 key.type = BTRFS_INODE_REF_KEY;
1638 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1642 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1644 if (ret || found_key.objectid != ino ||
1645 (found_key.type != BTRFS_INODE_REF_KEY &&
1646 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1651 if (key.type == BTRFS_INODE_REF_KEY) {
1652 struct btrfs_inode_ref *iref;
1653 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1654 struct btrfs_inode_ref);
1655 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1656 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1657 (unsigned long)(iref + 1),
1659 parent_dir = found_key.offset;
1661 struct btrfs_inode_extref *extref;
1662 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1663 struct btrfs_inode_extref);
1664 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1665 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1666 (unsigned long)&extref->name, len);
1667 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1671 btrfs_release_path(path);
1673 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
1681 btrfs_free_path(path);
1685 static int is_first_ref(struct btrfs_root *root,
1687 const char *name, int name_len)
1690 struct fs_path *tmp_name;
1694 tmp_name = fs_path_alloc();
1698 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1702 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1707 ret = !memcmp(tmp_name->start, name, name_len);
1710 fs_path_free(tmp_name);
1715 * Used by process_recorded_refs to determine if a new ref would overwrite an
1716 * already existing ref. In case it detects an overwrite, it returns the
1717 * inode/gen in who_ino/who_gen.
1718 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1719 * to make sure later references to the overwritten inode are possible.
1720 * Orphanizing is however only required for the first ref of an inode.
1721 * process_recorded_refs does an additional is_first_ref check to see if
1722 * orphanizing is really required.
1724 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1725 const char *name, int name_len,
1726 u64 *who_ino, u64 *who_gen)
1730 u64 other_inode = 0;
1733 if (!sctx->parent_root)
1736 ret = is_inode_existent(sctx, dir, dir_gen);
1741 * If we have a parent root we need to verify that the parent dir was
1742 * not delted and then re-created, if it was then we have no overwrite
1743 * and we can just unlink this entry.
1745 if (sctx->parent_root) {
1746 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1748 if (ret < 0 && ret != -ENOENT)
1758 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1759 &other_inode, &other_type);
1760 if (ret < 0 && ret != -ENOENT)
1768 * Check if the overwritten ref was already processed. If yes, the ref
1769 * was already unlinked/moved, so we can safely assume that we will not
1770 * overwrite anything at this point in time.
1772 if (other_inode > sctx->send_progress) {
1773 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1774 who_gen, NULL, NULL, NULL, NULL);
1779 *who_ino = other_inode;
1789 * Checks if the ref was overwritten by an already processed inode. This is
1790 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1791 * thus the orphan name needs be used.
1792 * process_recorded_refs also uses it to avoid unlinking of refs that were
1795 static int did_overwrite_ref(struct send_ctx *sctx,
1796 u64 dir, u64 dir_gen,
1797 u64 ino, u64 ino_gen,
1798 const char *name, int name_len)
1805 if (!sctx->parent_root)
1808 ret = is_inode_existent(sctx, dir, dir_gen);
1812 /* check if the ref was overwritten by another ref */
1813 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1814 &ow_inode, &other_type);
1815 if (ret < 0 && ret != -ENOENT)
1818 /* was never and will never be overwritten */
1823 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1828 if (ow_inode == ino && gen == ino_gen) {
1833 /* we know that it is or will be overwritten. check this now */
1834 if (ow_inode < sctx->send_progress)
1844 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1845 * that got overwritten. This is used by process_recorded_refs to determine
1846 * if it has to use the path as returned by get_cur_path or the orphan name.
1848 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1851 struct fs_path *name = NULL;
1855 if (!sctx->parent_root)
1858 name = fs_path_alloc();
1862 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1866 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1867 name->start, fs_path_len(name));
1875 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1876 * so we need to do some special handling in case we have clashes. This function
1877 * takes care of this with the help of name_cache_entry::radix_list.
1878 * In case of error, nce is kfreed.
1880 static int name_cache_insert(struct send_ctx *sctx,
1881 struct name_cache_entry *nce)
1884 struct list_head *nce_head;
1886 nce_head = radix_tree_lookup(&sctx->name_cache,
1887 (unsigned long)nce->ino);
1889 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1894 INIT_LIST_HEAD(nce_head);
1896 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1903 list_add_tail(&nce->radix_list, nce_head);
1904 list_add_tail(&nce->list, &sctx->name_cache_list);
1905 sctx->name_cache_size++;
1910 static void name_cache_delete(struct send_ctx *sctx,
1911 struct name_cache_entry *nce)
1913 struct list_head *nce_head;
1915 nce_head = radix_tree_lookup(&sctx->name_cache,
1916 (unsigned long)nce->ino);
1918 btrfs_err(sctx->send_root->fs_info,
1919 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1920 nce->ino, sctx->name_cache_size);
1923 list_del(&nce->radix_list);
1924 list_del(&nce->list);
1925 sctx->name_cache_size--;
1928 * We may not get to the final release of nce_head if the lookup fails
1930 if (nce_head && list_empty(nce_head)) {
1931 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1936 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1939 struct list_head *nce_head;
1940 struct name_cache_entry *cur;
1942 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1946 list_for_each_entry(cur, nce_head, radix_list) {
1947 if (cur->ino == ino && cur->gen == gen)
1954 * Removes the entry from the list and adds it back to the end. This marks the
1955 * entry as recently used so that name_cache_clean_unused does not remove it.
1957 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1959 list_del(&nce->list);
1960 list_add_tail(&nce->list, &sctx->name_cache_list);
1964 * Remove some entries from the beginning of name_cache_list.
1966 static void name_cache_clean_unused(struct send_ctx *sctx)
1968 struct name_cache_entry *nce;
1970 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1973 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1974 nce = list_entry(sctx->name_cache_list.next,
1975 struct name_cache_entry, list);
1976 name_cache_delete(sctx, nce);
1981 static void name_cache_free(struct send_ctx *sctx)
1983 struct name_cache_entry *nce;
1985 while (!list_empty(&sctx->name_cache_list)) {
1986 nce = list_entry(sctx->name_cache_list.next,
1987 struct name_cache_entry, list);
1988 name_cache_delete(sctx, nce);
1994 * Used by get_cur_path for each ref up to the root.
1995 * Returns 0 if it succeeded.
1996 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1997 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1998 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1999 * Returns <0 in case of error.
2001 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2003 int skip_name_cache,
2006 struct fs_path *dest)
2010 struct btrfs_path *path = NULL;
2011 struct name_cache_entry *nce = NULL;
2013 if (skip_name_cache)
2016 * First check if we already did a call to this function with the same
2017 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2018 * return the cached result.
2020 nce = name_cache_search(sctx, ino, gen);
2022 if (ino < sctx->send_progress && nce->need_later_update) {
2023 name_cache_delete(sctx, nce);
2027 name_cache_used(sctx, nce);
2028 *parent_ino = nce->parent_ino;
2029 *parent_gen = nce->parent_gen;
2030 ret = fs_path_add(dest, nce->name, nce->name_len);
2038 path = alloc_path_for_send();
2043 * If the inode is not existent yet, add the orphan name and return 1.
2044 * This should only happen for the parent dir that we determine in
2047 ret = is_inode_existent(sctx, ino, gen);
2052 ret = gen_unique_name(sctx, ino, gen, dest);
2061 * Depending on whether the inode was already processed or not, use
2062 * send_root or parent_root for ref lookup.
2064 if (ino < sctx->send_progress && !skip_name_cache)
2065 ret = get_first_ref(sctx->send_root, ino,
2066 parent_ino, parent_gen, dest);
2068 ret = get_first_ref(sctx->parent_root, ino,
2069 parent_ino, parent_gen, dest);
2074 * Check if the ref was overwritten by an inode's ref that was processed
2075 * earlier. If yes, treat as orphan and return 1.
2077 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2078 dest->start, dest->end - dest->start);
2082 fs_path_reset(dest);
2083 ret = gen_unique_name(sctx, ino, gen, dest);
2088 if (skip_name_cache)
2093 * Store the result of the lookup in the name cache.
2095 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2103 nce->parent_ino = *parent_ino;
2104 nce->parent_gen = *parent_gen;
2105 nce->name_len = fs_path_len(dest);
2107 strcpy(nce->name, dest->start);
2109 if (ino < sctx->send_progress)
2110 nce->need_later_update = 0;
2112 nce->need_later_update = 1;
2114 nce_ret = name_cache_insert(sctx, nce);
2117 name_cache_clean_unused(sctx);
2120 btrfs_free_path(path);
2125 * Magic happens here. This function returns the first ref to an inode as it
2126 * would look like while receiving the stream at this point in time.
2127 * We walk the path up to the root. For every inode in between, we check if it
2128 * was already processed/sent. If yes, we continue with the parent as found
2129 * in send_root. If not, we continue with the parent as found in parent_root.
2130 * If we encounter an inode that was deleted at this point in time, we use the
2131 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2132 * that were not created yet and overwritten inodes/refs.
2134 * When do we have have orphan inodes:
2135 * 1. When an inode is freshly created and thus no valid refs are available yet
2136 * 2. When a directory lost all it's refs (deleted) but still has dir items
2137 * inside which were not processed yet (pending for move/delete). If anyone
2138 * tried to get the path to the dir items, it would get a path inside that
2140 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2141 * of an unprocessed inode. If in that case the first ref would be
2142 * overwritten, the overwritten inode gets "orphanized". Later when we
2143 * process this overwritten inode, it is restored at a new place by moving
2146 * sctx->send_progress tells this function at which point in time receiving
2149 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2150 struct fs_path *dest)
2153 struct fs_path *name = NULL;
2154 u64 parent_inode = 0;
2157 int skip_name_cache = 0;
2159 name = fs_path_alloc();
2165 if (is_waiting_for_move(sctx, ino))
2166 skip_name_cache = 1;
2169 fs_path_reset(dest);
2171 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2172 fs_path_reset(name);
2174 if (is_waiting_for_rm(sctx, ino)) {
2175 ret = gen_unique_name(sctx, ino, gen, name);
2178 ret = fs_path_add_path(dest, name);
2182 ret = __get_cur_name_and_parent(sctx, ino, gen, skip_name_cache,
2183 &parent_inode, &parent_gen, name);
2189 if (!skip_name_cache &&
2190 is_waiting_for_move(sctx, parent_inode))
2191 skip_name_cache = 1;
2193 ret = fs_path_add_path(dest, name);
2204 fs_path_unreverse(dest);
2209 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2211 static int send_subvol_begin(struct send_ctx *sctx)
2214 struct btrfs_root *send_root = sctx->send_root;
2215 struct btrfs_root *parent_root = sctx->parent_root;
2216 struct btrfs_path *path;
2217 struct btrfs_key key;
2218 struct btrfs_root_ref *ref;
2219 struct extent_buffer *leaf;
2223 path = btrfs_alloc_path();
2227 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2229 btrfs_free_path(path);
2233 key.objectid = send_root->objectid;
2234 key.type = BTRFS_ROOT_BACKREF_KEY;
2237 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2246 leaf = path->nodes[0];
2247 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2248 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2249 key.objectid != send_root->objectid) {
2253 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2254 namelen = btrfs_root_ref_name_len(leaf, ref);
2255 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2256 btrfs_release_path(path);
2259 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2263 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2268 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2269 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2270 sctx->send_root->root_item.uuid);
2271 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2272 le64_to_cpu(sctx->send_root->root_item.ctransid));
2274 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2275 sctx->parent_root->root_item.uuid);
2276 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2277 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2280 ret = send_cmd(sctx);
2284 btrfs_free_path(path);
2289 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2294 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2296 p = fs_path_alloc();
2300 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2304 ret = get_cur_path(sctx, ino, gen, p);
2307 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2308 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2310 ret = send_cmd(sctx);
2318 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2323 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2325 p = fs_path_alloc();
2329 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2333 ret = get_cur_path(sctx, ino, gen, p);
2336 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2337 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2339 ret = send_cmd(sctx);
2347 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2352 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2354 p = fs_path_alloc();
2358 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2362 ret = get_cur_path(sctx, ino, gen, p);
2365 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2366 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2367 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2369 ret = send_cmd(sctx);
2377 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2380 struct fs_path *p = NULL;
2381 struct btrfs_inode_item *ii;
2382 struct btrfs_path *path = NULL;
2383 struct extent_buffer *eb;
2384 struct btrfs_key key;
2387 verbose_printk("btrfs: send_utimes %llu\n", ino);
2389 p = fs_path_alloc();
2393 path = alloc_path_for_send();
2400 key.type = BTRFS_INODE_ITEM_KEY;
2402 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2406 eb = path->nodes[0];
2407 slot = path->slots[0];
2408 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2410 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2414 ret = get_cur_path(sctx, ino, gen, p);
2417 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2418 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2419 btrfs_inode_atime(ii));
2420 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2421 btrfs_inode_mtime(ii));
2422 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2423 btrfs_inode_ctime(ii));
2424 /* TODO Add otime support when the otime patches get into upstream */
2426 ret = send_cmd(sctx);
2431 btrfs_free_path(path);
2436 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2437 * a valid path yet because we did not process the refs yet. So, the inode
2438 * is created as orphan.
2440 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2449 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2451 p = fs_path_alloc();
2455 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2460 if (S_ISREG(mode)) {
2461 cmd = BTRFS_SEND_C_MKFILE;
2462 } else if (S_ISDIR(mode)) {
2463 cmd = BTRFS_SEND_C_MKDIR;
2464 } else if (S_ISLNK(mode)) {
2465 cmd = BTRFS_SEND_C_SYMLINK;
2466 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2467 cmd = BTRFS_SEND_C_MKNOD;
2468 } else if (S_ISFIFO(mode)) {
2469 cmd = BTRFS_SEND_C_MKFIFO;
2470 } else if (S_ISSOCK(mode)) {
2471 cmd = BTRFS_SEND_C_MKSOCK;
2473 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2474 (int)(mode & S_IFMT));
2479 ret = begin_cmd(sctx, cmd);
2483 ret = gen_unique_name(sctx, ino, gen, p);
2487 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2488 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2490 if (S_ISLNK(mode)) {
2492 ret = read_symlink(sctx->send_root, ino, p);
2495 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2496 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2497 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2498 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2499 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2502 ret = send_cmd(sctx);
2514 * We need some special handling for inodes that get processed before the parent
2515 * directory got created. See process_recorded_refs for details.
2516 * This function does the check if we already created the dir out of order.
2518 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2521 struct btrfs_path *path = NULL;
2522 struct btrfs_key key;
2523 struct btrfs_key found_key;
2524 struct btrfs_key di_key;
2525 struct extent_buffer *eb;
2526 struct btrfs_dir_item *di;
2529 path = alloc_path_for_send();
2536 key.type = BTRFS_DIR_INDEX_KEY;
2538 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2543 eb = path->nodes[0];
2544 slot = path->slots[0];
2545 if (slot >= btrfs_header_nritems(eb)) {
2546 ret = btrfs_next_leaf(sctx->send_root, path);
2549 } else if (ret > 0) {
2556 btrfs_item_key_to_cpu(eb, &found_key, slot);
2557 if (found_key.objectid != key.objectid ||
2558 found_key.type != key.type) {
2563 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2564 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2566 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2567 di_key.objectid < sctx->send_progress) {
2576 btrfs_free_path(path);
2581 * Only creates the inode if it is:
2582 * 1. Not a directory
2583 * 2. Or a directory which was not created already due to out of order
2584 * directories. See did_create_dir and process_recorded_refs for details.
2586 static int send_create_inode_if_needed(struct send_ctx *sctx)
2590 if (S_ISDIR(sctx->cur_inode_mode)) {
2591 ret = did_create_dir(sctx, sctx->cur_ino);
2600 ret = send_create_inode(sctx, sctx->cur_ino);
2608 struct recorded_ref {
2609 struct list_head list;
2612 struct fs_path *full_path;
2620 * We need to process new refs before deleted refs, but compare_tree gives us
2621 * everything mixed. So we first record all refs and later process them.
2622 * This function is a helper to record one ref.
2624 static int record_ref(struct list_head *head, u64 dir,
2625 u64 dir_gen, struct fs_path *path)
2627 struct recorded_ref *ref;
2629 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2634 ref->dir_gen = dir_gen;
2635 ref->full_path = path;
2637 ref->name = (char *)kbasename(ref->full_path->start);
2638 ref->name_len = ref->full_path->end - ref->name;
2639 ref->dir_path = ref->full_path->start;
2640 if (ref->name == ref->full_path->start)
2641 ref->dir_path_len = 0;
2643 ref->dir_path_len = ref->full_path->end -
2644 ref->full_path->start - 1 - ref->name_len;
2646 list_add_tail(&ref->list, head);
2650 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2652 struct recorded_ref *new;
2654 new = kmalloc(sizeof(*ref), GFP_NOFS);
2658 new->dir = ref->dir;
2659 new->dir_gen = ref->dir_gen;
2660 new->full_path = NULL;
2661 INIT_LIST_HEAD(&new->list);
2662 list_add_tail(&new->list, list);
2666 static void __free_recorded_refs(struct list_head *head)
2668 struct recorded_ref *cur;
2670 while (!list_empty(head)) {
2671 cur = list_entry(head->next, struct recorded_ref, list);
2672 fs_path_free(cur->full_path);
2673 list_del(&cur->list);
2678 static void free_recorded_refs(struct send_ctx *sctx)
2680 __free_recorded_refs(&sctx->new_refs);
2681 __free_recorded_refs(&sctx->deleted_refs);
2685 * Renames/moves a file/dir to its orphan name. Used when the first
2686 * ref of an unprocessed inode gets overwritten and for all non empty
2689 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2690 struct fs_path *path)
2693 struct fs_path *orphan;
2695 orphan = fs_path_alloc();
2699 ret = gen_unique_name(sctx, ino, gen, orphan);
2703 ret = send_rename(sctx, path, orphan);
2706 fs_path_free(orphan);
2710 static struct orphan_dir_info *
2711 add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2713 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2714 struct rb_node *parent = NULL;
2715 struct orphan_dir_info *entry, *odi;
2717 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2719 return ERR_PTR(-ENOMEM);
2725 entry = rb_entry(parent, struct orphan_dir_info, node);
2726 if (dir_ino < entry->ino) {
2728 } else if (dir_ino > entry->ino) {
2729 p = &(*p)->rb_right;
2736 rb_link_node(&odi->node, parent, p);
2737 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2741 static struct orphan_dir_info *
2742 get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2744 struct rb_node *n = sctx->orphan_dirs.rb_node;
2745 struct orphan_dir_info *entry;
2748 entry = rb_entry(n, struct orphan_dir_info, node);
2749 if (dir_ino < entry->ino)
2751 else if (dir_ino > entry->ino)
2759 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2761 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2766 static void free_orphan_dir_info(struct send_ctx *sctx,
2767 struct orphan_dir_info *odi)
2771 rb_erase(&odi->node, &sctx->orphan_dirs);
2776 * Returns 1 if a directory can be removed at this point in time.
2777 * We check this by iterating all dir items and checking if the inode behind
2778 * the dir item was already processed.
2780 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2784 struct btrfs_root *root = sctx->parent_root;
2785 struct btrfs_path *path;
2786 struct btrfs_key key;
2787 struct btrfs_key found_key;
2788 struct btrfs_key loc;
2789 struct btrfs_dir_item *di;
2792 * Don't try to rmdir the top/root subvolume dir.
2794 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2797 path = alloc_path_for_send();
2802 key.type = BTRFS_DIR_INDEX_KEY;
2804 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2809 struct waiting_dir_move *dm;
2811 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2812 ret = btrfs_next_leaf(root, path);
2819 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2821 if (found_key.objectid != key.objectid ||
2822 found_key.type != key.type)
2825 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2826 struct btrfs_dir_item);
2827 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2829 dm = get_waiting_dir_move(sctx, loc.objectid);
2831 struct orphan_dir_info *odi;
2833 odi = add_orphan_dir_info(sctx, dir);
2839 dm->rmdir_ino = dir;
2844 if (loc.objectid > send_progress) {
2855 btrfs_free_path(path);
2859 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2861 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
2863 return entry != NULL;
2866 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2868 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2869 struct rb_node *parent = NULL;
2870 struct waiting_dir_move *entry, *dm;
2872 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2880 entry = rb_entry(parent, struct waiting_dir_move, node);
2881 if (ino < entry->ino) {
2883 } else if (ino > entry->ino) {
2884 p = &(*p)->rb_right;
2891 rb_link_node(&dm->node, parent, p);
2892 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2896 static struct waiting_dir_move *
2897 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2899 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2900 struct waiting_dir_move *entry;
2903 entry = rb_entry(n, struct waiting_dir_move, node);
2904 if (ino < entry->ino)
2906 else if (ino > entry->ino)
2914 static void free_waiting_dir_move(struct send_ctx *sctx,
2915 struct waiting_dir_move *dm)
2919 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2923 static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino)
2925 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2926 struct rb_node *parent = NULL;
2927 struct pending_dir_move *entry, *pm;
2928 struct recorded_ref *cur;
2932 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2935 pm->parent_ino = parent_ino;
2936 pm->ino = sctx->cur_ino;
2937 pm->gen = sctx->cur_inode_gen;
2938 INIT_LIST_HEAD(&pm->list);
2939 INIT_LIST_HEAD(&pm->update_refs);
2940 RB_CLEAR_NODE(&pm->node);
2944 entry = rb_entry(parent, struct pending_dir_move, node);
2945 if (parent_ino < entry->parent_ino) {
2947 } else if (parent_ino > entry->parent_ino) {
2948 p = &(*p)->rb_right;
2955 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2956 ret = dup_ref(cur, &pm->update_refs);
2960 list_for_each_entry(cur, &sctx->new_refs, list) {
2961 ret = dup_ref(cur, &pm->update_refs);
2966 ret = add_waiting_dir_move(sctx, pm->ino);
2971 list_add_tail(&pm->list, &entry->list);
2973 rb_link_node(&pm->node, parent, p);
2974 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
2979 __free_recorded_refs(&pm->update_refs);
2985 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
2988 struct rb_node *n = sctx->pending_dir_moves.rb_node;
2989 struct pending_dir_move *entry;
2992 entry = rb_entry(n, struct pending_dir_move, node);
2993 if (parent_ino < entry->parent_ino)
2995 else if (parent_ino > entry->parent_ino)
3003 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3005 struct fs_path *from_path = NULL;
3006 struct fs_path *to_path = NULL;
3007 struct fs_path *name = NULL;
3008 u64 orig_progress = sctx->send_progress;
3009 struct recorded_ref *cur;
3010 u64 parent_ino, parent_gen;
3011 struct waiting_dir_move *dm = NULL;
3015 name = fs_path_alloc();
3016 from_path = fs_path_alloc();
3017 if (!name || !from_path) {
3022 dm = get_waiting_dir_move(sctx, pm->ino);
3024 rmdir_ino = dm->rmdir_ino;
3025 free_waiting_dir_move(sctx, dm);
3027 ret = get_first_ref(sctx->parent_root, pm->ino,
3028 &parent_ino, &parent_gen, name);
3032 if (parent_ino == sctx->cur_ino) {
3033 /* child only renamed, not moved */
3034 ASSERT(parent_gen == sctx->cur_inode_gen);
3035 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3039 ret = fs_path_add_path(from_path, name);
3043 /* child moved and maybe renamed too */
3044 sctx->send_progress = pm->ino;
3045 ret = get_cur_path(sctx, pm->ino, pm->gen, from_path);
3053 to_path = fs_path_alloc();
3059 sctx->send_progress = sctx->cur_ino + 1;
3060 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3064 ret = send_rename(sctx, from_path, to_path);
3069 struct orphan_dir_info *odi;
3071 odi = get_orphan_dir_info(sctx, rmdir_ino);
3073 /* already deleted */
3076 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3082 name = fs_path_alloc();
3087 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3090 ret = send_rmdir(sctx, name);
3093 free_orphan_dir_info(sctx, odi);
3097 ret = send_utimes(sctx, pm->ino, pm->gen);
3102 * After rename/move, need to update the utimes of both new parent(s)
3103 * and old parent(s).
3105 list_for_each_entry(cur, &pm->update_refs, list) {
3106 if (cur->dir == rmdir_ino)
3108 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3115 fs_path_free(from_path);
3116 fs_path_free(to_path);
3117 sctx->send_progress = orig_progress;
3122 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3124 if (!list_empty(&m->list))
3126 if (!RB_EMPTY_NODE(&m->node))
3127 rb_erase(&m->node, &sctx->pending_dir_moves);
3128 __free_recorded_refs(&m->update_refs);
3132 static void tail_append_pending_moves(struct pending_dir_move *moves,
3133 struct list_head *stack)
3135 if (list_empty(&moves->list)) {
3136 list_add_tail(&moves->list, stack);
3139 list_splice_init(&moves->list, &list);
3140 list_add_tail(&moves->list, stack);
3141 list_splice_tail(&list, stack);
3145 static int apply_children_dir_moves(struct send_ctx *sctx)
3147 struct pending_dir_move *pm;
3148 struct list_head stack;
3149 u64 parent_ino = sctx->cur_ino;
3152 pm = get_pending_dir_moves(sctx, parent_ino);
3156 INIT_LIST_HEAD(&stack);
3157 tail_append_pending_moves(pm, &stack);
3159 while (!list_empty(&stack)) {
3160 pm = list_first_entry(&stack, struct pending_dir_move, list);
3161 parent_ino = pm->ino;
3162 ret = apply_dir_move(sctx, pm);
3163 free_pending_move(sctx, pm);
3166 pm = get_pending_dir_moves(sctx, parent_ino);
3168 tail_append_pending_moves(pm, &stack);
3173 while (!list_empty(&stack)) {
3174 pm = list_first_entry(&stack, struct pending_dir_move, list);
3175 free_pending_move(sctx, pm);
3180 static int wait_for_parent_move(struct send_ctx *sctx,
3181 struct recorded_ref *parent_ref)
3184 u64 ino = parent_ref->dir;
3185 u64 parent_ino_before, parent_ino_after;
3186 u64 new_gen, old_gen;
3187 struct fs_path *path_before = NULL;
3188 struct fs_path *path_after = NULL;
3191 if (parent_ref->dir <= sctx->cur_ino)
3194 if (is_waiting_for_move(sctx, ino))
3197 ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen,
3198 NULL, NULL, NULL, NULL);
3204 ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen,
3205 NULL, NULL, NULL, NULL);
3209 if (new_gen != old_gen)
3212 path_before = fs_path_alloc();
3216 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3218 if (ret == -ENOENT) {
3221 } else if (ret < 0) {
3225 path_after = fs_path_alloc();
3231 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3233 if (ret == -ENOENT) {
3236 } else if (ret < 0) {
3240 len1 = fs_path_len(path_before);
3241 len2 = fs_path_len(path_after);
3242 if (parent_ino_before != parent_ino_after || len1 != len2 ||
3243 memcmp(path_before->start, path_after->start, len1)) {
3250 fs_path_free(path_before);
3251 fs_path_free(path_after);
3257 * This does all the move/link/unlink/rmdir magic.
3259 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3262 struct recorded_ref *cur;
3263 struct recorded_ref *cur2;
3264 struct list_head check_dirs;
3265 struct fs_path *valid_path = NULL;
3268 int did_overwrite = 0;
3270 u64 last_dir_ino_rm = 0;
3272 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3275 * This should never happen as the root dir always has the same ref
3276 * which is always '..'
3278 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3279 INIT_LIST_HEAD(&check_dirs);
3281 valid_path = fs_path_alloc();
3288 * First, check if the first ref of the current inode was overwritten
3289 * before. If yes, we know that the current inode was already orphanized
3290 * and thus use the orphan name. If not, we can use get_cur_path to
3291 * get the path of the first ref as it would like while receiving at
3292 * this point in time.
3293 * New inodes are always orphan at the beginning, so force to use the
3294 * orphan name in this case.
3295 * The first ref is stored in valid_path and will be updated if it
3296 * gets moved around.
3298 if (!sctx->cur_inode_new) {
3299 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3300 sctx->cur_inode_gen);
3306 if (sctx->cur_inode_new || did_overwrite) {
3307 ret = gen_unique_name(sctx, sctx->cur_ino,
3308 sctx->cur_inode_gen, valid_path);
3313 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3319 list_for_each_entry(cur, &sctx->new_refs, list) {
3321 * We may have refs where the parent directory does not exist
3322 * yet. This happens if the parent directories inum is higher
3323 * the the current inum. To handle this case, we create the
3324 * parent directory out of order. But we need to check if this
3325 * did already happen before due to other refs in the same dir.
3327 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3330 if (ret == inode_state_will_create) {
3333 * First check if any of the current inodes refs did
3334 * already create the dir.
3336 list_for_each_entry(cur2, &sctx->new_refs, list) {
3339 if (cur2->dir == cur->dir) {
3346 * If that did not happen, check if a previous inode
3347 * did already create the dir.
3350 ret = did_create_dir(sctx, cur->dir);
3354 ret = send_create_inode(sctx, cur->dir);
3361 * Check if this new ref would overwrite the first ref of
3362 * another unprocessed inode. If yes, orphanize the
3363 * overwritten inode. If we find an overwritten ref that is
3364 * not the first ref, simply unlink it.
3366 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3367 cur->name, cur->name_len,
3368 &ow_inode, &ow_gen);
3372 ret = is_first_ref(sctx->parent_root,
3373 ow_inode, cur->dir, cur->name,
3378 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3383 ret = send_unlink(sctx, cur->full_path);
3390 * link/move the ref to the new place. If we have an orphan
3391 * inode, move it and update valid_path. If not, link or move
3392 * it depending on the inode mode.
3395 ret = send_rename(sctx, valid_path, cur->full_path);
3399 ret = fs_path_copy(valid_path, cur->full_path);
3403 if (S_ISDIR(sctx->cur_inode_mode)) {
3405 * Dirs can't be linked, so move it. For moved
3406 * dirs, we always have one new and one deleted
3407 * ref. The deleted ref is ignored later.
3409 ret = wait_for_parent_move(sctx, cur);
3413 ret = add_pending_dir_move(sctx,
3417 ret = send_rename(sctx, valid_path,
3420 ret = fs_path_copy(valid_path,
3426 ret = send_link(sctx, cur->full_path,
3432 ret = dup_ref(cur, &check_dirs);
3437 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3439 * Check if we can already rmdir the directory. If not,
3440 * orphanize it. For every dir item inside that gets deleted
3441 * later, we do this check again and rmdir it then if possible.
3442 * See the use of check_dirs for more details.
3444 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3449 ret = send_rmdir(sctx, valid_path);
3452 } else if (!is_orphan) {
3453 ret = orphanize_inode(sctx, sctx->cur_ino,
3454 sctx->cur_inode_gen, valid_path);
3460 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3461 ret = dup_ref(cur, &check_dirs);
3465 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3466 !list_empty(&sctx->deleted_refs)) {
3468 * We have a moved dir. Add the old parent to check_dirs
3470 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3472 ret = dup_ref(cur, &check_dirs);
3475 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3477 * We have a non dir inode. Go through all deleted refs and
3478 * unlink them if they were not already overwritten by other
3481 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3482 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3483 sctx->cur_ino, sctx->cur_inode_gen,
3484 cur->name, cur->name_len);
3488 ret = send_unlink(sctx, cur->full_path);
3492 ret = dup_ref(cur, &check_dirs);
3497 * If the inode is still orphan, unlink the orphan. This may
3498 * happen when a previous inode did overwrite the first ref
3499 * of this inode and no new refs were added for the current
3500 * inode. Unlinking does not mean that the inode is deleted in
3501 * all cases. There may still be links to this inode in other
3505 ret = send_unlink(sctx, valid_path);
3512 * We did collect all parent dirs where cur_inode was once located. We
3513 * now go through all these dirs and check if they are pending for
3514 * deletion and if it's finally possible to perform the rmdir now.
3515 * We also update the inode stats of the parent dirs here.
3517 list_for_each_entry(cur, &check_dirs, list) {
3519 * In case we had refs into dirs that were not processed yet,
3520 * we don't need to do the utime and rmdir logic for these dirs.
3521 * The dir will be processed later.
3523 if (cur->dir > sctx->cur_ino)
3526 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3530 if (ret == inode_state_did_create ||
3531 ret == inode_state_no_change) {
3532 /* TODO delayed utimes */
3533 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3536 } else if (ret == inode_state_did_delete &&
3537 cur->dir != last_dir_ino_rm) {
3538 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3543 ret = get_cur_path(sctx, cur->dir,
3544 cur->dir_gen, valid_path);
3547 ret = send_rmdir(sctx, valid_path);
3550 last_dir_ino_rm = cur->dir;
3558 __free_recorded_refs(&check_dirs);
3559 free_recorded_refs(sctx);
3560 fs_path_free(valid_path);
3564 static int __record_new_ref(int num, u64 dir, int index,
3565 struct fs_path *name,
3569 struct send_ctx *sctx = ctx;
3573 p = fs_path_alloc();
3577 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
3582 ret = get_cur_path(sctx, dir, gen, p);
3585 ret = fs_path_add_path(p, name);
3589 ret = record_ref(&sctx->new_refs, dir, gen, p);
3597 static int __record_deleted_ref(int num, u64 dir, int index,
3598 struct fs_path *name,
3602 struct send_ctx *sctx = ctx;
3606 p = fs_path_alloc();
3610 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3615 ret = get_cur_path(sctx, dir, gen, p);
3618 ret = fs_path_add_path(p, name);
3622 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3630 static int record_new_ref(struct send_ctx *sctx)
3634 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3635 sctx->cmp_key, 0, __record_new_ref, sctx);
3644 static int record_deleted_ref(struct send_ctx *sctx)
3648 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3649 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3658 struct find_ref_ctx {
3661 struct btrfs_root *root;
3662 struct fs_path *name;
3666 static int __find_iref(int num, u64 dir, int index,
3667 struct fs_path *name,
3670 struct find_ref_ctx *ctx = ctx_;
3674 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3675 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3677 * To avoid doing extra lookups we'll only do this if everything
3680 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3684 if (dir_gen != ctx->dir_gen)
3686 ctx->found_idx = num;
3692 static int find_iref(struct btrfs_root *root,
3693 struct btrfs_path *path,
3694 struct btrfs_key *key,
3695 u64 dir, u64 dir_gen, struct fs_path *name)
3698 struct find_ref_ctx ctx;
3702 ctx.dir_gen = dir_gen;
3706 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3710 if (ctx.found_idx == -1)
3713 return ctx.found_idx;
3716 static int __record_changed_new_ref(int num, u64 dir, int index,
3717 struct fs_path *name,
3722 struct send_ctx *sctx = ctx;
3724 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3729 ret = find_iref(sctx->parent_root, sctx->right_path,
3730 sctx->cmp_key, dir, dir_gen, name);
3732 ret = __record_new_ref(num, dir, index, name, sctx);
3739 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3740 struct fs_path *name,
3745 struct send_ctx *sctx = ctx;
3747 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3752 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3753 dir, dir_gen, name);
3755 ret = __record_deleted_ref(num, dir, index, name, sctx);
3762 static int record_changed_ref(struct send_ctx *sctx)
3766 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3767 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3770 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3771 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3781 * Record and process all refs at once. Needed when an inode changes the
3782 * generation number, which means that it was deleted and recreated.
3784 static int process_all_refs(struct send_ctx *sctx,
3785 enum btrfs_compare_tree_result cmd)
3788 struct btrfs_root *root;
3789 struct btrfs_path *path;
3790 struct btrfs_key key;
3791 struct btrfs_key found_key;
3792 struct extent_buffer *eb;
3794 iterate_inode_ref_t cb;
3795 int pending_move = 0;
3797 path = alloc_path_for_send();
3801 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3802 root = sctx->send_root;
3803 cb = __record_new_ref;
3804 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3805 root = sctx->parent_root;
3806 cb = __record_deleted_ref;
3808 btrfs_err(sctx->send_root->fs_info,
3809 "Wrong command %d in process_all_refs", cmd);
3814 key.objectid = sctx->cmp_key->objectid;
3815 key.type = BTRFS_INODE_REF_KEY;
3817 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3822 eb = path->nodes[0];
3823 slot = path->slots[0];
3824 if (slot >= btrfs_header_nritems(eb)) {
3825 ret = btrfs_next_leaf(root, path);
3833 btrfs_item_key_to_cpu(eb, &found_key, slot);
3835 if (found_key.objectid != key.objectid ||
3836 (found_key.type != BTRFS_INODE_REF_KEY &&
3837 found_key.type != BTRFS_INODE_EXTREF_KEY))
3840 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
3846 btrfs_release_path(path);
3848 ret = process_recorded_refs(sctx, &pending_move);
3849 /* Only applicable to an incremental send. */
3850 ASSERT(pending_move == 0);
3853 btrfs_free_path(path);
3857 static int send_set_xattr(struct send_ctx *sctx,
3858 struct fs_path *path,
3859 const char *name, int name_len,
3860 const char *data, int data_len)
3864 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3868 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3869 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3870 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3872 ret = send_cmd(sctx);
3879 static int send_remove_xattr(struct send_ctx *sctx,
3880 struct fs_path *path,
3881 const char *name, int name_len)
3885 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3889 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3890 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3892 ret = send_cmd(sctx);
3899 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3900 const char *name, int name_len,
3901 const char *data, int data_len,
3905 struct send_ctx *sctx = ctx;
3907 posix_acl_xattr_header dummy_acl;
3909 p = fs_path_alloc();
3914 * This hack is needed because empty acl's are stored as zero byte
3915 * data in xattrs. Problem with that is, that receiving these zero byte
3916 * acl's will fail later. To fix this, we send a dummy acl list that
3917 * only contains the version number and no entries.
3919 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3920 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3921 if (data_len == 0) {
3922 dummy_acl.a_version =
3923 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3924 data = (char *)&dummy_acl;
3925 data_len = sizeof(dummy_acl);
3929 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3933 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3940 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3941 const char *name, int name_len,
3942 const char *data, int data_len,
3946 struct send_ctx *sctx = ctx;
3949 p = fs_path_alloc();
3953 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3957 ret = send_remove_xattr(sctx, p, name, name_len);
3964 static int process_new_xattr(struct send_ctx *sctx)
3968 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3969 sctx->cmp_key, __process_new_xattr, sctx);
3974 static int process_deleted_xattr(struct send_ctx *sctx)
3978 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3979 sctx->cmp_key, __process_deleted_xattr, sctx);
3984 struct find_xattr_ctx {
3992 static int __find_xattr(int num, struct btrfs_key *di_key,
3993 const char *name, int name_len,
3994 const char *data, int data_len,
3995 u8 type, void *vctx)
3997 struct find_xattr_ctx *ctx = vctx;
3999 if (name_len == ctx->name_len &&
4000 strncmp(name, ctx->name, name_len) == 0) {
4001 ctx->found_idx = num;
4002 ctx->found_data_len = data_len;
4003 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
4004 if (!ctx->found_data)
4011 static int find_xattr(struct btrfs_root *root,
4012 struct btrfs_path *path,
4013 struct btrfs_key *key,
4014 const char *name, int name_len,
4015 char **data, int *data_len)
4018 struct find_xattr_ctx ctx;
4021 ctx.name_len = name_len;
4023 ctx.found_data = NULL;
4024 ctx.found_data_len = 0;
4026 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
4030 if (ctx.found_idx == -1)
4033 *data = ctx.found_data;
4034 *data_len = ctx.found_data_len;
4036 kfree(ctx.found_data);
4038 return ctx.found_idx;
4042 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4043 const char *name, int name_len,
4044 const char *data, int data_len,
4048 struct send_ctx *sctx = ctx;
4049 char *found_data = NULL;
4050 int found_data_len = 0;
4052 ret = find_xattr(sctx->parent_root, sctx->right_path,
4053 sctx->cmp_key, name, name_len, &found_data,
4055 if (ret == -ENOENT) {
4056 ret = __process_new_xattr(num, di_key, name, name_len, data,
4057 data_len, type, ctx);
4058 } else if (ret >= 0) {
4059 if (data_len != found_data_len ||
4060 memcmp(data, found_data, data_len)) {
4061 ret = __process_new_xattr(num, di_key, name, name_len,
4062 data, data_len, type, ctx);
4072 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4073 const char *name, int name_len,
4074 const char *data, int data_len,
4078 struct send_ctx *sctx = ctx;
4080 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4081 name, name_len, NULL, NULL);
4083 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4084 data_len, type, ctx);
4091 static int process_changed_xattr(struct send_ctx *sctx)
4095 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4096 sctx->cmp_key, __process_changed_new_xattr, sctx);
4099 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4100 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4106 static int process_all_new_xattrs(struct send_ctx *sctx)
4109 struct btrfs_root *root;
4110 struct btrfs_path *path;
4111 struct btrfs_key key;
4112 struct btrfs_key found_key;
4113 struct extent_buffer *eb;
4116 path = alloc_path_for_send();
4120 root = sctx->send_root;
4122 key.objectid = sctx->cmp_key->objectid;
4123 key.type = BTRFS_XATTR_ITEM_KEY;
4125 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4130 eb = path->nodes[0];
4131 slot = path->slots[0];
4132 if (slot >= btrfs_header_nritems(eb)) {
4133 ret = btrfs_next_leaf(root, path);
4136 } else if (ret > 0) {
4143 btrfs_item_key_to_cpu(eb, &found_key, slot);
4144 if (found_key.objectid != key.objectid ||
4145 found_key.type != key.type) {
4150 ret = iterate_dir_item(root, path, &found_key,
4151 __process_new_xattr, sctx);
4159 btrfs_free_path(path);
4163 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4165 struct btrfs_root *root = sctx->send_root;
4166 struct btrfs_fs_info *fs_info = root->fs_info;
4167 struct inode *inode;
4170 struct btrfs_key key;
4171 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4173 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4176 key.objectid = sctx->cur_ino;
4177 key.type = BTRFS_INODE_ITEM_KEY;
4180 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4182 return PTR_ERR(inode);
4184 if (offset + len > i_size_read(inode)) {
4185 if (offset > i_size_read(inode))
4188 len = offset - i_size_read(inode);
4193 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
4194 while (index <= last_index) {
4195 unsigned cur_len = min_t(unsigned, len,
4196 PAGE_CACHE_SIZE - pg_offset);
4197 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4203 if (!PageUptodate(page)) {
4204 btrfs_readpage(NULL, page);
4206 if (!PageUptodate(page)) {
4208 page_cache_release(page);
4215 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4218 page_cache_release(page);
4230 * Read some bytes from the current inode/file and send a write command to
4233 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4237 ssize_t num_read = 0;
4239 p = fs_path_alloc();
4243 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4245 num_read = fill_read_buf(sctx, offset, len);
4246 if (num_read <= 0) {
4252 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4256 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4260 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4261 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4262 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
4264 ret = send_cmd(sctx);
4275 * Send a clone command to user space.
4277 static int send_clone(struct send_ctx *sctx,
4278 u64 offset, u32 len,
4279 struct clone_root *clone_root)
4285 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4286 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4287 clone_root->root->objectid, clone_root->ino,
4288 clone_root->offset);
4290 p = fs_path_alloc();
4294 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4298 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4302 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4303 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4304 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4306 if (clone_root->root == sctx->send_root) {
4307 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
4308 &gen, NULL, NULL, NULL, NULL);
4311 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4313 ret = get_inode_path(clone_root->root, clone_root->ino, p);
4318 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4319 clone_root->root->root_item.uuid);
4320 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
4321 le64_to_cpu(clone_root->root->root_item.ctransid));
4322 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4323 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4324 clone_root->offset);
4326 ret = send_cmd(sctx);
4335 * Send an update extent command to user space.
4337 static int send_update_extent(struct send_ctx *sctx,
4338 u64 offset, u32 len)
4343 p = fs_path_alloc();
4347 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4351 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4355 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4356 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4357 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4359 ret = send_cmd(sctx);
4367 static int send_hole(struct send_ctx *sctx, u64 end)
4369 struct fs_path *p = NULL;
4370 u64 offset = sctx->cur_inode_last_extent;
4374 p = fs_path_alloc();
4377 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4378 while (offset < end) {
4379 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4381 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4384 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4387 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4388 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4389 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4390 ret = send_cmd(sctx);
4400 static int send_write_or_clone(struct send_ctx *sctx,
4401 struct btrfs_path *path,
4402 struct btrfs_key *key,
4403 struct clone_root *clone_root)
4406 struct btrfs_file_extent_item *ei;
4407 u64 offset = key->offset;
4412 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
4414 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4415 struct btrfs_file_extent_item);
4416 type = btrfs_file_extent_type(path->nodes[0], ei);
4417 if (type == BTRFS_FILE_EXTENT_INLINE) {
4418 len = btrfs_file_extent_inline_len(path->nodes[0],
4419 path->slots[0], ei);
4421 * it is possible the inline item won't cover the whole page,
4422 * but there may be items after this page. Make
4423 * sure to send the whole thing
4425 len = PAGE_CACHE_ALIGN(len);
4427 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
4430 if (offset + len > sctx->cur_inode_size)
4431 len = sctx->cur_inode_size - offset;
4437 if (clone_root && IS_ALIGNED(offset + len, bs)) {
4438 ret = send_clone(sctx, offset, len, clone_root);
4439 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4440 ret = send_update_extent(sctx, offset, len);
4444 if (l > BTRFS_SEND_READ_SIZE)
4445 l = BTRFS_SEND_READ_SIZE;
4446 ret = send_write(sctx, pos + offset, l);
4459 static int is_extent_unchanged(struct send_ctx *sctx,
4460 struct btrfs_path *left_path,
4461 struct btrfs_key *ekey)
4464 struct btrfs_key key;
4465 struct btrfs_path *path = NULL;
4466 struct extent_buffer *eb;
4468 struct btrfs_key found_key;
4469 struct btrfs_file_extent_item *ei;
4474 u64 left_offset_fixed;
4482 path = alloc_path_for_send();
4486 eb = left_path->nodes[0];
4487 slot = left_path->slots[0];
4488 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4489 left_type = btrfs_file_extent_type(eb, ei);
4491 if (left_type != BTRFS_FILE_EXTENT_REG) {
4495 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4496 left_len = btrfs_file_extent_num_bytes(eb, ei);
4497 left_offset = btrfs_file_extent_offset(eb, ei);
4498 left_gen = btrfs_file_extent_generation(eb, ei);
4501 * Following comments will refer to these graphics. L is the left
4502 * extents which we are checking at the moment. 1-8 are the right
4503 * extents that we iterate.
4506 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4509 * |--1--|-2b-|...(same as above)
4511 * Alternative situation. Happens on files where extents got split.
4513 * |-----------7-----------|-6-|
4515 * Alternative situation. Happens on files which got larger.
4518 * Nothing follows after 8.
4521 key.objectid = ekey->objectid;
4522 key.type = BTRFS_EXTENT_DATA_KEY;
4523 key.offset = ekey->offset;
4524 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4533 * Handle special case where the right side has no extents at all.
4535 eb = path->nodes[0];
4536 slot = path->slots[0];
4537 btrfs_item_key_to_cpu(eb, &found_key, slot);
4538 if (found_key.objectid != key.objectid ||
4539 found_key.type != key.type) {
4540 /* If we're a hole then just pretend nothing changed */
4541 ret = (left_disknr) ? 0 : 1;
4546 * We're now on 2a, 2b or 7.
4549 while (key.offset < ekey->offset + left_len) {
4550 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4551 right_type = btrfs_file_extent_type(eb, ei);
4552 if (right_type != BTRFS_FILE_EXTENT_REG) {
4557 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4558 right_len = btrfs_file_extent_num_bytes(eb, ei);
4559 right_offset = btrfs_file_extent_offset(eb, ei);
4560 right_gen = btrfs_file_extent_generation(eb, ei);
4563 * Are we at extent 8? If yes, we know the extent is changed.
4564 * This may only happen on the first iteration.
4566 if (found_key.offset + right_len <= ekey->offset) {
4567 /* If we're a hole just pretend nothing changed */
4568 ret = (left_disknr) ? 0 : 1;
4572 left_offset_fixed = left_offset;
4573 if (key.offset < ekey->offset) {
4574 /* Fix the right offset for 2a and 7. */
4575 right_offset += ekey->offset - key.offset;
4577 /* Fix the left offset for all behind 2a and 2b */
4578 left_offset_fixed += key.offset - ekey->offset;
4582 * Check if we have the same extent.
4584 if (left_disknr != right_disknr ||
4585 left_offset_fixed != right_offset ||
4586 left_gen != right_gen) {
4592 * Go to the next extent.
4594 ret = btrfs_next_item(sctx->parent_root, path);
4598 eb = path->nodes[0];
4599 slot = path->slots[0];
4600 btrfs_item_key_to_cpu(eb, &found_key, slot);
4602 if (ret || found_key.objectid != key.objectid ||
4603 found_key.type != key.type) {
4604 key.offset += right_len;
4607 if (found_key.offset != key.offset + right_len) {
4615 * We're now behind the left extent (treat as unchanged) or at the end
4616 * of the right side (treat as changed).
4618 if (key.offset >= ekey->offset + left_len)
4625 btrfs_free_path(path);
4629 static int get_last_extent(struct send_ctx *sctx, u64 offset)
4631 struct btrfs_path *path;
4632 struct btrfs_root *root = sctx->send_root;
4633 struct btrfs_file_extent_item *fi;
4634 struct btrfs_key key;
4639 path = alloc_path_for_send();
4643 sctx->cur_inode_last_extent = 0;
4645 key.objectid = sctx->cur_ino;
4646 key.type = BTRFS_EXTENT_DATA_KEY;
4647 key.offset = offset;
4648 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4652 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4653 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4656 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4657 struct btrfs_file_extent_item);
4658 type = btrfs_file_extent_type(path->nodes[0], fi);
4659 if (type == BTRFS_FILE_EXTENT_INLINE) {
4660 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4661 path->slots[0], fi);
4662 extent_end = ALIGN(key.offset + size,
4663 sctx->send_root->sectorsize);
4665 extent_end = key.offset +
4666 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4668 sctx->cur_inode_last_extent = extent_end;
4670 btrfs_free_path(path);
4674 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4675 struct btrfs_key *key)
4677 struct btrfs_file_extent_item *fi;
4682 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4685 if (sctx->cur_inode_last_extent == (u64)-1) {
4686 ret = get_last_extent(sctx, key->offset - 1);
4691 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4692 struct btrfs_file_extent_item);
4693 type = btrfs_file_extent_type(path->nodes[0], fi);
4694 if (type == BTRFS_FILE_EXTENT_INLINE) {
4695 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4696 path->slots[0], fi);
4697 extent_end = ALIGN(key->offset + size,
4698 sctx->send_root->sectorsize);
4700 extent_end = key->offset +
4701 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4704 if (path->slots[0] == 0 &&
4705 sctx->cur_inode_last_extent < key->offset) {
4707 * We might have skipped entire leafs that contained only
4708 * file extent items for our current inode. These leafs have
4709 * a generation number smaller (older) than the one in the
4710 * current leaf and the leaf our last extent came from, and
4711 * are located between these 2 leafs.
4713 ret = get_last_extent(sctx, key->offset - 1);
4718 if (sctx->cur_inode_last_extent < key->offset)
4719 ret = send_hole(sctx, key->offset);
4720 sctx->cur_inode_last_extent = extent_end;
4724 static int process_extent(struct send_ctx *sctx,
4725 struct btrfs_path *path,
4726 struct btrfs_key *key)
4728 struct clone_root *found_clone = NULL;
4731 if (S_ISLNK(sctx->cur_inode_mode))
4734 if (sctx->parent_root && !sctx->cur_inode_new) {
4735 ret = is_extent_unchanged(sctx, path, key);
4743 struct btrfs_file_extent_item *ei;
4746 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4747 struct btrfs_file_extent_item);
4748 type = btrfs_file_extent_type(path->nodes[0], ei);
4749 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4750 type == BTRFS_FILE_EXTENT_REG) {
4752 * The send spec does not have a prealloc command yet,
4753 * so just leave a hole for prealloc'ed extents until
4754 * we have enough commands queued up to justify rev'ing
4757 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4762 /* Have a hole, just skip it. */
4763 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4770 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4771 sctx->cur_inode_size, &found_clone);
4772 if (ret != -ENOENT && ret < 0)
4775 ret = send_write_or_clone(sctx, path, key, found_clone);
4779 ret = maybe_send_hole(sctx, path, key);
4784 static int process_all_extents(struct send_ctx *sctx)
4787 struct btrfs_root *root;
4788 struct btrfs_path *path;
4789 struct btrfs_key key;
4790 struct btrfs_key found_key;
4791 struct extent_buffer *eb;
4794 root = sctx->send_root;
4795 path = alloc_path_for_send();
4799 key.objectid = sctx->cmp_key->objectid;
4800 key.type = BTRFS_EXTENT_DATA_KEY;
4802 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4807 eb = path->nodes[0];
4808 slot = path->slots[0];
4810 if (slot >= btrfs_header_nritems(eb)) {
4811 ret = btrfs_next_leaf(root, path);
4814 } else if (ret > 0) {
4821 btrfs_item_key_to_cpu(eb, &found_key, slot);
4823 if (found_key.objectid != key.objectid ||
4824 found_key.type != key.type) {
4829 ret = process_extent(sctx, path, &found_key);
4837 btrfs_free_path(path);
4841 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4843 int *refs_processed)
4847 if (sctx->cur_ino == 0)
4849 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4850 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4852 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4855 ret = process_recorded_refs(sctx, pending_move);
4859 *refs_processed = 1;
4864 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4875 int pending_move = 0;
4876 int refs_processed = 0;
4878 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4884 * We have processed the refs and thus need to advance send_progress.
4885 * Now, calls to get_cur_xxx will take the updated refs of the current
4886 * inode into account.
4888 * On the other hand, if our current inode is a directory and couldn't
4889 * be moved/renamed because its parent was renamed/moved too and it has
4890 * a higher inode number, we can only move/rename our current inode
4891 * after we moved/renamed its parent. Therefore in this case operate on
4892 * the old path (pre move/rename) of our current inode, and the
4893 * move/rename will be performed later.
4895 if (refs_processed && !pending_move)
4896 sctx->send_progress = sctx->cur_ino + 1;
4898 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4900 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4903 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4904 &left_mode, &left_uid, &left_gid, NULL);
4908 if (!sctx->parent_root || sctx->cur_inode_new) {
4910 if (!S_ISLNK(sctx->cur_inode_mode))
4913 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4914 NULL, NULL, &right_mode, &right_uid,
4919 if (left_uid != right_uid || left_gid != right_gid)
4921 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4925 if (S_ISREG(sctx->cur_inode_mode)) {
4926 if (need_send_hole(sctx)) {
4927 if (sctx->cur_inode_last_extent == (u64)-1) {
4928 ret = get_last_extent(sctx, (u64)-1);
4932 if (sctx->cur_inode_last_extent <
4933 sctx->cur_inode_size) {
4934 ret = send_hole(sctx, sctx->cur_inode_size);
4939 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4940 sctx->cur_inode_size);
4946 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4947 left_uid, left_gid);
4952 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4959 * If other directory inodes depended on our current directory
4960 * inode's move/rename, now do their move/rename operations.
4962 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
4963 ret = apply_children_dir_moves(sctx);
4969 * Need to send that every time, no matter if it actually
4970 * changed between the two trees as we have done changes to
4973 sctx->send_progress = sctx->cur_ino + 1;
4974 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4982 static int changed_inode(struct send_ctx *sctx,
4983 enum btrfs_compare_tree_result result)
4986 struct btrfs_key *key = sctx->cmp_key;
4987 struct btrfs_inode_item *left_ii = NULL;
4988 struct btrfs_inode_item *right_ii = NULL;
4992 sctx->cur_ino = key->objectid;
4993 sctx->cur_inode_new_gen = 0;
4994 sctx->cur_inode_last_extent = (u64)-1;
4997 * Set send_progress to current inode. This will tell all get_cur_xxx
4998 * functions that the current inode's refs are not updated yet. Later,
4999 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5001 sctx->send_progress = sctx->cur_ino;
5003 if (result == BTRFS_COMPARE_TREE_NEW ||
5004 result == BTRFS_COMPARE_TREE_CHANGED) {
5005 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5006 sctx->left_path->slots[0],
5007 struct btrfs_inode_item);
5008 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5011 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5012 sctx->right_path->slots[0],
5013 struct btrfs_inode_item);
5014 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5017 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5018 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5019 sctx->right_path->slots[0],
5020 struct btrfs_inode_item);
5022 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5026 * The cur_ino = root dir case is special here. We can't treat
5027 * the inode as deleted+reused because it would generate a
5028 * stream that tries to delete/mkdir the root dir.
5030 if (left_gen != right_gen &&
5031 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5032 sctx->cur_inode_new_gen = 1;
5035 if (result == BTRFS_COMPARE_TREE_NEW) {
5036 sctx->cur_inode_gen = left_gen;
5037 sctx->cur_inode_new = 1;
5038 sctx->cur_inode_deleted = 0;
5039 sctx->cur_inode_size = btrfs_inode_size(
5040 sctx->left_path->nodes[0], left_ii);
5041 sctx->cur_inode_mode = btrfs_inode_mode(
5042 sctx->left_path->nodes[0], left_ii);
5043 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5044 ret = send_create_inode_if_needed(sctx);
5045 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5046 sctx->cur_inode_gen = right_gen;
5047 sctx->cur_inode_new = 0;
5048 sctx->cur_inode_deleted = 1;
5049 sctx->cur_inode_size = btrfs_inode_size(
5050 sctx->right_path->nodes[0], right_ii);
5051 sctx->cur_inode_mode = btrfs_inode_mode(
5052 sctx->right_path->nodes[0], right_ii);
5053 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
5055 * We need to do some special handling in case the inode was
5056 * reported as changed with a changed generation number. This
5057 * means that the original inode was deleted and new inode
5058 * reused the same inum. So we have to treat the old inode as
5059 * deleted and the new one as new.
5061 if (sctx->cur_inode_new_gen) {
5063 * First, process the inode as if it was deleted.
5065 sctx->cur_inode_gen = right_gen;
5066 sctx->cur_inode_new = 0;
5067 sctx->cur_inode_deleted = 1;
5068 sctx->cur_inode_size = btrfs_inode_size(
5069 sctx->right_path->nodes[0], right_ii);
5070 sctx->cur_inode_mode = btrfs_inode_mode(
5071 sctx->right_path->nodes[0], right_ii);
5072 ret = process_all_refs(sctx,
5073 BTRFS_COMPARE_TREE_DELETED);
5078 * Now process the inode as if it was new.
5080 sctx->cur_inode_gen = left_gen;
5081 sctx->cur_inode_new = 1;
5082 sctx->cur_inode_deleted = 0;
5083 sctx->cur_inode_size = btrfs_inode_size(
5084 sctx->left_path->nodes[0], left_ii);
5085 sctx->cur_inode_mode = btrfs_inode_mode(
5086 sctx->left_path->nodes[0], left_ii);
5087 ret = send_create_inode_if_needed(sctx);
5091 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5095 * Advance send_progress now as we did not get into
5096 * process_recorded_refs_if_needed in the new_gen case.
5098 sctx->send_progress = sctx->cur_ino + 1;
5101 * Now process all extents and xattrs of the inode as if
5102 * they were all new.
5104 ret = process_all_extents(sctx);
5107 ret = process_all_new_xattrs(sctx);
5111 sctx->cur_inode_gen = left_gen;
5112 sctx->cur_inode_new = 0;
5113 sctx->cur_inode_new_gen = 0;
5114 sctx->cur_inode_deleted = 0;
5115 sctx->cur_inode_size = btrfs_inode_size(
5116 sctx->left_path->nodes[0], left_ii);
5117 sctx->cur_inode_mode = btrfs_inode_mode(
5118 sctx->left_path->nodes[0], left_ii);
5127 * We have to process new refs before deleted refs, but compare_trees gives us
5128 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5129 * first and later process them in process_recorded_refs.
5130 * For the cur_inode_new_gen case, we skip recording completely because
5131 * changed_inode did already initiate processing of refs. The reason for this is
5132 * that in this case, compare_tree actually compares the refs of 2 different
5133 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5134 * refs of the right tree as deleted and all refs of the left tree as new.
5136 static int changed_ref(struct send_ctx *sctx,
5137 enum btrfs_compare_tree_result result)
5141 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5143 if (!sctx->cur_inode_new_gen &&
5144 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5145 if (result == BTRFS_COMPARE_TREE_NEW)
5146 ret = record_new_ref(sctx);
5147 else if (result == BTRFS_COMPARE_TREE_DELETED)
5148 ret = record_deleted_ref(sctx);
5149 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5150 ret = record_changed_ref(sctx);
5157 * Process new/deleted/changed xattrs. We skip processing in the
5158 * cur_inode_new_gen case because changed_inode did already initiate processing
5159 * of xattrs. The reason is the same as in changed_ref
5161 static int changed_xattr(struct send_ctx *sctx,
5162 enum btrfs_compare_tree_result result)
5166 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5168 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5169 if (result == BTRFS_COMPARE_TREE_NEW)
5170 ret = process_new_xattr(sctx);
5171 else if (result == BTRFS_COMPARE_TREE_DELETED)
5172 ret = process_deleted_xattr(sctx);
5173 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5174 ret = process_changed_xattr(sctx);
5181 * Process new/deleted/changed extents. We skip processing in the
5182 * cur_inode_new_gen case because changed_inode did already initiate processing
5183 * of extents. The reason is the same as in changed_ref
5185 static int changed_extent(struct send_ctx *sctx,
5186 enum btrfs_compare_tree_result result)
5190 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5192 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5193 if (result != BTRFS_COMPARE_TREE_DELETED)
5194 ret = process_extent(sctx, sctx->left_path,
5201 static int dir_changed(struct send_ctx *sctx, u64 dir)
5203 u64 orig_gen, new_gen;
5206 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5211 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5216 return (orig_gen != new_gen) ? 1 : 0;
5219 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5220 struct btrfs_key *key)
5222 struct btrfs_inode_extref *extref;
5223 struct extent_buffer *leaf;
5224 u64 dirid = 0, last_dirid = 0;
5231 /* Easy case, just check this one dirid */
5232 if (key->type == BTRFS_INODE_REF_KEY) {
5233 dirid = key->offset;
5235 ret = dir_changed(sctx, dirid);
5239 leaf = path->nodes[0];
5240 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5241 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5242 while (cur_offset < item_size) {
5243 extref = (struct btrfs_inode_extref *)(ptr +
5245 dirid = btrfs_inode_extref_parent(leaf, extref);
5246 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5247 cur_offset += ref_name_len + sizeof(*extref);
5248 if (dirid == last_dirid)
5250 ret = dir_changed(sctx, dirid);
5260 * Updates compare related fields in sctx and simply forwards to the actual
5261 * changed_xxx functions.
5263 static int changed_cb(struct btrfs_root *left_root,
5264 struct btrfs_root *right_root,
5265 struct btrfs_path *left_path,
5266 struct btrfs_path *right_path,
5267 struct btrfs_key *key,
5268 enum btrfs_compare_tree_result result,
5272 struct send_ctx *sctx = ctx;
5274 if (result == BTRFS_COMPARE_TREE_SAME) {
5275 if (key->type == BTRFS_INODE_REF_KEY ||
5276 key->type == BTRFS_INODE_EXTREF_KEY) {
5277 ret = compare_refs(sctx, left_path, key);
5282 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5283 return maybe_send_hole(sctx, left_path, key);
5287 result = BTRFS_COMPARE_TREE_CHANGED;
5291 sctx->left_path = left_path;
5292 sctx->right_path = right_path;
5293 sctx->cmp_key = key;
5295 ret = finish_inode_if_needed(sctx, 0);
5299 /* Ignore non-FS objects */
5300 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5301 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5304 if (key->type == BTRFS_INODE_ITEM_KEY)
5305 ret = changed_inode(sctx, result);
5306 else if (key->type == BTRFS_INODE_REF_KEY ||
5307 key->type == BTRFS_INODE_EXTREF_KEY)
5308 ret = changed_ref(sctx, result);
5309 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5310 ret = changed_xattr(sctx, result);
5311 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5312 ret = changed_extent(sctx, result);
5318 static int full_send_tree(struct send_ctx *sctx)
5321 struct btrfs_trans_handle *trans = NULL;
5322 struct btrfs_root *send_root = sctx->send_root;
5323 struct btrfs_key key;
5324 struct btrfs_key found_key;
5325 struct btrfs_path *path;
5326 struct extent_buffer *eb;
5331 path = alloc_path_for_send();
5335 spin_lock(&send_root->root_item_lock);
5336 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
5337 spin_unlock(&send_root->root_item_lock);
5339 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5340 key.type = BTRFS_INODE_ITEM_KEY;
5345 * We need to make sure the transaction does not get committed
5346 * while we do anything on commit roots. Join a transaction to prevent
5349 trans = btrfs_join_transaction(send_root);
5350 if (IS_ERR(trans)) {
5351 ret = PTR_ERR(trans);
5357 * Make sure the tree has not changed after re-joining. We detect this
5358 * by comparing start_ctransid and ctransid. They should always match.
5360 spin_lock(&send_root->root_item_lock);
5361 ctransid = btrfs_root_ctransid(&send_root->root_item);
5362 spin_unlock(&send_root->root_item_lock);
5364 if (ctransid != start_ctransid) {
5365 WARN(1, KERN_WARNING "BTRFS: the root that you're trying to "
5366 "send was modified in between. This is "
5367 "probably a bug.\n");
5372 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5380 * When someone want to commit while we iterate, end the
5381 * joined transaction and rejoin.
5383 if (btrfs_should_end_transaction(trans, send_root)) {
5384 ret = btrfs_end_transaction(trans, send_root);
5388 btrfs_release_path(path);
5392 eb = path->nodes[0];
5393 slot = path->slots[0];
5394 btrfs_item_key_to_cpu(eb, &found_key, slot);
5396 ret = changed_cb(send_root, NULL, path, NULL,
5397 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5401 key.objectid = found_key.objectid;
5402 key.type = found_key.type;
5403 key.offset = found_key.offset + 1;
5405 ret = btrfs_next_item(send_root, path);
5415 ret = finish_inode_if_needed(sctx, 1);
5418 btrfs_free_path(path);
5421 ret = btrfs_end_transaction(trans, send_root);
5423 btrfs_end_transaction(trans, send_root);
5428 static int send_subvol(struct send_ctx *sctx)
5432 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5433 ret = send_header(sctx);
5438 ret = send_subvol_begin(sctx);
5442 if (sctx->parent_root) {
5443 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5447 ret = finish_inode_if_needed(sctx, 1);
5451 ret = full_send_tree(sctx);
5457 free_recorded_refs(sctx);
5461 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5463 spin_lock(&root->root_item_lock);
5464 root->send_in_progress--;
5466 * Not much left to do, we don't know why it's unbalanced and
5467 * can't blindly reset it to 0.
5469 if (root->send_in_progress < 0)
5470 btrfs_err(root->fs_info,
5471 "send_in_progres unbalanced %d root %llu\n",
5472 root->send_in_progress, root->root_key.objectid);
5473 spin_unlock(&root->root_item_lock);
5476 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5479 struct btrfs_root *send_root;
5480 struct btrfs_root *clone_root;
5481 struct btrfs_fs_info *fs_info;
5482 struct btrfs_ioctl_send_args *arg = NULL;
5483 struct btrfs_key key;
5484 struct send_ctx *sctx = NULL;
5486 u64 *clone_sources_tmp = NULL;
5487 int clone_sources_to_rollback = 0;
5488 int sort_clone_roots = 0;
5491 if (!capable(CAP_SYS_ADMIN))
5494 send_root = BTRFS_I(file_inode(mnt_file))->root;
5495 fs_info = send_root->fs_info;
5498 * The subvolume must remain read-only during send, protect against
5501 spin_lock(&send_root->root_item_lock);
5502 send_root->send_in_progress++;
5503 spin_unlock(&send_root->root_item_lock);
5506 * This is done when we lookup the root, it should already be complete
5507 * by the time we get here.
5509 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5512 * Userspace tools do the checks and warn the user if it's
5515 if (!btrfs_root_readonly(send_root)) {
5520 arg = memdup_user(arg_, sizeof(*arg));
5527 if (!access_ok(VERIFY_READ, arg->clone_sources,
5528 sizeof(*arg->clone_sources) *
5529 arg->clone_sources_count)) {
5534 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
5539 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5545 INIT_LIST_HEAD(&sctx->new_refs);
5546 INIT_LIST_HEAD(&sctx->deleted_refs);
5547 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5548 INIT_LIST_HEAD(&sctx->name_cache_list);
5550 sctx->flags = arg->flags;
5552 sctx->send_filp = fget(arg->send_fd);
5553 if (!sctx->send_filp) {
5558 sctx->send_root = send_root;
5559 sctx->clone_roots_cnt = arg->clone_sources_count;
5561 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5562 sctx->send_buf = vmalloc(sctx->send_max_size);
5563 if (!sctx->send_buf) {
5568 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5569 if (!sctx->read_buf) {
5574 sctx->pending_dir_moves = RB_ROOT;
5575 sctx->waiting_dir_moves = RB_ROOT;
5576 sctx->orphan_dirs = RB_ROOT;
5578 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5579 (arg->clone_sources_count + 1));
5580 if (!sctx->clone_roots) {
5585 if (arg->clone_sources_count) {
5586 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5587 sizeof(*arg->clone_sources));
5588 if (!clone_sources_tmp) {
5593 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5594 arg->clone_sources_count *
5595 sizeof(*arg->clone_sources));
5601 for (i = 0; i < arg->clone_sources_count; i++) {
5602 key.objectid = clone_sources_tmp[i];
5603 key.type = BTRFS_ROOT_ITEM_KEY;
5604 key.offset = (u64)-1;
5606 index = srcu_read_lock(&fs_info->subvol_srcu);
5608 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
5609 if (IS_ERR(clone_root)) {
5610 srcu_read_unlock(&fs_info->subvol_srcu, index);
5611 ret = PTR_ERR(clone_root);
5614 clone_sources_to_rollback = i + 1;
5615 spin_lock(&clone_root->root_item_lock);
5616 clone_root->send_in_progress++;
5617 if (!btrfs_root_readonly(clone_root)) {
5618 spin_unlock(&clone_root->root_item_lock);
5619 srcu_read_unlock(&fs_info->subvol_srcu, index);
5623 spin_unlock(&clone_root->root_item_lock);
5624 srcu_read_unlock(&fs_info->subvol_srcu, index);
5626 sctx->clone_roots[i].root = clone_root;
5628 vfree(clone_sources_tmp);
5629 clone_sources_tmp = NULL;
5632 if (arg->parent_root) {
5633 key.objectid = arg->parent_root;
5634 key.type = BTRFS_ROOT_ITEM_KEY;
5635 key.offset = (u64)-1;
5637 index = srcu_read_lock(&fs_info->subvol_srcu);
5639 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
5640 if (IS_ERR(sctx->parent_root)) {
5641 srcu_read_unlock(&fs_info->subvol_srcu, index);
5642 ret = PTR_ERR(sctx->parent_root);
5646 spin_lock(&sctx->parent_root->root_item_lock);
5647 sctx->parent_root->send_in_progress++;
5648 if (!btrfs_root_readonly(sctx->parent_root)) {
5649 spin_unlock(&sctx->parent_root->root_item_lock);
5650 srcu_read_unlock(&fs_info->subvol_srcu, index);
5654 spin_unlock(&sctx->parent_root->root_item_lock);
5656 srcu_read_unlock(&fs_info->subvol_srcu, index);
5660 * Clones from send_root are allowed, but only if the clone source
5661 * is behind the current send position. This is checked while searching
5662 * for possible clone sources.
5664 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5666 /* We do a bsearch later */
5667 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5668 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5670 sort_clone_roots = 1;
5672 ret = send_subvol(sctx);
5676 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5677 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5680 ret = send_cmd(sctx);
5686 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5687 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5689 struct pending_dir_move *pm;
5691 n = rb_first(&sctx->pending_dir_moves);
5692 pm = rb_entry(n, struct pending_dir_move, node);
5693 while (!list_empty(&pm->list)) {
5694 struct pending_dir_move *pm2;
5696 pm2 = list_first_entry(&pm->list,
5697 struct pending_dir_move, list);
5698 free_pending_move(sctx, pm2);
5700 free_pending_move(sctx, pm);
5703 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5704 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5706 struct waiting_dir_move *dm;
5708 n = rb_first(&sctx->waiting_dir_moves);
5709 dm = rb_entry(n, struct waiting_dir_move, node);
5710 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5714 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5715 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5717 struct orphan_dir_info *odi;
5719 n = rb_first(&sctx->orphan_dirs);
5720 odi = rb_entry(n, struct orphan_dir_info, node);
5721 free_orphan_dir_info(sctx, odi);
5724 if (sort_clone_roots) {
5725 for (i = 0; i < sctx->clone_roots_cnt; i++)
5726 btrfs_root_dec_send_in_progress(
5727 sctx->clone_roots[i].root);
5729 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5730 btrfs_root_dec_send_in_progress(
5731 sctx->clone_roots[i].root);
5733 btrfs_root_dec_send_in_progress(send_root);
5735 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5736 btrfs_root_dec_send_in_progress(sctx->parent_root);
5739 vfree(clone_sources_tmp);
5742 if (sctx->send_filp)
5743 fput(sctx->send_filp);
5745 vfree(sctx->clone_roots);
5746 vfree(sctx->send_buf);
5747 vfree(sctx->read_buf);
5749 name_cache_free(sctx);