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