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