blob: 84cb6e5ef36c291070527e3755027b24a9d02e10 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2012 Alexander Block. All rights reserved.
4 */
5
6#include <linux/bsearch.h>
7#include <linux/fs.h>
8#include <linux/file.h>
9#include <linux/sort.h>
10#include <linux/mount.h>
11#include <linux/xattr.h>
12#include <linux/posix_acl_xattr.h>
13#include <linux/radix-tree.h>
14#include <linux/vmalloc.h>
15#include <linux/string.h>
16#include <linux/compat.h>
17#include <linux/crc32c.h>
18
19#include "send.h"
20#include "backref.h"
21#include "locking.h"
22#include "disk-io.h"
23#include "btrfs_inode.h"
24#include "transaction.h"
25#include "compression.h"
26
27/*
28 * A fs_path is a helper to dynamically build path names with unknown size.
29 * It reallocates the internal buffer on demand.
30 * It allows fast adding of path elements on the right side (normal path) and
31 * fast adding to the left side (reversed path). A reversed path can also be
32 * unreversed if needed.
33 */
34struct fs_path {
35 union {
36 struct {
37 char *start;
38 char *end;
39
40 char *buf;
41 unsigned short buf_len:15;
42 unsigned short reversed:1;
43 char inline_buf[];
44 };
45 /*
46 * Average path length does not exceed 200 bytes, we'll have
47 * better packing in the slab and higher chance to satisfy
48 * a allocation later during send.
49 */
50 char pad[256];
51 };
52};
53#define FS_PATH_INLINE_SIZE \
54 (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf))
55
56
57/* reused for each extent */
58struct clone_root {
59 struct btrfs_root *root;
60 u64 ino;
61 u64 offset;
62
63 u64 found_refs;
64};
65
66#define SEND_CTX_MAX_NAME_CACHE_SIZE 128
67#define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2)
68
69struct send_ctx {
70 struct file *send_filp;
71 loff_t send_off;
72 char *send_buf;
73 u32 send_size;
74 u32 send_max_size;
75 u64 total_send_size;
76 u64 cmd_send_size[BTRFS_SEND_C_MAX + 1];
77 u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */
78
79 struct btrfs_root *send_root;
80 struct btrfs_root *parent_root;
81 struct clone_root *clone_roots;
82 int clone_roots_cnt;
83
84 /* current state of the compare_tree call */
85 struct btrfs_path *left_path;
86 struct btrfs_path *right_path;
87 struct btrfs_key *cmp_key;
88
89 /*
90 * infos of the currently processed inode. In case of deleted inodes,
91 * these are the values from the deleted inode.
92 */
93 u64 cur_ino;
94 u64 cur_inode_gen;
95 int cur_inode_new;
96 int cur_inode_new_gen;
97 int cur_inode_deleted;
98 u64 cur_inode_size;
99 u64 cur_inode_mode;
100 u64 cur_inode_rdev;
101 u64 cur_inode_last_extent;
102 u64 cur_inode_next_write_offset;
103 bool ignore_cur_inode;
104
105 u64 send_progress;
106
107 struct list_head new_refs;
108 struct list_head deleted_refs;
109
110 struct radix_tree_root name_cache;
111 struct list_head name_cache_list;
112 int name_cache_size;
113
114 struct file_ra_state ra;
115
116 char *read_buf;
117
118 /*
119 * We process inodes by their increasing order, so if before an
120 * incremental send we reverse the parent/child relationship of
121 * directories such that a directory with a lower inode number was
122 * the parent of a directory with a higher inode number, and the one
123 * becoming the new parent got renamed too, we can't rename/move the
124 * directory with lower inode number when we finish processing it - we
125 * must process the directory with higher inode number first, then
126 * rename/move it and then rename/move the directory with lower inode
127 * number. Example follows.
128 *
129 * Tree state when the first send was performed:
130 *
131 * .
132 * |-- a (ino 257)
133 * |-- b (ino 258)
134 * |
135 * |
136 * |-- c (ino 259)
137 * | |-- d (ino 260)
138 * |
139 * |-- c2 (ino 261)
140 *
141 * Tree state when the second (incremental) send is performed:
142 *
143 * .
144 * |-- a (ino 257)
145 * |-- b (ino 258)
146 * |-- c2 (ino 261)
147 * |-- d2 (ino 260)
148 * |-- cc (ino 259)
149 *
150 * The sequence of steps that lead to the second state was:
151 *
152 * mv /a/b/c/d /a/b/c2/d2
153 * mv /a/b/c /a/b/c2/d2/cc
154 *
155 * "c" has lower inode number, but we can't move it (2nd mv operation)
156 * before we move "d", which has higher inode number.
157 *
158 * So we just memorize which move/rename operations must be performed
159 * later when their respective parent is processed and moved/renamed.
160 */
161
162 /* Indexed by parent directory inode number. */
163 struct rb_root pending_dir_moves;
164
165 /*
166 * Reverse index, indexed by the inode number of a directory that
167 * is waiting for the move/rename of its immediate parent before its
168 * own move/rename can be performed.
169 */
170 struct rb_root waiting_dir_moves;
171
172 /*
173 * A directory that is going to be rm'ed might have a child directory
174 * which is in the pending directory moves index above. In this case,
175 * the directory can only be removed after the move/rename of its child
176 * is performed. Example:
177 *
178 * Parent snapshot:
179 *
180 * . (ino 256)
181 * |-- a/ (ino 257)
182 * |-- b/ (ino 258)
183 * |-- c/ (ino 259)
184 * | |-- x/ (ino 260)
185 * |
186 * |-- y/ (ino 261)
187 *
188 * Send snapshot:
189 *
190 * . (ino 256)
191 * |-- a/ (ino 257)
192 * |-- b/ (ino 258)
193 * |-- YY/ (ino 261)
194 * |-- x/ (ino 260)
195 *
196 * Sequence of steps that lead to the send snapshot:
197 * rm -f /a/b/c/foo.txt
198 * mv /a/b/y /a/b/YY
199 * mv /a/b/c/x /a/b/YY
200 * rmdir /a/b/c
201 *
202 * When the child is processed, its move/rename is delayed until its
203 * parent is processed (as explained above), but all other operations
204 * like update utimes, chown, chgrp, etc, are performed and the paths
205 * that it uses for those operations must use the orphanized name of
206 * its parent (the directory we're going to rm later), so we need to
207 * memorize that name.
208 *
209 * Indexed by the inode number of the directory to be deleted.
210 */
211 struct rb_root orphan_dirs;
212};
213
214struct pending_dir_move {
215 struct rb_node node;
216 struct list_head list;
217 u64 parent_ino;
218 u64 ino;
219 u64 gen;
220 struct list_head update_refs;
221};
222
223struct waiting_dir_move {
224 struct rb_node node;
225 u64 ino;
226 /*
227 * There might be some directory that could not be removed because it
228 * was waiting for this directory inode to be moved first. Therefore
229 * after this directory is moved, we can try to rmdir the ino rmdir_ino.
230 */
231 u64 rmdir_ino;
232 bool orphanized;
233};
234
235struct orphan_dir_info {
236 struct rb_node node;
237 u64 ino;
238 u64 gen;
239 u64 last_dir_index_offset;
240};
241
242struct name_cache_entry {
243 struct list_head list;
244 /*
245 * radix_tree has only 32bit entries but we need to handle 64bit inums.
246 * We use the lower 32bit of the 64bit inum to store it in the tree. If
247 * more then one inum would fall into the same entry, we use radix_list
248 * to store the additional entries. radix_list is also used to store
249 * entries where two entries have the same inum but different
250 * generations.
251 */
252 struct list_head radix_list;
253 u64 ino;
254 u64 gen;
255 u64 parent_ino;
256 u64 parent_gen;
257 int ret;
258 int need_later_update;
259 int name_len;
260 char name[];
261};
262
263__cold
264static void inconsistent_snapshot_error(struct send_ctx *sctx,
265 enum btrfs_compare_tree_result result,
266 const char *what)
267{
268 const char *result_string;
269
270 switch (result) {
271 case BTRFS_COMPARE_TREE_NEW:
272 result_string = "new";
273 break;
274 case BTRFS_COMPARE_TREE_DELETED:
275 result_string = "deleted";
276 break;
277 case BTRFS_COMPARE_TREE_CHANGED:
278 result_string = "updated";
279 break;
280 case BTRFS_COMPARE_TREE_SAME:
281 ASSERT(0);
282 result_string = "unchanged";
283 break;
284 default:
285 ASSERT(0);
286 result_string = "unexpected";
287 }
288
289 btrfs_err(sctx->send_root->fs_info,
290 "Send: inconsistent snapshot, found %s %s for inode %llu without updated inode item, send root is %llu, parent root is %llu",
291 result_string, what, sctx->cmp_key->objectid,
292 sctx->send_root->root_key.objectid,
293 (sctx->parent_root ?
294 sctx->parent_root->root_key.objectid : 0));
295}
296
297static int is_waiting_for_move(struct send_ctx *sctx, u64 ino);
298
299static struct waiting_dir_move *
300get_waiting_dir_move(struct send_ctx *sctx, u64 ino);
301
302static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino);
303
304static int need_send_hole(struct send_ctx *sctx)
305{
306 return (sctx->parent_root && !sctx->cur_inode_new &&
307 !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted &&
308 S_ISREG(sctx->cur_inode_mode));
309}
310
311static void fs_path_reset(struct fs_path *p)
312{
313 if (p->reversed) {
314 p->start = p->buf + p->buf_len - 1;
315 p->end = p->start;
316 *p->start = 0;
317 } else {
318 p->start = p->buf;
319 p->end = p->start;
320 *p->start = 0;
321 }
322}
323
324static struct fs_path *fs_path_alloc(void)
325{
326 struct fs_path *p;
327
328 p = kmalloc(sizeof(*p), GFP_KERNEL);
329 if (!p)
330 return NULL;
331 p->reversed = 0;
332 p->buf = p->inline_buf;
333 p->buf_len = FS_PATH_INLINE_SIZE;
334 fs_path_reset(p);
335 return p;
336}
337
338static struct fs_path *fs_path_alloc_reversed(void)
339{
340 struct fs_path *p;
341
342 p = fs_path_alloc();
343 if (!p)
344 return NULL;
345 p->reversed = 1;
346 fs_path_reset(p);
347 return p;
348}
349
350static void fs_path_free(struct fs_path *p)
351{
352 if (!p)
353 return;
354 if (p->buf != p->inline_buf)
355 kfree(p->buf);
356 kfree(p);
357}
358
359static int fs_path_len(struct fs_path *p)
360{
361 return p->end - p->start;
362}
363
364static int fs_path_ensure_buf(struct fs_path *p, int len)
365{
366 char *tmp_buf;
367 int path_len;
368 int old_buf_len;
369
370 len++;
371
372 if (p->buf_len >= len)
373 return 0;
374
375 if (len > PATH_MAX) {
376 WARN_ON(1);
377 return -ENOMEM;
378 }
379
380 path_len = p->end - p->start;
381 old_buf_len = p->buf_len;
382
383 /*
384 * First time the inline_buf does not suffice
385 */
386 if (p->buf == p->inline_buf) {
387 tmp_buf = kmalloc(len, GFP_KERNEL);
388 if (tmp_buf)
389 memcpy(tmp_buf, p->buf, old_buf_len);
390 } else {
391 tmp_buf = krealloc(p->buf, len, GFP_KERNEL);
392 }
393 if (!tmp_buf)
394 return -ENOMEM;
395 p->buf = tmp_buf;
396 /*
397 * The real size of the buffer is bigger, this will let the fast path
398 * happen most of the time
399 */
400 p->buf_len = ksize(p->buf);
401
402 if (p->reversed) {
403 tmp_buf = p->buf + old_buf_len - path_len - 1;
404 p->end = p->buf + p->buf_len - 1;
405 p->start = p->end - path_len;
406 memmove(p->start, tmp_buf, path_len + 1);
407 } else {
408 p->start = p->buf;
409 p->end = p->start + path_len;
410 }
411 return 0;
412}
413
414static int fs_path_prepare_for_add(struct fs_path *p, int name_len,
415 char **prepared)
416{
417 int ret;
418 int new_len;
419
420 new_len = p->end - p->start + name_len;
421 if (p->start != p->end)
422 new_len++;
423 ret = fs_path_ensure_buf(p, new_len);
424 if (ret < 0)
425 goto out;
426
427 if (p->reversed) {
428 if (p->start != p->end)
429 *--p->start = '/';
430 p->start -= name_len;
431 *prepared = p->start;
432 } else {
433 if (p->start != p->end)
434 *p->end++ = '/';
435 *prepared = p->end;
436 p->end += name_len;
437 *p->end = 0;
438 }
439
440out:
441 return ret;
442}
443
444static int fs_path_add(struct fs_path *p, const char *name, int name_len)
445{
446 int ret;
447 char *prepared;
448
449 ret = fs_path_prepare_for_add(p, name_len, &prepared);
450 if (ret < 0)
451 goto out;
452 memcpy(prepared, name, name_len);
453
454out:
455 return ret;
456}
457
458static int fs_path_add_path(struct fs_path *p, struct fs_path *p2)
459{
460 int ret;
461 char *prepared;
462
463 ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared);
464 if (ret < 0)
465 goto out;
466 memcpy(prepared, p2->start, p2->end - p2->start);
467
468out:
469 return ret;
470}
471
472static int fs_path_add_from_extent_buffer(struct fs_path *p,
473 struct extent_buffer *eb,
474 unsigned long off, int len)
475{
476 int ret;
477 char *prepared;
478
479 ret = fs_path_prepare_for_add(p, len, &prepared);
480 if (ret < 0)
481 goto out;
482
483 read_extent_buffer(eb, prepared, off, len);
484
485out:
486 return ret;
487}
488
489static int fs_path_copy(struct fs_path *p, struct fs_path *from)
490{
491 int ret;
492
493 p->reversed = from->reversed;
494 fs_path_reset(p);
495
496 ret = fs_path_add_path(p, from);
497
498 return ret;
499}
500
501
502static void fs_path_unreverse(struct fs_path *p)
503{
504 char *tmp;
505 int len;
506
507 if (!p->reversed)
508 return;
509
510 tmp = p->start;
511 len = p->end - p->start;
512 p->start = p->buf;
513 p->end = p->start + len;
514 memmove(p->start, tmp, len + 1);
515 p->reversed = 0;
516}
517
518static struct btrfs_path *alloc_path_for_send(void)
519{
520 struct btrfs_path *path;
521
522 path = btrfs_alloc_path();
523 if (!path)
524 return NULL;
525 path->search_commit_root = 1;
526 path->skip_locking = 1;
527 path->need_commit_sem = 1;
528 return path;
529}
530
531static int write_buf(struct file *filp, const void *buf, u32 len, loff_t *off)
532{
533 int ret;
534 u32 pos = 0;
535
536 while (pos < len) {
537 ret = kernel_write(filp, buf + pos, len - pos, off);
538 /* TODO handle that correctly */
539 /*if (ret == -ERESTARTSYS) {
540 continue;
541 }*/
542 if (ret < 0)
543 return ret;
544 if (ret == 0) {
545 return -EIO;
546 }
547 pos += ret;
548 }
549
550 return 0;
551}
552
553static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len)
554{
555 struct btrfs_tlv_header *hdr;
556 int total_len = sizeof(*hdr) + len;
557 int left = sctx->send_max_size - sctx->send_size;
558
559 if (unlikely(left < total_len))
560 return -EOVERFLOW;
561
562 hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size);
563 hdr->tlv_type = cpu_to_le16(attr);
564 hdr->tlv_len = cpu_to_le16(len);
565 memcpy(hdr + 1, data, len);
566 sctx->send_size += total_len;
567
568 return 0;
569}
570
571#define TLV_PUT_DEFINE_INT(bits) \
572 static int tlv_put_u##bits(struct send_ctx *sctx, \
573 u##bits attr, u##bits value) \
574 { \
575 __le##bits __tmp = cpu_to_le##bits(value); \
576 return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \
577 }
578
579TLV_PUT_DEFINE_INT(64)
580
581static int tlv_put_string(struct send_ctx *sctx, u16 attr,
582 const char *str, int len)
583{
584 if (len == -1)
585 len = strlen(str);
586 return tlv_put(sctx, attr, str, len);
587}
588
589static int tlv_put_uuid(struct send_ctx *sctx, u16 attr,
590 const u8 *uuid)
591{
592 return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE);
593}
594
595static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr,
596 struct extent_buffer *eb,
597 struct btrfs_timespec *ts)
598{
599 struct btrfs_timespec bts;
600 read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts));
601 return tlv_put(sctx, attr, &bts, sizeof(bts));
602}
603
604
605#define TLV_PUT(sctx, attrtype, data, attrlen) \
606 do { \
607 ret = tlv_put(sctx, attrtype, data, attrlen); \
608 if (ret < 0) \
609 goto tlv_put_failure; \
610 } while (0)
611
612#define TLV_PUT_INT(sctx, attrtype, bits, value) \
613 do { \
614 ret = tlv_put_u##bits(sctx, attrtype, value); \
615 if (ret < 0) \
616 goto tlv_put_failure; \
617 } while (0)
618
619#define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data)
620#define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data)
621#define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data)
622#define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data)
623#define TLV_PUT_STRING(sctx, attrtype, str, len) \
624 do { \
625 ret = tlv_put_string(sctx, attrtype, str, len); \
626 if (ret < 0) \
627 goto tlv_put_failure; \
628 } while (0)
629#define TLV_PUT_PATH(sctx, attrtype, p) \
630 do { \
631 ret = tlv_put_string(sctx, attrtype, p->start, \
632 p->end - p->start); \
633 if (ret < 0) \
634 goto tlv_put_failure; \
635 } while(0)
636#define TLV_PUT_UUID(sctx, attrtype, uuid) \
637 do { \
638 ret = tlv_put_uuid(sctx, attrtype, uuid); \
639 if (ret < 0) \
640 goto tlv_put_failure; \
641 } while (0)
642#define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \
643 do { \
644 ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \
645 if (ret < 0) \
646 goto tlv_put_failure; \
647 } while (0)
648
649static int send_header(struct send_ctx *sctx)
650{
651 struct btrfs_stream_header hdr;
652
653 strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC);
654 hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION);
655
656 return write_buf(sctx->send_filp, &hdr, sizeof(hdr),
657 &sctx->send_off);
658}
659
660/*
661 * For each command/item we want to send to userspace, we call this function.
662 */
663static int begin_cmd(struct send_ctx *sctx, int cmd)
664{
665 struct btrfs_cmd_header *hdr;
666
667 if (WARN_ON(!sctx->send_buf))
668 return -EINVAL;
669
670 BUG_ON(sctx->send_size);
671
672 sctx->send_size += sizeof(*hdr);
673 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
674 hdr->cmd = cpu_to_le16(cmd);
675
676 return 0;
677}
678
679static int send_cmd(struct send_ctx *sctx)
680{
681 int ret;
682 struct btrfs_cmd_header *hdr;
683 u32 crc;
684
685 hdr = (struct btrfs_cmd_header *)sctx->send_buf;
686 hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
687 hdr->crc = 0;
688
689 crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
690 hdr->crc = cpu_to_le32(crc);
691
692 ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
693 &sctx->send_off);
694
695 sctx->total_send_size += sctx->send_size;
696 sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size;
697 sctx->send_size = 0;
698
699 return ret;
700}
701
702/*
703 * Sends a move instruction to user space
704 */
705static int send_rename(struct send_ctx *sctx,
706 struct fs_path *from, struct fs_path *to)
707{
708 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
709 int ret;
710
711 btrfs_debug(fs_info, "send_rename %s -> %s", from->start, to->start);
712
713 ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME);
714 if (ret < 0)
715 goto out;
716
717 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from);
718 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to);
719
720 ret = send_cmd(sctx);
721
722tlv_put_failure:
723out:
724 return ret;
725}
726
727/*
728 * Sends a link instruction to user space
729 */
730static int send_link(struct send_ctx *sctx,
731 struct fs_path *path, struct fs_path *lnk)
732{
733 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
734 int ret;
735
736 btrfs_debug(fs_info, "send_link %s -> %s", path->start, lnk->start);
737
738 ret = begin_cmd(sctx, BTRFS_SEND_C_LINK);
739 if (ret < 0)
740 goto out;
741
742 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
743 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk);
744
745 ret = send_cmd(sctx);
746
747tlv_put_failure:
748out:
749 return ret;
750}
751
752/*
753 * Sends an unlink instruction to user space
754 */
755static int send_unlink(struct send_ctx *sctx, struct fs_path *path)
756{
757 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
758 int ret;
759
760 btrfs_debug(fs_info, "send_unlink %s", path->start);
761
762 ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK);
763 if (ret < 0)
764 goto out;
765
766 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
767
768 ret = send_cmd(sctx);
769
770tlv_put_failure:
771out:
772 return ret;
773}
774
775/*
776 * Sends a rmdir instruction to user space
777 */
778static int send_rmdir(struct send_ctx *sctx, struct fs_path *path)
779{
780 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
781 int ret;
782
783 btrfs_debug(fs_info, "send_rmdir %s", path->start);
784
785 ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR);
786 if (ret < 0)
787 goto out;
788
789 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
790
791 ret = send_cmd(sctx);
792
793tlv_put_failure:
794out:
795 return ret;
796}
797
798/*
799 * Helper function to retrieve some fields from an inode item.
800 */
801static int __get_inode_info(struct btrfs_root *root, struct btrfs_path *path,
802 u64 ino, u64 *size, u64 *gen, u64 *mode, u64 *uid,
803 u64 *gid, u64 *rdev)
804{
805 int ret;
806 struct btrfs_inode_item *ii;
807 struct btrfs_key key;
808
809 key.objectid = ino;
810 key.type = BTRFS_INODE_ITEM_KEY;
811 key.offset = 0;
812 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
813 if (ret) {
814 if (ret > 0)
815 ret = -ENOENT;
816 return ret;
817 }
818
819 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
820 struct btrfs_inode_item);
821 if (size)
822 *size = btrfs_inode_size(path->nodes[0], ii);
823 if (gen)
824 *gen = btrfs_inode_generation(path->nodes[0], ii);
825 if (mode)
826 *mode = btrfs_inode_mode(path->nodes[0], ii);
827 if (uid)
828 *uid = btrfs_inode_uid(path->nodes[0], ii);
829 if (gid)
830 *gid = btrfs_inode_gid(path->nodes[0], ii);
831 if (rdev)
832 *rdev = btrfs_inode_rdev(path->nodes[0], ii);
833
834 return ret;
835}
836
837static int get_inode_info(struct btrfs_root *root,
838 u64 ino, u64 *size, u64 *gen,
839 u64 *mode, u64 *uid, u64 *gid,
840 u64 *rdev)
841{
842 struct btrfs_path *path;
843 int ret;
844
845 path = alloc_path_for_send();
846 if (!path)
847 return -ENOMEM;
848 ret = __get_inode_info(root, path, ino, size, gen, mode, uid, gid,
849 rdev);
850 btrfs_free_path(path);
851 return ret;
852}
853
854typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index,
855 struct fs_path *p,
856 void *ctx);
857
858/*
859 * Helper function to iterate the entries in ONE btrfs_inode_ref or
860 * btrfs_inode_extref.
861 * The iterate callback may return a non zero value to stop iteration. This can
862 * be a negative value for error codes or 1 to simply stop it.
863 *
864 * path must point to the INODE_REF or INODE_EXTREF when called.
865 */
866static int iterate_inode_ref(struct btrfs_root *root, struct btrfs_path *path,
867 struct btrfs_key *found_key, int resolve,
868 iterate_inode_ref_t iterate, void *ctx)
869{
870 struct extent_buffer *eb = path->nodes[0];
871 struct btrfs_item *item;
872 struct btrfs_inode_ref *iref;
873 struct btrfs_inode_extref *extref;
874 struct btrfs_path *tmp_path;
875 struct fs_path *p;
876 u32 cur = 0;
877 u32 total;
878 int slot = path->slots[0];
879 u32 name_len;
880 char *start;
881 int ret = 0;
882 int num = 0;
883 int index;
884 u64 dir;
885 unsigned long name_off;
886 unsigned long elem_size;
887 unsigned long ptr;
888
889 p = fs_path_alloc_reversed();
890 if (!p)
891 return -ENOMEM;
892
893 tmp_path = alloc_path_for_send();
894 if (!tmp_path) {
895 fs_path_free(p);
896 return -ENOMEM;
897 }
898
899
900 if (found_key->type == BTRFS_INODE_REF_KEY) {
901 ptr = (unsigned long)btrfs_item_ptr(eb, slot,
902 struct btrfs_inode_ref);
903 item = btrfs_item_nr(slot);
904 total = btrfs_item_size(eb, item);
905 elem_size = sizeof(*iref);
906 } else {
907 ptr = btrfs_item_ptr_offset(eb, slot);
908 total = btrfs_item_size_nr(eb, slot);
909 elem_size = sizeof(*extref);
910 }
911
912 while (cur < total) {
913 fs_path_reset(p);
914
915 if (found_key->type == BTRFS_INODE_REF_KEY) {
916 iref = (struct btrfs_inode_ref *)(ptr + cur);
917 name_len = btrfs_inode_ref_name_len(eb, iref);
918 name_off = (unsigned long)(iref + 1);
919 index = btrfs_inode_ref_index(eb, iref);
920 dir = found_key->offset;
921 } else {
922 extref = (struct btrfs_inode_extref *)(ptr + cur);
923 name_len = btrfs_inode_extref_name_len(eb, extref);
924 name_off = (unsigned long)&extref->name;
925 index = btrfs_inode_extref_index(eb, extref);
926 dir = btrfs_inode_extref_parent(eb, extref);
927 }
928
929 if (resolve) {
930 start = btrfs_ref_to_path(root, tmp_path, name_len,
931 name_off, eb, dir,
932 p->buf, p->buf_len);
933 if (IS_ERR(start)) {
934 ret = PTR_ERR(start);
935 goto out;
936 }
937 if (start < p->buf) {
938 /* overflow , try again with larger buffer */
939 ret = fs_path_ensure_buf(p,
940 p->buf_len + p->buf - start);
941 if (ret < 0)
942 goto out;
943 start = btrfs_ref_to_path(root, tmp_path,
944 name_len, name_off,
945 eb, dir,
946 p->buf, p->buf_len);
947 if (IS_ERR(start)) {
948 ret = PTR_ERR(start);
949 goto out;
950 }
951 BUG_ON(start < p->buf);
952 }
953 p->start = start;
954 } else {
955 ret = fs_path_add_from_extent_buffer(p, eb, name_off,
956 name_len);
957 if (ret < 0)
958 goto out;
959 }
960
961 cur += elem_size + name_len;
962 ret = iterate(num, dir, index, p, ctx);
963 if (ret)
964 goto out;
965 num++;
966 }
967
968out:
969 btrfs_free_path(tmp_path);
970 fs_path_free(p);
971 return ret;
972}
973
974typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key,
975 const char *name, int name_len,
976 const char *data, int data_len,
977 u8 type, void *ctx);
978
979/*
980 * Helper function to iterate the entries in ONE btrfs_dir_item.
981 * The iterate callback may return a non zero value to stop iteration. This can
982 * be a negative value for error codes or 1 to simply stop it.
983 *
984 * path must point to the dir item when called.
985 */
986static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
987 iterate_dir_item_t iterate, void *ctx)
988{
989 int ret = 0;
990 struct extent_buffer *eb;
991 struct btrfs_item *item;
992 struct btrfs_dir_item *di;
993 struct btrfs_key di_key;
994 char *buf = NULL;
995 int buf_len;
996 u32 name_len;
997 u32 data_len;
998 u32 cur;
999 u32 len;
1000 u32 total;
1001 int slot;
1002 int num;
1003 u8 type;
1004
1005 /*
1006 * Start with a small buffer (1 page). If later we end up needing more
1007 * space, which can happen for xattrs on a fs with a leaf size greater
1008 * then the page size, attempt to increase the buffer. Typically xattr
1009 * values are small.
1010 */
1011 buf_len = PATH_MAX;
1012 buf = kmalloc(buf_len, GFP_KERNEL);
1013 if (!buf) {
1014 ret = -ENOMEM;
1015 goto out;
1016 }
1017
1018 eb = path->nodes[0];
1019 slot = path->slots[0];
1020 item = btrfs_item_nr(slot);
1021 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1022 cur = 0;
1023 len = 0;
1024 total = btrfs_item_size(eb, item);
1025
1026 num = 0;
1027 while (cur < total) {
1028 name_len = btrfs_dir_name_len(eb, di);
1029 data_len = btrfs_dir_data_len(eb, di);
1030 type = btrfs_dir_type(eb, di);
1031 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
1032
1033 if (type == BTRFS_FT_XATTR) {
1034 if (name_len > XATTR_NAME_MAX) {
1035 ret = -ENAMETOOLONG;
1036 goto out;
1037 }
1038 if (name_len + data_len >
1039 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1040 ret = -E2BIG;
1041 goto out;
1042 }
1043 } else {
1044 /*
1045 * Path too long
1046 */
1047 if (name_len + data_len > PATH_MAX) {
1048 ret = -ENAMETOOLONG;
1049 goto out;
1050 }
1051 }
1052
1053 if (name_len + data_len > buf_len) {
1054 buf_len = name_len + data_len;
1055 if (is_vmalloc_addr(buf)) {
1056 vfree(buf);
1057 buf = NULL;
1058 } else {
1059 char *tmp = krealloc(buf, buf_len,
1060 GFP_KERNEL | __GFP_NOWARN);
1061
1062 if (!tmp)
1063 kfree(buf);
1064 buf = tmp;
1065 }
1066 if (!buf) {
1067 buf = kvmalloc(buf_len, GFP_KERNEL);
1068 if (!buf) {
1069 ret = -ENOMEM;
1070 goto out;
1071 }
1072 }
1073 }
1074
1075 read_extent_buffer(eb, buf, (unsigned long)(di + 1),
1076 name_len + data_len);
1077
1078 len = sizeof(*di) + name_len + data_len;
1079 di = (struct btrfs_dir_item *)((char *)di + len);
1080 cur += len;
1081
1082 ret = iterate(num, &di_key, buf, name_len, buf + name_len,
1083 data_len, type, ctx);
1084 if (ret < 0)
1085 goto out;
1086 if (ret) {
1087 ret = 0;
1088 goto out;
1089 }
1090
1091 num++;
1092 }
1093
1094out:
1095 kvfree(buf);
1096 return ret;
1097}
1098
1099static int __copy_first_ref(int num, u64 dir, int index,
1100 struct fs_path *p, void *ctx)
1101{
1102 int ret;
1103 struct fs_path *pt = ctx;
1104
1105 ret = fs_path_copy(pt, p);
1106 if (ret < 0)
1107 return ret;
1108
1109 /* we want the first only */
1110 return 1;
1111}
1112
1113/*
1114 * Retrieve the first path of an inode. If an inode has more then one
1115 * ref/hardlink, this is ignored.
1116 */
1117static int get_inode_path(struct btrfs_root *root,
1118 u64 ino, struct fs_path *path)
1119{
1120 int ret;
1121 struct btrfs_key key, found_key;
1122 struct btrfs_path *p;
1123
1124 p = alloc_path_for_send();
1125 if (!p)
1126 return -ENOMEM;
1127
1128 fs_path_reset(path);
1129
1130 key.objectid = ino;
1131 key.type = BTRFS_INODE_REF_KEY;
1132 key.offset = 0;
1133
1134 ret = btrfs_search_slot_for_read(root, &key, p, 1, 0);
1135 if (ret < 0)
1136 goto out;
1137 if (ret) {
1138 ret = 1;
1139 goto out;
1140 }
1141 btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]);
1142 if (found_key.objectid != ino ||
1143 (found_key.type != BTRFS_INODE_REF_KEY &&
1144 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1145 ret = -ENOENT;
1146 goto out;
1147 }
1148
1149 ret = iterate_inode_ref(root, p, &found_key, 1,
1150 __copy_first_ref, path);
1151 if (ret < 0)
1152 goto out;
1153 ret = 0;
1154
1155out:
1156 btrfs_free_path(p);
1157 return ret;
1158}
1159
1160struct backref_ctx {
1161 struct send_ctx *sctx;
1162
1163 struct btrfs_path *path;
1164 /* number of total found references */
1165 u64 found;
1166
1167 /*
1168 * used for clones found in send_root. clones found behind cur_objectid
1169 * and cur_offset are not considered as allowed clones.
1170 */
1171 u64 cur_objectid;
1172 u64 cur_offset;
1173
1174 /* may be truncated in case it's the last extent in a file */
1175 u64 extent_len;
1176
1177 /* data offset in the file extent item */
1178 u64 data_offset;
1179
1180 /* Just to check for bugs in backref resolving */
1181 int found_itself;
1182};
1183
1184static int __clone_root_cmp_bsearch(const void *key, const void *elt)
1185{
1186 u64 root = (u64)(uintptr_t)key;
1187 struct clone_root *cr = (struct clone_root *)elt;
1188
1189 if (root < cr->root->objectid)
1190 return -1;
1191 if (root > cr->root->objectid)
1192 return 1;
1193 return 0;
1194}
1195
1196static int __clone_root_cmp_sort(const void *e1, const void *e2)
1197{
1198 struct clone_root *cr1 = (struct clone_root *)e1;
1199 struct clone_root *cr2 = (struct clone_root *)e2;
1200
1201 if (cr1->root->objectid < cr2->root->objectid)
1202 return -1;
1203 if (cr1->root->objectid > cr2->root->objectid)
1204 return 1;
1205 return 0;
1206}
1207
1208/*
1209 * Called for every backref that is found for the current extent.
1210 * Results are collected in sctx->clone_roots->ino/offset/found_refs
1211 */
1212static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_)
1213{
1214 struct backref_ctx *bctx = ctx_;
1215 struct clone_root *found;
1216 int ret;
1217 u64 i_size;
1218
1219 /* First check if the root is in the list of accepted clone sources */
1220 found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots,
1221 bctx->sctx->clone_roots_cnt,
1222 sizeof(struct clone_root),
1223 __clone_root_cmp_bsearch);
1224 if (!found)
1225 return 0;
1226
1227 if (found->root == bctx->sctx->send_root &&
1228 ino == bctx->cur_objectid &&
1229 offset == bctx->cur_offset) {
1230 bctx->found_itself = 1;
1231 }
1232
1233 /*
1234 * There are inodes that have extents that lie behind its i_size. Don't
1235 * accept clones from these extents.
1236 */
1237 ret = __get_inode_info(found->root, bctx->path, ino, &i_size, NULL, NULL,
1238 NULL, NULL, NULL);
1239 btrfs_release_path(bctx->path);
1240 if (ret < 0)
1241 return ret;
1242
1243 if (offset + bctx->data_offset + bctx->extent_len > i_size)
1244 return 0;
1245
1246 /*
1247 * Make sure we don't consider clones from send_root that are
1248 * behind the current inode/offset.
1249 */
1250 if (found->root == bctx->sctx->send_root) {
1251 /*
1252 * TODO for the moment we don't accept clones from the inode
1253 * that is currently send. We may change this when
1254 * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same
1255 * file.
1256 */
1257 if (ino >= bctx->cur_objectid)
1258 return 0;
1259 }
1260
1261 bctx->found++;
1262 found->found_refs++;
1263 if (ino < found->ino) {
1264 found->ino = ino;
1265 found->offset = offset;
1266 } else if (found->ino == ino) {
1267 /*
1268 * same extent found more then once in the same file.
1269 */
1270 if (found->offset > offset + bctx->extent_len)
1271 found->offset = offset;
1272 }
1273
1274 return 0;
1275}
1276
1277/*
1278 * Given an inode, offset and extent item, it finds a good clone for a clone
1279 * instruction. Returns -ENOENT when none could be found. The function makes
1280 * sure that the returned clone is usable at the point where sending is at the
1281 * moment. This means, that no clones are accepted which lie behind the current
1282 * inode+offset.
1283 *
1284 * path must point to the extent item when called.
1285 */
1286static int find_extent_clone(struct send_ctx *sctx,
1287 struct btrfs_path *path,
1288 u64 ino, u64 data_offset,
1289 u64 ino_size,
1290 struct clone_root **found)
1291{
1292 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
1293 int ret;
1294 int extent_type;
1295 u64 logical;
1296 u64 disk_byte;
1297 u64 num_bytes;
1298 u64 extent_item_pos;
1299 u64 flags = 0;
1300 struct btrfs_file_extent_item *fi;
1301 struct extent_buffer *eb = path->nodes[0];
1302 struct backref_ctx *backref_ctx = NULL;
1303 struct clone_root *cur_clone_root;
1304 struct btrfs_key found_key;
1305 struct btrfs_path *tmp_path;
1306 int compressed;
1307 u32 i;
1308
1309 tmp_path = alloc_path_for_send();
1310 if (!tmp_path)
1311 return -ENOMEM;
1312
1313 /* We only use this path under the commit sem */
1314 tmp_path->need_commit_sem = 0;
1315
1316 backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_KERNEL);
1317 if (!backref_ctx) {
1318 ret = -ENOMEM;
1319 goto out;
1320 }
1321
1322 backref_ctx->path = tmp_path;
1323
1324 if (data_offset >= ino_size) {
1325 /*
1326 * There may be extents that lie behind the file's size.
1327 * I at least had this in combination with snapshotting while
1328 * writing large files.
1329 */
1330 ret = 0;
1331 goto out;
1332 }
1333
1334 fi = btrfs_item_ptr(eb, path->slots[0],
1335 struct btrfs_file_extent_item);
1336 extent_type = btrfs_file_extent_type(eb, fi);
1337 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1338 ret = -ENOENT;
1339 goto out;
1340 }
1341 compressed = btrfs_file_extent_compression(eb, fi);
1342
1343 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1344 disk_byte = btrfs_file_extent_disk_bytenr(eb, fi);
1345 if (disk_byte == 0) {
1346 ret = -ENOENT;
1347 goto out;
1348 }
1349 logical = disk_byte + btrfs_file_extent_offset(eb, fi);
1350
1351 down_read(&fs_info->commit_root_sem);
1352 ret = extent_from_logical(fs_info, disk_byte, tmp_path,
1353 &found_key, &flags);
1354 up_read(&fs_info->commit_root_sem);
1355 btrfs_release_path(tmp_path);
1356
1357 if (ret < 0)
1358 goto out;
1359 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1360 ret = -EIO;
1361 goto out;
1362 }
1363
1364 /*
1365 * Setup the clone roots.
1366 */
1367 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1368 cur_clone_root = sctx->clone_roots + i;
1369 cur_clone_root->ino = (u64)-1;
1370 cur_clone_root->offset = 0;
1371 cur_clone_root->found_refs = 0;
1372 }
1373
1374 backref_ctx->sctx = sctx;
1375 backref_ctx->found = 0;
1376 backref_ctx->cur_objectid = ino;
1377 backref_ctx->cur_offset = data_offset;
1378 backref_ctx->found_itself = 0;
1379 backref_ctx->extent_len = num_bytes;
1380 /*
1381 * For non-compressed extents iterate_extent_inodes() gives us extent
1382 * offsets that already take into account the data offset, but not for
1383 * compressed extents, since the offset is logical and not relative to
1384 * the physical extent locations. We must take this into account to
1385 * avoid sending clone offsets that go beyond the source file's size,
1386 * which would result in the clone ioctl failing with -EINVAL on the
1387 * receiving end.
1388 */
1389 if (compressed == BTRFS_COMPRESS_NONE)
1390 backref_ctx->data_offset = 0;
1391 else
1392 backref_ctx->data_offset = btrfs_file_extent_offset(eb, fi);
1393
1394 /*
1395 * The last extent of a file may be too large due to page alignment.
1396 * We need to adjust extent_len in this case so that the checks in
1397 * __iterate_backrefs work.
1398 */
1399 if (data_offset + num_bytes >= ino_size)
1400 backref_ctx->extent_len = ino_size - data_offset;
1401
1402 /*
1403 * Now collect all backrefs.
1404 */
1405 if (compressed == BTRFS_COMPRESS_NONE)
1406 extent_item_pos = logical - found_key.objectid;
1407 else
1408 extent_item_pos = 0;
1409 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1410 extent_item_pos, 1, __iterate_backrefs,
1411 backref_ctx, false);
1412
1413 if (ret < 0)
1414 goto out;
1415
1416 if (!backref_ctx->found_itself) {
1417 /* found a bug in backref code? */
1418 ret = -EIO;
1419 btrfs_err(fs_info,
1420 "did not find backref in send_root. inode=%llu, offset=%llu, disk_byte=%llu found extent=%llu",
1421 ino, data_offset, disk_byte, found_key.objectid);
1422 goto out;
1423 }
1424
1425 btrfs_debug(fs_info,
1426 "find_extent_clone: data_offset=%llu, ino=%llu, num_bytes=%llu, logical=%llu",
1427 data_offset, ino, num_bytes, logical);
1428
1429 if (!backref_ctx->found)
1430 btrfs_debug(fs_info, "no clones found");
1431
1432 cur_clone_root = NULL;
1433 for (i = 0; i < sctx->clone_roots_cnt; i++) {
1434 if (sctx->clone_roots[i].found_refs) {
1435 if (!cur_clone_root)
1436 cur_clone_root = sctx->clone_roots + i;
1437 else if (sctx->clone_roots[i].root == sctx->send_root)
1438 /* prefer clones from send_root over others */
1439 cur_clone_root = sctx->clone_roots + i;
1440 }
1441
1442 }
1443
1444 if (cur_clone_root) {
1445 *found = cur_clone_root;
1446 ret = 0;
1447 } else {
1448 ret = -ENOENT;
1449 }
1450
1451out:
1452 btrfs_free_path(tmp_path);
1453 kfree(backref_ctx);
1454 return ret;
1455}
1456
1457static int read_symlink(struct btrfs_root *root,
1458 u64 ino,
1459 struct fs_path *dest)
1460{
1461 int ret;
1462 struct btrfs_path *path;
1463 struct btrfs_key key;
1464 struct btrfs_file_extent_item *ei;
1465 u8 type;
1466 u8 compression;
1467 unsigned long off;
1468 int len;
1469
1470 path = alloc_path_for_send();
1471 if (!path)
1472 return -ENOMEM;
1473
1474 key.objectid = ino;
1475 key.type = BTRFS_EXTENT_DATA_KEY;
1476 key.offset = 0;
1477 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1478 if (ret < 0)
1479 goto out;
1480 if (ret) {
1481 /*
1482 * An empty symlink inode. Can happen in rare error paths when
1483 * creating a symlink (transaction committed before the inode
1484 * eviction handler removed the symlink inode items and a crash
1485 * happened in between or the subvol was snapshoted in between).
1486 * Print an informative message to dmesg/syslog so that the user
1487 * can delete the symlink.
1488 */
1489 btrfs_err(root->fs_info,
1490 "Found empty symlink inode %llu at root %llu",
1491 ino, root->root_key.objectid);
1492 ret = -EIO;
1493 goto out;
1494 }
1495
1496 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1497 struct btrfs_file_extent_item);
1498 type = btrfs_file_extent_type(path->nodes[0], ei);
1499 compression = btrfs_file_extent_compression(path->nodes[0], ei);
1500 BUG_ON(type != BTRFS_FILE_EXTENT_INLINE);
1501 BUG_ON(compression);
1502
1503 off = btrfs_file_extent_inline_start(ei);
1504 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
1505
1506 ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len);
1507
1508out:
1509 btrfs_free_path(path);
1510 return ret;
1511}
1512
1513/*
1514 * Helper function to generate a file name that is unique in the root of
1515 * send_root and parent_root. This is used to generate names for orphan inodes.
1516 */
1517static int gen_unique_name(struct send_ctx *sctx,
1518 u64 ino, u64 gen,
1519 struct fs_path *dest)
1520{
1521 int ret = 0;
1522 struct btrfs_path *path;
1523 struct btrfs_dir_item *di;
1524 char tmp[64];
1525 int len;
1526 u64 idx = 0;
1527
1528 path = alloc_path_for_send();
1529 if (!path)
1530 return -ENOMEM;
1531
1532 while (1) {
1533 len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu",
1534 ino, gen, idx);
1535 ASSERT(len < sizeof(tmp));
1536
1537 di = btrfs_lookup_dir_item(NULL, sctx->send_root,
1538 path, BTRFS_FIRST_FREE_OBJECTID,
1539 tmp, strlen(tmp), 0);
1540 btrfs_release_path(path);
1541 if (IS_ERR(di)) {
1542 ret = PTR_ERR(di);
1543 goto out;
1544 }
1545 if (di) {
1546 /* not unique, try again */
1547 idx++;
1548 continue;
1549 }
1550
1551 if (!sctx->parent_root) {
1552 /* unique */
1553 ret = 0;
1554 break;
1555 }
1556
1557 di = btrfs_lookup_dir_item(NULL, sctx->parent_root,
1558 path, BTRFS_FIRST_FREE_OBJECTID,
1559 tmp, strlen(tmp), 0);
1560 btrfs_release_path(path);
1561 if (IS_ERR(di)) {
1562 ret = PTR_ERR(di);
1563 goto out;
1564 }
1565 if (di) {
1566 /* not unique, try again */
1567 idx++;
1568 continue;
1569 }
1570 /* unique */
1571 break;
1572 }
1573
1574 ret = fs_path_add(dest, tmp, strlen(tmp));
1575
1576out:
1577 btrfs_free_path(path);
1578 return ret;
1579}
1580
1581enum inode_state {
1582 inode_state_no_change,
1583 inode_state_will_create,
1584 inode_state_did_create,
1585 inode_state_will_delete,
1586 inode_state_did_delete,
1587};
1588
1589static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
1590{
1591 int ret;
1592 int left_ret;
1593 int right_ret;
1594 u64 left_gen;
1595 u64 right_gen;
1596
1597 ret = get_inode_info(sctx->send_root, ino, NULL, &left_gen, NULL, NULL,
1598 NULL, NULL);
1599 if (ret < 0 && ret != -ENOENT)
1600 goto out;
1601 left_ret = ret;
1602
1603 if (!sctx->parent_root) {
1604 right_ret = -ENOENT;
1605 } else {
1606 ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen,
1607 NULL, NULL, NULL, NULL);
1608 if (ret < 0 && ret != -ENOENT)
1609 goto out;
1610 right_ret = ret;
1611 }
1612
1613 if (!left_ret && !right_ret) {
1614 if (left_gen == gen && right_gen == gen) {
1615 ret = inode_state_no_change;
1616 } else if (left_gen == gen) {
1617 if (ino < sctx->send_progress)
1618 ret = inode_state_did_create;
1619 else
1620 ret = inode_state_will_create;
1621 } else if (right_gen == gen) {
1622 if (ino < sctx->send_progress)
1623 ret = inode_state_did_delete;
1624 else
1625 ret = inode_state_will_delete;
1626 } else {
1627 ret = -ENOENT;
1628 }
1629 } else if (!left_ret) {
1630 if (left_gen == gen) {
1631 if (ino < sctx->send_progress)
1632 ret = inode_state_did_create;
1633 else
1634 ret = inode_state_will_create;
1635 } else {
1636 ret = -ENOENT;
1637 }
1638 } else if (!right_ret) {
1639 if (right_gen == gen) {
1640 if (ino < sctx->send_progress)
1641 ret = inode_state_did_delete;
1642 else
1643 ret = inode_state_will_delete;
1644 } else {
1645 ret = -ENOENT;
1646 }
1647 } else {
1648 ret = -ENOENT;
1649 }
1650
1651out:
1652 return ret;
1653}
1654
1655static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
1656{
1657 int ret;
1658
1659 if (ino == BTRFS_FIRST_FREE_OBJECTID)
1660 return 1;
1661
1662 ret = get_cur_inode_state(sctx, ino, gen);
1663 if (ret < 0)
1664 goto out;
1665
1666 if (ret == inode_state_no_change ||
1667 ret == inode_state_did_create ||
1668 ret == inode_state_will_delete)
1669 ret = 1;
1670 else
1671 ret = 0;
1672
1673out:
1674 return ret;
1675}
1676
1677/*
1678 * Helper function to lookup a dir item in a dir.
1679 */
1680static int lookup_dir_item_inode(struct btrfs_root *root,
1681 u64 dir, const char *name, int name_len,
1682 u64 *found_inode,
1683 u8 *found_type)
1684{
1685 int ret = 0;
1686 struct btrfs_dir_item *di;
1687 struct btrfs_key key;
1688 struct btrfs_path *path;
1689
1690 path = alloc_path_for_send();
1691 if (!path)
1692 return -ENOMEM;
1693
1694 di = btrfs_lookup_dir_item(NULL, root, path,
1695 dir, name, name_len, 0);
1696 if (!di) {
1697 ret = -ENOENT;
1698 goto out;
1699 }
1700 if (IS_ERR(di)) {
1701 ret = PTR_ERR(di);
1702 goto out;
1703 }
1704 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1705 if (key.type == BTRFS_ROOT_ITEM_KEY) {
1706 ret = -ENOENT;
1707 goto out;
1708 }
1709 *found_inode = key.objectid;
1710 *found_type = btrfs_dir_type(path->nodes[0], di);
1711
1712out:
1713 btrfs_free_path(path);
1714 return ret;
1715}
1716
1717/*
1718 * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir,
1719 * generation of the parent dir and the name of the dir entry.
1720 */
1721static int get_first_ref(struct btrfs_root *root, u64 ino,
1722 u64 *dir, u64 *dir_gen, struct fs_path *name)
1723{
1724 int ret;
1725 struct btrfs_key key;
1726 struct btrfs_key found_key;
1727 struct btrfs_path *path;
1728 int len;
1729 u64 parent_dir;
1730
1731 path = alloc_path_for_send();
1732 if (!path)
1733 return -ENOMEM;
1734
1735 key.objectid = ino;
1736 key.type = BTRFS_INODE_REF_KEY;
1737 key.offset = 0;
1738
1739 ret = btrfs_search_slot_for_read(root, &key, path, 1, 0);
1740 if (ret < 0)
1741 goto out;
1742 if (!ret)
1743 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1744 path->slots[0]);
1745 if (ret || found_key.objectid != ino ||
1746 (found_key.type != BTRFS_INODE_REF_KEY &&
1747 found_key.type != BTRFS_INODE_EXTREF_KEY)) {
1748 ret = -ENOENT;
1749 goto out;
1750 }
1751
1752 if (found_key.type == BTRFS_INODE_REF_KEY) {
1753 struct btrfs_inode_ref *iref;
1754 iref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1755 struct btrfs_inode_ref);
1756 len = btrfs_inode_ref_name_len(path->nodes[0], iref);
1757 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1758 (unsigned long)(iref + 1),
1759 len);
1760 parent_dir = found_key.offset;
1761 } else {
1762 struct btrfs_inode_extref *extref;
1763 extref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1764 struct btrfs_inode_extref);
1765 len = btrfs_inode_extref_name_len(path->nodes[0], extref);
1766 ret = fs_path_add_from_extent_buffer(name, path->nodes[0],
1767 (unsigned long)&extref->name, len);
1768 parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref);
1769 }
1770 if (ret < 0)
1771 goto out;
1772 btrfs_release_path(path);
1773
1774 if (dir_gen) {
1775 ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL,
1776 NULL, NULL, NULL);
1777 if (ret < 0)
1778 goto out;
1779 }
1780
1781 *dir = parent_dir;
1782
1783out:
1784 btrfs_free_path(path);
1785 return ret;
1786}
1787
1788static int is_first_ref(struct btrfs_root *root,
1789 u64 ino, u64 dir,
1790 const char *name, int name_len)
1791{
1792 int ret;
1793 struct fs_path *tmp_name;
1794 u64 tmp_dir;
1795
1796 tmp_name = fs_path_alloc();
1797 if (!tmp_name)
1798 return -ENOMEM;
1799
1800 ret = get_first_ref(root, ino, &tmp_dir, NULL, tmp_name);
1801 if (ret < 0)
1802 goto out;
1803
1804 if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) {
1805 ret = 0;
1806 goto out;
1807 }
1808
1809 ret = !memcmp(tmp_name->start, name, name_len);
1810
1811out:
1812 fs_path_free(tmp_name);
1813 return ret;
1814}
1815
1816/*
1817 * Used by process_recorded_refs to determine if a new ref would overwrite an
1818 * already existing ref. In case it detects an overwrite, it returns the
1819 * inode/gen in who_ino/who_gen.
1820 * When an overwrite is detected, process_recorded_refs does proper orphanizing
1821 * to make sure later references to the overwritten inode are possible.
1822 * Orphanizing is however only required for the first ref of an inode.
1823 * process_recorded_refs does an additional is_first_ref check to see if
1824 * orphanizing is really required.
1825 */
1826static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
1827 const char *name, int name_len,
1828 u64 *who_ino, u64 *who_gen, u64 *who_mode)
1829{
1830 int ret = 0;
1831 u64 gen;
1832 u64 other_inode = 0;
1833 u8 other_type = 0;
1834
1835 if (!sctx->parent_root)
1836 goto out;
1837
1838 ret = is_inode_existent(sctx, dir, dir_gen);
1839 if (ret <= 0)
1840 goto out;
1841
1842 /*
1843 * If we have a parent root we need to verify that the parent dir was
1844 * not deleted and then re-created, if it was then we have no overwrite
1845 * and we can just unlink this entry.
1846 */
1847 if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
1848 ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL,
1849 NULL, NULL, NULL);
1850 if (ret < 0 && ret != -ENOENT)
1851 goto out;
1852 if (ret) {
1853 ret = 0;
1854 goto out;
1855 }
1856 if (gen != dir_gen)
1857 goto out;
1858 }
1859
1860 ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
1861 &other_inode, &other_type);
1862 if (ret < 0 && ret != -ENOENT)
1863 goto out;
1864 if (ret) {
1865 ret = 0;
1866 goto out;
1867 }
1868
1869 /*
1870 * Check if the overwritten ref was already processed. If yes, the ref
1871 * was already unlinked/moved, so we can safely assume that we will not
1872 * overwrite anything at this point in time.
1873 */
1874 if (other_inode > sctx->send_progress ||
1875 is_waiting_for_move(sctx, other_inode)) {
1876 ret = get_inode_info(sctx->parent_root, other_inode, NULL,
1877 who_gen, who_mode, NULL, NULL, NULL);
1878 if (ret < 0)
1879 goto out;
1880
1881 ret = 1;
1882 *who_ino = other_inode;
1883 } else {
1884 ret = 0;
1885 }
1886
1887out:
1888 return ret;
1889}
1890
1891/*
1892 * Checks if the ref was overwritten by an already processed inode. This is
1893 * used by __get_cur_name_and_parent to find out if the ref was orphanized and
1894 * thus the orphan name needs be used.
1895 * process_recorded_refs also uses it to avoid unlinking of refs that were
1896 * overwritten.
1897 */
1898static int did_overwrite_ref(struct send_ctx *sctx,
1899 u64 dir, u64 dir_gen,
1900 u64 ino, u64 ino_gen,
1901 const char *name, int name_len)
1902{
1903 int ret = 0;
1904 u64 gen;
1905 u64 ow_inode;
1906 u8 other_type;
1907
1908 if (!sctx->parent_root)
1909 goto out;
1910
1911 ret = is_inode_existent(sctx, dir, dir_gen);
1912 if (ret <= 0)
1913 goto out;
1914
1915 if (dir != BTRFS_FIRST_FREE_OBJECTID) {
1916 ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL,
1917 NULL, NULL, NULL);
1918 if (ret < 0 && ret != -ENOENT)
1919 goto out;
1920 if (ret) {
1921 ret = 0;
1922 goto out;
1923 }
1924 if (gen != dir_gen)
1925 goto out;
1926 }
1927
1928 /* check if the ref was overwritten by another ref */
1929 ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
1930 &ow_inode, &other_type);
1931 if (ret < 0 && ret != -ENOENT)
1932 goto out;
1933 if (ret) {
1934 /* was never and will never be overwritten */
1935 ret = 0;
1936 goto out;
1937 }
1938
1939 ret = get_inode_info(sctx->send_root, ow_inode, NULL, &gen, NULL, NULL,
1940 NULL, NULL);
1941 if (ret < 0)
1942 goto out;
1943
1944 if (ow_inode == ino && gen == ino_gen) {
1945 ret = 0;
1946 goto out;
1947 }
1948
1949 /*
1950 * We know that it is or will be overwritten. Check this now.
1951 * The current inode being processed might have been the one that caused
1952 * inode 'ino' to be orphanized, therefore check if ow_inode matches
1953 * the current inode being processed.
1954 */
1955 if ((ow_inode < sctx->send_progress) ||
1956 (ino != sctx->cur_ino && ow_inode == sctx->cur_ino &&
1957 gen == sctx->cur_inode_gen))
1958 ret = 1;
1959 else
1960 ret = 0;
1961
1962out:
1963 return ret;
1964}
1965
1966/*
1967 * Same as did_overwrite_ref, but also checks if it is the first ref of an inode
1968 * that got overwritten. This is used by process_recorded_refs to determine
1969 * if it has to use the path as returned by get_cur_path or the orphan name.
1970 */
1971static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen)
1972{
1973 int ret = 0;
1974 struct fs_path *name = NULL;
1975 u64 dir;
1976 u64 dir_gen;
1977
1978 if (!sctx->parent_root)
1979 goto out;
1980
1981 name = fs_path_alloc();
1982 if (!name)
1983 return -ENOMEM;
1984
1985 ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name);
1986 if (ret < 0)
1987 goto out;
1988
1989 ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen,
1990 name->start, fs_path_len(name));
1991
1992out:
1993 fs_path_free(name);
1994 return ret;
1995}
1996
1997/*
1998 * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit,
1999 * so we need to do some special handling in case we have clashes. This function
2000 * takes care of this with the help of name_cache_entry::radix_list.
2001 * In case of error, nce is kfreed.
2002 */
2003static int name_cache_insert(struct send_ctx *sctx,
2004 struct name_cache_entry *nce)
2005{
2006 int ret = 0;
2007 struct list_head *nce_head;
2008
2009 nce_head = radix_tree_lookup(&sctx->name_cache,
2010 (unsigned long)nce->ino);
2011 if (!nce_head) {
2012 nce_head = kmalloc(sizeof(*nce_head), GFP_KERNEL);
2013 if (!nce_head) {
2014 kfree(nce);
2015 return -ENOMEM;
2016 }
2017 INIT_LIST_HEAD(nce_head);
2018
2019 ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head);
2020 if (ret < 0) {
2021 kfree(nce_head);
2022 kfree(nce);
2023 return ret;
2024 }
2025 }
2026 list_add_tail(&nce->radix_list, nce_head);
2027 list_add_tail(&nce->list, &sctx->name_cache_list);
2028 sctx->name_cache_size++;
2029
2030 return ret;
2031}
2032
2033static void name_cache_delete(struct send_ctx *sctx,
2034 struct name_cache_entry *nce)
2035{
2036 struct list_head *nce_head;
2037
2038 nce_head = radix_tree_lookup(&sctx->name_cache,
2039 (unsigned long)nce->ino);
2040 if (!nce_head) {
2041 btrfs_err(sctx->send_root->fs_info,
2042 "name_cache_delete lookup failed ino %llu cache size %d, leaking memory",
2043 nce->ino, sctx->name_cache_size);
2044 }
2045
2046 list_del(&nce->radix_list);
2047 list_del(&nce->list);
2048 sctx->name_cache_size--;
2049
2050 /*
2051 * We may not get to the final release of nce_head if the lookup fails
2052 */
2053 if (nce_head && list_empty(nce_head)) {
2054 radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino);
2055 kfree(nce_head);
2056 }
2057}
2058
2059static struct name_cache_entry *name_cache_search(struct send_ctx *sctx,
2060 u64 ino, u64 gen)
2061{
2062 struct list_head *nce_head;
2063 struct name_cache_entry *cur;
2064
2065 nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino);
2066 if (!nce_head)
2067 return NULL;
2068
2069 list_for_each_entry(cur, nce_head, radix_list) {
2070 if (cur->ino == ino && cur->gen == gen)
2071 return cur;
2072 }
2073 return NULL;
2074}
2075
2076/*
2077 * Removes the entry from the list and adds it back to the end. This marks the
2078 * entry as recently used so that name_cache_clean_unused does not remove it.
2079 */
2080static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce)
2081{
2082 list_del(&nce->list);
2083 list_add_tail(&nce->list, &sctx->name_cache_list);
2084}
2085
2086/*
2087 * Remove some entries from the beginning of name_cache_list.
2088 */
2089static void name_cache_clean_unused(struct send_ctx *sctx)
2090{
2091 struct name_cache_entry *nce;
2092
2093 if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE)
2094 return;
2095
2096 while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) {
2097 nce = list_entry(sctx->name_cache_list.next,
2098 struct name_cache_entry, list);
2099 name_cache_delete(sctx, nce);
2100 kfree(nce);
2101 }
2102}
2103
2104static void name_cache_free(struct send_ctx *sctx)
2105{
2106 struct name_cache_entry *nce;
2107
2108 while (!list_empty(&sctx->name_cache_list)) {
2109 nce = list_entry(sctx->name_cache_list.next,
2110 struct name_cache_entry, list);
2111 name_cache_delete(sctx, nce);
2112 kfree(nce);
2113 }
2114}
2115
2116/*
2117 * Used by get_cur_path for each ref up to the root.
2118 * Returns 0 if it succeeded.
2119 * Returns 1 if the inode is not existent or got overwritten. In that case, the
2120 * name is an orphan name. This instructs get_cur_path to stop iterating. If 1
2121 * is returned, parent_ino/parent_gen are not guaranteed to be valid.
2122 * Returns <0 in case of error.
2123 */
2124static int __get_cur_name_and_parent(struct send_ctx *sctx,
2125 u64 ino, u64 gen,
2126 u64 *parent_ino,
2127 u64 *parent_gen,
2128 struct fs_path *dest)
2129{
2130 int ret;
2131 int nce_ret;
2132 struct name_cache_entry *nce = NULL;
2133
2134 /*
2135 * First check if we already did a call to this function with the same
2136 * ino/gen. If yes, check if the cache entry is still up-to-date. If yes
2137 * return the cached result.
2138 */
2139 nce = name_cache_search(sctx, ino, gen);
2140 if (nce) {
2141 if (ino < sctx->send_progress && nce->need_later_update) {
2142 name_cache_delete(sctx, nce);
2143 kfree(nce);
2144 nce = NULL;
2145 } else {
2146 name_cache_used(sctx, nce);
2147 *parent_ino = nce->parent_ino;
2148 *parent_gen = nce->parent_gen;
2149 ret = fs_path_add(dest, nce->name, nce->name_len);
2150 if (ret < 0)
2151 goto out;
2152 ret = nce->ret;
2153 goto out;
2154 }
2155 }
2156
2157 /*
2158 * If the inode is not existent yet, add the orphan name and return 1.
2159 * This should only happen for the parent dir that we determine in
2160 * __record_new_ref
2161 */
2162 ret = is_inode_existent(sctx, ino, gen);
2163 if (ret < 0)
2164 goto out;
2165
2166 if (!ret) {
2167 ret = gen_unique_name(sctx, ino, gen, dest);
2168 if (ret < 0)
2169 goto out;
2170 ret = 1;
2171 goto out_cache;
2172 }
2173
2174 /*
2175 * Depending on whether the inode was already processed or not, use
2176 * send_root or parent_root for ref lookup.
2177 */
2178 if (ino < sctx->send_progress)
2179 ret = get_first_ref(sctx->send_root, ino,
2180 parent_ino, parent_gen, dest);
2181 else
2182 ret = get_first_ref(sctx->parent_root, ino,
2183 parent_ino, parent_gen, dest);
2184 if (ret < 0)
2185 goto out;
2186
2187 /*
2188 * Check if the ref was overwritten by an inode's ref that was processed
2189 * earlier. If yes, treat as orphan and return 1.
2190 */
2191 ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen,
2192 dest->start, dest->end - dest->start);
2193 if (ret < 0)
2194 goto out;
2195 if (ret) {
2196 fs_path_reset(dest);
2197 ret = gen_unique_name(sctx, ino, gen, dest);
2198 if (ret < 0)
2199 goto out;
2200 ret = 1;
2201 }
2202
2203out_cache:
2204 /*
2205 * Store the result of the lookup in the name cache.
2206 */
2207 nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_KERNEL);
2208 if (!nce) {
2209 ret = -ENOMEM;
2210 goto out;
2211 }
2212
2213 nce->ino = ino;
2214 nce->gen = gen;
2215 nce->parent_ino = *parent_ino;
2216 nce->parent_gen = *parent_gen;
2217 nce->name_len = fs_path_len(dest);
2218 nce->ret = ret;
2219 strcpy(nce->name, dest->start);
2220
2221 if (ino < sctx->send_progress)
2222 nce->need_later_update = 0;
2223 else
2224 nce->need_later_update = 1;
2225
2226 nce_ret = name_cache_insert(sctx, nce);
2227 if (nce_ret < 0)
2228 ret = nce_ret;
2229 name_cache_clean_unused(sctx);
2230
2231out:
2232 return ret;
2233}
2234
2235/*
2236 * Magic happens here. This function returns the first ref to an inode as it
2237 * would look like while receiving the stream at this point in time.
2238 * We walk the path up to the root. For every inode in between, we check if it
2239 * was already processed/sent. If yes, we continue with the parent as found
2240 * in send_root. If not, we continue with the parent as found in parent_root.
2241 * If we encounter an inode that was deleted at this point in time, we use the
2242 * inodes "orphan" name instead of the real name and stop. Same with new inodes
2243 * that were not created yet and overwritten inodes/refs.
2244 *
2245 * When do we have have orphan inodes:
2246 * 1. When an inode is freshly created and thus no valid refs are available yet
2247 * 2. When a directory lost all it's refs (deleted) but still has dir items
2248 * inside which were not processed yet (pending for move/delete). If anyone
2249 * tried to get the path to the dir items, it would get a path inside that
2250 * orphan directory.
2251 * 3. When an inode is moved around or gets new links, it may overwrite the ref
2252 * of an unprocessed inode. If in that case the first ref would be
2253 * overwritten, the overwritten inode gets "orphanized". Later when we
2254 * process this overwritten inode, it is restored at a new place by moving
2255 * the orphan inode.
2256 *
2257 * sctx->send_progress tells this function at which point in time receiving
2258 * would be.
2259 */
2260static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen,
2261 struct fs_path *dest)
2262{
2263 int ret = 0;
2264 struct fs_path *name = NULL;
2265 u64 parent_inode = 0;
2266 u64 parent_gen = 0;
2267 int stop = 0;
2268
2269 name = fs_path_alloc();
2270 if (!name) {
2271 ret = -ENOMEM;
2272 goto out;
2273 }
2274
2275 dest->reversed = 1;
2276 fs_path_reset(dest);
2277
2278 while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) {
2279 struct waiting_dir_move *wdm;
2280
2281 fs_path_reset(name);
2282
2283 if (is_waiting_for_rm(sctx, ino)) {
2284 ret = gen_unique_name(sctx, ino, gen, name);
2285 if (ret < 0)
2286 goto out;
2287 ret = fs_path_add_path(dest, name);
2288 break;
2289 }
2290
2291 wdm = get_waiting_dir_move(sctx, ino);
2292 if (wdm && wdm->orphanized) {
2293 ret = gen_unique_name(sctx, ino, gen, name);
2294 stop = 1;
2295 } else if (wdm) {
2296 ret = get_first_ref(sctx->parent_root, ino,
2297 &parent_inode, &parent_gen, name);
2298 } else {
2299 ret = __get_cur_name_and_parent(sctx, ino, gen,
2300 &parent_inode,
2301 &parent_gen, name);
2302 if (ret)
2303 stop = 1;
2304 }
2305
2306 if (ret < 0)
2307 goto out;
2308
2309 ret = fs_path_add_path(dest, name);
2310 if (ret < 0)
2311 goto out;
2312
2313 ino = parent_inode;
2314 gen = parent_gen;
2315 }
2316
2317out:
2318 fs_path_free(name);
2319 if (!ret)
2320 fs_path_unreverse(dest);
2321 return ret;
2322}
2323
2324/*
2325 * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace
2326 */
2327static int send_subvol_begin(struct send_ctx *sctx)
2328{
2329 int ret;
2330 struct btrfs_root *send_root = sctx->send_root;
2331 struct btrfs_root *parent_root = sctx->parent_root;
2332 struct btrfs_path *path;
2333 struct btrfs_key key;
2334 struct btrfs_root_ref *ref;
2335 struct extent_buffer *leaf;
2336 char *name = NULL;
2337 int namelen;
2338
2339 path = btrfs_alloc_path();
2340 if (!path)
2341 return -ENOMEM;
2342
2343 name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_KERNEL);
2344 if (!name) {
2345 btrfs_free_path(path);
2346 return -ENOMEM;
2347 }
2348
2349 key.objectid = send_root->objectid;
2350 key.type = BTRFS_ROOT_BACKREF_KEY;
2351 key.offset = 0;
2352
2353 ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root,
2354 &key, path, 1, 0);
2355 if (ret < 0)
2356 goto out;
2357 if (ret) {
2358 ret = -ENOENT;
2359 goto out;
2360 }
2361
2362 leaf = path->nodes[0];
2363 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2364 if (key.type != BTRFS_ROOT_BACKREF_KEY ||
2365 key.objectid != send_root->objectid) {
2366 ret = -ENOENT;
2367 goto out;
2368 }
2369 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
2370 namelen = btrfs_root_ref_name_len(leaf, ref);
2371 read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen);
2372 btrfs_release_path(path);
2373
2374 if (parent_root) {
2375 ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT);
2376 if (ret < 0)
2377 goto out;
2378 } else {
2379 ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL);
2380 if (ret < 0)
2381 goto out;
2382 }
2383
2384 TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen);
2385
2386 if (!btrfs_is_empty_uuid(sctx->send_root->root_item.received_uuid))
2387 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2388 sctx->send_root->root_item.received_uuid);
2389 else
2390 TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID,
2391 sctx->send_root->root_item.uuid);
2392
2393 TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID,
2394 le64_to_cpu(sctx->send_root->root_item.ctransid));
2395 if (parent_root) {
2396 if (!btrfs_is_empty_uuid(parent_root->root_item.received_uuid))
2397 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2398 parent_root->root_item.received_uuid);
2399 else
2400 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
2401 parent_root->root_item.uuid);
2402 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
2403 le64_to_cpu(sctx->parent_root->root_item.ctransid));
2404 }
2405
2406 ret = send_cmd(sctx);
2407
2408tlv_put_failure:
2409out:
2410 btrfs_free_path(path);
2411 kfree(name);
2412 return ret;
2413}
2414
2415static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size)
2416{
2417 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2418 int ret = 0;
2419 struct fs_path *p;
2420
2421 btrfs_debug(fs_info, "send_truncate %llu size=%llu", ino, size);
2422
2423 p = fs_path_alloc();
2424 if (!p)
2425 return -ENOMEM;
2426
2427 ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE);
2428 if (ret < 0)
2429 goto out;
2430
2431 ret = get_cur_path(sctx, ino, gen, p);
2432 if (ret < 0)
2433 goto out;
2434 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2435 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size);
2436
2437 ret = send_cmd(sctx);
2438
2439tlv_put_failure:
2440out:
2441 fs_path_free(p);
2442 return ret;
2443}
2444
2445static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode)
2446{
2447 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2448 int ret = 0;
2449 struct fs_path *p;
2450
2451 btrfs_debug(fs_info, "send_chmod %llu mode=%llu", ino, mode);
2452
2453 p = fs_path_alloc();
2454 if (!p)
2455 return -ENOMEM;
2456
2457 ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD);
2458 if (ret < 0)
2459 goto out;
2460
2461 ret = get_cur_path(sctx, ino, gen, p);
2462 if (ret < 0)
2463 goto out;
2464 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2465 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777);
2466
2467 ret = send_cmd(sctx);
2468
2469tlv_put_failure:
2470out:
2471 fs_path_free(p);
2472 return ret;
2473}
2474
2475static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid)
2476{
2477 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2478 int ret = 0;
2479 struct fs_path *p;
2480
2481 btrfs_debug(fs_info, "send_chown %llu uid=%llu, gid=%llu",
2482 ino, uid, gid);
2483
2484 p = fs_path_alloc();
2485 if (!p)
2486 return -ENOMEM;
2487
2488 ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN);
2489 if (ret < 0)
2490 goto out;
2491
2492 ret = get_cur_path(sctx, ino, gen, p);
2493 if (ret < 0)
2494 goto out;
2495 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2496 TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid);
2497 TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid);
2498
2499 ret = send_cmd(sctx);
2500
2501tlv_put_failure:
2502out:
2503 fs_path_free(p);
2504 return ret;
2505}
2506
2507static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen)
2508{
2509 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2510 int ret = 0;
2511 struct fs_path *p = NULL;
2512 struct btrfs_inode_item *ii;
2513 struct btrfs_path *path = NULL;
2514 struct extent_buffer *eb;
2515 struct btrfs_key key;
2516 int slot;
2517
2518 btrfs_debug(fs_info, "send_utimes %llu", ino);
2519
2520 p = fs_path_alloc();
2521 if (!p)
2522 return -ENOMEM;
2523
2524 path = alloc_path_for_send();
2525 if (!path) {
2526 ret = -ENOMEM;
2527 goto out;
2528 }
2529
2530 key.objectid = ino;
2531 key.type = BTRFS_INODE_ITEM_KEY;
2532 key.offset = 0;
2533 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2534 if (ret > 0)
2535 ret = -ENOENT;
2536 if (ret < 0)
2537 goto out;
2538
2539 eb = path->nodes[0];
2540 slot = path->slots[0];
2541 ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
2542
2543 ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES);
2544 if (ret < 0)
2545 goto out;
2546
2547 ret = get_cur_path(sctx, ino, gen, p);
2548 if (ret < 0)
2549 goto out;
2550 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2551 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, &ii->atime);
2552 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, &ii->mtime);
2553 TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, &ii->ctime);
2554 /* TODO Add otime support when the otime patches get into upstream */
2555
2556 ret = send_cmd(sctx);
2557
2558tlv_put_failure:
2559out:
2560 fs_path_free(p);
2561 btrfs_free_path(path);
2562 return ret;
2563}
2564
2565/*
2566 * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have
2567 * a valid path yet because we did not process the refs yet. So, the inode
2568 * is created as orphan.
2569 */
2570static int send_create_inode(struct send_ctx *sctx, u64 ino)
2571{
2572 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
2573 int ret = 0;
2574 struct fs_path *p;
2575 int cmd;
2576 u64 gen;
2577 u64 mode;
2578 u64 rdev;
2579
2580 btrfs_debug(fs_info, "send_create_inode %llu", ino);
2581
2582 p = fs_path_alloc();
2583 if (!p)
2584 return -ENOMEM;
2585
2586 if (ino != sctx->cur_ino) {
2587 ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode,
2588 NULL, NULL, &rdev);
2589 if (ret < 0)
2590 goto out;
2591 } else {
2592 gen = sctx->cur_inode_gen;
2593 mode = sctx->cur_inode_mode;
2594 rdev = sctx->cur_inode_rdev;
2595 }
2596
2597 if (S_ISREG(mode)) {
2598 cmd = BTRFS_SEND_C_MKFILE;
2599 } else if (S_ISDIR(mode)) {
2600 cmd = BTRFS_SEND_C_MKDIR;
2601 } else if (S_ISLNK(mode)) {
2602 cmd = BTRFS_SEND_C_SYMLINK;
2603 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
2604 cmd = BTRFS_SEND_C_MKNOD;
2605 } else if (S_ISFIFO(mode)) {
2606 cmd = BTRFS_SEND_C_MKFIFO;
2607 } else if (S_ISSOCK(mode)) {
2608 cmd = BTRFS_SEND_C_MKSOCK;
2609 } else {
2610 btrfs_warn(sctx->send_root->fs_info, "unexpected inode type %o",
2611 (int)(mode & S_IFMT));
2612 ret = -EOPNOTSUPP;
2613 goto out;
2614 }
2615
2616 ret = begin_cmd(sctx, cmd);
2617 if (ret < 0)
2618 goto out;
2619
2620 ret = gen_unique_name(sctx, ino, gen, p);
2621 if (ret < 0)
2622 goto out;
2623
2624 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
2625 TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino);
2626
2627 if (S_ISLNK(mode)) {
2628 fs_path_reset(p);
2629 ret = read_symlink(sctx->send_root, ino, p);
2630 if (ret < 0)
2631 goto out;
2632 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p);
2633 } else if (S_ISCHR(mode) || S_ISBLK(mode) ||
2634 S_ISFIFO(mode) || S_ISSOCK(mode)) {
2635 TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev));
2636 TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode);
2637 }
2638
2639 ret = send_cmd(sctx);
2640 if (ret < 0)
2641 goto out;
2642
2643
2644tlv_put_failure:
2645out:
2646 fs_path_free(p);
2647 return ret;
2648}
2649
2650/*
2651 * We need some special handling for inodes that get processed before the parent
2652 * directory got created. See process_recorded_refs for details.
2653 * This function does the check if we already created the dir out of order.
2654 */
2655static int did_create_dir(struct send_ctx *sctx, u64 dir)
2656{
2657 int ret = 0;
2658 struct btrfs_path *path = NULL;
2659 struct btrfs_key key;
2660 struct btrfs_key found_key;
2661 struct btrfs_key di_key;
2662 struct extent_buffer *eb;
2663 struct btrfs_dir_item *di;
2664 int slot;
2665
2666 path = alloc_path_for_send();
2667 if (!path) {
2668 ret = -ENOMEM;
2669 goto out;
2670 }
2671
2672 key.objectid = dir;
2673 key.type = BTRFS_DIR_INDEX_KEY;
2674 key.offset = 0;
2675 ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0);
2676 if (ret < 0)
2677 goto out;
2678
2679 while (1) {
2680 eb = path->nodes[0];
2681 slot = path->slots[0];
2682 if (slot >= btrfs_header_nritems(eb)) {
2683 ret = btrfs_next_leaf(sctx->send_root, path);
2684 if (ret < 0) {
2685 goto out;
2686 } else if (ret > 0) {
2687 ret = 0;
2688 break;
2689 }
2690 continue;
2691 }
2692
2693 btrfs_item_key_to_cpu(eb, &found_key, slot);
2694 if (found_key.objectid != key.objectid ||
2695 found_key.type != key.type) {
2696 ret = 0;
2697 goto out;
2698 }
2699
2700 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
2701 btrfs_dir_item_key_to_cpu(eb, di, &di_key);
2702
2703 if (di_key.type != BTRFS_ROOT_ITEM_KEY &&
2704 di_key.objectid < sctx->send_progress) {
2705 ret = 1;
2706 goto out;
2707 }
2708
2709 path->slots[0]++;
2710 }
2711
2712out:
2713 btrfs_free_path(path);
2714 return ret;
2715}
2716
2717/*
2718 * Only creates the inode if it is:
2719 * 1. Not a directory
2720 * 2. Or a directory which was not created already due to out of order
2721 * directories. See did_create_dir and process_recorded_refs for details.
2722 */
2723static int send_create_inode_if_needed(struct send_ctx *sctx)
2724{
2725 int ret;
2726
2727 if (S_ISDIR(sctx->cur_inode_mode)) {
2728 ret = did_create_dir(sctx, sctx->cur_ino);
2729 if (ret < 0)
2730 goto out;
2731 if (ret) {
2732 ret = 0;
2733 goto out;
2734 }
2735 }
2736
2737 ret = send_create_inode(sctx, sctx->cur_ino);
2738 if (ret < 0)
2739 goto out;
2740
2741out:
2742 return ret;
2743}
2744
2745struct recorded_ref {
2746 struct list_head list;
2747 char *name;
2748 struct fs_path *full_path;
2749 u64 dir;
2750 u64 dir_gen;
2751 int name_len;
2752};
2753
2754static void set_ref_path(struct recorded_ref *ref, struct fs_path *path)
2755{
2756 ref->full_path = path;
2757 ref->name = (char *)kbasename(ref->full_path->start);
2758 ref->name_len = ref->full_path->end - ref->name;
2759}
2760
2761/*
2762 * We need to process new refs before deleted refs, but compare_tree gives us
2763 * everything mixed. So we first record all refs and later process them.
2764 * This function is a helper to record one ref.
2765 */
2766static int __record_ref(struct list_head *head, u64 dir,
2767 u64 dir_gen, struct fs_path *path)
2768{
2769 struct recorded_ref *ref;
2770
2771 ref = kmalloc(sizeof(*ref), GFP_KERNEL);
2772 if (!ref)
2773 return -ENOMEM;
2774
2775 ref->dir = dir;
2776 ref->dir_gen = dir_gen;
2777 set_ref_path(ref, path);
2778 list_add_tail(&ref->list, head);
2779 return 0;
2780}
2781
2782static int dup_ref(struct recorded_ref *ref, struct list_head *list)
2783{
2784 struct recorded_ref *new;
2785
2786 new = kmalloc(sizeof(*ref), GFP_KERNEL);
2787 if (!new)
2788 return -ENOMEM;
2789
2790 new->dir = ref->dir;
2791 new->dir_gen = ref->dir_gen;
2792 new->full_path = NULL;
2793 INIT_LIST_HEAD(&new->list);
2794 list_add_tail(&new->list, list);
2795 return 0;
2796}
2797
2798static void __free_recorded_refs(struct list_head *head)
2799{
2800 struct recorded_ref *cur;
2801
2802 while (!list_empty(head)) {
2803 cur = list_entry(head->next, struct recorded_ref, list);
2804 fs_path_free(cur->full_path);
2805 list_del(&cur->list);
2806 kfree(cur);
2807 }
2808}
2809
2810static void free_recorded_refs(struct send_ctx *sctx)
2811{
2812 __free_recorded_refs(&sctx->new_refs);
2813 __free_recorded_refs(&sctx->deleted_refs);
2814}
2815
2816/*
2817 * Renames/moves a file/dir to its orphan name. Used when the first
2818 * ref of an unprocessed inode gets overwritten and for all non empty
2819 * directories.
2820 */
2821static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen,
2822 struct fs_path *path)
2823{
2824 int ret;
2825 struct fs_path *orphan;
2826
2827 orphan = fs_path_alloc();
2828 if (!orphan)
2829 return -ENOMEM;
2830
2831 ret = gen_unique_name(sctx, ino, gen, orphan);
2832 if (ret < 0)
2833 goto out;
2834
2835 ret = send_rename(sctx, path, orphan);
2836
2837out:
2838 fs_path_free(orphan);
2839 return ret;
2840}
2841
2842static struct orphan_dir_info *
2843add_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2844{
2845 struct rb_node **p = &sctx->orphan_dirs.rb_node;
2846 struct rb_node *parent = NULL;
2847 struct orphan_dir_info *entry, *odi;
2848
2849 while (*p) {
2850 parent = *p;
2851 entry = rb_entry(parent, struct orphan_dir_info, node);
2852 if (dir_ino < entry->ino) {
2853 p = &(*p)->rb_left;
2854 } else if (dir_ino > entry->ino) {
2855 p = &(*p)->rb_right;
2856 } else {
2857 return entry;
2858 }
2859 }
2860
2861 odi = kmalloc(sizeof(*odi), GFP_KERNEL);
2862 if (!odi)
2863 return ERR_PTR(-ENOMEM);
2864 odi->ino = dir_ino;
2865 odi->gen = 0;
2866 odi->last_dir_index_offset = 0;
2867
2868 rb_link_node(&odi->node, parent, p);
2869 rb_insert_color(&odi->node, &sctx->orphan_dirs);
2870 return odi;
2871}
2872
2873static struct orphan_dir_info *
2874get_orphan_dir_info(struct send_ctx *sctx, u64 dir_ino)
2875{
2876 struct rb_node *n = sctx->orphan_dirs.rb_node;
2877 struct orphan_dir_info *entry;
2878
2879 while (n) {
2880 entry = rb_entry(n, struct orphan_dir_info, node);
2881 if (dir_ino < entry->ino)
2882 n = n->rb_left;
2883 else if (dir_ino > entry->ino)
2884 n = n->rb_right;
2885 else
2886 return entry;
2887 }
2888 return NULL;
2889}
2890
2891static int is_waiting_for_rm(struct send_ctx *sctx, u64 dir_ino)
2892{
2893 struct orphan_dir_info *odi = get_orphan_dir_info(sctx, dir_ino);
2894
2895 return odi != NULL;
2896}
2897
2898static void free_orphan_dir_info(struct send_ctx *sctx,
2899 struct orphan_dir_info *odi)
2900{
2901 if (!odi)
2902 return;
2903 rb_erase(&odi->node, &sctx->orphan_dirs);
2904 kfree(odi);
2905}
2906
2907/*
2908 * Returns 1 if a directory can be removed at this point in time.
2909 * We check this by iterating all dir items and checking if the inode behind
2910 * the dir item was already processed.
2911 */
2912static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 dir_gen,
2913 u64 send_progress)
2914{
2915 int ret = 0;
2916 struct btrfs_root *root = sctx->parent_root;
2917 struct btrfs_path *path;
2918 struct btrfs_key key;
2919 struct btrfs_key found_key;
2920 struct btrfs_key loc;
2921 struct btrfs_dir_item *di;
2922 struct orphan_dir_info *odi = NULL;
2923
2924 /*
2925 * Don't try to rmdir the top/root subvolume dir.
2926 */
2927 if (dir == BTRFS_FIRST_FREE_OBJECTID)
2928 return 0;
2929
2930 path = alloc_path_for_send();
2931 if (!path)
2932 return -ENOMEM;
2933
2934 key.objectid = dir;
2935 key.type = BTRFS_DIR_INDEX_KEY;
2936 key.offset = 0;
2937
2938 odi = get_orphan_dir_info(sctx, dir);
2939 if (odi)
2940 key.offset = odi->last_dir_index_offset;
2941
2942 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2943 if (ret < 0)
2944 goto out;
2945
2946 while (1) {
2947 struct waiting_dir_move *dm;
2948
2949 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2950 ret = btrfs_next_leaf(root, path);
2951 if (ret < 0)
2952 goto out;
2953 else if (ret > 0)
2954 break;
2955 continue;
2956 }
2957 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2958 path->slots[0]);
2959 if (found_key.objectid != key.objectid ||
2960 found_key.type != key.type)
2961 break;
2962
2963 di = btrfs_item_ptr(path->nodes[0], path->slots[0],
2964 struct btrfs_dir_item);
2965 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc);
2966
2967 dm = get_waiting_dir_move(sctx, loc.objectid);
2968 if (dm) {
2969 odi = add_orphan_dir_info(sctx, dir);
2970 if (IS_ERR(odi)) {
2971 ret = PTR_ERR(odi);
2972 goto out;
2973 }
2974 odi->gen = dir_gen;
2975 odi->last_dir_index_offset = found_key.offset;
2976 dm->rmdir_ino = dir;
2977 ret = 0;
2978 goto out;
2979 }
2980
2981 if (loc.objectid > send_progress) {
2982 odi = add_orphan_dir_info(sctx, dir);
2983 if (IS_ERR(odi)) {
2984 ret = PTR_ERR(odi);
2985 goto out;
2986 }
2987 odi->gen = dir_gen;
2988 odi->last_dir_index_offset = found_key.offset;
2989 ret = 0;
2990 goto out;
2991 }
2992
2993 path->slots[0]++;
2994 }
2995 free_orphan_dir_info(sctx, odi);
2996
2997 ret = 1;
2998
2999out:
3000 btrfs_free_path(path);
3001 return ret;
3002}
3003
3004static int is_waiting_for_move(struct send_ctx *sctx, u64 ino)
3005{
3006 struct waiting_dir_move *entry = get_waiting_dir_move(sctx, ino);
3007
3008 return entry != NULL;
3009}
3010
3011static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino, bool orphanized)
3012{
3013 struct rb_node **p = &sctx->waiting_dir_moves.rb_node;
3014 struct rb_node *parent = NULL;
3015 struct waiting_dir_move *entry, *dm;
3016
3017 dm = kmalloc(sizeof(*dm), GFP_KERNEL);
3018 if (!dm)
3019 return -ENOMEM;
3020 dm->ino = ino;
3021 dm->rmdir_ino = 0;
3022 dm->orphanized = orphanized;
3023
3024 while (*p) {
3025 parent = *p;
3026 entry = rb_entry(parent, struct waiting_dir_move, node);
3027 if (ino < entry->ino) {
3028 p = &(*p)->rb_left;
3029 } else if (ino > entry->ino) {
3030 p = &(*p)->rb_right;
3031 } else {
3032 kfree(dm);
3033 return -EEXIST;
3034 }
3035 }
3036
3037 rb_link_node(&dm->node, parent, p);
3038 rb_insert_color(&dm->node, &sctx->waiting_dir_moves);
3039 return 0;
3040}
3041
3042static struct waiting_dir_move *
3043get_waiting_dir_move(struct send_ctx *sctx, u64 ino)
3044{
3045 struct rb_node *n = sctx->waiting_dir_moves.rb_node;
3046 struct waiting_dir_move *entry;
3047
3048 while (n) {
3049 entry = rb_entry(n, struct waiting_dir_move, node);
3050 if (ino < entry->ino)
3051 n = n->rb_left;
3052 else if (ino > entry->ino)
3053 n = n->rb_right;
3054 else
3055 return entry;
3056 }
3057 return NULL;
3058}
3059
3060static void free_waiting_dir_move(struct send_ctx *sctx,
3061 struct waiting_dir_move *dm)
3062{
3063 if (!dm)
3064 return;
3065 rb_erase(&dm->node, &sctx->waiting_dir_moves);
3066 kfree(dm);
3067}
3068
3069static int add_pending_dir_move(struct send_ctx *sctx,
3070 u64 ino,
3071 u64 ino_gen,
3072 u64 parent_ino,
3073 struct list_head *new_refs,
3074 struct list_head *deleted_refs,
3075 const bool is_orphan)
3076{
3077 struct rb_node **p = &sctx->pending_dir_moves.rb_node;
3078 struct rb_node *parent = NULL;
3079 struct pending_dir_move *entry = NULL, *pm;
3080 struct recorded_ref *cur;
3081 int exists = 0;
3082 int ret;
3083
3084 pm = kmalloc(sizeof(*pm), GFP_KERNEL);
3085 if (!pm)
3086 return -ENOMEM;
3087 pm->parent_ino = parent_ino;
3088 pm->ino = ino;
3089 pm->gen = ino_gen;
3090 INIT_LIST_HEAD(&pm->list);
3091 INIT_LIST_HEAD(&pm->update_refs);
3092 RB_CLEAR_NODE(&pm->node);
3093
3094 while (*p) {
3095 parent = *p;
3096 entry = rb_entry(parent, struct pending_dir_move, node);
3097 if (parent_ino < entry->parent_ino) {
3098 p = &(*p)->rb_left;
3099 } else if (parent_ino > entry->parent_ino) {
3100 p = &(*p)->rb_right;
3101 } else {
3102 exists = 1;
3103 break;
3104 }
3105 }
3106
3107 list_for_each_entry(cur, deleted_refs, list) {
3108 ret = dup_ref(cur, &pm->update_refs);
3109 if (ret < 0)
3110 goto out;
3111 }
3112 list_for_each_entry(cur, new_refs, list) {
3113 ret = dup_ref(cur, &pm->update_refs);
3114 if (ret < 0)
3115 goto out;
3116 }
3117
3118 ret = add_waiting_dir_move(sctx, pm->ino, is_orphan);
3119 if (ret)
3120 goto out;
3121
3122 if (exists) {
3123 list_add_tail(&pm->list, &entry->list);
3124 } else {
3125 rb_link_node(&pm->node, parent, p);
3126 rb_insert_color(&pm->node, &sctx->pending_dir_moves);
3127 }
3128 ret = 0;
3129out:
3130 if (ret) {
3131 __free_recorded_refs(&pm->update_refs);
3132 kfree(pm);
3133 }
3134 return ret;
3135}
3136
3137static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx,
3138 u64 parent_ino)
3139{
3140 struct rb_node *n = sctx->pending_dir_moves.rb_node;
3141 struct pending_dir_move *entry;
3142
3143 while (n) {
3144 entry = rb_entry(n, struct pending_dir_move, node);
3145 if (parent_ino < entry->parent_ino)
3146 n = n->rb_left;
3147 else if (parent_ino > entry->parent_ino)
3148 n = n->rb_right;
3149 else
3150 return entry;
3151 }
3152 return NULL;
3153}
3154
3155static int path_loop(struct send_ctx *sctx, struct fs_path *name,
3156 u64 ino, u64 gen, u64 *ancestor_ino)
3157{
3158 int ret = 0;
3159 u64 parent_inode = 0;
3160 u64 parent_gen = 0;
3161 u64 start_ino = ino;
3162
3163 *ancestor_ino = 0;
3164 while (ino != BTRFS_FIRST_FREE_OBJECTID) {
3165 fs_path_reset(name);
3166
3167 if (is_waiting_for_rm(sctx, ino))
3168 break;
3169 if (is_waiting_for_move(sctx, ino)) {
3170 if (*ancestor_ino == 0)
3171 *ancestor_ino = ino;
3172 ret = get_first_ref(sctx->parent_root, ino,
3173 &parent_inode, &parent_gen, name);
3174 } else {
3175 ret = __get_cur_name_and_parent(sctx, ino, gen,
3176 &parent_inode,
3177 &parent_gen, name);
3178 if (ret > 0) {
3179 ret = 0;
3180 break;
3181 }
3182 }
3183 if (ret < 0)
3184 break;
3185 if (parent_inode == start_ino) {
3186 ret = 1;
3187 if (*ancestor_ino == 0)
3188 *ancestor_ino = ino;
3189 break;
3190 }
3191 ino = parent_inode;
3192 gen = parent_gen;
3193 }
3194 return ret;
3195}
3196
3197static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm)
3198{
3199 struct fs_path *from_path = NULL;
3200 struct fs_path *to_path = NULL;
3201 struct fs_path *name = NULL;
3202 u64 orig_progress = sctx->send_progress;
3203 struct recorded_ref *cur;
3204 u64 parent_ino, parent_gen;
3205 struct waiting_dir_move *dm = NULL;
3206 u64 rmdir_ino = 0;
3207 u64 ancestor;
3208 bool is_orphan;
3209 int ret;
3210
3211 name = fs_path_alloc();
3212 from_path = fs_path_alloc();
3213 if (!name || !from_path) {
3214 ret = -ENOMEM;
3215 goto out;
3216 }
3217
3218 dm = get_waiting_dir_move(sctx, pm->ino);
3219 ASSERT(dm);
3220 rmdir_ino = dm->rmdir_ino;
3221 is_orphan = dm->orphanized;
3222 free_waiting_dir_move(sctx, dm);
3223
3224 if (is_orphan) {
3225 ret = gen_unique_name(sctx, pm->ino,
3226 pm->gen, from_path);
3227 } else {
3228 ret = get_first_ref(sctx->parent_root, pm->ino,
3229 &parent_ino, &parent_gen, name);
3230 if (ret < 0)
3231 goto out;
3232 ret = get_cur_path(sctx, parent_ino, parent_gen,
3233 from_path);
3234 if (ret < 0)
3235 goto out;
3236 ret = fs_path_add_path(from_path, name);
3237 }
3238 if (ret < 0)
3239 goto out;
3240
3241 sctx->send_progress = sctx->cur_ino + 1;
3242 ret = path_loop(sctx, name, pm->ino, pm->gen, &ancestor);
3243 if (ret < 0)
3244 goto out;
3245 if (ret) {
3246 LIST_HEAD(deleted_refs);
3247 ASSERT(ancestor > BTRFS_FIRST_FREE_OBJECTID);
3248 ret = add_pending_dir_move(sctx, pm->ino, pm->gen, ancestor,
3249 &pm->update_refs, &deleted_refs,
3250 is_orphan);
3251 if (ret < 0)
3252 goto out;
3253 if (rmdir_ino) {
3254 dm = get_waiting_dir_move(sctx, pm->ino);
3255 ASSERT(dm);
3256 dm->rmdir_ino = rmdir_ino;
3257 }
3258 goto out;
3259 }
3260 fs_path_reset(name);
3261 to_path = name;
3262 name = NULL;
3263 ret = get_cur_path(sctx, pm->ino, pm->gen, to_path);
3264 if (ret < 0)
3265 goto out;
3266
3267 ret = send_rename(sctx, from_path, to_path);
3268 if (ret < 0)
3269 goto out;
3270
3271 if (rmdir_ino) {
3272 struct orphan_dir_info *odi;
3273 u64 gen;
3274
3275 odi = get_orphan_dir_info(sctx, rmdir_ino);
3276 if (!odi) {
3277 /* already deleted */
3278 goto finish;
3279 }
3280 gen = odi->gen;
3281
3282 ret = can_rmdir(sctx, rmdir_ino, gen, sctx->cur_ino);
3283 if (ret < 0)
3284 goto out;
3285 if (!ret)
3286 goto finish;
3287
3288 name = fs_path_alloc();
3289 if (!name) {
3290 ret = -ENOMEM;
3291 goto out;
3292 }
3293 ret = get_cur_path(sctx, rmdir_ino, gen, name);
3294 if (ret < 0)
3295 goto out;
3296 ret = send_rmdir(sctx, name);
3297 if (ret < 0)
3298 goto out;
3299 }
3300
3301finish:
3302 ret = send_utimes(sctx, pm->ino, pm->gen);
3303 if (ret < 0)
3304 goto out;
3305
3306 /*
3307 * After rename/move, need to update the utimes of both new parent(s)
3308 * and old parent(s).
3309 */
3310 list_for_each_entry(cur, &pm->update_refs, list) {
3311 /*
3312 * The parent inode might have been deleted in the send snapshot
3313 */
3314 ret = get_inode_info(sctx->send_root, cur->dir, NULL,
3315 NULL, NULL, NULL, NULL, NULL);
3316 if (ret == -ENOENT) {
3317 ret = 0;
3318 continue;
3319 }
3320 if (ret < 0)
3321 goto out;
3322
3323 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
3324 if (ret < 0)
3325 goto out;
3326 }
3327
3328out:
3329 fs_path_free(name);
3330 fs_path_free(from_path);
3331 fs_path_free(to_path);
3332 sctx->send_progress = orig_progress;
3333
3334 return ret;
3335}
3336
3337static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m)
3338{
3339 if (!list_empty(&m->list))
3340 list_del(&m->list);
3341 if (!RB_EMPTY_NODE(&m->node))
3342 rb_erase(&m->node, &sctx->pending_dir_moves);
3343 __free_recorded_refs(&m->update_refs);
3344 kfree(m);
3345}
3346
3347static void tail_append_pending_moves(struct send_ctx *sctx,
3348 struct pending_dir_move *moves,
3349 struct list_head *stack)
3350{
3351 if (list_empty(&moves->list)) {
3352 list_add_tail(&moves->list, stack);
3353 } else {
3354 LIST_HEAD(list);
3355 list_splice_init(&moves->list, &list);
3356 list_add_tail(&moves->list, stack);
3357 list_splice_tail(&list, stack);
3358 }
3359 if (!RB_EMPTY_NODE(&moves->node)) {
3360 rb_erase(&moves->node, &sctx->pending_dir_moves);
3361 RB_CLEAR_NODE(&moves->node);
3362 }
3363}
3364
3365static int apply_children_dir_moves(struct send_ctx *sctx)
3366{
3367 struct pending_dir_move *pm;
3368 struct list_head stack;
3369 u64 parent_ino = sctx->cur_ino;
3370 int ret = 0;
3371
3372 pm = get_pending_dir_moves(sctx, parent_ino);
3373 if (!pm)
3374 return 0;
3375
3376 INIT_LIST_HEAD(&stack);
3377 tail_append_pending_moves(sctx, pm, &stack);
3378
3379 while (!list_empty(&stack)) {
3380 pm = list_first_entry(&stack, struct pending_dir_move, list);
3381 parent_ino = pm->ino;
3382 ret = apply_dir_move(sctx, pm);
3383 free_pending_move(sctx, pm);
3384 if (ret)
3385 goto out;
3386 pm = get_pending_dir_moves(sctx, parent_ino);
3387 if (pm)
3388 tail_append_pending_moves(sctx, pm, &stack);
3389 }
3390 return 0;
3391
3392out:
3393 while (!list_empty(&stack)) {
3394 pm = list_first_entry(&stack, struct pending_dir_move, list);
3395 free_pending_move(sctx, pm);
3396 }
3397 return ret;
3398}
3399
3400/*
3401 * We might need to delay a directory rename even when no ancestor directory
3402 * (in the send root) with a higher inode number than ours (sctx->cur_ino) was
3403 * renamed. This happens when we rename a directory to the old name (the name
3404 * in the parent root) of some other unrelated directory that got its rename
3405 * delayed due to some ancestor with higher number that got renamed.
3406 *
3407 * Example:
3408 *
3409 * Parent snapshot:
3410 * . (ino 256)
3411 * |---- a/ (ino 257)
3412 * | |---- file (ino 260)
3413 * |
3414 * |---- b/ (ino 258)
3415 * |---- c/ (ino 259)
3416 *
3417 * Send snapshot:
3418 * . (ino 256)
3419 * |---- a/ (ino 258)
3420 * |---- x/ (ino 259)
3421 * |---- y/ (ino 257)
3422 * |----- file (ino 260)
3423 *
3424 * Here we can not rename 258 from 'b' to 'a' without the rename of inode 257
3425 * from 'a' to 'x/y' happening first, which in turn depends on the rename of
3426 * inode 259 from 'c' to 'x'. So the order of rename commands the send stream
3427 * must issue is:
3428 *
3429 * 1 - rename 259 from 'c' to 'x'
3430 * 2 - rename 257 from 'a' to 'x/y'
3431 * 3 - rename 258 from 'b' to 'a'
3432 *
3433 * Returns 1 if the rename of sctx->cur_ino needs to be delayed, 0 if it can
3434 * be done right away and < 0 on error.
3435 */
3436static int wait_for_dest_dir_move(struct send_ctx *sctx,
3437 struct recorded_ref *parent_ref,
3438 const bool is_orphan)
3439{
3440 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3441 struct btrfs_path *path;
3442 struct btrfs_key key;
3443 struct btrfs_key di_key;
3444 struct btrfs_dir_item *di;
3445 u64 left_gen;
3446 u64 right_gen;
3447 int ret = 0;
3448 struct waiting_dir_move *wdm;
3449
3450 if (RB_EMPTY_ROOT(&sctx->waiting_dir_moves))
3451 return 0;
3452
3453 path = alloc_path_for_send();
3454 if (!path)
3455 return -ENOMEM;
3456
3457 key.objectid = parent_ref->dir;
3458 key.type = BTRFS_DIR_ITEM_KEY;
3459 key.offset = btrfs_name_hash(parent_ref->name, parent_ref->name_len);
3460
3461 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
3462 if (ret < 0) {
3463 goto out;
3464 } else if (ret > 0) {
3465 ret = 0;
3466 goto out;
3467 }
3468
3469 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3470 parent_ref->name_len);
3471 if (!di) {
3472 ret = 0;
3473 goto out;
3474 }
3475 /*
3476 * di_key.objectid has the number of the inode that has a dentry in the
3477 * parent directory with the same name that sctx->cur_ino is being
3478 * renamed to. We need to check if that inode is in the send root as
3479 * well and if it is currently marked as an inode with a pending rename,
3480 * if it is, we need to delay the rename of sctx->cur_ino as well, so
3481 * that it happens after that other inode is renamed.
3482 */
3483 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &di_key);
3484 if (di_key.type != BTRFS_INODE_ITEM_KEY) {
3485 ret = 0;
3486 goto out;
3487 }
3488
3489 ret = get_inode_info(sctx->parent_root, di_key.objectid, NULL,
3490 &left_gen, NULL, NULL, NULL, NULL);
3491 if (ret < 0)
3492 goto out;
3493 ret = get_inode_info(sctx->send_root, di_key.objectid, NULL,
3494 &right_gen, NULL, NULL, NULL, NULL);
3495 if (ret < 0) {
3496 if (ret == -ENOENT)
3497 ret = 0;
3498 goto out;
3499 }
3500
3501 /* Different inode, no need to delay the rename of sctx->cur_ino */
3502 if (right_gen != left_gen) {
3503 ret = 0;
3504 goto out;
3505 }
3506
3507 wdm = get_waiting_dir_move(sctx, di_key.objectid);
3508 if (wdm && !wdm->orphanized) {
3509 ret = add_pending_dir_move(sctx,
3510 sctx->cur_ino,
3511 sctx->cur_inode_gen,
3512 di_key.objectid,
3513 &sctx->new_refs,
3514 &sctx->deleted_refs,
3515 is_orphan);
3516 if (!ret)
3517 ret = 1;
3518 }
3519out:
3520 btrfs_free_path(path);
3521 return ret;
3522}
3523
3524/*
3525 * Check if inode ino2, or any of its ancestors, is inode ino1.
3526 * Return 1 if true, 0 if false and < 0 on error.
3527 */
3528static int check_ino_in_path(struct btrfs_root *root,
3529 const u64 ino1,
3530 const u64 ino1_gen,
3531 const u64 ino2,
3532 const u64 ino2_gen,
3533 struct fs_path *fs_path)
3534{
3535 u64 ino = ino2;
3536
3537 if (ino1 == ino2)
3538 return ino1_gen == ino2_gen;
3539
3540 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3541 u64 parent;
3542 u64 parent_gen;
3543 int ret;
3544
3545 fs_path_reset(fs_path);
3546 ret = get_first_ref(root, ino, &parent, &parent_gen, fs_path);
3547 if (ret < 0)
3548 return ret;
3549 if (parent == ino1)
3550 return parent_gen == ino1_gen;
3551 ino = parent;
3552 }
3553 return 0;
3554}
3555
3556/*
3557 * Check if ino ino1 is an ancestor of inode ino2 in the given root for any
3558 * possible path (in case ino2 is not a directory and has multiple hard links).
3559 * Return 1 if true, 0 if false and < 0 on error.
3560 */
3561static int is_ancestor(struct btrfs_root *root,
3562 const u64 ino1,
3563 const u64 ino1_gen,
3564 const u64 ino2,
3565 struct fs_path *fs_path)
3566{
3567 bool free_fs_path = false;
3568 int ret = 0;
3569 struct btrfs_path *path = NULL;
3570 struct btrfs_key key;
3571
3572 if (!fs_path) {
3573 fs_path = fs_path_alloc();
3574 if (!fs_path)
3575 return -ENOMEM;
3576 free_fs_path = true;
3577 }
3578
3579 path = alloc_path_for_send();
3580 if (!path) {
3581 ret = -ENOMEM;
3582 goto out;
3583 }
3584
3585 key.objectid = ino2;
3586 key.type = BTRFS_INODE_REF_KEY;
3587 key.offset = 0;
3588
3589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3590 if (ret < 0)
3591 goto out;
3592
3593 while (true) {
3594 struct extent_buffer *leaf = path->nodes[0];
3595 int slot = path->slots[0];
3596 u32 cur_offset = 0;
3597 u32 item_size;
3598
3599 if (slot >= btrfs_header_nritems(leaf)) {
3600 ret = btrfs_next_leaf(root, path);
3601 if (ret < 0)
3602 goto out;
3603 if (ret > 0)
3604 break;
3605 continue;
3606 }
3607
3608 btrfs_item_key_to_cpu(leaf, &key, slot);
3609 if (key.objectid != ino2)
3610 break;
3611 if (key.type != BTRFS_INODE_REF_KEY &&
3612 key.type != BTRFS_INODE_EXTREF_KEY)
3613 break;
3614
3615 item_size = btrfs_item_size_nr(leaf, slot);
3616 while (cur_offset < item_size) {
3617 u64 parent;
3618 u64 parent_gen;
3619
3620 if (key.type == BTRFS_INODE_EXTREF_KEY) {
3621 unsigned long ptr;
3622 struct btrfs_inode_extref *extref;
3623
3624 ptr = btrfs_item_ptr_offset(leaf, slot);
3625 extref = (struct btrfs_inode_extref *)
3626 (ptr + cur_offset);
3627 parent = btrfs_inode_extref_parent(leaf,
3628 extref);
3629 cur_offset += sizeof(*extref);
3630 cur_offset += btrfs_inode_extref_name_len(leaf,
3631 extref);
3632 } else {
3633 parent = key.offset;
3634 cur_offset = item_size;
3635 }
3636
3637 ret = get_inode_info(root, parent, NULL, &parent_gen,
3638 NULL, NULL, NULL, NULL);
3639 if (ret < 0)
3640 goto out;
3641 ret = check_ino_in_path(root, ino1, ino1_gen,
3642 parent, parent_gen, fs_path);
3643 if (ret)
3644 goto out;
3645 }
3646 path->slots[0]++;
3647 }
3648 ret = 0;
3649 out:
3650 btrfs_free_path(path);
3651 if (free_fs_path)
3652 fs_path_free(fs_path);
3653 return ret;
3654}
3655
3656static int wait_for_parent_move(struct send_ctx *sctx,
3657 struct recorded_ref *parent_ref,
3658 const bool is_orphan)
3659{
3660 int ret = 0;
3661 u64 ino = parent_ref->dir;
3662 u64 ino_gen = parent_ref->dir_gen;
3663 u64 parent_ino_before, parent_ino_after;
3664 struct fs_path *path_before = NULL;
3665 struct fs_path *path_after = NULL;
3666 int len1, len2;
3667
3668 path_after = fs_path_alloc();
3669 path_before = fs_path_alloc();
3670 if (!path_after || !path_before) {
3671 ret = -ENOMEM;
3672 goto out;
3673 }
3674
3675 /*
3676 * Our current directory inode may not yet be renamed/moved because some
3677 * ancestor (immediate or not) has to be renamed/moved first. So find if
3678 * such ancestor exists and make sure our own rename/move happens after
3679 * that ancestor is processed to avoid path build infinite loops (done
3680 * at get_cur_path()).
3681 */
3682 while (ino > BTRFS_FIRST_FREE_OBJECTID) {
3683 u64 parent_ino_after_gen;
3684
3685 if (is_waiting_for_move(sctx, ino)) {
3686 /*
3687 * If the current inode is an ancestor of ino in the
3688 * parent root, we need to delay the rename of the
3689 * current inode, otherwise don't delayed the rename
3690 * because we can end up with a circular dependency
3691 * of renames, resulting in some directories never
3692 * getting the respective rename operations issued in
3693 * the send stream or getting into infinite path build
3694 * loops.
3695 */
3696 ret = is_ancestor(sctx->parent_root,
3697 sctx->cur_ino, sctx->cur_inode_gen,
3698 ino, path_before);
3699 if (ret)
3700 break;
3701 }
3702
3703 fs_path_reset(path_before);
3704 fs_path_reset(path_after);
3705
3706 ret = get_first_ref(sctx->send_root, ino, &parent_ino_after,
3707 &parent_ino_after_gen, path_after);
3708 if (ret < 0)
3709 goto out;
3710 ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before,
3711 NULL, path_before);
3712 if (ret < 0 && ret != -ENOENT) {
3713 goto out;
3714 } else if (ret == -ENOENT) {
3715 ret = 0;
3716 break;
3717 }
3718
3719 len1 = fs_path_len(path_before);
3720 len2 = fs_path_len(path_after);
3721 if (ino > sctx->cur_ino &&
3722 (parent_ino_before != parent_ino_after || len1 != len2 ||
3723 memcmp(path_before->start, path_after->start, len1))) {
3724 u64 parent_ino_gen;
3725
3726 ret = get_inode_info(sctx->parent_root, ino, NULL,
3727 &parent_ino_gen, NULL, NULL, NULL,
3728 NULL);
3729 if (ret < 0)
3730 goto out;
3731 if (ino_gen == parent_ino_gen) {
3732 ret = 1;
3733 break;
3734 }
3735 }
3736 ino = parent_ino_after;
3737 ino_gen = parent_ino_after_gen;
3738 }
3739
3740out:
3741 fs_path_free(path_before);
3742 fs_path_free(path_after);
3743
3744 if (ret == 1) {
3745 ret = add_pending_dir_move(sctx,
3746 sctx->cur_ino,
3747 sctx->cur_inode_gen,
3748 ino,
3749 &sctx->new_refs,
3750 &sctx->deleted_refs,
3751 is_orphan);
3752 if (!ret)
3753 ret = 1;
3754 }
3755
3756 return ret;
3757}
3758
3759static int update_ref_path(struct send_ctx *sctx, struct recorded_ref *ref)
3760{
3761 int ret;
3762 struct fs_path *new_path;
3763
3764 /*
3765 * Our reference's name member points to its full_path member string, so
3766 * we use here a new path.
3767 */
3768 new_path = fs_path_alloc();
3769 if (!new_path)
3770 return -ENOMEM;
3771
3772 ret = get_cur_path(sctx, ref->dir, ref->dir_gen, new_path);
3773 if (ret < 0) {
3774 fs_path_free(new_path);
3775 return ret;
3776 }
3777 ret = fs_path_add(new_path, ref->name, ref->name_len);
3778 if (ret < 0) {
3779 fs_path_free(new_path);
3780 return ret;
3781 }
3782
3783 fs_path_free(ref->full_path);
3784 set_ref_path(ref, new_path);
3785
3786 return 0;
3787}
3788
3789/*
3790 * This does all the move/link/unlink/rmdir magic.
3791 */
3792static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
3793{
3794 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
3795 int ret = 0;
3796 struct recorded_ref *cur;
3797 struct recorded_ref *cur2;
3798 struct list_head check_dirs;
3799 struct fs_path *valid_path = NULL;
3800 u64 ow_inode = 0;
3801 u64 ow_gen;
3802 u64 ow_mode;
3803 int did_overwrite = 0;
3804 int is_orphan = 0;
3805 u64 last_dir_ino_rm = 0;
3806 bool can_rename = true;
3807 bool orphanized_dir = false;
3808 bool orphanized_ancestor = false;
3809
3810 btrfs_debug(fs_info, "process_recorded_refs %llu", sctx->cur_ino);
3811
3812 /*
3813 * This should never happen as the root dir always has the same ref
3814 * which is always '..'
3815 */
3816 BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID);
3817 INIT_LIST_HEAD(&check_dirs);
3818
3819 valid_path = fs_path_alloc();
3820 if (!valid_path) {
3821 ret = -ENOMEM;
3822 goto out;
3823 }
3824
3825 /*
3826 * First, check if the first ref of the current inode was overwritten
3827 * before. If yes, we know that the current inode was already orphanized
3828 * and thus use the orphan name. If not, we can use get_cur_path to
3829 * get the path of the first ref as it would like while receiving at
3830 * this point in time.
3831 * New inodes are always orphan at the beginning, so force to use the
3832 * orphan name in this case.
3833 * The first ref is stored in valid_path and will be updated if it
3834 * gets moved around.
3835 */
3836 if (!sctx->cur_inode_new) {
3837 ret = did_overwrite_first_ref(sctx, sctx->cur_ino,
3838 sctx->cur_inode_gen);
3839 if (ret < 0)
3840 goto out;
3841 if (ret)
3842 did_overwrite = 1;
3843 }
3844 if (sctx->cur_inode_new || did_overwrite) {
3845 ret = gen_unique_name(sctx, sctx->cur_ino,
3846 sctx->cur_inode_gen, valid_path);
3847 if (ret < 0)
3848 goto out;
3849 is_orphan = 1;
3850 } else {
3851 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen,
3852 valid_path);
3853 if (ret < 0)
3854 goto out;
3855 }
3856
3857 list_for_each_entry(cur, &sctx->new_refs, list) {
3858 /*
3859 * We may have refs where the parent directory does not exist
3860 * yet. This happens if the parent directories inum is higher
3861 * the the current inum. To handle this case, we create the
3862 * parent directory out of order. But we need to check if this
3863 * did already happen before due to other refs in the same dir.
3864 */
3865 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
3866 if (ret < 0)
3867 goto out;
3868 if (ret == inode_state_will_create) {
3869 ret = 0;
3870 /*
3871 * First check if any of the current inodes refs did
3872 * already create the dir.
3873 */
3874 list_for_each_entry(cur2, &sctx->new_refs, list) {
3875 if (cur == cur2)
3876 break;
3877 if (cur2->dir == cur->dir) {
3878 ret = 1;
3879 break;
3880 }
3881 }
3882
3883 /*
3884 * If that did not happen, check if a previous inode
3885 * did already create the dir.
3886 */
3887 if (!ret)
3888 ret = did_create_dir(sctx, cur->dir);
3889 if (ret < 0)
3890 goto out;
3891 if (!ret) {
3892 ret = send_create_inode(sctx, cur->dir);
3893 if (ret < 0)
3894 goto out;
3895 }
3896 }
3897
3898 /*
3899 * Check if this new ref would overwrite the first ref of
3900 * another unprocessed inode. If yes, orphanize the
3901 * overwritten inode. If we find an overwritten ref that is
3902 * not the first ref, simply unlink it.
3903 */
3904 ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen,
3905 cur->name, cur->name_len,
3906 &ow_inode, &ow_gen, &ow_mode);
3907 if (ret < 0)
3908 goto out;
3909 if (ret) {
3910 ret = is_first_ref(sctx->parent_root,
3911 ow_inode, cur->dir, cur->name,
3912 cur->name_len);
3913 if (ret < 0)
3914 goto out;
3915 if (ret) {
3916 struct name_cache_entry *nce;
3917 struct waiting_dir_move *wdm;
3918
3919 ret = orphanize_inode(sctx, ow_inode, ow_gen,
3920 cur->full_path);
3921 if (ret < 0)
3922 goto out;
3923 if (S_ISDIR(ow_mode))
3924 orphanized_dir = true;
3925
3926 /*
3927 * If ow_inode has its rename operation delayed
3928 * make sure that its orphanized name is used in
3929 * the source path when performing its rename
3930 * operation.
3931 */
3932 if (is_waiting_for_move(sctx, ow_inode)) {
3933 wdm = get_waiting_dir_move(sctx,
3934 ow_inode);
3935 ASSERT(wdm);
3936 wdm->orphanized = true;
3937 }
3938
3939 /*
3940 * Make sure we clear our orphanized inode's
3941 * name from the name cache. This is because the
3942 * inode ow_inode might be an ancestor of some
3943 * other inode that will be orphanized as well
3944 * later and has an inode number greater than
3945 * sctx->send_progress. We need to prevent
3946 * future name lookups from using the old name
3947 * and get instead the orphan name.
3948 */
3949 nce = name_cache_search(sctx, ow_inode, ow_gen);
3950 if (nce) {
3951 name_cache_delete(sctx, nce);
3952 kfree(nce);
3953 }
3954
3955 /*
3956 * ow_inode might currently be an ancestor of
3957 * cur_ino, therefore compute valid_path (the
3958 * current path of cur_ino) again because it
3959 * might contain the pre-orphanization name of
3960 * ow_inode, which is no longer valid.
3961 */
3962 ret = is_ancestor(sctx->parent_root,
3963 ow_inode, ow_gen,
3964 sctx->cur_ino, NULL);
3965 if (ret > 0) {
3966 orphanized_ancestor = true;
3967 fs_path_reset(valid_path);
3968 ret = get_cur_path(sctx, sctx->cur_ino,
3969 sctx->cur_inode_gen,
3970 valid_path);
3971 }
3972 if (ret < 0)
3973 goto out;
3974 } else {
3975 ret = send_unlink(sctx, cur->full_path);
3976 if (ret < 0)
3977 goto out;
3978 }
3979 }
3980
3981 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root) {
3982 ret = wait_for_dest_dir_move(sctx, cur, is_orphan);
3983 if (ret < 0)
3984 goto out;
3985 if (ret == 1) {
3986 can_rename = false;
3987 *pending_move = 1;
3988 }
3989 }
3990
3991 if (S_ISDIR(sctx->cur_inode_mode) && sctx->parent_root &&
3992 can_rename) {
3993 ret = wait_for_parent_move(sctx, cur, is_orphan);
3994 if (ret < 0)
3995 goto out;
3996 if (ret == 1) {
3997 can_rename = false;
3998 *pending_move = 1;
3999 }
4000 }
4001
4002 /*
4003 * link/move the ref to the new place. If we have an orphan
4004 * inode, move it and update valid_path. If not, link or move
4005 * it depending on the inode mode.
4006 */
4007 if (is_orphan && can_rename) {
4008 ret = send_rename(sctx, valid_path, cur->full_path);
4009 if (ret < 0)
4010 goto out;
4011 is_orphan = 0;
4012 ret = fs_path_copy(valid_path, cur->full_path);
4013 if (ret < 0)
4014 goto out;
4015 } else if (can_rename) {
4016 if (S_ISDIR(sctx->cur_inode_mode)) {
4017 /*
4018 * Dirs can't be linked, so move it. For moved
4019 * dirs, we always have one new and one deleted
4020 * ref. The deleted ref is ignored later.
4021 */
4022 ret = send_rename(sctx, valid_path,
4023 cur->full_path);
4024 if (!ret)
4025 ret = fs_path_copy(valid_path,
4026 cur->full_path);
4027 if (ret < 0)
4028 goto out;
4029 } else {
4030 /*
4031 * We might have previously orphanized an inode
4032 * which is an ancestor of our current inode,
4033 * so our reference's full path, which was
4034 * computed before any such orphanizations, must
4035 * be updated.
4036 */
4037 if (orphanized_dir) {
4038 ret = update_ref_path(sctx, cur);
4039 if (ret < 0)
4040 goto out;
4041 }
4042 ret = send_link(sctx, cur->full_path,
4043 valid_path);
4044 if (ret < 0)
4045 goto out;
4046 }
4047 }
4048 ret = dup_ref(cur, &check_dirs);
4049 if (ret < 0)
4050 goto out;
4051 }
4052
4053 if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) {
4054 /*
4055 * Check if we can already rmdir the directory. If not,
4056 * orphanize it. For every dir item inside that gets deleted
4057 * later, we do this check again and rmdir it then if possible.
4058 * See the use of check_dirs for more details.
4059 */
4060 ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_inode_gen,
4061 sctx->cur_ino);
4062 if (ret < 0)
4063 goto out;
4064 if (ret) {
4065 ret = send_rmdir(sctx, valid_path);
4066 if (ret < 0)
4067 goto out;
4068 } else if (!is_orphan) {
4069 ret = orphanize_inode(sctx, sctx->cur_ino,
4070 sctx->cur_inode_gen, valid_path);
4071 if (ret < 0)
4072 goto out;
4073 is_orphan = 1;
4074 }
4075
4076 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4077 ret = dup_ref(cur, &check_dirs);
4078 if (ret < 0)
4079 goto out;
4080 }
4081 } else if (S_ISDIR(sctx->cur_inode_mode) &&
4082 !list_empty(&sctx->deleted_refs)) {
4083 /*
4084 * We have a moved dir. Add the old parent to check_dirs
4085 */
4086 cur = list_entry(sctx->deleted_refs.next, struct recorded_ref,
4087 list);
4088 ret = dup_ref(cur, &check_dirs);
4089 if (ret < 0)
4090 goto out;
4091 } else if (!S_ISDIR(sctx->cur_inode_mode)) {
4092 /*
4093 * We have a non dir inode. Go through all deleted refs and
4094 * unlink them if they were not already overwritten by other
4095 * inodes.
4096 */
4097 list_for_each_entry(cur, &sctx->deleted_refs, list) {
4098 ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen,
4099 sctx->cur_ino, sctx->cur_inode_gen,
4100 cur->name, cur->name_len);
4101 if (ret < 0)
4102 goto out;
4103 if (!ret) {
4104 /*
4105 * If we orphanized any ancestor before, we need
4106 * to recompute the full path for deleted names,
4107 * since any such path was computed before we
4108 * processed any references and orphanized any
4109 * ancestor inode.
4110 */
4111 if (orphanized_ancestor) {
4112 ret = update_ref_path(sctx, cur);
4113 if (ret < 0)
4114 goto out;
4115 }
4116 ret = send_unlink(sctx, cur->full_path);
4117 if (ret < 0)
4118 goto out;
4119 }
4120 ret = dup_ref(cur, &check_dirs);
4121 if (ret < 0)
4122 goto out;
4123 }
4124 /*
4125 * If the inode is still orphan, unlink the orphan. This may
4126 * happen when a previous inode did overwrite the first ref
4127 * of this inode and no new refs were added for the current
4128 * inode. Unlinking does not mean that the inode is deleted in
4129 * all cases. There may still be links to this inode in other
4130 * places.
4131 */
4132 if (is_orphan) {
4133 ret = send_unlink(sctx, valid_path);
4134 if (ret < 0)
4135 goto out;
4136 }
4137 }
4138
4139 /*
4140 * We did collect all parent dirs where cur_inode was once located. We
4141 * now go through all these dirs and check if they are pending for
4142 * deletion and if it's finally possible to perform the rmdir now.
4143 * We also update the inode stats of the parent dirs here.
4144 */
4145 list_for_each_entry(cur, &check_dirs, list) {
4146 /*
4147 * In case we had refs into dirs that were not processed yet,
4148 * we don't need to do the utime and rmdir logic for these dirs.
4149 * The dir will be processed later.
4150 */
4151 if (cur->dir > sctx->cur_ino)
4152 continue;
4153
4154 ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
4155 if (ret < 0)
4156 goto out;
4157
4158 if (ret == inode_state_did_create ||
4159 ret == inode_state_no_change) {
4160 /* TODO delayed utimes */
4161 ret = send_utimes(sctx, cur->dir, cur->dir_gen);
4162 if (ret < 0)
4163 goto out;
4164 } else if (ret == inode_state_did_delete &&
4165 cur->dir != last_dir_ino_rm) {
4166 ret = can_rmdir(sctx, cur->dir, cur->dir_gen,
4167 sctx->cur_ino);
4168 if (ret < 0)
4169 goto out;
4170 if (ret) {
4171 ret = get_cur_path(sctx, cur->dir,
4172 cur->dir_gen, valid_path);
4173 if (ret < 0)
4174 goto out;
4175 ret = send_rmdir(sctx, valid_path);
4176 if (ret < 0)
4177 goto out;
4178 last_dir_ino_rm = cur->dir;
4179 }
4180 }
4181 }
4182
4183 ret = 0;
4184
4185out:
4186 __free_recorded_refs(&check_dirs);
4187 free_recorded_refs(sctx);
4188 fs_path_free(valid_path);
4189 return ret;
4190}
4191
4192static int record_ref(struct btrfs_root *root, u64 dir, struct fs_path *name,
4193 void *ctx, struct list_head *refs)
4194{
4195 int ret = 0;
4196 struct send_ctx *sctx = ctx;
4197 struct fs_path *p;
4198 u64 gen;
4199
4200 p = fs_path_alloc();
4201 if (!p)
4202 return -ENOMEM;
4203
4204 ret = get_inode_info(root, dir, NULL, &gen, NULL, NULL,
4205 NULL, NULL);
4206 if (ret < 0)
4207 goto out;
4208
4209 ret = get_cur_path(sctx, dir, gen, p);
4210 if (ret < 0)
4211 goto out;
4212 ret = fs_path_add_path(p, name);
4213 if (ret < 0)
4214 goto out;
4215
4216 ret = __record_ref(refs, dir, gen, p);
4217
4218out:
4219 if (ret)
4220 fs_path_free(p);
4221 return ret;
4222}
4223
4224static int __record_new_ref(int num, u64 dir, int index,
4225 struct fs_path *name,
4226 void *ctx)
4227{
4228 struct send_ctx *sctx = ctx;
4229 return record_ref(sctx->send_root, dir, name, ctx, &sctx->new_refs);
4230}
4231
4232
4233static int __record_deleted_ref(int num, u64 dir, int index,
4234 struct fs_path *name,
4235 void *ctx)
4236{
4237 struct send_ctx *sctx = ctx;
4238 return record_ref(sctx->parent_root, dir, name, ctx,
4239 &sctx->deleted_refs);
4240}
4241
4242static int record_new_ref(struct send_ctx *sctx)
4243{
4244 int ret;
4245
4246 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4247 sctx->cmp_key, 0, __record_new_ref, sctx);
4248 if (ret < 0)
4249 goto out;
4250 ret = 0;
4251
4252out:
4253 return ret;
4254}
4255
4256static int record_deleted_ref(struct send_ctx *sctx)
4257{
4258 int ret;
4259
4260 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4261 sctx->cmp_key, 0, __record_deleted_ref, sctx);
4262 if (ret < 0)
4263 goto out;
4264 ret = 0;
4265
4266out:
4267 return ret;
4268}
4269
4270struct find_ref_ctx {
4271 u64 dir;
4272 u64 dir_gen;
4273 struct btrfs_root *root;
4274 struct fs_path *name;
4275 int found_idx;
4276};
4277
4278static int __find_iref(int num, u64 dir, int index,
4279 struct fs_path *name,
4280 void *ctx_)
4281{
4282 struct find_ref_ctx *ctx = ctx_;
4283 u64 dir_gen;
4284 int ret;
4285
4286 if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) &&
4287 strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) {
4288 /*
4289 * To avoid doing extra lookups we'll only do this if everything
4290 * else matches.
4291 */
4292 ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL,
4293 NULL, NULL, NULL);
4294 if (ret)
4295 return ret;
4296 if (dir_gen != ctx->dir_gen)
4297 return 0;
4298 ctx->found_idx = num;
4299 return 1;
4300 }
4301 return 0;
4302}
4303
4304static int find_iref(struct btrfs_root *root,
4305 struct btrfs_path *path,
4306 struct btrfs_key *key,
4307 u64 dir, u64 dir_gen, struct fs_path *name)
4308{
4309 int ret;
4310 struct find_ref_ctx ctx;
4311
4312 ctx.dir = dir;
4313 ctx.name = name;
4314 ctx.dir_gen = dir_gen;
4315 ctx.found_idx = -1;
4316 ctx.root = root;
4317
4318 ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx);
4319 if (ret < 0)
4320 return ret;
4321
4322 if (ctx.found_idx == -1)
4323 return -ENOENT;
4324
4325 return ctx.found_idx;
4326}
4327
4328static int __record_changed_new_ref(int num, u64 dir, int index,
4329 struct fs_path *name,
4330 void *ctx)
4331{
4332 u64 dir_gen;
4333 int ret;
4334 struct send_ctx *sctx = ctx;
4335
4336 ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL,
4337 NULL, NULL, NULL);
4338 if (ret)
4339 return ret;
4340
4341 ret = find_iref(sctx->parent_root, sctx->right_path,
4342 sctx->cmp_key, dir, dir_gen, name);
4343 if (ret == -ENOENT)
4344 ret = __record_new_ref(num, dir, index, name, sctx);
4345 else if (ret > 0)
4346 ret = 0;
4347
4348 return ret;
4349}
4350
4351static int __record_changed_deleted_ref(int num, u64 dir, int index,
4352 struct fs_path *name,
4353 void *ctx)
4354{
4355 u64 dir_gen;
4356 int ret;
4357 struct send_ctx *sctx = ctx;
4358
4359 ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL,
4360 NULL, NULL, NULL);
4361 if (ret)
4362 return ret;
4363
4364 ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key,
4365 dir, dir_gen, name);
4366 if (ret == -ENOENT)
4367 ret = __record_deleted_ref(num, dir, index, name, sctx);
4368 else if (ret > 0)
4369 ret = 0;
4370
4371 return ret;
4372}
4373
4374static int record_changed_ref(struct send_ctx *sctx)
4375{
4376 int ret = 0;
4377
4378 ret = iterate_inode_ref(sctx->send_root, sctx->left_path,
4379 sctx->cmp_key, 0, __record_changed_new_ref, sctx);
4380 if (ret < 0)
4381 goto out;
4382 ret = iterate_inode_ref(sctx->parent_root, sctx->right_path,
4383 sctx->cmp_key, 0, __record_changed_deleted_ref, sctx);
4384 if (ret < 0)
4385 goto out;
4386 ret = 0;
4387
4388out:
4389 return ret;
4390}
4391
4392/*
4393 * Record and process all refs at once. Needed when an inode changes the
4394 * generation number, which means that it was deleted and recreated.
4395 */
4396static int process_all_refs(struct send_ctx *sctx,
4397 enum btrfs_compare_tree_result cmd)
4398{
4399 int ret;
4400 struct btrfs_root *root;
4401 struct btrfs_path *path;
4402 struct btrfs_key key;
4403 struct btrfs_key found_key;
4404 struct extent_buffer *eb;
4405 int slot;
4406 iterate_inode_ref_t cb;
4407 int pending_move = 0;
4408
4409 path = alloc_path_for_send();
4410 if (!path)
4411 return -ENOMEM;
4412
4413 if (cmd == BTRFS_COMPARE_TREE_NEW) {
4414 root = sctx->send_root;
4415 cb = __record_new_ref;
4416 } else if (cmd == BTRFS_COMPARE_TREE_DELETED) {
4417 root = sctx->parent_root;
4418 cb = __record_deleted_ref;
4419 } else {
4420 btrfs_err(sctx->send_root->fs_info,
4421 "Wrong command %d in process_all_refs", cmd);
4422 ret = -EINVAL;
4423 goto out;
4424 }
4425
4426 key.objectid = sctx->cmp_key->objectid;
4427 key.type = BTRFS_INODE_REF_KEY;
4428 key.offset = 0;
4429 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4430 if (ret < 0)
4431 goto out;
4432
4433 while (1) {
4434 eb = path->nodes[0];
4435 slot = path->slots[0];
4436 if (slot >= btrfs_header_nritems(eb)) {
4437 ret = btrfs_next_leaf(root, path);
4438 if (ret < 0)
4439 goto out;
4440 else if (ret > 0)
4441 break;
4442 continue;
4443 }
4444
4445 btrfs_item_key_to_cpu(eb, &found_key, slot);
4446
4447 if (found_key.objectid != key.objectid ||
4448 (found_key.type != BTRFS_INODE_REF_KEY &&
4449 found_key.type != BTRFS_INODE_EXTREF_KEY))
4450 break;
4451
4452 ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx);
4453 if (ret < 0)
4454 goto out;
4455
4456 path->slots[0]++;
4457 }
4458 btrfs_release_path(path);
4459
4460 /*
4461 * We don't actually care about pending_move as we are simply
4462 * re-creating this inode and will be rename'ing it into place once we
4463 * rename the parent directory.
4464 */
4465 ret = process_recorded_refs(sctx, &pending_move);
4466out:
4467 btrfs_free_path(path);
4468 return ret;
4469}
4470
4471static int send_set_xattr(struct send_ctx *sctx,
4472 struct fs_path *path,
4473 const char *name, int name_len,
4474 const char *data, int data_len)
4475{
4476 int ret = 0;
4477
4478 ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR);
4479 if (ret < 0)
4480 goto out;
4481
4482 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4483 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4484 TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len);
4485
4486 ret = send_cmd(sctx);
4487
4488tlv_put_failure:
4489out:
4490 return ret;
4491}
4492
4493static int send_remove_xattr(struct send_ctx *sctx,
4494 struct fs_path *path,
4495 const char *name, int name_len)
4496{
4497 int ret = 0;
4498
4499 ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR);
4500 if (ret < 0)
4501 goto out;
4502
4503 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path);
4504 TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len);
4505
4506 ret = send_cmd(sctx);
4507
4508tlv_put_failure:
4509out:
4510 return ret;
4511}
4512
4513static int __process_new_xattr(int num, struct btrfs_key *di_key,
4514 const char *name, int name_len,
4515 const char *data, int data_len,
4516 u8 type, void *ctx)
4517{
4518 int ret;
4519 struct send_ctx *sctx = ctx;
4520 struct fs_path *p;
4521 struct posix_acl_xattr_header dummy_acl;
4522
4523 p = fs_path_alloc();
4524 if (!p)
4525 return -ENOMEM;
4526
4527 /*
4528 * This hack is needed because empty acls are stored as zero byte
4529 * data in xattrs. Problem with that is, that receiving these zero byte
4530 * acls will fail later. To fix this, we send a dummy acl list that
4531 * only contains the version number and no entries.
4532 */
4533 if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) ||
4534 !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) {
4535 if (data_len == 0) {
4536 dummy_acl.a_version =
4537 cpu_to_le32(POSIX_ACL_XATTR_VERSION);
4538 data = (char *)&dummy_acl;
4539 data_len = sizeof(dummy_acl);
4540 }
4541 }
4542
4543 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4544 if (ret < 0)
4545 goto out;
4546
4547 ret = send_set_xattr(sctx, p, name, name_len, data, data_len);
4548
4549out:
4550 fs_path_free(p);
4551 return ret;
4552}
4553
4554static int __process_deleted_xattr(int num, struct btrfs_key *di_key,
4555 const char *name, int name_len,
4556 const char *data, int data_len,
4557 u8 type, void *ctx)
4558{
4559 int ret;
4560 struct send_ctx *sctx = ctx;
4561 struct fs_path *p;
4562
4563 p = fs_path_alloc();
4564 if (!p)
4565 return -ENOMEM;
4566
4567 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4568 if (ret < 0)
4569 goto out;
4570
4571 ret = send_remove_xattr(sctx, p, name, name_len);
4572
4573out:
4574 fs_path_free(p);
4575 return ret;
4576}
4577
4578static int process_new_xattr(struct send_ctx *sctx)
4579{
4580 int ret = 0;
4581
4582 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4583 __process_new_xattr, sctx);
4584
4585 return ret;
4586}
4587
4588static int process_deleted_xattr(struct send_ctx *sctx)
4589{
4590 return iterate_dir_item(sctx->parent_root, sctx->right_path,
4591 __process_deleted_xattr, sctx);
4592}
4593
4594struct find_xattr_ctx {
4595 const char *name;
4596 int name_len;
4597 int found_idx;
4598 char *found_data;
4599 int found_data_len;
4600};
4601
4602static int __find_xattr(int num, struct btrfs_key *di_key,
4603 const char *name, int name_len,
4604 const char *data, int data_len,
4605 u8 type, void *vctx)
4606{
4607 struct find_xattr_ctx *ctx = vctx;
4608
4609 if (name_len == ctx->name_len &&
4610 strncmp(name, ctx->name, name_len) == 0) {
4611 ctx->found_idx = num;
4612 ctx->found_data_len = data_len;
4613 ctx->found_data = kmemdup(data, data_len, GFP_KERNEL);
4614 if (!ctx->found_data)
4615 return -ENOMEM;
4616 return 1;
4617 }
4618 return 0;
4619}
4620
4621static int find_xattr(struct btrfs_root *root,
4622 struct btrfs_path *path,
4623 struct btrfs_key *key,
4624 const char *name, int name_len,
4625 char **data, int *data_len)
4626{
4627 int ret;
4628 struct find_xattr_ctx ctx;
4629
4630 ctx.name = name;
4631 ctx.name_len = name_len;
4632 ctx.found_idx = -1;
4633 ctx.found_data = NULL;
4634 ctx.found_data_len = 0;
4635
4636 ret = iterate_dir_item(root, path, __find_xattr, &ctx);
4637 if (ret < 0)
4638 return ret;
4639
4640 if (ctx.found_idx == -1)
4641 return -ENOENT;
4642 if (data) {
4643 *data = ctx.found_data;
4644 *data_len = ctx.found_data_len;
4645 } else {
4646 kfree(ctx.found_data);
4647 }
4648 return ctx.found_idx;
4649}
4650
4651
4652static int __process_changed_new_xattr(int num, struct btrfs_key *di_key,
4653 const char *name, int name_len,
4654 const char *data, int data_len,
4655 u8 type, void *ctx)
4656{
4657 int ret;
4658 struct send_ctx *sctx = ctx;
4659 char *found_data = NULL;
4660 int found_data_len = 0;
4661
4662 ret = find_xattr(sctx->parent_root, sctx->right_path,
4663 sctx->cmp_key, name, name_len, &found_data,
4664 &found_data_len);
4665 if (ret == -ENOENT) {
4666 ret = __process_new_xattr(num, di_key, name, name_len, data,
4667 data_len, type, ctx);
4668 } else if (ret >= 0) {
4669 if (data_len != found_data_len ||
4670 memcmp(data, found_data, data_len)) {
4671 ret = __process_new_xattr(num, di_key, name, name_len,
4672 data, data_len, type, ctx);
4673 } else {
4674 ret = 0;
4675 }
4676 }
4677
4678 kfree(found_data);
4679 return ret;
4680}
4681
4682static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key,
4683 const char *name, int name_len,
4684 const char *data, int data_len,
4685 u8 type, void *ctx)
4686{
4687 int ret;
4688 struct send_ctx *sctx = ctx;
4689
4690 ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key,
4691 name, name_len, NULL, NULL);
4692 if (ret == -ENOENT)
4693 ret = __process_deleted_xattr(num, di_key, name, name_len, data,
4694 data_len, type, ctx);
4695 else if (ret >= 0)
4696 ret = 0;
4697
4698 return ret;
4699}
4700
4701static int process_changed_xattr(struct send_ctx *sctx)
4702{
4703 int ret = 0;
4704
4705 ret = iterate_dir_item(sctx->send_root, sctx->left_path,
4706 __process_changed_new_xattr, sctx);
4707 if (ret < 0)
4708 goto out;
4709 ret = iterate_dir_item(sctx->parent_root, sctx->right_path,
4710 __process_changed_deleted_xattr, sctx);
4711
4712out:
4713 return ret;
4714}
4715
4716static int process_all_new_xattrs(struct send_ctx *sctx)
4717{
4718 int ret;
4719 struct btrfs_root *root;
4720 struct btrfs_path *path;
4721 struct btrfs_key key;
4722 struct btrfs_key found_key;
4723 struct extent_buffer *eb;
4724 int slot;
4725
4726 path = alloc_path_for_send();
4727 if (!path)
4728 return -ENOMEM;
4729
4730 root = sctx->send_root;
4731
4732 key.objectid = sctx->cmp_key->objectid;
4733 key.type = BTRFS_XATTR_ITEM_KEY;
4734 key.offset = 0;
4735 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4736 if (ret < 0)
4737 goto out;
4738
4739 while (1) {
4740 eb = path->nodes[0];
4741 slot = path->slots[0];
4742 if (slot >= btrfs_header_nritems(eb)) {
4743 ret = btrfs_next_leaf(root, path);
4744 if (ret < 0) {
4745 goto out;
4746 } else if (ret > 0) {
4747 ret = 0;
4748 break;
4749 }
4750 continue;
4751 }
4752
4753 btrfs_item_key_to_cpu(eb, &found_key, slot);
4754 if (found_key.objectid != key.objectid ||
4755 found_key.type != key.type) {
4756 ret = 0;
4757 goto out;
4758 }
4759
4760 ret = iterate_dir_item(root, path, __process_new_xattr, sctx);
4761 if (ret < 0)
4762 goto out;
4763
4764 path->slots[0]++;
4765 }
4766
4767out:
4768 btrfs_free_path(path);
4769 return ret;
4770}
4771
4772static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
4773{
4774 struct btrfs_root *root = sctx->send_root;
4775 struct btrfs_fs_info *fs_info = root->fs_info;
4776 struct inode *inode;
4777 struct page *page;
4778 char *addr;
4779 struct btrfs_key key;
4780 pgoff_t index = offset >> PAGE_SHIFT;
4781 pgoff_t last_index;
4782 unsigned pg_offset = offset & ~PAGE_MASK;
4783 ssize_t ret = 0;
4784
4785 key.objectid = sctx->cur_ino;
4786 key.type = BTRFS_INODE_ITEM_KEY;
4787 key.offset = 0;
4788
4789 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4790 if (IS_ERR(inode))
4791 return PTR_ERR(inode);
4792
4793 if (offset + len > i_size_read(inode)) {
4794 if (offset > i_size_read(inode))
4795 len = 0;
4796 else
4797 len = offset - i_size_read(inode);
4798 }
4799 if (len == 0)
4800 goto out;
4801
4802 last_index = (offset + len - 1) >> PAGE_SHIFT;
4803
4804 /* initial readahead */
4805 memset(&sctx->ra, 0, sizeof(struct file_ra_state));
4806 file_ra_state_init(&sctx->ra, inode->i_mapping);
4807
4808 while (index <= last_index) {
4809 unsigned cur_len = min_t(unsigned, len,
4810 PAGE_SIZE - pg_offset);
4811
4812 page = find_lock_page(inode->i_mapping, index);
4813 if (!page) {
4814 page_cache_sync_readahead(inode->i_mapping, &sctx->ra,
4815 NULL, index, last_index + 1 - index);
4816
4817 page = find_or_create_page(inode->i_mapping, index,
4818 GFP_KERNEL);
4819 if (!page) {
4820 ret = -ENOMEM;
4821 break;
4822 }
4823 }
4824
4825 if (PageReadahead(page)) {
4826 page_cache_async_readahead(inode->i_mapping, &sctx->ra,
4827 NULL, page, index, last_index + 1 - index);
4828 }
4829
4830 if (!PageUptodate(page)) {
4831 btrfs_readpage(NULL, page);
4832 lock_page(page);
4833 if (!PageUptodate(page)) {
4834 unlock_page(page);
4835 put_page(page);
4836 ret = -EIO;
4837 break;
4838 }
4839 }
4840
4841 addr = kmap(page);
4842 memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len);
4843 kunmap(page);
4844 unlock_page(page);
4845 put_page(page);
4846 index++;
4847 pg_offset = 0;
4848 len -= cur_len;
4849 ret += cur_len;
4850 }
4851out:
4852 iput(inode);
4853 return ret;
4854}
4855
4856/*
4857 * Read some bytes from the current inode/file and send a write command to
4858 * user space.
4859 */
4860static int send_write(struct send_ctx *sctx, u64 offset, u32 len)
4861{
4862 struct btrfs_fs_info *fs_info = sctx->send_root->fs_info;
4863 int ret = 0;
4864 struct fs_path *p;
4865 ssize_t num_read = 0;
4866
4867 p = fs_path_alloc();
4868 if (!p)
4869 return -ENOMEM;
4870
4871 btrfs_debug(fs_info, "send_write offset=%llu, len=%d", offset, len);
4872
4873 num_read = fill_read_buf(sctx, offset, len);
4874 if (num_read <= 0) {
4875 if (num_read < 0)
4876 ret = num_read;
4877 goto out;
4878 }
4879
4880 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
4881 if (ret < 0)
4882 goto out;
4883
4884 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4885 if (ret < 0)
4886 goto out;
4887
4888 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4889 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4890 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read);
4891
4892 ret = send_cmd(sctx);
4893
4894tlv_put_failure:
4895out:
4896 fs_path_free(p);
4897 if (ret < 0)
4898 return ret;
4899 return num_read;
4900}
4901
4902/*
4903 * Send a clone command to user space.
4904 */
4905static int send_clone(struct send_ctx *sctx,
4906 u64 offset, u32 len,
4907 struct clone_root *clone_root)
4908{
4909 int ret = 0;
4910 struct fs_path *p;
4911 u64 gen;
4912
4913 btrfs_debug(sctx->send_root->fs_info,
4914 "send_clone offset=%llu, len=%d, clone_root=%llu, clone_inode=%llu, clone_offset=%llu",
4915 offset, len, clone_root->root->objectid, clone_root->ino,
4916 clone_root->offset);
4917
4918 p = fs_path_alloc();
4919 if (!p)
4920 return -ENOMEM;
4921
4922 ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE);
4923 if (ret < 0)
4924 goto out;
4925
4926 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4927 if (ret < 0)
4928 goto out;
4929
4930 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4931 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len);
4932 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4933
4934 if (clone_root->root == sctx->send_root) {
4935 ret = get_inode_info(sctx->send_root, clone_root->ino, NULL,
4936 &gen, NULL, NULL, NULL, NULL);
4937 if (ret < 0)
4938 goto out;
4939 ret = get_cur_path(sctx, clone_root->ino, gen, p);
4940 } else {
4941 ret = get_inode_path(clone_root->root, clone_root->ino, p);
4942 }
4943 if (ret < 0)
4944 goto out;
4945
4946 /*
4947 * If the parent we're using has a received_uuid set then use that as
4948 * our clone source as that is what we will look for when doing a
4949 * receive.
4950 *
4951 * This covers the case that we create a snapshot off of a received
4952 * subvolume and then use that as the parent and try to receive on a
4953 * different host.
4954 */
4955 if (!btrfs_is_empty_uuid(clone_root->root->root_item.received_uuid))
4956 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4957 clone_root->root->root_item.received_uuid);
4958 else
4959 TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID,
4960 clone_root->root->root_item.uuid);
4961 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID,
4962 le64_to_cpu(clone_root->root->root_item.ctransid));
4963 TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p);
4964 TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET,
4965 clone_root->offset);
4966
4967 ret = send_cmd(sctx);
4968
4969tlv_put_failure:
4970out:
4971 fs_path_free(p);
4972 return ret;
4973}
4974
4975/*
4976 * Send an update extent command to user space.
4977 */
4978static int send_update_extent(struct send_ctx *sctx,
4979 u64 offset, u32 len)
4980{
4981 int ret = 0;
4982 struct fs_path *p;
4983
4984 p = fs_path_alloc();
4985 if (!p)
4986 return -ENOMEM;
4987
4988 ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT);
4989 if (ret < 0)
4990 goto out;
4991
4992 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
4993 if (ret < 0)
4994 goto out;
4995
4996 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
4997 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
4998 TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len);
4999
5000 ret = send_cmd(sctx);
5001
5002tlv_put_failure:
5003out:
5004 fs_path_free(p);
5005 return ret;
5006}
5007
5008static int send_hole(struct send_ctx *sctx, u64 end)
5009{
5010 struct fs_path *p = NULL;
5011 u64 offset = sctx->cur_inode_last_extent;
5012 u64 len;
5013 int ret = 0;
5014
5015 /*
5016 * A hole that starts at EOF or beyond it. Since we do not yet support
5017 * fallocate (for extent preallocation and hole punching), sending a
5018 * write of zeroes starting at EOF or beyond would later require issuing
5019 * a truncate operation which would undo the write and achieve nothing.
5020 */
5021 if (offset >= sctx->cur_inode_size)
5022 return 0;
5023
5024 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5025 return send_update_extent(sctx, offset, end - offset);
5026
5027 p = fs_path_alloc();
5028 if (!p)
5029 return -ENOMEM;
5030 ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p);
5031 if (ret < 0)
5032 goto tlv_put_failure;
5033 memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE);
5034 while (offset < end) {
5035 len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE);
5036
5037 ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE);
5038 if (ret < 0)
5039 break;
5040 TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p);
5041 TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset);
5042 TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len);
5043 ret = send_cmd(sctx);
5044 if (ret < 0)
5045 break;
5046 offset += len;
5047 }
5048 sctx->cur_inode_next_write_offset = offset;
5049tlv_put_failure:
5050 fs_path_free(p);
5051 return ret;
5052}
5053
5054static int send_extent_data(struct send_ctx *sctx,
5055 const u64 offset,
5056 const u64 len)
5057{
5058 u64 sent = 0;
5059
5060 if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA)
5061 return send_update_extent(sctx, offset, len);
5062
5063 while (sent < len) {
5064 u64 size = len - sent;
5065 int ret;
5066
5067 if (size > BTRFS_SEND_READ_SIZE)
5068 size = BTRFS_SEND_READ_SIZE;
5069 ret = send_write(sctx, offset + sent, size);
5070 if (ret < 0)
5071 return ret;
5072 if (!ret)
5073 break;
5074 sent += ret;
5075 }
5076 return 0;
5077}
5078
5079static int clone_range(struct send_ctx *sctx,
5080 struct clone_root *clone_root,
5081 const u64 disk_byte,
5082 u64 data_offset,
5083 u64 offset,
5084 u64 len)
5085{
5086 struct btrfs_path *path;
5087 struct btrfs_key key;
5088 int ret;
5089
5090 /*
5091 * Prevent cloning from a zero offset with a length matching the sector
5092 * size because in some scenarios this will make the receiver fail.
5093 *
5094 * For example, if in the source filesystem the extent at offset 0
5095 * has a length of sectorsize and it was written using direct IO, then
5096 * it can never be an inline extent (even if compression is enabled).
5097 * Then this extent can be cloned in the original filesystem to a non
5098 * zero file offset, but it may not be possible to clone in the
5099 * destination filesystem because it can be inlined due to compression
5100 * on the destination filesystem (as the receiver's write operations are
5101 * always done using buffered IO). The same happens when the original
5102 * filesystem does not have compression enabled but the destination
5103 * filesystem has.
5104 */
5105 if (clone_root->offset == 0 &&
5106 len == sctx->send_root->fs_info->sectorsize)
5107 return send_extent_data(sctx, offset, len);
5108
5109 path = alloc_path_for_send();
5110 if (!path)
5111 return -ENOMEM;
5112
5113 /*
5114 * We can't send a clone operation for the entire range if we find
5115 * extent items in the respective range in the source file that
5116 * refer to different extents or if we find holes.
5117 * So check for that and do a mix of clone and regular write/copy
5118 * operations if needed.
5119 *
5120 * Example:
5121 *
5122 * mkfs.btrfs -f /dev/sda
5123 * mount /dev/sda /mnt
5124 * xfs_io -f -c "pwrite -S 0xaa 0K 100K" /mnt/foo
5125 * cp --reflink=always /mnt/foo /mnt/bar
5126 * xfs_io -c "pwrite -S 0xbb 50K 50K" /mnt/foo
5127 * btrfs subvolume snapshot -r /mnt /mnt/snap
5128 *
5129 * If when we send the snapshot and we are processing file bar (which
5130 * has a higher inode number than foo) we blindly send a clone operation
5131 * for the [0, 100K[ range from foo to bar, the receiver ends up getting
5132 * a file bar that matches the content of file foo - iow, doesn't match
5133 * the content from bar in the original filesystem.
5134 */
5135 key.objectid = clone_root->ino;
5136 key.type = BTRFS_EXTENT_DATA_KEY;
5137 key.offset = clone_root->offset;
5138 ret = btrfs_search_slot(NULL, clone_root->root, &key, path, 0, 0);
5139 if (ret < 0)
5140 goto out;
5141 if (ret > 0 && path->slots[0] > 0) {
5142 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
5143 if (key.objectid == clone_root->ino &&
5144 key.type == BTRFS_EXTENT_DATA_KEY)
5145 path->slots[0]--;
5146 }
5147
5148 while (true) {
5149 struct extent_buffer *leaf = path->nodes[0];
5150 int slot = path->slots[0];
5151 struct btrfs_file_extent_item *ei;
5152 u8 type;
5153 u64 ext_len;
5154 u64 clone_len;
5155
5156 if (slot >= btrfs_header_nritems(leaf)) {
5157 ret = btrfs_next_leaf(clone_root->root, path);
5158 if (ret < 0)
5159 goto out;
5160 else if (ret > 0)
5161 break;
5162 continue;
5163 }
5164
5165 btrfs_item_key_to_cpu(leaf, &key, slot);
5166
5167 /*
5168 * We might have an implicit trailing hole (NO_HOLES feature
5169 * enabled). We deal with it after leaving this loop.
5170 */
5171 if (key.objectid != clone_root->ino ||
5172 key.type != BTRFS_EXTENT_DATA_KEY)
5173 break;
5174
5175 ei = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5176 type = btrfs_file_extent_type(leaf, ei);
5177 if (type == BTRFS_FILE_EXTENT_INLINE) {
5178 ext_len = btrfs_file_extent_ram_bytes(leaf, ei);
5179 ext_len = PAGE_ALIGN(ext_len);
5180 } else {
5181 ext_len = btrfs_file_extent_num_bytes(leaf, ei);
5182 }
5183
5184 if (key.offset + ext_len <= clone_root->offset)
5185 goto next;
5186
5187 if (key.offset > clone_root->offset) {
5188 /* Implicit hole, NO_HOLES feature enabled. */
5189 u64 hole_len = key.offset - clone_root->offset;
5190
5191 if (hole_len > len)
5192 hole_len = len;
5193 ret = send_extent_data(sctx, offset, hole_len);
5194 if (ret < 0)
5195 goto out;
5196
5197 len -= hole_len;
5198 if (len == 0)
5199 break;
5200 offset += hole_len;
5201 clone_root->offset += hole_len;
5202 data_offset += hole_len;
5203 }
5204
5205 if (key.offset >= clone_root->offset + len)
5206 break;
5207
5208 clone_len = min_t(u64, ext_len, len);
5209
5210 if (btrfs_file_extent_disk_bytenr(leaf, ei) == disk_byte &&
5211 btrfs_file_extent_offset(leaf, ei) == data_offset)
5212 ret = send_clone(sctx, offset, clone_len, clone_root);
5213 else
5214 ret = send_extent_data(sctx, offset, clone_len);
5215
5216 if (ret < 0)
5217 goto out;
5218
5219 len -= clone_len;
5220 if (len == 0)
5221 break;
5222 offset += clone_len;
5223 clone_root->offset += clone_len;
5224 data_offset += clone_len;
5225next:
5226 path->slots[0]++;
5227 }
5228
5229 if (len > 0)
5230 ret = send_extent_data(sctx, offset, len);
5231 else
5232 ret = 0;
5233out:
5234 btrfs_free_path(path);
5235 return ret;
5236}
5237
5238static int send_write_or_clone(struct send_ctx *sctx,
5239 struct btrfs_path *path,
5240 struct btrfs_key *key,
5241 struct clone_root *clone_root)
5242{
5243 int ret = 0;
5244 struct btrfs_file_extent_item *ei;
5245 u64 offset = key->offset;
5246 u64 len;
5247 u8 type;
5248 u64 bs = sctx->send_root->fs_info->sb->s_blocksize;
5249
5250 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5251 struct btrfs_file_extent_item);
5252 type = btrfs_file_extent_type(path->nodes[0], ei);
5253 if (type == BTRFS_FILE_EXTENT_INLINE) {
5254 len = btrfs_file_extent_ram_bytes(path->nodes[0], ei);
5255 /*
5256 * it is possible the inline item won't cover the whole page,
5257 * but there may be items after this page. Make
5258 * sure to send the whole thing
5259 */
5260 len = PAGE_ALIGN(len);
5261 } else {
5262 len = btrfs_file_extent_num_bytes(path->nodes[0], ei);
5263 }
5264
5265 if (offset >= sctx->cur_inode_size) {
5266 ret = 0;
5267 goto out;
5268 }
5269 if (offset + len > sctx->cur_inode_size)
5270 len = sctx->cur_inode_size - offset;
5271 if (len == 0) {
5272 ret = 0;
5273 goto out;
5274 }
5275
5276 if (clone_root && IS_ALIGNED(offset + len, bs)) {
5277 u64 disk_byte;
5278 u64 data_offset;
5279
5280 disk_byte = btrfs_file_extent_disk_bytenr(path->nodes[0], ei);
5281 data_offset = btrfs_file_extent_offset(path->nodes[0], ei);
5282 ret = clone_range(sctx, clone_root, disk_byte, data_offset,
5283 offset, len);
5284 } else {
5285 ret = send_extent_data(sctx, offset, len);
5286 }
5287 sctx->cur_inode_next_write_offset = offset + len;
5288out:
5289 return ret;
5290}
5291
5292static int is_extent_unchanged(struct send_ctx *sctx,
5293 struct btrfs_path *left_path,
5294 struct btrfs_key *ekey)
5295{
5296 int ret = 0;
5297 struct btrfs_key key;
5298 struct btrfs_path *path = NULL;
5299 struct extent_buffer *eb;
5300 int slot;
5301 struct btrfs_key found_key;
5302 struct btrfs_file_extent_item *ei;
5303 u64 left_disknr;
5304 u64 right_disknr;
5305 u64 left_offset;
5306 u64 right_offset;
5307 u64 left_offset_fixed;
5308 u64 left_len;
5309 u64 right_len;
5310 u64 left_gen;
5311 u64 right_gen;
5312 u8 left_type;
5313 u8 right_type;
5314
5315 path = alloc_path_for_send();
5316 if (!path)
5317 return -ENOMEM;
5318
5319 eb = left_path->nodes[0];
5320 slot = left_path->slots[0];
5321 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5322 left_type = btrfs_file_extent_type(eb, ei);
5323
5324 if (left_type != BTRFS_FILE_EXTENT_REG) {
5325 ret = 0;
5326 goto out;
5327 }
5328 left_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5329 left_len = btrfs_file_extent_num_bytes(eb, ei);
5330 left_offset = btrfs_file_extent_offset(eb, ei);
5331 left_gen = btrfs_file_extent_generation(eb, ei);
5332
5333 /*
5334 * Following comments will refer to these graphics. L is the left
5335 * extents which we are checking at the moment. 1-8 are the right
5336 * extents that we iterate.
5337 *
5338 * |-----L-----|
5339 * |-1-|-2a-|-3-|-4-|-5-|-6-|
5340 *
5341 * |-----L-----|
5342 * |--1--|-2b-|...(same as above)
5343 *
5344 * Alternative situation. Happens on files where extents got split.
5345 * |-----L-----|
5346 * |-----------7-----------|-6-|
5347 *
5348 * Alternative situation. Happens on files which got larger.
5349 * |-----L-----|
5350 * |-8-|
5351 * Nothing follows after 8.
5352 */
5353
5354 key.objectid = ekey->objectid;
5355 key.type = BTRFS_EXTENT_DATA_KEY;
5356 key.offset = ekey->offset;
5357 ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0);
5358 if (ret < 0)
5359 goto out;
5360 if (ret) {
5361 ret = 0;
5362 goto out;
5363 }
5364
5365 /*
5366 * Handle special case where the right side has no extents at all.
5367 */
5368 eb = path->nodes[0];
5369 slot = path->slots[0];
5370 btrfs_item_key_to_cpu(eb, &found_key, slot);
5371 if (found_key.objectid != key.objectid ||
5372 found_key.type != key.type) {
5373 /* If we're a hole then just pretend nothing changed */
5374 ret = (left_disknr) ? 0 : 1;
5375 goto out;
5376 }
5377
5378 /*
5379 * We're now on 2a, 2b or 7.
5380 */
5381 key = found_key;
5382 while (key.offset < ekey->offset + left_len) {
5383 ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
5384 right_type = btrfs_file_extent_type(eb, ei);
5385 if (right_type != BTRFS_FILE_EXTENT_REG &&
5386 right_type != BTRFS_FILE_EXTENT_INLINE) {
5387 ret = 0;
5388 goto out;
5389 }
5390
5391 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5392 right_len = btrfs_file_extent_ram_bytes(eb, ei);
5393 right_len = PAGE_ALIGN(right_len);
5394 } else {
5395 right_len = btrfs_file_extent_num_bytes(eb, ei);
5396 }
5397
5398 /*
5399 * Are we at extent 8? If yes, we know the extent is changed.
5400 * This may only happen on the first iteration.
5401 */
5402 if (found_key.offset + right_len <= ekey->offset) {
5403 /* If we're a hole just pretend nothing changed */
5404 ret = (left_disknr) ? 0 : 1;
5405 goto out;
5406 }
5407
5408 /*
5409 * We just wanted to see if when we have an inline extent, what
5410 * follows it is a regular extent (wanted to check the above
5411 * condition for inline extents too). This should normally not
5412 * happen but it's possible for example when we have an inline
5413 * compressed extent representing data with a size matching
5414 * the page size (currently the same as sector size).
5415 */
5416 if (right_type == BTRFS_FILE_EXTENT_INLINE) {
5417 ret = 0;
5418 goto out;
5419 }
5420
5421 right_disknr = btrfs_file_extent_disk_bytenr(eb, ei);
5422 right_offset = btrfs_file_extent_offset(eb, ei);
5423 right_gen = btrfs_file_extent_generation(eb, ei);
5424
5425 left_offset_fixed = left_offset;
5426 if (key.offset < ekey->offset) {
5427 /* Fix the right offset for 2a and 7. */
5428 right_offset += ekey->offset - key.offset;
5429 } else {
5430 /* Fix the left offset for all behind 2a and 2b */
5431 left_offset_fixed += key.offset - ekey->offset;
5432 }
5433
5434 /*
5435 * Check if we have the same extent.
5436 */
5437 if (left_disknr != right_disknr ||
5438 left_offset_fixed != right_offset ||
5439 left_gen != right_gen) {
5440 ret = 0;
5441 goto out;
5442 }
5443
5444 /*
5445 * Go to the next extent.
5446 */
5447 ret = btrfs_next_item(sctx->parent_root, path);
5448 if (ret < 0)
5449 goto out;
5450 if (!ret) {
5451 eb = path->nodes[0];
5452 slot = path->slots[0];
5453 btrfs_item_key_to_cpu(eb, &found_key, slot);
5454 }
5455 if (ret || found_key.objectid != key.objectid ||
5456 found_key.type != key.type) {
5457 key.offset += right_len;
5458 break;
5459 }
5460 if (found_key.offset != key.offset + right_len) {
5461 ret = 0;
5462 goto out;
5463 }
5464 key = found_key;
5465 }
5466
5467 /*
5468 * We're now behind the left extent (treat as unchanged) or at the end
5469 * of the right side (treat as changed).
5470 */
5471 if (key.offset >= ekey->offset + left_len)
5472 ret = 1;
5473 else
5474 ret = 0;
5475
5476
5477out:
5478 btrfs_free_path(path);
5479 return ret;
5480}
5481
5482static int get_last_extent(struct send_ctx *sctx, u64 offset)
5483{
5484 struct btrfs_path *path;
5485 struct btrfs_root *root = sctx->send_root;
5486 struct btrfs_file_extent_item *fi;
5487 struct btrfs_key key;
5488 u64 extent_end;
5489 u8 type;
5490 int ret;
5491
5492 path = alloc_path_for_send();
5493 if (!path)
5494 return -ENOMEM;
5495
5496 sctx->cur_inode_last_extent = 0;
5497
5498 key.objectid = sctx->cur_ino;
5499 key.type = BTRFS_EXTENT_DATA_KEY;
5500 key.offset = offset;
5501 ret = btrfs_search_slot_for_read(root, &key, path, 0, 1);
5502 if (ret < 0)
5503 goto out;
5504 ret = 0;
5505 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5506 if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY)
5507 goto out;
5508
5509 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5510 struct btrfs_file_extent_item);
5511 type = btrfs_file_extent_type(path->nodes[0], fi);
5512 if (type == BTRFS_FILE_EXTENT_INLINE) {
5513 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
5514 extent_end = ALIGN(key.offset + size,
5515 sctx->send_root->fs_info->sectorsize);
5516 } else {
5517 extent_end = key.offset +
5518 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5519 }
5520 sctx->cur_inode_last_extent = extent_end;
5521out:
5522 btrfs_free_path(path);
5523 return ret;
5524}
5525
5526static int range_is_hole_in_parent(struct send_ctx *sctx,
5527 const u64 start,
5528 const u64 end)
5529{
5530 struct btrfs_path *path;
5531 struct btrfs_key key;
5532 struct btrfs_root *root = sctx->parent_root;
5533 u64 search_start = start;
5534 int ret;
5535
5536 path = alloc_path_for_send();
5537 if (!path)
5538 return -ENOMEM;
5539
5540 key.objectid = sctx->cur_ino;
5541 key.type = BTRFS_EXTENT_DATA_KEY;
5542 key.offset = search_start;
5543 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5544 if (ret < 0)
5545 goto out;
5546 if (ret > 0 && path->slots[0] > 0)
5547 path->slots[0]--;
5548
5549 while (search_start < end) {
5550 struct extent_buffer *leaf = path->nodes[0];
5551 int slot = path->slots[0];
5552 struct btrfs_file_extent_item *fi;
5553 u64 extent_end;
5554
5555 if (slot >= btrfs_header_nritems(leaf)) {
5556 ret = btrfs_next_leaf(root, path);
5557 if (ret < 0)
5558 goto out;
5559 else if (ret > 0)
5560 break;
5561 continue;
5562 }
5563
5564 btrfs_item_key_to_cpu(leaf, &key, slot);
5565 if (key.objectid < sctx->cur_ino ||
5566 key.type < BTRFS_EXTENT_DATA_KEY)
5567 goto next;
5568 if (key.objectid > sctx->cur_ino ||
5569 key.type > BTRFS_EXTENT_DATA_KEY ||
5570 key.offset >= end)
5571 break;
5572
5573 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5574 if (btrfs_file_extent_type(leaf, fi) ==
5575 BTRFS_FILE_EXTENT_INLINE) {
5576 u64 size = btrfs_file_extent_ram_bytes(leaf, fi);
5577
5578 extent_end = ALIGN(key.offset + size,
5579 root->fs_info->sectorsize);
5580 } else {
5581 extent_end = key.offset +
5582 btrfs_file_extent_num_bytes(leaf, fi);
5583 }
5584 if (extent_end <= start)
5585 goto next;
5586 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) {
5587 search_start = extent_end;
5588 goto next;
5589 }
5590 ret = 0;
5591 goto out;
5592next:
5593 path->slots[0]++;
5594 }
5595 ret = 1;
5596out:
5597 btrfs_free_path(path);
5598 return ret;
5599}
5600
5601static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path,
5602 struct btrfs_key *key)
5603{
5604 struct btrfs_file_extent_item *fi;
5605 u64 extent_end;
5606 u8 type;
5607 int ret = 0;
5608
5609 if (sctx->cur_ino != key->objectid || !need_send_hole(sctx))
5610 return 0;
5611
5612 if (sctx->cur_inode_last_extent == (u64)-1) {
5613 ret = get_last_extent(sctx, key->offset - 1);
5614 if (ret)
5615 return ret;
5616 }
5617
5618 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5619 struct btrfs_file_extent_item);
5620 type = btrfs_file_extent_type(path->nodes[0], fi);
5621 if (type == BTRFS_FILE_EXTENT_INLINE) {
5622 u64 size = btrfs_file_extent_ram_bytes(path->nodes[0], fi);
5623 extent_end = ALIGN(key->offset + size,
5624 sctx->send_root->fs_info->sectorsize);
5625 } else {
5626 extent_end = key->offset +
5627 btrfs_file_extent_num_bytes(path->nodes[0], fi);
5628 }
5629
5630 if (path->slots[0] == 0 &&
5631 sctx->cur_inode_last_extent < key->offset) {
5632 /*
5633 * We might have skipped entire leafs that contained only
5634 * file extent items for our current inode. These leafs have
5635 * a generation number smaller (older) than the one in the
5636 * current leaf and the leaf our last extent came from, and
5637 * are located between these 2 leafs.
5638 */
5639 ret = get_last_extent(sctx, key->offset - 1);
5640 if (ret)
5641 return ret;
5642 }
5643
5644 if (sctx->cur_inode_last_extent < key->offset) {
5645 ret = range_is_hole_in_parent(sctx,
5646 sctx->cur_inode_last_extent,
5647 key->offset);
5648 if (ret < 0)
5649 return ret;
5650 else if (ret == 0)
5651 ret = send_hole(sctx, key->offset);
5652 else
5653 ret = 0;
5654 }
5655 sctx->cur_inode_last_extent = extent_end;
5656 return ret;
5657}
5658
5659static int process_extent(struct send_ctx *sctx,
5660 struct btrfs_path *path,
5661 struct btrfs_key *key)
5662{
5663 struct clone_root *found_clone = NULL;
5664 int ret = 0;
5665
5666 if (S_ISLNK(sctx->cur_inode_mode))
5667 return 0;
5668
5669 if (sctx->parent_root && !sctx->cur_inode_new) {
5670 ret = is_extent_unchanged(sctx, path, key);
5671 if (ret < 0)
5672 goto out;
5673 if (ret) {
5674 ret = 0;
5675 goto out_hole;
5676 }
5677 } else {
5678 struct btrfs_file_extent_item *ei;
5679 u8 type;
5680
5681 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
5682 struct btrfs_file_extent_item);
5683 type = btrfs_file_extent_type(path->nodes[0], ei);
5684 if (type == BTRFS_FILE_EXTENT_PREALLOC ||
5685 type == BTRFS_FILE_EXTENT_REG) {
5686 /*
5687 * The send spec does not have a prealloc command yet,
5688 * so just leave a hole for prealloc'ed extents until
5689 * we have enough commands queued up to justify rev'ing
5690 * the send spec.
5691 */
5692 if (type == BTRFS_FILE_EXTENT_PREALLOC) {
5693 ret = 0;
5694 goto out;
5695 }
5696
5697 /* Have a hole, just skip it. */
5698 if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) {
5699 ret = 0;
5700 goto out;
5701 }
5702 }
5703 }
5704
5705 ret = find_extent_clone(sctx, path, key->objectid, key->offset,
5706 sctx->cur_inode_size, &found_clone);
5707 if (ret != -ENOENT && ret < 0)
5708 goto out;
5709
5710 ret = send_write_or_clone(sctx, path, key, found_clone);
5711 if (ret)
5712 goto out;
5713out_hole:
5714 ret = maybe_send_hole(sctx, path, key);
5715out:
5716 return ret;
5717}
5718
5719static int process_all_extents(struct send_ctx *sctx)
5720{
5721 int ret;
5722 struct btrfs_root *root;
5723 struct btrfs_path *path;
5724 struct btrfs_key key;
5725 struct btrfs_key found_key;
5726 struct extent_buffer *eb;
5727 int slot;
5728
5729 root = sctx->send_root;
5730 path = alloc_path_for_send();
5731 if (!path)
5732 return -ENOMEM;
5733
5734 key.objectid = sctx->cmp_key->objectid;
5735 key.type = BTRFS_EXTENT_DATA_KEY;
5736 key.offset = 0;
5737 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5738 if (ret < 0)
5739 goto out;
5740
5741 while (1) {
5742 eb = path->nodes[0];
5743 slot = path->slots[0];
5744
5745 if (slot >= btrfs_header_nritems(eb)) {
5746 ret = btrfs_next_leaf(root, path);
5747 if (ret < 0) {
5748 goto out;
5749 } else if (ret > 0) {
5750 ret = 0;
5751 break;
5752 }
5753 continue;
5754 }
5755
5756 btrfs_item_key_to_cpu(eb, &found_key, slot);
5757
5758 if (found_key.objectid != key.objectid ||
5759 found_key.type != key.type) {
5760 ret = 0;
5761 goto out;
5762 }
5763
5764 ret = process_extent(sctx, path, &found_key);
5765 if (ret < 0)
5766 goto out;
5767
5768 path->slots[0]++;
5769 }
5770
5771out:
5772 btrfs_free_path(path);
5773 return ret;
5774}
5775
5776static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end,
5777 int *pending_move,
5778 int *refs_processed)
5779{
5780 int ret = 0;
5781
5782 if (sctx->cur_ino == 0)
5783 goto out;
5784 if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid &&
5785 sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY)
5786 goto out;
5787 if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs))
5788 goto out;
5789
5790 ret = process_recorded_refs(sctx, pending_move);
5791 if (ret < 0)
5792 goto out;
5793
5794 *refs_processed = 1;
5795out:
5796 return ret;
5797}
5798
5799static int finish_inode_if_needed(struct send_ctx *sctx, int at_end)
5800{
5801 int ret = 0;
5802 u64 left_mode;
5803 u64 left_uid;
5804 u64 left_gid;
5805 u64 right_mode;
5806 u64 right_uid;
5807 u64 right_gid;
5808 int need_chmod = 0;
5809 int need_chown = 0;
5810 int need_truncate = 1;
5811 int pending_move = 0;
5812 int refs_processed = 0;
5813
5814 if (sctx->ignore_cur_inode)
5815 return 0;
5816
5817 ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move,
5818 &refs_processed);
5819 if (ret < 0)
5820 goto out;
5821
5822 /*
5823 * We have processed the refs and thus need to advance send_progress.
5824 * Now, calls to get_cur_xxx will take the updated refs of the current
5825 * inode into account.
5826 *
5827 * On the other hand, if our current inode is a directory and couldn't
5828 * be moved/renamed because its parent was renamed/moved too and it has
5829 * a higher inode number, we can only move/rename our current inode
5830 * after we moved/renamed its parent. Therefore in this case operate on
5831 * the old path (pre move/rename) of our current inode, and the
5832 * move/rename will be performed later.
5833 */
5834 if (refs_processed && !pending_move)
5835 sctx->send_progress = sctx->cur_ino + 1;
5836
5837 if (sctx->cur_ino == 0 || sctx->cur_inode_deleted)
5838 goto out;
5839 if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino)
5840 goto out;
5841
5842 ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL,
5843 &left_mode, &left_uid, &left_gid, NULL);
5844 if (ret < 0)
5845 goto out;
5846
5847 if (!sctx->parent_root || sctx->cur_inode_new) {
5848 need_chown = 1;
5849 if (!S_ISLNK(sctx->cur_inode_mode))
5850 need_chmod = 1;
5851 if (sctx->cur_inode_next_write_offset == sctx->cur_inode_size)
5852 need_truncate = 0;
5853 } else {
5854 u64 old_size;
5855
5856 ret = get_inode_info(sctx->parent_root, sctx->cur_ino,
5857 &old_size, NULL, &right_mode, &right_uid,
5858 &right_gid, NULL);
5859 if (ret < 0)
5860 goto out;
5861
5862 if (left_uid != right_uid || left_gid != right_gid)
5863 need_chown = 1;
5864 if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode)
5865 need_chmod = 1;
5866 if ((old_size == sctx->cur_inode_size) ||
5867 (sctx->cur_inode_size > old_size &&
5868 sctx->cur_inode_next_write_offset == sctx->cur_inode_size))
5869 need_truncate = 0;
5870 }
5871
5872 if (S_ISREG(sctx->cur_inode_mode)) {
5873 if (need_send_hole(sctx)) {
5874 if (sctx->cur_inode_last_extent == (u64)-1 ||
5875 sctx->cur_inode_last_extent <
5876 sctx->cur_inode_size) {
5877 ret = get_last_extent(sctx, (u64)-1);
5878 if (ret)
5879 goto out;
5880 }
5881 if (sctx->cur_inode_last_extent <
5882 sctx->cur_inode_size) {
5883 ret = send_hole(sctx, sctx->cur_inode_size);
5884 if (ret)
5885 goto out;
5886 }
5887 }
5888 if (need_truncate) {
5889 ret = send_truncate(sctx, sctx->cur_ino,
5890 sctx->cur_inode_gen,
5891 sctx->cur_inode_size);
5892 if (ret < 0)
5893 goto out;
5894 }
5895 }
5896
5897 if (need_chown) {
5898 ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5899 left_uid, left_gid);
5900 if (ret < 0)
5901 goto out;
5902 }
5903 if (need_chmod) {
5904 ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen,
5905 left_mode);
5906 if (ret < 0)
5907 goto out;
5908 }
5909
5910 /*
5911 * If other directory inodes depended on our current directory
5912 * inode's move/rename, now do their move/rename operations.
5913 */
5914 if (!is_waiting_for_move(sctx, sctx->cur_ino)) {
5915 ret = apply_children_dir_moves(sctx);
5916 if (ret)
5917 goto out;
5918 /*
5919 * Need to send that every time, no matter if it actually
5920 * changed between the two trees as we have done changes to
5921 * the inode before. If our inode is a directory and it's
5922 * waiting to be moved/renamed, we will send its utimes when
5923 * it's moved/renamed, therefore we don't need to do it here.
5924 */
5925 sctx->send_progress = sctx->cur_ino + 1;
5926 ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen);
5927 if (ret < 0)
5928 goto out;
5929 }
5930
5931out:
5932 return ret;
5933}
5934
5935struct parent_paths_ctx {
5936 struct list_head *refs;
5937 struct send_ctx *sctx;
5938};
5939
5940static int record_parent_ref(int num, u64 dir, int index, struct fs_path *name,
5941 void *ctx)
5942{
5943 struct parent_paths_ctx *ppctx = ctx;
5944
5945 return record_ref(ppctx->sctx->parent_root, dir, name, ppctx->sctx,
5946 ppctx->refs);
5947}
5948
5949/*
5950 * Issue unlink operations for all paths of the current inode found in the
5951 * parent snapshot.
5952 */
5953static int btrfs_unlink_all_paths(struct send_ctx *sctx)
5954{
5955 LIST_HEAD(deleted_refs);
5956 struct btrfs_path *path;
5957 struct btrfs_key key;
5958 struct parent_paths_ctx ctx;
5959 int ret;
5960
5961 path = alloc_path_for_send();
5962 if (!path)
5963 return -ENOMEM;
5964
5965 key.objectid = sctx->cur_ino;
5966 key.type = BTRFS_INODE_REF_KEY;
5967 key.offset = 0;
5968 ret = btrfs_search_slot(NULL, sctx->parent_root, &key, path, 0, 0);
5969 if (ret < 0)
5970 goto out;
5971
5972 ctx.refs = &deleted_refs;
5973 ctx.sctx = sctx;
5974
5975 while (true) {
5976 struct extent_buffer *eb = path->nodes[0];
5977 int slot = path->slots[0];
5978
5979 if (slot >= btrfs_header_nritems(eb)) {
5980 ret = btrfs_next_leaf(sctx->parent_root, path);
5981 if (ret < 0)
5982 goto out;
5983 else if (ret > 0)
5984 break;
5985 continue;
5986 }
5987
5988 btrfs_item_key_to_cpu(eb, &key, slot);
5989 if (key.objectid != sctx->cur_ino)
5990 break;
5991 if (key.type != BTRFS_INODE_REF_KEY &&
5992 key.type != BTRFS_INODE_EXTREF_KEY)
5993 break;
5994
5995 ret = iterate_inode_ref(sctx->parent_root, path, &key, 1,
5996 record_parent_ref, &ctx);
5997 if (ret < 0)
5998 goto out;
5999
6000 path->slots[0]++;
6001 }
6002
6003 while (!list_empty(&deleted_refs)) {
6004 struct recorded_ref *ref;
6005
6006 ref = list_first_entry(&deleted_refs, struct recorded_ref, list);
6007 ret = send_unlink(sctx, ref->full_path);
6008 if (ret < 0)
6009 goto out;
6010 fs_path_free(ref->full_path);
6011 list_del(&ref->list);
6012 kfree(ref);
6013 }
6014 ret = 0;
6015out:
6016 btrfs_free_path(path);
6017 if (ret)
6018 __free_recorded_refs(&deleted_refs);
6019 return ret;
6020}
6021
6022static int changed_inode(struct send_ctx *sctx,
6023 enum btrfs_compare_tree_result result)
6024{
6025 int ret = 0;
6026 struct btrfs_key *key = sctx->cmp_key;
6027 struct btrfs_inode_item *left_ii = NULL;
6028 struct btrfs_inode_item *right_ii = NULL;
6029 u64 left_gen = 0;
6030 u64 right_gen = 0;
6031
6032 sctx->cur_ino = key->objectid;
6033 sctx->cur_inode_new_gen = 0;
6034 sctx->cur_inode_last_extent = (u64)-1;
6035 sctx->cur_inode_next_write_offset = 0;
6036 sctx->ignore_cur_inode = false;
6037
6038 /*
6039 * Set send_progress to current inode. This will tell all get_cur_xxx
6040 * functions that the current inode's refs are not updated yet. Later,
6041 * when process_recorded_refs is finished, it is set to cur_ino + 1.
6042 */
6043 sctx->send_progress = sctx->cur_ino;
6044
6045 if (result == BTRFS_COMPARE_TREE_NEW ||
6046 result == BTRFS_COMPARE_TREE_CHANGED) {
6047 left_ii = btrfs_item_ptr(sctx->left_path->nodes[0],
6048 sctx->left_path->slots[0],
6049 struct btrfs_inode_item);
6050 left_gen = btrfs_inode_generation(sctx->left_path->nodes[0],
6051 left_ii);
6052 } else {
6053 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6054 sctx->right_path->slots[0],
6055 struct btrfs_inode_item);
6056 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6057 right_ii);
6058 }
6059 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6060 right_ii = btrfs_item_ptr(sctx->right_path->nodes[0],
6061 sctx->right_path->slots[0],
6062 struct btrfs_inode_item);
6063
6064 right_gen = btrfs_inode_generation(sctx->right_path->nodes[0],
6065 right_ii);
6066
6067 /*
6068 * The cur_ino = root dir case is special here. We can't treat
6069 * the inode as deleted+reused because it would generate a
6070 * stream that tries to delete/mkdir the root dir.
6071 */
6072 if (left_gen != right_gen &&
6073 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6074 sctx->cur_inode_new_gen = 1;
6075 }
6076
6077 /*
6078 * Normally we do not find inodes with a link count of zero (orphans)
6079 * because the most common case is to create a snapshot and use it
6080 * for a send operation. However other less common use cases involve
6081 * using a subvolume and send it after turning it to RO mode just
6082 * after deleting all hard links of a file while holding an open
6083 * file descriptor against it or turning a RO snapshot into RW mode,
6084 * keep an open file descriptor against a file, delete it and then
6085 * turn the snapshot back to RO mode before using it for a send
6086 * operation. So if we find such cases, ignore the inode and all its
6087 * items completely if it's a new inode, or if it's a changed inode
6088 * make sure all its previous paths (from the parent snapshot) are all
6089 * unlinked and all other the inode items are ignored.
6090 */
6091 if (result == BTRFS_COMPARE_TREE_NEW ||
6092 result == BTRFS_COMPARE_TREE_CHANGED) {
6093 u32 nlinks;
6094
6095 nlinks = btrfs_inode_nlink(sctx->left_path->nodes[0], left_ii);
6096 if (nlinks == 0) {
6097 sctx->ignore_cur_inode = true;
6098 if (result == BTRFS_COMPARE_TREE_CHANGED)
6099 ret = btrfs_unlink_all_paths(sctx);
6100 goto out;
6101 }
6102 }
6103
6104 if (result == BTRFS_COMPARE_TREE_NEW) {
6105 sctx->cur_inode_gen = left_gen;
6106 sctx->cur_inode_new = 1;
6107 sctx->cur_inode_deleted = 0;
6108 sctx->cur_inode_size = btrfs_inode_size(
6109 sctx->left_path->nodes[0], left_ii);
6110 sctx->cur_inode_mode = btrfs_inode_mode(
6111 sctx->left_path->nodes[0], left_ii);
6112 sctx->cur_inode_rdev = btrfs_inode_rdev(
6113 sctx->left_path->nodes[0], left_ii);
6114 if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID)
6115 ret = send_create_inode_if_needed(sctx);
6116 } else if (result == BTRFS_COMPARE_TREE_DELETED) {
6117 sctx->cur_inode_gen = right_gen;
6118 sctx->cur_inode_new = 0;
6119 sctx->cur_inode_deleted = 1;
6120 sctx->cur_inode_size = btrfs_inode_size(
6121 sctx->right_path->nodes[0], right_ii);
6122 sctx->cur_inode_mode = btrfs_inode_mode(
6123 sctx->right_path->nodes[0], right_ii);
6124 } else if (result == BTRFS_COMPARE_TREE_CHANGED) {
6125 /*
6126 * We need to do some special handling in case the inode was
6127 * reported as changed with a changed generation number. This
6128 * means that the original inode was deleted and new inode
6129 * reused the same inum. So we have to treat the old inode as
6130 * deleted and the new one as new.
6131 */
6132 if (sctx->cur_inode_new_gen) {
6133 /*
6134 * First, process the inode as if it was deleted.
6135 */
6136 sctx->cur_inode_gen = right_gen;
6137 sctx->cur_inode_new = 0;
6138 sctx->cur_inode_deleted = 1;
6139 sctx->cur_inode_size = btrfs_inode_size(
6140 sctx->right_path->nodes[0], right_ii);
6141 sctx->cur_inode_mode = btrfs_inode_mode(
6142 sctx->right_path->nodes[0], right_ii);
6143 ret = process_all_refs(sctx,
6144 BTRFS_COMPARE_TREE_DELETED);
6145 if (ret < 0)
6146 goto out;
6147
6148 /*
6149 * Now process the inode as if it was new.
6150 */
6151 sctx->cur_inode_gen = left_gen;
6152 sctx->cur_inode_new = 1;
6153 sctx->cur_inode_deleted = 0;
6154 sctx->cur_inode_size = btrfs_inode_size(
6155 sctx->left_path->nodes[0], left_ii);
6156 sctx->cur_inode_mode = btrfs_inode_mode(
6157 sctx->left_path->nodes[0], left_ii);
6158 sctx->cur_inode_rdev = btrfs_inode_rdev(
6159 sctx->left_path->nodes[0], left_ii);
6160 ret = send_create_inode_if_needed(sctx);
6161 if (ret < 0)
6162 goto out;
6163
6164 ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW);
6165 if (ret < 0)
6166 goto out;
6167 /*
6168 * Advance send_progress now as we did not get into
6169 * process_recorded_refs_if_needed in the new_gen case.
6170 */
6171 sctx->send_progress = sctx->cur_ino + 1;
6172
6173 /*
6174 * Now process all extents and xattrs of the inode as if
6175 * they were all new.
6176 */
6177 ret = process_all_extents(sctx);
6178 if (ret < 0)
6179 goto out;
6180 ret = process_all_new_xattrs(sctx);
6181 if (ret < 0)
6182 goto out;
6183 } else {
6184 sctx->cur_inode_gen = left_gen;
6185 sctx->cur_inode_new = 0;
6186 sctx->cur_inode_new_gen = 0;
6187 sctx->cur_inode_deleted = 0;
6188 sctx->cur_inode_size = btrfs_inode_size(
6189 sctx->left_path->nodes[0], left_ii);
6190 sctx->cur_inode_mode = btrfs_inode_mode(
6191 sctx->left_path->nodes[0], left_ii);
6192 }
6193 }
6194
6195out:
6196 return ret;
6197}
6198
6199/*
6200 * We have to process new refs before deleted refs, but compare_trees gives us
6201 * the new and deleted refs mixed. To fix this, we record the new/deleted refs
6202 * first and later process them in process_recorded_refs.
6203 * For the cur_inode_new_gen case, we skip recording completely because
6204 * changed_inode did already initiate processing of refs. The reason for this is
6205 * that in this case, compare_tree actually compares the refs of 2 different
6206 * inodes. To fix this, process_all_refs is used in changed_inode to handle all
6207 * refs of the right tree as deleted and all refs of the left tree as new.
6208 */
6209static int changed_ref(struct send_ctx *sctx,
6210 enum btrfs_compare_tree_result result)
6211{
6212 int ret = 0;
6213
6214 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6215 inconsistent_snapshot_error(sctx, result, "reference");
6216 return -EIO;
6217 }
6218
6219 if (!sctx->cur_inode_new_gen &&
6220 sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) {
6221 if (result == BTRFS_COMPARE_TREE_NEW)
6222 ret = record_new_ref(sctx);
6223 else if (result == BTRFS_COMPARE_TREE_DELETED)
6224 ret = record_deleted_ref(sctx);
6225 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6226 ret = record_changed_ref(sctx);
6227 }
6228
6229 return ret;
6230}
6231
6232/*
6233 * Process new/deleted/changed xattrs. We skip processing in the
6234 * cur_inode_new_gen case because changed_inode did already initiate processing
6235 * of xattrs. The reason is the same as in changed_ref
6236 */
6237static int changed_xattr(struct send_ctx *sctx,
6238 enum btrfs_compare_tree_result result)
6239{
6240 int ret = 0;
6241
6242 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6243 inconsistent_snapshot_error(sctx, result, "xattr");
6244 return -EIO;
6245 }
6246
6247 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6248 if (result == BTRFS_COMPARE_TREE_NEW)
6249 ret = process_new_xattr(sctx);
6250 else if (result == BTRFS_COMPARE_TREE_DELETED)
6251 ret = process_deleted_xattr(sctx);
6252 else if (result == BTRFS_COMPARE_TREE_CHANGED)
6253 ret = process_changed_xattr(sctx);
6254 }
6255
6256 return ret;
6257}
6258
6259/*
6260 * Process new/deleted/changed extents. We skip processing in the
6261 * cur_inode_new_gen case because changed_inode did already initiate processing
6262 * of extents. The reason is the same as in changed_ref
6263 */
6264static int changed_extent(struct send_ctx *sctx,
6265 enum btrfs_compare_tree_result result)
6266{
6267 int ret = 0;
6268
6269 if (sctx->cur_ino != sctx->cmp_key->objectid) {
6270
6271 if (result == BTRFS_COMPARE_TREE_CHANGED) {
6272 struct extent_buffer *leaf_l;
6273 struct extent_buffer *leaf_r;
6274 struct btrfs_file_extent_item *ei_l;
6275 struct btrfs_file_extent_item *ei_r;
6276
6277 leaf_l = sctx->left_path->nodes[0];
6278 leaf_r = sctx->right_path->nodes[0];
6279 ei_l = btrfs_item_ptr(leaf_l,
6280 sctx->left_path->slots[0],
6281 struct btrfs_file_extent_item);
6282 ei_r = btrfs_item_ptr(leaf_r,
6283 sctx->right_path->slots[0],
6284 struct btrfs_file_extent_item);
6285
6286 /*
6287 * We may have found an extent item that has changed
6288 * only its disk_bytenr field and the corresponding
6289 * inode item was not updated. This case happens due to
6290 * very specific timings during relocation when a leaf
6291 * that contains file extent items is COWed while
6292 * relocation is ongoing and its in the stage where it
6293 * updates data pointers. So when this happens we can
6294 * safely ignore it since we know it's the same extent,
6295 * but just at different logical and physical locations
6296 * (when an extent is fully replaced with a new one, we
6297 * know the generation number must have changed too,
6298 * since snapshot creation implies committing the current
6299 * transaction, and the inode item must have been updated
6300 * as well).
6301 * This replacement of the disk_bytenr happens at
6302 * relocation.c:replace_file_extents() through
6303 * relocation.c:btrfs_reloc_cow_block().
6304 */
6305 if (btrfs_file_extent_generation(leaf_l, ei_l) ==
6306 btrfs_file_extent_generation(leaf_r, ei_r) &&
6307 btrfs_file_extent_ram_bytes(leaf_l, ei_l) ==
6308 btrfs_file_extent_ram_bytes(leaf_r, ei_r) &&
6309 btrfs_file_extent_compression(leaf_l, ei_l) ==
6310 btrfs_file_extent_compression(leaf_r, ei_r) &&
6311 btrfs_file_extent_encryption(leaf_l, ei_l) ==
6312 btrfs_file_extent_encryption(leaf_r, ei_r) &&
6313 btrfs_file_extent_other_encoding(leaf_l, ei_l) ==
6314 btrfs_file_extent_other_encoding(leaf_r, ei_r) &&
6315 btrfs_file_extent_type(leaf_l, ei_l) ==
6316 btrfs_file_extent_type(leaf_r, ei_r) &&
6317 btrfs_file_extent_disk_bytenr(leaf_l, ei_l) !=
6318 btrfs_file_extent_disk_bytenr(leaf_r, ei_r) &&
6319 btrfs_file_extent_disk_num_bytes(leaf_l, ei_l) ==
6320 btrfs_file_extent_disk_num_bytes(leaf_r, ei_r) &&
6321 btrfs_file_extent_offset(leaf_l, ei_l) ==
6322 btrfs_file_extent_offset(leaf_r, ei_r) &&
6323 btrfs_file_extent_num_bytes(leaf_l, ei_l) ==
6324 btrfs_file_extent_num_bytes(leaf_r, ei_r))
6325 return 0;
6326 }
6327
6328 inconsistent_snapshot_error(sctx, result, "extent");
6329 return -EIO;
6330 }
6331
6332 if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) {
6333 if (result != BTRFS_COMPARE_TREE_DELETED)
6334 ret = process_extent(sctx, sctx->left_path,
6335 sctx->cmp_key);
6336 }
6337
6338 return ret;
6339}
6340
6341static int dir_changed(struct send_ctx *sctx, u64 dir)
6342{
6343 u64 orig_gen, new_gen;
6344 int ret;
6345
6346 ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL,
6347 NULL, NULL);
6348 if (ret)
6349 return ret;
6350
6351 ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL,
6352 NULL, NULL, NULL);
6353 if (ret)
6354 return ret;
6355
6356 return (orig_gen != new_gen) ? 1 : 0;
6357}
6358
6359static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path,
6360 struct btrfs_key *key)
6361{
6362 struct btrfs_inode_extref *extref;
6363 struct extent_buffer *leaf;
6364 u64 dirid = 0, last_dirid = 0;
6365 unsigned long ptr;
6366 u32 item_size;
6367 u32 cur_offset = 0;
6368 int ref_name_len;
6369 int ret = 0;
6370
6371 /* Easy case, just check this one dirid */
6372 if (key->type == BTRFS_INODE_REF_KEY) {
6373 dirid = key->offset;
6374
6375 ret = dir_changed(sctx, dirid);
6376 goto out;
6377 }
6378
6379 leaf = path->nodes[0];
6380 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
6381 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
6382 while (cur_offset < item_size) {
6383 extref = (struct btrfs_inode_extref *)(ptr +
6384 cur_offset);
6385 dirid = btrfs_inode_extref_parent(leaf, extref);
6386 ref_name_len = btrfs_inode_extref_name_len(leaf, extref);
6387 cur_offset += ref_name_len + sizeof(*extref);
6388 if (dirid == last_dirid)
6389 continue;
6390 ret = dir_changed(sctx, dirid);
6391 if (ret)
6392 break;
6393 last_dirid = dirid;
6394 }
6395out:
6396 return ret;
6397}
6398
6399/*
6400 * Updates compare related fields in sctx and simply forwards to the actual
6401 * changed_xxx functions.
6402 */
6403static int changed_cb(struct btrfs_path *left_path,
6404 struct btrfs_path *right_path,
6405 struct btrfs_key *key,
6406 enum btrfs_compare_tree_result result,
6407 void *ctx)
6408{
6409 int ret = 0;
6410 struct send_ctx *sctx = ctx;
6411
6412 if (result == BTRFS_COMPARE_TREE_SAME) {
6413 if (key->type == BTRFS_INODE_REF_KEY ||
6414 key->type == BTRFS_INODE_EXTREF_KEY) {
6415 ret = compare_refs(sctx, left_path, key);
6416 if (!ret)
6417 return 0;
6418 if (ret < 0)
6419 return ret;
6420 } else if (key->type == BTRFS_EXTENT_DATA_KEY) {
6421 return maybe_send_hole(sctx, left_path, key);
6422 } else {
6423 return 0;
6424 }
6425 result = BTRFS_COMPARE_TREE_CHANGED;
6426 ret = 0;
6427 }
6428
6429 sctx->left_path = left_path;
6430 sctx->right_path = right_path;
6431 sctx->cmp_key = key;
6432
6433 ret = finish_inode_if_needed(sctx, 0);
6434 if (ret < 0)
6435 goto out;
6436
6437 /* Ignore non-FS objects */
6438 if (key->objectid == BTRFS_FREE_INO_OBJECTID ||
6439 key->objectid == BTRFS_FREE_SPACE_OBJECTID)
6440 goto out;
6441
6442 if (key->type == BTRFS_INODE_ITEM_KEY) {
6443 ret = changed_inode(sctx, result);
6444 } else if (!sctx->ignore_cur_inode) {
6445 if (key->type == BTRFS_INODE_REF_KEY ||
6446 key->type == BTRFS_INODE_EXTREF_KEY)
6447 ret = changed_ref(sctx, result);
6448 else if (key->type == BTRFS_XATTR_ITEM_KEY)
6449 ret = changed_xattr(sctx, result);
6450 else if (key->type == BTRFS_EXTENT_DATA_KEY)
6451 ret = changed_extent(sctx, result);
6452 }
6453
6454out:
6455 return ret;
6456}
6457
6458static int full_send_tree(struct send_ctx *sctx)
6459{
6460 int ret;
6461 struct btrfs_root *send_root = sctx->send_root;
6462 struct btrfs_key key;
6463 struct btrfs_path *path;
6464 struct extent_buffer *eb;
6465 int slot;
6466
6467 path = alloc_path_for_send();
6468 if (!path)
6469 return -ENOMEM;
6470
6471 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6472 key.type = BTRFS_INODE_ITEM_KEY;
6473 key.offset = 0;
6474
6475 ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0);
6476 if (ret < 0)
6477 goto out;
6478 if (ret)
6479 goto out_finish;
6480
6481 while (1) {
6482 eb = path->nodes[0];
6483 slot = path->slots[0];
6484 btrfs_item_key_to_cpu(eb, &key, slot);
6485
6486 ret = changed_cb(path, NULL, &key,
6487 BTRFS_COMPARE_TREE_NEW, sctx);
6488 if (ret < 0)
6489 goto out;
6490
6491 ret = btrfs_next_item(send_root, path);
6492 if (ret < 0)
6493 goto out;
6494 if (ret) {
6495 ret = 0;
6496 break;
6497 }
6498 }
6499
6500out_finish:
6501 ret = finish_inode_if_needed(sctx, 1);
6502
6503out:
6504 btrfs_free_path(path);
6505 return ret;
6506}
6507
6508static int send_subvol(struct send_ctx *sctx)
6509{
6510 int ret;
6511
6512 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) {
6513 ret = send_header(sctx);
6514 if (ret < 0)
6515 goto out;
6516 }
6517
6518 ret = send_subvol_begin(sctx);
6519 if (ret < 0)
6520 goto out;
6521
6522 if (sctx->parent_root) {
6523 ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root,
6524 changed_cb, sctx);
6525 if (ret < 0)
6526 goto out;
6527 ret = finish_inode_if_needed(sctx, 1);
6528 if (ret < 0)
6529 goto out;
6530 } else {
6531 ret = full_send_tree(sctx);
6532 if (ret < 0)
6533 goto out;
6534 }
6535
6536out:
6537 free_recorded_refs(sctx);
6538 return ret;
6539}
6540
6541/*
6542 * If orphan cleanup did remove any orphans from a root, it means the tree
6543 * was modified and therefore the commit root is not the same as the current
6544 * root anymore. This is a problem, because send uses the commit root and
6545 * therefore can see inode items that don't exist in the current root anymore,
6546 * and for example make calls to btrfs_iget, which will do tree lookups based
6547 * on the current root and not on the commit root. Those lookups will fail,
6548 * returning a -ESTALE error, and making send fail with that error. So make
6549 * sure a send does not see any orphans we have just removed, and that it will
6550 * see the same inodes regardless of whether a transaction commit happened
6551 * before it started (meaning that the commit root will be the same as the
6552 * current root) or not.
6553 */
6554static int ensure_commit_roots_uptodate(struct send_ctx *sctx)
6555{
6556 int i;
6557 struct btrfs_trans_handle *trans = NULL;
6558
6559again:
6560 if (sctx->parent_root &&
6561 sctx->parent_root->node != sctx->parent_root->commit_root)
6562 goto commit_trans;
6563
6564 for (i = 0; i < sctx->clone_roots_cnt; i++)
6565 if (sctx->clone_roots[i].root->node !=
6566 sctx->clone_roots[i].root->commit_root)
6567 goto commit_trans;
6568
6569 if (trans)
6570 return btrfs_end_transaction(trans);
6571
6572 return 0;
6573
6574commit_trans:
6575 /* Use any root, all fs roots will get their commit roots updated. */
6576 if (!trans) {
6577 trans = btrfs_join_transaction(sctx->send_root);
6578 if (IS_ERR(trans))
6579 return PTR_ERR(trans);
6580 goto again;
6581 }
6582
6583 return btrfs_commit_transaction(trans);
6584}
6585
6586static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6587{
6588 spin_lock(&root->root_item_lock);
6589 root->send_in_progress--;
6590 /*
6591 * Not much left to do, we don't know why it's unbalanced and
6592 * can't blindly reset it to 0.
6593 */
6594 if (root->send_in_progress < 0)
6595 btrfs_err(root->fs_info,
6596 "send_in_progress unbalanced %d root %llu",
6597 root->send_in_progress, root->root_key.objectid);
6598 spin_unlock(&root->root_item_lock);
6599}
6600
6601long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
6602{
6603 int ret = 0;
6604 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6605 struct btrfs_fs_info *fs_info = send_root->fs_info;
6606 struct btrfs_root *clone_root;
6607 struct btrfs_key key;
6608 struct send_ctx *sctx = NULL;
6609 u32 i;
6610 u64 *clone_sources_tmp = NULL;
6611 int clone_sources_to_rollback = 0;
6612 unsigned alloc_size;
6613 int sort_clone_roots = 0;
6614 int index;
6615
6616 if (!capable(CAP_SYS_ADMIN))
6617 return -EPERM;
6618
6619 /*
6620 * The subvolume must remain read-only during send, protect against
6621 * making it RW. This also protects against deletion.
6622 */
6623 spin_lock(&send_root->root_item_lock);
6624 send_root->send_in_progress++;
6625 spin_unlock(&send_root->root_item_lock);
6626
6627 /*
6628 * This is done when we lookup the root, it should already be complete
6629 * by the time we get here.
6630 */
6631 WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE);
6632
6633 /*
6634 * Userspace tools do the checks and warn the user if it's
6635 * not RO.
6636 */
6637 if (!btrfs_root_readonly(send_root)) {
6638 ret = -EPERM;
6639 goto out;
6640 }
6641
6642 /*
6643 * Check that we don't overflow at later allocations, we request
6644 * clone_sources_count + 1 items, and compare to unsigned long inside
6645 * access_ok.
6646 */
6647 if (arg->clone_sources_count >
6648 ULONG_MAX / sizeof(struct clone_root) - 1) {
6649 ret = -EINVAL;
6650 goto out;
6651 }
6652
6653 if (!access_ok(VERIFY_READ, arg->clone_sources,
6654 sizeof(*arg->clone_sources) *
6655 arg->clone_sources_count)) {
6656 ret = -EFAULT;
6657 goto out;
6658 }
6659
6660 if (arg->flags & ~BTRFS_SEND_FLAG_MASK) {
6661 ret = -EINVAL;
6662 goto out;
6663 }
6664
6665 sctx = kzalloc(sizeof(struct send_ctx), GFP_KERNEL);
6666 if (!sctx) {
6667 ret = -ENOMEM;
6668 goto out;
6669 }
6670
6671 INIT_LIST_HEAD(&sctx->new_refs);
6672 INIT_LIST_HEAD(&sctx->deleted_refs);
6673 INIT_RADIX_TREE(&sctx->name_cache, GFP_KERNEL);
6674 INIT_LIST_HEAD(&sctx->name_cache_list);
6675
6676 sctx->flags = arg->flags;
6677
6678 sctx->send_filp = fget(arg->send_fd);
6679 if (!sctx->send_filp) {
6680 ret = -EBADF;
6681 goto out;
6682 }
6683
6684 sctx->send_root = send_root;
6685 /*
6686 * Unlikely but possible, if the subvolume is marked for deletion but
6687 * is slow to remove the directory entry, send can still be started
6688 */
6689 if (btrfs_root_dead(sctx->send_root)) {
6690 ret = -EPERM;
6691 goto out;
6692 }
6693
6694 sctx->clone_roots_cnt = arg->clone_sources_count;
6695
6696 sctx->send_max_size = BTRFS_SEND_BUF_SIZE;
6697 sctx->send_buf = kvmalloc(sctx->send_max_size, GFP_KERNEL);
6698 if (!sctx->send_buf) {
6699 ret = -ENOMEM;
6700 goto out;
6701 }
6702
6703 sctx->read_buf = kvmalloc(BTRFS_SEND_READ_SIZE, GFP_KERNEL);
6704 if (!sctx->read_buf) {
6705 ret = -ENOMEM;
6706 goto out;
6707 }
6708
6709 sctx->pending_dir_moves = RB_ROOT;
6710 sctx->waiting_dir_moves = RB_ROOT;
6711 sctx->orphan_dirs = RB_ROOT;
6712
6713 alloc_size = sizeof(struct clone_root) * (arg->clone_sources_count + 1);
6714
6715 sctx->clone_roots = kzalloc(alloc_size, GFP_KERNEL);
6716 if (!sctx->clone_roots) {
6717 ret = -ENOMEM;
6718 goto out;
6719 }
6720
6721 alloc_size = arg->clone_sources_count * sizeof(*arg->clone_sources);
6722
6723 if (arg->clone_sources_count) {
6724 clone_sources_tmp = kvmalloc(alloc_size, GFP_KERNEL);
6725 if (!clone_sources_tmp) {
6726 ret = -ENOMEM;
6727 goto out;
6728 }
6729
6730 ret = copy_from_user(clone_sources_tmp, arg->clone_sources,
6731 alloc_size);
6732 if (ret) {
6733 ret = -EFAULT;
6734 goto out;
6735 }
6736
6737 for (i = 0; i < arg->clone_sources_count; i++) {
6738 key.objectid = clone_sources_tmp[i];
6739 key.type = BTRFS_ROOT_ITEM_KEY;
6740 key.offset = (u64)-1;
6741
6742 index = srcu_read_lock(&fs_info->subvol_srcu);
6743
6744 clone_root = btrfs_read_fs_root_no_name(fs_info, &key);
6745 if (IS_ERR(clone_root)) {
6746 srcu_read_unlock(&fs_info->subvol_srcu, index);
6747 ret = PTR_ERR(clone_root);
6748 goto out;
6749 }
6750 spin_lock(&clone_root->root_item_lock);
6751 if (!btrfs_root_readonly(clone_root) ||
6752 btrfs_root_dead(clone_root)) {
6753 spin_unlock(&clone_root->root_item_lock);
6754 srcu_read_unlock(&fs_info->subvol_srcu, index);
6755 ret = -EPERM;
6756 goto out;
6757 }
6758 clone_root->send_in_progress++;
6759 spin_unlock(&clone_root->root_item_lock);
6760 srcu_read_unlock(&fs_info->subvol_srcu, index);
6761
6762 sctx->clone_roots[i].root = clone_root;
6763 clone_sources_to_rollback = i + 1;
6764 }
6765 kvfree(clone_sources_tmp);
6766 clone_sources_tmp = NULL;
6767 }
6768
6769 if (arg->parent_root) {
6770 key.objectid = arg->parent_root;
6771 key.type = BTRFS_ROOT_ITEM_KEY;
6772 key.offset = (u64)-1;
6773
6774 index = srcu_read_lock(&fs_info->subvol_srcu);
6775
6776 sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key);
6777 if (IS_ERR(sctx->parent_root)) {
6778 srcu_read_unlock(&fs_info->subvol_srcu, index);
6779 ret = PTR_ERR(sctx->parent_root);
6780 goto out;
6781 }
6782
6783 spin_lock(&sctx->parent_root->root_item_lock);
6784 sctx->parent_root->send_in_progress++;
6785 if (!btrfs_root_readonly(sctx->parent_root) ||
6786 btrfs_root_dead(sctx->parent_root)) {
6787 spin_unlock(&sctx->parent_root->root_item_lock);
6788 srcu_read_unlock(&fs_info->subvol_srcu, index);
6789 ret = -EPERM;
6790 goto out;
6791 }
6792 spin_unlock(&sctx->parent_root->root_item_lock);
6793
6794 srcu_read_unlock(&fs_info->subvol_srcu, index);
6795 }
6796
6797 /*
6798 * Clones from send_root are allowed, but only if the clone source
6799 * is behind the current send position. This is checked while searching
6800 * for possible clone sources.
6801 */
6802 sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root;
6803
6804 /* We do a bsearch later */
6805 sort(sctx->clone_roots, sctx->clone_roots_cnt,
6806 sizeof(*sctx->clone_roots), __clone_root_cmp_sort,
6807 NULL);
6808 sort_clone_roots = 1;
6809
6810 ret = ensure_commit_roots_uptodate(sctx);
6811 if (ret)
6812 goto out;
6813
6814 current->journal_info = BTRFS_SEND_TRANS_STUB;
6815 ret = send_subvol(sctx);
6816 current->journal_info = NULL;
6817 if (ret < 0)
6818 goto out;
6819
6820 if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) {
6821 ret = begin_cmd(sctx, BTRFS_SEND_C_END);
6822 if (ret < 0)
6823 goto out;
6824 ret = send_cmd(sctx);
6825 if (ret < 0)
6826 goto out;
6827 }
6828
6829out:
6830 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves));
6831 while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) {
6832 struct rb_node *n;
6833 struct pending_dir_move *pm;
6834
6835 n = rb_first(&sctx->pending_dir_moves);
6836 pm = rb_entry(n, struct pending_dir_move, node);
6837 while (!list_empty(&pm->list)) {
6838 struct pending_dir_move *pm2;
6839
6840 pm2 = list_first_entry(&pm->list,
6841 struct pending_dir_move, list);
6842 free_pending_move(sctx, pm2);
6843 }
6844 free_pending_move(sctx, pm);
6845 }
6846
6847 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves));
6848 while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) {
6849 struct rb_node *n;
6850 struct waiting_dir_move *dm;
6851
6852 n = rb_first(&sctx->waiting_dir_moves);
6853 dm = rb_entry(n, struct waiting_dir_move, node);
6854 rb_erase(&dm->node, &sctx->waiting_dir_moves);
6855 kfree(dm);
6856 }
6857
6858 WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->orphan_dirs));
6859 while (sctx && !RB_EMPTY_ROOT(&sctx->orphan_dirs)) {
6860 struct rb_node *n;
6861 struct orphan_dir_info *odi;
6862
6863 n = rb_first(&sctx->orphan_dirs);
6864 odi = rb_entry(n, struct orphan_dir_info, node);
6865 free_orphan_dir_info(sctx, odi);
6866 }
6867
6868 if (sort_clone_roots) {
6869 for (i = 0; i < sctx->clone_roots_cnt; i++)
6870 btrfs_root_dec_send_in_progress(
6871 sctx->clone_roots[i].root);
6872 } else {
6873 for (i = 0; sctx && i < clone_sources_to_rollback; i++)
6874 btrfs_root_dec_send_in_progress(
6875 sctx->clone_roots[i].root);
6876
6877 btrfs_root_dec_send_in_progress(send_root);
6878 }
6879 if (sctx && !IS_ERR_OR_NULL(sctx->parent_root))
6880 btrfs_root_dec_send_in_progress(sctx->parent_root);
6881
6882 kvfree(clone_sources_tmp);
6883
6884 if (sctx) {
6885 if (sctx->send_filp)
6886 fput(sctx->send_filp);
6887
6888 kvfree(sctx->clone_roots);
6889 kvfree(sctx->send_buf);
6890 kvfree(sctx->read_buf);
6891
6892 name_cache_free(sctx);
6893
6894 kfree(sctx);
6895 }
6896
6897 return ret;
6898}