2 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/bsearch.h>
21 #include <linux/file.h>
22 #include <linux/sort.h>
23 #include <linux/mount.h>
24 #include <linux/xattr.h>
25 #include <linux/posix_acl_xattr.h>
26 #include <linux/radix-tree.h>
27 #include <linux/vmalloc.h>
28 #include <linux/string.h>
35 #include "btrfs_inode.h"
36 #include "transaction.h"
38 static int g_verbose = 0;
40 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
43 * A fs_path is a helper to dynamically build path names with unknown size.
44 * It reallocates the internal buffer on demand.
45 * It allows fast adding of path elements on the right side (normal path) and
46 * fast adding to the left side (reversed path). A reversed path can also be
47 * unreversed if needed.
56 unsigned short buf_len:15;
57 unsigned short reversed:1;
61 * Average path length does not exceed 200 bytes, we'll have
62 * better packing in the slab and higher chance to satisfy
63 * a allocation later during send.
68 #define FS_PATH_INLINE_SIZE \
69 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
72 /* reused for each extent */
74 struct btrfs_root *root;
81 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
82 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
85 struct file *send_filp;
91 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
92 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
94 struct btrfs_root *send_root;
95 struct btrfs_root *parent_root;
96 struct clone_root *clone_roots;
99 /* current state of the compare_tree call */
100 struct btrfs_path *left_path;
101 struct btrfs_path *right_path;
102 struct btrfs_key *cmp_key;
105 * infos of the currently processed inode. In case of deleted inodes,
106 * these are the values from the deleted inode.
111 int cur_inode_new_gen;
112 int cur_inode_deleted;
116 u64 cur_inode_last_extent;
120 struct list_head new_refs;
121 struct list_head deleted_refs;
123 struct radix_tree_root name_cache;
124 struct list_head name_cache_list;
127 struct file_ra_state ra;
132 * We process inodes by their increasing order, so if before an
133 * incremental send we reverse the parent/child relationship of
134 * directories such that a directory with a lower inode number was
135 * the parent of a directory with a higher inode number, and the one
136 * becoming the new parent got renamed too, we can't rename/move the
137 * directory with lower inode number when we finish processing it - we
138 * must process the directory with higher inode number first, then
139 * rename/move it and then rename/move the directory with lower inode
140 * number. Example follows.
142 * Tree state when the first send was performed:
154 * Tree state when the second (incremental) send is performed:
163 * The sequence of steps that lead to the second state was:
165 * mv /a/b/c/d /a/b/c2/d2
166 * mv /a/b/c /a/b/c2/d2/cc
168 * "c" has lower inode number, but we can't move it (2nd mv operation)
169 * before we move "d", which has higher inode number.
171 * So we just memorize which move/rename operations must be performed
172 * later when their respective parent is processed and moved/renamed.
175 /* Indexed by parent directory inode number. */
176 struct rb_root pending_dir_moves;
179 * Reverse index, indexed by the inode number of a directory that
180 * is waiting for the move/rename of its immediate parent before its
181 * own move/rename can be performed.
183 struct rb_root waiting_dir_moves;
186 * A directory that is going to be rm'ed might have a child directory
187 * which is in the pending directory moves index above. In this case,
188 * the directory can only be removed after the move/rename of its child
189 * is performed. Example:
209 * Sequence of steps that lead to the send snapshot:
210 * rm -f /a/b/c/foo.txt
212 * mv /a/b/c/x /a/b/YY
215 * When the child is processed, its move/rename is delayed until its
216 * parent is processed (as explained above), but all other operations
217 * like update utimes, chown, chgrp, etc, are performed and the paths
218 * that it uses for those operations must use the orphanized name of
219 * its parent (the directory we're going to rm later), so we need to
220 * memorize that name.
222 * Indexed by the inode number of the directory to be deleted.
224 struct rb_root orphan_dirs;
227 struct pending_dir_move {
229 struct list_head list;
233 struct list_head update_refs;
236 struct waiting_dir_move {
240 * There might be some directory that could not be removed because it
241 * was waiting for this directory inode to be moved first. Therefore
242 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
247 struct orphan_dir_info {
253 struct name_cache_entry {
254 struct list_head list;
256 * radix_tree has only 32bit entries but we need to handle 64bit inums.
257 * We use the lower 32bit of the 64bit inum to store it in the tree. If
258 * more then one inum would fall into the same entry, we use radix_list
259 * to store the additional entries. radix_list is also used to store
260 * entries where two entries have the same inum but different
263 struct list_head radix_list;
269 int need_later_update;
274 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
276 static struct waiting_dir_move *
277 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
279 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
281 static int need_send_hole(struct send_ctx *sctx)
283 return (sctx->parent_root && !sctx->cur_inode_new &&
284 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
285 S_ISREG(sctx->cur_inode_mode));
288 static void fs_path_reset(struct fs_path *p)
291 p->start = p->buf + p->buf_len - 1;
301 static struct fs_path *fs_path_alloc(void)
305 p = kmalloc(sizeof(*p), GFP_NOFS);
309 p->buf = p->inline_buf;
310 p->buf_len = FS_PATH_INLINE_SIZE;
315 static struct fs_path *fs_path_alloc_reversed(void)
327 static void fs_path_free(struct fs_path *p)
331 if (p->buf != p->inline_buf)
336 static int fs_path_len(struct fs_path *p)
338 return p->end - p->start;
341 static int fs_path_ensure_buf(struct fs_path *p, int len)
349 if (p->buf_len >= len)
352 if (len > PATH_MAX) {
357 path_len = p->end - p->start;
358 old_buf_len = p->buf_len;
361 * First time the inline_buf does not suffice
363 if (p->buf == p->inline_buf) {
364 tmp_buf = kmalloc(len, GFP_NOFS);
366 memcpy(tmp_buf, p->buf, old_buf_len);
368 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
374 * The real size of the buffer is bigger, this will let the fast path
375 * happen most of the time
377 p->buf_len = ksize(p->buf);
380 tmp_buf = p->buf + old_buf_len - path_len - 1;
381 p->end = p->buf + p->buf_len - 1;
382 p->start = p->end - path_len;
383 memmove(p->start, tmp_buf, path_len + 1);
386 p->end = p->start + path_len;
391 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
397 new_len = p->end - p->start + name_len;
398 if (p->start != p->end)
400 ret = fs_path_ensure_buf(p, new_len);
405 if (p->start != p->end)
407 p->start -= name_len;
408 *prepared = p->start;
410 if (p->start != p->end)
421 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
426 ret = fs_path_prepare_for_add(p, name_len, &prepared);
429 memcpy(prepared, name, name_len);
435 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
440 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
443 memcpy(prepared, p2->start, p2->end - p2->start);
449 static int fs_path_add_from_extent_buffer(struct fs_path *p,
450 struct extent_buffer *eb,
451 unsigned long off, int len)
456 ret = fs_path_prepare_for_add(p, len, &prepared);
460 read_extent_buffer(eb, prepared, off, len);
466 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
470 p->reversed = from->reversed;
473 ret = fs_path_add_path(p, from);
479 static void fs_path_unreverse(struct fs_path *p)
488 len = p->end - p->start;
490 p->end = p->start + len;
491 memmove(p->start, tmp, len + 1);
495 static struct btrfs_path *alloc_path_for_send(void)
497 struct btrfs_path *path;
499 path = btrfs_alloc_path();
502 path->search_commit_root = 1;
503 path->skip_locking = 1;
504 path->need_commit_sem = 1;
508 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
518 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
519 /* TODO handle that correctly */
520 /*if (ret == -ERESTARTSYS) {
539 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
541 struct btrfs_tlv_header *hdr;
542 int total_len = sizeof(*hdr) + len;
543 int left = sctx->send_max_size - sctx->send_size;
545 if (unlikely(left < total_len))
548 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
549 hdr->tlv_type = cpu_to_le16(attr);
550 hdr->tlv_len = cpu_to_le16(len);
551 memcpy(hdr + 1, data, len);
552 sctx->send_size += total_len;
557 #define TLV_PUT_DEFINE_INT(bits) \
558 static int tlv_put_u##bits(struct send_ctx *sctx, \
559 u##bits attr, u##bits value) \
561 __le##bits __tmp = cpu_to_le##bits(value); \
562 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
565 TLV_PUT_DEFINE_INT(64)
567 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
568 const char *str, int len)
572 return tlv_put(sctx, attr, str, len);
575 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
578 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
581 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
582 struct extent_buffer *eb,
583 struct btrfs_timespec *ts)
585 struct btrfs_timespec bts;
586 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
587 return tlv_put(sctx, attr, &bts, sizeof(bts));
591 #define TLV_PUT(sctx, attrtype, attrlen, data) \
593 ret = tlv_put(sctx, attrtype, attrlen, data); \
595 goto tlv_put_failure; \
598 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
600 ret = tlv_put_u##bits(sctx, attrtype, value); \
602 goto tlv_put_failure; \
605 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
606 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
607 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
608 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
609 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
611 ret = tlv_put_string(sctx, attrtype, str, len); \
613 goto tlv_put_failure; \
615 #define TLV_PUT_PATH(sctx, attrtype, p) \
617 ret = tlv_put_string(sctx, attrtype, p->start, \
618 p->end - p->start); \
620 goto tlv_put_failure; \
622 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
624 ret = tlv_put_uuid(sctx, attrtype, uuid); \
626 goto tlv_put_failure; \
628 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
630 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
632 goto tlv_put_failure; \
635 static int send_header(struct send_ctx *sctx)
637 struct btrfs_stream_header hdr;
639 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
640 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
642 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
647 * For each command/item we want to send to userspace, we call this function.
649 static int begin_cmd(struct send_ctx *sctx, int cmd)
651 struct btrfs_cmd_header *hdr;
653 if (WARN_ON(!sctx->send_buf))
656 BUG_ON(sctx->send_size);
658 sctx->send_size += sizeof(*hdr);
659 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
660 hdr->cmd = cpu_to_le16(cmd);
665 static int send_cmd(struct send_ctx *sctx)
668 struct btrfs_cmd_header *hdr;
671 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
672 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
675 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
676 hdr->crc = cpu_to_le32(crc);
678 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
681 sctx->total_send_size += sctx->send_size;
682 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
689 * Sends a move instruction to user space
691 static int send_rename(struct send_ctx *sctx,
692 struct fs_path *from, struct fs_path *to)
696 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
698 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
702 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
703 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
705 ret = send_cmd(sctx);
713 * Sends a link instruction to user space
715 static int send_link(struct send_ctx *sctx,
716 struct fs_path *path, struct fs_path *lnk)
720 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
722 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
726 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
727 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
729 ret = send_cmd(sctx);
737 * Sends an unlink instruction to user space
739 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
743 verbose_printk("btrfs: send_unlink %s\n", path->start);
745 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
749 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
751 ret = send_cmd(sctx);
759 * Sends a rmdir instruction to user space
761 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
765 verbose_printk("btrfs: send_rmdir %s\n", path->start);
767 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
771 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
773 ret = send_cmd(sctx);
781 * Helper function to retrieve some fields from an inode item.
783 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
784 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
788 struct btrfs_inode_item *ii;
789 struct btrfs_key key;
792 key.type = BTRFS_INODE_ITEM_KEY;
794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
801 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
802 struct btrfs_inode_item);
804 *size = btrfs_inode_size(path->nodes[0], ii);
806 *gen = btrfs_inode_generation(path->nodes[0], ii);
808 *mode = btrfs_inode_mode(path->nodes[0], ii);
810 *uid = btrfs_inode_uid(path->nodes[0], ii);
812 *gid = btrfs_inode_gid(path->nodes[0], ii);
814 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
819 static int get_inode_info(struct btrfs_root *root,
820 u64 ino, u64 *size, u64 *gen,
821 u64 *mode, u64 *uid, u64 *gid,
824 struct btrfs_path *path;
827 path = alloc_path_for_send();
830 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
832 btrfs_free_path(path);
836 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
841 * Helper function to iterate the entries in ONE btrfs_inode_ref or
842 * btrfs_inode_extref.
843 * The iterate callback may return a non zero value to stop iteration. This can
844 * be a negative value for error codes or 1 to simply stop it.
846 * path must point to the INODE_REF or INODE_EXTREF when called.
848 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
849 struct btrfs_key *found_key, int resolve,
850 iterate_inode_ref_t iterate, void *ctx)
852 struct extent_buffer *eb = path->nodes[0];
853 struct btrfs_item *item;
854 struct btrfs_inode_ref *iref;
855 struct btrfs_inode_extref *extref;
856 struct btrfs_path *tmp_path;
860 int slot = path->slots[0];
867 unsigned long name_off;
868 unsigned long elem_size;
871 p = fs_path_alloc_reversed();
875 tmp_path = alloc_path_for_send();
882 if (found_key->type == BTRFS_INODE_REF_KEY) {
883 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
884 struct btrfs_inode_ref);
885 item = btrfs_item_nr(slot);
886 total = btrfs_item_size(eb, item);
887 elem_size = sizeof(*iref);
889 ptr = btrfs_item_ptr_offset(eb, slot);
890 total = btrfs_item_size_nr(eb, slot);
891 elem_size = sizeof(*extref);
894 while (cur < total) {
897 if (found_key->type == BTRFS_INODE_REF_KEY) {
898 iref = (struct btrfs_inode_ref *)(ptr + cur);
899 name_len = btrfs_inode_ref_name_len(eb, iref);
900 name_off = (unsigned long)(iref + 1);
901 index = btrfs_inode_ref_index(eb, iref);
902 dir = found_key->offset;
904 extref = (struct btrfs_inode_extref *)(ptr + cur);
905 name_len = btrfs_inode_extref_name_len(eb, extref);
906 name_off = (unsigned long)&extref->name;
907 index = btrfs_inode_extref_index(eb, extref);
908 dir = btrfs_inode_extref_parent(eb, extref);
912 start = btrfs_ref_to_path(root, tmp_path, name_len,
916 ret = PTR_ERR(start);
919 if (start < p->buf) {
920 /* overflow , try again with larger buffer */
921 ret = fs_path_ensure_buf(p,
922 p->buf_len + p->buf - start);
925 start = btrfs_ref_to_path(root, tmp_path,
930 ret = PTR_ERR(start);
933 BUG_ON(start < p->buf);
937 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
943 cur += elem_size + name_len;
944 ret = iterate(num, dir, index, p, ctx);
951 btrfs_free_path(tmp_path);
956 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
957 const char *name, int name_len,
958 const char *data, int data_len,
962 * Helper function to iterate the entries in ONE btrfs_dir_item.
963 * The iterate callback may return a non zero value to stop iteration. This can
964 * be a negative value for error codes or 1 to simply stop it.
966 * path must point to the dir item when called.
968 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
969 struct btrfs_key *found_key,
970 iterate_dir_item_t iterate, void *ctx)
973 struct extent_buffer *eb;
974 struct btrfs_item *item;
975 struct btrfs_dir_item *di;
976 struct btrfs_key di_key;
978 const int buf_len = PATH_MAX;
988 buf = kmalloc(buf_len, GFP_NOFS);
995 slot = path->slots[0];
996 item = btrfs_item_nr(slot);
997 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1000 total = btrfs_item_size(eb, item);
1003 while (cur < total) {
1004 name_len = btrfs_dir_name_len(eb, di);
1005 data_len = btrfs_dir_data_len(eb, di);
1006 type = btrfs_dir_type(eb, di);
1007 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1012 if (name_len + data_len > buf_len) {
1013 ret = -ENAMETOOLONG;
1017 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1018 name_len + data_len);
1020 len = sizeof(*di) + name_len + data_len;
1021 di = (struct btrfs_dir_item *)((char *)di + len);
1024 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1025 data_len, type, ctx);
1041 static int __copy_first_ref(int num, u64 dir, int index,
1042 struct fs_path *p, void *ctx)
1045 struct fs_path *pt = ctx;
1047 ret = fs_path_copy(pt, p);
1051 /* we want the first only */
1056 * Retrieve the first path of an inode. If an inode has more then one
1057 * ref/hardlink, this is ignored.
1059 static int get_inode_path(struct btrfs_root *root,
1060 u64 ino, struct fs_path *path)
1063 struct btrfs_key key, found_key;
1064 struct btrfs_path *p;
1066 p = alloc_path_for_send();
1070 fs_path_reset(path);
1073 key.type = BTRFS_INODE_REF_KEY;
1076 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1083 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1084 if (found_key.objectid != ino ||
1085 (found_key.type != BTRFS_INODE_REF_KEY &&
1086 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1091 ret = iterate_inode_ref(root, p, &found_key, 1,
1092 __copy_first_ref, path);
1102 struct backref_ctx {
1103 struct send_ctx *sctx;
1105 struct btrfs_path *path;
1106 /* number of total found references */
1110 * used for clones found in send_root. clones found behind cur_objectid
1111 * and cur_offset are not considered as allowed clones.
1116 /* may be truncated in case it's the last extent in a file */
1119 /* Just to check for bugs in backref resolving */
1123 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1125 u64 root = (u64)(uintptr_t)key;
1126 struct clone_root *cr = (struct clone_root *)elt;
1128 if (root < cr->root->objectid)
1130 if (root > cr->root->objectid)
1135 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1137 struct clone_root *cr1 = (struct clone_root *)e1;
1138 struct clone_root *cr2 = (struct clone_root *)e2;
1140 if (cr1->root->objectid < cr2->root->objectid)
1142 if (cr1->root->objectid > cr2->root->objectid)
1148 * Called for every backref that is found for the current extent.
1149 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1151 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1153 struct backref_ctx *bctx = ctx_;
1154 struct clone_root *found;
1158 /* First check if the root is in the list of accepted clone sources */
1159 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1160 bctx->sctx->clone_roots_cnt,
1161 sizeof(struct clone_root),
1162 __clone_root_cmp_bsearch);
1166 if (found->root == bctx->sctx->send_root &&
1167 ino == bctx->cur_objectid &&
1168 offset == bctx->cur_offset) {
1169 bctx->found_itself = 1;
1173 * There are inodes that have extents that lie behind its i_size. Don't
1174 * accept clones from these extents.
1176 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1178 btrfs_release_path(bctx->path);
1182 if (offset + bctx->extent_len > i_size)
1186 * Make sure we don't consider clones from send_root that are
1187 * behind the current inode/offset.
1189 if (found->root == bctx->sctx->send_root) {
1191 * TODO for the moment we don't accept clones from the inode
1192 * that is currently send. We may change this when
1193 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1196 if (ino >= bctx->cur_objectid)
1199 if (ino > bctx->cur_objectid)
1201 if (offset + bctx->extent_len > bctx->cur_offset)
1207 found->found_refs++;
1208 if (ino < found->ino) {
1210 found->offset = offset;
1211 } else if (found->ino == ino) {
1213 * same extent found more then once in the same file.
1215 if (found->offset > offset + bctx->extent_len)
1216 found->offset = offset;
1223 * Given an inode, offset and extent item, it finds a good clone for a clone
1224 * instruction. Returns -ENOENT when none could be found. The function makes
1225 * sure that the returned clone is usable at the point where sending is at the
1226 * moment. This means, that no clones are accepted which lie behind the current
1229 * path must point to the extent item when called.
1231 static int find_extent_clone(struct send_ctx *sctx,
1232 struct btrfs_path *path,
1233 u64 ino, u64 data_offset,
1235 struct clone_root **found)
1242 u64 extent_item_pos;
1244 struct btrfs_file_extent_item *fi;
1245 struct extent_buffer *eb = path->nodes[0];
1246 struct backref_ctx *backref_ctx = NULL;
1247 struct clone_root *cur_clone_root;
1248 struct btrfs_key found_key;
1249 struct btrfs_path *tmp_path;
1253 tmp_path = alloc_path_for_send();
1257 /* We only use this path under the commit sem */
1258 tmp_path->need_commit_sem = 0;
1260 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1266 backref_ctx->path = tmp_path;
1268 if (data_offset >= ino_size) {
1270 * There may be extents that lie behind the file's size.
1271 * I at least had this in combination with snapshotting while
1272 * writing large files.
1278 fi = btrfs_item_ptr(eb, path->slots[0],
1279 struct btrfs_file_extent_item);
1280 extent_type = btrfs_file_extent_type(eb, fi);
1281 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1285 compressed = btrfs_file_extent_compression(eb, fi);
1287 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1288 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1289 if (disk_byte == 0) {
1293 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1295 down_read(&sctx->send_root->fs_info->commit_root_sem);
1296 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1297 &found_key, &flags);
1298 up_read(&sctx->send_root->fs_info->commit_root_sem);
1299 btrfs_release_path(tmp_path);
1303 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1309 * Setup the clone roots.
1311 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1312 cur_clone_root = sctx->clone_roots + i;
1313 cur_clone_root->ino = (u64)-1;
1314 cur_clone_root->offset = 0;
1315 cur_clone_root->found_refs = 0;
1318 backref_ctx->sctx = sctx;
1319 backref_ctx->found = 0;
1320 backref_ctx->cur_objectid = ino;
1321 backref_ctx->cur_offset = data_offset;
1322 backref_ctx->found_itself = 0;
1323 backref_ctx->extent_len = num_bytes;
1326 * The last extent of a file may be too large due to page alignment.
1327 * We need to adjust extent_len in this case so that the checks in
1328 * __iterate_backrefs work.
1330 if (data_offset + num_bytes >= ino_size)
1331 backref_ctx->extent_len = ino_size - data_offset;
1334 * Now collect all backrefs.
1336 if (compressed == BTRFS_COMPRESS_NONE)
1337 extent_item_pos = logical - found_key.objectid;
1339 extent_item_pos = 0;
1340 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1341 found_key.objectid, extent_item_pos, 1,
1342 __iterate_backrefs, backref_ctx);
1347 if (!backref_ctx->found_itself) {
1348 /* found a bug in backref code? */
1350 btrfs_err(sctx->send_root->fs_info, "did not find backref in "
1351 "send_root. inode=%llu, offset=%llu, "
1352 "disk_byte=%llu found extent=%llu",
1353 ino, data_offset, disk_byte, found_key.objectid);
1357 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1359 "num_bytes=%llu, logical=%llu\n",
1360 data_offset, ino, num_bytes, logical);
1362 if (!backref_ctx->found)
1363 verbose_printk("btrfs: no clones found\n");
1365 cur_clone_root = NULL;
1366 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1367 if (sctx->clone_roots[i].found_refs) {
1368 if (!cur_clone_root)
1369 cur_clone_root = sctx->clone_roots + i;
1370 else if (sctx->clone_roots[i].root == sctx->send_root)
1371 /* prefer clones from send_root over others */
1372 cur_clone_root = sctx->clone_roots + i;
1377 if (cur_clone_root) {
1378 if (compressed != BTRFS_COMPRESS_NONE) {
1380 * Offsets given by iterate_extent_inodes() are relative
1381 * to the start of the extent, we need to add logical
1382 * offset from the file extent item.
1383 * (See why at backref.c:check_extent_in_eb())
1385 cur_clone_root->offset += btrfs_file_extent_offset(eb,
1388 *found = cur_clone_root;
1395 btrfs_free_path(tmp_path);
1400 static int read_symlink(struct btrfs_root *root,
1402 struct fs_path *dest)
1405 struct btrfs_path *path;
1406 struct btrfs_key key;
1407 struct btrfs_file_extent_item *ei;
1413 path = alloc_path_for_send();
1418 key.type = BTRFS_EXTENT_DATA_KEY;
1420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1425 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1426 struct btrfs_file_extent_item);
1427 type = btrfs_file_extent_type(path->nodes[0], ei);
1428 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1429 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1430 BUG_ON(compression);
1432 off = btrfs_file_extent_inline_start(ei);
1433 len = btrfs_file_extent_inline_len(path->nodes[0], path->slots[0], ei);
1435 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1438 btrfs_free_path(path);
1443 * Helper function to generate a file name that is unique in the root of
1444 * send_root and parent_root. This is used to generate names for orphan inodes.
1446 static int gen_unique_name(struct send_ctx *sctx,
1448 struct fs_path *dest)
1451 struct btrfs_path *path;
1452 struct btrfs_dir_item *di;
1457 path = alloc_path_for_send();
1462 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1464 ASSERT(len < sizeof(tmp));
1466 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1467 path, BTRFS_FIRST_FREE_OBJECTID,
1468 tmp, strlen(tmp), 0);
1469 btrfs_release_path(path);
1475 /* not unique, try again */
1480 if (!sctx->parent_root) {
1486 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1487 path, BTRFS_FIRST_FREE_OBJECTID,
1488 tmp, strlen(tmp), 0);
1489 btrfs_release_path(path);
1495 /* not unique, try again */
1503 ret = fs_path_add(dest, tmp, strlen(tmp));
1506 btrfs_free_path(path);
1511 inode_state_no_change,
1512 inode_state_will_create,
1513 inode_state_did_create,
1514 inode_state_will_delete,
1515 inode_state_did_delete,
1518 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1526 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1528 if (ret < 0 && ret != -ENOENT)
1532 if (!sctx->parent_root) {
1533 right_ret = -ENOENT;
1535 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1536 NULL, NULL, NULL, NULL);
1537 if (ret < 0 && ret != -ENOENT)
1542 if (!left_ret && !right_ret) {
1543 if (left_gen == gen && right_gen == gen) {
1544 ret = inode_state_no_change;
1545 } else if (left_gen == gen) {
1546 if (ino < sctx->send_progress)
1547 ret = inode_state_did_create;
1549 ret = inode_state_will_create;
1550 } else if (right_gen == gen) {
1551 if (ino < sctx->send_progress)
1552 ret = inode_state_did_delete;
1554 ret = inode_state_will_delete;
1558 } else if (!left_ret) {
1559 if (left_gen == gen) {
1560 if (ino < sctx->send_progress)
1561 ret = inode_state_did_create;
1563 ret = inode_state_will_create;
1567 } else if (!right_ret) {
1568 if (right_gen == gen) {
1569 if (ino < sctx->send_progress)
1570 ret = inode_state_did_delete;
1572 ret = inode_state_will_delete;
1584 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1588 ret = get_cur_inode_state(sctx, ino, gen);
1592 if (ret == inode_state_no_change ||
1593 ret == inode_state_did_create ||
1594 ret == inode_state_will_delete)
1604 * Helper function to lookup a dir item in a dir.
1606 static int lookup_dir_item_inode(struct btrfs_root *root,
1607 u64 dir, const char *name, int name_len,
1612 struct btrfs_dir_item *di;
1613 struct btrfs_key key;
1614 struct btrfs_path *path;
1616 path = alloc_path_for_send();
1620 di = btrfs_lookup_dir_item(NULL, root, path,
1621 dir, name, name_len, 0);
1630 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1631 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1635 *found_inode = key.objectid;
1636 *found_type = btrfs_dir_type(path->nodes[0], di);
1639 btrfs_free_path(path);
1644 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1645 * generation of the parent dir and the name of the dir entry.
1647 static int get_first_ref(struct btrfs_root *root, u64 ino,
1648 u64 *dir, u64 *dir_gen, struct fs_path *name)
1651 struct btrfs_key key;
1652 struct btrfs_key found_key;
1653 struct btrfs_path *path;
1657 path = alloc_path_for_send();
1662 key.type = BTRFS_INODE_REF_KEY;
1665 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1669 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1671 if (ret || found_key.objectid != ino ||
1672 (found_key.type != BTRFS_INODE_REF_KEY &&
1673 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1678 if (found_key.type == BTRFS_INODE_REF_KEY) {
1679 struct btrfs_inode_ref *iref;
1680 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1681 struct btrfs_inode_ref);
1682 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1683 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1684 (unsigned long)(iref + 1),
1686 parent_dir = found_key.offset;
1688 struct btrfs_inode_extref *extref;
1689 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1690 struct btrfs_inode_extref);
1691 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1692 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1693 (unsigned long)&extref->name, len);
1694 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1698 btrfs_release_path(path);
1701 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1710 btrfs_free_path(path);
1714 static int is_first_ref(struct btrfs_root *root,
1716 const char *name, int name_len)
1719 struct fs_path *tmp_name;
1722 tmp_name = fs_path_alloc();
1726 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1730 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1735 ret = !memcmp(tmp_name->start, name, name_len);
1738 fs_path_free(tmp_name);
1743 * Used by process_recorded_refs to determine if a new ref would overwrite an
1744 * already existing ref. In case it detects an overwrite, it returns the
1745 * inode/gen in who_ino/who_gen.
1746 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1747 * to make sure later references to the overwritten inode are possible.
1748 * Orphanizing is however only required for the first ref of an inode.
1749 * process_recorded_refs does an additional is_first_ref check to see if
1750 * orphanizing is really required.
1752 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1753 const char *name, int name_len,
1754 u64 *who_ino, u64 *who_gen)
1758 u64 other_inode = 0;
1761 if (!sctx->parent_root)
1764 ret = is_inode_existent(sctx, dir, dir_gen);
1769 * If we have a parent root we need to verify that the parent dir was
1770 * not delted and then re-created, if it was then we have no overwrite
1771 * and we can just unlink this entry.
1773 if (sctx->parent_root) {
1774 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1776 if (ret < 0 && ret != -ENOENT)
1786 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1787 &other_inode, &other_type);
1788 if (ret < 0 && ret != -ENOENT)
1796 * Check if the overwritten ref was already processed. If yes, the ref
1797 * was already unlinked/moved, so we can safely assume that we will not
1798 * overwrite anything at this point in time.
1800 if (other_inode > sctx->send_progress) {
1801 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1802 who_gen, NULL, NULL, NULL, NULL);
1807 *who_ino = other_inode;
1817 * Checks if the ref was overwritten by an already processed inode. This is
1818 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1819 * thus the orphan name needs be used.
1820 * process_recorded_refs also uses it to avoid unlinking of refs that were
1823 static int did_overwrite_ref(struct send_ctx *sctx,
1824 u64 dir, u64 dir_gen,
1825 u64 ino, u64 ino_gen,
1826 const char *name, int name_len)
1833 if (!sctx->parent_root)
1836 ret = is_inode_existent(sctx, dir, dir_gen);
1840 /* check if the ref was overwritten by another ref */
1841 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1842 &ow_inode, &other_type);
1843 if (ret < 0 && ret != -ENOENT)
1846 /* was never and will never be overwritten */
1851 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1856 if (ow_inode == ino && gen == ino_gen) {
1861 /* we know that it is or will be overwritten. check this now */
1862 if (ow_inode < sctx->send_progress)
1872 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1873 * that got overwritten. This is used by process_recorded_refs to determine
1874 * if it has to use the path as returned by get_cur_path or the orphan name.
1876 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1879 struct fs_path *name = NULL;
1883 if (!sctx->parent_root)
1886 name = fs_path_alloc();
1890 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1894 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1895 name->start, fs_path_len(name));
1903 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1904 * so we need to do some special handling in case we have clashes. This function
1905 * takes care of this with the help of name_cache_entry::radix_list.
1906 * In case of error, nce is kfreed.
1908 static int name_cache_insert(struct send_ctx *sctx,
1909 struct name_cache_entry *nce)
1912 struct list_head *nce_head;
1914 nce_head = radix_tree_lookup(&sctx->name_cache,
1915 (unsigned long)nce->ino);
1917 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1922 INIT_LIST_HEAD(nce_head);
1924 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1931 list_add_tail(&nce->radix_list, nce_head);
1932 list_add_tail(&nce->list, &sctx->name_cache_list);
1933 sctx->name_cache_size++;
1938 static void name_cache_delete(struct send_ctx *sctx,
1939 struct name_cache_entry *nce)
1941 struct list_head *nce_head;
1943 nce_head = radix_tree_lookup(&sctx->name_cache,
1944 (unsigned long)nce->ino);
1946 btrfs_err(sctx->send_root->fs_info,
1947 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
1948 nce->ino, sctx->name_cache_size);
1951 list_del(&nce->radix_list);
1952 list_del(&nce->list);
1953 sctx->name_cache_size--;
1956 * We may not get to the final release of nce_head if the lookup fails
1958 if (nce_head && list_empty(nce_head)) {
1959 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1964 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1967 struct list_head *nce_head;
1968 struct name_cache_entry *cur;
1970 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1974 list_for_each_entry(cur, nce_head, radix_list) {
1975 if (cur->ino == ino && cur->gen == gen)
1982 * Removes the entry from the list and adds it back to the end. This marks the
1983 * entry as recently used so that name_cache_clean_unused does not remove it.
1985 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1987 list_del(&nce->list);
1988 list_add_tail(&nce->list, &sctx->name_cache_list);
1992 * Remove some entries from the beginning of name_cache_list.
1994 static void name_cache_clean_unused(struct send_ctx *sctx)
1996 struct name_cache_entry *nce;
1998 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2001 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2002 nce = list_entry(sctx->name_cache_list.next,
2003 struct name_cache_entry, list);
2004 name_cache_delete(sctx, nce);
2009 static void name_cache_free(struct send_ctx *sctx)
2011 struct name_cache_entry *nce;
2013 while (!list_empty(&sctx->name_cache_list)) {
2014 nce = list_entry(sctx->name_cache_list.next,
2015 struct name_cache_entry, list);
2016 name_cache_delete(sctx, nce);
2022 * Used by get_cur_path for each ref up to the root.
2023 * Returns 0 if it succeeded.
2024 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2025 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2026 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2027 * Returns <0 in case of error.
2029 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2033 struct fs_path *dest)
2037 struct name_cache_entry *nce = NULL;
2040 * First check if we already did a call to this function with the same
2041 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2042 * return the cached result.
2044 nce = name_cache_search(sctx, ino, gen);
2046 if (ino < sctx->send_progress && nce->need_later_update) {
2047 name_cache_delete(sctx, nce);
2051 name_cache_used(sctx, nce);
2052 *parent_ino = nce->parent_ino;
2053 *parent_gen = nce->parent_gen;
2054 ret = fs_path_add(dest, nce->name, nce->name_len);
2063 * If the inode is not existent yet, add the orphan name and return 1.
2064 * This should only happen for the parent dir that we determine in
2067 ret = is_inode_existent(sctx, ino, gen);
2072 ret = gen_unique_name(sctx, ino, gen, dest);
2080 * Depending on whether the inode was already processed or not, use
2081 * send_root or parent_root for ref lookup.
2083 if (ino < sctx->send_progress)
2084 ret = get_first_ref(sctx->send_root, ino,
2085 parent_ino, parent_gen, dest);
2087 ret = get_first_ref(sctx->parent_root, ino,
2088 parent_ino, parent_gen, dest);
2093 * Check if the ref was overwritten by an inode's ref that was processed
2094 * earlier. If yes, treat as orphan and return 1.
2096 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2097 dest->start, dest->end - dest->start);
2101 fs_path_reset(dest);
2102 ret = gen_unique_name(sctx, ino, gen, dest);
2110 * Store the result of the lookup in the name cache.
2112 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2120 nce->parent_ino = *parent_ino;
2121 nce->parent_gen = *parent_gen;
2122 nce->name_len = fs_path_len(dest);
2124 strcpy(nce->name, dest->start);
2126 if (ino < sctx->send_progress)
2127 nce->need_later_update = 0;
2129 nce->need_later_update = 1;
2131 nce_ret = name_cache_insert(sctx, nce);
2134 name_cache_clean_unused(sctx);
2141 * Magic happens here. This function returns the first ref to an inode as it
2142 * would look like while receiving the stream at this point in time.
2143 * We walk the path up to the root. For every inode in between, we check if it
2144 * was already processed/sent. If yes, we continue with the parent as found
2145 * in send_root. If not, we continue with the parent as found in parent_root.
2146 * If we encounter an inode that was deleted at this point in time, we use the
2147 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2148 * that were not created yet and overwritten inodes/refs.
2150 * When do we have have orphan inodes:
2151 * 1. When an inode is freshly created and thus no valid refs are available yet
2152 * 2. When a directory lost all it's refs (deleted) but still has dir items
2153 * inside which were not processed yet (pending for move/delete). If anyone
2154 * tried to get the path to the dir items, it would get a path inside that
2156 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2157 * of an unprocessed inode. If in that case the first ref would be
2158 * overwritten, the overwritten inode gets "orphanized". Later when we
2159 * process this overwritten inode, it is restored at a new place by moving
2162 * sctx->send_progress tells this function at which point in time receiving
2165 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2166 struct fs_path *dest)
2169 struct fs_path *name = NULL;
2170 u64 parent_inode = 0;
2174 name = fs_path_alloc();
2181 fs_path_reset(dest);
2183 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2184 fs_path_reset(name);
2186 if (is_waiting_for_rm(sctx, ino)) {
2187 ret = gen_unique_name(sctx, ino, gen, name);
2190 ret = fs_path_add_path(dest, name);
2194 if (is_waiting_for_move(sctx, ino)) {
2195 ret = get_first_ref(sctx->parent_root, ino,
2196 &parent_inode, &parent_gen, name);
2198 ret = __get_cur_name_and_parent(sctx, ino, gen,
2208 ret = fs_path_add_path(dest, name);
2219 fs_path_unreverse(dest);
2224 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2226 static int send_subvol_begin(struct send_ctx *sctx)
2229 struct btrfs_root *send_root = sctx->send_root;
2230 struct btrfs_root *parent_root = sctx->parent_root;
2231 struct btrfs_path *path;
2232 struct btrfs_key key;
2233 struct btrfs_root_ref *ref;
2234 struct extent_buffer *leaf;
2238 path = btrfs_alloc_path();
2242 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2244 btrfs_free_path(path);
2248 key.objectid = send_root->objectid;
2249 key.type = BTRFS_ROOT_BACKREF_KEY;
2252 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2261 leaf = path->nodes[0];
2262 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2263 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2264 key.objectid != send_root->objectid) {
2268 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2269 namelen = btrfs_root_ref_name_len(leaf, ref);
2270 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2271 btrfs_release_path(path);
2274 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2278 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2283 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2284 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2285 sctx->send_root->root_item.uuid);
2286 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2287 le64_to_cpu(sctx->send_root->root_item.ctransid));
2289 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2290 sctx->parent_root->root_item.uuid);
2291 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2292 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2295 ret = send_cmd(sctx);
2299 btrfs_free_path(path);
2304 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2309 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2311 p = fs_path_alloc();
2315 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2319 ret = get_cur_path(sctx, ino, gen, p);
2322 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2323 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2325 ret = send_cmd(sctx);
2333 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2338 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2340 p = fs_path_alloc();
2344 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2348 ret = get_cur_path(sctx, ino, gen, p);
2351 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2352 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2354 ret = send_cmd(sctx);
2362 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2367 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2369 p = fs_path_alloc();
2373 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2377 ret = get_cur_path(sctx, ino, gen, p);
2380 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2381 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2382 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2384 ret = send_cmd(sctx);
2392 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2395 struct fs_path *p = NULL;
2396 struct btrfs_inode_item *ii;
2397 struct btrfs_path *path = NULL;
2398 struct extent_buffer *eb;
2399 struct btrfs_key key;
2402 verbose_printk("btrfs: send_utimes %llu\n", ino);
2404 p = fs_path_alloc();
2408 path = alloc_path_for_send();
2415 key.type = BTRFS_INODE_ITEM_KEY;
2417 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2421 eb = path->nodes[0];
2422 slot = path->slots[0];
2423 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2425 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2429 ret = get_cur_path(sctx, ino, gen, p);
2432 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2433 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2434 btrfs_inode_atime(ii));
2435 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2436 btrfs_inode_mtime(ii));
2437 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2438 btrfs_inode_ctime(ii));
2439 /* TODO Add otime support when the otime patches get into upstream */
2441 ret = send_cmd(sctx);
2446 btrfs_free_path(path);
2451 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2452 * a valid path yet because we did not process the refs yet. So, the inode
2453 * is created as orphan.
2455 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2464 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2466 p = fs_path_alloc();
2470 if (ino != sctx->cur_ino) {
2471 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2476 gen = sctx->cur_inode_gen;
2477 mode = sctx->cur_inode_mode;
2478 rdev = sctx->cur_inode_rdev;
2481 if (S_ISREG(mode)) {
2482 cmd = BTRFS_SEND_C_MKFILE;
2483 } else if (S_ISDIR(mode)) {
2484 cmd = BTRFS_SEND_C_MKDIR;
2485 } else if (S_ISLNK(mode)) {
2486 cmd = BTRFS_SEND_C_SYMLINK;
2487 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2488 cmd = BTRFS_SEND_C_MKNOD;
2489 } else if (S_ISFIFO(mode)) {
2490 cmd = BTRFS_SEND_C_MKFIFO;
2491 } else if (S_ISSOCK(mode)) {
2492 cmd = BTRFS_SEND_C_MKSOCK;
2494 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2495 (int)(mode & S_IFMT));
2500 ret = begin_cmd(sctx, cmd);
2504 ret = gen_unique_name(sctx, ino, gen, p);
2508 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2509 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2511 if (S_ISLNK(mode)) {
2513 ret = read_symlink(sctx->send_root, ino, p);
2516 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2517 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2518 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2519 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2520 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2523 ret = send_cmd(sctx);
2535 * We need some special handling for inodes that get processed before the parent
2536 * directory got created. See process_recorded_refs for details.
2537 * This function does the check if we already created the dir out of order.
2539 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2542 struct btrfs_path *path = NULL;
2543 struct btrfs_key key;
2544 struct btrfs_key found_key;
2545 struct btrfs_key di_key;
2546 struct extent_buffer *eb;
2547 struct btrfs_dir_item *di;
2550 path = alloc_path_for_send();
2557 key.type = BTRFS_DIR_INDEX_KEY;
2559 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2564 eb = path->nodes[0];
2565 slot = path->slots[0];
2566 if (slot >= btrfs_header_nritems(eb)) {
2567 ret = btrfs_next_leaf(sctx->send_root, path);
2570 } else if (ret > 0) {
2577 btrfs_item_key_to_cpu(eb, &found_key, slot);
2578 if (found_key.objectid != key.objectid ||
2579 found_key.type != key.type) {
2584 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2585 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2587 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2588 di_key.objectid < sctx->send_progress) {
2597 btrfs_free_path(path);
2602 * Only creates the inode if it is:
2603 * 1. Not a directory
2604 * 2. Or a directory which was not created already due to out of order
2605 * directories. See did_create_dir and process_recorded_refs for details.
2607 static int send_create_inode_if_needed(struct send_ctx *sctx)
2611 if (S_ISDIR(sctx->cur_inode_mode)) {
2612 ret = did_create_dir(sctx, sctx->cur_ino);
2621 ret = send_create_inode(sctx, sctx->cur_ino);
2629 struct recorded_ref {
2630 struct list_head list;
2633 struct fs_path *full_path;
2641 * We need to process new refs before deleted refs, but compare_tree gives us
2642 * everything mixed. So we first record all refs and later process them.
2643 * This function is a helper to record one ref.
2645 static int __record_ref(struct list_head *head, u64 dir,
2646 u64 dir_gen, struct fs_path *path)
2648 struct recorded_ref *ref;
2650 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2655 ref->dir_gen = dir_gen;
2656 ref->full_path = path;
2658 ref->name = (char *)kbasename(ref->full_path->start);
2659 ref->name_len = ref->full_path->end - ref->name;
2660 ref->dir_path = ref->full_path->start;
2661 if (ref->name == ref->full_path->start)
2662 ref->dir_path_len = 0;
2664 ref->dir_path_len = ref->full_path->end -
2665 ref->full_path->start - 1 - ref->name_len;
2667 list_add_tail(&ref->list, head);
2671 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2673 struct recorded_ref *new;
2675 new = kmalloc(sizeof(*ref), GFP_NOFS);
2679 new->dir = ref->dir;
2680 new->dir_gen = ref->dir_gen;
2681 new->full_path = NULL;
2682 INIT_LIST_HEAD(&new->list);
2683 list_add_tail(&new->list, list);
2687 static void __free_recorded_refs(struct list_head *head)
2689 struct recorded_ref *cur;
2691 while (!list_empty(head)) {
2692 cur = list_entry(head->next, struct recorded_ref, list);
2693 fs_path_free(cur->full_path);
2694 list_del(&cur->list);
2699 static void free_recorded_refs(struct send_ctx *sctx)
2701 __free_recorded_refs(&sctx->new_refs);
2702 __free_recorded_refs(&sctx->deleted_refs);
2706 * Renames/moves a file/dir to its orphan name. Used when the first
2707 * ref of an unprocessed inode gets overwritten and for all non empty
2710 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2711 struct fs_path *path)
2714 struct fs_path *orphan;
2716 orphan = fs_path_alloc();
2720 ret = gen_unique_name(sctx, ino, gen, orphan);
2724 ret = send_rename(sctx, path, orphan);
2727 fs_path_free(orphan);
2731 static struct orphan_dir_info *
2732 add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2734 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2735 struct rb_node *parent = NULL;
2736 struct orphan_dir_info *entry, *odi;
2738 odi = kmalloc(sizeof(*odi), GFP_NOFS);
2740 return ERR_PTR(-ENOMEM);
2746 entry = rb_entry(parent, struct orphan_dir_info, node);
2747 if (dir_ino < entry->ino) {
2749 } else if (dir_ino > entry->ino) {
2750 p = &(*p)->rb_right;
2757 rb_link_node(&odi->node, parent, p);
2758 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2762 static struct orphan_dir_info *
2763 get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2765 struct rb_node *n = sctx->orphan_dirs.rb_node;
2766 struct orphan_dir_info *entry;
2769 entry = rb_entry(n, struct orphan_dir_info, node);
2770 if (dir_ino < entry->ino)
2772 else if (dir_ino > entry->ino)
2780 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2782 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2787 static void free_orphan_dir_info(struct send_ctx *sctx,
2788 struct orphan_dir_info *odi)
2792 rb_erase(&odi->node, &sctx->orphan_dirs);
2797 * Returns 1 if a directory can be removed at this point in time.
2798 * We check this by iterating all dir items and checking if the inode behind
2799 * the dir item was already processed.
2801 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2805 struct btrfs_root *root = sctx->parent_root;
2806 struct btrfs_path *path;
2807 struct btrfs_key key;
2808 struct btrfs_key found_key;
2809 struct btrfs_key loc;
2810 struct btrfs_dir_item *di;
2813 * Don't try to rmdir the top/root subvolume dir.
2815 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2818 path = alloc_path_for_send();
2823 key.type = BTRFS_DIR_INDEX_KEY;
2825 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2830 struct waiting_dir_move *dm;
2832 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2833 ret = btrfs_next_leaf(root, path);
2840 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2842 if (found_key.objectid != key.objectid ||
2843 found_key.type != key.type)
2846 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2847 struct btrfs_dir_item);
2848 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2850 dm = get_waiting_dir_move(sctx, loc.objectid);
2852 struct orphan_dir_info *odi;
2854 odi = add_orphan_dir_info(sctx, dir);
2860 dm->rmdir_ino = dir;
2865 if (loc.objectid > send_progress) {
2876 btrfs_free_path(path);
2880 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
2882 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
2884 return entry != NULL;
2887 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2889 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
2890 struct rb_node *parent = NULL;
2891 struct waiting_dir_move *entry, *dm;
2893 dm = kmalloc(sizeof(*dm), GFP_NOFS);
2901 entry = rb_entry(parent, struct waiting_dir_move, node);
2902 if (ino < entry->ino) {
2904 } else if (ino > entry->ino) {
2905 p = &(*p)->rb_right;
2912 rb_link_node(&dm->node, parent, p);
2913 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
2917 static struct waiting_dir_move *
2918 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
2920 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
2921 struct waiting_dir_move *entry;
2924 entry = rb_entry(n, struct waiting_dir_move, node);
2925 if (ino < entry->ino)
2927 else if (ino > entry->ino)
2935 static void free_waiting_dir_move(struct send_ctx *sctx,
2936 struct waiting_dir_move *dm)
2940 rb_erase(&dm->node, &sctx->waiting_dir_moves);
2944 static int add_pending_dir_move(struct send_ctx *sctx,
2948 struct list_head *new_refs,
2949 struct list_head *deleted_refs)
2951 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
2952 struct rb_node *parent = NULL;
2953 struct pending_dir_move *entry = NULL, *pm;
2954 struct recorded_ref *cur;
2958 pm = kmalloc(sizeof(*pm), GFP_NOFS);
2961 pm->parent_ino = parent_ino;
2964 INIT_LIST_HEAD(&pm->list);
2965 INIT_LIST_HEAD(&pm->update_refs);
2966 RB_CLEAR_NODE(&pm->node);
2970 entry = rb_entry(parent, struct pending_dir_move, node);
2971 if (parent_ino < entry->parent_ino) {
2973 } else if (parent_ino > entry->parent_ino) {
2974 p = &(*p)->rb_right;
2981 list_for_each_entry(cur, deleted_refs, list) {
2982 ret = dup_ref(cur, &pm->update_refs);
2986 list_for_each_entry(cur, new_refs, list) {
2987 ret = dup_ref(cur, &pm->update_refs);
2992 ret = add_waiting_dir_move(sctx, pm->ino);
2997 list_add_tail(&pm->list, &entry->list);
2999 rb_link_node(&pm->node, parent, p);
3000 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3005 __free_recorded_refs(&pm->update_refs);
3011 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3014 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3015 struct pending_dir_move *entry;
3018 entry = rb_entry(n, struct pending_dir_move, node);
3019 if (parent_ino < entry->parent_ino)
3021 else if (parent_ino > entry->parent_ino)
3029 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3030 u64 ino, u64 gen, u64 *ancestor_ino)
3033 u64 parent_inode = 0;
3035 u64 start_ino = ino;
3038 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3039 fs_path_reset(name);
3041 if (is_waiting_for_rm(sctx, ino))
3043 if (is_waiting_for_move(sctx, ino)) {
3044 if (*ancestor_ino == 0)
3045 *ancestor_ino = ino;
3046 ret = get_first_ref(sctx->parent_root, ino,
3047 &parent_inode, &parent_gen, name);
3049 ret = __get_cur_name_and_parent(sctx, ino, gen,
3059 if (parent_inode == start_ino) {
3061 if (*ancestor_ino == 0)
3062 *ancestor_ino = ino;
3071 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3073 struct fs_path *from_path = NULL;
3074 struct fs_path *to_path = NULL;
3075 struct fs_path *name = NULL;
3076 u64 orig_progress = sctx->send_progress;
3077 struct recorded_ref *cur;
3078 u64 parent_ino, parent_gen;
3079 struct waiting_dir_move *dm = NULL;
3084 name = fs_path_alloc();
3085 from_path = fs_path_alloc();
3086 if (!name || !from_path) {
3091 dm = get_waiting_dir_move(sctx, pm->ino);
3093 rmdir_ino = dm->rmdir_ino;
3094 free_waiting_dir_move(sctx, dm);
3096 ret = get_first_ref(sctx->parent_root, pm->ino,
3097 &parent_ino, &parent_gen, name);
3101 ret = get_cur_path(sctx, parent_ino, parent_gen,
3105 ret = fs_path_add_path(from_path, name);
3109 sctx->send_progress = sctx->cur_ino + 1;
3110 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3112 LIST_HEAD(deleted_refs);
3113 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3114 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3115 &pm->update_refs, &deleted_refs);
3119 dm = get_waiting_dir_move(sctx, pm->ino);
3121 dm->rmdir_ino = rmdir_ino;
3125 fs_path_reset(name);
3128 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3132 ret = send_rename(sctx, from_path, to_path);
3137 struct orphan_dir_info *odi;
3139 odi = get_orphan_dir_info(sctx, rmdir_ino);
3141 /* already deleted */
3144 ret = can_rmdir(sctx, rmdir_ino, odi->gen, sctx->cur_ino + 1);
3150 name = fs_path_alloc();
3155 ret = get_cur_path(sctx, rmdir_ino, odi->gen, name);
3158 ret = send_rmdir(sctx, name);
3161 free_orphan_dir_info(sctx, odi);
3165 ret = send_utimes(sctx, pm->ino, pm->gen);
3170 * After rename/move, need to update the utimes of both new parent(s)
3171 * and old parent(s).
3173 list_for_each_entry(cur, &pm->update_refs, list) {
3174 if (cur->dir == rmdir_ino)
3176 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3183 fs_path_free(from_path);
3184 fs_path_free(to_path);
3185 sctx->send_progress = orig_progress;
3190 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3192 if (!list_empty(&m->list))
3194 if (!RB_EMPTY_NODE(&m->node))
3195 rb_erase(&m->node, &sctx->pending_dir_moves);
3196 __free_recorded_refs(&m->update_refs);
3200 static void tail_append_pending_moves(struct pending_dir_move *moves,
3201 struct list_head *stack)
3203 if (list_empty(&moves->list)) {
3204 list_add_tail(&moves->list, stack);
3207 list_splice_init(&moves->list, &list);
3208 list_add_tail(&moves->list, stack);
3209 list_splice_tail(&list, stack);
3213 static int apply_children_dir_moves(struct send_ctx *sctx)
3215 struct pending_dir_move *pm;
3216 struct list_head stack;
3217 u64 parent_ino = sctx->cur_ino;
3220 pm = get_pending_dir_moves(sctx, parent_ino);
3224 INIT_LIST_HEAD(&stack);
3225 tail_append_pending_moves(pm, &stack);
3227 while (!list_empty(&stack)) {
3228 pm = list_first_entry(&stack, struct pending_dir_move, list);
3229 parent_ino = pm->ino;
3230 ret = apply_dir_move(sctx, pm);
3231 free_pending_move(sctx, pm);
3234 pm = get_pending_dir_moves(sctx, parent_ino);
3236 tail_append_pending_moves(pm, &stack);
3241 while (!list_empty(&stack)) {
3242 pm = list_first_entry(&stack, struct pending_dir_move, list);
3243 free_pending_move(sctx, pm);
3248 static int wait_for_parent_move(struct send_ctx *sctx,
3249 struct recorded_ref *parent_ref)
3252 u64 ino = parent_ref->dir;
3253 u64 parent_ino_before, parent_ino_after;
3254 struct fs_path *path_before = NULL;
3255 struct fs_path *path_after = NULL;
3258 path_after = fs_path_alloc();
3259 path_before = fs_path_alloc();
3260 if (!path_after || !path_before) {
3266 * Our current directory inode may not yet be renamed/moved because some
3267 * ancestor (immediate or not) has to be renamed/moved first. So find if
3268 * such ancestor exists and make sure our own rename/move happens after
3269 * that ancestor is processed.
3271 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3272 if (is_waiting_for_move(sctx, ino)) {
3277 fs_path_reset(path_before);
3278 fs_path_reset(path_after);
3280 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3284 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3286 if (ret < 0 && ret != -ENOENT) {
3288 } else if (ret == -ENOENT) {
3293 len1 = fs_path_len(path_before);
3294 len2 = fs_path_len(path_after);
3295 if (ino > sctx->cur_ino &&
3296 (parent_ino_before != parent_ino_after || len1 != len2 ||
3297 memcmp(path_before->start, path_after->start, len1))) {
3301 ino = parent_ino_after;
3305 fs_path_free(path_before);
3306 fs_path_free(path_after);
3309 ret = add_pending_dir_move(sctx,
3311 sctx->cur_inode_gen,
3314 &sctx->deleted_refs);
3323 * This does all the move/link/unlink/rmdir magic.
3325 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3328 struct recorded_ref *cur;
3329 struct recorded_ref *cur2;
3330 struct list_head check_dirs;
3331 struct fs_path *valid_path = NULL;
3334 int did_overwrite = 0;
3336 u64 last_dir_ino_rm = 0;
3338 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
3341 * This should never happen as the root dir always has the same ref
3342 * which is always '..'
3344 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3345 INIT_LIST_HEAD(&check_dirs);
3347 valid_path = fs_path_alloc();
3354 * First, check if the first ref of the current inode was overwritten
3355 * before. If yes, we know that the current inode was already orphanized
3356 * and thus use the orphan name. If not, we can use get_cur_path to
3357 * get the path of the first ref as it would like while receiving at
3358 * this point in time.
3359 * New inodes are always orphan at the beginning, so force to use the
3360 * orphan name in this case.
3361 * The first ref is stored in valid_path and will be updated if it
3362 * gets moved around.
3364 if (!sctx->cur_inode_new) {
3365 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3366 sctx->cur_inode_gen);
3372 if (sctx->cur_inode_new || did_overwrite) {
3373 ret = gen_unique_name(sctx, sctx->cur_ino,
3374 sctx->cur_inode_gen, valid_path);
3379 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3385 list_for_each_entry(cur, &sctx->new_refs, list) {
3387 * We may have refs where the parent directory does not exist
3388 * yet. This happens if the parent directories inum is higher
3389 * the the current inum. To handle this case, we create the
3390 * parent directory out of order. But we need to check if this
3391 * did already happen before due to other refs in the same dir.
3393 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3396 if (ret == inode_state_will_create) {
3399 * First check if any of the current inodes refs did
3400 * already create the dir.
3402 list_for_each_entry(cur2, &sctx->new_refs, list) {
3405 if (cur2->dir == cur->dir) {
3412 * If that did not happen, check if a previous inode
3413 * did already create the dir.
3416 ret = did_create_dir(sctx, cur->dir);
3420 ret = send_create_inode(sctx, cur->dir);
3427 * Check if this new ref would overwrite the first ref of
3428 * another unprocessed inode. If yes, orphanize the
3429 * overwritten inode. If we find an overwritten ref that is
3430 * not the first ref, simply unlink it.
3432 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3433 cur->name, cur->name_len,
3434 &ow_inode, &ow_gen);
3438 ret = is_first_ref(sctx->parent_root,
3439 ow_inode, cur->dir, cur->name,
3444 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3449 ret = send_unlink(sctx, cur->full_path);
3456 * link/move the ref to the new place. If we have an orphan
3457 * inode, move it and update valid_path. If not, link or move
3458 * it depending on the inode mode.
3461 ret = send_rename(sctx, valid_path, cur->full_path);
3465 ret = fs_path_copy(valid_path, cur->full_path);
3469 if (S_ISDIR(sctx->cur_inode_mode)) {
3471 * Dirs can't be linked, so move it. For moved
3472 * dirs, we always have one new and one deleted
3473 * ref. The deleted ref is ignored later.
3475 ret = wait_for_parent_move(sctx, cur);
3481 ret = send_rename(sctx, valid_path,
3484 ret = fs_path_copy(valid_path,
3490 ret = send_link(sctx, cur->full_path,
3496 ret = dup_ref(cur, &check_dirs);
3501 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
3503 * Check if we can already rmdir the directory. If not,
3504 * orphanize it. For every dir item inside that gets deleted
3505 * later, we do this check again and rmdir it then if possible.
3506 * See the use of check_dirs for more details.
3508 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3513 ret = send_rmdir(sctx, valid_path);
3516 } else if (!is_orphan) {
3517 ret = orphanize_inode(sctx, sctx->cur_ino,
3518 sctx->cur_inode_gen, valid_path);
3524 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3525 ret = dup_ref(cur, &check_dirs);
3529 } else if (S_ISDIR(sctx->cur_inode_mode) &&
3530 !list_empty(&sctx->deleted_refs)) {
3532 * We have a moved dir. Add the old parent to check_dirs
3534 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
3536 ret = dup_ref(cur, &check_dirs);
3539 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
3541 * We have a non dir inode. Go through all deleted refs and
3542 * unlink them if they were not already overwritten by other
3545 list_for_each_entry(cur, &sctx->deleted_refs, list) {
3546 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3547 sctx->cur_ino, sctx->cur_inode_gen,
3548 cur->name, cur->name_len);
3552 ret = send_unlink(sctx, cur->full_path);
3556 ret = dup_ref(cur, &check_dirs);
3561 * If the inode is still orphan, unlink the orphan. This may
3562 * happen when a previous inode did overwrite the first ref
3563 * of this inode and no new refs were added for the current
3564 * inode. Unlinking does not mean that the inode is deleted in
3565 * all cases. There may still be links to this inode in other
3569 ret = send_unlink(sctx, valid_path);
3576 * We did collect all parent dirs where cur_inode was once located. We
3577 * now go through all these dirs and check if they are pending for
3578 * deletion and if it's finally possible to perform the rmdir now.
3579 * We also update the inode stats of the parent dirs here.
3581 list_for_each_entry(cur, &check_dirs, list) {
3583 * In case we had refs into dirs that were not processed yet,
3584 * we don't need to do the utime and rmdir logic for these dirs.
3585 * The dir will be processed later.
3587 if (cur->dir > sctx->cur_ino)
3590 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3594 if (ret == inode_state_did_create ||
3595 ret == inode_state_no_change) {
3596 /* TODO delayed utimes */
3597 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3600 } else if (ret == inode_state_did_delete &&
3601 cur->dir != last_dir_ino_rm) {
3602 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
3607 ret = get_cur_path(sctx, cur->dir,
3608 cur->dir_gen, valid_path);
3611 ret = send_rmdir(sctx, valid_path);
3614 last_dir_ino_rm = cur->dir;
3622 __free_recorded_refs(&check_dirs);
3623 free_recorded_refs(sctx);
3624 fs_path_free(valid_path);
3628 static int record_ref(struct btrfs_root *root, int num, u64 dir, int index,
3629 struct fs_path *name, void *ctx, struct list_head *refs)
3632 struct send_ctx *sctx = ctx;
3636 p = fs_path_alloc();
3640 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
3645 ret = get_cur_path(sctx, dir, gen, p);
3648 ret = fs_path_add_path(p, name);
3652 ret = __record_ref(refs, dir, gen, p);
3660 static int __record_new_ref(int num, u64 dir, int index,
3661 struct fs_path *name,
3664 struct send_ctx *sctx = ctx;
3665 return record_ref(sctx->send_root, num, dir, index, name,
3666 ctx, &sctx->new_refs);
3670 static int __record_deleted_ref(int num, u64 dir, int index,
3671 struct fs_path *name,
3674 struct send_ctx *sctx = ctx;
3675 return record_ref(sctx->parent_root, num, dir, index, name,
3676 ctx, &sctx->deleted_refs);
3679 static int record_new_ref(struct send_ctx *sctx)
3683 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3684 sctx->cmp_key, 0, __record_new_ref, sctx);
3693 static int record_deleted_ref(struct send_ctx *sctx)
3697 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3698 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3707 struct find_ref_ctx {
3710 struct btrfs_root *root;
3711 struct fs_path *name;
3715 static int __find_iref(int num, u64 dir, int index,
3716 struct fs_path *name,
3719 struct find_ref_ctx *ctx = ctx_;
3723 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3724 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3726 * To avoid doing extra lookups we'll only do this if everything
3729 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3733 if (dir_gen != ctx->dir_gen)
3735 ctx->found_idx = num;
3741 static int find_iref(struct btrfs_root *root,
3742 struct btrfs_path *path,
3743 struct btrfs_key *key,
3744 u64 dir, u64 dir_gen, struct fs_path *name)
3747 struct find_ref_ctx ctx;
3751 ctx.dir_gen = dir_gen;
3755 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3759 if (ctx.found_idx == -1)
3762 return ctx.found_idx;
3765 static int __record_changed_new_ref(int num, u64 dir, int index,
3766 struct fs_path *name,
3771 struct send_ctx *sctx = ctx;
3773 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3778 ret = find_iref(sctx->parent_root, sctx->right_path,
3779 sctx->cmp_key, dir, dir_gen, name);
3781 ret = __record_new_ref(num, dir, index, name, sctx);
3788 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3789 struct fs_path *name,
3794 struct send_ctx *sctx = ctx;
3796 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3801 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3802 dir, dir_gen, name);
3804 ret = __record_deleted_ref(num, dir, index, name, sctx);
3811 static int record_changed_ref(struct send_ctx *sctx)
3815 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3816 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3819 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3820 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3830 * Record and process all refs at once. Needed when an inode changes the
3831 * generation number, which means that it was deleted and recreated.
3833 static int process_all_refs(struct send_ctx *sctx,
3834 enum btrfs_compare_tree_result cmd)
3837 struct btrfs_root *root;
3838 struct btrfs_path *path;
3839 struct btrfs_key key;
3840 struct btrfs_key found_key;
3841 struct extent_buffer *eb;
3843 iterate_inode_ref_t cb;
3844 int pending_move = 0;
3846 path = alloc_path_for_send();
3850 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3851 root = sctx->send_root;
3852 cb = __record_new_ref;
3853 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3854 root = sctx->parent_root;
3855 cb = __record_deleted_ref;
3857 btrfs_err(sctx->send_root->fs_info,
3858 "Wrong command %d in process_all_refs", cmd);
3863 key.objectid = sctx->cmp_key->objectid;
3864 key.type = BTRFS_INODE_REF_KEY;
3866 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3871 eb = path->nodes[0];
3872 slot = path->slots[0];
3873 if (slot >= btrfs_header_nritems(eb)) {
3874 ret = btrfs_next_leaf(root, path);
3882 btrfs_item_key_to_cpu(eb, &found_key, slot);
3884 if (found_key.objectid != key.objectid ||
3885 (found_key.type != BTRFS_INODE_REF_KEY &&
3886 found_key.type != BTRFS_INODE_EXTREF_KEY))
3889 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
3895 btrfs_release_path(path);
3897 ret = process_recorded_refs(sctx, &pending_move);
3898 /* Only applicable to an incremental send. */
3899 ASSERT(pending_move == 0);
3902 btrfs_free_path(path);
3906 static int send_set_xattr(struct send_ctx *sctx,
3907 struct fs_path *path,
3908 const char *name, int name_len,
3909 const char *data, int data_len)
3913 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3917 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3918 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3919 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3921 ret = send_cmd(sctx);
3928 static int send_remove_xattr(struct send_ctx *sctx,
3929 struct fs_path *path,
3930 const char *name, int name_len)
3934 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3938 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3939 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3941 ret = send_cmd(sctx);
3948 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3949 const char *name, int name_len,
3950 const char *data, int data_len,
3954 struct send_ctx *sctx = ctx;
3956 posix_acl_xattr_header dummy_acl;
3958 p = fs_path_alloc();
3963 * This hack is needed because empty acl's are stored as zero byte
3964 * data in xattrs. Problem with that is, that receiving these zero byte
3965 * acl's will fail later. To fix this, we send a dummy acl list that
3966 * only contains the version number and no entries.
3968 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3969 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3970 if (data_len == 0) {
3971 dummy_acl.a_version =
3972 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3973 data = (char *)&dummy_acl;
3974 data_len = sizeof(dummy_acl);
3978 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3982 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3989 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3990 const char *name, int name_len,
3991 const char *data, int data_len,
3995 struct send_ctx *sctx = ctx;
3998 p = fs_path_alloc();
4002 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4006 ret = send_remove_xattr(sctx, p, name, name_len);
4013 static int process_new_xattr(struct send_ctx *sctx)
4017 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4018 sctx->cmp_key, __process_new_xattr, sctx);
4023 static int process_deleted_xattr(struct send_ctx *sctx)
4027 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4028 sctx->cmp_key, __process_deleted_xattr, sctx);
4033 struct find_xattr_ctx {
4041 static int __find_xattr(int num, struct btrfs_key *di_key,
4042 const char *name, int name_len,
4043 const char *data, int data_len,
4044 u8 type, void *vctx)
4046 struct find_xattr_ctx *ctx = vctx;
4048 if (name_len == ctx->name_len &&
4049 strncmp(name, ctx->name, name_len) == 0) {
4050 ctx->found_idx = num;
4051 ctx->found_data_len = data_len;
4052 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
4053 if (!ctx->found_data)
4060 static int find_xattr(struct btrfs_root *root,
4061 struct btrfs_path *path,
4062 struct btrfs_key *key,
4063 const char *name, int name_len,
4064 char **data, int *data_len)
4067 struct find_xattr_ctx ctx;
4070 ctx.name_len = name_len;
4072 ctx.found_data = NULL;
4073 ctx.found_data_len = 0;
4075 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
4079 if (ctx.found_idx == -1)
4082 *data = ctx.found_data;
4083 *data_len = ctx.found_data_len;
4085 kfree(ctx.found_data);
4087 return ctx.found_idx;
4091 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4092 const char *name, int name_len,
4093 const char *data, int data_len,
4097 struct send_ctx *sctx = ctx;
4098 char *found_data = NULL;
4099 int found_data_len = 0;
4101 ret = find_xattr(sctx->parent_root, sctx->right_path,
4102 sctx->cmp_key, name, name_len, &found_data,
4104 if (ret == -ENOENT) {
4105 ret = __process_new_xattr(num, di_key, name, name_len, data,
4106 data_len, type, ctx);
4107 } else if (ret >= 0) {
4108 if (data_len != found_data_len ||
4109 memcmp(data, found_data, data_len)) {
4110 ret = __process_new_xattr(num, di_key, name, name_len,
4111 data, data_len, type, ctx);
4121 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4122 const char *name, int name_len,
4123 const char *data, int data_len,
4127 struct send_ctx *sctx = ctx;
4129 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4130 name, name_len, NULL, NULL);
4132 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4133 data_len, type, ctx);
4140 static int process_changed_xattr(struct send_ctx *sctx)
4144 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4145 sctx->cmp_key, __process_changed_new_xattr, sctx);
4148 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4149 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
4155 static int process_all_new_xattrs(struct send_ctx *sctx)
4158 struct btrfs_root *root;
4159 struct btrfs_path *path;
4160 struct btrfs_key key;
4161 struct btrfs_key found_key;
4162 struct extent_buffer *eb;
4165 path = alloc_path_for_send();
4169 root = sctx->send_root;
4171 key.objectid = sctx->cmp_key->objectid;
4172 key.type = BTRFS_XATTR_ITEM_KEY;
4174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4179 eb = path->nodes[0];
4180 slot = path->slots[0];
4181 if (slot >= btrfs_header_nritems(eb)) {
4182 ret = btrfs_next_leaf(root, path);
4185 } else if (ret > 0) {
4192 btrfs_item_key_to_cpu(eb, &found_key, slot);
4193 if (found_key.objectid != key.objectid ||
4194 found_key.type != key.type) {
4199 ret = iterate_dir_item(root, path, &found_key,
4200 __process_new_xattr, sctx);
4208 btrfs_free_path(path);
4212 static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4214 struct btrfs_root *root = sctx->send_root;
4215 struct btrfs_fs_info *fs_info = root->fs_info;
4216 struct inode *inode;
4219 struct btrfs_key key;
4220 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
4222 unsigned pg_offset = offset & ~PAGE_CACHE_MASK;
4225 key.objectid = sctx->cur_ino;
4226 key.type = BTRFS_INODE_ITEM_KEY;
4229 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4231 return PTR_ERR(inode);
4233 if (offset + len > i_size_read(inode)) {
4234 if (offset > i_size_read(inode))
4237 len = offset - i_size_read(inode);
4242 last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT;
4244 /* initial readahead */
4245 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4246 file_ra_state_init(&sctx->ra, inode->i_mapping);
4247 btrfs_force_ra(inode->i_mapping, &sctx->ra, NULL, index,
4248 last_index - index + 1);
4250 while (index <= last_index) {
4251 unsigned cur_len = min_t(unsigned, len,
4252 PAGE_CACHE_SIZE - pg_offset);
4253 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
4259 if (!PageUptodate(page)) {
4260 btrfs_readpage(NULL, page);
4262 if (!PageUptodate(page)) {
4264 page_cache_release(page);
4271 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4274 page_cache_release(page);
4286 * Read some bytes from the current inode/file and send a write command to
4289 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4293 ssize_t num_read = 0;
4295 p = fs_path_alloc();
4299 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
4301 num_read = fill_read_buf(sctx, offset, len);
4302 if (num_read <= 0) {
4308 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4312 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4316 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4317 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4318 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
4320 ret = send_cmd(sctx);
4331 * Send a clone command to user space.
4333 static int send_clone(struct send_ctx *sctx,
4334 u64 offset, u32 len,
4335 struct clone_root *clone_root)
4341 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
4342 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
4343 clone_root->root->objectid, clone_root->ino,
4344 clone_root->offset);
4346 p = fs_path_alloc();
4350 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4354 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4358 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4359 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4360 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4362 if (clone_root->root == sctx->send_root) {
4363 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
4364 &gen, NULL, NULL, NULL, NULL);
4367 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4369 ret = get_inode_path(clone_root->root, clone_root->ino, p);
4374 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4375 clone_root->root->root_item.uuid);
4376 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
4377 le64_to_cpu(clone_root->root->root_item.ctransid));
4378 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4379 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4380 clone_root->offset);
4382 ret = send_cmd(sctx);
4391 * Send an update extent command to user space.
4393 static int send_update_extent(struct send_ctx *sctx,
4394 u64 offset, u32 len)
4399 p = fs_path_alloc();
4403 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4407 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4411 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4412 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4413 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4415 ret = send_cmd(sctx);
4423 static int send_hole(struct send_ctx *sctx, u64 end)
4425 struct fs_path *p = NULL;
4426 u64 offset = sctx->cur_inode_last_extent;
4430 p = fs_path_alloc();
4433 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4435 goto tlv_put_failure;
4436 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
4437 while (offset < end) {
4438 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
4440 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4443 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4444 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4445 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
4446 ret = send_cmd(sctx);
4456 static int send_write_or_clone(struct send_ctx *sctx,
4457 struct btrfs_path *path,
4458 struct btrfs_key *key,
4459 struct clone_root *clone_root)
4462 struct btrfs_file_extent_item *ei;
4463 u64 offset = key->offset;
4468 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
4470 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4471 struct btrfs_file_extent_item);
4472 type = btrfs_file_extent_type(path->nodes[0], ei);
4473 if (type == BTRFS_FILE_EXTENT_INLINE) {
4474 len = btrfs_file_extent_inline_len(path->nodes[0],
4475 path->slots[0], ei);
4477 * it is possible the inline item won't cover the whole page,
4478 * but there may be items after this page. Make
4479 * sure to send the whole thing
4481 len = PAGE_CACHE_ALIGN(len);
4483 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
4486 if (offset + len > sctx->cur_inode_size)
4487 len = sctx->cur_inode_size - offset;
4493 if (clone_root && IS_ALIGNED(offset + len, bs)) {
4494 ret = send_clone(sctx, offset, len, clone_root);
4495 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
4496 ret = send_update_extent(sctx, offset, len);
4500 if (l > BTRFS_SEND_READ_SIZE)
4501 l = BTRFS_SEND_READ_SIZE;
4502 ret = send_write(sctx, pos + offset, l);
4515 static int is_extent_unchanged(struct send_ctx *sctx,
4516 struct btrfs_path *left_path,
4517 struct btrfs_key *ekey)
4520 struct btrfs_key key;
4521 struct btrfs_path *path = NULL;
4522 struct extent_buffer *eb;
4524 struct btrfs_key found_key;
4525 struct btrfs_file_extent_item *ei;
4530 u64 left_offset_fixed;
4538 path = alloc_path_for_send();
4542 eb = left_path->nodes[0];
4543 slot = left_path->slots[0];
4544 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4545 left_type = btrfs_file_extent_type(eb, ei);
4547 if (left_type != BTRFS_FILE_EXTENT_REG) {
4551 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4552 left_len = btrfs_file_extent_num_bytes(eb, ei);
4553 left_offset = btrfs_file_extent_offset(eb, ei);
4554 left_gen = btrfs_file_extent_generation(eb, ei);
4557 * Following comments will refer to these graphics. L is the left
4558 * extents which we are checking at the moment. 1-8 are the right
4559 * extents that we iterate.
4562 * |-1-|-2a-|-3-|-4-|-5-|-6-|
4565 * |--1--|-2b-|...(same as above)
4567 * Alternative situation. Happens on files where extents got split.
4569 * |-----------7-----------|-6-|
4571 * Alternative situation. Happens on files which got larger.
4574 * Nothing follows after 8.
4577 key.objectid = ekey->objectid;
4578 key.type = BTRFS_EXTENT_DATA_KEY;
4579 key.offset = ekey->offset;
4580 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
4589 * Handle special case where the right side has no extents at all.
4591 eb = path->nodes[0];
4592 slot = path->slots[0];
4593 btrfs_item_key_to_cpu(eb, &found_key, slot);
4594 if (found_key.objectid != key.objectid ||
4595 found_key.type != key.type) {
4596 /* If we're a hole then just pretend nothing changed */
4597 ret = (left_disknr) ? 0 : 1;
4602 * We're now on 2a, 2b or 7.
4605 while (key.offset < ekey->offset + left_len) {
4606 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
4607 right_type = btrfs_file_extent_type(eb, ei);
4608 if (right_type != BTRFS_FILE_EXTENT_REG) {
4613 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
4614 right_len = btrfs_file_extent_num_bytes(eb, ei);
4615 right_offset = btrfs_file_extent_offset(eb, ei);
4616 right_gen = btrfs_file_extent_generation(eb, ei);
4619 * Are we at extent 8? If yes, we know the extent is changed.
4620 * This may only happen on the first iteration.
4622 if (found_key.offset + right_len <= ekey->offset) {
4623 /* If we're a hole just pretend nothing changed */
4624 ret = (left_disknr) ? 0 : 1;
4628 left_offset_fixed = left_offset;
4629 if (key.offset < ekey->offset) {
4630 /* Fix the right offset for 2a and 7. */
4631 right_offset += ekey->offset - key.offset;
4633 /* Fix the left offset for all behind 2a and 2b */
4634 left_offset_fixed += key.offset - ekey->offset;
4638 * Check if we have the same extent.
4640 if (left_disknr != right_disknr ||
4641 left_offset_fixed != right_offset ||
4642 left_gen != right_gen) {
4648 * Go to the next extent.
4650 ret = btrfs_next_item(sctx->parent_root, path);
4654 eb = path->nodes[0];
4655 slot = path->slots[0];
4656 btrfs_item_key_to_cpu(eb, &found_key, slot);
4658 if (ret || found_key.objectid != key.objectid ||
4659 found_key.type != key.type) {
4660 key.offset += right_len;
4663 if (found_key.offset != key.offset + right_len) {
4671 * We're now behind the left extent (treat as unchanged) or at the end
4672 * of the right side (treat as changed).
4674 if (key.offset >= ekey->offset + left_len)
4681 btrfs_free_path(path);
4685 static int get_last_extent(struct send_ctx *sctx, u64 offset)
4687 struct btrfs_path *path;
4688 struct btrfs_root *root = sctx->send_root;
4689 struct btrfs_file_extent_item *fi;
4690 struct btrfs_key key;
4695 path = alloc_path_for_send();
4699 sctx->cur_inode_last_extent = 0;
4701 key.objectid = sctx->cur_ino;
4702 key.type = BTRFS_EXTENT_DATA_KEY;
4703 key.offset = offset;
4704 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
4708 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4709 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
4712 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4713 struct btrfs_file_extent_item);
4714 type = btrfs_file_extent_type(path->nodes[0], fi);
4715 if (type == BTRFS_FILE_EXTENT_INLINE) {
4716 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4717 path->slots[0], fi);
4718 extent_end = ALIGN(key.offset + size,
4719 sctx->send_root->sectorsize);
4721 extent_end = key.offset +
4722 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4724 sctx->cur_inode_last_extent = extent_end;
4726 btrfs_free_path(path);
4730 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
4731 struct btrfs_key *key)
4733 struct btrfs_file_extent_item *fi;
4738 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
4741 if (sctx->cur_inode_last_extent == (u64)-1) {
4742 ret = get_last_extent(sctx, key->offset - 1);
4747 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4748 struct btrfs_file_extent_item);
4749 type = btrfs_file_extent_type(path->nodes[0], fi);
4750 if (type == BTRFS_FILE_EXTENT_INLINE) {
4751 u64 size = btrfs_file_extent_inline_len(path->nodes[0],
4752 path->slots[0], fi);
4753 extent_end = ALIGN(key->offset + size,
4754 sctx->send_root->sectorsize);
4756 extent_end = key->offset +
4757 btrfs_file_extent_num_bytes(path->nodes[0], fi);
4760 if (path->slots[0] == 0 &&
4761 sctx->cur_inode_last_extent < key->offset) {
4763 * We might have skipped entire leafs that contained only
4764 * file extent items for our current inode. These leafs have
4765 * a generation number smaller (older) than the one in the
4766 * current leaf and the leaf our last extent came from, and
4767 * are located between these 2 leafs.
4769 ret = get_last_extent(sctx, key->offset - 1);
4774 if (sctx->cur_inode_last_extent < key->offset)
4775 ret = send_hole(sctx, key->offset);
4776 sctx->cur_inode_last_extent = extent_end;
4780 static int process_extent(struct send_ctx *sctx,
4781 struct btrfs_path *path,
4782 struct btrfs_key *key)
4784 struct clone_root *found_clone = NULL;
4787 if (S_ISLNK(sctx->cur_inode_mode))
4790 if (sctx->parent_root && !sctx->cur_inode_new) {
4791 ret = is_extent_unchanged(sctx, path, key);
4799 struct btrfs_file_extent_item *ei;
4802 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4803 struct btrfs_file_extent_item);
4804 type = btrfs_file_extent_type(path->nodes[0], ei);
4805 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4806 type == BTRFS_FILE_EXTENT_REG) {
4808 * The send spec does not have a prealloc command yet,
4809 * so just leave a hole for prealloc'ed extents until
4810 * we have enough commands queued up to justify rev'ing
4813 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4818 /* Have a hole, just skip it. */
4819 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4826 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4827 sctx->cur_inode_size, &found_clone);
4828 if (ret != -ENOENT && ret < 0)
4831 ret = send_write_or_clone(sctx, path, key, found_clone);
4835 ret = maybe_send_hole(sctx, path, key);
4840 static int process_all_extents(struct send_ctx *sctx)
4843 struct btrfs_root *root;
4844 struct btrfs_path *path;
4845 struct btrfs_key key;
4846 struct btrfs_key found_key;
4847 struct extent_buffer *eb;
4850 root = sctx->send_root;
4851 path = alloc_path_for_send();
4855 key.objectid = sctx->cmp_key->objectid;
4856 key.type = BTRFS_EXTENT_DATA_KEY;
4858 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4863 eb = path->nodes[0];
4864 slot = path->slots[0];
4866 if (slot >= btrfs_header_nritems(eb)) {
4867 ret = btrfs_next_leaf(root, path);
4870 } else if (ret > 0) {
4877 btrfs_item_key_to_cpu(eb, &found_key, slot);
4879 if (found_key.objectid != key.objectid ||
4880 found_key.type != key.type) {
4885 ret = process_extent(sctx, path, &found_key);
4893 btrfs_free_path(path);
4897 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
4899 int *refs_processed)
4903 if (sctx->cur_ino == 0)
4905 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4906 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4908 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4911 ret = process_recorded_refs(sctx, pending_move);
4915 *refs_processed = 1;
4920 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4931 int pending_move = 0;
4932 int refs_processed = 0;
4934 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
4940 * We have processed the refs and thus need to advance send_progress.
4941 * Now, calls to get_cur_xxx will take the updated refs of the current
4942 * inode into account.
4944 * On the other hand, if our current inode is a directory and couldn't
4945 * be moved/renamed because its parent was renamed/moved too and it has
4946 * a higher inode number, we can only move/rename our current inode
4947 * after we moved/renamed its parent. Therefore in this case operate on
4948 * the old path (pre move/rename) of our current inode, and the
4949 * move/rename will be performed later.
4951 if (refs_processed && !pending_move)
4952 sctx->send_progress = sctx->cur_ino + 1;
4954 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4956 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4959 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4960 &left_mode, &left_uid, &left_gid, NULL);
4964 if (!sctx->parent_root || sctx->cur_inode_new) {
4966 if (!S_ISLNK(sctx->cur_inode_mode))
4969 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4970 NULL, NULL, &right_mode, &right_uid,
4975 if (left_uid != right_uid || left_gid != right_gid)
4977 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4981 if (S_ISREG(sctx->cur_inode_mode)) {
4982 if (need_send_hole(sctx)) {
4983 if (sctx->cur_inode_last_extent == (u64)-1 ||
4984 sctx->cur_inode_last_extent <
4985 sctx->cur_inode_size) {
4986 ret = get_last_extent(sctx, (u64)-1);
4990 if (sctx->cur_inode_last_extent <
4991 sctx->cur_inode_size) {
4992 ret = send_hole(sctx, sctx->cur_inode_size);
4997 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4998 sctx->cur_inode_size);
5004 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5005 left_uid, left_gid);
5010 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5017 * If other directory inodes depended on our current directory
5018 * inode's move/rename, now do their move/rename operations.
5020 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5021 ret = apply_children_dir_moves(sctx);
5025 * Need to send that every time, no matter if it actually
5026 * changed between the two trees as we have done changes to
5027 * the inode before. If our inode is a directory and it's
5028 * waiting to be moved/renamed, we will send its utimes when
5029 * it's moved/renamed, therefore we don't need to do it here.
5031 sctx->send_progress = sctx->cur_ino + 1;
5032 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5041 static int changed_inode(struct send_ctx *sctx,
5042 enum btrfs_compare_tree_result result)
5045 struct btrfs_key *key = sctx->cmp_key;
5046 struct btrfs_inode_item *left_ii = NULL;
5047 struct btrfs_inode_item *right_ii = NULL;
5051 sctx->cur_ino = key->objectid;
5052 sctx->cur_inode_new_gen = 0;
5053 sctx->cur_inode_last_extent = (u64)-1;
5056 * Set send_progress to current inode. This will tell all get_cur_xxx
5057 * functions that the current inode's refs are not updated yet. Later,
5058 * when process_recorded_refs is finished, it is set to cur_ino + 1.
5060 sctx->send_progress = sctx->cur_ino;
5062 if (result == BTRFS_COMPARE_TREE_NEW ||
5063 result == BTRFS_COMPARE_TREE_CHANGED) {
5064 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
5065 sctx->left_path->slots[0],
5066 struct btrfs_inode_item);
5067 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
5070 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5071 sctx->right_path->slots[0],
5072 struct btrfs_inode_item);
5073 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5076 if (result == BTRFS_COMPARE_TREE_CHANGED) {
5077 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
5078 sctx->right_path->slots[0],
5079 struct btrfs_inode_item);
5081 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
5085 * The cur_ino = root dir case is special here. We can't treat
5086 * the inode as deleted+reused because it would generate a
5087 * stream that tries to delete/mkdir the root dir.
5089 if (left_gen != right_gen &&
5090 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5091 sctx->cur_inode_new_gen = 1;
5094 if (result == BTRFS_COMPARE_TREE_NEW) {
5095 sctx->cur_inode_gen = left_gen;
5096 sctx->cur_inode_new = 1;
5097 sctx->cur_inode_deleted = 0;
5098 sctx->cur_inode_size = btrfs_inode_size(
5099 sctx->left_path->nodes[0], left_ii);
5100 sctx->cur_inode_mode = btrfs_inode_mode(
5101 sctx->left_path->nodes[0], left_ii);
5102 sctx->cur_inode_rdev = btrfs_inode_rdev(
5103 sctx->left_path->nodes[0], left_ii);
5104 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
5105 ret = send_create_inode_if_needed(sctx);
5106 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
5107 sctx->cur_inode_gen = right_gen;
5108 sctx->cur_inode_new = 0;
5109 sctx->cur_inode_deleted = 1;
5110 sctx->cur_inode_size = btrfs_inode_size(
5111 sctx->right_path->nodes[0], right_ii);
5112 sctx->cur_inode_mode = btrfs_inode_mode(
5113 sctx->right_path->nodes[0], right_ii);
5114 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
5116 * We need to do some special handling in case the inode was
5117 * reported as changed with a changed generation number. This
5118 * means that the original inode was deleted and new inode
5119 * reused the same inum. So we have to treat the old inode as
5120 * deleted and the new one as new.
5122 if (sctx->cur_inode_new_gen) {
5124 * First, process the inode as if it was deleted.
5126 sctx->cur_inode_gen = right_gen;
5127 sctx->cur_inode_new = 0;
5128 sctx->cur_inode_deleted = 1;
5129 sctx->cur_inode_size = btrfs_inode_size(
5130 sctx->right_path->nodes[0], right_ii);
5131 sctx->cur_inode_mode = btrfs_inode_mode(
5132 sctx->right_path->nodes[0], right_ii);
5133 ret = process_all_refs(sctx,
5134 BTRFS_COMPARE_TREE_DELETED);
5139 * Now process the inode as if it was new.
5141 sctx->cur_inode_gen = left_gen;
5142 sctx->cur_inode_new = 1;
5143 sctx->cur_inode_deleted = 0;
5144 sctx->cur_inode_size = btrfs_inode_size(
5145 sctx->left_path->nodes[0], left_ii);
5146 sctx->cur_inode_mode = btrfs_inode_mode(
5147 sctx->left_path->nodes[0], left_ii);
5148 sctx->cur_inode_rdev = btrfs_inode_rdev(
5149 sctx->left_path->nodes[0], left_ii);
5150 ret = send_create_inode_if_needed(sctx);
5154 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
5158 * Advance send_progress now as we did not get into
5159 * process_recorded_refs_if_needed in the new_gen case.
5161 sctx->send_progress = sctx->cur_ino + 1;
5164 * Now process all extents and xattrs of the inode as if
5165 * they were all new.
5167 ret = process_all_extents(sctx);
5170 ret = process_all_new_xattrs(sctx);
5174 sctx->cur_inode_gen = left_gen;
5175 sctx->cur_inode_new = 0;
5176 sctx->cur_inode_new_gen = 0;
5177 sctx->cur_inode_deleted = 0;
5178 sctx->cur_inode_size = btrfs_inode_size(
5179 sctx->left_path->nodes[0], left_ii);
5180 sctx->cur_inode_mode = btrfs_inode_mode(
5181 sctx->left_path->nodes[0], left_ii);
5190 * We have to process new refs before deleted refs, but compare_trees gives us
5191 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
5192 * first and later process them in process_recorded_refs.
5193 * For the cur_inode_new_gen case, we skip recording completely because
5194 * changed_inode did already initiate processing of refs. The reason for this is
5195 * that in this case, compare_tree actually compares the refs of 2 different
5196 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
5197 * refs of the right tree as deleted and all refs of the left tree as new.
5199 static int changed_ref(struct send_ctx *sctx,
5200 enum btrfs_compare_tree_result result)
5204 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5206 if (!sctx->cur_inode_new_gen &&
5207 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
5208 if (result == BTRFS_COMPARE_TREE_NEW)
5209 ret = record_new_ref(sctx);
5210 else if (result == BTRFS_COMPARE_TREE_DELETED)
5211 ret = record_deleted_ref(sctx);
5212 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5213 ret = record_changed_ref(sctx);
5220 * Process new/deleted/changed xattrs. We skip processing in the
5221 * cur_inode_new_gen case because changed_inode did already initiate processing
5222 * of xattrs. The reason is the same as in changed_ref
5224 static int changed_xattr(struct send_ctx *sctx,
5225 enum btrfs_compare_tree_result result)
5229 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5231 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5232 if (result == BTRFS_COMPARE_TREE_NEW)
5233 ret = process_new_xattr(sctx);
5234 else if (result == BTRFS_COMPARE_TREE_DELETED)
5235 ret = process_deleted_xattr(sctx);
5236 else if (result == BTRFS_COMPARE_TREE_CHANGED)
5237 ret = process_changed_xattr(sctx);
5244 * Process new/deleted/changed extents. We skip processing in the
5245 * cur_inode_new_gen case because changed_inode did already initiate processing
5246 * of extents. The reason is the same as in changed_ref
5248 static int changed_extent(struct send_ctx *sctx,
5249 enum btrfs_compare_tree_result result)
5253 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
5255 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
5256 if (result != BTRFS_COMPARE_TREE_DELETED)
5257 ret = process_extent(sctx, sctx->left_path,
5264 static int dir_changed(struct send_ctx *sctx, u64 dir)
5266 u64 orig_gen, new_gen;
5269 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
5274 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
5279 return (orig_gen != new_gen) ? 1 : 0;
5282 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
5283 struct btrfs_key *key)
5285 struct btrfs_inode_extref *extref;
5286 struct extent_buffer *leaf;
5287 u64 dirid = 0, last_dirid = 0;
5294 /* Easy case, just check this one dirid */
5295 if (key->type == BTRFS_INODE_REF_KEY) {
5296 dirid = key->offset;
5298 ret = dir_changed(sctx, dirid);
5302 leaf = path->nodes[0];
5303 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
5304 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
5305 while (cur_offset < item_size) {
5306 extref = (struct btrfs_inode_extref *)(ptr +
5308 dirid = btrfs_inode_extref_parent(leaf, extref);
5309 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
5310 cur_offset += ref_name_len + sizeof(*extref);
5311 if (dirid == last_dirid)
5313 ret = dir_changed(sctx, dirid);
5323 * Updates compare related fields in sctx and simply forwards to the actual
5324 * changed_xxx functions.
5326 static int changed_cb(struct btrfs_root *left_root,
5327 struct btrfs_root *right_root,
5328 struct btrfs_path *left_path,
5329 struct btrfs_path *right_path,
5330 struct btrfs_key *key,
5331 enum btrfs_compare_tree_result result,
5335 struct send_ctx *sctx = ctx;
5337 if (result == BTRFS_COMPARE_TREE_SAME) {
5338 if (key->type == BTRFS_INODE_REF_KEY ||
5339 key->type == BTRFS_INODE_EXTREF_KEY) {
5340 ret = compare_refs(sctx, left_path, key);
5345 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
5346 return maybe_send_hole(sctx, left_path, key);
5350 result = BTRFS_COMPARE_TREE_CHANGED;
5354 sctx->left_path = left_path;
5355 sctx->right_path = right_path;
5356 sctx->cmp_key = key;
5358 ret = finish_inode_if_needed(sctx, 0);
5362 /* Ignore non-FS objects */
5363 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
5364 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
5367 if (key->type == BTRFS_INODE_ITEM_KEY)
5368 ret = changed_inode(sctx, result);
5369 else if (key->type == BTRFS_INODE_REF_KEY ||
5370 key->type == BTRFS_INODE_EXTREF_KEY)
5371 ret = changed_ref(sctx, result);
5372 else if (key->type == BTRFS_XATTR_ITEM_KEY)
5373 ret = changed_xattr(sctx, result);
5374 else if (key->type == BTRFS_EXTENT_DATA_KEY)
5375 ret = changed_extent(sctx, result);
5381 static int full_send_tree(struct send_ctx *sctx)
5384 struct btrfs_root *send_root = sctx->send_root;
5385 struct btrfs_key key;
5386 struct btrfs_key found_key;
5387 struct btrfs_path *path;
5388 struct extent_buffer *eb;
5391 path = alloc_path_for_send();
5395 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
5396 key.type = BTRFS_INODE_ITEM_KEY;
5399 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
5406 eb = path->nodes[0];
5407 slot = path->slots[0];
5408 btrfs_item_key_to_cpu(eb, &found_key, slot);
5410 ret = changed_cb(send_root, NULL, path, NULL,
5411 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
5415 key.objectid = found_key.objectid;
5416 key.type = found_key.type;
5417 key.offset = found_key.offset + 1;
5419 ret = btrfs_next_item(send_root, path);
5429 ret = finish_inode_if_needed(sctx, 1);
5432 btrfs_free_path(path);
5436 static int send_subvol(struct send_ctx *sctx)
5440 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
5441 ret = send_header(sctx);
5446 ret = send_subvol_begin(sctx);
5450 if (sctx->parent_root) {
5451 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
5455 ret = finish_inode_if_needed(sctx, 1);
5459 ret = full_send_tree(sctx);
5465 free_recorded_refs(sctx);
5469 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
5471 spin_lock(&root->root_item_lock);
5472 root->send_in_progress--;
5474 * Not much left to do, we don't know why it's unbalanced and
5475 * can't blindly reset it to 0.
5477 if (root->send_in_progress < 0)
5478 btrfs_err(root->fs_info,
5479 "send_in_progres unbalanced %d root %llu",
5480 root->send_in_progress, root->root_key.objectid);
5481 spin_unlock(&root->root_item_lock);
5484 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
5487 struct btrfs_root *send_root;
5488 struct btrfs_root *clone_root;
5489 struct btrfs_fs_info *fs_info;
5490 struct btrfs_ioctl_send_args *arg = NULL;
5491 struct btrfs_key key;
5492 struct send_ctx *sctx = NULL;
5494 u64 *clone_sources_tmp = NULL;
5495 int clone_sources_to_rollback = 0;
5496 int sort_clone_roots = 0;
5499 if (!capable(CAP_SYS_ADMIN))
5502 send_root = BTRFS_I(file_inode(mnt_file))->root;
5503 fs_info = send_root->fs_info;
5506 * The subvolume must remain read-only during send, protect against
5507 * making it RW. This also protects against deletion.
5509 spin_lock(&send_root->root_item_lock);
5510 send_root->send_in_progress++;
5511 spin_unlock(&send_root->root_item_lock);
5514 * This is done when we lookup the root, it should already be complete
5515 * by the time we get here.
5517 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
5520 * Userspace tools do the checks and warn the user if it's
5523 if (!btrfs_root_readonly(send_root)) {
5528 arg = memdup_user(arg_, sizeof(*arg));
5535 if (!access_ok(VERIFY_READ, arg->clone_sources,
5536 sizeof(*arg->clone_sources) *
5537 arg->clone_sources_count)) {
5542 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
5547 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
5553 INIT_LIST_HEAD(&sctx->new_refs);
5554 INIT_LIST_HEAD(&sctx->deleted_refs);
5555 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
5556 INIT_LIST_HEAD(&sctx->name_cache_list);
5558 sctx->flags = arg->flags;
5560 sctx->send_filp = fget(arg->send_fd);
5561 if (!sctx->send_filp) {
5566 sctx->send_root = send_root;
5568 * Unlikely but possible, if the subvolume is marked for deletion but
5569 * is slow to remove the directory entry, send can still be started
5571 if (btrfs_root_dead(sctx->send_root)) {
5576 sctx->clone_roots_cnt = arg->clone_sources_count;
5578 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
5579 sctx->send_buf = vmalloc(sctx->send_max_size);
5580 if (!sctx->send_buf) {
5585 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
5586 if (!sctx->read_buf) {
5591 sctx->pending_dir_moves = RB_ROOT;
5592 sctx->waiting_dir_moves = RB_ROOT;
5593 sctx->orphan_dirs = RB_ROOT;
5595 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
5596 (arg->clone_sources_count + 1));
5597 if (!sctx->clone_roots) {
5602 if (arg->clone_sources_count) {
5603 clone_sources_tmp = vmalloc(arg->clone_sources_count *
5604 sizeof(*arg->clone_sources));
5605 if (!clone_sources_tmp) {
5610 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
5611 arg->clone_sources_count *
5612 sizeof(*arg->clone_sources));
5618 for (i = 0; i < arg->clone_sources_count; i++) {
5619 key.objectid = clone_sources_tmp[i];
5620 key.type = BTRFS_ROOT_ITEM_KEY;
5621 key.offset = (u64)-1;
5623 index = srcu_read_lock(&fs_info->subvol_srcu);
5625 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
5626 if (IS_ERR(clone_root)) {
5627 srcu_read_unlock(&fs_info->subvol_srcu, index);
5628 ret = PTR_ERR(clone_root);
5631 clone_sources_to_rollback = i + 1;
5632 spin_lock(&clone_root->root_item_lock);
5633 clone_root->send_in_progress++;
5634 if (!btrfs_root_readonly(clone_root)) {
5635 spin_unlock(&clone_root->root_item_lock);
5636 srcu_read_unlock(&fs_info->subvol_srcu, index);
5640 spin_unlock(&clone_root->root_item_lock);
5641 srcu_read_unlock(&fs_info->subvol_srcu, index);
5643 sctx->clone_roots[i].root = clone_root;
5645 vfree(clone_sources_tmp);
5646 clone_sources_tmp = NULL;
5649 if (arg->parent_root) {
5650 key.objectid = arg->parent_root;
5651 key.type = BTRFS_ROOT_ITEM_KEY;
5652 key.offset = (u64)-1;
5654 index = srcu_read_lock(&fs_info->subvol_srcu);
5656 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
5657 if (IS_ERR(sctx->parent_root)) {
5658 srcu_read_unlock(&fs_info->subvol_srcu, index);
5659 ret = PTR_ERR(sctx->parent_root);
5663 spin_lock(&sctx->parent_root->root_item_lock);
5664 sctx->parent_root->send_in_progress++;
5665 if (!btrfs_root_readonly(sctx->parent_root) ||
5666 btrfs_root_dead(sctx->parent_root)) {
5667 spin_unlock(&sctx->parent_root->root_item_lock);
5668 srcu_read_unlock(&fs_info->subvol_srcu, index);
5672 spin_unlock(&sctx->parent_root->root_item_lock);
5674 srcu_read_unlock(&fs_info->subvol_srcu, index);
5678 * Clones from send_root are allowed, but only if the clone source
5679 * is behind the current send position. This is checked while searching
5680 * for possible clone sources.
5682 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
5684 /* We do a bsearch later */
5685 sort(sctx->clone_roots, sctx->clone_roots_cnt,
5686 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
5688 sort_clone_roots = 1;
5690 current->journal_info = (void *)BTRFS_SEND_TRANS_STUB;
5691 ret = send_subvol(sctx);
5692 current->journal_info = NULL;
5696 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
5697 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
5700 ret = send_cmd(sctx);
5706 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
5707 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
5709 struct pending_dir_move *pm;
5711 n = rb_first(&sctx->pending_dir_moves);
5712 pm = rb_entry(n, struct pending_dir_move, node);
5713 while (!list_empty(&pm->list)) {
5714 struct pending_dir_move *pm2;
5716 pm2 = list_first_entry(&pm->list,
5717 struct pending_dir_move, list);
5718 free_pending_move(sctx, pm2);
5720 free_pending_move(sctx, pm);
5723 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
5724 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
5726 struct waiting_dir_move *dm;
5728 n = rb_first(&sctx->waiting_dir_moves);
5729 dm = rb_entry(n, struct waiting_dir_move, node);
5730 rb_erase(&dm->node, &sctx->waiting_dir_moves);
5734 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
5735 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
5737 struct orphan_dir_info *odi;
5739 n = rb_first(&sctx->orphan_dirs);
5740 odi = rb_entry(n, struct orphan_dir_info, node);
5741 free_orphan_dir_info(sctx, odi);
5744 if (sort_clone_roots) {
5745 for (i = 0; i < sctx->clone_roots_cnt; i++)
5746 btrfs_root_dec_send_in_progress(
5747 sctx->clone_roots[i].root);
5749 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
5750 btrfs_root_dec_send_in_progress(
5751 sctx->clone_roots[i].root);
5753 btrfs_root_dec_send_in_progress(send_root);
5755 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
5756 btrfs_root_dec_send_in_progress(sctx->parent_root);
5759 vfree(clone_sources_tmp);
5762 if (sctx->send_filp)
5763 fput(sctx->send_filp);
5765 vfree(sctx->clone_roots);
5766 vfree(sctx->send_buf);
5767 vfree(sctx->read_buf);
5769 name_cache_free(sctx);