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