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