0001
0002
0003
0004
0005
0006 #include <linux/bsearch.h>
0007 #include <linux/fs.h>
0008 #include <linux/file.h>
0009 #include <linux/sort.h>
0010 #include <linux/mount.h>
0011 #include <linux/xattr.h>
0012 #include <linux/posix_acl_xattr.h>
0013 #include <linux/radix-tree.h>
0014 #include <linux/vmalloc.h>
0015 #include <linux/string.h>
0016 #include <linux/compat.h>
0017 #include <linux/crc32c.h>
0018
0019 #include "send.h"
0020 #include "ctree.h"
0021 #include "backref.h"
0022 #include "locking.h"
0023 #include "disk-io.h"
0024 #include "btrfs_inode.h"
0025 #include "transaction.h"
0026 #include "compression.h"
0027 #include "xattr.h"
0028 #include "print-tree.h"
0029
0030
0031
0032
0033
0034
0035
0036 #define SEND_MAX_EXTENT_REFS 64
0037
0038
0039
0040
0041
0042
0043
0044
0045 struct fs_path {
0046 union {
0047 struct {
0048 char *start;
0049 char *end;
0050
0051 char *buf;
0052 unsigned short buf_len:15;
0053 unsigned short reversed:1;
0054 char inline_buf[];
0055 };
0056
0057
0058
0059
0060
0061 char pad[256];
0062 };
0063 };
0064 #define FS_PATH_INLINE_SIZE \
0065 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
0066
0067
0068
0069 struct clone_root {
0070 struct btrfs_root *root;
0071 u64 ino;
0072 u64 offset;
0073
0074 u64 found_refs;
0075 };
0076
0077 #define SEND_CTX_MAX_NAME_CACHE_SIZE 128
0078 #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
0079
0080 struct send_ctx {
0081 struct file *send_filp;
0082 loff_t send_off;
0083 char *send_buf;
0084 u32 send_size;
0085 u32 send_max_size;
0086
0087
0088
0089
0090 bool put_data;
0091 struct page **send_buf_pages;
0092 u64 flags;
0093
0094 u32 proto;
0095
0096 struct btrfs_root *send_root;
0097 struct btrfs_root *parent_root;
0098 struct clone_root *clone_roots;
0099 int clone_roots_cnt;
0100
0101
0102 struct btrfs_path *left_path;
0103 struct btrfs_path *right_path;
0104 struct btrfs_key *cmp_key;
0105
0106
0107
0108
0109
0110
0111
0112
0113 u64 last_reloc_trans;
0114
0115
0116
0117
0118
0119 u64 cur_ino;
0120 u64 cur_inode_gen;
0121 u64 cur_inode_size;
0122 u64 cur_inode_mode;
0123 u64 cur_inode_rdev;
0124 u64 cur_inode_last_extent;
0125 u64 cur_inode_next_write_offset;
0126 bool cur_inode_new;
0127 bool cur_inode_new_gen;
0128 bool cur_inode_deleted;
0129 bool ignore_cur_inode;
0130
0131 u64 send_progress;
0132
0133 struct list_head new_refs;
0134 struct list_head deleted_refs;
0135
0136 struct radix_tree_root name_cache;
0137 struct list_head name_cache_list;
0138 int name_cache_size;
0139
0140
0141
0142
0143
0144 struct inode *cur_inode;
0145 struct file_ra_state ra;
0146 u64 page_cache_clear_start;
0147 bool clean_page_cache;
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194 struct rb_root pending_dir_moves;
0195
0196
0197
0198
0199
0200
0201 struct rb_root waiting_dir_moves;
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 struct rb_root orphan_dirs;
0243
0244 struct rb_root rbtree_new_refs;
0245 struct rb_root rbtree_deleted_refs;
0246 };
0247
0248 struct pending_dir_move {
0249 struct rb_node node;
0250 struct list_head list;
0251 u64 parent_ino;
0252 u64 ino;
0253 u64 gen;
0254 struct list_head update_refs;
0255 };
0256
0257 struct waiting_dir_move {
0258 struct rb_node node;
0259 u64 ino;
0260
0261
0262
0263
0264
0265 u64 rmdir_ino;
0266 u64 rmdir_gen;
0267 bool orphanized;
0268 };
0269
0270 struct orphan_dir_info {
0271 struct rb_node node;
0272 u64 ino;
0273 u64 gen;
0274 u64 last_dir_index_offset;
0275 };
0276
0277 struct name_cache_entry {
0278 struct list_head list;
0279
0280
0281
0282
0283
0284
0285
0286
0287 struct list_head radix_list;
0288 u64 ino;
0289 u64 gen;
0290 u64 parent_ino;
0291 u64 parent_gen;
0292 int ret;
0293 int need_later_update;
0294 int name_len;
0295 char name[];
0296 };
0297
0298 #define ADVANCE 1
0299 #define ADVANCE_ONLY_NEXT -1
0300
0301 enum btrfs_compare_tree_result {
0302 BTRFS_COMPARE_TREE_NEW,
0303 BTRFS_COMPARE_TREE_DELETED,
0304 BTRFS_COMPARE_TREE_CHANGED,
0305 BTRFS_COMPARE_TREE_SAME,
0306 };
0307
0308 __cold
0309 static void inconsistent_snapshot_error(struct send_ctx *sctx,
0310 enum btrfs_compare_tree_result result,
0311 const char *what)
0312 {
0313 const char *result_string;
0314
0315 switch (result) {
0316 case BTRFS_COMPARE_TREE_NEW:
0317 result_string = "new";
0318 break;
0319 case BTRFS_COMPARE_TREE_DELETED:
0320 result_string = "deleted";
0321 break;
0322 case BTRFS_COMPARE_TREE_CHANGED:
0323 result_string = "updated";
0324 break;
0325 case BTRFS_COMPARE_TREE_SAME:
0326 ASSERT(0);
0327 result_string = "unchanged";
0328 break;
0329 default:
0330 ASSERT(0);
0331 result_string = "unexpected";
0332 }
0333
0334 btrfs_err(sctx->send_root->fs_info,
0335 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
0336 result_string, what, sctx->cmp_key->objectid,
0337 sctx->send_root->root_key.objectid,
0338 (sctx->parent_root ?
0339 sctx->parent_root->root_key.objectid : 0));
0340 }
0341
0342 __maybe_unused
0343 static bool proto_cmd_ok(const struct send_ctx *sctx, int cmd)
0344 {
0345 switch (sctx->proto) {
0346 case 1: return cmd <= BTRFS_SEND_C_MAX_V1;
0347 case 2: return cmd <= BTRFS_SEND_C_MAX_V2;
0348 default: return false;
0349 }
0350 }
0351
0352 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
0353
0354 static struct waiting_dir_move *
0355 get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
0356
0357 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen);
0358
0359 static int need_send_hole(struct send_ctx *sctx)
0360 {
0361 return (sctx->parent_root && !sctx->cur_inode_new &&
0362 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
0363 S_ISREG(sctx->cur_inode_mode));
0364 }
0365
0366 static void fs_path_reset(struct fs_path *p)
0367 {
0368 if (p->reversed) {
0369 p->start = p->buf + p->buf_len - 1;
0370 p->end = p->start;
0371 *p->start = 0;
0372 } else {
0373 p->start = p->buf;
0374 p->end = p->start;
0375 *p->start = 0;
0376 }
0377 }
0378
0379 static struct fs_path *fs_path_alloc(void)
0380 {
0381 struct fs_path *p;
0382
0383 p = kmalloc(sizeof(*p), GFP_KERNEL);
0384 if (!p)
0385 return NULL;
0386 p->reversed = 0;
0387 p->buf = p->inline_buf;
0388 p->buf_len = FS_PATH_INLINE_SIZE;
0389 fs_path_reset(p);
0390 return p;
0391 }
0392
0393 static struct fs_path *fs_path_alloc_reversed(void)
0394 {
0395 struct fs_path *p;
0396
0397 p = fs_path_alloc();
0398 if (!p)
0399 return NULL;
0400 p->reversed = 1;
0401 fs_path_reset(p);
0402 return p;
0403 }
0404
0405 static void fs_path_free(struct fs_path *p)
0406 {
0407 if (!p)
0408 return;
0409 if (p->buf != p->inline_buf)
0410 kfree(p->buf);
0411 kfree(p);
0412 }
0413
0414 static int fs_path_len(struct fs_path *p)
0415 {
0416 return p->end - p->start;
0417 }
0418
0419 static int fs_path_ensure_buf(struct fs_path *p, int len)
0420 {
0421 char *tmp_buf;
0422 int path_len;
0423 int old_buf_len;
0424
0425 len++;
0426
0427 if (p->buf_len >= len)
0428 return 0;
0429
0430 if (len > PATH_MAX) {
0431 WARN_ON(1);
0432 return -ENOMEM;
0433 }
0434
0435 path_len = p->end - p->start;
0436 old_buf_len = p->buf_len;
0437
0438
0439
0440
0441 if (p->buf == p->inline_buf) {
0442 tmp_buf = kmalloc(len, GFP_KERNEL);
0443 if (tmp_buf)
0444 memcpy(tmp_buf, p->buf, old_buf_len);
0445 } else {
0446 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
0447 }
0448 if (!tmp_buf)
0449 return -ENOMEM;
0450 p->buf = tmp_buf;
0451
0452
0453
0454
0455 p->buf_len = ksize(p->buf);
0456
0457 if (p->reversed) {
0458 tmp_buf = p->buf + old_buf_len - path_len - 1;
0459 p->end = p->buf + p->buf_len - 1;
0460 p->start = p->end - path_len;
0461 memmove(p->start, tmp_buf, path_len + 1);
0462 } else {
0463 p->start = p->buf;
0464 p->end = p->start + path_len;
0465 }
0466 return 0;
0467 }
0468
0469 static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
0470 char **prepared)
0471 {
0472 int ret;
0473 int new_len;
0474
0475 new_len = p->end - p->start + name_len;
0476 if (p->start != p->end)
0477 new_len++;
0478 ret = fs_path_ensure_buf(p, new_len);
0479 if (ret < 0)
0480 goto out;
0481
0482 if (p->reversed) {
0483 if (p->start != p->end)
0484 *--p->start = '/';
0485 p->start -= name_len;
0486 *prepared = p->start;
0487 } else {
0488 if (p->start != p->end)
0489 *p->end++ = '/';
0490 *prepared = p->end;
0491 p->end += name_len;
0492 *p->end = 0;
0493 }
0494
0495 out:
0496 return ret;
0497 }
0498
0499 static int fs_path_add(struct fs_path *p, const char *name, int name_len)
0500 {
0501 int ret;
0502 char *prepared;
0503
0504 ret = fs_path_prepare_for_add(p, name_len, &prepared);
0505 if (ret < 0)
0506 goto out;
0507 memcpy(prepared, name, name_len);
0508
0509 out:
0510 return ret;
0511 }
0512
0513 static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
0514 {
0515 int ret;
0516 char *prepared;
0517
0518 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
0519 if (ret < 0)
0520 goto out;
0521 memcpy(prepared, p2->start, p2->end - p2->start);
0522
0523 out:
0524 return ret;
0525 }
0526
0527 static int fs_path_add_from_extent_buffer(struct fs_path *p,
0528 struct extent_buffer *eb,
0529 unsigned long off, int len)
0530 {
0531 int ret;
0532 char *prepared;
0533
0534 ret = fs_path_prepare_for_add(p, len, &prepared);
0535 if (ret < 0)
0536 goto out;
0537
0538 read_extent_buffer(eb, prepared, off, len);
0539
0540 out:
0541 return ret;
0542 }
0543
0544 static int fs_path_copy(struct fs_path *p, struct fs_path *from)
0545 {
0546 p->reversed = from->reversed;
0547 fs_path_reset(p);
0548
0549 return fs_path_add_path(p, from);
0550 }
0551
0552 static void fs_path_unreverse(struct fs_path *p)
0553 {
0554 char *tmp;
0555 int len;
0556
0557 if (!p->reversed)
0558 return;
0559
0560 tmp = p->start;
0561 len = p->end - p->start;
0562 p->start = p->buf;
0563 p->end = p->start + len;
0564 memmove(p->start, tmp, len + 1);
0565 p->reversed = 0;
0566 }
0567
0568 static struct btrfs_path *alloc_path_for_send(void)
0569 {
0570 struct btrfs_path *path;
0571
0572 path = btrfs_alloc_path();
0573 if (!path)
0574 return NULL;
0575 path->search_commit_root = 1;
0576 path->skip_locking = 1;
0577 path->need_commit_sem = 1;
0578 return path;
0579 }
0580
0581 static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
0582 {
0583 int ret;
0584 u32 pos = 0;
0585
0586 while (pos < len) {
0587 ret = kernel_write(filp, buf + pos, len - pos, off);
0588 if (ret < 0)
0589 return ret;
0590 if (ret == 0)
0591 return -EIO;
0592 pos += ret;
0593 }
0594
0595 return 0;
0596 }
0597
0598 static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
0599 {
0600 struct btrfs_tlv_header *hdr;
0601 int total_len = sizeof(*hdr) + len;
0602 int left = sctx->send_max_size - sctx->send_size;
0603
0604 if (WARN_ON_ONCE(sctx->put_data))
0605 return -EINVAL;
0606
0607 if (unlikely(left < total_len))
0608 return -EOVERFLOW;
0609
0610 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
0611 put_unaligned_le16(attr, &hdr->tlv_type);
0612 put_unaligned_le16(len, &hdr->tlv_len);
0613 memcpy(hdr + 1, data, len);
0614 sctx->send_size += total_len;
0615
0616 return 0;
0617 }
0618
0619 #define TLV_PUT_DEFINE_INT(bits) \
0620 static int tlv_put_u##bits(struct send_ctx *sctx, \
0621 u##bits attr, u##bits value) \
0622 { \
0623 __le##bits __tmp = cpu_to_le##bits(value); \
0624 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
0625 }
0626
0627 TLV_PUT_DEFINE_INT(32)
0628 TLV_PUT_DEFINE_INT(64)
0629
0630 static int tlv_put_string(struct send_ctx *sctx, u16 attr,
0631 const char *str, int len)
0632 {
0633 if (len == -1)
0634 len = strlen(str);
0635 return tlv_put(sctx, attr, str, len);
0636 }
0637
0638 static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
0639 const u8 *uuid)
0640 {
0641 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
0642 }
0643
0644 static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
0645 struct extent_buffer *eb,
0646 struct btrfs_timespec *ts)
0647 {
0648 struct btrfs_timespec bts;
0649 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
0650 return tlv_put(sctx, attr, &bts, sizeof(bts));
0651 }
0652
0653
0654 #define TLV_PUT(sctx, attrtype, data, attrlen) \
0655 do { \
0656 ret = tlv_put(sctx, attrtype, data, attrlen); \
0657 if (ret < 0) \
0658 goto tlv_put_failure; \
0659 } while (0)
0660
0661 #define TLV_PUT_INT(sctx, attrtype, bits, value) \
0662 do { \
0663 ret = tlv_put_u##bits(sctx, attrtype, value); \
0664 if (ret < 0) \
0665 goto tlv_put_failure; \
0666 } while (0)
0667
0668 #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
0669 #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
0670 #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
0671 #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
0672 #define TLV_PUT_STRING(sctx, attrtype, str, len) \
0673 do { \
0674 ret = tlv_put_string(sctx, attrtype, str, len); \
0675 if (ret < 0) \
0676 goto tlv_put_failure; \
0677 } while (0)
0678 #define TLV_PUT_PATH(sctx, attrtype, p) \
0679 do { \
0680 ret = tlv_put_string(sctx, attrtype, p->start, \
0681 p->end - p->start); \
0682 if (ret < 0) \
0683 goto tlv_put_failure; \
0684 } while(0)
0685 #define TLV_PUT_UUID(sctx, attrtype, uuid) \
0686 do { \
0687 ret = tlv_put_uuid(sctx, attrtype, uuid); \
0688 if (ret < 0) \
0689 goto tlv_put_failure; \
0690 } while (0)
0691 #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
0692 do { \
0693 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
0694 if (ret < 0) \
0695 goto tlv_put_failure; \
0696 } while (0)
0697
0698 static int send_header(struct send_ctx *sctx)
0699 {
0700 struct btrfs_stream_header hdr;
0701
0702 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
0703 hdr.version = cpu_to_le32(sctx->proto);
0704 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
0705 &sctx->send_off);
0706 }
0707
0708
0709
0710
0711 static int begin_cmd(struct send_ctx *sctx, int cmd)
0712 {
0713 struct btrfs_cmd_header *hdr;
0714
0715 if (WARN_ON(!sctx->send_buf))
0716 return -EINVAL;
0717
0718 BUG_ON(sctx->send_size);
0719
0720 sctx->send_size += sizeof(*hdr);
0721 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
0722 put_unaligned_le16(cmd, &hdr->cmd);
0723
0724 return 0;
0725 }
0726
0727 static int send_cmd(struct send_ctx *sctx)
0728 {
0729 int ret;
0730 struct btrfs_cmd_header *hdr;
0731 u32 crc;
0732
0733 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
0734 put_unaligned_le32(sctx->send_size - sizeof(*hdr), &hdr->len);
0735 put_unaligned_le32(0, &hdr->crc);
0736
0737 crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
0738 put_unaligned_le32(crc, &hdr->crc);
0739
0740 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
0741 &sctx->send_off);
0742
0743 sctx->send_size = 0;
0744 sctx->put_data = false;
0745
0746 return ret;
0747 }
0748
0749
0750
0751
0752 static int send_rename(struct send_ctx *sctx,
0753 struct fs_path *from, struct fs_path *to)
0754 {
0755 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
0756 int ret;
0757
0758 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
0759
0760 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
0761 if (ret < 0)
0762 goto out;
0763
0764 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
0765 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
0766
0767 ret = send_cmd(sctx);
0768
0769 tlv_put_failure:
0770 out:
0771 return ret;
0772 }
0773
0774
0775
0776
0777 static int send_link(struct send_ctx *sctx,
0778 struct fs_path *path, struct fs_path *lnk)
0779 {
0780 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
0781 int ret;
0782
0783 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
0784
0785 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
0786 if (ret < 0)
0787 goto out;
0788
0789 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
0790 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
0791
0792 ret = send_cmd(sctx);
0793
0794 tlv_put_failure:
0795 out:
0796 return ret;
0797 }
0798
0799
0800
0801
0802 static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
0803 {
0804 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
0805 int ret;
0806
0807 btrfs_debug(fs_info, "send_unlink %s", path->start);
0808
0809 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
0810 if (ret < 0)
0811 goto out;
0812
0813 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
0814
0815 ret = send_cmd(sctx);
0816
0817 tlv_put_failure:
0818 out:
0819 return ret;
0820 }
0821
0822
0823
0824
0825 static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
0826 {
0827 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
0828 int ret;
0829
0830 btrfs_debug(fs_info, "send_rmdir %s", path->start);
0831
0832 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
0833 if (ret < 0)
0834 goto out;
0835
0836 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
0837
0838 ret = send_cmd(sctx);
0839
0840 tlv_put_failure:
0841 out:
0842 return ret;
0843 }
0844
0845
0846
0847
0848 static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
0849 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
0850 u64 *gid, u64 *rdev, u64 *fileattr)
0851 {
0852 int ret;
0853 struct btrfs_inode_item *ii;
0854 struct btrfs_key key;
0855
0856 key.objectid = ino;
0857 key.type = BTRFS_INODE_ITEM_KEY;
0858 key.offset = 0;
0859 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
0860 if (ret) {
0861 if (ret > 0)
0862 ret = -ENOENT;
0863 return ret;
0864 }
0865
0866 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
0867 struct btrfs_inode_item);
0868 if (size)
0869 *size = btrfs_inode_size(path->nodes[0], ii);
0870 if (gen)
0871 *gen = btrfs_inode_generation(path->nodes[0], ii);
0872 if (mode)
0873 *mode = btrfs_inode_mode(path->nodes[0], ii);
0874 if (uid)
0875 *uid = btrfs_inode_uid(path->nodes[0], ii);
0876 if (gid)
0877 *gid = btrfs_inode_gid(path->nodes[0], ii);
0878 if (rdev)
0879 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
0880
0881
0882
0883
0884 if (fileattr)
0885 *fileattr = btrfs_inode_flags(path->nodes[0], ii);
0886
0887 return ret;
0888 }
0889
0890 static int get_inode_info(struct btrfs_root *root,
0891 u64 ino, u64 *size, u64 *gen,
0892 u64 *mode, u64 *uid, u64 *gid,
0893 u64 *rdev, u64 *fileattr)
0894 {
0895 struct btrfs_path *path;
0896 int ret;
0897
0898 path = alloc_path_for_send();
0899 if (!path)
0900 return -ENOMEM;
0901 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
0902 rdev, fileattr);
0903 btrfs_free_path(path);
0904 return ret;
0905 }
0906
0907 typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
0908 struct fs_path *p,
0909 void *ctx);
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919 static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
0920 struct btrfs_key *found_key, int resolve,
0921 iterate_inode_ref_t iterate, void *ctx)
0922 {
0923 struct extent_buffer *eb = path->nodes[0];
0924 struct btrfs_inode_ref *iref;
0925 struct btrfs_inode_extref *extref;
0926 struct btrfs_path *tmp_path;
0927 struct fs_path *p;
0928 u32 cur = 0;
0929 u32 total;
0930 int slot = path->slots[0];
0931 u32 name_len;
0932 char *start;
0933 int ret = 0;
0934 int num = 0;
0935 int index;
0936 u64 dir;
0937 unsigned long name_off;
0938 unsigned long elem_size;
0939 unsigned long ptr;
0940
0941 p = fs_path_alloc_reversed();
0942 if (!p)
0943 return -ENOMEM;
0944
0945 tmp_path = alloc_path_for_send();
0946 if (!tmp_path) {
0947 fs_path_free(p);
0948 return -ENOMEM;
0949 }
0950
0951
0952 if (found_key->type == BTRFS_INODE_REF_KEY) {
0953 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
0954 struct btrfs_inode_ref);
0955 total = btrfs_item_size(eb, slot);
0956 elem_size = sizeof(*iref);
0957 } else {
0958 ptr = btrfs_item_ptr_offset(eb, slot);
0959 total = btrfs_item_size(eb, slot);
0960 elem_size = sizeof(*extref);
0961 }
0962
0963 while (cur < total) {
0964 fs_path_reset(p);
0965
0966 if (found_key->type == BTRFS_INODE_REF_KEY) {
0967 iref = (struct btrfs_inode_ref *)(ptr + cur);
0968 name_len = btrfs_inode_ref_name_len(eb, iref);
0969 name_off = (unsigned long)(iref + 1);
0970 index = btrfs_inode_ref_index(eb, iref);
0971 dir = found_key->offset;
0972 } else {
0973 extref = (struct btrfs_inode_extref *)(ptr + cur);
0974 name_len = btrfs_inode_extref_name_len(eb, extref);
0975 name_off = (unsigned long)&extref->name;
0976 index = btrfs_inode_extref_index(eb, extref);
0977 dir = btrfs_inode_extref_parent(eb, extref);
0978 }
0979
0980 if (resolve) {
0981 start = btrfs_ref_to_path(root, tmp_path, name_len,
0982 name_off, eb, dir,
0983 p->buf, p->buf_len);
0984 if (IS_ERR(start)) {
0985 ret = PTR_ERR(start);
0986 goto out;
0987 }
0988 if (start < p->buf) {
0989
0990 ret = fs_path_ensure_buf(p,
0991 p->buf_len + p->buf - start);
0992 if (ret < 0)
0993 goto out;
0994 start = btrfs_ref_to_path(root, tmp_path,
0995 name_len, name_off,
0996 eb, dir,
0997 p->buf, p->buf_len);
0998 if (IS_ERR(start)) {
0999 ret = PTR_ERR(start);
1000 goto out;
1001 }
1002 BUG_ON(start < p->buf);
1003 }
1004 p->start = start;
1005 } else {
1006 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
1007 name_len);
1008 if (ret < 0)
1009 goto out;
1010 }
1011
1012 cur += elem_size + name_len;
1013 ret = iterate(num, dir, index, p, ctx);
1014 if (ret)
1015 goto out;
1016 num++;
1017 }
1018
1019 out:
1020 btrfs_free_path(tmp_path);
1021 fs_path_free(p);
1022 return ret;
1023 }
1024
1025 typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
1026 const char *name, int name_len,
1027 const char *data, int data_len,
1028 void *ctx);
1029
1030
1031
1032
1033
1034
1035
1036
1037 static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
1038 iterate_dir_item_t iterate, void *ctx)
1039 {
1040 int ret = 0;
1041 struct extent_buffer *eb;
1042 struct btrfs_dir_item *di;
1043 struct btrfs_key di_key;
1044 char *buf = NULL;
1045 int buf_len;
1046 u32 name_len;
1047 u32 data_len;
1048 u32 cur;
1049 u32 len;
1050 u32 total;
1051 int slot;
1052 int num;
1053
1054
1055
1056
1057
1058
1059
1060 buf_len = PATH_MAX;
1061 buf = kmalloc(buf_len, GFP_KERNEL);
1062 if (!buf) {
1063 ret = -ENOMEM;
1064 goto out;
1065 }
1066
1067 eb = path->nodes[0];
1068 slot = path->slots[0];
1069 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1070 cur = 0;
1071 len = 0;
1072 total = btrfs_item_size(eb, slot);
1073
1074 num = 0;
1075 while (cur < total) {
1076 name_len = btrfs_dir_name_len(eb, di);
1077 data_len = btrfs_dir_data_len(eb, di);
1078 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1079
1080 if (btrfs_dir_type(eb, di) == BTRFS_FT_XATTR) {
1081 if (name_len > XATTR_NAME_MAX) {
1082 ret = -ENAMETOOLONG;
1083 goto out;
1084 }
1085 if (name_len + data_len >
1086 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1087 ret = -E2BIG;
1088 goto out;
1089 }
1090 } else {
1091
1092
1093
1094 if (name_len + data_len > PATH_MAX) {
1095 ret = -ENAMETOOLONG;
1096 goto out;
1097 }
1098 }
1099
1100 if (name_len + data_len > buf_len) {
1101 buf_len = name_len + data_len;
1102 if (is_vmalloc_addr(buf)) {
1103 vfree(buf);
1104 buf = NULL;
1105 } else {
1106 char *tmp = krealloc(buf, buf_len,
1107 GFP_KERNEL | __GFP_NOWARN);
1108
1109 if (!tmp)
1110 kfree(buf);
1111 buf = tmp;
1112 }
1113 if (!buf) {
1114 buf = kvmalloc(buf_len, GFP_KERNEL);
1115 if (!buf) {
1116 ret = -ENOMEM;
1117 goto out;
1118 }
1119 }
1120 }
1121
1122 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1123 name_len + data_len);
1124
1125 len = sizeof(*di) + name_len + data_len;
1126 di = (struct btrfs_dir_item *)((char *)di + len);
1127 cur += len;
1128
1129 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1130 data_len, ctx);
1131 if (ret < 0)
1132 goto out;
1133 if (ret) {
1134 ret = 0;
1135 goto out;
1136 }
1137
1138 num++;
1139 }
1140
1141 out:
1142 kvfree(buf);
1143 return ret;
1144 }
1145
1146 static int __copy_first_ref(int num, u64 dir, int index,
1147 struct fs_path *p, void *ctx)
1148 {
1149 int ret;
1150 struct fs_path *pt = ctx;
1151
1152 ret = fs_path_copy(pt, p);
1153 if (ret < 0)
1154 return ret;
1155
1156
1157 return 1;
1158 }
1159
1160
1161
1162
1163
1164 static int get_inode_path(struct btrfs_root *root,
1165 u64 ino, struct fs_path *path)
1166 {
1167 int ret;
1168 struct btrfs_key key, found_key;
1169 struct btrfs_path *p;
1170
1171 p = alloc_path_for_send();
1172 if (!p)
1173 return -ENOMEM;
1174
1175 fs_path_reset(path);
1176
1177 key.objectid = ino;
1178 key.type = BTRFS_INODE_REF_KEY;
1179 key.offset = 0;
1180
1181 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1182 if (ret < 0)
1183 goto out;
1184 if (ret) {
1185 ret = 1;
1186 goto out;
1187 }
1188 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1189 if (found_key.objectid != ino ||
1190 (found_key.type != BTRFS_INODE_REF_KEY &&
1191 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1192 ret = -ENOENT;
1193 goto out;
1194 }
1195
1196 ret = iterate_inode_ref(root, p, &found_key, 1,
1197 __copy_first_ref, path);
1198 if (ret < 0)
1199 goto out;
1200 ret = 0;
1201
1202 out:
1203 btrfs_free_path(p);
1204 return ret;
1205 }
1206
1207 struct backref_ctx {
1208 struct send_ctx *sctx;
1209
1210
1211 u64 found;
1212
1213
1214
1215
1216
1217 u64 cur_objectid;
1218 u64 cur_offset;
1219
1220
1221 u64 extent_len;
1222
1223
1224 int found_itself;
1225 };
1226
1227 static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1228 {
1229 u64 root = (u64)(uintptr_t)key;
1230 const struct clone_root *cr = elt;
1231
1232 if (root < cr->root->root_key.objectid)
1233 return -1;
1234 if (root > cr->root->root_key.objectid)
1235 return 1;
1236 return 0;
1237 }
1238
1239 static int __clone_root_cmp_sort(const void *e1, const void *e2)
1240 {
1241 const struct clone_root *cr1 = e1;
1242 const struct clone_root *cr2 = e2;
1243
1244 if (cr1->root->root_key.objectid < cr2->root->root_key.objectid)
1245 return -1;
1246 if (cr1->root->root_key.objectid > cr2->root->root_key.objectid)
1247 return 1;
1248 return 0;
1249 }
1250
1251
1252
1253
1254
1255 static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1256 {
1257 struct backref_ctx *bctx = ctx_;
1258 struct clone_root *found;
1259
1260
1261 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1262 bctx->sctx->clone_roots_cnt,
1263 sizeof(struct clone_root),
1264 __clone_root_cmp_bsearch);
1265 if (!found)
1266 return 0;
1267
1268 if (found->root == bctx->sctx->send_root &&
1269 ino == bctx->cur_objectid &&
1270 offset == bctx->cur_offset) {
1271 bctx->found_itself = 1;
1272 }
1273
1274
1275
1276
1277
1278 if (found->root == bctx->sctx->send_root) {
1279
1280
1281
1282
1283
1284 if (ino > bctx->cur_objectid)
1285 return 0;
1286
1287
1288
1289
1290
1291
1292 if (ino == bctx->cur_objectid &&
1293 offset + bctx->extent_len >
1294 bctx->sctx->cur_inode_next_write_offset)
1295 return 0;
1296 }
1297
1298 bctx->found++;
1299 found->found_refs++;
1300 if (ino < found->ino) {
1301 found->ino = ino;
1302 found->offset = offset;
1303 } else if (found->ino == ino) {
1304
1305
1306
1307 if (found->offset > offset + bctx->extent_len)
1308 found->offset = offset;
1309 }
1310
1311 return 0;
1312 }
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323 static int find_extent_clone(struct send_ctx *sctx,
1324 struct btrfs_path *path,
1325 u64 ino, u64 data_offset,
1326 u64 ino_size,
1327 struct clone_root **found)
1328 {
1329 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1330 int ret;
1331 int extent_type;
1332 u64 logical;
1333 u64 disk_byte;
1334 u64 num_bytes;
1335 u64 extent_item_pos;
1336 u64 flags = 0;
1337 struct btrfs_file_extent_item *fi;
1338 struct extent_buffer *eb = path->nodes[0];
1339 struct backref_ctx backref_ctx = {0};
1340 struct clone_root *cur_clone_root;
1341 struct btrfs_key found_key;
1342 struct btrfs_path *tmp_path;
1343 struct btrfs_extent_item *ei;
1344 int compressed;
1345 u32 i;
1346
1347 tmp_path = alloc_path_for_send();
1348 if (!tmp_path)
1349 return -ENOMEM;
1350
1351
1352 tmp_path->need_commit_sem = 0;
1353
1354 if (data_offset >= ino_size) {
1355
1356
1357
1358
1359
1360 ret = 0;
1361 goto out;
1362 }
1363
1364 fi = btrfs_item_ptr(eb, path->slots[0],
1365 struct btrfs_file_extent_item);
1366 extent_type = btrfs_file_extent_type(eb, fi);
1367 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1368 ret = -ENOENT;
1369 goto out;
1370 }
1371 compressed = btrfs_file_extent_compression(eb, fi);
1372
1373 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1374 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1375 if (disk_byte == 0) {
1376 ret = -ENOENT;
1377 goto out;
1378 }
1379 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1380
1381 down_read(&fs_info->commit_root_sem);
1382 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1383 &found_key, &flags);
1384 up_read(&fs_info->commit_root_sem);
1385
1386 if (ret < 0)
1387 goto out;
1388 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1389 ret = -EIO;
1390 goto out;
1391 }
1392
1393 ei = btrfs_item_ptr(tmp_path->nodes[0], tmp_path->slots[0],
1394 struct btrfs_extent_item);
1395
1396
1397
1398
1399
1400
1401
1402 if (btrfs_extent_refs(tmp_path->nodes[0], ei) > SEND_MAX_EXTENT_REFS) {
1403 ret = -ENOENT;
1404 goto out;
1405 }
1406 btrfs_release_path(tmp_path);
1407
1408
1409
1410
1411 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1412 cur_clone_root = sctx->clone_roots + i;
1413 cur_clone_root->ino = (u64)-1;
1414 cur_clone_root->offset = 0;
1415 cur_clone_root->found_refs = 0;
1416 }
1417
1418 backref_ctx.sctx = sctx;
1419 backref_ctx.found = 0;
1420 backref_ctx.cur_objectid = ino;
1421 backref_ctx.cur_offset = data_offset;
1422 backref_ctx.found_itself = 0;
1423 backref_ctx.extent_len = num_bytes;
1424
1425
1426
1427
1428
1429
1430 if (data_offset + num_bytes >= ino_size)
1431 backref_ctx.extent_len = ino_size - data_offset;
1432
1433
1434
1435
1436 if (compressed == BTRFS_COMPRESS_NONE)
1437 extent_item_pos = logical - found_key.objectid;
1438 else
1439 extent_item_pos = 0;
1440 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1441 extent_item_pos, 1, __iterate_backrefs,
1442 &backref_ctx, false);
1443
1444 if (ret < 0)
1445 goto out;
1446
1447 down_read(&fs_info->commit_root_sem);
1448 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461 up_read(&fs_info->commit_root_sem);
1462 ret = -ENOENT;
1463 goto out;
1464 }
1465 up_read(&fs_info->commit_root_sem);
1466
1467 if (!backref_ctx.found_itself) {
1468
1469 ret = -EIO;
1470 btrfs_err(fs_info,
1471 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1472 ino, data_offset, disk_byte, found_key.objectid);
1473 goto out;
1474 }
1475
1476 btrfs_debug(fs_info,
1477 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1478 data_offset, ino, num_bytes, logical);
1479
1480 if (!backref_ctx.found)
1481 btrfs_debug(fs_info, "no clones found");
1482
1483 cur_clone_root = NULL;
1484 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1485 if (sctx->clone_roots[i].found_refs) {
1486 if (!cur_clone_root)
1487 cur_clone_root = sctx->clone_roots + i;
1488 else if (sctx->clone_roots[i].root == sctx->send_root)
1489
1490 cur_clone_root = sctx->clone_roots + i;
1491 }
1492
1493 }
1494
1495 if (cur_clone_root) {
1496 *found = cur_clone_root;
1497 ret = 0;
1498 } else {
1499 ret = -ENOENT;
1500 }
1501
1502 out:
1503 btrfs_free_path(tmp_path);
1504 return ret;
1505 }
1506
1507 static int read_symlink(struct btrfs_root *root,
1508 u64 ino,
1509 struct fs_path *dest)
1510 {
1511 int ret;
1512 struct btrfs_path *path;
1513 struct btrfs_key key;
1514 struct btrfs_file_extent_item *ei;
1515 u8 type;
1516 u8 compression;
1517 unsigned long off;
1518 int len;
1519
1520 path = alloc_path_for_send();
1521 if (!path)
1522 return -ENOMEM;
1523
1524 key.objectid = ino;
1525 key.type = BTRFS_EXTENT_DATA_KEY;
1526 key.offset = 0;
1527 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1528 if (ret < 0)
1529 goto out;
1530 if (ret) {
1531
1532
1533
1534
1535
1536
1537
1538
1539 btrfs_err(root->fs_info,
1540 "Found empty symlink inode %llu at root %llu",
1541 ino, root->root_key.objectid);
1542 ret = -EIO;
1543 goto out;
1544 }
1545
1546 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1547 struct btrfs_file_extent_item);
1548 type = btrfs_file_extent_type(path->nodes[0], ei);
1549 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1550 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1551 BUG_ON(compression);
1552
1553 off = btrfs_file_extent_inline_start(ei);
1554 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
1555
1556 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1557
1558 out:
1559 btrfs_free_path(path);
1560 return ret;
1561 }
1562
1563
1564
1565
1566
1567 static int gen_unique_name(struct send_ctx *sctx,
1568 u64 ino, u64 gen,
1569 struct fs_path *dest)
1570 {
1571 int ret = 0;
1572 struct btrfs_path *path;
1573 struct btrfs_dir_item *di;
1574 char tmp[64];
1575 int len;
1576 u64 idx = 0;
1577
1578 path = alloc_path_for_send();
1579 if (!path)
1580 return -ENOMEM;
1581
1582 while (1) {
1583 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1584 ino, gen, idx);
1585 ASSERT(len < sizeof(tmp));
1586
1587 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1588 path, BTRFS_FIRST_FREE_OBJECTID,
1589 tmp, strlen(tmp), 0);
1590 btrfs_release_path(path);
1591 if (IS_ERR(di)) {
1592 ret = PTR_ERR(di);
1593 goto out;
1594 }
1595 if (di) {
1596
1597 idx++;
1598 continue;
1599 }
1600
1601 if (!sctx->parent_root) {
1602
1603 ret = 0;
1604 break;
1605 }
1606
1607 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1608 path, BTRFS_FIRST_FREE_OBJECTID,
1609 tmp, strlen(tmp), 0);
1610 btrfs_release_path(path);
1611 if (IS_ERR(di)) {
1612 ret = PTR_ERR(di);
1613 goto out;
1614 }
1615 if (di) {
1616
1617 idx++;
1618 continue;
1619 }
1620
1621 break;
1622 }
1623
1624 ret = fs_path_add(dest, tmp, strlen(tmp));
1625
1626 out:
1627 btrfs_free_path(path);
1628 return ret;
1629 }
1630
1631 enum inode_state {
1632 inode_state_no_change,
1633 inode_state_will_create,
1634 inode_state_did_create,
1635 inode_state_will_delete,
1636 inode_state_did_delete,
1637 };
1638
1639 static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1640 {
1641 int ret;
1642 int left_ret;
1643 int right_ret;
1644 u64 left_gen;
1645 u64 right_gen;
1646
1647 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1648 NULL, NULL, NULL);
1649 if (ret < 0 && ret != -ENOENT)
1650 goto out;
1651 left_ret = ret;
1652
1653 if (!sctx->parent_root) {
1654 right_ret = -ENOENT;
1655 } else {
1656 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1657 NULL, NULL, NULL, NULL, NULL);
1658 if (ret < 0 && ret != -ENOENT)
1659 goto out;
1660 right_ret = ret;
1661 }
1662
1663 if (!left_ret && !right_ret) {
1664 if (left_gen == gen && right_gen == gen) {
1665 ret = inode_state_no_change;
1666 } else if (left_gen == gen) {
1667 if (ino < sctx->send_progress)
1668 ret = inode_state_did_create;
1669 else
1670 ret = inode_state_will_create;
1671 } else if (right_gen == gen) {
1672 if (ino < sctx->send_progress)
1673 ret = inode_state_did_delete;
1674 else
1675 ret = inode_state_will_delete;
1676 } else {
1677 ret = -ENOENT;
1678 }
1679 } else if (!left_ret) {
1680 if (left_gen == gen) {
1681 if (ino < sctx->send_progress)
1682 ret = inode_state_did_create;
1683 else
1684 ret = inode_state_will_create;
1685 } else {
1686 ret = -ENOENT;
1687 }
1688 } else if (!right_ret) {
1689 if (right_gen == gen) {
1690 if (ino < sctx->send_progress)
1691 ret = inode_state_did_delete;
1692 else
1693 ret = inode_state_will_delete;
1694 } else {
1695 ret = -ENOENT;
1696 }
1697 } else {
1698 ret = -ENOENT;
1699 }
1700
1701 out:
1702 return ret;
1703 }
1704
1705 static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1706 {
1707 int ret;
1708
1709 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1710 return 1;
1711
1712 ret = get_cur_inode_state(sctx, ino, gen);
1713 if (ret < 0)
1714 goto out;
1715
1716 if (ret == inode_state_no_change ||
1717 ret == inode_state_did_create ||
1718 ret == inode_state_will_delete)
1719 ret = 1;
1720 else
1721 ret = 0;
1722
1723 out:
1724 return ret;
1725 }
1726
1727
1728
1729
1730 static int lookup_dir_item_inode(struct btrfs_root *root,
1731 u64 dir, const char *name, int name_len,
1732 u64 *found_inode)
1733 {
1734 int ret = 0;
1735 struct btrfs_dir_item *di;
1736 struct btrfs_key key;
1737 struct btrfs_path *path;
1738
1739 path = alloc_path_for_send();
1740 if (!path)
1741 return -ENOMEM;
1742
1743 di = btrfs_lookup_dir_item(NULL, root, path,
1744 dir, name, name_len, 0);
1745 if (IS_ERR_OR_NULL(di)) {
1746 ret = di ? PTR_ERR(di) : -ENOENT;
1747 goto out;
1748 }
1749 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1750 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1751 ret = -ENOENT;
1752 goto out;
1753 }
1754 *found_inode = key.objectid;
1755
1756 out:
1757 btrfs_free_path(path);
1758 return ret;
1759 }
1760
1761
1762
1763
1764
1765 static int get_first_ref(struct btrfs_root *root, u64 ino,
1766 u64 *dir, u64 *dir_gen, struct fs_path *name)
1767 {
1768 int ret;
1769 struct btrfs_key key;
1770 struct btrfs_key found_key;
1771 struct btrfs_path *path;
1772 int len;
1773 u64 parent_dir;
1774
1775 path = alloc_path_for_send();
1776 if (!path)
1777 return -ENOMEM;
1778
1779 key.objectid = ino;
1780 key.type = BTRFS_INODE_REF_KEY;
1781 key.offset = 0;
1782
1783 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1784 if (ret < 0)
1785 goto out;
1786 if (!ret)
1787 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1788 path->slots[0]);
1789 if (ret || found_key.objectid != ino ||
1790 (found_key.type != BTRFS_INODE_REF_KEY &&
1791 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1792 ret = -ENOENT;
1793 goto out;
1794 }
1795
1796 if (found_key.type == BTRFS_INODE_REF_KEY) {
1797 struct btrfs_inode_ref *iref;
1798 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1799 struct btrfs_inode_ref);
1800 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1801 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1802 (unsigned long)(iref + 1),
1803 len);
1804 parent_dir = found_key.offset;
1805 } else {
1806 struct btrfs_inode_extref *extref;
1807 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1808 struct btrfs_inode_extref);
1809 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1810 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1811 (unsigned long)&extref->name, len);
1812 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1813 }
1814 if (ret < 0)
1815 goto out;
1816 btrfs_release_path(path);
1817
1818 if (dir_gen) {
1819 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1820 NULL, NULL, NULL, NULL);
1821 if (ret < 0)
1822 goto out;
1823 }
1824
1825 *dir = parent_dir;
1826
1827 out:
1828 btrfs_free_path(path);
1829 return ret;
1830 }
1831
1832 static int is_first_ref(struct btrfs_root *root,
1833 u64 ino, u64 dir,
1834 const char *name, int name_len)
1835 {
1836 int ret;
1837 struct fs_path *tmp_name;
1838 u64 tmp_dir;
1839
1840 tmp_name = fs_path_alloc();
1841 if (!tmp_name)
1842 return -ENOMEM;
1843
1844 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1845 if (ret < 0)
1846 goto out;
1847
1848 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1849 ret = 0;
1850 goto out;
1851 }
1852
1853 ret = !memcmp(tmp_name->start, name, name_len);
1854
1855 out:
1856 fs_path_free(tmp_name);
1857 return ret;
1858 }
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870 static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1871 const char *name, int name_len,
1872 u64 *who_ino, u64 *who_gen, u64 *who_mode)
1873 {
1874 int ret = 0;
1875 u64 gen;
1876 u64 other_inode = 0;
1877
1878 if (!sctx->parent_root)
1879 goto out;
1880
1881 ret = is_inode_existent(sctx, dir, dir_gen);
1882 if (ret <= 0)
1883 goto out;
1884
1885
1886
1887
1888
1889
1890 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
1891 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1892 NULL, NULL, NULL, NULL);
1893 if (ret < 0 && ret != -ENOENT)
1894 goto out;
1895 if (ret) {
1896 ret = 0;
1897 goto out;
1898 }
1899 if (gen != dir_gen)
1900 goto out;
1901 }
1902
1903 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1904 &other_inode);
1905 if (ret < 0 && ret != -ENOENT)
1906 goto out;
1907 if (ret) {
1908 ret = 0;
1909 goto out;
1910 }
1911
1912
1913
1914
1915
1916
1917 if (other_inode > sctx->send_progress ||
1918 is_waiting_for_move(sctx, other_inode)) {
1919 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1920 who_gen, who_mode, NULL, NULL, NULL, NULL);
1921 if (ret < 0)
1922 goto out;
1923
1924 ret = 1;
1925 *who_ino = other_inode;
1926 } else {
1927 ret = 0;
1928 }
1929
1930 out:
1931 return ret;
1932 }
1933
1934
1935
1936
1937
1938
1939
1940
1941 static int did_overwrite_ref(struct send_ctx *sctx,
1942 u64 dir, u64 dir_gen,
1943 u64 ino, u64 ino_gen,
1944 const char *name, int name_len)
1945 {
1946 int ret = 0;
1947 u64 gen;
1948 u64 ow_inode;
1949
1950 if (!sctx->parent_root)
1951 goto out;
1952
1953 ret = is_inode_existent(sctx, dir, dir_gen);
1954 if (ret <= 0)
1955 goto out;
1956
1957 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1958 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1959 NULL, NULL, NULL, NULL);
1960 if (ret < 0 && ret != -ENOENT)
1961 goto out;
1962 if (ret) {
1963 ret = 0;
1964 goto out;
1965 }
1966 if (gen != dir_gen)
1967 goto out;
1968 }
1969
1970
1971 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1972 &ow_inode);
1973 if (ret < 0 && ret != -ENOENT)
1974 goto out;
1975 if (ret) {
1976
1977 ret = 0;
1978 goto out;
1979 }
1980
1981 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1982 NULL, NULL, NULL);
1983 if (ret < 0)
1984 goto out;
1985
1986 if (ow_inode == ino && gen == ino_gen) {
1987 ret = 0;
1988 goto out;
1989 }
1990
1991
1992
1993
1994
1995
1996
1997 if ((ow_inode < sctx->send_progress) ||
1998 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1999 gen == sctx->cur_inode_gen))
2000 ret = 1;
2001 else
2002 ret = 0;
2003
2004 out:
2005 return ret;
2006 }
2007
2008
2009
2010
2011
2012
2013 static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
2014 {
2015 int ret = 0;
2016 struct fs_path *name = NULL;
2017 u64 dir;
2018 u64 dir_gen;
2019
2020 if (!sctx->parent_root)
2021 goto out;
2022
2023 name = fs_path_alloc();
2024 if (!name)
2025 return -ENOMEM;
2026
2027 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
2028 if (ret < 0)
2029 goto out;
2030
2031 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
2032 name->start, fs_path_len(name));
2033
2034 out:
2035 fs_path_free(name);
2036 return ret;
2037 }
2038
2039
2040
2041
2042
2043
2044
2045 static int name_cache_insert(struct send_ctx *sctx,
2046 struct name_cache_entry *nce)
2047 {
2048 int ret = 0;
2049 struct list_head *nce_head;
2050
2051 nce_head = radix_tree_lookup(&sctx->name_cache,
2052 (unsigned long)nce->ino);
2053 if (!nce_head) {
2054 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
2055 if (!nce_head) {
2056 kfree(nce);
2057 return -ENOMEM;
2058 }
2059 INIT_LIST_HEAD(nce_head);
2060
2061 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
2062 if (ret < 0) {
2063 kfree(nce_head);
2064 kfree(nce);
2065 return ret;
2066 }
2067 }
2068 list_add_tail(&nce->radix_list, nce_head);
2069 list_add_tail(&nce->list, &sctx->name_cache_list);
2070 sctx->name_cache_size++;
2071
2072 return ret;
2073 }
2074
2075 static void name_cache_delete(struct send_ctx *sctx,
2076 struct name_cache_entry *nce)
2077 {
2078 struct list_head *nce_head;
2079
2080 nce_head = radix_tree_lookup(&sctx->name_cache,
2081 (unsigned long)nce->ino);
2082 if (!nce_head) {
2083 btrfs_err(sctx->send_root->fs_info,
2084 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2085 nce->ino, sctx->name_cache_size);
2086 }
2087
2088 list_del(&nce->radix_list);
2089 list_del(&nce->list);
2090 sctx->name_cache_size--;
2091
2092
2093
2094
2095 if (nce_head && list_empty(nce_head)) {
2096 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2097 kfree(nce_head);
2098 }
2099 }
2100
2101 static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2102 u64 ino, u64 gen)
2103 {
2104 struct list_head *nce_head;
2105 struct name_cache_entry *cur;
2106
2107 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2108 if (!nce_head)
2109 return NULL;
2110
2111 list_for_each_entry(cur, nce_head, radix_list) {
2112 if (cur->ino == ino && cur->gen == gen)
2113 return cur;
2114 }
2115 return NULL;
2116 }
2117
2118
2119
2120
2121 static void name_cache_clean_unused(struct send_ctx *sctx)
2122 {
2123 struct name_cache_entry *nce;
2124
2125 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2126 return;
2127
2128 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2129 nce = list_entry(sctx->name_cache_list.next,
2130 struct name_cache_entry, list);
2131 name_cache_delete(sctx, nce);
2132 kfree(nce);
2133 }
2134 }
2135
2136 static void name_cache_free(struct send_ctx *sctx)
2137 {
2138 struct name_cache_entry *nce;
2139
2140 while (!list_empty(&sctx->name_cache_list)) {
2141 nce = list_entry(sctx->name_cache_list.next,
2142 struct name_cache_entry, list);
2143 name_cache_delete(sctx, nce);
2144 kfree(nce);
2145 }
2146 }
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156 static int __get_cur_name_and_parent(struct send_ctx *sctx,
2157 u64 ino, u64 gen,
2158 u64 *parent_ino,
2159 u64 *parent_gen,
2160 struct fs_path *dest)
2161 {
2162 int ret;
2163 int nce_ret;
2164 struct name_cache_entry *nce = NULL;
2165
2166
2167
2168
2169
2170
2171 nce = name_cache_search(sctx, ino, gen);
2172 if (nce) {
2173 if (ino < sctx->send_progress && nce->need_later_update) {
2174 name_cache_delete(sctx, nce);
2175 kfree(nce);
2176 nce = NULL;
2177 } else {
2178
2179
2180
2181
2182
2183 list_move_tail(&nce->list, &sctx->name_cache_list);
2184
2185 *parent_ino = nce->parent_ino;
2186 *parent_gen = nce->parent_gen;
2187 ret = fs_path_add(dest, nce->name, nce->name_len);
2188 if (ret < 0)
2189 goto out;
2190 ret = nce->ret;
2191 goto out;
2192 }
2193 }
2194
2195
2196
2197
2198
2199
2200 ret = is_inode_existent(sctx, ino, gen);
2201 if (ret < 0)
2202 goto out;
2203
2204 if (!ret) {
2205 ret = gen_unique_name(sctx, ino, gen, dest);
2206 if (ret < 0)
2207 goto out;
2208 ret = 1;
2209 goto out_cache;
2210 }
2211
2212
2213
2214
2215
2216 if (ino < sctx->send_progress)
2217 ret = get_first_ref(sctx->send_root, ino,
2218 parent_ino, parent_gen, dest);
2219 else
2220 ret = get_first_ref(sctx->parent_root, ino,
2221 parent_ino, parent_gen, dest);
2222 if (ret < 0)
2223 goto out;
2224
2225
2226
2227
2228
2229 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2230 dest->start, dest->end - dest->start);
2231 if (ret < 0)
2232 goto out;
2233 if (ret) {
2234 fs_path_reset(dest);
2235 ret = gen_unique_name(sctx, ino, gen, dest);
2236 if (ret < 0)
2237 goto out;
2238 ret = 1;
2239 }
2240
2241 out_cache:
2242
2243
2244
2245 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2246 if (!nce) {
2247 ret = -ENOMEM;
2248 goto out;
2249 }
2250
2251 nce->ino = ino;
2252 nce->gen = gen;
2253 nce->parent_ino = *parent_ino;
2254 nce->parent_gen = *parent_gen;
2255 nce->name_len = fs_path_len(dest);
2256 nce->ret = ret;
2257 strcpy(nce->name, dest->start);
2258
2259 if (ino < sctx->send_progress)
2260 nce->need_later_update = 0;
2261 else
2262 nce->need_later_update = 1;
2263
2264 nce_ret = name_cache_insert(sctx, nce);
2265 if (nce_ret < 0)
2266 ret = nce_ret;
2267 name_cache_clean_unused(sctx);
2268
2269 out:
2270 return ret;
2271 }
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298 static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2299 struct fs_path *dest)
2300 {
2301 int ret = 0;
2302 struct fs_path *name = NULL;
2303 u64 parent_inode = 0;
2304 u64 parent_gen = 0;
2305 int stop = 0;
2306
2307 name = fs_path_alloc();
2308 if (!name) {
2309 ret = -ENOMEM;
2310 goto out;
2311 }
2312
2313 dest->reversed = 1;
2314 fs_path_reset(dest);
2315
2316 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2317 struct waiting_dir_move *wdm;
2318
2319 fs_path_reset(name);
2320
2321 if (is_waiting_for_rm(sctx, ino, gen)) {
2322 ret = gen_unique_name(sctx, ino, gen, name);
2323 if (ret < 0)
2324 goto out;
2325 ret = fs_path_add_path(dest, name);
2326 break;
2327 }
2328
2329 wdm = get_waiting_dir_move(sctx, ino);
2330 if (wdm && wdm->orphanized) {
2331 ret = gen_unique_name(sctx, ino, gen, name);
2332 stop = 1;
2333 } else if (wdm) {
2334 ret = get_first_ref(sctx->parent_root, ino,
2335 &parent_inode, &parent_gen, name);
2336 } else {
2337 ret = __get_cur_name_and_parent(sctx, ino, gen,
2338 &parent_inode,
2339 &parent_gen, name);
2340 if (ret)
2341 stop = 1;
2342 }
2343
2344 if (ret < 0)
2345 goto out;
2346
2347 ret = fs_path_add_path(dest, name);
2348 if (ret < 0)
2349 goto out;
2350
2351 ino = parent_inode;
2352 gen = parent_gen;
2353 }
2354
2355 out:
2356 fs_path_free(name);
2357 if (!ret)
2358 fs_path_unreverse(dest);
2359 return ret;
2360 }
2361
2362
2363
2364
2365 static int send_subvol_begin(struct send_ctx *sctx)
2366 {
2367 int ret;
2368 struct btrfs_root *send_root = sctx->send_root;
2369 struct btrfs_root *parent_root = sctx->parent_root;
2370 struct btrfs_path *path;
2371 struct btrfs_key key;
2372 struct btrfs_root_ref *ref;
2373 struct extent_buffer *leaf;
2374 char *name = NULL;
2375 int namelen;
2376
2377 path = btrfs_alloc_path();
2378 if (!path)
2379 return -ENOMEM;
2380
2381 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2382 if (!name) {
2383 btrfs_free_path(path);
2384 return -ENOMEM;
2385 }
2386
2387 key.objectid = send_root->root_key.objectid;
2388 key.type = BTRFS_ROOT_BACKREF_KEY;
2389 key.offset = 0;
2390
2391 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2392 &key, path, 1, 0);
2393 if (ret < 0)
2394 goto out;
2395 if (ret) {
2396 ret = -ENOENT;
2397 goto out;
2398 }
2399
2400 leaf = path->nodes[0];
2401 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2402 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2403 key.objectid != send_root->root_key.objectid) {
2404 ret = -ENOENT;
2405 goto out;
2406 }
2407 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2408 namelen = btrfs_root_ref_name_len(leaf, ref);
2409 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2410 btrfs_release_path(path);
2411
2412 if (parent_root) {
2413 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2414 if (ret < 0)
2415 goto out;
2416 } else {
2417 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2418 if (ret < 0)
2419 goto out;
2420 }
2421
2422 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2423
2424 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2425 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2426 sctx->send_root->root_item.received_uuid);
2427 else
2428 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2429 sctx->send_root->root_item.uuid);
2430
2431 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2432 btrfs_root_ctransid(&sctx->send_root->root_item));
2433 if (parent_root) {
2434 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2435 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2436 parent_root->root_item.received_uuid);
2437 else
2438 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2439 parent_root->root_item.uuid);
2440 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2441 btrfs_root_ctransid(&sctx->parent_root->root_item));
2442 }
2443
2444 ret = send_cmd(sctx);
2445
2446 tlv_put_failure:
2447 out:
2448 btrfs_free_path(path);
2449 kfree(name);
2450 return ret;
2451 }
2452
2453 static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2454 {
2455 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2456 int ret = 0;
2457 struct fs_path *p;
2458
2459 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
2460
2461 p = fs_path_alloc();
2462 if (!p)
2463 return -ENOMEM;
2464
2465 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2466 if (ret < 0)
2467 goto out;
2468
2469 ret = get_cur_path(sctx, ino, gen, p);
2470 if (ret < 0)
2471 goto out;
2472 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2473 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2474
2475 ret = send_cmd(sctx);
2476
2477 tlv_put_failure:
2478 out:
2479 fs_path_free(p);
2480 return ret;
2481 }
2482
2483 static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2484 {
2485 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2486 int ret = 0;
2487 struct fs_path *p;
2488
2489 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
2490
2491 p = fs_path_alloc();
2492 if (!p)
2493 return -ENOMEM;
2494
2495 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2496 if (ret < 0)
2497 goto out;
2498
2499 ret = get_cur_path(sctx, ino, gen, p);
2500 if (ret < 0)
2501 goto out;
2502 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2503 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2504
2505 ret = send_cmd(sctx);
2506
2507 tlv_put_failure:
2508 out:
2509 fs_path_free(p);
2510 return ret;
2511 }
2512
2513 static int send_fileattr(struct send_ctx *sctx, u64 ino, u64 gen, u64 fileattr)
2514 {
2515 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2516 int ret = 0;
2517 struct fs_path *p;
2518
2519 if (sctx->proto < 2)
2520 return 0;
2521
2522 btrfs_debug(fs_info, "send_fileattr %llu fileattr=%llu", ino, fileattr);
2523
2524 p = fs_path_alloc();
2525 if (!p)
2526 return -ENOMEM;
2527
2528 ret = begin_cmd(sctx, BTRFS_SEND_C_FILEATTR);
2529 if (ret < 0)
2530 goto out;
2531
2532 ret = get_cur_path(sctx, ino, gen, p);
2533 if (ret < 0)
2534 goto out;
2535 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2536 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILEATTR, fileattr);
2537
2538 ret = send_cmd(sctx);
2539
2540 tlv_put_failure:
2541 out:
2542 fs_path_free(p);
2543 return ret;
2544 }
2545
2546 static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2547 {
2548 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2549 int ret = 0;
2550 struct fs_path *p;
2551
2552 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2553 ino, uid, gid);
2554
2555 p = fs_path_alloc();
2556 if (!p)
2557 return -ENOMEM;
2558
2559 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2560 if (ret < 0)
2561 goto out;
2562
2563 ret = get_cur_path(sctx, ino, gen, p);
2564 if (ret < 0)
2565 goto out;
2566 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2567 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2568 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2569
2570 ret = send_cmd(sctx);
2571
2572 tlv_put_failure:
2573 out:
2574 fs_path_free(p);
2575 return ret;
2576 }
2577
2578 static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2579 {
2580 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2581 int ret = 0;
2582 struct fs_path *p = NULL;
2583 struct btrfs_inode_item *ii;
2584 struct btrfs_path *path = NULL;
2585 struct extent_buffer *eb;
2586 struct btrfs_key key;
2587 int slot;
2588
2589 btrfs_debug(fs_info, "send_utimes %llu", ino);
2590
2591 p = fs_path_alloc();
2592 if (!p)
2593 return -ENOMEM;
2594
2595 path = alloc_path_for_send();
2596 if (!path) {
2597 ret = -ENOMEM;
2598 goto out;
2599 }
2600
2601 key.objectid = ino;
2602 key.type = BTRFS_INODE_ITEM_KEY;
2603 key.offset = 0;
2604 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2605 if (ret > 0)
2606 ret = -ENOENT;
2607 if (ret < 0)
2608 goto out;
2609
2610 eb = path->nodes[0];
2611 slot = path->slots[0];
2612 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2613
2614 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2615 if (ret < 0)
2616 goto out;
2617
2618 ret = get_cur_path(sctx, ino, gen, p);
2619 if (ret < 0)
2620 goto out;
2621 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2622 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2623 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2624 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2625 if (sctx->proto >= 2)
2626 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_OTIME, eb, &ii->otime);
2627
2628 ret = send_cmd(sctx);
2629
2630 tlv_put_failure:
2631 out:
2632 fs_path_free(p);
2633 btrfs_free_path(path);
2634 return ret;
2635 }
2636
2637
2638
2639
2640
2641
2642 static int send_create_inode(struct send_ctx *sctx, u64 ino)
2643 {
2644 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2645 int ret = 0;
2646 struct fs_path *p;
2647 int cmd;
2648 u64 gen;
2649 u64 mode;
2650 u64 rdev;
2651
2652 btrfs_debug(fs_info, "send_create_inode %llu", ino);
2653
2654 p = fs_path_alloc();
2655 if (!p)
2656 return -ENOMEM;
2657
2658 if (ino != sctx->cur_ino) {
2659 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2660 NULL, NULL, &rdev, NULL);
2661 if (ret < 0)
2662 goto out;
2663 } else {
2664 gen = sctx->cur_inode_gen;
2665 mode = sctx->cur_inode_mode;
2666 rdev = sctx->cur_inode_rdev;
2667 }
2668
2669 if (S_ISREG(mode)) {
2670 cmd = BTRFS_SEND_C_MKFILE;
2671 } else if (S_ISDIR(mode)) {
2672 cmd = BTRFS_SEND_C_MKDIR;
2673 } else if (S_ISLNK(mode)) {
2674 cmd = BTRFS_SEND_C_SYMLINK;
2675 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2676 cmd = BTRFS_SEND_C_MKNOD;
2677 } else if (S_ISFIFO(mode)) {
2678 cmd = BTRFS_SEND_C_MKFIFO;
2679 } else if (S_ISSOCK(mode)) {
2680 cmd = BTRFS_SEND_C_MKSOCK;
2681 } else {
2682 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2683 (int)(mode & S_IFMT));
2684 ret = -EOPNOTSUPP;
2685 goto out;
2686 }
2687
2688 ret = begin_cmd(sctx, cmd);
2689 if (ret < 0)
2690 goto out;
2691
2692 ret = gen_unique_name(sctx, ino, gen, p);
2693 if (ret < 0)
2694 goto out;
2695
2696 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2697 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2698
2699 if (S_ISLNK(mode)) {
2700 fs_path_reset(p);
2701 ret = read_symlink(sctx->send_root, ino, p);
2702 if (ret < 0)
2703 goto out;
2704 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2705 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2706 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2707 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2708 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2709 }
2710
2711 ret = send_cmd(sctx);
2712 if (ret < 0)
2713 goto out;
2714
2715
2716 tlv_put_failure:
2717 out:
2718 fs_path_free(p);
2719 return ret;
2720 }
2721
2722
2723
2724
2725
2726
2727 static int did_create_dir(struct send_ctx *sctx, u64 dir)
2728 {
2729 int ret = 0;
2730 int iter_ret = 0;
2731 struct btrfs_path *path = NULL;
2732 struct btrfs_key key;
2733 struct btrfs_key found_key;
2734 struct btrfs_key di_key;
2735 struct btrfs_dir_item *di;
2736
2737 path = alloc_path_for_send();
2738 if (!path)
2739 return -ENOMEM;
2740
2741 key.objectid = dir;
2742 key.type = BTRFS_DIR_INDEX_KEY;
2743 key.offset = 0;
2744
2745 btrfs_for_each_slot(sctx->send_root, &key, &found_key, path, iter_ret) {
2746 struct extent_buffer *eb = path->nodes[0];
2747
2748 if (found_key.objectid != key.objectid ||
2749 found_key.type != key.type) {
2750 ret = 0;
2751 break;
2752 }
2753
2754 di = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dir_item);
2755 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2756
2757 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2758 di_key.objectid < sctx->send_progress) {
2759 ret = 1;
2760 break;
2761 }
2762 }
2763
2764 if (iter_ret < 0)
2765 ret = iter_ret;
2766
2767 btrfs_free_path(path);
2768 return ret;
2769 }
2770
2771
2772
2773
2774
2775
2776
2777 static int send_create_inode_if_needed(struct send_ctx *sctx)
2778 {
2779 int ret;
2780
2781 if (S_ISDIR(sctx->cur_inode_mode)) {
2782 ret = did_create_dir(sctx, sctx->cur_ino);
2783 if (ret < 0)
2784 return ret;
2785 else if (ret > 0)
2786 return 0;
2787 }
2788
2789 return send_create_inode(sctx, sctx->cur_ino);
2790 }
2791
2792 struct recorded_ref {
2793 struct list_head list;
2794 char *name;
2795 struct fs_path *full_path;
2796 u64 dir;
2797 u64 dir_gen;
2798 int name_len;
2799 struct rb_node node;
2800 struct rb_root *root;
2801 };
2802
2803 static struct recorded_ref *recorded_ref_alloc(void)
2804 {
2805 struct recorded_ref *ref;
2806
2807 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
2808 if (!ref)
2809 return NULL;
2810 RB_CLEAR_NODE(&ref->node);
2811 INIT_LIST_HEAD(&ref->list);
2812 return ref;
2813 }
2814
2815 static void recorded_ref_free(struct recorded_ref *ref)
2816 {
2817 if (!ref)
2818 return;
2819 if (!RB_EMPTY_NODE(&ref->node))
2820 rb_erase(&ref->node, ref->root);
2821 list_del(&ref->list);
2822 fs_path_free(ref->full_path);
2823 kfree(ref);
2824 }
2825
2826 static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2827 {
2828 ref->full_path = path;
2829 ref->name = (char *)kbasename(ref->full_path->start);
2830 ref->name_len = ref->full_path->end - ref->name;
2831 }
2832
2833 static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2834 {
2835 struct recorded_ref *new;
2836
2837 new = recorded_ref_alloc();
2838 if (!new)
2839 return -ENOMEM;
2840
2841 new->dir = ref->dir;
2842 new->dir_gen = ref->dir_gen;
2843 list_add_tail(&new->list, list);
2844 return 0;
2845 }
2846
2847 static void __free_recorded_refs(struct list_head *head)
2848 {
2849 struct recorded_ref *cur;
2850
2851 while (!list_empty(head)) {
2852 cur = list_entry(head->next, struct recorded_ref, list);
2853 recorded_ref_free(cur);
2854 }
2855 }
2856
2857 static void free_recorded_refs(struct send_ctx *sctx)
2858 {
2859 __free_recorded_refs(&sctx->new_refs);
2860 __free_recorded_refs(&sctx->deleted_refs);
2861 }
2862
2863
2864
2865
2866
2867
2868 static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2869 struct fs_path *path)
2870 {
2871 int ret;
2872 struct fs_path *orphan;
2873
2874 orphan = fs_path_alloc();
2875 if (!orphan)
2876 return -ENOMEM;
2877
2878 ret = gen_unique_name(sctx, ino, gen, orphan);
2879 if (ret < 0)
2880 goto out;
2881
2882 ret = send_rename(sctx, path, orphan);
2883
2884 out:
2885 fs_path_free(orphan);
2886 return ret;
2887 }
2888
2889 static struct orphan_dir_info *add_orphan_dir_info(struct send_ctx *sctx,
2890 u64 dir_ino, u64 dir_gen)
2891 {
2892 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2893 struct rb_node *parent = NULL;
2894 struct orphan_dir_info *entry, *odi;
2895
2896 while (*p) {
2897 parent = *p;
2898 entry = rb_entry(parent, struct orphan_dir_info, node);
2899 if (dir_ino < entry->ino)
2900 p = &(*p)->rb_left;
2901 else if (dir_ino > entry->ino)
2902 p = &(*p)->rb_right;
2903 else if (dir_gen < entry->gen)
2904 p = &(*p)->rb_left;
2905 else if (dir_gen > entry->gen)
2906 p = &(*p)->rb_right;
2907 else
2908 return entry;
2909 }
2910
2911 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2912 if (!odi)
2913 return ERR_PTR(-ENOMEM);
2914 odi->ino = dir_ino;
2915 odi->gen = dir_gen;
2916 odi->last_dir_index_offset = 0;
2917
2918 rb_link_node(&odi->node, parent, p);
2919 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2920 return odi;
2921 }
2922
2923 static struct orphan_dir_info *get_orphan_dir_info(struct send_ctx *sctx,
2924 u64 dir_ino, u64 gen)
2925 {
2926 struct rb_node *n = sctx->orphan_dirs.rb_node;
2927 struct orphan_dir_info *entry;
2928
2929 while (n) {
2930 entry = rb_entry(n, struct orphan_dir_info, node);
2931 if (dir_ino < entry->ino)
2932 n = n->rb_left;
2933 else if (dir_ino > entry->ino)
2934 n = n->rb_right;
2935 else if (gen < entry->gen)
2936 n = n->rb_left;
2937 else if (gen > entry->gen)
2938 n = n->rb_right;
2939 else
2940 return entry;
2941 }
2942 return NULL;
2943 }
2944
2945 static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino, u64 gen)
2946 {
2947 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino, gen);
2948
2949 return odi != NULL;
2950 }
2951
2952 static void free_orphan_dir_info(struct send_ctx *sctx,
2953 struct orphan_dir_info *odi)
2954 {
2955 if (!odi)
2956 return;
2957 rb_erase(&odi->node, &sctx->orphan_dirs);
2958 kfree(odi);
2959 }
2960
2961
2962
2963
2964
2965
2966 static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2967 u64 send_progress)
2968 {
2969 int ret = 0;
2970 int iter_ret = 0;
2971 struct btrfs_root *root = sctx->parent_root;
2972 struct btrfs_path *path;
2973 struct btrfs_key key;
2974 struct btrfs_key found_key;
2975 struct btrfs_key loc;
2976 struct btrfs_dir_item *di;
2977 struct orphan_dir_info *odi = NULL;
2978
2979
2980
2981
2982 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2983 return 0;
2984
2985 path = alloc_path_for_send();
2986 if (!path)
2987 return -ENOMEM;
2988
2989 key.objectid = dir;
2990 key.type = BTRFS_DIR_INDEX_KEY;
2991 key.offset = 0;
2992
2993 odi = get_orphan_dir_info(sctx, dir, dir_gen);
2994 if (odi)
2995 key.offset = odi->last_dir_index_offset;
2996
2997 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
2998 struct waiting_dir_move *dm;
2999
3000 if (found_key.objectid != key.objectid ||
3001 found_key.type != key.type)
3002 break;
3003
3004 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
3005 struct btrfs_dir_item);
3006 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
3007
3008 dm = get_waiting_dir_move(sctx, loc.objectid);
3009 if (dm) {
3010 odi = add_orphan_dir_info(sctx, dir, dir_gen);
3011 if (IS_ERR(odi)) {
3012 ret = PTR_ERR(odi);
3013 goto out;
3014 }
3015 odi->gen = dir_gen;
3016 odi->last_dir_index_offset = found_key.offset;
3017 dm->rmdir_ino = dir;
3018 dm->rmdir_gen = dir_gen;
3019 ret = 0;
3020 goto out;
3021 }
3022
3023 if (loc.objectid > send_progress) {
3024 odi = add_orphan_dir_info(sctx, dir, dir_gen);
3025 if (IS_ERR(odi)) {
3026 ret = PTR_ERR(odi);
3027 goto out;
3028 }
3029 odi->gen = dir_gen;
3030 odi->last_dir_index_offset = found_key.offset;
3031 ret = 0;
3032 goto out;
3033 }
3034 }
3035 if (iter_ret < 0) {
3036 ret = iter_ret;
3037 goto out;
3038 }
3039 free_orphan_dir_info(sctx, odi);
3040
3041 ret = 1;
3042
3043 out:
3044 btrfs_free_path(path);
3045 return ret;
3046 }
3047
3048 static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3049 {
3050 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3051
3052 return entry != NULL;
3053 }
3054
3055 static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3056 {
3057 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3058 struct rb_node *parent = NULL;
3059 struct waiting_dir_move *entry, *dm;
3060
3061 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
3062 if (!dm)
3063 return -ENOMEM;
3064 dm->ino = ino;
3065 dm->rmdir_ino = 0;
3066 dm->rmdir_gen = 0;
3067 dm->orphanized = orphanized;
3068
3069 while (*p) {
3070 parent = *p;
3071 entry = rb_entry(parent, struct waiting_dir_move, node);
3072 if (ino < entry->ino) {
3073 p = &(*p)->rb_left;
3074 } else if (ino > entry->ino) {
3075 p = &(*p)->rb_right;
3076 } else {
3077 kfree(dm);
3078 return -EEXIST;
3079 }
3080 }
3081
3082 rb_link_node(&dm->node, parent, p);
3083 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3084 return 0;
3085 }
3086
3087 static struct waiting_dir_move *
3088 get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3089 {
3090 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3091 struct waiting_dir_move *entry;
3092
3093 while (n) {
3094 entry = rb_entry(n, struct waiting_dir_move, node);
3095 if (ino < entry->ino)
3096 n = n->rb_left;
3097 else if (ino > entry->ino)
3098 n = n->rb_right;
3099 else
3100 return entry;
3101 }
3102 return NULL;
3103 }
3104
3105 static void free_waiting_dir_move(struct send_ctx *sctx,
3106 struct waiting_dir_move *dm)
3107 {
3108 if (!dm)
3109 return;
3110 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3111 kfree(dm);
3112 }
3113
3114 static int add_pending_dir_move(struct send_ctx *sctx,
3115 u64 ino,
3116 u64 ino_gen,
3117 u64 parent_ino,
3118 struct list_head *new_refs,
3119 struct list_head *deleted_refs,
3120 const bool is_orphan)
3121 {
3122 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3123 struct rb_node *parent = NULL;
3124 struct pending_dir_move *entry = NULL, *pm;
3125 struct recorded_ref *cur;
3126 int exists = 0;
3127 int ret;
3128
3129 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3130 if (!pm)
3131 return -ENOMEM;
3132 pm->parent_ino = parent_ino;
3133 pm->ino = ino;
3134 pm->gen = ino_gen;
3135 INIT_LIST_HEAD(&pm->list);
3136 INIT_LIST_HEAD(&pm->update_refs);
3137 RB_CLEAR_NODE(&pm->node);
3138
3139 while (*p) {
3140 parent = *p;
3141 entry = rb_entry(parent, struct pending_dir_move, node);
3142 if (parent_ino < entry->parent_ino) {
3143 p = &(*p)->rb_left;
3144 } else if (parent_ino > entry->parent_ino) {
3145 p = &(*p)->rb_right;
3146 } else {
3147 exists = 1;
3148 break;
3149 }
3150 }
3151
3152 list_for_each_entry(cur, deleted_refs, list) {
3153 ret = dup_ref(cur, &pm->update_refs);
3154 if (ret < 0)
3155 goto out;
3156 }
3157 list_for_each_entry(cur, new_refs, list) {
3158 ret = dup_ref(cur, &pm->update_refs);
3159 if (ret < 0)
3160 goto out;
3161 }
3162
3163 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3164 if (ret)
3165 goto out;
3166
3167 if (exists) {
3168 list_add_tail(&pm->list, &entry->list);
3169 } else {
3170 rb_link_node(&pm->node, parent, p);
3171 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3172 }
3173 ret = 0;
3174 out:
3175 if (ret) {
3176 __free_recorded_refs(&pm->update_refs);
3177 kfree(pm);
3178 }
3179 return ret;
3180 }
3181
3182 static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3183 u64 parent_ino)
3184 {
3185 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3186 struct pending_dir_move *entry;
3187
3188 while (n) {
3189 entry = rb_entry(n, struct pending_dir_move, node);
3190 if (parent_ino < entry->parent_ino)
3191 n = n->rb_left;
3192 else if (parent_ino > entry->parent_ino)
3193 n = n->rb_right;
3194 else
3195 return entry;
3196 }
3197 return NULL;
3198 }
3199
3200 static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3201 u64 ino, u64 gen, u64 *ancestor_ino)
3202 {
3203 int ret = 0;
3204 u64 parent_inode = 0;
3205 u64 parent_gen = 0;
3206 u64 start_ino = ino;
3207
3208 *ancestor_ino = 0;
3209 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3210 fs_path_reset(name);
3211
3212 if (is_waiting_for_rm(sctx, ino, gen))
3213 break;
3214 if (is_waiting_for_move(sctx, ino)) {
3215 if (*ancestor_ino == 0)
3216 *ancestor_ino = ino;
3217 ret = get_first_ref(sctx->parent_root, ino,
3218 &parent_inode, &parent_gen, name);
3219 } else {
3220 ret = __get_cur_name_and_parent(sctx, ino, gen,
3221 &parent_inode,
3222 &parent_gen, name);
3223 if (ret > 0) {
3224 ret = 0;
3225 break;
3226 }
3227 }
3228 if (ret < 0)
3229 break;
3230 if (parent_inode == start_ino) {
3231 ret = 1;
3232 if (*ancestor_ino == 0)
3233 *ancestor_ino = ino;
3234 break;
3235 }
3236 ino = parent_inode;
3237 gen = parent_gen;
3238 }
3239 return ret;
3240 }
3241
3242 static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3243 {
3244 struct fs_path *from_path = NULL;
3245 struct fs_path *to_path = NULL;
3246 struct fs_path *name = NULL;
3247 u64 orig_progress = sctx->send_progress;
3248 struct recorded_ref *cur;
3249 u64 parent_ino, parent_gen;
3250 struct waiting_dir_move *dm = NULL;
3251 u64 rmdir_ino = 0;
3252 u64 rmdir_gen;
3253 u64 ancestor;
3254 bool is_orphan;
3255 int ret;
3256
3257 name = fs_path_alloc();
3258 from_path = fs_path_alloc();
3259 if (!name || !from_path) {
3260 ret = -ENOMEM;
3261 goto out;
3262 }
3263
3264 dm = get_waiting_dir_move(sctx, pm->ino);
3265 ASSERT(dm);
3266 rmdir_ino = dm->rmdir_ino;
3267 rmdir_gen = dm->rmdir_gen;
3268 is_orphan = dm->orphanized;
3269 free_waiting_dir_move(sctx, dm);
3270
3271 if (is_orphan) {
3272 ret = gen_unique_name(sctx, pm->ino,
3273 pm->gen, from_path);
3274 } else {
3275 ret = get_first_ref(sctx->parent_root, pm->ino,
3276 &parent_ino, &parent_gen, name);
3277 if (ret < 0)
3278 goto out;
3279 ret = get_cur_path(sctx, parent_ino, parent_gen,
3280 from_path);
3281 if (ret < 0)
3282 goto out;
3283 ret = fs_path_add_path(from_path, name);
3284 }
3285 if (ret < 0)
3286 goto out;
3287
3288 sctx->send_progress = sctx->cur_ino + 1;
3289 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3290 if (ret < 0)
3291 goto out;
3292 if (ret) {
3293 LIST_HEAD(deleted_refs);
3294 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3295 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3296 &pm->update_refs, &deleted_refs,
3297 is_orphan);
3298 if (ret < 0)
3299 goto out;
3300 if (rmdir_ino) {
3301 dm = get_waiting_dir_move(sctx, pm->ino);
3302 ASSERT(dm);
3303 dm->rmdir_ino = rmdir_ino;
3304 dm->rmdir_gen = rmdir_gen;
3305 }
3306 goto out;
3307 }
3308 fs_path_reset(name);
3309 to_path = name;
3310 name = NULL;
3311 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3312 if (ret < 0)
3313 goto out;
3314
3315 ret = send_rename(sctx, from_path, to_path);
3316 if (ret < 0)
3317 goto out;
3318
3319 if (rmdir_ino) {
3320 struct orphan_dir_info *odi;
3321 u64 gen;
3322
3323 odi = get_orphan_dir_info(sctx, rmdir_ino, rmdir_gen);
3324 if (!odi) {
3325
3326 goto finish;
3327 }
3328 gen = odi->gen;
3329
3330 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
3331 if (ret < 0)
3332 goto out;
3333 if (!ret)
3334 goto finish;
3335
3336 name = fs_path_alloc();
3337 if (!name) {
3338 ret = -ENOMEM;
3339 goto out;
3340 }
3341 ret = get_cur_path(sctx, rmdir_ino, gen, name);
3342 if (ret < 0)
3343 goto out;
3344 ret = send_rmdir(sctx, name);
3345 if (ret < 0)
3346 goto out;
3347 }
3348
3349 finish:
3350 ret = send_utimes(sctx, pm->ino, pm->gen);
3351 if (ret < 0)
3352 goto out;
3353
3354
3355
3356
3357
3358 list_for_each_entry(cur, &pm->update_refs, list) {
3359
3360
3361
3362 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3363 NULL, NULL, NULL, NULL, NULL, NULL);
3364 if (ret == -ENOENT) {
3365 ret = 0;
3366 continue;
3367 }
3368 if (ret < 0)
3369 goto out;
3370
3371 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3372 if (ret < 0)
3373 goto out;
3374 }
3375
3376 out:
3377 fs_path_free(name);
3378 fs_path_free(from_path);
3379 fs_path_free(to_path);
3380 sctx->send_progress = orig_progress;
3381
3382 return ret;
3383 }
3384
3385 static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3386 {
3387 if (!list_empty(&m->list))
3388 list_del(&m->list);
3389 if (!RB_EMPTY_NODE(&m->node))
3390 rb_erase(&m->node, &sctx->pending_dir_moves);
3391 __free_recorded_refs(&m->update_refs);
3392 kfree(m);
3393 }
3394
3395 static void tail_append_pending_moves(struct send_ctx *sctx,
3396 struct pending_dir_move *moves,
3397 struct list_head *stack)
3398 {
3399 if (list_empty(&moves->list)) {
3400 list_add_tail(&moves->list, stack);
3401 } else {
3402 LIST_HEAD(list);
3403 list_splice_init(&moves->list, &list);
3404 list_add_tail(&moves->list, stack);
3405 list_splice_tail(&list, stack);
3406 }
3407 if (!RB_EMPTY_NODE(&moves->node)) {
3408 rb_erase(&moves->node, &sctx->pending_dir_moves);
3409 RB_CLEAR_NODE(&moves->node);
3410 }
3411 }
3412
3413 static int apply_children_dir_moves(struct send_ctx *sctx)
3414 {
3415 struct pending_dir_move *pm;
3416 struct list_head stack;
3417 u64 parent_ino = sctx->cur_ino;
3418 int ret = 0;
3419
3420 pm = get_pending_dir_moves(sctx, parent_ino);
3421 if (!pm)
3422 return 0;
3423
3424 INIT_LIST_HEAD(&stack);
3425 tail_append_pending_moves(sctx, pm, &stack);
3426
3427 while (!list_empty(&stack)) {
3428 pm = list_first_entry(&stack, struct pending_dir_move, list);
3429 parent_ino = pm->ino;
3430 ret = apply_dir_move(sctx, pm);
3431 free_pending_move(sctx, pm);
3432 if (ret)
3433 goto out;
3434 pm = get_pending_dir_moves(sctx, parent_ino);
3435 if (pm)
3436 tail_append_pending_moves(sctx, pm, &stack);
3437 }
3438 return 0;
3439
3440 out:
3441 while (!list_empty(&stack)) {
3442 pm = list_first_entry(&stack, struct pending_dir_move, list);
3443 free_pending_move(sctx, pm);
3444 }
3445 return ret;
3446 }
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484 static int wait_for_dest_dir_move(struct send_ctx *sctx,
3485 struct recorded_ref *parent_ref,
3486 const bool is_orphan)
3487 {
3488 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3489 struct btrfs_path *path;
3490 struct btrfs_key key;
3491 struct btrfs_key di_key;
3492 struct btrfs_dir_item *di;
3493 u64 left_gen;
3494 u64 right_gen;
3495 int ret = 0;
3496 struct waiting_dir_move *wdm;
3497
3498 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3499 return 0;
3500
3501 path = alloc_path_for_send();
3502 if (!path)
3503 return -ENOMEM;
3504
3505 key.objectid = parent_ref->dir;
3506 key.type = BTRFS_DIR_ITEM_KEY;
3507 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3508
3509 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3510 if (ret < 0) {
3511 goto out;
3512 } else if (ret > 0) {
3513 ret = 0;
3514 goto out;
3515 }
3516
3517 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3518 parent_ref->name_len);
3519 if (!di) {
3520 ret = 0;
3521 goto out;
3522 }
3523
3524
3525
3526
3527
3528
3529
3530
3531 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3532 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3533 ret = 0;
3534 goto out;
3535 }
3536
3537 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3538 &left_gen, NULL, NULL, NULL, NULL, NULL);
3539 if (ret < 0)
3540 goto out;
3541 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3542 &right_gen, NULL, NULL, NULL, NULL, NULL);
3543 if (ret < 0) {
3544 if (ret == -ENOENT)
3545 ret = 0;
3546 goto out;
3547 }
3548
3549
3550 if (right_gen != left_gen) {
3551 ret = 0;
3552 goto out;
3553 }
3554
3555 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3556 if (wdm && !wdm->orphanized) {
3557 ret = add_pending_dir_move(sctx,
3558 sctx->cur_ino,
3559 sctx->cur_inode_gen,
3560 di_key.objectid,
3561 &sctx->new_refs,
3562 &sctx->deleted_refs,
3563 is_orphan);
3564 if (!ret)
3565 ret = 1;
3566 }
3567 out:
3568 btrfs_free_path(path);
3569 return ret;
3570 }
3571
3572
3573
3574
3575
3576 static int check_ino_in_path(struct btrfs_root *root,
3577 const u64 ino1,
3578 const u64 ino1_gen,
3579 const u64 ino2,
3580 const u64 ino2_gen,
3581 struct fs_path *fs_path)
3582 {
3583 u64 ino = ino2;
3584
3585 if (ino1 == ino2)
3586 return ino1_gen == ino2_gen;
3587
3588 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3589 u64 parent;
3590 u64 parent_gen;
3591 int ret;
3592
3593 fs_path_reset(fs_path);
3594 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3595 if (ret < 0)
3596 return ret;
3597 if (parent == ino1)
3598 return parent_gen == ino1_gen;
3599 ino = parent;
3600 }
3601 return 0;
3602 }
3603
3604
3605
3606
3607
3608
3609 static int is_ancestor(struct btrfs_root *root,
3610 const u64 ino1,
3611 const u64 ino1_gen,
3612 const u64 ino2,
3613 struct fs_path *fs_path)
3614 {
3615 bool free_fs_path = false;
3616 int ret = 0;
3617 int iter_ret = 0;
3618 struct btrfs_path *path = NULL;
3619 struct btrfs_key key;
3620
3621 if (!fs_path) {
3622 fs_path = fs_path_alloc();
3623 if (!fs_path)
3624 return -ENOMEM;
3625 free_fs_path = true;
3626 }
3627
3628 path = alloc_path_for_send();
3629 if (!path) {
3630 ret = -ENOMEM;
3631 goto out;
3632 }
3633
3634 key.objectid = ino2;
3635 key.type = BTRFS_INODE_REF_KEY;
3636 key.offset = 0;
3637
3638 btrfs_for_each_slot(root, &key, &key, path, iter_ret) {
3639 struct extent_buffer *leaf = path->nodes[0];
3640 int slot = path->slots[0];
3641 u32 cur_offset = 0;
3642 u32 item_size;
3643
3644 if (key.objectid != ino2)
3645 break;
3646 if (key.type != BTRFS_INODE_REF_KEY &&
3647 key.type != BTRFS_INODE_EXTREF_KEY)
3648 break;
3649
3650 item_size = btrfs_item_size(leaf, slot);
3651 while (cur_offset < item_size) {
3652 u64 parent;
3653 u64 parent_gen;
3654
3655 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3656 unsigned long ptr;
3657 struct btrfs_inode_extref *extref;
3658
3659 ptr = btrfs_item_ptr_offset(leaf, slot);
3660 extref = (struct btrfs_inode_extref *)
3661 (ptr + cur_offset);
3662 parent = btrfs_inode_extref_parent(leaf,
3663 extref);
3664 cur_offset += sizeof(*extref);
3665 cur_offset += btrfs_inode_extref_name_len(leaf,
3666 extref);
3667 } else {
3668 parent = key.offset;
3669 cur_offset = item_size;
3670 }
3671
3672 ret = get_inode_info(root, parent, NULL, &parent_gen,
3673 NULL, NULL, NULL, NULL, NULL);
3674 if (ret < 0)
3675 goto out;
3676 ret = check_ino_in_path(root, ino1, ino1_gen,
3677 parent, parent_gen, fs_path);
3678 if (ret)
3679 goto out;
3680 }
3681 }
3682 ret = 0;
3683 if (iter_ret < 0)
3684 ret = iter_ret;
3685
3686 out:
3687 btrfs_free_path(path);
3688 if (free_fs_path)
3689 fs_path_free(fs_path);
3690 return ret;
3691 }
3692
3693 static int wait_for_parent_move(struct send_ctx *sctx,
3694 struct recorded_ref *parent_ref,
3695 const bool is_orphan)
3696 {
3697 int ret = 0;
3698 u64 ino = parent_ref->dir;
3699 u64 ino_gen = parent_ref->dir_gen;
3700 u64 parent_ino_before, parent_ino_after;
3701 struct fs_path *path_before = NULL;
3702 struct fs_path *path_after = NULL;
3703 int len1, len2;
3704
3705 path_after = fs_path_alloc();
3706 path_before = fs_path_alloc();
3707 if (!path_after || !path_before) {
3708 ret = -ENOMEM;
3709 goto out;
3710 }
3711
3712
3713
3714
3715
3716
3717
3718
3719 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3720 u64 parent_ino_after_gen;
3721
3722 if (is_waiting_for_move(sctx, ino)) {
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733 ret = is_ancestor(sctx->parent_root,
3734 sctx->cur_ino, sctx->cur_inode_gen,
3735 ino, path_before);
3736 if (ret)
3737 break;
3738 }
3739
3740 fs_path_reset(path_before);
3741 fs_path_reset(path_after);
3742
3743 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3744 &parent_ino_after_gen, path_after);
3745 if (ret < 0)
3746 goto out;
3747 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3748 NULL, path_before);
3749 if (ret < 0 && ret != -ENOENT) {
3750 goto out;
3751 } else if (ret == -ENOENT) {
3752 ret = 0;
3753 break;
3754 }
3755
3756 len1 = fs_path_len(path_before);
3757 len2 = fs_path_len(path_after);
3758 if (ino > sctx->cur_ino &&
3759 (parent_ino_before != parent_ino_after || len1 != len2 ||
3760 memcmp(path_before->start, path_after->start, len1))) {
3761 u64 parent_ino_gen;
3762
3763 ret = get_inode_info(sctx->parent_root, ino, NULL,
3764 &parent_ino_gen, NULL, NULL, NULL,
3765 NULL, NULL);
3766 if (ret < 0)
3767 goto out;
3768 if (ino_gen == parent_ino_gen) {
3769 ret = 1;
3770 break;
3771 }
3772 }
3773 ino = parent_ino_after;
3774 ino_gen = parent_ino_after_gen;
3775 }
3776
3777 out:
3778 fs_path_free(path_before);
3779 fs_path_free(path_after);
3780
3781 if (ret == 1) {
3782 ret = add_pending_dir_move(sctx,
3783 sctx->cur_ino,
3784 sctx->cur_inode_gen,
3785 ino,
3786 &sctx->new_refs,
3787 &sctx->deleted_refs,
3788 is_orphan);
3789 if (!ret)
3790 ret = 1;
3791 }
3792
3793 return ret;
3794 }
3795
3796 static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3797 {
3798 int ret;
3799 struct fs_path *new_path;
3800
3801
3802
3803
3804
3805 new_path = fs_path_alloc();
3806 if (!new_path)
3807 return -ENOMEM;
3808
3809 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3810 if (ret < 0) {
3811 fs_path_free(new_path);
3812 return ret;
3813 }
3814 ret = fs_path_add(new_path, ref->name, ref->name_len);
3815 if (ret < 0) {
3816 fs_path_free(new_path);
3817 return ret;
3818 }
3819
3820 fs_path_free(ref->full_path);
3821 set_ref_path(ref, new_path);
3822
3823 return 0;
3824 }
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867 static int refresh_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3868 {
3869 char *name;
3870 int ret;
3871
3872 name = kmemdup(ref->name, ref->name_len, GFP_KERNEL);
3873 if (!name)
3874 return -ENOMEM;
3875
3876 fs_path_reset(ref->full_path);
3877 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, ref->full_path);
3878 if (ret < 0)
3879 goto out;
3880
3881 ret = fs_path_add(ref->full_path, name, ref->name_len);
3882 if (ret < 0)
3883 goto out;
3884
3885
3886 set_ref_path(ref, ref->full_path);
3887 out:
3888 kfree(name);
3889 return ret;
3890 }
3891
3892
3893
3894
3895 static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3896 {
3897 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
3898 int ret = 0;
3899 struct recorded_ref *cur;
3900 struct recorded_ref *cur2;
3901 struct list_head check_dirs;
3902 struct fs_path *valid_path = NULL;
3903 u64 ow_inode = 0;
3904 u64 ow_gen;
3905 u64 ow_mode;
3906 int did_overwrite = 0;
3907 int is_orphan = 0;
3908 u64 last_dir_ino_rm = 0;
3909 bool can_rename = true;
3910 bool orphanized_dir = false;
3911 bool orphanized_ancestor = false;
3912
3913 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
3914
3915
3916
3917
3918
3919 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3920 INIT_LIST_HEAD(&check_dirs);
3921
3922 valid_path = fs_path_alloc();
3923 if (!valid_path) {
3924 ret = -ENOMEM;
3925 goto out;
3926 }
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939 if (!sctx->cur_inode_new) {
3940 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3941 sctx->cur_inode_gen);
3942 if (ret < 0)
3943 goto out;
3944 if (ret)
3945 did_overwrite = 1;
3946 }
3947 if (sctx->cur_inode_new || did_overwrite) {
3948 ret = gen_unique_name(sctx, sctx->cur_ino,
3949 sctx->cur_inode_gen, valid_path);
3950 if (ret < 0)
3951 goto out;
3952 is_orphan = 1;
3953 } else {
3954 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3955 valid_path);
3956 if (ret < 0)
3957 goto out;
3958 }
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998 list_for_each_entry(cur, &sctx->new_refs, list) {
3999 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4000 if (ret < 0)
4001 goto out;
4002 if (ret == inode_state_will_create)
4003 continue;
4004
4005
4006
4007
4008
4009
4010
4011 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4012 cur->name, cur->name_len,
4013 &ow_inode, &ow_gen, &ow_mode);
4014 if (ret < 0)
4015 goto out;
4016 if (ret) {
4017 ret = is_first_ref(sctx->parent_root,
4018 ow_inode, cur->dir, cur->name,
4019 cur->name_len);
4020 if (ret < 0)
4021 goto out;
4022 if (ret) {
4023 struct name_cache_entry *nce;
4024 struct waiting_dir_move *wdm;
4025
4026 if (orphanized_dir) {
4027 ret = refresh_ref_path(sctx, cur);
4028 if (ret < 0)
4029 goto out;
4030 }
4031
4032 ret = orphanize_inode(sctx, ow_inode, ow_gen,
4033 cur->full_path);
4034 if (ret < 0)
4035 goto out;
4036 if (S_ISDIR(ow_mode))
4037 orphanized_dir = true;
4038
4039
4040
4041
4042
4043
4044
4045 if (is_waiting_for_move(sctx, ow_inode)) {
4046 wdm = get_waiting_dir_move(sctx,
4047 ow_inode);
4048 ASSERT(wdm);
4049 wdm->orphanized = true;
4050 }
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062 nce = name_cache_search(sctx, ow_inode, ow_gen);
4063 if (nce) {
4064 name_cache_delete(sctx, nce);
4065 kfree(nce);
4066 }
4067
4068
4069
4070
4071
4072
4073
4074
4075 ret = is_ancestor(sctx->parent_root,
4076 ow_inode, ow_gen,
4077 sctx->cur_ino, NULL);
4078 if (ret > 0) {
4079 orphanized_ancestor = true;
4080 fs_path_reset(valid_path);
4081 ret = get_cur_path(sctx, sctx->cur_ino,
4082 sctx->cur_inode_gen,
4083 valid_path);
4084 }
4085 if (ret < 0)
4086 goto out;
4087 } else {
4088
4089
4090
4091
4092
4093
4094 if (orphanized_dir) {
4095 ret = refresh_ref_path(sctx, cur);
4096 if (ret < 0)
4097 goto out;
4098 }
4099 ret = send_unlink(sctx, cur->full_path);
4100 if (ret < 0)
4101 goto out;
4102 }
4103 }
4104
4105 }
4106
4107 list_for_each_entry(cur, &sctx->new_refs, list) {
4108
4109
4110
4111
4112
4113
4114
4115 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4116 if (ret < 0)
4117 goto out;
4118 if (ret == inode_state_will_create) {
4119 ret = 0;
4120
4121
4122
4123
4124 list_for_each_entry(cur2, &sctx->new_refs, list) {
4125 if (cur == cur2)
4126 break;
4127 if (cur2->dir == cur->dir) {
4128 ret = 1;
4129 break;
4130 }
4131 }
4132
4133
4134
4135
4136
4137 if (!ret)
4138 ret = did_create_dir(sctx, cur->dir);
4139 if (ret < 0)
4140 goto out;
4141 if (!ret) {
4142 ret = send_create_inode(sctx, cur->dir);
4143 if (ret < 0)
4144 goto out;
4145 }
4146 }
4147
4148 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
4149 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
4150 if (ret < 0)
4151 goto out;
4152 if (ret == 1) {
4153 can_rename = false;
4154 *pending_move = 1;
4155 }
4156 }
4157
4158 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
4159 can_rename) {
4160 ret = wait_for_parent_move(sctx, cur, is_orphan);
4161 if (ret < 0)
4162 goto out;
4163 if (ret == 1) {
4164 can_rename = false;
4165 *pending_move = 1;
4166 }
4167 }
4168
4169
4170
4171
4172
4173
4174 if (is_orphan && can_rename) {
4175 ret = send_rename(sctx, valid_path, cur->full_path);
4176 if (ret < 0)
4177 goto out;
4178 is_orphan = 0;
4179 ret = fs_path_copy(valid_path, cur->full_path);
4180 if (ret < 0)
4181 goto out;
4182 } else if (can_rename) {
4183 if (S_ISDIR(sctx->cur_inode_mode)) {
4184
4185
4186
4187
4188
4189 ret = send_rename(sctx, valid_path,
4190 cur->full_path);
4191 if (!ret)
4192 ret = fs_path_copy(valid_path,
4193 cur->full_path);
4194 if (ret < 0)
4195 goto out;
4196 } else {
4197
4198
4199
4200
4201
4202
4203
4204 if (orphanized_dir) {
4205 ret = update_ref_path(sctx, cur);
4206 if (ret < 0)
4207 goto out;
4208 }
4209 ret = send_link(sctx, cur->full_path,
4210 valid_path);
4211 if (ret < 0)
4212 goto out;
4213 }
4214 }
4215 ret = dup_ref(cur, &check_dirs);
4216 if (ret < 0)
4217 goto out;
4218 }
4219
4220 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4221
4222
4223
4224
4225
4226
4227 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4228 sctx->cur_ino);
4229 if (ret < 0)
4230 goto out;
4231 if (ret) {
4232 ret = send_rmdir(sctx, valid_path);
4233 if (ret < 0)
4234 goto out;
4235 } else if (!is_orphan) {
4236 ret = orphanize_inode(sctx, sctx->cur_ino,
4237 sctx->cur_inode_gen, valid_path);
4238 if (ret < 0)
4239 goto out;
4240 is_orphan = 1;
4241 }
4242
4243 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4244 ret = dup_ref(cur, &check_dirs);
4245 if (ret < 0)
4246 goto out;
4247 }
4248 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4249 !list_empty(&sctx->deleted_refs)) {
4250
4251
4252
4253 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4254 list);
4255 ret = dup_ref(cur, &check_dirs);
4256 if (ret < 0)
4257 goto out;
4258 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4259
4260
4261
4262
4263
4264 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4265 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4266 sctx->cur_ino, sctx->cur_inode_gen,
4267 cur->name, cur->name_len);
4268 if (ret < 0)
4269 goto out;
4270 if (!ret) {
4271
4272
4273
4274
4275
4276
4277
4278 if (orphanized_ancestor) {
4279 ret = update_ref_path(sctx, cur);
4280 if (ret < 0)
4281 goto out;
4282 }
4283 ret = send_unlink(sctx, cur->full_path);
4284 if (ret < 0)
4285 goto out;
4286 }
4287 ret = dup_ref(cur, &check_dirs);
4288 if (ret < 0)
4289 goto out;
4290 }
4291
4292
4293
4294
4295
4296
4297
4298
4299 if (is_orphan) {
4300 ret = send_unlink(sctx, valid_path);
4301 if (ret < 0)
4302 goto out;
4303 }
4304 }
4305
4306
4307
4308
4309
4310
4311
4312 list_for_each_entry(cur, &check_dirs, list) {
4313
4314
4315
4316
4317
4318 if (cur->dir > sctx->cur_ino)
4319 continue;
4320
4321 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4322 if (ret < 0)
4323 goto out;
4324
4325 if (ret == inode_state_did_create ||
4326 ret == inode_state_no_change) {
4327
4328 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
4329 if (ret < 0)
4330 goto out;
4331 } else if (ret == inode_state_did_delete &&
4332 cur->dir != last_dir_ino_rm) {
4333 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4334 sctx->cur_ino);
4335 if (ret < 0)
4336 goto out;
4337 if (ret) {
4338 ret = get_cur_path(sctx, cur->dir,
4339 cur->dir_gen, valid_path);
4340 if (ret < 0)
4341 goto out;
4342 ret = send_rmdir(sctx, valid_path);
4343 if (ret < 0)
4344 goto out;
4345 last_dir_ino_rm = cur->dir;
4346 }
4347 }
4348 }
4349
4350 ret = 0;
4351
4352 out:
4353 __free_recorded_refs(&check_dirs);
4354 free_recorded_refs(sctx);
4355 fs_path_free(valid_path);
4356 return ret;
4357 }
4358
4359 static int rbtree_ref_comp(const void *k, const struct rb_node *node)
4360 {
4361 const struct recorded_ref *data = k;
4362 const struct recorded_ref *ref = rb_entry(node, struct recorded_ref, node);
4363 int result;
4364
4365 if (data->dir > ref->dir)
4366 return 1;
4367 if (data->dir < ref->dir)
4368 return -1;
4369 if (data->dir_gen > ref->dir_gen)
4370 return 1;
4371 if (data->dir_gen < ref->dir_gen)
4372 return -1;
4373 if (data->name_len > ref->name_len)
4374 return 1;
4375 if (data->name_len < ref->name_len)
4376 return -1;
4377 result = strcmp(data->name, ref->name);
4378 if (result > 0)
4379 return 1;
4380 if (result < 0)
4381 return -1;
4382 return 0;
4383 }
4384
4385 static bool rbtree_ref_less(struct rb_node *node, const struct rb_node *parent)
4386 {
4387 const struct recorded_ref *entry = rb_entry(node, struct recorded_ref, node);
4388
4389 return rbtree_ref_comp(entry, parent) < 0;
4390 }
4391
4392 static int record_ref_in_tree(struct rb_root *root, struct list_head *refs,
4393 struct fs_path *name, u64 dir, u64 dir_gen,
4394 struct send_ctx *sctx)
4395 {
4396 int ret = 0;
4397 struct fs_path *path = NULL;
4398 struct recorded_ref *ref = NULL;
4399
4400 path = fs_path_alloc();
4401 if (!path) {
4402 ret = -ENOMEM;
4403 goto out;
4404 }
4405
4406 ref = recorded_ref_alloc();
4407 if (!ref) {
4408 ret = -ENOMEM;
4409 goto out;
4410 }
4411
4412 ret = get_cur_path(sctx, dir, dir_gen, path);
4413 if (ret < 0)
4414 goto out;
4415 ret = fs_path_add_path(path, name);
4416 if (ret < 0)
4417 goto out;
4418
4419 ref->dir = dir;
4420 ref->dir_gen = dir_gen;
4421 set_ref_path(ref, path);
4422 list_add_tail(&ref->list, refs);
4423 rb_add(&ref->node, root, rbtree_ref_less);
4424 ref->root = root;
4425 out:
4426 if (ret) {
4427 if (path && (!ref || !ref->full_path))
4428 fs_path_free(path);
4429 recorded_ref_free(ref);
4430 }
4431 return ret;
4432 }
4433
4434 static int record_new_ref_if_needed(int num, u64 dir, int index,
4435 struct fs_path *name, void *ctx)
4436 {
4437 int ret = 0;
4438 struct send_ctx *sctx = ctx;
4439 struct rb_node *node = NULL;
4440 struct recorded_ref data;
4441 struct recorded_ref *ref;
4442 u64 dir_gen;
4443
4444 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4445 NULL, NULL, NULL, NULL);
4446 if (ret < 0)
4447 goto out;
4448
4449 data.dir = dir;
4450 data.dir_gen = dir_gen;
4451 set_ref_path(&data, name);
4452 node = rb_find(&data, &sctx->rbtree_deleted_refs, rbtree_ref_comp);
4453 if (node) {
4454 ref = rb_entry(node, struct recorded_ref, node);
4455 recorded_ref_free(ref);
4456 } else {
4457 ret = record_ref_in_tree(&sctx->rbtree_new_refs,
4458 &sctx->new_refs, name, dir, dir_gen,
4459 sctx);
4460 }
4461 out:
4462 return ret;
4463 }
4464
4465 static int record_deleted_ref_if_needed(int num, u64 dir, int index,
4466 struct fs_path *name, void *ctx)
4467 {
4468 int ret = 0;
4469 struct send_ctx *sctx = ctx;
4470 struct rb_node *node = NULL;
4471 struct recorded_ref data;
4472 struct recorded_ref *ref;
4473 u64 dir_gen;
4474
4475 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4476 NULL, NULL, NULL, NULL);
4477 if (ret < 0)
4478 goto out;
4479
4480 data.dir = dir;
4481 data.dir_gen = dir_gen;
4482 set_ref_path(&data, name);
4483 node = rb_find(&data, &sctx->rbtree_new_refs, rbtree_ref_comp);
4484 if (node) {
4485 ref = rb_entry(node, struct recorded_ref, node);
4486 recorded_ref_free(ref);
4487 } else {
4488 ret = record_ref_in_tree(&sctx->rbtree_deleted_refs,
4489 &sctx->deleted_refs, name, dir,
4490 dir_gen, sctx);
4491 }
4492 out:
4493 return ret;
4494 }
4495
4496 static int record_new_ref(struct send_ctx *sctx)
4497 {
4498 int ret;
4499
4500 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4501 sctx->cmp_key, 0, record_new_ref_if_needed, sctx);
4502 if (ret < 0)
4503 goto out;
4504 ret = 0;
4505
4506 out:
4507 return ret;
4508 }
4509
4510 static int record_deleted_ref(struct send_ctx *sctx)
4511 {
4512 int ret;
4513
4514 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4515 sctx->cmp_key, 0, record_deleted_ref_if_needed,
4516 sctx);
4517 if (ret < 0)
4518 goto out;
4519 ret = 0;
4520
4521 out:
4522 return ret;
4523 }
4524
4525 static int record_changed_ref(struct send_ctx *sctx)
4526 {
4527 int ret = 0;
4528
4529 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4530 sctx->cmp_key, 0, record_new_ref_if_needed, sctx);
4531 if (ret < 0)
4532 goto out;
4533 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4534 sctx->cmp_key, 0, record_deleted_ref_if_needed, sctx);
4535 if (ret < 0)
4536 goto out;
4537 ret = 0;
4538
4539 out:
4540 return ret;
4541 }
4542
4543
4544
4545
4546
4547 static int process_all_refs(struct send_ctx *sctx,
4548 enum btrfs_compare_tree_result cmd)
4549 {
4550 int ret = 0;
4551 int iter_ret = 0;
4552 struct btrfs_root *root;
4553 struct btrfs_path *path;
4554 struct btrfs_key key;
4555 struct btrfs_key found_key;
4556 iterate_inode_ref_t cb;
4557 int pending_move = 0;
4558
4559 path = alloc_path_for_send();
4560 if (!path)
4561 return -ENOMEM;
4562
4563 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4564 root = sctx->send_root;
4565 cb = record_new_ref_if_needed;
4566 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4567 root = sctx->parent_root;
4568 cb = record_deleted_ref_if_needed;
4569 } else {
4570 btrfs_err(sctx->send_root->fs_info,
4571 "Wrong command %d in process_all_refs", cmd);
4572 ret = -EINVAL;
4573 goto out;
4574 }
4575
4576 key.objectid = sctx->cmp_key->objectid;
4577 key.type = BTRFS_INODE_REF_KEY;
4578 key.offset = 0;
4579 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
4580 if (found_key.objectid != key.objectid ||
4581 (found_key.type != BTRFS_INODE_REF_KEY &&
4582 found_key.type != BTRFS_INODE_EXTREF_KEY))
4583 break;
4584
4585 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4586 if (ret < 0)
4587 goto out;
4588 }
4589
4590 if (iter_ret < 0) {
4591 ret = iter_ret;
4592 goto out;
4593 }
4594 btrfs_release_path(path);
4595
4596
4597
4598
4599
4600
4601 ret = process_recorded_refs(sctx, &pending_move);
4602 out:
4603 btrfs_free_path(path);
4604 return ret;
4605 }
4606
4607 static int send_set_xattr(struct send_ctx *sctx,
4608 struct fs_path *path,
4609 const char *name, int name_len,
4610 const char *data, int data_len)
4611 {
4612 int ret = 0;
4613
4614 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4615 if (ret < 0)
4616 goto out;
4617
4618 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4619 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4620 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4621
4622 ret = send_cmd(sctx);
4623
4624 tlv_put_failure:
4625 out:
4626 return ret;
4627 }
4628
4629 static int send_remove_xattr(struct send_ctx *sctx,
4630 struct fs_path *path,
4631 const char *name, int name_len)
4632 {
4633 int ret = 0;
4634
4635 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4636 if (ret < 0)
4637 goto out;
4638
4639 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4640 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4641
4642 ret = send_cmd(sctx);
4643
4644 tlv_put_failure:
4645 out:
4646 return ret;
4647 }
4648
4649 static int __process_new_xattr(int num, struct btrfs_key *di_key,
4650 const char *name, int name_len, const char *data,
4651 int data_len, void *ctx)
4652 {
4653 int ret;
4654 struct send_ctx *sctx = ctx;
4655 struct fs_path *p;
4656 struct posix_acl_xattr_header dummy_acl;
4657
4658
4659 if (!strncmp(name, XATTR_NAME_CAPS, name_len))
4660 return 0;
4661
4662 p = fs_path_alloc();
4663 if (!p)
4664 return -ENOMEM;
4665
4666
4667
4668
4669
4670
4671
4672 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4673 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4674 if (data_len == 0) {
4675 dummy_acl.a_version =
4676 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4677 data = (char *)&dummy_acl;
4678 data_len = sizeof(dummy_acl);
4679 }
4680 }
4681
4682 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4683 if (ret < 0)
4684 goto out;
4685
4686 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4687
4688 out:
4689 fs_path_free(p);
4690 return ret;
4691 }
4692
4693 static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4694 const char *name, int name_len,
4695 const char *data, int data_len, void *ctx)
4696 {
4697 int ret;
4698 struct send_ctx *sctx = ctx;
4699 struct fs_path *p;
4700
4701 p = fs_path_alloc();
4702 if (!p)
4703 return -ENOMEM;
4704
4705 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4706 if (ret < 0)
4707 goto out;
4708
4709 ret = send_remove_xattr(sctx, p, name, name_len);
4710
4711 out:
4712 fs_path_free(p);
4713 return ret;
4714 }
4715
4716 static int process_new_xattr(struct send_ctx *sctx)
4717 {
4718 int ret = 0;
4719
4720 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4721 __process_new_xattr, sctx);
4722
4723 return ret;
4724 }
4725
4726 static int process_deleted_xattr(struct send_ctx *sctx)
4727 {
4728 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4729 __process_deleted_xattr, sctx);
4730 }
4731
4732 struct find_xattr_ctx {
4733 const char *name;
4734 int name_len;
4735 int found_idx;
4736 char *found_data;
4737 int found_data_len;
4738 };
4739
4740 static int __find_xattr(int num, struct btrfs_key *di_key, const char *name,
4741 int name_len, const char *data, int data_len, void *vctx)
4742 {
4743 struct find_xattr_ctx *ctx = vctx;
4744
4745 if (name_len == ctx->name_len &&
4746 strncmp(name, ctx->name, name_len) == 0) {
4747 ctx->found_idx = num;
4748 ctx->found_data_len = data_len;
4749 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4750 if (!ctx->found_data)
4751 return -ENOMEM;
4752 return 1;
4753 }
4754 return 0;
4755 }
4756
4757 static int find_xattr(struct btrfs_root *root,
4758 struct btrfs_path *path,
4759 struct btrfs_key *key,
4760 const char *name, int name_len,
4761 char **data, int *data_len)
4762 {
4763 int ret;
4764 struct find_xattr_ctx ctx;
4765
4766 ctx.name = name;
4767 ctx.name_len = name_len;
4768 ctx.found_idx = -1;
4769 ctx.found_data = NULL;
4770 ctx.found_data_len = 0;
4771
4772 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
4773 if (ret < 0)
4774 return ret;
4775
4776 if (ctx.found_idx == -1)
4777 return -ENOENT;
4778 if (data) {
4779 *data = ctx.found_data;
4780 *data_len = ctx.found_data_len;
4781 } else {
4782 kfree(ctx.found_data);
4783 }
4784 return ctx.found_idx;
4785 }
4786
4787
4788 static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4789 const char *name, int name_len,
4790 const char *data, int data_len,
4791 void *ctx)
4792 {
4793 int ret;
4794 struct send_ctx *sctx = ctx;
4795 char *found_data = NULL;
4796 int found_data_len = 0;
4797
4798 ret = find_xattr(sctx->parent_root, sctx->right_path,
4799 sctx->cmp_key, name, name_len, &found_data,
4800 &found_data_len);
4801 if (ret == -ENOENT) {
4802 ret = __process_new_xattr(num, di_key, name, name_len, data,
4803 data_len, ctx);
4804 } else if (ret >= 0) {
4805 if (data_len != found_data_len ||
4806 memcmp(data, found_data, data_len)) {
4807 ret = __process_new_xattr(num, di_key, name, name_len,
4808 data, data_len, ctx);
4809 } else {
4810 ret = 0;
4811 }
4812 }
4813
4814 kfree(found_data);
4815 return ret;
4816 }
4817
4818 static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4819 const char *name, int name_len,
4820 const char *data, int data_len,
4821 void *ctx)
4822 {
4823 int ret;
4824 struct send_ctx *sctx = ctx;
4825
4826 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4827 name, name_len, NULL, NULL);
4828 if (ret == -ENOENT)
4829 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4830 data_len, ctx);
4831 else if (ret >= 0)
4832 ret = 0;
4833
4834 return ret;
4835 }
4836
4837 static int process_changed_xattr(struct send_ctx *sctx)
4838 {
4839 int ret = 0;
4840
4841 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4842 __process_changed_new_xattr, sctx);
4843 if (ret < 0)
4844 goto out;
4845 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4846 __process_changed_deleted_xattr, sctx);
4847
4848 out:
4849 return ret;
4850 }
4851
4852 static int process_all_new_xattrs(struct send_ctx *sctx)
4853 {
4854 int ret = 0;
4855 int iter_ret = 0;
4856 struct btrfs_root *root;
4857 struct btrfs_path *path;
4858 struct btrfs_key key;
4859 struct btrfs_key found_key;
4860
4861 path = alloc_path_for_send();
4862 if (!path)
4863 return -ENOMEM;
4864
4865 root = sctx->send_root;
4866
4867 key.objectid = sctx->cmp_key->objectid;
4868 key.type = BTRFS_XATTR_ITEM_KEY;
4869 key.offset = 0;
4870 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
4871 if (found_key.objectid != key.objectid ||
4872 found_key.type != key.type) {
4873 ret = 0;
4874 break;
4875 }
4876
4877 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
4878 if (ret < 0)
4879 break;
4880 }
4881
4882 if (iter_ret < 0)
4883 ret = iter_ret;
4884
4885 btrfs_free_path(path);
4886 return ret;
4887 }
4888
4889 static inline u64 max_send_read_size(const struct send_ctx *sctx)
4890 {
4891 return sctx->send_max_size - SZ_16K;
4892 }
4893
4894 static int put_data_header(struct send_ctx *sctx, u32 len)
4895 {
4896 if (WARN_ON_ONCE(sctx->put_data))
4897 return -EINVAL;
4898 sctx->put_data = true;
4899 if (sctx->proto >= 2) {
4900
4901
4902
4903
4904 if (sctx->send_max_size - sctx->send_size < sizeof(__le16) + len)
4905 return -EOVERFLOW;
4906 put_unaligned_le16(BTRFS_SEND_A_DATA, sctx->send_buf + sctx->send_size);
4907 sctx->send_size += sizeof(__le16);
4908 } else {
4909 struct btrfs_tlv_header *hdr;
4910
4911 if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
4912 return -EOVERFLOW;
4913 hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
4914 put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
4915 put_unaligned_le16(len, &hdr->tlv_len);
4916 sctx->send_size += sizeof(*hdr);
4917 }
4918 return 0;
4919 }
4920
4921 static int put_file_data(struct send_ctx *sctx, u64 offset, u32 len)
4922 {
4923 struct btrfs_root *root = sctx->send_root;
4924 struct btrfs_fs_info *fs_info = root->fs_info;
4925 struct page *page;
4926 pgoff_t index = offset >> PAGE_SHIFT;
4927 pgoff_t last_index;
4928 unsigned pg_offset = offset_in_page(offset);
4929 int ret;
4930
4931 ret = put_data_header(sctx, len);
4932 if (ret)
4933 return ret;
4934
4935 last_index = (offset + len - 1) >> PAGE_SHIFT;
4936
4937 while (index <= last_index) {
4938 unsigned cur_len = min_t(unsigned, len,
4939 PAGE_SIZE - pg_offset);
4940
4941 page = find_lock_page(sctx->cur_inode->i_mapping, index);
4942 if (!page) {
4943 page_cache_sync_readahead(sctx->cur_inode->i_mapping,
4944 &sctx->ra, NULL, index,
4945 last_index + 1 - index);
4946
4947 page = find_or_create_page(sctx->cur_inode->i_mapping,
4948 index, GFP_KERNEL);
4949 if (!page) {
4950 ret = -ENOMEM;
4951 break;
4952 }
4953 }
4954
4955 if (PageReadahead(page))
4956 page_cache_async_readahead(sctx->cur_inode->i_mapping,
4957 &sctx->ra, NULL, page_folio(page),
4958 index, last_index + 1 - index);
4959
4960 if (!PageUptodate(page)) {
4961 btrfs_read_folio(NULL, page_folio(page));
4962 lock_page(page);
4963 if (!PageUptodate(page)) {
4964 unlock_page(page);
4965 btrfs_err(fs_info,
4966 "send: IO error at offset %llu for inode %llu root %llu",
4967 page_offset(page), sctx->cur_ino,
4968 sctx->send_root->root_key.objectid);
4969 put_page(page);
4970 ret = -EIO;
4971 break;
4972 }
4973 }
4974
4975 memcpy_from_page(sctx->send_buf + sctx->send_size, page,
4976 pg_offset, cur_len);
4977 unlock_page(page);
4978 put_page(page);
4979 index++;
4980 pg_offset = 0;
4981 len -= cur_len;
4982 sctx->send_size += cur_len;
4983 }
4984
4985 return ret;
4986 }
4987
4988
4989
4990
4991
4992 static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4993 {
4994 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
4995 int ret = 0;
4996 struct fs_path *p;
4997
4998 p = fs_path_alloc();
4999 if (!p)
5000 return -ENOMEM;
5001
5002 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
5003
5004 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5005 if (ret < 0)
5006 goto out;
5007
5008 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5009 if (ret < 0)
5010 goto out;
5011
5012 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5013 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5014 ret = put_file_data(sctx, offset, len);
5015 if (ret < 0)
5016 goto out;
5017
5018 ret = send_cmd(sctx);
5019
5020 tlv_put_failure:
5021 out:
5022 fs_path_free(p);
5023 return ret;
5024 }
5025
5026
5027
5028
5029 static int send_clone(struct send_ctx *sctx,
5030 u64 offset, u32 len,
5031 struct clone_root *clone_root)
5032 {
5033 int ret = 0;
5034 struct fs_path *p;
5035 u64 gen;
5036
5037 btrfs_debug(sctx->send_root->fs_info,
5038 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
5039 offset, len, clone_root->root->root_key.objectid,
5040 clone_root->ino, clone_root->offset);
5041
5042 p = fs_path_alloc();
5043 if (!p)
5044 return -ENOMEM;
5045
5046 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
5047 if (ret < 0)
5048 goto out;
5049
5050 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5051 if (ret < 0)
5052 goto out;
5053
5054 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5055 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
5056 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5057
5058 if (clone_root->root == sctx->send_root) {
5059 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
5060 &gen, NULL, NULL, NULL, NULL, NULL);
5061 if (ret < 0)
5062 goto out;
5063 ret = get_cur_path(sctx, clone_root->ino, gen, p);
5064 } else {
5065 ret = get_inode_path(clone_root->root, clone_root->ino, p);
5066 }
5067 if (ret < 0)
5068 goto out;
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
5080 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5081 clone_root->root->root_item.received_uuid);
5082 else
5083 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
5084 clone_root->root->root_item.uuid);
5085 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
5086 btrfs_root_ctransid(&clone_root->root->root_item));
5087 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
5088 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
5089 clone_root->offset);
5090
5091 ret = send_cmd(sctx);
5092
5093 tlv_put_failure:
5094 out:
5095 fs_path_free(p);
5096 return ret;
5097 }
5098
5099
5100
5101
5102 static int send_update_extent(struct send_ctx *sctx,
5103 u64 offset, u32 len)
5104 {
5105 int ret = 0;
5106 struct fs_path *p;
5107
5108 p = fs_path_alloc();
5109 if (!p)
5110 return -ENOMEM;
5111
5112 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
5113 if (ret < 0)
5114 goto out;
5115
5116 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5117 if (ret < 0)
5118 goto out;
5119
5120 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5121 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5122 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
5123
5124 ret = send_cmd(sctx);
5125
5126 tlv_put_failure:
5127 out:
5128 fs_path_free(p);
5129 return ret;
5130 }
5131
5132 static int send_hole(struct send_ctx *sctx, u64 end)
5133 {
5134 struct fs_path *p = NULL;
5135 u64 read_size = max_send_read_size(sctx);
5136 u64 offset = sctx->cur_inode_last_extent;
5137 int ret = 0;
5138
5139
5140
5141
5142
5143
5144
5145 if (offset >= sctx->cur_inode_size)
5146 return 0;
5147
5148
5149
5150
5151
5152 end = min_t(u64, end, sctx->cur_inode_size);
5153
5154 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5155 return send_update_extent(sctx, offset, end - offset);
5156
5157 p = fs_path_alloc();
5158 if (!p)
5159 return -ENOMEM;
5160 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5161 if (ret < 0)
5162 goto tlv_put_failure;
5163 while (offset < end) {
5164 u64 len = min(end - offset, read_size);
5165
5166 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5167 if (ret < 0)
5168 break;
5169 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5170 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5171 ret = put_data_header(sctx, len);
5172 if (ret < 0)
5173 break;
5174 memset(sctx->send_buf + sctx->send_size, 0, len);
5175 sctx->send_size += len;
5176 ret = send_cmd(sctx);
5177 if (ret < 0)
5178 break;
5179 offset += len;
5180 }
5181 sctx->cur_inode_next_write_offset = offset;
5182 tlv_put_failure:
5183 fs_path_free(p);
5184 return ret;
5185 }
5186
5187 static int send_encoded_inline_extent(struct send_ctx *sctx,
5188 struct btrfs_path *path, u64 offset,
5189 u64 len)
5190 {
5191 struct btrfs_root *root = sctx->send_root;
5192 struct btrfs_fs_info *fs_info = root->fs_info;
5193 struct inode *inode;
5194 struct fs_path *fspath;
5195 struct extent_buffer *leaf = path->nodes[0];
5196 struct btrfs_key key;
5197 struct btrfs_file_extent_item *ei;
5198 u64 ram_bytes;
5199 size_t inline_size;
5200 int ret;
5201
5202 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
5203 if (IS_ERR(inode))
5204 return PTR_ERR(inode);
5205
5206 fspath = fs_path_alloc();
5207 if (!fspath) {
5208 ret = -ENOMEM;
5209 goto out;
5210 }
5211
5212 ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE);
5213 if (ret < 0)
5214 goto out;
5215
5216 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5217 if (ret < 0)
5218 goto out;
5219
5220 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5221 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
5222 ram_bytes = btrfs_file_extent_ram_bytes(leaf, ei);
5223 inline_size = btrfs_file_extent_inline_item_len(leaf, path->slots[0]);
5224
5225 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath);
5226 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5227 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
5228 min(key.offset + ram_bytes - offset, len));
5229 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN, ram_bytes);
5230 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET, offset - key.offset);
5231 ret = btrfs_encoded_io_compression_from_extent(fs_info,
5232 btrfs_file_extent_compression(leaf, ei));
5233 if (ret < 0)
5234 goto out;
5235 TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret);
5236
5237 ret = put_data_header(sctx, inline_size);
5238 if (ret < 0)
5239 goto out;
5240 read_extent_buffer(leaf, sctx->send_buf + sctx->send_size,
5241 btrfs_file_extent_inline_start(ei), inline_size);
5242 sctx->send_size += inline_size;
5243
5244 ret = send_cmd(sctx);
5245
5246 tlv_put_failure:
5247 out:
5248 fs_path_free(fspath);
5249 iput(inode);
5250 return ret;
5251 }
5252
5253 static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
5254 u64 offset, u64 len)
5255 {
5256 struct btrfs_root *root = sctx->send_root;
5257 struct btrfs_fs_info *fs_info = root->fs_info;
5258 struct inode *inode;
5259 struct fs_path *fspath;
5260 struct extent_buffer *leaf = path->nodes[0];
5261 struct btrfs_key key;
5262 struct btrfs_file_extent_item *ei;
5263 u64 disk_bytenr, disk_num_bytes;
5264 u32 data_offset;
5265 struct btrfs_cmd_header *hdr;
5266 u32 crc;
5267 int ret;
5268
5269 inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
5270 if (IS_ERR(inode))
5271 return PTR_ERR(inode);
5272
5273 fspath = fs_path_alloc();
5274 if (!fspath) {
5275 ret = -ENOMEM;
5276 goto out;
5277 }
5278
5279 ret = begin_cmd(sctx, BTRFS_SEND_C_ENCODED_WRITE);
5280 if (ret < 0)
5281 goto out;
5282
5283 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5284 if (ret < 0)
5285 goto out;
5286
5287 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5288 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
5289 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
5290 disk_num_bytes = btrfs_file_extent_disk_num_bytes(leaf, ei);
5291
5292 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, fspath);
5293 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5294 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_FILE_LEN,
5295 min(key.offset + btrfs_file_extent_num_bytes(leaf, ei) - offset,
5296 len));
5297 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_LEN,
5298 btrfs_file_extent_ram_bytes(leaf, ei));
5299 TLV_PUT_U64(sctx, BTRFS_SEND_A_UNENCODED_OFFSET,
5300 offset - key.offset + btrfs_file_extent_offset(leaf, ei));
5301 ret = btrfs_encoded_io_compression_from_extent(fs_info,
5302 btrfs_file_extent_compression(leaf, ei));
5303 if (ret < 0)
5304 goto out;
5305 TLV_PUT_U32(sctx, BTRFS_SEND_A_COMPRESSION, ret);
5306 TLV_PUT_U32(sctx, BTRFS_SEND_A_ENCRYPTION, 0);
5307
5308 ret = put_data_header(sctx, disk_num_bytes);
5309 if (ret < 0)
5310 goto out;
5311
5312
5313
5314
5315
5316
5317 data_offset = ALIGN(sctx->send_size, PAGE_SIZE);
5318 if (data_offset > sctx->send_max_size ||
5319 sctx->send_max_size - data_offset < disk_num_bytes) {
5320 ret = -EOVERFLOW;
5321 goto out;
5322 }
5323
5324
5325
5326
5327
5328 ret = btrfs_encoded_read_regular_fill_pages(BTRFS_I(inode), offset,
5329 disk_bytenr, disk_num_bytes,
5330 sctx->send_buf_pages +
5331 (data_offset >> PAGE_SHIFT));
5332 if (ret)
5333 goto out;
5334
5335 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
5336 hdr->len = cpu_to_le32(sctx->send_size + disk_num_bytes - sizeof(*hdr));
5337 hdr->crc = 0;
5338 crc = btrfs_crc32c(0, sctx->send_buf, sctx->send_size);
5339 crc = btrfs_crc32c(crc, sctx->send_buf + data_offset, disk_num_bytes);
5340 hdr->crc = cpu_to_le32(crc);
5341
5342 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
5343 &sctx->send_off);
5344 if (!ret) {
5345 ret = write_buf(sctx->send_filp, sctx->send_buf + data_offset,
5346 disk_num_bytes, &sctx->send_off);
5347 }
5348 sctx->send_size = 0;
5349 sctx->put_data = false;
5350
5351 tlv_put_failure:
5352 out:
5353 fs_path_free(fspath);
5354 iput(inode);
5355 return ret;
5356 }
5357
5358 static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path,
5359 const u64 offset, const u64 len)
5360 {
5361 const u64 end = offset + len;
5362 struct extent_buffer *leaf = path->nodes[0];
5363 struct btrfs_file_extent_item *ei;
5364 u64 read_size = max_send_read_size(sctx);
5365 u64 sent = 0;
5366
5367 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5368 return send_update_extent(sctx, offset, len);
5369
5370 ei = btrfs_item_ptr(leaf, path->slots[0],
5371 struct btrfs_file_extent_item);
5372 if ((sctx->flags & BTRFS_SEND_FLAG_COMPRESSED) &&
5373 btrfs_file_extent_compression(leaf, ei) != BTRFS_COMPRESS_NONE) {
5374 bool is_inline = (btrfs_file_extent_type(leaf, ei) ==
5375 BTRFS_FILE_EXTENT_INLINE);
5376
5377
5378
5379
5380
5381
5382
5383
5384 if (is_inline &&
5385 btrfs_file_extent_inline_item_len(leaf,
5386 path->slots[0]) <= len) {
5387 return send_encoded_inline_extent(sctx, path, offset,
5388 len);
5389 } else if (!is_inline &&
5390 btrfs_file_extent_disk_num_bytes(leaf, ei) <= len) {
5391 return send_encoded_extent(sctx, path, offset, len);
5392 }
5393 }
5394
5395 if (sctx->cur_inode == NULL) {
5396 struct btrfs_root *root = sctx->send_root;
5397
5398 sctx->cur_inode = btrfs_iget(root->fs_info->sb, sctx->cur_ino, root);
5399 if (IS_ERR(sctx->cur_inode)) {
5400 int err = PTR_ERR(sctx->cur_inode);
5401
5402 sctx->cur_inode = NULL;
5403 return err;
5404 }
5405 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
5406 file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping);
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427 sctx->clean_page_cache = (sctx->cur_inode->i_mapping->nrpages == 0);
5428 sctx->page_cache_clear_start = round_down(offset, PAGE_SIZE);
5429 }
5430
5431 while (sent < len) {
5432 u64 size = min(len - sent, read_size);
5433 int ret;
5434
5435 ret = send_write(sctx, offset + sent, size);
5436 if (ret < 0)
5437 return ret;
5438 sent += size;
5439 }
5440
5441 if (sctx->clean_page_cache && IS_ALIGNED(end, PAGE_SIZE)) {
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465 truncate_inode_pages_range(&sctx->cur_inode->i_data,
5466 sctx->page_cache_clear_start,
5467 end - 1);
5468 sctx->page_cache_clear_start = end;
5469 }
5470
5471 return 0;
5472 }
5473
5474
5475
5476
5477
5478
5479
5480
5481 static int send_capabilities(struct send_ctx *sctx)
5482 {
5483 struct fs_path *fspath = NULL;
5484 struct btrfs_path *path;
5485 struct btrfs_dir_item *di;
5486 struct extent_buffer *leaf;
5487 unsigned long data_ptr;
5488 char *buf = NULL;
5489 int buf_len;
5490 int ret = 0;
5491
5492 path = alloc_path_for_send();
5493 if (!path)
5494 return -ENOMEM;
5495
5496 di = btrfs_lookup_xattr(NULL, sctx->send_root, path, sctx->cur_ino,
5497 XATTR_NAME_CAPS, strlen(XATTR_NAME_CAPS), 0);
5498 if (!di) {
5499
5500 goto out;
5501 } else if (IS_ERR(di)) {
5502 ret = PTR_ERR(di);
5503 goto out;
5504 }
5505
5506 leaf = path->nodes[0];
5507 buf_len = btrfs_dir_data_len(leaf, di);
5508
5509 fspath = fs_path_alloc();
5510 buf = kmalloc(buf_len, GFP_KERNEL);
5511 if (!fspath || !buf) {
5512 ret = -ENOMEM;
5513 goto out;
5514 }
5515
5516 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, fspath);
5517 if (ret < 0)
5518 goto out;
5519
5520 data_ptr = (unsigned long)(di + 1) + btrfs_dir_name_len(leaf, di);
5521 read_extent_buffer(leaf, buf, data_ptr, buf_len);
5522
5523 ret = send_set_xattr(sctx, fspath, XATTR_NAME_CAPS,
5524 strlen(XATTR_NAME_CAPS), buf, buf_len);
5525 out:
5526 kfree(buf);
5527 fs_path_free(fspath);
5528 btrfs_free_path(path);
5529 return ret;
5530 }
5531
5532 static int clone_range(struct send_ctx *sctx, struct btrfs_path *dst_path,
5533 struct clone_root *clone_root, const u64 disk_byte,
5534 u64 data_offset, u64 offset, u64 len)
5535 {
5536 struct btrfs_path *path;
5537 struct btrfs_key key;
5538 int ret;
5539 u64 clone_src_i_size = 0;
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556 if (clone_root->offset == 0 &&
5557 len == sctx->send_root->fs_info->sectorsize)
5558 return send_extent_data(sctx, dst_path, offset, len);
5559
5560 path = alloc_path_for_send();
5561 if (!path)
5562 return -ENOMEM;
5563
5564
5565
5566
5567
5568 ret = __get_inode_info(clone_root->root, path, clone_root->ino,
5569 &clone_src_i_size, NULL, NULL, NULL, NULL, NULL,
5570 NULL);
5571 btrfs_release_path(path);
5572 if (ret < 0)
5573 goto out;
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597 key.objectid = clone_root->ino;
5598 key.type = BTRFS_EXTENT_DATA_KEY;
5599 key.offset = clone_root->offset;
5600 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5601 if (ret < 0)
5602 goto out;
5603 if (ret > 0 && path->slots[0] > 0) {
5604 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5605 if (key.objectid == clone_root->ino &&
5606 key.type == BTRFS_EXTENT_DATA_KEY)
5607 path->slots[0]--;
5608 }
5609
5610 while (true) {
5611 struct extent_buffer *leaf = path->nodes[0];
5612 int slot = path->slots[0];
5613 struct btrfs_file_extent_item *ei;
5614 u8 type;
5615 u64 ext_len;
5616 u64 clone_len;
5617 u64 clone_data_offset;
5618
5619 if (slot >= btrfs_header_nritems(leaf)) {
5620 ret = btrfs_next_leaf(clone_root->root, path);
5621 if (ret < 0)
5622 goto out;
5623 else if (ret > 0)
5624 break;
5625 continue;
5626 }
5627
5628 btrfs_item_key_to_cpu(leaf, &key, slot);
5629
5630
5631
5632
5633
5634 if (key.objectid != clone_root->ino ||
5635 key.type != BTRFS_EXTENT_DATA_KEY)
5636 break;
5637
5638 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5639 type = btrfs_file_extent_type(leaf, ei);
5640 if (type == BTRFS_FILE_EXTENT_INLINE) {
5641 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
5642 ext_len = PAGE_ALIGN(ext_len);
5643 } else {
5644 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5645 }
5646
5647 if (key.offset + ext_len <= clone_root->offset)
5648 goto next;
5649
5650 if (key.offset > clone_root->offset) {
5651
5652 u64 hole_len = key.offset - clone_root->offset;
5653
5654 if (hole_len > len)
5655 hole_len = len;
5656 ret = send_extent_data(sctx, dst_path, offset,
5657 hole_len);
5658 if (ret < 0)
5659 goto out;
5660
5661 len -= hole_len;
5662 if (len == 0)
5663 break;
5664 offset += hole_len;
5665 clone_root->offset += hole_len;
5666 data_offset += hole_len;
5667 }
5668
5669 if (key.offset >= clone_root->offset + len)
5670 break;
5671
5672 if (key.offset >= clone_src_i_size)
5673 break;
5674
5675 if (key.offset + ext_len > clone_src_i_size)
5676 ext_len = clone_src_i_size - key.offset;
5677
5678 clone_data_offset = btrfs_file_extent_offset(leaf, ei);
5679 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte) {
5680 clone_root->offset = key.offset;
5681 if (clone_data_offset < data_offset &&
5682 clone_data_offset + ext_len > data_offset) {
5683 u64 extent_offset;
5684
5685 extent_offset = data_offset - clone_data_offset;
5686 ext_len -= extent_offset;
5687 clone_data_offset += extent_offset;
5688 clone_root->offset += extent_offset;
5689 }
5690 }
5691
5692 clone_len = min_t(u64, ext_len, len);
5693
5694 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5695 clone_data_offset == data_offset) {
5696 const u64 src_end = clone_root->offset + clone_len;
5697 const u64 sectorsize = SZ_64K;
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717 if (src_end == clone_src_i_size &&
5718 !IS_ALIGNED(src_end, sectorsize) &&
5719 offset + clone_len < sctx->cur_inode_size) {
5720 u64 slen;
5721
5722 slen = ALIGN_DOWN(src_end - clone_root->offset,
5723 sectorsize);
5724 if (slen > 0) {
5725 ret = send_clone(sctx, offset, slen,
5726 clone_root);
5727 if (ret < 0)
5728 goto out;
5729 }
5730 ret = send_extent_data(sctx, dst_path,
5731 offset + slen,
5732 clone_len - slen);
5733 } else {
5734 ret = send_clone(sctx, offset, clone_len,
5735 clone_root);
5736 }
5737 } else {
5738 ret = send_extent_data(sctx, dst_path, offset,
5739 clone_len);
5740 }
5741
5742 if (ret < 0)
5743 goto out;
5744
5745 len -= clone_len;
5746 if (len == 0)
5747 break;
5748 offset += clone_len;
5749 clone_root->offset += clone_len;
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760 if (clone_root->root == sctx->send_root &&
5761 clone_root->ino == sctx->cur_ino &&
5762 clone_root->offset >= sctx->cur_inode_next_write_offset)
5763 break;
5764
5765 data_offset += clone_len;
5766 next:
5767 path->slots[0]++;
5768 }
5769
5770 if (len > 0)
5771 ret = send_extent_data(sctx, dst_path, offset, len);
5772 else
5773 ret = 0;
5774 out:
5775 btrfs_free_path(path);
5776 return ret;
5777 }
5778
5779 static int send_write_or_clone(struct send_ctx *sctx,
5780 struct btrfs_path *path,
5781 struct btrfs_key *key,
5782 struct clone_root *clone_root)
5783 {
5784 int ret = 0;
5785 u64 offset = key->offset;
5786 u64 end;
5787 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
5788
5789 end = min_t(u64, btrfs_file_extent_end(path), sctx->cur_inode_size);
5790 if (offset >= end)
5791 return 0;
5792
5793 if (clone_root && IS_ALIGNED(end, bs)) {
5794 struct btrfs_file_extent_item *ei;
5795 u64 disk_byte;
5796 u64 data_offset;
5797
5798 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5799 struct btrfs_file_extent_item);
5800 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5801 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5802 ret = clone_range(sctx, path, clone_root, disk_byte,
5803 data_offset, offset, end - offset);
5804 } else {
5805 ret = send_extent_data(sctx, path, offset, end - offset);
5806 }
5807 sctx->cur_inode_next_write_offset = end;
5808 return ret;
5809 }
5810
5811 static int is_extent_unchanged(struct send_ctx *sctx,
5812 struct btrfs_path *left_path,
5813 struct btrfs_key *ekey)
5814 {
5815 int ret = 0;
5816 struct btrfs_key key;
5817 struct btrfs_path *path = NULL;
5818 struct extent_buffer *eb;
5819 int slot;
5820 struct btrfs_key found_key;
5821 struct btrfs_file_extent_item *ei;
5822 u64 left_disknr;
5823 u64 right_disknr;
5824 u64 left_offset;
5825 u64 right_offset;
5826 u64 left_offset_fixed;
5827 u64 left_len;
5828 u64 right_len;
5829 u64 left_gen;
5830 u64 right_gen;
5831 u8 left_type;
5832 u8 right_type;
5833
5834 path = alloc_path_for_send();
5835 if (!path)
5836 return -ENOMEM;
5837
5838 eb = left_path->nodes[0];
5839 slot = left_path->slots[0];
5840 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5841 left_type = btrfs_file_extent_type(eb, ei);
5842
5843 if (left_type != BTRFS_FILE_EXTENT_REG) {
5844 ret = 0;
5845 goto out;
5846 }
5847 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5848 left_len = btrfs_file_extent_num_bytes(eb, ei);
5849 left_offset = btrfs_file_extent_offset(eb, ei);
5850 left_gen = btrfs_file_extent_generation(eb, ei);
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873 key.objectid = ekey->objectid;
5874 key.type = BTRFS_EXTENT_DATA_KEY;
5875 key.offset = ekey->offset;
5876 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5877 if (ret < 0)
5878 goto out;
5879 if (ret) {
5880 ret = 0;
5881 goto out;
5882 }
5883
5884
5885
5886
5887 eb = path->nodes[0];
5888 slot = path->slots[0];
5889 btrfs_item_key_to_cpu(eb, &found_key, slot);
5890 if (found_key.objectid != key.objectid ||
5891 found_key.type != key.type) {
5892
5893 ret = (left_disknr) ? 0 : 1;
5894 goto out;
5895 }
5896
5897
5898
5899
5900 key = found_key;
5901 while (key.offset < ekey->offset + left_len) {
5902 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5903 right_type = btrfs_file_extent_type(eb, ei);
5904 if (right_type != BTRFS_FILE_EXTENT_REG &&
5905 right_type != BTRFS_FILE_EXTENT_INLINE) {
5906 ret = 0;
5907 goto out;
5908 }
5909
5910 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5911 right_len = btrfs_file_extent_ram_bytes(eb, ei);
5912 right_len = PAGE_ALIGN(right_len);
5913 } else {
5914 right_len = btrfs_file_extent_num_bytes(eb, ei);
5915 }
5916
5917
5918
5919
5920
5921 if (found_key.offset + right_len <= ekey->offset) {
5922
5923 ret = (left_disknr) ? 0 : 1;
5924 goto out;
5925 }
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5936 ret = 0;
5937 goto out;
5938 }
5939
5940 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5941 right_offset = btrfs_file_extent_offset(eb, ei);
5942 right_gen = btrfs_file_extent_generation(eb, ei);
5943
5944 left_offset_fixed = left_offset;
5945 if (key.offset < ekey->offset) {
5946
5947 right_offset += ekey->offset - key.offset;
5948 } else {
5949
5950 left_offset_fixed += key.offset - ekey->offset;
5951 }
5952
5953
5954
5955
5956 if (left_disknr != right_disknr ||
5957 left_offset_fixed != right_offset ||
5958 left_gen != right_gen) {
5959 ret = 0;
5960 goto out;
5961 }
5962
5963
5964
5965
5966 ret = btrfs_next_item(sctx->parent_root, path);
5967 if (ret < 0)
5968 goto out;
5969 if (!ret) {
5970 eb = path->nodes[0];
5971 slot = path->slots[0];
5972 btrfs_item_key_to_cpu(eb, &found_key, slot);
5973 }
5974 if (ret || found_key.objectid != key.objectid ||
5975 found_key.type != key.type) {
5976 key.offset += right_len;
5977 break;
5978 }
5979 if (found_key.offset != key.offset + right_len) {
5980 ret = 0;
5981 goto out;
5982 }
5983 key = found_key;
5984 }
5985
5986
5987
5988
5989
5990 if (key.offset >= ekey->offset + left_len)
5991 ret = 1;
5992 else
5993 ret = 0;
5994
5995
5996 out:
5997 btrfs_free_path(path);
5998 return ret;
5999 }
6000
6001 static int get_last_extent(struct send_ctx *sctx, u64 offset)
6002 {
6003 struct btrfs_path *path;
6004 struct btrfs_root *root = sctx->send_root;
6005 struct btrfs_key key;
6006 int ret;
6007
6008 path = alloc_path_for_send();
6009 if (!path)
6010 return -ENOMEM;
6011
6012 sctx->cur_inode_last_extent = 0;
6013
6014 key.objectid = sctx->cur_ino;
6015 key.type = BTRFS_EXTENT_DATA_KEY;
6016 key.offset = offset;
6017 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
6018 if (ret < 0)
6019 goto out;
6020 ret = 0;
6021 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
6022 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
6023 goto out;
6024
6025 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
6026 out:
6027 btrfs_free_path(path);
6028 return ret;
6029 }
6030
6031 static int range_is_hole_in_parent(struct send_ctx *sctx,
6032 const u64 start,
6033 const u64 end)
6034 {
6035 struct btrfs_path *path;
6036 struct btrfs_key key;
6037 struct btrfs_root *root = sctx->parent_root;
6038 u64 search_start = start;
6039 int ret;
6040
6041 path = alloc_path_for_send();
6042 if (!path)
6043 return -ENOMEM;
6044
6045 key.objectid = sctx->cur_ino;
6046 key.type = BTRFS_EXTENT_DATA_KEY;
6047 key.offset = search_start;
6048 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6049 if (ret < 0)
6050 goto out;
6051 if (ret > 0 && path->slots[0] > 0)
6052 path->slots[0]--;
6053
6054 while (search_start < end) {
6055 struct extent_buffer *leaf = path->nodes[0];
6056 int slot = path->slots[0];
6057 struct btrfs_file_extent_item *fi;
6058 u64 extent_end;
6059
6060 if (slot >= btrfs_header_nritems(leaf)) {
6061 ret = btrfs_next_leaf(root, path);
6062 if (ret < 0)
6063 goto out;
6064 else if (ret > 0)
6065 break;
6066 continue;
6067 }
6068
6069 btrfs_item_key_to_cpu(leaf, &key, slot);
6070 if (key.objectid < sctx->cur_ino ||
6071 key.type < BTRFS_EXTENT_DATA_KEY)
6072 goto next;
6073 if (key.objectid > sctx->cur_ino ||
6074 key.type > BTRFS_EXTENT_DATA_KEY ||
6075 key.offset >= end)
6076 break;
6077
6078 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6079 extent_end = btrfs_file_extent_end(path);
6080 if (extent_end <= start)
6081 goto next;
6082 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
6083 search_start = extent_end;
6084 goto next;
6085 }
6086 ret = 0;
6087 goto out;
6088 next:
6089 path->slots[0]++;
6090 }
6091 ret = 1;
6092 out:
6093 btrfs_free_path(path);
6094 return ret;
6095 }
6096
6097 static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
6098 struct btrfs_key *key)
6099 {
6100 int ret = 0;
6101
6102 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
6103 return 0;
6104
6105 if (sctx->cur_inode_last_extent == (u64)-1) {
6106 ret = get_last_extent(sctx, key->offset - 1);
6107 if (ret)
6108 return ret;
6109 }
6110
6111 if (path->slots[0] == 0 &&
6112 sctx->cur_inode_last_extent < key->offset) {
6113
6114
6115
6116
6117
6118
6119
6120 ret = get_last_extent(sctx, key->offset - 1);
6121 if (ret)
6122 return ret;
6123 }
6124
6125 if (sctx->cur_inode_last_extent < key->offset) {
6126 ret = range_is_hole_in_parent(sctx,
6127 sctx->cur_inode_last_extent,
6128 key->offset);
6129 if (ret < 0)
6130 return ret;
6131 else if (ret == 0)
6132 ret = send_hole(sctx, key->offset);
6133 else
6134 ret = 0;
6135 }
6136 sctx->cur_inode_last_extent = btrfs_file_extent_end(path);
6137 return ret;
6138 }
6139
6140 static int process_extent(struct send_ctx *sctx,
6141 struct btrfs_path *path,
6142 struct btrfs_key *key)
6143 {
6144 struct clone_root *found_clone = NULL;
6145 int ret = 0;
6146
6147 if (S_ISLNK(sctx->cur_inode_mode))
6148 return 0;
6149
6150 if (sctx->parent_root && !sctx->cur_inode_new) {
6151 ret = is_extent_unchanged(sctx, path, key);
6152 if (ret < 0)
6153 goto out;
6154 if (ret) {
6155 ret = 0;
6156 goto out_hole;
6157 }
6158 } else {
6159 struct btrfs_file_extent_item *ei;
6160 u8 type;
6161
6162 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
6163 struct btrfs_file_extent_item);
6164 type = btrfs_file_extent_type(path->nodes[0], ei);
6165 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
6166 type == BTRFS_FILE_EXTENT_REG) {
6167
6168
6169
6170
6171
6172
6173 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
6174 ret = 0;
6175 goto out;
6176 }
6177
6178
6179 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
6180 ret = 0;
6181 goto out;
6182 }
6183 }
6184 }
6185
6186 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
6187 sctx->cur_inode_size, &found_clone);
6188 if (ret != -ENOENT && ret < 0)
6189 goto out;
6190
6191 ret = send_write_or_clone(sctx, path, key, found_clone);
6192 if (ret)
6193 goto out;
6194 out_hole:
6195 ret = maybe_send_hole(sctx, path, key);
6196 out:
6197 return ret;
6198 }
6199
6200 static int process_all_extents(struct send_ctx *sctx)
6201 {
6202 int ret = 0;
6203 int iter_ret = 0;
6204 struct btrfs_root *root;
6205 struct btrfs_path *path;
6206 struct btrfs_key key;
6207 struct btrfs_key found_key;
6208
6209 root = sctx->send_root;
6210 path = alloc_path_for_send();
6211 if (!path)
6212 return -ENOMEM;
6213
6214 key.objectid = sctx->cmp_key->objectid;
6215 key.type = BTRFS_EXTENT_DATA_KEY;
6216 key.offset = 0;
6217 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
6218 if (found_key.objectid != key.objectid ||
6219 found_key.type != key.type) {
6220 ret = 0;
6221 break;
6222 }
6223
6224 ret = process_extent(sctx, path, &found_key);
6225 if (ret < 0)
6226 break;
6227 }
6228
6229 if (iter_ret < 0)
6230 ret = iter_ret;
6231
6232 btrfs_free_path(path);
6233 return ret;
6234 }
6235
6236 static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
6237 int *pending_move,
6238 int *refs_processed)
6239 {
6240 int ret = 0;
6241
6242 if (sctx->cur_ino == 0)
6243 goto out;
6244 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
6245 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
6246 goto out;
6247 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
6248 goto out;
6249
6250 ret = process_recorded_refs(sctx, pending_move);
6251 if (ret < 0)
6252 goto out;
6253
6254 *refs_processed = 1;
6255 out:
6256 return ret;
6257 }
6258
6259 static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
6260 {
6261 int ret = 0;
6262 u64 left_mode;
6263 u64 left_uid;
6264 u64 left_gid;
6265 u64 left_fileattr;
6266 u64 right_mode;
6267 u64 right_uid;
6268 u64 right_gid;
6269 u64 right_fileattr;
6270 int need_chmod = 0;
6271 int need_chown = 0;
6272 bool need_fileattr = false;
6273 int need_truncate = 1;
6274 int pending_move = 0;
6275 int refs_processed = 0;
6276
6277 if (sctx->ignore_cur_inode)
6278 return 0;
6279
6280 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
6281 &refs_processed);
6282 if (ret < 0)
6283 goto out;
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297 if (refs_processed && !pending_move)
6298 sctx->send_progress = sctx->cur_ino + 1;
6299
6300 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
6301 goto out;
6302 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
6303 goto out;
6304
6305 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
6306 &left_mode, &left_uid, &left_gid, NULL, &left_fileattr);
6307 if (ret < 0)
6308 goto out;
6309
6310 if (!sctx->parent_root || sctx->cur_inode_new) {
6311 need_chown = 1;
6312 if (!S_ISLNK(sctx->cur_inode_mode))
6313 need_chmod = 1;
6314 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
6315 need_truncate = 0;
6316 } else {
6317 u64 old_size;
6318
6319 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
6320 &old_size, NULL, &right_mode, &right_uid,
6321 &right_gid, NULL, &right_fileattr);
6322 if (ret < 0)
6323 goto out;
6324
6325 if (left_uid != right_uid || left_gid != right_gid)
6326 need_chown = 1;
6327 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
6328 need_chmod = 1;
6329 if (!S_ISLNK(sctx->cur_inode_mode) && left_fileattr != right_fileattr)
6330 need_fileattr = true;
6331 if ((old_size == sctx->cur_inode_size) ||
6332 (sctx->cur_inode_size > old_size &&
6333 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
6334 need_truncate = 0;
6335 }
6336
6337 if (S_ISREG(sctx->cur_inode_mode)) {
6338 if (need_send_hole(sctx)) {
6339 if (sctx->cur_inode_last_extent == (u64)-1 ||
6340 sctx->cur_inode_last_extent <
6341 sctx->cur_inode_size) {
6342 ret = get_last_extent(sctx, (u64)-1);
6343 if (ret)
6344 goto out;
6345 }
6346 if (sctx->cur_inode_last_extent <
6347 sctx->cur_inode_size) {
6348 ret = send_hole(sctx, sctx->cur_inode_size);
6349 if (ret)
6350 goto out;
6351 }
6352 }
6353 if (need_truncate) {
6354 ret = send_truncate(sctx, sctx->cur_ino,
6355 sctx->cur_inode_gen,
6356 sctx->cur_inode_size);
6357 if (ret < 0)
6358 goto out;
6359 }
6360 }
6361
6362 if (need_chown) {
6363 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6364 left_uid, left_gid);
6365 if (ret < 0)
6366 goto out;
6367 }
6368 if (need_chmod) {
6369 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6370 left_mode);
6371 if (ret < 0)
6372 goto out;
6373 }
6374 if (need_fileattr) {
6375 ret = send_fileattr(sctx, sctx->cur_ino, sctx->cur_inode_gen,
6376 left_fileattr);
6377 if (ret < 0)
6378 goto out;
6379 }
6380
6381 ret = send_capabilities(sctx);
6382 if (ret < 0)
6383 goto out;
6384
6385
6386
6387
6388
6389 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
6390 ret = apply_children_dir_moves(sctx);
6391 if (ret)
6392 goto out;
6393
6394
6395
6396
6397
6398
6399
6400 sctx->send_progress = sctx->cur_ino + 1;
6401 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
6402 if (ret < 0)
6403 goto out;
6404 }
6405
6406 out:
6407 return ret;
6408 }
6409
6410 struct parent_paths_ctx {
6411 struct list_head *refs;
6412 struct send_ctx *sctx;
6413 };
6414
6415 static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
6416 void *ctx)
6417 {
6418 struct parent_paths_ctx *ppctx = ctx;
6419
6420
6421
6422
6423
6424
6425 return record_ref_in_tree(&ppctx->sctx->rbtree_deleted_refs, ppctx->refs,
6426 name, dir, 0, ppctx->sctx);
6427 }
6428
6429
6430
6431
6432
6433 static int btrfs_unlink_all_paths(struct send_ctx *sctx)
6434 {
6435 LIST_HEAD(deleted_refs);
6436 struct btrfs_path *path;
6437 struct btrfs_root *root = sctx->parent_root;
6438 struct btrfs_key key;
6439 struct btrfs_key found_key;
6440 struct parent_paths_ctx ctx;
6441 int iter_ret = 0;
6442 int ret;
6443
6444 path = alloc_path_for_send();
6445 if (!path)
6446 return -ENOMEM;
6447
6448 key.objectid = sctx->cur_ino;
6449 key.type = BTRFS_INODE_REF_KEY;
6450 key.offset = 0;
6451
6452 ctx.refs = &deleted_refs;
6453 ctx.sctx = sctx;
6454
6455 btrfs_for_each_slot(root, &key, &found_key, path, iter_ret) {
6456 if (found_key.objectid != key.objectid)
6457 break;
6458 if (found_key.type != key.type &&
6459 found_key.type != BTRFS_INODE_EXTREF_KEY)
6460 break;
6461
6462 ret = iterate_inode_ref(root, path, &found_key, 1,
6463 record_parent_ref, &ctx);
6464 if (ret < 0)
6465 goto out;
6466 }
6467
6468 if (iter_ret < 0) {
6469 ret = iter_ret;
6470 goto out;
6471 }
6472
6473 while (!list_empty(&deleted_refs)) {
6474 struct recorded_ref *ref;
6475
6476 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6477 ret = send_unlink(sctx, ref->full_path);
6478 if (ret < 0)
6479 goto out;
6480 recorded_ref_free(ref);
6481 }
6482 ret = 0;
6483 out:
6484 btrfs_free_path(path);
6485 if (ret)
6486 __free_recorded_refs(&deleted_refs);
6487 return ret;
6488 }
6489
6490 static void close_current_inode(struct send_ctx *sctx)
6491 {
6492 u64 i_size;
6493
6494 if (sctx->cur_inode == NULL)
6495 return;
6496
6497 i_size = i_size_read(sctx->cur_inode);
6498
6499
6500
6501
6502
6503
6504
6505 if (sctx->clean_page_cache && sctx->page_cache_clear_start < i_size)
6506 truncate_inode_pages_range(&sctx->cur_inode->i_data,
6507 sctx->page_cache_clear_start,
6508 round_up(i_size, PAGE_SIZE) - 1);
6509
6510 iput(sctx->cur_inode);
6511 sctx->cur_inode = NULL;
6512 }
6513
6514 static int changed_inode(struct send_ctx *sctx,
6515 enum btrfs_compare_tree_result result)
6516 {
6517 int ret = 0;
6518 struct btrfs_key *key = sctx->cmp_key;
6519 struct btrfs_inode_item *left_ii = NULL;
6520 struct btrfs_inode_item *right_ii = NULL;
6521 u64 left_gen = 0;
6522 u64 right_gen = 0;
6523
6524 close_current_inode(sctx);
6525
6526 sctx->cur_ino = key->objectid;
6527 sctx->cur_inode_new_gen = false;
6528 sctx->cur_inode_last_extent = (u64)-1;
6529 sctx->cur_inode_next_write_offset = 0;
6530 sctx->ignore_cur_inode = false;
6531
6532
6533
6534
6535
6536
6537 sctx->send_progress = sctx->cur_ino;
6538
6539 if (result == BTRFS_COMPARE_TREE_NEW ||
6540 result == BTRFS_COMPARE_TREE_CHANGED) {
6541 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6542 sctx->left_path->slots[0],
6543 struct btrfs_inode_item);
6544 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6545 left_ii);
6546 } else {
6547 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6548 sctx->right_path->slots[0],
6549 struct btrfs_inode_item);
6550 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6551 right_ii);
6552 }
6553 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6554 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6555 sctx->right_path->slots[0],
6556 struct btrfs_inode_item);
6557
6558 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6559 right_ii);
6560
6561
6562
6563
6564
6565
6566 if (left_gen != right_gen &&
6567 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6568 sctx->cur_inode_new_gen = true;
6569 }
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585 if (result == BTRFS_COMPARE_TREE_NEW ||
6586 result == BTRFS_COMPARE_TREE_CHANGED) {
6587 u32 nlinks;
6588
6589 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6590 if (nlinks == 0) {
6591 sctx->ignore_cur_inode = true;
6592 if (result == BTRFS_COMPARE_TREE_CHANGED)
6593 ret = btrfs_unlink_all_paths(sctx);
6594 goto out;
6595 }
6596 }
6597
6598 if (result == BTRFS_COMPARE_TREE_NEW) {
6599 sctx->cur_inode_gen = left_gen;
6600 sctx->cur_inode_new = true;
6601 sctx->cur_inode_deleted = false;
6602 sctx->cur_inode_size = btrfs_inode_size(
6603 sctx->left_path->nodes[0], left_ii);
6604 sctx->cur_inode_mode = btrfs_inode_mode(
6605 sctx->left_path->nodes[0], left_ii);
6606 sctx->cur_inode_rdev = btrfs_inode_rdev(
6607 sctx->left_path->nodes[0], left_ii);
6608 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6609 ret = send_create_inode_if_needed(sctx);
6610 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6611 sctx->cur_inode_gen = right_gen;
6612 sctx->cur_inode_new = false;
6613 sctx->cur_inode_deleted = true;
6614 sctx->cur_inode_size = btrfs_inode_size(
6615 sctx->right_path->nodes[0], right_ii);
6616 sctx->cur_inode_mode = btrfs_inode_mode(
6617 sctx->right_path->nodes[0], right_ii);
6618 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
6619
6620
6621
6622
6623
6624
6625
6626 if (sctx->cur_inode_new_gen) {
6627
6628
6629
6630 sctx->cur_inode_gen = right_gen;
6631 sctx->cur_inode_new = false;
6632 sctx->cur_inode_deleted = true;
6633 sctx->cur_inode_size = btrfs_inode_size(
6634 sctx->right_path->nodes[0], right_ii);
6635 sctx->cur_inode_mode = btrfs_inode_mode(
6636 sctx->right_path->nodes[0], right_ii);
6637 ret = process_all_refs(sctx,
6638 BTRFS_COMPARE_TREE_DELETED);
6639 if (ret < 0)
6640 goto out;
6641
6642
6643
6644
6645 sctx->cur_inode_gen = left_gen;
6646 sctx->cur_inode_new = true;
6647 sctx->cur_inode_deleted = false;
6648 sctx->cur_inode_size = btrfs_inode_size(
6649 sctx->left_path->nodes[0], left_ii);
6650 sctx->cur_inode_mode = btrfs_inode_mode(
6651 sctx->left_path->nodes[0], left_ii);
6652 sctx->cur_inode_rdev = btrfs_inode_rdev(
6653 sctx->left_path->nodes[0], left_ii);
6654 ret = send_create_inode_if_needed(sctx);
6655 if (ret < 0)
6656 goto out;
6657
6658 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6659 if (ret < 0)
6660 goto out;
6661
6662
6663
6664
6665 sctx->send_progress = sctx->cur_ino + 1;
6666
6667
6668
6669
6670
6671 ret = process_all_extents(sctx);
6672 if (ret < 0)
6673 goto out;
6674 ret = process_all_new_xattrs(sctx);
6675 if (ret < 0)
6676 goto out;
6677 } else {
6678 sctx->cur_inode_gen = left_gen;
6679 sctx->cur_inode_new = false;
6680 sctx->cur_inode_new_gen = false;
6681 sctx->cur_inode_deleted = false;
6682 sctx->cur_inode_size = btrfs_inode_size(
6683 sctx->left_path->nodes[0], left_ii);
6684 sctx->cur_inode_mode = btrfs_inode_mode(
6685 sctx->left_path->nodes[0], left_ii);
6686 }
6687 }
6688
6689 out:
6690 return ret;
6691 }
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703 static int changed_ref(struct send_ctx *sctx,
6704 enum btrfs_compare_tree_result result)
6705 {
6706 int ret = 0;
6707
6708 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6709 inconsistent_snapshot_error(sctx, result, "reference");
6710 return -EIO;
6711 }
6712
6713 if (!sctx->cur_inode_new_gen &&
6714 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6715 if (result == BTRFS_COMPARE_TREE_NEW)
6716 ret = record_new_ref(sctx);
6717 else if (result == BTRFS_COMPARE_TREE_DELETED)
6718 ret = record_deleted_ref(sctx);
6719 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6720 ret = record_changed_ref(sctx);
6721 }
6722
6723 return ret;
6724 }
6725
6726
6727
6728
6729
6730
6731 static int changed_xattr(struct send_ctx *sctx,
6732 enum btrfs_compare_tree_result result)
6733 {
6734 int ret = 0;
6735
6736 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6737 inconsistent_snapshot_error(sctx, result, "xattr");
6738 return -EIO;
6739 }
6740
6741 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6742 if (result == BTRFS_COMPARE_TREE_NEW)
6743 ret = process_new_xattr(sctx);
6744 else if (result == BTRFS_COMPARE_TREE_DELETED)
6745 ret = process_deleted_xattr(sctx);
6746 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6747 ret = process_changed_xattr(sctx);
6748 }
6749
6750 return ret;
6751 }
6752
6753
6754
6755
6756
6757
6758 static int changed_extent(struct send_ctx *sctx,
6759 enum btrfs_compare_tree_result result)
6760 {
6761 int ret = 0;
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776 if (sctx->cur_ino != sctx->cmp_key->objectid)
6777 return 0;
6778
6779 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6780 if (result != BTRFS_COMPARE_TREE_DELETED)
6781 ret = process_extent(sctx, sctx->left_path,
6782 sctx->cmp_key);
6783 }
6784
6785 return ret;
6786 }
6787
6788 static int dir_changed(struct send_ctx *sctx, u64 dir)
6789 {
6790 u64 orig_gen, new_gen;
6791 int ret;
6792
6793 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6794 NULL, NULL, NULL);
6795 if (ret)
6796 return ret;
6797
6798 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6799 NULL, NULL, NULL, NULL);
6800 if (ret)
6801 return ret;
6802
6803 return (orig_gen != new_gen) ? 1 : 0;
6804 }
6805
6806 static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6807 struct btrfs_key *key)
6808 {
6809 struct btrfs_inode_extref *extref;
6810 struct extent_buffer *leaf;
6811 u64 dirid = 0, last_dirid = 0;
6812 unsigned long ptr;
6813 u32 item_size;
6814 u32 cur_offset = 0;
6815 int ref_name_len;
6816 int ret = 0;
6817
6818
6819 if (key->type == BTRFS_INODE_REF_KEY) {
6820 dirid = key->offset;
6821
6822 ret = dir_changed(sctx, dirid);
6823 goto out;
6824 }
6825
6826 leaf = path->nodes[0];
6827 item_size = btrfs_item_size(leaf, path->slots[0]);
6828 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6829 while (cur_offset < item_size) {
6830 extref = (struct btrfs_inode_extref *)(ptr +
6831 cur_offset);
6832 dirid = btrfs_inode_extref_parent(leaf, extref);
6833 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6834 cur_offset += ref_name_len + sizeof(*extref);
6835 if (dirid == last_dirid)
6836 continue;
6837 ret = dir_changed(sctx, dirid);
6838 if (ret)
6839 break;
6840 last_dirid = dirid;
6841 }
6842 out:
6843 return ret;
6844 }
6845
6846
6847
6848
6849
6850 static int changed_cb(struct btrfs_path *left_path,
6851 struct btrfs_path *right_path,
6852 struct btrfs_key *key,
6853 enum btrfs_compare_tree_result result,
6854 struct send_ctx *sctx)
6855 {
6856 int ret = 0;
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883 lockdep_assert_not_held(&sctx->send_root->fs_info->commit_root_sem);
6884
6885
6886
6887
6888
6889
6890 if (left_path->nodes[0])
6891 ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
6892 &left_path->nodes[0]->bflags));
6893
6894
6895
6896
6897
6898 if (right_path && right_path->nodes[0])
6899 ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED,
6900 &right_path->nodes[0]->bflags));
6901
6902 if (result == BTRFS_COMPARE_TREE_SAME) {
6903 if (key->type == BTRFS_INODE_REF_KEY ||
6904 key->type == BTRFS_INODE_EXTREF_KEY) {
6905 ret = compare_refs(sctx, left_path, key);
6906 if (!ret)
6907 return 0;
6908 if (ret < 0)
6909 return ret;
6910 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6911 return maybe_send_hole(sctx, left_path, key);
6912 } else {
6913 return 0;
6914 }
6915 result = BTRFS_COMPARE_TREE_CHANGED;
6916 ret = 0;
6917 }
6918
6919 sctx->left_path = left_path;
6920 sctx->right_path = right_path;
6921 sctx->cmp_key = key;
6922
6923 ret = finish_inode_if_needed(sctx, 0);
6924 if (ret < 0)
6925 goto out;
6926
6927
6928 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6929 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6930 goto out;
6931
6932 if (key->type == BTRFS_INODE_ITEM_KEY) {
6933 ret = changed_inode(sctx, result);
6934 } else if (!sctx->ignore_cur_inode) {
6935 if (key->type == BTRFS_INODE_REF_KEY ||
6936 key->type == BTRFS_INODE_EXTREF_KEY)
6937 ret = changed_ref(sctx, result);
6938 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6939 ret = changed_xattr(sctx, result);
6940 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6941 ret = changed_extent(sctx, result);
6942 }
6943
6944 out:
6945 return ret;
6946 }
6947
6948 static int search_key_again(const struct send_ctx *sctx,
6949 struct btrfs_root *root,
6950 struct btrfs_path *path,
6951 const struct btrfs_key *key)
6952 {
6953 int ret;
6954
6955 if (!path->need_commit_sem)
6956 lockdep_assert_held_read(&root->fs_info->commit_root_sem);
6957
6958
6959
6960
6961
6962
6963
6964
6965 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6966 ASSERT(ret <= 0);
6967 if (ret > 0) {
6968 btrfs_print_tree(path->nodes[path->lowest_level], false);
6969 btrfs_err(root->fs_info,
6970 "send: key (%llu %u %llu) not found in %s root %llu, lowest_level %d, slot %d",
6971 key->objectid, key->type, key->offset,
6972 (root == sctx->parent_root ? "parent" : "send"),
6973 root->root_key.objectid, path->lowest_level,
6974 path->slots[path->lowest_level]);
6975 return -EUCLEAN;
6976 }
6977
6978 return ret;
6979 }
6980
6981 static int full_send_tree(struct send_ctx *sctx)
6982 {
6983 int ret;
6984 struct btrfs_root *send_root = sctx->send_root;
6985 struct btrfs_key key;
6986 struct btrfs_fs_info *fs_info = send_root->fs_info;
6987 struct btrfs_path *path;
6988
6989 path = alloc_path_for_send();
6990 if (!path)
6991 return -ENOMEM;
6992 path->reada = READA_FORWARD_ALWAYS;
6993
6994 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6995 key.type = BTRFS_INODE_ITEM_KEY;
6996 key.offset = 0;
6997
6998 down_read(&fs_info->commit_root_sem);
6999 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7000 up_read(&fs_info->commit_root_sem);
7001
7002 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
7003 if (ret < 0)
7004 goto out;
7005 if (ret)
7006 goto out_finish;
7007
7008 while (1) {
7009 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
7010
7011 ret = changed_cb(path, NULL, &key,
7012 BTRFS_COMPARE_TREE_NEW, sctx);
7013 if (ret < 0)
7014 goto out;
7015
7016 down_read(&fs_info->commit_root_sem);
7017 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
7018 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7019 up_read(&fs_info->commit_root_sem);
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030 btrfs_release_path(path);
7031 ret = search_key_again(sctx, send_root, path, &key);
7032 if (ret < 0)
7033 goto out;
7034 } else {
7035 up_read(&fs_info->commit_root_sem);
7036 }
7037
7038 ret = btrfs_next_item(send_root, path);
7039 if (ret < 0)
7040 goto out;
7041 if (ret) {
7042 ret = 0;
7043 break;
7044 }
7045 }
7046
7047 out_finish:
7048 ret = finish_inode_if_needed(sctx, 1);
7049
7050 out:
7051 btrfs_free_path(path);
7052 return ret;
7053 }
7054
7055 static int replace_node_with_clone(struct btrfs_path *path, int level)
7056 {
7057 struct extent_buffer *clone;
7058
7059 clone = btrfs_clone_extent_buffer(path->nodes[level]);
7060 if (!clone)
7061 return -ENOMEM;
7062
7063 free_extent_buffer(path->nodes[level]);
7064 path->nodes[level] = clone;
7065
7066 return 0;
7067 }
7068
7069 static int tree_move_down(struct btrfs_path *path, int *level, u64 reada_min_gen)
7070 {
7071 struct extent_buffer *eb;
7072 struct extent_buffer *parent = path->nodes[*level];
7073 int slot = path->slots[*level];
7074 const int nritems = btrfs_header_nritems(parent);
7075 u64 reada_max;
7076 u64 reada_done = 0;
7077
7078 lockdep_assert_held_read(&parent->fs_info->commit_root_sem);
7079
7080 BUG_ON(*level == 0);
7081 eb = btrfs_read_node_slot(parent, slot);
7082 if (IS_ERR(eb))
7083 return PTR_ERR(eb);
7084
7085
7086
7087
7088
7089
7090
7091 reada_max = (*level == 1 ? SZ_128K : eb->fs_info->nodesize);
7092
7093 for (slot++; slot < nritems && reada_done < reada_max; slot++) {
7094 if (btrfs_node_ptr_generation(parent, slot) > reada_min_gen) {
7095 btrfs_readahead_node_child(parent, slot);
7096 reada_done += eb->fs_info->nodesize;
7097 }
7098 }
7099
7100 path->nodes[*level - 1] = eb;
7101 path->slots[*level - 1] = 0;
7102 (*level)--;
7103
7104 if (*level == 0)
7105 return replace_node_with_clone(path, 0);
7106
7107 return 0;
7108 }
7109
7110 static int tree_move_next_or_upnext(struct btrfs_path *path,
7111 int *level, int root_level)
7112 {
7113 int ret = 0;
7114 int nritems;
7115 nritems = btrfs_header_nritems(path->nodes[*level]);
7116
7117 path->slots[*level]++;
7118
7119 while (path->slots[*level] >= nritems) {
7120 if (*level == root_level) {
7121 path->slots[*level] = nritems - 1;
7122 return -1;
7123 }
7124
7125
7126 path->slots[*level] = 0;
7127 free_extent_buffer(path->nodes[*level]);
7128 path->nodes[*level] = NULL;
7129 (*level)++;
7130 path->slots[*level]++;
7131
7132 nritems = btrfs_header_nritems(path->nodes[*level]);
7133 ret = 1;
7134 }
7135 return ret;
7136 }
7137
7138
7139
7140
7141
7142 static int tree_advance(struct btrfs_path *path,
7143 int *level, int root_level,
7144 int allow_down,
7145 struct btrfs_key *key,
7146 u64 reada_min_gen)
7147 {
7148 int ret;
7149
7150 if (*level == 0 || !allow_down) {
7151 ret = tree_move_next_or_upnext(path, level, root_level);
7152 } else {
7153 ret = tree_move_down(path, level, reada_min_gen);
7154 }
7155
7156
7157
7158
7159
7160
7161
7162 if (*level == 0)
7163 btrfs_item_key_to_cpu(path->nodes[*level], key,
7164 path->slots[*level]);
7165 else
7166 btrfs_node_key_to_cpu(path->nodes[*level], key,
7167 path->slots[*level]);
7168
7169 return ret;
7170 }
7171
7172 static int tree_compare_item(struct btrfs_path *left_path,
7173 struct btrfs_path *right_path,
7174 char *tmp_buf)
7175 {
7176 int cmp;
7177 int len1, len2;
7178 unsigned long off1, off2;
7179
7180 len1 = btrfs_item_size(left_path->nodes[0], left_path->slots[0]);
7181 len2 = btrfs_item_size(right_path->nodes[0], right_path->slots[0]);
7182 if (len1 != len2)
7183 return 1;
7184
7185 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
7186 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
7187 right_path->slots[0]);
7188
7189 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
7190
7191 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
7192 if (cmp)
7193 return 1;
7194 return 0;
7195 }
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216 static int restart_after_relocation(struct btrfs_path *left_path,
7217 struct btrfs_path *right_path,
7218 const struct btrfs_key *left_key,
7219 const struct btrfs_key *right_key,
7220 int left_level,
7221 int right_level,
7222 const struct send_ctx *sctx)
7223 {
7224 int root_level;
7225 int ret;
7226
7227 lockdep_assert_held_read(&sctx->send_root->fs_info->commit_root_sem);
7228
7229 btrfs_release_path(left_path);
7230 btrfs_release_path(right_path);
7231
7232
7233
7234
7235
7236
7237
7238 left_path->lowest_level = left_level;
7239 ret = search_key_again(sctx, sctx->send_root, left_path, left_key);
7240 if (ret < 0)
7241 return ret;
7242
7243 right_path->lowest_level = right_level;
7244 ret = search_key_again(sctx, sctx->parent_root, right_path, right_key);
7245 if (ret < 0)
7246 return ret;
7247
7248
7249
7250
7251
7252
7253
7254 if (left_level == 0) {
7255 ret = replace_node_with_clone(left_path, 0);
7256 if (ret < 0)
7257 return ret;
7258 }
7259
7260 if (right_level == 0) {
7261 ret = replace_node_with_clone(right_path, 0);
7262 if (ret < 0)
7263 return ret;
7264 }
7265
7266
7267
7268
7269
7270
7271 root_level = btrfs_header_level(sctx->send_root->commit_root);
7272 if (root_level > 0) {
7273 ret = replace_node_with_clone(left_path, root_level);
7274 if (ret < 0)
7275 return ret;
7276 }
7277
7278 root_level = btrfs_header_level(sctx->parent_root->commit_root);
7279 if (root_level > 0) {
7280 ret = replace_node_with_clone(right_path, root_level);
7281 if (ret < 0)
7282 return ret;
7283 }
7284
7285 return 0;
7286 }
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301 static int btrfs_compare_trees(struct btrfs_root *left_root,
7302 struct btrfs_root *right_root, struct send_ctx *sctx)
7303 {
7304 struct btrfs_fs_info *fs_info = left_root->fs_info;
7305 int ret;
7306 int cmp;
7307 struct btrfs_path *left_path = NULL;
7308 struct btrfs_path *right_path = NULL;
7309 struct btrfs_key left_key;
7310 struct btrfs_key right_key;
7311 char *tmp_buf = NULL;
7312 int left_root_level;
7313 int right_root_level;
7314 int left_level;
7315 int right_level;
7316 int left_end_reached = 0;
7317 int right_end_reached = 0;
7318 int advance_left = 0;
7319 int advance_right = 0;
7320 u64 left_blockptr;
7321 u64 right_blockptr;
7322 u64 left_gen;
7323 u64 right_gen;
7324 u64 reada_min_gen;
7325
7326 left_path = btrfs_alloc_path();
7327 if (!left_path) {
7328 ret = -ENOMEM;
7329 goto out;
7330 }
7331 right_path = btrfs_alloc_path();
7332 if (!right_path) {
7333 ret = -ENOMEM;
7334 goto out;
7335 }
7336
7337 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
7338 if (!tmp_buf) {
7339 ret = -ENOMEM;
7340 goto out;
7341 }
7342
7343 left_path->search_commit_root = 1;
7344 left_path->skip_locking = 1;
7345 right_path->search_commit_root = 1;
7346 right_path->skip_locking = 1;
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384 down_read(&fs_info->commit_root_sem);
7385 left_level = btrfs_header_level(left_root->commit_root);
7386 left_root_level = left_level;
7387
7388
7389
7390
7391
7392
7393
7394 left_path->nodes[left_level] =
7395 btrfs_clone_extent_buffer(left_root->commit_root);
7396 if (!left_path->nodes[left_level]) {
7397 ret = -ENOMEM;
7398 goto out_unlock;
7399 }
7400
7401 right_level = btrfs_header_level(right_root->commit_root);
7402 right_root_level = right_level;
7403 right_path->nodes[right_level] =
7404 btrfs_clone_extent_buffer(right_root->commit_root);
7405 if (!right_path->nodes[right_level]) {
7406 ret = -ENOMEM;
7407 goto out_unlock;
7408 }
7409
7410
7411
7412
7413
7414
7415
7416 reada_min_gen = btrfs_header_generation(right_root->commit_root);
7417
7418 if (left_level == 0)
7419 btrfs_item_key_to_cpu(left_path->nodes[left_level],
7420 &left_key, left_path->slots[left_level]);
7421 else
7422 btrfs_node_key_to_cpu(left_path->nodes[left_level],
7423 &left_key, left_path->slots[left_level]);
7424 if (right_level == 0)
7425 btrfs_item_key_to_cpu(right_path->nodes[right_level],
7426 &right_key, right_path->slots[right_level]);
7427 else
7428 btrfs_node_key_to_cpu(right_path->nodes[right_level],
7429 &right_key, right_path->slots[right_level]);
7430
7431 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7432
7433 while (1) {
7434 if (need_resched() ||
7435 rwsem_is_contended(&fs_info->commit_root_sem)) {
7436 up_read(&fs_info->commit_root_sem);
7437 cond_resched();
7438 down_read(&fs_info->commit_root_sem);
7439 }
7440
7441 if (fs_info->last_reloc_trans > sctx->last_reloc_trans) {
7442 ret = restart_after_relocation(left_path, right_path,
7443 &left_key, &right_key,
7444 left_level, right_level,
7445 sctx);
7446 if (ret < 0)
7447 goto out_unlock;
7448 sctx->last_reloc_trans = fs_info->last_reloc_trans;
7449 }
7450
7451 if (advance_left && !left_end_reached) {
7452 ret = tree_advance(left_path, &left_level,
7453 left_root_level,
7454 advance_left != ADVANCE_ONLY_NEXT,
7455 &left_key, reada_min_gen);
7456 if (ret == -1)
7457 left_end_reached = ADVANCE;
7458 else if (ret < 0)
7459 goto out_unlock;
7460 advance_left = 0;
7461 }
7462 if (advance_right && !right_end_reached) {
7463 ret = tree_advance(right_path, &right_level,
7464 right_root_level,
7465 advance_right != ADVANCE_ONLY_NEXT,
7466 &right_key, reada_min_gen);
7467 if (ret == -1)
7468 right_end_reached = ADVANCE;
7469 else if (ret < 0)
7470 goto out_unlock;
7471 advance_right = 0;
7472 }
7473
7474 if (left_end_reached && right_end_reached) {
7475 ret = 0;
7476 goto out_unlock;
7477 } else if (left_end_reached) {
7478 if (right_level == 0) {
7479 up_read(&fs_info->commit_root_sem);
7480 ret = changed_cb(left_path, right_path,
7481 &right_key,
7482 BTRFS_COMPARE_TREE_DELETED,
7483 sctx);
7484 if (ret < 0)
7485 goto out;
7486 down_read(&fs_info->commit_root_sem);
7487 }
7488 advance_right = ADVANCE;
7489 continue;
7490 } else if (right_end_reached) {
7491 if (left_level == 0) {
7492 up_read(&fs_info->commit_root_sem);
7493 ret = changed_cb(left_path, right_path,
7494 &left_key,
7495 BTRFS_COMPARE_TREE_NEW,
7496 sctx);
7497 if (ret < 0)
7498 goto out;
7499 down_read(&fs_info->commit_root_sem);
7500 }
7501 advance_left = ADVANCE;
7502 continue;
7503 }
7504
7505 if (left_level == 0 && right_level == 0) {
7506 up_read(&fs_info->commit_root_sem);
7507 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7508 if (cmp < 0) {
7509 ret = changed_cb(left_path, right_path,
7510 &left_key,
7511 BTRFS_COMPARE_TREE_NEW,
7512 sctx);
7513 advance_left = ADVANCE;
7514 } else if (cmp > 0) {
7515 ret = changed_cb(left_path, right_path,
7516 &right_key,
7517 BTRFS_COMPARE_TREE_DELETED,
7518 sctx);
7519 advance_right = ADVANCE;
7520 } else {
7521 enum btrfs_compare_tree_result result;
7522
7523 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
7524 ret = tree_compare_item(left_path, right_path,
7525 tmp_buf);
7526 if (ret)
7527 result = BTRFS_COMPARE_TREE_CHANGED;
7528 else
7529 result = BTRFS_COMPARE_TREE_SAME;
7530 ret = changed_cb(left_path, right_path,
7531 &left_key, result, sctx);
7532 advance_left = ADVANCE;
7533 advance_right = ADVANCE;
7534 }
7535
7536 if (ret < 0)
7537 goto out;
7538 down_read(&fs_info->commit_root_sem);
7539 } else if (left_level == right_level) {
7540 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
7541 if (cmp < 0) {
7542 advance_left = ADVANCE;
7543 } else if (cmp > 0) {
7544 advance_right = ADVANCE;
7545 } else {
7546 left_blockptr = btrfs_node_blockptr(
7547 left_path->nodes[left_level],
7548 left_path->slots[left_level]);
7549 right_blockptr = btrfs_node_blockptr(
7550 right_path->nodes[right_level],
7551 right_path->slots[right_level]);
7552 left_gen = btrfs_node_ptr_generation(
7553 left_path->nodes[left_level],
7554 left_path->slots[left_level]);
7555 right_gen = btrfs_node_ptr_generation(
7556 right_path->nodes[right_level],
7557 right_path->slots[right_level]);
7558 if (left_blockptr == right_blockptr &&
7559 left_gen == right_gen) {
7560
7561
7562
7563
7564 advance_left = ADVANCE_ONLY_NEXT;
7565 advance_right = ADVANCE_ONLY_NEXT;
7566 } else {
7567 advance_left = ADVANCE;
7568 advance_right = ADVANCE;
7569 }
7570 }
7571 } else if (left_level < right_level) {
7572 advance_right = ADVANCE;
7573 } else {
7574 advance_left = ADVANCE;
7575 }
7576 }
7577
7578 out_unlock:
7579 up_read(&fs_info->commit_root_sem);
7580 out:
7581 btrfs_free_path(left_path);
7582 btrfs_free_path(right_path);
7583 kvfree(tmp_buf);
7584 return ret;
7585 }
7586
7587 static int send_subvol(struct send_ctx *sctx)
7588 {
7589 int ret;
7590
7591 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
7592 ret = send_header(sctx);
7593 if (ret < 0)
7594 goto out;
7595 }
7596
7597 ret = send_subvol_begin(sctx);
7598 if (ret < 0)
7599 goto out;
7600
7601 if (sctx->parent_root) {
7602 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, sctx);
7603 if (ret < 0)
7604 goto out;
7605 ret = finish_inode_if_needed(sctx, 1);
7606 if (ret < 0)
7607 goto out;
7608 } else {
7609 ret = full_send_tree(sctx);
7610 if (ret < 0)
7611 goto out;
7612 }
7613
7614 out:
7615 free_recorded_refs(sctx);
7616 return ret;
7617 }
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632 static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
7633 {
7634 int i;
7635 struct btrfs_trans_handle *trans = NULL;
7636
7637 again:
7638 if (sctx->parent_root &&
7639 sctx->parent_root->node != sctx->parent_root->commit_root)
7640 goto commit_trans;
7641
7642 for (i = 0; i < sctx->clone_roots_cnt; i++)
7643 if (sctx->clone_roots[i].root->node !=
7644 sctx->clone_roots[i].root->commit_root)
7645 goto commit_trans;
7646
7647 if (trans)
7648 return btrfs_end_transaction(trans);
7649
7650 return 0;
7651
7652 commit_trans:
7653
7654 if (!trans) {
7655 trans = btrfs_join_transaction(sctx->send_root);
7656 if (IS_ERR(trans))
7657 return PTR_ERR(trans);
7658 goto again;
7659 }
7660
7661 return btrfs_commit_transaction(trans);
7662 }
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672 static int flush_delalloc_roots(struct send_ctx *sctx)
7673 {
7674 struct btrfs_root *root = sctx->parent_root;
7675 int ret;
7676 int i;
7677
7678 if (root) {
7679 ret = btrfs_start_delalloc_snapshot(root, false);
7680 if (ret)
7681 return ret;
7682 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7683 }
7684
7685 for (i = 0; i < sctx->clone_roots_cnt; i++) {
7686 root = sctx->clone_roots[i].root;
7687 ret = btrfs_start_delalloc_snapshot(root, false);
7688 if (ret)
7689 return ret;
7690 btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX);
7691 }
7692
7693 return 0;
7694 }
7695
7696 static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
7697 {
7698 spin_lock(&root->root_item_lock);
7699 root->send_in_progress--;
7700
7701
7702
7703
7704 if (root->send_in_progress < 0)
7705 btrfs_err(root->fs_info,
7706 "send_in_progress unbalanced %d root %llu",
7707 root->send_in_progress, root->root_key.objectid);
7708 spin_unlock(&root->root_item_lock);
7709 }
7710
7711 static void dedupe_in_progress_warn(const struct btrfs_root *root)
7712 {
7713 btrfs_warn_rl(root->fs_info,
7714 "cannot use root %llu for send while deduplications on it are in progress (%d in progress)",
7715 root->root_key.objectid, root->dedupe_in_progress);
7716 }
7717
7718 long btrfs_ioctl_send(struct inode *inode, struct btrfs_ioctl_send_args *arg)
7719 {
7720 int ret = 0;
7721 struct btrfs_root *send_root = BTRFS_I(inode)->root;
7722 struct btrfs_fs_info *fs_info = send_root->fs_info;
7723 struct btrfs_root *clone_root;
7724 struct send_ctx *sctx = NULL;
7725 u32 i;
7726 u64 *clone_sources_tmp = NULL;
7727 int clone_sources_to_rollback = 0;
7728 size_t alloc_size;
7729 int sort_clone_roots = 0;
7730
7731 if (!capable(CAP_SYS_ADMIN))
7732 return -EPERM;
7733
7734
7735
7736
7737
7738 spin_lock(&send_root->root_item_lock);
7739 if (btrfs_root_readonly(send_root) && send_root->dedupe_in_progress) {
7740 dedupe_in_progress_warn(send_root);
7741 spin_unlock(&send_root->root_item_lock);
7742 return -EAGAIN;
7743 }
7744 send_root->send_in_progress++;
7745 spin_unlock(&send_root->root_item_lock);
7746
7747
7748
7749
7750
7751 if (!btrfs_root_readonly(send_root)) {
7752 ret = -EPERM;
7753 goto out;
7754 }
7755
7756
7757
7758
7759
7760
7761 if (arg->clone_sources_count >
7762 ULONG_MAX / sizeof(struct clone_root) - 1) {
7763 ret = -EINVAL;
7764 goto out;
7765 }
7766
7767 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
7768 ret = -EINVAL;
7769 goto out;
7770 }
7771
7772 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
7773 if (!sctx) {
7774 ret = -ENOMEM;
7775 goto out;
7776 }
7777
7778 INIT_LIST_HEAD(&sctx->new_refs);
7779 INIT_LIST_HEAD(&sctx->deleted_refs);
7780 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
7781 INIT_LIST_HEAD(&sctx->name_cache_list);
7782
7783 sctx->flags = arg->flags;
7784
7785 if (arg->flags & BTRFS_SEND_FLAG_VERSION) {
7786 if (arg->version > BTRFS_SEND_STREAM_VERSION) {
7787 ret = -EPROTO;
7788 goto out;
7789 }
7790
7791 sctx->proto = arg->version ?: BTRFS_SEND_STREAM_VERSION;
7792 } else {
7793 sctx->proto = 1;
7794 }
7795 if ((arg->flags & BTRFS_SEND_FLAG_COMPRESSED) && sctx->proto < 2) {
7796 ret = -EINVAL;
7797 goto out;
7798 }
7799
7800 sctx->send_filp = fget(arg->send_fd);
7801 if (!sctx->send_filp) {
7802 ret = -EBADF;
7803 goto out;
7804 }
7805
7806 sctx->send_root = send_root;
7807
7808
7809
7810
7811 if (btrfs_root_dead(sctx->send_root)) {
7812 ret = -EPERM;
7813 goto out;
7814 }
7815
7816 sctx->clone_roots_cnt = arg->clone_sources_count;
7817
7818 if (sctx->proto >= 2) {
7819 u32 send_buf_num_pages;
7820
7821 sctx->send_max_size = ALIGN(SZ_16K + BTRFS_MAX_COMPRESSED, PAGE_SIZE);
7822 sctx->send_buf = vmalloc(sctx->send_max_size);
7823 if (!sctx->send_buf) {
7824 ret = -ENOMEM;
7825 goto out;
7826 }
7827 send_buf_num_pages = sctx->send_max_size >> PAGE_SHIFT;
7828 sctx->send_buf_pages = kcalloc(send_buf_num_pages,
7829 sizeof(*sctx->send_buf_pages),
7830 GFP_KERNEL);
7831 if (!sctx->send_buf_pages) {
7832 ret = -ENOMEM;
7833 goto out;
7834 }
7835 for (i = 0; i < send_buf_num_pages; i++) {
7836 sctx->send_buf_pages[i] =
7837 vmalloc_to_page(sctx->send_buf + (i << PAGE_SHIFT));
7838 }
7839 } else {
7840 sctx->send_max_size = BTRFS_SEND_BUF_SIZE_V1;
7841 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
7842 }
7843 if (!sctx->send_buf) {
7844 ret = -ENOMEM;
7845 goto out;
7846 }
7847
7848 sctx->pending_dir_moves = RB_ROOT;
7849 sctx->waiting_dir_moves = RB_ROOT;
7850 sctx->orphan_dirs = RB_ROOT;
7851 sctx->rbtree_new_refs = RB_ROOT;
7852 sctx->rbtree_deleted_refs = RB_ROOT;
7853
7854 sctx->clone_roots = kvcalloc(sizeof(*sctx->clone_roots),
7855 arg->clone_sources_count + 1,
7856 GFP_KERNEL);
7857 if (!sctx->clone_roots) {
7858 ret = -ENOMEM;
7859 goto out;
7860 }
7861
7862 alloc_size = array_size(sizeof(*arg->clone_sources),
7863 arg->clone_sources_count);
7864
7865 if (arg->clone_sources_count) {
7866 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
7867 if (!clone_sources_tmp) {
7868 ret = -ENOMEM;
7869 goto out;
7870 }
7871
7872 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
7873 alloc_size);
7874 if (ret) {
7875 ret = -EFAULT;
7876 goto out;
7877 }
7878
7879 for (i = 0; i < arg->clone_sources_count; i++) {
7880 clone_root = btrfs_get_fs_root(fs_info,
7881 clone_sources_tmp[i], true);
7882 if (IS_ERR(clone_root)) {
7883 ret = PTR_ERR(clone_root);
7884 goto out;
7885 }
7886 spin_lock(&clone_root->root_item_lock);
7887 if (!btrfs_root_readonly(clone_root) ||
7888 btrfs_root_dead(clone_root)) {
7889 spin_unlock(&clone_root->root_item_lock);
7890 btrfs_put_root(clone_root);
7891 ret = -EPERM;
7892 goto out;
7893 }
7894 if (clone_root->dedupe_in_progress) {
7895 dedupe_in_progress_warn(clone_root);
7896 spin_unlock(&clone_root->root_item_lock);
7897 btrfs_put_root(clone_root);
7898 ret = -EAGAIN;
7899 goto out;
7900 }
7901 clone_root->send_in_progress++;
7902 spin_unlock(&clone_root->root_item_lock);
7903
7904 sctx->clone_roots[i].root = clone_root;
7905 clone_sources_to_rollback = i + 1;
7906 }
7907 kvfree(clone_sources_tmp);
7908 clone_sources_tmp = NULL;
7909 }
7910
7911 if (arg->parent_root) {
7912 sctx->parent_root = btrfs_get_fs_root(fs_info, arg->parent_root,
7913 true);
7914 if (IS_ERR(sctx->parent_root)) {
7915 ret = PTR_ERR(sctx->parent_root);
7916 goto out;
7917 }
7918
7919 spin_lock(&sctx->parent_root->root_item_lock);
7920 sctx->parent_root->send_in_progress++;
7921 if (!btrfs_root_readonly(sctx->parent_root) ||
7922 btrfs_root_dead(sctx->parent_root)) {
7923 spin_unlock(&sctx->parent_root->root_item_lock);
7924 ret = -EPERM;
7925 goto out;
7926 }
7927 if (sctx->parent_root->dedupe_in_progress) {
7928 dedupe_in_progress_warn(sctx->parent_root);
7929 spin_unlock(&sctx->parent_root->root_item_lock);
7930 ret = -EAGAIN;
7931 goto out;
7932 }
7933 spin_unlock(&sctx->parent_root->root_item_lock);
7934 }
7935
7936
7937
7938
7939
7940
7941 sctx->clone_roots[sctx->clone_roots_cnt++].root =
7942 btrfs_grab_root(sctx->send_root);
7943
7944
7945 sort(sctx->clone_roots, sctx->clone_roots_cnt,
7946 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
7947 NULL);
7948 sort_clone_roots = 1;
7949
7950 ret = flush_delalloc_roots(sctx);
7951 if (ret)
7952 goto out;
7953
7954 ret = ensure_commit_roots_uptodate(sctx);
7955 if (ret)
7956 goto out;
7957
7958 ret = send_subvol(sctx);
7959 if (ret < 0)
7960 goto out;
7961
7962 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
7963 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
7964 if (ret < 0)
7965 goto out;
7966 ret = send_cmd(sctx);
7967 if (ret < 0)
7968 goto out;
7969 }
7970
7971 out:
7972 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
7973 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
7974 struct rb_node *n;
7975 struct pending_dir_move *pm;
7976
7977 n = rb_first(&sctx->pending_dir_moves);
7978 pm = rb_entry(n, struct pending_dir_move, node);
7979 while (!list_empty(&pm->list)) {
7980 struct pending_dir_move *pm2;
7981
7982 pm2 = list_first_entry(&pm->list,
7983 struct pending_dir_move, list);
7984 free_pending_move(sctx, pm2);
7985 }
7986 free_pending_move(sctx, pm);
7987 }
7988
7989 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
7990 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
7991 struct rb_node *n;
7992 struct waiting_dir_move *dm;
7993
7994 n = rb_first(&sctx->waiting_dir_moves);
7995 dm = rb_entry(n, struct waiting_dir_move, node);
7996 rb_erase(&dm->node, &sctx->waiting_dir_moves);
7997 kfree(dm);
7998 }
7999
8000 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
8001 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
8002 struct rb_node *n;
8003 struct orphan_dir_info *odi;
8004
8005 n = rb_first(&sctx->orphan_dirs);
8006 odi = rb_entry(n, struct orphan_dir_info, node);
8007 free_orphan_dir_info(sctx, odi);
8008 }
8009
8010 if (sort_clone_roots) {
8011 for (i = 0; i < sctx->clone_roots_cnt; i++) {
8012 btrfs_root_dec_send_in_progress(
8013 sctx->clone_roots[i].root);
8014 btrfs_put_root(sctx->clone_roots[i].root);
8015 }
8016 } else {
8017 for (i = 0; sctx && i < clone_sources_to_rollback; i++) {
8018 btrfs_root_dec_send_in_progress(
8019 sctx->clone_roots[i].root);
8020 btrfs_put_root(sctx->clone_roots[i].root);
8021 }
8022
8023 btrfs_root_dec_send_in_progress(send_root);
8024 }
8025 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) {
8026 btrfs_root_dec_send_in_progress(sctx->parent_root);
8027 btrfs_put_root(sctx->parent_root);
8028 }
8029
8030 kvfree(clone_sources_tmp);
8031
8032 if (sctx) {
8033 if (sctx->send_filp)
8034 fput(sctx->send_filp);
8035
8036 kvfree(sctx->clone_roots);
8037 kfree(sctx->send_buf_pages);
8038 kvfree(sctx->send_buf);
8039
8040 name_cache_free(sctx);
8041
8042 close_current_inode(sctx);
8043
8044 kfree(sctx);
8045 }
8046
8047 return ret;
8048 }