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