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