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/crc32c.h>
28 #include <linux/vmalloc.h>
34 #include "btrfs_inode.h"
35 #include "transaction.h"
37 static int g_verbose = 0;
39 #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__)
42 * A fs_path is a helper to dynamically build path names with unknown size.
43 * It reallocates the internal buffer on demand.
44 * It allows fast adding of path elements on the right side (normal path) and
45 * fast adding to the left side (reversed path). A reversed path can also be
46 * unreversed if needed.
57 unsigned int reversed:1;
58 unsigned int virtual_mem:1;
64 #define FS_PATH_INLINE_SIZE \
65 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
68 /* reused for each extent */
70 struct btrfs_root *root;
77 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
78 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
81 struct file *send_filp;
87 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
88 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
92 struct btrfs_root *send_root;
93 struct btrfs_root *parent_root;
94 struct clone_root *clone_roots;
97 /* current state of the compare_tree call */
98 struct btrfs_path *left_path;
99 struct btrfs_path *right_path;
100 struct btrfs_key *cmp_key;
103 * infos of the currently processed inode. In case of deleted inodes,
104 * these are the values from the deleted inode.
109 int cur_inode_new_gen;
110 int cur_inode_deleted;
116 struct list_head new_refs;
117 struct list_head deleted_refs;
119 struct radix_tree_root name_cache;
120 struct list_head name_cache_list;
123 struct file *cur_inode_filp;
127 struct name_cache_entry {
128 struct list_head list;
130 * radix_tree has only 32bit entries but we need to handle 64bit inums.
131 * We use the lower 32bit of the 64bit inum to store it in the tree. If
132 * more then one inum would fall into the same entry, we use radix_list
133 * to store the additional entries. radix_list is also used to store
134 * entries where two entries have the same inum but different
137 struct list_head radix_list;
143 int need_later_update;
148 static void fs_path_reset(struct fs_path *p)
151 p->start = p->buf + p->buf_len - 1;
161 static struct fs_path *fs_path_alloc(void)
165 p = kmalloc(sizeof(*p), GFP_NOFS);
170 p->buf = p->inline_buf;
171 p->buf_len = FS_PATH_INLINE_SIZE;
176 static struct fs_path *fs_path_alloc_reversed(void)
188 static void fs_path_free(struct fs_path *p)
192 if (p->buf != p->inline_buf) {
201 static int fs_path_len(struct fs_path *p)
203 return p->end - p->start;
206 static int fs_path_ensure_buf(struct fs_path *p, int len)
214 if (p->buf_len >= len)
217 path_len = p->end - p->start;
218 old_buf_len = p->buf_len;
219 len = PAGE_ALIGN(len);
221 if (p->buf == p->inline_buf) {
222 tmp_buf = kmalloc(len, GFP_NOFS);
224 tmp_buf = vmalloc(len);
229 memcpy(tmp_buf, p->buf, p->buf_len);
233 if (p->virtual_mem) {
234 tmp_buf = vmalloc(len);
237 memcpy(tmp_buf, p->buf, p->buf_len);
240 tmp_buf = krealloc(p->buf, len, GFP_NOFS);
242 tmp_buf = vmalloc(len);
245 memcpy(tmp_buf, p->buf, p->buf_len);
254 tmp_buf = p->buf + old_buf_len - path_len - 1;
255 p->end = p->buf + p->buf_len - 1;
256 p->start = p->end - path_len;
257 memmove(p->start, tmp_buf, path_len + 1);
260 p->end = p->start + path_len;
265 static int fs_path_prepare_for_add(struct fs_path *p, int name_len)
270 new_len = p->end - p->start + name_len;
271 if (p->start != p->end)
273 ret = fs_path_ensure_buf(p, new_len);
278 if (p->start != p->end)
280 p->start -= name_len;
281 p->prepared = p->start;
283 if (p->start != p->end)
285 p->prepared = p->end;
294 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
298 ret = fs_path_prepare_for_add(p, name_len);
301 memcpy(p->prepared, name, name_len);
308 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
312 ret = fs_path_prepare_for_add(p, p2->end - p2->start);
315 memcpy(p->prepared, p2->start, p2->end - p2->start);
322 static int fs_path_add_from_extent_buffer(struct fs_path *p,
323 struct extent_buffer *eb,
324 unsigned long off, int len)
328 ret = fs_path_prepare_for_add(p, len);
332 read_extent_buffer(eb, p->prepared, off, len);
340 static void fs_path_remove(struct fs_path *p)
343 while (p->start != p->end && *p->end != '/')
349 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
353 p->reversed = from->reversed;
356 ret = fs_path_add_path(p, from);
362 static void fs_path_unreverse(struct fs_path *p)
371 len = p->end - p->start;
373 p->end = p->start + len;
374 memmove(p->start, tmp, len + 1);
378 static struct btrfs_path *alloc_path_for_send(void)
380 struct btrfs_path *path;
382 path = btrfs_alloc_path();
385 path->search_commit_root = 1;
386 path->skip_locking = 1;
390 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
400 ret = vfs_write(filp, (char *)buf + pos, len - pos, off);
401 /* TODO handle that correctly */
402 /*if (ret == -ERESTARTSYS) {
421 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
423 struct btrfs_tlv_header *hdr;
424 int total_len = sizeof(*hdr) + len;
425 int left = sctx->send_max_size - sctx->send_size;
427 if (unlikely(left < total_len))
430 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
431 hdr->tlv_type = cpu_to_le16(attr);
432 hdr->tlv_len = cpu_to_le16(len);
433 memcpy(hdr + 1, data, len);
434 sctx->send_size += total_len;
440 static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value)
442 return tlv_put(sctx, attr, &value, sizeof(value));
445 static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value)
447 __le16 tmp = cpu_to_le16(value);
448 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
451 static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value)
453 __le32 tmp = cpu_to_le32(value);
454 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
458 static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value)
460 __le64 tmp = cpu_to_le64(value);
461 return tlv_put(sctx, attr, &tmp, sizeof(tmp));
464 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
465 const char *str, int len)
469 return tlv_put(sctx, attr, str, len);
472 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
475 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
479 static int tlv_put_timespec(struct send_ctx *sctx, u16 attr,
482 struct btrfs_timespec bts;
483 bts.sec = cpu_to_le64(ts->tv_sec);
484 bts.nsec = cpu_to_le32(ts->tv_nsec);
485 return tlv_put(sctx, attr, &bts, sizeof(bts));
489 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
490 struct extent_buffer *eb,
491 struct btrfs_timespec *ts)
493 struct btrfs_timespec bts;
494 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
495 return tlv_put(sctx, attr, &bts, sizeof(bts));
499 #define TLV_PUT(sctx, attrtype, attrlen, data) \
501 ret = tlv_put(sctx, attrtype, attrlen, data); \
503 goto tlv_put_failure; \
506 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
508 ret = tlv_put_u##bits(sctx, attrtype, value); \
510 goto tlv_put_failure; \
513 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
514 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
515 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
516 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
517 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
519 ret = tlv_put_string(sctx, attrtype, str, len); \
521 goto tlv_put_failure; \
523 #define TLV_PUT_PATH(sctx, attrtype, p) \
525 ret = tlv_put_string(sctx, attrtype, p->start, \
526 p->end - p->start); \
528 goto tlv_put_failure; \
530 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
532 ret = tlv_put_uuid(sctx, attrtype, uuid); \
534 goto tlv_put_failure; \
536 #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \
538 ret = tlv_put_timespec(sctx, attrtype, ts); \
540 goto tlv_put_failure; \
542 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
544 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
546 goto tlv_put_failure; \
549 static int send_header(struct send_ctx *sctx)
551 struct btrfs_stream_header hdr;
553 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
554 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
556 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
561 * For each command/item we want to send to userspace, we call this function.
563 static int begin_cmd(struct send_ctx *sctx, int cmd)
565 struct btrfs_cmd_header *hdr;
567 if (!sctx->send_buf) {
572 BUG_ON(sctx->send_size);
574 sctx->send_size += sizeof(*hdr);
575 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
576 hdr->cmd = cpu_to_le16(cmd);
581 static int send_cmd(struct send_ctx *sctx)
584 struct btrfs_cmd_header *hdr;
587 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
588 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
591 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
592 hdr->crc = cpu_to_le32(crc);
594 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
597 sctx->total_send_size += sctx->send_size;
598 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
605 * Sends a move instruction to user space
607 static int send_rename(struct send_ctx *sctx,
608 struct fs_path *from, struct fs_path *to)
612 verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start);
614 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
618 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
619 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
621 ret = send_cmd(sctx);
629 * Sends a link instruction to user space
631 static int send_link(struct send_ctx *sctx,
632 struct fs_path *path, struct fs_path *lnk)
636 verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start);
638 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
642 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
643 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
645 ret = send_cmd(sctx);
653 * Sends an unlink instruction to user space
655 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
659 verbose_printk("btrfs: send_unlink %s\n", path->start);
661 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
665 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
667 ret = send_cmd(sctx);
675 * Sends a rmdir instruction to user space
677 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
681 verbose_printk("btrfs: send_rmdir %s\n", path->start);
683 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
687 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
689 ret = send_cmd(sctx);
697 * Helper function to retrieve some fields from an inode item.
699 static int get_inode_info(struct btrfs_root *root,
700 u64 ino, u64 *size, u64 *gen,
701 u64 *mode, u64 *uid, u64 *gid,
705 struct btrfs_inode_item *ii;
706 struct btrfs_key key;
707 struct btrfs_path *path;
709 path = alloc_path_for_send();
714 key.type = BTRFS_INODE_ITEM_KEY;
716 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
724 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
725 struct btrfs_inode_item);
727 *size = btrfs_inode_size(path->nodes[0], ii);
729 *gen = btrfs_inode_generation(path->nodes[0], ii);
731 *mode = btrfs_inode_mode(path->nodes[0], ii);
733 *uid = btrfs_inode_uid(path->nodes[0], ii);
735 *gid = btrfs_inode_gid(path->nodes[0], ii);
737 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
740 btrfs_free_path(path);
744 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
749 * Helper function to iterate the entries in ONE btrfs_inode_ref or
750 * btrfs_inode_extref.
751 * The iterate callback may return a non zero value to stop iteration. This can
752 * be a negative value for error codes or 1 to simply stop it.
754 * path must point to the INODE_REF or INODE_EXTREF when called.
756 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
757 struct btrfs_key *found_key, int resolve,
758 iterate_inode_ref_t iterate, void *ctx)
760 struct extent_buffer *eb = path->nodes[0];
761 struct btrfs_item *item;
762 struct btrfs_inode_ref *iref;
763 struct btrfs_inode_extref *extref;
764 struct btrfs_path *tmp_path;
768 int slot = path->slots[0];
775 unsigned long name_off;
776 unsigned long elem_size;
779 p = fs_path_alloc_reversed();
783 tmp_path = alloc_path_for_send();
790 if (found_key->type == BTRFS_INODE_REF_KEY) {
791 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
792 struct btrfs_inode_ref);
793 item = btrfs_item_nr(eb, slot);
794 total = btrfs_item_size(eb, item);
795 elem_size = sizeof(*iref);
797 ptr = btrfs_item_ptr_offset(eb, slot);
798 total = btrfs_item_size_nr(eb, slot);
799 elem_size = sizeof(*extref);
802 while (cur < total) {
805 if (found_key->type == BTRFS_INODE_REF_KEY) {
806 iref = (struct btrfs_inode_ref *)(ptr + cur);
807 name_len = btrfs_inode_ref_name_len(eb, iref);
808 name_off = (unsigned long)(iref + 1);
809 index = btrfs_inode_ref_index(eb, iref);
810 dir = found_key->offset;
812 extref = (struct btrfs_inode_extref *)(ptr + cur);
813 name_len = btrfs_inode_extref_name_len(eb, extref);
814 name_off = (unsigned long)&extref->name;
815 index = btrfs_inode_extref_index(eb, extref);
816 dir = btrfs_inode_extref_parent(eb, extref);
820 start = btrfs_ref_to_path(root, tmp_path, name_len,
824 ret = PTR_ERR(start);
827 if (start < p->buf) {
828 /* overflow , try again with larger buffer */
829 ret = fs_path_ensure_buf(p,
830 p->buf_len + p->buf - start);
833 start = btrfs_ref_to_path(root, tmp_path,
838 ret = PTR_ERR(start);
841 BUG_ON(start < p->buf);
845 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
851 cur += elem_size + name_len;
852 ret = iterate(num, dir, index, p, ctx);
859 btrfs_free_path(tmp_path);
864 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
865 const char *name, int name_len,
866 const char *data, int data_len,
870 * Helper function to iterate the entries in ONE btrfs_dir_item.
871 * The iterate callback may return a non zero value to stop iteration. This can
872 * be a negative value for error codes or 1 to simply stop it.
874 * path must point to the dir item when called.
876 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
877 struct btrfs_key *found_key,
878 iterate_dir_item_t iterate, void *ctx)
881 struct extent_buffer *eb;
882 struct btrfs_item *item;
883 struct btrfs_dir_item *di;
884 struct btrfs_key di_key;
899 buf = kmalloc(buf_len, GFP_NOFS);
906 slot = path->slots[0];
907 item = btrfs_item_nr(eb, slot);
908 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
911 total = btrfs_item_size(eb, item);
914 while (cur < total) {
915 name_len = btrfs_dir_name_len(eb, di);
916 data_len = btrfs_dir_data_len(eb, di);
917 type = btrfs_dir_type(eb, di);
918 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
920 if (name_len + data_len > buf_len) {
921 buf_len = PAGE_ALIGN(name_len + data_len);
923 buf2 = vmalloc(buf_len);
930 buf2 = krealloc(buf, buf_len, GFP_NOFS);
932 buf2 = vmalloc(buf_len);
946 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
947 name_len + data_len);
949 len = sizeof(*di) + name_len + data_len;
950 di = (struct btrfs_dir_item *)((char *)di + len);
953 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
954 data_len, type, ctx);
973 static int __copy_first_ref(int num, u64 dir, int index,
974 struct fs_path *p, void *ctx)
977 struct fs_path *pt = ctx;
979 ret = fs_path_copy(pt, p);
983 /* we want the first only */
988 * Retrieve the first path of an inode. If an inode has more then one
989 * ref/hardlink, this is ignored.
991 static int get_inode_path(struct btrfs_root *root,
992 u64 ino, struct fs_path *path)
995 struct btrfs_key key, found_key;
996 struct btrfs_path *p;
998 p = alloc_path_for_send();
1002 fs_path_reset(path);
1005 key.type = BTRFS_INODE_REF_KEY;
1008 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1015 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1016 if (found_key.objectid != ino ||
1017 (found_key.type != BTRFS_INODE_REF_KEY &&
1018 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1023 ret = iterate_inode_ref(root, p, &found_key, 1,
1024 __copy_first_ref, path);
1034 struct backref_ctx {
1035 struct send_ctx *sctx;
1037 /* number of total found references */
1041 * used for clones found in send_root. clones found behind cur_objectid
1042 * and cur_offset are not considered as allowed clones.
1047 /* may be truncated in case it's the last extent in a file */
1050 /* Just to check for bugs in backref resolving */
1054 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1056 u64 root = (u64)(uintptr_t)key;
1057 struct clone_root *cr = (struct clone_root *)elt;
1059 if (root < cr->root->objectid)
1061 if (root > cr->root->objectid)
1066 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1068 struct clone_root *cr1 = (struct clone_root *)e1;
1069 struct clone_root *cr2 = (struct clone_root *)e2;
1071 if (cr1->root->objectid < cr2->root->objectid)
1073 if (cr1->root->objectid > cr2->root->objectid)
1079 * Called for every backref that is found for the current extent.
1080 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1082 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1084 struct backref_ctx *bctx = ctx_;
1085 struct clone_root *found;
1089 /* First check if the root is in the list of accepted clone sources */
1090 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1091 bctx->sctx->clone_roots_cnt,
1092 sizeof(struct clone_root),
1093 __clone_root_cmp_bsearch);
1097 if (found->root == bctx->sctx->send_root &&
1098 ino == bctx->cur_objectid &&
1099 offset == bctx->cur_offset) {
1100 bctx->found_itself = 1;
1104 * There are inodes that have extents that lie behind its i_size. Don't
1105 * accept clones from these extents.
1107 ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL,
1112 if (offset + bctx->extent_len > i_size)
1116 * Make sure we don't consider clones from send_root that are
1117 * behind the current inode/offset.
1119 if (found->root == bctx->sctx->send_root) {
1121 * TODO for the moment we don't accept clones from the inode
1122 * that is currently send. We may change this when
1123 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1126 if (ino >= bctx->cur_objectid)
1129 if (ino > bctx->cur_objectid)
1131 if (offset + bctx->extent_len > bctx->cur_offset)
1137 found->found_refs++;
1138 if (ino < found->ino) {
1140 found->offset = offset;
1141 } else if (found->ino == ino) {
1143 * same extent found more then once in the same file.
1145 if (found->offset > offset + bctx->extent_len)
1146 found->offset = offset;
1153 * Given an inode, offset and extent item, it finds a good clone for a clone
1154 * instruction. Returns -ENOENT when none could be found. The function makes
1155 * sure that the returned clone is usable at the point where sending is at the
1156 * moment. This means, that no clones are accepted which lie behind the current
1159 * path must point to the extent item when called.
1161 static int find_extent_clone(struct send_ctx *sctx,
1162 struct btrfs_path *path,
1163 u64 ino, u64 data_offset,
1165 struct clone_root **found)
1172 u64 extent_item_pos;
1174 struct btrfs_file_extent_item *fi;
1175 struct extent_buffer *eb = path->nodes[0];
1176 struct backref_ctx *backref_ctx = NULL;
1177 struct clone_root *cur_clone_root;
1178 struct btrfs_key found_key;
1179 struct btrfs_path *tmp_path;
1183 tmp_path = alloc_path_for_send();
1187 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS);
1193 if (data_offset >= ino_size) {
1195 * There may be extents that lie behind the file's size.
1196 * I at least had this in combination with snapshotting while
1197 * writing large files.
1203 fi = btrfs_item_ptr(eb, path->slots[0],
1204 struct btrfs_file_extent_item);
1205 extent_type = btrfs_file_extent_type(eb, fi);
1206 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1210 compressed = btrfs_file_extent_compression(eb, fi);
1212 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1213 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1214 if (disk_byte == 0) {
1218 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1220 ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path,
1221 &found_key, &flags);
1222 btrfs_release_path(tmp_path);
1226 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1232 * Setup the clone roots.
1234 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1235 cur_clone_root = sctx->clone_roots + i;
1236 cur_clone_root->ino = (u64)-1;
1237 cur_clone_root->offset = 0;
1238 cur_clone_root->found_refs = 0;
1241 backref_ctx->sctx = sctx;
1242 backref_ctx->found = 0;
1243 backref_ctx->cur_objectid = ino;
1244 backref_ctx->cur_offset = data_offset;
1245 backref_ctx->found_itself = 0;
1246 backref_ctx->extent_len = num_bytes;
1249 * The last extent of a file may be too large due to page alignment.
1250 * We need to adjust extent_len in this case so that the checks in
1251 * __iterate_backrefs work.
1253 if (data_offset + num_bytes >= ino_size)
1254 backref_ctx->extent_len = ino_size - data_offset;
1257 * Now collect all backrefs.
1259 if (compressed == BTRFS_COMPRESS_NONE)
1260 extent_item_pos = logical - found_key.objectid;
1262 extent_item_pos = 0;
1264 extent_item_pos = logical - found_key.objectid;
1265 ret = iterate_extent_inodes(sctx->send_root->fs_info,
1266 found_key.objectid, extent_item_pos, 1,
1267 __iterate_backrefs, backref_ctx);
1272 if (!backref_ctx->found_itself) {
1273 /* found a bug in backref code? */
1275 printk(KERN_ERR "btrfs: ERROR did not find backref in "
1276 "send_root. inode=%llu, offset=%llu, "
1277 "disk_byte=%llu found extent=%llu\n",
1278 ino, data_offset, disk_byte, found_key.objectid);
1282 verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, "
1284 "num_bytes=%llu, logical=%llu\n",
1285 data_offset, ino, num_bytes, logical);
1287 if (!backref_ctx->found)
1288 verbose_printk("btrfs: no clones found\n");
1290 cur_clone_root = NULL;
1291 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1292 if (sctx->clone_roots[i].found_refs) {
1293 if (!cur_clone_root)
1294 cur_clone_root = sctx->clone_roots + i;
1295 else if (sctx->clone_roots[i].root == sctx->send_root)
1296 /* prefer clones from send_root over others */
1297 cur_clone_root = sctx->clone_roots + i;
1302 if (cur_clone_root) {
1303 *found = cur_clone_root;
1310 btrfs_free_path(tmp_path);
1315 static int read_symlink(struct btrfs_root *root,
1317 struct fs_path *dest)
1320 struct btrfs_path *path;
1321 struct btrfs_key key;
1322 struct btrfs_file_extent_item *ei;
1328 path = alloc_path_for_send();
1333 key.type = BTRFS_EXTENT_DATA_KEY;
1335 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1340 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1341 struct btrfs_file_extent_item);
1342 type = btrfs_file_extent_type(path->nodes[0], ei);
1343 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1344 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1345 BUG_ON(compression);
1347 off = btrfs_file_extent_inline_start(ei);
1348 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
1350 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1353 btrfs_free_path(path);
1358 * Helper function to generate a file name that is unique in the root of
1359 * send_root and parent_root. This is used to generate names for orphan inodes.
1361 static int gen_unique_name(struct send_ctx *sctx,
1363 struct fs_path *dest)
1366 struct btrfs_path *path;
1367 struct btrfs_dir_item *di;
1372 path = alloc_path_for_send();
1377 len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu",
1379 if (len >= sizeof(tmp)) {
1380 /* should really not happen */
1385 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1386 path, BTRFS_FIRST_FREE_OBJECTID,
1387 tmp, strlen(tmp), 0);
1388 btrfs_release_path(path);
1394 /* not unique, try again */
1399 if (!sctx->parent_root) {
1405 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1406 path, BTRFS_FIRST_FREE_OBJECTID,
1407 tmp, strlen(tmp), 0);
1408 btrfs_release_path(path);
1414 /* not unique, try again */
1422 ret = fs_path_add(dest, tmp, strlen(tmp));
1425 btrfs_free_path(path);
1430 inode_state_no_change,
1431 inode_state_will_create,
1432 inode_state_did_create,
1433 inode_state_will_delete,
1434 inode_state_did_delete,
1437 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1445 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1447 if (ret < 0 && ret != -ENOENT)
1451 if (!sctx->parent_root) {
1452 right_ret = -ENOENT;
1454 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1455 NULL, NULL, NULL, NULL);
1456 if (ret < 0 && ret != -ENOENT)
1461 if (!left_ret && !right_ret) {
1462 if (left_gen == gen && right_gen == gen) {
1463 ret = inode_state_no_change;
1464 } else if (left_gen == gen) {
1465 if (ino < sctx->send_progress)
1466 ret = inode_state_did_create;
1468 ret = inode_state_will_create;
1469 } else if (right_gen == gen) {
1470 if (ino < sctx->send_progress)
1471 ret = inode_state_did_delete;
1473 ret = inode_state_will_delete;
1477 } else if (!left_ret) {
1478 if (left_gen == gen) {
1479 if (ino < sctx->send_progress)
1480 ret = inode_state_did_create;
1482 ret = inode_state_will_create;
1486 } else if (!right_ret) {
1487 if (right_gen == gen) {
1488 if (ino < sctx->send_progress)
1489 ret = inode_state_did_delete;
1491 ret = inode_state_will_delete;
1503 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1507 ret = get_cur_inode_state(sctx, ino, gen);
1511 if (ret == inode_state_no_change ||
1512 ret == inode_state_did_create ||
1513 ret == inode_state_will_delete)
1523 * Helper function to lookup a dir item in a dir.
1525 static int lookup_dir_item_inode(struct btrfs_root *root,
1526 u64 dir, const char *name, int name_len,
1531 struct btrfs_dir_item *di;
1532 struct btrfs_key key;
1533 struct btrfs_path *path;
1535 path = alloc_path_for_send();
1539 di = btrfs_lookup_dir_item(NULL, root, path,
1540 dir, name, name_len, 0);
1549 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1550 *found_inode = key.objectid;
1551 *found_type = btrfs_dir_type(path->nodes[0], di);
1554 btrfs_free_path(path);
1559 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1560 * generation of the parent dir and the name of the dir entry.
1562 static int get_first_ref(struct btrfs_root *root, u64 ino,
1563 u64 *dir, u64 *dir_gen, struct fs_path *name)
1566 struct btrfs_key key;
1567 struct btrfs_key found_key;
1568 struct btrfs_path *path;
1572 path = alloc_path_for_send();
1577 key.type = BTRFS_INODE_REF_KEY;
1580 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1584 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1586 if (ret || found_key.objectid != ino ||
1587 (found_key.type != BTRFS_INODE_REF_KEY &&
1588 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1593 if (key.type == BTRFS_INODE_REF_KEY) {
1594 struct btrfs_inode_ref *iref;
1595 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1596 struct btrfs_inode_ref);
1597 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1598 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1599 (unsigned long)(iref + 1),
1601 parent_dir = found_key.offset;
1603 struct btrfs_inode_extref *extref;
1604 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1605 struct btrfs_inode_extref);
1606 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1607 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1608 (unsigned long)&extref->name, len);
1609 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1613 btrfs_release_path(path);
1615 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL,
1623 btrfs_free_path(path);
1627 static int is_first_ref(struct btrfs_root *root,
1629 const char *name, int name_len)
1632 struct fs_path *tmp_name;
1636 tmp_name = fs_path_alloc();
1640 ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name);
1644 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1649 ret = !memcmp(tmp_name->start, name, name_len);
1652 fs_path_free(tmp_name);
1657 * Used by process_recorded_refs to determine if a new ref would overwrite an
1658 * already existing ref. In case it detects an overwrite, it returns the
1659 * inode/gen in who_ino/who_gen.
1660 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1661 * to make sure later references to the overwritten inode are possible.
1662 * Orphanizing is however only required for the first ref of an inode.
1663 * process_recorded_refs does an additional is_first_ref check to see if
1664 * orphanizing is really required.
1666 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1667 const char *name, int name_len,
1668 u64 *who_ino, u64 *who_gen)
1672 u64 other_inode = 0;
1675 if (!sctx->parent_root)
1678 ret = is_inode_existent(sctx, dir, dir_gen);
1683 * If we have a parent root we need to verify that the parent dir was
1684 * not delted and then re-created, if it was then we have no overwrite
1685 * and we can just unlink this entry.
1687 if (sctx->parent_root) {
1688 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1690 if (ret < 0 && ret != -ENOENT)
1700 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1701 &other_inode, &other_type);
1702 if (ret < 0 && ret != -ENOENT)
1710 * Check if the overwritten ref was already processed. If yes, the ref
1711 * was already unlinked/moved, so we can safely assume that we will not
1712 * overwrite anything at this point in time.
1714 if (other_inode > sctx->send_progress) {
1715 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1716 who_gen, NULL, NULL, NULL, NULL);
1721 *who_ino = other_inode;
1731 * Checks if the ref was overwritten by an already processed inode. This is
1732 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1733 * thus the orphan name needs be used.
1734 * process_recorded_refs also uses it to avoid unlinking of refs that were
1737 static int did_overwrite_ref(struct send_ctx *sctx,
1738 u64 dir, u64 dir_gen,
1739 u64 ino, u64 ino_gen,
1740 const char *name, int name_len)
1747 if (!sctx->parent_root)
1750 ret = is_inode_existent(sctx, dir, dir_gen);
1754 /* check if the ref was overwritten by another ref */
1755 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1756 &ow_inode, &other_type);
1757 if (ret < 0 && ret != -ENOENT)
1760 /* was never and will never be overwritten */
1765 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1770 if (ow_inode == ino && gen == ino_gen) {
1775 /* we know that it is or will be overwritten. check this now */
1776 if (ow_inode < sctx->send_progress)
1786 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1787 * that got overwritten. This is used by process_recorded_refs to determine
1788 * if it has to use the path as returned by get_cur_path or the orphan name.
1790 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1793 struct fs_path *name = NULL;
1797 if (!sctx->parent_root)
1800 name = fs_path_alloc();
1804 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1808 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1809 name->start, fs_path_len(name));
1817 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1818 * so we need to do some special handling in case we have clashes. This function
1819 * takes care of this with the help of name_cache_entry::radix_list.
1820 * In case of error, nce is kfreed.
1822 static int name_cache_insert(struct send_ctx *sctx,
1823 struct name_cache_entry *nce)
1826 struct list_head *nce_head;
1828 nce_head = radix_tree_lookup(&sctx->name_cache,
1829 (unsigned long)nce->ino);
1831 nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS);
1836 INIT_LIST_HEAD(nce_head);
1838 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
1845 list_add_tail(&nce->radix_list, nce_head);
1846 list_add_tail(&nce->list, &sctx->name_cache_list);
1847 sctx->name_cache_size++;
1852 static void name_cache_delete(struct send_ctx *sctx,
1853 struct name_cache_entry *nce)
1855 struct list_head *nce_head;
1857 nce_head = radix_tree_lookup(&sctx->name_cache,
1858 (unsigned long)nce->ino);
1861 list_del(&nce->radix_list);
1862 list_del(&nce->list);
1863 sctx->name_cache_size--;
1865 if (list_empty(nce_head)) {
1866 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
1871 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
1874 struct list_head *nce_head;
1875 struct name_cache_entry *cur;
1877 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
1881 list_for_each_entry(cur, nce_head, radix_list) {
1882 if (cur->ino == ino && cur->gen == gen)
1889 * Removes the entry from the list and adds it back to the end. This marks the
1890 * entry as recently used so that name_cache_clean_unused does not remove it.
1892 static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
1894 list_del(&nce->list);
1895 list_add_tail(&nce->list, &sctx->name_cache_list);
1899 * Remove some entries from the beginning of name_cache_list.
1901 static void name_cache_clean_unused(struct send_ctx *sctx)
1903 struct name_cache_entry *nce;
1905 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
1908 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
1909 nce = list_entry(sctx->name_cache_list.next,
1910 struct name_cache_entry, list);
1911 name_cache_delete(sctx, nce);
1916 static void name_cache_free(struct send_ctx *sctx)
1918 struct name_cache_entry *nce;
1920 while (!list_empty(&sctx->name_cache_list)) {
1921 nce = list_entry(sctx->name_cache_list.next,
1922 struct name_cache_entry, list);
1923 name_cache_delete(sctx, nce);
1929 * Used by get_cur_path for each ref up to the root.
1930 * Returns 0 if it succeeded.
1931 * Returns 1 if the inode is not existent or got overwritten. In that case, the
1932 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
1933 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
1934 * Returns <0 in case of error.
1936 static int __get_cur_name_and_parent(struct send_ctx *sctx,
1940 struct fs_path *dest)
1944 struct btrfs_path *path = NULL;
1945 struct name_cache_entry *nce = NULL;
1948 * First check if we already did a call to this function with the same
1949 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
1950 * return the cached result.
1952 nce = name_cache_search(sctx, ino, gen);
1954 if (ino < sctx->send_progress && nce->need_later_update) {
1955 name_cache_delete(sctx, nce);
1959 name_cache_used(sctx, nce);
1960 *parent_ino = nce->parent_ino;
1961 *parent_gen = nce->parent_gen;
1962 ret = fs_path_add(dest, nce->name, nce->name_len);
1970 path = alloc_path_for_send();
1975 * If the inode is not existent yet, add the orphan name and return 1.
1976 * This should only happen for the parent dir that we determine in
1979 ret = is_inode_existent(sctx, ino, gen);
1984 ret = gen_unique_name(sctx, ino, gen, dest);
1992 * Depending on whether the inode was already processed or not, use
1993 * send_root or parent_root for ref lookup.
1995 if (ino < sctx->send_progress)
1996 ret = get_first_ref(sctx->send_root, ino,
1997 parent_ino, parent_gen, dest);
1999 ret = get_first_ref(sctx->parent_root, ino,
2000 parent_ino, parent_gen, dest);
2005 * Check if the ref was overwritten by an inode's ref that was processed
2006 * earlier. If yes, treat as orphan and return 1.
2008 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2009 dest->start, dest->end - dest->start);
2013 fs_path_reset(dest);
2014 ret = gen_unique_name(sctx, ino, gen, dest);
2022 * Store the result of the lookup in the name cache.
2024 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS);
2032 nce->parent_ino = *parent_ino;
2033 nce->parent_gen = *parent_gen;
2034 nce->name_len = fs_path_len(dest);
2036 strcpy(nce->name, dest->start);
2038 if (ino < sctx->send_progress)
2039 nce->need_later_update = 0;
2041 nce->need_later_update = 1;
2043 nce_ret = name_cache_insert(sctx, nce);
2046 name_cache_clean_unused(sctx);
2049 btrfs_free_path(path);
2054 * Magic happens here. This function returns the first ref to an inode as it
2055 * would look like while receiving the stream at this point in time.
2056 * We walk the path up to the root. For every inode in between, we check if it
2057 * was already processed/sent. If yes, we continue with the parent as found
2058 * in send_root. If not, we continue with the parent as found in parent_root.
2059 * If we encounter an inode that was deleted at this point in time, we use the
2060 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2061 * that were not created yet and overwritten inodes/refs.
2063 * When do we have have orphan inodes:
2064 * 1. When an inode is freshly created and thus no valid refs are available yet
2065 * 2. When a directory lost all it's refs (deleted) but still has dir items
2066 * inside which were not processed yet (pending for move/delete). If anyone
2067 * tried to get the path to the dir items, it would get a path inside that
2069 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2070 * of an unprocessed inode. If in that case the first ref would be
2071 * overwritten, the overwritten inode gets "orphanized". Later when we
2072 * process this overwritten inode, it is restored at a new place by moving
2075 * sctx->send_progress tells this function at which point in time receiving
2078 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2079 struct fs_path *dest)
2082 struct fs_path *name = NULL;
2083 u64 parent_inode = 0;
2087 name = fs_path_alloc();
2094 fs_path_reset(dest);
2096 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2097 fs_path_reset(name);
2099 ret = __get_cur_name_and_parent(sctx, ino, gen,
2100 &parent_inode, &parent_gen, name);
2106 ret = fs_path_add_path(dest, name);
2117 fs_path_unreverse(dest);
2122 * Called for regular files when sending extents data. Opens a struct file
2123 * to read from the file.
2125 static int open_cur_inode_file(struct send_ctx *sctx)
2128 struct btrfs_key key;
2130 struct inode *inode;
2131 struct dentry *dentry;
2135 if (sctx->cur_inode_filp)
2138 key.objectid = sctx->cur_ino;
2139 key.type = BTRFS_INODE_ITEM_KEY;
2142 inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root,
2144 if (IS_ERR(inode)) {
2145 ret = PTR_ERR(inode);
2149 dentry = d_obtain_alias(inode);
2151 if (IS_ERR(dentry)) {
2152 ret = PTR_ERR(dentry);
2156 path.mnt = sctx->mnt;
2157 path.dentry = dentry;
2158 filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred());
2162 ret = PTR_ERR(filp);
2165 sctx->cur_inode_filp = filp;
2169 * no xxxput required here as every vfs op
2170 * does it by itself on failure
2176 * Closes the struct file that was created in open_cur_inode_file
2178 static int close_cur_inode_file(struct send_ctx *sctx)
2182 if (!sctx->cur_inode_filp)
2185 ret = filp_close(sctx->cur_inode_filp, NULL);
2186 sctx->cur_inode_filp = NULL;
2193 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2195 static int send_subvol_begin(struct send_ctx *sctx)
2198 struct btrfs_root *send_root = sctx->send_root;
2199 struct btrfs_root *parent_root = sctx->parent_root;
2200 struct btrfs_path *path;
2201 struct btrfs_key key;
2202 struct btrfs_root_ref *ref;
2203 struct extent_buffer *leaf;
2207 path = alloc_path_for_send();
2211 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS);
2213 btrfs_free_path(path);
2217 key.objectid = send_root->objectid;
2218 key.type = BTRFS_ROOT_BACKREF_KEY;
2221 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2230 leaf = path->nodes[0];
2231 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2232 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2233 key.objectid != send_root->objectid) {
2237 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2238 namelen = btrfs_root_ref_name_len(leaf, ref);
2239 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2240 btrfs_release_path(path);
2243 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2247 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2252 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2253 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2254 sctx->send_root->root_item.uuid);
2255 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2256 sctx->send_root->root_item.ctransid);
2258 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2259 sctx->parent_root->root_item.uuid);
2260 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2261 sctx->parent_root->root_item.ctransid);
2264 ret = send_cmd(sctx);
2268 btrfs_free_path(path);
2273 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2278 verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size);
2280 p = fs_path_alloc();
2284 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2288 ret = get_cur_path(sctx, ino, gen, p);
2291 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2292 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2294 ret = send_cmd(sctx);
2302 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2307 verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode);
2309 p = fs_path_alloc();
2313 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2317 ret = get_cur_path(sctx, ino, gen, p);
2320 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2321 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2323 ret = send_cmd(sctx);
2331 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2336 verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid);
2338 p = fs_path_alloc();
2342 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2346 ret = get_cur_path(sctx, ino, gen, p);
2349 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2350 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2351 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2353 ret = send_cmd(sctx);
2361 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2364 struct fs_path *p = NULL;
2365 struct btrfs_inode_item *ii;
2366 struct btrfs_path *path = NULL;
2367 struct extent_buffer *eb;
2368 struct btrfs_key key;
2371 verbose_printk("btrfs: send_utimes %llu\n", ino);
2373 p = fs_path_alloc();
2377 path = alloc_path_for_send();
2384 key.type = BTRFS_INODE_ITEM_KEY;
2386 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2390 eb = path->nodes[0];
2391 slot = path->slots[0];
2392 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2394 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2398 ret = get_cur_path(sctx, ino, gen, p);
2401 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2402 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb,
2403 btrfs_inode_atime(ii));
2404 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb,
2405 btrfs_inode_mtime(ii));
2406 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb,
2407 btrfs_inode_ctime(ii));
2408 /* TODO Add otime support when the otime patches get into upstream */
2410 ret = send_cmd(sctx);
2415 btrfs_free_path(path);
2420 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2421 * a valid path yet because we did not process the refs yet. So, the inode
2422 * is created as orphan.
2424 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2433 verbose_printk("btrfs: send_create_inode %llu\n", ino);
2435 p = fs_path_alloc();
2439 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL,
2444 if (S_ISREG(mode)) {
2445 cmd = BTRFS_SEND_C_MKFILE;
2446 } else if (S_ISDIR(mode)) {
2447 cmd = BTRFS_SEND_C_MKDIR;
2448 } else if (S_ISLNK(mode)) {
2449 cmd = BTRFS_SEND_C_SYMLINK;
2450 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2451 cmd = BTRFS_SEND_C_MKNOD;
2452 } else if (S_ISFIFO(mode)) {
2453 cmd = BTRFS_SEND_C_MKFIFO;
2454 } else if (S_ISSOCK(mode)) {
2455 cmd = BTRFS_SEND_C_MKSOCK;
2457 printk(KERN_WARNING "btrfs: unexpected inode type %o",
2458 (int)(mode & S_IFMT));
2463 ret = begin_cmd(sctx, cmd);
2467 ret = gen_unique_name(sctx, ino, gen, p);
2471 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2472 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2474 if (S_ISLNK(mode)) {
2476 ret = read_symlink(sctx->send_root, ino, p);
2479 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2480 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2481 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2482 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2483 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2486 ret = send_cmd(sctx);
2498 * We need some special handling for inodes that get processed before the parent
2499 * directory got created. See process_recorded_refs for details.
2500 * This function does the check if we already created the dir out of order.
2502 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2505 struct btrfs_path *path = NULL;
2506 struct btrfs_key key;
2507 struct btrfs_key found_key;
2508 struct btrfs_key di_key;
2509 struct extent_buffer *eb;
2510 struct btrfs_dir_item *di;
2513 path = alloc_path_for_send();
2520 key.type = BTRFS_DIR_INDEX_KEY;
2523 ret = btrfs_search_slot_for_read(sctx->send_root, &key, path,
2528 eb = path->nodes[0];
2529 slot = path->slots[0];
2530 btrfs_item_key_to_cpu(eb, &found_key, slot);
2532 if (ret || found_key.objectid != key.objectid ||
2533 found_key.type != key.type) {
2538 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2539 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2541 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2542 di_key.objectid < sctx->send_progress) {
2547 key.offset = found_key.offset + 1;
2548 btrfs_release_path(path);
2552 btrfs_free_path(path);
2557 * Only creates the inode if it is:
2558 * 1. Not a directory
2559 * 2. Or a directory which was not created already due to out of order
2560 * directories. See did_create_dir and process_recorded_refs for details.
2562 static int send_create_inode_if_needed(struct send_ctx *sctx)
2566 if (S_ISDIR(sctx->cur_inode_mode)) {
2567 ret = did_create_dir(sctx, sctx->cur_ino);
2576 ret = send_create_inode(sctx, sctx->cur_ino);
2584 struct recorded_ref {
2585 struct list_head list;
2588 struct fs_path *full_path;
2596 * We need to process new refs before deleted refs, but compare_tree gives us
2597 * everything mixed. So we first record all refs and later process them.
2598 * This function is a helper to record one ref.
2600 static int record_ref(struct list_head *head, u64 dir,
2601 u64 dir_gen, struct fs_path *path)
2603 struct recorded_ref *ref;
2606 ref = kmalloc(sizeof(*ref), GFP_NOFS);
2611 ref->dir_gen = dir_gen;
2612 ref->full_path = path;
2614 tmp = strrchr(ref->full_path->start, '/');
2616 ref->name_len = ref->full_path->end - ref->full_path->start;
2617 ref->name = ref->full_path->start;
2618 ref->dir_path_len = 0;
2619 ref->dir_path = ref->full_path->start;
2622 ref->name_len = ref->full_path->end - tmp;
2624 ref->dir_path = ref->full_path->start;
2625 ref->dir_path_len = ref->full_path->end -
2626 ref->full_path->start - 1 - ref->name_len;
2629 list_add_tail(&ref->list, head);
2633 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2635 struct recorded_ref *new;
2637 new = kmalloc(sizeof(*ref), GFP_NOFS);
2641 new->dir = ref->dir;
2642 new->dir_gen = ref->dir_gen;
2643 new->full_path = NULL;
2644 INIT_LIST_HEAD(&new->list);
2645 list_add_tail(&new->list, list);
2649 static void __free_recorded_refs(struct list_head *head)
2651 struct recorded_ref *cur;
2653 while (!list_empty(head)) {
2654 cur = list_entry(head->next, struct recorded_ref, list);
2655 fs_path_free(cur->full_path);
2656 list_del(&cur->list);
2661 static void free_recorded_refs(struct send_ctx *sctx)
2663 __free_recorded_refs(&sctx->new_refs);
2664 __free_recorded_refs(&sctx->deleted_refs);
2668 * Renames/moves a file/dir to its orphan name. Used when the first
2669 * ref of an unprocessed inode gets overwritten and for all non empty
2672 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2673 struct fs_path *path)
2676 struct fs_path *orphan;
2678 orphan = fs_path_alloc();
2682 ret = gen_unique_name(sctx, ino, gen, orphan);
2686 ret = send_rename(sctx, path, orphan);
2689 fs_path_free(orphan);
2694 * Returns 1 if a directory can be removed at this point in time.
2695 * We check this by iterating all dir items and checking if the inode behind
2696 * the dir item was already processed.
2698 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress)
2701 struct btrfs_root *root = sctx->parent_root;
2702 struct btrfs_path *path;
2703 struct btrfs_key key;
2704 struct btrfs_key found_key;
2705 struct btrfs_key loc;
2706 struct btrfs_dir_item *di;
2709 * Don't try to rmdir the top/root subvolume dir.
2711 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2714 path = alloc_path_for_send();
2719 key.type = BTRFS_DIR_INDEX_KEY;
2723 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
2727 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2730 if (ret || found_key.objectid != key.objectid ||
2731 found_key.type != key.type) {
2735 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2736 struct btrfs_dir_item);
2737 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2739 if (loc.objectid > send_progress) {
2744 btrfs_release_path(path);
2745 key.offset = found_key.offset + 1;
2751 btrfs_free_path(path);
2756 * This does all the move/link/unlink/rmdir magic.
2758 static int process_recorded_refs(struct send_ctx *sctx)
2761 struct recorded_ref *cur;
2762 struct recorded_ref *cur2;
2763 struct list_head check_dirs;
2764 struct fs_path *valid_path = NULL;
2767 int did_overwrite = 0;
2770 verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino);
2773 * This should never happen as the root dir always has the same ref
2774 * which is always '..'
2776 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
2777 INIT_LIST_HEAD(&check_dirs);
2779 valid_path = fs_path_alloc();
2786 * First, check if the first ref of the current inode was overwritten
2787 * before. If yes, we know that the current inode was already orphanized
2788 * and thus use the orphan name. If not, we can use get_cur_path to
2789 * get the path of the first ref as it would like while receiving at
2790 * this point in time.
2791 * New inodes are always orphan at the beginning, so force to use the
2792 * orphan name in this case.
2793 * The first ref is stored in valid_path and will be updated if it
2794 * gets moved around.
2796 if (!sctx->cur_inode_new) {
2797 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
2798 sctx->cur_inode_gen);
2804 if (sctx->cur_inode_new || did_overwrite) {
2805 ret = gen_unique_name(sctx, sctx->cur_ino,
2806 sctx->cur_inode_gen, valid_path);
2811 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
2817 list_for_each_entry(cur, &sctx->new_refs, list) {
2819 * We may have refs where the parent directory does not exist
2820 * yet. This happens if the parent directories inum is higher
2821 * the the current inum. To handle this case, we create the
2822 * parent directory out of order. But we need to check if this
2823 * did already happen before due to other refs in the same dir.
2825 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
2828 if (ret == inode_state_will_create) {
2831 * First check if any of the current inodes refs did
2832 * already create the dir.
2834 list_for_each_entry(cur2, &sctx->new_refs, list) {
2837 if (cur2->dir == cur->dir) {
2844 * If that did not happen, check if a previous inode
2845 * did already create the dir.
2848 ret = did_create_dir(sctx, cur->dir);
2852 ret = send_create_inode(sctx, cur->dir);
2859 * Check if this new ref would overwrite the first ref of
2860 * another unprocessed inode. If yes, orphanize the
2861 * overwritten inode. If we find an overwritten ref that is
2862 * not the first ref, simply unlink it.
2864 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2865 cur->name, cur->name_len,
2866 &ow_inode, &ow_gen);
2870 ret = is_first_ref(sctx->parent_root,
2871 ow_inode, cur->dir, cur->name,
2876 ret = orphanize_inode(sctx, ow_inode, ow_gen,
2881 ret = send_unlink(sctx, cur->full_path);
2888 * link/move the ref to the new place. If we have an orphan
2889 * inode, move it and update valid_path. If not, link or move
2890 * it depending on the inode mode.
2893 ret = send_rename(sctx, valid_path, cur->full_path);
2897 ret = fs_path_copy(valid_path, cur->full_path);
2901 if (S_ISDIR(sctx->cur_inode_mode)) {
2903 * Dirs can't be linked, so move it. For moved
2904 * dirs, we always have one new and one deleted
2905 * ref. The deleted ref is ignored later.
2907 ret = send_rename(sctx, valid_path,
2911 ret = fs_path_copy(valid_path, cur->full_path);
2915 ret = send_link(sctx, cur->full_path,
2921 ret = dup_ref(cur, &check_dirs);
2926 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
2928 * Check if we can already rmdir the directory. If not,
2929 * orphanize it. For every dir item inside that gets deleted
2930 * later, we do this check again and rmdir it then if possible.
2931 * See the use of check_dirs for more details.
2933 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino);
2937 ret = send_rmdir(sctx, valid_path);
2940 } else if (!is_orphan) {
2941 ret = orphanize_inode(sctx, sctx->cur_ino,
2942 sctx->cur_inode_gen, valid_path);
2948 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2949 ret = dup_ref(cur, &check_dirs);
2953 } else if (S_ISDIR(sctx->cur_inode_mode) &&
2954 !list_empty(&sctx->deleted_refs)) {
2956 * We have a moved dir. Add the old parent to check_dirs
2958 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
2960 ret = dup_ref(cur, &check_dirs);
2963 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
2965 * We have a non dir inode. Go through all deleted refs and
2966 * unlink them if they were not already overwritten by other
2969 list_for_each_entry(cur, &sctx->deleted_refs, list) {
2970 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
2971 sctx->cur_ino, sctx->cur_inode_gen,
2972 cur->name, cur->name_len);
2976 ret = send_unlink(sctx, cur->full_path);
2980 ret = dup_ref(cur, &check_dirs);
2985 * If the inode is still orphan, unlink the orphan. This may
2986 * happen when a previous inode did overwrite the first ref
2987 * of this inode and no new refs were added for the current
2988 * inode. Unlinking does not mean that the inode is deleted in
2989 * all cases. There may still be links to this inode in other
2993 ret = send_unlink(sctx, valid_path);
3000 * We did collect all parent dirs where cur_inode was once located. We
3001 * now go through all these dirs and check if they are pending for
3002 * deletion and if it's finally possible to perform the rmdir now.
3003 * We also update the inode stats of the parent dirs here.
3005 list_for_each_entry(cur, &check_dirs, list) {
3007 * In case we had refs into dirs that were not processed yet,
3008 * we don't need to do the utime and rmdir logic for these dirs.
3009 * The dir will be processed later.
3011 if (cur->dir > sctx->cur_ino)
3014 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3018 if (ret == inode_state_did_create ||
3019 ret == inode_state_no_change) {
3020 /* TODO delayed utimes */
3021 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3024 } else if (ret == inode_state_did_delete) {
3025 ret = can_rmdir(sctx, cur->dir, sctx->cur_ino);
3029 ret = get_cur_path(sctx, cur->dir,
3030 cur->dir_gen, valid_path);
3033 ret = send_rmdir(sctx, valid_path);
3043 __free_recorded_refs(&check_dirs);
3044 free_recorded_refs(sctx);
3045 fs_path_free(valid_path);
3049 static int __record_new_ref(int num, u64 dir, int index,
3050 struct fs_path *name,
3054 struct send_ctx *sctx = ctx;
3058 p = fs_path_alloc();
3062 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL,
3067 ret = get_cur_path(sctx, dir, gen, p);
3070 ret = fs_path_add_path(p, name);
3074 ret = record_ref(&sctx->new_refs, dir, gen, p);
3082 static int __record_deleted_ref(int num, u64 dir, int index,
3083 struct fs_path *name,
3087 struct send_ctx *sctx = ctx;
3091 p = fs_path_alloc();
3095 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL,
3100 ret = get_cur_path(sctx, dir, gen, p);
3103 ret = fs_path_add_path(p, name);
3107 ret = record_ref(&sctx->deleted_refs, dir, gen, p);
3115 static int record_new_ref(struct send_ctx *sctx)
3119 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3120 sctx->cmp_key, 0, __record_new_ref, sctx);
3129 static int record_deleted_ref(struct send_ctx *sctx)
3133 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3134 sctx->cmp_key, 0, __record_deleted_ref, sctx);
3143 struct find_ref_ctx {
3146 struct btrfs_root *root;
3147 struct fs_path *name;
3151 static int __find_iref(int num, u64 dir, int index,
3152 struct fs_path *name,
3155 struct find_ref_ctx *ctx = ctx_;
3159 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
3160 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
3162 * To avoid doing extra lookups we'll only do this if everything
3165 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
3169 if (dir_gen != ctx->dir_gen)
3171 ctx->found_idx = num;
3177 static int find_iref(struct btrfs_root *root,
3178 struct btrfs_path *path,
3179 struct btrfs_key *key,
3180 u64 dir, u64 dir_gen, struct fs_path *name)
3183 struct find_ref_ctx ctx;
3187 ctx.dir_gen = dir_gen;
3191 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
3195 if (ctx.found_idx == -1)
3198 return ctx.found_idx;
3201 static int __record_changed_new_ref(int num, u64 dir, int index,
3202 struct fs_path *name,
3207 struct send_ctx *sctx = ctx;
3209 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
3214 ret = find_iref(sctx->parent_root, sctx->right_path,
3215 sctx->cmp_key, dir, dir_gen, name);
3217 ret = __record_new_ref(num, dir, index, name, sctx);
3224 static int __record_changed_deleted_ref(int num, u64 dir, int index,
3225 struct fs_path *name,
3230 struct send_ctx *sctx = ctx;
3232 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
3237 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
3238 dir, dir_gen, name);
3240 ret = __record_deleted_ref(num, dir, index, name, sctx);
3247 static int record_changed_ref(struct send_ctx *sctx)
3251 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
3252 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
3255 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
3256 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
3266 * Record and process all refs at once. Needed when an inode changes the
3267 * generation number, which means that it was deleted and recreated.
3269 static int process_all_refs(struct send_ctx *sctx,
3270 enum btrfs_compare_tree_result cmd)
3273 struct btrfs_root *root;
3274 struct btrfs_path *path;
3275 struct btrfs_key key;
3276 struct btrfs_key found_key;
3277 struct extent_buffer *eb;
3279 iterate_inode_ref_t cb;
3281 path = alloc_path_for_send();
3285 if (cmd == BTRFS_COMPARE_TREE_NEW) {
3286 root = sctx->send_root;
3287 cb = __record_new_ref;
3288 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
3289 root = sctx->parent_root;
3290 cb = __record_deleted_ref;
3295 key.objectid = sctx->cmp_key->objectid;
3296 key.type = BTRFS_INODE_REF_KEY;
3299 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3305 eb = path->nodes[0];
3306 slot = path->slots[0];
3307 btrfs_item_key_to_cpu(eb, &found_key, slot);
3309 if (found_key.objectid != key.objectid ||
3310 (found_key.type != BTRFS_INODE_REF_KEY &&
3311 found_key.type != BTRFS_INODE_EXTREF_KEY))
3314 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
3315 btrfs_release_path(path);
3319 key.offset = found_key.offset + 1;
3321 btrfs_release_path(path);
3323 ret = process_recorded_refs(sctx);
3326 btrfs_free_path(path);
3330 static int send_set_xattr(struct send_ctx *sctx,
3331 struct fs_path *path,
3332 const char *name, int name_len,
3333 const char *data, int data_len)
3337 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
3341 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3342 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3343 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
3345 ret = send_cmd(sctx);
3352 static int send_remove_xattr(struct send_ctx *sctx,
3353 struct fs_path *path,
3354 const char *name, int name_len)
3358 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
3362 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
3363 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
3365 ret = send_cmd(sctx);
3372 static int __process_new_xattr(int num, struct btrfs_key *di_key,
3373 const char *name, int name_len,
3374 const char *data, int data_len,
3378 struct send_ctx *sctx = ctx;
3380 posix_acl_xattr_header dummy_acl;
3382 p = fs_path_alloc();
3387 * This hack is needed because empty acl's are stored as zero byte
3388 * data in xattrs. Problem with that is, that receiving these zero byte
3389 * acl's will fail later. To fix this, we send a dummy acl list that
3390 * only contains the version number and no entries.
3392 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
3393 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
3394 if (data_len == 0) {
3395 dummy_acl.a_version =
3396 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
3397 data = (char *)&dummy_acl;
3398 data_len = sizeof(dummy_acl);
3402 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3406 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
3413 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
3414 const char *name, int name_len,
3415 const char *data, int data_len,
3419 struct send_ctx *sctx = ctx;
3422 p = fs_path_alloc();
3426 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3430 ret = send_remove_xattr(sctx, p, name, name_len);
3437 static int process_new_xattr(struct send_ctx *sctx)
3441 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3442 sctx->cmp_key, __process_new_xattr, sctx);
3447 static int process_deleted_xattr(struct send_ctx *sctx)
3451 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3452 sctx->cmp_key, __process_deleted_xattr, sctx);
3457 struct find_xattr_ctx {
3465 static int __find_xattr(int num, struct btrfs_key *di_key,
3466 const char *name, int name_len,
3467 const char *data, int data_len,
3468 u8 type, void *vctx)
3470 struct find_xattr_ctx *ctx = vctx;
3472 if (name_len == ctx->name_len &&
3473 strncmp(name, ctx->name, name_len) == 0) {
3474 ctx->found_idx = num;
3475 ctx->found_data_len = data_len;
3476 ctx->found_data = kmemdup(data, data_len, GFP_NOFS);
3477 if (!ctx->found_data)
3484 static int find_xattr(struct btrfs_root *root,
3485 struct btrfs_path *path,
3486 struct btrfs_key *key,
3487 const char *name, int name_len,
3488 char **data, int *data_len)
3491 struct find_xattr_ctx ctx;
3494 ctx.name_len = name_len;
3496 ctx.found_data = NULL;
3497 ctx.found_data_len = 0;
3499 ret = iterate_dir_item(root, path, key, __find_xattr, &ctx);
3503 if (ctx.found_idx == -1)
3506 *data = ctx.found_data;
3507 *data_len = ctx.found_data_len;
3509 kfree(ctx.found_data);
3511 return ctx.found_idx;
3515 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
3516 const char *name, int name_len,
3517 const char *data, int data_len,
3521 struct send_ctx *sctx = ctx;
3522 char *found_data = NULL;
3523 int found_data_len = 0;
3525 ret = find_xattr(sctx->parent_root, sctx->right_path,
3526 sctx->cmp_key, name, name_len, &found_data,
3528 if (ret == -ENOENT) {
3529 ret = __process_new_xattr(num, di_key, name, name_len, data,
3530 data_len, type, ctx);
3531 } else if (ret >= 0) {
3532 if (data_len != found_data_len ||
3533 memcmp(data, found_data, data_len)) {
3534 ret = __process_new_xattr(num, di_key, name, name_len,
3535 data, data_len, type, ctx);
3545 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
3546 const char *name, int name_len,
3547 const char *data, int data_len,
3551 struct send_ctx *sctx = ctx;
3553 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
3554 name, name_len, NULL, NULL);
3556 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
3557 data_len, type, ctx);
3564 static int process_changed_xattr(struct send_ctx *sctx)
3568 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
3569 sctx->cmp_key, __process_changed_new_xattr, sctx);
3572 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
3573 sctx->cmp_key, __process_changed_deleted_xattr, sctx);
3579 static int process_all_new_xattrs(struct send_ctx *sctx)
3582 struct btrfs_root *root;
3583 struct btrfs_path *path;
3584 struct btrfs_key key;
3585 struct btrfs_key found_key;
3586 struct extent_buffer *eb;
3589 path = alloc_path_for_send();
3593 root = sctx->send_root;
3595 key.objectid = sctx->cmp_key->objectid;
3596 key.type = BTRFS_XATTR_ITEM_KEY;
3599 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
3607 eb = path->nodes[0];
3608 slot = path->slots[0];
3609 btrfs_item_key_to_cpu(eb, &found_key, slot);
3611 if (found_key.objectid != key.objectid ||
3612 found_key.type != key.type) {
3617 ret = iterate_dir_item(root, path, &found_key,
3618 __process_new_xattr, sctx);
3622 btrfs_release_path(path);
3623 key.offset = found_key.offset + 1;
3627 btrfs_free_path(path);
3632 * Read some bytes from the current inode/file and send a write command to
3635 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
3639 loff_t pos = offset;
3641 mm_segment_t old_fs;
3643 p = fs_path_alloc();
3648 * vfs normally only accepts user space buffers for security reasons.
3649 * we only read from the file and also only provide the read_buf buffer
3650 * to vfs. As this buffer does not come from a user space call, it's
3651 * ok to temporary allow kernel space buffers.
3656 verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len);
3658 ret = open_cur_inode_file(sctx);
3662 ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos);
3669 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
3673 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3677 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3678 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3679 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
3681 ret = send_cmd(sctx);
3693 * Send a clone command to user space.
3695 static int send_clone(struct send_ctx *sctx,
3696 u64 offset, u32 len,
3697 struct clone_root *clone_root)
3703 verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, "
3704 "clone_inode=%llu, clone_offset=%llu\n", offset, len,
3705 clone_root->root->objectid, clone_root->ino,
3706 clone_root->offset);
3708 p = fs_path_alloc();
3712 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
3716 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3720 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3721 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
3722 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3724 if (clone_root->root == sctx->send_root) {
3725 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
3726 &gen, NULL, NULL, NULL, NULL);
3729 ret = get_cur_path(sctx, clone_root->ino, gen, p);
3731 ret = get_inode_path(clone_root->root, clone_root->ino, p);
3736 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
3737 clone_root->root->root_item.uuid);
3738 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
3739 clone_root->root->root_item.ctransid);
3740 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
3741 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
3742 clone_root->offset);
3744 ret = send_cmd(sctx);
3753 * Send an update extent command to user space.
3755 static int send_update_extent(struct send_ctx *sctx,
3756 u64 offset, u32 len)
3761 p = fs_path_alloc();
3765 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
3769 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
3773 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
3774 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
3775 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
3777 ret = send_cmd(sctx);
3785 static int send_write_or_clone(struct send_ctx *sctx,
3786 struct btrfs_path *path,
3787 struct btrfs_key *key,
3788 struct clone_root *clone_root)
3791 struct btrfs_file_extent_item *ei;
3792 u64 offset = key->offset;
3798 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3799 struct btrfs_file_extent_item);
3800 type = btrfs_file_extent_type(path->nodes[0], ei);
3801 if (type == BTRFS_FILE_EXTENT_INLINE) {
3802 len = btrfs_file_extent_inline_len(path->nodes[0], ei);
3804 * it is possible the inline item won't cover the whole page,
3805 * but there may be items after this page. Make
3806 * sure to send the whole thing
3808 len = PAGE_CACHE_ALIGN(len);
3810 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
3813 if (offset + len > sctx->cur_inode_size)
3814 len = sctx->cur_inode_size - offset;
3821 ret = send_clone(sctx, offset, len, clone_root);
3822 } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) {
3823 ret = send_update_extent(sctx, offset, len);
3827 if (l > BTRFS_SEND_READ_SIZE)
3828 l = BTRFS_SEND_READ_SIZE;
3829 ret = send_write(sctx, pos + offset, l);
3842 static int is_extent_unchanged(struct send_ctx *sctx,
3843 struct btrfs_path *left_path,
3844 struct btrfs_key *ekey)
3847 struct btrfs_key key;
3848 struct btrfs_path *path = NULL;
3849 struct extent_buffer *eb;
3851 struct btrfs_key found_key;
3852 struct btrfs_file_extent_item *ei;
3857 u64 left_offset_fixed;
3865 path = alloc_path_for_send();
3869 eb = left_path->nodes[0];
3870 slot = left_path->slots[0];
3871 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3872 left_type = btrfs_file_extent_type(eb, ei);
3874 if (left_type != BTRFS_FILE_EXTENT_REG) {
3878 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3879 left_len = btrfs_file_extent_num_bytes(eb, ei);
3880 left_offset = btrfs_file_extent_offset(eb, ei);
3881 left_gen = btrfs_file_extent_generation(eb, ei);
3884 * Following comments will refer to these graphics. L is the left
3885 * extents which we are checking at the moment. 1-8 are the right
3886 * extents that we iterate.
3889 * |-1-|-2a-|-3-|-4-|-5-|-6-|
3892 * |--1--|-2b-|...(same as above)
3894 * Alternative situation. Happens on files where extents got split.
3896 * |-----------7-----------|-6-|
3898 * Alternative situation. Happens on files which got larger.
3901 * Nothing follows after 8.
3904 key.objectid = ekey->objectid;
3905 key.type = BTRFS_EXTENT_DATA_KEY;
3906 key.offset = ekey->offset;
3907 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
3916 * Handle special case where the right side has no extents at all.
3918 eb = path->nodes[0];
3919 slot = path->slots[0];
3920 btrfs_item_key_to_cpu(eb, &found_key, slot);
3921 if (found_key.objectid != key.objectid ||
3922 found_key.type != key.type) {
3923 /* If we're a hole then just pretend nothing changed */
3924 ret = (left_disknr) ? 0 : 1;
3929 * We're now on 2a, 2b or 7.
3932 while (key.offset < ekey->offset + left_len) {
3933 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
3934 right_type = btrfs_file_extent_type(eb, ei);
3935 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
3936 right_len = btrfs_file_extent_num_bytes(eb, ei);
3937 right_offset = btrfs_file_extent_offset(eb, ei);
3938 right_gen = btrfs_file_extent_generation(eb, ei);
3940 if (right_type != BTRFS_FILE_EXTENT_REG) {
3946 * Are we at extent 8? If yes, we know the extent is changed.
3947 * This may only happen on the first iteration.
3949 if (found_key.offset + right_len <= ekey->offset) {
3950 /* If we're a hole just pretend nothing changed */
3951 ret = (left_disknr) ? 0 : 1;
3955 left_offset_fixed = left_offset;
3956 if (key.offset < ekey->offset) {
3957 /* Fix the right offset for 2a and 7. */
3958 right_offset += ekey->offset - key.offset;
3960 /* Fix the left offset for all behind 2a and 2b */
3961 left_offset_fixed += key.offset - ekey->offset;
3965 * Check if we have the same extent.
3967 if (left_disknr != right_disknr ||
3968 left_offset_fixed != right_offset ||
3969 left_gen != right_gen) {
3975 * Go to the next extent.
3977 ret = btrfs_next_item(sctx->parent_root, path);
3981 eb = path->nodes[0];
3982 slot = path->slots[0];
3983 btrfs_item_key_to_cpu(eb, &found_key, slot);
3985 if (ret || found_key.objectid != key.objectid ||
3986 found_key.type != key.type) {
3987 key.offset += right_len;
3990 if (found_key.offset != key.offset + right_len) {
3998 * We're now behind the left extent (treat as unchanged) or at the end
3999 * of the right side (treat as changed).
4001 if (key.offset >= ekey->offset + left_len)
4008 btrfs_free_path(path);
4012 static int process_extent(struct send_ctx *sctx,
4013 struct btrfs_path *path,
4014 struct btrfs_key *key)
4016 struct clone_root *found_clone = NULL;
4019 if (S_ISLNK(sctx->cur_inode_mode))
4022 if (sctx->parent_root && !sctx->cur_inode_new) {
4023 ret = is_extent_unchanged(sctx, path, key);
4031 struct btrfs_file_extent_item *ei;
4034 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4035 struct btrfs_file_extent_item);
4036 type = btrfs_file_extent_type(path->nodes[0], ei);
4037 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
4038 type == BTRFS_FILE_EXTENT_REG) {
4040 * The send spec does not have a prealloc command yet,
4041 * so just leave a hole for prealloc'ed extents until
4042 * we have enough commands queued up to justify rev'ing
4045 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
4050 /* Have a hole, just skip it. */
4051 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
4058 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
4059 sctx->cur_inode_size, &found_clone);
4060 if (ret != -ENOENT && ret < 0)
4063 ret = send_write_or_clone(sctx, path, key, found_clone);
4069 static int process_all_extents(struct send_ctx *sctx)
4072 struct btrfs_root *root;
4073 struct btrfs_path *path;
4074 struct btrfs_key key;
4075 struct btrfs_key found_key;
4076 struct extent_buffer *eb;
4079 root = sctx->send_root;
4080 path = alloc_path_for_send();
4084 key.objectid = sctx->cmp_key->objectid;
4085 key.type = BTRFS_EXTENT_DATA_KEY;
4088 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
4096 eb = path->nodes[0];
4097 slot = path->slots[0];
4098 btrfs_item_key_to_cpu(eb, &found_key, slot);
4100 if (found_key.objectid != key.objectid ||
4101 found_key.type != key.type) {
4106 ret = process_extent(sctx, path, &found_key);
4110 btrfs_release_path(path);
4111 key.offset = found_key.offset + 1;
4115 btrfs_free_path(path);
4119 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end)
4123 if (sctx->cur_ino == 0)
4125 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
4126 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
4128 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
4131 ret = process_recorded_refs(sctx);
4136 * We have processed the refs and thus need to advance send_progress.
4137 * Now, calls to get_cur_xxx will take the updated refs of the current
4138 * inode into account.
4140 sctx->send_progress = sctx->cur_ino + 1;
4146 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
4158 ret = process_recorded_refs_if_needed(sctx, at_end);
4162 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
4164 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
4167 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
4168 &left_mode, &left_uid, &left_gid, NULL);
4172 if (!sctx->parent_root || sctx->cur_inode_new) {
4174 if (!S_ISLNK(sctx->cur_inode_mode))
4177 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
4178 NULL, NULL, &right_mode, &right_uid,
4183 if (left_uid != right_uid || left_gid != right_gid)
4185 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
4189 if (S_ISREG(sctx->cur_inode_mode)) {
4190 ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4191 sctx->cur_inode_size);
4197 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4198 left_uid, left_gid);
4203 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4210 * Need to send that every time, no matter if it actually changed
4211 * between the two trees as we have done changes to the inode before.
4213 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
4221 static int changed_inode(struct send_ctx *sctx,
4222 enum btrfs_compare_tree_result result)
4225 struct btrfs_key *key = sctx->cmp_key;
4226 struct btrfs_inode_item *left_ii = NULL;
4227 struct btrfs_inode_item *right_ii = NULL;
4231 ret = close_cur_inode_file(sctx);
4235 sctx->cur_ino = key->objectid;
4236 sctx->cur_inode_new_gen = 0;
4239 * Set send_progress to current inode. This will tell all get_cur_xxx
4240 * functions that the current inode's refs are not updated yet. Later,
4241 * when process_recorded_refs is finished, it is set to cur_ino + 1.
4243 sctx->send_progress = sctx->cur_ino;
4245 if (result == BTRFS_COMPARE_TREE_NEW ||
4246 result == BTRFS_COMPARE_TREE_CHANGED) {
4247 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
4248 sctx->left_path->slots[0],
4249 struct btrfs_inode_item);
4250 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
4253 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4254 sctx->right_path->slots[0],
4255 struct btrfs_inode_item);
4256 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4259 if (result == BTRFS_COMPARE_TREE_CHANGED) {
4260 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
4261 sctx->right_path->slots[0],
4262 struct btrfs_inode_item);
4264 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
4268 * The cur_ino = root dir case is special here. We can't treat
4269 * the inode as deleted+reused because it would generate a
4270 * stream that tries to delete/mkdir the root dir.
4272 if (left_gen != right_gen &&
4273 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4274 sctx->cur_inode_new_gen = 1;
4277 if (result == BTRFS_COMPARE_TREE_NEW) {
4278 sctx->cur_inode_gen = left_gen;
4279 sctx->cur_inode_new = 1;
4280 sctx->cur_inode_deleted = 0;
4281 sctx->cur_inode_size = btrfs_inode_size(
4282 sctx->left_path->nodes[0], left_ii);
4283 sctx->cur_inode_mode = btrfs_inode_mode(
4284 sctx->left_path->nodes[0], left_ii);
4285 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
4286 ret = send_create_inode_if_needed(sctx);
4287 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
4288 sctx->cur_inode_gen = right_gen;
4289 sctx->cur_inode_new = 0;
4290 sctx->cur_inode_deleted = 1;
4291 sctx->cur_inode_size = btrfs_inode_size(
4292 sctx->right_path->nodes[0], right_ii);
4293 sctx->cur_inode_mode = btrfs_inode_mode(
4294 sctx->right_path->nodes[0], right_ii);
4295 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
4297 * We need to do some special handling in case the inode was
4298 * reported as changed with a changed generation number. This
4299 * means that the original inode was deleted and new inode
4300 * reused the same inum. So we have to treat the old inode as
4301 * deleted and the new one as new.
4303 if (sctx->cur_inode_new_gen) {
4305 * First, process the inode as if it was deleted.
4307 sctx->cur_inode_gen = right_gen;
4308 sctx->cur_inode_new = 0;
4309 sctx->cur_inode_deleted = 1;
4310 sctx->cur_inode_size = btrfs_inode_size(
4311 sctx->right_path->nodes[0], right_ii);
4312 sctx->cur_inode_mode = btrfs_inode_mode(
4313 sctx->right_path->nodes[0], right_ii);
4314 ret = process_all_refs(sctx,
4315 BTRFS_COMPARE_TREE_DELETED);
4320 * Now process the inode as if it was new.
4322 sctx->cur_inode_gen = left_gen;
4323 sctx->cur_inode_new = 1;
4324 sctx->cur_inode_deleted = 0;
4325 sctx->cur_inode_size = btrfs_inode_size(
4326 sctx->left_path->nodes[0], left_ii);
4327 sctx->cur_inode_mode = btrfs_inode_mode(
4328 sctx->left_path->nodes[0], left_ii);
4329 ret = send_create_inode_if_needed(sctx);
4333 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
4337 * Advance send_progress now as we did not get into
4338 * process_recorded_refs_if_needed in the new_gen case.
4340 sctx->send_progress = sctx->cur_ino + 1;
4343 * Now process all extents and xattrs of the inode as if
4344 * they were all new.
4346 ret = process_all_extents(sctx);
4349 ret = process_all_new_xattrs(sctx);
4353 sctx->cur_inode_gen = left_gen;
4354 sctx->cur_inode_new = 0;
4355 sctx->cur_inode_new_gen = 0;
4356 sctx->cur_inode_deleted = 0;
4357 sctx->cur_inode_size = btrfs_inode_size(
4358 sctx->left_path->nodes[0], left_ii);
4359 sctx->cur_inode_mode = btrfs_inode_mode(
4360 sctx->left_path->nodes[0], left_ii);
4369 * We have to process new refs before deleted refs, but compare_trees gives us
4370 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
4371 * first and later process them in process_recorded_refs.
4372 * For the cur_inode_new_gen case, we skip recording completely because
4373 * changed_inode did already initiate processing of refs. The reason for this is
4374 * that in this case, compare_tree actually compares the refs of 2 different
4375 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
4376 * refs of the right tree as deleted and all refs of the left tree as new.
4378 static int changed_ref(struct send_ctx *sctx,
4379 enum btrfs_compare_tree_result result)
4383 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4385 if (!sctx->cur_inode_new_gen &&
4386 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
4387 if (result == BTRFS_COMPARE_TREE_NEW)
4388 ret = record_new_ref(sctx);
4389 else if (result == BTRFS_COMPARE_TREE_DELETED)
4390 ret = record_deleted_ref(sctx);
4391 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4392 ret = record_changed_ref(sctx);
4399 * Process new/deleted/changed xattrs. We skip processing in the
4400 * cur_inode_new_gen case because changed_inode did already initiate processing
4401 * of xattrs. The reason is the same as in changed_ref
4403 static int changed_xattr(struct send_ctx *sctx,
4404 enum btrfs_compare_tree_result result)
4408 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4410 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4411 if (result == BTRFS_COMPARE_TREE_NEW)
4412 ret = process_new_xattr(sctx);
4413 else if (result == BTRFS_COMPARE_TREE_DELETED)
4414 ret = process_deleted_xattr(sctx);
4415 else if (result == BTRFS_COMPARE_TREE_CHANGED)
4416 ret = process_changed_xattr(sctx);
4423 * Process new/deleted/changed extents. We skip processing in the
4424 * cur_inode_new_gen case because changed_inode did already initiate processing
4425 * of extents. The reason is the same as in changed_ref
4427 static int changed_extent(struct send_ctx *sctx,
4428 enum btrfs_compare_tree_result result)
4432 BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid);
4434 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
4435 if (result != BTRFS_COMPARE_TREE_DELETED)
4436 ret = process_extent(sctx, sctx->left_path,
4443 static int dir_changed(struct send_ctx *sctx, u64 dir)
4445 u64 orig_gen, new_gen;
4448 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
4453 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
4458 return (orig_gen != new_gen) ? 1 : 0;
4461 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
4462 struct btrfs_key *key)
4464 struct btrfs_inode_extref *extref;
4465 struct extent_buffer *leaf;
4466 u64 dirid = 0, last_dirid = 0;
4473 /* Easy case, just check this one dirid */
4474 if (key->type == BTRFS_INODE_REF_KEY) {
4475 dirid = key->offset;
4477 ret = dir_changed(sctx, dirid);
4481 leaf = path->nodes[0];
4482 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4483 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4484 while (cur_offset < item_size) {
4485 extref = (struct btrfs_inode_extref *)(ptr +
4487 dirid = btrfs_inode_extref_parent(leaf, extref);
4488 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
4489 cur_offset += ref_name_len + sizeof(*extref);
4490 if (dirid == last_dirid)
4492 ret = dir_changed(sctx, dirid);
4502 * Updates compare related fields in sctx and simply forwards to the actual
4503 * changed_xxx functions.
4505 static int changed_cb(struct btrfs_root *left_root,
4506 struct btrfs_root *right_root,
4507 struct btrfs_path *left_path,
4508 struct btrfs_path *right_path,
4509 struct btrfs_key *key,
4510 enum btrfs_compare_tree_result result,
4514 struct send_ctx *sctx = ctx;
4516 if (result == BTRFS_COMPARE_TREE_SAME) {
4517 if (key->type != BTRFS_INODE_REF_KEY &&
4518 key->type != BTRFS_INODE_EXTREF_KEY)
4520 ret = compare_refs(sctx, left_path, key);
4525 result = BTRFS_COMPARE_TREE_CHANGED;
4529 sctx->left_path = left_path;
4530 sctx->right_path = right_path;
4531 sctx->cmp_key = key;
4533 ret = finish_inode_if_needed(sctx, 0);
4537 /* Ignore non-FS objects */
4538 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
4539 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
4542 if (key->type == BTRFS_INODE_ITEM_KEY)
4543 ret = changed_inode(sctx, result);
4544 else if (key->type == BTRFS_INODE_REF_KEY ||
4545 key->type == BTRFS_INODE_EXTREF_KEY)
4546 ret = changed_ref(sctx, result);
4547 else if (key->type == BTRFS_XATTR_ITEM_KEY)
4548 ret = changed_xattr(sctx, result);
4549 else if (key->type == BTRFS_EXTENT_DATA_KEY)
4550 ret = changed_extent(sctx, result);
4556 static int full_send_tree(struct send_ctx *sctx)
4559 struct btrfs_trans_handle *trans = NULL;
4560 struct btrfs_root *send_root = sctx->send_root;
4561 struct btrfs_key key;
4562 struct btrfs_key found_key;
4563 struct btrfs_path *path;
4564 struct extent_buffer *eb;
4569 path = alloc_path_for_send();
4573 spin_lock(&send_root->root_item_lock);
4574 start_ctransid = btrfs_root_ctransid(&send_root->root_item);
4575 spin_unlock(&send_root->root_item_lock);
4577 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4578 key.type = BTRFS_INODE_ITEM_KEY;
4583 * We need to make sure the transaction does not get committed
4584 * while we do anything on commit roots. Join a transaction to prevent
4587 trans = btrfs_join_transaction(send_root);
4588 if (IS_ERR(trans)) {
4589 ret = PTR_ERR(trans);
4595 * Make sure the tree has not changed after re-joining. We detect this
4596 * by comparing start_ctransid and ctransid. They should always match.
4598 spin_lock(&send_root->root_item_lock);
4599 ctransid = btrfs_root_ctransid(&send_root->root_item);
4600 spin_unlock(&send_root->root_item_lock);
4602 if (ctransid != start_ctransid) {
4603 WARN(1, KERN_WARNING "btrfs: the root that you're trying to "
4604 "send was modified in between. This is "
4605 "probably a bug.\n");
4610 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
4618 * When someone want to commit while we iterate, end the
4619 * joined transaction and rejoin.
4621 if (btrfs_should_end_transaction(trans, send_root)) {
4622 ret = btrfs_end_transaction(trans, send_root);
4626 btrfs_release_path(path);
4630 eb = path->nodes[0];
4631 slot = path->slots[0];
4632 btrfs_item_key_to_cpu(eb, &found_key, slot);
4634 ret = changed_cb(send_root, NULL, path, NULL,
4635 &found_key, BTRFS_COMPARE_TREE_NEW, sctx);
4639 key.objectid = found_key.objectid;
4640 key.type = found_key.type;
4641 key.offset = found_key.offset + 1;
4643 ret = btrfs_next_item(send_root, path);
4653 ret = finish_inode_if_needed(sctx, 1);
4656 btrfs_free_path(path);
4659 ret = btrfs_end_transaction(trans, send_root);
4661 btrfs_end_transaction(trans, send_root);
4666 static int send_subvol(struct send_ctx *sctx)
4670 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
4671 ret = send_header(sctx);
4676 ret = send_subvol_begin(sctx);
4680 if (sctx->parent_root) {
4681 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
4685 ret = finish_inode_if_needed(sctx, 1);
4689 ret = full_send_tree(sctx);
4696 ret = close_cur_inode_file(sctx);
4698 close_cur_inode_file(sctx);
4700 free_recorded_refs(sctx);
4704 long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
4707 struct btrfs_root *send_root;
4708 struct btrfs_root *clone_root;
4709 struct btrfs_fs_info *fs_info;
4710 struct btrfs_ioctl_send_args *arg = NULL;
4711 struct btrfs_key key;
4712 struct send_ctx *sctx = NULL;
4714 u64 *clone_sources_tmp = NULL;
4716 if (!capable(CAP_SYS_ADMIN))
4719 send_root = BTRFS_I(file_inode(mnt_file))->root;
4720 fs_info = send_root->fs_info;
4723 * This is done when we lookup the root, it should already be complete
4724 * by the time we get here.
4726 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
4729 * If we just created this root we need to make sure that the orphan
4730 * cleanup has been done and committed since we search the commit root,
4731 * so check its commit root transid with our otransid and if they match
4732 * commit the transaction to make sure everything is updated.
4734 down_read(&send_root->fs_info->extent_commit_sem);
4735 if (btrfs_header_generation(send_root->commit_root) ==
4736 btrfs_root_otransid(&send_root->root_item)) {
4737 struct btrfs_trans_handle *trans;
4739 up_read(&send_root->fs_info->extent_commit_sem);
4741 trans = btrfs_attach_transaction_barrier(send_root);
4742 if (IS_ERR(trans)) {
4743 if (PTR_ERR(trans) != -ENOENT) {
4744 ret = PTR_ERR(trans);
4747 /* ENOENT means theres no transaction */
4749 ret = btrfs_commit_transaction(trans, send_root);
4754 up_read(&send_root->fs_info->extent_commit_sem);
4757 arg = memdup_user(arg_, sizeof(*arg));
4764 if (!access_ok(VERIFY_READ, arg->clone_sources,
4765 sizeof(*arg->clone_sources *
4766 arg->clone_sources_count))) {
4771 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
4776 sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS);
4782 INIT_LIST_HEAD(&sctx->new_refs);
4783 INIT_LIST_HEAD(&sctx->deleted_refs);
4784 INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS);
4785 INIT_LIST_HEAD(&sctx->name_cache_list);
4787 sctx->flags = arg->flags;
4789 sctx->send_filp = fget(arg->send_fd);
4790 if (!sctx->send_filp) {
4795 sctx->mnt = mnt_file->f_path.mnt;
4797 sctx->send_root = send_root;
4798 sctx->clone_roots_cnt = arg->clone_sources_count;
4800 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
4801 sctx->send_buf = vmalloc(sctx->send_max_size);
4802 if (!sctx->send_buf) {
4807 sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE);
4808 if (!sctx->read_buf) {
4813 sctx->clone_roots = vzalloc(sizeof(struct clone_root) *
4814 (arg->clone_sources_count + 1));
4815 if (!sctx->clone_roots) {
4820 if (arg->clone_sources_count) {
4821 clone_sources_tmp = vmalloc(arg->clone_sources_count *
4822 sizeof(*arg->clone_sources));
4823 if (!clone_sources_tmp) {
4828 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
4829 arg->clone_sources_count *
4830 sizeof(*arg->clone_sources));
4836 for (i = 0; i < arg->clone_sources_count; i++) {
4837 key.objectid = clone_sources_tmp[i];
4838 key.type = BTRFS_ROOT_ITEM_KEY;
4839 key.offset = (u64)-1;
4840 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
4841 if (IS_ERR(clone_root)) {
4842 ret = PTR_ERR(clone_root);
4845 sctx->clone_roots[i].root = clone_root;
4847 vfree(clone_sources_tmp);
4848 clone_sources_tmp = NULL;
4851 if (arg->parent_root) {
4852 key.objectid = arg->parent_root;
4853 key.type = BTRFS_ROOT_ITEM_KEY;
4854 key.offset = (u64)-1;
4855 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
4856 if (IS_ERR(sctx->parent_root)) {
4857 ret = PTR_ERR(sctx->parent_root);
4863 * Clones from send_root are allowed, but only if the clone source
4864 * is behind the current send position. This is checked while searching
4865 * for possible clone sources.
4867 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
4869 /* We do a bsearch later */
4870 sort(sctx->clone_roots, sctx->clone_roots_cnt,
4871 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
4874 ret = send_subvol(sctx);
4878 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
4879 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
4882 ret = send_cmd(sctx);
4889 vfree(clone_sources_tmp);
4892 if (sctx->send_filp)
4893 fput(sctx->send_filp);
4895 vfree(sctx->clone_roots);
4896 vfree(sctx->send_buf);
4897 vfree(sctx->read_buf);
4899 name_cache_free(sctx);