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