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