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