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