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