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